-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay29.java
More file actions
50 lines (41 loc) · 1.46 KB
/
Day29.java
File metadata and controls
50 lines (41 loc) · 1.46 KB
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
import java.util.*;
public class Day29 {
public static boolean hasCycle(int V, List<List<Integer>> adj) {
boolean[] visited = new boolean[V];
boolean[] recursionStack = new boolean[V];
for (int i = 0; i < V; i++) {
if (!visited[i] && isCyclicUtil(i, visited, recursionStack, adj)) {
return true;
}
}
return false;
}
private static boolean isCyclicUtil(int v, boolean[] visited, boolean[] recursionStack, List<List<Integer>> adj) {
visited[v] = true;
recursionStack[v] = true;
for (int neighbor : adj.get(v)) {
if (!visited[neighbor]) {
if (isCyclicUtil(neighbor, visited, recursionStack, adj)) {
return true;
}
} else if (recursionStack[neighbor]) {
return true; // Cycle detected
}
}
recursionStack[v] = false;
return false;
}
public static void main(String[] args) {
int V = 4; // Number of vertices
List<List<Integer>> adj = new ArrayList<>(V);
for (int i = 0; i < V; i++) {
adj.add(new ArrayList<>());
}
adj.get(0).add(1);
adj.get(1).add(2);
adj.get(2).add(0);
adj.get(2).add(3);
boolean hasCycle = hasCycle(V, adj);
System.out.println("Does the graph have a cycle? " + hasCycle);
}
}