-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopological_Sorting.cpp
More file actions
41 lines (33 loc) · 1021 Bytes
/
Copy pathTopological_Sorting.cpp
File metadata and controls
41 lines (33 loc) · 1021 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
#include <stdio.h>
int temp[10], k = 0;
void topologicalSort(int n, int indegree[], int adj[10][10]) {
for (int i = 1; i <= n; i++) {
if (indegree[i] == 0) {
indegree[i] = -1; // Mark as processed
temp[++k] = i;
for (int j = 1; j <= n; j++)
if (adj[i][j] == 1) indegree[j]--;
i = 0; // Restart to handle updates
}
}
}
int main() {
int n, indegree[10] = {0}, adj[10][10];
printf("Enter number of vertices: ");
scanf("%d", &n);
printf("Enter adjacency matrix:\n");
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
scanf("%d", &adj[i][j]);
if (adj[i][j] == 1) indegree[j]++;
}
topologicalSort(n, indegree, adj);
if (k != n)
printf("Topological ordering is not possible (Graph has a cycle).\n");
else {
printf("Topological Order: ");
for (int i = 1; i <= k; i++)
printf("v%d ", temp[i]);
}
return 0;
}