-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkahn_algorithm_topoSort.cpp
59 lines (52 loc) · 1.11 KB
/
kahn_algorithm_topoSort.cpp
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
#include <bits/stdc++.h>
using namespace std;
vector<int> kahns_algorithm_TopoSort(vector<int> adj[], int n){
//consrtuct an array inDegree to store indegree of every node
vector<int> inDegree(n, 0);
for(int i = 0; i < n; i++){
for(auto u: adj[i]){
inDegree[u]++;
}
}
vector<int> res;
queue<int> q;
//store all nodes with indegree = 0 in queue
for(int i = 0; i < n; i++){
if(inDegree[i] == 0){
q.push(i);
}
}
while(!q.empty()){
//extract front of the queue and add it to topological order
int i = q.front();
q.pop();
res.push_back(i);
//iterate through all adjacent nodes of dequeued node i
//and decrease their indegree by 1
for(auto u: adj[i]){
inDegree[u]--;
//if in-degree(u) becomes 0 after decrement push it into
//the queue
if(inDegree[u] == 0){
q.push(u);
}
}
}
return res;
}
int main(){
int n, m;
cin >> n >> m;
vector<int> adj[n];
for(int i = 0; i < m; i++){
int f, s;
cin >> f >> s;
adj[f].push_back(s);
}
vector<int> res = kahns_algorithm_TopoSort(adj, n);
for(int i = 0; i < n; i++){
cout << res[i] << " ";
}
cout << endl;
return 0;
}