-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathTopologicalSort_DFS.java
49 lines (39 loc) · 1.69 KB
/
TopologicalSort_DFS.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
//Reference - https://www.geeksforgeeks.org/topological-sorting/
/*Algorithms -
1. Maintain a visited set and a stack to record the order of vertices
2. Starting from a vertex loop through all the vertices
2.1 check if the vertex is present in the visited set
(i) vertex in visited set ==> continue the loop
(ii) vertex not in visited set ==> explore children of the current vertex recursivley ==> in a Depth First manner
Exploring a vertex implies visiting it's children. When there are no children to explore push the vertex to the stack
*/
class topologicalSort{
int vertices;
ArrayList<Integer> adjList[]; //adjacency list
topologicalSort(int n){
vertices = n;
adjList = new ArrayList[n];
for(int i=0;i<n;i++) adjList[i] = new ArrayList<Integer>();
}
void topo(){
boolean viisted[] = new boolean[vertices]; //keep track of visited vertices
Arrays.fill(visited,false);
Stack<Integer> stk = new Stack<Integer>(); //keep track of the order
for(int i=0;i<vertices;i++){
if(!visited[i]) topoUtil(i,visited,stk);
}
//print out the topological sort
while(!stk.isEmpty()){
System.out.println(stk.pop());
}
}
void topoUtil(int v,boolean visited[],Stack<Integer> stk){
visited[v] = true;
ArrayList<Integer> neighbours = adjList[v];
for(int i=0 ; i<neighbours.size() ; i++){ //exploring neighbours
int neighbour = neighbours.get(i);
if(!visited[neighbour]) topoUtil(neighbour,visited,stk); //exploring neighbours in Depth First Manner
}
stk.push(v);
}
}