| ... | ... |
@@ -505,136 +505,138 @@ |
| 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 |
|
|
| 601 |
if (j != 0) {
|
|
| 602 |
u = _gr.source(_data[u][j].pred); |
|
| 603 |
} |
|
| 602 | 604 |
} |
| 603 | 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)