-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopological-Sorting--DFS.cpp
More file actions
64 lines (54 loc) · 1.24 KB
/
Copy pathTopological-Sorting--DFS.cpp
File metadata and controls
64 lines (54 loc) · 1.24 KB
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
#include <bits/stdc++.h>
using namespace std;
class Graph
{
int V;
list<int> *graph;
public:
Graph(int V)
{
this->V = V;
graph = new list<int>[V];
}
void addEdge(int v, int w)
{
graph[v].push_back(w);
}
void topologicalSortDFS(int source, vector<int> &visited, stack<int> &answer)
{
visited[source] = 1;
for (auto i : graph[source])
{
if (!visited[i])
topologicalSortDFS(i, visited, answer);
}
// During backtracking, pushing the current node in the stack to print it later.
answer.push(source);
}
};
int main()
{
int v = 6;
Graph g(v);
g.addEdge(5, 2);
g.addEdge(5, 0);
g.addEdge(4, 0);
g.addEdge(4, 1);
g.addEdge(2, 3);
g.addEdge(3, 1);
vector<int> visited(v, 0);
// For printing the topological sorting, using a stack.
stack<int> answer;
for (int source = 0; source < v; source++)
{
if (!visited[source])
g.topologicalSortDFS(source, visited, answer);
}
cout << "Following is a Topological Sort of the given graph : ";
while(!answer.empty())
{
cout<<answer.top()<<" ";
answer.pop();
}
return 0;
}