!
!
!
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; |
30 | 30 |
int main(int argc, char **argv) |
31 | 31 |
{ |
32 | 32 |
// Initialize the argument parser |
33 | 33 |
ArgParser ap(argc, argv); |
34 | 34 |
int i; |
35 | 35 |
std::string s; |
36 | 36 |
double d = 1.0; |
37 | 37 |
bool b, nh; |
38 | 38 |
bool g1, g2, g3; |
39 | 39 |
|
40 | 40 |
// Add a mandatory integer option with storage reference |
41 | 41 |
ap.refOption("n", "An integer input.", i, true); |
42 | 42 |
// Add a double option with storage reference (the default value is 1.0) |
43 | 43 |
ap.refOption("val", "A double input.", d); |
44 | 44 |
// Add a double option without storage reference (the default value is 3.14) |
45 | 45 |
ap.doubleOption("val2", "A double input.", 3.14); |
46 | 46 |
// Set synonym for -val option |
47 | 47 |
ap.synonym("vals", "val"); |
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 |
|
97 | 97 |
switch(ap.files().size()) { |
98 | 98 |
case 0: |
99 | 99 |
std::cout << " No file argument was given.\n"; |
100 | 100 |
break; |
101 | 101 |
case 1: |
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. |
30 | 30 |
|
31 | 31 |
The implementation of combinatorial algorithms heavily relies on |
32 | 32 |
efficient graph implementations. LEMON offers data structures which are |
33 | 33 |
planned to be easily used in an experimental phase of implementation studies, |
34 | 34 |
and thereafter the program code can be made efficient by small modifications. |
35 | 35 |
|
36 | 36 |
The most efficient implementation of diverse applications require the |
37 | 37 |
usage of different physical graph implementations. These differences |
38 | 38 |
appear in the size of graph we require to handle, memory or time usage |
39 | 39 |
limitations or in the set of operations through which the graph can be |
40 | 40 |
accessed. LEMON provides several physical graph structures to meet |
41 | 41 |
the diverging requirements of the possible users. In order to save on |
42 | 42 |
running time or on memory usage, some structures may fail to provide |
43 | 43 |
some graph features like arc/edge or node deletion. |
44 | 44 |
|
45 | 45 |
Alteration of standard containers need a very limited number of |
46 | 46 |
operations, these together satisfy the everyday requirements. |
47 | 47 |
In the case of graph structures, different operations are needed which do |
48 | 48 |
not alter the physical graph, but gives another view. If some nodes or |
49 | 49 |
arcs have to be hidden or the reverse oriented graph have to be used, then |
50 | 50 |
this is the case. It also may happen that in a flow implementation |
51 | 51 |
the residual graph can be accessed by another algorithm, or a node-set |
52 | 52 |
is to be shrunk for another algorithm. |
53 | 53 |
LEMON also provides a variety of graphs for these requirements called |
54 | 54 |
\ref graph_adaptors "graph adaptors". Adaptors cannot be used alone but only |
55 | 55 |
in conjunction with other graph representations. |
56 | 56 |
|
57 | 57 |
You are free to use the graph structure that fit your requirements |
58 | 58 |
the best, most graph algorithms and auxiliary data structures can be used |
59 | 59 |
with any graph structure. |
60 | 60 |
|
61 | 61 |
<b>See also:</b> \ref graph_concepts "Graph Structure Concepts". |
62 | 62 |
*/ |
63 | 63 |
|
64 | 64 |
/** |
65 | 65 |
@defgroup graph_adaptors Adaptor Classes for Graphs |
66 | 66 |
@ingroup graphs |
67 | 67 |
\brief Adaptor classes for digraphs and graphs |
68 | 68 |
|
69 | 69 |
This group contains several useful adaptor classes for digraphs and graphs. |
70 | 70 |
|
71 | 71 |
The main parts of LEMON are the different graph structures, generic |
72 | 72 |
graph algorithms, graph concepts, which couple them, and graph |
73 | 73 |
adaptors. While the previous notions are more or less clear, the |
74 | 74 |
latter one needs further explanation. Graph adaptors are graph classes |
75 | 75 |
which serve for considering graph structures in different ways. |
76 | 76 |
|
77 | 77 |
A short example makes this much clearer. Suppose that we have an |
78 | 78 |
instance \c g of a directed graph type, say ListDigraph and an algorithm |
79 | 79 |
\code |
80 | 80 |
template <typename Digraph> |
81 | 81 |
int algorithm(const Digraph&); |
82 | 82 |
\endcode |
83 | 83 |
is needed to run on the reverse oriented graph. It may be expensive |
84 | 84 |
(in time or in memory usage) to copy \c g with the reversed |
85 | 85 |
arcs. In this case, an adaptor class is used, which (according |
86 | 86 |
to LEMON \ref concepts::Digraph "digraph concepts") works as a digraph. |
87 | 87 |
The adaptor uses the original digraph structure and digraph operations when |
88 | 88 |
methods of the reversed oriented graph are called. This means that the adaptor |
89 | 89 |
have minor memory usage, and do not perform sophisticated algorithmic |
90 | 90 |
actions. The purpose of it is to give a tool for the cases when a |
91 | 91 |
graph have to be used in a specific alteration. If this alteration is |
92 | 92 |
obtained by a usual construction like filtering the node or the arc set or |
93 | 93 |
considering a new orientation, then an adaptor is worthwhile to use. |
94 | 94 |
To come back to the reverse oriented graph, in this situation |
95 | 95 |
\code |
96 | 96 |
template<typename Digraph> class ReverseDigraph; |
97 | 97 |
\endcode |
98 | 98 |
template class can be used. The code looks as follows |
99 | 99 |
\code |
100 | 100 |
ListDigraph g; |
101 | 101 |
ReverseDigraph<ListDigraph> rg(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 |
/** |
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 | 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 | 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. |
30 | 30 |
|
31 | 31 |
Formally, let \f$G=(V,A)\f$ be a digraph, \f$lower: A\rightarrow\mathbf{R}\f$, |
32 | 32 |
\f$upper: A\rightarrow\mathbf{R}\cup\{+\infty\}\f$ denote the lower and |
33 | 33 |
upper bounds for the flow values on the arcs, for which |
34 | 34 |
\f$lower(uv) \leq upper(uv)\f$ must hold for all \f$uv\in A\f$, |
35 | 35 |
\f$cost: A\rightarrow\mathbf{R}\f$ denotes the cost per unit flow |
36 | 36 |
on the arcs and \f$sup: V\rightarrow\mathbf{R}\f$ denotes the |
37 | 37 |
signed supply values of the nodes. |
38 | 38 |
If \f$sup(u)>0\f$, then \f$u\f$ is a supply node with \f$sup(u)\f$ |
39 | 39 |
supply, if \f$sup(u)<0\f$, then \f$u\f$ is a demand node with |
40 | 40 |
\f$-sup(u)\f$ demand. |
41 | 41 |
A minimum cost flow is an \f$f: A\rightarrow\mathbf{R}\f$ solution |
42 | 42 |
of the following optimization problem. |
43 | 43 |
|
44 | 44 |
\f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f] |
45 | 45 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \geq |
46 | 46 |
sup(u) \quad \forall u\in V \f] |
47 | 47 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f] |
48 | 48 |
|
49 | 49 |
The sum of the supply values, i.e. \f$\sum_{u\in V} sup(u)\f$ must be |
50 | 50 |
zero or negative in order to have a feasible solution (since the sum |
51 | 51 |
of the expressions on the left-hand side of the inequalities is zero). |
52 | 52 |
It means that the total demand must be greater or equal to the total |
53 | 53 |
supply and all the supplies have to be carried out from the supply nodes, |
54 | 54 |
but there could be demands that are not satisfied. |
55 | 55 |
If \f$\sum_{u\in V} sup(u)\f$ is zero, then all the supply/demand |
56 | 56 |
constraints have to be satisfied with equality, i.e. all demands |
57 | 57 |
have to be satisfied and all supplies have to be used. |
58 | 58 |
|
59 | 59 |
|
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] |
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> |
30 | 30 |
#include <lemon/bits/variant.h> |
31 | 31 |
|
32 | 32 |
#include <lemon/bits/graph_adaptor_extender.h> |
33 | 33 |
#include <lemon/bits/map_extender.h> |
34 | 34 |
#include <lemon/tolerance.h> |
35 | 35 |
|
36 | 36 |
#include <algorithm> |
37 | 37 |
|
38 | 38 |
namespace lemon { |
39 | 39 |
|
40 | 40 |
#ifdef _MSC_VER |
41 | 41 |
#define LEMON_SCOPE_FIX(OUTER, NESTED) OUTER::NESTED |
42 | 42 |
#else |
43 | 43 |
#define LEMON_SCOPE_FIX(OUTER, NESTED) typename OUTER::template NESTED |
44 | 44 |
#endif |
45 | 45 |
|
46 | 46 |
template<typename DGR> |
47 | 47 |
class DigraphAdaptorBase { |
48 | 48 |
public: |
49 | 49 |
typedef DGR Digraph; |
50 | 50 |
typedef DigraphAdaptorBase Adaptor; |
51 | 51 |
|
52 | 52 |
protected: |
53 | 53 |
DGR* _digraph; |
54 | 54 |
DigraphAdaptorBase() : _digraph(0) { } |
55 | 55 |
void initialize(DGR& digraph) { _digraph = &digraph; } |
56 | 56 |
|
57 | 57 |
public: |
58 | 58 |
DigraphAdaptorBase(DGR& digraph) : _digraph(&digraph) { } |
59 | 59 |
|
60 | 60 |
typedef typename DGR::Node Node; |
61 | 61 |
typedef typename DGR::Arc Arc; |
62 | 62 |
|
63 | 63 |
void first(Node& i) const { _digraph->first(i); } |
64 | 64 |
void first(Arc& i) const { _digraph->first(i); } |
65 | 65 |
void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); } |
66 | 66 |
void firstOut(Arc& i, const Node& n ) const { _digraph->firstOut(i, n); } |
67 | 67 |
|
68 | 68 |
void next(Node& i) const { _digraph->next(i); } |
69 | 69 |
void next(Arc& i) const { _digraph->next(i); } |
70 | 70 |
void nextIn(Arc& i) const { _digraph->nextIn(i); } |
71 | 71 |
void nextOut(Arc& i) const { _digraph->nextOut(i); } |
72 | 72 |
|
73 | 73 |
Node source(const Arc& a) const { return _digraph->source(a); } |
74 | 74 |
Node target(const Arc& a) const { return _digraph->target(a); } |
75 | 75 |
|
76 | 76 |
typedef NodeNumTagIndicator<DGR> NodeNumTag; |
77 | 77 |
int nodeNum() const { return _digraph->nodeNum(); } |
78 | 78 |
|
79 | 79 |
typedef ArcNumTagIndicator<DGR> ArcNumTag; |
80 | 80 |
int arcNum() const { return _digraph->arcNum(); } |
81 | 81 |
|
82 | 82 |
typedef FindArcTagIndicator<DGR> FindArcTag; |
83 | 83 |
Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) const { |
84 | 84 |
return _digraph->findArc(u, v, prev); |
85 | 85 |
} |
86 | 86 |
|
87 | 87 |
Node addNode() { return _digraph->addNode(); } |
88 | 88 |
Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); } |
89 | 89 |
|
90 | 90 |
void erase(const Node& n) { _digraph->erase(n); } |
91 | 91 |
void erase(const Arc& a) { _digraph->erase(a); } |
92 | 92 |
|
93 | 93 |
void clear() { _digraph->clear(); } |
94 | 94 |
|
95 | 95 |
int id(const Node& n) const { return _digraph->id(n); } |
96 | 96 |
int id(const Arc& a) const { return _digraph->id(a); } |
97 | 97 |
|
98 | 98 |
Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); } |
99 | 99 |
Arc arcFromId(int ix) const { return _digraph->arcFromId(ix); } |
100 | 100 |
|
101 | 101 |
int maxNodeId() const { return _digraph->maxNodeId(); } |
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 | 29 |
|
30 | 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; |
55 | 55 |
break; |
56 | 56 |
case DOUBLE: |
57 | 57 |
delete i->second.double_p; |
58 | 58 |
break; |
59 | 59 |
case INTEGER: |
60 | 60 |
delete i->second.int_p; |
61 | 61 |
break; |
62 | 62 |
case UNKNOWN: |
63 | 63 |
break; |
64 | 64 |
case FUNC: |
65 | 65 |
break; |
66 | 66 |
} |
67 | 67 |
} |
68 | 68 |
|
69 | 69 |
|
70 | 70 |
ArgParser &ArgParser::intOption(const std::string &name, |
71 | 71 |
const std::string &help, |
72 | 72 |
int value, bool obl) |
73 | 73 |
{ |
74 | 74 |
ParData p; |
75 | 75 |
p.int_p=new int(value); |
76 | 76 |
p.self_delete=true; |
77 | 77 |
p.help=help; |
78 | 78 |
p.type=INTEGER; |
79 | 79 |
p.mandatory=obl; |
80 | 80 |
_opts[name]=p; |
81 | 81 |
return *this; |
82 | 82 |
} |
83 | 83 |
|
84 | 84 |
ArgParser &ArgParser::doubleOption(const std::string &name, |
85 | 85 |
const std::string &help, |
86 | 86 |
double value, bool obl) |
87 | 87 |
{ |
88 | 88 |
ParData p; |
89 | 89 |
p.double_p=new double(value); |
90 | 90 |
p.self_delete=true; |
91 | 91 |
p.help=help; |
92 | 92 |
p.type=DOUBLE; |
93 | 93 |
p.mandatory=obl; |
94 | 94 |
_opts[name]=p; |
95 | 95 |
return *this; |
96 | 96 |
} |
97 | 97 |
|
98 | 98 |
ArgParser &ArgParser::boolOption(const std::string &name, |
99 | 99 |
const std::string &help, |
100 | 100 |
bool value, bool obl) |
101 | 101 |
{ |
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 |
}; |
73 | 73 |
|
74 | 74 |
|
75 | 75 |
///Command line arguments parser |
76 | 76 |
|
77 | 77 |
///\ingroup misc |
78 | 78 |
///Command line arguments parser. |
79 | 79 |
/// |
80 | 80 |
///For a complete example see the \ref arg_parser_demo.cc demo file. |
81 | 81 |
class ArgParser { |
82 | 82 |
|
83 | 83 |
static void _showHelp(void *p); |
84 | 84 |
protected: |
85 | 85 |
|
86 | 86 |
int _argc; |
87 | 87 |
const char * const *_argv; |
88 | 88 |
|
89 | 89 |
enum OptType { UNKNOWN=0, BOOL=1, STRING=2, DOUBLE=3, INTEGER=4, FUNC=5 }; |
90 | 90 |
|
91 | 91 |
class ParData { |
92 | 92 |
public: |
93 | 93 |
union { |
94 | 94 |
bool *bool_p; |
95 | 95 |
int *int_p; |
96 | 96 |
double *double_p; |
97 | 97 |
std::string *string_p; |
98 | 98 |
struct { |
99 | 99 |
void (*p)(void *); |
100 | 100 |
void *data; |
101 | 101 |
} func_p; |
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 | 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. |
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> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/path.h> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
///Default traits class of Bfs class. |
36 | 36 |
|
37 | 37 |
///Default traits class of Bfs class. |
38 | 38 |
///\tparam GR Digraph type. |
39 | 39 |
template<class GR> |
40 | 40 |
struct BfsDefaultTraits |
41 | 41 |
{ |
42 | 42 |
///The type of the digraph the algorithm runs on. |
43 | 43 |
typedef GR Digraph; |
44 | 44 |
|
45 | 45 |
///\brief The type of the map that stores the predecessor |
46 | 46 |
///arcs of the shortest paths. |
47 | 47 |
/// |
48 | 48 |
///The type of the map that stores the predecessor |
49 | 49 |
///arcs of the shortest paths. |
50 | 50 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
52 | 52 |
///Instantiates a \c PredMap. |
53 | 53 |
|
54 | 54 |
///This function instantiates a \ref PredMap. |
55 | 55 |
///\param g is the digraph, to which we would like to define the |
56 | 56 |
///\ref PredMap. |
57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
58 | 58 |
{ |
59 | 59 |
return new PredMap(g); |
60 | 60 |
} |
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); |
110 | 111 |
} |
111 | 112 |
}; |
112 | 113 |
|
113 | 114 |
///%BFS algorithm class. |
114 | 115 |
|
115 | 116 |
///\ingroup search |
116 | 117 |
///This class provides an efficient implementation of the %BFS algorithm. |
117 | 118 |
/// |
118 | 119 |
///There is also a \ref bfs() "function-type interface" for the BFS |
119 | 120 |
///algorithm, which is convenient in the simplier cases and it can be |
120 | 121 |
///used easier. |
121 | 122 |
/// |
122 | 123 |
///\tparam GR The type of the digraph the algorithm runs on. |
123 | 124 |
///The default type is \ref ListDigraph. |
124 | 125 |
///\tparam TR The traits class that defines various types used by the |
125 | 126 |
///algorithm. By default, it is \ref BfsDefaultTraits |
126 | 127 |
///"BfsDefaultTraits<GR>". |
127 | 128 |
///In most cases, this parameter should not be set directly, |
128 | 129 |
///consider to use the named template parameters instead. |
129 | 130 |
#ifdef DOXYGEN |
130 | 131 |
template <typename GR, |
131 | 132 |
typename TR> |
132 | 133 |
#else |
133 | 134 |
template <typename GR=ListDigraph, |
134 | 135 |
typename TR=BfsDefaultTraits<GR> > |
135 | 136 |
#endif |
136 | 137 |
class Bfs { |
137 | 138 |
public: |
138 | 139 |
|
139 | 140 |
///The type of the digraph the algorithm runs on. |
140 | 141 |
typedef typename TR::Digraph Digraph; |
141 | 142 |
|
142 | 143 |
///\brief The type of the map that stores the predecessor arcs of the |
143 | 144 |
///shortest paths. |
144 | 145 |
typedef typename TR::PredMap PredMap; |
145 | 146 |
///The type of the map that stores the distances of the nodes. |
146 | 147 |
typedef typename TR::DistMap DistMap; |
147 | 148 |
///The type of the map that indicates which nodes are reached. |
148 | 149 |
typedef typename TR::ReachedMap ReachedMap; |
149 | 150 |
///The type of the map that indicates which nodes are processed. |
150 | 151 |
typedef typename TR::ProcessedMap ProcessedMap; |
151 | 152 |
///The type of the paths. |
152 | 153 |
typedef PredMapPath<Digraph, PredMap> Path; |
153 | 154 |
|
154 | 155 |
///The \ref BfsDefaultTraits "traits class" of the algorithm. |
155 | 156 |
typedef TR Traits; |
156 | 157 |
|
157 | 158 |
private: |
158 | 159 |
|
159 | 160 |
typedef typename Digraph::Node Node; |
160 | 161 |
typedef typename Digraph::NodeIt NodeIt; |
161 | 162 |
typedef typename Digraph::Arc Arc; |
162 | 163 |
typedef typename Digraph::OutArcIt OutArcIt; |
163 | 164 |
|
164 | 165 |
//Pointer to the underlying digraph. |
165 | 166 |
const Digraph *G; |
166 | 167 |
//Pointer to the map of predecessor arcs. |
167 | 168 |
PredMap *_pred; |
168 | 169 |
//Indicates if _pred is locally allocated (true) or not. |
169 | 170 |
bool local_pred; |
170 | 171 |
//Pointer to the map of distances. |
171 | 172 |
DistMap *_dist; |
172 | 173 |
//Indicates if _dist is locally allocated (true) or not. |
173 | 174 |
bool local_dist; |
174 | 175 |
//Pointer to the map of reached status of the nodes. |
175 | 176 |
ReachedMap *_reached; |
176 | 177 |
//Indicates if _reached is locally allocated (true) or not. |
177 | 178 |
bool local_reached; |
178 | 179 |
//Pointer to the map of processed status of the nodes. |
179 | 180 |
ProcessedMap *_processed; |
180 | 181 |
//Indicates if _processed is locally allocated (true) or not. |
181 | 182 |
bool local_processed; |
182 | 183 |
|
183 | 184 |
std::vector<typename Digraph::Node> _queue; |
184 | 185 |
int _queue_head,_queue_tail,_queue_next_dist; |
185 | 186 |
int _curr_dist; |
186 | 187 |
|
187 | 188 |
//Creates the maps if necessary. |
188 | 189 |
void create_maps() |
189 | 190 |
{ |
190 | 191 |
if(!_pred) { |
191 | 192 |
local_pred = true; |
192 | 193 |
_pred = Traits::createPredMap(*G); |
193 | 194 |
} |
194 | 195 |
if(!_dist) { |
195 | 196 |
local_dist = true; |
196 | 197 |
_dist = Traits::createDistMap(*G); |
197 | 198 |
} |
198 | 199 |
if(!_reached) { |
199 | 200 |
local_reached = true; |
200 | 201 |
_reached = Traits::createReachedMap(*G); |
201 | 202 |
} |
202 | 203 |
if(!_processed) { |
203 | 204 |
local_processed = true; |
204 | 205 |
_processed = Traits::createProcessedMap(*G); |
205 | 206 |
} |
206 | 207 |
} |
207 | 208 |
|
208 | 209 |
protected: |
209 | 210 |
|
210 | 211 |
Bfs() {} |
211 | 212 |
|
212 | 213 |
public: |
213 | 214 |
|
214 | 215 |
typedef Bfs Create; |
215 | 216 |
|
216 | 217 |
///\name Named Template Parameters |
217 | 218 |
|
218 | 219 |
///@{ |
219 | 220 |
|
220 | 221 |
template <class T> |
221 | 222 |
struct SetPredMapTraits : public Traits { |
222 | 223 |
typedef T PredMap; |
223 | 224 |
static PredMap *createPredMap(const Digraph &) |
224 | 225 |
{ |
225 | 226 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
226 | 227 |
return 0; // ignore warnings |
227 | 228 |
} |
228 | 229 |
}; |
229 | 230 |
///\brief \ref named-templ-param "Named parameter" for setting |
230 | 231 |
///\c PredMap type. |
231 | 232 |
/// |
232 | 233 |
///\ref named-templ-param "Named parameter" for setting |
233 | 234 |
///\c PredMap type. |
234 | 235 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
235 | 236 |
template <class T> |
236 | 237 |
struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > { |
237 | 238 |
typedef Bfs< Digraph, SetPredMapTraits<T> > Create; |
238 | 239 |
}; |
239 | 240 |
|
240 | 241 |
template <class T> |
241 | 242 |
struct SetDistMapTraits : public Traits { |
242 | 243 |
typedef T DistMap; |
243 | 244 |
static DistMap *createDistMap(const Digraph &) |
244 | 245 |
{ |
245 | 246 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
246 | 247 |
return 0; // ignore warnings |
247 | 248 |
} |
248 | 249 |
}; |
249 | 250 |
///\brief \ref named-templ-param "Named parameter" for setting |
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 |
}; |
299 | 301 |
|
300 | 302 |
struct SetStandardProcessedMapTraits : public Traits { |
301 | 303 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
302 | 304 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
303 | 305 |
{ |
304 | 306 |
return new ProcessedMap(g); |
305 | 307 |
return 0; // ignore warnings |
306 | 308 |
} |
307 | 309 |
}; |
308 | 310 |
///\brief \ref named-templ-param "Named parameter" for setting |
309 | 311 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
310 | 312 |
/// |
311 | 313 |
///\ref named-templ-param "Named parameter" for setting |
312 | 314 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
313 | 315 |
///If you don't set it explicitly, it will be automatically allocated. |
314 | 316 |
struct SetStandardProcessedMap : |
315 | 317 |
public Bfs< Digraph, SetStandardProcessedMapTraits > { |
316 | 318 |
typedef Bfs< Digraph, SetStandardProcessedMapTraits > Create; |
317 | 319 |
}; |
318 | 320 |
|
319 | 321 |
///@} |
320 | 322 |
|
321 | 323 |
public: |
322 | 324 |
|
323 | 325 |
///Constructor. |
324 | 326 |
|
325 | 327 |
///Constructor. |
326 | 328 |
///\param g The digraph the algorithm runs on. |
327 | 329 |
Bfs(const Digraph &g) : |
328 | 330 |
G(&g), |
329 | 331 |
_pred(NULL), local_pred(false), |
330 | 332 |
_dist(NULL), local_dist(false), |
331 | 333 |
_reached(NULL), local_reached(false), |
332 | 334 |
_processed(NULL), local_processed(false) |
333 | 335 |
{ } |
334 | 336 |
|
335 | 337 |
///Destructor. |
336 | 338 |
~Bfs() |
337 | 339 |
{ |
338 | 340 |
if(local_pred) delete _pred; |
339 | 341 |
if(local_dist) delete _dist; |
340 | 342 |
if(local_reached) delete _reached; |
341 | 343 |
if(local_processed) delete _processed; |
342 | 344 |
} |
343 | 345 |
|
344 | 346 |
///Sets the map that stores the predecessor arcs. |
345 | 347 |
|
346 | 348 |
///Sets the map that stores the predecessor arcs. |
347 | 349 |
///If you don't use this function before calling \ref run(Node) "run()" |
348 | 350 |
///or \ref init(), an instance will be allocated automatically. |
349 | 351 |
///The destructor deallocates this automatically allocated map, |
350 | 352 |
///of course. |
351 | 353 |
///\return <tt> (*this) </tt> |
352 | 354 |
Bfs &predMap(PredMap &m) |
353 | 355 |
{ |
354 | 356 |
if(local_pred) { |
355 | 357 |
delete _pred; |
356 | 358 |
local_pred=false; |
357 | 359 |
} |
358 | 360 |
_pred = &m; |
359 | 361 |
return *this; |
360 | 362 |
} |
361 | 363 |
|
362 | 364 |
///Sets the map that indicates which nodes are reached. |
363 | 365 |
|
364 | 366 |
///Sets the map that indicates which nodes are reached. |
365 | 367 |
///If you don't use this function before calling \ref run(Node) "run()" |
366 | 368 |
///or \ref init(), an instance will be allocated automatically. |
367 | 369 |
///The destructor deallocates this automatically allocated map, |
368 | 370 |
///of course. |
369 | 371 |
///\return <tt> (*this) </tt> |
370 | 372 |
Bfs &reachedMap(ReachedMap &m) |
... | ... |
@@ -779,193 +781,194 @@ |
779 | 781 |
///the given node. |
780 | 782 |
/// |
781 | 783 |
///This function returns the 'previous node' of the shortest path |
782 | 784 |
///tree for the node \c v, i.e. it returns the last but one node |
783 | 785 |
///of a shortest path from a root to \c v. It is \c INVALID |
784 | 786 |
///if \c v is not reached from the root(s) or if \c v is a root. |
785 | 787 |
/// |
786 | 788 |
///The shortest path tree used here is equal to the shortest path |
787 | 789 |
///tree used in \ref predArc() and \ref predMap(). |
788 | 790 |
/// |
789 | 791 |
///\pre Either \ref run(Node) "run()" or \ref init() |
790 | 792 |
///must be called before using this function. |
791 | 793 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
792 | 794 |
G->source((*_pred)[v]); } |
793 | 795 |
|
794 | 796 |
///\brief Returns a const reference to the node map that stores the |
795 | 797 |
/// distances of the nodes. |
796 | 798 |
/// |
797 | 799 |
///Returns a const reference to the node map that stores the distances |
798 | 800 |
///of the nodes calculated by the algorithm. |
799 | 801 |
/// |
800 | 802 |
///\pre Either \ref run(Node) "run()" or \ref init() |
801 | 803 |
///must be called before using this function. |
802 | 804 |
const DistMap &distMap() const { return *_dist;} |
803 | 805 |
|
804 | 806 |
///\brief Returns a const reference to the node map that stores the |
805 | 807 |
///predecessor arcs. |
806 | 808 |
/// |
807 | 809 |
///Returns a const reference to the node map that stores the predecessor |
808 | 810 |
///arcs, which form the shortest path tree (forest). |
809 | 811 |
/// |
810 | 812 |
///\pre Either \ref run(Node) "run()" or \ref init() |
811 | 813 |
///must be called before using this function. |
812 | 814 |
const PredMap &predMap() const { return *_pred;} |
813 | 815 |
|
814 | 816 |
///Checks if the given node is reached from the root(s). |
815 | 817 |
|
816 | 818 |
///Returns \c true if \c v is reached from the root(s). |
817 | 819 |
/// |
818 | 820 |
///\pre Either \ref run(Node) "run()" or \ref init() |
819 | 821 |
///must be called before using this function. |
820 | 822 |
bool reached(Node v) const { return (*_reached)[v]; } |
821 | 823 |
|
822 | 824 |
///@} |
823 | 825 |
}; |
824 | 826 |
|
825 | 827 |
///Default traits class of bfs() function. |
826 | 828 |
|
827 | 829 |
///Default traits class of bfs() function. |
828 | 830 |
///\tparam GR Digraph type. |
829 | 831 |
template<class GR> |
830 | 832 |
struct BfsWizardDefaultTraits |
831 | 833 |
{ |
832 | 834 |
///The type of the digraph the algorithm runs on. |
833 | 835 |
typedef GR Digraph; |
834 | 836 |
|
835 | 837 |
///\brief The type of the map that stores the predecessor |
836 | 838 |
///arcs of the shortest paths. |
837 | 839 |
/// |
838 | 840 |
///The type of the map that stores the predecessor |
839 | 841 |
///arcs of the shortest paths. |
840 | 842 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
841 | 843 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
842 | 844 |
///Instantiates a PredMap. |
843 | 845 |
|
844 | 846 |
///This function instantiates a PredMap. |
845 | 847 |
///\param g is the digraph, to which we would like to define the |
846 | 848 |
///PredMap. |
847 | 849 |
static PredMap *createPredMap(const Digraph &g) |
848 | 850 |
{ |
849 | 851 |
return new PredMap(g); |
850 | 852 |
} |
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); |
900 | 903 |
} |
901 | 904 |
|
902 | 905 |
///The type of the shortest paths. |
903 | 906 |
|
904 | 907 |
///The type of the shortest paths. |
905 | 908 |
///It must conform to the \ref concepts::Path "Path" concept. |
906 | 909 |
typedef lemon::Path<Digraph> Path; |
907 | 910 |
}; |
908 | 911 |
|
909 | 912 |
/// Default traits class used by BfsWizard |
910 | 913 |
|
911 | 914 |
/// Default traits class used by BfsWizard. |
912 | 915 |
/// \tparam GR The type of the digraph. |
913 | 916 |
template<class GR> |
914 | 917 |
class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
915 | 918 |
{ |
916 | 919 |
|
917 | 920 |
typedef BfsWizardDefaultTraits<GR> Base; |
918 | 921 |
protected: |
919 | 922 |
//The type of the nodes in the digraph. |
920 | 923 |
typedef typename Base::Digraph::Node Node; |
921 | 924 |
|
922 | 925 |
//Pointer to the digraph the algorithm runs on. |
923 | 926 |
void *_g; |
924 | 927 |
//Pointer to the map of reached nodes. |
925 | 928 |
void *_reached; |
926 | 929 |
//Pointer to the map of processed nodes. |
927 | 930 |
void *_processed; |
928 | 931 |
//Pointer to the map of predecessors arcs. |
929 | 932 |
void *_pred; |
930 | 933 |
//Pointer to the map of distances. |
931 | 934 |
void *_dist; |
932 | 935 |
//Pointer to the shortest path to the target node. |
933 | 936 |
void *_path; |
934 | 937 |
//Pointer to the distance of the target node. |
935 | 938 |
int *_di; |
936 | 939 |
|
937 | 940 |
public: |
938 | 941 |
/// Constructor. |
939 | 942 |
|
940 | 943 |
/// This constructor does not require parameters, it initiates |
941 | 944 |
/// all of the attributes to \c 0. |
942 | 945 |
BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
943 | 946 |
_dist(0), _path(0), _di(0) {} |
944 | 947 |
|
945 | 948 |
/// Constructor. |
946 | 949 |
|
947 | 950 |
/// This constructor requires one parameter, |
948 | 951 |
/// others are initiated to \c 0. |
949 | 952 |
/// \param g The digraph the algorithm runs on. |
950 | 953 |
BfsWizardBase(const GR &g) : |
951 | 954 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
952 | 955 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
953 | 956 |
|
954 | 957 |
}; |
955 | 958 |
|
956 | 959 |
/// Auxiliary class for the function-type interface of BFS algorithm. |
957 | 960 |
|
958 | 961 |
/// This auxiliary class is created to implement the |
959 | 962 |
/// \ref bfs() "function-type interface" of \ref Bfs algorithm. |
960 | 963 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
961 | 964 |
/// functions and features of the plain \ref Bfs. |
962 | 965 |
/// |
963 | 966 |
/// This class should only be used through the \ref bfs() function, |
964 | 967 |
/// which makes it easier to use the algorithm. |
965 | 968 |
/// |
966 | 969 |
/// \tparam TR The traits class that defines various types used by the |
967 | 970 |
/// algorithm. |
968 | 971 |
template<class TR> |
969 | 972 |
class BfsWizard : public TR |
970 | 973 |
{ |
971 | 974 |
typedef TR Base; |
... | ... |
@@ -1172,193 +1175,194 @@ |
1172 | 1175 |
///This function also has several \ref named-func-param "named parameters", |
1173 | 1176 |
///they are declared as the members of class \ref BfsWizard. |
1174 | 1177 |
///The following examples show how to use these parameters. |
1175 | 1178 |
///\code |
1176 | 1179 |
/// // Compute shortest path from node s to each node |
1177 | 1180 |
/// bfs(g).predMap(preds).distMap(dists).run(s); |
1178 | 1181 |
/// |
1179 | 1182 |
/// // Compute shortest path from s to t |
1180 | 1183 |
/// bool reached = bfs(g).path(p).dist(d).run(s,t); |
1181 | 1184 |
///\endcode |
1182 | 1185 |
///\warning Don't forget to put the \ref BfsWizard::run(Node) "run()" |
1183 | 1186 |
///to the end of the parameter list. |
1184 | 1187 |
///\sa BfsWizard |
1185 | 1188 |
///\sa Bfs |
1186 | 1189 |
template<class GR> |
1187 | 1190 |
BfsWizard<BfsWizardBase<GR> > |
1188 | 1191 |
bfs(const GR &digraph) |
1189 | 1192 |
{ |
1190 | 1193 |
return BfsWizard<BfsWizardBase<GR> >(digraph); |
1191 | 1194 |
} |
1192 | 1195 |
|
1193 | 1196 |
#ifdef DOXYGEN |
1194 | 1197 |
/// \brief Visitor class for BFS. |
1195 | 1198 |
/// |
1196 | 1199 |
/// This class defines the interface of the BfsVisit events, and |
1197 | 1200 |
/// it could be the base of a real visitor class. |
1198 | 1201 |
template <typename GR> |
1199 | 1202 |
struct BfsVisitor { |
1200 | 1203 |
typedef GR Digraph; |
1201 | 1204 |
typedef typename Digraph::Arc Arc; |
1202 | 1205 |
typedef typename Digraph::Node Node; |
1203 | 1206 |
/// \brief Called for the source node(s) of the BFS. |
1204 | 1207 |
/// |
1205 | 1208 |
/// This function is called for the source node(s) of the BFS. |
1206 | 1209 |
void start(const Node& node) {} |
1207 | 1210 |
/// \brief Called when a node is reached first time. |
1208 | 1211 |
/// |
1209 | 1212 |
/// This function is called when a node is reached first time. |
1210 | 1213 |
void reach(const Node& node) {} |
1211 | 1214 |
/// \brief Called when a node is processed. |
1212 | 1215 |
/// |
1213 | 1216 |
/// This function is called when a node is processed. |
1214 | 1217 |
void process(const Node& node) {} |
1215 | 1218 |
/// \brief Called when an arc reaches a new node. |
1216 | 1219 |
/// |
1217 | 1220 |
/// This function is called when the BFS finds an arc whose target node |
1218 | 1221 |
/// is not reached yet. |
1219 | 1222 |
void discover(const Arc& arc) {} |
1220 | 1223 |
/// \brief Called when an arc is examined but its target node is |
1221 | 1224 |
/// already discovered. |
1222 | 1225 |
/// |
1223 | 1226 |
/// This function is called when an arc is examined but its target node is |
1224 | 1227 |
/// already discovered. |
1225 | 1228 |
void examine(const Arc& arc) {} |
1226 | 1229 |
}; |
1227 | 1230 |
#else |
1228 | 1231 |
template <typename GR> |
1229 | 1232 |
struct BfsVisitor { |
1230 | 1233 |
typedef GR Digraph; |
1231 | 1234 |
typedef typename Digraph::Arc Arc; |
1232 | 1235 |
typedef typename Digraph::Node Node; |
1233 | 1236 |
void start(const Node&) {} |
1234 | 1237 |
void reach(const Node&) {} |
1235 | 1238 |
void process(const Node&) {} |
1236 | 1239 |
void discover(const Arc&) {} |
1237 | 1240 |
void examine(const Arc&) {} |
1238 | 1241 |
|
1239 | 1242 |
template <typename _Visitor> |
1240 | 1243 |
struct Constraints { |
1241 | 1244 |
void constraints() { |
1242 | 1245 |
Arc arc; |
1243 | 1246 |
Node node; |
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 |
/// |
1293 | 1297 |
/// This interface of the BFS algorithm should be used in special cases |
1294 | 1298 |
/// when extra actions have to be performed in connection with certain |
1295 | 1299 |
/// events of the BFS algorithm. Otherwise consider to use Bfs or bfs() |
1296 | 1300 |
/// instead. |
1297 | 1301 |
/// |
1298 | 1302 |
/// \tparam GR The type of the digraph the algorithm runs on. |
1299 | 1303 |
/// The default type is \ref ListDigraph. |
1300 | 1304 |
/// The value of GR is not used directly by \ref BfsVisit, |
1301 | 1305 |
/// it is only passed to \ref BfsVisitDefaultTraits. |
1302 | 1306 |
/// \tparam VS The Visitor type that is used by the algorithm. |
1303 | 1307 |
/// \ref BfsVisitor "BfsVisitor<GR>" is an empty visitor, which |
1304 | 1308 |
/// does not observe the BFS events. If you want to observe the BFS |
1305 | 1309 |
/// events, you should implement your own visitor class. |
1306 | 1310 |
/// \tparam TR The traits class that defines various types used by the |
1307 | 1311 |
/// algorithm. By default, it is \ref BfsVisitDefaultTraits |
1308 | 1312 |
/// "BfsVisitDefaultTraits<GR>". |
1309 | 1313 |
/// In most cases, this parameter should not be set directly, |
1310 | 1314 |
/// consider to use the named template parameters instead. |
1311 | 1315 |
#ifdef DOXYGEN |
1312 | 1316 |
template <typename GR, typename VS, typename TR> |
1313 | 1317 |
#else |
1314 | 1318 |
template <typename GR = ListDigraph, |
1315 | 1319 |
typename VS = BfsVisitor<GR>, |
1316 | 1320 |
typename TR = BfsVisitDefaultTraits<GR> > |
1317 | 1321 |
#endif |
1318 | 1322 |
class BfsVisit { |
1319 | 1323 |
public: |
1320 | 1324 |
|
1321 | 1325 |
///The traits class. |
1322 | 1326 |
typedef TR Traits; |
1323 | 1327 |
|
1324 | 1328 |
///The type of the digraph the algorithm runs on. |
1325 | 1329 |
typedef typename Traits::Digraph Digraph; |
1326 | 1330 |
|
1327 | 1331 |
///The visitor type used by the algorithm. |
1328 | 1332 |
typedef VS Visitor; |
1329 | 1333 |
|
1330 | 1334 |
///The type of the map that indicates which nodes are reached. |
1331 | 1335 |
typedef typename Traits::ReachedMap ReachedMap; |
1332 | 1336 |
|
1333 | 1337 |
private: |
1334 | 1338 |
|
1335 | 1339 |
typedef typename Digraph::Node Node; |
1336 | 1340 |
typedef typename Digraph::NodeIt NodeIt; |
1337 | 1341 |
typedef typename Digraph::Arc Arc; |
1338 | 1342 |
typedef typename Digraph::OutArcIt OutArcIt; |
1339 | 1343 |
|
1340 | 1344 |
//Pointer to the underlying digraph. |
1341 | 1345 |
const Digraph *_digraph; |
1342 | 1346 |
//Pointer to the visitor object. |
1343 | 1347 |
Visitor *_visitor; |
1344 | 1348 |
//Pointer to the map of reached status of the nodes. |
1345 | 1349 |
ReachedMap *_reached; |
1346 | 1350 |
//Indicates if _reached is locally allocated (true) or not. |
1347 | 1351 |
bool local_reached; |
1348 | 1352 |
|
1349 | 1353 |
std::vector<typename Digraph::Node> _list; |
1350 | 1354 |
int _list_front, _list_back; |
1351 | 1355 |
|
1352 | 1356 |
//Creates the maps if necessary. |
1353 | 1357 |
void create_maps() { |
1354 | 1358 |
if(!_reached) { |
1355 | 1359 |
local_reached = true; |
1356 | 1360 |
_reached = Traits::createReachedMap(*_digraph); |
1357 | 1361 |
} |
1358 | 1362 |
} |
1359 | 1363 |
|
1360 | 1364 |
protected: |
1361 | 1365 |
|
1362 | 1366 |
BfsVisit() {} |
1363 | 1367 |
|
1364 | 1368 |
public: |
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> |
30 | 30 |
#include <lemon/counter.h> |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
/// \ingroup heaps |
35 | 35 |
/// |
36 | 36 |
///\brief Binomial heap data structure. |
37 | 37 |
/// |
38 | 38 |
/// This class implements the \e binomial \e heap data structure. |
39 | 39 |
/// It fully conforms to the \ref concepts::Heap "heap concept". |
40 | 40 |
/// |
41 | 41 |
/// The methods \ref increase() and \ref erase() are not efficient |
42 | 42 |
/// in a binomial heap. In case of many calls of these operations, |
43 | 43 |
/// it is better to use other heap structure, e.g. \ref BinHeap |
44 | 44 |
/// "binary heap". |
45 | 45 |
/// |
46 | 46 |
/// \tparam PR Type of the priorities of the items. |
47 | 47 |
/// \tparam IM A read-writable item map with \c int values, used |
48 | 48 |
/// internally to handle the cross references. |
49 | 49 |
/// \tparam CMP A functor class for comparing the priorities. |
50 | 50 |
/// The default is \c std::less<PR>. |
51 | 51 |
#ifdef DOXYGEN |
52 | 52 |
template <typename PR, typename IM, typename CMP> |
53 | 53 |
#else |
54 | 54 |
template <typename PR, typename IM, typename CMP = std::less<PR> > |
55 | 55 |
#endif |
56 | 56 |
class BinomialHeap { |
57 | 57 |
public: |
58 | 58 |
/// Type of the item-int map. |
59 | 59 |
typedef IM ItemIntMap; |
60 | 60 |
/// Type of the priorities. |
61 | 61 |
typedef PR Prio; |
62 | 62 |
/// Type of the items stored in the heap. |
63 | 63 |
typedef typename ItemIntMap::Key Item; |
64 | 64 |
/// Functor type for comparing the priorities. |
65 | 65 |
typedef CMP Compare; |
66 | 66 |
|
67 | 67 |
/// \brief Type to represent the states of the items. |
68 | 68 |
/// |
69 | 69 |
/// Each item has a state associated to it. It can be "in heap", |
70 | 70 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the |
71 | 71 |
/// heap's point of view, but may be useful to the user. |
72 | 72 |
/// |
73 | 73 |
/// The item-int map must be initialized in such way that it assigns |
74 | 74 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
75 | 75 |
enum State { |
76 | 76 |
IN_HEAP = 0, ///< = 0. |
77 | 77 |
PRE_HEAP = -1, ///< = -1. |
78 | 78 |
POST_HEAP = -2 ///< = -2. |
79 | 79 |
}; |
80 | 80 |
|
81 | 81 |
private: |
82 | 82 |
class Store; |
83 | 83 |
|
84 | 84 |
std::vector<Store> _data; |
85 | 85 |
int _min, _head; |
86 | 86 |
ItemIntMap &_iim; |
87 | 87 |
Compare _comp; |
88 | 88 |
int _num_items; |
89 | 89 |
|
90 | 90 |
public: |
91 | 91 |
/// \brief Constructor. |
92 | 92 |
/// |
93 | 93 |
/// Constructor. |
94 | 94 |
/// \param map A map that assigns \c int values to the items. |
95 | 95 |
/// It is used internally to handle the cross references. |
96 | 96 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
97 | 97 |
explicit BinomialHeap(ItemIntMap &map) |
98 | 98 |
: _min(0), _head(-1), _iim(map), _num_items(0) {} |
99 | 99 |
|
100 | 100 |
/// \brief Constructor. |
101 | 101 |
/// |
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 |
30 | 30 |
// \file |
31 | 31 |
// \brief Graph map based on the array storage. |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
// \ingroup graphbits |
36 | 36 |
// |
37 | 37 |
// \brief Graph map based on the array storage. |
38 | 38 |
// |
39 | 39 |
// The ArrayMap template class is graph map structure that automatically |
40 | 40 |
// updates the map when a key is added to or erased from the graph. |
41 | 41 |
// This map uses the allocators to implement the container functionality. |
42 | 42 |
// |
43 | 43 |
// The template parameters are the Graph, the current Item type and |
44 | 44 |
// the Value type of the map. |
45 | 45 |
template <typename _Graph, typename _Item, typename _Value> |
46 | 46 |
class ArrayMap |
47 | 47 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { |
48 | 48 |
public: |
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. |
98 | 98 |
ArrayMap(const GraphType& graph, const Value& value) { |
99 | 99 |
Parent::attach(graph.notifier(Item())); |
100 | 100 |
allocate_memory(); |
101 | 101 |
Notifier* nf = Parent::notifier(); |
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. |
30 | 30 |
|
31 | 31 |
namespace lemon { |
32 | 32 |
|
33 | 33 |
|
34 | 34 |
//#ifndef LEMON_USE_DEBUG_MAP |
35 | 35 |
|
36 | 36 |
template <typename _Graph, typename _Item, typename _Value> |
37 | 37 |
struct DefaultMapSelector { |
38 | 38 |
typedef ArrayMap<_Graph, _Item, _Value> Map; |
39 | 39 |
}; |
40 | 40 |
|
41 | 41 |
// bool |
42 | 42 |
template <typename _Graph, typename _Item> |
43 | 43 |
struct DefaultMapSelector<_Graph, _Item, bool> { |
44 | 44 |
typedef VectorMap<_Graph, _Item, bool> Map; |
45 | 45 |
}; |
46 | 46 |
|
47 | 47 |
// char |
48 | 48 |
template <typename _Graph, typename _Item> |
49 | 49 |
struct DefaultMapSelector<_Graph, _Item, char> { |
50 | 50 |
typedef VectorMap<_Graph, _Item, char> Map; |
51 | 51 |
}; |
52 | 52 |
|
53 | 53 |
template <typename _Graph, typename _Item> |
54 | 54 |
struct DefaultMapSelector<_Graph, _Item, signed char> { |
55 | 55 |
typedef VectorMap<_Graph, _Item, signed char> Map; |
56 | 56 |
}; |
57 | 57 |
|
58 | 58 |
template <typename _Graph, typename _Item> |
59 | 59 |
struct DefaultMapSelector<_Graph, _Item, unsigned char> { |
60 | 60 |
typedef VectorMap<_Graph, _Item, unsigned char> Map; |
61 | 61 |
}; |
62 | 62 |
|
63 | 63 |
|
64 | 64 |
// int |
65 | 65 |
template <typename _Graph, typename _Item> |
66 | 66 |
struct DefaultMapSelector<_Graph, _Item, signed int> { |
67 | 67 |
typedef VectorMap<_Graph, _Item, signed int> Map; |
68 | 68 |
}; |
69 | 69 |
|
70 | 70 |
template <typename _Graph, typename _Item> |
71 | 71 |
struct DefaultMapSelector<_Graph, _Item, unsigned int> { |
72 | 72 |
typedef VectorMap<_Graph, _Item, unsigned int> Map; |
73 | 73 |
}; |
74 | 74 |
|
75 | 75 |
|
76 | 76 |
// short |
77 | 77 |
template <typename _Graph, typename _Item> |
78 | 78 |
struct DefaultMapSelector<_Graph, _Item, signed short> { |
79 | 79 |
typedef VectorMap<_Graph, _Item, signed short> Map; |
80 | 80 |
}; |
81 | 81 |
|
82 | 82 |
template <typename _Graph, typename _Item> |
83 | 83 |
struct DefaultMapSelector<_Graph, _Item, unsigned short> { |
84 | 84 |
typedef VectorMap<_Graph, _Item, unsigned short> Map; |
85 | 85 |
}; |
86 | 86 |
|
87 | 87 |
|
88 | 88 |
// long |
89 | 89 |
template <typename _Graph, typename _Item> |
90 | 90 |
struct DefaultMapSelector<_Graph, _Item, signed long> { |
91 | 91 |
typedef VectorMap<_Graph, _Item, signed long> Map; |
92 | 92 |
}; |
93 | 93 |
|
94 | 94 |
template <typename _Graph, typename _Item> |
95 | 95 |
struct DefaultMapSelector<_Graph, _Item, unsigned long> { |
96 | 96 |
typedef VectorMap<_Graph, _Item, unsigned long> Map; |
97 | 97 |
}; |
98 | 98 |
|
99 | 99 |
|
100 | 100 |
#if defined LEMON_HAVE_LONG_LONG |
101 | 101 |
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 |
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 | 32 |
// \ingroup digraphbits |
33 | 33 |
// |
34 | 34 |
// \brief Extender for the ArcSets |
35 | 35 |
template <typename Base> |
36 | 36 |
class ArcSetExtender : public Base { |
37 | 37 |
typedef Base Parent; |
38 | 38 |
|
39 | 39 |
public: |
40 | 40 |
|
41 | 41 |
typedef ArcSetExtender Digraph; |
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 | 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 |
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: |
30 | 30 |
struct ItemT { |
31 | 31 |
int prev, next; |
32 | 32 |
int index; |
33 | 33 |
}; |
34 | 34 |
std::vector<ItemT> items; |
35 | 35 |
int first_item, last_item, first_free_item; |
36 | 36 |
|
37 | 37 |
std::vector<int> cross; |
38 | 38 |
|
39 | 39 |
public: |
40 | 40 |
|
41 | 41 |
VarIndex() |
42 | 42 |
: first_item(-1), last_item(-1), first_free_item(-1) { |
43 | 43 |
} |
44 | 44 |
|
45 | 45 |
void clear() { |
46 | 46 |
first_item = -1; |
47 | 47 |
first_free_item = -1; |
48 | 48 |
items.clear(); |
49 | 49 |
cross.clear(); |
50 | 50 |
} |
51 | 51 |
|
52 | 52 |
int addIndex(int idx) { |
53 | 53 |
int n; |
54 | 54 |
if (first_free_item == -1) { |
55 | 55 |
n = items.size(); |
56 | 56 |
items.push_back(ItemT()); |
57 | 57 |
} else { |
58 | 58 |
n = first_free_item; |
59 | 59 |
first_free_item = items[n].next; |
60 | 60 |
if (first_free_item != -1) { |
61 | 61 |
items[first_free_item].prev = -1; |
62 | 62 |
} |
63 | 63 |
} |
64 | 64 |
items[n].index = idx; |
65 | 65 |
if (static_cast<int>(cross.size()) <= idx) { |
66 | 66 |
cross.resize(idx + 1, -1); |
67 | 67 |
} |
68 | 68 |
cross[idx] = n; |
69 | 69 |
|
70 | 70 |
items[n].prev = last_item; |
71 | 71 |
items[n].next = -1; |
72 | 72 |
if (last_item != -1) { |
73 | 73 |
items[last_item].next = n; |
74 | 74 |
} else { |
75 | 75 |
first_item = n; |
76 | 76 |
} |
77 | 77 |
last_item = n; |
78 | 78 |
|
79 | 79 |
return n; |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
int addIndex(int idx, int n) { |
83 | 83 |
while (n >= static_cast<int>(items.size())) { |
84 | 84 |
items.push_back(ItemT()); |
85 | 85 |
items.back().prev = -1; |
86 | 86 |
items.back().next = first_free_item; |
87 | 87 |
if (first_free_item != -1) { |
88 | 88 |
items[first_free_item].prev = items.size() - 1; |
89 | 89 |
} |
90 | 90 |
first_free_item = items.size() - 1; |
91 | 91 |
} |
92 | 92 |
if (items[n].next != -1) { |
93 | 93 |
items[items[n].next].prev = items[n].prev; |
94 | 94 |
} |
95 | 95 |
if (items[n].prev != -1) { |
96 | 96 |
items[items[n].prev].next = items[n].next; |
97 | 97 |
} else { |
98 | 98 |
first_free_item = items[n].next; |
99 | 99 |
} |
100 | 100 |
|
101 | 101 |
items[n].index = idx; |
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 |
30 | 30 |
#endif |
31 | 31 |
#ifdef UNICODE |
32 | 32 |
#undef UNICODE |
33 | 33 |
#endif |
34 | 34 |
#include <windows.h> |
35 | 35 |
#ifdef LOCALE_INVARIANT |
36 | 36 |
#define MY_LOCALE LOCALE_INVARIANT |
37 | 37 |
#else |
38 | 38 |
#define MY_LOCALE LOCALE_NEUTRAL |
39 | 39 |
#endif |
40 | 40 |
#else |
41 | 41 |
#include <unistd.h> |
42 | 42 |
#include <ctime> |
43 | 43 |
#include <sys/times.h> |
44 | 44 |
#include <sys/time.h> |
45 | 45 |
#endif |
46 | 46 |
|
47 | 47 |
#include <cmath> |
48 | 48 |
#include <sstream> |
49 | 49 |
|
50 | 50 |
namespace lemon { |
51 | 51 |
namespace bits { |
52 | 52 |
void getWinProcTimes(double &rtime, |
53 | 53 |
double &utime, double &stime, |
54 | 54 |
double &cutime, double &cstime) |
55 | 55 |
{ |
56 | 56 |
#ifdef WIN32 |
57 | 57 |
static const double ch = 4294967296.0e-7; |
58 | 58 |
static const double cl = 1.0e-7; |
59 | 59 |
|
60 | 60 |
FILETIME system; |
61 | 61 |
GetSystemTimeAsFileTime(&system); |
62 | 62 |
rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime; |
63 | 63 |
|
64 | 64 |
FILETIME create, exit, kernel, user; |
65 | 65 |
if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) { |
66 | 66 |
utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime; |
67 | 67 |
stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime; |
68 | 68 |
cutime = 0; |
69 | 69 |
cstime = 0; |
70 | 70 |
} else { |
71 | 71 |
rtime = 0; |
72 | 72 |
utime = 0; |
73 | 73 |
stime = 0; |
74 | 74 |
cutime = 0; |
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, |
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 |
|
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 | 32 |
namespace _bucket_heap_bits { |
33 | 33 |
|
34 | 34 |
template <bool MIN> |
35 | 35 |
struct DirectionTraits { |
36 | 36 |
static bool less(int left, int right) { |
37 | 37 |
return left < right; |
38 | 38 |
} |
39 | 39 |
static void increase(int& value) { |
40 | 40 |
++value; |
41 | 41 |
} |
42 | 42 |
}; |
43 | 43 |
|
44 | 44 |
template <> |
45 | 45 |
struct DirectionTraits<false> { |
46 | 46 |
static bool less(int left, int right) { |
47 | 47 |
return left > right; |
48 | 48 |
} |
49 | 49 |
static void increase(int& value) { |
50 | 50 |
--value; |
51 | 51 |
} |
52 | 52 |
}; |
53 | 53 |
|
54 | 54 |
} |
55 | 55 |
|
56 | 56 |
/// \ingroup heaps |
57 | 57 |
/// |
58 | 58 |
/// \brief Bucket heap data structure. |
59 | 59 |
/// |
60 | 60 |
/// This class implements the \e bucket \e heap data structure. |
61 | 61 |
/// It practically conforms to the \ref concepts::Heap "heap concept", |
62 | 62 |
/// but it has some limitations. |
63 | 63 |
/// |
64 | 64 |
/// The bucket heap is a very simple structure. It can store only |
65 | 65 |
/// \c int priorities and it maintains a list of items for each priority |
66 | 66 |
/// in the range <tt>[0..C)</tt>. So it should only be used when the |
67 | 67 |
/// priorities are small. It is not intended to use as a Dijkstra heap. |
68 | 68 |
/// |
69 | 69 |
/// \tparam IM A read-writable item map with \c int values, used |
70 | 70 |
/// internally to handle the cross references. |
71 | 71 |
/// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap. |
72 | 72 |
/// The default is \e min-heap. If this parameter is set to \c false, |
73 | 73 |
/// then the comparison is reversed, so the top(), prio() and pop() |
74 | 74 |
/// functions deal with the item having maximum priority instead of the |
75 | 75 |
/// minimum. |
76 | 76 |
/// |
77 | 77 |
/// \sa SimpleBucketHeap |
78 | 78 |
template <typename IM, bool MIN = true> |
79 | 79 |
class BucketHeap { |
80 | 80 |
|
81 | 81 |
public: |
82 | 82 |
|
83 | 83 |
/// Type of the item-int map. |
84 | 84 |
typedef IM ItemIntMap; |
85 | 85 |
/// Type of the priorities. |
86 | 86 |
typedef int Prio; |
87 | 87 |
/// Type of the items stored in the heap. |
88 | 88 |
typedef typename ItemIntMap::Key Item; |
89 | 89 |
/// Type of the item-priority pairs. |
90 | 90 |
typedef std::pair<Item,Prio> Pair; |
91 | 91 |
|
92 | 92 |
private: |
93 | 93 |
|
94 | 94 |
typedef _bucket_heap_bits::DirectionTraits<MIN> Direction; |
95 | 95 |
|
96 | 96 |
public: |
97 | 97 |
|
98 | 98 |
/// \brief Type to represent the states of the items. |
99 | 99 |
/// |
100 | 100 |
/// Each item has a state associated to it. It can be "in heap", |
101 | 101 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the |
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> |
30 | 30 |
#include <lemon/bin_heap.h> |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
/// \brief Default traits class of CapacityScaling algorithm. |
35 | 35 |
/// |
36 | 36 |
/// Default traits class of CapacityScaling algorithm. |
37 | 37 |
/// \tparam GR Digraph type. |
38 | 38 |
/// \tparam V The number type used for flow amounts, capacity bounds |
39 | 39 |
/// and supply values. By default it is \c int. |
40 | 40 |
/// \tparam C The number type used for costs and potentials. |
41 | 41 |
/// By default it is the same as \c V. |
42 | 42 |
template <typename GR, typename V = int, typename C = V> |
43 | 43 |
struct CapacityScalingDefaultTraits |
44 | 44 |
{ |
45 | 45 |
/// The type of the digraph |
46 | 46 |
typedef GR Digraph; |
47 | 47 |
/// The type of the flow amounts, capacity bounds and supply values |
48 | 48 |
typedef V Value; |
49 | 49 |
/// The type of the arc costs |
50 | 50 |
typedef C Cost; |
51 | 51 |
|
52 | 52 |
/// \brief The type of the heap used for internal Dijkstra computations. |
53 | 53 |
/// |
54 | 54 |
/// The type of the heap used for internal Dijkstra computations. |
55 | 55 |
/// It must conform to the \ref lemon::concepts::Heap "Heap" concept, |
56 | 56 |
/// its priority type must be \c Cost and its cross reference type |
57 | 57 |
/// must be \ref RangeMap "RangeMap<int>". |
58 | 58 |
typedef BinHeap<Cost, RangeMap<int> > Heap; |
59 | 59 |
}; |
60 | 60 |
|
61 | 61 |
/// \addtogroup min_cost_flow_algs |
62 | 62 |
/// @{ |
63 | 63 |
|
64 | 64 |
/// \brief Implementation of the Capacity Scaling algorithm for |
65 | 65 |
/// finding a \ref min_cost_flow "minimum cost flow". |
66 | 66 |
/// |
67 | 67 |
/// \ref CapacityScaling implements the capacity scaling version |
68 | 68 |
/// of the successive shortest path algorithm for finding a |
69 | 69 |
/// \ref min_cost_flow "minimum cost flow" \ref amo93networkflows, |
70 | 70 |
/// \ref edmondskarp72theoretical. It is an efficient dual |
71 | 71 |
/// solution method. |
72 | 72 |
/// |
73 | 73 |
/// Most of the parameters of the problem (except for the digraph) |
74 | 74 |
/// can be given using separate functions, and the algorithm can be |
75 | 75 |
/// executed using the \ref run() function. If some parameters are not |
76 | 76 |
/// specified, then default values will be used. |
77 | 77 |
/// |
78 | 78 |
/// \tparam GR The digraph type the algorithm runs on. |
79 | 79 |
/// \tparam V The number type used for flow amounts, capacity bounds |
80 | 80 |
/// and supply values in the algorithm. By default, it is \c int. |
81 | 81 |
/// \tparam C The number type used for costs and potentials in the |
82 | 82 |
/// algorithm. By default, it is the same as \c V. |
83 | 83 |
/// \tparam TR The traits class that defines various types used by the |
84 | 84 |
/// algorithm. By default, it is \ref CapacityScalingDefaultTraits |
85 | 85 |
/// "CapacityScalingDefaultTraits<GR, V, C>". |
86 | 86 |
/// In most cases, this parameter should not be set directly, |
87 | 87 |
/// consider to use the named template parameters instead. |
88 | 88 |
/// |
89 | 89 |
/// \warning Both number types must be signed and all input data must |
90 | 90 |
/// be integer. |
91 | 91 |
/// \warning This algorithm does not support negative costs for such |
92 | 92 |
/// arcs that have infinite upper bound. |
93 | 93 |
#ifdef DOXYGEN |
94 | 94 |
template <typename GR, typename V, typename C, typename TR> |
95 | 95 |
#else |
96 | 96 |
template < typename GR, typename V = int, typename C = V, |
97 | 97 |
typename TR = CapacityScalingDefaultTraits<GR, V, C> > |
98 | 98 |
#endif |
99 | 99 |
class CapacityScaling |
100 | 100 |
{ |
101 | 101 |
public: |
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; |
30 | 30 |
class OsiSolverInterface; |
31 | 31 |
class CbcModel; |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
/// \brief Interface for the CBC MIP solver |
36 | 36 |
/// |
37 | 37 |
/// This class implements an interface for the CBC MIP solver. |
38 | 38 |
///\ingroup lp_group |
39 | 39 |
class CbcMip : public MipSolver { |
40 | 40 |
protected: |
41 | 41 |
|
42 | 42 |
CoinModel *_prob; |
43 | 43 |
OsiSolverInterface *_osi_solver; |
44 | 44 |
CbcModel *_cbc_model; |
45 | 45 |
|
46 | 46 |
public: |
47 | 47 |
|
48 | 48 |
/// \e |
49 | 49 |
CbcMip(); |
50 | 50 |
/// \e |
51 | 51 |
CbcMip(const CbcMip&); |
52 | 52 |
/// \e |
53 | 53 |
~CbcMip(); |
54 | 54 |
/// \e |
55 | 55 |
virtual CbcMip* newSolver() const; |
56 | 56 |
/// \e |
57 | 57 |
virtual CbcMip* cloneSolver() const; |
58 | 58 |
|
59 | 59 |
protected: |
60 | 60 |
|
61 | 61 |
virtual const char* _solverName() const; |
62 | 62 |
|
63 | 63 |
virtual int _addCol(); |
64 | 64 |
virtual int _addRow(); |
65 | 65 |
virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u); |
66 | 66 |
|
67 | 67 |
virtual void _eraseCol(int i); |
68 | 68 |
virtual void _eraseRow(int i); |
69 | 69 |
|
70 | 70 |
virtual void _eraseColId(int i); |
71 | 71 |
virtual void _eraseRowId(int i); |
72 | 72 |
|
73 | 73 |
virtual void _getColName(int col, std::string& name) const; |
74 | 74 |
virtual void _setColName(int col, const std::string& name); |
75 | 75 |
virtual int _colByName(const std::string& name) const; |
76 | 76 |
|
77 | 77 |
virtual void _getRowName(int row, std::string& name) const; |
78 | 78 |
virtual void _setRowName(int row, const std::string& name); |
79 | 79 |
virtual int _rowByName(const std::string& name) const; |
80 | 80 |
|
81 | 81 |
virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e); |
82 | 82 |
virtual void _getRowCoeffs(int i, InsertIterator b) const; |
83 | 83 |
|
84 | 84 |
virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e); |
85 | 85 |
virtual void _getColCoeffs(int i, InsertIterator b) const; |
86 | 86 |
|
87 | 87 |
virtual void _setCoeff(int row, int col, Value value); |
88 | 88 |
virtual Value _getCoeff(int row, int col) const; |
89 | 89 |
|
90 | 90 |
virtual void _setColLowerBound(int i, Value value); |
91 | 91 |
virtual Value _getColLowerBound(int i) const; |
92 | 92 |
virtual void _setColUpperBound(int i, Value value); |
93 | 93 |
virtual Value _getColUpperBound(int i) const; |
94 | 94 |
|
95 | 95 |
virtual void _setRowLowerBound(int i, Value value); |
96 | 96 |
virtual Value _getRowLowerBound(int i) const; |
97 | 97 |
virtual void _setRowUpperBound(int i, Value value); |
98 | 98 |
virtual Value _getRowUpperBound(int i) const; |
99 | 99 |
|
100 | 100 |
virtual void _setObjCoeffs(ExprIterator b, ExprIterator e); |
101 | 101 |
virtual void _getObjCoeffs(InsertIterator b) 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_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 |
/// |
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 | 32 |
/// \brief Default traits class of Circulation class. |
33 | 33 |
/// |
34 | 34 |
/// Default traits class of Circulation class. |
35 | 35 |
/// |
36 | 36 |
/// \tparam GR Type of the digraph the algorithm runs on. |
37 | 37 |
/// \tparam LM The type of the lower bound map. |
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 | 62 |
/// The type of the map that stores the signed supply values of the |
63 | 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); |
88 | 88 |
} |
89 | 89 |
|
90 | 90 |
/// \brief The elevator type used by the algorithm. |
91 | 91 |
/// |
92 | 92 |
/// The elevator type used by the algorithm. |
93 | 93 |
/// |
94 | 94 |
/// \sa Elevator, LinkedElevator |
95 | 95 |
#ifdef DOXYGEN |
96 | 96 |
typedef lemon::Elevator<GR, GR::Node> Elevator; |
97 | 97 |
#else |
98 | 98 |
typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator; |
99 | 99 |
#endif |
100 | 100 |
|
101 | 101 |
/// \brief Instantiates an Elevator. |
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 |
|
30 | 30 |
ClpLp::ClpLp(const ClpLp& other) { |
31 | 31 |
_prob = new ClpSimplex(*other._prob); |
32 | 32 |
rows = other.rows; |
33 | 33 |
cols = other.cols; |
34 | 34 |
_init_temporals(); |
35 | 35 |
messageLevel(MESSAGE_NOTHING); |
36 | 36 |
} |
37 | 37 |
|
38 | 38 |
ClpLp::~ClpLp() { |
39 | 39 |
delete _prob; |
40 | 40 |
_clear_temporals(); |
41 | 41 |
} |
42 | 42 |
|
43 | 43 |
void ClpLp::_init_temporals() { |
44 | 44 |
_primal_ray = 0; |
45 | 45 |
_dual_ray = 0; |
46 | 46 |
} |
47 | 47 |
|
48 | 48 |
void ClpLp::_clear_temporals() { |
49 | 49 |
if (_primal_ray) { |
50 | 50 |
delete[] _primal_ray; |
51 | 51 |
_primal_ray = 0; |
52 | 52 |
} |
53 | 53 |
if (_dual_ray) { |
54 | 54 |
delete[] _dual_ray; |
55 | 55 |
_dual_ray = 0; |
56 | 56 |
} |
57 | 57 |
} |
58 | 58 |
|
59 | 59 |
ClpLp* ClpLp::newSolver() const { |
60 | 60 |
ClpLp* newlp = new ClpLp; |
61 | 61 |
return newlp; |
62 | 62 |
} |
63 | 63 |
|
64 | 64 |
ClpLp* ClpLp::cloneSolver() const { |
65 | 65 |
ClpLp* copylp = new ClpLp(*this); |
66 | 66 |
return copylp; |
67 | 67 |
} |
68 | 68 |
|
69 | 69 |
const char* ClpLp::_solverName() const { return "ClpLp"; } |
70 | 70 |
|
71 | 71 |
int ClpLp::_addCol() { |
72 | 72 |
_prob->addColumn(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX, 0.0); |
73 | 73 |
return _prob->numberColumns() - 1; |
74 | 74 |
} |
75 | 75 |
|
76 | 76 |
int ClpLp::_addRow() { |
77 | 77 |
_prob->addRow(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX); |
78 | 78 |
return _prob->numberRows() - 1; |
79 | 79 |
} |
80 | 80 |
|
81 | 81 |
int ClpLp::_addRow(Value l, ExprIterator b, ExprIterator e, Value u) { |
82 | 82 |
std::vector<int> indexes; |
83 | 83 |
std::vector<Value> values; |
84 | 84 |
|
85 | 85 |
for(ExprIterator it = b; it != e; ++it) { |
86 | 86 |
indexes.push_back(it->first); |
87 | 87 |
values.push_back(it->second); |
88 | 88 |
} |
89 | 89 |
|
90 | 90 |
_prob->addRow(values.size(), &indexes.front(), &values.front(), l, u); |
91 | 91 |
return _prob->numberRows() - 1; |
92 | 92 |
} |
93 | 93 |
|
94 | 94 |
|
95 | 95 |
void ClpLp::_eraseCol(int c) { |
96 | 96 |
_col_names_ref.erase(_prob->getColumnName(c)); |
97 | 97 |
_prob->deleteColumns(1, &c); |
98 | 98 |
} |
99 | 99 |
|
100 | 100 |
void ClpLp::_eraseRow(int r) { |
101 | 101 |
_row_names_ref.erase(_prob->getRowName(r)); |
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 |
|
30 | 30 |
class ClpSimplex; |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
/// \ingroup lp_group |
35 | 35 |
/// |
36 | 36 |
/// \brief Interface for the CLP solver |
37 | 37 |
/// |
38 | 38 |
/// This class implements an interface for the Clp LP solver. The |
39 | 39 |
/// Clp library is an object oriented lp solver library developed at |
40 | 40 |
/// the IBM. The CLP is part of the COIN-OR package and it can be |
41 | 41 |
/// used with Common Public License. |
42 | 42 |
class ClpLp : public LpSolver { |
43 | 43 |
protected: |
44 | 44 |
|
45 | 45 |
ClpSimplex* _prob; |
46 | 46 |
|
47 | 47 |
std::map<std::string, int> _col_names_ref; |
48 | 48 |
std::map<std::string, int> _row_names_ref; |
49 | 49 |
|
50 | 50 |
public: |
51 | 51 |
|
52 | 52 |
/// \e |
53 | 53 |
ClpLp(); |
54 | 54 |
/// \e |
55 | 55 |
ClpLp(const ClpLp&); |
56 | 56 |
/// \e |
57 | 57 |
~ClpLp(); |
58 | 58 |
|
59 | 59 |
/// \e |
60 | 60 |
virtual ClpLp* newSolver() const; |
61 | 61 |
/// \e |
62 | 62 |
virtual ClpLp* cloneSolver() const; |
63 | 63 |
|
64 | 64 |
protected: |
65 | 65 |
|
66 | 66 |
mutable double* _primal_ray; |
67 | 67 |
mutable double* _dual_ray; |
68 | 68 |
|
69 | 69 |
void _init_temporals(); |
70 | 70 |
void _clear_temporals(); |
71 | 71 |
|
72 | 72 |
protected: |
73 | 73 |
|
74 | 74 |
virtual const char* _solverName() const; |
75 | 75 |
|
76 | 76 |
virtual int _addCol(); |
77 | 77 |
virtual int _addRow(); |
78 | 78 |
virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u); |
79 | 79 |
|
80 | 80 |
virtual void _eraseCol(int i); |
81 | 81 |
virtual void _eraseRow(int i); |
82 | 82 |
|
83 | 83 |
virtual void _eraseColId(int i); |
84 | 84 |
virtual void _eraseRowId(int i); |
85 | 85 |
|
86 | 86 |
virtual void _getColName(int col, std::string& name) const; |
87 | 87 |
virtual void _setColName(int col, const std::string& name); |
88 | 88 |
virtual int _colByName(const std::string& name) const; |
89 | 89 |
|
90 | 90 |
virtual void _getRowName(int row, std::string& name) const; |
91 | 91 |
virtual void _setRowName(int row, const std::string& name); |
92 | 92 |
virtual int _rowByName(const std::string& name) const; |
93 | 93 |
|
94 | 94 |
virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e); |
95 | 95 |
virtual void _getRowCoeffs(int i, InsertIterator b) const; |
96 | 96 |
|
97 | 97 |
virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e); |
98 | 98 |
virtual void _getColCoeffs(int i, InsertIterator b) const; |
99 | 99 |
|
100 | 100 |
virtual void _setCoeff(int row, int col, Value value); |
101 | 101 |
virtual Value _getCoeff(int row, int col) 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_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> |
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 directed graphs. |
37 | 37 |
/// |
38 | 38 |
/// This class describes the common interface of all directed |
39 | 39 |
/// graphs (digraphs). |
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 |
/// directed graphs should compile with this class, but it will not |
44 | 44 |
/// run properly, of course. |
45 | 45 |
/// An actual digraph implementation like \ref ListDigraph or |
46 | 46 |
/// \ref SmartDigraph may have additional functionality. |
47 | 47 |
/// |
48 | 48 |
/// \sa Graph |
49 | 49 |
class Digraph { |
50 | 50 |
private: |
51 | 51 |
/// Diraphs are \e not copy constructible. Use DigraphCopy instead. |
52 | 52 |
Digraph(const Digraph &) {} |
53 | 53 |
/// \brief Assignment of a digraph to another one is \e not allowed. |
54 | 54 |
/// Use DigraphCopy instead. |
55 | 55 |
void operator=(const Digraph &) {} |
56 | 56 |
|
57 | 57 |
public: |
58 | 58 |
/// Default constructor. |
59 | 59 |
Digraph() { } |
60 | 60 |
|
61 | 61 |
/// The node type of the digraph |
62 | 62 |
|
63 | 63 |
/// This class identifies a node of the digraph. It also serves |
64 | 64 |
/// as a base class of the node iterators, |
65 | 65 |
/// thus they convert to this type. |
66 | 66 |
class Node { |
67 | 67 |
public: |
68 | 68 |
/// Default constructor |
69 | 69 |
|
70 | 70 |
/// Default constructor. |
71 | 71 |
/// \warning It sets the object to an undefined value. |
72 | 72 |
Node() { } |
73 | 73 |
/// Copy constructor. |
74 | 74 |
|
75 | 75 |
/// Copy constructor. |
76 | 76 |
/// |
77 | 77 |
Node(const Node&) { } |
78 | 78 |
|
79 | 79 |
/// %Invalid constructor \& conversion. |
80 | 80 |
|
81 | 81 |
/// Initializes the object to be invalid. |
82 | 82 |
/// \sa Invalid for more details. |
83 | 83 |
Node(Invalid) { } |
84 | 84 |
/// Equality operator |
85 | 85 |
|
86 | 86 |
/// Equality operator. |
87 | 87 |
/// |
88 | 88 |
/// Two iterators are equal if and only if they point to the |
89 | 89 |
/// same object or both are \c INVALID. |
90 | 90 |
bool operator==(Node) const { return true; } |
91 | 91 |
|
92 | 92 |
/// Inequality operator |
93 | 93 |
|
94 | 94 |
/// Inequality operator. |
95 | 95 |
bool operator!=(Node) const { return true; } |
96 | 96 |
|
97 | 97 |
/// Artificial ordering operator. |
98 | 98 |
|
99 | 99 |
/// Artificial ordering operator. |
100 | 100 |
/// |
101 | 101 |
/// \note This operator only has to define some strict ordering of |
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 | 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 |
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 | 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. |
66 | 66 |
/// \sa Invalid for more details. |
67 | 67 |
GraphItem(Invalid) {} |
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 | 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 { |
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 { |
30 | 30 |
|
31 | 31 |
namespace concepts { |
32 | 32 |
|
33 | 33 |
/// \addtogroup concept |
34 | 34 |
/// @{ |
35 | 35 |
|
36 | 36 |
/// \brief The heap concept. |
37 | 37 |
/// |
38 | 38 |
/// This concept class describes the main interface of heaps. |
39 | 39 |
/// The various \ref heaps "heap structures" are efficient |
40 | 40 |
/// implementations of the abstract data type \e priority \e queue. |
41 | 41 |
/// They store items with specified values called \e priorities |
42 | 42 |
/// in such a way that finding and removing the item with minimum |
43 | 43 |
/// priority are efficient. The basic operations are adding and |
44 | 44 |
/// erasing items, changing the priority of an item, etc. |
45 | 45 |
/// |
46 | 46 |
/// Heaps are crucial in several algorithms, such as Dijkstra and Prim. |
47 | 47 |
/// Any class that conforms to this concept can be used easily in such |
48 | 48 |
/// algorithms. |
49 | 49 |
/// |
50 | 50 |
/// \tparam PR Type of the priorities of the items. |
51 | 51 |
/// \tparam IM A read-writable item map with \c int values, used |
52 | 52 |
/// internally to handle the cross references. |
53 | 53 |
/// \tparam CMP A functor class for comparing the priorities. |
54 | 54 |
/// The default is \c std::less<PR>. |
55 | 55 |
#ifdef DOXYGEN |
56 | 56 |
template <typename PR, typename IM, typename CMP> |
57 | 57 |
#else |
58 | 58 |
template <typename PR, typename IM, typename CMP = std::less<PR> > |
59 | 59 |
#endif |
60 | 60 |
class Heap { |
61 | 61 |
public: |
62 | 62 |
|
63 | 63 |
/// Type of the item-int map. |
64 | 64 |
typedef IM ItemIntMap; |
65 | 65 |
/// Type of the priorities. |
66 | 66 |
typedef PR Prio; |
67 | 67 |
/// Type of the items stored in the heap. |
68 | 68 |
typedef typename ItemIntMap::Key Item; |
69 | 69 |
|
70 | 70 |
/// \brief Type to represent the states of the items. |
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 | 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 |
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> |
30 | 30 |
#include <lemon/concept_check.h> |
31 | 31 |
|
32 | 32 |
#include <stack> |
33 | 33 |
#include <functional> |
34 | 34 |
|
35 | 35 |
/// \ingroup graph_properties |
36 | 36 |
/// \file |
37 | 37 |
/// \brief Connectivity algorithms |
38 | 38 |
/// |
39 | 39 |
/// Connectivity algorithms |
40 | 40 |
|
41 | 41 |
namespace lemon { |
42 | 42 |
|
43 | 43 |
/// \ingroup graph_properties |
44 | 44 |
/// |
45 | 45 |
/// \brief Check whether an undirected graph is connected. |
46 | 46 |
/// |
47 | 47 |
/// This function checks whether the given undirected graph is connected, |
48 | 48 |
/// i.e. there is a path between any two nodes in the graph. |
49 | 49 |
/// |
50 | 50 |
/// \return \c true if the graph is connected. |
51 | 51 |
/// \note By definition, the empty graph is connected. |
52 | 52 |
/// |
53 | 53 |
/// \see countConnectedComponents(), connectedComponents() |
54 | 54 |
/// \see stronglyConnected() |
55 | 55 |
template <typename Graph> |
56 | 56 |
bool connected(const Graph& graph) { |
57 | 57 |
checkConcept<concepts::Graph, Graph>(); |
58 | 58 |
typedef typename Graph::NodeIt NodeIt; |
59 | 59 |
if (NodeIt(graph) == INVALID) return true; |
60 | 60 |
Dfs<Graph> dfs(graph); |
61 | 61 |
dfs.run(NodeIt(graph)); |
62 | 62 |
for (NodeIt it(graph); it != INVALID; ++it) { |
63 | 63 |
if (!dfs.reached(it)) { |
64 | 64 |
return false; |
65 | 65 |
} |
66 | 66 |
} |
67 | 67 |
return true; |
68 | 68 |
} |
69 | 69 |
|
70 | 70 |
/// \ingroup graph_properties |
71 | 71 |
/// |
72 | 72 |
/// \brief Count the number of connected components of an undirected graph |
73 | 73 |
/// |
74 | 74 |
/// This function counts the number of connected components of the given |
75 | 75 |
/// undirected graph. |
76 | 76 |
/// |
77 | 77 |
/// The connected components are the classes of an equivalence relation |
78 | 78 |
/// on the nodes of an undirected graph. Two nodes are in the same class |
79 | 79 |
/// if they are connected with a path. |
80 | 80 |
/// |
81 | 81 |
/// \return The number of connected components. |
82 | 82 |
/// \note By definition, the empty graph consists |
83 | 83 |
/// of zero connected components. |
84 | 84 |
/// |
85 | 85 |
/// \see connected(), connectedComponents() |
86 | 86 |
template <typename Graph> |
87 | 87 |
int countConnectedComponents(const Graph &graph) { |
88 | 88 |
checkConcept<concepts::Graph, Graph>(); |
89 | 89 |
typedef typename Graph::Node Node; |
90 | 90 |
typedef typename Graph::Arc Arc; |
91 | 91 |
|
92 | 92 |
typedef NullMap<Node, Arc> PredMap; |
93 | 93 |
typedef NullMap<Node, int> DistMap; |
94 | 94 |
|
95 | 95 |
int compNum = 0; |
96 | 96 |
typename Bfs<Graph>:: |
97 | 97 |
template SetPredMap<PredMap>:: |
98 | 98 |
template SetDistMap<DistMap>:: |
99 | 99 |
Create bfs(graph); |
100 | 100 |
|
101 | 101 |
PredMap predMap; |
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 |
|
30 | 30 |
// Disable the following warnings when compiling with MSVC: |
31 | 31 |
// C4250: 'class1' : inherits 'class2::member' via dominance |
32 | 32 |
// C4355: 'this' : used in base member initializer list |
33 | 33 |
// C4503: 'function' : decorated name length exceeded, name was truncated |
34 | 34 |
// C4800: 'type' : forcing value to bool 'true' or 'false' (performance warning) |
35 | 35 |
// C4996: 'function': was declared deprecated |
36 | 36 |
#ifdef _MSC_VER |
37 | 37 |
#pragma warning( disable : 4250 4355 4503 4800 4996 ) |
38 | 38 |
#endif |
39 | 39 |
|
40 | 40 |
///\file |
41 | 41 |
///\brief LEMON core utilities. |
42 | 42 |
/// |
43 | 43 |
///This header file contains core utilities for LEMON. |
44 | 44 |
///It is automatically included by all graph types, therefore it usually |
45 | 45 |
///do not have to be included directly. |
46 | 46 |
|
47 | 47 |
namespace lemon { |
48 | 48 |
|
49 | 49 |
/// \brief Dummy type to make it easier to create invalid iterators. |
50 | 50 |
/// |
51 | 51 |
/// Dummy type to make it easier to create invalid iterators. |
52 | 52 |
/// See \ref INVALID for the usage. |
53 | 53 |
struct Invalid { |
54 | 54 |
public: |
55 | 55 |
bool operator==(Invalid) { return true; } |
56 | 56 |
bool operator!=(Invalid) { return false; } |
57 | 57 |
bool operator< (Invalid) { return false; } |
58 | 58 |
}; |
59 | 59 |
|
60 | 60 |
/// \brief Invalid iterators. |
61 | 61 |
/// |
62 | 62 |
/// \ref Invalid is a global type that converts to each iterator |
63 | 63 |
/// in such a way that the value of the target iterator will be invalid. |
64 | 64 |
#ifdef LEMON_ONLY_TEMPLATES |
65 | 65 |
const Invalid INVALID = Invalid(); |
66 | 66 |
#else |
67 | 67 |
extern const Invalid INVALID; |
68 | 68 |
#endif |
69 | 69 |
|
70 | 70 |
/// \addtogroup gutils |
71 | 71 |
/// @{ |
72 | 72 |
|
73 | 73 |
///Create convenience typedefs for the digraph types and iterators |
74 | 74 |
|
75 | 75 |
///This \c \#define creates convenient type definitions for the following |
76 | 76 |
///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
77 | 77 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
78 | 78 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
79 | 79 |
/// |
80 | 80 |
///\note If the graph type is a dependent type, ie. the graph type depend |
81 | 81 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
82 | 82 |
///macro. |
83 | 83 |
#define DIGRAPH_TYPEDEFS(Digraph) \ |
84 | 84 |
typedef Digraph::Node Node; \ |
85 | 85 |
typedef Digraph::NodeIt NodeIt; \ |
86 | 86 |
typedef Digraph::Arc Arc; \ |
87 | 87 |
typedef Digraph::ArcIt ArcIt; \ |
88 | 88 |
typedef Digraph::InArcIt InArcIt; \ |
89 | 89 |
typedef Digraph::OutArcIt OutArcIt; \ |
90 | 90 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \ |
91 | 91 |
typedef Digraph::NodeMap<int> IntNodeMap; \ |
92 | 92 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \ |
93 | 93 |
typedef Digraph::ArcMap<bool> BoolArcMap; \ |
94 | 94 |
typedef Digraph::ArcMap<int> IntArcMap; \ |
95 | 95 |
typedef Digraph::ArcMap<double> DoubleArcMap |
96 | 96 |
|
97 | 97 |
///Create convenience typedefs for the digraph types and iterators |
98 | 98 |
|
99 | 99 |
///\see DIGRAPH_TYPEDEFS |
100 | 100 |
/// |
101 | 101 |
///\note Use this macro, if the graph type is a dependent type, |
... | ... |
@@ -1146,193 +1146,194 @@ |
1146 | 1146 |
/// \note \ref ConEdgeIt provides iterator interface for the same |
1147 | 1147 |
/// functionality. |
1148 | 1148 |
/// |
1149 | 1149 |
///\sa ConEdgeIt |
1150 | 1150 |
template <typename Graph> |
1151 | 1151 |
inline typename Graph::Edge |
1152 | 1152 |
findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
1153 | 1153 |
typename Graph::Edge p = INVALID) { |
1154 | 1154 |
return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p); |
1155 | 1155 |
} |
1156 | 1156 |
|
1157 | 1157 |
/// \brief Iterator for iterating on parallel edges connecting the same nodes. |
1158 | 1158 |
/// |
1159 | 1159 |
/// Iterator for iterating on parallel edges connecting the same nodes. |
1160 | 1160 |
/// It is a higher level interface for the findEdge() function. You can |
1161 | 1161 |
/// use it the following way: |
1162 | 1162 |
///\code |
1163 | 1163 |
/// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) { |
1164 | 1164 |
/// ... |
1165 | 1165 |
/// } |
1166 | 1166 |
///\endcode |
1167 | 1167 |
/// |
1168 | 1168 |
///\sa findEdge() |
1169 | 1169 |
template <typename GR> |
1170 | 1170 |
class ConEdgeIt : public GR::Edge { |
1171 | 1171 |
typedef typename GR::Edge Parent; |
1172 | 1172 |
|
1173 | 1173 |
public: |
1174 | 1174 |
|
1175 | 1175 |
typedef typename GR::Edge Edge; |
1176 | 1176 |
typedef typename GR::Node Node; |
1177 | 1177 |
|
1178 | 1178 |
/// \brief Constructor. |
1179 | 1179 |
/// |
1180 | 1180 |
/// Construct a new ConEdgeIt iterating on the edges that |
1181 | 1181 |
/// connects nodes \c u and \c v. |
1182 | 1182 |
ConEdgeIt(const GR& g, Node u, Node v) : _graph(g), _u(u), _v(v) { |
1183 | 1183 |
Parent::operator=(findEdge(_graph, _u, _v)); |
1184 | 1184 |
} |
1185 | 1185 |
|
1186 | 1186 |
/// \brief Constructor. |
1187 | 1187 |
/// |
1188 | 1188 |
/// Construct a new ConEdgeIt that continues iterating from edge \c e. |
1189 | 1189 |
ConEdgeIt(const GR& g, Edge e) : Parent(e), _graph(g) {} |
1190 | 1190 |
|
1191 | 1191 |
/// \brief Increment operator. |
1192 | 1192 |
/// |
1193 | 1193 |
/// It increments the iterator and gives back the next edge. |
1194 | 1194 |
ConEdgeIt& operator++() { |
1195 | 1195 |
Parent::operator=(findEdge(_graph, _u, _v, *this)); |
1196 | 1196 |
return *this; |
1197 | 1197 |
} |
1198 | 1198 |
private: |
1199 | 1199 |
const GR& _graph; |
1200 | 1200 |
Node _u, _v; |
1201 | 1201 |
}; |
1202 | 1202 |
|
1203 | 1203 |
|
1204 | 1204 |
///Dynamic arc look-up between given endpoints. |
1205 | 1205 |
|
1206 | 1206 |
///Using this class, you can find an arc in a digraph from a given |
1207 | 1207 |
///source to a given target in amortized time <em>O</em>(log<em>d</em>), |
1208 | 1208 |
///where <em>d</em> is the out-degree of the source node. |
1209 | 1209 |
/// |
1210 | 1210 |
///It is possible to find \e all parallel arcs between two nodes with |
1211 | 1211 |
///the \c operator() member. |
1212 | 1212 |
/// |
1213 | 1213 |
///This is a dynamic data structure. Consider to use \ref ArcLookUp or |
1214 | 1214 |
///\ref AllArcLookUp if your digraph is not changed so frequently. |
1215 | 1215 |
/// |
1216 | 1216 |
///This class uses a self-adjusting binary search tree, the Splay tree |
1217 | 1217 |
///of Sleator and Tarjan to guarantee the logarithmic amortized |
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 | 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) { |
1306 | 1307 |
insert(arc); |
1307 | 1308 |
} |
1308 | 1309 |
|
1309 | 1310 |
virtual void add(const std::vector<Arc>& arcs) { |
1310 | 1311 |
for (int i = 0; i < int(arcs.size()); ++i) { |
1311 | 1312 |
insert(arcs[i]); |
1312 | 1313 |
} |
1313 | 1314 |
} |
1314 | 1315 |
|
1315 | 1316 |
virtual void erase(const Arc& arc) { |
1316 | 1317 |
remove(arc); |
1317 | 1318 |
} |
1318 | 1319 |
|
1319 | 1320 |
virtual void erase(const std::vector<Arc>& arcs) { |
1320 | 1321 |
for (int i = 0; i < int(arcs.size()); ++i) { |
1321 | 1322 |
remove(arcs[i]); |
1322 | 1323 |
} |
1323 | 1324 |
} |
1324 | 1325 |
|
1325 | 1326 |
virtual void build() { |
1326 | 1327 |
refresh(); |
1327 | 1328 |
} |
1328 | 1329 |
|
1329 | 1330 |
virtual void clear() { |
1330 | 1331 |
for(NodeIt n(_g);n!=INVALID;++n) { |
1331 | 1332 |
_head[n] = INVALID; |
1332 | 1333 |
} |
1333 | 1334 |
} |
1334 | 1335 |
|
1335 | 1336 |
void insert(Arc arc) { |
1336 | 1337 |
Node s = _g.source(arc); |
1337 | 1338 |
Node t = _g.target(arc); |
1338 | 1339 |
_left[arc] = INVALID; |
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 |
|
30 | 30 |
#include <lemon/core.h> |
31 | 31 |
#include <lemon/maps.h> |
32 | 32 |
#include <lemon/math.h> |
33 | 33 |
#include <lemon/static_graph.h> |
34 | 34 |
#include <lemon/circulation.h> |
35 | 35 |
#include <lemon/bellman_ford.h> |
36 | 36 |
|
37 | 37 |
namespace lemon { |
38 | 38 |
|
39 | 39 |
/// \brief Default traits class of CostScaling algorithm. |
40 | 40 |
/// |
41 | 41 |
/// Default traits class of CostScaling algorithm. |
42 | 42 |
/// \tparam GR Digraph type. |
43 | 43 |
/// \tparam V The number type used for flow amounts, capacity bounds |
44 | 44 |
/// and supply values. By default it is \c int. |
45 | 45 |
/// \tparam C The number type used for costs and potentials. |
46 | 46 |
/// By default it is the same as \c V. |
47 | 47 |
#ifdef DOXYGEN |
48 | 48 |
template <typename GR, typename V = int, typename C = V> |
49 | 49 |
#else |
50 | 50 |
template < typename GR, typename V = int, typename C = V, |
51 | 51 |
bool integer = std::numeric_limits<C>::is_integer > |
52 | 52 |
#endif |
53 | 53 |
struct CostScalingDefaultTraits |
54 | 54 |
{ |
55 | 55 |
/// The type of the digraph |
56 | 56 |
typedef GR Digraph; |
57 | 57 |
/// The type of the flow amounts, capacity bounds and supply values |
58 | 58 |
typedef V Value; |
59 | 59 |
/// The type of the arc costs |
60 | 60 |
typedef C Cost; |
61 | 61 |
|
62 | 62 |
/// \brief The large cost type used for internal computations |
63 | 63 |
/// |
64 | 64 |
/// The large cost type used for internal computations. |
65 | 65 |
/// It is \c long \c long if the \c Cost type is integer, |
66 | 66 |
/// otherwise it is \c double. |
67 | 67 |
/// \c Cost must be convertible to \c LargeCost. |
68 | 68 |
typedef double LargeCost; |
69 | 69 |
}; |
70 | 70 |
|
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 | 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 |
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 |
|
30 | 30 |
///\file |
31 | 31 |
///\brief Implementation of the LEMON-CPLEX lp solver interface. |
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
CplexEnv::LicenseError::LicenseError(int status) { |
35 | 35 |
if (!CPXgeterrorstring(0, status, _message)) { |
36 | 36 |
std::strcpy(_message, "Cplex unknown error"); |
37 | 37 |
} |
38 | 38 |
} |
39 | 39 |
|
40 | 40 |
CplexEnv::CplexEnv() { |
41 | 41 |
int status; |
42 | 42 |
_cnt = new int; |
43 | 43 |
_env = CPXopenCPLEX(&status); |
44 | 44 |
if (_env == 0) { |
45 | 45 |
delete _cnt; |
46 | 46 |
_cnt = 0; |
47 | 47 |
throw LicenseError(status); |
48 | 48 |
} |
49 | 49 |
} |
50 | 50 |
|
51 | 51 |
CplexEnv::CplexEnv(const CplexEnv& other) { |
52 | 52 |
_env = other._env; |
53 | 53 |
_cnt = other._cnt; |
54 | 54 |
++(*_cnt); |
55 | 55 |
} |
56 | 56 |
|
57 | 57 |
CplexEnv& CplexEnv::operator=(const CplexEnv& other) { |
58 | 58 |
_env = other._env; |
59 | 59 |
_cnt = other._cnt; |
60 | 60 |
++(*_cnt); |
61 | 61 |
return *this; |
62 | 62 |
} |
63 | 63 |
|
64 | 64 |
CplexEnv::~CplexEnv() { |
65 | 65 |
--(*_cnt); |
66 | 66 |
if (*_cnt == 0) { |
67 | 67 |
delete _cnt; |
68 | 68 |
CPXcloseCPLEX(&_env); |
69 | 69 |
} |
70 | 70 |
} |
71 | 71 |
|
72 | 72 |
CplexBase::CplexBase() : LpBase() { |
73 | 73 |
int status; |
74 | 74 |
_prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem"); |
75 | 75 |
messageLevel(MESSAGE_NOTHING); |
76 | 76 |
} |
77 | 77 |
|
78 | 78 |
CplexBase::CplexBase(const CplexEnv& env) |
79 | 79 |
: LpBase(), _env(env) { |
80 | 80 |
int status; |
81 | 81 |
_prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem"); |
82 | 82 |
messageLevel(MESSAGE_NOTHING); |
83 | 83 |
} |
84 | 84 |
|
85 | 85 |
CplexBase::CplexBase(const CplexBase& cplex) |
86 | 86 |
: LpBase() { |
87 | 87 |
int status; |
88 | 88 |
_prob = CPXcloneprob(cplexEnv(), cplex._prob, &status); |
89 | 89 |
rows = cplex.rows; |
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); |
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> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/path.h> |
32 | 32 |
#include <lemon/math.h> |
33 | 33 |
#include <lemon/static_graph.h> |
34 | 34 |
#include <lemon/adaptors.h> |
35 | 35 |
#include <lemon/circulation.h> |
36 | 36 |
#include <lemon/bellman_ford.h> |
37 | 37 |
#include <lemon/howard_mmc.h> |
38 | 38 |
|
39 | 39 |
namespace lemon { |
40 | 40 |
|
41 | 41 |
/// \addtogroup min_cost_flow_algs |
42 | 42 |
/// @{ |
43 | 43 |
|
44 | 44 |
/// \brief Implementation of cycle-canceling algorithms for |
45 | 45 |
/// finding a \ref min_cost_flow "minimum cost flow". |
46 | 46 |
/// |
47 | 47 |
/// \ref CycleCanceling implements three different cycle-canceling |
48 | 48 |
/// algorithms for finding a \ref min_cost_flow "minimum cost flow" |
49 | 49 |
/// \ref amo93networkflows, \ref klein67primal, |
50 | 50 |
/// \ref goldberg89cyclecanceling. |
51 | 51 |
/// The most efficent one (both theoretically and practically) |
52 | 52 |
/// is the \ref CANCEL_AND_TIGHTEN "Cancel and Tighten" algorithm, |
53 | 53 |
/// thus it is the default method. |
54 | 54 |
/// It is strongly polynomial, but in practice, it is typically much |
55 | 55 |
/// slower than the scaling algorithms and NetworkSimplex. |
56 | 56 |
/// |
57 | 57 |
/// Most of the parameters of the problem (except for the digraph) |
58 | 58 |
/// can be given using separate functions, and the algorithm can be |
59 | 59 |
/// executed using the \ref run() function. If some parameters are not |
60 | 60 |
/// specified, then default values will be used. |
61 | 61 |
/// |
62 | 62 |
/// \tparam GR The digraph type the algorithm runs on. |
63 | 63 |
/// \tparam V The number type used for flow amounts, capacity bounds |
64 | 64 |
/// and supply values in the algorithm. By default, it is \c int. |
65 | 65 |
/// \tparam C The number type used for costs and potentials in the |
66 | 66 |
/// algorithm. By default, it is the same as \c V. |
67 | 67 |
/// |
68 | 68 |
/// \warning Both number types must be signed and all input data must |
69 | 69 |
/// be integer. |
70 | 70 |
/// \warning This algorithm does not support negative costs for such |
71 | 71 |
/// arcs that have infinite upper bound. |
72 | 72 |
/// |
73 | 73 |
/// \note For more information about the three available methods, |
74 | 74 |
/// see \ref Method. |
75 | 75 |
#ifdef DOXYGEN |
76 | 76 |
template <typename GR, typename V, typename C> |
77 | 77 |
#else |
78 | 78 |
template <typename GR, typename V = int, typename C = V> |
79 | 79 |
#endif |
80 | 80 |
class CycleCanceling |
81 | 81 |
{ |
82 | 82 |
public: |
83 | 83 |
|
84 | 84 |
/// The type of the digraph |
85 | 85 |
typedef GR Digraph; |
86 | 86 |
/// The type of the flow amounts, capacity bounds and supply values |
87 | 87 |
typedef V Value; |
88 | 88 |
/// The type of the arc costs |
89 | 89 |
typedef C Cost; |
90 | 90 |
|
91 | 91 |
public: |
92 | 92 |
|
93 | 93 |
/// \brief Problem type constants for the \c run() function. |
94 | 94 |
/// |
95 | 95 |
/// Enum type containing the problem type constants that can be |
96 | 96 |
/// returned by the \ref run() function of the algorithm. |
97 | 97 |
enum ProblemType { |
98 | 98 |
/// The problem has no feasible solution (flow). |
99 | 99 |
INFEASIBLE, |
100 | 100 |
/// The problem has optimal solution (i.e. it is feasible and |
101 | 101 |
/// bounded), and the algorithm has found optimal flow and node |
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> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/path.h> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
///Default traits class of Dfs class. |
36 | 36 |
|
37 | 37 |
///Default traits class of Dfs class. |
38 | 38 |
///\tparam GR Digraph type. |
39 | 39 |
template<class GR> |
40 | 40 |
struct DfsDefaultTraits |
41 | 41 |
{ |
42 | 42 |
///The type of the digraph the algorithm runs on. |
43 | 43 |
typedef GR Digraph; |
44 | 44 |
|
45 | 45 |
///\brief The type of the map that stores the predecessor |
46 | 46 |
///arcs of the %DFS paths. |
47 | 47 |
/// |
48 | 48 |
///The type of the map that stores the predecessor |
49 | 49 |
///arcs of the %DFS paths. |
50 | 50 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
52 | 52 |
///Instantiates a \c PredMap. |
53 | 53 |
|
54 | 54 |
///This function instantiates a \ref PredMap. |
55 | 55 |
///\param g is the digraph, to which we would like to define the |
56 | 56 |
///\ref PredMap. |
57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
58 | 58 |
{ |
59 | 59 |
return new PredMap(g); |
60 | 60 |
} |
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); |
110 | 111 |
} |
111 | 112 |
}; |
112 | 113 |
|
113 | 114 |
///%DFS algorithm class. |
114 | 115 |
|
115 | 116 |
///\ingroup search |
116 | 117 |
///This class provides an efficient implementation of the %DFS algorithm. |
117 | 118 |
/// |
118 | 119 |
///There is also a \ref dfs() "function-type interface" for the DFS |
119 | 120 |
///algorithm, which is convenient in the simplier cases and it can be |
120 | 121 |
///used easier. |
121 | 122 |
/// |
122 | 123 |
///\tparam GR The type of the digraph the algorithm runs on. |
123 | 124 |
///The default type is \ref ListDigraph. |
124 | 125 |
///\tparam TR The traits class that defines various types used by the |
125 | 126 |
///algorithm. By default, it is \ref DfsDefaultTraits |
126 | 127 |
///"DfsDefaultTraits<GR>". |
127 | 128 |
///In most cases, this parameter should not be set directly, |
128 | 129 |
///consider to use the named template parameters instead. |
129 | 130 |
#ifdef DOXYGEN |
130 | 131 |
template <typename GR, |
131 | 132 |
typename TR> |
132 | 133 |
#else |
133 | 134 |
template <typename GR=ListDigraph, |
134 | 135 |
typename TR=DfsDefaultTraits<GR> > |
135 | 136 |
#endif |
136 | 137 |
class Dfs { |
137 | 138 |
public: |
138 | 139 |
|
139 | 140 |
///The type of the digraph the algorithm runs on. |
140 | 141 |
typedef typename TR::Digraph Digraph; |
141 | 142 |
|
142 | 143 |
///\brief The type of the map that stores the predecessor arcs of the |
143 | 144 |
///DFS paths. |
144 | 145 |
typedef typename TR::PredMap PredMap; |
145 | 146 |
///The type of the map that stores the distances of the nodes. |
146 | 147 |
typedef typename TR::DistMap DistMap; |
147 | 148 |
///The type of the map that indicates which nodes are reached. |
148 | 149 |
typedef typename TR::ReachedMap ReachedMap; |
149 | 150 |
///The type of the map that indicates which nodes are processed. |
150 | 151 |
typedef typename TR::ProcessedMap ProcessedMap; |
151 | 152 |
///The type of the paths. |
152 | 153 |
typedef PredMapPath<Digraph, PredMap> Path; |
153 | 154 |
|
154 | 155 |
///The \ref DfsDefaultTraits "traits class" of the algorithm. |
155 | 156 |
typedef TR Traits; |
156 | 157 |
|
157 | 158 |
private: |
158 | 159 |
|
159 | 160 |
typedef typename Digraph::Node Node; |
160 | 161 |
typedef typename Digraph::NodeIt NodeIt; |
161 | 162 |
typedef typename Digraph::Arc Arc; |
162 | 163 |
typedef typename Digraph::OutArcIt OutArcIt; |
163 | 164 |
|
164 | 165 |
//Pointer to the underlying digraph. |
165 | 166 |
const Digraph *G; |
166 | 167 |
//Pointer to the map of predecessor arcs. |
167 | 168 |
PredMap *_pred; |
168 | 169 |
//Indicates if _pred is locally allocated (true) or not. |
169 | 170 |
bool local_pred; |
170 | 171 |
//Pointer to the map of distances. |
171 | 172 |
DistMap *_dist; |
172 | 173 |
//Indicates if _dist is locally allocated (true) or not. |
173 | 174 |
bool local_dist; |
174 | 175 |
//Pointer to the map of reached status of the nodes. |
175 | 176 |
ReachedMap *_reached; |
176 | 177 |
//Indicates if _reached is locally allocated (true) or not. |
177 | 178 |
bool local_reached; |
178 | 179 |
//Pointer to the map of processed status of the nodes. |
179 | 180 |
ProcessedMap *_processed; |
180 | 181 |
//Indicates if _processed is locally allocated (true) or not. |
181 | 182 |
bool local_processed; |
182 | 183 |
|
183 | 184 |
std::vector<typename Digraph::OutArcIt> _stack; |
184 | 185 |
int _stack_head; |
185 | 186 |
|
186 | 187 |
//Creates the maps if necessary. |
187 | 188 |
void create_maps() |
188 | 189 |
{ |
189 | 190 |
if(!_pred) { |
190 | 191 |
local_pred = true; |
191 | 192 |
_pred = Traits::createPredMap(*G); |
192 | 193 |
} |
193 | 194 |
if(!_dist) { |
194 | 195 |
local_dist = true; |
195 | 196 |
_dist = Traits::createDistMap(*G); |
196 | 197 |
} |
197 | 198 |
if(!_reached) { |
198 | 199 |
local_reached = true; |
199 | 200 |
_reached = Traits::createReachedMap(*G); |
200 | 201 |
} |
201 | 202 |
if(!_processed) { |
202 | 203 |
local_processed = true; |
203 | 204 |
_processed = Traits::createProcessedMap(*G); |
204 | 205 |
} |
205 | 206 |
} |
206 | 207 |
|
207 | 208 |
protected: |
208 | 209 |
|
209 | 210 |
Dfs() {} |
210 | 211 |
|
211 | 212 |
public: |
212 | 213 |
|
213 | 214 |
typedef Dfs Create; |
214 | 215 |
|
215 | 216 |
///\name Named Template Parameters |
216 | 217 |
|
217 | 218 |
///@{ |
218 | 219 |
|
219 | 220 |
template <class T> |
220 | 221 |
struct SetPredMapTraits : public Traits { |
221 | 222 |
typedef T PredMap; |
222 | 223 |
static PredMap *createPredMap(const Digraph &) |
223 | 224 |
{ |
224 | 225 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
225 | 226 |
return 0; // ignore warnings |
226 | 227 |
} |
227 | 228 |
}; |
228 | 229 |
///\brief \ref named-templ-param "Named parameter" for setting |
229 | 230 |
///\c PredMap type. |
230 | 231 |
/// |
231 | 232 |
///\ref named-templ-param "Named parameter" for setting |
232 | 233 |
///\c PredMap type. |
233 | 234 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
234 | 235 |
template <class T> |
235 | 236 |
struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > { |
236 | 237 |
typedef Dfs<Digraph, SetPredMapTraits<T> > Create; |
237 | 238 |
}; |
238 | 239 |
|
239 | 240 |
template <class T> |
240 | 241 |
struct SetDistMapTraits : public Traits { |
241 | 242 |
typedef T DistMap; |
242 | 243 |
static DistMap *createDistMap(const Digraph &) |
243 | 244 |
{ |
244 | 245 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
245 | 246 |
return 0; // ignore warnings |
246 | 247 |
} |
247 | 248 |
}; |
248 | 249 |
///\brief \ref named-templ-param "Named parameter" for setting |
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 |
}; |
298 | 300 |
|
299 | 301 |
struct SetStandardProcessedMapTraits : public Traits { |
300 | 302 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
301 | 303 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
302 | 304 |
{ |
303 | 305 |
return new ProcessedMap(g); |
304 | 306 |
} |
305 | 307 |
}; |
306 | 308 |
///\brief \ref named-templ-param "Named parameter" for setting |
307 | 309 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
308 | 310 |
/// |
309 | 311 |
///\ref named-templ-param "Named parameter" for setting |
310 | 312 |
///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
311 | 313 |
///If you don't set it explicitly, it will be automatically allocated. |
312 | 314 |
struct SetStandardProcessedMap : |
313 | 315 |
public Dfs< Digraph, SetStandardProcessedMapTraits > { |
314 | 316 |
typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create; |
315 | 317 |
}; |
316 | 318 |
|
317 | 319 |
///@} |
318 | 320 |
|
319 | 321 |
public: |
320 | 322 |
|
321 | 323 |
///Constructor. |
322 | 324 |
|
323 | 325 |
///Constructor. |
324 | 326 |
///\param g The digraph the algorithm runs on. |
325 | 327 |
Dfs(const Digraph &g) : |
326 | 328 |
G(&g), |
327 | 329 |
_pred(NULL), local_pred(false), |
328 | 330 |
_dist(NULL), local_dist(false), |
329 | 331 |
_reached(NULL), local_reached(false), |
330 | 332 |
_processed(NULL), local_processed(false) |
331 | 333 |
{ } |
332 | 334 |
|
333 | 335 |
///Destructor. |
334 | 336 |
~Dfs() |
335 | 337 |
{ |
336 | 338 |
if(local_pred) delete _pred; |
337 | 339 |
if(local_dist) delete _dist; |
338 | 340 |
if(local_reached) delete _reached; |
339 | 341 |
if(local_processed) delete _processed; |
340 | 342 |
} |
341 | 343 |
|
342 | 344 |
///Sets the map that stores the predecessor arcs. |
343 | 345 |
|
344 | 346 |
///Sets the map that stores the predecessor arcs. |
345 | 347 |
///If you don't use this function before calling \ref run(Node) "run()" |
346 | 348 |
///or \ref init(), an instance will be allocated automatically. |
347 | 349 |
///The destructor deallocates this automatically allocated map, |
348 | 350 |
///of course. |
349 | 351 |
///\return <tt> (*this) </tt> |
350 | 352 |
Dfs &predMap(PredMap &m) |
351 | 353 |
{ |
352 | 354 |
if(local_pred) { |
353 | 355 |
delete _pred; |
354 | 356 |
local_pred=false; |
355 | 357 |
} |
356 | 358 |
_pred = &m; |
357 | 359 |
return *this; |
358 | 360 |
} |
359 | 361 |
|
360 | 362 |
///Sets the map that indicates which nodes are reached. |
361 | 363 |
|
362 | 364 |
///Sets the map that indicates which nodes are reached. |
363 | 365 |
///If you don't use this function before calling \ref run(Node) "run()" |
364 | 366 |
///or \ref init(), an instance will be allocated automatically. |
365 | 367 |
///The destructor deallocates this automatically allocated map, |
366 | 368 |
///of course. |
367 | 369 |
///\return <tt> (*this) </tt> |
368 | 370 |
Dfs &reachedMap(ReachedMap &m) |
369 | 371 |
{ |
... | ... |
@@ -709,193 +711,194 @@ |
709 | 711 |
///Returns the 'previous node' of the %DFS tree for the given node. |
710 | 712 |
|
711 | 713 |
///This function returns the 'previous node' of the %DFS |
712 | 714 |
///tree for the node \c v, i.e. it returns the last but one node |
713 | 715 |
///of a %DFS path from a root to \c v. It is \c INVALID |
714 | 716 |
///if \c v is not reached from the root(s) or if \c v is a root. |
715 | 717 |
/// |
716 | 718 |
///The %DFS tree used here is equal to the %DFS tree used in |
717 | 719 |
///\ref predArc() and \ref predMap(). |
718 | 720 |
/// |
719 | 721 |
///\pre Either \ref run(Node) "run()" or \ref init() |
720 | 722 |
///must be called before using this function. |
721 | 723 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
722 | 724 |
G->source((*_pred)[v]); } |
723 | 725 |
|
724 | 726 |
///\brief Returns a const reference to the node map that stores the |
725 | 727 |
///distances of the nodes. |
726 | 728 |
/// |
727 | 729 |
///Returns a const reference to the node map that stores the |
728 | 730 |
///distances of the nodes calculated by the algorithm. |
729 | 731 |
/// |
730 | 732 |
///\pre Either \ref run(Node) "run()" or \ref init() |
731 | 733 |
///must be called before using this function. |
732 | 734 |
const DistMap &distMap() const { return *_dist;} |
733 | 735 |
|
734 | 736 |
///\brief Returns a const reference to the node map that stores the |
735 | 737 |
///predecessor arcs. |
736 | 738 |
/// |
737 | 739 |
///Returns a const reference to the node map that stores the predecessor |
738 | 740 |
///arcs, which form the DFS tree (forest). |
739 | 741 |
/// |
740 | 742 |
///\pre Either \ref run(Node) "run()" or \ref init() |
741 | 743 |
///must be called before using this function. |
742 | 744 |
const PredMap &predMap() const { return *_pred;} |
743 | 745 |
|
744 | 746 |
///Checks if the given node. node is reached from the root(s). |
745 | 747 |
|
746 | 748 |
///Returns \c true if \c v is reached from the root(s). |
747 | 749 |
/// |
748 | 750 |
///\pre Either \ref run(Node) "run()" or \ref init() |
749 | 751 |
///must be called before using this function. |
750 | 752 |
bool reached(Node v) const { return (*_reached)[v]; } |
751 | 753 |
|
752 | 754 |
///@} |
753 | 755 |
}; |
754 | 756 |
|
755 | 757 |
///Default traits class of dfs() function. |
756 | 758 |
|
757 | 759 |
///Default traits class of dfs() function. |
758 | 760 |
///\tparam GR Digraph type. |
759 | 761 |
template<class GR> |
760 | 762 |
struct DfsWizardDefaultTraits |
761 | 763 |
{ |
762 | 764 |
///The type of the digraph the algorithm runs on. |
763 | 765 |
typedef GR Digraph; |
764 | 766 |
|
765 | 767 |
///\brief The type of the map that stores the predecessor |
766 | 768 |
///arcs of the %DFS paths. |
767 | 769 |
/// |
768 | 770 |
///The type of the map that stores the predecessor |
769 | 771 |
///arcs of the %DFS paths. |
770 | 772 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
771 | 773 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
772 | 774 |
///Instantiates a PredMap. |
773 | 775 |
|
774 | 776 |
///This function instantiates a PredMap. |
775 | 777 |
///\param g is the digraph, to which we would like to define the |
776 | 778 |
///PredMap. |
777 | 779 |
static PredMap *createPredMap(const Digraph &g) |
778 | 780 |
{ |
779 | 781 |
return new PredMap(g); |
780 | 782 |
} |
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); |
830 | 833 |
} |
831 | 834 |
|
832 | 835 |
///The type of the DFS paths. |
833 | 836 |
|
834 | 837 |
///The type of the DFS paths. |
835 | 838 |
///It must conform to the \ref concepts::Path "Path" concept. |
836 | 839 |
typedef lemon::Path<Digraph> Path; |
837 | 840 |
}; |
838 | 841 |
|
839 | 842 |
/// Default traits class used by DfsWizard |
840 | 843 |
|
841 | 844 |
/// Default traits class used by DfsWizard. |
842 | 845 |
/// \tparam GR The type of the digraph. |
843 | 846 |
template<class GR> |
844 | 847 |
class DfsWizardBase : public DfsWizardDefaultTraits<GR> |
845 | 848 |
{ |
846 | 849 |
|
847 | 850 |
typedef DfsWizardDefaultTraits<GR> Base; |
848 | 851 |
protected: |
849 | 852 |
//The type of the nodes in the digraph. |
850 | 853 |
typedef typename Base::Digraph::Node Node; |
851 | 854 |
|
852 | 855 |
//Pointer to the digraph the algorithm runs on. |
853 | 856 |
void *_g; |
854 | 857 |
//Pointer to the map of reached nodes. |
855 | 858 |
void *_reached; |
856 | 859 |
//Pointer to the map of processed nodes. |
857 | 860 |
void *_processed; |
858 | 861 |
//Pointer to the map of predecessors arcs. |
859 | 862 |
void *_pred; |
860 | 863 |
//Pointer to the map of distances. |
861 | 864 |
void *_dist; |
862 | 865 |
//Pointer to the DFS path to the target node. |
863 | 866 |
void *_path; |
864 | 867 |
//Pointer to the distance of the target node. |
865 | 868 |
int *_di; |
866 | 869 |
|
867 | 870 |
public: |
868 | 871 |
/// Constructor. |
869 | 872 |
|
870 | 873 |
/// This constructor does not require parameters, it initiates |
871 | 874 |
/// all of the attributes to \c 0. |
872 | 875 |
DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
873 | 876 |
_dist(0), _path(0), _di(0) {} |
874 | 877 |
|
875 | 878 |
/// Constructor. |
876 | 879 |
|
877 | 880 |
/// This constructor requires one parameter, |
878 | 881 |
/// others are initiated to \c 0. |
879 | 882 |
/// \param g The digraph the algorithm runs on. |
880 | 883 |
DfsWizardBase(const GR &g) : |
881 | 884 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
882 | 885 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
883 | 886 |
|
884 | 887 |
}; |
885 | 888 |
|
886 | 889 |
/// Auxiliary class for the function-type interface of DFS algorithm. |
887 | 890 |
|
888 | 891 |
/// This auxiliary class is created to implement the |
889 | 892 |
/// \ref dfs() "function-type interface" of \ref Dfs algorithm. |
890 | 893 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
891 | 894 |
/// functions and features of the plain \ref Dfs. |
892 | 895 |
/// |
893 | 896 |
/// This class should only be used through the \ref dfs() function, |
894 | 897 |
/// which makes it easier to use the algorithm. |
895 | 898 |
/// |
896 | 899 |
/// \tparam TR The traits class that defines various types used by the |
897 | 900 |
/// algorithm. |
898 | 901 |
template<class TR> |
899 | 902 |
class DfsWizard : public TR |
900 | 903 |
{ |
901 | 904 |
typedef TR Base; |
... | ... |
@@ -1114,193 +1117,194 @@ |
1114 | 1117 |
///\sa DfsWizard |
1115 | 1118 |
///\sa Dfs |
1116 | 1119 |
template<class GR> |
1117 | 1120 |
DfsWizard<DfsWizardBase<GR> > |
1118 | 1121 |
dfs(const GR &digraph) |
1119 | 1122 |
{ |
1120 | 1123 |
return DfsWizard<DfsWizardBase<GR> >(digraph); |
1121 | 1124 |
} |
1122 | 1125 |
|
1123 | 1126 |
#ifdef DOXYGEN |
1124 | 1127 |
/// \brief Visitor class for DFS. |
1125 | 1128 |
/// |
1126 | 1129 |
/// This class defines the interface of the DfsVisit events, and |
1127 | 1130 |
/// it could be the base of a real visitor class. |
1128 | 1131 |
template <typename GR> |
1129 | 1132 |
struct DfsVisitor { |
1130 | 1133 |
typedef GR Digraph; |
1131 | 1134 |
typedef typename Digraph::Arc Arc; |
1132 | 1135 |
typedef typename Digraph::Node Node; |
1133 | 1136 |
/// \brief Called for the source node of the DFS. |
1134 | 1137 |
/// |
1135 | 1138 |
/// This function is called for the source node of the DFS. |
1136 | 1139 |
void start(const Node& node) {} |
1137 | 1140 |
/// \brief Called when the source node is leaved. |
1138 | 1141 |
/// |
1139 | 1142 |
/// This function is called when the source node is leaved. |
1140 | 1143 |
void stop(const Node& node) {} |
1141 | 1144 |
/// \brief Called when a node is reached first time. |
1142 | 1145 |
/// |
1143 | 1146 |
/// This function is called when a node is reached first time. |
1144 | 1147 |
void reach(const Node& node) {} |
1145 | 1148 |
/// \brief Called when an arc reaches a new node. |
1146 | 1149 |
/// |
1147 | 1150 |
/// This function is called when the DFS finds an arc whose target node |
1148 | 1151 |
/// is not reached yet. |
1149 | 1152 |
void discover(const Arc& arc) {} |
1150 | 1153 |
/// \brief Called when an arc is examined but its target node is |
1151 | 1154 |
/// already discovered. |
1152 | 1155 |
/// |
1153 | 1156 |
/// This function is called when an arc is examined but its target node is |
1154 | 1157 |
/// already discovered. |
1155 | 1158 |
void examine(const Arc& arc) {} |
1156 | 1159 |
/// \brief Called when the DFS steps back from a node. |
1157 | 1160 |
/// |
1158 | 1161 |
/// This function is called when the DFS steps back from a node. |
1159 | 1162 |
void leave(const Node& node) {} |
1160 | 1163 |
/// \brief Called when the DFS steps back on an arc. |
1161 | 1164 |
/// |
1162 | 1165 |
/// This function is called when the DFS steps back on an arc. |
1163 | 1166 |
void backtrack(const Arc& arc) {} |
1164 | 1167 |
}; |
1165 | 1168 |
#else |
1166 | 1169 |
template <typename GR> |
1167 | 1170 |
struct DfsVisitor { |
1168 | 1171 |
typedef GR Digraph; |
1169 | 1172 |
typedef typename Digraph::Arc Arc; |
1170 | 1173 |
typedef typename Digraph::Node Node; |
1171 | 1174 |
void start(const Node&) {} |
1172 | 1175 |
void stop(const Node&) {} |
1173 | 1176 |
void reach(const Node&) {} |
1174 | 1177 |
void discover(const Arc&) {} |
1175 | 1178 |
void examine(const Arc&) {} |
1176 | 1179 |
void leave(const Node&) {} |
1177 | 1180 |
void backtrack(const Arc&) {} |
1178 | 1181 |
|
1179 | 1182 |
template <typename _Visitor> |
1180 | 1183 |
struct Constraints { |
1181 | 1184 |
void constraints() { |
1182 | 1185 |
Arc arc; |
1183 | 1186 |
Node node; |
1184 | 1187 |
visitor.start(node); |
1185 | 1188 |
visitor.stop(arc); |
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 |
/// |
1235 | 1239 |
/// This interface of the DFS algorithm should be used in special cases |
1236 | 1240 |
/// when extra actions have to be performed in connection with certain |
1237 | 1241 |
/// events of the DFS algorithm. Otherwise consider to use Dfs or dfs() |
1238 | 1242 |
/// instead. |
1239 | 1243 |
/// |
1240 | 1244 |
/// \tparam GR The type of the digraph the algorithm runs on. |
1241 | 1245 |
/// The default type is \ref ListDigraph. |
1242 | 1246 |
/// The value of GR is not used directly by \ref DfsVisit, |
1243 | 1247 |
/// it is only passed to \ref DfsVisitDefaultTraits. |
1244 | 1248 |
/// \tparam VS The Visitor type that is used by the algorithm. |
1245 | 1249 |
/// \ref DfsVisitor "DfsVisitor<GR>" is an empty visitor, which |
1246 | 1250 |
/// does not observe the DFS events. If you want to observe the DFS |
1247 | 1251 |
/// events, you should implement your own visitor class. |
1248 | 1252 |
/// \tparam TR The traits class that defines various types used by the |
1249 | 1253 |
/// algorithm. By default, it is \ref DfsVisitDefaultTraits |
1250 | 1254 |
/// "DfsVisitDefaultTraits<GR>". |
1251 | 1255 |
/// In most cases, this parameter should not be set directly, |
1252 | 1256 |
/// consider to use the named template parameters instead. |
1253 | 1257 |
#ifdef DOXYGEN |
1254 | 1258 |
template <typename GR, typename VS, typename TR> |
1255 | 1259 |
#else |
1256 | 1260 |
template <typename GR = ListDigraph, |
1257 | 1261 |
typename VS = DfsVisitor<GR>, |
1258 | 1262 |
typename TR = DfsVisitDefaultTraits<GR> > |
1259 | 1263 |
#endif |
1260 | 1264 |
class DfsVisit { |
1261 | 1265 |
public: |
1262 | 1266 |
|
1263 | 1267 |
///The traits class. |
1264 | 1268 |
typedef TR Traits; |
1265 | 1269 |
|
1266 | 1270 |
///The type of the digraph the algorithm runs on. |
1267 | 1271 |
typedef typename Traits::Digraph Digraph; |
1268 | 1272 |
|
1269 | 1273 |
///The visitor type used by the algorithm. |
1270 | 1274 |
typedef VS Visitor; |
1271 | 1275 |
|
1272 | 1276 |
///The type of the map that indicates which nodes are reached. |
1273 | 1277 |
typedef typename Traits::ReachedMap ReachedMap; |
1274 | 1278 |
|
1275 | 1279 |
private: |
1276 | 1280 |
|
1277 | 1281 |
typedef typename Digraph::Node Node; |
1278 | 1282 |
typedef typename Digraph::NodeIt NodeIt; |
1279 | 1283 |
typedef typename Digraph::Arc Arc; |
1280 | 1284 |
typedef typename Digraph::OutArcIt OutArcIt; |
1281 | 1285 |
|
1282 | 1286 |
//Pointer to the underlying digraph. |
1283 | 1287 |
const Digraph *_digraph; |
1284 | 1288 |
//Pointer to the visitor object. |
1285 | 1289 |
Visitor *_visitor; |
1286 | 1290 |
//Pointer to the map of reached status of the nodes. |
1287 | 1291 |
ReachedMap *_reached; |
1288 | 1292 |
//Indicates if _reached is locally allocated (true) or not. |
1289 | 1293 |
bool local_reached; |
1290 | 1294 |
|
1291 | 1295 |
std::vector<typename Digraph::Arc> _stack; |
1292 | 1296 |
int _stack_head; |
1293 | 1297 |
|
1294 | 1298 |
//Creates the maps if necessary. |
1295 | 1299 |
void create_maps() { |
1296 | 1300 |
if(!_reached) { |
1297 | 1301 |
local_reached = true; |
1298 | 1302 |
_reached = Traits::createReachedMap(*_digraph); |
1299 | 1303 |
} |
1300 | 1304 |
} |
1301 | 1305 |
|
1302 | 1306 |
protected: |
1303 | 1307 |
|
1304 | 1308 |
DfsVisit() {} |
1305 | 1309 |
|
1306 | 1310 |
public: |
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> |
30 | 30 |
#include <lemon/core.h> |
31 | 31 |
#include <lemon/error.h> |
32 | 32 |
#include <lemon/maps.h> |
33 | 33 |
#include <lemon/path.h> |
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
/// \brief Default operation traits for the Dijkstra algorithm class. |
38 | 38 |
/// |
39 | 39 |
/// This operation traits class defines all computational operations and |
40 | 40 |
/// constants which are used in the Dijkstra algorithm. |
41 | 41 |
template <typename V> |
42 | 42 |
struct DijkstraDefaultOperationTraits { |
43 | 43 |
/// \e |
44 | 44 |
typedef V Value; |
45 | 45 |
/// \brief Gives back the zero value of the type. |
46 | 46 |
static Value zero() { |
47 | 47 |
return static_cast<Value>(0); |
48 | 48 |
} |
49 | 49 |
/// \brief Gives back the sum of the given two elements. |
50 | 50 |
static Value plus(const Value& left, const Value& right) { |
51 | 51 |
return left + right; |
52 | 52 |
} |
53 | 53 |
/// \brief Gives back true only if the first value is less than the second. |
54 | 54 |
static bool less(const Value& left, const Value& right) { |
55 | 55 |
return left < right; |
56 | 56 |
} |
57 | 57 |
}; |
58 | 58 |
|
59 | 59 |
///Default traits class of Dijkstra class. |
60 | 60 |
|
61 | 61 |
///Default traits class of Dijkstra class. |
62 | 62 |
///\tparam GR The type of the digraph. |
63 | 63 |
///\tparam LEN The type of the length map. |
64 | 64 |
template<typename GR, typename LEN> |
65 | 65 |
struct DijkstraDefaultTraits |
66 | 66 |
{ |
67 | 67 |
///The type of the digraph the algorithm runs on. |
68 | 68 |
typedef GR Digraph; |
69 | 69 |
|
70 | 70 |
///The type of the map that stores the arc lengths. |
71 | 71 |
|
72 | 72 |
///The type of the map that stores the arc lengths. |
73 | 73 |
///It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
74 | 74 |
typedef LEN LengthMap; |
75 | 75 |
///The type of the arc lengths. |
76 | 76 |
typedef typename LEN::Value Value; |
77 | 77 |
|
78 | 78 |
/// Operation traits for %Dijkstra algorithm. |
79 | 79 |
|
80 | 80 |
/// This class defines the operations that are used in the algorithm. |
81 | 81 |
/// \see DijkstraDefaultOperationTraits |
82 | 82 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
83 | 83 |
|
84 | 84 |
/// The cross reference type used by the heap. |
85 | 85 |
|
86 | 86 |
/// The cross reference type used by the heap. |
87 | 87 |
/// Usually it is \c Digraph::NodeMap<int>. |
88 | 88 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
89 | 89 |
///Instantiates a \c HeapCrossRef. |
90 | 90 |
|
91 | 91 |
///This function instantiates a \ref HeapCrossRef. |
92 | 92 |
/// \param g is the digraph, to which we would like to define the |
93 | 93 |
/// \ref HeapCrossRef. |
94 | 94 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
95 | 95 |
{ |
96 | 96 |
return new HeapCrossRef(g); |
97 | 97 |
} |
98 | 98 |
|
99 | 99 |
///The heap type used by the %Dijkstra algorithm. |
100 | 100 |
|
101 | 101 |
///The heap type used by the Dijkstra algorithm. |
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 |
30 | 30 |
/// \brief DIMACS file format reader. |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
/// \addtogroup dimacs_group |
35 | 35 |
/// @{ |
36 | 36 |
|
37 | 37 |
/// DIMACS file type descriptor. |
38 | 38 |
struct DimacsDescriptor |
39 | 39 |
{ |
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 | 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 |
89 | 89 |
{ |
90 | 90 |
throw FormatError("Missing or wrong problem type declaration."); |
91 | 91 |
} |
92 | 92 |
break; |
93 | 93 |
case 'c': |
94 | 94 |
getline(is, str); |
95 | 95 |
r.lineShift++; |
96 | 96 |
break; |
97 | 97 |
default: |
98 | 98 |
throw FormatError("Unknown DIMACS declaration."); |
99 | 99 |
} |
100 | 100 |
throw FormatError("Missing problem type declaration."); |
101 | 101 |
} |
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. |
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 | 32 |
template <typename GR> |
33 | 33 |
class ListArcSetBase { |
34 | 34 |
public: |
35 | 35 |
|
36 | 36 |
typedef typename GR::Node Node; |
37 | 37 |
typedef typename GR::NodeIt NodeIt; |
38 | 38 |
|
39 | 39 |
protected: |
40 | 40 |
|
41 | 41 |
struct NodeT { |
42 | 42 |
int first_out, first_in; |
43 | 43 |
NodeT() : first_out(-1), first_in(-1) {} |
44 | 44 |
}; |
45 | 45 |
|
46 | 46 |
typedef typename ItemSetTraits<GR, Node>:: |
47 | 47 |
template Map<NodeT>::Type NodesImplBase; |
48 | 48 |
|
49 | 49 |
NodesImplBase* _nodes; |
50 | 50 |
|
51 | 51 |
struct ArcT { |
52 | 52 |
Node source, target; |
53 | 53 |
int next_out, next_in; |
54 | 54 |
int prev_out, prev_in; |
55 | 55 |
ArcT() : prev_out(-1), prev_in(-1) {} |
56 | 56 |
}; |
57 | 57 |
|
58 | 58 |
std::vector<ArcT> arcs; |
59 | 59 |
|
60 | 60 |
int first_arc; |
61 | 61 |
int first_free_arc; |
62 | 62 |
|
63 | 63 |
const GR* _graph; |
64 | 64 |
|
65 | 65 |
void initalize(const GR& graph, NodesImplBase& nodes) { |
66 | 66 |
_graph = &graph; |
67 | 67 |
_nodes = &nodes; |
68 | 68 |
} |
69 | 69 |
|
70 | 70 |
public: |
71 | 71 |
|
72 | 72 |
class Arc { |
73 | 73 |
friend class ListArcSetBase<GR>; |
74 | 74 |
protected: |
75 | 75 |
Arc(int _id) : id(_id) {} |
76 | 76 |
int id; |
77 | 77 |
public: |
78 | 78 |
Arc() {} |
79 | 79 |
Arc(Invalid) : id(-1) {} |
80 | 80 |
bool operator==(const Arc& arc) const { return id == arc.id; } |
81 | 81 |
bool operator!=(const Arc& arc) const { return id != arc.id; } |
82 | 82 |
bool operator<(const Arc& arc) const { return id < arc.id; } |
83 | 83 |
}; |
84 | 84 |
|
85 | 85 |
ListArcSetBase() : first_arc(-1), first_free_arc(-1) {} |
86 | 86 |
|
87 | 87 |
Node addNode() { |
88 | 88 |
LEMON_ASSERT(false, |
89 | 89 |
"This graph structure does not support node insertion"); |
90 | 90 |
return INVALID; // avoid warning |
91 | 91 |
} |
92 | 92 |
|
93 | 93 |
Arc addArc(const Node& u, const Node& v) { |
94 | 94 |
int n; |
95 | 95 |
if (first_free_arc == -1) { |
96 | 96 |
n = arcs.size(); |
97 | 97 |
arcs.push_back(ArcT()); |
98 | 98 |
} else { |
99 | 99 |
n = first_free_arc; |
100 | 100 |
first_free_arc = arcs[first_free_arc].next_in; |
101 | 101 |
} |
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 | 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 | 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 |
|
69 | 69 |
public: |
70 | 70 |
|
71 | 71 |
///Constructor |
72 | 72 |
|
73 | 73 |
///Constructor. |
74 | 74 |
///\param gr A digraph. |
75 | 75 |
///\param start The starting point of the tour. If it is not given, |
76 | 76 |
///the tour will start from the first node that has an outgoing arc. |
77 | 77 |
DiEulerIt(const GR &gr, typename GR::Node start = INVALID) |
78 | 78 |
: g(gr), narc(g) |
79 | 79 |
{ |
80 | 80 |
if (start==INVALID) { |
81 | 81 |
NodeIt n(g); |
82 | 82 |
while (n!=INVALID && OutArcIt(g,n)==INVALID) ++n; |
83 | 83 |
start=n; |
84 | 84 |
} |
85 | 85 |
if (start!=INVALID) { |
86 | 86 |
for (NodeIt n(g); n!=INVALID; ++n) narc[n]=OutArcIt(g,n); |
87 | 87 |
while (narc[start]!=INVALID) { |
88 | 88 |
euler.push_back(narc[start]); |
89 | 89 |
Node next=g.target(narc[start]); |
90 | 90 |
++narc[start]; |
91 | 91 |
start=next; |
92 | 92 |
} |
93 | 93 |
} |
94 | 94 |
} |
95 | 95 |
|
96 | 96 |
///Arc conversion |
97 | 97 |
operator Arc() { return euler.empty()?INVALID:euler.front(); } |
98 | 98 |
///Compare with \c INVALID |
99 | 99 |
bool operator==(Invalid) { return euler.empty(); } |
100 | 100 |
///Compare with \c INVALID |
101 | 101 |
bool operator!=(Invalid) { return !euler.empty(); } |
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> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/assert.h> |
32 | 32 |
#include <lemon/elevator.h> |
33 | 33 |
|
34 | 34 |
///\ingroup matching |
35 | 35 |
///\file |
36 | 36 |
///\brief Fractional matching algorithms in general graphs. |
37 | 37 |
|
38 | 38 |
namespace lemon { |
39 | 39 |
|
40 | 40 |
/// \brief Default traits class of MaxFractionalMatching class. |
41 | 41 |
/// |
42 | 42 |
/// Default traits class of MaxFractionalMatching class. |
43 | 43 |
/// \tparam GR Graph type. |
44 | 44 |
template <typename GR> |
45 | 45 |
struct MaxFractionalMatchingDefaultTraits { |
46 | 46 |
|
47 | 47 |
/// \brief The type of the graph the algorithm runs on. |
48 | 48 |
typedef GR Graph; |
49 | 49 |
|
50 | 50 |
/// \brief The type of the map that stores the matching. |
51 | 51 |
/// |
52 | 52 |
/// The type of the map that stores the matching arcs. |
53 | 53 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
54 | 54 |
typedef typename Graph::template NodeMap<typename GR::Arc> MatchingMap; |
55 | 55 |
|
56 | 56 |
/// \brief Instantiates a MatchingMap. |
57 | 57 |
/// |
58 | 58 |
/// This function instantiates a \ref MatchingMap. |
59 | 59 |
/// \param graph The graph for which we would like to define |
60 | 60 |
/// the matching map. |
61 | 61 |
static MatchingMap* createMatchingMap(const Graph& graph) { |
62 | 62 |
return new MatchingMap(graph); |
63 | 63 |
} |
64 | 64 |
|
65 | 65 |
/// \brief The elevator type used by MaxFractionalMatching algorithm. |
66 | 66 |
/// |
67 | 67 |
/// The elevator type used by MaxFractionalMatching algorithm. |
68 | 68 |
/// |
69 | 69 |
/// \sa Elevator |
70 | 70 |
/// \sa LinkedElevator |
71 | 71 |
typedef LinkedElevator<Graph, typename Graph::Node> Elevator; |
72 | 72 |
|
73 | 73 |
/// \brief Instantiates an Elevator. |
74 | 74 |
/// |
75 | 75 |
/// This function instantiates an \ref Elevator. |
76 | 76 |
/// \param graph The graph for which we would like to define |
77 | 77 |
/// the elevator. |
78 | 78 |
/// \param max_level The maximum level of the elevator. |
79 | 79 |
static Elevator* createElevator(const Graph& graph, int max_level) { |
80 | 80 |
return new Elevator(graph, max_level); |
81 | 81 |
} |
82 | 82 |
}; |
83 | 83 |
|
84 | 84 |
/// \ingroup matching |
85 | 85 |
/// |
86 | 86 |
/// \brief Max cardinality fractional matching |
87 | 87 |
/// |
88 | 88 |
/// This class provides an implementation of fractional matching |
89 | 89 |
/// algorithm based on push-relabel principle. |
90 | 90 |
/// |
91 | 91 |
/// The maximum cardinality fractional matching is a relaxation of the |
92 | 92 |
/// maximum cardinality matching problem where the odd set constraints |
93 | 93 |
/// are omitted. |
94 | 94 |
/// It can be formulated with the following linear program. |
95 | 95 |
/// \f[ \sum_{e \in \delta(u)}x_e \le 1 \quad \forall u\in V\f] |
96 | 96 |
/// \f[x_e \ge 0\quad \forall e\in E\f] |
97 | 97 |
/// \f[\max \sum_{e\in E}x_e\f] |
98 | 98 |
/// where \f$\delta(X)\f$ is the set of edges incident to a node in |
99 | 99 |
/// \f$X\f$. The result can be represented as the union of a |
100 | 100 |
/// matching with one value edges and a set of odd length cycles |
101 | 101 |
/// with half value edges. |
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 { |
30 | 30 |
|
31 | 31 |
class FullDigraphBase { |
32 | 32 |
public: |
33 | 33 |
|
34 | 34 |
typedef FullDigraphBase Digraph; |
35 | 35 |
|
36 | 36 |
class Node; |
37 | 37 |
class Arc; |
38 | 38 |
|
39 | 39 |
protected: |
40 | 40 |
|
41 | 41 |
int _node_num; |
42 | 42 |
int _arc_num; |
43 | 43 |
|
44 | 44 |
FullDigraphBase() {} |
45 | 45 |
|
46 | 46 |
void construct(int n) { _node_num = n; _arc_num = n * n; } |
47 | 47 |
|
48 | 48 |
public: |
49 | 49 |
|
50 | 50 |
typedef True NodeNumTag; |
51 | 51 |
typedef True ArcNumTag; |
52 | 52 |
|
53 | 53 |
Node operator()(int ix) const { return Node(ix); } |
54 | 54 |
static int index(const Node& node) { return node._id; } |
55 | 55 |
|
56 | 56 |
Arc arc(const Node& s, const Node& t) const { |
57 | 57 |
return Arc(s._id * _node_num + t._id); |
58 | 58 |
} |
59 | 59 |
|
60 | 60 |
int nodeNum() const { return _node_num; } |
61 | 61 |
int arcNum() const { return _arc_num; } |
62 | 62 |
|
63 | 63 |
int maxNodeId() const { return _node_num - 1; } |
64 | 64 |
int maxArcId() const { return _arc_num - 1; } |
65 | 65 |
|
66 | 66 |
Node source(Arc arc) const { return arc._id / _node_num; } |
67 | 67 |
Node target(Arc arc) const { return arc._id % _node_num; } |
68 | 68 |
|
69 | 69 |
static int id(Node node) { return node._id; } |
70 | 70 |
static int id(Arc arc) { return arc._id; } |
71 | 71 |
|
72 | 72 |
static Node nodeFromId(int id) { return Node(id);} |
73 | 73 |
static Arc arcFromId(int id) { return Arc(id);} |
74 | 74 |
|
75 | 75 |
typedef True FindArcTag; |
76 | 76 |
|
77 | 77 |
Arc findArc(Node s, Node t, Arc prev = INVALID) const { |
78 | 78 |
return prev == INVALID ? arc(s, t) : INVALID; |
79 | 79 |
} |
80 | 80 |
|
81 | 81 |
class Node { |
82 | 82 |
friend class FullDigraphBase; |
83 | 83 |
|
84 | 84 |
protected: |
85 | 85 |
int _id; |
86 | 86 |
Node(int id) : _id(id) {} |
87 | 87 |
public: |
88 | 88 |
Node() {} |
89 | 89 |
Node (Invalid) : _id(-1) {} |
90 | 90 |
bool operator==(const Node node) const {return _id == node._id;} |
91 | 91 |
bool operator!=(const Node node) const {return _id != node._id;} |
92 | 92 |
bool operator<(const Node node) const {return _id < node._id;} |
93 | 93 |
}; |
94 | 94 |
|
95 | 95 |
class Arc { |
96 | 96 |
friend class FullDigraphBase; |
97 | 97 |
|
98 | 98 |
protected: |
99 | 99 |
int _id; // _node_num * source + target; |
100 | 100 |
|
101 | 101 |
Arc(int id) : _id(id) {} |
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 |
30 | 30 |
|
31 | 31 |
GlpkBase::GlpkBase() : LpBase() { |
32 | 32 |
lp = glp_create_prob(); |
33 | 33 |
glp_create_index(lp); |
34 | 34 |
messageLevel(MESSAGE_NOTHING); |
35 | 35 |
} |
36 | 36 |
|
37 | 37 |
GlpkBase::GlpkBase(const GlpkBase &other) : LpBase() { |
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 | 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 | 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]; |
100 | 100 |
ca[1] = i; |
101 | 101 |
glp_del_cols(lp, 1, ca); |
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 | 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 | 41 |
VoidPtr& operator=(T* ptr) { |
42 | 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(); |
67 | 67 |
virtual int _addRow(); |
68 | 68 |
virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u); |
69 | 69 |
|
70 | 70 |
virtual void _eraseCol(int i); |
71 | 71 |
virtual void _eraseRow(int i); |
72 | 72 |
|
73 | 73 |
virtual void _eraseColId(int i); |
74 | 74 |
virtual void _eraseRowId(int i); |
75 | 75 |
|
76 | 76 |
virtual void _getColName(int col, std::string& name) const; |
77 | 77 |
virtual void _setColName(int col, const std::string& name); |
78 | 78 |
virtual int _colByName(const std::string& name) const; |
79 | 79 |
|
80 | 80 |
virtual void _getRowName(int row, std::string& name) const; |
81 | 81 |
virtual void _setRowName(int row, const std::string& name); |
82 | 82 |
virtual int _rowByName(const std::string& name) const; |
83 | 83 |
|
84 | 84 |
virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e); |
85 | 85 |
virtual void _getRowCoeffs(int i, InsertIterator b) const; |
86 | 86 |
|
87 | 87 |
virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e); |
88 | 88 |
virtual void _getColCoeffs(int i, InsertIterator b) const; |
89 | 89 |
|
90 | 90 |
virtual void _setCoeff(int row, int col, Value value); |
91 | 91 |
virtual Value _getCoeff(int row, int col) const; |
92 | 92 |
|
93 | 93 |
virtual void _setColLowerBound(int i, Value value); |
94 | 94 |
virtual Value _getColLowerBound(int i) const; |
95 | 95 |
|
96 | 96 |
virtual void _setColUpperBound(int i, Value value); |
97 | 97 |
virtual Value _getColUpperBound(int i) const; |
98 | 98 |
|
99 | 99 |
virtual void _setRowLowerBound(int i, Value value); |
100 | 100 |
virtual Value _getRowLowerBound(int i) const; |
101 | 101 |
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 | 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 | 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 |
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> |
30 | 30 |
#include<ctime> |
31 | 31 |
#else |
32 | 32 |
#include<lemon/bits/windows.h> |
33 | 33 |
#endif |
34 | 34 |
|
35 | 35 |
#include<lemon/math.h> |
36 | 36 |
#include<lemon/core.h> |
37 | 37 |
#include<lemon/dim2.h> |
38 | 38 |
#include<lemon/maps.h> |
39 | 39 |
#include<lemon/color.h> |
40 | 40 |
#include<lemon/bits/bezier.h> |
41 | 41 |
#include<lemon/error.h> |
42 | 42 |
|
43 | 43 |
|
44 | 44 |
///\ingroup eps_io |
45 | 45 |
///\file |
46 | 46 |
///\brief A well configurable tool for visualizing graphs |
47 | 47 |
|
48 | 48 |
namespace lemon { |
49 | 49 |
|
50 | 50 |
namespace _graph_to_eps_bits { |
51 | 51 |
template<class MT> |
52 | 52 |
class _NegY { |
53 | 53 |
public: |
54 | 54 |
typedef typename MT::Key Key; |
55 | 55 |
typedef typename MT::Value Value; |
56 | 56 |
const MT ↦ |
57 | 57 |
int yscale; |
58 | 58 |
_NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {} |
59 | 59 |
Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);} |
60 | 60 |
}; |
61 | 61 |
} |
62 | 62 |
|
63 | 63 |
///Default traits class of GraphToEps |
64 | 64 |
|
65 | 65 |
///Default traits class of \ref GraphToEps. |
66 | 66 |
/// |
67 | 67 |
///\param GR is the type of the underlying graph. |
68 | 68 |
template<class GR> |
69 | 69 |
struct DefaultGraphToEpsTraits |
70 | 70 |
{ |
71 | 71 |
typedef GR Graph; |
72 | 72 |
typedef GR Digraph; |
73 | 73 |
typedef typename Graph::Node Node; |
74 | 74 |
typedef typename Graph::NodeIt NodeIt; |
75 | 75 |
typedef typename Graph::Arc Arc; |
76 | 76 |
typedef typename Graph::ArcIt ArcIt; |
77 | 77 |
typedef typename Graph::InArcIt InArcIt; |
78 | 78 |
typedef typename Graph::OutArcIt OutArcIt; |
79 | 79 |
|
80 | 80 |
|
81 | 81 |
const Graph &g; |
82 | 82 |
|
83 | 83 |
std::ostream& os; |
84 | 84 |
|
85 | 85 |
typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType; |
86 | 86 |
CoordsMapType _coords; |
87 | 87 |
ConstMap<typename Graph::Node,double > _nodeSizes; |
88 | 88 |
ConstMap<typename Graph::Node,int > _nodeShapes; |
89 | 89 |
|
90 | 90 |
ConstMap<typename Graph::Node,Color > _nodeColors; |
91 | 91 |
ConstMap<typename Graph::Arc,Color > _arcColors; |
92 | 92 |
|
93 | 93 |
ConstMap<typename Graph::Arc,double > _arcWidths; |
94 | 94 |
|
95 | 95 |
double _arcWidthScale; |
96 | 96 |
|
97 | 97 |
double _nodeScale; |
98 | 98 |
double _xBorder, _yBorder; |
99 | 99 |
double _scale; |
100 | 100 |
double _nodeBorderQuotient; |
101 | 101 |
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 | 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 | 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 | 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; |
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> |
30 | 30 |
#include <lemon/path.h> |
31 | 31 |
#include <lemon/tolerance.h> |
32 | 32 |
#include <lemon/connectivity.h> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \brief Default traits class of HartmannOrlinMmc class. |
37 | 37 |
/// |
38 | 38 |
/// Default traits class of HartmannOrlinMmc class. |
39 | 39 |
/// \tparam GR The type of the digraph. |
40 | 40 |
/// \tparam CM The type of the cost map. |
41 | 41 |
/// It must conform to the \ref concepts::Rea_data "Rea_data" concept. |
42 | 42 |
#ifdef DOXYGEN |
43 | 43 |
template <typename GR, typename CM> |
44 | 44 |
#else |
45 | 45 |
template <typename GR, typename CM, |
46 | 46 |
bool integer = std::numeric_limits<typename CM::Value>::is_integer> |
47 | 47 |
#endif |
48 | 48 |
struct HartmannOrlinMmcDefaultTraits |
49 | 49 |
{ |
50 | 50 |
/// The type of the digraph |
51 | 51 |
typedef GR Digraph; |
52 | 52 |
/// The type of the cost map |
53 | 53 |
typedef CM CostMap; |
54 | 54 |
/// The type of the arc costs |
55 | 55 |
typedef typename CostMap::Value Cost; |
56 | 56 |
|
57 | 57 |
/// \brief The large cost type used for internal computations |
58 | 58 |
/// |
59 | 59 |
/// The large cost type used for internal computations. |
60 | 60 |
/// It is \c long \c long if the \c Cost type is integer, |
61 | 61 |
/// otherwise it is \c double. |
62 | 62 |
/// \c Cost must be convertible to \c LargeCost. |
63 | 63 |
typedef double LargeCost; |
64 | 64 |
|
65 | 65 |
/// The tolerance type used for internal computations |
66 | 66 |
typedef lemon::Tolerance<LargeCost> Tolerance; |
67 | 67 |
|
68 | 68 |
/// \brief The path type of the found cycles |
69 | 69 |
/// |
70 | 70 |
/// The path type of the found cycles. |
71 | 71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
72 | 72 |
/// and it must have an \c addFront() function. |
73 | 73 |
typedef lemon::Path<Digraph> Path; |
74 | 74 |
}; |
75 | 75 |
|
76 | 76 |
// Default traits class for integer cost types |
77 | 77 |
template <typename GR, typename CM> |
78 | 78 |
struct HartmannOrlinMmcDefaultTraits<GR, CM, true> |
79 | 79 |
{ |
80 | 80 |
typedef GR Digraph; |
81 | 81 |
typedef CM CostMap; |
82 | 82 |
typedef typename CostMap::Value Cost; |
83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
84 | 84 |
typedef long long LargeCost; |
85 | 85 |
#else |
86 | 86 |
typedef long LargeCost; |
87 | 87 |
#endif |
88 | 88 |
typedef lemon::Tolerance<LargeCost> Tolerance; |
89 | 89 |
typedef lemon::Path<Digraph> Path; |
90 | 90 |
}; |
91 | 91 |
|
92 | 92 |
|
93 | 93 |
/// \addtogroup min_mean_cycle |
94 | 94 |
/// @{ |
95 | 95 |
|
96 | 96 |
/// \brief Implementation of the Hartmann-Orlin algorithm for finding |
97 | 97 |
/// a minimum mean cycle. |
98 | 98 |
/// |
99 | 99 |
/// This class implements the Hartmann-Orlin algorithm for finding |
100 | 100 |
/// a directed cycle of minimum mean cost in a digraph |
101 | 101 |
/// \ref amo93networkflows, \ref dasdan98minmeancycle. |
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> |
30 | 30 |
#include <lemon/path.h> |
31 | 31 |
#include <lemon/tolerance.h> |
32 | 32 |
#include <lemon/connectivity.h> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \brief Default traits class of HowardMmc class. |
37 | 37 |
/// |
38 | 38 |
/// Default traits class of HowardMmc class. |
39 | 39 |
/// \tparam GR The type of the digraph. |
40 | 40 |
/// \tparam CM The type of the cost map. |
41 | 41 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
42 | 42 |
#ifdef DOXYGEN |
43 | 43 |
template <typename GR, typename CM> |
44 | 44 |
#else |
45 | 45 |
template <typename GR, typename CM, |
46 | 46 |
bool integer = std::numeric_limits<typename CM::Value>::is_integer> |
47 | 47 |
#endif |
48 | 48 |
struct HowardMmcDefaultTraits |
49 | 49 |
{ |
50 | 50 |
/// The type of the digraph |
51 | 51 |
typedef GR Digraph; |
52 | 52 |
/// The type of the cost map |
53 | 53 |
typedef CM CostMap; |
54 | 54 |
/// The type of the arc costs |
55 | 55 |
typedef typename CostMap::Value Cost; |
56 | 56 |
|
57 | 57 |
/// \brief The large cost type used for internal computations |
58 | 58 |
/// |
59 | 59 |
/// The large cost type used for internal computations. |
60 | 60 |
/// It is \c long \c long if the \c Cost type is integer, |
61 | 61 |
/// otherwise it is \c double. |
62 | 62 |
/// \c Cost must be convertible to \c LargeCost. |
63 | 63 |
typedef double LargeCost; |
64 | 64 |
|
65 | 65 |
/// The tolerance type used for internal computations |
66 | 66 |
typedef lemon::Tolerance<LargeCost> Tolerance; |
67 | 67 |
|
68 | 68 |
/// \brief The path type of the found cycles |
69 | 69 |
/// |
70 | 70 |
/// The path type of the found cycles. |
71 | 71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
72 | 72 |
/// and it must have an \c addBack() function. |
73 | 73 |
typedef lemon::Path<Digraph> Path; |
74 | 74 |
}; |
75 | 75 |
|
76 | 76 |
// Default traits class for integer cost types |
77 | 77 |
template <typename GR, typename CM> |
78 | 78 |
struct HowardMmcDefaultTraits<GR, CM, true> |
79 | 79 |
{ |
80 | 80 |
typedef GR Digraph; |
81 | 81 |
typedef CM CostMap; |
82 | 82 |
typedef typename CostMap::Value Cost; |
83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
84 | 84 |
typedef long long LargeCost; |
85 | 85 |
#else |
86 | 86 |
typedef long LargeCost; |
87 | 87 |
#endif |
88 | 88 |
typedef lemon::Tolerance<LargeCost> Tolerance; |
89 | 89 |
typedef lemon::Path<Digraph> Path; |
90 | 90 |
}; |
91 | 91 |
|
92 | 92 |
|
93 | 93 |
/// \addtogroup min_mean_cycle |
94 | 94 |
/// @{ |
95 | 95 |
|
96 | 96 |
/// \brief Implementation of Howard's algorithm for finding a minimum |
97 | 97 |
/// mean cycle. |
98 | 98 |
/// |
99 | 99 |
/// This class implements Howard's policy iteration algorithm for finding |
100 | 100 |
/// a directed cycle of minimum mean cost in a digraph |
101 | 101 |
/// \ref amo93networkflows, \ref dasdan98minmeancycle. |
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> |
30 | 30 |
#include <lemon/path.h> |
31 | 31 |
#include <lemon/tolerance.h> |
32 | 32 |
#include <lemon/connectivity.h> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \brief Default traits class of KarpMmc class. |
37 | 37 |
/// |
38 | 38 |
/// Default traits class of KarpMmc class. |
39 | 39 |
/// \tparam GR The type of the digraph. |
40 | 40 |
/// \tparam CM The type of the cost map. |
41 | 41 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
42 | 42 |
#ifdef DOXYGEN |
43 | 43 |
template <typename GR, typename CM> |
44 | 44 |
#else |
45 | 45 |
template <typename GR, typename CM, |
46 | 46 |
bool integer = std::numeric_limits<typename CM::Value>::is_integer> |
47 | 47 |
#endif |
48 | 48 |
struct KarpMmcDefaultTraits |
49 | 49 |
{ |
50 | 50 |
/// The type of the digraph |
51 | 51 |
typedef GR Digraph; |
52 | 52 |
/// The type of the cost map |
53 | 53 |
typedef CM CostMap; |
54 | 54 |
/// The type of the arc costs |
55 | 55 |
typedef typename CostMap::Value Cost; |
56 | 56 |
|
57 | 57 |
/// \brief The large cost type used for internal computations |
58 | 58 |
/// |
59 | 59 |
/// The large cost type used for internal computations. |
60 | 60 |
/// It is \c long \c long if the \c Cost type is integer, |
61 | 61 |
/// otherwise it is \c double. |
62 | 62 |
/// \c Cost must be convertible to \c LargeCost. |
63 | 63 |
typedef double LargeCost; |
64 | 64 |
|
65 | 65 |
/// The tolerance type used for internal computations |
66 | 66 |
typedef lemon::Tolerance<LargeCost> Tolerance; |
67 | 67 |
|
68 | 68 |
/// \brief The path type of the found cycles |
69 | 69 |
/// |
70 | 70 |
/// The path type of the found cycles. |
71 | 71 |
/// It must conform to the \ref lemon::concepts::Path "Path" concept |
72 | 72 |
/// and it must have an \c addFront() function. |
73 | 73 |
typedef lemon::Path<Digraph> Path; |
74 | 74 |
}; |
75 | 75 |
|
76 | 76 |
// Default traits class for integer cost types |
77 | 77 |
template <typename GR, typename CM> |
78 | 78 |
struct KarpMmcDefaultTraits<GR, CM, true> |
79 | 79 |
{ |
80 | 80 |
typedef GR Digraph; |
81 | 81 |
typedef CM CostMap; |
82 | 82 |
typedef typename CostMap::Value Cost; |
83 | 83 |
#ifdef LEMON_HAVE_LONG_LONG |
84 | 84 |
typedef long long LargeCost; |
85 | 85 |
#else |
86 | 86 |
typedef long LargeCost; |
87 | 87 |
#endif |
88 | 88 |
typedef lemon::Tolerance<LargeCost> Tolerance; |
89 | 89 |
typedef lemon::Path<Digraph> Path; |
90 | 90 |
}; |
91 | 91 |
|
92 | 92 |
|
93 | 93 |
/// \addtogroup min_mean_cycle |
94 | 94 |
/// @{ |
95 | 95 |
|
96 | 96 |
/// \brief Implementation of Karp's algorithm for finding a minimum |
97 | 97 |
/// mean cycle. |
98 | 98 |
/// |
99 | 99 |
/// This class implements Karp's algorithm for finding a directed |
100 | 100 |
/// cycle of minimum mean cost in a digraph |
101 | 101 |
/// \ref amo93networkflows, \ref dasdan98minmeancycle. |
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> |
30 | 30 |
|
31 | 31 |
#include <set> |
32 | 32 |
#include <map> |
33 | 33 |
|
34 | 34 |
#include <lemon/core.h> |
35 | 35 |
|
36 | 36 |
#include <lemon/lgf_writer.h> |
37 | 37 |
|
38 | 38 |
#include <lemon/concept_check.h> |
39 | 39 |
#include <lemon/concepts/maps.h> |
40 | 40 |
|
41 | 41 |
namespace lemon { |
42 | 42 |
|
43 | 43 |
namespace _reader_bits { |
44 | 44 |
|
45 | 45 |
template <typename Value> |
46 | 46 |
struct DefaultConverter { |
47 | 47 |
Value operator()(const std::string& str) { |
48 | 48 |
std::istringstream is(str); |
49 | 49 |
Value value; |
50 | 50 |
if (!(is >> value)) { |
51 | 51 |
throw FormatError("Cannot read token"); |
52 | 52 |
} |
53 | 53 |
|
54 | 54 |
char c; |
55 | 55 |
if (is >> std::ws >> c) { |
56 | 56 |
throw FormatError("Remaining characters in token"); |
57 | 57 |
} |
58 | 58 |
return value; |
59 | 59 |
} |
60 | 60 |
}; |
61 | 61 |
|
62 | 62 |
template <> |
63 | 63 |
struct DefaultConverter<std::string> { |
64 | 64 |
std::string operator()(const std::string& str) { |
65 | 65 |
return str; |
66 | 66 |
} |
67 | 67 |
}; |
68 | 68 |
|
69 | 69 |
template <typename _Item> |
70 | 70 |
class MapStorageBase { |
71 | 71 |
public: |
72 | 72 |
typedef _Item Item; |
73 | 73 |
|
74 | 74 |
public: |
75 | 75 |
MapStorageBase() {} |
76 | 76 |
virtual ~MapStorageBase() {} |
77 | 77 |
|
78 | 78 |
virtual void set(const Item& item, const std::string& value) = 0; |
79 | 79 |
|
80 | 80 |
}; |
81 | 81 |
|
82 | 82 |
template <typename _Item, typename _Map, |
83 | 83 |
typename _Converter = DefaultConverter<typename _Map::Value> > |
84 | 84 |
class MapStorage : public MapStorageBase<_Item> { |
85 | 85 |
public: |
86 | 86 |
typedef _Map Map; |
87 | 87 |
typedef _Converter Converter; |
88 | 88 |
typedef _Item Item; |
89 | 89 |
|
90 | 90 |
private: |
91 | 91 |
Map& _map; |
92 | 92 |
Converter _converter; |
93 | 93 |
|
94 | 94 |
public: |
95 | 95 |
MapStorage(Map& map, const Converter& converter = Converter()) |
96 | 96 |
: _map(map), _converter(converter) {} |
97 | 97 |
virtual ~MapStorage() {} |
98 | 98 |
|
99 | 99 |
virtual void set(const Item& item ,const std::string& value) { |
100 | 100 |
_map.set(item, _converter(value)); |
101 | 101 |
} |
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> |
30 | 30 |
|
31 | 31 |
#include <algorithm> |
32 | 32 |
|
33 | 33 |
#include <vector> |
34 | 34 |
#include <functional> |
35 | 35 |
|
36 | 36 |
#include <lemon/core.h> |
37 | 37 |
#include <lemon/maps.h> |
38 | 38 |
|
39 | 39 |
#include <lemon/concept_check.h> |
40 | 40 |
#include <lemon/concepts/maps.h> |
41 | 41 |
|
42 | 42 |
namespace lemon { |
43 | 43 |
|
44 | 44 |
namespace _writer_bits { |
45 | 45 |
|
46 | 46 |
template <typename Value> |
47 | 47 |
struct DefaultConverter { |
48 | 48 |
std::string operator()(const Value& value) { |
49 | 49 |
std::ostringstream os; |
50 | 50 |
os << value; |
51 | 51 |
return os.str(); |
52 | 52 |
} |
53 | 53 |
}; |
54 | 54 |
|
55 | 55 |
template <typename T> |
56 | 56 |
bool operator<(const T&, const T&) { |
57 | 57 |
throw FormatError("Label map is not comparable"); |
58 | 58 |
} |
59 | 59 |
|
60 | 60 |
template <typename _Map> |
61 | 61 |
class MapLess { |
62 | 62 |
public: |
63 | 63 |
typedef _Map Map; |
64 | 64 |
typedef typename Map::Key Item; |
65 | 65 |
|
66 | 66 |
private: |
67 | 67 |
const Map& _map; |
68 | 68 |
|
69 | 69 |
public: |
70 | 70 |
MapLess(const Map& map) : _map(map) {} |
71 | 71 |
|
72 | 72 |
bool operator()(const Item& left, const Item& right) { |
73 | 73 |
return _map[left] < _map[right]; |
74 | 74 |
} |
75 | 75 |
}; |
76 | 76 |
|
77 | 77 |
template <typename _Graph, bool _dir, typename _Map> |
78 | 78 |
class GraphArcMapLess { |
79 | 79 |
public: |
80 | 80 |
typedef _Map Map; |
81 | 81 |
typedef _Graph Graph; |
82 | 82 |
typedef typename Graph::Edge Item; |
83 | 83 |
|
84 | 84 |
private: |
85 | 85 |
const Graph& _graph; |
86 | 86 |
const Map& _map; |
87 | 87 |
|
88 | 88 |
public: |
89 | 89 |
GraphArcMapLess(const Graph& graph, const Map& map) |
90 | 90 |
: _graph(graph), _map(map) {} |
91 | 91 |
|
92 | 92 |
bool operator()(const Item& left, const Item& right) { |
93 | 93 |
return _map[_graph.direct(left, _dir)] < |
94 | 94 |
_map[_graph.direct(right, _dir)]; |
95 | 95 |
} |
96 | 96 |
}; |
97 | 97 |
|
98 | 98 |
template <typename _Item> |
99 | 99 |
class MapStorageBase { |
100 | 100 |
public: |
101 | 101 |
typedef _Item Item; |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2010 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
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 |
|
30 | 30 |
#include <vector> |
31 | 31 |
#include <list> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
class ListDigraph; |
36 | 36 |
|
37 | 37 |
class ListDigraphBase { |
38 | 38 |
|
39 | 39 |
protected: |
40 | 40 |
struct NodeT { |
41 | 41 |
int first_in, first_out; |
42 | 42 |
int prev, next; |
43 | 43 |
}; |
44 | 44 |
|
45 | 45 |
struct ArcT { |
46 | 46 |
int target, source; |
47 | 47 |
int prev_in, prev_out; |
48 | 48 |
int next_in, next_out; |
49 | 49 |
}; |
50 | 50 |
|
51 | 51 |
std::vector<NodeT> nodes; |
52 | 52 |
|
53 | 53 |
int first_node; |
54 | 54 |
|
55 | 55 |
int first_free_node; |
56 | 56 |
|
57 | 57 |
std::vector<ArcT> arcs; |
58 | 58 |
|
59 | 59 |
int first_free_arc; |
60 | 60 |
|
61 | 61 |
public: |
62 | 62 |
|
63 | 63 |
typedef ListDigraphBase Digraph; |
64 | 64 |
|
65 | 65 |
class Node { |
66 | 66 |
friend class ListDigraphBase; |
67 | 67 |
friend class ListDigraph; |
68 | 68 |
protected: |
69 | 69 |
|
70 | 70 |
int id; |
71 | 71 |
explicit Node(int pid) { id = pid;} |
72 | 72 |
|
73 | 73 |
public: |
74 | 74 |
Node() {} |
75 | 75 |
Node (Invalid) { id = -1; } |
76 | 76 |
bool operator==(const Node& node) const {return id == node.id;} |
77 | 77 |
bool operator!=(const Node& node) const {return id != node.id;} |
78 | 78 |
bool operator<(const Node& node) const {return id < node.id;} |
79 | 79 |
}; |
80 | 80 |
|
81 | 81 |
class Arc { |
82 | 82 |
friend class ListDigraphBase; |
83 | 83 |
friend class ListDigraph; |
84 | 84 |
protected: |
85 | 85 |
|
86 | 86 |
int id; |
87 | 87 |
explicit Arc(int pid) { id = pid;} |
88 | 88 |
|
89 | 89 |
public: |
90 | 90 |
Arc() {} |
91 | 91 |
Arc (Invalid) { id = -1; } |
92 | 92 |
bool operator==(const Arc& arc) const {return id == arc.id;} |
93 | 93 |
bool operator!=(const Arc& arc) const {return id != arc.id;} |
94 | 94 |
bool operator<(const Arc& arc) const {return id < arc.id;} |
95 | 95 |
}; |
96 | 96 |
|
97 | 97 |
|
98 | 98 |
|
99 | 99 |
ListDigraphBase() |
100 | 100 |
: nodes(), first_node(-1), |
101 | 101 |
first_free_node(-1), arcs(), first_free_arc(-1) {} |
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 |
30 | 30 |
#include <lemon/soplex.h> |
31 | 31 |
#elif LEMON_HAVE_CLP |
32 | 32 |
#include <lemon/clp.h> |
33 | 33 |
#endif |
34 | 34 |
|
35 | 35 |
///\file |
36 | 36 |
///\brief Defines a default LP solver |
37 | 37 |
///\ingroup lp_group |
38 | 38 |
namespace lemon { |
39 | 39 |
|
40 | 40 |
#ifdef DOXYGEN |
41 | 41 |
///The default LP solver identifier |
42 | 42 |
|
43 | 43 |
///The default LP solver identifier. |
44 | 44 |
///\ingroup lp_group |
45 | 45 |
/// |
46 | 46 |
///Currently, the possible values are \c GLPK, \c CPLEX, |
47 | 47 |
///\c SOPLEX or \c CLP |
48 | 48 |
#define LEMON_DEFAULT_LP SOLVER |
49 | 49 |
///The default LP solver |
50 | 50 |
|
51 | 51 |
///The default LP solver. |
52 | 52 |
///\ingroup lp_group |
53 | 53 |
/// |
54 | 54 |
///Currently, it is either \c GlpkLp, \c CplexLp, \c SoplexLp or \c ClpLp |
55 | 55 |
typedef GlpkLp Lp; |
56 | 56 |
|
57 | 57 |
///The default MIP solver identifier |
58 | 58 |
|
59 | 59 |
///The default MIP solver identifier. |
60 | 60 |
///\ingroup lp_group |
61 | 61 |
/// |
62 | 62 |
///Currently, the possible values are \c GLPK or \c CPLEX |
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 | 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 |
|
30 | 30 |
} //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_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> |
30 | 30 |
|
31 | 31 |
#include<lemon/core.h> |
32 | 32 |
#include<lemon/bits/solver_bits.h> |
33 | 33 |
|
34 | 34 |
///\file |
35 | 35 |
///\brief The interface of the LP solver interface. |
36 | 36 |
///\ingroup lp_group |
37 | 37 |
namespace lemon { |
38 | 38 |
|
39 | 39 |
///Common base class for LP and MIP solvers |
40 | 40 |
|
41 | 41 |
///Usually this class is not used directly, please use one of the concrete |
42 | 42 |
///implementations of the solver interface. |
43 | 43 |
///\ingroup lp_group |
44 | 44 |
class LpBase { |
45 | 45 |
|
46 | 46 |
protected: |
47 | 47 |
|
48 | 48 |
_solver_bits::VarIndex rows; |
49 | 49 |
_solver_bits::VarIndex cols; |
50 | 50 |
|
51 | 51 |
public: |
52 | 52 |
|
53 | 53 |
///Possible outcomes of an LP solving procedure |
54 | 54 |
enum SolveExitStatus { |
55 | 55 |
/// = 0. It means that the problem has been successfully solved: either |
56 | 56 |
///an optimal solution has been found or infeasibility/unboundedness |
57 | 57 |
///has been proved. |
58 | 58 |
SOLVED = 0, |
59 | 59 |
/// = 1. Any other case (including the case when some user specified |
60 | 60 |
///limit has been exceeded). |
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. |
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 |
|
30 | 30 |
int SkeletonSolverBase::_addRow() |
31 | 31 |
{ |
32 | 32 |
return ++row_num; |
33 | 33 |
} |
34 | 34 |
|
35 | 35 |
int SkeletonSolverBase::_addRow(Value, ExprIterator, ExprIterator, Value) |
36 | 36 |
{ |
37 | 37 |
return ++row_num; |
38 | 38 |
} |
39 | 39 |
|
40 | 40 |
void SkeletonSolverBase::_eraseCol(int) {} |
41 | 41 |
void SkeletonSolverBase::_eraseRow(int) {} |
42 | 42 |
|
43 | 43 |
void SkeletonSolverBase::_getColName(int, std::string &) const {} |
44 | 44 |
void SkeletonSolverBase::_setColName(int, const std::string &) {} |
45 | 45 |
int SkeletonSolverBase::_colByName(const std::string&) const { return -1; } |
46 | 46 |
|
47 | 47 |
void SkeletonSolverBase::_getRowName(int, std::string &) const {} |
48 | 48 |
void SkeletonSolverBase::_setRowName(int, const std::string &) {} |
49 | 49 |
int SkeletonSolverBase::_rowByName(const std::string&) const { return -1; } |
50 | 50 |
|
51 | 51 |
void SkeletonSolverBase::_setRowCoeffs(int, ExprIterator, ExprIterator) {} |
52 | 52 |
void SkeletonSolverBase::_getRowCoeffs(int, InsertIterator) const {} |
53 | 53 |
|
54 | 54 |
void SkeletonSolverBase::_setColCoeffs(int, ExprIterator, ExprIterator) {} |
55 | 55 |
void SkeletonSolverBase::_getColCoeffs(int, InsertIterator) const {} |
56 | 56 |
|
57 | 57 |
void SkeletonSolverBase::_setCoeff(int, int, Value) {} |
58 | 58 |
SkeletonSolverBase::Value SkeletonSolverBase::_getCoeff(int, int) const |
59 | 59 |
{ return 0; } |
60 | 60 |
|
61 | 61 |
void SkeletonSolverBase::_setColLowerBound(int, Value) {} |
62 | 62 |
SkeletonSolverBase::Value SkeletonSolverBase::_getColLowerBound(int) const |
63 | 63 |
{ return 0; } |
64 | 64 |
|
65 | 65 |
void SkeletonSolverBase::_setColUpperBound(int, Value) {} |
66 | 66 |
SkeletonSolverBase::Value SkeletonSolverBase::_getColUpperBound(int) const |
67 | 67 |
{ return 0; } |
68 | 68 |
|
69 | 69 |
void SkeletonSolverBase::_setRowLowerBound(int, Value) {} |
70 | 70 |
SkeletonSolverBase::Value SkeletonSolverBase::_getRowLowerBound(int) const |
71 | 71 |
{ return 0; } |
72 | 72 |
|
73 | 73 |
void SkeletonSolverBase::_setRowUpperBound(int, Value) {} |
74 | 74 |
SkeletonSolverBase::Value SkeletonSolverBase::_getRowUpperBound(int) const |
75 | 75 |
{ return 0; } |
76 | 76 |
|
77 | 77 |
void SkeletonSolverBase::_setObjCoeffs(ExprIterator, ExprIterator) {} |
78 | 78 |
void SkeletonSolverBase::_getObjCoeffs(InsertIterator) const {}; |
79 | 79 |
|
80 | 80 |
void SkeletonSolverBase::_setObjCoeff(int, Value) {} |
81 | 81 |
SkeletonSolverBase::Value SkeletonSolverBase::_getObjCoeff(int) const |
82 | 82 |
{ return 0; } |
83 | 83 |
|
84 | 84 |
void SkeletonSolverBase::_setSense(Sense) {} |
85 | 85 |
SkeletonSolverBase::Sense SkeletonSolverBase::_getSense() const |
86 | 86 |
{ return MIN; } |
87 | 87 |
|
88 | 88 |
void SkeletonSolverBase::_clear() { |
89 | 89 |
row_num = col_num = 0; |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
void SkeletonSolverBase::_messageLevel(MessageLevel) {} |
93 | 93 |
|
94 | 94 |
LpSkeleton::SolveExitStatus LpSkeleton::_solve() { return SOLVED; } |
95 | 95 |
|
96 | 96 |
LpSkeleton::Value LpSkeleton::_getPrimal(int) const { return 0; } |
97 | 97 |
LpSkeleton::Value LpSkeleton::_getDual(int) const { return 0; } |
98 | 98 |
LpSkeleton::Value LpSkeleton::_getPrimalValue() const { return 0; } |
99 | 99 |
|
100 | 100 |
LpSkeleton::Value LpSkeleton::_getPrimalRay(int) const { return 0; } |
101 | 101 |
LpSkeleton::Value LpSkeleton::_getDualRay(int) const { 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 |
#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 |
57 | 57 |
virtual void _setColName(int col, const std::string& name); |
58 | 58 |
/// \e |
59 | 59 |
virtual int _colByName(const std::string& name) const; |
60 | 60 |
|
61 | 61 |
/// \e |
62 | 62 |
virtual void _getRowName(int row, std::string& name) const; |
63 | 63 |
/// \e |
64 | 64 |
virtual void _setRowName(int row, const std::string& name); |
65 | 65 |
/// \e |
66 | 66 |
virtual int _rowByName(const std::string& name) const; |
67 | 67 |
|
68 | 68 |
/// \e |
69 | 69 |
virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e); |
70 | 70 |
/// \e |
71 | 71 |
virtual void _getRowCoeffs(int i, InsertIterator b) const; |
72 | 72 |
/// \e |
73 | 73 |
virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e); |
74 | 74 |
/// \e |
75 | 75 |
virtual void _getColCoeffs(int i, InsertIterator b) const; |
76 | 76 |
|
77 | 77 |
/// Set one element of the coefficient matrix |
78 | 78 |
virtual void _setCoeff(int row, int col, Value value); |
79 | 79 |
|
80 | 80 |
/// Get one element of the coefficient matrix |
81 | 81 |
virtual Value _getCoeff(int row, int col) const; |
82 | 82 |
|
83 | 83 |
/// The lower bound of a variable (column) have to be given by an |
84 | 84 |
/// extended number of type Value, i.e. a finite number of type |
85 | 85 |
/// Value or -\ref INF. |
86 | 86 |
virtual void _setColLowerBound(int i, Value value); |
87 | 87 |
/// \e |
88 | 88 |
|
89 | 89 |
/// The lower bound of a variable (column) is an |
90 | 90 |
/// extended number of type Value, i.e. a finite number of type |
91 | 91 |
/// Value or -\ref INF. |
92 | 92 |
virtual Value _getColLowerBound(int i) const; |
93 | 93 |
|
94 | 94 |
/// The upper bound of a variable (column) have to be given by an |
95 | 95 |
/// extended number of type Value, i.e. a finite number of type |
96 | 96 |
/// Value or \ref INF. |
97 | 97 |
virtual void _setColUpperBound(int i, Value value); |
98 | 98 |
/// \e |
99 | 99 |
|
100 | 100 |
/// The upper bound of a variable (column) is an |
101 | 101 |
/// extended number of type Value, i.e. a finite number of type |
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 |
30 | 30 |
///\ingroup maps |
31 | 31 |
///\brief Miscellaneous property maps |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
/// \addtogroup maps |
36 | 36 |
/// @{ |
37 | 37 |
|
38 | 38 |
/// Base class of maps. |
39 | 39 |
|
40 | 40 |
/// Base class of maps. It provides the necessary type definitions |
41 | 41 |
/// required by the map %concepts. |
42 | 42 |
template<typename K, typename V> |
43 | 43 |
class MapBase { |
44 | 44 |
public: |
45 | 45 |
/// \brief The key type of the map. |
46 | 46 |
typedef K Key; |
47 | 47 |
/// \brief The value type of the map. |
48 | 48 |
/// (The type of objects associated with the keys). |
49 | 49 |
typedef V Value; |
50 | 50 |
}; |
51 | 51 |
|
52 | 52 |
|
53 | 53 |
/// Null map. (a.k.a. DoNothingMap) |
54 | 54 |
|
55 | 55 |
/// This map can be used if you have to provide a map only for |
56 | 56 |
/// its type definitions, or if you have to provide a writable map, |
57 | 57 |
/// but data written to it is not required (i.e. it will be sent to |
58 | 58 |
/// <tt>/dev/null</tt>). |
59 | 59 |
/// It conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
60 | 60 |
/// |
61 | 61 |
/// \sa ConstMap |
62 | 62 |
template<typename K, typename V> |
63 | 63 |
class NullMap : public MapBase<K, V> { |
64 | 64 |
public: |
65 | 65 |
///\e |
66 | 66 |
typedef K Key; |
67 | 67 |
///\e |
68 | 68 |
typedef V Value; |
69 | 69 |
|
70 | 70 |
/// Gives back a default constructed element. |
71 | 71 |
Value operator[](const Key&) const { return Value(); } |
72 | 72 |
/// Absorbs the value. |
73 | 73 |
void set(const Key&, const Value&) {} |
74 | 74 |
}; |
75 | 75 |
|
76 | 76 |
/// Returns a \c NullMap class |
77 | 77 |
|
78 | 78 |
/// This function just returns a \c NullMap class. |
79 | 79 |
/// \relates NullMap |
80 | 80 |
template <typename K, typename V> |
81 | 81 |
NullMap<K, V> nullMap() { |
82 | 82 |
return NullMap<K, V>(); |
83 | 83 |
} |
84 | 84 |
|
85 | 85 |
|
86 | 86 |
/// Constant map. |
87 | 87 |
|
88 | 88 |
/// This \ref concepts::ReadMap "readable map" assigns a specified |
89 | 89 |
/// value to each key. |
90 | 90 |
/// |
91 | 91 |
/// In other aspects it is equivalent to \c NullMap. |
92 | 92 |
/// So it conforms to the \ref concepts::ReadWriteMap "ReadWriteMap" |
93 | 93 |
/// concept, but it absorbs the data written to it. |
94 | 94 |
/// |
95 | 95 |
/// The simplest way of using this map is through the constMap() |
96 | 96 |
/// function. |
97 | 97 |
/// |
98 | 98 |
/// \sa NullMap |
99 | 99 |
/// \sa IdentityMap |
100 | 100 |
template<typename K, typename V> |
101 | 101 |
class ConstMap : public MapBase<K, V> { |
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> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/fractional_matching.h> |
32 | 32 |
|
33 | 33 |
///\ingroup matching |
34 | 34 |
///\file |
35 | 35 |
///\brief Maximum matching algorithms in general graphs. |
36 | 36 |
|
37 | 37 |
namespace lemon { |
38 | 38 |
|
39 | 39 |
/// \ingroup matching |
40 | 40 |
/// |
41 | 41 |
/// \brief Maximum cardinality matching in general graphs |
42 | 42 |
/// |
43 | 43 |
/// This class implements Edmonds' alternating forest matching algorithm |
44 | 44 |
/// for finding a maximum cardinality matching in a general undirected graph. |
45 | 45 |
/// It can be started from an arbitrary initial matching |
46 | 46 |
/// (the default is the empty one). |
47 | 47 |
/// |
48 | 48 |
/// The dual solution of the problem is a map of the nodes to |
49 | 49 |
/// \ref MaxMatching::Status "Status", having values \c EVEN (or \c D), |
50 | 50 |
/// \c ODD (or \c A) and \c MATCHED (or \c C) defining the Gallai-Edmonds |
51 | 51 |
/// decomposition of the graph. The nodes in \c EVEN/D induce a subgraph |
52 | 52 |
/// with factor-critical components, the nodes in \c ODD/A form the |
53 | 53 |
/// canonical barrier, and the nodes in \c MATCHED/C induce a graph having |
54 | 54 |
/// a perfect matching. The number of the factor-critical components |
55 | 55 |
/// minus the number of barrier nodes is a lower bound on the |
56 | 56 |
/// unmatched nodes, and the matching is optimal if and only if this bound is |
57 | 57 |
/// tight. This decomposition can be obtained using \ref status() or |
58 | 58 |
/// \ref statusMap() after running the algorithm. |
59 | 59 |
/// |
60 | 60 |
/// \tparam GR The undirected graph type the algorithm runs on. |
61 | 61 |
template <typename GR> |
62 | 62 |
class MaxMatching { |
63 | 63 |
public: |
64 | 64 |
|
65 | 65 |
/// The graph type of the algorithm |
66 | 66 |
typedef GR Graph; |
67 | 67 |
/// The type of the matching map |
68 | 68 |
typedef typename Graph::template NodeMap<typename Graph::Arc> |
69 | 69 |
MatchingMap; |
70 | 70 |
|
71 | 71 |
///\brief Status constants for Gallai-Edmonds decomposition. |
72 | 72 |
/// |
73 | 73 |
///These constants are used for indicating the Gallai-Edmonds |
74 | 74 |
///decomposition of a graph. The nodes with status \c EVEN (or \c D) |
75 | 75 |
///induce a subgraph with factor-critical components, the nodes with |
76 | 76 |
///status \c ODD (or \c A) form the canonical barrier, and the nodes |
77 | 77 |
///with status \c MATCHED (or \c C) induce a subgraph having a |
78 | 78 |
///perfect matching. |
79 | 79 |
enum Status { |
80 | 80 |
EVEN = 1, ///< = 1. (\c D is an alias for \c EVEN.) |
81 | 81 |
D = 1, |
82 | 82 |
MATCHED = 0, ///< = 0. (\c C is an alias for \c MATCHED.) |
83 | 83 |
C = 0, |
84 | 84 |
ODD = -1, ///< = -1. (\c A is an alias for \c ODD.) |
85 | 85 |
A = -1, |
86 | 86 |
UNMATCHED = -2 ///< = -2. |
87 | 87 |
}; |
88 | 88 |
|
89 | 89 |
/// The type of the status map |
90 | 90 |
typedef typename Graph::template NodeMap<Status> StatusMap; |
91 | 91 |
|
92 | 92 |
private: |
93 | 93 |
|
94 | 94 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
95 | 95 |
|
96 | 96 |
typedef UnionFindEnum<IntNodeMap> BlossomSet; |
97 | 97 |
typedef ExtendFindEnum<IntNodeMap> TreeSet; |
98 | 98 |
typedef RangeMap<Node> NodeIntMap; |
99 | 99 |
typedef MatchingMap EarMap; |
100 | 100 |
typedef std::vector<Node> NodeQueue; |
101 | 101 |
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 |
|
30 | 30 |
#include<cmath> |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
/// \addtogroup misc |
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> |
30 | 30 |
#include <lemon/assert.h> |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
|
35 | 35 |
/// \brief Default traits class for MinCostArborescence class. |
36 | 36 |
/// |
37 | 37 |
/// Default traits class for MinCostArborescence class. |
38 | 38 |
/// \param GR Digraph type. |
39 | 39 |
/// \param CM Type of the cost map. |
40 | 40 |
template <class GR, class CM> |
41 | 41 |
struct MinCostArborescenceDefaultTraits{ |
42 | 42 |
|
43 | 43 |
/// \brief The digraph type the algorithm runs on. |
44 | 44 |
typedef GR Digraph; |
45 | 45 |
|
46 | 46 |
/// \brief The type of the map that stores the arc costs. |
47 | 47 |
/// |
48 | 48 |
/// The type of the map that stores the arc costs. |
49 | 49 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
50 | 50 |
typedef CM CostMap; |
51 | 51 |
|
52 | 52 |
/// \brief The value type of the costs. |
53 | 53 |
/// |
54 | 54 |
/// The value type of the costs. |
55 | 55 |
typedef typename CostMap::Value Value; |
56 | 56 |
|
57 | 57 |
/// \brief The type of the map that stores which arcs are in the |
58 | 58 |
/// arborescence. |
59 | 59 |
/// |
60 | 60 |
/// The type of the map that stores which arcs are in the |
61 | 61 |
/// arborescence. It must conform to the \ref concepts::WriteMap |
62 | 62 |
/// "WriteMap" concept, and its value type must be \c bool |
63 | 63 |
/// (or convertible). Initially it will be set to \c false on each |
64 | 64 |
/// arc, then it will be set on each arborescence arc once. |
65 | 65 |
typedef typename Digraph::template ArcMap<bool> ArborescenceMap; |
66 | 66 |
|
67 | 67 |
/// \brief Instantiates a \c ArborescenceMap. |
68 | 68 |
/// |
69 | 69 |
/// This function instantiates a \c ArborescenceMap. |
70 | 70 |
/// \param digraph The digraph to which we would like to calculate |
71 | 71 |
/// the \c ArborescenceMap. |
72 | 72 |
static ArborescenceMap *createArborescenceMap(const Digraph &digraph){ |
73 | 73 |
return new ArborescenceMap(digraph); |
74 | 74 |
} |
75 | 75 |
|
76 | 76 |
/// \brief The type of the \c PredMap |
77 | 77 |
/// |
78 | 78 |
/// The type of the \c PredMap. It must confrom to the |
79 | 79 |
/// \ref concepts::WriteMap "WriteMap" concept, and its value type |
80 | 80 |
/// must be the \c Arc type of the digraph. |
81 | 81 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
82 | 82 |
|
83 | 83 |
/// \brief Instantiates a \c PredMap. |
84 | 84 |
/// |
85 | 85 |
/// This function instantiates a \c PredMap. |
86 | 86 |
/// \param digraph The digraph to which we would like to define the |
87 | 87 |
/// \c PredMap. |
88 | 88 |
static PredMap *createPredMap(const Digraph &digraph){ |
89 | 89 |
return new PredMap(digraph); |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
}; |
93 | 93 |
|
94 | 94 |
/// \ingroup spantree |
95 | 95 |
/// |
96 | 96 |
/// \brief Minimum Cost Arborescence algorithm class. |
97 | 97 |
/// |
98 | 98 |
/// This class provides an efficient implementation of the |
99 | 99 |
/// Minimum Cost Arborescence algorithm. The arborescence is a tree |
100 | 100 |
/// which is directed from a given source node of the digraph. One or |
101 | 101 |
/// more sources should be given to the algorithm and it will calculate |
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> |
30 | 30 |
|
31 | 31 |
#include <lemon/core.h> |
32 | 32 |
#include <lemon/math.h> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \addtogroup min_cost_flow_algs |
37 | 37 |
/// @{ |
38 | 38 |
|
39 | 39 |
/// \brief Implementation of the primal Network Simplex algorithm |
40 | 40 |
/// for finding a \ref min_cost_flow "minimum cost flow". |
41 | 41 |
/// |
42 | 42 |
/// \ref NetworkSimplex implements the primal Network Simplex algorithm |
43 | 43 |
/// for finding a \ref min_cost_flow "minimum cost flow" |
44 | 44 |
/// \ref amo93networkflows, \ref dantzig63linearprog, |
45 | 45 |
/// \ref kellyoneill91netsimplex. |
46 | 46 |
/// This algorithm is a highly efficient specialized version of the |
47 | 47 |
/// linear programming simplex method directly for the minimum cost |
48 | 48 |
/// flow problem. |
49 | 49 |
/// |
50 | 50 |
/// In general, %NetworkSimplex is the fastest implementation available |
51 | 51 |
/// in LEMON for this problem. |
52 | 52 |
/// Moreover, it supports both directions of the supply/demand inequality |
53 | 53 |
/// constraints. For more information, see \ref SupplyType. |
54 | 54 |
/// |
55 | 55 |
/// Most of the parameters of the problem (except for the digraph) |
56 | 56 |
/// can be given using separate functions, and the algorithm can be |
57 | 57 |
/// executed using the \ref run() function. If some parameters are not |
58 | 58 |
/// specified, then default values will be used. |
59 | 59 |
/// |
60 | 60 |
/// \tparam GR The digraph type the algorithm runs on. |
61 | 61 |
/// \tparam V The number type used for flow amounts, capacity bounds |
62 | 62 |
/// and supply values in the algorithm. By default, it is \c int. |
63 | 63 |
/// \tparam C The number type used for costs and potentials in the |
64 | 64 |
/// algorithm. By default, it is the same as \c V. |
65 | 65 |
/// |
66 | 66 |
/// \warning Both number types must be signed and all input data must |
67 | 67 |
/// be integer. |
68 | 68 |
/// |
69 | 69 |
/// \note %NetworkSimplex provides five different pivot rule |
70 | 70 |
/// implementations, from which the most efficient one is used |
71 | 71 |
/// by default. For more information, see \ref PivotRule. |
72 | 72 |
template <typename GR, typename V = int, typename C = V> |
73 | 73 |
class NetworkSimplex |
74 | 74 |
{ |
75 | 75 |
public: |
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. |
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 |
|
30 | 30 |
#include <lemon/error.h> |
31 | 31 |
#include <lemon/core.h> |
32 | 32 |
#include <lemon/concepts/path.h> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \addtogroup paths |
37 | 37 |
/// @{ |
38 | 38 |
|
39 | 39 |
|
40 | 40 |
/// \brief A structure for representing directed paths in a digraph. |
41 | 41 |
/// |
42 | 42 |
/// A structure for representing directed path in a digraph. |
43 | 43 |
/// \tparam GR The digraph type in which the path is. |
44 | 44 |
/// |
45 | 45 |
/// In a sense, the path can be treated as a list of arcs. The |
46 | 46 |
/// lemon path type stores just this list. As a consequence, it |
47 | 47 |
/// cannot enumerate the nodes of the path and the source node of |
48 | 48 |
/// a zero length path is undefined. |
49 | 49 |
/// |
50 | 50 |
/// This implementation is a back and front insertable and erasable |
51 | 51 |
/// path type. It can be indexed in O(1) time. The front and back |
52 | 52 |
/// insertion and erase is done in O(1) (amortized) time. The |
53 | 53 |
/// implementation uses two vectors for storing the front and back |
54 | 54 |
/// insertions. |
55 | 55 |
template <typename GR> |
56 | 56 |
class Path { |
57 | 57 |
public: |
58 | 58 |
|
59 | 59 |
typedef GR Digraph; |
60 | 60 |
typedef typename Digraph::Arc Arc; |
61 | 61 |
|
62 | 62 |
/// \brief Default constructor |
63 | 63 |
/// |
64 | 64 |
/// Default constructor |
65 | 65 |
Path() {} |
66 | 66 |
|
67 | 67 |
/// \brief Template copy constructor |
68 | 68 |
/// |
69 | 69 |
/// This constuctor initializes the path from any other path type. |
70 | 70 |
/// It simply makes a copy of the given path. |
71 | 71 |
template <typename CPath> |
72 | 72 |
Path(const CPath& cpath) { |
73 | 73 |
pathCopy(cpath, *this); |
74 | 74 |
} |
75 | 75 |
|
76 | 76 |
/// \brief Template copy assignment |
77 | 77 |
/// |
78 | 78 |
/// This operator makes a copy of a path of any other type. |
79 | 79 |
template <typename CPath> |
80 | 80 |
Path& operator=(const CPath& cpath) { |
81 | 81 |
pathCopy(cpath, *this); |
82 | 82 |
return *this; |
83 | 83 |
} |
84 | 84 |
|
85 | 85 |
/// \brief LEMON style iterator for path arcs |
86 | 86 |
/// |
87 | 87 |
/// This class is used to iterate on the arcs of the paths. |
88 | 88 |
class ArcIt { |
89 | 89 |
friend class Path; |
90 | 90 |
public: |
91 | 91 |
/// \brief Default constructor |
92 | 92 |
ArcIt() {} |
93 | 93 |
/// \brief Invalid constructor |
94 | 94 |
ArcIt(Invalid) : path(0), idx(-1) {} |
95 | 95 |
/// \brief Initializate the iterator to the first arc of path |
96 | 96 |
ArcIt(const Path &_path) |
97 | 97 |
: path(&_path), idx(_path.empty() ? -1 : 0) {} |
98 | 98 |
|
99 | 99 |
private: |
100 | 100 |
|
101 | 101 |
ArcIt(const Path &_path, int _idx) |
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> |
30 | 30 |
#include <lemon/bfs.h> |
31 | 31 |
#include <lemon/radix_sort.h> |
32 | 32 |
#include <lemon/maps.h> |
33 | 33 |
#include <lemon/path.h> |
34 | 34 |
#include <lemon/bucket_heap.h> |
35 | 35 |
#include <lemon/adaptors.h> |
36 | 36 |
#include <lemon/edge_set.h> |
37 | 37 |
#include <lemon/color.h> |
38 | 38 |
#include <lemon/dim2.h> |
39 | 39 |
|
40 | 40 |
namespace lemon { |
41 | 41 |
|
42 | 42 |
namespace _planarity_bits { |
43 | 43 |
|
44 | 44 |
template <typename Graph> |
45 | 45 |
struct PlanarityVisitor : DfsVisitor<Graph> { |
46 | 46 |
|
47 | 47 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
48 | 48 |
|
49 | 49 |
typedef typename Graph::template NodeMap<Arc> PredMap; |
50 | 50 |
|
51 | 51 |
typedef typename Graph::template EdgeMap<bool> TreeMap; |
52 | 52 |
|
53 | 53 |
typedef typename Graph::template NodeMap<int> OrderMap; |
54 | 54 |
typedef std::vector<Node> OrderList; |
55 | 55 |
|
56 | 56 |
typedef typename Graph::template NodeMap<int> LowMap; |
57 | 57 |
typedef typename Graph::template NodeMap<int> AncestorMap; |
58 | 58 |
|
59 | 59 |
PlanarityVisitor(const Graph& graph, |
60 | 60 |
PredMap& pred_map, TreeMap& tree_map, |
61 | 61 |
OrderMap& order_map, OrderList& order_list, |
62 | 62 |
AncestorMap& ancestor_map, LowMap& low_map) |
63 | 63 |
: _graph(graph), _pred_map(pred_map), _tree_map(tree_map), |
64 | 64 |
_order_map(order_map), _order_list(order_list), |
65 | 65 |
_ancestor_map(ancestor_map), _low_map(low_map) {} |
66 | 66 |
|
67 | 67 |
void reach(const Node& node) { |
68 | 68 |
_order_map[node] = _order_list.size(); |
69 | 69 |
_low_map[node] = _order_list.size(); |
70 | 70 |
_ancestor_map[node] = _order_list.size(); |
71 | 71 |
_order_list.push_back(node); |
72 | 72 |
} |
73 | 73 |
|
74 | 74 |
void discover(const Arc& arc) { |
75 | 75 |
Node source = _graph.source(arc); |
76 | 76 |
Node target = _graph.target(arc); |
77 | 77 |
|
78 | 78 |
_tree_map[arc] = true; |
79 | 79 |
_pred_map[target] = arc; |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
void examine(const Arc& arc) { |
83 | 83 |
Node source = _graph.source(arc); |
84 | 84 |
Node target = _graph.target(arc); |
85 | 85 |
|
86 | 86 |
if (_order_map[target] < _order_map[source] && !_tree_map[arc]) { |
87 | 87 |
if (_low_map[source] > _order_map[target]) { |
88 | 88 |
_low_map[source] = _order_map[target]; |
89 | 89 |
} |
90 | 90 |
if (_ancestor_map[source] > _order_map[target]) { |
91 | 91 |
_ancestor_map[source] = _order_map[target]; |
92 | 92 |
} |
93 | 93 |
} |
94 | 94 |
} |
95 | 95 |
|
96 | 96 |
void backtrack(const Arc& arc) { |
97 | 97 |
Node source = _graph.source(arc); |
98 | 98 |
Node target = _graph.target(arc); |
99 | 99 |
|
100 | 100 |
if (_low_map[source] > _low_map[target]) { |
101 | 101 |
_low_map[source] = _low_map[target]; |
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 { |
30 | 30 |
|
31 | 31 |
/// \brief Default traits class of Preflow class. |
32 | 32 |
/// |
33 | 33 |
/// Default traits class of Preflow class. |
34 | 34 |
/// \tparam GR Digraph type. |
35 | 35 |
/// \tparam CAP Capacity map type. |
36 | 36 |
template <typename GR, typename CAP> |
37 | 37 |
struct PreflowDefaultTraits { |
38 | 38 |
|
39 | 39 |
/// \brief The type of the digraph the algorithm runs on. |
40 | 40 |
typedef GR Digraph; |
41 | 41 |
|
42 | 42 |
/// \brief The type of the map that stores the arc capacities. |
43 | 43 |
/// |
44 | 44 |
/// The type of the map that stores the arc capacities. |
45 | 45 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
46 | 46 |
typedef CAP CapacityMap; |
47 | 47 |
|
48 | 48 |
/// \brief The type of the flow values. |
49 | 49 |
typedef typename CapacityMap::Value Value; |
50 | 50 |
|
51 | 51 |
/// \brief The type of the map that stores the flow values. |
52 | 52 |
/// |
53 | 53 |
/// The type of the map that stores the flow values. |
54 | 54 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
55 | 55 |
#ifdef DOXYGEN |
56 | 56 |
typedef GR::ArcMap<Value> FlowMap; |
57 | 57 |
#else |
58 | 58 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
59 | 59 |
#endif |
60 | 60 |
|
61 | 61 |
/// \brief Instantiates a FlowMap. |
62 | 62 |
/// |
63 | 63 |
/// This function instantiates a \ref FlowMap. |
64 | 64 |
/// \param digraph The digraph for which we would like to define |
65 | 65 |
/// the flow map. |
66 | 66 |
static FlowMap* createFlowMap(const Digraph& digraph) { |
67 | 67 |
return new FlowMap(digraph); |
68 | 68 |
} |
69 | 69 |
|
70 | 70 |
/// \brief The elevator type used by Preflow algorithm. |
71 | 71 |
/// |
72 | 72 |
/// The elevator type used by Preflow algorithm. |
73 | 73 |
/// |
74 | 74 |
/// \sa Elevator, LinkedElevator |
75 | 75 |
#ifdef DOXYGEN |
76 | 76 |
typedef lemon::Elevator<GR, GR::Node> Elevator; |
77 | 77 |
#else |
78 | 78 |
typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator; |
79 | 79 |
#endif |
80 | 80 |
|
81 | 81 |
/// \brief Instantiates an Elevator. |
82 | 82 |
/// |
83 | 83 |
/// This function instantiates an \ref Elevator. |
84 | 84 |
/// \param digraph The digraph for which we would like to define |
85 | 85 |
/// the elevator. |
86 | 86 |
/// \param max_level The maximum level of the elevator. |
87 | 87 |
static Elevator* createElevator(const Digraph& digraph, int max_level) { |
88 | 88 |
return new Elevator(digraph, max_level); |
89 | 89 |
} |
90 | 90 |
|
91 | 91 |
/// \brief The tolerance used by the algorithm |
92 | 92 |
/// |
93 | 93 |
/// The tolerance used by the algorithm to handle inexact computation. |
94 | 94 |
typedef lemon::Tolerance<Value> Tolerance; |
95 | 95 |
|
96 | 96 |
}; |
97 | 97 |
|
98 | 98 |
|
99 | 99 |
/// \ingroup max_flow |
100 | 100 |
/// |
101 | 101 |
/// \brief %Preflow algorithm class. |
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> |
30 | 30 |
#include <lemon/bits/graph_extender.h> |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
class SmartDigraph; |
35 | 35 |
|
36 | 36 |
class SmartDigraphBase { |
37 | 37 |
protected: |
38 | 38 |
|
39 | 39 |
struct NodeT |
40 | 40 |
{ |
41 | 41 |
int first_in, first_out; |
42 | 42 |
NodeT() {} |
43 | 43 |
}; |
44 | 44 |
struct ArcT |
45 | 45 |
{ |
46 | 46 |
int target, source, next_in, next_out; |
47 | 47 |
ArcT() {} |
48 | 48 |
}; |
49 | 49 |
|
50 | 50 |
std::vector<NodeT> nodes; |
51 | 51 |
std::vector<ArcT> arcs; |
52 | 52 |
|
53 | 53 |
public: |
54 | 54 |
|
55 | 55 |
typedef SmartDigraphBase Digraph; |
56 | 56 |
|
57 | 57 |
class Node; |
58 | 58 |
class Arc; |
59 | 59 |
|
60 | 60 |
public: |
61 | 61 |
|
62 | 62 |
SmartDigraphBase() : nodes(), arcs() { } |
63 | 63 |
SmartDigraphBase(const SmartDigraphBase &_g) |
64 | 64 |
: nodes(_g.nodes), arcs(_g.arcs) { } |
65 | 65 |
|
66 | 66 |
typedef True NodeNumTag; |
67 | 67 |
typedef True ArcNumTag; |
68 | 68 |
|
69 | 69 |
int nodeNum() const { return nodes.size(); } |
70 | 70 |
int arcNum() const { return arcs.size(); } |
71 | 71 |
|
72 | 72 |
int maxNodeId() const { return nodes.size()-1; } |
73 | 73 |
int maxArcId() const { return arcs.size()-1; } |
74 | 74 |
|
75 | 75 |
Node addNode() { |
76 | 76 |
int n = nodes.size(); |
77 | 77 |
nodes.push_back(NodeT()); |
78 | 78 |
nodes[n].first_in = -1; |
79 | 79 |
nodes[n].first_out = -1; |
80 | 80 |
return Node(n); |
81 | 81 |
} |
82 | 82 |
|
83 | 83 |
Arc addArc(Node u, Node v) { |
84 | 84 |
int n = arcs.size(); |
85 | 85 |
arcs.push_back(ArcT()); |
86 | 86 |
arcs[n].source = u._id; |
87 | 87 |
arcs[n].target = v._id; |
88 | 88 |
arcs[n].next_out = nodes[u._id].first_out; |
89 | 89 |
arcs[n].next_in = nodes[v._id].first_in; |
90 | 90 |
nodes[u._id].first_out = nodes[v._id].first_in = n; |
91 | 91 |
|
92 | 92 |
return Arc(n); |
93 | 93 |
} |
94 | 94 |
|
95 | 95 |
void clear() { |
96 | 96 |
arcs.clear(); |
97 | 97 |
nodes.clear(); |
98 | 98 |
} |
99 | 99 |
|
100 | 100 |
Node source(Arc a) const { return Node(arcs[a._id].source); } |
101 | 101 |
Node target(Arc a) const { return Node(arcs[a._id].target); } |
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 |
|
30 | 30 |
SoplexLp::SoplexLp() { |
31 | 31 |
soplex = new soplex::SoPlex; |
32 | 32 |
messageLevel(MESSAGE_NOTHING); |
33 | 33 |
} |
34 | 34 |
|
35 | 35 |
SoplexLp::~SoplexLp() { |
36 | 36 |
delete soplex; |
37 | 37 |
} |
38 | 38 |
|
39 | 39 |
SoplexLp::SoplexLp(const SoplexLp& lp) { |
40 | 40 |
rows = lp.rows; |
41 | 41 |
cols = lp.cols; |
42 | 42 |
|
43 | 43 |
soplex = new soplex::SoPlex; |
44 | 44 |
(*static_cast<soplex::SPxLP*>(soplex)) = *(lp.soplex); |
45 | 45 |
|
46 | 46 |
_col_names = lp._col_names; |
47 | 47 |
_col_names_ref = lp._col_names_ref; |
48 | 48 |
|
49 | 49 |
_row_names = lp._row_names; |
50 | 50 |
_row_names_ref = lp._row_names_ref; |
51 | 51 |
|
52 | 52 |
messageLevel(MESSAGE_NOTHING); |
53 | 53 |
} |
54 | 54 |
|
55 | 55 |
void SoplexLp::_clear_temporals() { |
56 | 56 |
_primal_values.clear(); |
57 | 57 |
_dual_values.clear(); |
58 | 58 |
} |
59 | 59 |
|
60 | 60 |
SoplexLp* SoplexLp::newSolver() const { |
61 | 61 |
SoplexLp* newlp = new SoplexLp(); |
62 | 62 |
return newlp; |
63 | 63 |
} |
64 | 64 |
|
65 | 65 |
SoplexLp* SoplexLp::cloneSolver() const { |
66 | 66 |
SoplexLp* newlp = new SoplexLp(*this); |
67 | 67 |
return newlp; |
68 | 68 |
} |
69 | 69 |
|
70 | 70 |
const char* SoplexLp::_solverName() const { return "SoplexLp"; } |
71 | 71 |
|
72 | 72 |
int SoplexLp::_addCol() { |
73 | 73 |
soplex::LPCol c; |
74 | 74 |
c.setLower(-soplex::infinity); |
75 | 75 |
c.setUpper(soplex::infinity); |
76 | 76 |
soplex->addCol(c); |
77 | 77 |
|
78 | 78 |
_col_names.push_back(std::string()); |
79 | 79 |
|
80 | 80 |
return soplex->nCols() - 1; |
81 | 81 |
} |
82 | 82 |
|
83 | 83 |
int SoplexLp::_addRow() { |
84 | 84 |
soplex::LPRow r; |
85 | 85 |
r.setLhs(-soplex::infinity); |
86 | 86 |
r.setRhs(soplex::infinity); |
87 | 87 |
soplex->addRow(r); |
88 | 88 |
|
89 | 89 |
_row_names.push_back(std::string()); |
90 | 90 |
|
91 | 91 |
return soplex->nRows() - 1; |
92 | 92 |
} |
93 | 93 |
|
94 | 94 |
int SoplexLp::_addRow(Value l, ExprIterator b, ExprIterator e, Value u) { |
95 | 95 |
soplex::DSVector v; |
96 | 96 |
for (ExprIterator it = b; it != e; ++it) { |
97 | 97 |
v.add(it->first, it->second); |
98 | 98 |
} |
99 | 99 |
soplex::LPRow r(l, v, u); |
100 | 100 |
soplex->addRow(r); |
101 | 101 |
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 |
|
30 | 30 |
// Forward declaration |
31 | 31 |
namespace soplex { |
32 | 32 |
class SoPlex; |
33 | 33 |
} |
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
/// \ingroup lp_group |
38 | 38 |
/// |
39 | 39 |
/// \brief Interface for the SOPLEX solver |
40 | 40 |
/// |
41 | 41 |
/// This class implements an interface for the SoPlex LP solver. |
42 | 42 |
/// The SoPlex library is an object oriented lp solver library |
43 | 43 |
/// developed at the Konrad-Zuse-Zentrum f�r Informationstechnik |
44 | 44 |
/// Berlin (ZIB). You can find detailed information about it at the |
45 | 45 |
/// <tt>http://soplex.zib.de</tt> address. |
46 | 46 |
class SoplexLp : public LpSolver { |
47 | 47 |
private: |
48 | 48 |
|
49 | 49 |
soplex::SoPlex* soplex; |
50 | 50 |
|
51 | 51 |
std::vector<std::string> _col_names; |
52 | 52 |
std::map<std::string, int> _col_names_ref; |
53 | 53 |
|
54 | 54 |
std::vector<std::string> _row_names; |
55 | 55 |
std::map<std::string, int> _row_names_ref; |
56 | 56 |
|
57 | 57 |
private: |
58 | 58 |
|
59 | 59 |
// these values cannot be retrieved element by element |
60 | 60 |
mutable std::vector<Value> _primal_values; |
61 | 61 |
mutable std::vector<Value> _dual_values; |
62 | 62 |
|
63 | 63 |
mutable std::vector<Value> _primal_ray; |
64 | 64 |
mutable std::vector<Value> _dual_ray; |
65 | 65 |
|
66 | 66 |
void _clear_temporals(); |
67 | 67 |
|
68 | 68 |
public: |
69 | 69 |
|
70 | 70 |
/// \e |
71 | 71 |
SoplexLp(); |
72 | 72 |
/// \e |
73 | 73 |
SoplexLp(const SoplexLp&); |
74 | 74 |
/// \e |
75 | 75 |
~SoplexLp(); |
76 | 76 |
/// \e |
77 | 77 |
virtual SoplexLp* newSolver() const; |
78 | 78 |
/// \e |
79 | 79 |
virtual SoplexLp* cloneSolver() const; |
80 | 80 |
|
81 | 81 |
protected: |
82 | 82 |
|
83 | 83 |
virtual const char* _solverName() const; |
84 | 84 |
|
85 | 85 |
virtual int _addCol(); |
86 | 86 |
virtual int _addRow(); |
87 | 87 |
virtual int _addRow(Value l, ExprIterator b, ExprIterator e, Value u); |
88 | 88 |
|
89 | 89 |
virtual void _eraseCol(int i); |
90 | 90 |
virtual void _eraseRow(int i); |
91 | 91 |
|
92 | 92 |
virtual void _eraseColId(int i); |
93 | 93 |
virtual void _eraseRowId(int i); |
94 | 94 |
|
95 | 95 |
virtual void _getColName(int col, std::string& name) const; |
96 | 96 |
virtual void _setColName(int col, const std::string& name); |
97 | 97 |
virtual int _colByName(const std::string& name) const; |
98 | 98 |
|
99 | 99 |
virtual void _getRowName(int row, std::string& name) const; |
100 | 100 |
virtual void _setRowName(int row, const std::string& name); |
101 | 101 |
virtual int _rowByName(const std::string& name) const; |
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 | 34 |
StaticDigraphBase() |
35 | 35 |
: built(false), node_num(0), arc_num(0), |
36 | 36 |
node_first_out(NULL), node_first_in(NULL), |
37 | 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 | 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 | 86 |
void firstOut(Arc& e, const Node& n) const { |
87 | 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; } |
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)