| 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) {
|
| 134 | 134 |
ReverseDigraph<const ListDigraph> rg(g); |
| 135 | 135 |
return algorithm2(rg); |
| 136 | 136 |
} |
| 137 | 137 |
\endcode |
| 138 | 138 |
*/ |
| 139 | 139 |
|
| 140 | 140 |
/** |
| 141 | 141 |
@defgroup maps Maps |
| 142 | 142 |
@ingroup datas |
| 143 | 143 |
\brief Map structures implemented in LEMON. |
| 144 | 144 |
|
| 145 | 145 |
This group contains the map structures implemented in LEMON. |
| 146 | 146 |
|
| 147 | 147 |
LEMON provides several special purpose maps and map adaptors that e.g. combine |
| 148 | 148 |
new maps from existing ones. |
| 149 | 149 |
|
| 150 | 150 |
<b>See also:</b> \ref map_concepts "Map Concepts". |
| 151 | 151 |
*/ |
| 152 | 152 |
|
| 153 | 153 |
/** |
| 154 | 154 |
@defgroup graph_maps Graph Maps |
| 155 | 155 |
@ingroup maps |
| 156 | 156 |
\brief Special graph-related maps. |
| 157 | 157 |
|
| 158 | 158 |
This group contains maps that are specifically designed to assign |
| 159 | 159 |
values to the nodes and arcs/edges of graphs. |
| 160 | 160 |
|
| 161 | 161 |
If you are looking for the standard graph maps (\c NodeMap, \c ArcMap, |
| 162 | 162 |
\c EdgeMap), see the \ref graph_concepts "Graph Structure Concepts". |
| 163 | 163 |
*/ |
| 164 | 164 |
|
| 165 | 165 |
/** |
| 166 | 166 |
\defgroup map_adaptors Map Adaptors |
| 167 | 167 |
\ingroup maps |
| 168 | 168 |
\brief Tools to create new maps from existing ones |
| 169 | 169 |
|
| 170 | 170 |
This group contains map adaptors that are used to create "implicit" |
| 171 | 171 |
maps from other maps. |
| 172 | 172 |
|
| 173 | 173 |
Most of them are \ref concepts::ReadMap "read-only maps". |
| 174 | 174 |
They can make arithmetic and logical operations between one or two maps |
| 175 | 175 |
(negation, shifting, addition, multiplication, logical 'and', 'or', |
| 176 | 176 |
'not' etc.) or e.g. convert a map to another one of different Value type. |
| 177 | 177 |
|
| 178 | 178 |
The typical usage of this classes is passing implicit maps to |
| 179 | 179 |
algorithms. If a function type algorithm is called then the function |
| 180 | 180 |
type map adaptors can be used comfortable. For example let's see the |
| 181 | 181 |
usage of map adaptors with the \c graphToEps() function. |
| 182 | 182 |
\code |
| 183 | 183 |
Color nodeColor(int deg) {
|
| 184 | 184 |
if (deg >= 2) {
|
| 185 | 185 |
return Color(0.5, 0.0, 0.5); |
| 186 | 186 |
} else if (deg == 1) {
|
| 187 | 187 |
return Color(1.0, 0.5, 1.0); |
| 188 | 188 |
} else {
|
| 189 | 189 |
return Color(0.0, 0.0, 0.0); |
| 190 | 190 |
} |
| 191 | 191 |
} |
| 192 | 192 |
|
| 193 | 193 |
Digraph::NodeMap<int> degree_map(graph); |
| 194 | 194 |
|
| 195 | 195 |
graphToEps(graph, "graph.eps") |
| 196 | 196 |
.coords(coords).scaleToA4().undirected() |
| 197 | 197 |
.nodeColors(composeMap(functorToMap(nodeColor), degree_map)) |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
/** |
| 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 |
| 134 | 134 |
for the sake of convenience. |
| 135 | 135 |
|
| 136 | 136 |
Note that the optimality conditions for this supply constraint type are |
| 137 | 137 |
slightly differ from the conditions that are discussed for the GEQ form, |
| 138 | 138 |
namely the potentials have to be non-negative instead of non-positive. |
| 139 | 139 |
An \f$f: A\rightarrow\mathbf{R}\f$ feasible solution of this problem
|
| 140 | 140 |
is optimal if and only if for some \f$\pi: V\rightarrow\mathbf{R}\f$
|
| 141 | 141 |
node potentials the following conditions hold. |
| 142 | 142 |
|
| 143 | 143 |
- For all \f$uv\in A\f$ arcs: |
| 144 | 144 |
- if \f$cost^\pi(uv)>0\f$, then \f$f(uv)=lower(uv)\f$; |
| 145 | 145 |
- if \f$lower(uv)<f(uv)<upper(uv)\f$, then \f$cost^\pi(uv)=0\f$; |
| 146 | 146 |
- if \f$cost^\pi(uv)<0\f$, then \f$f(uv)=upper(uv)\f$. |
| 147 | 147 |
- For all \f$u\in V\f$ nodes: |
| 148 | 148 |
- \f$\pi(u)\geq 0\f$; |
| 149 | 149 |
- if \f$\sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \neq sup(u)\f$,
|
| 150 | 150 |
then \f$\pi(u)=0\f$. |
| 151 | 151 |
|
| 152 | 152 |
*/ |
| 153 | 153 |
} |
| 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> |
| 134 | 134 |
class ArcMap : public DGR::template ArcMap<V> {
|
| 135 | 135 |
typedef typename DGR::template ArcMap<V> Parent; |
| 136 | 136 |
|
| 137 | 137 |
public: |
| 138 | 138 |
explicit ArcMap(const DigraphAdaptorBase<DGR>& adaptor) |
| 139 | 139 |
: Parent(*adaptor._digraph) {}
|
| 140 | 140 |
ArcMap(const DigraphAdaptorBase<DGR>& adaptor, const V& value) |
| 141 | 141 |
: Parent(*adaptor._digraph, value) {}
|
| 142 | 142 |
|
| 143 | 143 |
private: |
| 144 | 144 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 145 | 145 |
return operator=<ArcMap>(cmap); |
| 146 | 146 |
} |
| 147 | 147 |
|
| 148 | 148 |
template <typename CMap> |
| 149 | 149 |
ArcMap& operator=(const CMap& cmap) {
|
| 150 | 150 |
Parent::operator=(cmap); |
| 151 | 151 |
return *this; |
| 152 | 152 |
} |
| 153 | 153 |
|
| 154 | 154 |
}; |
| 155 | 155 |
|
| 156 | 156 |
}; |
| 157 | 157 |
|
| 158 | 158 |
template<typename GR> |
| 159 | 159 |
class GraphAdaptorBase {
|
| 160 | 160 |
public: |
| 161 | 161 |
typedef GR Graph; |
| 162 | 162 |
|
| 163 | 163 |
protected: |
| 164 | 164 |
GR* _graph; |
| 165 | 165 |
|
| 166 | 166 |
GraphAdaptorBase() : _graph(0) {}
|
| 167 | 167 |
|
| 168 | 168 |
void initialize(GR& graph) { _graph = &graph; }
|
| 169 | 169 |
|
| 170 | 170 |
public: |
| 171 | 171 |
GraphAdaptorBase(GR& graph) : _graph(&graph) {}
|
| 172 | 172 |
|
| 173 | 173 |
typedef typename GR::Node Node; |
| 174 | 174 |
typedef typename GR::Arc Arc; |
| 175 | 175 |
typedef typename GR::Edge Edge; |
| 176 | 176 |
|
| 177 | 177 |
void first(Node& i) const { _graph->first(i); }
|
| 178 | 178 |
void first(Arc& i) const { _graph->first(i); }
|
| 179 | 179 |
void first(Edge& i) const { _graph->first(i); }
|
| 180 | 180 |
void firstIn(Arc& i, const Node& n) const { _graph->firstIn(i, n); }
|
| 181 | 181 |
void firstOut(Arc& i, const Node& n ) const { _graph->firstOut(i, n); }
|
| 182 | 182 |
void firstInc(Edge &i, bool &d, const Node &n) const {
|
| 183 | 183 |
_graph->firstInc(i, d, n); |
| 184 | 184 |
} |
| 185 | 185 |
|
| 186 | 186 |
void next(Node& i) const { _graph->next(i); }
|
| 187 | 187 |
void next(Arc& i) const { _graph->next(i); }
|
| 188 | 188 |
void next(Edge& i) const { _graph->next(i); }
|
| 189 | 189 |
void nextIn(Arc& i) const { _graph->nextIn(i); }
|
| 190 | 190 |
void nextOut(Arc& i) const { _graph->nextOut(i); }
|
| 191 | 191 |
void nextInc(Edge &i, bool &d) const { _graph->nextInc(i, d); }
|
| 192 | 192 |
|
| 193 | 193 |
Node u(const Edge& e) const { return _graph->u(e); }
|
| 194 | 194 |
Node v(const Edge& e) const { return _graph->v(e); }
|
| 195 | 195 |
|
| 196 | 196 |
Node source(const Arc& a) const { return _graph->source(a); }
|
| 197 | 197 |
Node target(const Arc& a) const { return _graph->target(a); }
|
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 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; |
| 134 | 134 |
p.type=INTEGER; |
| 135 | 135 |
p.mandatory=obl; |
| 136 | 136 |
_opts[name]=p; |
| 137 | 137 |
return *this; |
| 138 | 138 |
} |
| 139 | 139 |
|
| 140 | 140 |
ArgParser &ArgParser::refOption(const std::string &name, |
| 141 | 141 |
const std::string &help, |
| 142 | 142 |
double &ref, bool obl) |
| 143 | 143 |
{
|
| 144 | 144 |
ParData p; |
| 145 | 145 |
p.double_p=&ref; |
| 146 | 146 |
p.self_delete=false; |
| 147 | 147 |
p.help=help; |
| 148 | 148 |
p.type=DOUBLE; |
| 149 | 149 |
p.mandatory=obl; |
| 150 | 150 |
_opts[name]=p; |
| 151 | 151 |
return *this; |
| 152 | 152 |
} |
| 153 | 153 |
|
| 154 | 154 |
ArgParser &ArgParser::refOption(const std::string &name, |
| 155 | 155 |
const std::string &help, |
| 156 | 156 |
bool &ref, bool obl) |
| 157 | 157 |
{
|
| 158 | 158 |
ParData p; |
| 159 | 159 |
p.bool_p=&ref; |
| 160 | 160 |
p.self_delete=false; |
| 161 | 161 |
p.help=help; |
| 162 | 162 |
p.type=BOOL; |
| 163 | 163 |
p.mandatory=obl; |
| 164 | 164 |
_opts[name]=p; |
| 165 | 165 |
|
| 166 | 166 |
ref = false; |
| 167 | 167 |
|
| 168 | 168 |
return *this; |
| 169 | 169 |
} |
| 170 | 170 |
|
| 171 | 171 |
ArgParser &ArgParser::refOption(const std::string &name, |
| 172 | 172 |
const std::string &help, |
| 173 | 173 |
std::string &ref, bool obl) |
| 174 | 174 |
{
|
| 175 | 175 |
ParData p; |
| 176 | 176 |
p.string_p=&ref; |
| 177 | 177 |
p.self_delete=false; |
| 178 | 178 |
p.help=help; |
| 179 | 179 |
p.type=STRING; |
| 180 | 180 |
p.mandatory=obl; |
| 181 | 181 |
_opts[name]=p; |
| 182 | 182 |
return *this; |
| 183 | 183 |
} |
| 184 | 184 |
|
| 185 | 185 |
ArgParser &ArgParser::funcOption(const std::string &name, |
| 186 | 186 |
const std::string &help, |
| 187 | 187 |
void (*func)(void *),void *data) |
| 188 | 188 |
{
|
| 189 | 189 |
ParData p; |
| 190 | 190 |
p.func_p.p=func; |
| 191 | 191 |
p.func_p.data=data; |
| 192 | 192 |
p.self_delete=false; |
| 193 | 193 |
p.help=help; |
| 194 | 194 |
p.type=FUNC; |
| 195 | 195 |
p.mandatory=false; |
| 196 | 196 |
_opts[name]=p; |
| 197 | 197 |
return *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_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 |
{
|
| 134 | 134 |
std::string name; |
| 135 | 135 |
std::string help; |
| 136 | 136 |
OtherArg(std::string n, std::string h) :name(n), help(h) {}
|
| 137 | 137 |
|
| 138 | 138 |
}; |
| 139 | 139 |
|
| 140 | 140 |
std::vector<OtherArg> _others_help; |
| 141 | 141 |
std::vector<std::string> _file_args; |
| 142 | 142 |
std::string _command_name; |
| 143 | 143 |
|
| 144 | 144 |
|
| 145 | 145 |
private: |
| 146 | 146 |
//Bind a function to an option. |
| 147 | 147 |
|
| 148 | 148 |
//\param name The name of the option. The leading '-' must be omitted. |
| 149 | 149 |
//\param help A help string. |
| 150 | 150 |
//\retval func The function to be called when the option is given. It |
| 151 | 151 |
// must be of type "void f(void *)" |
| 152 | 152 |
//\param data Data to be passed to \c func |
| 153 | 153 |
ArgParser &funcOption(const std::string &name, |
| 154 | 154 |
const std::string &help, |
| 155 | 155 |
void (*func)(void *),void *data); |
| 156 | 156 |
|
| 157 | 157 |
bool _exit_on_problems; |
| 158 | 158 |
|
| 159 | 159 |
void _terminate(ArgParserException::Reason reason) const; |
| 160 | 160 |
|
| 161 | 161 |
public: |
| 162 | 162 |
|
| 163 | 163 |
///Constructor |
| 164 | 164 |
ArgParser(int argc, const char * const *argv); |
| 165 | 165 |
|
| 166 | 166 |
~ArgParser(); |
| 167 | 167 |
|
| 168 | 168 |
///\name Options |
| 169 | 169 |
/// |
| 170 | 170 |
|
| 171 | 171 |
///@{
|
| 172 | 172 |
|
| 173 | 173 |
///Add a new integer type option |
| 174 | 174 |
|
| 175 | 175 |
///Add a new integer type option. |
| 176 | 176 |
///\param name The name of the option. The leading '-' must be omitted. |
| 177 | 177 |
///\param help A help string. |
| 178 | 178 |
///\param value A default value for the option. |
| 179 | 179 |
///\param obl Indicate if the option is mandatory. |
| 180 | 180 |
ArgParser &intOption(const std::string &name, |
| 181 | 181 |
const std::string &help, |
| 182 | 182 |
int value=0, bool obl=false); |
| 183 | 183 |
|
| 184 | 184 |
///Add a new floating point type option |
| 185 | 185 |
|
| 186 | 186 |
///Add a new floating point type option. |
| 187 | 187 |
///\param name The name of the option. The leading '-' must be omitted. |
| 188 | 188 |
///\param help A help string. |
| 189 | 189 |
///\param value A default value for the option. |
| 190 | 190 |
///\param obl Indicate if the option is mandatory. |
| 191 | 191 |
ArgParser &doubleOption(const std::string &name, |
| 192 | 192 |
const std::string &help, |
| 193 | 193 |
double value=0, bool obl=false); |
| 194 | 194 |
|
| 195 | 195 |
///Add a new bool type option |
| 196 | 196 |
|
| 197 | 197 |
///Add a new bool type option. |
| 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 |
}; |
| 134 | 134 |
|
| 135 | 135 |
/// \brief Default traits class of BellmanFord class. |
| 136 | 136 |
/// |
| 137 | 137 |
/// Default traits class of BellmanFord class. |
| 138 | 138 |
/// \param GR The type of the digraph. |
| 139 | 139 |
/// \param LEN The type of the length map. |
| 140 | 140 |
template<typename GR, typename LEN> |
| 141 | 141 |
struct BellmanFordDefaultTraits {
|
| 142 | 142 |
/// The type of the digraph the algorithm runs on. |
| 143 | 143 |
typedef GR Digraph; |
| 144 | 144 |
|
| 145 | 145 |
/// \brief The type of the map that stores the arc lengths. |
| 146 | 146 |
/// |
| 147 | 147 |
/// The type of the map that stores the arc lengths. |
| 148 | 148 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
| 149 | 149 |
typedef LEN LengthMap; |
| 150 | 150 |
|
| 151 | 151 |
/// The type of the arc lengths. |
| 152 | 152 |
typedef typename LEN::Value Value; |
| 153 | 153 |
|
| 154 | 154 |
/// \brief Operation traits for Bellman-Ford algorithm. |
| 155 | 155 |
/// |
| 156 | 156 |
/// It defines the used operations and the infinity value for the |
| 157 | 157 |
/// given \c Value type. |
| 158 | 158 |
/// \see BellmanFordDefaultOperationTraits, |
| 159 | 159 |
/// BellmanFordToleranceOperationTraits |
| 160 | 160 |
typedef BellmanFordDefaultOperationTraits<Value> OperationTraits; |
| 161 | 161 |
|
| 162 | 162 |
/// \brief The type of the map that stores the last arcs of the |
| 163 | 163 |
/// shortest paths. |
| 164 | 164 |
/// |
| 165 | 165 |
/// The type of the map that stores the last |
| 166 | 166 |
/// arcs of the shortest paths. |
| 167 | 167 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 168 | 168 |
typedef typename GR::template NodeMap<typename GR::Arc> PredMap; |
| 169 | 169 |
|
| 170 | 170 |
/// \brief Instantiates a \c PredMap. |
| 171 | 171 |
/// |
| 172 | 172 |
/// This function instantiates a \ref PredMap. |
| 173 | 173 |
/// \param g is the digraph to which we would like to define the |
| 174 | 174 |
/// \ref PredMap. |
| 175 | 175 |
static PredMap *createPredMap(const GR& g) {
|
| 176 | 176 |
return new PredMap(g); |
| 177 | 177 |
} |
| 178 | 178 |
|
| 179 | 179 |
/// \brief The type of the map that stores the distances of the nodes. |
| 180 | 180 |
/// |
| 181 | 181 |
/// The type of the map that stores the distances of the nodes. |
| 182 | 182 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 183 | 183 |
typedef typename GR::template NodeMap<typename LEN::Value> DistMap; |
| 184 | 184 |
|
| 185 | 185 |
/// \brief Instantiates a \c DistMap. |
| 186 | 186 |
/// |
| 187 | 187 |
/// This function instantiates a \ref DistMap. |
| 188 | 188 |
/// \param g is the digraph to which we would like to define the |
| 189 | 189 |
/// \ref DistMap. |
| 190 | 190 |
static DistMap *createDistMap(const GR& g) {
|
| 191 | 191 |
return new DistMap(g); |
| 192 | 192 |
} |
| 193 | 193 |
|
| 194 | 194 |
}; |
| 195 | 195 |
|
| 196 | 196 |
/// \brief %BellmanFord algorithm class. |
| 197 | 197 |
/// |
| 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()" |
| 403 | 405 |
///or \ref init(), an instance will be allocated automatically. |
| 404 | 406 |
///The destructor deallocates this automatically allocated map, |
| 405 | 407 |
///of course. |
| 406 | 408 |
///\return <tt> (*this) </tt> |
| 407 | 409 |
Bfs &distMap(DistMap &m) |
| 408 | 410 |
{
|
| 409 | 411 |
if(local_dist) {
|
| 410 | 412 |
delete _dist; |
| 411 | 413 |
local_dist=false; |
| 412 | 414 |
} |
| 413 | 415 |
_dist = &m; |
| 414 | 416 |
return *this; |
| 415 | 417 |
} |
| 416 | 418 |
|
| 417 | 419 |
public: |
| 418 | 420 |
|
| 419 | 421 |
///\name Execution Control |
| 420 | 422 |
///The simplest way to execute the BFS algorithm is to use one of the |
| 421 | 423 |
///member functions called \ref run(Node) "run()".\n |
| 422 | 424 |
///If you need better control on the execution, you have to call |
| 423 | 425 |
///\ref init() first, then you can add several source nodes with |
| 424 | 426 |
///\ref addSource(). Finally the actual path computation can be |
| 425 | 427 |
///performed with one of the \ref start() functions. |
| 426 | 428 |
|
| 427 | 429 |
///@{
|
| 428 | 430 |
|
| 429 | 431 |
///\brief Initializes the internal data structures. |
| 430 | 432 |
/// |
| 431 | 433 |
///Initializes the internal data structures. |
| 432 | 434 |
void init() |
| 433 | 435 |
{
|
| 434 | 436 |
create_maps(); |
| 435 | 437 |
_queue.resize(countNodes(*G)); |
| 436 | 438 |
_queue_head=_queue_tail=0; |
| 437 | 439 |
_curr_dist=1; |
| 438 | 440 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
|
| 439 | 441 |
_pred->set(u,INVALID); |
| 440 | 442 |
_reached->set(u,false); |
| 441 | 443 |
_processed->set(u,false); |
| 442 | 444 |
} |
| 443 | 445 |
} |
| 444 | 446 |
|
| 445 | 447 |
///Adds a new source node. |
| 446 | 448 |
|
| 447 | 449 |
///Adds a new source node to the set of nodes to be processed. |
| 448 | 450 |
/// |
| 449 | 451 |
void addSource(Node s) |
| 450 | 452 |
{
|
| 451 | 453 |
if(!(*_reached)[s]) |
| 452 | 454 |
{
|
| 453 | 455 |
_reached->set(s,true); |
| 454 | 456 |
_pred->set(s,INVALID); |
| 455 | 457 |
_dist->set(s,0); |
| 456 | 458 |
_queue[_queue_head++]=s; |
| 457 | 459 |
_queue_next_dist=_queue_head; |
| 458 | 460 |
} |
| 459 | 461 |
} |
| 460 | 462 |
|
| 461 | 463 |
///Processes the next node. |
| 462 | 464 |
|
| 463 | 465 |
///Processes the next node. |
| 464 | 466 |
/// |
| 465 | 467 |
///\return The processed node. |
| 466 | 468 |
/// |
| ... | ... |
@@ -683,385 +685,386 @@ |
| 683 | 685 |
} |
| 684 | 686 |
|
| 685 | 687 |
///Finds the shortest path between \c s and \c t. |
| 686 | 688 |
|
| 687 | 689 |
///This method runs the %BFS algorithm from node \c s |
| 688 | 690 |
///in order to compute the shortest path to node \c t |
| 689 | 691 |
///(it stops searching when \c t is processed). |
| 690 | 692 |
/// |
| 691 | 693 |
///\return \c true if \c t is reachable form \c s. |
| 692 | 694 |
/// |
| 693 | 695 |
///\note Apart from the return value, <tt>b.run(s,t)</tt> is just a |
| 694 | 696 |
///shortcut of the following code. |
| 695 | 697 |
///\code |
| 696 | 698 |
/// b.init(); |
| 697 | 699 |
/// b.addSource(s); |
| 698 | 700 |
/// b.start(t); |
| 699 | 701 |
///\endcode |
| 700 | 702 |
bool run(Node s,Node t) {
|
| 701 | 703 |
init(); |
| 702 | 704 |
addSource(s); |
| 703 | 705 |
start(t); |
| 704 | 706 |
return reached(t); |
| 705 | 707 |
} |
| 706 | 708 |
|
| 707 | 709 |
///Runs the algorithm to visit all nodes in the digraph. |
| 708 | 710 |
|
| 709 | 711 |
///This method runs the %BFS algorithm in order to visit all nodes |
| 710 | 712 |
///in the digraph. |
| 711 | 713 |
/// |
| 712 | 714 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
| 713 | 715 |
///\code |
| 714 | 716 |
/// b.init(); |
| 715 | 717 |
/// for (NodeIt n(gr); n != INVALID; ++n) {
|
| 716 | 718 |
/// if (!b.reached(n)) {
|
| 717 | 719 |
/// b.addSource(n); |
| 718 | 720 |
/// b.start(); |
| 719 | 721 |
/// } |
| 720 | 722 |
/// } |
| 721 | 723 |
///\endcode |
| 722 | 724 |
void run() {
|
| 723 | 725 |
init(); |
| 724 | 726 |
for (NodeIt n(*G); n != INVALID; ++n) {
|
| 725 | 727 |
if (!reached(n)) {
|
| 726 | 728 |
addSource(n); |
| 727 | 729 |
start(); |
| 728 | 730 |
} |
| 729 | 731 |
} |
| 730 | 732 |
} |
| 731 | 733 |
|
| 732 | 734 |
///@} |
| 733 | 735 |
|
| 734 | 736 |
///\name Query Functions |
| 735 | 737 |
///The results of the BFS algorithm can be obtained using these |
| 736 | 738 |
///functions.\n |
| 737 | 739 |
///Either \ref run(Node) "run()" or \ref start() should be called |
| 738 | 740 |
///before using them. |
| 739 | 741 |
|
| 740 | 742 |
///@{
|
| 741 | 743 |
|
| 742 | 744 |
///The shortest path to the given node. |
| 743 | 745 |
|
| 744 | 746 |
///Returns the shortest path to the given node from the root(s). |
| 745 | 747 |
/// |
| 746 | 748 |
///\warning \c t should be 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 |
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 |
|
| 1004 | 1007 |
///Runs BFS algorithm from the given source node. |
| 1005 | 1008 |
|
| 1006 | 1009 |
///This method runs BFS algorithm from node \c s |
| 1007 | 1010 |
///in order to compute the shortest path to each node. |
| 1008 | 1011 |
void run(Node s) |
| 1009 | 1012 |
{
|
| 1010 | 1013 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
| 1011 | 1014 |
if (Base::_pred) |
| 1012 | 1015 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 1013 | 1016 |
if (Base::_dist) |
| 1014 | 1017 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 1015 | 1018 |
if (Base::_reached) |
| 1016 | 1019 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
| 1017 | 1020 |
if (Base::_processed) |
| 1018 | 1021 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
| 1019 | 1022 |
if (s!=INVALID) |
| 1020 | 1023 |
alg.run(s); |
| 1021 | 1024 |
else |
| 1022 | 1025 |
alg.run(); |
| 1023 | 1026 |
} |
| 1024 | 1027 |
|
| 1025 | 1028 |
///Finds the shortest path between \c s and \c t. |
| 1026 | 1029 |
|
| 1027 | 1030 |
///This method runs BFS algorithm from node \c s |
| 1028 | 1031 |
///in order to compute the shortest path to node \c t |
| 1029 | 1032 |
///(it stops searching when \c t is processed). |
| 1030 | 1033 |
/// |
| 1031 | 1034 |
///\return \c true if \c t is reachable form \c s. |
| 1032 | 1035 |
bool run(Node s, Node t) |
| 1033 | 1036 |
{
|
| 1034 | 1037 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
| 1035 | 1038 |
if (Base::_pred) |
| 1036 | 1039 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
| 1037 | 1040 |
if (Base::_dist) |
| 1038 | 1041 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
| 1039 | 1042 |
if (Base::_reached) |
| 1040 | 1043 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
| 1041 | 1044 |
if (Base::_processed) |
| 1042 | 1045 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
| 1043 | 1046 |
alg.run(s,t); |
| 1044 | 1047 |
if (Base::_path) |
| 1045 | 1048 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
| 1046 | 1049 |
if (Base::_di) |
| 1047 | 1050 |
*Base::_di = alg.dist(t); |
| 1048 | 1051 |
return alg.reached(t); |
| 1049 | 1052 |
} |
| 1050 | 1053 |
|
| 1051 | 1054 |
///Runs BFS algorithm to visit all nodes in the digraph. |
| 1052 | 1055 |
|
| 1053 | 1056 |
///This method runs BFS algorithm in order to visit all nodes |
| 1054 | 1057 |
///in the digraph. |
| 1055 | 1058 |
void run() |
| 1056 | 1059 |
{
|
| 1057 | 1060 |
run(INVALID); |
| 1058 | 1061 |
} |
| 1059 | 1062 |
|
| 1060 | 1063 |
template<class T> |
| 1061 | 1064 |
struct SetPredMapBase : public Base {
|
| 1062 | 1065 |
typedef T PredMap; |
| 1063 | 1066 |
static PredMap *createPredMap(const Digraph &) { return 0; };
|
| 1064 | 1067 |
SetPredMapBase(const TR &b) : TR(b) {}
|
| 1065 | 1068 |
}; |
| 1066 | 1069 |
|
| 1067 | 1070 |
///\brief \ref named-templ-param "Named parameter" for setting |
| ... | ... |
@@ -1076,385 +1079,386 @@ |
| 1076 | 1079 |
return BfsWizard<SetPredMapBase<T> >(*this); |
| 1077 | 1080 |
} |
| 1078 | 1081 |
|
| 1079 | 1082 |
template<class T> |
| 1080 | 1083 |
struct SetReachedMapBase : public Base {
|
| 1081 | 1084 |
typedef T ReachedMap; |
| 1082 | 1085 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; };
|
| 1083 | 1086 |
SetReachedMapBase(const TR &b) : TR(b) {}
|
| 1084 | 1087 |
}; |
| 1085 | 1088 |
|
| 1086 | 1089 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 1087 | 1090 |
///the reached map. |
| 1088 | 1091 |
/// |
| 1089 | 1092 |
///\ref named-templ-param "Named parameter" function for setting |
| 1090 | 1093 |
///the map that indicates which nodes are reached. |
| 1091 | 1094 |
template<class T> |
| 1092 | 1095 |
BfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
| 1093 | 1096 |
{
|
| 1094 | 1097 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1095 | 1098 |
return BfsWizard<SetReachedMapBase<T> >(*this); |
| 1096 | 1099 |
} |
| 1097 | 1100 |
|
| 1098 | 1101 |
template<class T> |
| 1099 | 1102 |
struct SetDistMapBase : public Base {
|
| 1100 | 1103 |
typedef T DistMap; |
| 1101 | 1104 |
static DistMap *createDistMap(const Digraph &) { return 0; };
|
| 1102 | 1105 |
SetDistMapBase(const TR &b) : TR(b) {}
|
| 1103 | 1106 |
}; |
| 1104 | 1107 |
|
| 1105 | 1108 |
///\brief \ref named-templ-param "Named parameter" for setting |
| 1106 | 1109 |
///the distance map. |
| 1107 | 1110 |
/// |
| 1108 | 1111 |
///\ref named-templ-param "Named parameter" function for setting |
| 1109 | 1112 |
///the map that stores the distances of the nodes calculated |
| 1110 | 1113 |
///by the algorithm. |
| 1111 | 1114 |
template<class T> |
| 1112 | 1115 |
BfsWizard<SetDistMapBase<T> > distMap(const T &t) |
| 1113 | 1116 |
{
|
| 1114 | 1117 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1115 | 1118 |
return BfsWizard<SetDistMapBase<T> >(*this); |
| 1116 | 1119 |
} |
| 1117 | 1120 |
|
| 1118 | 1121 |
template<class T> |
| 1119 | 1122 |
struct SetProcessedMapBase : public Base {
|
| 1120 | 1123 |
typedef T ProcessedMap; |
| 1121 | 1124 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
|
| 1122 | 1125 |
SetProcessedMapBase(const TR &b) : TR(b) {}
|
| 1123 | 1126 |
}; |
| 1124 | 1127 |
|
| 1125 | 1128 |
///\brief \ref named-func-param "Named parameter" for setting |
| 1126 | 1129 |
///the processed map. |
| 1127 | 1130 |
/// |
| 1128 | 1131 |
///\ref named-templ-param "Named parameter" function for setting |
| 1129 | 1132 |
///the map that indicates which nodes are processed. |
| 1130 | 1133 |
template<class T> |
| 1131 | 1134 |
BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
| 1132 | 1135 |
{
|
| 1133 | 1136 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 1134 | 1137 |
return BfsWizard<SetProcessedMapBase<T> >(*this); |
| 1135 | 1138 |
} |
| 1136 | 1139 |
|
| 1137 | 1140 |
template<class T> |
| 1138 | 1141 |
struct SetPathBase : public Base {
|
| 1139 | 1142 |
typedef T Path; |
| 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. |
| 1397 | 1401 |
/// \param visitor The visitor object of the algorithm. |
| 1398 | 1402 |
BfsVisit(const Digraph& digraph, Visitor& visitor) |
| 1399 | 1403 |
: _digraph(&digraph), _visitor(&visitor), |
| 1400 | 1404 |
_reached(0), local_reached(false) {}
|
| 1401 | 1405 |
|
| 1402 | 1406 |
/// \brief Destructor. |
| 1403 | 1407 |
~BfsVisit() {
|
| 1404 | 1408 |
if(local_reached) delete _reached; |
| 1405 | 1409 |
} |
| 1406 | 1410 |
|
| 1407 | 1411 |
/// \brief Sets the map that indicates which nodes are reached. |
| 1408 | 1412 |
/// |
| 1409 | 1413 |
/// Sets the map that indicates which nodes are reached. |
| 1410 | 1414 |
/// If you don't use this function before calling \ref run(Node) "run()" |
| 1411 | 1415 |
/// or \ref init(), an instance will be allocated automatically. |
| 1412 | 1416 |
/// The destructor deallocates this automatically allocated map, |
| 1413 | 1417 |
/// of course. |
| 1414 | 1418 |
/// \return <tt> (*this) </tt> |
| 1415 | 1419 |
BfsVisit &reachedMap(ReachedMap &m) {
|
| 1416 | 1420 |
if(local_reached) {
|
| 1417 | 1421 |
delete _reached; |
| 1418 | 1422 |
local_reached = false; |
| 1419 | 1423 |
} |
| 1420 | 1424 |
_reached = &m; |
| 1421 | 1425 |
return *this; |
| 1422 | 1426 |
} |
| 1423 | 1427 |
|
| 1424 | 1428 |
public: |
| 1425 | 1429 |
|
| 1426 | 1430 |
/// \name Execution Control |
| 1427 | 1431 |
/// The simplest way to execute the BFS algorithm is to use one of the |
| 1428 | 1432 |
/// member functions called \ref run(Node) "run()".\n |
| 1429 | 1433 |
/// If you need better control on the execution, you have to call |
| 1430 | 1434 |
/// \ref init() first, then you can add several source nodes with |
| 1431 | 1435 |
/// \ref addSource(). Finally the actual path computation can be |
| 1432 | 1436 |
/// performed with one of the \ref start() functions. |
| 1433 | 1437 |
|
| 1434 | 1438 |
/// @{
|
| 1435 | 1439 |
|
| 1436 | 1440 |
/// \brief Initializes the internal data structures. |
| 1437 | 1441 |
/// |
| 1438 | 1442 |
/// Initializes the internal data structures. |
| 1439 | 1443 |
void init() {
|
| 1440 | 1444 |
create_maps(); |
| 1441 | 1445 |
_list.resize(countNodes(*_digraph)); |
| 1442 | 1446 |
_list_front = _list_back = -1; |
| 1443 | 1447 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
|
| 1444 | 1448 |
_reached->set(u, false); |
| 1445 | 1449 |
} |
| 1446 | 1450 |
} |
| 1447 | 1451 |
|
| 1448 | 1452 |
/// \brief Adds a new source node. |
| 1449 | 1453 |
/// |
| 1450 | 1454 |
/// Adds a new source node to the set of nodes to be processed. |
| 1451 | 1455 |
void addSource(Node s) {
|
| 1452 | 1456 |
if(!(*_reached)[s]) {
|
| 1453 | 1457 |
_reached->set(s,true); |
| 1454 | 1458 |
_visitor->start(s); |
| 1455 | 1459 |
_visitor->reach(s); |
| 1456 | 1460 |
_list[++_list_back] = s; |
| 1457 | 1461 |
} |
| 1458 | 1462 |
} |
| 1459 | 1463 |
|
| 1460 | 1464 |
/// \brief Processes the next node. |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_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 |
/// |
| 134 | 134 |
/// This method sets the priority of the given item if it is |
| 135 | 135 |
/// already stored in the heap. Otherwise it inserts the given |
| 136 | 136 |
/// item into the heap with the given priority. |
| 137 | 137 |
/// \param item The item. |
| 138 | 138 |
/// \param value The priority. |
| 139 | 139 |
void set (const Item& item, const Prio& value) {
|
| 140 | 140 |
int i=_iim[item]; |
| 141 | 141 |
if ( i >= 0 && _data[i].in ) {
|
| 142 | 142 |
if ( _comp(value, _data[i].prio) ) decrease(item, value); |
| 143 | 143 |
if ( _comp(_data[i].prio, value) ) increase(item, value); |
| 144 | 144 |
} else push(item, value); |
| 145 | 145 |
} |
| 146 | 146 |
|
| 147 | 147 |
/// \brief Insert an item into the heap with the given priority. |
| 148 | 148 |
/// |
| 149 | 149 |
/// This function inserts the given item into the heap with the |
| 150 | 150 |
/// given priority. |
| 151 | 151 |
/// \param item The item to insert. |
| 152 | 152 |
/// \param value The priority of the item. |
| 153 | 153 |
/// \pre \e item must not be stored in the heap. |
| 154 | 154 |
void push (const Item& item, const Prio& value) {
|
| 155 | 155 |
int i=_iim[item]; |
| 156 | 156 |
if ( i<0 ) {
|
| 157 | 157 |
int s=_data.size(); |
| 158 | 158 |
_iim.set( item,s ); |
| 159 | 159 |
Store st; |
| 160 | 160 |
st.name=item; |
| 161 | 161 |
st.prio=value; |
| 162 | 162 |
_data.push_back(st); |
| 163 | 163 |
i=s; |
| 164 | 164 |
} |
| 165 | 165 |
else {
|
| 166 | 166 |
_data[i].parent=_data[i].right_neighbor=_data[i].child=-1; |
| 167 | 167 |
_data[i].degree=0; |
| 168 | 168 |
_data[i].in=true; |
| 169 | 169 |
_data[i].prio=value; |
| 170 | 170 |
} |
| 171 | 171 |
|
| 172 | 172 |
if( 0==_num_items ) {
|
| 173 | 173 |
_head=i; |
| 174 | 174 |
_min=i; |
| 175 | 175 |
} else {
|
| 176 | 176 |
merge(i); |
| 177 | 177 |
if( _comp(_data[i].prio, _data[_min].prio) ) _min=i; |
| 178 | 178 |
} |
| 179 | 179 |
++_num_items; |
| 180 | 180 |
} |
| 181 | 181 |
|
| 182 | 182 |
/// \brief Return the item having minimum priority. |
| 183 | 183 |
/// |
| 184 | 184 |
/// This function returns the item having minimum priority. |
| 185 | 185 |
/// \pre The heap must be non-empty. |
| 186 | 186 |
Item top() const { return _data[_min].name; }
|
| 187 | 187 |
|
| 188 | 188 |
/// \brief The minimum priority. |
| 189 | 189 |
/// |
| 190 | 190 |
/// This function returns the minimum priority. |
| 191 | 191 |
/// \pre The heap must be non-empty. |
| 192 | 192 |
Prio prio() const { return _data[_min].prio; }
|
| 193 | 193 |
|
| 194 | 194 |
/// \brief The priority of the given item. |
| 195 | 195 |
/// |
| 196 | 196 |
/// This function returns the priority of the given item. |
| 197 | 197 |
/// \param item The item. |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_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 |
| 134 | 134 |
// the container of the map. |
| 135 | 135 |
ArrayMap& operator=(const ArrayMap& cmap) {
|
| 136 | 136 |
return operator=<ArrayMap>(cmap); |
| 137 | 137 |
} |
| 138 | 138 |
|
| 139 | 139 |
|
| 140 | 140 |
// \brief Template assign operator. |
| 141 | 141 |
// |
| 142 | 142 |
// The given parameter should conform to the ReadMap |
| 143 | 143 |
// concecpt and could be indiced by the current item set of |
| 144 | 144 |
// the NodeMap. In this case the value for each item |
| 145 | 145 |
// is assigned by the value of the given ReadMap. |
| 146 | 146 |
template <typename CMap> |
| 147 | 147 |
ArrayMap& operator=(const CMap& cmap) {
|
| 148 | 148 |
checkConcept<concepts::ReadMap<Key, _Value>, CMap>(); |
| 149 | 149 |
const typename Parent::Notifier* nf = Parent::notifier(); |
| 150 | 150 |
Item it; |
| 151 | 151 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 152 | 152 |
set(it, cmap[it]); |
| 153 | 153 |
} |
| 154 | 154 |
return *this; |
| 155 | 155 |
} |
| 156 | 156 |
|
| 157 | 157 |
public: |
| 158 | 158 |
// \brief The destructor of the map. |
| 159 | 159 |
// |
| 160 | 160 |
// The destructor of the map. |
| 161 | 161 |
virtual ~ArrayMap() {
|
| 162 | 162 |
if (attached()) {
|
| 163 | 163 |
clear(); |
| 164 | 164 |
detach(); |
| 165 | 165 |
} |
| 166 | 166 |
} |
| 167 | 167 |
|
| 168 | 168 |
protected: |
| 169 | 169 |
|
| 170 | 170 |
using Parent::attach; |
| 171 | 171 |
using Parent::detach; |
| 172 | 172 |
using Parent::attached; |
| 173 | 173 |
|
| 174 | 174 |
public: |
| 175 | 175 |
|
| 176 | 176 |
// \brief The subscript operator. |
| 177 | 177 |
// |
| 178 | 178 |
// The subscript operator. The map can be subscripted by the |
| 179 | 179 |
// actual keys of the graph. |
| 180 | 180 |
Value& operator[](const Key& key) {
|
| 181 | 181 |
int id = Parent::notifier()->id(key); |
| 182 | 182 |
return values[id]; |
| 183 | 183 |
} |
| 184 | 184 |
|
| 185 | 185 |
// \brief The const subscript operator. |
| 186 | 186 |
// |
| 187 | 187 |
// The const subscript operator. The map can be subscripted by the |
| 188 | 188 |
// actual keys of the graph. |
| 189 | 189 |
const Value& operator[](const Key& key) const {
|
| 190 | 190 |
int id = Parent::notifier()->id(key); |
| 191 | 191 |
return values[id]; |
| 192 | 192 |
} |
| 193 | 193 |
|
| 194 | 194 |
// \brief Setter function of the map. |
| 195 | 195 |
// |
| 196 | 196 |
// Setter function of the map. Equivalent with map[key] = val. |
| 197 | 197 |
// This is a compatibility feature with the not dereferable maps. |
| 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; |
| 134 | 134 |
}; |
| 135 | 135 |
|
| 136 | 136 |
|
| 137 | 137 |
// pointer |
| 138 | 138 |
template <typename _Graph, typename _Item, typename _Ptr> |
| 139 | 139 |
struct DefaultMapSelector<_Graph, _Item, _Ptr*> {
|
| 140 | 140 |
typedef VectorMap<_Graph, _Item, _Ptr*> Map; |
| 141 | 141 |
}; |
| 142 | 142 |
|
| 143 | 143 |
// #else |
| 144 | 144 |
|
| 145 | 145 |
// template <typename _Graph, typename _Item, typename _Value> |
| 146 | 146 |
// struct DefaultMapSelector {
|
| 147 | 147 |
// typedef DebugMap<_Graph, _Item, _Value> Map; |
| 148 | 148 |
// }; |
| 149 | 149 |
|
| 150 | 150 |
// #endif |
| 151 | 151 |
|
| 152 | 152 |
// DefaultMap class |
| 153 | 153 |
template <typename _Graph, typename _Item, typename _Value> |
| 154 | 154 |
class DefaultMap |
| 155 | 155 |
: public DefaultMapSelector<_Graph, _Item, _Value>::Map {
|
| 156 | 156 |
typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent; |
| 157 | 157 |
|
| 158 | 158 |
public: |
| 159 | 159 |
typedef DefaultMap<_Graph, _Item, _Value> Map; |
| 160 | 160 |
|
| 161 | 161 |
typedef typename Parent::GraphType GraphType; |
| 162 | 162 |
typedef typename Parent::Value Value; |
| 163 | 163 |
|
| 164 | 164 |
explicit DefaultMap(const GraphType& graph) : Parent(graph) {}
|
| 165 | 165 |
DefaultMap(const GraphType& graph, const Value& value) |
| 166 | 166 |
: Parent(graph, value) {}
|
| 167 | 167 |
|
| 168 | 168 |
DefaultMap& operator=(const DefaultMap& cmap) {
|
| 169 | 169 |
return operator=<DefaultMap>(cmap); |
| 170 | 170 |
} |
| 171 | 171 |
|
| 172 | 172 |
template <typename CMap> |
| 173 | 173 |
DefaultMap& operator=(const CMap& cmap) {
|
| 174 | 174 |
Parent::operator=(cmap); |
| 175 | 175 |
return *this; |
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
}; |
| 179 | 179 |
|
| 180 | 180 |
} |
| 181 | 181 |
|
| 182 | 182 |
#endif |
| 1 |
/* -*- C++ -*- |
|
| 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
| 2 | 2 |
* |
| 3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
| 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_EDGE_SET_EXTENDER_H |
| 20 | 20 |
#define LEMON_BITS_EDGE_SET_EXTENDER_H |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/core.h> |
| 23 | 23 |
#include <lemon/error.h> |
| 24 | 24 |
#include <lemon/bits/default_map.h> |
| 25 | 25 |
#include <lemon/bits/map_extender.h> |
| 26 | 26 |
|
| 27 | 27 |
//\ingroup digraphbits |
| 28 | 28 |
//\file |
| 29 | 29 |
//\brief Extenders for the arc set types |
| 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); |
| 134 | 134 |
return *this; |
| 135 | 135 |
} |
| 136 | 136 |
|
| 137 | 137 |
}; |
| 138 | 138 |
|
| 139 | 139 |
|
| 140 | 140 |
class OutArcIt : public Arc {
|
| 141 | 141 |
const Digraph* digraph; |
| 142 | 142 |
public: |
| 143 | 143 |
|
| 144 | 144 |
OutArcIt() { }
|
| 145 | 145 |
|
| 146 | 146 |
OutArcIt(Invalid i) : Arc(i) { }
|
| 147 | 147 |
|
| 148 | 148 |
OutArcIt(const Digraph& _graph, const Node& node) |
| 149 | 149 |
: digraph(&_graph) {
|
| 150 | 150 |
_graph.firstOut(*this, node); |
| 151 | 151 |
} |
| 152 | 152 |
|
| 153 | 153 |
OutArcIt(const Digraph& _graph, const Arc& arc) |
| 154 | 154 |
: Arc(arc), digraph(&_graph) {}
|
| 155 | 155 |
|
| 156 | 156 |
OutArcIt& operator++() {
|
| 157 | 157 |
digraph->nextOut(*this); |
| 158 | 158 |
return *this; |
| 159 | 159 |
} |
| 160 | 160 |
|
| 161 | 161 |
}; |
| 162 | 162 |
|
| 163 | 163 |
|
| 164 | 164 |
class InArcIt : public Arc {
|
| 165 | 165 |
const Digraph* digraph; |
| 166 | 166 |
public: |
| 167 | 167 |
|
| 168 | 168 |
InArcIt() { }
|
| 169 | 169 |
|
| 170 | 170 |
InArcIt(Invalid i) : Arc(i) { }
|
| 171 | 171 |
|
| 172 | 172 |
InArcIt(const Digraph& _graph, const Node& node) |
| 173 | 173 |
: digraph(&_graph) {
|
| 174 | 174 |
_graph.firstIn(*this, node); |
| 175 | 175 |
} |
| 176 | 176 |
|
| 177 | 177 |
InArcIt(const Digraph& _graph, const Arc& arc) : |
| 178 | 178 |
Arc(arc), digraph(&_graph) {}
|
| 179 | 179 |
|
| 180 | 180 |
InArcIt& operator++() {
|
| 181 | 181 |
digraph->nextIn(*this); |
| 182 | 182 |
return *this; |
| 183 | 183 |
} |
| 184 | 184 |
|
| 185 | 185 |
}; |
| 186 | 186 |
|
| 187 | 187 |
// \brief Base node of the iterator |
| 188 | 188 |
// |
| 189 | 189 |
// Returns the base node (ie. the source in this case) of the iterator |
| 190 | 190 |
Node baseNode(const OutArcIt &e) const {
|
| 191 | 191 |
return Parent::source(static_cast<const Arc&>(e)); |
| 192 | 192 |
} |
| 193 | 193 |
// \brief Running node of the iterator |
| 194 | 194 |
// |
| 195 | 195 |
// Returns the running node (ie. the target in this case) of the |
| 196 | 196 |
// iterator |
| 197 | 197 |
Node runningNode(const OutArcIt &e) const {
|
| 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) {
|
| 134 | 134 |
items[first_free_item].prev = n; |
| 135 | 135 |
} |
| 136 | 136 |
items[n].next = first_free_item; |
| 137 | 137 |
items[n].prev = -1; |
| 138 | 138 |
first_free_item = n; |
| 139 | 139 |
|
| 140 | 140 |
while (!cross.empty() && cross.back() == -1) {
|
| 141 | 141 |
cross.pop_back(); |
| 142 | 142 |
} |
| 143 | 143 |
} |
| 144 | 144 |
|
| 145 | 145 |
int maxIndex() const {
|
| 146 | 146 |
return cross.size() - 1; |
| 147 | 147 |
} |
| 148 | 148 |
|
| 149 | 149 |
void shiftIndices(int idx) {
|
| 150 | 150 |
for (int i = idx + 1; i < static_cast<int>(cross.size()); ++i) {
|
| 151 | 151 |
cross[i - 1] = cross[i]; |
| 152 | 152 |
if (cross[i] != -1) {
|
| 153 | 153 |
--items[cross[i]].index; |
| 154 | 154 |
} |
| 155 | 155 |
} |
| 156 | 156 |
cross.back() = -1; |
| 157 | 157 |
cross.pop_back(); |
| 158 | 158 |
while (!cross.empty() && cross.back() == -1) {
|
| 159 | 159 |
cross.pop_back(); |
| 160 | 160 |
} |
| 161 | 161 |
} |
| 162 | 162 |
|
| 163 | 163 |
void relocateIndex(int idx, int jdx) {
|
| 164 | 164 |
cross[idx] = cross[jdx]; |
| 165 | 165 |
items[cross[jdx]].index = idx; |
| 166 | 166 |
cross[jdx] = -1; |
| 167 | 167 |
|
| 168 | 168 |
while (!cross.empty() && cross.back() == -1) {
|
| 169 | 169 |
cross.pop_back(); |
| 170 | 170 |
} |
| 171 | 171 |
} |
| 172 | 172 |
|
| 173 | 173 |
int operator[](int idx) const {
|
| 174 | 174 |
return cross[idx]; |
| 175 | 175 |
} |
| 176 | 176 |
|
| 177 | 177 |
int operator()(int fdx) const {
|
| 178 | 178 |
return items[fdx].index; |
| 179 | 179 |
} |
| 180 | 180 |
|
| 181 | 181 |
void firstItem(int& fdx) const {
|
| 182 | 182 |
fdx = first_item; |
| 183 | 183 |
} |
| 184 | 184 |
|
| 185 | 185 |
void nextItem(int& fdx) const {
|
| 186 | 186 |
fdx = items[fdx].next; |
| 187 | 187 |
} |
| 188 | 188 |
|
| 189 | 189 |
}; |
| 190 | 190 |
} |
| 191 | 191 |
} |
| 192 | 192 |
|
| 193 | 193 |
#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 |
///\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 |
/// |
| 134 | 134 |
/// This functon makes the heap empty. |
| 135 | 135 |
/// It does not change the cross reference map. If you want to reuse |
| 136 | 136 |
/// a heap that is not surely empty, you should first clear it and |
| 137 | 137 |
/// then you should set the cross reference map to \c PRE_HEAP |
| 138 | 138 |
/// for each item. |
| 139 | 139 |
void clear() {
|
| 140 | 140 |
_data.clear(); _first.clear(); _minimum = 0; |
| 141 | 141 |
} |
| 142 | 142 |
|
| 143 | 143 |
private: |
| 144 | 144 |
|
| 145 | 145 |
void relocateLast(int idx) {
|
| 146 | 146 |
if (idx + 1 < int(_data.size())) {
|
| 147 | 147 |
_data[idx] = _data.back(); |
| 148 | 148 |
if (_data[idx].prev != -1) {
|
| 149 | 149 |
_data[_data[idx].prev].next = idx; |
| 150 | 150 |
} else {
|
| 151 | 151 |
_first[_data[idx].value] = idx; |
| 152 | 152 |
} |
| 153 | 153 |
if (_data[idx].next != -1) {
|
| 154 | 154 |
_data[_data[idx].next].prev = idx; |
| 155 | 155 |
} |
| 156 | 156 |
_iim[_data[idx].item] = idx; |
| 157 | 157 |
} |
| 158 | 158 |
_data.pop_back(); |
| 159 | 159 |
} |
| 160 | 160 |
|
| 161 | 161 |
void unlace(int idx) {
|
| 162 | 162 |
if (_data[idx].prev != -1) {
|
| 163 | 163 |
_data[_data[idx].prev].next = _data[idx].next; |
| 164 | 164 |
} else {
|
| 165 | 165 |
_first[_data[idx].value] = _data[idx].next; |
| 166 | 166 |
} |
| 167 | 167 |
if (_data[idx].next != -1) {
|
| 168 | 168 |
_data[_data[idx].next].prev = _data[idx].prev; |
| 169 | 169 |
} |
| 170 | 170 |
} |
| 171 | 171 |
|
| 172 | 172 |
void lace(int idx) {
|
| 173 | 173 |
if (int(_first.size()) <= _data[idx].value) {
|
| 174 | 174 |
_first.resize(_data[idx].value + 1, -1); |
| 175 | 175 |
} |
| 176 | 176 |
_data[idx].next = _first[_data[idx].value]; |
| 177 | 177 |
if (_data[idx].next != -1) {
|
| 178 | 178 |
_data[_data[idx].next].prev = idx; |
| 179 | 179 |
} |
| 180 | 180 |
_first[_data[idx].value] = idx; |
| 181 | 181 |
_data[idx].prev = -1; |
| 182 | 182 |
} |
| 183 | 183 |
|
| 184 | 184 |
public: |
| 185 | 185 |
|
| 186 | 186 |
/// \brief Insert a pair of item and priority into the heap. |
| 187 | 187 |
/// |
| 188 | 188 |
/// This function inserts \c p.first to the heap with priority |
| 189 | 189 |
/// \c p.second. |
| 190 | 190 |
/// \param p The pair to insert. |
| 191 | 191 |
/// \pre \c p.first must not be stored in the heap. |
| 192 | 192 |
void push(const Pair& p) {
|
| 193 | 193 |
push(p.first, p.second); |
| 194 | 194 |
} |
| 195 | 195 |
|
| 196 | 196 |
/// \brief Insert an item into the heap with the given priority. |
| 197 | 197 |
/// |
| 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. |
| 134 | 134 |
UNBOUNDED |
| 135 | 135 |
}; |
| 136 | 136 |
|
| 137 | 137 |
private: |
| 138 | 138 |
|
| 139 | 139 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 140 | 140 |
|
| 141 | 141 |
typedef std::vector<int> IntVector; |
| 142 | 142 |
typedef std::vector<Value> ValueVector; |
| 143 | 143 |
typedef std::vector<Cost> CostVector; |
| 144 | 144 |
typedef std::vector<char> BoolVector; |
| 145 | 145 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
| 146 | 146 |
|
| 147 | 147 |
private: |
| 148 | 148 |
|
| 149 | 149 |
// Data related to the underlying digraph |
| 150 | 150 |
const GR &_graph; |
| 151 | 151 |
int _node_num; |
| 152 | 152 |
int _arc_num; |
| 153 | 153 |
int _res_arc_num; |
| 154 | 154 |
int _root; |
| 155 | 155 |
|
| 156 | 156 |
// Parameters of the problem |
| 157 | 157 |
bool _have_lower; |
| 158 | 158 |
Value _sum_supply; |
| 159 | 159 |
|
| 160 | 160 |
// Data structures for storing the digraph |
| 161 | 161 |
IntNodeMap _node_id; |
| 162 | 162 |
IntArcMap _arc_idf; |
| 163 | 163 |
IntArcMap _arc_idb; |
| 164 | 164 |
IntVector _first_out; |
| 165 | 165 |
BoolVector _forward; |
| 166 | 166 |
IntVector _source; |
| 167 | 167 |
IntVector _target; |
| 168 | 168 |
IntVector _reverse; |
| 169 | 169 |
|
| 170 | 170 |
// Node and arc data |
| 171 | 171 |
ValueVector _lower; |
| 172 | 172 |
ValueVector _upper; |
| 173 | 173 |
CostVector _cost; |
| 174 | 174 |
ValueVector _supply; |
| 175 | 175 |
|
| 176 | 176 |
ValueVector _res_cap; |
| 177 | 177 |
CostVector _pi; |
| 178 | 178 |
ValueVector _excess; |
| 179 | 179 |
IntVector _excess_nodes; |
| 180 | 180 |
IntVector _deficit_nodes; |
| 181 | 181 |
|
| 182 | 182 |
Value _delta; |
| 183 | 183 |
int _factor; |
| 184 | 184 |
IntVector _pred; |
| 185 | 185 |
|
| 186 | 186 |
public: |
| 187 | 187 |
|
| 188 | 188 |
/// \brief Constant for infinite upper bounds (capacities). |
| 189 | 189 |
/// |
| 190 | 190 |
/// Constant for infinite upper bounds (capacities). |
| 191 | 191 |
/// It is \c std::numeric_limits<Value>::infinity() if available, |
| 192 | 192 |
/// \c std::numeric_limits<Value>::max() otherwise. |
| 193 | 193 |
const Value INF; |
| 194 | 194 |
|
| 195 | 195 |
private: |
| 196 | 196 |
|
| 197 | 197 |
// Special implementation of the Dijkstra algorithm for finding |
| 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$
|
| 134 | 134 |
denotes the signed supply values of the nodes. |
| 135 | 135 |
If \f$sup(u)>0\f$, then \f$u\f$ is a supply node with \f$sup(u)\f$ |
| 136 | 136 |
supply, if \f$sup(u)<0\f$, then \f$u\f$ is a demand node with |
| 137 | 137 |
\f$-sup(u)\f$ demand. |
| 138 | 138 |
A feasible circulation is an \f$f: A\rightarrow\mathbf{R}\f$
|
| 139 | 139 |
solution of the following problem. |
| 140 | 140 |
|
| 141 | 141 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu)
|
| 142 | 142 |
\geq sup(u) \quad \forall u\in V, \f] |
| 143 | 143 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A. \f] |
| 144 | 144 |
|
| 145 | 145 |
The sum of the supply values, i.e. \f$\sum_{u\in V} sup(u)\f$ must be
|
| 146 | 146 |
zero or negative in order to have a feasible solution (since the sum |
| 147 | 147 |
of the expressions on the left-hand side of the inequalities is zero). |
| 148 | 148 |
It means that the total demand must be greater or equal to the total |
| 149 | 149 |
supply and all the supplies have to be carried out from the supply nodes, |
| 150 | 150 |
but there could be demands that are not satisfied. |
| 151 | 151 |
If \f$\sum_{u\in V} sup(u)\f$ is zero, then all the supply/demand
|
| 152 | 152 |
constraints have to be satisfied with equality, i.e. all demands |
| 153 | 153 |
have to be satisfied and all supplies have to be used. |
| 154 | 154 |
|
| 155 | 155 |
If you need the opposite inequalities in the supply/demand constraints |
| 156 | 156 |
(i.e. the total demand is less than the total supply and all the demands |
| 157 | 157 |
have to be satisfied while there could be supplies that are not used), |
| 158 | 158 |
then you could easily transform the problem to the above form by reversing |
| 159 | 159 |
the direction of the arcs and taking the negative of the supply values |
| 160 | 160 |
(e.g. using \ref ReverseDigraph and \ref NegMap adaptors). |
| 161 | 161 |
|
| 162 | 162 |
This algorithm either calculates a feasible circulation, or provides |
| 163 | 163 |
a \ref barrier() "barrier", which prooves that a feasible soultion |
| 164 | 164 |
cannot exist. |
| 165 | 165 |
|
| 166 | 166 |
Note that this algorithm also provides a feasible solution for the |
| 167 | 167 |
\ref min_cost_flow "minimum cost flow problem". |
| 168 | 168 |
|
| 169 | 169 |
\tparam GR The type of the digraph the algorithm runs on. |
| 170 | 170 |
\tparam LM The type of the lower bound map. The default |
| 171 | 171 |
map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 172 | 172 |
\tparam UM The type of the upper bound (capacity) map. |
| 173 | 173 |
The default map type is \c LM. |
| 174 | 174 |
\tparam SM The type of the supply map. The default map type is |
| 175 | 175 |
\ref concepts::Digraph::NodeMap "GR::NodeMap<UM::Value>". |
| 176 | 176 |
\tparam TR The traits class that defines various types used by the |
| 177 | 177 |
algorithm. By default, it is \ref CirculationDefaultTraits |
| 178 | 178 |
"CirculationDefaultTraits<GR, LM, UM, SM>". |
| 179 | 179 |
In most cases, this parameter should not be set directly, |
| 180 | 180 |
consider to use the named template parameters instead. |
| 181 | 181 |
*/ |
| 182 | 182 |
#ifdef DOXYGEN |
| 183 | 183 |
template< typename GR, |
| 184 | 184 |
typename LM, |
| 185 | 185 |
typename UM, |
| 186 | 186 |
typename SM, |
| 187 | 187 |
typename TR > |
| 188 | 188 |
#else |
| 189 | 189 |
template< typename GR, |
| 190 | 190 |
typename LM = typename GR::template ArcMap<int>, |
| 191 | 191 |
typename UM = LM, |
| 192 | 192 |
typename SM = typename GR::template NodeMap<typename UM::Value>, |
| 193 | 193 |
typename TR = CirculationDefaultTraits<GR, LM, UM, SM> > |
| 194 | 194 |
#endif |
| 195 | 195 |
class Circulation {
|
| 196 | 196 |
public: |
| 197 | 197 |
| 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) {
|
| 134 | 134 |
_prob->setRowName(r, const_cast<std::string&>(name)); |
| 135 | 135 |
_row_names_ref[name] = r; |
| 136 | 136 |
} |
| 137 | 137 |
|
| 138 | 138 |
int ClpLp::_rowByName(const std::string& name) const {
|
| 139 | 139 |
std::map<std::string, int>::const_iterator it = _row_names_ref.find(name); |
| 140 | 140 |
return it != _row_names_ref.end() ? it->second : -1; |
| 141 | 141 |
} |
| 142 | 142 |
|
| 143 | 143 |
|
| 144 | 144 |
void ClpLp::_setRowCoeffs(int ix, ExprIterator b, ExprIterator e) {
|
| 145 | 145 |
std::map<int, Value> coeffs; |
| 146 | 146 |
|
| 147 | 147 |
int n = _prob->clpMatrix()->getNumCols(); |
| 148 | 148 |
|
| 149 | 149 |
const int* indices = _prob->clpMatrix()->getIndices(); |
| 150 | 150 |
const double* elements = _prob->clpMatrix()->getElements(); |
| 151 | 151 |
|
| 152 | 152 |
for (int i = 0; i < n; ++i) {
|
| 153 | 153 |
CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[i]; |
| 154 | 154 |
CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[i]; |
| 155 | 155 |
|
| 156 | 156 |
const int* it = std::lower_bound(indices + begin, indices + end, ix); |
| 157 | 157 |
if (it != indices + end && *it == ix && elements[it - indices] != 0.0) {
|
| 158 | 158 |
coeffs[i] = 0.0; |
| 159 | 159 |
} |
| 160 | 160 |
} |
| 161 | 161 |
|
| 162 | 162 |
for (ExprIterator it = b; it != e; ++it) {
|
| 163 | 163 |
coeffs[it->first] = it->second; |
| 164 | 164 |
} |
| 165 | 165 |
|
| 166 | 166 |
for (std::map<int, Value>::iterator it = coeffs.begin(); |
| 167 | 167 |
it != coeffs.end(); ++it) {
|
| 168 | 168 |
_prob->modifyCoefficient(ix, it->first, it->second); |
| 169 | 169 |
} |
| 170 | 170 |
} |
| 171 | 171 |
|
| 172 | 172 |
void ClpLp::_getRowCoeffs(int ix, InsertIterator b) const {
|
| 173 | 173 |
int n = _prob->clpMatrix()->getNumCols(); |
| 174 | 174 |
|
| 175 | 175 |
const int* indices = _prob->clpMatrix()->getIndices(); |
| 176 | 176 |
const double* elements = _prob->clpMatrix()->getElements(); |
| 177 | 177 |
|
| 178 | 178 |
for (int i = 0; i < n; ++i) {
|
| 179 | 179 |
CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[i]; |
| 180 | 180 |
CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[i]; |
| 181 | 181 |
|
| 182 | 182 |
const int* it = std::lower_bound(indices + begin, indices + end, ix); |
| 183 | 183 |
if (it != indices + end && *it == ix) {
|
| 184 | 184 |
*b = std::make_pair(i, elements[it - indices]); |
| 185 | 185 |
} |
| 186 | 186 |
} |
| 187 | 187 |
} |
| 188 | 188 |
|
| 189 | 189 |
void ClpLp::_setColCoeffs(int ix, ExprIterator b, ExprIterator e) {
|
| 190 | 190 |
std::map<int, Value> coeffs; |
| 191 | 191 |
|
| 192 | 192 |
CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix]; |
| 193 | 193 |
CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix]; |
| 194 | 194 |
|
| 195 | 195 |
const int* indices = _prob->clpMatrix()->getIndices(); |
| 196 | 196 |
const double* elements = _prob->clpMatrix()->getElements(); |
| 197 | 197 |
| 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; |
| 134 | 134 |
|
| 135 | 135 |
virtual ProblemType _getPrimalType() const; |
| 136 | 136 |
virtual ProblemType _getDualType() const; |
| 137 | 137 |
|
| 138 | 138 |
virtual void _clear(); |
| 139 | 139 |
|
| 140 | 140 |
virtual void _messageLevel(MessageLevel); |
| 141 | 141 |
|
| 142 | 142 |
public: |
| 143 | 143 |
|
| 144 | 144 |
///Solves LP with primal simplex method. |
| 145 | 145 |
SolveExitStatus solvePrimal(); |
| 146 | 146 |
|
| 147 | 147 |
///Solves LP with dual simplex method. |
| 148 | 148 |
SolveExitStatus solveDual(); |
| 149 | 149 |
|
| 150 | 150 |
///Solves LP with barrier method. |
| 151 | 151 |
SolveExitStatus solveBarrier(); |
| 152 | 152 |
|
| 153 | 153 |
///Returns the constraint identifier understood by CLP. |
| 154 | 154 |
int clpRow(Row r) const { return rows(id(r)); }
|
| 155 | 155 |
|
| 156 | 156 |
///Returns the variable identifier understood by CLP. |
| 157 | 157 |
int clpCol(Col c) const { return cols(id(c)); }
|
| 158 | 158 |
|
| 159 | 159 |
}; |
| 160 | 160 |
|
| 161 | 161 |
} //END OF NAMESPACE LEMON |
| 162 | 162 |
|
| 163 | 163 |
#endif //LEMON_CLP_H |
| 164 | 164 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_CONCEPTS_DIGRAPH_H |
| 20 | 20 |
#define LEMON_CONCEPTS_DIGRAPH_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup graph_concepts |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief The concept of directed graphs. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/core.h> |
| 27 | 27 |
#include <lemon/concepts/maps.h> |
| 28 | 28 |
#include <lemon/concept_check.h> |
| 29 | 29 |
#include <lemon/concepts/graph_components.h> |
| 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. |
| 134 | 134 |
|
| 135 | 135 |
/// Sets the iterator to the first node of the given digraph. |
| 136 | 136 |
/// |
| 137 | 137 |
explicit NodeIt(const Digraph&) { }
|
| 138 | 138 |
/// Sets the iterator to the given node. |
| 139 | 139 |
|
| 140 | 140 |
/// Sets the iterator to the given node of the given digraph. |
| 141 | 141 |
/// |
| 142 | 142 |
NodeIt(const Digraph&, const Node&) { }
|
| 143 | 143 |
/// Next node. |
| 144 | 144 |
|
| 145 | 145 |
/// Assign the iterator to the next node. |
| 146 | 146 |
/// |
| 147 | 147 |
NodeIt& operator++() { return *this; }
|
| 148 | 148 |
}; |
| 149 | 149 |
|
| 150 | 150 |
|
| 151 | 151 |
/// The arc type of the digraph |
| 152 | 152 |
|
| 153 | 153 |
/// This class identifies an arc of the digraph. It also serves |
| 154 | 154 |
/// as a base class of the arc iterators, |
| 155 | 155 |
/// thus they will convert to this type. |
| 156 | 156 |
class Arc {
|
| 157 | 157 |
public: |
| 158 | 158 |
/// Default constructor |
| 159 | 159 |
|
| 160 | 160 |
/// Default constructor. |
| 161 | 161 |
/// \warning It sets the object to an undefined value. |
| 162 | 162 |
Arc() { }
|
| 163 | 163 |
/// Copy constructor. |
| 164 | 164 |
|
| 165 | 165 |
/// Copy constructor. |
| 166 | 166 |
/// |
| 167 | 167 |
Arc(const Arc&) { }
|
| 168 | 168 |
/// %Invalid constructor \& conversion. |
| 169 | 169 |
|
| 170 | 170 |
/// Initializes the object to be invalid. |
| 171 | 171 |
/// \sa Invalid for more details. |
| 172 | 172 |
Arc(Invalid) { }
|
| 173 | 173 |
/// Equality operator |
| 174 | 174 |
|
| 175 | 175 |
/// Equality operator. |
| 176 | 176 |
/// |
| 177 | 177 |
/// Two iterators are equal if and only if they point to the |
| 178 | 178 |
/// same object or both are \c INVALID. |
| 179 | 179 |
bool operator==(Arc) const { return true; }
|
| 180 | 180 |
/// Inequality operator |
| 181 | 181 |
|
| 182 | 182 |
/// Inequality operator. |
| 183 | 183 |
bool operator!=(Arc) const { return true; }
|
| 184 | 184 |
|
| 185 | 185 |
/// Artificial ordering operator. |
| 186 | 186 |
|
| 187 | 187 |
/// Artificial ordering operator. |
| 188 | 188 |
/// |
| 189 | 189 |
/// \note This operator only has to define some strict ordering of |
| 190 | 190 |
/// the arcs; this order has nothing to do with the iteration |
| 191 | 191 |
/// ordering of the arcs. |
| 192 | 192 |
bool operator<(Arc) const { return false; }
|
| 193 | 193 |
}; |
| 194 | 194 |
|
| 195 | 195 |
/// Iterator class for the outgoing arcs of a node. |
| 196 | 196 |
|
| 197 | 197 |
/// This iterator goes trough the \e outgoing arcs of a certain 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 |
| 134 | 134 |
/// the items; this order has nothing to do with the iteration |
| 135 | 135 |
/// ordering of the items. |
| 136 | 136 |
bool operator<(Node) const { return false; }
|
| 137 | 137 |
|
| 138 | 138 |
}; |
| 139 | 139 |
|
| 140 | 140 |
/// Iterator class for the nodes. |
| 141 | 141 |
|
| 142 | 142 |
/// This iterator goes through each node of the graph. |
| 143 | 143 |
/// Its usage is quite simple, for example, you can count the number |
| 144 | 144 |
/// of nodes in a graph \c g of type \c %Graph like this: |
| 145 | 145 |
///\code |
| 146 | 146 |
/// int count=0; |
| 147 | 147 |
/// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count; |
| 148 | 148 |
///\endcode |
| 149 | 149 |
class NodeIt : public Node {
|
| 150 | 150 |
public: |
| 151 | 151 |
/// Default constructor |
| 152 | 152 |
|
| 153 | 153 |
/// Default constructor. |
| 154 | 154 |
/// \warning It sets the iterator to an undefined value. |
| 155 | 155 |
NodeIt() { }
|
| 156 | 156 |
/// Copy constructor. |
| 157 | 157 |
|
| 158 | 158 |
/// Copy constructor. |
| 159 | 159 |
/// |
| 160 | 160 |
NodeIt(const NodeIt& n) : Node(n) { }
|
| 161 | 161 |
/// %Invalid constructor \& conversion. |
| 162 | 162 |
|
| 163 | 163 |
/// Initializes the iterator to be invalid. |
| 164 | 164 |
/// \sa Invalid for more details. |
| 165 | 165 |
NodeIt(Invalid) { }
|
| 166 | 166 |
/// Sets the iterator to the first node. |
| 167 | 167 |
|
| 168 | 168 |
/// Sets the iterator to the first node of the given digraph. |
| 169 | 169 |
/// |
| 170 | 170 |
explicit NodeIt(const Graph&) { }
|
| 171 | 171 |
/// Sets the iterator to the given node. |
| 172 | 172 |
|
| 173 | 173 |
/// Sets the iterator to the given node of the given digraph. |
| 174 | 174 |
/// |
| 175 | 175 |
NodeIt(const Graph&, const Node&) { }
|
| 176 | 176 |
/// Next node. |
| 177 | 177 |
|
| 178 | 178 |
/// Assign the iterator to the next node. |
| 179 | 179 |
/// |
| 180 | 180 |
NodeIt& operator++() { return *this; }
|
| 181 | 181 |
}; |
| 182 | 182 |
|
| 183 | 183 |
|
| 184 | 184 |
/// The edge type of the graph |
| 185 | 185 |
|
| 186 | 186 |
/// This class identifies an edge of the graph. It also serves |
| 187 | 187 |
/// as a base class of the edge iterators, |
| 188 | 188 |
/// thus they will convert to this type. |
| 189 | 189 |
class Edge {
|
| 190 | 190 |
public: |
| 191 | 191 |
/// Default constructor |
| 192 | 192 |
|
| 193 | 193 |
/// Default constructor. |
| 194 | 194 |
/// \warning It sets the object to an undefined value. |
| 195 | 195 |
Edge() { }
|
| 196 | 196 |
/// Copy constructor. |
| 197 | 197 |
| 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 |
/// |
| 134 | 134 |
/// This class represents the nodes of the digraph. |
| 135 | 135 |
typedef GraphItem<'n'> Node; |
| 136 | 136 |
|
| 137 | 137 |
/// \brief Arc class of the digraph. |
| 138 | 138 |
/// |
| 139 | 139 |
/// This class represents the arcs of the digraph. |
| 140 | 140 |
typedef GraphItem<'a'> Arc; |
| 141 | 141 |
|
| 142 | 142 |
/// \brief Return the source node of an arc. |
| 143 | 143 |
/// |
| 144 | 144 |
/// This function returns the source node of an arc. |
| 145 | 145 |
Node source(const Arc&) const { return INVALID; }
|
| 146 | 146 |
|
| 147 | 147 |
/// \brief Return the target node of an arc. |
| 148 | 148 |
/// |
| 149 | 149 |
/// This function returns the target node of an arc. |
| 150 | 150 |
Node target(const Arc&) const { return INVALID; }
|
| 151 | 151 |
|
| 152 | 152 |
/// \brief Return the opposite node on the given arc. |
| 153 | 153 |
/// |
| 154 | 154 |
/// This function returns the opposite node on the given arc. |
| 155 | 155 |
Node oppositeNode(const Node&, const Arc&) const {
|
| 156 | 156 |
return INVALID; |
| 157 | 157 |
} |
| 158 | 158 |
|
| 159 | 159 |
template <typename _Digraph> |
| 160 | 160 |
struct Constraints {
|
| 161 | 161 |
typedef typename _Digraph::Node Node; |
| 162 | 162 |
typedef typename _Digraph::Arc Arc; |
| 163 | 163 |
|
| 164 | 164 |
void constraints() {
|
| 165 | 165 |
checkConcept<GraphItem<'n'>, Node>(); |
| 166 | 166 |
checkConcept<GraphItem<'a'>, Arc>(); |
| 167 | 167 |
{
|
| 168 | 168 |
Node n; |
| 169 | 169 |
Arc e(INVALID); |
| 170 | 170 |
n = digraph.source(e); |
| 171 | 171 |
n = digraph.target(e); |
| 172 | 172 |
n = digraph.oppositeNode(n, e); |
| 173 | 173 |
} |
| 174 | 174 |
} |
| 175 | 175 |
|
| 176 | 176 |
const _Digraph& digraph; |
| 177 | 177 |
}; |
| 178 | 178 |
}; |
| 179 | 179 |
|
| 180 | 180 |
/// \brief Base skeleton class for undirected graphs. |
| 181 | 181 |
/// |
| 182 | 182 |
/// This class describes the base interface of undirected graph types. |
| 183 | 183 |
/// All graph %concepts have to conform to this class. |
| 184 | 184 |
/// It extends the interface of \ref BaseDigraphComponent with an |
| 185 | 185 |
/// \c Edge type and functions to get the end nodes of edges, |
| 186 | 186 |
/// to convert from arcs to edges and to get both direction of edges. |
| 187 | 187 |
class BaseGraphComponent : public BaseDigraphComponent {
|
| 188 | 188 |
public: |
| 189 | 189 |
|
| 190 | 190 |
typedef BaseGraphComponent Graph; |
| 191 | 191 |
|
| 192 | 192 |
typedef BaseDigraphComponent::Node Node; |
| 193 | 193 |
typedef BaseDigraphComponent::Arc Arc; |
| 194 | 194 |
|
| 195 | 195 |
/// \brief Undirected edge class of the graph. |
| 196 | 196 |
/// |
| 197 | 197 |
/// This class represents the undirected edges of the graph. |
| 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. |
| 134 | 134 |
/// \param i The item to insert. |
| 135 | 135 |
/// \param p The priority of the item. |
| 136 | 136 |
/// \pre \e i must not be stored in the heap. |
| 137 | 137 |
#ifdef DOXYGEN |
| 138 | 138 |
void push(const Item &i, const Prio &p) {}
|
| 139 | 139 |
#else |
| 140 | 140 |
void push(const Item&, const Prio&) {}
|
| 141 | 141 |
#endif |
| 142 | 142 |
|
| 143 | 143 |
/// \brief Return the item having minimum priority. |
| 144 | 144 |
/// |
| 145 | 145 |
/// This function returns the item having minimum priority. |
| 146 | 146 |
/// \pre The heap must be non-empty. |
| 147 | 147 |
Item top() const { return Item(); }
|
| 148 | 148 |
|
| 149 | 149 |
/// \brief The minimum priority. |
| 150 | 150 |
/// |
| 151 | 151 |
/// This function returns the minimum priority. |
| 152 | 152 |
/// \pre The heap must be non-empty. |
| 153 | 153 |
Prio prio() const { return Prio(); }
|
| 154 | 154 |
|
| 155 | 155 |
/// \brief Remove the item having minimum priority. |
| 156 | 156 |
/// |
| 157 | 157 |
/// This function removes the item having minimum priority. |
| 158 | 158 |
/// \pre The heap must be non-empty. |
| 159 | 159 |
void pop() {}
|
| 160 | 160 |
|
| 161 | 161 |
/// \brief Remove the given item from the heap. |
| 162 | 162 |
/// |
| 163 | 163 |
/// This function removes the given item from the heap if it is |
| 164 | 164 |
/// already stored. |
| 165 | 165 |
/// \param i The item to delete. |
| 166 | 166 |
/// \pre \e i must be in the heap. |
| 167 | 167 |
#ifdef DOXYGEN |
| 168 | 168 |
void erase(const Item &i) {}
|
| 169 | 169 |
#else |
| 170 | 170 |
void erase(const Item&) {}
|
| 171 | 171 |
#endif |
| 172 | 172 |
|
| 173 | 173 |
/// \brief The priority of the given item. |
| 174 | 174 |
/// |
| 175 | 175 |
/// This function returns the priority of the given item. |
| 176 | 176 |
/// \param i The item. |
| 177 | 177 |
/// \pre \e i must be in the heap. |
| 178 | 178 |
#ifdef DOXYGEN |
| 179 | 179 |
Prio operator[](const Item &i) const {}
|
| 180 | 180 |
#else |
| 181 | 181 |
Prio operator[](const Item&) const { return Prio(); }
|
| 182 | 182 |
#endif |
| 183 | 183 |
|
| 184 | 184 |
/// \brief Set the priority of an item or insert it, if it is |
| 185 | 185 |
/// not stored in the heap. |
| 186 | 186 |
/// |
| 187 | 187 |
/// This method sets the priority of the given item if it is |
| 188 | 188 |
/// already stored in the heap. Otherwise it inserts the given |
| 189 | 189 |
/// item into the heap with the given priority. |
| 190 | 190 |
/// |
| 191 | 191 |
/// \param i The item. |
| 192 | 192 |
/// \param p The priority. |
| 193 | 193 |
#ifdef DOXYGEN |
| 194 | 194 |
void set(const Item &i, const Prio &p) {}
|
| 195 | 195 |
#else |
| 196 | 196 |
void set(const Item&, const Prio&) {}
|
| 197 | 197 |
#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_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 |
| 134 | 134 |
/// the number of the connected components minus one. Each value of the map |
| 135 | 135 |
/// will be set exactly once, and the values of a certain component will be |
| 136 | 136 |
/// set continuously. |
| 137 | 137 |
/// \return The number of connected components. |
| 138 | 138 |
/// \note By definition, the empty graph consists |
| 139 | 139 |
/// of zero connected components. |
| 140 | 140 |
/// |
| 141 | 141 |
/// \see connected(), countConnectedComponents() |
| 142 | 142 |
template <class Graph, class NodeMap> |
| 143 | 143 |
int connectedComponents(const Graph &graph, NodeMap &compMap) {
|
| 144 | 144 |
checkConcept<concepts::Graph, Graph>(); |
| 145 | 145 |
typedef typename Graph::Node Node; |
| 146 | 146 |
typedef typename Graph::Arc Arc; |
| 147 | 147 |
checkConcept<concepts::WriteMap<Node, int>, NodeMap>(); |
| 148 | 148 |
|
| 149 | 149 |
typedef NullMap<Node, Arc> PredMap; |
| 150 | 150 |
typedef NullMap<Node, int> DistMap; |
| 151 | 151 |
|
| 152 | 152 |
int compNum = 0; |
| 153 | 153 |
typename Bfs<Graph>:: |
| 154 | 154 |
template SetPredMap<PredMap>:: |
| 155 | 155 |
template SetDistMap<DistMap>:: |
| 156 | 156 |
Create bfs(graph); |
| 157 | 157 |
|
| 158 | 158 |
PredMap predMap; |
| 159 | 159 |
bfs.predMap(predMap); |
| 160 | 160 |
|
| 161 | 161 |
DistMap distMap; |
| 162 | 162 |
bfs.distMap(distMap); |
| 163 | 163 |
|
| 164 | 164 |
bfs.init(); |
| 165 | 165 |
for(typename Graph::NodeIt n(graph); n != INVALID; ++n) {
|
| 166 | 166 |
if(!bfs.reached(n)) {
|
| 167 | 167 |
bfs.addSource(n); |
| 168 | 168 |
while (!bfs.emptyQueue()) {
|
| 169 | 169 |
compMap.set(bfs.nextNode(), compNum); |
| 170 | 170 |
bfs.processNextNode(); |
| 171 | 171 |
} |
| 172 | 172 |
++compNum; |
| 173 | 173 |
} |
| 174 | 174 |
} |
| 175 | 175 |
return compNum; |
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
namespace _connectivity_bits {
|
| 179 | 179 |
|
| 180 | 180 |
template <typename Digraph, typename Iterator > |
| 181 | 181 |
struct LeaveOrderVisitor : public DfsVisitor<Digraph> {
|
| 182 | 182 |
public: |
| 183 | 183 |
typedef typename Digraph::Node Node; |
| 184 | 184 |
LeaveOrderVisitor(Iterator it) : _it(it) {}
|
| 185 | 185 |
|
| 186 | 186 |
void leave(const Node& node) {
|
| 187 | 187 |
*(_it++) = node; |
| 188 | 188 |
} |
| 189 | 189 |
|
| 190 | 190 |
private: |
| 191 | 191 |
Iterator _it; |
| 192 | 192 |
}; |
| 193 | 193 |
|
| 194 | 194 |
template <typename Digraph, typename Map> |
| 195 | 195 |
struct FillMapVisitor : public DfsVisitor<Digraph> {
|
| 196 | 196 |
public: |
| 197 | 197 |
typedef typename Digraph::Node Node; |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_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; \ |
| 134 | 134 |
typedef Graph::EdgeMap<double> DoubleEdgeMap |
| 135 | 135 |
|
| 136 | 136 |
///Create convenience typedefs for the graph types and iterators |
| 137 | 137 |
|
| 138 | 138 |
///\see GRAPH_TYPEDEFS |
| 139 | 139 |
/// |
| 140 | 140 |
///\note Use this macro, if the graph type is a dependent type, |
| 141 | 141 |
///ie. the graph type depend on a template parameter. |
| 142 | 142 |
#define TEMPLATE_GRAPH_TYPEDEFS(Graph) \ |
| 143 | 143 |
TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \ |
| 144 | 144 |
typedef typename Graph::Edge Edge; \ |
| 145 | 145 |
typedef typename Graph::EdgeIt EdgeIt; \ |
| 146 | 146 |
typedef typename Graph::IncEdgeIt IncEdgeIt; \ |
| 147 | 147 |
typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \ |
| 148 | 148 |
typedef typename Graph::template EdgeMap<int> IntEdgeMap; \ |
| 149 | 149 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap |
| 150 | 150 |
|
| 151 | 151 |
/// \brief Function to count the items in a graph. |
| 152 | 152 |
/// |
| 153 | 153 |
/// This function counts the items (nodes, arcs etc.) in a graph. |
| 154 | 154 |
/// The complexity of the function is linear because |
| 155 | 155 |
/// it iterates on all of the items. |
| 156 | 156 |
template <typename Graph, typename Item> |
| 157 | 157 |
inline int countItems(const Graph& g) {
|
| 158 | 158 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
| 159 | 159 |
int num = 0; |
| 160 | 160 |
for (ItemIt it(g); it != INVALID; ++it) {
|
| 161 | 161 |
++num; |
| 162 | 162 |
} |
| 163 | 163 |
return num; |
| 164 | 164 |
} |
| 165 | 165 |
|
| 166 | 166 |
// Node counting: |
| 167 | 167 |
|
| 168 | 168 |
namespace _core_bits {
|
| 169 | 169 |
|
| 170 | 170 |
template <typename Graph, typename Enable = void> |
| 171 | 171 |
struct CountNodesSelector {
|
| 172 | 172 |
static int count(const Graph &g) {
|
| 173 | 173 |
return countItems<Graph, typename Graph::Node>(g); |
| 174 | 174 |
} |
| 175 | 175 |
}; |
| 176 | 176 |
|
| 177 | 177 |
template <typename Graph> |
| 178 | 178 |
struct CountNodesSelector< |
| 179 | 179 |
Graph, typename |
| 180 | 180 |
enable_if<typename Graph::NodeNumTag, void>::type> |
| 181 | 181 |
{
|
| 182 | 182 |
static int count(const Graph &g) {
|
| 183 | 183 |
return g.nodeNum(); |
| 184 | 184 |
} |
| 185 | 185 |
}; |
| 186 | 186 |
} |
| 187 | 187 |
|
| 188 | 188 |
/// \brief Function to count the nodes in the graph. |
| 189 | 189 |
/// |
| 190 | 190 |
/// This function counts the nodes in the graph. |
| 191 | 191 |
/// The complexity of the function is <em>O</em>(<em>n</em>), but for some |
| 192 | 192 |
/// graph structures it is specialized to run in <em>O</em>(1). |
| 193 | 193 |
/// |
| 194 | 194 |
/// \note If the graph contains a \c nodeNum() member function and a |
| 195 | 195 |
/// \c NodeNumTag tag then this function calls directly the member |
| 196 | 196 |
/// function to query the cardinality of the node set. |
| 197 | 197 |
template <typename Graph> |
| ... | ... |
@@ -1050,385 +1050,386 @@ |
| 1050 | 1050 |
|
| 1051 | 1051 |
public: |
| 1052 | 1052 |
|
| 1053 | 1053 |
typedef typename GR::Arc Arc; |
| 1054 | 1054 |
typedef typename GR::Node Node; |
| 1055 | 1055 |
|
| 1056 | 1056 |
/// \brief Constructor. |
| 1057 | 1057 |
/// |
| 1058 | 1058 |
/// Construct a new ConArcIt iterating on the arcs that |
| 1059 | 1059 |
/// connects nodes \c u and \c v. |
| 1060 | 1060 |
ConArcIt(const GR& g, Node u, Node v) : _graph(g) {
|
| 1061 | 1061 |
Parent::operator=(findArc(_graph, u, v)); |
| 1062 | 1062 |
} |
| 1063 | 1063 |
|
| 1064 | 1064 |
/// \brief Constructor. |
| 1065 | 1065 |
/// |
| 1066 | 1066 |
/// Construct a new ConArcIt that continues the iterating from arc \c a. |
| 1067 | 1067 |
ConArcIt(const GR& g, Arc a) : Parent(a), _graph(g) {}
|
| 1068 | 1068 |
|
| 1069 | 1069 |
/// \brief Increment operator. |
| 1070 | 1070 |
/// |
| 1071 | 1071 |
/// It increments the iterator and gives back the next arc. |
| 1072 | 1072 |
ConArcIt& operator++() {
|
| 1073 | 1073 |
Parent::operator=(findArc(_graph, _graph.source(*this), |
| 1074 | 1074 |
_graph.target(*this), *this)); |
| 1075 | 1075 |
return *this; |
| 1076 | 1076 |
} |
| 1077 | 1077 |
private: |
| 1078 | 1078 |
const GR& _graph; |
| 1079 | 1079 |
}; |
| 1080 | 1080 |
|
| 1081 | 1081 |
namespace _core_bits {
|
| 1082 | 1082 |
|
| 1083 | 1083 |
template <typename Graph, typename Enable = void> |
| 1084 | 1084 |
struct FindEdgeSelector {
|
| 1085 | 1085 |
typedef typename Graph::Node Node; |
| 1086 | 1086 |
typedef typename Graph::Edge Edge; |
| 1087 | 1087 |
static Edge find(const Graph &g, Node u, Node v, Edge e) {
|
| 1088 | 1088 |
bool b; |
| 1089 | 1089 |
if (u != v) {
|
| 1090 | 1090 |
if (e == INVALID) {
|
| 1091 | 1091 |
g.firstInc(e, b, u); |
| 1092 | 1092 |
} else {
|
| 1093 | 1093 |
b = g.u(e) == u; |
| 1094 | 1094 |
g.nextInc(e, b); |
| 1095 | 1095 |
} |
| 1096 | 1096 |
while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) {
|
| 1097 | 1097 |
g.nextInc(e, b); |
| 1098 | 1098 |
} |
| 1099 | 1099 |
} else {
|
| 1100 | 1100 |
if (e == INVALID) {
|
| 1101 | 1101 |
g.firstInc(e, b, u); |
| 1102 | 1102 |
} else {
|
| 1103 | 1103 |
b = true; |
| 1104 | 1104 |
g.nextInc(e, b); |
| 1105 | 1105 |
} |
| 1106 | 1106 |
while (e != INVALID && (!b || g.v(e) != v)) {
|
| 1107 | 1107 |
g.nextInc(e, b); |
| 1108 | 1108 |
} |
| 1109 | 1109 |
} |
| 1110 | 1110 |
return e; |
| 1111 | 1111 |
} |
| 1112 | 1112 |
}; |
| 1113 | 1113 |
|
| 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) {
|
| 1371 | 1372 |
if (_left[arc] == INVALID) {
|
| 1372 | 1373 |
if (_right[arc] != INVALID) {
|
| 1373 | 1374 |
_parent[_right[arc]] = _parent[arc]; |
| 1374 | 1375 |
} |
| 1375 | 1376 |
if (_parent[arc] != INVALID) {
|
| 1376 | 1377 |
if (_left[_parent[arc]] == arc) {
|
| 1377 | 1378 |
_left[_parent[arc]] = _right[arc]; |
| 1378 | 1379 |
} else {
|
| 1379 | 1380 |
_right[_parent[arc]] = _right[arc]; |
| 1380 | 1381 |
} |
| 1381 | 1382 |
} else {
|
| 1382 | 1383 |
_head[_g.source(arc)] = _right[arc]; |
| 1383 | 1384 |
} |
| 1384 | 1385 |
} else if (_right[arc] == INVALID) {
|
| 1385 | 1386 |
_parent[_left[arc]] = _parent[arc]; |
| 1386 | 1387 |
if (_parent[arc] != INVALID) {
|
| 1387 | 1388 |
if (_left[_parent[arc]] == arc) {
|
| 1388 | 1389 |
_left[_parent[arc]] = _left[arc]; |
| 1389 | 1390 |
} else {
|
| 1390 | 1391 |
_right[_parent[arc]] = _left[arc]; |
| 1391 | 1392 |
} |
| 1392 | 1393 |
} else {
|
| 1393 | 1394 |
_head[_g.source(arc)] = _left[arc]; |
| 1394 | 1395 |
} |
| 1395 | 1396 |
} else {
|
| 1396 | 1397 |
Arc e = _left[arc]; |
| 1397 | 1398 |
if (_right[e] != INVALID) {
|
| 1398 | 1399 |
e = _right[e]; |
| 1399 | 1400 |
while (_right[e] != INVALID) {
|
| 1400 | 1401 |
e = _right[e]; |
| 1401 | 1402 |
} |
| 1402 | 1403 |
Arc s = _parent[e]; |
| 1403 | 1404 |
_right[_parent[e]] = _left[e]; |
| 1404 | 1405 |
if (_left[e] != INVALID) {
|
| 1405 | 1406 |
_parent[_left[e]] = _parent[e]; |
| 1406 | 1407 |
} |
| 1407 | 1408 |
|
| 1408 | 1409 |
_left[e] = _left[arc]; |
| 1409 | 1410 |
_parent[_left[arc]] = e; |
| 1410 | 1411 |
_right[e] = _right[arc]; |
| 1411 | 1412 |
_parent[_right[arc]] = e; |
| 1412 | 1413 |
|
| 1413 | 1414 |
_parent[e] = _parent[arc]; |
| 1414 | 1415 |
if (_parent[arc] != INVALID) {
|
| 1415 | 1416 |
if (_left[_parent[arc]] == arc) {
|
| 1416 | 1417 |
_left[_parent[arc]] = e; |
| 1417 | 1418 |
} else {
|
| 1418 | 1419 |
_right[_parent[arc]] = e; |
| 1419 | 1420 |
} |
| 1420 | 1421 |
} |
| 1421 | 1422 |
splay(s); |
| 1422 | 1423 |
} else {
|
| 1423 | 1424 |
_right[e] = _right[arc]; |
| 1424 | 1425 |
_parent[_right[arc]] = e; |
| 1425 | 1426 |
_parent[e] = _parent[arc]; |
| 1426 | 1427 |
|
| 1427 | 1428 |
if (_parent[arc] != INVALID) {
|
| 1428 | 1429 |
if (_left[_parent[arc]] == arc) {
|
| 1429 | 1430 |
_left[_parent[arc]] = e; |
| 1430 | 1431 |
} else {
|
| 1431 | 1432 |
_right[_parent[arc]] = e; |
| 1432 | 1433 |
} |
| 1433 | 1434 |
} else {
|
| 1434 | 1435 |
_head[_g.source(arc)] = e; |
| 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 |
|
| 134 | 134 |
/// The type of the digraph |
| 135 | 135 |
typedef typename TR::Digraph Digraph; |
| 136 | 136 |
/// The type of the flow amounts, capacity bounds and supply values |
| 137 | 137 |
typedef typename TR::Value Value; |
| 138 | 138 |
/// The type of the arc costs |
| 139 | 139 |
typedef typename TR::Cost Cost; |
| 140 | 140 |
|
| 141 | 141 |
/// \brief The large cost type |
| 142 | 142 |
/// |
| 143 | 143 |
/// The large cost type used for internal computations. |
| 144 | 144 |
/// By default, it is \c long \c long if the \c Cost type is integer, |
| 145 | 145 |
/// otherwise it is \c double. |
| 146 | 146 |
typedef typename TR::LargeCost LargeCost; |
| 147 | 147 |
|
| 148 | 148 |
/// The \ref CostScalingDefaultTraits "traits class" of the algorithm |
| 149 | 149 |
typedef TR Traits; |
| 150 | 150 |
|
| 151 | 151 |
public: |
| 152 | 152 |
|
| 153 | 153 |
/// \brief Problem type constants for the \c run() function. |
| 154 | 154 |
/// |
| 155 | 155 |
/// Enum type containing the problem type constants that can be |
| 156 | 156 |
/// returned by the \ref run() function of the algorithm. |
| 157 | 157 |
enum ProblemType {
|
| 158 | 158 |
/// The problem has no feasible solution (flow). |
| 159 | 159 |
INFEASIBLE, |
| 160 | 160 |
/// The problem has optimal solution (i.e. it is feasible and |
| 161 | 161 |
/// bounded), and the algorithm has found optimal flow and node |
| 162 | 162 |
/// potentials (primal and dual solutions). |
| 163 | 163 |
OPTIMAL, |
| 164 | 164 |
/// The digraph contains an arc of negative cost and infinite |
| 165 | 165 |
/// upper bound. It means that the objective function is unbounded |
| 166 | 166 |
/// on that arc, however, note that it could actually be bounded |
| 167 | 167 |
/// over the feasible flows, but this algroithm cannot handle |
| 168 | 168 |
/// these cases. |
| 169 | 169 |
UNBOUNDED |
| 170 | 170 |
}; |
| 171 | 171 |
|
| 172 | 172 |
/// \brief Constants for selecting the internal method. |
| 173 | 173 |
/// |
| 174 | 174 |
/// Enum type containing constants for selecting the internal method |
| 175 | 175 |
/// for the \ref run() function. |
| 176 | 176 |
/// |
| 177 | 177 |
/// \ref CostScaling provides three internal methods that differ mainly |
| 178 | 178 |
/// in their base operations, which are used in conjunction with the |
| 179 | 179 |
/// relabel operation. |
| 180 | 180 |
/// By default, the so called \ref PARTIAL_AUGMENT |
| 181 | 181 |
/// "Partial Augment-Relabel" method is used, which proved to be |
| 182 | 182 |
/// the most efficient and the most robust on various test inputs. |
| 183 | 183 |
/// However, the other methods can be selected using the \ref run() |
| 184 | 184 |
/// function with the proper parameter. |
| 185 | 185 |
enum Method {
|
| 186 | 186 |
/// Local push operations are used, i.e. flow is moved only on one |
| 187 | 187 |
/// admissible arc at once. |
| 188 | 188 |
PUSH, |
| 189 | 189 |
/// Augment operations are used, i.e. flow is moved on admissible |
| 190 | 190 |
/// paths from a node with excess to a node with deficit. |
| 191 | 191 |
AUGMENT, |
| 192 | 192 |
/// Partial augment operations are used, i.e. flow is moved on |
| 193 | 193 |
/// admissible paths started from a node with excess, but the |
| 194 | 194 |
/// lengths of these paths are limited. This method can be viewed |
| 195 | 195 |
/// as a combined version of the previous two operations. |
| 196 | 196 |
PARTIAL_AUGMENT |
| 197 | 197 |
}; |
| 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; |
| 134 | 134 |
std::vector<Value> values; |
| 135 | 135 |
|
| 136 | 136 |
for(ExprIterator it=b; it!=e; ++it) {
|
| 137 | 137 |
indices.push_back(it->first); |
| 138 | 138 |
values.push_back(it->second); |
| 139 | 139 |
rowlist.push_back(i); |
| 140 | 140 |
} |
| 141 | 141 |
|
| 142 | 142 |
CPXchgcoeflist(cplexEnv(), _prob, values.size(), |
| 143 | 143 |
&rowlist.front(), &indices.front(), &values.front()); |
| 144 | 144 |
|
| 145 | 145 |
return i; |
| 146 | 146 |
} |
| 147 | 147 |
|
| 148 | 148 |
void CplexBase::_eraseCol(int i) {
|
| 149 | 149 |
CPXdelcols(cplexEnv(), _prob, i, i); |
| 150 | 150 |
} |
| 151 | 151 |
|
| 152 | 152 |
void CplexBase::_eraseRow(int i) {
|
| 153 | 153 |
CPXdelrows(cplexEnv(), _prob, i, i); |
| 154 | 154 |
} |
| 155 | 155 |
|
| 156 | 156 |
void CplexBase::_eraseColId(int i) {
|
| 157 | 157 |
cols.eraseIndex(i); |
| 158 | 158 |
cols.shiftIndices(i); |
| 159 | 159 |
} |
| 160 | 160 |
void CplexBase::_eraseRowId(int i) {
|
| 161 | 161 |
rows.eraseIndex(i); |
| 162 | 162 |
rows.shiftIndices(i); |
| 163 | 163 |
} |
| 164 | 164 |
|
| 165 | 165 |
void CplexBase::_getColName(int col, std::string &name) const {
|
| 166 | 166 |
int size; |
| 167 | 167 |
CPXgetcolname(cplexEnv(), _prob, 0, 0, 0, &size, col, col); |
| 168 | 168 |
if (size == 0) {
|
| 169 | 169 |
name.clear(); |
| 170 | 170 |
return; |
| 171 | 171 |
} |
| 172 | 172 |
|
| 173 | 173 |
size *= -1; |
| 174 | 174 |
std::vector<char> buf(size); |
| 175 | 175 |
char *cname; |
| 176 | 176 |
int tmp; |
| 177 | 177 |
CPXgetcolname(cplexEnv(), _prob, &cname, &buf.front(), size, |
| 178 | 178 |
&tmp, col, col); |
| 179 | 179 |
name = cname; |
| 180 | 180 |
} |
| 181 | 181 |
|
| 182 | 182 |
void CplexBase::_setColName(int col, const std::string &name) {
|
| 183 | 183 |
char *cname; |
| 184 | 184 |
cname = const_cast<char*>(name.c_str()); |
| 185 | 185 |
CPXchgcolname(cplexEnv(), _prob, 1, &col, &cname); |
| 186 | 186 |
} |
| 187 | 187 |
|
| 188 | 188 |
int CplexBase::_colByName(const std::string& name) const {
|
| 189 | 189 |
int index; |
| 190 | 190 |
if (CPXgetcolindex(cplexEnv(), _prob, |
| 191 | 191 |
const_cast<char*>(name.c_str()), &index) == 0) {
|
| 192 | 192 |
return index; |
| 193 | 193 |
} |
| 194 | 194 |
return -1; |
| 195 | 195 |
} |
| 196 | 196 |
|
| 197 | 197 |
void CplexBase::_getRowName(int row, std::string &name) const {
|
| 1 |
/* -*- C++ -*- |
|
| 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
| 2 | 2 |
* |
| 3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
| 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_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, |
| 134 | 134 |
/// The "Cancel And Tighten" algorithm, which can be viewed as an |
| 135 | 135 |
/// improved version of the previous method |
| 136 | 136 |
/// \ref goldberg89cyclecanceling. |
| 137 | 137 |
/// It is faster both in theory and in practice, its running time |
| 138 | 138 |
/// complexity is O(n<sup>2</sup>m<sup>2</sup>log(n)). |
| 139 | 139 |
CANCEL_AND_TIGHTEN |
| 140 | 140 |
}; |
| 141 | 141 |
|
| 142 | 142 |
private: |
| 143 | 143 |
|
| 144 | 144 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
| 145 | 145 |
|
| 146 | 146 |
typedef std::vector<int> IntVector; |
| 147 | 147 |
typedef std::vector<double> DoubleVector; |
| 148 | 148 |
typedef std::vector<Value> ValueVector; |
| 149 | 149 |
typedef std::vector<Cost> CostVector; |
| 150 | 150 |
typedef std::vector<char> BoolVector; |
| 151 | 151 |
// Note: vector<char> is used instead of vector<bool> for efficiency reasons |
| 152 | 152 |
|
| 153 | 153 |
private: |
| 154 | 154 |
|
| 155 | 155 |
template <typename KT, typename VT> |
| 156 | 156 |
class StaticVectorMap {
|
| 157 | 157 |
public: |
| 158 | 158 |
typedef KT Key; |
| 159 | 159 |
typedef VT Value; |
| 160 | 160 |
|
| 161 | 161 |
StaticVectorMap(std::vector<Value>& v) : _v(v) {}
|
| 162 | 162 |
|
| 163 | 163 |
const Value& operator[](const Key& key) const {
|
| 164 | 164 |
return _v[StaticDigraph::id(key)]; |
| 165 | 165 |
} |
| 166 | 166 |
|
| 167 | 167 |
Value& operator[](const Key& key) {
|
| 168 | 168 |
return _v[StaticDigraph::id(key)]; |
| 169 | 169 |
} |
| 170 | 170 |
|
| 171 | 171 |
void set(const Key& key, const Value& val) {
|
| 172 | 172 |
_v[StaticDigraph::id(key)] = val; |
| 173 | 173 |
} |
| 174 | 174 |
|
| 175 | 175 |
private: |
| 176 | 176 |
std::vector<Value>& _v; |
| 177 | 177 |
}; |
| 178 | 178 |
|
| 179 | 179 |
typedef StaticVectorMap<StaticDigraph::Node, Cost> CostNodeMap; |
| 180 | 180 |
typedef StaticVectorMap<StaticDigraph::Arc, Cost> CostArcMap; |
| 181 | 181 |
|
| 182 | 182 |
private: |
| 183 | 183 |
|
| 184 | 184 |
|
| 185 | 185 |
// Data related to the underlying digraph |
| 186 | 186 |
const GR &_graph; |
| 187 | 187 |
int _node_num; |
| 188 | 188 |
int _arc_num; |
| 189 | 189 |
int _res_node_num; |
| 190 | 190 |
int _res_arc_num; |
| 191 | 191 |
int _root; |
| 192 | 192 |
|
| 193 | 193 |
// Parameters of the problem |
| 194 | 194 |
bool _have_lower; |
| 195 | 195 |
Value _sum_supply; |
| 196 | 196 |
|
| 197 | 197 |
// Data structures for storing the digraph |
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)