lemon/hartmann_orlin.h
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 26 Feb 2010 23:53:09 +0100
changeset 914 aa8c9008b3de
parent 891 75e6020b19b1
child 941 a93f1a27d831
permissions -rw-r--r--
Better return type for cycleLength() functions (#179)
in the min mean cycle algorithms.

The original Value type is used instead of the LargeValue type,
which is introduced for internal computations.
kpeter@813
     1
/* -*- C++ -*-
kpeter@813
     2
 *
kpeter@813
     3
 * This file is a part of LEMON, a generic C++ optimization library
kpeter@813
     4
 *
kpeter@813
     5
 * Copyright (C) 2003-2008
kpeter@813
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
kpeter@813
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
kpeter@813
     8
 *
kpeter@813
     9
 * Permission to use, modify and distribute this software is granted
kpeter@813
    10
 * provided that this copyright notice appears in all copies. For
kpeter@813
    11
 * precise terms see the accompanying LICENSE file.
kpeter@813
    12
 *
kpeter@813
    13
 * This software is provided "AS IS" with no warranty of any kind,
kpeter@813
    14
 * express or implied, and with no claim as to its suitability for any
kpeter@813
    15
 * purpose.
kpeter@813
    16
 *
kpeter@813
    17
 */
kpeter@813
    18
kpeter@813
    19
#ifndef LEMON_HARTMANN_ORLIN_H
kpeter@813
    20
#define LEMON_HARTMANN_ORLIN_H
kpeter@813
    21
kpeter@815
    22
/// \ingroup min_mean_cycle
kpeter@813
    23
///
kpeter@813
    24
/// \file
kpeter@813
    25
/// \brief Hartmann-Orlin's algorithm for finding a minimum mean cycle.
kpeter@813
    26
kpeter@813
    27
#include <vector>
kpeter@813
    28
#include <limits>
kpeter@813
    29
#include <lemon/core.h>
kpeter@813
    30
#include <lemon/path.h>
kpeter@813
    31
#include <lemon/tolerance.h>
kpeter@813
    32
#include <lemon/connectivity.h>
kpeter@813
    33
kpeter@813
    34
namespace lemon {
kpeter@813
    35
kpeter@813
    36
  /// \brief Default traits class of HartmannOrlin algorithm.
kpeter@813
    37
  ///
kpeter@813
    38
  /// Default traits class of HartmannOrlin algorithm.
kpeter@813
    39
  /// \tparam GR The type of the digraph.
kpeter@813
    40
  /// \tparam LEN The type of the length map.
kpeter@813
    41
  /// It must conform to the \ref concepts::Rea_data "Rea_data" concept.
kpeter@813
    42
#ifdef DOXYGEN
kpeter@813
    43
  template <typename GR, typename LEN>
kpeter@813
    44
#else
kpeter@813
    45
  template <typename GR, typename LEN,
kpeter@813
    46
    bool integer = std::numeric_limits<typename LEN::Value>::is_integer>
kpeter@813
    47
#endif
kpeter@813
    48
  struct HartmannOrlinDefaultTraits
kpeter@813
    49
  {
kpeter@813
    50
    /// The type of the digraph
kpeter@813
    51
    typedef GR Digraph;
kpeter@813
    52
    /// The type of the length map
kpeter@813
    53
    typedef LEN LengthMap;
kpeter@813
    54
    /// The type of the arc lengths
kpeter@813
    55
    typedef typename LengthMap::Value Value;
kpeter@813
    56
kpeter@813
    57
    /// \brief The large value type used for internal computations
kpeter@813
    58
    ///
kpeter@813
    59
    /// The large value type used for internal computations.
kpeter@813
    60
    /// It is \c long \c long if the \c Value type is integer,
kpeter@813
    61
    /// otherwise it is \c double.
kpeter@813
    62
    /// \c Value must be convertible to \c LargeValue.
kpeter@813
    63
    typedef double LargeValue;
kpeter@813
    64
kpeter@813
    65
    /// The tolerance type used for internal computations
kpeter@813
    66
    typedef lemon::Tolerance<LargeValue> Tolerance;
kpeter@813
    67
kpeter@813
    68
    /// \brief The path type of the found cycles
kpeter@813
    69
    ///
kpeter@813
    70
    /// The path type of the found cycles.
kpeter@813
    71
    /// It must conform to the \ref lemon::concepts::Path "Path" concept
kpeter@819
    72
    /// and it must have an \c addFront() function.
kpeter@813
    73
    typedef lemon::Path<Digraph> Path;
kpeter@813
    74
  };
kpeter@813
    75
kpeter@813
    76
  // Default traits class for integer value types
kpeter@813
    77
  template <typename GR, typename LEN>
kpeter@813
    78
  struct HartmannOrlinDefaultTraits<GR, LEN, true>
kpeter@813
    79
  {
kpeter@813
    80
    typedef GR Digraph;
kpeter@813
    81
    typedef LEN LengthMap;
kpeter@813
    82
    typedef typename LengthMap::Value Value;
kpeter@813
    83
#ifdef LEMON_HAVE_LONG_LONG
kpeter@813
    84
    typedef long long LargeValue;
kpeter@813
    85
#else
kpeter@813
    86
    typedef long LargeValue;
kpeter@813
    87
#endif
kpeter@813
    88
    typedef lemon::Tolerance<LargeValue> Tolerance;
kpeter@813
    89
    typedef lemon::Path<Digraph> Path;
kpeter@813
    90
  };
kpeter@813
    91
kpeter@813
    92
kpeter@815
    93
  /// \addtogroup min_mean_cycle
kpeter@813
    94
  /// @{
kpeter@813
    95
kpeter@813
    96
  /// \brief Implementation of the Hartmann-Orlin algorithm for finding
kpeter@813
    97
  /// a minimum mean cycle.
kpeter@813
    98
  ///
kpeter@813
    99
  /// This class implements the Hartmann-Orlin algorithm for finding
kpeter@818
   100
  /// a directed cycle of minimum mean length (cost) in a digraph
kpeter@818
   101
  /// \ref amo93networkflows, \ref dasdan98minmeancycle.
kpeter@815
   102
  /// It is an improved version of \ref Karp "Karp"'s original algorithm,
kpeter@813
   103
  /// it applies an efficient early termination scheme.
kpeter@815
   104
  /// It runs in time O(ne) and uses space O(n<sup>2</sup>+e).
kpeter@813
   105
  ///
kpeter@813
   106
  /// \tparam GR The type of the digraph the algorithm runs on.
kpeter@813
   107
  /// \tparam LEN The type of the length map. The default
kpeter@813
   108
  /// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
kpeter@891
   109
  /// \tparam TR The traits class that defines various types used by the
kpeter@891
   110
  /// algorithm. By default, it is \ref HartmannOrlinDefaultTraits
kpeter@891
   111
  /// "HartmannOrlinDefaultTraits<GR, LEN>".
kpeter@891
   112
  /// In most cases, this parameter should not be set directly,
kpeter@891
   113
  /// consider to use the named template parameters instead.
kpeter@813
   114
#ifdef DOXYGEN
kpeter@813
   115
  template <typename GR, typename LEN, typename TR>
kpeter@813
   116
#else
kpeter@813
   117
  template < typename GR,
kpeter@813
   118
             typename LEN = typename GR::template ArcMap<int>,
kpeter@813
   119
             typename TR = HartmannOrlinDefaultTraits<GR, LEN> >
kpeter@813
   120
#endif
kpeter@813
   121
  class HartmannOrlin
kpeter@813
   122
  {
kpeter@813
   123
  public:
kpeter@813
   124
kpeter@813
   125
    /// The type of the digraph
kpeter@813
   126
    typedef typename TR::Digraph Digraph;
kpeter@813
   127
    /// The type of the length map
kpeter@813
   128
    typedef typename TR::LengthMap LengthMap;
kpeter@813
   129
    /// The type of the arc lengths
kpeter@813
   130
    typedef typename TR::Value Value;
kpeter@813
   131
kpeter@813
   132
    /// \brief The large value type
kpeter@813
   133
    ///
kpeter@813
   134
    /// The large value type used for internal computations.
kpeter@891
   135
    /// By default, it is \c long \c long if the \c Value type is integer,
kpeter@813
   136
    /// otherwise it is \c double.
kpeter@813
   137
    typedef typename TR::LargeValue LargeValue;
kpeter@813
   138
kpeter@813
   139
    /// The tolerance type
kpeter@813
   140
    typedef typename TR::Tolerance Tolerance;
kpeter@813
   141
kpeter@813
   142
    /// \brief The path type of the found cycles
kpeter@813
   143
    ///
kpeter@813
   144
    /// The path type of the found cycles.
kpeter@813
   145
    /// Using the \ref HartmannOrlinDefaultTraits "default traits class",
kpeter@813
   146
    /// it is \ref lemon::Path "Path<Digraph>".
kpeter@813
   147
    typedef typename TR::Path Path;
kpeter@813
   148
kpeter@813
   149
    /// The \ref HartmannOrlinDefaultTraits "traits class" of the algorithm
kpeter@813
   150
    typedef TR Traits;
kpeter@813
   151
kpeter@813
   152
  private:
kpeter@813
   153
kpeter@813
   154
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
kpeter@813
   155
kpeter@813
   156
    // Data sturcture for path data
kpeter@813
   157
    struct PathData
kpeter@813
   158
    {
kpeter@813
   159
      LargeValue dist;
kpeter@813
   160
      Arc pred;
kpeter@814
   161
      PathData(LargeValue d, Arc p = INVALID) :
kpeter@814
   162
        dist(d), pred(p) {}
kpeter@813
   163
    };
kpeter@813
   164
kpeter@813
   165
    typedef typename Digraph::template NodeMap<std::vector<PathData> >
kpeter@813
   166
      PathDataNodeMap;
kpeter@813
   167
kpeter@813
   168
  private:
kpeter@813
   169
kpeter@813
   170
    // The digraph the algorithm runs on
kpeter@813
   171
    const Digraph &_gr;
kpeter@813
   172
    // The length of the arcs
kpeter@813
   173
    const LengthMap &_length;
kpeter@813
   174
kpeter@813
   175
    // Data for storing the strongly connected components
kpeter@813
   176
    int _comp_num;
kpeter@813
   177
    typename Digraph::template NodeMap<int> _comp;
kpeter@813
   178
    std::vector<std::vector<Node> > _comp_nodes;
kpeter@813
   179
    std::vector<Node>* _nodes;
kpeter@813
   180
    typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs;
kpeter@813
   181
kpeter@813
   182
    // Data for the found cycles
kpeter@813
   183
    bool _curr_found, _best_found;
kpeter@813
   184
    LargeValue _curr_length, _best_length;
kpeter@813
   185
    int _curr_size, _best_size;
kpeter@813
   186
    Node _curr_node, _best_node;
kpeter@813
   187
    int _curr_level, _best_level;
kpeter@813
   188
kpeter@813
   189
    Path *_cycle_path;
kpeter@813
   190
    bool _local_path;
kpeter@813
   191
kpeter@813
   192
    // Node map for storing path data
kpeter@813
   193
    PathDataNodeMap _data;
kpeter@813
   194
    // The processed nodes in the last round
kpeter@813
   195
    std::vector<Node> _process;
kpeter@813
   196
kpeter@813
   197
    Tolerance _tolerance;
kpeter@813
   198
kpeter@814
   199
    // Infinite constant
kpeter@814
   200
    const LargeValue INF;
kpeter@814
   201
kpeter@813
   202
  public:
kpeter@813
   203
kpeter@813
   204
    /// \name Named Template Parameters
kpeter@813
   205
    /// @{
kpeter@813
   206
kpeter@813
   207
    template <typename T>
kpeter@813
   208
    struct SetLargeValueTraits : public Traits {
kpeter@813
   209
      typedef T LargeValue;
kpeter@813
   210
      typedef lemon::Tolerance<T> Tolerance;
kpeter@813
   211
    };
kpeter@813
   212
kpeter@813
   213
    /// \brief \ref named-templ-param "Named parameter" for setting
kpeter@813
   214
    /// \c LargeValue type.
kpeter@813
   215
    ///
kpeter@813
   216
    /// \ref named-templ-param "Named parameter" for setting \c LargeValue
kpeter@813
   217
    /// type. It is used for internal computations in the algorithm.
kpeter@813
   218
    template <typename T>
kpeter@813
   219
    struct SetLargeValue
kpeter@813
   220
      : public HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > {
kpeter@813
   221
      typedef HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > Create;
kpeter@813
   222
    };
kpeter@813
   223
kpeter@813
   224
    template <typename T>
kpeter@813
   225
    struct SetPathTraits : public Traits {
kpeter@813
   226
      typedef T Path;
kpeter@813
   227
    };
kpeter@813
   228
kpeter@813
   229
    /// \brief \ref named-templ-param "Named parameter" for setting
kpeter@813
   230
    /// \c %Path type.
kpeter@813
   231
    ///
kpeter@813
   232
    /// \ref named-templ-param "Named parameter" for setting the \c %Path
kpeter@813
   233
    /// type of the found cycles.
kpeter@813
   234
    /// It must conform to the \ref lemon::concepts::Path "Path" concept
kpeter@813
   235
    /// and it must have an \c addFront() function.
kpeter@813
   236
    template <typename T>
kpeter@813
   237
    struct SetPath
kpeter@813
   238
      : public HartmannOrlin<GR, LEN, SetPathTraits<T> > {
kpeter@813
   239
      typedef HartmannOrlin<GR, LEN, SetPathTraits<T> > Create;
kpeter@813
   240
    };
kpeter@813
   241
kpeter@813
   242
    /// @}
kpeter@813
   243
kpeter@813
   244
  public:
kpeter@813
   245
kpeter@813
   246
    /// \brief Constructor.
kpeter@813
   247
    ///
kpeter@813
   248
    /// The constructor of the class.
kpeter@813
   249
    ///
kpeter@813
   250
    /// \param digraph The digraph the algorithm runs on.
kpeter@813
   251
    /// \param length The lengths (costs) of the arcs.
kpeter@813
   252
    HartmannOrlin( const Digraph &digraph,
kpeter@813
   253
                   const LengthMap &length ) :
kpeter@813
   254
      _gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph),
kpeter@813
   255
      _best_found(false), _best_length(0), _best_size(1),
kpeter@814
   256
      _cycle_path(NULL), _local_path(false), _data(digraph),
kpeter@814
   257
      INF(std::numeric_limits<LargeValue>::has_infinity ?
kpeter@814
   258
          std::numeric_limits<LargeValue>::infinity() :
kpeter@814
   259
          std::numeric_limits<LargeValue>::max())
kpeter@813
   260
    {}
kpeter@813
   261
kpeter@813
   262
    /// Destructor.
kpeter@813
   263
    ~HartmannOrlin() {
kpeter@813
   264
      if (_local_path) delete _cycle_path;
kpeter@813
   265
    }
kpeter@813
   266
kpeter@813
   267
    /// \brief Set the path structure for storing the found cycle.
kpeter@813
   268
    ///
kpeter@813
   269
    /// This function sets an external path structure for storing the
kpeter@813
   270
    /// found cycle.
kpeter@813
   271
    ///
kpeter@813
   272
    /// If you don't call this function before calling \ref run() or
kpeter@813
   273
    /// \ref findMinMean(), it will allocate a local \ref Path "path"
kpeter@813
   274
    /// structure. The destuctor deallocates this automatically
kpeter@813
   275
    /// allocated object, of course.
kpeter@813
   276
    ///
kpeter@813
   277
    /// \note The algorithm calls only the \ref lemon::Path::addFront()
kpeter@813
   278
    /// "addFront()" function of the given path structure.
kpeter@813
   279
    ///
kpeter@813
   280
    /// \return <tt>(*this)</tt>
kpeter@813
   281
    HartmannOrlin& cycle(Path &path) {
kpeter@813
   282
      if (_local_path) {
kpeter@813
   283
        delete _cycle_path;
kpeter@813
   284
        _local_path = false;
kpeter@813
   285
      }
kpeter@813
   286
      _cycle_path = &path;
kpeter@813
   287
      return *this;
kpeter@813
   288
    }
kpeter@813
   289
kpeter@816
   290
    /// \brief Set the tolerance used by the algorithm.
kpeter@816
   291
    ///
kpeter@816
   292
    /// This function sets the tolerance object used by the algorithm.
kpeter@816
   293
    ///
kpeter@816
   294
    /// \return <tt>(*this)</tt>
kpeter@816
   295
    HartmannOrlin& tolerance(const Tolerance& tolerance) {
kpeter@816
   296
      _tolerance = tolerance;
kpeter@816
   297
      return *this;
kpeter@816
   298
    }
kpeter@816
   299
kpeter@816
   300
    /// \brief Return a const reference to the tolerance.
kpeter@816
   301
    ///
kpeter@816
   302
    /// This function returns a const reference to the tolerance object
kpeter@816
   303
    /// used by the algorithm.
kpeter@816
   304
    const Tolerance& tolerance() const {
kpeter@816
   305
      return _tolerance;
kpeter@816
   306
    }
kpeter@816
   307
kpeter@813
   308
    /// \name Execution control
kpeter@813
   309
    /// The simplest way to execute the algorithm is to call the \ref run()
kpeter@813
   310
    /// function.\n
kpeter@813
   311
    /// If you only need the minimum mean length, you may call
kpeter@813
   312
    /// \ref findMinMean().
kpeter@813
   313
kpeter@813
   314
    /// @{
kpeter@813
   315
kpeter@813
   316
    /// \brief Run the algorithm.
kpeter@813
   317
    ///
kpeter@813
   318
    /// This function runs the algorithm.
kpeter@813
   319
    /// It can be called more than once (e.g. if the underlying digraph
kpeter@813
   320
    /// and/or the arc lengths have been modified).
kpeter@813
   321
    ///
kpeter@813
   322
    /// \return \c true if a directed cycle exists in the digraph.
kpeter@813
   323
    ///
kpeter@813
   324
    /// \note <tt>mmc.run()</tt> is just a shortcut of the following code.
kpeter@813
   325
    /// \code
kpeter@813
   326
    ///   return mmc.findMinMean() && mmc.findCycle();
kpeter@813
   327
    /// \endcode
kpeter@813
   328
    bool run() {
kpeter@813
   329
      return findMinMean() && findCycle();
kpeter@813
   330
    }
kpeter@813
   331
kpeter@813
   332
    /// \brief Find the minimum cycle mean.
kpeter@813
   333
    ///
kpeter@813
   334
    /// This function finds the minimum mean length of the directed
kpeter@813
   335
    /// cycles in the digraph.
kpeter@813
   336
    ///
kpeter@813
   337
    /// \return \c true if a directed cycle exists in the digraph.
kpeter@813
   338
    bool findMinMean() {
kpeter@813
   339
      // Initialization and find strongly connected components
kpeter@813
   340
      init();
kpeter@813
   341
      findComponents();
kpeter@813
   342
      
kpeter@813
   343
      // Find the minimum cycle mean in the components
kpeter@813
   344
      for (int comp = 0; comp < _comp_num; ++comp) {
kpeter@813
   345
        if (!initComponent(comp)) continue;
kpeter@813
   346
        processRounds();
kpeter@813
   347
        
kpeter@813
   348
        // Update the best cycle (global minimum mean cycle)
kpeter@813
   349
        if ( _curr_found && (!_best_found || 
kpeter@813
   350
             _curr_length * _best_size < _best_length * _curr_size) ) {
kpeter@813
   351
          _best_found = true;
kpeter@813
   352
          _best_length = _curr_length;
kpeter@813
   353
          _best_size = _curr_size;
kpeter@813
   354
          _best_node = _curr_node;
kpeter@813
   355
          _best_level = _curr_level;
kpeter@813
   356
        }
kpeter@813
   357
      }
kpeter@813
   358
      return _best_found;
kpeter@813
   359
    }
kpeter@813
   360
kpeter@813
   361
    /// \brief Find a minimum mean directed cycle.
kpeter@813
   362
    ///
kpeter@813
   363
    /// This function finds a directed cycle of minimum mean length
kpeter@813
   364
    /// in the digraph using the data computed by findMinMean().
kpeter@813
   365
    ///
kpeter@813
   366
    /// \return \c true if a directed cycle exists in the digraph.
kpeter@813
   367
    ///
kpeter@813
   368
    /// \pre \ref findMinMean() must be called before using this function.
kpeter@813
   369
    bool findCycle() {
kpeter@813
   370
      if (!_best_found) return false;
kpeter@813
   371
      IntNodeMap reached(_gr, -1);
kpeter@813
   372
      int r = _best_level + 1;
kpeter@813
   373
      Node u = _best_node;
kpeter@813
   374
      while (reached[u] < 0) {
kpeter@813
   375
        reached[u] = --r;
kpeter@813
   376
        u = _gr.source(_data[u][r].pred);
kpeter@813
   377
      }
kpeter@813
   378
      r = reached[u];
kpeter@813
   379
      Arc e = _data[u][r].pred;
kpeter@813
   380
      _cycle_path->addFront(e);
kpeter@813
   381
      _best_length = _length[e];
kpeter@813
   382
      _best_size = 1;
kpeter@813
   383
      Node v;
kpeter@813
   384
      while ((v = _gr.source(e)) != u) {
kpeter@813
   385
        e = _data[v][--r].pred;
kpeter@813
   386
        _cycle_path->addFront(e);
kpeter@813
   387
        _best_length += _length[e];
kpeter@813
   388
        ++_best_size;
kpeter@813
   389
      }
kpeter@813
   390
      return true;
kpeter@813
   391
    }
kpeter@813
   392
kpeter@813
   393
    /// @}
kpeter@813
   394
kpeter@813
   395
    /// \name Query Functions
kpeter@813
   396
    /// The results of the algorithm can be obtained using these
kpeter@813
   397
    /// functions.\n
kpeter@813
   398
    /// The algorithm should be executed before using them.
kpeter@813
   399
kpeter@813
   400
    /// @{
kpeter@813
   401
kpeter@813
   402
    /// \brief Return the total length of the found cycle.
kpeter@813
   403
    ///
kpeter@813
   404
    /// This function returns the total length of the found cycle.
kpeter@813
   405
    ///
kpeter@813
   406
    /// \pre \ref run() or \ref findMinMean() must be called before
kpeter@813
   407
    /// using this function.
kpeter@914
   408
    Value cycleLength() const {
kpeter@914
   409
      return static_cast<Value>(_best_length);
kpeter@813
   410
    }
kpeter@813
   411
kpeter@813
   412
    /// \brief Return the number of arcs on the found cycle.
kpeter@813
   413
    ///
kpeter@813
   414
    /// This function returns the number of arcs on the found cycle.
kpeter@813
   415
    ///
kpeter@813
   416
    /// \pre \ref run() or \ref findMinMean() must be called before
kpeter@813
   417
    /// using this function.
kpeter@813
   418
    int cycleArcNum() const {
kpeter@813
   419
      return _best_size;
kpeter@813
   420
    }
kpeter@813
   421
kpeter@813
   422
    /// \brief Return the mean length of the found cycle.
kpeter@813
   423
    ///
kpeter@813
   424
    /// This function returns the mean length of the found cycle.
kpeter@813
   425
    ///
kpeter@813
   426
    /// \note <tt>alg.cycleMean()</tt> is just a shortcut of the
kpeter@813
   427
    /// following code.
kpeter@813
   428
    /// \code
kpeter@813
   429
    ///   return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum();
kpeter@813
   430
    /// \endcode
kpeter@813
   431
    ///
kpeter@813
   432
    /// \pre \ref run() or \ref findMinMean() must be called before
kpeter@813
   433
    /// using this function.
kpeter@813
   434
    double cycleMean() const {
kpeter@813
   435
      return static_cast<double>(_best_length) / _best_size;
kpeter@813
   436
    }
kpeter@813
   437
kpeter@813
   438
    /// \brief Return the found cycle.
kpeter@813
   439
    ///
kpeter@813
   440
    /// This function returns a const reference to the path structure
kpeter@813
   441
    /// storing the found cycle.
kpeter@813
   442
    ///
kpeter@813
   443
    /// \pre \ref run() or \ref findCycle() must be called before using
kpeter@813
   444
    /// this function.
kpeter@813
   445
    const Path& cycle() const {
kpeter@813
   446
      return *_cycle_path;
kpeter@813
   447
    }
kpeter@813
   448
kpeter@813
   449
    ///@}
kpeter@813
   450
kpeter@813
   451
  private:
kpeter@813
   452
kpeter@813
   453
    // Initialization
kpeter@813
   454
    void init() {
kpeter@813
   455
      if (!_cycle_path) {
kpeter@813
   456
        _local_path = true;
kpeter@813
   457
        _cycle_path = new Path;
kpeter@813
   458
      }
kpeter@813
   459
      _cycle_path->clear();
kpeter@813
   460
      _best_found = false;
kpeter@813
   461
      _best_length = 0;
kpeter@813
   462
      _best_size = 1;
kpeter@813
   463
      _cycle_path->clear();
kpeter@813
   464
      for (NodeIt u(_gr); u != INVALID; ++u)
kpeter@813
   465
        _data[u].clear();
kpeter@813
   466
    }
kpeter@813
   467
kpeter@813
   468
    // Find strongly connected components and initialize _comp_nodes
kpeter@813
   469
    // and _out_arcs
kpeter@813
   470
    void findComponents() {
kpeter@813
   471
      _comp_num = stronglyConnectedComponents(_gr, _comp);
kpeter@813
   472
      _comp_nodes.resize(_comp_num);
kpeter@813
   473
      if (_comp_num == 1) {
kpeter@813
   474
        _comp_nodes[0].clear();
kpeter@813
   475
        for (NodeIt n(_gr); n != INVALID; ++n) {
kpeter@813
   476
          _comp_nodes[0].push_back(n);
kpeter@813
   477
          _out_arcs[n].clear();
kpeter@813
   478
          for (OutArcIt a(_gr, n); a != INVALID; ++a) {
kpeter@813
   479
            _out_arcs[n].push_back(a);
kpeter@813
   480
          }
kpeter@813
   481
        }
kpeter@813
   482
      } else {
kpeter@813
   483
        for (int i = 0; i < _comp_num; ++i)
kpeter@813
   484
          _comp_nodes[i].clear();
kpeter@813
   485
        for (NodeIt n(_gr); n != INVALID; ++n) {
kpeter@813
   486
          int k = _comp[n];
kpeter@813
   487
          _comp_nodes[k].push_back(n);
kpeter@813
   488
          _out_arcs[n].clear();
kpeter@813
   489
          for (OutArcIt a(_gr, n); a != INVALID; ++a) {
kpeter@813
   490
            if (_comp[_gr.target(a)] == k) _out_arcs[n].push_back(a);
kpeter@813
   491
          }
kpeter@813
   492
        }
kpeter@813
   493
      }
kpeter@813
   494
    }
kpeter@813
   495
kpeter@813
   496
    // Initialize path data for the current component
kpeter@813
   497
    bool initComponent(int comp) {
kpeter@813
   498
      _nodes = &(_comp_nodes[comp]);
kpeter@813
   499
      int n = _nodes->size();
kpeter@813
   500
      if (n < 1 || (n == 1 && _out_arcs[(*_nodes)[0]].size() == 0)) {
kpeter@813
   501
        return false;
kpeter@813
   502
      }      
kpeter@813
   503
      for (int i = 0; i < n; ++i) {
kpeter@814
   504
        _data[(*_nodes)[i]].resize(n + 1, PathData(INF));
kpeter@813
   505
      }
kpeter@813
   506
      return true;
kpeter@813
   507
    }
kpeter@813
   508
kpeter@813
   509
    // Process all rounds of computing path data for the current component.
kpeter@813
   510
    // _data[v][k] is the length of a shortest directed walk from the root
kpeter@813
   511
    // node to node v containing exactly k arcs.
kpeter@813
   512
    void processRounds() {
kpeter@813
   513
      Node start = (*_nodes)[0];
kpeter@814
   514
      _data[start][0] = PathData(0);
kpeter@813
   515
      _process.clear();
kpeter@813
   516
      _process.push_back(start);
kpeter@813
   517
kpeter@813
   518
      int k, n = _nodes->size();
kpeter@813
   519
      int next_check = 4;
kpeter@813
   520
      bool terminate = false;
kpeter@813
   521
      for (k = 1; k <= n && int(_process.size()) < n && !terminate; ++k) {
kpeter@813
   522
        processNextBuildRound(k);
kpeter@813
   523
        if (k == next_check || k == n) {
kpeter@813
   524
          terminate = checkTermination(k);
kpeter@813
   525
          next_check = next_check * 3 / 2;
kpeter@813
   526
        }
kpeter@813
   527
      }
kpeter@813
   528
      for ( ; k <= n && !terminate; ++k) {
kpeter@813
   529
        processNextFullRound(k);
kpeter@813
   530
        if (k == next_check || k == n) {
kpeter@813
   531
          terminate = checkTermination(k);
kpeter@813
   532
          next_check = next_check * 3 / 2;
kpeter@813
   533
        }
kpeter@813
   534
      }
kpeter@813
   535
    }
kpeter@813
   536
kpeter@813
   537
    // Process one round and rebuild _process
kpeter@813
   538
    void processNextBuildRound(int k) {
kpeter@813
   539
      std::vector<Node> next;
kpeter@813
   540
      Node u, v;
kpeter@813
   541
      Arc e;
kpeter@813
   542
      LargeValue d;
kpeter@813
   543
      for (int i = 0; i < int(_process.size()); ++i) {
kpeter@813
   544
        u = _process[i];
kpeter@813
   545
        for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
kpeter@813
   546
          e = _out_arcs[u][j];
kpeter@813
   547
          v = _gr.target(e);
kpeter@813
   548
          d = _data[u][k-1].dist + _length[e];
kpeter@814
   549
          if (_tolerance.less(d, _data[v][k].dist)) {
kpeter@814
   550
            if (_data[v][k].dist == INF) next.push_back(v);
kpeter@814
   551
            _data[v][k] = PathData(d, e);
kpeter@813
   552
          }
kpeter@813
   553
        }
kpeter@813
   554
      }
kpeter@813
   555
      _process.swap(next);
kpeter@813
   556
    }
kpeter@813
   557
kpeter@813
   558
    // Process one round using _nodes instead of _process
kpeter@813
   559
    void processNextFullRound(int k) {
kpeter@813
   560
      Node u, v;
kpeter@813
   561
      Arc e;
kpeter@813
   562
      LargeValue d;
kpeter@813
   563
      for (int i = 0; i < int(_nodes->size()); ++i) {
kpeter@813
   564
        u = (*_nodes)[i];
kpeter@813
   565
        for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
kpeter@813
   566
          e = _out_arcs[u][j];
kpeter@813
   567
          v = _gr.target(e);
kpeter@813
   568
          d = _data[u][k-1].dist + _length[e];
kpeter@814
   569
          if (_tolerance.less(d, _data[v][k].dist)) {
kpeter@814
   570
            _data[v][k] = PathData(d, e);
kpeter@813
   571
          }
kpeter@813
   572
        }
kpeter@813
   573
      }
kpeter@813
   574
    }
