COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/jacint/preflow.h @ 468:3a2cb784750a

Last change on this file since 468:3a2cb784750a was 468:3a2cb784750a, checked in by marci, 20 years ago

nem irunk olyat hogy "void resetTarget(const Node _t) {t=_t;}" mert az a const az ott jobbara hulyeseg

File size: 14.2 KB
RevLine 
[109]1// -*- C++ -*-
[372]2
[109]3/*
4Heuristics:
5 2 phase
6 gap
7 list 'level_list' on the nodes on level i implemented by hand
[451]8 stack 'active' on the active nodes on level i
[109]9 runs heuristic 'highest label' for H1*n relabels
[113]10 runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
[109]11 
[451]12Parameters H0 and H1 are initialized to 20 and 1.
[109]13
[211]14Constructors:
[109]15
[372]16Preflow(Graph, Node, Node, CapMap, FlowMap, bool) : bool must be false if
17     FlowMap is not constant zero, and should be true if it is
[109]18
19Members:
20
[211]21void run()
[109]22
[468]23Num flowValue() : returns the value of a maximum flow
[109]24
25void minMinCut(CutMap& M) : sets M to the characteristic vector of the
[451]26     minimum min cut. M should be a map of bools initialized to false. ??Is it OK?
[109]27
28void maxMinCut(CutMap& M) : sets M to the characteristic vector of the
29     maximum min cut. M should be a map of bools initialized to false.
30
31void minCut(CutMap& M) : sets M to the characteristic vector of
32     a min cut. M should be a map of bools initialized to false.
33
34*/
35
[211]36#ifndef HUGO_PREFLOW_H
37#define HUGO_PREFLOW_H
[109]38
39#define H0 20
40#define H1 1
41
42#include <vector>
43#include <queue>
[451]44#include <stack>
[109]45
46namespace hugo {
47
[468]48  template <typename Graph, typename Num,
49            typename CapMap=typename Graph::template EdgeMap<Num>,
50            typename FlowMap=typename Graph::template EdgeMap<Num> >
[211]51  class Preflow {
[109]52   
[211]53    typedef typename Graph::Node Node;
[109]54    typedef typename Graph::NodeIt NodeIt;
55    typedef typename Graph::OutEdgeIt OutEdgeIt;
56    typedef typename Graph::InEdgeIt InEdgeIt;
[451]57
58    typedef typename std::vector<std::stack<Node> > VecStack;
59    typedef typename Graph::template NodeMap<Node> NNMap;
60    typedef typename std::vector<Node> VecNode;
61
[466]62    const Graph* g;
[211]63    Node s;
64    Node t;
[465]65    const CapMap* capacity; 
[451]66    FlowMap* flow;
67    int n;      //the number of nodes of G
68    typename Graph::template NodeMap<int> level;     
[468]69    typename Graph::template NodeMap<Num> excess;
[451]70
[109]71
72  public:
[451]73 
74    enum flowEnum{
75      ZERO_FLOW=0,
76      GEN_FLOW=1,
77      PREFLOW=2
78    };
79
[465]80    Preflow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
[451]81            FlowMap& _flow) :
[466]82      g(&_G), s(_s), t(_t), capacity(&_capacity),
[451]83      flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0) {}
84
85    void run() {
86      preflow( ZERO_FLOW );
87    }
[372]88   
[451]89    void preflow( flowEnum fe ) {
90      preflowPhase0(fe);
91      preflowPhase1();
92    }
93
94    void preflowPhase0( flowEnum fe ) {
[372]95     
[109]96      int heur0=(int)(H0*n);  //time while running 'bound decrease'
97      int heur1=(int)(H1*n);  //time while running 'highest label'
98      int heur=heur1;         //starting time interval (#of relabels)
[451]99      int numrelabel=0;
100     
[109]101      bool what_heur=1;       
[451]102      //It is 0 in case 'bound decrease' and 1 in case 'highest label'
103
[109]104      bool end=false;     
[451]105      //Needed for 'bound decrease', true means no active nodes are above bound b.
106
[109]107      int k=n-2;  //bound on the highest level under n containing a node
108      int b=k;    //bound on the highest level under n of an active node
109     
[451]110      VecStack active(n);
111     
[466]112      NNMap left(*g, INVALID);
113      NNMap right(*g, INVALID);
[451]114      VecNode level_list(n,INVALID);
115      //List of the nodes in level i<n, set to n.
[109]116
[451]117      NodeIt v;
[466]118      for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
[451]119      //setting each node to level n
120     
121      switch ( fe ) {
122      case PREFLOW:
123        {
124          //counting the excess
125          NodeIt v;
[466]126          for(g->first(v); g->valid(v); g->next(v)) {
[468]127            Num exc=0;
[372]128         
[451]129            InEdgeIt e;
[466]130            for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
[451]131            OutEdgeIt f;
[466]132            for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
[451]133           
134            excess.set(v,exc);   
135           
136            //putting the active nodes into the stack
137            int lev=level[v];
138            if ( exc > 0 && lev < n && v != t ) active[lev].push(v);
139          }
140          break;
141        }
142      case GEN_FLOW:
143        {
144          //Counting the excess of t
[468]145          Num exc=0;
[372]146         
147          InEdgeIt e;
[466]148          for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
[451]149          OutEdgeIt f;
[466]150          for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
[451]151         
152          excess.set(t,exc);   
153         
154          break;
155        }
[465]156//       default:
157//      break;
158//      ZERO_FLOW ize kell
159
[451]160      }
161     
162      preflowPreproc( fe, active, level_list, left, right );
163      //End of preprocessing
164     
165     
166      //Push/relabel on the highest level active nodes.
167      while ( true ) {
168        if ( b == 0 ) {
169          if ( !what_heur && !end && k > 0 ) {
170            b=k;
171            end=true;
172          } else break;
173        }
174       
175        if ( active[b].empty() ) --b;
176        else {
177          end=false; 
178          Node w=active[b].top();
179          active[b].pop();
180          int newlevel=push(w,active);
181          if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list,
182                                       left, right, b, k, what_heur);
183         
184          ++numrelabel;
185          if ( numrelabel >= heur ) {
186            numrelabel=0;
187            if ( what_heur ) {
188              what_heur=0;
189              heur=heur0;
190              end=false;
191            } else {
192              what_heur=1;
193              heur=heur1;
194              b=k;
[372]195            }
[109]196          }
[451]197        }
198      }
199    }
200
201
202    void preflowPhase1() {
203     
204      int k=n-2;  //bound on the highest level under n containing a node
205      int b=k;    //bound on the highest level under n of an active node
206     
207      VecStack active(n);
208      level.set(s,0);
209      std::queue<Node> bfs_queue;
210      bfs_queue.push(s);
211           
212      while (!bfs_queue.empty()) {
213       
214        Node v=bfs_queue.front();       
215        bfs_queue.pop();
216        int l=level[v]+1;
217             
218        InEdgeIt e;
[466]219        for(g->first(e,v); g->valid(e); g->next(e)) {
[451]220          if ( (*capacity)[e] == (*flow)[e] ) continue;
[466]221          Node u=g->tail(e);
[451]222          if ( level[u] >= n ) {
223            bfs_queue.push(u);
224            level.set(u, l);
225            if ( excess[u] > 0 ) active[l].push(u);
226          }
[109]227        }
[451]228       
229        OutEdgeIt f;
[466]230        for(g->first(f,v); g->valid(f); g->next(f)) {
[451]231          if ( 0 == (*flow)[f] ) continue;
[466]232          Node u=g->head(f);
[451]233          if ( level[u] >= n ) {
234            bfs_queue.push(u);
235            level.set(u, l);
236            if ( excess[u] > 0 ) active[l].push(u);
[109]237          }
238        }
[372]239      }
[451]240      b=n-2;
[372]241
[109]242      while ( true ) {
243       
[451]244        if ( b == 0 ) break;
245
246        if ( active[b].empty() ) --b;
[109]247        else {
[451]248          Node w=active[b].top();
249          active[b].pop();
250          int newlevel=push(w,active);   
[109]251
[451]252          //relabel
253          if ( excess[w] > 0 ) {
[109]254            level.set(w,++newlevel);
[451]255            active[newlevel].push(w);
[109]256            b=newlevel;
[451]257          }
[109]258        }  // if stack[b] is nonempty
259      } // while(true)
260    }
261
262
[451]263    //Returns the maximum value of a flow.
[468]264    Num flowValue() {
[451]265      return excess[t];
266    }
[109]267
[451]268    //should be used only between preflowPhase0 and preflowPhase1
269    template<typename _CutMap>
270    void actMinCut(_CutMap& M) {
[211]271      NodeIt v;
[466]272      for(g->first(v); g->valid(v); g->next(v))
273      if ( level[v] < n ) {
274        M.set(v,false);
275      } else {
276        M.set(v,true);
277      }
[211]278    }
[109]279
280
281
282    /*
283      Returns the minimum min cut, by a bfs from s in the residual graph.
284    */
285    template<typename _CutMap>
286    void minMinCut(_CutMap& M) {
287   
[211]288      std::queue<Node> queue;
[109]289     
290      M.set(s,true);     
291      queue.push(s);
292
293      while (!queue.empty()) {
[211]294        Node w=queue.front();
[109]295        queue.pop();
296
[211]297        OutEdgeIt e;
[466]298        for(g->first(e,w) ; g->valid(e); g->next(e)) {
299          Node v=g->head(e);
[451]300          if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
[109]301            queue.push(v);
302            M.set(v, true);
303          }
304        }
305
[211]306        InEdgeIt f;
[466]307        for(g->first(f,w) ; g->valid(f); g->next(f)) {
308          Node v=g->tail(f);
[451]309          if (!M[v] && (*flow)[f] > 0 ) {
[109]310            queue.push(v);
311            M.set(v, true);
312          }
313        }
314      }
315    }
316
317
318 
319    /*
320      Returns the maximum min cut, by a reverse bfs
321      from t in the residual graph.
322    */
323   
324    template<typename _CutMap>
325    void maxMinCut(_CutMap& M) {
[451]326
327      NodeIt v;
[466]328      for(g->first(v) ; g->valid(v); g->next(v)) {
[451]329        M.set(v, true);
330      }
331
[211]332      std::queue<Node> queue;
[109]333     
[451]334      M.set(t,false);       
[109]335      queue.push(t);
336
337      while (!queue.empty()) {
[211]338        Node w=queue.front();
[109]339        queue.pop();
340
[211]341
342        InEdgeIt e;
[466]343        for(g->first(e,w) ; g->valid(e); g->next(e)) {
344          Node v=g->tail(e);
[451]345          if (M[v] && (*flow)[e] < (*capacity)[e] ) {
[109]346            queue.push(v);
[451]347            M.set(v, false);
[109]348          }
349        }
[211]350       
351        OutEdgeIt f;
[466]352        for(g->first(f,w) ; g->valid(f); g->next(f)) {
353          Node v=g->head(f);
[451]354          if (M[v] && (*flow)[f] > 0 ) {
[109]355            queue.push(v);
[451]356            M.set(v, false);
[109]357          }
358        }
359      }
360    }
361
362
363    template<typename CutMap>
364    void minCut(CutMap& M) {
365      minMinCut(M);
366    }
367
[468]368    void resetTarget(Node _t) {t=_t;}
369    void resetSource(Node _s) {s=_s;}
[372]370   
[468]371    void resetCap(const CapMap& _cap) {
[451]372      capacity=&_cap;
373    }
374   
[468]375    void resetFlow(FlowMap& _flow) {
[451]376      flow=&_flow;
[372]377    }
378
379
[451]380  private:
381
382    int push(const Node w, VecStack& active) {
383     
384      int lev=level[w];
[468]385      Num exc=excess[w];
[451]386      int newlevel=n;       //bound on the next level of w
387         
388      OutEdgeIt e;
[466]389      for(g->first(e,w); g->valid(e); g->next(e)) {
[451]390           
391        if ( (*flow)[e] == (*capacity)[e] ) continue;
[466]392        Node v=g->head(e);           
[451]393           
394        if( lev > level[v] ) { //Push is allowed now
395         
396          if ( excess[v]==0 && v!=t && v!=s ) {
397            int lev_v=level[v];
398            active[lev_v].push(v);
399          }
400         
[468]401          Num cap=(*capacity)[e];
402          Num flo=(*flow)[e];
403          Num remcap=cap-flo;
[451]404         
405          if ( remcap >= exc ) { //A nonsaturating push.
406           
407            flow->set(e, flo+exc);
408            excess.set(v, excess[v]+exc);
409            exc=0;
410            break;
411           
412          } else { //A saturating push.
413            flow->set(e, cap);
414            excess.set(v, excess[v]+remcap);
415            exc-=remcap;
416          }
417        } else if ( newlevel > level[v] ) newlevel = level[v];
418      } //for out edges wv
419     
420      if ( exc > 0 ) { 
421        InEdgeIt e;
[466]422        for(g->first(e,w); g->valid(e); g->next(e)) {
[451]423         
424          if( (*flow)[e] == 0 ) continue;
[466]425          Node v=g->tail(e);
[451]426         
427          if( lev > level[v] ) { //Push is allowed now
428           
429            if ( excess[v]==0 && v!=t && v!=s ) {
430              int lev_v=level[v];
431              active[lev_v].push(v);
432            }
433           
[468]434            Num flo=(*flow)[e];
[451]435           
436            if ( flo >= exc ) { //A nonsaturating push.
437             
438              flow->set(e, flo-exc);
439              excess.set(v, excess[v]+exc);
440              exc=0;
441              break;
442            } else {  //A saturating push.
443             
444              excess.set(v, excess[v]+flo);
445              exc-=flo;
446              flow->set(e,0);
447            } 
448          } else if ( newlevel > level[v] ) newlevel = level[v];
449        } //for in edges vw
450       
451      } // if w still has excess after the out edge for cycle
452     
453      excess.set(w, exc);
454     
455      return newlevel;
456     }
457
458
459    void preflowPreproc ( flowEnum fe, VecStack& active,
460                          VecNode& level_list, NNMap& left, NNMap& right ) {
461
462      std::queue<Node> bfs_queue;
463     
464      switch ( fe ) {
465      case ZERO_FLOW:
466        {
467          //Reverse_bfs from t, to find the starting level.
468          level.set(t,0);
469          bfs_queue.push(t);
470       
471          while (!bfs_queue.empty()) {
472           
473            Node v=bfs_queue.front();   
474            bfs_queue.pop();
475            int l=level[v]+1;
476           
477            InEdgeIt e;
[466]478            for(g->first(e,v); g->valid(e); g->next(e)) {
479              Node w=g->tail(e);
[451]480              if ( level[w] == n && w != s ) {
481                bfs_queue.push(w);
482                Node first=level_list[l];
[466]483                if ( g->valid(first) ) left.set(first,w);
[451]484                right.set(w,first);
485                level_list[l]=w;
486                level.set(w, l);
487              }
488            }
489          }
490         
491          //the starting flow
492          OutEdgeIt e;
[466]493          for(g->first(e,s); g->valid(e); g->next(e))
[451]494            {
[468]495              Num c=(*capacity)[e];
[451]496              if ( c == 0 ) continue;
[466]497              Node w=g->head(e);
[451]498              if ( level[w] < n ) {       
499                if ( excess[w] == 0 && w!=t ) active[level[w]].push(w);
500                flow->set(e, c);
501                excess.set(w, excess[w]+c);
502              }
503            }
504          break;
505        }
506       
507      case GEN_FLOW:
508      case PREFLOW:
509        {
510          //Reverse_bfs from t in the residual graph,
511          //to find the starting level.
512          level.set(t,0);
513          bfs_queue.push(t);
514         
515          while (!bfs_queue.empty()) {
516           
517            Node v=bfs_queue.front();   
518            bfs_queue.pop();
519            int l=level[v]+1;
520           
521            InEdgeIt e;
[466]522            for(g->first(e,v); g->valid(e); g->next(e)) {
[451]523              if ( (*capacity)[e] == (*flow)[e] ) continue;
[466]524              Node w=g->tail(e);
[451]525              if ( level[w] == n && w != s ) {
526                bfs_queue.push(w);
527                Node first=level_list[l];
[466]528                if ( g->valid(first) ) left.set(first,w);
[451]529                right.set(w,first);
530                level_list[l]=w;
531                level.set(w, l);
532              }
533            }
534           
535            OutEdgeIt f;
[466]536            for(g->first(f,v); g->valid(f); g->next(f)) {
[451]537              if ( 0 == (*flow)[f] ) continue;
[466]538              Node w=g->head(f);
[451]539              if ( level[w] == n && w != s ) {
540                bfs_queue.push(w);
541                Node first=level_list[l];
[466]542                if ( g->valid(first) ) left.set(first,w);
[451]543                right.set(w,first);
544                level_list[l]=w;
545                level.set(w, l);
546              }
547            }
548          }
549         
550         
551          //the starting flow
552          OutEdgeIt e;
[466]553          for(g->first(e,s); g->valid(e); g->next(e))
[451]554            {
[468]555              Num rem=(*capacity)[e]-(*flow)[e];
[451]556              if ( rem == 0 ) continue;
[466]557              Node w=g->head(e);
[451]558              if ( level[w] < n ) {       
559                if ( excess[w] == 0 && w!=t ) active[level[w]].push(w);
560                flow->set(e, (*capacity)[e]);
561                excess.set(w, excess[w]+rem);
562              }
563            }
564         
565          InEdgeIt f;
[466]566          for(g->first(f,s); g->valid(f); g->next(f))
[451]567            {
568              if ( (*flow)[f] == 0 ) continue;
[466]569              Node w=g->tail(f);
[451]570              if ( level[w] < n ) {       
571                if ( excess[w] == 0 && w!=t ) active[level[w]].push(w);
572                excess.set(w, excess[w]+(*flow)[f]);
573                flow->set(f, 0);
574              }
575            } 
576          break;
577        } //case PREFLOW
578      }
579    } //preflowPreproc
580
581
582
583    void relabel( const Node w, int newlevel, VecStack& active, 
584                  VecNode& level_list, NNMap& left,
585                  NNMap& right, int& b, int& k, const bool what_heur ) {
586
[468]587      Num lev=level[w];
[451]588     
589      Node right_n=right[w];
590      Node left_n=left[w];
591     
592      //unlacing starts
[466]593      if ( g->valid(right_n) ) {
594        if ( g->valid(left_n) ) {
[451]595          right.set(left_n, right_n);
596          left.set(right_n, left_n);
597        } else {
598          level_list[lev]=right_n;   
599          left.set(right_n, INVALID);
600        }
601      } else {
[466]602        if ( g->valid(left_n) ) {
[451]603          right.set(left_n, INVALID);
604        } else {
605          level_list[lev]=INVALID;   
606        }
607      }
608      //unlacing ends
609               
[466]610      if ( !g->valid(level_list[lev]) ) {
[451]611             
612        //gapping starts
613        for (int i=lev; i!=k ; ) {
614          Node v=level_list[++i];
[466]615          while ( g->valid(v) ) {
[451]616            level.set(v,n);
617            v=right[v];
618          }
619          level_list[i]=INVALID;
620          if ( !what_heur ) {
621            while ( !active[i].empty() ) {
622              active[i].pop();    //FIXME: ezt szebben kene
623            }
624          }         
625        }
626       
627        level.set(w,n);
628        b=lev-1;
629        k=b;
630        //gapping ends
631       
632      } else {
633       
634        if ( newlevel == n ) level.set(w,n);
635        else {
636          level.set(w,++newlevel);
637          active[newlevel].push(w);
638          if ( what_heur ) b=newlevel;
639          if ( k < newlevel ) ++k;      //now k=newlevel
640          Node first=level_list[newlevel];
[466]641          if ( g->valid(first) ) left.set(first,w);
[451]642          right.set(w,first);
643          left.set(w,INVALID);
644          level_list[newlevel]=w;
645        }
646      }
647     
648    } //relabel
649   
[109]650
651  };
652
[174]653} //namespace hugo
[109]654
[451]655#endif //HUGO_PREFLOW_H
[109]656
657
[174]658
659
Note: See TracBrowser for help on using the repository browser.