Some comments and minor additions to the AdvancedController.
2 #ifndef LEMON_MAX_FLOW_H
3 #define LEMON_MAX_FLOW_H
9 #include <lemon/graph_wrapper.h>
11 #include <lemon/invalid.h>
12 #include <lemon/maps.h>
13 #include <lemon/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_FAST_AUGMENTING,
126 AFTER_PRE_FLOW_PHASE_1,
127 AFTER_PRE_FLOW_PHASE_2
130 /// Don not needle this flag only if necessary.
132 int number_of_augmentations;
135 template<typename IntMap>
136 class TrickyReachedMap {
139 int* number_of_augmentations;
141 TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) :
142 map(&_map), number_of_augmentations(&_number_of_augmentations) { }
143 void set(const Node& n, bool b) {
145 map->set(n, *number_of_augmentations);
147 map->set(n, *number_of_augmentations-1);
149 bool operator[](const Node& n) const {
150 return (*map)[n]==*number_of_augmentations;
156 ///\todo Document, please.
158 MaxFlow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
160 g(&_G), s(_s), t(_t), capacity(&_capacity),
161 flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0),
162 status(AFTER_NOTHING), number_of_augmentations(0) { }
164 ///Runs a maximum flow algorithm.
166 ///Runs a preflow algorithm, which is the fastest maximum flow
167 ///algorithm up-to-date. The default for \c fe is ZERO_FLOW.
168 ///\pre The starting flow must be
169 /// - a constant zero flow if \c fe is \c ZERO_FLOW,
170 /// - an arbitary flow if \c fe is \c GEN_FLOW,
171 /// - an arbitary preflow if \c fe is \c PRE_FLOW,
172 /// - any map if \c fe is NO_FLOW.
173 void run(FlowEnum fe=ZERO_FLOW) {
178 ///Runs a preflow algorithm.
180 ///Runs a preflow algorithm. The preflow algorithms provide the
181 ///fastest way to compute a maximum flow in a directed graph.
182 ///\pre The starting flow must be
183 /// - a constant zero flow if \c fe is \c ZERO_FLOW,
184 /// - an arbitary flow if \c fe is \c GEN_FLOW,
185 /// - an arbitary preflow if \c fe is \c PRE_FLOW,
186 /// - any map if \c fe is NO_FLOW.
188 ///\todo NO_FLOW should be the default flow.
189 void preflow(FlowEnum fe) {
196 // list 'level_list' on the nodes on level i implemented by hand
197 // stack 'active' on the active nodes on level i
198 // runs heuristic 'highest label' for H1*n relabels
199 // runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
200 // Parameters H0 and H1 are initialized to 20 and 1.
202 ///Runs the first phase of the preflow algorithm.
204 ///The preflow algorithm consists of two phases, this method runs the
205 ///first phase. After the first phase the maximum flow value and a
206 ///minimum value cut can already be computed, though a maximum flow
207 ///is net yet obtained. So after calling this method \ref flowValue
208 ///and \ref actMinCut gives proper results.
209 ///\warning: \ref minCut, \ref minMinCut and \ref maxMinCut do not
210 ///give minimum value cuts unless calling \ref preflowPhase2.
211 ///\pre The starting flow must be
212 /// - a constant zero flow if \c fe is \c ZERO_FLOW,
213 /// - an arbitary flow if \c fe is \c GEN_FLOW,
214 /// - an arbitary preflow if \c fe is \c PRE_FLOW,
215 /// - any map if \c fe is NO_FLOW.
216 void preflowPhase1(FlowEnum fe);
218 ///Runs the second phase of the preflow algorithm.
220 ///The preflow algorithm consists of two phases, this method runs
221 ///the second phase. After calling \ref preflowPhase1 and then
222 ///\ref preflowPhase2 the methods \ref flowValue, \ref minCut,
223 ///\ref minMinCut and \ref maxMinCut give proper results.
224 ///\pre \ref preflowPhase1 must be called before.
225 void preflowPhase2();
227 /// Starting from a flow, this method searches for an augmenting path
228 /// according to the Edmonds-Karp algorithm
229 /// and augments the flow on if any.
230 /// The return value shows if the augmentation was succesful.
231 bool augmentOnShortestPath();
232 bool augmentOnShortestPath2();
234 /// Starting from a flow, this method searches for an augmenting blocking
235 /// flow according to Dinits' algorithm and augments the flow on if any.
236 /// The blocking flow is computed in a physically constructed
237 /// residual graph of type \c Mutablegraph.
238 /// The return value show sif the augmentation was succesful.
239 template<typename MutableGraph> bool augmentOnBlockingFlow();
241 /// The same as \c augmentOnBlockingFlow<MutableGraph> but the
242 /// residual graph is not constructed physically.
243 /// The return value shows if the augmentation was succesful.
244 bool augmentOnBlockingFlow2();
246 /// Returns the maximum value of a flow.
248 /// Returns the maximum value of a flow, by counting the
249 /// over-flow of the target node \ref t.
250 /// It can be called already after running \ref preflowPhase1.
251 Num flowValue() const {
253 FOR_EACH_INC_LOC(InEdgeIt, e, *g, t) a+=(*flow)[e];
254 FOR_EACH_INC_LOC(OutEdgeIt, e, *g, t) a-=(*flow)[e];
256 //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan
259 ///Returns a minimum value cut after calling \ref preflowPhase1.
261 ///After the first phase of the preflow algorithm the maximum flow
262 ///value and a minimum value cut can already be computed. This
263 ///method can be called after running \ref preflowPhase1 for
264 ///obtaining a minimum value cut.
265 /// \warning Gives proper result only right after calling \ref
267 /// \todo We have to make some status variable which shows the
269 /// of the class. This enables us to determine which methods are valid
270 /// for MinCut computation
271 template<typename _CutMap>
272 void actMinCut(_CutMap& M) const {
275 case AFTER_PRE_FLOW_PHASE_1:
276 for(g->first(v); g->valid(v); g->next(v)) {
284 case AFTER_PRE_FLOW_PHASE_2:
288 case AFTER_AUGMENTING:
289 for(g->first(v); g->valid(v); g->next(v)) {
297 case AFTER_FAST_AUGMENTING:
298 for(g->first(v); g->valid(v); g->next(v)) {
299 if (level[v]==number_of_augmentations) {
309 ///Returns the inclusionwise minimum of the minimum value cuts.
311 ///Sets \c M to the characteristic vector of the minimum value cut
312 ///which is inclusionwise minimum. It is computed by processing
313 ///a bfs from the source node \c s in the residual graph.
314 ///\pre M should be a node map of bools initialized to false.
315 ///\pre \c flow must be a maximum flow.
316 template<typename _CutMap>
317 void minMinCut(_CutMap& M) const {
318 std::queue<Node> queue;
323 while (!queue.empty()) {
324 Node w=queue.front();
328 for(g->first(e,w) ; g->valid(e); g->next(e)) {
330 if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
337 for(g->first(f,w) ; g->valid(f); g->next(f)) {
339 if (!M[v] && (*flow)[f] > 0 ) {
347 ///Returns the inclusionwise maximum of the minimum value cuts.
349 ///Sets \c M to the characteristic vector of the minimum value cut
350 ///which is inclusionwise maximum. It is computed by processing a
351 ///backward bfs from the target node \c t in the residual graph.
352 ///\pre M should be a node map of bools initialized to false.
353 ///\pre \c flow must be a maximum flow.
354 template<typename _CutMap>
355 void maxMinCut(_CutMap& M) const {
358 for(g->first(v) ; g->valid(v); g->next(v)) {
362 std::queue<Node> queue;
367 while (!queue.empty()) {
368 Node w=queue.front();
372 for(g->first(e,w) ; g->valid(e); g->next(e)) {
374 if (M[v] && (*flow)[e] < (*capacity)[e] ) {
381 for(g->first(f,w) ; g->valid(f); g->next(f)) {
383 if (M[v] && (*flow)[f] > 0 ) {
391 ///Returns a minimum value cut.
393 ///Sets \c M to the characteristic vector of a minimum value cut.
394 ///\pre M should be a node map of bools initialized to false.
395 ///\pre \c flow must be a maximum flow.
396 template<typename CutMap>
397 void minCut(CutMap& M) const { minMinCut(M); }
399 ///Resets the source node to \c _s.
401 ///Resets the source node to \c _s.
403 void resetSource(Node _s) { s=_s; status=AFTER_NOTHING; }
405 ///Resets the target node to \c _t.
407 ///Resets the target node to \c _t.
409 void resetTarget(Node _t) { t=_t; status=AFTER_NOTHING; }
411 /// Resets the edge map of the capacities to _cap.
413 /// Resets the edge map of the capacities to _cap.
415 void resetCap(const CapMap& _cap) { capacity=&_cap; status=AFTER_NOTHING; }
417 /// Resets the edge map of the flows to _flow.
419 /// Resets the edge map of the flows to _flow.
421 void resetFlow(FlowMap& _flow) { flow=&_flow; status=AFTER_NOTHING; }
426 int push(Node w, VecStack& active) {
430 int newlevel=n; //bound on the next level of w
433 for(g->first(e,w); g->valid(e); g->next(e)) {
435 if ( (*flow)[e] >= (*capacity)[e] ) continue;
438 if( lev > level[v] ) { //Push is allowed now
440 if ( excess[v]<=0 && v!=t && v!=s ) {
442 active[lev_v].push(v);
445 Num cap=(*capacity)[e];
449 if ( remcap >= exc ) { //A nonsaturating push.
451 flow->set(e, flo+exc);
452 excess.set(v, excess[v]+exc);
456 } else { //A saturating push.
458 excess.set(v, excess[v]+remcap);
461 } else if ( newlevel > level[v] ) newlevel = level[v];
466 for(g->first(e,w); g->valid(e); g->next(e)) {
468 if( (*flow)[e] <= 0 ) continue;
471 if( lev > level[v] ) { //Push is allowed now
473 if ( excess[v]<=0 && v!=t && v!=s ) {
475 active[lev_v].push(v);
480 if ( flo >= exc ) { //A nonsaturating push.
482 flow->set(e, flo-exc);
483 excess.set(v, excess[v]+exc);
486 } else { //A saturating push.
488 excess.set(v, excess[v]+flo);
492 } else if ( newlevel > level[v] ) newlevel = level[v];
495 } // if w still has excess after the out edge for cycle
503 void preflowPreproc(FlowEnum fe, VecStack& active,
504 VecNode& level_list, NNMap& left, NNMap& right)
506 std::queue<Node> bfs_queue;
509 case NO_FLOW: //flow is already set to const zero in this case
512 //Reverse_bfs from t, to find the starting level.
516 while (!bfs_queue.empty()) {
518 Node v=bfs_queue.front();
523 for(g->first(e,v); g->valid(e); g->next(e)) {
525 if ( level[w] == n && w != s ) {
527 Node first=level_list[l];
528 if ( g->valid(first) ) left.set(first,w);
538 for(g->first(e,s); g->valid(e); g->next(e))
540 Num c=(*capacity)[e];
541 if ( c <= 0 ) continue;
543 if ( level[w] < n ) {
544 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
546 excess.set(w, excess[w]+c);
555 //Reverse_bfs from t in the residual graph,
556 //to find the starting level.
560 while (!bfs_queue.empty()) {
562 Node v=bfs_queue.front();
567 for(g->first(e,v); g->valid(e); g->next(e)) {
568 if ( (*capacity)[e] <= (*flow)[e] ) continue;
570 if ( level[w] == n && w != s ) {
572 Node first=level_list[l];
573 if ( g->valid(first) ) left.set(first,w);
581 for(g->first(f,v); g->valid(f); g->next(f)) {
582 if ( 0 >= (*flow)[f] ) continue;
584 if ( level[w] == n && w != s ) {
586 Node first=level_list[l];
587 if ( g->valid(first) ) left.set(first,w);
598 for(g->first(e,s); g->valid(e); g->next(e))
600 Num rem=(*capacity)[e]-(*flow)[e];
601 if ( rem <= 0 ) continue;
603 if ( level[w] < n ) {
604 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
605 flow->set(e, (*capacity)[e]);
606 excess.set(w, excess[w]+rem);
611 for(g->first(f,s); g->valid(f); g->next(f))
613 if ( (*flow)[f] <= 0 ) continue;
615 if ( level[w] < n ) {
616 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
617 excess.set(w, excess[w]+(*flow)[f]);
628 void relabel(Node w, int newlevel, VecStack& active,
629 VecNode& level_list, NNMap& left,
630 NNMap& right, int& b, int& k, bool what_heur )
635 Node right_n=right[w];
639 if ( g->valid(right_n) ) {
640 if ( g->valid(left_n) ) {
641 right.set(left_n, right_n);
642 left.set(right_n, left_n);
644 level_list[lev]=right_n;
645 left.set(right_n, INVALID);
648 if ( g->valid(left_n) ) {
649 right.set(left_n, INVALID);
651 level_list[lev]=INVALID;
656 if ( !g->valid(level_list[lev]) ) {
659 for (int i=lev; i!=k ; ) {
660 Node v=level_list[++i];
661 while ( g->valid(v) ) {
665 level_list[i]=INVALID;
667 while ( !active[i].empty() ) {
668 active[i].pop(); //FIXME: ezt szebben kene
680 if ( newlevel == n ) level.set(w,n);
682 level.set(w,++newlevel);
683 active[newlevel].push(w);
684 if ( what_heur ) b=newlevel;
685 if ( k < newlevel ) ++k; //now k=newlevel
686 Node first=level_list[newlevel];
687 if ( g->valid(first) ) left.set(first,w);
690 level_list[newlevel]=w;
697 template<typename MapGraphWrapper>
700 const MapGraphWrapper* g;
701 typename MapGraphWrapper::template NodeMap<int> dist;
703 DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
704 void set(const typename MapGraphWrapper::Node& n, int a) {
707 int operator[](const typename MapGraphWrapper::Node& n) const {
710 // int get(const typename MapGraphWrapper::Node& n) const {
712 // bool get(const typename MapGraphWrapper::Edge& e) const {
713 // return (dist.get(g->source(e))<dist.get(g->target(e))); }
714 bool operator[](const typename MapGraphWrapper::Edge& e) const {
715 return (dist[g->source(e)]<dist[g->target(e)]);
722 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
723 void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase1(FlowEnum fe)
726 int heur0=(int)(H0*n); //time while running 'bound decrease'
727 int heur1=(int)(H1*n); //time while running 'highest label'
728 int heur=heur1; //starting time interval (#of relabels)
732 //It is 0 in case 'bound decrease' and 1 in case 'highest label'
735 //Needed for 'bound decrease', true means no active nodes are above bound
738 int k=n-2; //bound on the highest level under n containing a node
739 int b=k; //bound on the highest level under n of an active node
743 NNMap left(*g, INVALID);
744 NNMap right(*g, INVALID);
745 VecNode level_list(n,INVALID);
746 //List of the nodes in level i<n, set to n.
749 for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
750 //setting each node to level n
752 if ( fe == NO_FLOW ) {
754 for(g->first(e); g->valid(e); g->next(e)) flow->set(e,0);
757 switch (fe) { //computing the excess
761 for(g->first(v); g->valid(v); g->next(v)) {
765 for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
767 for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
771 //putting the active nodes into the stack
773 if ( exc > 0 && lev < n && v != t ) active[lev].push(v);
780 for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
784 for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
786 for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
794 for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
799 preflowPreproc(fe, active, level_list, left, right);
800 //End of preprocessing
803 //Push/relabel on the highest level active nodes.
806 if ( !what_heur && !end && k > 0 ) {
812 if ( active[b].empty() ) --b;
815 Node w=active[b].top();
817 int newlevel=push(w,active);
818 if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list,
819 left, right, b, k, what_heur);
822 if ( numrelabel >= heur ) {
837 status=AFTER_PRE_FLOW_PHASE_1;
842 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
843 void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase2()
846 int k=n-2; //bound on the highest level under n containing a node
847 int b=k; //bound on the highest level under n of an active node
851 std::queue<Node> bfs_queue;
854 while (!bfs_queue.empty()) {
856 Node v=bfs_queue.front();
861 for(g->first(e,v); g->valid(e); g->next(e)) {
862 if ( (*capacity)[e] <= (*flow)[e] ) continue;
864 if ( level[u] >= n ) {
867 if ( excess[u] > 0 ) active[l].push(u);
872 for(g->first(f,v); g->valid(f); g->next(f)) {
873 if ( 0 >= (*flow)[f] ) continue;
875 if ( level[u] >= n ) {
878 if ( excess[u] > 0 ) active[l].push(u);
888 if ( active[b].empty() ) --b;
890 Node w=active[b].top();
892 int newlevel=push(w,active);
895 if ( excess[w] > 0 ) {
896 level.set(w,++newlevel);
897 active[newlevel].push(w);
900 } // if stack[b] is nonempty
903 status=AFTER_PRE_FLOW_PHASE_2;
908 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
909 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath()
911 ResGW res_graph(*g, *capacity, *flow);
914 //ReachedMap level(res_graph);
915 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
916 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
917 bfs.pushAndSetReached(s);
919 typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
920 pred.set(s, INVALID);
922 typename ResGW::template NodeMap<Num> free(res_graph);
924 //searching for augmenting path
925 while ( !bfs.finished() ) {
926 ResGWOutEdgeIt e=bfs;
927 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
928 Node v=res_graph.source(e);
929 Node w=res_graph.target(e);
931 if (res_graph.valid(pred[v])) {
932 free.set(w, std::min(free[v], res_graph.resCap(e)));
934 free.set(w, res_graph.resCap(e));
936 if (res_graph.target(e)==t) { _augment=true; break; }
940 } //end of searching augmenting path
944 Num augment_value=free[t];
945 while (res_graph.valid(pred[n])) {
947 res_graph.augment(e, augment_value);
948 n=res_graph.source(e);
952 status=AFTER_AUGMENTING;
957 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
958 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath2()
960 ResGW res_graph(*g, *capacity, *flow);
963 if (status!=AFTER_FAST_AUGMENTING) {
964 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
965 number_of_augmentations=1;
967 ++number_of_augmentations;
969 TrickyReachedMap<ReachedMap>
970 tricky_reached_map(level, number_of_augmentations);
971 //ReachedMap level(res_graph);
972 // FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
973 BfsIterator<ResGW, TrickyReachedMap<ReachedMap> >
974 bfs(res_graph, tricky_reached_map);
975 bfs.pushAndSetReached(s);
977 typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
978 pred.set(s, INVALID);
980 typename ResGW::template NodeMap<Num> free(res_graph);
982 //searching for augmenting path
983 while ( !bfs.finished() ) {
984 ResGWOutEdgeIt e=bfs;
985 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
986 Node v=res_graph.source(e);
987 Node w=res_graph.target(e);
989 if (res_graph.valid(pred[v])) {
990 free.set(w, std::min(free[v], res_graph.resCap(e)));
992 free.set(w, res_graph.resCap(e));
994 if (res_graph.target(e)==t) { _augment=true; break; }
998 } //end of searching augmenting path
1002 Num augment_value=free[t];
1003 while (res_graph.valid(pred[n])) {
1004 ResGWEdge e=pred[n];
1005 res_graph.augment(e, augment_value);
1006 n=res_graph.source(e);
1010 status=AFTER_FAST_AUGMENTING;
1015 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1016 template<typename MutableGraph>
1017 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow()
1019 typedef MutableGraph MG;
1020 bool _augment=false;
1022 ResGW res_graph(*g, *capacity, *flow);
1024 //bfs for distances on the residual graph
1025 //ReachedMap level(res_graph);
1026 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1027 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
1028 bfs.pushAndSetReached(s);
1029 typename ResGW::template NodeMap<int>
1030 dist(res_graph); //filled up with 0's
1032 //F will contain the physical copy of the residual graph
1033 //with the set of edges which are on shortest paths
1035 typename ResGW::template NodeMap<typename MG::Node>
1036 res_graph_to_F(res_graph);
1038 typename ResGW::NodeIt n;
1039 for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
1040 res_graph_to_F.set(n, F.addNode());
1044 typename MG::Node sF=res_graph_to_F[s];
1045 typename MG::Node tF=res_graph_to_F[t];
1046 typename MG::template EdgeMap<ResGWEdge> original_edge(F);
1047 typename MG::template EdgeMap<Num> residual_capacity(F);
1049 while ( !bfs.finished() ) {
1050 ResGWOutEdgeIt e=bfs;
1051 if (res_graph.valid(e)) {
1052 if (bfs.isBNodeNewlyReached()) {
1053 dist.set(res_graph.target(e), dist[res_graph.source(e)]+1);
1054 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.source(e)],
1055 res_graph_to_F[res_graph.target(e)]);
1056 original_edge.update();
1057 original_edge.set(f, e);
1058 residual_capacity.update();
1059 residual_capacity.set(f, res_graph.resCap(e));
1061 if (dist[res_graph.target(e)]==(dist[res_graph.source(e)]+1)) {
1062 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.source(e)],
1063 res_graph_to_F[res_graph.target(e)]);
1064 original_edge.update();
1065 original_edge.set(f, e);
1066 residual_capacity.update();
1067 residual_capacity.set(f, res_graph.resCap(e));
1072 } //computing distances from s in the residual graph
1074 bool __augment=true;
1078 //computing blocking flow with dfs
1079 DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
1080 typename MG::template NodeMap<typename MG::Edge> pred(F);
1081 pred.set(sF, INVALID);
1082 //invalid iterators for sources
1084 typename MG::template NodeMap<Num> free(F);
1086 dfs.pushAndSetReached(sF);
1087 while (!dfs.finished()) {
1089 if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
1090 if (dfs.isBNodeNewlyReached()) {
1091 typename MG::Node v=F.aNode(dfs);
1092 typename MG::Node w=F.bNode(dfs);
1094 if (F.valid(pred[v])) {
1095 free.set(w, std::min(free[v], residual_capacity[dfs]));
1097 free.set(w, residual_capacity[dfs]);
1106 F.erase(/*typename MG::OutEdgeIt*/(dfs));
1112 typename MG::Node n=tF;
1113 Num augment_value=free[tF];
1114 while (F.valid(pred[n])) {
1115 typename MG::Edge e=pred[n];
1116 res_graph.augment(original_edge[e], augment_value);
1118 if (residual_capacity[e]==augment_value)
1121 residual_capacity.set(e, residual_capacity[e]-augment_value);
1127 status=AFTER_AUGMENTING;
1134 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1135 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2()
1137 bool _augment=false;
1139 ResGW res_graph(*g, *capacity, *flow);
1141 //ReachedMap level(res_graph);
1142 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1143 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
1145 bfs.pushAndSetReached(s);
1146 DistanceMap<ResGW> dist(res_graph);
1147 while ( !bfs.finished() ) {
1148 ResGWOutEdgeIt e=bfs;
1149 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
1150 dist.set(res_graph.target(e), dist[res_graph.source(e)]+1);
1153 } //computing distances from s in the residual graph
1155 //Subgraph containing the edges on some shortest paths
1156 ConstMap<typename ResGW::Node, bool> true_map(true);
1157 typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
1158 DistanceMap<ResGW> > FilterResGW;
1159 FilterResGW filter_res_graph(res_graph, true_map, dist);
1161 //Subgraph, which is able to delete edges which are already
1163 typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt>
1164 first_out_edges(filter_res_graph);
1165 typename FilterResGW::NodeIt v;
1166 for(filter_res_graph.first(v); filter_res_graph.valid(v);
1167 filter_res_graph.next(v))
1169 typename FilterResGW::OutEdgeIt e;
1170 filter_res_graph.first(e, v);
1171 first_out_edges.set(v, e);
1173 typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
1174 template NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
1175 ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
1177 bool __augment=true;
1182 //computing blocking flow with dfs
1183 DfsIterator< ErasingResGW,
1184 typename ErasingResGW::template NodeMap<bool> >
1185 dfs(erasing_res_graph);
1186 typename ErasingResGW::
1187 template NodeMap<typename ErasingResGW::OutEdgeIt>
1188 pred(erasing_res_graph);
1189 pred.set(s, INVALID);
1190 //invalid iterators for sources
1192 typename ErasingResGW::template NodeMap<Num>
1193 free1(erasing_res_graph);
1195 dfs.pushAndSetReached
1197 (typename ErasingResGW::Node
1198 (typename FilterResGW::Node
1199 (typename ResGW::Node(s)
1203 while (!dfs.finished()) {
1205 if (erasing_res_graph.valid(typename ErasingResGW::OutEdgeIt(dfs)))
1207 if (dfs.isBNodeNewlyReached()) {
1209 typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
1210 typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
1212 pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
1213 if (erasing_res_graph.valid(pred[v])) {
1215 (w, std::min(free1[v], res_graph.resCap
1216 (typename ErasingResGW::OutEdgeIt(dfs))));
1219 (w, res_graph.resCap
1220 (typename ErasingResGW::OutEdgeIt(dfs)));
1229 erasing_res_graph.erase(dfs);
1235 typename ErasingResGW::Node
1236 n=typename FilterResGW::Node(typename ResGW::Node(t));
1237 // typename ResGW::NodeMap<Num> a(res_graph);
1238 // typename ResGW::Node b;
1240 // typename FilterResGW::NodeMap<Num> a1(filter_res_graph);
1241 // typename FilterResGW::Node b1;
1243 // typename ErasingResGW::NodeMap<Num> a2(erasing_res_graph);
1244 // typename ErasingResGW::Node b2;
1246 Num augment_value=free1[n];
1247 while (erasing_res_graph.valid(pred[n])) {
1248 typename ErasingResGW::OutEdgeIt e=pred[n];
1249 res_graph.augment(e, augment_value);
1250 n=erasing_res_graph.source(e);
1251 if (res_graph.resCap(e)==0)
1252 erasing_res_graph.erase(e);
1256 } //while (__augment)
1258 status=AFTER_AUGMENTING;
1265 #endif //LEMON_MAX_FLOW_H