-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06.topological_Sort.java
52 lines (46 loc) · 1.55 KB
/
06.topological_Sort.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
class Solution
{
//Function to return list containing vertices in Topological order.
static int[] topoSort(int V, ArrayList<ArrayList<Integer>> adj)
{
// add your code here
Stack<Integer> stack = new Stack<>();
//visited array
boolean[] visited = new boolean[V];
Arrays.fill(visited,false);
// iterate to each node , even its a separate graph
for(int i =0;i<V;i++){
if(!visited[i] ){
topologicalSort(i,visited,adj,stack);
}
}
int[] ans = new int[V];
int ind = -1;
while (!stack.isEmpty())
{
//pushing elements of stack in list and popping them from stack.
ans[++ind] = stack.peek();
stack.pop();
}
return ans;
}
static void topologicalSort(int u,boolean[] visited,ArrayList<ArrayList<Integer>> adj,Stack<Integer> stack){
visited[u] = true;
// Integer i;
// Iterator<Integer> it = adj.get(u).iterator();
// while(it.hasNext()){
// i = it.next();
// if(!visited[i]){
// topologicalSort(i,visited,adj,stack);
// }
// }
// stack.push(new Integer(u);
for(int i =0;i<adj.get(u).size();i++){
//if any vertex is not visited call function recursivly
if(!visited[adj.get(u).get(i)]){
topologicalSort(adj.get(u).get(i),visited,adj,stack);
}
}
stack.push(u);
}
}