-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCycleInUndirectedGraph.java
48 lines (43 loc) · 1.55 KB
/
CycleInUndirectedGraph.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
package main.java.topcodingquestion.treesandgraphs;
import main.java.datastructure.GNode;
import main.java.datastructure.Graph;
import java.util.HashSet;
import java.util.Set;
public class CycleInUndirectedGraph {
public static void main(String[] args) {
Graph graph = Graph.getUndirectedGraph(true);
CycleInUndirectedGraph cycleInUndirectedGraph = new CycleInUndirectedGraph();
System.out.println("Has Cycle:" + cycleInUndirectedGraph.hasCycleDFS(graph));
graph = Graph.getUndirectedGraph(false);
System.out.println("Has Cycle:" + cycleInUndirectedGraph.hasCycleDFS(graph));
}
public boolean hasCycleDFS(Graph graph) {
Set<GNode> visited = new HashSet<>();
for (GNode vertex : graph.getNodes()) {
if (visited.contains(vertex)) {
continue;
}
boolean flag = hasCycleDFSUtil(graph, vertex, visited, new GNode("-1", -1));
if (flag) {
return true;
}
}
return false;
}
public boolean hasCycleDFSUtil(Graph graph, GNode vertex, Set<GNode> visited, GNode parent) {
visited.add(vertex);
for (GNode adj : graph.getAdjacentNodes(vertex)) {
if (adj.equals(parent)) {
continue;
}
if (visited.contains(adj)) {
return true;
}
boolean hasCycle = hasCycleDFSUtil(graph, adj, visited, vertex);
if (hasCycle) {
return true;
}
}
return false;
}
}