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 <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 can 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, otherwise it will
33 ///start from a maximum flow.
34 ///After running an algorithm of the class, the maximum value of a
35 ///value 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 type of the capacity map.
43 ///\param FlowMap The type of the flow map.
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 typename ResGW::OutEdgeIt ResGWOutEdgeIt;
68 typedef typename ResGW::Edge ResGWEdge;
69 //typedef typename ResGW::template NodeMap<bool> ReachedMap;
70 typedef typename Graph::template NodeMap<int> ReachedMap;
73 //level works as a bool map in augmenting path algorithms and is
74 //used by bfs for storing reached information. In preflow, it
75 //shows the levels of nodes.
78 //excess is needed only in preflow
79 typename Graph::template NodeMap<Num> excess;
84 // void set(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
90 // capacity=&_capacity;
93 // level.set (_G); //kellene vmi ilyesmi fv
94 // excess(_G,0); //itt is
97 // constants used for heuristics
98 static const int H0=20;
99 static const int H1=1;
103 ///Indicates the property of the starting flow.
105 ///Indicates the property of the starting flow. The meanings are as follows:
106 ///- \c ZERO_FLOW: constant zero flow
107 ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
108 ///the sum of the out-flows in every node except the \e source and
110 ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at
111 ///least the sum of the out-flows in every node except the \e source.
112 ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be
113 ///set to the constant zero flow in the beginning of the algorithm in this case.
121 MaxFlow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
123 g(&_G), s(_s), t(_t), capacity(&_capacity),
124 flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0) {}
126 ///Runs a maximum flow algorithm.
128 ///Runs a preflow algorithm, which is the fastest maximum flow
129 ///algorithm up-to-date. The default for \c fe is ZERO_FLOW.
130 ///\pre The starting flow must be
131 /// - a constant zero flow if \c fe is \c ZERO_FLOW,
132 /// - an arbitary flow if \c fe is \c GEN_FLOW,
133 /// - an arbitary preflow if \c fe is \c PRE_FLOW,
134 /// - any map if \c fe is NO_FLOW.
135 void run(flowEnum fe=ZERO_FLOW) {
140 ///Runs a preflow algorithm.
142 ///Runs a preflow algorithm. The preflow algorithms provide the
143 ///fastest way to compute a maximum flow in a directed graph.
144 ///\pre The starting flow must be
145 /// - a constant zero flow if \c fe is \c ZERO_FLOW,
146 /// - an arbitary flow if \c fe is \c GEN_FLOW,
147 /// - an arbitary preflow if \c fe is \c PRE_FLOW,
148 /// - any map if \c fe is NO_FLOW.
149 void preflow(flowEnum fe) {
156 // list 'level_list' on the nodes on level i implemented by hand
157 // stack 'active' on the active nodes on level i
158 // runs heuristic 'highest label' for H1*n relabels
159 // runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
160 // Parameters H0 and H1 are initialized to 20 and 1.
162 ///Runs the first phase of the preflow algorithm.
164 ///The preflow algorithm consists of two phases, this method runs the
165 ///first phase. After the first phase the maximum flow value and a
166 ///minimum value cut can already be computed, though a maximum flow
167 ///is net yet obtained. So after calling this method \ref flowValue
168 ///and \ref actMinCut gives proper results.
169 ///\warning: \ref minCut, \ref minMinCut and \ref maxMinCut do not
170 ///give minimum value cuts unless calling \ref preflowPhase2.
171 ///\pre The starting flow must be
172 /// - a constant zero flow if \c fe is \c ZERO_FLOW,
173 /// - an arbitary flow if \c fe is \c GEN_FLOW,
174 /// - an arbitary preflow if \c fe is \c PRE_FLOW,
175 /// - any map if \c fe is NO_FLOW.
176 void preflowPhase1( flowEnum fe );
178 ///Runs the second phase of the preflow algorithm.
180 ///The preflow algorithm consists of two phases, this method runs
181 ///the second phase. After calling \ref preflowPhase1 and then
182 ///\ref preflowPhase2 the methods \ref flowValue, \ref minCut,
183 ///\ref minMinCut and \ref maxMinCut give proper results.
184 ///\pre \ref preflowPhase1 must be called before.
185 void preflowPhase2();
187 /// Starting from a flow, this method searches for an augmenting path
188 /// according to the Edmonds-Karp algorithm
189 /// and augments the flow on if any.
190 /// The return value shows if the augmentation was succesful.
191 bool augmentOnShortestPath();
193 /// Starting from a flow, this method searches for an augmenting blocking
194 /// flow according to Dinits' algorithm and augments the flow on if any.
195 /// The blocking flow is computed in a physically constructed
196 /// residual graph of type \c Mutablegraph.
197 /// The return value show sif the augmentation was succesful.
198 template<typename MutableGraph> bool augmentOnBlockingFlow();
200 /// The same as \c augmentOnBlockingFlow<MutableGraph> but the
201 /// residual graph is not constructed physically.
202 /// The return value shows if the augmentation was succesful.
203 bool augmentOnBlockingFlow2();
205 /// Returns the maximum value of a flow.
207 /// Returns the maximum value of a flow, by counting the
208 /// over-flow of the target node \ref t.
209 /// It can be called already after running \ref preflowPhase1.
212 FOR_EACH_INC_LOC(InEdgeIt, e, *g, t) a+=(*flow)[e];
213 FOR_EACH_INC_LOC(OutEdgeIt, e, *g, t) a-=(*flow)[e];
215 //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan
218 ///Returns a minimum value cut after calling \ref preflowPhase1.
220 ///After the first phase of the preflow algorithm the maximum flow
221 ///value and a minimum value cut can already be computed. This
222 ///method can be called after running \ref preflowPhase1 for
223 ///obtaining a minimum value cut.
224 /// \warning Gives proper result only right after calling \ref
226 /// \todo We have to make some status variable which shows the
228 /// of the class. This enables us to determine which methods are valid
229 /// for MinCut computation
230 template<typename _CutMap>
231 void actMinCut(_CutMap& M) {
233 for(g->first(v); g->valid(v); g->next(v)) {
234 if ( level[v] < n ) {
242 ///Returns the inclusionwise minimum of the minimum value cuts.
244 ///Sets \c M to the characteristic vector of the minimum value cut
245 ///which is inclusionwise minimum. It is computed by processing
246 ///a bfs from the source node \c s in the residual graph.
247 ///\pre M should be a node map of bools initialized to false.
248 ///\pre \c flow must be a maximum flow.
249 template<typename _CutMap>
250 void minMinCut(_CutMap& M) {
252 std::queue<Node> queue;
257 while (!queue.empty()) {
258 Node w=queue.front();
262 for(g->first(e,w) ; g->valid(e); g->next(e)) {
264 if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
271 for(g->first(f,w) ; g->valid(f); g->next(f)) {
273 if (!M[v] && (*flow)[f] > 0 ) {
281 ///Returns the inclusionwise maximum of the minimum value cuts.
283 ///Sets \c M to the characteristic vector of the minimum value cut
284 ///which is inclusionwise maximum. It is computed by processing a
285 ///backward bfs from the target node \c t in the residual graph.
286 ///\pre M should be a node map of bools initialized to false.
287 ///\pre \c flow must be a maximum flow.
288 template<typename _CutMap>
289 void maxMinCut(_CutMap& M) {
292 for(g->first(v) ; g->valid(v); g->next(v)) {
296 std::queue<Node> queue;
301 while (!queue.empty()) {
302 Node w=queue.front();
306 for(g->first(e,w) ; g->valid(e); g->next(e)) {
308 if (M[v] && (*flow)[e] < (*capacity)[e] ) {
315 for(g->first(f,w) ; g->valid(f); g->next(f)) {
317 if (M[v] && (*flow)[f] > 0 ) {
325 ///Returns a minimum value cut.
327 ///Sets \c M to the characteristic vector of a minimum value cut.
328 ///\pre M should be a node map of bools initialized to false.
329 ///\pre \c flow must be a maximum flow.
330 template<typename CutMap>
331 void minCut(CutMap& M) { minMinCut(M); }
333 ///Resets the source node to \c _s.
335 ///Resets the source node to \c _s.
337 void resetSource(Node _s) { s=_s; }
339 ///Resets the target node to \c _t.
341 ///Resets the target node to \c _t.
343 void resetTarget(Node _t) { t=_t; }
345 /// Resets the edge map of the capacities to _cap.
347 /// Resets the edge map of the capacities to _cap.
349 void resetCap(const CapMap& _cap) { capacity=&_cap; }
351 /// Resets the edge map of the flows to _flow.
353 /// Resets the edge map of the flows to _flow.
355 void resetFlow(FlowMap& _flow) { flow=&_flow; }
360 int push(Node w, VecStack& active) {
364 int newlevel=n; //bound on the next level of w
367 for(g->first(e,w); g->valid(e); g->next(e)) {
369 if ( (*flow)[e] >= (*capacity)[e] ) continue;
372 if( lev > level[v] ) { //Push is allowed now
374 if ( excess[v]<=0 && v!=t && v!=s ) {
376 active[lev_v].push(v);
379 Num cap=(*capacity)[e];
383 if ( remcap >= exc ) { //A nonsaturating push.
385 flow->set(e, flo+exc);
386 excess.set(v, excess[v]+exc);
390 } else { //A saturating push.
392 excess.set(v, excess[v]+remcap);
395 } else if ( newlevel > level[v] ) newlevel = level[v];
400 for(g->first(e,w); g->valid(e); g->next(e)) {
402 if( (*flow)[e] <= 0 ) continue;
405 if( lev > level[v] ) { //Push is allowed now
407 if ( excess[v]<=0 && v!=t && v!=s ) {
409 active[lev_v].push(v);
414 if ( flo >= exc ) { //A nonsaturating push.
416 flow->set(e, flo-exc);
417 excess.set(v, excess[v]+exc);
420 } else { //A saturating push.
422 excess.set(v, excess[v]+flo);
426 } else if ( newlevel > level[v] ) newlevel = level[v];
429 } // if w still has excess after the out edge for cycle
437 void preflowPreproc(flowEnum fe, VecStack& active,
438 VecNode& level_list, NNMap& left, NNMap& right)
440 std::queue<Node> bfs_queue;
443 case NO_FLOW: //flow is already set to const zero in this case
446 //Reverse_bfs from t, to find the starting level.
450 while (!bfs_queue.empty()) {
452 Node v=bfs_queue.front();
457 for(g->first(e,v); g->valid(e); g->next(e)) {
459 if ( level[w] == n && w != s ) {
461 Node first=level_list[l];
462 if ( g->valid(first) ) left.set(first,w);
472 for(g->first(e,s); g->valid(e); g->next(e))
474 Num c=(*capacity)[e];
475 if ( c <= 0 ) continue;
477 if ( level[w] < n ) {
478 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
480 excess.set(w, excess[w]+c);
489 //Reverse_bfs from t in the residual graph,
490 //to find the starting level.
494 while (!bfs_queue.empty()) {
496 Node v=bfs_queue.front();
501 for(g->first(e,v); g->valid(e); g->next(e)) {
502 if ( (*capacity)[e] <= (*flow)[e] ) continue;
504 if ( level[w] == n && w != s ) {
506 Node first=level_list[l];
507 if ( g->valid(first) ) left.set(first,w);
515 for(g->first(f,v); g->valid(f); g->next(f)) {
516 if ( 0 >= (*flow)[f] ) continue;
518 if ( level[w] == n && w != s ) {
520 Node first=level_list[l];
521 if ( g->valid(first) ) left.set(first,w);
532 for(g->first(e,s); g->valid(e); g->next(e))
534 Num rem=(*capacity)[e]-(*flow)[e];
535 if ( rem <= 0 ) continue;
537 if ( level[w] < n ) {
538 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
539 flow->set(e, (*capacity)[e]);
540 excess.set(w, excess[w]+rem);
545 for(g->first(f,s); g->valid(f); g->next(f))
547 if ( (*flow)[f] <= 0 ) continue;
549 if ( level[w] < n ) {
550 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
551 excess.set(w, excess[w]+(*flow)[f]);
562 void relabel(Node w, int newlevel, VecStack& active,
563 VecNode& level_list, NNMap& left,
564 NNMap& right, int& b, int& k, bool what_heur )
569 Node right_n=right[w];
573 if ( g->valid(right_n) ) {
574 if ( g->valid(left_n) ) {
575 right.set(left_n, right_n);
576 left.set(right_n, left_n);
578 level_list[lev]=right_n;
579 left.set(right_n, INVALID);
582 if ( g->valid(left_n) ) {
583 right.set(left_n, INVALID);
585 level_list[lev]=INVALID;
590 if ( !g->valid(level_list[lev]) ) {
593 for (int i=lev; i!=k ; ) {
594 Node v=level_list[++i];
595 while ( g->valid(v) ) {
599 level_list[i]=INVALID;
601 while ( !active[i].empty() ) {
602 active[i].pop(); //FIXME: ezt szebben kene
614 if ( newlevel == n ) level.set(w,n);
616 level.set(w,++newlevel);
617 active[newlevel].push(w);
618 if ( what_heur ) b=newlevel;
619 if ( k < newlevel ) ++k; //now k=newlevel
620 Node first=level_list[newlevel];
621 if ( g->valid(first) ) left.set(first,w);
624 level_list[newlevel]=w;
631 template<typename MapGraphWrapper>
634 const MapGraphWrapper* g;
635 typename MapGraphWrapper::template NodeMap<int> dist;
637 DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
638 void set(const typename MapGraphWrapper::Node& n, int a) {
641 int operator[](const typename MapGraphWrapper::Node& n)
643 // int get(const typename MapGraphWrapper::Node& n) const {
645 // bool get(const typename MapGraphWrapper::Edge& e) const {
646 // return (dist.get(g->tail(e))<dist.get(g->head(e))); }
647 bool operator[](const typename MapGraphWrapper::Edge& e) const {
648 return (dist[g->tail(e)]<dist[g->head(e)]);
655 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
656 void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase1( flowEnum fe )
659 int heur0=(int)(H0*n); //time while running 'bound decrease'
660 int heur1=(int)(H1*n); //time while running 'highest label'
661 int heur=heur1; //starting time interval (#of relabels)
665 //It is 0 in case 'bound decrease' and 1 in case 'highest label'
668 //Needed for 'bound decrease', true means no active nodes are above bound
671 int k=n-2; //bound on the highest level under n containing a node
672 int b=k; //bound on the highest level under n of an active node
676 NNMap left(*g, INVALID);
677 NNMap right(*g, INVALID);
678 VecNode level_list(n,INVALID);
679 //List of the nodes in level i<n, set to n.
682 for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
683 //setting each node to level n
685 if ( fe == NO_FLOW ) {
687 for(g->first(e); g->valid(e); g->next(e)) flow->set(e,0);
690 switch (fe) { //computing the excess
694 for(g->first(v); g->valid(v); g->next(v)) {
698 for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
700 for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
704 //putting the active nodes into the stack
706 if ( exc > 0 && lev < n && v != t ) active[lev].push(v);
713 for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
717 for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
719 for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
727 for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
732 preflowPreproc(fe, active, level_list, left, right);
733 //End of preprocessing
736 //Push/relabel on the highest level active nodes.
739 if ( !what_heur && !end && k > 0 ) {
745 if ( active[b].empty() ) --b;
748 Node w=active[b].top();
750 int newlevel=push(w,active);
751 if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list,
752 left, right, b, k, what_heur);
755 if ( numrelabel >= heur ) {
773 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
774 void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase2()
777 int k=n-2; //bound on the highest level under n containing a node
778 int b=k; //bound on the highest level under n of an active node
782 std::queue<Node> bfs_queue;
785 while (!bfs_queue.empty()) {
787 Node v=bfs_queue.front();
792 for(g->first(e,v); g->valid(e); g->next(e)) {
793 if ( (*capacity)[e] <= (*flow)[e] ) continue;
795 if ( level[u] >= n ) {
798 if ( excess[u] > 0 ) active[l].push(u);
803 for(g->first(f,v); g->valid(f); g->next(f)) {
804 if ( 0 >= (*flow)[f] ) continue;
806 if ( level[u] >= n ) {
809 if ( excess[u] > 0 ) active[l].push(u);
819 if ( active[b].empty() ) --b;
821 Node w=active[b].top();
823 int newlevel=push(w,active);
826 if ( excess[w] > 0 ) {
827 level.set(w,++newlevel);
828 active[newlevel].push(w);
831 } // if stack[b] is nonempty
837 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
838 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath()
840 ResGW res_graph(*g, *capacity, *flow);
843 //ReachedMap level(res_graph);
844 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
845 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
846 bfs.pushAndSetReached(s);
848 typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
849 pred.set(s, INVALID);
851 typename ResGW::template NodeMap<Num> free(res_graph);
853 //searching for augmenting path
854 while ( !bfs.finished() ) {
855 ResGWOutEdgeIt e=bfs;
856 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
857 Node v=res_graph.tail(e);
858 Node w=res_graph.head(e);
860 if (res_graph.valid(pred[v])) {
861 free.set(w, std::min(free[v], res_graph.resCap(e)));
863 free.set(w, res_graph.resCap(e));
865 if (res_graph.head(e)==t) { _augment=true; break; }
869 } //end of searching augmenting path
873 Num augment_value=free[t];
874 while (res_graph.valid(pred[n])) {
876 res_graph.augment(e, augment_value);
890 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
891 template<typename MutableGraph>
892 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow()
894 typedef MutableGraph MG;
897 ResGW res_graph(*g, *capacity, *flow);
899 //bfs for distances on the residual graph
900 //ReachedMap level(res_graph);
901 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
902 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
903 bfs.pushAndSetReached(s);
904 typename ResGW::template NodeMap<int>
905 dist(res_graph); //filled up with 0's
907 //F will contain the physical copy of the residual graph
908 //with the set of edges which are on shortest paths
910 typename ResGW::template NodeMap<typename MG::Node>
911 res_graph_to_F(res_graph);
913 typename ResGW::NodeIt n;
914 for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
915 res_graph_to_F.set(n, F.addNode());
919 typename MG::Node sF=res_graph_to_F[s];
920 typename MG::Node tF=res_graph_to_F[t];
921 typename MG::template EdgeMap<ResGWEdge> original_edge(F);
922 typename MG::template EdgeMap<Num> residual_capacity(F);
924 while ( !bfs.finished() ) {
925 ResGWOutEdgeIt e=bfs;
926 if (res_graph.valid(e)) {
927 if (bfs.isBNodeNewlyReached()) {
928 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
929 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
930 res_graph_to_F[res_graph.head(e)]);
931 original_edge.update();
932 original_edge.set(f, e);
933 residual_capacity.update();
934 residual_capacity.set(f, res_graph.resCap(e));
936 if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
937 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
938 res_graph_to_F[res_graph.head(e)]);
939 original_edge.update();
940 original_edge.set(f, e);
941 residual_capacity.update();
942 residual_capacity.set(f, res_graph.resCap(e));
947 } //computing distances from s in the residual graph
953 //computing blocking flow with dfs
954 DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
955 typename MG::template NodeMap<typename MG::Edge> pred(F);
956 pred.set(sF, INVALID);
957 //invalid iterators for sources
959 typename MG::template NodeMap<Num> free(F);
961 dfs.pushAndSetReached(sF);
962 while (!dfs.finished()) {
964 if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
965 if (dfs.isBNodeNewlyReached()) {
966 typename MG::Node v=F.aNode(dfs);
967 typename MG::Node w=F.bNode(dfs);
969 if (F.valid(pred[v])) {
970 free.set(w, std::min(free[v], residual_capacity[dfs]));
972 free.set(w, residual_capacity[dfs]);
981 F.erase(/*typename MG::OutEdgeIt*/(dfs));
987 typename MG::Node n=tF;
988 Num augment_value=free[tF];
989 while (F.valid(pred[n])) {
990 typename MG::Edge e=pred[n];
991 res_graph.augment(original_edge[e], augment_value);
993 if (residual_capacity[e]==augment_value)
996 residual_capacity.set(e, residual_capacity[e]-augment_value);
1008 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1009 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2()
1011 bool _augment=false;
1013 ResGW res_graph(*g, *capacity, *flow);
1015 //ReachedMap level(res_graph);
1016 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1017 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
1019 bfs.pushAndSetReached(s);
1020 DistanceMap<ResGW> dist(res_graph);
1021 while ( !bfs.finished() ) {
1022 ResGWOutEdgeIt e=bfs;
1023 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
1024 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
1027 } //computing distances from s in the residual graph
1029 //Subgraph containing the edges on some shortest paths
1030 ConstMap<typename ResGW::Node, bool> true_map(true);
1031 typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
1032 DistanceMap<ResGW> > FilterResGW;
1033 FilterResGW filter_res_graph(res_graph, true_map, dist);
1035 //Subgraph, which is able to delete edges which are already
1037 typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt>
1038 first_out_edges(filter_res_graph);
1039 typename FilterResGW::NodeIt v;
1040 for(filter_res_graph.first(v); filter_res_graph.valid(v);
1041 filter_res_graph.next(v))
1043 typename FilterResGW::OutEdgeIt e;
1044 filter_res_graph.first(e, v);
1045 first_out_edges.set(v, e);
1047 typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
1048 template NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
1049 ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
1051 bool __augment=true;
1056 //computing blocking flow with dfs
1057 DfsIterator< ErasingResGW,
1058 typename ErasingResGW::template NodeMap<bool> >
1059 dfs(erasing_res_graph);
1060 typename ErasingResGW::
1061 template NodeMap<typename ErasingResGW::OutEdgeIt>
1062 pred(erasing_res_graph);
1063 pred.set(s, INVALID);
1064 //invalid iterators for sources
1066 typename ErasingResGW::template NodeMap<Num>
1067 free1(erasing_res_graph);
1069 dfs.pushAndSetReached
1071 (typename ErasingResGW::Node
1072 (typename FilterResGW::Node
1073 (typename ResGW::Node(s)
1077 while (!dfs.finished()) {
1079 if (erasing_res_graph.valid(typename ErasingResGW::OutEdgeIt(dfs)))
1081 if (dfs.isBNodeNewlyReached()) {
1083 typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
1084 typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
1086 pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
1087 if (erasing_res_graph.valid(pred[v])) {
1089 (w, std::min(free1[v], res_graph.resCap
1090 (typename ErasingResGW::OutEdgeIt(dfs))));
1093 (w, res_graph.resCap
1094 (typename ErasingResGW::OutEdgeIt(dfs)));
1103 erasing_res_graph.erase(dfs);
1109 typename ErasingResGW::Node
1110 n=typename FilterResGW::Node(typename ResGW::Node(t));
1111 // typename ResGW::NodeMap<Num> a(res_graph);
1112 // typename ResGW::Node b;
1114 // typename FilterResGW::NodeMap<Num> a1(filter_res_graph);
1115 // typename FilterResGW::Node b1;
1117 // typename ErasingResGW::NodeMap<Num> a2(erasing_res_graph);
1118 // typename ErasingResGW::Node b2;
1120 Num augment_value=free1[n];
1121 while (erasing_res_graph.valid(pred[n])) {
1122 typename ErasingResGW::OutEdgeIt e=pred[n];
1123 res_graph.augment(e, augment_value);
1124 n=erasing_res_graph.tail(e);
1125 if (res_graph.resCap(e)==0)
1126 erasing_res_graph.erase(e);
1130 } //while (__augment)
1138 #endif //HUGO_MAX_FLOW_H