COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/hugo/max_flow.h @ 734:329832ac02b7

Last change on this file since 734:329832ac02b7 was 726:835ebe1b3250, checked in by Alpar Juttner, 20 years ago

max_flow.h (wich doesn't use STL

  • max_flow.h (which doesn't use STL stack) moved to /src/hugo
  • for_each_macros.h was removed from max_flow.h
  • (blocking) flow augmenting stuffs was removed.
File size: 23.5 KB
Line 
1// -*- C++ -*-
2#ifndef HUGO_MAX_FLOW_NO_STACK_H
3#define HUGO_MAX_FLOW_NO_STACK_H
4
5#include <vector>
6#include <queue>
7//#include <stack>
8
9#include <hugo/graph_wrapper.h>
10#include <hugo/invalid.h>
11#include <hugo/maps.h>
12
13/// \file
14/// \brief The same as max_flow.h, but without using stl stack for the active nodes. Only for test.
15/// \ingroup galgs
16
17namespace hugo {
18
19  /// \addtogroup galgs
20  /// @{                                                                                                                                       
21  ///Maximum flow algorithms class.
22
23  ///This class provides various algorithms for finding a flow of
24  ///maximum value in a directed graph. The \e source node, the \e
25  ///target node, the \e capacity of the edges and the \e starting \e
26  ///flow value of the edges should be passed to the algorithm through the
27  ///constructor. It is possible to change these quantities using the
28  ///functions \ref resetSource, \ref resetTarget, \ref resetCap and
29  ///\ref resetFlow. Before any subsequent runs of any algorithm of
30  ///the class \ref resetFlow should be called.
31
32  ///After running an algorithm of the class, the actual flow value
33  ///can be obtained by calling \ref flowValue(). The minimum
34  ///value cut can be written into a \c node map of \c bools by
35  ///calling \ref minCut. (\ref minMinCut and \ref maxMinCut writes
36  ///the inclusionwise minimum and maximum of the minimum value
37  ///cuts, resp.)                                                                                                                               
38  ///\param Graph The directed graph type the algorithm runs on.
39  ///\param Num The number type of the capacities and the flow values.
40  ///\param CapMap The capacity map type.
41  ///\param FlowMap The flow map type.                                                                                                           
42  ///\author Marton Makai, Jacint Szabo
43  template <typename Graph, typename Num,
44            typename CapMap=typename Graph::template EdgeMap<Num>,
45            typename FlowMap=typename Graph::template EdgeMap<Num> >
46  class MaxFlow {
47  protected:
48    typedef typename Graph::Node Node;
49    typedef typename Graph::NodeIt NodeIt;
50    typedef typename Graph::EdgeIt EdgeIt;
51    typedef typename Graph::OutEdgeIt OutEdgeIt;
52    typedef typename Graph::InEdgeIt InEdgeIt;
53
54    //    typedef typename std::vector<std::stack<Node> > VecStack;
55    typedef typename std::vector<Node> VecFirst;
56    typedef typename Graph::template NodeMap<Node> NNMap;
57    typedef typename std::vector<Node> VecNode;
58
59    const Graph* g;
60    Node s;
61    Node t;
62    const CapMap* capacity;
63    FlowMap* flow;
64    int n;      //the number of nodes of G
65    typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;   
66    //typedef ExpResGraphWrapper<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;
71
72
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.     
76    ReachedMap level;
77
78    //excess is needed only in preflow
79    typename Graph::template NodeMap<Num> excess;
80
81    // constants used for heuristics
82    static const int H0=20;
83    static const int H1=1;
84
85  public:
86
87    ///Indicates the property of the starting flow.
88
89    ///Indicates the property of the starting flow. The meanings are as follows:
90    ///- \c ZERO_FLOW: constant zero flow
91    ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
92    ///the sum of the out-flows in every node except the \e source and
93    ///the \e target.
94    ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at
95    ///least the sum of the out-flows in every node except the \e source.
96    ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be
97    ///set to the constant zero flow in the beginning of the algorithm in this case.
98    enum FlowEnum{
99      ZERO_FLOW,
100      GEN_FLOW,
101      PRE_FLOW,
102      NO_FLOW
103    };
104
105    enum StatusEnum {
106      AFTER_NOTHING,
107      AFTER_AUGMENTING,
108      AFTER_FAST_AUGMENTING,
109      AFTER_PRE_FLOW_PHASE_1,     
110      AFTER_PRE_FLOW_PHASE_2
111    };
112
113    /// Don not needle this flag only if necessary.
114    StatusEnum status;
115
116//     int number_of_augmentations;
117
118
119//     template<typename IntMap>
120//     class TrickyReachedMap {
121//     protected:
122//       IntMap* map;
123//       int* number_of_augmentations;
124//     public:
125//       TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) :
126//      map(&_map), number_of_augmentations(&_number_of_augmentations) { }
127//       void set(const Node& n, bool b) {
128//      if (b)
129//        map->set(n, *number_of_augmentations);
130//      else
131//        map->set(n, *number_of_augmentations-1);
132//       }
133//       bool operator[](const Node& n) const {
134//      return (*map)[n]==*number_of_augmentations;
135//       }
136//     };
137   
138    ///Constructor
139
140    ///\todo Document, please.
141    ///
142    MaxFlow(const Graph& _G, Node _s, Node _t,
143                   const CapMap& _capacity, FlowMap& _flow) :
144      g(&_G), s(_s), t(_t), capacity(&_capacity),
145      flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0),
146      status(AFTER_NOTHING) { }
147
148    ///Runs a maximum flow algorithm.
149
150    ///Runs a preflow algorithm, which is the fastest maximum flow
151    ///algorithm up-to-date. The default for \c fe is ZERO_FLOW.
152    ///\pre The starting flow must be
153    /// - a constant zero flow if \c fe is \c ZERO_FLOW,
154    /// - an arbitary flow if \c fe is \c GEN_FLOW,
155    /// - an arbitary preflow if \c fe is \c PRE_FLOW,
156    /// - any map if \c fe is NO_FLOW.
157    void run(FlowEnum fe=ZERO_FLOW) {
158      preflow(fe);
159    }
160
161                                                                             
162    ///Runs a preflow algorithm. 
163
164    ///Runs a preflow algorithm. The preflow algorithms provide the
165    ///fastest way to compute a maximum flow in a directed graph.
166    ///\pre The starting flow must be
167    /// - a constant zero flow if \c fe is \c ZERO_FLOW,
168    /// - an arbitary flow if \c fe is \c GEN_FLOW,
169    /// - an arbitary preflow if \c fe is \c PRE_FLOW,
170    /// - any map if \c fe is NO_FLOW.
171    ///
172    ///\todo NO_FLOW should be the default flow.
173    void preflow(FlowEnum fe) {
174      preflowPhase1(fe);
175      preflowPhase2();
176    }
177    // Heuristics:
178    //   2 phase
179    //   gap
180    //   list 'level_list' on the nodes on level i implemented by hand
181    //   stack 'active' on the active nodes on level i                                                                                   
182    //   runs heuristic 'highest label' for H1*n relabels
183    //   runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
184    //   Parameters H0 and H1 are initialized to 20 and 1.
185
186    ///Runs the first phase of the preflow algorithm.
187
188    ///The preflow algorithm consists of two phases, this method runs the
189    ///first phase. After the first phase the maximum flow value and a
190    ///minimum value cut can already be computed, though a maximum flow
191    ///is net yet obtained. So after calling this method \ref flowValue
192    ///and \ref actMinCut gives proper results.
193    ///\warning: \ref minCut, \ref minMinCut and \ref maxMinCut do not
194    ///give minimum value cuts unless calling \ref preflowPhase2.
195    ///\pre The starting flow must be
196    /// - a constant zero flow if \c fe is \c ZERO_FLOW,
197    /// - an arbitary flow if \c fe is \c GEN_FLOW,
198    /// - an arbitary preflow if \c fe is \c PRE_FLOW,
199    /// - any map if \c fe is NO_FLOW.
200    void preflowPhase1(FlowEnum fe)
201    {
202
203      int heur0=(int)(H0*n);  //time while running 'bound decrease'
204      int heur1=(int)(H1*n);  //time while running 'highest label'
205      int heur=heur1;         //starting time interval (#of relabels)
206      int numrelabel=0;
207
208      bool what_heur=1;
209      //It is 0 in case 'bound decrease' and 1 in case 'highest label'
210
211      bool end=false;
212      //Needed for 'bound decrease', true means no active nodes are above bound
213      //b.
214
215      int k=n-2;  //bound on the highest level under n containing a node
216      int b=k;    //bound on the highest level under n of an active node
217
218      VecFirst first(n, INVALID);
219      NNMap next(*g, INVALID); //maybe INVALID is not needed
220      //    VecStack active(n);
221
222      NNMap left(*g, INVALID);
223      NNMap right(*g, INVALID);
224      VecNode level_list(n,INVALID);
225      //List of the nodes in level i<n, set to n.
226
227      NodeIt v;
228      for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
229      //setting each node to level n
230
231      if ( fe == NO_FLOW ) {
232        EdgeIt e;
233        for(g->first(e); g->valid(e); g->next(e)) flow->set(e,0);
234      }
235
236      switch (fe) { //computing the excess
237      case PRE_FLOW:
238        {
239          NodeIt v;
240          for(g->first(v); g->valid(v); g->next(v)) {
241            Num exc=0;
242
243            InEdgeIt e;
244            for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
245            OutEdgeIt f;
246            for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
247
248            excess.set(v,exc);
249
250            //putting the active nodes into the stack
251            int lev=level[v];
252            if ( exc > 0 && lev < n && v != t )
253              {
254                next.set(v,first[lev]);
255                first[lev]=v;
256              }
257            //    active[lev].push(v);
258          }
259          break;
260        }
261      case GEN_FLOW:
262        {
263          NodeIt v;
264          for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
265
266          Num exc=0;
267          InEdgeIt e;
268          for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
269          OutEdgeIt f;
270          for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
271          excess.set(t,exc);
272          break;
273        }
274      case ZERO_FLOW:
275      case NO_FLOW:
276        {
277          NodeIt v;
278          for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
279          break;
280        }
281      }
282
283      preflowPreproc(fe, next, first,/*active*/ level_list, left, right);
284      //End of preprocessing
285
286
287      //Push/relabel on the highest level active nodes.
288      while ( true ) {
289        if ( b == 0 ) {
290          if ( !what_heur && !end && k > 0 ) {
291            b=k;
292            end=true;
293          } else break;
294        }
295
296        if ( !g->valid(first[b])/*active[b].empty()*/ ) --b;
297        else {
298          end=false;
299          Node w=first[b];
300          first[b]=next[w];
301          /*    Node w=active[b].top();
302                active[b].pop();*/
303          int newlevel=push(w,/*active*/next, first);
304          if ( excess[w] > 0 ) relabel(w, newlevel, /*active*/next, first, level_list,
305                                       left, right, b, k, what_heur);
306
307          ++numrelabel;
308          if ( numrelabel >= heur ) {
309            numrelabel=0;
310            if ( what_heur ) {
311              what_heur=0;
312              heur=heur0;
313              end=false;
314            } else {
315              what_heur=1;
316              heur=heur1;
317              b=k;
318            }
319          }
320        }
321      }
322
323      status=AFTER_PRE_FLOW_PHASE_1;
324    }
325
326
327    ///Runs the second phase of the preflow algorithm.
328
329    ///The preflow algorithm consists of two phases, this method runs
330    ///the second phase. After calling \ref preflowPhase1 and then
331    ///\ref preflowPhase2 the methods \ref flowValue, \ref minCut,
332    ///\ref minMinCut and \ref maxMinCut give proper results.
333    ///\pre \ref preflowPhase1 must be called before.
334    void preflowPhase2()
335    {
336
337      int k=n-2;  //bound on the highest level under n containing a node
338      int b=k;    //bound on the highest level under n of an active node
339
340   
341      VecFirst first(n, INVALID);
342      NNMap next(*g, INVALID); //maybe INVALID is not needed
343      //    VecStack active(n);
344      level.set(s,0);
345      std::queue<Node> bfs_queue;
346      bfs_queue.push(s);
347
348      while (!bfs_queue.empty()) {
349
350        Node v=bfs_queue.front();
351        bfs_queue.pop();
352        int l=level[v]+1;
353
354        InEdgeIt e;
355        for(g->first(e,v); g->valid(e); g->next(e)) {
356          if ( (*capacity)[e] <= (*flow)[e] ) continue;
357          Node u=g->tail(e);
358          if ( level[u] >= n ) {
359            bfs_queue.push(u);
360            level.set(u, l);
361            if ( excess[u] > 0 ) {
362              next.set(u,first[l]);
363              first[l]=u;
364              //active[l].push(u);
365            }
366          }
367        }
368
369        OutEdgeIt f;
370        for(g->first(f,v); g->valid(f); g->next(f)) {
371          if ( 0 >= (*flow)[f] ) continue;
372          Node u=g->head(f);
373          if ( level[u] >= n ) {
374            bfs_queue.push(u);
375            level.set(u, l);
376            if ( excess[u] > 0 ) {
377              next.set(u,first[l]);
378              first[l]=u;
379              //active[l].push(u);
380            }
381          }
382        }
383      }
384      b=n-2;
385
386      while ( true ) {
387
388        if ( b == 0 ) break;
389
390        if ( !g->valid(first[b])/*active[b].empty()*/ ) --b;
391        else {
392
393          Node w=first[b];
394          first[b]=next[w];
395          /*    Node w=active[b].top();
396                active[b].pop();*/
397          int newlevel=push(w,next, first/*active*/);
398
399          //relabel
400          if ( excess[w] > 0 ) {
401            level.set(w,++newlevel);
402            next.set(w,first[newlevel]);
403            first[newlevel]=w;
404            //active[newlevel].push(w);
405            b=newlevel;
406          }
407        }  // if stack[b] is nonempty
408      } // while(true)
409
410      status=AFTER_PRE_FLOW_PHASE_2;
411    }
412
413
414    /// Returns the maximum value of a flow.
415
416    /// Returns the maximum value of a flow, by counting the
417    /// over-flow of the target node \ref t.
418    /// It can be called already after running \ref preflowPhase1.
419    Num flowValue() const {
420      Num a=0;
421      for(InEdgeIt e(*g,t);g->valid(e);G.next(e)) a+=(*flow)[e];
422      for(OutEdgeIt e(*g,t);g->valid(e);G.next(e)) a-=(*flow)[e];
423
424      //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan   
425    }
426
427    ///Returns a minimum value cut after calling \ref preflowPhase1.
428
429    ///After the first phase of the preflow algorithm the maximum flow
430    ///value and a minimum value cut can already be computed. This
431    ///method can be called after running \ref preflowPhase1 for
432    ///obtaining a minimum value cut.
433    /// \warning Gives proper result only right after calling \ref
434    /// preflowPhase1.
435    /// \todo We have to make some status variable which shows the
436    /// actual state
437    /// of the class. This enables us to determine which methods are valid
438    /// for MinCut computation
439    template<typename _CutMap>
440    void actMinCut(_CutMap& M) const {
441      NodeIt v;
442      switch (status) {
443      case AFTER_PRE_FLOW_PHASE_1:
444        for(g->first(v); g->valid(v); g->next(v)) {
445          if (level[v] < n) {
446            M.set(v, false);
447          } else {
448            M.set(v, true);
449          }
450        }
451        break;
452      case AFTER_PRE_FLOW_PHASE_2:
453      case AFTER_NOTHING:
454        minMinCut(M);
455        break;
456      case AFTER_AUGMENTING:
457        for(g->first(v); g->valid(v); g->next(v)) {
458          if (level[v]) {
459            M.set(v, true);
460          } else {
461            M.set(v, false);
462          }
463        }
464        break;
465      case AFTER_FAST_AUGMENTING:
466        for(g->first(v); g->valid(v); g->next(v)) {
467          if (level[v]==number_of_augmentations) {
468            M.set(v, true);
469          } else {
470            M.set(v, false);
471          }
472        }
473        break;
474      }
475    }
476
477    ///Returns the inclusionwise minimum of the minimum value cuts.
478
479    ///Sets \c M to the characteristic vector of the minimum value cut
480    ///which is inclusionwise minimum. It is computed by processing
481    ///a bfs from the source node \c s in the residual graph.
482    ///\pre M should be a node map of bools initialized to false.
483    ///\pre \c flow must be a maximum flow.
484    template<typename _CutMap>
485    void minMinCut(_CutMap& M) const {
486      std::queue<Node> queue;
487
488      M.set(s,true);
489      queue.push(s);
490
491      while (!queue.empty()) {
492        Node w=queue.front();
493        queue.pop();
494
495        OutEdgeIt e;
496        for(g->first(e,w) ; g->valid(e); g->next(e)) {
497          Node v=g->head(e);
498          if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
499            queue.push(v);
500            M.set(v, true);
501          }
502        }
503
504        InEdgeIt f;
505        for(g->first(f,w) ; g->valid(f); g->next(f)) {
506          Node v=g->tail(f);
507          if (!M[v] && (*flow)[f] > 0 ) {
508            queue.push(v);
509            M.set(v, true);
510          }
511        }
512      }
513    }
514
515    ///Returns the inclusionwise maximum of the minimum value cuts.
516
517    ///Sets \c M to the characteristic vector of the minimum value cut
518    ///which is inclusionwise maximum. It is computed by processing a
519    ///backward bfs from the target node \c t in the residual graph.
520    ///\pre M should be a node map of bools initialized to false.
521    ///\pre \c flow must be a maximum flow.
522    template<typename _CutMap>
523    void maxMinCut(_CutMap& M) const {
524
525      NodeIt v;
526      for(g->first(v) ; g->valid(v); g->next(v)) {
527        M.set(v, true);
528      }
529
530      std::queue<Node> queue;
531
532      M.set(t,false);
533      queue.push(t);
534
535      while (!queue.empty()) {
536        Node w=queue.front();
537        queue.pop();
538
539        InEdgeIt e;
540        for(g->first(e,w) ; g->valid(e); g->next(e)) {
541          Node v=g->tail(e);
542          if (M[v] && (*flow)[e] < (*capacity)[e] ) {
543            queue.push(v);
544            M.set(v, false);
545          }
546        }
547
548        OutEdgeIt f;
549        for(g->first(f,w) ; g->valid(f); g->next(f)) {
550          Node v=g->head(f);
551          if (M[v] && (*flow)[f] > 0 ) {
552            queue.push(v);
553            M.set(v, false);
554          }
555        }
556      }
557    }
558
559    ///Returns a minimum value cut.
560
561    ///Sets \c M to the characteristic vector of a minimum value cut.
562    ///\pre M should be a node map of bools initialized to false.
563    ///\pre \c flow must be a maximum flow.   
564    template<typename CutMap>
565    void minCut(CutMap& M) const { minMinCut(M); }
566
567    ///Resets the source node to \c _s.
568
569    ///Resets the source node to \c _s.
570    ///
571    void resetSource(Node _s) { s=_s; status=AFTER_NOTHING; }
572
573    ///Resets the target node to \c _t.
574
575    ///Resets the target node to \c _t.
576    ///
577    void resetTarget(Node _t) { t=_t; status=AFTER_NOTHING; }
578
579    /// Resets the edge map of the capacities to _cap.
580
581    /// Resets the edge map of the capacities to _cap.
582    ///
583    void resetCap(const CapMap& _cap)
584    { capacity=&_cap; status=AFTER_NOTHING; }
585
586    /// Resets the edge map of the flows to _flow.
587
588    /// Resets the edge map of the flows to _flow.
589    ///
590    void resetFlow(FlowMap& _flow) { flow=&_flow; status=AFTER_NOTHING; }
591
592
593  private:
594
595    int push(Node w, NNMap& next, VecFirst& first) {
596
597      int lev=level[w];
598      Num exc=excess[w];
599      int newlevel=n;       //bound on the next level of w
600
601      OutEdgeIt e;
602      for(g->first(e,w); g->valid(e); g->next(e)) {
603
604        if ( (*flow)[e] >= (*capacity)[e] ) continue;
605        Node v=g->head(e);
606
607        if( lev > level[v] ) { //Push is allowed now
608
609          if ( excess[v]<=0 && v!=t && v!=s ) {
610            next.set(v,first[level[v]]);
611            first[level[v]]=v;
612            //      int lev_v=level[v];
613            //active[lev_v].push(v);
614          }
615
616          Num cap=(*capacity)[e];
617          Num flo=(*flow)[e];
618          Num remcap=cap-flo;
619
620          if ( remcap >= exc ) { //A nonsaturating push.
621
622            flow->set(e, flo+exc);
623            excess.set(v, excess[v]+exc);
624            exc=0;
625            break;
626
627          } else { //A saturating push.
628            flow->set(e, cap);
629            excess.set(v, excess[v]+remcap);
630            exc-=remcap;
631          }
632        } else if ( newlevel > level[v] ) newlevel = level[v];
633      } //for out edges wv
634
635      if ( exc > 0 ) {
636        InEdgeIt e;
637        for(g->first(e,w); g->valid(e); g->next(e)) {
638
639          if( (*flow)[e] <= 0 ) continue;
640          Node v=g->tail(e);
641
642          if( lev > level[v] ) { //Push is allowed now
643
644            if ( excess[v]<=0 && v!=t && v!=s ) {
645              next.set(v,first[level[v]]);
646              first[level[v]]=v;
647              //int lev_v=level[v];
648              //active[lev_v].push(v);
649            }
650
651            Num flo=(*flow)[e];
652
653            if ( flo >= exc ) { //A nonsaturating push.
654
655              flow->set(e, flo-exc);
656              excess.set(v, excess[v]+exc);
657              exc=0;
658              break;
659            } else {  //A saturating push.
660
661              excess.set(v, excess[v]+flo);
662              exc-=flo;
663              flow->set(e,0);
664            }
665          } else if ( newlevel > level[v] ) newlevel = level[v];
666        } //for in edges vw
667
668      } // if w still has excess after the out edge for cycle
669
670      excess.set(w, exc);
671
672      return newlevel;
673    }
674
675
676    void preflowPreproc(FlowEnum fe, NNMap& next, VecFirst& first,
677                        VecNode& level_list, NNMap& left, NNMap& right)
678    {
679      std::queue<Node> bfs_queue;
680
681      switch (fe) {
682      case NO_FLOW:   //flow is already set to const zero in this case
683      case ZERO_FLOW:
684        {
685          //Reverse_bfs from t, to find the starting level.
686          level.set(t,0);
687          bfs_queue.push(t);
688
689          while (!bfs_queue.empty()) {
690
691            Node v=bfs_queue.front();
692            bfs_queue.pop();
693            int l=level[v]+1;
694
695            InEdgeIt e;
696            for(g->first(e,v); g->valid(e); g->next(e)) {
697              Node w=g->tail(e);
698              if ( level[w] == n && w != s ) {
699                bfs_queue.push(w);
700                Node z=level_list[l];
701                if ( g->valid(z) ) left.set(z,w);
702                right.set(w,z);
703                level_list[l]=w;
704                level.set(w, l);
705              }
706            }
707          }
708
709          //the starting flow
710          OutEdgeIt e;
711          for(g->first(e,s); g->valid(e); g->next(e))
712            {
713              Num c=(*capacity)[e];
714              if ( c <= 0 ) continue;
715              Node w=g->head(e);
716              if ( level[w] < n ) {
717                if ( excess[w] <= 0 && w!=t )
718                  {
719                    next.set(w,first[level[w]]);
720                    first[level[w]]=w;
721                    //active[level[w]].push(w);
722                  }
723                flow->set(e, c);
724                excess.set(w, excess[w]+c);
725              }
726            }
727          break;
728        }
729
730      case GEN_FLOW:
731      case PRE_FLOW:
732        {
733          //Reverse_bfs from t in the residual graph,
734          //to find the starting level.
735          level.set(t,0);
736          bfs_queue.push(t);
737
738          while (!bfs_queue.empty()) {
739
740            Node v=bfs_queue.front();
741            bfs_queue.pop();
742            int l=level[v]+1;
743
744            InEdgeIt e;
745            for(g->first(e,v); g->valid(e); g->next(e)) {
746              if ( (*capacity)[e] <= (*flow)[e] ) continue;
747              Node w=g->tail(e);
748              if ( level[w] == n && w != s ) {
749                bfs_queue.push(w);
750                Node z=level_list[l];
751                if ( g->valid(z) ) left.set(z,w);
752                right.set(w,z);
753                level_list[l]=w;
754                level.set(w, l);
755              }
756            }
757
758            OutEdgeIt f;
759            for(g->first(f,v); g->valid(f); g->next(f)) {
760              if ( 0 >= (*flow)[f] ) continue;
761              Node w=g->head(f);
762              if ( level[w] == n && w != s ) {
763                bfs_queue.push(w);
764                Node z=level_list[l];
765                if ( g->valid(z) ) left.set(z,w);
766                right.set(w,z);
767                level_list[l]=w;
768                level.set(w, l);
769              }
770            }
771          }
772
773
774          //the starting flow
775          OutEdgeIt e;
776          for(g->first(e,s); g->valid(e); g->next(e))
777            {
778              Num rem=(*capacity)[e]-(*flow)[e];
779              if ( rem <= 0 ) continue;
780              Node w=g->head(e);
781              if ( level[w] < n ) {
782                if ( excess[w] <= 0 && w!=t )
783                  {
784                    next.set(w,first[level[w]]);
785                    first[level[w]]=w;
786                    //active[level[w]].push(w);
787                  }   
788                flow->set(e, (*capacity)[e]);
789                excess.set(w, excess[w]+rem);
790              }
791            }
792
793          InEdgeIt f;
794          for(g->first(f,s); g->valid(f); g->next(f))
795            {
796              if ( (*flow)[f] <= 0 ) continue;
797              Node w=g->tail(f);
798              if ( level[w] < n ) {
799                if ( excess[w] <= 0 && w!=t )
800                  {
801                    next.set(w,first[level[w]]);
802                    first[level[w]]=w;
803                    //active[level[w]].push(w);
804                  }   
805                excess.set(w, excess[w]+(*flow)[f]);
806                flow->set(f, 0);
807              }
808            }
809          break;
810        } //case PRE_FLOW
811      }
812    } //preflowPreproc
813
814
815
816    void relabel(Node w, int newlevel, NNMap& next, VecFirst& first,
817                 VecNode& level_list, NNMap& left,
818                 NNMap& right, int& b, int& k, bool what_heur )
819    {
820
821      Num lev=level[w];
822
823      Node right_n=right[w];
824      Node left_n=left[w];
825
826      //unlacing starts
827      if ( g->valid(right_n) ) {
828        if ( g->valid(left_n) ) {
829          right.set(left_n, right_n);
830          left.set(right_n, left_n);
831        } else {
832          level_list[lev]=right_n;
833          left.set(right_n, INVALID);
834        }
835      } else {
836        if ( g->valid(left_n) ) {
837          right.set(left_n, INVALID);
838        } else {
839          level_list[lev]=INVALID;
840        }
841      }
842      //unlacing ends
843
844      if ( !g->valid(level_list[lev]) ) {
845
846        //gapping starts
847        for (int i=lev; i!=k ; ) {
848          Node v=level_list[++i];
849          while ( g->valid(v) ) {
850            level.set(v,n);
851            v=right[v];
852          }
853          level_list[i]=INVALID;
854          if ( !what_heur ) first[i]=INVALID;
855          /*{
856            while ( !active[i].empty() ) {
857            active[i].pop();    //FIXME: ezt szebben kene
858            }
859            }*/
860        }
861
862        level.set(w,n);
863        b=lev-1;
864        k=b;
865        //gapping ends
866
867      } else {
868
869        if ( newlevel == n ) level.set(w,n);
870        else {
871          level.set(w,++newlevel);
872          next.set(w,first[newlevel]);
873          first[newlevel]=w;
874          //      active[newlevel].push(w);
875          if ( what_heur ) b=newlevel;
876          if ( k < newlevel ) ++k;      //now k=newlevel
877          Node z=level_list[newlevel];
878          if ( g->valid(z) ) left.set(z,w);
879          right.set(w,z);
880          left.set(w,INVALID);
881          level_list[newlevel]=w;
882        }
883      }
884    } //relabel
885  };  //class MaxFlow
886} //namespace hugo
887
888#endif //HUGO_MAX_FLOW_H
889
890
891
892
Note: See TracBrowser for help on using the repository browser.