forked from bamsarts/DS-ALGO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopologicalSort.cpp
50 lines (43 loc) · 950 Bytes
/
TopologicalSort.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
#include<bits/stdc++.h>
using namespace std;
void addEdge(vector<vector<int>> &adj,int u,int v)
{
adj[u].push_back(v);
}
void util(int v,vector<vector<int>> &adj, vector<bool> &visited,stack<int> &s)
{
visited[v] = true;
for(auto i = adj[v].begin();i!=adj[v].end();i++)
if(!visited[*i])
util(*i,adj,visited,s);
s.push(v);
}
void topologicalSort(vector<vector<int>> &adj,int nodes)
{
vector<bool> visited(nodes,false);
stack<int> s;
for(int i=0;i<nodes;i++)
{
if(visited[i] == false)
{
util(i,adj,visited,s);
}
}
while(s.empty()==false)
{
cout<<s.top()<<" ";
s.pop();
}
}
int main()
{
vector<vector<int>> adj(6);
addEdge(adj,5, 2);
addEdge(adj,5, 0);
addEdge(adj,4, 0);
addEdge(adj,4, 1);
addEdge(adj,2, 3);
addEdge(adj,3, 1);
topologicalSort(adj,6);
return 0;
}