forked from iharsh234/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Topological_Sort.cpp
71 lines (57 loc) · 1.32 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
class Graph {
int numVertex;
vector<int> *adj;
public:
// Constructor to initialise graph
Graph(int numVertex) {
this->numVertex = numVertex;
adj = new vector<int> [numVertex];
}
// Function to add edge between source and destination
void addEdge(int src, int dest) {
adj[src].push_back(dest);
}
void topologicalSort();
void topologicalSortUtil(int vertex, vector<bool> &visited, stack<int> &Stack);
};
void Graph::topologicalSortUtil(int vertex, vector<bool> &visited, stack<int> &Stack) {
visited[vertex] = true;
for(int i = 0; i < adj[vertex].size(); i++) {
if(!visited[adj[vertex][i]]) {
topologicalSortUtil(adj[vertex][i], visited, Stack);
}
}
Stack.push(vertex);
}
void Graph::topologicalSort() {
stack<int> Stack;
vector<bool> visited(numVertex, false);
for(int i = 0; i < numVertex; i++) {
if(!visited[i]) {
topologicalSortUtil(i, visited, Stack);
}
}
while(!Stack.empty()) {
cout << Stack.top() << " ";
Stack.pop();
}
}
int main() {
Graph graph(6);
graph.addEdge(5, 2);
graph.addEdge(5, 0);
graph.addEdge(4, 0);
graph.addEdge(4, 1);
graph.addEdge(2, 3);
graph.addEdge(3, 1);
cout << "Topological Sort: ";
graph.topologicalSort();
return 0;
}
/* Output
Topological Sort: 5 4 2 3 1 0
*/