alpar@956: /* -*- mode: C++; indent-tabs-mode: nil; -*- kpeter@880: * alpar@956: * This file is a part of LEMON, a generic C++ optimization library. kpeter@880: * alpar@1270: * Copyright (C) 2003-2013 kpeter@880: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport kpeter@880: * (Egervary Research Group on Combinatorial Optimization, EGRES). kpeter@880: * kpeter@880: * Permission to use, modify and distribute this software is granted kpeter@880: * provided that this copyright notice appears in all copies. For kpeter@880: * precise terms see the accompanying LICENSE file. kpeter@880: * kpeter@880: * This software is provided "AS IS" with no warranty of any kind, kpeter@880: * express or implied, and with no claim as to its suitability for any kpeter@880: * purpose. kpeter@880: * kpeter@880: */ kpeter@880: kpeter@880: #ifndef LEMON_CYCLE_CANCELING_H kpeter@880: #define LEMON_CYCLE_CANCELING_H kpeter@880: kpeter@881: /// \ingroup min_cost_flow_algs kpeter@880: /// \file kpeter@881: /// \brief Cycle-canceling algorithms for finding a minimum cost flow. kpeter@880: kpeter@880: #include kpeter@881: #include kpeter@881: kpeter@881: #include kpeter@881: #include kpeter@881: #include kpeter@881: #include kpeter@881: #include kpeter@880: #include kpeter@880: #include kpeter@880: #include kpeter@942: #include kpeter@1179: #include kpeter@880: kpeter@880: namespace lemon { kpeter@880: kpeter@881: /// \addtogroup min_cost_flow_algs kpeter@880: /// @{ kpeter@880: kpeter@881: /// \brief Implementation of cycle-canceling algorithms for kpeter@881: /// finding a \ref min_cost_flow "minimum cost flow". kpeter@880: /// kpeter@881: /// \ref CycleCanceling implements three different cycle-canceling kpeter@882: /// algorithms for finding a \ref min_cost_flow "minimum cost flow" alpar@1221: /// \cite amo93networkflows, \cite klein67primal, alpar@1221: /// \cite goldberg89cyclecanceling. kpeter@1165: /// The most efficent one is the \ref CANCEL_AND_TIGHTEN kpeter@1165: /// "Cancel-and-Tighten" algorithm, thus it is the default method. kpeter@1255: /// It runs in strongly polynomial time \f$O(n^2 m^2 \log n)\f$, kpeter@1217: /// but in practice, it is typically orders of magnitude slower than kpeter@1217: /// the scaling algorithms and \ref NetworkSimplex. kpeter@1165: /// (For more information, see \ref min_cost_flow_algs "the module page".) kpeter@880: /// kpeter@881: /// Most of the parameters of the problem (except for the digraph) kpeter@881: /// can be given using separate functions, and the algorithm can be kpeter@881: /// executed using the \ref run() function. If some parameters are not kpeter@881: /// specified, then default values will be used. kpeter@880: /// kpeter@881: /// \tparam GR The digraph type the algorithm runs on. kpeter@881: /// \tparam V The number type used for flow amounts, capacity bounds kpeter@881: /// and supply values in the algorithm. By default, it is \c int. kpeter@881: /// \tparam C The number type used for costs and potentials in the kpeter@881: /// algorithm. By default, it is the same as \c V. kpeter@880: /// kpeter@1025: /// \warning Both \c V and \c C must be signed number types. kpeter@1025: /// \warning All input data (capacities, supply values, and costs) must kpeter@881: /// be integer. kpeter@1023: /// \warning This algorithm does not support negative costs for kpeter@1023: /// arcs having infinite upper bound. kpeter@880: /// kpeter@881: /// \note For more information about the three available methods, kpeter@881: /// see \ref Method. kpeter@881: #ifdef DOXYGEN kpeter@881: template kpeter@881: #else kpeter@881: template kpeter@881: #endif kpeter@880: class CycleCanceling kpeter@880: { kpeter@881: public: kpeter@880: kpeter@881: /// The type of the digraph kpeter@881: typedef GR Digraph; kpeter@881: /// The type of the flow amounts, capacity bounds and supply values kpeter@881: typedef V Value; kpeter@881: /// The type of the arc costs kpeter@881: typedef C Cost; kpeter@880: kpeter@880: public: kpeter@880: kpeter@881: /// \brief Problem type constants for the \c run() function. kpeter@881: /// kpeter@881: /// Enum type containing the problem type constants that can be kpeter@881: /// returned by the \ref run() function of the algorithm. kpeter@881: enum ProblemType { kpeter@881: /// The problem has no feasible solution (flow). kpeter@881: INFEASIBLE, kpeter@881: /// The problem has optimal solution (i.e. it is feasible and kpeter@881: /// bounded), and the algorithm has found optimal flow and node kpeter@881: /// potentials (primal and dual solutions). kpeter@881: OPTIMAL, kpeter@881: /// The digraph contains an arc of negative cost and infinite kpeter@881: /// upper bound. It means that the objective function is unbounded kpeter@881: /// on that arc, however, note that it could actually be bounded kpeter@881: /// over the feasible flows, but this algroithm cannot handle kpeter@881: /// these cases. kpeter@881: UNBOUNDED kpeter@881: }; kpeter@881: kpeter@881: /// \brief Constants for selecting the used method. kpeter@881: /// kpeter@881: /// Enum type containing constants for selecting the used method kpeter@881: /// for the \ref run() function. kpeter@881: /// kpeter@881: /// \ref CycleCanceling provides three different cycle-canceling kpeter@1165: /// methods. By default, \ref CANCEL_AND_TIGHTEN "Cancel-and-Tighten" kpeter@1023: /// is used, which is by far the most efficient and the most robust. kpeter@881: /// However, the other methods can be selected using the \ref run() kpeter@881: /// function with the proper parameter. kpeter@881: enum Method { kpeter@881: /// A simple cycle-canceling method, which uses the kpeter@1165: /// \ref BellmanFord "Bellman-Ford" algorithm for detecting negative kpeter@1165: /// cycles in the residual network. kpeter@1165: /// The number of Bellman-Ford iterations is bounded by a successively kpeter@1165: /// increased limit. kpeter@881: SIMPLE_CYCLE_CANCELING, kpeter@881: /// The "Minimum Mean Cycle-Canceling" algorithm, which is a kpeter@882: /// well-known strongly polynomial method alpar@1221: /// \cite goldberg89cyclecanceling. It improves along a kpeter@881: /// \ref min_mean_cycle "minimum mean cycle" in each iteration. kpeter@1255: /// Its running time complexity is \f$O(n^2 m^3 \log n)\f$. kpeter@881: MINIMUM_MEAN_CYCLE_CANCELING, kpeter@1165: /// The "Cancel-and-Tighten" algorithm, which can be viewed as an kpeter@882: /// improved version of the previous method alpar@1221: /// \cite goldberg89cyclecanceling. kpeter@881: /// It is faster both in theory and in practice, its running time kpeter@1255: /// complexity is \f$O(n^2 m^2 \log n)\f$. kpeter@881: CANCEL_AND_TIGHTEN kpeter@881: }; kpeter@880: kpeter@880: private: kpeter@880: kpeter@881: TEMPLATE_DIGRAPH_TYPEDEFS(GR); alpar@956: kpeter@881: typedef std::vector IntVector; kpeter@881: typedef std::vector DoubleVector; kpeter@881: typedef std::vector ValueVector; kpeter@881: typedef std::vector CostVector; kpeter@910: typedef std::vector BoolVector; kpeter@910: // Note: vector is used instead of vector for efficiency reasons kpeter@880: kpeter@881: private: alpar@956: kpeter@881: template kpeter@886: class StaticVectorMap { kpeter@880: public: kpeter@881: typedef KT Key; kpeter@881: typedef VT Value; alpar@956: kpeter@886: StaticVectorMap(std::vector& v) : _v(v) {} alpar@956: kpeter@881: const Value& operator[](const Key& key) const { kpeter@881: return _v[StaticDigraph::id(key)]; kpeter@880: } kpeter@880: kpeter@881: Value& operator[](const Key& key) { kpeter@881: return _v[StaticDigraph::id(key)]; kpeter@881: } alpar@956: kpeter@881: void set(const Key& key, const Value& val) { kpeter@881: _v[StaticDigraph::id(key)] = val; kpeter@881: } kpeter@881: kpeter@881: private: kpeter@881: std::vector& _v; kpeter@881: }; kpeter@881: kpeter@886: typedef StaticVectorMap CostNodeMap; kpeter@886: typedef StaticVectorMap CostArcMap; kpeter@880: kpeter@880: private: kpeter@880: kpeter@880: kpeter@881: // Data related to the underlying digraph kpeter@881: const GR &_graph; kpeter@881: int _node_num; kpeter@881: int _arc_num; kpeter@881: int _res_node_num; kpeter@881: int _res_arc_num; kpeter@881: int _root; kpeter@880: kpeter@881: // Parameters of the problem kpeter@1297: bool _has_lower; kpeter@881: Value _sum_supply; kpeter@880: kpeter@881: // Data structures for storing the digraph kpeter@881: IntNodeMap _node_id; kpeter@881: IntArcMap _arc_idf; kpeter@881: IntArcMap _arc_idb; kpeter@881: IntVector _first_out; kpeter@910: BoolVector _forward; kpeter@881: IntVector _source; kpeter@881: IntVector _target; kpeter@881: IntVector _reverse; kpeter@880: kpeter@881: // Node and arc data kpeter@881: ValueVector _lower; kpeter@881: ValueVector _upper; kpeter@881: CostVector _cost; kpeter@881: ValueVector _supply; kpeter@881: kpeter@881: ValueVector _res_cap; kpeter@881: CostVector _pi; kpeter@881: kpeter@881: // Data for a StaticDigraph structure kpeter@881: typedef std::pair IntPair; kpeter@881: StaticDigraph _sgr; kpeter@881: std::vector _arc_vec; kpeter@881: std::vector _cost_vec; kpeter@881: IntVector _id_vec; kpeter@881: CostArcMap _cost_map; kpeter@881: CostNodeMap _pi_map; alpar@956: kpeter@881: public: alpar@956: kpeter@881: /// \brief Constant for infinite upper bounds (capacities). kpeter@881: /// kpeter@881: /// Constant for infinite upper bounds (capacities). kpeter@881: /// It is \c std::numeric_limits::infinity() if available, kpeter@881: /// \c std::numeric_limits::max() otherwise. kpeter@881: const Value INF; kpeter@880: kpeter@880: public: kpeter@880: kpeter@881: /// \brief Constructor. kpeter@880: /// kpeter@881: /// The constructor of the class. kpeter@880: /// kpeter@881: /// \param graph The digraph the algorithm runs on. kpeter@881: CycleCanceling(const GR& graph) : kpeter@881: _graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph), kpeter@881: _cost_map(_cost_vec), _pi_map(_pi), kpeter@881: INF(std::numeric_limits::has_infinity ? kpeter@881: std::numeric_limits::infinity() : kpeter@881: std::numeric_limits::max()) kpeter@880: { kpeter@881: // Check the number types kpeter@881: LEMON_ASSERT(std::numeric_limits::is_signed, kpeter@881: "The flow type of CycleCanceling must be signed"); kpeter@881: LEMON_ASSERT(std::numeric_limits::is_signed, kpeter@881: "The cost type of CycleCanceling must be signed"); kpeter@881: kpeter@898: // Reset data structures kpeter@881: reset(); kpeter@880: } kpeter@880: kpeter@881: /// \name Parameters kpeter@881: /// The parameters of the algorithm can be specified using these kpeter@881: /// functions. kpeter@881: kpeter@881: /// @{ kpeter@881: kpeter@881: /// \brief Set the lower bounds on the arcs. kpeter@880: /// kpeter@881: /// This function sets the lower bounds on the arcs. kpeter@881: /// If it is not used before calling \ref run(), the lower bounds kpeter@881: /// will be set to zero on all arcs. kpeter@880: /// kpeter@881: /// \param map An arc map storing the lower bounds. kpeter@881: /// Its \c Value type must be convertible to the \c Value type kpeter@881: /// of the algorithm. kpeter@881: /// kpeter@881: /// \return (*this) kpeter@881: template kpeter@881: CycleCanceling& lowerMap(const LowerMap& map) { kpeter@1297: _has_lower = true; kpeter@881: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@881: _lower[_arc_idf[a]] = map[a]; kpeter@880: } kpeter@880: return *this; kpeter@880: } kpeter@880: kpeter@881: /// \brief Set the upper bounds (capacities) on the arcs. kpeter@880: /// kpeter@881: /// This function sets the upper bounds (capacities) on the arcs. kpeter@881: /// If it is not used before calling \ref run(), the upper bounds kpeter@881: /// will be set to \ref INF on all arcs (i.e. the flow value will be kpeter@881: /// unbounded from above). kpeter@880: /// kpeter@881: /// \param map An arc map storing the upper bounds. kpeter@881: /// Its \c Value type must be convertible to the \c Value type kpeter@881: /// of the algorithm. kpeter@881: /// kpeter@881: /// \return (*this) kpeter@881: template kpeter@881: CycleCanceling& upperMap(const UpperMap& map) { kpeter@881: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@881: _upper[_arc_idf[a]] = map[a]; kpeter@880: } kpeter@880: return *this; kpeter@880: } kpeter@880: kpeter@881: /// \brief Set the costs of the arcs. kpeter@881: /// kpeter@881: /// This function sets the costs of the arcs. kpeter@881: /// If it is not used before calling \ref run(), the costs kpeter@881: /// will be set to \c 1 on all arcs. kpeter@881: /// kpeter@881: /// \param map An arc map storing the costs. kpeter@881: /// Its \c Value type must be convertible to the \c Cost type kpeter@881: /// of the algorithm. kpeter@881: /// kpeter@881: /// \return (*this) kpeter@881: template kpeter@881: CycleCanceling& costMap(const CostMap& map) { kpeter@881: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@881: _cost[_arc_idf[a]] = map[a]; kpeter@881: _cost[_arc_idb[a]] = -map[a]; kpeter@881: } kpeter@881: return *this; kpeter@881: } kpeter@881: kpeter@881: /// \brief Set the supply values of the nodes. kpeter@881: /// kpeter@881: /// This function sets the supply values of the nodes. kpeter@881: /// If neither this function nor \ref stSupply() is used before kpeter@881: /// calling \ref run(), the supply of each node will be set to zero. kpeter@881: /// kpeter@881: /// \param map A node map storing the supply values. kpeter@881: /// Its \c Value type must be convertible to the \c Value type kpeter@881: /// of the algorithm. kpeter@881: /// kpeter@881: /// \return (*this) kpeter@881: template kpeter@881: CycleCanceling& supplyMap(const SupplyMap& map) { kpeter@881: for (NodeIt n(_graph); n != INVALID; ++n) { kpeter@881: _supply[_node_id[n]] = map[n]; kpeter@881: } kpeter@881: return *this; kpeter@881: } kpeter@881: kpeter@881: /// \brief Set single source and target nodes and a supply value. kpeter@881: /// kpeter@881: /// This function sets a single source node and a single target node kpeter@881: /// and the required flow value. kpeter@881: /// If neither this function nor \ref supplyMap() is used before kpeter@881: /// calling \ref run(), the supply of each node will be set to zero. kpeter@881: /// kpeter@881: /// Using this function has the same effect as using \ref supplyMap() kpeter@1023: /// with a map in which \c k is assigned to \c s, \c -k is kpeter@881: /// assigned to \c t and all other nodes have zero supply value. kpeter@881: /// kpeter@881: /// \param s The source node. kpeter@881: /// \param t The target node. kpeter@881: /// \param k The required amount of flow from node \c s to node \c t kpeter@881: /// (i.e. the supply of \c s and the demand of \c t). kpeter@881: /// kpeter@881: /// \return (*this) kpeter@881: CycleCanceling& stSupply(const Node& s, const Node& t, Value k) { kpeter@881: for (int i = 0; i != _res_node_num; ++i) { kpeter@881: _supply[i] = 0; kpeter@881: } kpeter@881: _supply[_node_id[s]] = k; kpeter@881: _supply[_node_id[t]] = -k; kpeter@881: return *this; kpeter@881: } alpar@956: kpeter@881: /// @} kpeter@881: kpeter@880: /// \name Execution control kpeter@881: /// The algorithm can be executed using \ref run(). kpeter@880: kpeter@880: /// @{ kpeter@880: kpeter@880: /// \brief Run the algorithm. kpeter@880: /// kpeter@881: /// This function runs the algorithm. kpeter@881: /// The paramters can be specified using functions \ref lowerMap(), kpeter@881: /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). kpeter@881: /// For example, kpeter@881: /// \code kpeter@881: /// CycleCanceling cc(graph); kpeter@881: /// cc.lowerMap(lower).upperMap(upper).costMap(cost) kpeter@881: /// .supplyMap(sup).run(); kpeter@881: /// \endcode kpeter@880: /// kpeter@898: /// This function can be called more than once. All the given parameters kpeter@898: /// are kept for the next call, unless \ref resetParams() or \ref reset() kpeter@898: /// is used, thus only the modified parameters have to be set again. kpeter@898: /// If the underlying digraph was also modified after the construction kpeter@898: /// of the class (or the last \ref reset() call), then the \ref reset() kpeter@898: /// function must be called. kpeter@880: /// kpeter@881: /// \param method The cycle-canceling method that will be used. kpeter@881: /// For more information, see \ref Method. kpeter@881: /// kpeter@881: /// \return \c INFEASIBLE if no feasible flow exists, kpeter@881: /// \n \c OPTIMAL if the problem has optimal solution kpeter@881: /// (i.e. it is feasible and bounded), and the algorithm has found kpeter@881: /// optimal flow and node potentials (primal and dual solutions), kpeter@881: /// \n \c UNBOUNDED if the digraph contains an arc of negative cost kpeter@881: /// and infinite upper bound. It means that the objective function kpeter@881: /// is unbounded on that arc, however, note that it could actually be kpeter@881: /// bounded over the feasible flows, but this algroithm cannot handle kpeter@881: /// these cases. kpeter@881: /// kpeter@881: /// \see ProblemType, Method kpeter@898: /// \see resetParams(), reset() kpeter@881: ProblemType run(Method method = CANCEL_AND_TIGHTEN) { kpeter@881: ProblemType pt = init(); kpeter@881: if (pt != OPTIMAL) return pt; kpeter@881: start(method); kpeter@881: return OPTIMAL; kpeter@881: } kpeter@881: kpeter@881: /// \brief Reset all the parameters that have been given before. kpeter@881: /// kpeter@881: /// This function resets all the paramaters that have been given kpeter@881: /// before using functions \ref lowerMap(), \ref upperMap(), kpeter@881: /// \ref costMap(), \ref supplyMap(), \ref stSupply(). kpeter@881: /// kpeter@898: /// It is useful for multiple \ref run() calls. Basically, all the given kpeter@898: /// parameters are kept for the next \ref run() call, unless kpeter@898: /// \ref resetParams() or \ref reset() is used. kpeter@898: /// If the underlying digraph was also modified after the construction kpeter@898: /// of the class or the last \ref reset() call, then the \ref reset() kpeter@898: /// function must be used, otherwise \ref resetParams() is sufficient. kpeter@881: /// kpeter@881: /// For example, kpeter@881: /// \code kpeter@881: /// CycleCanceling cs(graph); kpeter@881: /// kpeter@881: /// // First run kpeter@881: /// cc.lowerMap(lower).upperMap(upper).costMap(cost) kpeter@881: /// .supplyMap(sup).run(); kpeter@881: /// kpeter@898: /// // Run again with modified cost map (resetParams() is not called, kpeter@881: /// // so only the cost map have to be set again) kpeter@881: /// cost[e] += 100; kpeter@881: /// cc.costMap(cost).run(); kpeter@881: /// kpeter@898: /// // Run again from scratch using resetParams() kpeter@881: /// // (the lower bounds will be set to zero on all arcs) kpeter@898: /// cc.resetParams(); kpeter@881: /// cc.upperMap(capacity).costMap(cost) kpeter@881: /// .supplyMap(sup).run(); kpeter@881: /// \endcode kpeter@881: /// kpeter@881: /// \return (*this) kpeter@898: /// kpeter@898: /// \see reset(), run() kpeter@898: CycleCanceling& resetParams() { kpeter@881: for (int i = 0; i != _res_node_num; ++i) { kpeter@881: _supply[i] = 0; kpeter@881: } kpeter@881: int limit = _first_out[_root]; kpeter@881: for (int j = 0; j != limit; ++j) { kpeter@881: _lower[j] = 0; kpeter@881: _upper[j] = INF; kpeter@881: _cost[j] = _forward[j] ? 1 : -1; kpeter@881: } kpeter@881: for (int j = limit; j != _res_arc_num; ++j) { kpeter@881: _lower[j] = 0; kpeter@881: _upper[j] = INF; kpeter@881: _cost[j] = 0; kpeter@881: _cost[_reverse[j]] = 0; alpar@956: } kpeter@1297: _has_lower = false; kpeter@881: return *this; kpeter@880: } kpeter@880: kpeter@898: /// \brief Reset the internal data structures and all the parameters kpeter@898: /// that have been given before. kpeter@898: /// kpeter@898: /// This function resets the internal data structures and all the kpeter@898: /// paramaters that have been given before using functions \ref lowerMap(), kpeter@898: /// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). kpeter@898: /// kpeter@898: /// It is useful for multiple \ref run() calls. Basically, all the given kpeter@898: /// parameters are kept for the next \ref run() call, unless kpeter@898: /// \ref resetParams() or \ref reset() is used. kpeter@898: /// If the underlying digraph was also modified after the construction kpeter@898: /// of the class or the last \ref reset() call, then the \ref reset() kpeter@898: /// function must be used, otherwise \ref resetParams() is sufficient. kpeter@898: /// kpeter@898: /// See \ref resetParams() for examples. kpeter@898: /// kpeter@898: /// \return (*this) kpeter@898: /// kpeter@898: /// \see resetParams(), run() kpeter@898: CycleCanceling& reset() { kpeter@898: // Resize vectors kpeter@898: _node_num = countNodes(_graph); kpeter@898: _arc_num = countArcs(_graph); kpeter@898: _res_node_num = _node_num + 1; kpeter@898: _res_arc_num = 2 * (_arc_num + _node_num); kpeter@898: _root = _node_num; kpeter@898: kpeter@898: _first_out.resize(_res_node_num + 1); kpeter@898: _forward.resize(_res_arc_num); kpeter@898: _source.resize(_res_arc_num); kpeter@898: _target.resize(_res_arc_num); kpeter@898: _reverse.resize(_res_arc_num); kpeter@898: kpeter@898: _lower.resize(_res_arc_num); kpeter@898: _upper.resize(_res_arc_num); kpeter@898: _cost.resize(_res_arc_num); kpeter@898: _supply.resize(_res_node_num); alpar@956: kpeter@898: _res_cap.resize(_res_arc_num); kpeter@898: _pi.resize(_res_node_num); kpeter@898: kpeter@898: _arc_vec.reserve(_res_arc_num); kpeter@898: _cost_vec.reserve(_res_arc_num); kpeter@898: _id_vec.reserve(_res_arc_num); kpeter@898: kpeter@898: // Copy the graph kpeter@898: int i = 0, j = 0, k = 2 * _arc_num + _node_num; kpeter@898: for (NodeIt n(_graph); n != INVALID; ++n, ++i) { kpeter@898: _node_id[n] = i; kpeter@898: } kpeter@898: i = 0; kpeter@898: for (NodeIt n(_graph); n != INVALID; ++n, ++i) { kpeter@898: _first_out[i] = j; kpeter@898: for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) { kpeter@898: _arc_idf[a] = j; kpeter@898: _forward[j] = true; kpeter@898: _source[j] = i; kpeter@898: _target[j] = _node_id[_graph.runningNode(a)]; kpeter@898: } kpeter@898: for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) { kpeter@898: _arc_idb[a] = j; kpeter@898: _forward[j] = false; kpeter@898: _source[j] = i; kpeter@898: _target[j] = _node_id[_graph.runningNode(a)]; kpeter@898: } kpeter@898: _forward[j] = false; kpeter@898: _source[j] = i; kpeter@898: _target[j] = _root; kpeter@898: _reverse[j] = k; kpeter@898: _forward[k] = true; kpeter@898: _source[k] = _root; kpeter@898: _target[k] = i; kpeter@898: _reverse[k] = j; kpeter@898: ++j; ++k; kpeter@898: } kpeter@898: _first_out[i] = j; kpeter@898: _first_out[_res_node_num] = k; kpeter@898: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@898: int fi = _arc_idf[a]; kpeter@898: int bi = _arc_idb[a]; kpeter@898: _reverse[fi] = bi; kpeter@898: _reverse[bi] = fi; kpeter@898: } alpar@956: kpeter@898: // Reset parameters kpeter@898: resetParams(); kpeter@898: return *this; kpeter@898: } kpeter@898: kpeter@880: /// @} kpeter@880: kpeter@880: /// \name Query Functions kpeter@881: /// The results of the algorithm can be obtained using these kpeter@880: /// functions.\n kpeter@881: /// The \ref run() function must be called before using them. kpeter@880: kpeter@880: /// @{ kpeter@880: kpeter@881: /// \brief Return the total cost of the found flow. kpeter@880: /// kpeter@881: /// This function returns the total cost of the found flow. kpeter@1254: /// Its complexity is O(m). kpeter@881: /// kpeter@881: /// \note The return type of the function can be specified as a kpeter@881: /// template parameter. For example, kpeter@881: /// \code kpeter@881: /// cc.totalCost(); kpeter@881: /// \endcode kpeter@881: /// It is useful if the total cost cannot be stored in the \c Cost kpeter@881: /// type of the algorithm, which is the default return type of the kpeter@881: /// function. kpeter@880: /// kpeter@880: /// \pre \ref run() must be called before using this function. kpeter@881: template kpeter@881: Number totalCost() const { kpeter@881: Number c = 0; kpeter@881: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@881: int i = _arc_idb[a]; kpeter@881: c += static_cast(_res_cap[i]) * kpeter@881: (-static_cast(_cost[i])); kpeter@881: } kpeter@881: return c; kpeter@880: } kpeter@880: kpeter@881: #ifndef DOXYGEN kpeter@881: Cost totalCost() const { kpeter@881: return totalCost(); kpeter@880: } kpeter@881: #endif kpeter@880: kpeter@880: /// \brief Return the flow on the given arc. kpeter@880: /// kpeter@881: /// This function returns the flow on the given arc. kpeter@880: /// kpeter@880: /// \pre \ref run() must be called before using this function. kpeter@881: Value flow(const Arc& a) const { kpeter@881: return _res_cap[_arc_idb[a]]; kpeter@880: } kpeter@880: kpeter@1165: /// \brief Copy the flow values (the primal solution) into the kpeter@1165: /// given map. kpeter@880: /// kpeter@881: /// This function copies the flow value on each arc into the given kpeter@881: /// map. The \c Value type of the algorithm must be convertible to kpeter@881: /// the \c Value type of the map. kpeter@880: /// kpeter@880: /// \pre \ref run() must be called before using this function. kpeter@881: template kpeter@881: void flowMap(FlowMap &map) const { kpeter@881: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@881: map.set(a, _res_cap[_arc_idb[a]]); kpeter@881: } kpeter@880: } kpeter@880: kpeter@881: /// \brief Return the potential (dual value) of the given node. kpeter@880: /// kpeter@881: /// This function returns the potential (dual value) of the kpeter@881: /// given node. kpeter@880: /// kpeter@880: /// \pre \ref run() must be called before using this function. kpeter@881: Cost potential(const Node& n) const { kpeter@881: return static_cast(_pi[_node_id[n]]); kpeter@881: } kpeter@881: kpeter@1165: /// \brief Copy the potential values (the dual solution) into the kpeter@1165: /// given map. kpeter@881: /// kpeter@881: /// This function copies the potential (dual value) of each node kpeter@881: /// into the given map. kpeter@881: /// The \c Cost type of the algorithm must be convertible to the kpeter@881: /// \c Value type of the map. kpeter@881: /// kpeter@881: /// \pre \ref run() must be called before using this function. kpeter@881: template kpeter@881: void potentialMap(PotentialMap &map) const { kpeter@881: for (NodeIt n(_graph); n != INVALID; ++n) { kpeter@881: map.set(n, static_cast(_pi[_node_id[n]])); kpeter@881: } kpeter@880: } kpeter@880: kpeter@880: /// @} kpeter@880: kpeter@880: private: kpeter@880: kpeter@881: // Initialize the algorithm kpeter@881: ProblemType init() { kpeter@881: if (_res_node_num <= 1) return INFEASIBLE; kpeter@880: kpeter@881: // Check the sum of supply values kpeter@881: _sum_supply = 0; kpeter@881: for (int i = 0; i != _root; ++i) { kpeter@881: _sum_supply += _supply[i]; kpeter@880: } kpeter@881: if (_sum_supply > 0) return INFEASIBLE; alpar@956: kpeter@1240: // Check lower and upper bounds kpeter@1240: LEMON_DEBUG(checkBoundMaps(), kpeter@1240: "Upper bounds must be greater or equal to the lower bounds"); kpeter@1240: kpeter@881: kpeter@881: // Initialize vectors kpeter@881: for (int i = 0; i != _res_node_num; ++i) { kpeter@881: _pi[i] = 0; kpeter@881: } kpeter@881: ValueVector excess(_supply); alpar@956: kpeter@881: // Remove infinite upper bounds and check negative arcs kpeter@881: const Value MAX = std::numeric_limits::max(); kpeter@881: int last_out; kpeter@1297: if (_has_lower) { kpeter@881: for (int i = 0; i != _root; ++i) { kpeter@881: last_out = _first_out[i+1]; kpeter@881: for (int j = _first_out[i]; j != last_out; ++j) { kpeter@881: if (_forward[j]) { kpeter@881: Value c = _cost[j] < 0 ? _upper[j] : _lower[j]; kpeter@881: if (c >= MAX) return UNBOUNDED; kpeter@881: excess[i] -= c; kpeter@881: excess[_target[j]] += c; kpeter@881: } kpeter@881: } kpeter@881: } kpeter@881: } else { kpeter@881: for (int i = 0; i != _root; ++i) { kpeter@881: last_out = _first_out[i+1]; kpeter@881: for (int j = _first_out[i]; j != last_out; ++j) { kpeter@881: if (_forward[j] && _cost[j] < 0) { kpeter@881: Value c = _upper[j]; kpeter@881: if (c >= MAX) return UNBOUNDED; kpeter@881: excess[i] -= c; kpeter@881: excess[_target[j]] += c; kpeter@881: } kpeter@881: } kpeter@881: } kpeter@881: } kpeter@881: Value ex, max_cap = 0; kpeter@881: for (int i = 0; i != _res_node_num; ++i) { kpeter@881: ex = excess[i]; kpeter@881: if (ex < 0) max_cap -= ex; kpeter@881: } kpeter@881: for (int j = 0; j != _res_arc_num; ++j) { kpeter@881: if (_upper[j] >= MAX) _upper[j] = max_cap; kpeter@880: } kpeter@880: kpeter@881: // Initialize maps for Circulation and remove non-zero lower bounds kpeter@881: ConstMap low(0); kpeter@881: typedef typename Digraph::template ArcMap ValueArcMap; kpeter@881: typedef typename Digraph::template NodeMap ValueNodeMap; kpeter@881: ValueArcMap cap(_graph), flow(_graph); kpeter@881: ValueNodeMap sup(_graph); kpeter@881: for (NodeIt n(_graph); n != INVALID; ++n) { kpeter@881: sup[n] = _supply[_node_id[n]]; kpeter@881: } kpeter@1297: if (_has_lower) { kpeter@881: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@881: int j = _arc_idf[a]; kpeter@881: Value c = _lower[j]; kpeter@881: cap[a] = _upper[j] - c; kpeter@881: sup[_graph.source(a)] -= c; kpeter@881: sup[_graph.target(a)] += c; kpeter@881: } kpeter@881: } else { kpeter@881: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@881: cap[a] = _upper[_arc_idf[a]]; kpeter@881: } kpeter@881: } kpeter@880: kpeter@881: // Find a feasible flow using Circulation kpeter@881: Circulation, ValueArcMap, ValueNodeMap> kpeter@881: circ(_graph, low, cap, sup); kpeter@881: if (!circ.flowMap(flow).run()) return INFEASIBLE; kpeter@881: kpeter@881: // Set residual capacities and handle GEQ supply type kpeter@881: if (_sum_supply < 0) { kpeter@881: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@881: Value fa = flow[a]; kpeter@881: _res_cap[_arc_idf[a]] = cap[a] - fa; kpeter@881: _res_cap[_arc_idb[a]] = fa; kpeter@881: sup[_graph.source(a)] -= fa; kpeter@881: sup[_graph.target(a)] += fa; kpeter@881: } kpeter@881: for (NodeIt n(_graph); n != INVALID; ++n) { kpeter@881: excess[_node_id[n]] = sup[n]; kpeter@881: } kpeter@881: for (int a = _first_out[_root]; a != _res_arc_num; ++a) { kpeter@881: int u = _target[a]; kpeter@881: int ra = _reverse[a]; kpeter@881: _res_cap[a] = -_sum_supply + 1; kpeter@881: _res_cap[ra] = -excess[u]; kpeter@881: _cost[a] = 0; kpeter@881: _cost[ra] = 0; kpeter@881: } kpeter@881: } else { kpeter@881: for (ArcIt a(_graph); a != INVALID; ++a) { kpeter@881: Value fa = flow[a]; kpeter@881: _res_cap[_arc_idf[a]] = cap[a] - fa; kpeter@881: _res_cap[_arc_idb[a]] = fa; kpeter@881: } kpeter@881: for (int a = _first_out[_root]; a != _res_arc_num; ++a) { kpeter@881: int ra = _reverse[a]; kpeter@881: _res_cap[a] = 1; kpeter@881: _res_cap[ra] = 0; kpeter@881: _cost[a] = 0; kpeter@881: _cost[ra] = 0; kpeter@881: } kpeter@881: } alpar@956: kpeter@881: return OPTIMAL; kpeter@881: } alpar@1270: kpeter@1296: // Check if the upper bound is greater than or equal to the lower bound kpeter@1296: // on each forward arc. kpeter@1240: bool checkBoundMaps() { kpeter@1240: for (int j = 0; j != _res_arc_num; ++j) { kpeter@1296: if (_forward[j] && _upper[j] < _lower[j]) return false; kpeter@1240: } kpeter@1240: return true; kpeter@1240: } alpar@956: kpeter@881: // Build a StaticDigraph structure containing the current kpeter@881: // residual network kpeter@881: void buildResidualNetwork() { kpeter@881: _arc_vec.clear(); kpeter@881: _cost_vec.clear(); kpeter@881: _id_vec.clear(); kpeter@881: for (int j = 0; j != _res_arc_num; ++j) { kpeter@881: if (_res_cap[j] > 0) { kpeter@881: _arc_vec.push_back(IntPair(_source[j], _target[j])); kpeter@881: _cost_vec.push_back(_cost[j]); kpeter@881: _id_vec.push_back(j); kpeter@881: } kpeter@881: } kpeter@881: _sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); kpeter@880: } kpeter@880: kpeter@881: // Execute the algorithm and transform the results kpeter@881: void start(Method method) { kpeter@881: // Execute the algorithm kpeter@881: switch (method) { kpeter@881: case SIMPLE_CYCLE_CANCELING: kpeter@881: startSimpleCycleCanceling(); kpeter@881: break; kpeter@881: case MINIMUM_MEAN_CYCLE_CANCELING: kpeter@881: startMinMeanCycleCanceling(); kpeter@881: break; kpeter@881: case CANCEL_AND_TIGHTEN: kpeter@881: startCancelAndTighten(); kpeter@881: break; kpeter@881: } kpeter@880: kpeter@881: // Compute node potentials kpeter@881: if (method != SIMPLE_CYCLE_CANCELING) { kpeter@881: buildResidualNetwork(); kpeter@881: typename BellmanFord kpeter@881: ::template SetDistMap::Create bf(_sgr, _cost_map); kpeter@881: bf.distMap(_pi_map); kpeter@881: bf.init(0); kpeter@881: bf.start(); kpeter@880: } kpeter@881: kpeter@881: // Handle non-zero lower bounds kpeter@1297: if (_has_lower) { kpeter@881: int limit = _first_out[_root]; kpeter@881: for (int j = 0; j != limit; ++j) { kpeter@1296: if (_forward[j]) _res_cap[_reverse[j]] += _lower[j]; kpeter@881: } kpeter@881: } kpeter@880: } kpeter@880: kpeter@881: // Execute the "Simple Cycle Canceling" method kpeter@881: void startSimpleCycleCanceling() { kpeter@881: // Constants for computing the iteration limits kpeter@881: const int BF_FIRST_LIMIT = 2; kpeter@881: const double BF_LIMIT_FACTOR = 1.5; alpar@956: kpeter@886: typedef StaticVectorMap FilterMap; kpeter@881: typedef FilterArcs ResDigraph; kpeter@886: typedef StaticVectorMap PredMap; kpeter@881: typedef typename BellmanFord kpeter@881: ::template SetDistMap kpeter@881: ::template SetPredMap::Create BF; alpar@956: kpeter@881: // Build the residual network kpeter@881: _arc_vec.clear(); kpeter@881: _cost_vec.clear(); kpeter@881: for (int j = 0; j != _res_arc_num; ++j) { kpeter@881: _arc_vec.push_back(IntPair(_source[j], _target[j])); kpeter@881: _cost_vec.push_back(_cost[j]); kpeter@881: } kpeter@881: _sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); kpeter@881: kpeter@881: FilterMap filter_map(_res_cap); kpeter@881: ResDigraph rgr(_sgr, filter_map); kpeter@881: std::vector cycle; kpeter@881: std::vector pred(_res_arc_num); kpeter@881: PredMap pred_map(pred); kpeter@881: BF bf(rgr, _cost_map); kpeter@881: bf.distMap(_pi_map).predMap(pred_map); kpeter@880: kpeter@880: int length_bound = BF_FIRST_LIMIT; kpeter@880: bool optimal = false; kpeter@880: while (!optimal) { kpeter@880: bf.init(0); kpeter@880: int iter_num = 0; kpeter@880: bool cycle_found = false; kpeter@880: while (!cycle_found) { kpeter@881: // Perform some iterations of the Bellman-Ford algorithm kpeter@881: int curr_iter_num = iter_num + length_bound <= _node_num ? kpeter@881: length_bound : _node_num - iter_num; kpeter@880: iter_num += curr_iter_num; kpeter@880: int real_iter_num = curr_iter_num; kpeter@880: for (int i = 0; i < curr_iter_num; ++i) { kpeter@880: if (bf.processNextWeakRound()) { kpeter@880: real_iter_num = i; kpeter@880: break; kpeter@880: } kpeter@880: } kpeter@880: if (real_iter_num < curr_iter_num) { kpeter@880: // Optimal flow is found kpeter@880: optimal = true; kpeter@880: break; kpeter@880: } else { kpeter@881: // Search for node disjoint negative cycles kpeter@881: std::vector state(_res_node_num, 0); kpeter@880: int id = 0; kpeter@881: for (int u = 0; u != _res_node_num; ++u) { kpeter@881: if (state[u] != 0) continue; kpeter@881: ++id; kpeter@881: int v = u; kpeter@881: for (; v != -1 && state[v] == 0; v = pred[v] == INVALID ? kpeter@881: -1 : rgr.id(rgr.source(pred[v]))) { kpeter@881: state[v] = id; kpeter@880: } kpeter@881: if (v != -1 && state[v] == id) { kpeter@881: // A negative cycle is found kpeter@880: cycle_found = true; kpeter@880: cycle.clear(); kpeter@881: StaticDigraph::Arc a = pred[v]; kpeter@881: Value d, delta = _res_cap[rgr.id(a)]; kpeter@881: cycle.push_back(rgr.id(a)); kpeter@881: while (rgr.id(rgr.source(a)) != v) { kpeter@881: a = pred_map[rgr.source(a)]; kpeter@881: d = _res_cap[rgr.id(a)]; kpeter@881: if (d < delta) delta = d; kpeter@881: cycle.push_back(rgr.id(a)); kpeter@880: } kpeter@880: kpeter@881: // Augment along the cycle kpeter@881: for (int i = 0; i < int(cycle.size()); ++i) { kpeter@881: int j = cycle[i]; kpeter@881: _res_cap[j] -= delta; kpeter@881: _res_cap[_reverse[j]] += delta; kpeter@881: } kpeter@880: } kpeter@880: } kpeter@880: } kpeter@880: kpeter@881: // Increase iteration limit if no cycle is found kpeter@881: if (!cycle_found) { kpeter@881: length_bound = static_cast(length_bound * BF_LIMIT_FACTOR); kpeter@881: } kpeter@880: } kpeter@880: } kpeter@880: } kpeter@880: kpeter@881: // Execute the "Minimum Mean Cycle Canceling" method kpeter@881: void startMinMeanCycleCanceling() { kpeter@1179: typedef Path SPath; kpeter@881: typedef typename SPath::ArcIt SPathArcIt; kpeter@942: typedef typename HowardMmc kpeter@1179: ::template SetPath::Create HwMmc; kpeter@1179: typedef typename HartmannOrlinMmc kpeter@1179: ::template SetPath::Create HoMmc; kpeter@1179: kpeter@1179: const double HW_ITER_LIMIT_FACTOR = 1.0; kpeter@1179: const int HW_ITER_LIMIT_MIN_VALUE = 5; kpeter@1179: kpeter@1179: const int hw_iter_limit = kpeter@1179: std::max(static_cast(HW_ITER_LIMIT_FACTOR * _node_num), kpeter@1179: HW_ITER_LIMIT_MIN_VALUE); alpar@956: kpeter@881: SPath cycle; kpeter@1179: HwMmc hw_mmc(_sgr, _cost_map); kpeter@1179: hw_mmc.cycle(cycle); kpeter@881: buildResidualNetwork(); kpeter@1179: while (true) { alpar@1270: kpeter@1179: typename HwMmc::TerminationCause hw_tc = kpeter@1179: hw_mmc.findCycleMean(hw_iter_limit); kpeter@1179: if (hw_tc == HwMmc::ITERATION_LIMIT) { kpeter@1179: // Howard's algorithm reached the iteration limit, start a kpeter@1179: // strongly polynomial algorithm instead kpeter@1179: HoMmc ho_mmc(_sgr, _cost_map); kpeter@1179: ho_mmc.cycle(cycle); kpeter@1179: // Find a minimum mean cycle (Hartmann-Orlin algorithm) kpeter@1179: if (!(ho_mmc.findCycleMean() && ho_mmc.cycleCost() < 0)) break; kpeter@1179: ho_mmc.findCycle(); kpeter@1179: } else { kpeter@1179: // Find a minimum mean cycle (Howard algorithm) kpeter@1179: if (!(hw_tc == HwMmc::OPTIMAL && hw_mmc.cycleCost() < 0)) break; kpeter@1179: hw_mmc.findCycle(); kpeter@1179: } alpar@1270: kpeter@881: // Compute delta value kpeter@881: Value delta = INF; kpeter@881: for (SPathArcIt a(cycle); a != INVALID; ++a) { kpeter@881: Value d = _res_cap[_id_vec[_sgr.id(a)]]; kpeter@881: if (d < delta) delta = d; kpeter@881: } kpeter@880: kpeter@881: // Augment along the cycle kpeter@881: for (SPathArcIt a(cycle); a != INVALID; ++a) { kpeter@881: int j = _id_vec[_sgr.id(a)]; kpeter@881: _res_cap[j] -= delta; kpeter@881: _res_cap[_reverse[j]] += delta; kpeter@881: } kpeter@881: alpar@956: // Rebuild the residual network kpeter@881: buildResidualNetwork(); kpeter@881: } kpeter@881: } kpeter@881: kpeter@1165: // Execute the "Cancel-and-Tighten" method kpeter@881: void startCancelAndTighten() { kpeter@881: // Constants for the min mean cycle computations kpeter@881: const double LIMIT_FACTOR = 1.0; kpeter@881: const int MIN_LIMIT = 5; kpeter@1179: const double HW_ITER_LIMIT_FACTOR = 1.0; kpeter@1179: const int HW_ITER_LIMIT_MIN_VALUE = 5; kpeter@1179: kpeter@1179: const int hw_iter_limit = kpeter@1179: std::max(static_cast(HW_ITER_LIMIT_FACTOR * _node_num), kpeter@1179: HW_ITER_LIMIT_MIN_VALUE); kpeter@881: kpeter@881: // Contruct auxiliary data vectors kpeter@881: DoubleVector pi(_res_node_num, 0.0); kpeter@881: IntVector level(_res_node_num); kpeter@910: BoolVector reached(_res_node_num); kpeter@910: BoolVector processed(_res_node_num); kpeter@881: IntVector pred_node(_res_node_num); kpeter@881: IntVector pred_arc(_res_node_num); kpeter@881: std::vector stack(_res_node_num); kpeter@881: std::vector proc_vector(_res_node_num); kpeter@881: kpeter@881: // Initialize epsilon kpeter@881: double epsilon = 0; kpeter@881: for (int a = 0; a != _res_arc_num; ++a) { kpeter@881: if (_res_cap[a] > 0 && -_cost[a] > epsilon) kpeter@881: epsilon = -_cost[a]; kpeter@881: } kpeter@881: kpeter@881: // Start phases kpeter@881: Tolerance tol; kpeter@881: tol.epsilon(1e-6); kpeter@881: int limit = int(LIMIT_FACTOR * std::sqrt(double(_res_node_num))); kpeter@881: if (limit < MIN_LIMIT) limit = MIN_LIMIT; kpeter@881: int iter = limit; kpeter@881: while (epsilon * _res_node_num >= 1) { kpeter@881: // Find and cancel cycles in the admissible network using DFS kpeter@881: for (int u = 0; u != _res_node_num; ++u) { kpeter@881: reached[u] = false; kpeter@881: processed[u] = false; kpeter@881: } kpeter@881: int stack_head = -1; kpeter@881: int proc_head = -1; kpeter@881: for (int start = 0; start != _res_node_num; ++start) { kpeter@881: if (reached[start]) continue; kpeter@881: kpeter@881: // New start node kpeter@881: reached[start] = true; kpeter@881: pred_arc[start] = -1; kpeter@881: pred_node[start] = -1; kpeter@881: kpeter@881: // Find the first admissible outgoing arc kpeter@881: double p = pi[start]; kpeter@881: int a = _first_out[start]; kpeter@881: int last_out = _first_out[start+1]; kpeter@881: for (; a != last_out && (_res_cap[a] == 0 || kpeter@881: !tol.negative(_cost[a] + p - pi[_target[a]])); ++a) ; kpeter@881: if (a == last_out) { kpeter@881: processed[start] = true; kpeter@881: proc_vector[++proc_head] = start; kpeter@881: continue; kpeter@881: } kpeter@881: stack[++stack_head] = a; kpeter@881: kpeter@881: while (stack_head >= 0) { kpeter@881: int sa = stack[stack_head]; kpeter@881: int u = _source[sa]; kpeter@881: int v = _target[sa]; kpeter@881: kpeter@881: if (!reached[v]) { kpeter@881: // A new node is reached kpeter@881: reached[v] = true; kpeter@881: pred_node[v] = u; kpeter@881: pred_arc[v] = sa; kpeter@881: p = pi[v]; kpeter@881: a = _first_out[v]; kpeter@881: last_out = _first_out[v+1]; kpeter@881: for (; a != last_out && (_res_cap[a] == 0 || kpeter@881: !tol.negative(_cost[a] + p - pi[_target[a]])); ++a) ; kpeter@881: stack[++stack_head] = a == last_out ? -1 : a; kpeter@881: } else { kpeter@881: if (!processed[v]) { kpeter@881: // A cycle is found kpeter@881: int n, w = u; kpeter@881: Value d, delta = _res_cap[sa]; kpeter@881: for (n = u; n != v; n = pred_node[n]) { kpeter@881: d = _res_cap[pred_arc[n]]; kpeter@881: if (d <= delta) { kpeter@881: delta = d; kpeter@881: w = pred_node[n]; kpeter@881: } kpeter@881: } kpeter@881: kpeter@881: // Augment along the cycle kpeter@881: _res_cap[sa] -= delta; kpeter@881: _res_cap[_reverse[sa]] += delta; kpeter@881: for (n = u; n != v; n = pred_node[n]) { kpeter@881: int pa = pred_arc[n]; kpeter@881: _res_cap[pa] -= delta; kpeter@881: _res_cap[_reverse[pa]] += delta; kpeter@881: } kpeter@881: for (n = u; stack_head > 0 && n != w; n = pred_node[n]) { kpeter@881: --stack_head; kpeter@881: reached[n] = false; kpeter@881: } kpeter@881: u = w; kpeter@881: } kpeter@881: v = u; kpeter@881: kpeter@881: // Find the next admissible outgoing arc kpeter@881: p = pi[v]; kpeter@881: a = stack[stack_head] + 1; kpeter@881: last_out = _first_out[v+1]; kpeter@881: for (; a != last_out && (_res_cap[a] == 0 || kpeter@881: !tol.negative(_cost[a] + p - pi[_target[a]])); ++a) ; kpeter@881: stack[stack_head] = a == last_out ? -1 : a; kpeter@881: } kpeter@881: kpeter@881: while (stack_head >= 0 && stack[stack_head] == -1) { kpeter@881: processed[v] = true; kpeter@881: proc_vector[++proc_head] = v; kpeter@881: if (--stack_head >= 0) { kpeter@881: // Find the next admissible outgoing arc kpeter@881: v = _source[stack[stack_head]]; kpeter@881: p = pi[v]; kpeter@881: a = stack[stack_head] + 1; kpeter@881: last_out = _first_out[v+1]; kpeter@881: for (; a != last_out && (_res_cap[a] == 0 || kpeter@881: !tol.negative(_cost[a] + p - pi[_target[a]])); ++a) ; kpeter@881: stack[stack_head] = a == last_out ? -1 : a; kpeter@881: } kpeter@881: } kpeter@881: } kpeter@881: } kpeter@881: kpeter@881: // Tighten potentials and epsilon kpeter@881: if (--iter > 0) { kpeter@881: for (int u = 0; u != _res_node_num; ++u) { kpeter@881: level[u] = 0; kpeter@881: } kpeter@881: for (int i = proc_head; i > 0; --i) { kpeter@881: int u = proc_vector[i]; kpeter@881: double p = pi[u]; kpeter@881: int l = level[u] + 1; kpeter@881: int last_out = _first_out[u+1]; kpeter@881: for (int a = _first_out[u]; a != last_out; ++a) { kpeter@881: int v = _target[a]; kpeter@881: if (_res_cap[a] > 0 && tol.negative(_cost[a] + p - pi[v]) && kpeter@881: l > level[v]) level[v] = l; kpeter@881: } kpeter@880: } kpeter@880: kpeter@881: // Modify potentials kpeter@881: double q = std::numeric_limits::max(); kpeter@881: for (int u = 0; u != _res_node_num; ++u) { kpeter@881: int lu = level[u]; kpeter@881: double p, pu = pi[u]; kpeter@881: int last_out = _first_out[u+1]; kpeter@881: for (int a = _first_out[u]; a != last_out; ++a) { kpeter@881: if (_res_cap[a] == 0) continue; kpeter@881: int v = _target[a]; kpeter@881: int ld = lu - level[v]; kpeter@881: if (ld > 0) { kpeter@881: p = (_cost[a] + pu - pi[v] + epsilon) / (ld + 1); kpeter@881: if (p < q) q = p; kpeter@881: } kpeter@881: } kpeter@881: } kpeter@881: for (int u = 0; u != _res_node_num; ++u) { kpeter@881: pi[u] -= q * level[u]; kpeter@881: } kpeter@880: kpeter@881: // Modify epsilon kpeter@881: epsilon = 0; kpeter@881: for (int u = 0; u != _res_node_num; ++u) { kpeter@881: double curr, pu = pi[u]; kpeter@881: int last_out = _first_out[u+1]; kpeter@881: for (int a = _first_out[u]; a != last_out; ++a) { kpeter@881: if (_res_cap[a] == 0) continue; kpeter@881: curr = _cost[a] + pu - pi[_target[a]]; kpeter@881: if (-curr > epsilon) epsilon = -curr; kpeter@881: } kpeter@881: } kpeter@881: } else { kpeter@1179: typedef HowardMmc HwMmc; kpeter@1179: typedef HartmannOrlinMmc HoMmc; kpeter@881: typedef typename BellmanFord kpeter@881: ::template SetDistMap::Create BF; kpeter@881: kpeter@881: // Set epsilon to the minimum cycle mean kpeter@1179: Cost cycle_cost = 0; kpeter@1179: int cycle_size = 1; kpeter@881: buildResidualNetwork(); kpeter@1179: HwMmc hw_mmc(_sgr, _cost_map); kpeter@1179: if (hw_mmc.findCycleMean(hw_iter_limit) == HwMmc::ITERATION_LIMIT) { kpeter@1179: // Howard's algorithm reached the iteration limit, start a kpeter@1179: // strongly polynomial algorithm instead kpeter@1179: HoMmc ho_mmc(_sgr, _cost_map); kpeter@1179: ho_mmc.findCycleMean(); kpeter@1179: epsilon = -ho_mmc.cycleMean(); kpeter@1179: cycle_cost = ho_mmc.cycleCost(); kpeter@1179: cycle_size = ho_mmc.cycleSize(); kpeter@1179: } else { kpeter@1179: // Set epsilon kpeter@1179: epsilon = -hw_mmc.cycleMean(); kpeter@1179: cycle_cost = hw_mmc.cycleCost(); kpeter@1179: cycle_size = hw_mmc.cycleSize(); kpeter@1179: } alpar@956: kpeter@881: // Compute feasible potentials for the current epsilon kpeter@881: for (int i = 0; i != int(_cost_vec.size()); ++i) { kpeter@881: _cost_vec[i] = cycle_size * _cost_vec[i] - cycle_cost; kpeter@881: } kpeter@881: BF bf(_sgr, _cost_map); kpeter@881: bf.distMap(_pi_map); kpeter@881: bf.init(0); kpeter@881: bf.start(); kpeter@881: for (int u = 0; u != _res_node_num; ++u) { kpeter@881: pi[u] = static_cast(_pi[u]) / cycle_size; kpeter@881: } alpar@956: kpeter@881: iter = limit; kpeter@880: } kpeter@880: } kpeter@880: } kpeter@880: kpeter@880: }; //class CycleCanceling kpeter@880: kpeter@880: ///@} kpeter@880: kpeter@880: } //namespace lemon kpeter@880: kpeter@880: #endif //LEMON_CYCLE_CANCELING_H