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