!
!
!
42
42
139
139
9
5
2
2
1
1
2
2
10
10
4
3
41
41
9
5
7
7
89
89
10
10
Changeset was too big and was cut off... Show full diff
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -60,25 +60,25 @@ |
| 60 | 60 |
// Set the group mandatory |
| 61 | 61 |
ap.mandatoryGroup("gr");
|
| 62 | 62 |
// Set the options of the group exclusive (only one option can be given) |
| 63 | 63 |
ap.onlyOneGroup("gr");
|
| 64 | 64 |
// Add non-parsed arguments (e.g. input files) |
| 65 | 65 |
ap.other("infile", "The input file.")
|
| 66 | 66 |
.other("...");
|
| 67 | 67 |
|
| 68 | 68 |
// Throw an exception when problems occurs. The default behavior is to |
| 69 | 69 |
// exit(1) on these cases, but this makes Valgrind falsely warn |
| 70 | 70 |
// about memory leaks. |
| 71 | 71 |
ap.throwOnProblems(); |
| 72 |
|
|
| 72 |
|
|
| 73 | 73 |
// Perform the parsing process |
| 74 | 74 |
// (in case of any error it terminates the program) |
| 75 | 75 |
// The try {} construct is necessary only if the ap.trowOnProblems()
|
| 76 | 76 |
// setting is in use. |
| 77 | 77 |
try {
|
| 78 | 78 |
ap.parse(); |
| 79 | 79 |
} catch (ArgParserException &) { return 1; }
|
| 80 | 80 |
|
| 81 | 81 |
// Check each option if it has been given and print its value |
| 82 | 82 |
std::cout << "Parameters of '" << ap.commandName() << "':\n"; |
| 83 | 83 |
|
| 84 | 84 |
std::cout << " Value of -n: " << i << std::endl; |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
/** |
| 20 | 20 |
\mainpage LEMON Documentation |
| 21 | 21 |
|
| 22 | 22 |
\section intro Introduction |
| 23 | 23 |
|
| 24 | 24 |
<b>LEMON</b> stands for <i><b>L</b>ibrary for <b>E</b>fficient <b>M</b>odeling |
| 25 | 25 |
and <b>O</b>ptimization in <b>N</b>etworks</i>. |
| 26 | 26 |
It is a C++ template library providing efficient implementations of common |
| 27 | 27 |
data structures and algorithms with focus on combinatorial optimization |
| 28 |
tasks connected mainly with graphs and networks. |
|
| 28 |
tasks connected mainly with graphs and networks. |
|
| 29 | 29 |
|
| 30 | 30 |
<b> |
| 31 | 31 |
LEMON is an <a class="el" href="http://opensource.org/">open source</a> |
| 32 | 32 |
project. |
| 33 | 33 |
You are free to use it in your commercial or |
| 34 | 34 |
non-commercial applications under very permissive |
| 35 | 35 |
\ref license "license terms". |
| 36 | 36 |
</b> |
| 37 | 37 |
|
| 38 |
The project is maintained by the |
|
| 38 |
The project is maintained by the |
|
| 39 | 39 |
<a href="http://www.cs.elte.hu/egres/">Egerváry Research Group on |
| 40 | 40 |
Combinatorial Optimization</a> \ref egres |
| 41 | 41 |
at the Operations Research Department of the |
| 42 | 42 |
<a href="http://www.elte.hu/en/">Eötvös Loránd University</a>, |
| 43 | 43 |
Budapest, Hungary. |
| 44 | 44 |
LEMON is also a member of the <a href="http://www.coin-or.org/">COIN-OR</a> |
| 45 | 45 |
initiative \ref coinor. |
| 46 | 46 |
|
| 47 | 47 |
\section howtoread How to Read the Documentation |
| 48 | 48 |
|
| 49 | 49 |
If you would like to get to know the library, see |
| 50 | 50 |
<a class="el" href="http://lemon.cs.elte.hu/pub/tutorial/">LEMON Tutorial</a>. |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -72,25 +72,25 @@ |
| 72 | 72 |
An \f$f: A\rightarrow\mathbf{R}\f$ primal feasible solution is optimal
|
| 73 | 73 |
if and only if for some \f$\pi: V\rightarrow\mathbf{R}\f$ node potentials
|
| 74 | 74 |
the following \e complementary \e slackness optimality conditions hold. |
| 75 | 75 |
|
| 76 | 76 |
- For all \f$uv\in A\f$ arcs: |
| 77 | 77 |
- if \f$cost^\pi(uv)>0\f$, then \f$f(uv)=lower(uv)\f$; |
| 78 | 78 |
- if \f$lower(uv)<f(uv)<upper(uv)\f$, then \f$cost^\pi(uv)=0\f$; |
| 79 | 79 |
- if \f$cost^\pi(uv)<0\f$, then \f$f(uv)=upper(uv)\f$. |
| 80 | 80 |
- For all \f$u\in V\f$ nodes: |
| 81 | 81 |
- \f$\pi(u)\leq 0\f$; |
| 82 | 82 |
- if \f$\sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \neq sup(u)\f$,
|
| 83 | 83 |
then \f$\pi(u)=0\f$. |
| 84 |
|
|
| 84 |
|
|
| 85 | 85 |
Here \f$cost^\pi(uv)\f$ denotes the \e reduced \e cost of the arc |
| 86 | 86 |
\f$uv\in A\f$ with respect to the potential function \f$\pi\f$, i.e. |
| 87 | 87 |
\f[ cost^\pi(uv) = cost(uv) + \pi(u) - \pi(v).\f] |
| 88 | 88 |
|
| 89 | 89 |
All algorithms provide dual solution (node potentials), as well, |
| 90 | 90 |
if an optimal flow is found. |
| 91 | 91 |
|
| 92 | 92 |
|
| 93 | 93 |
\section mcf_eq Equality Form |
| 94 | 94 |
|
| 95 | 95 |
The above \ref mcf_def "definition" is actually more general than the |
| 96 | 96 |
usual formulation of the minimum cost flow problem, in which strict |
| ... | ... |
@@ -110,25 +110,25 @@ |
| 110 | 110 |
|
| 111 | 111 |
\section mcf_leq Opposite Inequalites (LEQ Form) |
| 112 | 112 |
|
| 113 | 113 |
Another possible definition of the minimum cost flow problem is |
| 114 | 114 |
when there are <em>"less or equal"</em> (LEQ) supply/demand constraints, |
| 115 | 115 |
instead of the <em>"greater or equal"</em> (GEQ) constraints. |
| 116 | 116 |
|
| 117 | 117 |
\f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f]
|
| 118 | 118 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \leq
|
| 119 | 119 |
sup(u) \quad \forall u\in V \f] |
| 120 | 120 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f] |
| 121 | 121 |
|
| 122 |
It means that the total demand must be less or equal to the |
|
| 122 |
It means that the total demand must be less or equal to the |
|
| 123 | 123 |
total supply (i.e. \f$\sum_{u\in V} sup(u)\f$ must be zero or
|
| 124 | 124 |
positive) and all the demands have to be satisfied, but there |
| 125 | 125 |
could be supplies that are not carried out from the supply |
| 126 | 126 |
nodes. |
| 127 | 127 |
The equality form is also a special case of this form, of course. |
| 128 | 128 |
|
| 129 | 129 |
You could easily transform this case to the \ref mcf_def "GEQ form" |
| 130 | 130 |
of the problem by reversing the direction of the arcs and taking the |
| 131 | 131 |
negative of the supply values (e.g. using \ref ReverseDigraph and |
| 132 | 132 |
\ref NegMap adaptors). |
| 133 | 133 |
However \ref NetworkSimplex algorithm also supports this form directly |
| 134 | 134 |
for the sake of convenience. |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -412,25 +412,25 @@ |
| 412 | 412 |
typedef AF ArcFilterMap; |
| 413 | 413 |
|
| 414 | 414 |
typedef SubDigraphBase Adaptor; |
| 415 | 415 |
protected: |
| 416 | 416 |
NF* _node_filter; |
| 417 | 417 |
AF* _arc_filter; |
| 418 | 418 |
SubDigraphBase() |
| 419 | 419 |
: Parent(), _node_filter(0), _arc_filter(0) { }
|
| 420 | 420 |
|
| 421 | 421 |
void initialize(DGR& digraph, NF& node_filter, AF& arc_filter) {
|
| 422 | 422 |
Parent::initialize(digraph); |
| 423 | 423 |
_node_filter = &node_filter; |
| 424 |
_arc_filter = &arc_filter; |
|
| 424 |
_arc_filter = &arc_filter; |
|
| 425 | 425 |
} |
| 426 | 426 |
|
| 427 | 427 |
public: |
| 428 | 428 |
|
| 429 | 429 |
typedef typename Parent::Node Node; |
| 430 | 430 |
typedef typename Parent::Arc Arc; |
| 431 | 431 |
|
| 432 | 432 |
void first(Node& i) const {
|
| 433 | 433 |
Parent::first(i); |
| 434 | 434 |
while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); |
| 435 | 435 |
} |
| 436 | 436 |
|
| ... | ... |
@@ -499,54 +499,54 @@ |
| 499 | 499 |
return INVALID; |
| 500 | 500 |
} |
| 501 | 501 |
Arc arc = Parent::findArc(source, target, prev); |
| 502 | 502 |
while (arc != INVALID && !(*_arc_filter)[arc]) {
|
| 503 | 503 |
arc = Parent::findArc(source, target, arc); |
| 504 | 504 |
} |
| 505 | 505 |
return arc; |
| 506 | 506 |
} |
| 507 | 507 |
|
| 508 | 508 |
public: |
| 509 | 509 |
|
| 510 | 510 |
template <typename V> |
| 511 |
class NodeMap |
|
| 512 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
|
| 513 |
|
|
| 511 |
class NodeMap |
|
| 512 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
|
| 513 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> {
|
|
| 514 | 514 |
typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
| 515 |
|
|
| 515 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> Parent; |
|
| 516 | 516 |
|
| 517 | 517 |
public: |
| 518 | 518 |
typedef V Value; |
| 519 | 519 |
|
| 520 | 520 |
NodeMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor) |
| 521 | 521 |
: Parent(adaptor) {}
|
| 522 | 522 |
NodeMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor, const V& value) |
| 523 | 523 |
: Parent(adaptor, value) {}
|
| 524 | 524 |
|
| 525 | 525 |
private: |
| 526 | 526 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 527 | 527 |
return operator=<NodeMap>(cmap); |
| 528 | 528 |
} |
| 529 | 529 |
|
| 530 | 530 |
template <typename CMap> |
| 531 | 531 |
NodeMap& operator=(const CMap& cmap) {
|
| 532 | 532 |
Parent::operator=(cmap); |
| 533 | 533 |
return *this; |
| 534 | 534 |
} |
| 535 | 535 |
}; |
| 536 | 536 |
|
| 537 | 537 |
template <typename V> |
| 538 |
class ArcMap |
|
| 538 |
class ArcMap |
|
| 539 | 539 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
| 540 |
|
|
| 540 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> {
|
|
| 541 | 541 |
typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
| 542 | 542 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent; |
| 543 | 543 |
|
| 544 | 544 |
public: |
| 545 | 545 |
typedef V Value; |
| 546 | 546 |
|
| 547 | 547 |
ArcMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor) |
| 548 | 548 |
: Parent(adaptor) {}
|
| 549 | 549 |
ArcMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor, const V& value) |
| 550 | 550 |
: Parent(adaptor, value) {}
|
| 551 | 551 |
|
| 552 | 552 |
private: |
| ... | ... |
@@ -573,25 +573,25 @@ |
| 573 | 573 |
typedef AF ArcFilterMap; |
| 574 | 574 |
|
| 575 | 575 |
typedef SubDigraphBase Adaptor; |
| 576 | 576 |
protected: |
| 577 | 577 |
NF* _node_filter; |
| 578 | 578 |
AF* _arc_filter; |
| 579 | 579 |
SubDigraphBase() |
| 580 | 580 |
: Parent(), _node_filter(0), _arc_filter(0) { }
|
| 581 | 581 |
|
| 582 | 582 |
void initialize(DGR& digraph, NF& node_filter, AF& arc_filter) {
|
| 583 | 583 |
Parent::initialize(digraph); |
| 584 | 584 |
_node_filter = &node_filter; |
| 585 |
_arc_filter = &arc_filter; |
|
| 585 |
_arc_filter = &arc_filter; |
|
| 586 | 586 |
} |
| 587 | 587 |
|
| 588 | 588 |
public: |
| 589 | 589 |
|
| 590 | 590 |
typedef typename Parent::Node Node; |
| 591 | 591 |
typedef typename Parent::Arc Arc; |
| 592 | 592 |
|
| 593 | 593 |
void first(Node& i) const {
|
| 594 | 594 |
Parent::first(i); |
| 595 | 595 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
| 596 | 596 |
} |
| 597 | 597 |
|
| ... | ... |
@@ -642,52 +642,52 @@ |
| 642 | 642 |
const Arc& prev = INVALID) const {
|
| 643 | 643 |
if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
|
| 644 | 644 |
return INVALID; |
| 645 | 645 |
} |
| 646 | 646 |
Arc arc = Parent::findArc(source, target, prev); |
| 647 | 647 |
while (arc != INVALID && !(*_arc_filter)[arc]) {
|
| 648 | 648 |
arc = Parent::findArc(source, target, arc); |
| 649 | 649 |
} |
| 650 | 650 |
return arc; |
| 651 | 651 |
} |
| 652 | 652 |
|
| 653 | 653 |
template <typename V> |
| 654 |
class NodeMap |
|
| 654 |
class NodeMap |
|
| 655 | 655 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
| 656 | 656 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> {
|
| 657 |
typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
|
| 657 |
typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
|
| 658 | 658 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> Parent; |
| 659 | 659 |
|
| 660 | 660 |
public: |
| 661 | 661 |
typedef V Value; |
| 662 | 662 |
|
| 663 | 663 |
NodeMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor) |
| 664 | 664 |
: Parent(adaptor) {}
|
| 665 | 665 |
NodeMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor, const V& value) |
| 666 | 666 |
: Parent(adaptor, value) {}
|
| 667 | 667 |
|
| 668 | 668 |
private: |
| 669 | 669 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 670 | 670 |
return operator=<NodeMap>(cmap); |
| 671 | 671 |
} |
| 672 | 672 |
|
| 673 | 673 |
template <typename CMap> |
| 674 | 674 |
NodeMap& operator=(const CMap& cmap) {
|
| 675 | 675 |
Parent::operator=(cmap); |
| 676 | 676 |
return *this; |
| 677 | 677 |
} |
| 678 | 678 |
}; |
| 679 | 679 |
|
| 680 | 680 |
template <typename V> |
| 681 |
class ArcMap |
|
| 681 |
class ArcMap |
|
| 682 | 682 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
| 683 | 683 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> {
|
| 684 | 684 |
typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
| 685 | 685 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent; |
| 686 | 686 |
|
| 687 | 687 |
public: |
| 688 | 688 |
typedef V Value; |
| 689 | 689 |
|
| 690 | 690 |
ArcMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor) |
| 691 | 691 |
: Parent(adaptor) {}
|
| 692 | 692 |
ArcMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor, const V& value) |
| 693 | 693 |
: Parent(adaptor, value) {}
|
| ... | ... |
@@ -1012,82 +1012,82 @@ |
| 1012 | 1012 |
const Edge& prev = INVALID) const {
|
| 1013 | 1013 |
if (!(*_node_filter)[u] || !(*_node_filter)[v]) {
|
| 1014 | 1014 |
return INVALID; |
| 1015 | 1015 |
} |
| 1016 | 1016 |
Edge edge = Parent::findEdge(u, v, prev); |
| 1017 | 1017 |
while (edge != INVALID && !(*_edge_filter)[edge]) {
|
| 1018 | 1018 |
edge = Parent::findEdge(u, v, edge); |
| 1019 | 1019 |
} |
| 1020 | 1020 |
return edge; |
| 1021 | 1021 |
} |
| 1022 | 1022 |
|
| 1023 | 1023 |
template <typename V> |
| 1024 |
class NodeMap |
|
| 1024 |
class NodeMap |
|
| 1025 | 1025 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
| 1026 | 1026 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> {
|
| 1027 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
|
| 1027 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
|
| 1028 | 1028 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent; |
| 1029 | 1029 |
|
| 1030 | 1030 |
public: |
| 1031 | 1031 |
typedef V Value; |
| 1032 | 1032 |
|
| 1033 | 1033 |
NodeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor) |
| 1034 | 1034 |
: Parent(adaptor) {}
|
| 1035 | 1035 |
NodeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value) |
| 1036 | 1036 |
: Parent(adaptor, value) {}
|
| 1037 | 1037 |
|
| 1038 | 1038 |
private: |
| 1039 | 1039 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 1040 | 1040 |
return operator=<NodeMap>(cmap); |
| 1041 | 1041 |
} |
| 1042 | 1042 |
|
| 1043 | 1043 |
template <typename CMap> |
| 1044 | 1044 |
NodeMap& operator=(const CMap& cmap) {
|
| 1045 | 1045 |
Parent::operator=(cmap); |
| 1046 | 1046 |
return *this; |
| 1047 | 1047 |
} |
| 1048 | 1048 |
}; |
| 1049 | 1049 |
|
| 1050 | 1050 |
template <typename V> |
| 1051 |
class ArcMap |
|
| 1051 |
class ArcMap |
|
| 1052 | 1052 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
| 1053 | 1053 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> {
|
| 1054 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
|
| 1054 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
|
| 1055 | 1055 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent; |
| 1056 | 1056 |
|
| 1057 | 1057 |
public: |
| 1058 | 1058 |
typedef V Value; |
| 1059 | 1059 |
|
| 1060 | 1060 |
ArcMap(const SubGraphBase<GR, NF, EF, ch>& adaptor) |
| 1061 | 1061 |
: Parent(adaptor) {}
|
| 1062 | 1062 |
ArcMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value) |
| 1063 | 1063 |
: Parent(adaptor, value) {}
|
| 1064 | 1064 |
|
| 1065 | 1065 |
private: |
| 1066 | 1066 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 1067 | 1067 |
return operator=<ArcMap>(cmap); |
| 1068 | 1068 |
} |
| 1069 | 1069 |
|
| 1070 | 1070 |
template <typename CMap> |
| 1071 | 1071 |
ArcMap& operator=(const CMap& cmap) {
|
| 1072 | 1072 |
Parent::operator=(cmap); |
| 1073 | 1073 |
return *this; |
| 1074 | 1074 |
} |
| 1075 | 1075 |
}; |
| 1076 | 1076 |
|
| 1077 | 1077 |
template <typename V> |
| 1078 |
class EdgeMap |
|
| 1078 |
class EdgeMap |
|
| 1079 | 1079 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
| 1080 | 1080 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> {
|
| 1081 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
|
| 1081 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
|
| 1082 | 1082 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent; |
| 1083 | 1083 |
|
| 1084 | 1084 |
public: |
| 1085 | 1085 |
typedef V Value; |
| 1086 | 1086 |
|
| 1087 | 1087 |
EdgeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor) |
| 1088 | 1088 |
: Parent(adaptor) {}
|
| 1089 | 1089 |
|
| 1090 | 1090 |
EdgeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value) |
| 1091 | 1091 |
: Parent(adaptor, value) {}
|
| 1092 | 1092 |
|
| 1093 | 1093 |
private: |
| ... | ... |
@@ -1108,26 +1108,26 @@ |
| 1108 | 1108 |
class SubGraphBase<GR, NF, EF, false> |
| 1109 | 1109 |
: public GraphAdaptorBase<GR> {
|
| 1110 | 1110 |
typedef GraphAdaptorBase<GR> Parent; |
| 1111 | 1111 |
public: |
| 1112 | 1112 |
typedef GR Graph; |
| 1113 | 1113 |
typedef NF NodeFilterMap; |
| 1114 | 1114 |
typedef EF EdgeFilterMap; |
| 1115 | 1115 |
|
| 1116 | 1116 |
typedef SubGraphBase Adaptor; |
| 1117 | 1117 |
protected: |
| 1118 | 1118 |
NF* _node_filter; |
| 1119 | 1119 |
EF* _edge_filter; |
| 1120 |
SubGraphBase() |
|
| 1121 |
: Parent(), _node_filter(0), _edge_filter(0) { }
|
|
| 1120 |
SubGraphBase() |
|
| 1121 |
: Parent(), _node_filter(0), _edge_filter(0) { }
|
|
| 1122 | 1122 |
|
| 1123 | 1123 |
void initialize(GR& graph, NF& node_filter, EF& edge_filter) {
|
| 1124 | 1124 |
Parent::initialize(graph); |
| 1125 | 1125 |
_node_filter = &node_filter; |
| 1126 | 1126 |
_edge_filter = &edge_filter; |
| 1127 | 1127 |
} |
| 1128 | 1128 |
|
| 1129 | 1129 |
public: |
| 1130 | 1130 |
|
| 1131 | 1131 |
typedef typename Parent::Node Node; |
| 1132 | 1132 |
typedef typename Parent::Arc Arc; |
| 1133 | 1133 |
typedef typename Parent::Edge Edge; |
| ... | ... |
@@ -1210,83 +1210,83 @@ |
| 1210 | 1210 |
|
| 1211 | 1211 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
| 1212 | 1212 |
Edge findEdge(const Node& u, const Node& v, |
| 1213 | 1213 |
const Edge& prev = INVALID) const {
|
| 1214 | 1214 |
Edge edge = Parent::findEdge(u, v, prev); |
| 1215 | 1215 |
while (edge != INVALID && !(*_edge_filter)[edge]) {
|
| 1216 | 1216 |
edge = Parent::findEdge(u, v, edge); |
| 1217 | 1217 |
} |
| 1218 | 1218 |
return edge; |
| 1219 | 1219 |
} |
| 1220 | 1220 |
|
| 1221 | 1221 |
template <typename V> |
| 1222 |
class NodeMap |
|
| 1222 |
class NodeMap |
|
| 1223 | 1223 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
| 1224 | 1224 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> {
|
| 1225 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
|
| 1225 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
|
| 1226 | 1226 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent; |
| 1227 | 1227 |
|
| 1228 | 1228 |
public: |
| 1229 | 1229 |
typedef V Value; |
| 1230 | 1230 |
|
| 1231 | 1231 |
NodeMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
| 1232 | 1232 |
: Parent(adaptor) {}
|
| 1233 | 1233 |
NodeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
| 1234 | 1234 |
: Parent(adaptor, value) {}
|
| 1235 | 1235 |
|
| 1236 | 1236 |
private: |
| 1237 | 1237 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 1238 | 1238 |
return operator=<NodeMap>(cmap); |
| 1239 | 1239 |
} |
| 1240 | 1240 |
|
| 1241 | 1241 |
template <typename CMap> |
| 1242 | 1242 |
NodeMap& operator=(const CMap& cmap) {
|
| 1243 | 1243 |
Parent::operator=(cmap); |
| 1244 | 1244 |
return *this; |
| 1245 | 1245 |
} |
| 1246 | 1246 |
}; |
| 1247 | 1247 |
|
| 1248 | 1248 |
template <typename V> |
| 1249 |
class ArcMap |
|
| 1249 |
class ArcMap |
|
| 1250 | 1250 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
| 1251 | 1251 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> {
|
| 1252 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
|
| 1252 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
|
| 1253 | 1253 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent; |
| 1254 | 1254 |
|
| 1255 | 1255 |
public: |
| 1256 | 1256 |
typedef V Value; |
| 1257 | 1257 |
|
| 1258 | 1258 |
ArcMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
| 1259 | 1259 |
: Parent(adaptor) {}
|
| 1260 | 1260 |
ArcMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
| 1261 | 1261 |
: Parent(adaptor, value) {}
|
| 1262 | 1262 |
|
| 1263 | 1263 |
private: |
| 1264 | 1264 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 1265 | 1265 |
return operator=<ArcMap>(cmap); |
| 1266 | 1266 |
} |
| 1267 | 1267 |
|
| 1268 | 1268 |
template <typename CMap> |
| 1269 | 1269 |
ArcMap& operator=(const CMap& cmap) {
|
| 1270 | 1270 |
Parent::operator=(cmap); |
| 1271 | 1271 |
return *this; |
| 1272 | 1272 |
} |
| 1273 | 1273 |
}; |
| 1274 | 1274 |
|
| 1275 | 1275 |
template <typename V> |
| 1276 |
class EdgeMap |
|
| 1276 |
class EdgeMap |
|
| 1277 | 1277 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
| 1278 | 1278 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> {
|
| 1279 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
|
| 1280 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent; |
|
| 1279 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
|
| 1280 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent; |
|
| 1281 | 1281 |
|
| 1282 | 1282 |
public: |
| 1283 | 1283 |
typedef V Value; |
| 1284 | 1284 |
|
| 1285 | 1285 |
EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
| 1286 | 1286 |
: Parent(adaptor) {}
|
| 1287 | 1287 |
|
| 1288 | 1288 |
EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
| 1289 | 1289 |
: Parent(adaptor, value) {}
|
| 1290 | 1290 |
|
| 1291 | 1291 |
private: |
| 1292 | 1292 |
EdgeMap& operator=(const EdgeMap& cmap) {
|
| ... | ... |
@@ -1495,46 +1495,46 @@ |
| 1495 | 1495 |
template<typename GR, typename NF> |
| 1496 | 1496 |
class FilterNodes {
|
| 1497 | 1497 |
#else |
| 1498 | 1498 |
template<typename GR, |
| 1499 | 1499 |
typename NF = typename GR::template NodeMap<bool>, |
| 1500 | 1500 |
typename Enable = void> |
| 1501 | 1501 |
class FilterNodes : |
| 1502 | 1502 |
public DigraphAdaptorExtender< |
| 1503 | 1503 |
SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >, |
| 1504 | 1504 |
true> > {
|
| 1505 | 1505 |
#endif |
| 1506 | 1506 |
typedef DigraphAdaptorExtender< |
| 1507 |
SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >, |
|
| 1507 |
SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >, |
|
| 1508 | 1508 |
true> > Parent; |
| 1509 | 1509 |
|
| 1510 | 1510 |
public: |
| 1511 | 1511 |
|
| 1512 | 1512 |
typedef GR Digraph; |
| 1513 | 1513 |
typedef NF NodeFilterMap; |
| 1514 | 1514 |
|
| 1515 | 1515 |
typedef typename Parent::Node Node; |
| 1516 | 1516 |
|
| 1517 | 1517 |
protected: |
| 1518 | 1518 |
ConstMap<typename Digraph::Arc, Const<bool, true> > const_true_map; |
| 1519 | 1519 |
|
| 1520 | 1520 |
FilterNodes() : const_true_map() {}
|
| 1521 | 1521 |
|
| 1522 | 1522 |
public: |
| 1523 | 1523 |
|
| 1524 | 1524 |
/// \brief Constructor |
| 1525 | 1525 |
/// |
| 1526 | 1526 |
/// Creates a subgraph for the given digraph or graph with the |
| 1527 | 1527 |
/// given node filter map. |
| 1528 |
FilterNodes(GR& graph, NF& node_filter) |
|
| 1528 |
FilterNodes(GR& graph, NF& node_filter) |
|
| 1529 | 1529 |
: Parent(), const_true_map() |
| 1530 | 1530 |
{
|
| 1531 | 1531 |
Parent::initialize(graph, node_filter, const_true_map); |
| 1532 | 1532 |
} |
| 1533 | 1533 |
|
| 1534 | 1534 |
/// \brief Sets the status of the given node |
| 1535 | 1535 |
/// |
| 1536 | 1536 |
/// This function sets the status of the given node. |
| 1537 | 1537 |
/// It is done by simply setting the assigned value of \c n |
| 1538 | 1538 |
/// to \c v in the node filter map. |
| 1539 | 1539 |
void status(const Node& n, bool v) const { Parent::status(n, v); }
|
| 1540 | 1540 |
|
| ... | ... |
@@ -1554,29 +1554,29 @@ |
| 1554 | 1554 |
/// \brief Enables the given node |
| 1555 | 1555 |
/// |
| 1556 | 1556 |
/// This function enables the given node. |
| 1557 | 1557 |
/// It is the same as \ref status() "status(n, true)". |
| 1558 | 1558 |
void enable(const Node& n) const { Parent::status(n, true); }
|
| 1559 | 1559 |
|
| 1560 | 1560 |
}; |
| 1561 | 1561 |
|
| 1562 | 1562 |
template<typename GR, typename NF> |
| 1563 | 1563 |
class FilterNodes<GR, NF, |
| 1564 | 1564 |
typename enable_if<UndirectedTagIndicator<GR> >::type> : |
| 1565 | 1565 |
public GraphAdaptorExtender< |
| 1566 |
SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, |
|
| 1566 |
SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, |
|
| 1567 | 1567 |
true> > {
|
| 1568 | 1568 |
|
| 1569 | 1569 |
typedef GraphAdaptorExtender< |
| 1570 |
SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, |
|
| 1570 |
SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, |
|
| 1571 | 1571 |
true> > Parent; |
| 1572 | 1572 |
|
| 1573 | 1573 |
public: |
| 1574 | 1574 |
|
| 1575 | 1575 |
typedef GR Graph; |
| 1576 | 1576 |
typedef NF NodeFilterMap; |
| 1577 | 1577 |
|
| 1578 | 1578 |
typedef typename Parent::Node Node; |
| 1579 | 1579 |
|
| 1580 | 1580 |
protected: |
| 1581 | 1581 |
ConstMap<typename GR::Edge, Const<bool, true> > const_true_map; |
| 1582 | 1582 |
|
| ... | ... |
@@ -1644,25 +1644,25 @@ |
| 1644 | 1644 |
template<typename DGR, |
| 1645 | 1645 |
typename AF> |
| 1646 | 1646 |
class FilterArcs {
|
| 1647 | 1647 |
#else |
| 1648 | 1648 |
template<typename DGR, |
| 1649 | 1649 |
typename AF = typename DGR::template ArcMap<bool> > |
| 1650 | 1650 |
class FilterArcs : |
| 1651 | 1651 |
public DigraphAdaptorExtender< |
| 1652 | 1652 |
SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >, |
| 1653 | 1653 |
AF, false> > {
|
| 1654 | 1654 |
#endif |
| 1655 | 1655 |
typedef DigraphAdaptorExtender< |
| 1656 |
SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >, |
|
| 1656 |
SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >, |
|
| 1657 | 1657 |
AF, false> > Parent; |
| 1658 | 1658 |
|
| 1659 | 1659 |
public: |
| 1660 | 1660 |
|
| 1661 | 1661 |
/// The type of the adapted digraph. |
| 1662 | 1662 |
typedef DGR Digraph; |
| 1663 | 1663 |
/// The type of the arc filter map. |
| 1664 | 1664 |
typedef AF ArcFilterMap; |
| 1665 | 1665 |
|
| 1666 | 1666 |
typedef typename Parent::Arc Arc; |
| 1667 | 1667 |
|
| 1668 | 1668 |
protected: |
| ... | ... |
@@ -1752,54 +1752,54 @@ |
| 1752 | 1752 |
/// |
| 1753 | 1753 |
/// \note The \c Node, \c Edge and \c Arc types of this adaptor and the |
| 1754 | 1754 |
/// adapted graph are convertible to each other. |
| 1755 | 1755 |
#ifdef DOXYGEN |
| 1756 | 1756 |
template<typename GR, |
| 1757 | 1757 |
typename EF> |
| 1758 | 1758 |
class FilterEdges {
|
| 1759 | 1759 |
#else |
| 1760 | 1760 |
template<typename GR, |
| 1761 | 1761 |
typename EF = typename GR::template EdgeMap<bool> > |
| 1762 | 1762 |
class FilterEdges : |
| 1763 | 1763 |
public GraphAdaptorExtender< |
| 1764 |
SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true> >, |
|
| 1764 |
SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true> >, |
|
| 1765 | 1765 |
EF, false> > {
|
| 1766 | 1766 |
#endif |
| 1767 | 1767 |
typedef GraphAdaptorExtender< |
| 1768 |
SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true > >, |
|
| 1768 |
SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true > >, |
|
| 1769 | 1769 |
EF, false> > Parent; |
| 1770 | 1770 |
|
| 1771 | 1771 |
public: |
| 1772 | 1772 |
|
| 1773 | 1773 |
/// The type of the adapted graph. |
| 1774 | 1774 |
typedef GR Graph; |
| 1775 | 1775 |
/// The type of the edge filter map. |
| 1776 | 1776 |
typedef EF EdgeFilterMap; |
| 1777 | 1777 |
|
| 1778 | 1778 |
typedef typename Parent::Edge Edge; |
| 1779 | 1779 |
|
| 1780 | 1780 |
protected: |
| 1781 | 1781 |
ConstMap<typename GR::Node, Const<bool, true> > const_true_map; |
| 1782 | 1782 |
|
| 1783 | 1783 |
FilterEdges() : const_true_map(true) {
|
| 1784 | 1784 |
Parent::setNodeFilterMap(const_true_map); |
| 1785 | 1785 |
} |
| 1786 | 1786 |
|
| 1787 | 1787 |
public: |
| 1788 | 1788 |
|
| 1789 | 1789 |
/// \brief Constructor |
| 1790 | 1790 |
/// |
| 1791 | 1791 |
/// Creates a subgraph for the given graph with the given edge |
| 1792 | 1792 |
/// filter map. |
| 1793 |
FilterEdges(GR& graph, EF& edge_filter) |
|
| 1793 |
FilterEdges(GR& graph, EF& edge_filter) |
|
| 1794 | 1794 |
: Parent(), const_true_map() {
|
| 1795 | 1795 |
Parent::initialize(graph, const_true_map, edge_filter); |
| 1796 | 1796 |
} |
| 1797 | 1797 |
|
| 1798 | 1798 |
/// \brief Sets the status of the given edge |
| 1799 | 1799 |
/// |
| 1800 | 1800 |
/// This function sets the status of the given edge. |
| 1801 | 1801 |
/// It is done by simply setting the assigned value of \c e |
| 1802 | 1802 |
/// to \c v in the edge filter map. |
| 1803 | 1803 |
void status(const Edge& e, bool v) const { Parent::status(e, v); }
|
| 1804 | 1804 |
|
| 1805 | 1805 |
/// \brief Returns the status of the given edge |
| ... | ... |
@@ -1849,25 +1849,25 @@ |
| 1849 | 1849 |
|
| 1850 | 1850 |
typedef True UndirectedTag; |
| 1851 | 1851 |
|
| 1852 | 1852 |
typedef typename Digraph::Arc Edge; |
| 1853 | 1853 |
typedef typename Digraph::Node Node; |
| 1854 | 1854 |
|
| 1855 | 1855 |
class Arc {
|
| 1856 | 1856 |
friend class UndirectorBase; |
| 1857 | 1857 |
protected: |
| 1858 | 1858 |
Edge _edge; |
| 1859 | 1859 |
bool _forward; |
| 1860 | 1860 |
|
| 1861 |
Arc(const Edge& edge, bool forward) |
|
| 1861 |
Arc(const Edge& edge, bool forward) |
|
| 1862 | 1862 |
: _edge(edge), _forward(forward) {}
|
| 1863 | 1863 |
|
| 1864 | 1864 |
public: |
| 1865 | 1865 |
Arc() {}
|
| 1866 | 1866 |
|
| 1867 | 1867 |
Arc(Invalid) : _edge(INVALID), _forward(true) {}
|
| 1868 | 1868 |
|
| 1869 | 1869 |
operator const Edge&() const { return _edge; }
|
| 1870 | 1870 |
|
| 1871 | 1871 |
bool operator==(const Arc &other) const {
|
| 1872 | 1872 |
return _forward == other._forward && _edge == other._edge; |
| 1873 | 1873 |
} |
| ... | ... |
@@ -2089,25 +2089,25 @@ |
| 2089 | 2089 |
|
| 2090 | 2090 |
typedef V Value; |
| 2091 | 2091 |
typedef Arc Key; |
| 2092 | 2092 |
typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReturnValue; |
| 2093 | 2093 |
typedef typename MapTraits<MapImpl>::ReturnValue ReturnValue; |
| 2094 | 2094 |
typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReference; |
| 2095 | 2095 |
typedef typename MapTraits<MapImpl>::ReturnValue Reference; |
| 2096 | 2096 |
|
| 2097 | 2097 |
ArcMapBase(const UndirectorBase<DGR>& adaptor) : |
| 2098 | 2098 |
_forward(*adaptor._digraph), _backward(*adaptor._digraph) {}
|
| 2099 | 2099 |
|
| 2100 | 2100 |
ArcMapBase(const UndirectorBase<DGR>& adaptor, const V& value) |
| 2101 |
: _forward(*adaptor._digraph, value), |
|
| 2101 |
: _forward(*adaptor._digraph, value), |
|
| 2102 | 2102 |
_backward(*adaptor._digraph, value) {}
|
| 2103 | 2103 |
|
| 2104 | 2104 |
void set(const Arc& a, const V& value) {
|
| 2105 | 2105 |
if (direction(a)) {
|
| 2106 | 2106 |
_forward.set(a, value); |
| 2107 | 2107 |
} else {
|
| 2108 | 2108 |
_backward.set(a, value); |
| 2109 | 2109 |
} |
| 2110 | 2110 |
} |
| 2111 | 2111 |
|
| 2112 | 2112 |
ConstReturnValue operator[](const Arc& a) const {
|
| 2113 | 2113 |
if (direction(a)) {
|
| ... | ... |
@@ -2207,25 +2207,25 @@ |
| 2207 | 2207 |
EdgeMap& operator=(const CMap& cmap) {
|
| 2208 | 2208 |
Parent::operator=(cmap); |
| 2209 | 2209 |
return *this; |
| 2210 | 2210 |
} |
| 2211 | 2211 |
|
| 2212 | 2212 |
}; |
| 2213 | 2213 |
|
| 2214 | 2214 |
typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier; |
| 2215 | 2215 |
NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); }
|
| 2216 | 2216 |
|
| 2217 | 2217 |
typedef typename ItemSetTraits<DGR, Edge>::ItemNotifier EdgeNotifier; |
| 2218 | 2218 |
EdgeNotifier& notifier(Edge) const { return _digraph->notifier(Edge()); }
|
| 2219 |
|
|
| 2219 |
|
|
| 2220 | 2220 |
typedef EdgeNotifier ArcNotifier; |
| 2221 | 2221 |
ArcNotifier& notifier(Arc) const { return _digraph->notifier(Edge()); }
|
| 2222 | 2222 |
|
| 2223 | 2223 |
protected: |
| 2224 | 2224 |
|
| 2225 | 2225 |
UndirectorBase() : _digraph(0) {}
|
| 2226 | 2226 |
|
| 2227 | 2227 |
DGR* _digraph; |
| 2228 | 2228 |
|
| 2229 | 2229 |
void initialize(DGR& digraph) {
|
| 2230 | 2230 |
_digraph = &digraph; |
| 2231 | 2231 |
} |
| ... | ... |
@@ -2719,25 +2719,25 @@ |
| 2719 | 2719 |
/// |
| 2720 | 2720 |
/// \note The \c Node type of this adaptor and the adapted digraph are |
| 2721 | 2721 |
/// convertible to each other, moreover the \c Arc type of the adaptor |
| 2722 | 2722 |
/// is convertible to the \c Arc type of the adapted digraph. |
| 2723 | 2723 |
#ifdef DOXYGEN |
| 2724 | 2724 |
template<typename DGR, typename CM, typename FM, typename TL> |
| 2725 | 2725 |
class ResidualDigraph |
| 2726 | 2726 |
#else |
| 2727 | 2727 |
template<typename DGR, |
| 2728 | 2728 |
typename CM = typename DGR::template ArcMap<int>, |
| 2729 | 2729 |
typename FM = CM, |
| 2730 | 2730 |
typename TL = Tolerance<typename CM::Value> > |
| 2731 |
class ResidualDigraph |
|
| 2731 |
class ResidualDigraph |
|
| 2732 | 2732 |
: public SubDigraph< |
| 2733 | 2733 |
Undirector<const DGR>, |
| 2734 | 2734 |
ConstMap<typename DGR::Node, Const<bool, true> >, |
| 2735 | 2735 |
typename Undirector<const DGR>::template CombinedArcMap< |
| 2736 | 2736 |
_adaptor_bits::ResForwardFilter<const DGR, CM, FM, TL>, |
| 2737 | 2737 |
_adaptor_bits::ResBackwardFilter<const DGR, CM, FM, TL> > > |
| 2738 | 2738 |
#endif |
| 2739 | 2739 |
{
|
| 2740 | 2740 |
public: |
| 2741 | 2741 |
|
| 2742 | 2742 |
/// The type of the underlying digraph. |
| 2743 | 2743 |
typedef DGR Digraph; |
| ... | ... |
@@ -2776,25 +2776,25 @@ |
| 2776 | 2776 |
ForwardFilter _forward_filter; |
| 2777 | 2777 |
BackwardFilter _backward_filter; |
| 2778 | 2778 |
ArcFilter _arc_filter; |
| 2779 | 2779 |
|
| 2780 | 2780 |
public: |
| 2781 | 2781 |
|
| 2782 | 2782 |
/// \brief Constructor |
| 2783 | 2783 |
/// |
| 2784 | 2784 |
/// Constructor of the residual digraph adaptor. The parameters are the |
| 2785 | 2785 |
/// digraph, the capacity map, the flow map, and a tolerance object. |
| 2786 | 2786 |
ResidualDigraph(const DGR& digraph, const CM& capacity, |
| 2787 | 2787 |
FM& flow, const TL& tolerance = Tolerance()) |
| 2788 |
: Parent(), _capacity(&capacity), _flow(&flow), |
|
| 2788 |
: Parent(), _capacity(&capacity), _flow(&flow), |
|
| 2789 | 2789 |
_graph(digraph), _node_filter(), |
| 2790 | 2790 |
_forward_filter(capacity, flow, tolerance), |
| 2791 | 2791 |
_backward_filter(capacity, flow, tolerance), |
| 2792 | 2792 |
_arc_filter(_forward_filter, _backward_filter) |
| 2793 | 2793 |
{
|
| 2794 | 2794 |
Parent::initialize(_graph, _node_filter, _arc_filter); |
| 2795 | 2795 |
} |
| 2796 | 2796 |
|
| 2797 | 2797 |
typedef typename Parent::Arc Arc; |
| 2798 | 2798 |
|
| 2799 | 2799 |
/// \brief Returns the residual capacity of the given arc. |
| 2800 | 2800 |
/// |
| ... | ... |
@@ -2858,25 +2858,25 @@ |
| 2858 | 2858 |
/// capacities as an arc map of the residual digraph. |
| 2859 | 2859 |
/// Its value type is inherited from the capacity map. |
| 2860 | 2860 |
class ResidualCapacity {
|
| 2861 | 2861 |
protected: |
| 2862 | 2862 |
const Adaptor* _adaptor; |
| 2863 | 2863 |
public: |
| 2864 | 2864 |
/// The key type of the map |
| 2865 | 2865 |
typedef Arc Key; |
| 2866 | 2866 |
/// The value type of the map |
| 2867 | 2867 |
typedef typename CapacityMap::Value Value; |
| 2868 | 2868 |
|
| 2869 | 2869 |
/// Constructor |
| 2870 |
ResidualCapacity(const ResidualDigraph<DGR, CM, FM, TL>& adaptor) |
|
| 2870 |
ResidualCapacity(const ResidualDigraph<DGR, CM, FM, TL>& adaptor) |
|
| 2871 | 2871 |
: _adaptor(&adaptor) {}
|
| 2872 | 2872 |
|
| 2873 | 2873 |
/// Returns the value associated with the given residual arc |
| 2874 | 2874 |
Value operator[](const Arc& a) const {
|
| 2875 | 2875 |
return _adaptor->residualCapacity(a); |
| 2876 | 2876 |
} |
| 2877 | 2877 |
|
| 2878 | 2878 |
}; |
| 2879 | 2879 |
|
| 2880 | 2880 |
/// \brief Returns a residual capacity map |
| 2881 | 2881 |
/// |
| 2882 | 2882 |
/// This function just returns a residual capacity map. |
| ... | ... |
@@ -3438,25 +3438,25 @@ |
| 3438 | 3438 |
/// |
| 3439 | 3439 |
/// Returns the arc in the adaptor that corresponds to the given |
| 3440 | 3440 |
/// original arc. |
| 3441 | 3441 |
static Arc arc(const DigraphArc& a) {
|
| 3442 | 3442 |
return Parent::arc(a); |
| 3443 | 3443 |
} |
| 3444 | 3444 |
|
| 3445 | 3445 |
/// \brief Node map combined from two original node maps |
| 3446 | 3446 |
/// |
| 3447 | 3447 |
/// This map adaptor class adapts two node maps of the original digraph |
| 3448 | 3448 |
/// to get a node map of the split digraph. |
| 3449 | 3449 |
/// Its value type is inherited from the first node map type (\c IN). |
| 3450 |
/// \tparam IN The type of the node map for the in-nodes. |
|
| 3450 |
/// \tparam IN The type of the node map for the in-nodes. |
|
| 3451 | 3451 |
/// \tparam OUT The type of the node map for the out-nodes. |
| 3452 | 3452 |
template <typename IN, typename OUT> |
| 3453 | 3453 |
class CombinedNodeMap {
|
| 3454 | 3454 |
public: |
| 3455 | 3455 |
|
| 3456 | 3456 |
/// The key type of the map |
| 3457 | 3457 |
typedef Node Key; |
| 3458 | 3458 |
/// The value type of the map |
| 3459 | 3459 |
typedef typename IN::Value Value; |
| 3460 | 3460 |
|
| 3461 | 3461 |
typedef typename MapTraits<IN>::ReferenceMapTag ReferenceMapTag; |
| 3462 | 3462 |
typedef typename MapTraits<IN>::ReturnValue ReturnValue; |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
#include <lemon/arg_parser.h> |
| 20 | 20 |
|
| 21 | 21 |
namespace lemon {
|
| 22 | 22 |
|
| 23 | 23 |
void ArgParser::_terminate(ArgParserException::Reason reason) const |
| 24 | 24 |
{
|
| 25 | 25 |
if(_exit_on_problems) |
| 26 | 26 |
exit(1); |
| 27 | 27 |
else throw(ArgParserException(reason)); |
| 28 | 28 |
} |
| 29 |
|
|
| 30 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 | 31 |
void ArgParser::_showHelp(void *p) |
| 32 | 32 |
{
|
| 33 | 33 |
(static_cast<ArgParser*>(p))->showHelp(); |
| 34 | 34 |
(static_cast<ArgParser*>(p))->_terminate(ArgParserException::HELP); |
| 35 | 35 |
} |
| 36 | 36 |
|
| 37 | 37 |
ArgParser::ArgParser(int argc, const char * const *argv) |
| 38 | 38 |
:_argc(argc), _argv(argv), _command_name(argv[0]), |
| 39 | 39 |
_exit_on_problems(true) {
|
| 40 | 40 |
funcOption("-help","Print a short help message",_showHelp,this);
|
| 41 | 41 |
synonym("help","-help");
|
| 42 | 42 |
synonym("h","-help");
|
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -33,28 +33,28 @@ |
| 33 | 33 |
///\brief A tool to parse command line arguments. |
| 34 | 34 |
|
| 35 | 35 |
namespace lemon {
|
| 36 | 36 |
|
| 37 | 37 |
///Exception used by ArgParser |
| 38 | 38 |
class ArgParserException : public Exception {
|
| 39 | 39 |
public: |
| 40 | 40 |
enum Reason {
|
| 41 | 41 |
HELP, /// <tt>--help</tt> option was given |
| 42 | 42 |
UNKNOWN_OPT, /// Unknown option was given |
| 43 | 43 |
INVALID_OPT /// Invalid combination of options |
| 44 | 44 |
}; |
| 45 |
|
|
| 45 |
|
|
| 46 | 46 |
private: |
| 47 | 47 |
Reason _reason; |
| 48 |
|
|
| 48 |
|
|
| 49 | 49 |
public: |
| 50 | 50 |
///Constructor |
| 51 | 51 |
ArgParserException(Reason r) throw() : _reason(r) {}
|
| 52 | 52 |
///Virtual destructor |
| 53 | 53 |
virtual ~ArgParserException() throw() {}
|
| 54 | 54 |
///A short description of the exception |
| 55 | 55 |
virtual const char* what() const throw() {
|
| 56 | 56 |
switch(_reason) |
| 57 | 57 |
{
|
| 58 | 58 |
case HELP: |
| 59 | 59 |
return "lemon::ArgParseException: ask for help"; |
| 60 | 60 |
break; |
| ... | ... |
@@ -132,39 +132,39 @@ |
| 132 | 132 |
struct OtherArg |
| 133 | 133 |
{
|
| 134 | 134 |
std::string name; |
| 135 | 135 |
std::string help; |
| 136 | 136 |
OtherArg(std::string n, std::string h) :name(n), help(h) {}
|
| 137 | 137 |
|
| 138 | 138 |
}; |
| 139 | 139 |
|
| 140 | 140 |
std::vector<OtherArg> _others_help; |
| 141 | 141 |
std::vector<std::string> _file_args; |
| 142 | 142 |
std::string _command_name; |
| 143 | 143 |
|
| 144 |
|
|
| 144 |
|
|
| 145 | 145 |
private: |
| 146 | 146 |
//Bind a function to an option. |
| 147 | 147 |
|
| 148 | 148 |
//\param name The name of the option. The leading '-' must be omitted. |
| 149 | 149 |
//\param help A help string. |
| 150 | 150 |
//\retval func The function to be called when the option is given. It |
| 151 | 151 |
// must be of type "void f(void *)" |
| 152 | 152 |
//\param data Data to be passed to \c func |
| 153 | 153 |
ArgParser &funcOption(const std::string &name, |
| 154 | 154 |
const std::string &help, |
| 155 | 155 |
void (*func)(void *),void *data); |
| 156 | 156 |
|
| 157 | 157 |
bool _exit_on_problems; |
| 158 |
|
|
| 158 |
|
|
| 159 | 159 |
void _terminate(ArgParserException::Reason reason) const; |
| 160 | 160 |
|
| 161 | 161 |
public: |
| 162 | 162 |
|
| 163 | 163 |
///Constructor |
| 164 | 164 |
ArgParser(int argc, const char * const *argv); |
| 165 | 165 |
|
| 166 | 166 |
~ArgParser(); |
| 167 | 167 |
|
| 168 | 168 |
///\name Options |
| 169 | 169 |
/// |
| 170 | 170 |
|
| ... | ... |
@@ -414,20 +414,20 @@ |
| 414 | 414 |
RefType operator[](const std::string &n) const |
| 415 | 415 |
{
|
| 416 | 416 |
return RefType(*this, n); |
| 417 | 417 |
} |
| 418 | 418 |
|
| 419 | 419 |
///Give back the non-option type arguments. |
| 420 | 420 |
|
| 421 | 421 |
///Give back a reference to a vector consisting of the program arguments |
| 422 | 422 |
///not starting with a '-' character. |
| 423 | 423 |
const std::vector<std::string> &files() const { return _file_args; }
|
| 424 | 424 |
|
| 425 | 425 |
///Throw instead of exit in case of problems |
| 426 |
void throwOnProblems() |
|
| 426 |
void throwOnProblems() |
|
| 427 | 427 |
{
|
| 428 | 428 |
_exit_on_problems=false; |
| 429 | 429 |
} |
| 430 | 430 |
}; |
| 431 | 431 |
} |
| 432 | 432 |
|
| 433 | 433 |
#endif // LEMON_ARG_PARSER_H |
| 1 |
/* -*- C++ -*- |
|
| 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
| 2 | 2 |
* |
| 3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
| 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -27,34 +27,34 @@ |
| 27 | 27 |
#include <lemon/bits/path_dump.h> |
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/error.h> |
| 30 | 30 |
#include <lemon/maps.h> |
| 31 | 31 |
#include <lemon/tolerance.h> |
| 32 | 32 |
#include <lemon/path.h> |
| 33 | 33 |
|
| 34 | 34 |
#include <limits> |
| 35 | 35 |
|
| 36 | 36 |
namespace lemon {
|
| 37 | 37 |
|
| 38 | 38 |
/// \brief Default operation traits for the BellmanFord algorithm class. |
| 39 |
/// |
|
| 39 |
/// |
|
| 40 | 40 |
/// This operation traits class defines all computational operations |
| 41 | 41 |
/// and constants that are used in the Bellman-Ford algorithm. |
| 42 | 42 |
/// The default implementation is based on the \c numeric_limits class. |
| 43 | 43 |
/// If the numeric type does not have infinity value, then the maximum |
| 44 | 44 |
/// value is used as extremal infinity value. |
| 45 | 45 |
/// |
| 46 | 46 |
/// \see BellmanFordToleranceOperationTraits |
| 47 | 47 |
template < |
| 48 |
typename V, |
|
| 48 |
typename V, |
|
| 49 | 49 |
bool has_inf = std::numeric_limits<V>::has_infinity> |
| 50 | 50 |
struct BellmanFordDefaultOperationTraits {
|
| 51 | 51 |
/// \brief Value type for the algorithm. |
| 52 | 52 |
typedef V Value; |
| 53 | 53 |
/// \brief Gives back the zero value of the type. |
| 54 | 54 |
static Value zero() {
|
| 55 | 55 |
return static_cast<Value>(0); |
| 56 | 56 |
} |
| 57 | 57 |
/// \brief Gives back the positive infinity value of the type. |
| 58 | 58 |
static Value infinity() {
|
| 59 | 59 |
return std::numeric_limits<Value>::infinity(); |
| 60 | 60 |
} |
| ... | ... |
@@ -77,25 +77,25 @@ |
| 77 | 77 |
} |
| 78 | 78 |
static Value infinity() {
|
| 79 | 79 |
return std::numeric_limits<Value>::max(); |
| 80 | 80 |
} |
| 81 | 81 |
static Value plus(const Value& left, const Value& right) {
|
| 82 | 82 |
if (left == infinity() || right == infinity()) return infinity(); |
| 83 | 83 |
return left + right; |
| 84 | 84 |
} |
| 85 | 85 |
static bool less(const Value& left, const Value& right) {
|
| 86 | 86 |
return left < right; |
| 87 | 87 |
} |
| 88 | 88 |
}; |
| 89 |
|
|
| 89 |
|
|
| 90 | 90 |
/// \brief Operation traits for the BellmanFord algorithm class |
| 91 | 91 |
/// using tolerance. |
| 92 | 92 |
/// |
| 93 | 93 |
/// This operation traits class defines all computational operations |
| 94 | 94 |
/// and constants that are used in the Bellman-Ford algorithm. |
| 95 | 95 |
/// The only difference between this implementation and |
| 96 | 96 |
/// \ref BellmanFordDefaultOperationTraits is that this class uses |
| 97 | 97 |
/// the \ref Tolerance "tolerance technique" in its \ref less() |
| 98 | 98 |
/// function. |
| 99 | 99 |
/// |
| 100 | 100 |
/// \tparam V The value type. |
| 101 | 101 |
/// \tparam eps The epsilon value for the \ref less() function. |
| ... | ... |
@@ -130,93 +130,93 @@ |
| 130 | 130 |
static bool less(const Value& left, const Value& right) {
|
| 131 | 131 |
return left + eps < right; |
| 132 | 132 |
} |
| 133 | 133 |
}; |
| 134 | 134 |
|
| 135 | 135 |
/// \brief Default traits class of BellmanFord class. |
| 136 | 136 |
/// |
| 137 | 137 |
/// Default traits class of BellmanFord class. |
| 138 | 138 |
/// \param GR The type of the digraph. |
| 139 | 139 |
/// \param LEN The type of the length map. |
| 140 | 140 |
template<typename GR, typename LEN> |
| 141 | 141 |
struct BellmanFordDefaultTraits {
|
| 142 |
/// The type of the digraph the algorithm runs on. |
|
| 142 |
/// The type of the digraph the algorithm runs on. |
|
| 143 | 143 |
typedef GR Digraph; |
| 144 | 144 |
|
| 145 | 145 |
/// \brief The type of the map that stores the arc lengths. |
| 146 | 146 |
/// |
| 147 | 147 |
/// The type of the map that stores the arc lengths. |
| 148 | 148 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
| 149 | 149 |
typedef LEN LengthMap; |
| 150 | 150 |
|
| 151 | 151 |
/// The type of the arc lengths. |
| 152 | 152 |
typedef typename LEN::Value Value; |
| 153 | 153 |
|
| 154 | 154 |
/// \brief Operation traits for Bellman-Ford algorithm. |
| 155 | 155 |
/// |
| 156 | 156 |
/// It defines the used operations and the infinity value for the |
| 157 | 157 |
/// given \c Value type. |
| 158 | 158 |
/// \see BellmanFordDefaultOperationTraits, |
| 159 | 159 |
/// BellmanFordToleranceOperationTraits |
| 160 | 160 |
typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
| 161 |
|
|
| 162 |
/// \brief The type of the map that stores the last arcs of the |
|
| 161 |
|
|
| 162 |
/// \brief The type of the map that stores the last arcs of the |
|
| 163 | 163 |
/// shortest paths. |
| 164 |
/// |
|
| 164 |
/// |
|
| 165 | 165 |
/// The type of the map that stores the last |
| 166 | 166 |
/// arcs of the shortest paths. |
| 167 | 167 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 168 | 168 |
typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
| 169 | 169 |
|
| 170 | 170 |
/// \brief Instantiates a \c PredMap. |
| 171 |
/// |
|
| 172 |
/// This function instantiates a \ref PredMap. |
|
| 171 |
/// |
|
| 172 |
/// This function instantiates a \ref PredMap. |
|
| 173 | 173 |
/// \param g is the digraph to which we would like to define the |
| 174 | 174 |
/// \ref PredMap. |
| 175 | 175 |
static PredMap *createPredMap(const GR& g) {
|
| 176 | 176 |
return new PredMap(g); |
| 177 | 177 |
} |
| 178 | 178 |
|
| 179 | 179 |
/// \brief The type of the map that stores the distances of the nodes. |
| 180 | 180 |
/// |
| 181 | 181 |
/// The type of the map that stores the distances of the nodes. |
| 182 | 182 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 183 | 183 |
typedef typename GR::template NodeMap<typename LEN::Value> DistMap; |
| 184 | 184 |
|
| 185 | 185 |
/// \brief Instantiates a \c DistMap. |
| 186 | 186 |
/// |
| 187 |
/// This function instantiates a \ref DistMap. |
|
| 188 |
/// \param g is the digraph to which we would like to define the |
|
| 187 |
/// This function instantiates a \ref DistMap. |
|
| 188 |
/// \param g is the digraph to which we would like to define the |
|
| 189 | 189 |
/// \ref DistMap. |
| 190 | 190 |
static DistMap *createDistMap(const GR& g) {
|
| 191 | 191 |
return new DistMap(g); |
| 192 | 192 |
} |
| 193 | 193 |
|
| 194 | 194 |
}; |
| 195 |
|
|
| 195 |
|
|
| 196 | 196 |
/// \brief %BellmanFord algorithm class. |
| 197 | 197 |
/// |
| 198 | 198 |
/// \ingroup shortest_path |
| 199 |
/// This class provides an efficient implementation of the Bellman-Ford |
|
| 199 |
/// This class provides an efficient implementation of the Bellman-Ford |
|
| 200 | 200 |
/// algorithm. The maximum time complexity of the algorithm is |
| 201 | 201 |
/// <tt>O(ne)</tt>. |
| 202 | 202 |
/// |
| 203 | 203 |
/// The Bellman-Ford algorithm solves the single-source shortest path |
| 204 | 204 |
/// problem when the arcs can have negative lengths, but the digraph |
| 205 | 205 |
/// should not contain directed cycles with negative total length. |
| 206 | 206 |
/// If all arc costs are non-negative, consider to use the Dijkstra |
| 207 | 207 |
/// algorithm instead, since it is more efficient. |
| 208 | 208 |
/// |
| 209 | 209 |
/// The arc lengths are passed to the algorithm using a |
| 210 |
/// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any |
|
| 210 |
/// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any |
|
| 211 | 211 |
/// kind of length. The type of the length values is determined by the |
| 212 | 212 |
/// \ref concepts::ReadMap::Value "Value" type of the length map. |
| 213 | 213 |
/// |
| 214 | 214 |
/// There is also a \ref bellmanFord() "function-type interface" for the |
| 215 | 215 |
/// Bellman-Ford algorithm, which is convenient in the simplier cases and |
| 216 | 216 |
/// it can be used easier. |
| 217 | 217 |
/// |
| 218 | 218 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 219 | 219 |
/// The default type is \ref ListDigraph. |
| 220 | 220 |
/// \tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies |
| 221 | 221 |
/// the lengths of the arcs. The default map type is |
| 222 | 222 |
/// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| ... | ... |
@@ -228,25 +228,25 @@ |
| 228 | 228 |
#ifdef DOXYGEN |
| 229 | 229 |
template <typename GR, typename LEN, typename TR> |
| 230 | 230 |
#else |
| 231 | 231 |
template <typename GR=ListDigraph, |
| 232 | 232 |
typename LEN=typename GR::template ArcMap<int>, |
| 233 | 233 |
typename TR=BellmanFordDefaultTraits<GR,LEN> > |
| 234 | 234 |
#endif |
| 235 | 235 |
class BellmanFord {
|
| 236 | 236 |
public: |
| 237 | 237 |
|
| 238 | 238 |
///The type of the underlying digraph. |
| 239 | 239 |
typedef typename TR::Digraph Digraph; |
| 240 |
|
|
| 240 |
|
|
| 241 | 241 |
/// \brief The type of the arc lengths. |
| 242 | 242 |
typedef typename TR::LengthMap::Value Value; |
| 243 | 243 |
/// \brief The type of the map that stores the arc lengths. |
| 244 | 244 |
typedef typename TR::LengthMap LengthMap; |
| 245 | 245 |
/// \brief The type of the map that stores the last |
| 246 | 246 |
/// arcs of the shortest paths. |
| 247 | 247 |
typedef typename TR::PredMap PredMap; |
| 248 | 248 |
/// \brief The type of the map that stores the distances of the nodes. |
| 249 | 249 |
typedef typename TR::DistMap DistMap; |
| 250 | 250 |
/// The type of the paths. |
| 251 | 251 |
typedef PredMapPath<Digraph, PredMap> Path; |
| 252 | 252 |
///\brief The \ref BellmanFordDefaultOperationTraits |
| ... | ... |
@@ -275,122 +275,122 @@ |
| 275 | 275 |
DistMap *_dist; |
| 276 | 276 |
// Indicates if _dist is locally allocated (true) or not. |
| 277 | 277 |
bool _local_dist; |
| 278 | 278 |
|
| 279 | 279 |
typedef typename Digraph::template NodeMap<bool> MaskMap; |
| 280 | 280 |
MaskMap *_mask; |
| 281 | 281 |
|
| 282 | 282 |
std::vector<Node> _process; |
| 283 | 283 |
|
| 284 | 284 |
// Creates the maps if necessary. |
| 285 | 285 |
void create_maps() {
|
| 286 | 286 |
if(!_pred) {
|
| 287 |
_local_pred = true; |
|
| 288 |
_pred = Traits::createPredMap(*_gr); |
|
| 287 |
_local_pred = true; |
|
| 288 |
_pred = Traits::createPredMap(*_gr); |
|
| 289 | 289 |
} |
| 290 | 290 |
if(!_dist) {
|
| 291 |
_local_dist = true; |
|
| 292 |
_dist = Traits::createDistMap(*_gr); |
|
| 291 |
_local_dist = true; |
|
| 292 |
_dist = Traits::createDistMap(*_gr); |
|
| 293 | 293 |
} |
| 294 | 294 |
if(!_mask) {
|
| 295 | 295 |
_mask = new MaskMap(*_gr); |
| 296 | 296 |
} |
| 297 | 297 |
} |
| 298 |
|
|
| 298 |
|
|
| 299 | 299 |
public : |
| 300 |
|
|
| 300 |
|
|
| 301 | 301 |
typedef BellmanFord Create; |
| 302 | 302 |
|
| 303 | 303 |
/// \name Named Template Parameters |
| 304 | 304 |
|
| 305 | 305 |
///@{
|
| 306 | 306 |
|
| 307 | 307 |
template <class T> |
| 308 | 308 |
struct SetPredMapTraits : public Traits {
|
| 309 | 309 |
typedef T PredMap; |
| 310 | 310 |
static PredMap *createPredMap(const Digraph&) {
|
| 311 | 311 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
| 312 | 312 |
return 0; // ignore warnings |
| 313 | 313 |
} |
| 314 | 314 |
}; |
| 315 | 315 |
|
| 316 | 316 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 317 | 317 |
/// \c PredMap type. |
| 318 | 318 |
/// |
| 319 | 319 |
/// \ref named-templ-param "Named parameter" for setting |
| 320 | 320 |
/// \c PredMap type. |
| 321 | 321 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 322 | 322 |
template <class T> |
| 323 |
struct SetPredMap |
|
| 323 |
struct SetPredMap |
|
| 324 | 324 |
: public BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > {
|
| 325 | 325 |
typedef BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > Create; |
| 326 | 326 |
}; |
| 327 |
|
|
| 327 |
|
|
| 328 | 328 |
template <class T> |
| 329 | 329 |
struct SetDistMapTraits : public Traits {
|
| 330 | 330 |
typedef T DistMap; |
| 331 | 331 |
static DistMap *createDistMap(const Digraph&) {
|
| 332 | 332 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
| 333 | 333 |
return 0; // ignore warnings |
| 334 | 334 |
} |
| 335 | 335 |
}; |
| 336 | 336 |
|
| 337 | 337 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 338 | 338 |
/// \c DistMap type. |
| 339 | 339 |
/// |
| 340 | 340 |
/// \ref named-templ-param "Named parameter" for setting |
| 341 | 341 |
/// \c DistMap type. |
| 342 | 342 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 343 | 343 |
template <class T> |
| 344 |
struct SetDistMap |
|
| 344 |
struct SetDistMap |
|
| 345 | 345 |
: public BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > {
|
| 346 | 346 |
typedef BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > Create; |
| 347 | 347 |
}; |
| 348 | 348 |
|
| 349 | 349 |
template <class T> |
| 350 | 350 |
struct SetOperationTraitsTraits : public Traits {
|
| 351 | 351 |
typedef T OperationTraits; |
| 352 | 352 |
}; |
| 353 |
|
|
| 354 |
/// \brief \ref named-templ-param "Named parameter" for setting |
|
| 353 |
|
|
| 354 |
/// \brief \ref named-templ-param "Named parameter" for setting |
|
| 355 | 355 |
/// \c OperationTraits type. |
| 356 | 356 |
/// |
| 357 | 357 |
/// \ref named-templ-param "Named parameter" for setting |
| 358 | 358 |
/// \c OperationTraits type. |
| 359 | 359 |
/// For more information, see \ref BellmanFordDefaultOperationTraits. |
| 360 | 360 |
template <class T> |
| 361 | 361 |
struct SetOperationTraits |
| 362 | 362 |
: public BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> > {
|
| 363 | 363 |
typedef BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> > |
| 364 | 364 |
Create; |
| 365 | 365 |
}; |
| 366 |
|
|
| 366 |
|
|
| 367 | 367 |
///@} |
| 368 | 368 |
|
| 369 | 369 |
protected: |
| 370 |
|
|
| 370 |
|
|
| 371 | 371 |
BellmanFord() {}
|
| 372 | 372 |
|
| 373 |
public: |
|
| 374 |
|
|
| 373 |
public: |
|
| 374 |
|
|
| 375 | 375 |
/// \brief Constructor. |
| 376 | 376 |
/// |
| 377 | 377 |
/// Constructor. |
| 378 | 378 |
/// \param g The digraph the algorithm runs on. |
| 379 | 379 |
/// \param length The length map used by the algorithm. |
| 380 | 380 |
BellmanFord(const Digraph& g, const LengthMap& length) : |
| 381 | 381 |
_gr(&g), _length(&length), |
| 382 | 382 |
_pred(0), _local_pred(false), |
| 383 | 383 |
_dist(0), _local_dist(false), _mask(0) {}
|
| 384 |
|
|
| 384 |
|
|
| 385 | 385 |
///Destructor. |
| 386 | 386 |
~BellmanFord() {
|
| 387 | 387 |
if(_local_pred) delete _pred; |
| 388 | 388 |
if(_local_dist) delete _dist; |
| 389 | 389 |
if(_mask) delete _mask; |
| 390 | 390 |
} |
| 391 | 391 |
|
| 392 | 392 |
/// \brief Sets the length map. |
| 393 | 393 |
/// |
| 394 | 394 |
/// Sets the length map. |
| 395 | 395 |
/// \return <tt>(*this)</tt> |
| 396 | 396 |
BellmanFord &lengthMap(const LengthMap &map) {
|
| ... | ... |
@@ -399,273 +399,273 @@ |
| 399 | 399 |
} |
| 400 | 400 |
|
| 401 | 401 |
/// \brief Sets the map that stores the predecessor arcs. |
| 402 | 402 |
/// |
| 403 | 403 |
/// Sets the map that stores the predecessor arcs. |
| 404 | 404 |
/// If you don't use this function before calling \ref run() |
| 405 | 405 |
/// or \ref init(), an instance will be allocated automatically. |
| 406 | 406 |
/// The destructor deallocates this automatically allocated map, |
| 407 | 407 |
/// of course. |
| 408 | 408 |
/// \return <tt>(*this)</tt> |
| 409 | 409 |
BellmanFord &predMap(PredMap &map) {
|
| 410 | 410 |
if(_local_pred) {
|
| 411 |
delete _pred; |
|
| 412 |
_local_pred=false; |
|
| 411 |
delete _pred; |
|
| 412 |
_local_pred=false; |
|
| 413 | 413 |
} |
| 414 | 414 |
_pred = ↦ |
| 415 | 415 |
return *this; |
| 416 | 416 |
} |
| 417 | 417 |
|
| 418 | 418 |
/// \brief Sets the map that stores the distances of the nodes. |
| 419 | 419 |
/// |
| 420 | 420 |
/// Sets the map that stores the distances of the nodes calculated |
| 421 | 421 |
/// by the algorithm. |
| 422 | 422 |
/// If you don't use this function before calling \ref run() |
| 423 | 423 |
/// or \ref init(), an instance will be allocated automatically. |
| 424 | 424 |
/// The destructor deallocates this automatically allocated map, |
| 425 | 425 |
/// of course. |
| 426 | 426 |
/// \return <tt>(*this)</tt> |
| 427 | 427 |
BellmanFord &distMap(DistMap &map) {
|
| 428 | 428 |
if(_local_dist) {
|
| 429 |
delete _dist; |
|
| 430 |
_local_dist=false; |
|
| 429 |
delete _dist; |
|
| 430 |
_local_dist=false; |
|
| 431 | 431 |
} |
| 432 | 432 |
_dist = ↦ |
| 433 | 433 |
return *this; |
| 434 | 434 |
} |
| 435 | 435 |
|
| 436 | 436 |
/// \name Execution Control |
| 437 | 437 |
/// The simplest way to execute the Bellman-Ford algorithm is to use |
| 438 | 438 |
/// one of the member functions called \ref run().\n |
| 439 | 439 |
/// If you need better control on the execution, you have to call |
| 440 | 440 |
/// \ref init() first, then you can add several source nodes |
| 441 | 441 |
/// with \ref addSource(). Finally the actual path computation can be |
| 442 | 442 |
/// performed with \ref start(), \ref checkedStart() or |
| 443 | 443 |
/// \ref limitedStart(). |
| 444 | 444 |
|
| 445 | 445 |
///@{
|
| 446 | 446 |
|
| 447 | 447 |
/// \brief Initializes the internal data structures. |
| 448 |
/// |
|
| 448 |
/// |
|
| 449 | 449 |
/// Initializes the internal data structures. The optional parameter |
| 450 | 450 |
/// is the initial distance of each node. |
| 451 | 451 |
void init(const Value value = OperationTraits::infinity()) {
|
| 452 | 452 |
create_maps(); |
| 453 | 453 |
for (NodeIt it(*_gr); it != INVALID; ++it) {
|
| 454 |
_pred->set(it, INVALID); |
|
| 455 |
_dist->set(it, value); |
|
| 454 |
_pred->set(it, INVALID); |
|
| 455 |
_dist->set(it, value); |
|
| 456 | 456 |
} |
| 457 | 457 |
_process.clear(); |
| 458 | 458 |
if (OperationTraits::less(value, OperationTraits::infinity())) {
|
| 459 |
for (NodeIt it(*_gr); it != INVALID; ++it) {
|
|
| 460 |
_process.push_back(it); |
|
| 461 |
_mask->set(it, true); |
|
| 462 |
} |
|
| 459 |
for (NodeIt it(*_gr); it != INVALID; ++it) {
|
|
| 460 |
_process.push_back(it); |
|
| 461 |
_mask->set(it, true); |
|
| 462 |
} |
|
| 463 | 463 |
} else {
|
| 464 |
for (NodeIt it(*_gr); it != INVALID; ++it) {
|
|
| 465 |
_mask->set(it, false); |
|
| 466 |
|
|
| 464 |
for (NodeIt it(*_gr); it != INVALID; ++it) {
|
|
| 465 |
_mask->set(it, false); |
|
| 466 |
} |
|
| 467 | 467 |
} |
| 468 | 468 |
} |
| 469 |
|
|
| 469 |
|
|
| 470 | 470 |
/// \brief Adds a new source node. |
| 471 | 471 |
/// |
| 472 | 472 |
/// This function adds a new source node. The optional second parameter |
| 473 | 473 |
/// is the initial distance of the node. |
| 474 | 474 |
void addSource(Node source, Value dst = OperationTraits::zero()) {
|
| 475 | 475 |
_dist->set(source, dst); |
| 476 | 476 |
if (!(*_mask)[source]) {
|
| 477 |
_process.push_back(source); |
|
| 478 |
_mask->set(source, true); |
|
| 477 |
_process.push_back(source); |
|
| 478 |
_mask->set(source, true); |
|
| 479 | 479 |
} |
| 480 | 480 |
} |
| 481 | 481 |
|
| 482 | 482 |
/// \brief Executes one round from the Bellman-Ford algorithm. |
| 483 | 483 |
/// |
| 484 | 484 |
/// If the algoritm calculated the distances in the previous round |
| 485 | 485 |
/// exactly for the paths of at most \c k arcs, then this function |
| 486 | 486 |
/// will calculate the distances exactly for the paths of at most |
| 487 | 487 |
/// <tt>k+1</tt> arcs. Performing \c k iterations using this function |
| 488 | 488 |
/// calculates the shortest path distances exactly for the paths |
| 489 | 489 |
/// consisting of at most \c k arcs. |
| 490 | 490 |
/// |
| 491 | 491 |
/// \warning The paths with limited arc number cannot be retrieved |
| 492 | 492 |
/// easily with \ref path() or \ref predArc() functions. If you also |
| 493 | 493 |
/// need the shortest paths and not only the distances, you should |
| 494 | 494 |
/// store the \ref predMap() "predecessor map" after each iteration |
| 495 | 495 |
/// and build the path manually. |
| 496 | 496 |
/// |
| 497 | 497 |
/// \return \c true when the algorithm have not found more shorter |
| 498 | 498 |
/// paths. |
| 499 | 499 |
/// |
| 500 | 500 |
/// \see ActiveIt |
| 501 | 501 |
bool processNextRound() {
|
| 502 | 502 |
for (int i = 0; i < int(_process.size()); ++i) {
|
| 503 |
|
|
| 503 |
_mask->set(_process[i], false); |
|
| 504 | 504 |
} |
| 505 | 505 |
std::vector<Node> nextProcess; |
| 506 | 506 |
std::vector<Value> values(_process.size()); |
| 507 | 507 |
for (int i = 0; i < int(_process.size()); ++i) {
|
| 508 |
|
|
| 508 |
values[i] = (*_dist)[_process[i]]; |
|
| 509 | 509 |
} |
| 510 | 510 |
for (int i = 0; i < int(_process.size()); ++i) {
|
| 511 |
for (OutArcIt it(*_gr, _process[i]); it != INVALID; ++it) {
|
|
| 512 |
Node target = _gr->target(it); |
|
| 513 |
Value relaxed = OperationTraits::plus(values[i], (*_length)[it]); |
|
| 514 |
if (OperationTraits::less(relaxed, (*_dist)[target])) {
|
|
| 515 |
_pred->set(target, it); |
|
| 516 |
_dist->set(target, relaxed); |
|
| 517 |
if (!(*_mask)[target]) {
|
|
| 518 |
_mask->set(target, true); |
|
| 519 |
nextProcess.push_back(target); |
|
| 520 |
} |
|
| 521 |
} |
|
| 522 |
} |
|
| 511 |
for (OutArcIt it(*_gr, _process[i]); it != INVALID; ++it) {
|
|
| 512 |
Node target = _gr->target(it); |
|
| 513 |
Value relaxed = OperationTraits::plus(values[i], (*_length)[it]); |
|
| 514 |
if (OperationTraits::less(relaxed, (*_dist)[target])) {
|
|
| 515 |
_pred->set(target, it); |
|
| 516 |
_dist->set(target, relaxed); |
|
| 517 |
if (!(*_mask)[target]) {
|
|
| 518 |
_mask->set(target, true); |
|
| 519 |
nextProcess.push_back(target); |
|
| 520 |
} |
|
| 521 |
} |
|
| 522 |
} |
|
| 523 | 523 |
} |
| 524 | 524 |
_process.swap(nextProcess); |
| 525 | 525 |
return _process.empty(); |
| 526 | 526 |
} |
| 527 | 527 |
|
| 528 | 528 |
/// \brief Executes one weak round from the Bellman-Ford algorithm. |
| 529 | 529 |
/// |
| 530 | 530 |
/// If the algorithm calculated the distances in the previous round |
| 531 | 531 |
/// at least for the paths of at most \c k arcs, then this function |
| 532 | 532 |
/// will calculate the distances at least for the paths of at most |
| 533 | 533 |
/// <tt>k+1</tt> arcs. |
| 534 | 534 |
/// This function does not make it possible to calculate the shortest |
| 535 | 535 |
/// path distances exactly for paths consisting of at most \c k arcs, |
| 536 | 536 |
/// this is why it is called weak round. |
| 537 | 537 |
/// |
| 538 | 538 |
/// \return \c true when the algorithm have not found more shorter |
| 539 | 539 |
/// paths. |
| 540 | 540 |
/// |
| 541 | 541 |
/// \see ActiveIt |
| 542 | 542 |
bool processNextWeakRound() {
|
| 543 | 543 |
for (int i = 0; i < int(_process.size()); ++i) {
|
| 544 |
|
|
| 544 |
_mask->set(_process[i], false); |
|
| 545 | 545 |
} |
| 546 | 546 |
std::vector<Node> nextProcess; |
| 547 | 547 |
for (int i = 0; i < int(_process.size()); ++i) {
|
| 548 |
for (OutArcIt it(*_gr, _process[i]); it != INVALID; ++it) {
|
|
| 549 |
Node target = _gr->target(it); |
|
| 550 |
Value relaxed = |
|
| 551 |
OperationTraits::plus((*_dist)[_process[i]], (*_length)[it]); |
|
| 552 |
if (OperationTraits::less(relaxed, (*_dist)[target])) {
|
|
| 553 |
_pred->set(target, it); |
|
| 554 |
_dist->set(target, relaxed); |
|
| 555 |
if (!(*_mask)[target]) {
|
|
| 556 |
_mask->set(target, true); |
|
| 557 |
nextProcess.push_back(target); |
|
| 558 |
} |
|
| 559 |
} |
|
| 560 |
|
|
| 548 |
for (OutArcIt it(*_gr, _process[i]); it != INVALID; ++it) {
|
|
| 549 |
Node target = _gr->target(it); |
|
| 550 |
Value relaxed = |
|
| 551 |
OperationTraits::plus((*_dist)[_process[i]], (*_length)[it]); |
|
| 552 |
if (OperationTraits::less(relaxed, (*_dist)[target])) {
|
|
| 553 |
_pred->set(target, it); |
|
| 554 |
_dist->set(target, relaxed); |
|
| 555 |
if (!(*_mask)[target]) {
|
|
| 556 |
_mask->set(target, true); |
|
| 557 |
nextProcess.push_back(target); |
|
| 558 |
} |
|
| 559 |
} |
|
| 560 |
} |
|
| 561 | 561 |
} |
| 562 | 562 |
_process.swap(nextProcess); |
| 563 | 563 |
return _process.empty(); |
| 564 | 564 |
} |
| 565 | 565 |
|
| 566 | 566 |
/// \brief Executes the algorithm. |
| 567 | 567 |
/// |
| 568 | 568 |
/// Executes the algorithm. |
| 569 | 569 |
/// |
| 570 | 570 |
/// This method runs the Bellman-Ford algorithm from the root node(s) |
| 571 | 571 |
/// in order to compute the shortest path to each node. |
| 572 | 572 |
/// |
| 573 | 573 |
/// The algorithm computes |
| 574 | 574 |
/// - the shortest path tree (forest), |
| 575 | 575 |
/// - the distance of each node from the root(s). |
| 576 | 576 |
/// |
| 577 | 577 |
/// \pre init() must be called and at least one root node should be |
| 578 | 578 |
/// added with addSource() before using this function. |
| 579 | 579 |
void start() {
|
| 580 | 580 |
int num = countNodes(*_gr) - 1; |
| 581 | 581 |
for (int i = 0; i < num; ++i) {
|
| 582 |
|
|
| 582 |
if (processNextWeakRound()) break; |
|
| 583 | 583 |
} |
| 584 | 584 |
} |
| 585 | 585 |
|
| 586 | 586 |
/// \brief Executes the algorithm and checks the negative cycles. |
| 587 | 587 |
/// |
| 588 | 588 |
/// Executes the algorithm and checks the negative cycles. |
| 589 | 589 |
/// |
| 590 | 590 |
/// This method runs the Bellman-Ford algorithm from the root node(s) |
| 591 | 591 |
/// in order to compute the shortest path to each node and also checks |
| 592 | 592 |
/// if the digraph contains cycles with negative total length. |
| 593 | 593 |
/// |
| 594 |
/// The algorithm computes |
|
| 594 |
/// The algorithm computes |
|
| 595 | 595 |
/// - the shortest path tree (forest), |
| 596 | 596 |
/// - the distance of each node from the root(s). |
| 597 |
/// |
|
| 597 |
/// |
|
| 598 | 598 |
/// \return \c false if there is a negative cycle in the digraph. |
| 599 | 599 |
/// |
| 600 | 600 |
/// \pre init() must be called and at least one root node should be |
| 601 |
/// added with addSource() before using this function. |
|
| 601 |
/// added with addSource() before using this function. |
|
| 602 | 602 |
bool checkedStart() {
|
| 603 | 603 |
int num = countNodes(*_gr); |
| 604 | 604 |
for (int i = 0; i < num; ++i) {
|
| 605 |
|
|
| 605 |
if (processNextWeakRound()) return true; |
|
| 606 | 606 |
} |
| 607 | 607 |
return _process.empty(); |
| 608 | 608 |
} |
| 609 | 609 |
|
| 610 | 610 |
/// \brief Executes the algorithm with arc number limit. |
| 611 | 611 |
/// |
| 612 | 612 |
/// Executes the algorithm with arc number limit. |
| 613 | 613 |
/// |
| 614 | 614 |
/// This method runs the Bellman-Ford algorithm from the root node(s) |
| 615 | 615 |
/// in order to compute the shortest path distance for each node |
| 616 | 616 |
/// using only the paths consisting of at most \c num arcs. |
| 617 | 617 |
/// |
| 618 | 618 |
/// The algorithm computes |
| 619 | 619 |
/// - the limited distance of each node from the root(s), |
| 620 | 620 |
/// - the predecessor arc for each node. |
| 621 | 621 |
/// |
| 622 | 622 |
/// \warning The paths with limited arc number cannot be retrieved |
| 623 | 623 |
/// easily with \ref path() or \ref predArc() functions. If you also |
| 624 | 624 |
/// need the shortest paths and not only the distances, you should |
| 625 | 625 |
/// store the \ref predMap() "predecessor map" after each iteration |
| 626 | 626 |
/// and build the path manually. |
| 627 | 627 |
/// |
| 628 | 628 |
/// \pre init() must be called and at least one root node should be |
| 629 |
/// added with addSource() before using this function. |
|
| 629 |
/// added with addSource() before using this function. |
|
| 630 | 630 |
void limitedStart(int num) {
|
| 631 | 631 |
for (int i = 0; i < num; ++i) {
|
| 632 |
|
|
| 632 |
if (processNextRound()) break; |
|
| 633 | 633 |
} |
| 634 | 634 |
} |
| 635 |
|
|
| 635 |
|
|
| 636 | 636 |
/// \brief Runs the algorithm from the given root node. |
| 637 |
/// |
|
| 637 |
/// |
|
| 638 | 638 |
/// This method runs the Bellman-Ford algorithm from the given root |
| 639 | 639 |
/// node \c s in order to compute the shortest path to each node. |
| 640 | 640 |
/// |
| 641 | 641 |
/// The algorithm computes |
| 642 | 642 |
/// - the shortest path tree (forest), |
| 643 | 643 |
/// - the distance of each node from the root(s). |
| 644 | 644 |
/// |
| 645 | 645 |
/// \note bf.run(s) is just a shortcut of the following code. |
| 646 | 646 |
/// \code |
| 647 | 647 |
/// bf.init(); |
| 648 | 648 |
/// bf.addSource(s); |
| 649 | 649 |
/// bf.start(); |
| 650 | 650 |
/// \endcode |
| 651 | 651 |
void run(Node s) {
|
| 652 | 652 |
init(); |
| 653 | 653 |
addSource(s); |
| 654 | 654 |
start(); |
| 655 | 655 |
} |
| 656 |
|
|
| 656 |
|
|
| 657 | 657 |
/// \brief Runs the algorithm from the given root node with arc |
| 658 | 658 |
/// number limit. |
| 659 |
/// |
|
| 659 |
/// |
|
| 660 | 660 |
/// This method runs the Bellman-Ford algorithm from the given root |
| 661 | 661 |
/// node \c s in order to compute the shortest path distance for each |
| 662 | 662 |
/// node using only the paths consisting of at most \c num arcs. |
| 663 | 663 |
/// |
| 664 | 664 |
/// The algorithm computes |
| 665 | 665 |
/// - the limited distance of each node from the root(s), |
| 666 | 666 |
/// - the predecessor arc for each node. |
| 667 | 667 |
/// |
| 668 | 668 |
/// \warning The paths with limited arc number cannot be retrieved |
| 669 | 669 |
/// easily with \ref path() or \ref predArc() functions. If you also |
| 670 | 670 |
/// need the shortest paths and not only the distances, you should |
| 671 | 671 |
/// store the \ref predMap() "predecessor map" after each iteration |
| ... | ... |
@@ -673,100 +673,100 @@ |
| 673 | 673 |
/// |
| 674 | 674 |
/// \note bf.run(s, num) is just a shortcut of the following code. |
| 675 | 675 |
/// \code |
| 676 | 676 |
/// bf.init(); |
| 677 | 677 |
/// bf.addSource(s); |
| 678 | 678 |
/// bf.limitedStart(num); |
| 679 | 679 |
/// \endcode |
| 680 | 680 |
void run(Node s, int num) {
|
| 681 | 681 |
init(); |
| 682 | 682 |
addSource(s); |
| 683 | 683 |
limitedStart(num); |
| 684 | 684 |
} |
| 685 |
|
|
| 685 |
|
|
| 686 | 686 |
///@} |
| 687 | 687 |
|
| 688 | 688 |
/// \brief LEMON iterator for getting the active nodes. |
| 689 | 689 |
/// |
| 690 | 690 |
/// This class provides a common style LEMON iterator that traverses |
| 691 | 691 |
/// the active nodes of the Bellman-Ford algorithm after the last |
| 692 | 692 |
/// phase. These nodes should be checked in the next phase to |
| 693 | 693 |
/// find augmenting arcs outgoing from them. |
| 694 | 694 |
class ActiveIt {
|
| 695 | 695 |
public: |
| 696 | 696 |
|
| 697 | 697 |
/// \brief Constructor. |
| 698 | 698 |
/// |
| 699 | 699 |
/// Constructor for getting the active nodes of the given BellmanFord |
| 700 |
/// instance. |
|
| 700 |
/// instance. |
|
| 701 | 701 |
ActiveIt(const BellmanFord& algorithm) : _algorithm(&algorithm) |
| 702 | 702 |
{
|
| 703 | 703 |
_index = _algorithm->_process.size() - 1; |
| 704 | 704 |
} |
| 705 | 705 |
|
| 706 | 706 |
/// \brief Invalid constructor. |
| 707 | 707 |
/// |
| 708 | 708 |
/// Invalid constructor. |
| 709 | 709 |
ActiveIt(Invalid) : _algorithm(0), _index(-1) {}
|
| 710 | 710 |
|
| 711 | 711 |
/// \brief Conversion to \c Node. |
| 712 | 712 |
/// |
| 713 | 713 |
/// Conversion to \c Node. |
| 714 |
operator Node() const {
|
|
| 714 |
operator Node() const {
|
|
| 715 | 715 |
return _index >= 0 ? _algorithm->_process[_index] : INVALID; |
| 716 | 716 |
} |
| 717 | 717 |
|
| 718 | 718 |
/// \brief Increment operator. |
| 719 | 719 |
/// |
| 720 | 720 |
/// Increment operator. |
| 721 | 721 |
ActiveIt& operator++() {
|
| 722 | 722 |
--_index; |
| 723 |
return *this; |
|
| 723 |
return *this; |
|
| 724 | 724 |
} |
| 725 | 725 |
|
| 726 |
bool operator==(const ActiveIt& it) const {
|
|
| 727 |
return static_cast<Node>(*this) == static_cast<Node>(it); |
|
| 726 |
bool operator==(const ActiveIt& it) const {
|
|
| 727 |
return static_cast<Node>(*this) == static_cast<Node>(it); |
|
| 728 | 728 |
} |
| 729 |
bool operator!=(const ActiveIt& it) const {
|
|
| 730 |
return static_cast<Node>(*this) != static_cast<Node>(it); |
|
| 729 |
bool operator!=(const ActiveIt& it) const {
|
|
| 730 |
return static_cast<Node>(*this) != static_cast<Node>(it); |
|
| 731 | 731 |
} |
| 732 |
bool operator<(const ActiveIt& it) const {
|
|
| 733 |
return static_cast<Node>(*this) < static_cast<Node>(it); |
|
| 732 |
bool operator<(const ActiveIt& it) const {
|
|
| 733 |
return static_cast<Node>(*this) < static_cast<Node>(it); |
|
| 734 | 734 |
} |
| 735 |
|
|
| 735 |
|
|
| 736 | 736 |
private: |
| 737 | 737 |
const BellmanFord* _algorithm; |
| 738 | 738 |
int _index; |
| 739 | 739 |
}; |
| 740 |
|
|
| 740 |
|
|
| 741 | 741 |
/// \name Query Functions |
| 742 | 742 |
/// The result of the Bellman-Ford algorithm can be obtained using these |
| 743 | 743 |
/// functions.\n |
| 744 | 744 |
/// Either \ref run() or \ref init() should be called before using them. |
| 745 |
|
|
| 745 |
|
|
| 746 | 746 |
///@{
|
| 747 | 747 |
|
| 748 | 748 |
/// \brief The shortest path to the given node. |
| 749 |
/// |
|
| 749 |
/// |
|
| 750 | 750 |
/// Gives back the shortest path to the given node from the root(s). |
| 751 | 751 |
/// |
| 752 | 752 |
/// \warning \c t should be reached from the root(s). |
| 753 | 753 |
/// |
| 754 | 754 |
/// \pre Either \ref run() or \ref init() must be called before |
| 755 | 755 |
/// using this function. |
| 756 | 756 |
Path path(Node t) const |
| 757 | 757 |
{
|
| 758 | 758 |
return Path(*_gr, *_pred, t); |
| 759 | 759 |
} |
| 760 |
|
|
| 760 |
|
|
| 761 | 761 |
/// \brief The distance of the given node from the root(s). |
| 762 | 762 |
/// |
| 763 | 763 |
/// Returns the distance of the given node from the root(s). |
| 764 | 764 |
/// |
| 765 | 765 |
/// \warning If node \c v is not reached from the root(s), then |
| 766 | 766 |
/// the return value of this function is undefined. |
| 767 | 767 |
/// |
| 768 | 768 |
/// \pre Either \ref run() or \ref init() must be called before |
| 769 | 769 |
/// using this function. |
| 770 | 770 |
Value dist(Node v) const { return (*_dist)[v]; }
|
| 771 | 771 |
|
| 772 | 772 |
/// \brief Returns the 'previous arc' of the shortest path tree for |
| ... | ... |
@@ -788,60 +788,60 @@ |
| 788 | 788 |
/// the given node. |
| 789 | 789 |
/// |
| 790 | 790 |
/// This function returns the 'previous node' of the shortest path |
| 791 | 791 |
/// tree for node \c v, i.e. it returns the last but one node of |
| 792 | 792 |
/// a shortest path from a root to \c v. It is \c INVALID if \c v |
| 793 | 793 |
/// is not reached from the root(s) or if \c v is a root. |
| 794 | 794 |
/// |
| 795 | 795 |
/// The shortest path tree used here is equal to the shortest path |
| 796 | 796 |
/// tree used in \ref predArc() and \ref predMap(). |
| 797 | 797 |
/// |
| 798 | 798 |
/// \pre Either \ref run() or \ref init() must be called before |
| 799 | 799 |
/// using this function. |
| 800 |
Node predNode(Node v) const {
|
|
| 801 |
return (*_pred)[v] == INVALID ? INVALID : _gr->source((*_pred)[v]); |
|
| 800 |
Node predNode(Node v) const {
|
|
| 801 |
return (*_pred)[v] == INVALID ? INVALID : _gr->source((*_pred)[v]); |
|
| 802 | 802 |
} |
| 803 |
|
|
| 803 |
|
|
| 804 | 804 |
/// \brief Returns a const reference to the node map that stores the |
| 805 | 805 |
/// distances of the nodes. |
| 806 | 806 |
/// |
| 807 | 807 |
/// Returns a const reference to the node map that stores the distances |
| 808 | 808 |
/// of the nodes calculated by the algorithm. |
| 809 | 809 |
/// |
| 810 | 810 |
/// \pre Either \ref run() or \ref init() must be called before |
| 811 | 811 |
/// using this function. |
| 812 | 812 |
const DistMap &distMap() const { return *_dist;}
|
| 813 |
|
|
| 813 |
|
|
| 814 | 814 |
/// \brief Returns a const reference to the node map that stores the |
| 815 | 815 |
/// predecessor arcs. |
| 816 | 816 |
/// |
| 817 | 817 |
/// Returns a const reference to the node map that stores the predecessor |
| 818 | 818 |
/// arcs, which form the shortest path tree (forest). |
| 819 | 819 |
/// |
| 820 | 820 |
/// \pre Either \ref run() or \ref init() must be called before |
| 821 | 821 |
/// using this function. |
| 822 | 822 |
const PredMap &predMap() const { return *_pred; }
|
| 823 |
|
|
| 823 |
|
|
| 824 | 824 |
/// \brief Checks if a node is reached from the root(s). |
| 825 | 825 |
/// |
| 826 | 826 |
/// Returns \c true if \c v is reached from the root(s). |
| 827 | 827 |
/// |
| 828 | 828 |
/// \pre Either \ref run() or \ref init() must be called before |
| 829 | 829 |
/// using this function. |
| 830 | 830 |
bool reached(Node v) const {
|
| 831 | 831 |
return (*_dist)[v] != OperationTraits::infinity(); |
| 832 | 832 |
} |
| 833 | 833 |
|
| 834 | 834 |
/// \brief Gives back a negative cycle. |
| 835 |
/// |
|
| 835 |
/// |
|
| 836 | 836 |
/// This function gives back a directed cycle with negative total |
| 837 | 837 |
/// length if the algorithm has already found one. |
| 838 | 838 |
/// Otherwise it gives back an empty path. |
| 839 | 839 |
lemon::Path<Digraph> negativeCycle() const {
|
| 840 | 840 |
typename Digraph::template NodeMap<int> state(*_gr, -1); |
| 841 | 841 |
lemon::Path<Digraph> cycle; |
| 842 | 842 |
for (int i = 0; i < int(_process.size()); ++i) {
|
| 843 | 843 |
if (state[_process[i]] != -1) continue; |
| 844 | 844 |
for (Node v = _process[i]; (*_pred)[v] != INVALID; |
| 845 | 845 |
v = _gr->source((*_pred)[v])) {
|
| 846 | 846 |
if (state[v] == i) {
|
| 847 | 847 |
cycle.addFront((*_pred)[v]); |
| ... | ... |
@@ -850,198 +850,198 @@ |
| 850 | 850 |
cycle.addFront((*_pred)[u]); |
| 851 | 851 |
} |
| 852 | 852 |
return cycle; |
| 853 | 853 |
} |
| 854 | 854 |
else if (state[v] >= 0) {
|
| 855 | 855 |
break; |
| 856 | 856 |
} |
| 857 | 857 |
state[v] = i; |
| 858 | 858 |
} |
| 859 | 859 |
} |
| 860 | 860 |
return cycle; |
| 861 | 861 |
} |
| 862 |
|
|
| 862 |
|
|
| 863 | 863 |
///@} |
| 864 | 864 |
}; |
| 865 |
|
|
| 865 |
|
|
| 866 | 866 |
/// \brief Default traits class of bellmanFord() function. |
| 867 | 867 |
/// |
| 868 | 868 |
/// Default traits class of bellmanFord() function. |
| 869 | 869 |
/// \tparam GR The type of the digraph. |
| 870 | 870 |
/// \tparam LEN The type of the length map. |
| 871 | 871 |
template <typename GR, typename LEN> |
| 872 | 872 |
struct BellmanFordWizardDefaultTraits {
|
| 873 |
/// The type of the digraph the algorithm runs on. |
|
| 873 |
/// The type of the digraph the algorithm runs on. |
|
| 874 | 874 |
typedef GR Digraph; |
| 875 | 875 |
|
| 876 | 876 |
/// \brief The type of the map that stores the arc lengths. |
| 877 | 877 |
/// |
| 878 | 878 |
/// The type of the map that stores the arc lengths. |
| 879 | 879 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
| 880 | 880 |
typedef LEN LengthMap; |
| 881 | 881 |
|
| 882 | 882 |
/// The type of the arc lengths. |
| 883 | 883 |
typedef typename LEN::Value Value; |
| 884 | 884 |
|
| 885 | 885 |
/// \brief Operation traits for Bellman-Ford algorithm. |
| 886 | 886 |
/// |
| 887 | 887 |
/// It defines the used operations and the infinity value for the |
| 888 | 888 |
/// given \c Value type. |
| 889 | 889 |
/// \see BellmanFordDefaultOperationTraits, |
| 890 | 890 |
/// BellmanFordToleranceOperationTraits |
| 891 | 891 |
typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
| 892 | 892 |
|
| 893 | 893 |
/// \brief The type of the map that stores the last |
| 894 | 894 |
/// arcs of the shortest paths. |
| 895 |
/// |
|
| 895 |
/// |
|
| 896 | 896 |
/// The type of the map that stores the last arcs of the shortest paths. |
| 897 | 897 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 898 | 898 |
typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
| 899 | 899 |
|
| 900 | 900 |
/// \brief Instantiates a \c PredMap. |
| 901 |
/// |
|
| 901 |
/// |
|
| 902 | 902 |
/// This function instantiates a \ref PredMap. |
| 903 | 903 |
/// \param g is the digraph to which we would like to define the |
| 904 | 904 |
/// \ref PredMap. |
| 905 | 905 |
static PredMap *createPredMap(const GR &g) {
|
| 906 | 906 |
return new PredMap(g); |
| 907 | 907 |
} |
| 908 | 908 |
|
| 909 | 909 |
/// \brief The type of the map that stores the distances of the nodes. |
| 910 | 910 |
/// |
| 911 | 911 |
/// The type of the map that stores the distances of the nodes. |
| 912 | 912 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 913 | 913 |
typedef typename GR::template NodeMap<Value> DistMap; |
| 914 | 914 |
|
| 915 | 915 |
/// \brief Instantiates a \c DistMap. |
| 916 | 916 |
/// |
| 917 |
/// This function instantiates a \ref DistMap. |
|
| 917 |
/// This function instantiates a \ref DistMap. |
|
| 918 | 918 |
/// \param g is the digraph to which we would like to define the |
| 919 | 919 |
/// \ref DistMap. |
| 920 | 920 |
static DistMap *createDistMap(const GR &g) {
|
| 921 | 921 |
return new DistMap(g); |
| 922 | 922 |
} |
| 923 | 923 |
|
| 924 | 924 |
///The type of the shortest paths. |
| 925 | 925 |
|
| 926 | 926 |
///The type of the shortest paths. |
| 927 | 927 |
///It must meet the \ref concepts::Path "Path" concept. |
| 928 | 928 |
typedef lemon::Path<Digraph> Path; |
| 929 | 929 |
}; |
| 930 |
|
|
| 930 |
|
|
| 931 | 931 |
/// \brief Default traits class used by BellmanFordWizard. |
| 932 | 932 |
/// |
| 933 | 933 |
/// Default traits class used by BellmanFordWizard. |
| 934 | 934 |
/// \tparam GR The type of the digraph. |
| 935 | 935 |
/// \tparam LEN The type of the length map. |
| 936 | 936 |
template <typename GR, typename LEN> |
| 937 |
class BellmanFordWizardBase |
|
| 937 |
class BellmanFordWizardBase |
|
| 938 | 938 |
: public BellmanFordWizardDefaultTraits<GR, LEN> {
|
| 939 | 939 |
|
| 940 | 940 |
typedef BellmanFordWizardDefaultTraits<GR, LEN> Base; |
| 941 | 941 |
protected: |
| 942 | 942 |
// Type of the nodes in the digraph. |
| 943 | 943 |
typedef typename Base::Digraph::Node Node; |
| 944 | 944 |
|
| 945 | 945 |
// Pointer to the underlying digraph. |
| 946 | 946 |
void *_graph; |
| 947 | 947 |
// Pointer to the length map |
| 948 | 948 |
void *_length; |
| 949 | 949 |
// Pointer to the map of predecessors arcs. |
| 950 | 950 |
void *_pred; |
| 951 | 951 |
// Pointer to the map of distances. |
| 952 | 952 |
void *_dist; |
| 953 | 953 |
//Pointer to the shortest path to the target node. |
| 954 | 954 |
void *_path; |
| 955 | 955 |
//Pointer to the distance of the target node. |
| 956 | 956 |
void *_di; |
| 957 | 957 |
|
| 958 | 958 |
public: |
| 959 | 959 |
/// Constructor. |
| 960 |
|
|
| 960 |
|
|
| 961 | 961 |
/// This constructor does not require parameters, it initiates |
| 962 | 962 |
/// all of the attributes to default values \c 0. |
| 963 | 963 |
BellmanFordWizardBase() : |
| 964 | 964 |
_graph(0), _length(0), _pred(0), _dist(0), _path(0), _di(0) {}
|
| 965 | 965 |
|
| 966 | 966 |
/// Constructor. |
| 967 |
|
|
| 967 |
|
|
| 968 | 968 |
/// This constructor requires two parameters, |
| 969 | 969 |
/// others are initiated to \c 0. |
| 970 | 970 |
/// \param gr The digraph the algorithm runs on. |
| 971 | 971 |
/// \param len The length map. |
| 972 |
BellmanFordWizardBase(const GR& gr, |
|
| 973 |
const LEN& len) : |
|
| 974 |
_graph(reinterpret_cast<void*>(const_cast<GR*>(&gr))), |
|
| 975 |
_length(reinterpret_cast<void*>(const_cast<LEN*>(&len))), |
|
| 972 |
BellmanFordWizardBase(const GR& gr, |
|
| 973 |
const LEN& len) : |
|
| 974 |
_graph(reinterpret_cast<void*>(const_cast<GR*>(&gr))), |
|
| 975 |
_length(reinterpret_cast<void*>(const_cast<LEN*>(&len))), |
|
| 976 | 976 |
_pred(0), _dist(0), _path(0), _di(0) {}
|
| 977 | 977 |
|
| 978 | 978 |
}; |
| 979 |
|
|
| 979 |
|
|
| 980 | 980 |
/// \brief Auxiliary class for the function-type interface of the |
| 981 | 981 |
/// \ref BellmanFord "Bellman-Ford" algorithm. |
| 982 | 982 |
/// |
| 983 | 983 |
/// This auxiliary class is created to implement the |
| 984 | 984 |
/// \ref bellmanFord() "function-type interface" of the |
| 985 | 985 |
/// \ref BellmanFord "Bellman-Ford" algorithm. |
| 986 | 986 |
/// It does not have own \ref run() method, it uses the |
| 987 | 987 |
/// functions and features of the plain \ref BellmanFord. |
| 988 | 988 |
/// |
| 989 | 989 |
/// This class should only be used through the \ref bellmanFord() |
| 990 | 990 |
/// function, which makes it easier to use the algorithm. |
| 991 | 991 |
/// |
| 992 | 992 |
/// \tparam TR The traits class that defines various types used by the |
| 993 | 993 |
/// algorithm. |
| 994 | 994 |
template<class TR> |
| 995 | 995 |
class BellmanFordWizard : public TR {
|
| 996 | 996 |
typedef TR Base; |
| 997 | 997 |
|
| 998 | 998 |
typedef typename TR::Digraph Digraph; |
| 999 | 999 |
|
| 1000 | 1000 |
typedef typename Digraph::Node Node; |
| 1001 | 1001 |
typedef typename Digraph::NodeIt NodeIt; |
| 1002 | 1002 |
typedef typename Digraph::Arc Arc; |
| 1003 | 1003 |
typedef typename Digraph::OutArcIt ArcIt; |
| 1004 |
|
|
| 1004 |
|
|
| 1005 | 1005 |
typedef typename TR::LengthMap LengthMap; |
| 1006 | 1006 |
typedef typename LengthMap::Value Value; |
| 1007 | 1007 |
typedef typename TR::PredMap PredMap; |
| 1008 | 1008 |
typedef typename TR::DistMap DistMap; |
| 1009 | 1009 |
typedef typename TR::Path Path; |
| 1010 | 1010 |
|
| 1011 | 1011 |
public: |
| 1012 | 1012 |
/// Constructor. |
| 1013 | 1013 |
BellmanFordWizard() : TR() {}
|
| 1014 | 1014 |
|
| 1015 | 1015 |
/// \brief Constructor that requires parameters. |
| 1016 | 1016 |
/// |
| 1017 | 1017 |
/// Constructor that requires parameters. |
| 1018 | 1018 |
/// These parameters will be the default values for the traits class. |
| 1019 | 1019 |
/// \param gr The digraph the algorithm runs on. |
| 1020 | 1020 |
/// \param len The length map. |
| 1021 |
BellmanFordWizard(const Digraph& gr, const LengthMap& len) |
|
| 1021 |
BellmanFordWizard(const Digraph& gr, const LengthMap& len) |
|
| 1022 | 1022 |
: TR(gr, len) {}
|
| 1023 | 1023 |
|
| 1024 | 1024 |
/// \brief Copy constructor |
| 1025 | 1025 |
BellmanFordWizard(const TR &b) : TR(b) {}
|
| 1026 | 1026 |
|
| 1027 | 1027 |
~BellmanFordWizard() {}
|
| 1028 | 1028 |
|
| 1029 | 1029 |
/// \brief Runs the Bellman-Ford algorithm from the given source node. |
| 1030 |
/// |
|
| 1030 |
/// |
|
| 1031 | 1031 |
/// This method runs the Bellman-Ford algorithm from the given source |
| 1032 | 1032 |
/// node in order to compute the shortest path to each node. |
| 1033 | 1033 |
void run(Node s) {
|
| 1034 |
BellmanFord<Digraph,LengthMap,TR> |
|
| 1035 |
bf(*reinterpret_cast<const Digraph*>(Base::_graph), |
|
| 1034 |
BellmanFord<Digraph,LengthMap,TR> |
|
| 1035 |
bf(*reinterpret_cast<const Digraph*>(Base::_graph), |
|
| 1036 | 1036 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
| 1037 | 1037 |
if (Base::_pred) bf.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 1038 | 1038 |
if (Base::_dist) bf.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 1039 | 1039 |
bf.run(s); |
| 1040 | 1040 |
} |
| 1041 | 1041 |
|
| 1042 | 1042 |
/// \brief Runs the Bellman-Ford algorithm to find the shortest path |
| 1043 | 1043 |
/// between \c s and \c t. |
| 1044 | 1044 |
/// |
| 1045 | 1045 |
/// This method runs the Bellman-Ford algorithm from node \c s |
| 1046 | 1046 |
/// in order to compute the shortest path to node \c t. |
| 1047 | 1047 |
/// Actually, it computes the shortest path to each node, but using |
| ... | ... |
@@ -1058,43 +1058,43 @@ |
| 1058 | 1058 |
bf.run(s); |
| 1059 | 1059 |
if (Base::_path) *reinterpret_cast<Path*>(Base::_path) = bf.path(t); |
| 1060 | 1060 |
if (Base::_di) *reinterpret_cast<Value*>(Base::_di) = bf.dist(t); |
| 1061 | 1061 |
return bf.reached(t); |
| 1062 | 1062 |
} |
| 1063 | 1063 |
|
| 1064 | 1064 |
template<class T> |
| 1065 | 1065 |
struct SetPredMapBase : public Base {
|
| 1066 | 1066 |
typedef T PredMap; |
| 1067 | 1067 |
static PredMap *createPredMap(const Digraph &) { return 0; };
|
| 1068 | 1068 |
SetPredMapBase(const TR &b) : TR(b) {}
|
| 1069 | 1069 |
}; |
| 1070 |
|
|
| 1070 |
|
|
| 1071 | 1071 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 1072 | 1072 |
/// the predecessor map. |
| 1073 | 1073 |
/// |
| 1074 | 1074 |
/// \ref named-templ-param "Named parameter" for setting |
| 1075 | 1075 |
/// the map that stores the predecessor arcs of the nodes. |
| 1076 | 1076 |
template<class T> |
| 1077 | 1077 |
BellmanFordWizard<SetPredMapBase<T> > predMap(const T &t) {
|
| 1078 | 1078 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1079 | 1079 |
return BellmanFordWizard<SetPredMapBase<T> >(*this); |
| 1080 | 1080 |
} |
| 1081 |
|
|
| 1081 |
|
|
| 1082 | 1082 |
template<class T> |
| 1083 | 1083 |
struct SetDistMapBase : public Base {
|
| 1084 | 1084 |
typedef T DistMap; |
| 1085 | 1085 |
static DistMap *createDistMap(const Digraph &) { return 0; };
|
| 1086 | 1086 |
SetDistMapBase(const TR &b) : TR(b) {}
|
| 1087 | 1087 |
}; |
| 1088 |
|
|
| 1088 |
|
|
| 1089 | 1089 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 1090 | 1090 |
/// the distance map. |
| 1091 | 1091 |
/// |
| 1092 | 1092 |
/// \ref named-templ-param "Named parameter" for setting |
| 1093 | 1093 |
/// the map that stores the distances of the nodes calculated |
| 1094 | 1094 |
/// by the algorithm. |
| 1095 | 1095 |
template<class T> |
| 1096 | 1096 |
BellmanFordWizard<SetDistMapBase<T> > distMap(const T &t) {
|
| 1097 | 1097 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1098 | 1098 |
return BellmanFordWizard<SetDistMapBase<T> >(*this); |
| 1099 | 1099 |
} |
| 1100 | 1100 |
|
| ... | ... |
@@ -1117,49 +1117,49 @@ |
| 1117 | 1117 |
} |
| 1118 | 1118 |
|
| 1119 | 1119 |
/// \brief \ref named-func-param "Named parameter" for getting |
| 1120 | 1120 |
/// the distance of the target node. |
| 1121 | 1121 |
/// |
| 1122 | 1122 |
/// \ref named-func-param "Named parameter" for getting |
| 1123 | 1123 |
/// the distance of the target node. |
| 1124 | 1124 |
BellmanFordWizard dist(const Value &d) |
| 1125 | 1125 |
{
|
| 1126 | 1126 |
Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d)); |
| 1127 | 1127 |
return *this; |
| 1128 | 1128 |
} |
| 1129 |
|
|
| 1129 |
|
|
| 1130 | 1130 |
}; |
| 1131 |
|
|
| 1131 |
|
|
| 1132 | 1132 |
/// \brief Function type interface for the \ref BellmanFord "Bellman-Ford" |
| 1133 | 1133 |
/// algorithm. |
| 1134 | 1134 |
/// |
| 1135 | 1135 |
/// \ingroup shortest_path |
| 1136 | 1136 |
/// Function type interface for the \ref BellmanFord "Bellman-Ford" |
| 1137 | 1137 |
/// algorithm. |
| 1138 | 1138 |
/// |
| 1139 |
/// This function also has several \ref named-templ-func-param |
|
| 1140 |
/// "named parameters", they are declared as the members of class |
|
| 1139 |
/// This function also has several \ref named-templ-func-param |
|
| 1140 |
/// "named parameters", they are declared as the members of class |
|
| 1141 | 1141 |
/// \ref BellmanFordWizard. |
| 1142 | 1142 |
/// The following examples show how to use these parameters. |
| 1143 | 1143 |
/// \code |
| 1144 | 1144 |
/// // Compute shortest path from node s to each node |
| 1145 | 1145 |
/// bellmanFord(g,length).predMap(preds).distMap(dists).run(s); |
| 1146 | 1146 |
/// |
| 1147 | 1147 |
/// // Compute shortest path from s to t |
| 1148 | 1148 |
/// bool reached = bellmanFord(g,length).path(p).dist(d).run(s,t); |
| 1149 | 1149 |
/// \endcode |
| 1150 | 1150 |
/// \warning Don't forget to put the \ref BellmanFordWizard::run() "run()" |
| 1151 | 1151 |
/// to the end of the parameter list. |
| 1152 | 1152 |
/// \sa BellmanFordWizard |
| 1153 | 1153 |
/// \sa BellmanFord |
| 1154 | 1154 |
template<typename GR, typename LEN> |
| 1155 | 1155 |
BellmanFordWizard<BellmanFordWizardBase<GR,LEN> > |
| 1156 | 1156 |
bellmanFord(const GR& digraph, |
| 1157 |
|
|
| 1157 |
const LEN& length) |
|
| 1158 | 1158 |
{
|
| 1159 | 1159 |
return BellmanFordWizard<BellmanFordWizardBase<GR,LEN> >(digraph, length); |
| 1160 | 1160 |
} |
| 1161 | 1161 |
|
| 1162 | 1162 |
} //END OF NAMESPACE LEMON |
| 1163 | 1163 |
|
| 1164 | 1164 |
#endif |
| 1165 | 1165 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -73,25 +73,26 @@ |
| 73 | 73 |
#ifdef DOXYGEN |
| 74 | 74 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 75 | 75 |
#else |
| 76 | 76 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 77 | 77 |
#endif |
| 78 | 78 |
{
|
| 79 | 79 |
return new ProcessedMap(); |
| 80 | 80 |
} |
| 81 | 81 |
|
| 82 | 82 |
///The type of the map that indicates which nodes are reached. |
| 83 | 83 |
|
| 84 | 84 |
///The type of the map that indicates which nodes are reached. |
| 85 |
///It must conform to |
|
| 85 |
///It must conform to |
|
| 86 |
///the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
| 86 | 87 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 87 | 88 |
///Instantiates a \c ReachedMap. |
| 88 | 89 |
|
| 89 | 90 |
///This function instantiates a \ref ReachedMap. |
| 90 | 91 |
///\param g is the digraph, to which |
| 91 | 92 |
///we would like to define the \ref ReachedMap. |
| 92 | 93 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 93 | 94 |
{
|
| 94 | 95 |
return new ReachedMap(g); |
| 95 | 96 |
} |
| 96 | 97 |
|
| 97 | 98 |
///The type of the map that stores the distances of the nodes. |
| ... | ... |
@@ -262,25 +263,26 @@ |
| 262 | 263 |
typedef T ReachedMap; |
| 263 | 264 |
static ReachedMap *createReachedMap(const Digraph &) |
| 264 | 265 |
{
|
| 265 | 266 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
| 266 | 267 |
return 0; // ignore warnings |
| 267 | 268 |
} |
| 268 | 269 |
}; |
| 269 | 270 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 270 | 271 |
///\c ReachedMap type. |
| 271 | 272 |
/// |
| 272 | 273 |
///\ref named-templ-param "Named parameter" for setting |
| 273 | 274 |
///\c ReachedMap type. |
| 274 |
///It must conform to |
|
| 275 |
///It must conform to |
|
| 276 |
///the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
| 275 | 277 |
template <class T> |
| 276 | 278 |
struct SetReachedMap : public Bfs< Digraph, SetReachedMapTraits<T> > {
|
| 277 | 279 |
typedef Bfs< Digraph, SetReachedMapTraits<T> > Create; |
| 278 | 280 |
}; |
| 279 | 281 |
|
| 280 | 282 |
template <class T> |
| 281 | 283 |
struct SetProcessedMapTraits : public Traits {
|
| 282 | 284 |
typedef T ProcessedMap; |
| 283 | 285 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 284 | 286 |
{
|
| 285 | 287 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
| 286 | 288 |
return 0; // ignore warnings |
| ... | ... |
@@ -863,25 +865,26 @@ |
| 863 | 865 |
#ifdef DOXYGEN |
| 864 | 866 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 865 | 867 |
#else |
| 866 | 868 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 867 | 869 |
#endif |
| 868 | 870 |
{
|
| 869 | 871 |
return new ProcessedMap(); |
| 870 | 872 |
} |
| 871 | 873 |
|
| 872 | 874 |
///The type of the map that indicates which nodes are reached. |
| 873 | 875 |
|
| 874 | 876 |
///The type of the map that indicates which nodes are reached. |
| 875 |
///It must conform to |
|
| 877 |
///It must conform to |
|
| 878 |
///the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
| 876 | 879 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 877 | 880 |
///Instantiates a ReachedMap. |
| 878 | 881 |
|
| 879 | 882 |
///This function instantiates a ReachedMap. |
| 880 | 883 |
///\param g is the digraph, to which |
| 881 | 884 |
///we would like to define the ReachedMap. |
| 882 | 885 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 883 | 886 |
{
|
| 884 | 887 |
return new ReachedMap(g); |
| 885 | 888 |
} |
| 886 | 889 |
|
| 887 | 890 |
///The type of the map that stores the distances of the nodes. |
| ... | ... |
@@ -1256,25 +1259,26 @@ |
| 1256 | 1259 |
/// |
| 1257 | 1260 |
/// Default traits class of BfsVisit class. |
| 1258 | 1261 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 1259 | 1262 |
template<class GR> |
| 1260 | 1263 |
struct BfsVisitDefaultTraits {
|
| 1261 | 1264 |
|
| 1262 | 1265 |
/// \brief The type of the digraph the algorithm runs on. |
| 1263 | 1266 |
typedef GR Digraph; |
| 1264 | 1267 |
|
| 1265 | 1268 |
/// \brief The type of the map that indicates which nodes are reached. |
| 1266 | 1269 |
/// |
| 1267 | 1270 |
/// The type of the map that indicates which nodes are reached. |
| 1268 |
/// It must conform to |
|
| 1271 |
/// It must conform to |
|
| 1272 |
///the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
| 1269 | 1273 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 1270 | 1274 |
|
| 1271 | 1275 |
/// \brief Instantiates a ReachedMap. |
| 1272 | 1276 |
/// |
| 1273 | 1277 |
/// This function instantiates a ReachedMap. |
| 1274 | 1278 |
/// \param digraph is the digraph, to which |
| 1275 | 1279 |
/// we would like to define the ReachedMap. |
| 1276 | 1280 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1277 | 1281 |
return new ReachedMap(digraph); |
| 1278 | 1282 |
} |
| 1279 | 1283 |
|
| 1280 | 1284 |
}; |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -249,25 +249,25 @@ |
| 249 | 249 |
} |
| 250 | 250 |
|
| 251 | 251 |
/// \brief Decrease the priority of an item to the given value. |
| 252 | 252 |
/// |
| 253 | 253 |
/// This function decreases the priority of an item to the given value. |
| 254 | 254 |
/// \param item The item. |
| 255 | 255 |
/// \param value The priority. |
| 256 | 256 |
/// \pre \e item must be stored in the heap with priority at least \e value. |
| 257 | 257 |
void decrease (Item item, const Prio& value) {
|
| 258 | 258 |
int i=_iim[item]; |
| 259 | 259 |
int p=_data[i].parent; |
| 260 | 260 |
_data[i].prio=value; |
| 261 |
|
|
| 261 |
|
|
| 262 | 262 |
while( p!=-1 && _comp(value, _data[p].prio) ) {
|
| 263 | 263 |
_data[i].name=_data[p].name; |
| 264 | 264 |
_data[i].prio=_data[p].prio; |
| 265 | 265 |
_data[p].name=item; |
| 266 | 266 |
_data[p].prio=value; |
| 267 | 267 |
_iim[_data[i].name]=i; |
| 268 | 268 |
i=p; |
| 269 | 269 |
p=_data[p].parent; |
| 270 | 270 |
} |
| 271 | 271 |
_iim[item]=i; |
| 272 | 272 |
if ( _comp(value, _data[_min].prio) ) _min=i; |
| 273 | 273 |
} |
| ... | ... |
@@ -313,25 +313,25 @@ |
| 313 | 313 |
case PRE_HEAP: |
| 314 | 314 |
if (state(i) == IN_HEAP) {
|
| 315 | 315 |
erase(i); |
| 316 | 316 |
} |
| 317 | 317 |
_iim[i] = st; |
| 318 | 318 |
break; |
| 319 | 319 |
case IN_HEAP: |
| 320 | 320 |
break; |
| 321 | 321 |
} |
| 322 | 322 |
} |
| 323 | 323 |
|
| 324 | 324 |
private: |
| 325 |
|
|
| 325 |
|
|
| 326 | 326 |
// Find the minimum of the roots |
| 327 | 327 |
int findMin() {
|
| 328 | 328 |
if( _head!=-1 ) {
|
| 329 | 329 |
int min_loc=_head, min_val=_data[_head].prio; |
| 330 | 330 |
for( int x=_data[_head].right_neighbor; x!=-1; |
| 331 | 331 |
x=_data[x].right_neighbor ) {
|
| 332 | 332 |
if( _comp( _data[x].prio,min_val ) ) {
|
| 333 | 333 |
min_val=_data[x].prio; |
| 334 | 334 |
min_loc=x; |
| 335 | 335 |
} |
| 336 | 336 |
} |
| 337 | 337 |
return min_loc; |
| ... | ... |
@@ -341,25 +341,25 @@ |
| 341 | 341 |
|
| 342 | 342 |
// Merge the heap with another heap starting at the given position |
| 343 | 343 |
void merge(int a) {
|
| 344 | 344 |
if( _head==-1 || a==-1 ) return; |
| 345 | 345 |
if( _data[a].right_neighbor==-1 && |
| 346 | 346 |
_data[a].degree<=_data[_head].degree ) {
|
| 347 | 347 |
_data[a].right_neighbor=_head; |
| 348 | 348 |
_head=a; |
| 349 | 349 |
} else {
|
| 350 | 350 |
interleave(a); |
| 351 | 351 |
} |
| 352 | 352 |
if( _data[_head].right_neighbor==-1 ) return; |
| 353 |
|
|
| 353 |
|
|
| 354 | 354 |
int x=_head; |
| 355 | 355 |
int x_prev=-1, x_next=_data[x].right_neighbor; |
| 356 | 356 |
while( x_next!=-1 ) {
|
| 357 | 357 |
if( _data[x].degree!=_data[x_next].degree || |
| 358 | 358 |
( _data[x_next].right_neighbor!=-1 && |
| 359 | 359 |
_data[_data[x_next].right_neighbor].degree==_data[x].degree ) ) {
|
| 360 | 360 |
x_prev=x; |
| 361 | 361 |
x=x_next; |
| 362 | 362 |
} |
| 363 | 363 |
else {
|
| 364 | 364 |
if( _comp(_data[x_next].prio,_data[x].prio) ) {
|
| 365 | 365 |
if( x_prev==-1 ) {
|
| ... | ... |
@@ -375,38 +375,38 @@ |
| 375 | 375 |
fuse(x_next,x); |
| 376 | 376 |
} |
| 377 | 377 |
} |
| 378 | 378 |
x_next=_data[x].right_neighbor; |
| 379 | 379 |
} |
| 380 | 380 |
} |
| 381 | 381 |
|
| 382 | 382 |
// Interleave the elements of the given list into the list of the roots |
| 383 | 383 |
void interleave(int a) {
|
| 384 | 384 |
int p=_head, q=a; |
| 385 | 385 |
int curr=_data.size(); |
| 386 | 386 |
_data.push_back(Store()); |
| 387 |
|
|
| 387 |
|
|
| 388 | 388 |
while( p!=-1 || q!=-1 ) {
|
| 389 | 389 |
if( q==-1 || ( p!=-1 && _data[p].degree<_data[q].degree ) ) {
|
| 390 | 390 |
_data[curr].right_neighbor=p; |
| 391 | 391 |
curr=p; |
| 392 | 392 |
p=_data[p].right_neighbor; |
| 393 | 393 |
} |
| 394 | 394 |
else {
|
| 395 | 395 |
_data[curr].right_neighbor=q; |
| 396 | 396 |
curr=q; |
| 397 | 397 |
q=_data[q].right_neighbor; |
| 398 | 398 |
} |
| 399 | 399 |
} |
| 400 |
|
|
| 400 |
|
|
| 401 | 401 |
_head=_data.back().right_neighbor; |
| 402 | 402 |
_data.pop_back(); |
| 403 | 403 |
} |
| 404 | 404 |
|
| 405 | 405 |
// Lace node a under node b |
| 406 | 406 |
void fuse(int a, int b) {
|
| 407 | 407 |
_data[a].parent=b; |
| 408 | 408 |
_data[a].right_neighbor=_data[b].child; |
| 409 | 409 |
_data[b].child=a; |
| 410 | 410 |
|
| 411 | 411 |
++_data[b].degree; |
| 412 | 412 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -61,25 +61,25 @@ |
| 61 | 61 |
// The const reference type of the map. |
| 62 | 62 |
typedef const _Value& ConstReference; |
| 63 | 63 |
// The reference type of the map. |
| 64 | 64 |
typedef _Value& Reference; |
| 65 | 65 |
|
| 66 | 66 |
// The map type. |
| 67 | 67 |
typedef ArrayMap Map; |
| 68 | 68 |
|
| 69 | 69 |
// The notifier type. |
| 70 | 70 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
| 71 | 71 |
|
| 72 | 72 |
private: |
| 73 |
|
|
| 73 |
|
|
| 74 | 74 |
// The MapBase of the Map which imlements the core regisitry function. |
| 75 | 75 |
typedef typename Notifier::ObserverBase Parent; |
| 76 | 76 |
|
| 77 | 77 |
typedef std::allocator<Value> Allocator; |
| 78 | 78 |
|
| 79 | 79 |
public: |
| 80 | 80 |
|
| 81 | 81 |
// \brief Graph initialized map constructor. |
| 82 | 82 |
// |
| 83 | 83 |
// Graph initialized map constructor. |
| 84 | 84 |
explicit ArrayMap(const GraphType& graph) {
|
| 85 | 85 |
Parent::attach(graph.notifier(Item())); |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -148,25 +148,25 @@ |
| 148 | 148 |
// }; |
| 149 | 149 |
|
| 150 | 150 |
// #endif |
| 151 | 151 |
|
| 152 | 152 |
// DefaultMap class |
| 153 | 153 |
template <typename _Graph, typename _Item, typename _Value> |
| 154 | 154 |
class DefaultMap |
| 155 | 155 |
: public DefaultMapSelector<_Graph, _Item, _Value>::Map {
|
| 156 | 156 |
typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent; |
| 157 | 157 |
|
| 158 | 158 |
public: |
| 159 | 159 |
typedef DefaultMap<_Graph, _Item, _Value> Map; |
| 160 |
|
|
| 160 |
|
|
| 161 | 161 |
typedef typename Parent::GraphType GraphType; |
| 162 | 162 |
typedef typename Parent::Value Value; |
| 163 | 163 |
|
| 164 | 164 |
explicit DefaultMap(const GraphType& graph) : Parent(graph) {}
|
| 165 | 165 |
DefaultMap(const GraphType& graph, const Value& value) |
| 166 | 166 |
: Parent(graph, value) {}
|
| 167 | 167 |
|
| 168 | 168 |
DefaultMap& operator=(const DefaultMap& cmap) {
|
| 169 | 169 |
return operator=<DefaultMap>(cmap); |
| 170 | 170 |
} |
| 171 | 171 |
|
| 172 | 172 |
template <typename CMap> |
| 1 |
/* -*- C++ -*- |
|
| 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
| 2 | 2 |
* |
| 3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
| 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -54,141 +54,141 @@ |
| 54 | 54 |
} |
| 55 | 55 |
|
| 56 | 56 |
Node fromId(int id, Node) const {
|
| 57 | 57 |
return Parent::nodeFromId(id); |
| 58 | 58 |
} |
| 59 | 59 |
|
| 60 | 60 |
Arc fromId(int id, Arc) const {
|
| 61 | 61 |
return Parent::arcFromId(id); |
| 62 | 62 |
} |
| 63 | 63 |
|
| 64 | 64 |
Node oppositeNode(const Node &n, const Arc &e) const {
|
| 65 | 65 |
if (n == Parent::source(e)) |
| 66 |
|
|
| 66 |
return Parent::target(e); |
|
| 67 | 67 |
else if(n==Parent::target(e)) |
| 68 |
|
|
| 68 |
return Parent::source(e); |
|
| 69 | 69 |
else |
| 70 |
|
|
| 70 |
return INVALID; |
|
| 71 | 71 |
} |
| 72 | 72 |
|
| 73 | 73 |
|
| 74 | 74 |
// Alteration notifier extensions |
| 75 | 75 |
|
| 76 | 76 |
// The arc observer registry. |
| 77 | 77 |
typedef AlterationNotifier<ArcSetExtender, Arc> ArcNotifier; |
| 78 | 78 |
|
| 79 | 79 |
protected: |
| 80 | 80 |
|
| 81 | 81 |
mutable ArcNotifier arc_notifier; |
| 82 | 82 |
|
| 83 | 83 |
public: |
| 84 | 84 |
|
| 85 | 85 |
using Parent::notifier; |
| 86 | 86 |
|
| 87 | 87 |
// Gives back the arc alteration notifier. |
| 88 | 88 |
ArcNotifier& notifier(Arc) const {
|
| 89 | 89 |
return arc_notifier; |
| 90 | 90 |
} |
| 91 | 91 |
|
| 92 | 92 |
// Iterable extensions |
| 93 | 93 |
|
| 94 |
class NodeIt : public Node {
|
|
| 94 |
class NodeIt : public Node {
|
|
| 95 | 95 |
const Digraph* digraph; |
| 96 | 96 |
public: |
| 97 | 97 |
|
| 98 | 98 |
NodeIt() {}
|
| 99 | 99 |
|
| 100 | 100 |
NodeIt(Invalid i) : Node(i) { }
|
| 101 | 101 |
|
| 102 | 102 |
explicit NodeIt(const Digraph& _graph) : digraph(&_graph) {
|
| 103 |
|
|
| 103 |
_graph.first(static_cast<Node&>(*this)); |
|
| 104 | 104 |
} |
| 105 | 105 |
|
| 106 |
NodeIt(const Digraph& _graph, const Node& node) |
|
| 107 |
: Node(node), digraph(&_graph) {}
|
|
| 106 |
NodeIt(const Digraph& _graph, const Node& node) |
|
| 107 |
: Node(node), digraph(&_graph) {}
|
|
| 108 | 108 |
|
| 109 |
NodeIt& operator++() {
|
|
| 110 |
digraph->next(*this); |
|
| 111 |
|
|
| 109 |
NodeIt& operator++() {
|
|
| 110 |
digraph->next(*this); |
|
| 111 |
return *this; |
|
| 112 | 112 |
} |
| 113 | 113 |
|
| 114 | 114 |
}; |
| 115 | 115 |
|
| 116 | 116 |
|
| 117 |
class ArcIt : public Arc {
|
|
| 117 |
class ArcIt : public Arc {
|
|
| 118 | 118 |
const Digraph* digraph; |
| 119 | 119 |
public: |
| 120 | 120 |
|
| 121 | 121 |
ArcIt() { }
|
| 122 | 122 |
|
| 123 | 123 |
ArcIt(Invalid i) : Arc(i) { }
|
| 124 | 124 |
|
| 125 | 125 |
explicit ArcIt(const Digraph& _graph) : digraph(&_graph) {
|
| 126 |
|
|
| 126 |
_graph.first(static_cast<Arc&>(*this)); |
|
| 127 | 127 |
} |
| 128 | 128 |
|
| 129 |
ArcIt(const Digraph& _graph, const Arc& e) : |
|
| 130 |
Arc(e), digraph(&_graph) { }
|
|
| 129 |
ArcIt(const Digraph& _graph, const Arc& e) : |
|
| 130 |
Arc(e), digraph(&_graph) { }
|
|
| 131 | 131 |
|
| 132 |
ArcIt& operator++() {
|
|
| 133 |
digraph->next(*this); |
|
| 134 |
|
|
| 132 |
ArcIt& operator++() {
|
|
| 133 |
digraph->next(*this); |
|
| 134 |
return *this; |
|
| 135 | 135 |
} |
| 136 | 136 |
|
| 137 | 137 |
}; |
| 138 | 138 |
|
| 139 | 139 |
|
| 140 |
class OutArcIt : public Arc {
|
|
| 140 |
class OutArcIt : public Arc {
|
|
| 141 | 141 |
const Digraph* digraph; |
| 142 | 142 |
public: |
| 143 | 143 |
|
| 144 | 144 |
OutArcIt() { }
|
| 145 | 145 |
|
| 146 | 146 |
OutArcIt(Invalid i) : Arc(i) { }
|
| 147 | 147 |
|
| 148 |
OutArcIt(const Digraph& _graph, const Node& node) |
|
| 149 |
: digraph(&_graph) {
|
|
| 150 |
|
|
| 148 |
OutArcIt(const Digraph& _graph, const Node& node) |
|
| 149 |
: digraph(&_graph) {
|
|
| 150 |
_graph.firstOut(*this, node); |
|
| 151 | 151 |
} |
| 152 | 152 |
|
| 153 |
OutArcIt(const Digraph& _graph, const Arc& arc) |
|
| 154 |
: Arc(arc), digraph(&_graph) {}
|
|
| 153 |
OutArcIt(const Digraph& _graph, const Arc& arc) |
|
| 154 |
: Arc(arc), digraph(&_graph) {}
|
|
| 155 | 155 |
|
| 156 |
OutArcIt& operator++() {
|
|
| 157 |
digraph->nextOut(*this); |
|
| 158 |
|
|
| 156 |
OutArcIt& operator++() {
|
|
| 157 |
digraph->nextOut(*this); |
|
| 158 |
return *this; |
|
| 159 | 159 |
} |
| 160 | 160 |
|
| 161 | 161 |
}; |
| 162 | 162 |
|
| 163 | 163 |
|
| 164 |
class InArcIt : public Arc {
|
|
| 164 |
class InArcIt : public Arc {
|
|
| 165 | 165 |
const Digraph* digraph; |
| 166 | 166 |
public: |
| 167 | 167 |
|
| 168 | 168 |
InArcIt() { }
|
| 169 | 169 |
|
| 170 | 170 |
InArcIt(Invalid i) : Arc(i) { }
|
| 171 | 171 |
|
| 172 |
InArcIt(const Digraph& _graph, const Node& node) |
|
| 173 |
: digraph(&_graph) {
|
|
| 174 |
|
|
| 172 |
InArcIt(const Digraph& _graph, const Node& node) |
|
| 173 |
: digraph(&_graph) {
|
|
| 174 |
_graph.firstIn(*this, node); |
|
| 175 | 175 |
} |
| 176 | 176 |
|
| 177 |
InArcIt(const Digraph& _graph, const Arc& arc) : |
|
| 178 |
Arc(arc), digraph(&_graph) {}
|
|
| 177 |
InArcIt(const Digraph& _graph, const Arc& arc) : |
|
| 178 |
Arc(arc), digraph(&_graph) {}
|
|
| 179 | 179 |
|
| 180 |
InArcIt& operator++() {
|
|
| 181 |
digraph->nextIn(*this); |
|
| 182 |
|
|
| 180 |
InArcIt& operator++() {
|
|
| 181 |
digraph->nextIn(*this); |
|
| 182 |
return *this; |
|
| 183 | 183 |
} |
| 184 | 184 |
|
| 185 | 185 |
}; |
| 186 | 186 |
|
| 187 | 187 |
// \brief Base node of the iterator |
| 188 | 188 |
// |
| 189 | 189 |
// Returns the base node (ie. the source in this case) of the iterator |
| 190 | 190 |
Node baseNode(const OutArcIt &e) const {
|
| 191 | 191 |
return Parent::source(static_cast<const Arc&>(e)); |
| 192 | 192 |
} |
| 193 | 193 |
// \brief Running node of the iterator |
| 194 | 194 |
// |
| ... | ... |
@@ -206,57 +206,57 @@ |
| 206 | 206 |
} |
| 207 | 207 |
// \brief Running node of the iterator |
| 208 | 208 |
// |
| 209 | 209 |
// Returns the running node (ie. the source in this case) of the |
| 210 | 210 |
// iterator |
| 211 | 211 |
Node runningNode(const InArcIt &e) const {
|
| 212 | 212 |
return Parent::source(static_cast<const Arc&>(e)); |
| 213 | 213 |
} |
| 214 | 214 |
|
| 215 | 215 |
using Parent::first; |
| 216 | 216 |
|
| 217 | 217 |
// Mappable extension |
| 218 |
|
|
| 218 |
|
|
| 219 | 219 |
template <typename _Value> |
| 220 |
class ArcMap |
|
| 220 |
class ArcMap |
|
| 221 | 221 |
: public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
|
| 222 | 222 |
typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent; |
| 223 | 223 |
|
| 224 | 224 |
public: |
| 225 |
explicit ArcMap(const Digraph& _g) |
|
| 226 |
: Parent(_g) {}
|
|
| 227 |
ArcMap(const Digraph& _g, const _Value& _v) |
|
| 228 |
: Parent(_g, _v) {}
|
|
| 225 |
explicit ArcMap(const Digraph& _g) |
|
| 226 |
: Parent(_g) {}
|
|
| 227 |
ArcMap(const Digraph& _g, const _Value& _v) |
|
| 228 |
: Parent(_g, _v) {}
|
|
| 229 | 229 |
|
| 230 | 230 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 231 |
|
|
| 231 |
return operator=<ArcMap>(cmap); |
|
| 232 | 232 |
} |
| 233 | 233 |
|
| 234 | 234 |
template <typename CMap> |
| 235 | 235 |
ArcMap& operator=(const CMap& cmap) {
|
| 236 | 236 |
Parent::operator=(cmap); |
| 237 |
|
|
| 237 |
return *this; |
|
| 238 | 238 |
} |
| 239 | 239 |
|
| 240 | 240 |
}; |
| 241 | 241 |
|
| 242 | 242 |
|
| 243 | 243 |
// Alteration extension |
| 244 | 244 |
|
| 245 | 245 |
Arc addArc(const Node& from, const Node& to) {
|
| 246 | 246 |
Arc arc = Parent::addArc(from, to); |
| 247 | 247 |
notifier(Arc()).add(arc); |
| 248 | 248 |
return arc; |
| 249 | 249 |
} |
| 250 |
|
|
| 250 |
|
|
| 251 | 251 |
void clear() {
|
| 252 | 252 |
notifier(Arc()).clear(); |
| 253 | 253 |
Parent::clear(); |
| 254 | 254 |
} |
| 255 | 255 |
|
| 256 | 256 |
void erase(const Arc& arc) {
|
| 257 | 257 |
notifier(Arc()).erase(arc); |
| 258 | 258 |
Parent::erase(arc); |
| 259 | 259 |
} |
| 260 | 260 |
|
| 261 | 261 |
ArcSetExtender() {
|
| 262 | 262 |
arc_notifier.setContainer(*this); |
| ... | ... |
@@ -301,200 +301,200 @@ |
| 301 | 301 |
} |
| 302 | 302 |
|
| 303 | 303 |
Arc fromId(int id, Arc) const {
|
| 304 | 304 |
return Parent::arcFromId(id); |
| 305 | 305 |
} |
| 306 | 306 |
|
| 307 | 307 |
Edge fromId(int id, Edge) const {
|
| 308 | 308 |
return Parent::edgeFromId(id); |
| 309 | 309 |
} |
| 310 | 310 |
|
| 311 | 311 |
Node oppositeNode(const Node &n, const Edge &e) const {
|
| 312 | 312 |
if( n == Parent::u(e)) |
| 313 |
|
|
| 313 |
return Parent::v(e); |
|
| 314 | 314 |
else if( n == Parent::v(e)) |
| 315 |
|
|
| 315 |
return Parent::u(e); |
|
| 316 | 316 |
else |
| 317 |
|
|
| 317 |
return INVALID; |
|
| 318 | 318 |
} |
| 319 | 319 |
|
| 320 | 320 |
Arc oppositeArc(const Arc &e) const {
|
| 321 | 321 |
return Parent::direct(e, !Parent::direction(e)); |
| 322 | 322 |
} |
| 323 | 323 |
|
| 324 | 324 |
using Parent::direct; |
| 325 | 325 |
Arc direct(const Edge &e, const Node &s) const {
|
| 326 | 326 |
return Parent::direct(e, Parent::u(e) == s); |
| 327 | 327 |
} |
| 328 | 328 |
|
| 329 | 329 |
typedef AlterationNotifier<EdgeSetExtender, Arc> ArcNotifier; |
| 330 | 330 |
typedef AlterationNotifier<EdgeSetExtender, Edge> EdgeNotifier; |
| 331 | 331 |
|
| 332 | 332 |
|
| 333 | 333 |
protected: |
| 334 | 334 |
|
| 335 | 335 |
mutable ArcNotifier arc_notifier; |
| 336 | 336 |
mutable EdgeNotifier edge_notifier; |
| 337 | 337 |
|
| 338 | 338 |
public: |
| 339 | 339 |
|
| 340 | 340 |
using Parent::notifier; |
| 341 |
|
|
| 341 |
|
|
| 342 | 342 |
ArcNotifier& notifier(Arc) const {
|
| 343 | 343 |
return arc_notifier; |
| 344 | 344 |
} |
| 345 | 345 |
|
| 346 | 346 |
EdgeNotifier& notifier(Edge) const {
|
| 347 | 347 |
return edge_notifier; |
| 348 | 348 |
} |
| 349 | 349 |
|
| 350 | 350 |
|
| 351 |
class NodeIt : public Node {
|
|
| 351 |
class NodeIt : public Node {
|
|
| 352 | 352 |
const Graph* graph; |
| 353 | 353 |
public: |
| 354 | 354 |
|
| 355 | 355 |
NodeIt() {}
|
| 356 | 356 |
|
| 357 | 357 |
NodeIt(Invalid i) : Node(i) { }
|
| 358 | 358 |
|
| 359 | 359 |
explicit NodeIt(const Graph& _graph) : graph(&_graph) {
|
| 360 |
|
|
| 360 |
_graph.first(static_cast<Node&>(*this)); |
|
| 361 | 361 |
} |
| 362 | 362 |
|
| 363 |
NodeIt(const Graph& _graph, const Node& node) |
|
| 364 |
: Node(node), graph(&_graph) {}
|
|
| 363 |
NodeIt(const Graph& _graph, const Node& node) |
|
| 364 |
: Node(node), graph(&_graph) {}
|
|
| 365 | 365 |
|
| 366 |
NodeIt& operator++() {
|
|
| 367 |
graph->next(*this); |
|
| 368 |
|
|
| 366 |
NodeIt& operator++() {
|
|
| 367 |
graph->next(*this); |
|
| 368 |
return *this; |
|
| 369 | 369 |
} |
| 370 | 370 |
|
| 371 | 371 |
}; |
| 372 | 372 |
|
| 373 | 373 |
|
| 374 |
class ArcIt : public Arc {
|
|
| 374 |
class ArcIt : public Arc {
|
|
| 375 | 375 |
const Graph* graph; |
| 376 | 376 |
public: |
| 377 | 377 |
|
| 378 | 378 |
ArcIt() { }
|
| 379 | 379 |
|
| 380 | 380 |
ArcIt(Invalid i) : Arc(i) { }
|
| 381 | 381 |
|
| 382 | 382 |
explicit ArcIt(const Graph& _graph) : graph(&_graph) {
|
| 383 |
|
|
| 383 |
_graph.first(static_cast<Arc&>(*this)); |
|
| 384 | 384 |
} |
| 385 | 385 |
|
| 386 |
ArcIt(const Graph& _graph, const Arc& e) : |
|
| 387 |
Arc(e), graph(&_graph) { }
|
|
| 386 |
ArcIt(const Graph& _graph, const Arc& e) : |
|
| 387 |
Arc(e), graph(&_graph) { }
|
|
| 388 | 388 |
|
| 389 |
ArcIt& operator++() {
|
|
| 390 |
graph->next(*this); |
|
| 391 |
|
|
| 389 |
ArcIt& operator++() {
|
|
| 390 |
graph->next(*this); |
|
| 391 |
return *this; |
|
| 392 | 392 |
} |
| 393 | 393 |
|
| 394 | 394 |
}; |
| 395 | 395 |
|
| 396 | 396 |
|
| 397 |
class OutArcIt : public Arc {
|
|
| 397 |
class OutArcIt : public Arc {
|
|
| 398 | 398 |
const Graph* graph; |
| 399 | 399 |
public: |
| 400 | 400 |
|
| 401 | 401 |
OutArcIt() { }
|
| 402 | 402 |
|
| 403 | 403 |
OutArcIt(Invalid i) : Arc(i) { }
|
| 404 | 404 |
|
| 405 |
OutArcIt(const Graph& _graph, const Node& node) |
|
| 406 |
: graph(&_graph) {
|
|
| 407 |
|
|
| 405 |
OutArcIt(const Graph& _graph, const Node& node) |
|
| 406 |
: graph(&_graph) {
|
|
| 407 |
_graph.firstOut(*this, node); |
|
| 408 | 408 |
} |
| 409 | 409 |
|
| 410 |
OutArcIt(const Graph& _graph, const Arc& arc) |
|
| 411 |
: Arc(arc), graph(&_graph) {}
|
|
| 410 |
OutArcIt(const Graph& _graph, const Arc& arc) |
|
| 411 |
: Arc(arc), graph(&_graph) {}
|
|
| 412 | 412 |
|
| 413 |
OutArcIt& operator++() {
|
|
| 414 |
graph->nextOut(*this); |
|
| 415 |
|
|
| 413 |
OutArcIt& operator++() {
|
|
| 414 |
graph->nextOut(*this); |
|
| 415 |
return *this; |
|
| 416 | 416 |
} |
| 417 | 417 |
|
| 418 | 418 |
}; |
| 419 | 419 |
|
| 420 | 420 |
|
| 421 |
class InArcIt : public Arc {
|
|
| 421 |
class InArcIt : public Arc {
|
|
| 422 | 422 |
const Graph* graph; |
| 423 | 423 |
public: |
| 424 | 424 |
|
| 425 | 425 |
InArcIt() { }
|
| 426 | 426 |
|
| 427 | 427 |
InArcIt(Invalid i) : Arc(i) { }
|
| 428 | 428 |
|
| 429 |
InArcIt(const Graph& _graph, const Node& node) |
|
| 430 |
: graph(&_graph) {
|
|
| 431 |
|
|
| 429 |
InArcIt(const Graph& _graph, const Node& node) |
|
| 430 |
: graph(&_graph) {
|
|
| 431 |
_graph.firstIn(*this, node); |
|
| 432 | 432 |
} |
| 433 | 433 |
|
| 434 |
InArcIt(const Graph& _graph, const Arc& arc) : |
|
| 435 |
Arc(arc), graph(&_graph) {}
|
|
| 434 |
InArcIt(const Graph& _graph, const Arc& arc) : |
|
| 435 |
Arc(arc), graph(&_graph) {}
|
|
| 436 | 436 |
|
| 437 |
InArcIt& operator++() {
|
|
| 438 |
graph->nextIn(*this); |
|
| 439 |
|
|
| 437 |
InArcIt& operator++() {
|
|
| 438 |
graph->nextIn(*this); |
|
| 439 |
return *this; |
|
| 440 | 440 |
} |
| 441 | 441 |
|
| 442 | 442 |
}; |
| 443 | 443 |
|
| 444 | 444 |
|
| 445 |
class EdgeIt : public Parent::Edge {
|
|
| 445 |
class EdgeIt : public Parent::Edge {
|
|
| 446 | 446 |
const Graph* graph; |
| 447 | 447 |
public: |
| 448 | 448 |
|
| 449 | 449 |
EdgeIt() { }
|
| 450 | 450 |
|
| 451 | 451 |
EdgeIt(Invalid i) : Edge(i) { }
|
| 452 | 452 |
|
| 453 | 453 |
explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
|
| 454 |
|
|
| 454 |
_graph.first(static_cast<Edge&>(*this)); |
|
| 455 | 455 |
} |
| 456 | 456 |
|
| 457 |
EdgeIt(const Graph& _graph, const Edge& e) : |
|
| 458 |
Edge(e), graph(&_graph) { }
|
|
| 457 |
EdgeIt(const Graph& _graph, const Edge& e) : |
|
| 458 |
Edge(e), graph(&_graph) { }
|
|
| 459 | 459 |
|
| 460 |
EdgeIt& operator++() {
|
|
| 461 |
graph->next(*this); |
|
| 462 |
|
|
| 460 |
EdgeIt& operator++() {
|
|
| 461 |
graph->next(*this); |
|
| 462 |
return *this; |
|
| 463 | 463 |
} |
| 464 | 464 |
|
| 465 | 465 |
}; |
| 466 | 466 |
|
| 467 | 467 |
class IncEdgeIt : public Parent::Edge {
|
| 468 | 468 |
friend class EdgeSetExtender; |
| 469 | 469 |
const Graph* graph; |
| 470 | 470 |
bool direction; |
| 471 | 471 |
public: |
| 472 | 472 |
|
| 473 | 473 |
IncEdgeIt() { }
|
| 474 | 474 |
|
| 475 | 475 |
IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
|
| 476 | 476 |
|
| 477 | 477 |
IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
|
| 478 |
|
|
| 478 |
_graph.firstInc(*this, direction, n); |
|
| 479 | 479 |
} |
| 480 | 480 |
|
| 481 | 481 |
IncEdgeIt(const Graph& _graph, const Edge &ue, const Node &n) |
| 482 |
: graph(&_graph), Edge(ue) {
|
|
| 483 |
direction = (_graph.source(ue) == n); |
|
| 482 |
: graph(&_graph), Edge(ue) {
|
|
| 483 |
direction = (_graph.source(ue) == n); |
|
| 484 | 484 |
} |
| 485 | 485 |
|
| 486 | 486 |
IncEdgeIt& operator++() {
|
| 487 |
graph->nextInc(*this, direction); |
|
| 488 |
return *this; |
|
| 487 |
graph->nextInc(*this, direction); |
|
| 488 |
return *this; |
|
| 489 | 489 |
} |
| 490 | 490 |
}; |
| 491 | 491 |
|
| 492 | 492 |
// \brief Base node of the iterator |
| 493 | 493 |
// |
| 494 | 494 |
// Returns the base node (ie. the source in this case) of the iterator |
| 495 | 495 |
Node baseNode(const OutArcIt &e) const {
|
| 496 | 496 |
return Parent::source(static_cast<const Arc&>(e)); |
| 497 | 497 |
} |
| 498 | 498 |
// \brief Running node of the iterator |
| 499 | 499 |
// |
| 500 | 500 |
// Returns the running node (ie. the target in this case) of the |
| ... | ... |
@@ -523,84 +523,84 @@ |
| 523 | 523 |
Node baseNode(const IncEdgeIt &e) const {
|
| 524 | 524 |
return e.direction ? u(e) : v(e); |
| 525 | 525 |
} |
| 526 | 526 |
// Running node of the iterator |
| 527 | 527 |
// |
| 528 | 528 |
// Returns the running node of the iterator |
| 529 | 529 |
Node runningNode(const IncEdgeIt &e) const {
|
| 530 | 530 |
return e.direction ? v(e) : u(e); |
| 531 | 531 |
} |
| 532 | 532 |
|
| 533 | 533 |
|
| 534 | 534 |
template <typename _Value> |
| 535 |
class ArcMap |
|
| 535 |
class ArcMap |
|
| 536 | 536 |
: public MapExtender<DefaultMap<Graph, Arc, _Value> > {
|
| 537 | 537 |
typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent; |
| 538 | 538 |
|
| 539 | 539 |
public: |
| 540 |
explicit ArcMap(const Graph& _g) |
|
| 541 |
: Parent(_g) {}
|
|
| 542 |
ArcMap(const Graph& _g, const _Value& _v) |
|
| 543 |
: Parent(_g, _v) {}
|
|
| 540 |
explicit ArcMap(const Graph& _g) |
|
| 541 |
: Parent(_g) {}
|
|
| 542 |
ArcMap(const Graph& _g, const _Value& _v) |
|
| 543 |
: Parent(_g, _v) {}
|
|
| 544 | 544 |
|
| 545 | 545 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 546 |
|
|
| 546 |
return operator=<ArcMap>(cmap); |
|
| 547 | 547 |
} |
| 548 | 548 |
|
| 549 | 549 |
template <typename CMap> |
| 550 | 550 |
ArcMap& operator=(const CMap& cmap) {
|
| 551 | 551 |
Parent::operator=(cmap); |
| 552 |
|
|
| 552 |
return *this; |
|
| 553 | 553 |
} |
| 554 | 554 |
|
| 555 | 555 |
}; |
| 556 | 556 |
|
| 557 | 557 |
|
| 558 | 558 |
template <typename _Value> |
| 559 |
class EdgeMap |
|
| 559 |
class EdgeMap |
|
| 560 | 560 |
: public MapExtender<DefaultMap<Graph, Edge, _Value> > {
|
| 561 | 561 |
typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent; |
| 562 | 562 |
|
| 563 | 563 |
public: |
| 564 |
explicit EdgeMap(const Graph& _g) |
|
| 565 |
: Parent(_g) {}
|
|
| 564 |
explicit EdgeMap(const Graph& _g) |
|
| 565 |
: Parent(_g) {}
|
|
| 566 | 566 |
|
| 567 |
EdgeMap(const Graph& _g, const _Value& _v) |
|
| 568 |
: Parent(_g, _v) {}
|
|
| 567 |
EdgeMap(const Graph& _g, const _Value& _v) |
|
| 568 |
: Parent(_g, _v) {}
|
|
| 569 | 569 |
|
| 570 | 570 |
EdgeMap& operator=(const EdgeMap& cmap) {
|
| 571 |
|
|
| 571 |
return operator=<EdgeMap>(cmap); |
|
| 572 | 572 |
} |
| 573 | 573 |
|
| 574 | 574 |
template <typename CMap> |
| 575 | 575 |
EdgeMap& operator=(const CMap& cmap) {
|
| 576 | 576 |
Parent::operator=(cmap); |
| 577 |
|
|
| 577 |
return *this; |
|
| 578 | 578 |
} |
| 579 | 579 |
|
| 580 | 580 |
}; |
| 581 | 581 |
|
| 582 | 582 |
|
| 583 | 583 |
// Alteration extension |
| 584 | 584 |
|
| 585 | 585 |
Edge addEdge(const Node& from, const Node& to) {
|
| 586 | 586 |
Edge edge = Parent::addEdge(from, to); |
| 587 | 587 |
notifier(Edge()).add(edge); |
| 588 | 588 |
std::vector<Arc> arcs; |
| 589 | 589 |
arcs.push_back(Parent::direct(edge, true)); |
| 590 | 590 |
arcs.push_back(Parent::direct(edge, false)); |
| 591 | 591 |
notifier(Arc()).add(arcs); |
| 592 | 592 |
return edge; |
| 593 | 593 |
} |
| 594 |
|
|
| 594 |
|
|
| 595 | 595 |
void clear() {
|
| 596 | 596 |
notifier(Arc()).clear(); |
| 597 | 597 |
notifier(Edge()).clear(); |
| 598 | 598 |
Parent::clear(); |
| 599 | 599 |
} |
| 600 | 600 |
|
| 601 | 601 |
void erase(const Edge& edge) {
|
| 602 | 602 |
std::vector<Arc> arcs; |
| 603 | 603 |
arcs.push_back(Parent::direct(edge, true)); |
| 604 | 604 |
arcs.push_back(Parent::direct(edge, false)); |
| 605 | 605 |
notifier(Arc()).erase(arcs); |
| 606 | 606 |
notifier(Edge()).erase(edge); |
| ... | ... |
@@ -608,18 +608,18 @@ |
| 608 | 608 |
} |
| 609 | 609 |
|
| 610 | 610 |
|
| 611 | 611 |
EdgeSetExtender() {
|
| 612 | 612 |
arc_notifier.setContainer(*this); |
| 613 | 613 |
edge_notifier.setContainer(*this); |
| 614 | 614 |
} |
| 615 | 615 |
|
| 616 | 616 |
~EdgeSetExtender() {
|
| 617 | 617 |
edge_notifier.clear(); |
| 618 | 618 |
arc_notifier.clear(); |
| 619 | 619 |
} |
| 620 |
|
|
| 620 |
|
|
| 621 | 621 |
}; |
| 622 | 622 |
|
| 623 | 623 |
} |
| 624 | 624 |
|
| 625 | 625 |
#endif |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -87,25 +87,25 @@ |
| 87 | 87 |
cutime=ts.tms_cutime/tck; |
| 88 | 88 |
cstime=ts.tms_cstime/tck; |
| 89 | 89 |
#endif |
| 90 | 90 |
} |
| 91 | 91 |
|
| 92 | 92 |
std::string getWinFormattedDate() |
| 93 | 93 |
{
|
| 94 | 94 |
std::ostringstream os; |
| 95 | 95 |
#ifdef WIN32 |
| 96 | 96 |
SYSTEMTIME time; |
| 97 | 97 |
GetSystemTime(&time); |
| 98 | 98 |
char buf1[11], buf2[9], buf3[5]; |
| 99 |
|
|
| 99 |
if (GetDateFormat(MY_LOCALE, 0, &time, |
|
| 100 | 100 |
("ddd MMM dd"), buf1, 11) &&
|
| 101 | 101 |
GetTimeFormat(MY_LOCALE, 0, &time, |
| 102 | 102 |
("HH':'mm':'ss"), buf2, 9) &&
|
| 103 | 103 |
GetDateFormat(MY_LOCALE, 0, &time, |
| 104 | 104 |
("yyyy"), buf3, 5)) {
|
| 105 | 105 |
os << buf1 << ' ' << buf2 << ' ' << buf3; |
| 106 | 106 |
} |
| 107 | 107 |
else os << "unknown"; |
| 108 | 108 |
#else |
| 109 | 109 |
timeval tv; |
| 110 | 110 |
gettimeofday(&tv, 0); |
| 111 | 111 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -375,25 +375,25 @@ |
| 375 | 375 |
/// |
| 376 | 376 |
/// \brief Simplified bucket heap data structure. |
| 377 | 377 |
/// |
| 378 | 378 |
/// This class implements a simplified \e bucket \e heap data |
| 379 | 379 |
/// structure. It does not provide some functionality, but it is |
| 380 | 380 |
/// faster and simpler than BucketHeap. The main difference is |
| 381 | 381 |
/// that BucketHeap stores a doubly-linked list for each key while |
| 382 | 382 |
/// this class stores only simply-linked lists. It supports erasing |
| 383 | 383 |
/// only for the item having minimum priority and it does not support |
| 384 | 384 |
/// key increasing and decreasing. |
| 385 | 385 |
/// |
| 386 | 386 |
/// Note that this implementation does not conform to the |
| 387 |
/// \ref concepts::Heap "heap concept" due to the lack of some |
|
| 387 |
/// \ref concepts::Heap "heap concept" due to the lack of some |
|
| 388 | 388 |
/// functionality. |
| 389 | 389 |
/// |
| 390 | 390 |
/// \tparam IM A read-writable item map with \c int values, used |
| 391 | 391 |
/// internally to handle the cross references. |
| 392 | 392 |
/// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap. |
| 393 | 393 |
/// The default is \e min-heap. If this parameter is set to \c false, |
| 394 | 394 |
/// then the comparison is reversed, so the top(), prio() and pop() |
| 395 | 395 |
/// functions deal with the item having maximum priority instead of the |
| 396 | 396 |
/// minimum. |
| 397 | 397 |
/// |
| 398 | 398 |
/// \sa BucketHeap |
| 399 | 399 |
template <typename IM, bool MIN = true > |
| 1 |
/* -*- C++ -*- |
|
| 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
| 2 | 2 |
* |
| 3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
| 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -124,25 +124,25 @@ |
| 124 | 124 |
INFEASIBLE, |
| 125 | 125 |
/// The problem has optimal solution (i.e. it is feasible and |
| 126 | 126 |
/// bounded), and the algorithm has found optimal flow and node |
| 127 | 127 |
/// potentials (primal and dual solutions). |
| 128 | 128 |
OPTIMAL, |
| 129 | 129 |
/// The digraph contains an arc of negative cost and infinite |
| 130 | 130 |
/// upper bound. It means that the objective function is unbounded |
| 131 | 131 |
/// on that arc, however, note that it could actually be bounded |
| 132 | 132 |
/// over the feasible flows, but this algroithm cannot handle |
| 133 | 133 |
/// these cases. |
| 134 | 134 |
UNBOUNDED |
| 135 | 135 |
}; |
| 136 |
|
|
| 136 |
|
|
| 137 | 137 |
private: |
| 138 | 138 |
|
| 139 | 139 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 140 | 140 |
|
| 141 | 141 |
typedef std::vector<int> IntVector; |
| 142 | 142 |
typedef std::vector<Value> ValueVector; |
| 143 | 143 |
typedef std::vector<Cost> CostVector; |
| 144 | 144 |
typedef std::vector<char> BoolVector; |
| 145 | 145 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
| 146 | 146 |
|
| 147 | 147 |
private: |
| 148 | 148 |
|
| ... | ... |
@@ -175,25 +175,25 @@ |
| 175 | 175 |
|
| 176 | 176 |
ValueVector _res_cap; |
| 177 | 177 |
CostVector _pi; |
| 178 | 178 |
ValueVector _excess; |
| 179 | 179 |
IntVector _excess_nodes; |
| 180 | 180 |
IntVector _deficit_nodes; |
| 181 | 181 |
|
| 182 | 182 |
Value _delta; |
| 183 | 183 |
int _factor; |
| 184 | 184 |
IntVector _pred; |
| 185 | 185 |
|
| 186 | 186 |
public: |
| 187 |
|
|
| 187 |
|
|
| 188 | 188 |
/// \brief Constant for infinite upper bounds (capacities). |
| 189 | 189 |
/// |
| 190 | 190 |
/// Constant for infinite upper bounds (capacities). |
| 191 | 191 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
| 192 | 192 |
/// \c std::numeric_limits<Value>::max() otherwise. |
| 193 | 193 |
const Value INF; |
| 194 | 194 |
|
| 195 | 195 |
private: |
| 196 | 196 |
|
| 197 | 197 |
// Special implementation of the Dijkstra algorithm for finding |
| 198 | 198 |
// shortest paths in the residual network of the digraph with |
| 199 | 199 |
// respect to the reduced arc costs and modifying the node |
| ... | ... |
@@ -202,28 +202,28 @@ |
| 202 | 202 |
{
|
| 203 | 203 |
private: |
| 204 | 204 |
|
| 205 | 205 |
int _node_num; |
| 206 | 206 |
bool _geq; |
| 207 | 207 |
const IntVector &_first_out; |
| 208 | 208 |
const IntVector &_target; |
| 209 | 209 |
const CostVector &_cost; |
| 210 | 210 |
const ValueVector &_res_cap; |
| 211 | 211 |
const ValueVector &_excess; |
| 212 | 212 |
CostVector &_pi; |
| 213 | 213 |
IntVector &_pred; |
| 214 |
|
|
| 214 |
|
|
| 215 | 215 |
IntVector _proc_nodes; |
| 216 | 216 |
CostVector _dist; |
| 217 |
|
|
| 217 |
|
|
| 218 | 218 |
public: |
| 219 | 219 |
|
| 220 | 220 |
ResidualDijkstra(CapacityScaling& cs) : |
| 221 | 221 |
_node_num(cs._node_num), _geq(cs._sum_supply < 0), |
| 222 | 222 |
_first_out(cs._first_out), _target(cs._target), _cost(cs._cost), |
| 223 | 223 |
_res_cap(cs._res_cap), _excess(cs._excess), _pi(cs._pi), |
| 224 | 224 |
_pred(cs._pred), _dist(cs._node_num) |
| 225 | 225 |
{}
|
| 226 | 226 |
|
| 227 | 227 |
int run(int s, Value delta = 1) {
|
| 228 | 228 |
RangeMap<int> heap_cross_ref(_node_num, Heap::PRE_HEAP); |
| 229 | 229 |
Heap heap(heap_cross_ref); |
| ... | ... |
@@ -430,25 +430,25 @@ |
| 430 | 430 |
/// \param k The required amount of flow from node \c s to node \c t |
| 431 | 431 |
/// (i.e. the supply of \c s and the demand of \c t). |
| 432 | 432 |
/// |
| 433 | 433 |
/// \return <tt>(*this)</tt> |
| 434 | 434 |
CapacityScaling& stSupply(const Node& s, const Node& t, Value k) {
|
| 435 | 435 |
for (int i = 0; i != _node_num; ++i) {
|
| 436 | 436 |
_supply[i] = 0; |
| 437 | 437 |
} |
| 438 | 438 |
_supply[_node_id[s]] = k; |
| 439 | 439 |
_supply[_node_id[t]] = -k; |
| 440 | 440 |
return *this; |
| 441 | 441 |
} |
| 442 |
|
|
| 442 |
|
|
| 443 | 443 |
/// @} |
| 444 | 444 |
|
| 445 | 445 |
/// \name Execution control |
| 446 | 446 |
/// The algorithm can be executed using \ref run(). |
| 447 | 447 |
|
| 448 | 448 |
/// @{
|
| 449 | 449 |
|
| 450 | 450 |
/// \brief Run the algorithm. |
| 451 | 451 |
/// |
| 452 | 452 |
/// This function runs the algorithm. |
| 453 | 453 |
/// The paramters can be specified using functions \ref lowerMap(), |
| 454 | 454 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| ... | ... |
@@ -566,25 +566,25 @@ |
| 566 | 566 |
++_node_num; |
| 567 | 567 |
|
| 568 | 568 |
_first_out.resize(_node_num + 1); |
| 569 | 569 |
_forward.resize(_res_arc_num); |
| 570 | 570 |
_source.resize(_res_arc_num); |
| 571 | 571 |
_target.resize(_res_arc_num); |
| 572 | 572 |
_reverse.resize(_res_arc_num); |
| 573 | 573 |
|
| 574 | 574 |
_lower.resize(_res_arc_num); |
| 575 | 575 |
_upper.resize(_res_arc_num); |
| 576 | 576 |
_cost.resize(_res_arc_num); |
| 577 | 577 |
_supply.resize(_node_num); |
| 578 |
|
|
| 578 |
|
|
| 579 | 579 |
_res_cap.resize(_res_arc_num); |
| 580 | 580 |
_pi.resize(_node_num); |
| 581 | 581 |
_excess.resize(_node_num); |
| 582 | 582 |
_pred.resize(_node_num); |
| 583 | 583 |
|
| 584 | 584 |
// Copy the graph |
| 585 | 585 |
int i = 0, j = 0, k = 2 * _arc_num + _node_num - 1; |
| 586 | 586 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
| 587 | 587 |
_node_id[n] = i; |
| 588 | 588 |
} |
| 589 | 589 |
i = 0; |
| 590 | 590 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
| ... | ... |
@@ -610,25 +610,25 @@ |
| 610 | 610 |
_target[k] = i; |
| 611 | 611 |
_reverse[k] = j; |
| 612 | 612 |
++j; ++k; |
| 613 | 613 |
} |
| 614 | 614 |
_first_out[i] = j; |
| 615 | 615 |
_first_out[_node_num] = k; |
| 616 | 616 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 617 | 617 |
int fi = _arc_idf[a]; |
| 618 | 618 |
int bi = _arc_idb[a]; |
| 619 | 619 |
_reverse[fi] = bi; |
| 620 | 620 |
_reverse[bi] = fi; |
| 621 | 621 |
} |
| 622 |
|
|
| 622 |
|
|
| 623 | 623 |
// Reset parameters |
| 624 | 624 |
resetParams(); |
| 625 | 625 |
return *this; |
| 626 | 626 |
} |
| 627 | 627 |
|
| 628 | 628 |
/// @} |
| 629 | 629 |
|
| 630 | 630 |
/// \name Query Functions |
| 631 | 631 |
/// The results of the algorithm can be obtained using these |
| 632 | 632 |
/// functions.\n |
| 633 | 633 |
/// The \ref run() function must be called before using them. |
| 634 | 634 |
|
| ... | ... |
@@ -719,25 +719,25 @@ |
| 719 | 719 |
private: |
| 720 | 720 |
|
| 721 | 721 |
// Initialize the algorithm |
| 722 | 722 |
ProblemType init() {
|
| 723 | 723 |
if (_node_num <= 1) return INFEASIBLE; |
| 724 | 724 |
|
| 725 | 725 |
// Check the sum of supply values |
| 726 | 726 |
_sum_supply = 0; |
| 727 | 727 |
for (int i = 0; i != _root; ++i) {
|
| 728 | 728 |
_sum_supply += _supply[i]; |
| 729 | 729 |
} |
| 730 | 730 |
if (_sum_supply > 0) return INFEASIBLE; |
| 731 |
|
|
| 731 |
|
|
| 732 | 732 |
// Initialize vectors |
| 733 | 733 |
for (int i = 0; i != _root; ++i) {
|
| 734 | 734 |
_pi[i] = 0; |
| 735 | 735 |
_excess[i] = _supply[i]; |
| 736 | 736 |
} |
| 737 | 737 |
|
| 738 | 738 |
// Remove non-zero lower bounds |
| 739 | 739 |
const Value MAX = std::numeric_limits<Value>::max(); |
| 740 | 740 |
int last_out; |
| 741 | 741 |
if (_have_lower) {
|
| 742 | 742 |
for (int i = 0; i != _root; ++i) {
|
| 743 | 743 |
last_out = _first_out[i+1]; |
| ... | ... |
@@ -767,25 +767,25 @@ |
| 767 | 767 |
last_out = _first_out[i+1] - 1; |
| 768 | 768 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 769 | 769 |
Value rc = _res_cap[j]; |
| 770 | 770 |
if (_cost[j] < 0 && rc > 0) {
|
| 771 | 771 |
if (rc >= MAX) return UNBOUNDED; |
| 772 | 772 |
_excess[i] -= rc; |
| 773 | 773 |
_excess[_target[j]] += rc; |
| 774 | 774 |
_res_cap[j] = 0; |
| 775 | 775 |
_res_cap[_reverse[j]] += rc; |
| 776 | 776 |
} |
| 777 | 777 |
} |
| 778 | 778 |
} |
| 779 |
|
|
| 779 |
|
|
| 780 | 780 |
// Handle GEQ supply type |
| 781 | 781 |
if (_sum_supply < 0) {
|
| 782 | 782 |
_pi[_root] = 0; |
| 783 | 783 |
_excess[_root] = -_sum_supply; |
| 784 | 784 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
| 785 | 785 |
int ra = _reverse[a]; |
| 786 | 786 |
_res_cap[a] = -_sum_supply + 1; |
| 787 | 787 |
_res_cap[ra] = 0; |
| 788 | 788 |
_cost[a] = 0; |
| 789 | 789 |
_cost[ra] = 0; |
| 790 | 790 |
} |
| 791 | 791 |
} else {
|
| ... | ... |
@@ -835,27 +835,27 @@ |
| 835 | 835 |
if (_have_lower) {
|
| 836 | 836 |
int limit = _first_out[_root]; |
| 837 | 837 |
for (int j = 0; j != limit; ++j) {
|
| 838 | 838 |
if (!_forward[j]) _res_cap[j] += _lower[j]; |
| 839 | 839 |
} |
| 840 | 840 |
} |
| 841 | 841 |
|
| 842 | 842 |
// Shift potentials if necessary |
| 843 | 843 |
Cost pr = _pi[_root]; |
| 844 | 844 |
if (_sum_supply < 0 || pr > 0) {
|
| 845 | 845 |
for (int i = 0; i != _node_num; ++i) {
|
| 846 | 846 |
_pi[i] -= pr; |
| 847 |
} |
|
| 847 |
} |
|
| 848 | 848 |
} |
| 849 |
|
|
| 849 |
|
|
| 850 | 850 |
return pt; |
| 851 | 851 |
} |
| 852 | 852 |
|
| 853 | 853 |
// Execute the capacity scaling algorithm |
| 854 | 854 |
ProblemType startWithScaling() {
|
| 855 | 855 |
// Perform capacity scaling phases |
| 856 | 856 |
int s, t; |
| 857 | 857 |
ResidualDijkstra _dijkstra(*this); |
| 858 | 858 |
while (true) {
|
| 859 | 859 |
// Saturate all arcs not satisfying the optimality condition |
| 860 | 860 |
int last_out; |
| 861 | 861 |
for (int u = 0; u != _node_num; ++u) {
|
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -112,19 +112,19 @@ |
| 112 | 112 |
virtual SolveExitStatus _solve(); |
| 113 | 113 |
virtual ProblemType _getType() const; |
| 114 | 114 |
virtual Value _getSol(int i) const; |
| 115 | 115 |
virtual Value _getSolValue() const; |
| 116 | 116 |
|
| 117 | 117 |
virtual void _clear(); |
| 118 | 118 |
|
| 119 | 119 |
virtual void _messageLevel(MessageLevel level); |
| 120 | 120 |
void _applyMessageLevel(); |
| 121 | 121 |
|
| 122 | 122 |
int _message_level; |
| 123 | 123 |
|
| 124 |
|
|
| 124 |
|
|
| 125 | 125 |
|
| 126 | 126 |
}; |
| 127 | 127 |
|
| 128 | 128 |
} |
| 129 | 129 |
|
| 130 | 130 |
#endif |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -50,26 +50,26 @@ |
| 50 | 50 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
| 51 | 51 |
typedef LM LowerMap; |
| 52 | 52 |
|
| 53 | 53 |
/// \brief The type of the upper bound (capacity) map. |
| 54 | 54 |
/// |
| 55 | 55 |
/// The type of the map that stores the upper bounds (capacities) |
| 56 | 56 |
/// on the arcs. |
| 57 | 57 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
| 58 | 58 |
typedef UM UpperMap; |
| 59 | 59 |
|
| 60 | 60 |
/// \brief The type of supply map. |
| 61 | 61 |
/// |
| 62 |
/// The type of the map that stores the signed supply values of the |
|
| 63 |
/// nodes. |
|
| 62 |
/// The type of the map that stores the signed supply values of the |
|
| 63 |
/// nodes. |
|
| 64 | 64 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
| 65 | 65 |
typedef SM SupplyMap; |
| 66 | 66 |
|
| 67 | 67 |
/// \brief The type of the flow and supply values. |
| 68 | 68 |
typedef typename SupplyMap::Value Value; |
| 69 | 69 |
|
| 70 | 70 |
/// \brief The type of the map that stores the flow values. |
| 71 | 71 |
/// |
| 72 | 72 |
/// The type of the map that stores the flow values. |
| 73 | 73 |
/// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" |
| 74 | 74 |
/// concept. |
| 75 | 75 |
#ifdef DOXYGEN |
| ... | ... |
@@ -132,35 +132,35 @@ |
| 132 | 132 |
upper bounds on the arcs, for which \f$lower(uv) \leq upper(uv)\f$ |
| 133 | 133 |
holds for all \f$uv\in A\f$, and \f$sup: V\rightarrow\mathbf{R}\f$
|
| 134 | 134 |
denotes the signed supply values of the nodes. |
| 135 | 135 |
If \f$sup(u)>0\f$, then \f$u\f$ is a supply node with \f$sup(u)\f$ |
| 136 | 136 |
supply, if \f$sup(u)<0\f$, then \f$u\f$ is a demand node with |
| 137 | 137 |
\f$-sup(u)\f$ demand. |
| 138 | 138 |
A feasible circulation is an \f$f: A\rightarrow\mathbf{R}\f$
|
| 139 | 139 |
solution of the following problem. |
| 140 | 140 |
|
| 141 | 141 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu)
|
| 142 | 142 |
\geq sup(u) \quad \forall u\in V, \f] |
| 143 | 143 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A. \f] |
| 144 |
|
|
| 144 |
|
|
| 145 | 145 |
The sum of the supply values, i.e. \f$\sum_{u\in V} sup(u)\f$ must be
|
| 146 | 146 |
zero or negative in order to have a feasible solution (since the sum |
| 147 | 147 |
of the expressions on the left-hand side of the inequalities is zero). |
| 148 | 148 |
It means that the total demand must be greater or equal to the total |
| 149 | 149 |
supply and all the supplies have to be carried out from the supply nodes, |
| 150 | 150 |
but there could be demands that are not satisfied. |
| 151 | 151 |
If \f$\sum_{u\in V} sup(u)\f$ is zero, then all the supply/demand
|
| 152 | 152 |
constraints have to be satisfied with equality, i.e. all demands |
| 153 | 153 |
have to be satisfied and all supplies have to be used. |
| 154 |
|
|
| 154 |
|
|
| 155 | 155 |
If you need the opposite inequalities in the supply/demand constraints |
| 156 | 156 |
(i.e. the total demand is less than the total supply and all the demands |
| 157 | 157 |
have to be satisfied while there could be supplies that are not used), |
| 158 | 158 |
then you could easily transform the problem to the above form by reversing |
| 159 | 159 |
the direction of the arcs and taking the negative of the supply values |
| 160 | 160 |
(e.g. using \ref ReverseDigraph and \ref NegMap adaptors). |
| 161 | 161 |
|
| 162 | 162 |
This algorithm either calculates a feasible circulation, or provides |
| 163 | 163 |
a \ref barrier() "barrier", which prooves that a feasible soultion |
| 164 | 164 |
cannot exist. |
| 165 | 165 |
|
| 166 | 166 |
Note that this algorithm also provides a feasible solution for the |
| ... | ... |
@@ -328,25 +328,25 @@ |
| 328 | 328 |
protected: |
| 329 | 329 |
|
| 330 | 330 |
Circulation() {}
|
| 331 | 331 |
|
| 332 | 332 |
public: |
| 333 | 333 |
|
| 334 | 334 |
/// Constructor. |
| 335 | 335 |
|
| 336 | 336 |
/// The constructor of the class. |
| 337 | 337 |
/// |
| 338 | 338 |
/// \param graph The digraph the algorithm runs on. |
| 339 | 339 |
/// \param lower The lower bounds for the flow values on the arcs. |
| 340 |
/// \param upper The upper bounds (capacities) for the flow values |
|
| 340 |
/// \param upper The upper bounds (capacities) for the flow values |
|
| 341 | 341 |
/// on the arcs. |
| 342 | 342 |
/// \param supply The signed supply values of the nodes. |
| 343 | 343 |
Circulation(const Digraph &graph, const LowerMap &lower, |
| 344 | 344 |
const UpperMap &upper, const SupplyMap &supply) |
| 345 | 345 |
: _g(graph), _lo(&lower), _up(&upper), _supply(&supply), |
| 346 | 346 |
_flow(NULL), _local_flow(false), _level(NULL), _local_level(false), |
| 347 | 347 |
_excess(NULL) {}
|
| 348 | 348 |
|
| 349 | 349 |
/// Destructor. |
| 350 | 350 |
~Circulation() {
|
| 351 | 351 |
destroyStructures(); |
| 352 | 352 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -129,25 +129,25 @@ |
| 129 | 129 |
virtual Value _getPrimalRay(int i) const; |
| 130 | 130 |
virtual Value _getDualRay(int i) const; |
| 131 | 131 |
|
| 132 | 132 |
virtual VarStatus _getColStatus(int i) const; |
| 133 | 133 |
virtual VarStatus _getRowStatus(int i) const; |
| 134 | 134 |
|
| 135 | 135 |
virtual ProblemType _getPrimalType() const; |
| 136 | 136 |
virtual ProblemType _getDualType() const; |
| 137 | 137 |
|
| 138 | 138 |
virtual void _clear(); |
| 139 | 139 |
|
| 140 | 140 |
virtual void _messageLevel(MessageLevel); |
| 141 |
|
|
| 141 |
|
|
| 142 | 142 |
public: |
| 143 | 143 |
|
| 144 | 144 |
///Solves LP with primal simplex method. |
| 145 | 145 |
SolveExitStatus solvePrimal(); |
| 146 | 146 |
|
| 147 | 147 |
///Solves LP with dual simplex method. |
| 148 | 148 |
SolveExitStatus solveDual(); |
| 149 | 149 |
|
| 150 | 150 |
///Solves LP with barrier method. |
| 151 | 151 |
SolveExitStatus solveBarrier(); |
| 152 | 152 |
|
| 153 | 153 |
///Returns the constraint identifier understood by CLP. |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -425,25 +425,25 @@ |
| 425 | 425 |
/// It conforms to the ReferenceMap concept. |
| 426 | 426 |
template<class T> |
| 427 | 427 |
class NodeMap : public ReferenceMap<Node, T, T&, const T&> {
|
| 428 | 428 |
public: |
| 429 | 429 |
|
| 430 | 430 |
/// Constructor |
| 431 | 431 |
explicit NodeMap(const Digraph&) { }
|
| 432 | 432 |
/// Constructor with given initial value |
| 433 | 433 |
NodeMap(const Digraph&, T) { }
|
| 434 | 434 |
|
| 435 | 435 |
private: |
| 436 | 436 |
///Copy constructor |
| 437 |
NodeMap(const NodeMap& nm) : |
|
| 437 |
NodeMap(const NodeMap& nm) : |
|
| 438 | 438 |
ReferenceMap<Node, T, T&, const T&>(nm) { }
|
| 439 | 439 |
///Assignment operator |
| 440 | 440 |
template <typename CMap> |
| 441 | 441 |
NodeMap& operator=(const CMap&) {
|
| 442 | 442 |
checkConcept<ReadMap<Node, T>, CMap>(); |
| 443 | 443 |
return *this; |
| 444 | 444 |
} |
| 445 | 445 |
}; |
| 446 | 446 |
|
| 447 | 447 |
/// \brief Standard graph map type for the arcs. |
| 448 | 448 |
/// |
| 449 | 449 |
/// Standard graph map type for the arcs. |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -34,25 +34,25 @@ |
| 34 | 34 |
/// \ingroup graph_concepts |
| 35 | 35 |
/// |
| 36 | 36 |
/// \brief Class describing the concept of undirected graphs. |
| 37 | 37 |
/// |
| 38 | 38 |
/// This class describes the common interface of all undirected |
| 39 | 39 |
/// graphs. |
| 40 | 40 |
/// |
| 41 | 41 |
/// Like all concept classes, it only provides an interface |
| 42 | 42 |
/// without any sensible implementation. So any general algorithm for |
| 43 | 43 |
/// undirected graphs should compile with this class, but it will not |
| 44 | 44 |
/// run properly, of course. |
| 45 | 45 |
/// An actual graph implementation like \ref ListGraph or |
| 46 |
/// \ref SmartGraph may have additional functionality. |
|
| 46 |
/// \ref SmartGraph may have additional functionality. |
|
| 47 | 47 |
/// |
| 48 | 48 |
/// The undirected graphs also fulfill the concept of \ref Digraph |
| 49 | 49 |
/// "directed graphs", since each edge can also be regarded as two |
| 50 | 50 |
/// oppositely directed arcs. |
| 51 | 51 |
/// Undirected graphs provide an Edge type for the undirected edges and |
| 52 | 52 |
/// an Arc type for the directed arcs. The Arc type is convertible to |
| 53 | 53 |
/// Edge or inherited from it, i.e. the corresponding edge can be |
| 54 | 54 |
/// obtained from an arc. |
| 55 | 55 |
/// EdgeIt and EdgeMap classes can be used for the edges, while ArcIt |
| 56 | 56 |
/// and ArcMap classes can be used for the arcs (just like in digraphs). |
| 57 | 57 |
/// Both InArcIt and OutArcIt iterates on the same edges but with |
| 58 | 58 |
/// opposite direction. IncEdgeIt also iterates on the same edges |
| ... | ... |
@@ -76,25 +76,25 @@ |
| 76 | 76 |
Graph(const Graph&) {}
|
| 77 | 77 |
/// \brief Assignment of a graph to another one is \e not allowed. |
| 78 | 78 |
/// Use DigraphCopy instead. |
| 79 | 79 |
void operator=(const Graph&) {}
|
| 80 | 80 |
|
| 81 | 81 |
public: |
| 82 | 82 |
/// Default constructor. |
| 83 | 83 |
Graph() {}
|
| 84 | 84 |
|
| 85 | 85 |
/// \brief Undirected graphs should be tagged with \c UndirectedTag. |
| 86 | 86 |
/// |
| 87 | 87 |
/// Undirected graphs should be tagged with \c UndirectedTag. |
| 88 |
/// |
|
| 88 |
/// |
|
| 89 | 89 |
/// This tag helps the \c enable_if technics to make compile time |
| 90 | 90 |
/// specializations for undirected graphs. |
| 91 | 91 |
typedef True UndirectedTag; |
| 92 | 92 |
|
| 93 | 93 |
/// The node type of the graph |
| 94 | 94 |
|
| 95 | 95 |
/// This class identifies a node of the graph. It also serves |
| 96 | 96 |
/// as a base class of the node iterators, |
| 97 | 97 |
/// thus they convert to this type. |
| 98 | 98 |
class Node {
|
| 99 | 99 |
public: |
| 100 | 100 |
/// Default constructor |
| ... | ... |
@@ -351,25 +351,25 @@ |
| 351 | 351 |
bool operator!=(Arc) const { return true; }
|
| 352 | 352 |
|
| 353 | 353 |
/// Artificial ordering operator. |
| 354 | 354 |
|
| 355 | 355 |
/// Artificial ordering operator. |
| 356 | 356 |
/// |
| 357 | 357 |
/// \note This operator only has to define some strict ordering of |
| 358 | 358 |
/// the arcs; this order has nothing to do with the iteration |
| 359 | 359 |
/// ordering of the arcs. |
| 360 | 360 |
bool operator<(Arc) const { return false; }
|
| 361 | 361 |
|
| 362 | 362 |
/// Converison to \c Edge |
| 363 |
|
|
| 363 |
|
|
| 364 | 364 |
/// Converison to \c Edge. |
| 365 | 365 |
/// |
| 366 | 366 |
operator Edge() const { return Edge(); }
|
| 367 | 367 |
}; |
| 368 | 368 |
|
| 369 | 369 |
/// Iterator class for the arcs. |
| 370 | 370 |
|
| 371 | 371 |
/// This iterator goes through each directed arc of the graph. |
| 372 | 372 |
/// Its usage is quite simple, for example, you can count the number |
| 373 | 373 |
/// of arcs in a graph \c g of type \c %Graph as follows: |
| 374 | 374 |
///\code |
| 375 | 375 |
/// int count=0; |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -29,25 +29,25 @@ |
| 29 | 29 |
#include <lemon/bits/alteration_notifier.h> |
| 30 | 30 |
|
| 31 | 31 |
namespace lemon {
|
| 32 | 32 |
namespace concepts {
|
| 33 | 33 |
|
| 34 | 34 |
/// \brief Concept class for \c Node, \c Arc and \c Edge types. |
| 35 | 35 |
/// |
| 36 | 36 |
/// This class describes the concept of \c Node, \c Arc and \c Edge |
| 37 | 37 |
/// subtypes of digraph and graph types. |
| 38 | 38 |
/// |
| 39 | 39 |
/// \note This class is a template class so that we can use it to |
| 40 | 40 |
/// create graph skeleton classes. The reason for this is that \c Node |
| 41 |
/// and \c Arc (or \c Edge) types should \e not derive from the same |
|
| 41 |
/// and \c Arc (or \c Edge) types should \e not derive from the same |
|
| 42 | 42 |
/// base class. For \c Node you should instantiate it with character |
| 43 | 43 |
/// \c 'n', for \c Arc with \c 'a' and for \c Edge with \c 'e'. |
| 44 | 44 |
#ifndef DOXYGEN |
| 45 | 45 |
template <char sel = '0'> |
| 46 | 46 |
#endif |
| 47 | 47 |
class GraphItem {
|
| 48 | 48 |
public: |
| 49 | 49 |
/// \brief Default constructor. |
| 50 | 50 |
/// |
| 51 | 51 |
/// Default constructor. |
| 52 | 52 |
/// \warning The default constructor is not required to set |
| 53 | 53 |
/// the item to some well-defined value. So you should consider it |
| ... | ... |
@@ -80,25 +80,25 @@ |
| 80 | 80 |
/// |
| 81 | 81 |
/// Equality operator. |
| 82 | 82 |
bool operator==(const GraphItem&) const { return false; }
|
| 83 | 83 |
|
| 84 | 84 |
/// \brief Inequality operator. |
| 85 | 85 |
/// |
| 86 | 86 |
/// Inequality operator. |
| 87 | 87 |
bool operator!=(const GraphItem&) const { return false; }
|
| 88 | 88 |
|
| 89 | 89 |
/// \brief Ordering operator. |
| 90 | 90 |
/// |
| 91 | 91 |
/// This operator defines an ordering of the items. |
| 92 |
/// It makes possible to use graph item types as key types in |
|
| 92 |
/// It makes possible to use graph item types as key types in |
|
| 93 | 93 |
/// associative containers (e.g. \c std::map). |
| 94 | 94 |
/// |
| 95 | 95 |
/// \note This operator only has to define some strict ordering of |
| 96 | 96 |
/// the items; this order has nothing to do with the iteration |
| 97 | 97 |
/// ordering of the items. |
| 98 | 98 |
bool operator<(const GraphItem&) const { return false; }
|
| 99 | 99 |
|
| 100 | 100 |
template<typename _GraphItem> |
| 101 | 101 |
struct Constraints {
|
| 102 | 102 |
void constraints() {
|
| 103 | 103 |
_GraphItem i1; |
| 104 | 104 |
i1=INVALID; |
| ... | ... |
@@ -113,25 +113,25 @@ |
| 113 | 113 |
b = (ia < ib); |
| 114 | 114 |
} |
| 115 | 115 |
|
| 116 | 116 |
const _GraphItem &ia; |
| 117 | 117 |
const _GraphItem &ib; |
| 118 | 118 |
}; |
| 119 | 119 |
}; |
| 120 | 120 |
|
| 121 | 121 |
/// \brief Base skeleton class for directed graphs. |
| 122 | 122 |
/// |
| 123 | 123 |
/// This class describes the base interface of directed graph types. |
| 124 | 124 |
/// All digraph %concepts have to conform to this class. |
| 125 |
/// It just provides types for nodes and arcs and functions |
|
| 125 |
/// It just provides types for nodes and arcs and functions |
|
| 126 | 126 |
/// to get the source and the target nodes of arcs. |
| 127 | 127 |
class BaseDigraphComponent {
|
| 128 | 128 |
public: |
| 129 | 129 |
|
| 130 | 130 |
typedef BaseDigraphComponent Digraph; |
| 131 | 131 |
|
| 132 | 132 |
/// \brief Node class of the digraph. |
| 133 | 133 |
/// |
| 134 | 134 |
/// This class represents the nodes of the digraph. |
| 135 | 135 |
typedef GraphItem<'n'> Node; |
| 136 | 136 |
|
| 137 | 137 |
/// \brief Arc class of the digraph. |
| ... | ... |
@@ -417,25 +417,25 @@ |
| 417 | 417 |
ueid = graph.id(edge); |
| 418 | 418 |
edge = graph.edgeFromId(ueid); |
| 419 | 419 |
ueid = graph.maxEdgeId(); |
| 420 | 420 |
ignore_unused_variable_warning(ueid); |
| 421 | 421 |
} |
| 422 | 422 |
|
| 423 | 423 |
const _Graph& graph; |
| 424 | 424 |
}; |
| 425 | 425 |
}; |
| 426 | 426 |
|
| 427 | 427 |
/// \brief Concept class for \c NodeIt, \c ArcIt and \c EdgeIt types. |
| 428 | 428 |
/// |
| 429 |
/// This class describes the concept of \c NodeIt, \c ArcIt and |
|
| 429 |
/// This class describes the concept of \c NodeIt, \c ArcIt and |
|
| 430 | 430 |
/// \c EdgeIt subtypes of digraph and graph types. |
| 431 | 431 |
template <typename GR, typename Item> |
| 432 | 432 |
class GraphItemIt : public Item {
|
| 433 | 433 |
public: |
| 434 | 434 |
/// \brief Default constructor. |
| 435 | 435 |
/// |
| 436 | 436 |
/// Default constructor. |
| 437 | 437 |
/// \warning The default constructor is not required to set |
| 438 | 438 |
/// the iterator to some well-defined value. So you should consider it |
| 439 | 439 |
/// as uninitialized. |
| 440 | 440 |
GraphItemIt() {}
|
| 441 | 441 |
|
| ... | ... |
@@ -457,25 +457,25 @@ |
| 457 | 457 |
GraphItemIt(Invalid) {}
|
| 458 | 458 |
|
| 459 | 459 |
/// \brief Assignment operator. |
| 460 | 460 |
/// |
| 461 | 461 |
/// Assignment operator for the iterator. |
| 462 | 462 |
GraphItemIt& operator=(const GraphItemIt&) { return *this; }
|
| 463 | 463 |
|
| 464 | 464 |
/// \brief Increment the iterator. |
| 465 | 465 |
/// |
| 466 | 466 |
/// This operator increments the iterator, i.e. assigns it to the |
| 467 | 467 |
/// next item. |
| 468 | 468 |
GraphItemIt& operator++() { return *this; }
|
| 469 |
|
|
| 469 |
|
|
| 470 | 470 |
/// \brief Equality operator |
| 471 | 471 |
/// |
| 472 | 472 |
/// Equality operator. |
| 473 | 473 |
/// Two iterators are equal if and only if they point to the |
| 474 | 474 |
/// same object or both are invalid. |
| 475 | 475 |
bool operator==(const GraphItemIt&) const { return true;}
|
| 476 | 476 |
|
| 477 | 477 |
/// \brief Inequality operator |
| 478 | 478 |
/// |
| 479 | 479 |
/// Inequality operator. |
| 480 | 480 |
/// Two iterators are equal if and only if they point to the |
| 481 | 481 |
/// same object or both are invalid. |
| ... | ... |
@@ -492,57 +492,57 @@ |
| 492 | 492 |
|
| 493 | 493 |
it2 = ++it1; |
| 494 | 494 |
++it2 = it1; |
| 495 | 495 |
++(++it1); |
| 496 | 496 |
|
| 497 | 497 |
Item bi = it1; |
| 498 | 498 |
bi = it2; |
| 499 | 499 |
} |
| 500 | 500 |
const GR& g; |
| 501 | 501 |
}; |
| 502 | 502 |
}; |
| 503 | 503 |
|
| 504 |
/// \brief Concept class for \c InArcIt, \c OutArcIt and |
|
| 504 |
/// \brief Concept class for \c InArcIt, \c OutArcIt and |
|
| 505 | 505 |
/// \c IncEdgeIt types. |
| 506 | 506 |
/// |
| 507 |
/// This class describes the concept of \c InArcIt, \c OutArcIt |
|
| 507 |
/// This class describes the concept of \c InArcIt, \c OutArcIt |
|
| 508 | 508 |
/// and \c IncEdgeIt subtypes of digraph and graph types. |
| 509 | 509 |
/// |
| 510 | 510 |
/// \note Since these iterator classes do not inherit from the same |
| 511 | 511 |
/// base class, there is an additional template parameter (selector) |
| 512 |
/// \c sel. For \c InArcIt you should instantiate it with character |
|
| 512 |
/// \c sel. For \c InArcIt you should instantiate it with character |
|
| 513 | 513 |
/// \c 'i', for \c OutArcIt with \c 'o' and for \c IncEdgeIt with \c 'e'. |
| 514 | 514 |
template <typename GR, |
| 515 | 515 |
typename Item = typename GR::Arc, |
| 516 | 516 |
typename Base = typename GR::Node, |
| 517 | 517 |
char sel = '0'> |
| 518 | 518 |
class GraphIncIt : public Item {
|
| 519 | 519 |
public: |
| 520 | 520 |
/// \brief Default constructor. |
| 521 | 521 |
/// |
| 522 | 522 |
/// Default constructor. |
| 523 | 523 |
/// \warning The default constructor is not required to set |
| 524 | 524 |
/// the iterator to some well-defined value. So you should consider it |
| 525 | 525 |
/// as uninitialized. |
| 526 | 526 |
GraphIncIt() {}
|
| 527 | 527 |
|
| 528 | 528 |
/// \brief Copy constructor. |
| 529 | 529 |
/// |
| 530 | 530 |
/// Copy constructor. |
| 531 | 531 |
GraphIncIt(const GraphIncIt& it) : Item(it) {}
|
| 532 | 532 |
|
| 533 |
/// \brief Constructor that sets the iterator to the first |
|
| 533 |
/// \brief Constructor that sets the iterator to the first |
|
| 534 | 534 |
/// incoming or outgoing arc. |
| 535 | 535 |
/// |
| 536 |
/// Constructor that sets the iterator to the first arc |
|
| 536 |
/// Constructor that sets the iterator to the first arc |
|
| 537 | 537 |
/// incoming to or outgoing from the given node. |
| 538 | 538 |
explicit GraphIncIt(const GR&, const Base&) {}
|
| 539 | 539 |
|
| 540 | 540 |
/// \brief Constructor for conversion from \c INVALID. |
| 541 | 541 |
/// |
| 542 | 542 |
/// Constructor for conversion from \c INVALID. |
| 543 | 543 |
/// It initializes the iterator to be invalid. |
| 544 | 544 |
/// \sa Invalid for more details. |
| 545 | 545 |
GraphIncIt(Invalid) {}
|
| 546 | 546 |
|
| 547 | 547 |
/// \brief Assignment operator. |
| 548 | 548 |
/// |
| ... | ... |
@@ -795,34 +795,34 @@ |
| 795 | 795 |
/// \brief Return the first edge. |
| 796 | 796 |
/// |
| 797 | 797 |
/// This function gives back the first edge in the iteration order. |
| 798 | 798 |
void first(Edge&) const {}
|
| 799 | 799 |
|
| 800 | 800 |
/// \brief Return the next edge. |
| 801 | 801 |
/// |
| 802 | 802 |
/// This function gives back the next edge in the iteration order. |
| 803 | 803 |
void next(Edge&) const {}
|
| 804 | 804 |
|
| 805 | 805 |
/// \brief Return the first edge incident to the given node. |
| 806 | 806 |
/// |
| 807 |
/// This function gives back the first edge incident to the given |
|
| 807 |
/// This function gives back the first edge incident to the given |
|
| 808 | 808 |
/// node. The bool parameter gives back the direction for which the |
| 809 |
/// source node of the directed arc representing the edge is the |
|
| 809 |
/// source node of the directed arc representing the edge is the |
|
| 810 | 810 |
/// given node. |
| 811 | 811 |
void firstInc(Edge&, bool&, const Node&) const {}
|
| 812 | 812 |
|
| 813 | 813 |
/// \brief Gives back the next of the edges from the |
| 814 | 814 |
/// given node. |
| 815 | 815 |
/// |
| 816 |
/// This function gives back the next edge incident to the given |
|
| 816 |
/// This function gives back the next edge incident to the given |
|
| 817 | 817 |
/// node. The bool parameter should be used as \c firstInc() use it. |
| 818 | 818 |
void nextInc(Edge&, bool&) const {}
|
| 819 | 819 |
|
| 820 | 820 |
using IterableDigraphComponent<Base>::baseNode; |
| 821 | 821 |
using IterableDigraphComponent<Base>::runningNode; |
| 822 | 822 |
|
| 823 | 823 |
/// @} |
| 824 | 824 |
|
| 825 | 825 |
/// \name Class Based Iteration |
| 826 | 826 |
/// |
| 827 | 827 |
/// This interface provides iterator classes for edges. |
| 828 | 828 |
/// |
| ... | ... |
@@ -981,25 +981,25 @@ |
| 981 | 981 |
typename _Graph::EdgeNotifier& uen |
| 982 | 982 |
= graph.notifier(typename _Graph::Edge()); |
| 983 | 983 |
ignore_unused_variable_warning(uen); |
| 984 | 984 |
} |
| 985 | 985 |
|
| 986 | 986 |
const _Graph& graph; |
| 987 | 987 |
}; |
| 988 | 988 |
}; |
| 989 | 989 |
|
| 990 | 990 |
/// \brief Concept class for standard graph maps. |
| 991 | 991 |
/// |
| 992 | 992 |
/// This class describes the concept of standard graph maps, i.e. |
| 993 |
/// the \c NodeMap, \c ArcMap and \c EdgeMap subtypes of digraph and |
|
| 993 |
/// the \c NodeMap, \c ArcMap and \c EdgeMap subtypes of digraph and |
|
| 994 | 994 |
/// graph types, which can be used for associating data to graph items. |
| 995 | 995 |
/// The standard graph maps must conform to the ReferenceMap concept. |
| 996 | 996 |
template <typename GR, typename K, typename V> |
| 997 | 997 |
class GraphMap : public ReferenceMap<K, V, V&, const V&> {
|
| 998 | 998 |
typedef ReferenceMap<K, V, V&, const V&> Parent; |
| 999 | 999 |
|
| 1000 | 1000 |
public: |
| 1001 | 1001 |
|
| 1002 | 1002 |
/// The key type of the map. |
| 1003 | 1003 |
typedef K Key; |
| 1004 | 1004 |
/// The value type of the map. |
| 1005 | 1005 |
typedef V Value; |
| ... | ... |
@@ -1036,48 +1036,48 @@ |
| 1036 | 1036 |
checkConcept<ReadMap<Key, Value>, CMap>(); |
| 1037 | 1037 |
return *this; |
| 1038 | 1038 |
} |
| 1039 | 1039 |
|
| 1040 | 1040 |
public: |
| 1041 | 1041 |
template<typename _Map> |
| 1042 | 1042 |
struct Constraints {
|
| 1043 | 1043 |
void constraints() {
|
| 1044 | 1044 |
checkConcept |
| 1045 | 1045 |
<ReferenceMap<Key, Value, Value&, const Value&>, _Map>(); |
| 1046 | 1046 |
_Map m1(g); |
| 1047 | 1047 |
_Map m2(g,t); |
| 1048 |
|
|
| 1048 |
|
|
| 1049 | 1049 |
// Copy constructor |
| 1050 | 1050 |
// _Map m3(m); |
| 1051 | 1051 |
|
| 1052 | 1052 |
// Assignment operator |
| 1053 | 1053 |
// ReadMap<Key, Value> cmap; |
| 1054 | 1054 |
// m3 = cmap; |
| 1055 | 1055 |
|
| 1056 | 1056 |
ignore_unused_variable_warning(m1); |
| 1057 | 1057 |
ignore_unused_variable_warning(m2); |
| 1058 | 1058 |
// ignore_unused_variable_warning(m3); |
| 1059 | 1059 |
} |
| 1060 | 1060 |
|
| 1061 | 1061 |
const _Map &m; |
| 1062 | 1062 |
const GR &g; |
| 1063 | 1063 |
const typename GraphMap::Value &t; |
| 1064 | 1064 |
}; |
| 1065 | 1065 |
|
| 1066 | 1066 |
}; |
| 1067 | 1067 |
|
| 1068 | 1068 |
/// \brief Skeleton class for mappable directed graphs. |
| 1069 | 1069 |
/// |
| 1070 | 1070 |
/// This class describes the interface of mappable directed graphs. |
| 1071 |
/// It extends \ref BaseDigraphComponent with the standard digraph |
|
| 1071 |
/// It extends \ref BaseDigraphComponent with the standard digraph |
|
| 1072 | 1072 |
/// map classes, namely \c NodeMap and \c ArcMap. |
| 1073 | 1073 |
/// This concept is part of the Digraph concept. |
| 1074 | 1074 |
template <typename BAS = BaseDigraphComponent> |
| 1075 | 1075 |
class MappableDigraphComponent : public BAS {
|
| 1076 | 1076 |
public: |
| 1077 | 1077 |
|
| 1078 | 1078 |
typedef BAS Base; |
| 1079 | 1079 |
typedef typename Base::Node Node; |
| 1080 | 1080 |
typedef typename Base::Arc Arc; |
| 1081 | 1081 |
|
| 1082 | 1082 |
typedef MappableDigraphComponent Digraph; |
| 1083 | 1083 |
|
| ... | ... |
@@ -1196,25 +1196,25 @@ |
| 1196 | 1196 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, Dummy>, |
| 1197 | 1197 |
DummyArcMap >(); |
| 1198 | 1198 |
} |
| 1199 | 1199 |
} |
| 1200 | 1200 |
|
| 1201 | 1201 |
const _Digraph& digraph; |
| 1202 | 1202 |
}; |
| 1203 | 1203 |
}; |
| 1204 | 1204 |
|
| 1205 | 1205 |
/// \brief Skeleton class for mappable undirected graphs. |
| 1206 | 1206 |
/// |
| 1207 | 1207 |
/// This class describes the interface of mappable undirected graphs. |
| 1208 |
/// It extends \ref MappableDigraphComponent with the standard graph |
|
| 1208 |
/// It extends \ref MappableDigraphComponent with the standard graph |
|
| 1209 | 1209 |
/// map class for edges (\c EdgeMap). |
| 1210 | 1210 |
/// This concept is part of the Graph concept. |
| 1211 | 1211 |
template <typename BAS = BaseGraphComponent> |
| 1212 | 1212 |
class MappableGraphComponent : public MappableDigraphComponent<BAS> {
|
| 1213 | 1213 |
public: |
| 1214 | 1214 |
|
| 1215 | 1215 |
typedef BAS Base; |
| 1216 | 1216 |
typedef typename Base::Edge Edge; |
| 1217 | 1217 |
|
| 1218 | 1218 |
typedef MappableGraphComponent Graph; |
| 1219 | 1219 |
|
| 1220 | 1220 |
/// \brief Standard graph map for the edges. |
| ... | ... |
@@ -1281,25 +1281,25 @@ |
| 1281 | 1281 |
checkConcept<GraphMap<_Graph, typename _Graph::Edge, Dummy>, |
| 1282 | 1282 |
DummyEdgeMap >(); |
| 1283 | 1283 |
} |
| 1284 | 1284 |
} |
| 1285 | 1285 |
|
| 1286 | 1286 |
const _Graph& graph; |
| 1287 | 1287 |
}; |
| 1288 | 1288 |
}; |
| 1289 | 1289 |
|
| 1290 | 1290 |
/// \brief Skeleton class for extendable directed graphs. |
| 1291 | 1291 |
/// |
| 1292 | 1292 |
/// This class describes the interface of extendable directed graphs. |
| 1293 |
/// It extends \ref BaseDigraphComponent with functions for adding |
|
| 1293 |
/// It extends \ref BaseDigraphComponent with functions for adding |
|
| 1294 | 1294 |
/// nodes and arcs to the digraph. |
| 1295 | 1295 |
/// This concept requires \ref AlterableDigraphComponent. |
| 1296 | 1296 |
template <typename BAS = BaseDigraphComponent> |
| 1297 | 1297 |
class ExtendableDigraphComponent : public BAS {
|
| 1298 | 1298 |
public: |
| 1299 | 1299 |
typedef BAS Base; |
| 1300 | 1300 |
|
| 1301 | 1301 |
typedef typename Base::Node Node; |
| 1302 | 1302 |
typedef typename Base::Arc Arc; |
| 1303 | 1303 |
|
| 1304 | 1304 |
/// \brief Add a new node to the digraph. |
| 1305 | 1305 |
/// |
| ... | ... |
@@ -1325,25 +1325,25 @@ |
| 1325 | 1325 |
node_b = digraph.addNode(); |
| 1326 | 1326 |
typename _Digraph::Arc arc; |
| 1327 | 1327 |
arc = digraph.addArc(node_a, node_b); |
| 1328 | 1328 |
} |
| 1329 | 1329 |
|
| 1330 | 1330 |
_Digraph& digraph; |
| 1331 | 1331 |
}; |
| 1332 | 1332 |
}; |
| 1333 | 1333 |
|
| 1334 | 1334 |
/// \brief Skeleton class for extendable undirected graphs. |
| 1335 | 1335 |
/// |
| 1336 | 1336 |
/// This class describes the interface of extendable undirected graphs. |
| 1337 |
/// It extends \ref BaseGraphComponent with functions for adding |
|
| 1337 |
/// It extends \ref BaseGraphComponent with functions for adding |
|
| 1338 | 1338 |
/// nodes and edges to the graph. |
| 1339 | 1339 |
/// This concept requires \ref AlterableGraphComponent. |
| 1340 | 1340 |
template <typename BAS = BaseGraphComponent> |
| 1341 | 1341 |
class ExtendableGraphComponent : public BAS {
|
| 1342 | 1342 |
public: |
| 1343 | 1343 |
|
| 1344 | 1344 |
typedef BAS Base; |
| 1345 | 1345 |
typedef typename Base::Node Node; |
| 1346 | 1346 |
typedef typename Base::Edge Edge; |
| 1347 | 1347 |
|
| 1348 | 1348 |
/// \brief Add a new node to the digraph. |
| 1349 | 1349 |
/// |
| ... | ... |
@@ -1369,38 +1369,38 @@ |
| 1369 | 1369 |
node_b = graph.addNode(); |
| 1370 | 1370 |
typename _Graph::Edge edge; |
| 1371 | 1371 |
edge = graph.addEdge(node_a, node_b); |
| 1372 | 1372 |
} |
| 1373 | 1373 |
|
| 1374 | 1374 |
_Graph& graph; |
| 1375 | 1375 |
}; |
| 1376 | 1376 |
}; |
| 1377 | 1377 |
|
| 1378 | 1378 |
/// \brief Skeleton class for erasable directed graphs. |
| 1379 | 1379 |
/// |
| 1380 | 1380 |
/// This class describes the interface of erasable directed graphs. |
| 1381 |
/// It extends \ref BaseDigraphComponent with functions for removing |
|
| 1381 |
/// It extends \ref BaseDigraphComponent with functions for removing |
|
| 1382 | 1382 |
/// nodes and arcs from the digraph. |
| 1383 | 1383 |
/// This concept requires \ref AlterableDigraphComponent. |
| 1384 | 1384 |
template <typename BAS = BaseDigraphComponent> |
| 1385 | 1385 |
class ErasableDigraphComponent : public BAS {
|
| 1386 | 1386 |
public: |
| 1387 | 1387 |
|
| 1388 | 1388 |
typedef BAS Base; |
| 1389 | 1389 |
typedef typename Base::Node Node; |
| 1390 | 1390 |
typedef typename Base::Arc Arc; |
| 1391 | 1391 |
|
| 1392 | 1392 |
/// \brief Erase a node from the digraph. |
| 1393 | 1393 |
/// |
| 1394 |
/// This function erases the given node from the digraph and all arcs |
|
| 1394 |
/// This function erases the given node from the digraph and all arcs |
|
| 1395 | 1395 |
/// connected to the node. |
| 1396 | 1396 |
void erase(const Node&) {}
|
| 1397 | 1397 |
|
| 1398 | 1398 |
/// \brief Erase an arc from the digraph. |
| 1399 | 1399 |
/// |
| 1400 | 1400 |
/// This function erases the given arc from the digraph. |
| 1401 | 1401 |
void erase(const Arc&) {}
|
| 1402 | 1402 |
|
| 1403 | 1403 |
template <typename _Digraph> |
| 1404 | 1404 |
struct Constraints {
|
| 1405 | 1405 |
void constraints() {
|
| 1406 | 1406 |
checkConcept<Base, _Digraph>(); |
| ... | ... |
@@ -1408,25 +1408,25 @@ |
| 1408 | 1408 |
digraph.erase(node); |
| 1409 | 1409 |
const typename _Digraph::Arc arc(INVALID); |
| 1410 | 1410 |
digraph.erase(arc); |
| 1411 | 1411 |
} |
| 1412 | 1412 |
|
| 1413 | 1413 |
_Digraph& digraph; |
| 1414 | 1414 |
}; |
| 1415 | 1415 |
}; |
| 1416 | 1416 |
|
| 1417 | 1417 |
/// \brief Skeleton class for erasable undirected graphs. |
| 1418 | 1418 |
/// |
| 1419 | 1419 |
/// This class describes the interface of erasable undirected graphs. |
| 1420 |
/// It extends \ref BaseGraphComponent with functions for removing |
|
| 1420 |
/// It extends \ref BaseGraphComponent with functions for removing |
|
| 1421 | 1421 |
/// nodes and edges from the graph. |
| 1422 | 1422 |
/// This concept requires \ref AlterableGraphComponent. |
| 1423 | 1423 |
template <typename BAS = BaseGraphComponent> |
| 1424 | 1424 |
class ErasableGraphComponent : public BAS {
|
| 1425 | 1425 |
public: |
| 1426 | 1426 |
|
| 1427 | 1427 |
typedef BAS Base; |
| 1428 | 1428 |
typedef typename Base::Node Node; |
| 1429 | 1429 |
typedef typename Base::Edge Edge; |
| 1430 | 1430 |
|
| 1431 | 1431 |
/// \brief Erase a node from the graph. |
| 1432 | 1432 |
/// |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -83,39 +83,39 @@ |
| 83 | 83 |
|
| 84 | 84 |
/// \brief Constructor. |
| 85 | 85 |
/// |
| 86 | 86 |
/// Constructor. |
| 87 | 87 |
/// \param map A map that assigns \c int values to keys of type |
| 88 | 88 |
/// \c Item. It is used internally by the heap implementations to |
| 89 | 89 |
/// handle the cross references. The assigned value must be |
| 90 | 90 |
/// \c PRE_HEAP (<tt>-1</tt>) for each item. |
| 91 | 91 |
#ifdef DOXYGEN |
| 92 | 92 |
explicit Heap(ItemIntMap &map) {}
|
| 93 | 93 |
#else |
| 94 | 94 |
explicit Heap(ItemIntMap&) {}
|
| 95 |
#endif |
|
| 95 |
#endif |
|
| 96 | 96 |
|
| 97 | 97 |
/// \brief Constructor. |
| 98 | 98 |
/// |
| 99 | 99 |
/// Constructor. |
| 100 | 100 |
/// \param map A map that assigns \c int values to keys of type |
| 101 | 101 |
/// \c Item. It is used internally by the heap implementations to |
| 102 | 102 |
/// handle the cross references. The assigned value must be |
| 103 | 103 |
/// \c PRE_HEAP (<tt>-1</tt>) for each item. |
| 104 | 104 |
/// \param comp The function object used for comparing the priorities. |
| 105 | 105 |
#ifdef DOXYGEN |
| 106 | 106 |
explicit Heap(ItemIntMap &map, const CMP &comp) {}
|
| 107 | 107 |
#else |
| 108 | 108 |
explicit Heap(ItemIntMap&, const CMP&) {}
|
| 109 |
#endif |
|
| 109 |
#endif |
|
| 110 | 110 |
|
| 111 | 111 |
/// \brief The number of items stored in the heap. |
| 112 | 112 |
/// |
| 113 | 113 |
/// This function returns the number of items stored in the heap. |
| 114 | 114 |
int size() const { return 0; }
|
| 115 | 115 |
|
| 116 | 116 |
/// \brief Check if the heap is empty. |
| 117 | 117 |
/// |
| 118 | 118 |
/// This function returns \c true if the heap is empty. |
| 119 | 119 |
bool empty() const { return false; }
|
| 120 | 120 |
|
| 121 | 121 |
/// \brief Make the heap empty. |
| ... | ... |
@@ -129,25 +129,25 @@ |
| 129 | 129 |
|
| 130 | 130 |
/// \brief Insert an item into the heap with the given priority. |
| 131 | 131 |
/// |
| 132 | 132 |
/// This function inserts the given item into the heap with the |
| 133 | 133 |
/// given priority. |
| 134 | 134 |
/// \param i The item to insert. |
| 135 | 135 |
/// \param p The priority of the item. |
| 136 | 136 |
/// \pre \e i must not be stored in the heap. |
| 137 | 137 |
#ifdef DOXYGEN |
| 138 | 138 |
void push(const Item &i, const Prio &p) {}
|
| 139 | 139 |
#else |
| 140 | 140 |
void push(const Item&, const Prio&) {}
|
| 141 |
#endif |
|
| 141 |
#endif |
|
| 142 | 142 |
|
| 143 | 143 |
/// \brief Return the item having minimum priority. |
| 144 | 144 |
/// |
| 145 | 145 |
/// This function returns the item having minimum priority. |
| 146 | 146 |
/// \pre The heap must be non-empty. |
| 147 | 147 |
Item top() const { return Item(); }
|
| 148 | 148 |
|
| 149 | 149 |
/// \brief The minimum priority. |
| 150 | 150 |
/// |
| 151 | 151 |
/// This function returns the minimum priority. |
| 152 | 152 |
/// \pre The heap must be non-empty. |
| 153 | 153 |
Prio prio() const { return Prio(); }
|
| ... | ... |
@@ -159,102 +159,102 @@ |
| 159 | 159 |
void pop() {}
|
| 160 | 160 |
|
| 161 | 161 |
/// \brief Remove the given item from the heap. |
| 162 | 162 |
/// |
| 163 | 163 |
/// This function removes the given item from the heap if it is |
| 164 | 164 |
/// already stored. |
| 165 | 165 |
/// \param i The item to delete. |
| 166 | 166 |
/// \pre \e i must be in the heap. |
| 167 | 167 |
#ifdef DOXYGEN |
| 168 | 168 |
void erase(const Item &i) {}
|
| 169 | 169 |
#else |
| 170 | 170 |
void erase(const Item&) {}
|
| 171 |
#endif |
|
| 171 |
#endif |
|
| 172 | 172 |
|
| 173 | 173 |
/// \brief The priority of the given item. |
| 174 | 174 |
/// |
| 175 | 175 |
/// This function returns the priority of the given item. |
| 176 | 176 |
/// \param i The item. |
| 177 | 177 |
/// \pre \e i must be in the heap. |
| 178 | 178 |
#ifdef DOXYGEN |
| 179 | 179 |
Prio operator[](const Item &i) const {}
|
| 180 | 180 |
#else |
| 181 | 181 |
Prio operator[](const Item&) const { return Prio(); }
|
| 182 |
#endif |
|
| 182 |
#endif |
|
| 183 | 183 |
|
| 184 | 184 |
/// \brief Set the priority of an item or insert it, if it is |
| 185 | 185 |
/// not stored in the heap. |
| 186 | 186 |
/// |
| 187 | 187 |
/// This method sets the priority of the given item if it is |
| 188 | 188 |
/// already stored in the heap. Otherwise it inserts the given |
| 189 | 189 |
/// item into the heap with the given priority. |
| 190 | 190 |
/// |
| 191 | 191 |
/// \param i The item. |
| 192 | 192 |
/// \param p The priority. |
| 193 | 193 |
#ifdef DOXYGEN |
| 194 | 194 |
void set(const Item &i, const Prio &p) {}
|
| 195 | 195 |
#else |
| 196 | 196 |
void set(const Item&, const Prio&) {}
|
| 197 |
#endif |
|
| 197 |
#endif |
|
| 198 | 198 |
|
| 199 | 199 |
/// \brief Decrease the priority of an item to the given value. |
| 200 | 200 |
/// |
| 201 | 201 |
/// This function decreases the priority of an item to the given value. |
| 202 | 202 |
/// \param i The item. |
| 203 | 203 |
/// \param p The priority. |
| 204 | 204 |
/// \pre \e i must be stored in the heap with priority at least \e p. |
| 205 | 205 |
#ifdef DOXYGEN |
| 206 | 206 |
void decrease(const Item &i, const Prio &p) {}
|
| 207 | 207 |
#else |
| 208 | 208 |
void decrease(const Item&, const Prio&) {}
|
| 209 |
#endif |
|
| 209 |
#endif |
|
| 210 | 210 |
|
| 211 | 211 |
/// \brief Increase the priority of an item to the given value. |
| 212 | 212 |
/// |
| 213 | 213 |
/// This function increases the priority of an item to the given value. |
| 214 | 214 |
/// \param i The item. |
| 215 | 215 |
/// \param p The priority. |
| 216 | 216 |
/// \pre \e i must be stored in the heap with priority at most \e p. |
| 217 | 217 |
#ifdef DOXYGEN |
| 218 | 218 |
void increase(const Item &i, const Prio &p) {}
|
| 219 | 219 |
#else |
| 220 | 220 |
void increase(const Item&, const Prio&) {}
|
| 221 |
#endif |
|
| 221 |
#endif |
|
| 222 | 222 |
|
| 223 | 223 |
/// \brief Return the state of an item. |
| 224 | 224 |
/// |
| 225 | 225 |
/// This method returns \c PRE_HEAP if the given item has never |
| 226 | 226 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
| 227 | 227 |
/// and \c POST_HEAP otherwise. |
| 228 | 228 |
/// In the latter case it is possible that the item will get back |
| 229 | 229 |
/// to the heap again. |
| 230 | 230 |
/// \param i The item. |
| 231 | 231 |
#ifdef DOXYGEN |
| 232 | 232 |
State state(const Item &i) const {}
|
| 233 | 233 |
#else |
| 234 | 234 |
State state(const Item&) const { return PRE_HEAP; }
|
| 235 |
#endif |
|
| 235 |
#endif |
|
| 236 | 236 |
|
| 237 | 237 |
/// \brief Set the state of an item in the heap. |
| 238 | 238 |
/// |
| 239 | 239 |
/// This function sets the state of the given item in the heap. |
| 240 | 240 |
/// It can be used to manually clear the heap when it is important |
| 241 | 241 |
/// to achive better time complexity. |
| 242 | 242 |
/// \param i The item. |
| 243 | 243 |
/// \param st The state. It should not be \c IN_HEAP. |
| 244 | 244 |
#ifdef DOXYGEN |
| 245 | 245 |
void state(const Item& i, State st) {}
|
| 246 | 246 |
#else |
| 247 | 247 |
void state(const Item&, State) {}
|
| 248 |
#endif |
|
| 248 |
#endif |
|
| 249 | 249 |
|
| 250 | 250 |
|
| 251 | 251 |
template <typename _Heap> |
| 252 | 252 |
struct Constraints {
|
| 253 | 253 |
public: |
| 254 | 254 |
void constraints() {
|
| 255 | 255 |
typedef typename _Heap::Item OwnItem; |
| 256 | 256 |
typedef typename _Heap::Prio OwnPrio; |
| 257 | 257 |
typedef typename _Heap::State OwnState; |
| 258 | 258 |
|
| 259 | 259 |
Item item; |
| 260 | 260 |
Prio prio; |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -249,25 +249,25 @@ |
| 249 | 249 |
|
| 250 | 250 |
|
| 251 | 251 |
/// \ingroup graph_properties |
| 252 | 252 |
/// |
| 253 | 253 |
/// \brief Check whether a directed graph is strongly connected. |
| 254 | 254 |
/// |
| 255 | 255 |
/// This function checks whether the given directed graph is strongly |
| 256 | 256 |
/// connected, i.e. any two nodes of the digraph are |
| 257 | 257 |
/// connected with directed paths in both direction. |
| 258 | 258 |
/// |
| 259 | 259 |
/// \return \c true if the digraph is strongly connected. |
| 260 | 260 |
/// \note By definition, the empty digraph is strongly connected. |
| 261 |
/// |
|
| 261 |
/// |
|
| 262 | 262 |
/// \see countStronglyConnectedComponents(), stronglyConnectedComponents() |
| 263 | 263 |
/// \see connected() |
| 264 | 264 |
template <typename Digraph> |
| 265 | 265 |
bool stronglyConnected(const Digraph& digraph) {
|
| 266 | 266 |
checkConcept<concepts::Digraph, Digraph>(); |
| 267 | 267 |
|
| 268 | 268 |
typedef typename Digraph::Node Node; |
| 269 | 269 |
typedef typename Digraph::NodeIt NodeIt; |
| 270 | 270 |
|
| 271 | 271 |
typename Digraph::Node source = NodeIt(digraph); |
| 272 | 272 |
if (source == INVALID) return true; |
| 273 | 273 |
|
| ... | ... |
@@ -301,25 +301,25 @@ |
| 301 | 301 |
|
| 302 | 302 |
for (RNodeIt it(rdigraph); it != INVALID; ++it) {
|
| 303 | 303 |
if (!rdfs.reached(it)) {
|
| 304 | 304 |
return false; |
| 305 | 305 |
} |
| 306 | 306 |
} |
| 307 | 307 |
|
| 308 | 308 |
return true; |
| 309 | 309 |
} |
| 310 | 310 |
|
| 311 | 311 |
/// \ingroup graph_properties |
| 312 | 312 |
/// |
| 313 |
/// \brief Count the number of strongly connected components of a |
|
| 313 |
/// \brief Count the number of strongly connected components of a |
|
| 314 | 314 |
/// directed graph |
| 315 | 315 |
/// |
| 316 | 316 |
/// This function counts the number of strongly connected components of |
| 317 | 317 |
/// the given directed graph. |
| 318 | 318 |
/// |
| 319 | 319 |
/// The strongly connected components are the classes of an |
| 320 | 320 |
/// equivalence relation on the nodes of a digraph. Two nodes are in |
| 321 | 321 |
/// the same class if they are connected with directed paths in both |
| 322 | 322 |
/// direction. |
| 323 | 323 |
/// |
| 324 | 324 |
/// \return The number of strongly connected components. |
| 325 | 325 |
/// \note By definition, the empty digraph has zero |
| ... | ... |
@@ -735,39 +735,39 @@ |
| 735 | 735 |
bool rootCut; |
| 736 | 736 |
}; |
| 737 | 737 |
|
| 738 | 738 |
} |
| 739 | 739 |
|
| 740 | 740 |
template <typename Graph> |
| 741 | 741 |
int countBiNodeConnectedComponents(const Graph& graph); |
| 742 | 742 |
|
| 743 | 743 |
/// \ingroup graph_properties |
| 744 | 744 |
/// |
| 745 | 745 |
/// \brief Check whether an undirected graph is bi-node-connected. |
| 746 | 746 |
/// |
| 747 |
/// This function checks whether the given undirected graph is |
|
| 747 |
/// This function checks whether the given undirected graph is |
|
| 748 | 748 |
/// bi-node-connected, i.e. any two edges are on same circle. |
| 749 | 749 |
/// |
| 750 | 750 |
/// \return \c true if the graph bi-node-connected. |
| 751 | 751 |
/// \note By definition, the empty graph is bi-node-connected. |
| 752 | 752 |
/// |
| 753 | 753 |
/// \see countBiNodeConnectedComponents(), biNodeConnectedComponents() |
| 754 | 754 |
template <typename Graph> |
| 755 | 755 |
bool biNodeConnected(const Graph& graph) {
|
| 756 | 756 |
return countBiNodeConnectedComponents(graph) <= 1; |
| 757 | 757 |
} |
| 758 | 758 |
|
| 759 | 759 |
/// \ingroup graph_properties |
| 760 | 760 |
/// |
| 761 |
/// \brief Count the number of bi-node-connected components of an |
|
| 761 |
/// \brief Count the number of bi-node-connected components of an |
|
| 762 | 762 |
/// undirected graph. |
| 763 | 763 |
/// |
| 764 | 764 |
/// This function counts the number of bi-node-connected components of |
| 765 | 765 |
/// the given undirected graph. |
| 766 | 766 |
/// |
| 767 | 767 |
/// The bi-node-connected components are the classes of an equivalence |
| 768 | 768 |
/// relation on the edges of a undirected graph. Two edges are in the |
| 769 | 769 |
/// same class if they are on same circle. |
| 770 | 770 |
/// |
| 771 | 771 |
/// \return The number of bi-node-connected components. |
| 772 | 772 |
/// |
| 773 | 773 |
/// \see biNodeConnected(), biNodeConnectedComponents() |
| ... | ... |
@@ -803,25 +803,25 @@ |
| 803 | 803 |
/// undirected graph. |
| 804 | 804 |
/// |
| 805 | 805 |
/// The bi-node-connected components are the classes of an equivalence |
| 806 | 806 |
/// relation on the edges of a undirected graph. Two edges are in the |
| 807 | 807 |
/// same class if they are on same circle. |
| 808 | 808 |
/// |
| 809 | 809 |
/// \image html node_biconnected_components.png |
| 810 | 810 |
/// \image latex node_biconnected_components.eps "bi-node-connected components" width=\textwidth |
| 811 | 811 |
/// |
| 812 | 812 |
/// \param graph The undirected graph. |
| 813 | 813 |
/// \retval compMap A writable edge map. The values will be set from 0 |
| 814 | 814 |
/// to the number of the bi-node-connected components minus one. Each |
| 815 |
/// value of the map will be set exactly once, and the values of a |
|
| 815 |
/// value of the map will be set exactly once, and the values of a |
|
| 816 | 816 |
/// certain component will be set continuously. |
| 817 | 817 |
/// \return The number of bi-node-connected components. |
| 818 | 818 |
/// |
| 819 | 819 |
/// \see biNodeConnected(), countBiNodeConnectedComponents() |
| 820 | 820 |
template <typename Graph, typename EdgeMap> |
| 821 | 821 |
int biNodeConnectedComponents(const Graph& graph, |
| 822 | 822 |
EdgeMap& compMap) {
|
| 823 | 823 |
checkConcept<concepts::Graph, Graph>(); |
| 824 | 824 |
typedef typename Graph::NodeIt NodeIt; |
| 825 | 825 |
typedef typename Graph::Edge Edge; |
| 826 | 826 |
checkConcept<concepts::WriteMap<Edge, int>, EdgeMap>(); |
| 827 | 827 |
|
| ... | ... |
@@ -849,25 +849,25 @@ |
| 849 | 849 |
/// \brief Find the bi-node-connected cut nodes in an undirected graph. |
| 850 | 850 |
/// |
| 851 | 851 |
/// This function finds the bi-node-connected cut nodes in the given |
| 852 | 852 |
/// undirected graph. |
| 853 | 853 |
/// |
| 854 | 854 |
/// The bi-node-connected components are the classes of an equivalence |
| 855 | 855 |
/// relation on the edges of a undirected graph. Two edges are in the |
| 856 | 856 |
/// same class if they are on same circle. |
| 857 | 857 |
/// The bi-node-connected components are separted by the cut nodes of |
| 858 | 858 |
/// the components. |
| 859 | 859 |
/// |
| 860 | 860 |
/// \param graph The undirected graph. |
| 861 |
/// \retval cutMap A writable node map. The values will be set to |
|
| 861 |
/// \retval cutMap A writable node map. The values will be set to |
|
| 862 | 862 |
/// \c true for the nodes that separate two or more components |
| 863 | 863 |
/// (exactly once for each cut node), and will not be changed for |
| 864 | 864 |
/// other nodes. |
| 865 | 865 |
/// \return The number of the cut nodes. |
| 866 | 866 |
/// |
| 867 | 867 |
/// \see biNodeConnected(), biNodeConnectedComponents() |
| 868 | 868 |
template <typename Graph, typename NodeMap> |
| 869 | 869 |
int biNodeConnectedCutNodes(const Graph& graph, NodeMap& cutMap) {
|
| 870 | 870 |
checkConcept<concepts::Graph, Graph>(); |
| 871 | 871 |
typedef typename Graph::Node Node; |
| 872 | 872 |
typedef typename Graph::NodeIt NodeIt; |
| 873 | 873 |
checkConcept<concepts::WriteMap<Node, bool>, NodeMap>(); |
| ... | ... |
@@ -1076,25 +1076,25 @@ |
| 1076 | 1076 |
typename Digraph::template NodeMap<Arc> _predMap; |
| 1077 | 1077 |
int _num; |
| 1078 | 1078 |
}; |
| 1079 | 1079 |
} |
| 1080 | 1080 |
|
| 1081 | 1081 |
template <typename Graph> |
| 1082 | 1082 |
int countBiEdgeConnectedComponents(const Graph& graph); |
| 1083 | 1083 |
|
| 1084 | 1084 |
/// \ingroup graph_properties |
| 1085 | 1085 |
/// |
| 1086 | 1086 |
/// \brief Check whether an undirected graph is bi-edge-connected. |
| 1087 | 1087 |
/// |
| 1088 |
/// This function checks whether the given undirected graph is |
|
| 1088 |
/// This function checks whether the given undirected graph is |
|
| 1089 | 1089 |
/// bi-edge-connected, i.e. any two nodes are connected with at least |
| 1090 | 1090 |
/// two edge-disjoint paths. |
| 1091 | 1091 |
/// |
| 1092 | 1092 |
/// \return \c true if the graph is bi-edge-connected. |
| 1093 | 1093 |
/// \note By definition, the empty graph is bi-edge-connected. |
| 1094 | 1094 |
/// |
| 1095 | 1095 |
/// \see countBiEdgeConnectedComponents(), biEdgeConnectedComponents() |
| 1096 | 1096 |
template <typename Graph> |
| 1097 | 1097 |
bool biEdgeConnected(const Graph& graph) {
|
| 1098 | 1098 |
return countBiEdgeConnectedComponents(graph) <= 1; |
| 1099 | 1099 |
} |
| 1100 | 1100 |
|
| ... | ... |
@@ -1183,25 +1183,25 @@ |
| 1183 | 1183 |
dfs.addSource(it); |
| 1184 | 1184 |
dfs.start(); |
| 1185 | 1185 |
} |
| 1186 | 1186 |
} |
| 1187 | 1187 |
return compNum; |
| 1188 | 1188 |
} |
| 1189 | 1189 |
|
| 1190 | 1190 |
/// \ingroup graph_properties |
| 1191 | 1191 |
/// |
| 1192 | 1192 |
/// \brief Find the bi-edge-connected cut edges in an undirected graph. |
| 1193 | 1193 |
/// |
| 1194 | 1194 |
/// This function finds the bi-edge-connected cut edges in the given |
| 1195 |
/// undirected graph. |
|
| 1195 |
/// undirected graph. |
|
| 1196 | 1196 |
/// |
| 1197 | 1197 |
/// The bi-edge-connected components are the classes of an equivalence |
| 1198 | 1198 |
/// relation on the nodes of an undirected graph. Two nodes are in the |
| 1199 | 1199 |
/// same class if they are connected with at least two edge-disjoint |
| 1200 | 1200 |
/// paths. |
| 1201 | 1201 |
/// The bi-edge-connected components are separted by the cut edges of |
| 1202 | 1202 |
/// the components. |
| 1203 | 1203 |
/// |
| 1204 | 1204 |
/// \param graph The undirected graph. |
| 1205 | 1205 |
/// \retval cutMap A writable edge map. The values will be set to \c true |
| 1206 | 1206 |
/// for the cut edges (exactly once for each cut edge), and will not be |
| 1207 | 1207 |
/// changed for other edges. |
| ... | ... |
@@ -1340,25 +1340,25 @@ |
| 1340 | 1340 |
} |
| 1341 | 1341 |
|
| 1342 | 1342 |
/// \ingroup graph_properties |
| 1343 | 1343 |
/// |
| 1344 | 1344 |
/// \brief Sort the nodes of a DAG into topolgical order. |
| 1345 | 1345 |
/// |
| 1346 | 1346 |
/// This function sorts the nodes of the given acyclic digraph (DAG) |
| 1347 | 1347 |
/// into topolgical order and also checks whether the given digraph |
| 1348 | 1348 |
/// is DAG. |
| 1349 | 1349 |
/// |
| 1350 | 1350 |
/// \param digraph The digraph. |
| 1351 | 1351 |
/// \retval order A readable and writable node map. The values will be |
| 1352 |
/// set from 0 to the number of the nodes in the digraph minus one. |
|
| 1352 |
/// set from 0 to the number of the nodes in the digraph minus one. |
|
| 1353 | 1353 |
/// Each value of the map will be set exactly once, and the values will |
| 1354 | 1354 |
/// be set descending order. |
| 1355 | 1355 |
/// \return \c false if the digraph is not DAG. |
| 1356 | 1356 |
/// |
| 1357 | 1357 |
/// \see dag(), topologicalSort() |
| 1358 | 1358 |
template <typename Digraph, typename NodeMap> |
| 1359 | 1359 |
bool checkedTopologicalSort(const Digraph& digraph, NodeMap& order) {
|
| 1360 | 1360 |
using namespace _connectivity_bits; |
| 1361 | 1361 |
|
| 1362 | 1362 |
checkConcept<concepts::Digraph, Digraph>(); |
| 1363 | 1363 |
checkConcept<concepts::ReadWriteMap<typename Digraph::Node, int>, |
| 1364 | 1364 |
NodeMap>(); |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -1230,25 +1230,26 @@ |
| 1230 | 1230 |
typedef typename ItemSetTraits<GR, typename GR::Arc> |
| 1231 | 1231 |
::ItemNotifier::ObserverBase Parent; |
| 1232 | 1232 |
|
| 1233 | 1233 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 1234 | 1234 |
|
| 1235 | 1235 |
public: |
| 1236 | 1236 |
|
| 1237 | 1237 |
/// The Digraph type |
| 1238 | 1238 |
typedef GR Digraph; |
| 1239 | 1239 |
|
| 1240 | 1240 |
protected: |
| 1241 | 1241 |
|
| 1242 |
class AutoNodeMap : public ItemSetTraits<GR, Node>::template Map<Arc>::Type |
|
| 1242 |
class AutoNodeMap : public ItemSetTraits<GR, Node>::template Map<Arc>::Type |
|
| 1243 |
{
|
|
| 1243 | 1244 |
typedef typename ItemSetTraits<GR, Node>::template Map<Arc>::Type Parent; |
| 1244 | 1245 |
|
| 1245 | 1246 |
public: |
| 1246 | 1247 |
|
| 1247 | 1248 |
AutoNodeMap(const GR& digraph) : Parent(digraph, INVALID) {}
|
| 1248 | 1249 |
|
| 1249 | 1250 |
virtual void add(const Node& node) {
|
| 1250 | 1251 |
Parent::add(node); |
| 1251 | 1252 |
Parent::set(node, INVALID); |
| 1252 | 1253 |
} |
| 1253 | 1254 |
|
| 1254 | 1255 |
virtual void add(const std::vector<Node>& nodes) {
|
| ... | ... |
@@ -1269,25 +1270,25 @@ |
| 1269 | 1270 |
}; |
| 1270 | 1271 |
|
| 1271 | 1272 |
class ArcLess {
|
| 1272 | 1273 |
const Digraph &g; |
| 1273 | 1274 |
public: |
| 1274 | 1275 |
ArcLess(const Digraph &_g) : g(_g) {}
|
| 1275 | 1276 |
bool operator()(Arc a,Arc b) const |
| 1276 | 1277 |
{
|
| 1277 | 1278 |
return g.target(a)<g.target(b); |
| 1278 | 1279 |
} |
| 1279 | 1280 |
}; |
| 1280 | 1281 |
|
| 1281 |
protected: |
|
| 1282 |
protected: |
|
| 1282 | 1283 |
|
| 1283 | 1284 |
const Digraph &_g; |
| 1284 | 1285 |
AutoNodeMap _head; |
| 1285 | 1286 |
typename Digraph::template ArcMap<Arc> _parent; |
| 1286 | 1287 |
typename Digraph::template ArcMap<Arc> _left; |
| 1287 | 1288 |
typename Digraph::template ArcMap<Arc> _right; |
| 1288 | 1289 |
|
| 1289 | 1290 |
public: |
| 1290 | 1291 |
|
| 1291 | 1292 |
///Constructor |
| 1292 | 1293 |
|
| 1293 | 1294 |
///Constructor. |
| 1 |
/* -*- C++ -*- |
|
| 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
| 2 | 2 |
* |
| 3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
| 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -83,25 +83,25 @@ |
| 83 | 83 |
}; |
| 84 | 84 |
|
| 85 | 85 |
|
| 86 | 86 |
/// \addtogroup min_cost_flow_algs |
| 87 | 87 |
/// @{
|
| 88 | 88 |
|
| 89 | 89 |
/// \brief Implementation of the Cost Scaling algorithm for |
| 90 | 90 |
/// finding a \ref min_cost_flow "minimum cost flow". |
| 91 | 91 |
/// |
| 92 | 92 |
/// \ref CostScaling implements a cost scaling algorithm that performs |
| 93 | 93 |
/// push/augment and relabel operations for finding a \ref min_cost_flow |
| 94 | 94 |
/// "minimum cost flow" \ref amo93networkflows, \ref goldberg90approximation, |
| 95 |
/// \ref goldberg97efficient, \ref bunnagel98efficient. |
|
| 95 |
/// \ref goldberg97efficient, \ref bunnagel98efficient. |
|
| 96 | 96 |
/// It is a highly efficient primal-dual solution method, which |
| 97 | 97 |
/// can be viewed as the generalization of the \ref Preflow |
| 98 | 98 |
/// "preflow push-relabel" algorithm for the maximum flow problem. |
| 99 | 99 |
/// |
| 100 | 100 |
/// Most of the parameters of the problem (except for the digraph) |
| 101 | 101 |
/// can be given using separate functions, and the algorithm can be |
| 102 | 102 |
/// executed using the \ref run() function. If some parameters are not |
| 103 | 103 |
/// specified, then default values will be used. |
| 104 | 104 |
/// |
| 105 | 105 |
/// \tparam GR The digraph type the algorithm runs on. |
| 106 | 106 |
/// \tparam V The number type used for flow amounts, capacity bounds |
| 107 | 107 |
/// and supply values in the algorithm. By default, it is \c int. |
| ... | ... |
@@ -180,60 +180,60 @@ |
| 180 | 180 |
/// By default, the so called \ref PARTIAL_AUGMENT |
| 181 | 181 |
/// "Partial Augment-Relabel" method is used, which proved to be |
| 182 | 182 |
/// the most efficient and the most robust on various test inputs. |
| 183 | 183 |
/// However, the other methods can be selected using the \ref run() |
| 184 | 184 |
/// function with the proper parameter. |
| 185 | 185 |
enum Method {
|
| 186 | 186 |
/// Local push operations are used, i.e. flow is moved only on one |
| 187 | 187 |
/// admissible arc at once. |
| 188 | 188 |
PUSH, |
| 189 | 189 |
/// Augment operations are used, i.e. flow is moved on admissible |
| 190 | 190 |
/// paths from a node with excess to a node with deficit. |
| 191 | 191 |
AUGMENT, |
| 192 |
/// Partial augment operations are used, i.e. flow is moved on |
|
| 192 |
/// Partial augment operations are used, i.e. flow is moved on |
|
| 193 | 193 |
/// admissible paths started from a node with excess, but the |
| 194 | 194 |
/// lengths of these paths are limited. This method can be viewed |
| 195 | 195 |
/// as a combined version of the previous two operations. |
| 196 | 196 |
PARTIAL_AUGMENT |
| 197 | 197 |
}; |
| 198 | 198 |
|
| 199 | 199 |
private: |
| 200 | 200 |
|
| 201 | 201 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 202 | 202 |
|
| 203 | 203 |
typedef std::vector<int> IntVector; |
| 204 | 204 |
typedef std::vector<Value> ValueVector; |
| 205 | 205 |
typedef std::vector<Cost> CostVector; |
| 206 | 206 |
typedef std::vector<LargeCost> LargeCostVector; |
| 207 | 207 |
typedef std::vector<char> BoolVector; |
| 208 | 208 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
| 209 | 209 |
|
| 210 | 210 |
private: |
| 211 |
|
|
| 211 |
|
|
| 212 | 212 |
template <typename KT, typename VT> |
| 213 | 213 |
class StaticVectorMap {
|
| 214 | 214 |
public: |
| 215 | 215 |
typedef KT Key; |
| 216 | 216 |
typedef VT Value; |
| 217 |
|
|
| 217 |
|
|
| 218 | 218 |
StaticVectorMap(std::vector<Value>& v) : _v(v) {}
|
| 219 |
|
|
| 219 |
|
|
| 220 | 220 |
const Value& operator[](const Key& key) const {
|
| 221 | 221 |
return _v[StaticDigraph::id(key)]; |
| 222 | 222 |
} |
| 223 | 223 |
|
| 224 | 224 |
Value& operator[](const Key& key) {
|
| 225 | 225 |
return _v[StaticDigraph::id(key)]; |
| 226 | 226 |
} |
| 227 |
|
|
| 227 |
|
|
| 228 | 228 |
void set(const Key& key, const Value& val) {
|
| 229 | 229 |
_v[StaticDigraph::id(key)] = val; |
| 230 | 230 |
} |
| 231 | 231 |
|
| 232 | 232 |
private: |
| 233 | 233 |
std::vector<Value>& _v; |
| 234 | 234 |
}; |
| 235 | 235 |
|
| 236 | 236 |
typedef StaticVectorMap<StaticDigraph::Node, LargeCost> LargeCostNodeMap; |
| 237 | 237 |
typedef StaticVectorMap<StaticDigraph::Arc, LargeCost> LargeCostArcMap; |
| 238 | 238 |
|
| 239 | 239 |
private: |
| ... | ... |
@@ -274,35 +274,35 @@ |
| 274 | 274 |
IntVector _next_out; |
| 275 | 275 |
std::deque<int> _active_nodes; |
| 276 | 276 |
|
| 277 | 277 |
// Data for scaling |
| 278 | 278 |
LargeCost _epsilon; |
| 279 | 279 |
int _alpha; |
| 280 | 280 |
|
| 281 | 281 |
IntVector _buckets; |
| 282 | 282 |
IntVector _bucket_next; |
| 283 | 283 |
IntVector _bucket_prev; |
| 284 | 284 |
IntVector _rank; |
| 285 | 285 |
int _max_rank; |
| 286 |
|
|
| 286 |
|
|
| 287 | 287 |
// Data for a StaticDigraph structure |
| 288 | 288 |
typedef std::pair<int, int> IntPair; |
| 289 | 289 |
StaticDigraph _sgr; |
| 290 | 290 |
std::vector<IntPair> _arc_vec; |
| 291 | 291 |
std::vector<LargeCost> _cost_vec; |
| 292 | 292 |
LargeCostArcMap _cost_map; |
| 293 | 293 |
LargeCostNodeMap _pi_map; |
| 294 |
|
|
| 294 |
|
|
| 295 | 295 |
public: |
| 296 |
|
|
| 296 |
|
|
| 297 | 297 |
/// \brief Constant for infinite upper bounds (capacities). |
| 298 | 298 |
/// |
| 299 | 299 |
/// Constant for infinite upper bounds (capacities). |
| 300 | 300 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
| 301 | 301 |
/// \c std::numeric_limits<Value>::max() otherwise. |
| 302 | 302 |
const Value INF; |
| 303 | 303 |
|
| 304 | 304 |
public: |
| 305 | 305 |
|
| 306 | 306 |
/// \name Named Template Parameters |
| 307 | 307 |
/// @{
|
| 308 | 308 |
|
| ... | ... |
@@ -339,25 +339,25 @@ |
| 339 | 339 |
CostScaling(const GR& graph) : |
| 340 | 340 |
_graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph), |
| 341 | 341 |
_cost_map(_cost_vec), _pi_map(_pi), |
| 342 | 342 |
INF(std::numeric_limits<Value>::has_infinity ? |
| 343 | 343 |
std::numeric_limits<Value>::infinity() : |
| 344 | 344 |
std::numeric_limits<Value>::max()) |
| 345 | 345 |
{
|
| 346 | 346 |
// Check the number types |
| 347 | 347 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
| 348 | 348 |
"The flow type of CostScaling must be signed"); |
| 349 | 349 |
LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
| 350 | 350 |
"The cost type of CostScaling must be signed"); |
| 351 |
|
|
| 351 |
|
|
| 352 | 352 |
// Reset data structures |
| 353 | 353 |
reset(); |
| 354 | 354 |
} |
| 355 | 355 |
|
| 356 | 356 |
/// \name Parameters |
| 357 | 357 |
/// The parameters of the algorithm can be specified using these |
| 358 | 358 |
/// functions. |
| 359 | 359 |
|
| 360 | 360 |
/// @{
|
| 361 | 361 |
|
| 362 | 362 |
/// \brief Set the lower bounds on the arcs. |
| 363 | 363 |
/// |
| ... | ... |
@@ -455,25 +455,25 @@ |
| 455 | 455 |
/// \param k The required amount of flow from node \c s to node \c t |
| 456 | 456 |
/// (i.e. the supply of \c s and the demand of \c t). |
| 457 | 457 |
/// |
| 458 | 458 |
/// \return <tt>(*this)</tt> |
| 459 | 459 |
CostScaling& stSupply(const Node& s, const Node& t, Value k) {
|
| 460 | 460 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 461 | 461 |
_supply[i] = 0; |
| 462 | 462 |
} |
| 463 | 463 |
_supply[_node_id[s]] = k; |
| 464 | 464 |
_supply[_node_id[t]] = -k; |
| 465 | 465 |
return *this; |
| 466 | 466 |
} |
| 467 |
|
|
| 467 |
|
|
| 468 | 468 |
/// @} |
| 469 | 469 |
|
| 470 | 470 |
/// \name Execution control |
| 471 | 471 |
/// The algorithm can be executed using \ref run(). |
| 472 | 472 |
|
| 473 | 473 |
/// @{
|
| 474 | 474 |
|
| 475 | 475 |
/// \brief Run the algorithm. |
| 476 | 476 |
/// |
| 477 | 477 |
/// This function runs the algorithm. |
| 478 | 478 |
/// The paramters can be specified using functions \ref lowerMap(), |
| 479 | 479 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| ... | ... |
@@ -557,25 +557,25 @@ |
| 557 | 557 |
} |
| 558 | 558 |
int limit = _first_out[_root]; |
| 559 | 559 |
for (int j = 0; j != limit; ++j) {
|
| 560 | 560 |
_lower[j] = 0; |
| 561 | 561 |
_upper[j] = INF; |
| 562 | 562 |
_scost[j] = _forward[j] ? 1 : -1; |
| 563 | 563 |
} |
| 564 | 564 |
for (int j = limit; j != _res_arc_num; ++j) {
|
| 565 | 565 |
_lower[j] = 0; |
| 566 | 566 |
_upper[j] = INF; |
| 567 | 567 |
_scost[j] = 0; |
| 568 | 568 |
_scost[_reverse[j]] = 0; |
| 569 |
} |
|
| 569 |
} |
|
| 570 | 570 |
_have_lower = false; |
| 571 | 571 |
return *this; |
| 572 | 572 |
} |
| 573 | 573 |
|
| 574 | 574 |
/// \brief Reset all the parameters that have been given before. |
| 575 | 575 |
/// |
| 576 | 576 |
/// This function resets all the paramaters that have been given |
| 577 | 577 |
/// before using functions \ref lowerMap(), \ref upperMap(), |
| 578 | 578 |
/// \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 579 | 579 |
/// |
| 580 | 580 |
/// It is useful for multiple run() calls. If this function is not |
| 581 | 581 |
/// used, all the parameters given before are kept for the next |
| ... | ... |
@@ -592,25 +592,25 @@ |
| 592 | 592 |
_root = _node_num; |
| 593 | 593 |
|
| 594 | 594 |
_first_out.resize(_res_node_num + 1); |
| 595 | 595 |
_forward.resize(_res_arc_num); |
| 596 | 596 |
_source.resize(_res_arc_num); |
| 597 | 597 |
_target.resize(_res_arc_num); |
| 598 | 598 |
_reverse.resize(_res_arc_num); |
| 599 | 599 |
|
| 600 | 600 |
_lower.resize(_res_arc_num); |
| 601 | 601 |
_upper.resize(_res_arc_num); |
| 602 | 602 |
_scost.resize(_res_arc_num); |
| 603 | 603 |
_supply.resize(_res_node_num); |
| 604 |
|
|
| 604 |
|
|
| 605 | 605 |
_res_cap.resize(_res_arc_num); |
| 606 | 606 |
_cost.resize(_res_arc_num); |
| 607 | 607 |
_pi.resize(_res_node_num); |
| 608 | 608 |
_excess.resize(_res_node_num); |
| 609 | 609 |
_next_out.resize(_res_node_num); |
| 610 | 610 |
|
| 611 | 611 |
_arc_vec.reserve(_res_arc_num); |
| 612 | 612 |
_cost_vec.reserve(_res_arc_num); |
| 613 | 613 |
|
| 614 | 614 |
// Copy the graph |
| 615 | 615 |
int i = 0, j = 0, k = 2 * _arc_num + _node_num; |
| 616 | 616 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
| ... | ... |
@@ -640,25 +640,25 @@ |
| 640 | 640 |
_target[k] = i; |
| 641 | 641 |
_reverse[k] = j; |
| 642 | 642 |
++j; ++k; |
| 643 | 643 |
} |
| 644 | 644 |
_first_out[i] = j; |
| 645 | 645 |
_first_out[_res_node_num] = k; |
| 646 | 646 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 647 | 647 |
int fi = _arc_idf[a]; |
| 648 | 648 |
int bi = _arc_idb[a]; |
| 649 | 649 |
_reverse[fi] = bi; |
| 650 | 650 |
_reverse[bi] = fi; |
| 651 | 651 |
} |
| 652 |
|
|
| 652 |
|
|
| 653 | 653 |
// Reset parameters |
| 654 | 654 |
resetParams(); |
| 655 | 655 |
return *this; |
| 656 | 656 |
} |
| 657 | 657 |
|
| 658 | 658 |
/// @} |
| 659 | 659 |
|
| 660 | 660 |
/// \name Query Functions |
| 661 | 661 |
/// The results of the algorithm can be obtained using these |
| 662 | 662 |
/// functions.\n |
| 663 | 663 |
/// The \ref run() function must be called before using them. |
| 664 | 664 |
|
| ... | ... |
@@ -749,32 +749,32 @@ |
| 749 | 749 |
private: |
| 750 | 750 |
|
| 751 | 751 |
// Initialize the algorithm |
| 752 | 752 |
ProblemType init() {
|
| 753 | 753 |
if (_res_node_num <= 1) return INFEASIBLE; |
| 754 | 754 |
|
| 755 | 755 |
// Check the sum of supply values |
| 756 | 756 |
_sum_supply = 0; |
| 757 | 757 |
for (int i = 0; i != _root; ++i) {
|
| 758 | 758 |
_sum_supply += _supply[i]; |
| 759 | 759 |
} |
| 760 | 760 |
if (_sum_supply > 0) return INFEASIBLE; |
| 761 |
|
|
| 761 |
|
|
| 762 | 762 |
|
| 763 | 763 |
// Initialize vectors |
| 764 | 764 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 765 | 765 |
_pi[i] = 0; |
| 766 | 766 |
_excess[i] = _supply[i]; |
| 767 | 767 |
} |
| 768 |
|
|
| 768 |
|
|
| 769 | 769 |
// Remove infinite upper bounds and check negative arcs |
| 770 | 770 |
const Value MAX = std::numeric_limits<Value>::max(); |
| 771 | 771 |
int last_out; |
| 772 | 772 |
if (_have_lower) {
|
| 773 | 773 |
for (int i = 0; i != _root; ++i) {
|
| 774 | 774 |
last_out = _first_out[i+1]; |
| 775 | 775 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 776 | 776 |
if (_forward[j]) {
|
| 777 | 777 |
Value c = _scost[j] < 0 ? _upper[j] : _lower[j]; |
| 778 | 778 |
if (c >= MAX) return UNBOUNDED; |
| 779 | 779 |
_excess[i] -= c; |
| 780 | 780 |
_excess[_target[j]] += c; |
| ... | ... |
@@ -876,40 +876,40 @@ |
| 876 | 876 |
Value fa = flow[a]; |
| 877 | 877 |
_res_cap[_arc_idf[a]] = cap[a] - fa; |
| 878 | 878 |
_res_cap[_arc_idb[a]] = fa; |
| 879 | 879 |
} |
| 880 | 880 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
| 881 | 881 |
int ra = _reverse[a]; |
| 882 | 882 |
_res_cap[a] = 0; |
| 883 | 883 |
_res_cap[ra] = 0; |
| 884 | 884 |
_cost[a] = 0; |
| 885 | 885 |
_cost[ra] = 0; |
| 886 | 886 |
} |
| 887 | 887 |
} |
| 888 |
|
|
| 888 |
|
|
| 889 | 889 |
return OPTIMAL; |
| 890 | 890 |
} |
| 891 | 891 |
|
| 892 | 892 |
// Execute the algorithm and transform the results |
| 893 | 893 |
void start(Method method) {
|
| 894 | 894 |
// Maximum path length for partial augment |
| 895 | 895 |
const int MAX_PATH_LENGTH = 4; |
| 896 | 896 |
|
| 897 |
// Initialize data structures for buckets |
|
| 897 |
// Initialize data structures for buckets |
|
| 898 | 898 |
_max_rank = _alpha * _res_node_num; |
| 899 | 899 |
_buckets.resize(_max_rank); |
| 900 | 900 |
_bucket_next.resize(_res_node_num + 1); |
| 901 | 901 |
_bucket_prev.resize(_res_node_num + 1); |
| 902 | 902 |
_rank.resize(_res_node_num + 1); |
| 903 |
|
|
| 903 |
|
|
| 904 | 904 |
// Execute the algorithm |
| 905 | 905 |
switch (method) {
|
| 906 | 906 |
case PUSH: |
| 907 | 907 |
startPush(); |
| 908 | 908 |
break; |
| 909 | 909 |
case AUGMENT: |
| 910 | 910 |
startAugment(); |
| 911 | 911 |
break; |
| 912 | 912 |
case PARTIAL_AUGMENT: |
| 913 | 913 |
startAugment(MAX_PATH_LENGTH); |
| 914 | 914 |
break; |
| 915 | 915 |
} |
| ... | ... |
@@ -930,54 +930,54 @@ |
| 930 | 930 |
bf.distMap(_pi_map); |
| 931 | 931 |
bf.init(0); |
| 932 | 932 |
bf.start(); |
| 933 | 933 |
|
| 934 | 934 |
// Handle non-zero lower bounds |
| 935 | 935 |
if (_have_lower) {
|
| 936 | 936 |
int limit = _first_out[_root]; |
| 937 | 937 |
for (int j = 0; j != limit; ++j) {
|
| 938 | 938 |
if (!_forward[j]) _res_cap[j] += _lower[j]; |
| 939 | 939 |
} |
| 940 | 940 |
} |
| 941 | 941 |
} |
| 942 |
|
|
| 942 |
|
|
| 943 | 943 |
// Initialize a cost scaling phase |
| 944 | 944 |
void initPhase() {
|
| 945 | 945 |
// Saturate arcs not satisfying the optimality condition |
| 946 | 946 |
for (int u = 0; u != _res_node_num; ++u) {
|
| 947 | 947 |
int last_out = _first_out[u+1]; |
| 948 | 948 |
LargeCost pi_u = _pi[u]; |
| 949 | 949 |
for (int a = _first_out[u]; a != last_out; ++a) {
|
| 950 | 950 |
int v = _target[a]; |
| 951 | 951 |
if (_res_cap[a] > 0 && _cost[a] + pi_u - _pi[v] < 0) {
|
| 952 | 952 |
Value delta = _res_cap[a]; |
| 953 | 953 |
_excess[u] -= delta; |
| 954 | 954 |
_excess[v] += delta; |
| 955 | 955 |
_res_cap[a] = 0; |
| 956 | 956 |
_res_cap[_reverse[a]] += delta; |
| 957 | 957 |
} |
| 958 | 958 |
} |
| 959 | 959 |
} |
| 960 |
|
|
| 960 |
|
|
| 961 | 961 |
// Find active nodes (i.e. nodes with positive excess) |
| 962 | 962 |
for (int u = 0; u != _res_node_num; ++u) {
|
| 963 | 963 |
if (_excess[u] > 0) _active_nodes.push_back(u); |
| 964 | 964 |
} |
| 965 | 965 |
|
| 966 | 966 |
// Initialize the next arcs |
| 967 | 967 |
for (int u = 0; u != _res_node_num; ++u) {
|
| 968 | 968 |
_next_out[u] = _first_out[u]; |
| 969 | 969 |
} |
| 970 | 970 |
} |
| 971 |
|
|
| 971 |
|
|
| 972 | 972 |
// Early termination heuristic |
| 973 | 973 |
bool earlyTermination() {
|
| 974 | 974 |
const double EARLY_TERM_FACTOR = 3.0; |
| 975 | 975 |
|
| 976 | 976 |
// Build a static residual graph |
| 977 | 977 |
_arc_vec.clear(); |
| 978 | 978 |
_cost_vec.clear(); |
| 979 | 979 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 980 | 980 |
if (_res_cap[j] > 0) {
|
| 981 | 981 |
_arc_vec.push_back(IntPair(_source[j], _target[j])); |
| 982 | 982 |
_cost_vec.push_back(_cost[j] + 1); |
| 983 | 983 |
} |
| ... | ... |
@@ -989,25 +989,25 @@ |
| 989 | 989 |
bf.init(0); |
| 990 | 990 |
bool done = false; |
| 991 | 991 |
int K = int(EARLY_TERM_FACTOR * std::sqrt(double(_res_node_num))); |
| 992 | 992 |
for (int i = 0; i < K && !done; ++i) {
|
| 993 | 993 |
done = bf.processNextWeakRound(); |
| 994 | 994 |
} |
| 995 | 995 |
return done; |
| 996 | 996 |
} |
| 997 | 997 |
|
| 998 | 998 |
// Global potential update heuristic |
| 999 | 999 |
void globalUpdate() {
|
| 1000 | 1000 |
int bucket_end = _root + 1; |
| 1001 |
|
|
| 1001 |
|
|
| 1002 | 1002 |
// Initialize buckets |
| 1003 | 1003 |
for (int r = 0; r != _max_rank; ++r) {
|
| 1004 | 1004 |
_buckets[r] = bucket_end; |
| 1005 | 1005 |
} |
| 1006 | 1006 |
Value total_excess = 0; |
| 1007 | 1007 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 1008 | 1008 |
if (_excess[i] < 0) {
|
| 1009 | 1009 |
_rank[i] = 0; |
| 1010 | 1010 |
_bucket_next[i] = _buckets[0]; |
| 1011 | 1011 |
_bucket_prev[_buckets[0]] = i; |
| 1012 | 1012 |
_buckets[0] = i; |
| 1013 | 1013 |
} else {
|
| ... | ... |
@@ -1015,108 +1015,108 @@ |
| 1015 | 1015 |
_rank[i] = _max_rank; |
| 1016 | 1016 |
} |
| 1017 | 1017 |
} |
| 1018 | 1018 |
if (total_excess == 0) return; |
| 1019 | 1019 |
|
| 1020 | 1020 |
// Search the buckets |
| 1021 | 1021 |
int r = 0; |
| 1022 | 1022 |
for ( ; r != _max_rank; ++r) {
|
| 1023 | 1023 |
while (_buckets[r] != bucket_end) {
|
| 1024 | 1024 |
// Remove the first node from the current bucket |
| 1025 | 1025 |
int u = _buckets[r]; |
| 1026 | 1026 |
_buckets[r] = _bucket_next[u]; |
| 1027 |
|
|
| 1027 |
|
|
| 1028 | 1028 |
// Search the incomming arcs of u |
| 1029 | 1029 |
LargeCost pi_u = _pi[u]; |
| 1030 | 1030 |
int last_out = _first_out[u+1]; |
| 1031 | 1031 |
for (int a = _first_out[u]; a != last_out; ++a) {
|
| 1032 | 1032 |
int ra = _reverse[a]; |
| 1033 | 1033 |
if (_res_cap[ra] > 0) {
|
| 1034 | 1034 |
int v = _source[ra]; |
| 1035 | 1035 |
int old_rank_v = _rank[v]; |
| 1036 | 1036 |
if (r < old_rank_v) {
|
| 1037 | 1037 |
// Compute the new rank of v |
| 1038 | 1038 |
LargeCost nrc = (_cost[ra] + _pi[v] - pi_u) / _epsilon; |
| 1039 | 1039 |
int new_rank_v = old_rank_v; |
| 1040 | 1040 |
if (nrc < LargeCost(_max_rank)) |
| 1041 | 1041 |
new_rank_v = r + 1 + int(nrc); |
| 1042 |
|
|
| 1042 |
|
|
| 1043 | 1043 |
// Change the rank of v |
| 1044 | 1044 |
if (new_rank_v < old_rank_v) {
|
| 1045 | 1045 |
_rank[v] = new_rank_v; |
| 1046 | 1046 |
_next_out[v] = _first_out[v]; |
| 1047 |
|
|
| 1047 |
|
|
| 1048 | 1048 |
// Remove v from its old bucket |
| 1049 | 1049 |
if (old_rank_v < _max_rank) {
|
| 1050 | 1050 |
if (_buckets[old_rank_v] == v) {
|
| 1051 | 1051 |
_buckets[old_rank_v] = _bucket_next[v]; |
| 1052 | 1052 |
} else {
|
| 1053 | 1053 |
_bucket_next[_bucket_prev[v]] = _bucket_next[v]; |
| 1054 | 1054 |
_bucket_prev[_bucket_next[v]] = _bucket_prev[v]; |
| 1055 | 1055 |
} |
| 1056 | 1056 |
} |
| 1057 |
|
|
| 1057 |
|
|
| 1058 | 1058 |
// Insert v to its new bucket |
| 1059 | 1059 |
_bucket_next[v] = _buckets[new_rank_v]; |
| 1060 | 1060 |
_bucket_prev[_buckets[new_rank_v]] = v; |
| 1061 | 1061 |
_buckets[new_rank_v] = v; |
| 1062 | 1062 |
} |
| 1063 | 1063 |
} |
| 1064 | 1064 |
} |
| 1065 | 1065 |
} |
| 1066 | 1066 |
|
| 1067 | 1067 |
// Finish search if there are no more active nodes |
| 1068 | 1068 |
if (_excess[u] > 0) {
|
| 1069 | 1069 |
total_excess -= _excess[u]; |
| 1070 | 1070 |
if (total_excess <= 0) break; |
| 1071 | 1071 |
} |
| 1072 | 1072 |
} |
| 1073 | 1073 |
if (total_excess <= 0) break; |
| 1074 | 1074 |
} |
| 1075 |
|
|
| 1075 |
|
|
| 1076 | 1076 |
// Relabel nodes |
| 1077 | 1077 |
for (int u = 0; u != _res_node_num; ++u) {
|
| 1078 | 1078 |
int k = std::min(_rank[u], r); |
| 1079 | 1079 |
if (k > 0) {
|
| 1080 | 1080 |
_pi[u] -= _epsilon * k; |
| 1081 | 1081 |
_next_out[u] = _first_out[u]; |
| 1082 | 1082 |
} |
| 1083 | 1083 |
} |
| 1084 | 1084 |
} |
| 1085 | 1085 |
|
| 1086 | 1086 |
/// Execute the algorithm performing augment and relabel operations |
| 1087 | 1087 |
void startAugment(int max_length = std::numeric_limits<int>::max()) {
|
| 1088 | 1088 |
// Paramters for heuristics |
| 1089 | 1089 |
const int EARLY_TERM_EPSILON_LIMIT = 1000; |
| 1090 | 1090 |
const double GLOBAL_UPDATE_FACTOR = 3.0; |
| 1091 | 1091 |
|
| 1092 | 1092 |
const int global_update_freq = int(GLOBAL_UPDATE_FACTOR * |
| 1093 | 1093 |
(_res_node_num + _sup_node_num * _sup_node_num)); |
| 1094 | 1094 |
int next_update_limit = global_update_freq; |
| 1095 |
|
|
| 1095 |
|
|
| 1096 | 1096 |
int relabel_cnt = 0; |
| 1097 |
|
|
| 1097 |
|
|
| 1098 | 1098 |
// Perform cost scaling phases |
| 1099 | 1099 |
std::vector<int> path; |
| 1100 | 1100 |
for ( ; _epsilon >= 1; _epsilon = _epsilon < _alpha && _epsilon > 1 ? |
| 1101 | 1101 |
1 : _epsilon / _alpha ) |
| 1102 | 1102 |
{
|
| 1103 | 1103 |
// Early termination heuristic |
| 1104 | 1104 |
if (_epsilon <= EARLY_TERM_EPSILON_LIMIT) {
|
| 1105 | 1105 |
if (earlyTermination()) break; |
| 1106 | 1106 |
} |
| 1107 |
|
|
| 1107 |
|
|
| 1108 | 1108 |
// Initialize current phase |
| 1109 | 1109 |
initPhase(); |
| 1110 |
|
|
| 1110 |
|
|
| 1111 | 1111 |
// Perform partial augment and relabel operations |
| 1112 | 1112 |
while (true) {
|
| 1113 | 1113 |
// Select an active node (FIFO selection) |
| 1114 | 1114 |
while (_active_nodes.size() > 0 && |
| 1115 | 1115 |
_excess[_active_nodes.front()] <= 0) {
|
| 1116 | 1116 |
_active_nodes.pop_front(); |
| 1117 | 1117 |
} |
| 1118 | 1118 |
if (_active_nodes.size() == 0) break; |
| 1119 | 1119 |
int start = _active_nodes.front(); |
| 1120 | 1120 |
|
| 1121 | 1121 |
// Find an augmenting path from the start node |
| 1122 | 1122 |
path.clear(); |
| ... | ... |
@@ -1187,65 +1187,65 @@ |
| 1187 | 1187 |
|
| 1188 | 1188 |
/// Execute the algorithm performing push and relabel operations |
| 1189 | 1189 |
void startPush() {
|
| 1190 | 1190 |
// Paramters for heuristics |
| 1191 | 1191 |
const int EARLY_TERM_EPSILON_LIMIT = 1000; |
| 1192 | 1192 |
const double GLOBAL_UPDATE_FACTOR = 2.0; |
| 1193 | 1193 |
|
| 1194 | 1194 |
const int global_update_freq = int(GLOBAL_UPDATE_FACTOR * |
| 1195 | 1195 |
(_res_node_num + _sup_node_num * _sup_node_num)); |
| 1196 | 1196 |
int next_update_limit = global_update_freq; |
| 1197 | 1197 |
|
| 1198 | 1198 |
int relabel_cnt = 0; |
| 1199 |
|
|
| 1199 |
|
|
| 1200 | 1200 |
// Perform cost scaling phases |
| 1201 | 1201 |
BoolVector hyper(_res_node_num, false); |
| 1202 | 1202 |
LargeCostVector hyper_cost(_res_node_num); |
| 1203 | 1203 |
for ( ; _epsilon >= 1; _epsilon = _epsilon < _alpha && _epsilon > 1 ? |
| 1204 | 1204 |
1 : _epsilon / _alpha ) |
| 1205 | 1205 |
{
|
| 1206 | 1206 |
// Early termination heuristic |
| 1207 | 1207 |
if (_epsilon <= EARLY_TERM_EPSILON_LIMIT) {
|
| 1208 | 1208 |
if (earlyTermination()) break; |
| 1209 | 1209 |
} |
| 1210 |
|
|
| 1210 |
|
|
| 1211 | 1211 |
// Initialize current phase |
| 1212 | 1212 |
initPhase(); |
| 1213 | 1213 |
|
| 1214 | 1214 |
// Perform push and relabel operations |
| 1215 | 1215 |
while (_active_nodes.size() > 0) {
|
| 1216 | 1216 |
LargeCost min_red_cost, rc, pi_n; |
| 1217 | 1217 |
Value delta; |
| 1218 | 1218 |
int n, t, a, last_out = _res_arc_num; |
| 1219 | 1219 |
|
| 1220 | 1220 |
next_node: |
| 1221 | 1221 |
// Select an active node (FIFO selection) |
| 1222 | 1222 |
n = _active_nodes.front(); |
| 1223 | 1223 |
last_out = _first_out[n+1]; |
| 1224 | 1224 |
pi_n = _pi[n]; |
| 1225 |
|
|
| 1225 |
|
|
| 1226 | 1226 |
// Perform push operations if there are admissible arcs |
| 1227 | 1227 |
if (_excess[n] > 0) {
|
| 1228 | 1228 |
for (a = _next_out[n]; a != last_out; ++a) {
|
| 1229 | 1229 |
if (_res_cap[a] > 0 && |
| 1230 | 1230 |
_cost[a] + pi_n - _pi[_target[a]] < 0) {
|
| 1231 | 1231 |
delta = std::min(_res_cap[a], _excess[n]); |
| 1232 | 1232 |
t = _target[a]; |
| 1233 | 1233 |
|
| 1234 | 1234 |
// Push-look-ahead heuristic |
| 1235 | 1235 |
Value ahead = -_excess[t]; |
| 1236 | 1236 |
int last_out_t = _first_out[t+1]; |
| 1237 | 1237 |
LargeCost pi_t = _pi[t]; |
| 1238 | 1238 |
for (int ta = _next_out[t]; ta != last_out_t; ++ta) {
|
| 1239 |
if (_res_cap[ta] > 0 && |
|
| 1239 |
if (_res_cap[ta] > 0 && |
|
| 1240 | 1240 |
_cost[ta] + pi_t - _pi[_target[ta]] < 0) |
| 1241 | 1241 |
ahead += _res_cap[ta]; |
| 1242 | 1242 |
if (ahead >= delta) break; |
| 1243 | 1243 |
} |
| 1244 | 1244 |
if (ahead < 0) ahead = 0; |
| 1245 | 1245 |
|
| 1246 | 1246 |
// Push flow along the arc |
| 1247 | 1247 |
if (ahead < delta && !hyper[t]) {
|
| 1248 | 1248 |
_res_cap[a] -= ahead; |
| 1249 | 1249 |
_res_cap[_reverse[a]] += ahead; |
| 1250 | 1250 |
_excess[n] -= ahead; |
| 1251 | 1251 |
_excess[t] += ahead; |
| ... | ... |
@@ -1278,33 +1278,33 @@ |
| 1278 | 1278 |
std::numeric_limits<LargeCost>::max(); |
| 1279 | 1279 |
for (int a = _first_out[n]; a != last_out; ++a) {
|
| 1280 | 1280 |
rc = _cost[a] + pi_n - _pi[_target[a]]; |
| 1281 | 1281 |
if (_res_cap[a] > 0 && rc < min_red_cost) {
|
| 1282 | 1282 |
min_red_cost = rc; |
| 1283 | 1283 |
} |
| 1284 | 1284 |
} |
| 1285 | 1285 |
_pi[n] -= min_red_cost + _epsilon; |
| 1286 | 1286 |
_next_out[n] = _first_out[n]; |
| 1287 | 1287 |
hyper[n] = false; |
| 1288 | 1288 |
++relabel_cnt; |
| 1289 | 1289 |
} |
| 1290 |
|
|
| 1290 |
|
|
| 1291 | 1291 |
// Remove nodes that are not active nor hyper |
| 1292 | 1292 |
remove_nodes: |
| 1293 | 1293 |
while ( _active_nodes.size() > 0 && |
| 1294 | 1294 |
_excess[_active_nodes.front()] <= 0 && |
| 1295 | 1295 |
!hyper[_active_nodes.front()] ) {
|
| 1296 | 1296 |
_active_nodes.pop_front(); |
| 1297 | 1297 |
} |
| 1298 |
|
|
| 1298 |
|
|
| 1299 | 1299 |
// Global update heuristic |
| 1300 | 1300 |
if (relabel_cnt >= next_update_limit) {
|
| 1301 | 1301 |
globalUpdate(); |
| 1302 | 1302 |
for (int u = 0; u != _res_node_num; ++u) |
| 1303 | 1303 |
hyper[u] = false; |
| 1304 | 1304 |
next_update_limit += global_update_freq; |
| 1305 | 1305 |
} |
| 1306 | 1306 |
} |
| 1307 | 1307 |
} |
| 1308 | 1308 |
} |
| 1309 | 1309 |
|
| 1310 | 1310 |
}; //class CostScaling |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -102,25 +102,25 @@ |
| 102 | 102 |
return i; |
| 103 | 103 |
} |
| 104 | 104 |
|
| 105 | 105 |
|
| 106 | 106 |
int CplexBase::_addRow() {
|
| 107 | 107 |
int i = CPXgetnumrows(cplexEnv(), _prob); |
| 108 | 108 |
const double ub = INF; |
| 109 | 109 |
const char s = 'L'; |
| 110 | 110 |
CPXnewrows(cplexEnv(), _prob, 1, &ub, &s, 0, 0); |
| 111 | 111 |
return i; |
| 112 | 112 |
} |
| 113 | 113 |
|
| 114 |
int CplexBase::_addRow(Value lb, ExprIterator b, |
|
| 114 |
int CplexBase::_addRow(Value lb, ExprIterator b, |
|
| 115 | 115 |
ExprIterator e, Value ub) {
|
| 116 | 116 |
int i = CPXgetnumrows(cplexEnv(), _prob); |
| 117 | 117 |
if (lb == -INF) {
|
| 118 | 118 |
const char s = 'L'; |
| 119 | 119 |
CPXnewrows(cplexEnv(), _prob, 1, &ub, &s, 0, 0); |
| 120 | 120 |
} else if (ub == INF) {
|
| 121 | 121 |
const char s = 'G'; |
| 122 | 122 |
CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, 0, 0); |
| 123 | 123 |
} else if (lb == ub){
|
| 124 | 124 |
const char s = 'E'; |
| 125 | 125 |
CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, 0, 0); |
| 126 | 126 |
} else {
|
| ... | ... |
@@ -480,25 +480,25 @@ |
| 480 | 480 |
_message_enabled = false; |
| 481 | 481 |
break; |
| 482 | 482 |
case MESSAGE_ERROR: |
| 483 | 483 |
case MESSAGE_WARNING: |
| 484 | 484 |
case MESSAGE_NORMAL: |
| 485 | 485 |
case MESSAGE_VERBOSE: |
| 486 | 486 |
_message_enabled = true; |
| 487 | 487 |
break; |
| 488 | 488 |
} |
| 489 | 489 |
} |
| 490 | 490 |
|
| 491 | 491 |
void CplexBase::_applyMessageLevel() {
|
| 492 |
CPXsetintparam(cplexEnv(), CPX_PARAM_SCRIND, |
|
| 492 |
CPXsetintparam(cplexEnv(), CPX_PARAM_SCRIND, |
|
| 493 | 493 |
_message_enabled ? CPX_ON : CPX_OFF); |
| 494 | 494 |
} |
| 495 | 495 |
|
| 496 | 496 |
// CplexLp members |
| 497 | 497 |
|
| 498 | 498 |
CplexLp::CplexLp() |
| 499 | 499 |
: LpBase(), LpSolver(), CplexBase() {}
|
| 500 | 500 |
|
| 501 | 501 |
CplexLp::CplexLp(const CplexEnv& env) |
| 502 | 502 |
: LpBase(), LpSolver(), CplexBase(env) {}
|
| 503 | 503 |
|
| 504 | 504 |
CplexLp::CplexLp(const CplexLp& other) |
| 1 |
/* -*- C++ -*- |
|
| 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
| 2 | 2 |
* |
| 3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
| 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -133,50 +133,50 @@ |
| 133 | 133 |
MINIMUM_MEAN_CYCLE_CANCELING, |
| 134 | 134 |
/// The "Cancel And Tighten" algorithm, which can be viewed as an |
| 135 | 135 |
/// improved version of the previous method |
| 136 | 136 |
/// \ref goldberg89cyclecanceling. |
| 137 | 137 |
/// It is faster both in theory and in practice, its running time |
| 138 | 138 |
/// complexity is O(n<sup>2</sup>m<sup>2</sup>log(n)). |
| 139 | 139 |
CANCEL_AND_TIGHTEN |
| 140 | 140 |
}; |
| 141 | 141 |
|
| 142 | 142 |
private: |
| 143 | 143 |
|
| 144 | 144 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 145 |
|
|
| 145 |
|
|
| 146 | 146 |
typedef std::vector<int> IntVector; |
| 147 | 147 |
typedef std::vector<double> DoubleVector; |
| 148 | 148 |
typedef std::vector<Value> ValueVector; |
| 149 | 149 |
typedef std::vector<Cost> CostVector; |
| 150 | 150 |
typedef std::vector<char> BoolVector; |
| 151 | 151 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
| 152 | 152 |
|
| 153 | 153 |
private: |
| 154 |
|
|
| 154 |
|
|
| 155 | 155 |
template <typename KT, typename VT> |
| 156 | 156 |
class StaticVectorMap {
|
| 157 | 157 |
public: |
| 158 | 158 |
typedef KT Key; |
| 159 | 159 |
typedef VT Value; |
| 160 |
|
|
| 160 |
|
|
| 161 | 161 |
StaticVectorMap(std::vector<Value>& v) : _v(v) {}
|
| 162 |
|
|
| 162 |
|
|
| 163 | 163 |
const Value& operator[](const Key& key) const {
|
| 164 | 164 |
return _v[StaticDigraph::id(key)]; |
| 165 | 165 |
} |
| 166 | 166 |
|
| 167 | 167 |
Value& operator[](const Key& key) {
|
| 168 | 168 |
return _v[StaticDigraph::id(key)]; |
| 169 | 169 |
} |
| 170 |
|
|
| 170 |
|
|
| 171 | 171 |
void set(const Key& key, const Value& val) {
|
| 172 | 172 |
_v[StaticDigraph::id(key)] = val; |
| 173 | 173 |
} |
| 174 | 174 |
|
| 175 | 175 |
private: |
| 176 | 176 |
std::vector<Value>& _v; |
| 177 | 177 |
}; |
| 178 | 178 |
|
| 179 | 179 |
typedef StaticVectorMap<StaticDigraph::Node, Cost> CostNodeMap; |
| 180 | 180 |
typedef StaticVectorMap<StaticDigraph::Arc, Cost> CostArcMap; |
| 181 | 181 |
|
| 182 | 182 |
private: |
| ... | ... |
@@ -212,27 +212,27 @@ |
| 212 | 212 |
|
| 213 | 213 |
ValueVector _res_cap; |
| 214 | 214 |
CostVector _pi; |
| 215 | 215 |
|
| 216 | 216 |
// Data for a StaticDigraph structure |
| 217 | 217 |
typedef std::pair<int, int> IntPair; |
| 218 | 218 |
StaticDigraph _sgr; |
| 219 | 219 |
std::vector<IntPair> _arc_vec; |
| 220 | 220 |
std::vector<Cost> _cost_vec; |
| 221 | 221 |
IntVector _id_vec; |
| 222 | 222 |
CostArcMap _cost_map; |
| 223 | 223 |
CostNodeMap _pi_map; |
| 224 |
|
|
| 224 |
|
|
| 225 | 225 |
public: |
| 226 |
|
|
| 226 |
|
|
| 227 | 227 |
/// \brief Constant for infinite upper bounds (capacities). |
| 228 | 228 |
/// |
| 229 | 229 |
/// Constant for infinite upper bounds (capacities). |
| 230 | 230 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
| 231 | 231 |
/// \c std::numeric_limits<Value>::max() otherwise. |
| 232 | 232 |
const Value INF; |
| 233 | 233 |
|
| 234 | 234 |
public: |
| 235 | 235 |
|
| 236 | 236 |
/// \brief Constructor. |
| 237 | 237 |
/// |
| 238 | 238 |
/// The constructor of the class. |
| ... | ... |
@@ -357,25 +357,25 @@ |
| 357 | 357 |
/// \param k The required amount of flow from node \c s to node \c t |
| 358 | 358 |
/// (i.e. the supply of \c s and the demand of \c t). |
| 359 | 359 |
/// |
| 360 | 360 |
/// \return <tt>(*this)</tt> |
| 361 | 361 |
CycleCanceling& stSupply(const Node& s, const Node& t, Value k) {
|
| 362 | 362 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 363 | 363 |
_supply[i] = 0; |
| 364 | 364 |
} |
| 365 | 365 |
_supply[_node_id[s]] = k; |
| 366 | 366 |
_supply[_node_id[t]] = -k; |
| 367 | 367 |
return *this; |
| 368 | 368 |
} |
| 369 |
|
|
| 369 |
|
|
| 370 | 370 |
/// @} |
| 371 | 371 |
|
| 372 | 372 |
/// \name Execution control |
| 373 | 373 |
/// The algorithm can be executed using \ref run(). |
| 374 | 374 |
|
| 375 | 375 |
/// @{
|
| 376 | 376 |
|
| 377 | 377 |
/// \brief Run the algorithm. |
| 378 | 378 |
/// |
| 379 | 379 |
/// This function runs the algorithm. |
| 380 | 380 |
/// The paramters can be specified using functions \ref lowerMap(), |
| 381 | 381 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| ... | ... |
@@ -457,25 +457,25 @@ |
| 457 | 457 |
} |
| 458 | 458 |
int limit = _first_out[_root]; |
| 459 | 459 |
for (int j = 0; j != limit; ++j) {
|
| 460 | 460 |
_lower[j] = 0; |
| 461 | 461 |
_upper[j] = INF; |
| 462 | 462 |
_cost[j] = _forward[j] ? 1 : -1; |
| 463 | 463 |
} |
| 464 | 464 |
for (int j = limit; j != _res_arc_num; ++j) {
|
| 465 | 465 |
_lower[j] = 0; |
| 466 | 466 |
_upper[j] = INF; |
| 467 | 467 |
_cost[j] = 0; |
| 468 | 468 |
_cost[_reverse[j]] = 0; |
| 469 |
} |
|
| 469 |
} |
|
| 470 | 470 |
_have_lower = false; |
| 471 | 471 |
return *this; |
| 472 | 472 |
} |
| 473 | 473 |
|
| 474 | 474 |
/// \brief Reset the internal data structures and all the parameters |
| 475 | 475 |
/// that have been given before. |
| 476 | 476 |
/// |
| 477 | 477 |
/// This function resets the internal data structures and all the |
| 478 | 478 |
/// paramaters that have been given before using functions \ref lowerMap(), |
| 479 | 479 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(). |
| 480 | 480 |
/// |
| 481 | 481 |
/// It is useful for multiple \ref run() calls. Basically, all the given |
| ... | ... |
@@ -499,25 +499,25 @@ |
| 499 | 499 |
_root = _node_num; |
| 500 | 500 |
|
| 501 | 501 |
_first_out.resize(_res_node_num + 1); |
| 502 | 502 |
_forward.resize(_res_arc_num); |
| 503 | 503 |
_source.resize(_res_arc_num); |
| 504 | 504 |
_target.resize(_res_arc_num); |
| 505 | 505 |
_reverse.resize(_res_arc_num); |
| 506 | 506 |
|
| 507 | 507 |
_lower.resize(_res_arc_num); |
| 508 | 508 |
_upper.resize(_res_arc_num); |
| 509 | 509 |
_cost.resize(_res_arc_num); |
| 510 | 510 |
_supply.resize(_res_node_num); |
| 511 |
|
|
| 511 |
|
|
| 512 | 512 |
_res_cap.resize(_res_arc_num); |
| 513 | 513 |
_pi.resize(_res_node_num); |
| 514 | 514 |
|
| 515 | 515 |
_arc_vec.reserve(_res_arc_num); |
| 516 | 516 |
_cost_vec.reserve(_res_arc_num); |
| 517 | 517 |
_id_vec.reserve(_res_arc_num); |
| 518 | 518 |
|
| 519 | 519 |
// Copy the graph |
| 520 | 520 |
int i = 0, j = 0, k = 2 * _arc_num + _node_num; |
| 521 | 521 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
| 522 | 522 |
_node_id[n] = i; |
| 523 | 523 |
} |
| ... | ... |
@@ -545,25 +545,25 @@ |
| 545 | 545 |
_target[k] = i; |
| 546 | 546 |
_reverse[k] = j; |
| 547 | 547 |
++j; ++k; |
| 548 | 548 |
} |
| 549 | 549 |
_first_out[i] = j; |
| 550 | 550 |
_first_out[_res_node_num] = k; |
| 551 | 551 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 552 | 552 |
int fi = _arc_idf[a]; |
| 553 | 553 |
int bi = _arc_idb[a]; |
| 554 | 554 |
_reverse[fi] = bi; |
| 555 | 555 |
_reverse[bi] = fi; |
| 556 | 556 |
} |
| 557 |
|
|
| 557 |
|
|
| 558 | 558 |
// Reset parameters |
| 559 | 559 |
resetParams(); |
| 560 | 560 |
return *this; |
| 561 | 561 |
} |
| 562 | 562 |
|
| 563 | 563 |
/// @} |
| 564 | 564 |
|
| 565 | 565 |
/// \name Query Functions |
| 566 | 566 |
/// The results of the algorithm can be obtained using these |
| 567 | 567 |
/// functions.\n |
| 568 | 568 |
/// The \ref run() function must be called before using them. |
| 569 | 569 |
|
| ... | ... |
@@ -654,32 +654,32 @@ |
| 654 | 654 |
private: |
| 655 | 655 |
|
| 656 | 656 |
// Initialize the algorithm |
| 657 | 657 |
ProblemType init() {
|
| 658 | 658 |
if (_res_node_num <= 1) return INFEASIBLE; |
| 659 | 659 |
|
| 660 | 660 |
// Check the sum of supply values |
| 661 | 661 |
_sum_supply = 0; |
| 662 | 662 |
for (int i = 0; i != _root; ++i) {
|
| 663 | 663 |
_sum_supply += _supply[i]; |
| 664 | 664 |
} |
| 665 | 665 |
if (_sum_supply > 0) return INFEASIBLE; |
| 666 |
|
|
| 666 |
|
|
| 667 | 667 |
|
| 668 | 668 |
// Initialize vectors |
| 669 | 669 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 670 | 670 |
_pi[i] = 0; |
| 671 | 671 |
} |
| 672 | 672 |
ValueVector excess(_supply); |
| 673 |
|
|
| 673 |
|
|
| 674 | 674 |
// Remove infinite upper bounds and check negative arcs |
| 675 | 675 |
const Value MAX = std::numeric_limits<Value>::max(); |
| 676 | 676 |
int last_out; |
| 677 | 677 |
if (_have_lower) {
|
| 678 | 678 |
for (int i = 0; i != _root; ++i) {
|
| 679 | 679 |
last_out = _first_out[i+1]; |
| 680 | 680 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 681 | 681 |
if (_forward[j]) {
|
| 682 | 682 |
Value c = _cost[j] < 0 ? _upper[j] : _lower[j]; |
| 683 | 683 |
if (c >= MAX) return UNBOUNDED; |
| 684 | 684 |
excess[i] -= c; |
| 685 | 685 |
excess[_target[j]] += c; |
| ... | ... |
@@ -761,28 +761,28 @@ |
| 761 | 761 |
Value fa = flow[a]; |
| 762 | 762 |
_res_cap[_arc_idf[a]] = cap[a] - fa; |
| 763 | 763 |
_res_cap[_arc_idb[a]] = fa; |
| 764 | 764 |
} |
| 765 | 765 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
| 766 | 766 |
int ra = _reverse[a]; |
| 767 | 767 |
_res_cap[a] = 1; |
| 768 | 768 |
_res_cap[ra] = 0; |
| 769 | 769 |
_cost[a] = 0; |
| 770 | 770 |
_cost[ra] = 0; |
| 771 | 771 |
} |
| 772 | 772 |
} |
| 773 |
|
|
| 773 |
|
|
| 774 | 774 |
return OPTIMAL; |
| 775 | 775 |
} |
| 776 |
|
|
| 776 |
|
|
| 777 | 777 |
// Build a StaticDigraph structure containing the current |
| 778 | 778 |
// residual network |
| 779 | 779 |
void buildResidualNetwork() {
|
| 780 | 780 |
_arc_vec.clear(); |
| 781 | 781 |
_cost_vec.clear(); |
| 782 | 782 |
_id_vec.clear(); |
| 783 | 783 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 784 | 784 |
if (_res_cap[j] > 0) {
|
| 785 | 785 |
_arc_vec.push_back(IntPair(_source[j], _target[j])); |
| 786 | 786 |
_cost_vec.push_back(_cost[j]); |
| 787 | 787 |
_id_vec.push_back(j); |
| 788 | 788 |
} |
| ... | ... |
@@ -820,32 +820,32 @@ |
| 820 | 820 |
int limit = _first_out[_root]; |
| 821 | 821 |
for (int j = 0; j != limit; ++j) {
|
| 822 | 822 |
if (!_forward[j]) _res_cap[j] += _lower[j]; |
| 823 | 823 |
} |
| 824 | 824 |
} |
| 825 | 825 |
} |
| 826 | 826 |
|
| 827 | 827 |
// Execute the "Simple Cycle Canceling" method |
| 828 | 828 |
void startSimpleCycleCanceling() {
|
| 829 | 829 |
// Constants for computing the iteration limits |
| 830 | 830 |
const int BF_FIRST_LIMIT = 2; |
| 831 | 831 |
const double BF_LIMIT_FACTOR = 1.5; |
| 832 |
|
|
| 832 |
|
|
| 833 | 833 |
typedef StaticVectorMap<StaticDigraph::Arc, Value> FilterMap; |
| 834 | 834 |
typedef FilterArcs<StaticDigraph, FilterMap> ResDigraph; |
| 835 | 835 |
typedef StaticVectorMap<StaticDigraph::Node, StaticDigraph::Arc> PredMap; |
| 836 | 836 |
typedef typename BellmanFord<ResDigraph, CostArcMap> |
| 837 | 837 |
::template SetDistMap<CostNodeMap> |
| 838 | 838 |
::template SetPredMap<PredMap>::Create BF; |
| 839 |
|
|
| 839 |
|
|
| 840 | 840 |
// Build the residual network |
| 841 | 841 |
_arc_vec.clear(); |
| 842 | 842 |
_cost_vec.clear(); |
| 843 | 843 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 844 | 844 |
_arc_vec.push_back(IntPair(_source[j], _target[j])); |
| 845 | 845 |
_cost_vec.push_back(_cost[j]); |
| 846 | 846 |
} |
| 847 | 847 |
_sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); |
| 848 | 848 |
|
| 849 | 849 |
FilterMap filter_map(_res_cap); |
| 850 | 850 |
ResDigraph rgr(_sgr, filter_map); |
| 851 | 851 |
std::vector<int> cycle; |
| ... | ... |
@@ -917,48 +917,48 @@ |
| 917 | 917 |
length_bound = static_cast<int>(length_bound * BF_LIMIT_FACTOR); |
| 918 | 918 |
} |
| 919 | 919 |
} |
| 920 | 920 |
} |
| 921 | 921 |
} |
| 922 | 922 |
|
| 923 | 923 |
// Execute the "Minimum Mean Cycle Canceling" method |
| 924 | 924 |
void startMinMeanCycleCanceling() {
|
| 925 | 925 |
typedef SimplePath<StaticDigraph> SPath; |
| 926 | 926 |
typedef typename SPath::ArcIt SPathArcIt; |
| 927 | 927 |
typedef typename HowardMmc<StaticDigraph, CostArcMap> |
| 928 | 928 |
::template SetPath<SPath>::Create MMC; |
| 929 |
|
|
| 929 |
|
|
| 930 | 930 |
SPath cycle; |
| 931 | 931 |
MMC mmc(_sgr, _cost_map); |
| 932 | 932 |
mmc.cycle(cycle); |
| 933 | 933 |
buildResidualNetwork(); |
| 934 | 934 |
while (mmc.findCycleMean() && mmc.cycleCost() < 0) {
|
| 935 | 935 |
// Find the cycle |
| 936 | 936 |
mmc.findCycle(); |
| 937 | 937 |
|
| 938 | 938 |
// Compute delta value |
| 939 | 939 |
Value delta = INF; |
| 940 | 940 |
for (SPathArcIt a(cycle); a != INVALID; ++a) {
|
| 941 | 941 |
Value d = _res_cap[_id_vec[_sgr.id(a)]]; |
| 942 | 942 |
if (d < delta) delta = d; |
| 943 | 943 |
} |
| 944 | 944 |
|
| 945 | 945 |
// Augment along the cycle |
| 946 | 946 |
for (SPathArcIt a(cycle); a != INVALID; ++a) {
|
| 947 | 947 |
int j = _id_vec[_sgr.id(a)]; |
| 948 | 948 |
_res_cap[j] -= delta; |
| 949 | 949 |
_res_cap[_reverse[j]] += delta; |
| 950 | 950 |
} |
| 951 | 951 |
|
| 952 |
// Rebuild the residual network |
|
| 952 |
// Rebuild the residual network |
|
| 953 | 953 |
buildResidualNetwork(); |
| 954 | 954 |
} |
| 955 | 955 |
} |
| 956 | 956 |
|
| 957 | 957 |
// Execute the "Cancel And Tighten" method |
| 958 | 958 |
void startCancelAndTighten() {
|
| 959 | 959 |
// Constants for the min mean cycle computations |
| 960 | 960 |
const double LIMIT_FACTOR = 1.0; |
| 961 | 961 |
const int MIN_LIMIT = 5; |
| 962 | 962 |
|
| 963 | 963 |
// Contruct auxiliary data vectors |
| 964 | 964 |
DoubleVector pi(_res_node_num, 0.0); |
| ... | ... |
@@ -1134,37 +1134,37 @@ |
| 1134 | 1134 |
} else {
|
| 1135 | 1135 |
typedef HowardMmc<StaticDigraph, CostArcMap> MMC; |
| 1136 | 1136 |
typedef typename BellmanFord<StaticDigraph, CostArcMap> |
| 1137 | 1137 |
::template SetDistMap<CostNodeMap>::Create BF; |
| 1138 | 1138 |
|
| 1139 | 1139 |
// Set epsilon to the minimum cycle mean |
| 1140 | 1140 |
buildResidualNetwork(); |
| 1141 | 1141 |
MMC mmc(_sgr, _cost_map); |
| 1142 | 1142 |
mmc.findCycleMean(); |
| 1143 | 1143 |
epsilon = -mmc.cycleMean(); |
| 1144 | 1144 |
Cost cycle_cost = mmc.cycleCost(); |
| 1145 | 1145 |
int cycle_size = mmc.cycleSize(); |
| 1146 |
|
|
| 1146 |
|
|
| 1147 | 1147 |
// Compute feasible potentials for the current epsilon |
| 1148 | 1148 |
for (int i = 0; i != int(_cost_vec.size()); ++i) {
|
| 1149 | 1149 |
_cost_vec[i] = cycle_size * _cost_vec[i] - cycle_cost; |
| 1150 | 1150 |
} |
| 1151 | 1151 |
BF bf(_sgr, _cost_map); |
| 1152 | 1152 |
bf.distMap(_pi_map); |
| 1153 | 1153 |
bf.init(0); |
| 1154 | 1154 |
bf.start(); |
| 1155 | 1155 |
for (int u = 0; u != _res_node_num; ++u) {
|
| 1156 | 1156 |
pi[u] = static_cast<double>(_pi[u]) / cycle_size; |
| 1157 | 1157 |
} |
| 1158 |
|
|
| 1158 |
|
|
| 1159 | 1159 |
iter = limit; |
| 1160 | 1160 |
} |
| 1161 | 1161 |
} |
| 1162 | 1162 |
} |
| 1163 | 1163 |
|
| 1164 | 1164 |
}; //class CycleCanceling |
| 1165 | 1165 |
|
| 1166 | 1166 |
///@} |
| 1167 | 1167 |
|
| 1168 | 1168 |
} //namespace lemon |
| 1169 | 1169 |
|
| 1170 | 1170 |
#endif //LEMON_CYCLE_CANCELING_H |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -73,25 +73,26 @@ |
| 73 | 73 |
#ifdef DOXYGEN |
| 74 | 74 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 75 | 75 |
#else |
| 76 | 76 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 77 | 77 |
#endif |
| 78 | 78 |
{
|
| 79 | 79 |
return new ProcessedMap(); |
| 80 | 80 |
} |
| 81 | 81 |
|
| 82 | 82 |
///The type of the map that indicates which nodes are reached. |
| 83 | 83 |
|
| 84 | 84 |
///The type of the map that indicates which nodes are reached. |
| 85 |
///It must conform to |
|
| 85 |
///It must conform to |
|
| 86 |
///the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
| 86 | 87 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 87 | 88 |
///Instantiates a \c ReachedMap. |
| 88 | 89 |
|
| 89 | 90 |
///This function instantiates a \ref ReachedMap. |
| 90 | 91 |
///\param g is the digraph, to which |
| 91 | 92 |
///we would like to define the \ref ReachedMap. |
| 92 | 93 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 93 | 94 |
{
|
| 94 | 95 |
return new ReachedMap(g); |
| 95 | 96 |
} |
| 96 | 97 |
|
| 97 | 98 |
///The type of the map that stores the distances of the nodes. |
| ... | ... |
@@ -261,25 +262,26 @@ |
| 261 | 262 |
typedef T ReachedMap; |
| 262 | 263 |
static ReachedMap *createReachedMap(const Digraph &) |
| 263 | 264 |
{
|
| 264 | 265 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
| 265 | 266 |
return 0; // ignore warnings |
| 266 | 267 |
} |
| 267 | 268 |
}; |
| 268 | 269 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 269 | 270 |
///\c ReachedMap type. |
| 270 | 271 |
/// |
| 271 | 272 |
///\ref named-templ-param "Named parameter" for setting |
| 272 | 273 |
///\c ReachedMap type. |
| 273 |
///It must conform to |
|
| 274 |
///It must conform to |
|
| 275 |
///the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
| 274 | 276 |
template <class T> |
| 275 | 277 |
struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
|
| 276 | 278 |
typedef Dfs< Digraph, SetReachedMapTraits<T> > Create; |
| 277 | 279 |
}; |
| 278 | 280 |
|
| 279 | 281 |
template <class T> |
| 280 | 282 |
struct SetProcessedMapTraits : public Traits {
|
| 281 | 283 |
typedef T ProcessedMap; |
| 282 | 284 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 283 | 285 |
{
|
| 284 | 286 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
| 285 | 287 |
return 0; // ignore warnings |
| ... | ... |
@@ -793,25 +795,26 @@ |
| 793 | 795 |
#ifdef DOXYGEN |
| 794 | 796 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
| 795 | 797 |
#else |
| 796 | 798 |
static ProcessedMap *createProcessedMap(const Digraph &) |
| 797 | 799 |
#endif |
| 798 | 800 |
{
|
| 799 | 801 |
return new ProcessedMap(); |
| 800 | 802 |
} |
| 801 | 803 |
|
| 802 | 804 |
///The type of the map that indicates which nodes are reached. |
| 803 | 805 |
|
| 804 | 806 |
///The type of the map that indicates which nodes are reached. |
| 805 |
///It must conform to |
|
| 807 |
///It must conform to |
|
| 808 |
///the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
| 806 | 809 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 807 | 810 |
///Instantiates a ReachedMap. |
| 808 | 811 |
|
| 809 | 812 |
///This function instantiates a ReachedMap. |
| 810 | 813 |
///\param g is the digraph, to which |
| 811 | 814 |
///we would like to define the ReachedMap. |
| 812 | 815 |
static ReachedMap *createReachedMap(const Digraph &g) |
| 813 | 816 |
{
|
| 814 | 817 |
return new ReachedMap(g); |
| 815 | 818 |
} |
| 816 | 819 |
|
| 817 | 820 |
///The type of the map that stores the distances of the nodes. |
| ... | ... |
@@ -1198,25 +1201,26 @@ |
| 1198 | 1201 |
/// |
| 1199 | 1202 |
/// Default traits class of DfsVisit class. |
| 1200 | 1203 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
| 1201 | 1204 |
template<class GR> |
| 1202 | 1205 |
struct DfsVisitDefaultTraits {
|
| 1203 | 1206 |
|
| 1204 | 1207 |
/// \brief The type of the digraph the algorithm runs on. |
| 1205 | 1208 |
typedef GR Digraph; |
| 1206 | 1209 |
|
| 1207 | 1210 |
/// \brief The type of the map that indicates which nodes are reached. |
| 1208 | 1211 |
/// |
| 1209 | 1212 |
/// The type of the map that indicates which nodes are reached. |
| 1210 |
/// It must conform to the |
|
| 1213 |
/// It must conform to the |
|
| 1214 |
/// \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
| 1211 | 1215 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
| 1212 | 1216 |
|
| 1213 | 1217 |
/// \brief Instantiates a ReachedMap. |
| 1214 | 1218 |
/// |
| 1215 | 1219 |
/// This function instantiates a ReachedMap. |
| 1216 | 1220 |
/// \param digraph is the digraph, to which |
| 1217 | 1221 |
/// we would like to define the ReachedMap. |
| 1218 | 1222 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1219 | 1223 |
return new ReachedMap(digraph); |
| 1220 | 1224 |
} |
| 1221 | 1225 |
|
| 1222 | 1226 |
}; |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -52,25 +52,25 @@ |
| 52 | 52 |
///The number of nodes in the graph |
| 53 | 53 |
int nodeNum; |
| 54 | 54 |
///The number of edges in the graph |
| 55 | 55 |
int edgeNum; |
| 56 | 56 |
int lineShift; |
| 57 | 57 |
///Constructor. It sets the type to \c NONE. |
| 58 | 58 |
DimacsDescriptor() : type(NONE) {}
|
| 59 | 59 |
}; |
| 60 | 60 |
|
| 61 | 61 |
///Discover the type of a DIMACS file |
| 62 | 62 |
|
| 63 | 63 |
///This function starts seeking the beginning of the given file for the |
| 64 |
///problem type and size info. |
|
| 64 |
///problem type and size info. |
|
| 65 | 65 |
///The found data is returned in a special struct that can be evaluated |
| 66 | 66 |
///and passed to the appropriate reader function. |
| 67 | 67 |
DimacsDescriptor dimacsType(std::istream& is) |
| 68 | 68 |
{
|
| 69 | 69 |
DimacsDescriptor r; |
| 70 | 70 |
std::string problem,str; |
| 71 | 71 |
char c; |
| 72 | 72 |
r.lineShift=0; |
| 73 | 73 |
while (is >> c) |
| 74 | 74 |
switch(c) |
| 75 | 75 |
{
|
| 76 | 76 |
case 'p': |
| ... | ... |
@@ -203,50 +203,50 @@ |
| 203 | 203 |
typename CapacityMap::Value _cap; |
| 204 | 204 |
std::string str; |
| 205 | 205 |
nodes.resize(desc.nodeNum + 1); |
| 206 | 206 |
for (int k = 1; k <= desc.nodeNum; ++k) {
|
| 207 | 207 |
nodes[k] = g.addNode(); |
| 208 | 208 |
} |
| 209 | 209 |
typedef typename CapacityMap::Value Capacity; |
| 210 | 210 |
|
| 211 | 211 |
if(infty==0) |
| 212 | 212 |
infty = std::numeric_limits<Capacity>::has_infinity ? |
| 213 | 213 |
std::numeric_limits<Capacity>::infinity() : |
| 214 | 214 |
std::numeric_limits<Capacity>::max(); |
| 215 |
|
|
| 215 |
|
|
| 216 | 216 |
while (is >> c) {
|
| 217 | 217 |
switch (c) {
|
| 218 | 218 |
case 'c': // comment line |
| 219 | 219 |
getline(is, str); |
| 220 | 220 |
break; |
| 221 | 221 |
case 'n': // node definition line |
| 222 | 222 |
if (desc.type==DimacsDescriptor::SP) { // shortest path problem
|
| 223 | 223 |
is >> i; |
| 224 | 224 |
getline(is, str); |
| 225 | 225 |
s = nodes[i]; |
| 226 | 226 |
} |
| 227 | 227 |
if (desc.type==DimacsDescriptor::MAX) { // max flow problem
|
| 228 | 228 |
is >> i >> d; |
| 229 | 229 |
getline(is, str); |
| 230 | 230 |
if (d == 's') s = nodes[i]; |
| 231 | 231 |
if (d == 't') t = nodes[i]; |
| 232 | 232 |
} |
| 233 | 233 |
break; |
| 234 | 234 |
case 'a': // arc definition line |
| 235 | 235 |
if (desc.type==DimacsDescriptor::SP) {
|
| 236 | 236 |
is >> i >> j >> _cap; |
| 237 | 237 |
getline(is, str); |
| 238 | 238 |
e = g.addArc(nodes[i], nodes[j]); |
| 239 | 239 |
capacity.set(e, _cap); |
| 240 |
} |
|
| 240 |
} |
|
| 241 | 241 |
else if (desc.type==DimacsDescriptor::MAX) {
|
| 242 | 242 |
is >> i >> j >> _cap; |
| 243 | 243 |
getline(is, str); |
| 244 | 244 |
e = g.addArc(nodes[i], nodes[j]); |
| 245 | 245 |
if (_cap >= 0) |
| 246 | 246 |
capacity.set(e, _cap); |
| 247 | 247 |
else |
| 248 | 248 |
capacity.set(e, infty); |
| 249 | 249 |
} |
| 250 | 250 |
else {
|
| 251 | 251 |
is >> i >> j; |
| 252 | 252 |
getline(is, str); |
| ... | ... |
@@ -353,29 +353,29 @@ |
| 353 | 353 |
_addArcEdge(Graph &g, typename Graph::Node s, typename Graph::Node t, |
| 354 | 354 |
dummy<0> = 0) |
| 355 | 355 |
{
|
| 356 | 356 |
g.addEdge(s,t); |
| 357 | 357 |
} |
| 358 | 358 |
template<typename Graph> |
| 359 | 359 |
typename disable_if<lemon::UndirectedTagIndicator<Graph>,void>::type |
| 360 | 360 |
_addArcEdge(Graph &g, typename Graph::Node s, typename Graph::Node t, |
| 361 | 361 |
dummy<1> = 1) |
| 362 | 362 |
{
|
| 363 | 363 |
g.addArc(s,t); |
| 364 | 364 |
} |
| 365 |
|
|
| 365 |
|
|
| 366 | 366 |
/// \brief DIMACS plain (di)graph reader function. |
| 367 | 367 |
/// |
| 368 | 368 |
/// This function reads a plain (di)graph without any designated nodes |
| 369 |
/// and maps (e.g. a matching instance) from DIMACS format, i.e. from |
|
| 369 |
/// and maps (e.g. a matching instance) from DIMACS format, i.e. from |
|
| 370 | 370 |
/// DIMACS files having a line starting with |
| 371 | 371 |
/// \code |
| 372 | 372 |
/// p mat |
| 373 | 373 |
/// \endcode |
| 374 | 374 |
/// At the beginning, \c g is cleared by \c g.clear(). |
| 375 | 375 |
/// |
| 376 | 376 |
/// If the file type was previously evaluated by dimacsType(), then |
| 377 | 377 |
/// the descriptor struct should be given by the \c dest parameter. |
| 378 | 378 |
template<typename Graph> |
| 379 | 379 |
void readDimacsMat(std::istream& is, Graph &g, |
| 380 | 380 |
DimacsDescriptor desc=DimacsDescriptor()) |
| 381 | 381 |
{
|
| ... | ... |
@@ -383,25 +383,25 @@ |
| 383 | 383 |
if(desc.type!=DimacsDescriptor::MAT) |
| 384 | 384 |
throw FormatError("Problem type mismatch");
|
| 385 | 385 |
|
| 386 | 386 |
g.clear(); |
| 387 | 387 |
std::vector<typename Graph::Node> nodes; |
| 388 | 388 |
char c; |
| 389 | 389 |
int i, j; |
| 390 | 390 |
std::string str; |
| 391 | 391 |
nodes.resize(desc.nodeNum + 1); |
| 392 | 392 |
for (int k = 1; k <= desc.nodeNum; ++k) {
|
| 393 | 393 |
nodes[k] = g.addNode(); |
| 394 | 394 |
} |
| 395 |
|
|
| 395 |
|
|
| 396 | 396 |
while (is >> c) {
|
| 397 | 397 |
switch (c) {
|
| 398 | 398 |
case 'c': // comment line |
| 399 | 399 |
getline(is, str); |
| 400 | 400 |
break; |
| 401 | 401 |
case 'n': // node definition line |
| 402 | 402 |
break; |
| 403 | 403 |
case 'a': // arc definition line |
| 404 | 404 |
is >> i >> j; |
| 405 | 405 |
getline(is, str); |
| 406 | 406 |
_addArcEdge(g,nodes[i], nodes[j]); |
| 407 | 407 |
break; |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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_EULER_H |
| 20 | 20 |
#define LEMON_EULER_H |
| 21 | 21 |
|
| 22 | 22 |
#include<lemon/core.h> |
| 23 | 23 |
#include<lemon/adaptors.h> |
| 24 | 24 |
#include<lemon/connectivity.h> |
| 25 | 25 |
#include <list> |
| 26 | 26 |
|
| 27 | 27 |
/// \ingroup graph_properties |
| 28 | 28 |
/// \file |
| 29 |
/// \brief Euler tour iterators and a function for checking the \e Eulerian |
|
| 29 |
/// \brief Euler tour iterators and a function for checking the \e Eulerian |
|
| 30 | 30 |
/// property. |
| 31 | 31 |
/// |
| 32 | 32 |
///This file provides Euler tour iterators and a function to check |
| 33 | 33 |
///if a (di)graph is \e Eulerian. |
| 34 | 34 |
|
| 35 | 35 |
namespace lemon {
|
| 36 | 36 |
|
| 37 | 37 |
///Euler tour iterator for digraphs. |
| 38 | 38 |
|
| 39 | 39 |
/// \ingroup graph_prop |
| 40 | 40 |
///This iterator provides an Euler tour (Eulerian circuit) of a \e directed |
| 41 | 41 |
///graph (if there exists) and it converts to the \c Arc type of the digraph. |
| 42 | 42 |
/// |
| 43 | 43 |
///For example, if the given digraph has an Euler tour (i.e it has only one |
| 44 |
///non-trivial component and the in-degree is equal to the out-degree |
|
| 44 |
///non-trivial component and the in-degree is equal to the out-degree |
|
| 45 | 45 |
///for all nodes), then the following code will put the arcs of \c g |
| 46 | 46 |
///to the vector \c et according to an Euler tour of \c g. |
| 47 | 47 |
///\code |
| 48 | 48 |
/// std::vector<ListDigraph::Arc> et; |
| 49 | 49 |
/// for(DiEulerIt<ListDigraph> e(g); e!=INVALID; ++e) |
| 50 | 50 |
/// et.push_back(e); |
| 51 | 51 |
///\endcode |
| 52 | 52 |
///If \c g has no Euler tour, then the resulted walk will not be closed |
| 53 | 53 |
///or not contain all arcs. |
| 54 | 54 |
///\sa EulerIt |
| 55 | 55 |
template<typename GR> |
| 56 | 56 |
class DiEulerIt |
| ... | ... |
@@ -129,34 +129,34 @@ |
| 129 | 129 |
++(*this); |
| 130 | 130 |
return e; |
| 131 | 131 |
} |
| 132 | 132 |
}; |
| 133 | 133 |
|
| 134 | 134 |
///Euler tour iterator for graphs. |
| 135 | 135 |
|
| 136 | 136 |
/// \ingroup graph_properties |
| 137 | 137 |
///This iterator provides an Euler tour (Eulerian circuit) of an |
| 138 | 138 |
///\e undirected graph (if there exists) and it converts to the \c Arc |
| 139 | 139 |
///and \c Edge types of the graph. |
| 140 | 140 |
/// |
| 141 |
///For example, if the given graph has an Euler tour (i.e it has only one |
|
| 141 |
///For example, if the given graph has an Euler tour (i.e it has only one |
|
| 142 | 142 |
///non-trivial component and the degree of each node is even), |
| 143 | 143 |
///the following code will print the arc IDs according to an |
| 144 | 144 |
///Euler tour of \c g. |
| 145 | 145 |
///\code |
| 146 | 146 |
/// for(EulerIt<ListGraph> e(g); e!=INVALID; ++e) {
|
| 147 | 147 |
/// std::cout << g.id(Edge(e)) << std::eol; |
| 148 | 148 |
/// } |
| 149 | 149 |
///\endcode |
| 150 |
///Although this iterator is for undirected graphs, it still returns |
|
| 150 |
///Although this iterator is for undirected graphs, it still returns |
|
| 151 | 151 |
///arcs in order to indicate the direction of the tour. |
| 152 | 152 |
///(But arcs convert to edges, of course.) |
| 153 | 153 |
/// |
| 154 | 154 |
///If \c g has no Euler tour, then the resulted walk will not be closed |
| 155 | 155 |
///or not contain all edges. |
| 156 | 156 |
template<typename GR> |
| 157 | 157 |
class EulerIt |
| 158 | 158 |
{
|
| 159 | 159 |
typedef typename GR::Node Node; |
| 160 | 160 |
typedef typename GR::NodeIt NodeIt; |
| 161 | 161 |
typedef typename GR::Arc Arc; |
| 162 | 162 |
typedef typename GR::Edge Edge; |
| ... | ... |
@@ -224,25 +224,25 @@ |
| 224 | 224 |
Node n=g.target(narc[s]); |
| 225 | 225 |
++narc[s]; |
| 226 | 226 |
s=n; |
| 227 | 227 |
} |
| 228 | 228 |
} |
| 229 | 229 |
return *this; |
| 230 | 230 |
} |
| 231 | 231 |
|
| 232 | 232 |
///Postfix incrementation |
| 233 | 233 |
|
| 234 | 234 |
/// Postfix incrementation. |
| 235 | 235 |
/// |
| 236 |
///\warning This incrementation returns an \c Arc (which converts to |
|
| 236 |
///\warning This incrementation returns an \c Arc (which converts to |
|
| 237 | 237 |
///an \c Edge), not an \ref EulerIt, as one may expect. |
| 238 | 238 |
Arc operator++(int) |
| 239 | 239 |
{
|
| 240 | 240 |
Arc e=*this; |
| 241 | 241 |
++(*this); |
| 242 | 242 |
return e; |
| 243 | 243 |
} |
| 244 | 244 |
}; |
| 245 | 245 |
|
| 246 | 246 |
|
| 247 | 247 |
///Check if the given graph is Eulerian |
| 248 | 248 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -2000,25 +2000,25 @@ |
| 2000 | 2000 |
} else {
|
| 2001 | 2001 |
augmentOnEdge(e); |
| 2002 | 2002 |
unmatched -= 2; |
| 2003 | 2003 |
} |
| 2004 | 2004 |
} break; |
| 2005 | 2005 |
} |
| 2006 | 2006 |
} |
| 2007 | 2007 |
return true; |
| 2008 | 2008 |
} |
| 2009 | 2009 |
|
| 2010 | 2010 |
/// \brief Run the algorithm. |
| 2011 | 2011 |
/// |
| 2012 |
/// This method runs the \c %MaxWeightedPerfectFractionalMatching |
|
| 2012 |
/// This method runs the \c %MaxWeightedPerfectFractionalMatching |
|
| 2013 | 2013 |
/// algorithm. |
| 2014 | 2014 |
/// |
| 2015 | 2015 |
/// \note mwfm.run() is just a shortcut of the following code. |
| 2016 | 2016 |
/// \code |
| 2017 | 2017 |
/// mwpfm.init(); |
| 2018 | 2018 |
/// mwpfm.start(); |
| 2019 | 2019 |
/// \endcode |
| 2020 | 2020 |
bool run() {
|
| 2021 | 2021 |
init(); |
| 2022 | 2022 |
return start(); |
| 2023 | 2023 |
} |
| 2024 | 2024 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -194,34 +194,34 @@ |
| 194 | 194 |
/// rebuilds the structure, therefore the maps of the digraph will be |
| 195 | 195 |
/// reallocated automatically and the previous values will be lost. |
| 196 | 196 |
void resize(int n) {
|
| 197 | 197 |
Parent::notifier(Arc()).clear(); |
| 198 | 198 |
Parent::notifier(Node()).clear(); |
| 199 | 199 |
construct(n); |
| 200 | 200 |
Parent::notifier(Node()).build(); |
| 201 | 201 |
Parent::notifier(Arc()).build(); |
| 202 | 202 |
} |
| 203 | 203 |
|
| 204 | 204 |
/// \brief Returns the node with the given index. |
| 205 | 205 |
/// |
| 206 |
/// Returns the node with the given index. Since this structure is |
|
| 206 |
/// Returns the node with the given index. Since this structure is |
|
| 207 | 207 |
/// completely static, the nodes can be indexed with integers from |
| 208 | 208 |
/// the range <tt>[0..nodeNum()-1]</tt>. |
| 209 | 209 |
/// The index of a node is the same as its ID. |
| 210 | 210 |
/// \sa index() |
| 211 | 211 |
Node operator()(int ix) const { return Parent::operator()(ix); }
|
| 212 | 212 |
|
| 213 | 213 |
/// \brief Returns the index of the given node. |
| 214 | 214 |
/// |
| 215 |
/// Returns the index of the given node. Since this structure is |
|
| 215 |
/// Returns the index of the given node. Since this structure is |
|
| 216 | 216 |
/// completely static, the nodes can be indexed with integers from |
| 217 | 217 |
/// the range <tt>[0..nodeNum()-1]</tt>. |
| 218 | 218 |
/// The index of a node is the same as its ID. |
| 219 | 219 |
/// \sa operator()() |
| 220 | 220 |
static int index(const Node& node) { return Parent::index(node); }
|
| 221 | 221 |
|
| 222 | 222 |
/// \brief Returns the arc connecting the given nodes. |
| 223 | 223 |
/// |
| 224 | 224 |
/// Returns the arc connecting the given nodes. |
| 225 | 225 |
Arc arc(Node u, Node v) const {
|
| 226 | 226 |
return Parent::arc(u, v); |
| 227 | 227 |
} |
| ... | ... |
@@ -573,34 +573,34 @@ |
| 573 | 573 |
void resize(int n) {
|
| 574 | 574 |
Parent::notifier(Arc()).clear(); |
| 575 | 575 |
Parent::notifier(Edge()).clear(); |
| 576 | 576 |
Parent::notifier(Node()).clear(); |
| 577 | 577 |
construct(n); |
| 578 | 578 |
Parent::notifier(Node()).build(); |
| 579 | 579 |
Parent::notifier(Edge()).build(); |
| 580 | 580 |
Parent::notifier(Arc()).build(); |
| 581 | 581 |
} |
| 582 | 582 |
|
| 583 | 583 |
/// \brief Returns the node with the given index. |
| 584 | 584 |
/// |
| 585 |
/// Returns the node with the given index. Since this structure is |
|
| 585 |
/// Returns the node with the given index. Since this structure is |
|
| 586 | 586 |
/// completely static, the nodes can be indexed with integers from |
| 587 | 587 |
/// the range <tt>[0..nodeNum()-1]</tt>. |
| 588 | 588 |
/// The index of a node is the same as its ID. |
| 589 | 589 |
/// \sa index() |
| 590 | 590 |
Node operator()(int ix) const { return Parent::operator()(ix); }
|
| 591 | 591 |
|
| 592 | 592 |
/// \brief Returns the index of the given node. |
| 593 | 593 |
/// |
| 594 |
/// Returns the index of the given node. Since this structure is |
|
| 594 |
/// Returns the index of the given node. Since this structure is |
|
| 595 | 595 |
/// completely static, the nodes can be indexed with integers from |
| 596 | 596 |
/// the range <tt>[0..nodeNum()-1]</tt>. |
| 597 | 597 |
/// The index of a node is the same as its ID. |
| 598 | 598 |
/// \sa operator()() |
| 599 | 599 |
static int index(const Node& node) { return Parent::index(node); }
|
| 600 | 600 |
|
| 601 | 601 |
/// \brief Returns the arc connecting the given nodes. |
| 602 | 602 |
/// |
| 603 | 603 |
/// Returns the arc connecting the given nodes. |
| 604 | 604 |
Arc arc(Node s, Node t) const {
|
| 605 | 605 |
return Parent::arc(s, t); |
| 606 | 606 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -50,38 +50,38 @@ |
| 50 | 50 |
int GlpkBase::_addCol() {
|
| 51 | 51 |
int i = glp_add_cols(lp, 1); |
| 52 | 52 |
glp_set_col_bnds(lp, i, GLP_FR, 0.0, 0.0); |
| 53 | 53 |
return i; |
| 54 | 54 |
} |
| 55 | 55 |
|
| 56 | 56 |
int GlpkBase::_addRow() {
|
| 57 | 57 |
int i = glp_add_rows(lp, 1); |
| 58 | 58 |
glp_set_row_bnds(lp, i, GLP_FR, 0.0, 0.0); |
| 59 | 59 |
return i; |
| 60 | 60 |
} |
| 61 | 61 |
|
| 62 |
int GlpkBase::_addRow(Value lo, ExprIterator b, |
|
| 62 |
int GlpkBase::_addRow(Value lo, ExprIterator b, |
|
| 63 | 63 |
ExprIterator e, Value up) {
|
| 64 | 64 |
int i = glp_add_rows(lp, 1); |
| 65 | 65 |
|
| 66 | 66 |
if (lo == -INF) {
|
| 67 | 67 |
if (up == INF) {
|
| 68 | 68 |
glp_set_row_bnds(lp, i, GLP_FR, lo, up); |
| 69 | 69 |
} else {
|
| 70 | 70 |
glp_set_row_bnds(lp, i, GLP_UP, lo, up); |
| 71 |
} |
|
| 71 |
} |
|
| 72 | 72 |
} else {
|
| 73 | 73 |
if (up == INF) {
|
| 74 | 74 |
glp_set_row_bnds(lp, i, GLP_LO, lo, up); |
| 75 |
} else if (lo != up) {
|
|
| 75 |
} else if (lo != up) {
|
|
| 76 | 76 |
glp_set_row_bnds(lp, i, GLP_DB, lo, up); |
| 77 | 77 |
} else {
|
| 78 | 78 |
glp_set_row_bnds(lp, i, GLP_FX, lo, up); |
| 79 | 79 |
} |
| 80 | 80 |
} |
| 81 | 81 |
|
| 82 | 82 |
std::vector<int> indexes; |
| 83 | 83 |
std::vector<Value> values; |
| 84 | 84 |
|
| 85 | 85 |
indexes.push_back(0); |
| 86 | 86 |
values.push_back(0); |
| 87 | 87 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -21,34 +21,34 @@ |
| 21 | 21 |
|
| 22 | 22 |
///\file |
| 23 | 23 |
///\brief Header of the LEMON-GLPK lp solver interface. |
| 24 | 24 |
///\ingroup lp_group |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/lp_base.h> |
| 27 | 27 |
|
| 28 | 28 |
namespace lemon {
|
| 29 | 29 |
|
| 30 | 30 |
namespace _solver_bits {
|
| 31 | 31 |
class VoidPtr {
|
| 32 | 32 |
private: |
| 33 |
void *_ptr; |
|
| 33 |
void *_ptr; |
|
| 34 | 34 |
public: |
| 35 | 35 |
VoidPtr() : _ptr(0) {}
|
| 36 | 36 |
|
| 37 | 37 |
template <typename T> |
| 38 | 38 |
VoidPtr(T* ptr) : _ptr(reinterpret_cast<void*>(ptr)) {}
|
| 39 | 39 |
|
| 40 | 40 |
template <typename T> |
| 41 |
VoidPtr& operator=(T* ptr) {
|
|
| 42 |
_ptr = reinterpret_cast<void*>(ptr); |
|
| 41 |
VoidPtr& operator=(T* ptr) {
|
|
| 42 |
_ptr = reinterpret_cast<void*>(ptr); |
|
| 43 | 43 |
return *this; |
| 44 | 44 |
} |
| 45 | 45 |
|
| 46 | 46 |
template <typename T> |
| 47 | 47 |
operator T*() const { return reinterpret_cast<T*>(_ptr); }
|
| 48 | 48 |
}; |
| 49 | 49 |
} |
| 50 | 50 |
|
| 51 | 51 |
/// \brief Base interface for the GLPK LP and MIP solver |
| 52 | 52 |
/// |
| 53 | 53 |
/// This class implements the common interface of the GLPK LP and MIP solver. |
| 54 | 54 |
/// \ingroup lp_group |
| ... | ... |
@@ -115,31 +115,31 @@ |
| 115 | 115 |
|
| 116 | 116 |
virtual void _messageLevel(MessageLevel level); |
| 117 | 117 |
|
| 118 | 118 |
private: |
| 119 | 119 |
|
| 120 | 120 |
static void freeEnv(); |
| 121 | 121 |
|
| 122 | 122 |
struct FreeEnvHelper {
|
| 123 | 123 |
~FreeEnvHelper() {
|
| 124 | 124 |
freeEnv(); |
| 125 | 125 |
} |
| 126 | 126 |
}; |
| 127 |
|
|
| 127 |
|
|
| 128 | 128 |
static FreeEnvHelper freeEnvHelper; |
| 129 | 129 |
|
| 130 | 130 |
protected: |
| 131 |
|
|
| 131 |
|
|
| 132 | 132 |
int _message_level; |
| 133 |
|
|
| 133 |
|
|
| 134 | 134 |
public: |
| 135 | 135 |
|
| 136 | 136 |
///Pointer to the underlying GLPK data structure. |
| 137 | 137 |
_solver_bits::VoidPtr lpx() {return lp;}
|
| 138 | 138 |
///Const pointer to the underlying GLPK data structure. |
| 139 | 139 |
_solver_bits::VoidPtr lpx() const {return lp;}
|
| 140 | 140 |
|
| 141 | 141 |
///Returns the constraint identifier understood by GLPK. |
| 142 | 142 |
int lpxRow(Row r) const { return rows(id(r)); }
|
| 143 | 143 |
|
| 144 | 144 |
///Returns the variable identifier understood by GLPK. |
| 145 | 145 |
int lpxCol(Col c) const { return cols(id(c)); }
|
| 1 |
/* -*- C++ -*- |
|
| 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
| 2 | 2 |
* |
| 3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
| 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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_GOMORY_HU_TREE_H |
| 20 | 20 |
#define LEMON_GOMORY_HU_TREE_H |
| 21 | 21 |
|
| 22 | 22 |
#include <limits> |
| 23 | 23 |
|
| 24 | 24 |
#include <lemon/core.h> |
| 25 | 25 |
#include <lemon/preflow.h> |
| 26 | 26 |
#include <lemon/concept_check.h> |
| 27 | 27 |
#include <lemon/concepts/maps.h> |
| 28 | 28 |
|
| 29 | 29 |
/// \ingroup min_cut |
| 30 |
/// \file |
|
| 30 |
/// \file |
|
| 31 | 31 |
/// \brief Gomory-Hu cut tree in graphs. |
| 32 | 32 |
|
| 33 | 33 |
namespace lemon {
|
| 34 | 34 |
|
| 35 | 35 |
/// \ingroup min_cut |
| 36 | 36 |
/// |
| 37 | 37 |
/// \brief Gomory-Hu cut tree algorithm |
| 38 | 38 |
/// |
| 39 | 39 |
/// The Gomory-Hu tree is a tree on the node set of a given graph, but it |
| 40 | 40 |
/// may contain edges which are not in the original graph. It has the |
| 41 |
/// property that the minimum capacity edge of the path between two nodes |
|
| 41 |
/// property that the minimum capacity edge of the path between two nodes |
|
| 42 | 42 |
/// in this tree has the same weight as the minimum cut in the graph |
| 43 | 43 |
/// between these nodes. Moreover the components obtained by removing |
| 44 | 44 |
/// this edge from the tree determine the corresponding minimum cut. |
| 45 | 45 |
/// Therefore once this tree is computed, the minimum cut between any pair |
| 46 | 46 |
/// of nodes can easily be obtained. |
| 47 |
/// |
|
| 47 |
/// |
|
| 48 | 48 |
/// The algorithm calculates \e n-1 distinct minimum cuts (currently with |
| 49 | 49 |
/// the \ref Preflow algorithm), thus it has \f$O(n^3\sqrt{e})\f$ overall
|
| 50 | 50 |
/// time complexity. It calculates a rooted Gomory-Hu tree. |
| 51 | 51 |
/// The structure of the tree and the edge weights can be |
| 52 | 52 |
/// obtained using \c predNode(), \c predValue() and \c rootDist(). |
| 53 | 53 |
/// The functions \c minCutMap() and \c minCutValue() calculate |
| 54 | 54 |
/// the minimum cut and the minimum cut value between any two nodes |
| 55 | 55 |
/// in the graph. You can also list (iterate on) the nodes and the |
| 56 | 56 |
/// edges of the cuts using \c MinCutNodeIt and \c MinCutEdgeIt. |
| 57 | 57 |
/// |
| 58 | 58 |
/// \tparam GR The type of the undirected graph the algorithm runs on. |
| 59 | 59 |
/// \tparam CAP The type of the edge map containing the capacities. |
| 60 | 60 |
/// The default map type is \ref concepts::Graph::EdgeMap "GR::EdgeMap<int>". |
| 61 | 61 |
#ifdef DOXYGEN |
| 62 | 62 |
template <typename GR, |
| 63 |
|
|
| 63 |
typename CAP> |
|
| 64 | 64 |
#else |
| 65 | 65 |
template <typename GR, |
| 66 |
|
|
| 66 |
typename CAP = typename GR::template EdgeMap<int> > |
|
| 67 | 67 |
#endif |
| 68 | 68 |
class GomoryHu {
|
| 69 | 69 |
public: |
| 70 | 70 |
|
| 71 | 71 |
/// The graph type of the algorithm |
| 72 | 72 |
typedef GR Graph; |
| 73 | 73 |
/// The capacity map type of the algorithm |
| 74 | 74 |
typedef CAP Capacity; |
| 75 | 75 |
/// The value type of capacities |
| 76 | 76 |
typedef typename Capacity::Value Value; |
| 77 |
|
|
| 77 |
|
|
| 78 | 78 |
private: |
| 79 | 79 |
|
| 80 | 80 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 81 | 81 |
|
| 82 | 82 |
const Graph& _graph; |
| 83 | 83 |
const Capacity& _capacity; |
| 84 | 84 |
|
| 85 | 85 |
Node _root; |
| 86 | 86 |
typename Graph::template NodeMap<Node>* _pred; |
| 87 | 87 |
typename Graph::template NodeMap<Value>* _weight; |
| 88 | 88 |
typename Graph::template NodeMap<int>* _order; |
| 89 | 89 |
|
| 90 | 90 |
void createStructures() {
|
| 91 | 91 |
if (!_pred) {
|
| 92 |
|
|
| 92 |
_pred = new typename Graph::template NodeMap<Node>(_graph); |
|
| 93 | 93 |
} |
| 94 | 94 |
if (!_weight) {
|
| 95 |
|
|
| 95 |
_weight = new typename Graph::template NodeMap<Value>(_graph); |
|
| 96 | 96 |
} |
| 97 | 97 |
if (!_order) {
|
| 98 |
|
|
| 98 |
_order = new typename Graph::template NodeMap<int>(_graph); |
|
| 99 | 99 |
} |
| 100 | 100 |
} |
| 101 | 101 |
|
| 102 | 102 |
void destroyStructures() {
|
| 103 | 103 |
if (_pred) {
|
| 104 |
|
|
| 104 |
delete _pred; |
|
| 105 | 105 |
} |
| 106 | 106 |
if (_weight) {
|
| 107 |
|
|
| 107 |
delete _weight; |
|
| 108 | 108 |
} |
| 109 | 109 |
if (_order) {
|
| 110 |
|
|
| 110 |
delete _order; |
|
| 111 | 111 |
} |
| 112 | 112 |
} |
| 113 |
|
|
| 113 |
|
|
| 114 | 114 |
public: |
| 115 | 115 |
|
| 116 | 116 |
/// \brief Constructor |
| 117 | 117 |
/// |
| 118 | 118 |
/// Constructor. |
| 119 | 119 |
/// \param graph The undirected graph the algorithm runs on. |
| 120 | 120 |
/// \param capacity The edge capacity map. |
| 121 |
GomoryHu(const Graph& graph, const Capacity& capacity) |
|
| 121 |
GomoryHu(const Graph& graph, const Capacity& capacity) |
|
| 122 | 122 |
: _graph(graph), _capacity(capacity), |
| 123 |
|
|
| 123 |
_pred(0), _weight(0), _order(0) |
|
| 124 | 124 |
{
|
| 125 | 125 |
checkConcept<concepts::ReadMap<Edge, Value>, Capacity>(); |
| 126 | 126 |
} |
| 127 | 127 |
|
| 128 | 128 |
|
| 129 | 129 |
/// \brief Destructor |
| 130 | 130 |
/// |
| 131 | 131 |
/// Destructor. |
| 132 | 132 |
~GomoryHu() {
|
| 133 | 133 |
destroyStructures(); |
| 134 | 134 |
} |
| 135 | 135 |
|
| 136 | 136 |
private: |
| 137 |
|
|
| 137 |
|
|
| 138 | 138 |
// Initialize the internal data structures |
| 139 | 139 |
void init() {
|
| 140 | 140 |
createStructures(); |
| 141 | 141 |
|
| 142 | 142 |
_root = NodeIt(_graph); |
| 143 | 143 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 144 | 144 |
(*_pred)[n] = _root; |
| 145 | 145 |
(*_order)[n] = -1; |
| 146 | 146 |
} |
| 147 | 147 |
(*_pred)[_root] = INVALID; |
| 148 |
(*_weight)[_root] = std::numeric_limits<Value>::max(); |
|
| 148 |
(*_weight)[_root] = std::numeric_limits<Value>::max(); |
|
| 149 | 149 |
} |
| 150 | 150 |
|
| 151 | 151 |
|
| 152 | 152 |
// Start the algorithm |
| 153 | 153 |
void start() {
|
| 154 | 154 |
Preflow<Graph, Capacity> fa(_graph, _capacity, _root, INVALID); |
| 155 | 155 |
|
| 156 | 156 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 157 |
|
|
| 157 |
if (n == _root) continue; |
|
| 158 | 158 |
|
| 159 |
Node pn = (*_pred)[n]; |
|
| 160 |
fa.source(n); |
|
| 161 |
|
|
| 159 |
Node pn = (*_pred)[n]; |
|
| 160 |
fa.source(n); |
|
| 161 |
fa.target(pn); |
|
| 162 | 162 |
|
| 163 |
|
|
| 163 |
fa.runMinCut(); |
|
| 164 | 164 |
|
| 165 |
|
|
| 165 |
(*_weight)[n] = fa.flowValue(); |
|
| 166 | 166 |
|
| 167 |
for (NodeIt nn(_graph); nn != INVALID; ++nn) {
|
|
| 168 |
if (nn != n && fa.minCut(nn) && (*_pred)[nn] == pn) {
|
|
| 169 |
(*_pred)[nn] = n; |
|
| 170 |
} |
|
| 171 |
} |
|
| 172 |
if ((*_pred)[pn] != INVALID && fa.minCut((*_pred)[pn])) {
|
|
| 173 |
(*_pred)[n] = (*_pred)[pn]; |
|
| 174 |
(*_pred)[pn] = n; |
|
| 175 |
(*_weight)[n] = (*_weight)[pn]; |
|
| 176 |
(*_weight)[pn] = fa.flowValue(); |
|
| 177 |
|
|
| 167 |
for (NodeIt nn(_graph); nn != INVALID; ++nn) {
|
|
| 168 |
if (nn != n && fa.minCut(nn) && (*_pred)[nn] == pn) {
|
|
| 169 |
(*_pred)[nn] = n; |
|
| 170 |
} |
|
| 171 |
} |
|
| 172 |
if ((*_pred)[pn] != INVALID && fa.minCut((*_pred)[pn])) {
|
|
| 173 |
(*_pred)[n] = (*_pred)[pn]; |
|
| 174 |
(*_pred)[pn] = n; |
|
| 175 |
(*_weight)[n] = (*_weight)[pn]; |
|
| 176 |
(*_weight)[pn] = fa.flowValue(); |
|
| 177 |
} |
|
| 178 | 178 |
} |
| 179 | 179 |
|
| 180 | 180 |
(*_order)[_root] = 0; |
| 181 | 181 |
int index = 1; |
| 182 | 182 |
|
| 183 | 183 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 184 |
std::vector<Node> st; |
|
| 185 |
Node nn = n; |
|
| 186 |
while ((*_order)[nn] == -1) {
|
|
| 187 |
st.push_back(nn); |
|
| 188 |
nn = (*_pred)[nn]; |
|
| 189 |
} |
|
| 190 |
while (!st.empty()) {
|
|
| 191 |
(*_order)[st.back()] = index++; |
|
| 192 |
st.pop_back(); |
|
| 193 |
} |
|
| 184 |
std::vector<Node> st; |
|
| 185 |
Node nn = n; |
|
| 186 |
while ((*_order)[nn] == -1) {
|
|
| 187 |
st.push_back(nn); |
|
| 188 |
nn = (*_pred)[nn]; |
|
| 189 |
} |
|
| 190 |
while (!st.empty()) {
|
|
| 191 |
(*_order)[st.back()] = index++; |
|
| 192 |
st.pop_back(); |
|
| 193 |
} |
|
| 194 | 194 |
} |
| 195 | 195 |
} |
| 196 | 196 |
|
| 197 | 197 |
public: |
| 198 | 198 |
|
| 199 | 199 |
///\name Execution Control |
| 200 |
|
|
| 200 |
|
|
| 201 | 201 |
///@{
|
| 202 | 202 |
|
| 203 | 203 |
/// \brief Run the Gomory-Hu algorithm. |
| 204 | 204 |
/// |
| 205 | 205 |
/// This function runs the Gomory-Hu algorithm. |
| 206 | 206 |
void run() {
|
| 207 | 207 |
init(); |
| 208 | 208 |
start(); |
| 209 | 209 |
} |
| 210 |
|
|
| 210 |
|
|
| 211 | 211 |
/// @} |
| 212 | 212 |
|
| 213 | 213 |
///\name Query Functions |
| 214 | 214 |
///The results of the algorithm can be obtained using these |
| 215 | 215 |
///functions.\n |
| 216 | 216 |
///\ref run() should be called before using them.\n |
| 217 | 217 |
///See also \ref MinCutNodeIt and \ref MinCutEdgeIt. |
| 218 | 218 |
|
| 219 | 219 |
///@{
|
| 220 | 220 |
|
| 221 | 221 |
/// \brief Return the predecessor node in the Gomory-Hu tree. |
| 222 | 222 |
/// |
| 223 | 223 |
/// This function returns the predecessor node of the given node |
| 224 | 224 |
/// in the Gomory-Hu tree. |
| 225 | 225 |
/// If \c node is the root of the tree, then it returns \c INVALID. |
| 226 | 226 |
/// |
| 227 | 227 |
/// \pre \ref run() must be called before using this function. |
| 228 | 228 |
Node predNode(const Node& node) const {
|
| 229 | 229 |
return (*_pred)[node]; |
| 230 | 230 |
} |
| 231 | 231 |
|
| 232 | 232 |
/// \brief Return the weight of the predecessor edge in the |
| 233 | 233 |
/// Gomory-Hu tree. |
| 234 | 234 |
/// |
| 235 |
/// This function returns the weight of the predecessor edge of the |
|
| 235 |
/// This function returns the weight of the predecessor edge of the |
|
| 236 | 236 |
/// given node in the Gomory-Hu tree. |
| 237 | 237 |
/// If \c node is the root of the tree, the result is undefined. |
| 238 | 238 |
/// |
| 239 | 239 |
/// \pre \ref run() must be called before using this function. |
| 240 | 240 |
Value predValue(const Node& node) const {
|
| 241 | 241 |
return (*_weight)[node]; |
| 242 | 242 |
} |
| 243 | 243 |
|
| 244 | 244 |
/// \brief Return the distance from the root node in the Gomory-Hu tree. |
| 245 | 245 |
/// |
| 246 | 246 |
/// This function returns the distance of the given node from the root |
| 247 | 247 |
/// node in the Gomory-Hu tree. |
| 248 | 248 |
/// |
| 249 | 249 |
/// \pre \ref run() must be called before using this function. |
| 250 | 250 |
int rootDist(const Node& node) const {
|
| 251 | 251 |
return (*_order)[node]; |
| 252 | 252 |
} |
| 253 | 253 |
|
| 254 | 254 |
/// \brief Return the minimum cut value between two nodes |
| 255 | 255 |
/// |
| 256 | 256 |
/// This function returns the minimum cut value between the nodes |
| 257 |
/// \c s and \c t. |
|
| 257 |
/// \c s and \c t. |
|
| 258 | 258 |
/// It finds the nearest common ancestor of the given nodes in the |
| 259 | 259 |
/// Gomory-Hu tree and calculates the minimum weight edge on the |
| 260 | 260 |
/// paths to the ancestor. |
| 261 | 261 |
/// |
| 262 | 262 |
/// \pre \ref run() must be called before using this function. |
| 263 | 263 |
Value minCutValue(const Node& s, const Node& t) const {
|
| 264 | 264 |
Node sn = s, tn = t; |
| 265 | 265 |
Value value = std::numeric_limits<Value>::max(); |
| 266 |
|
|
| 266 |
|
|
| 267 | 267 |
while (sn != tn) {
|
| 268 |
if ((*_order)[sn] < (*_order)[tn]) {
|
|
| 269 |
if ((*_weight)[tn] <= value) value = (*_weight)[tn]; |
|
| 270 |
tn = (*_pred)[tn]; |
|
| 271 |
} else {
|
|
| 272 |
if ((*_weight)[sn] <= value) value = (*_weight)[sn]; |
|
| 273 |
sn = (*_pred)[sn]; |
|
| 274 |
|
|
| 268 |
if ((*_order)[sn] < (*_order)[tn]) {
|
|
| 269 |
if ((*_weight)[tn] <= value) value = (*_weight)[tn]; |
|
| 270 |
tn = (*_pred)[tn]; |
|
| 271 |
} else {
|
|
| 272 |
if ((*_weight)[sn] <= value) value = (*_weight)[sn]; |
|
| 273 |
sn = (*_pred)[sn]; |
|
| 274 |
} |
|
| 275 | 275 |
} |
| 276 | 276 |
return value; |
| 277 | 277 |
} |
| 278 | 278 |
|
| 279 | 279 |
/// \brief Return the minimum cut between two nodes |
| 280 | 280 |
/// |
| 281 | 281 |
/// This function returns the minimum cut between the nodes \c s and \c t |
| 282 | 282 |
/// in the \c cutMap parameter by setting the nodes in the component of |
| 283 | 283 |
/// \c s to \c true and the other nodes to \c false. |
| 284 | 284 |
/// |
| 285 | 285 |
/// For higher level interfaces see MinCutNodeIt and MinCutEdgeIt. |
| 286 | 286 |
/// |
| ... | ... |
@@ -293,72 +293,72 @@ |
| 293 | 293 |
/// \return The value of the minimum cut between \c s and \c t. |
| 294 | 294 |
/// |
| 295 | 295 |
/// \pre \ref run() must be called before using this function. |
| 296 | 296 |
template <typename CutMap> |
| 297 | 297 |
Value minCutMap(const Node& s, |
| 298 | 298 |
const Node& t, |
| 299 | 299 |
CutMap& cutMap |
| 300 | 300 |
) const {
|
| 301 | 301 |
Node sn = s, tn = t; |
| 302 | 302 |
bool s_root=false; |
| 303 | 303 |
Node rn = INVALID; |
| 304 | 304 |
Value value = std::numeric_limits<Value>::max(); |
| 305 |
|
|
| 305 |
|
|
| 306 | 306 |
while (sn != tn) {
|
| 307 |
if ((*_order)[sn] < (*_order)[tn]) {
|
|
| 308 |
if ((*_weight)[tn] <= value) {
|
|
| 309 |
|
|
| 307 |
if ((*_order)[sn] < (*_order)[tn]) {
|
|
| 308 |
if ((*_weight)[tn] <= value) {
|
|
| 309 |
rn = tn; |
|
| 310 | 310 |
s_root = false; |
| 311 |
value = (*_weight)[tn]; |
|
| 312 |
} |
|
| 313 |
tn = (*_pred)[tn]; |
|
| 314 |
} else {
|
|
| 315 |
if ((*_weight)[sn] <= value) {
|
|
| 316 |
rn = sn; |
|
| 311 |
value = (*_weight)[tn]; |
|
| 312 |
} |
|
| 313 |
tn = (*_pred)[tn]; |
|
| 314 |
} else {
|
|
| 315 |
if ((*_weight)[sn] <= value) {
|
|
| 316 |
rn = sn; |
|
| 317 | 317 |
s_root = true; |
| 318 |
value = (*_weight)[sn]; |
|
| 319 |
} |
|
| 320 |
sn = (*_pred)[sn]; |
|
| 321 |
} |
|
| 318 |
value = (*_weight)[sn]; |
|
| 319 |
} |
|
| 320 |
sn = (*_pred)[sn]; |
|
| 321 |
} |
|
| 322 | 322 |
} |
| 323 | 323 |
|
| 324 | 324 |
typename Graph::template NodeMap<bool> reached(_graph, false); |
| 325 | 325 |
reached[_root] = true; |
| 326 | 326 |
cutMap.set(_root, !s_root); |
| 327 | 327 |
reached[rn] = true; |
| 328 | 328 |
cutMap.set(rn, s_root); |
| 329 | 329 |
|
| 330 | 330 |
std::vector<Node> st; |
| 331 | 331 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 332 |
|
|
| 332 |
st.clear(); |
|
| 333 | 333 |
Node nn = n; |
| 334 |
while (!reached[nn]) {
|
|
| 335 |
st.push_back(nn); |
|
| 336 |
nn = (*_pred)[nn]; |
|
| 337 |
} |
|
| 338 |
while (!st.empty()) {
|
|
| 339 |
cutMap.set(st.back(), cutMap[nn]); |
|
| 340 |
st.pop_back(); |
|
| 341 |
} |
|
| 334 |
while (!reached[nn]) {
|
|
| 335 |
st.push_back(nn); |
|
| 336 |
nn = (*_pred)[nn]; |
|
| 337 |
} |
|
| 338 |
while (!st.empty()) {
|
|
| 339 |
cutMap.set(st.back(), cutMap[nn]); |
|
| 340 |
st.pop_back(); |
|
| 341 |
} |
|
| 342 | 342 |
} |
| 343 |
|
|
| 343 |
|
|
| 344 | 344 |
return value; |
| 345 | 345 |
} |
| 346 | 346 |
|
| 347 | 347 |
///@} |
| 348 | 348 |
|
| 349 | 349 |
friend class MinCutNodeIt; |
| 350 | 350 |
|
| 351 | 351 |
/// Iterate on the nodes of a minimum cut |
| 352 |
|
|
| 352 |
|
|
| 353 | 353 |
/// This iterator class lists the nodes of a minimum cut found by |
| 354 | 354 |
/// GomoryHu. Before using it, you must allocate a GomoryHu class |
| 355 | 355 |
/// and call its \ref GomoryHu::run() "run()" method. |
| 356 | 356 |
/// |
| 357 | 357 |
/// This example counts the nodes in the minimum cut separating \c s from |
| 358 | 358 |
/// \c t. |
| 359 | 359 |
/// \code |
| 360 | 360 |
/// GomoryHu<Graph> gom(g, capacities); |
| 361 | 361 |
/// gom.run(); |
| 362 | 362 |
/// int cnt=0; |
| 363 | 363 |
/// for(GomoryHu<Graph>::MinCutNodeIt n(gom,s,t); n!=INVALID; ++n) ++cnt; |
| 364 | 364 |
/// \endcode |
| ... | ... |
@@ -433,29 +433,29 @@ |
| 433 | 433 |
/// Postfix incrementation. |
| 434 | 434 |
/// |
| 435 | 435 |
/// \warning This incrementation |
| 436 | 436 |
/// returns a \c Node, not a \c MinCutNodeIt, as one may |
| 437 | 437 |
/// expect. |
| 438 | 438 |
typename Graph::Node operator++(int) |
| 439 | 439 |
{
|
| 440 | 440 |
typename Graph::Node n=*this; |
| 441 | 441 |
++(*this); |
| 442 | 442 |
return n; |
| 443 | 443 |
} |
| 444 | 444 |
}; |
| 445 |
|
|
| 445 |
|
|
| 446 | 446 |
friend class MinCutEdgeIt; |
| 447 |
|
|
| 447 |
|
|
| 448 | 448 |
/// Iterate on the edges of a minimum cut |
| 449 |
|
|
| 449 |
|
|
| 450 | 450 |
/// This iterator class lists the edges of a minimum cut found by |
| 451 | 451 |
/// GomoryHu. Before using it, you must allocate a GomoryHu class |
| 452 | 452 |
/// and call its \ref GomoryHu::run() "run()" method. |
| 453 | 453 |
/// |
| 454 | 454 |
/// This example computes the value of the minimum cut separating \c s from |
| 455 | 455 |
/// \c t. |
| 456 | 456 |
/// \code |
| 457 | 457 |
/// GomoryHu<Graph> gom(g, capacities); |
| 458 | 458 |
/// gom.run(); |
| 459 | 459 |
/// int value=0; |
| 460 | 460 |
/// for(GomoryHu<Graph>::MinCutEdgeIt e(gom,s,t); e!=INVALID; ++e) |
| 461 | 461 |
/// value+=capacities[e]; |
| ... | ... |
@@ -470,25 +470,25 @@ |
| 470 | 470 |
typename Graph::OutArcIt _arc_it; |
| 471 | 471 |
typename Graph::template NodeMap<bool> _cut; |
| 472 | 472 |
void step() |
| 473 | 473 |
{
|
| 474 | 474 |
++_arc_it; |
| 475 | 475 |
while(_node_it!=INVALID && _arc_it==INVALID) |
| 476 | 476 |
{
|
| 477 | 477 |
for(++_node_it;_node_it!=INVALID&&!_cut[_node_it];++_node_it) {}
|
| 478 | 478 |
if(_node_it!=INVALID) |
| 479 | 479 |
_arc_it=typename Graph::OutArcIt(_graph,_node_it); |
| 480 | 480 |
} |
| 481 | 481 |
} |
| 482 |
|
|
| 482 |
|
|
| 483 | 483 |
public: |
| 484 | 484 |
/// Constructor |
| 485 | 485 |
|
| 486 | 486 |
/// Constructor. |
| 487 | 487 |
/// |
| 488 | 488 |
MinCutEdgeIt(GomoryHu const &gomory, |
| 489 | 489 |
///< The GomoryHu class. You must call its |
| 490 | 490 |
/// run() method |
| 491 | 491 |
/// before initializing this iterator. |
| 492 | 492 |
const Node& s, ///< The base node. |
| 493 | 493 |
const Node& t, |
| 494 | 494 |
///< The node you want to separate from node \c s. |
| ... | ... |
@@ -539,25 +539,25 @@ |
| 539 | 539 |
bool operator!=(Invalid) { return _node_it!=INVALID; }
|
| 540 | 540 |
/// Next edge |
| 541 | 541 |
|
| 542 | 542 |
/// Next edge. |
| 543 | 543 |
/// |
| 544 | 544 |
MinCutEdgeIt &operator++() |
| 545 | 545 |
{
|
| 546 | 546 |
step(); |
| 547 | 547 |
while(_arc_it!=INVALID && _cut[_graph.target(_arc_it)]) step(); |
| 548 | 548 |
return *this; |
| 549 | 549 |
} |
| 550 | 550 |
/// Postfix incrementation |
| 551 |
|
|
| 551 |
|
|
| 552 | 552 |
/// Postfix incrementation. |
| 553 | 553 |
/// |
| 554 | 554 |
/// \warning This incrementation |
| 555 | 555 |
/// returns an \c Arc, not a \c MinCutEdgeIt, as one may expect. |
| 556 | 556 |
typename Graph::Arc operator++(int) |
| 557 | 557 |
{
|
| 558 | 558 |
typename Graph::Arc e=*this; |
| 559 | 559 |
++(*this); |
| 560 | 560 |
return e; |
| 561 | 561 |
} |
| 562 | 562 |
}; |
| 563 | 563 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -22,70 +22,70 @@ |
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <list> |
| 24 | 24 |
#include <limits> |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/maps.h> |
| 27 | 27 |
#include <lemon/core.h> |
| 28 | 28 |
#include <lemon/tolerance.h> |
| 29 | 29 |
|
| 30 | 30 |
/// \file |
| 31 | 31 |
/// \ingroup min_cut |
| 32 | 32 |
/// \brief Implementation of the Hao-Orlin algorithm. |
| 33 | 33 |
/// |
| 34 |
/// Implementation of the Hao-Orlin algorithm for finding a minimum cut |
|
| 34 |
/// Implementation of the Hao-Orlin algorithm for finding a minimum cut |
|
| 35 | 35 |
/// in a digraph. |
| 36 | 36 |
|
| 37 | 37 |
namespace lemon {
|
| 38 | 38 |
|
| 39 | 39 |
/// \ingroup min_cut |
| 40 | 40 |
/// |
| 41 | 41 |
/// \brief Hao-Orlin algorithm for finding a minimum cut in a digraph. |
| 42 | 42 |
/// |
| 43 | 43 |
/// This class implements the Hao-Orlin algorithm for finding a minimum |
| 44 |
/// value cut in a directed graph \f$D=(V,A)\f$. |
|
| 44 |
/// value cut in a directed graph \f$D=(V,A)\f$. |
|
| 45 | 45 |
/// It takes a fixed node \f$ source \in V \f$ and |
| 46 | 46 |
/// consists of two phases: in the first phase it determines a |
| 47 | 47 |
/// minimum cut with \f$ source \f$ on the source-side (i.e. a set |
| 48 | 48 |
/// \f$ X\subsetneq V \f$ with \f$ source \in X \f$ and minimal outgoing |
| 49 | 49 |
/// capacity) and in the second phase it determines a minimum cut |
| 50 | 50 |
/// with \f$ source \f$ on the sink-side (i.e. a set |
| 51 | 51 |
/// \f$ X\subsetneq V \f$ with \f$ source \notin X \f$ and minimal outgoing |
| 52 | 52 |
/// capacity). Obviously, the smaller of these two cuts will be a |
| 53 | 53 |
/// minimum cut of \f$ D \f$. The algorithm is a modified |
| 54 | 54 |
/// preflow push-relabel algorithm. Our implementation calculates |
| 55 | 55 |
/// the minimum cut in \f$ O(n^2\sqrt{m}) \f$ time (we use the
|
| 56 | 56 |
/// highest-label rule), or in \f$O(nm)\f$ for unit capacities. The |
| 57 | 57 |
/// purpose of such algorithm is e.g. testing network reliability. |
| 58 | 58 |
/// |
| 59 | 59 |
/// For an undirected graph you can run just the first phase of the |
| 60 | 60 |
/// algorithm or you can use the algorithm of Nagamochi and Ibaraki, |
| 61 |
/// which solves the undirected problem in \f$ O(nm + n^2 \log n) \f$ |
|
| 61 |
/// which solves the undirected problem in \f$ O(nm + n^2 \log n) \f$ |
|
| 62 | 62 |
/// time. It is implemented in the NagamochiIbaraki algorithm class. |
| 63 | 63 |
/// |
| 64 | 64 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 65 | 65 |
/// \tparam CAP The type of the arc map containing the capacities, |
| 66 | 66 |
/// which can be any numreric type. The default map type is |
| 67 | 67 |
/// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 68 | 68 |
/// \tparam TOL Tolerance class for handling inexact computations. The |
| 69 | 69 |
/// default tolerance type is \ref Tolerance "Tolerance<CAP::Value>". |
| 70 | 70 |
#ifdef DOXYGEN |
| 71 | 71 |
template <typename GR, typename CAP, typename TOL> |
| 72 | 72 |
#else |
| 73 | 73 |
template <typename GR, |
| 74 | 74 |
typename CAP = typename GR::template ArcMap<int>, |
| 75 | 75 |
typename TOL = Tolerance<typename CAP::Value> > |
| 76 | 76 |
#endif |
| 77 | 77 |
class HaoOrlin {
|
| 78 | 78 |
public: |
| 79 |
|
|
| 79 |
|
|
| 80 | 80 |
/// The digraph type of the algorithm |
| 81 | 81 |
typedef GR Digraph; |
| 82 | 82 |
/// The capacity map type of the algorithm |
| 83 | 83 |
typedef CAP CapacityMap; |
| 84 | 84 |
/// The tolerance type of the algorithm |
| 85 | 85 |
typedef TOL Tolerance; |
| 86 | 86 |
|
| 87 | 87 |
private: |
| 88 | 88 |
|
| 89 | 89 |
typedef typename CapacityMap::Value Value; |
| 90 | 90 |
|
| 91 | 91 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| ... | ... |
@@ -855,25 +855,25 @@ |
| 855 | 855 |
/// |
| 856 | 856 |
/// This function initializes the internal data structures. It creates |
| 857 | 857 |
/// the maps and some bucket structures for the algorithm. |
| 858 | 858 |
/// The first node is used as the source node for the push-relabel |
| 859 | 859 |
/// algorithm. |
| 860 | 860 |
void init() {
|
| 861 | 861 |
init(NodeIt(_graph)); |
| 862 | 862 |
} |
| 863 | 863 |
|
| 864 | 864 |
/// \brief Initialize the internal data structures. |
| 865 | 865 |
/// |
| 866 | 866 |
/// This function initializes the internal data structures. It creates |
| 867 |
/// the maps and some bucket structures for the algorithm. |
|
| 867 |
/// the maps and some bucket structures for the algorithm. |
|
| 868 | 868 |
/// The given node is used as the source node for the push-relabel |
| 869 | 869 |
/// algorithm. |
| 870 | 870 |
void init(const Node& source) {
|
| 871 | 871 |
_source = source; |
| 872 | 872 |
|
| 873 | 873 |
_node_num = countNodes(_graph); |
| 874 | 874 |
|
| 875 | 875 |
_first.resize(_node_num); |
| 876 | 876 |
_last.resize(_node_num); |
| 877 | 877 |
|
| 878 | 878 |
_dormant.resize(_node_num); |
| 879 | 879 |
|
| ... | ... |
@@ -935,67 +935,67 @@ |
| 935 | 935 |
/// |
| 936 | 936 |
/// This function runs the algorithm. It finds nodes \c source and |
| 937 | 937 |
/// \c target arbitrarily and then calls \ref init(), \ref calculateOut() |
| 938 | 938 |
/// and \ref calculateIn(). |
| 939 | 939 |
void run() {
|
| 940 | 940 |
init(); |
| 941 | 941 |
calculateOut(); |
| 942 | 942 |
calculateIn(); |
| 943 | 943 |
} |
| 944 | 944 |
|
| 945 | 945 |
/// \brief Run the algorithm. |
| 946 | 946 |
/// |
| 947 |
/// This function runs the algorithm. It uses the given \c source node, |
|
| 947 |
/// This function runs the algorithm. It uses the given \c source node, |
|
| 948 | 948 |
/// finds a proper \c target node and then calls the \ref init(), |
| 949 | 949 |
/// \ref calculateOut() and \ref calculateIn(). |
| 950 | 950 |
void run(const Node& s) {
|
| 951 | 951 |
init(s); |
| 952 | 952 |
calculateOut(); |
| 953 | 953 |
calculateIn(); |
| 954 | 954 |
} |
| 955 | 955 |
|
| 956 | 956 |
/// @} |
| 957 | 957 |
|
| 958 | 958 |
/// \name Query Functions |
| 959 | 959 |
/// The result of the %HaoOrlin algorithm |
| 960 | 960 |
/// can be obtained using these functions.\n |
| 961 |
/// \ref run(), \ref calculateOut() or \ref calculateIn() |
|
| 961 |
/// \ref run(), \ref calculateOut() or \ref calculateIn() |
|
| 962 | 962 |
/// should be called before using them. |
| 963 | 963 |
|
| 964 | 964 |
/// @{
|
| 965 | 965 |
|
| 966 | 966 |
/// \brief Return the value of the minimum cut. |
| 967 | 967 |
/// |
| 968 | 968 |
/// This function returns the value of the minimum cut. |
| 969 | 969 |
/// |
| 970 |
/// \pre \ref run(), \ref calculateOut() or \ref calculateIn() |
|
| 970 |
/// \pre \ref run(), \ref calculateOut() or \ref calculateIn() |
|
| 971 | 971 |
/// must be called before using this function. |
| 972 | 972 |
Value minCutValue() const {
|
| 973 | 973 |
return _min_cut; |
| 974 | 974 |
} |
| 975 | 975 |
|
| 976 | 976 |
|
| 977 | 977 |
/// \brief Return a minimum cut. |
| 978 | 978 |
/// |
| 979 | 979 |
/// This function sets \c cutMap to the characteristic vector of a |
| 980 | 980 |
/// minimum value cut: it will give a non-empty set \f$ X\subsetneq V \f$ |
| 981 | 981 |
/// with minimal outgoing capacity (i.e. \c cutMap will be \c true exactly |
| 982 | 982 |
/// for the nodes of \f$ X \f$). |
| 983 | 983 |
/// |
| 984 | 984 |
/// \param cutMap A \ref concepts::WriteMap "writable" node map with |
| 985 | 985 |
/// \c bool (or convertible) value type. |
| 986 | 986 |
/// |
| 987 | 987 |
/// \return The value of the minimum cut. |
| 988 | 988 |
/// |
| 989 |
/// \pre \ref run(), \ref calculateOut() or \ref calculateIn() |
|
| 989 |
/// \pre \ref run(), \ref calculateOut() or \ref calculateIn() |
|
| 990 | 990 |
/// must be called before using this function. |
| 991 | 991 |
template <typename CutMap> |
| 992 | 992 |
Value minCutMap(CutMap& cutMap) const {
|
| 993 | 993 |
for (NodeIt it(_graph); it != INVALID; ++it) {
|
| 994 | 994 |
cutMap.set(it, (*_min_cut_map)[it]); |
| 995 | 995 |
} |
| 996 | 996 |
return _min_cut; |
| 997 | 997 |
} |
| 998 | 998 |
|
| 999 | 999 |
/// @} |
| 1000 | 1000 |
|
| 1001 | 1001 |
}; //class HaoOrlin |
| 1 |
/* -*- C++ -*- |
|
| 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
| 2 | 2 |
* |
| 3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
| 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 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 |
*/ |
| ... | ... |
@@ -334,32 +334,32 @@ |
| 334 | 334 |
} |
| 335 | 335 |
|
| 336 | 336 |
/// \brief Find the minimum cycle mean. |
| 337 | 337 |
/// |
| 338 | 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 | 342 |
bool findCycleMean() {
|
| 343 | 343 |
// Initialization and find strongly connected components |
| 344 | 344 |
init(); |
| 345 | 345 |
findComponents(); |
| 346 |
|
|
| 346 |
|
|
| 347 | 347 |
// Find the minimum cycle mean in the components |
| 348 | 348 |
for (int comp = 0; comp < _comp_num; ++comp) {
|
| 349 | 349 |
if (!initComponent(comp)) continue; |
| 350 | 350 |
processRounds(); |
| 351 |
|
|
| 351 |
|
|
| 352 | 352 |
// Update the best cycle (global minimum mean cycle) |
| 353 |
if ( _curr_found && (!_best_found || |
|
| 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; |
| 359 | 359 |
_best_level = _curr_level; |
| 360 | 360 |
} |
| 361 | 361 |
} |
| 362 | 362 |
return _best_found; |
| 363 | 363 |
} |
| 364 | 364 |
|
| 365 | 365 |
/// \brief Find a minimum mean directed cycle. |
| ... | ... |
@@ -494,25 +494,25 @@ |
| 494 | 494 |
if (_comp[_gr.target(a)] == k) _out_arcs[n].push_back(a); |
| 495 | 495 |
} |
| 496 | 496 |
} |
| 497 | 497 |
} |
| 498 | 498 |
} |
| 499 | 499 |
|
| 500 | 500 |
// Initialize path data for the current component |
| 501 | 501 |
bool initComponent(int comp) {
|
| 502 | 502 |
_nodes = &(_comp_nodes[comp]); |
| 503 | 503 |
int n = _nodes->size(); |
| 504 | 504 |
if (n < 1 || (n == 1 && _out_arcs[(*_nodes)[0]].size() == 0)) {
|
| 505 | 505 |
return false; |
| 506 |
} |
|
| 506 |
} |
|
| 507 | 507 |
for (int i = 0; i < n; ++i) {
|
| 508 | 508 |
_data[(*_nodes)[i]].resize(n + 1, PathData(INF)); |
| 509 | 509 |
} |
| 510 | 510 |
return true; |
| 511 | 511 |
} |
| 512 | 512 |
|
| 513 | 513 |
// Process all rounds of computing path data for the current component. |
| 514 | 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() {
|
| 517 | 517 |
Node start = (*_nodes)[0]; |
| 518 | 518 |
_data[start][0] = PathData(0); |
| ... | ... |
@@ -567,57 +567,57 @@ |
| 567 | 567 |
for (int i = 0; i < int(_nodes->size()); ++i) {
|
| 568 | 568 |
u = (*_nodes)[i]; |
| 569 | 569 |
for (int j = 0; j < int(_out_arcs[u].size()); ++j) {
|
| 570 | 570 |
e = _out_arcs[u][j]; |
| 571 | 571 |
v = _gr.target(e); |
| 572 | 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); |
| 575 | 575 |
} |
| 576 | 576 |
} |
| 577 | 577 |
} |
| 578 | 578 |
} |
| 579 |
|
|
| 579 |
|
|
| 580 | 580 |
// Check early termination |
| 581 | 581 |
bool checkTermination(int k) {
|
| 582 | 582 |
typedef std::pair<int, int> Pair; |
| 583 | 583 |
typename GR::template NodeMap<Pair> level(_gr, Pair(-1, 0)); |
| 584 | 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; |
| 589 |
|
|
| 589 |
|
|
| 590 | 590 |
// Search for cycles that are already found |
| 591 | 591 |
_curr_found = false; |
| 592 | 592 |
for (int i = 0; i < n; ++i) {
|
| 593 | 593 |
u = (*_nodes)[i]; |
| 594 | 594 |
if (_data[u][k].dist == INF) continue; |
| 595 | 595 |
for (int j = k; j >= 0; --j) {
|
| 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 | 600 |
if (!_curr_found || cost * _curr_size < _curr_cost * size) {
|
| 601 | 601 |
_curr_cost = cost; |
| 602 | 602 |
_curr_size = size; |
| 603 | 603 |
_curr_node = u; |
| 604 | 604 |
_curr_level = level[u].second; |
| 605 | 605 |
_curr_found = true; |
| 606 | 606 |
} |
| 607 | 607 |
} |
| 608 | 608 |
level[u] = Pair(i, j); |
| 609 | 609 |
if (j != 0) {
|
| 610 |
u = _gr.source(_data[u][j].pred); |
|
| 611 |
} |
|
| 610 |
u = _gr.source(_data[u][j].pred); |
|
| 611 |
} |
|
| 612 | 612 |
} |
| 613 | 613 |
} |
| 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 |
| 619 | 619 |
for (int i = 0; i < n; ++i) {
|
| 620 | 620 |
u = (*_nodes)[i]; |
| 621 | 621 |
pi[u] = INF; |
| 622 | 622 |
for (int j = 0; j <= k; ++j) {
|
| 623 | 623 |
if (_data[u][j].dist < INF) {
|
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)