-
Notifications
You must be signed in to change notification settings - Fork 0
/
Topo_Sort.cpp
53 lines (46 loc) · 1.01 KB
/
Topo_Sort.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
// Implementation of Topological Sorting Recursively
//
// Running Time Complexity : O(V+E)
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define int long long
#define pii pair<int, int>
const int N = 100005;
int V, E;
bool vis[N];
stack<int> nodes;
vector<int> adj[N];
void TopologicalSortRecursive(int u)
{
vis[u] = true;
for(int v: adj[u])
if(!vis[v])
TopologicalSortRecursive(v);
nodes.push(u);
}
int32_t main()
{
int u, v, i;
memset(vis, false, sizeof(vis));
cout << "Enter the no of Nodes";
cin >> V;
cout << "Enter the no of Edges";
cin >> E;
for(i = 0 ; i < E ; i++)
{
cin >> u >> v; // There's an edge u -> v
adj[u].push_back(v);
}
for(u = 1 ; u <= V ; u++) {
if(!vis[u])
TopologicalSortRecursive(u);
}
cout << "Topological Sorting: \n";
while(!nodes.empty()) {
cout << nodes.top() << " ";
nodes.pop();
}
cout << endl;
return 0;
}