2 #ifndef HUGO_MAX_FLOW_NO_STACK_H
3 #define HUGO_MAX_FLOW_NO_STACK_H
9 #include <hugo/graph_wrapper.h>
11 #include <hugo/invalid.h>
12 #include <hugo/maps.h>
13 #include <hugo/for_each_macros.h>
16 /// \brief The same as max_flow.h, but without using stl stack for the active nodes. Only for test.
23 ///Maximum flow algorithms class.
25 ///This class provides various algorithms for finding a flow of
26 ///maximum value in a directed graph. The \e source node, the \e
27 ///target node, the \e capacity of the edges and the \e starting \e
28 ///flow value of the edges should be passed to the algorithm through the
29 ///constructor. It is possible to change these quantities using the
30 ///functions \ref resetSource, \ref resetTarget, \ref resetCap and
31 ///\ref resetFlow. Before any subsequent runs of any algorithm of
32 ///the class \ref resetFlow should be called.
34 ///After running an algorithm of the class, the actual flow value
35 ///can be obtained by calling \ref flowValue(). The minimum
36 ///value cut can be written into a \c node map of \c bools by
37 ///calling \ref minCut. (\ref minMinCut and \ref maxMinCut writes
38 ///the inclusionwise minimum and maximum of the minimum value
40 ///\param Graph The directed graph type the algorithm runs on.
41 ///\param Num The number type of the capacities and the flow values.
42 ///\param CapMap The capacity map type.
43 ///\param FlowMap The flow map type.
44 ///\author Marton Makai, Jacint Szabo
45 template <typename Graph, typename Num,
46 typename CapMap=typename Graph::template EdgeMap<Num>,
47 typename FlowMap=typename Graph::template EdgeMap<Num> >
48 class MaxFlowNoStack {
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 std::vector<std::stack<Node> > VecStack;
57 typedef typename std::vector<Node> VecFirst;
58 typedef typename Graph::template NodeMap<Node> NNMap;
59 typedef typename std::vector<Node> VecNode;
64 const CapMap* capacity;
66 int n; //the number of nodes of G
67 typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
68 //typedef ExpResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
69 typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
70 typedef typename ResGW::Edge ResGWEdge;
71 //typedef typename ResGW::template NodeMap<bool> ReachedMap;
72 typedef typename Graph::template NodeMap<int> ReachedMap;
75 //level works as a bool map in augmenting path algorithms and is
76 //used by bfs for storing reached information. In preflow, it
77 //shows the levels of nodes.
80 //excess is needed only in preflow
81 typename Graph::template NodeMap<Num> excess;
86 // void set(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
92 // capacity=&_capacity;
95 // level.set (_G); //kellene vmi ilyesmi fv
96 // excess(_G,0); //itt is
99 // constants used for heuristics
100 static const int H0=20;
101 static const int H1=1;
105 ///Indicates the property of the starting flow.
107 ///Indicates the property of the starting flow. The meanings are as follows:
108 ///- \c ZERO_FLOW: constant zero flow
109 ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
110 ///the sum of the out-flows in every node except the \e source and
112 ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at
113 ///least the sum of the out-flows in every node except the \e source.
114 ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be
115 ///set to the constant zero flow in the beginning of the algorithm in this case.
126 AFTER_FAST_AUGMENTING,
127 AFTER_PRE_FLOW_PHASE_1,
128 AFTER_PRE_FLOW_PHASE_2
131 /// Don not needle this flag only if necessary.
133 int number_of_augmentations;
136 template<typename IntMap>
137 class TrickyReachedMap {
140 int* number_of_augmentations;
142 TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) :
143 map(&_map), number_of_augmentations(&_number_of_augmentations) { }
144 void set(const Node& n, bool b) {
146 map->set(n, *number_of_augmentations);
148 map->set(n, *number_of_augmentations-1);
150 bool operator[](const Node& n) const {
151 return (*map)[n]==*number_of_augmentations;
157 ///\todo Document, please.
159 MaxFlowNoStack(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
161 g(&_G), s(_s), t(_t), capacity(&_capacity),
162 flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0),
163 status(AFTER_NOTHING), number_of_augmentations(0) { }
165 ///Runs a maximum flow algorithm.
167 ///Runs a preflow algorithm, which is the fastest maximum flow
168 ///algorithm up-to-date. The default for \c fe is ZERO_FLOW.
169 ///\pre The starting flow must be
170 /// - a constant zero flow if \c fe is \c ZERO_FLOW,
171 /// - an arbitary flow if \c fe is \c GEN_FLOW,
172 /// - an arbitary preflow if \c fe is \c PRE_FLOW,
173 /// - any map if \c fe is NO_FLOW.
174 void run(FlowEnum fe=ZERO_FLOW) {
179 ///Runs a preflow algorithm.
181 ///Runs a preflow algorithm. The preflow algorithms provide the
182 ///fastest way to compute a maximum flow in a directed graph.
183 ///\pre The starting flow must be
184 /// - a constant zero flow if \c fe is \c ZERO_FLOW,
185 /// - an arbitary flow if \c fe is \c GEN_FLOW,
186 /// - an arbitary preflow if \c fe is \c PRE_FLOW,
187 /// - any map if \c fe is NO_FLOW.
189 ///\todo NO_FLOW should be the default flow.
190 void preflow(FlowEnum fe) {
197 // list 'level_list' on the nodes on level i implemented by hand
198 // stack 'active' on the active nodes on level i
199 // runs heuristic 'highest label' for H1*n relabels
200 // runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
201 // Parameters H0 and H1 are initialized to 20 and 1.
203 ///Runs the first phase of the preflow algorithm.
205 ///The preflow algorithm consists of two phases, this method runs the
206 ///first phase. After the first phase the maximum flow value and a
207 ///minimum value cut can already be computed, though a maximum flow
208 ///is net yet obtained. So after calling this method \ref flowValue
209 ///and \ref actMinCut gives proper results.
210 ///\warning: \ref minCut, \ref minMinCut and \ref maxMinCut do not
211 ///give minimum value cuts unless calling \ref preflowPhase2.
212 ///\pre The starting flow must be
213 /// - a constant zero flow if \c fe is \c ZERO_FLOW,
214 /// - an arbitary flow if \c fe is \c GEN_FLOW,
215 /// - an arbitary preflow if \c fe is \c PRE_FLOW,
216 /// - any map if \c fe is NO_FLOW.
217 void preflowPhase1(FlowEnum fe);
219 ///Runs the second phase of the preflow algorithm.
221 ///The preflow algorithm consists of two phases, this method runs
222 ///the second phase. After calling \ref preflowPhase1 and then
223 ///\ref preflowPhase2 the methods \ref flowValue, \ref minCut,
224 ///\ref minMinCut and \ref maxMinCut give proper results.
225 ///\pre \ref preflowPhase1 must be called before.
226 void preflowPhase2();
228 /// Starting from a flow, this method searches for an augmenting path
229 /// according to the Edmonds-Karp algorithm
230 /// and augments the flow on if any.
231 /// The return value shows if the augmentation was succesful.
232 bool augmentOnShortestPath();
233 bool augmentOnShortestPath2();
235 /// Starting from a flow, this method searches for an augmenting blocking
236 /// flow according to Dinits' algorithm and augments the flow on if any.
237 /// The blocking flow is computed in a physically constructed
238 /// residual graph of type \c Mutablegraph.
239 /// The return value show sif the augmentation was succesful.
240 template<typename MutableGraph> bool augmentOnBlockingFlow();
242 /// The same as \c augmentOnBlockingFlow<MutableGraph> but the
243 /// residual graph is not constructed physically.
244 /// The return value shows if the augmentation was succesful.
245 bool augmentOnBlockingFlow2();
247 /// Returns the maximum value of a flow.
249 /// Returns the maximum value of a flow, by counting the
250 /// over-flow of the target node \ref t.
251 /// It can be called already after running \ref preflowPhase1.
252 Num flowValue() const {
254 FOR_EACH_INC_LOC(InEdgeIt, e, *g, t) a+=(*flow)[e];
255 FOR_EACH_INC_LOC(OutEdgeIt, e, *g, t) a-=(*flow)[e];
257 //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan
260 ///Returns a minimum value cut after calling \ref preflowPhase1.
262 ///After the first phase of the preflow algorithm the maximum flow
263 ///value and a minimum value cut can already be computed. This
264 ///method can be called after running \ref preflowPhase1 for
265 ///obtaining a minimum value cut.
266 /// \warning Gives proper result only right after calling \ref
268 /// \todo We have to make some status variable which shows the
270 /// of the class. This enables us to determine which methods are valid
271 /// for MinCut computation
272 template<typename _CutMap>
273 void actMinCut(_CutMap& M) const {
276 case AFTER_PRE_FLOW_PHASE_1:
277 for(g->first(v); g->valid(v); g->next(v)) {
285 case AFTER_PRE_FLOW_PHASE_2:
289 case AFTER_AUGMENTING:
290 for(g->first(v); g->valid(v); g->next(v)) {
298 case AFTER_FAST_AUGMENTING:
299 for(g->first(v); g->valid(v); g->next(v)) {
300 if (level[v]==number_of_augmentations) {
310 ///Returns the inclusionwise minimum of the minimum value cuts.
312 ///Sets \c M to the characteristic vector of the minimum value cut
313 ///which is inclusionwise minimum. It is computed by processing
314 ///a bfs from the source node \c s in the residual graph.
315 ///\pre M should be a node map of bools initialized to false.
316 ///\pre \c flow must be a maximum flow.
317 template<typename _CutMap>
318 void minMinCut(_CutMap& M) const {
319 std::queue<Node> queue;
324 while (!queue.empty()) {
325 Node w=queue.front();
329 for(g->first(e,w) ; g->valid(e); g->next(e)) {
331 if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
338 for(g->first(f,w) ; g->valid(f); g->next(f)) {
340 if (!M[v] && (*flow)[f] > 0 ) {
348 ///Returns the inclusionwise maximum of the minimum value cuts.
350 ///Sets \c M to the characteristic vector of the minimum value cut
351 ///which is inclusionwise maximum. It is computed by processing a
352 ///backward bfs from the target node \c t in the residual graph.
353 ///\pre M should be a node map of bools initialized to false.
354 ///\pre \c flow must be a maximum flow.
355 template<typename _CutMap>
356 void maxMinCut(_CutMap& M) const {
359 for(g->first(v) ; g->valid(v); g->next(v)) {
363 std::queue<Node> queue;
368 while (!queue.empty()) {
369 Node w=queue.front();
373 for(g->first(e,w) ; g->valid(e); g->next(e)) {
375 if (M[v] && (*flow)[e] < (*capacity)[e] ) {
382 for(g->first(f,w) ; g->valid(f); g->next(f)) {
384 if (M[v] && (*flow)[f] > 0 ) {
392 ///Returns a minimum value cut.
394 ///Sets \c M to the characteristic vector of a minimum value cut.
395 ///\pre M should be a node map of bools initialized to false.
396 ///\pre \c flow must be a maximum flow.
397 template<typename CutMap>
398 void minCut(CutMap& M) const { minMinCut(M); }
400 ///Resets the source node to \c _s.
402 ///Resets the source node to \c _s.
404 void resetSource(Node _s) { s=_s; status=AFTER_NOTHING; }
406 ///Resets the target node to \c _t.
408 ///Resets the target node to \c _t.
410 void resetTarget(Node _t) { t=_t; status=AFTER_NOTHING; }
412 /// Resets the edge map of the capacities to _cap.
414 /// Resets the edge map of the capacities to _cap.
416 void resetCap(const CapMap& _cap) { capacity=&_cap; status=AFTER_NOTHING; }
418 /// Resets the edge map of the flows to _flow.
420 /// Resets the edge map of the flows to _flow.
422 void resetFlow(FlowMap& _flow) { flow=&_flow; status=AFTER_NOTHING; }
427 int push(Node w, NNMap& next, VecFirst& first) {
431 int newlevel=n; //bound on the next level of w
434 for(g->first(e,w); g->valid(e); g->next(e)) {
436 if ( (*flow)[e] >= (*capacity)[e] ) continue;
439 if( lev > level[v] ) { //Push is allowed now
441 if ( excess[v]<=0 && v!=t && v!=s ) {
442 next.set(v,first[level[v]]);
444 // int lev_v=level[v];
445 //active[lev_v].push(v);
448 Num cap=(*capacity)[e];
452 if ( remcap >= exc ) { //A nonsaturating push.
454 flow->set(e, flo+exc);
455 excess.set(v, excess[v]+exc);
459 } else { //A saturating push.
461 excess.set(v, excess[v]+remcap);
464 } else if ( newlevel > level[v] ) newlevel = level[v];
469 for(g->first(e,w); g->valid(e); g->next(e)) {
471 if( (*flow)[e] <= 0 ) continue;
474 if( lev > level[v] ) { //Push is allowed now
476 if ( excess[v]<=0 && v!=t && v!=s ) {
477 next.set(v,first[level[v]]);
479 //int lev_v=level[v];
480 //active[lev_v].push(v);
485 if ( flo >= exc ) { //A nonsaturating push.
487 flow->set(e, flo-exc);
488 excess.set(v, excess[v]+exc);
491 } else { //A saturating push.
493 excess.set(v, excess[v]+flo);
497 } else if ( newlevel > level[v] ) newlevel = level[v];
500 } // if w still has excess after the out edge for cycle
508 void preflowPreproc(FlowEnum fe, NNMap& next, VecFirst& first,
509 VecNode& level_list, NNMap& left, NNMap& right)
511 std::queue<Node> bfs_queue;
514 case NO_FLOW: //flow is already set to const zero in this case
517 //Reverse_bfs from t, to find the starting level.
521 while (!bfs_queue.empty()) {
523 Node v=bfs_queue.front();
528 for(g->first(e,v); g->valid(e); g->next(e)) {
530 if ( level[w] == n && w != s ) {
532 Node z=level_list[l];
533 if ( g->valid(z) ) left.set(z,w);
543 for(g->first(e,s); g->valid(e); g->next(e))
545 Num c=(*capacity)[e];
546 if ( c <= 0 ) continue;
548 if ( level[w] < n ) {
549 if ( excess[w] <= 0 && w!=t )
551 next.set(w,first[level[w]]);
553 //active[level[w]].push(w);
556 excess.set(w, excess[w]+c);
565 //Reverse_bfs from t in the residual graph,
566 //to find the starting level.
570 while (!bfs_queue.empty()) {
572 Node v=bfs_queue.front();
577 for(g->first(e,v); g->valid(e); g->next(e)) {
578 if ( (*capacity)[e] <= (*flow)[e] ) continue;
580 if ( level[w] == n && w != s ) {
582 Node z=level_list[l];
583 if ( g->valid(z) ) left.set(z,w);
591 for(g->first(f,v); g->valid(f); g->next(f)) {
592 if ( 0 >= (*flow)[f] ) continue;
594 if ( level[w] == n && w != s ) {
596 Node z=level_list[l];
597 if ( g->valid(z) ) left.set(z,w);
608 for(g->first(e,s); g->valid(e); g->next(e))
610 Num rem=(*capacity)[e]-(*flow)[e];
611 if ( rem <= 0 ) continue;
613 if ( level[w] < n ) {
614 if ( excess[w] <= 0 && w!=t )
616 next.set(w,first[level[w]]);
618 //active[level[w]].push(w);
620 flow->set(e, (*capacity)[e]);
621 excess.set(w, excess[w]+rem);
626 for(g->first(f,s); g->valid(f); g->next(f))
628 if ( (*flow)[f] <= 0 ) continue;
630 if ( level[w] < n ) {
631 if ( excess[w] <= 0 && w!=t )
633 next.set(w,first[level[w]]);
635 //active[level[w]].push(w);
637 excess.set(w, excess[w]+(*flow)[f]);
648 void relabel(Node w, int newlevel, NNMap& next, VecFirst& first,
649 VecNode& level_list, NNMap& left,
650 NNMap& right, int& b, int& k, bool what_heur )
655 Node right_n=right[w];
659 if ( g->valid(right_n) ) {
660 if ( g->valid(left_n) ) {
661 right.set(left_n, right_n);
662 left.set(right_n, left_n);
664 level_list[lev]=right_n;
665 left.set(right_n, INVALID);
668 if ( g->valid(left_n) ) {
669 right.set(left_n, INVALID);
671 level_list[lev]=INVALID;
676 if ( !g->valid(level_list[lev]) ) {
679 for (int i=lev; i!=k ; ) {
680 Node v=level_list[++i];
681 while ( g->valid(v) ) {
685 level_list[i]=INVALID;
686 if ( !what_heur ) first[i]=INVALID;
688 while ( !active[i].empty() ) {
689 active[i].pop(); //FIXME: ezt szebben kene
701 if ( newlevel == n ) level.set(w,n);
703 level.set(w,++newlevel);
704 next.set(w,first[newlevel]);
706 // active[newlevel].push(w);
707 if ( what_heur ) b=newlevel;
708 if ( k < newlevel ) ++k; //now k=newlevel
709 Node z=level_list[newlevel];
710 if ( g->valid(z) ) left.set(z,w);
713 level_list[newlevel]=w;
720 template<typename MapGraphWrapper>
723 const MapGraphWrapper* g;
724 typename MapGraphWrapper::template NodeMap<int> dist;
726 DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
727 void set(const typename MapGraphWrapper::Node& n, int a) {
730 int operator[](const typename MapGraphWrapper::Node& n) const {
733 // int get(const typename MapGraphWrapper::Node& n) const {
735 // bool get(const typename MapGraphWrapper::Edge& e) const {
736 // return (dist.get(g->tail(e))<dist.get(g->head(e))); }
737 bool operator[](const typename MapGraphWrapper::Edge& e) const {
738 return (dist[g->tail(e)]<dist[g->head(e)]);
745 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
746 void MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::preflowPhase1(FlowEnum fe)
749 int heur0=(int)(H0*n); //time while running 'bound decrease'
750 int heur1=(int)(H1*n); //time while running 'highest label'
751 int heur=heur1; //starting time interval (#of relabels)
755 //It is 0 in case 'bound decrease' and 1 in case 'highest label'
758 //Needed for 'bound decrease', true means no active nodes are above bound
761 int k=n-2; //bound on the highest level under n containing a node
762 int b=k; //bound on the highest level under n of an active node
764 VecFirst first(n, INVALID);
765 NNMap next(*g, INVALID); //maybe INVALID is not needed
766 // VecStack active(n);
768 NNMap left(*g, INVALID);
769 NNMap right(*g, INVALID);
770 VecNode level_list(n,INVALID);
771 //List of the nodes in level i<n, set to n.
774 for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
775 //setting each node to level n
777 if ( fe == NO_FLOW ) {
779 for(g->first(e); g->valid(e); g->next(e)) flow->set(e,0);
782 switch (fe) { //computing the excess
786 for(g->first(v); g->valid(v); g->next(v)) {
790 for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
792 for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
796 //putting the active nodes into the stack
798 if ( exc > 0 && lev < n && v != t )
800 next.set(v,first[lev]);
803 // active[lev].push(v);
810 for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
814 for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
816 for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
824 for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
829 preflowPreproc(fe, next, first,/*active*/ level_list, left, right);
830 //End of preprocessing
833 //Push/relabel on the highest level active nodes.
836 if ( !what_heur && !end && k > 0 ) {
842 if ( !g->valid(first[b])/*active[b].empty()*/ ) --b;
847 /* Node w=active[b].top();
849 int newlevel=push(w,/*active*/next, first);
850 if ( excess[w] > 0 ) relabel(w, newlevel, /*active*/next, first, level_list,
851 left, right, b, k, what_heur);
854 if ( numrelabel >= heur ) {
869 status=AFTER_PRE_FLOW_PHASE_1;
874 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
875 void MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::preflowPhase2()
878 int k=n-2; //bound on the highest level under n containing a node
879 int b=k; //bound on the highest level under n of an active node
882 VecFirst first(n, INVALID);
883 NNMap next(*g, INVALID); //maybe INVALID is not needed
884 // VecStack active(n);
886 std::queue<Node> bfs_queue;
889 while (!bfs_queue.empty()) {
891 Node v=bfs_queue.front();
896 for(g->first(e,v); g->valid(e); g->next(e)) {
897 if ( (*capacity)[e] <= (*flow)[e] ) continue;
899 if ( level[u] >= n ) {
902 if ( excess[u] > 0 ) {
903 next.set(u,first[l]);
911 for(g->first(f,v); g->valid(f); g->next(f)) {
912 if ( 0 >= (*flow)[f] ) continue;
914 if ( level[u] >= n ) {
917 if ( excess[u] > 0 ) {
918 next.set(u,first[l]);
931 if ( !g->valid(first[b])/*active[b].empty()*/ ) --b;
936 /* Node w=active[b].top();
938 int newlevel=push(w,next, first/*active*/);
941 if ( excess[w] > 0 ) {
942 level.set(w,++newlevel);
943 next.set(w,first[newlevel]);
945 //active[newlevel].push(w);
948 } // if stack[b] is nonempty
951 status=AFTER_PRE_FLOW_PHASE_2;
956 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
957 bool MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath()
959 ResGW res_graph(*g, *capacity, *flow);
962 //ReachedMap level(res_graph);
963 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
964 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
965 bfs.pushAndSetReached(s);
967 typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
968 pred.set(s, INVALID);
970 typename ResGW::template NodeMap<Num> free(res_graph);
972 //searching for augmenting path
973 while ( !bfs.finished() ) {
974 ResGWOutEdgeIt e=bfs;
975 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
976 Node v=res_graph.tail(e);
977 Node w=res_graph.head(e);
979 if (res_graph.valid(pred[v])) {
980 free.set(w, std::min(free[v], res_graph.resCap(e)));
982 free.set(w, res_graph.resCap(e));
984 if (res_graph.head(e)==t) { _augment=true; break; }
988 } //end of searching augmenting path
992 Num augment_value=free[t];
993 while (res_graph.valid(pred[n])) {
995 res_graph.augment(e, augment_value);
1000 status=AFTER_AUGMENTING;
1005 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1006 bool MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath2()
1008 ResGW res_graph(*g, *capacity, *flow);
1009 bool _augment=false;
1011 if (status!=AFTER_FAST_AUGMENTING) {
1012 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1013 number_of_augmentations=1;
1015 ++number_of_augmentations;
1017 TrickyReachedMap<ReachedMap>
1018 tricky_reached_map(level, number_of_augmentations);
1019 //ReachedMap level(res_graph);
1020 // FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1021 BfsIterator<ResGW, TrickyReachedMap<ReachedMap> >
1022 bfs(res_graph, tricky_reached_map);
1023 bfs.pushAndSetReached(s);
1025 typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
1026 pred.set(s, INVALID);
1028 typename ResGW::template NodeMap<Num> free(res_graph);
1030 //searching for augmenting path
1031 while ( !bfs.finished() ) {
1032 ResGWOutEdgeIt e=bfs;
1033 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
1034 Node v=res_graph.tail(e);
1035 Node w=res_graph.head(e);
1037 if (res_graph.valid(pred[v])) {
1038 free.set(w, std::min(free[v], res_graph.resCap(e)));
1040 free.set(w, res_graph.resCap(e));
1042 if (res_graph.head(e)==t) { _augment=true; break; }
1046 } //end of searching augmenting path
1050 Num augment_value=free[t];
1051 while (res_graph.valid(pred[n])) {
1052 ResGWEdge e=pred[n];
1053 res_graph.augment(e, augment_value);
1054 n=res_graph.tail(e);
1058 status=AFTER_FAST_AUGMENTING;
1063 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1064 template<typename MutableGraph>
1065 bool MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow()
1067 typedef MutableGraph MG;
1068 bool _augment=false;
1070 ResGW res_graph(*g, *capacity, *flow);
1072 //bfs for distances on the residual graph
1073 //ReachedMap level(res_graph);
1074 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1075 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
1076 bfs.pushAndSetReached(s);
1077 typename ResGW::template NodeMap<int>
1078 dist(res_graph); //filled up with 0's
1080 //F will contain the physical copy of the residual graph
1081 //with the set of edges which are on shortest paths
1083 typename ResGW::template NodeMap<typename MG::Node>
1084 res_graph_to_F(res_graph);
1086 typename ResGW::NodeIt n;
1087 for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
1088 res_graph_to_F.set(n, F.addNode());
1092 typename MG::Node sF=res_graph_to_F[s];
1093 typename MG::Node tF=res_graph_to_F[t];
1094 typename MG::template EdgeMap<ResGWEdge> original_edge(F);
1095 typename MG::template EdgeMap<Num> residual_capacity(F);
1097 while ( !bfs.finished() ) {
1098 ResGWOutEdgeIt e=bfs;
1099 if (res_graph.valid(e)) {
1100 if (bfs.isBNodeNewlyReached()) {
1101 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
1102 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
1103 res_graph_to_F[res_graph.head(e)]);
1104 original_edge.update();
1105 original_edge.set(f, e);
1106 residual_capacity.update();
1107 residual_capacity.set(f, res_graph.resCap(e));
1109 if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
1110 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
1111 res_graph_to_F[res_graph.head(e)]);
1112 original_edge.update();
1113 original_edge.set(f, e);
1114 residual_capacity.update();
1115 residual_capacity.set(f, res_graph.resCap(e));
1120 } //computing distances from s in the residual graph
1122 bool __augment=true;
1126 //computing blocking flow with dfs
1127 DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
1128 typename MG::template NodeMap<typename MG::Edge> pred(F);
1129 pred.set(sF, INVALID);
1130 //invalid iterators for sources
1132 typename MG::template NodeMap<Num> free(F);
1134 dfs.pushAndSetReached(sF);
1135 while (!dfs.finished()) {
1137 if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
1138 if (dfs.isBNodeNewlyReached()) {
1139 typename MG::Node v=F.aNode(dfs);
1140 typename MG::Node w=F.bNode(dfs);
1142 if (F.valid(pred[v])) {
1143 free.set(w, std::min(free[v], residual_capacity[dfs]));
1145 free.set(w, residual_capacity[dfs]);
1154 F.erase(/*typename MG::OutEdgeIt*/(dfs));
1160 typename MG::Node n=tF;
1161 Num augment_value=free[tF];
1162 while (F.valid(pred[n])) {
1163 typename MG::Edge e=pred[n];
1164 res_graph.augment(original_edge[e], augment_value);
1166 if (residual_capacity[e]==augment_value)
1169 residual_capacity.set(e, residual_capacity[e]-augment_value);
1175 status=AFTER_AUGMENTING;
1182 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1183 bool MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2()
1185 bool _augment=false;
1187 ResGW res_graph(*g, *capacity, *flow);
1189 //ReachedMap level(res_graph);
1190 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1191 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
1193 bfs.pushAndSetReached(s);
1194 DistanceMap<ResGW> dist(res_graph);
1195 while ( !bfs.finished() ) {
1196 ResGWOutEdgeIt e=bfs;
1197 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
1198 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
1201 } //computing distances from s in the residual graph
1203 //Subgraph containing the edges on some shortest paths
1204 ConstMap<typename ResGW::Node, bool> true_map(true);
1205 typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
1206 DistanceMap<ResGW> > FilterResGW;
1207 FilterResGW filter_res_graph(res_graph, true_map, dist);
1209 //Subgraph, which is able to delete edges which are already
1211 typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt>
1212 first_out_edges(filter_res_graph);
1213 typename FilterResGW::NodeIt v;
1214 for(filter_res_graph.first(v); filter_res_graph.valid(v);
1215 filter_res_graph.next(v))
1217 typename FilterResGW::OutEdgeIt e;
1218 filter_res_graph.first(e, v);
1219 first_out_edges.set(v, e);
1221 typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
1222 template NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
1223 ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
1225 bool __augment=true;
1230 //computing blocking flow with dfs
1231 DfsIterator< ErasingResGW,
1232 typename ErasingResGW::template NodeMap<bool> >
1233 dfs(erasing_res_graph);
1234 typename ErasingResGW::
1235 template NodeMap<typename ErasingResGW::OutEdgeIt>
1236 pred(erasing_res_graph);
1237 pred.set(s, INVALID);
1238 //invalid iterators for sources
1240 typename ErasingResGW::template NodeMap<Num>
1241 free1(erasing_res_graph);
1243 dfs.pushAndSetReached
1245 (typename ErasingResGW::Node
1246 (typename FilterResGW::Node
1247 (typename ResGW::Node(s)
1251 while (!dfs.finished()) {
1253 if (erasing_res_graph.valid(typename ErasingResGW::OutEdgeIt(dfs)))
1255 if (dfs.isBNodeNewlyReached()) {
1257 typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
1258 typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
1260 pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
1261 if (erasing_res_graph.valid(pred[v])) {
1263 (w, std::min(free1[v], res_graph.resCap
1264 (typename ErasingResGW::OutEdgeIt(dfs))));
1267 (w, res_graph.resCap
1268 (typename ErasingResGW::OutEdgeIt(dfs)));
1277 erasing_res_graph.erase(dfs);
1283 typename ErasingResGW::Node
1284 n=typename FilterResGW::Node(typename ResGW::Node(t));
1285 // typename ResGW::NodeMap<Num> a(res_graph);
1286 // typename ResGW::Node b;
1288 // typename FilterResGW::NodeMap<Num> a1(filter_res_graph);
1289 // typename FilterResGW::Node b1;
1291 // typename ErasingResGW::NodeMap<Num> a2(erasing_res_graph);
1292 // typename ErasingResGW::Node b2;
1294 Num augment_value=free1[n];
1295 while (erasing_res_graph.valid(pred[n])) {
1296 typename ErasingResGW::OutEdgeIt e=pred[n];
1297 res_graph.augment(e, augment_value);
1298 n=erasing_res_graph.tail(e);
1299 if (res_graph.resCap(e)==0)
1300 erasing_res_graph.erase(e);
1304 } //while (__augment)
1306 status=AFTER_AUGMENTING;
1313 #endif //HUGO_MAX_FLOW_H