alpar@414: /* -*- mode: C++; indent-tabs-mode: nil; -*- alpar@414: * alpar@414: * This file is a part of LEMON, a generic C++ optimization library. alpar@414: * alpar@414: * Copyright (C) 2003-2008 alpar@414: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@414: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@414: * alpar@414: * Permission to use, modify and distribute this software is granted alpar@414: * provided that this copyright notice appears in all copies. For alpar@414: * precise terms see the accompanying LICENSE file. alpar@414: * alpar@414: * This software is provided "AS IS" with no warranty of any kind, alpar@414: * express or implied, and with no claim as to its suitability for any alpar@414: * purpose. alpar@414: * alpar@414: */ alpar@414: alpar@414: #ifndef LEMON_CIRCULATION_H alpar@414: #define LEMON_CIRCULATION_H alpar@414: alpar@414: #include alpar@414: #include alpar@414: alpar@414: ///\ingroup max_flow alpar@414: ///\file kpeter@417: ///\brief Push-relabel algorithm for finding a feasible circulation. alpar@414: /// alpar@414: namespace lemon { alpar@414: alpar@414: /// \brief Default traits class of Circulation class. alpar@414: /// alpar@414: /// Default traits class of Circulation class. kpeter@417: /// \tparam _Diraph Digraph type. kpeter@417: /// \tparam _LCapMap Lower bound capacity map type. kpeter@417: /// \tparam _UCapMap Upper bound capacity map type. kpeter@417: /// \tparam _DeltaMap Delta map type. kpeter@417: template alpar@414: struct CirculationDefaultTraits { alpar@414: kpeter@417: /// \brief The type of the digraph the algorithm runs on. kpeter@417: typedef _Diraph Digraph; alpar@414: alpar@414: /// \brief The type of the map that stores the circulation lower alpar@414: /// bound. alpar@414: /// alpar@414: /// The type of the map that stores the circulation lower bound. alpar@414: /// It must meet the \ref concepts::ReadMap "ReadMap" concept. alpar@414: typedef _LCapMap LCapMap; alpar@414: alpar@414: /// \brief The type of the map that stores the circulation upper alpar@414: /// bound. alpar@414: /// alpar@414: /// The type of the map that stores the circulation upper bound. alpar@414: /// It must meet the \ref concepts::ReadMap "ReadMap" concept. alpar@414: typedef _UCapMap UCapMap; alpar@414: kpeter@417: /// \brief The type of the map that stores the lower bound for kpeter@417: /// the supply of the nodes. alpar@414: /// kpeter@417: /// The type of the map that stores the lower bound for the supply kpeter@417: /// of the nodes. It must meet the \ref concepts::ReadMap "ReadMap" alpar@414: /// concept. alpar@414: typedef _DeltaMap DeltaMap; alpar@414: kpeter@417: /// \brief The type of the flow values. alpar@414: typedef typename DeltaMap::Value Value; alpar@414: kpeter@417: /// \brief The type of the map that stores the flow values. alpar@414: /// kpeter@417: /// The type of the map that stores the flow values. alpar@414: /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. alpar@414: typedef typename Digraph::template ArcMap FlowMap; alpar@414: alpar@414: /// \brief Instantiates a FlowMap. alpar@414: /// alpar@414: /// This function instantiates a \ref FlowMap. alpar@414: /// \param digraph The digraph, to which we would like to define alpar@414: /// the flow map. alpar@414: static FlowMap* createFlowMap(const Digraph& digraph) { alpar@414: return new FlowMap(digraph); alpar@414: } alpar@414: kpeter@417: /// \brief The elevator type used by the algorithm. alpar@414: /// kpeter@417: /// The elevator type used by the algorithm. alpar@414: /// alpar@414: /// \sa Elevator alpar@414: /// \sa LinkedElevator alpar@414: typedef lemon::Elevator Elevator; alpar@414: alpar@414: /// \brief Instantiates an Elevator. alpar@414: /// kpeter@417: /// This function instantiates an \ref Elevator. alpar@414: /// \param digraph The digraph, to which we would like to define alpar@414: /// the elevator. alpar@414: /// \param max_level The maximum level of the elevator. alpar@414: static Elevator* createElevator(const Digraph& digraph, int max_level) { alpar@414: return new Elevator(digraph, max_level); alpar@414: } alpar@414: alpar@414: /// \brief The tolerance used by the algorithm alpar@414: /// alpar@414: /// The tolerance used by the algorithm to handle inexact computation. alpar@414: typedef lemon::Tolerance Tolerance; alpar@414: alpar@414: }; alpar@414: kpeter@417: /** kpeter@417: \brief Push-relabel algorithm for the network circulation problem. alpar@414: alpar@414: \ingroup max_flow kpeter@417: This class implements a push-relabel algorithm for the network kpeter@417: circulation problem. kpeter@417: It is to find a feasible circulation when lower and upper bounds kpeter@417: are given for the flow values on the arcs and lower bounds kpeter@417: are given for the supply values of the nodes. kpeter@417: alpar@414: The exact formulation of this problem is the following. kpeter@417: Let \f$G=(V,A)\f$ be a digraph, kpeter@417: \f$lower, upper: A\rightarrow\mathbf{R}^+_0\f$, kpeter@417: \f$delta: V\rightarrow\mathbf{R}\f$. Find a feasible circulation kpeter@417: \f$f: A\rightarrow\mathbf{R}^+_0\f$ so that kpeter@417: \f[ \sum_{a\in\delta_{out}(v)} f(a) - \sum_{a\in\delta_{in}(v)} f(a) kpeter@417: \geq delta(v) \quad \forall v\in V, \f] kpeter@417: \f[ lower(a)\leq f(a) \leq upper(a) \quad \forall a\in A. \f] kpeter@417: \note \f$delta(v)\f$ specifies a lower bound for the supply of node kpeter@417: \f$v\f$. It can be either positive or negative, however note that kpeter@417: \f$\sum_{v\in V}delta(v)\f$ should be zero or negative in order to kpeter@417: have a feasible solution. kpeter@417: kpeter@417: \note A special case of this problem is when kpeter@417: \f$\sum_{v\in V}delta(v) = 0\f$. Then the supply of each node \f$v\f$ kpeter@417: will be \e equal \e to \f$delta(v)\f$, if a circulation can be found. kpeter@417: Thus a feasible solution for the kpeter@417: \ref min_cost_flow "minimum cost flow" problem can be calculated kpeter@417: in this way. kpeter@417: kpeter@417: \tparam _Digraph The type of the digraph the algorithm runs on. kpeter@417: \tparam _LCapMap The type of the lower bound capacity map. The default kpeter@417: map type is \ref concepts::Digraph::ArcMap "_Digraph::ArcMap". kpeter@417: \tparam _UCapMap The type of the upper bound capacity map. The default kpeter@417: map type is \c _LCapMap. kpeter@417: \tparam _DeltaMap The type of the map that stores the lower bound kpeter@417: for the supply of the nodes. The default map type is kpeter@417: \c _Digraph::ArcMap<_UCapMap::Value>. alpar@414: */ kpeter@417: #ifdef DOXYGEN kpeter@417: template< typename _Digraph, kpeter@417: typename _LCapMap, kpeter@417: typename _UCapMap, kpeter@417: typename _DeltaMap, kpeter@417: typename _Traits > kpeter@417: #else kpeter@417: template< typename _Digraph, kpeter@417: typename _LCapMap = typename _Digraph::template ArcMap, kpeter@417: typename _UCapMap = _LCapMap, kpeter@417: typename _DeltaMap = typename _Digraph:: kpeter@417: template NodeMap, kpeter@417: typename _Traits=CirculationDefaultTraits<_Digraph, _LCapMap, kpeter@417: _UCapMap, _DeltaMap> > kpeter@417: #endif alpar@414: class Circulation { kpeter@417: public: alpar@414: kpeter@417: ///The \ref CirculationDefaultTraits "traits class" of the algorithm. alpar@414: typedef _Traits Traits; kpeter@417: ///The type of the digraph the algorithm runs on. alpar@414: typedef typename Traits::Digraph Digraph; kpeter@417: ///The type of the flow values. alpar@414: typedef typename Traits::Value Value; alpar@414: kpeter@417: /// The type of the lower bound capacity map. alpar@414: typedef typename Traits::LCapMap LCapMap; kpeter@417: /// The type of the upper bound capacity map. alpar@414: typedef typename Traits::UCapMap UCapMap; kpeter@417: /// \brief The type of the map that stores the lower bound for kpeter@417: /// the supply of the nodes. alpar@414: typedef typename Traits::DeltaMap DeltaMap; kpeter@417: ///The type of the flow map. alpar@414: typedef typename Traits::FlowMap FlowMap; kpeter@417: kpeter@417: ///The type of the elevator. alpar@414: typedef typename Traits::Elevator Elevator; kpeter@417: ///The type of the tolerance. alpar@414: typedef typename Traits::Tolerance Tolerance; alpar@414: kpeter@417: private: kpeter@417: kpeter@417: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); alpar@414: alpar@414: const Digraph &_g; alpar@414: int _node_num; alpar@414: alpar@414: const LCapMap *_lo; alpar@414: const UCapMap *_up; alpar@414: const DeltaMap *_delta; alpar@414: alpar@414: FlowMap *_flow; alpar@414: bool _local_flow; alpar@414: alpar@414: Elevator* _level; alpar@414: bool _local_level; alpar@414: kpeter@417: typedef typename Digraph::template NodeMap ExcessMap; alpar@414: ExcessMap* _excess; alpar@414: alpar@414: Tolerance _tol; alpar@414: int _el; alpar@414: alpar@414: public: alpar@414: alpar@414: typedef Circulation Create; alpar@414: kpeter@417: ///\name Named Template Parameters alpar@414: alpar@414: ///@{ alpar@414: alpar@414: template alpar@416: struct SetFlowMapTraits : public Traits { alpar@414: typedef _FlowMap FlowMap; alpar@414: static FlowMap *createFlowMap(const Digraph&) { alpar@414: LEMON_ASSERT(false, "FlowMap is not initialized"); alpar@414: return 0; // ignore warnings alpar@414: } alpar@414: }; alpar@414: alpar@414: /// \brief \ref named-templ-param "Named parameter" for setting alpar@414: /// FlowMap type alpar@414: /// alpar@414: /// \ref named-templ-param "Named parameter" for setting FlowMap kpeter@417: /// type. alpar@414: template alpar@416: struct SetFlowMap alpar@414: : public Circulation > { alpar@414: typedef Circulation > Create; alpar@414: }; alpar@414: alpar@414: template alpar@416: struct SetElevatorTraits : public Traits { alpar@414: typedef _Elevator Elevator; alpar@414: static Elevator *createElevator(const Digraph&, int) { alpar@414: LEMON_ASSERT(false, "Elevator is not initialized"); alpar@414: return 0; // ignore warnings alpar@414: } alpar@414: }; alpar@414: alpar@414: /// \brief \ref named-templ-param "Named parameter" for setting alpar@414: /// Elevator type alpar@414: /// alpar@414: /// \ref named-templ-param "Named parameter" for setting Elevator kpeter@417: /// type. If this named parameter is used, then an external kpeter@417: /// elevator object must be passed to the algorithm using the kpeter@417: /// \ref elevator(Elevator&) "elevator()" function before calling kpeter@417: /// \ref run() or \ref init(). kpeter@417: /// \sa SetStandardElevator alpar@414: template alpar@416: struct SetElevator alpar@414: : public Circulation > { alpar@414: typedef Circulation > Create; alpar@414: }; alpar@414: alpar@414: template alpar@416: struct SetStandardElevatorTraits : public Traits { alpar@414: typedef _Elevator Elevator; alpar@414: static Elevator *createElevator(const Digraph& digraph, int max_level) { alpar@414: return new Elevator(digraph, max_level); alpar@414: } alpar@414: }; alpar@414: alpar@414: /// \brief \ref named-templ-param "Named parameter" for setting kpeter@417: /// Elevator type with automatic allocation alpar@414: /// alpar@414: /// \ref named-templ-param "Named parameter" for setting Elevator kpeter@417: /// type with automatic allocation. kpeter@417: /// The Elevator should have standard constructor interface to be kpeter@417: /// able to automatically created by the algorithm (i.e. the kpeter@417: /// digraph and the maximum level should be passed to it). kpeter@417: /// However an external elevator object could also be passed to the kpeter@417: /// algorithm with the \ref elevator(Elevator&) "elevator()" function kpeter@417: /// before calling \ref run() or \ref init(). kpeter@417: /// \sa SetElevator alpar@414: template alpar@416: struct SetStandardElevator alpar@414: : public Circulation > { alpar@414: typedef Circulation > Create; alpar@414: }; alpar@414: alpar@414: /// @} alpar@414: alpar@414: protected: alpar@414: alpar@414: Circulation() {} alpar@414: alpar@414: public: alpar@414: alpar@414: /// The constructor of the class. alpar@414: alpar@414: /// The constructor of the class. alpar@414: /// \param g The digraph the algorithm runs on. alpar@414: /// \param lo The lower bound capacity of the arcs. alpar@414: /// \param up The upper bound capacity of the arcs. kpeter@417: /// \param delta The lower bound for the supply of the nodes. alpar@414: Circulation(const Digraph &g,const LCapMap &lo, alpar@414: const UCapMap &up,const DeltaMap &delta) alpar@414: : _g(g), _node_num(), alpar@414: _lo(&lo),_up(&up),_delta(&delta),_flow(0),_local_flow(false), alpar@414: _level(0), _local_level(false), _excess(0), _el() {} alpar@414: kpeter@417: /// Destructor. alpar@414: ~Circulation() { alpar@414: destroyStructures(); alpar@414: } alpar@414: kpeter@417: alpar@414: private: alpar@414: alpar@414: void createStructures() { alpar@414: _node_num = _el = countNodes(_g); alpar@414: alpar@414: if (!_flow) { alpar@414: _flow = Traits::createFlowMap(_g); alpar@414: _local_flow = true; alpar@414: } alpar@414: if (!_level) { alpar@414: _level = Traits::createElevator(_g, _node_num); alpar@414: _local_level = true; alpar@414: } alpar@414: if (!_excess) { alpar@414: _excess = new ExcessMap(_g); alpar@414: } alpar@414: } alpar@414: alpar@414: void destroyStructures() { alpar@414: if (_local_flow) { alpar@414: delete _flow; alpar@414: } alpar@414: if (_local_level) { alpar@414: delete _level; alpar@414: } alpar@414: if (_excess) { alpar@414: delete _excess; alpar@414: } alpar@414: } alpar@414: alpar@414: public: alpar@414: alpar@414: /// Sets the lower bound capacity map. alpar@414: alpar@414: /// Sets the lower bound capacity map. kpeter@417: /// \return (*this) alpar@414: Circulation& lowerCapMap(const LCapMap& map) { alpar@414: _lo = ↦ alpar@414: return *this; alpar@414: } alpar@414: alpar@414: /// Sets the upper bound capacity map. alpar@414: alpar@414: /// Sets the upper bound capacity map. kpeter@417: /// \return (*this) alpar@414: Circulation& upperCapMap(const LCapMap& map) { alpar@414: _up = ↦ alpar@414: return *this; alpar@414: } alpar@414: kpeter@417: /// Sets the lower bound map for the supply of the nodes. alpar@414: kpeter@417: /// Sets the lower bound map for the supply of the nodes. kpeter@417: /// \return (*this) alpar@414: Circulation& deltaMap(const DeltaMap& map) { alpar@414: _delta = ↦ alpar@414: return *this; alpar@414: } alpar@414: kpeter@417: /// \brief Sets the flow map. kpeter@417: /// alpar@414: /// Sets the flow map. kpeter@417: /// If you don't use this function before calling \ref run() or kpeter@417: /// \ref init(), an instance will be allocated automatically. kpeter@417: /// The destructor deallocates this automatically allocated map, kpeter@417: /// of course. kpeter@417: /// \return (*this) alpar@414: Circulation& flowMap(FlowMap& map) { alpar@414: if (_local_flow) { alpar@414: delete _flow; alpar@414: _local_flow = false; alpar@414: } alpar@414: _flow = ↦ alpar@414: return *this; alpar@414: } alpar@414: kpeter@417: /// \brief Sets the elevator used by algorithm. alpar@414: /// kpeter@417: /// Sets the elevator used by algorithm. kpeter@417: /// If you don't use this function before calling \ref run() or kpeter@417: /// \ref init(), an instance will be allocated automatically. kpeter@417: /// The destructor deallocates this automatically allocated elevator, kpeter@417: /// of course. kpeter@417: /// \return (*this) alpar@414: Circulation& elevator(Elevator& elevator) { alpar@414: if (_local_level) { alpar@414: delete _level; alpar@414: _local_level = false; alpar@414: } alpar@414: _level = &elevator; alpar@414: return *this; alpar@414: } alpar@414: kpeter@417: /// \brief Returns a const reference to the elevator. alpar@414: /// kpeter@417: /// Returns a const reference to the elevator. kpeter@417: /// kpeter@417: /// \pre Either \ref run() or \ref init() must be called before kpeter@417: /// using this function. alpar@414: const Elevator& elevator() { alpar@414: return *_level; alpar@414: } alpar@414: kpeter@417: /// \brief Sets the tolerance used by algorithm. kpeter@417: /// alpar@414: /// Sets the tolerance used by algorithm. alpar@414: Circulation& tolerance(const Tolerance& tolerance) const { alpar@414: _tol = tolerance; alpar@414: return *this; alpar@414: } alpar@414: kpeter@417: /// \brief Returns a const reference to the tolerance. alpar@414: /// kpeter@417: /// Returns a const reference to the tolerance. alpar@414: const Tolerance& tolerance() const { alpar@414: return tolerance; alpar@414: } alpar@414: kpeter@417: /// \name Execution Control kpeter@417: /// The simplest way to execute the algorithm is to call \ref run().\n kpeter@417: /// If you need more control on the initial solution or the execution, kpeter@417: /// first you have to call one of the \ref init() functions, then kpeter@417: /// the \ref start() function. alpar@414: alpar@414: ///@{ alpar@414: alpar@414: /// Initializes the internal data structures. alpar@414: kpeter@417: /// Initializes the internal data structures and sets all flow values kpeter@417: /// to the lower bound. alpar@414: void init() alpar@414: { alpar@414: createStructures(); alpar@414: alpar@414: for(NodeIt n(_g);n!=INVALID;++n) { alpar@414: _excess->set(n, (*_delta)[n]); alpar@414: } alpar@414: alpar@414: for (ArcIt e(_g);e!=INVALID;++e) { alpar@414: _flow->set(e, (*_lo)[e]); alpar@414: _excess->set(_g.target(e), (*_excess)[_g.target(e)] + (*_flow)[e]); alpar@414: _excess->set(_g.source(e), (*_excess)[_g.source(e)] - (*_flow)[e]); alpar@414: } alpar@414: alpar@414: // global relabeling tested, but in general case it provides alpar@414: // worse performance for random digraphs alpar@414: _level->initStart(); alpar@414: for(NodeIt n(_g);n!=INVALID;++n) alpar@414: _level->initAddItem(n); alpar@414: _level->initFinish(); alpar@414: for(NodeIt n(_g);n!=INVALID;++n) alpar@414: if(_tol.positive((*_excess)[n])) alpar@414: _level->activate(n); alpar@414: } alpar@414: kpeter@417: /// Initializes the internal data structures using a greedy approach. alpar@414: kpeter@417: /// Initializes the internal data structures using a greedy approach kpeter@417: /// to construct the initial solution. alpar@414: void greedyInit() alpar@414: { alpar@414: createStructures(); alpar@414: alpar@414: for(NodeIt n(_g);n!=INVALID;++n) { alpar@414: _excess->set(n, (*_delta)[n]); alpar@414: } alpar@414: alpar@414: for (ArcIt e(_g);e!=INVALID;++e) { alpar@414: if (!_tol.positive((*_excess)[_g.target(e)] + (*_up)[e])) { alpar@414: _flow->set(e, (*_up)[e]); alpar@414: _excess->set(_g.target(e), (*_excess)[_g.target(e)] + (*_up)[e]); alpar@414: _excess->set(_g.source(e), (*_excess)[_g.source(e)] - (*_up)[e]); alpar@414: } else if (_tol.positive((*_excess)[_g.target(e)] + (*_lo)[e])) { alpar@414: _flow->set(e, (*_lo)[e]); alpar@414: _excess->set(_g.target(e), (*_excess)[_g.target(e)] + (*_lo)[e]); alpar@414: _excess->set(_g.source(e), (*_excess)[_g.source(e)] - (*_lo)[e]); alpar@414: } else { alpar@414: Value fc = -(*_excess)[_g.target(e)]; alpar@414: _flow->set(e, fc); alpar@414: _excess->set(_g.target(e), 0); alpar@414: _excess->set(_g.source(e), (*_excess)[_g.source(e)] - fc); alpar@414: } alpar@414: } alpar@414: alpar@414: _level->initStart(); alpar@414: for(NodeIt n(_g);n!=INVALID;++n) alpar@414: _level->initAddItem(n); alpar@414: _level->initFinish(); alpar@414: for(NodeIt n(_g);n!=INVALID;++n) alpar@414: if(_tol.positive((*_excess)[n])) alpar@414: _level->activate(n); alpar@414: } alpar@414: kpeter@417: ///Executes the algorithm alpar@414: kpeter@417: ///This function executes the algorithm. kpeter@417: /// kpeter@417: ///\return \c true if a feasible circulation is found. alpar@414: /// alpar@414: ///\sa barrier() kpeter@417: ///\sa barrierMap() alpar@414: bool start() alpar@414: { alpar@414: alpar@414: Node act; alpar@414: Node bact=INVALID; alpar@414: Node last_activated=INVALID; alpar@414: while((act=_level->highestActive())!=INVALID) { alpar@414: int actlevel=(*_level)[act]; alpar@414: int mlevel=_node_num; alpar@414: Value exc=(*_excess)[act]; alpar@414: alpar@414: for(OutArcIt e(_g,act);e!=INVALID; ++e) { alpar@414: Node v = _g.target(e); alpar@414: Value fc=(*_up)[e]-(*_flow)[e]; alpar@414: if(!_tol.positive(fc)) continue; alpar@414: if((*_level)[v]set(e, (*_flow)[e] + exc); alpar@414: _excess->set(v, (*_excess)[v] + exc); alpar@414: if(!_level->active(v) && _tol.positive((*_excess)[v])) alpar@414: _level->activate(v); alpar@414: _excess->set(act,0); alpar@414: _level->deactivate(act); alpar@414: goto next_l; alpar@414: } alpar@414: else { alpar@414: _flow->set(e, (*_up)[e]); alpar@414: _excess->set(v, (*_excess)[v] + fc); alpar@414: if(!_level->active(v) && _tol.positive((*_excess)[v])) alpar@414: _level->activate(v); alpar@414: exc-=fc; alpar@414: } alpar@414: } alpar@414: else if((*_level)[v]set(e, (*_flow)[e] - exc); alpar@414: _excess->set(v, (*_excess)[v] + exc); alpar@414: if(!_level->active(v) && _tol.positive((*_excess)[v])) alpar@414: _level->activate(v); alpar@414: _excess->set(act,0); alpar@414: _level->deactivate(act); alpar@414: goto next_l; alpar@414: } alpar@414: else { alpar@414: _flow->set(e, (*_lo)[e]); alpar@414: _excess->set(v, (*_excess)[v] + fc); alpar@414: if(!_level->active(v) && _tol.positive((*_excess)[v])) alpar@414: _level->activate(v); alpar@414: exc-=fc; alpar@414: } alpar@414: } alpar@414: else if((*_level)[v]set(act, exc); alpar@414: if(!_tol.positive(exc)) _level->deactivate(act); alpar@414: else if(mlevel==_node_num) { alpar@414: _level->liftHighestActiveToTop(); alpar@414: _el = _node_num; alpar@414: return false; alpar@414: } alpar@414: else { alpar@414: _level->liftHighestActive(mlevel+1); alpar@414: if(_level->onLevel(actlevel)==0) { alpar@414: _el = actlevel; alpar@414: return false; alpar@414: } alpar@414: } alpar@414: next_l: alpar@414: ; alpar@414: } alpar@414: return true; alpar@414: } alpar@414: kpeter@417: /// Runs the algorithm. alpar@414: kpeter@417: /// This function runs the algorithm. kpeter@417: /// kpeter@417: /// \return \c true if a feasible circulation is found. kpeter@417: /// kpeter@417: /// \note Apart from the return value, c.run() is just a shortcut of kpeter@417: /// the following code. alpar@414: /// \code kpeter@417: /// c.greedyInit(); kpeter@417: /// c.start(); alpar@414: /// \endcode alpar@414: bool run() { alpar@414: greedyInit(); alpar@414: return start(); alpar@414: } alpar@414: alpar@414: /// @} alpar@414: alpar@414: /// \name Query Functions kpeter@417: /// The results of the circulation algorithm can be obtained using kpeter@417: /// these functions.\n kpeter@417: /// Either \ref run() or \ref start() should be called before kpeter@417: /// using them. alpar@414: alpar@414: ///@{ alpar@414: kpeter@417: /// \brief Returns the flow on the given arc. kpeter@417: /// kpeter@417: /// Returns the flow on the given arc. kpeter@417: /// kpeter@417: /// \pre Either \ref run() or \ref init() must be called before kpeter@417: /// using this function. kpeter@417: Value flow(const Arc& arc) const { kpeter@417: return (*_flow)[arc]; kpeter@417: } kpeter@417: kpeter@417: /// \brief Returns a const reference to the flow map. kpeter@417: /// kpeter@417: /// Returns a const reference to the arc map storing the found flow. kpeter@417: /// kpeter@417: /// \pre Either \ref run() or \ref init() must be called before kpeter@417: /// using this function. kpeter@417: const FlowMap& flowMap() { kpeter@417: return *_flow; kpeter@417: } kpeter@417: alpar@414: /** kpeter@417: \brief Returns \c true if the given node is in a barrier. kpeter@417: alpar@414: Barrier is a set \e B of nodes for which kpeter@417: kpeter@417: \f[ \sum_{a\in\delta_{out}(B)} upper(a) - kpeter@417: \sum_{a\in\delta_{in}(B)} lower(a) < \sum_{v\in B}delta(v) \f] kpeter@417: kpeter@417: holds. The existence of a set with this property prooves that a kpeter@417: feasible circualtion cannot exist. kpeter@417: kpeter@417: This function returns \c true if the given node is in the found kpeter@417: barrier. If a feasible circulation is found, the function kpeter@417: gives back \c false for every node. kpeter@417: kpeter@417: \pre Either \ref run() or \ref init() must be called before kpeter@417: using this function. kpeter@417: kpeter@417: \sa barrierMap() alpar@414: \sa checkBarrier() alpar@414: */ kpeter@417: bool barrier(const Node& node) kpeter@417: { kpeter@417: return (*_level)[node] >= _el; kpeter@417: } kpeter@417: kpeter@417: /// \brief Gives back a barrier. kpeter@417: /// kpeter@417: /// This function sets \c bar to the characteristic vector of the kpeter@417: /// found barrier. \c bar should be a \ref concepts::WriteMap "writable" kpeter@417: /// node map with \c bool (or convertible) value type. kpeter@417: /// kpeter@417: /// If a feasible circulation is found, the function gives back an kpeter@417: /// empty set, so \c bar[v] will be \c false for all nodes \c v. kpeter@417: /// kpeter@417: /// \note This function calls \ref barrier() for each node, kpeter@417: /// so it runs in \f$O(n)\f$ time. kpeter@417: /// kpeter@417: /// \pre Either \ref run() or \ref init() must be called before kpeter@417: /// using this function. kpeter@417: /// kpeter@417: /// \sa barrier() kpeter@417: /// \sa checkBarrier() kpeter@417: template kpeter@417: void barrierMap(BarrierMap &bar) alpar@414: { alpar@414: for(NodeIt n(_g);n!=INVALID;++n) alpar@414: bar.set(n, (*_level)[n] >= _el); alpar@414: } alpar@414: alpar@414: /// @} alpar@414: alpar@414: /// \name Checker Functions kpeter@417: /// The feasibility of the results can be checked using kpeter@417: /// these functions.\n kpeter@417: /// Either \ref run() or \ref start() should be called before kpeter@417: /// using them. alpar@414: alpar@414: ///@{ alpar@414: kpeter@417: ///Check if the found flow is a feasible circulation kpeter@417: kpeter@417: ///Check if the found flow is a feasible circulation, kpeter@417: /// alpar@414: bool checkFlow() { alpar@414: for(ArcIt e(_g);e!=INVALID;++e) alpar@414: if((*_flow)[e]<(*_lo)[e]||(*_flow)[e]>(*_up)[e]) return false; alpar@414: for(NodeIt n(_g);n!=INVALID;++n) alpar@414: { alpar@414: Value dif=-(*_delta)[n]; alpar@414: for(InArcIt e(_g,n);e!=INVALID;++e) dif-=(*_flow)[e]; alpar@414: for(OutArcIt e(_g,n);e!=INVALID;++e) dif+=(*_flow)[e]; alpar@414: if(_tol.negative(dif)) return false; alpar@414: } alpar@414: return true; alpar@414: } alpar@414: alpar@414: ///Check whether or not the last execution provides a barrier alpar@414: kpeter@417: ///Check whether or not the last execution provides a barrier. alpar@414: ///\sa barrier() kpeter@417: ///\sa barrierMap() alpar@414: bool checkBarrier() alpar@414: { alpar@414: Value delta=0; alpar@414: for(NodeIt n(_g);n!=INVALID;++n) alpar@414: if(barrier(n)) alpar@414: delta-=(*_delta)[n]; alpar@414: for(ArcIt e(_g);e!=INVALID;++e) alpar@414: { alpar@414: Node s=_g.source(e); alpar@414: Node t=_g.target(e); alpar@414: if(barrier(s)&&!barrier(t)) delta+=(*_up)[e]; alpar@414: else if(barrier(t)&&!barrier(s)) delta-=(*_lo)[e]; alpar@414: } alpar@414: return _tol.negative(delta); alpar@414: } alpar@414: alpar@414: /// @} alpar@414: alpar@414: }; alpar@414: alpar@414: } alpar@414: alpar@414: #endif