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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
|
/* -*- mode: C++; indent-tabs-mode: nil; -*-
*
* This file is a part of LEMON, a generic C++ optimization library.
*
* Copyright (C) 2003-2011
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
*
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
*
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
* purpose.
*
*/
#ifndef LEMON_PREFLOW_H
#define LEMON_PREFLOW_H
#include <lemon/tolerance.h>
#include <lemon/elevator.h>
/// \file
/// \ingroup max_flow
/// \brief Implementation of the preflow algorithm.
namespace lemon {
/// \brief Default traits class of Preflow class.
///
/// Default traits class of Preflow class.
/// \tparam GR Digraph type.
/// \tparam CAP Capacity map type.
template <typename GR, typename CAP>
struct PreflowDefaultTraits {
/// \brief The type of the digraph the algorithm runs on.
typedef GR Digraph;
/// \brief The type of the map that stores the arc capacities.
///
/// The type of the map that stores the arc capacities.
/// It must meet the \ref concepts::ReadMap "ReadMap" concept.
typedef CAP CapacityMap;
/// \brief The type of the flow values.
typedef typename CapacityMap::Value Value;
/// \brief The type of the map that stores the flow values.
///
/// The type of the map that stores the flow values.
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
#ifdef DOXYGEN
typedef GR::ArcMap<Value> FlowMap;
#else
typedef typename Digraph::template ArcMap<Value> FlowMap;
#endif
/// \brief Instantiates a FlowMap.
///
/// This function instantiates a \ref FlowMap.
/// \param digraph The digraph for which we would like to define
/// the flow map.
static FlowMap* createFlowMap(const Digraph& digraph) {
return new FlowMap(digraph);
}
/// \brief The elevator type used by Preflow algorithm.
///
/// The elevator type used by Preflow algorithm.
///
/// \sa Elevator, LinkedElevator
#ifdef DOXYGEN
typedef lemon::Elevator<GR, GR::Node> Elevator;
#else
typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator;
#endif
/// \brief Instantiates an Elevator.
///
/// This function instantiates an \ref Elevator.
/// \param digraph The digraph for which we would like to define
/// the elevator.
/// \param max_level The maximum level of the elevator.
static Elevator* createElevator(const Digraph& digraph, int max_level) {
return new Elevator(digraph, max_level);
}
/// \brief The tolerance used by the algorithm
///
/// The tolerance used by the algorithm to handle inexact computation.
typedef lemon::Tolerance<Value> Tolerance;
};
/// \ingroup max_flow
///
/// \brief %Preflow algorithm class.
///
/// This class provides an implementation of Goldberg-Tarjan's \e preflow
/// \e push-relabel algorithm producing a \ref max_flow
/// "flow of maximum value" in a digraph \ref clrs01algorithms,
/// \ref amo93networkflows, \ref goldberg88newapproach.
/// The preflow algorithms are the fastest known maximum
/// flow algorithms. The current implementation uses a mixture of the
/// \e "highest label" and the \e "bound decrease" heuristics.
/// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$.
///
/// The algorithm consists of two phases. After the first phase
/// the maximum flow value and the minimum cut is obtained. The
/// second phase constructs a feasible maximum flow on each arc.
///
/// \warning This implementation cannot handle infinite or very large
/// capacities (e.g. the maximum value of \c CAP::Value).
///
/// \tparam GR The type of the digraph the algorithm runs on.
/// \tparam CAP The type of the capacity map. The default map
/// type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
/// \tparam TR The traits class that defines various types used by the
/// algorithm. By default, it is \ref PreflowDefaultTraits
/// "PreflowDefaultTraits<GR, CAP>".
/// In most cases, this parameter should not be set directly,
/// consider to use the named template parameters instead.
#ifdef DOXYGEN
template <typename GR, typename CAP, typename TR>
#else
template <typename GR,
typename CAP = typename GR::template ArcMap<int>,
typename TR = PreflowDefaultTraits<GR, CAP> >
#endif
class Preflow {
public:
///The \ref PreflowDefaultTraits "traits class" of the algorithm.
typedef TR Traits;
///The type of the digraph the algorithm runs on.
typedef typename Traits::Digraph Digraph;
///The type of the capacity map.
typedef typename Traits::CapacityMap CapacityMap;
///The type of the flow values.
typedef typename Traits::Value Value;
///The type of the flow map.
typedef typename Traits::FlowMap FlowMap;
///The type of the elevator.
typedef typename Traits::Elevator Elevator;
///The type of the tolerance.
typedef typename Traits::Tolerance Tolerance;
private:
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
const Digraph& _graph;
const CapacityMap* _capacity;
int _node_num;
Node _source, _target;
FlowMap* _flow;
bool _local_flow;
Elevator* _level;
bool _local_level;
typedef typename Digraph::template NodeMap<Value> ExcessMap;
ExcessMap* _excess;
Tolerance _tolerance;
bool _phase;
void createStructures() {
_node_num = countNodes(_graph);
if (!_flow) {
_flow = Traits::createFlowMap(_graph);
_local_flow = true;
}
if (!_level) {
_level = Traits::createElevator(_graph, _node_num);
_local_level = true;
}
if (!_excess) {
_excess = new ExcessMap(_graph);
}
}
void destroyStructures() {
if (_local_flow) {
delete _flow;
}
if (_local_level) {
delete _level;
}
if (_excess) {
delete _excess;
}
}
public:
typedef Preflow Create;
///\name Named Template Parameters
///@{
template <typename T>
struct SetFlowMapTraits : public Traits {
typedef T FlowMap;
static FlowMap *createFlowMap(const Digraph&) {
LEMON_ASSERT(false, "FlowMap is not initialized");
return 0; // ignore warnings
}
};
/// \brief \ref named-templ-param "Named parameter" for setting
/// FlowMap type
///
/// \ref named-templ-param "Named parameter" for setting FlowMap
/// type.
template <typename T>
struct SetFlowMap
: public Preflow<Digraph, CapacityMap, SetFlowMapTraits<T> > {
typedef Preflow<Digraph, CapacityMap,
SetFlowMapTraits<T> > Create;
};
template <typename T>
struct SetElevatorTraits : public Traits {
typedef T Elevator;
static Elevator *createElevator(const Digraph&, int) {
LEMON_ASSERT(false, "Elevator is not initialized");
return 0; // ignore warnings
}
};
/// \brief \ref named-templ-param "Named parameter" for setting
/// Elevator type
///
/// \ref named-templ-param "Named parameter" for setting Elevator
/// type. If this named parameter is used, then an external
/// elevator object must be passed to the algorithm using the
/// \ref elevator(Elevator&) "elevator()" function before calling
/// \ref run() or \ref init().
/// \sa SetStandardElevator
template <typename T>
struct SetElevator
: public Preflow<Digraph, CapacityMap, SetElevatorTraits<T> > {
typedef Preflow<Digraph, CapacityMap,
SetElevatorTraits<T> > Create;
};
template <typename T>
struct SetStandardElevatorTraits : public Traits {
typedef T Elevator;
static Elevator *createElevator(const Digraph& digraph, int max_level) {
return new Elevator(digraph, max_level);
}
};
/// \brief \ref named-templ-param "Named parameter" for setting
/// Elevator type with automatic allocation
///
/// \ref named-templ-param "Named parameter" for setting Elevator
/// type with automatic allocation.
/// The Elevator should have standard constructor interface to be
/// able to automatically created by the algorithm (i.e. the
/// digraph and the maximum level should be passed to it).
/// However, an external elevator object could also be passed to the
/// algorithm with the \ref elevator(Elevator&) "elevator()" function
/// before calling \ref run() or \ref init().
/// \sa SetElevator
template <typename T>
struct SetStandardElevator
: public Preflow<Digraph, CapacityMap,
SetStandardElevatorTraits<T> > {
typedef Preflow<Digraph, CapacityMap,
SetStandardElevatorTraits<T> > Create;
};
/// @}
protected:
Preflow() {}
public:
/// \brief The constructor of the class.
///
/// The constructor of the class.
/// \param digraph The digraph the algorithm runs on.
/// \param capacity The capacity of the arcs.
/// \param source The source node.
/// \param target The target node.
Preflow(const Digraph& digraph, const CapacityMap& capacity,
Node source, Node target)
: _graph(digraph), _capacity(&capacity),
_node_num(0), _source(source), _target(target),
_flow(0), _local_flow(false),
_level(0), _local_level(false),
_excess(0), _tolerance(), _phase() {}
/// \brief Destructor.
///
/// Destructor.
~Preflow() {
destroyStructures();
}
/// \brief Sets the capacity map.
///
/// Sets the capacity map.
/// \return <tt>(*this)</tt>
Preflow& capacityMap(const CapacityMap& map) {
_capacity = ↦
return *this;
}
/// \brief Sets the flow map.
///
/// Sets the flow map.
/// If you don't use this function before calling \ref run() or
/// \ref init(), an instance will be allocated automatically.
/// The destructor deallocates this automatically allocated map,
/// of course.
/// \return <tt>(*this)</tt>
Preflow& flowMap(FlowMap& map) {
if (_local_flow) {
delete _flow;
_local_flow = false;
}
_flow = ↦
return *this;
}
/// \brief Sets the source node.
///
/// Sets the source node.
/// \return <tt>(*this)</tt>
Preflow& source(const Node& node) {
_source = node;
return *this;
}
/// \brief Sets the target node.
///
/// Sets the target node.
/// \return <tt>(*this)</tt>
Preflow& target(const Node& node) {
_target = node;
return *this;
}
/// \brief Sets the elevator used by algorithm.
///
/// Sets the elevator used by algorithm.
/// If you don't use this function before calling \ref run() or
/// \ref init(), an instance will be allocated automatically.
/// The destructor deallocates this automatically allocated elevator,
/// of course.
/// \return <tt>(*this)</tt>
Preflow& elevator(Elevator& elevator) {
if (_local_level) {
delete _level;
_local_level = false;
}
_level = &elevator;
return *this;
}
/// \brief Returns a const reference to the elevator.
///
/// Returns a const reference to the elevator.
///
/// \pre Either \ref run() or \ref init() must be called before
/// using this function.
const Elevator& elevator() const {
return *_level;
}
/// \brief Sets the tolerance used by the algorithm.
///
/// Sets the tolerance object used by the algorithm.
/// \return <tt>(*this)</tt>
Preflow& tolerance(const Tolerance& tolerance) {
_tolerance = tolerance;
return *this;
}
/// \brief Returns a const reference to the tolerance.
///
/// Returns a const reference to the tolerance object used by
/// the algorithm.
const Tolerance& tolerance() const {
return _tolerance;
}
/// \name Execution Control
/// The simplest way to execute the preflow algorithm is to use
/// \ref run() or \ref runMinCut().\n
/// If you need better control on the initial solution or the execution,
/// you have to call one of the \ref init() functions first, then
/// \ref startFirstPhase() and if you need it \ref startSecondPhase().
///@{
/// \brief Initializes the internal data structures.
///
/// Initializes the internal data structures and sets the initial
/// flow to zero on each arc.
void init() {
createStructures();
_phase = true;
for (NodeIt n(_graph); n != INVALID; ++n) {
(*_excess)[n] = 0;
}
for (ArcIt e(_graph); e != INVALID; ++e) {
_flow->set(e, 0);
}
typename Digraph::template NodeMap<bool> reached(_graph, false);
_level->initStart();
_level->initAddItem(_target);
std::vector<Node> queue;
reached[_source] = true;
queue.push_back(_target);
reached[_target] = true;
while (!queue.empty()) {
_level->initNewLevel();
std::vector<Node> nqueue;
for (int i = 0; i < int(queue.size()); ++i) {
Node n = queue[i];
for (InArcIt e(_graph, n); e != INVALID; ++e) {
Node u = _graph.source(e);
if (!reached[u] && _tolerance.positive((*_capacity)[e])) {
reached[u] = true;
_level->initAddItem(u);
nqueue.push_back(u);
}
}
}
queue.swap(nqueue);
}
_level->initFinish();
for (OutArcIt e(_graph, _source); e != INVALID; ++e) {
if (_tolerance.positive((*_capacity)[e])) {
Node u = _graph.target(e);
if ((*_level)[u] == _level->maxLevel()) continue;
_flow->set(e, (*_capacity)[e]);
(*_excess)[u] += (*_capacity)[e];
if (u != _target && !_level->active(u)) {
_level->activate(u);
}
}
}
}
/// \brief Initializes the internal data structures using the
/// given flow map.
///
/// Initializes the internal data structures and sets the initial
/// flow to the given \c flowMap. The \c flowMap should contain a
/// flow or at least a preflow, i.e. at each node excluding the
/// source node the incoming flow should greater or equal to the
/// outgoing flow.
/// \return \c false if the given \c flowMap is not a preflow.
template <typename FlowMap>
bool init(const FlowMap& flowMap) {
createStructures();
for (ArcIt e(_graph); e != INVALID; ++e) {
_flow->set(e, flowMap[e]);
}
for (NodeIt n(_graph); n != INVALID; ++n) {
Value excess = 0;
for (InArcIt e(_graph, n); e != INVALID; ++e) {
excess += (*_flow)[e];
}
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
excess -= (*_flow)[e];
}
if (excess < 0 && n != _source) return false;
(*_excess)[n] = excess;
}
typename Digraph::template NodeMap<bool> reached(_graph, false);
_level->initStart();
_level->initAddItem(_target);
std::vector<Node> queue;
reached[_source] = true;
queue.push_back(_target);
reached[_target] = true;
while (!queue.empty()) {
_level->initNewLevel();
std::vector<Node> nqueue;
for (int i = 0; i < int(queue.size()); ++i) {
Node n = queue[i];
for (InArcIt e(_graph, n); e != INVALID; ++e) {
Node u = _graph.source(e);
if (!reached[u] &&
_tolerance.positive((*_capacity)[e] - (*_flow)[e])) {
reached[u] = true;
_level->initAddItem(u);
nqueue.push_back(u);
}
}
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
Node v = _graph.target(e);
if (!reached[v] && _tolerance.positive((*_flow)[e])) {
reached[v] = true;
_level->initAddItem(v);
nqueue.push_back(v);
}
}
}
queue.swap(nqueue);
}
_level->initFinish();
for (OutArcIt e(_graph, _source); e != INVALID; ++e) {
Value rem = (*_capacity)[e] - (*_flow)[e];
if (_tolerance.positive(rem)) {
Node u = _graph.target(e);
if ((*_level)[u] == _level->maxLevel()) continue;
_flow->set(e, (*_capacity)[e]);
(*_excess)[u] += rem;
}
}
for (InArcIt e(_graph, _source); e != INVALID; ++e) {
Value rem = (*_flow)[e];
if (_tolerance.positive(rem)) {
Node v = _graph.source(e);
if ((*_level)[v] == _level->maxLevel()) continue;
_flow->set(e, 0);
(*_excess)[v] += rem;
}
}
for (NodeIt n(_graph); n != INVALID; ++n)
if(n!=_source && n!=_target && _tolerance.positive((*_excess)[n]))
_level->activate(n);
return true;
}
/// \brief Starts the first phase of the preflow algorithm.
///
/// The preflow algorithm consists of two phases, this method runs
/// the first phase. After the first phase the maximum flow value
/// and a minimum value cut can already be computed, although a
/// maximum flow is not yet obtained. So after calling this method
/// \ref flowValue() returns the value of a maximum flow and \ref
/// minCut() returns a minimum cut.
/// \pre One of the \ref init() functions must be called before
/// using this function.
void startFirstPhase() {
_phase = true;
while (true) {
int num = _node_num;
Node n = INVALID;
int level = -1;
while (num > 0) {
n = _level->highestActive();
if (n == INVALID) goto first_phase_done;
level = _level->highestActiveLevel();
--num;
Value excess = (*_excess)[n];
int new_level = _level->maxLevel();
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
Value rem = (*_capacity)[e] - (*_flow)[e];
if (!_tolerance.positive(rem)) continue;
Node v = _graph.target(e);
if ((*_level)[v] < level) {
if (!_level->active(v) && v != _target) {
_level->activate(v);
}
if (!_tolerance.less(rem, excess)) {
_flow->set(e, (*_flow)[e] + excess);
(*_excess)[v] += excess;
excess = 0;
goto no_more_push_1;
} else {
excess -= rem;
(*_excess)[v] += rem;
_flow->set(e, (*_capacity)[e]);
}
} else if (new_level > (*_level)[v]) {
new_level = (*_level)[v];
}
}
for (InArcIt e(_graph, n); e != INVALID; ++e) {
Value rem = (*_flow)[e];
if (!_tolerance.positive(rem)) continue;
Node v = _graph.source(e);
if ((*_level)[v] < level) {
if (!_level->active(v) && v != _target) {
_level->activate(v);
}
if (!_tolerance.less(rem, excess)) {
_flow->set(e, (*_flow)[e] - excess);
(*_excess)[v] += excess;
excess = 0;
goto no_more_push_1;
} else {
excess -= rem;
(*_excess)[v] += rem;
_flow->set(e, 0);
}
} else if (new_level > (*_level)[v]) {
new_level = (*_level)[v];
}
}
no_more_push_1:
(*_excess)[n] = excess;
if (excess != 0) {
if (new_level + 1 < _level->maxLevel()) {
_level->liftHighestActive(new_level + 1);
} else {
_level->liftHighestActiveToTop();
}
if (_level->emptyLevel(level)) {
_level->liftToTop(level);
}
} else {
_level->deactivate(n);
}
}
num = _node_num * 20;
while (num > 0) {
while (level >= 0 && _level->activeFree(level)) {
--level;
}
if (level == -1) {
n = _level->highestActive();
level = _level->highestActiveLevel();
if (n == INVALID) goto first_phase_done;
} else {
n = _level->activeOn(level);
}
--num;
Value excess = (*_excess)[n];
int new_level = _level->maxLevel();
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
Value rem = (*_capacity)[e] - (*_flow)[e];
if (!_tolerance.positive(rem)) continue;
Node v = _graph.target(e);
if ((*_level)[v] < level) {
if (!_level->active(v) && v != _target) {
_level->activate(v);
}
if (!_tolerance.less(rem, excess)) {
_flow->set(e, (*_flow)[e] + excess);
(*_excess)[v] += excess;
excess = 0;
goto no_more_push_2;
} else {
excess -= rem;
(*_excess)[v] += rem;
_flow->set(e, (*_capacity)[e]);
}
} else if (new_level > (*_level)[v]) {
new_level = (*_level)[v];
}
}
for (InArcIt e(_graph, n); e != INVALID; ++e) {
Value rem = (*_flow)[e];
if (!_tolerance.positive(rem)) continue;
Node v = _graph.source(e);
if ((*_level)[v] < level) {
if (!_level->active(v) && v != _target) {
_level->activate(v);
}
if (!_tolerance.less(rem, excess)) {
_flow->set(e, (*_flow)[e] - excess);
(*_excess)[v] += excess;
excess = 0;
goto no_more_push_2;
} else {
excess -= rem;
(*_excess)[v] += rem;
_flow->set(e, 0);
}
} else if (new_level > (*_level)[v]) {
new_level = (*_level)[v];
}
}
no_more_push_2:
(*_excess)[n] = excess;
if (excess != 0) {
if (new_level + 1 < _level->maxLevel()) {
_level->liftActiveOn(level, new_level + 1);
} else {
_level->liftActiveToTop(level);
}
if (_level->emptyLevel(level)) {
_level->liftToTop(level);
}
} else {
_level->deactivate(n);
}
}
}
first_phase_done:;
}
/// \brief Starts the second phase of the preflow algorithm.
///
/// The preflow algorithm consists of two phases, this method runs
/// the second phase. After calling one of the \ref init() functions
/// and \ref startFirstPhase() and then \ref startSecondPhase(),
/// \ref flowMap() returns a maximum flow, \ref flowValue() returns the
/// value of a maximum flow, \ref minCut() returns a minimum cut
/// \pre One of the \ref init() functions and \ref startFirstPhase()
/// must be called before using this function.
void startSecondPhase() {
_phase = false;
typename Digraph::template NodeMap<bool> reached(_graph);
for (NodeIt n(_graph); n != INVALID; ++n) {
reached[n] = (*_level)[n] < _level->maxLevel();
}
_level->initStart();
_level->initAddItem(_source);
std::vector<Node> queue;
queue.push_back(_source);
reached[_source] = true;
while (!queue.empty()) {
_level->initNewLevel();
std::vector<Node> nqueue;
for (int i = 0; i < int(queue.size()); ++i) {
Node n = queue[i];
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
Node v = _graph.target(e);
if (!reached[v] && _tolerance.positive((*_flow)[e])) {
reached[v] = true;
_level->initAddItem(v);
nqueue.push_back(v);
}
}
for (InArcIt e(_graph, n); e != INVALID; ++e) {
Node u = _graph.source(e);
if (!reached[u] &&
_tolerance.positive((*_capacity)[e] - (*_flow)[e])) {
reached[u] = true;
_level->initAddItem(u);
nqueue.push_back(u);
}
}
}
queue.swap(nqueue);
}
_level->initFinish();
for (NodeIt n(_graph); n != INVALID; ++n) {
if (!reached[n]) {
_level->dirtyTopButOne(n);
} else if ((*_excess)[n] > 0 && _target != n) {
_level->activate(n);
}
}
Node n;
while ((n = _level->highestActive()) != INVALID) {
Value excess = (*_excess)[n];
int level = _level->highestActiveLevel();
int new_level = _level->maxLevel();
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
Value rem = (*_capacity)[e] - (*_flow)[e];
if (!_tolerance.positive(rem)) continue;
Node v = _graph.target(e);
if ((*_level)[v] < level) {
if (!_level->active(v) && v != _source) {
_level->activate(v);
}
if (!_tolerance.less(rem, excess)) {
_flow->set(e, (*_flow)[e] + excess);
(*_excess)[v] += excess;
excess = 0;
goto no_more_push;
} else {
excess -= rem;
(*_excess)[v] += rem;
_flow->set(e, (*_capacity)[e]);
}
} else if (new_level > (*_level)[v]) {
new_level = (*_level)[v];
}
}
for (InArcIt e(_graph, n); e != INVALID; ++e) {
Value rem = (*_flow)[e];
if (!_tolerance.positive(rem)) continue;
Node v = _graph.source(e);
if ((*_level)[v] < level) {
if (!_level->active(v) && v != _source) {
_level->activate(v);
}
if (!_tolerance.less(rem, excess)) {
_flow->set(e, (*_flow)[e] - excess);
(*_excess)[v] += excess;
excess = 0;
goto no_more_push;
} else {
excess -= rem;
(*_excess)[v] += rem;
_flow->set(e, 0);
}
} else if (new_level > (*_level)[v]) {
new_level = (*_level)[v];
}
}
no_more_push:
(*_excess)[n] = excess;
if (excess != 0) {
if (new_level + 1 < _level->maxLevel()) {
_level->liftHighestActive(new_level + 1);
} else {
// Calculation error
_level->liftHighestActiveToTop();
}
if (_level->emptyLevel(level)) {
// Calculation error
_level->liftToTop(level);
}
} else {
_level->deactivate(n);
}
}
}
/// \brief Runs the preflow algorithm.
///
/// Runs the preflow algorithm.
/// \note pf.run() is just a shortcut of the following code.
/// \code
/// pf.init();
/// pf.startFirstPhase();
/// pf.startSecondPhase();
/// \endcode
void run() {
init();
startFirstPhase();
startSecondPhase();
}
/// \brief Runs the preflow algorithm to compute the minimum cut.
///
/// Runs the preflow algorithm to compute the minimum cut.
/// \note pf.runMinCut() is just a shortcut of the following code.
/// \code
/// pf.init();
/// pf.startFirstPhase();
/// \endcode
void runMinCut() {
init();
startFirstPhase();
}
/// @}
/// \name Query Functions
/// The results of the preflow algorithm can be obtained using these
/// functions.\n
/// Either one of the \ref run() "run*()" functions or one of the
/// \ref startFirstPhase() "start*()" functions should be called
/// before using them.
///@{
/// \brief Returns the value of the maximum flow.
///
/// Returns the value of the maximum flow by returning the excess
/// of the target node. This value equals to the value of
/// the maximum flow already after the first phase of the algorithm.
///
/// \pre Either \ref run() or \ref init() must be called before
/// using this function.
Value flowValue() const {
return (*_excess)[_target];
}
/// \brief Returns the flow value on the given arc.
///
/// Returns the flow value on the given arc. This method can
/// be called after the second phase of the algorithm.
///
/// \pre Either \ref run() or \ref init() must be called before
/// using this function.
Value flow(const Arc& arc) const {
return (*_flow)[arc];
}
/// \brief Returns a const reference to the flow map.
///
/// Returns a const reference to the arc map storing the found flow.
/// This method can be called after the second phase of the algorithm.
///
/// \pre Either \ref run() or \ref init() must be called before
/// using this function.
const FlowMap& flowMap() const {
return *_flow;
}
/// \brief Returns \c true when the node is on the source side of the
/// minimum cut.
///
/// Returns true when the node is on the source side of the found
/// minimum cut. This method can be called both after running \ref
/// startFirstPhase() and \ref startSecondPhase().
///
/// \pre Either \ref run() or \ref init() must be called before
/// using this function.
bool minCut(const Node& node) const {
return ((*_level)[node] == _level->maxLevel()) == _phase;
}
/// \brief Gives back a minimum value cut.
///
/// Sets \c cutMap to the characteristic vector of a minimum value
/// cut. \c cutMap should be a \ref concepts::WriteMap "writable"
/// node map with \c bool (or convertible) value type.
///
/// This method can be called both after running \ref startFirstPhase()
/// and \ref startSecondPhase(). The result after the second phase
/// could be slightly different if inexact computation is used.
///
/// \note This function calls \ref minCut() for each node, so it runs in
/// O(n) time.
///
/// \pre Either \ref run() or \ref init() must be called before
/// using this function.
template <typename CutMap>
void minCutMap(CutMap& cutMap) const {
for (NodeIt n(_graph); n != INVALID; ++n) {
cutMap.set(n, minCut(n));
}
}
/// @}
};
}
#endif
|