Changed to conform to the new iterator style.
8 #include <hugo/invalid.h>
13 /// Implementation of the preflow algorithm.
17 /// \addtogroup flowalgs
20 ///%Preflow algorithms class.
22 ///This class provides an implementation of the \e preflow \e
23 ///algorithm producing a flow of maximum value in a directed
24 ///graph. The preflow algorithms are the fastest max flow algorithms
25 ///up to now. The \e source node, the \e target node, the \e
26 ///capacity of the edges and the \e starting \e flow value of the
27 ///edges should be passed to the algorithm through the
28 ///constructor. It is possible to change these quantities using the
29 ///functions \ref setSource, \ref setTarget, \ref setCap and \ref
32 ///After running \ref phase1() or \ref preflow(), the actual flow
33 ///value can be obtained by calling \ref flowValue(). The minimum
34 ///value cut can be written into a <tt>bool</tt> node map by
35 ///calling \ref minCut(). (\ref minMinCut() and \ref maxMinCut() writes
36 ///the inclusionwise minimum and maximum of the minimum value cuts,
39 ///\param Graph The directed graph type the algorithm runs on.
40 ///\param Num The number type of the capacities and the flow values.
41 ///\param CapMap The capacity map type.
42 ///\param FlowMap The flow map type.
44 ///\author Jacint Szabo
45 template <typename Graph, typename Num,
46 typename CapMap=typename Graph::template EdgeMap<Num>,
47 typename FlowMap=typename Graph::template EdgeMap<Num> >
50 typedef typename Graph::Node Node;
51 typedef typename Graph::NodeIt NodeIt;
52 typedef typename Graph::EdgeIt EdgeIt;
53 typedef typename Graph::OutEdgeIt OutEdgeIt;
54 typedef typename Graph::InEdgeIt InEdgeIt;
56 typedef typename Graph::template NodeMap<Node> NNMap;
57 typedef typename std::vector<Node> VecNode;
62 const CapMap* capacity;
64 int n; //the number of nodes of G
66 typename Graph::template NodeMap<int> level;
67 typename Graph::template NodeMap<Num> excess;
69 // constants used for heuristics
70 static const int H0=20;
71 static const int H1=1;
75 ///Indicates the property of the starting flow map.
77 ///Indicates the property of the starting flow map. The meanings are as follows:
78 ///- \c ZERO_FLOW: constant zero flow
79 ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
80 ///the sum of the out-flows in every node except the \e source and
82 ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at
83 ///least the sum of the out-flows in every node except the \e source.
84 ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be
85 ///set to the constant zero flow in the beginning of the algorithm in this case.
94 ///Indicates the state of the preflow algorithm.
96 ///Indicates the state of the preflow algorithm. The meanings are as follows:
97 ///- \c AFTER_NOTHING: before running the algorithm or at an unspecified state.
98 ///- \c AFTER_PREFLOW_PHASE_1: right after running \c phase1
99 ///- \c AFTER_PREFLOW_PHASE_2: after running \ref phase2()
103 AFTER_PREFLOW_PHASE_1,
104 AFTER_PREFLOW_PHASE_2
109 StatusEnum status; // Do not needle this flag only if necessary.
112 ///The constructor of the class.
114 ///The constructor of the class.
115 ///\param _G The directed graph the algorithm runs on.
116 ///\param _s The source node.
117 ///\param _t The target node.
118 ///\param _capacity The capacity of the edges.
119 ///\param _flow The flow of the edges.
120 ///Except the graph, all of these parameters can be reset by
121 ///calling \ref setSource, \ref setTarget, \ref setCap and \ref
123 Preflow(const Graph& _G, Node _s, Node _t,
124 const CapMap& _capacity, FlowMap& _flow) :
125 g(&_G), s(_s), t(_t), capacity(&_capacity),
126 flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0),
127 flow_prop(NO_FLOW), status(AFTER_NOTHING) { }
131 ///Runs the preflow algorithm.
133 ///Runs the preflow algorithm.
140 ///Runs the preflow algorithm.
142 ///Runs the preflow algorithm.
143 ///\pre The starting flow map must be
144 /// - a constant zero flow if \c fp is \c ZERO_FLOW,
145 /// - an arbitrary flow if \c fp is \c GEN_FLOW,
146 /// - an arbitrary preflow if \c fp is \c PRE_FLOW,
147 /// - any map if \c fp is NO_FLOW.
148 ///If the starting flow map is a flow or a preflow then
149 ///the algorithm terminates faster.
150 void run(FlowEnum fp) {
155 ///Runs the first phase of the preflow algorithm.
157 ///The preflow algorithm consists of two phases, this method runs the
158 ///first phase. After the first phase the maximum flow value and a
159 ///minimum value cut can already be computed, though a maximum flow
160 ///is not yet obtained. So after calling this method \ref flowValue
161 ///and \ref minCut gives proper results.
162 ///\warning \ref minMinCut and \ref maxMinCut do not
163 ///give minimum value cuts unless calling \ref phase2.
164 ///\pre The starting flow must be
165 /// - a constant zero flow if \c fp is \c ZERO_FLOW,
166 /// - an arbitary flow if \c fp is \c GEN_FLOW,
167 /// - an arbitary preflow if \c fp is \c PRE_FLOW,
168 /// - any map if \c fp is NO_FLOW.
169 void phase1(FlowEnum fp)
176 ///Runs the first phase of the preflow algorithm.
178 ///The preflow algorithm consists of two phases, this method runs the
179 ///first phase. After the first phase the maximum flow value and a
180 ///minimum value cut can already be computed, though a maximum flow
181 ///is not yet obtained. So after calling this method \ref flowValue
182 ///and \ref actMinCut gives proper results.
183 ///\warning \ref minCut, \ref minMinCut and \ref maxMinCut do not
184 ///give minimum value cuts unless calling \ref phase2.
187 int heur0=(int)(H0*n); //time while running 'bound decrease'
188 int heur1=(int)(H1*n); //time while running 'highest label'
189 int heur=heur1; //starting time interval (#of relabels)
193 //It is 0 in case 'bound decrease' and 1 in case 'highest label'
196 //Needed for 'bound decrease', true means no active
197 //nodes are above bound b.
199 int k=n-2; //bound on the highest level under n containing a node
200 int b=k; //bound on the highest level under n of an active node
202 VecNode first(n, INVALID);
203 NNMap next(*g, INVALID);
205 NNMap left(*g, INVALID);
206 NNMap right(*g, INVALID);
207 VecNode level_list(n,INVALID);
208 //List of the nodes in level i<n, set to n.
210 preflowPreproc(first, next, level_list, left, right);
212 //Push/relabel on the highest level active nodes.
215 if ( !what_heur && !end && k > 0 ) {
221 if ( first[b]==INVALID ) --b;
226 int newlevel=push(w, next, first);
227 if ( excess[w] > 0 ) relabel(w, newlevel, first, next, level_list,
228 left, right, b, k, what_heur);
231 if ( numrelabel >= heur ) {
246 status=AFTER_PREFLOW_PHASE_1;
251 // list 'level_list' on the nodes on level i implemented by hand
252 // stack 'active' on the active nodes on level i
253 // runs heuristic 'highest label' for H1*n relabels
254 // runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
255 // Parameters H0 and H1 are initialized to 20 and 1.
258 ///Runs the second phase of the preflow algorithm.
260 ///The preflow algorithm consists of two phases, this method runs
261 ///the second phase. After calling \ref phase1 and then
262 ///\ref phase2 the methods \ref flowValue, \ref minCut,
263 ///\ref minMinCut and \ref maxMinCut give proper results.
264 ///\pre \ref phase1 must be called before.
268 int k=n-2; //bound on the highest level under n containing a node
269 int b=k; //bound on the highest level under n of an active node
272 VecNode first(n, INVALID);
273 NNMap next(*g, INVALID);
275 std::queue<Node> bfs_queue;
278 while ( !bfs_queue.empty() ) {
280 Node v=bfs_queue.front();
284 for(InEdgeIt e(*g,v); e!=INVALID; ++e) {
285 if ( (*capacity)[e] <= (*flow)[e] ) continue;
287 if ( level[u] >= n ) {
290 if ( excess[u] > 0 ) {
291 next.set(u,first[l]);
297 for(OutEdgeIt e(*g,v); e!=INVALID; ++e) {
298 if ( 0 >= (*flow)[e] ) continue;
300 if ( level[u] >= n ) {
303 if ( excess[u] > 0 ) {
304 next.set(u,first[l]);
315 if ( first[b]==INVALID ) --b;
319 int newlevel=push(w,next, first);
322 if ( excess[w] > 0 ) {
323 level.set(w,++newlevel);
324 next.set(w,first[newlevel]);
331 status=AFTER_PREFLOW_PHASE_2;
334 /// Returns the value of the maximum flow.
336 /// Returns the value of the maximum flow by returning the excess
337 /// of the target node \ref t. This value equals to the value of
338 /// the maximum flow already after running \ref phase1.
339 Num flowValue() const {
344 ///Returns a minimum value cut.
346 ///Sets \c M to the characteristic vector of a minimum value
347 ///cut. This method can be called both after running \ref
348 ///phase1 and \ref phase2. It is much faster after
349 ///\ref phase1. \pre M should be a bool-valued node-map. \pre
350 ///If \ref mincut is called after \ref phase2 then M should
351 ///be initialized to false.
352 template<typename _CutMap>
353 void minCut(_CutMap& M) const {
355 case AFTER_PREFLOW_PHASE_1:
356 for(NodeIt v(*g); v!=INVALID; ++v) {
364 case AFTER_PREFLOW_PHASE_2:
372 ///Returns the inclusionwise minimum of the minimum value cuts.
374 ///Sets \c M to the characteristic vector of the minimum value cut
375 ///which is inclusionwise minimum. It is computed by processing a
376 ///bfs from the source node \c s in the residual graph. \pre M
377 ///should be a node map of bools initialized to false. \pre \ref
378 ///phase2 should already be run.
379 template<typename _CutMap>
380 void minMinCut(_CutMap& M) const {
382 std::queue<Node> queue;
386 while (!queue.empty()) {
387 Node w=queue.front();
390 for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
392 if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
398 for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
400 if (!M[v] && (*flow)[e] > 0 ) {
408 ///Returns the inclusionwise maximum of the minimum value cuts.
410 ///Sets \c M to the characteristic vector of the minimum value cut
411 ///which is inclusionwise maximum. It is computed by processing a
412 ///backward bfs from the target node \c t in the residual graph.
413 ///\pre \ref phase2() or preflow() should already be run.
414 template<typename _CutMap>
415 void maxMinCut(_CutMap& M) const {
417 for(NodeIt v(*g) ; v!=INVALID; ++v) M.set(v, true);
419 std::queue<Node> queue;
424 while (!queue.empty()) {
425 Node w=queue.front();
428 for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
430 if (M[v] && (*flow)[e] < (*capacity)[e] ) {
436 for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
438 if (M[v] && (*flow)[e] > 0 ) {
446 ///Sets the source node to \c _s.
448 ///Sets the source node to \c _s.
450 void setSource(Node _s) {
452 if ( flow_prop != ZERO_FLOW ) flow_prop=NO_FLOW;
453 status=AFTER_NOTHING;
456 ///Sets the target node to \c _t.
458 ///Sets the target node to \c _t.
460 void setTarget(Node _t) {
462 if ( flow_prop == GEN_FLOW ) flow_prop=PRE_FLOW;
463 status=AFTER_NOTHING;
466 /// Sets the edge map of the capacities to _cap.
468 /// Sets the edge map of the capacities to _cap.
470 void setCap(const CapMap& _cap) {
472 status=AFTER_NOTHING;
475 /// Sets the edge map of the flows to _flow.
477 /// Sets the edge map of the flows to _flow.
479 void setFlow(FlowMap& _flow) {
482 status=AFTER_NOTHING;
488 int push(Node w, NNMap& next, VecNode& first) {
492 int newlevel=n; //bound on the next level of w
494 for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
495 if ( (*flow)[e] >= (*capacity)[e] ) continue;
498 if( lev > level[v] ) { //Push is allowed now
500 if ( excess[v]<=0 && v!=t && v!=s ) {
501 next.set(v,first[level[v]]);
505 Num cap=(*capacity)[e];
509 if ( remcap >= exc ) { //A nonsaturating push.
511 flow->set(e, flo+exc);
512 excess.set(v, excess[v]+exc);
516 } else { //A saturating push.
518 excess.set(v, excess[v]+remcap);
521 } else if ( newlevel > level[v] ) newlevel = level[v];
525 for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
527 if( (*flow)[e] <= 0 ) continue;
530 if( lev > level[v] ) { //Push is allowed now
532 if ( excess[v]<=0 && v!=t && v!=s ) {
533 next.set(v,first[level[v]]);
539 if ( flo >= exc ) { //A nonsaturating push.
541 flow->set(e, flo-exc);
542 excess.set(v, excess[v]+exc);
545 } else { //A saturating push.
547 excess.set(v, excess[v]+flo);
551 } else if ( newlevel > level[v] ) newlevel = level[v];
554 } // if w still has excess after the out edge for cycle
563 void preflowPreproc(VecNode& first, NNMap& next,
564 VecNode& level_list, NNMap& left, NNMap& right)
566 for(NodeIt v(*g); v!=INVALID; ++v) level.set(v,n);
567 std::queue<Node> bfs_queue;
569 if ( flow_prop == GEN_FLOW || flow_prop == PRE_FLOW ) {
570 //Reverse_bfs from t in the residual graph,
571 //to find the starting level.
575 while ( !bfs_queue.empty() ) {
577 Node v=bfs_queue.front();
581 for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) {
582 if ( (*capacity)[e] <= (*flow)[e] ) continue;
584 if ( level[w] == n && w != s ) {
586 Node z=level_list[l];
587 if ( z!=INVALID ) left.set(z,w);
594 for(OutEdgeIt e(*g,v) ; e!=INVALID; ++e) {
595 if ( 0 >= (*flow)[e] ) continue;
597 if ( level[w] == n && w != s ) {
599 Node z=level_list[l];
600 if ( z!=INVALID ) left.set(z,w);
612 for(EdgeIt e(*g); e!=INVALID; ++e) flow->set(e,0);
614 for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0);
616 //Reverse_bfs from t, to find the starting level.
620 while ( !bfs_queue.empty() ) {
622 Node v=bfs_queue.front();
626 for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) {
628 if ( level[w] == n && w != s ) {
630 Node z=level_list[l];
631 if ( z!=INVALID ) left.set(z,w);
640 for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) {
641 Num c=(*capacity)[e];
642 if ( c <= 0 ) continue;
644 if ( level[w] < n ) {
645 if ( excess[w] <= 0 && w!=t ) { //putting into the stack
646 next.set(w,first[level[w]]);
650 excess.set(w, excess[w]+c);
656 for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0);
659 for(InEdgeIt e(*g,t) ; e!=INVALID; ++e) exc+=(*flow)[e];
660 for(OutEdgeIt e(*g,t) ; e!=INVALID; ++e) exc-=(*flow)[e];
665 for(OutEdgeIt e(*g,s); e!=INVALID; ++e) {
666 Num rem=(*capacity)[e]-(*flow)[e];
667 if ( rem <= 0 ) continue;
669 if ( level[w] < n ) {
670 if ( excess[w] <= 0 && w!=t ) { //putting into the stack
671 next.set(w,first[level[w]]);
674 flow->set(e, (*capacity)[e]);
675 excess.set(w, excess[w]+rem);
679 for(InEdgeIt e(*g,s); e!=INVALID; ++e) {
680 if ( (*flow)[e] <= 0 ) continue;
682 if ( level[w] < n ) {
683 if ( excess[w] <= 0 && w!=t ) {
684 next.set(w,first[level[w]]);
687 excess.set(w, excess[w]+(*flow)[e]);
695 for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) {
696 Num rem=(*capacity)[e]-(*flow)[e];
697 if ( rem <= 0 ) continue;
699 if ( level[w] < n ) flow->set(e, (*capacity)[e]);
702 for(InEdgeIt e(*g,s) ; e!=INVALID; ++e) {
703 if ( (*flow)[e] <= 0 ) continue;
705 if ( level[w] < n ) flow->set(e, 0);
708 //computing the excess
709 for(NodeIt w(*g); w!=INVALID; ++w) {
711 for(InEdgeIt e(*g,w); e!=INVALID; ++e) exc+=(*flow)[e];
712 for(OutEdgeIt e(*g,w); e!=INVALID; ++e) exc-=(*flow)[e];
715 //putting the active nodes into the stack
717 if ( exc > 0 && lev < n && Node(w) != t ) {
718 next.set(w,first[lev]);
727 void relabel(Node w, int newlevel, VecNode& first, NNMap& next,
728 VecNode& level_list, NNMap& left,
729 NNMap& right, int& b, int& k, bool what_heur )
734 Node right_n=right[w];
738 if ( right_n!=INVALID ) {
739 if ( left_n!=INVALID ) {
740 right.set(left_n, right_n);
741 left.set(right_n, left_n);
743 level_list[lev]=right_n;
744 left.set(right_n, INVALID);
747 if ( left_n!=INVALID ) {
748 right.set(left_n, INVALID);
750 level_list[lev]=INVALID;
755 if ( level_list[lev]==INVALID ) {
758 for (int i=lev; i!=k ; ) {
759 Node v=level_list[++i];
760 while ( v!=INVALID ) {
764 level_list[i]=INVALID;
765 if ( !what_heur ) first[i]=INVALID;
775 if ( newlevel == n ) level.set(w,n);
777 level.set(w,++newlevel);
778 next.set(w,first[newlevel]);
780 if ( what_heur ) b=newlevel;
781 if ( k < newlevel ) ++k; //now k=newlevel
782 Node z=level_list[newlevel];
783 if ( z!=INVALID ) left.set(z,w);
786 level_list[newlevel]=w;
794 #endif //HUGO_PREFLOW_H