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