0
89
0
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
11
11
2
2
46
46
11
11
15
15
2
2
4
4
31
31
12
12
54
53
| 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 |
///\ingroup demos |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief Argument parser demo |
| 22 | 22 |
/// |
| 23 | 23 |
/// This example shows how the argument parser can be used. |
| 24 | 24 |
/// |
| 25 | 25 |
/// \include arg_parser_demo.cc |
| 26 | 26 |
|
| 27 | 27 |
#include <lemon/arg_parser.h> |
| 28 | 28 |
|
| 29 | 29 |
using namespace lemon; |
| ... | ... |
@@ -48,49 +48,49 @@ |
| 48 | 48 |
// Add a string option |
| 49 | 49 |
ap.refOption("name", "A string input.", s);
|
| 50 | 50 |
// Add bool options |
| 51 | 51 |
ap.refOption("f", "A switch.", b)
|
| 52 | 52 |
.refOption("nohelp", "", nh)
|
| 53 | 53 |
.refOption("gra", "Choice A", g1)
|
| 54 | 54 |
.refOption("grb", "Choice B", g2)
|
| 55 | 55 |
.refOption("grc", "Choice C", g3);
|
| 56 | 56 |
// Bundle -gr* options into a group |
| 57 | 57 |
ap.optionGroup("gr", "gra")
|
| 58 | 58 |
.optionGroup("gr", "grb")
|
| 59 | 59 |
.optionGroup("gr", "grc");
|
| 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; |
| 85 | 85 |
if(ap.given("val")) std::cout << " Value of -val: " << d << std::endl;
|
| 86 | 86 |
if(ap.given("val2")) {
|
| 87 | 87 |
d = ap["val2"]; |
| 88 | 88 |
std::cout << " Value of -val2: " << d << std::endl; |
| 89 | 89 |
} |
| 90 | 90 |
if(ap.given("name")) std::cout << " Value of -name: " << s << std::endl;
|
| 91 | 91 |
if(ap.given("f")) std::cout << " -f is given\n";
|
| 92 | 92 |
if(ap.given("nohelp")) std::cout << " Value of -nohelp: " << nh << std::endl;
|
| 93 | 93 |
if(ap.given("gra")) std::cout << " -gra is given\n";
|
| 94 | 94 |
if(ap.given("grb")) std::cout << " -grb is given\n";
|
| 95 | 95 |
if(ap.given("grc")) std::cout << " -grc is given\n";
|
| 96 | 96 |
| 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 |
namespace lemon {
|
| 20 | 20 |
|
| 21 | 21 |
/** |
| 22 | 22 |
@defgroup datas Data Structures |
| 23 | 23 |
This group contains the several data structures implemented in LEMON. |
| 24 | 24 |
*/ |
| 25 | 25 |
|
| 26 | 26 |
/** |
| 27 | 27 |
@defgroup graphs Graph Structures |
| 28 | 28 |
@ingroup datas |
| 29 | 29 |
\brief Graph structures implemented in LEMON. |
| 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>. |
| 51 | 51 |
|
| 52 | 52 |
If you are interested in starting to use the library, see the <a class="el" |
| 53 | 53 |
href="http://lemon.cs.elte.hu/trac/lemon/wiki/InstallGuide/">Installation |
| 54 | 54 |
Guide</a>. |
| 55 | 55 |
|
| 56 | 56 |
If you know what you are looking for, then try to find it under the |
| 57 | 57 |
<a class="el" href="modules.html">Modules</a> section. |
| 58 | 58 |
|
| 59 | 59 |
If you are a user of the old (0.x) series of LEMON, please check out the |
| 60 | 60 |
\ref migration "Migration Guide" for the backward incompatibilities. |
| 61 | 61 |
*/ |
| 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 |
namespace lemon {
|
| 20 | 20 |
|
| 21 | 21 |
/** |
| 22 | 22 |
\page min_cost_flow Minimum Cost Flow Problem |
| 23 | 23 |
|
| 24 | 24 |
\section mcf_def Definition (GEQ form) |
| 25 | 25 |
|
| 26 | 26 |
The \e minimum \e cost \e flow \e problem is to find a feasible flow of |
| 27 | 27 |
minimum total cost from a set of supply nodes to a set of demand nodes |
| 28 | 28 |
in a network with capacity constraints (lower and upper bounds) |
| 29 | 29 |
and arc costs \ref amo93networkflows. |
| ... | ... |
@@ -60,87 +60,87 @@ |
| 60 | 60 |
\section mcf_algs Algorithms |
| 61 | 61 |
|
| 62 | 62 |
LEMON contains several algorithms for solving this problem, for more |
| 63 | 63 |
information see \ref min_cost_flow_algs "Minimum Cost Flow Algorithms". |
| 64 | 64 |
|
| 65 | 65 |
A feasible solution for this problem can be found using \ref Circulation. |
| 66 | 66 |
|
| 67 | 67 |
|
| 68 | 68 |
\section mcf_dual Dual Solution |
| 69 | 69 |
|
| 70 | 70 |
The dual solution of the minimum cost flow problem is represented by |
| 71 | 71 |
node potentials \f$\pi: V\rightarrow\mathbf{R}\f$.
|
| 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 |
| 97 | 97 |
equalities are required in the supply/demand contraints. |
| 98 | 98 |
|
| 99 | 99 |
\f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f]
|
| 100 | 100 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) =
|
| 101 | 101 |
sup(u) \quad \forall u\in V \f] |
| 102 | 102 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f] |
| 103 | 103 |
|
| 104 | 104 |
However if the sum of the supply values is zero, then these two problems |
| 105 | 105 |
are equivalent. |
| 106 | 106 |
The \ref min_cost_flow_algs "algorithms" in LEMON support the general |
| 107 | 107 |
form, so if you need the equality form, you have to ensure this additional |
| 108 | 108 |
contraint manually. |
| 109 | 109 |
|
| 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. |
| 135 | 135 |
|
| 136 | 136 |
Note that the optimality conditions for this supply constraint type are |
| 137 | 137 |
slightly differ from the conditions that are discussed for the GEQ form, |
| 138 | 138 |
namely the potentials have to be non-negative instead of non-positive. |
| 139 | 139 |
An \f$f: A\rightarrow\mathbf{R}\f$ feasible solution of this problem
|
| 140 | 140 |
is optimal if and only if for some \f$\pi: V\rightarrow\mathbf{R}\f$
|
| 141 | 141 |
node potentials the following conditions hold. |
| 142 | 142 |
|
| 143 | 143 |
- For all \f$uv\in A\f$ arcs: |
| 144 | 144 |
- if \f$cost^\pi(uv)>0\f$, then \f$f(uv)=lower(uv)\f$; |
| 145 | 145 |
- if \f$lower(uv)<f(uv)<upper(uv)\f$, then \f$cost^\pi(uv)=0\f$; |
| 146 | 146 |
- if \f$cost^\pi(uv)<0\f$, then \f$f(uv)=upper(uv)\f$. |
| 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_ADAPTORS_H |
| 20 | 20 |
#define LEMON_ADAPTORS_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup graph_adaptors |
| 23 | 23 |
/// \file |
| 24 | 24 |
/// \brief Adaptor classes for digraphs and graphs |
| 25 | 25 |
/// |
| 26 | 26 |
/// This file contains several useful adaptors for digraphs and graphs. |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/maps.h> |
| ... | ... |
@@ -400,49 +400,49 @@ |
| 400 | 400 |
template<typename DGR> |
| 401 | 401 |
ReverseDigraph<const DGR> reverseDigraph(const DGR& digraph) {
|
| 402 | 402 |
return ReverseDigraph<const DGR>(digraph); |
| 403 | 403 |
} |
| 404 | 404 |
|
| 405 | 405 |
|
| 406 | 406 |
template <typename DGR, typename NF, typename AF, bool ch = true> |
| 407 | 407 |
class SubDigraphBase : public DigraphAdaptorBase<DGR> {
|
| 408 | 408 |
typedef DigraphAdaptorBase<DGR> Parent; |
| 409 | 409 |
public: |
| 410 | 410 |
typedef DGR Digraph; |
| 411 | 411 |
typedef NF NodeFilterMap; |
| 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 |
|
| 437 | 437 |
void first(Arc& i) const {
|
| 438 | 438 |
Parent::first(i); |
| 439 | 439 |
while (i != INVALID && (!(*_arc_filter)[i] |
| 440 | 440 |
|| !(*_node_filter)[Parent::source(i)] |
| 441 | 441 |
|| !(*_node_filter)[Parent::target(i)])) |
| 442 | 442 |
Parent::next(i); |
| 443 | 443 |
} |
| 444 | 444 |
|
| 445 | 445 |
void firstIn(Arc& i, const Node& n) const {
|
| 446 | 446 |
Parent::firstIn(i, n); |
| 447 | 447 |
while (i != INVALID && (!(*_arc_filter)[i] |
| 448 | 448 |
|| !(*_node_filter)[Parent::source(i)])) |
| ... | ... |
@@ -487,123 +487,123 @@ |
| 487 | 487 |
void status(const Arc& a, bool v) const { _arc_filter->set(a, v); }
|
| 488 | 488 |
|
| 489 | 489 |
bool status(const Node& n) const { return (*_node_filter)[n]; }
|
| 490 | 490 |
bool status(const Arc& a) const { return (*_arc_filter)[a]; }
|
| 491 | 491 |
|
| 492 | 492 |
typedef False NodeNumTag; |
| 493 | 493 |
typedef False ArcNumTag; |
| 494 | 494 |
|
| 495 | 495 |
typedef FindArcTagIndicator<DGR> FindArcTag; |
| 496 | 496 |
Arc findArc(const Node& source, const Node& target, |
| 497 | 497 |
const Arc& prev = INVALID) const {
|
| 498 | 498 |
if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
|
| 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: |
| 553 | 553 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 554 | 554 |
return operator=<ArcMap>(cmap); |
| 555 | 555 |
} |
| 556 | 556 |
|
| 557 | 557 |
template <typename CMap> |
| 558 | 558 |
ArcMap& operator=(const CMap& cmap) {
|
| 559 | 559 |
Parent::operator=(cmap); |
| 560 | 560 |
return *this; |
| 561 | 561 |
} |
| 562 | 562 |
}; |
| 563 | 563 |
|
| 564 | 564 |
}; |
| 565 | 565 |
|
| 566 | 566 |
template <typename DGR, typename NF, typename AF> |
| 567 | 567 |
class SubDigraphBase<DGR, NF, AF, false> |
| 568 | 568 |
: public DigraphAdaptorBase<DGR> {
|
| 569 | 569 |
typedef DigraphAdaptorBase<DGR> Parent; |
| 570 | 570 |
public: |
| 571 | 571 |
typedef DGR Digraph; |
| 572 | 572 |
typedef NF NodeFilterMap; |
| 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 |
|
| 598 | 598 |
void first(Arc& i) const {
|
| 599 | 599 |
Parent::first(i); |
| 600 | 600 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); |
| 601 | 601 |
} |
| 602 | 602 |
|
| 603 | 603 |
void firstIn(Arc& i, const Node& n) const {
|
| 604 | 604 |
Parent::firstIn(i, n); |
| 605 | 605 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); |
| 606 | 606 |
} |
| 607 | 607 |
|
| 608 | 608 |
void firstOut(Arc& i, const Node& n) const {
|
| 609 | 609 |
Parent::firstOut(i, n); |
| ... | ... |
@@ -630,76 +630,76 @@ |
| 630 | 630 |
|
| 631 | 631 |
void status(const Node& n, bool v) const { _node_filter->set(n, v); }
|
| 632 | 632 |
void status(const Arc& a, bool v) const { _arc_filter->set(a, v); }
|
| 633 | 633 |
|
| 634 | 634 |
bool status(const Node& n) const { return (*_node_filter)[n]; }
|
| 635 | 635 |
bool status(const Arc& a) const { return (*_arc_filter)[a]; }
|
| 636 | 636 |
|
| 637 | 637 |
typedef False NodeNumTag; |
| 638 | 638 |
typedef False ArcNumTag; |
| 639 | 639 |
|
| 640 | 640 |
typedef FindArcTagIndicator<DGR> FindArcTag; |
| 641 | 641 |
Arc findArc(const Node& source, const Node& target, |
| 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) {}
|
| 694 | 694 |
|
| 695 | 695 |
private: |
| 696 | 696 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 697 | 697 |
return operator=<ArcMap>(cmap); |
| 698 | 698 |
} |
| 699 | 699 |
|
| 700 | 700 |
template <typename CMap> |
| 701 | 701 |
ArcMap& operator=(const CMap& cmap) {
|
| 702 | 702 |
Parent::operator=(cmap); |
| 703 | 703 |
return *this; |
| 704 | 704 |
} |
| 705 | 705 |
}; |
| ... | ... |
@@ -1000,146 +1000,146 @@ |
| 1000 | 1000 |
if (!(*_node_filter)[u] || !(*_node_filter)[v]) {
|
| 1001 | 1001 |
return INVALID; |
| 1002 | 1002 |
} |
| 1003 | 1003 |
Arc arc = Parent::findArc(u, v, prev); |
| 1004 | 1004 |
while (arc != INVALID && !(*_edge_filter)[arc]) {
|
| 1005 | 1005 |
arc = Parent::findArc(u, v, arc); |
| 1006 | 1006 |
} |
| 1007 | 1007 |
return arc; |
| 1008 | 1008 |
} |
| 1009 | 1009 |
|
| 1010 | 1010 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
| 1011 | 1011 |
Edge findEdge(const Node& u, const Node& v, |
| 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: |
| 1094 | 1094 |
EdgeMap& operator=(const EdgeMap& cmap) {
|
| 1095 | 1095 |
return operator=<EdgeMap>(cmap); |
| 1096 | 1096 |
} |
| 1097 | 1097 |
|
| 1098 | 1098 |
template <typename CMap> |
| 1099 | 1099 |
EdgeMap& operator=(const CMap& cmap) {
|
| 1100 | 1100 |
Parent::operator=(cmap); |
| 1101 | 1101 |
return *this; |
| 1102 | 1102 |
} |
| 1103 | 1103 |
}; |
| 1104 | 1104 |
|
| 1105 | 1105 |
}; |
| 1106 | 1106 |
|
| 1107 | 1107 |
template <typename GR, typename NF, typename EF> |
| 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; |
| 1134 | 1134 |
|
| 1135 | 1135 |
void first(Node& i) const {
|
| 1136 | 1136 |
Parent::first(i); |
| 1137 | 1137 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
| 1138 | 1138 |
} |
| 1139 | 1139 |
|
| 1140 | 1140 |
void first(Arc& i) const {
|
| 1141 | 1141 |
Parent::first(i); |
| 1142 | 1142 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
| 1143 | 1143 |
} |
| 1144 | 1144 |
|
| 1145 | 1145 |
void first(Edge& i) const {
|
| ... | ... |
@@ -1198,107 +1198,107 @@ |
| 1198 | 1198 |
typedef False ArcNumTag; |
| 1199 | 1199 |
typedef False EdgeNumTag; |
| 1200 | 1200 |
|
| 1201 | 1201 |
typedef FindArcTagIndicator<Graph> FindArcTag; |
| 1202 | 1202 |
Arc findArc(const Node& u, const Node& v, |
| 1203 | 1203 |
const Arc& prev = INVALID) const {
|
| 1204 | 1204 |
Arc arc = Parent::findArc(u, v, prev); |
| 1205 | 1205 |
while (arc != INVALID && !(*_edge_filter)[arc]) {
|
| 1206 | 1206 |
arc = Parent::findArc(u, v, arc); |
| 1207 | 1207 |
} |
| 1208 | 1208 |
return arc; |
| 1209 | 1209 |
} |
| 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) {
|
| 1293 | 1293 |
return operator=<EdgeMap>(cmap); |
| 1294 | 1294 |
} |
| 1295 | 1295 |
|
| 1296 | 1296 |
template <typename CMap> |
| 1297 | 1297 |
EdgeMap& operator=(const CMap& cmap) {
|
| 1298 | 1298 |
Parent::operator=(cmap); |
| 1299 | 1299 |
return *this; |
| 1300 | 1300 |
} |
| 1301 | 1301 |
}; |
| 1302 | 1302 |
|
| 1303 | 1303 |
}; |
| 1304 | 1304 |
|
| ... | ... |
@@ -1483,112 +1483,112 @@ |
| 1483 | 1483 |
/// \tparam GR The type of the adapted digraph or graph. |
| 1484 | 1484 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept |
| 1485 | 1485 |
/// or the \ref concepts::Graph "Graph" concept. |
| 1486 | 1486 |
/// It can also be specified to be \c const. |
| 1487 | 1487 |
/// \tparam NF The type of the node filter map. |
| 1488 | 1488 |
/// It must be a \c bool (or convertible) node map of the |
| 1489 | 1489 |
/// adapted (di)graph. The default type is |
| 1490 | 1490 |
/// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>". |
| 1491 | 1491 |
/// |
| 1492 | 1492 |
/// \note The \c Node and <tt>Arc/Edge</tt> types of this adaptor and the |
| 1493 | 1493 |
/// adapted (di)graph are convertible to each other. |
| 1494 | 1494 |
#ifdef DOXYGEN |
| 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 |
|
| 1541 | 1541 |
/// \brief Returns the status of the given node |
| 1542 | 1542 |
/// |
| 1543 | 1543 |
/// This function returns the status of the given node. |
| 1544 | 1544 |
/// It is \c true if the given node is enabled (i.e. not hidden). |
| 1545 | 1545 |
bool status(const Node& n) const { return Parent::status(n); }
|
| 1546 | 1546 |
|
| 1547 | 1547 |
/// \brief Disables the given node |
| 1548 | 1548 |
/// |
| 1549 | 1549 |
/// This function disables the given node, so the iteration |
| 1550 | 1550 |
/// jumps over it. |
| 1551 | 1551 |
/// It is the same as \ref status() "status(n, false)". |
| 1552 | 1552 |
void disable(const Node& n) const { Parent::status(n, false); }
|
| 1553 | 1553 |
|
| 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 |
|
| 1583 | 1583 |
FilterNodes() : const_true_map() {}
|
| 1584 | 1584 |
|
| 1585 | 1585 |
public: |
| 1586 | 1586 |
|
| 1587 | 1587 |
FilterNodes(GR& graph, NodeFilterMap& node_filter) : |
| 1588 | 1588 |
Parent(), const_true_map() {
|
| 1589 | 1589 |
Parent::initialize(graph, node_filter, const_true_map); |
| 1590 | 1590 |
} |
| 1591 | 1591 |
|
| 1592 | 1592 |
void status(const Node& n, bool v) const { Parent::status(n, v); }
|
| 1593 | 1593 |
bool status(const Node& n) const { return Parent::status(n); }
|
| 1594 | 1594 |
void disable(const Node& n) const { Parent::status(n, false); }
|
| ... | ... |
@@ -1632,49 +1632,49 @@ |
| 1632 | 1632 |
/// |
| 1633 | 1633 |
/// \tparam DGR The type of the adapted digraph. |
| 1634 | 1634 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
| 1635 | 1635 |
/// It can also be specified to be \c const. |
| 1636 | 1636 |
/// \tparam AF The type of the arc filter map. |
| 1637 | 1637 |
/// It must be a \c bool (or convertible) arc map of the |
| 1638 | 1638 |
/// adapted digraph. The default type is |
| 1639 | 1639 |
/// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>". |
| 1640 | 1640 |
/// |
| 1641 | 1641 |
/// \note The \c Node and \c Arc types of this adaptor and the adapted |
| 1642 | 1642 |
/// digraph are convertible to each other. |
| 1643 | 1643 |
#ifdef DOXYGEN |
| 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: |
| 1669 | 1669 |
ConstMap<typename DGR::Node, Const<bool, true> > const_true_map; |
| 1670 | 1670 |
|
| 1671 | 1671 |
FilterArcs() : const_true_map() {}
|
| 1672 | 1672 |
|
| 1673 | 1673 |
public: |
| 1674 | 1674 |
|
| 1675 | 1675 |
/// \brief Constructor |
| 1676 | 1676 |
/// |
| 1677 | 1677 |
/// Creates a subdigraph for the given digraph with the given arc |
| 1678 | 1678 |
/// filter map. |
| 1679 | 1679 |
FilterArcs(DGR& digraph, ArcFilterMap& arc_filter) |
| 1680 | 1680 |
: Parent(), const_true_map() {
|
| ... | ... |
@@ -1740,78 +1740,78 @@ |
| 1740 | 1740 |
/// by adding or removing nodes or edges, unless the \c GR template |
| 1741 | 1741 |
/// parameter is set to be \c const. |
| 1742 | 1742 |
/// |
| 1743 | 1743 |
/// This class provides only linear time counting for nodes, edges and arcs. |
| 1744 | 1744 |
/// |
| 1745 | 1745 |
/// \tparam GR The type of the adapted graph. |
| 1746 | 1746 |
/// It must conform to the \ref concepts::Graph "Graph" concept. |
| 1747 | 1747 |
/// It can also be specified to be \c const. |
| 1748 | 1748 |
/// \tparam EF The type of the edge filter map. |
| 1749 | 1749 |
/// It must be a \c bool (or convertible) edge map of the |
| 1750 | 1750 |
/// adapted graph. The default type is |
| 1751 | 1751 |
/// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
| 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 |
| 1806 | 1806 |
/// |
| 1807 | 1807 |
/// This function returns the status of the given edge. |
| 1808 | 1808 |
/// It is \c true if the given edge is enabled (i.e. not hidden). |
| 1809 | 1809 |
bool status(const Edge& e) const { return Parent::status(e); }
|
| 1810 | 1810 |
|
| 1811 | 1811 |
/// \brief Disables the given edge |
| 1812 | 1812 |
/// |
| 1813 | 1813 |
/// This function disables the given edge in the subgraph, |
| 1814 | 1814 |
/// so the iteration jumps over it. |
| 1815 | 1815 |
/// It is the same as \ref status() "status(e, false)". |
| 1816 | 1816 |
void disable(const Edge& e) const { Parent::status(e, false); }
|
| 1817 | 1817 |
|
| ... | ... |
@@ -1837,49 +1837,49 @@ |
| 1837 | 1837 |
template<typename GR, typename EF> |
| 1838 | 1838 |
FilterEdges<const GR, const EF> |
| 1839 | 1839 |
filterEdges(const GR& graph, const EF& edge_filter) {
|
| 1840 | 1840 |
return FilterEdges<const GR, const EF>(graph, edge_filter); |
| 1841 | 1841 |
} |
| 1842 | 1842 |
|
| 1843 | 1843 |
|
| 1844 | 1844 |
template <typename DGR> |
| 1845 | 1845 |
class UndirectorBase {
|
| 1846 | 1846 |
public: |
| 1847 | 1847 |
typedef DGR Digraph; |
| 1848 | 1848 |
typedef UndirectorBase Adaptor; |
| 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 |
} |
| 1874 | 1874 |
bool operator!=(const Arc &other) const {
|
| 1875 | 1875 |
return _forward != other._forward || _edge != other._edge; |
| 1876 | 1876 |
} |
| 1877 | 1877 |
bool operator<(const Arc &other) const {
|
| 1878 | 1878 |
return _forward < other._forward || |
| 1879 | 1879 |
(_forward == other._forward && _edge < other._edge); |
| 1880 | 1880 |
} |
| 1881 | 1881 |
}; |
| 1882 | 1882 |
|
| 1883 | 1883 |
void first(Node& n) const {
|
| 1884 | 1884 |
_digraph->first(n); |
| 1885 | 1885 |
} |
| ... | ... |
@@ -2077,49 +2077,49 @@ |
| 2077 | 2077 |
|
| 2078 | 2078 |
private: |
| 2079 | 2079 |
|
| 2080 | 2080 |
template <typename V> |
| 2081 | 2081 |
class ArcMapBase {
|
| 2082 | 2082 |
private: |
| 2083 | 2083 |
|
| 2084 | 2084 |
typedef typename DGR::template ArcMap<V> MapImpl; |
| 2085 | 2085 |
|
| 2086 | 2086 |
public: |
| 2087 | 2087 |
|
| 2088 | 2088 |
typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag; |
| 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)) {
|
| 2114 | 2114 |
return _forward[a]; |
| 2115 | 2115 |
} else {
|
| 2116 | 2116 |
return _backward[a]; |
| 2117 | 2117 |
} |
| 2118 | 2118 |
} |
| 2119 | 2119 |
|
| 2120 | 2120 |
ReturnValue operator[](const Arc& a) {
|
| 2121 | 2121 |
if (direction(a)) {
|
| 2122 | 2122 |
return _forward[a]; |
| 2123 | 2123 |
} else {
|
| 2124 | 2124 |
return _backward[a]; |
| 2125 | 2125 |
} |
| ... | ... |
@@ -2195,49 +2195,49 @@ |
| 2195 | 2195 |
explicit EdgeMap(const UndirectorBase<DGR>& adaptor) |
| 2196 | 2196 |
: Parent(*adaptor._digraph) {}
|
| 2197 | 2197 |
|
| 2198 | 2198 |
EdgeMap(const UndirectorBase<DGR>& adaptor, const V& value) |
| 2199 | 2199 |
: Parent(*adaptor._digraph, value) {}
|
| 2200 | 2200 |
|
| 2201 | 2201 |
private: |
| 2202 | 2202 |
EdgeMap& operator=(const EdgeMap& cmap) {
|
| 2203 | 2203 |
return operator=<EdgeMap>(cmap); |
| 2204 | 2204 |
} |
| 2205 | 2205 |
|
| 2206 | 2206 |
template <typename CMap> |
| 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 |
} |
| 2232 | 2232 |
|
| 2233 | 2233 |
}; |
| 2234 | 2234 |
|
| 2235 | 2235 |
/// \ingroup graph_adaptors |
| 2236 | 2236 |
/// |
| 2237 | 2237 |
/// \brief Adaptor class for viewing a digraph as an undirected graph. |
| 2238 | 2238 |
/// |
| 2239 | 2239 |
/// Undirector adaptor can be used for viewing a digraph as an undirected |
| 2240 | 2240 |
/// graph. All arcs of the underlying digraph are showed in the |
| 2241 | 2241 |
/// adaptor as an edge (and also as a pair of arcs, of course). |
| 2242 | 2242 |
/// This adaptor conforms to the \ref concepts::Graph "Graph" concept. |
| 2243 | 2243 |
/// |
| ... | ... |
@@ -2707,49 +2707,49 @@ |
| 2707 | 2707 |
/// the capacities in the flow problem. It is implicitly \c const. |
| 2708 | 2708 |
/// The default type is |
| 2709 | 2709 |
/// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 2710 | 2710 |
/// \tparam FM The type of the flow map. |
| 2711 | 2711 |
/// It must be an arc map of some numerical type, which defines |
| 2712 | 2712 |
/// the flow values in the flow problem. The default type is \c CM. |
| 2713 | 2713 |
/// \tparam TL The tolerance type for handling inexact computation. |
| 2714 | 2714 |
/// The default tolerance type depends on the value type of the |
| 2715 | 2715 |
/// capacity map. |
| 2716 | 2716 |
/// |
| 2717 | 2717 |
/// \note This adaptor is implemented using Undirector and FilterArcs |
| 2718 | 2718 |
/// adaptors. |
| 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; |
| 2744 | 2744 |
/// The type of the capacity map. |
| 2745 | 2745 |
typedef CM CapacityMap; |
| 2746 | 2746 |
/// The type of the flow map. |
| 2747 | 2747 |
typedef FM FlowMap; |
| 2748 | 2748 |
/// The tolerance type. |
| 2749 | 2749 |
typedef TL Tolerance; |
| 2750 | 2750 |
|
| 2751 | 2751 |
typedef typename CapacityMap::Value Value; |
| 2752 | 2752 |
typedef ResidualDigraph Adaptor; |
| 2753 | 2753 |
|
| 2754 | 2754 |
protected: |
| 2755 | 2755 |
|
| ... | ... |
@@ -2764,49 +2764,49 @@ |
| 2764 | 2764 |
FM, TL> BackwardFilter; |
| 2765 | 2765 |
|
| 2766 | 2766 |
typedef typename Undirected:: |
| 2767 | 2767 |
template CombinedArcMap<ForwardFilter, BackwardFilter> ArcFilter; |
| 2768 | 2768 |
|
| 2769 | 2769 |
typedef SubDigraph<Undirected, NodeFilter, ArcFilter> Parent; |
| 2770 | 2770 |
|
| 2771 | 2771 |
const CapacityMap* _capacity; |
| 2772 | 2772 |
FlowMap* _flow; |
| 2773 | 2773 |
|
| 2774 | 2774 |
Undirected _graph; |
| 2775 | 2775 |
NodeFilter _node_filter; |
| 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 |
/// |
| 2801 | 2801 |
/// Returns the residual capacity of the given arc. |
| 2802 | 2802 |
Value residualCapacity(const Arc& a) const {
|
| 2803 | 2803 |
if (Undirected::direction(a)) {
|
| 2804 | 2804 |
return (*_capacity)[a] - (*_flow)[a]; |
| 2805 | 2805 |
} else {
|
| 2806 | 2806 |
return (*_flow)[a]; |
| 2807 | 2807 |
} |
| 2808 | 2808 |
} |
| 2809 | 2809 |
|
| 2810 | 2810 |
/// \brief Augments on the given arc in the residual digraph. |
| 2811 | 2811 |
/// |
| 2812 | 2812 |
/// Augments on the given arc in the residual digraph. It increases |
| ... | ... |
@@ -2846,49 +2846,49 @@ |
| 2846 | 2846 |
|
| 2847 | 2847 |
/// \brief Returns the backward oriented residual arc. |
| 2848 | 2848 |
/// |
| 2849 | 2849 |
/// Returns the backward oriented residual arc related to the given |
| 2850 | 2850 |
/// arc of the underlying digraph. |
| 2851 | 2851 |
static Arc backward(const typename Digraph::Arc& a) {
|
| 2852 | 2852 |
return Undirected::direct(a, false); |
| 2853 | 2853 |
} |
| 2854 | 2854 |
|
| 2855 | 2855 |
/// \brief Residual capacity map. |
| 2856 | 2856 |
/// |
| 2857 | 2857 |
/// This map adaptor class can be used for obtaining the residual |
| 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. |
| 2883 | 2883 |
ResidualCapacity residualCapacity() const {
|
| 2884 | 2884 |
return ResidualCapacity(*this); |
| 2885 | 2885 |
} |
| 2886 | 2886 |
|
| 2887 | 2887 |
}; |
| 2888 | 2888 |
|
| 2889 | 2889 |
/// \brief Returns a (read-only) Residual adaptor |
| 2890 | 2890 |
/// |
| 2891 | 2891 |
/// This function just returns a (read-only) \ref ResidualDigraph adaptor. |
| 2892 | 2892 |
/// \ingroup graph_adaptors |
| 2893 | 2893 |
/// \relates ResidualDigraph |
| 2894 | 2894 |
template<typename DGR, typename CM, typename FM> |
| ... | ... |
@@ -3426,49 +3426,49 @@ |
| 3426 | 3426 |
|
| 3427 | 3427 |
/// \brief Returns the bind arc that corresponds to the given |
| 3428 | 3428 |
/// original node. |
| 3429 | 3429 |
/// |
| 3430 | 3430 |
/// Returns the bind arc in the adaptor that corresponds to the given |
| 3431 | 3431 |
/// original node, i.e. the arc connecting the in-node and out-node |
| 3432 | 3432 |
/// of \c n. |
| 3433 | 3433 |
static Arc arc(const DigraphNode& n) {
|
| 3434 | 3434 |
return Parent::arc(n); |
| 3435 | 3435 |
} |
| 3436 | 3436 |
|
| 3437 | 3437 |
/// \brief Returns the arc that corresponds to the given original arc. |
| 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; |
| 3463 | 3463 |
typedef typename MapTraits<IN>::ConstReturnValue ConstReturnValue; |
| 3464 | 3464 |
typedef typename MapTraits<IN>::ReturnValue Reference; |
| 3465 | 3465 |
typedef typename MapTraits<IN>::ConstReturnValue ConstReference; |
| 3466 | 3466 |
|
| 3467 | 3467 |
/// Constructor |
| 3468 | 3468 |
CombinedNodeMap(IN& in_map, OUT& out_map) |
| 3469 | 3469 |
: _in_map(in_map), _out_map(out_map) {}
|
| 3470 | 3470 |
|
| 3471 | 3471 |
/// Returns the value associated with the given key. |
| 3472 | 3472 |
Value operator[](const Key& key) const {
|
| 3473 | 3473 |
if (SplitNodesBase<const DGR>::inNode(key)) {
|
| 3474 | 3474 |
return _in_map[key]; |
| 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");
|
| 43 | 43 |
} |
| 44 | 44 |
|
| 45 | 45 |
ArgParser::~ArgParser() |
| 46 | 46 |
{
|
| 47 | 47 |
for(Opts::iterator i=_opts.begin();i!=_opts.end();++i) |
| 48 | 48 |
if(i->second.self_delete) |
| 49 | 49 |
switch(i->second.type) {
|
| 50 | 50 |
case BOOL: |
| 51 | 51 |
delete i->second.bool_p; |
| 52 | 52 |
break; |
| 53 | 53 |
case STRING: |
| 54 | 54 |
delete i->second.string_p; |
| 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_ARG_PARSER_H |
| 20 | 20 |
#define LEMON_ARG_PARSER_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <map> |
| 24 | 24 |
#include <list> |
| 25 | 25 |
#include <string> |
| 26 | 26 |
#include <iostream> |
| 27 | 27 |
#include <sstream> |
| 28 | 28 |
#include <algorithm> |
| 29 | 29 |
#include <lemon/assert.h> |
| 30 | 30 |
|
| 31 | 31 |
///\ingroup misc |
| 32 | 32 |
///\file |
| 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; |
| 61 | 61 |
case UNKNOWN_OPT: |
| 62 | 62 |
return "lemon::ArgParseException: unknown option"; |
| 63 | 63 |
break; |
| 64 | 64 |
case INVALID_OPT: |
| 65 | 65 |
return "lemon::ArgParseException: invalid combination of options"; |
| 66 | 66 |
break; |
| 67 | 67 |
} |
| 68 | 68 |
return ""; |
| 69 | 69 |
} |
| 70 | 70 |
///Return the reason for the failure |
| 71 | 71 |
Reason reason() const {return _reason; }
|
| 72 | 72 |
}; |
| ... | ... |
@@ -120,63 +120,63 @@ |
| 120 | 120 |
{
|
| 121 | 121 |
public: |
| 122 | 122 |
typedef std::list<std::string> Opts; |
| 123 | 123 |
Opts opts; |
| 124 | 124 |
bool only_one; |
| 125 | 125 |
bool mandatory; |
| 126 | 126 |
GroupData() :only_one(false), mandatory(false) {}
|
| 127 | 127 |
}; |
| 128 | 128 |
|
| 129 | 129 |
typedef std::map<std::string,GroupData> Groups; |
| 130 | 130 |
Groups _groups; |
| 131 | 131 |
|
| 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 |
|
| 171 | 171 |
///@{
|
| 172 | 172 |
|
| 173 | 173 |
///Add a new integer type option |
| 174 | 174 |
|
| 175 | 175 |
///Add a new integer type option. |
| 176 | 176 |
///\param name The name of the option. The leading '-' must be omitted. |
| 177 | 177 |
///\param help A help string. |
| 178 | 178 |
///\param value A default value for the option. |
| 179 | 179 |
///\param obl Indicate if the option is mandatory. |
| 180 | 180 |
ArgParser &intOption(const std::string &name, |
| 181 | 181 |
const std::string &help, |
| 182 | 182 |
int value=0, bool obl=false); |
| ... | ... |
@@ -402,32 +402,32 @@ |
| 402 | 402 |
std::string()+"Unkown option: '"+_name+"'"); |
| 403 | 403 |
LEMON_ASSERT(i->second.type==ArgParser::INTEGER, |
| 404 | 404 |
std::string()+"'"+_name+"' is an integer option"); |
| 405 | 405 |
return *(i->second.int_p); |
| 406 | 406 |
} |
| 407 | 407 |
|
| 408 | 408 |
}; |
| 409 | 409 |
|
| 410 | 410 |
///Give back the value of an option |
| 411 | 411 |
|
| 412 | 412 |
///Give back the value of an option. |
| 413 | 413 |
///\sa RefType |
| 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 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BELLMAN_FORD_H |
| 20 | 20 |
#define LEMON_BELLMAN_FORD_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup shortest_path |
| 23 | 23 |
/// \file |
| 24 | 24 |
/// \brief Bellman-Ford algorithm. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/list_graph.h> |
| 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 |
} |
| 61 | 61 |
/// \brief Gives back the sum of the given two elements. |
| 62 | 62 |
static Value plus(const Value& left, const Value& right) {
|
| 63 | 63 |
return left + right; |
| 64 | 64 |
} |
| 65 | 65 |
/// \brief Gives back \c true only if the first value is less than |
| 66 | 66 |
/// the second. |
| 67 | 67 |
static bool less(const Value& left, const Value& right) {
|
| 68 | 68 |
return left < right; |
| 69 | 69 |
} |
| 70 | 70 |
}; |
| 71 | 71 |
|
| 72 | 72 |
template <typename V> |
| 73 | 73 |
struct BellmanFordDefaultOperationTraits<V, false> {
|
| 74 | 74 |
typedef V Value; |
| 75 | 75 |
static Value zero() {
|
| 76 | 76 |
return static_cast<Value>(0); |
| 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. |
| 102 | 102 |
/// By default, it is the epsilon value used by \ref Tolerance |
| 103 | 103 |
/// "Tolerance<V>". |
| 104 | 104 |
/// |
| 105 | 105 |
/// \see BellmanFordDefaultOperationTraits |
| 106 | 106 |
#ifdef DOXYGEN |
| 107 | 107 |
template <typename V, V eps> |
| 108 | 108 |
#else |
| 109 | 109 |
template < |
| 110 | 110 |
typename V, |
| 111 | 111 |
V eps = Tolerance<V>::def_epsilon> |
| 112 | 112 |
#endif |
| 113 | 113 |
struct BellmanFordToleranceOperationTraits {
|
| ... | ... |
@@ -118,1048 +118,1048 @@ |
| 118 | 118 |
return static_cast<Value>(0); |
| 119 | 119 |
} |
| 120 | 120 |
/// \brief Gives back the positive infinity value of the type. |
| 121 | 121 |
static Value infinity() {
|
| 122 | 122 |
return std::numeric_limits<Value>::infinity(); |
| 123 | 123 |
} |
| 124 | 124 |
/// \brief Gives back the sum of the given two elements. |
| 125 | 125 |
static Value plus(const Value& left, const Value& right) {
|
| 126 | 126 |
return left + right; |
| 127 | 127 |
} |
| 128 | 128 |
/// \brief Gives back \c true only if the first value is less than |
| 129 | 129 |
/// the second. |
| 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>". |
| 223 | 223 |
/// \tparam TR The traits class that defines various types used by the |
| 224 | 224 |
/// algorithm. By default, it is \ref BellmanFordDefaultTraits |
| 225 | 225 |
/// "BellmanFordDefaultTraits<GR, LEN>". |
| 226 | 226 |
/// In most cases, this parameter should not be set directly, |
| 227 | 227 |
/// consider to use the named template parameters instead. |
| 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 |
| 253 | 253 |
/// "operation traits class" of the algorithm. |
| 254 | 254 |
typedef typename TR::OperationTraits OperationTraits; |
| 255 | 255 |
|
| 256 | 256 |
///The \ref BellmanFordDefaultTraits "traits class" of the algorithm. |
| 257 | 257 |
typedef TR Traits; |
| 258 | 258 |
|
| 259 | 259 |
private: |
| 260 | 260 |
|
| 261 | 261 |
typedef typename Digraph::Node Node; |
| 262 | 262 |
typedef typename Digraph::NodeIt NodeIt; |
| 263 | 263 |
typedef typename Digraph::Arc Arc; |
| 264 | 264 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 265 | 265 |
|
| 266 | 266 |
// Pointer to the underlying digraph. |
| 267 | 267 |
const Digraph *_gr; |
| 268 | 268 |
// Pointer to the length map |
| 269 | 269 |
const LengthMap *_length; |
| 270 | 270 |
// Pointer to the map of predecessors arcs. |
| 271 | 271 |
PredMap *_pred; |
| 272 | 272 |
// Indicates if _pred is locally allocated (true) or not. |
| 273 | 273 |
bool _local_pred; |
| 274 | 274 |
// Pointer to the map of distances. |
| 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) {
|
| 397 | 397 |
_length = ↦ |
| 398 | 398 |
return *this; |
| 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 |
| 672 | 672 |
/// and build the path manually. |
| 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 |
| 773 | 773 |
/// the given node. |
| 774 | 774 |
/// |
| 775 | 775 |
/// This function returns the 'previous arc' of the shortest path |
| 776 | 776 |
/// tree for node \c v, i.e. it returns the last arc of a |
| 777 | 777 |
/// shortest path from a root to \c v. It is \c INVALID if \c v |
| 778 | 778 |
/// is not reached from the root(s) or if \c v is a root. |
| 779 | 779 |
/// |
| 780 | 780 |
/// The shortest path tree used here is equal to the shortest path |
| 781 | 781 |
/// tree used in \ref predNode() and \ref predMap(). |
| 782 | 782 |
/// |
| 783 | 783 |
/// \pre Either \ref run() or \ref init() must be called before |
| 784 | 784 |
/// using this function. |
| 785 | 785 |
Arc predArc(Node v) const { return (*_pred)[v]; }
|
| 786 | 786 |
|
| 787 | 787 |
/// \brief Returns the 'previous node' of the shortest path tree for |
| 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]); |
| 848 | 848 |
for (Node u = _gr->source((*_pred)[v]); u != v; |
| 849 | 849 |
u = _gr->source((*_pred)[u])) {
|
| 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 |
| 1048 | 1048 |
/// this function you can retrieve the distance and the shortest path |
| 1049 | 1049 |
/// for a single target node easier. |
| 1050 | 1050 |
/// |
| 1051 | 1051 |
/// \return \c true if \c t is reachable form \c s. |
| 1052 | 1052 |
bool run(Node s, Node t) {
|
| 1053 | 1053 |
BellmanFord<Digraph,LengthMap,TR> |
| 1054 | 1054 |
bf(*reinterpret_cast<const Digraph*>(Base::_graph), |
| 1055 | 1055 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
| 1056 | 1056 |
if (Base::_pred) bf.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 1057 | 1057 |
if (Base::_dist) bf.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 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 |
|
| 1101 | 1101 |
template<class T> |
| 1102 | 1102 |
struct SetPathBase : public Base {
|
| 1103 | 1103 |
typedef T Path; |
| 1104 | 1104 |
SetPathBase(const TR &b) : TR(b) {}
|
| 1105 | 1105 |
}; |
| 1106 | 1106 |
|
| 1107 | 1107 |
/// \brief \ref named-func-param "Named parameter" for getting |
| 1108 | 1108 |
/// the shortest path to the target node. |
| 1109 | 1109 |
/// |
| 1110 | 1110 |
/// \ref named-func-param "Named parameter" for getting |
| 1111 | 1111 |
/// the shortest path to the target node. |
| 1112 | 1112 |
template<class T> |
| 1113 | 1113 |
BellmanFordWizard<SetPathBase<T> > path(const T &t) |
| 1114 | 1114 |
{
|
| 1115 | 1115 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1116 | 1116 |
return BellmanFordWizard<SetPathBase<T> >(*this); |
| 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 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BFS_H |
| 20 | 20 |
#define LEMON_BFS_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup search |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief BFS algorithm. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/list_graph.h> |
| 27 | 27 |
#include <lemon/bits/path_dump.h> |
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/error.h> |
| ... | ... |
@@ -61,49 +61,50 @@ |
| 61 | 61 |
|
| 62 | 62 |
///The type of the map that indicates which nodes are processed. |
| 63 | 63 |
|
| 64 | 64 |
///The type of the map that indicates which nodes are processed. |
| 65 | 65 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 66 | 66 |
///By default, it is a NullMap. |
| 67 | 67 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 68 | 68 |
///Instantiates a \c ProcessedMap. |
| 69 | 69 |
|
| 70 | 70 |
///This function instantiates a \ref ProcessedMap. |
| 71 | 71 |
///\param g is the digraph, to which |
| 72 | 72 |
///we would like to define the \ref ProcessedMap |
| 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. |
| 98 | 99 |
|
| 99 | 100 |
///The type of the map that stores the distances of the nodes. |
| 100 | 101 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 101 | 102 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 102 | 103 |
///Instantiates a \c DistMap. |
| 103 | 104 |
|
| 104 | 105 |
///This function instantiates a \ref DistMap. |
| 105 | 106 |
///\param g is the digraph, to which we would like to define the |
| 106 | 107 |
///\ref DistMap. |
| 107 | 108 |
static DistMap *createDistMap(const Digraph &g) |
| 108 | 109 |
{
|
| 109 | 110 |
return new DistMap(g); |
| ... | ... |
@@ -250,49 +251,50 @@ |
| 250 | 251 |
///\c DistMap type. |
| 251 | 252 |
/// |
| 252 | 253 |
///\ref named-templ-param "Named parameter" for setting |
| 253 | 254 |
///\c DistMap type. |
| 254 | 255 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 255 | 256 |
template <class T> |
| 256 | 257 |
struct SetDistMap : public Bfs< Digraph, SetDistMapTraits<T> > {
|
| 257 | 258 |
typedef Bfs< Digraph, SetDistMapTraits<T> > Create; |
| 258 | 259 |
}; |
| 259 | 260 |
|
| 260 | 261 |
template <class T> |
| 261 | 262 |
struct SetReachedMapTraits : public Traits {
|
| 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 |
| 287 | 289 |
} |
| 288 | 290 |
}; |
| 289 | 291 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 290 | 292 |
///\c ProcessedMap type. |
| 291 | 293 |
/// |
| 292 | 294 |
///\ref named-templ-param "Named parameter" for setting |
| 293 | 295 |
///\c ProcessedMap type. |
| 294 | 296 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 295 | 297 |
template <class T> |
| 296 | 298 |
struct SetProcessedMap : public Bfs< Digraph, SetProcessedMapTraits<T> > {
|
| 297 | 299 |
typedef Bfs< Digraph, SetProcessedMapTraits<T> > Create; |
| 298 | 300 |
}; |
| ... | ... |
@@ -851,49 +853,50 @@ |
| 851 | 853 |
|
| 852 | 854 |
///The type of the map that indicates which nodes are processed. |
| 853 | 855 |
|
| 854 | 856 |
///The type of the map that indicates which nodes are processed. |
| 855 | 857 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 856 | 858 |
///By default, it is a NullMap. |
| 857 | 859 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 858 | 860 |
///Instantiates a ProcessedMap. |
| 859 | 861 |
|
| 860 | 862 |
///This function instantiates a ProcessedMap. |
| 861 | 863 |
///\param g is the digraph, to which |
| 862 | 864 |
///we would like to define the ProcessedMap. |
| 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. |
| 888 | 891 |
|
| 889 | 892 |
///The type of the map that stores the distances of the nodes. |
| 890 | 893 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 891 | 894 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 892 | 895 |
///Instantiates a DistMap. |
| 893 | 896 |
|
| 894 | 897 |
///This function instantiates a DistMap. |
| 895 | 898 |
///\param g is the digraph, to which we would like to define |
| 896 | 899 |
///the DistMap |
| 897 | 900 |
static DistMap *createDistMap(const Digraph &g) |
| 898 | 901 |
{
|
| 899 | 902 |
return new DistMap(g); |
| ... | ... |
@@ -1244,49 +1247,50 @@ |
| 1244 | 1247 |
visitor.start(node); |
| 1245 | 1248 |
visitor.reach(node); |
| 1246 | 1249 |
visitor.process(node); |
| 1247 | 1250 |
visitor.discover(arc); |
| 1248 | 1251 |
visitor.examine(arc); |
| 1249 | 1252 |
} |
| 1250 | 1253 |
_Visitor& visitor; |
| 1251 | 1254 |
}; |
| 1252 | 1255 |
}; |
| 1253 | 1256 |
#endif |
| 1254 | 1257 |
|
| 1255 | 1258 |
/// \brief Default traits class of BfsVisit class. |
| 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 |
}; |
| 1281 | 1285 |
|
| 1282 | 1286 |
/// \ingroup search |
| 1283 | 1287 |
/// |
| 1284 | 1288 |
/// \brief BFS algorithm class with visitor interface. |
| 1285 | 1289 |
/// |
| 1286 | 1290 |
/// This class provides an efficient implementation of the BFS algorithm |
| 1287 | 1291 |
/// with visitor interface. |
| 1288 | 1292 |
/// |
| 1289 | 1293 |
/// The BfsVisit class provides an alternative interface to the Bfs |
| 1290 | 1294 |
/// class. It works with callback mechanism, the BfsVisit object calls |
| 1291 | 1295 |
/// the member functions of the \c Visitor class on every BFS event. |
| 1292 | 1296 |
/// |
| 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_BINOMIAL_HEAP_H |
| 20 | 20 |
#define LEMON_BINOMIAL_HEAP_H |
| 21 | 21 |
|
| 22 | 22 |
///\file |
| 23 | 23 |
///\ingroup heaps |
| 24 | 24 |
///\brief Binomial Heap implementation. |
| 25 | 25 |
|
| 26 | 26 |
#include <vector> |
| 27 | 27 |
#include <utility> |
| 28 | 28 |
#include <functional> |
| 29 | 29 |
#include <lemon/math.h> |
| ... | ... |
@@ -237,49 +237,49 @@ |
| 237 | 237 |
/// \brief Remove the given item from the heap. |
| 238 | 238 |
/// |
| 239 | 239 |
/// This function removes the given item from the heap if it is |
| 240 | 240 |
/// already stored. |
| 241 | 241 |
/// \param item The item to delete. |
| 242 | 242 |
/// \pre \e item must be in the heap. |
| 243 | 243 |
void erase (const Item& item) {
|
| 244 | 244 |
int i=_iim[item]; |
| 245 | 245 |
if ( i >= 0 && _data[i].in ) {
|
| 246 | 246 |
decrease( item, _data[_min].prio-1 ); |
| 247 | 247 |
pop(); |
| 248 | 248 |
} |
| 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 |
} |
| 274 | 274 |
|
| 275 | 275 |
/// \brief Increase the priority of an item to the given value. |
| 276 | 276 |
/// |
| 277 | 277 |
/// This function increases the priority of an item to the given value. |
| 278 | 278 |
/// \param item The item. |
| 279 | 279 |
/// \param value The priority. |
| 280 | 280 |
/// \pre \e item must be stored in the heap with priority at most \e value. |
| 281 | 281 |
void increase (Item item, const Prio& value) {
|
| 282 | 282 |
erase(item); |
| 283 | 283 |
push(item, value); |
| 284 | 284 |
} |
| 285 | 285 |
|
| ... | ... |
@@ -301,124 +301,124 @@ |
| 301 | 301 |
} |
| 302 | 302 |
|
| 303 | 303 |
/// \brief Set the state of an item in the heap. |
| 304 | 304 |
/// |
| 305 | 305 |
/// This function sets the state of the given item in the heap. |
| 306 | 306 |
/// It can be used to manually clear the heap when it is important |
| 307 | 307 |
/// to achive better time complexity. |
| 308 | 308 |
/// \param i The item. |
| 309 | 309 |
/// \param st The state. It should not be \c IN_HEAP. |
| 310 | 310 |
void state(const Item& i, State st) {
|
| 311 | 311 |
switch (st) {
|
| 312 | 312 |
case POST_HEAP: |
| 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; |
| 338 | 338 |
} |
| 339 | 339 |
else return -1; |
| 340 | 340 |
} |
| 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 ) {
|
| 366 | 366 |
_head=x_next; |
| 367 | 367 |
} else {
|
| 368 | 368 |
_data[x_prev].right_neighbor=x_next; |
| 369 | 369 |
} |
| 370 | 370 |
fuse(x,x_next); |
| 371 | 371 |
x=x_next; |
| 372 | 372 |
} |
| 373 | 373 |
else {
|
| 374 | 374 |
_data[x].right_neighbor=_data[x_next].right_neighbor; |
| 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 |
} |
| 413 | 413 |
|
| 414 | 414 |
// Unlace node a (if it has siblings) |
| 415 | 415 |
void unlace(int a) {
|
| 416 | 416 |
int neighb=_data[a].right_neighbor; |
| 417 | 417 |
int other=_head; |
| 418 | 418 |
|
| 419 | 419 |
while( _data[other].right_neighbor!=a ) |
| 420 | 420 |
other=_data[other].right_neighbor; |
| 421 | 421 |
_data[other].right_neighbor=neighb; |
| 422 | 422 |
} |
| 423 | 423 |
|
| 424 | 424 |
private: |
| 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_BITS_ARRAY_MAP_H |
| 20 | 20 |
#define LEMON_BITS_ARRAY_MAP_H |
| 21 | 21 |
|
| 22 | 22 |
#include <memory> |
| 23 | 23 |
|
| 24 | 24 |
#include <lemon/bits/traits.h> |
| 25 | 25 |
#include <lemon/bits/alteration_notifier.h> |
| 26 | 26 |
#include <lemon/concept_check.h> |
| 27 | 27 |
#include <lemon/concepts/maps.h> |
| 28 | 28 |
|
| 29 | 29 |
// \ingroup graphbits |
| ... | ... |
@@ -49,49 +49,49 @@ |
| 49 | 49 |
// The graph type. |
| 50 | 50 |
typedef _Graph GraphType; |
| 51 | 51 |
// The item type. |
| 52 | 52 |
typedef _Item Item; |
| 53 | 53 |
// The reference map tag. |
| 54 | 54 |
typedef True ReferenceMapTag; |
| 55 | 55 |
|
| 56 | 56 |
// The key type of the map. |
| 57 | 57 |
typedef _Item Key; |
| 58 | 58 |
// The value type of the map. |
| 59 | 59 |
typedef _Value Value; |
| 60 | 60 |
|
| 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())); |
| 86 | 86 |
allocate_memory(); |
| 87 | 87 |
Notifier* nf = Parent::notifier(); |
| 88 | 88 |
Item it; |
| 89 | 89 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 90 | 90 |
int id = nf->id(it);; |
| 91 | 91 |
allocator.construct(&(values[id]), Value()); |
| 92 | 92 |
} |
| 93 | 93 |
} |
| 94 | 94 |
|
| 95 | 95 |
// \brief Constructor to use default value to initialize the map. |
| 96 | 96 |
// |
| 97 | 97 |
// It constructs a map and initialize all of the the map. |
| 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_BITS_DEFAULT_MAP_H |
| 20 | 20 |
#define LEMON_BITS_DEFAULT_MAP_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/config.h> |
| 23 | 23 |
#include <lemon/bits/array_map.h> |
| 24 | 24 |
#include <lemon/bits/vector_map.h> |
| 25 | 25 |
//#include <lemon/bits/debug_map.h> |
| 26 | 26 |
|
| 27 | 27 |
//\ingroup graphbits |
| 28 | 28 |
//\file |
| 29 | 29 |
//\brief Graph maps that construct and destruct their elements dynamically. |
| ... | ... |
@@ -136,47 +136,47 @@ |
| 136 | 136 |
|
| 137 | 137 |
// pointer |
| 138 | 138 |
template <typename _Graph, typename _Item, typename _Ptr> |
| 139 | 139 |
struct DefaultMapSelector<_Graph, _Item, _Ptr*> {
|
| 140 | 140 |
typedef VectorMap<_Graph, _Item, _Ptr*> Map; |
| 141 | 141 |
}; |
| 142 | 142 |
|
| 143 | 143 |
// #else |
| 144 | 144 |
|
| 145 | 145 |
// template <typename _Graph, typename _Item, typename _Value> |
| 146 | 146 |
// struct DefaultMapSelector {
|
| 147 | 147 |
// typedef DebugMap<_Graph, _Item, _Value> Map; |
| 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> |
| 173 | 173 |
DefaultMap& operator=(const CMap& cmap) {
|
| 174 | 174 |
Parent::operator=(cmap); |
| 175 | 175 |
return *this; |
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
}; |
| 179 | 179 |
|
| 180 | 180 |
} |
| 181 | 181 |
|
| 182 | 182 |
#endif |
| 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_BITS_EDGE_SET_EXTENDER_H |
| 20 | 20 |
#define LEMON_BITS_EDGE_SET_EXTENDER_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/core.h> |
| 23 | 23 |
#include <lemon/error.h> |
| 24 | 24 |
#include <lemon/bits/default_map.h> |
| 25 | 25 |
#include <lemon/bits/map_extender.h> |
| 26 | 26 |
|
| 27 | 27 |
//\ingroup digraphbits |
| 28 | 28 |
//\file |
| 29 | 29 |
//\brief Extenders for the arc set types |
| ... | ... |
@@ -42,233 +42,233 @@ |
| 42 | 42 |
|
| 43 | 43 |
// Base extensions |
| 44 | 44 |
|
| 45 | 45 |
typedef typename Parent::Node Node; |
| 46 | 46 |
typedef typename Parent::Arc Arc; |
| 47 | 47 |
|
| 48 | 48 |
int maxId(Node) const {
|
| 49 | 49 |
return Parent::maxNodeId(); |
| 50 | 50 |
} |
| 51 | 51 |
|
| 52 | 52 |
int maxId(Arc) const {
|
| 53 | 53 |
return Parent::maxArcId(); |
| 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 |
// |
| 195 | 195 |
// Returns the running node (ie. the target in this case) of the |
| 196 | 196 |
// iterator |
| 197 | 197 |
Node runningNode(const OutArcIt &e) const {
|
| 198 | 198 |
return Parent::target(static_cast<const Arc&>(e)); |
| 199 | 199 |
} |
| 200 | 200 |
|
| 201 | 201 |
// \brief Base node of the iterator |
| 202 | 202 |
// |
| 203 | 203 |
// Returns the base node (ie. the target in this case) of the iterator |
| 204 | 204 |
Node baseNode(const InArcIt &e) const {
|
| 205 | 205 |
return Parent::target(static_cast<const Arc&>(e)); |
| 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); |
| 263 | 263 |
} |
| 264 | 264 |
|
| 265 | 265 |
~ArcSetExtender() {
|
| 266 | 266 |
arc_notifier.clear(); |
| 267 | 267 |
} |
| 268 | 268 |
|
| 269 | 269 |
}; |
| 270 | 270 |
|
| 271 | 271 |
|
| 272 | 272 |
// \ingroup digraphbits |
| 273 | 273 |
// |
| 274 | 274 |
// \brief Extender for the EdgeSets |
| ... | ... |
@@ -289,337 +289,337 @@ |
| 289 | 289 |
} |
| 290 | 290 |
|
| 291 | 291 |
int maxId(Arc) const {
|
| 292 | 292 |
return Parent::maxArcId(); |
| 293 | 293 |
} |
| 294 | 294 |
|
| 295 | 295 |
int maxId(Edge) const {
|
| 296 | 296 |
return Parent::maxEdgeId(); |
| 297 | 297 |
} |
| 298 | 298 |
|
| 299 | 299 |
Node fromId(int id, Node) const {
|
| 300 | 300 |
return Parent::nodeFromId(id); |
| 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 |
| 501 | 501 |
// iterator |
| 502 | 502 |
Node runningNode(const OutArcIt &e) const {
|
| 503 | 503 |
return Parent::target(static_cast<const Arc&>(e)); |
| 504 | 504 |
} |
| 505 | 505 |
|
| 506 | 506 |
// \brief Base node of the iterator |
| 507 | 507 |
// |
| 508 | 508 |
// Returns the base node (ie. the target in this case) of the iterator |
| 509 | 509 |
Node baseNode(const InArcIt &e) const {
|
| 510 | 510 |
return Parent::target(static_cast<const Arc&>(e)); |
| 511 | 511 |
} |
| 512 | 512 |
// \brief Running node of the iterator |
| 513 | 513 |
// |
| 514 | 514 |
// Returns the running node (ie. the source in this case) of the |
| 515 | 515 |
// iterator |
| 516 | 516 |
Node runningNode(const InArcIt &e) const {
|
| 517 | 517 |
return Parent::source(static_cast<const Arc&>(e)); |
| 518 | 518 |
} |
| 519 | 519 |
|
| 520 | 520 |
// Base node of the iterator |
| 521 | 521 |
// |
| 522 | 522 |
// Returns the base node of the iterator |
| 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); |
| 607 | 607 |
Parent::erase(edge); |
| 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 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_SOLVER_BITS_H |
| 20 | 20 |
#define LEMON_BITS_SOLVER_BITS_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
|
| 24 | 24 |
namespace lemon {
|
| 25 | 25 |
|
| 26 | 26 |
namespace _solver_bits {
|
| 27 | 27 |
|
| 28 | 28 |
class VarIndex {
|
| 29 | 29 |
private: |
| 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 |
///\file |
| 20 | 20 |
///\brief Some basic non-inline functions and static global data. |
| 21 | 21 |
|
| 22 | 22 |
#include<lemon/bits/windows.h> |
| 23 | 23 |
|
| 24 | 24 |
#ifdef WIN32 |
| 25 | 25 |
#ifndef WIN32_LEAN_AND_MEAN |
| 26 | 26 |
#define WIN32_LEAN_AND_MEAN |
| 27 | 27 |
#endif |
| 28 | 28 |
#ifndef NOMINMAX |
| 29 | 29 |
#define NOMINMAX |
| ... | ... |
@@ -75,49 +75,49 @@ |
| 75 | 75 |
cstime = 0; |
| 76 | 76 |
} |
| 77 | 77 |
#else |
| 78 | 78 |
timeval tv; |
| 79 | 79 |
gettimeofday(&tv, 0); |
| 80 | 80 |
rtime=tv.tv_sec+double(tv.tv_usec)/1e6; |
| 81 | 81 |
|
| 82 | 82 |
tms ts; |
| 83 | 83 |
double tck=sysconf(_SC_CLK_TCK); |
| 84 | 84 |
times(&ts); |
| 85 | 85 |
utime=ts.tms_utime/tck; |
| 86 | 86 |
stime=ts.tms_stime/tck; |
| 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 |
|
| 112 | 112 |
char cbuf[26]; |
| 113 | 113 |
ctime_r(&tv.tv_sec,cbuf); |
| 114 | 114 |
os << cbuf; |
| 115 | 115 |
#endif |
| 116 | 116 |
return os.str(); |
| 117 | 117 |
} |
| 118 | 118 |
|
| 119 | 119 |
int getWinRndSeed() |
| 120 | 120 |
{
|
| 121 | 121 |
#ifdef WIN32 |
| 122 | 122 |
FILETIME time; |
| 123 | 123 |
GetSystemTimeAsFileTime(&time); |
| 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_BUCKET_HEAP_H |
| 20 | 20 |
#define LEMON_BUCKET_HEAP_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup heaps |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief Bucket heap implementation. |
| 25 | 25 |
|
| 26 | 26 |
#include <vector> |
| 27 | 27 |
#include <utility> |
| 28 | 28 |
#include <functional> |
| 29 | 29 |
|
| ... | ... |
@@ -363,49 +363,49 @@ |
| 363 | 363 |
|
| 364 | 364 |
int prev, next; |
| 365 | 365 |
}; |
| 366 | 366 |
|
| 367 | 367 |
ItemIntMap& _iim; |
| 368 | 368 |
std::vector<int> _first; |
| 369 | 369 |
std::vector<BucketItem> _data; |
| 370 | 370 |
mutable int _minimum; |
| 371 | 371 |
|
| 372 | 372 |
}; // class BucketHeap |
| 373 | 373 |
|
| 374 | 374 |
/// \ingroup heaps |
| 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 > |
| 400 | 400 |
class SimpleBucketHeap {
|
| 401 | 401 |
|
| 402 | 402 |
public: |
| 403 | 403 |
|
| 404 | 404 |
/// Type of the item-int map. |
| 405 | 405 |
typedef IM ItemIntMap; |
| 406 | 406 |
/// Type of the priorities. |
| 407 | 407 |
typedef int Prio; |
| 408 | 408 |
/// Type of the items stored in the heap. |
| 409 | 409 |
typedef typename ItemIntMap::Key Item; |
| 410 | 410 |
/// Type of the item-priority pairs. |
| 411 | 411 |
typedef std::pair<Item,Prio> Pair; |
| 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_CAPACITY_SCALING_H |
| 20 | 20 |
#define LEMON_CAPACITY_SCALING_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup min_cost_flow_algs |
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief Capacity Scaling algorithm for finding a minimum cost flow. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <lemon/core.h> |
| ... | ... |
@@ -112,49 +112,49 @@ |
| 112 | 112 |
|
| 113 | 113 |
/// The \ref CapacityScalingDefaultTraits "traits class" of the algorithm |
| 114 | 114 |
typedef TR Traits; |
| 115 | 115 |
|
| 116 | 116 |
public: |
| 117 | 117 |
|
| 118 | 118 |
/// \brief Problem type constants for the \c run() function. |
| 119 | 119 |
/// |
| 120 | 120 |
/// Enum type containing the problem type constants that can be |
| 121 | 121 |
/// returned by the \ref run() function of the algorithm. |
| 122 | 122 |
enum ProblemType {
|
| 123 | 123 |
/// The problem has no feasible solution (flow). |
| 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 |
|
| 149 | 149 |
// Data related to the underlying digraph |
| 150 | 150 |
const GR &_graph; |
| 151 | 151 |
int _node_num; |
| 152 | 152 |
int _arc_num; |
| 153 | 153 |
int _res_arc_num; |
| 154 | 154 |
int _root; |
| 155 | 155 |
|
| 156 | 156 |
// Parameters of the problem |
| 157 | 157 |
bool _have_lower; |
| 158 | 158 |
Value _sum_supply; |
| 159 | 159 |
|
| 160 | 160 |
// Data structures for storing the digraph |
| ... | ... |
@@ -163,79 +163,79 @@ |
| 163 | 163 |
IntArcMap _arc_idb; |
| 164 | 164 |
IntVector _first_out; |
| 165 | 165 |
BoolVector _forward; |
| 166 | 166 |
IntVector _source; |
| 167 | 167 |
IntVector _target; |
| 168 | 168 |
IntVector _reverse; |
| 169 | 169 |
|
| 170 | 170 |
// Node and arc data |
| 171 | 171 |
ValueVector _lower; |
| 172 | 172 |
ValueVector _upper; |
| 173 | 173 |
CostVector _cost; |
| 174 | 174 |
ValueVector _supply; |
| 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 |
| 200 | 200 |
// potentials according to the found distance labels. |
| 201 | 201 |
class ResidualDijkstra |
| 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); |
| 230 | 230 |
heap.push(s, 0); |
| 231 | 231 |
_pred[s] = -1; |
| 232 | 232 |
_proc_nodes.clear(); |
| 233 | 233 |
|
| 234 | 234 |
// Process nodes |
| 235 | 235 |
while (!heap.empty() && _excess[heap.top()] > -delta) {
|
| 236 | 236 |
int u = heap.top(), v; |
| 237 | 237 |
Cost d = heap.prio() + _pi[u], dn; |
| 238 | 238 |
_dist[u] = heap.prio(); |
| 239 | 239 |
_proc_nodes.push_back(u); |
| 240 | 240 |
heap.pop(); |
| 241 | 241 |
|
| ... | ... |
@@ -418,49 +418,49 @@ |
| 418 | 418 |
/// |
| 419 | 419 |
/// This function sets a single source node and a single target node |
| 420 | 420 |
/// and the required flow value. |
| 421 | 421 |
/// If neither this function nor \ref supplyMap() is used before |
| 422 | 422 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 423 | 423 |
/// |
| 424 | 424 |
/// Using this function has the same effect as using \ref supplyMap() |
| 425 | 425 |
/// with such a map in which \c k is assigned to \c s, \c -k is |
| 426 | 426 |
/// assigned to \c t and all other nodes have zero supply value. |
| 427 | 427 |
/// |
| 428 | 428 |
/// \param s The source node. |
| 429 | 429 |
/// \param t The target node. |
| 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(). |
| 455 | 455 |
/// For example, |
| 456 | 456 |
/// \code |
| 457 | 457 |
/// CapacityScaling<ListDigraph> cs(graph); |
| 458 | 458 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 459 | 459 |
/// .supplyMap(sup).run(); |
| 460 | 460 |
/// \endcode |
| 461 | 461 |
/// |
| 462 | 462 |
/// This function can be called more than once. All the given parameters |
| 463 | 463 |
/// are kept for the next call, unless \ref resetParams() or \ref reset() |
| 464 | 464 |
/// is used, thus only the modified parameters have to be set again. |
| 465 | 465 |
/// If the underlying digraph was also modified after the construction |
| 466 | 466 |
/// of the class (or the last \ref reset() call), then the \ref reset() |
| ... | ... |
@@ -554,93 +554,93 @@ |
| 554 | 554 |
/// |
| 555 | 555 |
/// See \ref resetParams() for examples. |
| 556 | 556 |
/// |
| 557 | 557 |
/// \return <tt>(*this)</tt> |
| 558 | 558 |
/// |
| 559 | 559 |
/// \see resetParams(), run() |
| 560 | 560 |
CapacityScaling& reset() {
|
| 561 | 561 |
// Resize vectors |
| 562 | 562 |
_node_num = countNodes(_graph); |
| 563 | 563 |
_arc_num = countArcs(_graph); |
| 564 | 564 |
_res_arc_num = 2 * (_arc_num + _node_num); |
| 565 | 565 |
_root = _node_num; |
| 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) {
|
| 591 | 591 |
_first_out[i] = j; |
| 592 | 592 |
for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) {
|
| 593 | 593 |
_arc_idf[a] = j; |
| 594 | 594 |
_forward[j] = true; |
| 595 | 595 |
_source[j] = i; |
| 596 | 596 |
_target[j] = _node_id[_graph.runningNode(a)]; |
| 597 | 597 |
} |
| 598 | 598 |
for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) {
|
| 599 | 599 |
_arc_idb[a] = j; |
| 600 | 600 |
_forward[j] = false; |
| 601 | 601 |
_source[j] = i; |
| 602 | 602 |
_target[j] = _node_id[_graph.runningNode(a)]; |
| 603 | 603 |
} |
| 604 | 604 |
_forward[j] = false; |
| 605 | 605 |
_source[j] = i; |
| 606 | 606 |
_target[j] = _root; |
| 607 | 607 |
_reverse[j] = k; |
| 608 | 608 |
_forward[k] = true; |
| 609 | 609 |
_source[k] = _root; |
| 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 |
|
| 635 | 635 |
/// @{
|
| 636 | 636 |
|
| 637 | 637 |
/// \brief Return the total cost of the found flow. |
| 638 | 638 |
/// |
| 639 | 639 |
/// This function returns the total cost of the found flow. |
| 640 | 640 |
/// Its complexity is O(e). |
| 641 | 641 |
/// |
| 642 | 642 |
/// \note The return type of the function can be specified as a |
| 643 | 643 |
/// template parameter. For example, |
| 644 | 644 |
/// \code |
| 645 | 645 |
/// cs.totalCost<double>(); |
| 646 | 646 |
/// \endcode |
| ... | ... |
@@ -707,97 +707,97 @@ |
| 707 | 707 |
/// \c Value type of the map. |
| 708 | 708 |
/// |
| 709 | 709 |
/// \pre \ref run() must be called before using this function. |
| 710 | 710 |
template <typename PotentialMap> |
| 711 | 711 |
void potentialMap(PotentialMap &map) const {
|
| 712 | 712 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 713 | 713 |
map.set(n, _pi[_node_id[n]]); |
| 714 | 714 |
} |
| 715 | 715 |
} |
| 716 | 716 |
|
| 717 | 717 |
/// @} |
| 718 | 718 |
|
| 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]; |
| 744 | 744 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 745 | 745 |
if (_forward[j]) {
|
| 746 | 746 |
Value c = _lower[j]; |
| 747 | 747 |
if (c >= 0) {
|
| 748 | 748 |
_res_cap[j] = _upper[j] < MAX ? _upper[j] - c : INF; |
| 749 | 749 |
} else {
|
| 750 | 750 |
_res_cap[j] = _upper[j] < MAX + c ? _upper[j] - c : INF; |
| 751 | 751 |
} |
| 752 | 752 |
_excess[i] -= c; |
| 753 | 753 |
_excess[_target[j]] += c; |
| 754 | 754 |
} else {
|
| 755 | 755 |
_res_cap[j] = 0; |
| 756 | 756 |
} |
| 757 | 757 |
} |
| 758 | 758 |
} |
| 759 | 759 |
} else {
|
| 760 | 760 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 761 | 761 |
_res_cap[j] = _forward[j] ? _upper[j] : 0; |
| 762 | 762 |
} |
| 763 | 763 |
} |
| 764 | 764 |
|
| 765 | 765 |
// Handle negative costs |
| 766 | 766 |
for (int i = 0; i != _root; ++i) {
|
| 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 {
|
| 792 | 792 |
_pi[_root] = 0; |
| 793 | 793 |
_excess[_root] = 0; |
| 794 | 794 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
| 795 | 795 |
int ra = _reverse[a]; |
| 796 | 796 |
_res_cap[a] = 1; |
| 797 | 797 |
_res_cap[ra] = 0; |
| 798 | 798 |
_cost[a] = 0; |
| 799 | 799 |
_cost[ra] = 0; |
| 800 | 800 |
} |
| 801 | 801 |
} |
| 802 | 802 |
|
| 803 | 803 |
// Initialize delta value |
| ... | ... |
@@ -823,51 +823,51 @@ |
| 823 | 823 |
return OPTIMAL; |
| 824 | 824 |
} |
| 825 | 825 |
|
| 826 | 826 |
ProblemType start() {
|
| 827 | 827 |
// Execute the algorithm |
| 828 | 828 |
ProblemType pt; |
| 829 | 829 |
if (_delta > 1) |
| 830 | 830 |
pt = startWithScaling(); |
| 831 | 831 |
else |
| 832 | 832 |
pt = startWithoutScaling(); |
| 833 | 833 |
|
| 834 | 834 |
// Handle non-zero lower bounds |
| 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) {
|
| 862 | 862 |
last_out = _sum_supply < 0 ? |
| 863 | 863 |
_first_out[u+1] : _first_out[u+1] - 1; |
| 864 | 864 |
for (int a = _first_out[u]; a != last_out; ++a) {
|
| 865 | 865 |
int v = _target[a]; |
| 866 | 866 |
Cost c = _cost[a] + _pi[u] - _pi[v]; |
| 867 | 867 |
Value rc = _res_cap[a]; |
| 868 | 868 |
if (c < 0 && rc >= _delta) {
|
| 869 | 869 |
_excess[u] -= rc; |
| 870 | 870 |
_excess[v] += rc; |
| 871 | 871 |
_res_cap[a] = 0; |
| 872 | 872 |
_res_cap[_reverse[a]] += rc; |
| 873 | 873 |
} |
| 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 |
// -*- C++ -*- |
| 20 | 20 |
#ifndef LEMON_CBC_H |
| 21 | 21 |
#define LEMON_CBC_H |
| 22 | 22 |
|
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief Header of the LEMON-CBC mip solver interface. |
| 25 | 25 |
///\ingroup lp_group |
| 26 | 26 |
|
| 27 | 27 |
#include <lemon/lp_base.h> |
| 28 | 28 |
|
| 29 | 29 |
class CoinModel; |
| ... | ... |
@@ -100,31 +100,31 @@ |
| 100 | 100 |
virtual void _setObjCoeffs(ExprIterator b, ExprIterator e); |
| 101 | 101 |
virtual void _getObjCoeffs(InsertIterator b) const; |
| 102 | 102 |
|
| 103 | 103 |
virtual void _setObjCoeff(int i, Value obj_coef); |
| 104 | 104 |
virtual Value _getObjCoeff(int i) const; |
| 105 | 105 |
|
| 106 | 106 |
virtual void _setSense(Sense sense); |
| 107 | 107 |
virtual Sense _getSense() const; |
| 108 | 108 |
|
| 109 | 109 |
virtual ColTypes _getColType(int col) const; |
| 110 | 110 |
virtual void _setColType(int col, ColTypes col_type); |
| 111 | 111 |
|
| 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 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_CIRCULATION_H |
| 20 | 20 |
#define LEMON_CIRCULATION_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/tolerance.h> |
| 23 | 23 |
#include <lemon/elevator.h> |
| 24 | 24 |
#include <limits> |
| 25 | 25 |
|
| 26 | 26 |
///\ingroup max_flow |
| 27 | 27 |
///\file |
| 28 | 28 |
///\brief Push-relabel algorithm for finding a feasible circulation. |
| 29 | 29 |
/// |
| ... | ... |
@@ -38,50 +38,50 @@ |
| 38 | 38 |
/// \tparam UM The type of the upper bound (capacity) map. |
| 39 | 39 |
/// \tparam SM The type of the supply map. |
| 40 | 40 |
template <typename GR, typename LM, |
| 41 | 41 |
typename UM, typename SM> |
| 42 | 42 |
struct CirculationDefaultTraits {
|
| 43 | 43 |
|
| 44 | 44 |
/// \brief The type of the digraph the algorithm runs on. |
| 45 | 45 |
typedef GR Digraph; |
| 46 | 46 |
|
| 47 | 47 |
/// \brief The type of the lower bound map. |
| 48 | 48 |
/// |
| 49 | 49 |
/// The type of the map that stores the lower bounds on the arcs. |
| 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 |
| 76 | 76 |
typedef GR::ArcMap<Value> FlowMap; |
| 77 | 77 |
#else |
| 78 | 78 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
| 79 | 79 |
#endif |
| 80 | 80 |
|
| 81 | 81 |
/// \brief Instantiates a FlowMap. |
| 82 | 82 |
/// |
| 83 | 83 |
/// This function instantiates a \ref FlowMap. |
| 84 | 84 |
/// \param digraph The digraph for which we would like to define |
| 85 | 85 |
/// the flow map. |
| 86 | 86 |
static FlowMap* createFlowMap(const Digraph& digraph) {
|
| 87 | 87 |
return new FlowMap(digraph); |
| ... | ... |
@@ -120,59 +120,59 @@ |
| 120 | 120 |
|
| 121 | 121 |
\ingroup max_flow |
| 122 | 122 |
This class implements a push-relabel algorithm for the \e network |
| 123 | 123 |
\e circulation problem. |
| 124 | 124 |
It is to find a feasible circulation when lower and upper bounds |
| 125 | 125 |
are given for the flow values on the arcs and lower bounds are |
| 126 | 126 |
given for the difference between the outgoing and incoming flow |
| 127 | 127 |
at the nodes. |
| 128 | 128 |
|
| 129 | 129 |
The exact formulation of this problem is the following. |
| 130 | 130 |
Let \f$G=(V,A)\f$ be a digraph, \f$lower: A\rightarrow\mathbf{R}\f$
|
| 131 | 131 |
\f$upper: A\rightarrow\mathbf{R}\cup\{\infty\}\f$ denote the lower and
|
| 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 |
| 167 | 167 |
\ref min_cost_flow "minimum cost flow problem". |
| 168 | 168 |
|
| 169 | 169 |
\tparam GR The type of the digraph the algorithm runs on. |
| 170 | 170 |
\tparam LM The type of the lower bound map. The default |
| 171 | 171 |
map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 172 | 172 |
\tparam UM The type of the upper bound (capacity) map. |
| 173 | 173 |
The default map type is \c LM. |
| 174 | 174 |
\tparam SM The type of the supply map. The default map type is |
| 175 | 175 |
\ref concepts::Digraph::NodeMap "GR::NodeMap<UM::Value>". |
| 176 | 176 |
\tparam TR The traits class that defines various types used by the |
| 177 | 177 |
algorithm. By default, it is \ref CirculationDefaultTraits |
| 178 | 178 |
"CirculationDefaultTraits<GR, LM, UM, SM>". |
| ... | ... |
@@ -316,49 +316,49 @@ |
| 316 | 316 |
/// before calling \ref run() or \ref init(). |
| 317 | 317 |
/// \sa SetElevator |
| 318 | 318 |
template <typename T> |
| 319 | 319 |
struct SetStandardElevator |
| 320 | 320 |
: public Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
| 321 | 321 |
SetStandardElevatorTraits<T> > {
|
| 322 | 322 |
typedef Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
| 323 | 323 |
SetStandardElevatorTraits<T> > Create; |
| 324 | 324 |
}; |
| 325 | 325 |
|
| 326 | 326 |
/// @} |
| 327 | 327 |
|
| 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 |
} |
| 353 | 353 |
|
| 354 | 354 |
|
| 355 | 355 |
private: |
| 356 | 356 |
|
| 357 | 357 |
bool checkBoundMaps() {
|
| 358 | 358 |
for (ArcIt e(_g);e!=INVALID;++e) {
|
| 359 | 359 |
if (_tol.less((*_up)[e], (*_lo)[e])) return false; |
| 360 | 360 |
} |
| 361 | 361 |
return true; |
| 362 | 362 |
} |
| 363 | 363 |
|
| 364 | 364 |
void createStructures() {
|
| 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/clp.h> |
| 20 | 20 |
#include <coin/ClpSimplex.hpp> |
| 21 | 21 |
|
| 22 | 22 |
namespace lemon {
|
| 23 | 23 |
|
| 24 | 24 |
ClpLp::ClpLp() {
|
| 25 | 25 |
_prob = new ClpSimplex(); |
| 26 | 26 |
_init_temporals(); |
| 27 | 27 |
messageLevel(MESSAGE_NOTHING); |
| 28 | 28 |
} |
| 29 | 29 |
| 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_CLP_H |
| 20 | 20 |
#define LEMON_CLP_H |
| 21 | 21 |
|
| 22 | 22 |
///\file |
| 23 | 23 |
///\brief Header of the LEMON-CLP lp solver interface. |
| 24 | 24 |
|
| 25 | 25 |
#include <vector> |
| 26 | 26 |
#include <string> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/lp_base.h> |
| 29 | 29 |
|
| ... | ... |
@@ -117,48 +117,48 @@ |
| 117 | 117 |
virtual Value _getObjCoeff(int i) const; |
| 118 | 118 |
|
| 119 | 119 |
virtual void _setSense(Sense sense); |
| 120 | 120 |
virtual Sense _getSense() const; |
| 121 | 121 |
|
| 122 | 122 |
virtual SolveExitStatus _solve(); |
| 123 | 123 |
|
| 124 | 124 |
virtual Value _getPrimal(int i) const; |
| 125 | 125 |
virtual Value _getDual(int i) const; |
| 126 | 126 |
|
| 127 | 127 |
virtual Value _getPrimalValue() const; |
| 128 | 128 |
|
| 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. |
| 154 | 154 |
int clpRow(Row r) const { return rows(id(r)); }
|
| 155 | 155 |
|
| 156 | 156 |
///Returns the variable identifier understood by CLP. |
| 157 | 157 |
int clpCol(Col c) const { return cols(id(c)); }
|
| 158 | 158 |
|
| 159 | 159 |
}; |
| 160 | 160 |
|
| 161 | 161 |
} //END OF NAMESPACE LEMON |
| 162 | 162 |
|
| 163 | 163 |
#endif //LEMON_CLP_H |
| 164 | 164 |
| 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_CONCEPTS_DIGRAPH_H |
| 20 | 20 |
#define LEMON_CONCEPTS_DIGRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup graph_concepts |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief The concept of directed graphs. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/core.h> |
| 27 | 27 |
#include <lemon/concepts/maps.h> |
| 28 | 28 |
#include <lemon/concept_check.h> |
| 29 | 29 |
#include <lemon/concepts/graph_components.h> |
| ... | ... |
@@ -413,49 +413,49 @@ |
| 413 | 413 |
/// (i.e. the target node of the corresponding arc). |
| 414 | 414 |
Node baseNode(InArcIt) const { return INVALID; }
|
| 415 | 415 |
|
| 416 | 416 |
/// \brief The running node of the iterator. |
| 417 | 417 |
/// |
| 418 | 418 |
/// Returns the running node of the given incomming arc iterator |
| 419 | 419 |
/// (i.e. the source node of the corresponding arc). |
| 420 | 420 |
Node runningNode(InArcIt) const { return INVALID; }
|
| 421 | 421 |
|
| 422 | 422 |
/// \brief Standard graph map type for the nodes. |
| 423 | 423 |
/// |
| 424 | 424 |
/// Standard graph map type for the nodes. |
| 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. |
| 450 | 450 |
/// It conforms to the ReferenceMap concept. |
| 451 | 451 |
template<class T> |
| 452 | 452 |
class ArcMap : public ReferenceMap<Arc, T, T&, const T&> {
|
| 453 | 453 |
public: |
| 454 | 454 |
|
| 455 | 455 |
/// Constructor |
| 456 | 456 |
explicit ArcMap(const Digraph&) { }
|
| 457 | 457 |
/// Constructor with given initial value |
| 458 | 458 |
ArcMap(const Digraph&, T) { }
|
| 459 | 459 |
|
| 460 | 460 |
private: |
| 461 | 461 |
///Copy constructor |
| 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 |
///\ingroup graph_concepts |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief The concept of undirected graphs. |
| 22 | 22 |
|
| 23 | 23 |
#ifndef LEMON_CONCEPTS_GRAPH_H |
| 24 | 24 |
#define LEMON_CONCEPTS_GRAPH_H |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/concepts/graph_components.h> |
| 27 | 27 |
#include <lemon/concepts/maps.h> |
| 28 | 28 |
#include <lemon/concept_check.h> |
| 29 | 29 |
#include <lemon/core.h> |
| 30 | 30 |
|
| 31 | 31 |
namespace lemon {
|
| 32 | 32 |
namespace concepts {
|
| 33 | 33 |
|
| 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 |
| 59 | 59 |
/// as OutArcIt and InArcIt, but it is not convertible to Arc, |
| 60 | 60 |
/// only to Edge. |
| 61 | 61 |
/// |
| 62 | 62 |
/// In LEMON, each undirected edge has an inherent orientation. |
| 63 | 63 |
/// Thus it can defined if an arc is forward or backward oriented in |
| 64 | 64 |
/// an undirected graph with respect to this default oriantation of |
| 65 | 65 |
/// the represented edge. |
| 66 | 66 |
/// With the direction() and direct() functions the direction |
| 67 | 67 |
/// of an arc can be obtained and set, respectively. |
| 68 | 68 |
/// |
| 69 | 69 |
/// Only nodes and edges can be added to or removed from an undirected |
| 70 | 70 |
/// graph and the corresponding arcs are added or removed automatically. |
| 71 | 71 |
/// |
| 72 | 72 |
/// \sa Digraph |
| 73 | 73 |
class Graph {
|
| 74 | 74 |
private: |
| 75 | 75 |
/// Graphs are \e not copy constructible. Use DigraphCopy instead. |
| 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 |
| 101 | 101 |
|
| 102 | 102 |
/// Default constructor. |
| 103 | 103 |
/// \warning It sets the object to an undefined value. |
| 104 | 104 |
Node() { }
|
| 105 | 105 |
/// Copy constructor. |
| 106 | 106 |
|
| 107 | 107 |
/// Copy constructor. |
| 108 | 108 |
/// |
| 109 | 109 |
Node(const Node&) { }
|
| 110 | 110 |
|
| 111 | 111 |
/// %Invalid constructor \& conversion. |
| 112 | 112 |
|
| ... | ... |
@@ -339,49 +339,49 @@ |
| 339 | 339 |
/// \sa Invalid for more details. |
| 340 | 340 |
Arc(Invalid) { }
|
| 341 | 341 |
/// Equality operator |
| 342 | 342 |
|
| 343 | 343 |
/// Equality operator. |
| 344 | 344 |
/// |
| 345 | 345 |
/// Two iterators are equal if and only if they point to the |
| 346 | 346 |
/// same object or both are \c INVALID. |
| 347 | 347 |
bool operator==(Arc) const { return true; }
|
| 348 | 348 |
/// Inequality operator |
| 349 | 349 |
|
| 350 | 350 |
/// Inequality operator. |
| 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; |
| 376 | 376 |
/// for(Graph::ArcIt a(g); a!=INVALID; ++a) ++count; |
| 377 | 377 |
///\endcode |
| 378 | 378 |
class ArcIt : public Arc {
|
| 379 | 379 |
public: |
| 380 | 380 |
/// Default constructor |
| 381 | 381 |
|
| 382 | 382 |
/// Default constructor. |
| 383 | 383 |
/// \warning It sets the iterator to an undefined value. |
| 384 | 384 |
ArcIt() { }
|
| 385 | 385 |
/// Copy constructor. |
| 386 | 386 |
|
| 387 | 387 |
/// Copy constructor. |
| 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 |
///\ingroup graph_concepts |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief The concepts of graph components. |
| 22 | 22 |
|
| 23 | 23 |
#ifndef LEMON_CONCEPTS_GRAPH_COMPONENTS_H |
| 24 | 24 |
#define LEMON_CONCEPTS_GRAPH_COMPONENTS_H |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/core.h> |
| 27 | 27 |
#include <lemon/concepts/maps.h> |
| 28 | 28 |
|
| 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 |
| 54 | 54 |
/// as uninitialized. |
| 55 | 55 |
GraphItem() {}
|
| 56 | 56 |
|
| 57 | 57 |
/// \brief Copy constructor. |
| 58 | 58 |
/// |
| 59 | 59 |
/// Copy constructor. |
| 60 | 60 |
GraphItem(const GraphItem &) {}
|
| 61 | 61 |
|
| 62 | 62 |
/// \brief Constructor for conversion from \c INVALID. |
| 63 | 63 |
/// |
| 64 | 64 |
/// Constructor for conversion from \c INVALID. |
| 65 | 65 |
/// It initializes the item to be invalid. |
| ... | ... |
@@ -68,82 +68,82 @@ |
| 68 | 68 |
|
| 69 | 69 |
/// \brief Assignment operator. |
| 70 | 70 |
/// |
| 71 | 71 |
/// Assignment operator for the item. |
| 72 | 72 |
GraphItem& operator=(const GraphItem&) { return *this; }
|
| 73 | 73 |
|
| 74 | 74 |
/// \brief Assignment operator for INVALID. |
| 75 | 75 |
/// |
| 76 | 76 |
/// This operator makes the item invalid. |
| 77 | 77 |
GraphItem& operator=(Invalid) { return *this; }
|
| 78 | 78 |
|
| 79 | 79 |
/// \brief Equality operator. |
| 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; |
| 105 | 105 |
_GraphItem i2 = i1; |
| 106 | 106 |
_GraphItem i3 = INVALID; |
| 107 | 107 |
|
| 108 | 108 |
i1 = i2 = i3; |
| 109 | 109 |
|
| 110 | 110 |
bool b; |
| 111 | 111 |
b = (ia == ib) && (ia != ib); |
| 112 | 112 |
b = (ia == INVALID) && (ib != INVALID); |
| 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. |
| 138 | 138 |
/// |
| 139 | 139 |
/// This class represents the arcs of the digraph. |
| 140 | 140 |
typedef GraphItem<'a'> Arc; |
| 141 | 141 |
|
| 142 | 142 |
/// \brief Return the source node of an arc. |
| 143 | 143 |
/// |
| 144 | 144 |
/// This function returns the source node of an arc. |
| 145 | 145 |
Node source(const Arc&) const { return INVALID; }
|
| 146 | 146 |
|
| 147 | 147 |
/// \brief Return the target node of an arc. |
| 148 | 148 |
/// |
| 149 | 149 |
/// This function returns the target node of an arc. |
| ... | ... |
@@ -405,156 +405,156 @@ |
| 405 | 405 |
/// |
| 406 | 406 |
/// This function returns an integer greater or equal to the |
| 407 | 407 |
/// maximum edge id. |
| 408 | 408 |
int maxEdgeId() const { return -1; }
|
| 409 | 409 |
|
| 410 | 410 |
template <typename _Graph> |
| 411 | 411 |
struct Constraints {
|
| 412 | 412 |
|
| 413 | 413 |
void constraints() {
|
| 414 | 414 |
checkConcept<IDableDigraphComponent<Base>, _Graph >(); |
| 415 | 415 |
typename _Graph::Edge edge; |
| 416 | 416 |
int ueid = graph.id(edge); |
| 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 |
|
| 442 | 442 |
/// \brief Copy constructor. |
| 443 | 443 |
/// |
| 444 | 444 |
/// Copy constructor. |
| 445 | 445 |
GraphItemIt(const GraphItemIt& it) : Item(it) {}
|
| 446 | 446 |
|
| 447 | 447 |
/// \brief Constructor that sets the iterator to the first item. |
| 448 | 448 |
/// |
| 449 | 449 |
/// Constructor that sets the iterator to the first item. |
| 450 | 450 |
explicit GraphItemIt(const GR&) {}
|
| 451 | 451 |
|
| 452 | 452 |
/// \brief Constructor for conversion from \c INVALID. |
| 453 | 453 |
/// |
| 454 | 454 |
/// Constructor for conversion from \c INVALID. |
| 455 | 455 |
/// It initializes the iterator to be invalid. |
| 456 | 456 |
/// \sa Invalid for more details. |
| 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. |
| 482 | 482 |
bool operator!=(const GraphItemIt&) const { return true;}
|
| 483 | 483 |
|
| 484 | 484 |
template<typename _GraphItemIt> |
| 485 | 485 |
struct Constraints {
|
| 486 | 486 |
void constraints() {
|
| 487 | 487 |
checkConcept<GraphItem<>, _GraphItemIt>(); |
| 488 | 488 |
_GraphItemIt it1(g); |
| 489 | 489 |
_GraphItemIt it2; |
| 490 | 490 |
_GraphItemIt it3 = it1; |
| 491 | 491 |
_GraphItemIt it4 = INVALID; |
| 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 |
/// |
| 549 | 549 |
/// Assignment operator for the iterator. |
| 550 | 550 |
GraphIncIt& operator=(const GraphIncIt&) { return *this; }
|
| 551 | 551 |
|
| 552 | 552 |
/// \brief Increment the iterator. |
| 553 | 553 |
/// |
| 554 | 554 |
/// This operator increments the iterator, i.e. assigns it to the |
| 555 | 555 |
/// next arc incoming to or outgoing from the given node. |
| 556 | 556 |
GraphIncIt& operator++() { return *this; }
|
| 557 | 557 |
|
| 558 | 558 |
/// \brief Equality operator |
| 559 | 559 |
/// |
| 560 | 560 |
/// Equality operator. |
| ... | ... |
@@ -783,58 +783,58 @@ |
| 783 | 783 |
|
| 784 | 784 |
typedef IterableGraphComponent Graph; |
| 785 | 785 |
|
| 786 | 786 |
/// \name Base Iteration |
| 787 | 787 |
/// |
| 788 | 788 |
/// This interface provides functions for iteration on edges. |
| 789 | 789 |
/// |
| 790 | 790 |
/// @{
|
| 791 | 791 |
|
| 792 | 792 |
using IterableDigraphComponent<Base>::first; |
| 793 | 793 |
using IterableDigraphComponent<Base>::next; |
| 794 | 794 |
|
| 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 |
/// |
| 829 | 829 |
/// @{
|
| 830 | 830 |
|
| 831 | 831 |
/// \brief This iterator goes through each edge. |
| 832 | 832 |
/// |
| 833 | 833 |
/// This iterator goes through each edge. |
| 834 | 834 |
typedef GraphItemIt<Graph, Edge> EdgeIt; |
| 835 | 835 |
|
| 836 | 836 |
/// \brief This iterator goes trough the incident edges of a |
| 837 | 837 |
/// node. |
| 838 | 838 |
/// |
| 839 | 839 |
/// This iterator goes trough the incident edges of a certain |
| 840 | 840 |
/// node of a graph. |
| ... | ... |
@@ -969,49 +969,49 @@ |
| 969 | 969 |
|
| 970 | 970 |
/// \brief Return the edge alteration notifier. |
| 971 | 971 |
/// |
| 972 | 972 |
/// This function gives back the edge alteration notifier. |
| 973 | 973 |
EdgeNotifier& notifier(Edge) const {
|
| 974 | 974 |
return EdgeNotifier(); |
| 975 | 975 |
} |
| 976 | 976 |
|
| 977 | 977 |
template <typename _Graph> |
| 978 | 978 |
struct Constraints {
|
| 979 | 979 |
void constraints() {
|
| 980 | 980 |
checkConcept<AlterableDigraphComponent<Base>, _Graph>(); |
| 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; |
| 1006 | 1006 |
/// The reference type of the map. |
| 1007 | 1007 |
typedef Value& Reference; |
| 1008 | 1008 |
/// The const reference type of the map. |
| 1009 | 1009 |
typedef const Value& ConstReference; |
| 1010 | 1010 |
|
| 1011 | 1011 |
// The reference map tag. |
| 1012 | 1012 |
typedef True ReferenceMapTag; |
| 1013 | 1013 |
|
| 1014 | 1014 |
/// \brief Construct a new map. |
| 1015 | 1015 |
/// |
| 1016 | 1016 |
/// Construct a new map for the graph. |
| 1017 | 1017 |
explicit GraphMap(const GR&) {}
|
| ... | ... |
@@ -1024,72 +1024,72 @@ |
| 1024 | 1024 |
/// \brief Copy constructor. |
| 1025 | 1025 |
/// |
| 1026 | 1026 |
/// Copy Constructor. |
| 1027 | 1027 |
GraphMap(const GraphMap&) : Parent() {}
|
| 1028 | 1028 |
|
| 1029 | 1029 |
/// \brief Assignment operator. |
| 1030 | 1030 |
/// |
| 1031 | 1031 |
/// Assignment operator. It does not mofify the underlying graph, |
| 1032 | 1032 |
/// it just iterates on the current item set and set the map |
| 1033 | 1033 |
/// with the value returned by the assigned map. |
| 1034 | 1034 |
template <typename CMap> |
| 1035 | 1035 |
GraphMap& operator=(const CMap&) {
|
| 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 |
|
| 1084 | 1084 |
/// \brief Standard graph map for the nodes. |
| 1085 | 1085 |
/// |
| 1086 | 1086 |
/// Standard graph map for the nodes. |
| 1087 | 1087 |
/// It conforms to the ReferenceMap concept. |
| 1088 | 1088 |
template <typename V> |
| 1089 | 1089 |
class NodeMap : public GraphMap<MappableDigraphComponent, Node, V> {
|
| 1090 | 1090 |
typedef GraphMap<MappableDigraphComponent, Node, V> Parent; |
| 1091 | 1091 |
|
| 1092 | 1092 |
public: |
| 1093 | 1093 |
/// \brief Construct a new map. |
| 1094 | 1094 |
/// |
| 1095 | 1095 |
/// Construct a new map for the digraph. |
| ... | ... |
@@ -1184,49 +1184,49 @@ |
| 1184 | 1184 |
} |
| 1185 | 1185 |
|
| 1186 | 1186 |
{ // int map test
|
| 1187 | 1187 |
typedef typename _Digraph::template ArcMap<int> IntArcMap; |
| 1188 | 1188 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, int>, |
| 1189 | 1189 |
IntArcMap >(); |
| 1190 | 1190 |
} { // bool map test
|
| 1191 | 1191 |
typedef typename _Digraph::template ArcMap<bool> BoolArcMap; |
| 1192 | 1192 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, bool>, |
| 1193 | 1193 |
BoolArcMap >(); |
| 1194 | 1194 |
} { // Dummy map test
|
| 1195 | 1195 |
typedef typename _Digraph::template ArcMap<Dummy> DummyArcMap; |
| 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. |
| 1221 | 1221 |
/// |
| 1222 | 1222 |
/// Standard graph map for the edges. |
| 1223 | 1223 |
/// It conforms to the ReferenceMap concept. |
| 1224 | 1224 |
template <typename V> |
| 1225 | 1225 |
class EdgeMap : public GraphMap<MappableGraphComponent, Edge, V> {
|
| 1226 | 1226 |
typedef GraphMap<MappableGraphComponent, Edge, V> Parent; |
| 1227 | 1227 |
|
| 1228 | 1228 |
public: |
| 1229 | 1229 |
/// \brief Construct a new map. |
| 1230 | 1230 |
/// |
| 1231 | 1231 |
/// Construct a new map for the graph. |
| 1232 | 1232 |
explicit EdgeMap(const MappableGraphComponent& graph) |
| ... | ... |
@@ -1269,176 +1269,176 @@ |
| 1269 | 1269 |
checkConcept<MappableDigraphComponent<Base>, _Graph>(); |
| 1270 | 1270 |
|
| 1271 | 1271 |
{ // int map test
|
| 1272 | 1272 |
typedef typename _Graph::template EdgeMap<int> IntEdgeMap; |
| 1273 | 1273 |
checkConcept<GraphMap<_Graph, typename _Graph::Edge, int>, |
| 1274 | 1274 |
IntEdgeMap >(); |
| 1275 | 1275 |
} { // bool map test
|
| 1276 | 1276 |
typedef typename _Graph::template EdgeMap<bool> BoolEdgeMap; |
| 1277 | 1277 |
checkConcept<GraphMap<_Graph, typename _Graph::Edge, bool>, |
| 1278 | 1278 |
BoolEdgeMap >(); |
| 1279 | 1279 |
} { // Dummy map test
|
| 1280 | 1280 |
typedef typename _Graph::template EdgeMap<Dummy> DummyEdgeMap; |
| 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 |
/// |
| 1306 | 1306 |
/// This function adds a new node to the digraph. |
| 1307 | 1307 |
Node addNode() {
|
| 1308 | 1308 |
return INVALID; |
| 1309 | 1309 |
} |
| 1310 | 1310 |
|
| 1311 | 1311 |
/// \brief Add a new arc connecting the given two nodes. |
| 1312 | 1312 |
/// |
| 1313 | 1313 |
/// This function adds a new arc connecting the given two nodes |
| 1314 | 1314 |
/// of the digraph. |
| 1315 | 1315 |
Arc addArc(const Node&, const Node&) {
|
| 1316 | 1316 |
return INVALID; |
| 1317 | 1317 |
} |
| 1318 | 1318 |
|
| 1319 | 1319 |
template <typename _Digraph> |
| 1320 | 1320 |
struct Constraints {
|
| 1321 | 1321 |
void constraints() {
|
| 1322 | 1322 |
checkConcept<Base, _Digraph>(); |
| 1323 | 1323 |
typename _Digraph::Node node_a, node_b; |
| 1324 | 1324 |
node_a = digraph.addNode(); |
| 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 |
/// |
| 1350 | 1350 |
/// This function adds a new node to the digraph. |
| 1351 | 1351 |
Node addNode() {
|
| 1352 | 1352 |
return INVALID; |
| 1353 | 1353 |
} |
| 1354 | 1354 |
|
| 1355 | 1355 |
/// \brief Add a new edge connecting the given two nodes. |
| 1356 | 1356 |
/// |
| 1357 | 1357 |
/// This function adds a new edge connecting the given two nodes |
| 1358 | 1358 |
/// of the graph. |
| 1359 | 1359 |
Edge addEdge(const Node&, const Node&) {
|
| 1360 | 1360 |
return INVALID; |
| 1361 | 1361 |
} |
| 1362 | 1362 |
|
| 1363 | 1363 |
template <typename _Graph> |
| 1364 | 1364 |
struct Constraints {
|
| 1365 | 1365 |
void constraints() {
|
| 1366 | 1366 |
checkConcept<Base, _Graph>(); |
| 1367 | 1367 |
typename _Graph::Node node_a, node_b; |
| 1368 | 1368 |
node_a = graph.addNode(); |
| 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>(); |
| 1407 | 1407 |
const typename _Digraph::Node node(INVALID); |
| 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 |
/// |
| 1433 | 1433 |
/// This function erases the given node from the graph and all edges |
| 1434 | 1434 |
/// connected to the node. |
| 1435 | 1435 |
void erase(const Node&) {}
|
| 1436 | 1436 |
|
| 1437 | 1437 |
/// \brief Erase an edge from the digraph. |
| 1438 | 1438 |
/// |
| 1439 | 1439 |
/// This function erases the given edge from the digraph. |
| 1440 | 1440 |
void erase(const Edge&) {}
|
| 1441 | 1441 |
|
| 1442 | 1442 |
template <typename _Graph> |
| 1443 | 1443 |
struct Constraints {
|
| 1444 | 1444 |
void constraints() {
|
| 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_CONCEPTS_HEAP_H |
| 20 | 20 |
#define LEMON_CONCEPTS_HEAP_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup concept |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief The concept of heaps. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/core.h> |
| 27 | 27 |
#include <lemon/concept_check.h> |
| 28 | 28 |
|
| 29 | 29 |
namespace lemon {
|
| ... | ... |
@@ -71,202 +71,202 @@ |
| 71 | 71 |
/// |
| 72 | 72 |
/// Each item has a state associated to it. It can be "in heap", |
| 73 | 73 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the |
| 74 | 74 |
/// heap's point of view, but may be useful to the user. |
| 75 | 75 |
/// |
| 76 | 76 |
/// The item-int map must be initialized in such way that it assigns |
| 77 | 77 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
| 78 | 78 |
enum State {
|
| 79 | 79 |
IN_HEAP = 0, ///< = 0. The "in heap" state constant. |
| 80 | 80 |
PRE_HEAP = -1, ///< = -1. The "pre-heap" state constant. |
| 81 | 81 |
POST_HEAP = -2 ///< = -2. The "post-heap" state constant. |
| 82 | 82 |
}; |
| 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. |
| 122 | 122 |
/// |
| 123 | 123 |
/// This functon makes the heap empty. |
| 124 | 124 |
/// It does not change the cross reference map. If you want to reuse |
| 125 | 125 |
/// a heap that is not surely empty, you should first clear it and |
| 126 | 126 |
/// then you should set the cross reference map to \c PRE_HEAP |
| 127 | 127 |
/// for each item. |
| 128 | 128 |
void clear() {}
|
| 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(); }
|
| 154 | 154 |
|
| 155 | 155 |
/// \brief Remove the item having minimum priority. |
| 156 | 156 |
/// |
| 157 | 157 |
/// This function removes the item having minimum priority. |
| 158 | 158 |
/// \pre The heap must be non-empty. |
| 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; |
| 261 | 261 |
item=Item(); |
| 262 | 262 |
prio=Prio(); |
| 263 | 263 |
ignore_unused_variable_warning(item); |
| 264 | 264 |
ignore_unused_variable_warning(prio); |
| 265 | 265 |
|
| 266 | 266 |
OwnItem own_item; |
| 267 | 267 |
OwnPrio own_prio; |
| 268 | 268 |
OwnState own_state; |
| 269 | 269 |
own_item=Item(); |
| 270 | 270 |
own_prio=Prio(); |
| 271 | 271 |
ignore_unused_variable_warning(own_item); |
| 272 | 272 |
ignore_unused_variable_warning(own_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 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_CONNECTIVITY_H |
| 20 | 20 |
#define LEMON_CONNECTIVITY_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/dfs.h> |
| 23 | 23 |
#include <lemon/bfs.h> |
| 24 | 24 |
#include <lemon/core.h> |
| 25 | 25 |
#include <lemon/maps.h> |
| 26 | 26 |
#include <lemon/adaptors.h> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/concepts/digraph.h> |
| 29 | 29 |
#include <lemon/concepts/graph.h> |
| ... | ... |
@@ -237,49 +237,49 @@ |
| 237 | 237 |
} |
| 238 | 238 |
} |
| 239 | 239 |
private: |
| 240 | 240 |
const Digraph& _digraph; |
| 241 | 241 |
ArcMap& _cutMap; |
| 242 | 242 |
int& _cutNum; |
| 243 | 243 |
|
| 244 | 244 |
typename Digraph::template NodeMap<int> _compMap; |
| 245 | 245 |
int _num; |
| 246 | 246 |
}; |
| 247 | 247 |
|
| 248 | 248 |
} |
| 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 |
|
| 274 | 274 |
using namespace _connectivity_bits; |
| 275 | 275 |
|
| 276 | 276 |
typedef DfsVisitor<Digraph> Visitor; |
| 277 | 277 |
Visitor visitor; |
| 278 | 278 |
|
| 279 | 279 |
DfsVisit<Digraph, Visitor> dfs(digraph, visitor); |
| 280 | 280 |
dfs.init(); |
| 281 | 281 |
dfs.addSource(source); |
| 282 | 282 |
dfs.start(); |
| 283 | 283 |
|
| 284 | 284 |
for (NodeIt it(digraph); it != INVALID; ++it) {
|
| 285 | 285 |
if (!dfs.reached(it)) {
|
| ... | ... |
@@ -289,49 +289,49 @@ |
| 289 | 289 |
|
| 290 | 290 |
typedef ReverseDigraph<const Digraph> RDigraph; |
| 291 | 291 |
typedef typename RDigraph::NodeIt RNodeIt; |
| 292 | 292 |
RDigraph rdigraph(digraph); |
| 293 | 293 |
|
| 294 | 294 |
typedef DfsVisitor<RDigraph> RVisitor; |
| 295 | 295 |
RVisitor rvisitor; |
| 296 | 296 |
|
| 297 | 297 |
DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor); |
| 298 | 298 |
rdfs.init(); |
| 299 | 299 |
rdfs.addSource(source); |
| 300 | 300 |
rdfs.start(); |
| 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 |
| 326 | 326 |
/// strongly connected components. |
| 327 | 327 |
/// |
| 328 | 328 |
/// \see stronglyConnected(), stronglyConnectedComponents() |
| 329 | 329 |
template <typename Digraph> |
| 330 | 330 |
int countStronglyConnectedComponents(const Digraph& digraph) {
|
| 331 | 331 |
checkConcept<concepts::Digraph, Digraph>(); |
| 332 | 332 |
|
| 333 | 333 |
using namespace _connectivity_bits; |
| 334 | 334 |
|
| 335 | 335 |
typedef typename Digraph::Node Node; |
| 336 | 336 |
typedef typename Digraph::Arc Arc; |
| 337 | 337 |
typedef typename Digraph::NodeIt NodeIt; |
| ... | ... |
@@ -723,63 +723,63 @@ |
| 723 | 723 |
} |
| 724 | 724 |
|
| 725 | 725 |
private: |
| 726 | 726 |
const Digraph& _graph; |
| 727 | 727 |
NodeMap& _cutMap; |
| 728 | 728 |
int& _cutNum; |
| 729 | 729 |
|
| 730 | 730 |
typename Digraph::template NodeMap<int> _numMap; |
| 731 | 731 |
typename Digraph::template NodeMap<int> _retMap; |
| 732 | 732 |
typename Digraph::template NodeMap<Node> _predMap; |
| 733 | 733 |
std::stack<Edge> _edgeStack; |
| 734 | 734 |
int _num; |
| 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() |
| 774 | 774 |
template <typename Graph> |
| 775 | 775 |
int countBiNodeConnectedComponents(const Graph& graph) {
|
| 776 | 776 |
checkConcept<concepts::Graph, Graph>(); |
| 777 | 777 |
typedef typename Graph::NodeIt NodeIt; |
| 778 | 778 |
|
| 779 | 779 |
using namespace _connectivity_bits; |
| 780 | 780 |
|
| 781 | 781 |
typedef CountBiNodeConnectedComponentsVisitor<Graph> Visitor; |
| 782 | 782 |
|
| 783 | 783 |
int compNum = 0; |
| 784 | 784 |
Visitor visitor(graph, compNum); |
| 785 | 785 |
|
| ... | ... |
@@ -791,95 +791,95 @@ |
| 791 | 791 |
dfs.addSource(it); |
| 792 | 792 |
dfs.start(); |
| 793 | 793 |
} |
| 794 | 794 |
} |
| 795 | 795 |
return compNum; |
| 796 | 796 |
} |
| 797 | 797 |
|
| 798 | 798 |
/// \ingroup graph_properties |
| 799 | 799 |
/// |
| 800 | 800 |
/// \brief Find the bi-node-connected components of an undirected graph. |
| 801 | 801 |
/// |
| 802 | 802 |
/// This function finds the bi-node-connected components of the given |
| 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 |
|
| 828 | 828 |
using namespace _connectivity_bits; |
| 829 | 829 |
|
| 830 | 830 |
typedef BiNodeConnectedComponentsVisitor<Graph, EdgeMap> Visitor; |
| 831 | 831 |
|
| 832 | 832 |
int compNum = 0; |
| 833 | 833 |
Visitor visitor(graph, compMap, compNum); |
| 834 | 834 |
|
| 835 | 835 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
| 836 | 836 |
dfs.init(); |
| 837 | 837 |
|
| 838 | 838 |
for (NodeIt it(graph); it != INVALID; ++it) {
|
| 839 | 839 |
if (!dfs.reached(it)) {
|
| 840 | 840 |
dfs.addSource(it); |
| 841 | 841 |
dfs.start(); |
| 842 | 842 |
} |
| 843 | 843 |
} |
| 844 | 844 |
return compNum; |
| 845 | 845 |
} |
| 846 | 846 |
|
| 847 | 847 |
/// \ingroup graph_properties |
| 848 | 848 |
/// |
| 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>(); |
| 874 | 874 |
|
| 875 | 875 |
using namespace _connectivity_bits; |
| 876 | 876 |
|
| 877 | 877 |
typedef BiNodeConnectedCutNodesVisitor<Graph, NodeMap> Visitor; |
| 878 | 878 |
|
| 879 | 879 |
int cutNum = 0; |
| 880 | 880 |
Visitor visitor(graph, cutMap, cutNum); |
| 881 | 881 |
|
| 882 | 882 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
| 883 | 883 |
dfs.init(); |
| 884 | 884 |
|
| 885 | 885 |
for (NodeIt it(graph); it != INVALID; ++it) {
|
| ... | ... |
@@ -1064,49 +1064,49 @@ |
| 1064 | 1064 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) {
|
| 1065 | 1065 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
| 1066 | 1066 |
} |
| 1067 | 1067 |
} |
| 1068 | 1068 |
|
| 1069 | 1069 |
private: |
| 1070 | 1070 |
const Digraph& _graph; |
| 1071 | 1071 |
ArcMap& _cutMap; |
| 1072 | 1072 |
int& _cutNum; |
| 1073 | 1073 |
|
| 1074 | 1074 |
typename Digraph::template NodeMap<int> _numMap; |
| 1075 | 1075 |
typename Digraph::template NodeMap<int> _retMap; |
| 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 |
|
| 1101 | 1101 |
/// \ingroup graph_properties |
| 1102 | 1102 |
/// |
| 1103 | 1103 |
/// \brief Count the number of bi-edge-connected components of an |
| 1104 | 1104 |
/// undirected graph. |
| 1105 | 1105 |
/// |
| 1106 | 1106 |
/// This function counts the number of bi-edge-connected components of |
| 1107 | 1107 |
/// the given undirected graph. |
| 1108 | 1108 |
/// |
| 1109 | 1109 |
/// The bi-edge-connected components are the classes of an equivalence |
| 1110 | 1110 |
/// relation on the nodes of an undirected graph. Two nodes are in the |
| 1111 | 1111 |
/// same class if they are connected with at least two edge-disjoint |
| 1112 | 1112 |
/// paths. |
| ... | ... |
@@ -1171,49 +1171,49 @@ |
| 1171 | 1171 |
using namespace _connectivity_bits; |
| 1172 | 1172 |
|
| 1173 | 1173 |
typedef BiEdgeConnectedComponentsVisitor<Graph, NodeMap> Visitor; |
| 1174 | 1174 |
|
| 1175 | 1175 |
int compNum = 0; |
| 1176 | 1176 |
Visitor visitor(graph, compMap, compNum); |
| 1177 | 1177 |
|
| 1178 | 1178 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
| 1179 | 1179 |
dfs.init(); |
| 1180 | 1180 |
|
| 1181 | 1181 |
for (NodeIt it(graph); it != INVALID; ++it) {
|
| 1182 | 1182 |
if (!dfs.reached(it)) {
|
| 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. |
| 1208 | 1208 |
/// \return The number of cut edges. |
| 1209 | 1209 |
/// |
| 1210 | 1210 |
/// \see biEdgeConnected(), biEdgeConnectedComponents() |
| 1211 | 1211 |
template <typename Graph, typename EdgeMap> |
| 1212 | 1212 |
int biEdgeConnectedCutEdges(const Graph& graph, EdgeMap& cutMap) {
|
| 1213 | 1213 |
checkConcept<concepts::Graph, Graph>(); |
| 1214 | 1214 |
typedef typename Graph::NodeIt NodeIt; |
| 1215 | 1215 |
typedef typename Graph::Edge Edge; |
| 1216 | 1216 |
checkConcept<concepts::WriteMap<Edge, bool>, EdgeMap>(); |
| 1217 | 1217 |
|
| 1218 | 1218 |
using namespace _connectivity_bits; |
| 1219 | 1219 |
|
| ... | ... |
@@ -1328,49 +1328,49 @@ |
| 1328 | 1328 |
visitor(order, countNodes(digraph)); |
| 1329 | 1329 |
|
| 1330 | 1330 |
DfsVisit<Digraph, TopologicalSortVisitor<Digraph, NodeMap> > |
| 1331 | 1331 |
dfs(digraph, visitor); |
| 1332 | 1332 |
|
| 1333 | 1333 |
dfs.init(); |
| 1334 | 1334 |
for (NodeIt it(digraph); it != INVALID; ++it) {
|
| 1335 | 1335 |
if (!dfs.reached(it)) {
|
| 1336 | 1336 |
dfs.addSource(it); |
| 1337 | 1337 |
dfs.start(); |
| 1338 | 1338 |
} |
| 1339 | 1339 |
} |
| 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>(); |
| 1365 | 1365 |
|
| 1366 | 1366 |
typedef typename Digraph::Node Node; |
| 1367 | 1367 |
typedef typename Digraph::NodeIt NodeIt; |
| 1368 | 1368 |
typedef typename Digraph::Arc Arc; |
| 1369 | 1369 |
|
| 1370 | 1370 |
for (NodeIt it(digraph); it != INVALID; ++it) {
|
| 1371 | 1371 |
order.set(it, -1); |
| 1372 | 1372 |
} |
| 1373 | 1373 |
|
| 1374 | 1374 |
TopologicalSortVisitor<Digraph, NodeMap> |
| 1375 | 1375 |
visitor(order, countNodes(digraph)); |
| 1376 | 1376 |
| 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_CORE_H |
| 20 | 20 |
#define LEMON_CORE_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <algorithm> |
| 24 | 24 |
|
| 25 | 25 |
#include <lemon/config.h> |
| 26 | 26 |
#include <lemon/bits/enable_if.h> |
| 27 | 27 |
#include <lemon/bits/traits.h> |
| 28 | 28 |
#include <lemon/assert.h> |
| 29 | 29 |
|
| ... | ... |
@@ -1218,88 +1218,89 @@ |
| 1218 | 1218 |
///time bound for arc look-ups. This class also guarantees the |
| 1219 | 1219 |
///optimal time bound in a constant factor for any distribution of |
| 1220 | 1220 |
///queries. |
| 1221 | 1221 |
/// |
| 1222 | 1222 |
///\tparam GR The type of the underlying digraph. |
| 1223 | 1223 |
/// |
| 1224 | 1224 |
///\sa ArcLookUp |
| 1225 | 1225 |
///\sa AllArcLookUp |
| 1226 | 1226 |
template <typename GR> |
| 1227 | 1227 |
class DynArcLookUp |
| 1228 | 1228 |
: protected ItemSetTraits<GR, typename GR::Arc>::ItemNotifier::ObserverBase |
| 1229 | 1229 |
{
|
| 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) {
|
| 1255 | 1256 |
Parent::add(nodes); |
| 1256 | 1257 |
for (int i = 0; i < int(nodes.size()); ++i) {
|
| 1257 | 1258 |
Parent::set(nodes[i], INVALID); |
| 1258 | 1259 |
} |
| 1259 | 1260 |
} |
| 1260 | 1261 |
|
| 1261 | 1262 |
virtual void build() {
|
| 1262 | 1263 |
Parent::build(); |
| 1263 | 1264 |
Node it; |
| 1264 | 1265 |
typename Parent::Notifier* nf = Parent::notifier(); |
| 1265 | 1266 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 1266 | 1267 |
Parent::set(it, INVALID); |
| 1267 | 1268 |
} |
| 1268 | 1269 |
} |
| 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. |
| 1294 | 1295 |
/// |
| 1295 | 1296 |
///It builds up the search database. |
| 1296 | 1297 |
DynArcLookUp(const Digraph &g) |
| 1297 | 1298 |
: _g(g),_head(g),_parent(g),_left(g),_right(g) |
| 1298 | 1299 |
{
|
| 1299 | 1300 |
Parent::attach(_g.notifier(typename Digraph::Arc())); |
| 1300 | 1301 |
refresh(); |
| 1301 | 1302 |
} |
| 1302 | 1303 |
|
| 1303 | 1304 |
protected: |
| 1304 | 1305 |
|
| 1305 | 1306 |
virtual void add(const Arc& arc) {
|
| 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_COST_SCALING_H |
| 20 | 20 |
#define LEMON_COST_SCALING_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup min_cost_flow_algs |
| 23 | 23 |
/// \file |
| 24 | 24 |
/// \brief Cost scaling algorithm for finding a minimum cost flow. |
| 25 | 25 |
|
| 26 | 26 |
#include <vector> |
| 27 | 27 |
#include <deque> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
|
| ... | ... |
@@ -71,49 +71,49 @@ |
| 71 | 71 |
// Default traits class for integer cost types |
| 72 | 72 |
template <typename GR, typename V, typename C> |
| 73 | 73 |
struct CostScalingDefaultTraits<GR, V, C, true> |
| 74 | 74 |
{
|
| 75 | 75 |
typedef GR Digraph; |
| 76 | 76 |
typedef V Value; |
| 77 | 77 |
typedef C Cost; |
| 78 | 78 |
#ifdef LEMON_HAVE_LONG_LONG |
| 79 | 79 |
typedef long long LargeCost; |
| 80 | 80 |
#else |
| 81 | 81 |
typedef long LargeCost; |
| 82 | 82 |
#endif |
| 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. |
| 108 | 108 |
/// \tparam C The number type used for costs and potentials in the |
| 109 | 109 |
/// algorithm. By default, it is the same as \c V. |
| 110 | 110 |
/// \tparam TR The traits class that defines various types used by the |
| 111 | 111 |
/// algorithm. By default, it is \ref CostScalingDefaultTraits |
| 112 | 112 |
/// "CostScalingDefaultTraits<GR, V, C>". |
| 113 | 113 |
/// In most cases, this parameter should not be set directly, |
| 114 | 114 |
/// consider to use the named template parameters instead. |
| 115 | 115 |
/// |
| 116 | 116 |
/// \warning Both number types must be signed and all input data must |
| 117 | 117 |
/// be integer. |
| 118 | 118 |
/// \warning This algorithm does not support negative costs for such |
| 119 | 119 |
/// arcs that have infinite upper bound. |
| ... | ... |
@@ -168,84 +168,84 @@ |
| 168 | 168 |
/// these cases. |
| 169 | 169 |
UNBOUNDED |
| 170 | 170 |
}; |
| 171 | 171 |
|
| 172 | 172 |
/// \brief Constants for selecting the internal method. |
| 173 | 173 |
/// |
| 174 | 174 |
/// Enum type containing constants for selecting the internal method |
| 175 | 175 |
/// for the \ref run() function. |
| 176 | 176 |
/// |
| 177 | 177 |
/// \ref CostScaling provides three internal methods that differ mainly |
| 178 | 178 |
/// in their base operations, which are used in conjunction with the |
| 179 | 179 |
/// relabel operation. |
| 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: |
| 240 | 240 |
|
| 241 | 241 |
// Data related to the underlying digraph |
| 242 | 242 |
const GR &_graph; |
| 243 | 243 |
int _node_num; |
| 244 | 244 |
int _arc_num; |
| 245 | 245 |
int _res_node_num; |
| 246 | 246 |
int _res_arc_num; |
| 247 | 247 |
int _root; |
| 248 | 248 |
|
| 249 | 249 |
// Parameters of the problem |
| 250 | 250 |
bool _have_lower; |
| 251 | 251 |
Value _sum_supply; |
| ... | ... |
@@ -262,59 +262,59 @@ |
| 262 | 262 |
IntVector _reverse; |
| 263 | 263 |
|
| 264 | 264 |
// Node and arc data |
| 265 | 265 |
ValueVector _lower; |
| 266 | 266 |
ValueVector _upper; |
| 267 | 267 |
CostVector _scost; |
| 268 | 268 |
ValueVector _supply; |
| 269 | 269 |
|
| 270 | 270 |
ValueVector _res_cap; |
| 271 | 271 |
LargeCostVector _cost; |
| 272 | 272 |
LargeCostVector _pi; |
| 273 | 273 |
ValueVector _excess; |
| 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 |
|
| 309 | 309 |
template <typename T> |
| 310 | 310 |
struct SetLargeCostTraits : public Traits {
|
| 311 | 311 |
typedef T LargeCost; |
| 312 | 312 |
}; |
| 313 | 313 |
|
| 314 | 314 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 315 | 315 |
/// \c LargeCost type. |
| 316 | 316 |
/// |
| 317 | 317 |
/// \ref named-templ-param "Named parameter" for setting \c LargeCost |
| 318 | 318 |
/// type, which is used for internal computations in the algorithm. |
| 319 | 319 |
/// \c Cost must be convertible to \c LargeCost. |
| 320 | 320 |
template <typename T> |
| ... | ... |
@@ -327,49 +327,49 @@ |
| 327 | 327 |
|
| 328 | 328 |
protected: |
| 329 | 329 |
|
| 330 | 330 |
CostScaling() {}
|
| 331 | 331 |
|
| 332 | 332 |
public: |
| 333 | 333 |
|
| 334 | 334 |
/// \brief Constructor. |
| 335 | 335 |
/// |
| 336 | 336 |
/// The constructor of the class. |
| 337 | 337 |
/// |
| 338 | 338 |
/// \param graph The digraph the algorithm runs on. |
| 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 |
/// |
| 364 | 364 |
/// This function sets the lower bounds on the arcs. |
| 365 | 365 |
/// If it is not used before calling \ref run(), the lower bounds |
| 366 | 366 |
/// will be set to zero on all arcs. |
| 367 | 367 |
/// |
| 368 | 368 |
/// \param map An arc map storing the lower bounds. |
| 369 | 369 |
/// Its \c Value type must be convertible to the \c Value type |
| 370 | 370 |
/// of the algorithm. |
| 371 | 371 |
/// |
| 372 | 372 |
/// \return <tt>(*this)</tt> |
| 373 | 373 |
template <typename LowerMap> |
| 374 | 374 |
CostScaling& lowerMap(const LowerMap& map) {
|
| 375 | 375 |
_have_lower = true; |
| ... | ... |
@@ -443,49 +443,49 @@ |
| 443 | 443 |
/// |
| 444 | 444 |
/// This function sets a single source node and a single target node |
| 445 | 445 |
/// and the required flow value. |
| 446 | 446 |
/// If neither this function nor \ref supplyMap() is used before |
| 447 | 447 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 448 | 448 |
/// |
| 449 | 449 |
/// Using this function has the same effect as using \ref supplyMap() |
| 450 | 450 |
/// with such a map in which \c k is assigned to \c s, \c -k is |
| 451 | 451 |
/// assigned to \c t and all other nodes have zero supply value. |
| 452 | 452 |
/// |
| 453 | 453 |
/// \param s The source node. |
| 454 | 454 |
/// \param t The target node. |
| 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(). |
| 480 | 480 |
/// For example, |
| 481 | 481 |
/// \code |
| 482 | 482 |
/// CostScaling<ListDigraph> cs(graph); |
| 483 | 483 |
/// cs.lowerMap(lower).upperMap(upper).costMap(cost) |
| 484 | 484 |
/// .supplyMap(sup).run(); |
| 485 | 485 |
/// \endcode |
| 486 | 486 |
/// |
| 487 | 487 |
/// This function can be called more than once. All the given parameters |
| 488 | 488 |
/// are kept for the next call, unless \ref resetParams() or \ref reset() |
| 489 | 489 |
/// is used, thus only the modified parameters have to be set again. |
| 490 | 490 |
/// If the underlying digraph was also modified after the construction |
| 491 | 491 |
/// of the class (or the last \ref reset() call), then the \ref reset() |
| ... | ... |
@@ -545,132 +545,132 @@ |
| 545 | 545 |
/// // (the lower bounds will be set to zero on all arcs) |
| 546 | 546 |
/// cs.resetParams(); |
| 547 | 547 |
/// cs.upperMap(capacity).costMap(cost) |
| 548 | 548 |
/// .supplyMap(sup).run(); |
| 549 | 549 |
/// \endcode |
| 550 | 550 |
/// |
| 551 | 551 |
/// \return <tt>(*this)</tt> |
| 552 | 552 |
/// |
| 553 | 553 |
/// \see reset(), run() |
| 554 | 554 |
CostScaling& resetParams() {
|
| 555 | 555 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 556 | 556 |
_supply[i] = 0; |
| 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 |
| 582 | 582 |
/// \ref run() call. |
| 583 | 583 |
/// However, the underlying digraph must not be modified after this |
| 584 | 584 |
/// class have been constructed, since it copies and extends the graph. |
| 585 | 585 |
/// \return <tt>(*this)</tt> |
| 586 | 586 |
CostScaling& reset() {
|
| 587 | 587 |
// Resize vectors |
| 588 | 588 |
_node_num = countNodes(_graph); |
| 589 | 589 |
_arc_num = countArcs(_graph); |
| 590 | 590 |
_res_node_num = _node_num + 1; |
| 591 | 591 |
_res_arc_num = 2 * (_arc_num + _node_num); |
| 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) {
|
| 617 | 617 |
_node_id[n] = i; |
| 618 | 618 |
} |
| 619 | 619 |
i = 0; |
| 620 | 620 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
| 621 | 621 |
_first_out[i] = j; |
| 622 | 622 |
for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) {
|
| 623 | 623 |
_arc_idf[a] = j; |
| 624 | 624 |
_forward[j] = true; |
| 625 | 625 |
_source[j] = i; |
| 626 | 626 |
_target[j] = _node_id[_graph.runningNode(a)]; |
| 627 | 627 |
} |
| 628 | 628 |
for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) {
|
| 629 | 629 |
_arc_idb[a] = j; |
| 630 | 630 |
_forward[j] = false; |
| 631 | 631 |
_source[j] = i; |
| 632 | 632 |
_target[j] = _node_id[_graph.runningNode(a)]; |
| 633 | 633 |
} |
| 634 | 634 |
_forward[j] = false; |
| 635 | 635 |
_source[j] = i; |
| 636 | 636 |
_target[j] = _root; |
| 637 | 637 |
_reverse[j] = k; |
| 638 | 638 |
_forward[k] = true; |
| 639 | 639 |
_source[k] = _root; |
| 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 |
|
| 665 | 665 |
/// @{
|
| 666 | 666 |
|
| 667 | 667 |
/// \brief Return the total cost of the found flow. |
| 668 | 668 |
/// |
| 669 | 669 |
/// This function returns the total cost of the found flow. |
| 670 | 670 |
/// Its complexity is O(e). |
| 671 | 671 |
/// |
| 672 | 672 |
/// \note The return type of the function can be specified as a |
| 673 | 673 |
/// template parameter. For example, |
| 674 | 674 |
/// \code |
| 675 | 675 |
/// cs.totalCost<double>(); |
| 676 | 676 |
/// \endcode |
| ... | ... |
@@ -737,56 +737,56 @@ |
| 737 | 737 |
/// \c Value type of the map. |
| 738 | 738 |
/// |
| 739 | 739 |
/// \pre \ref run() must be called before using this function. |
| 740 | 740 |
template <typename PotentialMap> |
| 741 | 741 |
void potentialMap(PotentialMap &map) const {
|
| 742 | 742 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 743 | 743 |
map.set(n, static_cast<Cost>(_pi[_node_id[n]])); |
| 744 | 744 |
} |
| 745 | 745 |
} |
| 746 | 746 |
|
| 747 | 747 |
/// @} |
| 748 | 748 |
|
| 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; |
| 781 | 781 |
} |
| 782 | 782 |
} |
| 783 | 783 |
} |
| 784 | 784 |
} else {
|
| 785 | 785 |
for (int i = 0; i != _root; ++i) {
|
| 786 | 786 |
last_out = _first_out[i+1]; |
| 787 | 787 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 788 | 788 |
if (_forward[j] && _scost[j] < 0) {
|
| 789 | 789 |
Value c = _upper[j]; |
| 790 | 790 |
if (c >= MAX) return UNBOUNDED; |
| 791 | 791 |
_excess[i] -= c; |
| 792 | 792 |
_excess[_target[j]] += c; |
| ... | ... |
@@ -864,271 +864,271 @@ |
| 864 | 864 |
} |
| 865 | 865 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
| 866 | 866 |
int u = _target[a]; |
| 867 | 867 |
int ra = _reverse[a]; |
| 868 | 868 |
_res_cap[a] = -_sum_supply + 1; |
| 869 | 869 |
_res_cap[ra] = -_excess[u]; |
| 870 | 870 |
_cost[a] = 0; |
| 871 | 871 |
_cost[ra] = 0; |
| 872 | 872 |
_excess[u] = 0; |
| 873 | 873 |
} |
| 874 | 874 |
} else {
|
| 875 | 875 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 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 |
} |
| 916 | 916 |
|
| 917 | 917 |
// Compute node potentials for the original costs |
| 918 | 918 |
_arc_vec.clear(); |
| 919 | 919 |
_cost_vec.clear(); |
| 920 | 920 |
for (int j = 0; j != _res_arc_num; ++j) {
|
| 921 | 921 |
if (_res_cap[j] > 0) {
|
| 922 | 922 |
_arc_vec.push_back(IntPair(_source[j], _target[j])); |
| 923 | 923 |
_cost_vec.push_back(_scost[j]); |
| 924 | 924 |
} |
| 925 | 925 |
} |
| 926 | 926 |
_sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); |
| 927 | 927 |
|
| 928 | 928 |
typename BellmanFord<StaticDigraph, LargeCostArcMap> |
| 929 | 929 |
::template SetDistMap<LargeCostNodeMap>::Create bf(_sgr, _cost_map); |
| 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 |
} |
| 984 | 984 |
} |
| 985 | 985 |
_sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); |
| 986 | 986 |
|
| 987 | 987 |
// Run Bellman-Ford algorithm to check if the current flow is optimal |
| 988 | 988 |
BellmanFord<StaticDigraph, LargeCostArcMap> bf(_sgr, _cost_map); |
| 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 {
|
| 1014 | 1014 |
total_excess += _excess[i]; |
| 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(); |
| 1123 | 1123 |
int tip = start; |
| 1124 | 1124 |
while (_excess[tip] >= 0 && int(path.size()) < max_length) {
|
| 1125 | 1125 |
int u; |
| 1126 | 1126 |
LargeCost min_red_cost, rc, pi_tip = _pi[tip]; |
| 1127 | 1127 |
int last_out = _first_out[tip+1]; |
| 1128 | 1128 |
for (int a = _next_out[tip]; a != last_out; ++a) {
|
| 1129 | 1129 |
u = _target[a]; |
| 1130 | 1130 |
if (_res_cap[a] > 0 && _cost[a] + pi_tip - _pi[u] < 0) {
|
| 1131 | 1131 |
path.push_back(a); |
| 1132 | 1132 |
_next_out[tip] = a; |
| 1133 | 1133 |
tip = u; |
| 1134 | 1134 |
goto next_step; |
| ... | ... |
@@ -1175,89 +1175,89 @@ |
| 1175 | 1175 |
if (_excess[v] > 0 && _excess[v] <= delta) |
| 1176 | 1176 |
_active_nodes.push_back(v); |
| 1177 | 1177 |
} |
| 1178 | 1178 |
|
| 1179 | 1179 |
// Global update heuristic |
| 1180 | 1180 |
if (relabel_cnt >= next_update_limit) {
|
| 1181 | 1181 |
globalUpdate(); |
| 1182 | 1182 |
next_update_limit += global_update_freq; |
| 1183 | 1183 |
} |
| 1184 | 1184 |
} |
| 1185 | 1185 |
} |
| 1186 | 1186 |
} |
| 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; |
| 1252 | 1252 |
_active_nodes.push_front(t); |
| 1253 | 1253 |
hyper[t] = true; |
| 1254 | 1254 |
hyper_cost[t] = _cost[a] + pi_n - pi_t; |
| 1255 | 1255 |
_next_out[n] = a; |
| 1256 | 1256 |
goto next_node; |
| 1257 | 1257 |
} else {
|
| 1258 | 1258 |
_res_cap[a] -= delta; |
| 1259 | 1259 |
_res_cap[_reverse[a]] += delta; |
| 1260 | 1260 |
_excess[n] -= delta; |
| 1261 | 1261 |
_excess[t] += delta; |
| 1262 | 1262 |
if (_excess[t] > 0 && _excess[t] <= delta) |
| 1263 | 1263 |
_active_nodes.push_back(t); |
| ... | ... |
@@ -1266,51 +1266,51 @@ |
| 1266 | 1266 |
if (_excess[n] == 0) {
|
| 1267 | 1267 |
_next_out[n] = a; |
| 1268 | 1268 |
goto remove_nodes; |
| 1269 | 1269 |
} |
| 1270 | 1270 |
} |
| 1271 | 1271 |
} |
| 1272 | 1272 |
_next_out[n] = a; |
| 1273 | 1273 |
} |
| 1274 | 1274 |
|
| 1275 | 1275 |
// Relabel the node if it is still active (or hyper) |
| 1276 | 1276 |
if (_excess[n] > 0 || hyper[n]) {
|
| 1277 | 1277 |
min_red_cost = hyper[n] ? -hyper_cost[n] : |
| 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 |
| 1311 | 1311 |
|
| 1312 | 1312 |
///@} |
| 1313 | 1313 |
|
| 1314 | 1314 |
} //namespace lemon |
| 1315 | 1315 |
|
| 1316 | 1316 |
#endif //LEMON_COST_SCALING_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 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <iostream> |
| 20 | 20 |
#include <vector> |
| 21 | 21 |
#include <cstring> |
| 22 | 22 |
|
| 23 | 23 |
#include <lemon/cplex.h> |
| 24 | 24 |
|
| 25 | 25 |
extern "C" {
|
| 26 | 26 |
#include <ilcplex/cplex.h> |
| 27 | 27 |
} |
| 28 | 28 |
|
| 29 | 29 |
|
| ... | ... |
@@ -90,49 +90,49 @@ |
| 90 | 90 |
cols = cplex.cols; |
| 91 | 91 |
messageLevel(MESSAGE_NOTHING); |
| 92 | 92 |
} |
| 93 | 93 |
|
| 94 | 94 |
CplexBase::~CplexBase() {
|
| 95 | 95 |
CPXfreeprob(cplexEnv(),&_prob); |
| 96 | 96 |
} |
| 97 | 97 |
|
| 98 | 98 |
int CplexBase::_addCol() {
|
| 99 | 99 |
int i = CPXgetnumcols(cplexEnv(), _prob); |
| 100 | 100 |
double lb = -INF, ub = INF; |
| 101 | 101 |
CPXnewcols(cplexEnv(), _prob, 1, 0, &lb, &ub, 0, 0); |
| 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 {
|
| 127 | 127 |
const char s = 'R'; |
| 128 | 128 |
double len = ub - lb; |
| 129 | 129 |
CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, &len, 0); |
| 130 | 130 |
} |
| 131 | 131 |
|
| 132 | 132 |
std::vector<int> indices; |
| 133 | 133 |
std::vector<int> rowlist; |
| 134 | 134 |
std::vector<Value> values; |
| 135 | 135 |
|
| 136 | 136 |
for(ExprIterator it=b; it!=e; ++it) {
|
| 137 | 137 |
indices.push_back(it->first); |
| 138 | 138 |
values.push_back(it->second); |
| ... | ... |
@@ -468,49 +468,49 @@ |
| 468 | 468 |
|
| 469 | 469 |
void CplexBase::_clear() {
|
| 470 | 470 |
CPXfreeprob(cplexEnv(),&_prob); |
| 471 | 471 |
int status; |
| 472 | 472 |
_prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem"); |
| 473 | 473 |
rows.clear(); |
| 474 | 474 |
cols.clear(); |
| 475 | 475 |
} |
| 476 | 476 |
|
| 477 | 477 |
void CplexBase::_messageLevel(MessageLevel level) {
|
| 478 | 478 |
switch (level) {
|
| 479 | 479 |
case MESSAGE_NOTHING: |
| 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) |
| 505 | 505 |
: LpBase(), LpSolver(), CplexBase(other) {}
|
| 506 | 506 |
|
| 507 | 507 |
CplexLp::~CplexLp() {}
|
| 508 | 508 |
|
| 509 | 509 |
CplexLp* CplexLp::newSolver() const { return new CplexLp; }
|
| 510 | 510 |
CplexLp* CplexLp::cloneSolver() const {return new CplexLp(*this); }
|
| 511 | 511 |
|
| 512 | 512 |
const char* CplexLp::_solverName() const { return "CplexLp"; }
|
| 513 | 513 |
|
| 514 | 514 |
void CplexLp::_clear_temporals() {
|
| 515 | 515 |
_col_status.clear(); |
| 516 | 516 |
_row_status.clear(); |
| 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_CYCLE_CANCELING_H |
| 20 | 20 |
#define LEMON_CYCLE_CANCELING_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup min_cost_flow_algs |
| 23 | 23 |
/// \file |
| 24 | 24 |
/// \brief Cycle-canceling algorithms for finding a minimum cost flow. |
| 25 | 25 |
|
| 26 | 26 |
#include <vector> |
| 27 | 27 |
#include <limits> |
| 28 | 28 |
|
| 29 | 29 |
#include <lemon/core.h> |
| ... | ... |
@@ -121,74 +121,74 @@ |
| 121 | 121 |
/// However, the other methods can be selected using the \ref run() |
| 122 | 122 |
/// function with the proper parameter. |
| 123 | 123 |
enum Method {
|
| 124 | 124 |
/// A simple cycle-canceling method, which uses the |
| 125 | 125 |
/// \ref BellmanFord "Bellman-Ford" algorithm with limited iteration |
| 126 | 126 |
/// number for detecting negative cycles in the residual network. |
| 127 | 127 |
SIMPLE_CYCLE_CANCELING, |
| 128 | 128 |
/// The "Minimum Mean Cycle-Canceling" algorithm, which is a |
| 129 | 129 |
/// well-known strongly polynomial method |
| 130 | 130 |
/// \ref goldberg89cyclecanceling. It improves along a |
| 131 | 131 |
/// \ref min_mean_cycle "minimum mean cycle" in each iteration. |
| 132 | 132 |
/// Its running time complexity is O(n<sup>2</sup>m<sup>3</sup>log(n)). |
| 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: |
| 183 | 183 |
|
| 184 | 184 |
|
| 185 | 185 |
// Data related to the underlying digraph |
| 186 | 186 |
const GR &_graph; |
| 187 | 187 |
int _node_num; |
| 188 | 188 |
int _arc_num; |
| 189 | 189 |
int _res_node_num; |
| 190 | 190 |
int _res_arc_num; |
| 191 | 191 |
int _root; |
| 192 | 192 |
|
| 193 | 193 |
// Parameters of the problem |
| 194 | 194 |
bool _have_lower; |
| ... | ... |
@@ -200,51 +200,51 @@ |
| 200 | 200 |
IntArcMap _arc_idb; |
| 201 | 201 |
IntVector _first_out; |
| 202 | 202 |
BoolVector _forward; |
| 203 | 203 |
IntVector _source; |
| 204 | 204 |
IntVector _target; |
| 205 | 205 |
IntVector _reverse; |
| 206 | 206 |
|
| 207 | 207 |
// Node and arc data |
| 208 | 208 |
ValueVector _lower; |
| 209 | 209 |
ValueVector _upper; |
| 210 | 210 |
CostVector _cost; |
| 211 | 211 |
ValueVector _supply; |
| 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. |
| 239 | 239 |
/// |
| 240 | 240 |
/// \param graph The digraph the algorithm runs on. |
| 241 | 241 |
CycleCanceling(const GR& graph) : |
| 242 | 242 |
_graph(graph), _node_id(graph), _arc_idf(graph), _arc_idb(graph), |
| 243 | 243 |
_cost_map(_cost_vec), _pi_map(_pi), |
| 244 | 244 |
INF(std::numeric_limits<Value>::has_infinity ? |
| 245 | 245 |
std::numeric_limits<Value>::infinity() : |
| 246 | 246 |
std::numeric_limits<Value>::max()) |
| 247 | 247 |
{
|
| 248 | 248 |
// Check the number types |
| 249 | 249 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
| 250 | 250 |
"The flow type of CycleCanceling must be signed"); |
| ... | ... |
@@ -345,49 +345,49 @@ |
| 345 | 345 |
/// |
| 346 | 346 |
/// This function sets a single source node and a single target node |
| 347 | 347 |
/// and the required flow value. |
| 348 | 348 |
/// If neither this function nor \ref supplyMap() is used before |
| 349 | 349 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 350 | 350 |
/// |
| 351 | 351 |
/// Using this function has the same effect as using \ref supplyMap() |
| 352 | 352 |
/// with such a map in which \c k is assigned to \c s, \c -k is |
| 353 | 353 |
/// assigned to \c t and all other nodes have zero supply value. |
| 354 | 354 |
/// |
| 355 | 355 |
/// \param s The source node. |
| 356 | 356 |
/// \param t The target node. |
| 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(). |
| 382 | 382 |
/// For example, |
| 383 | 383 |
/// \code |
| 384 | 384 |
/// CycleCanceling<ListDigraph> cc(graph); |
| 385 | 385 |
/// cc.lowerMap(lower).upperMap(upper).costMap(cost) |
| 386 | 386 |
/// .supplyMap(sup).run(); |
| 387 | 387 |
/// \endcode |
| 388 | 388 |
/// |
| 389 | 389 |
/// This function can be called more than once. All the given parameters |
| 390 | 390 |
/// are kept for the next call, unless \ref resetParams() or \ref reset() |
| 391 | 391 |
/// is used, thus only the modified parameters have to be set again. |
| 392 | 392 |
/// If the underlying digraph was also modified after the construction |
| 393 | 393 |
/// of the class (or the last \ref reset() call), then the \ref reset() |
| ... | ... |
@@ -445,137 +445,137 @@ |
| 445 | 445 |
/// // (the lower bounds will be set to zero on all arcs) |
| 446 | 446 |
/// cc.resetParams(); |
| 447 | 447 |
/// cc.upperMap(capacity).costMap(cost) |
| 448 | 448 |
/// .supplyMap(sup).run(); |
| 449 | 449 |
/// \endcode |
| 450 | 450 |
/// |
| 451 | 451 |
/// \return <tt>(*this)</tt> |
| 452 | 452 |
/// |
| 453 | 453 |
/// \see reset(), run() |
| 454 | 454 |
CycleCanceling& resetParams() {
|
| 455 | 455 |
for (int i = 0; i != _res_node_num; ++i) {
|
| 456 | 456 |
_supply[i] = 0; |
| 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 |
| 482 | 482 |
/// parameters are kept for the next \ref run() call, unless |
| 483 | 483 |
/// \ref resetParams() or \ref reset() is used. |
| 484 | 484 |
/// If the underlying digraph was also modified after the construction |
| 485 | 485 |
/// of the class or the last \ref reset() call, then the \ref reset() |
| 486 | 486 |
/// function must be used, otherwise \ref resetParams() is sufficient. |
| 487 | 487 |
/// |
| 488 | 488 |
/// See \ref resetParams() for examples. |
| 489 | 489 |
/// |
| 490 | 490 |
/// \return <tt>(*this)</tt> |
| 491 | 491 |
/// |
| 492 | 492 |
/// \see resetParams(), run() |
| 493 | 493 |
CycleCanceling& reset() {
|
| 494 | 494 |
// Resize vectors |
| 495 | 495 |
_node_num = countNodes(_graph); |
| 496 | 496 |
_arc_num = countArcs(_graph); |
| 497 | 497 |
_res_node_num = _node_num + 1; |
| 498 | 498 |
_res_arc_num = 2 * (_arc_num + _node_num); |
| 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 |
} |
| 524 | 524 |
i = 0; |
| 525 | 525 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
| 526 | 526 |
_first_out[i] = j; |
| 527 | 527 |
for (OutArcIt a(_graph, n); a != INVALID; ++a, ++j) {
|
| 528 | 528 |
_arc_idf[a] = j; |
| 529 | 529 |
_forward[j] = true; |
| 530 | 530 |
_source[j] = i; |
| 531 | 531 |
_target[j] = _node_id[_graph.runningNode(a)]; |
| 532 | 532 |
} |
| 533 | 533 |
for (InArcIt a(_graph, n); a != INVALID; ++a, ++j) {
|
| 534 | 534 |
_arc_idb[a] = j; |
| 535 | 535 |
_forward[j] = false; |
| 536 | 536 |
_source[j] = i; |
| 537 | 537 |
_target[j] = _node_id[_graph.runningNode(a)]; |
| 538 | 538 |
} |
| 539 | 539 |
_forward[j] = false; |
| 540 | 540 |
_source[j] = i; |
| 541 | 541 |
_target[j] = _root; |
| 542 | 542 |
_reverse[j] = k; |
| 543 | 543 |
_forward[k] = true; |
| 544 | 544 |
_source[k] = _root; |
| 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 |
|
| 570 | 570 |
/// @{
|
| 571 | 571 |
|
| 572 | 572 |
/// \brief Return the total cost of the found flow. |
| 573 | 573 |
/// |
| 574 | 574 |
/// This function returns the total cost of the found flow. |
| 575 | 575 |
/// Its complexity is O(e). |
| 576 | 576 |
/// |
| 577 | 577 |
/// \note The return type of the function can be specified as a |
| 578 | 578 |
/// template parameter. For example, |
| 579 | 579 |
/// \code |
| 580 | 580 |
/// cc.totalCost<double>(); |
| 581 | 581 |
/// \endcode |
| ... | ... |
@@ -642,56 +642,56 @@ |
| 642 | 642 |
/// \c Value type of the map. |
| 643 | 643 |
/// |
| 644 | 644 |
/// \pre \ref run() must be called before using this function. |
| 645 | 645 |
template <typename PotentialMap> |
| 646 | 646 |
void potentialMap(PotentialMap &map) const {
|
| 647 | 647 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 648 | 648 |
map.set(n, static_cast<Cost>(_pi[_node_id[n]])); |
| 649 | 649 |
} |
| 650 | 650 |
} |
| 651 | 651 |
|
| 652 | 652 |
/// @} |
| 653 | 653 |
|
| 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; |
| 686 | 686 |
} |
| 687 | 687 |
} |
| 688 | 688 |
} |
| 689 | 689 |
} else {
|
| 690 | 690 |
for (int i = 0; i != _root; ++i) {
|
| 691 | 691 |
last_out = _first_out[i+1]; |
| 692 | 692 |
for (int j = _first_out[i]; j != last_out; ++j) {
|
| 693 | 693 |
if (_forward[j] && _cost[j] < 0) {
|
| 694 | 694 |
Value c = _upper[j]; |
| 695 | 695 |
if (c >= MAX) return UNBOUNDED; |
| 696 | 696 |
excess[i] -= c; |
| 697 | 697 |
excess[_target[j]] += c; |
| ... | ... |
@@ -749,52 +749,52 @@ |
| 749 | 749 |
excess[_node_id[n]] = sup[n]; |
| 750 | 750 |
} |
| 751 | 751 |
for (int a = _first_out[_root]; a != _res_arc_num; ++a) {
|
| 752 | 752 |
int u = _target[a]; |
| 753 | 753 |
int ra = _reverse[a]; |
| 754 | 754 |
_res_cap[a] = -_sum_supply + 1; |
| 755 | 755 |
_res_cap[ra] = -excess[u]; |
| 756 | 756 |
_cost[a] = 0; |
| 757 | 757 |
_cost[ra] = 0; |
| 758 | 758 |
} |
| 759 | 759 |
} else {
|
| 760 | 760 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 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 |
} |
| 789 | 789 |
} |
| 790 | 790 |
_sgr.build(_res_node_num, _arc_vec.begin(), _arc_vec.end()); |
| 791 | 791 |
} |
| 792 | 792 |
|
| 793 | 793 |
// Execute the algorithm and transform the results |
| 794 | 794 |
void start(Method method) {
|
| 795 | 795 |
// Execute the algorithm |
| 796 | 796 |
switch (method) {
|
| 797 | 797 |
case SIMPLE_CYCLE_CANCELING: |
| 798 | 798 |
startSimpleCycleCanceling(); |
| 799 | 799 |
break; |
| 800 | 800 |
case MINIMUM_MEAN_CYCLE_CANCELING: |
| ... | ... |
@@ -808,56 +808,56 @@ |
| 808 | 808 |
// Compute node potentials |
| 809 | 809 |
if (method != SIMPLE_CYCLE_CANCELING) {
|
| 810 | 810 |
buildResidualNetwork(); |
| 811 | 811 |
typename BellmanFord<StaticDigraph, CostArcMap> |
| 812 | 812 |
::template SetDistMap<CostNodeMap>::Create bf(_sgr, _cost_map); |
| 813 | 813 |
bf.distMap(_pi_map); |
| 814 | 814 |
bf.init(0); |
| 815 | 815 |
bf.start(); |
| 816 | 816 |
} |
| 817 | 817 |
|
| 818 | 818 |
// Handle non-zero lower bounds |
| 819 | 819 |
if (_have_lower) {
|
| 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; |
| 852 | 852 |
std::vector<StaticDigraph::Arc> pred(_res_arc_num); |
| 853 | 853 |
PredMap pred_map(pred); |
| 854 | 854 |
BF bf(rgr, _cost_map); |
| 855 | 855 |
bf.distMap(_pi_map).predMap(pred_map); |
| 856 | 856 |
|
| 857 | 857 |
int length_bound = BF_FIRST_LIMIT; |
| 858 | 858 |
bool optimal = false; |
| 859 | 859 |
while (!optimal) {
|
| 860 | 860 |
bf.init(0); |
| 861 | 861 |
int iter_num = 0; |
| 862 | 862 |
bool cycle_found = false; |
| 863 | 863 |
while (!cycle_found) {
|
| ... | ... |
@@ -905,72 +905,72 @@ |
| 905 | 905 |
// Augment along the cycle |
| 906 | 906 |
for (int i = 0; i < int(cycle.size()); ++i) {
|
| 907 | 907 |
int j = cycle[i]; |
| 908 | 908 |
_res_cap[j] -= delta; |
| 909 | 909 |
_res_cap[_reverse[j]] += delta; |
| 910 | 910 |
} |
| 911 | 911 |
} |
| 912 | 912 |
} |
| 913 | 913 |
} |
| 914 | 914 |
|
| 915 | 915 |
// Increase iteration limit if no cycle is found |
| 916 | 916 |
if (!cycle_found) {
|
| 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); |
| 965 | 965 |
IntVector level(_res_node_num); |
| 966 | 966 |
BoolVector reached(_res_node_num); |
| 967 | 967 |
BoolVector processed(_res_node_num); |
| 968 | 968 |
IntVector pred_node(_res_node_num); |
| 969 | 969 |
IntVector pred_arc(_res_node_num); |
| 970 | 970 |
std::vector<int> stack(_res_node_num); |
| 971 | 971 |
std::vector<int> proc_vector(_res_node_num); |
| 972 | 972 |
|
| 973 | 973 |
// Initialize epsilon |
| 974 | 974 |
double epsilon = 0; |
| 975 | 975 |
for (int a = 0; a != _res_arc_num; ++a) {
|
| 976 | 976 |
if (_res_cap[a] > 0 && -_cost[a] > epsilon) |
| ... | ... |
@@ -1122,49 +1122,49 @@ |
| 1122 | 1122 |
|
| 1123 | 1123 |
// Modify epsilon |
| 1124 | 1124 |
epsilon = 0; |
| 1125 | 1125 |
for (int u = 0; u != _res_node_num; ++u) {
|
| 1126 | 1126 |
double curr, pu = pi[u]; |
| 1127 | 1127 |
int last_out = _first_out[u+1]; |
| 1128 | 1128 |
for (int a = _first_out[u]; a != last_out; ++a) {
|
| 1129 | 1129 |
if (_res_cap[a] == 0) continue; |
| 1130 | 1130 |
curr = _cost[a] + pu - pi[_target[a]]; |
| 1131 | 1131 |
if (-curr > epsilon) epsilon = -curr; |
| 1132 | 1132 |
} |
| 1133 | 1133 |
} |
| 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 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_DFS_H |
| 20 | 20 |
#define LEMON_DFS_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup search |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief DFS algorithm. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/list_graph.h> |
| 27 | 27 |
#include <lemon/bits/path_dump.h> |
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/error.h> |
| ... | ... |
@@ -61,49 +61,50 @@ |
| 61 | 61 |
|
| 62 | 62 |
///The type of the map that indicates which nodes are processed. |
| 63 | 63 |
|
| 64 | 64 |
///The type of the map that indicates which nodes are processed. |
| 65 | 65 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 66 | 66 |
///By default, it is a NullMap. |
| 67 | 67 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 68 | 68 |
///Instantiates a \c ProcessedMap. |
| 69 | 69 |
|
| 70 | 70 |
///This function instantiates a \ref ProcessedMap. |
| 71 | 71 |
///\param g is the digraph, to which |
| 72 | 72 |
///we would like to define the \ref ProcessedMap. |
| 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. |
| 98 | 99 |
|
| 99 | 100 |
///The type of the map that stores the distances of the nodes. |
| 100 | 101 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 101 | 102 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 102 | 103 |
///Instantiates a \c DistMap. |
| 103 | 104 |
|
| 104 | 105 |
///This function instantiates a \ref DistMap. |
| 105 | 106 |
///\param g is the digraph, to which we would like to define the |
| 106 | 107 |
///\ref DistMap. |
| 107 | 108 |
static DistMap *createDistMap(const Digraph &g) |
| 108 | 109 |
{
|
| 109 | 110 |
return new DistMap(g); |
| ... | ... |
@@ -249,49 +250,50 @@ |
| 249 | 250 |
///\c DistMap type. |
| 250 | 251 |
/// |
| 251 | 252 |
///\ref named-templ-param "Named parameter" for setting |
| 252 | 253 |
///\c DistMap type. |
| 253 | 254 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 254 | 255 |
template <class T> |
| 255 | 256 |
struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > {
|
| 256 | 257 |
typedef Dfs<Digraph, SetDistMapTraits<T> > Create; |
| 257 | 258 |
}; |
| 258 | 259 |
|
| 259 | 260 |
template <class T> |
| 260 | 261 |
struct SetReachedMapTraits : public Traits {
|
| 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 |
| 286 | 288 |
} |
| 287 | 289 |
}; |
| 288 | 290 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 289 | 291 |
///\c ProcessedMap type. |
| 290 | 292 |
/// |
| 291 | 293 |
///\ref named-templ-param "Named parameter" for setting |
| 292 | 294 |
///\c ProcessedMap type. |
| 293 | 295 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 294 | 296 |
template <class T> |
| 295 | 297 |
struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
|
| 296 | 298 |
typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create; |
| 297 | 299 |
}; |
| ... | ... |
@@ -781,49 +783,50 @@ |
| 781 | 783 |
|
| 782 | 784 |
///The type of the map that indicates which nodes are processed. |
| 783 | 785 |
|
| 784 | 786 |
///The type of the map that indicates which nodes are processed. |
| 785 | 787 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 786 | 788 |
///By default, it is a NullMap. |
| 787 | 789 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
| 788 | 790 |
///Instantiates a ProcessedMap. |
| 789 | 791 |
|
| 790 | 792 |
///This function instantiates a ProcessedMap. |
| 791 | 793 |
///\param g is the digraph, to which |
| 792 | 794 |
///we would like to define the ProcessedMap. |
| 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. |
| 818 | 821 |
|
| 819 | 822 |
///The type of the map that stores the distances of the nodes. |
| 820 | 823 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 821 | 824 |
typedef typename Digraph::template NodeMap<int> DistMap; |
| 822 | 825 |
///Instantiates a DistMap. |
| 823 | 826 |
|
| 824 | 827 |
///This function instantiates a DistMap. |
| 825 | 828 |
///\param g is the digraph, to which we would like to define |
| 826 | 829 |
///the DistMap |
| 827 | 830 |
static DistMap *createDistMap(const Digraph &g) |
| 828 | 831 |
{
|
| 829 | 832 |
return new DistMap(g); |
| ... | ... |
@@ -1186,49 +1189,50 @@ |
| 1186 | 1189 |
visitor.reach(node); |
| 1187 | 1190 |
visitor.discover(arc); |
| 1188 | 1191 |
visitor.examine(arc); |
| 1189 | 1192 |
visitor.leave(node); |
| 1190 | 1193 |
visitor.backtrack(arc); |
| 1191 | 1194 |
} |
| 1192 | 1195 |
_Visitor& visitor; |
| 1193 | 1196 |
}; |
| 1194 | 1197 |
}; |
| 1195 | 1198 |
#endif |
| 1196 | 1199 |
|
| 1197 | 1200 |
/// \brief Default traits class of DfsVisit class. |
| 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 |
}; |
| 1223 | 1227 |
|
| 1224 | 1228 |
/// \ingroup search |
| 1225 | 1229 |
/// |
| 1226 | 1230 |
/// \brief DFS algorithm class with visitor interface. |
| 1227 | 1231 |
/// |
| 1228 | 1232 |
/// This class provides an efficient implementation of the DFS algorithm |
| 1229 | 1233 |
/// with visitor interface. |
| 1230 | 1234 |
/// |
| 1231 | 1235 |
/// The DfsVisit class provides an alternative interface to the Dfs |
| 1232 | 1236 |
/// class. It works with callback mechanism, the DfsVisit object calls |
| 1233 | 1237 |
/// the member functions of the \c Visitor class on every DFS event. |
| 1234 | 1238 |
/// |
| 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_DIJKSTRA_H |
| 20 | 20 |
#define LEMON_DIJKSTRA_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup shortest_path |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief Dijkstra algorithm. |
| 25 | 25 |
|
| 26 | 26 |
#include <limits> |
| 27 | 27 |
#include <lemon/list_graph.h> |
| 28 | 28 |
#include <lemon/bin_heap.h> |
| 29 | 29 |
#include <lemon/bits/path_dump.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 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_DIMACS_H |
| 20 | 20 |
#define LEMON_DIMACS_H |
| 21 | 21 |
|
| 22 | 22 |
#include <iostream> |
| 23 | 23 |
#include <string> |
| 24 | 24 |
#include <vector> |
| 25 | 25 |
#include <limits> |
| 26 | 26 |
#include <lemon/maps.h> |
| 27 | 27 |
#include <lemon/error.h> |
| 28 | 28 |
/// \ingroup dimacs_group |
| 29 | 29 |
/// \file |
| ... | ... |
@@ -40,49 +40,49 @@ |
| 40 | 40 |
///\brief DIMACS file type enum |
| 41 | 41 |
/// |
| 42 | 42 |
///DIMACS file type enum. |
| 43 | 43 |
enum Type {
|
| 44 | 44 |
NONE, ///< Undefined type. |
| 45 | 45 |
MIN, ///< DIMACS file type for minimum cost flow problems. |
| 46 | 46 |
MAX, ///< DIMACS file type for maximum flow problems. |
| 47 | 47 |
SP, ///< DIMACS file type for shostest path problems. |
| 48 | 48 |
MAT ///< DIMACS file type for plain graphs and matching problems. |
| 49 | 49 |
}; |
| 50 | 50 |
///The file type |
| 51 | 51 |
Type type; |
| 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': |
| 77 | 77 |
if(is >> problem >> r.nodeNum >> r.edgeNum) |
| 78 | 78 |
{
|
| 79 | 79 |
getline(is, str); |
| 80 | 80 |
r.lineShift++; |
| 81 | 81 |
if(problem=="min") r.type=DimacsDescriptor::MIN; |
| 82 | 82 |
else if(problem=="max") r.type=DimacsDescriptor::MAX; |
| 83 | 83 |
else if(problem=="sp") r.type=DimacsDescriptor::SP; |
| 84 | 84 |
else if(problem=="mat") r.type=DimacsDescriptor::MAT; |
| 85 | 85 |
else throw FormatError("Unknown problem type");
|
| 86 | 86 |
return r; |
| 87 | 87 |
} |
| 88 | 88 |
else |
| ... | ... |
@@ -191,74 +191,74 @@ |
| 191 | 191 |
Digraph &g, |
| 192 | 192 |
CapacityMap& capacity, |
| 193 | 193 |
typename Digraph::Node &s, |
| 194 | 194 |
typename Digraph::Node &t, |
| 195 | 195 |
typename CapacityMap::Value infty = 0, |
| 196 | 196 |
DimacsDescriptor desc=DimacsDescriptor()) {
|
| 197 | 197 |
g.clear(); |
| 198 | 198 |
s=t=INVALID; |
| 199 | 199 |
std::vector<typename Digraph::Node> nodes; |
| 200 | 200 |
typename Digraph::Arc e; |
| 201 | 201 |
char c, d; |
| 202 | 202 |
int i, j; |
| 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); |
| 253 | 253 |
g.addArc(nodes[i], nodes[j]); |
| 254 | 254 |
} |
| 255 | 255 |
break; |
| 256 | 256 |
} |
| 257 | 257 |
} |
| 258 | 258 |
} |
| 259 | 259 |
|
| 260 | 260 |
/// \brief DIMACS maximum flow reader function. |
| 261 | 261 |
/// |
| 262 | 262 |
/// This function reads a maximum flow instance from DIMACS format, |
| 263 | 263 |
/// i.e. from a DIMACS file having a line starting with |
| 264 | 264 |
/// \code |
| ... | ... |
@@ -341,79 +341,79 @@ |
| 341 | 341 |
CapacityMap& capacity, |
| 342 | 342 |
typename CapacityMap::Value infty = 0, |
| 343 | 343 |
DimacsDescriptor desc=DimacsDescriptor()) {
|
| 344 | 344 |
typename Digraph::Node u,v; |
| 345 | 345 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
| 346 | 346 |
if(desc.type!=DimacsDescriptor::MAX || desc.type!=DimacsDescriptor::SP) |
| 347 | 347 |
throw FormatError("Problem type mismatch");
|
| 348 | 348 |
_readDimacs(is, g, capacity, u, v, infty, desc); |
| 349 | 349 |
} |
| 350 | 350 |
|
| 351 | 351 |
template<typename Graph> |
| 352 | 352 |
typename enable_if<lemon::UndirectedTagIndicator<Graph>,void>::type |
| 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 |
{
|
| 382 | 382 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
| 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; |
| 408 | 408 |
} |
| 409 | 409 |
} |
| 410 | 410 |
} |
| 411 | 411 |
|
| 412 | 412 |
/// DIMACS plain digraph writer function. |
| 413 | 413 |
/// |
| 414 | 414 |
/// This function writes a digraph without any designated nodes and |
| 415 | 415 |
/// maps into DIMACS format, i.e. into DIMACS file having a line |
| 416 | 416 |
/// starting with |
| 417 | 417 |
/// \code |
| 418 | 418 |
/// p mat |
| 419 | 419 |
/// \endcode |
| 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_EDGE_SET_H |
| 20 | 20 |
#define LEMON_EDGE_SET_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/core.h> |
| 23 | 23 |
#include <lemon/bits/edge_set_extender.h> |
| 24 | 24 |
|
| 25 | 25 |
/// \ingroup graphs |
| 26 | 26 |
/// \file |
| 27 | 27 |
/// \brief ArcSet and EdgeSet classes. |
| 28 | 28 |
/// |
| 29 | 29 |
/// Graphs which use another graph's node-set as own. |
| 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 |
| 57 | 57 |
{
|
| 58 | 58 |
typedef typename GR::Node Node; |
| 59 | 59 |
typedef typename GR::NodeIt NodeIt; |
| 60 | 60 |
typedef typename GR::Arc Arc; |
| 61 | 61 |
typedef typename GR::ArcIt ArcIt; |
| 62 | 62 |
typedef typename GR::OutArcIt OutArcIt; |
| 63 | 63 |
typedef typename GR::InArcIt InArcIt; |
| 64 | 64 |
|
| 65 | 65 |
const GR &g; |
| 66 | 66 |
typename GR::template NodeMap<OutArcIt> narc; |
| 67 | 67 |
std::list<Arc> euler; |
| 68 | 68 |
|
| ... | ... |
@@ -117,58 +117,58 @@ |
| 117 | 117 |
return *this; |
| 118 | 118 |
} |
| 119 | 119 |
///Postfix incrementation |
| 120 | 120 |
|
| 121 | 121 |
/// Postfix incrementation. |
| 122 | 122 |
/// |
| 123 | 123 |
///\warning This incrementation |
| 124 | 124 |
///returns an \c Arc, not a \ref DiEulerIt, as one may |
| 125 | 125 |
///expect. |
| 126 | 126 |
Arc operator++(int) |
| 127 | 127 |
{
|
| 128 | 128 |
Arc e=*this; |
| 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; |
| 163 | 163 |
typedef typename GR::ArcIt ArcIt; |
| 164 | 164 |
typedef typename GR::OutArcIt OutArcIt; |
| 165 | 165 |
typedef typename GR::InArcIt InArcIt; |
| 166 | 166 |
|
| 167 | 167 |
const GR &g; |
| 168 | 168 |
typename GR::template NodeMap<OutArcIt> narc; |
| 169 | 169 |
typename GR::template EdgeMap<bool> visited; |
| 170 | 170 |
std::list<Arc> euler; |
| 171 | 171 |
|
| 172 | 172 |
public: |
| 173 | 173 |
|
| 174 | 174 |
///Constructor |
| ... | ... |
@@ -212,49 +212,49 @@ |
| 212 | 212 |
///Next arc of the tour |
| 213 | 213 |
/// |
| 214 | 214 |
EulerIt &operator++() {
|
| 215 | 215 |
Node s=g.target(euler.front()); |
| 216 | 216 |
euler.pop_front(); |
| 217 | 217 |
typename std::list<Arc>::iterator next=euler.begin(); |
| 218 | 218 |
while(narc[s]!=INVALID) {
|
| 219 | 219 |
while(narc[s]!=INVALID && visited[narc[s]]) ++narc[s]; |
| 220 | 220 |
if(narc[s]==INVALID) break; |
| 221 | 221 |
else {
|
| 222 | 222 |
euler.insert(next,narc[s]); |
| 223 | 223 |
visited[narc[s]]=true; |
| 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 |
|
| 249 | 249 |
/// \ingroup graph_properties |
| 250 | 250 |
///This function checks if the given graph is Eulerian. |
| 251 | 251 |
///It works for both directed and undirected graphs. |
| 252 | 252 |
/// |
| 253 | 253 |
///By definition, a digraph is called \e Eulerian if |
| 254 | 254 |
///and only if it is connected and the number of incoming and outgoing |
| 255 | 255 |
///arcs are the same for each node. |
| 256 | 256 |
///Similarly, an undirected graph is called \e Eulerian if |
| 257 | 257 |
///and only if it is connected and the number of incident edges is even |
| 258 | 258 |
///for each node. |
| 259 | 259 |
/// |
| 260 | 260 |
///\note There are (di)graphs that are not Eulerian, but still have an |
| 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_FRACTIONAL_MATCHING_H |
| 20 | 20 |
#define LEMON_FRACTIONAL_MATCHING_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <queue> |
| 24 | 24 |
#include <set> |
| 25 | 25 |
#include <limits> |
| 26 | 26 |
|
| 27 | 27 |
#include <lemon/core.h> |
| 28 | 28 |
#include <lemon/unionfind.h> |
| 29 | 29 |
#include <lemon/bin_heap.h> |
| ... | ... |
@@ -1988,49 +1988,49 @@ |
| 1988 | 1988 |
{
|
| 1989 | 1989 |
Edge e = _delta3->top(); |
| 1990 | 1990 |
|
| 1991 | 1991 |
Node left = _graph.u(e); |
| 1992 | 1992 |
Node right = _graph.v(e); |
| 1993 | 1993 |
|
| 1994 | 1994 |
int left_tree = _tree_set->find(left); |
| 1995 | 1995 |
int right_tree = _tree_set->find(right); |
| 1996 | 1996 |
|
| 1997 | 1997 |
if (left_tree == right_tree) {
|
| 1998 | 1998 |
cycleOnEdge(e, left_tree); |
| 1999 | 1999 |
--unmatched; |
| 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 |
|
| 2025 | 2025 |
/// @} |
| 2026 | 2026 |
|
| 2027 | 2027 |
/// \name Primal Solution |
| 2028 | 2028 |
/// Functions to get the primal solution, i.e. the maximum weighted |
| 2029 | 2029 |
/// matching.\n |
| 2030 | 2030 |
/// Either \ref run() or \ref start() function should be called before |
| 2031 | 2031 |
/// using them. |
| 2032 | 2032 |
|
| 2033 | 2033 |
/// @{
|
| 2034 | 2034 |
|
| 2035 | 2035 |
/// \brief Return the weight of the matching. |
| 2036 | 2036 |
/// |
| 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_FULL_GRAPH_H |
| 20 | 20 |
#define LEMON_FULL_GRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/core.h> |
| 23 | 23 |
#include <lemon/bits/graph_extender.h> |
| 24 | 24 |
|
| 25 | 25 |
///\ingroup graphs |
| 26 | 26 |
///\file |
| 27 | 27 |
///\brief FullDigraph and FullGraph classes. |
| 28 | 28 |
|
| 29 | 29 |
namespace lemon {
|
| ... | ... |
@@ -182,58 +182,58 @@ |
| 182 | 182 |
/// Default constructor. The number of nodes and arcs will be zero. |
| 183 | 183 |
FullDigraph() { construct(0); }
|
| 184 | 184 |
|
| 185 | 185 |
/// \brief Constructor |
| 186 | 186 |
/// |
| 187 | 187 |
/// Constructor. |
| 188 | 188 |
/// \param n The number of the nodes. |
| 189 | 189 |
FullDigraph(int n) { construct(n); }
|
| 190 | 190 |
|
| 191 | 191 |
/// \brief Resizes the digraph |
| 192 | 192 |
/// |
| 193 | 193 |
/// This function resizes the digraph. It fully destroys and |
| 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 |
} |
| 228 | 228 |
|
| 229 | 229 |
/// \brief Number of nodes. |
| 230 | 230 |
int nodeNum() const { return Parent::nodeNum(); }
|
| 231 | 231 |
/// \brief Number of arcs. |
| 232 | 232 |
int arcNum() const { return Parent::arcNum(); }
|
| 233 | 233 |
}; |
| 234 | 234 |
|
| 235 | 235 |
|
| 236 | 236 |
class FullGraphBase {
|
| 237 | 237 |
public: |
| 238 | 238 |
|
| 239 | 239 |
typedef FullGraphBase Graph; |
| ... | ... |
@@ -561,58 +561,58 @@ |
| 561 | 561 |
|
| 562 | 562 |
/// \brief Constructor |
| 563 | 563 |
/// |
| 564 | 564 |
/// Constructor. |
| 565 | 565 |
/// \param n The number of the nodes. |
| 566 | 566 |
FullGraph(int n) { construct(n); }
|
| 567 | 567 |
|
| 568 | 568 |
/// \brief Resizes the graph |
| 569 | 569 |
/// |
| 570 | 570 |
/// This function resizes the graph. It fully destroys and |
| 571 | 571 |
/// rebuilds the structure, therefore the maps of the graph will be |
| 572 | 572 |
/// reallocated automatically and the previous values will be lost. |
| 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 |
} |
| 607 | 607 |
|
| 608 | 608 |
/// \brief Returns the edge connecting the given nodes. |
| 609 | 609 |
/// |
| 610 | 610 |
/// Returns the edge connecting the given nodes. |
| 611 | 611 |
Edge edge(Node u, Node v) const {
|
| 612 | 612 |
return Parent::edge(u, v); |
| 613 | 613 |
} |
| 614 | 614 |
|
| 615 | 615 |
/// \brief Number of nodes. |
| 616 | 616 |
int nodeNum() const { return Parent::nodeNum(); }
|
| 617 | 617 |
/// \brief Number of arcs. |
| 618 | 618 |
int arcNum() const { return Parent::arcNum(); }
|
| 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 |
///\file |
| 20 | 20 |
///\brief Implementation of the LEMON GLPK LP and MIP solver interface. |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/glpk.h> |
| 23 | 23 |
#include <glpk.h> |
| 24 | 24 |
|
| 25 | 25 |
#include <lemon/assert.h> |
| 26 | 26 |
|
| 27 | 27 |
namespace lemon {
|
| 28 | 28 |
|
| 29 | 29 |
// GlpkBase members |
| ... | ... |
@@ -38,62 +38,62 @@ |
| 38 | 38 |
lp = glp_create_prob(); |
| 39 | 39 |
glp_copy_prob(lp, other.lp, GLP_ON); |
| 40 | 40 |
glp_create_index(lp); |
| 41 | 41 |
rows = other.rows; |
| 42 | 42 |
cols = other.cols; |
| 43 | 43 |
messageLevel(MESSAGE_NOTHING); |
| 44 | 44 |
} |
| 45 | 45 |
|
| 46 | 46 |
GlpkBase::~GlpkBase() {
|
| 47 | 47 |
glp_delete_prob(lp); |
| 48 | 48 |
} |
| 49 | 49 |
|
| 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 |
|
| 88 | 88 |
for(ExprIterator it = b; it != e; ++it) {
|
| 89 | 89 |
indexes.push_back(it->first); |
| 90 | 90 |
values.push_back(it->second); |
| 91 | 91 |
} |
| 92 | 92 |
|
| 93 | 93 |
glp_set_mat_row(lp, i, values.size() - 1, |
| 94 | 94 |
&indexes.front(), &values.front()); |
| 95 | 95 |
return i; |
| 96 | 96 |
} |
| 97 | 97 |
|
| 98 | 98 |
void GlpkBase::_eraseCol(int i) {
|
| 99 | 99 |
int ca[2]; |
| 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_GLPK_H |
| 20 | 20 |
#define LEMON_GLPK_H |
| 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 |
| 55 | 55 |
class GlpkBase : virtual public LpBase {
|
| 56 | 56 |
protected: |
| 57 | 57 |
|
| 58 | 58 |
_solver_bits::VoidPtr lp; |
| 59 | 59 |
|
| 60 | 60 |
GlpkBase(); |
| 61 | 61 |
GlpkBase(const GlpkBase&); |
| 62 | 62 |
virtual ~GlpkBase(); |
| 63 | 63 |
|
| 64 | 64 |
protected: |
| 65 | 65 |
|
| 66 | 66 |
virtual int _addCol(); |
| ... | ... |
@@ -103,55 +103,55 @@ |
| 103 | 103 |
virtual Value _getRowUpperBound(int i) const; |
| 104 | 104 |
|
| 105 | 105 |
virtual void _setObjCoeffs(ExprIterator b, ExprIterator e); |
| 106 | 106 |
virtual void _getObjCoeffs(InsertIterator b) const; |
| 107 | 107 |
|
| 108 | 108 |
virtual void _setObjCoeff(int i, Value obj_coef); |
| 109 | 109 |
virtual Value _getObjCoeff(int i) const; |
| 110 | 110 |
|
| 111 | 111 |
virtual void _setSense(Sense); |
| 112 | 112 |
virtual Sense _getSense() const; |
| 113 | 113 |
|
| 114 | 114 |
virtual void _clear(); |
| 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)); }
|
| 146 | 146 |
|
| 147 | 147 |
}; |
| 148 | 148 |
|
| 149 | 149 |
/// \brief Interface for the GLPK LP solver |
| 150 | 150 |
/// |
| 151 | 151 |
/// This class implements an interface for the GLPK LP solver. |
| 152 | 152 |
///\ingroup lp_group |
| 153 | 153 |
class GlpkLp : public LpSolver, public GlpkBase {
|
| 154 | 154 |
public: |
| 155 | 155 |
|
| 156 | 156 |
///\e |
| 157 | 157 |
GlpkLp(); |
| 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 |
/// |
| 287 | 287 |
/// \param s The base node. |
| 288 | 288 |
/// \param t The node you want to separate from node \c s. |
| 289 | 289 |
/// \param cutMap The cut will be returned in this map. |
| 290 | 290 |
/// It must be a \c bool (or convertible) \ref concepts::ReadWriteMap |
| 291 | 291 |
/// "ReadWriteMap" on the graph nodes. |
| 292 | 292 |
/// |
| 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 |
| 365 | 365 |
class MinCutNodeIt |
| 366 | 366 |
{
|
| 367 | 367 |
bool _side; |
| 368 | 368 |
typename Graph::NodeIt _node_it; |
| 369 | 369 |
typename Graph::template NodeMap<bool> _cut; |
| 370 | 370 |
public: |
| 371 | 371 |
/// Constructor |
| 372 | 372 |
|
| 373 | 373 |
/// Constructor. |
| 374 | 374 |
/// |
| 375 | 375 |
MinCutNodeIt(GomoryHu const &gomory, |
| 376 | 376 |
///< The GomoryHu class. You must call its |
| ... | ... |
@@ -421,86 +421,86 @@ |
| 421 | 421 |
bool operator!=(Invalid) { return _node_it!=INVALID; }
|
| 422 | 422 |
/// Next node |
| 423 | 423 |
|
| 424 | 424 |
/// Next node. |
| 425 | 425 |
/// |
| 426 | 426 |
MinCutNodeIt &operator++() |
| 427 | 427 |
{
|
| 428 | 428 |
for(++_node_it;_node_it!=INVALID&&_cut[_node_it]!=_side;++_node_it) {}
|
| 429 | 429 |
return *this; |
| 430 | 430 |
} |
| 431 | 431 |
/// Postfix incrementation |
| 432 | 432 |
|
| 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]; |
| 462 | 462 |
/// \endcode |
| 463 | 463 |
/// The result will be the same as the value returned by |
| 464 | 464 |
/// \ref GomoryHu::minCutValue() "gom.minCutValue(s,t)". |
| 465 | 465 |
class MinCutEdgeIt |
| 466 | 466 |
{
|
| 467 | 467 |
bool _side; |
| 468 | 468 |
const Graph &_graph; |
| 469 | 469 |
typename Graph::NodeIt _node_it; |
| 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. |
| 495 | 495 |
bool side=true |
| 496 | 496 |
///< If it is \c true (default) then the listed arcs |
| 497 | 497 |
/// will be oriented from the |
| 498 | 498 |
/// nodes of the component containing \c s, |
| 499 | 499 |
/// otherwise they will be oriented in the opposite |
| 500 | 500 |
/// direction. |
| 501 | 501 |
) |
| 502 | 502 |
: _graph(gomory._graph), _cut(_graph) |
| 503 | 503 |
{
|
| 504 | 504 |
gomory.minCutMap(s,t,_cut); |
| 505 | 505 |
if(!side) |
| 506 | 506 |
for(typename Graph::NodeIt n(_graph);n!=INVALID;++n) |
| ... | ... |
@@ -527,42 +527,42 @@ |
| 527 | 527 |
{
|
| 528 | 528 |
return _arc_it; |
| 529 | 529 |
} |
| 530 | 530 |
/// Conversion to \c Edge |
| 531 | 531 |
|
| 532 | 532 |
/// Conversion to \c Edge. |
| 533 | 533 |
/// |
| 534 | 534 |
operator typename Graph::Edge() const |
| 535 | 535 |
{
|
| 536 | 536 |
return _arc_it; |
| 537 | 537 |
} |
| 538 | 538 |
bool operator==(Invalid) { return _node_it==INVALID; }
|
| 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 |
|
| 564 | 564 |
}; |
| 565 | 565 |
|
| 566 | 566 |
} |
| 567 | 567 |
|
| 568 | 568 |
#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 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_GRAPH_TO_EPS_H |
| 20 | 20 |
#define LEMON_GRAPH_TO_EPS_H |
| 21 | 21 |
|
| 22 | 22 |
#include<iostream> |
| 23 | 23 |
#include<fstream> |
| 24 | 24 |
#include<sstream> |
| 25 | 25 |
#include<algorithm> |
| 26 | 26 |
#include<vector> |
| 27 | 27 |
|
| 28 | 28 |
#ifndef WIN32 |
| 29 | 29 |
#include<sys/time.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 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_HAO_ORLIN_H |
| 20 | 20 |
#define LEMON_HAO_ORLIN_H |
| 21 | 21 |
|
| 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); |
| 92 | 92 |
|
| 93 | 93 |
const Digraph& _graph; |
| 94 | 94 |
const CapacityMap* _capacity; |
| 95 | 95 |
|
| 96 | 96 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
| 97 | 97 |
FlowMap* _flow; |
| 98 | 98 |
|
| 99 | 99 |
Node _source; |
| 100 | 100 |
|
| 101 | 101 |
int _node_num; |
| 102 | 102 |
|
| 103 | 103 |
// Bucketing structure |
| ... | ... |
@@ -843,49 +843,49 @@ |
| 843 | 843 |
|
| 844 | 844 |
/// \name Execution Control |
| 845 | 845 |
/// The simplest way to execute the algorithm is to use |
| 846 | 846 |
/// one of the member functions called \ref run(). |
| 847 | 847 |
/// \n |
| 848 | 848 |
/// If you need better control on the execution, |
| 849 | 849 |
/// you have to call one of the \ref init() functions first, then |
| 850 | 850 |
/// \ref calculateOut() and/or \ref calculateIn(). |
| 851 | 851 |
|
| 852 | 852 |
/// @{
|
| 853 | 853 |
|
| 854 | 854 |
/// \brief Initialize the internal data structures. |
| 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 |
|
| 880 | 880 |
if (!_flow) {
|
| 881 | 881 |
_flow = new FlowMap(_graph); |
| 882 | 882 |
} |
| 883 | 883 |
if (!_next) {
|
| 884 | 884 |
_next = new typename Digraph::template NodeMap<Node>(_graph); |
| 885 | 885 |
} |
| 886 | 886 |
if (!_prev) {
|
| 887 | 887 |
_prev = new typename Digraph::template NodeMap<Node>(_graph); |
| 888 | 888 |
} |
| 889 | 889 |
if (!_active) {
|
| 890 | 890 |
_active = new typename Digraph::template NodeMap<bool>(_graph); |
| 891 | 891 |
} |
| ... | ... |
@@ -923,83 +923,83 @@ |
| 923 | 923 |
/// |
| 924 | 924 |
/// This function calculates a minimum cut with \f$ source \f$ on the |
| 925 | 925 |
/// sink-side (i.e. a set \f$ X\subsetneq V \f$ with |
| 926 | 926 |
/// \f$ source \notin X \f$ and minimal outgoing capacity). |
| 927 | 927 |
/// |
| 928 | 928 |
/// \pre \ref init() must be called before using this function. |
| 929 | 929 |
void calculateIn() {
|
| 930 | 930 |
findMinCutIn(); |
| 931 | 931 |
} |
| 932 | 932 |
|
| 933 | 933 |
|
| 934 | 934 |
/// \brief Run the algorithm. |
| 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 |
| 1002 | 1002 |
|
| 1003 | 1003 |
} //namespace lemon |
| 1004 | 1004 |
|
| 1005 | 1005 |
#endif //LEMON_HAO_ORLIN_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 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_HARTMANN_ORLIN_MMC_H |
| 20 | 20 |
#define LEMON_HARTMANN_ORLIN_MMC_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup min_mean_cycle |
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief Hartmann-Orlin's algorithm for finding a minimum mean cycle. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <lemon/core.h> |
| ... | ... |
@@ -322,56 +322,56 @@ |
| 322 | 322 |
/// This function runs the algorithm. |
| 323 | 323 |
/// It can be called more than once (e.g. if the underlying digraph |
| 324 | 324 |
/// and/or the arc costs have been modified). |
| 325 | 325 |
/// |
| 326 | 326 |
/// \return \c true if a directed cycle exists in the digraph. |
| 327 | 327 |
/// |
| 328 | 328 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
| 329 | 329 |
/// \code |
| 330 | 330 |
/// return mmc.findCycleMean() && mmc.findCycle(); |
| 331 | 331 |
/// \endcode |
| 332 | 332 |
bool run() {
|
| 333 | 333 |
return findCycleMean() && findCycle(); |
| 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. |
| 366 | 366 |
/// |
| 367 | 367 |
/// This function finds a directed cycle of minimum mean cost |
| 368 | 368 |
/// in the digraph using the data computed by findCycleMean(). |
| 369 | 369 |
/// |
| 370 | 370 |
/// \return \c true if a directed cycle exists in the digraph. |
| 371 | 371 |
/// |
| 372 | 372 |
/// \pre \ref findCycleMean() must be called before using this function. |
| 373 | 373 |
bool findCycle() {
|
| 374 | 374 |
if (!_best_found) return false; |
| 375 | 375 |
IntNodeMap reached(_gr, -1); |
| 376 | 376 |
int r = _best_level + 1; |
| 377 | 377 |
Node u = _best_node; |
| ... | ... |
@@ -482,49 +482,49 @@ |
| 482 | 482 |
for (OutArcIt a(_gr, n); a != INVALID; ++a) {
|
| 483 | 483 |
_out_arcs[n].push_back(a); |
| 484 | 484 |
} |
| 485 | 485 |
} |
| 486 | 486 |
} else {
|
| 487 | 487 |
for (int i = 0; i < _comp_num; ++i) |
| 488 | 488 |
_comp_nodes[i].clear(); |
| 489 | 489 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
| 490 | 490 |
int k = _comp[n]; |
| 491 | 491 |
_comp_nodes[k].push_back(n); |
| 492 | 492 |
_out_arcs[n].clear(); |
| 493 | 493 |
for (OutArcIt a(_gr, n); a != INVALID; ++a) {
|
| 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); |
| 519 | 519 |
_process.clear(); |
| 520 | 520 |
_process.push_back(start); |
| 521 | 521 |
|
| 522 | 522 |
int k, n = _nodes->size(); |
| 523 | 523 |
int next_check = 4; |
| 524 | 524 |
bool terminate = false; |
| 525 | 525 |
for (k = 1; k <= n && int(_process.size()) < n && !terminate; ++k) {
|
| 526 | 526 |
processNextBuildRound(k); |
| 527 | 527 |
if (k == next_check || k == n) {
|
| 528 | 528 |
terminate = checkTermination(k); |
| 529 | 529 |
next_check = next_check * 3 / 2; |
| 530 | 530 |
} |
| ... | ... |
@@ -555,81 +555,81 @@ |
| 555 | 555 |
_data[v][k] = PathData(d, e); |
| 556 | 556 |
} |
| 557 | 557 |
} |
| 558 | 558 |
} |
| 559 | 559 |
_process.swap(next); |
| 560 | 560 |
} |
| 561 | 561 |
|
| 562 | 562 |
// Process one round using _nodes instead of _process |
| 563 | 563 |
void processNextFullRound(int k) {
|
| 564 | 564 |
Node u, v; |
| 565 | 565 |
Arc e; |
| 566 | 566 |
LargeCost d; |
| 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) {
|
| 624 | 624 |
d = _data[u][j].dist * _curr_size - j * _curr_cost; |
| 625 | 625 |
if (_tolerance.less(d, pi[u])) pi[u] = d; |
| 626 | 626 |
} |
| 627 | 627 |
} |
| 628 | 628 |
} |
| 629 | 629 |
|
| 630 | 630 |
// Check the optimality condition for all arcs |
| 631 | 631 |
bool done = true; |
| 632 | 632 |
for (ArcIt a(_gr); a != INVALID; ++a) {
|
| 633 | 633 |
if (_tolerance.less(_cost[a] * _curr_size - _curr_cost, |
| 634 | 634 |
pi[_gr.target(a)] - pi[_gr.source(a)]) ) {
|
| 635 | 635 |
done = false; |
| 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_HOWARD_MMC_H |
| 20 | 20 |
#define LEMON_HOWARD_MMC_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup min_mean_cycle |
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief Howard's algorithm for finding a minimum mean cycle. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <lemon/core.h> |
| ... | ... |
@@ -100,156 +100,156 @@ |
| 100 | 100 |
/// a directed cycle of minimum mean cost in a digraph |
| 101 | 101 |
/// \ref amo93networkflows, \ref dasdan98minmeancycle. |
| 102 | 102 |
/// This class provides the most efficient algorithm for the |
| 103 | 103 |
/// minimum mean cycle problem, though the best known theoretical |
| 104 | 104 |
/// bound on its running time is exponential. |
| 105 | 105 |
/// |
| 106 | 106 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 107 | 107 |
/// \tparam CM The type of the cost map. The default |
| 108 | 108 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 109 | 109 |
/// \tparam TR The traits class that defines various types used by the |
| 110 | 110 |
/// algorithm. By default, it is \ref HowardMmcDefaultTraits |
| 111 | 111 |
/// "HowardMmcDefaultTraits<GR, CM>". |
| 112 | 112 |
/// In most cases, this parameter should not be set directly, |
| 113 | 113 |
/// consider to use the named template parameters instead. |
| 114 | 114 |
#ifdef DOXYGEN |
| 115 | 115 |
template <typename GR, typename CM, typename TR> |
| 116 | 116 |
#else |
| 117 | 117 |
template < typename GR, |
| 118 | 118 |
typename CM = typename GR::template ArcMap<int>, |
| 119 | 119 |
typename TR = HowardMmcDefaultTraits<GR, CM> > |
| 120 | 120 |
#endif |
| 121 | 121 |
class HowardMmc |
| 122 | 122 |
{
|
| 123 | 123 |
public: |
| 124 |
|
|
| 124 |
|
|
| 125 | 125 |
/// The type of the digraph |
| 126 | 126 |
typedef typename TR::Digraph Digraph; |
| 127 | 127 |
/// The type of the cost map |
| 128 | 128 |
typedef typename TR::CostMap CostMap; |
| 129 | 129 |
/// The type of the arc costs |
| 130 | 130 |
typedef typename TR::Cost Cost; |
| 131 | 131 |
|
| 132 | 132 |
/// \brief The large cost type |
| 133 | 133 |
/// |
| 134 | 134 |
/// The large cost type used for internal computations. |
| 135 | 135 |
/// By default, it is \c long \c long if the \c Cost type is integer, |
| 136 | 136 |
/// otherwise it is \c double. |
| 137 | 137 |
typedef typename TR::LargeCost LargeCost; |
| 138 | 138 |
|
| 139 | 139 |
/// The tolerance type |
| 140 | 140 |
typedef typename TR::Tolerance Tolerance; |
| 141 | 141 |
|
| 142 | 142 |
/// \brief The path type of the found cycles |
| 143 | 143 |
/// |
| 144 | 144 |
/// The path type of the found cycles. |
| 145 | 145 |
/// Using the \ref HowardMmcDefaultTraits "default traits class", |
| 146 | 146 |
/// it is \ref lemon::Path "Path<Digraph>". |
| 147 | 147 |
typedef typename TR::Path Path; |
| 148 | 148 |
|
| 149 | 149 |
/// The \ref HowardMmcDefaultTraits "traits class" of the algorithm |
| 150 | 150 |
typedef TR Traits; |
| 151 | 151 |
|
| 152 | 152 |
private: |
| 153 | 153 |
|
| 154 | 154 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 155 |
|
|
| 155 |
|
|
| 156 | 156 |
// The digraph the algorithm runs on |
| 157 | 157 |
const Digraph &_gr; |
| 158 | 158 |
// The cost of the arcs |
| 159 | 159 |
const CostMap &_cost; |
| 160 | 160 |
|
| 161 | 161 |
// Data for the found cycles |
| 162 | 162 |
bool _curr_found, _best_found; |
| 163 | 163 |
LargeCost _curr_cost, _best_cost; |
| 164 | 164 |
int _curr_size, _best_size; |
| 165 | 165 |
Node _curr_node, _best_node; |
| 166 | 166 |
|
| 167 | 167 |
Path *_cycle_path; |
| 168 | 168 |
bool _local_path; |
| 169 | 169 |
|
| 170 | 170 |
// Internal data used by the algorithm |
| 171 | 171 |
typename Digraph::template NodeMap<Arc> _policy; |
| 172 | 172 |
typename Digraph::template NodeMap<bool> _reached; |
| 173 | 173 |
typename Digraph::template NodeMap<int> _level; |
| 174 | 174 |
typename Digraph::template NodeMap<LargeCost> _dist; |
| 175 | 175 |
|
| 176 | 176 |
// Data for storing the strongly connected components |
| 177 | 177 |
int _comp_num; |
| 178 | 178 |
typename Digraph::template NodeMap<int> _comp; |
| 179 | 179 |
std::vector<std::vector<Node> > _comp_nodes; |
| 180 | 180 |
std::vector<Node>* _nodes; |
| 181 | 181 |
typename Digraph::template NodeMap<std::vector<Arc> > _in_arcs; |
| 182 |
|
|
| 182 |
|
|
| 183 | 183 |
// Queue used for BFS search |
| 184 | 184 |
std::vector<Node> _queue; |
| 185 | 185 |
int _qfront, _qback; |
| 186 | 186 |
|
| 187 | 187 |
Tolerance _tolerance; |
| 188 |
|
|
| 188 |
|
|
| 189 | 189 |
// Infinite constant |
| 190 | 190 |
const LargeCost INF; |
| 191 | 191 |
|
| 192 | 192 |
public: |
| 193 |
|
|
| 193 |
|
|
| 194 | 194 |
/// \name Named Template Parameters |
| 195 | 195 |
/// @{
|
| 196 | 196 |
|
| 197 | 197 |
template <typename T> |
| 198 | 198 |
struct SetLargeCostTraits : public Traits {
|
| 199 | 199 |
typedef T LargeCost; |
| 200 | 200 |
typedef lemon::Tolerance<T> Tolerance; |
| 201 | 201 |
}; |
| 202 | 202 |
|
| 203 | 203 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 204 | 204 |
/// \c LargeCost type. |
| 205 | 205 |
/// |
| 206 | 206 |
/// \ref named-templ-param "Named parameter" for setting \c LargeCost |
| 207 | 207 |
/// type. It is used for internal computations in the algorithm. |
| 208 | 208 |
template <typename T> |
| 209 | 209 |
struct SetLargeCost |
| 210 | 210 |
: public HowardMmc<GR, CM, SetLargeCostTraits<T> > {
|
| 211 | 211 |
typedef HowardMmc<GR, CM, SetLargeCostTraits<T> > Create; |
| 212 | 212 |
}; |
| 213 | 213 |
|
| 214 | 214 |
template <typename T> |
| 215 | 215 |
struct SetPathTraits : public Traits {
|
| 216 | 216 |
typedef T Path; |
| 217 | 217 |
}; |
| 218 | 218 |
|
| 219 | 219 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 220 | 220 |
/// \c %Path type. |
| 221 | 221 |
/// |
| 222 | 222 |
/// \ref named-templ-param "Named parameter" for setting the \c %Path |
| 223 | 223 |
/// type of the found cycles. |
| 224 | 224 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 225 | 225 |
/// and it must have an \c addBack() function. |
| 226 | 226 |
template <typename T> |
| 227 | 227 |
struct SetPath |
| 228 | 228 |
: public HowardMmc<GR, CM, SetPathTraits<T> > {
|
| 229 | 229 |
typedef HowardMmc<GR, CM, SetPathTraits<T> > Create; |
| 230 | 230 |
}; |
| 231 |
|
|
| 231 |
|
|
| 232 | 232 |
/// @} |
| 233 | 233 |
|
| 234 | 234 |
protected: |
| 235 | 235 |
|
| 236 | 236 |
HowardMmc() {}
|
| 237 | 237 |
|
| 238 | 238 |
public: |
| 239 | 239 |
|
| 240 | 240 |
/// \brief Constructor. |
| 241 | 241 |
/// |
| 242 | 242 |
/// The constructor of the class. |
| 243 | 243 |
/// |
| 244 | 244 |
/// \param digraph The digraph the algorithm runs on. |
| 245 | 245 |
/// \param cost The costs of the arcs. |
| 246 | 246 |
HowardMmc( const Digraph &digraph, |
| 247 | 247 |
const CostMap &cost ) : |
| 248 | 248 |
_gr(digraph), _cost(cost), _best_found(false), |
| 249 | 249 |
_best_cost(0), _best_size(1), _cycle_path(NULL), _local_path(false), |
| 250 | 250 |
_policy(digraph), _reached(digraph), _level(digraph), _dist(digraph), |
| 251 | 251 |
_comp(digraph), _in_arcs(digraph), |
| 252 | 252 |
INF(std::numeric_limits<LargeCost>::has_infinity ? |
| 253 | 253 |
std::numeric_limits<LargeCost>::infinity() : |
| 254 | 254 |
std::numeric_limits<LargeCost>::max()) |
| 255 | 255 |
{}
|
| ... | ... |
@@ -313,49 +313,49 @@ |
| 313 | 313 |
/// This function runs the algorithm. |
| 314 | 314 |
/// It can be called more than once (e.g. if the underlying digraph |
| 315 | 315 |
/// and/or the arc costs have been modified). |
| 316 | 316 |
/// |
| 317 | 317 |
/// \return \c true if a directed cycle exists in the digraph. |
| 318 | 318 |
/// |
| 319 | 319 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
| 320 | 320 |
/// \code |
| 321 | 321 |
/// return mmc.findCycleMean() && mmc.findCycle(); |
| 322 | 322 |
/// \endcode |
| 323 | 323 |
bool run() {
|
| 324 | 324 |
return findCycleMean() && findCycle(); |
| 325 | 325 |
} |
| 326 | 326 |
|
| 327 | 327 |
/// \brief Find the minimum cycle mean. |
| 328 | 328 |
/// |
| 329 | 329 |
/// This function finds the minimum mean cost of the directed |
| 330 | 330 |
/// cycles in the digraph. |
| 331 | 331 |
/// |
| 332 | 332 |
/// \return \c true if a directed cycle exists in the digraph. |
| 333 | 333 |
bool findCycleMean() {
|
| 334 | 334 |
// Initialize and find strongly connected components |
| 335 | 335 |
init(); |
| 336 | 336 |
findComponents(); |
| 337 |
|
|
| 337 |
|
|
| 338 | 338 |
// Find the minimum cycle mean in the components |
| 339 | 339 |
for (int comp = 0; comp < _comp_num; ++comp) {
|
| 340 | 340 |
// Find the minimum mean cycle in the current component |
| 341 | 341 |
if (!buildPolicyGraph(comp)) continue; |
| 342 | 342 |
while (true) {
|
| 343 | 343 |
findPolicyCycle(); |
| 344 | 344 |
if (!computeNodeDistances()) break; |
| 345 | 345 |
} |
| 346 | 346 |
// Update the best cycle (global minimum mean cycle) |
| 347 | 347 |
if ( _curr_found && (!_best_found || |
| 348 | 348 |
_curr_cost * _best_size < _best_cost * _curr_size) ) {
|
| 349 | 349 |
_best_found = true; |
| 350 | 350 |
_best_cost = _curr_cost; |
| 351 | 351 |
_best_size = _curr_size; |
| 352 | 352 |
_best_node = _curr_node; |
| 353 | 353 |
} |
| 354 | 354 |
} |
| 355 | 355 |
return _best_found; |
| 356 | 356 |
} |
| 357 | 357 |
|
| 358 | 358 |
/// \brief Find a minimum mean directed cycle. |
| 359 | 359 |
/// |
| 360 | 360 |
/// This function finds a directed cycle of minimum mean cost |
| 361 | 361 |
/// in the digraph using the data computed by findCycleMean(). |
| ... | ... |
@@ -424,49 +424,49 @@ |
| 424 | 424 |
/// storing the found cycle. |
| 425 | 425 |
/// |
| 426 | 426 |
/// \pre \ref run() or \ref findCycle() must be called before using |
| 427 | 427 |
/// this function. |
| 428 | 428 |
const Path& cycle() const {
|
| 429 | 429 |
return *_cycle_path; |
| 430 | 430 |
} |
| 431 | 431 |
|
| 432 | 432 |
///@} |
| 433 | 433 |
|
| 434 | 434 |
private: |
| 435 | 435 |
|
| 436 | 436 |
// Initialize |
| 437 | 437 |
void init() {
|
| 438 | 438 |
if (!_cycle_path) {
|
| 439 | 439 |
_local_path = true; |
| 440 | 440 |
_cycle_path = new Path; |
| 441 | 441 |
} |
| 442 | 442 |
_queue.resize(countNodes(_gr)); |
| 443 | 443 |
_best_found = false; |
| 444 | 444 |
_best_cost = 0; |
| 445 | 445 |
_best_size = 1; |
| 446 | 446 |
_cycle_path->clear(); |
| 447 | 447 |
} |
| 448 |
|
|
| 448 |
|
|
| 449 | 449 |
// Find strongly connected components and initialize _comp_nodes |
| 450 | 450 |
// and _in_arcs |
| 451 | 451 |
void findComponents() {
|
| 452 | 452 |
_comp_num = stronglyConnectedComponents(_gr, _comp); |
| 453 | 453 |
_comp_nodes.resize(_comp_num); |
| 454 | 454 |
if (_comp_num == 1) {
|
| 455 | 455 |
_comp_nodes[0].clear(); |
| 456 | 456 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
| 457 | 457 |
_comp_nodes[0].push_back(n); |
| 458 | 458 |
_in_arcs[n].clear(); |
| 459 | 459 |
for (InArcIt a(_gr, n); a != INVALID; ++a) {
|
| 460 | 460 |
_in_arcs[n].push_back(a); |
| 461 | 461 |
} |
| 462 | 462 |
} |
| 463 | 463 |
} else {
|
| 464 | 464 |
for (int i = 0; i < _comp_num; ++i) |
| 465 | 465 |
_comp_nodes[i].clear(); |
| 466 | 466 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
| 467 | 467 |
int k = _comp[n]; |
| 468 | 468 |
_comp_nodes[k].push_back(n); |
| 469 | 469 |
_in_arcs[n].clear(); |
| 470 | 470 |
for (InArcIt a(_gr, n); a != INVALID; ++a) {
|
| 471 | 471 |
if (_comp[_gr.source(a)] == k) _in_arcs[n].push_back(a); |
| 472 | 472 |
} |
| 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_KARP_MMC_H |
| 20 | 20 |
#define LEMON_KARP_MMC_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup min_mean_cycle |
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief Karp's algorithm for finding a minimum mean cycle. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <lemon/core.h> |
| ... | ... |
@@ -170,49 +170,49 @@ |
| 170 | 170 |
// The cost of the arcs |
| 171 | 171 |
const CostMap &_cost; |
| 172 | 172 |
|
| 173 | 173 |
// Data for storing the strongly connected components |
| 174 | 174 |
int _comp_num; |
| 175 | 175 |
typename Digraph::template NodeMap<int> _comp; |
| 176 | 176 |
std::vector<std::vector<Node> > _comp_nodes; |
| 177 | 177 |
std::vector<Node>* _nodes; |
| 178 | 178 |
typename Digraph::template NodeMap<std::vector<Arc> > _out_arcs; |
| 179 | 179 |
|
| 180 | 180 |
// Data for the found cycle |
| 181 | 181 |
LargeCost _cycle_cost; |
| 182 | 182 |
int _cycle_size; |
| 183 | 183 |
Node _cycle_node; |
| 184 | 184 |
|
| 185 | 185 |
Path *_cycle_path; |
| 186 | 186 |
bool _local_path; |
| 187 | 187 |
|
| 188 | 188 |
// Node map for storing path data |
| 189 | 189 |
PathDataNodeMap _data; |
| 190 | 190 |
// The processed nodes in the last round |
| 191 | 191 |
std::vector<Node> _process; |
| 192 | 192 |
|
| 193 | 193 |
Tolerance _tolerance; |
| 194 |
|
|
| 194 |
|
|
| 195 | 195 |
// Infinite constant |
| 196 | 196 |
const LargeCost INF; |
| 197 | 197 |
|
| 198 | 198 |
public: |
| 199 | 199 |
|
| 200 | 200 |
/// \name Named Template Parameters |
| 201 | 201 |
/// @{
|
| 202 | 202 |
|
| 203 | 203 |
template <typename T> |
| 204 | 204 |
struct SetLargeCostTraits : public Traits {
|
| 205 | 205 |
typedef T LargeCost; |
| 206 | 206 |
typedef lemon::Tolerance<T> Tolerance; |
| 207 | 207 |
}; |
| 208 | 208 |
|
| 209 | 209 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 210 | 210 |
/// \c LargeCost type. |
| 211 | 211 |
/// |
| 212 | 212 |
/// \ref named-templ-param "Named parameter" for setting \c LargeCost |
| 213 | 213 |
/// type. It is used for internal computations in the algorithm. |
| 214 | 214 |
template <typename T> |
| 215 | 215 |
struct SetLargeCost |
| 216 | 216 |
: public KarpMmc<GR, CM, SetLargeCostTraits<T> > {
|
| 217 | 217 |
typedef KarpMmc<GR, CM, SetLargeCostTraits<T> > Create; |
| 218 | 218 |
}; |
| ... | ... |
@@ -318,49 +318,49 @@ |
| 318 | 318 |
/// This function runs the algorithm. |
| 319 | 319 |
/// It can be called more than once (e.g. if the underlying digraph |
| 320 | 320 |
/// and/or the arc costs have been modified). |
| 321 | 321 |
/// |
| 322 | 322 |
/// \return \c true if a directed cycle exists in the digraph. |
| 323 | 323 |
/// |
| 324 | 324 |
/// \note <tt>mmc.run()</tt> is just a shortcut of the following code. |
| 325 | 325 |
/// \code |
| 326 | 326 |
/// return mmc.findCycleMean() && mmc.findCycle(); |
| 327 | 327 |
/// \endcode |
| 328 | 328 |
bool run() {
|
| 329 | 329 |
return findCycleMean() && findCycle(); |
| 330 | 330 |
} |
| 331 | 331 |
|
| 332 | 332 |
/// \brief Find the minimum cycle mean. |
| 333 | 333 |
/// |
| 334 | 334 |
/// This function finds the minimum mean cost of the directed |
| 335 | 335 |
/// cycles in the digraph. |
| 336 | 336 |
/// |
| 337 | 337 |
/// \return \c true if a directed cycle exists in the digraph. |
| 338 | 338 |
bool findCycleMean() {
|
| 339 | 339 |
// Initialization and find strongly connected components |
| 340 | 340 |
init(); |
| 341 | 341 |
findComponents(); |
| 342 |
|
|
| 342 |
|
|
| 343 | 343 |
// Find the minimum cycle mean in the components |
| 344 | 344 |
for (int comp = 0; comp < _comp_num; ++comp) {
|
| 345 | 345 |
if (!initComponent(comp)) continue; |
| 346 | 346 |
processRounds(); |
| 347 | 347 |
updateMinMean(); |
| 348 | 348 |
} |
| 349 | 349 |
return (_cycle_node != INVALID); |
| 350 | 350 |
} |
| 351 | 351 |
|
| 352 | 352 |
/// \brief Find a minimum mean directed cycle. |
| 353 | 353 |
/// |
| 354 | 354 |
/// This function finds a directed cycle of minimum mean cost |
| 355 | 355 |
/// in the digraph using the data computed by findCycleMean(). |
| 356 | 356 |
/// |
| 357 | 357 |
/// \return \c true if a directed cycle exists in the digraph. |
| 358 | 358 |
/// |
| 359 | 359 |
/// \pre \ref findCycleMean() must be called before using this function. |
| 360 | 360 |
bool findCycle() {
|
| 361 | 361 |
if (_cycle_node == INVALID) return false; |
| 362 | 362 |
IntNodeMap reached(_gr, -1); |
| 363 | 363 |
int r = _data[_cycle_node].size(); |
| 364 | 364 |
Node u = _cycle_node; |
| 365 | 365 |
while (reached[u] < 0) {
|
| 366 | 366 |
reached[u] = --r; |
| ... | ... |
@@ -468,49 +468,49 @@ |
| 468 | 468 |
for (OutArcIt a(_gr, n); a != INVALID; ++a) {
|
| 469 | 469 |
_out_arcs[n].push_back(a); |
| 470 | 470 |
} |
| 471 | 471 |
} |
| 472 | 472 |
} else {
|
| 473 | 473 |
for (int i = 0; i < _comp_num; ++i) |
| 474 | 474 |
_comp_nodes[i].clear(); |
| 475 | 475 |
for (NodeIt n(_gr); n != INVALID; ++n) {
|
| 476 | 476 |
int k = _comp[n]; |
| 477 | 477 |
_comp_nodes[k].push_back(n); |
| 478 | 478 |
_out_arcs[n].clear(); |
| 479 | 479 |
for (OutArcIt a(_gr, n); a != INVALID; ++a) {
|
| 480 | 480 |
if (_comp[_gr.target(a)] == k) _out_arcs[n].push_back(a); |
| 481 | 481 |
} |
| 482 | 482 |
} |
| 483 | 483 |
} |
| 484 | 484 |
} |
| 485 | 485 |
|
| 486 | 486 |
// Initialize path data for the current component |
| 487 | 487 |
bool initComponent(int comp) {
|
| 488 | 488 |
_nodes = &(_comp_nodes[comp]); |
| 489 | 489 |
int n = _nodes->size(); |
| 490 | 490 |
if (n < 1 || (n == 1 && _out_arcs[(*_nodes)[0]].size() == 0)) {
|
| 491 | 491 |
return false; |
| 492 |
} |
|
| 492 |
} |
|
| 493 | 493 |
for (int i = 0; i < n; ++i) {
|
| 494 | 494 |
_data[(*_nodes)[i]].resize(n + 1, PathData(INF)); |
| 495 | 495 |
} |
| 496 | 496 |
return true; |
| 497 | 497 |
} |
| 498 | 498 |
|
| 499 | 499 |
// Process all rounds of computing path data for the current component. |
| 500 | 500 |
// _data[v][k] is the cost of a shortest directed walk from the root |
| 501 | 501 |
// node to node v containing exactly k arcs. |
| 502 | 502 |
void processRounds() {
|
| 503 | 503 |
Node start = (*_nodes)[0]; |
| 504 | 504 |
_data[start][0] = PathData(0); |
| 505 | 505 |
_process.clear(); |
| 506 | 506 |
_process.push_back(start); |
| 507 | 507 |
|
| 508 | 508 |
int k, n = _nodes->size(); |
| 509 | 509 |
for (k = 1; k <= n && int(_process.size()) < n; ++k) {
|
| 510 | 510 |
processNextBuildRound(k); |
| 511 | 511 |
} |
| 512 | 512 |
for ( ; k <= n; ++k) {
|
| 513 | 513 |
processNextFullRound(k); |
| 514 | 514 |
} |
| 515 | 515 |
} |
| 516 | 516 |
| 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 |
///\ingroup lemon_io |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief \ref lgf-format "LEMON Graph Format" reader. |
| 22 | 22 |
|
| 23 | 23 |
|
| 24 | 24 |
#ifndef LEMON_LGF_READER_H |
| 25 | 25 |
#define LEMON_LGF_READER_H |
| 26 | 26 |
|
| 27 | 27 |
#include <iostream> |
| 28 | 28 |
#include <fstream> |
| 29 | 29 |
#include <sstream> |
| ... | ... |
@@ -541,49 +541,49 @@ |
| 541 | 541 |
delete it->second; |
| 542 | 542 |
} |
| 543 | 543 |
|
| 544 | 544 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 545 | 545 |
it != _arc_maps.end(); ++it) {
|
| 546 | 546 |
delete it->second; |
| 547 | 547 |
} |
| 548 | 548 |
|
| 549 | 549 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 550 | 550 |
it != _attributes.end(); ++it) {
|
| 551 | 551 |
delete it->second; |
| 552 | 552 |
} |
| 553 | 553 |
|
| 554 | 554 |
if (local_is) {
|
| 555 | 555 |
delete _is; |
| 556 | 556 |
} |
| 557 | 557 |
|
| 558 | 558 |
} |
| 559 | 559 |
|
| 560 | 560 |
private: |
| 561 | 561 |
|
| 562 | 562 |
template <typename TDGR> |
| 563 | 563 |
friend DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is); |
| 564 | 564 |
template <typename TDGR> |
| 565 |
friend DigraphReader<TDGR> digraphReader(TDGR& digraph, |
|
| 565 |
friend DigraphReader<TDGR> digraphReader(TDGR& digraph, |
|
| 566 | 566 |
const std::string& fn); |
| 567 | 567 |
template <typename TDGR> |
| 568 | 568 |
friend DigraphReader<TDGR> digraphReader(TDGR& digraph, const char *fn); |
| 569 | 569 |
|
| 570 | 570 |
DigraphReader(DigraphReader& other) |
| 571 | 571 |
: _is(other._is), local_is(other.local_is), _digraph(other._digraph), |
| 572 | 572 |
_use_nodes(other._use_nodes), _use_arcs(other._use_arcs), |
| 573 | 573 |
_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
|
| 574 | 574 |
|
| 575 | 575 |
other._is = 0; |
| 576 | 576 |
other.local_is = false; |
| 577 | 577 |
|
| 578 | 578 |
_node_index.swap(other._node_index); |
| 579 | 579 |
_arc_index.swap(other._arc_index); |
| 580 | 580 |
|
| 581 | 581 |
_node_maps.swap(other._node_maps); |
| 582 | 582 |
_arc_maps.swap(other._arc_maps); |
| 583 | 583 |
_attributes.swap(other._attributes); |
| 584 | 584 |
|
| 585 | 585 |
_nodes_caption = other._nodes_caption; |
| 586 | 586 |
_arcs_caption = other._arcs_caption; |
| 587 | 587 |
_attributes_caption = other._attributes_caption; |
| 588 | 588 |
|
| 589 | 589 |
} |
| ... | ... |
@@ -1166,56 +1166,56 @@ |
| 1166 | 1166 |
} catch (FormatError& error) {
|
| 1167 | 1167 |
error.line(line_num); |
| 1168 | 1168 |
error.file(_filename); |
| 1169 | 1169 |
throw; |
| 1170 | 1170 |
} |
| 1171 | 1171 |
} |
| 1172 | 1172 |
|
| 1173 | 1173 |
if (!nodes_done) {
|
| 1174 | 1174 |
throw FormatError("Section @nodes not found");
|
| 1175 | 1175 |
} |
| 1176 | 1176 |
|
| 1177 | 1177 |
if (!arcs_done) {
|
| 1178 | 1178 |
throw FormatError("Section @arcs not found");
|
| 1179 | 1179 |
} |
| 1180 | 1180 |
|
| 1181 | 1181 |
if (!attributes_done && !_attributes.empty()) {
|
| 1182 | 1182 |
throw FormatError("Section @attributes not found");
|
| 1183 | 1183 |
} |
| 1184 | 1184 |
|
| 1185 | 1185 |
} |
| 1186 | 1186 |
|
| 1187 | 1187 |
/// @} |
| 1188 | 1188 |
|
| 1189 | 1189 |
}; |
| 1190 |
|
|
| 1190 |
|
|
| 1191 | 1191 |
/// \ingroup lemon_io |
| 1192 | 1192 |
/// |
| 1193 | 1193 |
/// \brief Return a \ref DigraphReader class |
| 1194 | 1194 |
/// |
| 1195 | 1195 |
/// This function just returns a \ref DigraphReader class. |
| 1196 | 1196 |
/// |
| 1197 |
/// With this function a digraph can be read from an |
|
| 1197 |
/// With this function a digraph can be read from an |
|
| 1198 | 1198 |
/// \ref lgf-format "LGF" file or input stream with several maps and |
| 1199 | 1199 |
/// attributes. For example, there is network flow problem on a |
| 1200 | 1200 |
/// digraph, i.e. a digraph with a \e capacity map on the arcs and |
| 1201 | 1201 |
/// \e source and \e target nodes. This digraph can be read with the |
| 1202 | 1202 |
/// following code: |
| 1203 | 1203 |
/// |
| 1204 | 1204 |
///\code |
| 1205 | 1205 |
///ListDigraph digraph; |
| 1206 | 1206 |
///ListDigraph::ArcMap<int> cm(digraph); |
| 1207 | 1207 |
///ListDigraph::Node src, trg; |
| 1208 | 1208 |
///digraphReader(digraph, std::cin). |
| 1209 | 1209 |
/// arcMap("capacity", cap).
|
| 1210 | 1210 |
/// node("source", src).
|
| 1211 | 1211 |
/// node("target", trg).
|
| 1212 | 1212 |
/// run(); |
| 1213 | 1213 |
///\endcode |
| 1214 | 1214 |
/// |
| 1215 | 1215 |
/// For a complete documentation, please see the \ref DigraphReader |
| 1216 | 1216 |
/// class documentation. |
| 1217 | 1217 |
/// \warning Don't forget to put the \ref DigraphReader::run() "run()" |
| 1218 | 1218 |
/// to the end of the parameter list. |
| 1219 | 1219 |
/// \relates DigraphReader |
| 1220 | 1220 |
/// \sa digraphReader(TDGR& digraph, const std::string& fn) |
| 1221 | 1221 |
/// \sa digraphReader(TDGR& digraph, const char* fn) |
| ... | ... |
@@ -1228,49 +1228,49 @@ |
| 1228 | 1228 |
/// \brief Return a \ref DigraphReader class |
| 1229 | 1229 |
/// |
| 1230 | 1230 |
/// This function just returns a \ref DigraphReader class. |
| 1231 | 1231 |
/// \relates DigraphReader |
| 1232 | 1232 |
/// \sa digraphReader(TDGR& digraph, std::istream& is) |
| 1233 | 1233 |
template <typename TDGR> |
| 1234 | 1234 |
DigraphReader<TDGR> digraphReader(TDGR& digraph, const std::string& fn) {
|
| 1235 | 1235 |
DigraphReader<TDGR> tmp(digraph, fn); |
| 1236 | 1236 |
return tmp; |
| 1237 | 1237 |
} |
| 1238 | 1238 |
|
| 1239 | 1239 |
/// \brief Return a \ref DigraphReader class |
| 1240 | 1240 |
/// |
| 1241 | 1241 |
/// This function just returns a \ref DigraphReader class. |
| 1242 | 1242 |
/// \relates DigraphReader |
| 1243 | 1243 |
/// \sa digraphReader(TDGR& digraph, std::istream& is) |
| 1244 | 1244 |
template <typename TDGR> |
| 1245 | 1245 |
DigraphReader<TDGR> digraphReader(TDGR& digraph, const char* fn) {
|
| 1246 | 1246 |
DigraphReader<TDGR> tmp(digraph, fn); |
| 1247 | 1247 |
return tmp; |
| 1248 | 1248 |
} |
| 1249 | 1249 |
|
| 1250 | 1250 |
template <typename GR> |
| 1251 | 1251 |
class GraphReader; |
| 1252 |
|
|
| 1252 |
|
|
| 1253 | 1253 |
template <typename TGR> |
| 1254 | 1254 |
GraphReader<TGR> graphReader(TGR& graph, std::istream& is = std::cin); |
| 1255 | 1255 |
template <typename TGR> |
| 1256 | 1256 |
GraphReader<TGR> graphReader(TGR& graph, const std::string& fn); |
| 1257 | 1257 |
template <typename TGR> |
| 1258 | 1258 |
GraphReader<TGR> graphReader(TGR& graph, const char *fn); |
| 1259 | 1259 |
|
| 1260 | 1260 |
/// \ingroup lemon_io |
| 1261 | 1261 |
/// |
| 1262 | 1262 |
/// \brief \ref lgf-format "LGF" reader for undirected graphs |
| 1263 | 1263 |
/// |
| 1264 | 1264 |
/// This utility reads an \ref lgf-format "LGF" file. |
| 1265 | 1265 |
/// |
| 1266 | 1266 |
/// It can be used almost the same way as \c DigraphReader. |
| 1267 | 1267 |
/// The only difference is that this class can handle edges and |
| 1268 | 1268 |
/// edge maps as well as arcs and arc maps. |
| 1269 | 1269 |
/// |
| 1270 | 1270 |
/// The columns in the \c \@edges (or \c \@arcs) section are the |
| 1271 | 1271 |
/// edge maps. However, if there are two maps with the same name |
| 1272 | 1272 |
/// prefixed with \c '+' and \c '-', then these can be read into an |
| 1273 | 1273 |
/// arc map. Similarly, an attribute can be read into an arc, if |
| 1274 | 1274 |
/// it's value is an edge label prefixed with \c '+' or \c '-'. |
| 1275 | 1275 |
template <typename GR> |
| 1276 | 1276 |
class GraphReader {
|
| ... | ... |
@@ -1365,49 +1365,49 @@ |
| 1365 | 1365 |
it != _node_maps.end(); ++it) {
|
| 1366 | 1366 |
delete it->second; |
| 1367 | 1367 |
} |
| 1368 | 1368 |
|
| 1369 | 1369 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1370 | 1370 |
it != _edge_maps.end(); ++it) {
|
| 1371 | 1371 |
delete it->second; |
| 1372 | 1372 |
} |
| 1373 | 1373 |
|
| 1374 | 1374 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1375 | 1375 |
it != _attributes.end(); ++it) {
|
| 1376 | 1376 |
delete it->second; |
| 1377 | 1377 |
} |
| 1378 | 1378 |
|
| 1379 | 1379 |
if (local_is) {
|
| 1380 | 1380 |
delete _is; |
| 1381 | 1381 |
} |
| 1382 | 1382 |
|
| 1383 | 1383 |
} |
| 1384 | 1384 |
|
| 1385 | 1385 |
private: |
| 1386 | 1386 |
template <typename TGR> |
| 1387 | 1387 |
friend GraphReader<TGR> graphReader(TGR& graph, std::istream& is); |
| 1388 | 1388 |
template <typename TGR> |
| 1389 |
friend GraphReader<TGR> graphReader(TGR& graph, const std::string& fn); |
|
| 1389 |
friend GraphReader<TGR> graphReader(TGR& graph, const std::string& fn); |
|
| 1390 | 1390 |
template <typename TGR> |
| 1391 | 1391 |
friend GraphReader<TGR> graphReader(TGR& graph, const char *fn); |
| 1392 | 1392 |
|
| 1393 | 1393 |
GraphReader(GraphReader& other) |
| 1394 | 1394 |
: _is(other._is), local_is(other.local_is), _graph(other._graph), |
| 1395 | 1395 |
_use_nodes(other._use_nodes), _use_edges(other._use_edges), |
| 1396 | 1396 |
_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
|
| 1397 | 1397 |
|
| 1398 | 1398 |
other._is = 0; |
| 1399 | 1399 |
other.local_is = false; |
| 1400 | 1400 |
|
| 1401 | 1401 |
_node_index.swap(other._node_index); |
| 1402 | 1402 |
_edge_index.swap(other._edge_index); |
| 1403 | 1403 |
|
| 1404 | 1404 |
_node_maps.swap(other._node_maps); |
| 1405 | 1405 |
_edge_maps.swap(other._edge_maps); |
| 1406 | 1406 |
_attributes.swap(other._attributes); |
| 1407 | 1407 |
|
| 1408 | 1408 |
_nodes_caption = other._nodes_caption; |
| 1409 | 1409 |
_edges_caption = other._edges_caption; |
| 1410 | 1410 |
_attributes_caption = other._attributes_caption; |
| 1411 | 1411 |
|
| 1412 | 1412 |
} |
| 1413 | 1413 |
|
| ... | ... |
@@ -2042,51 +2042,51 @@ |
| 2042 | 2042 |
} |
| 2043 | 2043 |
|
| 2044 | 2044 |
if (!nodes_done) {
|
| 2045 | 2045 |
throw FormatError("Section @nodes not found");
|
| 2046 | 2046 |
} |
| 2047 | 2047 |
|
| 2048 | 2048 |
if (!edges_done) {
|
| 2049 | 2049 |
throw FormatError("Section @edges not found");
|
| 2050 | 2050 |
} |
| 2051 | 2051 |
|
| 2052 | 2052 |
if (!attributes_done && !_attributes.empty()) {
|
| 2053 | 2053 |
throw FormatError("Section @attributes not found");
|
| 2054 | 2054 |
} |
| 2055 | 2055 |
|
| 2056 | 2056 |
} |
| 2057 | 2057 |
|
| 2058 | 2058 |
/// @} |
| 2059 | 2059 |
|
| 2060 | 2060 |
}; |
| 2061 | 2061 |
|
| 2062 | 2062 |
/// \ingroup lemon_io |
| 2063 | 2063 |
/// |
| 2064 | 2064 |
/// \brief Return a \ref GraphReader class |
| 2065 | 2065 |
/// |
| 2066 |
/// This function just returns a \ref GraphReader class. |
|
| 2066 |
/// This function just returns a \ref GraphReader class. |
|
| 2067 | 2067 |
/// |
| 2068 |
/// With this function a graph can be read from an |
|
| 2068 |
/// With this function a graph can be read from an |
|
| 2069 | 2069 |
/// \ref lgf-format "LGF" file or input stream with several maps and |
| 2070 | 2070 |
/// attributes. For example, there is weighted matching problem on a |
| 2071 | 2071 |
/// graph, i.e. a graph with a \e weight map on the edges. This |
| 2072 | 2072 |
/// graph can be read with the following code: |
| 2073 | 2073 |
/// |
| 2074 | 2074 |
///\code |
| 2075 | 2075 |
///ListGraph graph; |
| 2076 | 2076 |
///ListGraph::EdgeMap<int> weight(graph); |
| 2077 | 2077 |
///graphReader(graph, std::cin). |
| 2078 | 2078 |
/// edgeMap("weight", weight).
|
| 2079 | 2079 |
/// run(); |
| 2080 | 2080 |
///\endcode |
| 2081 | 2081 |
/// |
| 2082 | 2082 |
/// For a complete documentation, please see the \ref GraphReader |
| 2083 | 2083 |
/// class documentation. |
| 2084 | 2084 |
/// \warning Don't forget to put the \ref GraphReader::run() "run()" |
| 2085 | 2085 |
/// to the end of the parameter list. |
| 2086 | 2086 |
/// \relates GraphReader |
| 2087 | 2087 |
/// \sa graphReader(TGR& graph, const std::string& fn) |
| 2088 | 2088 |
/// \sa graphReader(TGR& graph, const char* fn) |
| 2089 | 2089 |
template <typename TGR> |
| 2090 | 2090 |
GraphReader<TGR> graphReader(TGR& graph, std::istream& is) {
|
| 2091 | 2091 |
GraphReader<TGR> tmp(graph, is); |
| 2092 | 2092 |
return tmp; |
| 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 |
///\ingroup lemon_io |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief \ref lgf-format "LEMON Graph Format" writer. |
| 22 | 22 |
|
| 23 | 23 |
|
| 24 | 24 |
#ifndef LEMON_LGF_WRITER_H |
| 25 | 25 |
#define LEMON_LGF_WRITER_H |
| 26 | 26 |
|
| 27 | 27 |
#include <iostream> |
| 28 | 28 |
#include <fstream> |
| 29 | 29 |
#include <sstream> |
| ... | ... |
@@ -330,49 +330,49 @@ |
| 330 | 330 |
}; |
| 331 | 331 |
|
| 332 | 332 |
template <typename Functor> |
| 333 | 333 |
class StreamSection : public Section {
|
| 334 | 334 |
private: |
| 335 | 335 |
|
| 336 | 336 |
Functor _functor; |
| 337 | 337 |
|
| 338 | 338 |
public: |
| 339 | 339 |
|
| 340 | 340 |
StreamSection(const Functor& functor) : _functor(functor) {}
|
| 341 | 341 |
virtual ~StreamSection() {}
|
| 342 | 342 |
|
| 343 | 343 |
virtual void process(std::ostream& os) {
|
| 344 | 344 |
_functor(os); |
| 345 | 345 |
} |
| 346 | 346 |
}; |
| 347 | 347 |
|
| 348 | 348 |
} |
| 349 | 349 |
|
| 350 | 350 |
template <typename DGR> |
| 351 | 351 |
class DigraphWriter; |
| 352 | 352 |
|
| 353 | 353 |
template <typename TDGR> |
| 354 |
DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, |
|
| 354 |
DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, |
|
| 355 | 355 |
std::ostream& os = std::cout); |
| 356 | 356 |
template <typename TDGR> |
| 357 | 357 |
DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, const std::string& fn); |
| 358 | 358 |
|
| 359 | 359 |
template <typename TDGR> |
| 360 | 360 |
DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, const char* fn); |
| 361 | 361 |
|
| 362 | 362 |
|
| 363 | 363 |
/// \ingroup lemon_io |
| 364 | 364 |
/// |
| 365 | 365 |
/// \brief \ref lgf-format "LGF" writer for directed graphs |
| 366 | 366 |
/// |
| 367 | 367 |
/// This utility writes an \ref lgf-format "LGF" file. |
| 368 | 368 |
/// |
| 369 | 369 |
/// The writing method does a batch processing. The user creates a |
| 370 | 370 |
/// writer object, then various writing rules can be added to the |
| 371 | 371 |
/// writer, and eventually the writing is executed with the \c run() |
| 372 | 372 |
/// member function. A map writing rule can be added to the writer |
| 373 | 373 |
/// with the \c nodeMap() or \c arcMap() members. An optional |
| 374 | 374 |
/// converter parameter can also be added as a standard functor |
| 375 | 375 |
/// converting from the value type of the map to \c std::string. If it |
| 376 | 376 |
/// is set, it will determine how the value type of the map is written to |
| 377 | 377 |
/// the output stream. If the functor is not set, then a default |
| 378 | 378 |
/// conversion will be used. The \c attribute(), \c node() and \c |
| ... | ... |
@@ -483,49 +483,49 @@ |
| 483 | 483 |
~DigraphWriter() {
|
| 484 | 484 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 485 | 485 |
it != _node_maps.end(); ++it) {
|
| 486 | 486 |
delete it->second; |
| 487 | 487 |
} |
| 488 | 488 |
|
| 489 | 489 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 490 | 490 |
it != _arc_maps.end(); ++it) {
|
| 491 | 491 |
delete it->second; |
| 492 | 492 |
} |
| 493 | 493 |
|
| 494 | 494 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 495 | 495 |
it != _attributes.end(); ++it) {
|
| 496 | 496 |
delete it->second; |
| 497 | 497 |
} |
| 498 | 498 |
|
| 499 | 499 |
if (local_os) {
|
| 500 | 500 |
delete _os; |
| 501 | 501 |
} |
| 502 | 502 |
} |
| 503 | 503 |
|
| 504 | 504 |
private: |
| 505 | 505 |
|
| 506 | 506 |
template <typename TDGR> |
| 507 |
friend DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, |
|
| 507 |
friend DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, |
|
| 508 | 508 |
std::ostream& os); |
| 509 | 509 |
template <typename TDGR> |
| 510 | 510 |
friend DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, |
| 511 | 511 |
const std::string& fn); |
| 512 | 512 |
template <typename TDGR> |
| 513 | 513 |
friend DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, |
| 514 | 514 |
const char *fn); |
| 515 | 515 |
|
| 516 | 516 |
DigraphWriter(DigraphWriter& other) |
| 517 | 517 |
: _os(other._os), local_os(other.local_os), _digraph(other._digraph), |
| 518 | 518 |
_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
|
| 519 | 519 |
|
| 520 | 520 |
other._os = 0; |
| 521 | 521 |
other.local_os = false; |
| 522 | 522 |
|
| 523 | 523 |
_node_index.swap(other._node_index); |
| 524 | 524 |
_arc_index.swap(other._arc_index); |
| 525 | 525 |
|
| 526 | 526 |
_node_maps.swap(other._node_maps); |
| 527 | 527 |
_arc_maps.swap(other._arc_maps); |
| 528 | 528 |
_attributes.swap(other._attributes); |
| 529 | 529 |
|
| 530 | 530 |
_nodes_caption = other._nodes_caption; |
| 531 | 531 |
_arcs_caption = other._arcs_caption; |
| ... | ... |
@@ -896,89 +896,89 @@ |
| 896 | 896 |
createNodeIndex(); |
| 897 | 897 |
} |
| 898 | 898 |
if (!_skip_arcs) {
|
| 899 | 899 |
writeArcs(); |
| 900 | 900 |
} else {
|
| 901 | 901 |
createArcIndex(); |
| 902 | 902 |
} |
| 903 | 903 |
writeAttributes(); |
| 904 | 904 |
} |
| 905 | 905 |
|
| 906 | 906 |
/// \brief Give back the stream of the writer |
| 907 | 907 |
/// |
| 908 | 908 |
/// Give back the stream of the writer. |
| 909 | 909 |
std::ostream& ostream() {
|
| 910 | 910 |
return *_os; |
| 911 | 911 |
} |
| 912 | 912 |
|
| 913 | 913 |
/// @} |
| 914 | 914 |
}; |
| 915 | 915 |
|
| 916 | 916 |
/// \ingroup lemon_io |
| 917 | 917 |
/// |
| 918 | 918 |
/// \brief Return a \ref DigraphWriter class |
| 919 | 919 |
/// |
| 920 |
/// This function just returns a \ref DigraphWriter class. |
|
| 920 |
/// This function just returns a \ref DigraphWriter class. |
|
| 921 | 921 |
/// |
| 922 | 922 |
/// With this function a digraph can be write to a file or output |
| 923 | 923 |
/// stream in \ref lgf-format "LGF" format with several maps and |
| 924 | 924 |
/// attributes. For example, with the following code a network flow |
| 925 | 925 |
/// problem can be written to the standard output, i.e. a digraph |
| 926 | 926 |
/// with a \e capacity map on the arcs and \e source and \e target |
| 927 | 927 |
/// nodes: |
| 928 | 928 |
/// |
| 929 | 929 |
///\code |
| 930 | 930 |
///ListDigraph digraph; |
| 931 | 931 |
///ListDigraph::ArcMap<int> cap(digraph); |
| 932 | 932 |
///ListDigraph::Node src, trg; |
| 933 | 933 |
/// // Setting the capacity map and source and target nodes |
| 934 | 934 |
///digraphWriter(digraph, std::cout). |
| 935 | 935 |
/// arcMap("capacity", cap).
|
| 936 | 936 |
/// node("source", src).
|
| 937 | 937 |
/// node("target", trg).
|
| 938 | 938 |
/// run(); |
| 939 | 939 |
///\endcode |
| 940 | 940 |
/// |
| 941 | 941 |
/// For a complete documentation, please see the \ref DigraphWriter |
| 942 | 942 |
/// class documentation. |
| 943 | 943 |
/// \warning Don't forget to put the \ref DigraphWriter::run() "run()" |
| 944 | 944 |
/// to the end of the parameter list. |
| 945 | 945 |
/// \relates DigraphWriter |
| 946 | 946 |
/// \sa digraphWriter(const TDGR& digraph, const std::string& fn) |
| 947 | 947 |
/// \sa digraphWriter(const TDGR& digraph, const char* fn) |
| 948 | 948 |
template <typename TDGR> |
| 949 | 949 |
DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, std::ostream& os) {
|
| 950 | 950 |
DigraphWriter<TDGR> tmp(digraph, os); |
| 951 | 951 |
return tmp; |
| 952 | 952 |
} |
| 953 | 953 |
|
| 954 | 954 |
/// \brief Return a \ref DigraphWriter class |
| 955 | 955 |
/// |
| 956 | 956 |
/// This function just returns a \ref DigraphWriter class. |
| 957 | 957 |
/// \relates DigraphWriter |
| 958 | 958 |
/// \sa digraphWriter(const TDGR& digraph, std::ostream& os) |
| 959 | 959 |
template <typename TDGR> |
| 960 |
DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, |
|
| 960 |
DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, |
|
| 961 | 961 |
const std::string& fn) {
|
| 962 | 962 |
DigraphWriter<TDGR> tmp(digraph, fn); |
| 963 | 963 |
return tmp; |
| 964 | 964 |
} |
| 965 | 965 |
|
| 966 | 966 |
/// \brief Return a \ref DigraphWriter class |
| 967 | 967 |
/// |
| 968 | 968 |
/// This function just returns a \ref DigraphWriter class. |
| 969 | 969 |
/// \relates DigraphWriter |
| 970 | 970 |
/// \sa digraphWriter(const TDGR& digraph, std::ostream& os) |
| 971 | 971 |
template <typename TDGR> |
| 972 | 972 |
DigraphWriter<TDGR> digraphWriter(const TDGR& digraph, const char* fn) {
|
| 973 | 973 |
DigraphWriter<TDGR> tmp(digraph, fn); |
| 974 | 974 |
return tmp; |
| 975 | 975 |
} |
| 976 | 976 |
|
| 977 | 977 |
template <typename GR> |
| 978 | 978 |
class GraphWriter; |
| 979 | 979 |
|
| 980 | 980 |
template <typename TGR> |
| 981 | 981 |
GraphWriter<TGR> graphWriter(const TGR& graph, std::ostream& os = std::cout); |
| 982 | 982 |
template <typename TGR> |
| 983 | 983 |
GraphWriter<TGR> graphWriter(const TGR& graph, const std::string& fn); |
| 984 | 984 |
template <typename TGR> |
| ... | ... |
@@ -1080,53 +1080,53 @@ |
| 1080 | 1080 |
it != _node_maps.end(); ++it) {
|
| 1081 | 1081 |
delete it->second; |
| 1082 | 1082 |
} |
| 1083 | 1083 |
|
| 1084 | 1084 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1085 | 1085 |
it != _edge_maps.end(); ++it) {
|
| 1086 | 1086 |
delete it->second; |
| 1087 | 1087 |
} |
| 1088 | 1088 |
|
| 1089 | 1089 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1090 | 1090 |
it != _attributes.end(); ++it) {
|
| 1091 | 1091 |
delete it->second; |
| 1092 | 1092 |
} |
| 1093 | 1093 |
|
| 1094 | 1094 |
if (local_os) {
|
| 1095 | 1095 |
delete _os; |
| 1096 | 1096 |
} |
| 1097 | 1097 |
} |
| 1098 | 1098 |
|
| 1099 | 1099 |
private: |
| 1100 | 1100 |
|
| 1101 | 1101 |
template <typename TGR> |
| 1102 | 1102 |
friend GraphWriter<TGR> graphWriter(const TGR& graph, std::ostream& os); |
| 1103 | 1103 |
template <typename TGR> |
| 1104 |
friend GraphWriter<TGR> graphWriter(const TGR& graph, |
|
| 1104 |
friend GraphWriter<TGR> graphWriter(const TGR& graph, |
|
| 1105 | 1105 |
const std::string& fn); |
| 1106 | 1106 |
template <typename TGR> |
| 1107 | 1107 |
friend GraphWriter<TGR> graphWriter(const TGR& graph, const char *fn); |
| 1108 |
|
|
| 1108 |
|
|
| 1109 | 1109 |
GraphWriter(GraphWriter& other) |
| 1110 | 1110 |
: _os(other._os), local_os(other.local_os), _graph(other._graph), |
| 1111 | 1111 |
_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
|
| 1112 | 1112 |
|
| 1113 | 1113 |
other._os = 0; |
| 1114 | 1114 |
other.local_os = false; |
| 1115 | 1115 |
|
| 1116 | 1116 |
_node_index.swap(other._node_index); |
| 1117 | 1117 |
_edge_index.swap(other._edge_index); |
| 1118 | 1118 |
|
| 1119 | 1119 |
_node_maps.swap(other._node_maps); |
| 1120 | 1120 |
_edge_maps.swap(other._edge_maps); |
| 1121 | 1121 |
_attributes.swap(other._attributes); |
| 1122 | 1122 |
|
| 1123 | 1123 |
_nodes_caption = other._nodes_caption; |
| 1124 | 1124 |
_edges_caption = other._edges_caption; |
| 1125 | 1125 |
_attributes_caption = other._attributes_caption; |
| 1126 | 1126 |
} |
| 1127 | 1127 |
|
| 1128 | 1128 |
GraphWriter& operator=(const GraphWriter&); |
| 1129 | 1129 |
|
| 1130 | 1130 |
public: |
| 1131 | 1131 |
|
| 1132 | 1132 |
/// \name Writing Rules |
| ... | ... |
@@ -1535,49 +1535,49 @@ |
| 1535 | 1535 |
createNodeIndex(); |
| 1536 | 1536 |
} |
| 1537 | 1537 |
if (!_skip_edges) {
|
| 1538 | 1538 |
writeEdges(); |
| 1539 | 1539 |
} else {
|
| 1540 | 1540 |
createEdgeIndex(); |
| 1541 | 1541 |
} |
| 1542 | 1542 |
writeAttributes(); |
| 1543 | 1543 |
} |
| 1544 | 1544 |
|
| 1545 | 1545 |
/// \brief Give back the stream of the writer |
| 1546 | 1546 |
/// |
| 1547 | 1547 |
/// Give back the stream of the writer |
| 1548 | 1548 |
std::ostream& ostream() {
|
| 1549 | 1549 |
return *_os; |
| 1550 | 1550 |
} |
| 1551 | 1551 |
|
| 1552 | 1552 |
/// @} |
| 1553 | 1553 |
}; |
| 1554 | 1554 |
|
| 1555 | 1555 |
/// \ingroup lemon_io |
| 1556 | 1556 |
/// |
| 1557 | 1557 |
/// \brief Return a \ref GraphWriter class |
| 1558 | 1558 |
/// |
| 1559 |
/// This function just returns a \ref GraphWriter class. |
|
| 1559 |
/// This function just returns a \ref GraphWriter class. |
|
| 1560 | 1560 |
/// |
| 1561 | 1561 |
/// With this function a graph can be write to a file or output |
| 1562 | 1562 |
/// stream in \ref lgf-format "LGF" format with several maps and |
| 1563 | 1563 |
/// attributes. For example, with the following code a weighted |
| 1564 | 1564 |
/// matching problem can be written to the standard output, i.e. a |
| 1565 | 1565 |
/// graph with a \e weight map on the edges: |
| 1566 | 1566 |
/// |
| 1567 | 1567 |
///\code |
| 1568 | 1568 |
///ListGraph graph; |
| 1569 | 1569 |
///ListGraph::EdgeMap<int> weight(graph); |
| 1570 | 1570 |
/// // Setting the weight map |
| 1571 | 1571 |
///graphWriter(graph, std::cout). |
| 1572 | 1572 |
/// edgeMap("weight", weight).
|
| 1573 | 1573 |
/// run(); |
| 1574 | 1574 |
///\endcode |
| 1575 | 1575 |
/// |
| 1576 | 1576 |
/// For a complete documentation, please see the \ref GraphWriter |
| 1577 | 1577 |
/// class documentation. |
| 1578 | 1578 |
/// \warning Don't forget to put the \ref GraphWriter::run() "run()" |
| 1579 | 1579 |
/// to the end of the parameter list. |
| 1580 | 1580 |
/// \relates GraphWriter |
| 1581 | 1581 |
/// \sa graphWriter(const TGR& graph, const std::string& fn) |
| 1582 | 1582 |
/// \sa graphWriter(const TGR& graph, const char* fn) |
| 1583 | 1583 |
template <typename TGR> |
| 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_LIST_GRAPH_H |
| 20 | 20 |
#define LEMON_LIST_GRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup graphs |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief ListDigraph and ListGraph classes. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/core.h> |
| 27 | 27 |
#include <lemon/error.h> |
| 28 | 28 |
#include <lemon/bits/graph_extender.h> |
| 29 | 29 |
|
| ... | ... |
@@ -425,49 +425,49 @@ |
| 425 | 425 |
/// This function reverses the direction of the given arc. |
| 426 | 426 |
///\note \c ArcIt, \c OutArcIt and \c InArcIt iterators referencing |
| 427 | 427 |
///the changed arc are invalidated. |
| 428 | 428 |
/// |
| 429 | 429 |
///\warning This functionality cannot be used together with the Snapshot |
| 430 | 430 |
///feature. |
| 431 | 431 |
void reverseArc(Arc a) {
|
| 432 | 432 |
Node t=target(a); |
| 433 | 433 |
changeTarget(a,source(a)); |
| 434 | 434 |
changeSource(a,t); |
| 435 | 435 |
} |
| 436 | 436 |
|
| 437 | 437 |
///Contract two nodes. |
| 438 | 438 |
|
| 439 | 439 |
///This function contracts the given two nodes. |
| 440 | 440 |
///Node \c v is removed, but instead of deleting its |
| 441 | 441 |
///incident arcs, they are joined to node \c u. |
| 442 | 442 |
///If the last parameter \c r is \c true (this is the default value), |
| 443 | 443 |
///then the newly created loops are removed. |
| 444 | 444 |
/// |
| 445 | 445 |
///\note The moved arcs are joined to node \c u using changeSource() |
| 446 | 446 |
///or changeTarget(), thus \c ArcIt and \c OutArcIt iterators are |
| 447 | 447 |
///invalidated for the outgoing arcs of node \c v and \c InArcIt |
| 448 | 448 |
///iterators are invalidated for the incomming arcs of \c v. |
| 449 |
///Moreover all iterators referencing node \c v or the removed |
|
| 449 |
///Moreover all iterators referencing node \c v or the removed |
|
| 450 | 450 |
///loops are also invalidated. Other iterators remain valid. |
| 451 | 451 |
/// |
| 452 | 452 |
///\warning This functionality cannot be used together with the Snapshot |
| 453 | 453 |
///feature. |
| 454 | 454 |
void contract(Node u, Node v, bool r = true) |
| 455 | 455 |
{
|
| 456 | 456 |
for(OutArcIt e(*this,v);e!=INVALID;) {
|
| 457 | 457 |
OutArcIt f=e; |
| 458 | 458 |
++f; |
| 459 | 459 |
if(r && target(e)==u) erase(e); |
| 460 | 460 |
else changeSource(e,u); |
| 461 | 461 |
e=f; |
| 462 | 462 |
} |
| 463 | 463 |
for(InArcIt e(*this,v);e!=INVALID;) {
|
| 464 | 464 |
InArcIt f=e; |
| 465 | 465 |
++f; |
| 466 | 466 |
if(r && source(e)==u) erase(e); |
| 467 | 467 |
else changeTarget(e,u); |
| 468 | 468 |
e=f; |
| 469 | 469 |
} |
| 470 | 470 |
erase(v); |
| 471 | 471 |
} |
| 472 | 472 |
|
| 473 | 473 |
///Split a node. |
| ... | ... |
@@ -531,49 +531,49 @@ |
| 531 | 531 |
/// be large (e.g. it will contain millions of nodes and/or arcs), |
| 532 | 532 |
/// then it is worth reserving space for this amount before starting |
| 533 | 533 |
/// to build the digraph. |
| 534 | 534 |
/// \sa reserveArc() |
| 535 | 535 |
void reserveNode(int n) { nodes.reserve(n); };
|
| 536 | 536 |
|
| 537 | 537 |
/// Reserve memory for arcs. |
| 538 | 538 |
|
| 539 | 539 |
/// Using this function, it is possible to avoid superfluous memory |
| 540 | 540 |
/// allocation: if you know that the digraph you want to build will |
| 541 | 541 |
/// be large (e.g. it will contain millions of nodes and/or arcs), |
| 542 | 542 |
/// then it is worth reserving space for this amount before starting |
| 543 | 543 |
/// to build the digraph. |
| 544 | 544 |
/// \sa reserveNode() |
| 545 | 545 |
void reserveArc(int m) { arcs.reserve(m); };
|
| 546 | 546 |
|
| 547 | 547 |
/// \brief Class to make a snapshot of the digraph and restore |
| 548 | 548 |
/// it later. |
| 549 | 549 |
/// |
| 550 | 550 |
/// Class to make a snapshot of the digraph and restore it later. |
| 551 | 551 |
/// |
| 552 | 552 |
/// The newly added nodes and arcs can be removed using the |
| 553 | 553 |
/// restore() function. |
| 554 | 554 |
/// |
| 555 |
/// \note After a state is restored, you cannot restore a later state, |
|
| 555 |
/// \note After a state is restored, you cannot restore a later state, |
|
| 556 | 556 |
/// i.e. you cannot add the removed nodes and arcs again using |
| 557 | 557 |
/// another Snapshot instance. |
| 558 | 558 |
/// |
| 559 | 559 |
/// \warning Node and arc deletions and other modifications (e.g. |
| 560 | 560 |
/// reversing, contracting, splitting arcs or nodes) cannot be |
| 561 | 561 |
/// restored. These events invalidate the snapshot. |
| 562 | 562 |
/// However, the arcs and nodes that were added to the digraph after |
| 563 | 563 |
/// making the current snapshot can be removed without invalidating it. |
| 564 | 564 |
class Snapshot {
|
| 565 | 565 |
protected: |
| 566 | 566 |
|
| 567 | 567 |
typedef Parent::NodeNotifier NodeNotifier; |
| 568 | 568 |
|
| 569 | 569 |
class NodeObserverProxy : public NodeNotifier::ObserverBase {
|
| 570 | 570 |
public: |
| 571 | 571 |
|
| 572 | 572 |
NodeObserverProxy(Snapshot& _snapshot) |
| 573 | 573 |
: snapshot(_snapshot) {}
|
| 574 | 574 |
|
| 575 | 575 |
using NodeNotifier::ObserverBase::attach; |
| 576 | 576 |
using NodeNotifier::ObserverBase::detach; |
| 577 | 577 |
using NodeNotifier::ObserverBase::attached; |
| 578 | 578 |
|
| 579 | 579 |
protected: |
| ... | ... |
@@ -1286,49 +1286,49 @@ |
| 1286 | 1286 |
/// This function changes the second node of the given edge \c e to \c n. |
| 1287 | 1287 |
/// |
| 1288 | 1288 |
///\note \c EdgeIt iterators referencing the changed edge remain |
| 1289 | 1289 |
///valid, but \c ArcIt iterators referencing the changed edge and |
| 1290 | 1290 |
///all other iterators whose base node is the changed node are also |
| 1291 | 1291 |
///invalidated. |
| 1292 | 1292 |
/// |
| 1293 | 1293 |
///\warning This functionality cannot be used together with the |
| 1294 | 1294 |
///Snapshot feature. |
| 1295 | 1295 |
void changeV(Edge e, Node n) {
|
| 1296 | 1296 |
Parent::changeV(e,n); |
| 1297 | 1297 |
} |
| 1298 | 1298 |
|
| 1299 | 1299 |
/// \brief Contract two nodes. |
| 1300 | 1300 |
/// |
| 1301 | 1301 |
/// This function contracts the given two nodes. |
| 1302 | 1302 |
/// Node \c b is removed, but instead of deleting |
| 1303 | 1303 |
/// its incident edges, they are joined to node \c a. |
| 1304 | 1304 |
/// If the last parameter \c r is \c true (this is the default value), |
| 1305 | 1305 |
/// then the newly created loops are removed. |
| 1306 | 1306 |
/// |
| 1307 | 1307 |
/// \note The moved edges are joined to node \c a using changeU() |
| 1308 | 1308 |
/// or changeV(), thus all edge and arc iterators whose base node is |
| 1309 | 1309 |
/// \c b are invalidated. |
| 1310 |
/// Moreover all iterators referencing node \c b or the removed |
|
| 1310 |
/// Moreover all iterators referencing node \c b or the removed |
|
| 1311 | 1311 |
/// loops are also invalidated. Other iterators remain valid. |
| 1312 | 1312 |
/// |
| 1313 | 1313 |
///\warning This functionality cannot be used together with the |
| 1314 | 1314 |
///Snapshot feature. |
| 1315 | 1315 |
void contract(Node a, Node b, bool r = true) {
|
| 1316 | 1316 |
for(IncEdgeIt e(*this, b); e!=INVALID;) {
|
| 1317 | 1317 |
IncEdgeIt f = e; ++f; |
| 1318 | 1318 |
if (r && runningNode(e) == a) {
|
| 1319 | 1319 |
erase(e); |
| 1320 | 1320 |
} else if (u(e) == b) {
|
| 1321 | 1321 |
changeU(e, a); |
| 1322 | 1322 |
} else {
|
| 1323 | 1323 |
changeV(e, a); |
| 1324 | 1324 |
} |
| 1325 | 1325 |
e = f; |
| 1326 | 1326 |
} |
| 1327 | 1327 |
erase(b); |
| 1328 | 1328 |
} |
| 1329 | 1329 |
|
| 1330 | 1330 |
///Clear the graph. |
| 1331 | 1331 |
|
| 1332 | 1332 |
///This function erases all nodes and arcs from the graph. |
| 1333 | 1333 |
/// |
| 1334 | 1334 |
///\note All iterators of the graph are invalidated, of course. |
| ... | ... |
@@ -1343,49 +1343,49 @@ |
| 1343 | 1343 |
/// be large (e.g. it will contain millions of nodes and/or edges), |
| 1344 | 1344 |
/// then it is worth reserving space for this amount before starting |
| 1345 | 1345 |
/// to build the graph. |
| 1346 | 1346 |
/// \sa reserveEdge() |
| 1347 | 1347 |
void reserveNode(int n) { nodes.reserve(n); };
|
| 1348 | 1348 |
|
| 1349 | 1349 |
/// Reserve memory for edges. |
| 1350 | 1350 |
|
| 1351 | 1351 |
/// Using this function, it is possible to avoid superfluous memory |
| 1352 | 1352 |
/// allocation: if you know that the graph you want to build will |
| 1353 | 1353 |
/// be large (e.g. it will contain millions of nodes and/or edges), |
| 1354 | 1354 |
/// then it is worth reserving space for this amount before starting |
| 1355 | 1355 |
/// to build the graph. |
| 1356 | 1356 |
/// \sa reserveNode() |
| 1357 | 1357 |
void reserveEdge(int m) { arcs.reserve(2 * m); };
|
| 1358 | 1358 |
|
| 1359 | 1359 |
/// \brief Class to make a snapshot of the graph and restore |
| 1360 | 1360 |
/// it later. |
| 1361 | 1361 |
/// |
| 1362 | 1362 |
/// Class to make a snapshot of the graph and restore it later. |
| 1363 | 1363 |
/// |
| 1364 | 1364 |
/// The newly added nodes and edges can be removed |
| 1365 | 1365 |
/// using the restore() function. |
| 1366 | 1366 |
/// |
| 1367 |
/// \note After a state is restored, you cannot restore a later state, |
|
| 1367 |
/// \note After a state is restored, you cannot restore a later state, |
|
| 1368 | 1368 |
/// i.e. you cannot add the removed nodes and edges again using |
| 1369 | 1369 |
/// another Snapshot instance. |
| 1370 | 1370 |
/// |
| 1371 | 1371 |
/// \warning Node and edge deletions and other modifications |
| 1372 | 1372 |
/// (e.g. changing the end-nodes of edges or contracting nodes) |
| 1373 | 1373 |
/// cannot be restored. These events invalidate the snapshot. |
| 1374 | 1374 |
/// However, the edges and nodes that were added to the graph after |
| 1375 | 1375 |
/// making the current snapshot can be removed without invalidating it. |
| 1376 | 1376 |
class Snapshot {
|
| 1377 | 1377 |
protected: |
| 1378 | 1378 |
|
| 1379 | 1379 |
typedef Parent::NodeNotifier NodeNotifier; |
| 1380 | 1380 |
|
| 1381 | 1381 |
class NodeObserverProxy : public NodeNotifier::ObserverBase {
|
| 1382 | 1382 |
public: |
| 1383 | 1383 |
|
| 1384 | 1384 |
NodeObserverProxy(Snapshot& _snapshot) |
| 1385 | 1385 |
: snapshot(_snapshot) {}
|
| 1386 | 1386 |
|
| 1387 | 1387 |
using NodeNotifier::ObserverBase::attach; |
| 1388 | 1388 |
using NodeNotifier::ObserverBase::detach; |
| 1389 | 1389 |
using NodeNotifier::ObserverBase::attached; |
| 1390 | 1390 |
|
| 1391 | 1391 |
protected: |
| 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_LP_H |
| 20 | 20 |
#define LEMON_LP_H |
| 21 | 21 |
|
| 22 | 22 |
#include<lemon/config.h> |
| 23 | 23 |
|
| 24 | 24 |
|
| 25 | 25 |
#ifdef LEMON_HAVE_GLPK |
| 26 | 26 |
#include <lemon/glpk.h> |
| 27 | 27 |
#elif LEMON_HAVE_CPLEX |
| 28 | 28 |
#include <lemon/cplex.h> |
| 29 | 29 |
#elif LEMON_HAVE_SOPLEX |
| ... | ... |
@@ -63,31 +63,31 @@ |
| 63 | 63 |
#define LEMON_DEFAULT_MIP SOLVER |
| 64 | 64 |
///The default MIP solver. |
| 65 | 65 |
|
| 66 | 66 |
///The default MIP solver. |
| 67 | 67 |
///\ingroup lp_group |
| 68 | 68 |
/// |
| 69 | 69 |
///Currently, it is either \c GlpkMip or \c CplexMip |
| 70 | 70 |
typedef GlpkMip Mip; |
| 71 | 71 |
#else |
| 72 | 72 |
#ifdef LEMON_HAVE_GLPK |
| 73 | 73 |
# define LEMON_DEFAULT_LP GLPK |
| 74 | 74 |
typedef GlpkLp Lp; |
| 75 | 75 |
# define LEMON_DEFAULT_MIP GLPK |
| 76 | 76 |
typedef GlpkMip Mip; |
| 77 | 77 |
#elif LEMON_HAVE_CPLEX |
| 78 | 78 |
# define LEMON_DEFAULT_LP CPLEX |
| 79 | 79 |
typedef CplexLp Lp; |
| 80 | 80 |
# define LEMON_DEFAULT_MIP CPLEX |
| 81 | 81 |
typedef CplexMip Mip; |
| 82 | 82 |
#elif LEMON_HAVE_SOPLEX |
| 83 | 83 |
# define DEFAULT_LP SOPLEX |
| 84 | 84 |
typedef SoplexLp Lp; |
| 85 | 85 |
#elif LEMON_HAVE_CLP |
| 86 | 86 |
# define DEFAULT_LP CLP |
| 87 |
typedef ClpLp Lp; |
|
| 87 |
typedef ClpLp Lp; |
|
| 88 | 88 |
#endif |
| 89 | 89 |
#endif |
| 90 | 90 |
|
| 91 | 91 |
} //namespace lemon |
| 92 | 92 |
|
| 93 | 93 |
#endif //LEMON_LP_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 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
///\file |
| 20 | 20 |
///\brief The implementation of the LP solver interface. |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/lp_base.h> |
| 23 | 23 |
namespace lemon {
|
| 24 | 24 |
|
| 25 | 25 |
const LpBase::Value LpBase::INF = |
| 26 | 26 |
std::numeric_limits<LpBase::Value>::infinity(); |
| 27 | 27 |
const LpBase::Value LpBase::NaN = |
| 28 | 28 |
std::numeric_limits<LpBase::Value>::quiet_NaN(); |
| 29 | 29 |
| 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_LP_BASE_H |
| 20 | 20 |
#define LEMON_LP_BASE_H |
| 21 | 21 |
|
| 22 | 22 |
#include<iostream> |
| 23 | 23 |
#include<vector> |
| 24 | 24 |
#include<map> |
| 25 | 25 |
#include<limits> |
| 26 | 26 |
#include<lemon/math.h> |
| 27 | 27 |
|
| 28 | 28 |
#include<lemon/error.h> |
| 29 | 29 |
#include<lemon/assert.h> |
| ... | ... |
@@ -61,236 +61,236 @@ |
| 61 | 61 |
UNSOLVED = 1 |
| 62 | 62 |
}; |
| 63 | 63 |
|
| 64 | 64 |
///Direction of the optimization |
| 65 | 65 |
enum Sense {
|
| 66 | 66 |
/// Minimization |
| 67 | 67 |
MIN, |
| 68 | 68 |
/// Maximization |
| 69 | 69 |
MAX |
| 70 | 70 |
}; |
| 71 | 71 |
|
| 72 | 72 |
///Enum for \c messageLevel() parameter |
| 73 | 73 |
enum MessageLevel {
|
| 74 | 74 |
/// No output (default value). |
| 75 | 75 |
MESSAGE_NOTHING, |
| 76 | 76 |
/// Error messages only. |
| 77 | 77 |
MESSAGE_ERROR, |
| 78 | 78 |
/// Warnings. |
| 79 | 79 |
MESSAGE_WARNING, |
| 80 | 80 |
/// Normal output. |
| 81 | 81 |
MESSAGE_NORMAL, |
| 82 | 82 |
/// Verbose output. |
| 83 | 83 |
MESSAGE_VERBOSE |
| 84 | 84 |
}; |
| 85 |
|
|
| 85 |
|
|
| 86 | 86 |
|
| 87 | 87 |
///The floating point type used by the solver |
| 88 | 88 |
typedef double Value; |
| 89 | 89 |
///The infinity constant |
| 90 | 90 |
static const Value INF; |
| 91 | 91 |
///The not a number constant |
| 92 | 92 |
static const Value NaN; |
| 93 | 93 |
|
| 94 | 94 |
friend class Col; |
| 95 | 95 |
friend class ColIt; |
| 96 | 96 |
friend class Row; |
| 97 | 97 |
friend class RowIt; |
| 98 | 98 |
|
| 99 | 99 |
///Refer to a column of the LP. |
| 100 | 100 |
|
| 101 | 101 |
///This type is used to refer to a column of the LP. |
| 102 | 102 |
/// |
| 103 | 103 |
///Its value remains valid and correct even after the addition or erase of |
| 104 | 104 |
///other columns. |
| 105 | 105 |
/// |
| 106 | 106 |
///\note This class is similar to other Item types in LEMON, like |
| 107 | 107 |
///Node and Arc types in digraph. |
| 108 | 108 |
class Col {
|
| 109 | 109 |
friend class LpBase; |
| 110 | 110 |
protected: |
| 111 | 111 |
int _id; |
| 112 | 112 |
explicit Col(int id) : _id(id) {}
|
| 113 | 113 |
public: |
| 114 | 114 |
typedef Value ExprValue; |
| 115 | 115 |
typedef True LpCol; |
| 116 | 116 |
/// Default constructor |
| 117 |
|
|
| 117 |
|
|
| 118 | 118 |
/// \warning The default constructor sets the Col to an |
| 119 | 119 |
/// undefined value. |
| 120 | 120 |
Col() {}
|
| 121 | 121 |
/// Invalid constructor \& conversion. |
| 122 |
|
|
| 122 |
|
|
| 123 | 123 |
/// This constructor initializes the Col to be invalid. |
| 124 |
/// \sa Invalid for more details. |
|
| 124 |
/// \sa Invalid for more details. |
|
| 125 | 125 |
Col(const Invalid&) : _id(-1) {}
|
| 126 | 126 |
/// Equality operator |
| 127 | 127 |
|
| 128 | 128 |
/// Two \ref Col "Col"s are equal if and only if they point to |
| 129 | 129 |
/// the same LP column or both are invalid. |
| 130 | 130 |
bool operator==(Col c) const {return _id == c._id;}
|
| 131 | 131 |
/// Inequality operator |
| 132 | 132 |
|
| 133 | 133 |
/// \sa operator==(Col c) |
| 134 | 134 |
/// |
| 135 | 135 |
bool operator!=(Col c) const {return _id != c._id;}
|
| 136 | 136 |
/// Artificial ordering operator. |
| 137 | 137 |
|
| 138 | 138 |
/// To allow the use of this object in std::map or similar |
| 139 | 139 |
/// associative container we require this. |
| 140 | 140 |
/// |
| 141 | 141 |
/// \note This operator only have to define some strict ordering of |
| 142 | 142 |
/// the items; this order has nothing to do with the iteration |
| 143 | 143 |
/// ordering of the items. |
| 144 | 144 |
bool operator<(Col c) const {return _id < c._id;}
|
| 145 | 145 |
}; |
| 146 | 146 |
|
| 147 | 147 |
///Iterator for iterate over the columns of an LP problem |
| 148 | 148 |
|
| 149 | 149 |
/// Its usage is quite simple, for example, you can count the number |
| 150 | 150 |
/// of columns in an LP \c lp: |
| 151 | 151 |
///\code |
| 152 | 152 |
/// int count=0; |
| 153 | 153 |
/// for (LpBase::ColIt c(lp); c!=INVALID; ++c) ++count; |
| 154 | 154 |
///\endcode |
| 155 | 155 |
class ColIt : public Col {
|
| 156 | 156 |
const LpBase *_solver; |
| 157 | 157 |
public: |
| 158 | 158 |
/// Default constructor |
| 159 |
|
|
| 159 |
|
|
| 160 | 160 |
/// \warning The default constructor sets the iterator |
| 161 | 161 |
/// to an undefined value. |
| 162 | 162 |
ColIt() {}
|
| 163 | 163 |
/// Sets the iterator to the first Col |
| 164 |
|
|
| 164 |
|
|
| 165 | 165 |
/// Sets the iterator to the first Col. |
| 166 | 166 |
/// |
| 167 | 167 |
ColIt(const LpBase &solver) : _solver(&solver) |
| 168 | 168 |
{
|
| 169 | 169 |
_solver->cols.firstItem(_id); |
| 170 | 170 |
} |
| 171 | 171 |
/// Invalid constructor \& conversion |
| 172 |
|
|
| 172 |
|
|
| 173 | 173 |
/// Initialize the iterator to be invalid. |
| 174 | 174 |
/// \sa Invalid for more details. |
| 175 | 175 |
ColIt(const Invalid&) : Col(INVALID) {}
|
| 176 | 176 |
/// Next column |
| 177 |
|
|
| 177 |
|
|
| 178 | 178 |
/// Assign the iterator to the next column. |
| 179 | 179 |
/// |
| 180 | 180 |
ColIt &operator++() |
| 181 | 181 |
{
|
| 182 | 182 |
_solver->cols.nextItem(_id); |
| 183 | 183 |
return *this; |
| 184 | 184 |
} |
| 185 | 185 |
}; |
| 186 | 186 |
|
| 187 | 187 |
/// \brief Returns the ID of the column. |
| 188 | 188 |
static int id(const Col& col) { return col._id; }
|
| 189 | 189 |
/// \brief Returns the column with the given ID. |
| 190 | 190 |
/// |
| 191 | 191 |
/// \pre The argument should be a valid column ID in the LP problem. |
| 192 | 192 |
static Col colFromId(int id) { return Col(id); }
|
| 193 | 193 |
|
| 194 | 194 |
///Refer to a row of the LP. |
| 195 | 195 |
|
| 196 | 196 |
///This type is used to refer to a row of the LP. |
| 197 | 197 |
/// |
| 198 | 198 |
///Its value remains valid and correct even after the addition or erase of |
| 199 | 199 |
///other rows. |
| 200 | 200 |
/// |
| 201 | 201 |
///\note This class is similar to other Item types in LEMON, like |
| 202 | 202 |
///Node and Arc types in digraph. |
| 203 | 203 |
class Row {
|
| 204 | 204 |
friend class LpBase; |
| 205 | 205 |
protected: |
| 206 | 206 |
int _id; |
| 207 | 207 |
explicit Row(int id) : _id(id) {}
|
| 208 | 208 |
public: |
| 209 | 209 |
typedef Value ExprValue; |
| 210 | 210 |
typedef True LpRow; |
| 211 | 211 |
/// Default constructor |
| 212 |
|
|
| 212 |
|
|
| 213 | 213 |
/// \warning The default constructor sets the Row to an |
| 214 | 214 |
/// undefined value. |
| 215 | 215 |
Row() {}
|
| 216 | 216 |
/// Invalid constructor \& conversion. |
| 217 |
|
|
| 217 |
|
|
| 218 | 218 |
/// This constructor initializes the Row to be invalid. |
| 219 |
/// \sa Invalid for more details. |
|
| 219 |
/// \sa Invalid for more details. |
|
| 220 | 220 |
Row(const Invalid&) : _id(-1) {}
|
| 221 | 221 |
/// Equality operator |
| 222 | 222 |
|
| 223 | 223 |
/// Two \ref Row "Row"s are equal if and only if they point to |
| 224 | 224 |
/// the same LP row or both are invalid. |
| 225 | 225 |
bool operator==(Row r) const {return _id == r._id;}
|
| 226 | 226 |
/// Inequality operator |
| 227 |
|
|
| 227 |
|
|
| 228 | 228 |
/// \sa operator==(Row r) |
| 229 | 229 |
/// |
| 230 | 230 |
bool operator!=(Row r) const {return _id != r._id;}
|
| 231 | 231 |
/// Artificial ordering operator. |
| 232 | 232 |
|
| 233 | 233 |
/// To allow the use of this object in std::map or similar |
| 234 | 234 |
/// associative container we require this. |
| 235 | 235 |
/// |
| 236 | 236 |
/// \note This operator only have to define some strict ordering of |
| 237 | 237 |
/// the items; this order has nothing to do with the iteration |
| 238 | 238 |
/// ordering of the items. |
| 239 | 239 |
bool operator<(Row r) const {return _id < r._id;}
|
| 240 | 240 |
}; |
| 241 | 241 |
|
| 242 | 242 |
///Iterator for iterate over the rows of an LP problem |
| 243 | 243 |
|
| 244 | 244 |
/// Its usage is quite simple, for example, you can count the number |
| 245 | 245 |
/// of rows in an LP \c lp: |
| 246 | 246 |
///\code |
| 247 | 247 |
/// int count=0; |
| 248 | 248 |
/// for (LpBase::RowIt c(lp); c!=INVALID; ++c) ++count; |
| 249 | 249 |
///\endcode |
| 250 | 250 |
class RowIt : public Row {
|
| 251 | 251 |
const LpBase *_solver; |
| 252 | 252 |
public: |
| 253 | 253 |
/// Default constructor |
| 254 |
|
|
| 254 |
|
|
| 255 | 255 |
/// \warning The default constructor sets the iterator |
| 256 | 256 |
/// to an undefined value. |
| 257 | 257 |
RowIt() {}
|
| 258 | 258 |
/// Sets the iterator to the first Row |
| 259 |
|
|
| 259 |
|
|
| 260 | 260 |
/// Sets the iterator to the first Row. |
| 261 | 261 |
/// |
| 262 | 262 |
RowIt(const LpBase &solver) : _solver(&solver) |
| 263 | 263 |
{
|
| 264 | 264 |
_solver->rows.firstItem(_id); |
| 265 | 265 |
} |
| 266 | 266 |
/// Invalid constructor \& conversion |
| 267 |
|
|
| 267 |
|
|
| 268 | 268 |
/// Initialize the iterator to be invalid. |
| 269 | 269 |
/// \sa Invalid for more details. |
| 270 | 270 |
RowIt(const Invalid&) : Row(INVALID) {}
|
| 271 | 271 |
/// Next row |
| 272 |
|
|
| 272 |
|
|
| 273 | 273 |
/// Assign the iterator to the next row. |
| 274 | 274 |
/// |
| 275 | 275 |
RowIt &operator++() |
| 276 | 276 |
{
|
| 277 | 277 |
_solver->rows.nextItem(_id); |
| 278 | 278 |
return *this; |
| 279 | 279 |
} |
| 280 | 280 |
}; |
| 281 | 281 |
|
| 282 | 282 |
/// \brief Returns the ID of the row. |
| 283 | 283 |
static int id(const Row& row) { return row._id; }
|
| 284 | 284 |
/// \brief Returns the row with the given ID. |
| 285 | 285 |
/// |
| 286 | 286 |
/// \pre The argument should be a valid row ID in the LP problem. |
| 287 | 287 |
static Row rowFromId(int id) { return Row(id); }
|
| 288 | 288 |
|
| 289 | 289 |
public: |
| 290 | 290 |
|
| 291 | 291 |
///Linear expression of variables and a constant component |
| 292 | 292 |
|
| 293 | 293 |
///This data structure stores a linear expression of the variables |
| 294 | 294 |
///(\ref Col "Col"s) and also has a constant component. |
| 295 | 295 |
/// |
| 296 | 296 |
///There are several ways to access and modify the contents of this |
| ... | ... |
@@ -326,49 +326,49 @@ |
| 326 | 326 |
///- The constant member can be set and read by dereference |
| 327 | 327 |
/// operator (unary *) |
| 328 | 328 |
/// |
| 329 | 329 |
///\code |
| 330 | 330 |
///*e=12; |
| 331 | 331 |
///double c=*e; |
| 332 | 332 |
///\endcode |
| 333 | 333 |
/// |
| 334 | 334 |
///\sa Constr |
| 335 | 335 |
class Expr {
|
| 336 | 336 |
friend class LpBase; |
| 337 | 337 |
public: |
| 338 | 338 |
/// The key type of the expression |
| 339 | 339 |
typedef LpBase::Col Key; |
| 340 | 340 |
/// The value type of the expression |
| 341 | 341 |
typedef LpBase::Value Value; |
| 342 | 342 |
|
| 343 | 343 |
protected: |
| 344 | 344 |
Value const_comp; |
| 345 | 345 |
std::map<int, Value> comps; |
| 346 | 346 |
|
| 347 | 347 |
public: |
| 348 | 348 |
typedef True SolverExpr; |
| 349 | 349 |
/// Default constructor |
| 350 |
|
|
| 350 |
|
|
| 351 | 351 |
/// Construct an empty expression, the coefficients and |
| 352 | 352 |
/// the constant component are initialized to zero. |
| 353 | 353 |
Expr() : const_comp(0) {}
|
| 354 | 354 |
/// Construct an expression from a column |
| 355 | 355 |
|
| 356 | 356 |
/// Construct an expression, which has a term with \c c variable |
| 357 | 357 |
/// and 1.0 coefficient. |
| 358 | 358 |
Expr(const Col &c) : const_comp(0) {
|
| 359 | 359 |
typedef std::map<int, Value>::value_type pair_type; |
| 360 | 360 |
comps.insert(pair_type(id(c), 1)); |
| 361 | 361 |
} |
| 362 | 362 |
/// Construct an expression from a constant |
| 363 | 363 |
|
| 364 | 364 |
/// Construct an expression, which's constant component is \c v. |
| 365 | 365 |
/// |
| 366 | 366 |
Expr(const Value &v) : const_comp(v) {}
|
| 367 | 367 |
/// Returns the coefficient of the column |
| 368 | 368 |
Value operator[](const Col& c) const {
|
| 369 | 369 |
std::map<int, Value>::const_iterator it=comps.find(id(c)); |
| 370 | 370 |
if (it != comps.end()) {
|
| 371 | 371 |
return it->second; |
| 372 | 372 |
} else {
|
| 373 | 373 |
return 0; |
| 374 | 374 |
} |
| ... | ... |
@@ -427,125 +427,125 @@ |
| 427 | 427 |
for (std::map<int, Value>::const_iterator it=e.comps.begin(); |
| 428 | 428 |
it!=e.comps.end(); ++it) |
| 429 | 429 |
comps[it->first]-=it->second; |
| 430 | 430 |
const_comp-=e.const_comp; |
| 431 | 431 |
return *this; |
| 432 | 432 |
} |
| 433 | 433 |
///Multiply with a constant |
| 434 | 434 |
Expr &operator*=(const Value &v) {
|
| 435 | 435 |
for (std::map<int, Value>::iterator it=comps.begin(); |
| 436 | 436 |
it!=comps.end(); ++it) |
| 437 | 437 |
it->second*=v; |
| 438 | 438 |
const_comp*=v; |
| 439 | 439 |
return *this; |
| 440 | 440 |
} |
| 441 | 441 |
///Division with a constant |
| 442 | 442 |
Expr &operator/=(const Value &c) {
|
| 443 | 443 |
for (std::map<int, Value>::iterator it=comps.begin(); |
| 444 | 444 |
it!=comps.end(); ++it) |
| 445 | 445 |
it->second/=c; |
| 446 | 446 |
const_comp/=c; |
| 447 | 447 |
return *this; |
| 448 | 448 |
} |
| 449 | 449 |
|
| 450 | 450 |
///Iterator over the expression |
| 451 |
|
|
| 452 |
///The iterator iterates over the terms of the expression. |
|
| 453 |
|
|
| 451 |
|
|
| 452 |
///The iterator iterates over the terms of the expression. |
|
| 453 |
/// |
|
| 454 | 454 |
///\code |
| 455 | 455 |
///double s=0; |
| 456 | 456 |
///for(LpBase::Expr::CoeffIt i(e);i!=INVALID;++i) |
| 457 | 457 |
/// s+= *i * primal(i); |
| 458 | 458 |
///\endcode |
| 459 | 459 |
class CoeffIt {
|
| 460 | 460 |
private: |
| 461 | 461 |
|
| 462 | 462 |
std::map<int, Value>::iterator _it, _end; |
| 463 | 463 |
|
| 464 | 464 |
public: |
| 465 | 465 |
|
| 466 | 466 |
/// Sets the iterator to the first term |
| 467 |
|
|
| 467 |
|
|
| 468 | 468 |
/// Sets the iterator to the first term of the expression. |
| 469 | 469 |
/// |
| 470 | 470 |
CoeffIt(Expr& e) |
| 471 | 471 |
: _it(e.comps.begin()), _end(e.comps.end()){}
|
| 472 | 472 |
|
| 473 | 473 |
/// Convert the iterator to the column of the term |
| 474 | 474 |
operator Col() const {
|
| 475 | 475 |
return colFromId(_it->first); |
| 476 | 476 |
} |
| 477 | 477 |
|
| 478 | 478 |
/// Returns the coefficient of the term |
| 479 | 479 |
Value& operator*() { return _it->second; }
|
| 480 | 480 |
|
| 481 | 481 |
/// Returns the coefficient of the term |
| 482 | 482 |
const Value& operator*() const { return _it->second; }
|
| 483 | 483 |
/// Next term |
| 484 |
|
|
| 484 |
|
|
| 485 | 485 |
/// Assign the iterator to the next term. |
| 486 | 486 |
/// |
| 487 | 487 |
CoeffIt& operator++() { ++_it; return *this; }
|
| 488 | 488 |
|
| 489 | 489 |
/// Equality operator |
| 490 | 490 |
bool operator==(Invalid) const { return _it == _end; }
|
| 491 | 491 |
/// Inequality operator |
| 492 | 492 |
bool operator!=(Invalid) const { return _it != _end; }
|
| 493 | 493 |
}; |
| 494 | 494 |
|
| 495 | 495 |
/// Const iterator over the expression |
| 496 |
|
|
| 497 |
///The iterator iterates over the terms of the expression. |
|
| 498 |
|
|
| 496 |
|
|
| 497 |
///The iterator iterates over the terms of the expression. |
|
| 498 |
/// |
|
| 499 | 499 |
///\code |
| 500 | 500 |
///double s=0; |
| 501 | 501 |
///for(LpBase::Expr::ConstCoeffIt i(e);i!=INVALID;++i) |
| 502 | 502 |
/// s+=*i * primal(i); |
| 503 | 503 |
///\endcode |
| 504 | 504 |
class ConstCoeffIt {
|
| 505 | 505 |
private: |
| 506 | 506 |
|
| 507 | 507 |
std::map<int, Value>::const_iterator _it, _end; |
| 508 | 508 |
|
| 509 | 509 |
public: |
| 510 | 510 |
|
| 511 | 511 |
/// Sets the iterator to the first term |
| 512 |
|
|
| 512 |
|
|
| 513 | 513 |
/// Sets the iterator to the first term of the expression. |
| 514 | 514 |
/// |
| 515 | 515 |
ConstCoeffIt(const Expr& e) |
| 516 | 516 |
: _it(e.comps.begin()), _end(e.comps.end()){}
|
| 517 | 517 |
|
| 518 | 518 |
/// Convert the iterator to the column of the term |
| 519 | 519 |
operator Col() const {
|
| 520 | 520 |
return colFromId(_it->first); |
| 521 | 521 |
} |
| 522 | 522 |
|
| 523 | 523 |
/// Returns the coefficient of the term |
| 524 | 524 |
const Value& operator*() const { return _it->second; }
|
| 525 | 525 |
|
| 526 | 526 |
/// Next term |
| 527 |
|
|
| 527 |
|
|
| 528 | 528 |
/// Assign the iterator to the next term. |
| 529 | 529 |
/// |
| 530 | 530 |
ConstCoeffIt& operator++() { ++_it; return *this; }
|
| 531 | 531 |
|
| 532 | 532 |
/// Equality operator |
| 533 | 533 |
bool operator==(Invalid) const { return _it == _end; }
|
| 534 | 534 |
/// Inequality operator |
| 535 | 535 |
bool operator!=(Invalid) const { return _it != _end; }
|
| 536 | 536 |
}; |
| 537 | 537 |
|
| 538 | 538 |
}; |
| 539 | 539 |
|
| 540 | 540 |
///Linear constraint |
| 541 | 541 |
|
| 542 | 542 |
///This data stucture represents a linear constraint in the LP. |
| 543 | 543 |
///Basically it is a linear expression with a lower or an upper bound |
| 544 | 544 |
///(or both). These parts of the constraint can be obtained by the member |
| 545 | 545 |
///functions \ref expr(), \ref lowerBound() and \ref upperBound(), |
| 546 | 546 |
///respectively. |
| 547 | 547 |
///There are two ways to construct a constraint. |
| 548 | 548 |
///- You can set the linear expression and the bounds directly |
| 549 | 549 |
/// by the functions above. |
| 550 | 550 |
///- The operators <tt>\<=</tt>, <tt>==</tt> and <tt>\>=</tt> |
| 551 | 551 |
/// are defined between expressions, or even between constraints whenever |
| ... | ... |
@@ -652,210 +652,210 @@ |
| 652 | 652 |
///are valid \ref DualExpr dual expressions. |
| 653 | 653 |
///The usual assignment operations are also defined. |
| 654 | 654 |
///\code |
| 655 | 655 |
///e=v+w; |
| 656 | 656 |
///e+=2*v-3.12*(v-w/2); |
| 657 | 657 |
///e*=3.4; |
| 658 | 658 |
///e/=5; |
| 659 | 659 |
///\endcode |
| 660 | 660 |
/// |
| 661 | 661 |
///\sa Expr |
| 662 | 662 |
class DualExpr {
|
| 663 | 663 |
friend class LpBase; |
| 664 | 664 |
public: |
| 665 | 665 |
/// The key type of the expression |
| 666 | 666 |
typedef LpBase::Row Key; |
| 667 | 667 |
/// The value type of the expression |
| 668 | 668 |
typedef LpBase::Value Value; |
| 669 | 669 |
|
| 670 | 670 |
protected: |
| 671 | 671 |
std::map<int, Value> comps; |
| 672 | 672 |
|
| 673 | 673 |
public: |
| 674 | 674 |
typedef True SolverExpr; |
| 675 | 675 |
/// Default constructor |
| 676 |
|
|
| 676 |
|
|
| 677 | 677 |
/// Construct an empty expression, the coefficients are |
| 678 | 678 |
/// initialized to zero. |
| 679 | 679 |
DualExpr() {}
|
| 680 | 680 |
/// Construct an expression from a row |
| 681 | 681 |
|
| 682 | 682 |
/// Construct an expression, which has a term with \c r dual |
| 683 | 683 |
/// variable and 1.0 coefficient. |
| 684 | 684 |
DualExpr(const Row &r) {
|
| 685 | 685 |
typedef std::map<int, Value>::value_type pair_type; |
| 686 | 686 |
comps.insert(pair_type(id(r), 1)); |
| 687 | 687 |
} |
| 688 | 688 |
/// Returns the coefficient of the row |
| 689 | 689 |
Value operator[](const Row& r) const {
|
| 690 | 690 |
std::map<int, Value>::const_iterator it = comps.find(id(r)); |
| 691 | 691 |
if (it != comps.end()) {
|
| 692 | 692 |
return it->second; |
| 693 | 693 |
} else {
|
| 694 | 694 |
return 0; |
| 695 | 695 |
} |
| 696 | 696 |
} |
| 697 | 697 |
/// Returns the coefficient of the row |
| 698 | 698 |
Value& operator[](const Row& r) {
|
| 699 | 699 |
return comps[id(r)]; |
| 700 | 700 |
} |
| 701 | 701 |
/// Sets the coefficient of the row |
| 702 | 702 |
void set(const Row &r, const Value &v) {
|
| 703 | 703 |
if (v != 0.0) {
|
| 704 | 704 |
typedef std::map<int, Value>::value_type pair_type; |
| 705 | 705 |
comps.insert(pair_type(id(r), v)); |
| 706 | 706 |
} else {
|
| 707 | 707 |
comps.erase(id(r)); |
| 708 | 708 |
} |
| 709 | 709 |
} |
| 710 | 710 |
/// \brief Removes the coefficients which's absolute value does |
| 711 |
/// not exceed \c epsilon. |
|
| 711 |
/// not exceed \c epsilon. |
|
| 712 | 712 |
void simplify(Value epsilon = 0.0) {
|
| 713 | 713 |
std::map<int, Value>::iterator it=comps.begin(); |
| 714 | 714 |
while (it != comps.end()) {
|
| 715 | 715 |
std::map<int, Value>::iterator jt=it; |
| 716 | 716 |
++jt; |
| 717 | 717 |
if (std::fabs((*it).second) <= epsilon) comps.erase(it); |
| 718 | 718 |
it=jt; |
| 719 | 719 |
} |
| 720 | 720 |
} |
| 721 | 721 |
|
| 722 | 722 |
void simplify(Value epsilon = 0.0) const {
|
| 723 | 723 |
const_cast<DualExpr*>(this)->simplify(epsilon); |
| 724 | 724 |
} |
| 725 | 725 |
|
| 726 | 726 |
///Sets all coefficients to 0. |
| 727 | 727 |
void clear() {
|
| 728 | 728 |
comps.clear(); |
| 729 | 729 |
} |
| 730 | 730 |
///Compound assignment |
| 731 | 731 |
DualExpr &operator+=(const DualExpr &e) {
|
| 732 | 732 |
for (std::map<int, Value>::const_iterator it=e.comps.begin(); |
| 733 | 733 |
it!=e.comps.end(); ++it) |
| 734 | 734 |
comps[it->first]+=it->second; |
| 735 | 735 |
return *this; |
| 736 | 736 |
} |
| 737 | 737 |
///Compound assignment |
| 738 | 738 |
DualExpr &operator-=(const DualExpr &e) {
|
| 739 | 739 |
for (std::map<int, Value>::const_iterator it=e.comps.begin(); |
| 740 | 740 |
it!=e.comps.end(); ++it) |
| 741 | 741 |
comps[it->first]-=it->second; |
| 742 | 742 |
return *this; |
| 743 | 743 |
} |
| 744 | 744 |
///Multiply with a constant |
| 745 | 745 |
DualExpr &operator*=(const Value &v) {
|
| 746 | 746 |
for (std::map<int, Value>::iterator it=comps.begin(); |
| 747 | 747 |
it!=comps.end(); ++it) |
| 748 | 748 |
it->second*=v; |
| 749 | 749 |
return *this; |
| 750 | 750 |
} |
| 751 | 751 |
///Division with a constant |
| 752 | 752 |
DualExpr &operator/=(const Value &v) {
|
| 753 | 753 |
for (std::map<int, Value>::iterator it=comps.begin(); |
| 754 | 754 |
it!=comps.end(); ++it) |
| 755 | 755 |
it->second/=v; |
| 756 | 756 |
return *this; |
| 757 | 757 |
} |
| 758 | 758 |
|
| 759 | 759 |
///Iterator over the expression |
| 760 |
|
|
| 761 |
///The iterator iterates over the terms of the expression. |
|
| 762 |
|
|
| 760 |
|
|
| 761 |
///The iterator iterates over the terms of the expression. |
|
| 762 |
/// |
|
| 763 | 763 |
///\code |
| 764 | 764 |
///double s=0; |
| 765 | 765 |
///for(LpBase::DualExpr::CoeffIt i(e);i!=INVALID;++i) |
| 766 | 766 |
/// s+= *i * dual(i); |
| 767 | 767 |
///\endcode |
| 768 | 768 |
class CoeffIt {
|
| 769 | 769 |
private: |
| 770 | 770 |
|
| 771 | 771 |
std::map<int, Value>::iterator _it, _end; |
| 772 | 772 |
|
| 773 | 773 |
public: |
| 774 | 774 |
|
| 775 | 775 |
/// Sets the iterator to the first term |
| 776 |
|
|
| 776 |
|
|
| 777 | 777 |
/// Sets the iterator to the first term of the expression. |
| 778 | 778 |
/// |
| 779 | 779 |
CoeffIt(DualExpr& e) |
| 780 | 780 |
: _it(e.comps.begin()), _end(e.comps.end()){}
|
| 781 | 781 |
|
| 782 | 782 |
/// Convert the iterator to the row of the term |
| 783 | 783 |
operator Row() const {
|
| 784 | 784 |
return rowFromId(_it->first); |
| 785 | 785 |
} |
| 786 | 786 |
|
| 787 | 787 |
/// Returns the coefficient of the term |
| 788 | 788 |
Value& operator*() { return _it->second; }
|
| 789 | 789 |
|
| 790 | 790 |
/// Returns the coefficient of the term |
| 791 | 791 |
const Value& operator*() const { return _it->second; }
|
| 792 | 792 |
|
| 793 | 793 |
/// Next term |
| 794 |
|
|
| 794 |
|
|
| 795 | 795 |
/// Assign the iterator to the next term. |
| 796 | 796 |
/// |
| 797 | 797 |
CoeffIt& operator++() { ++_it; return *this; }
|
| 798 | 798 |
|
| 799 | 799 |
/// Equality operator |
| 800 | 800 |
bool operator==(Invalid) const { return _it == _end; }
|
| 801 | 801 |
/// Inequality operator |
| 802 | 802 |
bool operator!=(Invalid) const { return _it != _end; }
|
| 803 | 803 |
}; |
| 804 | 804 |
|
| 805 | 805 |
///Iterator over the expression |
| 806 |
|
|
| 807 |
///The iterator iterates over the terms of the expression. |
|
| 808 |
|
|
| 806 |
|
|
| 807 |
///The iterator iterates over the terms of the expression. |
|
| 808 |
/// |
|
| 809 | 809 |
///\code |
| 810 | 810 |
///double s=0; |
| 811 | 811 |
///for(LpBase::DualExpr::ConstCoeffIt i(e);i!=INVALID;++i) |
| 812 | 812 |
/// s+= *i * dual(i); |
| 813 | 813 |
///\endcode |
| 814 | 814 |
class ConstCoeffIt {
|
| 815 | 815 |
private: |
| 816 | 816 |
|
| 817 | 817 |
std::map<int, Value>::const_iterator _it, _end; |
| 818 | 818 |
|
| 819 | 819 |
public: |
| 820 | 820 |
|
| 821 | 821 |
/// Sets the iterator to the first term |
| 822 |
|
|
| 822 |
|
|
| 823 | 823 |
/// Sets the iterator to the first term of the expression. |
| 824 | 824 |
/// |
| 825 | 825 |
ConstCoeffIt(const DualExpr& e) |
| 826 | 826 |
: _it(e.comps.begin()), _end(e.comps.end()){}
|
| 827 | 827 |
|
| 828 | 828 |
/// Convert the iterator to the row of the term |
| 829 | 829 |
operator Row() const {
|
| 830 | 830 |
return rowFromId(_it->first); |
| 831 | 831 |
} |
| 832 | 832 |
|
| 833 | 833 |
/// Returns the coefficient of the term |
| 834 | 834 |
const Value& operator*() const { return _it->second; }
|
| 835 | 835 |
|
| 836 | 836 |
/// Next term |
| 837 |
|
|
| 837 |
|
|
| 838 | 838 |
/// Assign the iterator to the next term. |
| 839 | 839 |
/// |
| 840 | 840 |
ConstCoeffIt& operator++() { ++_it; return *this; }
|
| 841 | 841 |
|
| 842 | 842 |
/// Equality operator |
| 843 | 843 |
bool operator==(Invalid) const { return _it == _end; }
|
| 844 | 844 |
/// Inequality operator |
| 845 | 845 |
bool operator!=(Invalid) const { return _it != _end; }
|
| 846 | 846 |
}; |
| 847 | 847 |
}; |
| 848 | 848 |
|
| 849 | 849 |
|
| 850 | 850 |
protected: |
| 851 | 851 |
|
| 852 | 852 |
class InsertIterator {
|
| 853 | 853 |
private: |
| 854 | 854 |
|
| 855 | 855 |
std::map<int, Value>& _host; |
| 856 | 856 |
const _solver_bits::VarIndex& _index; |
| 857 | 857 |
|
| 858 | 858 |
public: |
| 859 | 859 |
|
| 860 | 860 |
typedef std::output_iterator_tag iterator_category; |
| 861 | 861 |
typedef void difference_type; |
| ... | ... |
@@ -1208,49 +1208,49 @@ |
| 1208 | 1208 |
return e; |
| 1209 | 1209 |
} |
| 1210 | 1210 |
|
| 1211 | 1211 |
///Add a new row (i.e a new constraint) to the LP |
| 1212 | 1212 |
|
| 1213 | 1213 |
///\param l is the lower bound (-\ref INF means no bound) |
| 1214 | 1214 |
///\param e is a linear expression (see \ref Expr) |
| 1215 | 1215 |
///\param u is the upper bound (\ref INF means no bound) |
| 1216 | 1216 |
///\return The created row. |
| 1217 | 1217 |
Row addRow(Value l,const Expr &e, Value u) {
|
| 1218 | 1218 |
Row r; |
| 1219 | 1219 |
e.simplify(); |
| 1220 | 1220 |
r._id = _addRowId(_addRow(l - *e, ExprIterator(e.comps.begin(), cols), |
| 1221 | 1221 |
ExprIterator(e.comps.end(), cols), u - *e)); |
| 1222 | 1222 |
return r; |
| 1223 | 1223 |
} |
| 1224 | 1224 |
|
| 1225 | 1225 |
///Add a new row (i.e a new constraint) to the LP |
| 1226 | 1226 |
|
| 1227 | 1227 |
///\param c is a linear expression (see \ref Constr) |
| 1228 | 1228 |
///\return The created row. |
| 1229 | 1229 |
Row addRow(const Constr &c) {
|
| 1230 | 1230 |
Row r; |
| 1231 | 1231 |
c.expr().simplify(); |
| 1232 |
r._id = _addRowId(_addRow(c.lowerBounded()?c.lowerBound()-*c.expr():-INF, |
|
| 1232 |
r._id = _addRowId(_addRow(c.lowerBounded()?c.lowerBound()-*c.expr():-INF, |
|
| 1233 | 1233 |
ExprIterator(c.expr().comps.begin(), cols), |
| 1234 | 1234 |
ExprIterator(c.expr().comps.end(), cols), |
| 1235 | 1235 |
c.upperBounded()?c.upperBound()-*c.expr():INF)); |
| 1236 | 1236 |
return r; |
| 1237 | 1237 |
} |
| 1238 | 1238 |
///Erase a column (i.e a variable) from the LP |
| 1239 | 1239 |
|
| 1240 | 1240 |
///\param c is the column to be deleted |
| 1241 | 1241 |
void erase(Col c) {
|
| 1242 | 1242 |
_eraseCol(cols(id(c))); |
| 1243 | 1243 |
_eraseColId(cols(id(c))); |
| 1244 | 1244 |
} |
| 1245 | 1245 |
///Erase a row (i.e a constraint) from the LP |
| 1246 | 1246 |
|
| 1247 | 1247 |
///\param r is the row to be deleted |
| 1248 | 1248 |
void erase(Row r) {
|
| 1249 | 1249 |
_eraseRow(rows(id(r))); |
| 1250 | 1250 |
_eraseRowId(rows(id(r))); |
| 1251 | 1251 |
} |
| 1252 | 1252 |
|
| 1253 | 1253 |
/// Get the name of a column |
| 1254 | 1254 |
|
| 1255 | 1255 |
///\param c is the coresponding column |
| 1256 | 1256 |
///\return The name of the colunm |
| ... | ... |
@@ -1796,52 +1796,52 @@ |
| 1796 | 1796 |
/// descendants as a concrete implementation, or the \c Lp |
| 1797 | 1797 |
/// default LP solver. However, if you would like to handle LP |
| 1798 | 1798 |
/// solvers as reference or pointer in a generic way, you can use |
| 1799 | 1799 |
/// this class directly. |
| 1800 | 1800 |
class LpSolver : virtual public LpBase {
|
| 1801 | 1801 |
public: |
| 1802 | 1802 |
|
| 1803 | 1803 |
/// The problem types for primal and dual problems |
| 1804 | 1804 |
enum ProblemType {
|
| 1805 | 1805 |
/// = 0. Feasible solution hasn't been found (but may exist). |
| 1806 | 1806 |
UNDEFINED = 0, |
| 1807 | 1807 |
/// = 1. The problem has no feasible solution. |
| 1808 | 1808 |
INFEASIBLE = 1, |
| 1809 | 1809 |
/// = 2. Feasible solution found. |
| 1810 | 1810 |
FEASIBLE = 2, |
| 1811 | 1811 |
/// = 3. Optimal solution exists and found. |
| 1812 | 1812 |
OPTIMAL = 3, |
| 1813 | 1813 |
/// = 4. The cost function is unbounded. |
| 1814 | 1814 |
UNBOUNDED = 4 |
| 1815 | 1815 |
}; |
| 1816 | 1816 |
|
| 1817 | 1817 |
///The basis status of variables |
| 1818 | 1818 |
enum VarStatus {
|
| 1819 | 1819 |
/// The variable is in the basis |
| 1820 |
BASIC, |
|
| 1820 |
BASIC, |
|
| 1821 | 1821 |
/// The variable is free, but not basic |
| 1822 | 1822 |
FREE, |
| 1823 |
/// The variable has active lower bound |
|
| 1823 |
/// The variable has active lower bound |
|
| 1824 | 1824 |
LOWER, |
| 1825 | 1825 |
/// The variable has active upper bound |
| 1826 | 1826 |
UPPER, |
| 1827 | 1827 |
/// The variable is non-basic and fixed |
| 1828 | 1828 |
FIXED |
| 1829 | 1829 |
}; |
| 1830 | 1830 |
|
| 1831 | 1831 |
protected: |
| 1832 | 1832 |
|
| 1833 | 1833 |
virtual SolveExitStatus _solve() = 0; |
| 1834 | 1834 |
|
| 1835 | 1835 |
virtual Value _getPrimal(int i) const = 0; |
| 1836 | 1836 |
virtual Value _getDual(int i) const = 0; |
| 1837 | 1837 |
|
| 1838 | 1838 |
virtual Value _getPrimalRay(int i) const = 0; |
| 1839 | 1839 |
virtual Value _getDualRay(int i) const = 0; |
| 1840 | 1840 |
|
| 1841 | 1841 |
virtual Value _getPrimalValue() const = 0; |
| 1842 | 1842 |
|
| 1843 | 1843 |
virtual VarStatus _getColStatus(int i) const = 0; |
| 1844 | 1844 |
virtual VarStatus _getRowStatus(int i) const = 0; |
| 1845 | 1845 |
|
| 1846 | 1846 |
virtual ProblemType _getPrimalType() const = 0; |
| 1847 | 1847 |
virtual ProblemType _getDualType() const = 0; |
| ... | ... |
@@ -1878,83 +1878,83 @@ |
| 1878 | 1878 |
/// The type of the dual problem |
| 1879 | 1879 |
ProblemType dualType() const {
|
| 1880 | 1880 |
return _getDualType(); |
| 1881 | 1881 |
} |
| 1882 | 1882 |
|
| 1883 | 1883 |
/// Return the primal value of the column |
| 1884 | 1884 |
|
| 1885 | 1885 |
/// Return the primal value of the column. |
| 1886 | 1886 |
/// \pre The problem is solved. |
| 1887 | 1887 |
Value primal(Col c) const { return _getPrimal(cols(id(c))); }
|
| 1888 | 1888 |
|
| 1889 | 1889 |
/// Return the primal value of the expression |
| 1890 | 1890 |
|
| 1891 | 1891 |
/// Return the primal value of the expression, i.e. the dot |
| 1892 | 1892 |
/// product of the primal solution and the expression. |
| 1893 | 1893 |
/// \pre The problem is solved. |
| 1894 | 1894 |
Value primal(const Expr& e) const {
|
| 1895 | 1895 |
double res = *e; |
| 1896 | 1896 |
for (Expr::ConstCoeffIt c(e); c != INVALID; ++c) {
|
| 1897 | 1897 |
res += *c * primal(c); |
| 1898 | 1898 |
} |
| 1899 | 1899 |
return res; |
| 1900 | 1900 |
} |
| 1901 | 1901 |
/// Returns a component of the primal ray |
| 1902 |
|
|
| 1902 |
|
|
| 1903 | 1903 |
/// The primal ray is solution of the modified primal problem, |
| 1904 | 1904 |
/// where we change each finite bound to 0, and we looking for a |
| 1905 | 1905 |
/// negative objective value in case of minimization, and positive |
| 1906 | 1906 |
/// objective value for maximization. If there is such solution, |
| 1907 | 1907 |
/// that proofs the unsolvability of the dual problem, and if a |
| 1908 | 1908 |
/// feasible primal solution exists, then the unboundness of |
| 1909 | 1909 |
/// primal problem. |
| 1910 | 1910 |
/// |
| 1911 | 1911 |
/// \pre The problem is solved and the dual problem is infeasible. |
| 1912 | 1912 |
/// \note Some solvers does not provide primal ray calculation |
| 1913 | 1913 |
/// functions. |
| 1914 | 1914 |
Value primalRay(Col c) const { return _getPrimalRay(cols(id(c))); }
|
| 1915 | 1915 |
|
| 1916 | 1916 |
/// Return the dual value of the row |
| 1917 | 1917 |
|
| 1918 | 1918 |
/// Return the dual value of the row. |
| 1919 | 1919 |
/// \pre The problem is solved. |
| 1920 | 1920 |
Value dual(Row r) const { return _getDual(rows(id(r))); }
|
| 1921 | 1921 |
|
| 1922 | 1922 |
/// Return the dual value of the dual expression |
| 1923 | 1923 |
|
| 1924 | 1924 |
/// Return the dual value of the dual expression, i.e. the dot |
| 1925 | 1925 |
/// product of the dual solution and the dual expression. |
| 1926 | 1926 |
/// \pre The problem is solved. |
| 1927 | 1927 |
Value dual(const DualExpr& e) const {
|
| 1928 | 1928 |
double res = 0.0; |
| 1929 | 1929 |
for (DualExpr::ConstCoeffIt r(e); r != INVALID; ++r) {
|
| 1930 | 1930 |
res += *r * dual(r); |
| 1931 | 1931 |
} |
| 1932 | 1932 |
return res; |
| 1933 | 1933 |
} |
| 1934 | 1934 |
|
| 1935 | 1935 |
/// Returns a component of the dual ray |
| 1936 |
|
|
| 1936 |
|
|
| 1937 | 1937 |
/// The dual ray is solution of the modified primal problem, where |
| 1938 | 1938 |
/// we change each finite bound to 0 (i.e. the objective function |
| 1939 | 1939 |
/// coefficients in the primal problem), and we looking for a |
| 1940 | 1940 |
/// ositive objective value. If there is such solution, that |
| 1941 | 1941 |
/// proofs the unsolvability of the primal problem, and if a |
| 1942 | 1942 |
/// feasible dual solution exists, then the unboundness of |
| 1943 | 1943 |
/// dual problem. |
| 1944 | 1944 |
/// |
| 1945 | 1945 |
/// \pre The problem is solved and the primal problem is infeasible. |
| 1946 | 1946 |
/// \note Some solvers does not provide dual ray calculation |
| 1947 | 1947 |
/// functions. |
| 1948 | 1948 |
Value dualRay(Row r) const { return _getDualRay(rows(id(r))); }
|
| 1949 | 1949 |
|
| 1950 | 1950 |
/// Return the basis status of the column |
| 1951 | 1951 |
|
| 1952 | 1952 |
/// \see VarStatus |
| 1953 | 1953 |
VarStatus colStatus(Col c) const { return _getColStatus(cols(id(c))); }
|
| 1954 | 1954 |
|
| 1955 | 1955 |
/// Return the basis status of the row |
| 1956 | 1956 |
|
| 1957 | 1957 |
/// \see VarStatus |
| 1958 | 1958 |
VarStatus rowStatus(Row r) const { return _getRowStatus(rows(id(r))); }
|
| 1959 | 1959 |
|
| 1960 | 1960 |
///The value of the objective function |
| ... | ... |
@@ -2054,49 +2054,49 @@ |
| 2054 | 2054 |
/// The type of the MIP problem |
| 2055 | 2055 |
ProblemType type() const {
|
| 2056 | 2056 |
return _getType(); |
| 2057 | 2057 |
} |
| 2058 | 2058 |
|
| 2059 | 2059 |
/// Return the value of the row in the solution |
| 2060 | 2060 |
|
| 2061 | 2061 |
/// Return the value of the row in the solution. |
| 2062 | 2062 |
/// \pre The problem is solved. |
| 2063 | 2063 |
Value sol(Col c) const { return _getSol(cols(id(c))); }
|
| 2064 | 2064 |
|
| 2065 | 2065 |
/// Return the value of the expression in the solution |
| 2066 | 2066 |
|
| 2067 | 2067 |
/// Return the value of the expression in the solution, i.e. the |
| 2068 | 2068 |
/// dot product of the solution and the expression. |
| 2069 | 2069 |
/// \pre The problem is solved. |
| 2070 | 2070 |
Value sol(const Expr& e) const {
|
| 2071 | 2071 |
double res = *e; |
| 2072 | 2072 |
for (Expr::ConstCoeffIt c(e); c != INVALID; ++c) {
|
| 2073 | 2073 |
res += *c * sol(c); |
| 2074 | 2074 |
} |
| 2075 | 2075 |
return res; |
| 2076 | 2076 |
} |
| 2077 | 2077 |
///The value of the objective function |
| 2078 |
|
|
| 2078 |
|
|
| 2079 | 2079 |
///\return |
| 2080 | 2080 |
///- \ref INF or -\ref INF means either infeasibility or unboundedness |
| 2081 | 2081 |
/// of the problem, depending on whether we minimize or maximize. |
| 2082 | 2082 |
///- \ref NaN if no primal solution is found. |
| 2083 | 2083 |
///- The (finite) objective value if an optimal solution is found. |
| 2084 | 2084 |
Value solValue() const { return _getSolValue()+obj_const_comp;}
|
| 2085 | 2085 |
///@} |
| 2086 | 2086 |
|
| 2087 | 2087 |
protected: |
| 2088 | 2088 |
|
| 2089 | 2089 |
virtual SolveExitStatus _solve() = 0; |
| 2090 | 2090 |
virtual ColTypes _getColType(int col) const = 0; |
| 2091 | 2091 |
virtual void _setColType(int col, ColTypes col_type) = 0; |
| 2092 | 2092 |
virtual ProblemType _getType() const = 0; |
| 2093 | 2093 |
virtual Value _getSol(int i) const = 0; |
| 2094 | 2094 |
virtual Value _getSolValue() const = 0; |
| 2095 | 2095 |
|
| 2096 | 2096 |
}; |
| 2097 | 2097 |
|
| 2098 | 2098 |
|
| 2099 | 2099 |
|
| 2100 | 2100 |
} //namespace lemon |
| 2101 | 2101 |
|
| 2102 | 2102 |
#endif //LEMON_LP_BASE_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 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <lemon/lp_skeleton.h> |
| 20 | 20 |
|
| 21 | 21 |
///\file |
| 22 | 22 |
///\brief A skeleton file to implement LP solver interfaces |
| 23 | 23 |
namespace lemon {
|
| 24 | 24 |
|
| 25 | 25 |
int SkeletonSolverBase::_addCol() |
| 26 | 26 |
{
|
| 27 | 27 |
return ++col_num; |
| 28 | 28 |
} |
| 29 | 29 |
| 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_LP_SKELETON_H |
| 20 | 20 |
#define LEMON_LP_SKELETON_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/lp_base.h> |
| 23 | 23 |
|
| 24 | 24 |
///\file |
| 25 | 25 |
///\brief Skeleton file to implement LP/MIP solver interfaces |
| 26 |
/// |
|
| 26 |
/// |
|
| 27 | 27 |
///The classes in this file do nothing, but they can serve as skeletons when |
| 28 | 28 |
///implementing an interface to new solvers. |
| 29 | 29 |
namespace lemon {
|
| 30 | 30 |
|
| 31 | 31 |
///A skeleton class to implement LP/MIP solver base interface |
| 32 |
|
|
| 32 |
|
|
| 33 | 33 |
///This class does nothing, but it can serve as a skeleton when |
| 34 | 34 |
///implementing an interface to new solvers. |
| 35 | 35 |
class SkeletonSolverBase : public virtual LpBase {
|
| 36 | 36 |
int col_num,row_num; |
| 37 | 37 |
|
| 38 | 38 |
protected: |
| 39 | 39 |
|
| 40 | 40 |
SkeletonSolverBase() |
| 41 | 41 |
: col_num(-1), row_num(-1) {}
|
| 42 | 42 |
|
| 43 | 43 |
/// \e |
| 44 | 44 |
virtual int _addCol(); |
| 45 | 45 |
/// \e |
| 46 | 46 |
virtual int _addRow(); |
| 47 | 47 |
/// \e |
| 48 | 48 |
virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u); |
| 49 | 49 |
/// \e |
| 50 | 50 |
virtual void _eraseCol(int i); |
| 51 | 51 |
/// \e |
| 52 | 52 |
virtual void _eraseRow(int i); |
| 53 | 53 |
|
| 54 | 54 |
/// \e |
| 55 | 55 |
virtual void _getColName(int col, std::string& name) const; |
| 56 | 56 |
/// \e |
| 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_MAPS_H |
| 20 | 20 |
#define LEMON_MAPS_H |
| 21 | 21 |
|
| 22 | 22 |
#include <iterator> |
| 23 | 23 |
#include <functional> |
| 24 | 24 |
#include <vector> |
| 25 | 25 |
#include <map> |
| 26 | 26 |
|
| 27 | 27 |
#include <lemon/core.h> |
| 28 | 28 |
|
| 29 | 29 |
///\file |
| ... | ... |
@@ -212,49 +212,49 @@ |
| 212 | 212 |
/// Gives back the given value without any modification. |
| 213 | 213 |
Value operator[](const Key &k) const {
|
| 214 | 214 |
return k; |
| 215 | 215 |
} |
| 216 | 216 |
}; |
| 217 | 217 |
|
| 218 | 218 |
/// Returns an \c IdentityMap class |
| 219 | 219 |
|
| 220 | 220 |
/// This function just returns an \c IdentityMap class. |
| 221 | 221 |
/// \relates IdentityMap |
| 222 | 222 |
template<typename T> |
| 223 | 223 |
inline IdentityMap<T> identityMap() {
|
| 224 | 224 |
return IdentityMap<T>(); |
| 225 | 225 |
} |
| 226 | 226 |
|
| 227 | 227 |
|
| 228 | 228 |
/// \brief Map for storing values for integer keys from the range |
| 229 | 229 |
/// <tt>[0..size-1]</tt>. |
| 230 | 230 |
/// |
| 231 | 231 |
/// This map is essentially a wrapper for \c std::vector. It assigns |
| 232 | 232 |
/// values to integer keys from the range <tt>[0..size-1]</tt>. |
| 233 | 233 |
/// It can be used together with some data structures, e.g. |
| 234 | 234 |
/// heap types and \c UnionFind, when the used items are small |
| 235 | 235 |
/// integers. This map conforms to the \ref concepts::ReferenceMap |
| 236 |
/// "ReferenceMap" concept. |
|
| 236 |
/// "ReferenceMap" concept. |
|
| 237 | 237 |
/// |
| 238 | 238 |
/// The simplest way of using this map is through the rangeMap() |
| 239 | 239 |
/// function. |
| 240 | 240 |
template <typename V> |
| 241 | 241 |
class RangeMap : public MapBase<int, V> {
|
| 242 | 242 |
template <typename V1> |
| 243 | 243 |
friend class RangeMap; |
| 244 | 244 |
private: |
| 245 | 245 |
|
| 246 | 246 |
typedef std::vector<V> Vector; |
| 247 | 247 |
Vector _vector; |
| 248 | 248 |
|
| 249 | 249 |
public: |
| 250 | 250 |
|
| 251 | 251 |
/// Key type |
| 252 | 252 |
typedef int Key; |
| 253 | 253 |
/// Value type |
| 254 | 254 |
typedef V Value; |
| 255 | 255 |
/// Reference type |
| 256 | 256 |
typedef typename Vector::reference Reference; |
| 257 | 257 |
/// Const reference type |
| 258 | 258 |
typedef typename Vector::const_reference ConstReference; |
| 259 | 259 |
|
| 260 | 260 |
typedef True ReferenceMapTag; |
| ... | ... |
@@ -1895,53 +1895,53 @@ |
| 1895 | 1895 |
|
| 1896 | 1896 |
/// \brief Gives back the inverse of the map. |
| 1897 | 1897 |
/// |
| 1898 | 1898 |
/// Gives back the inverse of the IdMap. |
| 1899 | 1899 |
InverseMap inverse() const { return InverseMap(*_graph);}
|
| 1900 | 1900 |
}; |
| 1901 | 1901 |
|
| 1902 | 1902 |
/// \brief Returns an \c IdMap class. |
| 1903 | 1903 |
/// |
| 1904 | 1904 |
/// This function just returns an \c IdMap class. |
| 1905 | 1905 |
/// \relates IdMap |
| 1906 | 1906 |
template <typename K, typename GR> |
| 1907 | 1907 |
inline IdMap<GR, K> idMap(const GR& graph) {
|
| 1908 | 1908 |
return IdMap<GR, K>(graph); |
| 1909 | 1909 |
} |
| 1910 | 1910 |
|
| 1911 | 1911 |
/// \brief General cross reference graph map type. |
| 1912 | 1912 |
|
| 1913 | 1913 |
/// This class provides simple invertable graph maps. |
| 1914 | 1914 |
/// It wraps a standard graph map (\c NodeMap, \c ArcMap or \c EdgeMap) |
| 1915 | 1915 |
/// and if a key is set to a new value, then stores it in the inverse map. |
| 1916 | 1916 |
/// The graph items can be accessed by their values either using |
| 1917 | 1917 |
/// \c InverseMap or \c operator()(), and the values of the map can be |
| 1918 | 1918 |
/// accessed with an STL compatible forward iterator (\c ValueIt). |
| 1919 |
/// |
|
| 1919 |
/// |
|
| 1920 | 1920 |
/// This map is intended to be used when all associated values are |
| 1921 | 1921 |
/// different (the map is actually invertable) or there are only a few |
| 1922 | 1922 |
/// items with the same value. |
| 1923 |
/// Otherwise consider to use \c IterableValueMap, which is more |
|
| 1923 |
/// Otherwise consider to use \c IterableValueMap, which is more |
|
| 1924 | 1924 |
/// suitable and more efficient for such cases. It provides iterators |
| 1925 | 1925 |
/// to traverse the items with the same associated value, but |
| 1926 | 1926 |
/// it does not have \c InverseMap. |
| 1927 | 1927 |
/// |
| 1928 | 1928 |
/// This type is not reference map, so it cannot be modified with |
| 1929 | 1929 |
/// the subscript operator. |
| 1930 | 1930 |
/// |
| 1931 | 1931 |
/// \tparam GR The graph type. |
| 1932 | 1932 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
| 1933 | 1933 |
/// \c GR::Edge). |
| 1934 | 1934 |
/// \tparam V The value type of the map. |
| 1935 | 1935 |
/// |
| 1936 | 1936 |
/// \see IterableValueMap |
| 1937 | 1937 |
template <typename GR, typename K, typename V> |
| 1938 | 1938 |
class CrossRefMap |
| 1939 | 1939 |
: protected ItemSetTraits<GR, K>::template Map<V>::Type {
|
| 1940 | 1940 |
private: |
| 1941 | 1941 |
|
| 1942 | 1942 |
typedef typename ItemSetTraits<GR, K>:: |
| 1943 | 1943 |
template Map<V>::Type Map; |
| 1944 | 1944 |
|
| 1945 | 1945 |
typedef std::multimap<V, K> Container; |
| 1946 | 1946 |
Container _inv_map; |
| 1947 | 1947 |
|
| ... | ... |
@@ -1981,49 +1981,49 @@ |
| 1981 | 1981 |
ValueIt() {}
|
| 1982 | 1982 |
|
| 1983 | 1983 |
/// \e |
| 1984 | 1984 |
ValueIt& operator++() { ++it; return *this; }
|
| 1985 | 1985 |
/// \e |
| 1986 | 1986 |
ValueIt operator++(int) {
|
| 1987 | 1987 |
ValueIt tmp(*this); |
| 1988 | 1988 |
operator++(); |
| 1989 | 1989 |
return tmp; |
| 1990 | 1990 |
} |
| 1991 | 1991 |
|
| 1992 | 1992 |
/// \e |
| 1993 | 1993 |
const Value& operator*() const { return it->first; }
|
| 1994 | 1994 |
/// \e |
| 1995 | 1995 |
const Value* operator->() const { return &(it->first); }
|
| 1996 | 1996 |
|
| 1997 | 1997 |
/// \e |
| 1998 | 1998 |
bool operator==(ValueIt jt) const { return it == jt.it; }
|
| 1999 | 1999 |
/// \e |
| 2000 | 2000 |
bool operator!=(ValueIt jt) const { return it != jt.it; }
|
| 2001 | 2001 |
|
| 2002 | 2002 |
private: |
| 2003 | 2003 |
typename Container::const_iterator it; |
| 2004 | 2004 |
}; |
| 2005 |
|
|
| 2005 |
|
|
| 2006 | 2006 |
/// Alias for \c ValueIt |
| 2007 | 2007 |
typedef ValueIt ValueIterator; |
| 2008 | 2008 |
|
| 2009 | 2009 |
/// \brief Returns an iterator to the first value. |
| 2010 | 2010 |
/// |
| 2011 | 2011 |
/// Returns an STL compatible iterator to the |
| 2012 | 2012 |
/// first value of the map. The values of the |
| 2013 | 2013 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt> |
| 2014 | 2014 |
/// range. |
| 2015 | 2015 |
ValueIt beginValue() const {
|
| 2016 | 2016 |
return ValueIt(_inv_map.begin()); |
| 2017 | 2017 |
} |
| 2018 | 2018 |
|
| 2019 | 2019 |
/// \brief Returns an iterator after the last value. |
| 2020 | 2020 |
/// |
| 2021 | 2021 |
/// Returns an STL compatible iterator after the |
| 2022 | 2022 |
/// last value of the map. The values of the |
| 2023 | 2023 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt> |
| 2024 | 2024 |
/// range. |
| 2025 | 2025 |
ValueIt endValue() const {
|
| 2026 | 2026 |
return ValueIt(_inv_map.end()); |
| 2027 | 2027 |
} |
| 2028 | 2028 |
|
| 2029 | 2029 |
/// \brief Sets the value associated with the given key. |
| ... | ... |
@@ -2040,49 +2040,49 @@ |
| 2040 | 2040 |
} |
| 2041 | 2041 |
} |
| 2042 | 2042 |
_inv_map.insert(std::make_pair(val, key)); |
| 2043 | 2043 |
Map::set(key, val); |
| 2044 | 2044 |
} |
| 2045 | 2045 |
|
| 2046 | 2046 |
/// \brief Returns the value associated with the given key. |
| 2047 | 2047 |
/// |
| 2048 | 2048 |
/// Returns the value associated with the given key. |
| 2049 | 2049 |
typename MapTraits<Map>::ConstReturnValue |
| 2050 | 2050 |
operator[](const Key& key) const {
|
| 2051 | 2051 |
return Map::operator[](key); |
| 2052 | 2052 |
} |
| 2053 | 2053 |
|
| 2054 | 2054 |
/// \brief Gives back an item by its value. |
| 2055 | 2055 |
/// |
| 2056 | 2056 |
/// This function gives back an item that is assigned to |
| 2057 | 2057 |
/// the given value or \c INVALID if no such item exists. |
| 2058 | 2058 |
/// If there are more items with the same associated value, |
| 2059 | 2059 |
/// only one of them is returned. |
| 2060 | 2060 |
Key operator()(const Value& val) const {
|
| 2061 | 2061 |
typename Container::const_iterator it = _inv_map.find(val); |
| 2062 | 2062 |
return it != _inv_map.end() ? it->second : INVALID; |
| 2063 | 2063 |
} |
| 2064 |
|
|
| 2064 |
|
|
| 2065 | 2065 |
/// \brief Returns the number of items with the given value. |
| 2066 | 2066 |
/// |
| 2067 | 2067 |
/// This function returns the number of items with the given value |
| 2068 | 2068 |
/// associated with it. |
| 2069 | 2069 |
int count(const Value &val) const {
|
| 2070 | 2070 |
return _inv_map.count(val); |
| 2071 | 2071 |
} |
| 2072 | 2072 |
|
| 2073 | 2073 |
protected: |
| 2074 | 2074 |
|
| 2075 | 2075 |
/// \brief Erase the key from the map and the inverse map. |
| 2076 | 2076 |
/// |
| 2077 | 2077 |
/// Erase the key from the map and the inverse map. It is called by the |
| 2078 | 2078 |
/// \c AlterationNotifier. |
| 2079 | 2079 |
virtual void erase(const Key& key) {
|
| 2080 | 2080 |
Value val = Map::operator[](key); |
| 2081 | 2081 |
typename Container::iterator it; |
| 2082 | 2082 |
for (it = _inv_map.equal_range(val).first; |
| 2083 | 2083 |
it != _inv_map.equal_range(val).second; ++it) {
|
| 2084 | 2084 |
if (it->second == key) {
|
| 2085 | 2085 |
_inv_map.erase(it); |
| 2086 | 2086 |
break; |
| 2087 | 2087 |
} |
| 2088 | 2088 |
} |
| ... | ... |
@@ -2357,49 +2357,49 @@ |
| 2357 | 2357 |
unsigned int size() const {
|
| 2358 | 2358 |
return _inverted.size(); |
| 2359 | 2359 |
} |
| 2360 | 2360 |
|
| 2361 | 2361 |
private: |
| 2362 | 2362 |
const RangeIdMap& _inverted; |
| 2363 | 2363 |
}; |
| 2364 | 2364 |
|
| 2365 | 2365 |
/// \brief Gives back the inverse of the map. |
| 2366 | 2366 |
/// |
| 2367 | 2367 |
/// Gives back the inverse of the RangeIdMap. |
| 2368 | 2368 |
const InverseMap inverse() const {
|
| 2369 | 2369 |
return InverseMap(*this); |
| 2370 | 2370 |
} |
| 2371 | 2371 |
}; |
| 2372 | 2372 |
|
| 2373 | 2373 |
/// \brief Returns a \c RangeIdMap class. |
| 2374 | 2374 |
/// |
| 2375 | 2375 |
/// This function just returns an \c RangeIdMap class. |
| 2376 | 2376 |
/// \relates RangeIdMap |
| 2377 | 2377 |
template <typename K, typename GR> |
| 2378 | 2378 |
inline RangeIdMap<GR, K> rangeIdMap(const GR& graph) {
|
| 2379 | 2379 |
return RangeIdMap<GR, K>(graph); |
| 2380 | 2380 |
} |
| 2381 |
|
|
| 2381 |
|
|
| 2382 | 2382 |
/// \brief Dynamic iterable \c bool map. |
| 2383 | 2383 |
/// |
| 2384 | 2384 |
/// This class provides a special graph map type which can store a |
| 2385 | 2385 |
/// \c bool value for graph items (\c Node, \c Arc or \c Edge). |
| 2386 | 2386 |
/// For both \c true and \c false values it is possible to iterate on |
| 2387 | 2387 |
/// the keys mapped to the value. |
| 2388 | 2388 |
/// |
| 2389 | 2389 |
/// This type is a reference map, so it can be modified with the |
| 2390 | 2390 |
/// subscript operator. |
| 2391 | 2391 |
/// |
| 2392 | 2392 |
/// \tparam GR The graph type. |
| 2393 | 2393 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
| 2394 | 2394 |
/// \c GR::Edge). |
| 2395 | 2395 |
/// |
| 2396 | 2396 |
/// \see IterableIntMap, IterableValueMap |
| 2397 | 2397 |
/// \see CrossRefMap |
| 2398 | 2398 |
template <typename GR, typename K> |
| 2399 | 2399 |
class IterableBoolMap |
| 2400 | 2400 |
: protected ItemSetTraits<GR, K>::template Map<int>::Type {
|
| 2401 | 2401 |
private: |
| 2402 | 2402 |
typedef GR Graph; |
| 2403 | 2403 |
|
| 2404 | 2404 |
typedef typename ItemSetTraits<GR, K>::ItemIt KeyIt; |
| 2405 | 2405 |
typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Parent; |
| ... | ... |
@@ -2617,49 +2617,49 @@ |
| 2617 | 2617 |
} |
| 2618 | 2618 |
|
| 2619 | 2619 |
private: |
| 2620 | 2620 |
const IterableBoolMap* _map; |
| 2621 | 2621 |
}; |
| 2622 | 2622 |
|
| 2623 | 2623 |
/// \brief Iterator for the keys mapped to a given value. |
| 2624 | 2624 |
/// |
| 2625 | 2625 |
/// Iterator for the keys mapped to a given value. It works |
| 2626 | 2626 |
/// like a graph item iterator, it can be converted to |
| 2627 | 2627 |
/// the key type of the map, incremented with \c ++ operator, and |
| 2628 | 2628 |
/// if the iterator leaves the last valid key, it will be equal to |
| 2629 | 2629 |
/// \c INVALID. |
| 2630 | 2630 |
class ItemIt : public Key {
|
| 2631 | 2631 |
public: |
| 2632 | 2632 |
typedef Key Parent; |
| 2633 | 2633 |
|
| 2634 | 2634 |
/// \brief Creates an iterator with a value. |
| 2635 | 2635 |
/// |
| 2636 | 2636 |
/// Creates an iterator with a value. It iterates on the |
| 2637 | 2637 |
/// keys mapped to the given value. |
| 2638 | 2638 |
/// \param map The IterableBoolMap. |
| 2639 | 2639 |
/// \param value The value. |
| 2640 | 2640 |
ItemIt(const IterableBoolMap& map, bool value) |
| 2641 |
: Parent(value ? |
|
| 2641 |
: Parent(value ? |
|
| 2642 | 2642 |
(map._sep > 0 ? |
| 2643 | 2643 |
map._array[map._sep - 1] : INVALID) : |
| 2644 | 2644 |
(map._sep < int(map._array.size()) ? |
| 2645 | 2645 |
map._array.back() : INVALID)), _map(&map) {}
|
| 2646 | 2646 |
|
| 2647 | 2647 |
/// \brief Invalid constructor \& conversion. |
| 2648 | 2648 |
/// |
| 2649 | 2649 |
/// This constructor initializes the iterator to be invalid. |
| 2650 | 2650 |
/// \sa Invalid for more details. |
| 2651 | 2651 |
ItemIt(Invalid) : Parent(INVALID), _map(0) {}
|
| 2652 | 2652 |
|
| 2653 | 2653 |
/// \brief Increment operator. |
| 2654 | 2654 |
/// |
| 2655 | 2655 |
/// Increment operator. |
| 2656 | 2656 |
ItemIt& operator++() {
|
| 2657 | 2657 |
int pos = _map->position(*this); |
| 2658 | 2658 |
int _sep = pos >= _map->_sep ? _map->_sep : 0; |
| 2659 | 2659 |
Parent::operator=(pos > _sep ? _map->_array[pos - 1] : INVALID); |
| 2660 | 2660 |
return *this; |
| 2661 | 2661 |
} |
| 2662 | 2662 |
|
| 2663 | 2663 |
private: |
| 2664 | 2664 |
const IterableBoolMap* _map; |
| 2665 | 2665 |
}; |
| ... | ... |
@@ -3765,69 +3765,69 @@ |
| 3765 | 3765 |
} |
| 3766 | 3766 |
|
| 3767 | 3767 |
|
| 3768 | 3768 |
/// \brief Copy the values of a graph map to another map. |
| 3769 | 3769 |
/// |
| 3770 | 3770 |
/// This function copies the values of a graph map to another graph map. |
| 3771 | 3771 |
/// \c To::Key must be equal or convertible to \c From::Key and |
| 3772 | 3772 |
/// \c From::Value must be equal or convertible to \c To::Value. |
| 3773 | 3773 |
/// |
| 3774 | 3774 |
/// For example, an edge map of \c int value type can be copied to |
| 3775 | 3775 |
/// an arc map of \c double value type in an undirected graph, but |
| 3776 | 3776 |
/// an arc map cannot be copied to an edge map. |
| 3777 | 3777 |
/// Note that even a \ref ConstMap can be copied to a standard graph map, |
| 3778 | 3778 |
/// but \ref mapFill() can also be used for this purpose. |
| 3779 | 3779 |
/// |
| 3780 | 3780 |
/// \param gr The graph for which the maps are defined. |
| 3781 | 3781 |
/// \param from The map from which the values have to be copied. |
| 3782 | 3782 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
| 3783 | 3783 |
/// \param to The map to which the values have to be copied. |
| 3784 | 3784 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 3785 | 3785 |
template <typename GR, typename From, typename To> |
| 3786 | 3786 |
void mapCopy(const GR& gr, const From& from, To& to) {
|
| 3787 | 3787 |
typedef typename To::Key Item; |
| 3788 | 3788 |
typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt; |
| 3789 |
|
|
| 3789 |
|
|
| 3790 | 3790 |
for (ItemIt it(gr); it != INVALID; ++it) {
|
| 3791 | 3791 |
to.set(it, from[it]); |
| 3792 | 3792 |
} |
| 3793 | 3793 |
} |
| 3794 | 3794 |
|
| 3795 | 3795 |
/// \brief Compare two graph maps. |
| 3796 | 3796 |
/// |
| 3797 |
/// This function compares the values of two graph maps. It returns |
|
| 3797 |
/// This function compares the values of two graph maps. It returns |
|
| 3798 | 3798 |
/// \c true if the maps assign the same value for all items in the graph. |
| 3799 | 3799 |
/// The \c Key type of the maps (\c Node, \c Arc or \c Edge) must be equal |
| 3800 | 3800 |
/// and their \c Value types must be comparable using \c %operator==(). |
| 3801 | 3801 |
/// |
| 3802 | 3802 |
/// \param gr The graph for which the maps are defined. |
| 3803 | 3803 |
/// \param map1 The first map. |
| 3804 | 3804 |
/// \param map2 The second map. |
| 3805 | 3805 |
template <typename GR, typename Map1, typename Map2> |
| 3806 | 3806 |
bool mapCompare(const GR& gr, const Map1& map1, const Map2& map2) {
|
| 3807 | 3807 |
typedef typename Map2::Key Item; |
| 3808 | 3808 |
typedef typename ItemSetTraits<GR, Item>::ItemIt ItemIt; |
| 3809 |
|
|
| 3809 |
|
|
| 3810 | 3810 |
for (ItemIt it(gr); it != INVALID; ++it) {
|
| 3811 | 3811 |
if (!(map1[it] == map2[it])) return false; |
| 3812 | 3812 |
} |
| 3813 | 3813 |
return true; |
| 3814 | 3814 |
} |
| 3815 | 3815 |
|
| 3816 | 3816 |
/// \brief Return an item having minimum value of a graph map. |
| 3817 | 3817 |
/// |
| 3818 | 3818 |
/// This function returns an item (\c Node, \c Arc or \c Edge) having |
| 3819 | 3819 |
/// minimum value of the given graph map. |
| 3820 | 3820 |
/// If the item set is empty, it returns \c INVALID. |
| 3821 | 3821 |
/// |
| 3822 | 3822 |
/// \param gr The graph for which the map is defined. |
| 3823 | 3823 |
/// \param map The graph map. |
| 3824 | 3824 |
template <typename GR, typename Map> |
| 3825 | 3825 |
typename Map::Key mapMin(const GR& gr, const Map& map) {
|
| 3826 | 3826 |
return mapMin(gr, map, std::less<typename Map::Value>()); |
| 3827 | 3827 |
} |
| 3828 | 3828 |
|
| 3829 | 3829 |
/// \brief Return an item having minimum value of a graph map. |
| 3830 | 3830 |
/// |
| 3831 | 3831 |
/// This function returns an item (\c Node, \c Arc or \c Edge) having |
| 3832 | 3832 |
/// minimum value of the given graph map. |
| 3833 | 3833 |
/// If the item set is empty, it returns \c INVALID. |
| 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_MATCHING_H |
| 20 | 20 |
#define LEMON_MATCHING_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
#include <queue> |
| 24 | 24 |
#include <set> |
| 25 | 25 |
#include <limits> |
| 26 | 26 |
|
| 27 | 27 |
#include <lemon/core.h> |
| 28 | 28 |
#include <lemon/unionfind.h> |
| 29 | 29 |
#include <lemon/bin_heap.h> |
| ... | ... |
@@ -1602,49 +1602,49 @@ |
| 1602 | 1602 |
///@{
|
| 1603 | 1603 |
|
| 1604 | 1604 |
/// \brief Initialize the algorithm |
| 1605 | 1605 |
/// |
| 1606 | 1606 |
/// This function initializes the algorithm. |
| 1607 | 1607 |
void init() {
|
| 1608 | 1608 |
createStructures(); |
| 1609 | 1609 |
|
| 1610 | 1610 |
_blossom_node_list.clear(); |
| 1611 | 1611 |
_blossom_potential.clear(); |
| 1612 | 1612 |
|
| 1613 | 1613 |
for (ArcIt e(_graph); e != INVALID; ++e) {
|
| 1614 | 1614 |
(*_node_heap_index)[e] = BinHeap<Value, IntArcMap>::PRE_HEAP; |
| 1615 | 1615 |
} |
| 1616 | 1616 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1617 | 1617 |
(*_delta1_index)[n] = _delta1->PRE_HEAP; |
| 1618 | 1618 |
} |
| 1619 | 1619 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
| 1620 | 1620 |
(*_delta3_index)[e] = _delta3->PRE_HEAP; |
| 1621 | 1621 |
} |
| 1622 | 1622 |
for (int i = 0; i < _blossom_num; ++i) {
|
| 1623 | 1623 |
(*_delta2_index)[i] = _delta2->PRE_HEAP; |
| 1624 | 1624 |
(*_delta4_index)[i] = _delta4->PRE_HEAP; |
| 1625 | 1625 |
} |
| 1626 |
|
|
| 1626 |
|
|
| 1627 | 1627 |
_unmatched = _node_num; |
| 1628 | 1628 |
|
| 1629 | 1629 |
_delta1->clear(); |
| 1630 | 1630 |
_delta2->clear(); |
| 1631 | 1631 |
_delta3->clear(); |
| 1632 | 1632 |
_delta4->clear(); |
| 1633 | 1633 |
_blossom_set->clear(); |
| 1634 | 1634 |
_tree_set->clear(); |
| 1635 | 1635 |
|
| 1636 | 1636 |
int index = 0; |
| 1637 | 1637 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1638 | 1638 |
Value max = 0; |
| 1639 | 1639 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
| 1640 | 1640 |
if (_graph.target(e) == n) continue; |
| 1641 | 1641 |
if ((dualScale * _weight[e]) / 2 > max) {
|
| 1642 | 1642 |
max = (dualScale * _weight[e]) / 2; |
| 1643 | 1643 |
} |
| 1644 | 1644 |
} |
| 1645 | 1645 |
(*_node_index)[n] = index; |
| 1646 | 1646 |
(*_node_data)[index].heap_index.clear(); |
| 1647 | 1647 |
(*_node_data)[index].heap.clear(); |
| 1648 | 1648 |
(*_node_data)[index].pot = max; |
| 1649 | 1649 |
_delta1->push(n, max); |
| 1650 | 1650 |
int blossom = |
| ... | ... |
@@ -1657,49 +1657,49 @@ |
| 1657 | 1657 |
(*_blossom_data)[blossom].next = INVALID; |
| 1658 | 1658 |
(*_blossom_data)[blossom].pot = 0; |
| 1659 | 1659 |
(*_blossom_data)[blossom].offset = 0; |
| 1660 | 1660 |
++index; |
| 1661 | 1661 |
} |
| 1662 | 1662 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
| 1663 | 1663 |
int si = (*_node_index)[_graph.u(e)]; |
| 1664 | 1664 |
int ti = (*_node_index)[_graph.v(e)]; |
| 1665 | 1665 |
if (_graph.u(e) != _graph.v(e)) {
|
| 1666 | 1666 |
_delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot - |
| 1667 | 1667 |
dualScale * _weight[e]) / 2); |
| 1668 | 1668 |
} |
| 1669 | 1669 |
} |
| 1670 | 1670 |
} |
| 1671 | 1671 |
|
| 1672 | 1672 |
/// \brief Initialize the algorithm with fractional matching |
| 1673 | 1673 |
/// |
| 1674 | 1674 |
/// This function initializes the algorithm with a fractional |
| 1675 | 1675 |
/// matching. This initialization is also called jumpstart heuristic. |
| 1676 | 1676 |
void fractionalInit() {
|
| 1677 | 1677 |
createStructures(); |
| 1678 | 1678 |
|
| 1679 | 1679 |
_blossom_node_list.clear(); |
| 1680 | 1680 |
_blossom_potential.clear(); |
| 1681 |
|
|
| 1681 |
|
|
| 1682 | 1682 |
if (_fractional == 0) {
|
| 1683 | 1683 |
_fractional = new FractionalMatching(_graph, _weight, false); |
| 1684 | 1684 |
} |
| 1685 | 1685 |
_fractional->run(); |
| 1686 | 1686 |
|
| 1687 | 1687 |
for (ArcIt e(_graph); e != INVALID; ++e) {
|
| 1688 | 1688 |
(*_node_heap_index)[e] = BinHeap<Value, IntArcMap>::PRE_HEAP; |
| 1689 | 1689 |
} |
| 1690 | 1690 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1691 | 1691 |
(*_delta1_index)[n] = _delta1->PRE_HEAP; |
| 1692 | 1692 |
} |
| 1693 | 1693 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
| 1694 | 1694 |
(*_delta3_index)[e] = _delta3->PRE_HEAP; |
| 1695 | 1695 |
} |
| 1696 | 1696 |
for (int i = 0; i < _blossom_num; ++i) {
|
| 1697 | 1697 |
(*_delta2_index)[i] = _delta2->PRE_HEAP; |
| 1698 | 1698 |
(*_delta4_index)[i] = _delta4->PRE_HEAP; |
| 1699 | 1699 |
} |
| 1700 | 1700 |
|
| 1701 | 1701 |
_unmatched = 0; |
| 1702 | 1702 |
|
| 1703 | 1703 |
_delta1->clear(); |
| 1704 | 1704 |
_delta2->clear(); |
| 1705 | 1705 |
_delta3->clear(); |
| ... | ... |
@@ -1729,59 +1729,59 @@ |
| 1729 | 1729 |
} |
| 1730 | 1730 |
|
| 1731 | 1731 |
typename Graph::template NodeMap<bool> processed(_graph, false); |
| 1732 | 1732 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1733 | 1733 |
if (processed[n]) continue; |
| 1734 | 1734 |
processed[n] = true; |
| 1735 | 1735 |
if (_fractional->matching(n) == INVALID) continue; |
| 1736 | 1736 |
int num = 1; |
| 1737 | 1737 |
Node v = _graph.target(_fractional->matching(n)); |
| 1738 | 1738 |
while (n != v) {
|
| 1739 | 1739 |
processed[v] = true; |
| 1740 | 1740 |
v = _graph.target(_fractional->matching(v)); |
| 1741 | 1741 |
++num; |
| 1742 | 1742 |
} |
| 1743 | 1743 |
|
| 1744 | 1744 |
if (num % 2 == 1) {
|
| 1745 | 1745 |
std::vector<int> subblossoms(num); |
| 1746 | 1746 |
|
| 1747 | 1747 |
subblossoms[--num] = _blossom_set->find(n); |
| 1748 | 1748 |
_delta1->push(n, _fractional->nodeValue(n)); |
| 1749 | 1749 |
v = _graph.target(_fractional->matching(n)); |
| 1750 | 1750 |
while (n != v) {
|
| 1751 | 1751 |
subblossoms[--num] = _blossom_set->find(v); |
| 1752 | 1752 |
_delta1->push(v, _fractional->nodeValue(v)); |
| 1753 |
v = _graph.target(_fractional->matching(v)); |
|
| 1753 |
v = _graph.target(_fractional->matching(v)); |
|
| 1754 | 1754 |
} |
| 1755 |
|
|
| 1756 |
int surface = |
|
| 1755 |
|
|
| 1756 |
int surface = |
|
| 1757 | 1757 |
_blossom_set->join(subblossoms.begin(), subblossoms.end()); |
| 1758 | 1758 |
(*_blossom_data)[surface].status = EVEN; |
| 1759 | 1759 |
(*_blossom_data)[surface].pred = INVALID; |
| 1760 | 1760 |
(*_blossom_data)[surface].next = INVALID; |
| 1761 | 1761 |
(*_blossom_data)[surface].pot = 0; |
| 1762 | 1762 |
(*_blossom_data)[surface].offset = 0; |
| 1763 |
|
|
| 1763 |
|
|
| 1764 | 1764 |
_tree_set->insert(surface); |
| 1765 | 1765 |
++_unmatched; |
| 1766 | 1766 |
} |
| 1767 | 1767 |
} |
| 1768 | 1768 |
|
| 1769 | 1769 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
| 1770 | 1770 |
int si = (*_node_index)[_graph.u(e)]; |
| 1771 | 1771 |
int sb = _blossom_set->find(_graph.u(e)); |
| 1772 | 1772 |
int ti = (*_node_index)[_graph.v(e)]; |
| 1773 | 1773 |
int tb = _blossom_set->find(_graph.v(e)); |
| 1774 | 1774 |
if ((*_blossom_data)[sb].status == EVEN && |
| 1775 | 1775 |
(*_blossom_data)[tb].status == EVEN && sb != tb) {
|
| 1776 | 1776 |
_delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot - |
| 1777 | 1777 |
dualScale * _weight[e]) / 2); |
| 1778 | 1778 |
} |
| 1779 | 1779 |
} |
| 1780 | 1780 |
|
| 1781 | 1781 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1782 | 1782 |
int nb = _blossom_set->find(n); |
| 1783 | 1783 |
if ((*_blossom_data)[nb].status != MATCHED) continue; |
| 1784 | 1784 |
int ni = (*_node_index)[n]; |
| 1785 | 1785 |
|
| 1786 | 1786 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
| 1787 | 1787 |
Node v = _graph.target(e); |
| ... | ... |
@@ -1789,49 +1789,49 @@ |
| 1789 | 1789 |
int vi = (*_node_index)[v]; |
| 1790 | 1790 |
|
| 1791 | 1791 |
Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - |
| 1792 | 1792 |
dualScale * _weight[e]; |
| 1793 | 1793 |
|
| 1794 | 1794 |
if ((*_blossom_data)[vb].status == EVEN) {
|
| 1795 | 1795 |
|
| 1796 | 1796 |
int vt = _tree_set->find(vb); |
| 1797 | 1797 |
|
| 1798 | 1798 |
typename std::map<int, Arc>::iterator it = |
| 1799 | 1799 |
(*_node_data)[ni].heap_index.find(vt); |
| 1800 | 1800 |
|
| 1801 | 1801 |
if (it != (*_node_data)[ni].heap_index.end()) {
|
| 1802 | 1802 |
if ((*_node_data)[ni].heap[it->second] > rw) {
|
| 1803 | 1803 |
(*_node_data)[ni].heap.replace(it->second, e); |
| 1804 | 1804 |
(*_node_data)[ni].heap.decrease(e, rw); |
| 1805 | 1805 |
it->second = e; |
| 1806 | 1806 |
} |
| 1807 | 1807 |
} else {
|
| 1808 | 1808 |
(*_node_data)[ni].heap.push(e, rw); |
| 1809 | 1809 |
(*_node_data)[ni].heap_index.insert(std::make_pair(vt, e)); |
| 1810 | 1810 |
} |
| 1811 | 1811 |
} |
| 1812 | 1812 |
} |
| 1813 |
|
|
| 1813 |
|
|
| 1814 | 1814 |
if (!(*_node_data)[ni].heap.empty()) {
|
| 1815 | 1815 |
_blossom_set->decrease(n, (*_node_data)[ni].heap.prio()); |
| 1816 | 1816 |
_delta2->push(nb, _blossom_set->classPrio(nb)); |
| 1817 | 1817 |
} |
| 1818 | 1818 |
} |
| 1819 | 1819 |
} |
| 1820 | 1820 |
|
| 1821 | 1821 |
/// \brief Start the algorithm |
| 1822 | 1822 |
/// |
| 1823 | 1823 |
/// This function starts the algorithm. |
| 1824 | 1824 |
/// |
| 1825 | 1825 |
/// \pre \ref init() or \ref fractionalInit() must be called |
| 1826 | 1826 |
/// before using this function. |
| 1827 | 1827 |
void start() {
|
| 1828 | 1828 |
enum OpType {
|
| 1829 | 1829 |
D1, D2, D3, D4 |
| 1830 | 1830 |
}; |
| 1831 | 1831 |
|
| 1832 | 1832 |
while (_unmatched > 0) {
|
| 1833 | 1833 |
Value d1 = !_delta1->empty() ? |
| 1834 | 1834 |
_delta1->prio() : std::numeric_limits<Value>::max(); |
| 1835 | 1835 |
|
| 1836 | 1836 |
Value d2 = !_delta2->empty() ? |
| 1837 | 1837 |
_delta2->prio() : std::numeric_limits<Value>::max(); |
| ... | ... |
@@ -2248,49 +2248,49 @@ |
| 2248 | 2248 |
std::map<int, Arc> heap_index; |
| 2249 | 2249 |
|
| 2250 | 2250 |
int tree; |
| 2251 | 2251 |
}; |
| 2252 | 2252 |
|
| 2253 | 2253 |
RangeMap<NodeData>* _node_data; |
| 2254 | 2254 |
|
| 2255 | 2255 |
typedef ExtendFindEnum<IntIntMap> TreeSet; |
| 2256 | 2256 |
|
| 2257 | 2257 |
IntIntMap *_tree_set_index; |
| 2258 | 2258 |
TreeSet *_tree_set; |
| 2259 | 2259 |
|
| 2260 | 2260 |
IntIntMap *_delta2_index; |
| 2261 | 2261 |
BinHeap<Value, IntIntMap> *_delta2; |
| 2262 | 2262 |
|
| 2263 | 2263 |
IntEdgeMap *_delta3_index; |
| 2264 | 2264 |
BinHeap<Value, IntEdgeMap> *_delta3; |
| 2265 | 2265 |
|
| 2266 | 2266 |
IntIntMap *_delta4_index; |
| 2267 | 2267 |
BinHeap<Value, IntIntMap> *_delta4; |
| 2268 | 2268 |
|
| 2269 | 2269 |
Value _delta_sum; |
| 2270 | 2270 |
int _unmatched; |
| 2271 | 2271 |
|
| 2272 |
typedef MaxWeightedPerfectFractionalMatching<Graph, WeightMap> |
|
| 2272 |
typedef MaxWeightedPerfectFractionalMatching<Graph, WeightMap> |
|
| 2273 | 2273 |
FractionalMatching; |
| 2274 | 2274 |
FractionalMatching *_fractional; |
| 2275 | 2275 |
|
| 2276 | 2276 |
void createStructures() {
|
| 2277 | 2277 |
_node_num = countNodes(_graph); |
| 2278 | 2278 |
_blossom_num = _node_num * 3 / 2; |
| 2279 | 2279 |
|
| 2280 | 2280 |
if (!_matching) {
|
| 2281 | 2281 |
_matching = new MatchingMap(_graph); |
| 2282 | 2282 |
} |
| 2283 | 2283 |
|
| 2284 | 2284 |
if (!_node_potential) {
|
| 2285 | 2285 |
_node_potential = new NodePotential(_graph); |
| 2286 | 2286 |
} |
| 2287 | 2287 |
|
| 2288 | 2288 |
if (!_blossom_set) {
|
| 2289 | 2289 |
_blossom_index = new IntNodeMap(_graph); |
| 2290 | 2290 |
_blossom_set = new BlossomSet(*_blossom_index); |
| 2291 | 2291 |
_blossom_data = new RangeMap<BlossomData>(_blossom_num); |
| 2292 | 2292 |
} else if (_blossom_data->size() != _blossom_num) {
|
| 2293 | 2293 |
delete _blossom_data; |
| 2294 | 2294 |
_blossom_data = new RangeMap<BlossomData>(_blossom_num); |
| 2295 | 2295 |
} |
| 2296 | 2296 |
|
| ... | ... |
@@ -3074,49 +3074,49 @@ |
| 3074 | 3074 |
(*_blossom_data)[blossom].next = INVALID; |
| 3075 | 3075 |
(*_blossom_data)[blossom].pot = 0; |
| 3076 | 3076 |
(*_blossom_data)[blossom].offset = 0; |
| 3077 | 3077 |
++index; |
| 3078 | 3078 |
} |
| 3079 | 3079 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
| 3080 | 3080 |
int si = (*_node_index)[_graph.u(e)]; |
| 3081 | 3081 |
int ti = (*_node_index)[_graph.v(e)]; |
| 3082 | 3082 |
if (_graph.u(e) != _graph.v(e)) {
|
| 3083 | 3083 |
_delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot - |
| 3084 | 3084 |
dualScale * _weight[e]) / 2); |
| 3085 | 3085 |
} |
| 3086 | 3086 |
} |
| 3087 | 3087 |
} |
| 3088 | 3088 |
|
| 3089 | 3089 |
/// \brief Initialize the algorithm with fractional matching |
| 3090 | 3090 |
/// |
| 3091 | 3091 |
/// This function initializes the algorithm with a fractional |
| 3092 | 3092 |
/// matching. This initialization is also called jumpstart heuristic. |
| 3093 | 3093 |
void fractionalInit() {
|
| 3094 | 3094 |
createStructures(); |
| 3095 | 3095 |
|
| 3096 | 3096 |
_blossom_node_list.clear(); |
| 3097 | 3097 |
_blossom_potential.clear(); |
| 3098 |
|
|
| 3098 |
|
|
| 3099 | 3099 |
if (_fractional == 0) {
|
| 3100 | 3100 |
_fractional = new FractionalMatching(_graph, _weight, false); |
| 3101 | 3101 |
} |
| 3102 | 3102 |
if (!_fractional->run()) {
|
| 3103 | 3103 |
_unmatched = -1; |
| 3104 | 3104 |
return; |
| 3105 | 3105 |
} |
| 3106 | 3106 |
|
| 3107 | 3107 |
for (ArcIt e(_graph); e != INVALID; ++e) {
|
| 3108 | 3108 |
(*_node_heap_index)[e] = BinHeap<Value, IntArcMap>::PRE_HEAP; |
| 3109 | 3109 |
} |
| 3110 | 3110 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
| 3111 | 3111 |
(*_delta3_index)[e] = _delta3->PRE_HEAP; |
| 3112 | 3112 |
} |
| 3113 | 3113 |
for (int i = 0; i < _blossom_num; ++i) {
|
| 3114 | 3114 |
(*_delta2_index)[i] = _delta2->PRE_HEAP; |
| 3115 | 3115 |
(*_delta4_index)[i] = _delta4->PRE_HEAP; |
| 3116 | 3116 |
} |
| 3117 | 3117 |
|
| 3118 | 3118 |
_unmatched = 0; |
| 3119 | 3119 |
|
| 3120 | 3120 |
_delta2->clear(); |
| 3121 | 3121 |
_delta3->clear(); |
| 3122 | 3122 |
_delta4->clear(); |
| ... | ... |
@@ -3140,59 +3140,59 @@ |
| 3140 | 3140 |
(*_blossom_data)[blossom].offset = 0; |
| 3141 | 3141 |
++index; |
| 3142 | 3142 |
} |
| 3143 | 3143 |
|
| 3144 | 3144 |
typename Graph::template NodeMap<bool> processed(_graph, false); |
| 3145 | 3145 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 3146 | 3146 |
if (processed[n]) continue; |
| 3147 | 3147 |
processed[n] = true; |
| 3148 | 3148 |
if (_fractional->matching(n) == INVALID) continue; |
| 3149 | 3149 |
int num = 1; |
| 3150 | 3150 |
Node v = _graph.target(_fractional->matching(n)); |
| 3151 | 3151 |
while (n != v) {
|
| 3152 | 3152 |
processed[v] = true; |
| 3153 | 3153 |
v = _graph.target(_fractional->matching(v)); |
| 3154 | 3154 |
++num; |
| 3155 | 3155 |
} |
| 3156 | 3156 |
|
| 3157 | 3157 |
if (num % 2 == 1) {
|
| 3158 | 3158 |
std::vector<int> subblossoms(num); |
| 3159 | 3159 |
|
| 3160 | 3160 |
subblossoms[--num] = _blossom_set->find(n); |
| 3161 | 3161 |
v = _graph.target(_fractional->matching(n)); |
| 3162 | 3162 |
while (n != v) {
|
| 3163 | 3163 |
subblossoms[--num] = _blossom_set->find(v); |
| 3164 |
v = _graph.target(_fractional->matching(v)); |
|
| 3164 |
v = _graph.target(_fractional->matching(v)); |
|
| 3165 | 3165 |
} |
| 3166 |
|
|
| 3167 |
int surface = |
|
| 3166 |
|
|
| 3167 |
int surface = |
|
| 3168 | 3168 |
_blossom_set->join(subblossoms.begin(), subblossoms.end()); |
| 3169 | 3169 |
(*_blossom_data)[surface].status = EVEN; |
| 3170 | 3170 |
(*_blossom_data)[surface].pred = INVALID; |
| 3171 | 3171 |
(*_blossom_data)[surface].next = INVALID; |
| 3172 | 3172 |
(*_blossom_data)[surface].pot = 0; |
| 3173 | 3173 |
(*_blossom_data)[surface].offset = 0; |
| 3174 |
|
|
| 3174 |
|
|
| 3175 | 3175 |
_tree_set->insert(surface); |
| 3176 | 3176 |
++_unmatched; |
| 3177 | 3177 |
} |
| 3178 | 3178 |
} |
| 3179 | 3179 |
|
| 3180 | 3180 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
| 3181 | 3181 |
int si = (*_node_index)[_graph.u(e)]; |
| 3182 | 3182 |
int sb = _blossom_set->find(_graph.u(e)); |
| 3183 | 3183 |
int ti = (*_node_index)[_graph.v(e)]; |
| 3184 | 3184 |
int tb = _blossom_set->find(_graph.v(e)); |
| 3185 | 3185 |
if ((*_blossom_data)[sb].status == EVEN && |
| 3186 | 3186 |
(*_blossom_data)[tb].status == EVEN && sb != tb) {
|
| 3187 | 3187 |
_delta3->push(e, ((*_node_data)[si].pot + (*_node_data)[ti].pot - |
| 3188 | 3188 |
dualScale * _weight[e]) / 2); |
| 3189 | 3189 |
} |
| 3190 | 3190 |
} |
| 3191 | 3191 |
|
| 3192 | 3192 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 3193 | 3193 |
int nb = _blossom_set->find(n); |
| 3194 | 3194 |
if ((*_blossom_data)[nb].status != MATCHED) continue; |
| 3195 | 3195 |
int ni = (*_node_index)[n]; |
| 3196 | 3196 |
|
| 3197 | 3197 |
for (OutArcIt e(_graph, n); e != INVALID; ++e) {
|
| 3198 | 3198 |
Node v = _graph.target(e); |
| ... | ... |
@@ -3200,49 +3200,49 @@ |
| 3200 | 3200 |
int vi = (*_node_index)[v]; |
| 3201 | 3201 |
|
| 3202 | 3202 |
Value rw = (*_node_data)[ni].pot + (*_node_data)[vi].pot - |
| 3203 | 3203 |
dualScale * _weight[e]; |
| 3204 | 3204 |
|
| 3205 | 3205 |
if ((*_blossom_data)[vb].status == EVEN) {
|
| 3206 | 3206 |
|
| 3207 | 3207 |
int vt = _tree_set->find(vb); |
| 3208 | 3208 |
|
| 3209 | 3209 |
typename std::map<int, Arc>::iterator it = |
| 3210 | 3210 |
(*_node_data)[ni].heap_index.find(vt); |
| 3211 | 3211 |
|
| 3212 | 3212 |
if (it != (*_node_data)[ni].heap_index.end()) {
|
| 3213 | 3213 |
if ((*_node_data)[ni].heap[it->second] > rw) {
|
| 3214 | 3214 |
(*_node_data)[ni].heap.replace(it->second, e); |
| 3215 | 3215 |
(*_node_data)[ni].heap.decrease(e, rw); |
| 3216 | 3216 |
it->second = e; |
| 3217 | 3217 |
} |
| 3218 | 3218 |
} else {
|
| 3219 | 3219 |
(*_node_data)[ni].heap.push(e, rw); |
| 3220 | 3220 |
(*_node_data)[ni].heap_index.insert(std::make_pair(vt, e)); |
| 3221 | 3221 |
} |
| 3222 | 3222 |
} |
| 3223 | 3223 |
} |
| 3224 |
|
|
| 3224 |
|
|
| 3225 | 3225 |
if (!(*_node_data)[ni].heap.empty()) {
|
| 3226 | 3226 |
_blossom_set->decrease(n, (*_node_data)[ni].heap.prio()); |
| 3227 | 3227 |
_delta2->push(nb, _blossom_set->classPrio(nb)); |
| 3228 | 3228 |
} |
| 3229 | 3229 |
} |
| 3230 | 3230 |
} |
| 3231 | 3231 |
|
| 3232 | 3232 |
/// \brief Start the algorithm |
| 3233 | 3233 |
/// |
| 3234 | 3234 |
/// This function starts the algorithm. |
| 3235 | 3235 |
/// |
| 3236 | 3236 |
/// \pre \ref init() or \ref fractionalInit() must be called before |
| 3237 | 3237 |
/// using this function. |
| 3238 | 3238 |
bool start() {
|
| 3239 | 3239 |
enum OpType {
|
| 3240 | 3240 |
D2, D3, D4 |
| 3241 | 3241 |
}; |
| 3242 | 3242 |
|
| 3243 | 3243 |
if (_unmatched == -1) return false; |
| 3244 | 3244 |
|
| 3245 | 3245 |
while (_unmatched > 0) {
|
| 3246 | 3246 |
Value d2 = !_delta2->empty() ? |
| 3247 | 3247 |
_delta2->prio() : std::numeric_limits<Value>::max(); |
| 3248 | 3248 |
| 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_MATH_H |
| 20 | 20 |
#define LEMON_MATH_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup misc |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief Some extensions to the standard \c cmath library. |
| 25 | 25 |
/// |
| 26 | 26 |
///Some extensions to the standard \c cmath library. |
| 27 | 27 |
/// |
| 28 | 28 |
///This file includes the standard math library (cmath). |
| 29 | 29 |
|
| ... | ... |
@@ -35,38 +35,38 @@ |
| 35 | 35 |
/// @{
|
| 36 | 36 |
|
| 37 | 37 |
/// The Euler constant |
| 38 | 38 |
const long double E = 2.7182818284590452353602874713526625L; |
| 39 | 39 |
/// log_2(e) |
| 40 | 40 |
const long double LOG2E = 1.4426950408889634073599246810018921L; |
| 41 | 41 |
/// log_10(e) |
| 42 | 42 |
const long double LOG10E = 0.4342944819032518276511289189166051L; |
| 43 | 43 |
/// ln(2) |
| 44 | 44 |
const long double LN2 = 0.6931471805599453094172321214581766L; |
| 45 | 45 |
/// ln(10) |
| 46 | 46 |
const long double LN10 = 2.3025850929940456840179914546843642L; |
| 47 | 47 |
/// pi |
| 48 | 48 |
const long double PI = 3.1415926535897932384626433832795029L; |
| 49 | 49 |
/// pi/2 |
| 50 | 50 |
const long double PI_2 = 1.5707963267948966192313216916397514L; |
| 51 | 51 |
/// pi/4 |
| 52 | 52 |
const long double PI_4 = 0.7853981633974483096156608458198757L; |
| 53 | 53 |
/// sqrt(2) |
| 54 | 54 |
const long double SQRT2 = 1.4142135623730950488016887242096981L; |
| 55 | 55 |
/// 1/sqrt(2) |
| 56 | 56 |
const long double SQRT1_2 = 0.7071067811865475244008443621048490L; |
| 57 | 57 |
|
| 58 | 58 |
///Check whether the parameter is NaN or not |
| 59 |
|
|
| 59 |
|
|
| 60 | 60 |
///This function checks whether the parameter is NaN or not. |
| 61 | 61 |
///Is should be equivalent with std::isnan(), but it is not |
| 62 | 62 |
///provided by all compilers. |
| 63 | 63 |
inline bool isNaN(double v) |
| 64 | 64 |
{
|
| 65 | 65 |
return v!=v; |
| 66 | 66 |
} |
| 67 | 67 |
|
| 68 | 68 |
/// @} |
| 69 | 69 |
|
| 70 | 70 |
} //namespace lemon |
| 71 | 71 |
|
| 72 | 72 |
#endif //LEMON_TOLERANCE_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 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_MIN_COST_ARBORESCENCE_H |
| 20 | 20 |
#define LEMON_MIN_COST_ARBORESCENCE_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup spantree |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief Minimum Cost Arborescence algorithm. |
| 25 | 25 |
|
| 26 | 26 |
#include <vector> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/list_graph.h> |
| 29 | 29 |
#include <lemon/bin_heap.h> |
| ... | ... |
@@ -107,50 +107,50 @@ |
| 107 | 107 |
/// the optimality of the solution can be checked. |
| 108 | 108 |
/// |
| 109 | 109 |
/// \param GR The digraph type the algorithm runs on. |
| 110 | 110 |
/// \param CM A read-only arc map storing the costs of the |
| 111 | 111 |
/// arcs. It is read once for each arc, so the map may involve in |
| 112 | 112 |
/// relatively time consuming process to compute the arc costs if |
| 113 | 113 |
/// it is necessary. The default map type is \ref |
| 114 | 114 |
/// concepts::Digraph::ArcMap "Digraph::ArcMap<int>". |
| 115 | 115 |
/// \tparam TR The traits class that defines various types used by the |
| 116 | 116 |
/// algorithm. By default, it is \ref MinCostArborescenceDefaultTraits |
| 117 | 117 |
/// "MinCostArborescenceDefaultTraits<GR, CM>". |
| 118 | 118 |
/// In most cases, this parameter should not be set directly, |
| 119 | 119 |
/// consider to use the named template parameters instead. |
| 120 | 120 |
#ifndef DOXYGEN |
| 121 | 121 |
template <typename GR, |
| 122 | 122 |
typename CM = typename GR::template ArcMap<int>, |
| 123 | 123 |
typename TR = |
| 124 | 124 |
MinCostArborescenceDefaultTraits<GR, CM> > |
| 125 | 125 |
#else |
| 126 | 126 |
template <typename GR, typename CM, typename TR> |
| 127 | 127 |
#endif |
| 128 | 128 |
class MinCostArborescence {
|
| 129 | 129 |
public: |
| 130 | 130 |
|
| 131 |
/// \brief The \ref MinCostArborescenceDefaultTraits "traits class" |
|
| 132 |
/// of the algorithm. |
|
| 131 |
/// \brief The \ref MinCostArborescenceDefaultTraits "traits class" |
|
| 132 |
/// of the algorithm. |
|
| 133 | 133 |
typedef TR Traits; |
| 134 | 134 |
/// The type of the underlying digraph. |
| 135 | 135 |
typedef typename Traits::Digraph Digraph; |
| 136 | 136 |
/// The type of the map that stores the arc costs. |
| 137 | 137 |
typedef typename Traits::CostMap CostMap; |
| 138 | 138 |
///The type of the costs of the arcs. |
| 139 | 139 |
typedef typename Traits::Value Value; |
| 140 | 140 |
///The type of the predecessor map. |
| 141 | 141 |
typedef typename Traits::PredMap PredMap; |
| 142 | 142 |
///The type of the map that stores which arcs are in the arborescence. |
| 143 | 143 |
typedef typename Traits::ArborescenceMap ArborescenceMap; |
| 144 | 144 |
|
| 145 | 145 |
typedef MinCostArborescence Create; |
| 146 | 146 |
|
| 147 | 147 |
private: |
| 148 | 148 |
|
| 149 | 149 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 150 | 150 |
|
| 151 | 151 |
struct CostArc {
|
| 152 | 152 |
|
| 153 | 153 |
Arc arc; |
| 154 | 154 |
Value value; |
| 155 | 155 |
|
| 156 | 156 |
CostArc() {}
|
| ... | ... |
@@ -415,49 +415,49 @@ |
| 415 | 415 |
/// and its value type must be \c bool (or convertible). |
| 416 | 416 |
/// Initially it will be set to \c false on each arc, |
| 417 | 417 |
/// then it will be set on each arborescence arc once. |
| 418 | 418 |
template <class T> |
| 419 | 419 |
struct SetArborescenceMap |
| 420 | 420 |
: public MinCostArborescence<Digraph, CostMap, |
| 421 | 421 |
SetArborescenceMapTraits<T> > {
|
| 422 | 422 |
}; |
| 423 | 423 |
|
| 424 | 424 |
template <class T> |
| 425 | 425 |
struct SetPredMapTraits : public Traits {
|
| 426 | 426 |
typedef T PredMap; |
| 427 | 427 |
static PredMap *createPredMap(const Digraph &) |
| 428 | 428 |
{
|
| 429 | 429 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
| 430 | 430 |
return 0; // ignore warnings |
| 431 | 431 |
} |
| 432 | 432 |
}; |
| 433 | 433 |
|
| 434 | 434 |
/// \brief \ref named-templ-param "Named parameter" for |
| 435 | 435 |
/// setting \c PredMap type |
| 436 | 436 |
/// |
| 437 | 437 |
/// \ref named-templ-param "Named parameter" for setting |
| 438 | 438 |
/// \c PredMap type. |
| 439 |
/// It must meet the \ref concepts::WriteMap "WriteMap" concept, |
|
| 439 |
/// It must meet the \ref concepts::WriteMap "WriteMap" concept, |
|
| 440 | 440 |
/// and its value type must be the \c Arc type of the digraph. |
| 441 | 441 |
template <class T> |
| 442 | 442 |
struct SetPredMap |
| 443 | 443 |
: public MinCostArborescence<Digraph, CostMap, SetPredMapTraits<T> > {
|
| 444 | 444 |
}; |
| 445 | 445 |
|
| 446 | 446 |
/// @} |
| 447 | 447 |
|
| 448 | 448 |
/// \brief Constructor. |
| 449 | 449 |
/// |
| 450 | 450 |
/// \param digraph The digraph the algorithm will run on. |
| 451 | 451 |
/// \param cost The cost map used by the algorithm. |
| 452 | 452 |
MinCostArborescence(const Digraph& digraph, const CostMap& cost) |
| 453 | 453 |
: _digraph(&digraph), _cost(&cost), _pred(0), local_pred(false), |
| 454 | 454 |
_arborescence(0), local_arborescence(false), |
| 455 | 455 |
_arc_order(0), _node_order(0), _cost_arcs(0), |
| 456 | 456 |
_heap_cross_ref(0), _heap(0) {}
|
| 457 | 457 |
|
| 458 | 458 |
/// \brief Destructor. |
| 459 | 459 |
~MinCostArborescence() {
|
| 460 | 460 |
destroyStructures(); |
| 461 | 461 |
} |
| 462 | 462 |
|
| 463 | 463 |
/// \brief Sets the arborescence map. |
| 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_NETWORK_SIMPLEX_H |
| 20 | 20 |
#define LEMON_NETWORK_SIMPLEX_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup min_cost_flow_algs |
| 23 | 23 |
/// |
| 24 | 24 |
/// \file |
| 25 | 25 |
/// \brief Network Simplex algorithm for finding a minimum cost flow. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <algorithm> |
| ... | ... |
@@ -76,110 +76,110 @@ |
| 76 | 76 |
|
| 77 | 77 |
/// The type of the flow amounts, capacity bounds and supply values |
| 78 | 78 |
typedef V Value; |
| 79 | 79 |
/// The type of the arc costs |
| 80 | 80 |
typedef C Cost; |
| 81 | 81 |
|
| 82 | 82 |
public: |
| 83 | 83 |
|
| 84 | 84 |
/// \brief Problem type constants for the \c run() function. |
| 85 | 85 |
/// |
| 86 | 86 |
/// Enum type containing the problem type constants that can be |
| 87 | 87 |
/// returned by the \ref run() function of the algorithm. |
| 88 | 88 |
enum ProblemType {
|
| 89 | 89 |
/// The problem has no feasible solution (flow). |
| 90 | 90 |
INFEASIBLE, |
| 91 | 91 |
/// The problem has optimal solution (i.e. it is feasible and |
| 92 | 92 |
/// bounded), and the algorithm has found optimal flow and node |
| 93 | 93 |
/// potentials (primal and dual solutions). |
| 94 | 94 |
OPTIMAL, |
| 95 | 95 |
/// The objective function of the problem is unbounded, i.e. |
| 96 | 96 |
/// there is a directed cycle having negative total cost and |
| 97 | 97 |
/// infinite upper bound. |
| 98 | 98 |
UNBOUNDED |
| 99 | 99 |
}; |
| 100 |
|
|
| 100 |
|
|
| 101 | 101 |
/// \brief Constants for selecting the type of the supply constraints. |
| 102 | 102 |
/// |
| 103 | 103 |
/// Enum type containing constants for selecting the supply type, |
| 104 | 104 |
/// i.e. the direction of the inequalities in the supply/demand |
| 105 | 105 |
/// constraints of the \ref min_cost_flow "minimum cost flow problem". |
| 106 | 106 |
/// |
| 107 | 107 |
/// The default supply type is \c GEQ, the \c LEQ type can be |
| 108 | 108 |
/// selected using \ref supplyType(). |
| 109 | 109 |
/// The equality form is a special case of both supply types. |
| 110 | 110 |
enum SupplyType {
|
| 111 | 111 |
/// This option means that there are <em>"greater or equal"</em> |
| 112 | 112 |
/// supply/demand constraints in the definition of the problem. |
| 113 | 113 |
GEQ, |
| 114 | 114 |
/// This option means that there are <em>"less or equal"</em> |
| 115 | 115 |
/// supply/demand constraints in the definition of the problem. |
| 116 | 116 |
LEQ |
| 117 | 117 |
}; |
| 118 |
|
|
| 118 |
|
|
| 119 | 119 |
/// \brief Constants for selecting the pivot rule. |
| 120 | 120 |
/// |
| 121 | 121 |
/// Enum type containing constants for selecting the pivot rule for |
| 122 | 122 |
/// the \ref run() function. |
| 123 | 123 |
/// |
| 124 | 124 |
/// \ref NetworkSimplex provides five different pivot rule |
| 125 | 125 |
/// implementations that significantly affect the running time |
| 126 | 126 |
/// of the algorithm. |
| 127 | 127 |
/// By default, \ref BLOCK_SEARCH "Block Search" is used, which |
| 128 | 128 |
/// proved to be the most efficient and the most robust on various |
| 129 | 129 |
/// test inputs. |
| 130 | 130 |
/// However, another pivot rule can be selected using the \ref run() |
| 131 | 131 |
/// function with the proper parameter. |
| 132 | 132 |
enum PivotRule {
|
| 133 | 133 |
|
| 134 | 134 |
/// The \e First \e Eligible pivot rule. |
| 135 | 135 |
/// The next eligible arc is selected in a wraparound fashion |
| 136 | 136 |
/// in every iteration. |
| 137 | 137 |
FIRST_ELIGIBLE, |
| 138 | 138 |
|
| 139 | 139 |
/// The \e Best \e Eligible pivot rule. |
| 140 | 140 |
/// The best eligible arc is selected in every iteration. |
| 141 | 141 |
BEST_ELIGIBLE, |
| 142 | 142 |
|
| 143 | 143 |
/// The \e Block \e Search pivot rule. |
| 144 | 144 |
/// A specified number of arcs are examined in every iteration |
| 145 | 145 |
/// in a wraparound fashion and the best eligible arc is selected |
| 146 | 146 |
/// from this block. |
| 147 | 147 |
BLOCK_SEARCH, |
| 148 | 148 |
|
| 149 | 149 |
/// The \e Candidate \e List pivot rule. |
| 150 | 150 |
/// In a major iteration a candidate list is built from eligible arcs |
| 151 | 151 |
/// in a wraparound fashion and in the following minor iterations |
| 152 | 152 |
/// the best eligible arc is selected from this list. |
| 153 | 153 |
CANDIDATE_LIST, |
| 154 | 154 |
|
| 155 | 155 |
/// The \e Altering \e Candidate \e List pivot rule. |
| 156 | 156 |
/// It is a modified version of the Candidate List method. |
| 157 | 157 |
/// It keeps only the several best eligible arcs from the former |
| 158 | 158 |
/// candidate list and extends this list in every iteration. |
| 159 | 159 |
ALTERING_LIST |
| 160 | 160 |
}; |
| 161 |
|
|
| 161 |
|
|
| 162 | 162 |
private: |
| 163 | 163 |
|
| 164 | 164 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 165 | 165 |
|
| 166 | 166 |
typedef std::vector<int> IntVector; |
| 167 | 167 |
typedef std::vector<Value> ValueVector; |
| 168 | 168 |
typedef std::vector<Cost> CostVector; |
| 169 | 169 |
typedef std::vector<char> BoolVector; |
| 170 | 170 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
| 171 | 171 |
|
| 172 | 172 |
// State constants for arcs |
| 173 | 173 |
enum ArcState {
|
| 174 | 174 |
STATE_UPPER = -1, |
| 175 | 175 |
STATE_TREE = 0, |
| 176 | 176 |
STATE_LOWER = 1 |
| 177 | 177 |
}; |
| 178 | 178 |
|
| 179 | 179 |
typedef std::vector<signed char> StateVector; |
| 180 | 180 |
// Note: vector<signed char> is used instead of vector<ArcState> for |
| 181 | 181 |
// efficiency reasons |
| 182 | 182 |
|
| 183 | 183 |
private: |
| 184 | 184 |
|
| 185 | 185 |
// Data related to the underlying digraph |
| ... | ... |
@@ -206,53 +206,53 @@ |
| 206 | 206 |
ValueVector _upper; |
| 207 | 207 |
ValueVector _cap; |
| 208 | 208 |
CostVector _cost; |
| 209 | 209 |
ValueVector _supply; |
| 210 | 210 |
ValueVector _flow; |
| 211 | 211 |
CostVector _pi; |
| 212 | 212 |
|
| 213 | 213 |
// Data for storing the spanning tree structure |
| 214 | 214 |
IntVector _parent; |
| 215 | 215 |
IntVector _pred; |
| 216 | 216 |
IntVector _thread; |
| 217 | 217 |
IntVector _rev_thread; |
| 218 | 218 |
IntVector _succ_num; |
| 219 | 219 |
IntVector _last_succ; |
| 220 | 220 |
IntVector _dirty_revs; |
| 221 | 221 |
BoolVector _forward; |
| 222 | 222 |
StateVector _state; |
| 223 | 223 |
int _root; |
| 224 | 224 |
|
| 225 | 225 |
// Temporary data used in the current pivot iteration |
| 226 | 226 |
int in_arc, join, u_in, v_in, u_out, v_out; |
| 227 | 227 |
int first, second, right, last; |
| 228 | 228 |
int stem, par_stem, new_stem; |
| 229 | 229 |
Value delta; |
| 230 |
|
|
| 230 |
|
|
| 231 | 231 |
const Value MAX; |
| 232 | 232 |
|
| 233 | 233 |
public: |
| 234 |
|
|
| 234 |
|
|
| 235 | 235 |
/// \brief Constant for infinite upper bounds (capacities). |
| 236 | 236 |
/// |
| 237 | 237 |
/// Constant for infinite upper bounds (capacities). |
| 238 | 238 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
| 239 | 239 |
/// \c std::numeric_limits<Value>::max() otherwise. |
| 240 | 240 |
const Value INF; |
| 241 | 241 |
|
| 242 | 242 |
private: |
| 243 | 243 |
|
| 244 | 244 |
// Implementation of the First Eligible pivot rule |
| 245 | 245 |
class FirstEligiblePivotRule |
| 246 | 246 |
{
|
| 247 | 247 |
private: |
| 248 | 248 |
|
| 249 | 249 |
// References to the NetworkSimplex class |
| 250 | 250 |
const IntVector &_source; |
| 251 | 251 |
const IntVector &_target; |
| 252 | 252 |
const CostVector &_cost; |
| 253 | 253 |
const StateVector &_state; |
| 254 | 254 |
const CostVector &_pi; |
| 255 | 255 |
int &_in_arc; |
| 256 | 256 |
int _search_arc_num; |
| 257 | 257 |
|
| 258 | 258 |
// Pivot rule data |
| ... | ... |
@@ -477,50 +477,50 @@ |
| 477 | 477 |
_curr_length = 0; |
| 478 | 478 |
for (e = _next_arc; e != _search_arc_num; ++e) {
|
| 479 | 479 |
c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
| 480 | 480 |
if (c < 0) {
|
| 481 | 481 |
_candidates[_curr_length++] = e; |
| 482 | 482 |
if (c < min) {
|
| 483 | 483 |
min = c; |
| 484 | 484 |
_in_arc = e; |
| 485 | 485 |
} |
| 486 | 486 |
if (_curr_length == _list_length) goto search_end; |
| 487 | 487 |
} |
| 488 | 488 |
} |
| 489 | 489 |
for (e = 0; e != _next_arc; ++e) {
|
| 490 | 490 |
c = _state[e] * (_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
| 491 | 491 |
if (c < 0) {
|
| 492 | 492 |
_candidates[_curr_length++] = e; |
| 493 | 493 |
if (c < min) {
|
| 494 | 494 |
min = c; |
| 495 | 495 |
_in_arc = e; |
| 496 | 496 |
} |
| 497 | 497 |
if (_curr_length == _list_length) goto search_end; |
| 498 | 498 |
} |
| 499 | 499 |
} |
| 500 | 500 |
if (_curr_length == 0) return false; |
| 501 |
|
|
| 502 |
search_end: |
|
| 501 |
|
|
| 502 |
search_end: |
|
| 503 | 503 |
_minor_count = 1; |
| 504 | 504 |
_next_arc = e; |
| 505 | 505 |
return true; |
| 506 | 506 |
} |
| 507 | 507 |
|
| 508 | 508 |
}; //class CandidateListPivotRule |
| 509 | 509 |
|
| 510 | 510 |
|
| 511 | 511 |
// Implementation of the Altering Candidate List pivot rule |
| 512 | 512 |
class AlteringListPivotRule |
| 513 | 513 |
{
|
| 514 | 514 |
private: |
| 515 | 515 |
|
| 516 | 516 |
// References to the NetworkSimplex class |
| 517 | 517 |
const IntVector &_source; |
| 518 | 518 |
const IntVector &_target; |
| 519 | 519 |
const CostVector &_cost; |
| 520 | 520 |
const StateVector &_state; |
| 521 | 521 |
const CostVector &_pi; |
| 522 | 522 |
int &_in_arc; |
| 523 | 523 |
int _search_arc_num; |
| 524 | 524 |
|
| 525 | 525 |
// Pivot rule data |
| 526 | 526 |
int _block_size, _head_length, _curr_length; |
| ... | ... |
@@ -587,90 +587,90 @@ |
| 587 | 587 |
_cand_cost[e] = _state[e] * |
| 588 | 588 |
(_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
| 589 | 589 |
if (_cand_cost[e] < 0) {
|
| 590 | 590 |
_candidates[_curr_length++] = e; |
| 591 | 591 |
} |
| 592 | 592 |
if (--cnt == 0) {
|
| 593 | 593 |
if (_curr_length > limit) goto search_end; |
| 594 | 594 |
limit = 0; |
| 595 | 595 |
cnt = _block_size; |
| 596 | 596 |
} |
| 597 | 597 |
} |
| 598 | 598 |
for (e = 0; e != _next_arc; ++e) {
|
| 599 | 599 |
_cand_cost[e] = _state[e] * |
| 600 | 600 |
(_cost[e] + _pi[_source[e]] - _pi[_target[e]]); |
| 601 | 601 |
if (_cand_cost[e] < 0) {
|
| 602 | 602 |
_candidates[_curr_length++] = e; |
| 603 | 603 |
} |
| 604 | 604 |
if (--cnt == 0) {
|
| 605 | 605 |
if (_curr_length > limit) goto search_end; |
| 606 | 606 |
limit = 0; |
| 607 | 607 |
cnt = _block_size; |
| 608 | 608 |
} |
| 609 | 609 |
} |
| 610 | 610 |
if (_curr_length == 0) return false; |
| 611 |
|
|
| 611 |
|
|
| 612 | 612 |
search_end: |
| 613 | 613 |
|
| 614 | 614 |
// Make heap of the candidate list (approximating a partial sort) |
| 615 | 615 |
make_heap( _candidates.begin(), _candidates.begin() + _curr_length, |
| 616 | 616 |
_sort_func ); |
| 617 | 617 |
|
| 618 | 618 |
// Pop the first element of the heap |
| 619 | 619 |
_in_arc = _candidates[0]; |
| 620 | 620 |
_next_arc = e; |
| 621 | 621 |
pop_heap( _candidates.begin(), _candidates.begin() + _curr_length, |
| 622 | 622 |
_sort_func ); |
| 623 | 623 |
_curr_length = std::min(_head_length, _curr_length - 1); |
| 624 | 624 |
return true; |
| 625 | 625 |
} |
| 626 | 626 |
|
| 627 | 627 |
}; //class AlteringListPivotRule |
| 628 | 628 |
|
| 629 | 629 |
public: |
| 630 | 630 |
|
| 631 | 631 |
/// \brief Constructor. |
| 632 | 632 |
/// |
| 633 | 633 |
/// The constructor of the class. |
| 634 | 634 |
/// |
| 635 | 635 |
/// \param graph The digraph the algorithm runs on. |
| 636 | 636 |
/// \param arc_mixing Indicate if the arcs have to be stored in a |
| 637 |
/// mixed order in the internal data structure. |
|
| 637 |
/// mixed order in the internal data structure. |
|
| 638 | 638 |
/// In special cases, it could lead to better overall performance, |
| 639 | 639 |
/// but it is usually slower. Therefore it is disabled by default. |
| 640 | 640 |
NetworkSimplex(const GR& graph, bool arc_mixing = false) : |
| 641 | 641 |
_graph(graph), _node_id(graph), _arc_id(graph), |
| 642 | 642 |
_arc_mixing(arc_mixing), |
| 643 | 643 |
MAX(std::numeric_limits<Value>::max()), |
| 644 | 644 |
INF(std::numeric_limits<Value>::has_infinity ? |
| 645 | 645 |
std::numeric_limits<Value>::infinity() : MAX) |
| 646 | 646 |
{
|
| 647 | 647 |
// Check the number types |
| 648 | 648 |
LEMON_ASSERT(std::numeric_limits<Value>::is_signed, |
| 649 | 649 |
"The flow type of NetworkSimplex must be signed"); |
| 650 | 650 |
LEMON_ASSERT(std::numeric_limits<Cost>::is_signed, |
| 651 | 651 |
"The cost type of NetworkSimplex must be signed"); |
| 652 |
|
|
| 652 |
|
|
| 653 | 653 |
// Reset data structures |
| 654 | 654 |
reset(); |
| 655 | 655 |
} |
| 656 | 656 |
|
| 657 | 657 |
/// \name Parameters |
| 658 | 658 |
/// The parameters of the algorithm can be specified using these |
| 659 | 659 |
/// functions. |
| 660 | 660 |
|
| 661 | 661 |
/// @{
|
| 662 | 662 |
|
| 663 | 663 |
/// \brief Set the lower bounds on the arcs. |
| 664 | 664 |
/// |
| 665 | 665 |
/// This function sets the lower bounds on the arcs. |
| 666 | 666 |
/// If it is not used before calling \ref run(), the lower bounds |
| 667 | 667 |
/// will be set to zero on all arcs. |
| 668 | 668 |
/// |
| 669 | 669 |
/// \param map An arc map storing the lower bounds. |
| 670 | 670 |
/// Its \c Value type must be convertible to the \c Value type |
| 671 | 671 |
/// of the algorithm. |
| 672 | 672 |
/// |
| 673 | 673 |
/// \return <tt>(*this)</tt> |
| 674 | 674 |
template <typename LowerMap> |
| 675 | 675 |
NetworkSimplex& lowerMap(const LowerMap& map) {
|
| 676 | 676 |
_have_lower = true; |
| ... | ... |
@@ -742,75 +742,75 @@ |
| 742 | 742 |
/// |
| 743 | 743 |
/// This function sets a single source node and a single target node |
| 744 | 744 |
/// and the required flow value. |
| 745 | 745 |
/// If neither this function nor \ref supplyMap() is used before |
| 746 | 746 |
/// calling \ref run(), the supply of each node will be set to zero. |
| 747 | 747 |
/// |
| 748 | 748 |
/// Using this function has the same effect as using \ref supplyMap() |
| 749 | 749 |
/// with such a map in which \c k is assigned to \c s, \c -k is |
| 750 | 750 |
/// assigned to \c t and all other nodes have zero supply value. |
| 751 | 751 |
/// |
| 752 | 752 |
/// \param s The source node. |
| 753 | 753 |
/// \param t The target node. |
| 754 | 754 |
/// \param k The required amount of flow from node \c s to node \c t |
| 755 | 755 |
/// (i.e. the supply of \c s and the demand of \c t). |
| 756 | 756 |
/// |
| 757 | 757 |
/// \return <tt>(*this)</tt> |
| 758 | 758 |
NetworkSimplex& stSupply(const Node& s, const Node& t, Value k) {
|
| 759 | 759 |
for (int i = 0; i != _node_num; ++i) {
|
| 760 | 760 |
_supply[i] = 0; |
| 761 | 761 |
} |
| 762 | 762 |
_supply[_node_id[s]] = k; |
| 763 | 763 |
_supply[_node_id[t]] = -k; |
| 764 | 764 |
return *this; |
| 765 | 765 |
} |
| 766 |
|
|
| 766 |
|
|
| 767 | 767 |
/// \brief Set the type of the supply constraints. |
| 768 | 768 |
/// |
| 769 | 769 |
/// This function sets the type of the supply/demand constraints. |
| 770 | 770 |
/// If it is not used before calling \ref run(), the \ref GEQ supply |
| 771 | 771 |
/// type will be used. |
| 772 | 772 |
/// |
| 773 | 773 |
/// For more information, see \ref SupplyType. |
| 774 | 774 |
/// |
| 775 | 775 |
/// \return <tt>(*this)</tt> |
| 776 | 776 |
NetworkSimplex& supplyType(SupplyType supply_type) {
|
| 777 | 777 |
_stype = supply_type; |
| 778 | 778 |
return *this; |
| 779 | 779 |
} |
| 780 | 780 |
|
| 781 | 781 |
/// @} |
| 782 | 782 |
|
| 783 | 783 |
/// \name Execution Control |
| 784 | 784 |
/// The algorithm can be executed using \ref run(). |
| 785 | 785 |
|
| 786 | 786 |
/// @{
|
| 787 | 787 |
|
| 788 | 788 |
/// \brief Run the algorithm. |
| 789 | 789 |
/// |
| 790 | 790 |
/// This function runs the algorithm. |
| 791 | 791 |
/// The paramters can be specified using functions \ref lowerMap(), |
| 792 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(), |
|
| 792 |
/// \ref upperMap(), \ref costMap(), \ref supplyMap(), \ref stSupply(), |
|
| 793 | 793 |
/// \ref supplyType(). |
| 794 | 794 |
/// For example, |
| 795 | 795 |
/// \code |
| 796 | 796 |
/// NetworkSimplex<ListDigraph> ns(graph); |
| 797 | 797 |
/// ns.lowerMap(lower).upperMap(upper).costMap(cost) |
| 798 | 798 |
/// .supplyMap(sup).run(); |
| 799 | 799 |
/// \endcode |
| 800 | 800 |
/// |
| 801 | 801 |
/// This function can be called more than once. All the given parameters |
| 802 | 802 |
/// are kept for the next call, unless \ref resetParams() or \ref reset() |
| 803 | 803 |
/// is used, thus only the modified parameters have to be set again. |
| 804 | 804 |
/// If the underlying digraph was also modified after the construction |
| 805 | 805 |
/// of the class (or the last \ref reset() call), then the \ref reset() |
| 806 | 806 |
/// function must be called. |
| 807 | 807 |
/// |
| 808 | 808 |
/// \param pivot_rule The pivot rule that will be used during the |
| 809 | 809 |
/// algorithm. For more information, see \ref PivotRule. |
| 810 | 810 |
/// |
| 811 | 811 |
/// \return \c INFEASIBLE if no feasible flow exists, |
| 812 | 812 |
/// \n \c OPTIMAL if the problem has optimal solution |
| 813 | 813 |
/// (i.e. it is feasible and bounded), and the algorithm has found |
| 814 | 814 |
/// optimal flow and node potentials (primal and dual solutions), |
| 815 | 815 |
/// \n \c UNBOUNDED if the objective function of the problem is |
| 816 | 816 |
/// unbounded, i.e. there is a directed cycle having negative total |
| ... | ... |
@@ -923,54 +923,54 @@ |
| 923 | 923 |
// Copy the graph |
| 924 | 924 |
int i = 0; |
| 925 | 925 |
for (NodeIt n(_graph); n != INVALID; ++n, ++i) {
|
| 926 | 926 |
_node_id[n] = i; |
| 927 | 927 |
} |
| 928 | 928 |
if (_arc_mixing) {
|
| 929 | 929 |
// Store the arcs in a mixed order |
| 930 | 930 |
int k = std::max(int(std::sqrt(double(_arc_num))), 10); |
| 931 | 931 |
int i = 0, j = 0; |
| 932 | 932 |
for (ArcIt a(_graph); a != INVALID; ++a) {
|
| 933 | 933 |
_arc_id[a] = i; |
| 934 | 934 |
_source[i] = _node_id[_graph.source(a)]; |
| 935 | 935 |
_target[i] = _node_id[_graph.target(a)]; |
| 936 | 936 |
if ((i += k) >= _arc_num) i = ++j; |
| 937 | 937 |
} |
| 938 | 938 |
} else {
|
| 939 | 939 |
// Store the arcs in the original order |
| 940 | 940 |
int i = 0; |
| 941 | 941 |
for (ArcIt a(_graph); a != INVALID; ++a, ++i) {
|
| 942 | 942 |
_arc_id[a] = i; |
| 943 | 943 |
_source[i] = _node_id[_graph.source(a)]; |
| 944 | 944 |
_target[i] = _node_id[_graph.target(a)]; |
| 945 | 945 |
} |
| 946 | 946 |
} |
| 947 |
|
|
| 947 |
|
|
| 948 | 948 |
// Reset parameters |
| 949 | 949 |
resetParams(); |
| 950 | 950 |
return *this; |
| 951 | 951 |
} |
| 952 |
|
|
| 952 |
|
|
| 953 | 953 |
/// @} |
| 954 | 954 |
|
| 955 | 955 |
/// \name Query Functions |
| 956 | 956 |
/// The results of the algorithm can be obtained using these |
| 957 | 957 |
/// functions.\n |
| 958 | 958 |
/// The \ref run() function must be called before using them. |
| 959 | 959 |
|
| 960 | 960 |
/// @{
|
| 961 | 961 |
|
| 962 | 962 |
/// \brief Return the total cost of the found flow. |
| 963 | 963 |
/// |
| 964 | 964 |
/// This function returns the total cost of the found flow. |
| 965 | 965 |
/// Its complexity is O(e). |
| 966 | 966 |
/// |
| 967 | 967 |
/// \note The return type of the function can be specified as a |
| 968 | 968 |
/// template parameter. For example, |
| 969 | 969 |
/// \code |
| 970 | 970 |
/// ns.totalCost<double>(); |
| 971 | 971 |
/// \endcode |
| 972 | 972 |
/// It is useful if the total cost cannot be stored in the \c Cost |
| 973 | 973 |
/// type of the algorithm, which is the default return type of the |
| 974 | 974 |
/// function. |
| 975 | 975 |
/// |
| 976 | 976 |
/// \pre \ref run() must be called before using this function. |
| ... | ... |
@@ -1068,49 +1068,49 @@ |
| 1068 | 1068 |
} |
| 1069 | 1069 |
} else {
|
| 1070 | 1070 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1071 | 1071 |
_cap[i] = _upper[i]; |
| 1072 | 1072 |
} |
| 1073 | 1073 |
} |
| 1074 | 1074 |
|
| 1075 | 1075 |
// Initialize artifical cost |
| 1076 | 1076 |
Cost ART_COST; |
| 1077 | 1077 |
if (std::numeric_limits<Cost>::is_exact) {
|
| 1078 | 1078 |
ART_COST = std::numeric_limits<Cost>::max() / 2 + 1; |
| 1079 | 1079 |
} else {
|
| 1080 | 1080 |
ART_COST = std::numeric_limits<Cost>::min(); |
| 1081 | 1081 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1082 | 1082 |
if (_cost[i] > ART_COST) ART_COST = _cost[i]; |
| 1083 | 1083 |
} |
| 1084 | 1084 |
ART_COST = (ART_COST + 1) * _node_num; |
| 1085 | 1085 |
} |
| 1086 | 1086 |
|
| 1087 | 1087 |
// Initialize arc maps |
| 1088 | 1088 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1089 | 1089 |
_flow[i] = 0; |
| 1090 | 1090 |
_state[i] = STATE_LOWER; |
| 1091 | 1091 |
} |
| 1092 |
|
|
| 1092 |
|
|
| 1093 | 1093 |
// Set data for the artificial root node |
| 1094 | 1094 |
_root = _node_num; |
| 1095 | 1095 |
_parent[_root] = -1; |
| 1096 | 1096 |
_pred[_root] = -1; |
| 1097 | 1097 |
_thread[_root] = 0; |
| 1098 | 1098 |
_rev_thread[0] = _root; |
| 1099 | 1099 |
_succ_num[_root] = _node_num + 1; |
| 1100 | 1100 |
_last_succ[_root] = _root - 1; |
| 1101 | 1101 |
_supply[_root] = -_sum_supply; |
| 1102 | 1102 |
_pi[_root] = 0; |
| 1103 | 1103 |
|
| 1104 | 1104 |
// Add artificial arcs and initialize the spanning tree data structure |
| 1105 | 1105 |
if (_sum_supply == 0) {
|
| 1106 | 1106 |
// EQ supply constraints |
| 1107 | 1107 |
_search_arc_num = _arc_num; |
| 1108 | 1108 |
_all_arc_num = _arc_num + _node_num; |
| 1109 | 1109 |
for (int u = 0, e = _arc_num; u != _node_num; ++u, ++e) {
|
| 1110 | 1110 |
_parent[u] = _root; |
| 1111 | 1111 |
_pred[u] = e; |
| 1112 | 1112 |
_thread[u] = u + 1; |
| 1113 | 1113 |
_rev_thread[u + 1] = u; |
| 1114 | 1114 |
_succ_num[u] = 1; |
| 1115 | 1115 |
_last_succ[u] = u; |
| 1116 | 1116 |
_cap[e] = INF; |
| ... | ... |
@@ -1242,49 +1242,49 @@ |
| 1242 | 1242 |
second = _target[in_arc]; |
| 1243 | 1243 |
} else {
|
| 1244 | 1244 |
first = _target[in_arc]; |
| 1245 | 1245 |
second = _source[in_arc]; |
| 1246 | 1246 |
} |
| 1247 | 1247 |
delta = _cap[in_arc]; |
| 1248 | 1248 |
int result = 0; |
| 1249 | 1249 |
Value d; |
| 1250 | 1250 |
int e; |
| 1251 | 1251 |
|
| 1252 | 1252 |
// Search the cycle along the path form the first node to the root |
| 1253 | 1253 |
for (int u = first; u != join; u = _parent[u]) {
|
| 1254 | 1254 |
e = _pred[u]; |
| 1255 | 1255 |
d = _forward[u] ? |
| 1256 | 1256 |
_flow[e] : (_cap[e] >= MAX ? INF : _cap[e] - _flow[e]); |
| 1257 | 1257 |
if (d < delta) {
|
| 1258 | 1258 |
delta = d; |
| 1259 | 1259 |
u_out = u; |
| 1260 | 1260 |
result = 1; |
| 1261 | 1261 |
} |
| 1262 | 1262 |
} |
| 1263 | 1263 |
// Search the cycle along the path form the second node to the root |
| 1264 | 1264 |
for (int u = second; u != join; u = _parent[u]) {
|
| 1265 | 1265 |
e = _pred[u]; |
| 1266 |
d = _forward[u] ? |
|
| 1266 |
d = _forward[u] ? |
|
| 1267 | 1267 |
(_cap[e] >= MAX ? INF : _cap[e] - _flow[e]) : _flow[e]; |
| 1268 | 1268 |
if (d <= delta) {
|
| 1269 | 1269 |
delta = d; |
| 1270 | 1270 |
u_out = u; |
| 1271 | 1271 |
result = 2; |
| 1272 | 1272 |
} |
| 1273 | 1273 |
} |
| 1274 | 1274 |
|
| 1275 | 1275 |
if (result == 1) {
|
| 1276 | 1276 |
u_in = first; |
| 1277 | 1277 |
v_in = second; |
| 1278 | 1278 |
} else {
|
| 1279 | 1279 |
u_in = second; |
| 1280 | 1280 |
v_in = first; |
| 1281 | 1281 |
} |
| 1282 | 1282 |
return result != 0; |
| 1283 | 1283 |
} |
| 1284 | 1284 |
|
| 1285 | 1285 |
// Change _flow and _state vectors |
| 1286 | 1286 |
void changeFlow(bool change) {
|
| 1287 | 1287 |
// Augment along the cycle |
| 1288 | 1288 |
if (delta > 0) {
|
| 1289 | 1289 |
Value val = _state[in_arc] * delta; |
| 1290 | 1290 |
_flow[in_arc] += val; |
| ... | ... |
@@ -1546,66 +1546,66 @@ |
| 1546 | 1546 |
case ALTERING_LIST: |
| 1547 | 1547 |
return start<AlteringListPivotRule>(); |
| 1548 | 1548 |
} |
| 1549 | 1549 |
return INFEASIBLE; // avoid warning |
| 1550 | 1550 |
} |
| 1551 | 1551 |
|
| 1552 | 1552 |
template <typename PivotRuleImpl> |
| 1553 | 1553 |
ProblemType start() {
|
| 1554 | 1554 |
PivotRuleImpl pivot(*this); |
| 1555 | 1555 |
|
| 1556 | 1556 |
// Perform heuristic initial pivots |
| 1557 | 1557 |
if (!initialPivots()) return UNBOUNDED; |
| 1558 | 1558 |
|
| 1559 | 1559 |
// Execute the Network Simplex algorithm |
| 1560 | 1560 |
while (pivot.findEnteringArc()) {
|
| 1561 | 1561 |
findJoinNode(); |
| 1562 | 1562 |
bool change = findLeavingArc(); |
| 1563 | 1563 |
if (delta >= MAX) return UNBOUNDED; |
| 1564 | 1564 |
changeFlow(change); |
| 1565 | 1565 |
if (change) {
|
| 1566 | 1566 |
updateTreeStructure(); |
| 1567 | 1567 |
updatePotential(); |
| 1568 | 1568 |
} |
| 1569 | 1569 |
} |
| 1570 |
|
|
| 1570 |
|
|
| 1571 | 1571 |
// Check feasibility |
| 1572 | 1572 |
for (int e = _search_arc_num; e != _all_arc_num; ++e) {
|
| 1573 | 1573 |
if (_flow[e] != 0) return INFEASIBLE; |
| 1574 | 1574 |
} |
| 1575 | 1575 |
|
| 1576 | 1576 |
// Transform the solution and the supply map to the original form |
| 1577 | 1577 |
if (_have_lower) {
|
| 1578 | 1578 |
for (int i = 0; i != _arc_num; ++i) {
|
| 1579 | 1579 |
Value c = _lower[i]; |
| 1580 | 1580 |
if (c != 0) {
|
| 1581 | 1581 |
_flow[i] += c; |
| 1582 | 1582 |
_supply[_source[i]] += c; |
| 1583 | 1583 |
_supply[_target[i]] -= c; |
| 1584 | 1584 |
} |
| 1585 | 1585 |
} |
| 1586 | 1586 |
} |
| 1587 |
|
|
| 1587 |
|
|
| 1588 | 1588 |
// Shift potentials to meet the requirements of the GEQ/LEQ type |
| 1589 | 1589 |
// optimality conditions |
| 1590 | 1590 |
if (_sum_supply == 0) {
|
| 1591 | 1591 |
if (_stype == GEQ) {
|
| 1592 | 1592 |
Cost max_pot = std::numeric_limits<Cost>::min(); |
| 1593 | 1593 |
for (int i = 0; i != _node_num; ++i) {
|
| 1594 | 1594 |
if (_pi[i] > max_pot) max_pot = _pi[i]; |
| 1595 | 1595 |
} |
| 1596 | 1596 |
if (max_pot > 0) {
|
| 1597 | 1597 |
for (int i = 0; i != _node_num; ++i) |
| 1598 | 1598 |
_pi[i] -= max_pot; |
| 1599 | 1599 |
} |
| 1600 | 1600 |
} else {
|
| 1601 | 1601 |
Cost min_pot = std::numeric_limits<Cost>::max(); |
| 1602 | 1602 |
for (int i = 0; i != _node_num; ++i) {
|
| 1603 | 1603 |
if (_pi[i] < min_pot) min_pot = _pi[i]; |
| 1604 | 1604 |
} |
| 1605 | 1605 |
if (min_pot < 0) {
|
| 1606 | 1606 |
for (int i = 0; i != _node_num; ++i) |
| 1607 | 1607 |
_pi[i] -= min_pot; |
| 1608 | 1608 |
} |
| 1609 | 1609 |
} |
| 1610 | 1610 |
} |
| 1611 | 1611 |
| 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 |
///\ingroup paths |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief Classes for representing paths in digraphs. |
| 22 | 22 |
/// |
| 23 | 23 |
|
| 24 | 24 |
#ifndef LEMON_PATH_H |
| 25 | 25 |
#define LEMON_PATH_H |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <algorithm> |
| 29 | 29 |
|
| ... | ... |
@@ -945,62 +945,62 @@ |
| 945 | 945 |
to.clear(); |
| 946 | 946 |
to.build(from); |
| 947 | 947 |
} |
| 948 | 948 |
}; |
| 949 | 949 |
|
| 950 | 950 |
template <typename From, typename To, |
| 951 | 951 |
bool buildEnable = BuildTagIndicator<To>::value> |
| 952 | 952 |
struct PathCopySelectorBackward {
|
| 953 | 953 |
static void copy(const From& from, To& to) {
|
| 954 | 954 |
to.clear(); |
| 955 | 955 |
for (typename From::RevArcIt it(from); it != INVALID; ++it) {
|
| 956 | 956 |
to.addFront(it); |
| 957 | 957 |
} |
| 958 | 958 |
} |
| 959 | 959 |
}; |
| 960 | 960 |
|
| 961 | 961 |
template <typename From, typename To> |
| 962 | 962 |
struct PathCopySelectorBackward<From, To, true> {
|
| 963 | 963 |
static void copy(const From& from, To& to) {
|
| 964 | 964 |
to.clear(); |
| 965 | 965 |
to.buildRev(from); |
| 966 | 966 |
} |
| 967 | 967 |
}; |
| 968 | 968 |
|
| 969 |
|
|
| 969 |
|
|
| 970 | 970 |
template <typename From, typename To, |
| 971 | 971 |
bool revEnable = RevPathTagIndicator<From>::value> |
| 972 | 972 |
struct PathCopySelector {
|
| 973 | 973 |
static void copy(const From& from, To& to) {
|
| 974 | 974 |
PathCopySelectorForward<From, To>::copy(from, to); |
| 975 |
} |
|
| 975 |
} |
|
| 976 | 976 |
}; |
| 977 | 977 |
|
| 978 | 978 |
template <typename From, typename To> |
| 979 | 979 |
struct PathCopySelector<From, To, true> {
|
| 980 | 980 |
static void copy(const From& from, To& to) {
|
| 981 | 981 |
PathCopySelectorBackward<From, To>::copy(from, to); |
| 982 |
} |
|
| 982 |
} |
|
| 983 | 983 |
}; |
| 984 | 984 |
|
| 985 | 985 |
} |
| 986 | 986 |
|
| 987 | 987 |
|
| 988 | 988 |
/// \brief Make a copy of a path. |
| 989 | 989 |
/// |
| 990 | 990 |
/// This function makes a copy of a path. |
| 991 | 991 |
template <typename From, typename To> |
| 992 | 992 |
void pathCopy(const From& from, To& to) {
|
| 993 | 993 |
checkConcept<concepts::PathDumper<typename From::Digraph>, From>(); |
| 994 | 994 |
_path_bits::PathCopySelector<From, To>::copy(from, to); |
| 995 | 995 |
} |
| 996 | 996 |
|
| 997 | 997 |
/// \brief Deprecated version of \ref pathCopy(). |
| 998 | 998 |
/// |
| 999 | 999 |
/// Deprecated version of \ref pathCopy() (only for reverse compatibility). |
| 1000 | 1000 |
template <typename To, typename From> |
| 1001 | 1001 |
void copyPath(To& to, const From& from) {
|
| 1002 | 1002 |
pathCopy(from, to); |
| 1003 | 1003 |
} |
| 1004 | 1004 |
|
| 1005 | 1005 |
/// \brief Check the consistency of a path. |
| 1006 | 1006 |
/// |
| 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_PLANARITY_H |
| 20 | 20 |
#define LEMON_PLANARITY_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup planar |
| 23 | 23 |
/// \file |
| 24 | 24 |
/// \brief Planarity checking, embedding, drawing and coloring |
| 25 | 25 |
|
| 26 | 26 |
#include <vector> |
| 27 | 27 |
#include <list> |
| 28 | 28 |
|
| 29 | 29 |
#include <lemon/dfs.h> |
| ... | ... |
@@ -119,59 +119,59 @@ |
| 119 | 119 |
bool inverted; |
| 120 | 120 |
}; |
| 121 | 121 |
|
| 122 | 122 |
template <typename Graph> |
| 123 | 123 |
struct NodeDataNode<Graph, false> {
|
| 124 | 124 |
int prev, next; |
| 125 | 125 |
int visited; |
| 126 | 126 |
}; |
| 127 | 127 |
|
| 128 | 128 |
template <typename Graph> |
| 129 | 129 |
struct ChildListNode {
|
| 130 | 130 |
typedef typename Graph::Node Node; |
| 131 | 131 |
Node first; |
| 132 | 132 |
Node prev, next; |
| 133 | 133 |
}; |
| 134 | 134 |
|
| 135 | 135 |
template <typename Graph> |
| 136 | 136 |
struct ArcListNode {
|
| 137 | 137 |
typename Graph::Arc prev, next; |
| 138 | 138 |
}; |
| 139 | 139 |
|
| 140 | 140 |
template <typename Graph> |
| 141 | 141 |
class PlanarityChecking {
|
| 142 | 142 |
private: |
| 143 |
|
|
| 143 |
|
|
| 144 | 144 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 145 | 145 |
|
| 146 | 146 |
const Graph& _graph; |
| 147 | 147 |
|
| 148 | 148 |
private: |
| 149 |
|
|
| 149 |
|
|
| 150 | 150 |
typedef typename Graph::template NodeMap<Arc> PredMap; |
| 151 |
|
|
| 151 |
|
|
| 152 | 152 |
typedef typename Graph::template EdgeMap<bool> TreeMap; |
| 153 |
|
|
| 153 |
|
|
| 154 | 154 |
typedef typename Graph::template NodeMap<int> OrderMap; |
| 155 | 155 |
typedef std::vector<Node> OrderList; |
| 156 | 156 |
|
| 157 | 157 |
typedef typename Graph::template NodeMap<int> LowMap; |
| 158 | 158 |
typedef typename Graph::template NodeMap<int> AncestorMap; |
| 159 | 159 |
|
| 160 | 160 |
typedef _planarity_bits::NodeDataNode<Graph> NodeDataNode; |
| 161 | 161 |
typedef std::vector<NodeDataNode> NodeData; |
| 162 | 162 |
|
| 163 | 163 |
typedef _planarity_bits::ChildListNode<Graph> ChildListNode; |
| 164 | 164 |
typedef typename Graph::template NodeMap<ChildListNode> ChildLists; |
| 165 | 165 |
|
| 166 | 166 |
typedef typename Graph::template NodeMap<std::list<int> > MergeRoots; |
| 167 | 167 |
|
| 168 | 168 |
typedef typename Graph::template NodeMap<bool> EmbedArc; |
| 169 | 169 |
|
| 170 | 170 |
public: |
| 171 | 171 |
|
| 172 | 172 |
PlanarityChecking(const Graph& graph) : _graph(graph) {}
|
| 173 | 173 |
|
| 174 | 174 |
bool run() {
|
| 175 | 175 |
typedef _planarity_bits::PlanarityVisitor<Graph> Visitor; |
| 176 | 176 |
|
| 177 | 177 |
PredMap pred_map(_graph, INVALID); |
| ... | ... |
@@ -200,49 +200,49 @@ |
| 200 | 200 |
for (int i = order_list.size() - 1; i >= 0; --i) {
|
| 201 | 201 |
|
| 202 | 202 |
Node node = order_list[i]; |
| 203 | 203 |
|
| 204 | 204 |
Node source = node; |
| 205 | 205 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
|
| 206 | 206 |
Node target = _graph.target(e); |
| 207 | 207 |
|
| 208 | 208 |
if (order_map[source] < order_map[target] && tree_map[e]) {
|
| 209 | 209 |
initFace(target, node_data, order_map, order_list); |
| 210 | 210 |
} |
| 211 | 211 |
} |
| 212 | 212 |
|
| 213 | 213 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
|
| 214 | 214 |
Node target = _graph.target(e); |
| 215 | 215 |
|
| 216 | 216 |
if (order_map[source] < order_map[target] && !tree_map[e]) {
|
| 217 | 217 |
embed_arc[target] = true; |
| 218 | 218 |
walkUp(target, source, i, pred_map, low_map, |
| 219 | 219 |
order_map, order_list, node_data, merge_roots); |
| 220 | 220 |
} |
| 221 | 221 |
} |
| 222 | 222 |
|
| 223 | 223 |
for (typename MergeRoots::Value::iterator it = |
| 224 |
merge_roots[node].begin(); |
|
| 224 |
merge_roots[node].begin(); |
|
| 225 | 225 |
it != merge_roots[node].end(); ++it) {
|
| 226 | 226 |
int rn = *it; |
| 227 | 227 |
walkDown(rn, i, node_data, order_list, child_lists, |
| 228 | 228 |
ancestor_map, low_map, embed_arc, merge_roots); |
| 229 | 229 |
} |
| 230 | 230 |
merge_roots[node].clear(); |
| 231 | 231 |
|
| 232 | 232 |
for (OutArcIt e(_graph, node); e != INVALID; ++e) {
|
| 233 | 233 |
Node target = _graph.target(e); |
| 234 | 234 |
|
| 235 | 235 |
if (order_map[source] < order_map[target] && !tree_map[e]) {
|
| 236 | 236 |
if (embed_arc[target]) {
|
| 237 | 237 |
return false; |
| 238 | 238 |
} |
| 239 | 239 |
} |
| 240 | 240 |
} |
| 241 | 241 |
} |
| 242 | 242 |
|
| 243 | 243 |
return true; |
| 244 | 244 |
} |
| 245 | 245 |
|
| 246 | 246 |
private: |
| 247 | 247 |
|
| 248 | 248 |
void createChildLists(const TreeMap& tree_map, const OrderMap& order_map, |
| ... | ... |
@@ -411,49 +411,49 @@ |
| 411 | 411 |
|
| 412 | 412 |
// Embedding arc into external face |
| 413 | 413 |
if (rd) node_data[rn].next = n; else node_data[rn].prev = n; |
| 414 | 414 |
if (d) node_data[n].prev = rn; else node_data[n].next = rn; |
| 415 | 415 |
pn = rn; |
| 416 | 416 |
|
| 417 | 417 |
embed_arc[order_list[n]] = false; |
| 418 | 418 |
} |
| 419 | 419 |
|
| 420 | 420 |
if (!merge_roots[node].empty()) {
|
| 421 | 421 |
|
| 422 | 422 |
bool d = pn == node_data[n].prev; |
| 423 | 423 |
|
| 424 | 424 |
merge_stack.push_back(std::make_pair(n, d)); |
| 425 | 425 |
|
| 426 | 426 |
int rn = merge_roots[node].front(); |
| 427 | 427 |
|
| 428 | 428 |
int xn = node_data[rn].next; |
| 429 | 429 |
Node xnode = order_list[xn]; |
| 430 | 430 |
|
| 431 | 431 |
int yn = node_data[rn].prev; |
| 432 | 432 |
Node ynode = order_list[yn]; |
| 433 | 433 |
|
| 434 | 434 |
bool rd; |
| 435 |
if (!external(xnode, rorder, child_lists, |
|
| 435 |
if (!external(xnode, rorder, child_lists, |
|
| 436 | 436 |
ancestor_map, low_map)) {
|
| 437 | 437 |
rd = true; |
| 438 | 438 |
} else if (!external(ynode, rorder, child_lists, |
| 439 | 439 |
ancestor_map, low_map)) {
|
| 440 | 440 |
rd = false; |
| 441 | 441 |
} else if (pertinent(xnode, embed_arc, merge_roots)) {
|
| 442 | 442 |
rd = true; |
| 443 | 443 |
} else {
|
| 444 | 444 |
rd = false; |
| 445 | 445 |
} |
| 446 | 446 |
|
| 447 | 447 |
merge_stack.push_back(std::make_pair(rn, rd)); |
| 448 | 448 |
|
| 449 | 449 |
pn = rn; |
| 450 | 450 |
n = rd ? xn : yn; |
| 451 | 451 |
|
| 452 | 452 |
} else if (!external(node, rorder, child_lists, |
| 453 | 453 |
ancestor_map, low_map)) {
|
| 454 | 454 |
int nn = (node_data[n].next != pn ? |
| 455 | 455 |
node_data[n].next : node_data[n].prev); |
| 456 | 456 |
|
| 457 | 457 |
bool nd = n == node_data[nn].prev; |
| 458 | 458 |
|
| 459 | 459 |
if (nd) node_data[nn].prev = pn; |
| 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_PREFLOW_H |
| 20 | 20 |
#define LEMON_PREFLOW_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/tolerance.h> |
| 23 | 23 |
#include <lemon/elevator.h> |
| 24 | 24 |
|
| 25 | 25 |
/// \file |
| 26 | 26 |
/// \ingroup max_flow |
| 27 | 27 |
/// \brief Implementation of the preflow algorithm. |
| 28 | 28 |
|
| 29 | 29 |
namespace lemon {
|
| 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_SMART_GRAPH_H |
| 20 | 20 |
#define LEMON_SMART_GRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup graphs |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief SmartDigraph and SmartGraph classes. |
| 25 | 25 |
|
| 26 | 26 |
#include <vector> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/error.h> |
| ... | ... |
@@ -165,49 +165,49 @@ |
| 165 | 165 |
} |
| 166 | 166 |
|
| 167 | 167 |
void nextOut(Arc& arc) const {
|
| 168 | 168 |
arc._id = arcs[arc._id].next_out; |
| 169 | 169 |
} |
| 170 | 170 |
|
| 171 | 171 |
void firstIn(Arc& arc, const Node& node) const {
|
| 172 | 172 |
arc._id = nodes[node._id].first_in; |
| 173 | 173 |
} |
| 174 | 174 |
|
| 175 | 175 |
void nextIn(Arc& arc) const {
|
| 176 | 176 |
arc._id = arcs[arc._id].next_in; |
| 177 | 177 |
} |
| 178 | 178 |
|
| 179 | 179 |
}; |
| 180 | 180 |
|
| 181 | 181 |
typedef DigraphExtender<SmartDigraphBase> ExtendedSmartDigraphBase; |
| 182 | 182 |
|
| 183 | 183 |
///\ingroup graphs |
| 184 | 184 |
/// |
| 185 | 185 |
///\brief A smart directed graph class. |
| 186 | 186 |
/// |
| 187 | 187 |
///\ref SmartDigraph is a simple and fast digraph implementation. |
| 188 | 188 |
///It is also quite memory efficient but at the price |
| 189 |
///that it does not support node and arc deletion |
|
| 189 |
///that it does not support node and arc deletion |
|
| 190 | 190 |
///(except for the Snapshot feature). |
| 191 | 191 |
/// |
| 192 | 192 |
///This type fully conforms to the \ref concepts::Digraph "Digraph concept" |
| 193 | 193 |
///and it also provides some additional functionalities. |
| 194 | 194 |
///Most of its member functions and nested classes are documented |
| 195 | 195 |
///only in the concept class. |
| 196 | 196 |
/// |
| 197 | 197 |
///This class provides constant time counting for nodes and arcs. |
| 198 | 198 |
/// |
| 199 | 199 |
///\sa concepts::Digraph |
| 200 | 200 |
///\sa SmartGraph |
| 201 | 201 |
class SmartDigraph : public ExtendedSmartDigraphBase {
|
| 202 | 202 |
typedef ExtendedSmartDigraphBase Parent; |
| 203 | 203 |
|
| 204 | 204 |
private: |
| 205 | 205 |
/// Digraphs are \e not copy constructible. Use DigraphCopy instead. |
| 206 | 206 |
SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {};
|
| 207 | 207 |
/// \brief Assignment of a digraph to another one is \e not allowed. |
| 208 | 208 |
/// Use DigraphCopy instead. |
| 209 | 209 |
void operator=(const SmartDigraph &) {}
|
| 210 | 210 |
|
| 211 | 211 |
public: |
| 212 | 212 |
|
| 213 | 213 |
/// Constructor |
| ... | ... |
@@ -314,49 +314,49 @@ |
| 314 | 314 |
while(s.arc_num<arcs.size()) {
|
| 315 | 315 |
Arc arc = arcFromId(arcs.size()-1); |
| 316 | 316 |
Parent::notifier(Arc()).erase(arc); |
| 317 | 317 |
nodes[arcs.back().source].first_out=arcs.back().next_out; |
| 318 | 318 |
nodes[arcs.back().target].first_in=arcs.back().next_in; |
| 319 | 319 |
arcs.pop_back(); |
| 320 | 320 |
} |
| 321 | 321 |
while(s.node_num<nodes.size()) {
|
| 322 | 322 |
Node node = nodeFromId(nodes.size()-1); |
| 323 | 323 |
Parent::notifier(Node()).erase(node); |
| 324 | 324 |
nodes.pop_back(); |
| 325 | 325 |
} |
| 326 | 326 |
} |
| 327 | 327 |
|
| 328 | 328 |
public: |
| 329 | 329 |
|
| 330 | 330 |
///Class to make a snapshot of the digraph and to restore it later. |
| 331 | 331 |
|
| 332 | 332 |
///Class to make a snapshot of the digraph and to restore it later. |
| 333 | 333 |
/// |
| 334 | 334 |
///The newly added nodes and arcs can be removed using the |
| 335 | 335 |
///restore() function. This is the only way for deleting nodes and/or |
| 336 | 336 |
///arcs from a SmartDigraph structure. |
| 337 | 337 |
/// |
| 338 |
///\note After a state is restored, you cannot restore a later state, |
|
| 338 |
///\note After a state is restored, you cannot restore a later state, |
|
| 339 | 339 |
///i.e. you cannot add the removed nodes and arcs again using |
| 340 | 340 |
///another Snapshot instance. |
| 341 | 341 |
/// |
| 342 | 342 |
///\warning Node splitting cannot be restored. |
| 343 | 343 |
///\warning The validity of the snapshot is not stored due to |
| 344 | 344 |
///performance reasons. If you do not use the snapshot correctly, |
| 345 | 345 |
///it can cause broken program, invalid or not restored state of |
| 346 | 346 |
///the digraph or no change. |
| 347 | 347 |
class Snapshot |
| 348 | 348 |
{
|
| 349 | 349 |
SmartDigraph *_graph; |
| 350 | 350 |
protected: |
| 351 | 351 |
friend class SmartDigraph; |
| 352 | 352 |
unsigned int node_num; |
| 353 | 353 |
unsigned int arc_num; |
| 354 | 354 |
public: |
| 355 | 355 |
///Default constructor. |
| 356 | 356 |
|
| 357 | 357 |
///Default constructor. |
| 358 | 358 |
///You have to call save() to actually make a snapshot. |
| 359 | 359 |
Snapshot() : _graph(0) {}
|
| 360 | 360 |
///Constructor that immediately makes a snapshot |
| 361 | 361 |
|
| 362 | 362 |
///This constructor immediately makes a snapshot of the given digraph. |
| ... | ... |
@@ -593,49 +593,49 @@ |
| 593 | 593 |
arcs[n].next_out = nodes[v._id].first_out; |
| 594 | 594 |
nodes[v._id].first_out = n; |
| 595 | 595 |
|
| 596 | 596 |
arcs[n | 1].next_out = nodes[u._id].first_out; |
| 597 | 597 |
nodes[u._id].first_out = (n | 1); |
| 598 | 598 |
|
| 599 | 599 |
return Edge(n / 2); |
| 600 | 600 |
} |
| 601 | 601 |
|
| 602 | 602 |
void clear() {
|
| 603 | 603 |
arcs.clear(); |
| 604 | 604 |
nodes.clear(); |
| 605 | 605 |
} |
| 606 | 606 |
|
| 607 | 607 |
}; |
| 608 | 608 |
|
| 609 | 609 |
typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase; |
| 610 | 610 |
|
| 611 | 611 |
/// \ingroup graphs |
| 612 | 612 |
/// |
| 613 | 613 |
/// \brief A smart undirected graph class. |
| 614 | 614 |
/// |
| 615 | 615 |
/// \ref SmartGraph is a simple and fast graph implementation. |
| 616 | 616 |
/// It is also quite memory efficient but at the price |
| 617 |
/// that it does not support node and edge deletion |
|
| 617 |
/// that it does not support node and edge deletion |
|
| 618 | 618 |
/// (except for the Snapshot feature). |
| 619 | 619 |
/// |
| 620 | 620 |
/// This type fully conforms to the \ref concepts::Graph "Graph concept" |
| 621 | 621 |
/// and it also provides some additional functionalities. |
| 622 | 622 |
/// Most of its member functions and nested classes are documented |
| 623 | 623 |
/// only in the concept class. |
| 624 | 624 |
/// |
| 625 | 625 |
/// This class provides constant time counting for nodes, edges and arcs. |
| 626 | 626 |
/// |
| 627 | 627 |
/// \sa concepts::Graph |
| 628 | 628 |
/// \sa SmartDigraph |
| 629 | 629 |
class SmartGraph : public ExtendedSmartGraphBase {
|
| 630 | 630 |
typedef ExtendedSmartGraphBase Parent; |
| 631 | 631 |
|
| 632 | 632 |
private: |
| 633 | 633 |
/// Graphs are \e not copy constructible. Use GraphCopy instead. |
| 634 | 634 |
SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {};
|
| 635 | 635 |
/// \brief Assignment of a graph to another one is \e not allowed. |
| 636 | 636 |
/// Use GraphCopy instead. |
| 637 | 637 |
void operator=(const SmartGraph &) {}
|
| 638 | 638 |
|
| 639 | 639 |
public: |
| 640 | 640 |
|
| 641 | 641 |
/// Constructor |
| ... | ... |
@@ -740,49 +740,49 @@ |
| 740 | 740 |
Parent::notifier(Arc()).erase(dir); |
| 741 | 741 |
nodes[arcs[n-1].target].first_out=arcs[n].next_out; |
| 742 | 742 |
nodes[arcs[n].target].first_out=arcs[n-1].next_out; |
| 743 | 743 |
arcs.pop_back(); |
| 744 | 744 |
arcs.pop_back(); |
| 745 | 745 |
} |
| 746 | 746 |
while(s.node_num<nodes.size()) {
|
| 747 | 747 |
int n=nodes.size()-1; |
| 748 | 748 |
Node node = nodeFromId(n); |
| 749 | 749 |
Parent::notifier(Node()).erase(node); |
| 750 | 750 |
nodes.pop_back(); |
| 751 | 751 |
} |
| 752 | 752 |
} |
| 753 | 753 |
|
| 754 | 754 |
public: |
| 755 | 755 |
|
| 756 | 756 |
///Class to make a snapshot of the graph and to restore it later. |
| 757 | 757 |
|
| 758 | 758 |
///Class to make a snapshot of the graph and to restore it later. |
| 759 | 759 |
/// |
| 760 | 760 |
///The newly added nodes and edges can be removed using the |
| 761 | 761 |
///restore() function. This is the only way for deleting nodes and/or |
| 762 | 762 |
///edges from a SmartGraph structure. |
| 763 | 763 |
/// |
| 764 |
///\note After a state is restored, you cannot restore a later state, |
|
| 764 |
///\note After a state is restored, you cannot restore a later state, |
|
| 765 | 765 |
///i.e. you cannot add the removed nodes and edges again using |
| 766 | 766 |
///another Snapshot instance. |
| 767 | 767 |
/// |
| 768 | 768 |
///\warning The validity of the snapshot is not stored due to |
| 769 | 769 |
///performance reasons. If you do not use the snapshot correctly, |
| 770 | 770 |
///it can cause broken program, invalid or not restored state of |
| 771 | 771 |
///the graph or no change. |
| 772 | 772 |
class Snapshot |
| 773 | 773 |
{
|
| 774 | 774 |
SmartGraph *_graph; |
| 775 | 775 |
protected: |
| 776 | 776 |
friend class SmartGraph; |
| 777 | 777 |
unsigned int node_num; |
| 778 | 778 |
unsigned int arc_num; |
| 779 | 779 |
public: |
| 780 | 780 |
///Default constructor. |
| 781 | 781 |
|
| 782 | 782 |
///Default constructor. |
| 783 | 783 |
///You have to call save() to actually make a snapshot. |
| 784 | 784 |
Snapshot() : _graph(0) {}
|
| 785 | 785 |
///Constructor that immediately makes a snapshot |
| 786 | 786 |
|
| 787 | 787 |
/// This constructor immediately makes a snapshot of the given graph. |
| 788 | 788 |
/// |
| 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 <iostream> |
| 20 | 20 |
#include <lemon/soplex.h> |
| 21 | 21 |
|
| 22 | 22 |
#include <soplex.h> |
| 23 | 23 |
#include <spxout.h> |
| 24 | 24 |
|
| 25 | 25 |
|
| 26 | 26 |
///\file |
| 27 | 27 |
///\brief Implementation of the LEMON-SOPLEX lp solver interface. |
| 28 | 28 |
namespace lemon {
|
| 29 | 29 |
|
| ... | ... |
@@ -266,49 +266,49 @@ |
| 266 | 266 |
} |
| 267 | 267 |
} |
| 268 | 268 |
|
| 269 | 269 |
void SoplexLp::_getObjCoeffs(InsertIterator b) const {
|
| 270 | 270 |
for (int j = 0; j < soplex->nCols(); ++j) {
|
| 271 | 271 |
Value coef = soplex->obj(j); |
| 272 | 272 |
if (coef != 0.0) {
|
| 273 | 273 |
*b = std::make_pair(j, coef); |
| 274 | 274 |
++b; |
| 275 | 275 |
} |
| 276 | 276 |
} |
| 277 | 277 |
} |
| 278 | 278 |
|
| 279 | 279 |
void SoplexLp::_setObjCoeff(int i, Value obj_coef) {
|
| 280 | 280 |
soplex->changeObj(i, obj_coef); |
| 281 | 281 |
} |
| 282 | 282 |
|
| 283 | 283 |
SoplexLp::Value SoplexLp::_getObjCoeff(int i) const {
|
| 284 | 284 |
return soplex->obj(i); |
| 285 | 285 |
} |
| 286 | 286 |
|
| 287 | 287 |
SoplexLp::SolveExitStatus SoplexLp::_solve() {
|
| 288 | 288 |
|
| 289 | 289 |
_clear_temporals(); |
| 290 |
|
|
| 290 |
|
|
| 291 | 291 |
_applyMessageLevel(); |
| 292 | 292 |
|
| 293 | 293 |
soplex::SPxSolver::Status status = soplex->solve(); |
| 294 | 294 |
|
| 295 | 295 |
switch (status) {
|
| 296 | 296 |
case soplex::SPxSolver::OPTIMAL: |
| 297 | 297 |
case soplex::SPxSolver::INFEASIBLE: |
| 298 | 298 |
case soplex::SPxSolver::UNBOUNDED: |
| 299 | 299 |
return SOLVED; |
| 300 | 300 |
default: |
| 301 | 301 |
return UNSOLVED; |
| 302 | 302 |
} |
| 303 | 303 |
} |
| 304 | 304 |
|
| 305 | 305 |
SoplexLp::Value SoplexLp::_getPrimal(int i) const {
|
| 306 | 306 |
if (_primal_values.empty()) {
|
| 307 | 307 |
_primal_values.resize(soplex->nCols()); |
| 308 | 308 |
soplex::Vector pv(_primal_values.size(), &_primal_values.front()); |
| 309 | 309 |
soplex->getPrimal(pv); |
| 310 | 310 |
} |
| 311 | 311 |
return _primal_values[i]; |
| 312 | 312 |
} |
| 313 | 313 |
|
| 314 | 314 |
SoplexLp::Value SoplexLp::_getDual(int i) const {
|
| 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_SOPLEX_H |
| 20 | 20 |
#define LEMON_SOPLEX_H |
| 21 | 21 |
|
| 22 | 22 |
///\file |
| 23 | 23 |
///\brief Header of the LEMON-SOPLEX lp solver interface. |
| 24 | 24 |
|
| 25 | 25 |
#include <vector> |
| 26 | 26 |
#include <string> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/lp_base.h> |
| 29 | 29 |
| 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_STATIC_GRAPH_H |
| 20 | 20 |
#define LEMON_STATIC_GRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup graphs |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief StaticDigraph class. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/core.h> |
| 27 | 27 |
#include <lemon/bits/graph_extender.h> |
| 28 | 28 |
|
| 29 | 29 |
namespace lemon {
|
| 30 | 30 |
|
| 31 | 31 |
class StaticDigraphBase {
|
| 32 | 32 |
public: |
| 33 | 33 |
|
| 34 |
StaticDigraphBase() |
|
| 35 |
: built(false), node_num(0), arc_num(0), |
|
| 34 |
StaticDigraphBase() |
|
| 35 |
: built(false), node_num(0), arc_num(0), |
|
| 36 | 36 |
node_first_out(NULL), node_first_in(NULL), |
| 37 |
arc_source(NULL), arc_target(NULL), |
|
| 37 |
arc_source(NULL), arc_target(NULL), |
|
| 38 | 38 |
arc_next_in(NULL), arc_next_out(NULL) {}
|
| 39 |
|
|
| 39 |
|
|
| 40 | 40 |
~StaticDigraphBase() {
|
| 41 | 41 |
if (built) {
|
| 42 | 42 |
delete[] node_first_out; |
| 43 | 43 |
delete[] node_first_in; |
| 44 | 44 |
delete[] arc_source; |
| 45 | 45 |
delete[] arc_target; |
| 46 | 46 |
delete[] arc_next_out; |
| 47 | 47 |
delete[] arc_next_in; |
| 48 | 48 |
} |
| 49 | 49 |
} |
| 50 | 50 |
|
| 51 | 51 |
class Node {
|
| 52 | 52 |
friend class StaticDigraphBase; |
| 53 | 53 |
protected: |
| 54 | 54 |
int id; |
| 55 | 55 |
Node(int _id) : id(_id) {}
|
| 56 | 56 |
public: |
| 57 | 57 |
Node() {}
|
| 58 | 58 |
Node (Invalid) : id(-1) {}
|
| 59 | 59 |
bool operator==(const Node& node) const { return id == node.id; }
|
| 60 | 60 |
bool operator!=(const Node& node) const { return id != node.id; }
|
| 61 | 61 |
bool operator<(const Node& node) const { return id < node.id; }
|
| 62 | 62 |
}; |
| 63 | 63 |
|
| 64 | 64 |
class Arc {
|
| 65 |
friend class StaticDigraphBase; |
|
| 65 |
friend class StaticDigraphBase; |
|
| 66 | 66 |
protected: |
| 67 | 67 |
int id; |
| 68 | 68 |
Arc(int _id) : id(_id) {}
|
| 69 | 69 |
public: |
| 70 | 70 |
Arc() { }
|
| 71 | 71 |
Arc (Invalid) : id(-1) {}
|
| 72 | 72 |
bool operator==(const Arc& arc) const { return id == arc.id; }
|
| 73 | 73 |
bool operator!=(const Arc& arc) const { return id != arc.id; }
|
| 74 | 74 |
bool operator<(const Arc& arc) const { return id < arc.id; }
|
| 75 | 75 |
}; |
| 76 | 76 |
|
| 77 | 77 |
Node source(const Arc& e) const { return Node(arc_source[e.id]); }
|
| 78 | 78 |
Node target(const Arc& e) const { return Node(arc_target[e.id]); }
|
| 79 | 79 |
|
| 80 | 80 |
void first(Node& n) const { n.id = node_num - 1; }
|
| 81 | 81 |
static void next(Node& n) { --n.id; }
|
| 82 | 82 |
|
| 83 | 83 |
void first(Arc& e) const { e.id = arc_num - 1; }
|
| 84 | 84 |
static void next(Arc& e) { --e.id; }
|
| 85 | 85 |
|
| 86 |
void firstOut(Arc& e, const Node& n) const {
|
|
| 87 |
e.id = node_first_out[n.id] != node_first_out[n.id + 1] ? |
|
| 86 |
void firstOut(Arc& e, const Node& n) const {
|
|
| 87 |
e.id = node_first_out[n.id] != node_first_out[n.id + 1] ? |
|
| 88 | 88 |
node_first_out[n.id] : -1; |
| 89 | 89 |
} |
| 90 | 90 |
void nextOut(Arc& e) const { e.id = arc_next_out[e.id]; }
|
| 91 | 91 |
|
| 92 | 92 |
void firstIn(Arc& e, const Node& n) const { e.id = node_first_in[n.id]; }
|
| 93 | 93 |
void nextIn(Arc& e) const { e.id = arc_next_in[e.id]; }
|
| 94 | 94 |
|
| 95 | 95 |
static int id(const Node& n) { return n.id; }
|
| 96 | 96 |
static Node nodeFromId(int id) { return Node(id); }
|
| 97 | 97 |
int maxNodeId() const { return node_num - 1; }
|
| 98 | 98 |
|
| 99 | 99 |
static int id(const Arc& e) { return e.id; }
|
| 100 | 100 |
static Arc arcFromId(int id) { return Arc(id); }
|
| 101 | 101 |
int maxArcId() const { return arc_num - 1; }
|
| 102 | 102 |
|
| 103 | 103 |
typedef True NodeNumTag; |
| 104 | 104 |
typedef True ArcNumTag; |
| 105 | 105 |
|
| 106 | 106 |
int nodeNum() const { return node_num; }
|
| 107 | 107 |
int arcNum() const { return arc_num; }
|
| 108 | 108 |
|
| 109 | 109 |
private: |
| 110 | 110 |
|
| 111 | 111 |
template <typename Digraph, typename NodeRefMap> |
| 112 | 112 |
class ArcLess {
|
| 113 | 113 |
public: |
| 114 | 114 |
typedef typename Digraph::Arc Arc; |
| 115 | 115 |
|
| 116 |
ArcLess(const Digraph &_graph, const NodeRefMap& _nodeRef) |
|
| 116 |
ArcLess(const Digraph &_graph, const NodeRefMap& _nodeRef) |
|
| 117 | 117 |
: digraph(_graph), nodeRef(_nodeRef) {}
|
| 118 |
|
|
| 118 |
|
|
| 119 | 119 |
bool operator()(const Arc& left, const Arc& right) const {
|
| 120 |
|
|
| 120 |
return nodeRef[digraph.target(left)] < nodeRef[digraph.target(right)]; |
|
| 121 | 121 |
} |
| 122 | 122 |
private: |
| 123 | 123 |
const Digraph& digraph; |
| 124 | 124 |
const NodeRefMap& nodeRef; |
| 125 | 125 |
}; |
| 126 |
|
|
| 126 |
|
|
| 127 | 127 |
public: |
| 128 | 128 |
|
| 129 | 129 |
typedef True BuildTag; |
| 130 |
|
|
| 130 |
|
|
| 131 | 131 |
void clear() {
|
| 132 | 132 |
if (built) {
|
| 133 | 133 |
delete[] node_first_out; |
| 134 | 134 |
delete[] node_first_in; |
| 135 | 135 |
delete[] arc_source; |
| 136 | 136 |
delete[] arc_target; |
| 137 | 137 |
delete[] arc_next_out; |
| 138 | 138 |
delete[] arc_next_in; |
| 139 | 139 |
} |
| 140 | 140 |
built = false; |
| 141 | 141 |
node_num = 0; |
| 142 | 142 |
arc_num = 0; |
| 143 | 143 |
} |
| 144 |
|
|
| 144 |
|
|
| 145 | 145 |
template <typename Digraph, typename NodeRefMap, typename ArcRefMap> |
| 146 | 146 |
void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
|
| 147 | 147 |
typedef typename Digraph::Node GNode; |
| 148 | 148 |
typedef typename Digraph::Arc GArc; |
| 149 | 149 |
|
| 150 | 150 |
built = true; |
| 151 | 151 |
|
| 152 | 152 |
node_num = countNodes(digraph); |
| 153 | 153 |
arc_num = countArcs(digraph); |
| 154 | 154 |
|
| 155 | 155 |
node_first_out = new int[node_num + 1]; |
| 156 | 156 |
node_first_in = new int[node_num]; |
| 157 | 157 |
|
| 158 | 158 |
arc_source = new int[arc_num]; |
| 159 | 159 |
arc_target = new int[arc_num]; |
| 160 | 160 |
arc_next_out = new int[arc_num]; |
| 161 | 161 |
arc_next_in = new int[arc_num]; |
| 162 | 162 |
|
| 163 | 163 |
int node_index = 0; |
| 164 | 164 |
for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) {
|
| 165 | 165 |
nodeRef[n] = Node(node_index); |
| 166 | 166 |
node_first_in[node_index] = -1; |
| 167 | 167 |
++node_index; |
| 168 | 168 |
} |
| 169 | 169 |
|
| 170 | 170 |
ArcLess<Digraph, NodeRefMap> arcLess(digraph, nodeRef); |
| 171 | 171 |
|
| 172 | 172 |
int arc_index = 0; |
| 173 | 173 |
for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) {
|
| 174 | 174 |
int source = nodeRef[n].id; |
| 175 | 175 |
std::vector<GArc> arcs; |
| 176 | 176 |
for (typename Digraph::OutArcIt e(digraph, n); e != INVALID; ++e) {
|
| 177 | 177 |
arcs.push_back(e); |
| 178 | 178 |
} |
| 179 | 179 |
if (!arcs.empty()) {
|
| 180 | 180 |
node_first_out[source] = arc_index; |
| 181 | 181 |
std::sort(arcs.begin(), arcs.end(), arcLess); |
| 182 | 182 |
for (typename std::vector<GArc>::iterator it = arcs.begin(); |
| 183 | 183 |
it != arcs.end(); ++it) {
|
| 184 | 184 |
int target = nodeRef[digraph.target(*it)].id; |
| 185 | 185 |
arcRef[*it] = Arc(arc_index); |
| 186 |
arc_source[arc_index] = source; |
|
| 186 |
arc_source[arc_index] = source; |
|
| 187 | 187 |
arc_target[arc_index] = target; |
| 188 | 188 |
arc_next_in[arc_index] = node_first_in[target]; |
| 189 | 189 |
node_first_in[target] = arc_index; |
| 190 | 190 |
arc_next_out[arc_index] = arc_index + 1; |
| 191 | 191 |
++arc_index; |
| 192 | 192 |
} |
| 193 | 193 |
arc_next_out[arc_index - 1] = -1; |
| 194 | 194 |
} else {
|
| 195 | 195 |
node_first_out[source] = arc_index; |
| 196 | 196 |
} |
| 197 | 197 |
} |
| 198 | 198 |
node_first_out[node_num] = arc_num; |
| 199 | 199 |
} |
| 200 |
|
|
| 200 |
|
|
| 201 | 201 |
template <typename ArcListIterator> |
| 202 | 202 |
void build(int n, ArcListIterator first, ArcListIterator last) {
|
| 203 | 203 |
built = true; |
| 204 | 204 |
|
| 205 | 205 |
node_num = n; |
| 206 | 206 |
arc_num = std::distance(first, last); |
| 207 | 207 |
|
| 208 | 208 |
node_first_out = new int[node_num + 1]; |
| 209 | 209 |
node_first_in = new int[node_num]; |
| 210 | 210 |
|
| 211 | 211 |
arc_source = new int[arc_num]; |
| 212 | 212 |
arc_target = new int[arc_num]; |
| 213 | 213 |
arc_next_out = new int[arc_num]; |
| 214 | 214 |
arc_next_in = new int[arc_num]; |
| 215 |
|
|
| 215 |
|
|
| 216 | 216 |
for (int i = 0; i != node_num; ++i) {
|
| 217 | 217 |
node_first_in[i] = -1; |
| 218 |
} |
|
| 219 |
|
|
| 218 |
} |
|
| 219 |
|
|
| 220 | 220 |
int arc_index = 0; |
| 221 | 221 |
for (int i = 0; i != node_num; ++i) {
|
| 222 | 222 |
node_first_out[i] = arc_index; |
| 223 | 223 |
for ( ; first != last && (*first).first == i; ++first) {
|
| 224 | 224 |
int j = (*first).second; |
| 225 | 225 |
LEMON_ASSERT(j >= 0 && j < node_num, |
| 226 | 226 |
"Wrong arc list for StaticDigraph::build()"); |
| 227 | 227 |
arc_source[arc_index] = i; |
| 228 | 228 |
arc_target[arc_index] = j; |
| 229 | 229 |
arc_next_in[arc_index] = node_first_in[j]; |
| 230 | 230 |
node_first_in[j] = arc_index; |
| 231 | 231 |
arc_next_out[arc_index] = arc_index + 1; |
| 232 | 232 |
++arc_index; |
| 233 | 233 |
} |
| 234 | 234 |
if (arc_index > node_first_out[i]) |
| 235 | 235 |
arc_next_out[arc_index - 1] = -1; |
| 236 | 236 |
} |
| 237 | 237 |
LEMON_ASSERT(first == last, |
| 238 | 238 |
"Wrong arc list for StaticDigraph::build()"); |
| 239 | 239 |
node_first_out[node_num] = arc_num; |
| 240 | 240 |
} |
| 241 | 241 |
|
| 242 | 242 |
protected: |
| 243 | 243 |
|
| ... | ... |
@@ -261,137 +261,137 @@ |
| 261 | 261 |
int *arc_source; |
| 262 | 262 |
int *arc_target; |
| 263 | 263 |
int *arc_next_in; |
| 264 | 264 |
int *arc_next_out; |
| 265 | 265 |
}; |
| 266 | 266 |
|
| 267 | 267 |
typedef DigraphExtender<StaticDigraphBase> ExtendedStaticDigraphBase; |
| 268 | 268 |
|
| 269 | 269 |
|
| 270 | 270 |
/// \ingroup graphs |
| 271 | 271 |
/// |
| 272 | 272 |
/// \brief A static directed graph class. |
| 273 | 273 |
/// |
| 274 | 274 |
/// \ref StaticDigraph is a highly efficient digraph implementation, |
| 275 | 275 |
/// but it is fully static. |
| 276 | 276 |
/// It stores only two \c int values for each node and only four \c int |
| 277 | 277 |
/// values for each arc. Moreover it provides faster item iteration than |
| 278 | 278 |
/// \ref ListDigraph and \ref SmartDigraph, especially using \c OutArcIt |
| 279 | 279 |
/// iterators, since its arcs are stored in an appropriate order. |
| 280 | 280 |
/// However it only provides build() and clear() functions and does not |
| 281 | 281 |
/// support any other modification of the digraph. |
| 282 | 282 |
/// |
| 283 | 283 |
/// Since this digraph structure is completely static, its nodes and arcs |
| 284 | 284 |
/// can be indexed with integers from the ranges <tt>[0..nodeNum()-1]</tt> |
| 285 |
/// and <tt>[0..arcNum()-1]</tt>, respectively. |
|
| 285 |
/// and <tt>[0..arcNum()-1]</tt>, respectively. |
|
| 286 | 286 |
/// The index of an item is the same as its ID, it can be obtained |
| 287 | 287 |
/// using the corresponding \ref index() or \ref concepts::Digraph::id() |
| 288 | 288 |
/// "id()" function. A node or arc with a certain index can be obtained |
| 289 | 289 |
/// using node() or arc(). |
| 290 | 290 |
/// |
| 291 | 291 |
/// This type fully conforms to the \ref concepts::Digraph "Digraph concept". |
| 292 | 292 |
/// Most of its member functions and nested classes are documented |
| 293 | 293 |
/// only in the concept class. |
| 294 | 294 |
/// |
| 295 | 295 |
/// This class provides constant time counting for nodes and arcs. |
| 296 | 296 |
/// |
| 297 | 297 |
/// \sa concepts::Digraph |
| 298 | 298 |
class StaticDigraph : public ExtendedStaticDigraphBase {
|
| 299 | 299 |
public: |
| 300 | 300 |
|
| 301 | 301 |
typedef ExtendedStaticDigraphBase Parent; |
| 302 |
|
|
| 302 |
|
|
| 303 | 303 |
public: |
| 304 |
|
|
| 304 |
|
|
| 305 | 305 |
/// \brief Constructor |
| 306 | 306 |
/// |
| 307 | 307 |
/// Default constructor. |
| 308 | 308 |
StaticDigraph() : Parent() {}
|
| 309 | 309 |
|
| 310 | 310 |
/// \brief The node with the given index. |
| 311 | 311 |
/// |
| 312 | 312 |
/// This function returns the node with the given index. |
| 313 | 313 |
/// \sa index() |
| 314 | 314 |
static Node node(int ix) { return Parent::nodeFromId(ix); }
|
| 315 | 315 |
|
| 316 | 316 |
/// \brief The arc with the given index. |
| 317 | 317 |
/// |
| 318 | 318 |
/// This function returns the arc with the given index. |
| 319 | 319 |
/// \sa index() |
| 320 | 320 |
static Arc arc(int ix) { return Parent::arcFromId(ix); }
|
| 321 | 321 |
|
| 322 | 322 |
/// \brief The index of the given node. |
| 323 | 323 |
/// |
| 324 | 324 |
/// This function returns the index of the the given node. |
| 325 | 325 |
/// \sa node() |
| 326 | 326 |
static int index(Node node) { return Parent::id(node); }
|
| 327 | 327 |
|
| 328 | 328 |
/// \brief The index of the given arc. |
| 329 | 329 |
/// |
| 330 | 330 |
/// This function returns the index of the the given arc. |
| 331 | 331 |
/// \sa arc() |
| 332 | 332 |
static int index(Arc arc) { return Parent::id(arc); }
|
| 333 | 333 |
|
| 334 | 334 |
/// \brief Number of nodes. |
| 335 | 335 |
/// |
| 336 | 336 |
/// This function returns the number of nodes. |
| 337 | 337 |
int nodeNum() const { return node_num; }
|
| 338 | 338 |
|
| 339 | 339 |
/// \brief Number of arcs. |
| 340 | 340 |
/// |
| 341 | 341 |
/// This function returns the number of arcs. |
| 342 | 342 |
int arcNum() const { return arc_num; }
|
| 343 | 343 |
|
| 344 | 344 |
/// \brief Build the digraph copying another digraph. |
| 345 | 345 |
/// |
| 346 | 346 |
/// This function builds the digraph copying another digraph of any |
| 347 | 347 |
/// kind. It can be called more than once, but in such case, the whole |
| 348 | 348 |
/// structure and all maps will be cleared and rebuilt. |
| 349 | 349 |
/// |
| 350 | 350 |
/// This method also makes possible to copy a digraph to a StaticDigraph |
| 351 | 351 |
/// structure using \ref DigraphCopy. |
| 352 |
/// |
|
| 352 |
/// |
|
| 353 | 353 |
/// \param digraph An existing digraph to be copied. |
| 354 | 354 |
/// \param nodeRef The node references will be copied into this map. |
| 355 | 355 |
/// Its key type must be \c Digraph::Node and its value type must be |
| 356 | 356 |
/// \c StaticDigraph::Node. |
| 357 | 357 |
/// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" |
| 358 | 358 |
/// concept. |
| 359 | 359 |
/// \param arcRef The arc references will be copied into this map. |
| 360 | 360 |
/// Its key type must be \c Digraph::Arc and its value type must be |
| 361 | 361 |
/// \c StaticDigraph::Arc. |
| 362 | 362 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 363 | 363 |
/// |
| 364 | 364 |
/// \note If you do not need the arc references, then you could use |
| 365 | 365 |
/// \ref NullMap for the last parameter. However the node references |
| 366 | 366 |
/// are required by the function itself, thus they must be readable |
| 367 | 367 |
/// from the map. |
| 368 | 368 |
template <typename Digraph, typename NodeRefMap, typename ArcRefMap> |
| 369 | 369 |
void build(const Digraph& digraph, NodeRefMap& nodeRef, ArcRefMap& arcRef) {
|
| 370 | 370 |
if (built) Parent::clear(); |
| 371 | 371 |
Parent::build(digraph, nodeRef, arcRef); |
| 372 | 372 |
} |
| 373 |
|
|
| 373 |
|
|
| 374 | 374 |
/// \brief Build the digraph from an arc list. |
| 375 | 375 |
/// |
| 376 | 376 |
/// This function builds the digraph from the given arc list. |
| 377 | 377 |
/// It can be called more than once, but in such case, the whole |
| 378 | 378 |
/// structure and all maps will be cleared and rebuilt. |
| 379 | 379 |
/// |
| 380 | 380 |
/// The list of the arcs must be given in the range <tt>[begin, end)</tt> |
| 381 | 381 |
/// specified by STL compatible itartors whose \c value_type must be |
| 382 | 382 |
/// <tt>std::pair<int,int></tt>. |
| 383 | 383 |
/// Each arc must be specified by a pair of integer indices |
| 384 | 384 |
/// from the range <tt>[0..n-1]</tt>. <i>The pairs must be in a |
| 385 | 385 |
/// non-decreasing order with respect to their first values.</i> |
| 386 | 386 |
/// If the k-th pair in the list is <tt>(i,j)</tt>, then |
| 387 | 387 |
/// <tt>arc(k-1)</tt> will connect <tt>node(i)</tt> to <tt>node(j)</tt>. |
| 388 | 388 |
/// |
| 389 | 389 |
/// \param n The number of nodes. |
| 390 | 390 |
/// \param begin An iterator pointing to the beginning of the arc list. |
| 391 | 391 |
/// \param end An iterator pointing to the end of the arc list. |
| 392 | 392 |
/// |
| 393 | 393 |
/// For example, a simple digraph can be constructed like this. |
| 394 | 394 |
/// \code |
| 395 | 395 |
/// std::vector<std::pair<int,int> > arcs; |
| 396 | 396 |
/// arcs.push_back(std::make_pair(0,1)); |
| 397 | 397 |
/// arcs.push_back(std::make_pair(0,2)); |
| ... | ... |
@@ -400,74 +400,74 @@ |
| 400 | 400 |
/// arcs.push_back(std::make_pair(3,0)); |
| 401 | 401 |
/// StaticDigraph gr; |
| 402 | 402 |
/// gr.build(4, arcs.begin(), arcs.end()); |
| 403 | 403 |
/// \endcode |
| 404 | 404 |
template <typename ArcListIterator> |
| 405 | 405 |
void build(int n, ArcListIterator begin, ArcListIterator end) {
|
| 406 | 406 |
if (built) Parent::clear(); |
| 407 | 407 |
StaticDigraphBase::build(n, begin, end); |
| 408 | 408 |
notifier(Node()).build(); |
| 409 | 409 |
notifier(Arc()).build(); |
| 410 | 410 |
} |
| 411 | 411 |
|
| 412 | 412 |
/// \brief Clear the digraph. |
| 413 | 413 |
/// |
| 414 | 414 |
/// This function erases all nodes and arcs from the digraph. |
| 415 | 415 |
void clear() {
|
| 416 | 416 |
Parent::clear(); |
| 417 | 417 |
} |
| 418 | 418 |
|
| 419 | 419 |
protected: |
| 420 | 420 |
|
| 421 | 421 |
using Parent::fastFirstOut; |
| 422 | 422 |
using Parent::fastNextOut; |
| 423 | 423 |
using Parent::fastLastOut; |
| 424 |
|
|
| 424 |
|
|
| 425 | 425 |
public: |
| 426 | 426 |
|
| 427 | 427 |
class OutArcIt : public Arc {
|
| 428 | 428 |
public: |
| 429 | 429 |
|
| 430 | 430 |
OutArcIt() { }
|
| 431 | 431 |
|
| 432 | 432 |
OutArcIt(Invalid i) : Arc(i) { }
|
| 433 | 433 |
|
| 434 | 434 |
OutArcIt(const StaticDigraph& digraph, const Node& node) {
|
| 435 |
digraph.fastFirstOut(*this, node); |
|
| 436 |
digraph.fastLastOut(last, node); |
|
| 435 |
digraph.fastFirstOut(*this, node); |
|
| 436 |
digraph.fastLastOut(last, node); |
|
| 437 | 437 |
if (last == *this) *this = INVALID; |
| 438 | 438 |
} |
| 439 | 439 |
|
| 440 | 440 |
OutArcIt(const StaticDigraph& digraph, const Arc& arc) : Arc(arc) {
|
| 441 | 441 |
if (arc != INVALID) {
|
| 442 | 442 |
digraph.fastLastOut(last, digraph.source(arc)); |
| 443 | 443 |
} |
| 444 | 444 |
} |
| 445 | 445 |
|
| 446 |
OutArcIt& operator++() {
|
|
| 446 |
OutArcIt& operator++() {
|
|
| 447 | 447 |
StaticDigraph::fastNextOut(*this); |
| 448 | 448 |
if (last == *this) *this = INVALID; |
| 449 |
return *this; |
|
| 449 |
return *this; |
|
| 450 | 450 |
} |
| 451 | 451 |
|
| 452 | 452 |
private: |
| 453 | 453 |
Arc last; |
| 454 | 454 |
}; |
| 455 | 455 |
|
| 456 | 456 |
Node baseNode(const OutArcIt &arc) const {
|
| 457 | 457 |
return Parent::source(static_cast<const Arc&>(arc)); |
| 458 | 458 |
} |
| 459 | 459 |
|
| 460 | 460 |
Node runningNode(const OutArcIt &arc) const {
|
| 461 | 461 |
return Parent::target(static_cast<const Arc&>(arc)); |
| 462 | 462 |
} |
| 463 | 463 |
|
| 464 | 464 |
Node baseNode(const InArcIt &arc) const {
|
| 465 | 465 |
return Parent::target(static_cast<const Arc&>(arc)); |
| 466 | 466 |
} |
| 467 | 467 |
|
| 468 | 468 |
Node runningNode(const InArcIt &arc) const {
|
| 469 | 469 |
return Parent::source(static_cast<const Arc&>(arc)); |
| 470 | 470 |
} |
| 471 | 471 |
|
| 472 | 472 |
}; |
| 473 | 473 |
| 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_SUURBALLE_H |
| 20 | 20 |
#define LEMON_SUURBALLE_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup shortest_path |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief An algorithm for finding arc-disjoint paths between two |
| 25 | 25 |
/// nodes having minimum total length. |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
#include <lemon/bin_heap.h> |
| ... | ... |
@@ -44,49 +44,49 @@ |
| 44 | 44 |
template <typename GR, typename LEN> |
| 45 | 45 |
#else |
| 46 | 46 |
template < typename GR, |
| 47 | 47 |
typename LEN = typename GR::template ArcMap<int> > |
| 48 | 48 |
#endif |
| 49 | 49 |
struct SuurballeDefaultTraits |
| 50 | 50 |
{
|
| 51 | 51 |
/// The type of the digraph. |
| 52 | 52 |
typedef GR Digraph; |
| 53 | 53 |
/// The type of the length map. |
| 54 | 54 |
typedef LEN LengthMap; |
| 55 | 55 |
/// The type of the lengths. |
| 56 | 56 |
typedef typename LEN::Value Length; |
| 57 | 57 |
/// The type of the flow map. |
| 58 | 58 |
typedef typename GR::template ArcMap<int> FlowMap; |
| 59 | 59 |
/// The type of the potential map. |
| 60 | 60 |
typedef typename GR::template NodeMap<Length> PotentialMap; |
| 61 | 61 |
|
| 62 | 62 |
/// \brief The path type |
| 63 | 63 |
/// |
| 64 | 64 |
/// The type used for storing the found arc-disjoint paths. |
| 65 | 65 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 66 | 66 |
/// and it must have an \c addBack() function. |
| 67 | 67 |
typedef lemon::Path<Digraph> Path; |
| 68 |
|
|
| 68 |
|
|
| 69 | 69 |
/// The cross reference type used for the heap. |
| 70 | 70 |
typedef typename GR::template NodeMap<int> HeapCrossRef; |
| 71 | 71 |
|
| 72 | 72 |
/// \brief The heap type used for internal Dijkstra computations. |
| 73 | 73 |
/// |
| 74 | 74 |
/// The type of the heap used for internal Dijkstra computations. |
| 75 | 75 |
/// It must conform to the \ref lemon::concepts::Heap "Heap" concept |
| 76 | 76 |
/// and its priority type must be \c Length. |
| 77 | 77 |
typedef BinHeap<Length, HeapCrossRef> Heap; |
| 78 | 78 |
}; |
| 79 | 79 |
|
| 80 | 80 |
/// \addtogroup shortest_path |
| 81 | 81 |
/// @{
|
| 82 | 82 |
|
| 83 | 83 |
/// \brief Algorithm for finding arc-disjoint paths between two nodes |
| 84 | 84 |
/// having minimum total length. |
| 85 | 85 |
/// |
| 86 | 86 |
/// \ref lemon::Suurballe "Suurballe" implements an algorithm for |
| 87 | 87 |
/// finding arc-disjoint paths having minimum total length (cost) |
| 88 | 88 |
/// from a given source node to a given target node in a digraph. |
| 89 | 89 |
/// |
| 90 | 90 |
/// Note that this problem is a special case of the \ref min_cost_flow |
| 91 | 91 |
/// "minimum cost flow problem". This implementation is actually an |
| 92 | 92 |
/// efficient specialized version of the \ref CapacityScaling |
| ... | ... |
@@ -137,68 +137,68 @@ |
| 137 | 137 |
/// The heap type used for internal Dijkstra computations. |
| 138 | 138 |
typedef typename TR::Heap Heap; |
| 139 | 139 |
|
| 140 | 140 |
/// The \ref SuurballeDefaultTraits "traits class" of the algorithm. |
| 141 | 141 |
typedef TR Traits; |
| 142 | 142 |
|
| 143 | 143 |
private: |
| 144 | 144 |
|
| 145 | 145 |
// ResidualDijkstra is a special implementation of the |
| 146 | 146 |
// Dijkstra algorithm for finding shortest paths in the |
| 147 | 147 |
// residual network with respect to the reduced arc lengths |
| 148 | 148 |
// and modifying the node potentials according to the |
| 149 | 149 |
// distance of the nodes. |
| 150 | 150 |
class ResidualDijkstra |
| 151 | 151 |
{
|
| 152 | 152 |
private: |
| 153 | 153 |
|
| 154 | 154 |
const Digraph &_graph; |
| 155 | 155 |
const LengthMap &_length; |
| 156 | 156 |
const FlowMap &_flow; |
| 157 | 157 |
PotentialMap &_pi; |
| 158 | 158 |
PredMap &_pred; |
| 159 | 159 |
Node _s; |
| 160 | 160 |
Node _t; |
| 161 |
|
|
| 161 |
|
|
| 162 | 162 |
PotentialMap _dist; |
| 163 | 163 |
std::vector<Node> _proc_nodes; |
| 164 | 164 |
|
| 165 | 165 |
public: |
| 166 | 166 |
|
| 167 | 167 |
// Constructor |
| 168 | 168 |
ResidualDijkstra(Suurballe &srb) : |
| 169 | 169 |
_graph(srb._graph), _length(srb._length), |
| 170 |
_flow(*srb._flow), _pi(*srb._potential), _pred(srb._pred), |
|
| 170 |
_flow(*srb._flow), _pi(*srb._potential), _pred(srb._pred), |
|
| 171 | 171 |
_s(srb._s), _t(srb._t), _dist(_graph) {}
|
| 172 |
|
|
| 172 |
|
|
| 173 | 173 |
// Run the algorithm and return true if a path is found |
| 174 | 174 |
// from the source node to the target node. |
| 175 | 175 |
bool run(int cnt) {
|
| 176 | 176 |
return cnt == 0 ? startFirst() : start(); |
| 177 | 177 |
} |
| 178 | 178 |
|
| 179 | 179 |
private: |
| 180 |
|
|
| 180 |
|
|
| 181 | 181 |
// Execute the algorithm for the first time (the flow and potential |
| 182 | 182 |
// functions have to be identically zero). |
| 183 | 183 |
bool startFirst() {
|
| 184 | 184 |
HeapCrossRef heap_cross_ref(_graph, Heap::PRE_HEAP); |
| 185 | 185 |
Heap heap(heap_cross_ref); |
| 186 | 186 |
heap.push(_s, 0); |
| 187 | 187 |
_pred[_s] = INVALID; |
| 188 | 188 |
_proc_nodes.clear(); |
| 189 | 189 |
|
| 190 | 190 |
// Process nodes |
| 191 | 191 |
while (!heap.empty() && heap.top() != _t) {
|
| 192 | 192 |
Node u = heap.top(), v; |
| 193 | 193 |
Length d = heap.prio(), dn; |
| 194 | 194 |
_dist[u] = heap.prio(); |
| 195 | 195 |
_proc_nodes.push_back(u); |
| 196 | 196 |
heap.pop(); |
| 197 | 197 |
|
| 198 | 198 |
// Traverse outgoing arcs |
| 199 | 199 |
for (OutArcIt e(_graph, u); e != INVALID; ++e) {
|
| 200 | 200 |
v = _graph.target(e); |
| 201 | 201 |
switch(heap.state(v)) {
|
| 202 | 202 |
case Heap::PRE_HEAP: |
| 203 | 203 |
heap.push(v, d + _length[e]); |
| 204 | 204 |
_pred[v] = e; |
| ... | ... |
@@ -327,98 +327,98 @@ |
| 327 | 327 |
/// \ref named-templ-param "Named parameter" for setting |
| 328 | 328 |
/// \c PotentialMap type. |
| 329 | 329 |
template <typename T> |
| 330 | 330 |
struct SetPotentialMap |
| 331 | 331 |
: public Suurballe<GR, LEN, SetPotentialMapTraits<T> > {
|
| 332 | 332 |
typedef Suurballe<GR, LEN, SetPotentialMapTraits<T> > Create; |
| 333 | 333 |
}; |
| 334 | 334 |
|
| 335 | 335 |
template <typename T> |
| 336 | 336 |
struct SetPathTraits : public Traits {
|
| 337 | 337 |
typedef T Path; |
| 338 | 338 |
}; |
| 339 | 339 |
|
| 340 | 340 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 341 | 341 |
/// \c %Path type. |
| 342 | 342 |
/// |
| 343 | 343 |
/// \ref named-templ-param "Named parameter" for setting \c %Path type. |
| 344 | 344 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
| 345 | 345 |
/// and it must have an \c addBack() function. |
| 346 | 346 |
template <typename T> |
| 347 | 347 |
struct SetPath |
| 348 | 348 |
: public Suurballe<GR, LEN, SetPathTraits<T> > {
|
| 349 | 349 |
typedef Suurballe<GR, LEN, SetPathTraits<T> > Create; |
| 350 | 350 |
}; |
| 351 |
|
|
| 351 |
|
|
| 352 | 352 |
template <typename H, typename CR> |
| 353 | 353 |
struct SetHeapTraits : public Traits {
|
| 354 | 354 |
typedef H Heap; |
| 355 | 355 |
typedef CR HeapCrossRef; |
| 356 | 356 |
}; |
| 357 | 357 |
|
| 358 | 358 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 359 | 359 |
/// \c Heap and \c HeapCrossRef types. |
| 360 | 360 |
/// |
| 361 | 361 |
/// \ref named-templ-param "Named parameter" for setting \c Heap |
| 362 |
/// and \c HeapCrossRef types with automatic allocation. |
|
| 362 |
/// and \c HeapCrossRef types with automatic allocation. |
|
| 363 | 363 |
/// They will be used for internal Dijkstra computations. |
| 364 | 364 |
/// The heap type must conform to the \ref lemon::concepts::Heap "Heap" |
| 365 | 365 |
/// concept and its priority type must be \c Length. |
| 366 | 366 |
template <typename H, |
| 367 | 367 |
typename CR = typename Digraph::template NodeMap<int> > |
| 368 | 368 |
struct SetHeap |
| 369 | 369 |
: public Suurballe<GR, LEN, SetHeapTraits<H, CR> > {
|
| 370 | 370 |
typedef Suurballe<GR, LEN, SetHeapTraits<H, CR> > Create; |
| 371 | 371 |
}; |
| 372 | 372 |
|
| 373 | 373 |
/// @} |
| 374 | 374 |
|
| 375 | 375 |
private: |
| 376 | 376 |
|
| 377 | 377 |
// The digraph the algorithm runs on |
| 378 | 378 |
const Digraph &_graph; |
| 379 | 379 |
// The length map |
| 380 | 380 |
const LengthMap &_length; |
| 381 | 381 |
|
| 382 | 382 |
// Arc map of the current flow |
| 383 | 383 |
FlowMap *_flow; |
| 384 | 384 |
bool _local_flow; |
| 385 | 385 |
// Node map of the current potentials |
| 386 | 386 |
PotentialMap *_potential; |
| 387 | 387 |
bool _local_potential; |
| 388 | 388 |
|
| 389 | 389 |
// The source node |
| 390 | 390 |
Node _s; |
| 391 | 391 |
// The target node |
| 392 | 392 |
Node _t; |
| 393 | 393 |
|
| 394 | 394 |
// Container to store the found paths |
| 395 | 395 |
std::vector<Path> _paths; |
| 396 | 396 |
int _path_num; |
| 397 | 397 |
|
| 398 | 398 |
// The pred arc map |
| 399 | 399 |
PredMap _pred; |
| 400 |
|
|
| 400 |
|
|
| 401 | 401 |
// Data for full init |
| 402 | 402 |
PotentialMap *_init_dist; |
| 403 | 403 |
PredMap *_init_pred; |
| 404 | 404 |
bool _full_init; |
| 405 | 405 |
|
| 406 | 406 |
protected: |
| 407 | 407 |
|
| 408 | 408 |
Suurballe() {}
|
| 409 | 409 |
|
| 410 | 410 |
public: |
| 411 | 411 |
|
| 412 | 412 |
/// \brief Constructor. |
| 413 | 413 |
/// |
| 414 | 414 |
/// Constructor. |
| 415 | 415 |
/// |
| 416 | 416 |
/// \param graph The digraph the algorithm runs on. |
| 417 | 417 |
/// \param length The length (cost) values of the arcs. |
| 418 | 418 |
Suurballe( const Digraph &graph, |
| 419 | 419 |
const LengthMap &length ) : |
| 420 | 420 |
_graph(graph), _length(length), _flow(0), _local_flow(false), |
| 421 | 421 |
_potential(0), _local_potential(false), _pred(graph), |
| 422 | 422 |
_init_dist(0), _init_pred(0) |
| 423 | 423 |
{}
|
| 424 | 424 |
|
| ... | ... |
@@ -534,107 +534,107 @@ |
| 534 | 534 |
/// have to perform %Dijkstra only k-1 times. |
| 535 | 535 |
/// |
| 536 | 536 |
/// This initialization is usually worth using instead of \ref init() |
| 537 | 537 |
/// if the algorithm is executed many times using the same source node. |
| 538 | 538 |
/// |
| 539 | 539 |
/// \param s The source node. |
| 540 | 540 |
void fullInit(const Node& s) {
|
| 541 | 541 |
// Initialize maps |
| 542 | 542 |
init(s); |
| 543 | 543 |
if (!_init_dist) {
|
| 544 | 544 |
_init_dist = new PotentialMap(_graph); |
| 545 | 545 |
} |
| 546 | 546 |
if (!_init_pred) {
|
| 547 | 547 |
_init_pred = new PredMap(_graph); |
| 548 | 548 |
} |
| 549 | 549 |
|
| 550 | 550 |
// Run a full Dijkstra |
| 551 | 551 |
typename Dijkstra<Digraph, LengthMap> |
| 552 | 552 |
::template SetStandardHeap<Heap> |
| 553 | 553 |
::template SetDistMap<PotentialMap> |
| 554 | 554 |
::template SetPredMap<PredMap> |
| 555 | 555 |
::Create dijk(_graph, _length); |
| 556 | 556 |
dijk.distMap(*_init_dist).predMap(*_init_pred); |
| 557 | 557 |
dijk.run(s); |
| 558 |
|
|
| 558 |
|
|
| 559 | 559 |
_full_init = true; |
| 560 | 560 |
} |
| 561 | 561 |
|
| 562 | 562 |
/// \brief Execute the algorithm. |
| 563 | 563 |
/// |
| 564 | 564 |
/// This function executes the algorithm. |
| 565 | 565 |
/// |
| 566 | 566 |
/// \param t The target node. |
| 567 | 567 |
/// \param k The number of paths to be found. |
| 568 | 568 |
/// |
| 569 | 569 |
/// \return \c k if there are at least \c k arc-disjoint paths from |
| 570 | 570 |
/// \c s to \c t in the digraph. Otherwise it returns the number of |
| 571 | 571 |
/// arc-disjoint paths found. |
| 572 | 572 |
/// |
| 573 | 573 |
/// \note Apart from the return value, <tt>s.start(t, k)</tt> is |
| 574 | 574 |
/// just a shortcut of the following code. |
| 575 | 575 |
/// \code |
| 576 | 576 |
/// s.findFlow(t, k); |
| 577 | 577 |
/// s.findPaths(); |
| 578 | 578 |
/// \endcode |
| 579 | 579 |
int start(const Node& t, int k = 2) {
|
| 580 | 580 |
findFlow(t, k); |
| 581 | 581 |
findPaths(); |
| 582 | 582 |
return _path_num; |
| 583 | 583 |
} |
| 584 | 584 |
|
| 585 | 585 |
/// \brief Execute the algorithm to find an optimal flow. |
| 586 | 586 |
/// |
| 587 | 587 |
/// This function executes the successive shortest path algorithm to |
| 588 | 588 |
/// find a minimum cost flow, which is the union of \c k (or less) |
| 589 | 589 |
/// arc-disjoint paths. |
| 590 | 590 |
/// |
| 591 | 591 |
/// \param t The target node. |
| 592 | 592 |
/// \param k The number of paths to be found. |
| 593 | 593 |
/// |
| 594 | 594 |
/// \return \c k if there are at least \c k arc-disjoint paths from |
| 595 | 595 |
/// the source node to the given node \c t in the digraph. |
| 596 | 596 |
/// Otherwise it returns the number of arc-disjoint paths found. |
| 597 | 597 |
/// |
| 598 | 598 |
/// \pre \ref init() must be called before using this function. |
| 599 | 599 |
int findFlow(const Node& t, int k = 2) {
|
| 600 | 600 |
_t = t; |
| 601 | 601 |
ResidualDijkstra dijkstra(*this); |
| 602 |
|
|
| 602 |
|
|
| 603 | 603 |
// Initialization |
| 604 | 604 |
for (ArcIt e(_graph); e != INVALID; ++e) {
|
| 605 | 605 |
(*_flow)[e] = 0; |
| 606 | 606 |
} |
| 607 | 607 |
if (_full_init) {
|
| 608 | 608 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 609 | 609 |
(*_potential)[n] = (*_init_dist)[n]; |
| 610 | 610 |
} |
| 611 | 611 |
Node u = _t; |
| 612 | 612 |
Arc e; |
| 613 | 613 |
while ((e = (*_init_pred)[u]) != INVALID) {
|
| 614 | 614 |
(*_flow)[e] = 1; |
| 615 | 615 |
u = _graph.source(e); |
| 616 |
} |
|
| 616 |
} |
|
| 617 | 617 |
_path_num = 1; |
| 618 | 618 |
} else {
|
| 619 | 619 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 620 | 620 |
(*_potential)[n] = 0; |
| 621 | 621 |
} |
| 622 | 622 |
_path_num = 0; |
| 623 | 623 |
} |
| 624 | 624 |
|
| 625 | 625 |
// Find shortest paths |
| 626 | 626 |
while (_path_num < k) {
|
| 627 | 627 |
// Run Dijkstra |
| 628 | 628 |
if (!dijkstra.run(_path_num)) break; |
| 629 | 629 |
++_path_num; |
| 630 | 630 |
|
| 631 | 631 |
// Set the flow along the found shortest path |
| 632 | 632 |
Node u = _t; |
| 633 | 633 |
Arc e; |
| 634 | 634 |
while ((e = _pred[u]) != INVALID) {
|
| 635 | 635 |
if (u == _graph.target(e)) {
|
| 636 | 636 |
(*_flow)[e] = 1; |
| 637 | 637 |
u = _graph.source(e); |
| 638 | 638 |
} else {
|
| 639 | 639 |
(*_flow)[e] = 0; |
| 640 | 640 |
u = _graph.target(e); |
| 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_UNION_FIND_H |
| 20 | 20 |
#define LEMON_UNION_FIND_H |
| 21 | 21 |
|
| 22 | 22 |
//!\ingroup auxdat |
| 23 | 23 |
//!\file |
| 24 | 24 |
//!\brief Union-Find data structures. |
| 25 | 25 |
//! |
| 26 | 26 |
|
| 27 | 27 |
#include <vector> |
| 28 | 28 |
#include <list> |
| 29 | 29 |
#include <utility> |
| 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/concepts/digraph.h> |
| 20 | 20 |
#include <lemon/smart_graph.h> |
| 21 | 21 |
#include <lemon/list_graph.h> |
| 22 | 22 |
#include <lemon/lgf_reader.h> |
| 23 | 23 |
#include <lemon/bellman_ford.h> |
| 24 | 24 |
#include <lemon/path.h> |
| 25 | 25 |
|
| 26 | 26 |
#include "graph_test.h" |
| 27 | 27 |
#include "test_tools.h" |
| 28 | 28 |
|
| 29 | 29 |
using namespace lemon; |
| ... | ... |
@@ -76,62 +76,62 @@ |
| 76 | 76 |
BF bf_test(gr,length); |
| 77 | 77 |
const BF& const_bf_test = bf_test; |
| 78 | 78 |
|
| 79 | 79 |
bf_test.run(s); |
| 80 | 80 |
bf_test.run(s,k); |
| 81 | 81 |
|
| 82 | 82 |
bf_test.init(); |
| 83 | 83 |
bf_test.addSource(s); |
| 84 | 84 |
bf_test.addSource(s, 1); |
| 85 | 85 |
b = bf_test.processNextRound(); |
| 86 | 86 |
b = bf_test.processNextWeakRound(); |
| 87 | 87 |
|
| 88 | 88 |
bf_test.start(); |
| 89 | 89 |
bf_test.checkedStart(); |
| 90 | 90 |
bf_test.limitedStart(k); |
| 91 | 91 |
|
| 92 | 92 |
l = const_bf_test.dist(t); |
| 93 | 93 |
e = const_bf_test.predArc(t); |
| 94 | 94 |
s = const_bf_test.predNode(t); |
| 95 | 95 |
b = const_bf_test.reached(t); |
| 96 | 96 |
d = const_bf_test.distMap(); |
| 97 | 97 |
p = const_bf_test.predMap(); |
| 98 | 98 |
pp = const_bf_test.path(t); |
| 99 | 99 |
pp = const_bf_test.negativeCycle(); |
| 100 |
|
|
| 100 |
|
|
| 101 | 101 |
for (BF::ActiveIt it(const_bf_test); it != INVALID; ++it) {}
|
| 102 | 102 |
} |
| 103 | 103 |
{
|
| 104 | 104 |
BF::SetPredMap<concepts::ReadWriteMap<Node,Arc> > |
| 105 | 105 |
::SetDistMap<concepts::ReadWriteMap<Node,Value> > |
| 106 | 106 |
::SetOperationTraits<BellmanFordDefaultOperationTraits<Value> > |
| 107 | 107 |
::SetOperationTraits<BellmanFordToleranceOperationTraits<Value, 0> > |
| 108 | 108 |
::Create bf_test(gr,length); |
| 109 | 109 |
|
| 110 | 110 |
LengthMap length_map; |
| 111 | 111 |
concepts::ReadWriteMap<Node,Arc> pred_map; |
| 112 | 112 |
concepts::ReadWriteMap<Node,Value> dist_map; |
| 113 |
|
|
| 113 |
|
|
| 114 | 114 |
bf_test |
| 115 | 115 |
.lengthMap(length_map) |
| 116 | 116 |
.predMap(pred_map) |
| 117 | 117 |
.distMap(dist_map); |
| 118 | 118 |
|
| 119 | 119 |
bf_test.run(s); |
| 120 | 120 |
bf_test.run(s,k); |
| 121 | 121 |
|
| 122 | 122 |
bf_test.init(); |
| 123 | 123 |
bf_test.addSource(s); |
| 124 | 124 |
bf_test.addSource(s, 1); |
| 125 | 125 |
b = bf_test.processNextRound(); |
| 126 | 126 |
b = bf_test.processNextWeakRound(); |
| 127 | 127 |
|
| 128 | 128 |
bf_test.start(); |
| 129 | 129 |
bf_test.checkedStart(); |
| 130 | 130 |
bf_test.limitedStart(k); |
| 131 | 131 |
|
| 132 | 132 |
l = bf_test.dist(t); |
| 133 | 133 |
e = bf_test.predArc(t); |
| 134 | 134 |
s = bf_test.predNode(t); |
| 135 | 135 |
b = bf_test.reached(t); |
| 136 | 136 |
pp = bf_test.path(t); |
| 137 | 137 |
pp = bf_test.negativeCycle(); |
| ... | ... |
@@ -168,119 +168,119 @@ |
| 168 | 168 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 169 | 169 |
typedef typename Digraph::template ArcMap<Value> LengthMap; |
| 170 | 170 |
|
| 171 | 171 |
Digraph gr; |
| 172 | 172 |
Node s, t; |
| 173 | 173 |
LengthMap length(gr); |
| 174 | 174 |
|
| 175 | 175 |
std::istringstream input(test_lgf); |
| 176 | 176 |
digraphReader(gr, input). |
| 177 | 177 |
arcMap("length", length).
|
| 178 | 178 |
node("source", s).
|
| 179 | 179 |
node("target", t).
|
| 180 | 180 |
run(); |
| 181 | 181 |
|
| 182 | 182 |
BellmanFord<Digraph, LengthMap> |
| 183 | 183 |
bf(gr, length); |
| 184 | 184 |
bf.run(s); |
| 185 | 185 |
Path<Digraph> p = bf.path(t); |
| 186 | 186 |
|
| 187 | 187 |
check(bf.reached(t) && bf.dist(t) == -1, "Bellman-Ford found a wrong path."); |
| 188 | 188 |
check(p.length() == 3, "path() found a wrong path."); |
| 189 | 189 |
check(checkPath(gr, p), "path() found a wrong path."); |
| 190 | 190 |
check(pathSource(gr, p) == s, "path() found a wrong path."); |
| 191 | 191 |
check(pathTarget(gr, p) == t, "path() found a wrong path."); |
| 192 |
|
|
| 192 |
|
|
| 193 | 193 |
ListPath<Digraph> path; |
| 194 | 194 |
Value dist; |
| 195 | 195 |
bool reached = bellmanFord(gr,length).path(path).dist(dist).run(s,t); |
| 196 | 196 |
|
| 197 | 197 |
check(reached && dist == -1, "Bellman-Ford found a wrong path."); |
| 198 | 198 |
check(path.length() == 3, "path() found a wrong path."); |
| 199 | 199 |
check(checkPath(gr, path), "path() found a wrong path."); |
| 200 | 200 |
check(pathSource(gr, path) == s, "path() found a wrong path."); |
| 201 | 201 |
check(pathTarget(gr, path) == t, "path() found a wrong path."); |
| 202 | 202 |
|
| 203 | 203 |
for(ArcIt e(gr); e!=INVALID; ++e) {
|
| 204 | 204 |
Node u=gr.source(e); |
| 205 | 205 |
Node v=gr.target(e); |
| 206 | 206 |
check(!bf.reached(u) || (bf.dist(v) - bf.dist(u) <= length[e]), |
| 207 | 207 |
"Wrong output. dist(target)-dist(source)-arc_length=" << |
| 208 | 208 |
bf.dist(v) - bf.dist(u) - length[e]); |
| 209 | 209 |
} |
| 210 | 210 |
|
| 211 | 211 |
for(NodeIt v(gr); v!=INVALID; ++v) {
|
| 212 | 212 |
if (bf.reached(v)) {
|
| 213 | 213 |
check(v==s || bf.predArc(v)!=INVALID, "Wrong tree."); |
| 214 | 214 |
if (bf.predArc(v)!=INVALID ) {
|
| 215 | 215 |
Arc e=bf.predArc(v); |
| 216 | 216 |
Node u=gr.source(e); |
| 217 | 217 |
check(u==bf.predNode(v),"Wrong tree."); |
| 218 | 218 |
check(bf.dist(v) - bf.dist(u) == length[e], |
| 219 | 219 |
"Wrong distance! Difference: " << |
| 220 | 220 |
bf.dist(v) - bf.dist(u) - length[e]); |
| 221 | 221 |
} |
| 222 | 222 |
} |
| 223 | 223 |
} |
| 224 | 224 |
} |
| 225 | 225 |
|
| 226 | 226 |
void checkBellmanFordNegativeCycle() {
|
| 227 | 227 |
DIGRAPH_TYPEDEFS(SmartDigraph); |
| 228 | 228 |
|
| 229 | 229 |
SmartDigraph gr; |
| 230 | 230 |
IntArcMap length(gr); |
| 231 |
|
|
| 231 |
|
|
| 232 | 232 |
Node n1 = gr.addNode(); |
| 233 | 233 |
Node n2 = gr.addNode(); |
| 234 | 234 |
Node n3 = gr.addNode(); |
| 235 | 235 |
Node n4 = gr.addNode(); |
| 236 |
|
|
| 236 |
|
|
| 237 | 237 |
Arc a1 = gr.addArc(n1, n2); |
| 238 | 238 |
Arc a2 = gr.addArc(n2, n2); |
| 239 |
|
|
| 239 |
|
|
| 240 | 240 |
length[a1] = 2; |
| 241 | 241 |
length[a2] = -1; |
| 242 |
|
|
| 242 |
|
|
| 243 | 243 |
{
|
| 244 | 244 |
BellmanFord<SmartDigraph, IntArcMap> bf(gr, length); |
| 245 | 245 |
bf.run(n1); |
| 246 | 246 |
StaticPath<SmartDigraph> p = bf.negativeCycle(); |
| 247 | 247 |
check(p.length() == 1 && p.front() == p.back() && p.front() == a2, |
| 248 | 248 |
"Wrong negative cycle."); |
| 249 | 249 |
} |
| 250 |
|
|
| 250 |
|
|
| 251 | 251 |
length[a2] = 0; |
| 252 |
|
|
| 252 |
|
|
| 253 | 253 |
{
|
| 254 | 254 |
BellmanFord<SmartDigraph, IntArcMap> bf(gr, length); |
| 255 | 255 |
bf.run(n1); |
| 256 | 256 |
check(bf.negativeCycle().empty(), |
| 257 | 257 |
"Negative cycle should not be found."); |
| 258 | 258 |
} |
| 259 |
|
|
| 259 |
|
|
| 260 | 260 |
length[gr.addArc(n1, n3)] = 5; |
| 261 | 261 |
length[gr.addArc(n4, n3)] = 1; |
| 262 | 262 |
length[gr.addArc(n2, n4)] = 2; |
| 263 | 263 |
length[gr.addArc(n3, n2)] = -4; |
| 264 |
|
|
| 264 |
|
|
| 265 | 265 |
{
|
| 266 | 266 |
BellmanFord<SmartDigraph, IntArcMap> bf(gr, length); |
| 267 | 267 |
bf.init(); |
| 268 | 268 |
bf.addSource(n1); |
| 269 | 269 |
for (int i = 0; i < 4; ++i) {
|
| 270 | 270 |
check(bf.negativeCycle().empty(), |
| 271 | 271 |
"Negative cycle should not be found."); |
| 272 | 272 |
bf.processNextRound(); |
| 273 | 273 |
} |
| 274 | 274 |
StaticPath<SmartDigraph> p = bf.negativeCycle(); |
| 275 | 275 |
check(p.length() == 3, "Wrong negative cycle."); |
| 276 | 276 |
check(length[p.nth(0)] + length[p.nth(1)] + length[p.nth(2)] == -1, |
| 277 | 277 |
"Wrong negative cycle."); |
| 278 | 278 |
} |
| 279 | 279 |
} |
| 280 | 280 |
|
| 281 | 281 |
int main() {
|
| 282 | 282 |
checkBellmanFord<ListDigraph, int>(); |
| 283 | 283 |
checkBellmanFord<SmartDigraph, double>(); |
| 284 | 284 |
checkBellmanFordNegativeCycle(); |
| 285 | 285 |
return 0; |
| 286 | 286 |
} |
| 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/concepts/digraph.h> |
| 20 | 20 |
#include <lemon/smart_graph.h> |
| 21 | 21 |
#include <lemon/list_graph.h> |
| 22 | 22 |
#include <lemon/lgf_reader.h> |
| 23 | 23 |
#include <lemon/bfs.h> |
| 24 | 24 |
#include <lemon/path.h> |
| 25 | 25 |
|
| 26 | 26 |
#include "graph_test.h" |
| 27 | 27 |
#include "test_tools.h" |
| 28 | 28 |
|
| 29 | 29 |
using namespace lemon; |
| ... | ... |
@@ -62,94 +62,94 @@ |
| 62 | 62 |
Arc e; |
| 63 | 63 |
int l, i; |
| 64 | 64 |
bool b; |
| 65 | 65 |
BType::DistMap d(G); |
| 66 | 66 |
BType::PredMap p(G); |
| 67 | 67 |
Path<Digraph> pp; |
| 68 | 68 |
concepts::ReadMap<Node,bool> nm; |
| 69 | 69 |
|
| 70 | 70 |
{
|
| 71 | 71 |
BType bfs_test(G); |
| 72 | 72 |
const BType& const_bfs_test = bfs_test; |
| 73 | 73 |
|
| 74 | 74 |
bfs_test.run(s); |
| 75 | 75 |
bfs_test.run(s,t); |
| 76 | 76 |
bfs_test.run(); |
| 77 | 77 |
|
| 78 | 78 |
bfs_test.init(); |
| 79 | 79 |
bfs_test.addSource(s); |
| 80 | 80 |
n = bfs_test.processNextNode(); |
| 81 | 81 |
n = bfs_test.processNextNode(t, b); |
| 82 | 82 |
n = bfs_test.processNextNode(nm, n); |
| 83 | 83 |
n = const_bfs_test.nextNode(); |
| 84 | 84 |
b = const_bfs_test.emptyQueue(); |
| 85 | 85 |
i = const_bfs_test.queueSize(); |
| 86 |
|
|
| 86 |
|
|
| 87 | 87 |
bfs_test.start(); |
| 88 | 88 |
bfs_test.start(t); |
| 89 | 89 |
bfs_test.start(nm); |
| 90 | 90 |
|
| 91 | 91 |
l = const_bfs_test.dist(t); |
| 92 | 92 |
e = const_bfs_test.predArc(t); |
| 93 | 93 |
s = const_bfs_test.predNode(t); |
| 94 | 94 |
b = const_bfs_test.reached(t); |
| 95 | 95 |
d = const_bfs_test.distMap(); |
| 96 | 96 |
p = const_bfs_test.predMap(); |
| 97 | 97 |
pp = const_bfs_test.path(t); |
| 98 | 98 |
} |
| 99 | 99 |
{
|
| 100 | 100 |
BType |
| 101 | 101 |
::SetPredMap<concepts::ReadWriteMap<Node,Arc> > |
| 102 | 102 |
::SetDistMap<concepts::ReadWriteMap<Node,int> > |
| 103 | 103 |
::SetReachedMap<concepts::ReadWriteMap<Node,bool> > |
| 104 | 104 |
::SetStandardProcessedMap |
| 105 | 105 |
::SetProcessedMap<concepts::WriteMap<Node,bool> > |
| 106 | 106 |
::Create bfs_test(G); |
| 107 |
|
|
| 107 |
|
|
| 108 | 108 |
concepts::ReadWriteMap<Node,Arc> pred_map; |
| 109 | 109 |
concepts::ReadWriteMap<Node,int> dist_map; |
| 110 | 110 |
concepts::ReadWriteMap<Node,bool> reached_map; |
| 111 | 111 |
concepts::WriteMap<Node,bool> processed_map; |
| 112 |
|
|
| 112 |
|
|
| 113 | 113 |
bfs_test |
| 114 | 114 |
.predMap(pred_map) |
| 115 | 115 |
.distMap(dist_map) |
| 116 | 116 |
.reachedMap(reached_map) |
| 117 | 117 |
.processedMap(processed_map); |
| 118 | 118 |
|
| 119 | 119 |
bfs_test.run(s); |
| 120 | 120 |
bfs_test.run(s,t); |
| 121 | 121 |
bfs_test.run(); |
| 122 |
|
|
| 122 |
|
|
| 123 | 123 |
bfs_test.init(); |
| 124 | 124 |
bfs_test.addSource(s); |
| 125 | 125 |
n = bfs_test.processNextNode(); |
| 126 | 126 |
n = bfs_test.processNextNode(t, b); |
| 127 | 127 |
n = bfs_test.processNextNode(nm, n); |
| 128 | 128 |
n = bfs_test.nextNode(); |
| 129 | 129 |
b = bfs_test.emptyQueue(); |
| 130 | 130 |
i = bfs_test.queueSize(); |
| 131 |
|
|
| 131 |
|
|
| 132 | 132 |
bfs_test.start(); |
| 133 | 133 |
bfs_test.start(t); |
| 134 | 134 |
bfs_test.start(nm); |
| 135 | 135 |
|
| 136 | 136 |
l = bfs_test.dist(t); |
| 137 | 137 |
e = bfs_test.predArc(t); |
| 138 | 138 |
s = bfs_test.predNode(t); |
| 139 | 139 |
b = bfs_test.reached(t); |
| 140 | 140 |
pp = bfs_test.path(t); |
| 141 | 141 |
} |
| 142 | 142 |
} |
| 143 | 143 |
|
| 144 | 144 |
void checkBfsFunctionCompile() |
| 145 | 145 |
{
|
| 146 | 146 |
typedef int VType; |
| 147 | 147 |
typedef concepts::Digraph Digraph; |
| 148 | 148 |
typedef Digraph::Arc Arc; |
| 149 | 149 |
typedef Digraph::Node Node; |
| 150 | 150 |
|
| 151 | 151 |
Digraph g; |
| 152 | 152 |
bool b; |
| 153 | 153 |
bfs(g).run(Node()); |
| 154 | 154 |
b=bfs(g).run(Node(),Node()); |
| 155 | 155 |
bfs(g).run(); |
| 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 <iostream> |
| 20 | 20 |
|
| 21 | 21 |
#include "test_tools.h" |
| 22 | 22 |
#include <lemon/list_graph.h> |
| 23 | 23 |
#include <lemon/circulation.h> |
| 24 | 24 |
#include <lemon/lgf_reader.h> |
| 25 | 25 |
#include <lemon/concepts/digraph.h> |
| 26 | 26 |
#include <lemon/concepts/maps.h> |
| 27 | 27 |
|
| 28 | 28 |
using namespace lemon; |
| 29 | 29 |
|
| ... | ... |
@@ -60,70 +60,70 @@ |
| 60 | 60 |
typedef concepts::ReadMap<Node,VType> SupplyMap; |
| 61 | 61 |
typedef concepts::ReadWriteMap<Arc,VType> FlowMap; |
| 62 | 62 |
typedef concepts::WriteMap<Node,bool> BarrierMap; |
| 63 | 63 |
|
| 64 | 64 |
typedef Elevator<Digraph, Digraph::Node> Elev; |
| 65 | 65 |
typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev; |
| 66 | 66 |
|
| 67 | 67 |
Digraph g; |
| 68 | 68 |
Node n; |
| 69 | 69 |
Arc a; |
| 70 | 70 |
CapMap lcap, ucap; |
| 71 | 71 |
SupplyMap supply; |
| 72 | 72 |
FlowMap flow; |
| 73 | 73 |
BarrierMap bar; |
| 74 | 74 |
VType v; |
| 75 | 75 |
bool b; |
| 76 | 76 |
|
| 77 | 77 |
typedef Circulation<Digraph, CapMap, CapMap, SupplyMap> |
| 78 | 78 |
::SetFlowMap<FlowMap> |
| 79 | 79 |
::SetElevator<Elev> |
| 80 | 80 |
::SetStandardElevator<LinkedElev> |
| 81 | 81 |
::Create CirculationType; |
| 82 | 82 |
CirculationType circ_test(g, lcap, ucap, supply); |
| 83 | 83 |
const CirculationType& const_circ_test = circ_test; |
| 84 |
|
|
| 84 |
|
|
| 85 | 85 |
circ_test |
| 86 | 86 |
.lowerMap(lcap) |
| 87 | 87 |
.upperMap(ucap) |
| 88 | 88 |
.supplyMap(supply) |
| 89 | 89 |
.flowMap(flow); |
| 90 |
|
|
| 90 |
|
|
| 91 | 91 |
const CirculationType::Elevator& elev = const_circ_test.elevator(); |
| 92 | 92 |
circ_test.elevator(const_cast<CirculationType::Elevator&>(elev)); |
| 93 | 93 |
CirculationType::Tolerance tol = const_circ_test.tolerance(); |
| 94 | 94 |
circ_test.tolerance(tol); |
| 95 | 95 |
|
| 96 | 96 |
circ_test.init(); |
| 97 | 97 |
circ_test.greedyInit(); |
| 98 | 98 |
circ_test.start(); |
| 99 | 99 |
circ_test.run(); |
| 100 | 100 |
|
| 101 | 101 |
v = const_circ_test.flow(a); |
| 102 | 102 |
const FlowMap& fm = const_circ_test.flowMap(); |
| 103 | 103 |
b = const_circ_test.barrier(n); |
| 104 | 104 |
const_circ_test.barrierMap(bar); |
| 105 |
|
|
| 105 |
|
|
| 106 | 106 |
ignore_unused_variable_warning(fm); |
| 107 | 107 |
} |
| 108 | 108 |
|
| 109 | 109 |
template <class G, class LM, class UM, class DM> |
| 110 | 110 |
void checkCirculation(const G& g, const LM& lm, const UM& um, |
| 111 | 111 |
const DM& dm, bool find) |
| 112 | 112 |
{
|
| 113 | 113 |
Circulation<G, LM, UM, DM> circ(g, lm, um, dm); |
| 114 | 114 |
bool ret = circ.run(); |
| 115 | 115 |
if (find) {
|
| 116 | 116 |
check(ret, "A feasible solution should have been found."); |
| 117 | 117 |
check(circ.checkFlow(), "The found flow is corrupt."); |
| 118 | 118 |
check(!circ.checkBarrier(), "A barrier should not have been found."); |
| 119 | 119 |
} else {
|
| 120 | 120 |
check(!ret, "A feasible solution should not have been found."); |
| 121 | 121 |
check(circ.checkBarrier(), "The found barrier is corrupt."); |
| 122 | 122 |
} |
| 123 | 123 |
} |
| 124 | 124 |
|
| 125 | 125 |
int main (int, char*[]) |
| 126 | 126 |
{
|
| 127 | 127 |
typedef ListDigraph Digraph; |
| 128 | 128 |
DIGRAPH_TYPEDEFS(Digraph); |
| 129 | 129 |
| 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/connectivity.h> |
| 20 | 20 |
#include <lemon/list_graph.h> |
| 21 | 21 |
#include <lemon/adaptors.h> |
| 22 | 22 |
|
| 23 | 23 |
#include "test_tools.h" |
| 24 | 24 |
|
| 25 | 25 |
using namespace lemon; |
| 26 | 26 |
|
| 27 | 27 |
|
| 28 | 28 |
int main() |
| 29 | 29 |
{
|
| 30 | 30 |
typedef ListDigraph Digraph; |
| 31 | 31 |
typedef Undirector<Digraph> Graph; |
| 32 |
|
|
| 32 |
|
|
| 33 | 33 |
{
|
| 34 | 34 |
Digraph d; |
| 35 | 35 |
Digraph::NodeMap<int> order(d); |
| 36 | 36 |
Graph g(d); |
| 37 |
|
|
| 37 |
|
|
| 38 | 38 |
check(stronglyConnected(d), "The empty digraph is strongly connected"); |
| 39 | 39 |
check(countStronglyConnectedComponents(d) == 0, |
| 40 | 40 |
"The empty digraph has 0 strongly connected component"); |
| 41 | 41 |
check(connected(g), "The empty graph is connected"); |
| 42 | 42 |
check(countConnectedComponents(g) == 0, |
| 43 | 43 |
"The empty graph has 0 connected component"); |
| 44 | 44 |
|
| 45 | 45 |
check(biNodeConnected(g), "The empty graph is bi-node-connected"); |
| 46 | 46 |
check(countBiNodeConnectedComponents(g) == 0, |
| 47 | 47 |
"The empty graph has 0 bi-node-connected component"); |
| 48 | 48 |
check(biEdgeConnected(g), "The empty graph is bi-edge-connected"); |
| 49 | 49 |
check(countBiEdgeConnectedComponents(g) == 0, |
| 50 | 50 |
"The empty graph has 0 bi-edge-connected component"); |
| 51 |
|
|
| 51 |
|
|
| 52 | 52 |
check(dag(d), "The empty digraph is DAG."); |
| 53 | 53 |
check(checkedTopologicalSort(d, order), "The empty digraph is DAG."); |
| 54 | 54 |
check(loopFree(d), "The empty digraph is loop-free."); |
| 55 | 55 |
check(parallelFree(d), "The empty digraph is parallel-free."); |
| 56 | 56 |
check(simpleGraph(d), "The empty digraph is simple."); |
| 57 | 57 |
|
| 58 | 58 |
check(acyclic(g), "The empty graph is acyclic."); |
| 59 | 59 |
check(tree(g), "The empty graph is tree."); |
| 60 | 60 |
check(bipartite(g), "The empty graph is bipartite."); |
| 61 | 61 |
check(loopFree(g), "The empty graph is loop-free."); |
| 62 | 62 |
check(parallelFree(g), "The empty graph is parallel-free."); |
| 63 | 63 |
check(simpleGraph(g), "The empty graph is simple."); |
| 64 | 64 |
} |
| 65 | 65 |
|
| 66 | 66 |
{
|
| 67 | 67 |
Digraph d; |
| 68 | 68 |
Digraph::NodeMap<int> order(d); |
| 69 | 69 |
Graph g(d); |
| 70 | 70 |
Digraph::Node n = d.addNode(); |
| 71 | 71 |
|
| 72 | 72 |
check(stronglyConnected(d), "This digraph is strongly connected"); |
| 73 | 73 |
check(countStronglyConnectedComponents(d) == 1, |
| 74 | 74 |
"This digraph has 1 strongly connected component"); |
| 75 | 75 |
check(connected(g), "This graph is connected"); |
| 76 | 76 |
check(countConnectedComponents(g) == 1, |
| 77 | 77 |
"This graph has 1 connected component"); |
| 78 | 78 |
|
| 79 | 79 |
check(biNodeConnected(g), "This graph is bi-node-connected"); |
| 80 | 80 |
check(countBiNodeConnectedComponents(g) == 0, |
| 81 | 81 |
"This graph has 0 bi-node-connected component"); |
| 82 | 82 |
check(biEdgeConnected(g), "This graph is bi-edge-connected"); |
| 83 | 83 |
check(countBiEdgeConnectedComponents(g) == 1, |
| 84 | 84 |
"This graph has 1 bi-edge-connected component"); |
| 85 |
|
|
| 85 |
|
|
| 86 | 86 |
check(dag(d), "This digraph is DAG."); |
| 87 | 87 |
check(checkedTopologicalSort(d, order), "This digraph is DAG."); |
| 88 | 88 |
check(loopFree(d), "This digraph is loop-free."); |
| 89 | 89 |
check(parallelFree(d), "This digraph is parallel-free."); |
| 90 | 90 |
check(simpleGraph(d), "This digraph is simple."); |
| 91 | 91 |
|
| 92 | 92 |
check(acyclic(g), "This graph is acyclic."); |
| 93 | 93 |
check(tree(g), "This graph is tree."); |
| 94 | 94 |
check(bipartite(g), "This graph is bipartite."); |
| 95 | 95 |
check(loopFree(g), "This graph is loop-free."); |
| 96 | 96 |
check(parallelFree(g), "This graph is parallel-free."); |
| 97 | 97 |
check(simpleGraph(g), "This graph is simple."); |
| 98 | 98 |
} |
| 99 | 99 |
|
| 100 | 100 |
{
|
| 101 | 101 |
Digraph d; |
| 102 | 102 |
Digraph::NodeMap<int> order(d); |
| 103 | 103 |
Graph g(d); |
| 104 |
|
|
| 104 |
|
|
| 105 | 105 |
Digraph::Node n1 = d.addNode(); |
| 106 | 106 |
Digraph::Node n2 = d.addNode(); |
| 107 | 107 |
Digraph::Node n3 = d.addNode(); |
| 108 | 108 |
Digraph::Node n4 = d.addNode(); |
| 109 | 109 |
Digraph::Node n5 = d.addNode(); |
| 110 | 110 |
Digraph::Node n6 = d.addNode(); |
| 111 |
|
|
| 111 |
|
|
| 112 | 112 |
d.addArc(n1, n3); |
| 113 | 113 |
d.addArc(n3, n2); |
| 114 | 114 |
d.addArc(n2, n1); |
| 115 | 115 |
d.addArc(n4, n2); |
| 116 | 116 |
d.addArc(n4, n3); |
| 117 | 117 |
d.addArc(n5, n6); |
| 118 | 118 |
d.addArc(n6, n5); |
| 119 | 119 |
|
| 120 | 120 |
check(!stronglyConnected(d), "This digraph is not strongly connected"); |
| 121 | 121 |
check(countStronglyConnectedComponents(d) == 3, |
| 122 | 122 |
"This digraph has 3 strongly connected components"); |
| 123 | 123 |
check(!connected(g), "This graph is not connected"); |
| 124 | 124 |
check(countConnectedComponents(g) == 2, |
| 125 | 125 |
"This graph has 2 connected components"); |
| 126 | 126 |
|
| 127 | 127 |
check(!dag(d), "This digraph is not DAG."); |
| 128 | 128 |
check(!checkedTopologicalSort(d, order), "This digraph is not DAG."); |
| 129 | 129 |
check(loopFree(d), "This digraph is loop-free."); |
| 130 | 130 |
check(parallelFree(d), "This digraph is parallel-free."); |
| 131 | 131 |
check(simpleGraph(d), "This digraph is simple."); |
| 132 | 132 |
|
| 133 | 133 |
check(!acyclic(g), "This graph is not acyclic."); |
| 134 | 134 |
check(!tree(g), "This graph is not tree."); |
| 135 | 135 |
check(!bipartite(g), "This graph is not bipartite."); |
| 136 | 136 |
check(loopFree(g), "This graph is loop-free."); |
| 137 | 137 |
check(!parallelFree(g), "This graph is not parallel-free."); |
| 138 | 138 |
check(!simpleGraph(g), "This graph is not simple."); |
| 139 |
|
|
| 139 |
|
|
| 140 | 140 |
d.addArc(n3, n3); |
| 141 |
|
|
| 141 |
|
|
| 142 | 142 |
check(!loopFree(d), "This digraph is not loop-free."); |
| 143 | 143 |
check(!loopFree(g), "This graph is not loop-free."); |
| 144 | 144 |
check(!simpleGraph(d), "This digraph is not simple."); |
| 145 |
|
|
| 145 |
|
|
| 146 | 146 |
d.addArc(n3, n2); |
| 147 |
|
|
| 147 |
|
|
| 148 | 148 |
check(!parallelFree(d), "This digraph is not parallel-free."); |
| 149 | 149 |
} |
| 150 |
|
|
| 150 |
|
|
| 151 | 151 |
{
|
| 152 | 152 |
Digraph d; |
| 153 | 153 |
Digraph::ArcMap<bool> cutarcs(d, false); |
| 154 | 154 |
Graph g(d); |
| 155 |
|
|
| 155 |
|
|
| 156 | 156 |
Digraph::Node n1 = d.addNode(); |
| 157 | 157 |
Digraph::Node n2 = d.addNode(); |
| 158 | 158 |
Digraph::Node n3 = d.addNode(); |
| 159 | 159 |
Digraph::Node n4 = d.addNode(); |
| 160 | 160 |
Digraph::Node n5 = d.addNode(); |
| 161 | 161 |
Digraph::Node n6 = d.addNode(); |
| 162 | 162 |
Digraph::Node n7 = d.addNode(); |
| 163 | 163 |
Digraph::Node n8 = d.addNode(); |
| 164 | 164 |
|
| 165 | 165 |
d.addArc(n1, n2); |
| 166 | 166 |
d.addArc(n5, n1); |
| 167 | 167 |
d.addArc(n2, n8); |
| 168 | 168 |
d.addArc(n8, n5); |
| 169 | 169 |
d.addArc(n6, n4); |
| 170 | 170 |
d.addArc(n4, n6); |
| 171 | 171 |
d.addArc(n2, n5); |
| 172 | 172 |
d.addArc(n1, n8); |
| 173 | 173 |
d.addArc(n6, n7); |
| 174 | 174 |
d.addArc(n7, n6); |
| 175 |
|
|
| 175 |
|
|
| 176 | 176 |
check(!stronglyConnected(d), "This digraph is not strongly connected"); |
| 177 | 177 |
check(countStronglyConnectedComponents(d) == 3, |
| 178 | 178 |
"This digraph has 3 strongly connected components"); |
| 179 | 179 |
Digraph::NodeMap<int> scomp1(d); |
| 180 | 180 |
check(stronglyConnectedComponents(d, scomp1) == 3, |
| 181 | 181 |
"This digraph has 3 strongly connected components"); |
| 182 | 182 |
check(scomp1[n1] != scomp1[n3] && scomp1[n1] != scomp1[n4] && |
| 183 | 183 |
scomp1[n3] != scomp1[n4], "Wrong stronglyConnectedComponents()"); |
| 184 | 184 |
check(scomp1[n1] == scomp1[n2] && scomp1[n1] == scomp1[n5] && |
| 185 | 185 |
scomp1[n1] == scomp1[n8], "Wrong stronglyConnectedComponents()"); |
| 186 | 186 |
check(scomp1[n4] == scomp1[n6] && scomp1[n4] == scomp1[n7], |
| 187 | 187 |
"Wrong stronglyConnectedComponents()"); |
| 188 | 188 |
Digraph::ArcMap<bool> scut1(d, false); |
| 189 | 189 |
check(stronglyConnectedCutArcs(d, scut1) == 0, |
| 190 | 190 |
"This digraph has 0 strongly connected cut arc."); |
| 191 | 191 |
for (Digraph::ArcIt a(d); a != INVALID; ++a) {
|
| 192 | 192 |
check(!scut1[a], "Wrong stronglyConnectedCutArcs()"); |
| 193 | 193 |
} |
| 194 | 194 |
|
| 195 | 195 |
check(!connected(g), "This graph is not connected"); |
| 196 | 196 |
check(countConnectedComponents(g) == 3, |
| 197 | 197 |
"This graph has 3 connected components"); |
| 198 | 198 |
Graph::NodeMap<int> comp(g); |
| 199 | 199 |
check(connectedComponents(g, comp) == 3, |
| ... | ... |
@@ -214,84 +214,84 @@ |
| 214 | 214 |
check(!stronglyConnected(d), "This digraph is not strongly connected"); |
| 215 | 215 |
check(countStronglyConnectedComponents(d) == 3, |
| 216 | 216 |
"This digraph has 3 strongly connected components"); |
| 217 | 217 |
Digraph::NodeMap<int> scomp2(d); |
| 218 | 218 |
check(stronglyConnectedComponents(d, scomp2) == 3, |
| 219 | 219 |
"This digraph has 3 strongly connected components"); |
| 220 | 220 |
check(scomp2[n3] == 0, "Wrong stronglyConnectedComponents()"); |
| 221 | 221 |
check(scomp2[n1] == 1 && scomp2[n2] == 1 && scomp2[n5] == 1 && |
| 222 | 222 |
scomp2[n8] == 1, "Wrong stronglyConnectedComponents()"); |
| 223 | 223 |
check(scomp2[n4] == 2 && scomp2[n6] == 2 && scomp2[n7] == 2, |
| 224 | 224 |
"Wrong stronglyConnectedComponents()"); |
| 225 | 225 |
Digraph::ArcMap<bool> scut2(d, false); |
| 226 | 226 |
check(stronglyConnectedCutArcs(d, scut2) == 5, |
| 227 | 227 |
"This digraph has 5 strongly connected cut arcs."); |
| 228 | 228 |
for (Digraph::ArcIt a(d); a != INVALID; ++a) {
|
| 229 | 229 |
check(scut2[a] == cutarcs[a], "Wrong stronglyConnectedCutArcs()"); |
| 230 | 230 |
} |
| 231 | 231 |
} |
| 232 | 232 |
|
| 233 | 233 |
{
|
| 234 | 234 |
// DAG example for topological sort from the book New Algorithms |
| 235 | 235 |
// (T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein) |
| 236 | 236 |
Digraph d; |
| 237 | 237 |
Digraph::NodeMap<int> order(d); |
| 238 |
|
|
| 238 |
|
|
| 239 | 239 |
Digraph::Node belt = d.addNode(); |
| 240 | 240 |
Digraph::Node trousers = d.addNode(); |
| 241 | 241 |
Digraph::Node necktie = d.addNode(); |
| 242 | 242 |
Digraph::Node coat = d.addNode(); |
| 243 | 243 |
Digraph::Node socks = d.addNode(); |
| 244 | 244 |
Digraph::Node shirt = d.addNode(); |
| 245 | 245 |
Digraph::Node shoe = d.addNode(); |
| 246 | 246 |
Digraph::Node watch = d.addNode(); |
| 247 | 247 |
Digraph::Node pants = d.addNode(); |
| 248 | 248 |
|
| 249 | 249 |
d.addArc(socks, shoe); |
| 250 | 250 |
d.addArc(pants, shoe); |
| 251 | 251 |
d.addArc(pants, trousers); |
| 252 | 252 |
d.addArc(trousers, shoe); |
| 253 | 253 |
d.addArc(trousers, belt); |
| 254 | 254 |
d.addArc(belt, coat); |
| 255 | 255 |
d.addArc(shirt, belt); |
| 256 | 256 |
d.addArc(shirt, necktie); |
| 257 | 257 |
d.addArc(necktie, coat); |
| 258 |
|
|
| 258 |
|
|
| 259 | 259 |
check(dag(d), "This digraph is DAG."); |
| 260 | 260 |
topologicalSort(d, order); |
| 261 | 261 |
for (Digraph::ArcIt a(d); a != INVALID; ++a) {
|
| 262 | 262 |
check(order[d.source(a)] < order[d.target(a)], |
| 263 | 263 |
"Wrong topologicalSort()"); |
| 264 | 264 |
} |
| 265 | 265 |
} |
| 266 | 266 |
|
| 267 | 267 |
{
|
| 268 | 268 |
ListGraph g; |
| 269 | 269 |
ListGraph::NodeMap<bool> map(g); |
| 270 |
|
|
| 270 |
|
|
| 271 | 271 |
ListGraph::Node n1 = g.addNode(); |
| 272 | 272 |
ListGraph::Node n2 = g.addNode(); |
| 273 | 273 |
ListGraph::Node n3 = g.addNode(); |
| 274 | 274 |
ListGraph::Node n4 = g.addNode(); |
| 275 | 275 |
ListGraph::Node n5 = g.addNode(); |
| 276 | 276 |
ListGraph::Node n6 = g.addNode(); |
| 277 | 277 |
ListGraph::Node n7 = g.addNode(); |
| 278 | 278 |
|
| 279 | 279 |
g.addEdge(n1, n3); |
| 280 | 280 |
g.addEdge(n1, n4); |
| 281 | 281 |
g.addEdge(n2, n5); |
| 282 | 282 |
g.addEdge(n3, n6); |
| 283 | 283 |
g.addEdge(n4, n6); |
| 284 | 284 |
g.addEdge(n4, n7); |
| 285 | 285 |
g.addEdge(n5, n7); |
| 286 |
|
|
| 286 |
|
|
| 287 | 287 |
check(bipartite(g), "This graph is bipartite"); |
| 288 | 288 |
check(bipartitePartitions(g, map), "This graph is bipartite"); |
| 289 |
|
|
| 289 |
|
|
| 290 | 290 |
check(map[n1] == map[n2] && map[n1] == map[n6] && map[n1] == map[n7], |
| 291 | 291 |
"Wrong bipartitePartitions()"); |
| 292 | 292 |
check(map[n3] == map[n4] && map[n3] == map[n5], |
| 293 | 293 |
"Wrong bipartitePartitions()"); |
| 294 | 294 |
} |
| 295 | 295 |
|
| 296 | 296 |
return 0; |
| 297 | 297 |
} |
| 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/concepts/digraph.h> |
| 20 | 20 |
#include <lemon/smart_graph.h> |
| 21 | 21 |
#include <lemon/list_graph.h> |
| 22 | 22 |
#include <lemon/lgf_reader.h> |
| 23 | 23 |
#include <lemon/dfs.h> |
| 24 | 24 |
#include <lemon/path.h> |
| 25 | 25 |
|
| 26 | 26 |
#include "graph_test.h" |
| 27 | 27 |
#include "test_tools.h" |
| 28 | 28 |
|
| 29 | 29 |
using namespace lemon; |
| ... | ... |
@@ -62,92 +62,92 @@ |
| 62 | 62 |
Digraph G; |
| 63 | 63 |
Node s, t; |
| 64 | 64 |
Arc e; |
| 65 | 65 |
int l, i; |
| 66 | 66 |
bool b; |
| 67 | 67 |
DType::DistMap d(G); |
| 68 | 68 |
DType::PredMap p(G); |
| 69 | 69 |
Path<Digraph> pp; |
| 70 | 70 |
concepts::ReadMap<Arc,bool> am; |
| 71 | 71 |
|
| 72 | 72 |
{
|
| 73 | 73 |
DType dfs_test(G); |
| 74 | 74 |
const DType& const_dfs_test = dfs_test; |
| 75 | 75 |
|
| 76 | 76 |
dfs_test.run(s); |
| 77 | 77 |
dfs_test.run(s,t); |
| 78 | 78 |
dfs_test.run(); |
| 79 | 79 |
|
| 80 | 80 |
dfs_test.init(); |
| 81 | 81 |
dfs_test.addSource(s); |
| 82 | 82 |
e = dfs_test.processNextArc(); |
| 83 | 83 |
e = const_dfs_test.nextArc(); |
| 84 | 84 |
b = const_dfs_test.emptyQueue(); |
| 85 | 85 |
i = const_dfs_test.queueSize(); |
| 86 |
|
|
| 86 |
|
|
| 87 | 87 |
dfs_test.start(); |
| 88 | 88 |
dfs_test.start(t); |
| 89 | 89 |
dfs_test.start(am); |
| 90 | 90 |
|
| 91 | 91 |
l = const_dfs_test.dist(t); |
| 92 | 92 |
e = const_dfs_test.predArc(t); |
| 93 | 93 |
s = const_dfs_test.predNode(t); |
| 94 | 94 |
b = const_dfs_test.reached(t); |
| 95 | 95 |
d = const_dfs_test.distMap(); |
| 96 | 96 |
p = const_dfs_test.predMap(); |
| 97 | 97 |
pp = const_dfs_test.path(t); |
| 98 | 98 |
} |
| 99 | 99 |
{
|
| 100 | 100 |
DType |
| 101 | 101 |
::SetPredMap<concepts::ReadWriteMap<Node,Arc> > |
| 102 | 102 |
::SetDistMap<concepts::ReadWriteMap<Node,int> > |
| 103 | 103 |
::SetReachedMap<concepts::ReadWriteMap<Node,bool> > |
| 104 | 104 |
::SetStandardProcessedMap |
| 105 | 105 |
::SetProcessedMap<concepts::WriteMap<Node,bool> > |
| 106 | 106 |
::Create dfs_test(G); |
| 107 | 107 |
|
| 108 | 108 |
concepts::ReadWriteMap<Node,Arc> pred_map; |
| 109 | 109 |
concepts::ReadWriteMap<Node,int> dist_map; |
| 110 | 110 |
concepts::ReadWriteMap<Node,bool> reached_map; |
| 111 | 111 |
concepts::WriteMap<Node,bool> processed_map; |
| 112 |
|
|
| 112 |
|
|
| 113 | 113 |
dfs_test |
| 114 | 114 |
.predMap(pred_map) |
| 115 | 115 |
.distMap(dist_map) |
| 116 | 116 |
.reachedMap(reached_map) |
| 117 | 117 |
.processedMap(processed_map); |
| 118 | 118 |
|
| 119 | 119 |
dfs_test.run(s); |
| 120 | 120 |
dfs_test.run(s,t); |
| 121 | 121 |
dfs_test.run(); |
| 122 | 122 |
dfs_test.init(); |
| 123 | 123 |
|
| 124 | 124 |
dfs_test.addSource(s); |
| 125 | 125 |
e = dfs_test.processNextArc(); |
| 126 | 126 |
e = dfs_test.nextArc(); |
| 127 | 127 |
b = dfs_test.emptyQueue(); |
| 128 | 128 |
i = dfs_test.queueSize(); |
| 129 |
|
|
| 129 |
|
|
| 130 | 130 |
dfs_test.start(); |
| 131 | 131 |
dfs_test.start(t); |
| 132 | 132 |
dfs_test.start(am); |
| 133 | 133 |
|
| 134 | 134 |
l = dfs_test.dist(t); |
| 135 | 135 |
e = dfs_test.predArc(t); |
| 136 | 136 |
s = dfs_test.predNode(t); |
| 137 | 137 |
b = dfs_test.reached(t); |
| 138 | 138 |
pp = dfs_test.path(t); |
| 139 | 139 |
} |
| 140 | 140 |
} |
| 141 | 141 |
|
| 142 | 142 |
void checkDfsFunctionCompile() |
| 143 | 143 |
{
|
| 144 | 144 |
typedef int VType; |
| 145 | 145 |
typedef concepts::Digraph Digraph; |
| 146 | 146 |
typedef Digraph::Arc Arc; |
| 147 | 147 |
typedef Digraph::Node Node; |
| 148 | 148 |
|
| 149 | 149 |
Digraph g; |
| 150 | 150 |
bool b; |
| 151 | 151 |
dfs(g).run(Node()); |
| 152 | 152 |
b=dfs(g).run(Node(),Node()); |
| 153 | 153 |
dfs(g).run(); |
| 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/concepts/digraph.h> |
| 20 | 20 |
#include <lemon/list_graph.h> |
| 21 | 21 |
#include <lemon/smart_graph.h> |
| 22 | 22 |
#include <lemon/static_graph.h> |
| 23 | 23 |
#include <lemon/full_graph.h> |
| 24 | 24 |
|
| 25 | 25 |
#include "test_tools.h" |
| 26 | 26 |
#include "graph_test.h" |
| 27 | 27 |
|
| 28 | 28 |
using namespace lemon; |
| 29 | 29 |
using namespace lemon::concepts; |
| ... | ... |
@@ -371,51 +371,51 @@ |
| 371 | 371 |
|
| 372 | 372 |
Arc |
| 373 | 373 |
e1 = g.addArc(n1, n2), |
| 374 | 374 |
e2 = g.addArc(n2, n3); |
| 375 | 375 |
|
| 376 | 376 |
check(g.valid(n1), "Wrong validity check"); |
| 377 | 377 |
check(g.valid(e1), "Wrong validity check"); |
| 378 | 378 |
|
| 379 | 379 |
g.erase(n1); |
| 380 | 380 |
|
| 381 | 381 |
check(!g.valid(n1), "Wrong validity check"); |
| 382 | 382 |
check(g.valid(n2), "Wrong validity check"); |
| 383 | 383 |
check(g.valid(n3), "Wrong validity check"); |
| 384 | 384 |
check(!g.valid(e1), "Wrong validity check"); |
| 385 | 385 |
check(g.valid(e2), "Wrong validity check"); |
| 386 | 386 |
|
| 387 | 387 |
check(!g.valid(g.nodeFromId(-1)), "Wrong validity check"); |
| 388 | 388 |
check(!g.valid(g.arcFromId(-1)), "Wrong validity check"); |
| 389 | 389 |
} |
| 390 | 390 |
|
| 391 | 391 |
void checkStaticDigraph() {
|
| 392 | 392 |
SmartDigraph g; |
| 393 | 393 |
SmartDigraph::NodeMap<StaticDigraph::Node> nref(g); |
| 394 | 394 |
SmartDigraph::ArcMap<StaticDigraph::Arc> aref(g); |
| 395 |
|
|
| 395 |
|
|
| 396 | 396 |
StaticDigraph G; |
| 397 |
|
|
| 397 |
|
|
| 398 | 398 |
checkGraphNodeList(G, 0); |
| 399 | 399 |
checkGraphArcList(G, 0); |
| 400 | 400 |
|
| 401 | 401 |
G.build(g, nref, aref); |
| 402 | 402 |
|
| 403 | 403 |
checkGraphNodeList(G, 0); |
| 404 | 404 |
checkGraphArcList(G, 0); |
| 405 | 405 |
|
| 406 | 406 |
SmartDigraph::Node |
| 407 | 407 |
n1 = g.addNode(), |
| 408 | 408 |
n2 = g.addNode(), |
| 409 | 409 |
n3 = g.addNode(); |
| 410 | 410 |
|
| 411 | 411 |
G.build(g, nref, aref); |
| 412 | 412 |
|
| 413 | 413 |
checkGraphNodeList(G, 3); |
| 414 | 414 |
checkGraphArcList(G, 0); |
| 415 | 415 |
|
| 416 | 416 |
SmartDigraph::Arc a1 = g.addArc(n1, n2); |
| 417 | 417 |
|
| 418 | 418 |
G.build(g, nref, aref); |
| 419 | 419 |
|
| 420 | 420 |
check(G.source(aref[a1]) == nref[n1] && G.target(aref[a1]) == nref[n2], |
| 421 | 421 |
"Wrong arc or wrong references"); |
| ... | ... |
@@ -443,73 +443,73 @@ |
| 443 | 443 |
checkGraphArcList(G, 4); |
| 444 | 444 |
|
| 445 | 445 |
checkGraphOutArcList(G, nref[n1], 1); |
| 446 | 446 |
checkGraphOutArcList(G, nref[n2], 3); |
| 447 | 447 |
checkGraphOutArcList(G, nref[n3], 0); |
| 448 | 448 |
|
| 449 | 449 |
checkGraphInArcList(G, nref[n1], 1); |
| 450 | 450 |
checkGraphInArcList(G, nref[n2], 1); |
| 451 | 451 |
checkGraphInArcList(G, nref[n3], 2); |
| 452 | 452 |
|
| 453 | 453 |
checkGraphConArcList(G, 4); |
| 454 | 454 |
|
| 455 | 455 |
std::vector<std::pair<int,int> > arcs; |
| 456 | 456 |
arcs.push_back(std::make_pair(0,1)); |
| 457 | 457 |
arcs.push_back(std::make_pair(0,2)); |
| 458 | 458 |
arcs.push_back(std::make_pair(1,3)); |
| 459 | 459 |
arcs.push_back(std::make_pair(1,2)); |
| 460 | 460 |
arcs.push_back(std::make_pair(3,0)); |
| 461 | 461 |
arcs.push_back(std::make_pair(3,3)); |
| 462 | 462 |
arcs.push_back(std::make_pair(4,2)); |
| 463 | 463 |
arcs.push_back(std::make_pair(4,3)); |
| 464 | 464 |
arcs.push_back(std::make_pair(4,1)); |
| 465 | 465 |
|
| 466 | 466 |
G.build(6, arcs.begin(), arcs.end()); |
| 467 |
|
|
| 467 |
|
|
| 468 | 468 |
checkGraphNodeList(G, 6); |
| 469 | 469 |
checkGraphArcList(G, 9); |
| 470 | 470 |
|
| 471 | 471 |
checkGraphOutArcList(G, G.node(0), 2); |
| 472 | 472 |
checkGraphOutArcList(G, G.node(1), 2); |
| 473 | 473 |
checkGraphOutArcList(G, G.node(2), 0); |
| 474 | 474 |
checkGraphOutArcList(G, G.node(3), 2); |
| 475 | 475 |
checkGraphOutArcList(G, G.node(4), 3); |
| 476 | 476 |
checkGraphOutArcList(G, G.node(5), 0); |
| 477 | 477 |
|
| 478 | 478 |
checkGraphInArcList(G, G.node(0), 1); |
| 479 | 479 |
checkGraphInArcList(G, G.node(1), 2); |
| 480 | 480 |
checkGraphInArcList(G, G.node(2), 3); |
| 481 | 481 |
checkGraphInArcList(G, G.node(3), 3); |
| 482 | 482 |
checkGraphInArcList(G, G.node(4), 0); |
| 483 | 483 |
checkGraphInArcList(G, G.node(5), 0); |
| 484 | 484 |
|
| 485 | 485 |
checkGraphConArcList(G, 9); |
| 486 | 486 |
|
| 487 | 487 |
checkNodeIds(G); |
| 488 | 488 |
checkArcIds(G); |
| 489 | 489 |
checkGraphNodeMap(G); |
| 490 | 490 |
checkGraphArcMap(G); |
| 491 |
|
|
| 491 |
|
|
| 492 | 492 |
int n = G.nodeNum(); |
| 493 | 493 |
int m = G.arcNum(); |
| 494 | 494 |
check(G.index(G.node(n-1)) == n-1, "Wrong index."); |
| 495 | 495 |
check(G.index(G.arc(m-1)) == m-1, "Wrong index."); |
| 496 | 496 |
} |
| 497 | 497 |
|
| 498 | 498 |
void checkFullDigraph(int num) {
|
| 499 | 499 |
typedef FullDigraph Digraph; |
| 500 | 500 |
DIGRAPH_TYPEDEFS(Digraph); |
| 501 | 501 |
|
| 502 | 502 |
Digraph G(num); |
| 503 | 503 |
check(G.nodeNum() == num && G.arcNum() == num * num, "Wrong size"); |
| 504 | 504 |
|
| 505 | 505 |
G.resize(num); |
| 506 | 506 |
check(G.nodeNum() == num && G.arcNum() == num * num, "Wrong size"); |
| 507 | 507 |
|
| 508 | 508 |
checkGraphNodeList(G, num); |
| 509 | 509 |
checkGraphArcList(G, num * num); |
| 510 | 510 |
|
| 511 | 511 |
for (NodeIt n(G); n != INVALID; ++n) {
|
| 512 | 512 |
checkGraphOutArcList(G, n, num); |
| 513 | 513 |
checkGraphInArcList(G, n, num); |
| 514 | 514 |
} |
| 515 | 515 |
| 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/concepts/digraph.h> |
| 20 | 20 |
#include <lemon/smart_graph.h> |
| 21 | 21 |
#include <lemon/list_graph.h> |
| 22 | 22 |
#include <lemon/lgf_reader.h> |
| 23 | 23 |
#include <lemon/dijkstra.h> |
| 24 | 24 |
#include <lemon/path.h> |
| 25 | 25 |
#include <lemon/bin_heap.h> |
| 26 | 26 |
|
| 27 | 27 |
#include "graph_test.h" |
| 28 | 28 |
#include "test_tools.h" |
| 29 | 29 |
|
| ... | ... |
@@ -64,100 +64,100 @@ |
| 64 | 64 |
Arc e; |
| 65 | 65 |
VType l; |
| 66 | 66 |
int i; |
| 67 | 67 |
bool b; |
| 68 | 68 |
DType::DistMap d(G); |
| 69 | 69 |
DType::PredMap p(G); |
| 70 | 70 |
LengthMap length; |
| 71 | 71 |
Path<Digraph> pp; |
| 72 | 72 |
concepts::ReadMap<Node,bool> nm; |
| 73 | 73 |
|
| 74 | 74 |
{
|
| 75 | 75 |
DType dijkstra_test(G,length); |
| 76 | 76 |
const DType& const_dijkstra_test = dijkstra_test; |
| 77 | 77 |
|
| 78 | 78 |
dijkstra_test.run(s); |
| 79 | 79 |
dijkstra_test.run(s,t); |
| 80 | 80 |
|
| 81 | 81 |
dijkstra_test.init(); |
| 82 | 82 |
dijkstra_test.addSource(s); |
| 83 | 83 |
dijkstra_test.addSource(s, 1); |
| 84 | 84 |
n = dijkstra_test.processNextNode(); |
| 85 | 85 |
n = const_dijkstra_test.nextNode(); |
| 86 | 86 |
b = const_dijkstra_test.emptyQueue(); |
| 87 | 87 |
i = const_dijkstra_test.queueSize(); |
| 88 |
|
|
| 88 |
|
|
| 89 | 89 |
dijkstra_test.start(); |
| 90 | 90 |
dijkstra_test.start(t); |
| 91 | 91 |
dijkstra_test.start(nm); |
| 92 | 92 |
|
| 93 | 93 |
l = const_dijkstra_test.dist(t); |
| 94 | 94 |
e = const_dijkstra_test.predArc(t); |
| 95 | 95 |
s = const_dijkstra_test.predNode(t); |
| 96 | 96 |
b = const_dijkstra_test.reached(t); |
| 97 | 97 |
b = const_dijkstra_test.processed(t); |
| 98 | 98 |
d = const_dijkstra_test.distMap(); |
| 99 | 99 |
p = const_dijkstra_test.predMap(); |
| 100 | 100 |
pp = const_dijkstra_test.path(t); |
| 101 | 101 |
l = const_dijkstra_test.currentDist(t); |
| 102 | 102 |
} |
| 103 | 103 |
{
|
| 104 | 104 |
DType |
| 105 | 105 |
::SetPredMap<concepts::ReadWriteMap<Node,Arc> > |
| 106 | 106 |
::SetDistMap<concepts::ReadWriteMap<Node,VType> > |
| 107 | 107 |
::SetStandardProcessedMap |
| 108 | 108 |
::SetProcessedMap<concepts::WriteMap<Node,bool> > |
| 109 | 109 |
::SetOperationTraits<DijkstraDefaultOperationTraits<VType> > |
| 110 | 110 |
::SetHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> > > |
| 111 | 111 |
::SetStandardHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> > > |
| 112 |
::SetHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> >, |
|
| 112 |
::SetHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> >, |
|
| 113 | 113 |
concepts::ReadWriteMap<Node,int> > |
| 114 | 114 |
::Create dijkstra_test(G,length); |
| 115 | 115 |
|
| 116 | 116 |
LengthMap length_map; |
| 117 | 117 |
concepts::ReadWriteMap<Node,Arc> pred_map; |
| 118 | 118 |
concepts::ReadWriteMap<Node,VType> dist_map; |
| 119 | 119 |
concepts::WriteMap<Node,bool> processed_map; |
| 120 | 120 |
concepts::ReadWriteMap<Node,int> heap_cross_ref; |
| 121 | 121 |
BinHeap<VType, concepts::ReadWriteMap<Node,int> > heap(heap_cross_ref); |
| 122 |
|
|
| 122 |
|
|
| 123 | 123 |
dijkstra_test |
| 124 | 124 |
.lengthMap(length_map) |
| 125 | 125 |
.predMap(pred_map) |
| 126 | 126 |
.distMap(dist_map) |
| 127 | 127 |
.processedMap(processed_map) |
| 128 | 128 |
.heap(heap, heap_cross_ref); |
| 129 | 129 |
|
| 130 | 130 |
dijkstra_test.run(s); |
| 131 | 131 |
dijkstra_test.run(s,t); |
| 132 | 132 |
|
| 133 | 133 |
dijkstra_test.addSource(s); |
| 134 | 134 |
dijkstra_test.addSource(s, 1); |
| 135 | 135 |
n = dijkstra_test.processNextNode(); |
| 136 | 136 |
n = dijkstra_test.nextNode(); |
| 137 | 137 |
b = dijkstra_test.emptyQueue(); |
| 138 | 138 |
i = dijkstra_test.queueSize(); |
| 139 |
|
|
| 139 |
|
|
| 140 | 140 |
dijkstra_test.start(); |
| 141 | 141 |
dijkstra_test.start(t); |
| 142 | 142 |
dijkstra_test.start(nm); |
| 143 | 143 |
|
| 144 | 144 |
l = dijkstra_test.dist(t); |
| 145 | 145 |
e = dijkstra_test.predArc(t); |
| 146 | 146 |
s = dijkstra_test.predNode(t); |
| 147 | 147 |
b = dijkstra_test.reached(t); |
| 148 | 148 |
b = dijkstra_test.processed(t); |
| 149 | 149 |
pp = dijkstra_test.path(t); |
| 150 | 150 |
l = dijkstra_test.currentDist(t); |
| 151 | 151 |
} |
| 152 | 152 |
|
| 153 | 153 |
} |
| 154 | 154 |
|
| 155 | 155 |
void checkDijkstraFunctionCompile() |
| 156 | 156 |
{
|
| 157 | 157 |
typedef int VType; |
| 158 | 158 |
typedef concepts::Digraph Digraph; |
| 159 | 159 |
typedef Digraph::Arc Arc; |
| 160 | 160 |
typedef Digraph::Node Node; |
| 161 | 161 |
typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap; |
| 162 | 162 |
|
| 163 | 163 |
Digraph g; |
| 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 <iostream> |
| 20 | 20 |
#include <vector> |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/concepts/digraph.h> |
| 23 | 23 |
#include <lemon/concepts/graph.h> |
| 24 | 24 |
#include <lemon/concept_check.h> |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/list_graph.h> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/edge_set.h> |
| 29 | 29 |
| 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/euler.h> |
| 20 | 20 |
#include <lemon/list_graph.h> |
| 21 | 21 |
#include <lemon/adaptors.h> |
| 22 | 22 |
#include "test_tools.h" |
| 23 | 23 |
|
| 24 | 24 |
using namespace lemon; |
| 25 | 25 |
|
| 26 | 26 |
template <typename Digraph> |
| 27 | 27 |
void checkDiEulerIt(const Digraph& g, |
| 28 | 28 |
const typename Digraph::Node& start = INVALID) |
| 29 | 29 |
{
|
| ... | ... |
@@ -64,160 +64,160 @@ |
| 64 | 64 |
typename Graph::Node lastNode = g.target(typename Graph::Arc(e)); |
| 65 | 65 |
if (start != INVALID) {
|
| 66 | 66 |
check(firstNode == start, "checkEulerIt: Wrong first node"); |
| 67 | 67 |
} |
| 68 | 68 |
|
| 69 | 69 |
for (; e != INVALID; ++e) {
|
| 70 | 70 |
if (e != INVALID) lastNode = g.target(typename Graph::Arc(e)); |
| 71 | 71 |
++visitationNumber[e]; |
| 72 | 72 |
} |
| 73 | 73 |
|
| 74 | 74 |
check(firstNode == lastNode, |
| 75 | 75 |
"checkEulerIt: First and last nodes are not the same"); |
| 76 | 76 |
|
| 77 | 77 |
for (typename Graph::EdgeIt e(g); e != INVALID; ++e) |
| 78 | 78 |
{
|
| 79 | 79 |
check(visitationNumber[e] == 1, |
| 80 | 80 |
"checkEulerIt: Not visited or multiple times visited edge found"); |
| 81 | 81 |
} |
| 82 | 82 |
} |
| 83 | 83 |
|
| 84 | 84 |
int main() |
| 85 | 85 |
{
|
| 86 | 86 |
typedef ListDigraph Digraph; |
| 87 | 87 |
typedef Undirector<Digraph> Graph; |
| 88 |
|
|
| 88 |
|
|
| 89 | 89 |
{
|
| 90 | 90 |
Digraph d; |
| 91 | 91 |
Graph g(d); |
| 92 |
|
|
| 92 |
|
|
| 93 | 93 |
checkDiEulerIt(d); |
| 94 | 94 |
checkDiEulerIt(g); |
| 95 | 95 |
checkEulerIt(g); |
| 96 | 96 |
|
| 97 | 97 |
check(eulerian(d), "This graph is Eulerian"); |
| 98 | 98 |
check(eulerian(g), "This graph is Eulerian"); |
| 99 | 99 |
} |
| 100 | 100 |
{
|
| 101 | 101 |
Digraph d; |
| 102 | 102 |
Graph g(d); |
| 103 | 103 |
Digraph::Node n = d.addNode(); |
| 104 | 104 |
|
| 105 | 105 |
checkDiEulerIt(d); |
| 106 | 106 |
checkDiEulerIt(g); |
| 107 | 107 |
checkEulerIt(g); |
| 108 | 108 |
|
| 109 | 109 |
check(eulerian(d), "This graph is Eulerian"); |
| 110 | 110 |
check(eulerian(g), "This graph is Eulerian"); |
| 111 | 111 |
} |
| 112 | 112 |
{
|
| 113 | 113 |
Digraph d; |
| 114 | 114 |
Graph g(d); |
| 115 | 115 |
Digraph::Node n = d.addNode(); |
| 116 | 116 |
d.addArc(n, n); |
| 117 | 117 |
|
| 118 | 118 |
checkDiEulerIt(d); |
| 119 | 119 |
checkDiEulerIt(g); |
| 120 | 120 |
checkEulerIt(g); |
| 121 | 121 |
|
| 122 | 122 |
check(eulerian(d), "This graph is Eulerian"); |
| 123 | 123 |
check(eulerian(g), "This graph is Eulerian"); |
| 124 | 124 |
} |
| 125 | 125 |
{
|
| 126 | 126 |
Digraph d; |
| 127 | 127 |
Graph g(d); |
| 128 | 128 |
Digraph::Node n1 = d.addNode(); |
| 129 | 129 |
Digraph::Node n2 = d.addNode(); |
| 130 | 130 |
Digraph::Node n3 = d.addNode(); |
| 131 |
|
|
| 131 |
|
|
| 132 | 132 |
d.addArc(n1, n2); |
| 133 | 133 |
d.addArc(n2, n1); |
| 134 | 134 |
d.addArc(n2, n3); |
| 135 | 135 |
d.addArc(n3, n2); |
| 136 | 136 |
|
| 137 | 137 |
checkDiEulerIt(d); |
| 138 | 138 |
checkDiEulerIt(d, n2); |
| 139 | 139 |
checkDiEulerIt(g); |
| 140 | 140 |
checkDiEulerIt(g, n2); |
| 141 | 141 |
checkEulerIt(g); |
| 142 | 142 |
checkEulerIt(g, n2); |
| 143 | 143 |
|
| 144 | 144 |
check(eulerian(d), "This graph is Eulerian"); |
| 145 | 145 |
check(eulerian(g), "This graph is Eulerian"); |
| 146 | 146 |
} |
| 147 | 147 |
{
|
| 148 | 148 |
Digraph d; |
| 149 | 149 |
Graph g(d); |
| 150 | 150 |
Digraph::Node n1 = d.addNode(); |
| 151 | 151 |
Digraph::Node n2 = d.addNode(); |
| 152 | 152 |
Digraph::Node n3 = d.addNode(); |
| 153 | 153 |
Digraph::Node n4 = d.addNode(); |
| 154 | 154 |
Digraph::Node n5 = d.addNode(); |
| 155 | 155 |
Digraph::Node n6 = d.addNode(); |
| 156 |
|
|
| 156 |
|
|
| 157 | 157 |
d.addArc(n1, n2); |
| 158 | 158 |
d.addArc(n2, n4); |
| 159 | 159 |
d.addArc(n1, n3); |
| 160 | 160 |
d.addArc(n3, n4); |
| 161 | 161 |
d.addArc(n4, n1); |
| 162 | 162 |
d.addArc(n3, n5); |
| 163 | 163 |
d.addArc(n5, n2); |
| 164 | 164 |
d.addArc(n4, n6); |
| 165 | 165 |
d.addArc(n2, n6); |
| 166 | 166 |
d.addArc(n6, n1); |
| 167 | 167 |
d.addArc(n6, n3); |
| 168 | 168 |
|
| 169 | 169 |
checkDiEulerIt(d); |
| 170 | 170 |
checkDiEulerIt(d, n1); |
| 171 | 171 |
checkDiEulerIt(d, n5); |
| 172 | 172 |
|
| 173 | 173 |
checkDiEulerIt(g); |
| 174 | 174 |
checkDiEulerIt(g, n1); |
| 175 | 175 |
checkDiEulerIt(g, n5); |
| 176 | 176 |
checkEulerIt(g); |
| 177 | 177 |
checkEulerIt(g, n1); |
| 178 | 178 |
checkEulerIt(g, n5); |
| 179 | 179 |
|
| 180 | 180 |
check(eulerian(d), "This graph is Eulerian"); |
| 181 | 181 |
check(eulerian(g), "This graph is Eulerian"); |
| 182 | 182 |
} |
| 183 | 183 |
{
|
| 184 | 184 |
Digraph d; |
| 185 | 185 |
Graph g(d); |
| 186 | 186 |
Digraph::Node n0 = d.addNode(); |
| 187 | 187 |
Digraph::Node n1 = d.addNode(); |
| 188 | 188 |
Digraph::Node n2 = d.addNode(); |
| 189 | 189 |
Digraph::Node n3 = d.addNode(); |
| 190 | 190 |
Digraph::Node n4 = d.addNode(); |
| 191 | 191 |
Digraph::Node n5 = d.addNode(); |
| 192 |
|
|
| 192 |
|
|
| 193 | 193 |
d.addArc(n1, n2); |
| 194 | 194 |
d.addArc(n2, n3); |
| 195 | 195 |
d.addArc(n3, n1); |
| 196 | 196 |
|
| 197 | 197 |
checkDiEulerIt(d); |
| 198 | 198 |
checkDiEulerIt(d, n2); |
| 199 | 199 |
|
| 200 | 200 |
checkDiEulerIt(g); |
| 201 | 201 |
checkDiEulerIt(g, n2); |
| 202 | 202 |
checkEulerIt(g); |
| 203 | 203 |
checkEulerIt(g, n2); |
| 204 | 204 |
|
| 205 | 205 |
check(!eulerian(d), "This graph is not Eulerian"); |
| 206 | 206 |
check(!eulerian(g), "This graph is not Eulerian"); |
| 207 | 207 |
} |
| 208 | 208 |
{
|
| 209 | 209 |
Digraph d; |
| 210 | 210 |
Graph g(d); |
| 211 | 211 |
Digraph::Node n1 = d.addNode(); |
| 212 | 212 |
Digraph::Node n2 = d.addNode(); |
| 213 | 213 |
Digraph::Node n3 = d.addNode(); |
| 214 |
|
|
| 214 |
|
|
| 215 | 215 |
d.addArc(n1, n2); |
| 216 | 216 |
d.addArc(n2, n3); |
| 217 | 217 |
|
| 218 | 218 |
check(!eulerian(d), "This graph is not Eulerian"); |
| 219 | 219 |
check(!eulerian(g), "This graph is not Eulerian"); |
| 220 | 220 |
} |
| 221 | 221 |
|
| 222 | 222 |
return 0; |
| 223 | 223 |
} |
| 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 <iostream> |
| 20 | 20 |
#include <sstream> |
| 21 | 21 |
#include <vector> |
| 22 | 22 |
#include <queue> |
| 23 | 23 |
#include <cstdlib> |
| 24 | 24 |
|
| 25 | 25 |
#include <lemon/fractional_matching.h> |
| 26 | 26 |
#include <lemon/smart_graph.h> |
| 27 | 27 |
#include <lemon/concepts/graph.h> |
| 28 | 28 |
#include <lemon/concepts/maps.h> |
| 29 | 29 |
#include <lemon/lgf_reader.h> |
| ... | ... |
@@ -217,49 +217,49 @@ |
| 217 | 217 |
} |
| 218 | 218 |
|
| 219 | 219 |
void checkFractionalMatching(const SmartGraph& graph, |
| 220 | 220 |
const MaxFractionalMatching<SmartGraph>& mfm, |
| 221 | 221 |
bool allow_loops = true) {
|
| 222 | 222 |
int pv = 0; |
| 223 | 223 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
| 224 | 224 |
int indeg = 0; |
| 225 | 225 |
for (InArcIt a(graph, n); a != INVALID; ++a) {
|
| 226 | 226 |
if (mfm.matching(graph.source(a)) == a) {
|
| 227 | 227 |
++indeg; |
| 228 | 228 |
} |
| 229 | 229 |
} |
| 230 | 230 |
if (mfm.matching(n) != INVALID) {
|
| 231 | 231 |
check(indeg == 1, "Invalid matching"); |
| 232 | 232 |
++pv; |
| 233 | 233 |
} else {
|
| 234 | 234 |
check(indeg == 0, "Invalid matching"); |
| 235 | 235 |
} |
| 236 | 236 |
} |
| 237 | 237 |
check(pv == mfm.matchingSize(), "Wrong matching size"); |
| 238 | 238 |
|
| 239 | 239 |
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
|
| 240 | 240 |
check((e == mfm.matching(graph.u(e)) ? 1 : 0) + |
| 241 |
(e == mfm.matching(graph.v(e)) ? 1 : 0) == |
|
| 241 |
(e == mfm.matching(graph.v(e)) ? 1 : 0) == |
|
| 242 | 242 |
mfm.matching(e), "Invalid matching"); |
| 243 | 243 |
} |
| 244 | 244 |
|
| 245 | 245 |
SmartGraph::NodeMap<bool> processed(graph, false); |
| 246 | 246 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
| 247 | 247 |
if (processed[n]) continue; |
| 248 | 248 |
processed[n] = true; |
| 249 | 249 |
if (mfm.matching(n) == INVALID) continue; |
| 250 | 250 |
int num = 1; |
| 251 | 251 |
Node v = graph.target(mfm.matching(n)); |
| 252 | 252 |
while (v != n) {
|
| 253 | 253 |
processed[v] = true; |
| 254 | 254 |
++num; |
| 255 | 255 |
v = graph.target(mfm.matching(v)); |
| 256 | 256 |
} |
| 257 | 257 |
check(num == 2 || num % 2 == 1, "Wrong cycle size"); |
| 258 | 258 |
check(allow_loops || num != 1, "Wrong cycle size"); |
| 259 | 259 |
} |
| 260 | 260 |
|
| 261 | 261 |
int anum = 0, bnum = 0; |
| 262 | 262 |
SmartGraph::NodeMap<bool> neighbours(graph, false); |
| 263 | 263 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
| 264 | 264 |
if (!mfm.barrier(n)) continue; |
| 265 | 265 |
++anum; |
| ... | ... |
@@ -271,49 +271,49 @@ |
| 271 | 271 |
++bnum; |
| 272 | 272 |
} |
| 273 | 273 |
} |
| 274 | 274 |
} |
| 275 | 275 |
check(anum - bnum + mfm.matchingSize() == countNodes(graph), |
| 276 | 276 |
"Wrong barrier"); |
| 277 | 277 |
} |
| 278 | 278 |
|
| 279 | 279 |
void checkPerfectFractionalMatching(const SmartGraph& graph, |
| 280 | 280 |
const MaxFractionalMatching<SmartGraph>& mfm, |
| 281 | 281 |
bool perfect, bool allow_loops = true) {
|
| 282 | 282 |
if (perfect) {
|
| 283 | 283 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
| 284 | 284 |
int indeg = 0; |
| 285 | 285 |
for (InArcIt a(graph, n); a != INVALID; ++a) {
|
| 286 | 286 |
if (mfm.matching(graph.source(a)) == a) {
|
| 287 | 287 |
++indeg; |
| 288 | 288 |
} |
| 289 | 289 |
} |
| 290 | 290 |
check(mfm.matching(n) != INVALID, "Invalid matching"); |
| 291 | 291 |
check(indeg == 1, "Invalid matching"); |
| 292 | 292 |
} |
| 293 | 293 |
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
|
| 294 | 294 |
check((e == mfm.matching(graph.u(e)) ? 1 : 0) + |
| 295 |
(e == mfm.matching(graph.v(e)) ? 1 : 0) == |
|
| 295 |
(e == mfm.matching(graph.v(e)) ? 1 : 0) == |
|
| 296 | 296 |
mfm.matching(e), "Invalid matching"); |
| 297 | 297 |
} |
| 298 | 298 |
} else {
|
| 299 | 299 |
int anum = 0, bnum = 0; |
| 300 | 300 |
SmartGraph::NodeMap<bool> neighbours(graph, false); |
| 301 | 301 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
| 302 | 302 |
if (!mfm.barrier(n)) continue; |
| 303 | 303 |
++anum; |
| 304 | 304 |
for (SmartGraph::InArcIt a(graph, n); a != INVALID; ++a) {
|
| 305 | 305 |
Node u = graph.source(a); |
| 306 | 306 |
if (!allow_loops && u == n) continue; |
| 307 | 307 |
if (!neighbours[u]) {
|
| 308 | 308 |
neighbours[u] = true; |
| 309 | 309 |
++bnum; |
| 310 | 310 |
} |
| 311 | 311 |
} |
| 312 | 312 |
} |
| 313 | 313 |
check(anum - bnum > 0, "Wrong barrier"); |
| 314 | 314 |
} |
| 315 | 315 |
} |
| 316 | 316 |
|
| 317 | 317 |
void checkWeightedFractionalMatching(const SmartGraph& graph, |
| 318 | 318 |
const SmartGraph::EdgeMap<int>& weight, |
| 319 | 319 |
const MaxWeightedFractionalMatching<SmartGraph>& mwfm, |
| ... | ... |
@@ -329,49 +329,49 @@ |
| 329 | 329 |
} |
| 330 | 330 |
|
| 331 | 331 |
int pv = 0; |
| 332 | 332 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
| 333 | 333 |
int indeg = 0; |
| 334 | 334 |
for (InArcIt a(graph, n); a != INVALID; ++a) {
|
| 335 | 335 |
if (mwfm.matching(graph.source(a)) == a) {
|
| 336 | 336 |
++indeg; |
| 337 | 337 |
} |
| 338 | 338 |
} |
| 339 | 339 |
check(indeg <= 1, "Invalid matching"); |
| 340 | 340 |
if (mwfm.matching(n) != INVALID) {
|
| 341 | 341 |
check(mwfm.nodeValue(n) >= 0, "Invalid node value"); |
| 342 | 342 |
check(indeg == 1, "Invalid matching"); |
| 343 | 343 |
pv += weight[mwfm.matching(n)]; |
| 344 | 344 |
SmartGraph::Node o = graph.target(mwfm.matching(n)); |
| 345 | 345 |
} else {
|
| 346 | 346 |
check(mwfm.nodeValue(n) == 0, "Invalid matching"); |
| 347 | 347 |
check(indeg == 0, "Invalid matching"); |
| 348 | 348 |
} |
| 349 | 349 |
} |
| 350 | 350 |
|
| 351 | 351 |
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
|
| 352 | 352 |
check((e == mwfm.matching(graph.u(e)) ? 1 : 0) + |
| 353 |
(e == mwfm.matching(graph.v(e)) ? 1 : 0) == |
|
| 353 |
(e == mwfm.matching(graph.v(e)) ? 1 : 0) == |
|
| 354 | 354 |
mwfm.matching(e), "Invalid matching"); |
| 355 | 355 |
} |
| 356 | 356 |
|
| 357 | 357 |
int dv = 0; |
| 358 | 358 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
| 359 | 359 |
dv += mwfm.nodeValue(n); |
| 360 | 360 |
} |
| 361 | 361 |
|
| 362 | 362 |
check(pv * mwfm.dualScale == dv * 2, "Wrong duality"); |
| 363 | 363 |
|
| 364 | 364 |
SmartGraph::NodeMap<bool> processed(graph, false); |
| 365 | 365 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
| 366 | 366 |
if (processed[n]) continue; |
| 367 | 367 |
processed[n] = true; |
| 368 | 368 |
if (mwfm.matching(n) == INVALID) continue; |
| 369 | 369 |
int num = 1; |
| 370 | 370 |
Node v = graph.target(mwfm.matching(n)); |
| 371 | 371 |
while (v != n) {
|
| 372 | 372 |
processed[v] = true; |
| 373 | 373 |
++num; |
| 374 | 374 |
v = graph.target(mwfm.matching(v)); |
| 375 | 375 |
} |
| 376 | 376 |
check(num == 2 || num % 2 == 1, "Wrong cycle size"); |
| 377 | 377 |
check(allow_loops || num != 1, "Wrong cycle size"); |
| ... | ... |
@@ -389,49 +389,49 @@ |
| 389 | 389 |
int rw = mwpfm.nodeValue(graph.u(e)) + mwpfm.nodeValue(graph.v(e)) |
| 390 | 390 |
- weight[e] * mwpfm.dualScale; |
| 391 | 391 |
|
| 392 | 392 |
check(rw >= 0, "Negative reduced weight"); |
| 393 | 393 |
check(rw == 0 || !mwpfm.matching(e), |
| 394 | 394 |
"Non-zero reduced weight on matching edge"); |
| 395 | 395 |
} |
| 396 | 396 |
|
| 397 | 397 |
int pv = 0; |
| 398 | 398 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
| 399 | 399 |
int indeg = 0; |
| 400 | 400 |
for (InArcIt a(graph, n); a != INVALID; ++a) {
|
| 401 | 401 |
if (mwpfm.matching(graph.source(a)) == a) {
|
| 402 | 402 |
++indeg; |
| 403 | 403 |
} |
| 404 | 404 |
} |
| 405 | 405 |
check(mwpfm.matching(n) != INVALID, "Invalid perfect matching"); |
| 406 | 406 |
check(indeg == 1, "Invalid perfect matching"); |
| 407 | 407 |
pv += weight[mwpfm.matching(n)]; |
| 408 | 408 |
SmartGraph::Node o = graph.target(mwpfm.matching(n)); |
| 409 | 409 |
} |
| 410 | 410 |
|
| 411 | 411 |
for (SmartGraph::EdgeIt e(graph); e != INVALID; ++e) {
|
| 412 | 412 |
check((e == mwpfm.matching(graph.u(e)) ? 1 : 0) + |
| 413 |
(e == mwpfm.matching(graph.v(e)) ? 1 : 0) == |
|
| 413 |
(e == mwpfm.matching(graph.v(e)) ? 1 : 0) == |
|
| 414 | 414 |
mwpfm.matching(e), "Invalid matching"); |
| 415 | 415 |
} |
| 416 | 416 |
|
| 417 | 417 |
int dv = 0; |
| 418 | 418 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
| 419 | 419 |
dv += mwpfm.nodeValue(n); |
| 420 | 420 |
} |
| 421 | 421 |
|
| 422 | 422 |
check(pv * mwpfm.dualScale == dv * 2, "Wrong duality"); |
| 423 | 423 |
|
| 424 | 424 |
SmartGraph::NodeMap<bool> processed(graph, false); |
| 425 | 425 |
for (SmartGraph::NodeIt n(graph); n != INVALID; ++n) {
|
| 426 | 426 |
if (processed[n]) continue; |
| 427 | 427 |
processed[n] = true; |
| 428 | 428 |
if (mwpfm.matching(n) == INVALID) continue; |
| 429 | 429 |
int num = 1; |
| 430 | 430 |
Node v = graph.target(mwpfm.matching(n)); |
| 431 | 431 |
while (v != n) {
|
| 432 | 432 |
processed[v] = true; |
| 433 | 433 |
++num; |
| 434 | 434 |
v = graph.target(mwpfm.matching(v)); |
| 435 | 435 |
} |
| 436 | 436 |
check(num == 2 || num % 2 == 1, "Wrong cycle size"); |
| 437 | 437 |
check(allow_loops || num != 1, "Wrong cycle size"); |
| 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
| 2 |
* |
|
| 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
| 4 |
* |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
|
| 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
|
| 8 |
* |
|
| 9 |
* Permission to use, modify and distribute this software is granted |
|
| 10 |
* provided that this copyright notice appears in all copies. For |
|
| 11 |
* precise terms see the accompanying LICENSE file. |
|
| 12 |
* |
|
| 13 |
* This software is provided "AS IS" with no warranty of any kind, |
|
| 14 |
* express or implied, and with no claim as to its suitability for any |
|
| 15 |
* purpose. |
|
| 16 |
* |
|
| 17 |
*/ |
|
| 18 |
|
|
| 1 | 19 |
#include <iostream> |
| 2 | 20 |
|
| 3 | 21 |
#include "test_tools.h" |
| 4 | 22 |
#include <lemon/smart_graph.h> |
| 5 | 23 |
#include <lemon/concepts/graph.h> |
| 6 | 24 |
#include <lemon/concepts/maps.h> |
| 7 | 25 |
#include <lemon/lgf_reader.h> |
| 8 | 26 |
#include <lemon/gomory_hu.h> |
| 9 | 27 |
#include <cstdlib> |
| 10 | 28 |
|
| 11 | 29 |
using namespace std; |
| 12 | 30 |
using namespace lemon; |
| 13 | 31 |
|
| 14 | 32 |
typedef SmartGraph Graph; |
| 15 | 33 |
|
| 16 | 34 |
char test_lgf[] = |
| 17 | 35 |
"@nodes\n" |
| 18 | 36 |
"label\n" |
| 19 | 37 |
"0\n" |
| 20 | 38 |
"1\n" |
| 21 | 39 |
"2\n" |
| 22 | 40 |
"3\n" |
| 23 | 41 |
"4\n" |
| 24 | 42 |
"@arcs\n" |
| 25 | 43 |
" label capacity\n" |
| 26 | 44 |
"0 1 0 1\n" |
| 27 | 45 |
"1 2 1 1\n" |
| 28 | 46 |
"2 3 2 1\n" |
| 29 | 47 |
"0 3 4 5\n" |
| 30 | 48 |
"0 3 5 10\n" |
| 31 | 49 |
"0 3 6 7\n" |
| 32 | 50 |
"4 2 7 1\n" |
| 33 | 51 |
"@attributes\n" |
| 34 | 52 |
"source 0\n" |
| 35 | 53 |
"target 3\n"; |
| 36 |
|
|
| 54 |
|
|
| 37 | 55 |
void checkGomoryHuCompile() |
| 38 | 56 |
{
|
| 39 | 57 |
typedef int Value; |
| 40 | 58 |
typedef concepts::Graph Graph; |
| 41 | 59 |
|
| 42 | 60 |
typedef Graph::Node Node; |
| 43 | 61 |
typedef Graph::Edge Edge; |
| 44 | 62 |
typedef concepts::ReadMap<Edge, Value> CapMap; |
| 45 | 63 |
typedef concepts::ReadWriteMap<Node, bool> CutMap; |
| 46 | 64 |
|
| 47 | 65 |
Graph g; |
| 48 | 66 |
Node n; |
| 49 | 67 |
CapMap cap; |
| 50 | 68 |
CutMap cut; |
| 51 | 69 |
Value v; |
| 52 | 70 |
int d; |
| 53 | 71 |
|
| 54 | 72 |
GomoryHu<Graph, CapMap> gh_test(g, cap); |
| 55 | 73 |
const GomoryHu<Graph, CapMap>& |
| 56 | 74 |
const_gh_test = gh_test; |
| 57 | 75 |
|
| 58 | 76 |
gh_test.run(); |
| 59 | 77 |
|
| 60 | 78 |
n = const_gh_test.predNode(n); |
| 61 | 79 |
v = const_gh_test.predValue(n); |
| 62 | 80 |
d = const_gh_test.rootDist(n); |
| 63 | 81 |
v = const_gh_test.minCutValue(n, n); |
| 64 | 82 |
v = const_gh_test.minCutMap(n, n, cut); |
| 65 | 83 |
} |
| 66 | 84 |
|
| 67 | 85 |
GRAPH_TYPEDEFS(Graph); |
| 68 | 86 |
typedef Graph::EdgeMap<int> IntEdgeMap; |
| 69 | 87 |
typedef Graph::NodeMap<bool> BoolNodeMap; |
| 70 | 88 |
|
| 71 | 89 |
int cutValue(const Graph& graph, const BoolNodeMap& cut, |
| 72 |
|
|
| 90 |
const IntEdgeMap& capacity) {
|
|
| 73 | 91 |
|
| 74 | 92 |
int sum = 0; |
| 75 | 93 |
for (EdgeIt e(graph); e != INVALID; ++e) {
|
| 76 | 94 |
Node s = graph.u(e); |
| 77 | 95 |
Node t = graph.v(e); |
| 78 | 96 |
|
| 79 | 97 |
if (cut[s] != cut[t]) {
|
| 80 | 98 |
sum += capacity[e]; |
| 81 | 99 |
} |
| 82 | 100 |
} |
| 83 | 101 |
return sum; |
| 84 | 102 |
} |
| 85 | 103 |
|
| 86 | 104 |
|
| 87 | 105 |
int main() {
|
| 88 | 106 |
Graph graph; |
| 89 | 107 |
IntEdgeMap capacity(graph); |
| 90 | 108 |
|
| 91 | 109 |
std::istringstream input(test_lgf); |
| 92 | 110 |
GraphReader<Graph>(graph, input). |
| 93 | 111 |
edgeMap("capacity", capacity).run();
|
| 94 | 112 |
|
| 95 | 113 |
GomoryHu<Graph> ght(graph, capacity); |
| 96 | 114 |
ght.run(); |
| 97 | 115 |
|
| 98 | 116 |
for (NodeIt u(graph); u != INVALID; ++u) {
|
| 99 | 117 |
for (NodeIt v(graph); v != u; ++v) {
|
| 100 | 118 |
Preflow<Graph, IntEdgeMap> pf(graph, capacity, u, v); |
| 101 | 119 |
pf.runMinCut(); |
| 102 | 120 |
BoolNodeMap cm(graph); |
| 103 | 121 |
ght.minCutMap(u, v, cm); |
| 104 | 122 |
check(pf.flowValue() == ght.minCutValue(u, v), "Wrong cut 1"); |
| 105 | 123 |
check(cm[u] != cm[v], "Wrong cut 2"); |
| 106 | 124 |
check(pf.flowValue() == cutValue(graph, cm, capacity), "Wrong cut 3"); |
| 107 | 125 |
|
| 108 | 126 |
int sum=0; |
| 109 | 127 |
for(GomoryHu<Graph>::MinCutEdgeIt a(ght, u, v);a!=INVALID;++a) |
| 110 |
sum+=capacity[a]; |
|
| 128 |
sum+=capacity[a]; |
|
| 111 | 129 |
check(sum == ght.minCutValue(u, v), "Problem with MinCutEdgeIt"); |
| 112 | 130 |
|
| 113 | 131 |
sum=0; |
| 114 | 132 |
for(GomoryHu<Graph>::MinCutNodeIt n(ght, u, v,true);n!=INVALID;++n) |
| 115 | 133 |
sum++; |
| 116 | 134 |
for(GomoryHu<Graph>::MinCutNodeIt n(ght, u, v,false);n!=INVALID;++n) |
| 117 | 135 |
sum++; |
| 118 | 136 |
check(sum == countNodes(graph), "Problem with MinCutNodeIt"); |
| 119 | 137 |
} |
| 120 | 138 |
} |
| 121 |
|
|
| 139 |
|
|
| 122 | 140 |
return 0; |
| 123 | 141 |
} |
| 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/concepts/graph.h> |
| 20 | 20 |
#include <lemon/list_graph.h> |
| 21 | 21 |
#include <lemon/smart_graph.h> |
| 22 | 22 |
#include <lemon/full_graph.h> |
| 23 | 23 |
#include <lemon/grid_graph.h> |
| 24 | 24 |
#include <lemon/hypercube_graph.h> |
| 25 | 25 |
|
| 26 | 26 |
#include "test_tools.h" |
| 27 | 27 |
#include "graph_test.h" |
| 28 | 28 |
|
| 29 | 29 |
using namespace lemon; |
| ... | ... |
@@ -243,49 +243,49 @@ |
| 243 | 243 |
checkGraphIncEdgeArcLists(G, n2, 3); |
| 244 | 244 |
checkGraphIncEdgeArcLists(G, n3, 1); |
| 245 | 245 |
|
| 246 | 246 |
checkGraphConEdgeList(G, 3); |
| 247 | 247 |
checkGraphConArcList(G, 6); |
| 248 | 248 |
|
| 249 | 249 |
checkNodeIds(G); |
| 250 | 250 |
checkEdgeIds(G); |
| 251 | 251 |
checkArcIds(G); |
| 252 | 252 |
checkGraphNodeMap(G); |
| 253 | 253 |
checkGraphEdgeMap(G); |
| 254 | 254 |
checkGraphArcMap(G); |
| 255 | 255 |
|
| 256 | 256 |
G.addNode(); |
| 257 | 257 |
snapshot.save(G); |
| 258 | 258 |
|
| 259 | 259 |
G.addEdge(G.addNode(), G.addNode()); |
| 260 | 260 |
|
| 261 | 261 |
snapshot.restore(); |
| 262 | 262 |
snapshot.save(G); |
| 263 | 263 |
|
| 264 | 264 |
checkGraphNodeList(G, 4); |
| 265 | 265 |
checkGraphEdgeList(G, 3); |
| 266 | 266 |
checkGraphArcList(G, 6); |
| 267 |
|
|
| 267 |
|
|
| 268 | 268 |
G.addEdge(G.addNode(), G.addNode()); |
| 269 | 269 |
|
| 270 | 270 |
snapshot.restore(); |
| 271 | 271 |
|
| 272 | 272 |
checkGraphNodeList(G, 4); |
| 273 | 273 |
checkGraphEdgeList(G, 3); |
| 274 | 274 |
checkGraphArcList(G, 6); |
| 275 | 275 |
} |
| 276 | 276 |
|
| 277 | 277 |
void checkFullGraph(int num) {
|
| 278 | 278 |
typedef FullGraph Graph; |
| 279 | 279 |
GRAPH_TYPEDEFS(Graph); |
| 280 | 280 |
|
| 281 | 281 |
Graph G(num); |
| 282 | 282 |
check(G.nodeNum() == num && G.edgeNum() == num * (num - 1) / 2, |
| 283 | 283 |
"Wrong size"); |
| 284 | 284 |
|
| 285 | 285 |
G.resize(num); |
| 286 | 286 |
check(G.nodeNum() == num && G.edgeNum() == num * (num - 1) / 2, |
| 287 | 287 |
"Wrong size"); |
| 288 | 288 |
|
| 289 | 289 |
checkGraphNodeList(G, num); |
| 290 | 290 |
checkGraphEdgeList(G, num * (num - 1) / 2); |
| 291 | 291 |
|
| ... | ... |
@@ -492,49 +492,49 @@ |
| 492 | 492 |
} |
| 493 | 493 |
|
| 494 | 494 |
checkArcDirections(G); |
| 495 | 495 |
|
| 496 | 496 |
checkGraphConArcList(G, 2 * (width * (height - 1) + (width - 1) * height)); |
| 497 | 497 |
checkGraphConEdgeList(G, width * (height - 1) + (width - 1) * height); |
| 498 | 498 |
|
| 499 | 499 |
checkNodeIds(G); |
| 500 | 500 |
checkArcIds(G); |
| 501 | 501 |
checkEdgeIds(G); |
| 502 | 502 |
checkGraphNodeMap(G); |
| 503 | 503 |
checkGraphArcMap(G); |
| 504 | 504 |
checkGraphEdgeMap(G); |
| 505 | 505 |
|
| 506 | 506 |
} |
| 507 | 507 |
|
| 508 | 508 |
void checkHypercubeGraph(int dim) {
|
| 509 | 509 |
GRAPH_TYPEDEFS(HypercubeGraph); |
| 510 | 510 |
|
| 511 | 511 |
HypercubeGraph G(dim); |
| 512 | 512 |
check(G.dimension() == dim, "Wrong dimension"); |
| 513 | 513 |
|
| 514 | 514 |
G.resize(dim); |
| 515 | 515 |
check(G.dimension() == dim, "Wrong dimension"); |
| 516 |
|
|
| 516 |
|
|
| 517 | 517 |
checkGraphNodeList(G, 1 << dim); |
| 518 | 518 |
checkGraphEdgeList(G, dim * (1 << (dim-1))); |
| 519 | 519 |
checkGraphArcList(G, dim * (1 << dim)); |
| 520 | 520 |
|
| 521 | 521 |
Node n = G.nodeFromId(dim); |
| 522 | 522 |
|
| 523 | 523 |
for (NodeIt n(G); n != INVALID; ++n) {
|
| 524 | 524 |
checkGraphIncEdgeList(G, n, dim); |
| 525 | 525 |
for (IncEdgeIt e(G, n); e != INVALID; ++e) {
|
| 526 | 526 |
check( (G.u(e) == n && |
| 527 | 527 |
G.id(G.v(e)) == (G.id(n) ^ (1 << G.dimension(e)))) || |
| 528 | 528 |
(G.v(e) == n && |
| 529 | 529 |
G.id(G.u(e)) == (G.id(n) ^ (1 << G.dimension(e)))), |
| 530 | 530 |
"Wrong edge or wrong dimension"); |
| 531 | 531 |
} |
| 532 | 532 |
|
| 533 | 533 |
checkGraphOutArcList(G, n, dim); |
| 534 | 534 |
for (OutArcIt a(G, n); a != INVALID; ++a) {
|
| 535 | 535 |
check(G.source(a) == n && |
| 536 | 536 |
G.id(G.target(a)) == (G.id(n) ^ (1 << G.dimension(a))), |
| 537 | 537 |
"Wrong arc or wrong dimension"); |
| 538 | 538 |
} |
| 539 | 539 |
|
| 540 | 540 |
checkGraphInArcList(G, n, dim); |
| 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 <sstream> |
| 20 | 20 |
|
| 21 | 21 |
#include <lemon/smart_graph.h> |
| 22 | 22 |
#include <lemon/adaptors.h> |
| 23 | 23 |
#include <lemon/concepts/digraph.h> |
| 24 | 24 |
#include <lemon/concepts/maps.h> |
| 25 | 25 |
#include <lemon/lgf_reader.h> |
| 26 | 26 |
#include <lemon/hao_orlin.h> |
| 27 | 27 |
|
| 28 | 28 |
#include "test_tools.h" |
| 29 | 29 |
|
| ... | ... |
@@ -62,102 +62,102 @@ |
| 62 | 62 |
typedef concepts::WriteMap<Node, bool> CutMap; |
| 63 | 63 |
|
| 64 | 64 |
Digraph g; |
| 65 | 65 |
Node n; |
| 66 | 66 |
CapMap cap; |
| 67 | 67 |
CutMap cut; |
| 68 | 68 |
Value v; |
| 69 | 69 |
|
| 70 | 70 |
HaoOrlin<Digraph, CapMap> ho_test(g, cap); |
| 71 | 71 |
const HaoOrlin<Digraph, CapMap>& |
| 72 | 72 |
const_ho_test = ho_test; |
| 73 | 73 |
|
| 74 | 74 |
ho_test.init(); |
| 75 | 75 |
ho_test.init(n); |
| 76 | 76 |
ho_test.calculateOut(); |
| 77 | 77 |
ho_test.calculateIn(); |
| 78 | 78 |
ho_test.run(); |
| 79 | 79 |
ho_test.run(n); |
| 80 | 80 |
|
| 81 | 81 |
v = const_ho_test.minCutValue(); |
| 82 | 82 |
v = const_ho_test.minCutMap(cut); |
| 83 | 83 |
} |
| 84 | 84 |
|
| 85 | 85 |
template <typename Graph, typename CapMap, typename CutMap> |
| 86 |
typename CapMap::Value |
|
| 86 |
typename CapMap::Value |
|
| 87 | 87 |
cutValue(const Graph& graph, const CapMap& cap, const CutMap& cut) |
| 88 | 88 |
{
|
| 89 | 89 |
typename CapMap::Value sum = 0; |
| 90 | 90 |
for (typename Graph::ArcIt a(graph); a != INVALID; ++a) {
|
| 91 | 91 |
if (cut[graph.source(a)] && !cut[graph.target(a)]) |
| 92 | 92 |
sum += cap[a]; |
| 93 | 93 |
} |
| 94 | 94 |
return sum; |
| 95 | 95 |
} |
| 96 | 96 |
|
| 97 | 97 |
int main() {
|
| 98 | 98 |
SmartDigraph graph; |
| 99 | 99 |
SmartDigraph::ArcMap<int> cap1(graph), cap2(graph), cap3(graph); |
| 100 | 100 |
SmartDigraph::NodeMap<bool> cut(graph); |
| 101 | 101 |
|
| 102 | 102 |
istringstream input(lgf); |
| 103 | 103 |
digraphReader(graph, input) |
| 104 | 104 |
.arcMap("cap1", cap1)
|
| 105 | 105 |
.arcMap("cap2", cap2)
|
| 106 | 106 |
.arcMap("cap3", cap3)
|
| 107 | 107 |
.run(); |
| 108 | 108 |
|
| 109 | 109 |
{
|
| 110 | 110 |
HaoOrlin<SmartDigraph> ho(graph, cap1); |
| 111 | 111 |
ho.run(); |
| 112 | 112 |
ho.minCutMap(cut); |
| 113 |
|
|
| 113 |
|
|
| 114 | 114 |
check(ho.minCutValue() == 1, "Wrong cut value"); |
| 115 | 115 |
check(ho.minCutValue() == cutValue(graph, cap1, cut), "Wrong cut value"); |
| 116 | 116 |
} |
| 117 | 117 |
{
|
| 118 | 118 |
HaoOrlin<SmartDigraph> ho(graph, cap2); |
| 119 | 119 |
ho.run(); |
| 120 | 120 |
ho.minCutMap(cut); |
| 121 | 121 |
|
| 122 | 122 |
check(ho.minCutValue() == 1, "Wrong cut value"); |
| 123 | 123 |
check(ho.minCutValue() == cutValue(graph, cap2, cut), "Wrong cut value"); |
| 124 | 124 |
} |
| 125 | 125 |
{
|
| 126 | 126 |
HaoOrlin<SmartDigraph> ho(graph, cap3); |
| 127 | 127 |
ho.run(); |
| 128 | 128 |
ho.minCutMap(cut); |
| 129 |
|
|
| 129 |
|
|
| 130 | 130 |
check(ho.minCutValue() == 1, "Wrong cut value"); |
| 131 | 131 |
check(ho.minCutValue() == cutValue(graph, cap3, cut), "Wrong cut value"); |
| 132 | 132 |
} |
| 133 |
|
|
| 133 |
|
|
| 134 | 134 |
typedef Undirector<SmartDigraph> UGraph; |
| 135 | 135 |
UGraph ugraph(graph); |
| 136 |
|
|
| 136 |
|
|
| 137 | 137 |
{
|
| 138 | 138 |
HaoOrlin<UGraph, SmartDigraph::ArcMap<int> > ho(ugraph, cap1); |
| 139 | 139 |
ho.run(); |
| 140 | 140 |
ho.minCutMap(cut); |
| 141 |
|
|
| 141 |
|
|
| 142 | 142 |
check(ho.minCutValue() == 2, "Wrong cut value"); |
| 143 | 143 |
check(ho.minCutValue() == cutValue(ugraph, cap1, cut), "Wrong cut value"); |
| 144 | 144 |
} |
| 145 | 145 |
{
|
| 146 | 146 |
HaoOrlin<UGraph, SmartDigraph::ArcMap<int> > ho(ugraph, cap2); |
| 147 | 147 |
ho.run(); |
| 148 | 148 |
ho.minCutMap(cut); |
| 149 |
|
|
| 149 |
|
|
| 150 | 150 |
check(ho.minCutValue() == 5, "Wrong cut value"); |
| 151 | 151 |
check(ho.minCutValue() == cutValue(ugraph, cap2, cut), "Wrong cut value"); |
| 152 | 152 |
} |
| 153 | 153 |
{
|
| 154 | 154 |
HaoOrlin<UGraph, SmartDigraph::ArcMap<int> > ho(ugraph, cap3); |
| 155 | 155 |
ho.run(); |
| 156 | 156 |
ho.minCutMap(cut); |
| 157 |
|
|
| 157 |
|
|
| 158 | 158 |
check(ho.minCutValue() == 5, "Wrong cut value"); |
| 159 | 159 |
check(ho.minCutValue() == cutValue(ugraph, cap3, cut), "Wrong cut value"); |
| 160 | 160 |
} |
| 161 | 161 |
|
| 162 | 162 |
return 0; |
| 163 | 163 |
} |
| 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 <deque> |
| 20 | 20 |
#include <set> |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/concept_check.h> |
| 23 | 23 |
#include <lemon/concepts/maps.h> |
| 24 | 24 |
#include <lemon/maps.h> |
| 25 | 25 |
#include <lemon/list_graph.h> |
| 26 | 26 |
#include <lemon/smart_graph.h> |
| 27 | 27 |
#include <lemon/adaptors.h> |
| 28 | 28 |
#include <lemon/dfs.h> |
| 29 | 29 |
#include <algorithm> |
| ... | ... |
@@ -204,49 +204,50 @@ |
| 204 | 204 |
check(!composeMap(m1,m2)[0] && composeMap(m1,m2)[1], |
| 205 | 205 |
"Something is wrong with ComposeMap") |
| 206 | 206 |
} |
| 207 | 207 |
|
| 208 | 208 |
// CombineMap |
| 209 | 209 |
{
|
| 210 | 210 |
typedef CombineMap<DoubleMap, DoubleMap, std::plus<double> > CombMap; |
| 211 | 211 |
checkConcept<ReadMap<A,double>, CombMap>(); |
| 212 | 212 |
CombMap map1 = CombMap(DoubleMap(), DoubleMap()); |
| 213 | 213 |
CombMap map2 = combineMap(DoubleMap(), DoubleMap(), std::plus<double>()); |
| 214 | 214 |
|
| 215 | 215 |
check(combineMap(constMap<B,int,2>(), identityMap<B>(), &binc)[B()] == 3, |
| 216 | 216 |
"Something is wrong with CombineMap"); |
| 217 | 217 |
} |
| 218 | 218 |
|
| 219 | 219 |
// FunctorToMap, MapToFunctor |
| 220 | 220 |
{
|
| 221 | 221 |
checkConcept<ReadMap<A,B>, FunctorToMap<F,A,B> >(); |
| 222 | 222 |
checkConcept<ReadMap<A,B>, FunctorToMap<F> >(); |
| 223 | 223 |
FunctorToMap<F> map1; |
| 224 | 224 |
FunctorToMap<F> map2 = FunctorToMap<F>(F()); |
| 225 | 225 |
B b = functorToMap(F())[A()]; |
| 226 | 226 |
|
| 227 | 227 |
checkConcept<ReadMap<A,B>, MapToFunctor<ReadMap<A,B> > >(); |
| 228 |
MapToFunctor<ReadMap<A,B> > map = |
|
| 228 |
MapToFunctor<ReadMap<A,B> > map = |
|
| 229 |
MapToFunctor<ReadMap<A,B> >(ReadMap<A,B>()); |
|
| 229 | 230 |
|
| 230 | 231 |
check(functorToMap(&func)[A()] == 3, |
| 231 | 232 |
"Something is wrong with FunctorToMap"); |
| 232 | 233 |
check(mapToFunctor(constMap<A,int>(2))(A()) == 2, |
| 233 | 234 |
"Something is wrong with MapToFunctor"); |
| 234 | 235 |
check(mapToFunctor(functorToMap(&func))(A()) == 3 && |
| 235 | 236 |
mapToFunctor(functorToMap(&func))[A()] == 3, |
| 236 | 237 |
"Something is wrong with FunctorToMap or MapToFunctor"); |
| 237 | 238 |
check(functorToMap(mapToFunctor(constMap<A,int>(2)))[A()] == 2, |
| 238 | 239 |
"Something is wrong with FunctorToMap or MapToFunctor"); |
| 239 | 240 |
} |
| 240 | 241 |
|
| 241 | 242 |
// ConvertMap |
| 242 | 243 |
{
|
| 243 | 244 |
checkConcept<ReadMap<double,double>, |
| 244 | 245 |
ConvertMap<ReadMap<double, int>, double> >(); |
| 245 | 246 |
ConvertMap<RangeMap<bool>, int> map1(rangeMap(1, true)); |
| 246 | 247 |
ConvertMap<RangeMap<bool>, int> map2 = convertMap<int>(rangeMap(2, false)); |
| 247 | 248 |
} |
| 248 | 249 |
|
| 249 | 250 |
// ForkMap |
| 250 | 251 |
{
|
| 251 | 252 |
checkConcept<DoubleWriteMap, ForkMap<DoubleWriteMap, DoubleWriteMap> >(); |
| 252 | 253 |
|
| ... | ... |
@@ -356,301 +357,301 @@ |
| 356 | 357 |
typedef std::vector<int> vec; |
| 357 | 358 |
checkConcept<WriteMap<int, bool>, LoggerBoolMap<vec::iterator> >(); |
| 358 | 359 |
checkConcept<WriteMap<int, bool>, |
| 359 | 360 |
LoggerBoolMap<std::back_insert_iterator<vec> > >(); |
| 360 | 361 |
|
| 361 | 362 |
vec v1; |
| 362 | 363 |
vec v2(10); |
| 363 | 364 |
LoggerBoolMap<std::back_insert_iterator<vec> > |
| 364 | 365 |
map1(std::back_inserter(v1)); |
| 365 | 366 |
LoggerBoolMap<vec::iterator> map2(v2.begin()); |
| 366 | 367 |
map1.set(10, false); |
| 367 | 368 |
map1.set(20, true); map2.set(20, true); |
| 368 | 369 |
map1.set(30, false); map2.set(40, false); |
| 369 | 370 |
map1.set(50, true); map2.set(50, true); |
| 370 | 371 |
map1.set(60, true); map2.set(60, true); |
| 371 | 372 |
check(v1.size() == 3 && v2.size() == 10 && |
| 372 | 373 |
v1[0]==20 && v1[1]==50 && v1[2]==60 && |
| 373 | 374 |
v2[0]==20 && v2[1]==50 && v2[2]==60, |
| 374 | 375 |
"Something is wrong with LoggerBoolMap"); |
| 375 | 376 |
|
| 376 | 377 |
int i = 0; |
| 377 | 378 |
for ( LoggerBoolMap<vec::iterator>::Iterator it = map2.begin(); |
| 378 | 379 |
it != map2.end(); ++it ) |
| 379 | 380 |
check(v1[i++] == *it, "Something is wrong with LoggerBoolMap"); |
| 380 |
|
|
| 381 |
|
|
| 381 | 382 |
typedef ListDigraph Graph; |
| 382 | 383 |
DIGRAPH_TYPEDEFS(Graph); |
| 383 | 384 |
Graph gr; |
| 384 | 385 |
|
| 385 | 386 |
Node n0 = gr.addNode(); |
| 386 | 387 |
Node n1 = gr.addNode(); |
| 387 | 388 |
Node n2 = gr.addNode(); |
| 388 | 389 |
Node n3 = gr.addNode(); |
| 389 |
|
|
| 390 |
|
|
| 390 | 391 |
gr.addArc(n3, n0); |
| 391 | 392 |
gr.addArc(n3, n2); |
| 392 | 393 |
gr.addArc(n0, n2); |
| 393 | 394 |
gr.addArc(n2, n1); |
| 394 | 395 |
gr.addArc(n0, n1); |
| 395 |
|
|
| 396 |
|
|
| 396 | 397 |
{
|
| 397 | 398 |
std::vector<Node> v; |
| 398 | 399 |
dfs(gr).processedMap(loggerBoolMap(std::back_inserter(v))).run(); |
| 399 | 400 |
|
| 400 | 401 |
check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3, |
| 401 | 402 |
"Something is wrong with LoggerBoolMap"); |
| 402 | 403 |
} |
| 403 | 404 |
{
|
| 404 | 405 |
std::vector<Node> v(countNodes(gr)); |
| 405 | 406 |
dfs(gr).processedMap(loggerBoolMap(v.begin())).run(); |
| 406 |
|
|
| 407 |
|
|
| 407 | 408 |
check(v.size()==4 && v[0]==n1 && v[1]==n2 && v[2]==n0 && v[3]==n3, |
| 408 | 409 |
"Something is wrong with LoggerBoolMap"); |
| 409 | 410 |
} |
| 410 | 411 |
} |
| 411 |
|
|
| 412 |
|
|
| 412 | 413 |
// IdMap, RangeIdMap |
| 413 | 414 |
{
|
| 414 | 415 |
typedef ListDigraph Graph; |
| 415 | 416 |
DIGRAPH_TYPEDEFS(Graph); |
| 416 | 417 |
|
| 417 | 418 |
checkConcept<ReadMap<Node, int>, IdMap<Graph, Node> >(); |
| 418 | 419 |
checkConcept<ReadMap<Arc, int>, IdMap<Graph, Arc> >(); |
| 419 | 420 |
checkConcept<ReadMap<Node, int>, RangeIdMap<Graph, Node> >(); |
| 420 | 421 |
checkConcept<ReadMap<Arc, int>, RangeIdMap<Graph, Arc> >(); |
| 421 |
|
|
| 422 |
|
|
| 422 | 423 |
Graph gr; |
| 423 | 424 |
IdMap<Graph, Node> nmap(gr); |
| 424 | 425 |
IdMap<Graph, Arc> amap(gr); |
| 425 | 426 |
RangeIdMap<Graph, Node> nrmap(gr); |
| 426 | 427 |
RangeIdMap<Graph, Arc> armap(gr); |
| 427 |
|
|
| 428 |
|
|
| 428 | 429 |
Node n0 = gr.addNode(); |
| 429 | 430 |
Node n1 = gr.addNode(); |
| 430 | 431 |
Node n2 = gr.addNode(); |
| 431 |
|
|
| 432 |
|
|
| 432 | 433 |
Arc a0 = gr.addArc(n0, n1); |
| 433 | 434 |
Arc a1 = gr.addArc(n0, n2); |
| 434 | 435 |
Arc a2 = gr.addArc(n2, n1); |
| 435 | 436 |
Arc a3 = gr.addArc(n2, n0); |
| 436 |
|
|
| 437 |
|
|
| 437 | 438 |
check(nmap[n0] == gr.id(n0) && nmap(gr.id(n0)) == n0, "Wrong IdMap"); |
| 438 | 439 |
check(nmap[n1] == gr.id(n1) && nmap(gr.id(n1)) == n1, "Wrong IdMap"); |
| 439 | 440 |
check(nmap[n2] == gr.id(n2) && nmap(gr.id(n2)) == n2, "Wrong IdMap"); |
| 440 | 441 |
|
| 441 | 442 |
check(amap[a0] == gr.id(a0) && amap(gr.id(a0)) == a0, "Wrong IdMap"); |
| 442 | 443 |
check(amap[a1] == gr.id(a1) && amap(gr.id(a1)) == a1, "Wrong IdMap"); |
| 443 | 444 |
check(amap[a2] == gr.id(a2) && amap(gr.id(a2)) == a2, "Wrong IdMap"); |
| 444 | 445 |
check(amap[a3] == gr.id(a3) && amap(gr.id(a3)) == a3, "Wrong IdMap"); |
| 445 | 446 |
|
| 446 | 447 |
check(nmap.inverse()[gr.id(n0)] == n0, "Wrong IdMap::InverseMap"); |
| 447 | 448 |
check(amap.inverse()[gr.id(a0)] == a0, "Wrong IdMap::InverseMap"); |
| 448 |
|
|
| 449 |
|
|
| 449 | 450 |
check(nrmap.size() == 3 && armap.size() == 4, |
| 450 | 451 |
"Wrong RangeIdMap::size()"); |
| 451 | 452 |
|
| 452 | 453 |
check(nrmap[n0] == 0 && nrmap(0) == n0, "Wrong RangeIdMap"); |
| 453 | 454 |
check(nrmap[n1] == 1 && nrmap(1) == n1, "Wrong RangeIdMap"); |
| 454 | 455 |
check(nrmap[n2] == 2 && nrmap(2) == n2, "Wrong RangeIdMap"); |
| 455 |
|
|
| 456 |
|
|
| 456 | 457 |
check(armap[a0] == 0 && armap(0) == a0, "Wrong RangeIdMap"); |
| 457 | 458 |
check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap"); |
| 458 | 459 |
check(armap[a2] == 2 && armap(2) == a2, "Wrong RangeIdMap"); |
| 459 | 460 |
check(armap[a3] == 3 && armap(3) == a3, "Wrong RangeIdMap"); |
| 460 | 461 |
|
| 461 | 462 |
check(nrmap.inverse()[0] == n0, "Wrong RangeIdMap::InverseMap"); |
| 462 | 463 |
check(armap.inverse()[0] == a0, "Wrong RangeIdMap::InverseMap"); |
| 463 |
|
|
| 464 |
|
|
| 464 | 465 |
gr.erase(n1); |
| 465 |
|
|
| 466 |
|
|
| 466 | 467 |
if (nrmap[n0] == 1) nrmap.swap(n0, n2); |
| 467 | 468 |
nrmap.swap(n2, n0); |
| 468 | 469 |
if (armap[a1] == 1) armap.swap(a1, a3); |
| 469 | 470 |
armap.swap(a3, a1); |
| 470 |
|
|
| 471 |
|
|
| 471 | 472 |
check(nrmap.size() == 2 && armap.size() == 2, |
| 472 | 473 |
"Wrong RangeIdMap::size()"); |
| 473 | 474 |
|
| 474 | 475 |
check(nrmap[n0] == 1 && nrmap(1) == n0, "Wrong RangeIdMap"); |
| 475 | 476 |
check(nrmap[n2] == 0 && nrmap(0) == n2, "Wrong RangeIdMap"); |
| 476 |
|
|
| 477 |
|
|
| 477 | 478 |
check(armap[a1] == 1 && armap(1) == a1, "Wrong RangeIdMap"); |
| 478 | 479 |
check(armap[a3] == 0 && armap(0) == a3, "Wrong RangeIdMap"); |
| 479 | 480 |
|
| 480 | 481 |
check(nrmap.inverse()[0] == n2, "Wrong RangeIdMap::InverseMap"); |
| 481 | 482 |
check(armap.inverse()[0] == a3, "Wrong RangeIdMap::InverseMap"); |
| 482 | 483 |
} |
| 483 |
|
|
| 484 |
|
|
| 484 | 485 |
// SourceMap, TargetMap, ForwardMap, BackwardMap, InDegMap, OutDegMap |
| 485 | 486 |
{
|
| 486 | 487 |
typedef ListGraph Graph; |
| 487 | 488 |
GRAPH_TYPEDEFS(Graph); |
| 488 |
|
|
| 489 |
|
|
| 489 | 490 |
checkConcept<ReadMap<Arc, Node>, SourceMap<Graph> >(); |
| 490 | 491 |
checkConcept<ReadMap<Arc, Node>, TargetMap<Graph> >(); |
| 491 | 492 |
checkConcept<ReadMap<Edge, Arc>, ForwardMap<Graph> >(); |
| 492 | 493 |
checkConcept<ReadMap<Edge, Arc>, BackwardMap<Graph> >(); |
| 493 | 494 |
checkConcept<ReadMap<Node, int>, InDegMap<Graph> >(); |
| 494 | 495 |
checkConcept<ReadMap<Node, int>, OutDegMap<Graph> >(); |
| 495 | 496 |
|
| 496 | 497 |
Graph gr; |
| 497 | 498 |
Node n0 = gr.addNode(); |
| 498 | 499 |
Node n1 = gr.addNode(); |
| 499 | 500 |
Node n2 = gr.addNode(); |
| 500 |
|
|
| 501 |
|
|
| 501 | 502 |
gr.addEdge(n0,n1); |
| 502 | 503 |
gr.addEdge(n1,n2); |
| 503 | 504 |
gr.addEdge(n0,n2); |
| 504 | 505 |
gr.addEdge(n2,n1); |
| 505 | 506 |
gr.addEdge(n1,n2); |
| 506 | 507 |
gr.addEdge(n0,n1); |
| 507 |
|
|
| 508 |
|
|
| 508 | 509 |
for (EdgeIt e(gr); e != INVALID; ++e) {
|
| 509 | 510 |
check(forwardMap(gr)[e] == gr.direct(e, true), "Wrong ForwardMap"); |
| 510 | 511 |
check(backwardMap(gr)[e] == gr.direct(e, false), "Wrong BackwardMap"); |
| 511 | 512 |
} |
| 512 |
|
|
| 513 |
|
|
| 513 | 514 |
check(mapCompare(gr, |
| 514 | 515 |
sourceMap(orienter(gr, constMap<Edge, bool>(true))), |
| 515 | 516 |
targetMap(orienter(gr, constMap<Edge, bool>(false)))), |
| 516 | 517 |
"Wrong SourceMap or TargetMap"); |
| 517 | 518 |
|
| 518 | 519 |
typedef Orienter<Graph, const ConstMap<Edge, bool> > Digraph; |
| 519 | 520 |
Digraph dgr(gr, constMap<Edge, bool>(true)); |
| 520 | 521 |
OutDegMap<Digraph> odm(dgr); |
| 521 | 522 |
InDegMap<Digraph> idm(dgr); |
| 522 |
|
|
| 523 |
|
|
| 523 | 524 |
check(odm[n0] == 3 && odm[n1] == 2 && odm[n2] == 1, "Wrong OutDegMap"); |
| 524 | 525 |
check(idm[n0] == 0 && idm[n1] == 3 && idm[n2] == 3, "Wrong InDegMap"); |
| 525 |
|
|
| 526 |
|
|
| 526 | 527 |
gr.addEdge(n2, n0); |
| 527 | 528 |
|
| 528 | 529 |
check(odm[n0] == 3 && odm[n1] == 2 && odm[n2] == 2, "Wrong OutDegMap"); |
| 529 | 530 |
check(idm[n0] == 1 && idm[n1] == 3 && idm[n2] == 3, "Wrong InDegMap"); |
| 530 | 531 |
} |
| 531 |
|
|
| 532 |
|
|
| 532 | 533 |
// CrossRefMap |
| 533 | 534 |
{
|
| 534 | 535 |
typedef ListDigraph Graph; |
| 535 | 536 |
DIGRAPH_TYPEDEFS(Graph); |
| 536 | 537 |
|
| 537 | 538 |
checkConcept<ReadWriteMap<Node, int>, |
| 538 | 539 |
CrossRefMap<Graph, Node, int> >(); |
| 539 | 540 |
checkConcept<ReadWriteMap<Node, bool>, |
| 540 | 541 |
CrossRefMap<Graph, Node, bool> >(); |
| 541 | 542 |
checkConcept<ReadWriteMap<Node, double>, |
| 542 | 543 |
CrossRefMap<Graph, Node, double> >(); |
| 543 |
|
|
| 544 |
|
|
| 544 | 545 |
Graph gr; |
| 545 | 546 |
typedef CrossRefMap<Graph, Node, char> CRMap; |
| 546 | 547 |
CRMap map(gr); |
| 547 |
|
|
| 548 |
|
|
| 548 | 549 |
Node n0 = gr.addNode(); |
| 549 | 550 |
Node n1 = gr.addNode(); |
| 550 | 551 |
Node n2 = gr.addNode(); |
| 551 |
|
|
| 552 |
|
|
| 552 | 553 |
map.set(n0, 'A'); |
| 553 | 554 |
map.set(n1, 'B'); |
| 554 | 555 |
map.set(n2, 'C'); |
| 555 |
|
|
| 556 |
|
|
| 556 | 557 |
check(map[n0] == 'A' && map('A') == n0 && map.inverse()['A'] == n0,
|
| 557 | 558 |
"Wrong CrossRefMap"); |
| 558 | 559 |
check(map[n1] == 'B' && map('B') == n1 && map.inverse()['B'] == n1,
|
| 559 | 560 |
"Wrong CrossRefMap"); |
| 560 | 561 |
check(map[n2] == 'C' && map('C') == n2 && map.inverse()['C'] == n2,
|
| 561 | 562 |
"Wrong CrossRefMap"); |
| 562 | 563 |
check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
|
| 563 | 564 |
"Wrong CrossRefMap::count()"); |
| 564 |
|
|
| 565 |
|
|
| 565 | 566 |
CRMap::ValueIt it = map.beginValue(); |
| 566 | 567 |
check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' && |
| 567 | 568 |
it == map.endValue(), "Wrong value iterator"); |
| 568 |
|
|
| 569 |
|
|
| 569 | 570 |
map.set(n2, 'A'); |
| 570 | 571 |
|
| 571 | 572 |
check(map[n0] == 'A' && map[n1] == 'B' && map[n2] == 'A', |
| 572 | 573 |
"Wrong CrossRefMap"); |
| 573 | 574 |
check(map('A') == n0 && map.inverse()['A'] == n0, "Wrong CrossRefMap");
|
| 574 | 575 |
check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
|
| 575 | 576 |
check(map('C') == INVALID && map.inverse()['C'] == INVALID,
|
| 576 | 577 |
"Wrong CrossRefMap"); |
| 577 | 578 |
check(map.count('A') == 2 && map.count('B') == 1 && map.count('C') == 0,
|
| 578 | 579 |
"Wrong CrossRefMap::count()"); |
| 579 | 580 |
|
| 580 | 581 |
it = map.beginValue(); |
| 581 | 582 |
check(*it++ == 'A' && *it++ == 'A' && *it++ == 'B' && |
| 582 | 583 |
it == map.endValue(), "Wrong value iterator"); |
| 583 | 584 |
|
| 584 | 585 |
map.set(n0, 'C'); |
| 585 | 586 |
|
| 586 | 587 |
check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A', |
| 587 | 588 |
"Wrong CrossRefMap"); |
| 588 | 589 |
check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
|
| 589 | 590 |
check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
|
| 590 | 591 |
check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
|
| 591 | 592 |
check(map.count('A') == 1 && map.count('B') == 1 && map.count('C') == 1,
|
| 592 | 593 |
"Wrong CrossRefMap::count()"); |
| 593 | 594 |
|
| 594 | 595 |
it = map.beginValue(); |
| 595 | 596 |
check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' && |
| 596 | 597 |
it == map.endValue(), "Wrong value iterator"); |
| 597 | 598 |
} |
| 598 | 599 |
|
| 599 | 600 |
// CrossRefMap |
| 600 | 601 |
{
|
| 601 | 602 |
typedef SmartDigraph Graph; |
| 602 | 603 |
DIGRAPH_TYPEDEFS(Graph); |
| 603 | 604 |
|
| 604 | 605 |
checkConcept<ReadWriteMap<Node, int>, |
| 605 | 606 |
CrossRefMap<Graph, Node, int> >(); |
| 606 |
|
|
| 607 |
|
|
| 607 | 608 |
Graph gr; |
| 608 | 609 |
typedef CrossRefMap<Graph, Node, char> CRMap; |
| 609 | 610 |
typedef CRMap::ValueIterator ValueIt; |
| 610 | 611 |
CRMap map(gr); |
| 611 |
|
|
| 612 |
|
|
| 612 | 613 |
Node n0 = gr.addNode(); |
| 613 | 614 |
Node n1 = gr.addNode(); |
| 614 | 615 |
Node n2 = gr.addNode(); |
| 615 |
|
|
| 616 |
|
|
| 616 | 617 |
map.set(n0, 'A'); |
| 617 | 618 |
map.set(n1, 'B'); |
| 618 | 619 |
map.set(n2, 'C'); |
| 619 | 620 |
map.set(n2, 'A'); |
| 620 | 621 |
map.set(n0, 'C'); |
| 621 | 622 |
|
| 622 | 623 |
check(map[n0] == 'C' && map[n1] == 'B' && map[n2] == 'A', |
| 623 | 624 |
"Wrong CrossRefMap"); |
| 624 | 625 |
check(map('A') == n2 && map.inverse()['A'] == n2, "Wrong CrossRefMap");
|
| 625 | 626 |
check(map('B') == n1 && map.inverse()['B'] == n1, "Wrong CrossRefMap");
|
| 626 | 627 |
check(map('C') == n0 && map.inverse()['C'] == n0, "Wrong CrossRefMap");
|
| 627 | 628 |
|
| 628 | 629 |
ValueIt it = map.beginValue(); |
| 629 | 630 |
check(*it++ == 'A' && *it++ == 'B' && *it++ == 'C' && |
| 630 | 631 |
it == map.endValue(), "Wrong value iterator"); |
| 631 | 632 |
} |
| 632 |
|
|
| 633 |
|
|
| 633 | 634 |
// Iterable bool map |
| 634 | 635 |
{
|
| 635 | 636 |
typedef SmartGraph Graph; |
| 636 | 637 |
typedef SmartGraph::Node Item; |
| 637 | 638 |
|
| 638 | 639 |
typedef IterableBoolMap<SmartGraph, SmartGraph::Node> Ibm; |
| 639 | 640 |
checkConcept<ReferenceMap<Item, bool, bool&, const bool&>, Ibm>(); |
| 640 | 641 |
|
| 641 | 642 |
const int num = 10; |
| 642 | 643 |
Graph g; |
| 643 | 644 |
std::vector<Item> items; |
| 644 | 645 |
for (int i = 0; i < num; ++i) {
|
| 645 | 646 |
items.push_back(g.addNode()); |
| 646 | 647 |
} |
| 647 | 648 |
|
| 648 | 649 |
Ibm map1(g, true); |
| 649 | 650 |
int n = 0; |
| 650 | 651 |
for (Ibm::TrueIt it(map1); it != INVALID; ++it) {
|
| 651 | 652 |
check(map1[static_cast<Item>(it)], "Wrong TrueIt"); |
| 652 | 653 |
++n; |
| 653 | 654 |
} |
| 654 | 655 |
check(n == num, "Wrong number"); |
| 655 | 656 |
|
| 656 | 657 |
n = 0; |
| ... | ... |
@@ -796,89 +797,89 @@ |
| 796 | 797 |
vit != map1.endValue(); ++vit) {
|
| 797 | 798 |
check(map1[static_cast<Item>(Ivm::ItemIt(map1, *vit))] == *vit, |
| 798 | 799 |
"Wrong ValueIt"); |
| 799 | 800 |
} |
| 800 | 801 |
|
| 801 | 802 |
for (int i = 0; i < num; ++i) {
|
| 802 | 803 |
map1.set(items[i], static_cast<double>(i % 2)); |
| 803 | 804 |
} |
| 804 | 805 |
check(distance(map1.beginValue(), map1.endValue()) == 2, "Wrong size"); |
| 805 | 806 |
|
| 806 | 807 |
int n = 0; |
| 807 | 808 |
for (Ivm::ItemIt it(map1, 0.0); it != INVALID; ++it) {
|
| 808 | 809 |
check(map1[static_cast<Item>(it)] == 0.0, "Wrong value"); |
| 809 | 810 |
++n; |
| 810 | 811 |
} |
| 811 | 812 |
check(n == (num + 1) / 2, "Wrong number"); |
| 812 | 813 |
|
| 813 | 814 |
for (Ivm::ItemIt it(map1, 1.0); it != INVALID; ++it) {
|
| 814 | 815 |
check(map1[static_cast<Item>(it)] == 1.0, "Wrong value"); |
| 815 | 816 |
++n; |
| 816 | 817 |
} |
| 817 | 818 |
check(n == num, "Wrong number"); |
| 818 | 819 |
|
| 819 | 820 |
} |
| 820 |
|
|
| 821 |
|
|
| 821 | 822 |
// Graph map utilities: |
| 822 | 823 |
// mapMin(), mapMax(), mapMinValue(), mapMaxValue() |
| 823 | 824 |
// mapFind(), mapFindIf(), mapCount(), mapCountIf() |
| 824 | 825 |
// mapCopy(), mapCompare(), mapFill() |
| 825 | 826 |
{
|
| 826 | 827 |
DIGRAPH_TYPEDEFS(SmartDigraph); |
| 827 | 828 |
|
| 828 | 829 |
SmartDigraph g; |
| 829 | 830 |
Node n1 = g.addNode(); |
| 830 | 831 |
Node n2 = g.addNode(); |
| 831 | 832 |
Node n3 = g.addNode(); |
| 832 |
|
|
| 833 |
|
|
| 833 | 834 |
SmartDigraph::NodeMap<int> map1(g); |
| 834 | 835 |
SmartDigraph::ArcMap<char> map2(g); |
| 835 | 836 |
ConstMap<Node, A> cmap1 = A(); |
| 836 | 837 |
ConstMap<Arc, C> cmap2 = C(0); |
| 837 |
|
|
| 838 |
|
|
| 838 | 839 |
map1[n1] = 10; |
| 839 | 840 |
map1[n2] = 5; |
| 840 | 841 |
map1[n3] = 12; |
| 841 |
|
|
| 842 |
|
|
| 842 | 843 |
// mapMin(), mapMax(), mapMinValue(), mapMaxValue() |
| 843 | 844 |
check(mapMin(g, map1) == n2, "Wrong mapMin()"); |
| 844 | 845 |
check(mapMax(g, map1) == n3, "Wrong mapMax()"); |
| 845 | 846 |
check(mapMin(g, map1, std::greater<int>()) == n3, "Wrong mapMin()"); |
| 846 | 847 |
check(mapMax(g, map1, std::greater<int>()) == n2, "Wrong mapMax()"); |
| 847 | 848 |
check(mapMinValue(g, map1) == 5, "Wrong mapMinValue()"); |
| 848 | 849 |
check(mapMaxValue(g, map1) == 12, "Wrong mapMaxValue()"); |
| 849 | 850 |
|
| 850 | 851 |
check(mapMin(g, map2) == INVALID, "Wrong mapMin()"); |
| 851 | 852 |
check(mapMax(g, map2) == INVALID, "Wrong mapMax()"); |
| 852 | 853 |
|
| 853 | 854 |
check(mapMin(g, cmap1) != INVALID, "Wrong mapMin()"); |
| 854 | 855 |
check(mapMax(g, cmap2) == INVALID, "Wrong mapMax()"); |
| 855 | 856 |
|
| 856 | 857 |
Arc a1 = g.addArc(n1, n2); |
| 857 | 858 |
Arc a2 = g.addArc(n1, n3); |
| 858 | 859 |
Arc a3 = g.addArc(n2, n3); |
| 859 | 860 |
Arc a4 = g.addArc(n3, n1); |
| 860 |
|
|
| 861 |
|
|
| 861 | 862 |
map2[a1] = 'b'; |
| 862 | 863 |
map2[a2] = 'a'; |
| 863 | 864 |
map2[a3] = 'b'; |
| 864 | 865 |
map2[a4] = 'c'; |
| 865 | 866 |
|
| 866 | 867 |
// mapMin(), mapMax(), mapMinValue(), mapMaxValue() |
| 867 | 868 |
check(mapMin(g, map2) == a2, "Wrong mapMin()"); |
| 868 | 869 |
check(mapMax(g, map2) == a4, "Wrong mapMax()"); |
| 869 | 870 |
check(mapMin(g, map2, std::greater<int>()) == a4, "Wrong mapMin()"); |
| 870 | 871 |
check(mapMax(g, map2, std::greater<int>()) == a2, "Wrong mapMax()"); |
| 871 | 872 |
check(mapMinValue(g, map2, std::greater<int>()) == 'c', |
| 872 | 873 |
"Wrong mapMinValue()"); |
| 873 | 874 |
check(mapMaxValue(g, map2, std::greater<int>()) == 'a', |
| 874 | 875 |
"Wrong mapMaxValue()"); |
| 875 | 876 |
|
| 876 | 877 |
check(mapMin(g, cmap1) != INVALID, "Wrong mapMin()"); |
| 877 | 878 |
check(mapMax(g, cmap2) != INVALID, "Wrong mapMax()"); |
| 878 | 879 |
check(mapMaxValue(g, cmap2) == C(0), "Wrong mapMaxValue()"); |
| 879 | 880 |
|
| 880 | 881 |
check(mapMin(g, composeMap(functorToMap(&createC), map2)) == a2, |
| 881 | 882 |
"Wrong mapMin()"); |
| 882 | 883 |
check(mapMax(g, composeMap(functorToMap(&createC), map2)) == a4, |
| 883 | 884 |
"Wrong mapMax()"); |
| 884 | 885 |
check(mapMinValue(g, composeMap(functorToMap(&createC), map2)) == C('a'),
|
| ... | ... |
@@ -903,97 +904,97 @@ |
| 903 | 904 |
check(mapFindIf(g, map2, Less<char>('a')) == INVALID,
|
| 904 | 905 |
"Wrong mapFindIf()"); |
| 905 | 906 |
|
| 906 | 907 |
// mapCount(), mapCountIf() |
| 907 | 908 |
check(mapCount(g, map1, 5) == 1, "Wrong mapCount()"); |
| 908 | 909 |
check(mapCount(g, map1, 6) == 0, "Wrong mapCount()"); |
| 909 | 910 |
check(mapCount(g, map2, 'a') == 1, "Wrong mapCount()"); |
| 910 | 911 |
check(mapCount(g, map2, 'b') == 2, "Wrong mapCount()"); |
| 911 | 912 |
check(mapCount(g, map2, 'e') == 0, "Wrong mapCount()"); |
| 912 | 913 |
check(mapCount(g, cmap2, C(0)) == 4, "Wrong mapCount()"); |
| 913 | 914 |
check(mapCount(g, cmap2, C(1)) == 0, "Wrong mapCount()"); |
| 914 | 915 |
|
| 915 | 916 |
check(mapCountIf(g, map1, Less<int>(11)) == 2, |
| 916 | 917 |
"Wrong mapCountIf()"); |
| 917 | 918 |
check(mapCountIf(g, map1, Less<int>(13)) == 3, |
| 918 | 919 |
"Wrong mapCountIf()"); |
| 919 | 920 |
check(mapCountIf(g, map1, Less<int>(5)) == 0, |
| 920 | 921 |
"Wrong mapCountIf()"); |
| 921 | 922 |
check(mapCountIf(g, map2, Less<char>('d')) == 4,
|
| 922 | 923 |
"Wrong mapCountIf()"); |
| 923 | 924 |
check(mapCountIf(g, map2, Less<char>('c')) == 3,
|
| 924 | 925 |
"Wrong mapCountIf()"); |
| 925 | 926 |
check(mapCountIf(g, map2, Less<char>('a')) == 0,
|
| 926 | 927 |
"Wrong mapCountIf()"); |
| 927 |
|
|
| 928 |
|
|
| 928 | 929 |
// MapIt, ConstMapIt |
| 929 | 930 |
/* |
| 930 | 931 |
These tests can be used after applying bugfix #330 |
| 931 | 932 |
typedef SmartDigraph::NodeMap<int>::MapIt MapIt; |
| 932 | 933 |
typedef SmartDigraph::NodeMap<int>::ConstMapIt ConstMapIt; |
| 933 | 934 |
check(*std::min_element(MapIt(map1), MapIt(INVALID)) == 5, |
| 934 | 935 |
"Wrong NodeMap<>::MapIt"); |
| 935 | 936 |
check(*std::max_element(ConstMapIt(map1), ConstMapIt(INVALID)) == 12, |
| 936 | 937 |
"Wrong NodeMap<>::MapIt"); |
| 937 |
|
|
| 938 |
|
|
| 938 | 939 |
int sum = 0; |
| 939 | 940 |
std::for_each(MapIt(map1), MapIt(INVALID), Sum<int>(sum)); |
| 940 | 941 |
check(sum == 27, "Wrong NodeMap<>::MapIt"); |
| 941 | 942 |
std::for_each(ConstMapIt(map1), ConstMapIt(INVALID), Sum<int>(sum)); |
| 942 | 943 |
check(sum == 54, "Wrong NodeMap<>::ConstMapIt"); |
| 943 | 944 |
*/ |
| 944 | 945 |
|
| 945 | 946 |
// mapCopy(), mapCompare(), mapFill() |
| 946 | 947 |
check(mapCompare(g, map1, map1), "Wrong mapCompare()"); |
| 947 | 948 |
check(mapCompare(g, cmap2, cmap2), "Wrong mapCompare()"); |
| 948 | 949 |
check(mapCompare(g, map1, shiftMap(map1, 0)), "Wrong mapCompare()"); |
| 949 | 950 |
check(mapCompare(g, map2, scaleMap(map2, 1)), "Wrong mapCompare()"); |
| 950 | 951 |
check(!mapCompare(g, map1, shiftMap(map1, 1)), "Wrong mapCompare()"); |
| 951 | 952 |
|
| 952 | 953 |
SmartDigraph::NodeMap<int> map3(g, 0); |
| 953 | 954 |
SmartDigraph::ArcMap<char> map4(g, 'a'); |
| 954 |
|
|
| 955 |
|
|
| 955 | 956 |
check(!mapCompare(g, map1, map3), "Wrong mapCompare()"); |
| 956 |
check(!mapCompare(g, map2, map4), "Wrong mapCompare()"); |
|
| 957 |
|
|
| 957 |
check(!mapCompare(g, map2, map4), "Wrong mapCompare()"); |
|
| 958 |
|
|
| 958 | 959 |
mapCopy(g, map1, map3); |
| 959 | 960 |
mapCopy(g, map2, map4); |
| 960 | 961 |
|
| 961 | 962 |
check(mapCompare(g, map1, map3), "Wrong mapCompare() or mapCopy()"); |
| 962 |
check(mapCompare(g, map2, map4), "Wrong mapCompare() or mapCopy()"); |
|
| 963 |
|
|
| 963 |
check(mapCompare(g, map2, map4), "Wrong mapCompare() or mapCopy()"); |
|
| 964 |
|
|
| 964 | 965 |
Undirector<SmartDigraph> ug(g); |
| 965 | 966 |
Undirector<SmartDigraph>::EdgeMap<char> umap1(ug, 'x'); |
| 966 | 967 |
Undirector<SmartDigraph>::ArcMap<double> umap2(ug, 3.14); |
| 967 |
|
|
| 968 |
|
|
| 968 | 969 |
check(!mapCompare(g, map2, umap1), "Wrong mapCompare() or mapCopy()"); |
| 969 | 970 |
check(!mapCompare(g, umap1, map2), "Wrong mapCompare() or mapCopy()"); |
| 970 | 971 |
check(!mapCompare(ug, map2, umap1), "Wrong mapCompare() or mapCopy()"); |
| 971 | 972 |
check(!mapCompare(ug, umap1, map2), "Wrong mapCompare() or mapCopy()"); |
| 972 |
|
|
| 973 |
|
|
| 973 | 974 |
mapCopy(g, map2, umap1); |
| 974 | 975 |
|
| 975 | 976 |
check(mapCompare(g, map2, umap1), "Wrong mapCompare() or mapCopy()"); |
| 976 | 977 |
check(mapCompare(g, umap1, map2), "Wrong mapCompare() or mapCopy()"); |
| 977 | 978 |
check(mapCompare(ug, map2, umap1), "Wrong mapCompare() or mapCopy()"); |
| 978 | 979 |
check(mapCompare(ug, umap1, map2), "Wrong mapCompare() or mapCopy()"); |
| 979 |
|
|
| 980 |
|
|
| 980 | 981 |
mapCopy(g, map2, umap1); |
| 981 | 982 |
mapCopy(g, umap1, map2); |
| 982 | 983 |
mapCopy(ug, map2, umap1); |
| 983 | 984 |
mapCopy(ug, umap1, map2); |
| 984 |
|
|
| 985 |
|
|
| 985 | 986 |
check(!mapCompare(ug, umap1, umap2), "Wrong mapCompare() or mapCopy()"); |
| 986 | 987 |
mapCopy(ug, umap1, umap2); |
| 987 | 988 |
check(mapCompare(ug, umap1, umap2), "Wrong mapCompare() or mapCopy()"); |
| 988 |
|
|
| 989 |
|
|
| 989 | 990 |
check(!mapCompare(g, map1, constMap<Node>(2)), "Wrong mapCompare()"); |
| 990 | 991 |
mapFill(g, map1, 2); |
| 991 | 992 |
check(mapCompare(g, constMap<Node>(2), map1), "Wrong mapFill()"); |
| 992 | 993 |
|
| 993 | 994 |
check(!mapCompare(g, map2, constMap<Arc>('z')), "Wrong mapCompare()");
|
| 994 | 995 |
mapCopy(g, constMap<Arc>('z'), map2);
|
| 995 | 996 |
check(mapCompare(g, constMap<Arc>('z'), map2), "Wrong mapCopy()");
|
| 996 | 997 |
} |
| 997 |
|
|
| 998 |
|
|
| 998 | 999 |
return 0; |
| 999 | 1000 |
} |
| 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 <iostream> |
| 20 | 20 |
#include <sstream> |
| 21 | 21 |
#include <vector> |
| 22 | 22 |
#include <queue> |
| 23 | 23 |
#include <cstdlib> |
| 24 | 24 |
|
| 25 | 25 |
#include <lemon/matching.h> |
| 26 | 26 |
#include <lemon/smart_graph.h> |
| 27 | 27 |
#include <lemon/concepts/graph.h> |
| 28 | 28 |
#include <lemon/concepts/maps.h> |
| 29 | 29 |
#include <lemon/lgf_reader.h> |
| ... | ... |
@@ -113,130 +113,130 @@ |
| 113 | 113 |
}; |
| 114 | 114 |
|
| 115 | 115 |
void checkMaxMatchingCompile() |
| 116 | 116 |
{
|
| 117 | 117 |
typedef concepts::Graph Graph; |
| 118 | 118 |
typedef Graph::Node Node; |
| 119 | 119 |
typedef Graph::Edge Edge; |
| 120 | 120 |
typedef Graph::EdgeMap<bool> MatMap; |
| 121 | 121 |
|
| 122 | 122 |
Graph g; |
| 123 | 123 |
Node n; |
| 124 | 124 |
Edge e; |
| 125 | 125 |
MatMap mat(g); |
| 126 | 126 |
|
| 127 | 127 |
MaxMatching<Graph> mat_test(g); |
| 128 | 128 |
const MaxMatching<Graph>& |
| 129 | 129 |
const_mat_test = mat_test; |
| 130 | 130 |
|
| 131 | 131 |
mat_test.init(); |
| 132 | 132 |
mat_test.greedyInit(); |
| 133 | 133 |
mat_test.matchingInit(mat); |
| 134 | 134 |
mat_test.startSparse(); |
| 135 | 135 |
mat_test.startDense(); |
| 136 | 136 |
mat_test.run(); |
| 137 |
|
|
| 137 |
|
|
| 138 | 138 |
const_mat_test.matchingSize(); |
| 139 | 139 |
const_mat_test.matching(e); |
| 140 | 140 |
const_mat_test.matching(n); |
| 141 | 141 |
const MaxMatching<Graph>::MatchingMap& mmap = |
| 142 | 142 |
const_mat_test.matchingMap(); |
| 143 | 143 |
e = mmap[n]; |
| 144 | 144 |
const_mat_test.mate(n); |
| 145 | 145 |
|
| 146 |
MaxMatching<Graph>::Status stat = |
|
| 146 |
MaxMatching<Graph>::Status stat = |
|
| 147 | 147 |
const_mat_test.status(n); |
| 148 | 148 |
const MaxMatching<Graph>::StatusMap& smap = |
| 149 | 149 |
const_mat_test.statusMap(); |
| 150 | 150 |
stat = smap[n]; |
| 151 | 151 |
const_mat_test.barrier(n); |
| 152 | 152 |
} |
| 153 | 153 |
|
| 154 | 154 |
void checkMaxWeightedMatchingCompile() |
| 155 | 155 |
{
|
| 156 | 156 |
typedef concepts::Graph Graph; |
| 157 | 157 |
typedef Graph::Node Node; |
| 158 | 158 |
typedef Graph::Edge Edge; |
| 159 | 159 |
typedef Graph::EdgeMap<int> WeightMap; |
| 160 | 160 |
|
| 161 | 161 |
Graph g; |
| 162 | 162 |
Node n; |
| 163 | 163 |
Edge e; |
| 164 | 164 |
WeightMap w(g); |
| 165 | 165 |
|
| 166 | 166 |
MaxWeightedMatching<Graph> mat_test(g, w); |
| 167 | 167 |
const MaxWeightedMatching<Graph>& |
| 168 | 168 |
const_mat_test = mat_test; |
| 169 | 169 |
|
| 170 | 170 |
mat_test.init(); |
| 171 | 171 |
mat_test.start(); |
| 172 | 172 |
mat_test.run(); |
| 173 |
|
|
| 173 |
|
|
| 174 | 174 |
const_mat_test.matchingWeight(); |
| 175 | 175 |
const_mat_test.matchingSize(); |
| 176 | 176 |
const_mat_test.matching(e); |
| 177 | 177 |
const_mat_test.matching(n); |
| 178 | 178 |
const MaxWeightedMatching<Graph>::MatchingMap& mmap = |
| 179 | 179 |
const_mat_test.matchingMap(); |
| 180 | 180 |
e = mmap[n]; |
| 181 | 181 |
const_mat_test.mate(n); |
| 182 |
|
|
| 182 |
|
|
| 183 | 183 |
int k = 0; |
| 184 | 184 |
const_mat_test.dualValue(); |
| 185 | 185 |
const_mat_test.nodeValue(n); |
| 186 | 186 |
const_mat_test.blossomNum(); |
| 187 | 187 |
const_mat_test.blossomSize(k); |
| 188 | 188 |
const_mat_test.blossomValue(k); |
| 189 | 189 |
} |
| 190 | 190 |
|
| 191 | 191 |
void checkMaxWeightedPerfectMatchingCompile() |
| 192 | 192 |
{
|
| 193 | 193 |
typedef concepts::Graph Graph; |
| 194 | 194 |
typedef Graph::Node Node; |
| 195 | 195 |
typedef Graph::Edge Edge; |
| 196 | 196 |
typedef Graph::EdgeMap<int> WeightMap; |
| 197 | 197 |
|
| 198 | 198 |
Graph g; |
| 199 | 199 |
Node n; |
| 200 | 200 |
Edge e; |
| 201 | 201 |
WeightMap w(g); |
| 202 | 202 |
|
| 203 | 203 |
MaxWeightedPerfectMatching<Graph> mat_test(g, w); |
| 204 | 204 |
const MaxWeightedPerfectMatching<Graph>& |
| 205 | 205 |
const_mat_test = mat_test; |
| 206 | 206 |
|
| 207 | 207 |
mat_test.init(); |
| 208 | 208 |
mat_test.start(); |
| 209 | 209 |
mat_test.run(); |
| 210 |
|
|
| 210 |
|
|
| 211 | 211 |
const_mat_test.matchingWeight(); |
| 212 | 212 |
const_mat_test.matching(e); |
| 213 | 213 |
const_mat_test.matching(n); |
| 214 | 214 |
const MaxWeightedPerfectMatching<Graph>::MatchingMap& mmap = |
| 215 | 215 |
const_mat_test.matchingMap(); |
| 216 | 216 |
e = mmap[n]; |
| 217 | 217 |
const_mat_test.mate(n); |
| 218 |
|
|
| 218 |
|
|
| 219 | 219 |
int k = 0; |
| 220 | 220 |
const_mat_test.dualValue(); |
| 221 | 221 |
const_mat_test.nodeValue(n); |
| 222 | 222 |
const_mat_test.blossomNum(); |
| 223 | 223 |
const_mat_test.blossomSize(k); |
| 224 | 224 |
const_mat_test.blossomValue(k); |
| 225 | 225 |
} |
| 226 | 226 |
|
| 227 | 227 |
void checkMatching(const SmartGraph& graph, |
| 228 | 228 |
const MaxMatching<SmartGraph>& mm) {
|
| 229 | 229 |
int num = 0; |
| 230 | 230 |
|
| 231 | 231 |
IntNodeMap comp_index(graph); |
| 232 | 232 |
UnionFind<IntNodeMap> comp(comp_index); |
| 233 | 233 |
|
| 234 | 234 |
int barrier_num = 0; |
| 235 | 235 |
|
| 236 | 236 |
for (NodeIt n(graph); n != INVALID; ++n) {
|
| 237 | 237 |
check(mm.status(n) == MaxMatching<SmartGraph>::EVEN || |
| 238 | 238 |
mm.matching(n) != INVALID, "Wrong Gallai-Edmonds decomposition"); |
| 239 | 239 |
if (mm.status(n) == MaxMatching<SmartGraph>::ODD) {
|
| 240 | 240 |
++barrier_num; |
| 241 | 241 |
} else {
|
| 242 | 242 |
comp.insert(n); |
| ... | ... |
@@ -404,45 +404,45 @@ |
| 404 | 404 |
bool perfect; |
| 405 | 405 |
{
|
| 406 | 406 |
MaxMatching<SmartGraph> mm(graph); |
| 407 | 407 |
mm.run(); |
| 408 | 408 |
checkMatching(graph, mm); |
| 409 | 409 |
perfect = 2 * mm.matchingSize() == countNodes(graph); |
| 410 | 410 |
} |
| 411 | 411 |
|
| 412 | 412 |
{
|
| 413 | 413 |
MaxWeightedMatching<SmartGraph> mwm(graph, weight); |
| 414 | 414 |
mwm.run(); |
| 415 | 415 |
checkWeightedMatching(graph, weight, mwm); |
| 416 | 416 |
} |
| 417 | 417 |
|
| 418 | 418 |
{
|
| 419 | 419 |
MaxWeightedMatching<SmartGraph> mwm(graph, weight); |
| 420 | 420 |
mwm.init(); |
| 421 | 421 |
mwm.start(); |
| 422 | 422 |
checkWeightedMatching(graph, weight, mwm); |
| 423 | 423 |
} |
| 424 | 424 |
|
| 425 | 425 |
{
|
| 426 | 426 |
MaxWeightedPerfectMatching<SmartGraph> mwpm(graph, weight); |
| 427 | 427 |
bool result = mwpm.run(); |
| 428 |
|
|
| 428 |
|
|
| 429 | 429 |
check(result == perfect, "Perfect matching found"); |
| 430 | 430 |
if (perfect) {
|
| 431 | 431 |
checkWeightedPerfectMatching(graph, weight, mwpm); |
| 432 | 432 |
} |
| 433 | 433 |
} |
| 434 | 434 |
|
| 435 | 435 |
{
|
| 436 | 436 |
MaxWeightedPerfectMatching<SmartGraph> mwpm(graph, weight); |
| 437 | 437 |
mwpm.init(); |
| 438 | 438 |
bool result = mwpm.start(); |
| 439 |
|
|
| 439 |
|
|
| 440 | 440 |
check(result == perfect, "Perfect matching found"); |
| 441 | 441 |
if (perfect) {
|
| 442 | 442 |
checkWeightedPerfectMatching(graph, weight, mwpm); |
| 443 | 443 |
} |
| 444 | 444 |
} |
| 445 | 445 |
} |
| 446 | 446 |
|
| 447 | 447 |
return 0; |
| 448 | 448 |
} |
| 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 <iostream> |
| 20 | 20 |
#include <set> |
| 21 | 21 |
#include <vector> |
| 22 | 22 |
#include <iterator> |
| 23 | 23 |
|
| 24 | 24 |
#include <lemon/smart_graph.h> |
| 25 | 25 |
#include <lemon/min_cost_arborescence.h> |
| 26 | 26 |
#include <lemon/lgf_reader.h> |
| 27 | 27 |
#include <lemon/concepts/digraph.h> |
| 28 | 28 |
|
| 29 | 29 |
#include "test_tools.h" |
| ... | ... |
@@ -89,64 +89,64 @@ |
| 89 | 89 |
Digraph g; |
| 90 | 90 |
Node s, n; |
| 91 | 91 |
Arc e; |
| 92 | 92 |
VType c; |
| 93 | 93 |
bool b; |
| 94 | 94 |
int i; |
| 95 | 95 |
CostMap cost; |
| 96 | 96 |
ArbMap arb; |
| 97 | 97 |
PredMap pred; |
| 98 | 98 |
|
| 99 | 99 |
MinCostArbType mcarb_test(g, cost); |
| 100 | 100 |
const MinCostArbType& const_mcarb_test = mcarb_test; |
| 101 | 101 |
|
| 102 | 102 |
mcarb_test |
| 103 | 103 |
.arborescenceMap(arb) |
| 104 | 104 |
.predMap(pred) |
| 105 | 105 |
.run(s); |
| 106 | 106 |
|
| 107 | 107 |
mcarb_test.init(); |
| 108 | 108 |
mcarb_test.addSource(s); |
| 109 | 109 |
mcarb_test.start(); |
| 110 | 110 |
n = mcarb_test.processNextNode(); |
| 111 | 111 |
b = const_mcarb_test.emptyQueue(); |
| 112 | 112 |
i = const_mcarb_test.queueSize(); |
| 113 |
|
|
| 113 |
|
|
| 114 | 114 |
c = const_mcarb_test.arborescenceCost(); |
| 115 | 115 |
b = const_mcarb_test.arborescence(e); |
| 116 | 116 |
e = const_mcarb_test.pred(n); |
| 117 | 117 |
const MinCostArbType::ArborescenceMap &am = |
| 118 | 118 |
const_mcarb_test.arborescenceMap(); |
| 119 | 119 |
const MinCostArbType::PredMap &pm = |
| 120 | 120 |
const_mcarb_test.predMap(); |
| 121 | 121 |
b = const_mcarb_test.reached(n); |
| 122 | 122 |
b = const_mcarb_test.processed(n); |
| 123 |
|
|
| 123 |
|
|
| 124 | 124 |
i = const_mcarb_test.dualNum(); |
| 125 | 125 |
c = const_mcarb_test.dualValue(); |
| 126 | 126 |
i = const_mcarb_test.dualSize(i); |
| 127 | 127 |
c = const_mcarb_test.dualValue(i); |
| 128 |
|
|
| 128 |
|
|
| 129 | 129 |
ignore_unused_variable_warning(am); |
| 130 | 130 |
ignore_unused_variable_warning(pm); |
| 131 | 131 |
} |
| 132 | 132 |
|
| 133 | 133 |
int main() {
|
| 134 | 134 |
typedef SmartDigraph Digraph; |
| 135 | 135 |
DIGRAPH_TYPEDEFS(Digraph); |
| 136 | 136 |
|
| 137 | 137 |
typedef Digraph::ArcMap<double> CostMap; |
| 138 | 138 |
|
| 139 | 139 |
Digraph digraph; |
| 140 | 140 |
CostMap cost(digraph); |
| 141 | 141 |
Node source; |
| 142 | 142 |
|
| 143 | 143 |
std::istringstream is(test_lgf); |
| 144 | 144 |
digraphReader(digraph, is). |
| 145 | 145 |
arcMap("cost", cost).
|
| 146 | 146 |
node("source", source).run();
|
| 147 | 147 |
|
| 148 | 148 |
MinCostArborescence<Digraph, CostMap> mca(digraph, cost); |
| 149 | 149 |
mca.run(source); |
| 150 | 150 |
|
| 151 | 151 |
vector<pair<double, set<Node> > > dualSolution(mca.dualNum()); |
| 152 | 152 |
| 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 <iostream> |
| 20 | 20 |
#include <fstream> |
| 21 | 21 |
#include <limits> |
| 22 | 22 |
|
| 23 | 23 |
#include <lemon/list_graph.h> |
| 24 | 24 |
#include <lemon/lgf_reader.h> |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/network_simplex.h> |
| 27 | 27 |
#include <lemon/capacity_scaling.h> |
| 28 | 28 |
#include <lemon/cost_scaling.h> |
| 29 | 29 |
#include <lemon/cycle_canceling.h> |
| ... | ... |
@@ -31,49 +31,49 @@ |
| 31 | 31 |
#include <lemon/concepts/digraph.h> |
| 32 | 32 |
#include <lemon/concepts/heap.h> |
| 33 | 33 |
#include <lemon/concept_check.h> |
| 34 | 34 |
|
| 35 | 35 |
#include "test_tools.h" |
| 36 | 36 |
|
| 37 | 37 |
using namespace lemon; |
| 38 | 38 |
|
| 39 | 39 |
// Test networks |
| 40 | 40 |
char test_lgf[] = |
| 41 | 41 |
"@nodes\n" |
| 42 | 42 |
"label sup1 sup2 sup3 sup4 sup5 sup6\n" |
| 43 | 43 |
" 1 20 27 0 30 20 30\n" |
| 44 | 44 |
" 2 -4 0 0 0 -8 -3\n" |
| 45 | 45 |
" 3 0 0 0 0 0 0\n" |
| 46 | 46 |
" 4 0 0 0 0 0 0\n" |
| 47 | 47 |
" 5 9 0 0 0 6 11\n" |
| 48 | 48 |
" 6 -6 0 0 0 -5 -6\n" |
| 49 | 49 |
" 7 0 0 0 0 0 0\n" |
| 50 | 50 |
" 8 0 0 0 0 0 3\n" |
| 51 | 51 |
" 9 3 0 0 0 0 0\n" |
| 52 | 52 |
" 10 -2 0 0 0 -7 -2\n" |
| 53 | 53 |
" 11 0 0 0 0 -10 0\n" |
| 54 | 54 |
" 12 -20 -27 0 -30 -30 -20\n" |
| 55 |
"\n" |
|
| 55 |
"\n" |
|
| 56 | 56 |
"@arcs\n" |
| 57 | 57 |
" cost cap low1 low2 low3\n" |
| 58 | 58 |
" 1 2 70 11 0 8 8\n" |
| 59 | 59 |
" 1 3 150 3 0 1 0\n" |
| 60 | 60 |
" 1 4 80 15 0 2 2\n" |
| 61 | 61 |
" 2 8 80 12 0 0 0\n" |
| 62 | 62 |
" 3 5 140 5 0 3 1\n" |
| 63 | 63 |
" 4 6 60 10 0 1 0\n" |
| 64 | 64 |
" 4 7 80 2 0 0 0\n" |
| 65 | 65 |
" 4 8 110 3 0 0 0\n" |
| 66 | 66 |
" 5 7 60 14 0 0 0\n" |
| 67 | 67 |
" 5 11 120 12 0 0 0\n" |
| 68 | 68 |
" 6 3 0 3 0 0 0\n" |
| 69 | 69 |
" 6 9 140 4 0 0 0\n" |
| 70 | 70 |
" 6 10 90 8 0 0 0\n" |
| 71 | 71 |
" 7 1 30 5 0 0 -5\n" |
| 72 | 72 |
" 8 12 60 16 0 4 3\n" |
| 73 | 73 |
" 9 12 50 6 0 0 0\n" |
| 74 | 74 |
"10 12 70 13 0 5 2\n" |
| 75 | 75 |
"10 2 100 7 0 0 0\n" |
| 76 | 76 |
"10 7 60 10 0 0 -3\n" |
| 77 | 77 |
"11 10 20 14 0 6 -20\n" |
| 78 | 78 |
"12 11 30 10 0 0 -10\n" |
| 79 | 79 |
"\n" |
| ... | ... |
@@ -81,127 +81,127 @@ |
| 81 | 81 |
"source 1\n" |
| 82 | 82 |
"target 12\n"; |
| 83 | 83 |
|
| 84 | 84 |
char test_neg1_lgf[] = |
| 85 | 85 |
"@nodes\n" |
| 86 | 86 |
"label sup\n" |
| 87 | 87 |
" 1 100\n" |
| 88 | 88 |
" 2 0\n" |
| 89 | 89 |
" 3 0\n" |
| 90 | 90 |
" 4 -100\n" |
| 91 | 91 |
" 5 0\n" |
| 92 | 92 |
" 6 0\n" |
| 93 | 93 |
" 7 0\n" |
| 94 | 94 |
"@arcs\n" |
| 95 | 95 |
" cost low1 low2\n" |
| 96 | 96 |
"1 2 100 0 0\n" |
| 97 | 97 |
"1 3 30 0 0\n" |
| 98 | 98 |
"2 4 20 0 0\n" |
| 99 | 99 |
"3 4 80 0 0\n" |
| 100 | 100 |
"3 2 50 0 0\n" |
| 101 | 101 |
"5 3 10 0 0\n" |
| 102 | 102 |
"5 6 80 0 1000\n" |
| 103 | 103 |
"6 7 30 0 -1000\n" |
| 104 | 104 |
"7 5 -120 0 0\n"; |
| 105 |
|
|
| 105 |
|
|
| 106 | 106 |
char test_neg2_lgf[] = |
| 107 | 107 |
"@nodes\n" |
| 108 | 108 |
"label sup\n" |
| 109 | 109 |
" 1 100\n" |
| 110 | 110 |
" 2 -300\n" |
| 111 | 111 |
"@arcs\n" |
| 112 | 112 |
" cost\n" |
| 113 | 113 |
"1 2 -1\n"; |
| 114 | 114 |
|
| 115 | 115 |
|
| 116 | 116 |
// Test data |
| 117 | 117 |
typedef ListDigraph Digraph; |
| 118 | 118 |
DIGRAPH_TYPEDEFS(ListDigraph); |
| 119 | 119 |
|
| 120 | 120 |
Digraph gr; |
| 121 | 121 |
Digraph::ArcMap<int> c(gr), l1(gr), l2(gr), l3(gr), u(gr); |
| 122 | 122 |
Digraph::NodeMap<int> s1(gr), s2(gr), s3(gr), s4(gr), s5(gr), s6(gr); |
| 123 | 123 |
ConstMap<Arc, int> cc(1), cu(std::numeric_limits<int>::max()); |
| 124 | 124 |
Node v, w; |
| 125 | 125 |
|
| 126 | 126 |
Digraph neg1_gr; |
| 127 | 127 |
Digraph::ArcMap<int> neg1_c(neg1_gr), neg1_l1(neg1_gr), neg1_l2(neg1_gr); |
| 128 | 128 |
ConstMap<Arc, int> neg1_u1(std::numeric_limits<int>::max()), neg1_u2(5000); |
| 129 | 129 |
Digraph::NodeMap<int> neg1_s(neg1_gr); |
| 130 | 130 |
|
| 131 | 131 |
Digraph neg2_gr; |
| 132 | 132 |
Digraph::ArcMap<int> neg2_c(neg2_gr); |
| 133 | 133 |
ConstMap<Arc, int> neg2_l(0), neg2_u(1000); |
| 134 | 134 |
Digraph::NodeMap<int> neg2_s(neg2_gr); |
| 135 | 135 |
|
| 136 | 136 |
|
| 137 | 137 |
enum SupplyType {
|
| 138 | 138 |
EQ, |
| 139 | 139 |
GEQ, |
| 140 | 140 |
LEQ |
| 141 | 141 |
}; |
| 142 | 142 |
|
| 143 | 143 |
|
| 144 | 144 |
// Check the interface of an MCF algorithm |
| 145 | 145 |
template <typename GR, typename Value, typename Cost> |
| 146 | 146 |
class McfClassConcept |
| 147 | 147 |
{
|
| 148 | 148 |
public: |
| 149 | 149 |
|
| 150 | 150 |
template <typename MCF> |
| 151 | 151 |
struct Constraints {
|
| 152 | 152 |
void constraints() {
|
| 153 | 153 |
checkConcept<concepts::Digraph, GR>(); |
| 154 |
|
|
| 154 |
|
|
| 155 | 155 |
const Constraints& me = *this; |
| 156 | 156 |
|
| 157 | 157 |
MCF mcf(me.g); |
| 158 | 158 |
const MCF& const_mcf = mcf; |
| 159 | 159 |
|
| 160 | 160 |
b = mcf.reset().resetParams() |
| 161 | 161 |
.lowerMap(me.lower) |
| 162 | 162 |
.upperMap(me.upper) |
| 163 | 163 |
.costMap(me.cost) |
| 164 | 164 |
.supplyMap(me.sup) |
| 165 | 165 |
.stSupply(me.n, me.n, me.k) |
| 166 | 166 |
.run(); |
| 167 | 167 |
|
| 168 | 168 |
c = const_mcf.totalCost(); |
| 169 | 169 |
x = const_mcf.template totalCost<double>(); |
| 170 | 170 |
v = const_mcf.flow(me.a); |
| 171 | 171 |
c = const_mcf.potential(me.n); |
| 172 | 172 |
const_mcf.flowMap(fm); |
| 173 | 173 |
const_mcf.potentialMap(pm); |
| 174 | 174 |
} |
| 175 | 175 |
|
| 176 | 176 |
typedef typename GR::Node Node; |
| 177 | 177 |
typedef typename GR::Arc Arc; |
| 178 | 178 |
typedef concepts::ReadMap<Node, Value> NM; |
| 179 | 179 |
typedef concepts::ReadMap<Arc, Value> VAM; |
| 180 | 180 |
typedef concepts::ReadMap<Arc, Cost> CAM; |
| 181 | 181 |
typedef concepts::WriteMap<Arc, Value> FlowMap; |
| 182 | 182 |
typedef concepts::WriteMap<Node, Cost> PotMap; |
| 183 |
|
|
| 183 |
|
|
| 184 | 184 |
GR g; |
| 185 | 185 |
VAM lower; |
| 186 | 186 |
VAM upper; |
| 187 | 187 |
CAM cost; |
| 188 | 188 |
NM sup; |
| 189 | 189 |
Node n; |
| 190 | 190 |
Arc a; |
| 191 | 191 |
Value k; |
| 192 | 192 |
|
| 193 | 193 |
FlowMap fm; |
| 194 | 194 |
PotMap pm; |
| 195 | 195 |
bool b; |
| 196 | 196 |
double x; |
| 197 | 197 |
typename MCF::Value v; |
| 198 | 198 |
typename MCF::Cost c; |
| 199 | 199 |
}; |
| 200 | 200 |
|
| 201 | 201 |
}; |
| 202 | 202 |
|
| 203 | 203 |
|
| 204 | 204 |
// Check the feasibility of the given flow (primal soluiton) |
| 205 | 205 |
template < typename GR, typename LM, typename UM, |
| 206 | 206 |
typename SM, typename FM > |
| 207 | 207 |
bool checkFlow( const GR& gr, const LM& lower, const UM& upper, |
| ... | ... |
@@ -213,147 +213,147 @@ |
| 213 | 213 |
for (ArcIt e(gr); e != INVALID; ++e) {
|
| 214 | 214 |
if (flow[e] < lower[e] || flow[e] > upper[e]) return false; |
| 215 | 215 |
} |
| 216 | 216 |
|
| 217 | 217 |
for (NodeIt n(gr); n != INVALID; ++n) {
|
| 218 | 218 |
typename SM::Value sum = 0; |
| 219 | 219 |
for (OutArcIt e(gr, n); e != INVALID; ++e) |
| 220 | 220 |
sum += flow[e]; |
| 221 | 221 |
for (InArcIt e(gr, n); e != INVALID; ++e) |
| 222 | 222 |
sum -= flow[e]; |
| 223 | 223 |
bool b = (type == EQ && sum == supply[n]) || |
| 224 | 224 |
(type == GEQ && sum >= supply[n]) || |
| 225 | 225 |
(type == LEQ && sum <= supply[n]); |
| 226 | 226 |
if (!b) return false; |
| 227 | 227 |
} |
| 228 | 228 |
|
| 229 | 229 |
return true; |
| 230 | 230 |
} |
| 231 | 231 |
|
| 232 | 232 |
// Check the feasibility of the given potentials (dual soluiton) |
| 233 | 233 |
// using the "Complementary Slackness" optimality condition |
| 234 | 234 |
template < typename GR, typename LM, typename UM, |
| 235 | 235 |
typename CM, typename SM, typename FM, typename PM > |
| 236 | 236 |
bool checkPotential( const GR& gr, const LM& lower, const UM& upper, |
| 237 |
const CM& cost, const SM& supply, const FM& flow, |
|
| 237 |
const CM& cost, const SM& supply, const FM& flow, |
|
| 238 | 238 |
const PM& pi, SupplyType type ) |
| 239 | 239 |
{
|
| 240 | 240 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 241 | 241 |
|
| 242 | 242 |
bool opt = true; |
| 243 | 243 |
for (ArcIt e(gr); opt && e != INVALID; ++e) {
|
| 244 | 244 |
typename CM::Value red_cost = |
| 245 | 245 |
cost[e] + pi[gr.source(e)] - pi[gr.target(e)]; |
| 246 | 246 |
opt = red_cost == 0 || |
| 247 | 247 |
(red_cost > 0 && flow[e] == lower[e]) || |
| 248 | 248 |
(red_cost < 0 && flow[e] == upper[e]); |
| 249 | 249 |
} |
| 250 |
|
|
| 250 |
|
|
| 251 | 251 |
for (NodeIt n(gr); opt && n != INVALID; ++n) {
|
| 252 | 252 |
typename SM::Value sum = 0; |
| 253 | 253 |
for (OutArcIt e(gr, n); e != INVALID; ++e) |
| 254 | 254 |
sum += flow[e]; |
| 255 | 255 |
for (InArcIt e(gr, n); e != INVALID; ++e) |
| 256 | 256 |
sum -= flow[e]; |
| 257 | 257 |
if (type != LEQ) {
|
| 258 | 258 |
opt = (pi[n] <= 0) && (sum == supply[n] || pi[n] == 0); |
| 259 | 259 |
} else {
|
| 260 | 260 |
opt = (pi[n] >= 0) && (sum == supply[n] || pi[n] == 0); |
| 261 | 261 |
} |
| 262 | 262 |
} |
| 263 |
|
|
| 263 |
|
|
| 264 | 264 |
return opt; |
| 265 | 265 |
} |
| 266 | 266 |
|
| 267 | 267 |
// Check whether the dual cost is equal to the primal cost |
| 268 | 268 |
template < typename GR, typename LM, typename UM, |
| 269 | 269 |
typename CM, typename SM, typename PM > |
| 270 | 270 |
bool checkDualCost( const GR& gr, const LM& lower, const UM& upper, |
| 271 | 271 |
const CM& cost, const SM& supply, const PM& pi, |
| 272 | 272 |
typename CM::Value total ) |
| 273 | 273 |
{
|
| 274 | 274 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 275 | 275 |
|
| 276 | 276 |
typename CM::Value dual_cost = 0; |
| 277 | 277 |
SM red_supply(gr); |
| 278 | 278 |
for (NodeIt n(gr); n != INVALID; ++n) {
|
| 279 | 279 |
red_supply[n] = supply[n]; |
| 280 | 280 |
} |
| 281 | 281 |
for (ArcIt a(gr); a != INVALID; ++a) {
|
| 282 | 282 |
if (lower[a] != 0) {
|
| 283 | 283 |
dual_cost += lower[a] * cost[a]; |
| 284 | 284 |
red_supply[gr.source(a)] -= lower[a]; |
| 285 | 285 |
red_supply[gr.target(a)] += lower[a]; |
| 286 | 286 |
} |
| 287 | 287 |
} |
| 288 |
|
|
| 288 |
|
|
| 289 | 289 |
for (NodeIt n(gr); n != INVALID; ++n) {
|
| 290 | 290 |
dual_cost -= red_supply[n] * pi[n]; |
| 291 | 291 |
} |
| 292 | 292 |
for (ArcIt a(gr); a != INVALID; ++a) {
|
| 293 | 293 |
typename CM::Value red_cost = |
| 294 | 294 |
cost[a] + pi[gr.source(a)] - pi[gr.target(a)]; |
| 295 | 295 |
dual_cost -= (upper[a] - lower[a]) * std::max(-red_cost, 0); |
| 296 | 296 |
} |
| 297 |
|
|
| 297 |
|
|
| 298 | 298 |
return dual_cost == total; |
| 299 | 299 |
} |
| 300 | 300 |
|
| 301 | 301 |
// Run a minimum cost flow algorithm and check the results |
| 302 | 302 |
template < typename MCF, typename GR, |
| 303 | 303 |
typename LM, typename UM, |
| 304 | 304 |
typename CM, typename SM, |
| 305 | 305 |
typename PT > |
| 306 | 306 |
void checkMcf( const MCF& mcf, PT mcf_result, |
| 307 | 307 |
const GR& gr, const LM& lower, const UM& upper, |
| 308 | 308 |
const CM& cost, const SM& supply, |
| 309 | 309 |
PT result, bool optimal, typename CM::Value total, |
| 310 | 310 |
const std::string &test_id = "", |
| 311 | 311 |
SupplyType type = EQ ) |
| 312 | 312 |
{
|
| 313 | 313 |
check(mcf_result == result, "Wrong result " + test_id); |
| 314 | 314 |
if (optimal) {
|
| 315 | 315 |
typename GR::template ArcMap<typename SM::Value> flow(gr); |
| 316 | 316 |
typename GR::template NodeMap<typename CM::Value> pi(gr); |
| 317 | 317 |
mcf.flowMap(flow); |
| 318 | 318 |
mcf.potentialMap(pi); |
| 319 | 319 |
check(checkFlow(gr, lower, upper, supply, flow, type), |
| 320 | 320 |
"The flow is not feasible " + test_id); |
| 321 | 321 |
check(mcf.totalCost() == total, "The flow is not optimal " + test_id); |
| 322 | 322 |
check(checkPotential(gr, lower, upper, cost, supply, flow, pi, type), |
| 323 | 323 |
"Wrong potentials " + test_id); |
| 324 | 324 |
check(checkDualCost(gr, lower, upper, cost, supply, pi, total), |
| 325 | 325 |
"Wrong dual cost " + test_id); |
| 326 | 326 |
} |
| 327 | 327 |
} |
| 328 | 328 |
|
| 329 | 329 |
template < typename MCF, typename Param > |
| 330 | 330 |
void runMcfGeqTests( Param param, |
| 331 | 331 |
const std::string &test_str = "", |
| 332 | 332 |
bool full_neg_cost_support = false ) |
| 333 | 333 |
{
|
| 334 | 334 |
MCF mcf1(gr), mcf2(neg1_gr), mcf3(neg2_gr); |
| 335 |
|
|
| 335 |
|
|
| 336 | 336 |
// Basic tests |
| 337 | 337 |
mcf1.upperMap(u).costMap(c).supplyMap(s1); |
| 338 | 338 |
checkMcf(mcf1, mcf1.run(param), gr, l1, u, c, s1, |
| 339 | 339 |
mcf1.OPTIMAL, true, 5240, test_str + "-1"); |
| 340 | 340 |
mcf1.stSupply(v, w, 27); |
| 341 | 341 |
checkMcf(mcf1, mcf1.run(param), gr, l1, u, c, s2, |
| 342 | 342 |
mcf1.OPTIMAL, true, 7620, test_str + "-2"); |
| 343 | 343 |
mcf1.lowerMap(l2).supplyMap(s1); |
| 344 | 344 |
checkMcf(mcf1, mcf1.run(param), gr, l2, u, c, s1, |
| 345 | 345 |
mcf1.OPTIMAL, true, 5970, test_str + "-3"); |
| 346 | 346 |
mcf1.stSupply(v, w, 27); |
| 347 | 347 |
checkMcf(mcf1, mcf1.run(param), gr, l2, u, c, s2, |
| 348 | 348 |
mcf1.OPTIMAL, true, 8010, test_str + "-4"); |
| 349 | 349 |
mcf1.resetParams().supplyMap(s1); |
| 350 | 350 |
checkMcf(mcf1, mcf1.run(param), gr, l1, cu, cc, s1, |
| 351 | 351 |
mcf1.OPTIMAL, true, 74, test_str + "-5"); |
| 352 | 352 |
mcf1.lowerMap(l2).stSupply(v, w, 27); |
| 353 | 353 |
checkMcf(mcf1, mcf1.run(param), gr, l2, cu, cc, s2, |
| 354 | 354 |
mcf1.OPTIMAL, true, 94, test_str + "-6"); |
| 355 | 355 |
mcf1.reset(); |
| 356 | 356 |
checkMcf(mcf1, mcf1.run(param), gr, l1, cu, cc, s3, |
| 357 | 357 |
mcf1.OPTIMAL, true, 0, test_str + "-7"); |
| 358 | 358 |
mcf1.lowerMap(l2).upperMap(u); |
| 359 | 359 |
checkMcf(mcf1, mcf1.run(param), gr, l2, u, cc, s3, |
| ... | ... |
@@ -414,63 +414,63 @@ |
| 414 | 414 |
checkMcf(mcf1, mcf1.run(param), gr, l2, u, c, s5, |
| 415 | 415 |
mcf1.INFEASIBLE, false, 0, test_str + "-21", LEQ); |
| 416 | 416 |
} |
| 417 | 417 |
|
| 418 | 418 |
|
| 419 | 419 |
int main() |
| 420 | 420 |
{
|
| 421 | 421 |
// Read the test networks |
| 422 | 422 |
std::istringstream input(test_lgf); |
| 423 | 423 |
DigraphReader<Digraph>(gr, input) |
| 424 | 424 |
.arcMap("cost", c)
|
| 425 | 425 |
.arcMap("cap", u)
|
| 426 | 426 |
.arcMap("low1", l1)
|
| 427 | 427 |
.arcMap("low2", l2)
|
| 428 | 428 |
.arcMap("low3", l3)
|
| 429 | 429 |
.nodeMap("sup1", s1)
|
| 430 | 430 |
.nodeMap("sup2", s2)
|
| 431 | 431 |
.nodeMap("sup3", s3)
|
| 432 | 432 |
.nodeMap("sup4", s4)
|
| 433 | 433 |
.nodeMap("sup5", s5)
|
| 434 | 434 |
.nodeMap("sup6", s6)
|
| 435 | 435 |
.node("source", v)
|
| 436 | 436 |
.node("target", w)
|
| 437 | 437 |
.run(); |
| 438 |
|
|
| 438 |
|
|
| 439 | 439 |
std::istringstream neg_inp1(test_neg1_lgf); |
| 440 | 440 |
DigraphReader<Digraph>(neg1_gr, neg_inp1) |
| 441 | 441 |
.arcMap("cost", neg1_c)
|
| 442 | 442 |
.arcMap("low1", neg1_l1)
|
| 443 | 443 |
.arcMap("low2", neg1_l2)
|
| 444 | 444 |
.nodeMap("sup", neg1_s)
|
| 445 | 445 |
.run(); |
| 446 |
|
|
| 446 |
|
|
| 447 | 447 |
std::istringstream neg_inp2(test_neg2_lgf); |
| 448 | 448 |
DigraphReader<Digraph>(neg2_gr, neg_inp2) |
| 449 | 449 |
.arcMap("cost", neg2_c)
|
| 450 | 450 |
.nodeMap("sup", neg2_s)
|
| 451 | 451 |
.run(); |
| 452 |
|
|
| 452 |
|
|
| 453 | 453 |
// Check the interface of NetworkSimplex |
| 454 | 454 |
{
|
| 455 | 455 |
typedef concepts::Digraph GR; |
| 456 | 456 |
checkConcept< McfClassConcept<GR, int, int>, |
| 457 | 457 |
NetworkSimplex<GR> >(); |
| 458 | 458 |
checkConcept< McfClassConcept<GR, double, double>, |
| 459 | 459 |
NetworkSimplex<GR, double> >(); |
| 460 | 460 |
checkConcept< McfClassConcept<GR, int, double>, |
| 461 | 461 |
NetworkSimplex<GR, int, double> >(); |
| 462 | 462 |
} |
| 463 | 463 |
|
| 464 | 464 |
// Check the interface of CapacityScaling |
| 465 | 465 |
{
|
| 466 | 466 |
typedef concepts::Digraph GR; |
| 467 | 467 |
checkConcept< McfClassConcept<GR, int, int>, |
| 468 | 468 |
CapacityScaling<GR> >(); |
| 469 | 469 |
checkConcept< McfClassConcept<GR, double, double>, |
| 470 | 470 |
CapacityScaling<GR, double> >(); |
| 471 | 471 |
checkConcept< McfClassConcept<GR, int, double>, |
| 472 | 472 |
CapacityScaling<GR, int, double> >(); |
| 473 | 473 |
typedef CapacityScaling<GR>:: |
| 474 | 474 |
SetHeap<concepts::Heap<int, RangeMap<int> > >::Create CAS; |
| 475 | 475 |
checkConcept< McfClassConcept<GR, int, int>, CAS >(); |
| 476 | 476 |
} |
| ... | ... |
@@ -480,62 +480,62 @@ |
| 480 | 480 |
typedef concepts::Digraph GR; |
| 481 | 481 |
checkConcept< McfClassConcept<GR, int, int>, |
| 482 | 482 |
CostScaling<GR> >(); |
| 483 | 483 |
checkConcept< McfClassConcept<GR, double, double>, |
| 484 | 484 |
CostScaling<GR, double> >(); |
| 485 | 485 |
checkConcept< McfClassConcept<GR, int, double>, |
| 486 | 486 |
CostScaling<GR, int, double> >(); |
| 487 | 487 |
typedef CostScaling<GR>:: |
| 488 | 488 |
SetLargeCost<double>::Create COS; |
| 489 | 489 |
checkConcept< McfClassConcept<GR, int, int>, COS >(); |
| 490 | 490 |
} |
| 491 | 491 |
|
| 492 | 492 |
// Check the interface of CycleCanceling |
| 493 | 493 |
{
|
| 494 | 494 |
typedef concepts::Digraph GR; |
| 495 | 495 |
checkConcept< McfClassConcept<GR, int, int>, |
| 496 | 496 |
CycleCanceling<GR> >(); |
| 497 | 497 |
checkConcept< McfClassConcept<GR, double, double>, |
| 498 | 498 |
CycleCanceling<GR, double> >(); |
| 499 | 499 |
checkConcept< McfClassConcept<GR, int, double>, |
| 500 | 500 |
CycleCanceling<GR, int, double> >(); |
| 501 | 501 |
} |
| 502 | 502 |
|
| 503 | 503 |
// Test NetworkSimplex |
| 504 |
{
|
|
| 504 |
{
|
|
| 505 | 505 |
typedef NetworkSimplex<Digraph> MCF; |
| 506 | 506 |
runMcfGeqTests<MCF>(MCF::FIRST_ELIGIBLE, "NS-FE", true); |
| 507 | 507 |
runMcfLeqTests<MCF>(MCF::FIRST_ELIGIBLE, "NS-FE"); |
| 508 | 508 |
runMcfGeqTests<MCF>(MCF::BEST_ELIGIBLE, "NS-BE", true); |
| 509 | 509 |
runMcfLeqTests<MCF>(MCF::BEST_ELIGIBLE, "NS-BE"); |
| 510 | 510 |
runMcfGeqTests<MCF>(MCF::BLOCK_SEARCH, "NS-BS", true); |
| 511 | 511 |
runMcfLeqTests<MCF>(MCF::BLOCK_SEARCH, "NS-BS"); |
| 512 | 512 |
runMcfGeqTests<MCF>(MCF::CANDIDATE_LIST, "NS-CL", true); |
| 513 | 513 |
runMcfLeqTests<MCF>(MCF::CANDIDATE_LIST, "NS-CL"); |
| 514 | 514 |
runMcfGeqTests<MCF>(MCF::ALTERING_LIST, "NS-AL", true); |
| 515 | 515 |
runMcfLeqTests<MCF>(MCF::ALTERING_LIST, "NS-AL"); |
| 516 | 516 |
} |
| 517 |
|
|
| 517 |
|
|
| 518 | 518 |
// Test CapacityScaling |
| 519 | 519 |
{
|
| 520 | 520 |
typedef CapacityScaling<Digraph> MCF; |
| 521 | 521 |
runMcfGeqTests<MCF>(0, "SSP"); |
| 522 | 522 |
runMcfGeqTests<MCF>(2, "CAS"); |
| 523 | 523 |
} |
| 524 | 524 |
|
| 525 | 525 |
// Test CostScaling |
| 526 | 526 |
{
|
| 527 | 527 |
typedef CostScaling<Digraph> MCF; |
| 528 | 528 |
runMcfGeqTests<MCF>(MCF::PUSH, "COS-PR"); |
| 529 | 529 |
runMcfGeqTests<MCF>(MCF::AUGMENT, "COS-AR"); |
| 530 | 530 |
runMcfGeqTests<MCF>(MCF::PARTIAL_AUGMENT, "COS-PAR"); |
| 531 | 531 |
} |
| 532 | 532 |
|
| 533 | 533 |
// Test CycleCanceling |
| 534 | 534 |
{
|
| 535 | 535 |
typedef CycleCanceling<Digraph> MCF; |
| 536 | 536 |
runMcfGeqTests<MCF>(MCF::SIMPLE_CYCLE_CANCELING, "SCC"); |
| 537 | 537 |
runMcfGeqTests<MCF>(MCF::MINIMUM_MEAN_CYCLE_CANCELING, "MMCC"); |
| 538 | 538 |
runMcfGeqTests<MCF>(MCF::CANCEL_AND_TIGHTEN, "CAT"); |
| 539 | 539 |
} |
| 540 | 540 |
|
| 541 | 541 |
return 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 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <iostream> |
| 20 | 20 |
#include <sstream> |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/smart_graph.h> |
| 23 | 23 |
#include <lemon/lgf_reader.h> |
| 24 | 24 |
#include <lemon/path.h> |
| 25 | 25 |
#include <lemon/concepts/digraph.h> |
| 26 | 26 |
#include <lemon/concept_check.h> |
| 27 | 27 |
|
| 28 | 28 |
#include <lemon/karp_mmc.h> |
| 29 | 29 |
#include <lemon/hartmann_orlin_mmc.h> |
| ... | ... |
@@ -40,80 +40,80 @@ |
| 40 | 40 |
"2\n" |
| 41 | 41 |
"3\n" |
| 42 | 42 |
"4\n" |
| 43 | 43 |
"5\n" |
| 44 | 44 |
"6\n" |
| 45 | 45 |
"7\n" |
| 46 | 46 |
"@arcs\n" |
| 47 | 47 |
" len1 len2 len3 len4 c1 c2 c3 c4\n" |
| 48 | 48 |
"1 2 1 1 1 1 0 0 0 0\n" |
| 49 | 49 |
"2 4 5 5 5 5 1 0 0 0\n" |
| 50 | 50 |
"2 3 8 8 8 8 0 0 0 0\n" |
| 51 | 51 |
"3 2 -2 0 0 0 1 0 0 0\n" |
| 52 | 52 |
"3 4 4 4 4 4 0 0 0 0\n" |
| 53 | 53 |
"3 7 -4 -4 -4 -4 0 0 0 0\n" |
| 54 | 54 |
"4 1 2 2 2 2 0 0 0 0\n" |
| 55 | 55 |
"4 3 3 3 3 3 1 0 0 0\n" |
| 56 | 56 |
"4 4 3 3 0 0 0 0 1 0\n" |
| 57 | 57 |
"5 2 4 4 4 4 0 0 0 0\n" |
| 58 | 58 |
"5 6 3 3 3 3 0 1 0 0\n" |
| 59 | 59 |
"6 5 2 2 2 2 0 1 0 0\n" |
| 60 | 60 |
"6 4 -1 -1 -1 -1 0 0 0 0\n" |
| 61 | 61 |
"6 7 1 1 1 1 0 0 0 0\n" |
| 62 | 62 |
"7 7 4 4 4 -1 0 0 0 1\n"; |
| 63 | 63 |
|
| 64 |
|
|
| 64 |
|
|
| 65 | 65 |
// Check the interface of an MMC algorithm |
| 66 | 66 |
template <typename GR, typename Cost> |
| 67 | 67 |
struct MmcClassConcept |
| 68 | 68 |
{
|
| 69 | 69 |
template <typename MMC> |
| 70 | 70 |
struct Constraints {
|
| 71 | 71 |
void constraints() {
|
| 72 | 72 |
const Constraints& me = *this; |
| 73 | 73 |
|
| 74 | 74 |
typedef typename MMC |
| 75 | 75 |
::template SetPath<ListPath<GR> > |
| 76 | 76 |
::template SetLargeCost<Cost> |
| 77 | 77 |
::Create MmcAlg; |
| 78 | 78 |
MmcAlg mmc(me.g, me.cost); |
| 79 | 79 |
const MmcAlg& const_mmc = mmc; |
| 80 |
|
|
| 80 |
|
|
| 81 | 81 |
typename MmcAlg::Tolerance tol = const_mmc.tolerance(); |
| 82 | 82 |
mmc.tolerance(tol); |
| 83 |
|
|
| 83 |
|
|
| 84 | 84 |
b = mmc.cycle(p).run(); |
| 85 | 85 |
b = mmc.findCycleMean(); |
| 86 | 86 |
b = mmc.findCycle(); |
| 87 | 87 |
|
| 88 | 88 |
v = const_mmc.cycleCost(); |
| 89 | 89 |
i = const_mmc.cycleSize(); |
| 90 | 90 |
d = const_mmc.cycleMean(); |
| 91 | 91 |
p = const_mmc.cycle(); |
| 92 | 92 |
} |
| 93 | 93 |
|
| 94 | 94 |
typedef concepts::ReadMap<typename GR::Arc, Cost> CM; |
| 95 |
|
|
| 95 |
|
|
| 96 | 96 |
GR g; |
| 97 | 97 |
CM cost; |
| 98 | 98 |
ListPath<GR> p; |
| 99 | 99 |
Cost v; |
| 100 | 100 |
int i; |
| 101 | 101 |
double d; |
| 102 | 102 |
bool b; |
| 103 | 103 |
}; |
| 104 | 104 |
}; |
| 105 | 105 |
|
| 106 | 106 |
// Perform a test with the given parameters |
| 107 | 107 |
template <typename MMC> |
| 108 | 108 |
void checkMmcAlg(const SmartDigraph& gr, |
| 109 | 109 |
const SmartDigraph::ArcMap<int>& lm, |
| 110 | 110 |
const SmartDigraph::ArcMap<int>& cm, |
| 111 | 111 |
int cost, int size) {
|
| 112 | 112 |
MMC alg(gr, lm); |
| 113 | 113 |
alg.findCycleMean(); |
| 114 | 114 |
check(alg.cycleMean() == static_cast<double>(cost) / size, |
| 115 | 115 |
"Wrong cycle mean"); |
| 116 | 116 |
alg.findCycle(); |
| 117 | 117 |
check(alg.cycleCost() == cost && alg.cycleSize() == size, |
| 118 | 118 |
"Wrong path"); |
| 119 | 119 |
SmartDigraph::ArcMap<int> cycle(gr, 0); |
| ... | ... |
@@ -132,76 +132,76 @@ |
| 132 | 132 |
}; |
| 133 | 133 |
|
| 134 | 134 |
template <typename T> |
| 135 | 135 |
struct IsSameType<T,T> {
|
| 136 | 136 |
static const int result = 1; |
| 137 | 137 |
}; |
| 138 | 138 |
|
| 139 | 139 |
|
| 140 | 140 |
int main() {
|
| 141 | 141 |
#ifdef LEMON_HAVE_LONG_LONG |
| 142 | 142 |
typedef long long long_int; |
| 143 | 143 |
#else |
| 144 | 144 |
typedef long long_int; |
| 145 | 145 |
#endif |
| 146 | 146 |
|
| 147 | 147 |
// Check the interface |
| 148 | 148 |
{
|
| 149 | 149 |
typedef concepts::Digraph GR; |
| 150 | 150 |
|
| 151 | 151 |
// KarpMmc |
| 152 | 152 |
checkConcept< MmcClassConcept<GR, int>, |
| 153 | 153 |
KarpMmc<GR, concepts::ReadMap<GR::Arc, int> > >(); |
| 154 | 154 |
checkConcept< MmcClassConcept<GR, float>, |
| 155 | 155 |
KarpMmc<GR, concepts::ReadMap<GR::Arc, float> > >(); |
| 156 |
|
|
| 156 |
|
|
| 157 | 157 |
// HartmannOrlinMmc |
| 158 | 158 |
checkConcept< MmcClassConcept<GR, int>, |
| 159 | 159 |
HartmannOrlinMmc<GR, concepts::ReadMap<GR::Arc, int> > >(); |
| 160 | 160 |
checkConcept< MmcClassConcept<GR, float>, |
| 161 | 161 |
HartmannOrlinMmc<GR, concepts::ReadMap<GR::Arc, float> > >(); |
| 162 |
|
|
| 162 |
|
|
| 163 | 163 |
// HowardMmc |
| 164 | 164 |
checkConcept< MmcClassConcept<GR, int>, |
| 165 | 165 |
HowardMmc<GR, concepts::ReadMap<GR::Arc, int> > >(); |
| 166 | 166 |
checkConcept< MmcClassConcept<GR, float>, |
| 167 | 167 |
HowardMmc<GR, concepts::ReadMap<GR::Arc, float> > >(); |
| 168 | 168 |
|
| 169 | 169 |
check((IsSameType<HowardMmc<GR, concepts::ReadMap<GR::Arc, int> > |
| 170 | 170 |
::LargeCost, long_int>::result == 1), "Wrong LargeCost type"); |
| 171 | 171 |
check((IsSameType<HowardMmc<GR, concepts::ReadMap<GR::Arc, float> > |
| 172 | 172 |
::LargeCost, double>::result == 1), "Wrong LargeCost type"); |
| 173 | 173 |
} |
| 174 | 174 |
|
| 175 | 175 |
// Run various tests |
| 176 | 176 |
{
|
| 177 | 177 |
typedef SmartDigraph GR; |
| 178 | 178 |
DIGRAPH_TYPEDEFS(GR); |
| 179 |
|
|
| 179 |
|
|
| 180 | 180 |
GR gr; |
| 181 | 181 |
IntArcMap l1(gr), l2(gr), l3(gr), l4(gr); |
| 182 | 182 |
IntArcMap c1(gr), c2(gr), c3(gr), c4(gr); |
| 183 |
|
|
| 183 |
|
|
| 184 | 184 |
std::istringstream input(test_lgf); |
| 185 | 185 |
digraphReader(gr, input). |
| 186 | 186 |
arcMap("len1", l1).
|
| 187 | 187 |
arcMap("len2", l2).
|
| 188 | 188 |
arcMap("len3", l3).
|
| 189 | 189 |
arcMap("len4", l4).
|
| 190 | 190 |
arcMap("c1", c1).
|
| 191 | 191 |
arcMap("c2", c2).
|
| 192 | 192 |
arcMap("c3", c3).
|
| 193 | 193 |
arcMap("c4", c4).
|
| 194 | 194 |
run(); |
| 195 | 195 |
|
| 196 | 196 |
// Karp |
| 197 | 197 |
checkMmcAlg<KarpMmc<GR, IntArcMap> >(gr, l1, c1, 6, 3); |
| 198 | 198 |
checkMmcAlg<KarpMmc<GR, IntArcMap> >(gr, l2, c2, 5, 2); |
| 199 | 199 |
checkMmcAlg<KarpMmc<GR, IntArcMap> >(gr, l3, c3, 0, 1); |
| 200 | 200 |
checkMmcAlg<KarpMmc<GR, IntArcMap> >(gr, l4, c4, -1, 1); |
| 201 | 201 |
|
| 202 | 202 |
// HartmannOrlin |
| 203 | 203 |
checkMmcAlg<HartmannOrlinMmc<GR, IntArcMap> >(gr, l1, c1, 6, 3); |
| 204 | 204 |
checkMmcAlg<HartmannOrlinMmc<GR, IntArcMap> >(gr, l2, c2, 5, 2); |
| 205 | 205 |
checkMmcAlg<HartmannOrlinMmc<GR, IntArcMap> >(gr, l3, c3, 0, 1); |
| 206 | 206 |
checkMmcAlg<HartmannOrlinMmc<GR, IntArcMap> >(gr, l4, c4, -1, 1); |
| 207 | 207 |
| 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 <iostream> |
| 20 | 20 |
|
| 21 | 21 |
#include "test_tools.h" |
| 22 | 22 |
#include <lemon/smart_graph.h> |
| 23 | 23 |
#include <lemon/preflow.h> |
| 24 | 24 |
#include <lemon/concepts/digraph.h> |
| 25 | 25 |
#include <lemon/concepts/maps.h> |
| 26 | 26 |
#include <lemon/lgf_reader.h> |
| 27 | 27 |
#include <lemon/elevator.h> |
| 28 | 28 |
|
| 29 | 29 |
using namespace lemon; |
| ... | ... |
@@ -73,73 +73,73 @@ |
| 73 | 73 |
typedef Digraph::Arc Arc; |
| 74 | 74 |
typedef concepts::ReadMap<Arc,VType> CapMap; |
| 75 | 75 |
typedef concepts::ReadWriteMap<Arc,VType> FlowMap; |
| 76 | 76 |
typedef concepts::WriteMap<Node,bool> CutMap; |
| 77 | 77 |
|
| 78 | 78 |
typedef Elevator<Digraph, Digraph::Node> Elev; |
| 79 | 79 |
typedef LinkedElevator<Digraph, Digraph::Node> LinkedElev; |
| 80 | 80 |
|
| 81 | 81 |
Digraph g; |
| 82 | 82 |
Node n; |
| 83 | 83 |
Arc e; |
| 84 | 84 |
CapMap cap; |
| 85 | 85 |
FlowMap flow; |
| 86 | 86 |
CutMap cut; |
| 87 | 87 |
VType v; |
| 88 | 88 |
bool b; |
| 89 | 89 |
|
| 90 | 90 |
typedef Preflow<Digraph, CapMap> |
| 91 | 91 |
::SetFlowMap<FlowMap> |
| 92 | 92 |
::SetElevator<Elev> |
| 93 | 93 |
::SetStandardElevator<LinkedElev> |
| 94 | 94 |
::Create PreflowType; |
| 95 | 95 |
PreflowType preflow_test(g, cap, n, n); |
| 96 | 96 |
const PreflowType& const_preflow_test = preflow_test; |
| 97 |
|
|
| 97 |
|
|
| 98 | 98 |
const PreflowType::Elevator& elev = const_preflow_test.elevator(); |
| 99 | 99 |
preflow_test.elevator(const_cast<PreflowType::Elevator&>(elev)); |
| 100 | 100 |
PreflowType::Tolerance tol = const_preflow_test.tolerance(); |
| 101 | 101 |
preflow_test.tolerance(tol); |
| 102 | 102 |
|
| 103 | 103 |
preflow_test |
| 104 | 104 |
.capacityMap(cap) |
| 105 | 105 |
.flowMap(flow) |
| 106 | 106 |
.source(n) |
| 107 | 107 |
.target(n); |
| 108 | 108 |
|
| 109 | 109 |
preflow_test.init(); |
| 110 | 110 |
preflow_test.init(cap); |
| 111 | 111 |
preflow_test.startFirstPhase(); |
| 112 | 112 |
preflow_test.startSecondPhase(); |
| 113 | 113 |
preflow_test.run(); |
| 114 | 114 |
preflow_test.runMinCut(); |
| 115 | 115 |
|
| 116 | 116 |
v = const_preflow_test.flowValue(); |
| 117 | 117 |
v = const_preflow_test.flow(e); |
| 118 | 118 |
const FlowMap& fm = const_preflow_test.flowMap(); |
| 119 | 119 |
b = const_preflow_test.minCut(n); |
| 120 | 120 |
const_preflow_test.minCutMap(cut); |
| 121 |
|
|
| 121 |
|
|
| 122 | 122 |
ignore_unused_variable_warning(fm); |
| 123 | 123 |
} |
| 124 | 124 |
|
| 125 | 125 |
int cutValue (const SmartDigraph& g, |
| 126 | 126 |
const SmartDigraph::NodeMap<bool>& cut, |
| 127 | 127 |
const SmartDigraph::ArcMap<int>& cap) {
|
| 128 | 128 |
|
| 129 | 129 |
int c=0; |
| 130 | 130 |
for(SmartDigraph::ArcIt e(g); e!=INVALID; ++e) {
|
| 131 | 131 |
if (cut[g.source(e)] && !cut[g.target(e)]) c+=cap[e]; |
| 132 | 132 |
} |
| 133 | 133 |
return c; |
| 134 | 134 |
} |
| 135 | 135 |
|
| 136 | 136 |
bool checkFlow(const SmartDigraph& g, |
| 137 | 137 |
const SmartDigraph::ArcMap<int>& flow, |
| 138 | 138 |
const SmartDigraph::ArcMap<int>& cap, |
| 139 | 139 |
SmartDigraph::Node s, SmartDigraph::Node t) {
|
| 140 | 140 |
|
| 141 | 141 |
for (SmartDigraph::ArcIt e(g); e != INVALID; ++e) {
|
| 142 | 142 |
if (flow[e] < 0 || flow[e] > cap[e]) return false; |
| 143 | 143 |
} |
| 144 | 144 |
|
| 145 | 145 |
for (SmartDigraph::NodeIt n(g); n != INVALID; ++n) {
|
| 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 <iostream> |
| 20 | 20 |
|
| 21 | 21 |
#include <lemon/list_graph.h> |
| 22 | 22 |
#include <lemon/lgf_reader.h> |
| 23 | 23 |
#include <lemon/path.h> |
| 24 | 24 |
#include <lemon/suurballe.h> |
| 25 | 25 |
#include <lemon/concepts/digraph.h> |
| 26 | 26 |
#include <lemon/concepts/heap.h> |
| 27 | 27 |
|
| 28 | 28 |
#include "test_tools.h" |
| 29 | 29 |
|
| ... | ... |
@@ -60,94 +60,94 @@ |
| 60 | 60 |
" 6 9 140\n" |
| 61 | 61 |
" 6 10 90\n" |
| 62 | 62 |
" 7 1 30\n" |
| 63 | 63 |
" 8 12 60\n" |
| 64 | 64 |
" 9 12 50\n" |
| 65 | 65 |
"10 12 70\n" |
| 66 | 66 |
"10 2 100\n" |
| 67 | 67 |
"10 7 60\n" |
| 68 | 68 |
"11 10 20\n" |
| 69 | 69 |
"12 11 30\n" |
| 70 | 70 |
"@attributes\n" |
| 71 | 71 |
"source 1\n" |
| 72 | 72 |
"target 12\n" |
| 73 | 73 |
"@end\n"; |
| 74 | 74 |
|
| 75 | 75 |
// Check the interface of Suurballe |
| 76 | 76 |
void checkSuurballeCompile() |
| 77 | 77 |
{
|
| 78 | 78 |
typedef int VType; |
| 79 | 79 |
typedef concepts::Digraph Digraph; |
| 80 | 80 |
|
| 81 | 81 |
typedef Digraph::Node Node; |
| 82 | 82 |
typedef Digraph::Arc Arc; |
| 83 | 83 |
typedef concepts::ReadMap<Arc, VType> LengthMap; |
| 84 |
|
|
| 84 |
|
|
| 85 | 85 |
typedef Suurballe<Digraph, LengthMap> ST; |
| 86 | 86 |
typedef Suurballe<Digraph, LengthMap> |
| 87 | 87 |
::SetFlowMap<ST::FlowMap> |
| 88 | 88 |
::SetPotentialMap<ST::PotentialMap> |
| 89 | 89 |
::SetPath<SimplePath<Digraph> > |
| 90 | 90 |
::SetHeap<concepts::Heap<VType, Digraph::NodeMap<int> > > |
| 91 | 91 |
::Create SuurballeType; |
| 92 | 92 |
|
| 93 | 93 |
Digraph g; |
| 94 | 94 |
Node n; |
| 95 | 95 |
Arc e; |
| 96 | 96 |
LengthMap len; |
| 97 | 97 |
SuurballeType::FlowMap flow(g); |
| 98 | 98 |
SuurballeType::PotentialMap pi(g); |
| 99 | 99 |
|
| 100 | 100 |
SuurballeType suurb_test(g, len); |
| 101 | 101 |
const SuurballeType& const_suurb_test = suurb_test; |
| 102 | 102 |
|
| 103 | 103 |
suurb_test |
| 104 | 104 |
.flowMap(flow) |
| 105 | 105 |
.potentialMap(pi); |
| 106 | 106 |
|
| 107 | 107 |
int k; |
| 108 | 108 |
k = suurb_test.run(n, n); |
| 109 | 109 |
k = suurb_test.run(n, n, k); |
| 110 | 110 |
suurb_test.init(n); |
| 111 | 111 |
suurb_test.fullInit(n); |
| 112 | 112 |
suurb_test.start(n); |
| 113 | 113 |
suurb_test.start(n, k); |
| 114 | 114 |
k = suurb_test.findFlow(n); |
| 115 | 115 |
k = suurb_test.findFlow(n, k); |
| 116 | 116 |
suurb_test.findPaths(); |
| 117 |
|
|
| 117 |
|
|
| 118 | 118 |
int f; |
| 119 | 119 |
VType c; |
| 120 | 120 |
c = const_suurb_test.totalLength(); |
| 121 | 121 |
f = const_suurb_test.flow(e); |
| 122 | 122 |
const SuurballeType::FlowMap& fm = |
| 123 | 123 |
const_suurb_test.flowMap(); |
| 124 | 124 |
c = const_suurb_test.potential(n); |
| 125 | 125 |
const SuurballeType::PotentialMap& pm = |
| 126 | 126 |
const_suurb_test.potentialMap(); |
| 127 | 127 |
k = const_suurb_test.pathNum(); |
| 128 | 128 |
Path<Digraph> p = const_suurb_test.path(k); |
| 129 |
|
|
| 129 |
|
|
| 130 | 130 |
ignore_unused_variable_warning(fm); |
| 131 | 131 |
ignore_unused_variable_warning(pm); |
| 132 | 132 |
} |
| 133 | 133 |
|
| 134 | 134 |
// Check the feasibility of the flow |
| 135 | 135 |
template <typename Digraph, typename FlowMap> |
| 136 | 136 |
bool checkFlow( const Digraph& gr, const FlowMap& flow, |
| 137 | 137 |
typename Digraph::Node s, typename Digraph::Node t, |
| 138 | 138 |
int value ) |
| 139 | 139 |
{
|
| 140 | 140 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 141 | 141 |
for (ArcIt e(gr); e != INVALID; ++e) |
| 142 | 142 |
if (!(flow[e] == 0 || flow[e] == 1)) return false; |
| 143 | 143 |
|
| 144 | 144 |
for (NodeIt n(gr); n != INVALID; ++n) {
|
| 145 | 145 |
int sum = 0; |
| 146 | 146 |
for (OutArcIt e(gr, n); e != INVALID; ++e) |
| 147 | 147 |
sum += flow[e]; |
| 148 | 148 |
for (InArcIt e(gr, n); e != INVALID; ++e) |
| 149 | 149 |
sum -= flow[e]; |
| 150 | 150 |
if (n == s && sum != value) return false; |
| 151 | 151 |
if (n == t && sum != -value) return false; |
| 152 | 152 |
if (n != s && n != t && sum != 0) return false; |
| 153 | 153 |
} |
| ... | ... |
@@ -187,79 +187,79 @@ |
| 187 | 187 |
} |
| 188 | 188 |
return n == t; |
| 189 | 189 |
} |
| 190 | 190 |
|
| 191 | 191 |
|
| 192 | 192 |
int main() |
| 193 | 193 |
{
|
| 194 | 194 |
DIGRAPH_TYPEDEFS(ListDigraph); |
| 195 | 195 |
|
| 196 | 196 |
// Read the test digraph |
| 197 | 197 |
ListDigraph digraph; |
| 198 | 198 |
ListDigraph::ArcMap<int> length(digraph); |
| 199 | 199 |
Node s, t; |
| 200 | 200 |
|
| 201 | 201 |
std::istringstream input(test_lgf); |
| 202 | 202 |
DigraphReader<ListDigraph>(digraph, input). |
| 203 | 203 |
arcMap("length", length).
|
| 204 | 204 |
node("source", s).
|
| 205 | 205 |
node("target", t).
|
| 206 | 206 |
run(); |
| 207 | 207 |
|
| 208 | 208 |
// Check run() |
| 209 | 209 |
{
|
| 210 | 210 |
Suurballe<ListDigraph> suurballe(digraph, length); |
| 211 |
|
|
| 211 |
|
|
| 212 | 212 |
// Find 2 paths |
| 213 | 213 |
check(suurballe.run(s, t) == 2, "Wrong number of paths"); |
| 214 | 214 |
check(checkFlow(digraph, suurballe.flowMap(), s, t, 2), |
| 215 | 215 |
"The flow is not feasible"); |
| 216 | 216 |
check(suurballe.totalLength() == 510, "The flow is not optimal"); |
| 217 | 217 |
check(checkOptimality(digraph, length, suurballe.flowMap(), |
| 218 | 218 |
suurballe.potentialMap()), |
| 219 | 219 |
"Wrong potentials"); |
| 220 | 220 |
for (int i = 0; i < suurballe.pathNum(); ++i) |
| 221 | 221 |
check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path"); |
| 222 |
|
|
| 222 |
|
|
| 223 | 223 |
// Find 3 paths |
| 224 | 224 |
check(suurballe.run(s, t, 3) == 3, "Wrong number of paths"); |
| 225 | 225 |
check(checkFlow(digraph, suurballe.flowMap(), s, t, 3), |
| 226 | 226 |
"The flow is not feasible"); |
| 227 | 227 |
check(suurballe.totalLength() == 1040, "The flow is not optimal"); |
| 228 | 228 |
check(checkOptimality(digraph, length, suurballe.flowMap(), |
| 229 | 229 |
suurballe.potentialMap()), |
| 230 | 230 |
"Wrong potentials"); |
| 231 | 231 |
for (int i = 0; i < suurballe.pathNum(); ++i) |
| 232 | 232 |
check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path"); |
| 233 |
|
|
| 233 |
|
|
| 234 | 234 |
// Find 5 paths (only 3 can be found) |
| 235 | 235 |
check(suurballe.run(s, t, 5) == 3, "Wrong number of paths"); |
| 236 | 236 |
check(checkFlow(digraph, suurballe.flowMap(), s, t, 3), |
| 237 | 237 |
"The flow is not feasible"); |
| 238 | 238 |
check(suurballe.totalLength() == 1040, "The flow is not optimal"); |
| 239 | 239 |
check(checkOptimality(digraph, length, suurballe.flowMap(), |
| 240 | 240 |
suurballe.potentialMap()), |
| 241 | 241 |
"Wrong potentials"); |
| 242 | 242 |
for (int i = 0; i < suurballe.pathNum(); ++i) |
| 243 | 243 |
check(checkPath(digraph, suurballe.path(i), s, t), "Wrong path"); |
| 244 | 244 |
} |
| 245 |
|
|
| 245 |
|
|
| 246 | 246 |
// Check fullInit() + start() |
| 247 | 247 |
{
|
| 248 | 248 |
Suurballe<ListDigraph> suurballe(digraph, length); |
| 249 | 249 |
suurballe.fullInit(s); |
| 250 |
|
|
| 250 |
|
|
| 251 | 251 |
// Find 2 paths |
| 252 | 252 |
check(suurballe.start(t) == 2, "Wrong number of paths"); |
| 253 | 253 |
check(suurballe.totalLength() == 510, "The flow is not optimal"); |
| 254 | 254 |
|
| 255 | 255 |
// Find 3 paths |
| 256 | 256 |
check(suurballe.start(t, 3) == 3, "Wrong number of paths"); |
| 257 | 257 |
check(suurballe.totalLength() == 1040, "The flow is not optimal"); |
| 258 | 258 |
|
| 259 | 259 |
// Find 5 paths (only 3 can be found) |
| 260 | 260 |
check(suurballe.start(t, 5) == 3, "Wrong number of paths"); |
| 261 | 261 |
check(suurballe.totalLength() == 1040, "The flow is not optimal"); |
| 262 | 262 |
} |
| 263 | 263 |
|
| 264 | 264 |
return 0; |
| 265 | 265 |
} |
| 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_TEST_TEST_TOOLS_H |
| 20 | 20 |
#define LEMON_TEST_TEST_TOOLS_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup misc |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief Some utilities to write test programs. |
| 25 | 25 |
|
| 26 | 26 |
#include <iostream> |
| 27 | 27 |
#include <stdlib.h> |
| 28 | 28 |
|
| 29 | 29 |
///If \c rc is fail, writes an error message and exits. |
| 30 | 30 |
|
| 31 | 31 |
///If \c rc is fail, writes an error message and exits. |
| 32 | 32 |
///The error message contains the file name and the line number of the |
| 33 | 33 |
///source code in a standard from, which makes it possible to go there |
| 34 | 34 |
///using good source browsers like e.g. \c emacs. |
| 35 | 35 |
/// |
| 36 | 36 |
///For example |
| 37 | 37 |
///\code check(0==1,"This is obviously false.");\endcode will |
| 38 | 38 |
///print something like this (and then exits). |
| 39 | 39 |
///\verbatim file_name.cc:123: error: This is obviously false. \endverbatim |
| 40 | 40 |
#define check(rc, msg) \ |
| 41 | 41 |
{ \
|
| 42 | 42 |
if(!(rc)) { \
|
| 43 | 43 |
std::cerr << __FILE__ ":" << __LINE__ << ": error: " \ |
| 44 | 44 |
<< msg << std::endl; \ |
| 45 | 45 |
abort(); \ |
| 46 | 46 |
} else { } \
|
| 47 | 47 |
} \ |
| 48 |
|
|
| 48 |
|
|
| 49 | 49 |
|
| 50 | 50 |
#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 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
///\ingroup tools |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief DIMACS problem solver. |
| 22 | 22 |
/// |
| 23 | 23 |
/// This program solves various problems given in DIMACS format. |
| 24 | 24 |
/// |
| 25 | 25 |
/// See |
| 26 | 26 |
/// \code |
| 27 | 27 |
/// dimacs-solver --help |
| 28 | 28 |
/// \endcode |
| 29 | 29 |
/// for more info on usage. |
| ... | ... |
@@ -67,49 +67,49 @@ |
| 67 | 67 |
if(report) std::cerr << "Setup Dijkstra class: " << t << '\n'; |
| 68 | 68 |
t.restart(); |
| 69 | 69 |
dij.run(s); |
| 70 | 70 |
if(report) std::cerr << "Run Dijkstra: " << t << '\n'; |
| 71 | 71 |
} |
| 72 | 72 |
|
| 73 | 73 |
template<class Value> |
| 74 | 74 |
void solve_max(ArgParser &ap, std::istream &is, std::ostream &, |
| 75 | 75 |
Value infty, DimacsDescriptor &desc) |
| 76 | 76 |
{
|
| 77 | 77 |
bool report = !ap.given("q");
|
| 78 | 78 |
Digraph g; |
| 79 | 79 |
Node s,t; |
| 80 | 80 |
Digraph::ArcMap<Value> cap(g); |
| 81 | 81 |
Timer ti; |
| 82 | 82 |
ti.restart(); |
| 83 | 83 |
readDimacsMax(is, g, cap, s, t, infty, desc); |
| 84 | 84 |
if(report) std::cerr << "Read the file: " << ti << '\n'; |
| 85 | 85 |
ti.restart(); |
| 86 | 86 |
Preflow<Digraph, Digraph::ArcMap<Value> > pre(g,cap,s,t); |
| 87 | 87 |
if(report) std::cerr << "Setup Preflow class: " << ti << '\n'; |
| 88 | 88 |
ti.restart(); |
| 89 | 89 |
pre.run(); |
| 90 | 90 |
if(report) std::cerr << "Run Preflow: " << ti << '\n'; |
| 91 |
if(report) std::cerr << "\nMax flow value: " << pre.flowValue() << '\n'; |
|
| 91 |
if(report) std::cerr << "\nMax flow value: " << pre.flowValue() << '\n'; |
|
| 92 | 92 |
} |
| 93 | 93 |
|
| 94 | 94 |
template<class Value, class LargeValue> |
| 95 | 95 |
void solve_min(ArgParser &ap, std::istream &is, std::ostream &, |
| 96 | 96 |
Value infty, DimacsDescriptor &desc) |
| 97 | 97 |
{
|
| 98 | 98 |
bool report = !ap.given("q");
|
| 99 | 99 |
Digraph g; |
| 100 | 100 |
Digraph::ArcMap<Value> lower(g), cap(g), cost(g); |
| 101 | 101 |
Digraph::NodeMap<Value> sup(g); |
| 102 | 102 |
Timer ti; |
| 103 | 103 |
|
| 104 | 104 |
ti.restart(); |
| 105 | 105 |
readDimacsMin(is, g, lower, cap, cost, sup, infty, desc); |
| 106 | 106 |
ti.stop(); |
| 107 | 107 |
Value sum_sup = 0; |
| 108 | 108 |
for (Digraph::NodeIt n(g); n != INVALID; ++n) {
|
| 109 | 109 |
sum_sup += sup[n]; |
| 110 | 110 |
} |
| 111 | 111 |
if (report) {
|
| 112 | 112 |
std::cerr << "Sum of supply values: " << sum_sup << "\n"; |
| 113 | 113 |
if (sum_sup <= 0) |
| 114 | 114 |
std::cerr << "GEQ supply contraints are used for NetworkSimplex\n\n"; |
| 115 | 115 |
else |
| ... | ... |
@@ -127,67 +127,67 @@ |
| 127 | 127 |
if (report) {
|
| 128 | 128 |
std::cerr << "Run NetworkSimplex: " << ti << "\n\n"; |
| 129 | 129 |
std::cerr << "Feasible flow: " << (res ? "found" : "not found") << '\n'; |
| 130 | 130 |
if (res) std::cerr << "Min flow cost: " |
| 131 | 131 |
<< ns.template totalCost<LargeValue>() << '\n'; |
| 132 | 132 |
} |
| 133 | 133 |
} |
| 134 | 134 |
|
| 135 | 135 |
void solve_mat(ArgParser &ap, std::istream &is, std::ostream &, |
| 136 | 136 |
DimacsDescriptor &desc) |
| 137 | 137 |
{
|
| 138 | 138 |
bool report = !ap.given("q");
|
| 139 | 139 |
Graph g; |
| 140 | 140 |
Timer ti; |
| 141 | 141 |
ti.restart(); |
| 142 | 142 |
readDimacsMat(is, g, desc); |
| 143 | 143 |
if(report) std::cerr << "Read the file: " << ti << '\n'; |
| 144 | 144 |
ti.restart(); |
| 145 | 145 |
MaxMatching<Graph> mat(g); |
| 146 | 146 |
if(report) std::cerr << "Setup MaxMatching class: " << ti << '\n'; |
| 147 | 147 |
ti.restart(); |
| 148 | 148 |
mat.run(); |
| 149 | 149 |
if(report) std::cerr << "Run MaxMatching: " << ti << '\n'; |
| 150 | 150 |
if(report) std::cerr << "\nCardinality of max matching: " |
| 151 |
<< mat.matchingSize() << '\n'; |
|
| 151 |
<< mat.matchingSize() << '\n'; |
|
| 152 | 152 |
} |
| 153 | 153 |
|
| 154 | 154 |
|
| 155 | 155 |
template<class Value, class LargeValue> |
| 156 | 156 |
void solve(ArgParser &ap, std::istream &is, std::ostream &os, |
| 157 | 157 |
DimacsDescriptor &desc) |
| 158 | 158 |
{
|
| 159 | 159 |
std::stringstream iss(static_cast<std::string>(ap["infcap"])); |
| 160 | 160 |
Value infty; |
| 161 | 161 |
iss >> infty; |
| 162 | 162 |
if(iss.fail()) |
| 163 | 163 |
{
|
| 164 | 164 |
std::cerr << "Cannot interpret '" |
| 165 | 165 |
<< static_cast<std::string>(ap["infcap"]) << "' as infinite" |
| 166 | 166 |
<< std::endl; |
| 167 | 167 |
exit(1); |
| 168 | 168 |
} |
| 169 |
|
|
| 169 |
|
|
| 170 | 170 |
switch(desc.type) |
| 171 | 171 |
{
|
| 172 | 172 |
case DimacsDescriptor::MIN: |
| 173 | 173 |
solve_min<Value, LargeValue>(ap,is,os,infty,desc); |
| 174 | 174 |
break; |
| 175 | 175 |
case DimacsDescriptor::MAX: |
| 176 | 176 |
solve_max<Value>(ap,is,os,infty,desc); |
| 177 | 177 |
break; |
| 178 | 178 |
case DimacsDescriptor::SP: |
| 179 | 179 |
solve_sp<Value>(ap,is,os,desc); |
| 180 | 180 |
break; |
| 181 | 181 |
case DimacsDescriptor::MAT: |
| 182 | 182 |
solve_mat(ap,is,os,desc); |
| 183 | 183 |
break; |
| 184 | 184 |
default: |
| 185 | 185 |
break; |
| 186 | 186 |
} |
| 187 | 187 |
} |
| 188 | 188 |
|
| 189 | 189 |
int main(int argc, const char *argv[]) {
|
| 190 | 190 |
typedef SmartDigraph Digraph; |
| 191 | 191 |
|
| 192 | 192 |
typedef Digraph::Arc Arc; |
| 193 | 193 |
|
| ... | ... |
@@ -217,64 +217,64 @@ |
| 217 | 217 |
std::ofstream output; |
| 218 | 218 |
|
| 219 | 219 |
switch(ap.files().size()) |
| 220 | 220 |
{
|
| 221 | 221 |
case 2: |
| 222 | 222 |
output.open(ap.files()[1].c_str()); |
| 223 | 223 |
if (!output) {
|
| 224 | 224 |
throw IoError("Cannot open the file for writing", ap.files()[1]);
|
| 225 | 225 |
} |
| 226 | 226 |
case 1: |
| 227 | 227 |
input.open(ap.files()[0].c_str()); |
| 228 | 228 |
if (!input) {
|
| 229 | 229 |
throw IoError("File cannot be found", ap.files()[0]);
|
| 230 | 230 |
} |
| 231 | 231 |
case 0: |
| 232 | 232 |
break; |
| 233 | 233 |
default: |
| 234 | 234 |
std::cerr << ap.commandName() << ": too many arguments\n"; |
| 235 | 235 |
return 1; |
| 236 | 236 |
} |
| 237 | 237 |
std::istream& is = (ap.files().size()<1 ? std::cin : input); |
| 238 | 238 |
std::ostream& os = (ap.files().size()<2 ? std::cout : output); |
| 239 | 239 |
|
| 240 | 240 |
DimacsDescriptor desc = dimacsType(is); |
| 241 |
|
|
| 241 |
|
|
| 242 | 242 |
if(!ap.given("q"))
|
| 243 | 243 |
{
|
| 244 | 244 |
std::cout << "Problem type: "; |
| 245 | 245 |
switch(desc.type) |
| 246 | 246 |
{
|
| 247 | 247 |
case DimacsDescriptor::MIN: |
| 248 | 248 |
std::cout << "min"; |
| 249 | 249 |
break; |
| 250 | 250 |
case DimacsDescriptor::MAX: |
| 251 | 251 |
std::cout << "max"; |
| 252 | 252 |
break; |
| 253 | 253 |
case DimacsDescriptor::SP: |
| 254 | 254 |
std::cout << "sp"; |
| 255 | 255 |
case DimacsDescriptor::MAT: |
| 256 | 256 |
std::cout << "mat"; |
| 257 | 257 |
break; |
| 258 | 258 |
default: |
| 259 | 259 |
exit(1); |
| 260 | 260 |
break; |
| 261 | 261 |
} |
| 262 | 262 |
std::cout << "\nNum of nodes: " << desc.nodeNum; |
| 263 | 263 |
std::cout << "\nNum of arcs: " << desc.edgeNum; |
| 264 | 264 |
std::cout << "\n\n"; |
| 265 | 265 |
} |
| 266 |
|
|
| 266 |
|
|
| 267 | 267 |
if(ap.given("double"))
|
| 268 | 268 |
solve<double, double>(ap,is,os,desc); |
| 269 | 269 |
else if(ap.given("ldouble"))
|
| 270 | 270 |
solve<long double, long double>(ap,is,os,desc); |
| 271 | 271 |
#ifdef LEMON_HAVE_LONG_LONG |
| 272 | 272 |
else if(ap.given("long"))
|
| 273 | 273 |
solve<long long, long long>(ap,is,os,desc); |
| 274 | 274 |
else solve<int, long long>(ap,is,os,desc); |
| 275 | 275 |
#else |
| 276 | 276 |
else solve<int, long>(ap,is,os,desc); |
| 277 | 277 |
#endif |
| 278 | 278 |
|
| 279 | 279 |
return 0; |
| 280 | 280 |
} |
0 comments (0 inline)