Concept modification to resolve the item by its ID.
2 #ifndef LEMON_MAX_FLOW_H
3 #define LEMON_MAX_FLOW_H
7 ///\brief Maximum flow algorithm.
16 #include <graph_wrapper.h>
17 #include <bfs_iterator.h>
20 #include <for_each_macros.h>
23 /// \brief Dimacs file format reader.
30 ///Maximum flow algorithms class.
32 ///This class provides various algorithms for finding a flow of
33 ///maximum value in a directed graph. The \e source node, the \e
34 ///target node, the \e capacity of the edges and the \e starting \e
35 ///flow value of the edges can be passed to the algorithm by the
36 ///constructor. It is possible to change these quantities using the
37 ///functions \ref resetSource, \ref resetTarget, \ref resetCap and
38 ///\ref resetFlow. Before any subsequent runs of any algorithm of
39 ///the class \ref resetFlow should be called, otherwise it will
40 ///start from a maximum flow.
42 ///After running an algorithm of the class, the maximum value of a
43 ///value can be obtained by calling \ref flowValue(). The minimum
44 ///value cut can be written into a \c node map of \c bools by
45 ///calling \ref minCut. (\ref minMinCut and \ref maxMinCut writes
46 ///the inclusionwise minimum and maximum of the minimum value
49 ///\param Graph The undirected graph type the algorithm runs on.
50 ///\param Num The number type of the capacities and the flow values.
51 ///\param The type of the capacity map.
52 ///\param The type of the flow map.
54 ///\author Marton Makai, Jacint Szabo
55 template <typename Graph, typename Num,
56 typename CapMap=typename Graph::template EdgeMap<Num>,
57 typename FlowMap=typename Graph::template EdgeMap<Num> >
60 typedef typename Graph::Node Node;
61 typedef typename Graph::NodeIt NodeIt;
62 typedef typename Graph::OutEdgeIt OutEdgeIt;
63 typedef typename Graph::InEdgeIt InEdgeIt;
65 typedef typename std::vector<std::stack<Node> > VecStack;
66 typedef typename Graph::template NodeMap<Node> NNMap;
67 typedef typename std::vector<Node> VecNode;
69 typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
70 typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
71 typedef typename ResGW::Edge ResGWEdge;
72 //typedef typename ResGW::template NodeMap<bool> ReachedMap; //fixme
73 typedef typename Graph::template NodeMap<int> ReachedMap;
78 const CapMap* capacity;
80 int n; //the number of nodes of G
82 //level works as a bool map in augmenting path algorithms and is
83 //used by bfs for storing reached information. In preflow, it
84 //shows the levels of nodes.
87 //excess is needed only in preflow
88 typename Graph::template NodeMap<Num> excess;
94 // void set(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
100 // capacity=&_capacity;
103 // level.set (_G); //kellene vmi ilyesmi fv
104 // excess(_G,0); //itt is
109 ///Indicates the property of the starting flow.
111 ///Indicates the property of the starting flow. The meanings:
112 ///- \c ZERO_FLOW: constant zero flow
113 ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
114 ///the sum of the out-flows in every node except the source and
116 ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at
117 ///least the sum of the out-flows in every node except the source.
124 MaxFlow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
126 g(&_G), s(_s), t(_t), capacity(&_capacity),
127 flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0) {}
129 ///Runs a maximum flow algorithm.
131 ///Runs a preflow algorithm, which is the fastest maximum flow
132 ///algorithm up-to-date. The default for \c fe is ZERO_FLOW.
133 ///\pre The starting flow must be a
134 /// - constant zero flow if \c fe is \c ZERO_FLOW,
135 /// - an arbitary flow if \c fe is \c GEN_FLOW,
136 /// - an arbitary preflow if \c fe is \c PRE_FLOW.
137 void run( flowEnum fe=ZERO_FLOW ) {
141 ///Runs a preflow algorithm.
143 ///Runs a preflow algorithm. The preflow algorithms provide the
144 ///fastest way to compute a maximum flow in a directed graph.
145 ///\pre The starting flow must be a
146 /// - constant zero flow if \c fe is \c ZERO_FLOW,
147 /// - an arbitary flow if \c fe is \c GEN_FLOW,
148 /// - an arbitary preflow if \c fe is \c PRE_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 a
172 /// - 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 void preflowPhase1( flowEnum fe );
177 ///Runs the second phase of the preflow algorithm.
179 ///The preflow algorithm consists of two phases, this method runs
180 ///the second phase. After calling \ref preflowPhase1 and then
181 ///\ref preflowPhase2 the methods \ref flowValue, \ref minCut,
182 ///\ref minMinCut and \ref maxMinCut give proper results.
183 ///\pre \ref preflowPhase1 must be called before.
184 void preflowPhase2();
186 /// Starting from a flow, this method searches for an augmenting path
187 /// according to the Edmonds-Karp algorithm
188 /// and augments the flow on if any.
189 /// The return value shows if the augmentation was successful.
190 bool augmentOnShortestPath();
192 /// Starting from a flow, this method searches for an augmenting blockin
193 /// flow according to Dinits' algorithm and augments the flow on if any.
194 /// The blocking flow is computed in a physically constructed
195 /// residual graph of type \c Mutablegraph.
196 /// The return value show sif the augmentation was succesful.
197 template<typename MutableGraph> bool augmentOnBlockingFlow();
199 /// The same as \c augmentOnBlockingFlow<MutableGraph> but the
200 /// residual graph is not constructed physically.
201 /// The return value shows if the augmentation was succesful.
202 bool augmentOnBlockingFlow2();
204 /// Returns the actual flow value.
205 /// More precisely, it returns the negative excess of s, thus
206 /// this works also for preflows.
207 ///Can be called already after \ref preflowPhase1.
211 FOR_EACH_INC_LOC(OutEdgeIt, e, *g, s) a+=(*flow)[e];
212 FOR_EACH_INC_LOC(InEdgeIt, e, *g, s) a-=(*flow)[e];
214 //marci figyu: excess[t] epp ezt adja preflow 0. fazisa utan
217 ///Returns a minimum value cut after calling \ref preflowPhase1.
219 ///After the first phase of the preflow algorithm the maximum flow
220 ///value and a minimum value cut can already be computed. This
221 ///method can be called after running \ref preflowPhase1 for
222 ///obtaining a minimum value cut.
223 ///\warning: Gives proper result only right after calling \ref
225 ///\todo We have to make some status variable which shows the actual state
226 /// of the class. This enables us to determine which methods are valid
227 /// for MinCut computation
228 template<typename _CutMap>
229 void actMinCut(_CutMap& M) {
231 for(g->first(v); g->valid(v); g->next(v)) {
232 if ( level[v] < n ) {
240 ///Returns the inclusionwise minimum of the minimum value cuts.
242 ///Sets \c M to the characteristic vector of the minimum value cut
243 ///which is inclusionwise minimum. It is computed by processing
244 ///a bfs from the source node \c s in the residual graph.
245 ///\pre M should be a node map of bools initialized to false.
246 ///\pre \c flow must be a maximum flow.
247 template<typename _CutMap>
248 void minMinCut(_CutMap& M) {
250 std::queue<Node> queue;
255 while (!queue.empty()) {
256 Node w=queue.front();
260 for(g->first(e,w) ; g->valid(e); g->next(e)) {
262 if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
269 for(g->first(f,w) ; g->valid(f); g->next(f)) {
271 if (!M[v] && (*flow)[f] > 0 ) {
280 ///Returns the inclusionwise maximum of the minimum value cuts.
282 ///Sets \c M to the characteristic vector of the minimum value cut
283 ///which is inclusionwise maximum. It is computed by processing a
284 ///backward bfs from the target node \c t in the residual graph.
285 ///\pre M should be a node map of bools initialized to false.
286 ///\pre \c flow must be a maximum flow.
287 template<typename _CutMap>
288 void maxMinCut(_CutMap& M) {
291 for(g->first(v) ; g->valid(v); g->next(v)) {
295 std::queue<Node> queue;
300 while (!queue.empty()) {
301 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 ) {
326 ///Returns a minimum value cut.
328 ///Sets \c M to the characteristic vector of a minimum value cut.
329 ///\pre M should be a node map of bools initialized to false.
330 ///\pre \c flow must be a maximum flow.
331 template<typename CutMap>
332 void minCut(CutMap& M) { minMinCut(M); }
334 ///Resets the source node to \c _s.
336 ///Resets the source node to \c _s.
338 void resetSource(Node _s) { s=_s; }
341 ///Resets the target node to \c _t.
343 ///Resets the target node to \c _t.
345 void resetTarget(Node _t) { t=_t; }
347 /// Resets the edge map of the capacities to _cap.
349 /// Resets the edge map of the capacities to _cap.
351 void resetCap(const CapMap& _cap) { capacity=&_cap; }
353 /// Resets the edge map of the flows to _flow.
355 /// Resets the edge map of the flows to _flow.
357 void resetFlow(FlowMap& _flow) { flow=&_flow; }
362 int push(Node w, VecStack& active) {
366 int newlevel=n; //bound on the next level of w
369 for(g->first(e,w); g->valid(e); g->next(e)) {
371 if ( (*flow)[e] >= (*capacity)[e] ) continue;
374 if( lev > level[v] ) { //Push is allowed now
376 if ( excess[v]<=0 && v!=t && v!=s ) {
378 active[lev_v].push(v);
381 Num cap=(*capacity)[e];
385 if ( remcap >= exc ) { //A nonsaturating push.
387 flow->set(e, flo+exc);
388 excess.set(v, excess[v]+exc);
392 } else { //A saturating push.
394 excess.set(v, excess[v]+remcap);
397 } else if ( newlevel > level[v] ) newlevel = level[v];
402 for(g->first(e,w); g->valid(e); g->next(e)) {
404 if( (*flow)[e] <= 0 ) continue;
407 if( lev > level[v] ) { //Push is allowed now
409 if ( excess[v]<=0 && v!=t && v!=s ) {
411 active[lev_v].push(v);
416 if ( flo >= exc ) { //A nonsaturating push.
418 flow->set(e, flo-exc);
419 excess.set(v, excess[v]+exc);
422 } else { //A saturating push.
424 excess.set(v, excess[v]+flo);
428 } else if ( newlevel > level[v] ) newlevel = level[v];
431 } // if w still has excess after the out edge for cycle
439 void preflowPreproc ( flowEnum fe, VecStack& active,
440 VecNode& level_list, NNMap& left, NNMap& right ) {
442 std::queue<Node> bfs_queue;
447 //Reverse_bfs from t, to find the starting level.
451 while (!bfs_queue.empty()) {
453 Node v=bfs_queue.front();
458 for(g->first(e,v); g->valid(e); g->next(e)) {
460 if ( level[w] == n && w != s ) {
462 Node first=level_list[l];
463 if ( g->valid(first) ) left.set(first,w);
473 for(g->first(e,s); g->valid(e); g->next(e))
475 Num c=(*capacity)[e];
476 if ( c <= 0 ) continue;
478 if ( level[w] < n ) {
479 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
481 excess.set(w, excess[w]+c);
490 //Reverse_bfs from t in the residual graph,
491 //to find the starting level.
495 while (!bfs_queue.empty()) {
497 Node v=bfs_queue.front();
502 for(g->first(e,v); g->valid(e); g->next(e)) {
503 if ( (*capacity)[e] <= (*flow)[e] ) continue;
505 if ( level[w] == n && w != s ) {
507 Node first=level_list[l];
508 if ( g->valid(first) ) left.set(first,w);
516 for(g->first(f,v); g->valid(f); g->next(f)) {
517 if ( 0 >= (*flow)[f] ) continue;
519 if ( level[w] == n && w != s ) {
521 Node first=level_list[l];
522 if ( g->valid(first) ) left.set(first,w);
533 for(g->first(e,s); g->valid(e); g->next(e))
535 Num rem=(*capacity)[e]-(*flow)[e];
536 if ( rem <= 0 ) continue;
538 if ( level[w] < n ) {
539 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
540 flow->set(e, (*capacity)[e]);
541 excess.set(w, excess[w]+rem);
546 for(g->first(f,s); g->valid(f); g->next(f))
548 if ( (*flow)[f] <= 0 ) continue;
550 if ( level[w] < n ) {
551 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
552 excess.set(w, excess[w]+(*flow)[f]);
563 void relabel(Node w, int newlevel, VecStack& active,
564 VecNode& level_list, NNMap& left,
565 NNMap& right, int& b, int& k, bool what_heur )
570 Node right_n=right[w];
574 if ( g->valid(right_n) ) {
575 if ( g->valid(left_n) ) {
576 right.set(left_n, right_n);
577 left.set(right_n, left_n);
579 level_list[lev]=right_n;
580 left.set(right_n, INVALID);
583 if ( g->valid(left_n) ) {
584 right.set(left_n, INVALID);
586 level_list[lev]=INVALID;
591 if ( !g->valid(level_list[lev]) ) {
594 for (int i=lev; i!=k ; ) {
595 Node v=level_list[++i];
596 while ( g->valid(v) ) {
600 level_list[i]=INVALID;
602 while ( !active[i].empty() ) {
603 active[i].pop(); //FIXME: ezt szebben kene
615 if ( newlevel == n ) level.set(w,n);
617 level.set(w,++newlevel);
618 active[newlevel].push(w);
619 if ( what_heur ) b=newlevel;
620 if ( k < newlevel ) ++k; //now k=newlevel
621 Node first=level_list[newlevel];
622 if ( g->valid(first) ) left.set(first,w);
625 level_list[newlevel]=w;
632 template<typename MapGraphWrapper>
635 const MapGraphWrapper* g;
636 typename MapGraphWrapper::template NodeMap<int> dist;
638 DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
639 void set(const typename MapGraphWrapper::Node& n, int a) {
642 int operator[](const typename MapGraphWrapper::Node& n)
644 // int get(const typename MapGraphWrapper::Node& n) const {
646 // bool get(const typename MapGraphWrapper::Edge& e) const {
647 // return (dist.get(g->source(e))<dist.get(g->target(e))); }
648 bool operator[](const typename MapGraphWrapper::Edge& e) const {
649 return (dist[g->source(e)]<dist[g->target(e)]);
656 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
657 void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase1( flowEnum fe )
660 int heur0=(int)(H0*n); //time while running 'bound decrease'
661 int heur1=(int)(H1*n); //time while running 'highest label'
662 int heur=heur1; //starting time interval (#of relabels)
666 //It is 0 in case 'bound decrease' and 1 in case 'highest label'
669 //Needed for 'bound decrease', true means no active nodes are above bound b.
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
688 //counting the excess
690 for(g->first(v); g->valid(v); g->next(v)) {
694 for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
696 for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
700 //putting the active nodes into the stack
702 if ( exc > 0 && lev < n && v != t ) active[lev].push(v);
708 //Counting the excess of t
712 for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
714 for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
724 preflowPreproc( fe, active, level_list, left, right );
725 //End of preprocessing
728 //Push/relabel on the highest level active nodes.
731 if ( !what_heur && !end && k > 0 ) {
737 if ( active[b].empty() ) --b;
740 Node w=active[b].top();
742 int newlevel=push(w,active);
743 if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list,
744 left, right, b, k, what_heur);
747 if ( numrelabel >= heur ) {
765 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
766 void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase2()
769 int k=n-2; //bound on the highest level under n containing a node
770 int b=k; //bound on the highest level under n of an active node
774 std::queue<Node> bfs_queue;
777 while (!bfs_queue.empty()) {
779 Node v=bfs_queue.front();
784 for(g->first(e,v); g->valid(e); g->next(e)) {
785 if ( (*capacity)[e] <= (*flow)[e] ) continue;
787 if ( level[u] >= n ) {
790 if ( excess[u] > 0 ) active[l].push(u);
795 for(g->first(f,v); g->valid(f); g->next(f)) {
796 if ( 0 >= (*flow)[f] ) continue;
798 if ( level[u] >= n ) {
801 if ( excess[u] > 0 ) active[l].push(u);
811 if ( active[b].empty() ) --b;
813 Node w=active[b].top();
815 int newlevel=push(w,active);
818 if ( excess[w] > 0 ) {
819 level.set(w,++newlevel);
820 active[newlevel].push(w);
823 } // if stack[b] is nonempty
829 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
830 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath()
832 ResGW res_graph(*g, *capacity, *flow);
835 //ReachedMap level(res_graph);
836 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
837 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
838 bfs.pushAndSetReached(s);
840 typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
841 pred.set(s, INVALID);
843 typename ResGW::template NodeMap<Num> free(res_graph);
845 //searching for augmenting path
846 while ( !bfs.finished() ) {
847 ResGWOutEdgeIt e=bfs;
848 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
849 Node v=res_graph.source(e);
850 Node w=res_graph.target(e);
852 if (res_graph.valid(pred[v])) {
853 free.set(w, std::min(free[v], res_graph.resCap(e)));
855 free.set(w, res_graph.resCap(e));
857 if (res_graph.target(e)==t) { _augment=true; break; }
861 } //end of searching augmenting path
865 Num augment_value=free[t];
866 while (res_graph.valid(pred[n])) {
868 res_graph.augment(e, augment_value);
869 n=res_graph.source(e);
884 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
885 template<typename MutableGraph>
886 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow()
888 typedef MutableGraph MG;
891 ResGW res_graph(*g, *capacity, *flow);
893 //bfs for distances on the residual graph
894 //ReachedMap level(res_graph);
895 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
896 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
897 bfs.pushAndSetReached(s);
898 typename ResGW::template NodeMap<int>
899 dist(res_graph); //filled up with 0's
901 //F will contain the physical copy of the residual graph
902 //with the set of edges which are on shortest paths
904 typename ResGW::template NodeMap<typename MG::Node>
905 res_graph_to_F(res_graph);
907 typename ResGW::NodeIt n;
908 for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
909 res_graph_to_F.set(n, F.addNode());
913 typename MG::Node sF=res_graph_to_F[s];
914 typename MG::Node tF=res_graph_to_F[t];
915 typename MG::template EdgeMap<ResGWEdge> original_edge(F);
916 typename MG::template EdgeMap<Num> residual_capacity(F);
918 while ( !bfs.finished() ) {
919 ResGWOutEdgeIt e=bfs;
920 if (res_graph.valid(e)) {
921 if (bfs.isBNodeNewlyReached()) {
922 dist.set(res_graph.target(e), dist[res_graph.source(e)]+1);
923 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.source(e)], res_graph_to_F[res_graph.target(e)]);
924 original_edge.update();
925 original_edge.set(f, e);
926 residual_capacity.update();
927 residual_capacity.set(f, res_graph.resCap(e));
929 if (dist[res_graph.target(e)]==(dist[res_graph.source(e)]+1)) {
930 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.source(e)], res_graph_to_F[res_graph.target(e)]);
931 original_edge.update();
932 original_edge.set(f, e);
933 residual_capacity.update();
934 residual_capacity.set(f, res_graph.resCap(e));
939 } //computing distances from s in the residual graph
945 //computing blocking flow with dfs
946 DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
947 typename MG::template NodeMap<typename MG::Edge> pred(F);
948 pred.set(sF, INVALID);
949 //invalid iterators for sources
951 typename MG::template NodeMap<Num> free(F);
953 dfs.pushAndSetReached(sF);
954 while (!dfs.finished()) {
956 if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
957 if (dfs.isBNodeNewlyReached()) {
958 typename MG::Node v=F.aNode(dfs);
959 typename MG::Node w=F.bNode(dfs);
961 if (F.valid(pred[v])) {
962 free.set(w, std::min(free[v], residual_capacity[dfs]));
964 free.set(w, residual_capacity[dfs]);
973 F.erase(/*typename MG::OutEdgeIt*/(dfs));
979 typename MG::Node n=tF;
980 Num augment_value=free[tF];
981 while (F.valid(pred[n])) {
982 typename MG::Edge e=pred[n];
983 res_graph.augment(original_edge[e], augment_value);
985 if (residual_capacity[e]==augment_value)
988 residual_capacity.set(e, residual_capacity[e]-augment_value);
1002 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1003 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2()
1005 bool _augment=false;
1007 ResGW res_graph(*g, *capacity, *flow);
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);
1013 bfs.pushAndSetReached(s);
1014 DistanceMap<ResGW> dist(res_graph);
1015 while ( !bfs.finished() ) {
1016 ResGWOutEdgeIt e=bfs;
1017 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
1018 dist.set(res_graph.target(e), dist[res_graph.source(e)]+1);
1021 } //computing distances from s in the residual graph
1023 //Subgraph containing the edges on some shortest paths
1024 ConstMap<typename ResGW::Node, bool> true_map(true);
1025 typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
1026 DistanceMap<ResGW> > FilterResGW;
1027 FilterResGW filter_res_graph(res_graph, true_map, dist);
1029 //Subgraph, which is able to delete edges which are already
1031 typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt>
1032 first_out_edges(filter_res_graph);
1033 typename FilterResGW::NodeIt v;
1034 for(filter_res_graph.first(v); filter_res_graph.valid(v);
1035 filter_res_graph.next(v))
1037 typename FilterResGW::OutEdgeIt e;
1038 filter_res_graph.first(e, v);
1039 first_out_edges.set(v, e);
1041 typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
1042 template NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
1043 ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
1045 bool __augment=true;
1050 //computing blocking flow with dfs
1051 DfsIterator< ErasingResGW,
1052 typename ErasingResGW::template NodeMap<bool> >
1053 dfs(erasing_res_graph);
1054 typename ErasingResGW::
1055 template NodeMap<typename ErasingResGW::OutEdgeIt>
1056 pred(erasing_res_graph);
1057 pred.set(s, INVALID);
1058 //invalid iterators for sources
1060 typename ErasingResGW::template NodeMap<Num>
1061 free1(erasing_res_graph);
1063 dfs.pushAndSetReached(
1064 typename ErasingResGW::Node(
1065 typename FilterResGW::Node(
1066 typename ResGW::Node(s)
1070 while (!dfs.finished()) {
1072 if (erasing_res_graph.valid(
1073 typename ErasingResGW::OutEdgeIt(dfs)))
1075 if (dfs.isBNodeNewlyReached()) {
1077 typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
1078 typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
1080 pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
1081 if (erasing_res_graph.valid(pred[v])) {
1082 free1.set(w, std::min(free1[v], res_graph.resCap(
1083 typename ErasingResGW::OutEdgeIt(dfs))));
1085 free1.set(w, res_graph.resCap(
1086 typename ErasingResGW::OutEdgeIt(dfs)));
1095 erasing_res_graph.erase(dfs);
1101 typename ErasingResGW::Node n=typename FilterResGW::Node(typename ResGW::Node(t));
1102 // typename ResGW::NodeMap<Num> a(res_graph);
1103 // typename ResGW::Node b;
1105 // typename FilterResGW::NodeMap<Num> a1(filter_res_graph);
1106 // typename FilterResGW::Node b1;
1108 // typename ErasingResGW::NodeMap<Num> a2(erasing_res_graph);
1109 // typename ErasingResGW::Node b2;
1111 Num augment_value=free1[n];
1112 while (erasing_res_graph.valid(pred[n])) {
1113 typename ErasingResGW::OutEdgeIt e=pred[n];
1114 res_graph.augment(e, augment_value);
1115 n=erasing_res_graph.source(e);
1116 if (res_graph.resCap(e)==0)
1117 erasing_res_graph.erase(e);
1121 } //while (__augment)
1130 } //END OF NAMESPACE LEMON
1132 #endif //LEMON_MAX_FLOW_H