-
Notifications
You must be signed in to change notification settings - Fork 0
/
TopologicalSort.c
66 lines (59 loc) · 1.23 KB
/
TopologicalSort.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
//Lab Program
#include <stdio.h>
#define MAX 10
void main()
{
int i, j, arr[MAX][MAX], n, queue[MAX], flag[MAX];
int m = 0, k;
printf("Enter the number of vertices: ");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
flag[i] = 0;
queue[i] = 0;
}
printf("Enter the adjacency matrix: \n");
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
scanf("%d", &arr[i][j]);
if(arr[i][j])
{
flag[j]++;
}
}
}
for(k = 1; k <= n; k++)
{
for(i = 1; i <= n; i++)
{
if(flag[i] == 0)
{
flag[i] = -1;
queue[++m] = i;
for(j = 1; j <= n; j++)
{
if(arr[i][j] == 1)
{
flag[j]--;
}
}
break;
}
}
}
if(m == n)
{
printf("The topological order is: \n");
for(i = 1; i <= n; i++)
{
printf("%d ", queue[i]);
}
printf("\n");
}
else
{
printf("No topological order possible as it is not a DAG.\n");
}
}