forked from ghostmkg/programming-language
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopological-sort.cpp
57 lines (45 loc) · 1.22 KB
/
Topological-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
54
55
56
57
#include <bits/stdc++.h>
using namespace std;
void dfsSort(int v, vector<vector<int>>& adj,
vector<bool>& visited,
stack<int>& Stack)
{
// Mark the current node as visited
visited[v] = true;
// Recur for all adjacent vertices
for (int i : adj[v]) {
if (!visited[i])
dfsSort(i, adj, visited, Stack);
}
// Push current vertex to stack which stores the result
Stack.push(v);
}
// Function to perform Topological Sort
void topoSort(vector<vector<int>>& adj, int V)
{
stack<int> Stack;
vector<bool> visited(V, false);
for (int i = 0; i < V; i++) {
if (!visited[i])
dfsSort(i, adj, visited, Stack);
}
while (!Stack.empty()) {
cout << Stack.top() << " ";
Stack.pop();
}
}
int main()
{
// Number of nodes
int V = 4;
// Edges
vector<vector<int>> edges = { { 0, 1 }, { 1, 2 }, { 3, 1 }, { 3, 2 } };
// Graph represented as an adjacency list
vector<vector<int>> adj(V);
for (auto i : edges) {
adj[i[0]].push_back(i[1]);
}
cout << "Topological sorting of the graph: ";
topoSort(adj, V);
return 0;
}