alpar@956: /* -*- mode: C++; indent-tabs-mode: nil; -*- kpeter@813: * alpar@956: * This file is a part of LEMON, a generic C++ optimization library. kpeter@813: * alpar@956: * Copyright (C) 2003-2010 kpeter@813: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport kpeter@813: * (Egervary Research Group on Combinatorial Optimization, EGRES). kpeter@813: * kpeter@813: * Permission to use, modify and distribute this software is granted kpeter@813: * provided that this copyright notice appears in all copies. For kpeter@813: * precise terms see the accompanying LICENSE file. kpeter@813: * kpeter@813: * This software is provided "AS IS" with no warranty of any kind, kpeter@813: * express or implied, and with no claim as to its suitability for any kpeter@813: * purpose. kpeter@813: * kpeter@813: */ kpeter@813: kpeter@942: #ifndef LEMON_HARTMANN_ORLIN_MMC_H kpeter@942: #define LEMON_HARTMANN_ORLIN_MMC_H kpeter@813: kpeter@815: /// \ingroup min_mean_cycle kpeter@813: /// kpeter@813: /// \file kpeter@813: /// \brief Hartmann-Orlin's algorithm for finding a minimum mean cycle. kpeter@813: kpeter@813: #include kpeter@813: #include kpeter@813: #include kpeter@813: #include kpeter@813: #include kpeter@813: #include kpeter@813: kpeter@813: namespace lemon { kpeter@813: kpeter@942: /// \brief Default traits class of HartmannOrlinMmc class. kpeter@813: /// kpeter@942: /// Default traits class of HartmannOrlinMmc class. kpeter@813: /// \tparam GR The type of the digraph. kpeter@942: /// \tparam CM The type of the cost map. kpeter@959: /// It must conform to the \ref concepts::ReadMap "ReadMap" concept. kpeter@813: #ifdef DOXYGEN kpeter@942: template kpeter@813: #else kpeter@942: template ::is_integer> kpeter@813: #endif kpeter@942: struct HartmannOrlinMmcDefaultTraits kpeter@813: { kpeter@813: /// The type of the digraph kpeter@813: typedef GR Digraph; kpeter@942: /// The type of the cost map kpeter@942: typedef CM CostMap; kpeter@942: /// The type of the arc costs kpeter@942: typedef typename CostMap::Value Cost; kpeter@813: kpeter@942: /// \brief The large cost type used for internal computations kpeter@813: /// kpeter@942: /// The large cost type used for internal computations. kpeter@942: /// It is \c long \c long if the \c Cost type is integer, kpeter@813: /// otherwise it is \c double. kpeter@942: /// \c Cost must be convertible to \c LargeCost. kpeter@942: typedef double LargeCost; kpeter@813: kpeter@813: /// The tolerance type used for internal computations kpeter@942: typedef lemon::Tolerance Tolerance; kpeter@813: kpeter@813: /// \brief The path type of the found cycles kpeter@813: /// kpeter@813: /// The path type of the found cycles. kpeter@813: /// It must conform to the \ref lemon::concepts::Path "Path" concept kpeter@819: /// and it must have an \c addFront() function. kpeter@813: typedef lemon::Path Path; kpeter@813: }; kpeter@813: kpeter@942: // Default traits class for integer cost types kpeter@942: template kpeter@942: struct HartmannOrlinMmcDefaultTraits kpeter@813: { kpeter@813: typedef GR Digraph; kpeter@942: typedef CM CostMap; kpeter@942: typedef typename CostMap::Value Cost; kpeter@813: #ifdef LEMON_HAVE_LONG_LONG kpeter@942: typedef long long LargeCost; kpeter@813: #else kpeter@942: typedef long LargeCost; kpeter@813: #endif kpeter@942: typedef lemon::Tolerance Tolerance; kpeter@813: typedef lemon::Path Path; kpeter@813: }; kpeter@813: kpeter@813: kpeter@815: /// \addtogroup min_mean_cycle kpeter@813: /// @{ kpeter@813: kpeter@813: /// \brief Implementation of the Hartmann-Orlin algorithm for finding kpeter@813: /// a minimum mean cycle. kpeter@813: /// kpeter@813: /// This class implements the Hartmann-Orlin algorithm for finding kpeter@942: /// a directed cycle of minimum mean cost in a digraph kpeter@818: /// \ref amo93networkflows, \ref dasdan98minmeancycle. kpeter@959: /// It is an improved version of \ref KarpMmc "Karp"'s original algorithm, kpeter@813: /// it applies an efficient early termination scheme. kpeter@815: /// It runs in time O(ne) and uses space O(n2+e). kpeter@813: /// kpeter@813: /// \tparam GR The type of the digraph the algorithm runs on. kpeter@942: /// \tparam CM The type of the cost map. The default kpeter@813: /// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap". kpeter@891: /// \tparam TR The traits class that defines various types used by the kpeter@942: /// algorithm. By default, it is \ref HartmannOrlinMmcDefaultTraits kpeter@942: /// "HartmannOrlinMmcDefaultTraits". kpeter@891: /// In most cases, this parameter should not be set directly, kpeter@891: /// consider to use the named template parameters instead. kpeter@813: #ifdef DOXYGEN kpeter@942: template kpeter@813: #else kpeter@813: template < typename GR, kpeter@942: typename CM = typename GR::template ArcMap, kpeter@942: typename TR = HartmannOrlinMmcDefaultTraits > kpeter@813: #endif kpeter@942: class HartmannOrlinMmc kpeter@813: { kpeter@813: public: kpeter@813: kpeter@813: /// The type of the digraph kpeter@813: typedef typename TR::Digraph Digraph; kpeter@942: /// The type of the cost map kpeter@942: typedef typename TR::CostMap CostMap; kpeter@942: /// The type of the arc costs kpeter@942: typedef typename TR::Cost Cost; kpeter@813: kpeter@942: /// \brief The large cost type kpeter@813: /// kpeter@942: /// The large cost type used for internal computations. kpeter@942: /// By default, it is \c long \c long if the \c Cost type is integer, kpeter@813: /// otherwise it is \c double. kpeter@942: typedef typename TR::LargeCost LargeCost; kpeter@813: kpeter@813: /// The tolerance type kpeter@813: typedef typename TR::Tolerance Tolerance; kpeter@813: kpeter@813: /// \brief The path type of the found cycles kpeter@813: /// kpeter@813: /// The path type of the found cycles. kpeter@942: /// Using the \ref HartmannOrlinMmcDefaultTraits "default traits class", kpeter@813: /// it is \ref lemon::Path "Path". kpeter@813: typedef typename TR::Path Path; kpeter@813: kpeter@942: /// The \ref HartmannOrlinMmcDefaultTraits "traits class" of the algorithm kpeter@813: typedef TR Traits; kpeter@813: kpeter@813: private: kpeter@813: kpeter@813: TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); kpeter@813: kpeter@813: // Data sturcture for path data kpeter@813: struct PathData kpeter@813: { kpeter@942: LargeCost dist; kpeter@813: Arc pred; kpeter@942: PathData(LargeCost d, Arc p = INVALID) : kpeter@814: dist(d), pred(p) {} kpeter@813: }; kpeter@813: kpeter@813: typedef typename Digraph::template NodeMap > kpeter@813: PathDataNodeMap; kpeter@813: kpeter@813: private: kpeter@813: kpeter@813: // The digraph the algorithm runs on kpeter@813: const Digraph &_gr; kpeter@942: // The cost of the arcs kpeter@942: const CostMap &_cost; kpeter@813: kpeter@813: // Data for storing the strongly connected components kpeter@813: int _comp_num; kpeter@813: typename Digraph::template NodeMap _comp; kpeter@813: std::vector > _comp_nodes; kpeter@813: std::vector* _nodes; kpeter@813: typename Digraph::template NodeMap > _out_arcs; kpeter@813: kpeter@813: // Data for the found cycles kpeter@813: bool _curr_found, _best_found; kpeter@942: LargeCost _curr_cost, _best_cost; kpeter@813: int _curr_size, _best_size; kpeter@813: Node _curr_node, _best_node; kpeter@813: int _curr_level, _best_level; kpeter@813: kpeter@813: Path *_cycle_path; kpeter@813: bool _local_path; kpeter@813: kpeter@813: // Node map for storing path data kpeter@813: PathDataNodeMap _data; kpeter@813: // The processed nodes in the last round kpeter@813: std::vector _process; kpeter@813: kpeter@813: Tolerance _tolerance; kpeter@813: kpeter@814: // Infinite constant kpeter@942: const LargeCost INF; kpeter@814: kpeter@813: public: kpeter@813: kpeter@813: /// \name Named Template Parameters kpeter@813: /// @{ kpeter@813: kpeter@813: template kpeter@942: struct SetLargeCostTraits : public Traits { kpeter@942: typedef T LargeCost; kpeter@813: typedef lemon::Tolerance Tolerance; kpeter@813: }; kpeter@813: kpeter@813: /// \brief \ref named-templ-param "Named parameter" for setting kpeter@942: /// \c LargeCost type. kpeter@813: /// kpeter@942: /// \ref named-templ-param "Named parameter" for setting \c LargeCost kpeter@813: /// type. It is used for internal computations in the algorithm. kpeter@813: template kpeter@942: struct SetLargeCost kpeter@942: : public HartmannOrlinMmc > { kpeter@942: typedef HartmannOrlinMmc > Create; kpeter@813: }; kpeter@813: kpeter@813: template kpeter@813: struct SetPathTraits : public Traits { kpeter@813: typedef T Path; kpeter@813: }; kpeter@813: kpeter@813: /// \brief \ref named-templ-param "Named parameter" for setting kpeter@813: /// \c %Path type. kpeter@813: /// kpeter@813: /// \ref named-templ-param "Named parameter" for setting the \c %Path kpeter@813: /// type of the found cycles. kpeter@813: /// It must conform to the \ref lemon::concepts::Path "Path" concept kpeter@813: /// and it must have an \c addFront() function. kpeter@813: template kpeter@813: struct SetPath kpeter@942: : public HartmannOrlinMmc > { kpeter@942: typedef HartmannOrlinMmc > Create; kpeter@813: }; kpeter@813: kpeter@813: /// @} kpeter@813: kpeter@941: protected: kpeter@941: kpeter@942: HartmannOrlinMmc() {} kpeter@941: kpeter@813: public: kpeter@813: kpeter@813: /// \brief Constructor. kpeter@813: /// kpeter@813: /// The constructor of the class. kpeter@813: /// kpeter@813: /// \param digraph The digraph the algorithm runs on. kpeter@942: /// \param cost The costs of the arcs. kpeter@942: HartmannOrlinMmc( const Digraph &digraph, kpeter@942: const CostMap &cost ) : kpeter@942: _gr(digraph), _cost(cost), _comp(digraph), _out_arcs(digraph), kpeter@942: _best_found(false), _best_cost(0), _best_size(1), kpeter@814: _cycle_path(NULL), _local_path(false), _data(digraph), kpeter@942: INF(std::numeric_limits::has_infinity ? kpeter@942: std::numeric_limits::infinity() : kpeter@942: std::numeric_limits::max()) kpeter@813: {} kpeter@813: kpeter@813: /// Destructor. kpeter@942: ~HartmannOrlinMmc() { kpeter@813: if (_local_path) delete _cycle_path; kpeter@813: } kpeter@813: kpeter@813: /// \brief Set the path structure for storing the found cycle. kpeter@813: /// kpeter@813: /// This function sets an external path structure for storing the kpeter@813: /// found cycle. kpeter@813: /// kpeter@813: /// If you don't call this function before calling \ref run() or kpeter@942: /// \ref findCycleMean(), it will allocate a local \ref Path "path" kpeter@813: /// structure. The destuctor deallocates this automatically kpeter@813: /// allocated object, of course. kpeter@813: /// kpeter@813: /// \note The algorithm calls only the \ref lemon::Path::addFront() kpeter@813: /// "addFront()" function of the given path structure. kpeter@813: /// kpeter@813: /// \return (*this) kpeter@942: HartmannOrlinMmc& cycle(Path &path) { kpeter@813: if (_local_path) { kpeter@813: delete _cycle_path; kpeter@813: _local_path = false; kpeter@813: } kpeter@813: _cycle_path = &path; kpeter@813: return *this; kpeter@813: } kpeter@813: kpeter@816: /// \brief Set the tolerance used by the algorithm. kpeter@816: /// kpeter@816: /// This function sets the tolerance object used by the algorithm. kpeter@816: /// kpeter@816: /// \return (*this) kpeter@942: HartmannOrlinMmc& tolerance(const Tolerance& tolerance) { kpeter@816: _tolerance = tolerance; kpeter@816: return *this; kpeter@816: } kpeter@816: kpeter@816: /// \brief Return a const reference to the tolerance. kpeter@816: /// kpeter@816: /// This function returns a const reference to the tolerance object kpeter@816: /// used by the algorithm. kpeter@816: const Tolerance& tolerance() const { kpeter@816: return _tolerance; kpeter@816: } kpeter@816: kpeter@813: /// \name Execution control kpeter@813: /// The simplest way to execute the algorithm is to call the \ref run() kpeter@813: /// function.\n kpeter@942: /// If you only need the minimum mean cost, you may call kpeter@942: /// \ref findCycleMean(). kpeter@813: kpeter@813: /// @{ kpeter@813: kpeter@813: /// \brief Run the algorithm. kpeter@813: /// kpeter@813: /// This function runs the algorithm. kpeter@813: /// It can be called more than once (e.g. if the underlying digraph kpeter@942: /// and/or the arc costs have been modified). kpeter@813: /// kpeter@813: /// \return \c true if a directed cycle exists in the digraph. kpeter@813: /// kpeter@813: /// \note mmc.run() is just a shortcut of the following code. kpeter@813: /// \code kpeter@942: /// return mmc.findCycleMean() && mmc.findCycle(); kpeter@813: /// \endcode kpeter@813: bool run() { kpeter@942: return findCycleMean() && findCycle(); kpeter@813: } kpeter@813: kpeter@813: /// \brief Find the minimum cycle mean. kpeter@813: /// kpeter@942: /// This function finds the minimum mean cost of the directed kpeter@813: /// cycles in the digraph. kpeter@813: /// kpeter@813: /// \return \c true if a directed cycle exists in the digraph. kpeter@942: bool findCycleMean() { kpeter@813: // Initialization and find strongly connected components kpeter@813: init(); kpeter@813: findComponents(); alpar@956: kpeter@813: // Find the minimum cycle mean in the components kpeter@813: for (int comp = 0; comp < _comp_num; ++comp) { kpeter@813: if (!initComponent(comp)) continue; kpeter@813: processRounds(); alpar@956: kpeter@813: // Update the best cycle (global minimum mean cycle) alpar@956: if ( _curr_found && (!_best_found || kpeter@942: _curr_cost * _best_size < _best_cost * _curr_size) ) { kpeter@813: _best_found = true; kpeter@942: _best_cost = _curr_cost; kpeter@813: _best_size = _curr_size; kpeter@813: _best_node = _curr_node; kpeter@813: _best_level = _curr_level; kpeter@813: } kpeter@813: } kpeter@813: return _best_found; kpeter@813: } kpeter@813: kpeter@813: /// \brief Find a minimum mean directed cycle. kpeter@813: /// kpeter@942: /// This function finds a directed cycle of minimum mean cost kpeter@942: /// in the digraph using the data computed by findCycleMean(). kpeter@813: /// kpeter@813: /// \return \c true if a directed cycle exists in the digraph. kpeter@813: /// kpeter@942: /// \pre \ref findCycleMean() must be called before using this function. kpeter@813: bool findCycle() { kpeter@813: if (!_best_found) return false; kpeter@813: IntNodeMap reached(_gr, -1); kpeter@813: int r = _best_level + 1; kpeter@813: Node u = _best_node; kpeter@813: while (reached[u] < 0) { kpeter@813: reached[u] = --r; kpeter@813: u = _gr.source(_data[u][r].pred); kpeter@813: } kpeter@813: r = reached[u]; kpeter@813: Arc e = _data[u][r].pred; kpeter@813: _cycle_path->addFront(e); kpeter@942: _best_cost = _cost[e]; kpeter@813: _best_size = 1; kpeter@813: Node v; kpeter@813: while ((v = _gr.source(e)) != u) { kpeter@813: e = _data[v][--r].pred; kpeter@813: _cycle_path->addFront(e); kpeter@942: _best_cost += _cost[e]; kpeter@813: ++_best_size; kpeter@813: } kpeter@813: return true; kpeter@813: } kpeter@813: kpeter@813: /// @} kpeter@813: kpeter@813: /// \name Query Functions kpeter@813: /// The results of the algorithm can be obtained using these kpeter@813: /// functions.\n kpeter@813: /// The algorithm should be executed before using them. kpeter@813: kpeter@813: /// @{ kpeter@813: kpeter@942: /// \brief Return the total cost of the found cycle. kpeter@813: /// kpeter@942: /// This function returns the total cost of the found cycle. kpeter@813: /// kpeter@942: /// \pre \ref run() or \ref findCycleMean() must be called before kpeter@813: /// using this function. kpeter@942: Cost cycleCost() const { kpeter@942: return static_cast(_best_cost); kpeter@813: } kpeter@813: kpeter@813: /// \brief Return the number of arcs on the found cycle. kpeter@813: /// kpeter@813: /// This function returns the number of arcs on the found cycle. kpeter@813: /// kpeter@942: /// \pre \ref run() or \ref findCycleMean() must be called before kpeter@813: /// using this function. kpeter@942: int cycleSize() const { kpeter@813: return _best_size; kpeter@813: } kpeter@813: kpeter@942: /// \brief Return the mean cost of the found cycle. kpeter@813: /// kpeter@942: /// This function returns the mean cost of the found cycle. kpeter@813: /// kpeter@813: /// \note alg.cycleMean() is just a shortcut of the kpeter@813: /// following code. kpeter@813: /// \code kpeter@942: /// return static_cast(alg.cycleCost()) / alg.cycleSize(); kpeter@813: /// \endcode kpeter@813: /// kpeter@942: /// \pre \ref run() or \ref findCycleMean() must be called before kpeter@813: /// using this function. kpeter@813: double cycleMean() const { kpeter@942: return static_cast(_best_cost) / _best_size; kpeter@813: } kpeter@813: kpeter@813: /// \brief Return the found cycle. kpeter@813: /// kpeter@813: /// This function returns a const reference to the path structure kpeter@813: /// storing the found cycle. kpeter@813: /// kpeter@813: /// \pre \ref run() or \ref findCycle() must be called before using kpeter@813: /// this function. kpeter@813: const Path& cycle() const { kpeter@813: return *_cycle_path; kpeter@813: } kpeter@813: kpeter@813: ///@} kpeter@813: kpeter@813: private: kpeter@813: kpeter@813: // Initialization kpeter@813: void init() { kpeter@813: if (!_cycle_path) { kpeter@813: _local_path = true; kpeter@813: _cycle_path = new Path; kpeter@813: } kpeter@813: _cycle_path->clear(); kpeter@813: _best_found = false; kpeter@942: _best_cost = 0; kpeter@813: _best_size = 1; kpeter@813: _cycle_path->clear(); kpeter@813: for (NodeIt u(_gr); u != INVALID; ++u) kpeter@813: _data[u].clear(); kpeter@813: } kpeter@813: kpeter@813: // Find strongly connected components and initialize _comp_nodes kpeter@813: // and _out_arcs kpeter@813: void findComponents() { kpeter@813: _comp_num = stronglyConnectedComponents(_gr, _comp); kpeter@813: _comp_nodes.resize(_comp_num); kpeter@813: if (_comp_num == 1) { kpeter@813: _comp_nodes[0].clear(); kpeter@813: for (NodeIt n(_gr); n != INVALID; ++n) { kpeter@813: _comp_nodes[0].push_back(n); kpeter@813: _out_arcs[n].clear(); kpeter@813: for (OutArcIt a(_gr, n); a != INVALID; ++a) { kpeter@813: _out_arcs[n].push_back(a); kpeter@813: } kpeter@813: } kpeter@813: } else { kpeter@813: for (int i = 0; i < _comp_num; ++i) kpeter@813: _comp_nodes[i].clear(); kpeter@813: for (NodeIt n(_gr); n != INVALID; ++n) { kpeter@813: int k = _comp[n]; kpeter@813: _comp_nodes[k].push_back(n); kpeter@813: _out_arcs[n].clear(); kpeter@813: for (OutArcIt a(_gr, n); a != INVALID; ++a) { kpeter@813: if (_comp[_gr.target(a)] == k) _out_arcs[n].push_back(a); kpeter@813: } kpeter@813: } kpeter@813: } kpeter@813: } kpeter@813: kpeter@813: // Initialize path data for the current component kpeter@813: bool initComponent(int comp) { kpeter@813: _nodes = &(_comp_nodes[comp]); kpeter@813: int n = _nodes->size(); kpeter@813: if (n < 1 || (n == 1 && _out_arcs[(*_nodes)[0]].size() == 0)) { kpeter@813: return false; alpar@956: } kpeter@813: for (int i = 0; i < n; ++i) { kpeter@814: _data[(*_nodes)[i]].resize(n + 1, PathData(INF)); kpeter@813: } kpeter@813: return true; kpeter@813: } kpeter@813: kpeter@813: // Process all rounds of computing path data for the current component. kpeter@942: // _data[v][k] is the cost of a shortest directed walk from the root kpeter@813: // node to node v containing exactly k arcs. kpeter@813: void processRounds() { kpeter@813: Node start = (*_nodes)[0]; kpeter@814: _data[start][0] = PathData(0); kpeter@813: _process.clear(); kpeter@813: _process.push_back(start); kpeter@813: kpeter@813: int k, n = _nodes->size(); kpeter@813: int next_check = 4; kpeter@813: bool terminate = false; kpeter@813: for (k = 1; k <= n && int(_process.size()) < n && !terminate; ++k) { kpeter@813: processNextBuildRound(k); kpeter@813: if (k == next_check || k == n) { kpeter@813: terminate = checkTermination(k); kpeter@813: next_check = next_check * 3 / 2; kpeter@813: } kpeter@813: } kpeter@813: for ( ; k <= n && !terminate; ++k) { kpeter@813: processNextFullRound(k); kpeter@813: if (k == next_check || k == n) { kpeter@813: terminate = checkTermination(k); kpeter@813: next_check = next_check * 3 / 2; kpeter@813: } kpeter@813: } kpeter@813: } kpeter@813: kpeter@813: // Process one round and rebuild _process kpeter@813: void processNextBuildRound(int k) { kpeter@813: std::vector next; kpeter@813: Node u, v; kpeter@813: Arc e; kpeter@942: LargeCost d; kpeter@813: for (int i = 0; i < int(_process.size()); ++i) { kpeter@813: u = _process[i]; kpeter@813: for (int j = 0; j < int(_out_arcs[u].size()); ++j) { kpeter@813: e = _out_arcs[u][j]; kpeter@813: v = _gr.target(e); kpeter@942: d = _data[u][k-1].dist + _cost[e]; kpeter@814: if (_tolerance.less(d, _data[v][k].dist)) { kpeter@814: if (_data[v][k].dist == INF) next.push_back(v); kpeter@814: _data[v][k] = PathData(d, e); kpeter@813: } kpeter@813: } kpeter@813: } kpeter@813: _process.swap(next); kpeter@813: } kpeter@813: kpeter@813: // Process one round using _nodes instead of _process kpeter@813: void processNextFullRound(int k) { kpeter@813: Node u, v; kpeter@813: Arc e; kpeter@942: LargeCost d; kpeter@813: for (int i = 0; i < int(_nodes->size()); ++i) { kpeter@813: u = (*_nodes)[i]; kpeter@813: for (int j = 0; j < int(_out_arcs[u].size()); ++j) { kpeter@813: e = _out_arcs[u][j]; kpeter@813: v = _gr.target(e); kpeter@942: d = _data[u][k-1].dist + _cost[e]; kpeter@814: if (_tolerance.less(d, _data[v][k].dist)) { kpeter@814: _data[v][k] = PathData(d, e); kpeter@813: } kpeter@813: } kpeter@813: } kpeter@813: } alpar@956: kpeter@813: // Check early termination kpeter@813: bool checkTermination(int k) { kpeter@813: typedef std::pair Pair; kpeter@813: typename GR::template NodeMap level(_gr, Pair(-1, 0)); kpeter@942: typename GR::template NodeMap pi(_gr); kpeter@813: int n = _nodes->size(); kpeter@942: LargeCost cost; kpeter@813: int size; kpeter@813: Node u; alpar@956: kpeter@813: // Search for cycles that are already found kpeter@813: _curr_found = false; kpeter@813: for (int i = 0; i < n; ++i) { kpeter@813: u = (*_nodes)[i]; kpeter@814: if (_data[u][k].dist == INF) continue; kpeter@813: for (int j = k; j >= 0; --j) { kpeter@813: if (level[u].first == i && level[u].second > 0) { kpeter@813: // A cycle is found kpeter@942: cost = _data[u][level[u].second].dist - _data[u][j].dist; kpeter@813: size = level[u].second - j; kpeter@942: if (!_curr_found || cost * _curr_size < _curr_cost * size) { kpeter@942: _curr_cost = cost; kpeter@813: _curr_size = size; kpeter@813: _curr_node = u; kpeter@813: _curr_level = level[u].second; kpeter@813: _curr_found = true; kpeter@813: } kpeter@813: } kpeter@813: level[u] = Pair(i, j); deba@859: if (j != 0) { alpar@956: u = _gr.source(_data[u][j].pred); alpar@956: } kpeter@813: } kpeter@813: } kpeter@813: kpeter@813: // If at least one cycle is found, check the optimality condition kpeter@942: LargeCost d; kpeter@813: if (_curr_found && k < n) { kpeter@813: // Find node potentials kpeter@813: for (int i = 0; i < n; ++i) { kpeter@813: u = (*_nodes)[i]; kpeter@814: pi[u] = INF; kpeter@813: for (int j = 0; j <= k; ++j) { kpeter@814: if (_data[u][j].dist < INF) { kpeter@942: d = _data[u][j].dist * _curr_size - j * _curr_cost; kpeter@814: if (_tolerance.less(d, pi[u])) pi[u] = d; kpeter@813: } kpeter@813: } kpeter@813: } kpeter@813: kpeter@813: // Check the optimality condition for all arcs kpeter@813: bool done = true; kpeter@813: for (ArcIt a(_gr); a != INVALID; ++a) { kpeter@942: if (_tolerance.less(_cost[a] * _curr_size - _curr_cost, kpeter@813: pi[_gr.target(a)] - pi[_gr.source(a)]) ) { kpeter@813: done = false; kpeter@813: break; kpeter@813: } kpeter@813: } kpeter@813: return done; kpeter@813: } kpeter@813: return (k == n); kpeter@813: } kpeter@813: kpeter@942: }; //class HartmannOrlinMmc kpeter@813: kpeter@813: ///@} kpeter@813: kpeter@813: } //namespace lemon kpeter@813: kpeter@942: #endif //LEMON_HARTMANN_ORLIN_MMC_H