COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/work/jacint/preflow.h @ 469:5f6ea657b75d

Last change on this file since 469:5f6ea657b75d was 469:5f6ea657b75d, checked in by marci, 20 years ago

const-ok

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
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//      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    Num 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    void resetTarget(Node _t) {t=_t;}
369    void resetSource(Node _s) {s=_s;}
370   
371    void resetCap(const CapMap& _cap) {
372      capacity=&_cap;
373    }
374   
375    void resetFlow(FlowMap& _flow) {
376      flow=&_flow;
377    }
378
379
380  private:
381
382    int push(Node w, VecStack& active) {
383     
384      int lev=level[w];
385      Num exc=excess[w];
386      int newlevel=n;       //bound on the next level of w
387         
388      OutEdgeIt e;
389      for(g->first(e,w); g->valid(e); g->next(e)) {
390           
391        if ( (*flow)[e] == (*capacity)[e] ) continue;
392        Node v=g->head(e);           
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         
401          Num cap=(*capacity)[e];
402          Num flo=(*flow)[e];
403          Num remcap=cap-flo;
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;
422        for(g->first(e,w); g->valid(e); g->next(e)) {
423         
424          if( (*flow)[e] == 0 ) continue;
425          Node v=g->tail(e);
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           
434            Num flo=(*flow)[e];
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;
478            for(g->first(e,v); g->valid(e); g->next(e)) {
479              Node w=g->tail(e);
480              if ( level[w] == n && w != s ) {
481                bfs_queue.push(w);
482                Node first=level_list[l];
483                if ( g->valid(first) ) left.set(first,w);
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;
493          for(g->first(e,s); g->valid(e); g->next(e))
494            {
495              Num c=(*capacity)[e];
496              if ( c == 0 ) continue;
497              Node w=g->head(e);
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;
522            for(g->first(e,v); g->valid(e); g->next(e)) {
523              if ( (*capacity)[e] == (*flow)[e] ) continue;
524              Node w=g->tail(e);
525              if ( level[w] == n && w != s ) {
526                bfs_queue.push(w);
527                Node first=level_list[l];
528                if ( g->valid(first) ) left.set(first,w);
529                right.set(w,first);
530                level_list[l]=w;
531                level.set(w, l);
532              }
533            }
534           
535            OutEdgeIt f;
536            for(g->first(f,v); g->valid(f); g->next(f)) {
537              if ( 0 == (*flow)[f] ) continue;
538              Node w=g->head(f);
539              if ( level[w] == n && w != s ) {
540                bfs_queue.push(w);
541                Node first=level_list[l];
542                if ( g->valid(first) ) left.set(first,w);
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;
553          for(g->first(e,s); g->valid(e); g->next(e))
554            {
555              Num rem=(*capacity)[e]-(*flow)[e];
556              if ( rem == 0 ) continue;
557              Node w=g->head(e);
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;
566          for(g->first(f,s); g->valid(f); g->next(f))
567            {
568              if ( (*flow)[f] == 0 ) continue;
569              Node w=g->tail(f);
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(Node w, int newlevel, VecStack& active, 
584                 VecNode& level_list, NNMap& left,
585                 NNMap& right, int& b, int& k, const bool what_heur ) {
586
587      Num lev=level[w];
588     
589      Node right_n=right[w];
590      Node left_n=left[w];
591     
592      //unlacing starts
593      if ( g->valid(right_n) ) {
594        if ( g->valid(left_n) ) {
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 {
602        if ( g->valid(left_n) ) {
603          right.set(left_n, INVALID);
604        } else {
605          level_list[lev]=INVALID;   
606        }
607      }
608      //unlacing ends
609               
610      if ( !g->valid(level_list[lev]) ) {
611             
612        //gapping starts
613        for (int i=lev; i!=k ; ) {
614          Node v=level_list[++i];
615          while ( g->valid(v) ) {
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];
641          if ( g->valid(first) ) left.set(first,w);
642          right.set(w,first);
643          left.set(w,INVALID);
644          level_list[newlevel]=w;
645        }
646      }
647     
648    } //relabel
649   
650
651  };
652
653} //namespace hugo
654
655#endif //HUGO_PREFLOW_H
656
657
658
659
Note: See TracBrowser for help on using the repository browser.