-
Notifications
You must be signed in to change notification settings - Fork 287
/
Copy pathtopologicalSorting.c
69 lines (55 loc) · 1.63 KB
/
topologicalSorting.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
//Input : Adjacency Matrix
//Output: Order in which vertices are popped out in topological sorting
#include<stdio.h>
#include<stdlib.h>
//Topological sorting done by source removal algorithm
void topologicalSorting(int arr[10][10],int n){
//in[] is the input array that keep the count of sources for each node, out[] is the output array, s[] is the stack
int in[n],out[n],s[n],top=-1;
int i,j,k=0;
//putting the number of source nodes in the input array
for(i=0;i<n;i++){
in[i]=0;
for(j=0;j<n;j++){
if(arr[j][i]==1)
in[i]++;
}
}
while(1){
//if there is no source for the vertex then adding it to the stack and initialing the value of vertex in input array as -1
for(i=0;i<n;i++)
if(in[i]==0){
s[++top]=i;
in[i]=-1;
}
//if there are no vertex which are having 0 source then exit from the loop
//or if the stack is empty
if(top==-1)
break;
//popping the top element of the stack or vertex which has no source vertex
out[k]=s[top--];
for(i=0;i<n;i++){
//if the popped vertex has any connection in the graph then decrease the source count form the input array because we are removing the vertex from the graph
if(arr[out[k]][i]==1){
in[i]--;
}
}
//incrementing the value for the output array.
k++;
}
printf("Topological sorting order is \n");
for(i=0;i<n;i++)
printf("%d ",out[i]+1);
}
int main(){
int n;
printf("Enter the number of vertices in graph: ");
scanf("%d",&n);
int arr[10][10];
printf("Enter the adjacency matrix for the graph\n");
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&arr[i][j]);
topologicalSorting(arr,n);
return 0;
}