|
2 | 2 | Given a Directed Graph. Find any Topological Sorting of that Graph.
|
3 | 3 |
|
4 | 4 | Input:
|
5 |
| -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. |
| 5 | +The first line of input takes the number of test cases then T test cases follow . |
| 6 | +Each test case contains two lines. The first line of each test case contains two integers E and V |
| 7 | +representing no of edges and the number of vertices. Then in the next line are E pairs of integers u, v |
| 8 | +representing an edge from u to v in the graph. |
6 | 9 |
|
7 | 10 | Output:
|
8 | 11 | For each test case output will be 1 if the topological sort is done correctly else it will be 0.
|
9 | 12 |
|
10 | 13 | Your Task:
|
11 |
| -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. |
| 14 | +You don't need to read input or print anything. Your task is to complete the function topoSort() |
| 15 | +which takes the adjacency list of the Graph and the number of vertices (N) as inputs are returns |
| 16 | +an array consisting of a the vertices in Topological order. As there are multiple Topological |
| 17 | +orders possible, you may return any of them. |
12 | 18 |
|
13 | 19 | Expected Time Complexity: O(V + E).
|
14 | 20 | Expected Auxiliary Space: O(V).
|
@@ -91,34 +97,51 @@ int main() {
|
91 | 97 | }// } Driver Code Ends
|
92 | 98 |
|
93 | 99 |
|
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
94 | 115 | // The Graph structure is as folows
|
95 | 116 |
|
96 | 117 | /* Function which sorts the graph vertices in topological form
|
97 | 118 | * N: number of vertices
|
98 | 119 | * adj[]: input graph
|
99 | 120 | */
|
100 | 121 |
|
101 |
| -stack<int> s; |
| 122 | +stack <int> s; |
102 | 123 |
|
103 | 124 | void dfs(vector<int> adj[],int node, vector<bool> &vis){
|
104 |
| - vis[node]=true; |
105 |
| - for(auto x: adj[node]){ |
| 125 | + vis[node] = true; |
| 126 | + for (auto x: adj[node]){ |
106 | 127 | if(!vis[x])
|
107 | 128 | dfs(adj, x, vis);
|
108 | 129 | }
|
109 | 130 | s.push(node);
|
110 | 131 | }
|
111 | 132 |
|
112 |
| -vector<int> topoSort(int V, vector<int> adj[]) { |
113 |
| - vector<bool> vis(V, false); |
114 |
| - for(int i=0;i<V;i++){ |
115 |
| - if(!vis[i]) |
| 133 | +vector <int> topoSort(int V, vector<int> adj[]) { |
| 134 | + vector <bool> vis(V, false); |
| 135 | + for (int i = 0; i < V; i++){ |
| 136 | + if (!vis[i]) |
116 | 137 | dfs(adj, i, vis);
|
117 | 138 | }
|
118 |
| - vector<int>res; |
119 |
| - while(!s.empty()){ |
| 139 | + vector <int> res; |
| 140 | + |
| 141 | + while (!s.empty()){ |
120 | 142 | res.push_back(s.top());
|
121 | 143 | s.pop();
|
122 | 144 | }
|
| 145 | + |
123 | 146 | return res;
|
124 | 147 | }
|
0 commit comments