COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/jacint/preflow.h @ 470:b64956c701c9

Last change on this file since 470:b64956c701c9 was 470:b64956c701c9, checked in by jacint, 20 years ago

Comparison == changed to <=

File size: 14.1 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
23Num 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 Num,
49            typename CapMap=typename Graph::template EdgeMap<Num>,
50            typename FlowMap=typename Graph::template EdgeMap<Num> >
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<Num> 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            Num 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          Num 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      }
159     
160      preflowPreproc( fe, active, level_list, left, right );
161      //End of preprocessing
162     
163     
164      //Push/relabel on the highest level active nodes.
165      while ( true ) {
166        if ( b == 0 ) {
167          if ( !what_heur && !end && k > 0 ) {
168            b=k;
169            end=true;
170          } else break;
171        }
172       
173        if ( active[b].empty() ) --b;
174        else {
175          end=false; 
176          Node w=active[b].top();
177          active[b].pop();
178          int newlevel=push(w,active);
179          if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list,
180                                       left, right, b, k, what_heur);
181         
182          ++numrelabel;
183          if ( numrelabel >= heur ) {
184            numrelabel=0;
185            if ( what_heur ) {
186              what_heur=0;
187              heur=heur0;
188              end=false;
189            } else {
190              what_heur=1;
191              heur=heur1;
192              b=k;
193            }
194          }
195        }
196      }
197    }
198
199
200    void preflowPhase1() {
201     
202      int k=n-2;  //bound on the highest level under n containing a node
203      int b=k;    //bound on the highest level under n of an active node
204     
205      VecStack active(n);
206      level.set(s,0);
207      std::queue<Node> bfs_queue;
208      bfs_queue.push(s);
209           
210      while (!bfs_queue.empty()) {
211       
212        Node v=bfs_queue.front();       
213        bfs_queue.pop();
214        int l=level[v]+1;
215             
216        InEdgeIt e;
217        for(g->first(e,v); g->valid(e); g->next(e)) {
218          if ( (*capacity)[e] <= (*flow)[e] ) continue;
219          Node u=g->tail(e);
220          if ( level[u] >= n ) {
221            bfs_queue.push(u);
222            level.set(u, l);
223            if ( excess[u] > 0 ) active[l].push(u);
224          }
225        }
226       
227        OutEdgeIt f;
228        for(g->first(f,v); g->valid(f); g->next(f)) {
229          if ( 0 >= (*flow)[f] ) continue;
230          Node u=g->head(f);
231          if ( level[u] >= n ) {
232            bfs_queue.push(u);
233            level.set(u, l);
234            if ( excess[u] > 0 ) active[l].push(u);
235          }
236        }
237      }
238      b=n-2;
239
240      while ( true ) {
241       
242        if ( b == 0 ) break;
243
244        if ( active[b].empty() ) --b;
245        else {
246          Node w=active[b].top();
247          active[b].pop();
248          int newlevel=push(w,active);   
249
250          //relabel
251          if ( excess[w] > 0 ) {
252            level.set(w,++newlevel);
253            active[newlevel].push(w);
254            b=newlevel;
255          }
256        }  // if stack[b] is nonempty
257      } // while(true)
258    }
259
260
261    //Returns the maximum value of a flow.
262    Num flowValue() {
263      return excess[t];
264    }
265
266    //should be used only between preflowPhase0 and preflowPhase1
267    template<typename _CutMap>
268    void actMinCut(_CutMap& M) {
269      NodeIt v;
270      for(g->first(v); g->valid(v); g->next(v))
271      if ( level[v] < n ) {
272        M.set(v,false);
273      } else {
274        M.set(v,true);
275      }
276    }
277
278
279
280    /*
281      Returns the minimum min cut, by a bfs from s in the residual graph.
282    */
283    template<typename _CutMap>
284    void minMinCut(_CutMap& M) {
285   
286      std::queue<Node> queue;
287     
288      M.set(s,true);     
289      queue.push(s);
290
291      while (!queue.empty()) {
292        Node w=queue.front();
293        queue.pop();
294
295        OutEdgeIt e;
296        for(g->first(e,w) ; g->valid(e); g->next(e)) {
297          Node v=g->head(e);
298          if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
299            queue.push(v);
300            M.set(v, true);
301          }
302        }
303
304        InEdgeIt f;
305        for(g->first(f,w) ; g->valid(f); g->next(f)) {
306          Node v=g->tail(f);
307          if (!M[v] && (*flow)[f] > 0 ) {
308            queue.push(v);
309            M.set(v, true);
310          }
311        }
312      }
313    }
314
315
316 
317    /*
318      Returns the maximum min cut, by a reverse bfs
319      from t in the residual graph.
320    */
321   
322    template<typename _CutMap>
323    void maxMinCut(_CutMap& M) {
324
325      NodeIt v;
326      for(g->first(v) ; g->valid(v); g->next(v)) {
327        M.set(v, true);
328      }
329
330      std::queue<Node> queue;
331     
332      M.set(t,false);       
333      queue.push(t);
334
335      while (!queue.empty()) {
336        Node w=queue.front();
337        queue.pop();
338
339
340        InEdgeIt e;
341        for(g->first(e,w) ; g->valid(e); g->next(e)) {
342          Node v=g->tail(e);
343          if (M[v] && (*flow)[e] < (*capacity)[e] ) {
344            queue.push(v);
345            M.set(v, false);
346          }
347        }
348       
349        OutEdgeIt f;
350        for(g->first(f,w) ; g->valid(f); g->next(f)) {
351          Node v=g->head(f);
352          if (M[v] && (*flow)[f] > 0 ) {
353            queue.push(v);
354            M.set(v, false);
355          }
356        }
357      }
358    }
359
360
361    template<typename CutMap>
362    void minCut(CutMap& M) {
363      minMinCut(M);
364    }
365
366    void resetTarget(Node _t) {t=_t;}
367    void resetSource(Node _s) {s=_s;}
368   
369    void resetCap(const CapMap& _cap) {
370      capacity=&_cap;
371    }
372   
373    void resetFlow(FlowMap& _flow) {
374      flow=&_flow;
375    }
376
377
378  private:
379
380    int push(Node w, VecStack& active) {
381     
382      int lev=level[w];
383      Num exc=excess[w];
384      int newlevel=n;       //bound on the next level of w
385         
386      OutEdgeIt e;
387      for(g->first(e,w); g->valid(e); g->next(e)) {
388           
389        if ( (*flow)[e] >= (*capacity)[e] ) continue;
390        Node v=g->head(e);           
391           
392        if( lev > level[v] ) { //Push is allowed now
393         
394          if ( excess[v]<=0 && v!=t && v!=s ) {
395            int lev_v=level[v];
396            active[lev_v].push(v);
397          }
398         
399          Num cap=(*capacity)[e];
400          Num flo=(*flow)[e];
401          Num remcap=cap-flo;
402         
403          if ( remcap >= exc ) { //A nonsaturating push.
404           
405            flow->set(e, flo+exc);
406            excess.set(v, excess[v]+exc);
407            exc=0;
408            break;
409           
410          } else { //A saturating push.
411            flow->set(e, cap);
412            excess.set(v, excess[v]+remcap);
413            exc-=remcap;
414          }
415        } else if ( newlevel > level[v] ) newlevel = level[v];
416      } //for out edges wv
417     
418      if ( exc > 0 ) { 
419        InEdgeIt e;
420        for(g->first(e,w); g->valid(e); g->next(e)) {
421         
422          if( (*flow)[e] <= 0 ) continue;
423          Node v=g->tail(e);
424         
425          if( lev > level[v] ) { //Push is allowed now
426           
427            if ( excess[v]<=0 && v!=t && v!=s ) {
428              int lev_v=level[v];
429              active[lev_v].push(v);
430            }
431           
432            Num flo=(*flow)[e];
433           
434            if ( flo >= exc ) { //A nonsaturating push.
435             
436              flow->set(e, flo-exc);
437              excess.set(v, excess[v]+exc);
438              exc=0;
439              break;
440            } else {  //A saturating push.
441             
442              excess.set(v, excess[v]+flo);
443              exc-=flo;
444              flow->set(e,0);
445            } 
446          } else if ( newlevel > level[v] ) newlevel = level[v];
447        } //for in edges vw
448       
449      } // if w still has excess after the out edge for cycle
450     
451      excess.set(w, exc);
452     
453      return newlevel;
454     }
455
456
457    void preflowPreproc ( flowEnum fe, VecStack& active,
458                          VecNode& level_list, NNMap& left, NNMap& right ) {
459
460      std::queue<Node> bfs_queue;
461     
462      switch ( fe ) {
463      case ZERO_FLOW:
464        {
465          //Reverse_bfs from t, to find the starting level.
466          level.set(t,0);
467          bfs_queue.push(t);
468       
469          while (!bfs_queue.empty()) {
470           
471            Node v=bfs_queue.front();   
472            bfs_queue.pop();
473            int l=level[v]+1;
474           
475            InEdgeIt e;
476            for(g->first(e,v); g->valid(e); g->next(e)) {
477              Node w=g->tail(e);
478              if ( level[w] == n && w != s ) {
479                bfs_queue.push(w);
480                Node first=level_list[l];
481                if ( g->valid(first) ) left.set(first,w);
482                right.set(w,first);
483                level_list[l]=w;
484                level.set(w, l);
485              }
486            }
487          }
488         
489          //the starting flow
490          OutEdgeIt e;
491          for(g->first(e,s); g->valid(e); g->next(e))
492            {
493              Num c=(*capacity)[e];
494              if ( c <= 0 ) continue;
495              Node w=g->head(e);
496              if ( level[w] < n ) {       
497                if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
498                flow->set(e, c);
499                excess.set(w, excess[w]+c);
500              }
501            }
502          break;
503        }
504       
505      case GEN_FLOW:
506      case PREFLOW:
507        {
508          //Reverse_bfs from t in the residual graph,
509          //to find the starting level.
510          level.set(t,0);
511          bfs_queue.push(t);
512         
513          while (!bfs_queue.empty()) {
514           
515            Node v=bfs_queue.front();   
516            bfs_queue.pop();
517            int l=level[v]+1;
518           
519            InEdgeIt e;
520            for(g->first(e,v); g->valid(e); g->next(e)) {
521              if ( (*capacity)[e] <= (*flow)[e] ) continue;
522              Node w=g->tail(e);
523              if ( level[w] == n && w != s ) {
524                bfs_queue.push(w);
525                Node first=level_list[l];
526                if ( g->valid(first) ) left.set(first,w);
527                right.set(w,first);
528                level_list[l]=w;
529                level.set(w, l);
530              }
531            }
532           
533            OutEdgeIt f;
534            for(g->first(f,v); g->valid(f); g->next(f)) {
535              if ( 0 >= (*flow)[f] ) continue;
536              Node w=g->head(f);
537              if ( level[w] == n && w != s ) {
538                bfs_queue.push(w);
539                Node first=level_list[l];
540                if ( g->valid(first) ) left.set(first,w);
541                right.set(w,first);
542                level_list[l]=w;
543                level.set(w, l);
544              }
545            }
546          }
547         
548         
549          //the starting flow
550          OutEdgeIt e;
551          for(g->first(e,s); g->valid(e); g->next(e))
552            {
553              Num rem=(*capacity)[e]-(*flow)[e];
554              if ( rem <= 0 ) continue;
555              Node w=g->head(e);
556              if ( level[w] < n ) {       
557                if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
558                flow->set(e, (*capacity)[e]);
559                excess.set(w, excess[w]+rem);
560              }
561            }
562         
563          InEdgeIt f;
564          for(g->first(f,s); g->valid(f); g->next(f))
565            {
566              if ( (*flow)[f] <= 0 ) continue;
567              Node w=g->tail(f);
568              if ( level[w] < n ) {       
569                if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
570                excess.set(w, excess[w]+(*flow)[f]);
571                flow->set(f, 0);
572              }
573            } 
574          break;
575        } //case PREFLOW
576      }
577    } //preflowPreproc
578
579
580
581    void relabel(Node w, int newlevel, VecStack& active, 
582                 VecNode& level_list, NNMap& left,
583                 NNMap& right, int& b, int& k, bool what_heur ) {
584
585      Num lev=level[w];
586     
587      Node right_n=right[w];
588      Node left_n=left[w];
589     
590      //unlacing starts
591      if ( g->valid(right_n) ) {
592        if ( g->valid(left_n) ) {
593          right.set(left_n, right_n);
594          left.set(right_n, left_n);
595        } else {
596          level_list[lev]=right_n;   
597          left.set(right_n, INVALID);
598        }
599      } else {
600        if ( g->valid(left_n) ) {
601          right.set(left_n, INVALID);
602        } else {
603          level_list[lev]=INVALID;   
604        }
605      }
606      //unlacing ends
607               
608      if ( !g->valid(level_list[lev]) ) {
609             
610        //gapping starts
611        for (int i=lev; i!=k ; ) {
612          Node v=level_list[++i];
613          while ( g->valid(v) ) {
614            level.set(v,n);
615            v=right[v];
616          }
617          level_list[i]=INVALID;
618          if ( !what_heur ) {
619            while ( !active[i].empty() ) {
620              active[i].pop();    //FIXME: ezt szebben kene
621            }
622          }         
623        }
624       
625        level.set(w,n);
626        b=lev-1;
627        k=b;
628        //gapping ends
629       
630      } else {
631       
632        if ( newlevel == n ) level.set(w,n);
633        else {
634          level.set(w,++newlevel);
635          active[newlevel].push(w);
636          if ( what_heur ) b=newlevel;
637          if ( k < newlevel ) ++k;      //now k=newlevel
638          Node first=level_list[newlevel];
639          if ( g->valid(first) ) left.set(first,w);
640          right.set(w,first);
641          left.set(w,INVALID);
642          level_list[newlevel]=w;
643        }
644      }
645     
646    } //relabel
647   
648
649  };
650
651} //namespace hugo
652
653#endif //HUGO_PREFLOW_H
654
655
656
657
Note: See TracBrowser for help on using the repository browser.