kpeter@808: /* -*- C++ -*- kpeter@808: * kpeter@808: * This file is a part of LEMON, a generic C++ optimization library kpeter@808: * kpeter@808: * Copyright (C) 2003-2008 kpeter@808: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport kpeter@808: * (Egervary Research Group on Combinatorial Optimization, EGRES). kpeter@808: * kpeter@808: * Permission to use, modify and distribute this software is granted kpeter@808: * provided that this copyright notice appears in all copies. For kpeter@808: * precise terms see the accompanying LICENSE file. kpeter@808: * kpeter@808: * This software is provided "AS IS" with no warranty of any kind, kpeter@808: * express or implied, and with no claim as to its suitability for any kpeter@808: * purpose. kpeter@808: * kpeter@808: */ kpeter@808: kpeter@808: #ifndef LEMON_COST_SCALING_H kpeter@808: #define LEMON_COST_SCALING_H kpeter@808: kpeter@808: /// \ingroup min_cost_flow_algs kpeter@808: /// \file kpeter@808: /// \brief Cost scaling algorithm for finding a minimum cost flow. kpeter@808: kpeter@808: #include kpeter@808: #include kpeter@808: #include kpeter@808: kpeter@808: #include kpeter@808: #include kpeter@808: #include kpeter@809: #include kpeter@808: #include kpeter@808: #include kpeter@808: kpeter@808: namespace lemon { kpeter@808: kpeter@809: /// \brief Default traits class of CostScaling algorithm. kpeter@809: /// kpeter@809: /// Default traits class of CostScaling algorithm. kpeter@809: /// \tparam GR Digraph type. kpeter@812: /// \tparam V The number type used for flow amounts, capacity bounds kpeter@809: /// and supply values. By default it is \c int. kpeter@812: /// \tparam C The number type used for costs and potentials. kpeter@809: /// By default it is the same as \c V. kpeter@809: #ifdef DOXYGEN kpeter@809: template kpeter@809: #else kpeter@809: template < typename GR, typename V = int, typename C = V, kpeter@809: bool integer = std::numeric_limits::is_integer > kpeter@809: #endif kpeter@809: struct CostScalingDefaultTraits kpeter@809: { kpeter@809: /// The type of the digraph kpeter@809: typedef GR Digraph; kpeter@809: /// The type of the flow amounts, capacity bounds and supply values kpeter@809: typedef V Value; kpeter@809: /// The type of the arc costs kpeter@809: typedef C Cost; kpeter@809: kpeter@809: /// \brief The large cost type used for internal computations kpeter@809: /// kpeter@809: /// The large cost type used for internal computations. kpeter@809: /// It is \c long \c long if the \c Cost type is integer, kpeter@809: /// otherwise it is \c double. kpeter@809: /// \c Cost must be convertible to \c LargeCost. kpeter@809: typedef double LargeCost; kpeter@809: }; kpeter@809: kpeter@809: // Default traits class for integer cost types kpeter@809: template kpeter@809: struct CostScalingDefaultTraits kpeter@809: { kpeter@809: typedef GR Digraph; kpeter@809: typedef V Value; kpeter@809: typedef C Cost; kpeter@809: #ifdef LEMON_HAVE_LONG_LONG kpeter@809: typedef long long LargeCost; kpeter@809: #else kpeter@809: typedef long LargeCost; kpeter@809: #endif kpeter@809: }; kpeter@809: kpeter@809: kpeter@808: /// \addtogroup min_cost_flow_algs kpeter@808: /// @{ kpeter@808: kpeter@809: /// \brief Implementation of the Cost Scaling algorithm for kpeter@809: /// finding a \ref min_cost_flow "minimum cost flow". kpeter@808: /// kpeter@809: /// \ref CostScaling implements a cost scaling algorithm that performs kpeter@813: /// push/augment and relabel operations for finding a \ref min_cost_flow kpeter@813: /// "minimum cost flow" \ref amo93networkflows, \ref goldberg90approximation, kpeter@813: /// \ref goldberg97efficient, \ref bunnagel98efficient. kpeter@813: /// It is a highly efficient primal-dual solution method, which kpeter@809: /// can be viewed as the generalization of the \ref Preflow kpeter@809: /// "preflow push-relabel" algorithm for the maximum flow problem. kpeter@808: /// kpeter@809: /// Most of the parameters of the problem (except for the digraph) kpeter@809: /// can be given using separate functions, and the algorithm can be kpeter@809: /// executed using the \ref run() function. If some parameters are not kpeter@809: /// specified, then default values will be used. kpeter@808: /// kpeter@809: /// \tparam GR The digraph type the algorithm runs on. kpeter@812: /// \tparam V The number type used for flow amounts, capacity bounds kpeter@809: /// and supply values in the algorithm. By default it is \c int. kpeter@812: /// \tparam C The number type used for costs and potentials in the kpeter@809: /// algorithm. By default it is the same as \c V. kpeter@808: /// kpeter@812: /// \warning Both number types must be signed and all input data must kpeter@809: /// be integer. kpeter@809: /// \warning This algorithm does not support negative costs for such kpeter@809: /// arcs that have infinite upper bound. kpeter@810: /// kpeter@810: /// \note %CostScaling provides three different internal methods, kpeter@810: /// from which the most efficient one is used by default. kpeter@810: /// For more information, see \ref Method. kpeter@809: #ifdef DOXYGEN kpeter@809: template kpeter@809: #else kpeter@809: template < typename GR, typename V = int, typename C = V, kpeter@809: typename TR = CostScalingDefaultTraits > kpeter@809: #endif kpeter@808: class CostScaling kpeter@808: { kpeter@809: public: kpeter@808: kpeter@809: /// The type of the digraph kpeter@809: typedef typename TR::Digraph Digraph; kpeter@809: /// The type of the flow amounts, capacity bounds and supply values kpeter@809: typedef typename TR::Value Value; kpeter@809: /// The type of the arc costs kpeter@809: typedef typename TR::Cost Cost; kpeter@808: kpeter@809: /// \brief The large cost type kpeter@809: /// kpeter@809: /// The large cost type used for internal computations. kpeter@809: /// Using the \ref CostScalingDefaultTraits "default traits class", kpeter@809: /// it is \c long \c long if the \c Cost type is integer, kpeter@809: /// otherwise it is \c double. kpeter@809: typedef typename TR::LargeCost LargeCost; kpeter@808: kpeter@809: /// The \ref CostScalingDefaultTraits "traits class" of the algorithm kpeter@809: typedef TR Traits; kpeter@808: kpeter@808: public: kpeter@808: kpeter@809: /// \brief Problem type constants for the \c run() function. kpeter@809: /// kpeter@809: /// Enum type containing the problem type constants that can be kpeter@809: /// returned by the \ref run() function of the algorithm. kpeter@809: enum ProblemType { kpeter@809: /// The problem has no feasible solution (flow). kpeter@809: INFEASIBLE, kpeter@809: /// The problem has optimal solution (i.e. it is feasible and kpeter@809: /// bounded), and the algorithm has found optimal flow and node kpeter@809: /// potentials (primal and dual solutions). kpeter@809: OPTIMAL, kpeter@809: /// The digraph contains an arc of negative cost and infinite kpeter@809: /// upper bound. It means that the objective function is unbounded kpeter@812: /// on that arc, however, note that it could actually be bounded kpeter@809: /// over the feasible flows, but this algroithm cannot handle kpeter@809: /// these cases. kpeter@809: UNBOUNDED kpeter@809: }; kpeter@808: kpeter@810: /// \brief Constants for selecting the internal method. kpeter@810: /// kpeter@810: /// Enum type containing constants for selecting the internal method kpeter@810: /// for the \ref run() function. kpeter@810: /// kpeter@810: /// \ref CostScaling provides three internal methods that differ mainly kpeter@810: /// in their base operations, which are used in conjunction with the kpeter@810: /// relabel operation. kpeter@810: /// By default, the so called \ref PARTIAL_AUGMENT kpeter@810: /// "Partial Augment-Relabel" method is used, which proved to be kpeter@810: /// the most efficient and the most robust on various test inputs. kpeter@810: /// However, the other methods can be selected using the \ref run() kpeter@810: /// function with the proper parameter. kpeter@810: enum Method { kpeter@810: /// Local push operations are used, i.e. flow is moved only on one kpeter@810: /// admissible arc at once. kpeter@810: PUSH, kpeter@810: /// Augment operations are used, i.e. flow is moved on admissible kpeter@810: /// paths from a node with excess to a node with deficit. kpeter@810: AUGMENT, kpeter@810: /// Partial augment operations are used, i.e. flow is moved on kpeter@810: /// admissible paths started from a node with excess, but the kpeter@810: /// lengths of these paths are limited. This method can be viewed kpeter@810: /// as a combined version of the previous two operations. kpeter@810: PARTIAL_AUGMENT kpeter@810: }; kpeter@810: kpeter@808: private: kpeter@808: kpeter@809: TEMPLATE_DIGRAPH_TYPEDEFS(GR); kpeter@808: kpeter@809: typedef std::vector IntVector; kpeter@809: typedef std::vector BoolVector; kpeter@809: typedef std::vector ValueVector; kpeter@809: typedef std::vector CostVector; kpeter@809: typedef std::vector LargeCostVector; kpeter@808: kpeter@809: private: kpeter@809: kpeter@809: template kpeter@820: class StaticVectorMap { kpeter@808: public: kpeter@809: typedef KT Key; kpeter@809: typedef VT Value; kpeter@809: kpeter@820: StaticVectorMap(std::vector& v) : _v(v) {} kpeter@809: kpeter@809: const Value& operator[](const Key& key) const { kpeter@809: return _v[StaticDigraph::id(key)]; kpeter@808: } kpeter@808: kpeter@809: Value& operator[](const Key& key) { kpeter@809: return _v[StaticDigraph::id(key)]; kpeter@809: } kpeter@809: kpeter@809: void set(const Key& key, const Value& val) { kpeter@809: _v[StaticDigraph::id(key)] = val; kpeter@808: } kpeter@808: kpeter@809: private: kpeter@809: std::vector& _v; kpeter@809: }; kpeter@809: kpeter@820: typedef StaticVectorMap LargeCostNodeMap; kpeter@820: typedef StaticVectorMap LargeCostArcMap; kpeter@808: kpeter@808: private: kpeter@808: kpeter@809: // Data related to the underlying digraph kpeter@809: const GR &_graph; kpeter@809: int _node_num; kpeter@809: int _arc_num; kpeter@809: int _res_node_num; kpeter@809: int _res_arc_num; kpeter@809: int _root; kpeter@808: kpeter@809: // Parameters of the problem kpeter@809: bool _have_lower; kpeter@809: Value _sum_supply; kpeter@808: kpeter@809: // Data structures for storing the digraph kpeter@809: IntNodeMap _node_id; kpeter@809: IntArcMap _arc_idf; kpeter@809: IntArcMap _arc_idb; kpeter@809: IntVector _first_out; kpeter@809: BoolVector _forward; kpeter@809: IntVector _source; kpeter@809: IntVector _target; kpeter@809: IntVector _reverse; kpeter@809: kpeter@809: // Node and arc data kpeter@809: ValueVector _lower; kpeter@809: ValueVector _upper; kpeter@809: CostVector _scost; kpeter@809: ValueVector _supply; kpeter@809: kpeter@809: ValueVector _res_cap; kpeter@809: LargeCostVector _cost; kpeter@809: LargeCostVector _pi; kpeter@809: ValueVector _excess; kpeter@809: IntVector _next_out; kpeter@809: std::deque _active_nodes; kpeter@809: kpeter@809: // Data for scaling kpeter@809: LargeCost _epsilon; kpeter@808: int _alpha; kpeter@808: kpeter@809: // Data for a StaticDigraph structure kpeter@809: typedef std::pair IntPair; kpeter@809: StaticDigraph _sgr; kpeter@809: std::vector _arc_vec; kpeter@809: std::vector _cost_vec; kpeter@809: LargeCostArcMap _cost_map; kpeter@809: LargeCostNodeMap _pi_map; kpeter@809: kpeter@809: public: kpeter@809: kpeter@809: /// \brief Constant for infinite upper bounds (capacities). kpeter@809: /// kpeter@809: /// Constant for infinite upper bounds (capacities). kpeter@809: /// It is \c std::numeric_limits::infinity() if available, kpeter@809: /// \c std::numeric_limits::max() otherwise. kpeter@809: const Value INF; kpeter@809: kpeter@808: public: kpeter@808: kpeter@809: /// \name Named Template Parameters kpeter@809: /// @{ kpeter@809: kpeter@809: template kpeter@809: struct SetLargeCostTraits : public Traits { kpeter@809: typedef T LargeCost; kpeter@809: }; kpeter@809: kpeter@809: /// \brief \ref named-templ-param "Named parameter" for setting kpeter@809: /// \c LargeCost type. kpeter@808: /// kpeter@809: /// \ref named-templ-param "Named parameter" for setting \c LargeCost kpeter@809: /// type, which is used for internal computations in the algorithm. kpeter@809: /// \c Cost must be convertible to \c LargeCost. kpeter@809: template kpeter@809: struct SetLargeCost kpeter@809: : public CostScaling > { kpeter@809: typedef CostScaling > Create; kpeter@809: }; kpeter@809: kpeter@809: /// @} kpeter@809: kpeter@809: public: kpeter@809: kpeter@809: /// \brief Constructor. kpeter@808: /// kpeter@809: /// The constructor of the class. kpeter@809: /// kpeter@809: /// \param graph The digraph the algorithm runs on. kpeter@809: CostScaling(const GR& graph) : kpeter@809: _graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph), kpeter@809: _cost_map(_cost_vec), _pi_map(_pi), kpeter@809: INF(std::numeric_limits::has_infinity ? kpeter@809: std::numeric_limits::infinity() : kpeter@809: std::numeric_limits::max()) kpeter@808: { kpeter@812: // Check the number types kpeter@809: LEMON_ASSERT(std::numeric_limits::is_signed, kpeter@809: "The flow type of CostScaling must be signed"); kpeter@809: LEMON_ASSERT(std::numeric_limits::is_signed, kpeter@809: "The cost type of CostScaling must be signed"); kpeter@809: kpeter@809: // Resize vectors kpeter@809: _node_num = countNodes(_graph); kpeter@809: _arc_num = countArcs(_graph); kpeter@809: _res_node_num = _node_num + 1; kpeter@809: _res_arc_num = 2 * (_arc_num + _node_num); kpeter@809: _root = _node_num; kpeter@809: kpeter@809: _first_out.resize(_res_node_num + 1); kpeter@809: _forward.resize(_res_arc_num); kpeter@809: _source.resize(_res_arc_num); kpeter@809: _target.resize(_res_arc_num); kpeter@809: _reverse.resize(_res_arc_num); kpeter@809: kpeter@809: _lower.resize(_res_arc_num); kpeter@809: _upper.resize(_res_arc_num); kpeter@809: _scost.resize(_res_arc_num); kpeter@809: _supply.resize(_res_node_num); kpeter@808: kpeter@809: _res_cap.resize(_res_arc_num); kpeter@809: _cost.resize(_res_arc_num); kpeter@809: _pi.resize(_res_node_num); kpeter@809: _excess.resize(_res_node_num); kpeter@809: _next_out.resize(_res_node_num); kpeter@808: kpeter@809: _arc_vec.reserve(_res_arc_num); kpeter@809: _cost_vec.reserve(_res_arc_num); kpeter@809: kpeter@809: // Copy the graph kpeter@809: int i = 0, j = 0, k = 2 * _arc_num + _node_num; kpeter@809: for (NodeIt n(_graph); n != INVALID; ++n, ++i) { kpeter@809: _node_id[n] = i; kpeter@809: } kpeter@809: i = 0; kpeter@809: for (NodeIt n(_graph); n != INVALID; ++n, ++i) { kpeter@809: _first_out[i] = j; kpeter@809: for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) { kpeter@809: _arc_idf[a] = j; kpeter@809: _forward[j] = true; kpeter@809: _source[j] = i; kpeter@809: _target[j] = _node_id[_graph.runningNode(a)]; kpeter@808: } kpeter@809: for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) { kpeter@809: _arc_idb[a] = j; kpeter@809: _forward[j] = false; kpeter@809: _source[j] = i; kpeter@809: _target[j] = _node_id[_graph.runningNode(a)]; kpeter@809: } kpeter@809: _forward[j] = false; kpeter@809: _source[j] = i; kpeter@809: _target[j] = _root; kpeter@809: _reverse[j] = k; kpeter@809: _forward[k] = true; kpeter@809: _source[k] = _root; kpeter@809: _target[k] = i; kpeter@809: _reverse[k] = j; kpeter@809: ++j; ++k; kpeter@808: } kpeter@809: _first_out[i] = j; kpeter@809: _first_out[_res_node_num] = k; kpeter@809: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@809: int fi = _arc_idf[a]; kpeter@809: int bi = _arc_idb[a]; kpeter@809: _reverse[fi] = bi; kpeter@809: _reverse[bi] = fi; kpeter@809: } kpeter@809: kpeter@809: // Reset parameters kpeter@809: reset(); kpeter@808: } kpeter@808: kpeter@809: /// \name Parameters kpeter@809: /// The parameters of the algorithm can be specified using these kpeter@809: /// functions. kpeter@809: kpeter@809: /// @{ kpeter@809: kpeter@809: /// \brief Set the lower bounds on the arcs. kpeter@808: /// kpeter@809: /// This function sets the lower bounds on the arcs. kpeter@809: /// If it is not used before calling \ref run(), the lower bounds kpeter@809: /// will be set to zero on all arcs. kpeter@808: /// kpeter@809: /// \param map An arc map storing the lower bounds. kpeter@809: /// Its \c Value type must be convertible to the \c Value type kpeter@809: /// of the algorithm. kpeter@809: /// kpeter@809: /// \return (*this) kpeter@809: template kpeter@809: CostScaling& lowerMap(const LowerMap& map) { kpeter@809: _have_lower = true; kpeter@809: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@809: _lower[_arc_idf[a]] = map[a]; kpeter@809: _lower[_arc_idb[a]] = map[a]; kpeter@808: } kpeter@808: return *this; kpeter@808: } kpeter@808: kpeter@809: /// \brief Set the upper bounds (capacities) on the arcs. kpeter@808: /// kpeter@809: /// This function sets the upper bounds (capacities) on the arcs. kpeter@809: /// If it is not used before calling \ref run(), the upper bounds kpeter@809: /// will be set to \ref INF on all arcs (i.e. the flow value will be kpeter@812: /// unbounded from above). kpeter@808: /// kpeter@809: /// \param map An arc map storing the upper bounds. kpeter@809: /// Its \c Value type must be convertible to the \c Value type kpeter@809: /// of the algorithm. kpeter@809: /// kpeter@809: /// \return (*this) kpeter@809: template kpeter@809: CostScaling& upperMap(const UpperMap& map) { kpeter@809: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@809: _upper[_arc_idf[a]] = map[a]; kpeter@808: } kpeter@808: return *this; kpeter@808: } kpeter@808: kpeter@809: /// \brief Set the costs of the arcs. kpeter@809: /// kpeter@809: /// This function sets the costs of the arcs. kpeter@809: /// If it is not used before calling \ref run(), the costs kpeter@809: /// will be set to \c 1 on all arcs. kpeter@809: /// kpeter@809: /// \param map An arc map storing the costs. kpeter@809: /// Its \c Value type must be convertible to the \c Cost type kpeter@809: /// of the algorithm. kpeter@809: /// kpeter@809: /// \return (*this) kpeter@809: template kpeter@809: CostScaling& costMap(const CostMap& map) { kpeter@809: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@809: _scost[_arc_idf[a]] = map[a]; kpeter@809: _scost[_arc_idb[a]] = -map[a]; kpeter@809: } kpeter@809: return *this; kpeter@809: } kpeter@809: kpeter@809: /// \brief Set the supply values of the nodes. kpeter@809: /// kpeter@809: /// This function sets the supply values of the nodes. kpeter@809: /// If neither this function nor \ref stSupply() is used before kpeter@809: /// calling \ref run(), the supply of each node will be set to zero. kpeter@809: /// kpeter@809: /// \param map A node map storing the supply values. kpeter@809: /// Its \c Value type must be convertible to the \c Value type kpeter@809: /// of the algorithm. kpeter@809: /// kpeter@809: /// \return (*this) kpeter@809: template kpeter@809: CostScaling& supplyMap(const SupplyMap& map) { kpeter@809: for (NodeIt n(_graph); n != INVALID; ++n) { kpeter@809: _supply[_node_id[n]] = map[n]; kpeter@809: } kpeter@809: return *this; kpeter@809: } kpeter@809: kpeter@809: /// \brief Set single source and target nodes and a supply value. kpeter@809: /// kpeter@809: /// This function sets a single source node and a single target node kpeter@809: /// and the required flow value. kpeter@809: /// If neither this function nor \ref supplyMap() is used before kpeter@809: /// calling \ref run(), the supply of each node will be set to zero. kpeter@809: /// kpeter@809: /// Using this function has the same effect as using \ref supplyMap() kpeter@809: /// with such a map in which \c k is assigned to \c s, \c -k is kpeter@809: /// assigned to \c t and all other nodes have zero supply value. kpeter@809: /// kpeter@809: /// \param s The source node. kpeter@809: /// \param t The target node. kpeter@809: /// \param k The required amount of flow from node \c s to node \c t kpeter@809: /// (i.e. the supply of \c s and the demand of \c t). kpeter@809: /// kpeter@809: /// \return (*this) kpeter@809: CostScaling& stSupply(const Node& s, const Node& t, Value k) { kpeter@809: for (int i = 0; i != _res_node_num; ++i) { kpeter@809: _supply[i] = 0; kpeter@809: } kpeter@809: _supply[_node_id[s]] = k; kpeter@809: _supply[_node_id[t]] = -k; kpeter@809: return *this; kpeter@809: } kpeter@809: kpeter@809: /// @} kpeter@809: kpeter@808: /// \name Execution control kpeter@809: /// The algorithm can be executed using \ref run(). kpeter@808: kpeter@808: /// @{ kpeter@808: kpeter@808: /// \brief Run the algorithm. kpeter@808: /// kpeter@809: /// This function runs the algorithm. kpeter@809: /// The paramters can be specified using functions \ref lowerMap(), kpeter@809: /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). kpeter@809: /// For example, kpeter@809: /// \code kpeter@809: /// CostScaling cs(graph); kpeter@809: /// cs.lowerMap(lower).upperMap(upper).costMap(cost) kpeter@809: /// .supplyMap(sup).run(); kpeter@809: /// \endcode kpeter@809: /// kpeter@809: /// This function can be called more than once. All the parameters kpeter@809: /// that have been given are kept for the next call, unless kpeter@809: /// \ref reset() is called, thus only the modified parameters kpeter@809: /// have to be set again. See \ref reset() for examples. kpeter@810: /// However, the underlying digraph must not be modified after this kpeter@810: /// class have been constructed, since it copies and extends the graph. kpeter@808: /// kpeter@810: /// \param method The internal method that will be used in the kpeter@810: /// algorithm. For more information, see \ref Method. kpeter@810: /// \param factor The cost scaling factor. It must be larger than one. kpeter@808: /// kpeter@809: /// \return \c INFEASIBLE if no feasible flow exists, kpeter@809: /// \n \c OPTIMAL if the problem has optimal solution kpeter@809: /// (i.e. it is feasible and bounded), and the algorithm has found kpeter@809: /// optimal flow and node potentials (primal and dual solutions), kpeter@809: /// \n \c UNBOUNDED if the digraph contains an arc of negative cost kpeter@809: /// and infinite upper bound. It means that the objective function kpeter@812: /// is unbounded on that arc, however, note that it could actually be kpeter@809: /// bounded over the feasible flows, but this algroithm cannot handle kpeter@809: /// these cases. kpeter@809: /// kpeter@810: /// \see ProblemType, Method kpeter@810: ProblemType run(Method method = PARTIAL_AUGMENT, int factor = 8) { kpeter@810: _alpha = factor; kpeter@809: ProblemType pt = init(); kpeter@809: if (pt != OPTIMAL) return pt; kpeter@810: start(method); kpeter@809: return OPTIMAL; kpeter@809: } kpeter@809: kpeter@809: /// \brief Reset all the parameters that have been given before. kpeter@809: /// kpeter@809: /// This function resets all the paramaters that have been given kpeter@809: /// before using functions \ref lowerMap(), \ref upperMap(), kpeter@809: /// \ref costMap(), \ref supplyMap(), \ref stSupply(). kpeter@809: /// kpeter@809: /// It is useful for multiple run() calls. If this function is not kpeter@809: /// used, all the parameters given before are kept for the next kpeter@809: /// \ref run() call. kpeter@812: /// However, the underlying digraph must not be modified after this kpeter@809: /// class have been constructed, since it copies and extends the graph. kpeter@809: /// kpeter@809: /// For example, kpeter@809: /// \code kpeter@809: /// CostScaling cs(graph); kpeter@809: /// kpeter@809: /// // First run kpeter@809: /// cs.lowerMap(lower).upperMap(upper).costMap(cost) kpeter@809: /// .supplyMap(sup).run(); kpeter@809: /// kpeter@809: /// // Run again with modified cost map (reset() is not called, kpeter@809: /// // so only the cost map have to be set again) kpeter@809: /// cost[e] += 100; kpeter@809: /// cs.costMap(cost).run(); kpeter@809: /// kpeter@809: /// // Run again from scratch using reset() kpeter@809: /// // (the lower bounds will be set to zero on all arcs) kpeter@809: /// cs.reset(); kpeter@809: /// cs.upperMap(capacity).costMap(cost) kpeter@809: /// .supplyMap(sup).run(); kpeter@809: /// \endcode kpeter@809: /// kpeter@809: /// \return (*this) kpeter@809: CostScaling& reset() { kpeter@809: for (int i = 0; i != _res_node_num; ++i) { kpeter@809: _supply[i] = 0; kpeter@808: } kpeter@809: int limit = _first_out[_root]; kpeter@809: for (int j = 0; j != limit; ++j) { kpeter@809: _lower[j] = 0; kpeter@809: _upper[j] = INF; kpeter@809: _scost[j] = _forward[j] ? 1 : -1; kpeter@809: } kpeter@809: for (int j = limit; j != _res_arc_num; ++j) { kpeter@809: _lower[j] = 0; kpeter@809: _upper[j] = INF; kpeter@809: _scost[j] = 0; kpeter@809: _scost[_reverse[j]] = 0; kpeter@809: } kpeter@809: _have_lower = false; kpeter@809: return *this; kpeter@808: } kpeter@808: kpeter@808: /// @} kpeter@808: kpeter@808: /// \name Query Functions kpeter@809: /// The results of the algorithm can be obtained using these kpeter@808: /// functions.\n kpeter@809: /// The \ref run() function must be called before using them. kpeter@808: kpeter@808: /// @{ kpeter@808: kpeter@809: /// \brief Return the total cost of the found flow. kpeter@808: /// kpeter@809: /// This function returns the total cost of the found flow. kpeter@809: /// Its complexity is O(e). kpeter@809: /// kpeter@809: /// \note The return type of the function can be specified as a kpeter@809: /// template parameter. For example, kpeter@809: /// \code kpeter@809: /// cs.totalCost(); kpeter@809: /// \endcode kpeter@809: /// It is useful if the total cost cannot be stored in the \c Cost kpeter@809: /// type of the algorithm, which is the default return type of the kpeter@809: /// function. kpeter@808: /// kpeter@808: /// \pre \ref run() must be called before using this function. kpeter@809: template kpeter@809: Number totalCost() const { kpeter@809: Number c = 0; kpeter@809: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@809: int i = _arc_idb[a]; kpeter@809: c += static_cast(_res_cap[i]) * kpeter@809: (-static_cast(_scost[i])); kpeter@809: } kpeter@809: return c; kpeter@808: } kpeter@808: kpeter@809: #ifndef DOXYGEN kpeter@809: Cost totalCost() const { kpeter@809: return totalCost(); kpeter@808: } kpeter@809: #endif kpeter@808: kpeter@808: /// \brief Return the flow on the given arc. kpeter@808: /// kpeter@809: /// This function returns the flow on the given arc. kpeter@808: /// kpeter@808: /// \pre \ref run() must be called before using this function. kpeter@809: Value flow(const Arc& a) const { kpeter@809: return _res_cap[_arc_idb[a]]; kpeter@808: } kpeter@808: kpeter@809: /// \brief Return the flow map (the primal solution). kpeter@808: /// kpeter@809: /// This function copies the flow value on each arc into the given kpeter@809: /// map. The \c Value type of the algorithm must be convertible to kpeter@809: /// the \c Value type of the map. kpeter@808: /// kpeter@808: /// \pre \ref run() must be called before using this function. kpeter@809: template kpeter@809: void flowMap(FlowMap &map) const { kpeter@809: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@809: map.set(a, _res_cap[_arc_idb[a]]); kpeter@809: } kpeter@808: } kpeter@808: kpeter@809: /// \brief Return the potential (dual value) of the given node. kpeter@808: /// kpeter@809: /// This function returns the potential (dual value) of the kpeter@809: /// given node. kpeter@808: /// kpeter@808: /// \pre \ref run() must be called before using this function. kpeter@809: Cost potential(const Node& n) const { kpeter@809: return static_cast(_pi[_node_id[n]]); kpeter@809: } kpeter@809: kpeter@809: /// \brief Return the potential map (the dual solution). kpeter@809: /// kpeter@809: /// This function copies the potential (dual value) of each node kpeter@809: /// into the given map. kpeter@809: /// The \c Cost type of the algorithm must be convertible to the kpeter@809: /// \c Value type of the map. kpeter@809: /// kpeter@809: /// \pre \ref run() must be called before using this function. kpeter@809: template kpeter@809: void potentialMap(PotentialMap &map) const { kpeter@809: for (NodeIt n(_graph); n != INVALID; ++n) { kpeter@809: map.set(n, static_cast(_pi[_node_id[n]])); kpeter@809: } kpeter@808: } kpeter@808: kpeter@808: /// @} kpeter@808: kpeter@808: private: kpeter@808: kpeter@809: // Initialize the algorithm kpeter@809: ProblemType init() { kpeter@821: if (_res_node_num <= 1) return INFEASIBLE; kpeter@809: kpeter@809: // Check the sum of supply values kpeter@809: _sum_supply = 0; kpeter@809: for (int i = 0; i != _root; ++i) { kpeter@809: _sum_supply += _supply[i]; kpeter@808: } kpeter@809: if (_sum_supply > 0) return INFEASIBLE; kpeter@809: kpeter@809: kpeter@809: // Initialize vectors kpeter@809: for (int i = 0; i != _res_node_num; ++i) { kpeter@809: _pi[i] = 0; kpeter@809: _excess[i] = _supply[i]; kpeter@809: } kpeter@809: kpeter@809: // Remove infinite upper bounds and check negative arcs kpeter@809: const Value MAX = std::numeric_limits::max(); kpeter@809: int last_out; kpeter@809: if (_have_lower) { kpeter@809: for (int i = 0; i != _root; ++i) { kpeter@809: last_out = _first_out[i+1]; kpeter@809: for (int j = _first_out[i]; j != last_out; ++j) { kpeter@809: if (_forward[j]) { kpeter@809: Value c = _scost[j] < 0 ? _upper[j] : _lower[j]; kpeter@809: if (c >= MAX) return UNBOUNDED; kpeter@809: _excess[i] -= c; kpeter@809: _excess[_target[j]] += c; kpeter@809: } kpeter@809: } kpeter@809: } kpeter@809: } else { kpeter@809: for (int i = 0; i != _root; ++i) { kpeter@809: last_out = _first_out[i+1]; kpeter@809: for (int j = _first_out[i]; j != last_out; ++j) { kpeter@809: if (_forward[j] && _scost[j] < 0) { kpeter@809: Value c = _upper[j]; kpeter@809: if (c >= MAX) return UNBOUNDED; kpeter@809: _excess[i] -= c; kpeter@809: _excess[_target[j]] += c; kpeter@809: } kpeter@809: } kpeter@809: } kpeter@809: } kpeter@809: Value ex, max_cap = 0; kpeter@809: for (int i = 0; i != _res_node_num; ++i) { kpeter@809: ex = _excess[i]; kpeter@809: _excess[i] = 0; kpeter@809: if (ex < 0) max_cap -= ex; kpeter@809: } kpeter@809: for (int j = 0; j != _res_arc_num; ++j) { kpeter@809: if (_upper[j] >= MAX) _upper[j] = max_cap; kpeter@808: } kpeter@808: kpeter@809: // Initialize the large cost vector and the epsilon parameter kpeter@809: _epsilon = 0; kpeter@809: LargeCost lc; kpeter@809: for (int i = 0; i != _root; ++i) { kpeter@809: last_out = _first_out[i+1]; kpeter@809: for (int j = _first_out[i]; j != last_out; ++j) { kpeter@809: lc = static_cast(_scost[j]) * _res_node_num * _alpha; kpeter@809: _cost[j] = lc; kpeter@809: if (lc > _epsilon) _epsilon = lc; kpeter@809: } kpeter@809: } kpeter@809: _epsilon /= _alpha; kpeter@808: kpeter@809: // Initialize maps for Circulation and remove non-zero lower bounds kpeter@809: ConstMap low(0); kpeter@809: typedef typename Digraph::template ArcMap ValueArcMap; kpeter@809: typedef typename Digraph::template NodeMap ValueNodeMap; kpeter@809: ValueArcMap cap(_graph), flow(_graph); kpeter@809: ValueNodeMap sup(_graph); kpeter@809: for (NodeIt n(_graph); n != INVALID; ++n) { kpeter@809: sup[n] = _supply[_node_id[n]]; kpeter@808: } kpeter@809: if (_have_lower) { kpeter@809: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@809: int j = _arc_idf[a]; kpeter@809: Value c = _lower[j]; kpeter@809: cap[a] = _upper[j] - c; kpeter@809: sup[_graph.source(a)] -= c; kpeter@809: sup[_graph.target(a)] += c; kpeter@809: } kpeter@809: } else { kpeter@809: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@809: cap[a] = _upper[_arc_idf[a]]; kpeter@809: } kpeter@809: } kpeter@808: kpeter@808: // Find a feasible flow using Circulation kpeter@809: Circulation, ValueArcMap, ValueNodeMap> kpeter@809: circ(_graph, low, cap, sup); kpeter@809: if (!circ.flowMap(flow).run()) return INFEASIBLE; kpeter@809: kpeter@809: // Set residual capacities and handle GEQ supply type kpeter@809: if (_sum_supply < 0) { kpeter@809: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@809: Value fa = flow[a]; kpeter@809: _res_cap[_arc_idf[a]] = cap[a] - fa; kpeter@809: _res_cap[_arc_idb[a]] = fa; kpeter@809: sup[_graph.source(a)] -= fa; kpeter@809: sup[_graph.target(a)] += fa; kpeter@809: } kpeter@809: for (NodeIt n(_graph); n != INVALID; ++n) { kpeter@809: _excess[_node_id[n]] = sup[n]; kpeter@809: } kpeter@809: for (int a = _first_out[_root]; a != _res_arc_num; ++a) { kpeter@809: int u = _target[a]; kpeter@809: int ra = _reverse[a]; kpeter@809: _res_cap[a] = -_sum_supply + 1; kpeter@809: _res_cap[ra] = -_excess[u]; kpeter@809: _cost[a] = 0; kpeter@809: _cost[ra] = 0; kpeter@809: _excess[u] = 0; kpeter@809: } kpeter@809: } else { kpeter@809: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@809: Value fa = flow[a]; kpeter@809: _res_cap[_arc_idf[a]] = cap[a] - fa; kpeter@809: _res_cap[_arc_idb[a]] = fa; kpeter@809: } kpeter@809: for (int a = _first_out[_root]; a != _res_arc_num; ++a) { kpeter@809: int ra = _reverse[a]; kpeter@809: _res_cap[a] = 1; kpeter@809: _res_cap[ra] = 0; kpeter@809: _cost[a] = 0; kpeter@809: _cost[ra] = 0; kpeter@809: } kpeter@809: } kpeter@809: kpeter@809: return OPTIMAL; kpeter@809: } kpeter@809: kpeter@809: // Execute the algorithm and transform the results kpeter@810: void start(Method method) { kpeter@810: // Maximum path length for partial augment kpeter@810: const int MAX_PATH_LENGTH = 4; kpeter@810: kpeter@809: // Execute the algorithm kpeter@810: switch (method) { kpeter@810: case PUSH: kpeter@810: startPush(); kpeter@810: break; kpeter@810: case AUGMENT: kpeter@810: startAugment(); kpeter@810: break; kpeter@810: case PARTIAL_AUGMENT: kpeter@810: startAugment(MAX_PATH_LENGTH); kpeter@810: break; kpeter@809: } kpeter@809: kpeter@809: // Compute node potentials for the original costs kpeter@809: _arc_vec.clear(); kpeter@809: _cost_vec.clear(); kpeter@809: for (int j = 0; j != _res_arc_num; ++j) { kpeter@809: if (_res_cap[j] > 0) { kpeter@809: _arc_vec.push_back(IntPair(_source[j], _target[j])); kpeter@809: _cost_vec.push_back(_scost[j]); kpeter@809: } kpeter@809: } kpeter@809: _sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); kpeter@809: kpeter@809: typename BellmanFord kpeter@809: ::template SetDistMap::Create bf(_sgr, _cost_map); kpeter@809: bf.distMap(_pi_map); kpeter@809: bf.init(0); kpeter@809: bf.start(); kpeter@809: kpeter@809: // Handle non-zero lower bounds kpeter@809: if (_have_lower) { kpeter@809: int limit = _first_out[_root]; kpeter@809: for (int j = 0; j != limit; ++j) { kpeter@809: if (!_forward[j]) _res_cap[j] += _lower[j]; kpeter@809: } kpeter@809: } kpeter@808: } kpeter@808: kpeter@810: /// Execute the algorithm performing augment and relabel operations kpeter@810: void startAugment(int max_length = std::numeric_limits::max()) { kpeter@808: // Paramters for heuristics kpeter@809: const int BF_HEURISTIC_EPSILON_BOUND = 1000; kpeter@809: const int BF_HEURISTIC_BOUND_FACTOR = 3; kpeter@808: kpeter@809: // Perform cost scaling phases kpeter@809: IntVector pred_arc(_res_node_num); kpeter@809: std::vector path_nodes; kpeter@808: for ( ; _epsilon >= 1; _epsilon = _epsilon < _alpha && _epsilon > 1 ? kpeter@808: 1 : _epsilon / _alpha ) kpeter@808: { kpeter@808: // "Early Termination" heuristic: use Bellman-Ford algorithm kpeter@808: // to check if the current flow is optimal kpeter@808: if (_epsilon <= BF_HEURISTIC_EPSILON_BOUND) { kpeter@809: _arc_vec.clear(); kpeter@809: _cost_vec.clear(); kpeter@809: for (int j = 0; j != _res_arc_num; ++j) { kpeter@809: if (_res_cap[j] > 0) { kpeter@809: _arc_vec.push_back(IntPair(_source[j], _target[j])); kpeter@809: _cost_vec.push_back(_cost[j] + 1); kpeter@809: } kpeter@809: } kpeter@809: _sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); kpeter@809: kpeter@809: BellmanFord bf(_sgr, _cost_map); kpeter@808: bf.init(0); kpeter@808: bool done = false; kpeter@809: int K = int(BF_HEURISTIC_BOUND_FACTOR * sqrt(_res_node_num)); kpeter@808: for (int i = 0; i < K && !done; ++i) kpeter@808: done = bf.processNextWeakRound(); kpeter@808: if (done) break; kpeter@808: } kpeter@809: kpeter@808: // Saturate arcs not satisfying the optimality condition kpeter@809: for (int a = 0; a != _res_arc_num; ++a) { kpeter@809: if (_res_cap[a] > 0 && kpeter@809: _cost[a] + _pi[_source[a]] - _pi[_target[a]] < 0) { kpeter@809: Value delta = _res_cap[a]; kpeter@809: _excess[_source[a]] -= delta; kpeter@809: _excess[_target[a]] += delta; kpeter@809: _res_cap[a] = 0; kpeter@809: _res_cap[_reverse[a]] += delta; kpeter@808: } kpeter@808: } kpeter@809: kpeter@808: // Find active nodes (i.e. nodes with positive excess) kpeter@809: for (int u = 0; u != _res_node_num; ++u) { kpeter@809: if (_excess[u] > 0) _active_nodes.push_back(u); kpeter@808: } kpeter@808: kpeter@809: // Initialize the next arcs kpeter@809: for (int u = 0; u != _res_node_num; ++u) { kpeter@809: _next_out[u] = _first_out[u]; kpeter@808: } kpeter@808: kpeter@808: // Perform partial augment and relabel operations kpeter@809: while (true) { kpeter@808: // Select an active node (FIFO selection) kpeter@809: while (_active_nodes.size() > 0 && kpeter@809: _excess[_active_nodes.front()] <= 0) { kpeter@809: _active_nodes.pop_front(); kpeter@808: } kpeter@809: if (_active_nodes.size() == 0) break; kpeter@809: int start = _active_nodes.front(); kpeter@808: path_nodes.clear(); kpeter@808: path_nodes.push_back(start); kpeter@808: kpeter@808: // Find an augmenting path from the start node kpeter@809: int tip = start; kpeter@809: while (_excess[tip] >= 0 && kpeter@810: int(path_nodes.size()) <= max_length) { kpeter@809: int u; kpeter@809: LargeCost min_red_cost, rc; kpeter@809: int last_out = _sum_supply < 0 ? kpeter@809: _first_out[tip+1] : _first_out[tip+1] - 1; kpeter@809: for (int a = _next_out[tip]; a != last_out; ++a) { kpeter@809: if (_res_cap[a] > 0 && kpeter@809: _cost[a] + _pi[_source[a]] - _pi[_target[a]] < 0) { kpeter@809: u = _target[a]; kpeter@809: pred_arc[u] = a; kpeter@809: _next_out[tip] = a; kpeter@808: tip = u; kpeter@808: path_nodes.push_back(tip); kpeter@808: goto next_step; kpeter@808: } kpeter@808: } kpeter@808: kpeter@808: // Relabel tip node kpeter@809: min_red_cost = std::numeric_limits::max() / 2; kpeter@809: for (int a = _first_out[tip]; a != last_out; ++a) { kpeter@809: rc = _cost[a] + _pi[_source[a]] - _pi[_target[a]]; kpeter@809: if (_res_cap[a] > 0 && rc < min_red_cost) { kpeter@809: min_red_cost = rc; kpeter@809: } kpeter@808: } kpeter@809: _pi[tip] -= min_red_cost + _epsilon; kpeter@808: kpeter@809: // Reset the next arc of tip kpeter@809: _next_out[tip] = _first_out[tip]; kpeter@808: kpeter@808: // Step back kpeter@808: if (tip != start) { kpeter@808: path_nodes.pop_back(); kpeter@809: tip = path_nodes.back(); kpeter@808: } kpeter@808: kpeter@809: next_step: ; kpeter@808: } kpeter@808: kpeter@808: // Augment along the found path (as much flow as possible) kpeter@809: Value delta; kpeter@809: int u, v = path_nodes.front(), pa; kpeter@808: for (int i = 1; i < int(path_nodes.size()); ++i) { kpeter@809: u = v; kpeter@809: v = path_nodes[i]; kpeter@809: pa = pred_arc[v]; kpeter@809: delta = std::min(_res_cap[pa], _excess[u]); kpeter@809: _res_cap[pa] -= delta; kpeter@809: _res_cap[_reverse[pa]] += delta; kpeter@809: _excess[u] -= delta; kpeter@809: _excess[v] += delta; kpeter@809: if (_excess[v] > 0 && _excess[v] <= delta) kpeter@809: _active_nodes.push_back(v); kpeter@808: } kpeter@808: } kpeter@808: } kpeter@808: } kpeter@808: kpeter@809: /// Execute the algorithm performing push and relabel operations kpeter@810: void startPush() { kpeter@808: // Paramters for heuristics kpeter@809: const int BF_HEURISTIC_EPSILON_BOUND = 1000; kpeter@809: const int BF_HEURISTIC_BOUND_FACTOR = 3; kpeter@808: kpeter@809: // Perform cost scaling phases kpeter@809: BoolVector hyper(_res_node_num, false); kpeter@808: for ( ; _epsilon >= 1; _epsilon = _epsilon < _alpha && _epsilon > 1 ? kpeter@808: 1 : _epsilon / _alpha ) kpeter@808: { kpeter@808: // "Early Termination" heuristic: use Bellman-Ford algorithm kpeter@808: // to check if the current flow is optimal kpeter@808: if (_epsilon <= BF_HEURISTIC_EPSILON_BOUND) { kpeter@809: _arc_vec.clear(); kpeter@809: _cost_vec.clear(); kpeter@809: for (int j = 0; j != _res_arc_num; ++j) { kpeter@809: if (_res_cap[j] > 0) { kpeter@809: _arc_vec.push_back(IntPair(_source[j], _target[j])); kpeter@809: _cost_vec.push_back(_cost[j] + 1); kpeter@809: } kpeter@809: } kpeter@809: _sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); kpeter@809: kpeter@809: BellmanFord bf(_sgr, _cost_map); kpeter@808: bf.init(0); kpeter@808: bool done = false; kpeter@809: int K = int(BF_HEURISTIC_BOUND_FACTOR * sqrt(_res_node_num)); kpeter@808: for (int i = 0; i < K && !done; ++i) kpeter@808: done = bf.processNextWeakRound(); kpeter@808: if (done) break; kpeter@808: } kpeter@808: kpeter@808: // Saturate arcs not satisfying the optimality condition kpeter@809: for (int a = 0; a != _res_arc_num; ++a) { kpeter@809: if (_res_cap[a] > 0 && kpeter@809: _cost[a] + _pi[_source[a]] - _pi[_target[a]] < 0) { kpeter@809: Value delta = _res_cap[a]; kpeter@809: _excess[_source[a]] -= delta; kpeter@809: _excess[_target[a]] += delta; kpeter@809: _res_cap[a] = 0; kpeter@809: _res_cap[_reverse[a]] += delta; kpeter@808: } kpeter@808: } kpeter@808: kpeter@808: // Find active nodes (i.e. nodes with positive excess) kpeter@809: for (int u = 0; u != _res_node_num; ++u) { kpeter@809: if (_excess[u] > 0) _active_nodes.push_back(u); kpeter@808: } kpeter@808: kpeter@809: // Initialize the next arcs kpeter@809: for (int u = 0; u != _res_node_num; ++u) { kpeter@809: _next_out[u] = _first_out[u]; kpeter@808: } kpeter@808: kpeter@808: // Perform push and relabel operations kpeter@809: while (_active_nodes.size() > 0) { kpeter@809: LargeCost min_red_cost, rc; kpeter@809: Value delta; kpeter@809: int n, t, a, last_out = _res_arc_num; kpeter@809: kpeter@808: // Select an active node (FIFO selection) kpeter@809: next_node: kpeter@809: n = _active_nodes.front(); kpeter@809: last_out = _sum_supply < 0 ? kpeter@809: _first_out[n+1] : _first_out[n+1] - 1; kpeter@808: kpeter@808: // Perform push operations if there are admissible arcs kpeter@809: if (_excess[n] > 0) { kpeter@809: for (a = _next_out[n]; a != last_out; ++a) { kpeter@809: if (_res_cap[a] > 0 && kpeter@809: _cost[a] + _pi[_source[a]] - _pi[_target[a]] < 0) { kpeter@809: delta = std::min(_res_cap[a], _excess[n]); kpeter@809: t = _target[a]; kpeter@808: kpeter@808: // Push-look-ahead heuristic kpeter@809: Value ahead = -_excess[t]; kpeter@809: int last_out_t = _sum_supply < 0 ? kpeter@809: _first_out[t+1] : _first_out[t+1] - 1; kpeter@809: for (int ta = _next_out[t]; ta != last_out_t; ++ta) { kpeter@809: if (_res_cap[ta] > 0 && kpeter@809: _cost[ta] + _pi[_source[ta]] - _pi[_target[ta]] < 0) kpeter@809: ahead += _res_cap[ta]; kpeter@809: if (ahead >= delta) break; kpeter@808: } kpeter@808: if (ahead < 0) ahead = 0; kpeter@808: kpeter@808: // Push flow along the arc kpeter@808: if (ahead < delta) { kpeter@809: _res_cap[a] -= ahead; kpeter@809: _res_cap[_reverse[a]] += ahead; kpeter@808: _excess[n] -= ahead; kpeter@808: _excess[t] += ahead; kpeter@809: _active_nodes.push_front(t); kpeter@808: hyper[t] = true; kpeter@809: _next_out[n] = a; kpeter@809: goto next_node; kpeter@808: } else { kpeter@809: _res_cap[a] -= delta; kpeter@809: _res_cap[_reverse[a]] += delta; kpeter@808: _excess[n] -= delta; kpeter@808: _excess[t] += delta; kpeter@808: if (_excess[t] > 0 && _excess[t] <= delta) kpeter@809: _active_nodes.push_back(t); kpeter@808: } kpeter@808: kpeter@809: if (_excess[n] == 0) { kpeter@809: _next_out[n] = a; kpeter@809: goto remove_nodes; kpeter@809: } kpeter@808: } kpeter@808: } kpeter@809: _next_out[n] = a; kpeter@808: } kpeter@808: kpeter@808: // Relabel the node if it is still active (or hyper) kpeter@809: if (_excess[n] > 0 || hyper[n]) { kpeter@809: min_red_cost = std::numeric_limits::max() / 2; kpeter@809: for (int a = _first_out[n]; a != last_out; ++a) { kpeter@809: rc = _cost[a] + _pi[_source[a]] - _pi[_target[a]]; kpeter@809: if (_res_cap[a] > 0 && rc < min_red_cost) { kpeter@809: min_red_cost = rc; kpeter@809: } kpeter@808: } kpeter@809: _pi[n] -= min_red_cost + _epsilon; kpeter@808: hyper[n] = false; kpeter@808: kpeter@809: // Reset the next arc kpeter@809: _next_out[n] = _first_out[n]; kpeter@808: } kpeter@809: kpeter@808: // Remove nodes that are not active nor hyper kpeter@809: remove_nodes: kpeter@809: while ( _active_nodes.size() > 0 && kpeter@809: _excess[_active_nodes.front()] <= 0 && kpeter@809: !hyper[_active_nodes.front()] ) { kpeter@809: _active_nodes.pop_front(); kpeter@808: } kpeter@808: } kpeter@808: } kpeter@808: } kpeter@808: kpeter@808: }; //class CostScaling kpeter@808: kpeter@808: ///@} kpeter@808: kpeter@808: } //namespace lemon kpeter@808: kpeter@808: #endif //LEMON_COST_SCALING_H