lemon/howard_mmc.h
author Alpar Juttner <alpar@cs.elte.hu>
Thu, 02 Apr 2015 22:34:03 +0200
changeset 1129 39b6e65574c6
parent 1092 dceba191c00d
permissions -rw-r--r--
Merge
alpar@877
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
kpeter@758
     2
 *
alpar@877
     3
 * This file is a part of LEMON, a generic C++ optimization library.
kpeter@758
     4
 *
alpar@1092
     5
 * Copyright (C) 2003-2013
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@864
    19
#ifndef LEMON_HOWARD_MMC_H
kpeter@864
    20
#define LEMON_HOWARD_MMC_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@864
    36
  /// \brief Default traits class of HowardMmc class.
kpeter@761
    37
  ///
kpeter@864
    38
  /// Default traits class of HowardMmc class.
kpeter@761
    39
  /// \tparam GR The type of the digraph.
kpeter@864
    40
  /// \tparam CM The type of the cost map.
kpeter@761
    41
  /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
kpeter@761
    42
#ifdef DOXYGEN
kpeter@864
    43
  template <typename GR, typename CM>
kpeter@761
    44
#else
kpeter@864
    45
  template <typename GR, typename CM,
kpeter@864
    46
    bool integer = std::numeric_limits<typename CM::Value>::is_integer>
kpeter@761
    47
#endif
kpeter@864
    48
  struct HowardMmcDefaultTraits
