| ... | ... |
@@ -217,424 +217,426 @@ |
| 217 | 217 |
typedef HartmannOrlin<GR, LEN, SetLargeValueTraits<T> > Create; |
| 218 | 218 |
}; |
| 219 | 219 |
|
| 220 | 220 |
template <typename T> |
| 221 | 221 |
struct SetPathTraits : public Traits {
|
| 222 | 222 |
typedef T Path; |
| 223 | 223 |
}; |
| 224 | 224 |
|
| 225 | 225 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 226 | 226 |
/// \c %Path type. |
| 227 | 227 |
/// |
| 228 | 228 |
/// \ref named-templ-param "Named parameter" for setting the \c %Path |
| 229 | 229 |
/// type of the found cycles. |
| 230 | 230 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 231 | 231 |
/// and it must have an \c addFront() function. |
| 232 | 232 |
template <typename T> |
| 233 | 233 |
struct SetPath |
| 234 | 234 |
: public HartmannOrlin<GR, LEN, SetPathTraits<T> > {
|
| 235 | 235 |
typedef HartmannOrlin<GR, LEN, SetPathTraits<T> > Create; |
| 236 | 236 |
}; |
| 237 | 237 |
|
| 238 | 238 |
/// @} |
| 239 | 239 |
|
| 240 | 240 |
public: |
| 241 | 241 |
|
| 242 | 242 |
/// \brief Constructor. |
| 243 | 243 |
/// |
| 244 | 244 |
/// The constructor of the class. |
| 245 | 245 |
/// |
| 246 | 246 |
/// \param digraph The digraph the algorithm runs on. |
| 247 | 247 |
/// \param length The lengths (costs) of the arcs. |
| 248 | 248 |
HartmannOrlin( const Digraph &digraph, |
| 249 | 249 |
const LengthMap &length ) : |
| 250 | 250 |
_gr(digraph), _length(length), _comp(digraph), _out_arcs(digraph), |
| 251 | 251 |
_best_found(false), _best_length(0), _best_size(1), |
| 252 | 252 |
_cycle_path(NULL), _local_path(false), _data(digraph), |
| 253 | 253 |
INF(std::numeric_limits<LargeValue>::has_infinity ? |
| 254 | 254 |
std::numeric_limits<LargeValue>::infinity() : |
| 255 | 255 |
std::numeric_limits<LargeValue>::max()) |
| 256 | 256 |
{}
|
| 257 | 257 |
|
| 258 | 258 |
/// Destructor. |
| 259 | 259 |
~HartmannOrlin() {
|
| 260 | 260 |
if (_local_path) delete _cycle_path; |
| 261 | 261 |
} |
| 262 | 262 |
|
| 263 | 263 |
/// \brief Set the path structure for storing the found cycle. |
| 264 | 264 |
/// |
| 265 | 265 |
/// This function sets an external path structure for storing the |
| 266 | 266 |
/// found cycle. |
| 267 | 267 |
/// |
| 268 | 268 |
/// If you don't call this function before calling \ref run() or |
| 269 | 269 |
/// \ref findMinMean(), it will allocate a local \ref Path "path" |
| 270 | 270 |
/// structure. The destuctor deallocates this automatically |
| 271 | 271 |
/// allocated object, of course. |
| 272 | 272 |
/// |
| 273 | 273 |
/// \note The algorithm calls only the \ref lemon::Path::addFront() |
| 274 | 274 |
/// "addFront()" function of the given path structure. |
| 275 | 275 |
/// |
| 276 | 276 |
/// \return <tt>(*this)</tt> |
| 277 | 277 |
HartmannOrlin& cycle(Path &path) {
|
| 278 | 278 |
if (_local_path) {
|
| 279 | 279 |
delete _cycle_path; |
| 280 | 280 |
_local_path = false; |
| 281 | 281 |
} |
| 282 | 282 |
_cycle_path = &path; |
| 283 | 283 |
return *this; |
| 284 | 284 |
} |
| 285 | 285 |
|
| 286 | 286 |
/// \brief Set the tolerance used by the algorithm. |
| 287 | 287 |
/// |
| 288 | 288 |
/// This function sets the tolerance object used by the algorithm. |
| 289 | 289 |
/// |
| 290 | 290 |
/// \return <tt>(*this)</tt> |
| 291 | 291 |
HartmannOrlin& tolerance(const Tolerance& tolerance) {
|
| 292 | 292 |
_tolerance = tolerance; |
| 293 | 293 |
return *this; |
| 294 | 294 |
} |
| 295 | 295 |
|
| 296 | 296 |
/// \brief Return a const reference to the tolerance. |
| 297 | 297 |
/// |
| 298 | 298 |
/// This function returns a const reference to the tolerance object |
| 299 | 299 |
/// used by the algorithm. |
| 300 | 300 |
const Tolerance& tolerance() const {
|
| 301 | 301 |
return _tolerance; |
| 302 | 302 |
} |
| 303 | 303 |
|
| 304 | 304 |
/// \name Execution control |
| 305 | 305 |
/// The simplest way to execute the algorithm is to call the \ref run() |
| 306 | 306 |
/// function.\n |
| 307 | 307 |
/// If you only need the minimum mean length, you may call |
| 308 | 308 |
/// \ref findMinMean(). |
| 309 | 309 |
|
| 310 | 310 |
/// @{
|
| 311 | 311 |
|
| 312 | 312 |
/// \brief Run the algorithm. |
| 313 | 313 |
/// |
| 314 | 314 |
/// This function runs the algorithm. |
| 315 | 315 |
/// It can be called more than once (e.g. if the underlying digraph |
| 316 | 316 |
/// and/or the arc lengths have been modified). |
| 317 | 317 |
/// |
| 318 | 318 |
/// \return \c true if a directed cycle exists in the digraph. |
| 319 | 319 |
/// |
| 320 | 320 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
| 321 | 321 |
/// \code |
| 322 | 322 |
/// return mmc.findMinMean() && mmc.findCycle(); |
| 323 | 323 |
/// \endcode |
| 324 | 324 |
bool run() {
|
| 325 | 325 |
return findMinMean() && findCycle(); |
| 326 | 326 |
} |
| 327 | 327 |
|
| 328 | 328 |
/// \brief Find the minimum cycle mean. |
| 329 | 329 |
/// |
| 330 | 330 |
/// This function finds the minimum mean length of the directed |
| 331 | 331 |
/// cycles in the digraph. |
| 332 | 332 |
/// |
| 333 | 333 |
/// \return \c true if a directed cycle exists in the digraph. |
| 334 | 334 |
bool findMinMean() {
|
| 335 | 335 |
// Initialization and find strongly connected components |
| 336 | 336 |
init(); |
| 337 | 337 |
findComponents(); |
| 338 | 338 |
|
| 339 | 339 |
// Find the minimum cycle mean in the components |
| 340 | 340 |
for (int comp = 0; comp < _comp_num; ++comp) {
|
| 341 | 341 |
if (!initComponent(comp)) continue; |
| 342 | 342 |
processRounds(); |
| 343 | 343 |
|
| 344 | 344 |
// Update the best cycle (global minimum mean cycle) |
| 345 | 345 |
if ( _curr_found && (!_best_found || |
| 346 | 346 |
_curr_length * _best_size < _best_length * _curr_size) ) {
|
| 347 | 347 |
_best_found = true; |
| 348 | 348 |
_best_length = _curr_length; |
| 349 | 349 |
_best_size = _curr_size; |
| 350 | 350 |
_best_node = _curr_node; |
| 351 | 351 |
_best_level = _curr_level; |
| 352 | 352 |
} |
| 353 | 353 |
} |
| 354 | 354 |
return _best_found; |
| 355 | 355 |
} |
| 356 | 356 |
|
| 357 | 357 |
/// \brief Find a minimum mean directed cycle. |
| 358 | 358 |
/// |
| 359 | 359 |
/// This function finds a directed cycle of minimum mean length |
| 360 | 360 |
/// in the digraph using the data computed by findMinMean(). |
| 361 | 361 |
/// |
| 362 | 362 |
/// \return \c true if a directed cycle exists in the digraph. |
| 363 | 363 |
/// |
| 364 | 364 |
/// \pre \ref findMinMean() must be called before using this function. |
| 365 | 365 |
bool findCycle() {
|
| 366 | 366 |
if (!_best_found) return false; |
| 367 | 367 |
IntNodeMap reached(_gr, -1); |
| 368 | 368 |
int r = _best_level + 1; |
| 369 | 369 |
Node u = _best_node; |
| 370 | 370 |
while (reached[u] < 0) {
|
| 371 | 371 |
reached[u] = --r; |
| 372 | 372 |
u = _gr.source(_data[u][r].pred); |
| 373 | 373 |
} |
| 374 | 374 |
r = reached[u]; |
| 375 | 375 |
Arc e = _data[u][r].pred; |
| 376 | 376 |
_cycle_path->addFront(e); |
| 377 | 377 |
_best_length = _length[e]; |
| 378 | 378 |
_best_size = 1; |
| 379 | 379 |
Node v; |
| 380 | 380 |
while ((v = _gr.source(e)) != u) {
|
| 381 | 381 |
e = _data[v][--r].pred; |
| 382 | 382 |
_cycle_path->addFront(e); |
| 383 | 383 |
_best_length += _length[e]; |
| 384 | 384 |
++_best_size; |
| 385 | 385 |
} |
| 386 | 386 |
return true; |
| 387 | 387 |
} |
| 388 | 388 |
|
| 389 | 389 |
/// @} |
| 390 | 390 |
|
| 391 | 391 |
/// \name Query Functions |
| 392 | 392 |
/// The results of the algorithm can be obtained using these |
| 393 | 393 |
/// functions.\n |
| 394 | 394 |
/// The algorithm should be executed before using them. |
| 395 | 395 |
|
| 396 | 396 |
/// @{
|
| 397 | 397 |
|
| 398 | 398 |
/// \brief Return the total length of the found cycle. |
| 399 | 399 |
/// |
| 400 | 400 |
/// This function returns the total length of the found cycle. |
| 401 | 401 |
/// |
| 402 | 402 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 403 | 403 |
/// using this function. |
| 404 | 404 |
LargeValue cycleLength() const {
|
| 405 | 405 |
return _best_length; |
| 406 | 406 |
} |
| 407 | 407 |
|
| 408 | 408 |
/// \brief Return the number of arcs on the found cycle. |
| 409 | 409 |
/// |
| 410 | 410 |
/// This function returns the number of arcs on the found cycle. |
| 411 | 411 |
/// |
| 412 | 412 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 413 | 413 |
/// using this function. |
| 414 | 414 |
int cycleArcNum() const {
|
| 415 | 415 |
return _best_size; |
| 416 | 416 |
} |
| 417 | 417 |
|
| 418 | 418 |
/// \brief Return the mean length of the found cycle. |
| 419 | 419 |
/// |
| 420 | 420 |
/// This function returns the mean length of the found cycle. |
| 421 | 421 |
/// |
| 422 | 422 |
/// \note <tt>alg.cycleMean()</tt> is just a shortcut of the |
| 423 | 423 |
/// following code. |
| 424 | 424 |
/// \code |
| 425 | 425 |
/// return static_cast<double>(alg.cycleLength()) / alg.cycleArcNum(); |
| 426 | 426 |
/// \endcode |
| 427 | 427 |
/// |
| 428 | 428 |
/// \pre \ref run() or \ref findMinMean() must be called before |
| 429 | 429 |
/// using this function. |
| 430 | 430 |
double cycleMean() const {
|
| 431 | 431 |
return static_cast<double>(_best_length) / _best_size; |
| 432 | 432 |
} |
| 433 | 433 |
|
| 434 | 434 |
/// \brief Return the found cycle. |
| 435 | 435 |
/// |
| 436 | 436 |
/// This function returns a const reference to the path structure |
| 437 | 437 |
/// storing the found cycle. |
| 438 | 438 |
/// |
| 439 | 439 |
/// \pre \ref run() or \ref findCycle() must be called before using |
| 440 | 440 |
/// this function. |
| 441 | 441 |
const Path& cycle() const {
|
| 442 | 442 |
return *_cycle_path; |
| 443 | 443 |
} |
| 444 | 444 |
|
| 445 | 445 |
///@} |
| 446 | 446 |
|
| 447 | 447 |
private: |
| 448 | 448 |
|
| 449 | 449 |
// Initialization |
| 450 | 450 |
void init() {
|
| 451 | 451 |
if (!_cycle_path) {
|
| 452 | 452 |
_local_path = true; |
| 453 | 453 |
_cycle_path = new Path; |
| 454 | 454 |
} |
| 455 | 455 |
_cycle_path->clear(); |
| 456 | 456 |
_best_found = false; |
| 457 | 457 |
_best_length = 0; |
| 458 | 458 |
_best_size = 1; |
| 459 | 459 |
_cycle_path->clear(); |
| 460 | 460 |
for (NodeIt u(_gr); u != INVALID; ++u) |
| 461 | 461 |
_data[u].clear(); |
| 462 | 462 |
} |
| 463 | 463 |
|
| 464 | 464 |
// Find strongly connected components and initialize _comp_nodes |
| 465 | 465 |
// and _out_arcs |
| 466 | 466 |
void findComponents() {
|
| 467 | 467 |
_comp_num = stronglyConnectedComponents(_gr, _comp); |
| 468 | 468 |
_comp_nodes.resize(_comp_num); |
| 469 | 469 |
if (_comp_num == 1) {
|
| 470 | 470 |
_comp_nodes[0].clear(); |
| 471 | 471 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
| 472 | 472 |
_comp_nodes[0].push_back(n); |
| 473 | 473 |
_out_arcs[n].clear(); |
| 474 | 474 |
for (OutArcIt a(_gr, n); a != INVALID; ++a) {
|
| 475 | 475 |
_out_arcs[n].push_back(a); |
| 476 | 476 |
} |
| 477 | 477 |
} |
| 478 | 478 |
} else {
|
| 479 | 479 |
for (int i = 0; i < _comp_num; ++i) |
| 480 | 480 |
_comp_nodes[i].clear(); |
| 481 | 481 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
| 482 | 482 |
int k = _comp[n]; |
| 483 | 483 |
_comp_nodes[k].push_back(n); |
| 484 | 484 |
_out_arcs[n].clear(); |
| 485 | 485 |
for (OutArcIt a(_gr, n); a != INVALID; ++a) {
|
| 486 | 486 |
if (_comp[_gr.target(a)] == k) _out_arcs[n].push_back(a); |
| 487 | 487 |
} |
| 488 | 488 |
} |
| 489 | 489 |
} |
| 490 | 490 |
} |
| 491 | 491 |
|
| 492 | 492 |
// Initialize path data for the current component |
| 493 | 493 |
bool initComponent(int comp) {
|
| 494 | 494 |
_nodes = &(_comp_nodes[comp]); |
| 495 | 495 |
int n = _nodes->size(); |
| 496 | 496 |
if (n < 1 || (n == 1 && _out_arcs[(*_nodes)[0]].size() == 0)) {
|
| 497 | 497 |
return false; |
| 498 | 498 |
} |
| 499 | 499 |
for (int i = 0; i < n; ++i) {
|
| 500 | 500 |
_data[(*_nodes)[i]].resize(n + 1, PathData(INF)); |
| 501 | 501 |
} |
| 502 | 502 |
return true; |
| 503 | 503 |
} |
| 504 | 504 |
|
| 505 | 505 |
// Process all rounds of computing path data for the current component. |
| 506 | 506 |
// _data[v][k] is the length of a shortest directed walk from the root |
| 507 | 507 |
// node to node v containing exactly k arcs. |
| 508 | 508 |
void processRounds() {
|
| 509 | 509 |
Node start = (*_nodes)[0]; |
| 510 | 510 |
_data[start][0] = PathData(0); |
| 511 | 511 |
_process.clear(); |
| 512 | 512 |
_process.push_back(start); |
| 513 | 513 |
|
| 514 | 514 |
int k, n = _nodes->size(); |
| 515 | 515 |
int next_check = 4; |
| 516 | 516 |
bool terminate = false; |
| 517 | 517 |
for (k = 1; k <= n && int(_process.size()) < n && !terminate; ++k) {
|
| 518 | 518 |
processNextBuildRound(k); |
| 519 | 519 |
if (k == next_check || k == n) {
|
| 520 | 520 |
terminate = checkTermination(k); |
| 521 | 521 |
next_check = next_check * 3 / 2; |
| 522 | 522 |
} |
| 523 | 523 |
} |
| 524 | 524 |
for ( ; k <= n && !terminate; ++k) {
|
| 525 | 525 |
processNextFullRound(k); |
| 526 | 526 |
if (k == next_check || k == n) {
|
| 527 | 527 |
terminate = checkTermination(k); |
| 528 | 528 |
next_check = next_check * 3 / 2; |
| 529 | 529 |
} |
| 530 | 530 |
} |
| 531 | 531 |
} |
| 532 | 532 |
|
| 533 | 533 |
// Process one round and rebuild _process |
| 534 | 534 |
void processNextBuildRound(int k) {
|
| 535 | 535 |
std::vector<Node> next; |
| 536 | 536 |
Node u, v; |
| 537 | 537 |
Arc e; |
| 538 | 538 |
LargeValue d; |
| 539 | 539 |
for (int i = 0; i < int(_process.size()); ++i) {
|
| 540 | 540 |
u = _process[i]; |
| 541 | 541 |
for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
|
| 542 | 542 |
e = _out_arcs[u][j]; |
| 543 | 543 |
v = _gr.target(e); |
| 544 | 544 |
d = _data[u][k-1].dist + _length[e]; |
| 545 | 545 |
if (_tolerance.less(d, _data[v][k].dist)) {
|
| 546 | 546 |
if (_data[v][k].dist == INF) next.push_back(v); |
| 547 | 547 |
_data[v][k] = PathData(d, e); |
| 548 | 548 |
} |
| 549 | 549 |
} |
| 550 | 550 |
} |
| 551 | 551 |
_process.swap(next); |
| 552 | 552 |
} |
| 553 | 553 |
|
| 554 | 554 |
// Process one round using _nodes instead of _process |
| 555 | 555 |
void processNextFullRound(int k) {
|
| 556 | 556 |
Node u, v; |
| 557 | 557 |
Arc e; |
| 558 | 558 |
LargeValue d; |
| 559 | 559 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
| 560 | 560 |
u = (*_nodes)[i]; |
| 561 | 561 |
for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
|
| 562 | 562 |
e = _out_arcs[u][j]; |
| 563 | 563 |
v = _gr.target(e); |
| 564 | 564 |
d = _data[u][k-1].dist + _length[e]; |
| 565 | 565 |
if (_tolerance.less(d, _data[v][k].dist)) {
|
| 566 | 566 |
_data[v][k] = PathData(d, e); |
| 567 | 567 |
} |
| 568 | 568 |
} |
| 569 | 569 |
} |
| 570 | 570 |
} |
| 571 | 571 |
|
| 572 | 572 |
// Check early termination |
| 573 | 573 |
bool checkTermination(int k) {
|
| 574 | 574 |
typedef std::pair<int, int> Pair; |
| 575 | 575 |
typename GR::template NodeMap<Pair> level(_gr, Pair(-1, 0)); |
| 576 | 576 |
typename GR::template NodeMap<LargeValue> pi(_gr); |
| 577 | 577 |
int n = _nodes->size(); |
| 578 | 578 |
LargeValue length; |
| 579 | 579 |
int size; |
| 580 | 580 |
Node u; |
| 581 | 581 |
|
| 582 | 582 |
// Search for cycles that are already found |
| 583 | 583 |
_curr_found = false; |
| 584 | 584 |
for (int i = 0; i < n; ++i) {
|
| 585 | 585 |
u = (*_nodes)[i]; |
| 586 | 586 |
if (_data[u][k].dist == INF) continue; |
| 587 | 587 |
for (int j = k; j >= 0; --j) {
|
| 588 | 588 |
if (level[u].first == i && level[u].second > 0) {
|
| 589 | 589 |
// A cycle is found |
| 590 | 590 |
length = _data[u][level[u].second].dist - _data[u][j].dist; |
| 591 | 591 |
size = level[u].second - j; |
| 592 | 592 |
if (!_curr_found || length * _curr_size < _curr_length * size) {
|
| 593 | 593 |
_curr_length = length; |
| 594 | 594 |
_curr_size = size; |
| 595 | 595 |
_curr_node = u; |
| 596 | 596 |
_curr_level = level[u].second; |
| 597 | 597 |
_curr_found = true; |
| 598 | 598 |
} |
| 599 | 599 |
} |
| 600 | 600 |
level[u] = Pair(i, j); |
| 601 |
if (j != 0) {
|
|
| 601 | 602 |
u = _gr.source(_data[u][j].pred); |
| 602 | 603 |
} |
| 603 | 604 |
} |
| 605 |
} |
|
| 604 | 606 |
|
| 605 | 607 |
// If at least one cycle is found, check the optimality condition |
| 606 | 608 |
LargeValue d; |
| 607 | 609 |
if (_curr_found && k < n) {
|
| 608 | 610 |
// Find node potentials |
| 609 | 611 |
for (int i = 0; i < n; ++i) {
|
| 610 | 612 |
u = (*_nodes)[i]; |
| 611 | 613 |
pi[u] = INF; |
| 612 | 614 |
for (int j = 0; j <= k; ++j) {
|
| 613 | 615 |
if (_data[u][j].dist < INF) {
|
| 614 | 616 |
d = _data[u][j].dist * _curr_size - j * _curr_length; |
| 615 | 617 |
if (_tolerance.less(d, pi[u])) pi[u] = d; |
| 616 | 618 |
} |
| 617 | 619 |
} |
| 618 | 620 |
} |
| 619 | 621 |
|
| 620 | 622 |
// Check the optimality condition for all arcs |
| 621 | 623 |
bool done = true; |
| 622 | 624 |
for (ArcIt a(_gr); a != INVALID; ++a) {
|
| 623 | 625 |
if (_tolerance.less(_length[a] * _curr_size - _curr_length, |
| 624 | 626 |
pi[_gr.target(a)] - pi[_gr.source(a)]) ) {
|
| 625 | 627 |
done = false; |
| 626 | 628 |
break; |
| 627 | 629 |
} |
| 628 | 630 |
} |
| 629 | 631 |
return done; |
| 630 | 632 |
} |
| 631 | 633 |
return (k == n); |
| 632 | 634 |
} |
| 633 | 635 |
|
| 634 | 636 |
}; //class HartmannOrlin |
| 635 | 637 |
|
| 636 | 638 |
///@} |
| 637 | 639 |
|
| 638 | 640 |
} //namespace lemon |
| 639 | 641 |
|
| 640 | 642 |
#endif //LEMON_HARTMANN_ORLIN_H |
0 comments (0 inline)