COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/hugo/max_flow.h @ 736:ba76a7f56b23

Last change on this file since 736:ba76a7f56b23 was 735:2859c45c31dd, checked in by Alpar Juttner, 20 years ago

gcc-3.4 tries to compile the _unused_ members of
an instantiated template class

File size: 23.1 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      }
457    }
458
459    ///Returns the inclusionwise minimum of the minimum value cuts.
460
461    ///Sets \c M to the characteristic vector of the minimum value cut
462    ///which is inclusionwise minimum. It is computed by processing
463    ///a bfs from the source node \c s in the residual graph.
464    ///\pre M should be a node map of bools initialized to false.
465    ///\pre \c flow must be a maximum flow.
466    template<typename _CutMap>
467    void minMinCut(_CutMap& M) const {
468      std::queue<Node> queue;
469
470      M.set(s,true);
471      queue.push(s);
472
473      while (!queue.empty()) {
474        Node w=queue.front();
475        queue.pop();
476
477        OutEdgeIt e;
478        for(g->first(e,w) ; g->valid(e); g->next(e)) {
479          Node v=g->head(e);
480          if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
481            queue.push(v);
482            M.set(v, true);
483          }
484        }
485
486        InEdgeIt f;
487        for(g->first(f,w) ; g->valid(f); g->next(f)) {
488          Node v=g->tail(f);
489          if (!M[v] && (*flow)[f] > 0 ) {
490            queue.push(v);
491            M.set(v, true);
492          }
493        }
494      }
495    }
496
497    ///Returns the inclusionwise maximum of the minimum value cuts.
498
499    ///Sets \c M to the characteristic vector of the minimum value cut
500    ///which is inclusionwise maximum. It is computed by processing a
501    ///backward bfs from the target node \c t in the residual graph.
502    ///\pre M should be a node map of bools initialized to false.
503    ///\pre \c flow must be a maximum flow.
504    template<typename _CutMap>
505    void maxMinCut(_CutMap& M) const {
506
507      NodeIt v;
508      for(g->first(v) ; g->valid(v); g->next(v)) {
509        M.set(v, true);
510      }
511
512      std::queue<Node> queue;
513
514      M.set(t,false);
515      queue.push(t);
516
517      while (!queue.empty()) {
518        Node w=queue.front();
519        queue.pop();
520
521        InEdgeIt e;
522        for(g->first(e,w) ; g->valid(e); g->next(e)) {
523          Node v=g->tail(e);
524          if (M[v] && (*flow)[e] < (*capacity)[e] ) {
525            queue.push(v);
526            M.set(v, false);
527          }
528        }
529
530        OutEdgeIt f;
531        for(g->first(f,w) ; g->valid(f); g->next(f)) {
532          Node v=g->head(f);
533          if (M[v] && (*flow)[f] > 0 ) {
534            queue.push(v);
535            M.set(v, false);
536          }
537        }
538      }
539    }
540
541    ///Returns a minimum value cut.
542
543    ///Sets \c M to the characteristic vector of a minimum value cut.
544    ///\pre M should be a node map of bools initialized to false.
545    ///\pre \c flow must be a maximum flow.   
546    template<typename CutMap>
547    void minCut(CutMap& M) const { minMinCut(M); }
548
549    ///Resets the source node to \c _s.
550
551    ///Resets the source node to \c _s.
552    ///
553    void resetSource(Node _s) { s=_s; status=AFTER_NOTHING; }
554
555    ///Resets the target node to \c _t.
556
557    ///Resets the target node to \c _t.
558    ///
559    void resetTarget(Node _t) { t=_t; status=AFTER_NOTHING; }
560
561    /// Resets the edge map of the capacities to _cap.
562
563    /// Resets the edge map of the capacities to _cap.
564    ///
565    void resetCap(const CapMap& _cap)
566    { capacity=&_cap; status=AFTER_NOTHING; }
567
568    /// Resets the edge map of the flows to _flow.
569
570    /// Resets the edge map of the flows to _flow.
571    ///
572    void resetFlow(FlowMap& _flow) { flow=&_flow; status=AFTER_NOTHING; }
573
574
575  private:
576
577    int push(Node w, NNMap& next, VecFirst& first) {
578
579      int lev=level[w];
580      Num exc=excess[w];
581      int newlevel=n;       //bound on the next level of w
582
583      OutEdgeIt e;
584      for(g->first(e,w); g->valid(e); g->next(e)) {
585
586        if ( (*flow)[e] >= (*capacity)[e] ) continue;
587        Node v=g->head(e);
588
589        if( lev > level[v] ) { //Push is allowed now
590
591          if ( excess[v]<=0 && v!=t && v!=s ) {
592            next.set(v,first[level[v]]);
593            first[level[v]]=v;
594            //      int lev_v=level[v];
595            //active[lev_v].push(v);
596          }
597
598          Num cap=(*capacity)[e];
599          Num flo=(*flow)[e];
600          Num remcap=cap-flo;
601
602          if ( remcap >= exc ) { //A nonsaturating push.
603
604            flow->set(e, flo+exc);
605            excess.set(v, excess[v]+exc);
606            exc=0;
607            break;
608
609          } else { //A saturating push.
610            flow->set(e, cap);
611            excess.set(v, excess[v]+remcap);
612            exc-=remcap;
613          }
614        } else if ( newlevel > level[v] ) newlevel = level[v];
615      } //for out edges wv
616
617      if ( exc > 0 ) {
618        InEdgeIt e;
619        for(g->first(e,w); g->valid(e); g->next(e)) {
620
621          if( (*flow)[e] <= 0 ) continue;
622          Node v=g->tail(e);
623
624          if( lev > level[v] ) { //Push is allowed now
625
626            if ( excess[v]<=0 && v!=t && v!=s ) {
627              next.set(v,first[level[v]]);
628              first[level[v]]=v;
629              //int lev_v=level[v];
630              //active[lev_v].push(v);
631            }
632
633            Num flo=(*flow)[e];
634
635            if ( flo >= exc ) { //A nonsaturating push.
636
637              flow->set(e, flo-exc);
638              excess.set(v, excess[v]+exc);
639              exc=0;
640              break;
641            } else {  //A saturating push.
642
643              excess.set(v, excess[v]+flo);
644              exc-=flo;
645              flow->set(e,0);
646            }
647          } else if ( newlevel > level[v] ) newlevel = level[v];
648        } //for in edges vw
649
650      } // if w still has excess after the out edge for cycle
651
652      excess.set(w, exc);
653
654      return newlevel;
655    }
656
657
658    void preflowPreproc(FlowEnum fe, NNMap& next, VecFirst& first,
659                        VecNode& level_list, NNMap& left, NNMap& right)
660    {
661      std::queue<Node> bfs_queue;
662
663      switch (fe) {
664      case NO_FLOW:   //flow is already set to const zero in this case
665      case ZERO_FLOW:
666        {
667          //Reverse_bfs from t, to find the starting level.
668          level.set(t,0);
669          bfs_queue.push(t);
670
671          while (!bfs_queue.empty()) {
672
673            Node v=bfs_queue.front();
674            bfs_queue.pop();
675            int l=level[v]+1;
676
677            InEdgeIt e;
678            for(g->first(e,v); g->valid(e); g->next(e)) {
679              Node w=g->tail(e);
680              if ( level[w] == n && w != s ) {
681                bfs_queue.push(w);
682                Node z=level_list[l];
683                if ( g->valid(z) ) left.set(z,w);
684                right.set(w,z);
685                level_list[l]=w;
686                level.set(w, l);
687              }
688            }
689          }
690
691          //the starting flow
692          OutEdgeIt e;
693          for(g->first(e,s); g->valid(e); g->next(e))
694            {
695              Num c=(*capacity)[e];
696              if ( c <= 0 ) continue;
697              Node w=g->head(e);
698              if ( level[w] < n ) {
699                if ( excess[w] <= 0 && w!=t )
700                  {
701                    next.set(w,first[level[w]]);
702                    first[level[w]]=w;
703                    //active[level[w]].push(w);
704                  }
705                flow->set(e, c);
706                excess.set(w, excess[w]+c);
707              }
708            }
709          break;
710        }
711
712      case GEN_FLOW:
713      case PRE_FLOW:
714        {
715          //Reverse_bfs from t in the residual graph,
716          //to find the starting level.
717          level.set(t,0);
718          bfs_queue.push(t);
719
720          while (!bfs_queue.empty()) {
721
722            Node v=bfs_queue.front();
723            bfs_queue.pop();
724            int l=level[v]+1;
725
726            InEdgeIt e;
727            for(g->first(e,v); g->valid(e); g->next(e)) {
728              if ( (*capacity)[e] <= (*flow)[e] ) continue;
729              Node w=g->tail(e);
730              if ( level[w] == n && w != s ) {
731                bfs_queue.push(w);
732                Node z=level_list[l];
733                if ( g->valid(z) ) left.set(z,w);
734                right.set(w,z);
735                level_list[l]=w;
736                level.set(w, l);
737              }
738            }
739
740            OutEdgeIt f;
741            for(g->first(f,v); g->valid(f); g->next(f)) {
742              if ( 0 >= (*flow)[f] ) continue;
743              Node w=g->head(f);
744              if ( level[w] == n && w != s ) {
745                bfs_queue.push(w);
746                Node z=level_list[l];
747                if ( g->valid(z) ) left.set(z,w);
748                right.set(w,z);
749                level_list[l]=w;
750                level.set(w, l);
751              }
752            }
753          }
754
755
756          //the starting flow
757          OutEdgeIt e;
758          for(g->first(e,s); g->valid(e); g->next(e))
759            {
760              Num rem=(*capacity)[e]-(*flow)[e];
761              if ( rem <= 0 ) continue;
762              Node w=g->head(e);
763              if ( level[w] < n ) {
764                if ( excess[w] <= 0 && w!=t )
765                  {
766                    next.set(w,first[level[w]]);
767                    first[level[w]]=w;
768                    //active[level[w]].push(w);
769                  }   
770                flow->set(e, (*capacity)[e]);
771                excess.set(w, excess[w]+rem);
772              }
773            }
774
775          InEdgeIt f;
776          for(g->first(f,s); g->valid(f); g->next(f))
777            {
778              if ( (*flow)[f] <= 0 ) continue;
779              Node w=g->tail(f);
780              if ( level[w] < n ) {
781                if ( excess[w] <= 0 && w!=t )
782                  {
783                    next.set(w,first[level[w]]);
784                    first[level[w]]=w;
785                    //active[level[w]].push(w);
786                  }   
787                excess.set(w, excess[w]+(*flow)[f]);
788                flow->set(f, 0);
789              }
790            }
791          break;
792        } //case PRE_FLOW
793      }
794    } //preflowPreproc
795
796
797
798    void relabel(Node w, int newlevel, NNMap& next, VecFirst& first,
799                 VecNode& level_list, NNMap& left,
800                 NNMap& right, int& b, int& k, bool what_heur )
801    {
802
803      Num lev=level[w];
804
805      Node right_n=right[w];
806      Node left_n=left[w];
807
808      //unlacing starts
809      if ( g->valid(right_n) ) {
810        if ( g->valid(left_n) ) {
811          right.set(left_n, right_n);
812          left.set(right_n, left_n);
813        } else {
814          level_list[lev]=right_n;
815          left.set(right_n, INVALID);
816        }
817      } else {
818        if ( g->valid(left_n) ) {
819          right.set(left_n, INVALID);
820        } else {
821          level_list[lev]=INVALID;
822        }
823      }
824      //unlacing ends
825
826      if ( !g->valid(level_list[lev]) ) {
827
828        //gapping starts
829        for (int i=lev; i!=k ; ) {
830          Node v=level_list[++i];
831          while ( g->valid(v) ) {
832            level.set(v,n);
833            v=right[v];
834          }
835          level_list[i]=INVALID;
836          if ( !what_heur ) first[i]=INVALID;
837          /*{
838            while ( !active[i].empty() ) {
839            active[i].pop();    //FIXME: ezt szebben kene
840            }
841            }*/
842        }
843
844        level.set(w,n);
845        b=lev-1;
846        k=b;
847        //gapping ends
848
849      } else {
850
851        if ( newlevel == n ) level.set(w,n);
852        else {
853          level.set(w,++newlevel);
854          next.set(w,first[newlevel]);
855          first[newlevel]=w;
856          //      active[newlevel].push(w);
857          if ( what_heur ) b=newlevel;
858          if ( k < newlevel ) ++k;      //now k=newlevel
859          Node z=level_list[newlevel];
860          if ( g->valid(z) ) left.set(z,w);
861          right.set(w,z);
862          left.set(w,INVALID);
863          level_list[newlevel]=w;
864        }
865      }
866    } //relabel
867  };  //class MaxFlow
868} //namespace hugo
869
870#endif //HUGO_MAX_FLOW_H
871
872
873
874
Note: See TracBrowser for help on using the repository browser.