-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTopological Sort.cpp
More file actions
45 lines (42 loc) · 922 Bytes
/
Topological Sort.cpp
File metadata and controls
45 lines (42 loc) · 922 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int i,j,k,n,a[10][10],indeg[10],flag[10],count=0;
printf("Enter the no of vertices:");
scanf("%d",&n);
printf("Enter the adjacency matrix: ");
for(i=0; i<n; i++)
for(j=0; j<n; j++)
scanf("%d",&a[i][j]);
for(i=0; i<n; i++)
{
indeg[i]=0;
flag[i]=0;
}
for(i=0; i<n; i++)
for(j=0; j<n; j++)
indeg[i]=indeg[i]+a[j][i];
printf("The topological order is: ");
while(count<n)
{
for(k=0; k<n; k++)
{
if(indeg[k]==0 && flag[k]==0)
{
printf("%d",k+1);
flag [k]=1;
}
for(i=0; i<n; i++)
{
if(a[i][k]==1)
{
indeg[k]--;
}
}
}
count++;
}
return 0;
}