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