-
Notifications
You must be signed in to change notification settings - Fork 4
/
ibfs.h
executable file
·503 lines (449 loc) · 13.3 KB
/
ibfs.h
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*
#########################################################
# #
# IBFSGraph - Software for solving #
# Maximum s-t Flow / Minimum s-t Cut #
# using the IBFS algorithm #
# #
# http://www.cs.tau.ac.il/~sagihed/ibfs/ #
# #
# Haim Kaplan (haimk@cs.tau.ac.il) #
# Sagi Hed (sagihed@post.tau.ac.il) #
# #
#########################################################
This software implements the IBFS (Incremental Breadth First Search) maximum flow algorithm from
"Faster and More Dynamic Maximum Flow
by Incremental Breadth-First Search"
Andrew V. Goldberg, Sagi Hed, Haim Kaplan, Pushmeet Kohli,
Robert E. Tarjan, and Renato F. Werneck
In Proceedings of the 23rd European conference on Algorithms, ESA'15
2015
and from
"Maximum flows by incremental breadth-first search"
Andrew V. Goldberg, Sagi Hed, Haim Kaplan, Robert E. Tarjan, and Renato F. Werneck.
In Proceedings of the 19th European conference on Algorithms, ESA'11, pages 457-468.
ISBN 978-3-642-23718-8
2011
Copyright Haim Kaplan (haimk@cs.tau.ac.il) and Sagi Hed (sagihed@post.tau.ac.il)
###########
# LICENSE #
###########
This software can be used for research purposes only.
If you use this software for research purposes, you should cite the aforementioned papers
in any resulting publication and appropriately credit it.
If you require another license, please contact the above.
*/
#ifndef _IBFS_H__
#define _IBFS_H__
#include <stdio.h>
#include <string.h>
#include <cassert>
#include <limits>
#include <cmath>
#define IB_BOTTLENECK_ORIG 0
#define IBTEST 0
#define IB_MIN_MARGINALS_DEBUG 0
#define IB_MIN_MARGINALS_TEST 0
#define IBSTATS 0
#define IBDEBUG(X) fprintf(stdout, "\n"); fflush(stdout)
#define IB_ALTERNATE_SMART 1
#define IB_HYBRID_ADOPTION 1
#define IB_EXCESSES 1
#define IB_ALLOC_INIT_LEVELS 4096
#define IB_DEBUG_INIT 0
#define MAX_ARCS_PER_NODE 4
#define NULL_INDEX std::numeric_limits<node_index_t>::max()
#define CAST_TO_PTR(a) ((a) == NULL_INDEX ? NULL : &nodes[a])
#define CAST_TO_INDEX(a) ((node_index_t) ((a) == NULL ? NULL_INDEX : (a) - nodes))
class IBFSGraph
{
typedef unsigned int node_index_t;
public:
typedef float capacity_t;
IBFSGraph();
~IBFSGraph();
void initSize(node_index_t numNodes);
void addEdge(node_index_t nodeIndexFrom, node_index_t nodeIndexTo, capacity_t capacity, capacity_t reverseCapacity);
void addNode(node_index_t nodeIndex, capacity_t capFromSource, capacity_t capToSink);
struct Arc;
void initGraph();
capacity_t computeMaxFlow();
capacity_t computeMaxFlow(bool allowIncrements);
int isNodeOnSrcSide(node_index_t nodeIndex, int freeNodeValue = 0);
struct Node;
struct Arc
{
node_index_t head;
#if 0
bool isRevResidual_;
capacity_t capacity_;
capacity_t getRCap() {
return capacity_;
}
void setRCap(capacity_t new_capacity) {
capacity_ = new_capacity;
}
bool getIsRevResidual() {
return isRevResidual_;
}
void setIsRevResidual(bool isRevResidual) {
isRevResidual_ = isRevResidual;
}
#else
capacity_t capacity;
capacity_t getRCap() {
return (capacity_t) copysign(capacity, 1.0f);
}
void setRCap(capacity_t new_capacity) {
capacity = (capacity_t) copysign(std::max((capacity_t) 0, new_capacity), capacity);
}
bool getIsRevResidual() {
return std::signbit(capacity);
}
void setIsRevResidual(bool isRevResidual) {
capacity = (capacity_t) copysign(capacity, (isRevResidual ? -1.0f : 1.0f));
}
#endif
inline Arc* rev(Node *nodes) {
char* thiz = ((char*) this);
Node *thiz_node = (Node*) (thiz - (thiz - (char*) nodes) % sizeof(Node));
Node *head_node = &nodes[head];
for (int i = 0; i < MAX_ARCS_PER_NODE; ++i) {
if (head_node->arcs[i].head == thiz_node - nodes) {
return &head_node->arcs[i];
}
}
return NULL;
}
};
struct Node
{
int lastAugTimestamp:30;
int isParentCurr:1;
int isIncremental:1; // 4
Arc arcs[MAX_ARCS_PER_NODE]; // 8*4 = 32 | 36
Arc *parent;
node_index_t firstSon;
node_index_t nextPtr; // 8 + 4 * 4 | 60
int label; // label > 0: distance from s, label < 0: -distance from t
capacity_t excess; // excess > 0: capacity from s, excess < 0: -capacity to t
int arcsDegree() {
int degree = 0;
while (degree < MAX_ARCS_PER_NODE && arcs[degree].head != NULL_INDEX) {
++degree;
}
return degree;
}
};
private:
Arc *arcIter;
void augment(Arc *bridge);
template<bool sTree> int augmentPath(Node *x, capacity_t push);
template<bool sTree> int augmentExcess(Node *x, capacity_t push);
template<bool sTree> void augmentExcesses();
template<bool sTree> void augmentIncrements();
template <bool sTree> void adoption(int fromLevel, bool toTop);
template <bool sTree> void adoption3Pass(int minBucket);
template <bool dirS> void growth();
capacity_t computeMaxFlow(bool trackChanges, bool initialDirS);
// push relabel
class ActiveList
{
public:
inline ActiveList() {
list = NULL;
len = 0;
}
inline void init(node_index_t *mem) {
list = mem;
len = 0;
}
inline void clear() {
len = 0;
}
inline void add(node_index_t x) {
list[len] = x;
len++;
}
inline node_index_t pop() {
len--;
return list[len];
}
inline node_index_t* getEnd() {
return list+len;
}
inline static void swapLists(ActiveList *a, ActiveList *b) {
ActiveList tmp = (*a);
(*a) = (*b);
(*b) = tmp;
}
node_index_t *list;
int len;
};
#define IB_PREVPTR_EXCESS(x) (ptrs[(((x)-nodes)<<1) + 1])
#define IB_NEXTPTR_EXCESS(x) (ptrs[((x)-nodes)<<1])
#define IB_PREVPTR_3PASS(x) ((x)->firstSon)
class BucketsOneSided
{
public:
inline BucketsOneSided() {
buckets = NULL;
maxBucket = 0;
nodes = NULL;
allocLevels = 0;
}
inline void init(Node *a_nodes, int numNodes) {
nodes = a_nodes;
allocLevels = numNodes/8;
if (allocLevels < IB_ALLOC_INIT_LEVELS) {
if (numNodes < IB_ALLOC_INIT_LEVELS) allocLevels = numNodes;
else allocLevels = IB_ALLOC_INIT_LEVELS;
}
buckets = new Node*[allocLevels+1];
memset(buckets, 0, sizeof(Node*)*(allocLevels+1));
maxBucket = 0;
}
inline void allocate(int numLevels) {
if (numLevels > allocLevels) {
allocLevels <<= 1;
Node **alloc = new Node*[allocLevels+1];
memset(alloc, 0, sizeof(Node*)*(allocLevels+1));
delete []buckets;
buckets = alloc;
}
}
inline void free() {
delete []buckets;
buckets = NULL;
}
template <bool sTree> inline void add(Node* x) {
int bucket = (sTree ? (x->label) : (-x->label));
x->nextPtr = CAST_TO_INDEX(buckets[bucket]);
buckets[bucket] = x;
if (bucket > maxBucket) maxBucket = bucket;
}
inline Node* popFront(int bucket) {
Node *x;
if ((x = buckets[bucket]) == NULL) return NULL;
buckets[bucket] = CAST_TO_PTR(x->nextPtr);
return x;
}
Node **buckets;
int maxBucket;
Node *nodes;
int allocLevels;
};
class Buckets3Pass
{
public:
inline Buckets3Pass() {
buckets = NULL;
nodes = NULL;
maxBucket = allocLevels = -1;
}
inline void init(Node *a_nodes, int numNodes) {
nodes = a_nodes;
allocLevels = numNodes/8;
if (allocLevels < IB_ALLOC_INIT_LEVELS) {
if (numNodes < IB_ALLOC_INIT_LEVELS) allocLevels = numNodes;
else allocLevels = IB_ALLOC_INIT_LEVELS;
}
buckets = new Node*[allocLevels+1];
memset(buckets, 0, sizeof(Node*)*(allocLevels+1));
maxBucket = 0;
}
inline void allocate(int numLevels) {
if (numLevels > allocLevels) {
allocLevels <<= 1;
Node **alloc = new Node*[allocLevels+1];
memset(alloc, 0, sizeof(Node*)*(allocLevels+1));
//memcpy(alloc, buckets, sizeof(Node*)*(allocLevels+1));
delete []buckets;
buckets = alloc;
}
}
inline void free() {
delete []buckets;
buckets = NULL;
}
template <bool sTree> inline void add(Node* x) {
int bucket = (sTree ? (x->label) : (-x->label));
if ((x->nextPtr = CAST_TO_INDEX(buckets[bucket])) != NULL_INDEX) IB_PREVPTR_3PASS(CAST_TO_PTR(x->nextPtr)) = CAST_TO_INDEX(x);
buckets[bucket] = x;
if (bucket > maxBucket) maxBucket = bucket;
}
inline Node* popFront(int bucket) {
Node *x = buckets[bucket];
if (x == NULL) return NULL;
buckets[bucket] = CAST_TO_PTR(x->nextPtr);
IB_PREVPTR_3PASS(x) = NULL_INDEX;
return x;
}
template <bool sTree> inline void remove(Node *x) {
int bucket = (sTree ? (x->label) : (-x->label));
if (buckets[bucket] == x) {
buckets[bucket] = CAST_TO_PTR(x->nextPtr);
} else {
nodes[IB_PREVPTR_3PASS(x)].nextPtr = x->nextPtr;
if (x->nextPtr != NULL_INDEX) IB_PREVPTR_3PASS(CAST_TO_PTR(x->nextPtr)) = IB_PREVPTR_3PASS(x);
}
IB_PREVPTR_3PASS(x) = NULL_INDEX;
}
inline bool isEmpty(int bucket) {
return buckets[bucket] == NULL;
}
Node **buckets;
int maxBucket;
Node *nodes;
int allocLevels;
};
class ExcessBuckets
{
public:
inline ExcessBuckets() {
buckets = NULL;
ptrs = NULL;
nodes = NULL;
allocLevels = maxBucket = minBucket = -1;
}
inline void init(Node *a_nodes, node_index_t *a_ptrs, int numNodes) {
nodes = a_nodes;
allocLevels = numNodes/8;
if (allocLevels < IB_ALLOC_INIT_LEVELS) {
if (numNodes < IB_ALLOC_INIT_LEVELS) allocLevels = numNodes;
else allocLevels = IB_ALLOC_INIT_LEVELS;
}
buckets = new Node*[allocLevels+1];
memset(buckets, 0, sizeof(Node*)*(allocLevels+1));
ptrs = a_ptrs;
reset();
}
inline void allocate(int numLevels) {
if (numLevels > allocLevels) {
allocLevels <<= 1;
Node **alloc = new Node*[allocLevels+1];
memset(alloc, 0, sizeof(Node*)*(allocLevels+1));
//memcpy(alloc, buckets, sizeof(Node*)*(allocLevels+1));
delete []buckets;
buckets = alloc;
}
}
inline void free() {
delete []buckets;
buckets = NULL;
}
template <bool sTree> inline void add(Node* x) {
int bucket = (sTree ? (x->label) : (-x->label));
IB_NEXTPTR_EXCESS(x) = CAST_TO_INDEX(buckets[bucket]);
if (buckets[bucket] != NULL) {
IB_PREVPTR_EXCESS(buckets[bucket]) = CAST_TO_INDEX(x);
}
buckets[bucket] = x;
if (bucket > maxBucket) maxBucket = bucket;
if (bucket != 0 && bucket < minBucket) minBucket = bucket;
}
inline Node* popFront(int bucket) {
Node *x = buckets[bucket];
if (x == NULL) return NULL;
buckets[bucket] = CAST_TO_PTR(IB_NEXTPTR_EXCESS(x));
return x;
}
template <bool sTree> inline void remove(Node *x) {
int bucket = (sTree ? (x->label) : (-x->label));
if (buckets[bucket] == x) {
buckets[bucket] = CAST_TO_PTR(IB_NEXTPTR_EXCESS(x));
} else {
IB_NEXTPTR_EXCESS(CAST_TO_PTR(IB_PREVPTR_EXCESS(x))) = IB_NEXTPTR_EXCESS(x);
if (IB_NEXTPTR_EXCESS(x) != NULL_INDEX) IB_PREVPTR_EXCESS(CAST_TO_PTR(IB_NEXTPTR_EXCESS(x))) = IB_PREVPTR_EXCESS(x);
}
}
inline void incMaxBucket(int bucket) {
if (maxBucket < bucket) maxBucket = bucket;
}
inline bool empty() {
return maxBucket < minBucket;
}
inline void reset() {
maxBucket = 0;
minBucket = -1 ^ (1<<31);
}
Node **buckets;
node_index_t *ptrs;
int maxBucket;
int minBucket;
Node *nodes;
int allocLevels;
};
// members
Node *nodes, *nodeEnd;
Arc *arcEnd;
node_index_t *ptrs;
int numNodes;
capacity_t flow;
short augTimestamp;
int topLevelS, topLevelT;
ActiveList active0, activeS1, activeT1;
Node **incList;
int incLen;
int incIteration;
Buckets3Pass orphan3PassBuckets;
BucketsOneSided orphanBuckets;
ExcessBuckets excessBuckets;
Buckets3Pass &prNodeBuckets;
bool verbose;
//
// Orphans
//
unsigned int uniqOrphansS, uniqOrphansT;
template <bool sTree> inline void orphanFree(Node *x) {
if (IB_EXCESSES && x->excess) {
x->label = (sTree ? -topLevelT : topLevelS);
if (sTree) activeT1.add(CAST_TO_INDEX(x));
else activeS1.add(CAST_TO_INDEX(x));
x->isParentCurr = 0;
} else {
x->label = 0;
}
}
void initGraphFast();
void initNodes();
};
inline void IBFSGraph::addNode(node_index_t nodeIndex, capacity_t capSource, capacity_t capSink)
{
capacity_t f = nodes[nodeIndex].excess;
if (f > 0) {
capSource += f;
} else {
capSink -= f;
}
if (capSource < capSink) {
flow += capSource;
} else {
flow += capSink;
}
nodes[nodeIndex].excess = capSource - capSink;
}
inline void IBFSGraph::addEdge(node_index_t nodeIndexFrom, node_index_t nodeIndexTo, capacity_t capacity, capacity_t reverseCapacity)
{
int fromNextEdgeIndex = nodes[nodeIndexFrom].arcsDegree();
int toNextEdgeIndex = nodes[nodeIndexTo].arcsDegree();
assert (fromNextEdgeIndex < MAX_ARCS_PER_NODE && toNextEdgeIndex < MAX_ARCS_PER_NODE);
nodes[nodeIndexFrom ].arcs[fromNextEdgeIndex].head = nodeIndexTo;
nodes[nodeIndexFrom ].arcs[fromNextEdgeIndex].setRCap(capacity);
nodes[nodeIndexFrom ].arcs[fromNextEdgeIndex].setIsRevResidual(reverseCapacity != 0);
nodes[nodeIndexTo ].arcs[toNextEdgeIndex ].head = nodeIndexFrom;
nodes[nodeIndexTo ].arcs[toNextEdgeIndex ].setRCap(reverseCapacity);
nodes[nodeIndexTo ].arcs[toNextEdgeIndex ].setIsRevResidual(capacity != 0);
// use label as a temporary storage
// to count the out degree of nodes
nodes[nodeIndexFrom].label++;
nodes[nodeIndexTo].label++;
}
inline int IBFSGraph::isNodeOnSrcSide(node_index_t nodeIndex, int freeNodeValue)
{
if (nodes[nodeIndex].label == 0) {
return freeNodeValue;
}
return (nodes[nodeIndex].label > 0 ? 1 : 0);
}
#endif