bug fix, test...
2 #ifndef HUGO_AUGMENTING_FLOW_H
3 #define HUGO_AUGMENTING_FLOW_H
10 #include <hugo/graph_wrapper.h>
12 #include <hugo/invalid.h>
13 #include <hugo/maps.h>
14 #include <for_each_macros.h>
17 /// \brief Maximum flow algorithms.
24 ///Maximum flow algorithms class.
26 ///This class provides various algorithms for finding a flow of
27 ///maximum value in a directed graph. The \e source node, the \e
28 ///target node, the \e capacity of the edges and the \e starting \e
29 ///flow value of the edges should be passed to the algorithm through the
30 ///constructor. It is possible to change these quantities using the
31 ///functions \ref resetSource, \ref resetTarget, \ref resetCap and
32 ///\ref resetFlow. Before any subsequent runs of any algorithm of
33 ///the class \ref resetFlow should be called.
35 ///After running an algorithm of the class, the actual flow value
36 ///can be obtained by calling \ref flowValue(). The minimum
37 ///value cut can be written into a \c node map of \c bools by
38 ///calling \ref minCut. (\ref minMinCut and \ref maxMinCut writes
39 ///the inclusionwise minimum and maximum of the minimum value
41 ///\param Graph The directed graph type the algorithm runs on.
42 ///\param Num The number type of the capacities and the flow values.
43 ///\param CapMap The capacity map type.
44 ///\param FlowMap The flow map type.
45 ///\author Marton Makai, Jacint Szabo
46 // template <typename Graph, typename Num,
47 // typename CapMap=typename Graph::template EdgeMap<Num>,
48 // typename FlowMap=typename Graph::template EdgeMap<Num> >
51 // typedef typename Graph::Node Node;
52 // typedef typename Graph::NodeIt NodeIt;
53 // typedef typename Graph::EdgeIt EdgeIt;
54 // typedef typename Graph::OutEdgeIt OutEdgeIt;
55 // typedef typename Graph::InEdgeIt InEdgeIt;
57 // typedef typename std::vector<std::stack<Node> > VecStack;
58 // typedef typename Graph::template NodeMap<Node> NNMap;
59 // typedef typename std::vector<Node> VecNode;
64 // const CapMap* capacity;
66 // int n; //the number of nodes of G
67 // typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
68 // //typedef ExpResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
69 // typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
70 // typedef typename ResGW::Edge ResGWEdge;
71 // //typedef typename ResGW::template NodeMap<bool> ReachedMap;
72 // typedef typename Graph::template NodeMap<int> ReachedMap;
75 // //level works as a bool map in augmenting path algorithms and is
76 // //used by bfs for storing reached information. In preflow, it
77 // //shows the levels of nodes.
80 // //excess is needed only in preflow
81 // typename Graph::template NodeMap<Num> excess;
86 // // void set(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
92 // // capacity=&_capacity;
95 // // level.set (_G); //kellene vmi ilyesmi fv
96 // // excess(_G,0); //itt is
99 // // constants used for heuristics
100 // static const int H0=20;
101 // static const int H1=1;
105 // ///Indicates the property of the starting flow.
107 // ///Indicates the property of the starting flow. The meanings are as follows:
108 // ///- \c ZERO_FLOW: constant zero flow
109 // ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
110 // ///the sum of the out-flows in every node except the \e source and
112 // ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at
113 // ///least the sum of the out-flows in every node except the \e source.
114 // ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be
115 // ///set to the constant zero flow in the beginning of the algorithm in this case.
126 // AFTER_FAST_AUGMENTING,
127 // AFTER_PRE_FLOW_PHASE_1,
128 // AFTER_PRE_FLOW_PHASE_2
131 // /// Don not needle this flag only if necessary.
132 // StatusEnum status;
133 // // int number_of_augmentations;
136 // // template<typename IntMap>
137 // // class TrickyReachedMap {
140 // // int* number_of_augmentations;
142 // // TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) :
143 // // map(&_map), number_of_augmentations(&_number_of_augmentations) { }
144 // // void set(const Node& n, bool b) {
146 // // map->set(n, *number_of_augmentations);
148 // // map->set(n, *number_of_augmentations-1);
150 // // bool operator[](const Node& n) const {
151 // // return (*map)[n]==*number_of_augmentations;
155 // MaxFlow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
157 // g(&_G), s(_s), t(_t), capacity(&_capacity),
158 // flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0),
159 // status(AFTER_NOTHING) { }
161 // ///Runs a maximum flow algorithm.
163 // ///Runs a preflow algorithm, which is the fastest maximum flow
164 // ///algorithm up-to-date. The default for \c fe is ZERO_FLOW.
165 // ///\pre The starting flow must be
166 // /// - a constant zero flow if \c fe is \c ZERO_FLOW,
167 // /// - an arbitary flow if \c fe is \c GEN_FLOW,
168 // /// - an arbitary preflow if \c fe is \c PRE_FLOW,
169 // /// - any map if \c fe is NO_FLOW.
170 // void run(FlowEnum fe=ZERO_FLOW) {
175 // ///Runs a preflow algorithm.
177 // ///Runs a preflow algorithm. The preflow algorithms provide the
178 // ///fastest way to compute a maximum flow in a directed graph.
179 // ///\pre The starting flow must be
180 // /// - a constant zero flow if \c fe is \c ZERO_FLOW,
181 // /// - an arbitary flow if \c fe is \c GEN_FLOW,
182 // /// - an arbitary preflow if \c fe is \c PRE_FLOW,
183 // /// - any map if \c fe is NO_FLOW.
184 // void preflow(FlowEnum fe) {
185 // preflowPhase1(fe);
191 // // list 'level_list' on the nodes on level i implemented by hand
192 // // stack 'active' on the active nodes on level i
193 // // runs heuristic 'highest label' for H1*n relabels
194 // // runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
195 // // Parameters H0 and H1 are initialized to 20 and 1.
197 // ///Runs the first phase of the preflow algorithm.
199 // ///The preflow algorithm consists of two phases, this method runs the
200 // ///first phase. After the first phase the maximum flow value and a
201 // ///minimum value cut can already be computed, though a maximum flow
202 // ///is net yet obtained. So after calling this method \ref flowValue
203 // ///and \ref actMinCut gives proper results.
204 // ///\warning: \ref minCut, \ref minMinCut and \ref maxMinCut do not
205 // ///give minimum value cuts unless calling \ref preflowPhase2.
206 // ///\pre The starting flow must be
207 // /// - a constant zero flow if \c fe is \c ZERO_FLOW,
208 // /// - an arbitary flow if \c fe is \c GEN_FLOW,
209 // /// - an arbitary preflow if \c fe is \c PRE_FLOW,
210 // /// - any map if \c fe is NO_FLOW.
211 // void preflowPhase1(FlowEnum fe);
213 // ///Runs the second phase of the preflow algorithm.
215 // ///The preflow algorithm consists of two phases, this method runs
216 // ///the second phase. After calling \ref preflowPhase1 and then
217 // ///\ref preflowPhase2 the methods \ref flowValue, \ref minCut,
218 // ///\ref minMinCut and \ref maxMinCut give proper results.
219 // ///\pre \ref preflowPhase1 must be called before.
220 // void preflowPhase2();
222 // /// Returns the maximum value of a flow.
224 // /// Returns the maximum value of a flow, by counting the
225 // /// over-flow of the target node \ref t.
226 // /// It can be called already after running \ref preflowPhase1.
227 // Num flowValue() const {
229 // FOR_EACH_INC_LOC(InEdgeIt, e, *g, t) a+=(*flow)[e];
230 // FOR_EACH_INC_LOC(OutEdgeIt, e, *g, t) a-=(*flow)[e];
232 // //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan
235 // ///Returns a minimum value cut after calling \ref preflowPhase1.
237 // ///After the first phase of the preflow algorithm the maximum flow
238 // ///value and a minimum value cut can already be computed. This
239 // ///method can be called after running \ref preflowPhase1 for
240 // ///obtaining a minimum value cut.
241 // /// \warning Gives proper result only right after calling \ref
242 // /// preflowPhase1.
243 // /// \todo We have to make some status variable which shows the
245 // /// of the class. This enables us to determine which methods are valid
246 // /// for MinCut computation
247 // template<typename _CutMap>
248 // void actMinCut(_CutMap& M) const {
251 // case AFTER_PRE_FLOW_PHASE_1:
252 // for(g->first(v); g->valid(v); g->next(v)) {
253 // if (level[v] < n) {
260 // case AFTER_PRE_FLOW_PHASE_2:
261 // case AFTER_NOTHING:
262 // case AFTER_AUGMENTING:
263 // case AFTER_FAST_AUGMENTING:
266 // // case AFTER_AUGMENTING:
267 // // for(g->first(v); g->valid(v); g->next(v)) {
268 // // if (level[v]) {
269 // // M.set(v, true);
271 // // M.set(v, false);
275 // // case AFTER_FAST_AUGMENTING:
276 // // for(g->first(v); g->valid(v); g->next(v)) {
277 // // if (level[v]==number_of_augmentations) {
278 // // M.set(v, true);
280 // // M.set(v, false);
287 // ///Returns the inclusionwise minimum of the minimum value cuts.
289 // ///Sets \c M to the characteristic vector of the minimum value cut
290 // ///which is inclusionwise minimum. It is computed by processing
291 // ///a bfs from the source node \c s in the residual graph.
292 // ///\pre M should be a node map of bools initialized to false.
293 // ///\pre \c flow must be a maximum flow.
294 // template<typename _CutMap>
295 // void minMinCut(_CutMap& M) const {
296 // std::queue<Node> queue;
301 // while (!queue.empty()) {
302 // Node w=queue.front();
306 // for(g->first(e,w) ; g->valid(e); g->next(e)) {
307 // Node v=g->head(e);
308 // if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
315 // for(g->first(f,w) ; g->valid(f); g->next(f)) {
316 // Node v=g->tail(f);
317 // if (!M[v] && (*flow)[f] > 0 ) {
325 // ///Returns the inclusionwise maximum of the minimum value cuts.
327 // ///Sets \c M to the characteristic vector of the minimum value cut
328 // ///which is inclusionwise maximum. It is computed by processing a
329 // ///backward bfs from the target node \c t in the residual graph.
330 // ///\pre M should be a node map of bools initialized to false.
331 // ///\pre \c flow must be a maximum flow.
332 // template<typename _CutMap>
333 // void maxMinCut(_CutMap& M) const {
336 // for(g->first(v) ; g->valid(v); g->next(v)) {
340 // std::queue<Node> queue;
345 // while (!queue.empty()) {
346 // Node w=queue.front();
350 // for(g->first(e,w) ; g->valid(e); g->next(e)) {
351 // Node v=g->tail(e);
352 // if (M[v] && (*flow)[e] < (*capacity)[e] ) {
359 // for(g->first(f,w) ; g->valid(f); g->next(f)) {
360 // Node v=g->head(f);
361 // if (M[v] && (*flow)[f] > 0 ) {
369 // ///Returns a minimum value cut.
371 // ///Sets \c M to the characteristic vector of a minimum value cut.
372 // ///\pre M should be a node map of bools initialized to false.
373 // ///\pre \c flow must be a maximum flow.
374 // template<typename CutMap>
375 // void minCut(CutMap& M) const { minMinCut(M); }
377 // ///Resets the source node to \c _s.
379 // ///Resets the source node to \c _s.
381 // void resetSource(Node _s) { s=_s; status=AFTER_NOTHING; }
383 // ///Resets the target node to \c _t.
385 // ///Resets the target node to \c _t.
387 // void resetTarget(Node _t) { t=_t; status=AFTER_NOTHING; }
389 // /// Resets the edge map of the capacities to _cap.
391 // /// Resets the edge map of the capacities to _cap.
393 // void resetCap(const CapMap& _cap) { capacity=&_cap; status=AFTER_NOTHING; }
395 // /// Resets the edge map of the flows to _flow.
397 // /// Resets the edge map of the flows to _flow.
399 // void resetFlow(FlowMap& _flow) { flow=&_flow; status=AFTER_NOTHING; }
404 // int push(Node w, VecStack& active) {
407 // Num exc=excess[w];
408 // int newlevel=n; //bound on the next level of w
411 // for(g->first(e,w); g->valid(e); g->next(e)) {
413 // if ( (*flow)[e] >= (*capacity)[e] ) continue;
414 // Node v=g->head(e);
416 // if( lev > level[v] ) { //Push is allowed now
418 // if ( excess[v]<=0 && v!=t && v!=s ) {
419 // int lev_v=level[v];
420 // active[lev_v].push(v);
423 // Num cap=(*capacity)[e];
424 // Num flo=(*flow)[e];
425 // Num remcap=cap-flo;
427 // if ( remcap >= exc ) { //A nonsaturating push.
429 // flow->set(e, flo+exc);
430 // excess.set(v, excess[v]+exc);
434 // } else { //A saturating push.
435 // flow->set(e, cap);
436 // excess.set(v, excess[v]+remcap);
439 // } else if ( newlevel > level[v] ) newlevel = level[v];
440 // } //for out edges wv
444 // for(g->first(e,w); g->valid(e); g->next(e)) {
446 // if( (*flow)[e] <= 0 ) continue;
447 // Node v=g->tail(e);
449 // if( lev > level[v] ) { //Push is allowed now
451 // if ( excess[v]<=0 && v!=t && v!=s ) {
452 // int lev_v=level[v];
453 // active[lev_v].push(v);
456 // Num flo=(*flow)[e];
458 // if ( flo >= exc ) { //A nonsaturating push.
460 // flow->set(e, flo-exc);
461 // excess.set(v, excess[v]+exc);
464 // } else { //A saturating push.
466 // excess.set(v, excess[v]+flo);
470 // } else if ( newlevel > level[v] ) newlevel = level[v];
471 // } //for in edges vw
473 // } // if w still has excess after the out edge for cycle
475 // excess.set(w, exc);
481 // void preflowPreproc(FlowEnum fe, VecStack& active,
482 // VecNode& level_list, NNMap& left, NNMap& right)
484 // std::queue<Node> bfs_queue;
487 // case NO_FLOW: //flow is already set to const zero in this case
490 // //Reverse_bfs from t, to find the starting level.
492 // bfs_queue.push(t);
494 // while (!bfs_queue.empty()) {
496 // Node v=bfs_queue.front();
501 // for(g->first(e,v); g->valid(e); g->next(e)) {
502 // Node w=g->tail(e);
503 // if ( level[w] == n && w != s ) {
504 // bfs_queue.push(w);
505 // Node first=level_list[l];
506 // if ( g->valid(first) ) left.set(first,w);
507 // right.set(w,first);
514 // //the starting flow
516 // for(g->first(e,s); g->valid(e); g->next(e))
518 // Num c=(*capacity)[e];
519 // if ( c <= 0 ) continue;
520 // Node w=g->head(e);
521 // if ( level[w] < n ) {
522 // if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
524 // excess.set(w, excess[w]+c);
533 // //Reverse_bfs from t in the residual graph,
534 // //to find the starting level.
536 // bfs_queue.push(t);
538 // while (!bfs_queue.empty()) {
540 // Node v=bfs_queue.front();
545 // for(g->first(e,v); g->valid(e); g->next(e)) {
546 // if ( (*capacity)[e] <= (*flow)[e] ) continue;
547 // Node w=g->tail(e);
548 // if ( level[w] == n && w != s ) {
549 // bfs_queue.push(w);
550 // Node first=level_list[l];
551 // if ( g->valid(first) ) left.set(first,w);
552 // right.set(w,first);
559 // for(g->first(f,v); g->valid(f); g->next(f)) {
560 // if ( 0 >= (*flow)[f] ) continue;
561 // Node w=g->head(f);
562 // if ( level[w] == n && w != s ) {
563 // bfs_queue.push(w);
564 // Node first=level_list[l];
565 // if ( g->valid(first) ) left.set(first,w);
566 // right.set(w,first);
574 // //the starting flow
576 // for(g->first(e,s); g->valid(e); g->next(e))
578 // Num rem=(*capacity)[e]-(*flow)[e];
579 // if ( rem <= 0 ) continue;
580 // Node w=g->head(e);
581 // if ( level[w] < n ) {
582 // if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
583 // flow->set(e, (*capacity)[e]);
584 // excess.set(w, excess[w]+rem);
589 // for(g->first(f,s); g->valid(f); g->next(f))
591 // if ( (*flow)[f] <= 0 ) continue;
592 // Node w=g->tail(f);
593 // if ( level[w] < n ) {
594 // if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
595 // excess.set(w, excess[w]+(*flow)[f]);
602 // } //preflowPreproc
606 // void relabel(Node w, int newlevel, VecStack& active,
607 // VecNode& level_list, NNMap& left,
608 // NNMap& right, int& b, int& k, bool what_heur )
611 // //FIXME jacint: ez mitol num
612 // // Num lev=level[w];
615 // Node right_n=right[w];
616 // Node left_n=left[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;
646 // if ( !what_heur ) {
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);
668 // right.set(w,first);
669 // left.set(w,INVALID);
670 // level_list[newlevel]=w;
680 // template <typename Graph, typename Num, typename CapMap, typename FlowMap>
681 // void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase1(FlowEnum fe)
684 // int heur0=(int)(H0*n); //time while running 'bound decrease'
685 // int heur1=(int)(H1*n); //time while running 'highest label'
686 // int heur=heur1; //starting time interval (#of relabels)
690 // //It is 0 in case 'bound decrease' and 1 in case 'highest label'
693 // //Needed for 'bound decrease', true means no active nodes are above bound
696 // int k=n-2; //bound on the highest level under n containing a node
697 // int b=k; //bound on the highest level under n of an active node
699 // VecStack active(n);
701 // NNMap left(*g, INVALID);
702 // NNMap right(*g, INVALID);
703 // VecNode level_list(n,INVALID);
704 // //List of the nodes in level i<n, set to n.
707 // for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
708 // //setting each node to level n
710 // if ( fe == NO_FLOW ) {
712 // for(g->first(e); g->valid(e); g->next(e)) flow->set(e,0);
715 // switch (fe) { //computing the excess
719 // for(g->first(v); g->valid(v); g->next(v)) {
723 // for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
725 // for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
727 // excess.set(v,exc);
729 // //putting the active nodes into the stack
731 // if ( exc > 0 && lev < n && v != t ) active[lev].push(v);
738 // for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
742 // for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
744 // for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
745 // excess.set(t,exc);
752 // for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
757 // preflowPreproc(fe, active, level_list, left, right);
758 // //End of preprocessing
761 // //Push/relabel on the highest level active nodes.
764 // if ( !what_heur && !end && k > 0 ) {
770 // if ( active[b].empty() ) --b;
773 // Node w=active[b].top();
775 // int newlevel=push(w,active);
776 // if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list,
777 // left, right, b, k, what_heur);
780 // if ( numrelabel >= heur ) {
782 // if ( what_heur ) {
795 // status=AFTER_PRE_FLOW_PHASE_1;
800 // template <typename Graph, typename Num, typename CapMap, typename FlowMap>
801 // void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase2()
804 // int k=n-2; //bound on the highest level under n containing a node
805 // int b=k; //bound on the highest level under n of an active node
807 // VecStack active(n);
809 // std::queue<Node> bfs_queue;
810 // bfs_queue.push(s);
812 // while (!bfs_queue.empty()) {
814 // Node v=bfs_queue.front();
819 // for(g->first(e,v); g->valid(e); g->next(e)) {
820 // if ( (*capacity)[e] <= (*flow)[e] ) continue;
821 // Node u=g->tail(e);
822 // if ( level[u] >= n ) {
823 // bfs_queue.push(u);
825 // if ( excess[u] > 0 ) active[l].push(u);
830 // for(g->first(f,v); g->valid(f); g->next(f)) {
831 // if ( 0 >= (*flow)[f] ) continue;
832 // Node u=g->head(f);
833 // if ( level[u] >= n ) {
834 // bfs_queue.push(u);
836 // if ( excess[u] > 0 ) active[l].push(u);
844 // if ( b == 0 ) break;
846 // if ( active[b].empty() ) --b;
848 // Node w=active[b].top();
850 // int newlevel=push(w,active);
853 // if ( excess[w] > 0 ) {
854 // level.set(w,++newlevel);
855 // active[newlevel].push(w);
858 // } // if stack[b] is nonempty
861 // status=AFTER_PRE_FLOW_PHASE_2;
865 template <typename Graph, typename Num,
866 typename CapMap=typename Graph::template EdgeMap<Num>,
867 typename FlowMap=typename Graph::template EdgeMap<Num> >
868 class AugmentingFlow {
870 typedef typename Graph::Node Node;
871 typedef typename Graph::NodeIt NodeIt;
872 typedef typename Graph::EdgeIt EdgeIt;
873 typedef typename Graph::OutEdgeIt OutEdgeIt;
874 typedef typename Graph::InEdgeIt InEdgeIt;
876 // typedef typename std::vector<std::stack<Node> > VecStack;
877 // typedef typename Graph::template NodeMap<Node> NNMap;
878 // typedef typename std::vector<Node> VecNode;
883 const CapMap* capacity;
885 // int n; //the number of nodes of G
886 typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
887 //typedef ExpResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
888 typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
889 typedef typename ResGW::Edge ResGWEdge;
890 //typedef typename ResGW::template NodeMap<bool> ReachedMap;
891 typedef typename Graph::template NodeMap<int> ReachedMap;
894 //level works as a bool map in augmenting path algorithms and is
895 //used by bfs for storing reached information. In preflow, it
896 //shows the levels of nodes.
899 //excess is needed only in preflow
900 // typename Graph::template NodeMap<Num> excess;
905 // void set(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
911 // capacity=&_capacity;
914 // level.set (_G); //kellene vmi ilyesmi fv
915 // excess(_G,0); //itt is
918 // constants used for heuristics
919 // static const int H0=20;
920 // static const int H1=1;
924 ///Indicates the property of the starting flow.
926 ///Indicates the property of the starting flow. The meanings are as follows:
927 ///- \c ZERO_FLOW: constant zero flow
928 ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
929 ///the sum of the out-flows in every node except the \e source and
931 ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at
932 ///least the sum of the out-flows in every node except the \e source.
933 ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be
934 ///set to the constant zero flow in the beginning of the algorithm in this case.
945 AFTER_FAST_AUGMENTING,
946 AFTER_PRE_FLOW_PHASE_1,
947 AFTER_PRE_FLOW_PHASE_2
950 /// Don not needle this flag only if necessary.
952 int number_of_augmentations;
955 template<typename IntMap>
956 class TrickyReachedMap {
959 int* number_of_augmentations;
961 TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) :
962 map(&_map), number_of_augmentations(&_number_of_augmentations) { }
963 void set(const Node& n, bool b) {
965 map->set(n, *number_of_augmentations);
967 map->set(n, *number_of_augmentations-1);
969 bool operator[](const Node& n) const {
970 return (*map)[n]==*number_of_augmentations;
974 AugmentingFlow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
976 g(&_G), s(_s), t(_t), capacity(&_capacity),
977 flow(&_flow), //n(_G.nodeNum()),
978 level(_G), //excess(_G,0),
979 status(AFTER_NOTHING), number_of_augmentations(0) { }
981 /// Starting from a flow, this method searches for an augmenting path
982 /// according to the Edmonds-Karp algorithm
983 /// and augments the flow on if any.
984 /// The return value shows if the augmentation was succesful.
985 bool augmentOnShortestPath();
986 bool augmentOnShortestPath2();
988 /// Starting from a flow, this method searches for an augmenting blocking
989 /// flow according to Dinits' algorithm and augments the flow on if any.
990 /// The blocking flow is computed in a physically constructed
991 /// residual graph of type \c Mutablegraph.
992 /// The return value show sif the augmentation was succesful.
993 template<typename MutableGraph> bool augmentOnBlockingFlow();
995 /// The same as \c augmentOnBlockingFlow<MutableGraph> but the
996 /// residual graph is not constructed physically.
997 /// The return value shows if the augmentation was succesful.
998 bool augmentOnBlockingFlow2();
1000 template<typename _CutMap>
1001 void actMinCut(_CutMap& M) const {
1004 case AFTER_PRE_FLOW_PHASE_1:
1005 // std::cout << "AFTER_PRE_FLOW_PHASE_1" << std::endl;
1006 // for(g->first(v); g->valid(v); g->next(v)) {
1007 // if (level[v] < n) {
1014 case AFTER_PRE_FLOW_PHASE_2:
1015 // std::cout << "AFTER_PRE_FLOW_PHASE_2" << std::endl;
1018 // std::cout << "AFTER_NOTHING" << std::endl;
1021 case AFTER_AUGMENTING:
1022 // std::cout << "AFTER_AUGMENTING" << std::endl;
1023 for(g->first(v); g->valid(v); g->next(v)) {
1031 case AFTER_FAST_AUGMENTING:
1032 // std::cout << "AFTER_FAST_AUGMENTING" << std::endl;
1033 for(g->first(v); g->valid(v); g->next(v)) {
1034 if (level[v]==number_of_augmentations) {
1044 template<typename _CutMap>
1045 void minMinCut(_CutMap& M) const {
1046 std::queue<Node> queue;
1051 while (!queue.empty()) {
1052 Node w=queue.front();
1056 for(g->first(e,w) ; g->valid(e); g->next(e)) {
1058 if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
1065 for(g->first(f,w) ; g->valid(f); g->next(f)) {
1067 if (!M[v] && (*flow)[f] > 0 ) {
1075 template<typename _CutMap>
1076 void minMinCut2(_CutMap& M) const {
1077 ResGW res_graph(*g, *capacity, *flow);
1078 BfsIterator<ResGW, _CutMap> bfs(res_graph, M);
1079 bfs.pushAndSetReached(s);
1080 while (!bfs.finished()) ++bfs;
1083 Num flowValue() const {
1085 FOR_EACH_INC_LOC(InEdgeIt, e, *g, t) a+=(*flow)[e];
1086 FOR_EACH_INC_LOC(OutEdgeIt, e, *g, t) a-=(*flow)[e];
1088 //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan
1091 template<typename MapGraphWrapper>
1094 const MapGraphWrapper* g;
1095 typename MapGraphWrapper::template NodeMap<int> dist;
1097 DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
1098 void set(const typename MapGraphWrapper::Node& n, int a) {
1101 int operator[](const typename MapGraphWrapper::Node& n) const {
1104 // int get(const typename MapGraphWrapper::Node& n) const {
1105 // return dist[n]; }
1106 // bool get(const typename MapGraphWrapper::Edge& e) const {
1107 // return (dist.get(g->tail(e))<dist.get(g->head(e))); }
1108 bool operator[](const typename MapGraphWrapper::Edge& e) const {
1109 return (dist[g->tail(e)]<dist[g->head(e)]);
1117 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1118 bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath()
1120 ResGW res_graph(*g, *capacity, *flow);
1121 bool _augment=false;
1123 //ReachedMap level(res_graph);
1124 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1125 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
1126 bfs.pushAndSetReached(s);
1128 typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
1129 pred.set(s, INVALID);
1131 typename ResGW::template NodeMap<Num> free(res_graph);
1133 //searching for augmenting path
1134 while ( !bfs.finished() ) {
1135 ResGWOutEdgeIt e=bfs;
1136 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
1137 Node v=res_graph.tail(e);
1138 Node w=res_graph.head(e);
1140 if (res_graph.valid(pred[v])) {
1141 free.set(w, std::min(free[v], res_graph.resCap(e)));
1143 free.set(w, res_graph.resCap(e));
1145 if (res_graph.head(e)==t) { _augment=true; break; }
1149 } //end of searching augmenting path
1153 Num augment_value=free[t];
1154 while (res_graph.valid(pred[n])) {
1155 ResGWEdge e=pred[n];
1156 res_graph.augment(e, augment_value);
1157 n=res_graph.tail(e);
1161 status=AFTER_AUGMENTING;
1165 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1166 bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath2()
1168 ResGW res_graph(*g, *capacity, *flow);
1169 bool _augment=false;
1171 if (status!=AFTER_FAST_AUGMENTING) {
1172 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1173 number_of_augmentations=1;
1175 ++number_of_augmentations;
1177 TrickyReachedMap<ReachedMap>
1178 tricky_reached_map(level, number_of_augmentations);
1179 //ReachedMap level(res_graph);
1180 // FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1181 BfsIterator<ResGW, TrickyReachedMap<ReachedMap> >
1182 bfs(res_graph, tricky_reached_map);
1183 bfs.pushAndSetReached(s);
1185 typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
1186 pred.set(s, INVALID);
1188 typename ResGW::template NodeMap<Num> free(res_graph);
1190 //searching for augmenting path
1191 while ( !bfs.finished() ) {
1192 ResGWOutEdgeIt e=bfs;
1193 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
1194 Node v=res_graph.tail(e);
1195 Node w=res_graph.head(e);
1197 if (res_graph.valid(pred[v])) {
1198 free.set(w, std::min(free[v], res_graph.resCap(e)));
1200 free.set(w, res_graph.resCap(e));
1202 if (res_graph.head(e)==t) { _augment=true; break; }
1206 } //end of searching augmenting path
1210 Num augment_value=free[t];
1211 while (res_graph.valid(pred[n])) {
1212 ResGWEdge e=pred[n];
1213 res_graph.augment(e, augment_value);
1214 n=res_graph.tail(e);
1218 status=AFTER_FAST_AUGMENTING;
1223 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1224 template<typename MutableGraph>
1225 bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow()
1227 typedef MutableGraph MG;
1228 bool _augment=false;
1230 ResGW res_graph(*g, *capacity, *flow);
1232 //bfs for distances on the residual graph
1233 //ReachedMap level(res_graph);
1234 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1235 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
1236 bfs.pushAndSetReached(s);
1237 typename ResGW::template NodeMap<int>
1238 dist(res_graph); //filled up with 0's
1240 //F will contain the physical copy of the residual graph
1241 //with the set of edges which are on shortest paths
1243 typename ResGW::template NodeMap<typename MG::Node>
1244 res_graph_to_F(res_graph);
1246 typename ResGW::NodeIt n;
1247 for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
1248 res_graph_to_F.set(n, F.addNode());
1252 typename MG::Node sF=res_graph_to_F[s];
1253 typename MG::Node tF=res_graph_to_F[t];
1254 typename MG::template EdgeMap<ResGWEdge> original_edge(F);
1255 typename MG::template EdgeMap<Num> residual_capacity(F);
1257 while ( !bfs.finished() ) {
1258 ResGWOutEdgeIt e=bfs;
1259 if (res_graph.valid(e)) {
1260 if (bfs.isBNodeNewlyReached()) {
1261 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
1262 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
1263 res_graph_to_F[res_graph.head(e)]);
1264 original_edge.update();
1265 original_edge.set(f, e);
1266 residual_capacity.update();
1267 residual_capacity.set(f, res_graph.resCap(e));
1269 if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
1270 typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)],
1271 res_graph_to_F[res_graph.head(e)]);
1272 original_edge.update();
1273 original_edge.set(f, e);
1274 residual_capacity.update();
1275 residual_capacity.set(f, res_graph.resCap(e));
1280 } //computing distances from s in the residual graph
1282 bool __augment=true;
1286 //computing blocking flow with dfs
1287 DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
1288 typename MG::template NodeMap<typename MG::Edge> pred(F);
1289 pred.set(sF, INVALID);
1290 //invalid iterators for sources
1292 typename MG::template NodeMap<Num> free(F);
1294 dfs.pushAndSetReached(sF);
1295 while (!dfs.finished()) {
1297 if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
1298 if (dfs.isBNodeNewlyReached()) {
1299 typename MG::Node v=F.aNode(dfs);
1300 typename MG::Node w=F.bNode(dfs);
1302 if (F.valid(pred[v])) {
1303 free.set(w, std::min(free[v], residual_capacity[dfs]));
1305 free.set(w, residual_capacity[dfs]);
1314 F.erase(/*typename MG::OutEdgeIt*/(dfs));
1320 typename MG::Node n=tF;
1321 Num augment_value=free[tF];
1322 while (F.valid(pred[n])) {
1323 typename MG::Edge e=pred[n];
1324 res_graph.augment(original_edge[e], augment_value);
1326 if (residual_capacity[e]==augment_value)
1329 residual_capacity.set(e, residual_capacity[e]-augment_value);
1335 status=AFTER_AUGMENTING;
1342 template <typename Graph, typename Num, typename CapMap, typename FlowMap>
1343 bool AugmentingFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2()
1345 bool _augment=false;
1347 ResGW res_graph(*g, *capacity, *flow);
1349 //ReachedMap level(res_graph);
1350 FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
1351 BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
1353 bfs.pushAndSetReached(s);
1354 DistanceMap<ResGW> dist(res_graph);
1355 while ( !bfs.finished() ) {
1356 ResGWOutEdgeIt e=bfs;
1357 if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
1358 dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
1361 } //computing distances from s in the residual graph
1363 //Subgraph containing the edges on some shortest paths
1364 ConstMap<typename ResGW::Node, bool> true_map(true);
1365 typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
1366 DistanceMap<ResGW> > FilterResGW;
1367 FilterResGW filter_res_graph(res_graph, true_map, dist);
1369 //Subgraph, which is able to delete edges which are already
1371 typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt>
1372 first_out_edges(filter_res_graph);
1373 typename FilterResGW::NodeIt v;
1374 for(filter_res_graph.first(v); filter_res_graph.valid(v);
1375 filter_res_graph.next(v))
1377 typename FilterResGW::OutEdgeIt e;
1378 filter_res_graph.first(e, v);
1379 first_out_edges.set(v, e);
1381 typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
1382 template NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
1383 ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
1385 bool __augment=true;
1390 //computing blocking flow with dfs
1391 DfsIterator< ErasingResGW,
1392 typename ErasingResGW::template NodeMap<bool> >
1393 dfs(erasing_res_graph);
1394 typename ErasingResGW::
1395 template NodeMap<typename ErasingResGW::OutEdgeIt>
1396 pred(erasing_res_graph);
1397 pred.set(s, INVALID);
1398 //invalid iterators for sources
1400 typename ErasingResGW::template NodeMap<Num>
1401 free1(erasing_res_graph);
1403 dfs.pushAndSetReached
1405 (typename ErasingResGW::Node
1406 (typename FilterResGW::Node
1407 (typename ResGW::Node(s)
1411 while (!dfs.finished()) {
1413 if (erasing_res_graph.valid(typename ErasingResGW::OutEdgeIt(dfs)))
1415 if (dfs.isBNodeNewlyReached()) {
1417 typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
1418 typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
1420 pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
1421 if (erasing_res_graph.valid(pred[v])) {
1423 (w, std::min(free1[v], res_graph.resCap
1424 (typename ErasingResGW::OutEdgeIt(dfs))));
1427 (w, res_graph.resCap
1428 (typename ErasingResGW::OutEdgeIt(dfs)));
1437 erasing_res_graph.erase(dfs);
1443 typename ErasingResGW::Node
1444 n=typename FilterResGW::Node(typename ResGW::Node(t));
1445 // typename ResGW::NodeMap<Num> a(res_graph);
1446 // typename ResGW::Node b;
1448 // typename FilterResGW::NodeMap<Num> a1(filter_res_graph);
1449 // typename FilterResGW::Node b1;
1451 // typename ErasingResGW::NodeMap<Num> a2(erasing_res_graph);
1452 // typename ErasingResGW::Node b2;
1454 Num augment_value=free1[n];
1455 while (erasing_res_graph.valid(pred[n])) {
1456 typename ErasingResGW::OutEdgeIt e=pred[n];
1457 res_graph.augment(e, augment_value);
1458 n=erasing_res_graph.tail(e);
1459 if (res_graph.resCap(e)==0)
1460 erasing_res_graph.erase(e);
1464 } //while (__augment)
1466 status=AFTER_AUGMENTING;
1473 #endif //HUGO_AUGMENTING_FLOW_H