Set 'svn:ignore' property.
8 #include <hugo/invalid.h>
16 /// \addtogroup flowalgs
19 ///Preflow algorithms class.
21 ///This class provides an implementation of the \e preflow \e
22 ///algorithm producing a flow of maximum value in a directed
23 ///graph. The preflow algorithms are the fastest max flow algorithms
24 ///up-to-date. The \e source node, the \e target node, the \e
25 ///capacity of the edges and the \e starting \e flow value of the
26 ///edges should be passed to the algorithm through the
27 ///constructor. It is possible to change these quantities using the
28 ///functions \ref setSource, \ref setTarget, \ref setCap and \ref
31 ///After running \c phase1 or \c preflow, the actual flow
32 ///value can be obtained by calling \ref flowValue(). The minimum
33 ///value cut can be written into a \c node map of \c bools by
34 ///calling \ref minCut. (\ref minMinCut and \ref maxMinCut writes
35 ///the inclusionwise minimum and maximum of the minimum value cuts,
38 ///\param Graph The directed graph type the algorithm runs on.
39 ///\param Num The number type of the capacities and the flow values.
40 ///\param CapMap The capacity map type.
41 ///\param FlowMap The flow map type.
43 ///\author Jacint Szabo
44 template <typename Graph, typename Num,
45 typename CapMap=typename Graph::template EdgeMap<Num>,
46 typename FlowMap=typename Graph::template EdgeMap<Num> >
49 typedef typename Graph::Node Node;
50 typedef typename Graph::NodeIt NodeIt;
51 typedef typename Graph::EdgeIt EdgeIt;
52 typedef typename Graph::OutEdgeIt OutEdgeIt;
53 typedef typename Graph::InEdgeIt InEdgeIt;
55 typedef typename Graph::template NodeMap<Node> NNMap;
56 typedef typename std::vector<Node> VecNode;
61 const CapMap* capacity;
63 int n; //the number of nodes of G
65 typename Graph::template NodeMap<int> level;
66 typename Graph::template NodeMap<Num> excess;
68 // constants used for heuristics
69 static const int H0=20;
70 static const int H1=1;
74 ///Indicates the property of the starting flow map.
76 ///Indicates the property of the starting flow map. The meanings are as follows:
77 ///- \c ZERO_FLOW: constant zero flow
78 ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
79 ///the sum of the out-flows in every node except the \e source and
81 ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at
82 ///least the sum of the out-flows in every node except the \e source.
83 ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be
84 ///set to the constant zero flow in the beginning of the algorithm in this case.
93 ///Indicates the state of the preflow algorithm.
95 ///Indicates the state of the preflow algorithm. The meanings are as follows:
96 ///- \c AFTER_NOTHING: before running the algorithm or at an unspecified state.
97 ///- \c AFTER_PREFLOW_PHASE_1: right after running \c phase1
98 ///- \c AFTER_PREFLOW_PHASE_2: after running \ref phase2()
102 AFTER_PREFLOW_PHASE_1,
103 AFTER_PREFLOW_PHASE_2
108 StatusEnum status; // Do not needle this flag only if necessary.
111 ///The constructor of the class.
113 ///The constructor of the class.
114 ///\param _G The directed graph the algorithm runs on.
115 ///\param _s The source node.
116 ///\param _t The target node.
117 ///\param _capacity The capacity of the edges.
118 ///\param _flow The flow of the edges.
119 ///Except the graph, all of these parameters can be reset by
120 ///calling \ref setSource, \ref setTarget, \ref setCap and \ref
122 Preflow(const Graph& _G, Node _s, Node _t,
123 const CapMap& _capacity, FlowMap& _flow) :
124 g(&_G), s(_s), t(_t), capacity(&_capacity),
125 flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0),
126 flow_prop(NO_FLOW), status(AFTER_NOTHING) { }
130 ///Runs the preflow algorithm.
132 ///Runs the preflow algorithm.
138 ///Runs the preflow algorithm.
140 ///Runs the preflow algorithm.
141 ///\pre The starting flow map must be
142 /// - a constant zero flow if \c fp is \c ZERO_FLOW,
143 /// - an arbitrary flow if \c fp is \c GEN_FLOW,
144 /// - an arbitrary preflow if \c fp is \c PRE_FLOW,
145 /// - any map if \c fp is NO_FLOW.
146 ///If the starting flow map is a flow or a preflow then
147 ///the algorithm terminates faster.
148 void run(FlowEnum fp) {
153 ///Runs the first phase of the preflow algorithm.
155 ///The preflow algorithm consists of two phases, this method runs the
156 ///first phase. After the first phase the maximum flow value and a
157 ///minimum value cut can already be computed, though a maximum flow
158 ///is not yet obtained. So after calling this method \ref flowValue
159 ///and \ref minCut gives proper results.
160 ///\warning: \ref minMinCut and \ref maxMinCut do not
161 ///give minimum value cuts unless calling \ref phase2.
162 ///\pre The starting flow must be
163 /// - a constant zero flow if \c fp is \c ZERO_FLOW,
164 /// - an arbitary flow if \c fp is \c GEN_FLOW,
165 /// - an arbitary preflow if \c fp is \c PRE_FLOW,
166 /// - any map if \c fp is NO_FLOW.
167 void phase1(FlowEnum fp)
174 ///Runs the first phase of the preflow algorithm.
176 ///The preflow algorithm consists of two phases, this method runs the
177 ///first phase. After the first phase the maximum flow value and a
178 ///minimum value cut can already be computed, though a maximum flow
179 ///is not yet obtained. So after calling this method \ref flowValue
180 ///and \ref actMinCut gives proper results.
181 ///\warning: \ref minCut, \ref minMinCut and \ref maxMinCut do not
182 ///give minimum value cuts unless calling \ref phase2.
185 int heur0=(int)(H0*n); //time while running 'bound decrease'
186 int heur1=(int)(H1*n); //time while running 'highest label'
187 int heur=heur1; //starting time interval (#of relabels)
191 //It is 0 in case 'bound decrease' and 1 in case 'highest label'
194 //Needed for 'bound decrease', true means no active
195 //nodes are above bound b.
197 int k=n-2; //bound on the highest level under n containing a node
198 int b=k; //bound on the highest level under n of an active node
200 VecNode first(n, INVALID);
201 NNMap next(*g, INVALID);
203 NNMap left(*g, INVALID);
204 NNMap right(*g, INVALID);
205 VecNode level_list(n,INVALID);
206 //List of the nodes in level i<n, set to n.
208 preflowPreproc(first, next, level_list, left, right);
210 //Push/relabel on the highest level active nodes.
213 if ( !what_heur && !end && k > 0 ) {
219 if ( first[b]==INVALID ) --b;
224 int newlevel=push(w, next, first);
225 if ( excess[w] > 0 ) relabel(w, newlevel, first, next, level_list,
226 left, right, b, k, what_heur);
229 if ( numrelabel >= heur ) {
244 status=AFTER_PREFLOW_PHASE_1;
249 // list 'level_list' on the nodes on level i implemented by hand
250 // stack 'active' on the active nodes on level i
251 // runs heuristic 'highest label' for H1*n relabels
252 // runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
253 // Parameters H0 and H1 are initialized to 20 and 1.
256 ///Runs the second phase of the preflow algorithm.
258 ///The preflow algorithm consists of two phases, this method runs
259 ///the second phase. After calling \ref phase1 and then
260 ///\ref phase2 the methods \ref flowValue, \ref minCut,
261 ///\ref minMinCut and \ref maxMinCut give proper results.
262 ///\pre \ref phase1 must be called before.
266 int k=n-2; //bound on the highest level under n containing a node
267 int b=k; //bound on the highest level under n of an active node
270 VecNode first(n, INVALID);
271 NNMap next(*g, INVALID);
273 std::queue<Node> bfs_queue;
276 while ( !bfs_queue.empty() ) {
278 Node v=bfs_queue.front();
282 for(InEdgeIt e(*g,v); e!=INVALID; ++e) {
283 if ( (*capacity)[e] <= (*flow)[e] ) continue;
285 if ( level[u] >= n ) {
288 if ( excess[u] > 0 ) {
289 next.set(u,first[l]);
295 for(OutEdgeIt e(*g,v); e!=INVALID; ++e) {
296 if ( 0 >= (*flow)[e] ) continue;
298 if ( level[u] >= n ) {
301 if ( excess[u] > 0 ) {
302 next.set(u,first[l]);
313 if ( first[b]==INVALID ) --b;
317 int newlevel=push(w,next, first);
320 if ( excess[w] > 0 ) {
321 level.set(w,++newlevel);
322 next.set(w,first[newlevel]);
329 status=AFTER_PREFLOW_PHASE_2;
332 /// Returns the value of the maximum flow.
334 /// Returns the value of the maximum flow by returning the excess
335 /// of the target node \ref t. This value equals to the value of
336 /// the maximum flow already after running \ref phase1.
337 Num flowValue() const {
342 ///Returns a minimum value cut.
344 ///Sets \c M to the characteristic vector of a minimum value
345 ///cut. This method can be called both after running \ref
346 ///phase1 and \ref phase2. It is much faster after
347 ///\ref phase1. \pre M should be a node map of bools. \pre
348 ///If \ref mincut is called after \ref phase2 then M should
349 ///be initialized to false.
350 template<typename _CutMap>
351 void minCut(_CutMap& M) const {
353 case AFTER_PREFLOW_PHASE_1:
354 for(NodeIt v(*g); v!=INVALID; ++v) {
362 case AFTER_PREFLOW_PHASE_2:
370 ///Returns the inclusionwise minimum of the minimum value cuts.
372 ///Sets \c M to the characteristic vector of the minimum value cut
373 ///which is inclusionwise minimum. It is computed by processing a
374 ///bfs from the source node \c s in the residual graph. \pre M
375 ///should be a node map of bools initialized to false. \pre \ref
376 ///phase2 should already be run.
377 template<typename _CutMap>
378 void minMinCut(_CutMap& M) const {
380 std::queue<Node> queue;
384 while (!queue.empty()) {
385 Node w=queue.front();
388 for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
390 if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
396 for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
398 if (!M[v] && (*flow)[e] > 0 ) {
406 ///Returns the inclusionwise maximum of the minimum value cuts.
408 ///Sets \c M to the characteristic vector of the minimum value cut
409 ///which is inclusionwise maximum. It is computed by processing a
410 ///backward bfs from the target node \c t in the residual graph.
411 ///\pre \ref phase2() or preflow() should already be run.
412 template<typename _CutMap>
413 void maxMinCut(_CutMap& M) const {
415 for(NodeIt v(*g) ; v!=INVALID; ++v) M.set(v, true);
417 std::queue<Node> queue;
422 while (!queue.empty()) {
423 Node w=queue.front();
426 for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
428 if (M[v] && (*flow)[e] < (*capacity)[e] ) {
434 for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
436 if (M[v] && (*flow)[e] > 0 ) {
444 ///Sets the source node to \c _s.
446 ///Sets the source node to \c _s.
448 void setSource(Node _s) {
450 if ( flow_prop != ZERO_FLOW ) flow_prop=NO_FLOW;
451 status=AFTER_NOTHING;
454 ///Sets the target node to \c _t.
456 ///Sets the target node to \c _t.
458 void setTarget(Node _t) {
460 if ( flow_prop == GEN_FLOW ) flow_prop=PRE_FLOW;
461 status=AFTER_NOTHING;
464 /// Sets the edge map of the capacities to _cap.
466 /// Sets the edge map of the capacities to _cap.
468 void setCap(const CapMap& _cap) {
470 status=AFTER_NOTHING;
473 /// Sets the edge map of the flows to _flow.
475 /// Sets the edge map of the flows to _flow.
477 void setFlow(FlowMap& _flow) {
480 status=AFTER_NOTHING;
486 int push(Node w, NNMap& next, VecNode& first) {
490 int newlevel=n; //bound on the next level of w
492 for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
493 if ( (*flow)[e] >= (*capacity)[e] ) continue;
496 if( lev > level[v] ) { //Push is allowed now
498 if ( excess[v]<=0 && v!=t && v!=s ) {
499 next.set(v,first[level[v]]);
503 Num cap=(*capacity)[e];
507 if ( remcap >= exc ) { //A nonsaturating push.
509 flow->set(e, flo+exc);
510 excess.set(v, excess[v]+exc);
514 } else { //A saturating push.
516 excess.set(v, excess[v]+remcap);
519 } else if ( newlevel > level[v] ) newlevel = level[v];
523 for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
525 if( (*flow)[e] <= 0 ) continue;
528 if( lev > level[v] ) { //Push is allowed now
530 if ( excess[v]<=0 && v!=t && v!=s ) {
531 next.set(v,first[level[v]]);
537 if ( flo >= exc ) { //A nonsaturating push.
539 flow->set(e, flo-exc);
540 excess.set(v, excess[v]+exc);
543 } else { //A saturating push.
545 excess.set(v, excess[v]+flo);
549 } else if ( newlevel > level[v] ) newlevel = level[v];
552 } // if w still has excess after the out edge for cycle
561 void preflowPreproc(VecNode& first, NNMap& next,
562 VecNode& level_list, NNMap& left, NNMap& right)
564 for(NodeIt v(*g); v!=INVALID; ++v) level.set(v,n);
565 std::queue<Node> bfs_queue;
567 if ( flow_prop == GEN_FLOW || flow_prop == PRE_FLOW ) {
568 //Reverse_bfs from t in the residual graph,
569 //to find the starting level.
573 while ( !bfs_queue.empty() ) {
575 Node v=bfs_queue.front();
579 for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) {
580 if ( (*capacity)[e] <= (*flow)[e] ) continue;
582 if ( level[w] == n && w != s ) {
584 Node z=level_list[l];
585 if ( z!=INVALID ) left.set(z,w);
592 for(OutEdgeIt e(*g,v) ; e!=INVALID; ++e) {
593 if ( 0 >= (*flow)[e] ) continue;
595 if ( level[w] == n && w != s ) {
597 Node z=level_list[l];
598 if ( z!=INVALID ) left.set(z,w);
610 for(EdgeIt e(*g); e!=INVALID; ++e) flow->set(e,0);
612 for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0);
614 //Reverse_bfs from t, to find the starting level.
618 while ( !bfs_queue.empty() ) {
620 Node v=bfs_queue.front();
624 for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) {
626 if ( level[w] == n && w != s ) {
628 Node z=level_list[l];
629 if ( z!=INVALID ) left.set(z,w);
638 for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) {
639 Num c=(*capacity)[e];
640 if ( c <= 0 ) continue;
642 if ( level[w] < n ) {
643 if ( excess[w] <= 0 && w!=t ) { //putting into the stack
644 next.set(w,first[level[w]]);
648 excess.set(w, excess[w]+c);
654 for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0);
657 for(InEdgeIt e(*g,t) ; e!=INVALID; ++e) exc+=(*flow)[e];
658 for(OutEdgeIt e(*g,t) ; e!=INVALID; ++e) exc-=(*flow)[e];
663 for(OutEdgeIt e(*g,s); e!=INVALID; ++e) {
664 Num rem=(*capacity)[e]-(*flow)[e];
665 if ( rem <= 0 ) continue;
667 if ( level[w] < n ) {
668 if ( excess[w] <= 0 && w!=t ) { //putting into the stack
669 next.set(w,first[level[w]]);
672 flow->set(e, (*capacity)[e]);
673 excess.set(w, excess[w]+rem);
677 for(InEdgeIt e(*g,s); e!=INVALID; ++e) {
678 if ( (*flow)[e] <= 0 ) continue;
680 if ( level[w] < n ) {
681 if ( excess[w] <= 0 && w!=t ) {
682 next.set(w,first[level[w]]);
685 excess.set(w, excess[w]+(*flow)[e]);
693 for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) {
694 Num rem=(*capacity)[e]-(*flow)[e];
695 if ( rem <= 0 ) continue;
697 if ( level[w] < n ) flow->set(e, (*capacity)[e]);
700 for(InEdgeIt e(*g,s) ; e!=INVALID; ++e) {
701 if ( (*flow)[e] <= 0 ) continue;
703 if ( level[w] < n ) flow->set(e, 0);
706 //computing the excess
707 for(NodeIt w(*g); w!=INVALID; ++w) {
709 for(InEdgeIt e(*g,w); e!=INVALID; ++e) exc+=(*flow)[e];
710 for(OutEdgeIt e(*g,w); e!=INVALID; ++e) exc-=(*flow)[e];
713 //putting the active nodes into the stack
715 if ( exc > 0 && lev < n && Node(w) != t ) {
716 next.set(w,first[lev]);
725 void relabel(Node w, int newlevel, VecNode& first, NNMap& next,
726 VecNode& level_list, NNMap& left,
727 NNMap& right, int& b, int& k, bool what_heur )
732 Node right_n=right[w];
736 if ( right_n!=INVALID ) {
737 if ( left_n!=INVALID ) {
738 right.set(left_n, right_n);
739 left.set(right_n, left_n);
741 level_list[lev]=right_n;
742 left.set(right_n, INVALID);
745 if ( left_n!=INVALID ) {
746 right.set(left_n, INVALID);
748 level_list[lev]=INVALID;
753 if ( level_list[lev]==INVALID ) {
756 for (int i=lev; i!=k ; ) {
757 Node v=level_list[++i];
758 while ( v!=INVALID ) {
762 level_list[i]=INVALID;
763 if ( !what_heur ) first[i]=INVALID;
773 if ( newlevel == n ) level.set(w,n);
775 level.set(w,++newlevel);
776 next.set(w,first[newlevel]);
778 if ( what_heur ) b=newlevel;
779 if ( k < newlevel ) ++k; //now k=newlevel
780 Node z=level_list[newlevel];
781 if ( z!=INVALID ) left.set(z,w);
784 level_list[newlevel]=w;
792 #endif //HUGO_PREFLOW_H