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,
160 const CapMap& _capacity, FlowMap& _flow) :
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)
417 { capacity=&_cap; status=AFTER_NOTHING; }
419 /// Resets the edge map of the flows to _flow.
421 /// Resets the edge map of the flows to _flow.
423 void resetFlow(FlowMap& _flow) { flow=&_flow; status=AFTER_NOTHING; }
428 int push(Node w, NNMap& next, VecFirst& first) {
432 int newlevel=n; //bound on the next level of w
435 for(g->first(e,w); g->valid(e); g->next(e)) {
437 if ( (*flow)[e] >= (*capacity)[e] ) continue;
440 if( lev > level[v] ) { //Push is allowed now
442 if ( excess[v]<=0 && v!=t && v!=s ) {
443 next.set(v,first[level[v]]);
445 // int lev_v=level[v];
446 //active[lev_v].push(v);
449 Num cap=(*capacity)[e];
453 if ( remcap >= exc ) { //A nonsaturating push.
455 flow->set(e, flo+exc);
456 excess.set(v, excess[v]+exc);
460 } else { //A saturating push.
462 excess.set(v, excess[v]+remcap);
465 } else if ( newlevel > level[v] ) newlevel = level[v];
470 for(g->first(e,w); g->valid(e); g->next(e)) {
472 if( (*flow)[e] <= 0 ) continue;
475 if( lev > level[v] ) { //Push is allowed now
477 if ( excess[v]<=0 && v!=t && v!=s ) {
478 next.set(v,first[level[v]]);
480 //int lev_v=level[v];
481 //active[lev_v].push(v);
486 if ( flo >= exc ) { //A nonsaturating push.
488 flow->set(e, flo-exc);
489 excess.set(v, excess[v]+exc);
492 } else { //A saturating push.
494 excess.set(v, excess[v]+flo);
498 } else if ( newlevel > level[v] ) newlevel = level[v];
501 } // if w still has excess after the out edge for cycle
509 void preflowPreproc(FlowEnum fe, NNMap& next, VecFirst& first,
510 VecNode& level_list, NNMap& left, NNMap& right)
512 std::queue<Node> bfs_queue;
515 case NO_FLOW: //flow is already set to const zero in this case
518 //Reverse_bfs from t, to find the starting level.
522 while (!bfs_queue.empty()) {
524 Node v=bfs_queue.front();
529 for(g->first(e,v); g->valid(e); g->next(e)) {
531 if ( level[w] == n && w != s ) {
533 Node z=level_list[l];
534 if ( g->valid(z) ) left.set(z,w);
544 for(g->first(e,s); g->valid(e); g->next(e))
546 Num c=(*capacity)[e];
547 if ( c <= 0 ) continue;
549 if ( level[w] < n ) {
550 if ( excess[w] <= 0 && w!=t )
552 next.set(w,first[level[w]]);
554 //active[level[w]].push(w);
557 excess.set(w, excess[w]+c);
566 //Reverse_bfs from t in the residual graph,
567 //to find the starting level.
571 while (!bfs_queue.empty()) {
573 Node v=bfs_queue.front();
578 for(g->first(e,v); g->valid(e); g->next(e)) {
579 if ( (*capacity)[e] <= (*flow)[e] ) continue;
581 if ( level[w] == n && w != s ) {
583 Node z=level_list[l];
584 if ( g->valid(z) ) left.set(z,w);
592 for(g->first(f,v); g->valid(f); g->next(f)) {
593 if ( 0 >= (*flow)[f] ) continue;
595 if ( level[w] == n && w != s ) {
597 Node z=level_list[l];
598 if ( g->valid(z) ) left.set(z,w);
609 for(g->first(e,s); g->valid(e); g->next(e))
611 Num rem=(*capacity)[e]-(*flow)[e];
612 if ( rem <= 0 ) continue;
614 if ( level[w] < n ) {
615 if ( excess[w] <= 0 && w!=t )
617 next.set(w,first[level[w]]);
619 //active[level[w]].push(w);
621 flow->set(e, (*capacity)[e]);
622 excess.set(w, excess[w]+rem);
627 for(g->first(f,s); g->valid(f); g->next(f))
629 if ( (*flow)[f] <= 0 ) continue;
631 if ( level[w] < n ) {
632 if ( excess[w] <= 0 && w!=t )
634 next.set(w,first[level[w]]);
636 //active[level[w]].push(w);
638 excess.set(w, excess[w]+(*flow)[f]);
649 void relabel(Node w, int newlevel, NNMap& next, VecFirst& first,
650 VecNode& level_list, NNMap& left,
651 NNMap& right, int& b, int& k, bool what_heur )
656 Node right_n=right[w];
660 if ( g->valid(right_n) ) {
661 if ( g->valid(left_n) ) {
662 right.set(left_n, right_n);
663 left.set(right_n, left_n);
665 level_list[lev]=right_n;
666 left.set(right_n, INVALID);
669 if ( g->valid(left_n) ) {
670 right.set(left_n, INVALID);
672 level_list[lev]=INVALID;
677 if ( !g->valid(level_list[lev]) ) {
680 for (int i=lev; i!=k ; ) {
681 Node v=level_list[++i];
682 while ( g->valid(v) ) {
686 level_list[i]=INVALID;
687 if ( !what_heur ) first[i]=INVALID;
689 while ( !active[i].empty() ) {
690 active[i].pop(); //FIXME: ezt szebben kene
702 if ( newlevel == n ) level.set(w,n);
704 level.set(w,++newlevel);
705 next.set(w,first[newlevel]);
707 // active[newlevel].push(w);
708 if ( what_heur ) b=newlevel;
709 if ( k < newlevel ) ++k; //now k=newlevel
710 Node z=level_list[newlevel];
711 if ( g->valid(z) ) left.set(z,w);
714 level_list[newlevel]=w;
721 template<typename MapGraphWrapper>
724 const MapGraphWrapper* g;
725 typename MapGraphWrapper::template NodeMap<int> dist;
727 DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
728 void set(const typename MapGraphWrapper::Node& n, int a) {
731 int operator[](const typename MapGraphWrapper::Node& n) const {
734 // int get(const typename MapGraphWrapper::Node& n) const {
736 // bool get(const typename MapGraphWrapper::Edge& e) const {
737 // return (dist.get(g->tail(e))<dist.get(g->head(e))); }
738 bool operator[](const typename MapGraphWrapper::Edge& e) const {
739 return (dist[g->tail(e)]<dist[g->head(e)]);
746 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
747 void MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::preflowPhase1(FlowEnum fe)
750 int heur0=(int)(H0*n); //time while running 'bound decrease'
751 int heur1=(int)(H1*n); //time while running 'highest label'
752 int heur=heur1; //starting time interval (#of relabels)
756 //It is 0 in case 'bound decrease' and 1 in case 'highest label'
759 //Needed for 'bound decrease', true means no active nodes are above bound
762 int k=n-2; //bound on the highest level under n containing a node
763 int b=k; //bound on the highest level under n of an active node
765 VecFirst first(n, INVALID);
766 NNMap next(*g, INVALID); //maybe INVALID is not needed
767 // VecStack active(n);
769 NNMap left(*g, INVALID);
770 NNMap right(*g, INVALID);
771 VecNode level_list(n,INVALID);
772 //List of the nodes in level i<n, set to n.
775 for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
776 //setting each node to level n
778 if ( fe == NO_FLOW ) {
780 for(g->first(e); g->valid(e); g->next(e)) flow->set(e,0);
783 switch (fe) { //computing the excess
787 for(g->first(v); g->valid(v); g->next(v)) {
791 for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
793 for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
797 //putting the active nodes into the stack
799 if ( exc > 0 && lev < n && v != t )
801 next.set(v,first[lev]);
804 // active[lev].push(v);
811 for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
815 for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
817 for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
825 for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
830 preflowPreproc(fe, next, first,/*active*/ level_list, left, right);
831 //End of preprocessing
834 //Push/relabel on the highest level active nodes.
837 if ( !what_heur && !end && k > 0 ) {
843 if ( !g->valid(first[b])/*active[b].empty()*/ ) --b;
848 /* Node w=active[b].top();
850 int newlevel=push(w,/*active*/next, first);
851 if ( excess[w] > 0 ) relabel(w, newlevel, /*active*/next, first, level_list,
852 left, right, b, k, what_heur);
855 if ( numrelabel >= heur ) {
870 status=AFTER_PRE_FLOW_PHASE_1;
875 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
876 void MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::preflowPhase2()
879 int k=n-2; //bound on the highest level under n containing a node
880 int b=k; //bound on the highest level under n of an active node
883 VecFirst first(n, INVALID);
884 NNMap next(*g, INVALID); //maybe INVALID is not needed
885 // VecStack active(n);
887 std::queue<Node> bfs_queue;
890 while (!bfs_queue.empty()) {
892 Node v=bfs_queue.front();
897 for(g->first(e,v); g->valid(e); g->next(e)) {
898 if ( (*capacity)[e] <= (*flow)[e] ) continue;
900 if ( level[u] >= n ) {
903 if ( excess[u] > 0 ) {
904 next.set(u,first[l]);
912 for(g->first(f,v); g->valid(f); g->next(f)) {
913 if ( 0 >= (*flow)[f] ) continue;
915 if ( level[u] >= n ) {
918 if ( excess[u] > 0 ) {
919 next.set(u,first[l]);
932 if ( !g->valid(first[b])/*active[b].empty()*/ ) --b;
937 /* Node w=active[b].top();
939 int newlevel=push(w,next, first/*active*/);
942 if ( excess[w] > 0 ) {
943 level.set(w,++newlevel);
944 next.set(w,first[newlevel]);
946 //active[newlevel].push(w);
949 } // if stack[b] is nonempty
952 status=AFTER_PRE_FLOW_PHASE_2;
957 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
958 bool MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath()
960 ResGW res_graph(*g, *capacity, *flow);
963 //ReachedMap level(res_graph);
964 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
965 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
966 bfs.pushAndSetReached(s);
968 typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
969 pred.set(s, INVALID);
971 typename ResGW::template NodeMap<Num> free(res_graph);
973 //searching for augmenting path
974 while ( !bfs.finished() ) {
975 ResGWOutEdgeIt e=bfs;
976 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
977 Node v=res_graph.tail(e);
978 Node w=res_graph.head(e);
980 if (res_graph.valid(pred[v])) {
981 free.set(w, std::min(free[v], res_graph.resCap(e)));
983 free.set(w, res_graph.resCap(e));
985 if (res_graph.head(e)==t) { _augment=true; break; }
989 } //end of searching augmenting path
993 Num augment_value=free[t];
994 while (res_graph.valid(pred[n])) {
996 res_graph.augment(e, augment_value);
1001 status=AFTER_AUGMENTING;
1006 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1007 bool MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath2()
1009 ResGW res_graph(*g, *capacity, *flow);
1010 bool _augment=false;
1012 if (status!=AFTER_FAST_AUGMENTING) {
1013 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1014 number_of_augmentations=1;
1016 ++number_of_augmentations;
1018 TrickyReachedMap<ReachedMap>
1019 tricky_reached_map(level, number_of_augmentations);
1020 //ReachedMap level(res_graph);
1021 // FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1022 BfsIterator<ResGW, TrickyReachedMap<ReachedMap> >
1023 bfs(res_graph, tricky_reached_map);
1024 bfs.pushAndSetReached(s);
1026 typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
1027 pred.set(s, INVALID);
1029 typename ResGW::template NodeMap<Num> free(res_graph);
1031 //searching for augmenting path
1032 while ( !bfs.finished() ) {
1033 ResGWOutEdgeIt e=bfs;
1034 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
1035 Node v=res_graph.tail(e);
1036 Node w=res_graph.head(e);
1038 if (res_graph.valid(pred[v])) {
1039 free.set(w, std::min(free[v], res_graph.resCap(e)));
1041 free.set(w, res_graph.resCap(e));
1043 if (res_graph.head(e)==t) { _augment=true; break; }
1047 } //end of searching augmenting path
1051 Num augment_value=free[t];
1052 while (res_graph.valid(pred[n])) {
1053 ResGWEdge e=pred[n];
1054 res_graph.augment(e, augment_value);
1055 n=res_graph.tail(e);
1059 status=AFTER_FAST_AUGMENTING;
1064 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1065 template<typename MutableGraph>
1066 bool MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow()
1068 typedef MutableGraph MG;
1069 bool _augment=false;
1071 ResGW res_graph(*g, *capacity, *flow);
1073 //bfs for distances on the residual graph
1074 //ReachedMap level(res_graph);
1075 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1076 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
1077 bfs.pushAndSetReached(s);
1078 typename ResGW::template NodeMap<int>
1079 dist(res_graph); //filled up with 0's
1081 //F will contain the physical copy of the residual graph
1082 //with the set of edges which are on shortest paths
1084 typename ResGW::template NodeMap<typename MG::Node>
1085 res_graph_to_F(res_graph);
1087 typename ResGW::NodeIt n;
1088 for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
1089 res_graph_to_F.set(n, F.addNode());
1093 typename MG::Node sF=res_graph_to_F[s];
1094 typename MG::Node tF=res_graph_to_F[t];
1095 typename MG::template EdgeMap<ResGWEdge> original_edge(F);
1096 typename MG::template EdgeMap<Num> residual_capacity(F);
1098 while ( !bfs.finished() ) {
1099 ResGWOutEdgeIt e=bfs;
1100 if (res_graph.valid(e)) {
1101 if (bfs.isBNodeNewlyReached()) {
1102 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
1103 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
1104 res_graph_to_F[res_graph.head(e)]);
1105 original_edge.update();
1106 original_edge.set(f, e);
1107 residual_capacity.update();
1108 residual_capacity.set(f, res_graph.resCap(e));
1110 if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
1111 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
1112 res_graph_to_F[res_graph.head(e)]);
1113 original_edge.update();
1114 original_edge.set(f, e);
1115 residual_capacity.update();
1116 residual_capacity.set(f, res_graph.resCap(e));
1121 } //computing distances from s in the residual graph
1123 bool __augment=true;
1127 //computing blocking flow with dfs
1128 DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
1129 typename MG::template NodeMap<typename MG::Edge> pred(F);
1130 pred.set(sF, INVALID);
1131 //invalid iterators for sources
1133 typename MG::template NodeMap<Num> free(F);
1135 dfs.pushAndSetReached(sF);
1136 while (!dfs.finished()) {
1138 if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
1139 if (dfs.isBNodeNewlyReached()) {
1140 typename MG::Node v=F.aNode(dfs);
1141 typename MG::Node w=F.bNode(dfs);
1143 if (F.valid(pred[v])) {
1144 free.set(w, std::min(free[v], residual_capacity[dfs]));
1146 free.set(w, residual_capacity[dfs]);
1155 F.erase(/*typename MG::OutEdgeIt*/(dfs));
1161 typename MG::Node n=tF;
1162 Num augment_value=free[tF];
1163 while (F.valid(pred[n])) {
1164 typename MG::Edge e=pred[n];
1165 res_graph.augment(original_edge[e], augment_value);
1167 if (residual_capacity[e]==augment_value)
1170 residual_capacity.set(e, residual_capacity[e]-augment_value);
1176 status=AFTER_AUGMENTING;
1183 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1184 bool MaxFlowNoStack<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2()
1186 bool _augment=false;
1188 ResGW res_graph(*g, *capacity, *flow);
1190 //ReachedMap level(res_graph);
1191 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1192 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
1194 bfs.pushAndSetReached(s);
1195 DistanceMap<ResGW> dist(res_graph);
1196 while ( !bfs.finished() ) {
1197 ResGWOutEdgeIt e=bfs;
1198 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
1199 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
1202 } //computing distances from s in the residual graph
1204 //Subgraph containing the edges on some shortest paths
1205 ConstMap<typename ResGW::Node, bool> true_map(true);
1206 typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
1207 DistanceMap<ResGW> > FilterResGW;
1208 FilterResGW filter_res_graph(res_graph, true_map, dist);
1210 //Subgraph, which is able to delete edges which are already
1212 typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt>
1213 first_out_edges(filter_res_graph);
1214 typename FilterResGW::NodeIt v;
1215 for(filter_res_graph.first(v); filter_res_graph.valid(v);
1216 filter_res_graph.next(v))
1218 typename FilterResGW::OutEdgeIt e;
1219 filter_res_graph.first(e, v);
1220 first_out_edges.set(v, e);
1222 typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
1223 template NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
1224 ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
1226 bool __augment=true;
1231 //computing blocking flow with dfs
1232 DfsIterator< ErasingResGW,
1233 typename ErasingResGW::template NodeMap<bool> >
1234 dfs(erasing_res_graph);
1235 typename ErasingResGW::
1236 template NodeMap<typename ErasingResGW::OutEdgeIt>
1237 pred(erasing_res_graph);
1238 pred.set(s, INVALID);
1239 //invalid iterators for sources
1241 typename ErasingResGW::template NodeMap<Num>
1242 free1(erasing_res_graph);
1244 dfs.pushAndSetReached
1246 (typename ErasingResGW::Node
1247 (typename FilterResGW::Node
1248 (typename ResGW::Node(s)
1252 while (!dfs.finished()) {
1254 if (erasing_res_graph.valid(typename ErasingResGW::OutEdgeIt(dfs)))
1256 if (dfs.isBNodeNewlyReached()) {
1258 typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
1259 typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
1261 pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
1262 if (erasing_res_graph.valid(pred[v])) {
1264 (w, std::min(free1[v], res_graph.resCap
1265 (typename ErasingResGW::OutEdgeIt(dfs))));
1268 (w, res_graph.resCap
1269 (typename ErasingResGW::OutEdgeIt(dfs)));
1278 erasing_res_graph.erase(dfs);
1284 typename ErasingResGW::Node
1285 n=typename FilterResGW::Node(typename ResGW::Node(t));
1286 // typename ResGW::NodeMap<Num> a(res_graph);
1287 // typename ResGW::Node b;
1289 // typename FilterResGW::NodeMap<Num> a1(filter_res_graph);
1290 // typename FilterResGW::Node b1;
1292 // typename ErasingResGW::NodeMap<Num> a2(erasing_res_graph);
1293 // typename ErasingResGW::Node b2;
1295 Num augment_value=free1[n];
1296 while (erasing_res_graph.valid(pred[n])) {
1297 typename ErasingResGW::OutEdgeIt e=pred[n];
1298 res_graph.augment(e, augment_value);
1299 n=erasing_res_graph.tail(e);
1300 if (res_graph.resCap(e)==0)
1301 erasing_res_graph.erase(e);
1305 } //while (__augment)
1307 status=AFTER_AUGMENTING;
1314 #endif //HUGO_MAX_FLOW_H