src/work/jacint/max_flow.h
changeset 1365 c280de819a73
parent 1364 ee5959aa4410
child 1366 d00b85f8be45
     1.1 --- a/src/work/jacint/max_flow.h	Sun Apr 17 18:57:22 2005 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,1269 +0,0 @@
     1.4 -// -*- C++ -*-
     1.5 -#ifndef LEMON_MAX_FLOW_H
     1.6 -#define LEMON_MAX_FLOW_H
     1.7 -
     1.8 -#include <vector>
     1.9 -#include <queue>
    1.10 -#include <stack>
    1.11 -
    1.12 -#include <lemon/graph_wrapper.h>
    1.13 -#include <bfs_dfs.h>
    1.14 -#include <lemon/invalid.h>
    1.15 -#include <lemon/maps.h>
    1.16 -#include <lemon/for_each_macros.h>
    1.17 -
    1.18 -/// \file
    1.19 -/// \brief Maximum flow algorithms.
    1.20 -/// \ingroup galgs
    1.21 -
    1.22 -namespace lemon {
    1.23 -
    1.24 -  /// \addtogroup galgs
    1.25 -  /// @{                                                                                                                                        
    1.26 -  ///Maximum flow algorithms class.
    1.27 -
    1.28 -  ///This class provides various algorithms for finding a flow of
    1.29 -  ///maximum value in a directed graph. The \e source node, the \e
    1.30 -  ///target node, the \e capacity of the edges and the \e starting \e
    1.31 -  ///flow value of the edges should be passed to the algorithm through the
    1.32 -  ///constructor. It is possible to change these quantities using the
    1.33 -  ///functions \ref resetSource, \ref resetTarget, \ref resetCap and
    1.34 -  ///\ref resetFlow. Before any subsequent runs of any algorithm of
    1.35 -  ///the class \ref resetFlow should be called. 
    1.36 -
    1.37 -  ///After running an algorithm of the class, the actual flow value 
    1.38 -  ///can be obtained by calling \ref flowValue(). The minimum
    1.39 -  ///value cut can be written into a \c node map of \c bools by
    1.40 -  ///calling \ref minCut. (\ref minMinCut and \ref maxMinCut writes
    1.41 -  ///the inclusionwise minimum and maximum of the minimum value
    1.42 -  ///cuts, resp.)                                                                                                                               
    1.43 -  ///\param Graph The directed graph type the algorithm runs on.
    1.44 -  ///\param Num The number type of the capacities and the flow values.
    1.45 -  ///\param CapMap The capacity map type.
    1.46 -  ///\param FlowMap The flow map type.                                                                                                           
    1.47 -  ///\author Marton Makai, Jacint Szabo 
    1.48 -  template <typename Graph, typename Num,
    1.49 -	    typename CapMap=typename Graph::template EdgeMap<Num>,
    1.50 -            typename FlowMap=typename Graph::template EdgeMap<Num> >
    1.51 -  class MaxFlow {
    1.52 -  protected:
    1.53 -    typedef typename Graph::Node Node;
    1.54 -    typedef typename Graph::NodeIt NodeIt;
    1.55 -    typedef typename Graph::EdgeIt EdgeIt;
    1.56 -    typedef typename Graph::OutEdgeIt OutEdgeIt;
    1.57 -    typedef typename Graph::InEdgeIt InEdgeIt;
    1.58 -
    1.59 -    typedef typename std::vector<std::stack<Node> > VecStack;
    1.60 -    typedef typename Graph::template NodeMap<Node> NNMap;
    1.61 -    typedef typename std::vector<Node> VecNode;
    1.62 -
    1.63 -    const Graph* g;
    1.64 -    Node s;
    1.65 -    Node t;
    1.66 -    const CapMap* capacity;
    1.67 -    FlowMap* flow;
    1.68 -    int n;      //the number of nodes of G
    1.69 -    typedef ResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;   
    1.70 -    //typedef ExpResGraphWrapper<const Graph, Num, CapMap, FlowMap> ResGW;
    1.71 -    typedef typename ResGW::OutEdgeIt ResGWOutEdgeIt;
    1.72 -    typedef typename ResGW::Edge ResGWEdge;
    1.73 -    //typedef typename ResGW::template NodeMap<bool> ReachedMap;
    1.74 -    typedef typename Graph::template NodeMap<int> ReachedMap;
    1.75 -
    1.76 -
    1.77 -    //level works as a bool map in augmenting path algorithms and is
    1.78 -    //used by bfs for storing reached information.  In preflow, it
    1.79 -    //shows the levels of nodes.     
    1.80 -    ReachedMap level;
    1.81 -
    1.82 -    //excess is needed only in preflow
    1.83 -    typename Graph::template NodeMap<Num> excess;
    1.84 -
    1.85 -    //fixme    
    1.86 -//   protected:
    1.87 -    //     MaxFlow() { }
    1.88 -    //     void set(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
    1.89 -    // 	     FlowMap& _flow)
    1.90 -    //       {
    1.91 -    // 	g=&_G;
    1.92 -    // 	s=_s;
    1.93 -    // 	t=_t;
    1.94 -    // 	capacity=&_capacity;
    1.95 -    // 	flow=&_flow;
    1.96 -    // 	n=_G.nodeNum;
    1.97 -    // 	level.set (_G); //kellene vmi ilyesmi fv
    1.98 -    // 	excess(_G,0); //itt is
    1.99 -    //       }
   1.100 -
   1.101 -    // constants used for heuristics
   1.102 -    static const int H0=20;
   1.103 -    static const int H1=1;
   1.104 -
   1.105 -  public:
   1.106 -
   1.107 -    ///Indicates the property of the starting flow.
   1.108 -
   1.109 -    ///Indicates the property of the starting flow. The meanings are as follows:
   1.110 -    ///- \c ZERO_FLOW: constant zero flow
   1.111 -    ///- \c GEN_FLOW: any flow, i.e. the sum of the in-flows equals to
   1.112 -    ///the sum of the out-flows in every node except the \e source and
   1.113 -    ///the \e target.
   1.114 -    ///- \c PRE_FLOW: any preflow, i.e. the sum of the in-flows is at 
   1.115 -    ///least the sum of the out-flows in every node except the \e source.
   1.116 -    ///- \c NO_FLOW: indicates an unspecified edge map. \ref flow will be 
   1.117 -    ///set to the constant zero flow in the beginning of the algorithm in this case.
   1.118 -    enum FlowEnum{
   1.119 -      ZERO_FLOW,
   1.120 -      GEN_FLOW,
   1.121 -      PRE_FLOW,
   1.122 -      NO_FLOW
   1.123 -    };
   1.124 -
   1.125 -    enum StatusEnum {
   1.126 -      AFTER_NOTHING,
   1.127 -      AFTER_AUGMENTING,
   1.128 -      AFTER_FAST_AUGMENTING, 
   1.129 -      AFTER_PRE_FLOW_PHASE_1,      
   1.130 -      AFTER_PRE_FLOW_PHASE_2
   1.131 -    };
   1.132 -
   1.133 -    /// Don not needle this flag only if necessary.
   1.134 -    StatusEnum status;
   1.135 -    int number_of_augmentations;
   1.136 -
   1.137 -
   1.138 -    template<typename IntMap>
   1.139 -    class TrickyReachedMap {
   1.140 -    protected:
   1.141 -      IntMap* map;
   1.142 -      int* number_of_augmentations;
   1.143 -    public:
   1.144 -      TrickyReachedMap(IntMap& _map, int& _number_of_augmentations) : 
   1.145 -	map(&_map), number_of_augmentations(&_number_of_augmentations) { }
   1.146 -      void set(const Node& n, bool b) {
   1.147 -	if (b)
   1.148 -	  map->set(n, *number_of_augmentations);
   1.149 -	else 
   1.150 -	  map->set(n, *number_of_augmentations-1);
   1.151 -      }
   1.152 -      bool operator[](const Node& n) const { 
   1.153 -	return (*map)[n]==*number_of_augmentations; 
   1.154 -      }
   1.155 -    };
   1.156 -    
   1.157 -    ///Constructor
   1.158 -
   1.159 -    ///\todo Document, please.
   1.160 -    ///
   1.161 -    MaxFlow(const Graph& _G, Node _s, Node _t, const CapMap& _capacity,
   1.162 -	    FlowMap& _flow) :
   1.163 -      g(&_G), s(_s), t(_t), capacity(&_capacity),
   1.164 -      flow(&_flow), n(_G.nodeNum()), level(_G), excess(_G,0), 
   1.165 -      status(AFTER_NOTHING), number_of_augmentations(0) { }
   1.166 -
   1.167 -    ///Runs a maximum flow algorithm.
   1.168 -
   1.169 -    ///Runs a preflow algorithm, which is the fastest maximum flow
   1.170 -    ///algorithm up-to-date. The default for \c fe is ZERO_FLOW.
   1.171 -    ///\pre The starting flow must be
   1.172 -    /// - a constant zero flow if \c fe is \c ZERO_FLOW,
   1.173 -    /// - an arbitary flow if \c fe is \c GEN_FLOW,
   1.174 -    /// - an arbitary preflow if \c fe is \c PRE_FLOW,
   1.175 -    /// - any map if \c fe is NO_FLOW.
   1.176 -    void run(FlowEnum fe=ZERO_FLOW) {
   1.177 -      preflow(fe);
   1.178 -    }
   1.179 -
   1.180 -                                                                              
   1.181 -    ///Runs a preflow algorithm.  
   1.182 -
   1.183 -    ///Runs a preflow algorithm. The preflow algorithms provide the
   1.184 -    ///fastest way to compute a maximum flow in a directed graph.
   1.185 -    ///\pre The starting flow must be
   1.186 -    /// - a constant zero flow if \c fe is \c ZERO_FLOW,
   1.187 -    /// - an arbitary flow if \c fe is \c GEN_FLOW,
   1.188 -    /// - an arbitary preflow if \c fe is \c PRE_FLOW,
   1.189 -    /// - any map if \c fe is NO_FLOW.
   1.190 -    ///
   1.191 -    ///\todo NO_FLOW should be the default flow.
   1.192 -    void preflow(FlowEnum fe) {
   1.193 -      preflowPhase1(fe);
   1.194 -      preflowPhase2();
   1.195 -    }
   1.196 -    // Heuristics:
   1.197 -    //   2 phase
   1.198 -    //   gap
   1.199 -    //   list 'level_list' on the nodes on level i implemented by hand
   1.200 -    //   stack 'active' on the active nodes on level i                                                                                    
   1.201 -    //   runs heuristic 'highest label' for H1*n relabels
   1.202 -    //   runs heuristic 'bound decrease' for H0*n relabels, starts with 'highest label'
   1.203 -    //   Parameters H0 and H1 are initialized to 20 and 1.
   1.204 -
   1.205 -    ///Runs the first phase of the preflow algorithm.
   1.206 -
   1.207 -    ///The preflow algorithm consists of two phases, this method runs the
   1.208 -    ///first phase. After the first phase the maximum flow value and a
   1.209 -    ///minimum value cut can already be computed, though a maximum flow
   1.210 -    ///is net yet obtained. So after calling this method \ref flowValue
   1.211 -    ///and \ref actMinCut gives proper results.
   1.212 -    ///\warning: \ref minCut, \ref minMinCut and \ref maxMinCut do not
   1.213 -    ///give minimum value cuts unless calling \ref preflowPhase2.
   1.214 -    ///\pre The starting flow must be
   1.215 -    /// - a constant zero flow if \c fe is \c ZERO_FLOW,
   1.216 -    /// - an arbitary flow if \c fe is \c GEN_FLOW,
   1.217 -    /// - an arbitary preflow if \c fe is \c PRE_FLOW,
   1.218 -    /// - any map if \c fe is NO_FLOW.
   1.219 -    void preflowPhase1(FlowEnum fe);
   1.220 -
   1.221 -    ///Runs the second phase of the preflow algorithm.
   1.222 -
   1.223 -    ///The preflow algorithm consists of two phases, this method runs
   1.224 -    ///the second phase. After calling \ref preflowPhase1 and then
   1.225 -    ///\ref preflowPhase2 the methods \ref flowValue, \ref minCut,
   1.226 -    ///\ref minMinCut and \ref maxMinCut give proper results.
   1.227 -    ///\pre \ref preflowPhase1 must be called before.
   1.228 -    void preflowPhase2();
   1.229 -
   1.230 -    /// Starting from a flow, this method searches for an augmenting path
   1.231 -    /// according to the Edmonds-Karp algorithm
   1.232 -    /// and augments the flow on if any.
   1.233 -    /// The return value shows if the augmentation was succesful.
   1.234 -    bool augmentOnShortestPath();
   1.235 -    bool augmentOnShortestPath2();
   1.236 -
   1.237 -    /// Starting from a flow, this method searches for an augmenting blocking
   1.238 -    /// flow according to Dinits' algorithm and augments the flow on if any.
   1.239 -    /// The blocking flow is computed in a physically constructed
   1.240 -    /// residual graph of type \c Mutablegraph.
   1.241 -    /// The return value show sif the augmentation was succesful.
   1.242 -    template<typename MutableGraph> bool augmentOnBlockingFlow();
   1.243 -
   1.244 -    /// The same as \c augmentOnBlockingFlow<MutableGraph> but the
   1.245 -    /// residual graph is not constructed physically.
   1.246 -    /// The return value shows if the augmentation was succesful.
   1.247 -    bool augmentOnBlockingFlow2();
   1.248 -
   1.249 -    /// Returns the maximum value of a flow.
   1.250 -
   1.251 -    /// Returns the maximum value of a flow, by counting the 
   1.252 -    /// over-flow of the target node \ref t.
   1.253 -    /// It can be called already after running \ref preflowPhase1.
   1.254 -    Num flowValue() const {
   1.255 -      Num a=0;
   1.256 -      FOR_EACH_INC_LOC(InEdgeIt, e, *g, t) a+=(*flow)[e];
   1.257 -      FOR_EACH_INC_LOC(OutEdgeIt, e, *g, t) a-=(*flow)[e];
   1.258 -      return a;
   1.259 -      //marci figyu: excess[t] epp ezt adja preflow 1. fazisa utan   
   1.260 -    }
   1.261 -
   1.262 -    ///Returns a minimum value cut after calling \ref preflowPhase1.
   1.263 -
   1.264 -    ///After the first phase of the preflow algorithm the maximum flow
   1.265 -    ///value and a minimum value cut can already be computed. This
   1.266 -    ///method can be called after running \ref preflowPhase1 for
   1.267 -    ///obtaining a minimum value cut.
   1.268 -    /// \warning Gives proper result only right after calling \ref
   1.269 -    /// preflowPhase1.
   1.270 -    /// \todo We have to make some status variable which shows the
   1.271 -    /// actual state
   1.272 -    /// of the class. This enables us to determine which methods are valid
   1.273 -    /// for MinCut computation
   1.274 -    template<typename _CutMap>
   1.275 -    void actMinCut(_CutMap& M) const {
   1.276 -      NodeIt v;
   1.277 -      switch (status) {
   1.278 -      case AFTER_PRE_FLOW_PHASE_1:
   1.279 -	for(g->first(v); g->valid(v); g->next(v)) {
   1.280 -	  if (level[v] < n) {
   1.281 -	    M.set(v, false);
   1.282 -	  } else {
   1.283 -	    M.set(v, true);
   1.284 -	  }
   1.285 -	}
   1.286 -	break;
   1.287 -      case AFTER_PRE_FLOW_PHASE_2:
   1.288 -      case AFTER_NOTHING:
   1.289 -	minMinCut(M);
   1.290 -	break;
   1.291 -      case AFTER_AUGMENTING:
   1.292 -	for(g->first(v); g->valid(v); g->next(v)) {
   1.293 -	  if (level[v]) {
   1.294 -	    M.set(v, true);
   1.295 -	  } else {
   1.296 -	    M.set(v, false);
   1.297 -	  }
   1.298 -	}
   1.299 -	break;
   1.300 -      case AFTER_FAST_AUGMENTING:
   1.301 -	for(g->first(v); g->valid(v); g->next(v)) {
   1.302 -	  if (level[v]==number_of_augmentations) {
   1.303 -	    M.set(v, true);
   1.304 -	  } else {
   1.305 -	    M.set(v, false);
   1.306 -	  }
   1.307 -	}
   1.308 -	break;
   1.309 -      }
   1.310 -    }
   1.311 -
   1.312 -    ///Returns the inclusionwise minimum of the minimum value cuts.
   1.313 -
   1.314 -    ///Sets \c M to the characteristic vector of the minimum value cut
   1.315 -    ///which is inclusionwise minimum. It is computed by processing
   1.316 -    ///a bfs from the source node \c s in the residual graph.
   1.317 -    ///\pre M should be a node map of bools initialized to false.
   1.318 -    ///\pre \c flow must be a maximum flow.
   1.319 -    template<typename _CutMap>
   1.320 -    void minMinCut(_CutMap& M) const {
   1.321 -      std::queue<Node> queue;
   1.322 -
   1.323 -      M.set(s,true);
   1.324 -      queue.push(s);
   1.325 -
   1.326 -      while (!queue.empty()) {
   1.327 -        Node w=queue.front();
   1.328 -	queue.pop();
   1.329 -
   1.330 -	OutEdgeIt e;
   1.331 -	for(g->first(e,w) ; g->valid(e); g->next(e)) {
   1.332 -	  Node v=g->target(e);
   1.333 -	  if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
   1.334 -	    queue.push(v);
   1.335 -	    M.set(v, true);
   1.336 -	  }
   1.337 -	}
   1.338 -
   1.339 -	InEdgeIt f;
   1.340 -	for(g->first(f,w) ; g->valid(f); g->next(f)) {
   1.341 -	  Node v=g->source(f);
   1.342 -	  if (!M[v] && (*flow)[f] > 0 ) {
   1.343 -	    queue.push(v);
   1.344 -	    M.set(v, true);
   1.345 -	  }
   1.346 -	}
   1.347 -      }
   1.348 -    }
   1.349 -
   1.350 -    ///Returns the inclusionwise maximum of the minimum value cuts.
   1.351 -
   1.352 -    ///Sets \c M to the characteristic vector of the minimum value cut
   1.353 -    ///which is inclusionwise maximum. It is computed by processing a
   1.354 -    ///backward bfs from the target node \c t in the residual graph.
   1.355 -    ///\pre M should be a node map of bools initialized to false.
   1.356 -    ///\pre \c flow must be a maximum flow. 
   1.357 -    template<typename _CutMap>
   1.358 -    void maxMinCut(_CutMap& M) const {
   1.359 -
   1.360 -      NodeIt v;
   1.361 -      for(g->first(v) ; g->valid(v); g->next(v)) {
   1.362 -	M.set(v, true);
   1.363 -      }
   1.364 -
   1.365 -      std::queue<Node> queue;
   1.366 -
   1.367 -      M.set(t,false);
   1.368 -      queue.push(t);
   1.369 -
   1.370 -      while (!queue.empty()) {
   1.371 -        Node w=queue.front();
   1.372 -	queue.pop();
   1.373 -
   1.374 -	InEdgeIt e;
   1.375 -	for(g->first(e,w) ; g->valid(e); g->next(e)) {
   1.376 -	  Node v=g->source(e);
   1.377 -	  if (M[v] && (*flow)[e] < (*capacity)[e] ) {
   1.378 -	    queue.push(v);
   1.379 -	    M.set(v, false);
   1.380 -	  }
   1.381 -	}
   1.382 -
   1.383 -	OutEdgeIt f;
   1.384 -	for(g->first(f,w) ; g->valid(f); g->next(f)) {
   1.385 -	  Node v=g->target(f);
   1.386 -	  if (M[v] && (*flow)[f] > 0 ) {
   1.387 -	    queue.push(v);
   1.388 -	    M.set(v, false);
   1.389 -	  }
   1.390 -	}
   1.391 -      }
   1.392 -    }
   1.393 -
   1.394 -    ///Returns a minimum value cut.
   1.395 -
   1.396 -    ///Sets \c M to the characteristic vector of a minimum value cut.
   1.397 -    ///\pre M should be a node map of bools initialized to false.
   1.398 -    ///\pre \c flow must be a maximum flow.    
   1.399 -    template<typename CutMap>
   1.400 -    void minCut(CutMap& M) const { minMinCut(M); }
   1.401 -
   1.402 -    ///Resets the source node to \c _s.
   1.403 -
   1.404 -    ///Resets the source node to \c _s.
   1.405 -    /// 
   1.406 -    void resetSource(Node _s) { s=_s; status=AFTER_NOTHING; }
   1.407 -
   1.408 -    ///Resets the target node to \c _t.
   1.409 -
   1.410 -    ///Resets the target node to \c _t.
   1.411 -    ///
   1.412 -    void resetTarget(Node _t) { t=_t; status=AFTER_NOTHING; }
   1.413 -
   1.414 -    /// Resets the edge map of the capacities to _cap.
   1.415 -
   1.416 -    /// Resets the edge map of the capacities to _cap.
   1.417 -    /// 
   1.418 -    void resetCap(const CapMap& _cap) { capacity=&_cap; status=AFTER_NOTHING; }
   1.419 -
   1.420 -    /// Resets the edge map of the flows to _flow.
   1.421 -
   1.422 -    /// Resets the edge map of the flows to _flow.
   1.423 -    /// 
   1.424 -    void resetFlow(FlowMap& _flow) { flow=&_flow; status=AFTER_NOTHING; }
   1.425 -
   1.426 -
   1.427 -  private:
   1.428 -
   1.429 -    int push(Node w, VecStack& active) {
   1.430 -
   1.431 -      int lev=level[w];
   1.432 -      Num exc=excess[w];
   1.433 -      int newlevel=n;       //bound on the next level of w
   1.434 -
   1.435 -      OutEdgeIt e;
   1.436 -      for(g->first(e,w); g->valid(e); g->next(e)) {
   1.437 -
   1.438 -	if ( (*flow)[e] >= (*capacity)[e] ) continue;
   1.439 -	Node v=g->target(e);
   1.440 -
   1.441 -	if( lev > level[v] ) { //Push is allowed now
   1.442 -
   1.443 -	  if ( excess[v]<=0 && v!=t && v!=s ) {
   1.444 -	    int lev_v=level[v];
   1.445 -	    active[lev_v].push(v);
   1.446 -	  }
   1.447 -
   1.448 -	  Num cap=(*capacity)[e];
   1.449 -	  Num flo=(*flow)[e];
   1.450 -	  Num remcap=cap-flo;
   1.451 -
   1.452 -	  if ( remcap >= exc ) { //A nonsaturating push.
   1.453 -
   1.454 -	    flow->set(e, flo+exc);
   1.455 -	    excess.set(v, excess[v]+exc);
   1.456 -	    exc=0;
   1.457 -	    break;
   1.458 -
   1.459 -	  } else { //A saturating push.
   1.460 -	    flow->set(e, cap);
   1.461 -	    excess.set(v, excess[v]+remcap);
   1.462 -	    exc-=remcap;
   1.463 -	  }
   1.464 -	} else if ( newlevel > level[v] ) newlevel = level[v];
   1.465 -      } //for out edges wv
   1.466 -
   1.467 -      if ( exc > 0 ) {
   1.468 -	InEdgeIt e;
   1.469 -	for(g->first(e,w); g->valid(e); g->next(e)) {
   1.470 -
   1.471 -	  if( (*flow)[e] <= 0 ) continue;
   1.472 -	  Node v=g->source(e);
   1.473 -
   1.474 -	  if( lev > level[v] ) { //Push is allowed now
   1.475 -
   1.476 -	    if ( excess[v]<=0 && v!=t && v!=s ) {
   1.477 -	      int lev_v=level[v];
   1.478 -	      active[lev_v].push(v);
   1.479 -	    }
   1.480 -
   1.481 -	    Num flo=(*flow)[e];
   1.482 -
   1.483 -	    if ( flo >= exc ) { //A nonsaturating push.
   1.484 -
   1.485 -	      flow->set(e, flo-exc);
   1.486 -	      excess.set(v, excess[v]+exc);
   1.487 -	      exc=0;
   1.488 -	      break;
   1.489 -	    } else {  //A saturating push.
   1.490 -
   1.491 -	      excess.set(v, excess[v]+flo);
   1.492 -	      exc-=flo;
   1.493 -	      flow->set(e,0);
   1.494 -	    }
   1.495 -	  } else if ( newlevel > level[v] ) newlevel = level[v];
   1.496 -	} //for in edges vw
   1.497 -
   1.498 -      } // if w still has excess after the out edge for cycle
   1.499 -
   1.500 -      excess.set(w, exc);
   1.501 -
   1.502 -      return newlevel;
   1.503 -    }
   1.504 -
   1.505 -
   1.506 -    void preflowPreproc(FlowEnum fe, VecStack& active,
   1.507 -			VecNode& level_list, NNMap& left, NNMap& right)
   1.508 -    {
   1.509 -      std::queue<Node> bfs_queue;
   1.510 -
   1.511 -      switch (fe) {
   1.512 -      case NO_FLOW:   //flow is already set to const zero in this case
   1.513 -      case ZERO_FLOW:
   1.514 -	{
   1.515 -	  //Reverse_bfs from t, to find the starting level.
   1.516 -	  level.set(t,0);
   1.517 -	  bfs_queue.push(t);
   1.518 -
   1.519 -	  while (!bfs_queue.empty()) {
   1.520 -
   1.521 -	    Node v=bfs_queue.front();
   1.522 -	    bfs_queue.pop();
   1.523 -	    int l=level[v]+1;
   1.524 -
   1.525 -	    InEdgeIt e;
   1.526 -	    for(g->first(e,v); g->valid(e); g->next(e)) {
   1.527 -	      Node w=g->source(e);
   1.528 -	      if ( level[w] == n && w != s ) {
   1.529 -		bfs_queue.push(w);
   1.530 -		Node first=level_list[l];
   1.531 -		if ( g->valid(first) ) left.set(first,w);
   1.532 -		right.set(w,first);
   1.533 -		level_list[l]=w;
   1.534 -		level.set(w, l);
   1.535 -	      }
   1.536 -	    }
   1.537 -	  }
   1.538 -
   1.539 -	  //the starting flow
   1.540 -	  OutEdgeIt e;
   1.541 -	  for(g->first(e,s); g->valid(e); g->next(e))
   1.542 -	    {
   1.543 -	      Num c=(*capacity)[e];
   1.544 -	      if ( c <= 0 ) continue;
   1.545 -	      Node w=g->target(e);
   1.546 -	      if ( level[w] < n ) {
   1.547 -		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   1.548 -		flow->set(e, c);
   1.549 -		excess.set(w, excess[w]+c);
   1.550 -	      }
   1.551 -	    }
   1.552 -	  break;
   1.553 -	}
   1.554 -
   1.555 -      case GEN_FLOW:
   1.556 -      case PRE_FLOW:
   1.557 -	{
   1.558 -	  //Reverse_bfs from t in the residual graph,
   1.559 -	  //to find the starting level.
   1.560 -	  level.set(t,0);
   1.561 -	  bfs_queue.push(t);
   1.562 -
   1.563 -	  while (!bfs_queue.empty()) {
   1.564 -
   1.565 -	    Node v=bfs_queue.front();
   1.566 -	    bfs_queue.pop();
   1.567 -	    int l=level[v]+1;
   1.568 -
   1.569 -	    InEdgeIt e;
   1.570 -	    for(g->first(e,v); g->valid(e); g->next(e)) {
   1.571 -	      if ( (*capacity)[e] <= (*flow)[e] ) continue;
   1.572 -	      Node w=g->source(e);
   1.573 -	      if ( level[w] == n && w != s ) {
   1.574 -		bfs_queue.push(w);
   1.575 -		Node first=level_list[l];
   1.576 -		if ( g->valid(first) ) left.set(first,w);
   1.577 -		right.set(w,first);
   1.578 -		level_list[l]=w;
   1.579 -		level.set(w, l);
   1.580 -	      }
   1.581 -	    }
   1.582 -
   1.583 -	    OutEdgeIt f;
   1.584 -	    for(g->first(f,v); g->valid(f); g->next(f)) {
   1.585 -	      if ( 0 >= (*flow)[f] ) continue;
   1.586 -	      Node w=g->target(f);
   1.587 -	      if ( level[w] == n && w != s ) {
   1.588 -		bfs_queue.push(w);
   1.589 -		Node first=level_list[l];
   1.590 -		if ( g->valid(first) ) left.set(first,w);
   1.591 -		right.set(w,first);
   1.592 -		level_list[l]=w;
   1.593 -		level.set(w, l);
   1.594 -	      }
   1.595 -	    }
   1.596 -	  }
   1.597 -
   1.598 -
   1.599 -	  //the starting flow
   1.600 -	  OutEdgeIt e;
   1.601 -	  for(g->first(e,s); g->valid(e); g->next(e))
   1.602 -	    {
   1.603 -	      Num rem=(*capacity)[e]-(*flow)[e];
   1.604 -	      if ( rem <= 0 ) continue;
   1.605 -	      Node w=g->target(e);
   1.606 -	      if ( level[w] < n ) {
   1.607 -		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   1.608 -		flow->set(e, (*capacity)[e]);
   1.609 -		excess.set(w, excess[w]+rem);
   1.610 -	      }
   1.611 -	    }
   1.612 -
   1.613 -	  InEdgeIt f;
   1.614 -	  for(g->first(f,s); g->valid(f); g->next(f))
   1.615 -	    {
   1.616 -	      if ( (*flow)[f] <= 0 ) continue;
   1.617 -	      Node w=g->source(f);
   1.618 -	      if ( level[w] < n ) {
   1.619 -		if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   1.620 -		excess.set(w, excess[w]+(*flow)[f]);
   1.621 -		flow->set(f, 0);
   1.622 -	      }
   1.623 -	    }
   1.624 -	  break;
   1.625 -	} //case PRE_FLOW
   1.626 -      }
   1.627 -    } //preflowPreproc
   1.628 -
   1.629 -
   1.630 -
   1.631 -    void relabel(Node w, int newlevel, VecStack& active,
   1.632 -		 VecNode& level_list, NNMap& left,
   1.633 -		 NNMap& right, int& b, int& k, bool what_heur )
   1.634 -    {
   1.635 -
   1.636 -      Num lev=level[w];
   1.637 -
   1.638 -      Node right_n=right[w];
   1.639 -      Node left_n=left[w];
   1.640 -
   1.641 -      //unlacing starts
   1.642 -      if ( g->valid(right_n) ) {
   1.643 -	if ( g->valid(left_n) ) {
   1.644 -	  right.set(left_n, right_n);
   1.645 -	  left.set(right_n, left_n);
   1.646 -	} else {
   1.647 -	  level_list[lev]=right_n;
   1.648 -	  left.set(right_n, INVALID);
   1.649 -	}
   1.650 -      } else {
   1.651 -	if ( g->valid(left_n) ) {
   1.652 -	  right.set(left_n, INVALID);
   1.653 -	} else {
   1.654 -	  level_list[lev]=INVALID;
   1.655 -	}
   1.656 -      }
   1.657 -      //unlacing ends
   1.658 -
   1.659 -      if ( !g->valid(level_list[lev]) ) {
   1.660 -
   1.661 -	//gapping starts
   1.662 -	for (int i=lev; i!=k ; ) {
   1.663 -	  Node v=level_list[++i];
   1.664 -	  while ( g->valid(v) ) {
   1.665 -	    level.set(v,n);
   1.666 -	    v=right[v];
   1.667 -	  }
   1.668 -	  level_list[i]=INVALID;
   1.669 -	  if ( !what_heur ) {
   1.670 -	    while ( !active[i].empty() ) {
   1.671 -	      active[i].pop();    //FIXME: ezt szebben kene
   1.672 -	    }
   1.673 -	  }
   1.674 -	}
   1.675 -
   1.676 -	level.set(w,n);
   1.677 -	b=lev-1;
   1.678 -	k=b;
   1.679 -	//gapping ends
   1.680 -
   1.681 -      } else {
   1.682 -
   1.683 -	if ( newlevel == n ) level.set(w,n);
   1.684 -	else {
   1.685 -	  level.set(w,++newlevel);
   1.686 -	  active[newlevel].push(w);
   1.687 -	  if ( what_heur ) b=newlevel;
   1.688 -	  if ( k < newlevel ) ++k;      //now k=newlevel
   1.689 -	  Node first=level_list[newlevel];
   1.690 -	  if ( g->valid(first) ) left.set(first,w);
   1.691 -	  right.set(w,first);
   1.692 -	  left.set(w,INVALID);
   1.693 -	  level_list[newlevel]=w;
   1.694 -	}
   1.695 -      }
   1.696 -
   1.697 -    } //relabel
   1.698 -
   1.699 -
   1.700 -    template<typename MapGraphWrapper>
   1.701 -    class DistanceMap {
   1.702 -    protected:
   1.703 -      const MapGraphWrapper* g;
   1.704 -      typename MapGraphWrapper::template NodeMap<int> dist;
   1.705 -    public:
   1.706 -      DistanceMap(MapGraphWrapper& _g) : g(&_g), dist(*g, g->nodeNum()) { }
   1.707 -      void set(const typename MapGraphWrapper::Node& n, int a) {
   1.708 -	dist.set(n, a);
   1.709 -      }
   1.710 -      int operator[](const typename MapGraphWrapper::Node& n) const { 
   1.711 -	return dist[n]; 
   1.712 -      }
   1.713 -      //       int get(const typename MapGraphWrapper::Node& n) const {
   1.714 -      // 	return dist[n]; }
   1.715 -      //       bool get(const typename MapGraphWrapper::Edge& e) const {
   1.716 -      // 	return (dist.get(g->source(e))<dist.get(g->target(e))); }
   1.717 -      bool operator[](const typename MapGraphWrapper::Edge& e) const {
   1.718 -	return (dist[g->source(e)]<dist[g->target(e)]);
   1.719 -      }
   1.720 -    };
   1.721 -
   1.722 -  };
   1.723 -
   1.724 -
   1.725 -  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
   1.726 -  void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase1(FlowEnum fe)
   1.727 -  {
   1.728 -
   1.729 -    int heur0=(int)(H0*n);  //time while running 'bound decrease'
   1.730 -    int heur1=(int)(H1*n);  //time while running 'highest label'
   1.731 -    int heur=heur1;         //starting time interval (#of relabels)
   1.732 -    int numrelabel=0;
   1.733 -
   1.734 -    bool what_heur=1;
   1.735 -    //It is 0 in case 'bound decrease' and 1 in case 'highest label'
   1.736 -
   1.737 -    bool end=false;
   1.738 -    //Needed for 'bound decrease', true means no active nodes are above bound
   1.739 -    //b.
   1.740 -
   1.741 -    int k=n-2;  //bound on the highest level under n containing a node
   1.742 -    int b=k;    //bound on the highest level under n of an active node
   1.743 -
   1.744 -    VecStack active(n);
   1.745 -
   1.746 -    NNMap left(*g, INVALID);
   1.747 -    NNMap right(*g, INVALID);
   1.748 -    VecNode level_list(n,INVALID);
   1.749 -    //List of the nodes in level i<n, set to n.
   1.750 -
   1.751 -    NodeIt v;
   1.752 -    for(g->first(v); g->valid(v); g->next(v)) level.set(v,n);
   1.753 -    //setting each node to level n
   1.754 -
   1.755 -    if ( fe == NO_FLOW ) {
   1.756 -      EdgeIt e;
   1.757 -      for(g->first(e); g->valid(e); g->next(e)) flow->set(e,0);
   1.758 -    }
   1.759 -
   1.760 -    switch (fe) { //computing the excess
   1.761 -    case PRE_FLOW:
   1.762 -      {
   1.763 -	NodeIt v;
   1.764 -	for(g->first(v); g->valid(v); g->next(v)) {
   1.765 -	  Num exc=0;
   1.766 -
   1.767 -	  InEdgeIt e;
   1.768 -	  for(g->first(e,v); g->valid(e); g->next(e)) exc+=(*flow)[e];
   1.769 -	  OutEdgeIt f;
   1.770 -	  for(g->first(f,v); g->valid(f); g->next(f)) exc-=(*flow)[f];
   1.771 -
   1.772 -	  excess.set(v,exc);
   1.773 -
   1.774 -	  //putting the active nodes into the stack
   1.775 -	  int lev=level[v];
   1.776 -	  if ( exc > 0 && lev < n && v != t ) active[lev].push(v);
   1.777 -	}
   1.778 -	break;
   1.779 -      }
   1.780 -    case GEN_FLOW:
   1.781 -      {
   1.782 -	NodeIt v;
   1.783 -	for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
   1.784 -
   1.785 -	Num exc=0;
   1.786 -	InEdgeIt e;
   1.787 -	for(g->first(e,t); g->valid(e); g->next(e)) exc+=(*flow)[e];
   1.788 -	OutEdgeIt f;
   1.789 -	for(g->first(f,t); g->valid(f); g->next(f)) exc-=(*flow)[f];
   1.790 -	excess.set(t,exc);
   1.791 -	break;
   1.792 -      }
   1.793 -    case ZERO_FLOW:
   1.794 -    case NO_FLOW:
   1.795 -      {
   1.796 -	NodeIt v;
   1.797 -        for(g->first(v); g->valid(v); g->next(v)) excess.set(v,0);
   1.798 -	break;
   1.799 -      }
   1.800 -    }
   1.801 -
   1.802 -    preflowPreproc(fe, active, level_list, left, right);
   1.803 -    //End of preprocessing
   1.804 -
   1.805 -
   1.806 -    //Push/relabel on the highest level active nodes.
   1.807 -    while ( true ) {
   1.808 -      if ( b == 0 ) {
   1.809 -	if ( !what_heur && !end && k > 0 ) {
   1.810 -	  b=k;
   1.811 -	  end=true;
   1.812 -	} else break;
   1.813 -      }
   1.814 -
   1.815 -      if ( active[b].empty() ) --b;
   1.816 -      else {
   1.817 -	end=false;
   1.818 -	Node w=active[b].top();
   1.819 -	active[b].pop();
   1.820 -	int newlevel=push(w,active);
   1.821 -	if ( excess[w] > 0 ) relabel(w, newlevel, active, level_list,
   1.822 -				     left, right, b, k, what_heur);
   1.823 -
   1.824 -	++numrelabel;
   1.825 -	if ( numrelabel >= heur ) {
   1.826 -	  numrelabel=0;
   1.827 -	  if ( what_heur ) {
   1.828 -	    what_heur=0;
   1.829 -	    heur=heur0;
   1.830 -	    end=false;
   1.831 -	  } else {
   1.832 -	    what_heur=1;
   1.833 -	    heur=heur1;
   1.834 -	    b=k;
   1.835 -	  }
   1.836 -	}
   1.837 -      }
   1.838 -    }
   1.839 -
   1.840 -    status=AFTER_PRE_FLOW_PHASE_1;
   1.841 -  }
   1.842 -
   1.843 -
   1.844 -
   1.845 -  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
   1.846 -  void MaxFlow<Graph, Num, CapMap, FlowMap>::preflowPhase2()
   1.847 -  {
   1.848 -
   1.849 -    int k=n-2;  //bound on the highest level under n containing a node
   1.850 -    int b=k;    //bound on the highest level under n of an active node
   1.851 -
   1.852 -    VecStack active(n);
   1.853 -    level.set(s,0);
   1.854 -    std::queue<Node> bfs_queue;
   1.855 -    bfs_queue.push(s);
   1.856 -
   1.857 -    while (!bfs_queue.empty()) {
   1.858 -
   1.859 -      Node v=bfs_queue.front();
   1.860 -      bfs_queue.pop();
   1.861 -      int l=level[v]+1;
   1.862 -
   1.863 -      InEdgeIt e;
   1.864 -      for(g->first(e,v); g->valid(e); g->next(e)) {
   1.865 -	if ( (*capacity)[e] <= (*flow)[e] ) continue;
   1.866 -	Node u=g->source(e);
   1.867 -	if ( level[u] >= n ) {
   1.868 -	  bfs_queue.push(u);
   1.869 -	  level.set(u, l);
   1.870 -	  if ( excess[u] > 0 ) active[l].push(u);
   1.871 -	}
   1.872 -      }
   1.873 -
   1.874 -      OutEdgeIt f;
   1.875 -      for(g->first(f,v); g->valid(f); g->next(f)) {
   1.876 -	if ( 0 >= (*flow)[f] ) continue;
   1.877 -	Node u=g->target(f);
   1.878 -	if ( level[u] >= n ) {
   1.879 -	  bfs_queue.push(u);
   1.880 -	  level.set(u, l);
   1.881 -	  if ( excess[u] > 0 ) active[l].push(u);
   1.882 -	}
   1.883 -      }
   1.884 -    }
   1.885 -    b=n-2;
   1.886 -
   1.887 -    while ( true ) {
   1.888 -
   1.889 -      if ( b == 0 ) break;
   1.890 -
   1.891 -      if ( active[b].empty() ) --b;
   1.892 -      else {
   1.893 -	Node w=active[b].top();
   1.894 -	active[b].pop();
   1.895 -	int newlevel=push(w,active);
   1.896 -
   1.897 -	//relabel
   1.898 -	if ( excess[w] > 0 ) {
   1.899 -	  level.set(w,++newlevel);
   1.900 -	  active[newlevel].push(w);
   1.901 -	  b=newlevel;
   1.902 -	}
   1.903 -      }  // if stack[b] is nonempty
   1.904 -    } // while(true)
   1.905 -
   1.906 -    status=AFTER_PRE_FLOW_PHASE_2;
   1.907 -  }
   1.908 -
   1.909 -
   1.910 -
   1.911 -  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
   1.912 -  bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath()
   1.913 -  {
   1.914 -    ResGW res_graph(*g, *capacity, *flow);
   1.915 -    bool _augment=false;
   1.916 -
   1.917 -    //ReachedMap level(res_graph);
   1.918 -    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
   1.919 -    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
   1.920 -    bfs.pushAndSetReached(s);
   1.921 -
   1.922 -    typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
   1.923 -    pred.set(s, INVALID);
   1.924 -
   1.925 -    typename ResGW::template NodeMap<Num> free(res_graph);
   1.926 -
   1.927 -    //searching for augmenting path
   1.928 -    while ( !bfs.finished() ) {
   1.929 -      ResGWOutEdgeIt e=bfs;
   1.930 -      if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
   1.931 -	Node v=res_graph.source(e);
   1.932 -	Node w=res_graph.target(e);
   1.933 -	pred.set(w, e);
   1.934 -	if (res_graph.valid(pred[v])) {
   1.935 -	  free.set(w, std::min(free[v], res_graph.resCap(e)));
   1.936 -	} else {
   1.937 -	  free.set(w, res_graph.resCap(e));
   1.938 -	}
   1.939 -	if (res_graph.target(e)==t) { _augment=true; break; }
   1.940 -      }
   1.941 -
   1.942 -      ++bfs;
   1.943 -    } //end of searching augmenting path
   1.944 -
   1.945 -    if (_augment) {
   1.946 -      Node n=t;
   1.947 -      Num augment_value=free[t];
   1.948 -      while (res_graph.valid(pred[n])) {
   1.949 -	ResGWEdge e=pred[n];
   1.950 -	res_graph.augment(e, augment_value);
   1.951 -	n=res_graph.source(e);
   1.952 -      }
   1.953 -    }
   1.954 -
   1.955 -    status=AFTER_AUGMENTING;
   1.956 -    return _augment;
   1.957 -  }
   1.958 -
   1.959 -
   1.960 -  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
   1.961 -  bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnShortestPath2()
   1.962 -  {
   1.963 -    ResGW res_graph(*g, *capacity, *flow);
   1.964 -    bool _augment=false;
   1.965 -
   1.966 -    if (status!=AFTER_FAST_AUGMENTING) {
   1.967 -      FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0); 
   1.968 -      number_of_augmentations=1;
   1.969 -    } else {
   1.970 -      ++number_of_augmentations;
   1.971 -    }
   1.972 -    TrickyReachedMap<ReachedMap> 
   1.973 -      tricky_reached_map(level, number_of_augmentations);
   1.974 -    //ReachedMap level(res_graph);
   1.975 -//    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
   1.976 -    BfsIterator<ResGW, TrickyReachedMap<ReachedMap> > 
   1.977 -      bfs(res_graph, tricky_reached_map);
   1.978 -    bfs.pushAndSetReached(s);
   1.979 -
   1.980 -    typename ResGW::template NodeMap<ResGWEdge> pred(res_graph);
   1.981 -    pred.set(s, INVALID);
   1.982 -
   1.983 -    typename ResGW::template NodeMap<Num> free(res_graph);
   1.984 -
   1.985 -    //searching for augmenting path
   1.986 -    while ( !bfs.finished() ) {
   1.987 -      ResGWOutEdgeIt e=bfs;
   1.988 -      if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
   1.989 -	Node v=res_graph.source(e);
   1.990 -	Node w=res_graph.target(e);
   1.991 -	pred.set(w, e);
   1.992 -	if (res_graph.valid(pred[v])) {
   1.993 -	  free.set(w, std::min(free[v], res_graph.resCap(e)));
   1.994 -	} else {
   1.995 -	  free.set(w, res_graph.resCap(e));
   1.996 -	}
   1.997 -	if (res_graph.target(e)==t) { _augment=true; break; }
   1.998 -      }
   1.999 -
  1.1000 -      ++bfs;
  1.1001 -    } //end of searching augmenting path
  1.1002 -
  1.1003 -    if (_augment) {
  1.1004 -      Node n=t;
  1.1005 -      Num augment_value=free[t];
  1.1006 -      while (res_graph.valid(pred[n])) {
  1.1007 -	ResGWEdge e=pred[n];
  1.1008 -	res_graph.augment(e, augment_value);
  1.1009 -	n=res_graph.source(e);
  1.1010 -      }
  1.1011 -    }
  1.1012 -
  1.1013 -    status=AFTER_FAST_AUGMENTING;
  1.1014 -    return _augment;
  1.1015 -  }
  1.1016 -
  1.1017 -
  1.1018 -  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
  1.1019 -  template<typename MutableGraph>
  1.1020 -  bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow()
  1.1021 -  {
  1.1022 -    typedef MutableGraph MG;
  1.1023 -    bool _augment=false;
  1.1024 -
  1.1025 -    ResGW res_graph(*g, *capacity, *flow);
  1.1026 -
  1.1027 -    //bfs for distances on the residual graph
  1.1028 -    //ReachedMap level(res_graph);
  1.1029 -    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
  1.1030 -    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
  1.1031 -    bfs.pushAndSetReached(s);
  1.1032 -    typename ResGW::template NodeMap<int>
  1.1033 -      dist(res_graph); //filled up with 0's
  1.1034 -
  1.1035 -    //F will contain the physical copy of the residual graph
  1.1036 -    //with the set of edges which are on shortest paths
  1.1037 -    MG F;
  1.1038 -    typename ResGW::template NodeMap<typename MG::Node>
  1.1039 -      res_graph_to_F(res_graph);
  1.1040 -    {
  1.1041 -      typename ResGW::NodeIt n;
  1.1042 -      for(res_graph.first(n); res_graph.valid(n); res_graph.next(n)) {
  1.1043 -	res_graph_to_F.set(n, F.addNode());
  1.1044 -      }
  1.1045 -    }
  1.1046 -
  1.1047 -    typename MG::Node sF=res_graph_to_F[s];
  1.1048 -    typename MG::Node tF=res_graph_to_F[t];
  1.1049 -    typename MG::template EdgeMap<ResGWEdge> original_edge(F);
  1.1050 -    typename MG::template EdgeMap<Num> residual_capacity(F);
  1.1051 -
  1.1052 -    while ( !bfs.finished() ) {
  1.1053 -      ResGWOutEdgeIt e=bfs;
  1.1054 -      if (res_graph.valid(e)) {
  1.1055 -	if (bfs.isBNodeNewlyReached()) {
  1.1056 -	  dist.set(res_graph.target(e), dist[res_graph.source(e)]+1);
  1.1057 -	  typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.source(e)],
  1.1058 -					res_graph_to_F[res_graph.target(e)]);
  1.1059 -	  original_edge.update();
  1.1060 -	  original_edge.set(f, e);
  1.1061 -	  residual_capacity.update();
  1.1062 -	  residual_capacity.set(f, res_graph.resCap(e));
  1.1063 -	} else {
  1.1064 -	  if (dist[res_graph.target(e)]==(dist[res_graph.source(e)]+1)) {
  1.1065 -	    typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.source(e)],
  1.1066 -					  res_graph_to_F[res_graph.target(e)]);
  1.1067 -	    original_edge.update();
  1.1068 -	    original_edge.set(f, e);
  1.1069 -	    residual_capacity.update();
  1.1070 -	    residual_capacity.set(f, res_graph.resCap(e));
  1.1071 -	  }
  1.1072 -	}
  1.1073 -      }
  1.1074 -      ++bfs;
  1.1075 -    } //computing distances from s in the residual graph
  1.1076 -
  1.1077 -    bool __augment=true;
  1.1078 -
  1.1079 -    while (__augment) {
  1.1080 -      __augment=false;
  1.1081 -      //computing blocking flow with dfs
  1.1082 -      DfsIterator< MG, typename MG::template NodeMap<bool> > dfs(F);
  1.1083 -      typename MG::template NodeMap<typename MG::Edge> pred(F);
  1.1084 -      pred.set(sF, INVALID);
  1.1085 -      //invalid iterators for sources
  1.1086 -
  1.1087 -      typename MG::template NodeMap<Num> free(F);
  1.1088 -
  1.1089 -      dfs.pushAndSetReached(sF);
  1.1090 -      while (!dfs.finished()) {
  1.1091 -	++dfs;
  1.1092 -	if (F.valid(/*typename MG::OutEdgeIt*/(dfs))) {
  1.1093 -	  if (dfs.isBNodeNewlyReached()) {
  1.1094 -	    typename MG::Node v=F.aNode(dfs);
  1.1095 -	    typename MG::Node w=F.bNode(dfs);
  1.1096 -	    pred.set(w, dfs);
  1.1097 -	    if (F.valid(pred[v])) {
  1.1098 -	      free.set(w, std::min(free[v], residual_capacity[dfs]));
  1.1099 -	    } else {
  1.1100 -	      free.set(w, residual_capacity[dfs]);
  1.1101 -	    }
  1.1102 -	    if (w==tF) {
  1.1103 -	      __augment=true;
  1.1104 -	      _augment=true;
  1.1105 -	      break;
  1.1106 -	    }
  1.1107 -
  1.1108 -	  } else {
  1.1109 -	    F.erase(/*typename MG::OutEdgeIt*/(dfs));
  1.1110 -	  }
  1.1111 -	}
  1.1112 -      }
  1.1113 -
  1.1114 -      if (__augment) {
  1.1115 -	typename MG::Node n=tF;
  1.1116 -	Num augment_value=free[tF];
  1.1117 -	while (F.valid(pred[n])) {
  1.1118 -	  typename MG::Edge e=pred[n];
  1.1119 -	  res_graph.augment(original_edge[e], augment_value);
  1.1120 -	  n=F.source(e);
  1.1121 -	  if (residual_capacity[e]==augment_value)
  1.1122 -	    F.erase(e);
  1.1123 -	  else
  1.1124 -	    residual_capacity.set(e, residual_capacity[e]-augment_value);
  1.1125 -	}
  1.1126 -      }
  1.1127 -
  1.1128 -    }
  1.1129 -
  1.1130 -    status=AFTER_AUGMENTING;
  1.1131 -    return _augment;
  1.1132 -  }
  1.1133 -
  1.1134 -
  1.1135 -
  1.1136 -
  1.1137 -  template <typename Graph, typename Num, typename CapMap, typename FlowMap>
  1.1138 -  bool MaxFlow<Graph, Num, CapMap, FlowMap>::augmentOnBlockingFlow2()
  1.1139 -  {
  1.1140 -    bool _augment=false;
  1.1141 -
  1.1142 -    ResGW res_graph(*g, *capacity, *flow);
  1.1143 -
  1.1144 -    //ReachedMap level(res_graph);
  1.1145 -    FOR_EACH_LOC(typename Graph::NodeIt, e, *g) level.set(e, 0);
  1.1146 -    BfsIterator<ResGW, ReachedMap> bfs(res_graph, level);
  1.1147 -
  1.1148 -    bfs.pushAndSetReached(s);
  1.1149 -    DistanceMap<ResGW> dist(res_graph);
  1.1150 -    while ( !bfs.finished() ) {
  1.1151 -      ResGWOutEdgeIt e=bfs;
  1.1152 -      if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
  1.1153 -	dist.set(res_graph.target(e), dist[res_graph.source(e)]+1);
  1.1154 -      }
  1.1155 -      ++bfs;
  1.1156 -    } //computing distances from s in the residual graph
  1.1157 -
  1.1158 -      //Subgraph containing the edges on some shortest paths
  1.1159 -    ConstMap<typename ResGW::Node, bool> true_map(true);
  1.1160 -    typedef SubGraphWrapper<ResGW, ConstMap<typename ResGW::Node, bool>,
  1.1161 -      DistanceMap<ResGW> > FilterResGW;
  1.1162 -    FilterResGW filter_res_graph(res_graph, true_map, dist);
  1.1163 -
  1.1164 -    //Subgraph, which is able to delete edges which are already
  1.1165 -    //met by the dfs
  1.1166 -    typename FilterResGW::template NodeMap<typename FilterResGW::OutEdgeIt>
  1.1167 -      first_out_edges(filter_res_graph);
  1.1168 -    typename FilterResGW::NodeIt v;
  1.1169 -    for(filter_res_graph.first(v); filter_res_graph.valid(v);
  1.1170 -	filter_res_graph.next(v))
  1.1171 -      {
  1.1172 - 	typename FilterResGW::OutEdgeIt e;
  1.1173 - 	filter_res_graph.first(e, v);
  1.1174 - 	first_out_edges.set(v, e);
  1.1175 -      }
  1.1176 -    typedef ErasingFirstGraphWrapper<FilterResGW, typename FilterResGW::
  1.1177 -      template NodeMap<typename FilterResGW::OutEdgeIt> > ErasingResGW;
  1.1178 -    ErasingResGW erasing_res_graph(filter_res_graph, first_out_edges);
  1.1179 -
  1.1180 -    bool __augment=true;
  1.1181 -
  1.1182 -    while (__augment) {
  1.1183 -
  1.1184 -      __augment=false;
  1.1185 -      //computing blocking flow with dfs
  1.1186 -      DfsIterator< ErasingResGW,
  1.1187 -	typename ErasingResGW::template NodeMap<bool> >
  1.1188 -	dfs(erasing_res_graph);
  1.1189 -      typename ErasingResGW::
  1.1190 -	template NodeMap<typename ErasingResGW::OutEdgeIt>
  1.1191 -	pred(erasing_res_graph);
  1.1192 -      pred.set(s, INVALID);
  1.1193 -      //invalid iterators for sources
  1.1194 -
  1.1195 -      typename ErasingResGW::template NodeMap<Num>
  1.1196 -	free1(erasing_res_graph);
  1.1197 -
  1.1198 -      dfs.pushAndSetReached
  1.1199 -	///\bug lemon 0.2
  1.1200 -	(typename ErasingResGW::Node
  1.1201 -	 (typename FilterResGW::Node
  1.1202 -	  (typename ResGW::Node(s)
  1.1203 -	   )
  1.1204 -	  )
  1.1205 -	 );
  1.1206 -      while (!dfs.finished()) {
  1.1207 -	++dfs;
  1.1208 -	if (erasing_res_graph.valid(typename ErasingResGW::OutEdgeIt(dfs)))
  1.1209 - 	  {
  1.1210 -  	    if (dfs.isBNodeNewlyReached()) {
  1.1211 -
  1.1212 - 	      typename ErasingResGW::Node v=erasing_res_graph.aNode(dfs);
  1.1213 - 	      typename ErasingResGW::Node w=erasing_res_graph.bNode(dfs);
  1.1214 -
  1.1215 - 	      pred.set(w, /*typename ErasingResGW::OutEdgeIt*/(dfs));
  1.1216 - 	      if (erasing_res_graph.valid(pred[v])) {
  1.1217 - 		free1.set
  1.1218 -		  (w, std::min(free1[v], res_graph.resCap
  1.1219 -			       (typename ErasingResGW::OutEdgeIt(dfs))));
  1.1220 - 	      } else {
  1.1221 - 		free1.set
  1.1222 -		  (w, res_graph.resCap
  1.1223 -		   (typename ErasingResGW::OutEdgeIt(dfs)));
  1.1224 - 	      }
  1.1225 -
  1.1226 - 	      if (w==t) {
  1.1227 - 		__augment=true;
  1.1228 - 		_augment=true;
  1.1229 - 		break;
  1.1230 - 	      }
  1.1231 - 	    } else {
  1.1232 - 	      erasing_res_graph.erase(dfs);
  1.1233 -	    }
  1.1234 -	  }
  1.1235 -      }
  1.1236 -
  1.1237 -      if (__augment) {
  1.1238 -	typename ErasingResGW::Node
  1.1239 -	  n=typename FilterResGW::Node(typename ResGW::Node(t));
  1.1240 -	// 	  typename ResGW::NodeMap<Num> a(res_graph);
  1.1241 -	// 	  typename ResGW::Node b;
  1.1242 -	// 	  Num j=a[b];
  1.1243 -	// 	  typename FilterResGW::NodeMap<Num> a1(filter_res_graph);
  1.1244 -	// 	  typename FilterResGW::Node b1;
  1.1245 -	// 	  Num j1=a1[b1];
  1.1246 -	// 	  typename ErasingResGW::NodeMap<Num> a2(erasing_res_graph);
  1.1247 -	// 	  typename ErasingResGW::Node b2;
  1.1248 -	// 	  Num j2=a2[b2];
  1.1249 -	Num augment_value=free1[n];
  1.1250 -	while (erasing_res_graph.valid(pred[n])) {
  1.1251 -	  typename ErasingResGW::OutEdgeIt e=pred[n];
  1.1252 -	  res_graph.augment(e, augment_value);
  1.1253 -	  n=erasing_res_graph.source(e);
  1.1254 -	  if (res_graph.resCap(e)==0)
  1.1255 -	    erasing_res_graph.erase(e);
  1.1256 -	}
  1.1257 -      }
  1.1258 -
  1.1259 -    } //while (__augment)
  1.1260 -
  1.1261 -    status=AFTER_AUGMENTING;
  1.1262 -    return _augment;
  1.1263 -  }
  1.1264 -
  1.1265 -
  1.1266 -} //namespace lemon
  1.1267 -
  1.1268 -#endif //LEMON_MAX_FLOW_H
  1.1269 -
  1.1270 -
  1.1271 -
  1.1272 -