kpeter@758
|
1 |
/* -*- C++ -*-
|
kpeter@758
|
2 |
*
|
kpeter@758
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
kpeter@758
|
4 |
*
|
kpeter@758
|
5 |
* Copyright (C) 2003-2008
|
kpeter@758
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
kpeter@758
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
kpeter@758
|
8 |
*
|
kpeter@758
|
9 |
* Permission to use, modify and distribute this software is granted
|
kpeter@758
|
10 |
* provided that this copyright notice appears in all copies. For
|
kpeter@758
|
11 |
* precise terms see the accompanying LICENSE file.
|
kpeter@758
|
12 |
*
|
kpeter@758
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
kpeter@758
|
14 |
* express or implied, and with no claim as to its suitability for any
|
kpeter@758
|
15 |
* purpose.
|
kpeter@758
|
16 |
*
|
kpeter@758
|
17 |
*/
|
kpeter@758
|
18 |
|
kpeter@758
|
19 |
#ifndef LEMON_MIN_MEAN_CYCLE_H
|
kpeter@758
|
20 |
#define LEMON_MIN_MEAN_CYCLE_H
|
kpeter@758
|
21 |
|
kpeter@758
|
22 |
/// \ingroup shortest_path
|
kpeter@758
|
23 |
///
|
kpeter@758
|
24 |
/// \file
|
kpeter@758
|
25 |
/// \brief Howard's algorithm for finding a minimum mean cycle.
|
kpeter@758
|
26 |
|
kpeter@758
|
27 |
#include <vector>
|
kpeter@763
|
28 |
#include <limits>
|
kpeter@758
|
29 |
#include <lemon/core.h>
|
kpeter@758
|
30 |
#include <lemon/path.h>
|
kpeter@758
|
31 |
#include <lemon/tolerance.h>
|
kpeter@758
|
32 |
#include <lemon/connectivity.h>
|
kpeter@758
|
33 |
|
kpeter@758
|
34 |
namespace lemon {
|
kpeter@758
|
35 |
|
kpeter@761
|
36 |
/// \brief Default traits class of MinMeanCycle class.
|
kpeter@761
|
37 |
///
|
kpeter@761
|
38 |
/// Default traits class of MinMeanCycle class.
|
kpeter@761
|
39 |
/// \tparam GR The type of the digraph.
|
kpeter@761
|
40 |
/// \tparam LEN The type of the length map.
|
kpeter@761
|
41 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
|
kpeter@761
|
42 |
#ifdef DOXYGEN
|
kpeter@761
|
43 |
template <typename GR, typename LEN>
|
kpeter@761
|
44 |
#else
|
kpeter@761
|
45 |
template <typename GR, typename LEN,
|
kpeter@761
|
46 |
bool integer = std::numeric_limits<typename LEN::Value>::is_integer>
|
kpeter@761
|
47 |
#endif
|
kpeter@761
|
48 |
struct MinMeanCycleDefaultTraits
|
kpeter@761
|
49 |
{
|
kpeter@761
|
50 |
/// The type of the digraph
|
kpeter@761
|
51 |
typedef GR Digraph;
|
kpeter@761
|
52 |
/// The type of the length map
|
kpeter@761
|
53 |
typedef LEN LengthMap;
|
kpeter@761
|
54 |
/// The type of the arc lengths
|
kpeter@761
|
55 |
typedef typename LengthMap::Value Value;
|
kpeter@761
|
56 |
|
kpeter@761
|
57 |
/// \brief The large value type used for internal computations
|
kpeter@761
|
58 |
///
|
kpeter@761
|
59 |
/// The large value type used for internal computations.
|
kpeter@761
|
60 |
/// It is \c long \c long if the \c Value type is integer,
|
kpeter@761
|
61 |
/// otherwise it is \c double.
|
kpeter@761
|
62 |
/// \c Value must be convertible to \c LargeValue.
|
kpeter@761
|
63 |
typedef double LargeValue;
|
kpeter@761
|
64 |
|
kpeter@761
|
65 |
/// The tolerance type used for internal computations
|
kpeter@761
|
66 |
typedef lemon::Tolerance<LargeValue> Tolerance;
|
kpeter@761
|
67 |
|
kpeter@761
|
68 |
/// \brief The path type of the found cycles
|
kpeter@761
|
69 |
///
|
kpeter@761
|
70 |
/// The path type of the found cycles.
|
kpeter@761
|
71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept
|
kpeter@761
|
72 |
/// and it must have an \c addBack() function.
|
kpeter@761
|
73 |
typedef lemon::Path<Digraph> Path;
|
kpeter@761
|
74 |
};
|
kpeter@761
|
75 |
|
kpeter@761
|
76 |
// Default traits class for integer value types
|
kpeter@761
|
77 |
template <typename GR, typename LEN>
|
kpeter@761
|
78 |
struct MinMeanCycleDefaultTraits<GR, LEN, true>
|
kpeter@761
|
79 |
{
|
kpeter@761
|
80 |
typedef GR Digraph;
|
kpeter@761
|
81 |
typedef LEN LengthMap;
|
kpeter@761
|
82 |
typedef typename LengthMap::Value Value;
|
kpeter@761
|
83 |
#ifdef LEMON_HAVE_LONG_LONG
|
kpeter@761
|
84 |
typedef long long LargeValue;
|
kpeter@761
|
85 |
#else
|
kpeter@761
|
86 |
typedef long LargeValue;
|
kpeter@761
|
87 |
#endif
|
kpeter@761
|
88 |
typedef lemon::Tolerance<LargeValue> Tolerance;
|
kpeter@761
|
89 |
typedef lemon::Path<Digraph> Path;
|
kpeter@761
|
90 |
};
|
kpeter@761
|
91 |
|
kpeter@761
|
92 |
|
kpeter@758
|
93 |
/// \addtogroup shortest_path
|
kpeter@758
|
94 |
/// @{
|
kpeter@758
|
95 |
|
kpeter@758
|
96 |
/// \brief Implementation of Howard's algorithm for finding a minimum
|
kpeter@758
|
97 |
/// mean cycle.
|
kpeter@758
|
98 |
///
|
kpeter@758
|
99 |
/// \ref MinMeanCycle implements Howard's algorithm for finding a
|
kpeter@758
|
100 |
/// directed cycle of minimum mean length (cost) in a digraph.
|
kpeter@758
|
101 |
///
|
kpeter@758
|
102 |
/// \tparam GR The type of the digraph the algorithm runs on.
|
kpeter@758
|
103 |
/// \tparam LEN The type of the length map. The default
|
kpeter@758
|
104 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
|
kpeter@758
|
105 |
#ifdef DOXYGEN
|
kpeter@761
|
106 |
template <typename GR, typename LEN, typename TR>
|
kpeter@758
|
107 |
#else
|
kpeter@758
|
108 |
template < typename GR,
|
kpeter@761
|
109 |
typename LEN = typename GR::template ArcMap<int>,
|
kpeter@761
|
110 |
typename TR = MinMeanCycleDefaultTraits<GR, LEN> >
|
kpeter@758
|
111 |
#endif
|
kpeter@758
|
112 |
class MinMeanCycle
|
kpeter@758
|
113 |
{
|
kpeter@758
|
114 |
public:
|
kpeter@758
|
115 |
|
kpeter@761
|
116 |
/// The type of the digraph
|
kpeter@761
|
117 |
typedef typename TR::Digraph Digraph;
|
kpeter@758
|
118 |
/// The type of the length map
|
kpeter@761
|
119 |
typedef typename TR::LengthMap LengthMap;
|
kpeter@758
|
120 |
/// The type of the arc lengths
|
kpeter@761
|
121 |
typedef typename TR::Value Value;
|
kpeter@761
|
122 |
|
kpeter@761
|
123 |
/// \brief The large value type
|
kpeter@761
|
124 |
///
|
kpeter@761
|
125 |
/// The large value type used for internal computations.
|
kpeter@761
|
126 |
/// Using the \ref MinMeanCycleDefaultTraits "default traits class",
|
kpeter@761
|
127 |
/// it is \c long \c long if the \c Value type is integer,
|
kpeter@761
|
128 |
/// otherwise it is \c double.
|
kpeter@761
|
129 |
typedef typename TR::LargeValue LargeValue;
|
kpeter@761
|
130 |
|
kpeter@761
|
131 |
/// The tolerance type
|
kpeter@761
|
132 |
typedef typename TR::Tolerance Tolerance;
|
kpeter@761
|
133 |
|
kpeter@761
|
134 |
/// \brief The path type of the found cycles
|
kpeter@761
|
135 |
///
|
kpeter@761
|
136 |
/// The path type of the found cycles.
|
kpeter@761
|
137 |
/// Using the \ref MinMeanCycleDefaultTraits "default traits class",
|
kpeter@761
|
138 |
/// it is \ref lemon::Path "Path<Digraph>".
|
kpeter@761
|
139 |
typedef typename TR::Path Path;
|
kpeter@761
|
140 |
|
kpeter@761
|
141 |
/// The \ref MinMeanCycleDefaultTraits "traits class" of the algorithm
|
kpeter@761
|
142 |
typedef TR Traits;
|
kpeter@758
|
143 |
|
kpeter@758
|
144 |
private:
|
kpeter@758
|
145 |
|
kpeter@758
|
146 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
|
kpeter@758
|
147 |
|
kpeter@758
|
148 |
// The digraph the algorithm runs on
|
kpeter@758
|
149 |
const Digraph &_gr;
|
kpeter@758
|
150 |
// The length of the arcs
|
kpeter@758
|
151 |
const LengthMap &_length;
|
kpeter@758
|
152 |
|
kpeter@760
|
153 |
// Data for the found cycles
|
kpeter@760
|
154 |
bool _curr_found, _best_found;
|
kpeter@761
|
155 |
LargeValue _curr_length, _best_length;
|
kpeter@760
|
156 |
int _curr_size, _best_size;
|
kpeter@760
|
157 |
Node _curr_node, _best_node;
|
kpeter@760
|
158 |
|
kpeter@758
|
159 |
Path *_cycle_path;
|
kpeter@760
|
160 |
bool _local_path;
|
kpeter@758
|
161 |
|
kpeter@760
|
162 |
// Internal data used by the algorithm
|
kpeter@760
|
163 |
typename Digraph::template NodeMap<Arc> _policy;
|
kpeter@760
|
164 |
typename Digraph::template NodeMap<bool> _reached;
|
kpeter@760
|
165 |
typename Digraph::template NodeMap<int> _level;
|
kpeter@761
|
166 |
typename Digraph::template NodeMap<LargeValue> _dist;
|
kpeter@758
|
167 |
|
kpeter@760
|
168 |
// Data for storing the strongly connected components
|
kpeter@760
|
169 |
int _comp_num;
|
kpeter@758
|
170 |
typename Digraph::template NodeMap<int> _comp;
|
kpeter@760
|
171 |
std::vector<std::vector<Node> > _comp_nodes;
|
kpeter@760
|
172 |
std::vector<Node>* _nodes;
|
kpeter@760
|
173 |
typename Digraph::template NodeMap<std::vector<Arc> > _in_arcs;
|
kpeter@760
|
174 |
|
kpeter@760
|
175 |
// Queue used for BFS search
|
kpeter@760
|
176 |
std::vector<Node> _queue;
|
kpeter@760
|
177 |
int _qfront, _qback;
|
kpeter@761
|
178 |
|
kpeter@761
|
179 |
Tolerance _tolerance;
|
kpeter@761
|
180 |
|
kpeter@761
|
181 |
public:
|
kpeter@761
|
182 |
|
kpeter@761
|
183 |
/// \name Named Template Parameters
|
kpeter@761
|
184 |
/// @{
|
kpeter@761
|
185 |
|
kpeter@761
|
186 |
template <typename T>
|
kpeter@761
|
187 |
struct SetLargeValueTraits : public Traits {
|
kpeter@761
|
188 |
typedef T LargeValue;
|
kpeter@761
|
189 |
typedef lemon::Tolerance<T> Tolerance;
|
kpeter@761
|
190 |
};
|
kpeter@761
|
191 |
|
kpeter@761
|
192 |
/// \brief \ref named-templ-param "Named parameter" for setting
|
kpeter@761
|
193 |
/// \c LargeValue type.
|
kpeter@761
|
194 |
///
|
kpeter@761
|
195 |
/// \ref named-templ-param "Named parameter" for setting \c LargeValue
|
kpeter@761
|
196 |
/// type. It is used for internal computations in the algorithm.
|
kpeter@761
|
197 |
template <typename T>
|
kpeter@761
|
198 |
struct SetLargeValue
|
kpeter@761
|
199 |
: public MinMeanCycle<GR, LEN, SetLargeValueTraits<T> > {
|
kpeter@761
|
200 |
typedef MinMeanCycle<GR, LEN, SetLargeValueTraits<T> > Create;
|
kpeter@761
|
201 |
};
|
kpeter@761
|
202 |
|
kpeter@761
|
203 |
template <typename T>
|
kpeter@761
|
204 |
struct SetPathTraits : public Traits {
|
kpeter@761
|
205 |
typedef T Path;
|
kpeter@761
|
206 |
};
|
kpeter@761
|
207 |
|
kpeter@761
|
208 |
/// \brief \ref named-templ-param "Named parameter" for setting
|
kpeter@761
|
209 |
/// \c %Path type.
|
kpeter@761
|
210 |
///
|
kpeter@761
|
211 |
/// \ref named-templ-param "Named parameter" for setting the \c %Path
|
kpeter@761
|
212 |
/// type of the found cycles.
|
kpeter@761
|
213 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept
|
kpeter@761
|
214 |
/// and it must have an \c addBack() function.
|
kpeter@761
|
215 |
template <typename T>
|
kpeter@761
|
216 |
struct SetPath
|
kpeter@761
|
217 |
: public MinMeanCycle<GR, LEN, SetPathTraits<T> > {
|
kpeter@761
|
218 |
typedef MinMeanCycle<GR, LEN, SetPathTraits<T> > Create;
|
kpeter@761
|
219 |
};
|
kpeter@760
|
220 |
|
kpeter@761
|
221 |
/// @}
|
kpeter@758
|
222 |
|
kpeter@758
|
223 |
public:
|
kpeter@758
|
224 |
|
kpeter@758
|
225 |
/// \brief Constructor.
|
kpeter@758
|
226 |
///
|
kpeter@758
|
227 |
/// The constructor of the class.
|
kpeter@758
|
228 |
///
|
kpeter@758
|
229 |
/// \param digraph The digraph the algorithm runs on.
|
kpeter@758
|
230 |
/// \param length The lengths (costs) of the arcs.
|
kpeter@758
|
231 |
MinMeanCycle( const Digraph &digraph,
|
kpeter@758
|
232 |
const LengthMap &length ) :
|
kpeter@760
|
233 |
_gr(digraph), _length(length), _cycle_path(NULL), _local_path(false),
|
kpeter@760
|
234 |
_policy(digraph), _reached(digraph), _level(digraph), _dist(digraph),
|
kpeter@760
|
235 |
_comp(digraph), _in_arcs(digraph)
|
kpeter@758
|
236 |
{}
|
kpeter@758
|
237 |
|
kpeter@758
|
238 |
/// Destructor.
|
kpeter@758
|
239 |
~MinMeanCycle() {
|
kpeter@758
|
240 |
if (_local_path) delete _cycle_path;
|
kpeter@758
|
241 |
}
|
kpeter@758
|
242 |
|
kpeter@758
|
243 |
/// \brief Set the path structure for storing the found cycle.
|
kpeter@758
|
244 |
///
|
kpeter@758
|
245 |
/// This function sets an external path structure for storing the
|
kpeter@758
|
246 |
/// found cycle.
|
kpeter@758
|
247 |
///
|
kpeter@758
|
248 |
/// If you don't call this function before calling \ref run() or
|
kpeter@759
|
249 |
/// \ref findMinMean(), it will allocate a local \ref Path "path"
|
kpeter@758
|
250 |
/// structure. The destuctor deallocates this automatically
|
kpeter@758
|
251 |
/// allocated object, of course.
|
kpeter@758
|
252 |
///
|
kpeter@758
|
253 |
/// \note The algorithm calls only the \ref lemon::Path::addBack()
|
kpeter@758
|
254 |
/// "addBack()" function of the given path structure.
|
kpeter@758
|
255 |
///
|
kpeter@758
|
256 |
/// \return <tt>(*this)</tt>
|
kpeter@762
|
257 |
MinMeanCycle& cycle(Path &path) {
|
kpeter@758
|
258 |
if (_local_path) {
|
kpeter@758
|
259 |
delete _cycle_path;
|
kpeter@758
|
260 |
_local_path = false;
|
kpeter@758
|
261 |
}
|
kpeter@758
|
262 |
_cycle_path = &path;
|
kpeter@758
|
263 |
return *this;
|
kpeter@758
|
264 |
}
|
kpeter@758
|
265 |
|
kpeter@758
|
266 |
/// \name Execution control
|
kpeter@758
|
267 |
/// The simplest way to execute the algorithm is to call the \ref run()
|
kpeter@758
|
268 |
/// function.\n
|
kpeter@759
|
269 |
/// If you only need the minimum mean length, you may call
|
kpeter@759
|
270 |
/// \ref findMinMean().
|
kpeter@758
|
271 |
|
kpeter@758
|
272 |
/// @{
|
kpeter@758
|
273 |
|
kpeter@758
|
274 |
/// \brief Run the algorithm.
|
kpeter@758
|
275 |
///
|
kpeter@758
|
276 |
/// This function runs the algorithm.
|
kpeter@759
|
277 |
/// It can be called more than once (e.g. if the underlying digraph
|
kpeter@759
|
278 |
/// and/or the arc lengths have been modified).
|
kpeter@758
|
279 |
///
|
kpeter@758
|
280 |
/// \return \c true if a directed cycle exists in the digraph.
|
kpeter@758
|
281 |
///
|
kpeter@759
|
282 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code.
|
kpeter@758
|
283 |
/// \code
|
kpeter@759
|
284 |
/// return mmc.findMinMean() && mmc.findCycle();
|
kpeter@758
|
285 |
/// \endcode
|
kpeter@758
|
286 |
bool run() {
|
kpeter@758
|
287 |
return findMinMean() && findCycle();
|
kpeter@758
|
288 |
}
|
kpeter@758
|
289 |
|
kpeter@759
|
290 |
/// \brief Find the minimum cycle mean.
|
kpeter@758
|
291 |
///
|
kpeter@759
|
292 |
/// This function finds the minimum mean length of the directed
|
kpeter@759
|
293 |
/// cycles in the digraph.
|
kpeter@758
|
294 |
///
|
kpeter@759
|
295 |
/// \return \c true if a directed cycle exists in the digraph.
|
kpeter@759
|
296 |
bool findMinMean() {
|
kpeter@760
|
297 |
// Initialize and find strongly connected components
|
kpeter@760
|
298 |
init();
|
kpeter@760
|
299 |
findComponents();
|
kpeter@760
|
300 |
|
kpeter@759
|
301 |
// Find the minimum cycle mean in the components
|
kpeter@758
|
302 |
for (int comp = 0; comp < _comp_num; ++comp) {
|
kpeter@760
|
303 |
// Find the minimum mean cycle in the current component
|
kpeter@760
|
304 |
if (!buildPolicyGraph(comp)) continue;
|
kpeter@758
|
305 |
while (true) {
|
kpeter@760
|
306 |
findPolicyCycle();
|
kpeter@758
|
307 |
if (!computeNodeDistances()) break;
|
kpeter@758
|
308 |
}
|
kpeter@760
|
309 |
// Update the best cycle (global minimum mean cycle)
|
kpeter@760
|
310 |
if ( !_best_found || (_curr_found &&
|
kpeter@760
|
311 |
_curr_length * _best_size < _best_length * _curr_size) ) {
|
kpeter@760
|
312 |
_best_found = true;
|
kpeter@760
|
313 |
_best_length = _curr_length;
|
kpeter@760
|
314 |
_best_size = _curr_size;
|
kpeter@760
|
315 |
_best_node = _curr_node;
|
kpeter@760
|
316 |
}
|
kpeter@758
|
317 |
}
|
kpeter@760
|
318 |
return _best_found;
|
kpeter@758
|
319 |
}
|
kpeter@758
|
320 |
|
kpeter@758
|
321 |
/// \brief Find a minimum mean directed cycle.
|
kpeter@758
|
322 |
///
|
kpeter@758
|
323 |
/// This function finds a directed cycle of minimum mean length
|
kpeter@758
|
324 |
/// in the digraph using the data computed by findMinMean().
|
kpeter@758
|
325 |
///
|
kpeter@758
|
326 |
/// \return \c true if a directed cycle exists in the digraph.
|
kpeter@758
|
327 |
///
|
kpeter@759
|
328 |
/// \pre \ref findMinMean() must be called before using this function.
|
kpeter@758
|
329 |
bool findCycle() {
|
kpeter@760
|
330 |
if (!_best_found) return false;
|
kpeter@760
|
331 |
_cycle_path->addBack(_policy[_best_node]);
|
kpeter@760
|
332 |
for ( Node v = _best_node;
|
kpeter@760
|
333 |
(v = _gr.target(_policy[v])) != _best_node; ) {
|
kpeter@758
|
334 |
_cycle_path->addBack(_policy[v]);
|
kpeter@758
|
335 |
}
|
kpeter@758
|
336 |
return true;
|
kpeter@758
|
337 |
}
|
kpeter@758
|
338 |
|
kpeter@758
|
339 |
/// @}
|
kpeter@758
|
340 |
|
kpeter@758
|
341 |
/// \name Query Functions
|
kpeter@759
|
342 |
/// The results of the algorithm can be obtained using these
|
kpeter@758
|
343 |
/// functions.\n
|
kpeter@758
|
344 |
/// The algorithm should be executed before using them.
|
kpeter@758
|
345 |
|
kpeter@758
|
346 |
/// @{
|
kpeter@758
|
347 |
|
kpeter@758
|
348 |
/// \brief Return the total length of the found cycle.
|
kpeter@758
|
349 |
///
|
kpeter@758
|
350 |
/// This function returns the total length of the found cycle.
|
kpeter@758
|
351 |
///
|
kpeter@760
|
352 |
/// \pre \ref run() or \ref findMinMean() must be called before
|
kpeter@758
|
353 |
/// using this function.
|
kpeter@761
|
354 |
LargeValue cycleLength() const {
|
kpeter@760
|
355 |
return _best_length;
|
kpeter@758
|
356 |
}
|
kpeter@758
|
357 |
|
kpeter@758
|
358 |
/// \brief Return the number of arcs on the found cycle.
|
kpeter@758
|
359 |
///
|
kpeter@758
|
360 |
/// This function returns the number of arcs on the found cycle.
|
kpeter@758
|
361 |
///
|
kpeter@760
|
362 |
/// \pre \ref run() or \ref findMinMean() must be called before
|
kpeter@758
|
363 |
/// using this function.
|
kpeter@758
|
364 |
int cycleArcNum() const {
|
kpeter@760
|
365 |
return _best_size;
|
kpeter@758
|
366 |
}
|
kpeter@758
|
367 |
|
kpeter@758
|
368 |
/// \brief Return the mean length of the found cycle.
|
kpeter@758
|
369 |
///
|
kpeter@758
|
370 |
/// This function returns the mean length of the found cycle.
|
kpeter@758
|
371 |
///
|
kpeter@760
|
372 |
/// \note <tt>alg.cycleMean()</tt> is just a shortcut of the
|
kpeter@758
|
373 |
/// following code.
|
kpeter@758
|
374 |
/// \code
|
kpeter@760
|
375 |
/// return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum();
|
kpeter@758
|
376 |
/// \endcode
|
kpeter@758
|
377 |
///
|
kpeter@758
|
378 |
/// \pre \ref run() or \ref findMinMean() must be called before
|
kpeter@758
|
379 |
/// using this function.
|
kpeter@758
|
380 |
double cycleMean() const {
|
kpeter@760
|
381 |
return static_cast<double>(_best_length) / _best_size;
|
kpeter@758
|
382 |
}
|
kpeter@758
|
383 |
|
kpeter@758
|
384 |
/// \brief Return the found cycle.
|
kpeter@758
|
385 |
///
|
kpeter@758
|
386 |
/// This function returns a const reference to the path structure
|
kpeter@758
|
387 |
/// storing the found cycle.
|
kpeter@758
|
388 |
///
|
kpeter@758
|
389 |
/// \pre \ref run() or \ref findCycle() must be called before using
|
kpeter@758
|
390 |
/// this function.
|
kpeter@758
|
391 |
const Path& cycle() const {
|
kpeter@758
|
392 |
return *_cycle_path;
|
kpeter@758
|
393 |
}
|
kpeter@758
|
394 |
|
kpeter@758
|
395 |
///@}
|
kpeter@758
|
396 |
|
kpeter@758
|
397 |
private:
|
kpeter@758
|
398 |
|
kpeter@760
|
399 |
// Initialize
|
kpeter@760
|
400 |
void init() {
|
kpeter@760
|
401 |
if (!_cycle_path) {
|
kpeter@760
|
402 |
_local_path = true;
|
kpeter@760
|
403 |
_cycle_path = new Path;
|
kpeter@758
|
404 |
}
|
kpeter@760
|
405 |
_queue.resize(countNodes(_gr));
|
kpeter@760
|
406 |
_best_found = false;
|
kpeter@760
|
407 |
_best_length = 0;
|
kpeter@760
|
408 |
_best_size = 1;
|
kpeter@760
|
409 |
_cycle_path->clear();
|
kpeter@760
|
410 |
}
|
kpeter@760
|
411 |
|
kpeter@760
|
412 |
// Find strongly connected components and initialize _comp_nodes
|
kpeter@760
|
413 |
// and _in_arcs
|
kpeter@760
|
414 |
void findComponents() {
|
kpeter@760
|
415 |
_comp_num = stronglyConnectedComponents(_gr, _comp);
|
kpeter@760
|
416 |
_comp_nodes.resize(_comp_num);
|
kpeter@760
|
417 |
if (_comp_num == 1) {
|
kpeter@760
|
418 |
_comp_nodes[0].clear();
|
kpeter@760
|
419 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
kpeter@760
|
420 |
_comp_nodes[0].push_back(n);
|
kpeter@760
|
421 |
_in_arcs[n].clear();
|
kpeter@760
|
422 |
for (InArcIt a(_gr, n); a != INVALID; ++a) {
|
kpeter@760
|
423 |
_in_arcs[n].push_back(a);
|
kpeter@760
|
424 |
}
|
kpeter@760
|
425 |
}
|
kpeter@760
|
426 |
} else {
|
kpeter@760
|
427 |
for (int i = 0; i < _comp_num; ++i)
|
kpeter@760
|
428 |
_comp_nodes[i].clear();
|
kpeter@760
|
429 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
kpeter@760
|
430 |
int k = _comp[n];
|
kpeter@760
|
431 |
_comp_nodes[k].push_back(n);
|
kpeter@760
|
432 |
_in_arcs[n].clear();
|
kpeter@760
|
433 |
for (InArcIt a(_gr, n); a != INVALID; ++a) {
|
kpeter@760
|
434 |
if (_comp[_gr.source(a)] == k) _in_arcs[n].push_back(a);
|
kpeter@760
|
435 |
}
|
kpeter@760
|
436 |
}
|
kpeter@758
|
437 |
}
|
kpeter@760
|
438 |
}
|
kpeter@760
|
439 |
|
kpeter@760
|
440 |
// Build the policy graph in the given strongly connected component
|
kpeter@760
|
441 |
// (the out-degree of every node is 1)
|
kpeter@760
|
442 |
bool buildPolicyGraph(int comp) {
|
kpeter@760
|
443 |
_nodes = &(_comp_nodes[comp]);
|
kpeter@760
|
444 |
if (_nodes->size() < 1 ||
|
kpeter@760
|
445 |
(_nodes->size() == 1 && _in_arcs[(*_nodes)[0]].size() == 0)) {
|
kpeter@760
|
446 |
return false;
|
kpeter@758
|
447 |
}
|
kpeter@760
|
448 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
kpeter@761
|
449 |
_dist[(*_nodes)[i]] = std::numeric_limits<LargeValue>::max();
|
kpeter@760
|
450 |
}
|
kpeter@760
|
451 |
Node u, v;
|
kpeter@760
|
452 |
Arc e;
|
kpeter@760
|
453 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
kpeter@760
|
454 |
v = (*_nodes)[i];
|
kpeter@760
|
455 |
for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
|
kpeter@760
|
456 |
e = _in_arcs[v][j];
|
kpeter@760
|
457 |
u = _gr.source(e);
|
kpeter@760
|
458 |
if (_length[e] < _dist[u]) {
|
kpeter@760
|
459 |
_dist[u] = _length[e];
|
kpeter@760
|
460 |
_policy[u] = e;
|
kpeter@760
|
461 |
}
|
kpeter@758
|
462 |
}
|
kpeter@758
|
463 |
}
|
kpeter@758
|
464 |
return true;
|
kpeter@758
|
465 |
}
|
kpeter@758
|
466 |
|
kpeter@760
|
467 |
// Find the minimum mean cycle in the policy graph
|
kpeter@760
|
468 |
void findPolicyCycle() {
|
kpeter@760
|
469 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
kpeter@760
|
470 |
_level[(*_nodes)[i]] = -1;
|
kpeter@760
|
471 |
}
|
kpeter@761
|
472 |
LargeValue clength;
|
kpeter@758
|
473 |
int csize;
|
kpeter@758
|
474 |
Node u, v;
|
kpeter@760
|
475 |
_curr_found = false;
|
kpeter@760
|
476 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
kpeter@760
|
477 |
u = (*_nodes)[i];
|
kpeter@760
|
478 |
if (_level[u] >= 0) continue;
|
kpeter@760
|
479 |
for (; _level[u] < 0; u = _gr.target(_policy[u])) {
|
kpeter@760
|
480 |
_level[u] = i;
|
kpeter@760
|
481 |
}
|
kpeter@760
|
482 |
if (_level[u] == i) {
|
kpeter@760
|
483 |
// A cycle is found
|
kpeter@760
|
484 |
clength = _length[_policy[u]];
|
kpeter@760
|
485 |
csize = 1;
|
kpeter@760
|
486 |
for (v = u; (v = _gr.target(_policy[v])) != u; ) {
|
kpeter@760
|
487 |
clength += _length[_policy[v]];
|
kpeter@760
|
488 |
++csize;
|
kpeter@758
|
489 |
}
|
kpeter@760
|
490 |
if ( !_curr_found ||
|
kpeter@760
|
491 |
(clength * _curr_size < _curr_length * csize) ) {
|
kpeter@760
|
492 |
_curr_found = true;
|
kpeter@760
|
493 |
_curr_length = clength;
|
kpeter@760
|
494 |
_curr_size = csize;
|
kpeter@760
|
495 |
_curr_node = u;
|
kpeter@758
|
496 |
}
|
kpeter@758
|
497 |
}
|
kpeter@758
|
498 |
}
|
kpeter@758
|
499 |
}
|
kpeter@758
|
500 |
|
kpeter@760
|
501 |
// Contract the policy graph and compute node distances
|
kpeter@758
|
502 |
bool computeNodeDistances() {
|
kpeter@760
|
503 |
// Find the component of the main cycle and compute node distances
|
kpeter@760
|
504 |
// using reverse BFS
|
kpeter@760
|
505 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
kpeter@760
|
506 |
_reached[(*_nodes)[i]] = false;
|
kpeter@760
|
507 |
}
|
kpeter@760
|
508 |
_qfront = _qback = 0;
|
kpeter@760
|
509 |
_queue[0] = _curr_node;
|
kpeter@760
|
510 |
_reached[_curr_node] = true;
|
kpeter@760
|
511 |
_dist[_curr_node] = 0;
|
kpeter@758
|
512 |
Node u, v;
|
kpeter@760
|
513 |
Arc e;
|
kpeter@760
|
514 |
while (_qfront <= _qback) {
|
kpeter@760
|
515 |
v = _queue[_qfront++];
|
kpeter@760
|
516 |
for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
|
kpeter@760
|
517 |
e = _in_arcs[v][j];
|
kpeter@758
|
518 |
u = _gr.source(e);
|
kpeter@760
|
519 |
if (_policy[u] == e && !_reached[u]) {
|
kpeter@760
|
520 |
_reached[u] = true;
|
kpeter@761
|
521 |
_dist[u] = _dist[v] + _length[e] * _curr_size - _curr_length;
|
kpeter@760
|
522 |
_queue[++_qback] = u;
|
kpeter@758
|
523 |
}
|
kpeter@758
|
524 |
}
|
kpeter@758
|
525 |
}
|
kpeter@760
|
526 |
|
kpeter@760
|
527 |
// Connect all other nodes to this component and compute node
|
kpeter@760
|
528 |
// distances using reverse BFS
|
kpeter@760
|
529 |
_qfront = 0;
|
kpeter@760
|
530 |
while (_qback < int(_nodes->size())-1) {
|
kpeter@760
|
531 |
v = _queue[_qfront++];
|
kpeter@760
|
532 |
for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
|
kpeter@760
|
533 |
e = _in_arcs[v][j];
|
kpeter@760
|
534 |
u = _gr.source(e);
|
kpeter@760
|
535 |
if (!_reached[u]) {
|
kpeter@760
|
536 |
_reached[u] = true;
|
kpeter@760
|
537 |
_policy[u] = e;
|
kpeter@761
|
538 |
_dist[u] = _dist[v] + _length[e] * _curr_size - _curr_length;
|
kpeter@760
|
539 |
_queue[++_qback] = u;
|
kpeter@760
|
540 |
}
|
kpeter@760
|
541 |
}
|
kpeter@760
|
542 |
}
|
kpeter@760
|
543 |
|
kpeter@760
|
544 |
// Improve node distances
|
kpeter@758
|
545 |
bool improved = false;
|
kpeter@760
|
546 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
kpeter@760
|
547 |
v = (*_nodes)[i];
|
kpeter@760
|
548 |
for (int j = 0; j < int(_in_arcs[v].size()); ++j) {
|
kpeter@760
|
549 |
e = _in_arcs[v][j];
|
kpeter@760
|
550 |
u = _gr.source(e);
|
kpeter@761
|
551 |
LargeValue delta = _dist[v] + _length[e] * _curr_size - _curr_length;
|
kpeter@761
|
552 |
if (_tolerance.less(delta, _dist[u])) {
|
kpeter@760
|
553 |
_dist[u] = delta;
|
kpeter@760
|
554 |
_policy[u] = e;
|
kpeter@760
|
555 |
improved = true;
|
kpeter@760
|
556 |
}
|
kpeter@758
|
557 |
}
|
kpeter@758
|
558 |
}
|
kpeter@758
|
559 |
return improved;
|
kpeter@758
|
560 |
}
|
kpeter@758
|
561 |
|
kpeter@758
|
562 |
}; //class MinMeanCycle
|
kpeter@758
|
563 |
|
kpeter@758
|
564 |
///@}
|
kpeter@758
|
565 |
|
kpeter@758
|
566 |
} //namespace lemon
|
kpeter@758
|
567 |
|
kpeter@758
|
568 |
#endif //LEMON_MIN_MEAN_CYCLE_H
|