kpeter@813
   575
    
kpeter@813
   576
    // Check early termination
kpeter@813
   577
    bool checkTermination(int k) {
kpeter@813
   578
      typedef std::pair<int, int> Pair;
kpeter@813
   579
      typename GR::template NodeMap<Pair> level(_gr, Pair(-1, 0));
kpeter@813
   580
      typename GR::template NodeMap<LargeValue> pi(_gr);
kpeter@813
   581
      int n = _nodes->size();
kpeter@813
   582
      LargeValue length;
kpeter@813
   583
      int size;
kpeter@813
   584
      Node u;
kpeter@813
   585
      
kpeter@813
   586
      // Search for cycles that are already found
kpeter@813
   587
      _curr_found = false;
kpeter@813
   588
      for (int i = 0; i < n; ++i) {
kpeter@813
   589
        u = (*_nodes)[i];
kpeter@814
   590
        if (_data[u][k].dist == INF) continue;
kpeter@813
   591
        for (int j = k; j >= 0; --j) {
kpeter@813
   592
          if (level[u].first == i && level[u].second > 0) {
kpeter@813
   593
            // A cycle is found
kpeter@813
   594
            length = _data[u][level[u].second].dist - _data[u][j].dist;
kpeter@813
   595
            size = level[u].second - j;
kpeter@813
   596
            if (!_curr_found || length * _curr_size < _curr_length * size) {
kpeter@813
   597
              _curr_length = length;
kpeter@813
   598
              _curr_size = size;
kpeter@813
   599
              _curr_node = u;
kpeter@813
   600
              _curr_level = level[u].second;
kpeter@813
   601
              _curr_found = true;
kpeter@813
   602
            }
kpeter@813
   603
          }
kpeter@813
   604
          level[u] = Pair(i, j);
deba@859
   605
          if (j != 0) {
deba@859
   606
	    u = _gr.source(_data[u][j].pred);
deba@859
   607
	  }
kpeter@813
   608
        }
kpeter@813
   609
      }
kpeter@813
   610
kpeter@813
   611
      // If at least one cycle is found, check the optimality condition
kpeter@813
   612
      LargeValue d;
kpeter@813
   613
      if (_curr_found && k < n) {
kpeter@813
   614
        // Find node potentials
kpeter@813
   615
        for (int i = 0; i < n; ++i) {
kpeter@813
   616
          u = (*_nodes)[i];
kpeter@814
   617
          pi[u] = INF;
kpeter@813
   618
          for (int j = 0; j <= k; ++j) {
kpeter@814
   619
            if (_data[u][j].dist < INF) {
kpeter@814
   620
              d = _data[u][j].dist * _curr_size - j * _curr_length;
kpeter@814
   621
              if (_tolerance.less(d, pi[u])) pi[u] = d;
kpeter@813
   622
            }
kpeter@813
   623
          }
kpeter@813
   624
        }
kpeter@813
   625
kpeter@813
   626
        // Check the optimality condition for all arcs
kpeter@813
   627
        bool done = true;
kpeter@813
   628
        for (ArcIt a(_gr); a != INVALID; ++a) {
kpeter@813
   629
          if (_tolerance.less(_length[a] * _curr_size - _curr_length,
kpeter@813
   630
                              pi[_gr.target(a)] - pi[_gr.source(a)]) ) {
kpeter@813
   631
            done = false;
kpeter@813
   632
            break;
kpeter@813
   633
          }
kpeter@813
   634
        }
kpeter@813
   635
        return done;
kpeter@813
   636
      }
kpeter@813
   637
      return (k == n);
kpeter@813
   638
    }
kpeter@813
   639
kpeter@813
   640
  }; //class HartmannOrlin
kpeter@813
   641
kpeter@813
   642
  ///@}
kpeter@813
   643
kpeter@813
   644
} //namespace lemon
kpeter@813
   645
kpeter@813
   646
#endif //LEMON_HARTMANN_ORLIN_H