-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFPTBranchingSolver.java
133 lines (127 loc) · 3.33 KB
/
FPTBranchingSolver.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import tc.wata.data.*;
import tc.wata.debug.*;
import tc.wata.util.*;
public class FPTBranchingSolver extends Solver {
public static long counter = 0;
@Override
public void solve(Graph g) {
ReductionRoot.reduce(g);
if (ub <= LowerBound.lowerBound(g)) return;
if (g.m() == 0) {
update(g.getS());
return;
}
Graph[] gs = g.decompose(g.n() < g.n * 0.5);
if (gs != null) {
if (ReductionRoot.DEBUG) {
int[] size = new int[gs.length];
for (int i = 0; i < gs.length; i++) size[i] = gs[i].n;
System.err.printf("decompose: %s%n", Utils.toString(size, " + "));
}
IntArray tmp = new IntArray();
for (int i = 0; i < g.n; i++) if (g.used[i] == 'S') tmp.add(i);
for (int i = 0; i < gs.length; i++) {
Graph h = gs[i];
FPTBranchingSolver solver = new FPTBranchingSolver();
solver.ub = ub - tmp.length;
if (outputUB && i == gs.length - 1) {
solver.outputUB = true;
solver.add = add + tmp.length;
}
solver.solve(h);
if (solver.res == null) return;
for (int j : solver.res) tmp.add(h.id[j]);
}
ub = tmp.length;
res = tmp.toArray();
return;
}
ReductionRoot.DEBUG = false;
int s = -1;
for (int i = 0; i < g.n; i++) if (g.adj[i].length > 0) {
if (s < 0 || g.adj[s].length < g.adj[i].length) s = i;
}
count();
Graph g1 = new Graph(g), g2 = g;
// Debug.print("S", s);
g1.setS(s);
solve(g1);
// Debug.print("F", s);
g2.setF(s);
solve(g2, s);
}
void solve(Graph g, int s) {
Reduction.reduce(g, ub);
if (ub <= LowerBound.lowerBound(g)) return;
if (g.m() == 0) {
update(g.getS());
return;
}
if (g.adj[s].length == 0) {
solve(g);
return;
}
Graph[] gs = g.decompose(false);
if (gs != null) {
IntArray tmp = new IntArray();
for (int i = 0; i < g.n; i++) if (g.used[i] == 'S') tmp.add(i);
for (int i = 0; i < gs.length; i++) {
Graph h = gs[i];
FPTBranchingSolver solver = new FPTBranchingSolver();
solver.ub = ub - tmp.length;
if (outputUB && i == gs.length - 1) {
solver.outputUB = true;
solver.add = add + tmp.length;
}
int s2 = -1;
for (int j = 0; j < h.n; j++) if (h.adj[j].length > 0 && h.used[j] == 'F') {
s2 = j;
}
if (s2 >= 0) solver.solve(h, s2);
else solver.solve(h);
if (solver.res == null) return;
for (int j : solver.res) tmp.add(h.id[j]);
}
ub = tmp.length;
res = tmp.toArray();
return;
}
if (ReductionRoot.LEVEL >= 2) {
double[] x = new HalfIntegralRelax().solve(g, s);
if (ub <= x[g.n] + 0.5) return;
if (x[g.n] * 2 != g.adj[s].length) {
int[] que = new int[g.n];
int qs = 0, qt = 0;
boolean[] used = new boolean[g.n];
que[qt++] = s;
used[s] = true;
while (qs < qt) {
int v = que[qs++];
for (int u : g.adj[v]) if (x[u] == 0 && !used[u]) {
que[qt++] = u;
used[u] = true;
}
}
for (int i = 1; i < qt; i++) g.contract(que[i], s);
for (int i = 0; i < g.n; i++) if (x[i] == 1 && g.used[i] == 0) g.setS(i);
solve(g, s);
return;
}
}
int v = -1;
for (int u : g.adj[s]) {
if (v < 0 || g.adj[v].length < g.adj[u].length) v = u;
}
count();
Graph g1 = new Graph(g), g2 = g;
// Debug.print("+S", v);
g1.setS(v);
solve(g1, s);
// Debug.print("+F", v);
g2.contract(v, s);
solve(g2, s);
}
void count() {
if (Long.bitCount(++counter) == 1) Debug.print("#branch", counter);
}
}