COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/jacint/preflow.h @ 466:cd40ecf4d2a9

Last change on this file since 466:cd40ecf4d2a9 was 466:cd40ecf4d2a9, checked in by marci, 20 years ago

preflow, maxflow comp

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
[211]23T 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
48  template <typename Graph, typename T,
[389]49            typename CapMap=typename Graph::template EdgeMap<T>,
50            typename FlowMap=typename Graph::template EdgeMap<T> >
[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;     
69    typename Graph::template NodeMap<T> excess;
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)) {
[451]127            T 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
145          T 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.
264    T flowValue() {
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
[372]368   
[451]369    void resetTarget (const Node _t) {t=_t;}
370
371    void resetSource (const Node _s) {s=_s;}
[372]372   
[451]373    void resetCap (const CapMap& _cap) {
374      capacity=&_cap;
375    }
376   
377    void resetFlow (FlowMap& _flow) {
378      flow=&_flow;
[372]379    }
380
381
[451]382  private:
383
384    int push(const Node w, VecStack& active) {
385     
386      int lev=level[w];
387      T exc=excess[w];
388      int newlevel=n;       //bound on the next level of w
389         
390      OutEdgeIt e;
[466]391      for(g->first(e,w); g->valid(e); g->next(e)) {
[451]392           
393        if ( (*flow)[e] == (*capacity)[e] ) continue;
[466]394        Node v=g->head(e);           
[451]395           
396        if( lev > level[v] ) { //Push is allowed now
397         
398          if ( excess[v]==0 && v!=t && v!=s ) {
399            int lev_v=level[v];
400            active[lev_v].push(v);
401          }
402         
403          T cap=(*capacity)[e];
404          T flo=(*flow)[e];
405          T remcap=cap-flo;
406         
407          if ( remcap >= exc ) { //A nonsaturating push.
408           
409            flow->set(e, flo+exc);
410            excess.set(v, excess[v]+exc);
411            exc=0;
412            break;
413           
414          } else { //A saturating push.
415            flow->set(e, cap);
416            excess.set(v, excess[v]+remcap);
417            exc-=remcap;
418          }
419        } else if ( newlevel > level[v] ) newlevel = level[v];
420      } //for out edges wv
421     
422      if ( exc > 0 ) { 
423        InEdgeIt e;
[466]424        for(g->first(e,w); g->valid(e); g->next(e)) {
[451]425         
426          if( (*flow)[e] == 0 ) continue;
[466]427          Node v=g->tail(e);
[451]428         
429          if( lev > level[v] ) { //Push is allowed now
430           
431            if ( excess[v]==0 && v!=t && v!=s ) {
432              int lev_v=level[v];
433              active[lev_v].push(v);
434            }
435           
436            T flo=(*flow)[e];
437           
438            if ( flo >= exc ) { //A nonsaturating push.
439             
440              flow->set(e, flo-exc);
441              excess.set(v, excess[v]+exc);
442              exc=0;
443              break;
444            } else {  //A saturating push.
445             
446              excess.set(v, excess[v]+flo);
447              exc-=flo;
448              flow->set(e,0);
449            } 
450          } else if ( newlevel > level[v] ) newlevel = level[v];
451        } //for in edges vw
452       
453      } // if w still has excess after the out edge for cycle
454     
455      excess.set(w, exc);
456     
457      return newlevel;
458     }
459
460
461    void preflowPreproc ( flowEnum fe, VecStack& active,
462                          VecNode& level_list, NNMap& left, NNMap& right ) {
463
464      std::queue<Node> bfs_queue;
465     
466      switch ( fe ) {
467      case ZERO_FLOW:
468        {
469          //Reverse_bfs from t, to find the starting level.
470          level.set(t,0);
471          bfs_queue.push(t);
472       
473          while (!bfs_queue.empty()) {
474           
475            Node v=bfs_queue.front();   
476            bfs_queue.pop();
477            int l=level[v]+1;
478           
479            InEdgeIt e;
[466]480            for(g->first(e,v); g->valid(e); g->next(e)) {
481              Node w=g->tail(e);
[451]482              if ( level[w] == n && w != s ) {
483                bfs_queue.push(w);
484                Node first=level_list[l];
[466]485                if ( g->valid(first) ) left.set(first,w);
[451]486                right.set(w,first);
487                level_list[l]=w;
488                level.set(w, l);
489              }
490            }
491          }
492         
493          //the starting flow
494          OutEdgeIt e;
[466]495          for(g->first(e,s); g->valid(e); g->next(e))
[451]496            {
497              T c=(*capacity)[e];
498              if ( c == 0 ) continue;
[466]499              Node w=g->head(e);
[451]500              if ( level[w] < n ) {       
501                if ( excess[w] == 0 && w!=t ) active[level[w]].push(w);
502                flow->set(e, c);
503                excess.set(w, excess[w]+c);
504              }
505            }
506          break;
507        }
508       
509      case GEN_FLOW:
510      case PREFLOW:
511        {
512          //Reverse_bfs from t in the residual graph,
513          //to find the starting level.
514          level.set(t,0);
515          bfs_queue.push(t);
516         
517          while (!bfs_queue.empty()) {
518           
519            Node v=bfs_queue.front();   
520            bfs_queue.pop();
521            int l=level[v]+1;
522           
523            InEdgeIt e;
[466]524            for(g->first(e,v); g->valid(e); g->next(e)) {
[451]525              if ( (*capacity)[e] == (*flow)[e] ) continue;
[466]526              Node w=g->tail(e);
[451]527              if ( level[w] == n && w != s ) {
528                bfs_queue.push(w);
529                Node first=level_list[l];
[466]530                if ( g->valid(first) ) left.set(first,w);
[451]531                right.set(w,first);
532                level_list[l]=w;
533                level.set(w, l);
534              }
535            }
536           
537            OutEdgeIt f;
[466]538            for(g->first(f,v); g->valid(f); g->next(f)) {
[451]539              if ( 0 == (*flow)[f] ) continue;
[466]540              Node w=g->head(f);
[451]541              if ( level[w] == n && w != s ) {
542                bfs_queue.push(w);
543                Node first=level_list[l];
[466]544                if ( g->valid(first) ) left.set(first,w);
[451]545                right.set(w,first);
546                level_list[l]=w;
547                level.set(w, l);
548              }
549            }
550          }
551         
552         
553          //the starting flow
554          OutEdgeIt e;
[466]555          for(g->first(e,s); g->valid(e); g->next(e))
[451]556            {
557              T rem=(*capacity)[e]-(*flow)[e];
558              if ( rem == 0 ) continue;
[466]559              Node w=g->head(e);
[451]560              if ( level[w] < n ) {       
561                if ( excess[w] == 0 && w!=t ) active[level[w]].push(w);
562                flow->set(e, (*capacity)[e]);
563                excess.set(w, excess[w]+rem);
564              }
565            }
566         
567          InEdgeIt f;
[466]568          for(g->first(f,s); g->valid(f); g->next(f))
[451]569            {
570              if ( (*flow)[f] == 0 ) continue;
[466]571              Node w=g->tail(f);
[451]572              if ( level[w] < n ) {       
573                if ( excess[w] == 0 && w!=t ) active[level[w]].push(w);
574                excess.set(w, excess[w]+(*flow)[f]);
575                flow->set(f, 0);
576              }
577            } 
578          break;
579        } //case PREFLOW
580      }
581    } //preflowPreproc
582
583
584
585    void relabel( const Node w, int newlevel, VecStack& active, 
586                  VecNode& level_list, NNMap& left,
587                  NNMap& right, int& b, int& k, const bool what_heur ) {
588
589      T lev=level[w];   
590     
591      Node right_n=right[w];
592      Node left_n=left[w];
593     
594      //unlacing starts
[466]595      if ( g->valid(right_n) ) {
596        if ( g->valid(left_n) ) {
[451]597          right.set(left_n, right_n);
598          left.set(right_n, left_n);
599        } else {
600          level_list[lev]=right_n;   
601          left.set(right_n, INVALID);
602        }
603      } else {
[466]604        if ( g->valid(left_n) ) {
[451]605          right.set(left_n, INVALID);
606        } else {
607          level_list[lev]=INVALID;   
608        }
609      }
610      //unlacing ends
611               
[466]612      if ( !g->valid(level_list[lev]) ) {
[451]613             
614        //gapping starts
615        for (int i=lev; i!=k ; ) {
616          Node v=level_list[++i];
[466]617          while ( g->valid(v) ) {
[451]618            level.set(v,n);
619            v=right[v];
620          }
621          level_list[i]=INVALID;
622          if ( !what_heur ) {
623            while ( !active[i].empty() ) {
624              active[i].pop();    //FIXME: ezt szebben kene
625            }
626          }         
627        }
628       
629        level.set(w,n);
630        b=lev-1;
631        k=b;
632        //gapping ends
633       
634      } else {
635       
636        if ( newlevel == n ) level.set(w,n);
637        else {
638          level.set(w,++newlevel);
639          active[newlevel].push(w);
640          if ( what_heur ) b=newlevel;
641          if ( k < newlevel ) ++k;      //now k=newlevel
642          Node first=level_list[newlevel];
[466]643          if ( g->valid(first) ) left.set(first,w);
[451]644          right.set(w,first);
645          left.set(w,INVALID);
646          level_list[newlevel]=w;
647        }
648      }
649     
650    } //relabel
651   
[109]652
653  };
654
[174]655} //namespace hugo
[109]656
[451]657#endif //HUGO_PREFLOW_H
[109]658
659
[174]660
661
Note: See TracBrowser for help on using the repository browser.