lemon/hartmann_orlin.h
changeset 766 97744b6dabf8
child 767 11c946fa8d13
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/hartmann_orlin.h	Tue Aug 11 21:53:39 2009 +0200
     1.3 @@ -0,0 +1,618 @@
     1.4 +/* -*- C++ -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library
     1.7 + *
     1.8 + * Copyright (C) 2003-2008
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#ifndef LEMON_HARTMANN_ORLIN_H
    1.23 +#define LEMON_HARTMANN_ORLIN_H
    1.24 +
    1.25 +/// \ingroup shortest_path
    1.26 +///
    1.27 +/// \file
    1.28 +/// \brief Hartmann-Orlin's algorithm for finding a minimum mean cycle.
    1.29 +
    1.30 +#include <vector>
    1.31 +#include <limits>
    1.32 +#include <lemon/core.h>
    1.33 +#include <lemon/path.h>
    1.34 +#include <lemon/tolerance.h>
    1.35 +#include <lemon/connectivity.h>
    1.36 +
    1.37 +namespace lemon {
    1.38 +
    1.39 +  /// \brief Default traits class of HartmannOrlin algorithm.
    1.40 +  ///
    1.41 +  /// Default traits class of HartmannOrlin algorithm.
    1.42 +  /// \tparam GR The type of the digraph.
    1.43 +  /// \tparam LEN The type of the length map.
    1.44 +  /// It must conform to the \ref concepts::Rea_data "Rea_data" concept.
    1.45 +#ifdef DOXYGEN
    1.46 +  template <typename GR, typename LEN>
    1.47 +#else
    1.48 +  template <typename GR, typename LEN,
    1.49 +    bool integer = std::numeric_limits<typename LEN::Value>::is_integer>
    1.50 +#endif
    1.51 +  struct HartmannOrlinDefaultTraits
    1.52 +  {
    1.53 +    /// The type of the digraph
    1.54 +    typedef GR Digraph;
    1.55 +    /// The type of the length map
    1.56 +    typedef LEN LengthMap;
    1.57 +    /// The type of the arc lengths
    1.58 +    typedef typename LengthMap::Value Value;
    1.59 +
    1.60 +    /// \brief The large value type used for internal computations
    1.61 +    ///
    1.62 +    /// The large value type used for internal computations.
    1.63 +    /// It is \c long \c long if the \c Value type is integer,
    1.64 +    /// otherwise it is \c double.
    1.65 +    /// \c Value must be convertible to \c LargeValue.
    1.66 +    typedef double LargeValue;
    1.67 +
    1.68 +    /// The tolerance type used for internal computations
    1.69 +    typedef lemon::Tolerance<LargeValue> Tolerance;
    1.70 +
    1.71 +    /// \brief The path type of the found cycles
    1.72 +    ///
    1.73 +    /// The path type of the found cycles.
    1.74 +    /// It must conform to the \ref lemon::concepts::Path "Path" concept
    1.75 +    /// and it must have an \c addBack() function.
    1.76 +    typedef lemon::Path<Digraph> Path;
    1.77 +  };
    1.78 +
    1.79 +  // Default traits class for integer value types
    1.80 +  template <typename GR, typename LEN>
    1.81 +  struct HartmannOrlinDefaultTraits<GR, LEN, true>
    1.82 +  {
    1.83 +    typedef GR Digraph;
    1.84 +    typedef LEN LengthMap;
    1.85 +    typedef typename LengthMap::Value Value;
    1.86 +#ifdef LEMON_HAVE_LONG_LONG
    1.87 +    typedef long long LargeValue;
    1.88 +#else
    1.89 +    typedef long LargeValue;
    1.90 +#endif
    1.91 +    typedef lemon::Tolerance<LargeValue> Tolerance;
    1.92 +    typedef lemon::Path<Digraph> Path;
    1.93 +  };
    1.94 +
    1.95 +
    1.96 +  /// \addtogroup shortest_path
    1.97 +  /// @{
    1.98 +
    1.99 +  /// \brief Implementation of the Hartmann-Orlin algorithm for finding
   1.100 +  /// a minimum mean cycle.
   1.101 +  ///
   1.102 +  /// This class implements the Hartmann-Orlin algorithm for finding
   1.103 +  /// a directed cycle of minimum mean length (cost) in a digraph.
   1.104 +  /// It is an improved version of \ref Karp "Karp's original algorithm",
   1.105 +  /// it applies an efficient early termination scheme.
   1.106 +  ///
   1.107 +  /// \tparam GR The type of the digraph the algorithm runs on.
   1.108 +  /// \tparam LEN The type of the length map. The default
   1.109 +  /// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
   1.110 +#ifdef DOXYGEN
   1.111 +  template <typename GR, typename LEN, typename TR>
   1.112 +#else
   1.113 +  template < typename GR,
   1.114 +             typename LEN = typename GR::template ArcMap<int>,
   1.115 +             typename TR = HartmannOrlinDefaultTraits<GR, LEN> >
   1.116 +#endif
   1.117 +  class HartmannOrlin
   1.118 +  {
   1.119 +  public:
   1.120 +
   1.121 +    /// The type of the digraph
   1.122 +    typedef typename TR::Digraph Digraph;
   1.123 +    /// The type of the length map
   1.124 +    typedef typename TR::LengthMap LengthMap;
   1.125 +    /// The type of the arc lengths
   1.126 +    typedef typename TR::Value Value;
   1.127 +
   1.128 +    /// \brief The large value type
   1.129 +    ///
   1.130 +    /// The large value type used for internal computations.
   1.131 +    /// Using the \ref HartmannOrlinDefaultTraits "default traits class",
   1.132 +    /// it is \c long \c long if the \c Value type is integer,
   1.133 +    /// otherwise it is \c double.
   1.134 +    typedef typename TR::LargeValue LargeValue;
   1.135 +
   1.136 +    /// The tolerance type
   1.137 +    typedef typename TR::Tolerance Tolerance;
   1.138 +
   1.139 +    /// \brief The path type of the found cycles
   1.140 +    ///
   1.141 +    /// The path type of the found cycles.
   1.142 +    /// Using the \ref HartmannOrlinDefaultTraits "default traits class",
   1.143 +    /// it is \ref lemon::Path "Path<Digraph>".
   1.144 +    typedef typename TR::Path Path;
   1.145 +
   1.146 +    /// The \ref HartmannOrlinDefaultTraits "traits class" of the algorithm
   1.147 +    typedef TR Traits;
   1.148 +
   1.149 +  private:
   1.150 +
   1.151 +    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
   1.152 +
   1.153 +    // Data sturcture for path data
   1.154 +    struct PathData
   1.155 +    {
   1.156 +      bool found;
   1.157 +      LargeValue dist;
   1.158 +      Arc pred;
   1.159 +      PathData(bool f = false, LargeValue d = 0, Arc p = INVALID) :
   1.160 +        found(f), dist(d), pred(p) {}
   1.161 +    };
   1.162 +
   1.163 +    typedef typename Digraph::template NodeMap<std::vector<PathData> >
   1.164 +      PathDataNodeMap;
   1.165 +
   1.166 +  private:
   1.167 +
   1.168 +    // The digraph the algorithm runs on
   1.169 +    const Digraph &_gr;
   1.170 +    // The length of the arcs
   1.171 +    const LengthMap &_length;
   1.172 +
   1.173 +    // Data for storing the strongly connected components
   1.174 +    int _comp_num;
   1.175 +    typename Digraph::template NodeMap<int> _comp;
   1.176 +    std::vector<std::vector<Node> > _comp_nodes;
   1.177 +    std::vector<Node>* _nodes;
   1.178 +    typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs;
   1.179 +
   1.180 +    // Data for the found cycles
   1.181 +    bool _curr_found, _best_found;
   1.182 +    LargeValue _curr_length, _best_length;
   1.183 +    int _curr_size, _best_size;
   1.184 +    Node _curr_node, _best_node;
   1.185 +    int _curr_level, _best_level;
   1.186 +
   1.187 +    Path *_cycle_path;
   1.188 +    bool _local_path;
   1.189 +
   1.190 +    // Node map for storing path data
   1.191 +    PathDataNodeMap _data;
   1.192 +    // The processed nodes in the last round
   1.193 +    std::vector<Node> _process;
   1.194 +
   1.195 +    Tolerance _tolerance;
   1.196 +
   1.197 +  public:
   1.198 +
   1.199 +    /// \name Named Template Parameters
   1.200 +    /// @{
   1.201 +
   1.202 +    template <typename T>
   1.203 +    struct SetLargeValueTraits : public Traits {
   1.204 +      typedef T LargeValue;
   1.205 +      typedef lemon::Tolerance<T> Tolerance;
   1.206 +    };
   1.207 +
   1.208 +    /// \brief \ref named-templ-param "Named parameter" for setting
   1.209 +    /// \c LargeValue type.
   1.210 +    ///
   1.211 +    /// \ref named-templ-param "Named parameter" for setting \c LargeValue
   1.212 +    /// type. It is used for internal computations in the algorithm.
   1.213 +    template <typename T>
   1.214 +    struct SetLargeValue
   1.215 +      : public HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > {
   1.216 +      typedef HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > Create;
   1.217 +    };
   1.218 +
   1.219 +    template <typename T>
   1.220 +    struct SetPathTraits : public Traits {
   1.221 +      typedef T Path;
   1.222 +    };
   1.223 +
   1.224 +    /// \brief \ref named-templ-param "Named parameter" for setting
   1.225 +    /// \c %Path type.
   1.226 +    ///
   1.227 +    /// \ref named-templ-param "Named parameter" for setting the \c %Path
   1.228 +    /// type of the found cycles.
   1.229 +    /// It must conform to the \ref lemon::concepts::Path "Path" concept
   1.230 +    /// and it must have an \c addFront() function.
   1.231 +    template <typename T>
   1.232 +    struct SetPath
   1.233 +      : public HartmannOrlin<GR, LEN, SetPathTraits<T> > {
   1.234 +      typedef HartmannOrlin<GR, LEN, SetPathTraits<T> > Create;
   1.235 +    };
   1.236 +
   1.237 +    /// @}
   1.238 +
   1.239 +  public:
   1.240 +
   1.241 +    /// \brief Constructor.
   1.242 +    ///
   1.243 +    /// The constructor of the class.
   1.244 +    ///
   1.245 +    /// \param digraph The digraph the algorithm runs on.
   1.246 +    /// \param length The lengths (costs) of the arcs.
   1.247 +    HartmannOrlin( const Digraph &digraph,
   1.248 +                   const LengthMap &length ) :
   1.249 +      _gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph),
   1.250 +      _best_found(false), _best_length(0), _best_size(1),
   1.251 +      _cycle_path(NULL), _local_path(false), _data(digraph)
   1.252 +    {}
   1.253 +
   1.254 +    /// Destructor.
   1.255 +    ~HartmannOrlin() {
   1.256 +      if (_local_path) delete _cycle_path;
   1.257 +    }
   1.258 +
   1.259 +    /// \brief Set the path structure for storing the found cycle.
   1.260 +    ///
   1.261 +    /// This function sets an external path structure for storing the
   1.262 +    /// found cycle.
   1.263 +    ///
   1.264 +    /// If you don't call this function before calling \ref run() or
   1.265 +    /// \ref findMinMean(), it will allocate a local \ref Path "path"
   1.266 +    /// structure. The destuctor deallocates this automatically
   1.267 +    /// allocated object, of course.
   1.268 +    ///
   1.269 +    /// \note The algorithm calls only the \ref lemon::Path::addFront()
   1.270 +    /// "addFront()" function of the given path structure.
   1.271 +    ///
   1.272 +    /// \return <tt>(*this)</tt>
   1.273 +    HartmannOrlin& cycle(Path &path) {
   1.274 +      if (_local_path) {
   1.275 +        delete _cycle_path;
   1.276 +        _local_path = false;
   1.277 +      }
   1.278 +      _cycle_path = &path;
   1.279 +      return *this;
   1.280 +    }
   1.281 +
   1.282 +    /// \name Execution control
   1.283 +    /// The simplest way to execute the algorithm is to call the \ref run()
   1.284 +    /// function.\n
   1.285 +    /// If you only need the minimum mean length, you may call
   1.286 +    /// \ref findMinMean().
   1.287 +
   1.288 +    /// @{
   1.289 +
   1.290 +    /// \brief Run the algorithm.
   1.291 +    ///
   1.292 +    /// This function runs the algorithm.
   1.293 +    /// It can be called more than once (e.g. if the underlying digraph
   1.294 +    /// and/or the arc lengths have been modified).
   1.295 +    ///
   1.296 +    /// \return \c true if a directed cycle exists in the digraph.
   1.297 +    ///
   1.298 +    /// \note <tt>mmc.run()</tt> is just a shortcut of the following code.
   1.299 +    /// \code
   1.300 +    ///   return mmc.findMinMean() && mmc.findCycle();
   1.301 +    /// \endcode
   1.302 +    bool run() {
   1.303 +      return findMinMean() && findCycle();
   1.304 +    }
   1.305 +
   1.306 +    /// \brief Find the minimum cycle mean.
   1.307 +    ///
   1.308 +    /// This function finds the minimum mean length of the directed
   1.309 +    /// cycles in the digraph.
   1.310 +    ///
   1.311 +    /// \return \c true if a directed cycle exists in the digraph.
   1.312 +    bool findMinMean() {
   1.313 +      // Initialization and find strongly connected components
   1.314 +      init();
   1.315 +      findComponents();
   1.316 +      
   1.317 +      // Find the minimum cycle mean in the components
   1.318 +      for (int comp = 0; comp < _comp_num; ++comp) {
   1.319 +        if (!initComponent(comp)) continue;
   1.320 +        processRounds();
   1.321 +        
   1.322 +        // Update the best cycle (global minimum mean cycle)
   1.323 +        if ( _curr_found && (!_best_found || 
   1.324 +             _curr_length * _best_size < _best_length * _curr_size) ) {
   1.325 +          _best_found = true;
   1.326 +          _best_length = _curr_length;
   1.327 +          _best_size = _curr_size;
   1.328 +          _best_node = _curr_node;
   1.329 +          _best_level = _curr_level;
   1.330 +        }
   1.331 +      }
   1.332 +      return _best_found;
   1.333 +    }
   1.334 +
   1.335 +    /// \brief Find a minimum mean directed cycle.
   1.336 +    ///
   1.337 +    /// This function finds a directed cycle of minimum mean length
   1.338 +    /// in the digraph using the data computed by findMinMean().
   1.339 +    ///
   1.340 +    /// \return \c true if a directed cycle exists in the digraph.
   1.341 +    ///
   1.342 +    /// \pre \ref findMinMean() must be called before using this function.
   1.343 +    bool findCycle() {
   1.344 +      if (!_best_found) return false;
   1.345 +      IntNodeMap reached(_gr, -1);
   1.346 +      int r = _best_level + 1;
   1.347 +      Node u = _best_node;
   1.348 +      while (reached[u] < 0) {
   1.349 +        reached[u] = --r;
   1.350 +        u = _gr.source(_data[u][r].pred);
   1.351 +      }
   1.352 +      r = reached[u];
   1.353 +      Arc e = _data[u][r].pred;
   1.354 +      _cycle_path->addFront(e);
   1.355 +      _best_length = _length[e];
   1.356 +      _best_size = 1;
   1.357 +      Node v;
   1.358 +      while ((v = _gr.source(e)) != u) {
   1.359 +        e = _data[v][--r].pred;
   1.360 +        _cycle_path->addFront(e);
   1.361 +        _best_length += _length[e];
   1.362 +        ++_best_size;
   1.363 +      }
   1.364 +      return true;
   1.365 +    }
   1.366 +
   1.367 +    /// @}
   1.368 +
   1.369 +    /// \name Query Functions
   1.370 +    /// The results of the algorithm can be obtained using these
   1.371 +    /// functions.\n
   1.372 +    /// The algorithm should be executed before using them.
   1.373 +
   1.374 +    /// @{
   1.375 +
   1.376 +    /// \brief Return the total length of the found cycle.
   1.377 +    ///
   1.378 +    /// This function returns the total length of the found cycle.
   1.379 +    ///
   1.380 +    /// \pre \ref run() or \ref findMinMean() must be called before
   1.381 +    /// using this function.
   1.382 +    LargeValue cycleLength() const {
   1.383 +      return _best_length;
   1.384 +    }
   1.385 +
   1.386 +    /// \brief Return the number of arcs on the found cycle.
   1.387 +    ///
   1.388 +    /// This function returns the number of arcs on the found cycle.
   1.389 +    ///
   1.390 +    /// \pre \ref run() or \ref findMinMean() must be called before
   1.391 +    /// using this function.
   1.392 +    int cycleArcNum() const {
   1.393 +      return _best_size;
   1.394 +    }
   1.395 +
   1.396 +    /// \brief Return the mean length of the found cycle.
   1.397 +    ///
   1.398 +    /// This function returns the mean length of the found cycle.
   1.399 +    ///
   1.400 +    /// \note <tt>alg.cycleMean()</tt> is just a shortcut of the
   1.401 +    /// following code.
   1.402 +    /// \code
   1.403 +    ///   return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum();
   1.404 +    /// \endcode
   1.405 +    ///
   1.406 +    /// \pre \ref run() or \ref findMinMean() must be called before
   1.407 +    /// using this function.
   1.408 +    double cycleMean() const {
   1.409 +      return static_cast<double>(_best_length) / _best_size;
   1.410 +    }
   1.411 +
   1.412 +    /// \brief Return the found cycle.
   1.413 +    ///
   1.414 +    /// This function returns a const reference to the path structure
   1.415 +    /// storing the found cycle.
   1.416 +    ///
   1.417 +    /// \pre \ref run() or \ref findCycle() must be called before using
   1.418 +    /// this function.
   1.419 +    const Path& cycle() const {
   1.420 +      return *_cycle_path;
   1.421 +    }
   1.422 +
   1.423 +    ///@}
   1.424 +
   1.425 +  private:
   1.426 +
   1.427 +    // Initialization
   1.428 +    void init() {
   1.429 +      if (!_cycle_path) {
   1.430 +        _local_path = true;
   1.431 +        _cycle_path = new Path;
   1.432 +      }
   1.433 +      _cycle_path->clear();
   1.434 +      _best_found = false;
   1.435 +      _best_length = 0;
   1.436 +      _best_size = 1;
   1.437 +      _cycle_path->clear();
   1.438 +      for (NodeIt u(_gr); u != INVALID; ++u)
   1.439 +        _data[u].clear();
   1.440 +    }
   1.441 +
   1.442 +    // Find strongly connected components and initialize _comp_nodes
   1.443 +    // and _out_arcs
   1.444 +    void findComponents() {
   1.445 +      _comp_num = stronglyConnectedComponents(_gr, _comp);
   1.446 +      _comp_nodes.resize(_comp_num);
   1.447 +      if (_comp_num == 1) {
   1.448 +        _comp_nodes[0].clear();
   1.449 +        for (NodeIt n(_gr); n != INVALID; ++n) {
   1.450 +          _comp_nodes[0].push_back(n);
   1.451 +          _out_arcs[n].clear();
   1.452 +          for (OutArcIt a(_gr, n); a != INVALID; ++a) {
   1.453 +            _out_arcs[n].push_back(a);
   1.454 +          }
   1.455 +        }
   1.456 +      } else {
   1.457 +        for (int i = 0; i < _comp_num; ++i)
   1.458 +          _comp_nodes[i].clear();
   1.459 +        for (NodeIt n(_gr); n != INVALID; ++n) {
   1.460 +          int k = _comp[n];
   1.461 +          _comp_nodes[k].push_back(n);
   1.462 +          _out_arcs[n].clear();
   1.463 +          for (OutArcIt a(_gr, n); a != INVALID; ++a) {
   1.464 +            if (_comp[_gr.target(a)] == k) _out_arcs[n].push_back(a);
   1.465 +          }
   1.466 +        }
   1.467 +      }
   1.468 +    }
   1.469 +
   1.470 +    // Initialize path data for the current component
   1.471 +    bool initComponent(int comp) {
   1.472 +      _nodes = &(_comp_nodes[comp]);
   1.473 +      int n = _nodes->size();
   1.474 +      if (n < 1 || (n == 1 && _out_arcs[(*_nodes)[0]].size() == 0)) {
   1.475 +        return false;
   1.476 +      }      
   1.477 +      for (int i = 0; i < n; ++i) {
   1.478 +        _data[(*_nodes)[i]].resize(n + 1);
   1.479 +      }
   1.480 +      return true;
   1.481 +    }
   1.482 +
   1.483 +    // Process all rounds of computing path data for the current component.
   1.484 +    // _data[v][k] is the length of a shortest directed walk from the root
   1.485 +    // node to node v containing exactly k arcs.
   1.486 +    void processRounds() {
   1.487 +      Node start = (*_nodes)[0];
   1.488 +      _data[start][0] = PathData(true, 0);
   1.489 +      _process.clear();
   1.490 +      _process.push_back(start);
   1.491 +
   1.492 +      int k, n = _nodes->size();
   1.493 +      int next_check = 4;
   1.494 +      bool terminate = false;
   1.495 +      for (k = 1; k <= n && int(_process.size()) < n && !terminate; ++k) {
   1.496 +        processNextBuildRound(k);
   1.497 +        if (k == next_check || k == n) {
   1.498 +          terminate = checkTermination(k);
   1.499 +          next_check = next_check * 3 / 2;
   1.500 +        }
   1.501 +      }
   1.502 +      for ( ; k <= n && !terminate; ++k) {
   1.503 +        processNextFullRound(k);
   1.504 +        if (k == next_check || k == n) {
   1.505 +          terminate = checkTermination(k);
   1.506 +          next_check = next_check * 3 / 2;
   1.507 +        }
   1.508 +      }
   1.509 +    }
   1.510 +
   1.511 +    // Process one round and rebuild _process
   1.512 +    void processNextBuildRound(int k) {
   1.513 +      std::vector<Node> next;
   1.514 +      Node u, v;
   1.515 +      Arc e;
   1.516 +      LargeValue d;
   1.517 +      for (int i = 0; i < int(_process.size()); ++i) {
   1.518 +        u = _process[i];
   1.519 +        for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
   1.520 +          e = _out_arcs[u][j];
   1.521 +          v = _gr.target(e);
   1.522 +          d = _data[u][k-1].dist + _length[e];
   1.523 +          if (!_data[v][k].found) {
   1.524 +            next.push_back(v);
   1.525 +            _data[v][k] = PathData(true, _data[u][k-1].dist + _length[e], e);
   1.526 +          }
   1.527 +          else if (_tolerance.less(d, _data[v][k].dist)) {
   1.528 +            _data[v][k] = PathData(true, d, e);
   1.529 +          }
   1.530 +        }
   1.531 +      }
   1.532 +      _process.swap(next);
   1.533 +    }
   1.534 +
   1.535 +    // Process one round using _nodes instead of _process
   1.536 +    void processNextFullRound(int k) {
   1.537 +      Node u, v;
   1.538 +      Arc e;
   1.539 +      LargeValue d;
   1.540 +      for (int i = 0; i < int(_nodes->size()); ++i) {
   1.541 +        u = (*_nodes)[i];
   1.542 +        for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
   1.543 +          e = _out_arcs[u][j];
   1.544 +          v = _gr.target(e);
   1.545 +          d = _data[u][k-1].dist + _length[e];
   1.546 +          if (!_data[v][k].found || _tolerance.less(d, _data[v][k].dist)) {
   1.547 +            _data[v][k] = PathData(true, d, e);
   1.548 +          }
   1.549 +        }
   1.550 +      }
   1.551 +    }
   1.552 +    
   1.553 +    // Check early termination
   1.554 +    bool checkTermination(int k) {
   1.555 +      typedef std::pair<int, int> Pair;
   1.556 +      typename GR::template NodeMap<Pair> level(_gr, Pair(-1, 0));
   1.557 +      typename GR::template NodeMap<LargeValue> pi(_gr);
   1.558 +      int n = _nodes->size();
   1.559 +      LargeValue length;
   1.560 +      int size;
   1.561 +      Node u;
   1.562 +      
   1.563 +      // Search for cycles that are already found
   1.564 +      _curr_found = false;
   1.565 +      for (int i = 0; i < n; ++i) {
   1.566 +        u = (*_nodes)[i];
   1.567 +        if (!_data[u][k].found) continue;
   1.568 +        for (int j = k; j >= 0; --j) {
   1.569 +          if (level[u].first == i && level[u].second > 0) {
   1.570 +            // A cycle is found
   1.571 +            length = _data[u][level[u].second].dist - _data[u][j].dist;
   1.572 +            size = level[u].second - j;
   1.573 +            if (!_curr_found || length * _curr_size < _curr_length * size) {
   1.574 +              _curr_length = length;
   1.575 +              _curr_size = size;
   1.576 +              _curr_node = u;
   1.577 +              _curr_level = level[u].second;
   1.578 +              _curr_found = true;
   1.579 +            }
   1.580 +          }
   1.581 +          level[u] = Pair(i, j);
   1.582 +          u = _gr.source(_data[u][j].pred);
   1.583 +        }
   1.584 +      }
   1.585 +
   1.586 +      // If at least one cycle is found, check the optimality condition
   1.587 +      LargeValue d;
   1.588 +      if (_curr_found && k < n) {
   1.589 +        // Find node potentials
   1.590 +        for (int i = 0; i < n; ++i) {
   1.591 +          u = (*_nodes)[i];
   1.592 +          pi[u] = std::numeric_limits<LargeValue>::max();
   1.593 +          for (int j = 0; j <= k; ++j) {
   1.594 +            d = _data[u][j].dist * _curr_size - j * _curr_length;
   1.595 +            if (_data[u][j].found && _tolerance.less(d, pi[u])) {
   1.596 +              pi[u] = d;
   1.597 +            }
   1.598 +          }
   1.599 +        }
   1.600 +
   1.601 +        // Check the optimality condition for all arcs
   1.602 +        bool done = true;
   1.603 +        for (ArcIt a(_gr); a != INVALID; ++a) {
   1.604 +          if (_tolerance.less(_length[a] * _curr_size - _curr_length,
   1.605 +                              pi[_gr.target(a)] - pi[_gr.source(a)]) ) {
   1.606 +            done = false;
   1.607 +            break;
   1.608 +          }
   1.609 +        }
   1.610 +        return done;
   1.611 +      }
   1.612 +      return (k == n);
   1.613 +    }
   1.614 +
   1.615 +  }; //class HartmannOrlin
   1.616 +
   1.617 +  ///@}
   1.618 +
   1.619 +} //namespace lemon
   1.620 +
   1.621 +#endif //LEMON_HARTMANN_ORLIN_H