COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/hugo/max_flow.h @ 745:d976ba609099

Last change on this file since 745:d976ba609099 was 745:d976ba609099, checked in by marci, 17 years ago

jacint javitgatott.

File size: 22.8 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
221      NNMap left(*g, INVALID);
222      NNMap right(*g, INVALID);
223      VecNode level_list(n,INVALID);
224      //List of the nodes in level i<n, set to n.
225
226      NodeIt v;
227      for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
228      //setting each node to level n
229
230      if ( fe == NO_FLOW ) {
231        EdgeIt e;
232        for(g->first(e); g->valid(e); g->next(e)) flow->set(e,0);
233      }
234
235      switch (fe) { //computing the excess
236      case PRE_FLOW:
237        {
238          NodeIt v;
239          for(g->first(v); g->valid(v); g->next(v)) {
240            Num exc=0;
241
242            InEdgeIt e;
243            for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
244            OutEdgeIt f;
245            for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
246
247            excess.set(v,exc);
248
249            //putting the active nodes into the stack
250            int lev=level[v];
251            if ( exc > 0 && lev < n && v != t )
252              {
253                next.set(v,first[lev]);
254                first[lev]=v;
255              }
256          }
257          break;
258        }
259      case GEN_FLOW:
260        {
261          NodeIt v;
262          for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
263
264          Num exc=0;
265          InEdgeIt e;
266          for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
267          OutEdgeIt f;
268          for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
269          excess.set(t,exc);
270          break;
271        }
272      case ZERO_FLOW:
273      case NO_FLOW:
274        {
275          NodeIt v;
276          for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
277          break;
278        }
279      }
280
281      preflowPreproc(fe, next, first, level_list, left, right);
282      //End of preprocessing
283
284
285      //Push/relabel on the highest level active nodes.
286      while ( true ) {
287        if ( b == 0 ) {
288          if ( !what_heur && !end && k > 0 ) {
289            b=k;
290            end=true;
291          } else break;
292        }
293
294        if ( !g->valid(first[b]) ) --b;
295        else {
296          end=false;
297          Node w=first[b];
298          first[b]=next[w];
299          int newlevel=push(w, next, first);
300          if ( excess[w] > 0 ) relabel(w, newlevel, next, first, level_list,
301                                       left, right, b, k, what_heur);
302
303          ++numrelabel;
304          if ( numrelabel >= heur ) {
305            numrelabel=0;
306            if ( what_heur ) {
307              what_heur=0;
308              heur=heur0;
309              end=false;
310            } else {
311              what_heur=1;
312              heur=heur1;
313              b=k;
314            }
315          }
316        }
317      }
318
319      status=AFTER_PRE_FLOW_PHASE_1;
320    }
321
322
323    ///Runs the second phase of the preflow algorithm.
324
325    ///The preflow algorithm consists of two phases, this method runs
326    ///the second phase. After calling \ref preflowPhase1 and then
327    ///\ref preflowPhase2 the methods \ref flowValue, \ref minCut,
328    ///\ref minMinCut and \ref maxMinCut give proper results.
329    ///\pre \ref preflowPhase1 must be called before.
330    void preflowPhase2()
331    {
332
333      int k=n-2;  //bound on the highest level under n containing a node
334      int b=k;    //bound on the highest level under n of an active node
335
336   
337      VecFirst first(n, INVALID);
338      NNMap next(*g, INVALID); //maybe INVALID is not needed
339      level.set(s,0);
340      std::queue<Node> bfs_queue;
341      bfs_queue.push(s);
342
343      while (!bfs_queue.empty()) {
344
345        Node v=bfs_queue.front();
346        bfs_queue.pop();
347        int l=level[v]+1;
348
349        InEdgeIt e;
350        for(g->first(e,v); g->valid(e); g->next(e)) {
351          if ( (*capacity)[e] <= (*flow)[e] ) continue;
352          Node u=g->tail(e);
353          if ( level[u] >= n ) {
354            bfs_queue.push(u);
355            level.set(u, l);
356            if ( excess[u] > 0 ) {
357              next.set(u,first[l]);
358              first[l]=u;
359            }
360          }
361        }
362
363        OutEdgeIt f;
364        for(g->first(f,v); g->valid(f); g->next(f)) {
365          if ( 0 >= (*flow)[f] ) continue;
366          Node u=g->head(f);
367          if ( level[u] >= n ) {
368            bfs_queue.push(u);
369            level.set(u, l);
370            if ( excess[u] > 0 ) {
371              next.set(u,first[l]);
372              first[l]=u;
373            }
374          }
375        }
376      }
377      b=n-2;
378
379      while ( true ) {
380
381        if ( b == 0 ) break;
382
383        if ( !g->valid(first[b]) ) --b;
384        else {
385
386          Node w=first[b];
387          first[b]=next[w];
388          int newlevel=push(w,next, first/*active*/);
389
390          //relabel
391          if ( excess[w] > 0 ) {
392            level.set(w,++newlevel);
393            next.set(w,first[newlevel]);
394            first[newlevel]=w;
395            b=newlevel;
396          }
397        }  // if stack[b] is nonempty
398      } // while(true)
399
400      status=AFTER_PRE_FLOW_PHASE_2;
401    }
402
403
404    /// Returns the maximum value of a flow.
405
406    /// Returns the maximum value of a flow, by counting the
407    /// over-flow of the target node \ref t.
408    /// It can be called already after running \ref preflowPhase1.
409    Num flowValue() const {
410      Num a=0;
411      for(InEdgeIt e(*g,t);g->valid(e);g->next(e)) a+=(*flow)[e];
412      for(OutEdgeIt e(*g,t);g->valid(e);g->next(e)) a-=(*flow)[e];
413      return a;
414      //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan   
415    }
416    Num flowValue2() const {
417      return excess[t];
418//       Num a=0;
419//       for(InEdgeIt e(*g,t);g->valid(e);g->next(e)) a+=(*flow)[e];
420//       for(OutEdgeIt e(*g,t);g->valid(e);g->next(e)) a-=(*flow)[e];
421//       return a;
422//       //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan 
423     
424    }
425
426    ///Returns a minimum value cut after calling \ref preflowPhase1.
427
428    ///After the first phase of the preflow algorithm the maximum flow
429    ///value and a minimum value cut can already be computed. This
430    ///method can be called after running \ref preflowPhase1 for
431    ///obtaining a minimum value cut.
432    /// \warning Gives proper result only right after calling \ref
433    /// preflowPhase1.
434    /// \todo We have to make some status variable which shows the
435    /// actual state
436    /// of the class. This enables us to determine which methods are valid
437    /// for MinCut computation
438    template<typename _CutMap>
439    void actMinCut(_CutMap& M) const {
440      NodeIt v;
441      switch (status) {
442      case AFTER_PRE_FLOW_PHASE_1:
443        for(g->first(v); g->valid(v); g->next(v)) {
444          if (level[v] < n) {
445            M.set(v, false);
446          } else {
447            M.set(v, true);
448          }
449        }
450        break;
451      case AFTER_PRE_FLOW_PHASE_2:
452      case AFTER_NOTHING:
453      case AFTER_AUGMENTING:
454      case AFTER_FAST_AUGMENTING:
455        minMinCut(M);
456        break;
457      }
458    }
459
460    ///Returns the inclusionwise minimum of the minimum value cuts.
461
462    ///Sets \c M to the characteristic vector of the minimum value cut
463    ///which is inclusionwise minimum. It is computed by processing
464    ///a bfs from the source node \c s in the residual graph.
465    ///\pre M should be a node map of bools initialized to false.
466    ///\pre \c flow must be a maximum flow.
467    template<typename _CutMap>
468    void minMinCut(_CutMap& M) const {
469      std::queue<Node> queue;
470
471      M.set(s,true);
472      queue.push(s);
473
474      while (!queue.empty()) {
475        Node w=queue.front();
476        queue.pop();
477
478        OutEdgeIt e;
479        for(g->first(e,w) ; g->valid(e); g->next(e)) {
480          Node v=g->head(e);
481          if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
482            queue.push(v);
483            M.set(v, true);
484          }
485        }
486
487        InEdgeIt f;
488        for(g->first(f,w) ; g->valid(f); g->next(f)) {
489          Node v=g->tail(f);
490          if (!M[v] && (*flow)[f] > 0 ) {
491            queue.push(v);
492            M.set(v, true);
493          }
494        }
495      }
496    }
497
498    ///Returns the inclusionwise maximum of the minimum value cuts.
499
500    ///Sets \c M to the characteristic vector of the minimum value cut
501    ///which is inclusionwise maximum. It is computed by processing a
502    ///backward bfs from the target node \c t in the residual graph.
503    ///\pre M should be a node map of bools initialized to false.
504    ///\pre \c flow must be a maximum flow.
505    template<typename _CutMap>
506    void maxMinCut(_CutMap& M) const {
507
508      NodeIt v;
509      for(g->first(v) ; g->valid(v); g->next(v)) {
510        M.set(v, true);
511      }
512
513      std::queue<Node> queue;
514
515      M.set(t,false);
516      queue.push(t);
517
518      while (!queue.empty()) {
519        Node w=queue.front();
520        queue.pop();
521
522        InEdgeIt e;
523        for(g->first(e,w) ; g->valid(e); g->next(e)) {
524          Node v=g->tail(e);
525          if (M[v] && (*flow)[e] < (*capacity)[e] ) {
526            queue.push(v);
527            M.set(v, false);
528          }
529        }
530
531        OutEdgeIt f;
532        for(g->first(f,w) ; g->valid(f); g->next(f)) {
533          Node v=g->head(f);
534          if (M[v] && (*flow)[f] > 0 ) {
535            queue.push(v);
536            M.set(v, false);
537          }
538        }
539      }
540    }
541
542    ///Returns a minimum value cut.
543
544    ///Sets \c M to the characteristic vector of a minimum value cut.
545    ///\pre M should be a node map of bools initialized to false.
546    ///\pre \c flow must be a maximum flow.   
547    template<typename CutMap>
548    void minCut(CutMap& M) const { minMinCut(M); }
549
550    ///Resets the source node to \c _s.
551
552    ///Resets the source node to \c _s.
553    ///
554    void resetSource(Node _s) { s=_s; status=AFTER_NOTHING; }
555
556    ///Resets the target node to \c _t.
557
558    ///Resets the target node to \c _t.
559    ///
560    void resetTarget(Node _t) { t=_t; status=AFTER_NOTHING; }
561
562    /// Resets the edge map of the capacities to _cap.
563
564    /// Resets the edge map of the capacities to _cap.
565    ///
566    void resetCap(const CapMap& _cap)
567    { capacity=&_cap; status=AFTER_NOTHING; }
568
569    /// Resets the edge map of the flows to _flow.
570
571    /// Resets the edge map of the flows to _flow.
572    ///
573    void resetFlow(FlowMap& _flow) { flow=&_flow; status=AFTER_NOTHING; }
574
575
576  private:
577
578    int push(Node w, NNMap& next, VecFirst& first) {
579
580      int lev=level[w];
581      Num exc=excess[w];
582      int newlevel=n;       //bound on the next level of w
583
584      OutEdgeIt e;
585      for(g->first(e,w); g->valid(e); g->next(e)) {
586
587        if ( (*flow)[e] >= (*capacity)[e] ) continue;
588        Node v=g->head(e);
589
590        if( lev > level[v] ) { //Push is allowed now
591
592          if ( excess[v]<=0 && v!=t && v!=s ) {
593            next.set(v,first[level[v]]);
594            first[level[v]]=v;
595          }
596
597          Num cap=(*capacity)[e];
598          Num flo=(*flow)[e];
599          Num remcap=cap-flo;
600
601          if ( remcap >= exc ) { //A nonsaturating push.
602
603            flow->set(e, flo+exc);
604            excess.set(v, excess[v]+exc);
605            exc=0;
606            break;
607
608          } else { //A saturating push.
609            flow->set(e, cap);
610            excess.set(v, excess[v]+remcap);
611            exc-=remcap;
612          }
613        } else if ( newlevel > level[v] ) newlevel = level[v];
614      } //for out edges wv
615
616      if ( exc > 0 ) {
617        InEdgeIt e;
618        for(g->first(e,w); g->valid(e); g->next(e)) {
619
620          if( (*flow)[e] <= 0 ) continue;
621          Node v=g->tail(e);
622
623          if( lev > level[v] ) { //Push is allowed now
624
625            if ( excess[v]<=0 && v!=t && v!=s ) {
626              next.set(v,first[level[v]]);
627              first[level[v]]=v;
628            }
629
630            Num flo=(*flow)[e];
631
632            if ( flo >= exc ) { //A nonsaturating push.
633
634              flow->set(e, flo-exc);
635              excess.set(v, excess[v]+exc);
636              exc=0;
637              break;
638            } else {  //A saturating push.
639
640              excess.set(v, excess[v]+flo);
641              exc-=flo;
642              flow->set(e,0);
643            }
644          } else if ( newlevel > level[v] ) newlevel = level[v];
645        } //for in edges vw
646
647      } // if w still has excess after the out edge for cycle
648
649      excess.set(w, exc);
650
651      return newlevel;
652    }
653
654
655    void preflowPreproc(FlowEnum fe, NNMap& next, VecFirst& first,
656                        VecNode& level_list, NNMap& left, NNMap& right)
657    {
658      std::queue<Node> bfs_queue;
659
660      switch (fe) {
661      case NO_FLOW:   //flow is already set to const zero in this case
662      case ZERO_FLOW:
663        {
664          //Reverse_bfs from t, to find the starting level.
665          level.set(t,0);
666          bfs_queue.push(t);
667
668          while (!bfs_queue.empty()) {
669
670            Node v=bfs_queue.front();
671            bfs_queue.pop();
672            int l=level[v]+1;
673
674            InEdgeIt e;
675            for(g->first(e,v); g->valid(e); g->next(e)) {
676              Node w=g->tail(e);
677              if ( level[w] == n && w != s ) {
678                bfs_queue.push(w);
679                Node z=level_list[l];
680                if ( g->valid(z) ) left.set(z,w);
681                right.set(w,z);
682                level_list[l]=w;
683                level.set(w, l);
684              }
685            }
686          }
687
688          //the starting flow
689          OutEdgeIt e;
690          for(g->first(e,s); g->valid(e); g->next(e))
691            {
692              Num c=(*capacity)[e];
693              if ( c <= 0 ) continue;
694              Node w=g->head(e);
695              if ( level[w] < n ) {
696                if ( excess[w] <= 0 && w!=t )
697                  {
698                    next.set(w,first[level[w]]);
699                    first[level[w]]=w;
700                  }
701                flow->set(e, c);
702                excess.set(w, excess[w]+c);
703              }
704            }
705          break;
706        }
707
708      case GEN_FLOW:
709      case PRE_FLOW:
710        {
711          //Reverse_bfs from t in the residual graph,
712          //to find the starting level.
713          level.set(t,0);
714          bfs_queue.push(t);
715
716          while (!bfs_queue.empty()) {
717
718            Node v=bfs_queue.front();
719            bfs_queue.pop();
720            int l=level[v]+1;
721
722            InEdgeIt e;
723            for(g->first(e,v); g->valid(e); g->next(e)) {
724              if ( (*capacity)[e] <= (*flow)[e] ) continue;
725              Node w=g->tail(e);
726              if ( level[w] == n && w != s ) {
727                bfs_queue.push(w);
728                Node z=level_list[l];
729                if ( g->valid(z) ) left.set(z,w);
730                right.set(w,z);
731                level_list[l]=w;
732                level.set(w, l);
733              }
734            }
735
736            OutEdgeIt f;
737            for(g->first(f,v); g->valid(f); g->next(f)) {
738              if ( 0 >= (*flow)[f] ) continue;
739              Node w=g->head(f);
740              if ( level[w] == n && w != s ) {
741                bfs_queue.push(w);
742                Node z=level_list[l];
743                if ( g->valid(z) ) left.set(z,w);
744                right.set(w,z);
745                level_list[l]=w;
746                level.set(w, l);
747              }
748            }
749          }
750
751
752          //the starting flow
753          OutEdgeIt e;
754          for(g->first(e,s); g->valid(e); g->next(e))
755            {
756              Num rem=(*capacity)[e]-(*flow)[e];
757              if ( rem <= 0 ) continue;
758              Node w=g->head(e);
759              if ( level[w] < n ) {
760                if ( excess[w] <= 0 && w!=t )
761                  {
762                    next.set(w,first[level[w]]);
763                    first[level[w]]=w;
764                  }   
765                flow->set(e, (*capacity)[e]);
766                excess.set(w, excess[w]+rem);
767              }
768            }
769
770          InEdgeIt f;
771          for(g->first(f,s); g->valid(f); g->next(f))
772            {
773              if ( (*flow)[f] <= 0 ) continue;
774              Node w=g->tail(f);
775              if ( level[w] < n ) {
776                if ( excess[w] <= 0 && w!=t )
777                  {
778                    next.set(w,first[level[w]]);
779                    first[level[w]]=w;
780                  }   
781                excess.set(w, excess[w]+(*flow)[f]);
782                flow->set(f, 0);
783              }
784            }
785          break;
786        } //case PRE_FLOW
787      }
788    } //preflowPreproc
789
790
791
792    void relabel(Node w, int newlevel, NNMap& next, VecFirst& first,
793                 VecNode& level_list, NNMap& left,
794                 NNMap& right, int& b, int& k, bool what_heur )
795    {
796
797      Num lev=level[w];
798
799      Node right_n=right[w];
800      Node left_n=left[w];
801
802      //unlacing starts
803      if ( g->valid(right_n) ) {
804        if ( g->valid(left_n) ) {
805          right.set(left_n, right_n);
806          left.set(right_n, left_n);
807        } else {
808          level_list[lev]=right_n;
809          left.set(right_n, INVALID);
810        }
811      } else {
812        if ( g->valid(left_n) ) {
813          right.set(left_n, INVALID);
814        } else {
815          level_list[lev]=INVALID;
816        }
817      }
818      //unlacing ends
819
820      if ( !g->valid(level_list[lev]) ) {
821
822        //gapping starts
823        for (int i=lev; i!=k ; ) {
824          Node v=level_list[++i];
825          while ( g->valid(v) ) {
826            level.set(v,n);
827            v=right[v];
828          }
829          level_list[i]=INVALID;
830          if ( !what_heur ) first[i]=INVALID;
831        }
832
833        level.set(w,n);
834        b=lev-1;
835        k=b;
836        //gapping ends
837
838      } else {
839
840        if ( newlevel == n ) level.set(w,n);
841        else {
842          level.set(w,++newlevel);
843          next.set(w,first[newlevel]);
844          first[newlevel]=w;
845          if ( what_heur ) b=newlevel;
846          if ( k < newlevel ) ++k;      //now k=newlevel
847          Node z=level_list[newlevel];
848          if ( g->valid(z) ) left.set(z,w);
849          right.set(w,z);
850          left.set(w,INVALID);
851          level_list[newlevel]=w;
852        }
853      }
854    } //relabel
855  };  //class MaxFlow
856} //namespace hugo
857
858#endif //HUGO_MAX_FLOW_H
859
860
861
862
Note: See TracBrowser for help on using the repository browser.