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