Cooorected some eroorrs.
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 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.
124 AFTER_PRE_FLOW_PHASE_1,
125 AFTER_PRE_FLOW_PHASE_2
128 /// Don not needle this flag only if necessary.
130 int number_of_augmentations;
133 template<typename IntMap>
134 class TrickyReachedMap {
137 int* number_of_augmentations;
139 TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) :
140 map(&_map), number_of_augmentations(&_number_of_augmentations) { }
141 void set(const Node& n, bool b) {
142 map->set(n, *number_of_augmentations);
144 bool operator[](const Node& n) const {
145 return (*map)[n]==*number_of_augmentations;
149 MaxFlow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
151 g(&_G), s(_s), t(_t), capacity(&_capacity),
152 flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0),
153 status(AFTER_NOTHING), number_of_augmentations(0) { }
155 ///Runs a maximum flow algorithm.
157 ///Runs a preflow algorithm, which is the fastest maximum flow
158 ///algorithm up-to-date. The default for \c fe is ZERO_FLOW.
159 ///\pre The starting flow must be
160 /// - a constant zero flow if \c fe is \c ZERO_FLOW,
161 /// - an arbitary flow if \c fe is \c GEN_FLOW,
162 /// - an arbitary preflow if \c fe is \c PRE_FLOW,
163 /// - any map if \c fe is NO_FLOW.
164 void run(FlowEnum fe=ZERO_FLOW) {
169 ///Runs a preflow algorithm.
171 ///Runs a preflow algorithm. The preflow algorithms provide the
172 ///fastest way to compute a maximum flow in a directed graph.
173 ///\pre The starting flow must be
174 /// - a constant zero flow if \c fe is \c ZERO_FLOW,
175 /// - an arbitary flow if \c fe is \c GEN_FLOW,
176 /// - an arbitary preflow if \c fe is \c PRE_FLOW,
177 /// - any map if \c fe is NO_FLOW.
178 void preflow(FlowEnum fe) {
185 // list 'level_list' on the nodes on level i implemented by hand
186 // stack 'active' on the active nodes on level i
187 // runs heuristic 'highest label' for H1*n relabels
188 // runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
189 // Parameters H0 and H1 are initialized to 20 and 1.
191 ///Runs the first phase of the preflow algorithm.
193 ///The preflow algorithm consists of two phases, this method runs the
194 ///first phase. After the first phase the maximum flow value and a
195 ///minimum value cut can already be computed, though a maximum flow
196 ///is net yet obtained. So after calling this method \ref flowValue
197 ///and \ref actMinCut gives proper results.
198 ///\warning: \ref minCut, \ref minMinCut and \ref maxMinCut do not
199 ///give minimum value cuts unless calling \ref preflowPhase2.
200 ///\pre The starting flow must be
201 /// - a constant zero flow if \c fe is \c ZERO_FLOW,
202 /// - an arbitary flow if \c fe is \c GEN_FLOW,
203 /// - an arbitary preflow if \c fe is \c PRE_FLOW,
204 /// - any map if \c fe is NO_FLOW.
205 void preflowPhase1(FlowEnum fe);
207 ///Runs the second phase of the preflow algorithm.
209 ///The preflow algorithm consists of two phases, this method runs
210 ///the second phase. After calling \ref preflowPhase1 and then
211 ///\ref preflowPhase2 the methods \ref flowValue, \ref minCut,
212 ///\ref minMinCut and \ref maxMinCut give proper results.
213 ///\pre \ref preflowPhase1 must be called before.
214 void preflowPhase2();
216 /// Starting from a flow, this method searches for an augmenting path
217 /// according to the Edmonds-Karp algorithm
218 /// and augments the flow on if any.
219 /// The return value shows if the augmentation was succesful.
220 bool augmentOnShortestPath();
221 bool augmentOnShortestPath2();
223 /// Starting from a flow, this method searches for an augmenting blocking
224 /// flow according to Dinits' algorithm and augments the flow on if any.
225 /// The blocking flow is computed in a physically constructed
226 /// residual graph of type \c Mutablegraph.
227 /// The return value show sif the augmentation was succesful.
228 template<typename MutableGraph> bool augmentOnBlockingFlow();
230 /// The same as \c augmentOnBlockingFlow<MutableGraph> but the
231 /// residual graph is not constructed physically.
232 /// The return value shows if the augmentation was succesful.
233 bool augmentOnBlockingFlow2();
235 /// Returns the maximum value of a flow.
237 /// Returns the maximum value of a flow, by counting the
238 /// over-flow of the target node \ref t.
239 /// It can be called already after running \ref preflowPhase1.
240 Num flowValue() const {
242 FOR_EACH_INC_LOC(InEdgeIt, e, *g, t) a+=(*flow)[e];
243 FOR_EACH_INC_LOC(OutEdgeIt, e, *g, t) a-=(*flow)[e];
245 //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan
248 ///Returns a minimum value cut after calling \ref preflowPhase1.
250 ///After the first phase of the preflow algorithm the maximum flow
251 ///value and a minimum value cut can already be computed. This
252 ///method can be called after running \ref preflowPhase1 for
253 ///obtaining a minimum value cut.
254 /// \warning Gives proper result only right after calling \ref
256 /// \todo We have to make some status variable which shows the
258 /// of the class. This enables us to determine which methods are valid
259 /// for MinCut computation
260 template<typename _CutMap>
261 void actMinCut(_CutMap& M) const {
264 case AFTER_PRE_FLOW_PHASE_1:
265 for(g->first(v); g->valid(v); g->next(v)) {
273 case AFTER_PRE_FLOW_PHASE_2:
277 case AFTER_AUGMENTING:
278 for(g->first(v); g->valid(v); g->next(v)) {
289 ///Returns the inclusionwise minimum of the minimum value cuts.
291 ///Sets \c M to the characteristic vector of the minimum value cut
292 ///which is inclusionwise minimum. It is computed by processing
293 ///a bfs from the source node \c s in the residual graph.
294 ///\pre M should be a node map of bools initialized to false.
295 ///\pre \c flow must be a maximum flow.
296 template<typename _CutMap>
297 void minMinCut(_CutMap& M) const {
298 std::queue<Node> queue;
303 while (!queue.empty()) {
304 Node w=queue.front();
308 for(g->first(e,w) ; g->valid(e); g->next(e)) {
310 if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
317 for(g->first(f,w) ; g->valid(f); g->next(f)) {
319 if (!M[v] && (*flow)[f] > 0 ) {
327 ///Returns the inclusionwise maximum of the minimum value cuts.
329 ///Sets \c M to the characteristic vector of the minimum value cut
330 ///which is inclusionwise maximum. It is computed by processing a
331 ///backward bfs from the target node \c t in the residual graph.
332 ///\pre M should be a node map of bools initialized to false.
333 ///\pre \c flow must be a maximum flow.
334 template<typename _CutMap>
335 void maxMinCut(_CutMap& M) const {
338 for(g->first(v) ; g->valid(v); g->next(v)) {
342 std::queue<Node> queue;
347 while (!queue.empty()) {
348 Node w=queue.front();
352 for(g->first(e,w) ; g->valid(e); g->next(e)) {
354 if (M[v] && (*flow)[e] < (*capacity)[e] ) {
361 for(g->first(f,w) ; g->valid(f); g->next(f)) {
363 if (M[v] && (*flow)[f] > 0 ) {
371 ///Returns a minimum value cut.
373 ///Sets \c M to the characteristic vector of a minimum value cut.
374 ///\pre M should be a node map of bools initialized to false.
375 ///\pre \c flow must be a maximum flow.
376 template<typename CutMap>
377 void minCut(CutMap& M) const { minMinCut(M); }
379 ///Resets the source node to \c _s.
381 ///Resets the source node to \c _s.
383 void resetSource(Node _s) { s=_s; status=AFTER_NOTHING; }
385 ///Resets the target node to \c _t.
387 ///Resets the target node to \c _t.
389 void resetTarget(Node _t) { t=_t; status=AFTER_NOTHING; }
391 /// Resets the edge map of the capacities to _cap.
393 /// Resets the edge map of the capacities to _cap.
395 void resetCap(const CapMap& _cap) { capacity=&_cap; status=AFTER_NOTHING; }
397 /// Resets the edge map of the flows to _flow.
399 /// Resets the edge map of the flows to _flow.
401 void resetFlow(FlowMap& _flow) { flow=&_flow; status=AFTER_NOTHING; }
406 int push(Node w, VecStack& active) {
410 int newlevel=n; //bound on the next level of w
413 for(g->first(e,w); g->valid(e); g->next(e)) {
415 if ( (*flow)[e] >= (*capacity)[e] ) continue;
418 if( lev > level[v] ) { //Push is allowed now
420 if ( excess[v]<=0 && v!=t && v!=s ) {
422 active[lev_v].push(v);
425 Num cap=(*capacity)[e];
429 if ( remcap >= exc ) { //A nonsaturating push.
431 flow->set(e, flo+exc);
432 excess.set(v, excess[v]+exc);
436 } else { //A saturating push.
438 excess.set(v, excess[v]+remcap);
441 } else if ( newlevel > level[v] ) newlevel = level[v];
446 for(g->first(e,w); g->valid(e); g->next(e)) {
448 if( (*flow)[e] <= 0 ) continue;
451 if( lev > level[v] ) { //Push is allowed now
453 if ( excess[v]<=0 && v!=t && v!=s ) {
455 active[lev_v].push(v);
460 if ( flo >= exc ) { //A nonsaturating push.
462 flow->set(e, flo-exc);
463 excess.set(v, excess[v]+exc);
466 } else { //A saturating push.
468 excess.set(v, excess[v]+flo);
472 } else if ( newlevel > level[v] ) newlevel = level[v];
475 } // if w still has excess after the out edge for cycle
483 void preflowPreproc(FlowEnum fe, VecStack& active,
484 VecNode& level_list, NNMap& left, NNMap& right)
486 std::queue<Node> bfs_queue;
489 case NO_FLOW: //flow is already set to const zero in this case
492 //Reverse_bfs from t, to find the starting level.
496 while (!bfs_queue.empty()) {
498 Node v=bfs_queue.front();
503 for(g->first(e,v); g->valid(e); g->next(e)) {
505 if ( level[w] == n && w != s ) {
507 Node first=level_list[l];
508 if ( g->valid(first) ) left.set(first,w);
518 for(g->first(e,s); g->valid(e); g->next(e))
520 Num c=(*capacity)[e];
521 if ( c <= 0 ) continue;
523 if ( level[w] < n ) {
524 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
526 excess.set(w, excess[w]+c);
535 //Reverse_bfs from t in the residual graph,
536 //to find the starting level.
540 while (!bfs_queue.empty()) {
542 Node v=bfs_queue.front();
547 for(g->first(e,v); g->valid(e); g->next(e)) {
548 if ( (*capacity)[e] <= (*flow)[e] ) continue;
550 if ( level[w] == n && w != s ) {
552 Node first=level_list[l];
553 if ( g->valid(first) ) left.set(first,w);
561 for(g->first(f,v); g->valid(f); g->next(f)) {
562 if ( 0 >= (*flow)[f] ) continue;
564 if ( level[w] == n && w != s ) {
566 Node first=level_list[l];
567 if ( g->valid(first) ) left.set(first,w);
578 for(g->first(e,s); g->valid(e); g->next(e))
580 Num rem=(*capacity)[e]-(*flow)[e];
581 if ( rem <= 0 ) continue;
583 if ( level[w] < n ) {
584 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
585 flow->set(e, (*capacity)[e]);
586 excess.set(w, excess[w]+rem);
591 for(g->first(f,s); g->valid(f); g->next(f))
593 if ( (*flow)[f] <= 0 ) continue;
595 if ( level[w] < n ) {
596 if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
597 excess.set(w, excess[w]+(*flow)[f]);
608 void relabel(Node w, int newlevel, VecStack& active,
609 VecNode& level_list, NNMap& left,
610 NNMap& right, int& b, int& k, bool what_heur )
615 Node right_n=right[w];
619 if ( g->valid(right_n) ) {
620 if ( g->valid(left_n) ) {
621 right.set(left_n, right_n);
622 left.set(right_n, left_n);
624 level_list[lev]=right_n;
625 left.set(right_n, INVALID);
628 if ( g->valid(left_n) ) {
629 right.set(left_n, INVALID);
631 level_list[lev]=INVALID;
636 if ( !g->valid(level_list[lev]) ) {
639 for (int i=lev; i!=k ; ) {
640 Node v=level_list[++i];
641 while ( g->valid(v) ) {
645 level_list[i]=INVALID;
647 while ( !active[i].empty() ) {
648 active[i].pop(); //FIXME: ezt szebben kene
660 if ( newlevel == n ) level.set(w,n);
662 level.set(w,++newlevel);
663 active[newlevel].push(w);
664 if ( what_heur ) b=newlevel;
665 if ( k < newlevel ) ++k; //now k=newlevel
666 Node first=level_list[newlevel];
667 if ( g->valid(first) ) left.set(first,w);
670 level_list[newlevel]=w;
677 template<typename MapGraphWrapper>
680 const MapGraphWrapper* g;
681 typename MapGraphWrapper::template NodeMap<int> dist;
683 DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
684 void set(const typename MapGraphWrapper::Node& n, int a) {
687 int operator[](const typename MapGraphWrapper::Node& n) const {
690 // int get(const typename MapGraphWrapper::Node& n) const {
692 // bool get(const typename MapGraphWrapper::Edge& e) const {
693 // return (dist.get(g->tail(e))<dist.get(g->head(e))); }
694 bool operator[](const typename MapGraphWrapper::Edge& e) const {
695 return (dist[g->tail(e)]<dist[g->head(e)]);
702 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
703 void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase1(FlowEnum fe)
706 int heur0=(int)(H0*n); //time while running 'bound decrease'
707 int heur1=(int)(H1*n); //time while running 'highest label'
708 int heur=heur1; //starting time interval (#of relabels)
712 //It is 0 in case 'bound decrease' and 1 in case 'highest label'
715 //Needed for 'bound decrease', true means no active nodes are above bound
718 int k=n-2; //bound on the highest level under n containing a node
719 int b=k; //bound on the highest level under n of an active node
723 NNMap left(*g, INVALID);
724 NNMap right(*g, INVALID);
725 VecNode level_list(n,INVALID);
726 //List of the nodes in level i<n, set to n.
729 for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
730 //setting each node to level n
732 if ( fe == NO_FLOW ) {
734 for(g->first(e); g->valid(e); g->next(e)) flow->set(e,0);
737 switch (fe) { //computing the excess
741 for(g->first(v); g->valid(v); g->next(v)) {
745 for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
747 for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
751 //putting the active nodes into the stack
753 if ( exc > 0 && lev < n && v != t ) active[lev].push(v);
760 for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
764 for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
766 for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
774 for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
779 preflowPreproc(fe, active, level_list, left, right);
780 //End of preprocessing
783 //Push/relabel on the highest level active nodes.
786 if ( !what_heur && !end && k > 0 ) {
792 if ( active[b].empty() ) --b;
795 Node w=active[b].top();
797 int newlevel=push(w,active);
798 if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list,
799 left, right, b, k, what_heur);
802 if ( numrelabel >= heur ) {
817 status=AFTER_PRE_FLOW_PHASE_1;
822 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
823 void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase2()
826 int k=n-2; //bound on the highest level under n containing a node
827 int b=k; //bound on the highest level under n of an active node
831 std::queue<Node> bfs_queue;
834 while (!bfs_queue.empty()) {
836 Node v=bfs_queue.front();
841 for(g->first(e,v); g->valid(e); g->next(e)) {
842 if ( (*capacity)[e] <= (*flow)[e] ) continue;
844 if ( level[u] >= n ) {
847 if ( excess[u] > 0 ) active[l].push(u);
852 for(g->first(f,v); g->valid(f); g->next(f)) {
853 if ( 0 >= (*flow)[f] ) continue;
855 if ( level[u] >= n ) {
858 if ( excess[u] > 0 ) active[l].push(u);
868 if ( active[b].empty() ) --b;
870 Node w=active[b].top();
872 int newlevel=push(w,active);
875 if ( excess[w] > 0 ) {
876 level.set(w,++newlevel);
877 active[newlevel].push(w);
880 } // if stack[b] is nonempty
883 status=AFTER_PRE_FLOW_PHASE_2;
888 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
889 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath()
891 ResGW res_graph(*g, *capacity, *flow);
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);
899 typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
900 pred.set(s, INVALID);
902 typename ResGW::template NodeMap<Num> free(res_graph);
904 //searching for augmenting path
905 while ( !bfs.finished() ) {
906 ResGWOutEdgeIt e=bfs;
907 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
908 Node v=res_graph.tail(e);
909 Node w=res_graph.head(e);
911 if (res_graph.valid(pred[v])) {
912 free.set(w, std::min(free[v], res_graph.resCap(e)));
914 free.set(w, res_graph.resCap(e));
916 if (res_graph.head(e)==t) { _augment=true; break; }
920 } //end of searching augmenting path
924 Num augment_value=free[t];
925 while (res_graph.valid(pred[n])) {
927 res_graph.augment(e, augment_value);
932 status=AFTER_AUGMENTING;
937 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
938 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath2()
940 ResGW res_graph(*g, *capacity, *flow);
943 if (status!=AFTER_AUGMENTING) {
944 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, -1);
945 number_of_augmentations=0;
947 ++number_of_augmentations;
949 TrickyReachedMap<ReachedMap>
950 tricky_reached_map(level, number_of_augmentations);
951 //ReachedMap level(res_graph);
952 // FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
953 BfsIterator<ResGW, TrickyReachedMap<ReachedMap> >
954 bfs(res_graph, tricky_reached_map);
955 bfs.pushAndSetReached(s);
957 typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
958 pred.set(s, INVALID);
960 typename ResGW::template NodeMap<Num> free(res_graph);
962 //searching for augmenting path
963 while ( !bfs.finished() ) {
964 ResGWOutEdgeIt e=bfs;
965 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
966 Node v=res_graph.tail(e);
967 Node w=res_graph.head(e);
969 if (res_graph.valid(pred[v])) {
970 free.set(w, std::min(free[v], res_graph.resCap(e)));
972 free.set(w, res_graph.resCap(e));
974 if (res_graph.head(e)==t) { _augment=true; break; }
978 } //end of searching augmenting path
982 Num augment_value=free[t];
983 while (res_graph.valid(pred[n])) {
985 res_graph.augment(e, augment_value);
990 status=AFTER_AUGMENTING;
995 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
996 template<typename MutableGraph>
997 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow()
999 typedef MutableGraph MG;
1000 bool _augment=false;
1002 ResGW res_graph(*g, *capacity, *flow);
1004 //bfs for distances on the residual graph
1005 //ReachedMap level(res_graph);
1006 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1007 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
1008 bfs.pushAndSetReached(s);
1009 typename ResGW::template NodeMap<int>
1010 dist(res_graph); //filled up with 0's
1012 //F will contain the physical copy of the residual graph
1013 //with the set of edges which are on shortest paths
1015 typename ResGW::template NodeMap<typename MG::Node>
1016 res_graph_to_F(res_graph);
1018 typename ResGW::NodeIt n;
1019 for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
1020 res_graph_to_F.set(n, F.addNode());
1024 typename MG::Node sF=res_graph_to_F[s];
1025 typename MG::Node tF=res_graph_to_F[t];
1026 typename MG::template EdgeMap<ResGWEdge> original_edge(F);
1027 typename MG::template EdgeMap<Num> residual_capacity(F);
1029 while ( !bfs.finished() ) {
1030 ResGWOutEdgeIt e=bfs;
1031 if (res_graph.valid(e)) {
1032 if (bfs.isBNodeNewlyReached()) {
1033 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
1034 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
1035 res_graph_to_F[res_graph.head(e)]);
1036 original_edge.update();
1037 original_edge.set(f, e);
1038 residual_capacity.update();
1039 residual_capacity.set(f, res_graph.resCap(e));
1041 if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
1042 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
1043 res_graph_to_F[res_graph.head(e)]);
1044 original_edge.update();
1045 original_edge.set(f, e);
1046 residual_capacity.update();
1047 residual_capacity.set(f, res_graph.resCap(e));
1052 } //computing distances from s in the residual graph
1054 bool __augment=true;
1058 //computing blocking flow with dfs
1059 DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
1060 typename MG::template NodeMap<typename MG::Edge> pred(F);
1061 pred.set(sF, INVALID);
1062 //invalid iterators for sources
1064 typename MG::template NodeMap<Num> free(F);
1066 dfs.pushAndSetReached(sF);
1067 while (!dfs.finished()) {
1069 if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
1070 if (dfs.isBNodeNewlyReached()) {
1071 typename MG::Node v=F.aNode(dfs);
1072 typename MG::Node w=F.bNode(dfs);
1074 if (F.valid(pred[v])) {
1075 free.set(w, std::min(free[v], residual_capacity[dfs]));
1077 free.set(w, residual_capacity[dfs]);
1086 F.erase(/*typename MG::OutEdgeIt*/(dfs));
1092 typename MG::Node n=tF;
1093 Num augment_value=free[tF];
1094 while (F.valid(pred[n])) {
1095 typename MG::Edge e=pred[n];
1096 res_graph.augment(original_edge[e], augment_value);
1098 if (residual_capacity[e]==augment_value)
1101 residual_capacity.set(e, residual_capacity[e]-augment_value);
1107 status=AFTER_AUGMENTING;
1114 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1115 bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2()
1117 bool _augment=false;
1119 ResGW res_graph(*g, *capacity, *flow);
1121 //ReachedMap level(res_graph);
1122 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1123 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
1125 bfs.pushAndSetReached(s);
1126 DistanceMap<ResGW> dist(res_graph);
1127 while ( !bfs.finished() ) {
1128 ResGWOutEdgeIt e=bfs;
1129 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
1130 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
1133 } //computing distances from s in the residual graph
1135 //Subgraph containing the edges on some shortest paths
1136 ConstMap<typename ResGW::Node, bool> true_map(true);
1137 typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
1138 DistanceMap<ResGW> > FilterResGW;
1139 FilterResGW filter_res_graph(res_graph, true_map, dist);
1141 //Subgraph, which is able to delete edges which are already
1143 typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt>
1144 first_out_edges(filter_res_graph);
1145 typename FilterResGW::NodeIt v;
1146 for(filter_res_graph.first(v); filter_res_graph.valid(v);
1147 filter_res_graph.next(v))
1149 typename FilterResGW::OutEdgeIt e;
1150 filter_res_graph.first(e, v);
1151 first_out_edges.set(v, e);
1153 typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
1154 template NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
1155 ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
1157 bool __augment=true;
1162 //computing blocking flow with dfs
1163 DfsIterator< ErasingResGW,
1164 typename ErasingResGW::template NodeMap<bool> >
1165 dfs(erasing_res_graph);
1166 typename ErasingResGW::
1167 template NodeMap<typename ErasingResGW::OutEdgeIt>
1168 pred(erasing_res_graph);
1169 pred.set(s, INVALID);
1170 //invalid iterators for sources
1172 typename ErasingResGW::template NodeMap<Num>
1173 free1(erasing_res_graph);
1175 dfs.pushAndSetReached
1177 (typename ErasingResGW::Node
1178 (typename FilterResGW::Node
1179 (typename ResGW::Node(s)
1183 while (!dfs.finished()) {
1185 if (erasing_res_graph.valid(typename ErasingResGW::OutEdgeIt(dfs)))
1187 if (dfs.isBNodeNewlyReached()) {
1189 typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
1190 typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
1192 pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
1193 if (erasing_res_graph.valid(pred[v])) {
1195 (w, std::min(free1[v], res_graph.resCap
1196 (typename ErasingResGW::OutEdgeIt(dfs))));
1199 (w, res_graph.resCap
1200 (typename ErasingResGW::OutEdgeIt(dfs)));
1209 erasing_res_graph.erase(dfs);
1215 typename ErasingResGW::Node
1216 n=typename FilterResGW::Node(typename ResGW::Node(t));
1217 // typename ResGW::NodeMap<Num> a(res_graph);
1218 // typename ResGW::Node b;
1220 // typename FilterResGW::NodeMap<Num> a1(filter_res_graph);
1221 // typename FilterResGW::Node b1;
1223 // typename ErasingResGW::NodeMap<Num> a2(erasing_res_graph);
1224 // typename ErasingResGW::Node b2;
1226 Num augment_value=free1[n];
1227 while (erasing_res_graph.valid(pred[n])) {
1228 typename ErasingResGW::OutEdgeIt e=pred[n];
1229 res_graph.augment(e, augment_value);
1230 n=erasing_res_graph.tail(e);
1231 if (res_graph.resCap(e)==0)
1232 erasing_res_graph.erase(e);
1236 } //while (__augment)
1238 status=AFTER_AUGMENTING;
1245 #endif //HUGO_MAX_FLOW_H