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