Naming and coding style fixes and various other 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>
28 /// Implementation of the preflow algorithm.
32 /// \addtogroup flowalgs
35 ///%Preflow algorithms class.
37 ///This class provides an implementation of the \e preflow \e
38 ///algorithm producing a flow of maximum value in a directed
39 ///graph. The preflow algorithms are the fastest max flow algorithms
40 ///up to now. The \e source node, the \e target node, the \e
41 ///capacity of the edges and the \e starting \e flow value of the
42 ///edges should be passed to the algorithm through the
43 ///constructor. It is possible to change these quantities using the
44 ///functions \ref setSource, \ref setTarget, \ref setCap and \ref
47 ///After running \ref lemon::Preflow::phase1() "phase1()"
48 ///or \ref lemon::Preflow::run() "run()", the maximal flow
49 ///value can be obtained by calling \ref flowValue(). The minimum
50 ///value cut can be written into a <tt>bool</tt> node map by
51 ///calling \ref minCut(). (\ref minMinCut() and \ref maxMinCut() writes
52 ///the inclusionwise minimum and maximum of the minimum value cuts,
55 ///\param Graph The directed graph type the algorithm runs on.
56 ///\param Num The number type of the capacities and the flow values.
57 ///\param CapMap The capacity map type.
58 ///\param FlowMap The flow map type.
60 ///\author Jacint Szabo
61 template <typename Graph, typename Num,
62 typename CapMap=typename Graph::template EdgeMap<Num>,
63 typename FlowMap=typename Graph::template EdgeMap<Num> >
66 typedef typename Graph::Node Node;
67 typedef typename Graph::NodeIt NodeIt;
68 typedef typename Graph::EdgeIt EdgeIt;
69 typedef typename Graph::OutEdgeIt OutEdgeIt;
70 typedef typename Graph::InEdgeIt InEdgeIt;
72 typedef typename Graph::template NodeMap<Node> NNMap;
73 typedef typename std::vector<Node> VecNode;
78 const CapMap* capacity;
80 int n; //the number of nodes of G
82 typename Graph::template NodeMap<int> level;
83 typename Graph::template NodeMap<Num> excess;
85 // constants used for heuristics
86 static const int H0=20;
87 static const int H1=1;
91 ///Indicates the property of the starting flow map.
93 ///Indicates the property of the starting flow map. The meanings are as follows:
94 ///- \c ZERO_FLOW: constant zero flow
95 ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
96 ///the sum of the out-flows in every node except the \e source and
98 ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at
99 ///least the sum of the out-flows in every node except the \e source.
100 ///- \c NO_FLOW: indicates an unspecified edge map. \c flow will be
101 ///set to the constant zero flow in the beginning of
102 ///the algorithm in this case.
111 ///Indicates the state of the preflow algorithm.
113 ///Indicates the state of the preflow algorithm. The meanings are as follows:
114 ///- \c AFTER_NOTHING: before running the algorithm or at an unspecified state.
115 ///- \c AFTER_PREFLOW_PHASE_1: right after running \c phase1
116 ///- \c AFTER_PREFLOW_PHASE_2: after running \ref phase2()
120 AFTER_PREFLOW_PHASE_1,
121 AFTER_PREFLOW_PHASE_2
126 StatusEnum status; // Do not needle this flag only if necessary.
129 ///The constructor of the class.
131 ///The constructor of the class.
132 ///\param _G The directed graph the algorithm runs on.
133 ///\param _s The source node.
134 ///\param _t The target node.
135 ///\param _capacity The capacity of the edges.
136 ///\param _flow The flow of the edges.
137 ///Except the graph, all of these parameters can be reset by
138 ///calling \ref setSource, \ref setTarget, \ref setCap and \ref
140 Preflow(const Graph& _G, Node _s, Node _t,
141 const CapMap& _capacity, FlowMap& _flow) :
142 g(&_G), s(_s), t(_t), capacity(&_capacity),
143 flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0),
144 flow_prop(NO_FLOW), status(AFTER_NOTHING) { }
148 ///Runs the preflow algorithm.
150 ///Runs the preflow algorithm.
157 ///Runs the preflow algorithm.
159 ///Runs the preflow algorithm.
160 ///\pre The starting flow map must be
161 /// - a constant zero flow if \c fp is \c ZERO_FLOW,
162 /// - an arbitrary flow if \c fp is \c GEN_FLOW,
163 /// - an arbitrary preflow if \c fp is \c PRE_FLOW,
164 /// - any map if \c fp is NO_FLOW.
165 ///If the starting flow map is a flow or a preflow then
166 ///the algorithm terminates faster.
167 void run(FlowEnum fp) {
172 ///Runs the first phase of the preflow algorithm.
174 ///The preflow algorithm consists of two phases, this method runs
175 ///the first phase. After the first phase the maximum flow value
176 ///and a minimum value cut can already be computed, though a
177 ///maximum flow is not yet obtained. So after calling this method
178 ///\ref flowValue returns the value of a maximum flow and \ref
179 ///minCut returns a minimum cut.
180 ///\warning \ref minMinCut and \ref maxMinCut do not give minimum
181 ///value cuts unless calling \ref phase2.
182 ///\pre The starting flow must be
183 ///- a constant zero flow if \c fp is \c ZERO_FLOW,
184 ///- an arbitary flow if \c fp is \c GEN_FLOW,
185 ///- an arbitary preflow if \c fp is \c PRE_FLOW,
186 ///- any map if \c fp is NO_FLOW.
187 void phase1(FlowEnum fp)
194 ///Runs the first phase of the preflow algorithm.
196 ///The preflow algorithm consists of two phases, this method runs
197 ///the first phase. After the first phase the maximum flow value
198 ///and a minimum value cut can already be computed, though a
199 ///maximum flow is not yet obtained. So after calling this method
200 ///\ref flowValue returns the value of a maximum flow and \ref
201 ///minCut returns a minimum cut.
202 ///\warning \ref minCut(), \ref minMinCut() and \ref maxMinCut() do not
203 ///give minimum value cuts unless calling \ref phase2().
206 int heur0=(int)(H0*n); //time while running 'bound decrease'
207 int heur1=(int)(H1*n); //time while running 'highest label'
208 int heur=heur1; //starting time interval (#of relabels)
212 //It is 0 in case 'bound decrease' and 1 in case 'highest label'
215 //Needed for 'bound decrease', true means no active
216 //nodes are above bound b.
218 int k=n-2; //bound on the highest level under n containing a node
219 int b=k; //bound on the highest level under n of an active node
221 VecNode first(n, INVALID);
222 NNMap next(*g, INVALID);
224 NNMap left(*g, INVALID);
225 NNMap right(*g, INVALID);
226 VecNode level_list(n,INVALID);
227 //List of the nodes in level i<n, set to n.
229 preflowPreproc(first, next, level_list, left, right);
231 //Push/relabel on the highest level active nodes.
234 if ( !what_heur && !end && k > 0 ) {
240 if ( first[b]==INVALID ) --b;
245 int newlevel=push(w, next, first);
246 if ( excess[w] > 0 ) relabel(w, newlevel, first, next, level_list,
247 left, right, b, k, what_heur);
250 if ( numrelabel >= heur ) {
265 status=AFTER_PREFLOW_PHASE_1;
270 // list 'level_list' on the nodes on level i implemented by hand
271 // stack 'active' on the active nodes on level i
272 // runs heuristic 'highest label' for H1*n relabels
273 // runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
274 // Parameters H0 and H1 are initialized to 20 and 1.
277 ///Runs the second phase of the preflow algorithm.
279 ///The preflow algorithm consists of two phases, this method runs
280 ///the second phase. After calling \ref phase1 and then \ref
281 ///phase2, \ref flow contains a maximum flow, \ref flowValue
282 ///returns the value of a maximum flow, \ref minCut returns a
283 ///minimum cut, while the methods \ref minMinCut and \ref
284 ///maxMinCut return the inclusionwise minimum and maximum cuts of
285 ///minimum value, resp. \pre \ref phase1 must be called before.
289 int k=n-2; //bound on the highest level under n containing a node
290 int b=k; //bound on the highest level under n of an active node
293 VecNode first(n, INVALID);
294 NNMap next(*g, INVALID);
296 std::queue<Node> bfs_queue;
299 while ( !bfs_queue.empty() ) {
301 Node v=bfs_queue.front();
305 for(InEdgeIt e(*g,v); e!=INVALID; ++e) {
306 if ( (*capacity)[e] <= (*flow)[e] ) continue;
308 if ( level[u] >= n ) {
311 if ( excess[u] > 0 ) {
312 next.set(u,first[l]);
318 for(OutEdgeIt e(*g,v); e!=INVALID; ++e) {
319 if ( 0 >= (*flow)[e] ) continue;
321 if ( level[u] >= n ) {
324 if ( excess[u] > 0 ) {
325 next.set(u,first[l]);
336 if ( first[b]==INVALID ) --b;
340 int newlevel=push(w,next, first);
343 if ( excess[w] > 0 ) {
344 level.set(w,++newlevel);
345 next.set(w,first[newlevel]);
352 status=AFTER_PREFLOW_PHASE_2;
355 /// Returns the value of the maximum flow.
357 /// Returns the value of the maximum flow by returning the excess
358 /// of the target node \c t. This value equals to the value of
359 /// the maximum flow already after running \ref phase1.
360 Num flowValue() const {
365 ///Returns a minimum value cut.
367 ///Sets \c M to the characteristic vector of a minimum value
368 ///cut. This method can be called both after running \ref
369 ///phase1 and \ref phase2. It is much faster after
370 ///\ref phase1. \pre M should be a bool-valued node-map. \pre
371 ///If \ref minCut() is called after \ref phase2() then M should
372 ///be initialized to false.
373 template<typename _CutMap>
374 void minCut(_CutMap& M) const {
376 case AFTER_PREFLOW_PHASE_1:
377 for(NodeIt v(*g); v!=INVALID; ++v) {
385 case AFTER_PREFLOW_PHASE_2:
393 ///Returns the inclusionwise minimum of the minimum value cuts.
395 ///Sets \c M to the characteristic vector of the minimum value cut
396 ///which is inclusionwise minimum. It is computed by processing a
397 ///bfs from the source node \c s in the residual graph. \pre M
398 ///should be a node map of bools initialized to false. \pre \ref
399 ///phase2 should already be run.
400 template<typename _CutMap>
401 void minMinCut(_CutMap& M) const {
403 std::queue<Node> queue;
407 while (!queue.empty()) {
408 Node w=queue.front();
411 for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
413 if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
419 for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
421 if (!M[v] && (*flow)[e] > 0 ) {
429 ///Returns the inclusionwise maximum of the minimum value cuts.
431 ///Sets \c M to the characteristic vector of the minimum value cut
432 ///which is inclusionwise maximum. It is computed by processing a
433 ///backward bfs from the target node \c t in the residual graph.
434 ///\pre \ref phase2() or run() should already be run.
435 template<typename _CutMap>
436 void maxMinCut(_CutMap& M) const {
438 for(NodeIt v(*g) ; v!=INVALID; ++v) M.set(v, true);
440 std::queue<Node> queue;
445 while (!queue.empty()) {
446 Node w=queue.front();
449 for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
451 if (M[v] && (*flow)[e] < (*capacity)[e] ) {
457 for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
459 if (M[v] && (*flow)[e] > 0 ) {
467 ///Sets the source node to \c _s.
469 ///Sets the source node to \c _s.
471 void setSource(Node _s) {
473 if ( flow_prop != ZERO_FLOW ) flow_prop=NO_FLOW;
474 status=AFTER_NOTHING;
477 ///Sets the target node to \c _t.
479 ///Sets the target node to \c _t.
481 void setTarget(Node _t) {
483 if ( flow_prop == GEN_FLOW ) flow_prop=PRE_FLOW;
484 status=AFTER_NOTHING;
487 /// Sets the edge map of the capacities to _cap.
489 /// Sets the edge map of the capacities to _cap.
491 void setCap(const CapMap& _cap) {
493 status=AFTER_NOTHING;
496 /// Sets the edge map of the flows to _flow.
498 /// Sets the edge map of the flows to _flow.
500 void setFlow(FlowMap& _flow) {
503 status=AFTER_NOTHING;
509 int push(Node w, NNMap& next, VecNode& first) {
513 int newlevel=n; //bound on the next level of w
515 for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
516 if ( (*flow)[e] >= (*capacity)[e] ) continue;
519 if( lev > level[v] ) { //Push is allowed now
521 if ( excess[v]<=0 && v!=t && v!=s ) {
522 next.set(v,first[level[v]]);
526 Num cap=(*capacity)[e];
530 if ( remcap >= exc ) { //A nonsaturating push.
532 flow->set(e, flo+exc);
533 excess.set(v, excess[v]+exc);
537 } else { //A saturating push.
539 excess.set(v, excess[v]+remcap);
542 } else if ( newlevel > level[v] ) newlevel = level[v];
546 for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
548 if( (*flow)[e] <= 0 ) continue;
551 if( lev > level[v] ) { //Push is allowed now
553 if ( excess[v]<=0 && v!=t && v!=s ) {
554 next.set(v,first[level[v]]);
560 if ( flo >= exc ) { //A nonsaturating push.
562 flow->set(e, flo-exc);
563 excess.set(v, excess[v]+exc);
566 } else { //A saturating push.
568 excess.set(v, excess[v]+flo);
572 } else if ( newlevel > level[v] ) newlevel = level[v];
575 } // if w still has excess after the out edge for cycle
584 void preflowPreproc(VecNode& first, NNMap& next,
585 VecNode& level_list, NNMap& left, NNMap& right)
587 for(NodeIt v(*g); v!=INVALID; ++v) level.set(v,n);
588 std::queue<Node> bfs_queue;
590 if ( flow_prop == GEN_FLOW || flow_prop == PRE_FLOW ) {
591 //Reverse_bfs from t in the residual graph,
592 //to find the starting level.
596 while ( !bfs_queue.empty() ) {
598 Node v=bfs_queue.front();
602 for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) {
603 if ( (*capacity)[e] <= (*flow)[e] ) continue;
605 if ( level[w] == n && w != s ) {
607 Node z=level_list[l];
608 if ( z!=INVALID ) left.set(z,w);
615 for(OutEdgeIt e(*g,v) ; e!=INVALID; ++e) {
616 if ( 0 >= (*flow)[e] ) continue;
618 if ( level[w] == n && w != s ) {
620 Node z=level_list[l];
621 if ( z!=INVALID ) left.set(z,w);
633 for(EdgeIt e(*g); e!=INVALID; ++e) flow->set(e,0);
635 for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0);
637 //Reverse_bfs from t, to find the starting level.
641 while ( !bfs_queue.empty() ) {
643 Node v=bfs_queue.front();
647 for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) {
649 if ( level[w] == n && w != s ) {
651 Node z=level_list[l];
652 if ( z!=INVALID ) left.set(z,w);
661 for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) {
662 Num c=(*capacity)[e];
663 if ( c <= 0 ) continue;
665 if ( level[w] < n ) {
666 if ( excess[w] <= 0 && w!=t ) { //putting into the stack
667 next.set(w,first[level[w]]);
671 excess.set(w, excess[w]+c);
677 for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0);
680 for(InEdgeIt e(*g,t) ; e!=INVALID; ++e) exc+=(*flow)[e];
681 for(OutEdgeIt e(*g,t) ; e!=INVALID; ++e) exc-=(*flow)[e];
686 for(OutEdgeIt e(*g,s); e!=INVALID; ++e) {
687 Num rem=(*capacity)[e]-(*flow)[e];
688 if ( rem <= 0 ) continue;
690 if ( level[w] < n ) {
691 if ( excess[w] <= 0 && w!=t ) { //putting into the stack
692 next.set(w,first[level[w]]);
695 flow->set(e, (*capacity)[e]);
696 excess.set(w, excess[w]+rem);
700 for(InEdgeIt e(*g,s); e!=INVALID; ++e) {
701 if ( (*flow)[e] <= 0 ) continue;
703 if ( level[w] < n ) {
704 if ( excess[w] <= 0 && w!=t ) {
705 next.set(w,first[level[w]]);
708 excess.set(w, excess[w]+(*flow)[e]);
716 for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) {
717 Num rem=(*capacity)[e]-(*flow)[e];
718 if ( rem <= 0 ) continue;
720 if ( level[w] < n ) flow->set(e, (*capacity)[e]);
723 for(InEdgeIt e(*g,s) ; e!=INVALID; ++e) {
724 if ( (*flow)[e] <= 0 ) continue;
726 if ( level[w] < n ) flow->set(e, 0);
729 //computing the excess
730 for(NodeIt w(*g); w!=INVALID; ++w) {
732 for(InEdgeIt e(*g,w); e!=INVALID; ++e) exc+=(*flow)[e];
733 for(OutEdgeIt e(*g,w); e!=INVALID; ++e) exc-=(*flow)[e];
736 //putting the active nodes into the stack
738 if ( exc > 0 && lev < n && Node(w) != t ) {
739 next.set(w,first[lev]);
748 void relabel(Node w, int newlevel, VecNode& first, NNMap& next,
749 VecNode& level_list, NNMap& left,
750 NNMap& right, int& b, int& k, bool what_heur )
755 Node right_n=right[w];
759 if ( right_n!=INVALID ) {
760 if ( left_n!=INVALID ) {
761 right.set(left_n, right_n);
762 left.set(right_n, left_n);
764 level_list[lev]=right_n;
765 left.set(right_n, INVALID);
768 if ( left_n!=INVALID ) {
769 right.set(left_n, INVALID);
771 level_list[lev]=INVALID;
776 if ( level_list[lev]==INVALID ) {
779 for (int i=lev; i!=k ; ) {
780 Node v=level_list[++i];
781 while ( v!=INVALID ) {
785 level_list[i]=INVALID;
786 if ( !what_heur ) first[i]=INVALID;
796 if ( newlevel == n ) level.set(w,n);
798 level.set(w,++newlevel);
799 next.set(w,first[newlevel]);
801 if ( what_heur ) b=newlevel;
802 if ( k < newlevel ) ++k; //now k=newlevel
803 Node z=level_list[newlevel];
804 if ( z!=INVALID ) left.set(z,w);
807 level_list[newlevel]=w;
815 #endif //LEMON_PREFLOW_H