-
Notifications
You must be signed in to change notification settings - Fork 2
/
topological_sorting_dfs.cpp
147 lines (101 loc) · 2.97 KB
/
topological_sorting_dfs.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
Given a Directed Graph. Find any Topological Sorting of that Graph.
Input:
The first line of input takes the number of test cases then T test cases follow .
Each test case contains two lines. The first line of each test case contains two integers E and V
representing no of edges and the number of vertices. Then in the next line are E pairs of integers u, v
representing an edge from u to v in the graph.
Output:
For each test case output will be 1 if the topological sort is done correctly else it will be 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function topoSort()
which takes the adjacency list of the Graph and the number of vertices (N) as inputs are returns
an array consisting of a the vertices in Topological order. As there are multiple Topological
orders possible, you may return any of them.
Expected Time Complexity: O(V + E).
Expected Auxiliary Space: O(V).
Constraints:
1 <= T <= 100
2 <= V <= 104
1 <= E <= (N*(N-1))/2
0 <= u, v <= N-1
Graph doesn't contain multiple edges, self loops and cycles.
Graph may be disconnected.
Example:
Input
2
6 6
5 0 5 2 2 3 4 0 4 1 1 3
3 4
3 0 1 0 2 0
Output:
1
1
Explanation:
Testcase 1: The output 1 denotes that the order is valid. So, if you have implemented your function correctly, then output would be 1 for all test cases.
*/
#include <bits/stdc++.h>
using namespace std;
vector <int> topoSort(int N, vector<int> adj[]);
/* Function to check if elements returned by user
* contains the elements in topological sorted form
* V: number of vertices
* *res: array containing elements in topological sorted form
* adj[]: graph input
*/
bool check(int V, vector <int> &res, vector<int> adj[]) {
vector<int> map(V, -1);
for (int i = 0; i < V; i++) {
map[res[i]] = i;
}
for (int i = 0; i < V; i++) {
for (int v : adj[i]) {
if (map[i] > map[v]) return false;
}
}
return true;
}
// Driver Code
int main() {
int T;
cin >> T;
while (T--) {
int N, E;
cin >> E >> N;
int u, v;
vector<int> adj[N];
for (int i = 0; i < E; i++) {
cin >> u >> v;
adj[u].push_back(v);
}
vector <int> res = topoSort(N, adj);
cout << check(N, res, adj) << endl;
}
}// } Driver Code Ends
// The Graph structure is as folows
/* Function which sorts the graph vertices in topological form
* N: number of vertices
* adj[]: input graph
*/
stack <int> s;
void dfs(vector<int> adj[],int node, vector<bool> &vis){
vis[node] = true;
for (auto x: adj[node]){
if(!vis[x])
dfs(adj, x, vis);
}
s.push(node);
}
vector <int> topoSort(int V, vector<int> adj[]) {
vector <bool> vis(V, false);
for (int i = 0; i < V; i++){
if (!vis[i])
dfs(adj, i, vis);
}
vector <int> res;
while (!s.empty()){
res.push_back(s.top());
s.pop();
}
return res;
}