-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathDetectCycleDirected.java
60 lines (57 loc) · 1.66 KB
/
DetectCycleDirected.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
package SummerTrainingGFG.Graph;
import java.util.ArrayList;
/**
* @author Vishal Singh
* 07-02-2021
*/
public class DetectCycleDirected {
public static void addEdge(ArrayList<ArrayList<Integer>> graph,int s,int d ){
graph.get(s).add(d);
}
public static void detectCycle(ArrayList<ArrayList<Integer>> graph,int v){
boolean[] vis = new boolean[v];
boolean[] recStack = new boolean[v];
boolean isCycle = false;
for (int i = 0; i < v; i++) {
if (!vis[i]){
if (dfs(graph,vis,recStack,i)){
System.out.println("Cycle");
isCycle = true;
}
}
}
if (!isCycle){
System.out.println("No cycle");
}
}
public static boolean dfs(ArrayList<ArrayList<Integer>> graph,boolean[] vis,boolean[] recStack,int s){
vis[s] = true;
recStack[s] = true;
for (int d: graph.get(s)){
if (!vis[d]){
if (dfs(graph,vis,recStack,d)){
return true;
}
}else if (recStack[d]){
return true;
}
}
recStack[s] = false;
return false;
}
public static void main(String[] args) {
ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
int v = 6;
for (int i = 0; i < v; i++) {
graph.add(new ArrayList<>());
}
addEdge(graph,0,1);
addEdge(graph,2,1);
addEdge(graph,2,3);
addEdge(graph,3,4);
addEdge(graph,4,5);
addEdge(graph,5,3);
System.out.println(graph);
detectCycle(graph,v);
}
}