alpar@956
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
kpeter@813
|
2 |
*
|
alpar@956
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
kpeter@813
|
4 |
*
|
alpar@956
|
5 |
* Copyright (C) 2003-2010
|
kpeter@813
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
kpeter@813
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
kpeter@813
|
8 |
*
|
kpeter@813
|
9 |
* Permission to use, modify and distribute this software is granted
|
kpeter@813
|
10 |
* provided that this copyright notice appears in all copies. For
|
kpeter@813
|
11 |
* precise terms see the accompanying LICENSE file.
|
kpeter@813
|
12 |
*
|
kpeter@813
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
kpeter@813
|
14 |
* express or implied, and with no claim as to its suitability for any
|
kpeter@813
|
15 |
* purpose.
|
kpeter@813
|
16 |
*
|
kpeter@813
|
17 |
*/
|
kpeter@813
|
18 |
|
kpeter@942
|
19 |
#ifndef LEMON_HARTMANN_ORLIN_MMC_H
|
kpeter@942
|
20 |
#define LEMON_HARTMANN_ORLIN_MMC_H
|
kpeter@813
|
21 |
|
kpeter@815
|
22 |
/// \ingroup min_mean_cycle
|
kpeter@813
|
23 |
///
|
kpeter@813
|
24 |
/// \file
|
kpeter@813
|
25 |
/// \brief Hartmann-Orlin's algorithm for finding a minimum mean cycle.
|
kpeter@813
|
26 |
|
kpeter@813
|
27 |
#include <vector>
|
kpeter@813
|
28 |
#include <limits>
|
kpeter@813
|
29 |
#include <lemon/core.h>
|
kpeter@813
|
30 |
#include <lemon/path.h>
|
kpeter@813
|
31 |
#include <lemon/tolerance.h>
|
kpeter@813
|
32 |
#include <lemon/connectivity.h>
|
kpeter@813
|
33 |
|
kpeter@813
|
34 |
namespace lemon {
|
kpeter@813
|
35 |
|
kpeter@942
|
36 |
/// \brief Default traits class of HartmannOrlinMmc class.
|
kpeter@813
|
37 |
///
|
kpeter@942
|
38 |
/// Default traits class of HartmannOrlinMmc class.
|
kpeter@813
|
39 |
/// \tparam GR The type of the digraph.
|
kpeter@942
|
40 |
/// \tparam CM The type of the cost map.
|
kpeter@959
|
41 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept.
|
kpeter@813
|
42 |
#ifdef DOXYGEN
|
kpeter@942
|
43 |
template <typename GR, typename CM>
|
kpeter@813
|
44 |
#else
|
kpeter@942
|
45 |
template <typename GR, typename CM,
|
kpeter@942
|
46 |
bool integer = std::numeric_limits<typename CM::Value>::is_integer>
|
kpeter@813
|
47 |
#endif
|
kpeter@942
|
48 |
struct HartmannOrlinMmcDefaultTraits
|
kpeter@813
|
49 |
{
|
kpeter@813
|
50 |
/// The type of the digraph
|
kpeter@813
|
51 |
typedef GR Digraph;
|
kpeter@942
|
52 |
/// The type of the cost map
|
kpeter@942
|
53 |
typedef CM CostMap;
|
kpeter@942
|
54 |
/// The type of the arc costs
|
kpeter@942
|
55 |
typedef typename CostMap::Value Cost;
|
kpeter@813
|
56 |
|
kpeter@942
|
57 |
/// \brief The large cost type used for internal computations
|
kpeter@813
|
58 |
///
|
kpeter@942
|
59 |
/// The large cost type used for internal computations.
|
kpeter@942
|
60 |
/// It is \c long \c long if the \c Cost type is integer,
|
kpeter@813
|
61 |
/// otherwise it is \c double.
|
kpeter@942
|
62 |
/// \c Cost must be convertible to \c LargeCost.
|
kpeter@942
|
63 |
typedef double LargeCost;
|
kpeter@813
|
64 |
|
kpeter@813
|
65 |
/// The tolerance type used for internal computations
|
kpeter@942
|
66 |
typedef lemon::Tolerance<LargeCost> Tolerance;
|
kpeter@813
|
67 |
|
kpeter@813
|
68 |
/// \brief The path type of the found cycles
|
kpeter@813
|
69 |
///
|
kpeter@813
|
70 |
/// The path type of the found cycles.
|
kpeter@813
|
71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept
|
kpeter@819
|
72 |
/// and it must have an \c addFront() function.
|
kpeter@813
|
73 |
typedef lemon::Path<Digraph> Path;
|
kpeter@813
|
74 |
};
|
kpeter@813
|
75 |
|
kpeter@942
|
76 |
// Default traits class for integer cost types
|
kpeter@942
|
77 |
template <typename GR, typename CM>
|
kpeter@942
|
78 |
struct HartmannOrlinMmcDefaultTraits<GR, CM, true>
|
kpeter@813
|
79 |
{
|
kpeter@813
|
80 |
typedef GR Digraph;
|
kpeter@942
|
81 |
typedef CM CostMap;
|
kpeter@942
|
82 |
typedef typename CostMap::Value Cost;
|
kpeter@813
|
83 |
#ifdef LEMON_HAVE_LONG_LONG
|
kpeter@942
|
84 |
typedef long long LargeCost;
|
kpeter@813
|
85 |
#else
|
kpeter@942
|
86 |
typedef long LargeCost;
|
kpeter@813
|
87 |
#endif
|
kpeter@942
|
88 |
typedef lemon::Tolerance<LargeCost> Tolerance;
|
kpeter@813
|
89 |
typedef lemon::Path<Digraph> Path;
|
kpeter@813
|
90 |
};
|
kpeter@813
|
91 |
|
kpeter@813
|
92 |
|
kpeter@815
|
93 |
/// \addtogroup min_mean_cycle
|
kpeter@813
|
94 |
/// @{
|
kpeter@813
|
95 |
|
kpeter@813
|
96 |
/// \brief Implementation of the Hartmann-Orlin algorithm for finding
|
kpeter@813
|
97 |
/// a minimum mean cycle.
|
kpeter@813
|
98 |
///
|
kpeter@813
|
99 |
/// This class implements the Hartmann-Orlin algorithm for finding
|
kpeter@942
|
100 |
/// a directed cycle of minimum mean cost in a digraph
|
kpeter@1164
|
101 |
/// \ref hartmann93finding, \ref dasdan98minmeancycle.
|
kpeter@959
|
102 |
/// It is an improved version of \ref KarpMmc "Karp"'s original algorithm,
|
kpeter@813
|
103 |
/// it applies an efficient early termination scheme.
|
kpeter@815
|
104 |
/// It runs in time O(ne) and uses space O(n<sup>2</sup>+e).
|
kpeter@813
|
105 |
///
|
kpeter@813
|
106 |
/// \tparam GR The type of the digraph the algorithm runs on.
|
kpeter@942
|
107 |
/// \tparam CM The type of the cost map. The default
|
kpeter@813
|
108 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>".
|
kpeter@891
|
109 |
/// \tparam TR The traits class that defines various types used by the
|
kpeter@942
|
110 |
/// algorithm. By default, it is \ref HartmannOrlinMmcDefaultTraits
|
kpeter@942
|
111 |
/// "HartmannOrlinMmcDefaultTraits<GR, CM>".
|
kpeter@891
|
112 |
/// In most cases, this parameter should not be set directly,
|
kpeter@891
|
113 |
/// consider to use the named template parameters instead.
|
kpeter@813
|
114 |
#ifdef DOXYGEN
|
kpeter@942
|
115 |
template <typename GR, typename CM, typename TR>
|
kpeter@813
|
116 |
#else
|
kpeter@813
|
117 |
template < typename GR,
|
kpeter@942
|
118 |
typename CM = typename GR::template ArcMap<int>,
|
kpeter@942
|
119 |
typename TR = HartmannOrlinMmcDefaultTraits<GR, CM> >
|
kpeter@813
|
120 |
#endif
|
kpeter@942
|
121 |
class HartmannOrlinMmc
|
kpeter@813
|
122 |
{
|
kpeter@813
|
123 |
public:
|
kpeter@813
|
124 |
|
kpeter@813
|
125 |
/// The type of the digraph
|
kpeter@813
|
126 |
typedef typename TR::Digraph Digraph;
|
kpeter@942
|
127 |
/// The type of the cost map
|
kpeter@942
|
128 |
typedef typename TR::CostMap CostMap;
|
kpeter@942
|
129 |
/// The type of the arc costs
|
kpeter@942
|
130 |
typedef typename TR::Cost Cost;
|
kpeter@813
|
131 |
|
kpeter@942
|
132 |
/// \brief The large cost type
|
kpeter@813
|
133 |
///
|
kpeter@942
|
134 |
/// The large cost type used for internal computations.
|
kpeter@942
|
135 |
/// By default, it is \c long \c long if the \c Cost type is integer,
|
kpeter@813
|
136 |
/// otherwise it is \c double.
|
kpeter@942
|
137 |
typedef typename TR::LargeCost LargeCost;
|
kpeter@813
|
138 |
|
kpeter@813
|
139 |
/// The tolerance type
|
kpeter@813
|
140 |
typedef typename TR::Tolerance Tolerance;
|
kpeter@813
|
141 |
|
kpeter@813
|
142 |
/// \brief The path type of the found cycles
|
kpeter@813
|
143 |
///
|
kpeter@813
|
144 |
/// The path type of the found cycles.
|
kpeter@942
|
145 |
/// Using the \ref HartmannOrlinMmcDefaultTraits "default traits class",
|
kpeter@813
|
146 |
/// it is \ref lemon::Path "Path<Digraph>".
|
kpeter@813
|
147 |
typedef typename TR::Path Path;
|
kpeter@813
|
148 |
|
kpeter@942
|
149 |
/// The \ref HartmannOrlinMmcDefaultTraits "traits class" of the algorithm
|
kpeter@813
|
150 |
typedef TR Traits;
|
kpeter@813
|
151 |
|
kpeter@813
|
152 |
private:
|
kpeter@813
|
153 |
|
kpeter@813
|
154 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
|
kpeter@813
|
155 |
|
kpeter@813
|
156 |
// Data sturcture for path data
|
kpeter@813
|
157 |
struct PathData
|
kpeter@813
|
158 |
{
|
kpeter@942
|
159 |
LargeCost dist;
|
kpeter@813
|
160 |
Arc pred;
|
kpeter@942
|
161 |
PathData(LargeCost d, Arc p = INVALID) :
|
kpeter@814
|
162 |
dist(d), pred(p) {}
|
kpeter@813
|
163 |
};
|
kpeter@813
|
164 |
|
kpeter@813
|
165 |
typedef typename Digraph::template NodeMap<std::vector<PathData> >
|
kpeter@813
|
166 |
PathDataNodeMap;
|
kpeter@813
|
167 |
|
kpeter@813
|
168 |
private:
|
kpeter@813
|
169 |
|
kpeter@813
|
170 |
// The digraph the algorithm runs on
|
kpeter@813
|
171 |
const Digraph &_gr;
|
kpeter@942
|
172 |
// The cost of the arcs
|
kpeter@942
|
173 |
const CostMap &_cost;
|
kpeter@813
|
174 |
|
kpeter@813
|
175 |
// Data for storing the strongly connected components
|
kpeter@813
|
176 |
int _comp_num;
|
kpeter@813
|
177 |
typename Digraph::template NodeMap<int> _comp;
|
kpeter@813
|
178 |
std::vector<std::vector<Node> > _comp_nodes;
|
kpeter@813
|
179 |
std::vector<Node>* _nodes;
|
kpeter@813
|
180 |
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs;
|
kpeter@813
|
181 |
|
kpeter@813
|
182 |
// Data for the found cycles
|
kpeter@813
|
183 |
bool _curr_found, _best_found;
|
kpeter@942
|
184 |
LargeCost _curr_cost, _best_cost;
|
kpeter@813
|
185 |
int _curr_size, _best_size;
|
kpeter@813
|
186 |
Node _curr_node, _best_node;
|
kpeter@813
|
187 |
int _curr_level, _best_level;
|
kpeter@813
|
188 |
|
kpeter@813
|
189 |
Path *_cycle_path;
|
kpeter@813
|
190 |
bool _local_path;
|
kpeter@813
|
191 |
|
kpeter@813
|
192 |
// Node map for storing path data
|
kpeter@813
|
193 |
PathDataNodeMap _data;
|
kpeter@813
|
194 |
// The processed nodes in the last round
|
kpeter@813
|
195 |
std::vector<Node> _process;
|
kpeter@813
|
196 |
|
kpeter@813
|
197 |
Tolerance _tolerance;
|
kpeter@813
|
198 |
|
kpeter@814
|
199 |
// Infinite constant
|
kpeter@942
|
200 |
const LargeCost INF;
|
kpeter@814
|
201 |
|
kpeter@813
|
202 |
public:
|
kpeter@813
|
203 |
|
kpeter@813
|
204 |
/// \name Named Template Parameters
|
kpeter@813
|
205 |
/// @{
|
kpeter@813
|
206 |
|
kpeter@813
|
207 |
template <typename T>
|
kpeter@942
|
208 |
struct SetLargeCostTraits : public Traits {
|
kpeter@942
|
209 |
typedef T LargeCost;
|
kpeter@813
|
210 |
typedef lemon::Tolerance<T> Tolerance;
|
kpeter@813
|
211 |
};
|
kpeter@813
|
212 |
|
kpeter@813
|
213 |
/// \brief \ref named-templ-param "Named parameter" for setting
|
kpeter@942
|
214 |
/// \c LargeCost type.
|
kpeter@813
|
215 |
///
|
kpeter@942
|
216 |
/// \ref named-templ-param "Named parameter" for setting \c LargeCost
|
kpeter@813
|
217 |
/// type. It is used for internal computations in the algorithm.
|
kpeter@813
|
218 |
template <typename T>
|
kpeter@942
|
219 |
struct SetLargeCost
|
kpeter@942
|
220 |
: public HartmannOrlinMmc<GR, CM, SetLargeCostTraits<T> > {
|
kpeter@942
|
221 |
typedef HartmannOrlinMmc<GR, CM, SetLargeCostTraits<T> > Create;
|
kpeter@813
|
222 |
};
|
kpeter@813
|
223 |
|
kpeter@813
|
224 |
template <typename T>
|
kpeter@813
|
225 |
struct SetPathTraits : public Traits {
|
kpeter@813
|
226 |
typedef T Path;
|
kpeter@813
|
227 |
};
|
kpeter@813
|
228 |
|
kpeter@813
|
229 |
/// \brief \ref named-templ-param "Named parameter" for setting
|
kpeter@813
|
230 |
/// \c %Path type.
|
kpeter@813
|
231 |
///
|
kpeter@813
|
232 |
/// \ref named-templ-param "Named parameter" for setting the \c %Path
|
kpeter@813
|
233 |
/// type of the found cycles.
|
kpeter@813
|
234 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept
|
kpeter@813
|
235 |
/// and it must have an \c addFront() function.
|
kpeter@813
|
236 |
template <typename T>
|
kpeter@813
|
237 |
struct SetPath
|
kpeter@942
|
238 |
: public HartmannOrlinMmc<GR, CM, SetPathTraits<T> > {
|
kpeter@942
|
239 |
typedef HartmannOrlinMmc<GR, CM, SetPathTraits<T> > Create;
|
kpeter@813
|
240 |
};
|
kpeter@813
|
241 |
|
kpeter@813
|
242 |
/// @}
|
kpeter@813
|
243 |
|
kpeter@941
|
244 |
protected:
|
kpeter@941
|
245 |
|
kpeter@942
|
246 |
HartmannOrlinMmc() {}
|
kpeter@941
|
247 |
|
kpeter@813
|
248 |
public:
|
kpeter@813
|
249 |
|
kpeter@813
|
250 |
/// \brief Constructor.
|
kpeter@813
|
251 |
///
|
kpeter@813
|
252 |
/// The constructor of the class.
|
kpeter@813
|
253 |
///
|
kpeter@813
|
254 |
/// \param digraph The digraph the algorithm runs on.
|
kpeter@942
|
255 |
/// \param cost The costs of the arcs.
|
kpeter@942
|
256 |
HartmannOrlinMmc( const Digraph &digraph,
|
kpeter@942
|
257 |
const CostMap &cost ) :
|
kpeter@942
|
258 |
_gr(digraph), _cost(cost), _comp(digraph), _out_arcs(digraph),
|
kpeter@942
|
259 |
_best_found(false), _best_cost(0), _best_size(1),
|
kpeter@814
|
260 |
_cycle_path(NULL), _local_path(false), _data(digraph),
|
kpeter@942
|
261 |
INF(std::numeric_limits<LargeCost>::has_infinity ?
|
kpeter@942
|
262 |
std::numeric_limits<LargeCost>::infinity() :
|
kpeter@942
|
263 |
std::numeric_limits<LargeCost>::max())
|
kpeter@813
|
264 |
{}
|
kpeter@813
|
265 |
|
kpeter@813
|
266 |
/// Destructor.
|
kpeter@942
|
267 |
~HartmannOrlinMmc() {
|
kpeter@813
|
268 |
if (_local_path) delete _cycle_path;
|
kpeter@813
|
269 |
}
|
kpeter@813
|
270 |
|
kpeter@813
|
271 |
/// \brief Set the path structure for storing the found cycle.
|
kpeter@813
|
272 |
///
|
kpeter@813
|
273 |
/// This function sets an external path structure for storing the
|
kpeter@813
|
274 |
/// found cycle.
|
kpeter@813
|
275 |
///
|
kpeter@813
|
276 |
/// If you don't call this function before calling \ref run() or
|
kpeter@942
|
277 |
/// \ref findCycleMean(), it will allocate a local \ref Path "path"
|
kpeter@813
|
278 |
/// structure. The destuctor deallocates this automatically
|
kpeter@813
|
279 |
/// allocated object, of course.
|
kpeter@813
|
280 |
///
|
kpeter@813
|
281 |
/// \note The algorithm calls only the \ref lemon::Path::addFront()
|
kpeter@813
|
282 |
/// "addFront()" function of the given path structure.
|
kpeter@813
|
283 |
///
|
kpeter@813
|
284 |
/// \return <tt>(*this)</tt>
|
kpeter@942
|
285 |
HartmannOrlinMmc& cycle(Path &path) {
|
kpeter@813
|
286 |
if (_local_path) {
|
kpeter@813
|
287 |
delete _cycle_path;
|
kpeter@813
|
288 |
_local_path = false;
|
kpeter@813
|
289 |
}
|
kpeter@813
|
290 |
_cycle_path = &path;
|
kpeter@813
|
291 |
return *this;
|
kpeter@813
|
292 |
}
|
kpeter@813
|
293 |
|
kpeter@816
|
294 |
/// \brief Set the tolerance used by the algorithm.
|
kpeter@816
|
295 |
///
|
kpeter@816
|
296 |
/// This function sets the tolerance object used by the algorithm.
|
kpeter@816
|
297 |
///
|
kpeter@816
|
298 |
/// \return <tt>(*this)</tt>
|
kpeter@942
|
299 |
HartmannOrlinMmc& tolerance(const Tolerance& tolerance) {
|
kpeter@816
|
300 |
_tolerance = tolerance;
|
kpeter@816
|
301 |
return *this;
|
kpeter@816
|
302 |
}
|
kpeter@816
|
303 |
|
kpeter@816
|
304 |
/// \brief Return a const reference to the tolerance.
|
kpeter@816
|
305 |
///
|
kpeter@816
|
306 |
/// This function returns a const reference to the tolerance object
|
kpeter@816
|
307 |
/// used by the algorithm.
|
kpeter@816
|
308 |
const Tolerance& tolerance() const {
|
kpeter@816
|
309 |
return _tolerance;
|
kpeter@816
|
310 |
}
|
kpeter@816
|
311 |
|
kpeter@813
|
312 |
/// \name Execution control
|
kpeter@813
|
313 |
/// The simplest way to execute the algorithm is to call the \ref run()
|
kpeter@813
|
314 |
/// function.\n
|
kpeter@942
|
315 |
/// If you only need the minimum mean cost, you may call
|
kpeter@942
|
316 |
/// \ref findCycleMean().
|
kpeter@813
|
317 |
|
kpeter@813
|
318 |
/// @{
|
kpeter@813
|
319 |
|
kpeter@813
|
320 |
/// \brief Run the algorithm.
|
kpeter@813
|
321 |
///
|
kpeter@813
|
322 |
/// This function runs the algorithm.
|
kpeter@813
|
323 |
/// It can be called more than once (e.g. if the underlying digraph
|
kpeter@942
|
324 |
/// and/or the arc costs have been modified).
|
kpeter@813
|
325 |
///
|
kpeter@813
|
326 |
/// \return \c true if a directed cycle exists in the digraph.
|
kpeter@813
|
327 |
///
|
kpeter@813
|
328 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code.
|
kpeter@813
|
329 |
/// \code
|
kpeter@942
|
330 |
/// return mmc.findCycleMean() && mmc.findCycle();
|
kpeter@813
|
331 |
/// \endcode
|
kpeter@813
|
332 |
bool run() {
|
kpeter@942
|
333 |
return findCycleMean() && findCycle();
|
kpeter@813
|
334 |
}
|
kpeter@813
|
335 |
|
kpeter@813
|
336 |
/// \brief Find the minimum cycle mean.
|
kpeter@813
|
337 |
///
|
kpeter@942
|
338 |
/// This function finds the minimum mean cost of the directed
|
kpeter@813
|
339 |
/// cycles in the digraph.
|
kpeter@813
|
340 |
///
|
kpeter@813
|
341 |
/// \return \c true if a directed cycle exists in the digraph.
|
kpeter@942
|
342 |
bool findCycleMean() {
|
kpeter@813
|
343 |
// Initialization and find strongly connected components
|
kpeter@813
|
344 |
init();
|
kpeter@813
|
345 |
findComponents();
|
alpar@956
|
346 |
|
kpeter@813
|
347 |
// Find the minimum cycle mean in the components
|
kpeter@813
|
348 |
for (int comp = 0; comp < _comp_num; ++comp) {
|
kpeter@813
|
349 |
if (!initComponent(comp)) continue;
|
kpeter@813
|
350 |
processRounds();
|
alpar@956
|
351 |
|
kpeter@813
|
352 |
// Update the best cycle (global minimum mean cycle)
|
alpar@956
|
353 |
if ( _curr_found && (!_best_found ||
|
kpeter@942
|
354 |
_curr_cost * _best_size < _best_cost * _curr_size) ) {
|
kpeter@813
|
355 |
_best_found = true;
|
kpeter@942
|
356 |
_best_cost = _curr_cost;
|
kpeter@813
|
357 |
_best_size = _curr_size;
|
kpeter@813
|
358 |
_best_node = _curr_node;
|
kpeter@813
|
359 |
_best_level = _curr_level;
|
kpeter@813
|
360 |
}
|
kpeter@813
|
361 |
}
|
kpeter@813
|
362 |
return _best_found;
|
kpeter@813
|
363 |
}
|
kpeter@813
|
364 |
|
kpeter@813
|
365 |
/// \brief Find a minimum mean directed cycle.
|
kpeter@813
|
366 |
///
|
kpeter@942
|
367 |
/// This function finds a directed cycle of minimum mean cost
|
kpeter@942
|
368 |
/// in the digraph using the data computed by findCycleMean().
|
kpeter@813
|
369 |
///
|
kpeter@813
|
370 |
/// \return \c true if a directed cycle exists in the digraph.
|
kpeter@813
|
371 |
///
|
kpeter@942
|
372 |
/// \pre \ref findCycleMean() must be called before using this function.
|
kpeter@813
|
373 |
bool findCycle() {
|
kpeter@813
|
374 |
if (!_best_found) return false;
|
kpeter@813
|
375 |
IntNodeMap reached(_gr, -1);
|
kpeter@813
|
376 |
int r = _best_level + 1;
|
kpeter@813
|
377 |
Node u = _best_node;
|
kpeter@813
|
378 |
while (reached[u] < 0) {
|
kpeter@813
|
379 |
reached[u] = --r;
|
kpeter@813
|
380 |
u = _gr.source(_data[u][r].pred);
|
kpeter@813
|
381 |
}
|
kpeter@813
|
382 |
r = reached[u];
|
kpeter@813
|
383 |
Arc e = _data[u][r].pred;
|
kpeter@813
|
384 |
_cycle_path->addFront(e);
|
kpeter@942
|
385 |
_best_cost = _cost[e];
|
kpeter@813
|
386 |
_best_size = 1;
|
kpeter@813
|
387 |
Node v;
|
kpeter@813
|
388 |
while ((v = _gr.source(e)) != u) {
|
kpeter@813
|
389 |
e = _data[v][--r].pred;
|
kpeter@813
|
390 |
_cycle_path->addFront(e);
|
kpeter@942
|
391 |
_best_cost += _cost[e];
|
kpeter@813
|
392 |
++_best_size;
|
kpeter@813
|
393 |
}
|
kpeter@813
|
394 |
return true;
|
kpeter@813
|
395 |
}
|
kpeter@813
|
396 |
|
kpeter@813
|
397 |
/// @}
|
kpeter@813
|
398 |
|
kpeter@813
|
399 |
/// \name Query Functions
|
kpeter@813
|
400 |
/// The results of the algorithm can be obtained using these
|
kpeter@813
|
401 |
/// functions.\n
|
kpeter@813
|
402 |
/// The algorithm should be executed before using them.
|
kpeter@813
|
403 |
|
kpeter@813
|
404 |
/// @{
|
kpeter@813
|
405 |
|
kpeter@942
|
406 |
/// \brief Return the total cost of the found cycle.
|
kpeter@813
|
407 |
///
|
kpeter@942
|
408 |
/// This function returns the total cost of the found cycle.
|
kpeter@813
|
409 |
///
|
kpeter@942
|
410 |
/// \pre \ref run() or \ref findCycleMean() must be called before
|
kpeter@813
|
411 |
/// using this function.
|
kpeter@942
|
412 |
Cost cycleCost() const {
|
kpeter@942
|
413 |
return static_cast<Cost>(_best_cost);
|
kpeter@813
|
414 |
}
|
kpeter@813
|
415 |
|
kpeter@813
|
416 |
/// \brief Return the number of arcs on the found cycle.
|
kpeter@813
|
417 |
///
|
kpeter@813
|
418 |
/// This function returns the number of arcs on the found cycle.
|
kpeter@813
|
419 |
///
|
kpeter@942
|
420 |
/// \pre \ref run() or \ref findCycleMean() must be called before
|
kpeter@813
|
421 |
/// using this function.
|
kpeter@942
|
422 |
int cycleSize() const {
|
kpeter@813
|
423 |
return _best_size;
|
kpeter@813
|
424 |
}
|
kpeter@813
|
425 |
|
kpeter@942
|
426 |
/// \brief Return the mean cost of the found cycle.
|
kpeter@813
|
427 |
///
|
kpeter@942
|
428 |
/// This function returns the mean cost of the found cycle.
|
kpeter@813
|
429 |
///
|
kpeter@813
|
430 |
/// \note <tt>alg.cycleMean()</tt> is just a shortcut of the
|
kpeter@813
|
431 |
/// following code.
|
kpeter@813
|
432 |
/// \code
|
kpeter@942
|
433 |
/// return static_cast<double>(alg.cycleCost()) / alg.cycleSize();
|
kpeter@813
|
434 |
/// \endcode
|
kpeter@813
|
435 |
///
|
kpeter@942
|
436 |
/// \pre \ref run() or \ref findCycleMean() must be called before
|
kpeter@813
|
437 |
/// using this function.
|
kpeter@813
|
438 |
double cycleMean() const {
|
kpeter@942
|
439 |
return static_cast<double>(_best_cost) / _best_size;
|
kpeter@813
|
440 |
}
|
kpeter@813
|
441 |
|
kpeter@813
|
442 |
/// \brief Return the found cycle.
|
kpeter@813
|
443 |
///
|
kpeter@813
|
444 |
/// This function returns a const reference to the path structure
|
kpeter@813
|
445 |
/// storing the found cycle.
|
kpeter@813
|
446 |
///
|
kpeter@813
|
447 |
/// \pre \ref run() or \ref findCycle() must be called before using
|
kpeter@813
|
448 |
/// this function.
|
kpeter@813
|
449 |
const Path& cycle() const {
|
kpeter@813
|
450 |
return *_cycle_path;
|
kpeter@813
|
451 |
}
|
kpeter@813
|
452 |
|
kpeter@813
|
453 |
///@}
|
kpeter@813
|
454 |
|
kpeter@813
|
455 |
private:
|
kpeter@813
|
456 |
|
kpeter@813
|
457 |
// Initialization
|
kpeter@813
|
458 |
void init() {
|
kpeter@813
|
459 |
if (!_cycle_path) {
|
kpeter@813
|
460 |
_local_path = true;
|
kpeter@813
|
461 |
_cycle_path = new Path;
|
kpeter@813
|
462 |
}
|
kpeter@813
|
463 |
_cycle_path->clear();
|
kpeter@813
|
464 |
_best_found = false;
|
kpeter@942
|
465 |
_best_cost = 0;
|
kpeter@813
|
466 |
_best_size = 1;
|
kpeter@813
|
467 |
_cycle_path->clear();
|
kpeter@813
|
468 |
for (NodeIt u(_gr); u != INVALID; ++u)
|
kpeter@813
|
469 |
_data[u].clear();
|
kpeter@813
|
470 |
}
|
kpeter@813
|
471 |
|
kpeter@813
|
472 |
// Find strongly connected components and initialize _comp_nodes
|
kpeter@813
|
473 |
// and _out_arcs
|
kpeter@813
|
474 |
void findComponents() {
|
kpeter@813
|
475 |
_comp_num = stronglyConnectedComponents(_gr, _comp);
|
kpeter@813
|
476 |
_comp_nodes.resize(_comp_num);
|
kpeter@813
|
477 |
if (_comp_num == 1) {
|
kpeter@813
|
478 |
_comp_nodes[0].clear();
|
kpeter@813
|
479 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
kpeter@813
|
480 |
_comp_nodes[0].push_back(n);
|
kpeter@813
|
481 |
_out_arcs[n].clear();
|
kpeter@813
|
482 |
for (OutArcIt a(_gr, n); a != INVALID; ++a) {
|
kpeter@813
|
483 |
_out_arcs[n].push_back(a);
|
kpeter@813
|
484 |
}
|
kpeter@813
|
485 |
}
|
kpeter@813
|
486 |
} else {
|
kpeter@813
|
487 |
for (int i = 0; i < _comp_num; ++i)
|
kpeter@813
|
488 |
_comp_nodes[i].clear();
|
kpeter@813
|
489 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
kpeter@813
|
490 |
int k = _comp[n];
|
kpeter@813
|
491 |
_comp_nodes[k].push_back(n);
|
kpeter@813
|
492 |
_out_arcs[n].clear();
|
kpeter@813
|
493 |
for (OutArcIt a(_gr, n); a != INVALID; ++a) {
|
kpeter@813
|
494 |
if (_comp[_gr.target(a)] == k) _out_arcs[n].push_back(a);
|
kpeter@813
|
495 |
}
|
kpeter@813
|
496 |
}
|
kpeter@813
|
497 |
}
|
kpeter@813
|
498 |
}
|
kpeter@813
|
499 |
|
kpeter@813
|
500 |
// Initialize path data for the current component
|
kpeter@813
|
501 |
bool initComponent(int comp) {
|
kpeter@813
|
502 |
_nodes = &(_comp_nodes[comp]);
|
kpeter@813
|
503 |
int n = _nodes->size();
|
kpeter@813
|
504 |
if (n < 1 || (n == 1 && _out_arcs[(*_nodes)[0]].size() == 0)) {
|
kpeter@813
|
505 |
return false;
|
alpar@956
|
506 |
}
|
kpeter@813
|
507 |
for (int i = 0; i < n; ++i) {
|
kpeter@814
|
508 |
_data[(*_nodes)[i]].resize(n + 1, PathData(INF));
|
kpeter@813
|
509 |
}
|
kpeter@813
|
510 |
return true;
|
kpeter@813
|
511 |
}
|
kpeter@813
|
512 |
|
kpeter@813
|
513 |
// Process all rounds of computing path data for the current component.
|
kpeter@942
|
514 |
// _data[v][k] is the cost of a shortest directed walk from the root
|
kpeter@813
|
515 |
// node to node v containing exactly k arcs.
|
kpeter@813
|
516 |
void processRounds() {
|
kpeter@813
|
517 |
Node start = (*_nodes)[0];
|
kpeter@814
|
518 |
_data[start][0] = PathData(0);
|
kpeter@813
|
519 |
_process.clear();
|
kpeter@813
|
520 |
_process.push_back(start);
|
kpeter@813
|
521 |
|
kpeter@813
|
522 |
int k, n = _nodes->size();
|
kpeter@813
|
523 |
int next_check = 4;
|
kpeter@813
|
524 |
bool terminate = false;
|
kpeter@813
|
525 |
for (k = 1; k <= n && int(_process.size()) < n && !terminate; ++k) {
|
kpeter@813
|
526 |
processNextBuildRound(k);
|
kpeter@813
|
527 |
if (k == next_check || k == n) {
|
kpeter@813
|
528 |
terminate = checkTermination(k);
|
kpeter@813
|
529 |
next_check = next_check * 3 / 2;
|
kpeter@813
|
530 |
}
|
kpeter@813
|
531 |
}
|
kpeter@813
|
532 |
for ( ; k <= n && !terminate; ++k) {
|
kpeter@813
|
533 |
processNextFullRound(k);
|
kpeter@813
|
534 |
if (k == next_check || k == n) {
|
kpeter@813
|
535 |
terminate = checkTermination(k);
|
kpeter@813
|
536 |
next_check = next_check * 3 / 2;
|
kpeter@813
|
537 |
}
|
kpeter@813
|
538 |
}
|
kpeter@813
|
539 |
}
|
kpeter@813
|
540 |
|
kpeter@813
|
541 |
// Process one round and rebuild _process
|
kpeter@813
|
542 |
void processNextBuildRound(int k) {
|
kpeter@813
|
543 |
std::vector<Node> next;
|
kpeter@813
|
544 |
Node u, v;
|
kpeter@813
|
545 |
Arc e;
|
kpeter@942
|
546 |
LargeCost d;
|
kpeter@813
|
547 |
for (int i = 0; i < int(_process.size()); ++i) {
|
kpeter@813
|
548 |
u = _process[i];
|
kpeter@813
|
549 |
for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
|
kpeter@813
|
550 |
e = _out_arcs[u][j];
|
kpeter@813
|
551 |
v = _gr.target(e);
|
kpeter@942
|
552 |
d = _data[u][k-1].dist + _cost[e];
|
kpeter@814
|
553 |
if (_tolerance.less(d, _data[v][k].dist)) {
|
kpeter@814
|
554 |
if (_data[v][k].dist == INF) next.push_back(v);
|
kpeter@814
|
555 |
_data[v][k] = PathData(d, e);
|
kpeter@813
|
556 |
}
|
kpeter@813
|
557 |
}
|
kpeter@813
|
558 |
}
|
kpeter@813
|
559 |
_process.swap(next);
|
kpeter@813
|
560 |
}
|
kpeter@813
|
561 |
|
kpeter@813
|
562 |
// Process one round using _nodes instead of _process
|
kpeter@813
|
563 |
void processNextFullRound(int k) {
|
kpeter@813
|
564 |
Node u, v;
|
kpeter@813
|
565 |
Arc e;
|
kpeter@942
|
566 |
LargeCost d;
|
kpeter@813
|
567 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
kpeter@813
|
568 |
u = (*_nodes)[i];
|
kpeter@813
|
569 |
for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
|
kpeter@813
|
570 |
e = _out_arcs[u][j];
|
kpeter@813
|
571 |
v = _gr.target(e);
|
kpeter@942
|
572 |
d = _data[u][k-1].dist + _cost[e];
|
kpeter@814
|
573 |
if (_tolerance.less(d, _data[v][k].dist)) {
|
kpeter@814
|
574 |
_data[v][k] = PathData(d, e);
|
kpeter@813
|
575 |
}
|
kpeter@813
|
576 |
}
|
kpeter@813
|
577 |
}
|
kpeter@813
|
578 |
}
|
alpar@956
|
579 |
|
kpeter@813
|
580 |
// Check early termination
|
kpeter@813
|
581 |
bool checkTermination(int k) {
|
kpeter@813
|
582 |
typedef std::pair<int, int> Pair;
|
kpeter@813
|
583 |
typename GR::template NodeMap<Pair> level(_gr, Pair(-1, 0));
|
kpeter@942
|
584 |
typename GR::template NodeMap<LargeCost> pi(_gr);
|
kpeter@813
|
585 |
int n = _nodes->size();
|
kpeter@942
|
586 |
LargeCost cost;
|
kpeter@813
|
587 |
int size;
|
kpeter@813
|
588 |
Node u;
|
alpar@956
|
589 |
|
kpeter@813
|
590 |
// Search for cycles that are already found
|
kpeter@813
|
591 |
_curr_found = false;
|
kpeter@813
|
592 |
for (int i = 0; i < n; ++i) {
|
kpeter@813
|
593 |
u = (*_nodes)[i];
|
kpeter@814
|
594 |
if (_data[u][k].dist == INF) continue;
|
kpeter@813
|
595 |
for (int j = k; j >= 0; --j) {
|
kpeter@813
|
596 |
if (level[u].first == i && level[u].second > 0) {
|
kpeter@813
|
597 |
// A cycle is found
|
kpeter@942
|
598 |
cost = _data[u][level[u].second].dist - _data[u][j].dist;
|
kpeter@813
|
599 |
size = level[u].second - j;
|
kpeter@942
|
600 |
if (!_curr_found || cost * _curr_size < _curr_cost * size) {
|
kpeter@942
|
601 |
_curr_cost = cost;
|
kpeter@813
|
602 |
_curr_size = size;
|
kpeter@813
|
603 |
_curr_node = u;
|
kpeter@813
|
604 |
_curr_level = level[u].second;
|
kpeter@813
|
605 |
_curr_found = true;
|
kpeter@813
|
606 |
}
|
kpeter@813
|
607 |
}
|
kpeter@813
|
608 |
level[u] = Pair(i, j);
|
deba@859
|
609 |
if (j != 0) {
|
alpar@956
|
610 |
u = _gr.source(_data[u][j].pred);
|
alpar@956
|
611 |
}
|
kpeter@813
|
612 |
}
|
kpeter@813
|
613 |
}
|
kpeter@813
|
614 |
|
kpeter@813
|
615 |
// If at least one cycle is found, check the optimality condition
|
kpeter@942
|
616 |
LargeCost d;
|
kpeter@813
|
617 |
if (_curr_found && k < n) {
|
kpeter@813
|
618 |
// Find node potentials
|
kpeter@813
|
619 |
for (int i = 0; i < n; ++i) {
|
kpeter@813
|
620 |
u = (*_nodes)[i];
|
kpeter@814
|
621 |
pi[u] = INF;
|
kpeter@813
|
622 |
for (int j = 0; j <= k; ++j) {
|
kpeter@814
|
623 |
if (_data[u][j].dist < INF) {
|
kpeter@942
|
624 |
d = _data[u][j].dist * _curr_size - j * _curr_cost;
|
kpeter@814
|
625 |
if (_tolerance.less(d, pi[u])) pi[u] = d;
|
kpeter@813
|
626 |
}
|
kpeter@813
|
627 |
}
|
kpeter@813
|
628 |
}
|
kpeter@813
|
629 |
|
kpeter@813
|
630 |
// Check the optimality condition for all arcs
|
kpeter@813
|
631 |
bool done = true;
|
kpeter@813
|
632 |
for (ArcIt a(_gr); a != INVALID; ++a) {
|
kpeter@942
|
633 |
if (_tolerance.less(_cost[a] * _curr_size - _curr_cost,
|
kpeter@813
|
634 |
pi[_gr.target(a)] - pi[_gr.source(a)]) ) {
|
kpeter@813
|
635 |
done = false;
|
kpeter@813
|
636 |
break;
|
kpeter@813
|
637 |
}
|
kpeter@813
|
638 |
}
|
kpeter@813
|
639 |
return done;
|
kpeter@813
|
640 |
}
|
kpeter@813
|
641 |
return (k == n);
|
kpeter@813
|
642 |
}
|
kpeter@813
|
643 |
|
kpeter@942
|
644 |
}; //class HartmannOrlinMmc
|
kpeter@813
|
645 |
|
kpeter@813
|
646 |
///@}
|
kpeter@813
|
647 |
|
kpeter@813
|
648 |
} //namespace lemon
|
kpeter@813
|
649 |
|
kpeter@942
|
650 |
#endif //LEMON_HARTMANN_ORLIN_MMC_H
|