1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
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
19 #ifndef LEMON_KARP_MMC_H
20 #define LEMON_KARP_MMC_H
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 KarpMmc class.
38 /// Default traits class of KarpMmc class.
39 /// \tparam GR The type of the digraph.
40 /// \tparam CM The type of the cost map.
41 /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
43 template <typename GR, typename CM>
45 template <typename GR, typename CM,
46 bool integer = std::numeric_limits<typename CM::Value>::is_integer>
48 struct KarpMmcDefaultTraits
50 /// The type of the digraph
52 /// The type of the cost map
54 /// The type of the arc costs
55 typedef typename CostMap::Value Cost;
57 /// \brief The large cost type used for internal computations
59 /// The large cost type used for internal computations.
60 /// It is \c long \c long if the \c Cost type is integer,
61 /// otherwise it is \c double.
62 /// \c Cost must be convertible to \c LargeCost.
63 typedef double LargeCost;
65 /// The tolerance type used for internal computations
66 typedef lemon::Tolerance<LargeCost> 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 cost types
77 template <typename GR, typename CM>
78 struct KarpMmcDefaultTraits<GR, CM, true>
82 typedef typename CostMap::Value Cost;
83 #ifdef LEMON_HAVE_LONG_LONG
84 typedef long long LargeCost;
86 typedef long LargeCost;
88 typedef lemon::Tolerance<LargeCost> 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 cost in a digraph
101 /// \ref karp78characterization, \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 CM The type of the cost map. The default
106 /// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
107 /// \tparam TR The traits class that defines various types used by the
108 /// algorithm. By default, it is \ref KarpMmcDefaultTraits
109 /// "KarpMmcDefaultTraits<GR, CM>".
110 /// In most cases, this parameter should not be set directly,
111 /// consider to use the named template parameters instead.
113 template <typename GR, typename CM, typename TR>
115 template < typename GR,
116 typename CM = typename GR::template ArcMap<int>,
117 typename TR = KarpMmcDefaultTraits<GR, CM> >
123 /// The type of the digraph
124 typedef typename TR::Digraph Digraph;
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;
130 /// \brief The large cost type
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,
134 /// otherwise it is \c double.
135 typedef typename TR::LargeCost LargeCost;
137 /// The tolerance type
138 typedef typename TR::Tolerance Tolerance;
140 /// \brief The path type of the found cycles
142 /// The path type of the found cycles.
143 /// Using the \ref KarpMmcDefaultTraits "default traits class",
144 /// it is \ref lemon::Path "Path<Digraph>".
145 typedef typename TR::Path Path;
147 /// The \ref KarpMmcDefaultTraits "traits class" of the algorithm
152 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
154 // Data sturcture for path data
159 PathData(LargeCost d, Arc p = INVALID) :
163 typedef typename Digraph::template NodeMap<std::vector<PathData> >
168 // The digraph the algorithm runs on
170 // The cost of the arcs
171 const CostMap &_cost;
173 // Data for storing the strongly connected components
175 typename Digraph::template NodeMap<int> _comp;
176 std::vector<std::vector<Node> > _comp_nodes;
177 std::vector<Node>* _nodes;
178 typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs;
180 // Data for the found cycle
181 LargeCost _cycle_cost;
188 // Node map for storing path data
189 PathDataNodeMap _data;
190 // The processed nodes in the last round
191 std::vector<Node> _process;
193 Tolerance _tolerance;
200 /// \name Named Template Parameters
203 template <typename T>
204 struct SetLargeCostTraits : public Traits {
206 typedef lemon::Tolerance<T> Tolerance;
209 /// \brief \ref named-templ-param "Named parameter" for setting
210 /// \c LargeCost type.
212 /// \ref named-templ-param "Named parameter" for setting \c LargeCost
213 /// type. It is used for internal computations in the algorithm.
214 template <typename T>
216 : public KarpMmc<GR, CM, SetLargeCostTraits<T> > {
217 typedef KarpMmc<GR, CM, SetLargeCostTraits<T> > Create;
220 template <typename T>
221 struct SetPathTraits : public Traits {
225 /// \brief \ref named-templ-param "Named parameter" for setting
228 /// \ref named-templ-param "Named parameter" for setting the \c %Path
229 /// type of the found cycles.
230 /// It must conform to the \ref lemon::concepts::Path "Path" concept
231 /// and it must have an \c addFront() function.
232 template <typename T>
234 : public KarpMmc<GR, CM, SetPathTraits<T> > {
235 typedef KarpMmc<GR, CM, SetPathTraits<T> > Create;
246 /// \brief Constructor.
248 /// The constructor of the class.
250 /// \param digraph The digraph the algorithm runs on.
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),
256 _cycle_path(NULL), _local_path(false), _data(digraph),
257 INF(std::numeric_limits<LargeCost>::has_infinity ?
258 std::numeric_limits<LargeCost>::infinity() :
259 std::numeric_limits<LargeCost>::max())
264 if (_local_path) delete _cycle_path;
267 /// \brief Set the path structure for storing the found cycle.
269 /// This function sets an external path structure for storing the
272 /// If you don't call this function before calling \ref run() or
273 /// \ref findCycleMean(), it will allocate a local \ref Path "path"
274 /// structure. The destuctor deallocates this automatically
275 /// allocated object, of course.
277 /// \note The algorithm calls only the \ref lemon::Path::addFront()
278 /// "addFront()" function of the given path structure.
280 /// \return <tt>(*this)</tt>
281 KarpMmc& cycle(Path &path) {
290 /// \brief Set the tolerance used by the algorithm.
292 /// This function sets the tolerance object used by the algorithm.
294 /// \return <tt>(*this)</tt>
295 KarpMmc& tolerance(const Tolerance& tolerance) {
296 _tolerance = tolerance;
300 /// \brief Return a const reference to the tolerance.
302 /// This function returns a const reference to the tolerance object
303 /// used by the algorithm.
304 const Tolerance& tolerance() const {
308 /// \name Execution control
309 /// The simplest way to execute the algorithm is to call the \ref run()
311 /// If you only need the minimum mean cost, you may call
312 /// \ref findCycleMean().
316 /// \brief Run the algorithm.
318 /// This function runs the algorithm.
319 /// It can be called more than once (e.g. if the underlying digraph
320 /// and/or the arc costs have been modified).
322 /// \return \c true if a directed cycle exists in the digraph.
324 /// \note <tt>mmc.run()</tt> is just a shortcut of the following code.
326 /// return mmc.findCycleMean() && mmc.findCycle();
329 return findCycleMean() && findCycle();
332 /// \brief Find the minimum cycle mean.
334 /// This function finds the minimum mean cost of the directed
335 /// cycles in the digraph.
337 /// \return \c true if a directed cycle exists in the digraph.
338 bool findCycleMean() {
339 // Initialization and find strongly connected components
343 // Find the minimum cycle mean in the components
344 for (int comp = 0; comp < _comp_num; ++comp) {
345 if (!initComponent(comp)) continue;
349 return (_cycle_node != INVALID);
352 /// \brief Find a minimum mean directed cycle.
354 /// This function finds a directed cycle of minimum mean cost
355 /// in the digraph using the data computed by findCycleMean().
357 /// \return \c true if a directed cycle exists in the digraph.
359 /// \pre \ref findCycleMean() must be called before using this function.
361 if (_cycle_node == INVALID) return false;
362 IntNodeMap reached(_gr, -1);
363 int r = _data[_cycle_node].size();
364 Node u = _cycle_node;
365 while (reached[u] < 0) {
367 u = _gr.source(_data[u][r].pred);
370 Arc e = _data[u][r].pred;
371 _cycle_path->addFront(e);
372 _cycle_cost = _cost[e];
375 while ((v = _gr.source(e)) != u) {
376 e = _data[v][--r].pred;
377 _cycle_path->addFront(e);
378 _cycle_cost += _cost[e];
386 /// \name Query Functions
387 /// The results of the algorithm can be obtained using these
389 /// The algorithm should be executed before using them.
393 /// \brief Return the total cost of the found cycle.
395 /// This function returns the total cost of the found cycle.
397 /// \pre \ref run() or \ref findCycleMean() must be called before
398 /// using this function.
399 Cost cycleCost() const {
400 return static_cast<Cost>(_cycle_cost);
403 /// \brief Return the number of arcs on the found cycle.
405 /// This function returns the number of arcs on the found cycle.
407 /// \pre \ref run() or \ref findCycleMean() must be called before
408 /// using this function.
409 int cycleSize() const {
413 /// \brief Return the mean cost of the found cycle.
415 /// This function returns the mean cost of the found cycle.
417 /// \note <tt>alg.cycleMean()</tt> is just a shortcut of the
420 /// return static_cast<double>(alg.cycleCost()) / alg.cycleSize();
423 /// \pre \ref run() or \ref findCycleMean() must be called before
424 /// using this function.
425 double cycleMean() const {
426 return static_cast<double>(_cycle_cost) / _cycle_size;
429 /// \brief Return the found cycle.
431 /// This function returns a const reference to the path structure
432 /// storing the found cycle.
434 /// \pre \ref run() or \ref findCycle() must be called before using
436 const Path& cycle() const {
448 _cycle_path = new Path;
450 _cycle_path->clear();
453 _cycle_node = INVALID;
454 for (NodeIt u(_gr); u != INVALID; ++u)
458 // Find strongly connected components and initialize _comp_nodes
460 void findComponents() {
461 _comp_num = stronglyConnectedComponents(_gr, _comp);
462 _comp_nodes.resize(_comp_num);
463 if (_comp_num == 1) {
464 _comp_nodes[0].clear();
465 for (NodeIt n(_gr); n != INVALID; ++n) {
466 _comp_nodes[0].push_back(n);
467 _out_arcs[n].clear();
468 for (OutArcIt a(_gr, n); a != INVALID; ++a) {
469 _out_arcs[n].push_back(a);
473 for (int i = 0; i < _comp_num; ++i)
474 _comp_nodes[i].clear();
475 for (NodeIt n(_gr); n != INVALID; ++n) {
477 _comp_nodes[k].push_back(n);
478 _out_arcs[n].clear();
479 for (OutArcIt a(_gr, n); a != INVALID; ++a) {
480 if (_comp[_gr.target(a)] == k) _out_arcs[n].push_back(a);
486 // Initialize path data for the current component
487 bool initComponent(int comp) {
488 _nodes = &(_comp_nodes[comp]);
489 int n = _nodes->size();
490 if (n < 1 || (n == 1 && _out_arcs[(*_nodes)[0]].size() == 0)) {
493 for (int i = 0; i < n; ++i) {
494 _data[(*_nodes)[i]].resize(n + 1, PathData(INF));
499 // Process all rounds of computing path data for the current component.
500 // _data[v][k] is the cost of a shortest directed walk from the root
501 // node to node v containing exactly k arcs.
502 void processRounds() {
503 Node start = (*_nodes)[0];
504 _data[start][0] = PathData(0);
506 _process.push_back(start);
508 int k, n = _nodes->size();
509 for (k = 1; k <= n && int(_process.size()) < n; ++k) {
510 processNextBuildRound(k);
512 for ( ; k <= n; ++k) {
513 processNextFullRound(k);
517 // Process one round and rebuild _process
518 void processNextBuildRound(int k) {
519 std::vector<Node> next;
523 for (int i = 0; i < int(_process.size()); ++i) {
525 for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
528 d = _data[u][k-1].dist + _cost[e];
529 if (_tolerance.less(d, _data[v][k].dist)) {
530 if (_data[v][k].dist == INF) next.push_back(v);
531 _data[v][k] = PathData(d, e);
538 // Process one round using _nodes instead of _process
539 void processNextFullRound(int k) {
543 for (int i = 0; i < int(_nodes->size()); ++i) {
545 for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
548 d = _data[u][k-1].dist + _cost[e];
549 if (_tolerance.less(d, _data[v][k].dist)) {
550 _data[v][k] = PathData(d, e);
556 // Update the minimum cycle mean
557 void updateMinMean() {
558 int n = _nodes->size();
559 for (int i = 0; i < n; ++i) {
560 Node u = (*_nodes)[i];
561 if (_data[u][n].dist == INF) continue;
562 LargeCost cost, max_cost = 0;
563 int size, max_size = 1;
564 bool found_curr = false;
565 for (int k = 0; k < n; ++k) {
566 if (_data[u][k].dist == INF) continue;
567 cost = _data[u][n].dist - _data[u][k].dist;
569 if (!found_curr || cost * max_size > max_cost * size) {
575 if ( found_curr && (_cycle_node == INVALID ||
576 max_cost * _cycle_size < _cycle_cost * max_size) ) {
577 _cycle_cost = max_cost;
578 _cycle_size = max_size;
590 #endif //LEMON_KARP_MMC_H