forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kahn's_Algorithm.cpp
75 lines (58 loc) · 1.42 KB
/
Kahn's_Algorithm.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
75
#include <bits/stdc++.h>
using namespace std;
void topoSortBFS(unordered_map<int, list<int>> &adj, int n, vector<int> &indegree, vector<int> &ans){
//make queue
queue<int> q;
//push into queue which have 0 indegree
for(int i=1; i<n; i++){
if(indegree[i] == 0){
q.push(i);
}
}
//processing start
while(!q.empty()){
int front = q.front();
q.pop();
ans.push_back(front);
for(auto i : adj[front]){
indegree[i]--;
if(indegree[i] == 0){
q.push(i);
}
}
}
}
int main()
{
//given graph
vector<pair<int, int>> edges;
edges.push_back({1, 2});
edges.push_back({1, 3});
edges.push_back({2, 5});
edges.push_back({3, 5});
edges.push_back({5, 4});
int n = 5;
//make adjacency list
unordered_map<int, list<int>> adj;
for(int i=0; i<edges.size(); i++){
int u = edges[i].first;
int v = edges[i].second;
adj[u].push_back(v);
}
//take indgree of all nodes
vector<int> indegree(n+1, 0);
for(auto i : adj){
for(auto j : i.second){
indegree[j]++;
}
}
//data structure to store the values
vector<int> ans;
//calling the Breadth First Search function
topoSortBFS(adj, n, indegree, ans);
//printing the ans
for(auto i : ans){
cout << i << " ";
}
return 0;
}