kpeter@761
    49
  {
kpeter@761
    50
    /// The type of the digraph
kpeter@761
    51
    typedef GR Digraph;
kpeter@864
    52
    /// The type of the cost map
kpeter@864
    53
    typedef CM CostMap;
kpeter@864
    54
    /// The type of the arc costs
kpeter@864
    55
    typedef typename CostMap::Value Cost;
kpeter@761
    56
kpeter@864
    57
    /// \brief The large cost type used for internal computations
kpeter@761
    58
    ///
kpeter@864
    59
    /// The large cost type used for internal computations.
kpeter@864
    60
    /// It is \c long \c long if the \c Cost type is integer,
kpeter@761
    61
    /// otherwise it is \c double.
kpeter@864
    62
    /// \c Cost must be convertible to \c LargeCost.
kpeter@864
    63
    typedef double LargeCost;
kpeter@761
    64
kpeter@761
    65
    /// The tolerance type used for internal computations
kpeter@864
    66
    typedef lemon::Tolerance<LargeCost> 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@864
    76
  // Default traits class for integer cost types
kpeter@864
    77
  template <typename GR, typename CM>
kpeter@864
    78
  struct HowardMmcDefaultTraits<GR, CM, true>
kpeter@761
    79
  {
kpeter@761
    80
    typedef GR Digraph;
kpeter@864
    81
    typedef CM CostMap;
kpeter@864
    82
    typedef typename CostMap::Value Cost;
kpeter@761
    83
#ifdef LEMON_HAVE_LONG_LONG
kpeter@864
    84
    typedef long long LargeCost;
kpeter@761
    85
#else
kpeter@864
    86
    typedef long LargeCost;
kpeter@761
    87
#endif
kpeter@864
    88
    typedef lemon::Tolerance<LargeCost> 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@864
   100
  /// a directed cycle of minimum mean cost in a digraph
alpar@1053
   101
  /// \cite dasdan98minmeancycle, \cite dasdan04experimental.
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@864
   107
  /// \tparam CM The type of the cost 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@864
   110
  /// algorithm. By default, it is \ref HowardMmcDefaultTraits
kpeter@864
   111
  /// "HowardMmcDefaultTraits<GR, CM>".
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@864
   115
  template <typename GR, typename CM, typename TR>
kpeter@758
   116
#else
kpeter@758
   117
  template < typename GR,
kpeter@864
   118
             typename CM = typename GR::template ArcMap<int>,
kpeter@864
   119
             typename TR = HowardMmcDefaultTraits<GR, CM> >
kpeter@758
   120
#endif
kpeter@864
   121
  class HowardMmc
kpeter@758
   122
  {
kpeter@758
   123
  public:
alpar@877
   124
kpeter@761
   125
    /// The type of the digraph
kpeter@761
   126
    typedef typename TR::Digraph Digraph;
kpeter@864
   127
    /// The type of the cost map
kpeter@864
   128
    typedef typename TR::CostMap CostMap;
kpeter@864
   129
    /// The type of the arc costs
kpeter@864
   130
    typedef typename TR::Cost Cost;
kpeter@761
   131
kpeter@864
   132
    /// \brief The large cost type
kpeter@761
   133
    ///
kpeter@864
   134
    /// The large cost type used for internal computations.
kpeter@864
   135
    /// By default, it is \c long \c long if the \c Cost type is integer,
kpeter@761
   136
    /// otherwise it is \c double.
kpeter@864
   137
    typedef typename TR::LargeCost LargeCost;
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.
alpar@1074
   145
    /// Using the \ref lemon::HowardMmcDefaultTraits "default traits class",
kpeter@761
   146
    /// it is \ref lemon::Path "Path<Digraph>".
kpeter@761
   147
    typedef typename TR::Path Path;
kpeter@761
   148
alpar@1074
   149
    /// The \ref lemon::HowardMmcDefaultTraits "traits class" of the algorithm
kpeter@761
   150
    typedef TR Traits;
kpeter@758
   151
kpeter@1012
   152
    /// \brief Constants for the causes of search termination.
kpeter@1012
   153
    ///
kpeter@1012
   154
    /// Enum type containing constants for the different causes of search
kpeter@1012
   155
    /// termination. The \ref findCycleMean() function returns one of
kpeter@1012
   156
    /// these values.
kpeter@1012
   157
    enum TerminationCause {
alpar@1092
   158
kpeter@1012
   159
      /// No directed cycle can be found in the digraph.
kpeter@1012
   160
      NO_CYCLE = 0,
alpar@1092
   161
kpeter@1012
   162
      /// Optimal solution (minimum cycle mean) is found.
kpeter@1012
   163
      OPTIMAL = 1,
kpeter@1012
   164
kpeter@1012
   165
      /// The iteration count limit is reached.
kpeter@1012
   166
      ITERATION_LIMIT
kpeter@1012
   167
    };
kpeter@1012
   168
kpeter@758
   169
  private:
kpeter@758
   170
kpeter@758
   171
    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
alpar@877
   172
kpeter@758
   173
    // The digraph the algorithm runs on
kpeter@758
   174
    const Digraph &_gr;
kpeter@864
   175
    // The cost of the arcs
kpeter@864
   176
    const CostMap &_cost;
kpeter@758
   177
kpeter@760
   178
    // Data for the found cycles
kpeter@760
   179
    bool _curr_found, _best_found;
kpeter@864
   180
    LargeCost _curr_cost, _best_cost;
kpeter@760
   181
    int _curr_size, _best_size;
kpeter@760
   182
    Node _curr_node, _best_node;
kpeter@760
   183
kpeter@758
   184
    Path *_cycle_path;
kpeter@760
   185
    bool _local_path;
kpeter@758
   186
kpeter@760
   187
    // Internal data used by the algorithm
kpeter@760
   188
    typename Digraph::template NodeMap<Arc> _policy;
kpeter@760
   189
    typename Digraph::template NodeMap<bool> _reached;
kpeter@760
   190
    typename Digraph::template NodeMap<int> _level;
kpeter@864
   191
    typename Digraph::template NodeMap<LargeCost> _dist;
kpeter@758
   192
kpeter@760
   193
    // Data for storing the strongly connected components
kpeter@760
   194
    int _comp_num;
kpeter@758
   195
    typename Digraph::template NodeMap<int> _comp;
kpeter@760
   196
    std::vector<std::vector<Node> > _comp_nodes;
kpeter@760
   197
    std::vector<Node>* _nodes;
kpeter@760
   198
    typename Digraph::template NodeMap<std::vector<Arc> > _in_arcs;
alpar@877
   199
kpeter@760
   200
    // Queue used for BFS search
kpeter@760
   201
    std::vector<Node> _queue;
kpeter@760
   202
    int _qfront, _qback;
kpeter@761
   203
kpeter@761
   204
    Tolerance _tolerance;
alpar@877
   205
kpeter@767
   206
    // Infinite constant
kpeter@864
   207
    const LargeCost INF;
kpeter@767
   208
kpeter@761
   209
  public:
alpar@877
   210
kpeter@761
   211
    /// \name Named Template Parameters
kpeter@761
   212
    /// @{
kpeter@761
   213
kpeter@761
   214
    template <typename T>
kpeter@864
   215
    struct SetLargeCostTraits : public Traits {
kpeter@864
   216
      typedef T LargeCost;
kpeter@761
   217
      typedef lemon::Tolerance<T> Tolerance;
kpeter@761
   218
    };
kpeter@761
   219
kpeter@761
   220
    /// \brief \ref named-templ-param "Named parameter" for setting
kpeter@864
   221
    /// \c LargeCost type.
kpeter@761
   222
    ///
kpeter@864
   223
    /// \ref named-templ-param "Named parameter" for setting \c LargeCost
kpeter@761
   224
    /// type. It is used for internal computations in the algorithm.
kpeter@761
   225
    template <typename T>
kpeter@864
   226
    struct SetLargeCost
kpeter@864
   227
      : public HowardMmc<GR, CM, SetLargeCostTraits<T> > {
kpeter@864
   228
      typedef HowardMmc<GR, CM, SetLargeCostTraits<T> > Create;
kpeter@761
   229
    };
kpeter@761
   230
kpeter@761
   231
    template <typename T>
kpeter@761
   232
    struct SetPathTraits : public Traits {
kpeter@761
   233
      typedef T Path;
kpeter@761
   234
    };
kpeter@761
   235
kpeter@761
   236
    /// \brief \ref named-templ-param "Named parameter" for setting
kpeter@761
   237
    /// \c %Path type.
kpeter@761
   238
    ///
kpeter@761
   239
    /// \ref named-templ-param "Named parameter" for setting the \c %Path
kpeter@761
   240
    /// type of the found cycles.
kpeter@761
   241
    /// It must conform to the \ref lemon::concepts::Path "Path" concept
kpeter@761
   242
    /// and it must have an \c addBack() function.
kpeter@761
   243
    template <typename T>
kpeter@761
   244
    struct SetPath
kpeter@864
   245
      : public HowardMmc<GR, CM, SetPathTraits<T> > {
kpeter@864
   246
      typedef HowardMmc<GR, CM, SetPathTraits<T> > Create;
kpeter@761
   247
    };
alpar@877
   248
kpeter@761
   249
    /// @}
kpeter@758
   250
kpeter@863
   251
  protected:
kpeter@863
   252
kpeter@864
   253
    HowardMmc() {}
kpeter@863
   254
kpeter@758
   255
  public:
kpeter@758
   256
kpeter@758
   257
    /// \brief Constructor.
kpeter@758
   258
    ///
kpeter@758
   259
    /// The constructor of the class.
kpeter@758
   260
    ///
kpeter@758
   261
    /// \param digraph The digraph the algorithm runs on.
kpeter@864
   262
    /// \param cost The costs of the arcs.
kpeter@864
   263
    HowardMmc( const Digraph &digraph,
kpeter@864
   264
               const CostMap &cost ) :
kpeter@864
   265
      _gr(digraph), _cost(cost), _best_found(false),
kpeter@864
   266
      _best_cost(0), _best_size(1), _cycle_path(NULL), _local_path(false),
kpeter@760
   267
      _policy(digraph), _reached(digraph), _level(digraph), _dist(digraph),
kpeter@767
   268
      _comp(digraph), _in_arcs(digraph),
kpeter@864
   269
      INF(std::numeric_limits<LargeCost>::has_infinity ?
kpeter@864
   270
          std::numeric_limits<LargeCost>::infinity() :
kpeter@864
   271
          std::numeric_limits<LargeCost>::max())
kpeter@758
   272
    {}
kpeter@758
   273
kpeter@758
   274
    /// Destructor.
kpeter@864
   275
    ~HowardMmc() {
kpeter@758
   276
      if (_local_path) delete _cycle_path;
kpeter@758
   277
    }
kpeter@758
   278
kpeter@758
   279
    /// \brief Set the path structure for storing the found cycle.
kpeter@758
   280
    ///
kpeter@758
   281
    /// This function sets an external path structure for storing the
kpeter@758
   282
    /// found cycle.
kpeter@758
   283
    ///
kpeter@758
   284
    /// If you don't call this function before calling \ref run() or
kpeter@1049
   285
    /// \ref findCycleMean(), a local \ref Path "path" structure
kpeter@1049
   286
    /// will be allocated. The destuctor deallocates this automatically
kpeter@758
   287
    /// allocated object, of course.
kpeter@758
   288
    ///
kpeter@758
   289
    /// \note The algorithm calls only the \ref lemon::Path::addBack()
kpeter@758
   290
    /// "addBack()" function of the given path structure.
kpeter@758
   291
    ///
kpeter@758
   292
    /// \return <tt>(*this)</tt>
kpeter@864
   293
    HowardMmc& cycle(Path &path) {
kpeter@758
   294
      if (_local_path) {
kpeter@758
   295
        delete _cycle_path;
kpeter@758
   296
        _local_path = false;
kpeter@758
   297
      }
kpeter@758
   298
      _cycle_path = &path;
kpeter@758
   299
      return *this;
kpeter@758
   300
    }
kpeter@758
   301
kpeter@769
   302
    /// \brief Set the tolerance used by the algorithm.
kpeter@769
   303
    ///
kpeter@769
   304
    /// This function sets the tolerance object used by the algorithm.
kpeter@769
   305
    ///
kpeter@769
   306
    /// \return <tt>(*this)</tt>
kpeter@864
   307
    HowardMmc& tolerance(const Tolerance& tolerance) {
kpeter@769
   308
      _tolerance = tolerance;
kpeter@769
   309
      return *this;
kpeter@769
   310
    }
kpeter@769
   311
kpeter@769
   312
    /// \brief Return a const reference to the tolerance.
kpeter@769
   313
    ///
kpeter@769
   314
    /// This function returns a const reference to the tolerance object
kpeter@769
   315
    /// used by the algorithm.
kpeter@769
   316
    const Tolerance& tolerance() const {
kpeter@769
   317
      return _tolerance;
kpeter@769
   318
    }
kpeter@769
   319
kpeter@758
   320
    /// \name Execution control
kpeter@758
   321
    /// The simplest way to execute the algorithm is to call the \ref run()
kpeter@758
   322
    /// function.\n
kpeter@864
   323
    /// If you only need the minimum mean cost, you may call
kpeter@864
   324
    /// \ref findCycleMean().
kpeter@758
   325
kpeter@758
   326
    /// @{
kpeter@758
   327
kpeter@758
   328
    /// \brief Run the algorithm.
kpeter@758
   329
    ///
kpeter@758
   330
    /// This function runs the algorithm.
kpeter@759
   331
    /// It can be called more than once (e.g. if the underlying digraph
kpeter@864
   332
    /// and/or the arc costs have been modified).
kpeter@758
   333
    ///
kpeter@758
   334
    /// \return \c true if a directed cycle exists in the digraph.
kpeter@758
   335
    ///
kpeter@759
   336
    /// \note <tt>mmc.run()</tt> is just a shortcut of the following code.
kpeter@758
   337
    /// \code
kpeter@864
   338
    ///   return mmc.findCycleMean() && mmc.findCycle();
kpeter@758
   339
    /// \endcode
kpeter@758
   340
    bool run() {
kpeter@864
   341
      return findCycleMean() && findCycle();
kpeter@758
   342
    }
kpeter@758
   343
kpeter@1012
   344
    /// \brief Find the minimum cycle mean (or an upper bound).
kpeter@758
   345
    ///
kpeter@864
   346
    /// This function finds the minimum mean cost of the directed
kpeter@1012
   347
    /// cycles in the digraph (or an upper bound for it).
kpeter@758
   348
    ///
kpeter@1012
   349
    /// By default, the function finds the exact minimum cycle mean,
kpeter@1012
   350
    /// but an optional limit can also be specified for the number of
kpeter@1012
   351
    /// iterations performed during the search process.
kpeter@1012
   352
    /// The return value indicates if the optimal solution is found
kpeter@1012
   353
    /// or the iteration limit is reached. In the latter case, an
kpeter@1012
   354
    /// approximate solution is provided, which corresponds to a directed
kpeter@1012
   355
    /// cycle whose mean cost is relatively small, but not necessarily
kpeter@1012
   356
    /// minimal.
kpeter@1012
   357
    ///
kpeter@1012
   358
    /// \param limit  The maximum allowed number of iterations during
alpar@1092
   359
    /// the search process. Its default value implies that the algorithm
kpeter@1012
   360
    /// runs until it finds the exact optimal solution.
kpeter@1012
   361
    ///
kpeter@1012
   362
    /// \return The termination cause of the search process.
alpar@1092
   363
    /// For more information, see \ref TerminationCause.
alpar@1093
   364
    TerminationCause findCycleMean(int limit =
alpar@1093
   365
                                   std::numeric_limits<int>::max()) {
kpeter@760
   366
      // Initialize and find strongly connected components
kpeter@760
   367
      init();
kpeter@760
   368
      findComponents();
alpar@877
   369
kpeter@759
   370
      // Find the minimum cycle mean in the components
kpeter@1012
   371
      int iter_count = 0;
kpeter@1012
   372
      bool iter_limit_reached = false;
kpeter@758
   373
      for (int comp = 0; comp < _comp_num; ++comp) {
kpeter@760
   374
        // Find the minimum mean cycle in the current component
kpeter@760
   375
        if (!buildPolicyGraph(comp)) continue;
kpeter@758
   376
        while (true) {
kpeter@1012
   377
          if (++iter_count > limit) {
kpeter@1012
   378
            iter_limit_reached = true;
kpeter@1012
   379
            break;
kpeter@1012
   380
          }
kpeter@760
   381
          findPolicyCycle();
kpeter@758
   382
          if (!computeNodeDistances()) break;
kpeter@758
   383
        }
kpeter@1012
   384
kpeter@760
   385
        // Update the best cycle (global minimum mean cycle)
kpeter@767
   386
        if ( _curr_found && (!_best_found ||
kpeter@864
   387
             _curr_cost * _best_size < _best_cost * _curr_size) ) {
kpeter@760
   388
          _best_found = true;
kpeter@864
   389
          _best_cost = _curr_cost;
kpeter@760
   390
          _best_size = _curr_size;
kpeter@760
   391
          _best_node = _curr_node;
kpeter@760
   392
        }
alpar@1092
   393
kpeter@1012
   394
        if (iter_limit_reached) break;
kpeter@758
   395
      }
kpeter@1012
   396
kpeter@1012
   397
      if (iter_limit_reached) {
kpeter@1012
   398
        return ITERATION_LIMIT;
kpeter@1012
   399
      } else {
kpeter@1012
   400
        return _best_found ? OPTIMAL : NO_CYCLE;
kpeter@1012
   401
      }
kpeter@758
   402
    }
kpeter@758
   403
kpeter@758
   404
    /// \brief Find a minimum mean directed cycle.
kpeter@758
   405
    ///
kpeter@864
   406
    /// This function finds a directed cycle of minimum mean cost
kpeter@864
   407
    /// in the digraph using the data computed by findCycleMean().
kpeter@758
   408
    ///
kpeter@758
   409
    /// \return \c true if a directed cycle exists in the digraph.
kpeter@758
   410
    ///
kpeter@864
   411
    /// \pre \ref findCycleMean() must be called before using this function.
kpeter@758
   412
    bool findCycle() {
kpeter@760
   413
      if (!_best_found) return false;
kpeter@760
   414
      _cycle_path->addBack(_policy[_best_node]);
kpeter@760
   415
      for ( Node v = _best_node;
kpeter@760
   416
            (v = _gr.target(_policy[v])) != _best_node; ) {
kpeter@758
   417
        _cycle_path->addBack(_policy[v]);
kpeter@758
   418
      }
kpeter@758
   419
      return true;
kpeter@758
   420
    }
kpeter@758
   421
kpeter@758
   422
    /// @}
kpeter@758
   423
kpeter@758
   424
    /// \name Query Functions
kpeter@759
   425
    /// The results of the algorithm can be obtained using these
kpeter@758
   426
    /// functions.\n
kpeter@758
   427
    /// The algorithm should be executed before using them.
kpeter@758
   428
kpeter@758
   429
    /// @{
kpeter@758
   430
kpeter@864
   431
    /// \brief Return the total cost of the found cycle.
kpeter@758
   432
    ///
kpeter@864
   433
    /// This function returns the total cost of the found cycle.
kpeter@758
   434
    ///
kpeter@864
   435
    /// \pre \ref run() or \ref findCycleMean() must be called before
kpeter@758
   436
    /// using this function.
kpeter@864
   437
    Cost cycleCost() const {
kpeter@864
   438
      return static_cast<Cost>(_best_cost);
kpeter@758
   439
    }
kpeter@758
   440
kpeter@758
   441
    /// \brief Return the number of arcs on the found cycle.
kpeter@758
   442
    ///
kpeter@758
   443
    /// This function returns the number of arcs on the found cycle.
kpeter@758
   444
    ///
kpeter@864
   445
    /// \pre \ref run() or \ref findCycleMean() must be called before
kpeter@758
   446
    /// using this function.
kpeter@864
   447
    int cycleSize() const {
kpeter@760
   448
      return _best_size;
kpeter@758
   449
    }
kpeter@758
   450
kpeter@864
   451
    /// \brief Return the mean cost of the found cycle.
kpeter@758
   452
    ///
kpeter@864
   453
    /// This function returns the mean cost of the found cycle.
kpeter@758
   454
    ///
kpeter@760
   455
    /// \note <tt>alg.cycleMean()</tt> is just a shortcut of the
kpeter@758
   456
    /// following code.
kpeter@758
   457
    /// \code
kpeter@864
   458
    ///   return static_cast<double>(alg.cycleCost()) / alg.cycleSize();
kpeter@758
   459
    /// \endcode
kpeter@758
   460
    ///
kpeter@864
   461
    /// \pre \ref run() or \ref findCycleMean() must be called before
kpeter@758
   462
    /// using this function.
kpeter@758
   463
    double cycleMean() const {
kpeter@864
   464
      return static_cast<double>(_best_cost) / _best_size;
kpeter@758
   465
    }
kpeter@758
   466
kpeter@758
   467
    /// \brief Return the found cycle.
kpeter@758
   468
    ///
kpeter@758
   469
    /// This function returns a const reference to the path structure
kpeter@758
   470
    /// storing the found cycle.
kpeter@758
   471
    ///
kpeter@758
   472
    /// \pre \ref run() or \ref findCycle() must be called before using
kpeter@758
   473
    /// this function.
kpeter@758
   474
    const Path& cycle() const {
kpeter@758
   475
      return *_cycle_path;
kpeter@758
   476
    }
kpeter@758
   477
kpeter@758
   478
    ///@}
kpeter@758
   479
kpeter@758
   480
  private:
kpeter@758
   481
kpeter@760
   482
    // Initialize
kpeter@760
   483
    void init() {
kpeter@760
   484
      if (!_cycle_path) {
kpeter@760
   485
        _local_path = true;
kpeter@760
   486
        _cycle_path = new Path;
kpeter@758
   487
      }
kpeter@760
   488
      _queue.resize(countNodes(_gr));
kpeter@760
   489
      _best_found = false;
kpeter@864
   490
      _best_cost = 0;
kpeter@760
   491
      _best_size = 1;
kpeter@760
   492
      _cycle_path->clear();
kpeter@760
   493
    }
alpar@877
   494
kpeter@760
   495
    // Find strongly connected components and initialize _comp_nodes
kpeter@760
   496
    // and _in_arcs
kpeter@760
   497
    void findComponents() {
kpeter@760
   498
      _comp_num = stronglyConnectedComponents(_gr, _comp);
kpeter@760
   499
      _comp_nodes.resize(_comp_num);
kpeter@760
   500
      if (_comp_num == 1) {
kpeter@760
   501
        _comp_nodes[0].clear();
kpeter@760
   502
        for (NodeIt n(_gr); n != INVALID; ++n) {
kpeter@760
   503
          _comp_nodes[0].push_back(n);
kpeter@760
   504
          _in_arcs[n].clear();
kpeter@760
   505
          for (InArcIt a(_gr, n); a != INVALID; ++a) {
kpeter@760
   506
            _in_arcs[n].push_back(a);
kpeter@760
   507
          }
kpeter@760
   508
        }
kpeter@760
   509
      } else {
kpeter@760
   510
        for (int i = 0; i < _comp_num; ++i)
kpeter@760
   511
          _comp_nodes[i].clear();
kpeter@760
   512
        for (NodeIt n(_gr); n != INVALID; ++n) {
kpeter@760
   513
          int k = _comp[n];
kpeter@760
   514
          _comp_nodes[k].push_back(n);
kpeter@760
   515
          _in_arcs[n].clear();
kpeter@760
   516
          for (InArcIt a(_gr, n); a != INVALID; ++a) {
kpeter@760
   517
            if (_comp[_gr.source(a)] == k) _in_arcs[n].push_back(a);
kpeter@760
   518
          }
kpeter@760
   519
        }
kpeter@758
   520
      }
kpeter@760
   521
    }
kpeter@760
   522
kpeter@760
   523
    // Build the policy graph in the given strongly connected component
kpeter@760
   524
    // (the out-degree of every node is 1)
kpeter@760
   525
    bool buildPolicyGraph(int comp) {
kpeter@760
   526
      _nodes = &(_comp_nodes[comp]);
kpeter@760
   527
      if (_nodes->size() < 1 ||
kpeter@760
   528
          (_nodes->size() == 1 && _in_arcs[(*_nodes)[0]].size() == 0)) {
kpeter@760
   529
        return false;
kpeter@758
   530
      }
kpeter@760
   531
      for (int i = 0; i < int(_nodes->size()); ++i) {
kpeter@767
   532
        _dist[(*_nodes)[i]] = INF;
kpeter@760
   533
      }
kpeter@760
   534
      Node u, v;
kpeter@760
   535
      Arc e;
kpeter@760
   536
      for (int i = 0; i < int(_nodes->size()); ++i) {
kpeter@760
   537
        v = (*_nodes)[i];
kpeter@760
   538
        for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
kpeter@760
   539
          e = _in_arcs[v][j];
kpeter@760
   540
          u = _gr.source(e);
kpeter@864
   541
          if (_cost[e] < _dist[u]) {
kpeter@864
   542
            _dist[u] = _cost[e];
kpeter@760
   543
            _policy[u] = e;
kpeter@760
   544
          }
kpeter@758
   545
        }
kpeter@758
   546
      }
kpeter@758
   547
      return true;
kpeter@758
   548
    }
kpeter@758
   549
kpeter@760
   550
    // Find the minimum mean cycle in the policy graph
kpeter@760
   551
    void findPolicyCycle() {
kpeter@760
   552
      for (int i = 0; i < int(_nodes->size()); ++i) {
kpeter@760
   553
        _level[(*_nodes)[i]] = -1;
kpeter@760
   554
      }
kpeter@864
   555
      LargeCost ccost;
kpeter@758
   556
      int csize;
kpeter@758
   557
      Node u, v;
kpeter@760
   558
      _curr_found = false;
kpeter@760
   559
      for (int i = 0; i < int(_nodes->size()); ++i) {
kpeter@760
   560
        u = (*_nodes)[i];
kpeter@760
   561
        if (_level[u] >= 0) continue;
kpeter@760
   562
        for (; _level[u] < 0; u = _gr.target(_policy[u])) {
kpeter@760
   563
          _level[u] = i;
kpeter@760
   564
        }
kpeter@760
   565
        if (_level[u] == i) {
kpeter@760
   566
          // A cycle is found
kpeter@864
   567
          ccost = _cost[_policy[u]];
kpeter@760
   568
          csize = 1;
kpeter@760
   569
          for (v = u; (v = _gr.target(_policy[v])) != u; ) {
kpeter@864
   570
            ccost += _cost[_policy[v]];
kpeter@760
   571
            ++csize;
kpeter@758
   572
          }
kpeter@760
   573
          if ( !_curr_found ||
kpeter@864
   574
               (ccost * _curr_size < _curr_cost * csize) ) {
kpeter@760
   575
            _curr_found = true;
kpeter@864
   576
            _curr_cost = ccost;
kpeter@760
   577
            _curr_size = csize;
kpeter@760
   578
            _curr_node = u;
kpeter@758
   579
          }
kpeter@758
   580
        }
kpeter@758
   581
      }
kpeter@758
   582
    }
kpeter@758
   583
kpeter@760
   584
    // Contract the policy graph and compute node distances
kpeter@758
   585
    bool computeNodeDistances() {
kpeter@760
   586
      // Find the component of the main cycle and compute node distances
kpeter@760
   587
      // using reverse BFS
kpeter@760
   588
      for (int i = 0; i < int(_nodes->size()); ++i) {
kpeter@760
   589
        _reached[(*_nodes)[i]] = false;
kpeter@760
   590
      }
kpeter@760
   591
      _qfront = _qback = 0;
kpeter@760
   592
      _queue[0] = _curr_node;
kpeter@760
   593
      _reached[_curr_node] = true;
kpeter@760
   594
      _dist[_curr_node] = 0;
kpeter@758
   595
      Node u, v;
kpeter@760
   596
      Arc e;
kpeter@760
   597
      while (_qfront <= _qback) {
kpeter@760
   598
        v = _queue[_qfront++];
kpeter@760
   599
        for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
kpeter@760
   600
          e = _in_arcs[v][j];
kpeter@758
   601
          u = _gr.source(e);
kpeter@760
   602
          if (_policy[u] == e && !_reached[u]) {
kpeter@760
   603
            _reached[u] = true;
kpeter@864
   604
            _dist[u] = _dist[v] + _cost[e] * _curr_size - _curr_cost;
kpeter@760
   605
            _queue[++_qback] = u;
kpeter@758
   606
          }
kpeter@758
   607
        }
kpeter@758
   608
      }
kpeter@760
   609
kpeter@760
   610
      // Connect all other nodes to this component and compute node
kpeter@760
   611
      // distances using reverse BFS
kpeter@760
   612
      _qfront = 0;
kpeter@760
   613
      while (_qback < int(_nodes->size())-1) {
kpeter@760
   614
        v = _queue[_qfront++];
kpeter@760
   615
        for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
kpeter@760
   616
          e = _in_arcs[v][j];
kpeter@760
   617
          u = _gr.source(e);
kpeter@760
   618
          if (!_reached[u]) {
kpeter@760
   619
            _reached[u] = true;
kpeter@760
   620
            _policy[u] = e;
kpeter@864
   621
            _dist[u] = _dist[v] + _cost[e] * _curr_size - _curr_cost;
kpeter@760
   622
            _queue[++_qback] = u;
kpeter@760
   623
          }
kpeter@760
   624
        }
kpeter@760
   625
      }
kpeter@760
   626
kpeter@760
   627
      // Improve node distances
kpeter@758
   628
      bool improved = false;
kpeter@760
   629
      for (int i = 0; i < int(_nodes->size()); ++i) {
kpeter@760
   630
        v = (*_nodes)[i];
kpeter@760
   631
        for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
kpeter@760
   632
          e = _in_arcs[v][j];
kpeter@760
   633
          u = _gr.source(e);
kpeter@864
   634
          LargeCost delta = _dist[v] + _cost[e] * _curr_size - _curr_cost;
kpeter@761
   635
          if (_tolerance.less(delta, _dist[u])) {
kpeter@760
   636
            _dist[u] = delta;
kpeter@760
   637
            _policy[u] = e;
kpeter@760
   638
            improved = true;
kpeter@760
   639
          }
kpeter@758
   640
        }
kpeter@758
   641
      }
kpeter@758
   642
      return improved;
kpeter@758
   643
    }
kpeter@758
   644
kpeter@864
   645
  }; //class HowardMmc
kpeter@758
   646
kpeter@758
   647
  ///@}
kpeter@758
   648
kpeter@758
   649
} //namespace lemon
kpeter@758
   650
kpeter@864
   651
#endif //LEMON_HOWARD_MMC_H