-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKahnsTopologicalSort.cpp
More file actions
86 lines (73 loc) · 1.67 KB
/
KahnsTopologicalSort.cpp
File metadata and controls
86 lines (73 loc) · 1.67 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <vector>
#include <queue>
// QUESTION: https://www.techiedelight.com/kahn-topological-sort-algorithm/
// Explanation: https://medium.com/insider-inc-engineering/the-wrath-of-kahns-algorithm-68081cf2fc55
/**
* This is a BFS approach to getting a topological sort. We try to get vertices with
* indegree 0. Then we remove it from the graph and push it into our output list. This
* requires updating the indegree of the nodes in the adjacency list. If we find a node
* with 0 indegree, we push it to the queue
*/
struct Edge
{
int src;
int dest;
};
class DirectedGraph
{
private:
std::vector<std::vector<int>> adj;
std::vector<int> indegree;
int nodes;
public:
DirectedGraph(int n, const std::vector<Edge>& edges)
: nodes(n)
{
adj.resize(nodes);
indegree.resize(nodes);
for (const auto & iter : edges)
{
adj[iter.src].push_back(iter.dest);
// An edge from src to dest means, indegree of
// dest increases
indegree[iter.dest]++;
}
}
void doKahn()
{
std::cout << "Kahn's topological sorting" << std::endl;
std::queue<int> queue;
std::vector<int> topo;
for (int i = 0; i < nodes; ++i)
{
if (indegree[i] == 0)
queue.push(i);
}
while (!queue.empty())
{
int top = queue.front();
queue.pop();
topo.push_back(top);
for (int j : adj[top])
{
indegree[j]--;
if (indegree[j] == 0)
queue.push(j);
}
}
for (auto iter : topo)
{
std::cout << iter << " ";
}
std::cout << std::endl;
}
};
int main()
{
std::vector<Edge> edges{ {0, 6}, {1, 2}, {1, 4}, {1, 6}, {3, 0}, {3, 4}, {5, 1}, {7, 0}, {7, 1} };
int num = 8;
DirectedGraph obj(num, edges);
obj.doKahn();
return 0;
}