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