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
Line 
1// -*- C++ -*-
2
3/*
4Heuristics:
5 2 phase
6 gap
7 list 'level_list' on the nodes on level i implemented by hand
8 stack 'active' on the active nodes on level i
9 runs heuristic 'highest label' for H1*n relabels
10 runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
11 
12Parameters H0 and H1 are initialized to 20 and 1.
13
14Constructors:
15
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
18
19Members:
20
21void run()
22
23T flowValue() : returns the value of a maximum flow
24
25void minMinCut(CutMap& M) : sets M to the characteristic vector of the
26     minimum min cut. M should be a map of bools initialized to false. ??Is it OK?
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
36#ifndef HUGO_PREFLOW_H
37#define HUGO_PREFLOW_H
38
39#define H0 20
40#define H1 1
41
42#include <vector>
43#include <queue>
44#include <stack>
45
46namespace hugo {
47
48  template <typename Graph, typename T,
49            typename CapMap=typename Graph::template EdgeMap<T>,
50            typename FlowMap=typename Graph::template EdgeMap<T> >
51  class Preflow {
52   
53    typedef typename Graph::Node Node;
54    typedef typename Graph::NodeIt NodeIt;
55    typedef typename Graph::OutEdgeIt OutEdgeIt;
56    typedef typename Graph::InEdgeIt InEdgeIt;
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
62    const Graph* g;
63    Node s;
64    Node t;
65    const CapMap* capacity; 
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
71
72  public:
73 
74    enum flowEnum{
75      ZERO_FLOW=0,
76      GEN_FLOW=1,
77      PREFLOW=2
78    };
79
80    Preflow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
81            FlowMap& _flow) :
82      g(&_G), s(_s), t(_t), capacity(&_capacity),
83      flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0) {}
84
85    void run() {
86      preflow( ZERO_FLOW );
87    }
88   
89    void preflow( flowEnum fe ) {
90      preflowPhase0(fe);
91      preflowPhase1();
92    }
93
94    void preflowPhase0( flowEnum fe ) {
95     
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)
99      int numrelabel=0;
100     
101      bool what_heur=1;       
102      //It is 0 in case 'bound decrease' and 1 in case 'highest label'
103
104      bool end=false;     
105      //Needed for 'bound decrease', true means no active nodes are above bound b.
106
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     
110      VecStack active(n);
111     
112      NNMap left(*g, INVALID);
113      NNMap right(*g, INVALID);
114      VecNode level_list(n,INVALID);
115      //List of the nodes in level i<n, set to n.
116
117      NodeIt v;
118      for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
119      //setting each node to level n
120     
121      switch ( fe ) {
122      case PREFLOW:
123        {
124          //counting the excess
125          NodeIt v;
126          for(g->first(v); g->valid(v); g->next(v)) {
127            T exc=0;
128         
129            InEdgeIt e;
130            for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
131            OutEdgeIt f;
132            for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
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;
146         
147          InEdgeIt e;
148          for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
149          OutEdgeIt f;
150          for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
151         
152          excess.set(t,exc);   
153         
154          break;
155        }
156//       default:
157//      break;
158//      ZERO_FLOW ize kell
159
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;
195            }
196          }
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;
219        for(g->first(e,v); g->valid(e); g->next(e)) {
220          if ( (*capacity)[e] == (*flow)[e] ) continue;
221          Node u=g->tail(e);
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          }
227        }
228       
229        OutEdgeIt f;
230        for(g->first(f,v); g->valid(f); g->next(f)) {
231          if ( 0 == (*flow)[f] ) continue;
232          Node u=g->head(f);
233          if ( level[u] >= n ) {
234            bfs_queue.push(u);
235            level.set(u, l);
236            if ( excess[u] > 0 ) active[l].push(u);
237          }
238        }
239      }
240      b=n-2;
241
242      while ( true ) {
243       
244        if ( b == 0 ) break;
245
246        if ( active[b].empty() ) --b;
247        else {
248          Node w=active[b].top();
249          active[b].pop();
250          int newlevel=push(w,active);   
251
252          //relabel
253          if ( excess[w] > 0 ) {
254            level.set(w,++newlevel);
255            active[newlevel].push(w);
256            b=newlevel;
257          }
258        }  // if stack[b] is nonempty
259      } // while(true)
260    }
261
262
263    //Returns the maximum value of a flow.
264    T flowValue() {
265      return excess[t];
266    }
267
268    //should be used only between preflowPhase0 and preflowPhase1
269    template<typename _CutMap>
270    void actMinCut(_CutMap& M) {
271      NodeIt v;
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      }
278    }
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   
288      std::queue<Node> queue;
289     
290      M.set(s,true);     
291      queue.push(s);
292
293      while (!queue.empty()) {
294        Node w=queue.front();
295        queue.pop();
296
297        OutEdgeIt e;
298        for(g->first(e,w) ; g->valid(e); g->next(e)) {
299          Node v=g->head(e);
300          if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
301            queue.push(v);
302            M.set(v, true);
303          }
304        }
305
306        InEdgeIt f;
307        for(g->first(f,w) ; g->valid(f); g->next(f)) {
308          Node v=g->tail(f);
309          if (!M[v] && (*flow)[f] > 0 ) {
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) {
326
327      NodeIt v;
328      for(g->first(v) ; g->valid(v); g->next(v)) {
329        M.set(v, true);
330      }
331
332      std::queue<Node> queue;
333     
334      M.set(t,false);       
335      queue.push(t);
336
337      while (!queue.empty()) {
338        Node w=queue.front();
339        queue.pop();
340
341
342        InEdgeIt e;
343        for(g->first(e,w) ; g->valid(e); g->next(e)) {
344          Node v=g->tail(e);
345          if (M[v] && (*flow)[e] < (*capacity)[e] ) {
346            queue.push(v);
347            M.set(v, false);
348          }
349        }
350       
351        OutEdgeIt f;
352        for(g->first(f,w) ; g->valid(f); g->next(f)) {
353          Node v=g->head(f);
354          if (M[v] && (*flow)[f] > 0 ) {
355            queue.push(v);
356            M.set(v, false);
357          }
358        }
359      }
360    }
361
362
363    template<typename CutMap>
364    void minCut(CutMap& M) {
365      minMinCut(M);
366    }
367
368   
369    void resetTarget (const Node _t) {t=_t;}
370
371    void resetSource (const Node _s) {s=_s;}
372   
373    void resetCap (const CapMap& _cap) {
374      capacity=&_cap;
375    }
376   
377    void resetFlow (FlowMap& _flow) {
378      flow=&_flow;
379    }
380
381
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;
391      for(g->first(e,w); g->valid(e); g->next(e)) {
392           
393        if ( (*flow)[e] == (*capacity)[e] ) continue;
394        Node v=g->head(e);           
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;
424        for(g->first(e,w); g->valid(e); g->next(e)) {
425         
426          if( (*flow)[e] == 0 ) continue;
427          Node v=g->tail(e);
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;
480            for(g->first(e,v); g->valid(e); g->next(e)) {
481              Node w=g->tail(e);
482              if ( level[w] == n && w != s ) {
483                bfs_queue.push(w);
484                Node first=level_list[l];
485                if ( g->valid(first) ) left.set(first,w);
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;
495          for(g->first(e,s); g->valid(e); g->next(e))
496            {
497              T c=(*capacity)[e];
498              if ( c == 0 ) continue;
499              Node w=g->head(e);
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;
524            for(g->first(e,v); g->valid(e); g->next(e)) {
525              if ( (*capacity)[e] == (*flow)[e] ) continue;
526              Node w=g->tail(e);
527              if ( level[w] == n && w != s ) {
528                bfs_queue.push(w);
529                Node first=level_list[l];
530                if ( g->valid(first) ) left.set(first,w);
531                right.set(w,first);
532                level_list[l]=w;
533                level.set(w, l);
534              }
535            }
536           
537            OutEdgeIt f;
538            for(g->first(f,v); g->valid(f); g->next(f)) {
539              if ( 0 == (*flow)[f] ) continue;
540              Node w=g->head(f);
541              if ( level[w] == n && w != s ) {
542                bfs_queue.push(w);
543                Node first=level_list[l];
544                if ( g->valid(first) ) left.set(first,w);
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;
555          for(g->first(e,s); g->valid(e); g->next(e))
556            {
557              T rem=(*capacity)[e]-(*flow)[e];
558              if ( rem == 0 ) continue;
559              Node w=g->head(e);
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;
568          for(g->first(f,s); g->valid(f); g->next(f))
569            {
570              if ( (*flow)[f] == 0 ) continue;
571              Node w=g->tail(f);
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
595      if ( g->valid(right_n) ) {
596        if ( g->valid(left_n) ) {
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 {
604        if ( g->valid(left_n) ) {
605          right.set(left_n, INVALID);
606        } else {
607          level_list[lev]=INVALID;   
608        }
609      }
610      //unlacing ends
611               
612      if ( !g->valid(level_list[lev]) ) {
613             
614        //gapping starts
615        for (int i=lev; i!=k ; ) {
616          Node v=level_list[++i];
617          while ( g->valid(v) ) {
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];
643          if ( g->valid(first) ) left.set(first,w);
644          right.set(w,first);
645          left.set(w,INVALID);
646          level_list[newlevel]=w;
647        }
648      }
649     
650    } //relabel
651   
652
653  };
654
655} //namespace hugo
656
657#endif //HUGO_PREFLOW_H
658
659
660
661
Note: See TracBrowser for help on using the repository browser.