Debian Codegraph @

lemon/karp_mmc.h

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