alpar@906: /* -*- C++ -*-
alpar@906:  *
alpar@1956:  * This file is a part of LEMON, a generic C++ optimization library
alpar@1956:  *
alpar@1956:  * Copyright (C) 2003-2006
alpar@1956:  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359:  * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906:  *
alpar@906:  * Permission to use, modify and distribute this software is granted
alpar@906:  * provided that this copyright notice appears in all copies. For
alpar@906:  * precise terms see the accompanying LICENSE file.
alpar@906:  *
alpar@906:  * This software is provided "AS IS" with no warranty of any kind,
alpar@906:  * express or implied, and with no claim as to its suitability for any
alpar@906:  * purpose.
alpar@906:  *
alpar@906:  */
alpar@906: 
alpar@921: #ifndef LEMON_PREFLOW_H
alpar@921: #define LEMON_PREFLOW_H
jacint@836: 
jacint@836: #include <vector>
jacint@836: #include <queue>
jacint@836: 
jacint@1762: #include <lemon/error.h>
deba@1993: #include <lemon/bits/invalid.h>
alpar@1835: #include <lemon/tolerance.h>
alpar@921: #include <lemon/maps.h>
klao@977: #include <lemon/graph_utils.h>
jacint@836: 
jacint@836: /// \file
jacint@836: /// \ingroup flowalgs
deba@1742: /// \brief Implementation of the preflow algorithm.
jacint@836: 
alpar@921: namespace lemon {
jacint@836: 
deba@1792:   ///\ingroup flowalgs
deba@1792:   ///\brief %Preflow algorithms class.
deba@1792:   ///
jacint@836:   ///This class provides an implementation of the \e preflow \e
jacint@836:   ///algorithm producing a flow of maximum value in a directed
jacint@2024:   ///graph. The preflow algorithms are the fastest known max flow algorithms. 
jacint@2024:   ///The \e source node, the \e target node, the \e
jacint@836:   ///capacity of the edges and the \e starting \e flow value of the
jacint@836:   ///edges should be passed to the algorithm through the
jacint@836:   ///constructor. It is possible to change these quantities using the
zsuzska@1285:   ///functions \ref source, \ref target, \ref capacityMap and \ref
zsuzska@1285:   ///flowMap.
jacint@836:   ///
alpar@921:   ///After running \ref lemon::Preflow::phase1() "phase1()"
alpar@921:   ///or \ref lemon::Preflow::run() "run()", the maximal flow
jacint@836:   ///value can be obtained by calling \ref flowValue(). The minimum
alpar@851:   ///value cut can be written into a <tt>bool</tt> node map by
alpar@851:   ///calling \ref minCut(). (\ref minMinCut() and \ref maxMinCut() writes
jacint@836:   ///the inclusionwise minimum and maximum of the minimum value cuts,
jacint@836:   ///resp.)
jacint@836:   ///
jacint@836:   ///\param Graph The directed graph type the algorithm runs on.
jacint@836:   ///\param Num The number type of the capacities and the flow values.
alpar@1222:   ///\param CapacityMap The capacity map type.
jacint@836:   ///\param FlowMap The flow map type.
jacint@2024:   ///\param Tolerance The tolerance type. 
jacint@836:   ///
jacint@836:   ///\author Jacint Szabo 
alpar@1227:   ///\todo Second template parameter is superfluous
jacint@836:   template <typename Graph, typename Num,
alpar@1222: 	    typename CapacityMap=typename Graph::template EdgeMap<Num>,
alpar@1835:             typename FlowMap=typename Graph::template EdgeMap<Num>,
deba@2019: 	    typename Tolerance=Tolerance<Num> >
jacint@836:   class Preflow {
jacint@836:   protected:
jacint@836:     typedef typename Graph::Node Node;
jacint@836:     typedef typename Graph::NodeIt NodeIt;
jacint@836:     typedef typename Graph::EdgeIt EdgeIt;
jacint@836:     typedef typename Graph::OutEdgeIt OutEdgeIt;
jacint@836:     typedef typename Graph::InEdgeIt InEdgeIt;
jacint@836: 
jacint@836:     typedef typename Graph::template NodeMap<Node> NNMap;
jacint@836:     typedef typename std::vector<Node> VecNode;
jacint@836: 
alpar@1222:     const Graph* _g;
alpar@1222:     Node _source;
alpar@1222:     Node _target;
alpar@1222:     const CapacityMap* _capacity;
alpar@1222:     FlowMap* _flow;
alpar@1835: 
deba@2019:     Tolerance _surely;
alpar@1835:     
alpar@1222:     int _node_num;      //the number of nodes of G
jacint@836:     
jacint@836:     typename Graph::template NodeMap<int> level;  
jacint@836:     typename Graph::template NodeMap<Num> excess;
jacint@836: 
jacint@836:     // constants used for heuristics
jacint@836:     static const int H0=20;
jacint@836:     static const int H1=1;
jacint@836: 
jacint@1762:   public:
jacint@1762: 
jacint@1762:     ///\ref Exception for the case when s=t.
jacint@1762: 
jacint@1762:     ///\ref Exception for the case when the source equals the target.
jacint@1762:     class InvalidArgument : public lemon::LogicError {
jacint@836:     public:
jacint@1762:       virtual const char* exceptionName() const {
jacint@1762: 	return "lemon::Preflow::InvalidArgument";
jacint@1762:       }
jacint@1762:     };
jacint@1762:     
jacint@1762:     
jacint@836:     ///Indicates the property of the starting flow map.
jacint@1762:     
alpar@1222:     ///Indicates the property of the starting flow map.
jacint@836:     ///
jacint@836:     enum FlowEnum{
alpar@1898:       ///indicates an unspecified edge map. \c flow will be 
alpar@1898:       ///set to the constant zero flow in the beginning of
alpar@1898:       ///the algorithm in this case.
jacint@836:       NO_FLOW,
alpar@1898:       ///constant zero flow
jacint@836:       ZERO_FLOW,
alpar@1898:       ///any flow, i.e. the sum of the in-flows equals to
alpar@1898:       ///the sum of the out-flows in every node except the \c source and
alpar@1898:       ///the \c target.
jacint@836:       GEN_FLOW,
alpar@1898:       ///any preflow, i.e. the sum of the in-flows is at 
alpar@1898:       ///least the sum of the out-flows in every node except the \c source.
jacint@836:       PRE_FLOW
jacint@836:     };
jacint@836: 
jacint@836:     ///Indicates the state of the preflow algorithm.
jacint@836: 
alpar@1222:     ///Indicates the state of the preflow algorithm.
jacint@836:     ///
jacint@836:     enum StatusEnum {
alpar@1898:       ///before running the algorithm or
alpar@1898:       ///at an unspecified state.
jacint@836:       AFTER_NOTHING,
alpar@1898:       ///right after running \ref phase1()
jacint@836:       AFTER_PREFLOW_PHASE_1,      
alpar@1898:       ///after running \ref phase2()
jacint@836:       AFTER_PREFLOW_PHASE_2
jacint@836:     };
jacint@836:     
jacint@1762:   protected: 
jacint@1762:     FlowEnum flow_prop;
jacint@836:     StatusEnum status; // Do not needle this flag only if necessary.
jacint@836:     
jacint@836:   public: 
jacint@836:     ///The constructor of the class.
jacint@836: 
jacint@836:     ///The constructor of the class. 
zsuzska@1285:     ///\param _gr The directed graph the algorithm runs on. 
jacint@836:     ///\param _s The source node.
jacint@836:     ///\param _t The target node.
alpar@1222:     ///\param _cap The capacity of the edges. 
alpar@1222:     ///\param _f The flow of the edges. 
alpar@1953:     ///\param tol Tolerance class.
jacint@836:     ///Except the graph, all of these parameters can be reset by
zsuzska@1285:     ///calling \ref source, \ref target, \ref capacityMap and \ref
zsuzska@1285:     ///flowMap, resp.
deba@2033:     Preflow(const Graph& _gr, Node _s, Node _t, 
deba@2033:             const CapacityMap& _cap, FlowMap& _f,
deba@2033:             const Tolerance &_sr=Tolerance()) :
alpar@1222: 	_g(&_gr), _source(_s), _target(_t), _capacity(&_cap),
deba@2019: 	_flow(&_f), _surely(_sr),
alpar@1835: 	_node_num(countNodes(_gr)), level(_gr), excess(_gr,0), 
jacint@1762: 	flow_prop(NO_FLOW), status(AFTER_NOTHING) { 
jacint@1762: 	if ( _source==_target )
jacint@1762: 	  throw InvalidArgument();
deba@2033:     }
jacint@1762:     
alpar@1898:     ///Give a reference to the tolerance handler class
jacint@836: 
alpar@1898:     ///Give a reference to the tolerance handler class
alpar@1898:     ///\sa Tolerance
deba@2019:     Tolerance &tolerance() { return _surely; }
alpar@1898: 
jacint@836:     ///Runs the preflow algorithm.  
jacint@836: 
alpar@851:     ///Runs the preflow algorithm.
alpar@851:     ///
jacint@836:     void run() {
jacint@836:       phase1(flow_prop);
jacint@836:       phase2();
jacint@836:     }
jacint@836:     
jacint@836:     ///Runs the preflow algorithm.  
jacint@836:     
jacint@836:     ///Runs the preflow algorithm. 
jacint@836:     ///\pre The starting flow map must be
jacint@836:     /// - a constant zero flow if \c fp is \c ZERO_FLOW,
jacint@836:     /// - an arbitrary flow if \c fp is \c GEN_FLOW,
jacint@836:     /// - an arbitrary preflow if \c fp is \c PRE_FLOW,
jacint@836:     /// - any map if \c fp is NO_FLOW.
jacint@836:     ///If the starting flow map is a flow or a preflow then 
jacint@836:     ///the algorithm terminates faster.
jacint@836:     void run(FlowEnum fp) {
jacint@836:       flow_prop=fp;
jacint@836:       run();
jacint@836:     }
jacint@836:       
jacint@836:     ///Runs the first phase of the preflow algorithm.
jacint@836: 
jacint@920:     ///The preflow algorithm consists of two phases, this method runs
jacint@920:     ///the first phase. After the first phase the maximum flow value
zsuzska@1285:     ///and a minimum value cut can already be computed, although a
jacint@920:     ///maximum flow is not yet obtained. So after calling this method
jacint@920:     ///\ref flowValue returns the value of a maximum flow and \ref
jacint@920:     ///minCut returns a minimum cut.     
jacint@920:     ///\warning \ref minMinCut and \ref maxMinCut do not give minimum
jacint@920:     ///value cuts unless calling \ref phase2.  
jacint@920:     ///\pre The starting flow must be 
jacint@920:     ///- a constant zero flow if \c fp is \c ZERO_FLOW, 
jacint@920:     ///- an arbitary flow if \c fp is \c GEN_FLOW, 
jacint@920:     ///- an arbitary preflow if \c fp is \c PRE_FLOW, 
jacint@920:     ///- any map if \c fp is NO_FLOW.
jacint@836:     void phase1(FlowEnum fp)
jacint@836:     {
jacint@836:       flow_prop=fp;
jacint@836:       phase1();
jacint@836:     }
jacint@836: 
jacint@836:     
jacint@836:     ///Runs the first phase of the preflow algorithm.
jacint@836: 
jacint@920:     ///The preflow algorithm consists of two phases, this method runs
jacint@920:     ///the first phase. After the first phase the maximum flow value
zsuzska@1285:     ///and a minimum value cut can already be computed, although a
jacint@920:     ///maximum flow is not yet obtained. So after calling this method
jacint@920:     ///\ref flowValue returns the value of a maximum flow and \ref
jacint@920:     ///minCut returns a minimum cut.
deba@1786:     ///\warning \ref minMinCut() and \ref maxMinCut() do not
alpar@911:     ///give minimum value cuts unless calling \ref phase2().
jacint@836:     void phase1()
jacint@836:     {
alpar@1222:       int heur0=(int)(H0*_node_num);  //time while running 'bound decrease'
alpar@1222:       int heur1=(int)(H1*_node_num);  //time while running 'highest label'
jacint@836:       int heur=heur1;         //starting time interval (#of relabels)
jacint@836:       int numrelabel=0;
jacint@836: 
jacint@836:       bool what_heur=1;
jacint@836:       //It is 0 in case 'bound decrease' and 1 in case 'highest label'
jacint@836: 
jacint@836:       bool end=false;
jacint@836:       //Needed for 'bound decrease', true means no active 
jacint@836:       //nodes are above bound b.
jacint@836: 
alpar@1222:       int k=_node_num-2;  //bound on the highest level under n containing a node
jacint@2024:       int b=k;    //bound on the highest level under n containing an active node
jacint@836: 
alpar@1222:       VecNode first(_node_num, INVALID);
alpar@1222:       NNMap next(*_g, INVALID);
jacint@836: 
alpar@1222:       NNMap left(*_g, INVALID);
alpar@1222:       NNMap right(*_g, INVALID);
alpar@1222:       VecNode level_list(_node_num,INVALID);
jacint@836:       //List of the nodes in level i<n, set to n.
jacint@836: 
jacint@836:       preflowPreproc(first, next, level_list, left, right);
jacint@836: 
jacint@836:       //Push/relabel on the highest level active nodes.
jacint@836:       while ( true ) {
jacint@836: 	if ( b == 0 ) {
jacint@836: 	  if ( !what_heur && !end && k > 0 ) {
jacint@836: 	    b=k;
jacint@836: 	    end=true;
jacint@836: 	  } else break;
jacint@836: 	}
jacint@836: 
jacint@836: 	if ( first[b]==INVALID ) --b;
jacint@836: 	else {
jacint@836: 	  end=false;
jacint@836: 	  Node w=first[b];
jacint@836: 	  first[b]=next[w];
jacint@836: 	  int newlevel=push(w, next, first);
jacint@2024: 	  if ( _surely.positive(excess[w]) ) relabel(w, newlevel, first, next, level_list, 
jacint@836: 				       left, right, b, k, what_heur);
jacint@836: 
jacint@836: 	  ++numrelabel;
jacint@836: 	  if ( numrelabel >= heur ) {
jacint@836: 	    numrelabel=0;
jacint@836: 	    if ( what_heur ) {
jacint@836: 	      what_heur=0;
jacint@836: 	      heur=heur0;
jacint@836: 	      end=false;
jacint@836: 	    } else {
jacint@836: 	      what_heur=1;
jacint@836: 	      heur=heur1;
jacint@836: 	      b=k;
jacint@836: 	    }
jacint@836: 	  }
jacint@836: 	}
jacint@836:       }
jacint@836:       flow_prop=PRE_FLOW;
jacint@836:       status=AFTER_PREFLOW_PHASE_1;
jacint@836:     }
jacint@836:     // Heuristics:
jacint@836:     //   2 phase
jacint@836:     //   gap
jacint@836:     //   list 'level_list' on the nodes on level i implemented by hand
jacint@836:     //   stack 'active' on the active nodes on level i      
jacint@836:     //   runs heuristic 'highest label' for H1*n relabels
alpar@1222:     //   runs heuristic 'bound decrease' for H0*n relabels,
alpar@1222:     //        starts with 'highest label'
jacint@836:     //   Parameters H0 and H1 are initialized to 20 and 1.
jacint@836: 
jacint@836: 
jacint@836:     ///Runs the second phase of the preflow algorithm.
jacint@836: 
jacint@836:     ///The preflow algorithm consists of two phases, this method runs
alpar@1631:     ///the second phase. After calling \ref phase1() and then
alpar@1631:     ///\ref phase2(),
alpar@1631:     /// \ref flowMap() return a maximum flow, \ref flowValue
jacint@920:     ///returns the value of a maximum flow, \ref minCut returns a
jacint@920:     ///minimum cut, while the methods \ref minMinCut and \ref
jacint@920:     ///maxMinCut return the inclusionwise minimum and maximum cuts of
jacint@920:     ///minimum value, resp.  \pre \ref phase1 must be called before.
jacint@836:     void phase2()
jacint@836:     {
jacint@836: 
alpar@1222:       int k=_node_num-2;  //bound on the highest level under n containing a node
jacint@836:       int b=k;    //bound on the highest level under n of an active node
jacint@836: 
jacint@836:     
alpar@1222:       VecNode first(_node_num, INVALID);
alpar@1222:       NNMap next(*_g, INVALID); 
alpar@1222:       level.set(_source,0);
jacint@836:       std::queue<Node> bfs_queue;
alpar@1222:       bfs_queue.push(_source);
jacint@836: 
jacint@836:       while ( !bfs_queue.empty() ) {
jacint@836: 
jacint@836: 	Node v=bfs_queue.front();
jacint@836: 	bfs_queue.pop();
jacint@836: 	int l=level[v]+1;
jacint@836: 
alpar@1222: 	for(InEdgeIt e(*_g,v); e!=INVALID; ++e) {
jacint@2024: 	  if ( !_surely.less((*_flow)[e], (*_capacity)[e]) ) continue;
alpar@1222: 	  Node u=_g->source(e);
alpar@1222: 	  if ( level[u] >= _node_num ) {
jacint@836: 	    bfs_queue.push(u);
jacint@836: 	    level.set(u, l);
jacint@2024: 	    if ( _surely.positive(excess[u]) ) {
jacint@836: 	      next.set(u,first[l]);
jacint@836: 	      first[l]=u;
jacint@836: 	    }
jacint@836: 	  }
jacint@836: 	}
jacint@836: 
alpar@1222: 	for(OutEdgeIt e(*_g,v); e!=INVALID; ++e) {
jacint@2024: 	  if ( !_surely.positive((*_flow)[e]) ) continue;
alpar@1222: 	  Node u=_g->target(e);
alpar@1222: 	  if ( level[u] >= _node_num ) {
jacint@836: 	    bfs_queue.push(u);
jacint@836: 	    level.set(u, l);
jacint@2024: 	    if ( _surely.positive(excess[u]) ) {
jacint@836: 	      next.set(u,first[l]);
jacint@836: 	      first[l]=u;
jacint@836: 	    }
jacint@836: 	  }
jacint@836: 	}
jacint@836:       }
alpar@1222:       b=_node_num-2;
jacint@836: 
jacint@836:       while ( true ) {
jacint@836: 
jacint@836: 	if ( b == 0 ) break;
jacint@836: 	if ( first[b]==INVALID ) --b;
jacint@836: 	else {
jacint@836: 	  Node w=first[b];
jacint@836: 	  first[b]=next[w];
jacint@836: 	  int newlevel=push(w,next, first);
jacint@836: 	  
jacint@836: 	  //relabel
jacint@2024: 	  if ( _surely.positive(excess[w]) ) {
jacint@836: 	    level.set(w,++newlevel);
jacint@836: 	    next.set(w,first[newlevel]);
jacint@836: 	    first[newlevel]=w;
jacint@836: 	    b=newlevel;
jacint@836: 	  }
jacint@836: 	} 
jacint@836:       } // while(true)
jacint@836:       flow_prop=GEN_FLOW;
jacint@836:       status=AFTER_PREFLOW_PHASE_2;
jacint@836:     }
jacint@836: 
jacint@836:     /// Returns the value of the maximum flow.
jacint@836: 
jacint@836:     /// Returns the value of the maximum flow by returning the excess
alpar@911:     /// of the target node \c t. This value equals to the value of
jacint@836:     /// the maximum flow already after running \ref phase1.
jacint@836:     Num flowValue() const {
alpar@1222:       return excess[_target];
jacint@836:     }
jacint@836: 
jacint@836: 
jacint@836:     ///Returns a minimum value cut.
jacint@836: 
jacint@836:     ///Sets \c M to the characteristic vector of a minimum value
jacint@836:     ///cut. This method can be called both after running \ref
jacint@836:     ///phase1 and \ref phase2. It is much faster after
marci@849:     ///\ref phase1.  \pre M should be a bool-valued node-map. \pre
alpar@911:     ///If \ref minCut() is called after \ref phase2() then M should
jacint@836:     ///be initialized to false.
jacint@836:     template<typename _CutMap>
jacint@836:     void minCut(_CutMap& M) const {
jacint@836:       switch ( status ) {
jacint@836: 	case AFTER_PREFLOW_PHASE_1:
alpar@1222: 	for(NodeIt v(*_g); v!=INVALID; ++v) {
alpar@1222: 	  if (level[v] < _node_num) {
jacint@836: 	    M.set(v, false);
jacint@836: 	  } else {
jacint@836: 	    M.set(v, true);
jacint@836: 	  }
jacint@836: 	}
jacint@836: 	break;
jacint@836: 	case AFTER_PREFLOW_PHASE_2:
jacint@836: 	minMinCut(M);
jacint@836: 	break;
jacint@836: 	case AFTER_NOTHING:
jacint@836: 	break;
jacint@836:       }
jacint@836:     }
jacint@836: 
jacint@836:     ///Returns the inclusionwise minimum of the minimum value cuts.
jacint@836: 
jacint@836:     ///Sets \c M to the characteristic vector of the minimum value cut
jacint@836:     ///which is inclusionwise minimum. It is computed by processing a
jacint@836:     ///bfs from the source node \c s in the residual graph.  \pre M
jacint@836:     ///should be a node map of bools initialized to false.  \pre \ref
jacint@836:     ///phase2 should already be run.
jacint@836:     template<typename _CutMap>
jacint@836:     void minMinCut(_CutMap& M) const {
jacint@836: 
jacint@836:       std::queue<Node> queue;
alpar@1222:       M.set(_source,true);
alpar@1227:       queue.push(_source);
jacint@836:       
jacint@836:       while (!queue.empty()) {
jacint@836: 	Node w=queue.front();
jacint@836: 	queue.pop();
jacint@836: 	
alpar@1222: 	for(OutEdgeIt e(*_g,w) ; e!=INVALID; ++e) {
alpar@1222: 	  Node v=_g->target(e);
jacint@2024: 	  if (!M[v] && _surely.less((*_flow)[e] , (*_capacity)[e]) ) {
jacint@836: 	    queue.push(v);
jacint@836: 	    M.set(v, true);
jacint@836: 	  }
jacint@836: 	}
jacint@836: 	
alpar@1222: 	for(InEdgeIt e(*_g,w) ; e!=INVALID; ++e) {
alpar@1222: 	  Node v=_g->source(e);
jacint@2024: 	  if (!M[v] && _surely.positive((*_flow)[e]) ) {
jacint@836: 	    queue.push(v);
jacint@836: 	    M.set(v, true);
jacint@836: 	  }
jacint@836: 	}
jacint@836:       }
jacint@836:     }
jacint@836:     
jacint@836:     ///Returns the inclusionwise maximum of the minimum value cuts.
jacint@836: 
jacint@836:     ///Sets \c M to the characteristic vector of the minimum value cut
jacint@836:     ///which is inclusionwise maximum. It is computed by processing a
jacint@836:     ///backward bfs from the target node \c t in the residual graph.
alpar@911:     ///\pre \ref phase2() or run() should already be run.
jacint@836:     template<typename _CutMap>
jacint@836:     void maxMinCut(_CutMap& M) const {
jacint@836: 
alpar@1222:       for(NodeIt v(*_g) ; v!=INVALID; ++v) M.set(v, true);
jacint@836: 
jacint@836:       std::queue<Node> queue;
jacint@836: 
alpar@1222:       M.set(_target,false);
alpar@1222:       queue.push(_target);
jacint@836: 
jacint@836:       while (!queue.empty()) {
jacint@836:         Node w=queue.front();
jacint@836: 	queue.pop();
jacint@836: 
alpar@1222: 	for(InEdgeIt e(*_g,w) ; e!=INVALID; ++e) {
alpar@1222: 	  Node v=_g->source(e);
jacint@2024: 	  if (M[v] && _surely.less((*_flow)[e], (*_capacity)[e]) ) {
jacint@836: 	    queue.push(v);
jacint@836: 	    M.set(v, false);
jacint@836: 	  }
jacint@836: 	}
jacint@836: 
alpar@1222: 	for(OutEdgeIt e(*_g,w) ; e!=INVALID; ++e) {
alpar@1222: 	  Node v=_g->target(e);
jacint@2024: 	  if (M[v] && _surely.positive((*_flow)[e]) ) {
jacint@836: 	    queue.push(v);
jacint@836: 	    M.set(v, false);
jacint@836: 	  }
jacint@836: 	}
jacint@836:       }
jacint@836:     }
jacint@836: 
jacint@836:     ///Sets the source node to \c _s.
jacint@836: 
jacint@836:     ///Sets the source node to \c _s.
jacint@836:     /// 
alpar@1222:     void source(Node _s) { 
alpar@1222:       _source=_s; 
jacint@836:       if ( flow_prop != ZERO_FLOW ) flow_prop=NO_FLOW;
jacint@836:       status=AFTER_NOTHING; 
jacint@836:     }
jacint@836: 
alpar@1222:     ///Returns the source node.
alpar@1222: 
alpar@1222:     ///Returns the source node.
alpar@1222:     /// 
alpar@1222:     Node source() const { 
alpar@1222:       return _source;
alpar@1222:     }
alpar@1222: 
jacint@836:     ///Sets the target node to \c _t.
jacint@836: 
jacint@836:     ///Sets the target node to \c _t.
jacint@836:     ///
alpar@1222:     void target(Node _t) { 
alpar@1222:       _target=_t; 
jacint@836:       if ( flow_prop == GEN_FLOW ) flow_prop=PRE_FLOW;
jacint@836:       status=AFTER_NOTHING; 
jacint@836:     }
jacint@836: 
alpar@1222:     ///Returns the target node.
alpar@1222: 
alpar@1222:     ///Returns the target node.
alpar@1222:     /// 
alpar@1222:     Node target() const { 
alpar@1222:       return _target;
alpar@1222:     }
alpar@1222: 
jacint@836:     /// Sets the edge map of the capacities to _cap.
jacint@836: 
jacint@836:     /// Sets the edge map of the capacities to _cap.
jacint@836:     /// 
alpar@1222:     void capacityMap(const CapacityMap& _cap) { 
alpar@1222:       _capacity=&_cap; 
jacint@836:       status=AFTER_NOTHING; 
jacint@836:     }
zsuzska@1285:     /// Returns a reference to capacity map.
alpar@1222: 
zsuzska@1285:     /// Returns a reference to capacity map.
alpar@1222:     /// 
alpar@1222:     const CapacityMap &capacityMap() const { 
alpar@1222:       return *_capacity;
alpar@1222:     }
jacint@836: 
jacint@836:     /// Sets the edge map of the flows to _flow.
jacint@836: 
jacint@836:     /// Sets the edge map of the flows to _flow.
jacint@836:     /// 
alpar@1222:     void flowMap(FlowMap& _f) { 
alpar@1222:       _flow=&_f; 
jacint@836:       flow_prop=NO_FLOW;
jacint@836:       status=AFTER_NOTHING; 
jacint@836:     }
alpar@1222:      
zsuzska@1285:     /// Returns a reference to flow map.
jacint@836: 
zsuzska@1285:     /// Returns a reference to flow map.
alpar@1222:     /// 
alpar@1222:     const FlowMap &flowMap() const { 
alpar@1222:       return *_flow;
alpar@1222:     }
jacint@836: 
jacint@836:   private:
jacint@836: 
jacint@836:     int push(Node w, NNMap& next, VecNode& first) {
jacint@836: 
jacint@836:       int lev=level[w];
jacint@836:       Num exc=excess[w];
alpar@1222:       int newlevel=_node_num;       //bound on the next level of w
jacint@836: 
alpar@1222:       for(OutEdgeIt e(*_g,w) ; e!=INVALID; ++e) {
jacint@2024: 	if ( !_surely.less((*_flow)[e], (*_capacity)[e]) ) continue;
alpar@1222: 	Node v=_g->target(e);
jacint@2024: 	
jacint@836: 	if( lev > level[v] ) { //Push is allowed now
jacint@836: 	  
jacint@2024: 	  if ( !_surely.positive(excess[v]) && v!=_target && v!=_source ) {
jacint@836: 	    next.set(v,first[level[v]]);
jacint@836: 	    first[level[v]]=v;
jacint@836: 	  }
jacint@836: 
alpar@1222: 	  Num cap=(*_capacity)[e];
alpar@1222: 	  Num flo=(*_flow)[e];
jacint@836: 	  Num remcap=cap-flo;
jacint@836: 	  
jacint@2024: 	  if ( ! _surely.less(remcap, exc) ) { //A nonsaturating push.
jacint@836: 	    
alpar@1222: 	    _flow->set(e, flo+exc);
jacint@836: 	    excess.set(v, excess[v]+exc);
jacint@836: 	    exc=0;
jacint@836: 	    break;
jacint@836: 
jacint@836: 	  } else { //A saturating push.
alpar@1222: 	    _flow->set(e, cap);
jacint@836: 	    excess.set(v, excess[v]+remcap);
jacint@836: 	    exc-=remcap;
jacint@836: 	  }
jacint@836: 	} else if ( newlevel > level[v] ) newlevel = level[v];
jacint@836:       } //for out edges wv
jacint@836: 
jacint@2024:       if ( _surely.positive(exc) ) {
alpar@1222: 	for(InEdgeIt e(*_g,w) ; e!=INVALID; ++e) {
jacint@836: 	  
jacint@2024: 	  if ( !_surely.positive((*_flow)[e]) ) continue;
alpar@1222: 	  Node v=_g->source(e);
jacint@2024: 	  
jacint@836: 	  if( lev > level[v] ) { //Push is allowed now
jacint@836: 
jacint@2024: 	    if ( !_surely.positive(excess[v]) && v!=_target && v!=_source ) {
jacint@836: 	      next.set(v,first[level[v]]);
jacint@836: 	      first[level[v]]=v;
jacint@836: 	    }
jacint@836: 
alpar@1222: 	    Num flo=(*_flow)[e];
jacint@836: 
jacint@2024: 	    if ( !_surely.less(flo, exc) ) { //A nonsaturating push.
jacint@836: 
alpar@1222: 	      _flow->set(e, flo-exc);
jacint@836: 	      excess.set(v, excess[v]+exc);
jacint@836: 	      exc=0;
jacint@836: 	      break;
jacint@836: 	    } else {  //A saturating push.
jacint@836: 
jacint@836: 	      excess.set(v, excess[v]+flo);
jacint@836: 	      exc-=flo;
alpar@1222: 	      _flow->set(e,0);
jacint@836: 	    }
jacint@836: 	  } else if ( newlevel > level[v] ) newlevel = level[v];
jacint@836: 	} //for in edges vw
jacint@836: 
jacint@836:       } // if w still has excess after the out edge for cycle
jacint@836: 
jacint@836:       excess.set(w, exc);
jacint@836:       
jacint@836:       return newlevel;
jacint@836:     }
jacint@836:     
jacint@836:     
jacint@836:     
jacint@836:     void preflowPreproc(VecNode& first, NNMap& next, 
jacint@836: 			VecNode& level_list, NNMap& left, NNMap& right)
jacint@836:     {
alpar@1222:       for(NodeIt v(*_g); v!=INVALID; ++v) level.set(v,_node_num);
jacint@836:       std::queue<Node> bfs_queue;
jacint@836:       
jacint@836:       if ( flow_prop == GEN_FLOW || flow_prop == PRE_FLOW ) {
jacint@836: 	//Reverse_bfs from t in the residual graph,
jacint@836: 	//to find the starting level.
alpar@1222: 	level.set(_target,0);
alpar@1222: 	bfs_queue.push(_target);
jacint@836: 	
jacint@836: 	while ( !bfs_queue.empty() ) {
jacint@836: 	  
jacint@836: 	  Node v=bfs_queue.front();
jacint@836: 	  bfs_queue.pop();
jacint@836: 	  int l=level[v]+1;
jacint@836: 	  
alpar@1222: 	  for(InEdgeIt e(*_g,v) ; e!=INVALID; ++e) {
jacint@2024: 	    if ( !_surely.less((*_flow)[e],(*_capacity)[e]) ) continue;
alpar@1222: 	    Node w=_g->source(e);
alpar@1222: 	    if ( level[w] == _node_num && w != _source ) {
jacint@836: 	      bfs_queue.push(w);
jacint@836: 	      Node z=level_list[l];
jacint@836: 	      if ( z!=INVALID ) left.set(z,w);
jacint@836: 	      right.set(w,z);
jacint@836: 	      level_list[l]=w;
jacint@836: 	      level.set(w, l);
jacint@836: 	    }
jacint@836: 	  }
jacint@836: 	  
alpar@1222: 	  for(OutEdgeIt e(*_g,v) ; e!=INVALID; ++e) {
jacint@2024: 	    if ( !_surely.positive((*_flow)[e]) ) continue;
alpar@1222: 	    Node w=_g->target(e);
alpar@1222: 	    if ( level[w] == _node_num && w != _source ) {
jacint@836: 	      bfs_queue.push(w);
jacint@836: 	      Node z=level_list[l];
jacint@836: 	      if ( z!=INVALID ) left.set(z,w);
jacint@836: 	      right.set(w,z);
jacint@836: 	      level_list[l]=w;
jacint@836: 	      level.set(w, l);
jacint@836: 	    }
jacint@836: 	  }
jacint@836: 	} //while
jacint@836:       } //if
jacint@836: 
jacint@836: 
jacint@836:       switch (flow_prop) {
jacint@836: 	case NO_FLOW:  
alpar@1222: 	for(EdgeIt e(*_g); e!=INVALID; ++e) _flow->set(e,0);
jacint@836: 	case ZERO_FLOW:
alpar@1222: 	for(NodeIt v(*_g); v!=INVALID; ++v) excess.set(v,0);
jacint@836: 	
jacint@836: 	//Reverse_bfs from t, to find the starting level.
alpar@1222: 	level.set(_target,0);
alpar@1222: 	bfs_queue.push(_target);
jacint@836: 	
jacint@836: 	while ( !bfs_queue.empty() ) {
jacint@836: 	  
jacint@836: 	  Node v=bfs_queue.front();
jacint@836: 	  bfs_queue.pop();
jacint@836: 	  int l=level[v]+1;
jacint@836: 	  
alpar@1222: 	  for(InEdgeIt e(*_g,v) ; e!=INVALID; ++e) {
alpar@1222: 	    Node w=_g->source(e);
alpar@1222: 	    if ( level[w] == _node_num && w != _source ) {
jacint@836: 	      bfs_queue.push(w);
jacint@836: 	      Node z=level_list[l];
jacint@836: 	      if ( z!=INVALID ) left.set(z,w);
jacint@836: 	      right.set(w,z);
jacint@836: 	      level_list[l]=w;
jacint@836: 	      level.set(w, l);
jacint@836: 	    }
jacint@836: 	  }
jacint@836: 	}
jacint@836: 	
jacint@836: 	//the starting flow
alpar@1222: 	for(OutEdgeIt e(*_g,_source) ; e!=INVALID; ++e) {
alpar@1222: 	  Num c=(*_capacity)[e];
jacint@2024: 	  if ( !_surely.positive(c) ) continue;
alpar@1222: 	  Node w=_g->target(e);
alpar@1222: 	  if ( level[w] < _node_num ) {
jacint@2024: 	    if ( !_surely.positive(excess[w]) && w!=_target ) { //putting into the stack
jacint@836: 	      next.set(w,first[level[w]]);
jacint@836: 	      first[level[w]]=w;
jacint@836: 	    }
alpar@1222: 	    _flow->set(e, c);
jacint@836: 	    excess.set(w, excess[w]+c);
jacint@836: 	  }
jacint@836: 	}
jacint@836: 	break;
jacint@836: 
jacint@836: 	case GEN_FLOW:
alpar@1222: 	for(NodeIt v(*_g); v!=INVALID; ++v) excess.set(v,0);
jacint@836: 	{
jacint@836: 	  Num exc=0;
alpar@1222: 	  for(InEdgeIt e(*_g,_target) ; e!=INVALID; ++e) exc+=(*_flow)[e];
alpar@1222: 	  for(OutEdgeIt e(*_g,_target) ; e!=INVALID; ++e) exc-=(*_flow)[e];
alpar@1222: 	  excess.set(_target,exc);
jacint@836: 	}
jacint@836: 
jacint@836: 	//the starting flow
alpar@1222: 	for(OutEdgeIt e(*_g,_source); e!=INVALID; ++e)	{
alpar@1222: 	  Num rem=(*_capacity)[e]-(*_flow)[e];
jacint@2024: 	  if ( !_surely.positive(rem) ) continue;
alpar@1222: 	  Node w=_g->target(e);
alpar@1222: 	  if ( level[w] < _node_num ) {
jacint@2024: 	    if ( !_surely.positive(excess[w]) && w!=_target ) { //putting into the stack
jacint@836: 	      next.set(w,first[level[w]]);
jacint@836: 	      first[level[w]]=w;
jacint@836: 	    }   
alpar@1222: 	    _flow->set(e, (*_capacity)[e]);
jacint@836: 	    excess.set(w, excess[w]+rem);
jacint@836: 	  }
jacint@836: 	}
jacint@836: 	
alpar@1222: 	for(InEdgeIt e(*_g,_source); e!=INVALID; ++e) {
jacint@2024: 	  if ( !_surely.positive((*_flow)[e]) ) continue;
alpar@1222: 	  Node w=_g->source(e);
alpar@1222: 	  if ( level[w] < _node_num ) {
jacint@2024: 	    if ( !_surely.positive(excess[w]) && w!=_target ) {
jacint@836: 	      next.set(w,first[level[w]]);
jacint@836: 	      first[level[w]]=w;
jacint@836: 	    }  
alpar@1222: 	    excess.set(w, excess[w]+(*_flow)[e]);
alpar@1222: 	    _flow->set(e, 0);
jacint@836: 	  }
jacint@836: 	}
jacint@836: 	break;
jacint@836: 
jacint@836: 	case PRE_FLOW:	
jacint@836: 	//the starting flow
alpar@1222: 	for(OutEdgeIt e(*_g,_source) ; e!=INVALID; ++e) {
alpar@1222: 	  Num rem=(*_capacity)[e]-(*_flow)[e];
jacint@2024: 	  if ( !_surely.positive(rem) ) continue;
alpar@1222: 	  Node w=_g->target(e);
alpar@1222: 	  if ( level[w] < _node_num ) _flow->set(e, (*_capacity)[e]);
jacint@836: 	}
jacint@836: 	
alpar@1222: 	for(InEdgeIt e(*_g,_source) ; e!=INVALID; ++e) {
jacint@2024: 	  if ( !_surely.positive((*_flow)[e]) ) continue;
alpar@1222: 	  Node w=_g->source(e);
alpar@1222: 	  if ( level[w] < _node_num ) _flow->set(e, 0);
jacint@836: 	}
jacint@836: 	
jacint@836: 	//computing the excess
alpar@1222: 	for(NodeIt w(*_g); w!=INVALID; ++w) {
jacint@836: 	  Num exc=0;
alpar@1222: 	  for(InEdgeIt e(*_g,w); e!=INVALID; ++e) exc+=(*_flow)[e];
alpar@1222: 	  for(OutEdgeIt e(*_g,w); e!=INVALID; ++e) exc-=(*_flow)[e];
jacint@836: 	  excess.set(w,exc);
jacint@836: 	  
jacint@836: 	  //putting the active nodes into the stack
jacint@836: 	  int lev=level[w];
jacint@2024: 	    if ( _surely.positive(exc) && lev < _node_num && Node(w) != _target ) {
jacint@836: 	      next.set(w,first[lev]);
jacint@836: 	      first[lev]=w;
jacint@836: 	    }
jacint@836: 	}
jacint@836: 	break;
jacint@836:       } //switch
jacint@836:     } //preflowPreproc
jacint@836: 
jacint@836: 
jacint@836:     void relabel(Node w, int newlevel, VecNode& first, NNMap& next, 
jacint@836: 		 VecNode& level_list, NNMap& left,
jacint@836: 		 NNMap& right, int& b, int& k, bool what_heur )
jacint@836:     {
jacint@836: 
jacint@836:       int lev=level[w];
jacint@836: 
jacint@836:       Node right_n=right[w];
jacint@836:       Node left_n=left[w];
jacint@836: 
jacint@836:       //unlacing starts
jacint@836:       if ( right_n!=INVALID ) {
jacint@836: 	if ( left_n!=INVALID ) {
jacint@836: 	  right.set(left_n, right_n);
jacint@836: 	  left.set(right_n, left_n);
jacint@836: 	} else {
jacint@836: 	  level_list[lev]=right_n;
jacint@836: 	  left.set(right_n, INVALID);
jacint@836: 	}
jacint@836:       } else {
jacint@836: 	if ( left_n!=INVALID ) {
jacint@836: 	  right.set(left_n, INVALID);
jacint@836: 	} else {
jacint@836: 	  level_list[lev]=INVALID;
jacint@836: 	}
jacint@836:       }
jacint@836:       //unlacing ends
jacint@836: 
jacint@836:       if ( level_list[lev]==INVALID ) {
jacint@836: 
jacint@836: 	//gapping starts
jacint@836: 	for (int i=lev; i!=k ; ) {
jacint@836: 	  Node v=level_list[++i];
jacint@836: 	  while ( v!=INVALID ) {
alpar@1222: 	    level.set(v,_node_num);
jacint@836: 	    v=right[v];
jacint@836: 	  }
jacint@836: 	  level_list[i]=INVALID;
jacint@836: 	  if ( !what_heur ) first[i]=INVALID;
jacint@836: 	}
jacint@836: 
alpar@1222: 	level.set(w,_node_num);
jacint@836: 	b=lev-1;
jacint@836: 	k=b;
jacint@836: 	//gapping ends
jacint@836: 
jacint@836:       } else {
jacint@836: 
alpar@1222: 	if ( newlevel == _node_num ) level.set(w,_node_num);
jacint@836: 	else {
jacint@836: 	  level.set(w,++newlevel);
jacint@836: 	  next.set(w,first[newlevel]);
jacint@836: 	  first[newlevel]=w;
jacint@836: 	  if ( what_heur ) b=newlevel;
jacint@836: 	  if ( k < newlevel ) ++k;      //now k=newlevel
jacint@836: 	  Node z=level_list[newlevel];
jacint@836: 	  if ( z!=INVALID ) left.set(z,w);
jacint@836: 	  right.set(w,z);
jacint@836: 	  left.set(w,INVALID);
jacint@836: 	  level_list[newlevel]=w;
jacint@836: 	}
jacint@836:       }
jacint@836:     } //relabel
jacint@836: 
jacint@836:   }; 
alpar@1227: 
deba@1792:   ///\ingroup flowalgs
deba@1792:   ///\brief Function type interface for Preflow algorithm.
deba@1792:   ///
alpar@1227:   ///Function type interface for Preflow algorithm.
alpar@1227:   ///\sa Preflow
alpar@1227:   template<class GR, class CM, class FM>
alpar@1227:   Preflow<GR,typename CM::Value,CM,FM> preflow(const GR &g,
alpar@1227: 			    typename GR::Node source,
alpar@1227: 			    typename GR::Node target,
alpar@1227: 			    const CM &cap,
alpar@1227: 			    FM &flow
alpar@1227: 			    )
alpar@1227:   {
alpar@1227:     return Preflow<GR,typename CM::Value,CM,FM>(g,source,target,cap,flow);
alpar@1227:   }
alpar@1227: 
alpar@921: } //namespace lemon
jacint@836: 
alpar@921: #endif //LEMON_PREFLOW_H