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 now. 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 \ref phase1() or \ref preflow(), the actual flow
32 ///value can be obtained by calling \ref flowValue(). The minimum
33 ///value cut can be written into a <tt>bool</tt> node map 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.
139 ///Runs the preflow algorithm.
141 ///Runs the preflow algorithm.
142 ///\pre The starting flow map must be
143 /// - a constant zero flow if \c fp is \c ZERO_FLOW,
144 /// - an arbitrary flow if \c fp is \c GEN_FLOW,
145 /// - an arbitrary preflow if \c fp is \c PRE_FLOW,
146 /// - any map if \c fp is NO_FLOW.
147 ///If the starting flow map is a flow or a preflow then
148 ///the algorithm terminates faster.
149 void run(FlowEnum fp) {
154 ///Runs the first phase of the preflow algorithm.
156 ///The preflow algorithm consists of two phases, this method runs the
157 ///first phase. After the first phase the maximum flow value and a
158 ///minimum value cut can already be computed, though a maximum flow
159 ///is not yet obtained. So after calling this method \ref flowValue
160 ///and \ref minCut gives proper results.
161 ///\warning \ref minMinCut and \ref maxMinCut do not
162 ///give minimum value cuts unless calling \ref phase2.
163 ///\pre The starting flow must be
164 /// - a constant zero flow if \c fp is \c ZERO_FLOW,
165 /// - an arbitary flow if \c fp is \c GEN_FLOW,
166 /// - an arbitary preflow if \c fp is \c PRE_FLOW,
167 /// - any map if \c fp is NO_FLOW.
168 void phase1(FlowEnum fp)
175 ///Runs the first phase of the preflow algorithm.
177 ///The preflow algorithm consists of two phases, this method runs the
178 ///first phase. After the first phase the maximum flow value and a
179 ///minimum value cut can already be computed, though a maximum flow
180 ///is not yet obtained. So after calling this method \ref flowValue
181 ///and \ref actMinCut gives proper results.
182 ///\warning \ref minCut, \ref minMinCut and \ref maxMinCut do not
183 ///give minimum value cuts unless calling \ref phase2.
186 int heur0=(int)(H0*n); //time while running 'bound decrease'
187 int heur1=(int)(H1*n); //time while running 'highest label'
188 int heur=heur1; //starting time interval (#of relabels)
192 //It is 0 in case 'bound decrease' and 1 in case 'highest label'
195 //Needed for 'bound decrease', true means no active
196 //nodes are above bound b.
198 int k=n-2; //bound on the highest level under n containing a node
199 int b=k; //bound on the highest level under n of an active node
201 VecNode first(n, INVALID);
202 NNMap next(*g, INVALID);
204 NNMap left(*g, INVALID);
205 NNMap right(*g, INVALID);
206 VecNode level_list(n,INVALID);
207 //List of the nodes in level i<n, set to n.
209 preflowPreproc(first, next, level_list, left, right);
211 //Push/relabel on the highest level active nodes.
214 if ( !what_heur && !end && k > 0 ) {
220 if ( first[b]==INVALID ) --b;
225 int newlevel=push(w, next, first);
226 if ( excess[w] > 0 ) relabel(w, newlevel, first, next, level_list,
227 left, right, b, k, what_heur);
230 if ( numrelabel >= heur ) {
245 status=AFTER_PREFLOW_PHASE_1;
250 // list 'level_list' on the nodes on level i implemented by hand
251 // stack 'active' on the active nodes on level i
252 // runs heuristic 'highest label' for H1*n relabels
253 // runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
254 // Parameters H0 and H1 are initialized to 20 and 1.
257 ///Runs the second phase of the preflow algorithm.
259 ///The preflow algorithm consists of two phases, this method runs
260 ///the second phase. After calling \ref phase1 and then
261 ///\ref phase2 the methods \ref flowValue, \ref minCut,
262 ///\ref minMinCut and \ref maxMinCut give proper results.
263 ///\pre \ref phase1 must be called before.
267 int k=n-2; //bound on the highest level under n containing a node
268 int b=k; //bound on the highest level under n of an active node
271 VecNode first(n, INVALID);
272 NNMap next(*g, INVALID);
274 std::queue<Node> bfs_queue;
277 while ( !bfs_queue.empty() ) {
279 Node v=bfs_queue.front();
283 for(InEdgeIt e(*g,v); e!=INVALID; ++e) {
284 if ( (*capacity)[e] <= (*flow)[e] ) continue;
286 if ( level[u] >= n ) {
289 if ( excess[u] > 0 ) {
290 next.set(u,first[l]);
296 for(OutEdgeIt e(*g,v); e!=INVALID; ++e) {
297 if ( 0 >= (*flow)[e] ) continue;
299 if ( level[u] >= n ) {
302 if ( excess[u] > 0 ) {
303 next.set(u,first[l]);
314 if ( first[b]==INVALID ) --b;
318 int newlevel=push(w,next, first);
321 if ( excess[w] > 0 ) {
322 level.set(w,++newlevel);
323 next.set(w,first[newlevel]);
330 status=AFTER_PREFLOW_PHASE_2;
333 /// Returns the value of the maximum flow.
335 /// Returns the value of the maximum flow by returning the excess
336 /// of the target node \ref t. This value equals to the value of
337 /// the maximum flow already after running \ref phase1.
338 Num flowValue() const {
343 ///Returns a minimum value cut.
345 ///Sets \c M to the characteristic vector of a minimum value
346 ///cut. This method can be called both after running \ref
347 ///phase1 and \ref phase2. It is much faster after
348 ///\ref phase1. \pre M should be a bool-valued node-map. \pre
349 ///If \ref mincut is called after \ref phase2 then M should
350 ///be initialized to false.
351 template<typename _CutMap>
352 void minCut(_CutMap& M) const {
354 case AFTER_PREFLOW_PHASE_1:
355 for(NodeIt v(*g); v!=INVALID; ++v) {
363 case AFTER_PREFLOW_PHASE_2:
371 ///Returns the inclusionwise minimum of the minimum value cuts.
373 ///Sets \c M to the characteristic vector of the minimum value cut
374 ///which is inclusionwise minimum. It is computed by processing a
375 ///bfs from the source node \c s in the residual graph. \pre M
376 ///should be a node map of bools initialized to false. \pre \ref
377 ///phase2 should already be run.
378 template<typename _CutMap>
379 void minMinCut(_CutMap& M) const {
381 std::queue<Node> queue;
385 while (!queue.empty()) {
386 Node w=queue.front();
389 for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
391 if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
397 for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
399 if (!M[v] && (*flow)[e] > 0 ) {
407 ///Returns the inclusionwise maximum of the minimum value cuts.
409 ///Sets \c M to the characteristic vector of the minimum value cut
410 ///which is inclusionwise maximum. It is computed by processing a
411 ///backward bfs from the target node \c t in the residual graph.
412 ///\pre \ref phase2() or preflow() should already be run.
413 template<typename _CutMap>
414 void maxMinCut(_CutMap& M) const {
416 for(NodeIt v(*g) ; v!=INVALID; ++v) M.set(v, true);
418 std::queue<Node> queue;
423 while (!queue.empty()) {
424 Node w=queue.front();
427 for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
429 if (M[v] && (*flow)[e] < (*capacity)[e] ) {
435 for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
437 if (M[v] && (*flow)[e] > 0 ) {
445 ///Sets the source node to \c _s.
447 ///Sets the source node to \c _s.
449 void setSource(Node _s) {
451 if ( flow_prop != ZERO_FLOW ) flow_prop=NO_FLOW;
452 status=AFTER_NOTHING;
455 ///Sets the target node to \c _t.
457 ///Sets the target node to \c _t.
459 void setTarget(Node _t) {
461 if ( flow_prop == GEN_FLOW ) flow_prop=PRE_FLOW;
462 status=AFTER_NOTHING;
465 /// Sets the edge map of the capacities to _cap.
467 /// Sets the edge map of the capacities to _cap.
469 void setCap(const CapMap& _cap) {
471 status=AFTER_NOTHING;
474 /// Sets the edge map of the flows to _flow.
476 /// Sets the edge map of the flows to _flow.
478 void setFlow(FlowMap& _flow) {
481 status=AFTER_NOTHING;
487 int push(Node w, NNMap& next, VecNode& first) {
491 int newlevel=n; //bound on the next level of w
493 for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
494 if ( (*flow)[e] >= (*capacity)[e] ) continue;
497 if( lev > level[v] ) { //Push is allowed now
499 if ( excess[v]<=0 && v!=t && v!=s ) {
500 next.set(v,first[level[v]]);
504 Num cap=(*capacity)[e];
508 if ( remcap >= exc ) { //A nonsaturating push.
510 flow->set(e, flo+exc);
511 excess.set(v, excess[v]+exc);
515 } else { //A saturating push.
517 excess.set(v, excess[v]+remcap);
520 } else if ( newlevel > level[v] ) newlevel = level[v];
524 for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
526 if( (*flow)[e] <= 0 ) continue;
529 if( lev > level[v] ) { //Push is allowed now
531 if ( excess[v]<=0 && v!=t && v!=s ) {
532 next.set(v,first[level[v]]);
538 if ( flo >= exc ) { //A nonsaturating push.
540 flow->set(e, flo-exc);
541 excess.set(v, excess[v]+exc);
544 } else { //A saturating push.
546 excess.set(v, excess[v]+flo);
550 } else if ( newlevel > level[v] ) newlevel = level[v];
553 } // if w still has excess after the out edge for cycle
562 void preflowPreproc(VecNode& first, NNMap& next,
563 VecNode& level_list, NNMap& left, NNMap& right)
565 for(NodeIt v(*g); v!=INVALID; ++v) level.set(v,n);
566 std::queue<Node> bfs_queue;
568 if ( flow_prop == GEN_FLOW || flow_prop == PRE_FLOW ) {
569 //Reverse_bfs from t in the residual graph,
570 //to find the starting level.
574 while ( !bfs_queue.empty() ) {
576 Node v=bfs_queue.front();
580 for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) {
581 if ( (*capacity)[e] <= (*flow)[e] ) continue;
583 if ( level[w] == n && w != s ) {
585 Node z=level_list[l];
586 if ( z!=INVALID ) left.set(z,w);
593 for(OutEdgeIt e(*g,v) ; e!=INVALID; ++e) {
594 if ( 0 >= (*flow)[e] ) continue;
596 if ( level[w] == n && w != s ) {
598 Node z=level_list[l];
599 if ( z!=INVALID ) left.set(z,w);
611 for(EdgeIt e(*g); e!=INVALID; ++e) flow->set(e,0);
613 for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0);
615 //Reverse_bfs from t, to find the starting level.
619 while ( !bfs_queue.empty() ) {
621 Node v=bfs_queue.front();
625 for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) {
627 if ( level[w] == n && w != s ) {
629 Node z=level_list[l];
630 if ( z!=INVALID ) left.set(z,w);
639 for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) {
640 Num c=(*capacity)[e];
641 if ( c <= 0 ) continue;
643 if ( level[w] < n ) {
644 if ( excess[w] <= 0 && w!=t ) { //putting into the stack
645 next.set(w,first[level[w]]);
649 excess.set(w, excess[w]+c);
655 for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0);
658 for(InEdgeIt e(*g,t) ; e!=INVALID; ++e) exc+=(*flow)[e];
659 for(OutEdgeIt e(*g,t) ; e!=INVALID; ++e) exc-=(*flow)[e];
664 for(OutEdgeIt e(*g,s); e!=INVALID; ++e) {
665 Num rem=(*capacity)[e]-(*flow)[e];
666 if ( rem <= 0 ) continue;
668 if ( level[w] < n ) {
669 if ( excess[w] <= 0 && w!=t ) { //putting into the stack
670 next.set(w,first[level[w]]);
673 flow->set(e, (*capacity)[e]);
674 excess.set(w, excess[w]+rem);
678 for(InEdgeIt e(*g,s); e!=INVALID; ++e) {
679 if ( (*flow)[e] <= 0 ) continue;
681 if ( level[w] < n ) {
682 if ( excess[w] <= 0 && w!=t ) {
683 next.set(w,first[level[w]]);
686 excess.set(w, excess[w]+(*flow)[e]);
694 for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) {
695 Num rem=(*capacity)[e]-(*flow)[e];
696 if ( rem <= 0 ) continue;
698 if ( level[w] < n ) flow->set(e, (*capacity)[e]);
701 for(InEdgeIt e(*g,s) ; e!=INVALID; ++e) {
702 if ( (*flow)[e] <= 0 ) continue;
704 if ( level[w] < n ) flow->set(e, 0);
707 //computing the excess
708 for(NodeIt w(*g); w!=INVALID; ++w) {
710 for(InEdgeIt e(*g,w); e!=INVALID; ++e) exc+=(*flow)[e];
711 for(OutEdgeIt e(*g,w); e!=INVALID; ++e) exc-=(*flow)[e];
714 //putting the active nodes into the stack
716 if ( exc > 0 && lev < n && Node(w) != t ) {
717 next.set(w,first[lev]);
726 void relabel(Node w, int newlevel, VecNode& first, NNMap& next,
727 VecNode& level_list, NNMap& left,
728 NNMap& right, int& b, int& k, bool what_heur )
733 Node right_n=right[w];
737 if ( right_n!=INVALID ) {
738 if ( left_n!=INVALID ) {
739 right.set(left_n, right_n);
740 left.set(right_n, left_n);
742 level_list[lev]=right_n;
743 left.set(right_n, INVALID);
746 if ( left_n!=INVALID ) {
747 right.set(left_n, INVALID);
749 level_list[lev]=INVALID;
754 if ( level_list[lev]==INVALID ) {
757 for (int i=lev; i!=k ; ) {
758 Node v=level_list[++i];
759 while ( v!=INVALID ) {
763 level_list[i]=INVALID;
764 if ( !what_heur ) first[i]=INVALID;
774 if ( newlevel == n ) level.set(w,n);
776 level.set(w,++newlevel);
777 next.set(w,first[newlevel]);
779 if ( what_heur ) b=newlevel;
780 if ( k < newlevel ) ++k; //now k=newlevel
781 Node z=level_list[newlevel];
782 if ( z!=INVALID ) left.set(z,w);
785 level_list[newlevel]=w;
793 #endif //HUGO_PREFLOW_H