The new for macros are: h_for, h_for_inc, h_for_glob, h_for_inc_glob.
2 #ifndef HUGO_MAX_FLOW_H
3 #define HUGO_MAX_FLOW_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 Maximum flow algorithms.
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> >
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 Graph::template NodeMap<Node> NNMap;
58 typedef typename std::vector<Node> VecNode;
63 const CapMap* capacity;
65 int n; //the number of nodes of G
66 typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
67 //typedef ExpResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
68 typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
69 typedef typename ResGW::Edge ResGWEdge;
70 //typedef typename ResGW::template NodeMap<bool> ReachedMap;
71 typedef typename Graph::template NodeMap<int> ReachedMap;
74 //level works as a bool map in augmenting path algorithms and is
75 //used by bfs for storing reached information. In preflow, it
76 //shows the levels of nodes.
79 //excess is needed only in preflow
80 typename Graph::template NodeMap<Num> excess;
85 // void set(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
91 // capacity=&_capacity;
94 // level.set (_G); //kellene vmi ilyesmi fv
95 // excess(_G,0); //itt is
98 // constants used for heuristics
99 static const int H0=20;
100 static const int H1=1;
104 ///Indicates the property of the starting flow.
106 ///Indicates the property of the starting flow. The meanings are as follows:
107 ///- \c ZERO_FLOW: constant zero flow
108 ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
109 ///the sum of the out-flows in every node except the \e source and
111 ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at
112 ///least the sum of the out-flows in every node except the \e source.
113 ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be
114 ///set to the constant zero flow in the beginning of the algorithm in this case.
125 AFTER_PRE_FLOW_PHASE_1,
126 AFTER_PRE_FLOW_PHASE_2
129 /// Don not needle this flag only if necessary.
131 int number_of_augmentations;
134 template<typename IntMap>
135 class TrickyReachedMap {
138 int* number_of_augmentations;
140 TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) :
141 map(&_map), number_of_augmentations(&_number_of_augmentations) { }
142 void set(const Node& n, bool b) {
144 map->set(n, *number_of_augmentations);
146 map->set(n, *number_of_augmentations-1);
148 bool operator[](const Node& n) const {
149 return (*map)[n]==*number_of_augmentations;
153 MaxFlow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
155 g(&_G), s(_s), t(_t), capacity(&_capacity),
156 flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0),
157 status(AFTER_NOTHING), number_of_augmentations(0) { }
159 ///Runs a maximum flow algorithm.
161 ///Runs a preflow algorithm, which is the fastest maximum flow
162 ///algorithm up-to-date. The default for \c fe is ZERO_FLOW.
163 ///\pre The starting flow must be
164 /// - a constant zero flow if \c fe is \c ZERO_FLOW,
165 /// - an arbitary flow if \c fe is \c GEN_FLOW,
166 /// - an arbitary preflow if \c fe is \c PRE_FLOW,
167 /// - any map if \c fe is NO_FLOW.
168 void run(FlowEnum fe=ZERO_FLOW) {
173 ///Runs a preflow algorithm.
175 ///Runs a preflow algorithm. The preflow algorithms provide the
176 ///fastest way to compute a maximum flow in a directed graph.
177 ///\pre The starting flow must be
178 /// - a constant zero flow if \c fe is \c ZERO_FLOW,
179 /// - an arbitary flow if \c fe is \c GEN_FLOW,
180 /// - an arbitary preflow if \c fe is \c PRE_FLOW,
181 /// - any map if \c fe is NO_FLOW.
182 void preflow(FlowEnum fe) {
189 // list 'level_list' on the nodes on level i implemented by hand
190 // stack 'active' on the active nodes on level i
191 // runs heuristic 'highest label' for H1*n relabels
192 // runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
193 // Parameters H0 and H1 are initialized to 20 and 1.
195 ///Runs the first phase of the preflow algorithm.
197 ///The preflow algorithm consists of two phases, this method runs the
198 ///first phase. After the first phase the maximum flow value and a
199 ///minimum value cut can already be computed, though a maximum flow
200 ///is net yet obtained. So after calling this method \ref flowValue
201 ///and \ref actMinCut gives proper results.
202 ///\warning: \ref minCut, \ref minMinCut and \ref maxMinCut do not
203 ///give minimum value cuts unless calling \ref preflowPhase2.
204 ///\pre The starting flow must be
205 /// - a constant zero flow if \c fe is \c ZERO_FLOW,
206 /// - an arbitary flow if \c fe is \c GEN_FLOW,
207 /// - an arbitary preflow if \c fe is \c PRE_FLOW,
208 /// - any map if \c fe is NO_FLOW.
209 void preflowPhase1(FlowEnum fe);
211 ///Runs the second phase of the preflow algorithm.
213 ///The preflow algorithm consists of two phases, this method runs
214 ///the second phase. After calling \ref preflowPhase1 and then
215 ///\ref preflowPhase2 the methods \ref flowValue, \ref minCut,
216 ///\ref minMinCut and \ref maxMinCut give proper results.
217 ///\pre \ref preflowPhase1 must be called before.
218 void preflowPhase2();
220 /// Starting from a flow, this method searches for an augmenting path
221 /// according to the Edmonds-Karp algorithm
222 /// and augments the flow on if any.
223 /// The return value shows if the augmentation was succesful.
224 bool augmentOnShortestPath();
225 bool augmentOnShortestPath2();
227 /// Starting from a flow, this method searches for an augmenting blocking
228 /// flow according to Dinits' algorithm and augments the flow on if any.
229 /// The blocking flow is computed in a physically constructed
230 /// residual graph of type \c Mutablegraph.
231 /// The return value show sif the augmentation was succesful.
232 template<typename MutableGraph> bool augmentOnBlockingFlow();
234 /// The same as \c augmentOnBlockingFlow<MutableGraph> but the
235 /// residual graph is not constructed physically.
236 /// The return value shows if the augmentation was succesful.
237 bool augmentOnBlockingFlow2();
239 /// Returns the maximum value of a flow.
241 /// Returns the maximum value of a flow, by counting the
242 /// over-flow of the target node \ref t.
243 /// It can be called already after running \ref preflowPhase1.
244 Num flowValue() const {
246 FOR_EACH_INC_LOC(InEdgeIt, e, *g, t) a+=(*flow)[e];
247 FOR_EACH_INC_LOC(OutEdgeIt, e, *g, t) a-=(*flow)[e];
249 //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan
252 ///Returns a minimum value cut after calling \ref preflowPhase1.
254 ///After the first phase of the preflow algorithm the maximum flow
255 ///value and a minimum value cut can already be computed. This
256 ///method can be called after running \ref preflowPhase1 for
257 ///obtaining a minimum value cut.
258 /// \warning Gives proper result only right after calling \ref
260 /// \todo We have to make some status variable which shows the
262 /// of the class. This enables us to determine which methods are valid
263 /// for MinCut computation
264 template<typename _CutMap>
265 void actMinCut(_CutMap& M) const {
268 case AFTER_PRE_FLOW_PHASE_1:
269 for(g->first(v); g->valid(v); g->next(v)) {
277 case AFTER_PRE_FLOW_PHASE_2:
281 case AFTER_AUGMENTING:
282 for(g->first(v); g->valid(v); g->next(v)) {
293 ///Returns the inclusionwise minimum of the minimum value cuts.
295 ///Sets \c M to the characteristic vector of the minimum value cut
296 ///which is inclusionwise minimum. It is computed by processing
297 ///a bfs from the source node \c s in the residual graph.
298 ///\pre M should be a node map of bools initialized to false.
299 ///\pre \c flow must be a maximum flow.
300 template<typename _CutMap>
301 void minMinCut(_CutMap& M) const {
302 std::queue<Node> queue;
307 while (!queue.empty()) {
308 Node w=queue.front();
312 for(g->first(e,w) ; g->valid(e); g->next(e)) {
314 if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
321 for(g->first(f,w) ; g->valid(f); g->next(f)) {
323 if (!M[v] && (*flow)[f] > 0 ) {
331 ///Returns the inclusionwise maximum of the minimum value cuts.
333 ///Sets \c M to the characteristic vector of the minimum value cut
334 ///which is inclusionwise maximum. It is computed by processing a
335 ///backward bfs from the target node \c t in the residual graph.
336 ///\pre M should be a node map of bools initialized to false.
337 ///\pre \c flow must be a maximum flow.
338 template<typename _CutMap>
339 void maxMinCut(_CutMap& M) const {
342 for(g->first(v) ; g->valid(v); g->next(v)) {
346 std::queue<Node> queue;
351 while (!queue.empty()) {
352 Node w=queue.front();
356 for(g->first(e,w) ; g->valid(e); g->next(e)) {
358 if (M[v] && (*flow)[e] < (*capacity)[e] ) {
365 for(g->first(f,w) ; g->valid(f); g->next(f)) {
367 if (M[v] && (*flow)[f] > 0 ) {
375 ///Returns a minimum value cut.
377 ///Sets \c M to the characteristic vector of a minimum value cut.
378 ///\pre M should be a node map of bools initialized to false.
379 ///\pre \c flow must be a maximum flow.
380 template<typename CutMap>
381 void minCut(CutMap& M) const { minMinCut(M); }
383 ///Resets the source node to \c _s.
385 ///Resets the source node to \c _s.
387 void resetSource(Node _s) { s=_s; status=AFTER_NOTHING; }
389 ///Resets the target node to \c _t.
391 ///Resets the target node to \c _t.
393 void resetTarget(Node _t) { t=_t; status=AFTER_NOTHING; }
395 /// Resets the edge map of the capacities to _cap.
397 /// Resets the edge map of the capacities to _cap.
399 void resetCap(const CapMap& _cap) { capacity=&_cap; status=AFTER_NOTHING; }
401 /// Resets the edge map of the flows to _flow.
403 /// Resets the edge map of the flows to _flow.
405 void resetFlow(FlowMap& _flow) { flow=&_flow; status=AFTER_NOTHING; }
410 int push(Node w, VecStack& active) {
414 int newlevel=n; //bound on the next level of w
417 for(g->first(e,w); g->valid(e); g->next(e)) {
419 if ( (*flow)[e] >= (*capacity)[e] ) continue;
422 if( lev > level[v] ) { //Push is allowed now
424 if ( excess[v]<=0 && v!=t && v!=s ) {
426 active[lev_v].push(v);
429 Num cap=(*capacity)[e];
433 if ( remcap >= exc ) { //A nonsaturating push.
435 flow->set(e, flo+exc);
436 excess.set(v, excess[v]+exc);
440 } else { //A saturating push.
442 excess.set(v, excess[v]+remcap);
445 } else if ( newlevel > level[v] ) newlevel = level[v];
450 for(g->first(e,w); g->valid(e); g->next(e)) {
452 if( (*flow)[e] <= 0 ) continue;
455 if( lev > level[v] ) { //Push is allowed now
457 if ( excess[v]<=0 && v!=t && v!=s ) {
459 active[lev_v].push(v);
464 if ( flo >= exc ) { //A nonsaturating push.
466 flow->set(e, flo-exc);
467 excess.set(v, excess[v]+exc);
470 } else { //A saturating push.
472 excess.set(v, excess[v]+flo);
476 } else if ( newlevel > level[v] ) newlevel = level[v];
479 } // if w still has excess after the out edge for cycle
487 void preflowPreproc(FlowEnum fe, VecStack& active,
488 VecNode& level_list, NNMap& left, NNMap& right)
490 std::queue<Node> bfs_queue;
493 case NO_FLOW: //flow is already set to const zero in this case
496 //Reverse_bfs from t, to find the starting level.
500 while (!bfs_queue.empty()) {
502 Node v=bfs_queue.front();
507 for(g->first(e,v); g->valid(e); g->next(e)) {
509 if ( level[w] == n && w != s ) {
511 Node first=level_list[l];
512 if ( g->valid(first) ) left.set(first,w);
522 for(g->first(e,s); g->valid(e); g->next(e))
524 Num c=(*capacity)[e];
525 if ( c <= 0 ) continue;
527 if ( level[w] < n ) {
528 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
530 excess.set(w, excess[w]+c);
539 //Reverse_bfs from t in the residual graph,
540 //to find the starting level.
544 while (!bfs_queue.empty()) {
546 Node v=bfs_queue.front();
551 for(g->first(e,v); g->valid(e); g->next(e)) {
552 if ( (*capacity)[e] <= (*flow)[e] ) continue;
554 if ( level[w] == n && w != s ) {
556 Node first=level_list[l];
557 if ( g->valid(first) ) left.set(first,w);
565 for(g->first(f,v); g->valid(f); g->next(f)) {
566 if ( 0 >= (*flow)[f] ) continue;
568 if ( level[w] == n && w != s ) {
570 Node first=level_list[l];
571 if ( g->valid(first) ) left.set(first,w);
582 for(g->first(e,s); g->valid(e); g->next(e))
584 Num rem=(*capacity)[e]-(*flow)[e];
585 if ( rem <= 0 ) continue;
587 if ( level[w] < n ) {
588 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
589 flow->set(e, (*capacity)[e]);
590 excess.set(w, excess[w]+rem);
595 for(g->first(f,s); g->valid(f); g->next(f))
597 if ( (*flow)[f] <= 0 ) continue;
599 if ( level[w] < n ) {
600 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
601 excess.set(w, excess[w]+(*flow)[f]);
612 void relabel(Node w, int newlevel, VecStack& active,
613 VecNode& level_list, NNMap& left,
614 NNMap& right, int& b, int& k, bool what_heur )
619 Node right_n=right[w];
623 if ( g->valid(right_n) ) {
624 if ( g->valid(left_n) ) {
625 right.set(left_n, right_n);
626 left.set(right_n, left_n);
628 level_list[lev]=right_n;
629 left.set(right_n, INVALID);
632 if ( g->valid(left_n) ) {
633 right.set(left_n, INVALID);
635 level_list[lev]=INVALID;
640 if ( !g->valid(level_list[lev]) ) {
643 for (int i=lev; i!=k ; ) {
644 Node v=level_list[++i];
645 while ( g->valid(v) ) {
649 level_list[i]=INVALID;
651 while ( !active[i].empty() ) {
652 active[i].pop(); //FIXME: ezt szebben kene
664 if ( newlevel == n ) level.set(w,n);
666 level.set(w,++newlevel);
667 active[newlevel].push(w);
668 if ( what_heur ) b=newlevel;
669 if ( k < newlevel ) ++k; //now k=newlevel
670 Node first=level_list[newlevel];
671 if ( g->valid(first) ) left.set(first,w);
674 level_list[newlevel]=w;
681 template<typename MapGraphWrapper>
684 const MapGraphWrapper* g;
685 typename MapGraphWrapper::template NodeMap<int> dist;
687 DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
688 void set(const typename MapGraphWrapper::Node& n, int a) {
691 int operator[](const typename MapGraphWrapper::Node& n) const {
694 // int get(const typename MapGraphWrapper::Node& n) const {
696 // bool get(const typename MapGraphWrapper::Edge& e) const {
697 // return (dist.get(g->tail(e))<dist.get(g->head(e))); }
698 bool operator[](const typename MapGraphWrapper::Edge& e) const {
699 return (dist[g->tail(e)]<dist[g->head(e)]);
706 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
707 void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase1(FlowEnum fe)
710 int heur0=(int)(H0*n); //time while running 'bound decrease'
711 int heur1=(int)(H1*n); //time while running 'highest label'
712 int heur=heur1; //starting time interval (#of relabels)
716 //It is 0 in case 'bound decrease' and 1 in case 'highest label'
719 //Needed for 'bound decrease', true means no active nodes are above bound
722 int k=n-2; //bound on the highest level under n containing a node
723 int b=k; //bound on the highest level under n of an active node
727 NNMap left(*g, INVALID);
728 NNMap right(*g, INVALID);
729 VecNode level_list(n,INVALID);
730 //List of the nodes in level i<n, set to n.
733 for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
734 //setting each node to level n
736 if ( fe == NO_FLOW ) {
738 for(g->first(e); g->valid(e); g->next(e)) flow->set(e,0);
741 switch (fe) { //computing the excess
745 for(g->first(v); g->valid(v); g->next(v)) {
749 for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
751 for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
755 //putting the active nodes into the stack
757 if ( exc > 0 && lev < n && v != t ) active[lev].push(v);
764 for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
768 for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
770 for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
778 for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
783 preflowPreproc(fe, active, level_list, left, right);
784 //End of preprocessing
787 //Push/relabel on the highest level active nodes.
790 if ( !what_heur && !end && k > 0 ) {
796 if ( active[b].empty() ) --b;
799 Node w=active[b].top();
801 int newlevel=push(w,active);
802 if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list,
803 left, right, b, k, what_heur);
806 if ( numrelabel >= heur ) {
821 status=AFTER_PRE_FLOW_PHASE_1;
826 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
827 void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase2()
830 int k=n-2; //bound on the highest level under n containing a node
831 int b=k; //bound on the highest level under n of an active node
835 std::queue<Node> bfs_queue;
838 while (!bfs_queue.empty()) {
840 Node v=bfs_queue.front();
845 for(g->first(e,v); g->valid(e); g->next(e)) {
846 if ( (*capacity)[e] <= (*flow)[e] ) continue;
848 if ( level[u] >= n ) {
851 if ( excess[u] > 0 ) active[l].push(u);
856 for(g->first(f,v); g->valid(f); g->next(f)) {
857 if ( 0 >= (*flow)[f] ) continue;
859 if ( level[u] >= n ) {
862 if ( excess[u] > 0 ) active[l].push(u);
872 if ( active[b].empty() ) --b;
874 Node w=active[b].top();
876 int newlevel=push(w,active);
879 if ( excess[w] > 0 ) {
880 level.set(w,++newlevel);
881 active[newlevel].push(w);
884 } // if stack[b] is nonempty
887 status=AFTER_PRE_FLOW_PHASE_2;
892 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
893 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath()
895 ResGW res_graph(*g, *capacity, *flow);
898 //ReachedMap level(res_graph);
899 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
900 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
901 bfs.pushAndSetReached(s);
903 typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
904 pred.set(s, INVALID);
906 typename ResGW::template NodeMap<Num> free(res_graph);
908 //searching for augmenting path
909 while ( !bfs.finished() ) {
910 ResGWOutEdgeIt e=bfs;
911 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
912 Node v=res_graph.tail(e);
913 Node w=res_graph.head(e);
915 if (res_graph.valid(pred[v])) {
916 free.set(w, std::min(free[v], res_graph.resCap(e)));
918 free.set(w, res_graph.resCap(e));
920 if (res_graph.head(e)==t) { _augment=true; break; }
924 } //end of searching augmenting path
928 Num augment_value=free[t];
929 while (res_graph.valid(pred[n])) {
931 res_graph.augment(e, augment_value);
936 status=AFTER_AUGMENTING;
941 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
942 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath2()
944 ResGW res_graph(*g, *capacity, *flow);
947 if (status!=AFTER_AUGMENTING) {
948 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 3*n);
949 number_of_augmentations=3*n+1;
951 ++number_of_augmentations;
953 TrickyReachedMap<ReachedMap>
954 tricky_reached_map(level, number_of_augmentations);
955 //ReachedMap level(res_graph);
956 // FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
957 BfsIterator<ResGW, TrickyReachedMap<ReachedMap> >
958 bfs(res_graph, tricky_reached_map);
959 bfs.pushAndSetReached(s);
961 typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
962 pred.set(s, INVALID);
964 typename ResGW::template NodeMap<Num> free(res_graph);
966 //searching for augmenting path
967 while ( !bfs.finished() ) {
968 ResGWOutEdgeIt e=bfs;
969 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
970 Node v=res_graph.tail(e);
971 Node w=res_graph.head(e);
973 if (res_graph.valid(pred[v])) {
974 free.set(w, std::min(free[v], res_graph.resCap(e)));
976 free.set(w, res_graph.resCap(e));
978 if (res_graph.head(e)==t) { _augment=true; break; }
982 } //end of searching augmenting path
986 Num augment_value=free[t];
987 while (res_graph.valid(pred[n])) {
989 res_graph.augment(e, augment_value);
994 status=AFTER_AUGMENTING;
999 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1000 template<typename MutableGraph>
1001 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow()
1003 typedef MutableGraph MG;
1004 bool _augment=false;
1006 ResGW res_graph(*g, *capacity, *flow);
1008 //bfs for distances on the residual graph
1009 //ReachedMap level(res_graph);
1010 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1011 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
1012 bfs.pushAndSetReached(s);
1013 typename ResGW::template NodeMap<int>
1014 dist(res_graph); //filled up with 0's
1016 //F will contain the physical copy of the residual graph
1017 //with the set of edges which are on shortest paths
1019 typename ResGW::template NodeMap<typename MG::Node>
1020 res_graph_to_F(res_graph);
1022 typename ResGW::NodeIt n;
1023 for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
1024 res_graph_to_F.set(n, F.addNode());
1028 typename MG::Node sF=res_graph_to_F[s];
1029 typename MG::Node tF=res_graph_to_F[t];
1030 typename MG::template EdgeMap<ResGWEdge> original_edge(F);
1031 typename MG::template EdgeMap<Num> residual_capacity(F);
1033 while ( !bfs.finished() ) {
1034 ResGWOutEdgeIt e=bfs;
1035 if (res_graph.valid(e)) {
1036 if (bfs.isBNodeNewlyReached()) {
1037 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
1038 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
1039 res_graph_to_F[res_graph.head(e)]);
1040 original_edge.update();
1041 original_edge.set(f, e);
1042 residual_capacity.update();
1043 residual_capacity.set(f, res_graph.resCap(e));
1045 if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
1046 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
1047 res_graph_to_F[res_graph.head(e)]);
1048 original_edge.update();
1049 original_edge.set(f, e);
1050 residual_capacity.update();
1051 residual_capacity.set(f, res_graph.resCap(e));
1056 } //computing distances from s in the residual graph
1058 bool __augment=true;
1062 //computing blocking flow with dfs
1063 DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
1064 typename MG::template NodeMap<typename MG::Edge> pred(F);
1065 pred.set(sF, INVALID);
1066 //invalid iterators for sources
1068 typename MG::template NodeMap<Num> free(F);
1070 dfs.pushAndSetReached(sF);
1071 while (!dfs.finished()) {
1073 if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
1074 if (dfs.isBNodeNewlyReached()) {
1075 typename MG::Node v=F.aNode(dfs);
1076 typename MG::Node w=F.bNode(dfs);
1078 if (F.valid(pred[v])) {
1079 free.set(w, std::min(free[v], residual_capacity[dfs]));
1081 free.set(w, residual_capacity[dfs]);
1090 F.erase(/*typename MG::OutEdgeIt*/(dfs));
1096 typename MG::Node n=tF;
1097 Num augment_value=free[tF];
1098 while (F.valid(pred[n])) {
1099 typename MG::Edge e=pred[n];
1100 res_graph.augment(original_edge[e], augment_value);
1102 if (residual_capacity[e]==augment_value)
1105 residual_capacity.set(e, residual_capacity[e]-augment_value);
1111 status=AFTER_AUGMENTING;
1118 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1119 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2()
1121 bool _augment=false;
1123 ResGW res_graph(*g, *capacity, *flow);
1125 //ReachedMap level(res_graph);
1126 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1127 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
1129 bfs.pushAndSetReached(s);
1130 DistanceMap<ResGW> dist(res_graph);
1131 while ( !bfs.finished() ) {
1132 ResGWOutEdgeIt e=bfs;
1133 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
1134 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
1137 } //computing distances from s in the residual graph
1139 //Subgraph containing the edges on some shortest paths
1140 ConstMap<typename ResGW::Node, bool> true_map(true);
1141 typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
1142 DistanceMap<ResGW> > FilterResGW;
1143 FilterResGW filter_res_graph(res_graph, true_map, dist);
1145 //Subgraph, which is able to delete edges which are already
1147 typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt>
1148 first_out_edges(filter_res_graph);
1149 typename FilterResGW::NodeIt v;
1150 for(filter_res_graph.first(v); filter_res_graph.valid(v);
1151 filter_res_graph.next(v))
1153 typename FilterResGW::OutEdgeIt e;
1154 filter_res_graph.first(e, v);
1155 first_out_edges.set(v, e);
1157 typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
1158 template NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
1159 ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
1161 bool __augment=true;
1166 //computing blocking flow with dfs
1167 DfsIterator< ErasingResGW,
1168 typename ErasingResGW::template NodeMap<bool> >
1169 dfs(erasing_res_graph);
1170 typename ErasingResGW::
1171 template NodeMap<typename ErasingResGW::OutEdgeIt>
1172 pred(erasing_res_graph);
1173 pred.set(s, INVALID);
1174 //invalid iterators for sources
1176 typename ErasingResGW::template NodeMap<Num>
1177 free1(erasing_res_graph);
1179 dfs.pushAndSetReached
1181 (typename ErasingResGW::Node
1182 (typename FilterResGW::Node
1183 (typename ResGW::Node(s)
1187 while (!dfs.finished()) {
1189 if (erasing_res_graph.valid(typename ErasingResGW::OutEdgeIt(dfs)))
1191 if (dfs.isBNodeNewlyReached()) {
1193 typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
1194 typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
1196 pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
1197 if (erasing_res_graph.valid(pred[v])) {
1199 (w, std::min(free1[v], res_graph.resCap
1200 (typename ErasingResGW::OutEdgeIt(dfs))));
1203 (w, res_graph.resCap
1204 (typename ErasingResGW::OutEdgeIt(dfs)));
1213 erasing_res_graph.erase(dfs);
1219 typename ErasingResGW::Node
1220 n=typename FilterResGW::Node(typename ResGW::Node(t));
1221 // typename ResGW::NodeMap<Num> a(res_graph);
1222 // typename ResGW::Node b;
1224 // typename FilterResGW::NodeMap<Num> a1(filter_res_graph);
1225 // typename FilterResGW::Node b1;
1227 // typename ErasingResGW::NodeMap<Num> a2(erasing_res_graph);
1228 // typename ErasingResGW::Node b2;
1230 Num augment_value=free1[n];
1231 while (erasing_res_graph.valid(pred[n])) {
1232 typename ErasingResGW::OutEdgeIt e=pred[n];
1233 res_graph.augment(e, augment_value);
1234 n=erasing_res_graph.tail(e);
1235 if (res_graph.resCap(e)==0)
1236 erasing_res_graph.erase(e);
1240 } //while (__augment)
1242 status=AFTER_AUGMENTING;
1249 #endif //HUGO_MAX_FLOW_H