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