-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10_topology_source.c
44 lines (39 loc) · 1005 Bytes
/
10_topology_source.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
//Topological sorting using source removal
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
void main() {
int graph[MAX][MAX], flag[MAX], indeg[MAX], n, i, j, k, count=0;
printf("\nEnter the number of nodes in the graph:\n");
scanf("%d", &n);
printf("\nEnter the adjacency matrix:\n");
for(i=0; i<n; i++){
for(j=0; j<n; j++){
scanf("%d", &graph[i][j]);
}
flag[i] = 0;
indeg[i] = 0;
}
for(i=0; i<n; i++) {
for(j=0; j<n; j++) {
indeg[i] += graph[j][i];
}
}
printf("\nThe topological oreder is:\n");
while(count < n) {
int pos;
for(k=0; k<n; k++) {
if(indeg[k] == 0 && flag[k] == 0){
printf(" --> %c", k+65);
flag[k] = 1;
pos = k;
break;
}
}
for(i=0; i<n; i++) {
if(graph[pos][i] == 1)
indeg[i]--;
}
count++;
}
}