-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoposort.c
167 lines (141 loc) · 3.14 KB
/
toposort.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <string.h>
#define MAX 100
int visited[MAX];
int mat[MAX][MAX];
int num;
#define WHITE 0
#define GREY 1
#define BLACK 2
int Stack[MAX];
int f;
static int visitedcount;
static int cyclednode;
static int cyclednodereached;
static int numcycle;
int cycle[MAX];
int IsFull(void)
{
if (f == MAX-1) {
printf("Stack is FULL..\n");
return 1;
}
else
return 0;
}
int IsEmpty(void)
{
if (f == -1) {
printf("Stack is Empty..\n");
return 1;
}
else
return 0;
}
int pop(void)
{
if (IsEmpty()) {
return -1;
} else {
int element = Stack[f];
f--;
return element;
}
}
void push(int element)
{
if (IsFull()) {
return;
} else {
Stack[++f] = element;
}
}
int TopoSort(int startnode)
{
int k;
for(k=0; k < num; k++) {
if (mat[startnode][k] != 0 && visited[k] == WHITE) {
visitedcount++;
visited[k] = GREY;
if (TopoSort(k)) {
} else {
if (visited[startnode] == GREY) {
if (startnode == cyclednode) {
cycle[++numcycle] = startnode;
cyclednodereached = 1;
}
if (cyclednodereached == 0)
cycle[++numcycle] = startnode;
}
return 0;
}
} else if (mat[startnode][k] != 0 && visited[k] == GREY){
printf("Oops!! Loop detected while sorting, TopoSort not possible..\n", k);
cyclednode = k;
cycle[++numcycle] = k;
cycle[++numcycle] = startnode;
return 0;
}
}
//printf("Pushing startnode[%d] to stack and mark Black\n", startnode);
visited[startnode] = BLACK;
push(startnode);
return 1;
}
void PrintJobs(void)
{
printf("TopoSort Done, print jobs in decreasing order of their finish time\n");
printf("\n");
while(!IsEmpty()) {
printf("Node [%d]\n", pop());
}
}
void PrintCycle()
{
while(numcycle >=0) {
printf("[%d]", cycle[numcycle]);
numcycle--;
}
}
int main(void)
{
int r, c, k, startnode;
printf("Welcome to Topological Sort Algorithm..\n");
printf("Enter number of nodes in Entire graph..\n");
scanf("%d", &num);
for(r=0; r<num; r++) {
for(c=0; c<num; c++) {
scanf("%d", &mat[r][c]);
}
}
printf("Original graph\n");
printf("===============\n");
for(r=0; r<num; r++) {
for(c=0; c<num; c++) {
printf("[%d]", mat[r][c]);
}
printf("\n");
}
/* Initialize first node */
f = -1;
for(k=0; k < num; k++) {
visited[k] = WHITE;
cycle[k] = -1;
}
numcycle = -1;
visitedcount = 1;
for(k= 0; k < num; k++){
if (visited[k] == WHITE) {
visited[k] = GREY;
if (TopoSort(k)) {
} else {
printf("Cycle detected.. print the cycle\n");
break;
}
}
}
if (numcycle>=0)
PrintCycle();
else
PrintJobs();
}