-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy path13_implement_topological_sort.cpp
115 lines (87 loc) · 2.91 KB
/
13_implement_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
what is topological sorting ?
(DFS) https://www.geeksforgeeks.org/topological-sorting/
(BFS) https://www.geeksforgeeks.org/topological-sorting-indegree-based-solution/
topological sort is not for cyclic graph (as it won't be possible)
so it is always for **directed** **acyclic** graph
hence ""DAG""
Topological sorting for Directed Acyclic Graph (DAG) is a linear
ordering of vertices such that for every directed edge u v,
vertex u comes before v in the ordering. Topological Sorting for a
graph is not possible if the graph is not a DAG.
link: https://practice.geeksforgeeks.org/problems/topological-sort/1
video (DFS): https://youtu.be/Yh6EFazXipA?list=PLgUwDviBIf0rGEWe64KWas0Nryn7SCRWw
video (BFS): https://youtu.be/rZv_jHZva34?list=PLgUwDviBIf0rGEWe64KWas0Nryn7SCRWw
*/
// ----------------------------------------------------------------------------------------------------------------------- //
/*
DFS
TC: O(V + E)
SC: O(V) + O(V) => 1st -> visited array and 2nd -> stack
ASC (auxiliary): O(V)
here stack will ensure that all the directed nodes are visited and nothing's
left so now push element into the stack
hence stack will have ele. in topological order
*/
void dfs(int curr, stack<int>& s, vector<int>& vis, vector<int> adj[]) {
vis[curr] = 1;
for (auto i : adj[curr]) {
if (!vis[i]) {
dfs(i, s, vis, adj);
}
}
s.push(curr);
}
vector<int> topoSort(int V, vector<int> adj[])
{
vector<int> vis(V, 0);
stack<int> s;
for (int i = 0;i < V;i++) {
if (!vis[i]) {
dfs(i, s, vis, adj);
}
}
vector<int> sorted;
while (!s.empty()) {
sorted.push_back(s.top());
s.pop();
}
return sorted;
}
// ----------------------------------------------------------------------------------------------------------------------- //
/*
using BFS (kahn's algorithm)
here we will require inDeg vector as we won't have recursive calls
so to check every time which vertex has how many indegree
TC: O(V + E)
SC: O(V) + O(V) -> 1st for inDeg, 2nd for queue
*/
vector<int> topoSort(int V, vector<int> adj[])
{
vector<int> inDeg(V, 0);
// store indegrees of all
for (int i = 0; i < V; i++) {
for (auto k : adj[i]) {
inDeg[k]++;
}
}
queue<int> q;
// get all the node with indegree=0,
// if there is no node with indegree=0, means it has cycle
// if there is cycle then topological sort doesn't exist
for (int i = 0;i < V;i++) {
if (inDeg[i] == 0) q.push(i);
}
vector<int> topoSorted;
while (!q.empty()) {
int curr = q.front();
q.pop();
topoSorted.push_back(curr);
for (auto i : adj[curr]) {
inDeg[i]--;
// if i ele. became
if (inDeg[i] == 0) q.push(i);
}
}
return topoSorted;
}