-
Notifications
You must be signed in to change notification settings - Fork 2
/
HamiltonLoop3.java
65 lines (57 loc) · 1.38 KB
/
HamiltonLoop3.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
package com.github.datastructureandalgorithm.graph.chapter9;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class HamiltonLoop3 {
private Graph G;
private boolean[] visited;
private int[] pre;
private int end;
public HamiltonLoop3(Graph G) {
this.G = G;
visited = new boolean[G.V()];
pre = new int[G.V()];
end = -1;
dfs(0, 0, G.V());
}
/**
* 对图进行深度优先遍历
*
* @param v
* @param parent
* @param left 表示还有多少个点没有遍历
* @return
*/
private boolean dfs(int v, int parent, int left) {
visited[v] = true;
pre[v] = parent;
left--;
if (left == 0 && G.hasEdge(0, v)) {
end = v;
return true;
}
for (int w : G.adj(v))
if (!visited[w]) {
if (dfs(w, v, left)) return true;
}
visited[v] = false;
return false;
}
/**
* 获取哈密尔顿回路
*
* @return
*/
public List<Integer> result() {
List<Integer> res = new ArrayList<>();
if (end == -1) return res;
int cur = end;
while (cur != 0) {
res.add(cur);
cur = pre[cur];
}
res.add(0);
Collections.reverse(res);
return res;
}
}