forked from MukulCode/CodingClubIndia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcycleDetectionDfs.java
More file actions
32 lines (25 loc) · 807 Bytes
/
cycleDetectionDfs.java
File metadata and controls
32 lines (25 loc) · 807 Bytes
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
package Graphs;
import java.util.*;
public class cycleDetectionDfs {
public boolean isCyclic(int V, ArrayList<ArrayList<Integer>> adj) {
boolean vis[] = new boolean[V + 1];
for (int i = 1; i <= V + 1; i++) {
if (vis[i] == false) {
if (isCycleDetected(i, -1, vis, adj))
return true;
}
}
return false;
}
public boolean isCycleDetected(int n, int parent, boolean vis[], ArrayList<ArrayList<Integer>> adj) {
vis[n] = true;
for (Integer it : adj.get(n)) {
if (vis[it] == false) {
if (isCycleDetected(it, n, vis, adj))
return true;
} else if (it != parent)
return true;
}
return false;
}
}