-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdag.c
107 lines (89 loc) · 2.15 KB
/
dag.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
// directed asyclic graph ...
// topological sort
struct nodes {
struct nodes *outedges;
int nodenum;
};
struct stacknode {
struct stacknode *next;
int nodenum;
};
struct stacknode *stackhead;
typedef enum {
false,
true,
}bool;
void push(int node) {
struct stacknode *n = malloc(sizeof(struct stacknode));
assert(n);
n->nodenum = node;
n->next = stackhead;
stackhead = n;
}
int pop() {
int node;
struct stacknode *n;
if (!stackhead)
return -1;
node = stackhead->nodenum;
n = stackhead;
stackhead = stackhead->next;
free(n);
return node;
}
void toposort(struct nodes *nodelist, int node, bool *visited) {
visited[node] = true;
for (int j=0; nodelist[node].outedges[j].nodenum;j++) {
if (!visited[j])
toposort(nodelist, j, visited);
}
push(node+1);
}
int main(int argc, char **argv) {
bool* visited;
int nnodes, nedges, node, edge;
struct nodes* nodelist, *edgelist;
printf("How many nodes? \n");
scanf("%d", &nnodes);
if (!nnodes)
return 0;
nodelist = malloc((nnodes+1) * sizeof(struct nodes));
assert(nodelist);
memset(nodelist, 0, (nnodes+1) * sizeof(struct nodes));
visited = malloc(nnodes * sizeof(bool));
assert(visited);
memset(visited, false, nnodes * sizeof(bool));
for (int i=0;i<nnodes;i++) {
nodelist[i].nodenum = i+1;
printf("How many outgoing edges from node: %d?", i+1);
scanf("%d", &nedges);
fflush(stdin);
edgelist = malloc((nedges+1) * sizeof(struct nodes));
assert(edgelist);
memset(edgelist, 0, (nedges+1) * sizeof(struct nodes));
nodelist[i].outedges = edgelist;
if (!nedges)
continue;
printf("Enter outgoing edges ... \n");
for (int j = 0; j< nedges; j++) {
scanf("%d", &edge); fflush(stdin);
edgelist[j].nodenum = edge;
edgelist[j].outedges = NULL;
} /* for nedges */
} /* for nnodes */
for (int i = 0; i< nnodes; i++) {
if (!visited[i]) {
toposort(nodelist, i, visited);
}
}
printf("Sorted order is:\n");
while((node = pop()) != -1) {
printf("%d ", node);
}
printf("\n");
return 0;
}