3
3
3
115
115
109
109
111
111
... | ... |
@@ -90,8 +90,8 @@ |
90 | 90 |
lemon/graph_to_eps.h \ |
91 | 91 |
lemon/grid_graph.h \ |
92 |
lemon/hartmann_orlin.h \ |
|
93 |
lemon/howard.h \ |
|
92 |
lemon/hartmann_orlin_mmc.h \ |
|
93 |
lemon/howard_mmc.h \ |
|
94 | 94 |
lemon/hypercube_graph.h \ |
95 |
lemon/ |
|
95 |
lemon/karp_mmc.h \ |
|
96 | 96 |
lemon/kruskal.h \ |
97 | 97 |
lemon/hao_orlin.h \ |
... | ... |
@@ -35,5 +35,5 @@ |
35 | 35 |
#include <lemon/circulation.h> |
36 | 36 |
#include <lemon/bellman_ford.h> |
37 |
#include <lemon/ |
|
37 |
#include <lemon/howard_mmc.h> |
|
38 | 38 |
|
39 | 39 |
namespace lemon { |
... | ... |
@@ -925,5 +925,5 @@ |
925 | 925 |
typedef SimplePath<StaticDigraph> SPath; |
926 | 926 |
typedef typename SPath::ArcIt SPathArcIt; |
927 |
typedef typename |
|
927 |
typedef typename HowardMmc<StaticDigraph, CostArcMap> |
|
928 | 928 |
::template SetPath<SPath>::Create MMC; |
929 | 929 |
|
... | ... |
@@ -932,5 +932,5 @@ |
932 | 932 |
mmc.cycle(cycle); |
933 | 933 |
buildResidualNetwork(); |
934 |
while (mmc. |
|
934 |
while (mmc.findCycleMean() && mmc.cycleCost() < 0) { |
|
935 | 935 |
// Find the cycle |
936 | 936 |
mmc.findCycle(); |
... | ... |
@@ -1133,5 +1133,5 @@ |
1133 | 1133 |
} |
1134 | 1134 |
} else { |
1135 |
typedef |
|
1135 |
typedef HowardMmc<StaticDigraph, CostArcMap> MMC; |
|
1136 | 1136 |
typedef typename BellmanFord<StaticDigraph, CostArcMap> |
1137 | 1137 |
::template SetDistMap<CostNodeMap>::Create BF; |
... | ... |
@@ -1140,8 +1140,8 @@ |
1140 | 1140 |
buildResidualNetwork(); |
1141 | 1141 |
MMC mmc(_sgr, _cost_map); |
1142 |
mmc. |
|
1142 |
mmc.findCycleMean(); |
|
1143 | 1143 |
epsilon = -mmc.cycleMean(); |
1144 |
Cost cycle_cost = mmc.cycleLength(); |
|
1145 |
int cycle_size = mmc.cycleArcNum(); |
|
1144 |
Cost cycle_cost = mmc.cycleCost(); |
|
1145 |
int cycle_size = mmc.cycleSize(); |
|
1146 | 1146 |
|
1147 | 1147 |
// Compute feasible potentials for the current epsilon |
... | ... |
@@ -17,6 +17,6 @@ |
17 | 17 |
*/ |
18 | 18 |
|
19 |
#ifndef LEMON_HARTMANN_ORLIN_H |
|
20 |
#define LEMON_HARTMANN_ORLIN_H |
|
19 |
#ifndef LEMON_HARTMANN_ORLIN_MMC_H |
|
20 |
#define LEMON_HARTMANN_ORLIN_MMC_H |
|
21 | 21 |
|
22 | 22 |
/// \ingroup min_mean_cycle |
... | ... |
@@ -34,35 +34,35 @@ |
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 |
/// \brief Default traits class of |
|
36 |
/// \brief Default traits class of HartmannOrlinMmc class. |
|
37 | 37 |
/// |
38 |
/// Default traits class of |
|
38 |
/// Default traits class of HartmannOrlinMmc class. |
|
39 | 39 |
/// \tparam GR The type of the digraph. |
40 |
/// \tparam |
|
40 |
/// \tparam CM The type of the cost map. |
|
41 | 41 |
/// It must conform to the \ref concepts::Rea_data "Rea_data" concept. |
42 | 42 |
#ifdef DOXYGEN |
43 |
template <typename GR, typename |
|
43 |
template <typename GR, typename CM> |
|
44 | 44 |
#else |
45 |
template <typename GR, typename LEN, |
|
46 |
bool integer = std::numeric_limits<typename LEN::Value>::is_integer> |
|
45 |
template <typename GR, typename CM, |
|
46 |
bool integer = std::numeric_limits<typename CM::Value>::is_integer> |
|
47 | 47 |
#endif |
48 |
struct |
|
48 |
struct HartmannOrlinMmcDefaultTraits |
|
49 | 49 |
{ |
50 | 50 |
/// The type of the digraph |
51 | 51 |
typedef GR Digraph; |
52 |
/// The type of the length map |
|
53 |
typedef LEN LengthMap; |
|
54 |
/// The type of the arc lengths |
|
55 |
typedef typename LengthMap::Value Value; |
|
52 |
/// The type of the cost map |
|
53 |
typedef CM CostMap; |
|
54 |
/// The type of the arc costs |
|
55 |
typedef typename CostMap::Value Cost; |
|
56 | 56 |
|
57 |
/// \brief The large |
|
57 |
/// \brief The large cost type used for internal computations |
|
58 | 58 |
/// |
59 |
/// The large value type used for internal computations. |
|
60 |
/// It is \c long \c long if the \c Value type is integer, |
|
59 |
/// The large cost type used for internal computations. |
|
60 |
/// It is \c long \c long if the \c Cost type is integer, |
|
61 | 61 |
/// otherwise it is \c double. |
62 |
/// \c Value must be convertible to \c LargeValue. |
|
63 |
typedef double LargeValue; |
|
62 |
/// \c Cost must be convertible to \c LargeCost. |
|
63 |
typedef double LargeCost; |
|
64 | 64 |
|
65 | 65 |
/// The tolerance type used for internal computations |
66 |
typedef lemon::Tolerance< |
|
66 |
typedef lemon::Tolerance<LargeCost> Tolerance; |
|
67 | 67 |
|
68 | 68 |
/// \brief The path type of the found cycles |
... | ... |
@@ -74,17 +74,17 @@ |
74 | 74 |
}; |
75 | 75 |
|
76 |
// Default traits class for integer value types |
|
77 |
template <typename GR, typename LEN> |
|
78 |
|
|
76 |
// Default traits class for integer cost types |
|
77 |
template <typename GR, typename CM> |
|
78 |
struct HartmannOrlinMmcDefaultTraits<GR, CM, true> |
|
79 | 79 |
{ |
80 | 80 |
typedef GR Digraph; |
81 |
typedef LEN LengthMap; |
|
82 |
typedef typename LengthMap::Value Value; |
|
81 |
typedef CM CostMap; |
|
82 |
typedef typename CostMap::Value Cost; |
|
83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
84 |
typedef long long |
|
84 |
typedef long long LargeCost; |
|
85 | 85 |
#else |
86 |
typedef long |
|
86 |
typedef long LargeCost; |
|
87 | 87 |
#endif |
88 |
typedef lemon::Tolerance< |
|
88 |
typedef lemon::Tolerance<LargeCost> Tolerance; |
|
89 | 89 |
typedef lemon::Path<Digraph> Path; |
90 | 90 |
}; |
... | ... |
@@ -98,5 +98,5 @@ |
98 | 98 |
/// |
99 | 99 |
/// This class implements the Hartmann-Orlin algorithm for finding |
100 |
/// a directed cycle of minimum mean |
|
100 |
/// a directed cycle of minimum mean cost in a digraph |
|
101 | 101 |
/// \ref amo93networkflows, \ref dasdan98minmeancycle. |
102 | 102 |
/// It is an improved version of \ref Karp "Karp"'s original algorithm, |
... | ... |
@@ -105,19 +105,19 @@ |
105 | 105 |
/// |
106 | 106 |
/// \tparam GR The type of the digraph the algorithm runs on. |
107 |
/// \tparam |
|
107 |
/// \tparam CM The type of the cost map. The default |
|
108 | 108 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
109 | 109 |
/// \tparam TR The traits class that defines various types used by the |
110 |
/// algorithm. By default, it is \ref HartmannOrlinDefaultTraits |
|
111 |
/// "HartmannOrlinDefaultTraits<GR, LEN>". |
|
110 |
/// algorithm. By default, it is \ref HartmannOrlinMmcDefaultTraits |
|
111 |
/// "HartmannOrlinMmcDefaultTraits<GR, CM>". |
|
112 | 112 |
/// In most cases, this parameter should not be set directly, |
113 | 113 |
/// consider to use the named template parameters instead. |
114 | 114 |
#ifdef DOXYGEN |
115 |
template <typename GR, typename |
|
115 |
template <typename GR, typename CM, typename TR> |
|
116 | 116 |
#else |
117 | 117 |
template < typename GR, |
118 |
typename LEN = typename GR::template ArcMap<int>, |
|
119 |
typename TR = HartmannOrlinDefaultTraits<GR, LEN> > |
|
118 |
typename CM = typename GR::template ArcMap<int>, |
|
119 |
typename TR = HartmannOrlinMmcDefaultTraits<GR, CM> > |
|
120 | 120 |
#endif |
121 |
class |
|
121 |
class HartmannOrlinMmc |
|
122 | 122 |
{ |
123 | 123 |
public: |
... | ... |
@@ -125,15 +125,15 @@ |
125 | 125 |
/// The type of the digraph |
126 | 126 |
typedef typename TR::Digraph Digraph; |
127 |
/// The type of the length map |
|
128 |
typedef typename TR::LengthMap LengthMap; |
|
129 |
/// The type of the arc lengths |
|
130 |
typedef typename TR::Value Value; |
|
127 |
/// The type of the cost map |
|
128 |
typedef typename TR::CostMap CostMap; |
|
129 |
/// The type of the arc costs |
|
130 |
typedef typename TR::Cost Cost; |
|
131 | 131 |
|
132 |
/// \brief The large |
|
132 |
/// \brief The large cost type |
|
133 | 133 |
/// |
134 |
/// The large value type used for internal computations. |
|
135 |
/// By default, it is \c long \c long if the \c Value type is integer, |
|
134 |
/// The large cost type used for internal computations. |
|
135 |
/// By default, it is \c long \c long if the \c Cost type is integer, |
|
136 | 136 |
/// otherwise it is \c double. |
137 |
typedef typename TR:: |
|
137 |
typedef typename TR::LargeCost LargeCost; |
|
138 | 138 |
|
139 | 139 |
/// The tolerance type |
... | ... |
@@ -143,9 +143,9 @@ |
143 | 143 |
/// |
144 | 144 |
/// The path type of the found cycles. |
145 |
/// Using the \ref |
|
145 |
/// Using the \ref HartmannOrlinMmcDefaultTraits "default traits class", |
|
146 | 146 |
/// it is \ref lemon::Path "Path<Digraph>". |
147 | 147 |
typedef typename TR::Path Path; |
148 | 148 |
|
149 |
/// The \ref |
|
149 |
/// The \ref HartmannOrlinMmcDefaultTraits "traits class" of the algorithm |
|
150 | 150 |
typedef TR Traits; |
151 | 151 |
|
... | ... |
@@ -157,7 +157,7 @@ |
157 | 157 |
struct PathData |
158 | 158 |
{ |
159 |
|
|
159 |
LargeCost dist; |
|
160 | 160 |
Arc pred; |
161 |
PathData( |
|
161 |
PathData(LargeCost d, Arc p = INVALID) : |
|
162 | 162 |
dist(d), pred(p) {} |
163 | 163 |
}; |
... | ... |
@@ -170,6 +170,6 @@ |
170 | 170 |
// The digraph the algorithm runs on |
171 | 171 |
const Digraph &_gr; |
172 |
// The length of the arcs |
|
173 |
const LengthMap &_length; |
|
172 |
// The cost of the arcs |
|
173 |
const CostMap &_cost; |
|
174 | 174 |
|
175 | 175 |
// Data for storing the strongly connected components |
... | ... |
@@ -182,5 +182,5 @@ |
182 | 182 |
// Data for the found cycles |
183 | 183 |
bool _curr_found, _best_found; |
184 |
|
|
184 |
LargeCost _curr_cost, _best_cost; |
|
185 | 185 |
int _curr_size, _best_size; |
186 | 186 |
Node _curr_node, _best_node; |
... | ... |
@@ -198,5 +198,5 @@ |
198 | 198 |
|
199 | 199 |
// Infinite constant |
200 |
const |
|
200 |
const LargeCost INF; |
|
201 | 201 |
|
202 | 202 |
public: |
... | ... |
@@ -206,18 +206,18 @@ |
206 | 206 |
|
207 | 207 |
template <typename T> |
208 |
struct SetLargeValueTraits : public Traits { |
|
209 |
typedef T LargeValue; |
|
208 |
struct SetLargeCostTraits : public Traits { |
|
209 |
typedef T LargeCost; |
|
210 | 210 |
typedef lemon::Tolerance<T> Tolerance; |
211 | 211 |
}; |
212 | 212 |
|
213 | 213 |
/// \brief \ref named-templ-param "Named parameter" for setting |
214 |
/// \c |
|
214 |
/// \c LargeCost type. |
|
215 | 215 |
/// |
216 |
/// \ref named-templ-param "Named parameter" for setting \c |
|
216 |
/// \ref named-templ-param "Named parameter" for setting \c LargeCost |
|
217 | 217 |
/// type. It is used for internal computations in the algorithm. |
218 | 218 |
template <typename T> |
219 |
struct SetLargeValue |
|
220 |
: public HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > { |
|
221 |
|
|
219 |
struct SetLargeCost |
|
220 |
: public HartmannOrlinMmc<GR, CM, SetLargeCostTraits<T> > { |
|
221 |
typedef HartmannOrlinMmc<GR, CM, SetLargeCostTraits<T> > Create; |
|
222 | 222 |
}; |
223 | 223 |
|
... | ... |
@@ -236,6 +236,6 @@ |
236 | 236 |
template <typename T> |
237 | 237 |
struct SetPath |
238 |
: public HartmannOrlin<GR, LEN, SetPathTraits<T> > { |
|
239 |
typedef HartmannOrlin<GR, LEN, SetPathTraits<T> > Create; |
|
238 |
: public HartmannOrlinMmc<GR, CM, SetPathTraits<T> > { |
|
239 |
typedef HartmannOrlinMmc<GR, CM, SetPathTraits<T> > Create; |
|
240 | 240 |
}; |
241 | 241 |
|
... | ... |
@@ -244,5 +244,5 @@ |
244 | 244 |
protected: |
245 | 245 |
|
246 |
|
|
246 |
HartmannOrlinMmc() {} |
|
247 | 247 |
|
248 | 248 |
public: |
... | ... |
@@ -253,17 +253,17 @@ |
253 | 253 |
/// |
254 | 254 |
/// \param digraph The digraph the algorithm runs on. |
255 |
/// \param length The lengths (costs) of the arcs. |
|
256 |
HartmannOrlin( const Digraph &digraph, |
|
257 |
const LengthMap &length ) : |
|
258 |
_gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph), |
|
259 |
|
|
255 |
/// \param cost The costs of the arcs. |
|
256 |
HartmannOrlinMmc( const Digraph &digraph, |
|
257 |
const CostMap &cost ) : |
|
258 |
_gr(digraph), _cost(cost), _comp(digraph), _out_arcs(digraph), |
|
259 |
_best_found(false), _best_cost(0), _best_size(1), |
|
260 | 260 |
_cycle_path(NULL), _local_path(false), _data(digraph), |
261 |
INF(std::numeric_limits<LargeValue>::has_infinity ? |
|
262 |
std::numeric_limits<LargeValue>::infinity() : |
|
263 |
|
|
261 |
INF(std::numeric_limits<LargeCost>::has_infinity ? |
|
262 |
std::numeric_limits<LargeCost>::infinity() : |
|
263 |
std::numeric_limits<LargeCost>::max()) |
|
264 | 264 |
{} |
265 | 265 |
|
266 | 266 |
/// Destructor. |
267 |
~ |
|
267 |
~HartmannOrlinMmc() { |
|
268 | 268 |
if (_local_path) delete _cycle_path; |
269 | 269 |
} |
... | ... |
@@ -275,5 +275,5 @@ |
275 | 275 |
/// |
276 | 276 |
/// If you don't call this function before calling \ref run() or |
277 |
/// \ref |
|
277 |
/// \ref findCycleMean(), it will allocate a local \ref Path "path" |
|
278 | 278 |
/// structure. The destuctor deallocates this automatically |
279 | 279 |
/// allocated object, of course. |
... | ... |
@@ -283,5 +283,5 @@ |
283 | 283 |
/// |
284 | 284 |
/// \return <tt>(*this)</tt> |
285 |
|
|
285 |
HartmannOrlinMmc& cycle(Path &path) { |
|
286 | 286 |
if (_local_path) { |
287 | 287 |
delete _cycle_path; |
... | ... |
@@ -297,5 +297,5 @@ |
297 | 297 |
/// |
298 | 298 |
/// \return <tt>(*this)</tt> |
299 |
|
|
299 |
HartmannOrlinMmc& tolerance(const Tolerance& tolerance) { |
|
300 | 300 |
_tolerance = tolerance; |
301 | 301 |
return *this; |
... | ... |
@@ -313,6 +313,6 @@ |
313 | 313 |
/// The simplest way to execute the algorithm is to call the \ref run() |
314 | 314 |
/// function.\n |
315 |
/// If you only need the minimum mean length, you may call |
|
316 |
/// \ref findMinMean(). |
|
315 |
/// If you only need the minimum mean cost, you may call |
|
316 |
/// \ref findCycleMean(). |
|
317 | 317 |
|
318 | 318 |
/// @{ |
... | ... |
@@ -322,5 +322,5 @@ |
322 | 322 |
/// This function runs the algorithm. |
323 | 323 |
/// It can be called more than once (e.g. if the underlying digraph |
324 |
/// and/or the arc |
|
324 |
/// and/or the arc costs have been modified). |
|
325 | 325 |
/// |
326 | 326 |
/// \return \c true if a directed cycle exists in the digraph. |
... | ... |
@@ -328,17 +328,17 @@ |
328 | 328 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
329 | 329 |
/// \code |
330 |
/// return mmc. |
|
330 |
/// return mmc.findCycleMean() && mmc.findCycle(); |
|
331 | 331 |
/// \endcode |
332 | 332 |
bool run() { |
333 |
return |
|
333 |
return findCycleMean() && findCycle(); |
|
334 | 334 |
} |
335 | 335 |
|
336 | 336 |
/// \brief Find the minimum cycle mean. |
337 | 337 |
/// |
338 |
/// This function finds the minimum mean |
|
338 |
/// This function finds the minimum mean cost of the directed |
|
339 | 339 |
/// cycles in the digraph. |
340 | 340 |
/// |
341 | 341 |
/// \return \c true if a directed cycle exists in the digraph. |
342 |
bool |
|
342 |
bool findCycleMean() { |
|
343 | 343 |
// Initialization and find strongly connected components |
344 | 344 |
init(); |
... | ... |
@@ -352,7 +352,7 @@ |
352 | 352 |
// Update the best cycle (global minimum mean cycle) |
353 | 353 |
if ( _curr_found && (!_best_found || |
354 |
|
|
354 |
_curr_cost * _best_size < _best_cost * _curr_size) ) { |
|
355 | 355 |
_best_found = true; |
356 |
|
|
356 |
_best_cost = _curr_cost; |
|
357 | 357 |
_best_size = _curr_size; |
358 | 358 |
_best_node = _curr_node; |
... | ... |
@@ -365,10 +365,10 @@ |
365 | 365 |
/// \brief Find a minimum mean directed cycle. |
366 | 366 |
/// |
367 |
/// This function finds a directed cycle of minimum mean length |
|
368 |
/// in the digraph using the data computed by findMinMean(). |
|
367 |
/// This function finds a directed cycle of minimum mean cost |
|
368 |
/// in the digraph using the data computed by findCycleMean(). |
|
369 | 369 |
/// |
370 | 370 |
/// \return \c true if a directed cycle exists in the digraph. |
371 | 371 |
/// |
372 |
/// \pre \ref |
|
372 |
/// \pre \ref findCycleMean() must be called before using this function. |
|
373 | 373 |
bool findCycle() { |
374 | 374 |
if (!_best_found) return false; |
... | ... |
@@ -383,5 +383,5 @@ |
383 | 383 |
Arc e = _data[u][r].pred; |
384 | 384 |
_cycle_path->addFront(e); |
385 |
|
|
385 |
_best_cost = _cost[e]; |
|
386 | 386 |
_best_size = 1; |
387 | 387 |
Node v; |
... | ... |
@@ -389,5 +389,5 @@ |
389 | 389 |
e = _data[v][--r].pred; |
390 | 390 |
_cycle_path->addFront(e); |
391 |
|
|
391 |
_best_cost += _cost[e]; |
|
392 | 392 |
++_best_size; |
393 | 393 |
} |
... | ... |
@@ -404,12 +404,12 @@ |
404 | 404 |
/// @{ |
405 | 405 |
|
406 |
/// \brief Return the total |
|
406 |
/// \brief Return the total cost of the found cycle. |
|
407 | 407 |
/// |
408 |
/// This function returns the total |
|
408 |
/// This function returns the total cost of the found cycle. |
|
409 | 409 |
/// |
410 |
/// \pre \ref run() or \ref |
|
410 |
/// \pre \ref run() or \ref findCycleMean() must be called before |
|
411 | 411 |
/// using this function. |
412 |
Value cycleLength() const { |
|
413 |
return static_cast<Value>(_best_length); |
|
412 |
Cost cycleCost() const { |
|
413 |
return static_cast<Cost>(_best_cost); |
|
414 | 414 |
} |
415 | 415 |
|
... | ... |
@@ -418,24 +418,24 @@ |
418 | 418 |
/// This function returns the number of arcs on the found cycle. |
419 | 419 |
/// |
420 |
/// \pre \ref run() or \ref |
|
420 |
/// \pre \ref run() or \ref findCycleMean() must be called before |
|
421 | 421 |
/// using this function. |
422 |
int |
|
422 |
int cycleSize() const { |
|
423 | 423 |
return _best_size; |
424 | 424 |
} |
425 | 425 |
|
426 |
/// \brief Return the mean |
|
426 |
/// \brief Return the mean cost of the found cycle. |
|
427 | 427 |
/// |
428 |
/// This function returns the mean |
|
428 |
/// This function returns the mean cost of the found cycle. |
|
429 | 429 |
/// |
430 | 430 |
/// \note <tt>alg.cycleMean()</tt> is just a shortcut of the |
431 | 431 |
/// following code. |
432 | 432 |
/// \code |
433 |
/// return static_cast<double>(alg. |
|
433 |
/// return static_cast<double>(alg.cycleCost()) / alg.cycleSize(); |
|
434 | 434 |
/// \endcode |
435 | 435 |
/// |
436 |
/// \pre \ref run() or \ref |
|
436 |
/// \pre \ref run() or \ref findCycleMean() must be called before |
|
437 | 437 |
/// using this function. |
438 | 438 |
double cycleMean() const { |
439 |
return static_cast<double>( |
|
439 |
return static_cast<double>(_best_cost) / _best_size; |
|
440 | 440 |
} |
441 | 441 |
|
... | ... |
@@ -463,5 +463,5 @@ |
463 | 463 |
_cycle_path->clear(); |
464 | 464 |
_best_found = false; |
465 |
|
|
465 |
_best_cost = 0; |
|
466 | 466 |
_best_size = 1; |
467 | 467 |
_cycle_path->clear(); |
... | ... |
@@ -512,5 +512,5 @@ |
512 | 512 |
|
513 | 513 |
// Process all rounds of computing path data for the current component. |
514 |
// _data[v][k] is the |
|
514 |
// _data[v][k] is the cost of a shortest directed walk from the root |
|
515 | 515 |
// node to node v containing exactly k arcs. |
516 | 516 |
void processRounds() { |
... | ... |
@@ -544,5 +544,5 @@ |
544 | 544 |
Node u, v; |
545 | 545 |
Arc e; |
546 |
|
|
546 |
LargeCost d; |
|
547 | 547 |
for (int i = 0; i < int(_process.size()); ++i) { |
548 | 548 |
u = _process[i]; |
... | ... |
@@ -550,5 +550,5 @@ |
550 | 550 |
e = _out_arcs[u][j]; |
551 | 551 |
v = _gr.target(e); |
552 |
d = _data[u][k-1].dist + |
|
552 |
d = _data[u][k-1].dist + _cost[e]; |
|
553 | 553 |
if (_tolerance.less(d, _data[v][k].dist)) { |
554 | 554 |
if (_data[v][k].dist == INF) next.push_back(v); |
... | ... |
@@ -564,5 +564,5 @@ |
564 | 564 |
Node u, v; |
565 | 565 |
Arc e; |
566 |
|
|
566 |
LargeCost d; |
|
567 | 567 |
for (int i = 0; i < int(_nodes->size()); ++i) { |
568 | 568 |
u = (*_nodes)[i]; |
... | ... |
@@ -570,5 +570,5 @@ |
570 | 570 |
e = _out_arcs[u][j]; |
571 | 571 |
v = _gr.target(e); |
572 |
d = _data[u][k-1].dist + |
|
572 |
d = _data[u][k-1].dist + _cost[e]; |
|
573 | 573 |
if (_tolerance.less(d, _data[v][k].dist)) { |
574 | 574 |
_data[v][k] = PathData(d, e); |
... | ... |
@@ -582,7 +582,7 @@ |
582 | 582 |
typedef std::pair<int, int> Pair; |
583 | 583 |
typename GR::template NodeMap<Pair> level(_gr, Pair(-1, 0)); |
584 |
typename GR::template NodeMap< |
|
584 |
typename GR::template NodeMap<LargeCost> pi(_gr); |
|
585 | 585 |
int n = _nodes->size(); |
586 |
|
|
586 |
LargeCost cost; |
|
587 | 587 |
int size; |
588 | 588 |
Node u; |
... | ... |
@@ -596,8 +596,8 @@ |
596 | 596 |
if (level[u].first == i && level[u].second > 0) { |
597 | 597 |
// A cycle is found |
598 |
|
|
598 |
cost = _data[u][level[u].second].dist - _data[u][j].dist; |
|
599 | 599 |
size = level[u].second - j; |
600 |
if (!_curr_found || length * _curr_size < _curr_length * size) { |
|
601 |
_curr_length = length; |
|
600 |
if (!_curr_found || cost * _curr_size < _curr_cost * size) { |
|
601 |
_curr_cost = cost; |
|
602 | 602 |
_curr_size = size; |
603 | 603 |
_curr_node = u; |
... | ... |
@@ -614,5 +614,5 @@ |
614 | 614 |
|
615 | 615 |
// If at least one cycle is found, check the optimality condition |
616 |
|
|
616 |
LargeCost d; |
|
617 | 617 |
if (_curr_found && k < n) { |
618 | 618 |
// Find node potentials |
... | ... |
@@ -622,5 +622,5 @@ |
622 | 622 |
for (int j = 0; j <= k; ++j) { |
623 | 623 |
if (_data[u][j].dist < INF) { |
624 |
d = _data[u][j].dist * _curr_size - j * |
|
624 |
d = _data[u][j].dist * _curr_size - j * _curr_cost; |
|
625 | 625 |
if (_tolerance.less(d, pi[u])) pi[u] = d; |
626 | 626 |
} |
... | ... |
@@ -631,5 +631,5 @@ |
631 | 631 |
bool done = true; |
632 | 632 |
for (ArcIt a(_gr); a != INVALID; ++a) { |
633 |
if (_tolerance.less( |
|
633 |
if (_tolerance.less(_cost[a] * _curr_size - _curr_cost, |
|
634 | 634 |
pi[_gr.target(a)] - pi[_gr.source(a)]) ) { |
635 | 635 |
done = false; |
... | ... |
@@ -642,5 +642,5 @@ |
642 | 642 |
} |
643 | 643 |
|
644 |
}; //class |
|
644 |
}; //class HartmannOrlinMmc |
|
645 | 645 |
|
646 | 646 |
///@} |
... | ... |
@@ -648,3 +648,3 @@ |
648 | 648 |
} //namespace lemon |
649 | 649 |
|
650 |
#endif // |
|
650 |
#endif //LEMON_HARTMANN_ORLIN_MMC_H |
... | ... |
@@ -17,6 +17,6 @@ |
17 | 17 |
*/ |
18 | 18 |
|
19 |
#ifndef LEMON_HOWARD_H |
|
20 |
#define LEMON_HOWARD_H |
|
19 |
#ifndef LEMON_HOWARD_MMC_H |
|
20 |
#define LEMON_HOWARD_MMC_H |
|
21 | 21 |
|
22 | 22 |
/// \ingroup min_mean_cycle |
... | ... |
@@ -34,35 +34,35 @@ |
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 |
/// \brief Default traits class of |
|
36 |
/// \brief Default traits class of HowardMmc class. |
|
37 | 37 |
/// |
38 |
/// Default traits class of |
|
38 |
/// Default traits class of HowardMmc class. |
|
39 | 39 |
/// \tparam GR The type of the digraph. |
40 |
/// \tparam |
|
40 |
/// \tparam CM The type of the cost map. |
|
41 | 41 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
42 | 42 |
#ifdef DOXYGEN |
43 |
template <typename GR, typename |
|
43 |
template <typename GR, typename CM> |
|
44 | 44 |
#else |
45 |
template <typename GR, typename LEN, |
|
46 |
bool integer = std::numeric_limits<typename LEN::Value>::is_integer> |
|
45 |
template <typename GR, typename CM, |
|
46 |
bool integer = std::numeric_limits<typename CM::Value>::is_integer> |
|
47 | 47 |
#endif |
48 |
struct |
|
48 |
struct HowardMmcDefaultTraits |
|
49 | 49 |
{ |
50 | 50 |
/// The type of the digraph |
51 | 51 |
typedef GR Digraph; |
52 |
/// The type of the length map |
|
53 |
typedef LEN LengthMap; |
|
54 |
/// The type of the arc lengths |
|
55 |
typedef typename LengthMap::Value Value; |
|
52 |
/// The type of the cost map |
|
53 |
typedef CM CostMap; |
|
54 |
/// The type of the arc costs |
|
55 |
typedef typename CostMap::Value Cost; |
|
56 | 56 |
|
57 |
/// \brief The large |
|
57 |
/// \brief The large cost type used for internal computations |
|
58 | 58 |
/// |
59 |
/// The large value type used for internal computations. |
|
60 |
/// It is \c long \c long if the \c Value type is integer, |
|
59 |
/// The large cost type used for internal computations. |
|
60 |
/// It is \c long \c long if the \c Cost type is integer, |
|
61 | 61 |
/// otherwise it is \c double. |
62 |
/// \c Value must be convertible to \c LargeValue. |
|
63 |
typedef double LargeValue; |
|
62 |
/// \c Cost must be convertible to \c LargeCost. |
|
63 |
typedef double LargeCost; |
|
64 | 64 |
|
65 | 65 |
/// The tolerance type used for internal computations |
66 |
typedef lemon::Tolerance< |
|
66 |
typedef lemon::Tolerance<LargeCost> Tolerance; |
|
67 | 67 |
|
68 | 68 |
/// \brief The path type of the found cycles |
... | ... |
@@ -74,17 +74,17 @@ |
74 | 74 |
}; |
75 | 75 |
|
76 |
// Default traits class for integer value types |
|
77 |
template <typename GR, typename LEN> |
|
78 |
|
|
76 |
// Default traits class for integer cost types |
|
77 |
template <typename GR, typename CM> |
|
78 |
struct HowardMmcDefaultTraits<GR, CM, true> |
|
79 | 79 |
{ |
80 | 80 |
typedef GR Digraph; |
81 |
typedef LEN LengthMap; |
|
82 |
typedef typename LengthMap::Value Value; |
|
81 |
typedef CM CostMap; |
|
82 |
typedef typename CostMap::Value Cost; |
|
83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
84 |
typedef long long |
|
84 |
typedef long long LargeCost; |
|
85 | 85 |
#else |
86 |
typedef long |
|
86 |
typedef long LargeCost; |
|
87 | 87 |
#endif |
88 |
typedef lemon::Tolerance< |
|
88 |
typedef lemon::Tolerance<LargeCost> Tolerance; |
|
89 | 89 |
typedef lemon::Path<Digraph> Path; |
90 | 90 |
}; |
... | ... |
@@ -98,5 +98,5 @@ |
98 | 98 |
/// |
99 | 99 |
/// This class implements Howard's policy iteration algorithm for finding |
100 |
/// a directed cycle of minimum mean |
|
100 |
/// a directed cycle of minimum mean cost in a digraph |
|
101 | 101 |
/// \ref amo93networkflows, \ref dasdan98minmeancycle. |
102 | 102 |
/// This class provides the most efficient algorithm for the |
... | ... |
@@ -105,19 +105,19 @@ |
105 | 105 |
/// |
106 | 106 |
/// \tparam GR The type of the digraph the algorithm runs on. |
107 |
/// \tparam |
|
107 |
/// \tparam CM The type of the cost map. The default |
|
108 | 108 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
109 | 109 |
/// \tparam TR The traits class that defines various types used by the |
110 |
/// algorithm. By default, it is \ref HowardDefaultTraits |
|
111 |
/// "HowardDefaultTraits<GR, LEN>". |
|
110 |
/// algorithm. By default, it is \ref HowardMmcDefaultTraits |
|
111 |
/// "HowardMmcDefaultTraits<GR, CM>". |
|
112 | 112 |
/// In most cases, this parameter should not be set directly, |
113 | 113 |
/// consider to use the named template parameters instead. |
114 | 114 |
#ifdef DOXYGEN |
115 |
template <typename GR, typename |
|
115 |
template <typename GR, typename CM, typename TR> |
|
116 | 116 |
#else |
117 | 117 |
template < typename GR, |
118 |
typename LEN = typename GR::template ArcMap<int>, |
|
119 |
typename TR = HowardDefaultTraits<GR, LEN> > |
|
118 |
typename CM = typename GR::template ArcMap<int>, |
|
119 |
typename TR = HowardMmcDefaultTraits<GR, CM> > |
|
120 | 120 |
#endif |
121 |
class |
|
121 |
class HowardMmc |
|
122 | 122 |
{ |
123 | 123 |
public: |
... | ... |
@@ -125,15 +125,15 @@ |
125 | 125 |
/// The type of the digraph |
126 | 126 |
typedef typename TR::Digraph Digraph; |
127 |
/// The type of the length map |
|
128 |
typedef typename TR::LengthMap LengthMap; |
|
129 |
/// The type of the arc lengths |
|
130 |
typedef typename TR::Value Value; |
|
127 |
/// The type of the cost map |
|
128 |
typedef typename TR::CostMap CostMap; |
|
129 |
/// The type of the arc costs |
|
130 |
typedef typename TR::Cost Cost; |
|
131 | 131 |
|
132 |
/// \brief The large |
|
132 |
/// \brief The large cost type |
|
133 | 133 |
/// |
134 |
/// The large value type used for internal computations. |
|
135 |
/// By default, it is \c long \c long if the \c Value type is integer, |
|
134 |
/// The large cost type used for internal computations. |
|
135 |
/// By default, it is \c long \c long if the \c Cost type is integer, |
|
136 | 136 |
/// otherwise it is \c double. |
137 |
typedef typename TR:: |
|
137 |
typedef typename TR::LargeCost LargeCost; |
|
138 | 138 |
|
139 | 139 |
/// The tolerance type |
... | ... |
@@ -143,9 +143,9 @@ |
143 | 143 |
/// |
144 | 144 |
/// The path type of the found cycles. |
145 |
/// Using the \ref |
|
145 |
/// Using the \ref HowardMmcDefaultTraits "default traits class", |
|
146 | 146 |
/// it is \ref lemon::Path "Path<Digraph>". |
147 | 147 |
typedef typename TR::Path Path; |
148 | 148 |
|
149 |
/// The \ref |
|
149 |
/// The \ref HowardMmcDefaultTraits "traits class" of the algorithm |
|
150 | 150 |
typedef TR Traits; |
151 | 151 |
|
... | ... |
@@ -156,10 +156,10 @@ |
156 | 156 |
// The digraph the algorithm runs on |
157 | 157 |
const Digraph &_gr; |
158 |
// The length of the arcs |
|
159 |
const LengthMap &_length; |
|
158 |
// The cost of the arcs |
|
159 |
const CostMap &_cost; |
|
160 | 160 |
|
161 | 161 |
// Data for the found cycles |
162 | 162 |
bool _curr_found, _best_found; |
163 |
|
|
163 |
LargeCost _curr_cost, _best_cost; |
|
164 | 164 |
int _curr_size, _best_size; |
165 | 165 |
Node _curr_node, _best_node; |
... | ... |
@@ -172,5 +172,5 @@ |
172 | 172 |
typename Digraph::template NodeMap<bool> _reached; |
173 | 173 |
typename Digraph::template NodeMap<int> _level; |
174 |
typename Digraph::template NodeMap< |
|
174 |
typename Digraph::template NodeMap<LargeCost> _dist; |
|
175 | 175 |
|
176 | 176 |
// Data for storing the strongly connected components |
... | ... |
@@ -188,5 +188,5 @@ |
188 | 188 |
|
189 | 189 |
// Infinite constant |
190 |
const |
|
190 |
const LargeCost INF; |
|
191 | 191 |
|
192 | 192 |
public: |
... | ... |
@@ -196,18 +196,18 @@ |
196 | 196 |
|
197 | 197 |
template <typename T> |
198 |
struct SetLargeValueTraits : public Traits { |
|
199 |
typedef T LargeValue; |
|
198 |
struct SetLargeCostTraits : public Traits { |
|
199 |
typedef T LargeCost; |
|
200 | 200 |
typedef lemon::Tolerance<T> Tolerance; |
201 | 201 |
}; |
202 | 202 |
|
203 | 203 |
/// \brief \ref named-templ-param "Named parameter" for setting |
204 |
/// \c |
|
204 |
/// \c LargeCost type. |
|
205 | 205 |
/// |
206 |
/// \ref named-templ-param "Named parameter" for setting \c |
|
206 |
/// \ref named-templ-param "Named parameter" for setting \c LargeCost |
|
207 | 207 |
/// type. It is used for internal computations in the algorithm. |
208 | 208 |
template <typename T> |
209 |
struct SetLargeValue |
|
210 |
: public Howard<GR, LEN, SetLargeValueTraits<T> > { |
|
211 |
|
|
209 |
struct SetLargeCost |
|
210 |
: public HowardMmc<GR, CM, SetLargeCostTraits<T> > { |
|
211 |
typedef HowardMmc<GR, CM, SetLargeCostTraits<T> > Create; |
|
212 | 212 |
}; |
213 | 213 |
|
... | ... |
@@ -226,6 +226,6 @@ |
226 | 226 |
template <typename T> |
227 | 227 |
struct SetPath |
228 |
: public Howard<GR, LEN, SetPathTraits<T> > { |
|
229 |
typedef Howard<GR, LEN, SetPathTraits<T> > Create; |
|
228 |
: public HowardMmc<GR, CM, SetPathTraits<T> > { |
|
229 |
typedef HowardMmc<GR, CM, SetPathTraits<T> > Create; |
|
230 | 230 |
}; |
231 | 231 |
|
... | ... |
@@ -234,5 +234,5 @@ |
234 | 234 |
protected: |
235 | 235 |
|
236 |
|
|
236 |
HowardMmc() {} |
|
237 | 237 |
|
238 | 238 |
public: |
... | ... |
@@ -243,18 +243,18 @@ |
243 | 243 |
/// |
244 | 244 |
/// \param digraph The digraph the algorithm runs on. |
245 |
/// \param length The lengths (costs) of the arcs. |
|
246 |
Howard( const Digraph &digraph, |
|
247 |
const LengthMap &length ) : |
|
248 |
_gr(digraph), _length(length), _best_found(false), |
|
249 |
|
|
245 |
/// \param cost The costs of the arcs. |
|
246 |
HowardMmc( const Digraph &digraph, |
|
247 |
const CostMap &cost ) : |
|
248 |
_gr(digraph), _cost(cost), _best_found(false), |
|
249 |
_best_cost(0), _best_size(1), _cycle_path(NULL), _local_path(false), |
|
250 | 250 |
_policy(digraph), _reached(digraph), _level(digraph), _dist(digraph), |
251 | 251 |
_comp(digraph), _in_arcs(digraph), |
252 |
INF(std::numeric_limits<LargeValue>::has_infinity ? |
|
253 |
std::numeric_limits<LargeValue>::infinity() : |
|
254 |
|
|
252 |
INF(std::numeric_limits<LargeCost>::has_infinity ? |
|
253 |
std::numeric_limits<LargeCost>::infinity() : |
|
254 |
std::numeric_limits<LargeCost>::max()) |
|
255 | 255 |
{} |
256 | 256 |
|
257 | 257 |
/// Destructor. |
258 |
~ |
|
258 |
~HowardMmc() { |
|
259 | 259 |
if (_local_path) delete _cycle_path; |
260 | 260 |
} |
... | ... |
@@ -266,5 +266,5 @@ |
266 | 266 |
/// |
267 | 267 |
/// If you don't call this function before calling \ref run() or |
268 |
/// \ref |
|
268 |
/// \ref findCycleMean(), it will allocate a local \ref Path "path" |
|
269 | 269 |
/// structure. The destuctor deallocates this automatically |
270 | 270 |
/// allocated object, of course. |
... | ... |
@@ -274,5 +274,5 @@ |
274 | 274 |
/// |
275 | 275 |
/// \return <tt>(*this)</tt> |
276 |
|
|
276 |
HowardMmc& cycle(Path &path) { |
|
277 | 277 |
if (_local_path) { |
278 | 278 |
delete _cycle_path; |
... | ... |
@@ -288,5 +288,5 @@ |
288 | 288 |
/// |
289 | 289 |
/// \return <tt>(*this)</tt> |
290 |
|
|
290 |
HowardMmc& tolerance(const Tolerance& tolerance) { |
|
291 | 291 |
_tolerance = tolerance; |
292 | 292 |
return *this; |
... | ... |
@@ -304,6 +304,6 @@ |
304 | 304 |
/// The simplest way to execute the algorithm is to call the \ref run() |
305 | 305 |
/// function.\n |
306 |
/// If you only need the minimum mean length, you may call |
|
307 |
/// \ref findMinMean(). |
|
306 |
/// If you only need the minimum mean cost, you may call |
|
307 |
/// \ref findCycleMean(). |
|
308 | 308 |
|
309 | 309 |
/// @{ |
... | ... |
@@ -313,5 +313,5 @@ |
313 | 313 |
/// This function runs the algorithm. |
314 | 314 |
/// It can be called more than once (e.g. if the underlying digraph |
315 |
/// and/or the arc |
|
315 |
/// and/or the arc costs have been modified). |
|
316 | 316 |
/// |
317 | 317 |
/// \return \c true if a directed cycle exists in the digraph. |
... | ... |
@@ -319,17 +319,17 @@ |
319 | 319 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
320 | 320 |
/// \code |
321 |
/// return mmc. |
|
321 |
/// return mmc.findCycleMean() && mmc.findCycle(); |
|
322 | 322 |
/// \endcode |
323 | 323 |
bool run() { |
324 |
return |
|
324 |
return findCycleMean() && findCycle(); |
|
325 | 325 |
} |
326 | 326 |
|
327 | 327 |
/// \brief Find the minimum cycle mean. |
328 | 328 |
/// |
329 |
/// This function finds the minimum mean |
|
329 |
/// This function finds the minimum mean cost of the directed |
|
330 | 330 |
/// cycles in the digraph. |
331 | 331 |
/// |
332 | 332 |
/// \return \c true if a directed cycle exists in the digraph. |
333 |
bool |
|
333 |
bool findCycleMean() { |
|
334 | 334 |
// Initialize and find strongly connected components |
335 | 335 |
init(); |
... | ... |
@@ -346,7 +346,7 @@ |
346 | 346 |
// Update the best cycle (global minimum mean cycle) |
347 | 347 |
if ( _curr_found && (!_best_found || |
348 |
|
|
348 |
_curr_cost * _best_size < _best_cost * _curr_size) ) { |
|
349 | 349 |
_best_found = true; |
350 |
|
|
350 |
_best_cost = _curr_cost; |
|
351 | 351 |
_best_size = _curr_size; |
352 | 352 |
_best_node = _curr_node; |
... | ... |
@@ -358,10 +358,10 @@ |
358 | 358 |
/// \brief Find a minimum mean directed cycle. |
359 | 359 |
/// |
360 |
/// This function finds a directed cycle of minimum mean length |
|
361 |
/// in the digraph using the data computed by findMinMean(). |
|
360 |
/// This function finds a directed cycle of minimum mean cost |
|
361 |
/// in the digraph using the data computed by findCycleMean(). |
|
362 | 362 |
/// |
363 | 363 |
/// \return \c true if a directed cycle exists in the digraph. |
364 | 364 |
/// |
365 |
/// \pre \ref |
|
365 |
/// \pre \ref findCycleMean() must be called before using this function. |
|
366 | 366 |
bool findCycle() { |
367 | 367 |
if (!_best_found) return false; |
... | ... |
@@ -383,12 +383,12 @@ |
383 | 383 |
/// @{ |
384 | 384 |
|
385 |
/// \brief Return the total |
|
385 |
/// \brief Return the total cost of the found cycle. |
|
386 | 386 |
/// |
387 |
/// This function returns the total |
|
387 |
/// This function returns the total cost of the found cycle. |
|
388 | 388 |
/// |
389 |
/// \pre \ref run() or \ref |
|
389 |
/// \pre \ref run() or \ref findCycleMean() must be called before |
|
390 | 390 |
/// using this function. |
391 |
Value cycleLength() const { |
|
392 |
return static_cast<Value>(_best_length); |
|
391 |
Cost cycleCost() const { |
|
392 |
return static_cast<Cost>(_best_cost); |
|
393 | 393 |
} |
394 | 394 |
|
... | ... |
@@ -397,24 +397,24 @@ |
397 | 397 |
/// This function returns the number of arcs on the found cycle. |
398 | 398 |
/// |
399 |
/// \pre \ref run() or \ref |
|
399 |
/// \pre \ref run() or \ref findCycleMean() must be called before |
|
400 | 400 |
/// using this function. |
401 |
int |
|
401 |
int cycleSize() const { |
|
402 | 402 |
return _best_size; |
403 | 403 |
} |
404 | 404 |
|
405 |
/// \brief Return the mean |
|
405 |
/// \brief Return the mean cost of the found cycle. |
|
406 | 406 |
/// |
407 |
/// This function returns the mean |
|
407 |
/// This function returns the mean cost of the found cycle. |
|
408 | 408 |
/// |
409 | 409 |
/// \note <tt>alg.cycleMean()</tt> is just a shortcut of the |
410 | 410 |
/// following code. |
411 | 411 |
/// \code |
412 |
/// return static_cast<double>(alg. |
|
412 |
/// return static_cast<double>(alg.cycleCost()) / alg.cycleSize(); |
|
413 | 413 |
/// \endcode |
414 | 414 |
/// |
415 |
/// \pre \ref run() or \ref |
|
415 |
/// \pre \ref run() or \ref findCycleMean() must be called before |
|
416 | 416 |
/// using this function. |
417 | 417 |
double cycleMean() const { |
418 |
return static_cast<double>( |
|
418 |
return static_cast<double>(_best_cost) / _best_size; |
|
419 | 419 |
} |
420 | 420 |
|
... | ... |
@@ -442,5 +442,5 @@ |
442 | 442 |
_queue.resize(countNodes(_gr)); |
443 | 443 |
_best_found = false; |
444 |
|
|
444 |
_best_cost = 0; |
|
445 | 445 |
_best_size = 1; |
446 | 446 |
_cycle_path->clear(); |
... | ... |
@@ -493,6 +493,6 @@ |
493 | 493 |
e = _in_arcs[v][j]; |
494 | 494 |
u = _gr.source(e); |
495 |
if (_length[e] < _dist[u]) { |
|
496 |
_dist[u] = _length[e]; |
|
495 |
if (_cost[e] < _dist[u]) { |
|
496 |
_dist[u] = _cost[e]; |
|
497 | 497 |
_policy[u] = e; |
498 | 498 |
} |
... | ... |
@@ -507,5 +507,5 @@ |
507 | 507 |
_level[(*_nodes)[i]] = -1; |
508 | 508 |
} |
509 |
|
|
509 |
LargeCost ccost; |
|
510 | 510 |
int csize; |
511 | 511 |
Node u, v; |
... | ... |
@@ -519,14 +519,14 @@ |
519 | 519 |
if (_level[u] == i) { |
520 | 520 |
// A cycle is found |
521 |
|
|
521 |
ccost = _cost[_policy[u]]; |
|
522 | 522 |
csize = 1; |
523 | 523 |
for (v = u; (v = _gr.target(_policy[v])) != u; ) { |
524 |
|
|
524 |
ccost += _cost[_policy[v]]; |
|
525 | 525 |
++csize; |
526 | 526 |
} |
527 | 527 |
if ( !_curr_found || |
528 |
( |
|
528 |
(ccost * _curr_size < _curr_cost * csize) ) { |
|
529 | 529 |
_curr_found = true; |
530 |
|
|
530 |
_curr_cost = ccost; |
|
531 | 531 |
_curr_size = csize; |
532 | 532 |
_curr_node = u; |
... | ... |
@@ -556,5 +556,5 @@ |
556 | 556 |
if (_policy[u] == e && !_reached[u]) { |
557 | 557 |
_reached[u] = true; |
558 |
_dist[u] = _dist[v] + |
|
558 |
_dist[u] = _dist[v] + _cost[e] * _curr_size - _curr_cost; |
|
559 | 559 |
_queue[++_qback] = u; |
560 | 560 |
} |
... | ... |
@@ -573,5 +573,5 @@ |
573 | 573 |
_reached[u] = true; |
574 | 574 |
_policy[u] = e; |
575 |
_dist[u] = _dist[v] + |
|
575 |
_dist[u] = _dist[v] + _cost[e] * _curr_size - _curr_cost; |
|
576 | 576 |
_queue[++_qback] = u; |
577 | 577 |
} |
... | ... |
@@ -586,5 +586,5 @@ |
586 | 586 |
e = _in_arcs[v][j]; |
587 | 587 |
u = _gr.source(e); |
588 |
|
|
588 |
LargeCost delta = _dist[v] + _cost[e] * _curr_size - _curr_cost; |
|
589 | 589 |
if (_tolerance.less(delta, _dist[u])) { |
590 | 590 |
_dist[u] = delta; |
... | ... |
@@ -597,5 +597,5 @@ |
597 | 597 |
} |
598 | 598 |
|
599 |
}; //class |
|
599 |
}; //class HowardMmc |
|
600 | 600 |
|
601 | 601 |
///@} |
... | ... |
@@ -603,3 +603,3 @@ |
603 | 603 |
} //namespace lemon |
604 | 604 |
|
605 |
#endif // |
|
605 |
#endif //LEMON_HOWARD_MMC_H |
... | ... |
@@ -17,6 +17,6 @@ |
17 | 17 |
*/ |
18 | 18 |
|
19 |
#ifndef LEMON_KARP_H |
|
20 |
#define LEMON_KARP_H |
|
19 |
#ifndef LEMON_KARP_MMC_H |
|
20 |
#define LEMON_KARP_MMC_H |
|
21 | 21 |
|
22 | 22 |
/// \ingroup min_mean_cycle |
... | ... |
@@ -34,35 +34,35 @@ |
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 |
/// \brief Default traits class of |
|
36 |
/// \brief Default traits class of KarpMmc class. |
|
37 | 37 |
/// |
38 |
/// Default traits class of |
|
38 |
/// Default traits class of KarpMmc class. |
|
39 | 39 |
/// \tparam GR The type of the digraph. |
40 |
/// \tparam |
|
40 |
/// \tparam CM The type of the cost map. |
|
41 | 41 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
42 | 42 |
#ifdef DOXYGEN |
43 |
template <typename GR, typename |
|
43 |
template <typename GR, typename CM> |
|
44 | 44 |
#else |
45 |
template <typename GR, typename LEN, |
|
46 |
bool integer = std::numeric_limits<typename LEN::Value>::is_integer> |
|
45 |
template <typename GR, typename CM, |
|
46 |
bool integer = std::numeric_limits<typename CM::Value>::is_integer> |
|
47 | 47 |
#endif |
48 |
struct |
|
48 |
struct KarpMmcDefaultTraits |
|
49 | 49 |
{ |
50 | 50 |
/// The type of the digraph |
51 | 51 |
typedef GR Digraph; |
52 |
/// The type of the length map |
|
53 |
typedef LEN LengthMap; |
|
54 |
/// The type of the arc lengths |
|
55 |
typedef typename LengthMap::Value Value; |
|
52 |
/// The type of the cost map |
|
53 |
typedef CM CostMap; |
|
54 |
/// The type of the arc costs |
|
55 |
typedef typename CostMap::Value Cost; |
|
56 | 56 |
|
57 |
/// \brief The large |
|
57 |
/// \brief The large cost type used for internal computations |
|
58 | 58 |
/// |
59 |
/// The large value type used for internal computations. |
|
60 |
/// It is \c long \c long if the \c Value type is integer, |
|
59 |
/// The large cost type used for internal computations. |
|
60 |
/// It is \c long \c long if the \c Cost type is integer, |
|
61 | 61 |
/// otherwise it is \c double. |
62 |
/// \c Value must be convertible to \c LargeValue. |
|
63 |
typedef double LargeValue; |
|
62 |
/// \c Cost must be convertible to \c LargeCost. |
|
63 |
typedef double LargeCost; |
|
64 | 64 |
|
65 | 65 |
/// The tolerance type used for internal computations |
66 |
typedef lemon::Tolerance< |
|
66 |
typedef lemon::Tolerance<LargeCost> Tolerance; |
|
67 | 67 |
|
68 | 68 |
/// \brief The path type of the found cycles |
... | ... |
@@ -74,17 +74,17 @@ |
74 | 74 |
}; |
75 | 75 |
|
76 |
// Default traits class for integer value types |
|
77 |
template <typename GR, typename LEN> |
|
78 |
|
|
76 |
// Default traits class for integer cost types |
|
77 |
template <typename GR, typename CM> |
|
78 |
struct KarpMmcDefaultTraits<GR, CM, true> |
|
79 | 79 |
{ |
80 | 80 |
typedef GR Digraph; |
81 |
typedef LEN LengthMap; |
|
82 |
typedef typename LengthMap::Value Value; |
|
81 |
typedef CM CostMap; |
|
82 |
typedef typename CostMap::Value Cost; |
|
83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
84 |
typedef long long |
|
84 |
typedef long long LargeCost; |
|
85 | 85 |
#else |
86 |
typedef long |
|
86 |
typedef long LargeCost; |
|
87 | 87 |
#endif |
88 |
typedef lemon::Tolerance< |
|
88 |
typedef lemon::Tolerance<LargeCost> Tolerance; |
|
89 | 89 |
typedef lemon::Path<Digraph> Path; |
90 | 90 |
}; |
... | ... |
@@ -98,24 +98,24 @@ |
98 | 98 |
/// |
99 | 99 |
/// This class implements Karp's algorithm for finding a directed |
100 |
/// cycle of minimum mean |
|
100 |
/// cycle of minimum mean cost in a digraph |
|
101 | 101 |
/// \ref amo93networkflows, \ref dasdan98minmeancycle. |
102 | 102 |
/// It runs in time O(ne) and uses space O(n<sup>2</sup>+e). |
103 | 103 |
/// |
104 | 104 |
/// \tparam GR The type of the digraph the algorithm runs on. |
105 |
/// \tparam |
|
105 |
/// \tparam CM The type of the cost map. The default |
|
106 | 106 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
107 | 107 |
/// \tparam TR The traits class that defines various types used by the |
108 |
/// algorithm. By default, it is \ref KarpDefaultTraits |
|
109 |
/// "KarpDefaultTraits<GR, LEN>". |
|
108 |
/// algorithm. By default, it is \ref KarpMmcDefaultTraits |
|
109 |
/// "KarpMmcDefaultTraits<GR, CM>". |
|
110 | 110 |
/// In most cases, this parameter should not be set directly, |
111 | 111 |
/// consider to use the named template parameters instead. |
112 | 112 |
#ifdef DOXYGEN |
113 |
template <typename GR, typename |
|
113 |
template <typename GR, typename CM, typename TR> |
|
114 | 114 |
#else |
115 | 115 |
template < typename GR, |
116 |
typename LEN = typename GR::template ArcMap<int>, |
|
117 |
typename TR = KarpDefaultTraits<GR, LEN> > |
|
116 |
typename CM = typename GR::template ArcMap<int>, |
|
117 |
typename TR = KarpMmcDefaultTraits<GR, CM> > |
|
118 | 118 |
#endif |
119 |
class |
|
119 |
class KarpMmc |
|
120 | 120 |
{ |
121 | 121 |
public: |
... | ... |
@@ -123,15 +123,15 @@ |
123 | 123 |
/// The type of the digraph |
124 | 124 |
typedef typename TR::Digraph Digraph; |
125 |
/// The type of the length map |
|
126 |
typedef typename TR::LengthMap LengthMap; |
|
127 |
/// The type of the arc lengths |
|
128 |
typedef typename TR::Value Value; |
|
125 |
/// The type of the cost map |
|
126 |
typedef typename TR::CostMap CostMap; |
|
127 |
/// The type of the arc costs |
|
128 |
typedef typename TR::Cost Cost; |
|
129 | 129 |
|
130 |
/// \brief The large |
|
130 |
/// \brief The large cost type |
|
131 | 131 |
/// |
132 |
/// The large value type used for internal computations. |
|
133 |
/// By default, it is \c long \c long if the \c Value type is integer, |
|
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 | 134 |
/// otherwise it is \c double. |
135 |
typedef typename TR:: |
|
135 |
typedef typename TR::LargeCost LargeCost; |
|
136 | 136 |
|
137 | 137 |
/// The tolerance type |
... | ... |
@@ -141,9 +141,9 @@ |
141 | 141 |
/// |
142 | 142 |
/// The path type of the found cycles. |
143 |
/// Using the \ref |
|
143 |
/// Using the \ref KarpMmcDefaultTraits "default traits class", |
|
144 | 144 |
/// it is \ref lemon::Path "Path<Digraph>". |
145 | 145 |
typedef typename TR::Path Path; |
146 | 146 |
|
147 |
/// The \ref |
|
147 |
/// The \ref KarpMmcDefaultTraits "traits class" of the algorithm |
|
148 | 148 |
typedef TR Traits; |
149 | 149 |
|
... | ... |
@@ -155,7 +155,7 @@ |
155 | 155 |
struct PathData |
156 | 156 |
{ |
157 |
|
|
157 |
LargeCost dist; |
|
158 | 158 |
Arc pred; |
159 |
PathData( |
|
159 |
PathData(LargeCost d, Arc p = INVALID) : |
|
160 | 160 |
dist(d), pred(p) {} |
161 | 161 |
}; |
... | ... |
@@ -168,6 +168,6 @@ |
168 | 168 |
// The digraph the algorithm runs on |
169 | 169 |
const Digraph &_gr; |
170 |
// The length of the arcs |
|
171 |
const LengthMap &_length; |
|
170 |
// The cost of the arcs |
|
171 |
const CostMap &_cost; |
|
172 | 172 |
|
173 | 173 |
// Data for storing the strongly connected components |
... | ... |
@@ -179,5 +179,5 @@ |
179 | 179 |
|
180 | 180 |
// Data for the found cycle |
181 |
|
|
181 |
LargeCost _cycle_cost; |
|
182 | 182 |
int _cycle_size; |
183 | 183 |
Node _cycle_node; |
... | ... |
@@ -194,5 +194,5 @@ |
194 | 194 |
|
195 | 195 |
// Infinite constant |
196 |
const |
|
196 |
const LargeCost INF; |
|
197 | 197 |
|
198 | 198 |
public: |
... | ... |
@@ -202,18 +202,18 @@ |
202 | 202 |
|
203 | 203 |
template <typename T> |
204 |
struct SetLargeValueTraits : public Traits { |
|
205 |
typedef T LargeValue; |
|
204 |
struct SetLargeCostTraits : public Traits { |
|
205 |
typedef T LargeCost; |
|
206 | 206 |
typedef lemon::Tolerance<T> Tolerance; |
207 | 207 |
}; |
208 | 208 |
|
209 | 209 |
/// \brief \ref named-templ-param "Named parameter" for setting |
210 |
/// \c |
|
210 |
/// \c LargeCost type. |
|
211 | 211 |
/// |
212 |
/// \ref named-templ-param "Named parameter" for setting \c |
|
212 |
/// \ref named-templ-param "Named parameter" for setting \c LargeCost |
|
213 | 213 |
/// type. It is used for internal computations in the algorithm. |
214 | 214 |
template <typename T> |
215 |
struct SetLargeValue |
|
216 |
: public Karp<GR, LEN, SetLargeValueTraits<T> > { |
|
217 |
|
|
215 |
struct SetLargeCost |
|
216 |
: public KarpMmc<GR, CM, SetLargeCostTraits<T> > { |
|
217 |
typedef KarpMmc<GR, CM, SetLargeCostTraits<T> > Create; |
|
218 | 218 |
}; |
219 | 219 |
|
... | ... |
@@ -232,6 +232,6 @@ |
232 | 232 |
template <typename T> |
233 | 233 |
struct SetPath |
234 |
: public Karp<GR, LEN, SetPathTraits<T> > { |
|
235 |
typedef Karp<GR, LEN, SetPathTraits<T> > Create; |
|
234 |
: public KarpMmc<GR, CM, SetPathTraits<T> > { |
|
235 |
typedef KarpMmc<GR, CM, SetPathTraits<T> > Create; |
|
236 | 236 |
}; |
237 | 237 |
|
... | ... |
@@ -240,5 +240,5 @@ |
240 | 240 |
protected: |
241 | 241 |
|
242 |
|
|
242 |
KarpMmc() {} |
|
243 | 243 |
|
244 | 244 |
public: |
... | ... |
@@ -249,17 +249,17 @@ |
249 | 249 |
/// |
250 | 250 |
/// \param digraph The digraph the algorithm runs on. |
251 |
/// \param length The lengths (costs) of the arcs. |
|
252 |
Karp( const Digraph &digraph, |
|
253 |
const LengthMap &length ) : |
|
254 |
_gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph), |
|
255 |
|
|
251 |
/// \param cost The costs of the arcs. |
|
252 |
KarpMmc( const Digraph &digraph, |
|
253 |
const CostMap &cost ) : |
|
254 |
_gr(digraph), _cost(cost), _comp(digraph), _out_arcs(digraph), |
|
255 |
_cycle_cost(0), _cycle_size(1), _cycle_node(INVALID), |
|
256 | 256 |
_cycle_path(NULL), _local_path(false), _data(digraph), |
257 |
INF(std::numeric_limits<LargeValue>::has_infinity ? |
|
258 |
std::numeric_limits<LargeValue>::infinity() : |
|
259 |
|
|
257 |
INF(std::numeric_limits<LargeCost>::has_infinity ? |
|
258 |
std::numeric_limits<LargeCost>::infinity() : |
|
259 |
std::numeric_limits<LargeCost>::max()) |
|
260 | 260 |
{} |
261 | 261 |
|
262 | 262 |
/// Destructor. |
263 |
~ |
|
263 |
~KarpMmc() { |
|
264 | 264 |
if (_local_path) delete _cycle_path; |
265 | 265 |
} |
... | ... |
@@ -271,5 +271,5 @@ |
271 | 271 |
/// |
272 | 272 |
/// If you don't call this function before calling \ref run() or |
273 |
/// \ref |
|
273 |
/// \ref findCycleMean(), it will allocate a local \ref Path "path" |
|
274 | 274 |
/// structure. The destuctor deallocates this automatically |
275 | 275 |
/// allocated object, of course. |
... | ... |
@@ -279,5 +279,5 @@ |
279 | 279 |
/// |
280 | 280 |
/// \return <tt>(*this)</tt> |
281 |
|
|
281 |
KarpMmc& cycle(Path &path) { |
|
282 | 282 |
if (_local_path) { |
283 | 283 |
delete _cycle_path; |
... | ... |
@@ -293,5 +293,5 @@ |
293 | 293 |
/// |
294 | 294 |
/// \return <tt>(*this)</tt> |
295 |
|
|
295 |
KarpMmc& tolerance(const Tolerance& tolerance) { |
|
296 | 296 |
_tolerance = tolerance; |
297 | 297 |
return *this; |
... | ... |
@@ -309,6 +309,6 @@ |
309 | 309 |
/// The simplest way to execute the algorithm is to call the \ref run() |
310 | 310 |
/// function.\n |
311 |
/// If you only need the minimum mean length, you may call |
|
312 |
/// \ref findMinMean(). |
|
311 |
/// If you only need the minimum mean cost, you may call |
|
312 |
/// \ref findCycleMean(). |
|
313 | 313 |
|
314 | 314 |
/// @{ |
... | ... |
@@ -318,5 +318,5 @@ |
318 | 318 |
/// This function runs the algorithm. |
319 | 319 |
/// It can be called more than once (e.g. if the underlying digraph |
320 |
/// and/or the arc |
|
320 |
/// and/or the arc costs have been modified). |
|
321 | 321 |
/// |
322 | 322 |
/// \return \c true if a directed cycle exists in the digraph. |
... | ... |
@@ -324,17 +324,17 @@ |
324 | 324 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
325 | 325 |
/// \code |
326 |
/// return mmc. |
|
326 |
/// return mmc.findCycleMean() && mmc.findCycle(); |
|
327 | 327 |
/// \endcode |
328 | 328 |
bool run() { |
329 |
return |
|
329 |
return findCycleMean() && findCycle(); |
|
330 | 330 |
} |
331 | 331 |
|
332 | 332 |
/// \brief Find the minimum cycle mean. |
333 | 333 |
/// |
334 |
/// This function finds the minimum mean |
|
334 |
/// This function finds the minimum mean cost of the directed |
|
335 | 335 |
/// cycles in the digraph. |
336 | 336 |
/// |
337 | 337 |
/// \return \c true if a directed cycle exists in the digraph. |
338 |
bool |
|
338 |
bool findCycleMean() { |
|
339 | 339 |
// Initialization and find strongly connected components |
340 | 340 |
init(); |
... | ... |
@@ -352,10 +352,10 @@ |
352 | 352 |
/// \brief Find a minimum mean directed cycle. |
353 | 353 |
/// |
354 |
/// This function finds a directed cycle of minimum mean length |
|
355 |
/// in the digraph using the data computed by findMinMean(). |
|
354 |
/// This function finds a directed cycle of minimum mean cost |
|
355 |
/// in the digraph using the data computed by findCycleMean(). |
|
356 | 356 |
/// |
357 | 357 |
/// \return \c true if a directed cycle exists in the digraph. |
358 | 358 |
/// |
359 |
/// \pre \ref |
|
359 |
/// \pre \ref findCycleMean() must be called before using this function. |
|
360 | 360 |
bool findCycle() { |
361 | 361 |
if (_cycle_node == INVALID) return false; |
... | ... |
@@ -370,5 +370,5 @@ |
370 | 370 |
Arc e = _data[u][r].pred; |
371 | 371 |
_cycle_path->addFront(e); |
372 |
|
|
372 |
_cycle_cost = _cost[e]; |
|
373 | 373 |
_cycle_size = 1; |
374 | 374 |
Node v; |
... | ... |
@@ -376,5 +376,5 @@ |
376 | 376 |
e = _data[v][--r].pred; |
377 | 377 |
_cycle_path->addFront(e); |
378 |
|
|
378 |
_cycle_cost += _cost[e]; |
|
379 | 379 |
++_cycle_size; |
380 | 380 |
} |
... | ... |
@@ -391,12 +391,12 @@ |
391 | 391 |
/// @{ |
392 | 392 |
|
393 |
/// \brief Return the total |
|
393 |
/// \brief Return the total cost of the found cycle. |
|
394 | 394 |
/// |
395 |
/// This function returns the total |
|
395 |
/// This function returns the total cost of the found cycle. |
|
396 | 396 |
/// |
397 |
/// \pre \ref run() or \ref |
|
397 |
/// \pre \ref run() or \ref findCycleMean() must be called before |
|
398 | 398 |
/// using this function. |
399 |
Value cycleLength() const { |
|
400 |
return static_cast<Value>(_cycle_length); |
|
399 |
Cost cycleCost() const { |
|
400 |
return static_cast<Cost>(_cycle_cost); |
|
401 | 401 |
} |
402 | 402 |
|
... | ... |
@@ -405,24 +405,24 @@ |
405 | 405 |
/// This function returns the number of arcs on the found cycle. |
406 | 406 |
/// |
407 |
/// \pre \ref run() or \ref |
|
407 |
/// \pre \ref run() or \ref findCycleMean() must be called before |
|
408 | 408 |
/// using this function. |
409 |
int |
|
409 |
int cycleSize() const { |
|
410 | 410 |
return _cycle_size; |
411 | 411 |
} |
412 | 412 |
|
413 |
/// \brief Return the mean |
|
413 |
/// \brief Return the mean cost of the found cycle. |
|
414 | 414 |
/// |
415 |
/// This function returns the mean |
|
415 |
/// This function returns the mean cost of the found cycle. |
|
416 | 416 |
/// |
417 | 417 |
/// \note <tt>alg.cycleMean()</tt> is just a shortcut of the |
418 | 418 |
/// following code. |
419 | 419 |
/// \code |
420 |
/// return static_cast<double>(alg. |
|
420 |
/// return static_cast<double>(alg.cycleCost()) / alg.cycleSize(); |
|
421 | 421 |
/// \endcode |
422 | 422 |
/// |
423 |
/// \pre \ref run() or \ref |
|
423 |
/// \pre \ref run() or \ref findCycleMean() must be called before |
|
424 | 424 |
/// using this function. |
425 | 425 |
double cycleMean() const { |
426 |
return static_cast<double>( |
|
426 |
return static_cast<double>(_cycle_cost) / _cycle_size; |
|
427 | 427 |
} |
428 | 428 |
|
... | ... |
@@ -449,5 +449,5 @@ |
449 | 449 |
} |
450 | 450 |
_cycle_path->clear(); |
451 |
|
|
451 |
_cycle_cost = 0; |
|
452 | 452 |
_cycle_size = 1; |
453 | 453 |
_cycle_node = INVALID; |
... | ... |
@@ -498,5 +498,5 @@ |
498 | 498 |
|
499 | 499 |
// Process all rounds of computing path data for the current component. |
500 |
// _data[v][k] is the |
|
500 |
// _data[v][k] is the cost of a shortest directed walk from the root |
|
501 | 501 |
// node to node v containing exactly k arcs. |
502 | 502 |
void processRounds() { |
... | ... |
@@ -520,5 +520,5 @@ |
520 | 520 |
Node u, v; |
521 | 521 |
Arc e; |
522 |
|
|
522 |
LargeCost d; |
|
523 | 523 |
for (int i = 0; i < int(_process.size()); ++i) { |
524 | 524 |
u = _process[i]; |
... | ... |
@@ -526,5 +526,5 @@ |
526 | 526 |
e = _out_arcs[u][j]; |
527 | 527 |
v = _gr.target(e); |
528 |
d = _data[u][k-1].dist + |
|
528 |
d = _data[u][k-1].dist + _cost[e]; |
|
529 | 529 |
if (_tolerance.less(d, _data[v][k].dist)) { |
530 | 530 |
if (_data[v][k].dist == INF) next.push_back(v); |
... | ... |
@@ -540,5 +540,5 @@ |
540 | 540 |
Node u, v; |
541 | 541 |
Arc e; |
542 |
|
|
542 |
LargeCost d; |
|
543 | 543 |
for (int i = 0; i < int(_nodes->size()); ++i) { |
544 | 544 |
u = (*_nodes)[i]; |
... | ... |
@@ -546,5 +546,5 @@ |
546 | 546 |
e = _out_arcs[u][j]; |
547 | 547 |
v = _gr.target(e); |
548 |
d = _data[u][k-1].dist + |
|
548 |
d = _data[u][k-1].dist + _cost[e]; |
|
549 | 549 |
if (_tolerance.less(d, _data[v][k].dist)) { |
550 | 550 |
_data[v][k] = PathData(d, e); |
... | ... |
@@ -560,20 +560,20 @@ |
560 | 560 |
Node u = (*_nodes)[i]; |
561 | 561 |
if (_data[u][n].dist == INF) continue; |
562 |
|
|
562 |
LargeCost cost, max_cost = 0; |
|
563 | 563 |
int size, max_size = 1; |
564 | 564 |
bool found_curr = false; |
565 | 565 |
for (int k = 0; k < n; ++k) { |
566 | 566 |
if (_data[u][k].dist == INF) continue; |
567 |
|
|
567 |
cost = _data[u][n].dist - _data[u][k].dist; |
|
568 | 568 |
size = n - k; |
569 |
if (!found_curr || |
|
569 |
if (!found_curr || cost * max_size > max_cost * size) { |
|
570 | 570 |
found_curr = true; |
571 |
|
|
571 |
max_cost = cost; |
|
572 | 572 |
max_size = size; |
573 | 573 |
} |
574 | 574 |
} |
575 | 575 |
if ( found_curr && (_cycle_node == INVALID || |
576 |
max_length * _cycle_size < _cycle_length * max_size) ) { |
|
577 |
_cycle_length = max_length; |
|
576 |
max_cost * _cycle_size < _cycle_cost * max_size) ) { |
|
577 |
_cycle_cost = max_cost; |
|
578 | 578 |
_cycle_size = max_size; |
579 | 579 |
_cycle_node = u; |
... | ... |
@@ -582,5 +582,5 @@ |
582 | 582 |
} |
583 | 583 |
|
584 |
}; //class |
|
584 |
}; //class KarpMmc |
|
585 | 585 |
|
586 | 586 |
///@} |
... | ... |
@@ -588,3 +588,3 @@ |
588 | 588 |
} //namespace lemon |
589 | 589 |
|
590 |
#endif // |
|
590 |
#endif //LEMON_KARP_MMC_H |
... | ... |
@@ -26,7 +26,7 @@ |
26 | 26 |
#include <lemon/concept_check.h> |
27 | 27 |
|
28 |
#include <lemon/karp.h> |
|
29 |
#include <lemon/hartmann_orlin.h> |
|
30 |
#include <lemon/ |
|
28 |
#include <lemon/karp_mmc.h> |
|
29 |
#include <lemon/hartmann_orlin_mmc.h> |
|
30 |
#include <lemon/howard_mmc.h> |
|
31 | 31 |
|
32 | 32 |
#include "test_tools.h" |
... | ... |
@@ -64,5 +64,5 @@ |
64 | 64 |
|
65 | 65 |
// Check the interface of an MMC algorithm |
66 |
template <typename GR, typename |
|
66 |
template <typename GR, typename Cost> |
|
67 | 67 |
struct MmcClassConcept |
68 | 68 |
{ |
... | ... |
@@ -74,7 +74,7 @@ |
74 | 74 |
typedef typename MMC |
75 | 75 |
::template SetPath<ListPath<GR> > |
76 |
::template |
|
76 |
::template SetLargeCost<Cost> |
|
77 | 77 |
::Create MmcAlg; |
78 |
MmcAlg mmc(me.g, me. |
|
78 |
MmcAlg mmc(me.g, me.cost); |
|
79 | 79 |
const MmcAlg& const_mmc = mmc; |
80 | 80 |
|
... | ... |
@@ -83,19 +83,19 @@ |
83 | 83 |
|
84 | 84 |
b = mmc.cycle(p).run(); |
85 |
b = mmc. |
|
85 |
b = mmc.findCycleMean(); |
|
86 | 86 |
b = mmc.findCycle(); |
87 | 87 |
|
88 |
v = const_mmc.cycleLength(); |
|
89 |
i = const_mmc.cycleArcNum(); |
|
88 |
v = const_mmc.cycleCost(); |
|
89 |
i = const_mmc.cycleSize(); |
|
90 | 90 |
d = const_mmc.cycleMean(); |
91 | 91 |
p = const_mmc.cycle(); |
92 | 92 |
} |
93 | 93 |
|
94 |
typedef concepts::ReadMap<typename GR::Arc, |
|
94 |
typedef concepts::ReadMap<typename GR::Arc, Cost> CM; |
|
95 | 95 |
|
96 | 96 |
GR g; |
97 |
|
|
97 |
CM cost; |
|
98 | 98 |
ListPath<GR> p; |
99 |
|
|
99 |
Cost v; |
|
100 | 100 |
int i; |
101 | 101 |
double d; |
... | ... |
@@ -109,11 +109,11 @@ |
109 | 109 |
const SmartDigraph::ArcMap<int>& lm, |
110 | 110 |
const SmartDigraph::ArcMap<int>& cm, |
111 |
int |
|
111 |
int cost, int size) { |
|
112 | 112 |
MMC alg(gr, lm); |
113 |
alg.findMinMean(); |
|
114 |
check(alg.cycleMean() == static_cast<double>(length) / size, |
|
113 |
alg.findCycleMean(); |
|
114 |
check(alg.cycleMean() == static_cast<double>(cost) / size, |
|
115 | 115 |
"Wrong cycle mean"); |
116 | 116 |
alg.findCycle(); |
117 |
check(alg. |
|
117 |
check(alg.cycleCost() == cost && alg.cycleSize() == size, |
|
118 | 118 |
"Wrong path"); |
119 | 119 |
SmartDigraph::ArcMap<int> cycle(gr, 0); |
... | ... |
@@ -149,26 +149,26 @@ |
149 | 149 |
typedef concepts::Digraph GR; |
150 | 150 |
|
151 |
// |
|
151 |
// KarpMmc |
|
152 | 152 |
checkConcept< MmcClassConcept<GR, int>, |
153 |
|
|
153 |
KarpMmc<GR, concepts::ReadMap<GR::Arc, int> > >(); |
|
154 | 154 |
checkConcept< MmcClassConcept<GR, float>, |
155 |
|
|
155 |
KarpMmc<GR, concepts::ReadMap<GR::Arc, float> > >(); |
|
156 | 156 |
|
157 |
// |
|
157 |
// HartmannOrlinMmc |
|
158 | 158 |
checkConcept< MmcClassConcept<GR, int>, |
159 |
|
|
159 |
HartmannOrlinMmc<GR, concepts::ReadMap<GR::Arc, int> > >(); |
|
160 | 160 |
checkConcept< MmcClassConcept<GR, float>, |
161 |
|
|
161 |
HartmannOrlinMmc<GR, concepts::ReadMap<GR::Arc, float> > >(); |
|
162 | 162 |
|
163 |
// |
|
163 |
// HowardMmc |
|
164 | 164 |
checkConcept< MmcClassConcept<GR, int>, |
165 |
|
|
165 |
HowardMmc<GR, concepts::ReadMap<GR::Arc, int> > >(); |
|
166 | 166 |
checkConcept< MmcClassConcept<GR, float>, |
167 |
|
|
167 |
HowardMmc<GR, concepts::ReadMap<GR::Arc, float> > >(); |
|
168 | 168 |
|
169 |
if (IsSameType<Howard<GR, concepts::ReadMap<GR::Arc, int> >::LargeValue, |
|
170 |
long_int>::result == 0) check(false, "Wrong LargeValue type"); |
|
171 |
if (IsSameType<Howard<GR, concepts::ReadMap<GR::Arc, float> >::LargeValue, |
|
172 |
double>::result == 0) check(false, "Wrong LargeValue type"); |
|
169 |
check((IsSameType<HowardMmc<GR, concepts::ReadMap<GR::Arc, int> > |
|
170 |
::LargeCost, long_int>::result == 1), "Wrong LargeCost type"); |
|
171 |
check((IsSameType<HowardMmc<GR, concepts::ReadMap<GR::Arc, float> > |
|
172 |
::LargeCost, double>::result == 1), "Wrong LargeCost type"); |
|
173 | 173 |
} |
174 | 174 |
|
... | ... |
@@ -195,20 +195,20 @@ |
195 | 195 |
|
196 | 196 |
// Karp |
197 |
checkMmcAlg<Karp<GR, IntArcMap> >(gr, l1, c1, 6, 3); |
|
198 |
checkMmcAlg<Karp<GR, IntArcMap> >(gr, l2, c2, 5, 2); |
|
199 |
checkMmcAlg<Karp<GR, IntArcMap> >(gr, l3, c3, 0, 1); |
|
200 |
checkMmcAlg<Karp<GR, IntArcMap> >(gr, l4, c4, -1, 1); |
|
197 |
checkMmcAlg<KarpMmc<GR, IntArcMap> >(gr, l1, c1, 6, 3); |
|
198 |
checkMmcAlg<KarpMmc<GR, IntArcMap> >(gr, l2, c2, 5, 2); |
|
199 |
checkMmcAlg<KarpMmc<GR, IntArcMap> >(gr, l3, c3, 0, 1); |
|
200 |
checkMmcAlg<KarpMmc<GR, IntArcMap> >(gr, l4, c4, -1, 1); |
|
201 | 201 |
|
202 | 202 |
// HartmannOrlin |
203 |
checkMmcAlg<HartmannOrlin<GR, IntArcMap> >(gr, l1, c1, 6, 3); |
|
204 |
checkMmcAlg<HartmannOrlin<GR, IntArcMap> >(gr, l2, c2, 5, 2); |
|
205 |
checkMmcAlg<HartmannOrlin<GR, IntArcMap> >(gr, l3, c3, 0, 1); |
|
206 |
checkMmcAlg<HartmannOrlin<GR, IntArcMap> >(gr, l4, c4, -1, 1); |
|
203 |
checkMmcAlg<HartmannOrlinMmc<GR, IntArcMap> >(gr, l1, c1, 6, 3); |
|
204 |
checkMmcAlg<HartmannOrlinMmc<GR, IntArcMap> >(gr, l2, c2, 5, 2); |
|
205 |
checkMmcAlg<HartmannOrlinMmc<GR, IntArcMap> >(gr, l3, c3, 0, 1); |
|
206 |
checkMmcAlg<HartmannOrlinMmc<GR, IntArcMap> >(gr, l4, c4, -1, 1); |
|
207 | 207 |
|
208 | 208 |
// Howard |
209 |
checkMmcAlg<Howard<GR, IntArcMap> >(gr, l1, c1, 6, 3); |
|
210 |
checkMmcAlg<Howard<GR, IntArcMap> >(gr, l2, c2, 5, 2); |
|
211 |
checkMmcAlg<Howard<GR, IntArcMap> >(gr, l3, c3, 0, 1); |
|
212 |
checkMmcAlg<Howard<GR, IntArcMap> >(gr, l4, c4, -1, 1); |
|
209 |
checkMmcAlg<HowardMmc<GR, IntArcMap> >(gr, l1, c1, 6, 3); |
|
210 |
checkMmcAlg<HowardMmc<GR, IntArcMap> >(gr, l2, c2, 5, 2); |
|
211 |
checkMmcAlg<HowardMmc<GR, IntArcMap> >(gr, l3, c3, 0, 1); |
|
212 |
checkMmcAlg<HowardMmc<GR, IntArcMap> >(gr, l4, c4, -1, 1); |
|
213 | 213 |
} |
214 | 214 |
|
0 comments (0 inline)