-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.c
62 lines (55 loc) · 977 Bytes
/
10.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
#include<stdio.h>
#include<stdlib.h>
int n,a[10][10],q[10],r=-1,f=0,co=0,opc=0;
int indeg[10]={};
void calindeg()
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
opc++;
if(a[j][i])
indeg[i]++;
}
for(int i=1;i<=n;i++)
if(!indeg[i])
q[++r]=i;
}
void topoSort()
{
int* topo=calloc(n+1,sizeof(int));
while(r>=f)
{
int v=q[f++];
topo[co++]=v;
for(int j=1;j<=n;j++)
if(a[v][j])
{
a[v][j]=0;
indeg[j]--;
if(indeg[j]==0)
q[++r]=j;
}
}
if(co<n)
printf("The topological sorting is not possible");
else
{
printf("topological Sorting is : ");
for(int i=0;i<co;i++)
printf("%d ",topo[i]);
printf("\nThe operation count is %d",opc);
}
printf("\n");
}
void main()
{
printf("Enter the number of vertices : ");
scanf("%d",&n);
printf("Enter the adjacency matrix : \n");
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&a[i][j]);
calindeg();
topoSort();
}