-
Notifications
You must be signed in to change notification settings - Fork 104
/
HamiltonLoop.java
65 lines (49 loc) · 1.37 KB
/
HamiltonLoop.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
import java.util.ArrayList;
import java.util.Collections;
public class HamiltonLoop {
private Graph G;
private int[] pre;
private int end;
public HamiltonLoop(Graph G){
this.G = G;
pre = new int[G.V()];
end = -1;
int visited = 0;
dfs(visited, 0, 0, G.V());
}
private boolean dfs(int visited, int v, int parent, int left){
visited += (1 << v);
pre[v] = parent;
left --;
if(left == 0 && G.hasEdge(v, 0)){
end = v;
return true;
}
for(int w: G.adj(v))
if((visited & (1 << w)) == 0){
if(dfs(visited, w, v, left)) return true;
}
visited -= (1 << v);
return false;
}
public ArrayList<Integer> result(){
ArrayList<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;
}
public static void main(String[] args){
Graph g = new Graph("g.txt");
HamiltonLoop hl = new HamiltonLoop(g);
System.out.println(hl.result());
Graph g2 = new Graph("g2.txt");
HamiltonLoop hl2 = new HamiltonLoop(g2);
System.out.println(hl2.result());
}
}