My notes.
Sources of information and software packages which could be interesting wrt
HUGO.
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_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;
154 MaxFlow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
156 g(&_G), s(_s), t(_t), capacity(&_capacity),
157 flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0),
158 status(AFTER_NOTHING), number_of_augmentations(0) { }
160 ///Runs a maximum flow algorithm.
162 ///Runs a preflow algorithm, which is the fastest maximum flow
163 ///algorithm up-to-date. The default for \c fe is ZERO_FLOW.
164 ///\pre The starting flow must be
165 /// - a constant zero flow if \c fe is \c ZERO_FLOW,
166 /// - an arbitary flow if \c fe is \c GEN_FLOW,
167 /// - an arbitary preflow if \c fe is \c PRE_FLOW,
168 /// - any map if \c fe is NO_FLOW.
169 void run(FlowEnum fe=ZERO_FLOW) {
174 ///Runs a preflow algorithm.
176 ///Runs a preflow algorithm. The preflow algorithms provide the
177 ///fastest way to compute a maximum flow in a directed graph.
178 ///\pre The starting flow must be
179 /// - a constant zero flow if \c fe is \c ZERO_FLOW,
180 /// - an arbitary flow if \c fe is \c GEN_FLOW,
181 /// - an arbitary preflow if \c fe is \c PRE_FLOW,
182 /// - any map if \c fe is NO_FLOW.
183 void preflow(FlowEnum fe) {
190 // list 'level_list' on the nodes on level i implemented by hand
191 // stack 'active' on the active nodes on level i
192 // runs heuristic 'highest label' for H1*n relabels
193 // runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
194 // Parameters H0 and H1 are initialized to 20 and 1.
196 ///Runs the first phase of the preflow algorithm.
198 ///The preflow algorithm consists of two phases, this method runs the
199 ///first phase. After the first phase the maximum flow value and a
200 ///minimum value cut can already be computed, though a maximum flow
201 ///is net yet obtained. So after calling this method \ref flowValue
202 ///and \ref actMinCut gives proper results.
203 ///\warning: \ref minCut, \ref minMinCut and \ref maxMinCut do not
204 ///give minimum value cuts unless calling \ref preflowPhase2.
205 ///\pre The starting flow must be
206 /// - a constant zero flow if \c fe is \c ZERO_FLOW,
207 /// - an arbitary flow if \c fe is \c GEN_FLOW,
208 /// - an arbitary preflow if \c fe is \c PRE_FLOW,
209 /// - any map if \c fe is NO_FLOW.
210 void preflowPhase1(FlowEnum fe);
212 ///Runs the second phase of the preflow algorithm.
214 ///The preflow algorithm consists of two phases, this method runs
215 ///the second phase. After calling \ref preflowPhase1 and then
216 ///\ref preflowPhase2 the methods \ref flowValue, \ref minCut,
217 ///\ref minMinCut and \ref maxMinCut give proper results.
218 ///\pre \ref preflowPhase1 must be called before.
219 void preflowPhase2();
221 /// Starting from a flow, this method searches for an augmenting path
222 /// according to the Edmonds-Karp algorithm
223 /// and augments the flow on if any.
224 /// The return value shows if the augmentation was succesful.
225 bool augmentOnShortestPath();
226 bool augmentOnShortestPath2();
228 /// Starting from a flow, this method searches for an augmenting blocking
229 /// flow according to Dinits' algorithm and augments the flow on if any.
230 /// The blocking flow is computed in a physically constructed
231 /// residual graph of type \c Mutablegraph.
232 /// The return value show sif the augmentation was succesful.
233 template<typename MutableGraph> bool augmentOnBlockingFlow();
235 /// The same as \c augmentOnBlockingFlow<MutableGraph> but the
236 /// residual graph is not constructed physically.
237 /// The return value shows if the augmentation was succesful.
238 bool augmentOnBlockingFlow2();
240 /// Returns the maximum value of a flow.
242 /// Returns the maximum value of a flow, by counting the
243 /// over-flow of the target node \ref t.
244 /// It can be called already after running \ref preflowPhase1.
245 Num flowValue() const {
247 FOR_EACH_INC_LOC(InEdgeIt, e, *g, t) a+=(*flow)[e];
248 FOR_EACH_INC_LOC(OutEdgeIt, e, *g, t) a-=(*flow)[e];
250 //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan
253 ///Returns a minimum value cut after calling \ref preflowPhase1.
255 ///After the first phase of the preflow algorithm the maximum flow
256 ///value and a minimum value cut can already be computed. This
257 ///method can be called after running \ref preflowPhase1 for
258 ///obtaining a minimum value cut.
259 /// \warning Gives proper result only right after calling \ref
261 /// \todo We have to make some status variable which shows the
263 /// of the class. This enables us to determine which methods are valid
264 /// for MinCut computation
265 template<typename _CutMap>
266 void actMinCut(_CutMap& M) const {
269 case AFTER_PRE_FLOW_PHASE_1:
270 for(g->first(v); g->valid(v); g->next(v)) {
278 case AFTER_PRE_FLOW_PHASE_2:
282 case AFTER_AUGMENTING:
283 for(g->first(v); g->valid(v); g->next(v)) {
291 case AFTER_FAST_AUGMENTING:
292 for(g->first(v); g->valid(v); g->next(v)) {
293 if (level[v]==number_of_augmentations) {
303 ///Returns the inclusionwise minimum of the minimum value cuts.
305 ///Sets \c M to the characteristic vector of the minimum value cut
306 ///which is inclusionwise minimum. It is computed by processing
307 ///a bfs from the source node \c s in the residual graph.
308 ///\pre M should be a node map of bools initialized to false.
309 ///\pre \c flow must be a maximum flow.
310 template<typename _CutMap>
311 void minMinCut(_CutMap& M) const {
312 std::queue<Node> queue;
317 while (!queue.empty()) {
318 Node w=queue.front();
322 for(g->first(e,w) ; g->valid(e); g->next(e)) {
324 if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
331 for(g->first(f,w) ; g->valid(f); g->next(f)) {
333 if (!M[v] && (*flow)[f] > 0 ) {
341 ///Returns the inclusionwise maximum of the minimum value cuts.
343 ///Sets \c M to the characteristic vector of the minimum value cut
344 ///which is inclusionwise maximum. It is computed by processing a
345 ///backward bfs from the target node \c t in the residual graph.
346 ///\pre M should be a node map of bools initialized to false.
347 ///\pre \c flow must be a maximum flow.
348 template<typename _CutMap>
349 void maxMinCut(_CutMap& M) const {
352 for(g->first(v) ; g->valid(v); g->next(v)) {
356 std::queue<Node> queue;
361 while (!queue.empty()) {
362 Node w=queue.front();
366 for(g->first(e,w) ; g->valid(e); g->next(e)) {
368 if (M[v] && (*flow)[e] < (*capacity)[e] ) {
375 for(g->first(f,w) ; g->valid(f); g->next(f)) {
377 if (M[v] && (*flow)[f] > 0 ) {
385 ///Returns a minimum value cut.
387 ///Sets \c M to the characteristic vector of a minimum value cut.
388 ///\pre M should be a node map of bools initialized to false.
389 ///\pre \c flow must be a maximum flow.
390 template<typename CutMap>
391 void minCut(CutMap& M) const { minMinCut(M); }
393 ///Resets the source node to \c _s.
395 ///Resets the source node to \c _s.
397 void resetSource(Node _s) { s=_s; status=AFTER_NOTHING; }
399 ///Resets the target node to \c _t.
401 ///Resets the target node to \c _t.
403 void resetTarget(Node _t) { t=_t; status=AFTER_NOTHING; }
405 /// Resets the edge map of the capacities to _cap.
407 /// Resets the edge map of the capacities to _cap.
409 void resetCap(const CapMap& _cap) { capacity=&_cap; status=AFTER_NOTHING; }
411 /// Resets the edge map of the flows to _flow.
413 /// Resets the edge map of the flows to _flow.
415 void resetFlow(FlowMap& _flow) { flow=&_flow; status=AFTER_NOTHING; }
420 int push(Node w, VecStack& active) {
424 int newlevel=n; //bound on the next level of w
427 for(g->first(e,w); g->valid(e); g->next(e)) {
429 if ( (*flow)[e] >= (*capacity)[e] ) continue;
432 if( lev > level[v] ) { //Push is allowed now
434 if ( excess[v]<=0 && v!=t && v!=s ) {
436 active[lev_v].push(v);
439 Num cap=(*capacity)[e];
443 if ( remcap >= exc ) { //A nonsaturating push.
445 flow->set(e, flo+exc);
446 excess.set(v, excess[v]+exc);
450 } else { //A saturating push.
452 excess.set(v, excess[v]+remcap);
455 } else if ( newlevel > level[v] ) newlevel = level[v];
460 for(g->first(e,w); g->valid(e); g->next(e)) {
462 if( (*flow)[e] <= 0 ) continue;
465 if( lev > level[v] ) { //Push is allowed now
467 if ( excess[v]<=0 && v!=t && v!=s ) {
469 active[lev_v].push(v);
474 if ( flo >= exc ) { //A nonsaturating push.
476 flow->set(e, flo-exc);
477 excess.set(v, excess[v]+exc);
480 } else { //A saturating push.
482 excess.set(v, excess[v]+flo);
486 } else if ( newlevel > level[v] ) newlevel = level[v];
489 } // if w still has excess after the out edge for cycle
497 void preflowPreproc(FlowEnum fe, VecStack& active,
498 VecNode& level_list, NNMap& left, NNMap& right)
500 std::queue<Node> bfs_queue;
503 case NO_FLOW: //flow is already set to const zero in this case
506 //Reverse_bfs from t, to find the starting level.
510 while (!bfs_queue.empty()) {
512 Node v=bfs_queue.front();
517 for(g->first(e,v); g->valid(e); g->next(e)) {
519 if ( level[w] == n && w != s ) {
521 Node first=level_list[l];
522 if ( g->valid(first) ) left.set(first,w);
532 for(g->first(e,s); g->valid(e); g->next(e))
534 Num c=(*capacity)[e];
535 if ( c <= 0 ) continue;
537 if ( level[w] < n ) {
538 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
540 excess.set(w, excess[w]+c);
549 //Reverse_bfs from t in the residual graph,
550 //to find the starting level.
554 while (!bfs_queue.empty()) {
556 Node v=bfs_queue.front();
561 for(g->first(e,v); g->valid(e); g->next(e)) {
562 if ( (*capacity)[e] <= (*flow)[e] ) continue;
564 if ( level[w] == n && w != s ) {
566 Node first=level_list[l];
567 if ( g->valid(first) ) left.set(first,w);
575 for(g->first(f,v); g->valid(f); g->next(f)) {
576 if ( 0 >= (*flow)[f] ) continue;
578 if ( level[w] == n && w != s ) {
580 Node first=level_list[l];
581 if ( g->valid(first) ) left.set(first,w);
592 for(g->first(e,s); g->valid(e); g->next(e))
594 Num rem=(*capacity)[e]-(*flow)[e];
595 if ( rem <= 0 ) continue;
597 if ( level[w] < n ) {
598 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
599 flow->set(e, (*capacity)[e]);
600 excess.set(w, excess[w]+rem);
605 for(g->first(f,s); g->valid(f); g->next(f))
607 if ( (*flow)[f] <= 0 ) continue;
609 if ( level[w] < n ) {
610 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
611 excess.set(w, excess[w]+(*flow)[f]);
622 void relabel(Node w, int newlevel, VecStack& active,
623 VecNode& level_list, NNMap& left,
624 NNMap& right, int& b, int& k, bool what_heur )
629 Node right_n=right[w];
633 if ( g->valid(right_n) ) {
634 if ( g->valid(left_n) ) {
635 right.set(left_n, right_n);
636 left.set(right_n, left_n);
638 level_list[lev]=right_n;
639 left.set(right_n, INVALID);
642 if ( g->valid(left_n) ) {
643 right.set(left_n, INVALID);
645 level_list[lev]=INVALID;
650 if ( !g->valid(level_list[lev]) ) {
653 for (int i=lev; i!=k ; ) {
654 Node v=level_list[++i];
655 while ( g->valid(v) ) {
659 level_list[i]=INVALID;
661 while ( !active[i].empty() ) {
662 active[i].pop(); //FIXME: ezt szebben kene
674 if ( newlevel == n ) level.set(w,n);
676 level.set(w,++newlevel);
677 active[newlevel].push(w);
678 if ( what_heur ) b=newlevel;
679 if ( k < newlevel ) ++k; //now k=newlevel
680 Node first=level_list[newlevel];
681 if ( g->valid(first) ) left.set(first,w);
684 level_list[newlevel]=w;
691 template<typename MapGraphWrapper>
694 const MapGraphWrapper* g;
695 typename MapGraphWrapper::template NodeMap<int> dist;
697 DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
698 void set(const typename MapGraphWrapper::Node& n, int a) {
701 int operator[](const typename MapGraphWrapper::Node& n) const {
704 // int get(const typename MapGraphWrapper::Node& n) const {
706 // bool get(const typename MapGraphWrapper::Edge& e) const {
707 // return (dist.get(g->tail(e))<dist.get(g->head(e))); }
708 bool operator[](const typename MapGraphWrapper::Edge& e) const {
709 return (dist[g->tail(e)]<dist[g->head(e)]);
716 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
717 void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase1(FlowEnum fe)
720 int heur0=(int)(H0*n); //time while running 'bound decrease'
721 int heur1=(int)(H1*n); //time while running 'highest label'
722 int heur=heur1; //starting time interval (#of relabels)
726 //It is 0 in case 'bound decrease' and 1 in case 'highest label'
729 //Needed for 'bound decrease', true means no active nodes are above bound
732 int k=n-2; //bound on the highest level under n containing a node
733 int b=k; //bound on the highest level under n of an active node
737 NNMap left(*g, INVALID);
738 NNMap right(*g, INVALID);
739 VecNode level_list(n,INVALID);
740 //List of the nodes in level i<n, set to n.
743 for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
744 //setting each node to level n
746 if ( fe == NO_FLOW ) {
748 for(g->first(e); g->valid(e); g->next(e)) flow->set(e,0);
751 switch (fe) { //computing the excess
755 for(g->first(v); g->valid(v); g->next(v)) {
759 for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
761 for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
765 //putting the active nodes into the stack
767 if ( exc > 0 && lev < n && v != t ) active[lev].push(v);
774 for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
778 for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
780 for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
788 for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
793 preflowPreproc(fe, active, level_list, left, right);
794 //End of preprocessing
797 //Push/relabel on the highest level active nodes.
800 if ( !what_heur && !end && k > 0 ) {
806 if ( active[b].empty() ) --b;
809 Node w=active[b].top();
811 int newlevel=push(w,active);
812 if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list,
813 left, right, b, k, what_heur);
816 if ( numrelabel >= heur ) {
831 status=AFTER_PRE_FLOW_PHASE_1;
836 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
837 void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase2()
840 int k=n-2; //bound on the highest level under n containing a node
841 int b=k; //bound on the highest level under n of an active node
845 std::queue<Node> bfs_queue;
848 while (!bfs_queue.empty()) {
850 Node v=bfs_queue.front();
855 for(g->first(e,v); g->valid(e); g->next(e)) {
856 if ( (*capacity)[e] <= (*flow)[e] ) continue;
858 if ( level[u] >= n ) {
861 if ( excess[u] > 0 ) active[l].push(u);
866 for(g->first(f,v); g->valid(f); g->next(f)) {
867 if ( 0 >= (*flow)[f] ) continue;
869 if ( level[u] >= n ) {
872 if ( excess[u] > 0 ) active[l].push(u);
882 if ( active[b].empty() ) --b;
884 Node w=active[b].top();
886 int newlevel=push(w,active);
889 if ( excess[w] > 0 ) {
890 level.set(w,++newlevel);
891 active[newlevel].push(w);
894 } // if stack[b] is nonempty
897 status=AFTER_PRE_FLOW_PHASE_2;
902 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
903 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath()
905 ResGW res_graph(*g, *capacity, *flow);
908 //ReachedMap level(res_graph);
909 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
910 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
911 bfs.pushAndSetReached(s);
913 typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
914 pred.set(s, INVALID);
916 typename ResGW::template NodeMap<Num> free(res_graph);
918 //searching for augmenting path
919 while ( !bfs.finished() ) {
920 ResGWOutEdgeIt e=bfs;
921 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
922 Node v=res_graph.tail(e);
923 Node w=res_graph.head(e);
925 if (res_graph.valid(pred[v])) {
926 free.set(w, std::min(free[v], res_graph.resCap(e)));
928 free.set(w, res_graph.resCap(e));
930 if (res_graph.head(e)==t) { _augment=true; break; }
934 } //end of searching augmenting path
938 Num augment_value=free[t];
939 while (res_graph.valid(pred[n])) {
941 res_graph.augment(e, augment_value);
946 status=AFTER_AUGMENTING;
951 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
952 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath2()
954 ResGW res_graph(*g, *capacity, *flow);
957 if (status!=AFTER_FAST_AUGMENTING) {
958 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
959 number_of_augmentations=1;
961 ++number_of_augmentations;
963 TrickyReachedMap<ReachedMap>
964 tricky_reached_map(level, number_of_augmentations);
965 //ReachedMap level(res_graph);
966 // FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
967 BfsIterator<ResGW, TrickyReachedMap<ReachedMap> >
968 bfs(res_graph, tricky_reached_map);
969 bfs.pushAndSetReached(s);
971 typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
972 pred.set(s, INVALID);
974 typename ResGW::template NodeMap<Num> free(res_graph);
976 //searching for augmenting path
977 while ( !bfs.finished() ) {
978 ResGWOutEdgeIt e=bfs;
979 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
980 Node v=res_graph.tail(e);
981 Node w=res_graph.head(e);
983 if (res_graph.valid(pred[v])) {
984 free.set(w, std::min(free[v], res_graph.resCap(e)));
986 free.set(w, res_graph.resCap(e));
988 if (res_graph.head(e)==t) { _augment=true; break; }
992 } //end of searching augmenting path
996 Num augment_value=free[t];
997 while (res_graph.valid(pred[n])) {
999 res_graph.augment(e, augment_value);
1000 n=res_graph.tail(e);
1004 status=AFTER_FAST_AUGMENTING;
1009 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1010 template<typename MutableGraph>
1011 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow()
1013 typedef MutableGraph MG;
1014 bool _augment=false;
1016 ResGW res_graph(*g, *capacity, *flow);
1018 //bfs for distances on the residual graph
1019 //ReachedMap level(res_graph);
1020 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1021 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
1022 bfs.pushAndSetReached(s);
1023 typename ResGW::template NodeMap<int>
1024 dist(res_graph); //filled up with 0's
1026 //F will contain the physical copy of the residual graph
1027 //with the set of edges which are on shortest paths
1029 typename ResGW::template NodeMap<typename MG::Node>
1030 res_graph_to_F(res_graph);
1032 typename ResGW::NodeIt n;
1033 for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
1034 res_graph_to_F.set(n, F.addNode());
1038 typename MG::Node sF=res_graph_to_F[s];
1039 typename MG::Node tF=res_graph_to_F[t];
1040 typename MG::template EdgeMap<ResGWEdge> original_edge(F);
1041 typename MG::template EdgeMap<Num> residual_capacity(F);
1043 while ( !bfs.finished() ) {
1044 ResGWOutEdgeIt e=bfs;
1045 if (res_graph.valid(e)) {
1046 if (bfs.isBNodeNewlyReached()) {
1047 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
1048 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
1049 res_graph_to_F[res_graph.head(e)]);
1050 original_edge.update();
1051 original_edge.set(f, e);
1052 residual_capacity.update();
1053 residual_capacity.set(f, res_graph.resCap(e));
1055 if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
1056 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
1057 res_graph_to_F[res_graph.head(e)]);
1058 original_edge.update();
1059 original_edge.set(f, e);
1060 residual_capacity.update();
1061 residual_capacity.set(f, res_graph.resCap(e));
1066 } //computing distances from s in the residual graph
1068 bool __augment=true;
1072 //computing blocking flow with dfs
1073 DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
1074 typename MG::template NodeMap<typename MG::Edge> pred(F);
1075 pred.set(sF, INVALID);
1076 //invalid iterators for sources
1078 typename MG::template NodeMap<Num> free(F);
1080 dfs.pushAndSetReached(sF);
1081 while (!dfs.finished()) {
1083 if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
1084 if (dfs.isBNodeNewlyReached()) {
1085 typename MG::Node v=F.aNode(dfs);
1086 typename MG::Node w=F.bNode(dfs);
1088 if (F.valid(pred[v])) {
1089 free.set(w, std::min(free[v], residual_capacity[dfs]));
1091 free.set(w, residual_capacity[dfs]);
1100 F.erase(/*typename MG::OutEdgeIt*/(dfs));
1106 typename MG::Node n=tF;
1107 Num augment_value=free[tF];
1108 while (F.valid(pred[n])) {
1109 typename MG::Edge e=pred[n];
1110 res_graph.augment(original_edge[e], augment_value);
1112 if (residual_capacity[e]==augment_value)
1115 residual_capacity.set(e, residual_capacity[e]-augment_value);
1121 status=AFTER_AUGMENTING;
1128 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1129 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2()
1131 bool _augment=false;
1133 ResGW res_graph(*g, *capacity, *flow);
1135 //ReachedMap level(res_graph);
1136 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1137 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
1139 bfs.pushAndSetReached(s);
1140 DistanceMap<ResGW> dist(res_graph);
1141 while ( !bfs.finished() ) {
1142 ResGWOutEdgeIt e=bfs;
1143 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
1144 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
1147 } //computing distances from s in the residual graph
1149 //Subgraph containing the edges on some shortest paths
1150 ConstMap<typename ResGW::Node, bool> true_map(true);
1151 typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
1152 DistanceMap<ResGW> > FilterResGW;
1153 FilterResGW filter_res_graph(res_graph, true_map, dist);
1155 //Subgraph, which is able to delete edges which are already
1157 typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt>
1158 first_out_edges(filter_res_graph);
1159 typename FilterResGW::NodeIt v;
1160 for(filter_res_graph.first(v); filter_res_graph.valid(v);
1161 filter_res_graph.next(v))
1163 typename FilterResGW::OutEdgeIt e;
1164 filter_res_graph.first(e, v);
1165 first_out_edges.set(v, e);
1167 typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
1168 template NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
1169 ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
1171 bool __augment=true;
1176 //computing blocking flow with dfs
1177 DfsIterator< ErasingResGW,
1178 typename ErasingResGW::template NodeMap<bool> >
1179 dfs(erasing_res_graph);
1180 typename ErasingResGW::
1181 template NodeMap<typename ErasingResGW::OutEdgeIt>
1182 pred(erasing_res_graph);
1183 pred.set(s, INVALID);
1184 //invalid iterators for sources
1186 typename ErasingResGW::template NodeMap<Num>
1187 free1(erasing_res_graph);
1189 dfs.pushAndSetReached
1191 (typename ErasingResGW::Node
1192 (typename FilterResGW::Node
1193 (typename ResGW::Node(s)
1197 while (!dfs.finished()) {
1199 if (erasing_res_graph.valid(typename ErasingResGW::OutEdgeIt(dfs)))
1201 if (dfs.isBNodeNewlyReached()) {
1203 typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
1204 typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
1206 pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
1207 if (erasing_res_graph.valid(pred[v])) {
1209 (w, std::min(free1[v], res_graph.resCap
1210 (typename ErasingResGW::OutEdgeIt(dfs))));
1213 (w, res_graph.resCap
1214 (typename ErasingResGW::OutEdgeIt(dfs)));
1223 erasing_res_graph.erase(dfs);
1229 typename ErasingResGW::Node
1230 n=typename FilterResGW::Node(typename ResGW::Node(t));
1231 // typename ResGW::NodeMap<Num> a(res_graph);
1232 // typename ResGW::Node b;
1234 // typename FilterResGW::NodeMap<Num> a1(filter_res_graph);
1235 // typename FilterResGW::Node b1;
1237 // typename ErasingResGW::NodeMap<Num> a2(erasing_res_graph);
1238 // typename ErasingResGW::Node b2;
1240 Num augment_value=free1[n];
1241 while (erasing_res_graph.valid(pred[n])) {
1242 typename ErasingResGW::OutEdgeIt e=pred[n];
1243 res_graph.augment(e, augment_value);
1244 n=erasing_res_graph.tail(e);
1245 if (res_graph.resCap(e)==0)
1246 erasing_res_graph.erase(e);
1250 } //while (__augment)
1252 status=AFTER_AUGMENTING;
1259 #endif //HUGO_MAX_FLOW_H