Minor changes.
2 * src/lemon/preflow.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_PREFLOW_H
18 #define LEMON_PREFLOW_H
23 #include <lemon/invalid.h>
24 #include <lemon/maps.h>
25 #include <lemon/graph_utils.h>
29 /// Implementation of the preflow algorithm.
33 /// \addtogroup flowalgs
36 ///%Preflow algorithms class.
38 ///This class provides an implementation of the \e preflow \e
39 ///algorithm producing a flow of maximum value in a directed
40 ///graph. The preflow algorithms are the fastest max flow algorithms
41 ///up to now. The \e source node, the \e target node, the \e
42 ///capacity of the edges and the \e starting \e flow value of the
43 ///edges should be passed to the algorithm through the
44 ///constructor. It is possible to change these quantities using the
45 ///functions \ref setSource, \ref setTarget, \ref setCap and \ref
48 ///After running \ref lemon::Preflow::phase1() "phase1()"
49 ///or \ref lemon::Preflow::run() "run()", the maximal flow
50 ///value can be obtained by calling \ref flowValue(). The minimum
51 ///value cut can be written into a <tt>bool</tt> node map by
52 ///calling \ref minCut(). (\ref minMinCut() and \ref maxMinCut() writes
53 ///the inclusionwise minimum and maximum of the minimum value cuts,
56 ///\param Graph The directed graph type the algorithm runs on.
57 ///\param Num The number type of the capacities and the flow values.
58 ///\param CapMap The capacity map type.
59 ///\param FlowMap The flow map type.
61 ///\author Jacint Szabo
62 template <typename Graph, typename Num,
63 typename CapMap=typename Graph::template EdgeMap<Num>,
64 typename FlowMap=typename Graph::template EdgeMap<Num> >
67 typedef typename Graph::Node Node;
68 typedef typename Graph::NodeIt NodeIt;
69 typedef typename Graph::EdgeIt EdgeIt;
70 typedef typename Graph::OutEdgeIt OutEdgeIt;
71 typedef typename Graph::InEdgeIt InEdgeIt;
73 typedef typename Graph::template NodeMap<Node> NNMap;
74 typedef typename std::vector<Node> VecNode;
79 const CapMap* capacity;
81 int n; //the number of nodes of G
83 typename Graph::template NodeMap<int> level;
84 typename Graph::template NodeMap<Num> excess;
86 // constants used for heuristics
87 static const int H0=20;
88 static const int H1=1;
92 ///Indicates the property of the starting flow map.
94 ///Indicates the property of the starting flow map. The meanings are as follows:
95 ///- \c ZERO_FLOW: constant zero flow
96 ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
97 ///the sum of the out-flows in every node except the \e source and
99 ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at
100 ///least the sum of the out-flows in every node except the \e source.
101 ///- \c NO_FLOW: indicates an unspecified edge map. \c flow will be
102 ///set to the constant zero flow in the beginning of
103 ///the algorithm in this case.
112 ///Indicates the state of the preflow algorithm.
114 ///Indicates the state of the preflow algorithm. The meanings are as follows:
115 ///- \c AFTER_NOTHING: before running the algorithm or at an unspecified state.
116 ///- \c AFTER_PREFLOW_PHASE_1: right after running \c phase1
117 ///- \c AFTER_PREFLOW_PHASE_2: after running \ref phase2()
121 AFTER_PREFLOW_PHASE_1,
122 AFTER_PREFLOW_PHASE_2
127 StatusEnum status; // Do not needle this flag only if necessary.
130 ///The constructor of the class.
132 ///The constructor of the class.
133 ///\param _G The directed graph the algorithm runs on.
134 ///\param _s The source node.
135 ///\param _t The target node.
136 ///\param _capacity The capacity of the edges.
137 ///\param _flow The flow of the edges.
138 ///Except the graph, all of these parameters can be reset by
139 ///calling \ref setSource, \ref setTarget, \ref setCap and \ref
141 Preflow(const Graph& _G, Node _s, Node _t,
142 const CapMap& _capacity, FlowMap& _flow) :
143 g(&_G), s(_s), t(_t), capacity(&_capacity),
144 flow(&_flow), n(countNodes(_G)), level(_G), excess(_G,0),
145 flow_prop(NO_FLOW), status(AFTER_NOTHING) { }
149 ///Runs the preflow algorithm.
151 ///Runs the preflow algorithm.
158 ///Runs the preflow algorithm.
160 ///Runs the preflow algorithm.
161 ///\pre The starting flow map must be
162 /// - a constant zero flow if \c fp is \c ZERO_FLOW,
163 /// - an arbitrary flow if \c fp is \c GEN_FLOW,
164 /// - an arbitrary preflow if \c fp is \c PRE_FLOW,
165 /// - any map if \c fp is NO_FLOW.
166 ///If the starting flow map is a flow or a preflow then
167 ///the algorithm terminates faster.
168 void run(FlowEnum fp) {
173 ///Runs the first phase of the preflow algorithm.
175 ///The preflow algorithm consists of two phases, this method runs
176 ///the first phase. After the first phase the maximum flow value
177 ///and a minimum value cut can already be computed, though a
178 ///maximum flow is not yet obtained. So after calling this method
179 ///\ref flowValue returns the value of a maximum flow and \ref
180 ///minCut returns a minimum cut.
181 ///\warning \ref minMinCut and \ref maxMinCut do not give minimum
182 ///value cuts unless calling \ref phase2.
183 ///\pre The starting flow must be
184 ///- a constant zero flow if \c fp is \c ZERO_FLOW,
185 ///- an arbitary flow if \c fp is \c GEN_FLOW,
186 ///- an arbitary preflow if \c fp is \c PRE_FLOW,
187 ///- any map if \c fp is NO_FLOW.
188 void phase1(FlowEnum fp)
195 ///Runs the first phase of the preflow algorithm.
197 ///The preflow algorithm consists of two phases, this method runs
198 ///the first phase. After the first phase the maximum flow value
199 ///and a minimum value cut can already be computed, though a
200 ///maximum flow is not yet obtained. So after calling this method
201 ///\ref flowValue returns the value of a maximum flow and \ref
202 ///minCut returns a minimum cut.
203 ///\warning \ref minCut(), \ref minMinCut() and \ref maxMinCut() do not
204 ///give minimum value cuts unless calling \ref phase2().
207 int heur0=(int)(H0*n); //time while running 'bound decrease'
208 int heur1=(int)(H1*n); //time while running 'highest label'
209 int heur=heur1; //starting time interval (#of relabels)
213 //It is 0 in case 'bound decrease' and 1 in case 'highest label'
216 //Needed for 'bound decrease', true means no active
217 //nodes are above bound b.
219 int k=n-2; //bound on the highest level under n containing a node
220 int b=k; //bound on the highest level under n of an active node
222 VecNode first(n, INVALID);
223 NNMap next(*g, INVALID);
225 NNMap left(*g, INVALID);
226 NNMap right(*g, INVALID);
227 VecNode level_list(n,INVALID);
228 //List of the nodes in level i<n, set to n.
230 preflowPreproc(first, next, level_list, left, right);
232 //Push/relabel on the highest level active nodes.
235 if ( !what_heur && !end && k > 0 ) {
241 if ( first[b]==INVALID ) --b;
246 int newlevel=push(w, next, first);
247 if ( excess[w] > 0 ) relabel(w, newlevel, first, next, level_list,
248 left, right, b, k, what_heur);
251 if ( numrelabel >= heur ) {
266 status=AFTER_PREFLOW_PHASE_1;
271 // list 'level_list' on the nodes on level i implemented by hand
272 // stack 'active' on the active nodes on level i
273 // runs heuristic 'highest label' for H1*n relabels
274 // runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
275 // Parameters H0 and H1 are initialized to 20 and 1.
278 ///Runs the second phase of the preflow algorithm.
280 ///The preflow algorithm consists of two phases, this method runs
281 ///the second phase. After calling \ref phase1 and then \ref
282 ///phase2, \ref flow contains a maximum flow, \ref flowValue
283 ///returns the value of a maximum flow, \ref minCut returns a
284 ///minimum cut, while the methods \ref minMinCut and \ref
285 ///maxMinCut return the inclusionwise minimum and maximum cuts of
286 ///minimum value, resp. \pre \ref phase1 must be called before.
290 int k=n-2; //bound on the highest level under n containing a node
291 int b=k; //bound on the highest level under n of an active node
294 VecNode first(n, INVALID);
295 NNMap next(*g, INVALID);
297 std::queue<Node> bfs_queue;
300 while ( !bfs_queue.empty() ) {
302 Node v=bfs_queue.front();
306 for(InEdgeIt e(*g,v); e!=INVALID; ++e) {
307 if ( (*capacity)[e] <= (*flow)[e] ) continue;
309 if ( level[u] >= n ) {
312 if ( excess[u] > 0 ) {
313 next.set(u,first[l]);
319 for(OutEdgeIt e(*g,v); e!=INVALID; ++e) {
320 if ( 0 >= (*flow)[e] ) continue;
322 if ( level[u] >= n ) {
325 if ( excess[u] > 0 ) {
326 next.set(u,first[l]);
337 if ( first[b]==INVALID ) --b;
341 int newlevel=push(w,next, first);
344 if ( excess[w] > 0 ) {
345 level.set(w,++newlevel);
346 next.set(w,first[newlevel]);
353 status=AFTER_PREFLOW_PHASE_2;
356 /// Returns the value of the maximum flow.
358 /// Returns the value of the maximum flow by returning the excess
359 /// of the target node \c t. This value equals to the value of
360 /// the maximum flow already after running \ref phase1.
361 Num flowValue() const {
366 ///Returns a minimum value cut.
368 ///Sets \c M to the characteristic vector of a minimum value
369 ///cut. This method can be called both after running \ref
370 ///phase1 and \ref phase2. It is much faster after
371 ///\ref phase1. \pre M should be a bool-valued node-map. \pre
372 ///If \ref minCut() is called after \ref phase2() then M should
373 ///be initialized to false.
374 template<typename _CutMap>
375 void minCut(_CutMap& M) const {
377 case AFTER_PREFLOW_PHASE_1:
378 for(NodeIt v(*g); v!=INVALID; ++v) {
386 case AFTER_PREFLOW_PHASE_2:
394 ///Returns the inclusionwise minimum of the minimum value cuts.
396 ///Sets \c M to the characteristic vector of the minimum value cut
397 ///which is inclusionwise minimum. It is computed by processing a
398 ///bfs from the source node \c s in the residual graph. \pre M
399 ///should be a node map of bools initialized to false. \pre \ref
400 ///phase2 should already be run.
401 template<typename _CutMap>
402 void minMinCut(_CutMap& M) const {
404 std::queue<Node> queue;
408 while (!queue.empty()) {
409 Node w=queue.front();
412 for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
414 if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
420 for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
422 if (!M[v] && (*flow)[e] > 0 ) {
430 ///Returns the inclusionwise maximum of the minimum value cuts.
432 ///Sets \c M to the characteristic vector of the minimum value cut
433 ///which is inclusionwise maximum. It is computed by processing a
434 ///backward bfs from the target node \c t in the residual graph.
435 ///\pre \ref phase2() or run() should already be run.
436 template<typename _CutMap>
437 void maxMinCut(_CutMap& M) const {
439 for(NodeIt v(*g) ; v!=INVALID; ++v) M.set(v, true);
441 std::queue<Node> queue;
446 while (!queue.empty()) {
447 Node w=queue.front();
450 for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
452 if (M[v] && (*flow)[e] < (*capacity)[e] ) {
458 for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
460 if (M[v] && (*flow)[e] > 0 ) {
468 ///Sets the source node to \c _s.
470 ///Sets the source node to \c _s.
472 void setSource(Node _s) {
474 if ( flow_prop != ZERO_FLOW ) flow_prop=NO_FLOW;
475 status=AFTER_NOTHING;
478 ///Sets the target node to \c _t.
480 ///Sets the target node to \c _t.
482 void setTarget(Node _t) {
484 if ( flow_prop == GEN_FLOW ) flow_prop=PRE_FLOW;
485 status=AFTER_NOTHING;
488 /// Sets the edge map of the capacities to _cap.
490 /// Sets the edge map of the capacities to _cap.
492 void setCap(const CapMap& _cap) {
494 status=AFTER_NOTHING;
497 /// Sets the edge map of the flows to _flow.
499 /// Sets the edge map of the flows to _flow.
501 void setFlow(FlowMap& _flow) {
504 status=AFTER_NOTHING;
510 int push(Node w, NNMap& next, VecNode& first) {
514 int newlevel=n; //bound on the next level of w
516 for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
517 if ( (*flow)[e] >= (*capacity)[e] ) continue;
520 if( lev > level[v] ) { //Push is allowed now
522 if ( excess[v]<=0 && v!=t && v!=s ) {
523 next.set(v,first[level[v]]);
527 Num cap=(*capacity)[e];
531 if ( remcap >= exc ) { //A nonsaturating push.
533 flow->set(e, flo+exc);
534 excess.set(v, excess[v]+exc);
538 } else { //A saturating push.
540 excess.set(v, excess[v]+remcap);
543 } else if ( newlevel > level[v] ) newlevel = level[v];
547 for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
549 if( (*flow)[e] <= 0 ) continue;
552 if( lev > level[v] ) { //Push is allowed now
554 if ( excess[v]<=0 && v!=t && v!=s ) {
555 next.set(v,first[level[v]]);
561 if ( flo >= exc ) { //A nonsaturating push.
563 flow->set(e, flo-exc);
564 excess.set(v, excess[v]+exc);
567 } else { //A saturating push.
569 excess.set(v, excess[v]+flo);
573 } else if ( newlevel > level[v] ) newlevel = level[v];
576 } // if w still has excess after the out edge for cycle
585 void preflowPreproc(VecNode& first, NNMap& next,
586 VecNode& level_list, NNMap& left, NNMap& right)
588 for(NodeIt v(*g); v!=INVALID; ++v) level.set(v,n);
589 std::queue<Node> bfs_queue;
591 if ( flow_prop == GEN_FLOW || flow_prop == PRE_FLOW ) {
592 //Reverse_bfs from t in the residual graph,
593 //to find the starting level.
597 while ( !bfs_queue.empty() ) {
599 Node v=bfs_queue.front();
603 for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) {
604 if ( (*capacity)[e] <= (*flow)[e] ) continue;
606 if ( level[w] == n && w != s ) {
608 Node z=level_list[l];
609 if ( z!=INVALID ) left.set(z,w);
616 for(OutEdgeIt e(*g,v) ; e!=INVALID; ++e) {
617 if ( 0 >= (*flow)[e] ) continue;
619 if ( level[w] == n && w != s ) {
621 Node z=level_list[l];
622 if ( z!=INVALID ) left.set(z,w);
634 for(EdgeIt e(*g); e!=INVALID; ++e) flow->set(e,0);
636 for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0);
638 //Reverse_bfs from t, to find the starting level.
642 while ( !bfs_queue.empty() ) {
644 Node v=bfs_queue.front();
648 for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) {
650 if ( level[w] == n && w != s ) {
652 Node z=level_list[l];
653 if ( z!=INVALID ) left.set(z,w);
662 for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) {
663 Num c=(*capacity)[e];
664 if ( c <= 0 ) continue;
666 if ( level[w] < n ) {
667 if ( excess[w] <= 0 && w!=t ) { //putting into the stack
668 next.set(w,first[level[w]]);
672 excess.set(w, excess[w]+c);
678 for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0);
681 for(InEdgeIt e(*g,t) ; e!=INVALID; ++e) exc+=(*flow)[e];
682 for(OutEdgeIt e(*g,t) ; e!=INVALID; ++e) exc-=(*flow)[e];
687 for(OutEdgeIt e(*g,s); e!=INVALID; ++e) {
688 Num rem=(*capacity)[e]-(*flow)[e];
689 if ( rem <= 0 ) continue;
691 if ( level[w] < n ) {
692 if ( excess[w] <= 0 && w!=t ) { //putting into the stack
693 next.set(w,first[level[w]]);
696 flow->set(e, (*capacity)[e]);
697 excess.set(w, excess[w]+rem);
701 for(InEdgeIt e(*g,s); e!=INVALID; ++e) {
702 if ( (*flow)[e] <= 0 ) continue;
704 if ( level[w] < n ) {
705 if ( excess[w] <= 0 && w!=t ) {
706 next.set(w,first[level[w]]);
709 excess.set(w, excess[w]+(*flow)[e]);
717 for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) {
718 Num rem=(*capacity)[e]-(*flow)[e];
719 if ( rem <= 0 ) continue;
721 if ( level[w] < n ) flow->set(e, (*capacity)[e]);
724 for(InEdgeIt e(*g,s) ; e!=INVALID; ++e) {
725 if ( (*flow)[e] <= 0 ) continue;
727 if ( level[w] < n ) flow->set(e, 0);
730 //computing the excess
731 for(NodeIt w(*g); w!=INVALID; ++w) {
733 for(InEdgeIt e(*g,w); e!=INVALID; ++e) exc+=(*flow)[e];
734 for(OutEdgeIt e(*g,w); e!=INVALID; ++e) exc-=(*flow)[e];
737 //putting the active nodes into the stack
739 if ( exc > 0 && lev < n && Node(w) != t ) {
740 next.set(w,first[lev]);
749 void relabel(Node w, int newlevel, VecNode& first, NNMap& next,
750 VecNode& level_list, NNMap& left,
751 NNMap& right, int& b, int& k, bool what_heur )
756 Node right_n=right[w];
760 if ( right_n!=INVALID ) {
761 if ( left_n!=INVALID ) {
762 right.set(left_n, right_n);
763 left.set(right_n, left_n);
765 level_list[lev]=right_n;
766 left.set(right_n, INVALID);
769 if ( left_n!=INVALID ) {
770 right.set(left_n, INVALID);
772 level_list[lev]=INVALID;
777 if ( level_list[lev]==INVALID ) {
780 for (int i=lev; i!=k ; ) {
781 Node v=level_list[++i];
782 while ( v!=INVALID ) {
786 level_list[i]=INVALID;
787 if ( !what_heur ) first[i]=INVALID;
797 if ( newlevel == n ) level.set(w,n);
799 level.set(w,++newlevel);
800 next.set(w,first[newlevel]);
802 if ( what_heur ) b=newlevel;
803 if ( k < newlevel ) ++k; //now k=newlevel
804 Node z=level_list[newlevel];
805 if ( z!=INVALID ) left.set(z,w);
808 level_list[newlevel]=w;
816 #endif //LEMON_PREFLOW_H