-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9.c
69 lines (69 loc) · 1.49 KB
/
9.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
#include <stdio.h>
int a[10][10], n, opc = 0, visit[10], acyclic = 1, S[10], top = -1, pop[10], k = 0;
void dfs(int v)
{
int i, f = 0;
S[++top] = v;
opc++;
while (top != -1)
{
f = 0;
v = S[top];
visit[v] = 1;
for (i = 1; i <= n; i++)
{
if (a[v][i] && visit[i] == 1)
{
acyclic = 0;
}
if (a[v][i] && visit[i] == -1)
{
opc++;
S[++top] = i;
visit[i] = 0;
f = 1;
break;
}
}
if (f == 0)
{
opc++;
visit[S[top]] = 2;
pop[k++] = S[top--];
}
}
}
void topoSort()
{
int i, j;
for (i = 1; i <= n; i++)
{
if (visit[i] == -1 || visit[i] == 0)
dfs(i);
}
if (acyclic)
{
printf("\nToplogical Sorting is \n");
for (j = n - 1; j >= 0; j--)
printf("%d ", pop[j]);
printf("\n");
}
else
printf("Toplogical Sorting is not possible because graph is Cyclic\n");
}
int main()
{
int i, j, start = 1;
printf("Enter the number of nodes : ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
visit[i] = -1;
printf("Enter the adjacency matrix\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
dfs(start);
topoSort();
printf("Operation Count : %d\n", opc);
return 0;
}