| ... | ... |
@@ -567,30 +567,36 @@ |
| 567 | 567 |
/// |
| 568 | 568 |
/// The preflow algorithm consists of two phases, this method runs |
| 569 | 569 |
/// the first phase. After the first phase the maximum flow value |
| 570 | 570 |
/// and a minimum value cut can already be computed, although a |
| 571 | 571 |
/// maximum flow is not yet obtained. So after calling this method |
| 572 | 572 |
/// \ref flowValue() returns the value of a maximum flow and \ref |
| 573 | 573 |
/// minCut() returns a minimum cut. |
| 574 | 574 |
/// \pre One of the \ref init() functions must be called before |
| 575 | 575 |
/// using this function. |
| 576 | 576 |
void startFirstPhase() {
|
| 577 | 577 |
_phase = true; |
| 578 | 578 |
|
| 579 |
Node n = _level->highestActive(); |
|
| 580 |
int level = _level->highestActiveLevel(); |
|
| 581 |
while ( |
|
| 579 |
while (true) {
|
|
| 582 | 580 |
int num = _node_num; |
| 583 | 581 |
|
| 584 |
|
|
| 582 |
Node n = INVALID; |
|
| 583 |
int level = -1; |
|
| 584 |
|
|
| 585 |
while (num > 0) {
|
|
| 586 |
n = _level->highestActive(); |
|
| 587 |
if (n == INVALID) goto first_phase_done; |
|
| 588 |
level = _level->highestActiveLevel(); |
|
| 589 |
--num; |
|
| 590 |
|
|
| 585 | 591 |
Value excess = (*_excess)[n]; |
| 586 | 592 |
int new_level = _level->maxLevel(); |
| 587 | 593 |
|
| 588 | 594 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
| 589 | 595 |
Value rem = (*_capacity)[e] - (*_flow)[e]; |
| 590 | 596 |
if (!_tolerance.positive(rem)) continue; |
| 591 | 597 |
Node v = _graph.target(e); |
| 592 | 598 |
if ((*_level)[v] < level) {
|
| 593 | 599 |
if (!_level->active(v) && v != _target) {
|
| 594 | 600 |
_level->activate(v); |
| 595 | 601 |
} |
| 596 | 602 |
if (!_tolerance.less(rem, excess)) {
|
| ... | ... |
@@ -638,32 +644,40 @@ |
| 638 | 644 |
if (excess != 0) {
|
| 639 | 645 |
if (new_level + 1 < _level->maxLevel()) {
|
| 640 | 646 |
_level->liftHighestActive(new_level + 1); |
| 641 | 647 |
} else {
|
| 642 | 648 |
_level->liftHighestActiveToTop(); |
| 643 | 649 |
} |
| 644 | 650 |
if (_level->emptyLevel(level)) {
|
| 645 | 651 |
_level->liftToTop(level); |
| 646 | 652 |
} |
| 647 | 653 |
} else {
|
| 648 | 654 |
_level->deactivate(n); |
| 649 | 655 |
} |
| 650 |
|
|
| 651 |
n = _level->highestActive(); |
|
| 652 |
level = _level->highestActiveLevel(); |
|
| 653 |
--num; |
|
| 654 | 656 |
} |
| 655 | 657 |
|
| 656 | 658 |
num = _node_num * 20; |
| 657 |
while (num > 0 |
|
| 659 |
while (num > 0) {
|
|
| 660 |
while (level >= 0 && _level->activeFree(level)) {
|
|
| 661 |
--level; |
|
| 662 |
} |
|
| 663 |
if (level == -1) {
|
|
| 664 |
n = _level->highestActive(); |
|
| 665 |
level = _level->highestActiveLevel(); |
|
| 666 |
if (n == INVALID) goto first_phase_done; |
|
| 667 |
} else {
|
|
| 668 |
n = _level->activeOn(level); |
|
| 669 |
} |
|
| 670 |
--num; |
|
| 671 |
|
|
| 658 | 672 |
Value excess = (*_excess)[n]; |
| 659 | 673 |
int new_level = _level->maxLevel(); |
| 660 | 674 |
|
| 661 | 675 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
| 662 | 676 |
Value rem = (*_capacity)[e] - (*_flow)[e]; |
| 663 | 677 |
if (!_tolerance.positive(rem)) continue; |
| 664 | 678 |
Node v = _graph.target(e); |
| 665 | 679 |
if ((*_level)[v] < level) {
|
| 666 | 680 |
if (!_level->active(v) && v != _target) {
|
| 667 | 681 |
_level->activate(v); |
| 668 | 682 |
} |
| 669 | 683 |
if (!_tolerance.less(rem, excess)) {
|
| ... | ... |
@@ -711,37 +725,27 @@ |
| 711 | 725 |
if (excess != 0) {
|
| 712 | 726 |
if (new_level + 1 < _level->maxLevel()) {
|
| 713 | 727 |
_level->liftActiveOn(level, new_level + 1); |
| 714 | 728 |
} else {
|
| 715 | 729 |
_level->liftActiveToTop(level); |
| 716 | 730 |
} |
| 717 | 731 |
if (_level->emptyLevel(level)) {
|
| 718 | 732 |
_level->liftToTop(level); |
| 719 | 733 |
} |
| 720 | 734 |
} else {
|
| 721 | 735 |
_level->deactivate(n); |
| 722 | 736 |
} |
| 723 |
|
|
| 724 |
while (level >= 0 && _level->activeFree(level)) {
|
|
| 725 |
--level; |
|
| 726 | 737 |
} |
| 727 |
if (level == -1) {
|
|
| 728 |
n = _level->highestActive(); |
|
| 729 |
level = _level->highestActiveLevel(); |
|
| 730 |
} else {
|
|
| 731 |
n = _level->activeOn(level); |
|
| 732 | 738 |
} |
| 733 |
--num; |
|
| 734 |
} |
|
| 735 |
|
|
| 739 |
first_phase_done:; |
|
| 736 | 740 |
} |
| 737 | 741 |
|
| 738 | 742 |
/// \brief Starts the second phase of the preflow algorithm. |
| 739 | 743 |
/// |
| 740 | 744 |
/// The preflow algorithm consists of two phases, this method runs |
| 741 | 745 |
/// the second phase. After calling one of the \ref init() functions |
| 742 | 746 |
/// and \ref startFirstPhase() and then \ref startSecondPhase(), |
| 743 | 747 |
/// \ref flowMap() returns a maximum flow, \ref flowValue() returns the |
| 744 | 748 |
/// value of a maximum flow, \ref minCut() returns a minimum cut |
| 745 | 749 |
/// \pre One of the \ref init() functions and \ref startFirstPhase() |
| 746 | 750 |
/// must be called before using this function. |
| 747 | 751 |
void startSecondPhase() {
|
0 comments (0 inline)