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