lemon/howard.h
author Peter Kovacs <kpeter@inf.elte.hu>
Thu, 04 Mar 2010 10:03:14 +0100
changeset 862 b6f76c95992e
parent 825 75e6020b19b1
child 863 a93f1a27d831
permissions -rw-r--r--
Clarify type names in NetworkSimplex (#353)

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