3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
22 /// \ingroup min_mean_cycle
25 /// \brief Karp's algorithm for finding a minimum mean cycle.
29 #include <lemon/core.h>
30 #include <lemon/path.h>
31 #include <lemon/tolerance.h>
32 #include <lemon/connectivity.h>
36 /// \brief Default traits class of Karp algorithm.
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.
43 template <typename GR, typename LEN>
45 template <typename GR, typename LEN,
46 bool integer = std::numeric_limits<typename LEN::Value>::is_integer>
48 struct KarpDefaultTraits
50 /// The type of the 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;
57 /// \brief The large value type used for internal computations
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;
65 /// The tolerance type used for internal computations
66 typedef lemon::Tolerance<LargeValue> Tolerance;
68 /// \brief The path type of the found cycles
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 addFront() function.
73 typedef lemon::Path<Digraph> Path;
76 // Default traits class for integer value types
77 template <typename GR, typename LEN>
78 struct KarpDefaultTraits<GR, LEN, true>
81 typedef LEN LengthMap;
82 typedef typename LengthMap::Value Value;
83 #ifdef LEMON_HAVE_LONG_LONG
84 typedef long long LargeValue;
86 typedef long LargeValue;
88 typedef lemon::Tolerance<LargeValue> Tolerance;
89 typedef lemon::Path<Digraph> Path;
93 /// \addtogroup min_mean_cycle
96 /// \brief Implementation of Karp's algorithm for finding a minimum
99 /// This class implements Karp's algorithm for finding a directed
100 /// cycle of minimum mean length (cost) in a digraph
101 /// \ref amo93networkflows, \ref dasdan98minmeancycle.
102 /// It runs in time O(ne) and uses space O(n<sup>2</sup>+e).
104 /// \tparam GR The type of the digraph the algorithm runs on.
105 /// \tparam LEN The type of the length map. The default
106 /// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
108 template <typename GR, typename LEN, typename TR>
110 template < typename GR,
111 typename LEN = typename GR::template ArcMap<int>,
112 typename TR = KarpDefaultTraits<GR, LEN> >
118 /// The type of the digraph
119 typedef typename TR::Digraph Digraph;
120 /// The type of the length map
121 typedef typename TR::LengthMap LengthMap;
122 /// The type of the arc lengths
123 typedef typename TR::Value Value;
125 /// \brief The large value type
127 /// The large value type used for internal computations.
128 /// Using the \ref KarpDefaultTraits "default traits class",
129 /// it is \c long \c long if the \c Value type is integer,
130 /// otherwise it is \c double.
131 typedef typename TR::LargeValue LargeValue;
133 /// The tolerance type
134 typedef typename TR::Tolerance Tolerance;
136 /// \brief The path type of the found cycles
138 /// The path type of the found cycles.
139 /// Using the \ref KarpDefaultTraits "default traits class",
140 /// it is \ref lemon::Path "Path<Digraph>".
141 typedef typename TR::Path Path;
143 /// The \ref KarpDefaultTraits "traits class" of the algorithm
148 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
150 // Data sturcture for path data
155 PathData(LargeValue d, Arc p = INVALID) :
159 typedef typename Digraph::template NodeMap<std::vector<PathData> >
164 // The digraph the algorithm runs on
166 // The length of the arcs
167 const LengthMap &_length;
169 // Data for storing the strongly connected components
171 typename Digraph::template NodeMap<int> _comp;
172 std::vector<std::vector<Node> > _comp_nodes;
173 std::vector<Node>* _nodes;
174 typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs;
176 // Data for the found cycle
177 LargeValue _cycle_length;
184 // Node map for storing path data
185 PathDataNodeMap _data;
186 // The processed nodes in the last round
187 std::vector<Node> _process;
189 Tolerance _tolerance;
192 const LargeValue INF;
196 /// \name Named Template Parameters
199 template <typename T>
200 struct SetLargeValueTraits : public Traits {
201 typedef T LargeValue;
202 typedef lemon::Tolerance<T> Tolerance;
205 /// \brief \ref named-templ-param "Named parameter" for setting
206 /// \c LargeValue type.
208 /// \ref named-templ-param "Named parameter" for setting \c LargeValue
209 /// type. It is used for internal computations in the algorithm.
210 template <typename T>
212 : public Karp<GR, LEN, SetLargeValueTraits<T> > {
213 typedef Karp<GR, LEN, SetLargeValueTraits<T> > Create;
216 template <typename T>
217 struct SetPathTraits : public Traits {
221 /// \brief \ref named-templ-param "Named parameter" for setting
224 /// \ref named-templ-param "Named parameter" for setting the \c %Path
225 /// type of the found cycles.
226 /// It must conform to the \ref lemon::concepts::Path "Path" concept
227 /// and it must have an \c addFront() function.
228 template <typename T>
230 : public Karp<GR, LEN, SetPathTraits<T> > {
231 typedef Karp<GR, LEN, SetPathTraits<T> > Create;
238 /// \brief Constructor.
240 /// The constructor of the class.
242 /// \param digraph The digraph the algorithm runs on.
243 /// \param length The lengths (costs) of the arcs.
244 Karp( const Digraph &digraph,
245 const LengthMap &length ) :
246 _gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph),
247 _cycle_length(0), _cycle_size(1), _cycle_node(INVALID),
248 _cycle_path(NULL), _local_path(false), _data(digraph),
249 INF(std::numeric_limits<LargeValue>::has_infinity ?
250 std::numeric_limits<LargeValue>::infinity() :
251 std::numeric_limits<LargeValue>::max())
256 if (_local_path) delete _cycle_path;
259 /// \brief Set the path structure for storing the found cycle.
261 /// This function sets an external path structure for storing the
264 /// If you don't call this function before calling \ref run() or
265 /// \ref findMinMean(), it will allocate a local \ref Path "path"
266 /// structure. The destuctor deallocates this automatically
267 /// allocated object, of course.
269 /// \note The algorithm calls only the \ref lemon::Path::addFront()
270 /// "addFront()" function of the given path structure.
272 /// \return <tt>(*this)</tt>
273 Karp& cycle(Path &path) {
282 /// \brief Set the tolerance used by the algorithm.
284 /// This function sets the tolerance object used by the algorithm.
286 /// \return <tt>(*this)</tt>
287 Karp& tolerance(const Tolerance& tolerance) {
288 _tolerance = tolerance;
292 /// \brief Return a const reference to the tolerance.
294 /// This function returns a const reference to the tolerance object
295 /// used by the algorithm.
296 const Tolerance& tolerance() const {
300 /// \name Execution control
301 /// The simplest way to execute the algorithm is to call the \ref run()
303 /// If you only need the minimum mean length, you may call
304 /// \ref findMinMean().
308 /// \brief Run the algorithm.
310 /// This function runs the algorithm.
311 /// It can be called more than once (e.g. if the underlying digraph
312 /// and/or the arc lengths have been modified).
314 /// \return \c true if a directed cycle exists in the digraph.
316 /// \note <tt>mmc.run()</tt> is just a shortcut of the following code.
318 /// return mmc.findMinMean() && mmc.findCycle();
321 return findMinMean() && findCycle();
324 /// \brief Find the minimum cycle mean.
326 /// This function finds the minimum mean length of the directed
327 /// cycles in the digraph.
329 /// \return \c true if a directed cycle exists in the digraph.
331 // Initialization and find strongly connected components
335 // Find the minimum cycle mean in the components
336 for (int comp = 0; comp < _comp_num; ++comp) {
337 if (!initComponent(comp)) continue;
341 return (_cycle_node != INVALID);
344 /// \brief Find a minimum mean directed cycle.
346 /// This function finds a directed cycle of minimum mean length
347 /// in the digraph using the data computed by findMinMean().
349 /// \return \c true if a directed cycle exists in the digraph.
351 /// \pre \ref findMinMean() must be called before using this function.
353 if (_cycle_node == INVALID) return false;
354 IntNodeMap reached(_gr, -1);
355 int r = _data[_cycle_node].size();
356 Node u = _cycle_node;
357 while (reached[u] < 0) {
359 u = _gr.source(_data[u][r].pred);
362 Arc e = _data[u][r].pred;
363 _cycle_path->addFront(e);
364 _cycle_length = _length[e];
367 while ((v = _gr.source(e)) != u) {
368 e = _data[v][--r].pred;
369 _cycle_path->addFront(e);
370 _cycle_length += _length[e];
378 /// \name Query Functions
379 /// The results of the algorithm can be obtained using these
381 /// The algorithm should be executed before using them.
385 /// \brief Return the total length of the found cycle.
387 /// This function returns the total length of the found cycle.
389 /// \pre \ref run() or \ref findMinMean() must be called before
390 /// using this function.
391 LargeValue cycleLength() const {
392 return _cycle_length;
395 /// \brief Return the number of arcs on the found cycle.
397 /// This function returns the number of arcs on the found cycle.
399 /// \pre \ref run() or \ref findMinMean() must be called before
400 /// using this function.
401 int cycleArcNum() const {
405 /// \brief Return the mean length of the found cycle.
407 /// This function returns the mean length of the found cycle.
409 /// \note <tt>alg.cycleMean()</tt> is just a shortcut of the
412 /// return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum();
415 /// \pre \ref run() or \ref findMinMean() must be called before
416 /// using this function.
417 double cycleMean() const {
418 return static_cast<double>(_cycle_length) / _cycle_size;
421 /// \brief Return the found cycle.
423 /// This function returns a const reference to the path structure
424 /// storing the found cycle.
426 /// \pre \ref run() or \ref findCycle() must be called before using
428 const Path& cycle() const {
440 _cycle_path = new Path;
442 _cycle_path->clear();
445 _cycle_node = INVALID;
446 for (NodeIt u(_gr); u != INVALID; ++u)
450 // Find strongly connected components and initialize _comp_nodes
452 void findComponents() {
453 _comp_num = stronglyConnectedComponents(_gr, _comp);
454 _comp_nodes.resize(_comp_num);
455 if (_comp_num == 1) {
456 _comp_nodes[0].clear();
457 for (NodeIt n(_gr); n != INVALID; ++n) {
458 _comp_nodes[0].push_back(n);
459 _out_arcs[n].clear();
460 for (OutArcIt a(_gr, n); a != INVALID; ++a) {
461 _out_arcs[n].push_back(a);
465 for (int i = 0; i < _comp_num; ++i)
466 _comp_nodes[i].clear();
467 for (NodeIt n(_gr); n != INVALID; ++n) {
469 _comp_nodes[k].push_back(n);
470 _out_arcs[n].clear();
471 for (OutArcIt a(_gr, n); a != INVALID; ++a) {
472 if (_comp[_gr.target(a)] == k) _out_arcs[n].push_back(a);
478 // Initialize path data for the current component
479 bool initComponent(int comp) {
480 _nodes = &(_comp_nodes[comp]);
481 int n = _nodes->size();
482 if (n < 1 || (n == 1 && _out_arcs[(*_nodes)[0]].size() == 0)) {
485 for (int i = 0; i < n; ++i) {
486 _data[(*_nodes)[i]].resize(n + 1, PathData(INF));
491 // Process all rounds of computing path data for the current component.
492 // _data[v][k] is the length of a shortest directed walk from the root
493 // node to node v containing exactly k arcs.
494 void processRounds() {
495 Node start = (*_nodes)[0];
496 _data[start][0] = PathData(0);
498 _process.push_back(start);
500 int k, n = _nodes->size();
501 for (k = 1; k <= n && int(_process.size()) < n; ++k) {
502 processNextBuildRound(k);
504 for ( ; k <= n; ++k) {
505 processNextFullRound(k);
509 // Process one round and rebuild _process
510 void processNextBuildRound(int k) {
511 std::vector<Node> next;
515 for (int i = 0; i < int(_process.size()); ++i) {
517 for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
520 d = _data[u][k-1].dist + _length[e];
521 if (_tolerance.less(d, _data[v][k].dist)) {
522 if (_data[v][k].dist == INF) next.push_back(v);
523 _data[v][k] = PathData(d, e);
530 // Process one round using _nodes instead of _process
531 void processNextFullRound(int k) {
535 for (int i = 0; i < int(_nodes->size()); ++i) {
537 for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
540 d = _data[u][k-1].dist + _length[e];
541 if (_tolerance.less(d, _data[v][k].dist)) {
542 _data[v][k] = PathData(d, e);
548 // Update the minimum cycle mean
549 void updateMinMean() {
550 int n = _nodes->size();
551 for (int i = 0; i < n; ++i) {
552 Node u = (*_nodes)[i];
553 if (_data[u][n].dist == INF) continue;
554 LargeValue length, max_length = 0;
555 int size, max_size = 1;
556 bool found_curr = false;
557 for (int k = 0; k < n; ++k) {
558 if (_data[u][k].dist == INF) continue;
559 length = _data[u][n].dist - _data[u][k].dist;
561 if (!found_curr || length * max_size > max_length * size) {
567 if ( found_curr && (_cycle_node == INVALID ||
568 max_length * _cycle_size < _cycle_length * max_size) ) {
569 _cycle_length = max_length;
570 _cycle_size = max_size;
582 #endif //LEMON_KARP_H