COIN-OR::LEMON - Graph Library

source: lemon/lemon/karp.h @ 814:11c946fa8d13

Last change on this file since 814:11c946fa8d13 was 814:11c946fa8d13, checked in by Peter Kovacs <kpeter@…>, 15 years ago

Simplify comparisons in min mean cycle classes (#179)
using extreme INF values instead of bool flags.

File size: 16.3 KB
Line 
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_KARP_H
20#define LEMON_KARP_H
21
22/// \ingroup shortest_path
23///
24/// \file
25/// \brief Karp'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
34namespace lemon {
35
36  /// \brief Default traits class of Karp algorithm.
37  ///
38  /// Default traits class of Karp 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::ReadMap "ReadMap" 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 KarpDefaultTraits
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 KarpDefaultTraits<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 shortest_path
94  /// @{
95
96  /// \brief Implementation of Karp's algorithm for finding a minimum
97  /// mean cycle.
98  ///
99  /// This class implements Karp's algorithm for finding a directed
100  /// cycle of minimum mean length (cost) in a digraph.
101  ///
102  /// \tparam GR The type of the digraph the algorithm runs on.
103  /// \tparam LEN The type of the length map. The default
104  /// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
105#ifdef DOXYGEN
106  template <typename GR, typename LEN, typename TR>
107#else
108  template < typename GR,
109             typename LEN = typename GR::template ArcMap<int>,
110             typename TR = KarpDefaultTraits<GR, LEN> >
111#endif
112  class Karp
113  {
114  public:
115
116    /// The type of the digraph
117    typedef typename TR::Digraph Digraph;
118    /// The type of the length map
119    typedef typename TR::LengthMap LengthMap;
120    /// The type of the arc lengths
121    typedef typename TR::Value Value;
122
123    /// \brief The large value type
124    ///
125    /// The large value type used for internal computations.
126    /// Using the \ref KarpDefaultTraits "default traits class",
127    /// it is \c long \c long if the \c Value type is integer,
128    /// otherwise it is \c double.
129    typedef typename TR::LargeValue LargeValue;
130
131    /// The tolerance type
132    typedef typename TR::Tolerance Tolerance;
133
134    /// \brief The path type of the found cycles
135    ///
136    /// The path type of the found cycles.
137    /// Using the \ref KarpDefaultTraits "default traits class",
138    /// it is \ref lemon::Path "Path<Digraph>".
139    typedef typename TR::Path Path;
140
141    /// The \ref KarpDefaultTraits "traits class" of the algorithm
142    typedef TR Traits;
143
144  private:
145
146    TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
147
148    // Data sturcture for path data
149    struct PathData
150    {
151      LargeValue dist;
152      Arc pred;
153      PathData(LargeValue d, Arc p = INVALID) :
154        dist(d), pred(p) {}
155    };
156
157    typedef typename Digraph::template NodeMap<std::vector<PathData> >
158      PathDataNodeMap;
159
160  private:
161
162    // The digraph the algorithm runs on
163    const Digraph &_gr;
164    // The length of the arcs
165    const LengthMap &_length;
166
167    // Data for storing the strongly connected components
168    int _comp_num;
169    typename Digraph::template NodeMap<int> _comp;
170    std::vector<std::vector<Node> > _comp_nodes;
171    std::vector<Node>* _nodes;
172    typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs;
173
174    // Data for the found cycle
175    LargeValue _cycle_length;
176    int _cycle_size;
177    Node _cycle_node;
178
179    Path *_cycle_path;
180    bool _local_path;
181
182    // Node map for storing path data
183    PathDataNodeMap _data;
184    // The processed nodes in the last round
185    std::vector<Node> _process;
186
187    Tolerance _tolerance;
188   
189    // Infinite constant
190    const LargeValue INF;
191
192  public:
193
194    /// \name Named Template Parameters
195    /// @{
196
197    template <typename T>
198    struct SetLargeValueTraits : public Traits {
199      typedef T LargeValue;
200      typedef lemon::Tolerance<T> Tolerance;
201    };
202
203    /// \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
207    /// type. It is used for internal computations in the algorithm.
208    template <typename T>
209    struct SetLargeValue
210      : public Karp<GR, LEN, SetLargeValueTraits<T> > {
211      typedef Karp<GR, LEN, SetLargeValueTraits<T> > Create;
212    };
213
214    template <typename T>
215    struct SetPathTraits : public Traits {
216      typedef T Path;
217    };
218
219    /// \brief \ref named-templ-param "Named parameter" for setting
220    /// \c %Path type.
221    ///
222    /// \ref named-templ-param "Named parameter" for setting the \c %Path
223    /// type of the found cycles.
224    /// It must conform to the \ref lemon::concepts::Path "Path" concept
225    /// and it must have an \c addFront() function.
226    template <typename T>
227    struct SetPath
228      : public Karp<GR, LEN, SetPathTraits<T> > {
229      typedef Karp<GR, LEN, SetPathTraits<T> > Create;
230    };
231
232    /// @}
233
234  public:
235
236    /// \brief Constructor.
237    ///
238    /// The constructor of the class.
239    ///
240    /// \param digraph The digraph the algorithm runs on.
241    /// \param length The lengths (costs) of the arcs.
242    Karp( const Digraph &digraph,
243          const LengthMap &length ) :
244      _gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph),
245      _cycle_length(0), _cycle_size(1), _cycle_node(INVALID),
246      _cycle_path(NULL), _local_path(false), _data(digraph),
247      INF(std::numeric_limits<LargeValue>::has_infinity ?
248          std::numeric_limits<LargeValue>::infinity() :
249          std::numeric_limits<LargeValue>::max())
250    {}
251
252    /// Destructor.
253    ~Karp() {
254      if (_local_path) delete _cycle_path;
255    }
256
257    /// \brief Set the path structure for storing the found cycle.
258    ///
259    /// This function sets an external path structure for storing the
260    /// found cycle.
261    ///
262    /// If you don't call this function before calling \ref run() or
263    /// \ref findMinMean(), it will allocate a local \ref Path "path"
264    /// structure. The destuctor deallocates this automatically
265    /// allocated object, of course.
266    ///
267    /// \note The algorithm calls only the \ref lemon::Path::addFront()
268    /// "addFront()" function of the given path structure.
269    ///
270    /// \return <tt>(*this)</tt>
271    Karp& cycle(Path &path) {
272      if (_local_path) {
273        delete _cycle_path;
274        _local_path = false;
275      }
276      _cycle_path = &path;
277      return *this;
278    }
279
280    /// \name Execution control
281    /// The simplest way to execute the algorithm is to call the \ref run()
282    /// function.\n
283    /// If you only need the minimum mean length, you may call
284    /// \ref findMinMean().
285
286    /// @{
287
288    /// \brief Run the algorithm.
289    ///
290    /// This function runs the algorithm.
291    /// It can be called more than once (e.g. if the underlying digraph
292    /// and/or the arc lengths have been modified).
293    ///
294    /// \return \c true if a directed cycle exists in the digraph.
295    ///
296    /// \note <tt>mmc.run()</tt> is just a shortcut of the following code.
297    /// \code
298    ///   return mmc.findMinMean() && mmc.findCycle();
299    /// \endcode
300    bool run() {
301      return findMinMean() && findCycle();
302    }
303
304    /// \brief Find the minimum cycle mean.
305    ///
306    /// This function finds the minimum mean length of the directed
307    /// cycles in the digraph.
308    ///
309    /// \return \c true if a directed cycle exists in the digraph.
310    bool findMinMean() {
311      // Initialization and find strongly connected components
312      init();
313      findComponents();
314     
315      // Find the minimum cycle mean in the components
316      for (int comp = 0; comp < _comp_num; ++comp) {
317        if (!initComponent(comp)) continue;
318        processRounds();
319        updateMinMean();
320      }
321      return (_cycle_node != INVALID);
322    }
323
324    /// \brief Find a minimum mean directed cycle.
325    ///
326    /// This function finds a directed cycle of minimum mean length
327    /// in the digraph using the data computed by findMinMean().
328    ///
329    /// \return \c true if a directed cycle exists in the digraph.
330    ///
331    /// \pre \ref findMinMean() must be called before using this function.
332    bool findCycle() {
333      if (_cycle_node == INVALID) return false;
334      IntNodeMap reached(_gr, -1);
335      int r = _data[_cycle_node].size();
336      Node u = _cycle_node;
337      while (reached[u] < 0) {
338        reached[u] = --r;
339        u = _gr.source(_data[u][r].pred);
340      }
341      r = reached[u];
342      Arc e = _data[u][r].pred;
343      _cycle_path->addFront(e);
344      _cycle_length = _length[e];
345      _cycle_size = 1;
346      Node v;
347      while ((v = _gr.source(e)) != u) {
348        e = _data[v][--r].pred;
349        _cycle_path->addFront(e);
350        _cycle_length += _length[e];
351        ++_cycle_size;
352      }
353      return true;
354    }
355
356    /// @}
357
358    /// \name Query Functions
359    /// The results of the algorithm can be obtained using these
360    /// functions.\n
361    /// The algorithm should be executed before using them.
362
363    /// @{
364
365    /// \brief Return the total length of the found cycle.
366    ///
367    /// This function returns the total length of the found cycle.
368    ///
369    /// \pre \ref run() or \ref findMinMean() must be called before
370    /// using this function.
371    LargeValue cycleLength() const {
372      return _cycle_length;
373    }
374
375    /// \brief Return the number of arcs on the found cycle.
376    ///
377    /// This function returns the number of arcs on the found cycle.
378    ///
379    /// \pre \ref run() or \ref findMinMean() must be called before
380    /// using this function.
381    int cycleArcNum() const {
382      return _cycle_size;
383    }
384
385    /// \brief Return the mean length of the found cycle.
386    ///
387    /// This function returns the mean length of the found cycle.
388    ///
389    /// \note <tt>alg.cycleMean()</tt> is just a shortcut of the
390    /// following code.
391    /// \code
392    ///   return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum();
393    /// \endcode
394    ///
395    /// \pre \ref run() or \ref findMinMean() must be called before
396    /// using this function.
397    double cycleMean() const {
398      return static_cast<double>(_cycle_length) / _cycle_size;
399    }
400
401    /// \brief Return the found cycle.
402    ///
403    /// This function returns a const reference to the path structure
404    /// storing the found cycle.
405    ///
406    /// \pre \ref run() or \ref findCycle() must be called before using
407    /// this function.
408    const Path& cycle() const {
409      return *_cycle_path;
410    }
411
412    ///@}
413
414  private:
415
416    // Initialization
417    void init() {
418      if (!_cycle_path) {
419        _local_path = true;
420        _cycle_path = new Path;
421      }
422      _cycle_path->clear();
423      _cycle_length = 0;
424      _cycle_size = 1;
425      _cycle_node = INVALID;
426      for (NodeIt u(_gr); u != INVALID; ++u)
427        _data[u].clear();
428    }
429
430    // Find strongly connected components and initialize _comp_nodes
431    // and _out_arcs
432    void findComponents() {
433      _comp_num = stronglyConnectedComponents(_gr, _comp);
434      _comp_nodes.resize(_comp_num);
435      if (_comp_num == 1) {
436        _comp_nodes[0].clear();
437        for (NodeIt n(_gr); n != INVALID; ++n) {
438          _comp_nodes[0].push_back(n);
439          _out_arcs[n].clear();
440          for (OutArcIt a(_gr, n); a != INVALID; ++a) {
441            _out_arcs[n].push_back(a);
442          }
443        }
444      } else {
445        for (int i = 0; i < _comp_num; ++i)
446          _comp_nodes[i].clear();
447        for (NodeIt n(_gr); n != INVALID; ++n) {
448          int k = _comp[n];
449          _comp_nodes[k].push_back(n);
450          _out_arcs[n].clear();
451          for (OutArcIt a(_gr, n); a != INVALID; ++a) {
452            if (_comp[_gr.target(a)] == k) _out_arcs[n].push_back(a);
453          }
454        }
455      }
456    }
457
458    // Initialize path data for the current component
459    bool initComponent(int comp) {
460      _nodes = &(_comp_nodes[comp]);
461      int n = _nodes->size();
462      if (n < 1 || (n == 1 && _out_arcs[(*_nodes)[0]].size() == 0)) {
463        return false;
464      }     
465      for (int i = 0; i < n; ++i) {
466        _data[(*_nodes)[i]].resize(n + 1, PathData(INF));
467      }
468      return true;
469    }
470
471    // Process all rounds of computing path data for the current component.
472    // _data[v][k] is the length of a shortest directed walk from the root
473    // node to node v containing exactly k arcs.
474    void processRounds() {
475      Node start = (*_nodes)[0];
476      _data[start][0] = PathData(0);
477      _process.clear();
478      _process.push_back(start);
479
480      int k, n = _nodes->size();
481      for (k = 1; k <= n && int(_process.size()) < n; ++k) {
482        processNextBuildRound(k);
483      }
484      for ( ; k <= n; ++k) {
485        processNextFullRound(k);
486      }
487    }
488
489    // Process one round and rebuild _process
490    void processNextBuildRound(int k) {
491      std::vector<Node> next;
492      Node u, v;
493      Arc e;
494      LargeValue d;
495      for (int i = 0; i < int(_process.size()); ++i) {
496        u = _process[i];
497        for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
498          e = _out_arcs[u][j];
499          v = _gr.target(e);
500          d = _data[u][k-1].dist + _length[e];
501          if (_tolerance.less(d, _data[v][k].dist)) {
502            if (_data[v][k].dist == INF) next.push_back(v);
503            _data[v][k] = PathData(d, e);
504          }
505        }
506      }
507      _process.swap(next);
508    }
509
510    // Process one round using _nodes instead of _process
511    void processNextFullRound(int k) {
512      Node u, v;
513      Arc e;
514      LargeValue d;
515      for (int i = 0; i < int(_nodes->size()); ++i) {
516        u = (*_nodes)[i];
517        for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
518          e = _out_arcs[u][j];
519          v = _gr.target(e);
520          d = _data[u][k-1].dist + _length[e];
521          if (_tolerance.less(d, _data[v][k].dist)) {
522            _data[v][k] = PathData(d, e);
523          }
524        }
525      }
526    }
527
528    // Update the minimum cycle mean
529    void updateMinMean() {
530      int n = _nodes->size();
531      for (int i = 0; i < n; ++i) {
532        Node u = (*_nodes)[i];
533        if (_data[u][n].dist == INF) continue;
534        LargeValue length, max_length = 0;
535        int size, max_size = 1;
536        bool found_curr = false;
537        for (int k = 0; k < n; ++k) {
538          if (_data[u][k].dist == INF) continue;
539          length = _data[u][n].dist - _data[u][k].dist;
540          size = n - k;
541          if (!found_curr || length * max_size > max_length * size) {
542            found_curr = true;
543            max_length = length;
544            max_size = size;
545          }
546        }
547        if ( found_curr && (_cycle_node == INVALID ||
548             max_length * _cycle_size < _cycle_length * max_size) ) {
549          _cycle_length = max_length;
550          _cycle_size = max_size;
551          _cycle_node = u;
552        }
553      }
554    }
555
556  }; //class Karp
557
558  ///@}
559
560} //namespace lemon
561
562#endif //LEMON_KARP_H
Note: See TracBrowser for help on using the repository browser.