!
!
!
| 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: |
| 102 | 102 |
std::cout << " 1 file argument was given. It is:\n"; |
| 103 | 103 |
break; |
| 104 | 104 |
default: |
| 105 | 105 |
std::cout << " " |
| 106 | 106 |
<< ap.files().size() << " file arguments were given. They are:\n"; |
| 107 | 107 |
} |
| 108 | 108 |
for(unsigned int i=0;i<ap.files().size();++i) |
| 109 | 109 |
std::cout << " '" << ap.files()[i] << "'\n"; |
| 110 | 110 |
|
| 111 | 111 |
return 0; |
| 112 | 112 |
} |
| 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); |
| 102 | 102 |
int result = algorithm(rg); |
| 103 | 103 |
\endcode |
| 104 | 104 |
During running the algorithm, the original digraph \c g is untouched. |
| 105 | 105 |
This techniques give rise to an elegant code, and based on stable |
| 106 | 106 |
graph adaptors, complex algorithms can be implemented easily. |
| 107 | 107 |
|
| 108 | 108 |
In flow, circulation and matching problems, the residual |
| 109 | 109 |
graph is of particular importance. Combining an adaptor implementing |
| 110 | 110 |
this with shortest path algorithms or minimum mean cycle algorithms, |
| 111 | 111 |
a range of weighted and cardinality optimization algorithms can be |
| 112 | 112 |
obtained. For other examples, the interested user is referred to the |
| 113 | 113 |
detailed documentation of particular adaptors. |
| 114 | 114 |
|
| 115 | 115 |
The behavior of graph adaptors can be very different. Some of them keep |
| 116 | 116 |
capabilities of the original graph while in other cases this would be |
| 117 | 117 |
meaningless. This means that the concepts that they meet depend |
| 118 | 118 |
on the graph adaptor, and the wrapped graph. |
| 119 | 119 |
For example, if an arc of a reversed digraph is deleted, this is carried |
| 120 | 120 |
out by deleting the corresponding arc of the original digraph, thus the |
| 121 | 121 |
adaptor modifies the original digraph. |
| 122 | 122 |
However in case of a residual digraph, this operation has no sense. |
| 123 | 123 |
|
| 124 | 124 |
Let us stand one more example here to simplify your work. |
| 125 | 125 |
ReverseDigraph has constructor |
| 126 | 126 |
\code |
| 127 | 127 |
ReverseDigraph(Digraph& digraph); |
| 128 | 128 |
\endcode |
| 129 | 129 |
This means that in a situation, when a <tt>const %ListDigraph&</tt> |
| 130 | 130 |
reference to a graph is given, then it have to be instantiated with |
| 131 | 131 |
<tt>Digraph=const %ListDigraph</tt>. |
| 132 | 132 |
\code |
| 133 | 133 |
int algorithm1(const ListDigraph& 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] |
| 102 | 102 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f] |
| 103 | 103 |
|
| 104 | 104 |
However if the sum of the supply values is zero, then these two problems |
| 105 | 105 |
are equivalent. |
| 106 | 106 |
The \ref min_cost_flow_algs "algorithms" in LEMON support the general |
| 107 | 107 |
form, so if you need the equality form, you have to ensure this additional |
| 108 | 108 |
contraint manually. |
| 109 | 109 |
|
| 110 | 110 |
|
| 111 | 111 |
\section mcf_leq Opposite Inequalites (LEQ Form) |
| 112 | 112 |
|
| 113 | 113 |
Another possible definition of the minimum cost flow problem is |
| 114 | 114 |
when there are <em>"less or equal"</em> (LEQ) supply/demand constraints, |
| 115 | 115 |
instead of the <em>"greater or equal"</em> (GEQ) constraints. |
| 116 | 116 |
|
| 117 | 117 |
\f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f]
|
| 118 | 118 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \leq
|
| 119 | 119 |
sup(u) \quad \forall u\in V \f] |
| 120 | 120 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f] |
| 121 | 121 |
|
| 122 | 122 |
It means that the total demand must be less or equal to the |
| 123 | 123 |
total supply (i.e. \f$\sum_{u\in V} sup(u)\f$ must be zero or
|
| 124 | 124 |
positive) and all the demands have to be satisfied, but there |
| 125 | 125 |
could be supplies that are not carried out from the supply |
| 126 | 126 |
nodes. |
| 127 | 127 |
The equality form is also a special case of this form, of course. |
| 128 | 128 |
|
| 129 | 129 |
You could easily transform this case to the \ref mcf_def "GEQ form" |
| 130 | 130 |
of the problem by reversing the direction of the arcs and taking the |
| 131 | 131 |
negative of the supply values (e.g. using \ref ReverseDigraph and |
| 132 | 132 |
\ref NegMap adaptors). |
| 133 | 133 |
However \ref NetworkSimplex algorithm also supports this form directly |
| 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(); }
|
| 102 | 102 |
int maxArcId() const { return _digraph->maxArcId(); }
|
| 103 | 103 |
|
| 104 | 104 |
typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier; |
| 105 | 105 |
NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); }
|
| 106 | 106 |
|
| 107 | 107 |
typedef typename ItemSetTraits<DGR, Arc>::ItemNotifier ArcNotifier; |
| 108 | 108 |
ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); }
|
| 109 | 109 |
|
| 110 | 110 |
template <typename V> |
| 111 | 111 |
class NodeMap : public DGR::template NodeMap<V> {
|
| 112 | 112 |
typedef typename DGR::template NodeMap<V> Parent; |
| 113 | 113 |
|
| 114 | 114 |
public: |
| 115 | 115 |
explicit NodeMap(const Adaptor& adaptor) |
| 116 | 116 |
: Parent(*adaptor._digraph) {}
|
| 117 | 117 |
NodeMap(const Adaptor& adaptor, const V& value) |
| 118 | 118 |
: Parent(*adaptor._digraph, value) { }
|
| 119 | 119 |
|
| 120 | 120 |
private: |
| 121 | 121 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 122 | 122 |
return operator=<NodeMap>(cmap); |
| 123 | 123 |
} |
| 124 | 124 |
|
| 125 | 125 |
template <typename CMap> |
| 126 | 126 |
NodeMap& operator=(const CMap& cmap) {
|
| 127 | 127 |
Parent::operator=(cmap); |
| 128 | 128 |
return *this; |
| 129 | 129 |
} |
| 130 | 130 |
|
| 131 | 131 |
}; |
| 132 | 132 |
|
| 133 | 133 |
template <typename 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 |
#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 |
{
|
| 102 | 102 |
ParData p; |
| 103 | 103 |
p.bool_p=new bool(value); |
| 104 | 104 |
p.self_delete=true; |
| 105 | 105 |
p.help=help; |
| 106 | 106 |
p.type=BOOL; |
| 107 | 107 |
p.mandatory=obl; |
| 108 | 108 |
_opts[name]=p; |
| 109 | 109 |
return *this; |
| 110 | 110 |
} |
| 111 | 111 |
|
| 112 | 112 |
ArgParser &ArgParser::stringOption(const std::string &name, |
| 113 | 113 |
const std::string &help, |
| 114 | 114 |
std::string value, bool obl) |
| 115 | 115 |
{
|
| 116 | 116 |
ParData p; |
| 117 | 117 |
p.string_p=new std::string(value); |
| 118 | 118 |
p.self_delete=true; |
| 119 | 119 |
p.help=help; |
| 120 | 120 |
p.type=STRING; |
| 121 | 121 |
p.mandatory=obl; |
| 122 | 122 |
_opts[name]=p; |
| 123 | 123 |
return *this; |
| 124 | 124 |
} |
| 125 | 125 |
|
| 126 | 126 |
ArgParser &ArgParser::refOption(const std::string &name, |
| 127 | 127 |
const std::string &help, |
| 128 | 128 |
int &ref, bool obl) |
| 129 | 129 |
{
|
| 130 | 130 |
ParData p; |
| 131 | 131 |
p.int_p=&ref; |
| 132 | 132 |
p.self_delete=false; |
| 133 | 133 |
p.help=help; |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 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; |
| 102 | 102 |
|
| 103 | 103 |
}; |
| 104 | 104 |
std::string help; |
| 105 | 105 |
bool mandatory; |
| 106 | 106 |
OptType type; |
| 107 | 107 |
bool set; |
| 108 | 108 |
bool ingroup; |
| 109 | 109 |
bool has_syn; |
| 110 | 110 |
bool syn; |
| 111 | 111 |
bool self_delete; |
| 112 | 112 |
ParData() : mandatory(false), type(UNKNOWN), set(false), ingroup(false), |
| 113 | 113 |
has_syn(false), syn(false), self_delete(false) {}
|
| 114 | 114 |
}; |
| 115 | 115 |
|
| 116 | 116 |
typedef std::map<std::string,ParData> Opts; |
| 117 | 117 |
Opts _opts; |
| 118 | 118 |
|
| 119 | 119 |
class GroupData |
| 120 | 120 |
{
|
| 121 | 121 |
public: |
| 122 | 122 |
typedef std::list<std::string> Opts; |
| 123 | 123 |
Opts opts; |
| 124 | 124 |
bool only_one; |
| 125 | 125 |
bool mandatory; |
| 126 | 126 |
GroupData() :only_one(false), mandatory(false) {}
|
| 127 | 127 |
}; |
| 128 | 128 |
|
| 129 | 129 |
typedef std::map<std::string,GroupData> Groups; |
| 130 | 130 |
Groups _groups; |
| 131 | 131 |
|
| 132 | 132 |
struct OtherArg |
| 133 | 133 |
{
|
| 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. |
| 102 | 102 |
/// By default, it is the epsilon value used by \ref Tolerance |
| 103 | 103 |
/// "Tolerance<V>". |
| 104 | 104 |
/// |
| 105 | 105 |
/// \see BellmanFordDefaultOperationTraits |
| 106 | 106 |
#ifdef DOXYGEN |
| 107 | 107 |
template <typename V, V eps> |
| 108 | 108 |
#else |
| 109 | 109 |
template < |
| 110 | 110 |
typename V, |
| 111 | 111 |
V eps = Tolerance<V>::def_epsilon> |
| 112 | 112 |
#endif |
| 113 | 113 |
struct BellmanFordToleranceOperationTraits {
|
| 114 | 114 |
/// \brief Value type for the algorithm. |
| 115 | 115 |
typedef V Value; |
| 116 | 116 |
/// \brief Gives back the zero value of the type. |
| 117 | 117 |
static Value zero() {
|
| 118 | 118 |
return static_cast<Value>(0); |
| 119 | 119 |
} |
| 120 | 120 |
/// \brief Gives back the positive infinity value of the type. |
| 121 | 121 |
static Value infinity() {
|
| 122 | 122 |
return std::numeric_limits<Value>::infinity(); |
| 123 | 123 |
} |
| 124 | 124 |
/// \brief Gives back the sum of the given two elements. |
| 125 | 125 |
static Value plus(const Value& left, const Value& right) {
|
| 126 | 126 |
return left + right; |
| 127 | 127 |
} |
| 128 | 128 |
/// \brief Gives back \c true only if the first value is less than |
| 129 | 129 |
/// the second. |
| 130 | 130 |
static bool less(const Value& left, const Value& right) {
|
| 131 | 131 |
return left + eps < right; |
| 132 | 132 |
} |
| 133 | 133 |
}; |
| 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) |
| 371 | 373 |
{
|
| 372 | 374 |
if(local_reached) {
|
| 373 | 375 |
delete _reached; |
| 374 | 376 |
local_reached=false; |
| 375 | 377 |
} |
| 376 | 378 |
_reached = &m; |
| 377 | 379 |
return *this; |
| 378 | 380 |
} |
| 379 | 381 |
|
| 380 | 382 |
///Sets the map that indicates which nodes are processed. |
| 381 | 383 |
|
| 382 | 384 |
///Sets the map that indicates which nodes are processed. |
| 383 | 385 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 384 | 386 |
///or \ref init(), an instance will be allocated automatically. |
| 385 | 387 |
///The destructor deallocates this automatically allocated map, |
| 386 | 388 |
///of course. |
| 387 | 389 |
///\return <tt> (*this) </tt> |
| 388 | 390 |
Bfs &processedMap(ProcessedMap &m) |
| 389 | 391 |
{
|
| 390 | 392 |
if(local_processed) {
|
| 391 | 393 |
delete _processed; |
| 392 | 394 |
local_processed=false; |
| 393 | 395 |
} |
| 394 | 396 |
_processed = &m; |
| 395 | 397 |
return *this; |
| 396 | 398 |
} |
| 397 | 399 |
|
| 398 | 400 |
///Sets the map that stores the distances of the nodes. |
| 399 | 401 |
|
| 400 | 402 |
///Sets the map that stores the distances of the nodes calculated by |
| 401 | 403 |
///the algorithm. |
| 402 | 404 |
///If you don't use this function before calling \ref run(Node) "run()" |
| ... | ... |
@@ -747,257 +749,258 @@ |
| 747 | 749 |
/// |
| 748 | 750 |
///\pre Either \ref run(Node) "run()" or \ref init() |
| 749 | 751 |
///must be called before using this function. |
| 750 | 752 |
Path path(Node t) const { return Path(*G, *_pred, t); }
|
| 751 | 753 |
|
| 752 | 754 |
///The distance of the given node from the root(s). |
| 753 | 755 |
|
| 754 | 756 |
///Returns the distance of the given node from the root(s). |
| 755 | 757 |
/// |
| 756 | 758 |
///\warning If node \c v is not reached from the root(s), then |
| 757 | 759 |
///the return value of this function is undefined. |
| 758 | 760 |
/// |
| 759 | 761 |
///\pre Either \ref run(Node) "run()" or \ref init() |
| 760 | 762 |
///must be called before using this function. |
| 761 | 763 |
int dist(Node v) const { return (*_dist)[v]; }
|
| 762 | 764 |
|
| 763 | 765 |
///\brief Returns the 'previous arc' of the shortest path tree for |
| 764 | 766 |
///the given node. |
| 765 | 767 |
/// |
| 766 | 768 |
///This function returns the 'previous arc' of the shortest path |
| 767 | 769 |
///tree for the node \c v, i.e. it returns the last arc of a |
| 768 | 770 |
///shortest path from a root to \c v. It is \c INVALID if \c v |
| 769 | 771 |
///is not reached from the root(s) or if \c v is a root. |
| 770 | 772 |
/// |
| 771 | 773 |
///The shortest path tree used here is equal to the shortest path |
| 772 | 774 |
///tree used in \ref predNode() and \ref predMap(). |
| 773 | 775 |
/// |
| 774 | 776 |
///\pre Either \ref run(Node) "run()" or \ref init() |
| 775 | 777 |
///must be called before using this function. |
| 776 | 778 |
Arc predArc(Node v) const { return (*_pred)[v];}
|
| 777 | 779 |
|
| 778 | 780 |
///\brief Returns the 'previous node' of the shortest path tree for |
| 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; |
| 972 | 975 |
|
| 973 | 976 |
typedef typename TR::Digraph Digraph; |
| 974 | 977 |
|
| 975 | 978 |
typedef typename Digraph::Node Node; |
| 976 | 979 |
typedef typename Digraph::NodeIt NodeIt; |
| 977 | 980 |
typedef typename Digraph::Arc Arc; |
| 978 | 981 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 979 | 982 |
|
| 980 | 983 |
typedef typename TR::PredMap PredMap; |
| 981 | 984 |
typedef typename TR::DistMap DistMap; |
| 982 | 985 |
typedef typename TR::ReachedMap ReachedMap; |
| 983 | 986 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 984 | 987 |
typedef typename TR::Path Path; |
| 985 | 988 |
|
| 986 | 989 |
public: |
| 987 | 990 |
|
| 988 | 991 |
/// Constructor. |
| 989 | 992 |
BfsWizard() : TR() {}
|
| 990 | 993 |
|
| 991 | 994 |
/// Constructor that requires parameters. |
| 992 | 995 |
|
| 993 | 996 |
/// Constructor that requires parameters. |
| 994 | 997 |
/// These parameters will be the default values for the traits class. |
| 995 | 998 |
/// \param g The digraph the algorithm runs on. |
| 996 | 999 |
BfsWizard(const Digraph &g) : |
| 997 | 1000 |
TR(g) {}
|
| 998 | 1001 |
|
| 999 | 1002 |
///Copy constructor |
| 1000 | 1003 |
BfsWizard(const TR &b) : TR(b) {}
|
| 1001 | 1004 |
|
| 1002 | 1005 |
~BfsWizard() {}
|
| 1003 | 1006 |
|
| ... | ... |
@@ -1140,257 +1143,258 @@ |
| 1140 | 1143 |
SetPathBase(const TR &b) : TR(b) {}
|
| 1141 | 1144 |
}; |
| 1142 | 1145 |
///\brief \ref named-func-param "Named parameter" |
| 1143 | 1146 |
///for getting the shortest path to the target node. |
| 1144 | 1147 |
/// |
| 1145 | 1148 |
///\ref named-func-param "Named parameter" |
| 1146 | 1149 |
///for getting the shortest path to the target node. |
| 1147 | 1150 |
template<class T> |
| 1148 | 1151 |
BfsWizard<SetPathBase<T> > path(const T &t) |
| 1149 | 1152 |
{
|
| 1150 | 1153 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1151 | 1154 |
return BfsWizard<SetPathBase<T> >(*this); |
| 1152 | 1155 |
} |
| 1153 | 1156 |
|
| 1154 | 1157 |
///\brief \ref named-func-param "Named parameter" |
| 1155 | 1158 |
///for getting the distance of the target node. |
| 1156 | 1159 |
/// |
| 1157 | 1160 |
///\ref named-func-param "Named parameter" |
| 1158 | 1161 |
///for getting the distance of the target node. |
| 1159 | 1162 |
BfsWizard dist(const int &d) |
| 1160 | 1163 |
{
|
| 1161 | 1164 |
Base::_di=const_cast<int*>(&d); |
| 1162 | 1165 |
return *this; |
| 1163 | 1166 |
} |
| 1164 | 1167 |
|
| 1165 | 1168 |
}; |
| 1166 | 1169 |
|
| 1167 | 1170 |
///Function-type interface for BFS algorithm. |
| 1168 | 1171 |
|
| 1169 | 1172 |
/// \ingroup search |
| 1170 | 1173 |
///Function-type interface for BFS algorithm. |
| 1171 | 1174 |
/// |
| 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: |
| 1365 | 1369 |
|
| 1366 | 1370 |
typedef BfsVisit Create; |
| 1367 | 1371 |
|
| 1368 | 1372 |
/// \name Named Template Parameters |
| 1369 | 1373 |
|
| 1370 | 1374 |
///@{
|
| 1371 | 1375 |
template <class T> |
| 1372 | 1376 |
struct SetReachedMapTraits : public Traits {
|
| 1373 | 1377 |
typedef T ReachedMap; |
| 1374 | 1378 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1375 | 1379 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
| 1376 | 1380 |
return 0; // ignore warnings |
| 1377 | 1381 |
} |
| 1378 | 1382 |
}; |
| 1379 | 1383 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 1380 | 1384 |
/// ReachedMap type. |
| 1381 | 1385 |
/// |
| 1382 | 1386 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
| 1383 | 1387 |
template <class T> |
| 1384 | 1388 |
struct SetReachedMap : public BfsVisit< Digraph, Visitor, |
| 1385 | 1389 |
SetReachedMapTraits<T> > {
|
| 1386 | 1390 |
typedef BfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
| 1387 | 1391 |
}; |
| 1388 | 1392 |
///@} |
| 1389 | 1393 |
|
| 1390 | 1394 |
public: |
| 1391 | 1395 |
|
| 1392 | 1396 |
/// \brief Constructor. |
| 1393 | 1397 |
/// |
| 1394 | 1398 |
/// Constructor. |
| 1395 | 1399 |
/// |
| 1396 | 1400 |
/// \param digraph The digraph the algorithm runs on. |
| 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 |
/// |
| 102 | 102 |
/// Constructor. |
| 103 | 103 |
/// \param map A map that assigns \c int values to the items. |
| 104 | 104 |
/// It is used internally to handle the cross references. |
| 105 | 105 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
| 106 | 106 |
/// \param comp The function object used for comparing the priorities. |
| 107 | 107 |
BinomialHeap(ItemIntMap &map, const Compare &comp) |
| 108 | 108 |
: _min(0), _head(-1), _iim(map), _comp(comp), _num_items(0) {}
|
| 109 | 109 |
|
| 110 | 110 |
/// \brief The number of items stored in the heap. |
| 111 | 111 |
/// |
| 112 | 112 |
/// This function returns the number of items stored in the heap. |
| 113 | 113 |
int size() const { return _num_items; }
|
| 114 | 114 |
|
| 115 | 115 |
/// \brief Check if the heap is empty. |
| 116 | 116 |
/// |
| 117 | 117 |
/// This function returns \c true if the heap is empty. |
| 118 | 118 |
bool empty() const { return _num_items==0; }
|
| 119 | 119 |
|
| 120 | 120 |
/// \brief Make the heap empty. |
| 121 | 121 |
/// |
| 122 | 122 |
/// This functon makes the heap empty. |
| 123 | 123 |
/// It does not change the cross reference map. If you want to reuse |
| 124 | 124 |
/// a heap that is not surely empty, you should first clear it and |
| 125 | 125 |
/// then you should set the cross reference map to \c PRE_HEAP |
| 126 | 126 |
/// for each item. |
| 127 | 127 |
void clear() {
|
| 128 | 128 |
_data.clear(); _min=0; _num_items=0; _head=-1; |
| 129 | 129 |
} |
| 130 | 130 |
|
| 131 | 131 |
/// \brief Set the priority of an item or insert it, if it is |
| 132 | 132 |
/// not stored in the heap. |
| 133 | 133 |
/// |
| 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(); |
| 102 | 102 |
Item it; |
| 103 | 103 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 104 | 104 |
int id = nf->id(it);; |
| 105 | 105 |
allocator.construct(&(values[id]), value); |
| 106 | 106 |
} |
| 107 | 107 |
} |
| 108 | 108 |
|
| 109 | 109 |
private: |
| 110 | 110 |
// \brief Constructor to copy a map of the same map type. |
| 111 | 111 |
// |
| 112 | 112 |
// Constructor to copy a map of the same map type. |
| 113 | 113 |
ArrayMap(const ArrayMap& copy) : Parent() {
|
| 114 | 114 |
if (copy.attached()) {
|
| 115 | 115 |
attach(*copy.notifier()); |
| 116 | 116 |
} |
| 117 | 117 |
capacity = copy.capacity; |
| 118 | 118 |
if (capacity == 0) return; |
| 119 | 119 |
values = allocator.allocate(capacity); |
| 120 | 120 |
Notifier* nf = Parent::notifier(); |
| 121 | 121 |
Item it; |
| 122 | 122 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 123 | 123 |
int id = nf->id(it);; |
| 124 | 124 |
allocator.construct(&(values[id]), copy.values[id]); |
| 125 | 125 |
} |
| 126 | 126 |
} |
| 127 | 127 |
|
| 128 | 128 |
// \brief Assign operator. |
| 129 | 129 |
// |
| 130 | 130 |
// This operator assigns for each item in the map the |
| 131 | 131 |
// value mapped to the same item in the copied map. |
| 132 | 132 |
// The parameter map should be indiced with the same |
| 133 | 133 |
// itemset because this assign operator does not change |
| 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 |
|
| 102 | 102 |
// long long |
| 103 | 103 |
template <typename _Graph, typename _Item> |
| 104 | 104 |
struct DefaultMapSelector<_Graph, _Item, signed long long> {
|
| 105 | 105 |
typedef VectorMap<_Graph, _Item, signed long long> Map; |
| 106 | 106 |
}; |
| 107 | 107 |
|
| 108 | 108 |
template <typename _Graph, typename _Item> |
| 109 | 109 |
struct DefaultMapSelector<_Graph, _Item, unsigned long long> {
|
| 110 | 110 |
typedef VectorMap<_Graph, _Item, unsigned long long> Map; |
| 111 | 111 |
}; |
| 112 | 112 |
|
| 113 | 113 |
#endif |
| 114 | 114 |
|
| 115 | 115 |
|
| 116 | 116 |
// float |
| 117 | 117 |
template <typename _Graph, typename _Item> |
| 118 | 118 |
struct DefaultMapSelector<_Graph, _Item, float> {
|
| 119 | 119 |
typedef VectorMap<_Graph, _Item, float> Map; |
| 120 | 120 |
}; |
| 121 | 121 |
|
| 122 | 122 |
|
| 123 | 123 |
// double |
| 124 | 124 |
template <typename _Graph, typename _Item> |
| 125 | 125 |
struct DefaultMapSelector<_Graph, _Item, double> {
|
| 126 | 126 |
typedef VectorMap<_Graph, _Item, double> Map; |
| 127 | 127 |
}; |
| 128 | 128 |
|
| 129 | 129 |
|
| 130 | 130 |
// long double |
| 131 | 131 |
template <typename _Graph, typename _Item> |
| 132 | 132 |
struct DefaultMapSelector<_Graph, _Item, long double> {
|
| 133 | 133 |
typedef VectorMap<_Graph, _Item, long double> Map; |
| 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 |
|
| 102 | 102 |
explicit NodeIt(const Digraph& _graph) : digraph(&_graph) {
|
| 103 | 103 |
_graph.first(static_cast<Node&>(*this)); |
| 104 | 104 |
} |
| 105 | 105 |
|
| 106 | 106 |
NodeIt(const Digraph& _graph, const Node& node) |
| 107 | 107 |
: Node(node), digraph(&_graph) {}
|
| 108 | 108 |
|
| 109 | 109 |
NodeIt& operator++() {
|
| 110 | 110 |
digraph->next(*this); |
| 111 | 111 |
return *this; |
| 112 | 112 |
} |
| 113 | 113 |
|
| 114 | 114 |
}; |
| 115 | 115 |
|
| 116 | 116 |
|
| 117 | 117 |
class ArcIt : public Arc {
|
| 118 | 118 |
const Digraph* digraph; |
| 119 | 119 |
public: |
| 120 | 120 |
|
| 121 | 121 |
ArcIt() { }
|
| 122 | 122 |
|
| 123 | 123 |
ArcIt(Invalid i) : Arc(i) { }
|
| 124 | 124 |
|
| 125 | 125 |
explicit ArcIt(const Digraph& _graph) : digraph(&_graph) {
|
| 126 | 126 |
_graph.first(static_cast<Arc&>(*this)); |
| 127 | 127 |
} |
| 128 | 128 |
|
| 129 | 129 |
ArcIt(const Digraph& _graph, const Arc& e) : |
| 130 | 130 |
Arc(e), digraph(&_graph) { }
|
| 131 | 131 |
|
| 132 | 132 |
ArcIt& operator++() {
|
| 133 | 133 |
digraph->next(*this); |
| 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; |
| 102 | 102 |
if (static_cast<int>(cross.size()) <= idx) {
|
| 103 | 103 |
cross.resize(idx + 1, -1); |
| 104 | 104 |
} |
| 105 | 105 |
cross[idx] = n; |
| 106 | 106 |
|
| 107 | 107 |
items[n].prev = last_item; |
| 108 | 108 |
items[n].next = -1; |
| 109 | 109 |
if (last_item != -1) {
|
| 110 | 110 |
items[last_item].next = n; |
| 111 | 111 |
} else {
|
| 112 | 112 |
first_item = n; |
| 113 | 113 |
} |
| 114 | 114 |
last_item = n; |
| 115 | 115 |
|
| 116 | 116 |
return n; |
| 117 | 117 |
} |
| 118 | 118 |
|
| 119 | 119 |
void eraseIndex(int idx) {
|
| 120 | 120 |
int n = cross[idx]; |
| 121 | 121 |
|
| 122 | 122 |
if (items[n].prev != -1) {
|
| 123 | 123 |
items[items[n].prev].next = items[n].next; |
| 124 | 124 |
} else {
|
| 125 | 125 |
first_item = items[n].next; |
| 126 | 126 |
} |
| 127 | 127 |
if (items[n].next != -1) {
|
| 128 | 128 |
items[items[n].next].prev = items[n].prev; |
| 129 | 129 |
} else {
|
| 130 | 130 |
last_item = items[n].prev; |
| 131 | 131 |
} |
| 132 | 132 |
|
| 133 | 133 |
if (first_free_item != -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 |
///\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, |
| 102 | 102 |
("HH':'mm':'ss"), buf2, 9) &&
|
| 103 | 103 |
GetDateFormat(MY_LOCALE, 0, &time, |
| 104 | 104 |
("yyyy"), buf3, 5)) {
|
| 105 | 105 |
os << buf1 << ' ' << buf2 << ' ' << buf3; |
| 106 | 106 |
} |
| 107 | 107 |
else os << "unknown"; |
| 108 | 108 |
#else |
| 109 | 109 |
timeval tv; |
| 110 | 110 |
gettimeofday(&tv, 0); |
| 111 | 111 |
|
| 112 | 112 |
char cbuf[26]; |
| 113 | 113 |
ctime_r(&tv.tv_sec,cbuf); |
| 114 | 114 |
os << cbuf; |
| 115 | 115 |
#endif |
| 116 | 116 |
return os.str(); |
| 117 | 117 |
} |
| 118 | 118 |
|
| 119 | 119 |
int getWinRndSeed() |
| 120 | 120 |
{
|
| 121 | 121 |
#ifdef WIN32 |
| 122 | 122 |
FILETIME time; |
| 123 | 123 |
GetSystemTimeAsFileTime(&time); |
| 124 | 124 |
return GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime; |
| 125 | 125 |
#else |
| 126 | 126 |
timeval tv; |
| 127 | 127 |
gettimeofday(&tv, 0); |
| 128 | 128 |
return getpid() + tv.tv_sec + tv.tv_usec; |
| 129 | 129 |
#endif |
| 130 | 130 |
} |
| 131 | 131 |
} |
| 132 | 132 |
} |
| 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 |
| 102 | 102 |
/// heap's point of view, but may be useful to the user. |
| 103 | 103 |
/// |
| 104 | 104 |
/// The item-int map must be initialized in such way that it assigns |
| 105 | 105 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
| 106 | 106 |
enum State {
|
| 107 | 107 |
IN_HEAP = 0, ///< = 0. |
| 108 | 108 |
PRE_HEAP = -1, ///< = -1. |
| 109 | 109 |
POST_HEAP = -2 ///< = -2. |
| 110 | 110 |
}; |
| 111 | 111 |
|
| 112 | 112 |
public: |
| 113 | 113 |
|
| 114 | 114 |
/// \brief Constructor. |
| 115 | 115 |
/// |
| 116 | 116 |
/// Constructor. |
| 117 | 117 |
/// \param map A map that assigns \c int values to the items. |
| 118 | 118 |
/// It is used internally to handle the cross references. |
| 119 | 119 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
| 120 | 120 |
explicit BucketHeap(ItemIntMap &map) : _iim(map), _minimum(0) {}
|
| 121 | 121 |
|
| 122 | 122 |
/// \brief The number of items stored in the heap. |
| 123 | 123 |
/// |
| 124 | 124 |
/// This function returns the number of items stored in the heap. |
| 125 | 125 |
int size() const { return _data.size(); }
|
| 126 | 126 |
|
| 127 | 127 |
/// \brief Check if the heap is empty. |
| 128 | 128 |
/// |
| 129 | 129 |
/// This function returns \c true if the heap is empty. |
| 130 | 130 |
bool empty() const { return _data.empty(); }
|
| 131 | 131 |
|
| 132 | 132 |
/// \brief Make the heap empty. |
| 133 | 133 |
/// |
| 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: |
| 102 | 102 |
|
| 103 | 103 |
/// The type of the digraph |
| 104 | 104 |
typedef typename TR::Digraph Digraph; |
| 105 | 105 |
/// The type of the flow amounts, capacity bounds and supply values |
| 106 | 106 |
typedef typename TR::Value Value; |
| 107 | 107 |
/// The type of the arc costs |
| 108 | 108 |
typedef typename TR::Cost Cost; |
| 109 | 109 |
|
| 110 | 110 |
/// The type of the heap used for internal Dijkstra computations |
| 111 | 111 |
typedef typename TR::Heap Heap; |
| 112 | 112 |
|
| 113 | 113 |
/// The \ref CapacityScalingDefaultTraits "traits class" of the algorithm |
| 114 | 114 |
typedef TR Traits; |
| 115 | 115 |
|
| 116 | 116 |
public: |
| 117 | 117 |
|
| 118 | 118 |
/// \brief Problem type constants for the \c run() function. |
| 119 | 119 |
/// |
| 120 | 120 |
/// Enum type containing the problem type constants that can be |
| 121 | 121 |
/// returned by the \ref run() function of the algorithm. |
| 122 | 122 |
enum ProblemType {
|
| 123 | 123 |
/// The problem has no feasible solution (flow). |
| 124 | 124 |
INFEASIBLE, |
| 125 | 125 |
/// The problem has optimal solution (i.e. it is feasible and |
| 126 | 126 |
/// bounded), and the algorithm has found optimal flow and node |
| 127 | 127 |
/// potentials (primal and dual solutions). |
| 128 | 128 |
OPTIMAL, |
| 129 | 129 |
/// The digraph contains an arc of negative cost and infinite |
| 130 | 130 |
/// upper bound. It means that the objective function is unbounded |
| 131 | 131 |
/// on that arc, however, note that it could actually be bounded |
| 132 | 132 |
/// over the feasible flows, but this algroithm cannot handle |
| 133 | 133 |
/// these cases. |
| 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; |
| 102 | 102 |
|
| 103 | 103 |
virtual void _setObjCoeff(int i, Value obj_coef); |
| 104 | 104 |
virtual Value _getObjCoeff(int i) const; |
| 105 | 105 |
|
| 106 | 106 |
virtual void _setSense(Sense sense); |
| 107 | 107 |
virtual Sense _getSense() const; |
| 108 | 108 |
|
| 109 | 109 |
virtual ColTypes _getColType(int col) const; |
| 110 | 110 |
virtual void _setColType(int col, ColTypes col_type); |
| 111 | 111 |
|
| 112 | 112 |
virtual SolveExitStatus _solve(); |
| 113 | 113 |
virtual ProblemType _getType() const; |
| 114 | 114 |
virtual Value _getSol(int i) const; |
| 115 | 115 |
virtual Value _getSolValue() const; |
| 116 | 116 |
|
| 117 | 117 |
virtual void _clear(); |
| 118 | 118 |
|
| 119 | 119 |
virtual void _messageLevel(MessageLevel level); |
| 120 | 120 |
void _applyMessageLevel(); |
| 121 | 121 |
|
| 122 | 122 |
int _message_level; |
| 123 | 123 |
|
| 124 | 124 |
|
| 125 | 125 |
|
| 126 | 126 |
}; |
| 127 | 127 |
|
| 128 | 128 |
} |
| 129 | 129 |
|
| 130 | 130 |
#endif |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_CIRCULATION_H |
| 20 | 20 |
#define LEMON_CIRCULATION_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/tolerance.h> |
| 23 | 23 |
#include <lemon/elevator.h> |
| 24 | 24 |
#include <limits> |
| 25 | 25 |
|
| 26 | 26 |
///\ingroup max_flow |
| 27 | 27 |
///\file |
| 28 | 28 |
///\brief Push-relabel algorithm for finding a feasible circulation. |
| 29 | 29 |
/// |
| 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. |
| 102 | 102 |
/// |
| 103 | 103 |
/// This function instantiates an \ref Elevator. |
| 104 | 104 |
/// \param digraph The digraph for which we would like to define |
| 105 | 105 |
/// the elevator. |
| 106 | 106 |
/// \param max_level The maximum level of the elevator. |
| 107 | 107 |
static Elevator* createElevator(const Digraph& digraph, int max_level) {
|
| 108 | 108 |
return new Elevator(digraph, max_level); |
| 109 | 109 |
} |
| 110 | 110 |
|
| 111 | 111 |
/// \brief The tolerance used by the algorithm |
| 112 | 112 |
/// |
| 113 | 113 |
/// The tolerance used by the algorithm to handle inexact computation. |
| 114 | 114 |
typedef lemon::Tolerance<Value> Tolerance; |
| 115 | 115 |
|
| 116 | 116 |
}; |
| 117 | 117 |
|
| 118 | 118 |
/** |
| 119 | 119 |
\brief Push-relabel algorithm for the network circulation problem. |
| 120 | 120 |
|
| 121 | 121 |
\ingroup max_flow |
| 122 | 122 |
This class implements a push-relabel algorithm for the \e network |
| 123 | 123 |
\e circulation problem. |
| 124 | 124 |
It is to find a feasible circulation when lower and upper bounds |
| 125 | 125 |
are given for the flow values on the arcs and lower bounds are |
| 126 | 126 |
given for the difference between the outgoing and incoming flow |
| 127 | 127 |
at the nodes. |
| 128 | 128 |
|
| 129 | 129 |
The exact formulation of this problem is the following. |
| 130 | 130 |
Let \f$G=(V,A)\f$ be a digraph, \f$lower: A\rightarrow\mathbf{R}\f$
|
| 131 | 131 |
\f$upper: A\rightarrow\mathbf{R}\cup\{\infty\}\f$ denote the lower and
|
| 132 | 132 |
upper bounds on the arcs, for which \f$lower(uv) \leq upper(uv)\f$ |
| 133 | 133 |
holds for all \f$uv\in A\f$, and \f$sup: V\rightarrow\mathbf{R}\f$
|
| 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)); |
| 102 | 102 |
_prob->deleteRows(1, &r); |
| 103 | 103 |
} |
| 104 | 104 |
|
| 105 | 105 |
void ClpLp::_eraseColId(int i) {
|
| 106 | 106 |
cols.eraseIndex(i); |
| 107 | 107 |
cols.shiftIndices(i); |
| 108 | 108 |
} |
| 109 | 109 |
|
| 110 | 110 |
void ClpLp::_eraseRowId(int i) {
|
| 111 | 111 |
rows.eraseIndex(i); |
| 112 | 112 |
rows.shiftIndices(i); |
| 113 | 113 |
} |
| 114 | 114 |
|
| 115 | 115 |
void ClpLp::_getColName(int c, std::string& name) const {
|
| 116 | 116 |
name = _prob->getColumnName(c); |
| 117 | 117 |
} |
| 118 | 118 |
|
| 119 | 119 |
void ClpLp::_setColName(int c, const std::string& name) {
|
| 120 | 120 |
_prob->setColumnName(c, const_cast<std::string&>(name)); |
| 121 | 121 |
_col_names_ref[name] = c; |
| 122 | 122 |
} |
| 123 | 123 |
|
| 124 | 124 |
int ClpLp::_colByName(const std::string& name) const {
|
| 125 | 125 |
std::map<std::string, int>::const_iterator it = _col_names_ref.find(name); |
| 126 | 126 |
return it != _col_names_ref.end() ? it->second : -1; |
| 127 | 127 |
} |
| 128 | 128 |
|
| 129 | 129 |
void ClpLp::_getRowName(int r, std::string& name) const {
|
| 130 | 130 |
name = _prob->getRowName(r); |
| 131 | 131 |
} |
| 132 | 132 |
|
| 133 | 133 |
void ClpLp::_setRowName(int r, const std::string& name) {
|
| 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; |
| 102 | 102 |
|
| 103 | 103 |
virtual void _setColLowerBound(int i, Value value); |
| 104 | 104 |
virtual Value _getColLowerBound(int i) const; |
| 105 | 105 |
virtual void _setColUpperBound(int i, Value value); |
| 106 | 106 |
virtual Value _getColUpperBound(int i) const; |
| 107 | 107 |
|
| 108 | 108 |
virtual void _setRowLowerBound(int i, Value value); |
| 109 | 109 |
virtual Value _getRowLowerBound(int i) const; |
| 110 | 110 |
virtual void _setRowUpperBound(int i, Value value); |
| 111 | 111 |
virtual Value _getRowUpperBound(int i) const; |
| 112 | 112 |
|
| 113 | 113 |
virtual void _setObjCoeffs(ExprIterator, ExprIterator); |
| 114 | 114 |
virtual void _getObjCoeffs(InsertIterator) const; |
| 115 | 115 |
|
| 116 | 116 |
virtual void _setObjCoeff(int i, Value obj_coef); |
| 117 | 117 |
virtual Value _getObjCoeff(int i) const; |
| 118 | 118 |
|
| 119 | 119 |
virtual void _setSense(Sense sense); |
| 120 | 120 |
virtual Sense _getSense() const; |
| 121 | 121 |
|
| 122 | 122 |
virtual SolveExitStatus _solve(); |
| 123 | 123 |
|
| 124 | 124 |
virtual Value _getPrimal(int i) const; |
| 125 | 125 |
virtual Value _getDual(int i) const; |
| 126 | 126 |
|
| 127 | 127 |
virtual Value _getPrimalValue() const; |
| 128 | 128 |
|
| 129 | 129 |
virtual Value _getPrimalRay(int i) const; |
| 130 | 130 |
virtual Value _getDualRay(int i) const; |
| 131 | 131 |
|
| 132 | 132 |
virtual VarStatus _getColStatus(int i) const; |
| 133 | 133 |
virtual VarStatus _getRowStatus(int i) const; |
| 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 |
| 102 | 102 |
/// the nodes; this order has nothing to do with the iteration |
| 103 | 103 |
/// ordering of the nodes. |
| 104 | 104 |
bool operator<(Node) const { return false; }
|
| 105 | 105 |
}; |
| 106 | 106 |
|
| 107 | 107 |
/// Iterator class for the nodes. |
| 108 | 108 |
|
| 109 | 109 |
/// This iterator goes through each node of the digraph. |
| 110 | 110 |
/// Its usage is quite simple, for example, you can count the number |
| 111 | 111 |
/// of nodes in a digraph \c g of type \c %Digraph like this: |
| 112 | 112 |
///\code |
| 113 | 113 |
/// int count=0; |
| 114 | 114 |
/// for (Digraph::NodeIt n(g); n!=INVALID; ++n) ++count; |
| 115 | 115 |
///\endcode |
| 116 | 116 |
class NodeIt : public Node {
|
| 117 | 117 |
public: |
| 118 | 118 |
/// Default constructor |
| 119 | 119 |
|
| 120 | 120 |
/// Default constructor. |
| 121 | 121 |
/// \warning It sets the iterator to an undefined value. |
| 122 | 122 |
NodeIt() { }
|
| 123 | 123 |
/// Copy constructor. |
| 124 | 124 |
|
| 125 | 125 |
/// Copy constructor. |
| 126 | 126 |
/// |
| 127 | 127 |
NodeIt(const NodeIt& n) : Node(n) { }
|
| 128 | 128 |
/// %Invalid constructor \& conversion. |
| 129 | 129 |
|
| 130 | 130 |
/// Initializes the iterator to be invalid. |
| 131 | 131 |
/// \sa Invalid for more details. |
| 132 | 132 |
NodeIt(Invalid) { }
|
| 133 | 133 |
/// Sets the iterator to the first 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 |
///\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 |
|
| 102 | 102 |
/// Default constructor. |
| 103 | 103 |
/// \warning It sets the object to an undefined value. |
| 104 | 104 |
Node() { }
|
| 105 | 105 |
/// Copy constructor. |
| 106 | 106 |
|
| 107 | 107 |
/// Copy constructor. |
| 108 | 108 |
/// |
| 109 | 109 |
Node(const Node&) { }
|
| 110 | 110 |
|
| 111 | 111 |
/// %Invalid constructor \& conversion. |
| 112 | 112 |
|
| 113 | 113 |
/// Initializes the object to be invalid. |
| 114 | 114 |
/// \sa Invalid for more details. |
| 115 | 115 |
Node(Invalid) { }
|
| 116 | 116 |
/// Equality operator |
| 117 | 117 |
|
| 118 | 118 |
/// Equality operator. |
| 119 | 119 |
/// |
| 120 | 120 |
/// Two iterators are equal if and only if they point to the |
| 121 | 121 |
/// same object or both are \c INVALID. |
| 122 | 122 |
bool operator==(Node) const { return true; }
|
| 123 | 123 |
|
| 124 | 124 |
/// Inequality operator |
| 125 | 125 |
|
| 126 | 126 |
/// Inequality operator. |
| 127 | 127 |
bool operator!=(Node) const { return true; }
|
| 128 | 128 |
|
| 129 | 129 |
/// Artificial ordering operator. |
| 130 | 130 |
|
| 131 | 131 |
/// Artificial ordering operator. |
| 132 | 132 |
/// |
| 133 | 133 |
/// \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 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 {
|
| 102 | 102 |
void constraints() {
|
| 103 | 103 |
_GraphItem i1; |
| 104 | 104 |
i1=INVALID; |
| 105 | 105 |
_GraphItem i2 = i1; |
| 106 | 106 |
_GraphItem i3 = INVALID; |
| 107 | 107 |
|
| 108 | 108 |
i1 = i2 = i3; |
| 109 | 109 |
|
| 110 | 110 |
bool b; |
| 111 | 111 |
b = (ia == ib) && (ia != ib); |
| 112 | 112 |
b = (ia == INVALID) && (ib != INVALID); |
| 113 | 113 |
b = (ia < ib); |
| 114 | 114 |
} |
| 115 | 115 |
|
| 116 | 116 |
const _GraphItem &ia; |
| 117 | 117 |
const _GraphItem &ib; |
| 118 | 118 |
}; |
| 119 | 119 |
}; |
| 120 | 120 |
|
| 121 | 121 |
/// \brief Base skeleton class for directed graphs. |
| 122 | 122 |
/// |
| 123 | 123 |
/// This class describes the base interface of directed graph types. |
| 124 | 124 |
/// All digraph %concepts have to conform to this class. |
| 125 | 125 |
/// It just provides types for nodes and arcs and functions |
| 126 | 126 |
/// to get the source and the target nodes of arcs. |
| 127 | 127 |
class BaseDigraphComponent {
|
| 128 | 128 |
public: |
| 129 | 129 |
|
| 130 | 130 |
typedef BaseDigraphComponent Digraph; |
| 131 | 131 |
|
| 132 | 132 |
/// \brief Node class of the digraph. |
| 133 | 133 |
/// |
| 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 |
| 102 | 102 |
/// handle the cross references. The assigned value must be |
| 103 | 103 |
/// \c PRE_HEAP (<tt>-1</tt>) for each item. |
| 104 | 104 |
/// \param comp The function object used for comparing the priorities. |
| 105 | 105 |
#ifdef DOXYGEN |
| 106 | 106 |
explicit Heap(ItemIntMap &map, const CMP &comp) {}
|
| 107 | 107 |
#else |
| 108 | 108 |
explicit Heap(ItemIntMap&, const CMP&) {}
|
| 109 | 109 |
#endif |
| 110 | 110 |
|
| 111 | 111 |
/// \brief The number of items stored in the heap. |
| 112 | 112 |
/// |
| 113 | 113 |
/// This function returns the number of items stored in the heap. |
| 114 | 114 |
int size() const { return 0; }
|
| 115 | 115 |
|
| 116 | 116 |
/// \brief Check if the heap is empty. |
| 117 | 117 |
/// |
| 118 | 118 |
/// This function returns \c true if the heap is empty. |
| 119 | 119 |
bool empty() const { return false; }
|
| 120 | 120 |
|
| 121 | 121 |
/// \brief Make the heap empty. |
| 122 | 122 |
/// |
| 123 | 123 |
/// This functon makes the heap empty. |
| 124 | 124 |
/// It does not change the cross reference map. If you want to reuse |
| 125 | 125 |
/// a heap that is not surely empty, you should first clear it and |
| 126 | 126 |
/// then you should set the cross reference map to \c PRE_HEAP |
| 127 | 127 |
/// for each item. |
| 128 | 128 |
void clear() {}
|
| 129 | 129 |
|
| 130 | 130 |
/// \brief Insert an item into the heap with the given priority. |
| 131 | 131 |
/// |
| 132 | 132 |
/// This function inserts the given item into the heap with the |
| 133 | 133 |
/// given priority. |
| 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; |
| 102 | 102 |
bfs.predMap(predMap); |
| 103 | 103 |
|
| 104 | 104 |
DistMap distMap; |
| 105 | 105 |
bfs.distMap(distMap); |
| 106 | 106 |
|
| 107 | 107 |
bfs.init(); |
| 108 | 108 |
for(typename Graph::NodeIt n(graph); n != INVALID; ++n) {
|
| 109 | 109 |
if (!bfs.reached(n)) {
|
| 110 | 110 |
bfs.addSource(n); |
| 111 | 111 |
bfs.start(); |
| 112 | 112 |
++compNum; |
| 113 | 113 |
} |
| 114 | 114 |
} |
| 115 | 115 |
return compNum; |
| 116 | 116 |
} |
| 117 | 117 |
|
| 118 | 118 |
/// \ingroup graph_properties |
| 119 | 119 |
/// |
| 120 | 120 |
/// \brief Find the connected components of an undirected graph |
| 121 | 121 |
/// |
| 122 | 122 |
/// This function finds the connected components of the given undirected |
| 123 | 123 |
/// graph. |
| 124 | 124 |
/// |
| 125 | 125 |
/// The connected components are the classes of an equivalence relation |
| 126 | 126 |
/// on the nodes of an undirected graph. Two nodes are in the same class |
| 127 | 127 |
/// if they are connected with a path. |
| 128 | 128 |
/// |
| 129 | 129 |
/// \image html connected_components.png |
| 130 | 130 |
/// \image latex connected_components.eps "Connected components" width=\textwidth |
| 131 | 131 |
/// |
| 132 | 132 |
/// \param graph The undirected graph. |
| 133 | 133 |
/// \retval compMap A writable node map. The values will be set from 0 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_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, |
| 102 | 102 |
///ie. the graph type depend on a template parameter. |
| 103 | 103 |
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \ |
| 104 | 104 |
typedef typename Digraph::Node Node; \ |
| 105 | 105 |
typedef typename Digraph::NodeIt NodeIt; \ |
| 106 | 106 |
typedef typename Digraph::Arc Arc; \ |
| 107 | 107 |
typedef typename Digraph::ArcIt ArcIt; \ |
| 108 | 108 |
typedef typename Digraph::InArcIt InArcIt; \ |
| 109 | 109 |
typedef typename Digraph::OutArcIt OutArcIt; \ |
| 110 | 110 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \ |
| 111 | 111 |
typedef typename Digraph::template NodeMap<int> IntNodeMap; \ |
| 112 | 112 |
typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \ |
| 113 | 113 |
typedef typename Digraph::template ArcMap<bool> BoolArcMap; \ |
| 114 | 114 |
typedef typename Digraph::template ArcMap<int> IntArcMap; \ |
| 115 | 115 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap |
| 116 | 116 |
|
| 117 | 117 |
///Create convenience typedefs for the graph types and iterators |
| 118 | 118 |
|
| 119 | 119 |
///This \c \#define creates the same convenient type definitions as defined |
| 120 | 120 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates |
| 121 | 121 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap, |
| 122 | 122 |
///\c DoubleEdgeMap. |
| 123 | 123 |
/// |
| 124 | 124 |
///\note If the graph type is a dependent type, ie. the graph type depend |
| 125 | 125 |
///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS() |
| 126 | 126 |
///macro. |
| 127 | 127 |
#define GRAPH_TYPEDEFS(Graph) \ |
| 128 | 128 |
DIGRAPH_TYPEDEFS(Graph); \ |
| 129 | 129 |
typedef Graph::Edge Edge; \ |
| 130 | 130 |
typedef Graph::EdgeIt EdgeIt; \ |
| 131 | 131 |
typedef Graph::IncEdgeIt IncEdgeIt; \ |
| 132 | 132 |
typedef Graph::EdgeMap<bool> BoolEdgeMap; \ |
| 133 | 133 |
typedef Graph::EdgeMap<int> IntEdgeMap; \ |
| ... | ... |
@@ -1114,257 +1114,258 @@ |
| 1114 | 1114 |
template <typename Graph> |
| 1115 | 1115 |
struct FindEdgeSelector< |
| 1116 | 1116 |
Graph, |
| 1117 | 1117 |
typename enable_if<typename Graph::FindEdgeTag, void>::type> |
| 1118 | 1118 |
{
|
| 1119 | 1119 |
typedef typename Graph::Node Node; |
| 1120 | 1120 |
typedef typename Graph::Edge Edge; |
| 1121 | 1121 |
static Edge find(const Graph &g, Node u, Node v, Edge prev) {
|
| 1122 | 1122 |
return g.findEdge(u, v, prev); |
| 1123 | 1123 |
} |
| 1124 | 1124 |
}; |
| 1125 | 1125 |
} |
| 1126 | 1126 |
|
| 1127 | 1127 |
/// \brief Find an edge between two nodes of a graph. |
| 1128 | 1128 |
/// |
| 1129 | 1129 |
/// This function finds an edge from node \c u to node \c v in graph \c g. |
| 1130 | 1130 |
/// If node \c u and node \c v is equal then each loop edge |
| 1131 | 1131 |
/// will be enumerated once. |
| 1132 | 1132 |
/// |
| 1133 | 1133 |
/// If \c prev is \ref INVALID (this is the default value), then |
| 1134 | 1134 |
/// it finds the first edge from \c u to \c v. Otherwise it looks for |
| 1135 | 1135 |
/// the next edge from \c u to \c v after \c prev. |
| 1136 | 1136 |
/// \return The found edge or \ref INVALID if there is no such an edge. |
| 1137 | 1137 |
/// |
| 1138 | 1138 |
/// Thus you can iterate through each edge between \c u and \c v |
| 1139 | 1139 |
/// as it follows. |
| 1140 | 1140 |
///\code |
| 1141 | 1141 |
/// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) {
|
| 1142 | 1142 |
/// ... |
| 1143 | 1143 |
/// } |
| 1144 | 1144 |
///\endcode |
| 1145 | 1145 |
/// |
| 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; |
| 1339 | 1340 |
_right[arc] = INVALID; |
| 1340 | 1341 |
|
| 1341 | 1342 |
Arc e = _head[s]; |
| 1342 | 1343 |
if (e == INVALID) {
|
| 1343 | 1344 |
_head[s] = arc; |
| 1344 | 1345 |
_parent[arc] = INVALID; |
| 1345 | 1346 |
return; |
| 1346 | 1347 |
} |
| 1347 | 1348 |
while (true) {
|
| 1348 | 1349 |
if (t < _g.target(e)) {
|
| 1349 | 1350 |
if (_left[e] == INVALID) {
|
| 1350 | 1351 |
_left[e] = arc; |
| 1351 | 1352 |
_parent[arc] = e; |
| 1352 | 1353 |
splay(arc); |
| 1353 | 1354 |
return; |
| 1354 | 1355 |
} else {
|
| 1355 | 1356 |
e = _left[e]; |
| 1356 | 1357 |
} |
| 1357 | 1358 |
} else {
|
| 1358 | 1359 |
if (_right[e] == INVALID) {
|
| 1359 | 1360 |
_right[e] = arc; |
| 1360 | 1361 |
_parent[arc] = e; |
| 1361 | 1362 |
splay(arc); |
| 1362 | 1363 |
return; |
| 1363 | 1364 |
} else {
|
| 1364 | 1365 |
e = _right[e]; |
| 1365 | 1366 |
} |
| 1366 | 1367 |
} |
| 1367 | 1368 |
} |
| 1368 | 1369 |
} |
| 1369 | 1370 |
|
| 1370 | 1371 |
void remove(Arc arc) {
|
| 1 |
/* -*- C++ -*- |
|
| 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
| 2 | 2 |
* |
| 3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
| 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_COST_SCALING_H |
| 20 | 20 |
#define LEMON_COST_SCALING_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup min_cost_flow_algs |
| 23 | 23 |
/// \file |
| 24 | 24 |
/// \brief Cost scaling algorithm for finding a minimum cost flow. |
| 25 | 25 |
|
| 26 | 26 |
#include <vector> |
| 27 | 27 |
#include <deque> |
| 28 | 28 |
#include <limits> |
| 29 | 29 |
|
| 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 |
| 102 | 102 |
/// executed using the \ref run() function. If some parameters are not |
| 103 | 103 |
/// specified, then default values will be used. |
| 104 | 104 |
/// |
| 105 | 105 |
/// \tparam GR The digraph type the algorithm runs on. |
| 106 | 106 |
/// \tparam V The number type used for flow amounts, capacity bounds |
| 107 | 107 |
/// and supply values in the algorithm. By default, it is \c int. |
| 108 | 108 |
/// \tparam C The number type used for costs and potentials in the |
| 109 | 109 |
/// algorithm. By default, it is the same as \c V. |
| 110 | 110 |
/// \tparam TR The traits class that defines various types used by the |
| 111 | 111 |
/// algorithm. By default, it is \ref CostScalingDefaultTraits |
| 112 | 112 |
/// "CostScalingDefaultTraits<GR, V, C>". |
| 113 | 113 |
/// In most cases, this parameter should not be set directly, |
| 114 | 114 |
/// consider to use the named template parameters instead. |
| 115 | 115 |
/// |
| 116 | 116 |
/// \warning Both number types must be signed and all input data must |
| 117 | 117 |
/// be integer. |
| 118 | 118 |
/// \warning This algorithm does not support negative costs for such |
| 119 | 119 |
/// arcs that have infinite upper bound. |
| 120 | 120 |
/// |
| 121 | 121 |
/// \note %CostScaling provides three different internal methods, |
| 122 | 122 |
/// from which the most efficient one is used by default. |
| 123 | 123 |
/// For more information, see \ref Method. |
| 124 | 124 |
#ifdef DOXYGEN |
| 125 | 125 |
template <typename GR, typename V, typename C, typename TR> |
| 126 | 126 |
#else |
| 127 | 127 |
template < typename GR, typename V = int, typename C = V, |
| 128 | 128 |
typename TR = CostScalingDefaultTraits<GR, V, C> > |
| 129 | 129 |
#endif |
| 130 | 130 |
class CostScaling |
| 131 | 131 |
{
|
| 132 | 132 |
public: |
| 133 | 133 |
| 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); |
| 102 | 102 |
return i; |
| 103 | 103 |
} |
| 104 | 104 |
|
| 105 | 105 |
|
| 106 | 106 |
int CplexBase::_addRow() {
|
| 107 | 107 |
int i = CPXgetnumrows(cplexEnv(), _prob); |
| 108 | 108 |
const double ub = INF; |
| 109 | 109 |
const char s = 'L'; |
| 110 | 110 |
CPXnewrows(cplexEnv(), _prob, 1, &ub, &s, 0, 0); |
| 111 | 111 |
return i; |
| 112 | 112 |
} |
| 113 | 113 |
|
| 114 | 114 |
int CplexBase::_addRow(Value lb, ExprIterator b, |
| 115 | 115 |
ExprIterator e, Value ub) {
|
| 116 | 116 |
int i = CPXgetnumrows(cplexEnv(), _prob); |
| 117 | 117 |
if (lb == -INF) {
|
| 118 | 118 |
const char s = 'L'; |
| 119 | 119 |
CPXnewrows(cplexEnv(), _prob, 1, &ub, &s, 0, 0); |
| 120 | 120 |
} else if (ub == INF) {
|
| 121 | 121 |
const char s = 'G'; |
| 122 | 122 |
CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, 0, 0); |
| 123 | 123 |
} else if (lb == ub){
|
| 124 | 124 |
const char s = 'E'; |
| 125 | 125 |
CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, 0, 0); |
| 126 | 126 |
} else {
|
| 127 | 127 |
const char s = 'R'; |
| 128 | 128 |
double len = ub - lb; |
| 129 | 129 |
CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, &len, 0); |
| 130 | 130 |
} |
| 131 | 131 |
|
| 132 | 132 |
std::vector<int> indices; |
| 133 | 133 |
std::vector<int> rowlist; |
| 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 |
| 102 | 102 |
/// potentials (primal and dual solutions). |
| 103 | 103 |
OPTIMAL, |
| 104 | 104 |
/// The digraph contains an arc of negative cost and infinite |
| 105 | 105 |
/// upper bound. It means that the objective function is unbounded |
| 106 | 106 |
/// on that arc, however, note that it could actually be bounded |
| 107 | 107 |
/// over the feasible flows, but this algroithm cannot handle |
| 108 | 108 |
/// these cases. |
| 109 | 109 |
UNBOUNDED |
| 110 | 110 |
}; |
| 111 | 111 |
|
| 112 | 112 |
/// \brief Constants for selecting the used method. |
| 113 | 113 |
/// |
| 114 | 114 |
/// Enum type containing constants for selecting the used method |
| 115 | 115 |
/// for the \ref run() function. |
| 116 | 116 |
/// |
| 117 | 117 |
/// \ref CycleCanceling provides three different cycle-canceling |
| 118 | 118 |
/// methods. By default, \ref CANCEL_AND_TIGHTEN "Cancel and Tighten" |
| 119 | 119 |
/// is used, which proved to be the most efficient and the most robust |
| 120 | 120 |
/// on various test inputs. |
| 121 | 121 |
/// However, the other methods can be selected using the \ref run() |
| 122 | 122 |
/// function with the proper parameter. |
| 123 | 123 |
enum Method {
|
| 124 | 124 |
/// A simple cycle-canceling method, which uses the |
| 125 | 125 |
/// \ref BellmanFord "Bellman-Ford" algorithm with limited iteration |
| 126 | 126 |
/// number for detecting negative cycles in the residual network. |
| 127 | 127 |
SIMPLE_CYCLE_CANCELING, |
| 128 | 128 |
/// The "Minimum Mean Cycle-Canceling" algorithm, which is a |
| 129 | 129 |
/// well-known strongly polynomial method |
| 130 | 130 |
/// \ref goldberg89cyclecanceling. It improves along a |
| 131 | 131 |
/// \ref min_mean_cycle "minimum mean cycle" in each iteration. |
| 132 | 132 |
/// Its running time complexity is O(n<sup>2</sup>m<sup>3</sup>log(n)). |
| 133 | 133 |
MINIMUM_MEAN_CYCLE_CANCELING, |
| 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 |
{
|
| 370 | 372 |
if(local_reached) {
|
| 371 | 373 |
delete _reached; |
| 372 | 374 |
local_reached=false; |
| 373 | 375 |
} |
| 374 | 376 |
_reached = &m; |
| 375 | 377 |
return *this; |
| 376 | 378 |
} |
| 377 | 379 |
|
| 378 | 380 |
///Sets the map that indicates which nodes are processed. |
| 379 | 381 |
|
| 380 | 382 |
///Sets the map that indicates which nodes are processed. |
| 381 | 383 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 382 | 384 |
///or \ref init(), an instance will be allocated automatically. |
| 383 | 385 |
///The destructor deallocates this automatically allocated map, |
| 384 | 386 |
///of course. |
| 385 | 387 |
///\return <tt> (*this) </tt> |
| 386 | 388 |
Dfs &processedMap(ProcessedMap &m) |
| 387 | 389 |
{
|
| 388 | 390 |
if(local_processed) {
|
| 389 | 391 |
delete _processed; |
| 390 | 392 |
local_processed=false; |
| 391 | 393 |
} |
| 392 | 394 |
_processed = &m; |
| 393 | 395 |
return *this; |
| 394 | 396 |
} |
| 395 | 397 |
|
| 396 | 398 |
///Sets the map that stores the distances of the nodes. |
| 397 | 399 |
|
| 398 | 400 |
///Sets the map that stores the distances of the nodes calculated by |
| 399 | 401 |
///the algorithm. |
| 400 | 402 |
///If you don't use this function before calling \ref run(Node) "run()" |
| 401 | 403 |
///or \ref init(), an instance will be allocated automatically. |
| ... | ... |
@@ -677,257 +679,258 @@ |
| 677 | 679 |
/// |
| 678 | 680 |
///\warning \c t should be reached from the root(s). |
| 679 | 681 |
/// |
| 680 | 682 |
///\pre Either \ref run(Node) "run()" or \ref init() |
| 681 | 683 |
///must be called before using this function. |
| 682 | 684 |
Path path(Node t) const { return Path(*G, *_pred, t); }
|
| 683 | 685 |
|
| 684 | 686 |
///The distance of the given node from the root(s). |
| 685 | 687 |
|
| 686 | 688 |
///Returns the distance of the given node from the root(s). |
| 687 | 689 |
/// |
| 688 | 690 |
///\warning If node \c v is not reached from the root(s), then |
| 689 | 691 |
///the return value of this function is undefined. |
| 690 | 692 |
/// |
| 691 | 693 |
///\pre Either \ref run(Node) "run()" or \ref init() |
| 692 | 694 |
///must be called before using this function. |
| 693 | 695 |
int dist(Node v) const { return (*_dist)[v]; }
|
| 694 | 696 |
|
| 695 | 697 |
///Returns the 'previous arc' of the %DFS tree for the given node. |
| 696 | 698 |
|
| 697 | 699 |
///This function returns the 'previous arc' of the %DFS tree for the |
| 698 | 700 |
///node \c v, i.e. it returns the last arc of a %DFS path from a |
| 699 | 701 |
///root to \c v. It is \c INVALID if \c v is not reached from the |
| 700 | 702 |
///root(s) or if \c v is a root. |
| 701 | 703 |
/// |
| 702 | 704 |
///The %DFS tree used here is equal to the %DFS tree used in |
| 703 | 705 |
///\ref predNode() and \ref predMap(). |
| 704 | 706 |
/// |
| 705 | 707 |
///\pre Either \ref run(Node) "run()" or \ref init() |
| 706 | 708 |
///must be called before using this function. |
| 707 | 709 |
Arc predArc(Node v) const { return (*_pred)[v];}
|
| 708 | 710 |
|
| 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; |
| 902 | 905 |
|
| 903 | 906 |
typedef typename TR::Digraph Digraph; |
| 904 | 907 |
|
| 905 | 908 |
typedef typename Digraph::Node Node; |
| 906 | 909 |
typedef typename Digraph::NodeIt NodeIt; |
| 907 | 910 |
typedef typename Digraph::Arc Arc; |
| 908 | 911 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 909 | 912 |
|
| 910 | 913 |
typedef typename TR::PredMap PredMap; |
| 911 | 914 |
typedef typename TR::DistMap DistMap; |
| 912 | 915 |
typedef typename TR::ReachedMap ReachedMap; |
| 913 | 916 |
typedef typename TR::ProcessedMap ProcessedMap; |
| 914 | 917 |
typedef typename TR::Path Path; |
| 915 | 918 |
|
| 916 | 919 |
public: |
| 917 | 920 |
|
| 918 | 921 |
/// Constructor. |
| 919 | 922 |
DfsWizard() : TR() {}
|
| 920 | 923 |
|
| 921 | 924 |
/// Constructor that requires parameters. |
| 922 | 925 |
|
| 923 | 926 |
/// Constructor that requires parameters. |
| 924 | 927 |
/// These parameters will be the default values for the traits class. |
| 925 | 928 |
/// \param g The digraph the algorithm runs on. |
| 926 | 929 |
DfsWizard(const Digraph &g) : |
| 927 | 930 |
TR(g) {}
|
| 928 | 931 |
|
| 929 | 932 |
///Copy constructor |
| 930 | 933 |
DfsWizard(const TR &b) : TR(b) {}
|
| 931 | 934 |
|
| 932 | 935 |
~DfsWizard() {}
|
| 933 | 936 |
|
| ... | ... |
@@ -1082,257 +1085,258 @@ |
| 1082 | 1085 |
} |
| 1083 | 1086 |
|
| 1084 | 1087 |
///\brief \ref named-func-param "Named parameter" |
| 1085 | 1088 |
///for getting the distance of the target node. |
| 1086 | 1089 |
/// |
| 1087 | 1090 |
///\ref named-func-param "Named parameter" |
| 1088 | 1091 |
///for getting the distance of the target node. |
| 1089 | 1092 |
DfsWizard dist(const int &d) |
| 1090 | 1093 |
{
|
| 1091 | 1094 |
Base::_di=const_cast<int*>(&d); |
| 1092 | 1095 |
return *this; |
| 1093 | 1096 |
} |
| 1094 | 1097 |
|
| 1095 | 1098 |
}; |
| 1096 | 1099 |
|
| 1097 | 1100 |
///Function-type interface for DFS algorithm. |
| 1098 | 1101 |
|
| 1099 | 1102 |
///\ingroup search |
| 1100 | 1103 |
///Function-type interface for DFS algorithm. |
| 1101 | 1104 |
/// |
| 1102 | 1105 |
///This function also has several \ref named-func-param "named parameters", |
| 1103 | 1106 |
///they are declared as the members of class \ref DfsWizard. |
| 1104 | 1107 |
///The following examples show how to use these parameters. |
| 1105 | 1108 |
///\code |
| 1106 | 1109 |
/// // Compute the DFS tree |
| 1107 | 1110 |
/// dfs(g).predMap(preds).distMap(dists).run(s); |
| 1108 | 1111 |
/// |
| 1109 | 1112 |
/// // Compute the DFS path from s to t |
| 1110 | 1113 |
/// bool reached = dfs(g).path(p).dist(d).run(s,t); |
| 1111 | 1114 |
///\endcode |
| 1112 | 1115 |
///\warning Don't forget to put the \ref DfsWizard::run(Node) "run()" |
| 1113 | 1116 |
///to the end of the parameter list. |
| 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: |
| 1307 | 1311 |
|
| 1308 | 1312 |
typedef DfsVisit Create; |
| 1309 | 1313 |
|
| 1310 | 1314 |
/// \name Named Template Parameters |
| 1311 | 1315 |
|
| 1312 | 1316 |
///@{
|
| 1313 | 1317 |
template <class T> |
| 1314 | 1318 |
struct SetReachedMapTraits : public Traits {
|
| 1315 | 1319 |
typedef T ReachedMap; |
| 1316 | 1320 |
static ReachedMap *createReachedMap(const Digraph &digraph) {
|
| 1317 | 1321 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
| 1318 | 1322 |
return 0; // ignore warnings |
| 1319 | 1323 |
} |
| 1320 | 1324 |
}; |
| 1321 | 1325 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 1322 | 1326 |
/// ReachedMap type. |
| 1323 | 1327 |
/// |
| 1324 | 1328 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
| 1325 | 1329 |
template <class T> |
| 1326 | 1330 |
struct SetReachedMap : public DfsVisit< Digraph, Visitor, |
| 1327 | 1331 |
SetReachedMapTraits<T> > {
|
| 1328 | 1332 |
typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
| 1329 | 1333 |
}; |
| 1330 | 1334 |
///@} |
| 1331 | 1335 |
|
| 1332 | 1336 |
public: |
| 1333 | 1337 |
|
| 1334 | 1338 |
/// \brief Constructor. |
| 1335 | 1339 |
/// |
| 1336 | 1340 |
/// Constructor. |
| 1337 | 1341 |
/// |
| 1338 | 1342 |
/// \param digraph The digraph the algorithm runs on. |
| 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. |
| 102 | 102 |
/// |
| 103 | 103 |
///\sa BinHeap |
| 104 | 104 |
///\sa Dijkstra |
| 105 | 105 |
typedef BinHeap<typename LEN::Value, HeapCrossRef, std::less<Value> > Heap; |
| 106 | 106 |
///Instantiates a \c Heap. |
| 107 | 107 |
|
| 108 | 108 |
///This function instantiates a \ref Heap. |
| 109 | 109 |
static Heap *createHeap(HeapCrossRef& r) |
| 110 | 110 |
{
|
| 111 | 111 |
return new Heap(r); |
| 112 | 112 |
} |
| 113 | 113 |
|
| 114 | 114 |
///\brief The type of the map that stores the predecessor |
| 115 | 115 |
///arcs of the shortest paths. |
| 116 | 116 |
/// |
| 117 | 117 |
///The type of the map that stores the predecessor |
| 118 | 118 |
///arcs of the shortest paths. |
| 119 | 119 |
///It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 120 | 120 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
| 121 | 121 |
///Instantiates a \c PredMap. |
| 122 | 122 |
|
| 123 | 123 |
///This function instantiates a \ref PredMap. |
| 124 | 124 |
///\param g is the digraph, to which we would like to define the |
| 125 | 125 |
///\ref PredMap. |
| 126 | 126 |
static PredMap *createPredMap(const Digraph &g) |
| 127 | 127 |
{
|
| 128 | 128 |
return new PredMap(g); |
| 129 | 129 |
} |
| 130 | 130 |
|
| 131 | 131 |
///The type of the map that indicates which nodes are processed. |
| 132 | 132 |
|
| 133 | 133 |
///The type of the map that indicates which nodes are processed. |
| 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 |
} |
| 102 | 102 |
|
| 103 | 103 |
|
| 104 | 104 |
/// \brief DIMACS minimum cost flow reader function. |
| 105 | 105 |
/// |
| 106 | 106 |
/// This function reads a minimum cost flow instance from DIMACS format, |
| 107 | 107 |
/// i.e. from a DIMACS file having a line starting with |
| 108 | 108 |
/// \code |
| 109 | 109 |
/// p min |
| 110 | 110 |
/// \endcode |
| 111 | 111 |
/// At the beginning, \c g is cleared by \c g.clear(). The supply |
| 112 | 112 |
/// amount of the nodes are written to the \c supply node map |
| 113 | 113 |
/// (they are signed values). The lower bounds, capacities and costs |
| 114 | 114 |
/// of the arcs are written to the \c lower, \c capacity and \c cost |
| 115 | 115 |
/// arc maps. |
| 116 | 116 |
/// |
| 117 | 117 |
/// If the capacity of an arc is less than the lower bound, it will |
| 118 | 118 |
/// be set to "infinite" instead. The actual value of "infinite" is |
| 119 | 119 |
/// contolled by the \c infty parameter. If it is 0 (the default value), |
| 120 | 120 |
/// \c std::numeric_limits<Capacity>::infinity() will be used if available, |
| 121 | 121 |
/// \c std::numeric_limits<Capacity>::max() otherwise. If \c infty is set to |
| 122 | 122 |
/// a non-zero value, that value will be used as "infinite". |
| 123 | 123 |
/// |
| 124 | 124 |
/// If the file type was previously evaluated by dimacsType(), then |
| 125 | 125 |
/// the descriptor struct should be given by the \c dest parameter. |
| 126 | 126 |
template <typename Digraph, typename LowerMap, |
| 127 | 127 |
typename CapacityMap, typename CostMap, |
| 128 | 128 |
typename SupplyMap> |
| 129 | 129 |
void readDimacsMin(std::istream& is, |
| 130 | 130 |
Digraph &g, |
| 131 | 131 |
LowerMap& lower, |
| 132 | 132 |
CapacityMap& capacity, |
| 133 | 133 |
CostMap& cost, |
| 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 |
} |
| 102 | 102 |
arcs[n].next_in = (*_nodes)[v].first_in; |
| 103 | 103 |
if ((*_nodes)[v].first_in != -1) {
|
| 104 | 104 |
arcs[(*_nodes)[v].first_in].prev_in = n; |
| 105 | 105 |
} |
| 106 | 106 |
(*_nodes)[v].first_in = n; |
| 107 | 107 |
arcs[n].next_out = (*_nodes)[u].first_out; |
| 108 | 108 |
if ((*_nodes)[u].first_out != -1) {
|
| 109 | 109 |
arcs[(*_nodes)[u].first_out].prev_out = n; |
| 110 | 110 |
} |
| 111 | 111 |
(*_nodes)[u].first_out = n; |
| 112 | 112 |
arcs[n].source = u; |
| 113 | 113 |
arcs[n].target = v; |
| 114 | 114 |
return Arc(n); |
| 115 | 115 |
} |
| 116 | 116 |
|
| 117 | 117 |
void erase(const Arc& arc) {
|
| 118 | 118 |
int n = arc.id; |
| 119 | 119 |
if (arcs[n].prev_in != -1) {
|
| 120 | 120 |
arcs[arcs[n].prev_in].next_in = arcs[n].next_in; |
| 121 | 121 |
} else {
|
| 122 | 122 |
(*_nodes)[arcs[n].target].first_in = arcs[n].next_in; |
| 123 | 123 |
} |
| 124 | 124 |
if (arcs[n].next_in != -1) {
|
| 125 | 125 |
arcs[arcs[n].next_in].prev_in = arcs[n].prev_in; |
| 126 | 126 |
} |
| 127 | 127 |
|
| 128 | 128 |
if (arcs[n].prev_out != -1) {
|
| 129 | 129 |
arcs[arcs[n].prev_out].next_out = arcs[n].next_out; |
| 130 | 130 |
} else {
|
| 131 | 131 |
(*_nodes)[arcs[n].source].first_out = arcs[n].next_out; |
| 132 | 132 |
} |
| 133 | 133 |
if (arcs[n].next_out != -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_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(); }
|
| 102 | 102 |
|
| 103 | 103 |
///Next arc of the tour |
| 104 | 104 |
|
| 105 | 105 |
///Next arc of the tour |
| 106 | 106 |
/// |
| 107 | 107 |
DiEulerIt &operator++() {
|
| 108 | 108 |
Node s=g.target(euler.front()); |
| 109 | 109 |
euler.pop_front(); |
| 110 | 110 |
typename std::list<Arc>::iterator next=euler.begin(); |
| 111 | 111 |
while(narc[s]!=INVALID) {
|
| 112 | 112 |
euler.insert(next,narc[s]); |
| 113 | 113 |
Node n=g.target(narc[s]); |
| 114 | 114 |
++narc[s]; |
| 115 | 115 |
s=n; |
| 116 | 116 |
} |
| 117 | 117 |
return *this; |
| 118 | 118 |
} |
| 119 | 119 |
///Postfix incrementation |
| 120 | 120 |
|
| 121 | 121 |
/// Postfix incrementation. |
| 122 | 122 |
/// |
| 123 | 123 |
///\warning This incrementation |
| 124 | 124 |
///returns an \c Arc, not a \ref DiEulerIt, as one may |
| 125 | 125 |
///expect. |
| 126 | 126 |
Arc operator++(int) |
| 127 | 127 |
{
|
| 128 | 128 |
Arc e=*this; |
| 129 | 129 |
++(*this); |
| 130 | 130 |
return e; |
| 131 | 131 |
} |
| 132 | 132 |
}; |
| 133 | 133 |
| 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. |
| 102 | 102 |
/// |
| 103 | 103 |
/// The algorithm calculates an optimal fractional matching and a |
| 104 | 104 |
/// barrier. The number of adjacents of any node set minus the size |
| 105 | 105 |
/// of node set is a lower bound on the uncovered nodes in the |
| 106 | 106 |
/// graph. For maximum matching a barrier is computed which |
| 107 | 107 |
/// maximizes this difference. |
| 108 | 108 |
/// |
| 109 | 109 |
/// The algorithm can be executed with the run() function. After it |
| 110 | 110 |
/// the matching (the primal solution) and the barrier (the dual |
| 111 | 111 |
/// solution) can be obtained using the query functions. |
| 112 | 112 |
/// |
| 113 | 113 |
/// The primal solution is multiplied by |
| 114 | 114 |
/// \ref MaxFractionalMatching::primalScale "2". |
| 115 | 115 |
/// |
| 116 | 116 |
/// \tparam GR The undirected graph type the algorithm runs on. |
| 117 | 117 |
#ifdef DOXYGEN |
| 118 | 118 |
template <typename GR, typename TR> |
| 119 | 119 |
#else |
| 120 | 120 |
template <typename GR, |
| 121 | 121 |
typename TR = MaxFractionalMatchingDefaultTraits<GR> > |
| 122 | 122 |
#endif |
| 123 | 123 |
class MaxFractionalMatching {
|
| 124 | 124 |
public: |
| 125 | 125 |
|
| 126 | 126 |
/// \brief The \ref MaxFractionalMatchingDefaultTraits "traits |
| 127 | 127 |
/// class" of the algorithm. |
| 128 | 128 |
typedef TR Traits; |
| 129 | 129 |
/// The type of the graph the algorithm runs on. |
| 130 | 130 |
typedef typename TR::Graph Graph; |
| 131 | 131 |
/// The type of the matching map. |
| 132 | 132 |
typedef typename TR::MatchingMap MatchingMap; |
| 133 | 133 |
/// The type of the 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 |
#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) {}
|
| 102 | 102 |
|
| 103 | 103 |
public: |
| 104 | 104 |
Arc() { }
|
| 105 | 105 |
Arc (Invalid) { _id = -1; }
|
| 106 | 106 |
bool operator==(const Arc arc) const {return _id == arc._id;}
|
| 107 | 107 |
bool operator!=(const Arc arc) const {return _id != arc._id;}
|
| 108 | 108 |
bool operator<(const Arc arc) const {return _id < arc._id;}
|
| 109 | 109 |
}; |
| 110 | 110 |
|
| 111 | 111 |
void first(Node& node) const {
|
| 112 | 112 |
node._id = _node_num - 1; |
| 113 | 113 |
} |
| 114 | 114 |
|
| 115 | 115 |
static void next(Node& node) {
|
| 116 | 116 |
--node._id; |
| 117 | 117 |
} |
| 118 | 118 |
|
| 119 | 119 |
void first(Arc& arc) const {
|
| 120 | 120 |
arc._id = _arc_num - 1; |
| 121 | 121 |
} |
| 122 | 122 |
|
| 123 | 123 |
static void next(Arc& arc) {
|
| 124 | 124 |
--arc._id; |
| 125 | 125 |
} |
| 126 | 126 |
|
| 127 | 127 |
void firstOut(Arc& arc, const Node& node) const {
|
| 128 | 128 |
arc._id = (node._id + 1) * _node_num - 1; |
| 129 | 129 |
} |
| 130 | 130 |
|
| 131 | 131 |
void nextOut(Arc& arc) const {
|
| 132 | 132 |
if (arc._id % _node_num == 0) arc._id = 0; |
| 133 | 133 |
--arc._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); |
| 102 | 102 |
} |
| 103 | 103 |
|
| 104 | 104 |
void GlpkBase::_eraseRow(int i) {
|
| 105 | 105 |
int ra[2]; |
| 106 | 106 |
ra[1] = i; |
| 107 | 107 |
glp_del_rows(lp, 1, ra); |
| 108 | 108 |
} |
| 109 | 109 |
|
| 110 | 110 |
void GlpkBase::_eraseColId(int i) {
|
| 111 | 111 |
cols.eraseIndex(i); |
| 112 | 112 |
cols.shiftIndices(i); |
| 113 | 113 |
} |
| 114 | 114 |
|
| 115 | 115 |
void GlpkBase::_eraseRowId(int i) {
|
| 116 | 116 |
rows.eraseIndex(i); |
| 117 | 117 |
rows.shiftIndices(i); |
| 118 | 118 |
} |
| 119 | 119 |
|
| 120 | 120 |
void GlpkBase::_getColName(int c, std::string& name) const {
|
| 121 | 121 |
const char *str = glp_get_col_name(lp, c); |
| 122 | 122 |
if (str) name = str; |
| 123 | 123 |
else name.clear(); |
| 124 | 124 |
} |
| 125 | 125 |
|
| 126 | 126 |
void GlpkBase::_setColName(int c, const std::string & name) {
|
| 127 | 127 |
glp_set_col_name(lp, c, const_cast<char*>(name.c_str())); |
| 128 | 128 |
|
| 129 | 129 |
} |
| 130 | 130 |
|
| 131 | 131 |
int GlpkBase::_colByName(const std::string& name) const {
|
| 132 | 132 |
int k = glp_find_col(lp, const_cast<char*>(name.c_str())); |
| 133 | 133 |
return k > 0 ? k : -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_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 |
|
| 102 | 102 |
virtual void _setRowUpperBound(int i, Value value); |
| 103 | 103 |
virtual Value _getRowUpperBound(int i) const; |
| 104 | 104 |
|
| 105 | 105 |
virtual void _setObjCoeffs(ExprIterator b, ExprIterator e); |
| 106 | 106 |
virtual void _getObjCoeffs(InsertIterator b) const; |
| 107 | 107 |
|
| 108 | 108 |
virtual void _setObjCoeff(int i, Value obj_coef); |
| 109 | 109 |
virtual Value _getObjCoeff(int i) const; |
| 110 | 110 |
|
| 111 | 111 |
virtual void _setSense(Sense); |
| 112 | 112 |
virtual Sense _getSense() const; |
| 113 | 113 |
|
| 114 | 114 |
virtual void _clear(); |
| 115 | 115 |
|
| 116 | 116 |
virtual void _messageLevel(MessageLevel level); |
| 117 | 117 |
|
| 118 | 118 |
private: |
| 119 | 119 |
|
| 120 | 120 |
static void freeEnv(); |
| 121 | 121 |
|
| 122 | 122 |
struct FreeEnvHelper {
|
| 123 | 123 |
~FreeEnvHelper() {
|
| 124 | 124 |
freeEnv(); |
| 125 | 125 |
} |
| 126 | 126 |
}; |
| 127 | 127 |
|
| 128 | 128 |
static FreeEnvHelper freeEnvHelper; |
| 129 | 129 |
|
| 130 | 130 |
protected: |
| 131 | 131 |
|
| 132 | 132 |
int _message_level; |
| 133 | 133 |
|
| 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 |
|
| 102 | 102 |
void destroyStructures() {
|
| 103 | 103 |
if (_pred) {
|
| 104 | 104 |
delete _pred; |
| 105 | 105 |
} |
| 106 | 106 |
if (_weight) {
|
| 107 | 107 |
delete _weight; |
| 108 | 108 |
} |
| 109 | 109 |
if (_order) {
|
| 110 | 110 |
delete _order; |
| 111 | 111 |
} |
| 112 | 112 |
} |
| 113 | 113 |
|
| 114 | 114 |
public: |
| 115 | 115 |
|
| 116 | 116 |
/// \brief Constructor |
| 117 | 117 |
/// |
| 118 | 118 |
/// Constructor. |
| 119 | 119 |
/// \param graph The undirected graph the algorithm runs on. |
| 120 | 120 |
/// \param capacity The edge capacity map. |
| 121 | 121 |
GomoryHu(const Graph& graph, const Capacity& capacity) |
| 122 | 122 |
: _graph(graph), _capacity(capacity), |
| 123 | 123 |
_pred(0), _weight(0), _order(0) |
| 124 | 124 |
{
|
| 125 | 125 |
checkConcept<concepts::ReadMap<Edge, Value>, Capacity>(); |
| 126 | 126 |
} |
| 127 | 127 |
|
| 128 | 128 |
|
| 129 | 129 |
/// \brief Destructor |
| 130 | 130 |
/// |
| 131 | 131 |
/// Destructor. |
| 132 | 132 |
~GomoryHu() {
|
| 133 | 133 |
destroyStructures(); |
| 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 |
|
| 102 | 102 |
bool _drawArrows; |
| 103 | 103 |
double _arrowLength, _arrowWidth; |
| 104 | 104 |
|
| 105 | 105 |
bool _showNodes, _showArcs; |
| 106 | 106 |
|
| 107 | 107 |
bool _enableParallel; |
| 108 | 108 |
double _parArcDist; |
| 109 | 109 |
|
| 110 | 110 |
bool _showNodeText; |
| 111 | 111 |
ConstMap<typename Graph::Node,bool > _nodeTexts; |
| 112 | 112 |
double _nodeTextSize; |
| 113 | 113 |
|
| 114 | 114 |
bool _showNodePsText; |
| 115 | 115 |
ConstMap<typename Graph::Node,bool > _nodePsTexts; |
| 116 | 116 |
char *_nodePsTextsPreamble; |
| 117 | 117 |
|
| 118 | 118 |
bool _undirected; |
| 119 | 119 |
|
| 120 | 120 |
bool _pleaseRemoveOsStream; |
| 121 | 121 |
|
| 122 | 122 |
bool _scaleToA4; |
| 123 | 123 |
|
| 124 | 124 |
std::string _title; |
| 125 | 125 |
std::string _copyright; |
| 126 | 126 |
|
| 127 | 127 |
enum NodeTextColorType |
| 128 | 128 |
{ DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType;
|
| 129 | 129 |
ConstMap<typename Graph::Node,Color > _nodeTextColors; |
| 130 | 130 |
|
| 131 | 131 |
bool _autoNodeScale; |
| 132 | 132 |
bool _autoArcWidthScale; |
| 133 | 133 |
| 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; |
| 102 | 102 |
|
| 103 | 103 |
// Bucketing structure |
| 104 | 104 |
std::vector<Node> _first, _last; |
| 105 | 105 |
typename Digraph::template NodeMap<Node>* _next; |
| 106 | 106 |
typename Digraph::template NodeMap<Node>* _prev; |
| 107 | 107 |
typename Digraph::template NodeMap<bool>* _active; |
| 108 | 108 |
typename Digraph::template NodeMap<int>* _bucket; |
| 109 | 109 |
|
| 110 | 110 |
std::vector<bool> _dormant; |
| 111 | 111 |
|
| 112 | 112 |
std::list<std::list<int> > _sets; |
| 113 | 113 |
std::list<int>::iterator _highest; |
| 114 | 114 |
|
| 115 | 115 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
| 116 | 116 |
ExcessMap* _excess; |
| 117 | 117 |
|
| 118 | 118 |
typedef typename Digraph::template NodeMap<bool> SourceSetMap; |
| 119 | 119 |
SourceSetMap* _source_set; |
| 120 | 120 |
|
| 121 | 121 |
Value _min_cut; |
| 122 | 122 |
|
| 123 | 123 |
typedef typename Digraph::template NodeMap<bool> MinCutMap; |
| 124 | 124 |
MinCutMap* _min_cut_map; |
| 125 | 125 |
|
| 126 | 126 |
Tolerance _tolerance; |
| 127 | 127 |
|
| 128 | 128 |
public: |
| 129 | 129 |
|
| 130 | 130 |
/// \brief Constructor |
| 131 | 131 |
/// |
| 132 | 132 |
/// Constructor of the algorithm class. |
| 133 | 133 |
HaoOrlin(const Digraph& graph, const CapacityMap& capacity, |
| 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. |
| 102 | 102 |
/// It is an improved version of \ref Karp "Karp"'s original algorithm, |
| 103 | 103 |
/// it applies an efficient early termination scheme. |
| 104 | 104 |
/// It runs in time O(ne) and uses space O(n<sup>2</sup>+e). |
| 105 | 105 |
/// |
| 106 | 106 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 107 | 107 |
/// \tparam CM The type of the cost map. The default |
| 108 | 108 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 109 | 109 |
/// \tparam TR The traits class that defines various types used by the |
| 110 | 110 |
/// algorithm. By default, it is \ref HartmannOrlinMmcDefaultTraits |
| 111 | 111 |
/// "HartmannOrlinMmcDefaultTraits<GR, CM>". |
| 112 | 112 |
/// In most cases, this parameter should not be set directly, |
| 113 | 113 |
/// consider to use the named template parameters instead. |
| 114 | 114 |
#ifdef DOXYGEN |
| 115 | 115 |
template <typename GR, typename CM, typename TR> |
| 116 | 116 |
#else |
| 117 | 117 |
template < typename GR, |
| 118 | 118 |
typename CM = typename GR::template ArcMap<int>, |
| 119 | 119 |
typename TR = HartmannOrlinMmcDefaultTraits<GR, CM> > |
| 120 | 120 |
#endif |
| 121 | 121 |
class HartmannOrlinMmc |
| 122 | 122 |
{
|
| 123 | 123 |
public: |
| 124 | 124 |
|
| 125 | 125 |
/// The type of the digraph |
| 126 | 126 |
typedef typename TR::Digraph Digraph; |
| 127 | 127 |
/// The type of the cost map |
| 128 | 128 |
typedef typename TR::CostMap CostMap; |
| 129 | 129 |
/// The type of the arc costs |
| 130 | 130 |
typedef typename TR::Cost Cost; |
| 131 | 131 |
|
| 132 | 132 |
/// \brief The large cost type |
| 133 | 133 |
/// |
| 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. |
| 102 | 102 |
/// This class provides the most efficient algorithm for the |
| 103 | 103 |
/// minimum mean cycle problem, though the best known theoretical |
| 104 | 104 |
/// bound on its running time is exponential. |
| 105 | 105 |
/// |
| 106 | 106 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 107 | 107 |
/// \tparam CM The type of the cost map. The default |
| 108 | 108 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 109 | 109 |
/// \tparam TR The traits class that defines various types used by the |
| 110 | 110 |
/// algorithm. By default, it is \ref HowardMmcDefaultTraits |
| 111 | 111 |
/// "HowardMmcDefaultTraits<GR, CM>". |
| 112 | 112 |
/// In most cases, this parameter should not be set directly, |
| 113 | 113 |
/// consider to use the named template parameters instead. |
| 114 | 114 |
#ifdef DOXYGEN |
| 115 | 115 |
template <typename GR, typename CM, typename TR> |
| 116 | 116 |
#else |
| 117 | 117 |
template < typename GR, |
| 118 | 118 |
typename CM = typename GR::template ArcMap<int>, |
| 119 | 119 |
typename TR = HowardMmcDefaultTraits<GR, CM> > |
| 120 | 120 |
#endif |
| 121 | 121 |
class HowardMmc |
| 122 | 122 |
{
|
| 123 | 123 |
public: |
| 124 | 124 |
|
| 125 | 125 |
/// The type of the digraph |
| 126 | 126 |
typedef typename TR::Digraph Digraph; |
| 127 | 127 |
/// The type of the cost map |
| 128 | 128 |
typedef typename TR::CostMap CostMap; |
| 129 | 129 |
/// The type of the arc costs |
| 130 | 130 |
typedef typename TR::Cost Cost; |
| 131 | 131 |
|
| 132 | 132 |
/// \brief The large cost type |
| 133 | 133 |
/// |
| 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. |
| 102 | 102 |
/// It runs in time O(ne) and uses space O(n<sup>2</sup>+e). |
| 103 | 103 |
/// |
| 104 | 104 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 105 | 105 |
/// \tparam CM The type of the cost map. The default |
| 106 | 106 |
/// map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 107 | 107 |
/// \tparam TR The traits class that defines various types used by the |
| 108 | 108 |
/// algorithm. By default, it is \ref KarpMmcDefaultTraits |
| 109 | 109 |
/// "KarpMmcDefaultTraits<GR, CM>". |
| 110 | 110 |
/// In most cases, this parameter should not be set directly, |
| 111 | 111 |
/// consider to use the named template parameters instead. |
| 112 | 112 |
#ifdef DOXYGEN |
| 113 | 113 |
template <typename GR, typename CM, typename TR> |
| 114 | 114 |
#else |
| 115 | 115 |
template < typename GR, |
| 116 | 116 |
typename CM = typename GR::template ArcMap<int>, |
| 117 | 117 |
typename TR = KarpMmcDefaultTraits<GR, CM> > |
| 118 | 118 |
#endif |
| 119 | 119 |
class KarpMmc |
| 120 | 120 |
{
|
| 121 | 121 |
public: |
| 122 | 122 |
|
| 123 | 123 |
/// The type of the digraph |
| 124 | 124 |
typedef typename TR::Digraph Digraph; |
| 125 | 125 |
/// The type of the cost map |
| 126 | 126 |
typedef typename TR::CostMap CostMap; |
| 127 | 127 |
/// The type of the arc costs |
| 128 | 128 |
typedef typename TR::Cost Cost; |
| 129 | 129 |
|
| 130 | 130 |
/// \brief The large cost type |
| 131 | 131 |
/// |
| 132 | 132 |
/// The large cost type used for internal computations. |
| 133 | 133 |
/// By default, it is \c long \c long if the \c Cost type is integer, |
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)