kpeter@813: /* -*- C++ -*- kpeter@813: * kpeter@813: * This file is a part of LEMON, a generic C++ optimization library kpeter@813: * kpeter@813: * Copyright (C) 2003-2008 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@813: #ifndef LEMON_HARTMANN_ORLIN_H kpeter@813: #define LEMON_HARTMANN_ORLIN_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@813: /// \brief Default traits class of HartmannOrlin algorithm. kpeter@813: /// kpeter@813: /// Default traits class of HartmannOrlin algorithm. kpeter@813: /// \tparam GR The type of the digraph. kpeter@813: /// \tparam LEN The type of the length map. kpeter@813: /// It must conform to the \ref concepts::Rea_data "Rea_data" concept. kpeter@813: #ifdef DOXYGEN kpeter@813: template kpeter@813: #else kpeter@813: template ::is_integer> kpeter@813: #endif kpeter@813: struct HartmannOrlinDefaultTraits kpeter@813: { kpeter@813: /// The type of the digraph kpeter@813: typedef GR Digraph; kpeter@813: /// The type of the length map kpeter@813: typedef LEN LengthMap; kpeter@813: /// The type of the arc lengths kpeter@813: typedef typename LengthMap::Value Value; kpeter@813: kpeter@813: /// \brief The large value type used for internal computations kpeter@813: /// kpeter@813: /// The large value type used for internal computations. kpeter@813: /// It is \c long \c long if the \c Value type is integer, kpeter@813: /// otherwise it is \c double. kpeter@813: /// \c Value must be convertible to \c LargeValue. kpeter@813: typedef double LargeValue; kpeter@813: kpeter@813: /// The tolerance type used for internal computations kpeter@813: 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@813: // Default traits class for integer value types kpeter@813: template kpeter@813: struct HartmannOrlinDefaultTraits kpeter@813: { kpeter@813: typedef GR Digraph; kpeter@813: typedef LEN LengthMap; kpeter@813: typedef typename LengthMap::Value Value; kpeter@813: #ifdef LEMON_HAVE_LONG_LONG kpeter@813: typedef long long LargeValue; kpeter@813: #else kpeter@813: typedef long LargeValue; kpeter@813: #endif kpeter@813: 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@818: /// a directed cycle of minimum mean length (cost) in a digraph kpeter@818: /// \ref amo93networkflows, \ref dasdan98minmeancycle. kpeter@815: /// It is an improved version of \ref Karp "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@813: /// \tparam LEN The type of the length 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@891: /// algorithm. By default, it is \ref HartmannOrlinDefaultTraits kpeter@891: /// "HartmannOrlinDefaultTraits". 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@813: template kpeter@813: #else kpeter@813: template < typename GR, kpeter@813: typename LEN = typename GR::template ArcMap, kpeter@813: typename TR = HartmannOrlinDefaultTraits > kpeter@813: #endif kpeter@813: class HartmannOrlin kpeter@813: { kpeter@813: public: kpeter@813: kpeter@813: /// The type of the digraph kpeter@813: typedef typename TR::Digraph Digraph; kpeter@813: /// The type of the length map kpeter@813: typedef typename TR::LengthMap LengthMap; kpeter@813: /// The type of the arc lengths kpeter@813: typedef typename TR::Value Value; kpeter@813: kpeter@813: /// \brief The large value type kpeter@813: /// kpeter@813: /// The large value type used for internal computations. kpeter@891: /// By default, it is \c long \c long if the \c Value type is integer, kpeter@813: /// otherwise it is \c double. kpeter@813: typedef typename TR::LargeValue LargeValue; 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@813: /// Using the \ref HartmannOrlinDefaultTraits "default traits class", kpeter@813: /// it is \ref lemon::Path "Path". kpeter@813: typedef typename TR::Path Path; kpeter@813: kpeter@813: /// The \ref HartmannOrlinDefaultTraits "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@813: LargeValue dist; kpeter@813: Arc pred; kpeter@814: PathData(LargeValue 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@813: // The length of the arcs kpeter@813: const LengthMap &_length; 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@813: LargeValue _curr_length, _best_length; 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@814: const LargeValue INF; kpeter@814: kpeter@813: public: kpeter@813: kpeter@813: /// \name Named Template Parameters kpeter@813: /// @{ kpeter@813: kpeter@813: template kpeter@813: struct SetLargeValueTraits : public Traits { kpeter@813: typedef T LargeValue; kpeter@813: typedef lemon::Tolerance Tolerance; kpeter@813: }; kpeter@813: kpeter@813: /// \brief \ref named-templ-param "Named parameter" for setting kpeter@813: /// \c LargeValue type. kpeter@813: /// kpeter@813: /// \ref named-templ-param "Named parameter" for setting \c LargeValue kpeter@813: /// type. It is used for internal computations in the algorithm. kpeter@813: template kpeter@813: struct SetLargeValue kpeter@813: : public HartmannOrlin > { kpeter@813: typedef HartmannOrlin > 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@813: : public HartmannOrlin > { kpeter@813: typedef HartmannOrlin > Create; kpeter@813: }; kpeter@813: kpeter@813: /// @} kpeter@813: kpeter@941: protected: kpeter@941: kpeter@941: HartmannOrlin() {} 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@813: /// \param length The lengths (costs) of the arcs. kpeter@813: HartmannOrlin( const Digraph &digraph, kpeter@813: const LengthMap &length ) : kpeter@813: _gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph), kpeter@813: _best_found(false), _best_length(0), _best_size(1), kpeter@814: _cycle_path(NULL), _local_path(false), _data(digraph), kpeter@814: INF(std::numeric_limits::has_infinity ? kpeter@814: std::numeric_limits::infinity() : kpeter@814: std::numeric_limits::max()) kpeter@813: {} kpeter@813: kpeter@813: /// Destructor. kpeter@813: ~HartmannOrlin() { 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@813: /// \ref findMinMean(), 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@813: HartmannOrlin& 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@816: HartmannOrlin& 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@813: /// If you only need the minimum mean length, you may call kpeter@813: /// \ref findMinMean(). 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@813: /// and/or the arc lengths 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@813: /// return mmc.findMinMean() && mmc.findCycle(); kpeter@813: /// \endcode kpeter@813: bool run() { kpeter@813: return findMinMean() && findCycle(); kpeter@813: } kpeter@813: kpeter@813: /// \brief Find the minimum cycle mean. kpeter@813: /// kpeter@813: /// This function finds the minimum mean length 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@813: bool findMinMean() { kpeter@813: // Initialization and find strongly connected components kpeter@813: init(); kpeter@813: findComponents(); kpeter@813: 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(); kpeter@813: kpeter@813: // Update the best cycle (global minimum mean cycle) kpeter@813: if ( _curr_found && (!_best_found || kpeter@813: _curr_length * _best_size < _best_length * _curr_size) ) { kpeter@813: _best_found = true; kpeter@813: _best_length = _curr_length; 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@813: /// This function finds a directed cycle of minimum mean length kpeter@813: /// in the digraph using the data computed by findMinMean(). kpeter@813: /// kpeter@813: /// \return \c true if a directed cycle exists in the digraph. kpeter@813: /// kpeter@813: /// \pre \ref findMinMean() 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@813: _best_length = _length[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@813: _best_length += _length[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@813: /// \brief Return the total length of the found cycle. kpeter@813: /// kpeter@813: /// This function returns the total length of the found cycle. kpeter@813: /// kpeter@813: /// \pre \ref run() or \ref findMinMean() must be called before kpeter@813: /// using this function. kpeter@914: Value cycleLength() const { kpeter@914: return static_cast(_best_length); 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@813: /// \pre \ref run() or \ref findMinMean() must be called before kpeter@813: /// using this function. kpeter@813: int cycleArcNum() const { kpeter@813: return _best_size; kpeter@813: } kpeter@813: kpeter@813: /// \brief Return the mean length of the found cycle. kpeter@813: /// kpeter@813: /// This function returns the mean length 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@813: /// return static_cast(alg.cycleLength()) / alg.cycleArcNum(); kpeter@813: /// \endcode kpeter@813: /// kpeter@813: /// \pre \ref run() or \ref findMinMean() must be called before kpeter@813: /// using this function. kpeter@813: double cycleMean() const { kpeter@813: return static_cast(_best_length) / _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@813: _best_length = 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; kpeter@813: } 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@813: // _data[v][k] is the length 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@813: LargeValue 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@813: d = _data[u][k-1].dist + _length[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@813: LargeValue 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@813: d = _data[u][k-1].dist + _length[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: } kpeter@813: 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@813: typename GR::template NodeMap pi(_gr); kpeter@813: int n = _nodes->size(); kpeter@813: LargeValue length; kpeter@813: int size; kpeter@813: Node u; kpeter@813: 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@813: length = _data[u][level[u].second].dist - _data[u][j].dist; kpeter@813: size = level[u].second - j; kpeter@813: if (!_curr_found || length * _curr_size < _curr_length * size) { kpeter@813: _curr_length = length; 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) { deba@859: u = _gr.source(_data[u][j].pred); deba@859: } kpeter@813: } kpeter@813: } kpeter@813: kpeter@813: // If at least one cycle is found, check the optimality condition kpeter@813: LargeValue 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@814: d = _data[u][j].dist * _curr_size - j * _curr_length; 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@813: if (_tolerance.less(_length[a] * _curr_size - _curr_length, 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@813: }; //class HartmannOrlin kpeter@813: kpeter@813: ///@} kpeter@813: kpeter@813: } //namespace lemon kpeter@813: kpeter@813: #endif //LEMON_HARTMANN_ORLIN_H