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
19 #ifndef LEMON_MIN_MEAN_CYCLE_H
20 #define LEMON_MIN_MEAN_CYCLE_H
22 /// \ingroup shortest_path
25 /// \brief Howard's algorithm for finding a minimum mean cycle.
28 #include <lemon/core.h>
29 #include <lemon/path.h>
30 #include <lemon/tolerance.h>
31 #include <lemon/connectivity.h>
35 /// \brief Default traits class of MinMeanCycle class.
37 /// Default traits class of MinMeanCycle class.
38 /// \tparam GR The type of the digraph.
39 /// \tparam LEN The type of the length map.
40 /// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
42 template <typename GR, typename LEN>
44 template <typename GR, typename LEN,
45 bool integer = std::numeric_limits<typename LEN::Value>::is_integer>
47 struct MinMeanCycleDefaultTraits
49 /// The type of the digraph
51 /// The type of the length map
52 typedef LEN LengthMap;
53 /// The type of the arc lengths
54 typedef typename LengthMap::Value Value;
56 /// \brief The large value type used for internal computations
58 /// The large value type used for internal computations.
59 /// It is \c long \c long if the \c Value type is integer,
60 /// otherwise it is \c double.
61 /// \c Value must be convertible to \c LargeValue.
62 typedef double LargeValue;
64 /// The tolerance type used for internal computations
65 typedef lemon::Tolerance<LargeValue> Tolerance;
67 /// \brief The path type of the found cycles
69 /// The path type of the found cycles.
70 /// It must conform to the \ref lemon::concepts::Path "Path" concept
71 /// and it must have an \c addBack() function.
72 typedef lemon::Path<Digraph> Path;
75 // Default traits class for integer value types
76 template <typename GR, typename LEN>
77 struct MinMeanCycleDefaultTraits<GR, LEN, true>
80 typedef LEN LengthMap;
81 typedef typename LengthMap::Value Value;
82 #ifdef LEMON_HAVE_LONG_LONG
83 typedef long long LargeValue;
85 typedef long LargeValue;
87 typedef lemon::Tolerance<LargeValue> Tolerance;
88 typedef lemon::Path<Digraph> Path;
92 /// \addtogroup shortest_path
95 /// \brief Implementation of Howard's algorithm for finding a minimum
98 /// \ref MinMeanCycle implements Howard's algorithm for finding a
99 /// directed cycle of minimum mean length (cost) in a digraph.
101 /// \tparam GR The type of the digraph the algorithm runs on.
102 /// \tparam LEN The type of the length map. The default
103 /// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
105 template <typename GR, typename LEN, typename TR>
107 template < typename GR,
108 typename LEN = typename GR::template ArcMap<int>,
109 typename TR = MinMeanCycleDefaultTraits<GR, LEN> >
115 /// The type of the digraph
116 typedef typename TR::Digraph Digraph;
117 /// The type of the length map
118 typedef typename TR::LengthMap LengthMap;
119 /// The type of the arc lengths
120 typedef typename TR::Value Value;
122 /// \brief The large value type
124 /// The large value type used for internal computations.
125 /// Using the \ref MinMeanCycleDefaultTraits "default traits class",
126 /// it is \c long \c long if the \c Value type is integer,
127 /// otherwise it is \c double.
128 typedef typename TR::LargeValue LargeValue;
130 /// The tolerance type
131 typedef typename TR::Tolerance Tolerance;
133 /// \brief The path type of the found cycles
135 /// The path type of the found cycles.
136 /// Using the \ref MinMeanCycleDefaultTraits "default traits class",
137 /// it is \ref lemon::Path "Path<Digraph>".
138 typedef typename TR::Path Path;
140 /// The \ref MinMeanCycleDefaultTraits "traits class" of the algorithm
145 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
147 // The digraph the algorithm runs on
149 // The length of the arcs
150 const LengthMap &_length;
152 // Data for the found cycles
153 bool _curr_found, _best_found;
154 LargeValue _curr_length, _best_length;
155 int _curr_size, _best_size;
156 Node _curr_node, _best_node;
161 // Internal data used by the algorithm
162 typename Digraph::template NodeMap<Arc> _policy;
163 typename Digraph::template NodeMap<bool> _reached;
164 typename Digraph::template NodeMap<int> _level;
165 typename Digraph::template NodeMap<LargeValue> _dist;
167 // Data for storing the strongly connected components
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> > _in_arcs;
174 // Queue used for BFS search
175 std::vector<Node> _queue;
178 Tolerance _tolerance;
182 /// \name Named Template Parameters
185 template <typename T>
186 struct SetLargeValueTraits : public Traits {
187 typedef T LargeValue;
188 typedef lemon::Tolerance<T> Tolerance;
191 /// \brief \ref named-templ-param "Named parameter" for setting
192 /// \c LargeValue type.
194 /// \ref named-templ-param "Named parameter" for setting \c LargeValue
195 /// type. It is used for internal computations in the algorithm.
196 template <typename T>
198 : public MinMeanCycle<GR, LEN, SetLargeValueTraits<T> > {
199 typedef MinMeanCycle<GR, LEN, SetLargeValueTraits<T> > Create;
202 template <typename T>
203 struct SetPathTraits : public Traits {
207 /// \brief \ref named-templ-param "Named parameter" for setting
210 /// \ref named-templ-param "Named parameter" for setting the \c %Path
211 /// type of the found cycles.
212 /// It must conform to the \ref lemon::concepts::Path "Path" concept
213 /// and it must have an \c addBack() function.
214 template <typename T>
216 : public MinMeanCycle<GR, LEN, SetPathTraits<T> > {
217 typedef MinMeanCycle<GR, LEN, SetPathTraits<T> > Create;
224 /// \brief Constructor.
226 /// The constructor of the class.
228 /// \param digraph The digraph the algorithm runs on.
229 /// \param length The lengths (costs) of the arcs.
230 MinMeanCycle( const Digraph &digraph,
231 const LengthMap &length ) :
232 _gr(digraph), _length(length), _cycle_path(NULL), _local_path(false),
233 _policy(digraph), _reached(digraph), _level(digraph), _dist(digraph),
234 _comp(digraph), _in_arcs(digraph)
239 if (_local_path) delete _cycle_path;
242 /// \brief Set the path structure for storing the found cycle.
244 /// This function sets an external path structure for storing the
247 /// If you don't call this function before calling \ref run() or
248 /// \ref findMinMean(), it will allocate a local \ref Path "path"
249 /// structure. The destuctor deallocates this automatically
250 /// allocated object, of course.
252 /// \note The algorithm calls only the \ref lemon::Path::addBack()
253 /// "addBack()" function of the given path structure.
255 /// \return <tt>(*this)</tt>
256 MinMeanCycle& cycle(Path &path) {
265 /// \name Execution control
266 /// The simplest way to execute the algorithm is to call the \ref run()
268 /// If you only need the minimum mean length, you may call
269 /// \ref findMinMean().
273 /// \brief Run the algorithm.
275 /// This function runs the algorithm.
276 /// It can be called more than once (e.g. if the underlying digraph
277 /// and/or the arc lengths have been modified).
279 /// \return \c true if a directed cycle exists in the digraph.
281 /// \note <tt>mmc.run()</tt> is just a shortcut of the following code.
283 /// return mmc.findMinMean() && mmc.findCycle();
286 return findMinMean() && findCycle();
289 /// \brief Find the minimum cycle mean.
291 /// This function finds the minimum mean length of the directed
292 /// cycles in the digraph.
294 /// \return \c true if a directed cycle exists in the digraph.
296 // Initialize and find strongly connected components
300 // Find the minimum cycle mean in the components
301 for (int comp = 0; comp < _comp_num; ++comp) {
302 // Find the minimum mean cycle in the current component
303 if (!buildPolicyGraph(comp)) continue;
306 if (!computeNodeDistances()) break;
308 // Update the best cycle (global minimum mean cycle)
309 if ( !_best_found || (_curr_found &&
310 _curr_length * _best_size < _best_length * _curr_size) ) {
312 _best_length = _curr_length;
313 _best_size = _curr_size;
314 _best_node = _curr_node;
320 /// \brief Find a minimum mean directed cycle.
322 /// This function finds a directed cycle of minimum mean length
323 /// in the digraph using the data computed by findMinMean().
325 /// \return \c true if a directed cycle exists in the digraph.
327 /// \pre \ref findMinMean() must be called before using this function.
329 if (!_best_found) return false;
330 _cycle_path->addBack(_policy[_best_node]);
331 for ( Node v = _best_node;
332 (v = _gr.target(_policy[v])) != _best_node; ) {
333 _cycle_path->addBack(_policy[v]);
340 /// \name Query Functions
341 /// The results of the algorithm can be obtained using these
343 /// The algorithm should be executed before using them.
347 /// \brief Return the total length of the found cycle.
349 /// This function returns the total length of the found cycle.
351 /// \pre \ref run() or \ref findMinMean() must be called before
352 /// using this function.
353 LargeValue cycleLength() const {
357 /// \brief Return the number of arcs on the found cycle.
359 /// This function returns the number of arcs on the found cycle.
361 /// \pre \ref run() or \ref findMinMean() must be called before
362 /// using this function.
363 int cycleArcNum() const {
367 /// \brief Return the mean length of the found cycle.
369 /// This function returns the mean length of the found cycle.
371 /// \note <tt>alg.cycleMean()</tt> is just a shortcut of the
374 /// return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum();
377 /// \pre \ref run() or \ref findMinMean() must be called before
378 /// using this function.
379 double cycleMean() const {
380 return static_cast<double>(_best_length) / _best_size;
383 /// \brief Return the found cycle.
385 /// This function returns a const reference to the path structure
386 /// storing the found cycle.
388 /// \pre \ref run() or \ref findCycle() must be called before using
390 const Path& cycle() const {
402 _cycle_path = new Path;
404 _queue.resize(countNodes(_gr));
408 _cycle_path->clear();
411 // Find strongly connected components and initialize _comp_nodes
413 void findComponents() {
414 _comp_num = stronglyConnectedComponents(_gr, _comp);
415 _comp_nodes.resize(_comp_num);
416 if (_comp_num == 1) {
417 _comp_nodes[0].clear();
418 for (NodeIt n(_gr); n != INVALID; ++n) {
419 _comp_nodes[0].push_back(n);
421 for (InArcIt a(_gr, n); a != INVALID; ++a) {
422 _in_arcs[n].push_back(a);
426 for (int i = 0; i < _comp_num; ++i)
427 _comp_nodes[i].clear();
428 for (NodeIt n(_gr); n != INVALID; ++n) {
430 _comp_nodes[k].push_back(n);
432 for (InArcIt a(_gr, n); a != INVALID; ++a) {
433 if (_comp[_gr.source(a)] == k) _in_arcs[n].push_back(a);
439 // Build the policy graph in the given strongly connected component
440 // (the out-degree of every node is 1)
441 bool buildPolicyGraph(int comp) {
442 _nodes = &(_comp_nodes[comp]);
443 if (_nodes->size() < 1 ||
444 (_nodes->size() == 1 && _in_arcs[(*_nodes)[0]].size() == 0)) {
447 for (int i = 0; i < int(_nodes->size()); ++i) {
448 _dist[(*_nodes)[i]] = std::numeric_limits<LargeValue>::max();
452 for (int i = 0; i < int(_nodes->size()); ++i) {
454 for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
457 if (_length[e] < _dist[u]) {
458 _dist[u] = _length[e];
466 // Find the minimum mean cycle in the policy graph
467 void findPolicyCycle() {
468 for (int i = 0; i < int(_nodes->size()); ++i) {
469 _level[(*_nodes)[i]] = -1;
475 for (int i = 0; i < int(_nodes->size()); ++i) {
477 if (_level[u] >= 0) continue;
478 for (; _level[u] < 0; u = _gr.target(_policy[u])) {
481 if (_level[u] == i) {
483 clength = _length[_policy[u]];
485 for (v = u; (v = _gr.target(_policy[v])) != u; ) {
486 clength += _length[_policy[v]];
490 (clength * _curr_size < _curr_length * csize) ) {
492 _curr_length = clength;
500 // Contract the policy graph and compute node distances
501 bool computeNodeDistances() {
502 // Find the component of the main cycle and compute node distances
504 for (int i = 0; i < int(_nodes->size()); ++i) {
505 _reached[(*_nodes)[i]] = false;
507 _qfront = _qback = 0;
508 _queue[0] = _curr_node;
509 _reached[_curr_node] = true;
510 _dist[_curr_node] = 0;
513 while (_qfront <= _qback) {
514 v = _queue[_qfront++];
515 for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
518 if (_policy[u] == e && !_reached[u]) {
520 _dist[u] = _dist[v] + _length[e] * _curr_size - _curr_length;
521 _queue[++_qback] = u;
526 // Connect all other nodes to this component and compute node
527 // distances using reverse BFS
529 while (_qback < int(_nodes->size())-1) {
530 v = _queue[_qfront++];
531 for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
537 _dist[u] = _dist[v] + _length[e] * _curr_size - _curr_length;
538 _queue[++_qback] = u;
543 // Improve node distances
544 bool improved = false;
545 for (int i = 0; i < int(_nodes->size()); ++i) {
547 for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
550 LargeValue delta = _dist[v] + _length[e] * _curr_size - _curr_length;
551 if (_tolerance.less(delta, _dist[u])) {
561 }; //class MinMeanCycle
567 #endif //LEMON_MIN_MEAN_CYCLE_H