COIN-OR::LEMON - Graph Library

Changeset 864:d3ea191c3412 in lemon-1.2


Ignore:
Timestamp:
03/13/10 22:01:38 (10 years ago)
Author:
Peter Kovacs <kpeter@…>
Branch:
default
Phase:
public
Message:

Rename min mean cycle classes and their members (#179)
with respect to the possible introduction of min ratio
cycle algorithms in the future.

The renamed classes:

The renamed members:

Files:
3 edited
3 moved

Legend:

Unmodified
Added
Removed
  • lemon/Makefile.am

    r859 r864  
    9090        lemon/graph_to_eps.h \
    9191        lemon/grid_graph.h \
    92         lemon/hartmann_orlin.h \
    93         lemon/howard.h \
     92        lemon/hartmann_orlin_mmc.h \
     93        lemon/howard_mmc.h \
    9494        lemon/hypercube_graph.h \
    95         lemon/karp.h \
     95        lemon/karp_mmc.h \
    9696        lemon/kruskal.h \
    9797        lemon/hao_orlin.h \
  • lemon/cycle_canceling.h

    r840 r864  
    3535#include <lemon/circulation.h>
    3636#include <lemon/bellman_ford.h>
    37 #include <lemon/howard.h>
     37#include <lemon/howard_mmc.h>
    3838
    3939namespace lemon {
     
    925925      typedef SimplePath<StaticDigraph> SPath;
    926926      typedef typename SPath::ArcIt SPathArcIt;
    927       typedef typename Howard<StaticDigraph, CostArcMap>
     927      typedef typename HowardMmc<StaticDigraph, CostArcMap>
    928928        ::template SetPath<SPath>::Create MMC;
    929929     
     
    932932      mmc.cycle(cycle);
    933933      buildResidualNetwork();
    934       while (mmc.findMinMean() && mmc.cycleLength() < 0) {
     934      while (mmc.findCycleMean() && mmc.cycleCost() < 0) {
    935935        // Find the cycle
    936936        mmc.findCycle();
     
    11331133          }
    11341134        } else {
    1135           typedef Howard<StaticDigraph, CostArcMap> MMC;
     1135          typedef HowardMmc<StaticDigraph, CostArcMap> MMC;
    11361136          typedef typename BellmanFord<StaticDigraph, CostArcMap>
    11371137            ::template SetDistMap<CostNodeMap>::Create BF;
     
    11401140          buildResidualNetwork();
    11411141          MMC mmc(_sgr, _cost_map);
    1142           mmc.findMinMean();
     1142          mmc.findCycleMean();
    11431143          epsilon = -mmc.cycleMean();
    1144           Cost cycle_cost = mmc.cycleLength();
    1145           int cycle_size = mmc.cycleArcNum();
     1144          Cost cycle_cost = mmc.cycleCost();
     1145          int cycle_size = mmc.cycleSize();
    11461146         
    11471147          // Compute feasible potentials for the current epsilon
  • lemon/hartmann_orlin_mmc.h

    r863 r864  
    1717 */
    1818
    19 #ifndef LEMON_HARTMANN_ORLIN_H
    20 #define LEMON_HARTMANN_ORLIN_H
     19#ifndef LEMON_HARTMANN_ORLIN_MMC_H
     20#define LEMON_HARTMANN_ORLIN_MMC_H
    2121
    2222/// \ingroup min_mean_cycle
     
    3434namespace lemon {
    3535
    36   /// \brief Default traits class of HartmannOrlin algorithm.
     36  /// \brief Default traits class of HartmannOrlinMmc class.
    3737  ///
    38   /// Default traits class of HartmannOrlin algorithm.
     38  /// Default traits class of HartmannOrlinMmc class.
    3939  /// \tparam GR The type of the digraph.
    40   /// \tparam LEN The type of the length map.
     40  /// \tparam CM The type of the cost map.
    4141  /// It must conform to the \ref concepts::Rea_data "Rea_data" concept.
    4242#ifdef DOXYGEN
    43   template <typename GR, typename LEN>
     43  template <typename GR, typename CM>
    4444#else
    45   template <typename GR, typename LEN,
    46     bool integer = std::numeric_limits<typename LEN::Value>::is_integer>
     45  template <typename GR, typename CM,
     46    bool integer = std::numeric_limits<typename CM::Value>::is_integer>
    4747#endif
    48   struct HartmannOrlinDefaultTraits
     48  struct HartmannOrlinMmcDefaultTraits
    4949  {
    5050    /// The type of the digraph
    5151    typedef GR Digraph;
    52     /// The type of the length map
    53     typedef LEN LengthMap;
    54     /// The type of the arc lengths
    55     typedef typename LengthMap::Value Value;
    56 
    57     /// \brief The large value type used for internal computations
    58     ///
    59     /// The large value type used for internal computations.
    60     /// It is \c long \c long if the \c Value type is integer,
     52    /// The type of the cost map
     53    typedef CM CostMap;
     54    /// The type of the arc costs
     55    typedef typename CostMap::Value Cost;
     56
     57    /// \brief The large cost type used for internal computations
     58    ///
     59    /// The large cost type used for internal computations.
     60    /// It is \c long \c long if the \c Cost type is integer,
    6161    /// otherwise it is \c double.
    62     /// \c Value must be convertible to \c LargeValue.
    63     typedef double LargeValue;
     62    /// \c Cost must be convertible to \c LargeCost.
     63    typedef double LargeCost;
    6464
    6565    /// The tolerance type used for internal computations
    66     typedef lemon::Tolerance<LargeValue> Tolerance;
     66    typedef lemon::Tolerance<LargeCost> Tolerance;
    6767
    6868    /// \brief The path type of the found cycles
     
    7474  };
    7575
    76   // Default traits class for integer value types
    77   template <typename GR, typename LEN>
    78   struct HartmannOrlinDefaultTraits<GR, LEN, true>
     76  // Default traits class for integer cost types
     77  template <typename GR, typename CM>
     78  struct HartmannOrlinMmcDefaultTraits<GR, CM, true>
    7979  {
    8080    typedef GR Digraph;
    81     typedef LEN LengthMap;
    82     typedef typename LengthMap::Value Value;
     81    typedef CM CostMap;
     82    typedef typename CostMap::Value Cost;
    8383#ifdef LEMON_HAVE_LONG_LONG
    84     typedef long long LargeValue;
     84    typedef long long LargeCost;
    8585#else
    86     typedef long LargeValue;
     86    typedef long LargeCost;
    8787#endif
    88     typedef lemon::Tolerance<LargeValue> Tolerance;
     88    typedef lemon::Tolerance<LargeCost> Tolerance;
    8989    typedef lemon::Path<Digraph> Path;
    9090  };
     
    9898  ///
    9999  /// This class implements the Hartmann-Orlin algorithm for finding
    100   /// a directed cycle of minimum mean length (cost) in a digraph
     100  /// a directed cycle of minimum mean cost in a digraph
    101101  /// \ref amo93networkflows, \ref dasdan98minmeancycle.
    102102  /// It is an improved version of \ref Karp "Karp"'s original algorithm,
     
    105105  ///
    106106  /// \tparam GR The type of the digraph the algorithm runs on.
    107   /// \tparam LEN The type of the length map. The default
     107  /// \tparam CM The type of the cost map. The default
    108108  /// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
    109109  /// \tparam TR The traits class that defines various types used by the
    110   /// algorithm. By default, it is \ref HartmannOrlinDefaultTraits
    111   /// "HartmannOrlinDefaultTraits<GR, LEN>".
     110  /// algorithm. By default, it is \ref HartmannOrlinMmcDefaultTraits
     111  /// "HartmannOrlinMmcDefaultTraits<GR, CM>".
    112112  /// In most cases, this parameter should not be set directly,
    113113  /// consider to use the named template parameters instead.
    114114#ifdef DOXYGEN
    115   template <typename GR, typename LEN, typename TR>
     115  template <typename GR, typename CM, typename TR>
    116116#else
    117117  template < typename GR,
    118              typename LEN = typename GR::template ArcMap<int>,
    119              typename TR = HartmannOrlinDefaultTraits<GR, LEN> >
     118             typename CM = typename GR::template ArcMap<int>,
     119             typename TR = HartmannOrlinMmcDefaultTraits<GR, CM> >
    120120#endif
    121   class HartmannOrlin
     121  class HartmannOrlinMmc
    122122  {
    123123  public:
     
    125125    /// The type of the digraph
    126126    typedef typename TR::Digraph Digraph;
    127     /// The type of the length map
    128     typedef typename TR::LengthMap LengthMap;
    129     /// The type of the arc lengths
    130     typedef typename TR::Value Value;
    131 
    132     /// \brief The large value type
    133     ///
    134     /// The large value type used for internal computations.
    135     /// By default, it is \c long \c long if the \c Value type is integer,
     127    /// The type of the cost map
     128    typedef typename TR::CostMap CostMap;
     129    /// The type of the arc costs
     130    typedef typename TR::Cost Cost;
     131
     132    /// \brief The large cost type
     133    ///
     134    /// The large cost type used for internal computations.
     135    /// By default, it is \c long \c long if the \c Cost type is integer,
    136136    /// otherwise it is \c double.
    137     typedef typename TR::LargeValue LargeValue;
     137    typedef typename TR::LargeCost LargeCost;
    138138
    139139    /// The tolerance type
     
    143143    ///
    144144    /// The path type of the found cycles.
    145     /// Using the \ref HartmannOrlinDefaultTraits "default traits class",
     145    /// Using the \ref HartmannOrlinMmcDefaultTraits "default traits class",
    146146    /// it is \ref lemon::Path "Path<Digraph>".
    147147    typedef typename TR::Path Path;
    148148
    149     /// The \ref HartmannOrlinDefaultTraits "traits class" of the algorithm
     149    /// The \ref HartmannOrlinMmcDefaultTraits "traits class" of the algorithm
    150150    typedef TR Traits;
    151151
     
    157157    struct PathData
    158158    {
    159       LargeValue dist;
     159      LargeCost dist;
    160160      Arc pred;
    161       PathData(LargeValue d, Arc p = INVALID) :
     161      PathData(LargeCost d, Arc p = INVALID) :
    162162        dist(d), pred(p) {}
    163163    };
     
    170170    // The digraph the algorithm runs on
    171171    const Digraph &_gr;
    172     // The length of the arcs
    173     const LengthMap &_length;
     172    // The cost of the arcs
     173    const CostMap &_cost;
    174174
    175175    // Data for storing the strongly connected components
     
    182182    // Data for the found cycles
    183183    bool _curr_found, _best_found;
    184     LargeValue _curr_length, _best_length;
     184    LargeCost _curr_cost, _best_cost;
    185185    int _curr_size, _best_size;
    186186    Node _curr_node, _best_node;
     
    198198
    199199    // Infinite constant
    200     const LargeValue INF;
     200    const LargeCost INF;
    201201
    202202  public:
     
    206206
    207207    template <typename T>
    208     struct SetLargeValueTraits : public Traits {
    209       typedef T LargeValue;
     208    struct SetLargeCostTraits : public Traits {
     209      typedef T LargeCost;
    210210      typedef lemon::Tolerance<T> Tolerance;
    211211    };
    212212
    213213    /// \brief \ref named-templ-param "Named parameter" for setting
    214     /// \c LargeValue type.
    215     ///
    216     /// \ref named-templ-param "Named parameter" for setting \c LargeValue
     214    /// \c LargeCost type.
     215    ///
     216    /// \ref named-templ-param "Named parameter" for setting \c LargeCost
    217217    /// type. It is used for internal computations in the algorithm.
    218218    template <typename T>
    219     struct SetLargeValue
    220       : public HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > {
    221       typedef HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > Create;
     219    struct SetLargeCost
     220      : public HartmannOrlinMmc<GR, CM, SetLargeCostTraits<T> > {
     221      typedef HartmannOrlinMmc<GR, CM, SetLargeCostTraits<T> > Create;
    222222    };
    223223
     
    236236    template <typename T>
    237237    struct SetPath
    238       : public HartmannOrlin<GR, LEN, SetPathTraits<T> > {
    239       typedef HartmannOrlin<GR, LEN, SetPathTraits<T> > Create;
     238      : public HartmannOrlinMmc<GR, CM, SetPathTraits<T> > {
     239      typedef HartmannOrlinMmc<GR, CM, SetPathTraits<T> > Create;
    240240    };
    241241
     
    244244  protected:
    245245
    246     HartmannOrlin() {}
     246    HartmannOrlinMmc() {}
    247247
    248248  public:
     
    253253    ///
    254254    /// \param digraph The digraph the algorithm runs on.
    255     /// \param length The lengths (costs) of the arcs.
    256     HartmannOrlin( const Digraph &digraph,
    257                    const LengthMap &length ) :
    258       _gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph),
    259       _best_found(false), _best_length(0), _best_size(1),
     255    /// \param cost The costs of the arcs.
     256    HartmannOrlinMmc( const Digraph &digraph,
     257                      const CostMap &cost ) :
     258      _gr(digraph), _cost(cost), _comp(digraph), _out_arcs(digraph),
     259      _best_found(false), _best_cost(0), _best_size(1),
    260260      _cycle_path(NULL), _local_path(false), _data(digraph),
    261       INF(std::numeric_limits<LargeValue>::has_infinity ?
    262           std::numeric_limits<LargeValue>::infinity() :
    263           std::numeric_limits<LargeValue>::max())
     261      INF(std::numeric_limits<LargeCost>::has_infinity ?
     262          std::numeric_limits<LargeCost>::infinity() :
     263          std::numeric_limits<LargeCost>::max())
    264264    {}
    265265
    266266    /// Destructor.
    267     ~HartmannOrlin() {
     267    ~HartmannOrlinMmc() {
    268268      if (_local_path) delete _cycle_path;
    269269    }
     
    275275    ///
    276276    /// If you don't call this function before calling \ref run() or
    277     /// \ref findMinMean(), it will allocate a local \ref Path "path"
     277    /// \ref findCycleMean(), it will allocate a local \ref Path "path"
    278278    /// structure. The destuctor deallocates this automatically
    279279    /// allocated object, of course.
     
    283283    ///
    284284    /// \return <tt>(*this)</tt>
    285     HartmannOrlin& cycle(Path &path) {
     285    HartmannOrlinMmc& cycle(Path &path) {
    286286      if (_local_path) {
    287287        delete _cycle_path;
     
    297297    ///
    298298    /// \return <tt>(*this)</tt>
    299     HartmannOrlin& tolerance(const Tolerance& tolerance) {
     299    HartmannOrlinMmc& tolerance(const Tolerance& tolerance) {
    300300      _tolerance = tolerance;
    301301      return *this;
     
    313313    /// The simplest way to execute the algorithm is to call the \ref run()
    314314    /// function.\n
    315     /// If you only need the minimum mean length, you may call
    316     /// \ref findMinMean().
     315    /// If you only need the minimum mean cost, you may call
     316    /// \ref findCycleMean().
    317317
    318318    /// @{
     
    322322    /// This function runs the algorithm.
    323323    /// It can be called more than once (e.g. if the underlying digraph
    324     /// and/or the arc lengths have been modified).
     324    /// and/or the arc costs have been modified).
    325325    ///
    326326    /// \return \c true if a directed cycle exists in the digraph.
     
    328328    /// \note <tt>mmc.run()</tt> is just a shortcut of the following code.
    329329    /// \code
    330     ///   return mmc.findMinMean() && mmc.findCycle();
     330    ///   return mmc.findCycleMean() && mmc.findCycle();
    331331    /// \endcode
    332332    bool run() {
    333       return findMinMean() && findCycle();
     333      return findCycleMean() && findCycle();
    334334    }
    335335
    336336    /// \brief Find the minimum cycle mean.
    337337    ///
    338     /// This function finds the minimum mean length of the directed
     338    /// This function finds the minimum mean cost of the directed
    339339    /// cycles in the digraph.
    340340    ///
    341341    /// \return \c true if a directed cycle exists in the digraph.
    342     bool findMinMean() {
     342    bool findCycleMean() {
    343343      // Initialization and find strongly connected components
    344344      init();
     
    352352        // Update the best cycle (global minimum mean cycle)
    353353        if ( _curr_found && (!_best_found ||
    354              _curr_length * _best_size < _best_length * _curr_size) ) {
     354             _curr_cost * _best_size < _best_cost * _curr_size) ) {
    355355          _best_found = true;
    356           _best_length = _curr_length;
     356          _best_cost = _curr_cost;
    357357          _best_size = _curr_size;
    358358          _best_node = _curr_node;
     
    365365    /// \brief Find a minimum mean directed cycle.
    366366    ///
    367     /// This function finds a directed cycle of minimum mean length
    368     /// in the digraph using the data computed by findMinMean().
     367    /// This function finds a directed cycle of minimum mean cost
     368    /// in the digraph using the data computed by findCycleMean().
    369369    ///
    370370    /// \return \c true if a directed cycle exists in the digraph.
    371371    ///
    372     /// \pre \ref findMinMean() must be called before using this function.
     372    /// \pre \ref findCycleMean() must be called before using this function.
    373373    bool findCycle() {
    374374      if (!_best_found) return false;
     
    383383      Arc e = _data[u][r].pred;
    384384      _cycle_path->addFront(e);
    385       _best_length = _length[e];
     385      _best_cost = _cost[e];
    386386      _best_size = 1;
    387387      Node v;
     
    389389        e = _data[v][--r].pred;
    390390        _cycle_path->addFront(e);
    391         _best_length += _length[e];
     391        _best_cost += _cost[e];
    392392        ++_best_size;
    393393      }
     
    404404    /// @{
    405405
    406     /// \brief Return the total length of the found cycle.
    407     ///
    408     /// This function returns the total length of the found cycle.
    409     ///
    410     /// \pre \ref run() or \ref findMinMean() must be called before
     406    /// \brief Return the total cost of the found cycle.
     407    ///
     408    /// This function returns the total cost of the found cycle.
     409    ///
     410    /// \pre \ref run() or \ref findCycleMean() must be called before
    411411    /// using this function.
    412     Value cycleLength() const {
    413       return static_cast<Value>(_best_length);
     412    Cost cycleCost() const {
     413      return static_cast<Cost>(_best_cost);
    414414    }
    415415
     
    418418    /// This function returns the number of arcs on the found cycle.
    419419    ///
    420     /// \pre \ref run() or \ref findMinMean() must be called before
     420    /// \pre \ref run() or \ref findCycleMean() must be called before
    421421    /// using this function.
    422     int cycleArcNum() const {
     422    int cycleSize() const {
    423423      return _best_size;
    424424    }
    425425
    426     /// \brief Return the mean length of the found cycle.
    427     ///
    428     /// This function returns the mean length of the found cycle.
     426    /// \brief Return the mean cost of the found cycle.
     427    ///
     428    /// This function returns the mean cost of the found cycle.
    429429    ///
    430430    /// \note <tt>alg.cycleMean()</tt> is just a shortcut of the
    431431    /// following code.
    432432    /// \code
    433     ///   return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum();
     433    ///   return static_cast<double>(alg.cycleCost()) / alg.cycleSize();
    434434    /// \endcode
    435435    ///
    436     /// \pre \ref run() or \ref findMinMean() must be called before
     436    /// \pre \ref run() or \ref findCycleMean() must be called before
    437437    /// using this function.
    438438    double cycleMean() const {
    439       return static_cast<double>(_best_length) / _best_size;
     439      return static_cast<double>(_best_cost) / _best_size;
    440440    }
    441441
     
    463463      _cycle_path->clear();
    464464      _best_found = false;
    465       _best_length = 0;
     465      _best_cost = 0;
    466466      _best_size = 1;
    467467      _cycle_path->clear();
     
    512512
    513513    // Process all rounds of computing path data for the current component.
    514     // _data[v][k] is the length of a shortest directed walk from the root
     514    // _data[v][k] is the cost of a shortest directed walk from the root
    515515    // node to node v containing exactly k arcs.
    516516    void processRounds() {
     
    544544      Node u, v;
    545545      Arc e;
    546       LargeValue d;
     546      LargeCost d;
    547547      for (int i = 0; i < int(_process.size()); ++i) {
    548548        u = _process[i];
     
    550550          e = _out_arcs[u][j];
    551551          v = _gr.target(e);
    552           d = _data[u][k-1].dist + _length[e];
     552          d = _data[u][k-1].dist + _cost[e];
    553553          if (_tolerance.less(d, _data[v][k].dist)) {
    554554            if (_data[v][k].dist == INF) next.push_back(v);
     
    564564      Node u, v;
    565565      Arc e;
    566       LargeValue d;
     566      LargeCost d;
    567567      for (int i = 0; i < int(_nodes->size()); ++i) {
    568568        u = (*_nodes)[i];
     
    570570          e = _out_arcs[u][j];
    571571          v = _gr.target(e);
    572           d = _data[u][k-1].dist + _length[e];
     572          d = _data[u][k-1].dist + _cost[e];
    573573          if (_tolerance.less(d, _data[v][k].dist)) {
    574574            _data[v][k] = PathData(d, e);
     
    582582      typedef std::pair<int, int> Pair;
    583583      typename GR::template NodeMap<Pair> level(_gr, Pair(-1, 0));
    584       typename GR::template NodeMap<LargeValue> pi(_gr);
     584      typename GR::template NodeMap<LargeCost> pi(_gr);
    585585      int n = _nodes->size();
    586       LargeValue length;
     586      LargeCost cost;
    587587      int size;
    588588      Node u;
     
    596596          if (level[u].first == i && level[u].second > 0) {
    597597            // A cycle is found
    598             length = _data[u][level[u].second].dist - _data[u][j].dist;
     598            cost = _data[u][level[u].second].dist - _data[u][j].dist;
    599599            size = level[u].second - j;
    600             if (!_curr_found || length * _curr_size < _curr_length * size) {
    601               _curr_length = length;
     600            if (!_curr_found || cost * _curr_size < _curr_cost * size) {
     601              _curr_cost = cost;
    602602              _curr_size = size;
    603603              _curr_node = u;
     
    614614
    615615      // If at least one cycle is found, check the optimality condition
    616       LargeValue d;
     616      LargeCost d;
    617617      if (_curr_found && k < n) {
    618618        // Find node potentials
     
    622622          for (int j = 0; j <= k; ++j) {
    623623            if (_data[u][j].dist < INF) {
    624               d = _data[u][j].dist * _curr_size - j * _curr_length;
     624              d = _data[u][j].dist * _curr_size - j * _curr_cost;
    625625              if (_tolerance.less(d, pi[u])) pi[u] = d;
    626626            }
     
    631631        bool done = true;
    632632        for (ArcIt a(_gr); a != INVALID; ++a) {
    633           if (_tolerance.less(_length[a] * _curr_size - _curr_length,
     633          if (_tolerance.less(_cost[a] * _curr_size - _curr_cost,
    634634                              pi[_gr.target(a)] - pi[_gr.source(a)]) ) {
    635635            done = false;
     
    642642    }
    643643
    644   }; //class HartmannOrlin
     644  }; //class HartmannOrlinMmc
    645645
    646646  ///@}
     
    648648} //namespace lemon
    649649
    650 #endif //LEMON_HARTMANN_ORLIN_H
     650#endif //LEMON_HARTMANN_ORLIN_MMC_H
  • lemon/howard_mmc.h

    r863 r864  
    1717 */
    1818
    19 #ifndef LEMON_HOWARD_H
    20 #define LEMON_HOWARD_H
     19#ifndef LEMON_HOWARD_MMC_H
     20#define LEMON_HOWARD_MMC_H
    2121
    2222/// \ingroup min_mean_cycle
     
    3434namespace lemon {
    3535
    36   /// \brief Default traits class of Howard class.
     36  /// \brief Default traits class of HowardMmc class.
    3737  ///
    38   /// Default traits class of Howard class.
     38  /// Default traits class of HowardMmc class.
    3939  /// \tparam GR The type of the digraph.
    40   /// \tparam LEN The type of the length map.
     40  /// \tparam CM The type of the cost map.
    4141  /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
    4242#ifdef DOXYGEN
    43   template <typename GR, typename LEN>
     43  template <typename GR, typename CM>
    4444#else
    45   template <typename GR, typename LEN,
    46     bool integer = std::numeric_limits<typename LEN::Value>::is_integer>
     45  template <typename GR, typename CM,
     46    bool integer = std::numeric_limits<typename CM::Value>::is_integer>
    4747#endif
    48   struct HowardDefaultTraits
     48  struct HowardMmcDefaultTraits
    4949  {
    5050    /// The type of the digraph
    5151    typedef GR Digraph;
    52     /// The type of the length map
    53     typedef LEN LengthMap;
    54     /// The type of the arc lengths
    55     typedef typename LengthMap::Value Value;
    56 
    57     /// \brief The large value type used for internal computations
    58     ///
    59     /// The large value type used for internal computations.
    60     /// It is \c long \c long if the \c Value type is integer,
     52    /// The type of the cost map
     53    typedef CM CostMap;
     54    /// The type of the arc costs
     55    typedef typename CostMap::Value Cost;
     56
     57    /// \brief The large cost type used for internal computations
     58    ///
     59    /// The large cost type used for internal computations.
     60    /// It is \c long \c long if the \c Cost type is integer,
    6161    /// otherwise it is \c double.
    62     /// \c Value must be convertible to \c LargeValue.
    63     typedef double LargeValue;
     62    /// \c Cost must be convertible to \c LargeCost.
     63    typedef double LargeCost;
    6464
    6565    /// The tolerance type used for internal computations
    66     typedef lemon::Tolerance<LargeValue> Tolerance;
     66    typedef lemon::Tolerance<LargeCost> Tolerance;
    6767
    6868    /// \brief The path type of the found cycles
     
    7474  };
    7575
    76   // Default traits class for integer value types
    77   template <typename GR, typename LEN>
    78   struct HowardDefaultTraits<GR, LEN, true>
     76  // Default traits class for integer cost types
     77  template <typename GR, typename CM>
     78  struct HowardMmcDefaultTraits<GR, CM, true>
    7979  {
    8080    typedef GR Digraph;
    81     typedef LEN LengthMap;
    82     typedef typename LengthMap::Value Value;
     81    typedef CM CostMap;
     82    typedef typename CostMap::Value Cost;
    8383#ifdef LEMON_HAVE_LONG_LONG
    84     typedef long long LargeValue;
     84    typedef long long LargeCost;
    8585#else
    86     typedef long LargeValue;
     86    typedef long LargeCost;
    8787#endif
    88     typedef lemon::Tolerance<LargeValue> Tolerance;
     88    typedef lemon::Tolerance<LargeCost> Tolerance;
    8989    typedef lemon::Path<Digraph> Path;
    9090  };
     
    9898  ///
    9999  /// This class implements Howard's policy iteration algorithm for finding
    100   /// a directed cycle of minimum mean length (cost) in a digraph
     100  /// a directed cycle of minimum mean cost in a digraph
    101101  /// \ref amo93networkflows, \ref dasdan98minmeancycle.
    102102  /// This class provides the most efficient algorithm for the
     
    105105  ///
    106106  /// \tparam GR The type of the digraph the algorithm runs on.
    107   /// \tparam LEN The type of the length map. The default
     107  /// \tparam CM The type of the cost map. The default
    108108  /// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
    109109  /// \tparam TR The traits class that defines various types used by the
    110   /// algorithm. By default, it is \ref HowardDefaultTraits
    111   /// "HowardDefaultTraits<GR, LEN>".
     110  /// algorithm. By default, it is \ref HowardMmcDefaultTraits
     111  /// "HowardMmcDefaultTraits<GR, CM>".
    112112  /// In most cases, this parameter should not be set directly,
    113113  /// consider to use the named template parameters instead.
    114114#ifdef DOXYGEN
    115   template <typename GR, typename LEN, typename TR>
     115  template <typename GR, typename CM, typename TR>
    116116#else
    117117  template < typename GR,
    118              typename LEN = typename GR::template ArcMap<int>,
    119              typename TR = HowardDefaultTraits<GR, LEN> >
     118             typename CM = typename GR::template ArcMap<int>,
     119             typename TR = HowardMmcDefaultTraits<GR, CM> >
    120120#endif
    121   class Howard
     121  class HowardMmc
    122122  {
    123123  public:
     
    125125    /// The type of the digraph
    126126    typedef typename TR::Digraph Digraph;
    127     /// The type of the length map
    128     typedef typename TR::LengthMap LengthMap;
    129     /// The type of the arc lengths
    130     typedef typename TR::Value Value;
    131 
    132     /// \brief The large value type
    133     ///
    134     /// The large value type used for internal computations.
    135     /// By default, it is \c long \c long if the \c Value type is integer,
     127    /// The type of the cost map
     128    typedef typename TR::CostMap CostMap;
     129    /// The type of the arc costs
     130    typedef typename TR::Cost Cost;
     131
     132    /// \brief The large cost type
     133    ///
     134    /// The large cost type used for internal computations.
     135    /// By default, it is \c long \c long if the \c Cost type is integer,
    136136    /// otherwise it is \c double.
    137     typedef typename TR::LargeValue LargeValue;
     137    typedef typename TR::LargeCost LargeCost;
    138138
    139139    /// The tolerance type
     
    143143    ///
    144144    /// The path type of the found cycles.
    145     /// Using the \ref HowardDefaultTraits "default traits class",
     145    /// Using the \ref HowardMmcDefaultTraits "default traits class",
    146146    /// it is \ref lemon::Path "Path<Digraph>".
    147147    typedef typename TR::Path Path;
    148148
    149     /// The \ref HowardDefaultTraits "traits class" of the algorithm
     149    /// The \ref HowardMmcDefaultTraits "traits class" of the algorithm
    150150    typedef TR Traits;
    151151
     
    156156    // The digraph the algorithm runs on
    157157    const Digraph &_gr;
    158     // The length of the arcs
    159     const LengthMap &_length;
     158    // The cost of the arcs
     159    const CostMap &_cost;
    160160
    161161    // Data for the found cycles
    162162    bool _curr_found, _best_found;
    163     LargeValue _curr_length, _best_length;
     163    LargeCost _curr_cost, _best_cost;
    164164    int _curr_size, _best_size;
    165165    Node _curr_node, _best_node;
     
    172172    typename Digraph::template NodeMap<bool> _reached;
    173173    typename Digraph::template NodeMap<int> _level;
    174     typename Digraph::template NodeMap<LargeValue> _dist;
     174    typename Digraph::template NodeMap<LargeCost> _dist;
    175175
    176176    // Data for storing the strongly connected components
     
    188188 
    189189    // Infinite constant
    190     const LargeValue INF;
     190    const LargeCost INF;
    191191
    192192  public:
     
    196196
    197197    template <typename T>
    198     struct SetLargeValueTraits : public Traits {
    199       typedef T LargeValue;
     198    struct SetLargeCostTraits : public Traits {
     199      typedef T LargeCost;
    200200      typedef lemon::Tolerance<T> Tolerance;
    201201    };
    202202
    203203    /// \brief \ref named-templ-param "Named parameter" for setting
    204     /// \c LargeValue type.
    205     ///
    206     /// \ref named-templ-param "Named parameter" for setting \c LargeValue
     204    /// \c LargeCost type.
     205    ///
     206    /// \ref named-templ-param "Named parameter" for setting \c LargeCost
    207207    /// type. It is used for internal computations in the algorithm.
    208208    template <typename T>
    209     struct SetLargeValue
    210       : public Howard<GR, LEN, SetLargeValueTraits<T> > {
    211       typedef Howard<GR, LEN, SetLargeValueTraits<T> > Create;
     209    struct SetLargeCost
     210      : public HowardMmc<GR, CM, SetLargeCostTraits<T> > {
     211      typedef HowardMmc<GR, CM, SetLargeCostTraits<T> > Create;
    212212    };
    213213
     
    226226    template <typename T>
    227227    struct SetPath
    228       : public Howard<GR, LEN, SetPathTraits<T> > {
    229       typedef Howard<GR, LEN, SetPathTraits<T> > Create;
     228      : public HowardMmc<GR, CM, SetPathTraits<T> > {
     229      typedef HowardMmc<GR, CM, SetPathTraits<T> > Create;
    230230    };
    231231   
     
    234234  protected:
    235235
    236     Howard() {}
     236    HowardMmc() {}
    237237
    238238  public:
     
    243243    ///
    244244    /// \param digraph The digraph the algorithm runs on.
    245     /// \param length The lengths (costs) of the arcs.
    246     Howard( const Digraph &digraph,
    247             const LengthMap &length ) :
    248       _gr(digraph), _length(length), _best_found(false),
    249       _best_length(0), _best_size(1), _cycle_path(NULL), _local_path(false),
     245    /// \param cost The costs of the arcs.
     246    HowardMmc( const Digraph &digraph,
     247               const CostMap &cost ) :
     248      _gr(digraph), _cost(cost), _best_found(false),
     249      _best_cost(0), _best_size(1), _cycle_path(NULL), _local_path(false),
    250250      _policy(digraph), _reached(digraph), _level(digraph), _dist(digraph),
    251251      _comp(digraph), _in_arcs(digraph),
    252       INF(std::numeric_limits<LargeValue>::has_infinity ?
    253           std::numeric_limits<LargeValue>::infinity() :
    254           std::numeric_limits<LargeValue>::max())
     252      INF(std::numeric_limits<LargeCost>::has_infinity ?
     253          std::numeric_limits<LargeCost>::infinity() :
     254          std::numeric_limits<LargeCost>::max())
    255255    {}
    256256
    257257    /// Destructor.
    258     ~Howard() {
     258    ~HowardMmc() {
    259259      if (_local_path) delete _cycle_path;
    260260    }
     
    266266    ///
    267267    /// If you don't call this function before calling \ref run() or
    268     /// \ref findMinMean(), it will allocate a local \ref Path "path"
     268    /// \ref findCycleMean(), it will allocate a local \ref Path "path"
    269269    /// structure. The destuctor deallocates this automatically
    270270    /// allocated object, of course.
     
    274274    ///
    275275    /// \return <tt>(*this)</tt>
    276     Howard& cycle(Path &path) {
     276    HowardMmc& cycle(Path &path) {
    277277      if (_local_path) {
    278278        delete _cycle_path;
     
    288288    ///
    289289    /// \return <tt>(*this)</tt>
    290     Howard& tolerance(const Tolerance& tolerance) {
     290    HowardMmc& tolerance(const Tolerance& tolerance) {
    291291      _tolerance = tolerance;
    292292      return *this;
     
    304304    /// The simplest way to execute the algorithm is to call the \ref run()
    305305    /// function.\n
    306     /// If you only need the minimum mean length, you may call
    307     /// \ref findMinMean().
     306    /// If you only need the minimum mean cost, you may call
     307    /// \ref findCycleMean().
    308308
    309309    /// @{
     
    313313    /// This function runs the algorithm.
    314314    /// It can be called more than once (e.g. if the underlying digraph
    315     /// and/or the arc lengths have been modified).
     315    /// and/or the arc costs have been modified).
    316316    ///
    317317    /// \return \c true if a directed cycle exists in the digraph.
     
    319319    /// \note <tt>mmc.run()</tt> is just a shortcut of the following code.
    320320    /// \code
    321     ///   return mmc.findMinMean() && mmc.findCycle();
     321    ///   return mmc.findCycleMean() && mmc.findCycle();
    322322    /// \endcode
    323323    bool run() {
    324       return findMinMean() && findCycle();
     324      return findCycleMean() && findCycle();
    325325    }
    326326
    327327    /// \brief Find the minimum cycle mean.
    328328    ///
    329     /// This function finds the minimum mean length of the directed
     329    /// This function finds the minimum mean cost of the directed
    330330    /// cycles in the digraph.
    331331    ///
    332332    /// \return \c true if a directed cycle exists in the digraph.
    333     bool findMinMean() {
     333    bool findCycleMean() {
    334334      // Initialize and find strongly connected components
    335335      init();
     
    346346        // Update the best cycle (global minimum mean cycle)
    347347        if ( _curr_found && (!_best_found ||
    348              _curr_length * _best_size < _best_length * _curr_size) ) {
     348             _curr_cost * _best_size < _best_cost * _curr_size) ) {
    349349          _best_found = true;
    350           _best_length = _curr_length;
     350          _best_cost = _curr_cost;
    351351          _best_size = _curr_size;
    352352          _best_node = _curr_node;
     
    358358    /// \brief Find a minimum mean directed cycle.
    359359    ///
    360     /// This function finds a directed cycle of minimum mean length
    361     /// in the digraph using the data computed by findMinMean().
     360    /// This function finds a directed cycle of minimum mean cost
     361    /// in the digraph using the data computed by findCycleMean().
    362362    ///
    363363    /// \return \c true if a directed cycle exists in the digraph.
    364364    ///
    365     /// \pre \ref findMinMean() must be called before using this function.
     365    /// \pre \ref findCycleMean() must be called before using this function.
    366366    bool findCycle() {
    367367      if (!_best_found) return false;
     
    383383    /// @{
    384384
    385     /// \brief Return the total length of the found cycle.
    386     ///
    387     /// This function returns the total length of the found cycle.
    388     ///
    389     /// \pre \ref run() or \ref findMinMean() must be called before
     385    /// \brief Return the total cost of the found cycle.
     386    ///
     387    /// This function returns the total cost of the found cycle.
     388    ///
     389    /// \pre \ref run() or \ref findCycleMean() must be called before
    390390    /// using this function.
    391     Value cycleLength() const {
    392       return static_cast<Value>(_best_length);
     391    Cost cycleCost() const {
     392      return static_cast<Cost>(_best_cost);
    393393    }
    394394
     
    397397    /// This function returns the number of arcs on the found cycle.
    398398    ///
    399     /// \pre \ref run() or \ref findMinMean() must be called before
     399    /// \pre \ref run() or \ref findCycleMean() must be called before
    400400    /// using this function.
    401     int cycleArcNum() const {
     401    int cycleSize() const {
    402402      return _best_size;
    403403    }
    404404
    405     /// \brief Return the mean length of the found cycle.
    406     ///
    407     /// This function returns the mean length of the found cycle.
     405    /// \brief Return the mean cost of the found cycle.
     406    ///
     407    /// This function returns the mean cost of the found cycle.
    408408    ///
    409409    /// \note <tt>alg.cycleMean()</tt> is just a shortcut of the
    410410    /// following code.
    411411    /// \code
    412     ///   return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum();
     412    ///   return static_cast<double>(alg.cycleCost()) / alg.cycleSize();
    413413    /// \endcode
    414414    ///
    415     /// \pre \ref run() or \ref findMinMean() must be called before
     415    /// \pre \ref run() or \ref findCycleMean() must be called before
    416416    /// using this function.
    417417    double cycleMean() const {
    418       return static_cast<double>(_best_length) / _best_size;
     418      return static_cast<double>(_best_cost) / _best_size;
    419419    }
    420420
     
    442442      _queue.resize(countNodes(_gr));
    443443      _best_found = false;
    444       _best_length = 0;
     444      _best_cost = 0;
    445445      _best_size = 1;
    446446      _cycle_path->clear();
     
    493493          e = _in_arcs[v][j];
    494494          u = _gr.source(e);
    495           if (_length[e] < _dist[u]) {
    496             _dist[u] = _length[e];
     495          if (_cost[e] < _dist[u]) {
     496            _dist[u] = _cost[e];
    497497            _policy[u] = e;
    498498          }
     
    507507        _level[(*_nodes)[i]] = -1;
    508508      }
    509       LargeValue clength;
     509      LargeCost ccost;
    510510      int csize;
    511511      Node u, v;
     
    519519        if (_level[u] == i) {
    520520          // A cycle is found
    521           clength = _length[_policy[u]];
     521          ccost = _cost[_policy[u]];
    522522          csize = 1;
    523523          for (v = u; (v = _gr.target(_policy[v])) != u; ) {
    524             clength += _length[_policy[v]];
     524            ccost += _cost[_policy[v]];
    525525            ++csize;
    526526          }
    527527          if ( !_curr_found ||
    528                (clength * _curr_size < _curr_length * csize) ) {
     528               (ccost * _curr_size < _curr_cost * csize) ) {
    529529            _curr_found = true;
    530             _curr_length = clength;
     530            _curr_cost = ccost;
    531531            _curr_size = csize;
    532532            _curr_node = u;
     
    556556          if (_policy[u] == e && !_reached[u]) {
    557557            _reached[u] = true;
    558             _dist[u] = _dist[v] + _length[e] * _curr_size - _curr_length;
     558            _dist[u] = _dist[v] + _cost[e] * _curr_size - _curr_cost;
    559559            _queue[++_qback] = u;
    560560          }
     
    573573            _reached[u] = true;
    574574            _policy[u] = e;
    575             _dist[u] = _dist[v] + _length[e] * _curr_size - _curr_length;
     575            _dist[u] = _dist[v] + _cost[e] * _curr_size - _curr_cost;
    576576            _queue[++_qback] = u;
    577577          }
     
    586586          e = _in_arcs[v][j];
    587587          u = _gr.source(e);
    588           LargeValue delta = _dist[v] + _length[e] * _curr_size - _curr_length;
     588          LargeCost delta = _dist[v] + _cost[e] * _curr_size - _curr_cost;
    589589          if (_tolerance.less(delta, _dist[u])) {
    590590            _dist[u] = delta;
     
    597597    }
    598598
    599   }; //class Howard
     599  }; //class HowardMmc
    600600
    601601  ///@}
     
    603603} //namespace lemon
    604604
    605 #endif //LEMON_HOWARD_H
     605#endif //LEMON_HOWARD_MMC_H
  • lemon/karp_mmc.h

    r863 r864  
    1717 */
    1818
    19 #ifndef LEMON_KARP_H
    20 #define LEMON_KARP_H
     19#ifndef LEMON_KARP_MMC_H
     20#define LEMON_KARP_MMC_H
    2121
    2222/// \ingroup min_mean_cycle
     
    3434namespace lemon {
    3535
    36   /// \brief Default traits class of Karp algorithm.
     36  /// \brief Default traits class of KarpMmc class.
    3737  ///
    38   /// Default traits class of Karp algorithm.
     38  /// Default traits class of KarpMmc class.
    3939  /// \tparam GR The type of the digraph.
    40   /// \tparam LEN The type of the length map.
     40  /// \tparam CM The type of the cost map.
    4141  /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
    4242#ifdef DOXYGEN
    43   template <typename GR, typename LEN>
     43  template <typename GR, typename CM>
    4444#else
    45   template <typename GR, typename LEN,
    46     bool integer = std::numeric_limits<typename LEN::Value>::is_integer>
     45  template <typename GR, typename CM,
     46    bool integer = std::numeric_limits<typename CM::Value>::is_integer>
    4747#endif
    48   struct KarpDefaultTraits
     48  struct KarpMmcDefaultTraits
    4949  {
    5050    /// The type of the digraph
    5151    typedef GR Digraph;
    52     /// The type of the length map
    53     typedef LEN LengthMap;
    54     /// The type of the arc lengths
    55     typedef typename LengthMap::Value Value;
    56 
    57     /// \brief The large value type used for internal computations
    58     ///
    59     /// The large value type used for internal computations.
    60     /// It is \c long \c long if the \c Value type is integer,
     52    /// The type of the cost map
     53    typedef CM CostMap;
     54    /// The type of the arc costs
     55    typedef typename CostMap::Value Cost;
     56
     57    /// \brief The large cost type used for internal computations
     58    ///
     59    /// The large cost type used for internal computations.
     60    /// It is \c long \c long if the \c Cost type is integer,
    6161    /// otherwise it is \c double.
    62     /// \c Value must be convertible to \c LargeValue.
    63     typedef double LargeValue;
     62    /// \c Cost must be convertible to \c LargeCost.
     63    typedef double LargeCost;
    6464
    6565    /// The tolerance type used for internal computations
    66     typedef lemon::Tolerance<LargeValue> Tolerance;
     66    typedef lemon::Tolerance<LargeCost> Tolerance;
    6767
    6868    /// \brief The path type of the found cycles
     
    7474  };
    7575
    76   // Default traits class for integer value types
    77   template <typename GR, typename LEN>
    78   struct KarpDefaultTraits<GR, LEN, true>
     76  // Default traits class for integer cost types
     77  template <typename GR, typename CM>
     78  struct KarpMmcDefaultTraits<GR, CM, true>
    7979  {
    8080    typedef GR Digraph;
    81     typedef LEN LengthMap;
    82     typedef typename LengthMap::Value Value;
     81    typedef CM CostMap;
     82    typedef typename CostMap::Value Cost;
    8383#ifdef LEMON_HAVE_LONG_LONG
    84     typedef long long LargeValue;
     84    typedef long long LargeCost;
    8585#else
    86     typedef long LargeValue;
     86    typedef long LargeCost;
    8787#endif
    88     typedef lemon::Tolerance<LargeValue> Tolerance;
     88    typedef lemon::Tolerance<LargeCost> Tolerance;
    8989    typedef lemon::Path<Digraph> Path;
    9090  };
     
    9898  ///
    9999  /// This class implements Karp's algorithm for finding a directed
    100   /// cycle of minimum mean length (cost) in a digraph
     100  /// cycle of minimum mean cost in a digraph
    101101  /// \ref amo93networkflows, \ref dasdan98minmeancycle.
    102102  /// It runs in time O(ne) and uses space O(n<sup>2</sup>+e).
    103103  ///
    104104  /// \tparam GR The type of the digraph the algorithm runs on.
    105   /// \tparam LEN The type of the length map. The default
     105  /// \tparam CM The type of the cost map. The default
    106106  /// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
    107107  /// \tparam TR The traits class that defines various types used by the
    108   /// algorithm. By default, it is \ref KarpDefaultTraits
    109   /// "KarpDefaultTraits<GR, LEN>".
     108  /// algorithm. By default, it is \ref KarpMmcDefaultTraits
     109  /// "KarpMmcDefaultTraits<GR, CM>".
    110110  /// In most cases, this parameter should not be set directly,
    111111  /// consider to use the named template parameters instead.
    112112#ifdef DOXYGEN
    113   template <typename GR, typename LEN, typename TR>
     113  template <typename GR, typename CM, typename TR>
    114114#else
    115115  template < typename GR,
    116              typename LEN = typename GR::template ArcMap<int>,
    117              typename TR = KarpDefaultTraits<GR, LEN> >
     116             typename CM = typename GR::template ArcMap<int>,
     117             typename TR = KarpMmcDefaultTraits<GR, CM> >
    118118#endif
    119   class Karp
     119  class KarpMmc
    120120  {
    121121  public:
     
    123123    /// The type of the digraph
    124124    typedef typename TR::Digraph Digraph;
    125     /// The type of the length map
    126     typedef typename TR::LengthMap LengthMap;
    127     /// The type of the arc lengths
    128     typedef typename TR::Value Value;
    129 
    130     /// \brief The large value type
    131     ///
    132     /// The large value type used for internal computations.
    133     /// By default, it is \c long \c long if the \c Value type is integer,
     125    /// The type of the cost map
     126    typedef typename TR::CostMap CostMap;
     127    /// The type of the arc costs
     128    typedef typename TR::Cost Cost;
     129
     130    /// \brief The large cost type
     131    ///
     132    /// The large cost type used for internal computations.
     133    /// By default, it is \c long \c long if the \c Cost type is integer,
    134134    /// otherwise it is \c double.
    135     typedef typename TR::LargeValue LargeValue;
     135    typedef typename TR::LargeCost LargeCost;
    136136
    137137    /// The tolerance type
     
    141141    ///
    142142    /// The path type of the found cycles.
    143     /// Using the \ref KarpDefaultTraits "default traits class",
     143    /// Using the \ref KarpMmcDefaultTraits "default traits class",
    144144    /// it is \ref lemon::Path "Path<Digraph>".
    145145    typedef typename TR::Path Path;
    146146
    147     /// The \ref KarpDefaultTraits "traits class" of the algorithm
     147    /// The \ref KarpMmcDefaultTraits "traits class" of the algorithm
    148148    typedef TR Traits;
    149149
     
    155155    struct PathData
    156156    {
    157       LargeValue dist;
     157      LargeCost dist;
    158158      Arc pred;
    159       PathData(LargeValue d, Arc p = INVALID) :
     159      PathData(LargeCost d, Arc p = INVALID) :
    160160        dist(d), pred(p) {}
    161161    };
     
    168168    // The digraph the algorithm runs on
    169169    const Digraph &_gr;
    170     // The length of the arcs
    171     const LengthMap &_length;
     170    // The cost of the arcs
     171    const CostMap &_cost;
    172172
    173173    // Data for storing the strongly connected components
     
    179179
    180180    // Data for the found cycle
    181     LargeValue _cycle_length;
     181    LargeCost _cycle_cost;
    182182    int _cycle_size;
    183183    Node _cycle_node;
     
    194194   
    195195    // Infinite constant
    196     const LargeValue INF;
     196    const LargeCost INF;
    197197
    198198  public:
     
    202202
    203203    template <typename T>
    204     struct SetLargeValueTraits : public Traits {
    205       typedef T LargeValue;
     204    struct SetLargeCostTraits : public Traits {
     205      typedef T LargeCost;
    206206      typedef lemon::Tolerance<T> Tolerance;
    207207    };
    208208
    209209    /// \brief \ref named-templ-param "Named parameter" for setting
    210     /// \c LargeValue type.
    211     ///
    212     /// \ref named-templ-param "Named parameter" for setting \c LargeValue
     210    /// \c LargeCost type.
     211    ///
     212    /// \ref named-templ-param "Named parameter" for setting \c LargeCost
    213213    /// type. It is used for internal computations in the algorithm.
    214214    template <typename T>
    215     struct SetLargeValue
    216       : public Karp<GR, LEN, SetLargeValueTraits<T> > {
    217       typedef Karp<GR, LEN, SetLargeValueTraits<T> > Create;
     215    struct SetLargeCost
     216      : public KarpMmc<GR, CM, SetLargeCostTraits<T> > {
     217      typedef KarpMmc<GR, CM, SetLargeCostTraits<T> > Create;
    218218    };
    219219
     
    232232    template <typename T>
    233233    struct SetPath
    234       : public Karp<GR, LEN, SetPathTraits<T> > {
    235       typedef Karp<GR, LEN, SetPathTraits<T> > Create;
     234      : public KarpMmc<GR, CM, SetPathTraits<T> > {
     235      typedef KarpMmc<GR, CM, SetPathTraits<T> > Create;
    236236    };
    237237
     
    240240  protected:
    241241
    242     Karp() {}
     242    KarpMmc() {}
    243243
    244244  public:
     
    249249    ///
    250250    /// \param digraph The digraph the algorithm runs on.
    251     /// \param length The lengths (costs) of the arcs.
    252     Karp( const Digraph &digraph,
    253           const LengthMap &length ) :
    254       _gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph),
    255       _cycle_length(0), _cycle_size(1), _cycle_node(INVALID),
     251    /// \param cost The costs of the arcs.
     252    KarpMmc( const Digraph &digraph,
     253             const CostMap &cost ) :
     254      _gr(digraph), _cost(cost), _comp(digraph), _out_arcs(digraph),
     255      _cycle_cost(0), _cycle_size(1), _cycle_node(INVALID),
    256256      _cycle_path(NULL), _local_path(false), _data(digraph),
    257       INF(std::numeric_limits<LargeValue>::has_infinity ?
    258           std::numeric_limits<LargeValue>::infinity() :
    259           std::numeric_limits<LargeValue>::max())
     257      INF(std::numeric_limits<LargeCost>::has_infinity ?
     258          std::numeric_limits<LargeCost>::infinity() :
     259          std::numeric_limits<LargeCost>::max())
    260260    {}
    261261
    262262    /// Destructor.
    263     ~Karp() {
     263    ~KarpMmc() {
    264264      if (_local_path) delete _cycle_path;
    265265    }
     
    271271    ///
    272272    /// If you don't call this function before calling \ref run() or
    273     /// \ref findMinMean(), it will allocate a local \ref Path "path"
     273    /// \ref findCycleMean(), it will allocate a local \ref Path "path"
    274274    /// structure. The destuctor deallocates this automatically
    275275    /// allocated object, of course.
     
    279279    ///
    280280    /// \return <tt>(*this)</tt>
    281     Karp& cycle(Path &path) {
     281    KarpMmc& cycle(Path &path) {
    282282      if (_local_path) {
    283283        delete _cycle_path;
     
    293293    ///
    294294    /// \return <tt>(*this)</tt>
    295     Karp& tolerance(const Tolerance& tolerance) {
     295    KarpMmc& tolerance(const Tolerance& tolerance) {
    296296      _tolerance = tolerance;
    297297      return *this;
     
    309309    /// The simplest way to execute the algorithm is to call the \ref run()
    310310    /// function.\n
    311     /// If you only need the minimum mean length, you may call
    312     /// \ref findMinMean().
     311    /// If you only need the minimum mean cost, you may call
     312    /// \ref findCycleMean().
    313313
    314314    /// @{
     
    318318    /// This function runs the algorithm.
    319319    /// It can be called more than once (e.g. if the underlying digraph
    320     /// and/or the arc lengths have been modified).
     320    /// and/or the arc costs have been modified).
    321321    ///
    322322    /// \return \c true if a directed cycle exists in the digraph.
     
    324324    /// \note <tt>mmc.run()</tt> is just a shortcut of the following code.
    325325    /// \code
    326     ///   return mmc.findMinMean() && mmc.findCycle();
     326    ///   return mmc.findCycleMean() && mmc.findCycle();
    327327    /// \endcode
    328328    bool run() {
    329       return findMinMean() && findCycle();
     329      return findCycleMean() && findCycle();
    330330    }
    331331
    332332    /// \brief Find the minimum cycle mean.
    333333    ///
    334     /// This function finds the minimum mean length of the directed
     334    /// This function finds the minimum mean cost of the directed
    335335    /// cycles in the digraph.
    336336    ///
    337337    /// \return \c true if a directed cycle exists in the digraph.
    338     bool findMinMean() {
     338    bool findCycleMean() {
    339339      // Initialization and find strongly connected components
    340340      init();
     
    352352    /// \brief Find a minimum mean directed cycle.
    353353    ///
    354     /// This function finds a directed cycle of minimum mean length
    355     /// in the digraph using the data computed by findMinMean().
     354    /// This function finds a directed cycle of minimum mean cost
     355    /// in the digraph using the data computed by findCycleMean().
    356356    ///
    357357    /// \return \c true if a directed cycle exists in the digraph.
    358358    ///
    359     /// \pre \ref findMinMean() must be called before using this function.
     359    /// \pre \ref findCycleMean() must be called before using this function.
    360360    bool findCycle() {
    361361      if (_cycle_node == INVALID) return false;
     
    370370      Arc e = _data[u][r].pred;
    371371      _cycle_path->addFront(e);
    372       _cycle_length = _length[e];
     372      _cycle_cost = _cost[e];
    373373      _cycle_size = 1;
    374374      Node v;
     
    376376        e = _data[v][--r].pred;
    377377        _cycle_path->addFront(e);
    378         _cycle_length += _length[e];
     378        _cycle_cost += _cost[e];
    379379        ++_cycle_size;
    380380      }
     
    391391    /// @{
    392392
    393     /// \brief Return the total length of the found cycle.
    394     ///
    395     /// This function returns the total length of the found cycle.
    396     ///
    397     /// \pre \ref run() or \ref findMinMean() must be called before
     393    /// \brief Return the total cost of the found cycle.
     394    ///
     395    /// This function returns the total cost of the found cycle.
     396    ///
     397    /// \pre \ref run() or \ref findCycleMean() must be called before
    398398    /// using this function.
    399     Value cycleLength() const {
    400       return static_cast<Value>(_cycle_length);
     399    Cost cycleCost() const {
     400      return static_cast<Cost>(_cycle_cost);
    401401    }
    402402
     
    405405    /// This function returns the number of arcs on the found cycle.
    406406    ///
    407     /// \pre \ref run() or \ref findMinMean() must be called before
     407    /// \pre \ref run() or \ref findCycleMean() must be called before
    408408    /// using this function.
    409     int cycleArcNum() const {
     409    int cycleSize() const {
    410410      return _cycle_size;
    411411    }
    412412
    413     /// \brief Return the mean length of the found cycle.
    414     ///
    415     /// This function returns the mean length of the found cycle.
     413    /// \brief Return the mean cost of the found cycle.
     414    ///
     415    /// This function returns the mean cost of the found cycle.
    416416    ///
    417417    /// \note <tt>alg.cycleMean()</tt> is just a shortcut of the
    418418    /// following code.
    419419    /// \code
    420     ///   return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum();
     420    ///   return static_cast<double>(alg.cycleCost()) / alg.cycleSize();
    421421    /// \endcode
    422422    ///
    423     /// \pre \ref run() or \ref findMinMean() must be called before
     423    /// \pre \ref run() or \ref findCycleMean() must be called before
    424424    /// using this function.
    425425    double cycleMean() const {
    426       return static_cast<double>(_cycle_length) / _cycle_size;
     426      return static_cast<double>(_cycle_cost) / _cycle_size;
    427427    }
    428428
     
    449449      }
    450450      _cycle_path->clear();
    451       _cycle_length = 0;
     451      _cycle_cost = 0;
    452452      _cycle_size = 1;
    453453      _cycle_node = INVALID;
     
    498498
    499499    // Process all rounds of computing path data for the current component.
    500     // _data[v][k] is the length of a shortest directed walk from the root
     500    // _data[v][k] is the cost of a shortest directed walk from the root
    501501    // node to node v containing exactly k arcs.
    502502    void processRounds() {
     
    520520      Node u, v;
    521521      Arc e;
    522       LargeValue d;
     522      LargeCost d;
    523523      for (int i = 0; i < int(_process.size()); ++i) {
    524524        u = _process[i];
     
    526526          e = _out_arcs[u][j];
    527527          v = _gr.target(e);
    528           d = _data[u][k-1].dist + _length[e];
     528          d = _data[u][k-1].dist + _cost[e];
    529529          if (_tolerance.less(d, _data[v][k].dist)) {
    530530            if (_data[v][k].dist == INF) next.push_back(v);
     
    540540      Node u, v;
    541541      Arc e;
    542       LargeValue d;
     542      LargeCost d;
    543543      for (int i = 0; i < int(_nodes->size()); ++i) {
    544544        u = (*_nodes)[i];
     
    546546          e = _out_arcs[u][j];
    547547          v = _gr.target(e);
    548           d = _data[u][k-1].dist + _length[e];
     548          d = _data[u][k-1].dist + _cost[e];
    549549          if (_tolerance.less(d, _data[v][k].dist)) {
    550550            _data[v][k] = PathData(d, e);
     
    560560        Node u = (*_nodes)[i];
    561561        if (_data[u][n].dist == INF) continue;
    562         LargeValue length, max_length = 0;
     562        LargeCost cost, max_cost = 0;
    563563        int size, max_size = 1;
    564564        bool found_curr = false;
    565565        for (int k = 0; k < n; ++k) {
    566566          if (_data[u][k].dist == INF) continue;
    567           length = _data[u][n].dist - _data[u][k].dist;
     567          cost = _data[u][n].dist - _data[u][k].dist;
    568568          size = n - k;
    569           if (!found_curr || length * max_size > max_length * size) {
     569          if (!found_curr || cost * max_size > max_cost * size) {
    570570            found_curr = true;
    571             max_length = length;
     571            max_cost = cost;
    572572            max_size = size;
    573573          }
    574574        }
    575575        if ( found_curr && (_cycle_node == INVALID ||
    576              max_length * _cycle_size < _cycle_length * max_size) ) {
    577           _cycle_length = max_length;
     576             max_cost * _cycle_size < _cycle_cost * max_size) ) {
     577          _cycle_cost = max_cost;
    578578          _cycle_size = max_size;
    579579          _cycle_node = u;
     
    582582    }
    583583
    584   }; //class Karp
     584  }; //class KarpMmc
    585585
    586586  ///@}
     
    588588} //namespace lemon
    589589
    590 #endif //LEMON_KARP_H
     590#endif //LEMON_KARP_MMC_H
  • test/min_mean_cycle_test.cc

    r769 r864  
    2626#include <lemon/concept_check.h>
    2727
    28 #include <lemon/karp.h>
    29 #include <lemon/hartmann_orlin.h>
    30 #include <lemon/howard.h>
     28#include <lemon/karp_mmc.h>
     29#include <lemon/hartmann_orlin_mmc.h>
     30#include <lemon/howard_mmc.h>
    3131
    3232#include "test_tools.h"
     
    6464                       
    6565// Check the interface of an MMC algorithm
    66 template <typename GR, typename Value>
     66template <typename GR, typename Cost>
    6767struct MmcClassConcept
    6868{
     
    7474      typedef typename MMC
    7575        ::template SetPath<ListPath<GR> >
    76         ::template SetLargeValue<Value>
     76        ::template SetLargeCost<Cost>
    7777        ::Create MmcAlg;
    78       MmcAlg mmc(me.g, me.length);
     78      MmcAlg mmc(me.g, me.cost);
    7979      const MmcAlg& const_mmc = mmc;
    8080     
     
    8383     
    8484      b = mmc.cycle(p).run();
    85       b = mmc.findMinMean();
     85      b = mmc.findCycleMean();
    8686      b = mmc.findCycle();
    8787
    88       v = const_mmc.cycleLength();
    89       i = const_mmc.cycleArcNum();
     88      v = const_mmc.cycleCost();
     89      i = const_mmc.cycleSize();
    9090      d = const_mmc.cycleMean();
    9191      p = const_mmc.cycle();
    9292    }
    9393
    94     typedef concepts::ReadMap<typename GR::Arc, Value> LM;
     94    typedef concepts::ReadMap<typename GR::Arc, Cost> CM;
    9595 
    9696    GR g;
    97     LM length;
     97    CM cost;
    9898    ListPath<GR> p;
    99     Value v;
     99    Cost v;
    100100    int i;
    101101    double d;
     
    109109                 const SmartDigraph::ArcMap<int>& lm,
    110110                 const SmartDigraph::ArcMap<int>& cm,
    111                  int length, int size) {
     111                 int cost, int size) {
    112112  MMC alg(gr, lm);
    113   alg.findMinMean();
    114   check(alg.cycleMean() == static_cast<double>(length) / size,
     113  alg.findCycleMean();
     114  check(alg.cycleMean() == static_cast<double>(cost) / size,
    115115        "Wrong cycle mean");
    116116  alg.findCycle();
    117   check(alg.cycleLength() == length && alg.cycleArcNum() == size,
     117  check(alg.cycleCost() == cost && alg.cycleSize() == size,
    118118        "Wrong path");
    119119  SmartDigraph::ArcMap<int> cycle(gr, 0);
     
    149149    typedef concepts::Digraph GR;
    150150
    151     // Karp
     151    // KarpMmc
    152152    checkConcept< MmcClassConcept<GR, int>,
    153                   Karp<GR, concepts::ReadMap<GR::Arc, int> > >();
     153                  KarpMmc<GR, concepts::ReadMap<GR::Arc, int> > >();
    154154    checkConcept< MmcClassConcept<GR, float>,
    155                   Karp<GR, concepts::ReadMap<GR::Arc, float> > >();
    156    
    157     // HartmannOrlin
     155                  KarpMmc<GR, concepts::ReadMap<GR::Arc, float> > >();
     156   
     157    // HartmannOrlinMmc
    158158    checkConcept< MmcClassConcept<GR, int>,
    159                   HartmannOrlin<GR, concepts::ReadMap<GR::Arc, int> > >();
     159                  HartmannOrlinMmc<GR, concepts::ReadMap<GR::Arc, int> > >();
    160160    checkConcept< MmcClassConcept<GR, float>,
    161                   HartmannOrlin<GR, concepts::ReadMap<GR::Arc, float> > >();
    162    
    163     // Howard
     161                  HartmannOrlinMmc<GR, concepts::ReadMap<GR::Arc, float> > >();
     162   
     163    // HowardMmc
    164164    checkConcept< MmcClassConcept<GR, int>,
    165                   Howard<GR, concepts::ReadMap<GR::Arc, int> > >();
     165                  HowardMmc<GR, concepts::ReadMap<GR::Arc, int> > >();
    166166    checkConcept< MmcClassConcept<GR, float>,
    167                   Howard<GR, concepts::ReadMap<GR::Arc, float> > >();
    168 
    169     if (IsSameType<Howard<GR, concepts::ReadMap<GR::Arc, int> >::LargeValue,
    170           long_int>::result == 0) check(false, "Wrong LargeValue type");
    171     if (IsSameType<Howard<GR, concepts::ReadMap<GR::Arc, float> >::LargeValue,
    172           double>::result == 0) check(false, "Wrong LargeValue type");
     167                  HowardMmc<GR, concepts::ReadMap<GR::Arc, float> > >();
     168
     169    check((IsSameType<HowardMmc<GR, concepts::ReadMap<GR::Arc, int> >
     170           ::LargeCost, long_int>::result == 1), "Wrong LargeCost type");
     171    check((IsSameType<HowardMmc<GR, concepts::ReadMap<GR::Arc, float> >
     172           ::LargeCost, double>::result == 1), "Wrong LargeCost type");
    173173  }
    174174
     
    195195
    196196    // Karp
    197     checkMmcAlg<Karp<GR, IntArcMap> >(gr, l1, c1,  6, 3);
    198     checkMmcAlg<Karp<GR, IntArcMap> >(gr, l2, c2,  5, 2);
    199     checkMmcAlg<Karp<GR, IntArcMap> >(gr, l3, c3,  0, 1);
    200     checkMmcAlg<Karp<GR, IntArcMap> >(gr, l4, c4, -1, 1);
     197    checkMmcAlg<KarpMmc<GR, IntArcMap> >(gr, l1, c1,  6, 3);
     198    checkMmcAlg<KarpMmc<GR, IntArcMap> >(gr, l2, c2,  5, 2);
     199    checkMmcAlg<KarpMmc<GR, IntArcMap> >(gr, l3, c3,  0, 1);
     200    checkMmcAlg<KarpMmc<GR, IntArcMap> >(gr, l4, c4, -1, 1);
    201201
    202202    // HartmannOrlin
    203     checkMmcAlg<HartmannOrlin<GR, IntArcMap> >(gr, l1, c1,  6, 3);
    204     checkMmcAlg<HartmannOrlin<GR, IntArcMap> >(gr, l2, c2,  5, 2);
    205     checkMmcAlg<HartmannOrlin<GR, IntArcMap> >(gr, l3, c3,  0, 1);
    206     checkMmcAlg<HartmannOrlin<GR, IntArcMap> >(gr, l4, c4, -1, 1);
     203    checkMmcAlg<HartmannOrlinMmc<GR, IntArcMap> >(gr, l1, c1,  6, 3);
     204    checkMmcAlg<HartmannOrlinMmc<GR, IntArcMap> >(gr, l2, c2,  5, 2);
     205    checkMmcAlg<HartmannOrlinMmc<GR, IntArcMap> >(gr, l3, c3,  0, 1);
     206    checkMmcAlg<HartmannOrlinMmc<GR, IntArcMap> >(gr, l4, c4, -1, 1);
    207207
    208208    // Howard
    209     checkMmcAlg<Howard<GR, IntArcMap> >(gr, l1, c1,  6, 3);
    210     checkMmcAlg<Howard<GR, IntArcMap> >(gr, l2, c2,  5, 2);
    211     checkMmcAlg<Howard<GR, IntArcMap> >(gr, l3, c3,  0, 1);
    212     checkMmcAlg<Howard<GR, IntArcMap> >(gr, l4, c4, -1, 1);
     209    checkMmcAlg<HowardMmc<GR, IntArcMap> >(gr, l1, c1,  6, 3);
     210    checkMmcAlg<HowardMmc<GR, IntArcMap> >(gr, l2, c2,  5, 2);
     211    checkMmcAlg<HowardMmc<GR, IntArcMap> >(gr, l3, c3,  0, 1);
     212    checkMmcAlg<HowardMmc<GR, IntArcMap> >(gr, l4, c4, -1, 1);
    213213  }
    214214
Note: See TracChangeset for help on using the changeset viewer.