forked from D3v3sh5ingh/Graph-Theory-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TOPOLOGICAL SORT
72 lines (64 loc) · 1.78 KB
/
TOPOLOGICAL SORT
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
/* TOPOLOGICAL SORT :
In case of multiple possible answer return the lexographically smaller order.
INPUT:
N - No. of Nodes
M - No of edges
next M line contains M pair with a edge from a to b
Eg:
6 5
5 1
1 2
2 4
3 2
3 4
Output :
3 5 1 2 4 6
In case of graphs where TOPOLOGICAL Sort is not possible return empty vector
*/
/* Using KAHN"S ALGORITHM */
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
vector<int> TopologicalSort(int n,int m,vector<pair<int,int>>&edges){
vector<int>final_order;
unordered_map<int,int>indegree(n+1); //DS to maintain indegree of nodes
vector<vector<int>>adj_list(n+1);
//Creat adjacency list
for(auto ed:edges){
adj_list[ed.first].push_back(ed.second);
indegree[ed.second]++;
}
// Use priority queue to get lexographically smallest node
priority_queue<int,vector<int>,greater<int>>pq;
//Push Nodes with indegree 0 to queue
for(int i=1;i<n+1;i++){
if(indegree[i]==0)
pq.push(i);
}
// Traverse each node from pq one by one and decrease the indegree of its child
while(!pq.empty()){
int curr_node=pq.top();
final_order.push_back(curr_node);
pq.pop();
for(auto child :adj_list[curr_node]){
indegree[child]--;
if(indegree[child]==0)
pq.push(child);
}
}
//If the size of final order is not equal to given no. of nodes thn the GRAPH is not a valid DAG
if(final_order.size()!=n)
return vector<int>();
return final_order;
}
int main() {
int no_of_nodes=6;
int no_of_edges=5;
vector<pair<int,int>>edges({{5,1},{1,2},{2,4},{3,2},{3,4}});
auto ans=TopologicalSort(no_of_nodes,no_of_edges,edges);
for(auto node:ans){
cout<<node<<" ";
}
cout<<"\n";
return 0;
}