-
Notifications
You must be signed in to change notification settings - Fork 0
/
TopologicalSort(DFS).java
56 lines (52 loc) · 1.71 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
50
51
52
53
54
55
56
import java.util.*;
import utils.*;
//comment added by mayank.
class main{
public static void main(String args[]){
Graph g = new Graph(6);
ArrayList<Edge> graph[] = g.graph;
g.addEdge(5,0,0,false);
g.addEdge(5,2,0,false);
g.addEdge(4,0,0,false);
g.addEdge(4,1,0,false);
g.addEdge(2,3,0,false);
g.addEdge(3,1,0,false);
g.addEdge(1,2,0,false);
// g.display();
System.out.println(topologicalSort(graph));
}
static LinkedList<Integer> topologicalSort(ArrayList<Edge> graph[]){
int noOfVertex = graph.length;
boolean isVisited[] = new boolean[noOfVertex];
Stack<Integer> stack = new Stack<>();
for(int i=0;i<noOfVertex;i++){
boolean taken[] = new boolean[noOfVertex];
if(!isVisited[i]){
if(DFS(i,graph,isVisited,stack,taken)){
System.out.println("Cycle detected!");
return new LinkedList<>();
}
}
}
LinkedList<Integer> res = new LinkedList<>();
while(stack.size()>0){
res.addLast(stack.pop());
}
return res;
}
static boolean DFS(int src ,ArrayList<Edge> graph[], boolean isVisited[] , Stack<Integer> stack,boolean []taken){
isVisited[src]=true;
taken[src]=true;
for(Edge e : graph[src]){
int neighbourVertex = e.vertex;
if(taken[neighbourVertex]){
return true;
}
if(!isVisited[neighbourVertex]){
if(DFS(neighbourVertex,graph,isVisited,stack,taken)) return true;
}
}
stack.push(src);
return false;
}
}