-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtree.cpp
More file actions
354 lines (299 loc) · 8.8 KB
/
tree.cpp
File metadata and controls
354 lines (299 loc) · 8.8 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "tree.hpp"
#include <stack>
void TreeSolution::determine_initial_tree() {
long demand_sum = 0;
for (int i = 0; i < network->num_nodes; ++i) {
demand_sum += network->nodes[i]->demand;
}
if (demand_sum != 0) {
throw "Network is infeasible because demands do not sum up to zero.";
}
for (int i = 1; i < network->num_nodes; ++i) {
Node *node = network->nodes[i];
Arc *arc = NULL;
if (node->demand < 0) {
// this means that node has DEMAND!! eg we want to make an arc
// INTO this node
arc = network->add_artificial_arc(ROOT_NODE, node->id);
arc->flow = -node->demand;
basic_arcs[i] = arc;
basic_arc_dirs[i] = ARC_DOWN;
} else {
// network.getDemand(node) >= 0
// eg node has supply or flow conservation! want to make an arc
// FROM this node
arc = network->add_artificial_arc(node->id, ROOT_NODE);
arc->flow = node->demand;
basic_arcs[i] = arc;
basic_arc_dirs[i] = ARC_UP;
}
arc->state = ARC_STATE_T;
basic_arcs[0] = NULL;
}
}
void TreeSolution::calc_initial_tree_structure() {
depth[ROOT_NODE] = 0;
pred[ROOT_NODE] = -1;
thread[ROOT_NODE] = 1;
potential[ROOT_NODE] = 0;
int num_nodes = network->num_nodes;
for (int i = 1; i < num_nodes; ++i) {
depth[i] = 1;
pred[i] = 0;
thread[i] = i + 1;
if (basic_arc_dirs[i] == ARC_DOWN) {
potential[i] = network->max_cost;
} else {
potential[i] = -network->max_cost;
}
}
// last node points back to ROOT_NODE
thread[num_nodes - 1] = 0;
}
void TreeSolution::update(const std::vector<Arc*> &F,
const std::vector<Arc*> &B,
long theta,
Arc *entering,
Arc* leaving,
int common_predecessor) {
if (theta != 0) {
for (std::vector<Arc*>::const_iterator it = F.begin();
it != F.end(); ++it) {
(*it)->flow += theta;
}
for (std::vector<Arc*>::const_iterator it = B.begin();
it != B.end(); ++it) {
(*it)->flow -= theta;
}
}
if (leaving->flow == 0) {
// must now be in L
if (!leaving->artificial) {
leaving->state = ARC_STATE_L;
} else {
network->nodes[leaving->v]->outgoing.remove(leaving);
}
} else {
// must be at max capacity, so add to U
leaving->state = ARC_STATE_U;
}
// entering arc must not be removed from L/U because getEnteringArcFrom*
// already do that
entering->state = ARC_STATE_T;
this->update_tree(entering, leaving, common_predecessor);
}
void TreeSolution::update_tree(Arc* entering, Arc* leaving, int join)
{
if (entering == leaving) {
return;
}
int jOut, iOut, jNew = -1, iNew = -1;
// first determine iOut and jOut
if (pred[leaving->v] == leaving->w) {
jOut = leaving->w;
iOut = leaving->v;
} else if (pred[leaving->w] == leaving->v) {
jOut = leaving->v;
iOut = leaving->w;
} else {
throw "Serious problem.";
}
// check whether V or W is in subtree of iOut
int i = iOut;
// Check whether blocking arc is on way from entering.V to join or from
// entering.W to join. This tells us which side of the entering arc is
// the root of the subtree now hanging at the entering arc.
i = entering->w;
while (depth[i] > depth[join]) {
if (i == leaving->v || i == leaving->w) {
iNew = entering->w;
jNew = entering->v;
break;
}
i = pred[i];
}
i = entering->v;
while (depth[i] > depth[join]) {
if (i == leaving->v || i == leaving->w) {
iNew = entering->v;
jNew = entering->w;
break;
}
i = pred[i];
}
if (jNew == -1) {
throw "Serious problem.";
}
update_thread_parent(jOut, iOut, jNew, iNew, join);
update_arc_dir(jOut, iOut, jNew, iNew, entering);
update_depth_pot(jOut, iOut, jNew, iNew);
}
void TreeSolution::update_thread_parent(int jOut, int iOut, int jNew, int iNew,
int join) {
bool parent_first = false;
int i;
int first = -1;
int last;
int right;
int stem, newStem, predStem;
if (join == jOut) {
// determine whether jNew or iNew appear first on the thread
i = pred[join];
// have to do this because ROOT_NODE might be the "join"
if (i == -1) {
for (int index = 0; index < network->num_nodes; ++index) {
if (thread[index] == 0) {
i = index;
}
}
}
while (thread[i] != iNew && thread[i] != jNew) {
i = thread[i];
}
if (thread[i] == jNew) {
parent_first = true;
}
while (thread[i] != iOut) {
i = thread[i];
}
first = i;
}
// now traverse the stem
i = iNew;
// calculate the last right successor for the first iteration
while (depth[thread[i]] > depth[iNew]) {
i = thread[i];
}
right = thread[i];
// i is the last right successor if i (iNew) now
if (thread[jNew] == iOut) {
last = i;
while (depth[last] > depth[iOut]) {
last = thread[last];
}
if (last == iOut) {
last = thread[last];
}
} else {
last = thread[jNew];
}
thread[jNew] = iNew;
stem = iNew;
predStem = jNew;
while (stem != iOut) {
// i is the last right successor at this point
thread[i] = pred[stem];
i = pred[stem];
while (thread[i] != stem) {
i = thread[i];
}
// i is the last left successor of parent
thread[i] = right;
newStem = pred[stem];
pred[stem] = predStem;
predStem = stem;
stem = newStem;
// set i to the last right successor again
i = stem;
while (depth[thread[i]] > depth[stem]) {
i = thread[i];
}
right = thread[i];
}
thread[i] = last;
if (join == jOut) {
if (!parent_first) {
i = jOut;
while (thread[i] != iOut) {
i = thread[i];
}
thread[i] = right;
} else if (first != jNew) {
thread[first] = right;
}
} else {
i = jOut;
while (thread[i] != iOut) {
i = thread[i];
}
thread[i] = right;
}
pred[iOut] = predStem;
}
void TreeSolution::update_arc_dir(int jOut, int iOut, int jNew, int iNew,
Arc *entering) {
int i = iOut;
while (i != iNew) {
int j = pred[i];
basic_arcs[i] = basic_arcs[j];
if (basic_arc_dirs[j] == ARC_UP) {
basic_arc_dirs[i] = ARC_DOWN;
} else {
basic_arc_dirs[i] = ARC_UP;
}
i = j;
}
basic_arcs[iNew] = entering;
if (entering->v == iNew) {
basic_arc_dirs[iNew] = ARC_UP;
} else {
basic_arc_dirs[iNew] = ARC_DOWN;
}
}
/**
* Updates the depth of the dynamic spanning tree structure as well as the
* potential.
*
*/
void TreeSolution::update_depth_pot(int jOut, int iOut, int jNew, int iNew)
{
int i, j;
i = thread[jNew];
while (true) {
j = pred[i];
if (j == -1) {
// reached root
break;
}
depth[i] = depth[j] + 1;
// determine if there is a backward or a forward arc.
if (basic_arc_dirs[i] == ARC_UP) {
// forward arc in the tree exists
potential[i] = potential[pred[i]]
- basic_arcs[i]->cost;
} else {
// backward arc in the tree exists
potential[i] = potential[pred[i]]
+ basic_arcs[i]->cost;
}
if (depth[i] <= depth[jNew]) {
break;
} else {
i = thread[i];
}
}
}
long TreeSolution::solution_value() {
long sum = 0;
for (int i = 0; i < network->num_nodes; i++) {
for (std::list<Arc*>::iterator it = network->nodes[i]->outgoing.begin();
it != network->nodes[i]->outgoing.end(); ++it) {
sum += (*it)->flow * (*it)->cost;
}
}
return sum;
}
void TreeSolution::print_structure() {
for (int i = 0; i < network->num_nodes; ++i) {
std::cout << pred[i] << " ";
}
std::cout << std::endl;
for (int i = 0; i < network->num_nodes; ++i) {
std::cout << depth[i] << " ";
}
std::cout << std::endl;
for (int i = 0; i < network->num_nodes; ++i) {
std::cout << thread[i] << " ";
}
std::cout << std::endl;
}