-
Notifications
You must be signed in to change notification settings - Fork 81
/
topological-sorting-kahn-algo.cpp
74 lines (63 loc) · 1.39 KB
/
topological-sorting-kahn-algo.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <bits/stdc++.h>
using namespace std;
template <typename T>
class Graph {
unordered_map<T, list<T> > adjList;
public:
void addEdge(T u, T v, bool bidir = true ) {
adjList[u].push_back(v);
if( bidir ) adjList[v].push_back(u);
}
void topologicalSort() {
queue<T> q;
unordered_map<T, int> indegree;
unordered_map<T, bool> visited;
// initialize visited
for( auto vertex: adjList ) {
T node = vertex.first;
visited[node] = false;
indegree[node] = 0;
}
// initialize indegree
for( auto vertex: adjList ) {
T u = vertex.first;
for( T v: adjList[u] ) {
indegree[v]++;
}
}
// initialize queue, enqueue all the vertex having indegree 0
for( auto vertex: adjList ) {
T node = vertex.first;
if( indegree[node] == 0 ) q.push( node );
}
// kahn's algorithm
while( !q.empty() ) {
T node = q.front();
q.pop();
cout << node << "->";
for( T neighbour: adjList[node] ) {
indegree[neighbour]--;
if( indegree[neighbour] == 0 ) {
q.push(neighbour);
}
}
}
}
};
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
// directed acyclic graph ( DAG )
Graph<int> g;
g.addEdge(0, 2, false);
g.addEdge(1, 2, false);
g.addEdge(1, 4, false);
g.addEdge(2, 3, false);
g.addEdge(2, 4, false);
g.addEdge(3, 5, false);
g.addEdge(4, 5, false);
g.topologicalSort();
return 0;
}