-
Notifications
You must be signed in to change notification settings - Fork 0
/
DetectCycleInUndirectedGraph.java
66 lines (58 loc) · 2.03 KB
/
DetectCycleInUndirectedGraph.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
61
62
63
64
65
66
import java.util.ArrayList;
import java.util.List;
// Detect Cycle in an Undirected Graph
public class DetectCycleInUndirectedGraph {
int v;
List<Integer>[] adjList;
public DetectCycleInUndirectedGraph(int v) {
this.v = v;
adjList = new ArrayList[v];
for (int i = 0; i < v; i++) {
adjList[i] = new ArrayList<>();
}
}
public void populateGraph(int[][] edges) {
for (int[] edge : edges) {
adjList[edge[0]].add(edge[1]);
adjList[edge[1]].add(edge[0]);
}
}
public boolean detectCycle() {
boolean[] visited = new boolean[v];
for (int i = 0; i < v; i++) {
if (!visited[i]) {
if (detectCycleUtil(visited, i, -1)) {
return true;
}
}
}
return false;
}
private boolean detectCycleUtil(boolean[] visited, int source, int parent) {
visited[source] = true;
for (int neighbor : adjList[source]) {
if (!visited[neighbor]) {
if (detectCycleUtil(visited, neighbor, source)) {
return true;
}
} else if (neighbor != parent) {
return true;
}
}
return false;
}
public static void main(String[] args) {
DetectCycleInUndirectedGraph graph1 = new DetectCycleInUndirectedGraph(5);
int[][] edges = {{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 0}};
graph1.populateGraph(edges);
System.out.println(graph1.detectCycle()); // true
DetectCycleInUndirectedGraph graph2 = new DetectCycleInUndirectedGraph(3);
int[][] edges2 = {{0, 1}, {1, 2}};
graph2.populateGraph(edges2);
System.out.println(graph2.detectCycle()); // false
DetectCycleInUndirectedGraph graph3 = new DetectCycleInUndirectedGraph(5);
int[][] edges3 = {{1, 0}, {0, 2}, {2, 1}, {0, 3}, {3, 4}};
graph3.populateGraph(edges3);
System.out.println(graph3.detectCycle()); // true
}
}