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