forked from lsgwr/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphBFSCycleDetect.java
77 lines (68 loc) · 2.13 KB
/
GraphBFSCycleDetect.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
67
68
69
70
71
72
73
74
75
76
77
package Chapter05BreadthFirstTraversal.Section6GraphBFSCycleDetect;
import Chapter02GraphExpress.Graph;
import java.util.*;
/**
* 广度优先遍历实现环检测
*/
public class GraphBFSCycleDetect {
private Graph graph;
/**
* 顶点的访问情况的数组
*/
private boolean[] visited;
/**
* 广度优先遍历的顺序结果(只有一种,不想DFS有前序、后序两种)
*/
private List<Integer> orderList = new ArrayList<>();
private int[] pre;
private boolean hasCycle = false;
public GraphBFSCycleDetect(Graph graph) {
this.graph = graph;
this.visited = new boolean[graph.V()];
pre = new int[graph.V()];
Arrays.fill(pre, -1);
// 从bfs(0)改成下面的代码,可以支持非连通的图,不用考虑连通分量的时候直接用bfs(v)即可
for (int v = 0; v < graph.V(); v++) {
if (!visited[v]) {
if (bfs(v)) {
hasCycle = true;
break;
}
}
}
}
/**
* 从source点开始进行广度优先遍历
*/
private boolean bfs(int source) {
// ArrayDeque既可以当队列又可以当栈来用,参考 https://github.com/19920625lsg/liuyubobobo-algorithms/tree/master/Part2Basic/src/main/java/Chapter03StackAndQueues/JavaBuiltIn
Queue<Integer> queue = new ArrayDeque<>();
queue.offer(source);
visited[source] = true;
pre[source] = source;
while (!queue.isEmpty()) {
int v = queue.remove();
orderList.add(v);
for (int w : graph.adj(v)) {
// 遍历v的所有顶点
if (!visited[w]) {
queue.add(w);
visited[w] = true;
pre[w] = v;
} else if (pre[v] != w) {
return true;
}
}
}
return false;
}
/**
* 图中是否有环
*/
public boolean hasCycle() {
return hasCycle;
}
public Iterable<Integer> getOrderList() {
return orderList;
}
}