-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp134.java
72 lines (58 loc) · 1.94 KB
/
p134.java
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
/*Given a Directed Acyclic Graph (DAG) with V vertices and E edges, Find any Topological Sorting of that Graph.
Example 1:
Input:
Input: V = 4 , adj = [[] , [0], [0], [0]]
Output:
1
Explanation:
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.
One possible Topological order for the
graph is 3, 2, 1, 0.
Example 2:
Input:
Input: V = 6 , adj = [[] , [3], [3], [], [0,1], [0,2]]
Output:
1
Explanation:
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.
One possible Topological order for the
graph is 5, 4, 2, 1, 3, 0.
Your Task:
You don't need to read input or print anything. Your task is to complete the function topoSort() which takes the integer V denoting the number of vertices and adjacency list as input parameters and returns an array consisting of a the vertices in Topological order. As there are multiple Topological orders possible, you may return any of them. If your returned topo sort is correct then console output will be 1 else 0.
Expected Time Complexity: O(V + E).
Expected Auxiliary Space: O(V).
Constraints:
2 ≤ V ≤ 104
1 ≤ E ≤ (N*(N-1))/2*/
class Solution{
static int[] topoSort(int V, ArrayList<ArrayList<Integer>> adj) {
int arr[]=new int[V];
boolean vis[]=new boolean[V];
Stack<Integer> st=new Stack<>();
for(int i=0;i<V;i++){
if(vis[i]==false){
dfs(adj,vis,i,st);
}
}
int i=0;
while(!st.isEmpty()){
arr[i++]=st.pop();
}
return arr;
}
public static void dfs(ArrayList<ArrayList<Integer>> adj,boolean vis[],int curr,Stack<Integer> st){
vis[curr]=true;
for(int ele:adj.get(curr)){
if(vis[ele]==false){
dfs(adj,vis,ele,st);
}
}
st.push(curr);
}
}