COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/hugo/preflow.h @ 906:17f31d280385

Last change on this file since 906:17f31d280385 was 906:17f31d280385, checked in by Alpar Juttner, 20 years ago

Copyright header added.

File size: 21.2 KB
Line 
1/* -*- C++ -*-
2 * src/hugo/preflow.h - Part of HUGOlib, a generic C++ optimization library
3 *
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
6 *
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
10 *
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
13 * purpose.
14 *
15 */
16
17#ifndef HUGO_PREFLOW_H
18#define HUGO_PREFLOW_H
19
20#include <vector>
21#include <queue>
22
23#include <hugo/invalid.h>
24#include <hugo/maps.h>
25
26/// \file
27/// \ingroup flowalgs
28/// Implementation of the preflow algorithm.
29
30namespace hugo {
31
32  /// \addtogroup flowalgs
33  /// @{                                                   
34
35  ///%Preflow algorithms class.
36
37  ///This class provides an implementation of the \e preflow \e
38  ///algorithm producing a flow of maximum value in a directed
39  ///graph. The preflow algorithms are the fastest max flow algorithms
40  ///up to now. The \e source node, the \e target node, the \e
41  ///capacity of the edges and the \e starting \e flow value of the
42  ///edges should be passed to the algorithm through the
43  ///constructor. It is possible to change these quantities using the
44  ///functions \ref setSource, \ref setTarget, \ref setCap and \ref
45  ///setFlow.
46  ///
47  ///After running \ref phase1() or \ref preflow(), the actual flow
48  ///value can be obtained by calling \ref flowValue(). The minimum
49  ///value cut can be written into a <tt>bool</tt> node map by
50  ///calling \ref minCut(). (\ref minMinCut() and \ref maxMinCut() writes
51  ///the inclusionwise minimum and maximum of the minimum value cuts,
52  ///resp.)
53  ///
54  ///\param Graph The directed graph type the algorithm runs on.
55  ///\param Num The number type of the capacities and the flow values.
56  ///\param CapMap The capacity map type.
57  ///\param FlowMap The flow map type.
58  ///
59  ///\author Jacint Szabo
60  template <typename Graph, typename Num,
61            typename CapMap=typename Graph::template EdgeMap<Num>,
62            typename FlowMap=typename Graph::template EdgeMap<Num> >
63  class Preflow {
64  protected:
65    typedef typename Graph::Node Node;
66    typedef typename Graph::NodeIt NodeIt;
67    typedef typename Graph::EdgeIt EdgeIt;
68    typedef typename Graph::OutEdgeIt OutEdgeIt;
69    typedef typename Graph::InEdgeIt InEdgeIt;
70
71    typedef typename Graph::template NodeMap<Node> NNMap;
72    typedef typename std::vector<Node> VecNode;
73
74    const Graph* g;
75    Node s;
76    Node t;
77    const CapMap* capacity;
78    FlowMap* flow;
79    int n;      //the number of nodes of G
80   
81    typename Graph::template NodeMap<int> level; 
82    typename Graph::template NodeMap<Num> excess;
83
84    // constants used for heuristics
85    static const int H0=20;
86    static const int H1=1;
87
88    public:
89
90    ///Indicates the property of the starting flow map.
91
92    ///Indicates the property of the starting flow map. The meanings are as follows:
93    ///- \c ZERO_FLOW: constant zero flow
94    ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
95    ///the sum of the out-flows in every node except the \e source and
96    ///the \e target.
97    ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at
98    ///least the sum of the out-flows in every node except the \e source.
99    ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be
100    ///set to the constant zero flow in the beginning of the algorithm in this case.
101    ///
102    enum FlowEnum{
103      NO_FLOW,
104      ZERO_FLOW,
105      GEN_FLOW,
106      PRE_FLOW
107    };
108
109    ///Indicates the state of the preflow algorithm.
110
111    ///Indicates the state of the preflow algorithm. The meanings are as follows:
112    ///- \c AFTER_NOTHING: before running the algorithm or at an unspecified state.
113    ///- \c AFTER_PREFLOW_PHASE_1: right after running \c phase1
114    ///- \c AFTER_PREFLOW_PHASE_2: after running \ref phase2()
115    ///
116    enum StatusEnum {
117      AFTER_NOTHING,
118      AFTER_PREFLOW_PHASE_1,     
119      AFTER_PREFLOW_PHASE_2
120    };
121   
122    protected:
123      FlowEnum flow_prop;
124    StatusEnum status; // Do not needle this flag only if necessary.
125   
126  public:
127    ///The constructor of the class.
128
129    ///The constructor of the class.
130    ///\param _G The directed graph the algorithm runs on.
131    ///\param _s The source node.
132    ///\param _t The target node.
133    ///\param _capacity The capacity of the edges.
134    ///\param _flow The flow of the edges.
135    ///Except the graph, all of these parameters can be reset by
136    ///calling \ref setSource, \ref setTarget, \ref setCap and \ref
137    ///setFlow, resp.
138      Preflow(const Graph& _G, Node _s, Node _t,
139              const CapMap& _capacity, FlowMap& _flow) :
140        g(&_G), s(_s), t(_t), capacity(&_capacity),
141        flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0),
142        flow_prop(NO_FLOW), status(AFTER_NOTHING) { }
143
144
145                                                                             
146    ///Runs the preflow algorithm. 
147
148    ///Runs the preflow algorithm.
149    ///
150    void run() {
151      phase1(flow_prop);
152      phase2();
153    }
154   
155    ///Runs the preflow algorithm. 
156   
157    ///Runs the preflow algorithm.
158    ///\pre The starting flow map must be
159    /// - a constant zero flow if \c fp is \c ZERO_FLOW,
160    /// - an arbitrary flow if \c fp is \c GEN_FLOW,
161    /// - an arbitrary preflow if \c fp is \c PRE_FLOW,
162    /// - any map if \c fp is NO_FLOW.
163    ///If the starting flow map is a flow or a preflow then
164    ///the algorithm terminates faster.
165    void run(FlowEnum fp) {
166      flow_prop=fp;
167      run();
168    }
169     
170    ///Runs the first phase of the preflow algorithm.
171
172    ///The preflow algorithm consists of two phases, this method runs the
173    ///first phase. After the first phase the maximum flow value and a
174    ///minimum value cut can already be computed, though a maximum flow
175    ///is not yet obtained. So after calling this method \ref flowValue
176    ///and \ref minCut gives proper results.
177    ///\warning \ref minMinCut and \ref maxMinCut do not
178    ///give minimum value cuts unless calling \ref phase2.
179    ///\pre The starting flow must be
180    /// - a constant zero flow if \c fp is \c ZERO_FLOW,
181    /// - an arbitary flow if \c fp is \c GEN_FLOW,
182    /// - an arbitary preflow if \c fp is \c PRE_FLOW,
183    /// - any map if \c fp is NO_FLOW.
184    void phase1(FlowEnum fp)
185    {
186      flow_prop=fp;
187      phase1();
188    }
189
190   
191    ///Runs the first phase of the preflow algorithm.
192
193    ///The preflow algorithm consists of two phases, this method runs the
194    ///first phase. After the first phase the maximum flow value and a
195    ///minimum value cut can already be computed, though a maximum flow
196    ///is not yet obtained. So after calling this method \ref flowValue
197    ///and \ref actMinCut gives proper results.
198    ///\warning \ref minCut, \ref minMinCut and \ref maxMinCut do not
199    ///give minimum value cuts unless calling \ref phase2.
200    void phase1()
201    {
202      int heur0=(int)(H0*n);  //time while running 'bound decrease'
203      int heur1=(int)(H1*n);  //time while running 'highest label'
204      int heur=heur1;         //starting time interval (#of relabels)
205      int numrelabel=0;
206
207      bool what_heur=1;
208      //It is 0 in case 'bound decrease' and 1 in case 'highest label'
209
210      bool end=false;
211      //Needed for 'bound decrease', true means no active
212      //nodes are above bound b.
213
214      int k=n-2;  //bound on the highest level under n containing a node
215      int b=k;    //bound on the highest level under n of an active node
216
217      VecNode first(n, INVALID);
218      NNMap next(*g, INVALID);
219
220      NNMap left(*g, INVALID);
221      NNMap right(*g, INVALID);
222      VecNode level_list(n,INVALID);
223      //List of the nodes in level i<n, set to n.
224
225      preflowPreproc(first, next, level_list, left, right);
226
227      //Push/relabel on the highest level active nodes.
228      while ( true ) {
229        if ( b == 0 ) {
230          if ( !what_heur && !end && k > 0 ) {
231            b=k;
232            end=true;
233          } else break;
234        }
235
236        if ( first[b]==INVALID ) --b;
237        else {
238          end=false;
239          Node w=first[b];
240          first[b]=next[w];
241          int newlevel=push(w, next, first);
242          if ( excess[w] > 0 ) relabel(w, newlevel, first, next, level_list,
243                                       left, right, b, k, what_heur);
244
245          ++numrelabel;
246          if ( numrelabel >= heur ) {
247            numrelabel=0;
248            if ( what_heur ) {
249              what_heur=0;
250              heur=heur0;
251              end=false;
252            } else {
253              what_heur=1;
254              heur=heur1;
255              b=k;
256            }
257          }
258        }
259      }
260      flow_prop=PRE_FLOW;
261      status=AFTER_PREFLOW_PHASE_1;
262    }
263    // Heuristics:
264    //   2 phase
265    //   gap
266    //   list 'level_list' on the nodes on level i implemented by hand
267    //   stack 'active' on the active nodes on level i     
268    //   runs heuristic 'highest label' for H1*n relabels
269    //   runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
270    //   Parameters H0 and H1 are initialized to 20 and 1.
271
272
273    ///Runs the second phase of the preflow algorithm.
274
275    ///The preflow algorithm consists of two phases, this method runs
276    ///the second phase. After calling \ref phase1 and then
277    ///\ref phase2 the methods \ref flowValue, \ref minCut,
278    ///\ref minMinCut and \ref maxMinCut give proper results.
279    ///\pre \ref phase1 must be called before.
280    void phase2()
281    {
282
283      int k=n-2;  //bound on the highest level under n containing a node
284      int b=k;    //bound on the highest level under n of an active node
285
286   
287      VecNode first(n, INVALID);
288      NNMap next(*g, INVALID);
289      level.set(s,0);
290      std::queue<Node> bfs_queue;
291      bfs_queue.push(s);
292
293      while ( !bfs_queue.empty() ) {
294
295        Node v=bfs_queue.front();
296        bfs_queue.pop();
297        int l=level[v]+1;
298
299        for(InEdgeIt e(*g,v); e!=INVALID; ++e) {
300          if ( (*capacity)[e] <= (*flow)[e] ) continue;
301          Node u=g->tail(e);
302          if ( level[u] >= n ) {
303            bfs_queue.push(u);
304            level.set(u, l);
305            if ( excess[u] > 0 ) {
306              next.set(u,first[l]);
307              first[l]=u;
308            }
309          }
310        }
311
312        for(OutEdgeIt e(*g,v); e!=INVALID; ++e) {
313          if ( 0 >= (*flow)[e] ) continue;
314          Node u=g->head(e);
315          if ( level[u] >= n ) {
316            bfs_queue.push(u);
317            level.set(u, l);
318            if ( excess[u] > 0 ) {
319              next.set(u,first[l]);
320              first[l]=u;
321            }
322          }
323        }
324      }
325      b=n-2;
326
327      while ( true ) {
328
329        if ( b == 0 ) break;
330        if ( first[b]==INVALID ) --b;
331        else {
332          Node w=first[b];
333          first[b]=next[w];
334          int newlevel=push(w,next, first);
335         
336          //relabel
337          if ( excess[w] > 0 ) {
338            level.set(w,++newlevel);
339            next.set(w,first[newlevel]);
340            first[newlevel]=w;
341            b=newlevel;
342          }
343        }
344      } // while(true)
345      flow_prop=GEN_FLOW;
346      status=AFTER_PREFLOW_PHASE_2;
347    }
348
349    /// Returns the value of the maximum flow.
350
351    /// Returns the value of the maximum flow by returning the excess
352    /// of the target node \ref t. This value equals to the value of
353    /// the maximum flow already after running \ref phase1.
354    Num flowValue() const {
355      return excess[t];
356    }
357
358
359    ///Returns a minimum value cut.
360
361    ///Sets \c M to the characteristic vector of a minimum value
362    ///cut. This method can be called both after running \ref
363    ///phase1 and \ref phase2. It is much faster after
364    ///\ref phase1.  \pre M should be a bool-valued node-map. \pre
365    ///If \ref mincut is called after \ref phase2 then M should
366    ///be initialized to false.
367    template<typename _CutMap>
368    void minCut(_CutMap& M) const {
369      switch ( status ) {
370        case AFTER_PREFLOW_PHASE_1:
371        for(NodeIt v(*g); v!=INVALID; ++v) {
372          if (level[v] < n) {
373            M.set(v, false);
374          } else {
375            M.set(v, true);
376          }
377        }
378        break;
379        case AFTER_PREFLOW_PHASE_2:
380        minMinCut(M);
381        break;
382        case AFTER_NOTHING:
383        break;
384      }
385    }
386
387    ///Returns the inclusionwise minimum of the minimum value cuts.
388
389    ///Sets \c M to the characteristic vector of the minimum value cut
390    ///which is inclusionwise minimum. It is computed by processing a
391    ///bfs from the source node \c s in the residual graph.  \pre M
392    ///should be a node map of bools initialized to false.  \pre \ref
393    ///phase2 should already be run.
394    template<typename _CutMap>
395    void minMinCut(_CutMap& M) const {
396
397      std::queue<Node> queue;
398      M.set(s,true);
399      queue.push(s);
400     
401      while (!queue.empty()) {
402        Node w=queue.front();
403        queue.pop();
404       
405        for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
406          Node v=g->head(e);
407          if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
408            queue.push(v);
409            M.set(v, true);
410          }
411        }
412       
413        for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
414          Node v=g->tail(e);
415          if (!M[v] && (*flow)[e] > 0 ) {
416            queue.push(v);
417            M.set(v, true);
418          }
419        }
420      }
421    }
422   
423    ///Returns the inclusionwise maximum of the minimum value cuts.
424
425    ///Sets \c M to the characteristic vector of the minimum value cut
426    ///which is inclusionwise maximum. It is computed by processing a
427    ///backward bfs from the target node \c t in the residual graph.
428    ///\pre \ref phase2() or preflow() should already be run.
429    template<typename _CutMap>
430    void maxMinCut(_CutMap& M) const {
431
432      for(NodeIt v(*g) ; v!=INVALID; ++v) M.set(v, true);
433
434      std::queue<Node> queue;
435
436      M.set(t,false);
437      queue.push(t);
438
439      while (!queue.empty()) {
440        Node w=queue.front();
441        queue.pop();
442
443        for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
444          Node v=g->tail(e);
445          if (M[v] && (*flow)[e] < (*capacity)[e] ) {
446            queue.push(v);
447            M.set(v, false);
448          }
449        }
450
451        for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
452          Node v=g->head(e);
453          if (M[v] && (*flow)[e] > 0 ) {
454            queue.push(v);
455            M.set(v, false);
456          }
457        }
458      }
459    }
460
461    ///Sets the source node to \c _s.
462
463    ///Sets the source node to \c _s.
464    ///
465    void setSource(Node _s) {
466      s=_s;
467      if ( flow_prop != ZERO_FLOW ) flow_prop=NO_FLOW;
468      status=AFTER_NOTHING;
469    }
470
471    ///Sets the target node to \c _t.
472
473    ///Sets the target node to \c _t.
474    ///
475    void setTarget(Node _t) {
476      t=_t;
477      if ( flow_prop == GEN_FLOW ) flow_prop=PRE_FLOW;
478      status=AFTER_NOTHING;
479    }
480
481    /// Sets the edge map of the capacities to _cap.
482
483    /// Sets the edge map of the capacities to _cap.
484    ///
485    void setCap(const CapMap& _cap) {
486      capacity=&_cap;
487      status=AFTER_NOTHING;
488    }
489
490    /// Sets the edge map of the flows to _flow.
491
492    /// Sets the edge map of the flows to _flow.
493    ///
494    void setFlow(FlowMap& _flow) {
495      flow=&_flow;
496      flow_prop=NO_FLOW;
497      status=AFTER_NOTHING;
498    }
499
500
501  private:
502
503    int push(Node w, NNMap& next, VecNode& first) {
504
505      int lev=level[w];
506      Num exc=excess[w];
507      int newlevel=n;       //bound on the next level of w
508
509      for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
510        if ( (*flow)[e] >= (*capacity)[e] ) continue;
511        Node v=g->head(e);
512
513        if( lev > level[v] ) { //Push is allowed now
514         
515          if ( excess[v]<=0 && v!=t && v!=s ) {
516            next.set(v,first[level[v]]);
517            first[level[v]]=v;
518          }
519
520          Num cap=(*capacity)[e];
521          Num flo=(*flow)[e];
522          Num remcap=cap-flo;
523         
524          if ( remcap >= exc ) { //A nonsaturating push.
525           
526            flow->set(e, flo+exc);
527            excess.set(v, excess[v]+exc);
528            exc=0;
529            break;
530
531          } else { //A saturating push.
532            flow->set(e, cap);
533            excess.set(v, excess[v]+remcap);
534            exc-=remcap;
535          }
536        } else if ( newlevel > level[v] ) newlevel = level[v];
537      } //for out edges wv
538
539      if ( exc > 0 ) {
540        for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
541         
542          if( (*flow)[e] <= 0 ) continue;
543          Node v=g->tail(e);
544
545          if( lev > level[v] ) { //Push is allowed now
546
547            if ( excess[v]<=0 && v!=t && v!=s ) {
548              next.set(v,first[level[v]]);
549              first[level[v]]=v;
550            }
551
552            Num flo=(*flow)[e];
553
554            if ( flo >= exc ) { //A nonsaturating push.
555
556              flow->set(e, flo-exc);
557              excess.set(v, excess[v]+exc);
558              exc=0;
559              break;
560            } else {  //A saturating push.
561
562              excess.set(v, excess[v]+flo);
563              exc-=flo;
564              flow->set(e,0);
565            }
566          } else if ( newlevel > level[v] ) newlevel = level[v];
567        } //for in edges vw
568
569      } // if w still has excess after the out edge for cycle
570
571      excess.set(w, exc);
572     
573      return newlevel;
574    }
575   
576   
577   
578    void preflowPreproc(VecNode& first, NNMap& next,
579                        VecNode& level_list, NNMap& left, NNMap& right)
580    {
581      for(NodeIt v(*g); v!=INVALID; ++v) level.set(v,n);
582      std::queue<Node> bfs_queue;
583     
584      if ( flow_prop == GEN_FLOW || flow_prop == PRE_FLOW ) {
585        //Reverse_bfs from t in the residual graph,
586        //to find the starting level.
587        level.set(t,0);
588        bfs_queue.push(t);
589       
590        while ( !bfs_queue.empty() ) {
591         
592          Node v=bfs_queue.front();
593          bfs_queue.pop();
594          int l=level[v]+1;
595         
596          for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) {
597            if ( (*capacity)[e] <= (*flow)[e] ) continue;
598            Node w=g->tail(e);
599            if ( level[w] == n && w != s ) {
600              bfs_queue.push(w);
601              Node z=level_list[l];
602              if ( z!=INVALID ) left.set(z,w);
603              right.set(w,z);
604              level_list[l]=w;
605              level.set(w, l);
606            }
607          }
608         
609          for(OutEdgeIt e(*g,v) ; e!=INVALID; ++e) {
610            if ( 0 >= (*flow)[e] ) continue;
611            Node w=g->head(e);
612            if ( level[w] == n && w != s ) {
613              bfs_queue.push(w);
614              Node z=level_list[l];
615              if ( z!=INVALID ) left.set(z,w);
616              right.set(w,z);
617              level_list[l]=w;
618              level.set(w, l);
619            }
620          }
621        } //while
622      } //if
623
624
625      switch (flow_prop) {
626        case NO_FLOW: 
627        for(EdgeIt e(*g); e!=INVALID; ++e) flow->set(e,0);
628        case ZERO_FLOW:
629        for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0);
630       
631        //Reverse_bfs from t, to find the starting level.
632        level.set(t,0);
633        bfs_queue.push(t);
634       
635        while ( !bfs_queue.empty() ) {
636         
637          Node v=bfs_queue.front();
638          bfs_queue.pop();
639          int l=level[v]+1;
640         
641          for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) {
642            Node w=g->tail(e);
643            if ( level[w] == n && w != s ) {
644              bfs_queue.push(w);
645              Node z=level_list[l];
646              if ( z!=INVALID ) left.set(z,w);
647              right.set(w,z);
648              level_list[l]=w;
649              level.set(w, l);
650            }
651          }
652        }
653       
654        //the starting flow
655        for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) {
656          Num c=(*capacity)[e];
657          if ( c <= 0 ) continue;
658          Node w=g->head(e);
659          if ( level[w] < n ) {
660            if ( excess[w] <= 0 && w!=t ) { //putting into the stack
661              next.set(w,first[level[w]]);
662              first[level[w]]=w;
663            }
664            flow->set(e, c);
665            excess.set(w, excess[w]+c);
666          }
667        }
668        break;
669
670        case GEN_FLOW:
671        for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0);
672        {
673          Num exc=0;
674          for(InEdgeIt e(*g,t) ; e!=INVALID; ++e) exc+=(*flow)[e];
675          for(OutEdgeIt e(*g,t) ; e!=INVALID; ++e) exc-=(*flow)[e];
676          excess.set(t,exc);
677        }
678
679        //the starting flow
680        for(OutEdgeIt e(*g,s); e!=INVALID; ++e) {
681          Num rem=(*capacity)[e]-(*flow)[e];
682          if ( rem <= 0 ) continue;
683          Node w=g->head(e);
684          if ( level[w] < n ) {
685            if ( excess[w] <= 0 && w!=t ) { //putting into the stack
686              next.set(w,first[level[w]]);
687              first[level[w]]=w;
688            }   
689            flow->set(e, (*capacity)[e]);
690            excess.set(w, excess[w]+rem);
691          }
692        }
693       
694        for(InEdgeIt e(*g,s); e!=INVALID; ++e) {
695          if ( (*flow)[e] <= 0 ) continue;
696          Node w=g->tail(e);
697          if ( level[w] < n ) {
698            if ( excess[w] <= 0 && w!=t ) {
699              next.set(w,first[level[w]]);
700              first[level[w]]=w;
701            } 
702            excess.set(w, excess[w]+(*flow)[e]);
703            flow->set(e, 0);
704          }
705        }
706        break;
707
708        case PRE_FLOW: 
709        //the starting flow
710        for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) {
711          Num rem=(*capacity)[e]-(*flow)[e];
712          if ( rem <= 0 ) continue;
713          Node w=g->head(e);
714          if ( level[w] < n ) flow->set(e, (*capacity)[e]);
715        }
716       
717        for(InEdgeIt e(*g,s) ; e!=INVALID; ++e) {
718          if ( (*flow)[e] <= 0 ) continue;
719          Node w=g->tail(e);
720          if ( level[w] < n ) flow->set(e, 0);
721        }
722       
723        //computing the excess
724        for(NodeIt w(*g); w!=INVALID; ++w) {
725          Num exc=0;
726          for(InEdgeIt e(*g,w); e!=INVALID; ++e) exc+=(*flow)[e];
727          for(OutEdgeIt e(*g,w); e!=INVALID; ++e) exc-=(*flow)[e];
728          excess.set(w,exc);
729         
730          //putting the active nodes into the stack
731          int lev=level[w];
732            if ( exc > 0 && lev < n && Node(w) != t ) {
733              next.set(w,first[lev]);
734              first[lev]=w;
735            }
736        }
737        break;
738      } //switch
739    } //preflowPreproc
740
741
742    void relabel(Node w, int newlevel, VecNode& first, NNMap& next,
743                 VecNode& level_list, NNMap& left,
744                 NNMap& right, int& b, int& k, bool what_heur )
745    {
746
747      int lev=level[w];
748
749      Node right_n=right[w];
750      Node left_n=left[w];
751
752      //unlacing starts
753      if ( right_n!=INVALID ) {
754        if ( left_n!=INVALID ) {
755          right.set(left_n, right_n);
756          left.set(right_n, left_n);
757        } else {
758          level_list[lev]=right_n;
759          left.set(right_n, INVALID);
760        }
761      } else {
762        if ( left_n!=INVALID ) {
763          right.set(left_n, INVALID);
764        } else {
765          level_list[lev]=INVALID;
766        }
767      }
768      //unlacing ends
769
770      if ( level_list[lev]==INVALID ) {
771
772        //gapping starts
773        for (int i=lev; i!=k ; ) {
774          Node v=level_list[++i];
775          while ( v!=INVALID ) {
776            level.set(v,n);
777            v=right[v];
778          }
779          level_list[i]=INVALID;
780          if ( !what_heur ) first[i]=INVALID;
781        }
782
783        level.set(w,n);
784        b=lev-1;
785        k=b;
786        //gapping ends
787
788      } else {
789
790        if ( newlevel == n ) level.set(w,n);
791        else {
792          level.set(w,++newlevel);
793          next.set(w,first[newlevel]);
794          first[newlevel]=w;
795          if ( what_heur ) b=newlevel;
796          if ( k < newlevel ) ++k;      //now k=newlevel
797          Node z=level_list[newlevel];
798          if ( z!=INVALID ) left.set(z,w);
799          right.set(w,z);
800          left.set(w,INVALID);
801          level_list[newlevel]=w;
802        }
803      }
804    } //relabel
805
806  };
807} //namespace hugo
808
809#endif //HUGO_PREFLOW_H
810
811
812
813
Note: See TracBrowser for help on using the repository browser.