-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopological-Sorting-By-DFS-Method.c
72 lines (70 loc) · 1.35 KB
/
Topological-Sorting-By-DFS-Method.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
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
int j=0,pop[MAX],vis[MAX];
void dfs(int,int,int [][MAX]);
void topo(int,int [][MAX]);
int main()
{
int i,k,n,a[MAX][MAX];
printf("Enter no of vertices: ");
scanf("%d",&n);
printf("Enter adjacency matrix\n");
for(i=1;i<=n;i++)
{
for(k=1;k<=n;k++)
{
scanf("%d",&a[i][k]);
}
}
topo(n,a);
printf("Topological Order:");
for(i=n;i>0;i--)
printf("%d ",pop[i]);
return 0;
}
void dfs(int u,int n,int a[][MAX])
{
int i,v,top=-1,s[MAX];
vis[u]=1;
s[++top]=u;
while(top!= -1)
{
for(v=1;v<=n;v++)
{
if(a[u][v]==1 && vis[v]==1)
{
for(i=top;i>=0;i--)
{
if(s[i]==v)
{
printf("Cycle Detected topological sort not possible \n");
exit(1);
}
}
}
if(a[u][v]==1 && vis[v]==0)
{
vis[v]=1;
s[++top]=v;
u=v;
v=0;
}
}
j++;
pop[j]=u;
top--;
u=s[top];
}
}
void topo(int n,int a[][MAX])
{
int i,u;
for(i=1;i<=n;i++)
vis[i]=0;
for(u=1;u<=n;u++)
{
if(vis[u]==0)
dfs(u,n,a);
}
}