src/lemon/preflow.h
changeset 921 818510fa3d99
parent 920 2d6c8075d9d0
child 946 c94ef40a22ce
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/lemon/preflow.h	Wed Sep 29 15:30:04 2004 +0000
     1.3 @@ -0,0 +1,819 @@
     1.4 +/* -*- C++ -*-
     1.5 + * src/lemon/preflow.h - Part of LEMON, a generic C++ optimization library
     1.6 + *
     1.7 + * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     1.8 + * (Egervary Combinatorial Optimization Research Group, EGRES).
     1.9 + *
    1.10 + * Permission to use, modify and distribute this software is granted
    1.11 + * provided that this copyright notice appears in all copies. For
    1.12 + * precise terms see the accompanying LICENSE file.
    1.13 + *
    1.14 + * This software is provided "AS IS" with no warranty of any kind,
    1.15 + * express or implied, and with no claim as to its suitability for any
    1.16 + * purpose.
    1.17 + *
    1.18 + */
    1.19 +
    1.20 +#ifndef LEMON_PREFLOW_H
    1.21 +#define LEMON_PREFLOW_H
    1.22 +
    1.23 +#include <vector>
    1.24 +#include <queue>
    1.25 +
    1.26 +#include <lemon/invalid.h>
    1.27 +#include <lemon/maps.h>
    1.28 +
    1.29 +/// \file
    1.30 +/// \ingroup flowalgs
    1.31 +/// Implementation of the preflow algorithm.
    1.32 +
    1.33 +namespace lemon {
    1.34 +
    1.35 +  /// \addtogroup flowalgs
    1.36 +  /// @{                                                   
    1.37 +
    1.38 +  ///%Preflow algorithms class.
    1.39 +
    1.40 +  ///This class provides an implementation of the \e preflow \e
    1.41 +  ///algorithm producing a flow of maximum value in a directed
    1.42 +  ///graph. The preflow algorithms are the fastest max flow algorithms
    1.43 +  ///up to now. The \e source node, the \e target node, the \e
    1.44 +  ///capacity of the edges and the \e starting \e flow value of the
    1.45 +  ///edges should be passed to the algorithm through the
    1.46 +  ///constructor. It is possible to change these quantities using the
    1.47 +  ///functions \ref setSource, \ref setTarget, \ref setCap and \ref
    1.48 +  ///setFlow.
    1.49 +  ///
    1.50 +  ///After running \ref lemon::Preflow::phase1() "phase1()"
    1.51 +  ///or \ref lemon::Preflow::run() "run()", the maximal flow
    1.52 +  ///value can be obtained by calling \ref flowValue(). The minimum
    1.53 +  ///value cut can be written into a <tt>bool</tt> node map by
    1.54 +  ///calling \ref minCut(). (\ref minMinCut() and \ref maxMinCut() writes
    1.55 +  ///the inclusionwise minimum and maximum of the minimum value cuts,
    1.56 +  ///resp.)
    1.57 +  ///
    1.58 +  ///\param Graph The directed graph type the algorithm runs on.
    1.59 +  ///\param Num The number type of the capacities and the flow values.
    1.60 +  ///\param CapMap The capacity map type.
    1.61 +  ///\param FlowMap The flow map type.
    1.62 +  ///
    1.63 +  ///\author Jacint Szabo 
    1.64 +  template <typename Graph, typename Num,
    1.65 +	    typename CapMap=typename Graph::template EdgeMap<Num>,
    1.66 +            typename FlowMap=typename Graph::template EdgeMap<Num> >
    1.67 +  class Preflow {
    1.68 +  protected:
    1.69 +    typedef typename Graph::Node Node;
    1.70 +    typedef typename Graph::NodeIt NodeIt;
    1.71 +    typedef typename Graph::EdgeIt EdgeIt;
    1.72 +    typedef typename Graph::OutEdgeIt OutEdgeIt;
    1.73 +    typedef typename Graph::InEdgeIt InEdgeIt;
    1.74 +
    1.75 +    typedef typename Graph::template NodeMap<Node> NNMap;
    1.76 +    typedef typename std::vector<Node> VecNode;
    1.77 +
    1.78 +    const Graph* g;
    1.79 +    Node s;
    1.80 +    Node t;
    1.81 +    const CapMap* capacity;
    1.82 +    FlowMap* flow;
    1.83 +    int n;      //the number of nodes of G
    1.84 +    
    1.85 +    typename Graph::template NodeMap<int> level;  
    1.86 +    typename Graph::template NodeMap<Num> excess;
    1.87 +
    1.88 +    // constants used for heuristics
    1.89 +    static const int H0=20;
    1.90 +    static const int H1=1;
    1.91 +
    1.92 +    public:
    1.93 +
    1.94 +    ///Indicates the property of the starting flow map.
    1.95 +
    1.96 +    ///Indicates the property of the starting flow map. The meanings are as follows:
    1.97 +    ///- \c ZERO_FLOW: constant zero flow
    1.98 +    ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
    1.99 +    ///the sum of the out-flows in every node except the \e source and
   1.100 +    ///the \e target.
   1.101 +    ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at 
   1.102 +    ///least the sum of the out-flows in every node except the \e source.
   1.103 +    ///- \c NO_FLOW: indicates an unspecified edge map. \c flow will be 
   1.104 +    ///set to the constant zero flow in the beginning of
   1.105 +    ///the algorithm in this case.
   1.106 +    ///
   1.107 +    enum FlowEnum{
   1.108 +      NO_FLOW,
   1.109 +      ZERO_FLOW,
   1.110 +      GEN_FLOW,
   1.111 +      PRE_FLOW
   1.112 +    };
   1.113 +
   1.114 +    ///Indicates the state of the preflow algorithm.
   1.115 +
   1.116 +    ///Indicates the state of the preflow algorithm. The meanings are as follows:
   1.117 +    ///- \c AFTER_NOTHING: before running the algorithm or at an unspecified state.
   1.118 +    ///- \c AFTER_PREFLOW_PHASE_1: right after running \c phase1
   1.119 +    ///- \c AFTER_PREFLOW_PHASE_2: after running \ref phase2()
   1.120 +    ///
   1.121 +    enum StatusEnum {
   1.122 +      AFTER_NOTHING,
   1.123 +      AFTER_PREFLOW_PHASE_1,      
   1.124 +      AFTER_PREFLOW_PHASE_2
   1.125 +    };
   1.126 +    
   1.127 +    protected: 
   1.128 +      FlowEnum flow_prop;
   1.129 +    StatusEnum status; // Do not needle this flag only if necessary.
   1.130 +    
   1.131 +  public: 
   1.132 +    ///The constructor of the class.
   1.133 +
   1.134 +    ///The constructor of the class. 
   1.135 +    ///\param _G The directed graph the algorithm runs on. 
   1.136 +    ///\param _s The source node.
   1.137 +    ///\param _t The target node.
   1.138 +    ///\param _capacity The capacity of the edges. 
   1.139 +    ///\param _flow The flow of the edges. 
   1.140 +    ///Except the graph, all of these parameters can be reset by
   1.141 +    ///calling \ref setSource, \ref setTarget, \ref setCap and \ref
   1.142 +    ///setFlow, resp.
   1.143 +      Preflow(const Graph& _G, Node _s, Node _t, 
   1.144 +	      const CapMap& _capacity, FlowMap& _flow) :
   1.145 +	g(&_G), s(_s), t(_t), capacity(&_capacity),
   1.146 +	flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0), 
   1.147 +	flow_prop(NO_FLOW), status(AFTER_NOTHING) { }
   1.148 +
   1.149 +
   1.150 +                                                                              
   1.151 +    ///Runs the preflow algorithm.  
   1.152 +
   1.153 +    ///Runs the preflow algorithm.
   1.154 +    ///
   1.155 +    void run() {
   1.156 +      phase1(flow_prop);
   1.157 +      phase2();
   1.158 +    }
   1.159 +    
   1.160 +    ///Runs the preflow algorithm.  
   1.161 +    
   1.162 +    ///Runs the preflow algorithm. 
   1.163 +    ///\pre The starting flow map must be
   1.164 +    /// - a constant zero flow if \c fp is \c ZERO_FLOW,
   1.165 +    /// - an arbitrary flow if \c fp is \c GEN_FLOW,
   1.166 +    /// - an arbitrary preflow if \c fp is \c PRE_FLOW,
   1.167 +    /// - any map if \c fp is NO_FLOW.
   1.168 +    ///If the starting flow map is a flow or a preflow then 
   1.169 +    ///the algorithm terminates faster.
   1.170 +    void run(FlowEnum fp) {
   1.171 +      flow_prop=fp;
   1.172 +      run();
   1.173 +    }
   1.174 +      
   1.175 +    ///Runs the first phase of the preflow algorithm.
   1.176 +
   1.177 +    ///The preflow algorithm consists of two phases, this method runs
   1.178 +    ///the first phase. After the first phase the maximum flow value
   1.179 +    ///and a minimum value cut can already be computed, though a
   1.180 +    ///maximum flow is not yet obtained. So after calling this method
   1.181 +    ///\ref flowValue returns the value of a maximum flow and \ref
   1.182 +    ///minCut returns a minimum cut.     
   1.183 +    ///\warning \ref minMinCut and \ref maxMinCut do not give minimum
   1.184 +    ///value cuts unless calling \ref phase2.  
   1.185 +    ///\pre The starting flow must be 
   1.186 +    ///- a constant zero flow if \c fp is \c ZERO_FLOW, 
   1.187 +    ///- an arbitary flow if \c fp is \c GEN_FLOW, 
   1.188 +    ///- an arbitary preflow if \c fp is \c PRE_FLOW, 
   1.189 +    ///- any map if \c fp is NO_FLOW.
   1.190 +    void phase1(FlowEnum fp)
   1.191 +    {
   1.192 +      flow_prop=fp;
   1.193 +      phase1();
   1.194 +    }
   1.195 +
   1.196 +    
   1.197 +    ///Runs the first phase of the preflow algorithm.
   1.198 +
   1.199 +    ///The preflow algorithm consists of two phases, this method runs
   1.200 +    ///the first phase. After the first phase the maximum flow value
   1.201 +    ///and a minimum value cut can already be computed, though a
   1.202 +    ///maximum flow is not yet obtained. So after calling this method
   1.203 +    ///\ref flowValue returns the value of a maximum flow and \ref
   1.204 +    ///minCut returns a minimum cut.
   1.205 +    ///\warning \ref minCut(), \ref minMinCut() and \ref maxMinCut() do not
   1.206 +    ///give minimum value cuts unless calling \ref phase2().
   1.207 +    void phase1()
   1.208 +    {
   1.209 +      int heur0=(int)(H0*n);  //time while running 'bound decrease'
   1.210 +      int heur1=(int)(H1*n);  //time while running 'highest label'
   1.211 +      int heur=heur1;         //starting time interval (#of relabels)
   1.212 +      int numrelabel=0;
   1.213 +
   1.214 +      bool what_heur=1;
   1.215 +      //It is 0 in case 'bound decrease' and 1 in case 'highest label'
   1.216 +
   1.217 +      bool end=false;
   1.218 +      //Needed for 'bound decrease', true means no active 
   1.219 +      //nodes are above bound b.
   1.220 +
   1.221 +      int k=n-2;  //bound on the highest level under n containing a node
   1.222 +      int b=k;    //bound on the highest level under n of an active node
   1.223 +
   1.224 +      VecNode first(n, INVALID);
   1.225 +      NNMap next(*g, INVALID);
   1.226 +
   1.227 +      NNMap left(*g, INVALID);
   1.228 +      NNMap right(*g, INVALID);
   1.229 +      VecNode level_list(n,INVALID);
   1.230 +      //List of the nodes in level i<n, set to n.
   1.231 +
   1.232 +      preflowPreproc(first, next, level_list, left, right);
   1.233 +
   1.234 +      //Push/relabel on the highest level active nodes.
   1.235 +      while ( true ) {
   1.236 +	if ( b == 0 ) {
   1.237 +	  if ( !what_heur && !end && k > 0 ) {
   1.238 +	    b=k;
   1.239 +	    end=true;
   1.240 +	  } else break;
   1.241 +	}
   1.242 +
   1.243 +	if ( first[b]==INVALID ) --b;
   1.244 +	else {
   1.245 +	  end=false;
   1.246 +	  Node w=first[b];
   1.247 +	  first[b]=next[w];
   1.248 +	  int newlevel=push(w, next, first);
   1.249 +	  if ( excess[w] > 0 ) relabel(w, newlevel, first, next, level_list, 
   1.250 +				       left, right, b, k, what_heur);
   1.251 +
   1.252 +	  ++numrelabel;
   1.253 +	  if ( numrelabel >= heur ) {
   1.254 +	    numrelabel=0;
   1.255 +	    if ( what_heur ) {
   1.256 +	      what_heur=0;
   1.257 +	      heur=heur0;
   1.258 +	      end=false;
   1.259 +	    } else {
   1.260 +	      what_heur=1;
   1.261 +	      heur=heur1;
   1.262 +	      b=k;
   1.263 +	    }
   1.264 +	  }
   1.265 +	}
   1.266 +      }
   1.267 +      flow_prop=PRE_FLOW;
   1.268 +      status=AFTER_PREFLOW_PHASE_1;
   1.269 +    }
   1.270 +    // Heuristics:
   1.271 +    //   2 phase
   1.272 +    //   gap
   1.273 +    //   list 'level_list' on the nodes on level i implemented by hand
   1.274 +    //   stack 'active' on the active nodes on level i      
   1.275 +    //   runs heuristic 'highest label' for H1*n relabels
   1.276 +    //   runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
   1.277 +    //   Parameters H0 and H1 are initialized to 20 and 1.
   1.278 +
   1.279 +
   1.280 +    ///Runs the second phase of the preflow algorithm.
   1.281 +
   1.282 +    ///The preflow algorithm consists of two phases, this method runs
   1.283 +    ///the second phase. After calling \ref phase1 and then \ref
   1.284 +    ///phase2, \ref flow contains a maximum flow, \ref flowValue
   1.285 +    ///returns the value of a maximum flow, \ref minCut returns a
   1.286 +    ///minimum cut, while the methods \ref minMinCut and \ref
   1.287 +    ///maxMinCut return the inclusionwise minimum and maximum cuts of
   1.288 +    ///minimum value, resp.  \pre \ref phase1 must be called before.
   1.289 +    void phase2()
   1.290 +    {
   1.291 +
   1.292 +      int k=n-2;  //bound on the highest level under n containing a node
   1.293 +      int b=k;    //bound on the highest level under n of an active node
   1.294 +
   1.295 +    
   1.296 +      VecNode first(n, INVALID);
   1.297 +      NNMap next(*g, INVALID); 
   1.298 +      level.set(s,0);
   1.299 +      std::queue<Node> bfs_queue;
   1.300 +      bfs_queue.push(s);
   1.301 +
   1.302 +      while ( !bfs_queue.empty() ) {
   1.303 +
   1.304 +	Node v=bfs_queue.front();
   1.305 +	bfs_queue.pop();
   1.306 +	int l=level[v]+1;
   1.307 +
   1.308 +	for(InEdgeIt e(*g,v); e!=INVALID; ++e) {
   1.309 +	  if ( (*capacity)[e] <= (*flow)[e] ) continue;
   1.310 +	  Node u=g->tail(e);
   1.311 +	  if ( level[u] >= n ) {
   1.312 +	    bfs_queue.push(u);
   1.313 +	    level.set(u, l);
   1.314 +	    if ( excess[u] > 0 ) {
   1.315 +	      next.set(u,first[l]);
   1.316 +	      first[l]=u;
   1.317 +	    }
   1.318 +	  }
   1.319 +	}
   1.320 +
   1.321 +	for(OutEdgeIt e(*g,v); e!=INVALID; ++e) {
   1.322 +	  if ( 0 >= (*flow)[e] ) continue;
   1.323 +	  Node u=g->head(e);
   1.324 +	  if ( level[u] >= n ) {
   1.325 +	    bfs_queue.push(u);
   1.326 +	    level.set(u, l);
   1.327 +	    if ( excess[u] > 0 ) {
   1.328 +	      next.set(u,first[l]);
   1.329 +	      first[l]=u;
   1.330 +	    }
   1.331 +	  }
   1.332 +	}
   1.333 +      }
   1.334 +      b=n-2;
   1.335 +
   1.336 +      while ( true ) {
   1.337 +
   1.338 +	if ( b == 0 ) break;
   1.339 +	if ( first[b]==INVALID ) --b;
   1.340 +	else {
   1.341 +	  Node w=first[b];
   1.342 +	  first[b]=next[w];
   1.343 +	  int newlevel=push(w,next, first);
   1.344 +	  
   1.345 +	  //relabel
   1.346 +	  if ( excess[w] > 0 ) {
   1.347 +	    level.set(w,++newlevel);
   1.348 +	    next.set(w,first[newlevel]);
   1.349 +	    first[newlevel]=w;
   1.350 +	    b=newlevel;
   1.351 +	  }
   1.352 +	} 
   1.353 +      } // while(true)
   1.354 +      flow_prop=GEN_FLOW;
   1.355 +      status=AFTER_PREFLOW_PHASE_2;
   1.356 +    }
   1.357 +
   1.358 +    /// Returns the value of the maximum flow.
   1.359 +
   1.360 +    /// Returns the value of the maximum flow by returning the excess
   1.361 +    /// of the target node \c t. This value equals to the value of
   1.362 +    /// the maximum flow already after running \ref phase1.
   1.363 +    Num flowValue() const {
   1.364 +      return excess[t];
   1.365 +    }
   1.366 +
   1.367 +
   1.368 +    ///Returns a minimum value cut.
   1.369 +
   1.370 +    ///Sets \c M to the characteristic vector of a minimum value
   1.371 +    ///cut. This method can be called both after running \ref
   1.372 +    ///phase1 and \ref phase2. It is much faster after
   1.373 +    ///\ref phase1.  \pre M should be a bool-valued node-map. \pre
   1.374 +    ///If \ref minCut() is called after \ref phase2() then M should
   1.375 +    ///be initialized to false.
   1.376 +    template<typename _CutMap>
   1.377 +    void minCut(_CutMap& M) const {
   1.378 +      switch ( status ) {
   1.379 +	case AFTER_PREFLOW_PHASE_1:
   1.380 +	for(NodeIt v(*g); v!=INVALID; ++v) {
   1.381 +	  if (level[v] < n) {
   1.382 +	    M.set(v, false);
   1.383 +	  } else {
   1.384 +	    M.set(v, true);
   1.385 +	  }
   1.386 +	}
   1.387 +	break;
   1.388 +	case AFTER_PREFLOW_PHASE_2:
   1.389 +	minMinCut(M);
   1.390 +	break;
   1.391 +	case AFTER_NOTHING:
   1.392 +	break;
   1.393 +      }
   1.394 +    }
   1.395 +
   1.396 +    ///Returns the inclusionwise minimum of the minimum value cuts.
   1.397 +
   1.398 +    ///Sets \c M to the characteristic vector of the minimum value cut
   1.399 +    ///which is inclusionwise minimum. It is computed by processing a
   1.400 +    ///bfs from the source node \c s in the residual graph.  \pre M
   1.401 +    ///should be a node map of bools initialized to false.  \pre \ref
   1.402 +    ///phase2 should already be run.
   1.403 +    template<typename _CutMap>
   1.404 +    void minMinCut(_CutMap& M) const {
   1.405 +
   1.406 +      std::queue<Node> queue;
   1.407 +      M.set(s,true);
   1.408 +      queue.push(s);
   1.409 +      
   1.410 +      while (!queue.empty()) {
   1.411 +	Node w=queue.front();
   1.412 +	queue.pop();
   1.413 +	
   1.414 +	for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
   1.415 +	  Node v=g->head(e);
   1.416 +	  if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
   1.417 +	    queue.push(v);
   1.418 +	    M.set(v, true);
   1.419 +	  }
   1.420 +	}
   1.421 +	
   1.422 +	for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
   1.423 +	  Node v=g->tail(e);
   1.424 +	  if (!M[v] && (*flow)[e] > 0 ) {
   1.425 +	    queue.push(v);
   1.426 +	    M.set(v, true);
   1.427 +	  }
   1.428 +	}
   1.429 +      }
   1.430 +    }
   1.431 +    
   1.432 +    ///Returns the inclusionwise maximum of the minimum value cuts.
   1.433 +
   1.434 +    ///Sets \c M to the characteristic vector of the minimum value cut
   1.435 +    ///which is inclusionwise maximum. It is computed by processing a
   1.436 +    ///backward bfs from the target node \c t in the residual graph.
   1.437 +    ///\pre \ref phase2() or run() should already be run.
   1.438 +    template<typename _CutMap>
   1.439 +    void maxMinCut(_CutMap& M) const {
   1.440 +
   1.441 +      for(NodeIt v(*g) ; v!=INVALID; ++v) M.set(v, true);
   1.442 +
   1.443 +      std::queue<Node> queue;
   1.444 +
   1.445 +      M.set(t,false);
   1.446 +      queue.push(t);
   1.447 +
   1.448 +      while (!queue.empty()) {
   1.449 +        Node w=queue.front();
   1.450 +	queue.pop();
   1.451 +
   1.452 +	for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
   1.453 +	  Node v=g->tail(e);
   1.454 +	  if (M[v] && (*flow)[e] < (*capacity)[e] ) {
   1.455 +	    queue.push(v);
   1.456 +	    M.set(v, false);
   1.457 +	  }
   1.458 +	}
   1.459 +
   1.460 +	for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
   1.461 +	  Node v=g->head(e);
   1.462 +	  if (M[v] && (*flow)[e] > 0 ) {
   1.463 +	    queue.push(v);
   1.464 +	    M.set(v, false);
   1.465 +	  }
   1.466 +	}
   1.467 +      }
   1.468 +    }
   1.469 +
   1.470 +    ///Sets the source node to \c _s.
   1.471 +
   1.472 +    ///Sets the source node to \c _s.
   1.473 +    /// 
   1.474 +    void setSource(Node _s) { 
   1.475 +      s=_s; 
   1.476 +      if ( flow_prop != ZERO_FLOW ) flow_prop=NO_FLOW;
   1.477 +      status=AFTER_NOTHING; 
   1.478 +    }
   1.479 +
   1.480 +    ///Sets the target node to \c _t.
   1.481 +
   1.482 +    ///Sets the target node to \c _t.
   1.483 +    ///
   1.484 +    void setTarget(Node _t) { 
   1.485 +      t=_t; 
   1.486 +      if ( flow_prop == GEN_FLOW ) flow_prop=PRE_FLOW;
   1.487 +      status=AFTER_NOTHING; 
   1.488 +    }
   1.489 +
   1.490 +    /// Sets the edge map of the capacities to _cap.
   1.491 +
   1.492 +    /// Sets the edge map of the capacities to _cap.
   1.493 +    /// 
   1.494 +    void setCap(const CapMap& _cap) { 
   1.495 +      capacity=&_cap; 
   1.496 +      status=AFTER_NOTHING; 
   1.497 +    }
   1.498 +
   1.499 +    /// Sets the edge map of the flows to _flow.
   1.500 +
   1.501 +    /// Sets the edge map of the flows to _flow.
   1.502 +    /// 
   1.503 +    void setFlow(FlowMap& _flow) { 
   1.504 +      flow=&_flow; 
   1.505 +      flow_prop=NO_FLOW;
   1.506 +      status=AFTER_NOTHING; 
   1.507 +    }
   1.508 +
   1.509 +
   1.510 +  private:
   1.511 +
   1.512 +    int push(Node w, NNMap& next, VecNode& first) {
   1.513 +
   1.514 +      int lev=level[w];
   1.515 +      Num exc=excess[w];
   1.516 +      int newlevel=n;       //bound on the next level of w
   1.517 +
   1.518 +      for(OutEdgeIt e(*g,w) ; e!=INVALID; ++e) {
   1.519 +	if ( (*flow)[e] >= (*capacity)[e] ) continue;
   1.520 +	Node v=g->head(e);
   1.521 +
   1.522 +	if( lev > level[v] ) { //Push is allowed now
   1.523 +	  
   1.524 +	  if ( excess[v]<=0 && v!=t && v!=s ) {
   1.525 +	    next.set(v,first[level[v]]);
   1.526 +	    first[level[v]]=v;
   1.527 +	  }
   1.528 +
   1.529 +	  Num cap=(*capacity)[e];
   1.530 +	  Num flo=(*flow)[e];
   1.531 +	  Num remcap=cap-flo;
   1.532 +	  
   1.533 +	  if ( remcap >= exc ) { //A nonsaturating push.
   1.534 +	    
   1.535 +	    flow->set(e, flo+exc);
   1.536 +	    excess.set(v, excess[v]+exc);
   1.537 +	    exc=0;
   1.538 +	    break;
   1.539 +
   1.540 +	  } else { //A saturating push.
   1.541 +	    flow->set(e, cap);
   1.542 +	    excess.set(v, excess[v]+remcap);
   1.543 +	    exc-=remcap;
   1.544 +	  }
   1.545 +	} else if ( newlevel > level[v] ) newlevel = level[v];
   1.546 +      } //for out edges wv
   1.547 +
   1.548 +      if ( exc > 0 ) {
   1.549 +	for(InEdgeIt e(*g,w) ; e!=INVALID; ++e) {
   1.550 +	  
   1.551 +	  if( (*flow)[e] <= 0 ) continue;
   1.552 +	  Node v=g->tail(e);
   1.553 +
   1.554 +	  if( lev > level[v] ) { //Push is allowed now
   1.555 +
   1.556 +	    if ( excess[v]<=0 && v!=t && v!=s ) {
   1.557 +	      next.set(v,first[level[v]]);
   1.558 +	      first[level[v]]=v;
   1.559 +	    }
   1.560 +
   1.561 +	    Num flo=(*flow)[e];
   1.562 +
   1.563 +	    if ( flo >= exc ) { //A nonsaturating push.
   1.564 +
   1.565 +	      flow->set(e, flo-exc);
   1.566 +	      excess.set(v, excess[v]+exc);
   1.567 +	      exc=0;
   1.568 +	      break;
   1.569 +	    } else {  //A saturating push.
   1.570 +
   1.571 +	      excess.set(v, excess[v]+flo);
   1.572 +	      exc-=flo;
   1.573 +	      flow->set(e,0);
   1.574 +	    }
   1.575 +	  } else if ( newlevel > level[v] ) newlevel = level[v];
   1.576 +	} //for in edges vw
   1.577 +
   1.578 +      } // if w still has excess after the out edge for cycle
   1.579 +
   1.580 +      excess.set(w, exc);
   1.581 +      
   1.582 +      return newlevel;
   1.583 +    }
   1.584 +    
   1.585 +    
   1.586 +    
   1.587 +    void preflowPreproc(VecNode& first, NNMap& next, 
   1.588 +			VecNode& level_list, NNMap& left, NNMap& right)
   1.589 +    {
   1.590 +      for(NodeIt v(*g); v!=INVALID; ++v) level.set(v,n);
   1.591 +      std::queue<Node> bfs_queue;
   1.592 +      
   1.593 +      if ( flow_prop == GEN_FLOW || flow_prop == PRE_FLOW ) {
   1.594 +	//Reverse_bfs from t in the residual graph,
   1.595 +	//to find the starting level.
   1.596 +	level.set(t,0);
   1.597 +	bfs_queue.push(t);
   1.598 +	
   1.599 +	while ( !bfs_queue.empty() ) {
   1.600 +	  
   1.601 +	  Node v=bfs_queue.front();
   1.602 +	  bfs_queue.pop();
   1.603 +	  int l=level[v]+1;
   1.604 +	  
   1.605 +	  for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) {
   1.606 +	    if ( (*capacity)[e] <= (*flow)[e] ) continue;
   1.607 +	    Node w=g->tail(e);
   1.608 +	    if ( level[w] == n && w != s ) {
   1.609 +	      bfs_queue.push(w);
   1.610 +	      Node z=level_list[l];
   1.611 +	      if ( z!=INVALID ) left.set(z,w);
   1.612 +	      right.set(w,z);
   1.613 +	      level_list[l]=w;
   1.614 +	      level.set(w, l);
   1.615 +	    }
   1.616 +	  }
   1.617 +	  
   1.618 +	  for(OutEdgeIt e(*g,v) ; e!=INVALID; ++e) {
   1.619 +	    if ( 0 >= (*flow)[e] ) continue;
   1.620 +	    Node w=g->head(e);
   1.621 +	    if ( level[w] == n && w != s ) {
   1.622 +	      bfs_queue.push(w);
   1.623 +	      Node z=level_list[l];
   1.624 +	      if ( z!=INVALID ) left.set(z,w);
   1.625 +	      right.set(w,z);
   1.626 +	      level_list[l]=w;
   1.627 +	      level.set(w, l);
   1.628 +	    }
   1.629 +	  }
   1.630 +	} //while
   1.631 +      } //if
   1.632 +
   1.633 +
   1.634 +      switch (flow_prop) {
   1.635 +	case NO_FLOW:  
   1.636 +	for(EdgeIt e(*g); e!=INVALID; ++e) flow->set(e,0);
   1.637 +	case ZERO_FLOW:
   1.638 +	for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0);
   1.639 +	
   1.640 +	//Reverse_bfs from t, to find the starting level.
   1.641 +	level.set(t,0);
   1.642 +	bfs_queue.push(t);
   1.643 +	
   1.644 +	while ( !bfs_queue.empty() ) {
   1.645 +	  
   1.646 +	  Node v=bfs_queue.front();
   1.647 +	  bfs_queue.pop();
   1.648 +	  int l=level[v]+1;
   1.649 +	  
   1.650 +	  for(InEdgeIt e(*g,v) ; e!=INVALID; ++e) {
   1.651 +	    Node w=g->tail(e);
   1.652 +	    if ( level[w] == n && w != s ) {
   1.653 +	      bfs_queue.push(w);
   1.654 +	      Node z=level_list[l];
   1.655 +	      if ( z!=INVALID ) left.set(z,w);
   1.656 +	      right.set(w,z);
   1.657 +	      level_list[l]=w;
   1.658 +	      level.set(w, l);
   1.659 +	    }
   1.660 +	  }
   1.661 +	}
   1.662 +	
   1.663 +	//the starting flow
   1.664 +	for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) {
   1.665 +	  Num c=(*capacity)[e];
   1.666 +	  if ( c <= 0 ) continue;
   1.667 +	  Node w=g->head(e);
   1.668 +	  if ( level[w] < n ) {
   1.669 +	    if ( excess[w] <= 0 && w!=t ) { //putting into the stack
   1.670 +	      next.set(w,first[level[w]]);
   1.671 +	      first[level[w]]=w;
   1.672 +	    }
   1.673 +	    flow->set(e, c);
   1.674 +	    excess.set(w, excess[w]+c);
   1.675 +	  }
   1.676 +	}
   1.677 +	break;
   1.678 +
   1.679 +	case GEN_FLOW:
   1.680 +	for(NodeIt v(*g); v!=INVALID; ++v) excess.set(v,0);
   1.681 +	{
   1.682 +	  Num exc=0;
   1.683 +	  for(InEdgeIt e(*g,t) ; e!=INVALID; ++e) exc+=(*flow)[e];
   1.684 +	  for(OutEdgeIt e(*g,t) ; e!=INVALID; ++e) exc-=(*flow)[e];
   1.685 +	  excess.set(t,exc);
   1.686 +	}
   1.687 +
   1.688 +	//the starting flow
   1.689 +	for(OutEdgeIt e(*g,s); e!=INVALID; ++e)	{
   1.690 +	  Num rem=(*capacity)[e]-(*flow)[e];
   1.691 +	  if ( rem <= 0 ) continue;
   1.692 +	  Node w=g->head(e);
   1.693 +	  if ( level[w] < n ) {
   1.694 +	    if ( excess[w] <= 0 && w!=t ) { //putting into the stack
   1.695 +	      next.set(w,first[level[w]]);
   1.696 +	      first[level[w]]=w;
   1.697 +	    }   
   1.698 +	    flow->set(e, (*capacity)[e]);
   1.699 +	    excess.set(w, excess[w]+rem);
   1.700 +	  }
   1.701 +	}
   1.702 +	
   1.703 +	for(InEdgeIt e(*g,s); e!=INVALID; ++e) {
   1.704 +	  if ( (*flow)[e] <= 0 ) continue;
   1.705 +	  Node w=g->tail(e);
   1.706 +	  if ( level[w] < n ) {
   1.707 +	    if ( excess[w] <= 0 && w!=t ) {
   1.708 +	      next.set(w,first[level[w]]);
   1.709 +	      first[level[w]]=w;
   1.710 +	    }  
   1.711 +	    excess.set(w, excess[w]+(*flow)[e]);
   1.712 +	    flow->set(e, 0);
   1.713 +	  }
   1.714 +	}
   1.715 +	break;
   1.716 +
   1.717 +	case PRE_FLOW:	
   1.718 +	//the starting flow
   1.719 +	for(OutEdgeIt e(*g,s) ; e!=INVALID; ++e) {
   1.720 +	  Num rem=(*capacity)[e]-(*flow)[e];
   1.721 +	  if ( rem <= 0 ) continue;
   1.722 +	  Node w=g->head(e);
   1.723 +	  if ( level[w] < n ) flow->set(e, (*capacity)[e]);
   1.724 +	}
   1.725 +	
   1.726 +	for(InEdgeIt e(*g,s) ; e!=INVALID; ++e) {
   1.727 +	  if ( (*flow)[e] <= 0 ) continue;
   1.728 +	  Node w=g->tail(e);
   1.729 +	  if ( level[w] < n ) flow->set(e, 0);
   1.730 +	}
   1.731 +	
   1.732 +	//computing the excess
   1.733 +	for(NodeIt w(*g); w!=INVALID; ++w) {
   1.734 +	  Num exc=0;
   1.735 +	  for(InEdgeIt e(*g,w); e!=INVALID; ++e) exc+=(*flow)[e];
   1.736 +	  for(OutEdgeIt e(*g,w); e!=INVALID; ++e) exc-=(*flow)[e];
   1.737 +	  excess.set(w,exc);
   1.738 +	  
   1.739 +	  //putting the active nodes into the stack
   1.740 +	  int lev=level[w];
   1.741 +	    if ( exc > 0 && lev < n && Node(w) != t ) {
   1.742 +	      next.set(w,first[lev]);
   1.743 +	      first[lev]=w;
   1.744 +	    }
   1.745 +	}
   1.746 +	break;
   1.747 +      } //switch
   1.748 +    } //preflowPreproc
   1.749 +
   1.750 +
   1.751 +    void relabel(Node w, int newlevel, VecNode& first, NNMap& next, 
   1.752 +		 VecNode& level_list, NNMap& left,
   1.753 +		 NNMap& right, int& b, int& k, bool what_heur )
   1.754 +    {
   1.755 +
   1.756 +      int lev=level[w];
   1.757 +
   1.758 +      Node right_n=right[w];
   1.759 +      Node left_n=left[w];
   1.760 +
   1.761 +      //unlacing starts
   1.762 +      if ( right_n!=INVALID ) {
   1.763 +	if ( left_n!=INVALID ) {
   1.764 +	  right.set(left_n, right_n);
   1.765 +	  left.set(right_n, left_n);
   1.766 +	} else {
   1.767 +	  level_list[lev]=right_n;
   1.768 +	  left.set(right_n, INVALID);
   1.769 +	}
   1.770 +      } else {
   1.771 +	if ( left_n!=INVALID ) {
   1.772 +	  right.set(left_n, INVALID);
   1.773 +	} else {
   1.774 +	  level_list[lev]=INVALID;
   1.775 +	}
   1.776 +      }
   1.777 +      //unlacing ends
   1.778 +
   1.779 +      if ( level_list[lev]==INVALID ) {
   1.780 +
   1.781 +	//gapping starts
   1.782 +	for (int i=lev; i!=k ; ) {
   1.783 +	  Node v=level_list[++i];
   1.784 +	  while ( v!=INVALID ) {
   1.785 +	    level.set(v,n);
   1.786 +	    v=right[v];
   1.787 +	  }
   1.788 +	  level_list[i]=INVALID;
   1.789 +	  if ( !what_heur ) first[i]=INVALID;
   1.790 +	}
   1.791 +
   1.792 +	level.set(w,n);
   1.793 +	b=lev-1;
   1.794 +	k=b;
   1.795 +	//gapping ends
   1.796 +
   1.797 +      } else {
   1.798 +
   1.799 +	if ( newlevel == n ) level.set(w,n);
   1.800 +	else {
   1.801 +	  level.set(w,++newlevel);
   1.802 +	  next.set(w,first[newlevel]);
   1.803 +	  first[newlevel]=w;
   1.804 +	  if ( what_heur ) b=newlevel;
   1.805 +	  if ( k < newlevel ) ++k;      //now k=newlevel
   1.806 +	  Node z=level_list[newlevel];
   1.807 +	  if ( z!=INVALID ) left.set(z,w);
   1.808 +	  right.set(w,z);
   1.809 +	  left.set(w,INVALID);
   1.810 +	  level_list[newlevel]=w;
   1.811 +	}
   1.812 +      }
   1.813 +    } //relabel
   1.814 +
   1.815 +  }; 
   1.816 +} //namespace lemon
   1.817 +
   1.818 +#endif //LEMON_PREFLOW_H
   1.819 +
   1.820 +
   1.821 +
   1.822 +