| 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)) |
| 198 | 198 |
.run(); |
| 199 | 199 |
\endcode |
| 200 | 200 |
The \c functorToMap() function makes an \c int to \c Color map from the |
| 201 | 201 |
\c nodeColor() function. The \c composeMap() compose the \c degree_map |
| 202 | 202 |
and the previously created map. The composed map is a proper function to |
| 203 | 203 |
get the color of each node. |
| 204 | 204 |
|
| 205 | 205 |
The usage with class type algorithms is little bit harder. In this |
| 206 | 206 |
case the function type map adaptors can not be used, because the |
| 207 | 207 |
function map adaptors give back temporary objects. |
| 208 | 208 |
\code |
| 209 | 209 |
Digraph graph; |
| 210 | 210 |
|
| 211 | 211 |
typedef Digraph::ArcMap<double> DoubleArcMap; |
| 212 | 212 |
DoubleArcMap length(graph); |
| 213 | 213 |
DoubleArcMap speed(graph); |
| 214 | 214 |
|
| 215 | 215 |
typedef DivMap<DoubleArcMap, DoubleArcMap> TimeMap; |
| 216 | 216 |
TimeMap time(length, speed); |
| 217 | 217 |
|
| 218 | 218 |
Dijkstra<Digraph, TimeMap> dijkstra(graph, time); |
| 219 | 219 |
dijkstra.run(source, target); |
| 220 | 220 |
\endcode |
| 221 | 221 |
We have a length map and a maximum speed map on the arcs of a digraph. |
| 222 | 222 |
The minimum time to pass the arc can be calculated as the division of |
| 223 | 223 |
the two maps which can be done implicitly with the \c DivMap template |
| 224 | 224 |
class. We use the implicit minimum time map as the length map of the |
| 225 | 225 |
\c Dijkstra algorithm. |
| 226 | 226 |
*/ |
| 227 | 227 |
|
| 228 | 228 |
/** |
| 229 | 229 |
@defgroup paths Path Structures |
| 230 | 230 |
@ingroup datas |
| 231 | 231 |
\brief %Path structures implemented in LEMON. |
| 232 | 232 |
|
| 233 | 233 |
This group contains the path structures implemented in LEMON. |
| 234 | 234 |
|
| 235 | 235 |
LEMON provides flexible data structures to work with paths. |
| 236 | 236 |
All of them have similar interfaces and they can be copied easily with |
| 237 | 237 |
assignment operators and copy constructors. This makes it easy and |
| 238 | 238 |
efficient to have e.g. the Dijkstra algorithm to store its result in |
| 239 | 239 |
any kind of path structure. |
| 240 | 240 |
|
| 241 | 241 |
\sa \ref concepts::Path "Path concept" |
| 242 | 242 |
*/ |
| 243 | 243 |
|
| 244 | 244 |
/** |
| 245 | 245 |
@defgroup heaps Heap Structures |
| 246 | 246 |
@ingroup datas |
| 247 | 247 |
\brief %Heap structures implemented in LEMON. |
| 248 | 248 |
|
| 249 | 249 |
This group contains the heap structures implemented in LEMON. |
| 250 | 250 |
|
| 251 | 251 |
LEMON provides several heap classes. They are efficient implementations |
| 252 | 252 |
of the abstract data type \e priority \e queue. They store items with |
| 253 | 253 |
specified values called \e priorities in such a way that finding and |
| 254 | 254 |
removing the item with minimum priority are efficient. |
| 255 | 255 |
The basic operations are adding and erasing items, changing the priority |
| 256 | 256 |
of an item, etc. |
| 257 | 257 |
|
| 258 | 258 |
Heaps are crucial in several algorithms, such as Dijkstra and Prim. |
| 259 | 259 |
The heap implementations have the same interface, thus any of them can be |
| 260 | 260 |
used easily in such algorithms. |
| 261 | 261 |
|
| 262 | 262 |
\sa \ref concepts::Heap "Heap concept" |
| 263 | 263 |
*/ |
| 264 | 264 |
|
| 265 | 265 |
/** |
| 266 | 266 |
@defgroup matrices Matrices |
| 267 | 267 |
@ingroup datas |
| 268 | 268 |
\brief Two dimensional data storages implemented in LEMON. |
| 269 | 269 |
|
| 270 | 270 |
This group contains two dimensional data storages implemented in LEMON. |
| 271 | 271 |
*/ |
| 272 | 272 |
|
| 273 | 273 |
/** |
| 274 | 274 |
@defgroup auxdat Auxiliary Data Structures |
| 275 | 275 |
@ingroup datas |
| 276 | 276 |
\brief Auxiliary data structures implemented in LEMON. |
| 277 | 277 |
|
| 278 | 278 |
This group contains some data structures implemented in LEMON in |
| 279 | 279 |
order to make it easier to implement combinatorial algorithms. |
| 280 | 280 |
*/ |
| 281 | 281 |
|
| 282 | 282 |
/** |
| 283 | 283 |
@defgroup geomdat Geometric Data Structures |
| 284 | 284 |
@ingroup auxdat |
| 285 | 285 |
\brief Geometric data structures implemented in LEMON. |
| 286 | 286 |
|
| 287 | 287 |
This group contains geometric data structures implemented in LEMON. |
| 288 | 288 |
|
| 289 | 289 |
- \ref lemon::dim2::Point "dim2::Point" implements a two dimensional |
| 290 | 290 |
vector with the usual operations. |
| 291 | 291 |
- \ref lemon::dim2::Box "dim2::Box" can be used to determine the |
| 292 | 292 |
rectangular bounding box of a set of \ref lemon::dim2::Point |
| 293 | 293 |
"dim2::Point"'s. |
| 294 | 294 |
*/ |
| 295 | 295 |
|
| 296 | 296 |
/** |
| 297 | 297 |
@defgroup matrices Matrices |
| 298 | 298 |
@ingroup auxdat |
| 299 | 299 |
\brief Two dimensional data storages implemented in LEMON. |
| 300 | 300 |
|
| 301 | 301 |
This group contains two dimensional data storages implemented in LEMON. |
| 302 | 302 |
*/ |
| 303 | 303 |
|
| 304 | 304 |
/** |
| 305 | 305 |
@defgroup algs Algorithms |
| 306 | 306 |
\brief This group contains the several algorithms |
| 307 | 307 |
implemented in LEMON. |
| 308 | 308 |
|
| 309 | 309 |
This group contains the several algorithms |
| 310 | 310 |
implemented in LEMON. |
| 311 | 311 |
*/ |
| 312 | 312 |
|
| 313 | 313 |
/** |
| 314 | 314 |
@defgroup search Graph Search |
| 315 | 315 |
@ingroup algs |
| 316 | 316 |
\brief Common graph search algorithms. |
| 317 | 317 |
|
| 318 | 318 |
This group contains the common graph search algorithms, namely |
| 319 | 319 |
\e breadth-first \e search (BFS) and \e depth-first \e search (DFS) |
| 320 | 320 |
\ref clrs01algorithms. |
| 321 | 321 |
*/ |
| 322 | 322 |
|
| 323 | 323 |
/** |
| 324 | 324 |
@defgroup shortest_path Shortest Path Algorithms |
| 325 | 325 |
@ingroup algs |
| 326 | 326 |
\brief Algorithms for finding shortest paths. |
| 327 | 327 |
|
| 328 | 328 |
This group contains the algorithms for finding shortest paths in digraphs |
| 329 | 329 |
\ref clrs01algorithms. |
| 330 | 330 |
|
| 331 | 331 |
- \ref Dijkstra algorithm for finding shortest paths from a source node |
| 332 | 332 |
when all arc lengths are non-negative. |
| 333 | 333 |
- \ref BellmanFord "Bellman-Ford" algorithm for finding shortest paths |
| 334 | 334 |
from a source node when arc lenghts can be either positive or negative, |
| 335 | 335 |
but the digraph should not contain directed cycles with negative total |
| 336 | 336 |
length. |
| 337 | 337 |
- \ref FloydWarshall "Floyd-Warshall" and \ref Johnson "Johnson" algorithms |
| 338 | 338 |
for solving the \e all-pairs \e shortest \e paths \e problem when arc |
| 339 | 339 |
lenghts can be either positive or negative, but the digraph should |
| 340 | 340 |
not contain directed cycles with negative total length. |
| 341 | 341 |
- \ref Suurballe A successive shortest path algorithm for finding |
| 342 | 342 |
arc-disjoint paths between two nodes having minimum total length. |
| 343 | 343 |
*/ |
| 344 | 344 |
|
| 345 | 345 |
/** |
| 346 | 346 |
@defgroup spantree Minimum Spanning Tree Algorithms |
| 347 | 347 |
@ingroup algs |
| 348 | 348 |
\brief Algorithms for finding minimum cost spanning trees and arborescences. |
| 349 | 349 |
|
| 350 | 350 |
This group contains the algorithms for finding minimum cost spanning |
| 351 | 351 |
trees and arborescences \ref clrs01algorithms. |
| 352 | 352 |
*/ |
| 353 | 353 |
|
| 354 | 354 |
/** |
| 355 | 355 |
@defgroup max_flow Maximum Flow Algorithms |
| 356 | 356 |
@ingroup algs |
| 357 | 357 |
\brief Algorithms for finding maximum flows. |
| 358 | 358 |
|
| 359 | 359 |
This group contains the algorithms for finding maximum flows and |
| 360 | 360 |
feasible circulations \ref clrs01algorithms, \ref amo93networkflows. |
| 361 | 361 |
|
| 362 | 362 |
The \e maximum \e flow \e problem is to find a flow of maximum value between |
| 363 | 363 |
a single source and a single target. Formally, there is a \f$G=(V,A)\f$ |
| 364 | 364 |
digraph, a \f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function and
|
| 365 | 365 |
\f$s, t \in V\f$ source and target nodes. |
| 366 | 366 |
A maximum flow is an \f$f: A\rightarrow\mathbf{R}^+_0\f$ solution of the
|
| 367 | 367 |
following optimization problem. |
| 368 | 368 |
|
| 369 | 369 |
\f[ \max\sum_{sv\in A} f(sv) - \sum_{vs\in A} f(vs) \f]
|
| 370 | 370 |
\f[ \sum_{uv\in A} f(uv) = \sum_{vu\in A} f(vu)
|
| 371 | 371 |
\quad \forall u\in V\setminus\{s,t\} \f]
|
| 372 | 372 |
\f[ 0 \leq f(uv) \leq cap(uv) \quad \forall uv\in A \f] |
| 373 | 373 |
|
| 374 | 374 |
LEMON contains several algorithms for solving maximum flow problems: |
| 375 | 375 |
- \ref EdmondsKarp Edmonds-Karp algorithm |
| 376 | 376 |
\ref edmondskarp72theoretical. |
| 377 | 377 |
- \ref Preflow Goldberg-Tarjan's preflow push-relabel algorithm |
| 378 | 378 |
\ref goldberg88newapproach. |
| 379 | 379 |
- \ref DinitzSleatorTarjan Dinitz's blocking flow algorithm with dynamic trees |
| 380 | 380 |
\ref dinic70algorithm, \ref sleator83dynamic. |
| 381 | 381 |
- \ref GoldbergTarjan !Preflow push-relabel algorithm with dynamic trees |
| 382 | 382 |
\ref goldberg88newapproach, \ref sleator83dynamic. |
| 383 | 383 |
|
| 384 | 384 |
In most cases the \ref Preflow algorithm provides the |
| 385 | 385 |
fastest method for computing a maximum flow. All implementations |
| 386 | 386 |
also provide functions to query the minimum cut, which is the dual |
| 387 | 387 |
problem of maximum flow. |
| 388 | 388 |
|
| 389 | 389 |
\ref Circulation is a preflow push-relabel algorithm implemented directly |
| 390 | 390 |
for finding feasible circulations, which is a somewhat different problem, |
| 391 | 391 |
but it is strongly related to maximum flow. |
| 392 | 392 |
For more information, see \ref Circulation. |
| 393 | 393 |
*/ |
| 394 | 394 |
|
| 395 | 395 |
/** |
| 396 | 396 |
@defgroup min_cost_flow_algs Minimum Cost Flow Algorithms |
| 397 | 397 |
@ingroup algs |
| 398 | 398 |
|
| 399 | 399 |
\brief Algorithms for finding minimum cost flows and circulations. |
| 400 | 400 |
|
| 401 | 401 |
This group contains the algorithms for finding minimum cost flows and |
| 402 | 402 |
circulations \ref amo93networkflows. For more information about this |
| 403 | 403 |
problem and its dual solution, see \ref min_cost_flow |
| 404 | 404 |
"Minimum Cost Flow Problem". |
| 405 | 405 |
|
| 406 | 406 |
LEMON contains several algorithms for this problem. |
| 407 | 407 |
- \ref NetworkSimplex Primal Network Simplex algorithm with various |
| 408 | 408 |
pivot strategies \ref dantzig63linearprog, \ref kellyoneill91netsimplex. |
| 409 | 409 |
- \ref CostScaling Cost Scaling algorithm based on push/augment and |
| 410 | 410 |
relabel operations \ref goldberg90approximation, \ref goldberg97efficient, |
| 411 | 411 |
\ref bunnagel98efficient. |
| 412 | 412 |
- \ref CapacityScaling Capacity Scaling algorithm based on the successive |
| 413 | 413 |
shortest path method \ref edmondskarp72theoretical. |
| 414 | 414 |
- \ref CycleCanceling Cycle-Canceling algorithms, two of which are |
| 415 | 415 |
strongly polynomial \ref klein67primal, \ref goldberg89cyclecanceling. |
| 416 | 416 |
|
| 417 | 417 |
In general NetworkSimplex is the most efficient implementation, |
| 418 | 418 |
but in special cases other algorithms could be faster. |
| 419 | 419 |
For example, if the total supply and/or capacities are rather small, |
| 420 | 420 |
CapacityScaling is usually the fastest algorithm (without effective scaling). |
| 421 | 421 |
*/ |
| 422 | 422 |
|
| 423 | 423 |
/** |
| 424 | 424 |
@defgroup min_cut Minimum Cut Algorithms |
| 425 | 425 |
@ingroup algs |
| 426 | 426 |
|
| 427 | 427 |
\brief Algorithms for finding minimum cut in graphs. |
| 428 | 428 |
|
| 429 | 429 |
This group contains the algorithms for finding minimum cut in graphs. |
| 430 | 430 |
|
| 431 | 431 |
The \e minimum \e cut \e problem is to find a non-empty and non-complete |
| 432 | 432 |
\f$X\f$ subset of the nodes with minimum overall capacity on |
| 433 | 433 |
outgoing arcs. Formally, there is a \f$G=(V,A)\f$ digraph, a |
| 434 | 434 |
\f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function. The minimum
|
| 435 | 435 |
cut is the \f$X\f$ solution of the next optimization problem: |
| 436 | 436 |
|
| 437 | 437 |
\f[ \min_{X \subset V, X\not\in \{\emptyset, V\}}
|
| 438 | 438 |
\sum_{uv\in A: u\in X, v\not\in X}cap(uv) \f]
|
| 439 | 439 |
|
| 440 | 440 |
LEMON contains several algorithms related to minimum cut problems: |
| 441 | 441 |
|
| 442 | 442 |
- \ref HaoOrlin "Hao-Orlin algorithm" for calculating minimum cut |
| 443 | 443 |
in directed graphs. |
| 444 | 444 |
- \ref NagamochiIbaraki "Nagamochi-Ibaraki algorithm" for |
| 445 | 445 |
calculating minimum cut in undirected graphs. |
| 446 | 446 |
- \ref GomoryHu "Gomory-Hu tree computation" for calculating |
| 447 | 447 |
all-pairs minimum cut in undirected graphs. |
| 448 | 448 |
|
| 449 | 449 |
If you want to find minimum cut just between two distinict nodes, |
| 450 | 450 |
see the \ref max_flow "maximum flow problem". |
| 451 | 451 |
*/ |
| 452 | 452 |
|
| 453 | 453 |
/** |
| 454 | 454 |
@defgroup min_mean_cycle Minimum Mean Cycle Algorithms |
| 455 | 455 |
@ingroup algs |
| 456 | 456 |
\brief Algorithms for finding minimum mean cycles. |
| 457 | 457 |
|
| 458 | 458 |
This group contains the algorithms for finding minimum mean cycles |
| 459 | 459 |
\ref clrs01algorithms, \ref amo93networkflows. |
| 460 | 460 |
|
| 461 | 461 |
The \e minimum \e mean \e cycle \e problem is to find a directed cycle |
| 462 | 462 |
of minimum mean length (cost) in a digraph. |
| 463 | 463 |
The mean length of a cycle is the average length of its arcs, i.e. the |
| 464 | 464 |
ratio between the total length of the cycle and the number of arcs on it. |
| 465 | 465 |
|
| 466 | 466 |
This problem has an important connection to \e conservative \e length |
| 467 | 467 |
\e functions, too. A length function on the arcs of a digraph is called |
| 468 | 468 |
conservative if and only if there is no directed cycle of negative total |
| 469 | 469 |
length. For an arbitrary length function, the negative of the minimum |
| 470 | 470 |
cycle mean is the smallest \f$\epsilon\f$ value so that increasing the |
| 471 | 471 |
arc lengths uniformly by \f$\epsilon\f$ results in a conservative length |
| 472 | 472 |
function. |
| 473 | 473 |
|
| 474 | 474 |
LEMON contains three algorithms for solving the minimum mean cycle problem: |
| 475 | 475 |
- \ref Karp "Karp"'s original algorithm \ref amo93networkflows, |
| 476 | 476 |
\ref dasdan98minmeancycle. |
| 477 | 477 |
- \ref HartmannOrlin "Hartmann-Orlin"'s algorithm, which is an improved |
| 478 | 478 |
version of Karp's algorithm \ref dasdan98minmeancycle. |
| 479 | 479 |
- \ref Howard "Howard"'s policy iteration algorithm |
| 480 | 480 |
\ref dasdan98minmeancycle. |
| 481 | 481 |
|
| 482 | 482 |
In practice, the Howard algorithm proved to be by far the most efficient |
| 483 | 483 |
one, though the best known theoretical bound on its running time is |
| 484 | 484 |
exponential. |
| 485 | 485 |
Both Karp and HartmannOrlin algorithms run in time O(ne) and use space |
| 486 | 486 |
O(n<sup>2</sup>+e), but the latter one is typically faster due to the |
| 487 | 487 |
applied early termination scheme. |
| 488 | 488 |
*/ |
| 489 | 489 |
|
| 490 | 490 |
/** |
| 491 | 491 |
@defgroup matching Matching Algorithms |
| 492 | 492 |
@ingroup algs |
| 493 | 493 |
\brief Algorithms for finding matchings in graphs and bipartite graphs. |
| 494 | 494 |
|
| 495 | 495 |
This group contains the algorithms for calculating |
| 496 | 496 |
matchings in graphs and bipartite graphs. The general matching problem is |
| 497 | 497 |
finding a subset of the edges for which each node has at most one incident |
| 498 | 498 |
edge. |
| 499 | 499 |
|
| 500 | 500 |
There are several different algorithms for calculate matchings in |
| 501 | 501 |
graphs. The matching problems in bipartite graphs are generally |
| 502 | 502 |
easier than in general graphs. The goal of the matching optimization |
| 503 | 503 |
can be finding maximum cardinality, maximum weight or minimum cost |
| 504 | 504 |
matching. The search can be constrained to find perfect or |
| 505 | 505 |
maximum cardinality matching. |
| 506 | 506 |
|
| 507 | 507 |
The matching algorithms implemented in LEMON: |
| 508 | 508 |
- \ref MaxBipartiteMatching Hopcroft-Karp augmenting path algorithm |
| 509 | 509 |
for calculating maximum cardinality matching in bipartite graphs. |
| 510 | 510 |
- \ref PrBipartiteMatching Push-relabel algorithm |
| 511 | 511 |
for calculating maximum cardinality matching in bipartite graphs. |
| 512 | 512 |
- \ref MaxWeightedBipartiteMatching |
| 513 | 513 |
Successive shortest path algorithm for calculating maximum weighted |
| 514 | 514 |
matching and maximum weighted bipartite matching in bipartite graphs. |
| 515 | 515 |
- \ref MinCostMaxBipartiteMatching |
| 516 | 516 |
Successive shortest path algorithm for calculating minimum cost maximum |
| 517 | 517 |
matching in bipartite graphs. |
| 518 | 518 |
- \ref MaxMatching Edmond's blossom shrinking algorithm for calculating |
| 519 | 519 |
maximum cardinality matching in general graphs. |
| 520 | 520 |
- \ref MaxWeightedMatching Edmond's blossom shrinking algorithm for calculating |
| 521 | 521 |
maximum weighted matching in general graphs. |
| 522 | 522 |
- \ref MaxWeightedPerfectMatching |
| 523 | 523 |
Edmond's blossom shrinking algorithm for calculating maximum weighted |
| 524 | 524 |
perfect matching in general graphs. |
| 525 | 525 |
- \ref MaxFractionalMatching Push-relabel algorithm for calculating |
| 526 | 526 |
maximum cardinality fractional matching in general graphs. |
| 527 | 527 |
- \ref MaxWeightedFractionalMatching Augmenting path algorithm for calculating |
| 528 | 528 |
maximum weighted fractional matching in general graphs. |
| 529 | 529 |
- \ref MaxWeightedPerfectFractionalMatching |
| 530 | 530 |
Augmenting path algorithm for calculating maximum weighted |
| 531 | 531 |
perfect fractional matching in general graphs. |
| 532 | 532 |
|
| 533 | 533 |
\image html matching.png |
| 534 | 534 |
\image latex matching.eps "Min Cost Perfect Matching" width=\textwidth |
| 535 | 535 |
*/ |
| 536 | 536 |
|
| 537 | 537 |
/** |
| 538 | 538 |
@defgroup graph_properties Connectivity and Other Graph Properties |
| 539 | 539 |
@ingroup algs |
| 540 | 540 |
\brief Algorithms for discovering the graph properties |
| 541 | 541 |
|
| 542 | 542 |
This group contains the algorithms for discovering the graph properties |
| 543 | 543 |
like connectivity, bipartiteness, euler property, simplicity etc. |
| 544 | 544 |
|
| 545 | 545 |
\image html connected_components.png |
| 546 | 546 |
\image latex connected_components.eps "Connected components" width=\textwidth |
| 547 | 547 |
*/ |
| 548 | 548 |
|
| 549 | 549 |
/** |
| 550 | 550 |
@defgroup planar Planarity Embedding and Drawing |
| 551 | 551 |
@ingroup algs |
| 552 | 552 |
\brief Algorithms for planarity checking, embedding and drawing |
| 553 | 553 |
|
| 554 | 554 |
This group contains the algorithms for planarity checking, |
| 555 | 555 |
embedding and drawing. |
| 556 | 556 |
|
| 557 | 557 |
\image html planar.png |
| 558 | 558 |
\image latex planar.eps "Plane graph" width=\textwidth |
| 559 | 559 |
*/ |
| 560 | 560 |
|
| 561 | 561 |
/** |
| 562 | 562 |
@defgroup approx Approximation Algorithms |
| 563 | 563 |
@ingroup algs |
| 564 | 564 |
\brief Approximation algorithms. |
| 565 | 565 |
|
| 566 | 566 |
This group contains the approximation and heuristic algorithms |
| 567 | 567 |
implemented in LEMON. |
| 568 | 568 |
*/ |
| 569 | 569 |
|
| 570 | 570 |
/** |
| 571 | 571 |
@defgroup auxalg Auxiliary Algorithms |
| 572 | 572 |
@ingroup algs |
| 573 | 573 |
\brief Auxiliary algorithms implemented in LEMON. |
| 574 | 574 |
|
| 575 | 575 |
This group contains some algorithms implemented in LEMON |
| 576 | 576 |
in order to make it easier to implement complex algorithms. |
| 577 | 577 |
*/ |
| 578 | 578 |
|
| 579 | 579 |
/** |
| 580 | 580 |
@defgroup gen_opt_group General Optimization Tools |
| 581 | 581 |
\brief This group contains some general optimization frameworks |
| 582 | 582 |
implemented in LEMON. |
| 583 | 583 |
|
| 584 | 584 |
This group contains some general optimization frameworks |
| 585 | 585 |
implemented in LEMON. |
| 586 | 586 |
*/ |
| 587 | 587 |
|
| 588 | 588 |
/** |
| 589 | 589 |
@defgroup lp_group LP and MIP Solvers |
| 590 | 590 |
@ingroup gen_opt_group |
| 591 | 591 |
\brief LP and MIP solver interfaces for LEMON. |
| 592 | 592 |
|
| 593 | 593 |
This group contains LP and MIP solver interfaces for LEMON. |
| 594 | 594 |
Various LP solvers could be used in the same manner with this |
| 595 | 595 |
high-level interface. |
| 596 | 596 |
|
| 597 | 597 |
The currently supported solvers are \ref glpk, \ref clp, \ref cbc, |
| 598 | 598 |
\ref cplex, \ref soplex. |
| 599 | 599 |
*/ |
| 600 | 600 |
|
| 601 | 601 |
/** |
| 602 | 602 |
@defgroup lp_utils Tools for Lp and Mip Solvers |
| 603 | 603 |
@ingroup lp_group |
| 604 | 604 |
\brief Helper tools to the Lp and Mip solvers. |
| 605 | 605 |
|
| 606 | 606 |
This group adds some helper tools to general optimization framework |
| 607 | 607 |
implemented in LEMON. |
| 608 | 608 |
*/ |
| 609 | 609 |
|
| 610 | 610 |
/** |
| 611 | 611 |
@defgroup metah Metaheuristics |
| 612 | 612 |
@ingroup gen_opt_group |
| 613 | 613 |
\brief Metaheuristics for LEMON library. |
| 614 | 614 |
|
| 615 | 615 |
This group contains some metaheuristic optimization tools. |
| 616 | 616 |
*/ |
| 617 | 617 |
|
| 618 | 618 |
/** |
| 619 | 619 |
@defgroup utils Tools and Utilities |
| 620 | 620 |
\brief Tools and utilities for programming in LEMON |
| 621 | 621 |
|
| 622 | 622 |
Tools and utilities for programming in LEMON. |
| 623 | 623 |
*/ |
| 624 | 624 |
|
| 625 | 625 |
/** |
| 626 | 626 |
@defgroup gutils Basic Graph Utilities |
| 627 | 627 |
@ingroup utils |
| 628 | 628 |
\brief Simple basic graph utilities. |
| 629 | 629 |
|
| 630 | 630 |
This group contains some simple basic graph utilities. |
| 631 | 631 |
*/ |
| 632 | 632 |
|
| 633 | 633 |
/** |
| 634 | 634 |
@defgroup misc Miscellaneous Tools |
| 635 | 635 |
@ingroup utils |
| 636 | 636 |
\brief Tools for development, debugging and testing. |
| 637 | 637 |
|
| 638 | 638 |
This group contains several useful tools for development, |
| 639 | 639 |
debugging and testing. |
| 640 | 640 |
*/ |
| 641 | 641 |
|
| 642 | 642 |
/** |
| 643 | 643 |
@defgroup timecount Time Measuring and Counting |
| 644 | 644 |
@ingroup misc |
| 645 | 645 |
\brief Simple tools for measuring the performance of algorithms. |
| 646 | 646 |
|
| 647 | 647 |
This group contains simple tools for measuring the performance |
| 648 | 648 |
of algorithms. |
| 649 | 649 |
*/ |
| 650 | 650 |
|
| 651 | 651 |
/** |
| 652 | 652 |
@defgroup exceptions Exceptions |
| 653 | 653 |
@ingroup utils |
| 654 | 654 |
\brief Exceptions defined in LEMON. |
| 655 | 655 |
|
| 656 | 656 |
This group contains the exceptions defined in LEMON. |
| 657 | 657 |
*/ |
| 658 | 658 |
|
| 659 | 659 |
/** |
| 660 | 660 |
@defgroup io_group Input-Output |
| 661 | 661 |
\brief Graph Input-Output methods |
| 662 | 662 |
|
| 663 | 663 |
This group contains the tools for importing and exporting graphs |
| 664 | 664 |
and graph related data. Now it supports the \ref lgf-format |
| 665 | 665 |
"LEMON Graph Format", the \c DIMACS format and the encapsulated |
| 666 | 666 |
postscript (EPS) format. |
| 667 | 667 |
*/ |
| 668 | 668 |
|
| 669 | 669 |
/** |
| 670 | 670 |
@defgroup lemon_io LEMON Graph Format |
| 671 | 671 |
@ingroup io_group |
| 672 | 672 |
\brief Reading and writing LEMON Graph Format. |
| 673 | 673 |
|
| 674 | 674 |
This group contains methods for reading and writing |
| 675 | 675 |
\ref lgf-format "LEMON Graph Format". |
| 676 | 676 |
*/ |
| 677 | 677 |
|
| 678 | 678 |
/** |
| 679 | 679 |
@defgroup eps_io Postscript Exporting |
| 680 | 680 |
@ingroup io_group |
| 681 | 681 |
\brief General \c EPS drawer and graph exporter |
| 682 | 682 |
|
| 683 | 683 |
This group contains general \c EPS drawing methods and special |
| 684 | 684 |
graph exporting tools. |
| 685 | 685 |
*/ |
| 686 | 686 |
|
| 687 | 687 |
/** |
| 688 | 688 |
@defgroup dimacs_group DIMACS Format |
| 689 | 689 |
@ingroup io_group |
| 690 | 690 |
\brief Read and write files in DIMACS format |
| 691 | 691 |
|
| 692 | 692 |
Tools to read a digraph from or write it to a file in DIMACS format data. |
| 693 | 693 |
*/ |
| 694 | 694 |
|
| 695 | 695 |
/** |
| 696 | 696 |
@defgroup nauty_group NAUTY Format |
| 697 | 697 |
@ingroup io_group |
| 698 | 698 |
\brief Read \e Nauty format |
| 699 | 699 |
|
| 700 | 700 |
Tool to read graphs from \e Nauty format data. |
| 701 | 701 |
*/ |
| 702 | 702 |
|
| 703 | 703 |
/** |
| 704 | 704 |
@defgroup concept Concepts |
| 705 | 705 |
\brief Skeleton classes and concept checking classes |
| 706 | 706 |
|
| 707 | 707 |
This group contains the data/algorithm skeletons and concept checking |
| 708 | 708 |
classes implemented in LEMON. |
| 709 | 709 |
|
| 710 | 710 |
The purpose of the classes in this group is fourfold. |
| 711 | 711 |
|
| 712 | 712 |
- These classes contain the documentations of the %concepts. In order |
| 713 | 713 |
to avoid document multiplications, an implementation of a concept |
| 714 | 714 |
simply refers to the corresponding concept class. |
| 715 | 715 |
|
| 716 | 716 |
- These classes declare every functions, <tt>typedef</tt>s etc. an |
| 717 | 717 |
implementation of the %concepts should provide, however completely |
| 718 | 718 |
without implementations and real data structures behind the |
| 719 | 719 |
interface. On the other hand they should provide nothing else. All |
| 720 | 720 |
the algorithms working on a data structure meeting a certain concept |
| 721 | 721 |
should compile with these classes. (Though it will not run properly, |
| 722 | 722 |
of course.) In this way it is easily to check if an algorithm |
| 723 | 723 |
doesn't use any extra feature of a certain implementation. |
| 724 | 724 |
|
| 725 | 725 |
- The concept descriptor classes also provide a <em>checker class</em> |
| 726 | 726 |
that makes it possible to check whether a certain implementation of a |
| 727 | 727 |
concept indeed provides all the required features. |
| 728 | 728 |
|
| 729 | 729 |
- Finally, They can serve as a skeleton of a new implementation of a concept. |
| 730 | 730 |
*/ |
| 731 | 731 |
|
| 732 | 732 |
/** |
| 733 | 733 |
@defgroup graph_concepts Graph Structure Concepts |
| 734 | 734 |
@ingroup concept |
| 735 | 735 |
\brief Skeleton and concept checking classes for graph structures |
| 736 | 736 |
|
| 737 | 737 |
This group contains the skeletons and concept checking classes of |
| 738 | 738 |
graph structures. |
| 739 | 739 |
*/ |
| 740 | 740 |
|
| 741 | 741 |
/** |
| 742 | 742 |
@defgroup map_concepts Map Concepts |
| 743 | 743 |
@ingroup concept |
| 744 | 744 |
\brief Skeleton and concept checking classes for maps |
| 745 | 745 |
|
| 746 | 746 |
This group contains the skeletons and concept checking classes of maps. |
| 747 | 747 |
*/ |
| 748 | 748 |
|
| 749 | 749 |
/** |
| 750 | 750 |
@defgroup tools Standalone Utility Applications |
| 751 | 751 |
|
| 752 | 752 |
Some utility applications are listed here. |
| 753 | 753 |
|
| 754 | 754 |
The standard compilation procedure (<tt>./configure;make</tt>) will compile |
| 755 | 755 |
them, as well. |
| 756 | 756 |
*/ |
| 757 | 757 |
|
| 758 | 758 |
/** |
| 759 | 759 |
\anchor demoprograms |
| 760 | 760 |
|
| 761 | 761 |
@defgroup demos Demo Programs |
| 762 | 762 |
|
| 763 | 763 |
Some demo programs are listed here. Their full source codes can be found in |
| 764 | 764 |
the \c demo subdirectory of the source tree. |
| 765 | 765 |
|
| 766 | 766 |
In order to compile them, use the <tt>make demo</tt> or the |
| 767 | 767 |
<tt>make check</tt> commands. |
| 768 | 768 |
*/ |
| 769 | 769 |
|
| 770 | 770 |
} |
| 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); }
|
| 198 | 198 |
|
| 199 | 199 |
typedef NodeNumTagIndicator<Graph> NodeNumTag; |
| 200 | 200 |
int nodeNum() const { return _graph->nodeNum(); }
|
| 201 | 201 |
|
| 202 | 202 |
typedef ArcNumTagIndicator<Graph> ArcNumTag; |
| 203 | 203 |
int arcNum() const { return _graph->arcNum(); }
|
| 204 | 204 |
|
| 205 | 205 |
typedef EdgeNumTagIndicator<Graph> EdgeNumTag; |
| 206 | 206 |
int edgeNum() const { return _graph->edgeNum(); }
|
| 207 | 207 |
|
| 208 | 208 |
typedef FindArcTagIndicator<Graph> FindArcTag; |
| 209 | 209 |
Arc findArc(const Node& u, const Node& v, |
| 210 | 210 |
const Arc& prev = INVALID) const {
|
| 211 | 211 |
return _graph->findArc(u, v, prev); |
| 212 | 212 |
} |
| 213 | 213 |
|
| 214 | 214 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
| 215 | 215 |
Edge findEdge(const Node& u, const Node& v, |
| 216 | 216 |
const Edge& prev = INVALID) const {
|
| 217 | 217 |
return _graph->findEdge(u, v, prev); |
| 218 | 218 |
} |
| 219 | 219 |
|
| 220 | 220 |
Node addNode() { return _graph->addNode(); }
|
| 221 | 221 |
Edge addEdge(const Node& u, const Node& v) { return _graph->addEdge(u, v); }
|
| 222 | 222 |
|
| 223 | 223 |
void erase(const Node& i) { _graph->erase(i); }
|
| 224 | 224 |
void erase(const Edge& i) { _graph->erase(i); }
|
| 225 | 225 |
|
| 226 | 226 |
void clear() { _graph->clear(); }
|
| 227 | 227 |
|
| 228 | 228 |
bool direction(const Arc& a) const { return _graph->direction(a); }
|
| 229 | 229 |
Arc direct(const Edge& e, bool d) const { return _graph->direct(e, d); }
|
| 230 | 230 |
|
| 231 | 231 |
int id(const Node& v) const { return _graph->id(v); }
|
| 232 | 232 |
int id(const Arc& a) const { return _graph->id(a); }
|
| 233 | 233 |
int id(const Edge& e) const { return _graph->id(e); }
|
| 234 | 234 |
|
| 235 | 235 |
Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); }
|
| 236 | 236 |
Arc arcFromId(int ix) const { return _graph->arcFromId(ix); }
|
| 237 | 237 |
Edge edgeFromId(int ix) const { return _graph->edgeFromId(ix); }
|
| 238 | 238 |
|
| 239 | 239 |
int maxNodeId() const { return _graph->maxNodeId(); }
|
| 240 | 240 |
int maxArcId() const { return _graph->maxArcId(); }
|
| 241 | 241 |
int maxEdgeId() const { return _graph->maxEdgeId(); }
|
| 242 | 242 |
|
| 243 | 243 |
typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier; |
| 244 | 244 |
NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); }
|
| 245 | 245 |
|
| 246 | 246 |
typedef typename ItemSetTraits<GR, Arc>::ItemNotifier ArcNotifier; |
| 247 | 247 |
ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); }
|
| 248 | 248 |
|
| 249 | 249 |
typedef typename ItemSetTraits<GR, Edge>::ItemNotifier EdgeNotifier; |
| 250 | 250 |
EdgeNotifier& notifier(Edge) const { return _graph->notifier(Edge()); }
|
| 251 | 251 |
|
| 252 | 252 |
template <typename V> |
| 253 | 253 |
class NodeMap : public GR::template NodeMap<V> {
|
| 254 | 254 |
typedef typename GR::template NodeMap<V> Parent; |
| 255 | 255 |
|
| 256 | 256 |
public: |
| 257 | 257 |
explicit NodeMap(const GraphAdaptorBase<GR>& adapter) |
| 258 | 258 |
: Parent(*adapter._graph) {}
|
| 259 | 259 |
NodeMap(const GraphAdaptorBase<GR>& adapter, const V& value) |
| 260 | 260 |
: Parent(*adapter._graph, value) {}
|
| 261 | 261 |
|
| 262 | 262 |
private: |
| 263 | 263 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 264 | 264 |
return operator=<NodeMap>(cmap); |
| 265 | 265 |
} |
| 266 | 266 |
|
| 267 | 267 |
template <typename CMap> |
| 268 | 268 |
NodeMap& operator=(const CMap& cmap) {
|
| 269 | 269 |
Parent::operator=(cmap); |
| 270 | 270 |
return *this; |
| 271 | 271 |
} |
| 272 | 272 |
|
| 273 | 273 |
}; |
| 274 | 274 |
|
| 275 | 275 |
template <typename V> |
| 276 | 276 |
class ArcMap : public GR::template ArcMap<V> {
|
| 277 | 277 |
typedef typename GR::template ArcMap<V> Parent; |
| 278 | 278 |
|
| 279 | 279 |
public: |
| 280 | 280 |
explicit ArcMap(const GraphAdaptorBase<GR>& adapter) |
| 281 | 281 |
: Parent(*adapter._graph) {}
|
| 282 | 282 |
ArcMap(const GraphAdaptorBase<GR>& adapter, const V& value) |
| 283 | 283 |
: Parent(*adapter._graph, value) {}
|
| 284 | 284 |
|
| 285 | 285 |
private: |
| 286 | 286 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 287 | 287 |
return operator=<ArcMap>(cmap); |
| 288 | 288 |
} |
| 289 | 289 |
|
| 290 | 290 |
template <typename CMap> |
| 291 | 291 |
ArcMap& operator=(const CMap& cmap) {
|
| 292 | 292 |
Parent::operator=(cmap); |
| 293 | 293 |
return *this; |
| 294 | 294 |
} |
| 295 | 295 |
}; |
| 296 | 296 |
|
| 297 | 297 |
template <typename V> |
| 298 | 298 |
class EdgeMap : public GR::template EdgeMap<V> {
|
| 299 | 299 |
typedef typename GR::template EdgeMap<V> Parent; |
| 300 | 300 |
|
| 301 | 301 |
public: |
| 302 | 302 |
explicit EdgeMap(const GraphAdaptorBase<GR>& adapter) |
| 303 | 303 |
: Parent(*adapter._graph) {}
|
| 304 | 304 |
EdgeMap(const GraphAdaptorBase<GR>& adapter, const V& value) |
| 305 | 305 |
: Parent(*adapter._graph, value) {}
|
| 306 | 306 |
|
| 307 | 307 |
private: |
| 308 | 308 |
EdgeMap& operator=(const EdgeMap& cmap) {
|
| 309 | 309 |
return operator=<EdgeMap>(cmap); |
| 310 | 310 |
} |
| 311 | 311 |
|
| 312 | 312 |
template <typename CMap> |
| 313 | 313 |
EdgeMap& operator=(const CMap& cmap) {
|
| 314 | 314 |
Parent::operator=(cmap); |
| 315 | 315 |
return *this; |
| 316 | 316 |
} |
| 317 | 317 |
}; |
| 318 | 318 |
|
| 319 | 319 |
}; |
| 320 | 320 |
|
| 321 | 321 |
template <typename DGR> |
| 322 | 322 |
class ReverseDigraphBase : public DigraphAdaptorBase<DGR> {
|
| 323 | 323 |
typedef DigraphAdaptorBase<DGR> Parent; |
| 324 | 324 |
public: |
| 325 | 325 |
typedef DGR Digraph; |
| 326 | 326 |
protected: |
| 327 | 327 |
ReverseDigraphBase() : Parent() { }
|
| 328 | 328 |
public: |
| 329 | 329 |
typedef typename Parent::Node Node; |
| 330 | 330 |
typedef typename Parent::Arc Arc; |
| 331 | 331 |
|
| 332 | 332 |
void firstIn(Arc& a, const Node& n) const { Parent::firstOut(a, n); }
|
| 333 | 333 |
void firstOut(Arc& a, const Node& n ) const { Parent::firstIn(a, n); }
|
| 334 | 334 |
|
| 335 | 335 |
void nextIn(Arc& a) const { Parent::nextOut(a); }
|
| 336 | 336 |
void nextOut(Arc& a) const { Parent::nextIn(a); }
|
| 337 | 337 |
|
| 338 | 338 |
Node source(const Arc& a) const { return Parent::target(a); }
|
| 339 | 339 |
Node target(const Arc& a) const { return Parent::source(a); }
|
| 340 | 340 |
|
| 341 | 341 |
Arc addArc(const Node& u, const Node& v) { return Parent::addArc(v, u); }
|
| 342 | 342 |
|
| 343 | 343 |
typedef FindArcTagIndicator<DGR> FindArcTag; |
| 344 | 344 |
Arc findArc(const Node& u, const Node& v, |
| 345 | 345 |
const Arc& prev = INVALID) const {
|
| 346 | 346 |
return Parent::findArc(v, u, prev); |
| 347 | 347 |
} |
| 348 | 348 |
|
| 349 | 349 |
}; |
| 350 | 350 |
|
| 351 | 351 |
/// \ingroup graph_adaptors |
| 352 | 352 |
/// |
| 353 | 353 |
/// \brief Adaptor class for reversing the orientation of the arcs in |
| 354 | 354 |
/// a digraph. |
| 355 | 355 |
/// |
| 356 | 356 |
/// ReverseDigraph can be used for reversing the arcs in a digraph. |
| 357 | 357 |
/// It conforms to the \ref concepts::Digraph "Digraph" concept. |
| 358 | 358 |
/// |
| 359 | 359 |
/// The adapted digraph can also be modified through this adaptor |
| 360 | 360 |
/// by adding or removing nodes or arcs, unless the \c GR template |
| 361 | 361 |
/// parameter is set to be \c const. |
| 362 | 362 |
/// |
| 363 | 363 |
/// This class provides item counting in the same time as the adapted |
| 364 | 364 |
/// digraph structure. |
| 365 | 365 |
/// |
| 366 | 366 |
/// \tparam DGR The type of the adapted digraph. |
| 367 | 367 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
| 368 | 368 |
/// It can also be specified to be \c const. |
| 369 | 369 |
/// |
| 370 | 370 |
/// \note The \c Node and \c Arc types of this adaptor and the adapted |
| 371 | 371 |
/// digraph are convertible to each other. |
| 372 | 372 |
template<typename DGR> |
| 373 | 373 |
#ifdef DOXYGEN |
| 374 | 374 |
class ReverseDigraph {
|
| 375 | 375 |
#else |
| 376 | 376 |
class ReverseDigraph : |
| 377 | 377 |
public DigraphAdaptorExtender<ReverseDigraphBase<DGR> > {
|
| 378 | 378 |
#endif |
| 379 | 379 |
typedef DigraphAdaptorExtender<ReverseDigraphBase<DGR> > Parent; |
| 380 | 380 |
public: |
| 381 | 381 |
/// The type of the adapted digraph. |
| 382 | 382 |
typedef DGR Digraph; |
| 383 | 383 |
protected: |
| 384 | 384 |
ReverseDigraph() { }
|
| 385 | 385 |
public: |
| 386 | 386 |
|
| 387 | 387 |
/// \brief Constructor |
| 388 | 388 |
/// |
| 389 | 389 |
/// Creates a reverse digraph adaptor for the given digraph. |
| 390 | 390 |
explicit ReverseDigraph(DGR& digraph) {
|
| 391 | 391 |
Parent::initialize(digraph); |
| 392 | 392 |
} |
| 393 | 393 |
}; |
| 394 | 394 |
|
| 395 | 395 |
/// \brief Returns a read-only ReverseDigraph adaptor |
| 396 | 396 |
/// |
| 397 | 397 |
/// This function just returns a read-only \ref ReverseDigraph adaptor. |
| 398 | 398 |
/// \ingroup graph_adaptors |
| 399 | 399 |
/// \relates ReverseDigraph |
| 400 | 400 |
template<typename DGR> |
| 401 | 401 |
ReverseDigraph<const DGR> reverseDigraph(const DGR& digraph) {
|
| 402 | 402 |
return ReverseDigraph<const DGR>(digraph); |
| 403 | 403 |
} |
| 404 | 404 |
|
| 405 | 405 |
|
| 406 | 406 |
template <typename DGR, typename NF, typename AF, bool ch = true> |
| 407 | 407 |
class SubDigraphBase : public DigraphAdaptorBase<DGR> {
|
| 408 | 408 |
typedef DigraphAdaptorBase<DGR> Parent; |
| 409 | 409 |
public: |
| 410 | 410 |
typedef DGR Digraph; |
| 411 | 411 |
typedef NF NodeFilterMap; |
| 412 | 412 |
typedef AF ArcFilterMap; |
| 413 | 413 |
|
| 414 | 414 |
typedef SubDigraphBase Adaptor; |
| 415 | 415 |
protected: |
| 416 | 416 |
NF* _node_filter; |
| 417 | 417 |
AF* _arc_filter; |
| 418 | 418 |
SubDigraphBase() |
| 419 | 419 |
: Parent(), _node_filter(0), _arc_filter(0) { }
|
| 420 | 420 |
|
| 421 | 421 |
void initialize(DGR& digraph, NF& node_filter, AF& arc_filter) {
|
| 422 | 422 |
Parent::initialize(digraph); |
| 423 | 423 |
_node_filter = &node_filter; |
| 424 | 424 |
_arc_filter = &arc_filter; |
| 425 | 425 |
} |
| 426 | 426 |
|
| 427 | 427 |
public: |
| 428 | 428 |
|
| 429 | 429 |
typedef typename Parent::Node Node; |
| 430 | 430 |
typedef typename Parent::Arc Arc; |
| 431 | 431 |
|
| 432 | 432 |
void first(Node& i) const {
|
| 433 | 433 |
Parent::first(i); |
| 434 | 434 |
while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); |
| 435 | 435 |
} |
| 436 | 436 |
|
| 437 | 437 |
void first(Arc& i) const {
|
| 438 | 438 |
Parent::first(i); |
| 439 | 439 |
while (i != INVALID && (!(*_arc_filter)[i] |
| 440 | 440 |
|| !(*_node_filter)[Parent::source(i)] |
| 441 | 441 |
|| !(*_node_filter)[Parent::target(i)])) |
| 442 | 442 |
Parent::next(i); |
| 443 | 443 |
} |
| 444 | 444 |
|
| 445 | 445 |
void firstIn(Arc& i, const Node& n) const {
|
| 446 | 446 |
Parent::firstIn(i, n); |
| 447 | 447 |
while (i != INVALID && (!(*_arc_filter)[i] |
| 448 | 448 |
|| !(*_node_filter)[Parent::source(i)])) |
| 449 | 449 |
Parent::nextIn(i); |
| 450 | 450 |
} |
| 451 | 451 |
|
| 452 | 452 |
void firstOut(Arc& i, const Node& n) const {
|
| 453 | 453 |
Parent::firstOut(i, n); |
| 454 | 454 |
while (i != INVALID && (!(*_arc_filter)[i] |
| 455 | 455 |
|| !(*_node_filter)[Parent::target(i)])) |
| 456 | 456 |
Parent::nextOut(i); |
| 457 | 457 |
} |
| 458 | 458 |
|
| 459 | 459 |
void next(Node& i) const {
|
| 460 | 460 |
Parent::next(i); |
| 461 | 461 |
while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); |
| 462 | 462 |
} |
| 463 | 463 |
|
| 464 | 464 |
void next(Arc& i) const {
|
| 465 | 465 |
Parent::next(i); |
| 466 | 466 |
while (i != INVALID && (!(*_arc_filter)[i] |
| 467 | 467 |
|| !(*_node_filter)[Parent::source(i)] |
| 468 | 468 |
|| !(*_node_filter)[Parent::target(i)])) |
| 469 | 469 |
Parent::next(i); |
| 470 | 470 |
} |
| 471 | 471 |
|
| 472 | 472 |
void nextIn(Arc& i) const {
|
| 473 | 473 |
Parent::nextIn(i); |
| 474 | 474 |
while (i != INVALID && (!(*_arc_filter)[i] |
| 475 | 475 |
|| !(*_node_filter)[Parent::source(i)])) |
| 476 | 476 |
Parent::nextIn(i); |
| 477 | 477 |
} |
| 478 | 478 |
|
| 479 | 479 |
void nextOut(Arc& i) const {
|
| 480 | 480 |
Parent::nextOut(i); |
| 481 | 481 |
while (i != INVALID && (!(*_arc_filter)[i] |
| 482 | 482 |
|| !(*_node_filter)[Parent::target(i)])) |
| 483 | 483 |
Parent::nextOut(i); |
| 484 | 484 |
} |
| 485 | 485 |
|
| 486 | 486 |
void status(const Node& n, bool v) const { _node_filter->set(n, v); }
|
| 487 | 487 |
void status(const Arc& a, bool v) const { _arc_filter->set(a, v); }
|
| 488 | 488 |
|
| 489 | 489 |
bool status(const Node& n) const { return (*_node_filter)[n]; }
|
| 490 | 490 |
bool status(const Arc& a) const { return (*_arc_filter)[a]; }
|
| 491 | 491 |
|
| 492 | 492 |
typedef False NodeNumTag; |
| 493 | 493 |
typedef False ArcNumTag; |
| 494 | 494 |
|
| 495 | 495 |
typedef FindArcTagIndicator<DGR> FindArcTag; |
| 496 | 496 |
Arc findArc(const Node& source, const Node& target, |
| 497 | 497 |
const Arc& prev = INVALID) const {
|
| 498 | 498 |
if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
|
| 499 | 499 |
return INVALID; |
| 500 | 500 |
} |
| 501 | 501 |
Arc arc = Parent::findArc(source, target, prev); |
| 502 | 502 |
while (arc != INVALID && !(*_arc_filter)[arc]) {
|
| 503 | 503 |
arc = Parent::findArc(source, target, arc); |
| 504 | 504 |
} |
| 505 | 505 |
return arc; |
| 506 | 506 |
} |
| 507 | 507 |
|
| 508 | 508 |
public: |
| 509 | 509 |
|
| 510 | 510 |
template <typename V> |
| 511 | 511 |
class NodeMap |
| 512 | 512 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
| 513 | 513 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> {
|
| 514 | 514 |
typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
| 515 | 515 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> Parent; |
| 516 | 516 |
|
| 517 | 517 |
public: |
| 518 | 518 |
typedef V Value; |
| 519 | 519 |
|
| 520 | 520 |
NodeMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor) |
| 521 | 521 |
: Parent(adaptor) {}
|
| 522 | 522 |
NodeMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor, const V& value) |
| 523 | 523 |
: Parent(adaptor, value) {}
|
| 524 | 524 |
|
| 525 | 525 |
private: |
| 526 | 526 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 527 | 527 |
return operator=<NodeMap>(cmap); |
| 528 | 528 |
} |
| 529 | 529 |
|
| 530 | 530 |
template <typename CMap> |
| 531 | 531 |
NodeMap& operator=(const CMap& cmap) {
|
| 532 | 532 |
Parent::operator=(cmap); |
| 533 | 533 |
return *this; |
| 534 | 534 |
} |
| 535 | 535 |
}; |
| 536 | 536 |
|
| 537 | 537 |
template <typename V> |
| 538 | 538 |
class ArcMap |
| 539 | 539 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
| 540 | 540 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> {
|
| 541 | 541 |
typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
| 542 | 542 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent; |
| 543 | 543 |
|
| 544 | 544 |
public: |
| 545 | 545 |
typedef V Value; |
| 546 | 546 |
|
| 547 | 547 |
ArcMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor) |
| 548 | 548 |
: Parent(adaptor) {}
|
| 549 | 549 |
ArcMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor, const V& value) |
| 550 | 550 |
: Parent(adaptor, value) {}
|
| 551 | 551 |
|
| 552 | 552 |
private: |
| 553 | 553 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 554 | 554 |
return operator=<ArcMap>(cmap); |
| 555 | 555 |
} |
| 556 | 556 |
|
| 557 | 557 |
template <typename CMap> |
| 558 | 558 |
ArcMap& operator=(const CMap& cmap) {
|
| 559 | 559 |
Parent::operator=(cmap); |
| 560 | 560 |
return *this; |
| 561 | 561 |
} |
| 562 | 562 |
}; |
| 563 | 563 |
|
| 564 | 564 |
}; |
| 565 | 565 |
|
| 566 | 566 |
template <typename DGR, typename NF, typename AF> |
| 567 | 567 |
class SubDigraphBase<DGR, NF, AF, false> |
| 568 | 568 |
: public DigraphAdaptorBase<DGR> {
|
| 569 | 569 |
typedef DigraphAdaptorBase<DGR> Parent; |
| 570 | 570 |
public: |
| 571 | 571 |
typedef DGR Digraph; |
| 572 | 572 |
typedef NF NodeFilterMap; |
| 573 | 573 |
typedef AF ArcFilterMap; |
| 574 | 574 |
|
| 575 | 575 |
typedef SubDigraphBase Adaptor; |
| 576 | 576 |
protected: |
| 577 | 577 |
NF* _node_filter; |
| 578 | 578 |
AF* _arc_filter; |
| 579 | 579 |
SubDigraphBase() |
| 580 | 580 |
: Parent(), _node_filter(0), _arc_filter(0) { }
|
| 581 | 581 |
|
| 582 | 582 |
void initialize(DGR& digraph, NF& node_filter, AF& arc_filter) {
|
| 583 | 583 |
Parent::initialize(digraph); |
| 584 | 584 |
_node_filter = &node_filter; |
| 585 | 585 |
_arc_filter = &arc_filter; |
| 586 | 586 |
} |
| 587 | 587 |
|
| 588 | 588 |
public: |
| 589 | 589 |
|
| 590 | 590 |
typedef typename Parent::Node Node; |
| 591 | 591 |
typedef typename Parent::Arc Arc; |
| 592 | 592 |
|
| 593 | 593 |
void first(Node& i) const {
|
| 594 | 594 |
Parent::first(i); |
| 595 | 595 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
| 596 | 596 |
} |
| 597 | 597 |
|
| 598 | 598 |
void first(Arc& i) const {
|
| 599 | 599 |
Parent::first(i); |
| 600 | 600 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); |
| 601 | 601 |
} |
| 602 | 602 |
|
| 603 | 603 |
void firstIn(Arc& i, const Node& n) const {
|
| 604 | 604 |
Parent::firstIn(i, n); |
| 605 | 605 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); |
| 606 | 606 |
} |
| 607 | 607 |
|
| 608 | 608 |
void firstOut(Arc& i, const Node& n) const {
|
| 609 | 609 |
Parent::firstOut(i, n); |
| 610 | 610 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); |
| 611 | 611 |
} |
| 612 | 612 |
|
| 613 | 613 |
void next(Node& i) const {
|
| 614 | 614 |
Parent::next(i); |
| 615 | 615 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
| 616 | 616 |
} |
| 617 | 617 |
void next(Arc& i) const {
|
| 618 | 618 |
Parent::next(i); |
| 619 | 619 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); |
| 620 | 620 |
} |
| 621 | 621 |
void nextIn(Arc& i) const {
|
| 622 | 622 |
Parent::nextIn(i); |
| 623 | 623 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); |
| 624 | 624 |
} |
| 625 | 625 |
|
| 626 | 626 |
void nextOut(Arc& i) const {
|
| 627 | 627 |
Parent::nextOut(i); |
| 628 | 628 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); |
| 629 | 629 |
} |
| 630 | 630 |
|
| 631 | 631 |
void status(const Node& n, bool v) const { _node_filter->set(n, v); }
|
| 632 | 632 |
void status(const Arc& a, bool v) const { _arc_filter->set(a, v); }
|
| 633 | 633 |
|
| 634 | 634 |
bool status(const Node& n) const { return (*_node_filter)[n]; }
|
| 635 | 635 |
bool status(const Arc& a) const { return (*_arc_filter)[a]; }
|
| 636 | 636 |
|
| 637 | 637 |
typedef False NodeNumTag; |
| 638 | 638 |
typedef False ArcNumTag; |
| 639 | 639 |
|
| 640 | 640 |
typedef FindArcTagIndicator<DGR> FindArcTag; |
| 641 | 641 |
Arc findArc(const Node& source, const Node& target, |
| 642 | 642 |
const Arc& prev = INVALID) const {
|
| 643 | 643 |
if (!(*_node_filter)[source] || !(*_node_filter)[target]) {
|
| 644 | 644 |
return INVALID; |
| 645 | 645 |
} |
| 646 | 646 |
Arc arc = Parent::findArc(source, target, prev); |
| 647 | 647 |
while (arc != INVALID && !(*_arc_filter)[arc]) {
|
| 648 | 648 |
arc = Parent::findArc(source, target, arc); |
| 649 | 649 |
} |
| 650 | 650 |
return arc; |
| 651 | 651 |
} |
| 652 | 652 |
|
| 653 | 653 |
template <typename V> |
| 654 | 654 |
class NodeMap |
| 655 | 655 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
| 656 | 656 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> {
|
| 657 | 657 |
typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
| 658 | 658 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> Parent; |
| 659 | 659 |
|
| 660 | 660 |
public: |
| 661 | 661 |
typedef V Value; |
| 662 | 662 |
|
| 663 | 663 |
NodeMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor) |
| 664 | 664 |
: Parent(adaptor) {}
|
| 665 | 665 |
NodeMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor, const V& value) |
| 666 | 666 |
: Parent(adaptor, value) {}
|
| 667 | 667 |
|
| 668 | 668 |
private: |
| 669 | 669 |
NodeMap& operator=(const NodeMap& cmap) {
|
| 670 | 670 |
return operator=<NodeMap>(cmap); |
| 671 | 671 |
} |
| 672 | 672 |
|
| 673 | 673 |
template <typename CMap> |
| 674 | 674 |
NodeMap& operator=(const CMap& cmap) {
|
| 675 | 675 |
Parent::operator=(cmap); |
| 676 | 676 |
return *this; |
| 677 | 677 |
} |
| 678 | 678 |
}; |
| 679 | 679 |
|
| 680 | 680 |
template <typename V> |
| 681 | 681 |
class ArcMap |
| 682 | 682 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
| 683 | 683 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> {
|
| 684 | 684 |
typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
| 685 | 685 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent; |
| 686 | 686 |
|
| 687 | 687 |
public: |
| 688 | 688 |
typedef V Value; |
| 689 | 689 |
|
| 690 | 690 |
ArcMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor) |
| 691 | 691 |
: Parent(adaptor) {}
|
| 692 | 692 |
ArcMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor, const V& value) |
| 693 | 693 |
: Parent(adaptor, value) {}
|
| 694 | 694 |
|
| 695 | 695 |
private: |
| 696 | 696 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 697 | 697 |
return operator=<ArcMap>(cmap); |
| 698 | 698 |
} |
| 699 | 699 |
|
| 700 | 700 |
template <typename CMap> |
| 701 | 701 |
ArcMap& operator=(const CMap& cmap) {
|
| 702 | 702 |
Parent::operator=(cmap); |
| 703 | 703 |
return *this; |
| 704 | 704 |
} |
| 705 | 705 |
}; |
| 706 | 706 |
|
| 707 | 707 |
}; |
| 708 | 708 |
|
| 709 | 709 |
/// \ingroup graph_adaptors |
| 710 | 710 |
/// |
| 711 | 711 |
/// \brief Adaptor class for hiding nodes and arcs in a digraph |
| 712 | 712 |
/// |
| 713 | 713 |
/// SubDigraph can be used for hiding nodes and arcs in a digraph. |
| 714 | 714 |
/// A \c bool node map and a \c bool arc map must be specified, which |
| 715 | 715 |
/// define the filters for nodes and arcs. |
| 716 | 716 |
/// Only the nodes and arcs with \c true filter value are |
| 717 | 717 |
/// shown in the subdigraph. The arcs that are incident to hidden |
| 718 | 718 |
/// nodes are also filtered out. |
| 719 | 719 |
/// This adaptor conforms to the \ref concepts::Digraph "Digraph" concept. |
| 720 | 720 |
/// |
| 721 | 721 |
/// The adapted digraph can also be modified through this adaptor |
| 722 | 722 |
/// by adding or removing nodes or arcs, unless the \c GR template |
| 723 | 723 |
/// parameter is set to be \c const. |
| 724 | 724 |
/// |
| 725 | 725 |
/// This class provides only linear time counting for nodes and arcs. |
| 726 | 726 |
/// |
| 727 | 727 |
/// \tparam DGR The type of the adapted digraph. |
| 728 | 728 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
| 729 | 729 |
/// It can also be specified to be \c const. |
| 730 | 730 |
/// \tparam NF The type of the node filter map. |
| 731 | 731 |
/// It must be a \c bool (or convertible) node map of the |
| 732 | 732 |
/// adapted digraph. The default type is |
| 733 | 733 |
/// \ref concepts::Digraph::NodeMap "DGR::NodeMap<bool>". |
| 734 | 734 |
/// \tparam AF The type of the arc filter map. |
| 735 | 735 |
/// It must be \c bool (or convertible) arc map of the |
| 736 | 736 |
/// adapted digraph. The default type is |
| 737 | 737 |
/// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>". |
| 738 | 738 |
/// |
| 739 | 739 |
/// \note The \c Node and \c Arc types of this adaptor and the adapted |
| 740 | 740 |
/// digraph are convertible to each other. |
| 741 | 741 |
/// |
| 742 | 742 |
/// \see FilterNodes |
| 743 | 743 |
/// \see FilterArcs |
| 744 | 744 |
#ifdef DOXYGEN |
| 745 | 745 |
template<typename DGR, typename NF, typename AF> |
| 746 | 746 |
class SubDigraph {
|
| 747 | 747 |
#else |
| 748 | 748 |
template<typename DGR, |
| 749 | 749 |
typename NF = typename DGR::template NodeMap<bool>, |
| 750 | 750 |
typename AF = typename DGR::template ArcMap<bool> > |
| 751 | 751 |
class SubDigraph : |
| 752 | 752 |
public DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> > {
|
| 753 | 753 |
#endif |
| 754 | 754 |
public: |
| 755 | 755 |
/// The type of the adapted digraph. |
| 756 | 756 |
typedef DGR Digraph; |
| 757 | 757 |
/// The type of the node filter map. |
| 758 | 758 |
typedef NF NodeFilterMap; |
| 759 | 759 |
/// The type of the arc filter map. |
| 760 | 760 |
typedef AF ArcFilterMap; |
| 761 | 761 |
|
| 762 | 762 |
typedef DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> > |
| 763 | 763 |
Parent; |
| 764 | 764 |
|
| 765 | 765 |
typedef typename Parent::Node Node; |
| 766 | 766 |
typedef typename Parent::Arc Arc; |
| 767 | 767 |
|
| 768 | 768 |
protected: |
| 769 | 769 |
SubDigraph() { }
|
| 770 | 770 |
public: |
| 771 | 771 |
|
| 772 | 772 |
/// \brief Constructor |
| 773 | 773 |
/// |
| 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; |
| 198 | 198 |
} |
| 199 | 199 |
|
| 200 | 200 |
ArgParser &ArgParser::optionGroup(const std::string &group, |
| 201 | 201 |
const std::string &opt) |
| 202 | 202 |
{
|
| 203 | 203 |
Opts::iterator i = _opts.find(opt); |
| 204 | 204 |
LEMON_ASSERT(i!=_opts.end(), "Unknown option: '"+opt+"'"); |
| 205 | 205 |
LEMON_ASSERT(!(i->second.ingroup), |
| 206 | 206 |
"Option already in option group: '"+opt+"'"); |
| 207 | 207 |
GroupData &g=_groups[group]; |
| 208 | 208 |
g.opts.push_back(opt); |
| 209 | 209 |
i->second.ingroup=true; |
| 210 | 210 |
return *this; |
| 211 | 211 |
} |
| 212 | 212 |
|
| 213 | 213 |
ArgParser &ArgParser::onlyOneGroup(const std::string &group) |
| 214 | 214 |
{
|
| 215 | 215 |
GroupData &g=_groups[group]; |
| 216 | 216 |
g.only_one=true; |
| 217 | 217 |
return *this; |
| 218 | 218 |
} |
| 219 | 219 |
|
| 220 | 220 |
ArgParser &ArgParser::synonym(const std::string &syn, |
| 221 | 221 |
const std::string &opt) |
| 222 | 222 |
{
|
| 223 | 223 |
Opts::iterator o = _opts.find(opt); |
| 224 | 224 |
Opts::iterator s = _opts.find(syn); |
| 225 | 225 |
LEMON_ASSERT(o!=_opts.end(), "Unknown option: '"+opt+"'"); |
| 226 | 226 |
LEMON_ASSERT(s==_opts.end(), "Option already used: '"+syn+"'"); |
| 227 | 227 |
ParData p; |
| 228 | 228 |
p.help=opt; |
| 229 | 229 |
p.mandatory=false; |
| 230 | 230 |
p.syn=true; |
| 231 | 231 |
_opts[syn]=p; |
| 232 | 232 |
o->second.has_syn=true; |
| 233 | 233 |
return *this; |
| 234 | 234 |
} |
| 235 | 235 |
|
| 236 | 236 |
ArgParser &ArgParser::mandatoryGroup(const std::string &group) |
| 237 | 237 |
{
|
| 238 | 238 |
GroupData &g=_groups[group]; |
| 239 | 239 |
g.mandatory=true; |
| 240 | 240 |
return *this; |
| 241 | 241 |
} |
| 242 | 242 |
|
| 243 | 243 |
ArgParser &ArgParser::other(const std::string &name, |
| 244 | 244 |
const std::string &help) |
| 245 | 245 |
{
|
| 246 | 246 |
_others_help.push_back(OtherArg(name,help)); |
| 247 | 247 |
return *this; |
| 248 | 248 |
} |
| 249 | 249 |
|
| 250 | 250 |
void ArgParser::show(std::ostream &os,Opts::const_iterator i) const |
| 251 | 251 |
{
|
| 252 | 252 |
os << "-" << i->first; |
| 253 | 253 |
if(i->second.has_syn) |
| 254 | 254 |
for(Opts::const_iterator j=_opts.begin();j!=_opts.end();++j) |
| 255 | 255 |
if(j->second.syn&&j->second.help==i->first) |
| 256 | 256 |
os << "|-" << j->first; |
| 257 | 257 |
switch(i->second.type) {
|
| 258 | 258 |
case STRING: |
| 259 | 259 |
os << " str"; |
| 260 | 260 |
break; |
| 261 | 261 |
case INTEGER: |
| 262 | 262 |
os << " int"; |
| 263 | 263 |
break; |
| 264 | 264 |
case DOUBLE: |
| 265 | 265 |
os << " num"; |
| 266 | 266 |
break; |
| 267 | 267 |
default: |
| 268 | 268 |
break; |
| 269 | 269 |
} |
| 270 | 270 |
} |
| 271 | 271 |
|
| 272 | 272 |
void ArgParser::show(std::ostream &os,Groups::const_iterator i) const |
| 273 | 273 |
{
|
| 274 | 274 |
GroupData::Opts::const_iterator o=i->second.opts.begin(); |
| 275 | 275 |
while(o!=i->second.opts.end()) {
|
| 276 | 276 |
show(os,_opts.find(*o)); |
| 277 | 277 |
++o; |
| 278 | 278 |
if(o!=i->second.opts.end()) os<<'|'; |
| 279 | 279 |
} |
| 280 | 280 |
} |
| 281 | 281 |
|
| 282 | 282 |
void ArgParser::showHelp(Opts::const_iterator i) const |
| 283 | 283 |
{
|
| 284 | 284 |
if(i->second.help.size()==0||i->second.syn) return; |
| 285 | 285 |
std::cerr << " "; |
| 286 | 286 |
show(std::cerr,i); |
| 287 | 287 |
std::cerr << std::endl; |
| 288 | 288 |
std::cerr << " " << i->second.help << std::endl; |
| 289 | 289 |
} |
| 290 | 290 |
void ArgParser::showHelp(std::vector<ArgParser::OtherArg>::const_iterator i) |
| 291 | 291 |
const |
| 292 | 292 |
{
|
| 293 | 293 |
if(i->help.size()==0) return; |
| 294 | 294 |
std::cerr << " " << i->name << std::endl |
| 295 | 295 |
<< " " << i->help << std::endl; |
| 296 | 296 |
} |
| 297 | 297 |
|
| 298 | 298 |
void ArgParser::shortHelp() const |
| 299 | 299 |
{
|
| 300 | 300 |
const unsigned int LINE_LEN=77; |
| 301 | 301 |
const std::string indent(" ");
|
| 302 | 302 |
std::cerr << "Usage:\n " << _command_name; |
| 303 | 303 |
int pos=_command_name.size()+2; |
| 304 | 304 |
for(Groups::const_iterator g=_groups.begin();g!=_groups.end();++g) {
|
| 305 | 305 |
std::ostringstream cstr; |
| 306 | 306 |
cstr << ' '; |
| 307 | 307 |
if(!g->second.mandatory) cstr << '['; |
| 308 | 308 |
show(cstr,g); |
| 309 | 309 |
if(!g->second.mandatory) cstr << ']'; |
| 310 | 310 |
if(pos+cstr.str().size()>LINE_LEN) {
|
| 311 | 311 |
std::cerr << std::endl << indent; |
| 312 | 312 |
pos=indent.size(); |
| 313 | 313 |
} |
| 314 | 314 |
std::cerr << cstr.str(); |
| 315 | 315 |
pos+=cstr.str().size(); |
| 316 | 316 |
} |
| 317 | 317 |
for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) |
| 318 | 318 |
if(!i->second.ingroup&&!i->second.syn) {
|
| 319 | 319 |
std::ostringstream cstr; |
| 320 | 320 |
cstr << ' '; |
| 321 | 321 |
if(!i->second.mandatory) cstr << '['; |
| 322 | 322 |
show(cstr,i); |
| 323 | 323 |
if(!i->second.mandatory) cstr << ']'; |
| 324 | 324 |
if(pos+cstr.str().size()>LINE_LEN) {
|
| 325 | 325 |
std::cerr << std::endl << indent; |
| 326 | 326 |
pos=indent.size(); |
| 327 | 327 |
} |
| 328 | 328 |
std::cerr << cstr.str(); |
| 329 | 329 |
pos+=cstr.str().size(); |
| 330 | 330 |
} |
| 331 | 331 |
for(std::vector<OtherArg>::const_iterator i=_others_help.begin(); |
| 332 | 332 |
i!=_others_help.end();++i) |
| 333 | 333 |
{
|
| 334 | 334 |
std::ostringstream cstr; |
| 335 | 335 |
cstr << ' ' << i->name; |
| 336 | 336 |
|
| 337 | 337 |
if(pos+cstr.str().size()>LINE_LEN) {
|
| 338 | 338 |
std::cerr << std::endl << indent; |
| 339 | 339 |
pos=indent.size(); |
| 340 | 340 |
} |
| 341 | 341 |
std::cerr << cstr.str(); |
| 342 | 342 |
pos+=cstr.str().size(); |
| 343 | 343 |
} |
| 344 | 344 |
std::cerr << std::endl; |
| 345 | 345 |
} |
| 346 | 346 |
|
| 347 | 347 |
void ArgParser::showHelp() const |
| 348 | 348 |
{
|
| 349 | 349 |
shortHelp(); |
| 350 | 350 |
std::cerr << "Where:\n"; |
| 351 | 351 |
for(std::vector<OtherArg>::const_iterator i=_others_help.begin(); |
| 352 | 352 |
i!=_others_help.end();++i) showHelp(i); |
| 353 | 353 |
for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) showHelp(i); |
| 354 | 354 |
_terminate(ArgParserException::HELP); |
| 355 | 355 |
} |
| 356 | 356 |
|
| 357 | 357 |
|
| 358 | 358 |
void ArgParser::unknownOpt(std::string arg) const |
| 359 | 359 |
{
|
| 360 | 360 |
std::cerr << "\nUnknown option: " << arg << "\n"; |
| 361 | 361 |
std::cerr << "\nType '" << _command_name << |
| 362 | 362 |
" --help' to obtain a short summary on the usage.\n\n"; |
| 363 | 363 |
_terminate(ArgParserException::UNKNOWN_OPT); |
| 364 | 364 |
} |
| 365 | 365 |
|
| 366 | 366 |
void ArgParser::requiresValue(std::string arg, OptType t) const |
| 367 | 367 |
{
|
| 368 | 368 |
std::cerr << "Argument '" << arg << "' requires a"; |
| 369 | 369 |
switch(t) {
|
| 370 | 370 |
case STRING: |
| 371 | 371 |
std::cerr << " string"; |
| 372 | 372 |
break; |
| 373 | 373 |
case INTEGER: |
| 374 | 374 |
std::cerr << "n integer"; |
| 375 | 375 |
break; |
| 376 | 376 |
case DOUBLE: |
| 377 | 377 |
std::cerr << " floating point"; |
| 378 | 378 |
break; |
| 379 | 379 |
default: |
| 380 | 380 |
break; |
| 381 | 381 |
} |
| 382 | 382 |
std::cerr << " value\n\n"; |
| 383 | 383 |
showHelp(); |
| 384 | 384 |
} |
| 385 | 385 |
|
| 386 | 386 |
|
| 387 | 387 |
void ArgParser::checkMandatories() const |
| 388 | 388 |
{
|
| 389 | 389 |
bool ok=true; |
| 390 | 390 |
for(Opts::const_iterator i=_opts.begin();i!=_opts.end();++i) |
| 391 | 391 |
if(i->second.mandatory&&!i->second.set) |
| 392 | 392 |
{
|
| 393 | 393 |
if(ok) |
| 394 | 394 |
std::cerr << _command_name |
| 395 | 395 |
<< ": The following mandatory arguments are missing.\n"; |
| 396 | 396 |
ok=false; |
| 397 | 397 |
showHelp(i); |
| 398 | 398 |
} |
| 399 | 399 |
for(Groups::const_iterator i=_groups.begin();i!=_groups.end();++i) |
| 400 | 400 |
if(i->second.mandatory||i->second.only_one) |
| 401 | 401 |
{
|
| 402 | 402 |
int set=0; |
| 403 | 403 |
for(GroupData::Opts::const_iterator o=i->second.opts.begin(); |
| 404 | 404 |
o!=i->second.opts.end();++o) |
| 405 | 405 |
if(_opts.find(*o)->second.set) ++set; |
| 406 | 406 |
if(i->second.mandatory&&!set) {
|
| 407 | 407 |
std::cerr << _command_name << |
| 408 | 408 |
": At least one of the following arguments is mandatory.\n"; |
| 409 | 409 |
ok=false; |
| 410 | 410 |
for(GroupData::Opts::const_iterator o=i->second.opts.begin(); |
| 411 | 411 |
o!=i->second.opts.end();++o) |
| 412 | 412 |
showHelp(_opts.find(*o)); |
| 413 | 413 |
} |
| 414 | 414 |
if(i->second.only_one&&set>1) {
|
| 415 | 415 |
std::cerr << _command_name << |
| 416 | 416 |
": At most one of the following arguments can be given.\n"; |
| 417 | 417 |
ok=false; |
| 418 | 418 |
for(GroupData::Opts::const_iterator o=i->second.opts.begin(); |
| 419 | 419 |
o!=i->second.opts.end();++o) |
| 420 | 420 |
showHelp(_opts.find(*o)); |
| 421 | 421 |
} |
| 422 | 422 |
} |
| 423 | 423 |
if(!ok) {
|
| 424 | 424 |
std::cerr << "\nType '" << _command_name << |
| 425 | 425 |
" --help' to obtain a short summary on the usage.\n\n"; |
| 426 | 426 |
_terminate(ArgParserException::INVALID_OPT); |
| 427 | 427 |
} |
| 428 | 428 |
} |
| 429 | 429 |
|
| 430 | 430 |
ArgParser &ArgParser::parse() |
| 431 | 431 |
{
|
| 432 | 432 |
for(int ar=1; ar<_argc; ++ar) {
|
| 433 | 433 |
std::string arg(_argv[ar]); |
| 434 | 434 |
if (arg[0] != '-' || arg.size() == 1) {
|
| 435 | 435 |
_file_args.push_back(arg); |
| 436 | 436 |
} |
| 437 | 437 |
else {
|
| 438 | 438 |
Opts::iterator i = _opts.find(arg.substr(1)); |
| 439 | 439 |
if(i==_opts.end()) unknownOpt(arg); |
| 440 | 440 |
else {
|
| 441 | 441 |
if(i->second.syn) i=_opts.find(i->second.help); |
| 442 | 442 |
ParData &p(i->second); |
| 443 | 443 |
if (p.type==BOOL) *p.bool_p=true; |
| 444 | 444 |
else if (p.type==FUNC) p.func_p.p(p.func_p.data); |
| 445 | 445 |
else if(++ar==_argc) requiresValue(arg, p.type); |
| 446 | 446 |
else {
|
| 447 | 447 |
std::string val(_argv[ar]); |
| 448 | 448 |
std::istringstream vals(val); |
| 449 | 449 |
switch(p.type) {
|
| 450 | 450 |
case STRING: |
| 451 | 451 |
*p.string_p=val; |
| 452 | 452 |
break; |
| 453 | 453 |
case INTEGER: |
| 454 | 454 |
vals >> *p.int_p; |
| 455 | 455 |
break; |
| 456 | 456 |
case DOUBLE: |
| 457 | 457 |
vals >> *p.double_p; |
| 458 | 458 |
break; |
| 459 | 459 |
default: |
| 460 | 460 |
break; |
| 461 | 461 |
} |
| 462 | 462 |
if(p.type!=STRING&&(!vals||!vals.eof())) |
| 463 | 463 |
requiresValue(arg, p.type); |
| 464 | 464 |
} |
| 465 | 465 |
p.set = true; |
| 466 | 466 |
} |
| 467 | 467 |
} |
| 468 | 468 |
} |
| 469 | 469 |
checkMandatories(); |
| 470 | 470 |
|
| 471 | 471 |
return *this; |
| 472 | 472 |
} |
| 473 | 473 |
|
| 474 | 474 |
} |
| 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. |
| 198 | 198 |
///\param name The name of the option. The leading '-' must be omitted. |
| 199 | 199 |
///\param help A help string. |
| 200 | 200 |
///\param value A default value for the option. |
| 201 | 201 |
///\param obl Indicate if the option is mandatory. |
| 202 | 202 |
///\note A mandatory bool obtion is of very little use. |
| 203 | 203 |
ArgParser &boolOption(const std::string &name, |
| 204 | 204 |
const std::string &help, |
| 205 | 205 |
bool value=false, bool obl=false); |
| 206 | 206 |
|
| 207 | 207 |
///Add a new string type option |
| 208 | 208 |
|
| 209 | 209 |
///Add a new string type option. |
| 210 | 210 |
///\param name The name of the option. The leading '-' must be omitted. |
| 211 | 211 |
///\param help A help string. |
| 212 | 212 |
///\param value A default value for the option. |
| 213 | 213 |
///\param obl Indicate if the option is mandatory. |
| 214 | 214 |
ArgParser &stringOption(const std::string &name, |
| 215 | 215 |
const std::string &help, |
| 216 | 216 |
std::string value="", bool obl=false); |
| 217 | 217 |
|
| 218 | 218 |
///Give help string for non-parsed arguments. |
| 219 | 219 |
|
| 220 | 220 |
///With this function you can give help string for non-parsed arguments. |
| 221 | 221 |
///The parameter \c name will be printed in the short usage line, while |
| 222 | 222 |
///\c help gives a more detailed description. |
| 223 | 223 |
ArgParser &other(const std::string &name, |
| 224 | 224 |
const std::string &help=""); |
| 225 | 225 |
|
| 226 | 226 |
///@} |
| 227 | 227 |
|
| 228 | 228 |
///\name Options with External Storage |
| 229 | 229 |
///Using this functions, the value of the option will be directly written |
| 230 | 230 |
///into a variable once the option appears in the command line. |
| 231 | 231 |
|
| 232 | 232 |
///@{
|
| 233 | 233 |
|
| 234 | 234 |
///Add a new integer type option with a storage reference |
| 235 | 235 |
|
| 236 | 236 |
///Add a new integer type option with a storage reference. |
| 237 | 237 |
///\param name The name of the option. The leading '-' must be omitted. |
| 238 | 238 |
///\param help A help string. |
| 239 | 239 |
///\param obl Indicate if the option is mandatory. |
| 240 | 240 |
///\retval ref The value of the argument will be written to this variable. |
| 241 | 241 |
ArgParser &refOption(const std::string &name, |
| 242 | 242 |
const std::string &help, |
| 243 | 243 |
int &ref, bool obl=false); |
| 244 | 244 |
|
| 245 | 245 |
///Add a new floating type option with a storage reference |
| 246 | 246 |
|
| 247 | 247 |
///Add a new floating type option with a storage reference. |
| 248 | 248 |
///\param name The name of the option. The leading '-' must be omitted. |
| 249 | 249 |
///\param help A help string. |
| 250 | 250 |
///\param obl Indicate if the option is mandatory. |
| 251 | 251 |
///\retval ref The value of the argument will be written to this variable. |
| 252 | 252 |
ArgParser &refOption(const std::string &name, |
| 253 | 253 |
const std::string &help, |
| 254 | 254 |
double &ref, bool obl=false); |
| 255 | 255 |
|
| 256 | 256 |
///Add a new bool type option with a storage reference |
| 257 | 257 |
|
| 258 | 258 |
///Add a new bool type option with a storage reference. |
| 259 | 259 |
///\param name The name of the option. The leading '-' must be omitted. |
| 260 | 260 |
///\param help A help string. |
| 261 | 261 |
///\param obl Indicate if the option is mandatory. |
| 262 | 262 |
///\retval ref The value of the argument will be written to this variable. |
| 263 | 263 |
///\note A mandatory bool obtion is of very little use. |
| 264 | 264 |
ArgParser &refOption(const std::string &name, |
| 265 | 265 |
const std::string &help, |
| 266 | 266 |
bool &ref, bool obl=false); |
| 267 | 267 |
|
| 268 | 268 |
///Add a new string type option with a storage reference |
| 269 | 269 |
|
| 270 | 270 |
///Add a new string type option with a storage reference. |
| 271 | 271 |
///\param name The name of the option. The leading '-' must be omitted. |
| 272 | 272 |
///\param help A help string. |
| 273 | 273 |
///\param obl Indicate if the option is mandatory. |
| 274 | 274 |
///\retval ref The value of the argument will be written to this variable. |
| 275 | 275 |
ArgParser &refOption(const std::string &name, |
| 276 | 276 |
const std::string &help, |
| 277 | 277 |
std::string &ref, bool obl=false); |
| 278 | 278 |
|
| 279 | 279 |
///@} |
| 280 | 280 |
|
| 281 | 281 |
///\name Option Groups and Synonyms |
| 282 | 282 |
/// |
| 283 | 283 |
|
| 284 | 284 |
///@{
|
| 285 | 285 |
|
| 286 | 286 |
///Bundle some options into a group |
| 287 | 287 |
|
| 288 | 288 |
/// You can group some option by calling this function repeatedly for each |
| 289 | 289 |
/// option to be grouped with the same groupname. |
| 290 | 290 |
///\param group The group name. |
| 291 | 291 |
///\param opt The option name. |
| 292 | 292 |
ArgParser &optionGroup(const std::string &group, |
| 293 | 293 |
const std::string &opt); |
| 294 | 294 |
|
| 295 | 295 |
///Make the members of a group exclusive |
| 296 | 296 |
|
| 297 | 297 |
///If you call this function for a group, than at most one of them can be |
| 298 | 298 |
///given at the same time. |
| 299 | 299 |
ArgParser &onlyOneGroup(const std::string &group); |
| 300 | 300 |
|
| 301 | 301 |
///Make a group mandatory |
| 302 | 302 |
|
| 303 | 303 |
///Using this function, at least one of the members of \c group |
| 304 | 304 |
///must be given. |
| 305 | 305 |
ArgParser &mandatoryGroup(const std::string &group); |
| 306 | 306 |
|
| 307 | 307 |
///Create synonym to an option |
| 308 | 308 |
|
| 309 | 309 |
///With this function you can create a synonym \c syn of the |
| 310 | 310 |
///option \c opt. |
| 311 | 311 |
ArgParser &synonym(const std::string &syn, |
| 312 | 312 |
const std::string &opt); |
| 313 | 313 |
|
| 314 | 314 |
///@} |
| 315 | 315 |
|
| 316 | 316 |
private: |
| 317 | 317 |
void show(std::ostream &os,Opts::const_iterator i) const; |
| 318 | 318 |
void show(std::ostream &os,Groups::const_iterator i) const; |
| 319 | 319 |
void showHelp(Opts::const_iterator i) const; |
| 320 | 320 |
void showHelp(std::vector<OtherArg>::const_iterator i) const; |
| 321 | 321 |
|
| 322 | 322 |
void unknownOpt(std::string arg) const; |
| 323 | 323 |
|
| 324 | 324 |
void requiresValue(std::string arg, OptType t) const; |
| 325 | 325 |
void checkMandatories() const; |
| 326 | 326 |
|
| 327 | 327 |
void shortHelp() const; |
| 328 | 328 |
void showHelp() const; |
| 329 | 329 |
public: |
| 330 | 330 |
|
| 331 | 331 |
///Start the parsing process |
| 332 | 332 |
ArgParser &parse(); |
| 333 | 333 |
|
| 334 | 334 |
/// Synonym for parse() |
| 335 | 335 |
ArgParser &run() |
| 336 | 336 |
{
|
| 337 | 337 |
return parse(); |
| 338 | 338 |
} |
| 339 | 339 |
|
| 340 | 340 |
///Give back the command name (the 0th argument) |
| 341 | 341 |
const std::string &commandName() const { return _command_name; }
|
| 342 | 342 |
|
| 343 | 343 |
///Check if an opion has been given to the command. |
| 344 | 344 |
bool given(std::string op) const |
| 345 | 345 |
{
|
| 346 | 346 |
Opts::const_iterator i = _opts.find(op); |
| 347 | 347 |
return i!=_opts.end()?i->second.set:false; |
| 348 | 348 |
} |
| 349 | 349 |
|
| 350 | 350 |
|
| 351 | 351 |
///Magic type for operator[] |
| 352 | 352 |
|
| 353 | 353 |
///This is the type of the return value of ArgParser::operator[](). |
| 354 | 354 |
///It automatically converts to \c int, \c double, \c bool or |
| 355 | 355 |
///\c std::string if the type of the option matches, which is checked |
| 356 | 356 |
///with an \ref LEMON_ASSERT "assertion" (i.e. it performs runtime |
| 357 | 357 |
///type checking). |
| 358 | 358 |
class RefType |
| 359 | 359 |
{
|
| 360 | 360 |
const ArgParser &_parser; |
| 361 | 361 |
std::string _name; |
| 362 | 362 |
public: |
| 363 | 363 |
///\e |
| 364 | 364 |
RefType(const ArgParser &p,const std::string &n) :_parser(p),_name(n) {}
|
| 365 | 365 |
///\e |
| 366 | 366 |
operator bool() |
| 367 | 367 |
{
|
| 368 | 368 |
Opts::const_iterator i = _parser._opts.find(_name); |
| 369 | 369 |
LEMON_ASSERT(i!=_parser._opts.end(), |
| 370 | 370 |
std::string()+"Unkown option: '"+_name+"'"); |
| 371 | 371 |
LEMON_ASSERT(i->second.type==ArgParser::BOOL, |
| 372 | 372 |
std::string()+"'"+_name+"' is a bool option"); |
| 373 | 373 |
return *(i->second.bool_p); |
| 374 | 374 |
} |
| 375 | 375 |
///\e |
| 376 | 376 |
operator std::string() |
| 377 | 377 |
{
|
| 378 | 378 |
Opts::const_iterator i = _parser._opts.find(_name); |
| 379 | 379 |
LEMON_ASSERT(i!=_parser._opts.end(), |
| 380 | 380 |
std::string()+"Unkown option: '"+_name+"'"); |
| 381 | 381 |
LEMON_ASSERT(i->second.type==ArgParser::STRING, |
| 382 | 382 |
std::string()+"'"+_name+"' is a string option"); |
| 383 | 383 |
return *(i->second.string_p); |
| 384 | 384 |
} |
| 385 | 385 |
///\e |
| 386 | 386 |
operator double() |
| 387 | 387 |
{
|
| 388 | 388 |
Opts::const_iterator i = _parser._opts.find(_name); |
| 389 | 389 |
LEMON_ASSERT(i!=_parser._opts.end(), |
| 390 | 390 |
std::string()+"Unkown option: '"+_name+"'"); |
| 391 | 391 |
LEMON_ASSERT(i->second.type==ArgParser::DOUBLE || |
| 392 | 392 |
i->second.type==ArgParser::INTEGER, |
| 393 | 393 |
std::string()+"'"+_name+"' is a floating point option"); |
| 394 | 394 |
return i->second.type==ArgParser::DOUBLE ? |
| 395 | 395 |
*(i->second.double_p) : *(i->second.int_p); |
| 396 | 396 |
} |
| 397 | 397 |
///\e |
| 398 | 398 |
operator int() |
| 399 | 399 |
{
|
| 400 | 400 |
Opts::const_iterator i = _parser._opts.find(_name); |
| 401 | 401 |
LEMON_ASSERT(i!=_parser._opts.end(), |
| 402 | 402 |
std::string()+"Unkown option: '"+_name+"'"); |
| 403 | 403 |
LEMON_ASSERT(i->second.type==ArgParser::INTEGER, |
| 404 | 404 |
std::string()+"'"+_name+"' is an integer option"); |
| 405 | 405 |
return *(i->second.int_p); |
| 406 | 406 |
} |
| 407 | 407 |
|
| 408 | 408 |
}; |
| 409 | 409 |
|
| 410 | 410 |
///Give back the value of an option |
| 411 | 411 |
|
| 412 | 412 |
///Give back the value of an option. |
| 413 | 413 |
///\sa RefType |
| 414 | 414 |
RefType operator[](const std::string &n) const |
| 415 | 415 |
{
|
| 416 | 416 |
return RefType(*this, n); |
| 417 | 417 |
} |
| 418 | 418 |
|
| 419 | 419 |
///Give back the non-option type arguments. |
| 420 | 420 |
|
| 421 | 421 |
///Give back a reference to a vector consisting of the program arguments |
| 422 | 422 |
///not starting with a '-' character. |
| 423 | 423 |
const std::vector<std::string> &files() const { return _file_args; }
|
| 424 | 424 |
|
| 425 | 425 |
///Throw instead of exit in case of problems |
| 426 | 426 |
void throwOnProblems() |
| 427 | 427 |
{
|
| 428 | 428 |
_exit_on_problems=false; |
| 429 | 429 |
} |
| 430 | 430 |
}; |
| 431 | 431 |
} |
| 432 | 432 |
|
| 433 | 433 |
#endif // LEMON_ARG_PARSER_H |
| 1 |
/* -*- C++ -*- |
|
| 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
| 2 | 2 |
* |
| 3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
| 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BELLMAN_FORD_H |
| 20 | 20 |
#define LEMON_BELLMAN_FORD_H |
| 21 | 21 |
|
| 22 | 22 |
/// \ingroup shortest_path |
| 23 | 23 |
/// \file |
| 24 | 24 |
/// \brief Bellman-Ford algorithm. |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/list_graph.h> |
| 27 | 27 |
#include <lemon/bits/path_dump.h> |
| 28 | 28 |
#include <lemon/core.h> |
| 29 | 29 |
#include <lemon/error.h> |
| 30 | 30 |
#include <lemon/maps.h> |
| 31 | 31 |
#include <lemon/tolerance.h> |
| 32 | 32 |
#include <lemon/path.h> |
| 33 | 33 |
|
| 34 | 34 |
#include <limits> |
| 35 | 35 |
|
| 36 | 36 |
namespace lemon {
|
| 37 | 37 |
|
| 38 | 38 |
/// \brief Default operation traits for the BellmanFord algorithm class. |
| 39 | 39 |
/// |
| 40 | 40 |
/// This operation traits class defines all computational operations |
| 41 | 41 |
/// and constants that are used in the Bellman-Ford algorithm. |
| 42 | 42 |
/// The default implementation is based on the \c numeric_limits class. |
| 43 | 43 |
/// If the numeric type does not have infinity value, then the maximum |
| 44 | 44 |
/// value is used as extremal infinity value. |
| 45 | 45 |
/// |
| 46 | 46 |
/// \see BellmanFordToleranceOperationTraits |
| 47 | 47 |
template < |
| 48 | 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 |
/// |
| 198 | 198 |
/// \ingroup shortest_path |
| 199 | 199 |
/// This class provides an efficient implementation of the Bellman-Ford |
| 200 | 200 |
/// algorithm. The maximum time complexity of the algorithm is |
| 201 | 201 |
/// <tt>O(ne)</tt>. |
| 202 | 202 |
/// |
| 203 | 203 |
/// The Bellman-Ford algorithm solves the single-source shortest path |
| 204 | 204 |
/// problem when the arcs can have negative lengths, but the digraph |
| 205 | 205 |
/// should not contain directed cycles with negative total length. |
| 206 | 206 |
/// If all arc costs are non-negative, consider to use the Dijkstra |
| 207 | 207 |
/// algorithm instead, since it is more efficient. |
| 208 | 208 |
/// |
| 209 | 209 |
/// The arc lengths are passed to the algorithm using a |
| 210 | 210 |
/// \ref concepts::ReadMap "ReadMap", so it is easy to change it to any |
| 211 | 211 |
/// kind of length. The type of the length values is determined by the |
| 212 | 212 |
/// \ref concepts::ReadMap::Value "Value" type of the length map. |
| 213 | 213 |
/// |
| 214 | 214 |
/// There is also a \ref bellmanFord() "function-type interface" for the |
| 215 | 215 |
/// Bellman-Ford algorithm, which is convenient in the simplier cases and |
| 216 | 216 |
/// it can be used easier. |
| 217 | 217 |
/// |
| 218 | 218 |
/// \tparam GR The type of the digraph the algorithm runs on. |
| 219 | 219 |
/// The default type is \ref ListDigraph. |
| 220 | 220 |
/// \tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies |
| 221 | 221 |
/// the lengths of the arcs. The default map type is |
| 222 | 222 |
/// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
| 223 | 223 |
/// \tparam TR The traits class that defines various types used by the |
| 224 | 224 |
/// algorithm. By default, it is \ref BellmanFordDefaultTraits |
| 225 | 225 |
/// "BellmanFordDefaultTraits<GR, LEN>". |
| 226 | 226 |
/// In most cases, this parameter should not be set directly, |
| 227 | 227 |
/// consider to use the named template parameters instead. |
| 228 | 228 |
#ifdef DOXYGEN |
| 229 | 229 |
template <typename GR, typename LEN, typename TR> |
| 230 | 230 |
#else |
| 231 | 231 |
template <typename GR=ListDigraph, |
| 232 | 232 |
typename LEN=typename GR::template ArcMap<int>, |
| 233 | 233 |
typename TR=BellmanFordDefaultTraits<GR,LEN> > |
| 234 | 234 |
#endif |
| 235 | 235 |
class BellmanFord {
|
| 236 | 236 |
public: |
| 237 | 237 |
|
| 238 | 238 |
///The type of the underlying digraph. |
| 239 | 239 |
typedef typename TR::Digraph Digraph; |
| 240 | 240 |
|
| 241 | 241 |
/// \brief The type of the arc lengths. |
| 242 | 242 |
typedef typename TR::LengthMap::Value Value; |
| 243 | 243 |
/// \brief The type of the map that stores the arc lengths. |
| 244 | 244 |
typedef typename TR::LengthMap LengthMap; |
| 245 | 245 |
/// \brief The type of the map that stores the last |
| 246 | 246 |
/// arcs of the shortest paths. |
| 247 | 247 |
typedef typename TR::PredMap PredMap; |
| 248 | 248 |
/// \brief The type of the map that stores the distances of the nodes. |
| 249 | 249 |
typedef typename TR::DistMap DistMap; |
| 250 | 250 |
/// The type of the paths. |
| 251 | 251 |
typedef PredMapPath<Digraph, PredMap> Path; |
| 252 | 252 |
///\brief The \ref BellmanFordDefaultOperationTraits |
| 253 | 253 |
/// "operation traits class" of the algorithm. |
| 254 | 254 |
typedef typename TR::OperationTraits OperationTraits; |
| 255 | 255 |
|
| 256 | 256 |
///The \ref BellmanFordDefaultTraits "traits class" of the algorithm. |
| 257 | 257 |
typedef TR Traits; |
| 258 | 258 |
|
| 259 | 259 |
private: |
| 260 | 260 |
|
| 261 | 261 |
typedef typename Digraph::Node Node; |
| 262 | 262 |
typedef typename Digraph::NodeIt NodeIt; |
| 263 | 263 |
typedef typename Digraph::Arc Arc; |
| 264 | 264 |
typedef typename Digraph::OutArcIt OutArcIt; |
| 265 | 265 |
|
| 266 | 266 |
// Pointer to the underlying digraph. |
| 267 | 267 |
const Digraph *_gr; |
| 268 | 268 |
// Pointer to the length map |
| 269 | 269 |
const LengthMap *_length; |
| 270 | 270 |
// Pointer to the map of predecessors arcs. |
| 271 | 271 |
PredMap *_pred; |
| 272 | 272 |
// Indicates if _pred is locally allocated (true) or not. |
| 273 | 273 |
bool _local_pred; |
| 274 | 274 |
// Pointer to the map of distances. |
| 275 | 275 |
DistMap *_dist; |
| 276 | 276 |
// Indicates if _dist is locally allocated (true) or not. |
| 277 | 277 |
bool _local_dist; |
| 278 | 278 |
|
| 279 | 279 |
typedef typename Digraph::template NodeMap<bool> MaskMap; |
| 280 | 280 |
MaskMap *_mask; |
| 281 | 281 |
|
| 282 | 282 |
std::vector<Node> _process; |
| 283 | 283 |
|
| 284 | 284 |
// Creates the maps if necessary. |
| 285 | 285 |
void create_maps() {
|
| 286 | 286 |
if(!_pred) {
|
| 287 | 287 |
_local_pred = true; |
| 288 | 288 |
_pred = Traits::createPredMap(*_gr); |
| 289 | 289 |
} |
| 290 | 290 |
if(!_dist) {
|
| 291 | 291 |
_local_dist = true; |
| 292 | 292 |
_dist = Traits::createDistMap(*_gr); |
| 293 | 293 |
} |
| 294 | 294 |
if(!_mask) {
|
| 295 | 295 |
_mask = new MaskMap(*_gr); |
| 296 | 296 |
} |
| 297 | 297 |
} |
| 298 | 298 |
|
| 299 | 299 |
public : |
| 300 | 300 |
|
| 301 | 301 |
typedef BellmanFord Create; |
| 302 | 302 |
|
| 303 | 303 |
/// \name Named Template Parameters |
| 304 | 304 |
|
| 305 | 305 |
///@{
|
| 306 | 306 |
|
| 307 | 307 |
template <class T> |
| 308 | 308 |
struct SetPredMapTraits : public Traits {
|
| 309 | 309 |
typedef T PredMap; |
| 310 | 310 |
static PredMap *createPredMap(const Digraph&) {
|
| 311 | 311 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
| 312 | 312 |
return 0; // ignore warnings |
| 313 | 313 |
} |
| 314 | 314 |
}; |
| 315 | 315 |
|
| 316 | 316 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 317 | 317 |
/// \c PredMap type. |
| 318 | 318 |
/// |
| 319 | 319 |
/// \ref named-templ-param "Named parameter" for setting |
| 320 | 320 |
/// \c PredMap type. |
| 321 | 321 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 322 | 322 |
template <class T> |
| 323 | 323 |
struct SetPredMap |
| 324 | 324 |
: public BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > {
|
| 325 | 325 |
typedef BellmanFord< Digraph, LengthMap, SetPredMapTraits<T> > Create; |
| 326 | 326 |
}; |
| 327 | 327 |
|
| 328 | 328 |
template <class T> |
| 329 | 329 |
struct SetDistMapTraits : public Traits {
|
| 330 | 330 |
typedef T DistMap; |
| 331 | 331 |
static DistMap *createDistMap(const Digraph&) {
|
| 332 | 332 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
| 333 | 333 |
return 0; // ignore warnings |
| 334 | 334 |
} |
| 335 | 335 |
}; |
| 336 | 336 |
|
| 337 | 337 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 338 | 338 |
/// \c DistMap type. |
| 339 | 339 |
/// |
| 340 | 340 |
/// \ref named-templ-param "Named parameter" for setting |
| 341 | 341 |
/// \c DistMap type. |
| 342 | 342 |
/// It must conform to the \ref concepts::WriteMap "WriteMap" concept. |
| 343 | 343 |
template <class T> |
| 344 | 344 |
struct SetDistMap |
| 345 | 345 |
: public BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > {
|
| 346 | 346 |
typedef BellmanFord< Digraph, LengthMap, SetDistMapTraits<T> > Create; |
| 347 | 347 |
}; |
| 348 | 348 |
|
| 349 | 349 |
template <class T> |
| 350 | 350 |
struct SetOperationTraitsTraits : public Traits {
|
| 351 | 351 |
typedef T OperationTraits; |
| 352 | 352 |
}; |
| 353 | 353 |
|
| 354 | 354 |
/// \brief \ref named-templ-param "Named parameter" for setting |
| 355 | 355 |
/// \c OperationTraits type. |
| 356 | 356 |
/// |
| 357 | 357 |
/// \ref named-templ-param "Named parameter" for setting |
| 358 | 358 |
/// \c OperationTraits type. |
| 359 | 359 |
/// For more information, see \ref BellmanFordDefaultOperationTraits. |
| 360 | 360 |
template <class T> |
| 361 | 361 |
struct SetOperationTraits |
| 362 | 362 |
: public BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> > {
|
| 363 | 363 |
typedef BellmanFord< Digraph, LengthMap, SetOperationTraitsTraits<T> > |
| 364 | 364 |
Create; |
| 365 | 365 |
}; |
| 366 | 366 |
|
| 367 | 367 |
///@} |
| 368 | 368 |
|
| 369 | 369 |
protected: |
| 370 | 370 |
|
| 371 | 371 |
BellmanFord() {}
|
| 372 | 372 |
|
| 373 | 373 |
public: |
| 374 | 374 |
|
| 375 | 375 |
/// \brief Constructor. |
| 376 | 376 |
/// |
| 377 | 377 |
/// Constructor. |
| 378 | 378 |
/// \param g The digraph the algorithm runs on. |
| 379 | 379 |
/// \param length The length map used by the algorithm. |
| 380 | 380 |
BellmanFord(const Digraph& g, const LengthMap& length) : |
| 381 | 381 |
_gr(&g), _length(&length), |
| 382 | 382 |
_pred(0), _local_pred(false), |
| 383 | 383 |
_dist(0), _local_dist(false), _mask(0) {}
|
| 384 | 384 |
|
| 385 | 385 |
///Destructor. |
| 386 | 386 |
~BellmanFord() {
|
| 387 | 387 |
if(_local_pred) delete _pred; |
| 388 | 388 |
if(_local_dist) delete _dist; |
| 389 | 389 |
if(_mask) delete _mask; |
| 390 | 390 |
} |
| 391 | 391 |
|
| 392 | 392 |
/// \brief Sets the length map. |
| 393 | 393 |
/// |
| 394 | 394 |
/// Sets the length map. |
| 395 | 395 |
/// \return <tt>(*this)</tt> |
| 396 | 396 |
BellmanFord &lengthMap(const LengthMap &map) {
|
| 397 | 397 |
_length = ↦ |
| 398 | 398 |
return *this; |
| 399 | 399 |
} |
| 400 | 400 |
|
| 401 | 401 |
/// \brief Sets the map that stores the predecessor arcs. |
| 402 | 402 |
/// |
| 403 | 403 |
/// Sets the map that stores the predecessor arcs. |
| 404 | 404 |
/// If you don't use this function before calling \ref run() |
| 405 | 405 |
/// or \ref init(), an instance will be allocated automatically. |
| 406 | 406 |
/// The destructor deallocates this automatically allocated map, |
| 407 | 407 |
/// of course. |
| 408 | 408 |
/// \return <tt>(*this)</tt> |
| 409 | 409 |
BellmanFord &predMap(PredMap &map) {
|
| 410 | 410 |
if(_local_pred) {
|
| 411 | 411 |
delete _pred; |
| 412 | 412 |
_local_pred=false; |
| 413 | 413 |
} |
| 414 | 414 |
_pred = ↦ |
| 415 | 415 |
return *this; |
| 416 | 416 |
} |
| 417 | 417 |
|
| 418 | 418 |
/// \brief Sets the map that stores the distances of the nodes. |
| 419 | 419 |
/// |
| 420 | 420 |
/// Sets the map that stores the distances of the nodes calculated |
| 421 | 421 |
/// by the algorithm. |
| 422 | 422 |
/// If you don't use this function before calling \ref run() |
| 423 | 423 |
/// or \ref init(), an instance will be allocated automatically. |
| 424 | 424 |
/// The destructor deallocates this automatically allocated map, |
| 425 | 425 |
/// of course. |
| 426 | 426 |
/// \return <tt>(*this)</tt> |
| 427 | 427 |
BellmanFord &distMap(DistMap &map) {
|
| 428 | 428 |
if(_local_dist) {
|
| 429 | 429 |
delete _dist; |
| 430 | 430 |
_local_dist=false; |
| 431 | 431 |
} |
| 432 | 432 |
_dist = ↦ |
| 433 | 433 |
return *this; |
| 434 | 434 |
} |
| 435 | 435 |
|
| 436 | 436 |
/// \name Execution Control |
| 437 | 437 |
/// The simplest way to execute the Bellman-Ford algorithm is to use |
| 438 | 438 |
/// one of the member functions called \ref run().\n |
| 439 | 439 |
/// If you need better control on the execution, you have to call |
| 440 | 440 |
/// \ref init() first, then you can add several source nodes |
| 441 | 441 |
/// with \ref addSource(). Finally the actual path computation can be |
| 442 | 442 |
/// performed with \ref start(), \ref checkedStart() or |
| 443 | 443 |
/// \ref limitedStart(). |
| 444 | 444 |
|
| 445 | 445 |
///@{
|
| 446 | 446 |
|
| 447 | 447 |
/// \brief Initializes the internal data structures. |
| 448 | 448 |
/// |
| 449 | 449 |
/// Initializes the internal data structures. The optional parameter |
| 450 | 450 |
/// is the initial distance of each node. |
| 451 | 451 |
void init(const Value value = OperationTraits::infinity()) {
|
| 452 | 452 |
create_maps(); |
| 453 | 453 |
for (NodeIt it(*_gr); it != INVALID; ++it) {
|
| 454 | 454 |
_pred->set(it, INVALID); |
| 455 | 455 |
_dist->set(it, value); |
| 456 | 456 |
} |
| 457 | 457 |
_process.clear(); |
| 458 | 458 |
if (OperationTraits::less(value, OperationTraits::infinity())) {
|
| 459 | 459 |
for (NodeIt it(*_gr); it != INVALID; ++it) {
|
| 460 | 460 |
_process.push_back(it); |
| 461 | 461 |
_mask->set(it, true); |
| 462 | 462 |
} |
| 463 | 463 |
} else {
|
| 464 | 464 |
for (NodeIt it(*_gr); it != INVALID; ++it) {
|
| 465 | 465 |
_mask->set(it, false); |
| 466 | 466 |
} |
| 467 | 467 |
} |
| 468 | 468 |
} |
| 469 | 469 |
|
| 470 | 470 |
/// \brief Adds a new source node. |
| 471 | 471 |
/// |
| 472 | 472 |
/// This function adds a new source node. The optional second parameter |
| 473 | 473 |
/// is the initial distance of the node. |
| 474 | 474 |
void addSource(Node source, Value dst = OperationTraits::zero()) {
|
| 475 | 475 |
_dist->set(source, dst); |
| 476 | 476 |
if (!(*_mask)[source]) {
|
| 477 | 477 |
_process.push_back(source); |
| 478 | 478 |
_mask->set(source, true); |
| 479 | 479 |
} |
| 480 | 480 |
} |
| 481 | 481 |
|
| 482 | 482 |
/// \brief Executes one round from the Bellman-Ford algorithm. |
| 483 | 483 |
/// |
| 484 | 484 |
/// If the algoritm calculated the distances in the previous round |
| 485 | 485 |
/// exactly for the paths of at most \c k arcs, then this function |
| 486 | 486 |
/// will calculate the distances exactly for the paths of at most |
| 487 | 487 |
/// <tt>k+1</tt> arcs. Performing \c k iterations using this function |
| 488 | 488 |
/// calculates the shortest path distances exactly for the paths |
| 489 | 489 |
/// consisting of at most \c k arcs. |
| 490 | 490 |
/// |
| 491 | 491 |
/// \warning The paths with limited arc number cannot be retrieved |
| 492 | 492 |
/// easily with \ref path() or \ref predArc() functions. If you also |
| 493 | 493 |
/// need the shortest paths and not only the distances, you should |
| 494 | 494 |
/// store the \ref predMap() "predecessor map" after each iteration |
| 495 | 495 |
/// and build the path manually. |
| 496 | 496 |
/// |
| 497 | 497 |
/// \return \c true when the algorithm have not found more shorter |
| 498 | 498 |
/// paths. |
| 499 | 499 |
/// |
| 500 | 500 |
/// \see ActiveIt |
| 501 | 501 |
bool processNextRound() {
|
| 502 | 502 |
for (int i = 0; i < int(_process.size()); ++i) {
|
| 503 | 503 |
_mask->set(_process[i], false); |
| 504 | 504 |
} |
| 505 | 505 |
std::vector<Node> nextProcess; |
| 506 | 506 |
std::vector<Value> values(_process.size()); |
| 507 | 507 |
for (int i = 0; i < int(_process.size()); ++i) {
|
| 508 | 508 |
values[i] = (*_dist)[_process[i]]; |
| 509 | 509 |
} |
| 510 | 510 |
for (int i = 0; i < int(_process.size()); ++i) {
|
| 511 | 511 |
for (OutArcIt it(*_gr, _process[i]); it != INVALID; ++it) {
|
| 512 | 512 |
Node target = _gr->target(it); |
| 513 | 513 |
Value relaxed = OperationTraits::plus(values[i], (*_length)[it]); |
| 514 | 514 |
if (OperationTraits::less(relaxed, (*_dist)[target])) {
|
| 515 | 515 |
_pred->set(target, it); |
| 516 | 516 |
_dist->set(target, relaxed); |
| 517 | 517 |
if (!(*_mask)[target]) {
|
| 518 | 518 |
_mask->set(target, true); |
| 519 | 519 |
nextProcess.push_back(target); |
| 520 | 520 |
} |
| 521 | 521 |
} |
| 522 | 522 |
} |
| 523 | 523 |
} |
| 524 | 524 |
_process.swap(nextProcess); |
| 525 | 525 |
return _process.empty(); |
| 526 | 526 |
} |
| 527 | 527 |
|
| 528 | 528 |
/// \brief Executes one weak round from the Bellman-Ford algorithm. |
| 529 | 529 |
/// |
| 530 | 530 |
/// If the algorithm calculated the distances in the previous round |
| 531 | 531 |
/// at least for the paths of at most \c k arcs, then this function |
| 532 | 532 |
/// will calculate the distances at least for the paths of at most |
| 533 | 533 |
/// <tt>k+1</tt> arcs. |
| 534 | 534 |
/// This function does not make it possible to calculate the shortest |
| 535 | 535 |
/// path distances exactly for paths consisting of at most \c k arcs, |
| 536 | 536 |
/// this is why it is called weak round. |
| 537 | 537 |
/// |
| 538 | 538 |
/// \return \c true when the algorithm have not found more shorter |
| 539 | 539 |
/// paths. |
| 540 | 540 |
/// |
| 541 | 541 |
/// \see ActiveIt |
| 542 | 542 |
bool processNextWeakRound() {
|
| 543 | 543 |
for (int i = 0; i < int(_process.size()); ++i) {
|
| 544 | 544 |
_mask->set(_process[i], false); |
| 545 | 545 |
} |
| 546 | 546 |
std::vector<Node> nextProcess; |
| 547 | 547 |
for (int i = 0; i < int(_process.size()); ++i) {
|
| 548 | 548 |
for (OutArcIt it(*_gr, _process[i]); it != INVALID; ++it) {
|
| 549 | 549 |
Node target = _gr->target(it); |
| 550 | 550 |
Value relaxed = |
| 551 | 551 |
OperationTraits::plus((*_dist)[_process[i]], (*_length)[it]); |
| 552 | 552 |
if (OperationTraits::less(relaxed, (*_dist)[target])) {
|
| 553 | 553 |
_pred->set(target, it); |
| 554 | 554 |
_dist->set(target, relaxed); |
| 555 | 555 |
if (!(*_mask)[target]) {
|
| 556 | 556 |
_mask->set(target, true); |
| 557 | 557 |
nextProcess.push_back(target); |
| 558 | 558 |
} |
| 559 | 559 |
} |
| 560 | 560 |
} |
| 561 | 561 |
} |
| 562 | 562 |
_process.swap(nextProcess); |
| 563 | 563 |
return _process.empty(); |
| 564 | 564 |
} |
| 565 | 565 |
|
| 566 | 566 |
/// \brief Executes the algorithm. |
| 567 | 567 |
/// |
| 568 | 568 |
/// Executes the algorithm. |
| 569 | 569 |
/// |
| 570 | 570 |
/// This method runs the Bellman-Ford algorithm from the root node(s) |
| 571 | 571 |
/// in order to compute the shortest path to each node. |
| 572 | 572 |
/// |
| 573 | 573 |
/// The algorithm computes |
| 574 | 574 |
/// - the shortest path tree (forest), |
| 575 | 575 |
/// - the distance of each node from the root(s). |
| 576 | 576 |
/// |
| 577 | 577 |
/// \pre init() must be called and at least one root node should be |
| 578 | 578 |
/// added with addSource() before using this function. |
| 579 | 579 |
void start() {
|
| 580 | 580 |
int num = countNodes(*_gr) - 1; |
| 581 | 581 |
for (int i = 0; i < num; ++i) {
|
| 582 | 582 |
if (processNextWeakRound()) break; |
| 583 | 583 |
} |
| 584 | 584 |
} |
| 585 | 585 |
|
| 586 | 586 |
/// \brief Executes the algorithm and checks the negative cycles. |
| 587 | 587 |
/// |
| 588 | 588 |
/// Executes the algorithm and checks the negative cycles. |
| 589 | 589 |
/// |
| 590 | 590 |
/// This method runs the Bellman-Ford algorithm from the root node(s) |
| 591 | 591 |
/// in order to compute the shortest path to each node and also checks |
| 592 | 592 |
/// if the digraph contains cycles with negative total length. |
| 593 | 593 |
/// |
| 594 | 594 |
/// The algorithm computes |
| 595 | 595 |
/// - the shortest path tree (forest), |
| 596 | 596 |
/// - the distance of each node from the root(s). |
| 597 | 597 |
/// |
| 598 | 598 |
/// \return \c false if there is a negative cycle in the digraph. |
| 599 | 599 |
/// |
| 600 | 600 |
/// \pre init() must be called and at least one root node should be |
| 601 | 601 |
/// added with addSource() before using this function. |
| 602 | 602 |
bool checkedStart() {
|
| 603 | 603 |
int num = countNodes(*_gr); |
| 604 | 604 |
for (int i = 0; i < num; ++i) {
|
| 605 | 605 |
if (processNextWeakRound()) return true; |
| 606 | 606 |
} |
| 607 | 607 |
return _process.empty(); |
| 608 | 608 |
} |
| 609 | 609 |
|
| 610 | 610 |
/// \brief Executes the algorithm with arc number limit. |
| 611 | 611 |
/// |
| 612 | 612 |
/// Executes the algorithm with arc number limit. |
| 613 | 613 |
/// |
| 614 | 614 |
/// This method runs the Bellman-Ford algorithm from the root node(s) |
| 615 | 615 |
/// in order to compute the shortest path distance for each node |
| 616 | 616 |
/// using only the paths consisting of at most \c num arcs. |
| 617 | 617 |
/// |
| 618 | 618 |
/// The algorithm computes |
| 619 | 619 |
/// - the limited distance of each node from the root(s), |
| 620 | 620 |
/// - the predecessor arc for each node. |
| 621 | 621 |
/// |
| 622 | 622 |
/// \warning The paths with limited arc number cannot be retrieved |
| 623 | 623 |
/// easily with \ref path() or \ref predArc() functions. If you also |
| 624 | 624 |
/// need the shortest paths and not only the distances, you should |
| 625 | 625 |
/// store the \ref predMap() "predecessor map" after each iteration |
| 626 | 626 |
/// and build the path manually. |
| 627 | 627 |
/// |
| 628 | 628 |
/// \pre init() must be called and at least one root node should be |
| 629 | 629 |
/// added with addSource() before using this function. |
| 630 | 630 |
void limitedStart(int num) {
|
| 631 | 631 |
for (int i = 0; i < num; ++i) {
|
| 632 | 632 |
if (processNextRound()) break; |
| 633 | 633 |
} |
| 634 | 634 |
} |
| 635 | 635 |
|
| 636 | 636 |
/// \brief Runs the algorithm from the given root node. |
| 637 | 637 |
/// |
| 638 | 638 |
/// This method runs the Bellman-Ford algorithm from the given root |
| 639 | 639 |
/// node \c s in order to compute the shortest path to each node. |
| 640 | 640 |
/// |
| 641 | 641 |
/// The algorithm computes |
| 642 | 642 |
/// - the shortest path tree (forest), |
| 643 | 643 |
/// - the distance of each node from the root(s). |
| 644 | 644 |
/// |
| 645 | 645 |
/// \note bf.run(s) is just a shortcut of the following code. |
| 646 | 646 |
/// \code |
| 647 | 647 |
/// bf.init(); |
| 648 | 648 |
/// bf.addSource(s); |
| 649 | 649 |
/// bf.start(); |
| 650 | 650 |
/// \endcode |
| 651 | 651 |
void run(Node s) {
|
| 652 | 652 |
init(); |
| 653 | 653 |
addSource(s); |
| 654 | 654 |
start(); |
| 655 | 655 |
} |
| 656 | 656 |
|
| 657 | 657 |
/// \brief Runs the algorithm from the given root node with arc |
| 658 | 658 |
/// number limit. |
| 659 | 659 |
/// |
| 660 | 660 |
/// This method runs the Bellman-Ford algorithm from the given root |
| 661 | 661 |
/// node \c s in order to compute the shortest path distance for each |
| 662 | 662 |
/// node using only the paths consisting of at most \c num arcs. |
| 663 | 663 |
/// |
| 664 | 664 |
/// The algorithm computes |
| 665 | 665 |
/// - the limited distance of each node from the root(s), |
| 666 | 666 |
/// - the predecessor arc for each node. |
| 667 | 667 |
/// |
| 668 | 668 |
/// \warning The paths with limited arc number cannot be retrieved |
| 669 | 669 |
/// easily with \ref path() or \ref predArc() functions. If you also |
| 670 | 670 |
/// need the shortest paths and not only the distances, you should |
| 671 | 671 |
/// store the \ref predMap() "predecessor map" after each iteration |
| 672 | 672 |
/// and build the path manually. |
| 673 | 673 |
/// |
| 674 | 674 |
/// \note bf.run(s, num) is just a shortcut of the following code. |
| 675 | 675 |
/// \code |
| 676 | 676 |
/// bf.init(); |
| 677 | 677 |
/// bf.addSource(s); |
| 678 | 678 |
/// bf.limitedStart(num); |
| 679 | 679 |
/// \endcode |
| 680 | 680 |
void run(Node s, int num) {
|
| 681 | 681 |
init(); |
| 682 | 682 |
addSource(s); |
| 683 | 683 |
limitedStart(num); |
| 684 | 684 |
} |
| 685 | 685 |
|
| 686 | 686 |
///@} |
| 687 | 687 |
|
| 688 | 688 |
/// \brief LEMON iterator for getting the active nodes. |
| 689 | 689 |
/// |
| 690 | 690 |
/// This class provides a common style LEMON iterator that traverses |
| 691 | 691 |
/// the active nodes of the Bellman-Ford algorithm after the last |
| 692 | 692 |
/// phase. These nodes should be checked in the next phase to |
| 693 | 693 |
/// find augmenting arcs outgoing from them. |
| 694 | 694 |
class ActiveIt {
|
| 695 | 695 |
public: |
| 696 | 696 |
|
| 697 | 697 |
/// \brief Constructor. |
| 698 | 698 |
/// |
| 699 | 699 |
/// Constructor for getting the active nodes of the given BellmanFord |
| 700 | 700 |
/// instance. |
| 701 | 701 |
ActiveIt(const BellmanFord& algorithm) : _algorithm(&algorithm) |
| 702 | 702 |
{
|
| 703 | 703 |
_index = _algorithm->_process.size() - 1; |
| 704 | 704 |
} |
| 705 | 705 |
|
| 706 | 706 |
/// \brief Invalid constructor. |
| 707 | 707 |
/// |
| 708 | 708 |
/// Invalid constructor. |
| 709 | 709 |
ActiveIt(Invalid) : _algorithm(0), _index(-1) {}
|
| 710 | 710 |
|
| 711 | 711 |
/// \brief Conversion to \c Node. |
| 712 | 712 |
/// |
| 713 | 713 |
/// Conversion to \c Node. |
| 714 | 714 |
operator Node() const {
|
| 715 | 715 |
return _index >= 0 ? _algorithm->_process[_index] : INVALID; |
| 716 | 716 |
} |
| 717 | 717 |
|
| 718 | 718 |
/// \brief Increment operator. |
| 719 | 719 |
/// |
| 720 | 720 |
/// Increment operator. |
| 721 | 721 |
ActiveIt& operator++() {
|
| 722 | 722 |
--_index; |
| 723 | 723 |
return *this; |
| 724 | 724 |
} |
| 725 | 725 |
|
| 726 | 726 |
bool operator==(const ActiveIt& it) const {
|
| 727 | 727 |
return static_cast<Node>(*this) == static_cast<Node>(it); |
| 728 | 728 |
} |
| 729 | 729 |
bool operator!=(const ActiveIt& it) const {
|
| 730 | 730 |
return static_cast<Node>(*this) != static_cast<Node>(it); |
| 731 | 731 |
} |
| 732 | 732 |
bool operator<(const ActiveIt& it) const {
|
| 733 | 733 |
return static_cast<Node>(*this) < static_cast<Node>(it); |
| 734 | 734 |
} |
| 735 | 735 |
|
| 736 | 736 |
private: |
| 737 | 737 |
const BellmanFord* _algorithm; |
| 738 | 738 |
int _index; |
| 739 | 739 |
}; |
| 740 | 740 |
|
| 741 | 741 |
/// \name Query Functions |
| 742 | 742 |
/// The result of the Bellman-Ford algorithm can be obtained using these |
| 743 | 743 |
/// functions.\n |
| 744 | 744 |
/// Either \ref run() or \ref init() should be called before using them. |
| 745 | 745 |
|
| 746 | 746 |
///@{
|
| 747 | 747 |
|
| 748 | 748 |
/// \brief The shortest path to the given node. |
| 749 | 749 |
/// |
| 750 | 750 |
/// Gives back the shortest path to the given node from the root(s). |
| 751 | 751 |
/// |
| 752 | 752 |
/// \warning \c t should be reached from the root(s). |
| 753 | 753 |
/// |
| 754 | 754 |
/// \pre Either \ref run() or \ref init() must be called before |
| 755 | 755 |
/// using this function. |
| 756 | 756 |
Path path(Node t) const |
| 757 | 757 |
{
|
| 758 | 758 |
return Path(*_gr, *_pred, t); |
| 759 | 759 |
} |
| 760 | 760 |
|
| 761 | 761 |
/// \brief The distance of the given node from the root(s). |
| 762 | 762 |
/// |
| 763 | 763 |
/// Returns the distance of the given node from the root(s). |
| 764 | 764 |
/// |
| 765 | 765 |
/// \warning If node \c v is not reached from the root(s), then |
| 766 | 766 |
/// the return value of this function is undefined. |
| 767 | 767 |
/// |
| 768 | 768 |
/// \pre Either \ref run() or \ref init() must be called before |
| 769 | 769 |
/// using this function. |
| 770 | 770 |
Value dist(Node v) const { return (*_dist)[v]; }
|
| 771 | 771 |
|
| 772 | 772 |
/// \brief Returns the 'previous arc' of the shortest path tree for |
| 773 | 773 |
/// the given node. |
| 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 |
/// |
| 467 | 469 |
///\pre The queue must not be empty. |
| 468 | 470 |
Node processNextNode() |
| 469 | 471 |
{
|
| 470 | 472 |
if(_queue_tail==_queue_next_dist) {
|
| 471 | 473 |
_curr_dist++; |
| 472 | 474 |
_queue_next_dist=_queue_head; |
| 473 | 475 |
} |
| 474 | 476 |
Node n=_queue[_queue_tail++]; |
| 475 | 477 |
_processed->set(n,true); |
| 476 | 478 |
Node m; |
| 477 | 479 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
| 478 | 480 |
if(!(*_reached)[m=G->target(e)]) {
|
| 479 | 481 |
_queue[_queue_head++]=m; |
| 480 | 482 |
_reached->set(m,true); |
| 481 | 483 |
_pred->set(m,e); |
| 482 | 484 |
_dist->set(m,_curr_dist); |
| 483 | 485 |
} |
| 484 | 486 |
return n; |
| 485 | 487 |
} |
| 486 | 488 |
|
| 487 | 489 |
///Processes the next node. |
| 488 | 490 |
|
| 489 | 491 |
///Processes the next node and checks if the given target node |
| 490 | 492 |
///is reached. If the target node is reachable from the processed |
| 491 | 493 |
///node, then the \c reach parameter will be set to \c true. |
| 492 | 494 |
/// |
| 493 | 495 |
///\param target The target node. |
| 494 | 496 |
///\retval reach Indicates if the target node is reached. |
| 495 | 497 |
///It should be initially \c false. |
| 496 | 498 |
/// |
| 497 | 499 |
///\return The processed node. |
| 498 | 500 |
/// |
| 499 | 501 |
///\pre The queue must not be empty. |
| 500 | 502 |
Node processNextNode(Node target, bool& reach) |
| 501 | 503 |
{
|
| 502 | 504 |
if(_queue_tail==_queue_next_dist) {
|
| 503 | 505 |
_curr_dist++; |
| 504 | 506 |
_queue_next_dist=_queue_head; |
| 505 | 507 |
} |
| 506 | 508 |
Node n=_queue[_queue_tail++]; |
| 507 | 509 |
_processed->set(n,true); |
| 508 | 510 |
Node m; |
| 509 | 511 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
| 510 | 512 |
if(!(*_reached)[m=G->target(e)]) {
|
| 511 | 513 |
_queue[_queue_head++]=m; |
| 512 | 514 |
_reached->set(m,true); |
| 513 | 515 |
_pred->set(m,e); |
| 514 | 516 |
_dist->set(m,_curr_dist); |
| 515 | 517 |
reach = reach || (target == m); |
| 516 | 518 |
} |
| 517 | 519 |
return n; |
| 518 | 520 |
} |
| 519 | 521 |
|
| 520 | 522 |
///Processes the next node. |
| 521 | 523 |
|
| 522 | 524 |
///Processes the next node and checks if at least one of reached |
| 523 | 525 |
///nodes has \c true value in the \c nm node map. If one node |
| 524 | 526 |
///with \c true value is reachable from the processed node, then the |
| 525 | 527 |
///\c rnode parameter will be set to the first of such nodes. |
| 526 | 528 |
/// |
| 527 | 529 |
///\param nm A \c bool (or convertible) node map that indicates the |
| 528 | 530 |
///possible targets. |
| 529 | 531 |
///\retval rnode The reached target node. |
| 530 | 532 |
///It should be initially \c INVALID. |
| 531 | 533 |
/// |
| 532 | 534 |
///\return The processed node. |
| 533 | 535 |
/// |
| 534 | 536 |
///\pre The queue must not be empty. |
| 535 | 537 |
template<class NM> |
| 536 | 538 |
Node processNextNode(const NM& nm, Node& rnode) |
| 537 | 539 |
{
|
| 538 | 540 |
if(_queue_tail==_queue_next_dist) {
|
| 539 | 541 |
_curr_dist++; |
| 540 | 542 |
_queue_next_dist=_queue_head; |
| 541 | 543 |
} |
| 542 | 544 |
Node n=_queue[_queue_tail++]; |
| 543 | 545 |
_processed->set(n,true); |
| 544 | 546 |
Node m; |
| 545 | 547 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
| 546 | 548 |
if(!(*_reached)[m=G->target(e)]) {
|
| 547 | 549 |
_queue[_queue_head++]=m; |
| 548 | 550 |
_reached->set(m,true); |
| 549 | 551 |
_pred->set(m,e); |
| 550 | 552 |
_dist->set(m,_curr_dist); |
| 551 | 553 |
if (nm[m] && rnode == INVALID) rnode = m; |
| 552 | 554 |
} |
| 553 | 555 |
return n; |
| 554 | 556 |
} |
| 555 | 557 |
|
| 556 | 558 |
///The next node to be processed. |
| 557 | 559 |
|
| 558 | 560 |
///Returns the next node to be processed or \c INVALID if the queue |
| 559 | 561 |
///is empty. |
| 560 | 562 |
Node nextNode() const |
| 561 | 563 |
{
|
| 562 | 564 |
return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID; |
| 563 | 565 |
} |
| 564 | 566 |
|
| 565 | 567 |
///Returns \c false if there are nodes to be processed. |
| 566 | 568 |
|
| 567 | 569 |
///Returns \c false if there are nodes to be processed |
| 568 | 570 |
///in the queue. |
| 569 | 571 |
bool emptyQueue() const { return _queue_tail==_queue_head; }
|
| 570 | 572 |
|
| 571 | 573 |
///Returns the number of the nodes to be processed. |
| 572 | 574 |
|
| 573 | 575 |
///Returns the number of the nodes to be processed |
| 574 | 576 |
///in the queue. |
| 575 | 577 |
int queueSize() const { return _queue_head-_queue_tail; }
|
| 576 | 578 |
|
| 577 | 579 |
///Executes the algorithm. |
| 578 | 580 |
|
| 579 | 581 |
///Executes the algorithm. |
| 580 | 582 |
/// |
| 581 | 583 |
///This method runs the %BFS algorithm from the root node(s) |
| 582 | 584 |
///in order to compute the shortest path to each node. |
| 583 | 585 |
/// |
| 584 | 586 |
///The algorithm computes |
| 585 | 587 |
///- the shortest path tree (forest), |
| 586 | 588 |
///- the distance of each node from the root(s). |
| 587 | 589 |
/// |
| 588 | 590 |
///\pre init() must be called and at least one root node should be |
| 589 | 591 |
///added with addSource() before using this function. |
| 590 | 592 |
/// |
| 591 | 593 |
///\note <tt>b.start()</tt> is just a shortcut of the following code. |
| 592 | 594 |
///\code |
| 593 | 595 |
/// while ( !b.emptyQueue() ) {
|
| 594 | 596 |
/// b.processNextNode(); |
| 595 | 597 |
/// } |
| 596 | 598 |
///\endcode |
| 597 | 599 |
void start() |
| 598 | 600 |
{
|
| 599 | 601 |
while ( !emptyQueue() ) processNextNode(); |
| 600 | 602 |
} |
| 601 | 603 |
|
| 602 | 604 |
///Executes the algorithm until the given target node is reached. |
| 603 | 605 |
|
| 604 | 606 |
///Executes the algorithm until the given target node is reached. |
| 605 | 607 |
/// |
| 606 | 608 |
///This method runs the %BFS algorithm from the root node(s) |
| 607 | 609 |
///in order to compute the shortest path to \c t. |
| 608 | 610 |
/// |
| 609 | 611 |
///The algorithm computes |
| 610 | 612 |
///- the shortest path to \c t, |
| 611 | 613 |
///- the distance of \c t from the root(s). |
| 612 | 614 |
/// |
| 613 | 615 |
///\pre init() must be called and at least one root node should be |
| 614 | 616 |
///added with addSource() before using this function. |
| 615 | 617 |
/// |
| 616 | 618 |
///\note <tt>b.start(t)</tt> is just a shortcut of the following code. |
| 617 | 619 |
///\code |
| 618 | 620 |
/// bool reach = false; |
| 619 | 621 |
/// while ( !b.emptyQueue() && !reach ) {
|
| 620 | 622 |
/// b.processNextNode(t, reach); |
| 621 | 623 |
/// } |
| 622 | 624 |
///\endcode |
| 623 | 625 |
void start(Node t) |
| 624 | 626 |
{
|
| 625 | 627 |
bool reach = false; |
| 626 | 628 |
while ( !emptyQueue() && !reach ) processNextNode(t, reach); |
| 627 | 629 |
} |
| 628 | 630 |
|
| 629 | 631 |
///Executes the algorithm until a condition is met. |
| 630 | 632 |
|
| 631 | 633 |
///Executes the algorithm until a condition is met. |
| 632 | 634 |
/// |
| 633 | 635 |
///This method runs the %BFS algorithm from the root node(s) in |
| 634 | 636 |
///order to compute the shortest path to a node \c v with |
| 635 | 637 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
| 636 | 638 |
/// |
| 637 | 639 |
///\param nm A \c bool (or convertible) node map. The algorithm |
| 638 | 640 |
///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. |
| 639 | 641 |
/// |
| 640 | 642 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
| 641 | 643 |
///\c INVALID if no such node was found. |
| 642 | 644 |
/// |
| 643 | 645 |
///\pre init() must be called and at least one root node should be |
| 644 | 646 |
///added with addSource() before using this function. |
| 645 | 647 |
/// |
| 646 | 648 |
///\note <tt>b.start(nm)</tt> is just a shortcut of the following code. |
| 647 | 649 |
///\code |
| 648 | 650 |
/// Node rnode = INVALID; |
| 649 | 651 |
/// while ( !b.emptyQueue() && rnode == INVALID ) {
|
| 650 | 652 |
/// b.processNextNode(nm, rnode); |
| 651 | 653 |
/// } |
| 652 | 654 |
/// return rnode; |
| 653 | 655 |
///\endcode |
| 654 | 656 |
template<class NodeBoolMap> |
| 655 | 657 |
Node start(const NodeBoolMap &nm) |
| 656 | 658 |
{
|
| 657 | 659 |
Node rnode = INVALID; |
| 658 | 660 |
while ( !emptyQueue() && rnode == INVALID ) {
|
| 659 | 661 |
processNextNode(nm, rnode); |
| 660 | 662 |
} |
| 661 | 663 |
return rnode; |
| 662 | 664 |
} |
| 663 | 665 |
|
| 664 | 666 |
///Runs the algorithm from the given source node. |
| 665 | 667 |
|
| 666 | 668 |
///This method runs the %BFS algorithm from node \c s |
| 667 | 669 |
///in order to compute the shortest path to each node. |
| 668 | 670 |
/// |
| 669 | 671 |
///The algorithm computes |
| 670 | 672 |
///- the shortest path tree, |
| 671 | 673 |
///- the distance of each node from the root. |
| 672 | 674 |
/// |
| 673 | 675 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
| 674 | 676 |
///\code |
| 675 | 677 |
/// b.init(); |
| 676 | 678 |
/// b.addSource(s); |
| 677 | 679 |
/// b.start(); |
| 678 | 680 |
///\endcode |
| 679 | 681 |
void run(Node s) {
|
| 680 | 682 |
init(); |
| 681 | 683 |
addSource(s); |
| 682 | 684 |
start(); |
| 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 |
| 1068 | 1071 |
///the predecessor map. |
| 1069 | 1072 |
/// |
| 1070 | 1073 |
///\ref named-templ-param "Named parameter" function for setting |
| 1071 | 1074 |
///the map that stores the predecessor arcs of the nodes. |
| 1072 | 1075 |
template<class T> |
| 1073 | 1076 |
BfsWizard<SetPredMapBase<T> > predMap(const T &t) |
| 1074 | 1077 |
{
|
| 1075 | 1078 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
| 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. |
| 1461 | 1465 |
/// |
| 1462 | 1466 |
/// Processes the next node. |
| 1463 | 1467 |
/// |
| 1464 | 1468 |
/// \return The processed node. |
| 1465 | 1469 |
/// |
| 1466 | 1470 |
/// \pre The queue must not be empty. |
| 1467 | 1471 |
Node processNextNode() {
|
| 1468 | 1472 |
Node n = _list[++_list_front]; |
| 1469 | 1473 |
_visitor->process(n); |
| 1470 | 1474 |
Arc e; |
| 1471 | 1475 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
|
| 1472 | 1476 |
Node m = _digraph->target(e); |
| 1473 | 1477 |
if (!(*_reached)[m]) {
|
| 1474 | 1478 |
_visitor->discover(e); |
| 1475 | 1479 |
_visitor->reach(m); |
| 1476 | 1480 |
_reached->set(m, true); |
| 1477 | 1481 |
_list[++_list_back] = m; |
| 1478 | 1482 |
} else {
|
| 1479 | 1483 |
_visitor->examine(e); |
| 1480 | 1484 |
} |
| 1481 | 1485 |
} |
| 1482 | 1486 |
return n; |
| 1483 | 1487 |
} |
| 1484 | 1488 |
|
| 1485 | 1489 |
/// \brief Processes the next node. |
| 1486 | 1490 |
/// |
| 1487 | 1491 |
/// Processes the next node and checks if the given target node |
| 1488 | 1492 |
/// is reached. If the target node is reachable from the processed |
| 1489 | 1493 |
/// node, then the \c reach parameter will be set to \c true. |
| 1490 | 1494 |
/// |
| 1491 | 1495 |
/// \param target The target node. |
| 1492 | 1496 |
/// \retval reach Indicates if the target node is reached. |
| 1493 | 1497 |
/// It should be initially \c false. |
| 1494 | 1498 |
/// |
| 1495 | 1499 |
/// \return The processed node. |
| 1496 | 1500 |
/// |
| 1497 | 1501 |
/// \pre The queue must not be empty. |
| 1498 | 1502 |
Node processNextNode(Node target, bool& reach) {
|
| 1499 | 1503 |
Node n = _list[++_list_front]; |
| 1500 | 1504 |
_visitor->process(n); |
| 1501 | 1505 |
Arc e; |
| 1502 | 1506 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
|
| 1503 | 1507 |
Node m = _digraph->target(e); |
| 1504 | 1508 |
if (!(*_reached)[m]) {
|
| 1505 | 1509 |
_visitor->discover(e); |
| 1506 | 1510 |
_visitor->reach(m); |
| 1507 | 1511 |
_reached->set(m, true); |
| 1508 | 1512 |
_list[++_list_back] = m; |
| 1509 | 1513 |
reach = reach || (target == m); |
| 1510 | 1514 |
} else {
|
| 1511 | 1515 |
_visitor->examine(e); |
| 1512 | 1516 |
} |
| 1513 | 1517 |
} |
| 1514 | 1518 |
return n; |
| 1515 | 1519 |
} |
| 1516 | 1520 |
|
| 1517 | 1521 |
/// \brief Processes the next node. |
| 1518 | 1522 |
/// |
| 1519 | 1523 |
/// Processes the next node and checks if at least one of reached |
| 1520 | 1524 |
/// nodes has \c true value in the \c nm node map. If one node |
| 1521 | 1525 |
/// with \c true value is reachable from the processed node, then the |
| 1522 | 1526 |
/// \c rnode parameter will be set to the first of such nodes. |
| 1523 | 1527 |
/// |
| 1524 | 1528 |
/// \param nm A \c bool (or convertible) node map that indicates the |
| 1525 | 1529 |
/// possible targets. |
| 1526 | 1530 |
/// \retval rnode The reached target node. |
| 1527 | 1531 |
/// It should be initially \c INVALID. |
| 1528 | 1532 |
/// |
| 1529 | 1533 |
/// \return The processed node. |
| 1530 | 1534 |
/// |
| 1531 | 1535 |
/// \pre The queue must not be empty. |
| 1532 | 1536 |
template <typename NM> |
| 1533 | 1537 |
Node processNextNode(const NM& nm, Node& rnode) {
|
| 1534 | 1538 |
Node n = _list[++_list_front]; |
| 1535 | 1539 |
_visitor->process(n); |
| 1536 | 1540 |
Arc e; |
| 1537 | 1541 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
|
| 1538 | 1542 |
Node m = _digraph->target(e); |
| 1539 | 1543 |
if (!(*_reached)[m]) {
|
| 1540 | 1544 |
_visitor->discover(e); |
| 1541 | 1545 |
_visitor->reach(m); |
| 1542 | 1546 |
_reached->set(m, true); |
| 1543 | 1547 |
_list[++_list_back] = m; |
| 1544 | 1548 |
if (nm[m] && rnode == INVALID) rnode = m; |
| 1545 | 1549 |
} else {
|
| 1546 | 1550 |
_visitor->examine(e); |
| 1547 | 1551 |
} |
| 1548 | 1552 |
} |
| 1549 | 1553 |
return n; |
| 1550 | 1554 |
} |
| 1551 | 1555 |
|
| 1552 | 1556 |
/// \brief The next node to be processed. |
| 1553 | 1557 |
/// |
| 1554 | 1558 |
/// Returns the next node to be processed or \c INVALID if the queue |
| 1555 | 1559 |
/// is empty. |
| 1556 | 1560 |
Node nextNode() const {
|
| 1557 | 1561 |
return _list_front != _list_back ? _list[_list_front + 1] : INVALID; |
| 1558 | 1562 |
} |
| 1559 | 1563 |
|
| 1560 | 1564 |
/// \brief Returns \c false if there are nodes |
| 1561 | 1565 |
/// to be processed. |
| 1562 | 1566 |
/// |
| 1563 | 1567 |
/// Returns \c false if there are nodes |
| 1564 | 1568 |
/// to be processed in the queue. |
| 1565 | 1569 |
bool emptyQueue() const { return _list_front == _list_back; }
|
| 1566 | 1570 |
|
| 1567 | 1571 |
/// \brief Returns the number of the nodes to be processed. |
| 1568 | 1572 |
/// |
| 1569 | 1573 |
/// Returns the number of the nodes to be processed in the queue. |
| 1570 | 1574 |
int queueSize() const { return _list_back - _list_front; }
|
| 1571 | 1575 |
|
| 1572 | 1576 |
/// \brief Executes the algorithm. |
| 1573 | 1577 |
/// |
| 1574 | 1578 |
/// Executes the algorithm. |
| 1575 | 1579 |
/// |
| 1576 | 1580 |
/// This method runs the %BFS algorithm from the root node(s) |
| 1577 | 1581 |
/// in order to compute the shortest path to each node. |
| 1578 | 1582 |
/// |
| 1579 | 1583 |
/// The algorithm computes |
| 1580 | 1584 |
/// - the shortest path tree (forest), |
| 1581 | 1585 |
/// - the distance of each node from the root(s). |
| 1582 | 1586 |
/// |
| 1583 | 1587 |
/// \pre init() must be called and at least one root node should be added |
| 1584 | 1588 |
/// with addSource() before using this function. |
| 1585 | 1589 |
/// |
| 1586 | 1590 |
/// \note <tt>b.start()</tt> is just a shortcut of the following code. |
| 1587 | 1591 |
/// \code |
| 1588 | 1592 |
/// while ( !b.emptyQueue() ) {
|
| 1589 | 1593 |
/// b.processNextNode(); |
| 1590 | 1594 |
/// } |
| 1591 | 1595 |
/// \endcode |
| 1592 | 1596 |
void start() {
|
| 1593 | 1597 |
while ( !emptyQueue() ) processNextNode(); |
| 1594 | 1598 |
} |
| 1595 | 1599 |
|
| 1596 | 1600 |
/// \brief Executes the algorithm until the given target node is reached. |
| 1597 | 1601 |
/// |
| 1598 | 1602 |
/// Executes the algorithm until the given target node is reached. |
| 1599 | 1603 |
/// |
| 1600 | 1604 |
/// This method runs the %BFS algorithm from the root node(s) |
| 1601 | 1605 |
/// in order to compute the shortest path to \c t. |
| 1602 | 1606 |
/// |
| 1603 | 1607 |
/// The algorithm computes |
| 1604 | 1608 |
/// - the shortest path to \c t, |
| 1605 | 1609 |
/// - the distance of \c t from the root(s). |
| 1606 | 1610 |
/// |
| 1607 | 1611 |
/// \pre init() must be called and at least one root node should be |
| 1608 | 1612 |
/// added with addSource() before using this function. |
| 1609 | 1613 |
/// |
| 1610 | 1614 |
/// \note <tt>b.start(t)</tt> is just a shortcut of the following code. |
| 1611 | 1615 |
/// \code |
| 1612 | 1616 |
/// bool reach = false; |
| 1613 | 1617 |
/// while ( !b.emptyQueue() && !reach ) {
|
| 1614 | 1618 |
/// b.processNextNode(t, reach); |
| 1615 | 1619 |
/// } |
| 1616 | 1620 |
/// \endcode |
| 1617 | 1621 |
void start(Node t) {
|
| 1618 | 1622 |
bool reach = false; |
| 1619 | 1623 |
while ( !emptyQueue() && !reach ) processNextNode(t, reach); |
| 1620 | 1624 |
} |
| 1621 | 1625 |
|
| 1622 | 1626 |
/// \brief Executes the algorithm until a condition is met. |
| 1623 | 1627 |
/// |
| 1624 | 1628 |
/// Executes the algorithm until a condition is met. |
| 1625 | 1629 |
/// |
| 1626 | 1630 |
/// This method runs the %BFS algorithm from the root node(s) in |
| 1627 | 1631 |
/// order to compute the shortest path to a node \c v with |
| 1628 | 1632 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
| 1629 | 1633 |
/// |
| 1630 | 1634 |
/// \param nm must be a bool (or convertible) node map. The |
| 1631 | 1635 |
/// algorithm will stop when it reaches a node \c v with |
| 1632 | 1636 |
/// <tt>nm[v]</tt> true. |
| 1633 | 1637 |
/// |
| 1634 | 1638 |
/// \return The reached node \c v with <tt>nm[v]</tt> true or |
| 1635 | 1639 |
/// \c INVALID if no such node was found. |
| 1636 | 1640 |
/// |
| 1637 | 1641 |
/// \pre init() must be called and at least one root node should be |
| 1638 | 1642 |
/// added with addSource() before using this function. |
| 1639 | 1643 |
/// |
| 1640 | 1644 |
/// \note <tt>b.start(nm)</tt> is just a shortcut of the following code. |
| 1641 | 1645 |
/// \code |
| 1642 | 1646 |
/// Node rnode = INVALID; |
| 1643 | 1647 |
/// while ( !b.emptyQueue() && rnode == INVALID ) {
|
| 1644 | 1648 |
/// b.processNextNode(nm, rnode); |
| 1645 | 1649 |
/// } |
| 1646 | 1650 |
/// return rnode; |
| 1647 | 1651 |
/// \endcode |
| 1648 | 1652 |
template <typename NM> |
| 1649 | 1653 |
Node start(const NM &nm) {
|
| 1650 | 1654 |
Node rnode = INVALID; |
| 1651 | 1655 |
while ( !emptyQueue() && rnode == INVALID ) {
|
| 1652 | 1656 |
processNextNode(nm, rnode); |
| 1653 | 1657 |
} |
| 1654 | 1658 |
return rnode; |
| 1655 | 1659 |
} |
| 1656 | 1660 |
|
| 1657 | 1661 |
/// \brief Runs the algorithm from the given source node. |
| 1658 | 1662 |
/// |
| 1659 | 1663 |
/// This method runs the %BFS algorithm from node \c s |
| 1660 | 1664 |
/// in order to compute the shortest path to each node. |
| 1661 | 1665 |
/// |
| 1662 | 1666 |
/// The algorithm computes |
| 1663 | 1667 |
/// - the shortest path tree, |
| 1664 | 1668 |
/// - the distance of each node from the root. |
| 1665 | 1669 |
/// |
| 1666 | 1670 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
| 1667 | 1671 |
///\code |
| 1668 | 1672 |
/// b.init(); |
| 1669 | 1673 |
/// b.addSource(s); |
| 1670 | 1674 |
/// b.start(); |
| 1671 | 1675 |
///\endcode |
| 1672 | 1676 |
void run(Node s) {
|
| 1673 | 1677 |
init(); |
| 1674 | 1678 |
addSource(s); |
| 1675 | 1679 |
start(); |
| 1676 | 1680 |
} |
| 1677 | 1681 |
|
| 1678 | 1682 |
/// \brief Finds the shortest path between \c s and \c t. |
| 1679 | 1683 |
/// |
| 1680 | 1684 |
/// This method runs the %BFS algorithm from node \c s |
| 1681 | 1685 |
/// in order to compute the shortest path to node \c t |
| 1682 | 1686 |
/// (it stops searching when \c t is processed). |
| 1683 | 1687 |
/// |
| 1684 | 1688 |
/// \return \c true if \c t is reachable form \c s. |
| 1685 | 1689 |
/// |
| 1686 | 1690 |
/// \note Apart from the return value, <tt>b.run(s,t)</tt> is just a |
| 1687 | 1691 |
/// shortcut of the following code. |
| 1688 | 1692 |
///\code |
| 1689 | 1693 |
/// b.init(); |
| 1690 | 1694 |
/// b.addSource(s); |
| 1691 | 1695 |
/// b.start(t); |
| 1692 | 1696 |
///\endcode |
| 1693 | 1697 |
bool run(Node s,Node t) {
|
| 1694 | 1698 |
init(); |
| 1695 | 1699 |
addSource(s); |
| 1696 | 1700 |
start(t); |
| 1697 | 1701 |
return reached(t); |
| 1698 | 1702 |
} |
| 1699 | 1703 |
|
| 1700 | 1704 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
| 1701 | 1705 |
/// |
| 1702 | 1706 |
/// This method runs the %BFS algorithm in order to visit all nodes |
| 1703 | 1707 |
/// in the digraph. |
| 1704 | 1708 |
/// |
| 1705 | 1709 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
| 1706 | 1710 |
///\code |
| 1707 | 1711 |
/// b.init(); |
| 1708 | 1712 |
/// for (NodeIt n(gr); n != INVALID; ++n) {
|
| 1709 | 1713 |
/// if (!b.reached(n)) {
|
| 1710 | 1714 |
/// b.addSource(n); |
| 1711 | 1715 |
/// b.start(); |
| 1712 | 1716 |
/// } |
| 1713 | 1717 |
/// } |
| 1714 | 1718 |
///\endcode |
| 1715 | 1719 |
void run() {
|
| 1716 | 1720 |
init(); |
| 1717 | 1721 |
for (NodeIt it(*_digraph); it != INVALID; ++it) {
|
| 1718 | 1722 |
if (!reached(it)) {
|
| 1719 | 1723 |
addSource(it); |
| 1720 | 1724 |
start(); |
| 1721 | 1725 |
} |
| 1722 | 1726 |
} |
| 1723 | 1727 |
} |
| 1724 | 1728 |
|
| 1725 | 1729 |
///@} |
| 1726 | 1730 |
|
| 1727 | 1731 |
/// \name Query Functions |
| 1728 | 1732 |
/// The results of the BFS algorithm can be obtained using these |
| 1729 | 1733 |
/// functions.\n |
| 1730 | 1734 |
/// Either \ref run(Node) "run()" or \ref start() should be called |
| 1731 | 1735 |
/// before using them. |
| 1732 | 1736 |
|
| 1733 | 1737 |
///@{
|
| 1734 | 1738 |
|
| 1735 | 1739 |
/// \brief Checks if the given node is reached from the root(s). |
| 1736 | 1740 |
/// |
| 1737 | 1741 |
/// Returns \c true if \c v is reached from the root(s). |
| 1738 | 1742 |
/// |
| 1739 | 1743 |
/// \pre Either \ref run(Node) "run()" or \ref init() |
| 1740 | 1744 |
/// must be called before using this function. |
| 1741 | 1745 |
bool reached(Node v) const { return (*_reached)[v]; }
|
| 1742 | 1746 |
|
| 1743 | 1747 |
///@} |
| 1744 | 1748 |
|
| 1745 | 1749 |
}; |
| 1746 | 1750 |
|
| 1747 | 1751 |
} //END OF NAMESPACE LEMON |
| 1748 | 1752 |
|
| 1749 | 1753 |
#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_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. |
| 198 | 198 |
/// \pre \e item must be in the heap. |
| 199 | 199 |
const Prio& operator[](const Item& item) const {
|
| 200 | 200 |
return _data[_iim[item]].prio; |
| 201 | 201 |
} |
| 202 | 202 |
|
| 203 | 203 |
/// \brief Remove the item having minimum priority. |
| 204 | 204 |
/// |
| 205 | 205 |
/// This function removes the item having minimum priority. |
| 206 | 206 |
/// \pre The heap must be non-empty. |
| 207 | 207 |
void pop() {
|
| 208 | 208 |
_data[_min].in=false; |
| 209 | 209 |
|
| 210 | 210 |
int head_child=-1; |
| 211 | 211 |
if ( _data[_min].child!=-1 ) {
|
| 212 | 212 |
int child=_data[_min].child; |
| 213 | 213 |
int neighb; |
| 214 | 214 |
while( child!=-1 ) {
|
| 215 | 215 |
neighb=_data[child].right_neighbor; |
| 216 | 216 |
_data[child].parent=-1; |
| 217 | 217 |
_data[child].right_neighbor=head_child; |
| 218 | 218 |
head_child=child; |
| 219 | 219 |
child=neighb; |
| 220 | 220 |
} |
| 221 | 221 |
} |
| 222 | 222 |
|
| 223 | 223 |
if ( _data[_head].right_neighbor==-1 ) {
|
| 224 | 224 |
// there was only one root |
| 225 | 225 |
_head=head_child; |
| 226 | 226 |
} |
| 227 | 227 |
else {
|
| 228 | 228 |
// there were more roots |
| 229 | 229 |
if( _head!=_min ) { unlace(_min); }
|
| 230 | 230 |
else { _head=_data[_head].right_neighbor; }
|
| 231 | 231 |
merge(head_child); |
| 232 | 232 |
} |
| 233 | 233 |
_min=findMin(); |
| 234 | 234 |
--_num_items; |
| 235 | 235 |
} |
| 236 | 236 |
|
| 237 | 237 |
/// \brief Remove the given item from the heap. |
| 238 | 238 |
/// |
| 239 | 239 |
/// This function removes the given item from the heap if it is |
| 240 | 240 |
/// already stored. |
| 241 | 241 |
/// \param item The item to delete. |
| 242 | 242 |
/// \pre \e item must be in the heap. |
| 243 | 243 |
void erase (const Item& item) {
|
| 244 | 244 |
int i=_iim[item]; |
| 245 | 245 |
if ( i >= 0 && _data[i].in ) {
|
| 246 | 246 |
decrease( item, _data[_min].prio-1 ); |
| 247 | 247 |
pop(); |
| 248 | 248 |
} |
| 249 | 249 |
} |
| 250 | 250 |
|
| 251 | 251 |
/// \brief Decrease the priority of an item to the given value. |
| 252 | 252 |
/// |
| 253 | 253 |
/// This function decreases the priority of an item to the given value. |
| 254 | 254 |
/// \param item The item. |
| 255 | 255 |
/// \param value The priority. |
| 256 | 256 |
/// \pre \e item must be stored in the heap with priority at least \e value. |
| 257 | 257 |
void decrease (Item item, const Prio& value) {
|
| 258 | 258 |
int i=_iim[item]; |
| 259 | 259 |
int p=_data[i].parent; |
| 260 | 260 |
_data[i].prio=value; |
| 261 | 261 |
|
| 262 | 262 |
while( p!=-1 && _comp(value, _data[p].prio) ) {
|
| 263 | 263 |
_data[i].name=_data[p].name; |
| 264 | 264 |
_data[i].prio=_data[p].prio; |
| 265 | 265 |
_data[p].name=item; |
| 266 | 266 |
_data[p].prio=value; |
| 267 | 267 |
_iim[_data[i].name]=i; |
| 268 | 268 |
i=p; |
| 269 | 269 |
p=_data[p].parent; |
| 270 | 270 |
} |
| 271 | 271 |
_iim[item]=i; |
| 272 | 272 |
if ( _comp(value, _data[_min].prio) ) _min=i; |
| 273 | 273 |
} |
| 274 | 274 |
|
| 275 | 275 |
/// \brief Increase the priority of an item to the given value. |
| 276 | 276 |
/// |
| 277 | 277 |
/// This function increases the priority of an item to the given value. |
| 278 | 278 |
/// \param item The item. |
| 279 | 279 |
/// \param value The priority. |
| 280 | 280 |
/// \pre \e item must be stored in the heap with priority at most \e value. |
| 281 | 281 |
void increase (Item item, const Prio& value) {
|
| 282 | 282 |
erase(item); |
| 283 | 283 |
push(item, value); |
| 284 | 284 |
} |
| 285 | 285 |
|
| 286 | 286 |
/// \brief Return the state of an item. |
| 287 | 287 |
/// |
| 288 | 288 |
/// This method returns \c PRE_HEAP if the given item has never |
| 289 | 289 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
| 290 | 290 |
/// and \c POST_HEAP otherwise. |
| 291 | 291 |
/// In the latter case it is possible that the item will get back |
| 292 | 292 |
/// to the heap again. |
| 293 | 293 |
/// \param item The item. |
| 294 | 294 |
State state(const Item &item) const {
|
| 295 | 295 |
int i=_iim[item]; |
| 296 | 296 |
if( i>=0 ) {
|
| 297 | 297 |
if ( _data[i].in ) i=0; |
| 298 | 298 |
else i=-2; |
| 299 | 299 |
} |
| 300 | 300 |
return State(i); |
| 301 | 301 |
} |
| 302 | 302 |
|
| 303 | 303 |
/// \brief Set the state of an item in the heap. |
| 304 | 304 |
/// |
| 305 | 305 |
/// This function sets the state of the given item in the heap. |
| 306 | 306 |
/// It can be used to manually clear the heap when it is important |
| 307 | 307 |
/// to achive better time complexity. |
| 308 | 308 |
/// \param i The item. |
| 309 | 309 |
/// \param st The state. It should not be \c IN_HEAP. |
| 310 | 310 |
void state(const Item& i, State st) {
|
| 311 | 311 |
switch (st) {
|
| 312 | 312 |
case POST_HEAP: |
| 313 | 313 |
case PRE_HEAP: |
| 314 | 314 |
if (state(i) == IN_HEAP) {
|
| 315 | 315 |
erase(i); |
| 316 | 316 |
} |
| 317 | 317 |
_iim[i] = st; |
| 318 | 318 |
break; |
| 319 | 319 |
case IN_HEAP: |
| 320 | 320 |
break; |
| 321 | 321 |
} |
| 322 | 322 |
} |
| 323 | 323 |
|
| 324 | 324 |
private: |
| 325 | 325 |
|
| 326 | 326 |
// Find the minimum of the roots |
| 327 | 327 |
int findMin() {
|
| 328 | 328 |
if( _head!=-1 ) {
|
| 329 | 329 |
int min_loc=_head, min_val=_data[_head].prio; |
| 330 | 330 |
for( int x=_data[_head].right_neighbor; x!=-1; |
| 331 | 331 |
x=_data[x].right_neighbor ) {
|
| 332 | 332 |
if( _comp( _data[x].prio,min_val ) ) {
|
| 333 | 333 |
min_val=_data[x].prio; |
| 334 | 334 |
min_loc=x; |
| 335 | 335 |
} |
| 336 | 336 |
} |
| 337 | 337 |
return min_loc; |
| 338 | 338 |
} |
| 339 | 339 |
else return -1; |
| 340 | 340 |
} |
| 341 | 341 |
|
| 342 | 342 |
// Merge the heap with another heap starting at the given position |
| 343 | 343 |
void merge(int a) {
|
| 344 | 344 |
if( _head==-1 || a==-1 ) return; |
| 345 | 345 |
if( _data[a].right_neighbor==-1 && |
| 346 | 346 |
_data[a].degree<=_data[_head].degree ) {
|
| 347 | 347 |
_data[a].right_neighbor=_head; |
| 348 | 348 |
_head=a; |
| 349 | 349 |
} else {
|
| 350 | 350 |
interleave(a); |
| 351 | 351 |
} |
| 352 | 352 |
if( _data[_head].right_neighbor==-1 ) return; |
| 353 | 353 |
|
| 354 | 354 |
int x=_head; |
| 355 | 355 |
int x_prev=-1, x_next=_data[x].right_neighbor; |
| 356 | 356 |
while( x_next!=-1 ) {
|
| 357 | 357 |
if( _data[x].degree!=_data[x_next].degree || |
| 358 | 358 |
( _data[x_next].right_neighbor!=-1 && |
| 359 | 359 |
_data[_data[x_next].right_neighbor].degree==_data[x].degree ) ) {
|
| 360 | 360 |
x_prev=x; |
| 361 | 361 |
x=x_next; |
| 362 | 362 |
} |
| 363 | 363 |
else {
|
| 364 | 364 |
if( _comp(_data[x_next].prio,_data[x].prio) ) {
|
| 365 | 365 |
if( x_prev==-1 ) {
|
| 366 | 366 |
_head=x_next; |
| 367 | 367 |
} else {
|
| 368 | 368 |
_data[x_prev].right_neighbor=x_next; |
| 369 | 369 |
} |
| 370 | 370 |
fuse(x,x_next); |
| 371 | 371 |
x=x_next; |
| 372 | 372 |
} |
| 373 | 373 |
else {
|
| 374 | 374 |
_data[x].right_neighbor=_data[x_next].right_neighbor; |
| 375 | 375 |
fuse(x_next,x); |
| 376 | 376 |
} |
| 377 | 377 |
} |
| 378 | 378 |
x_next=_data[x].right_neighbor; |
| 379 | 379 |
} |
| 380 | 380 |
} |
| 381 | 381 |
|
| 382 | 382 |
// Interleave the elements of the given list into the list of the roots |
| 383 | 383 |
void interleave(int a) {
|
| 384 | 384 |
int p=_head, q=a; |
| 385 | 385 |
int curr=_data.size(); |
| 386 | 386 |
_data.push_back(Store()); |
| 387 | 387 |
|
| 388 | 388 |
while( p!=-1 || q!=-1 ) {
|
| 389 | 389 |
if( q==-1 || ( p!=-1 && _data[p].degree<_data[q].degree ) ) {
|
| 390 | 390 |
_data[curr].right_neighbor=p; |
| 391 | 391 |
curr=p; |
| 392 | 392 |
p=_data[p].right_neighbor; |
| 393 | 393 |
} |
| 394 | 394 |
else {
|
| 395 | 395 |
_data[curr].right_neighbor=q; |
| 396 | 396 |
curr=q; |
| 397 | 397 |
q=_data[q].right_neighbor; |
| 398 | 398 |
} |
| 399 | 399 |
} |
| 400 | 400 |
|
| 401 | 401 |
_head=_data.back().right_neighbor; |
| 402 | 402 |
_data.pop_back(); |
| 403 | 403 |
} |
| 404 | 404 |
|
| 405 | 405 |
// Lace node a under node b |
| 406 | 406 |
void fuse(int a, int b) {
|
| 407 | 407 |
_data[a].parent=b; |
| 408 | 408 |
_data[a].right_neighbor=_data[b].child; |
| 409 | 409 |
_data[b].child=a; |
| 410 | 410 |
|
| 411 | 411 |
++_data[b].degree; |
| 412 | 412 |
} |
| 413 | 413 |
|
| 414 | 414 |
// Unlace node a (if it has siblings) |
| 415 | 415 |
void unlace(int a) {
|
| 416 | 416 |
int neighb=_data[a].right_neighbor; |
| 417 | 417 |
int other=_head; |
| 418 | 418 |
|
| 419 | 419 |
while( _data[other].right_neighbor!=a ) |
| 420 | 420 |
other=_data[other].right_neighbor; |
| 421 | 421 |
_data[other].right_neighbor=neighb; |
| 422 | 422 |
} |
| 423 | 423 |
|
| 424 | 424 |
private: |
| 425 | 425 |
|
| 426 | 426 |
class Store {
|
| 427 | 427 |
friend class BinomialHeap; |
| 428 | 428 |
|
| 429 | 429 |
Item name; |
| 430 | 430 |
int parent; |
| 431 | 431 |
int right_neighbor; |
| 432 | 432 |
int child; |
| 433 | 433 |
int degree; |
| 434 | 434 |
bool in; |
| 435 | 435 |
Prio prio; |
| 436 | 436 |
|
| 437 | 437 |
Store() : parent(-1), right_neighbor(-1), child(-1), degree(0), |
| 438 | 438 |
in(true) {}
|
| 439 | 439 |
}; |
| 440 | 440 |
}; |
| 441 | 441 |
|
| 442 | 442 |
} //namespace lemon |
| 443 | 443 |
|
| 444 | 444 |
#endif //LEMON_BINOMIAL_HEAP_H |
| 445 | 445 |
| 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. |
| 198 | 198 |
void set(const Key& key, const Value& val) {
|
| 199 | 199 |
(*this)[key] = val; |
| 200 | 200 |
} |
| 201 | 201 |
|
| 202 | 202 |
protected: |
| 203 | 203 |
|
| 204 | 204 |
// \brief Adds a new key to the map. |
| 205 | 205 |
// |
| 206 | 206 |
// It adds a new key to the map. It is called by the observer notifier |
| 207 | 207 |
// and it overrides the add() member function of the observer base. |
| 208 | 208 |
virtual void add(const Key& key) {
|
| 209 | 209 |
Notifier* nf = Parent::notifier(); |
| 210 | 210 |
int id = nf->id(key); |
| 211 | 211 |
if (id >= capacity) {
|
| 212 | 212 |
int new_capacity = (capacity == 0 ? 1 : capacity); |
| 213 | 213 |
while (new_capacity <= id) {
|
| 214 | 214 |
new_capacity <<= 1; |
| 215 | 215 |
} |
| 216 | 216 |
Value* new_values = allocator.allocate(new_capacity); |
| 217 | 217 |
Item it; |
| 218 | 218 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 219 | 219 |
int jd = nf->id(it);; |
| 220 | 220 |
if (id != jd) {
|
| 221 | 221 |
allocator.construct(&(new_values[jd]), values[jd]); |
| 222 | 222 |
allocator.destroy(&(values[jd])); |
| 223 | 223 |
} |
| 224 | 224 |
} |
| 225 | 225 |
if (capacity != 0) allocator.deallocate(values, capacity); |
| 226 | 226 |
values = new_values; |
| 227 | 227 |
capacity = new_capacity; |
| 228 | 228 |
} |
| 229 | 229 |
allocator.construct(&(values[id]), Value()); |
| 230 | 230 |
} |
| 231 | 231 |
|
| 232 | 232 |
// \brief Adds more new keys to the map. |
| 233 | 233 |
// |
| 234 | 234 |
// It adds more new keys to the map. It is called by the observer notifier |
| 235 | 235 |
// and it overrides the add() member function of the observer base. |
| 236 | 236 |
virtual void add(const std::vector<Key>& keys) {
|
| 237 | 237 |
Notifier* nf = Parent::notifier(); |
| 238 | 238 |
int max_id = -1; |
| 239 | 239 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 240 | 240 |
int id = nf->id(keys[i]); |
| 241 | 241 |
if (id > max_id) {
|
| 242 | 242 |
max_id = id; |
| 243 | 243 |
} |
| 244 | 244 |
} |
| 245 | 245 |
if (max_id >= capacity) {
|
| 246 | 246 |
int new_capacity = (capacity == 0 ? 1 : capacity); |
| 247 | 247 |
while (new_capacity <= max_id) {
|
| 248 | 248 |
new_capacity <<= 1; |
| 249 | 249 |
} |
| 250 | 250 |
Value* new_values = allocator.allocate(new_capacity); |
| 251 | 251 |
Item it; |
| 252 | 252 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 253 | 253 |
int id = nf->id(it); |
| 254 | 254 |
bool found = false; |
| 255 | 255 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 256 | 256 |
int jd = nf->id(keys[i]); |
| 257 | 257 |
if (id == jd) {
|
| 258 | 258 |
found = true; |
| 259 | 259 |
break; |
| 260 | 260 |
} |
| 261 | 261 |
} |
| 262 | 262 |
if (found) continue; |
| 263 | 263 |
allocator.construct(&(new_values[id]), values[id]); |
| 264 | 264 |
allocator.destroy(&(values[id])); |
| 265 | 265 |
} |
| 266 | 266 |
if (capacity != 0) allocator.deallocate(values, capacity); |
| 267 | 267 |
values = new_values; |
| 268 | 268 |
capacity = new_capacity; |
| 269 | 269 |
} |
| 270 | 270 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 271 | 271 |
int id = nf->id(keys[i]); |
| 272 | 272 |
allocator.construct(&(values[id]), Value()); |
| 273 | 273 |
} |
| 274 | 274 |
} |
| 275 | 275 |
|
| 276 | 276 |
// \brief Erase a key from the map. |
| 277 | 277 |
// |
| 278 | 278 |
// Erase a key from the map. It is called by the observer notifier |
| 279 | 279 |
// and it overrides the erase() member function of the observer base. |
| 280 | 280 |
virtual void erase(const Key& key) {
|
| 281 | 281 |
int id = Parent::notifier()->id(key); |
| 282 | 282 |
allocator.destroy(&(values[id])); |
| 283 | 283 |
} |
| 284 | 284 |
|
| 285 | 285 |
// \brief Erase more keys from the map. |
| 286 | 286 |
// |
| 287 | 287 |
// Erase more keys from the map. It is called by the observer notifier |
| 288 | 288 |
// and it overrides the erase() member function of the observer base. |
| 289 | 289 |
virtual void erase(const std::vector<Key>& keys) {
|
| 290 | 290 |
for (int i = 0; i < int(keys.size()); ++i) {
|
| 291 | 291 |
int id = Parent::notifier()->id(keys[i]); |
| 292 | 292 |
allocator.destroy(&(values[id])); |
| 293 | 293 |
} |
| 294 | 294 |
} |
| 295 | 295 |
|
| 296 | 296 |
// \brief Builds the map. |
| 297 | 297 |
// |
| 298 | 298 |
// It builds the map. It is called by the observer notifier |
| 299 | 299 |
// and it overrides the build() member function of the observer base. |
| 300 | 300 |
virtual void build() {
|
| 301 | 301 |
Notifier* nf = Parent::notifier(); |
| 302 | 302 |
allocate_memory(); |
| 303 | 303 |
Item it; |
| 304 | 304 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 305 | 305 |
int id = nf->id(it);; |
| 306 | 306 |
allocator.construct(&(values[id]), Value()); |
| 307 | 307 |
} |
| 308 | 308 |
} |
| 309 | 309 |
|
| 310 | 310 |
// \brief Clear the map. |
| 311 | 311 |
// |
| 312 | 312 |
// It erase all items from the map. It is called by the observer notifier |
| 313 | 313 |
// and it overrides the clear() member function of the observer base. |
| 314 | 314 |
virtual void clear() {
|
| 315 | 315 |
Notifier* nf = Parent::notifier(); |
| 316 | 316 |
if (capacity != 0) {
|
| 317 | 317 |
Item it; |
| 318 | 318 |
for (nf->first(it); it != INVALID; nf->next(it)) {
|
| 319 | 319 |
int id = nf->id(it); |
| 320 | 320 |
allocator.destroy(&(values[id])); |
| 321 | 321 |
} |
| 322 | 322 |
allocator.deallocate(values, capacity); |
| 323 | 323 |
capacity = 0; |
| 324 | 324 |
} |
| 325 | 325 |
} |
| 326 | 326 |
|
| 327 | 327 |
private: |
| 328 | 328 |
|
| 329 | 329 |
void allocate_memory() {
|
| 330 | 330 |
int max_id = Parent::notifier()->maxId(); |
| 331 | 331 |
if (max_id == -1) {
|
| 332 | 332 |
capacity = 0; |
| 333 | 333 |
values = 0; |
| 334 | 334 |
return; |
| 335 | 335 |
} |
| 336 | 336 |
capacity = 1; |
| 337 | 337 |
while (capacity <= max_id) {
|
| 338 | 338 |
capacity <<= 1; |
| 339 | 339 |
} |
| 340 | 340 |
values = allocator.allocate(capacity); |
| 341 | 341 |
} |
| 342 | 342 |
|
| 343 | 343 |
int capacity; |
| 344 | 344 |
Value* values; |
| 345 | 345 |
Allocator allocator; |
| 346 | 346 |
|
| 347 | 347 |
}; |
| 348 | 348 |
|
| 349 | 349 |
} |
| 350 | 350 |
|
| 351 | 351 |
#endif |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_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 {
|
| 198 | 198 |
return Parent::target(static_cast<const Arc&>(e)); |
| 199 | 199 |
} |
| 200 | 200 |
|
| 201 | 201 |
// \brief Base node of the iterator |
| 202 | 202 |
// |
| 203 | 203 |
// Returns the base node (ie. the target in this case) of the iterator |
| 204 | 204 |
Node baseNode(const InArcIt &e) const {
|
| 205 | 205 |
return Parent::target(static_cast<const Arc&>(e)); |
| 206 | 206 |
} |
| 207 | 207 |
// \brief Running node of the iterator |
| 208 | 208 |
// |
| 209 | 209 |
// Returns the running node (ie. the source in this case) of the |
| 210 | 210 |
// iterator |
| 211 | 211 |
Node runningNode(const InArcIt &e) const {
|
| 212 | 212 |
return Parent::source(static_cast<const Arc&>(e)); |
| 213 | 213 |
} |
| 214 | 214 |
|
| 215 | 215 |
using Parent::first; |
| 216 | 216 |
|
| 217 | 217 |
// Mappable extension |
| 218 | 218 |
|
| 219 | 219 |
template <typename _Value> |
| 220 | 220 |
class ArcMap |
| 221 | 221 |
: public MapExtender<DefaultMap<Digraph, Arc, _Value> > {
|
| 222 | 222 |
typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent; |
| 223 | 223 |
|
| 224 | 224 |
public: |
| 225 | 225 |
explicit ArcMap(const Digraph& _g) |
| 226 | 226 |
: Parent(_g) {}
|
| 227 | 227 |
ArcMap(const Digraph& _g, const _Value& _v) |
| 228 | 228 |
: Parent(_g, _v) {}
|
| 229 | 229 |
|
| 230 | 230 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 231 | 231 |
return operator=<ArcMap>(cmap); |
| 232 | 232 |
} |
| 233 | 233 |
|
| 234 | 234 |
template <typename CMap> |
| 235 | 235 |
ArcMap& operator=(const CMap& cmap) {
|
| 236 | 236 |
Parent::operator=(cmap); |
| 237 | 237 |
return *this; |
| 238 | 238 |
} |
| 239 | 239 |
|
| 240 | 240 |
}; |
| 241 | 241 |
|
| 242 | 242 |
|
| 243 | 243 |
// Alteration extension |
| 244 | 244 |
|
| 245 | 245 |
Arc addArc(const Node& from, const Node& to) {
|
| 246 | 246 |
Arc arc = Parent::addArc(from, to); |
| 247 | 247 |
notifier(Arc()).add(arc); |
| 248 | 248 |
return arc; |
| 249 | 249 |
} |
| 250 | 250 |
|
| 251 | 251 |
void clear() {
|
| 252 | 252 |
notifier(Arc()).clear(); |
| 253 | 253 |
Parent::clear(); |
| 254 | 254 |
} |
| 255 | 255 |
|
| 256 | 256 |
void erase(const Arc& arc) {
|
| 257 | 257 |
notifier(Arc()).erase(arc); |
| 258 | 258 |
Parent::erase(arc); |
| 259 | 259 |
} |
| 260 | 260 |
|
| 261 | 261 |
ArcSetExtender() {
|
| 262 | 262 |
arc_notifier.setContainer(*this); |
| 263 | 263 |
} |
| 264 | 264 |
|
| 265 | 265 |
~ArcSetExtender() {
|
| 266 | 266 |
arc_notifier.clear(); |
| 267 | 267 |
} |
| 268 | 268 |
|
| 269 | 269 |
}; |
| 270 | 270 |
|
| 271 | 271 |
|
| 272 | 272 |
// \ingroup digraphbits |
| 273 | 273 |
// |
| 274 | 274 |
// \brief Extender for the EdgeSets |
| 275 | 275 |
template <typename Base> |
| 276 | 276 |
class EdgeSetExtender : public Base {
|
| 277 | 277 |
typedef Base Parent; |
| 278 | 278 |
|
| 279 | 279 |
public: |
| 280 | 280 |
|
| 281 | 281 |
typedef EdgeSetExtender Graph; |
| 282 | 282 |
|
| 283 | 283 |
typedef typename Parent::Node Node; |
| 284 | 284 |
typedef typename Parent::Arc Arc; |
| 285 | 285 |
typedef typename Parent::Edge Edge; |
| 286 | 286 |
|
| 287 | 287 |
int maxId(Node) const {
|
| 288 | 288 |
return Parent::maxNodeId(); |
| 289 | 289 |
} |
| 290 | 290 |
|
| 291 | 291 |
int maxId(Arc) const {
|
| 292 | 292 |
return Parent::maxArcId(); |
| 293 | 293 |
} |
| 294 | 294 |
|
| 295 | 295 |
int maxId(Edge) const {
|
| 296 | 296 |
return Parent::maxEdgeId(); |
| 297 | 297 |
} |
| 298 | 298 |
|
| 299 | 299 |
Node fromId(int id, Node) const {
|
| 300 | 300 |
return Parent::nodeFromId(id); |
| 301 | 301 |
} |
| 302 | 302 |
|
| 303 | 303 |
Arc fromId(int id, Arc) const {
|
| 304 | 304 |
return Parent::arcFromId(id); |
| 305 | 305 |
} |
| 306 | 306 |
|
| 307 | 307 |
Edge fromId(int id, Edge) const {
|
| 308 | 308 |
return Parent::edgeFromId(id); |
| 309 | 309 |
} |
| 310 | 310 |
|
| 311 | 311 |
Node oppositeNode(const Node &n, const Edge &e) const {
|
| 312 | 312 |
if( n == Parent::u(e)) |
| 313 | 313 |
return Parent::v(e); |
| 314 | 314 |
else if( n == Parent::v(e)) |
| 315 | 315 |
return Parent::u(e); |
| 316 | 316 |
else |
| 317 | 317 |
return INVALID; |
| 318 | 318 |
} |
| 319 | 319 |
|
| 320 | 320 |
Arc oppositeArc(const Arc &e) const {
|
| 321 | 321 |
return Parent::direct(e, !Parent::direction(e)); |
| 322 | 322 |
} |
| 323 | 323 |
|
| 324 | 324 |
using Parent::direct; |
| 325 | 325 |
Arc direct(const Edge &e, const Node &s) const {
|
| 326 | 326 |
return Parent::direct(e, Parent::u(e) == s); |
| 327 | 327 |
} |
| 328 | 328 |
|
| 329 | 329 |
typedef AlterationNotifier<EdgeSetExtender, Arc> ArcNotifier; |
| 330 | 330 |
typedef AlterationNotifier<EdgeSetExtender, Edge> EdgeNotifier; |
| 331 | 331 |
|
| 332 | 332 |
|
| 333 | 333 |
protected: |
| 334 | 334 |
|
| 335 | 335 |
mutable ArcNotifier arc_notifier; |
| 336 | 336 |
mutable EdgeNotifier edge_notifier; |
| 337 | 337 |
|
| 338 | 338 |
public: |
| 339 | 339 |
|
| 340 | 340 |
using Parent::notifier; |
| 341 | 341 |
|
| 342 | 342 |
ArcNotifier& notifier(Arc) const {
|
| 343 | 343 |
return arc_notifier; |
| 344 | 344 |
} |
| 345 | 345 |
|
| 346 | 346 |
EdgeNotifier& notifier(Edge) const {
|
| 347 | 347 |
return edge_notifier; |
| 348 | 348 |
} |
| 349 | 349 |
|
| 350 | 350 |
|
| 351 | 351 |
class NodeIt : public Node {
|
| 352 | 352 |
const Graph* graph; |
| 353 | 353 |
public: |
| 354 | 354 |
|
| 355 | 355 |
NodeIt() {}
|
| 356 | 356 |
|
| 357 | 357 |
NodeIt(Invalid i) : Node(i) { }
|
| 358 | 358 |
|
| 359 | 359 |
explicit NodeIt(const Graph& _graph) : graph(&_graph) {
|
| 360 | 360 |
_graph.first(static_cast<Node&>(*this)); |
| 361 | 361 |
} |
| 362 | 362 |
|
| 363 | 363 |
NodeIt(const Graph& _graph, const Node& node) |
| 364 | 364 |
: Node(node), graph(&_graph) {}
|
| 365 | 365 |
|
| 366 | 366 |
NodeIt& operator++() {
|
| 367 | 367 |
graph->next(*this); |
| 368 | 368 |
return *this; |
| 369 | 369 |
} |
| 370 | 370 |
|
| 371 | 371 |
}; |
| 372 | 372 |
|
| 373 | 373 |
|
| 374 | 374 |
class ArcIt : public Arc {
|
| 375 | 375 |
const Graph* graph; |
| 376 | 376 |
public: |
| 377 | 377 |
|
| 378 | 378 |
ArcIt() { }
|
| 379 | 379 |
|
| 380 | 380 |
ArcIt(Invalid i) : Arc(i) { }
|
| 381 | 381 |
|
| 382 | 382 |
explicit ArcIt(const Graph& _graph) : graph(&_graph) {
|
| 383 | 383 |
_graph.first(static_cast<Arc&>(*this)); |
| 384 | 384 |
} |
| 385 | 385 |
|
| 386 | 386 |
ArcIt(const Graph& _graph, const Arc& e) : |
| 387 | 387 |
Arc(e), graph(&_graph) { }
|
| 388 | 388 |
|
| 389 | 389 |
ArcIt& operator++() {
|
| 390 | 390 |
graph->next(*this); |
| 391 | 391 |
return *this; |
| 392 | 392 |
} |
| 393 | 393 |
|
| 394 | 394 |
}; |
| 395 | 395 |
|
| 396 | 396 |
|
| 397 | 397 |
class OutArcIt : public Arc {
|
| 398 | 398 |
const Graph* graph; |
| 399 | 399 |
public: |
| 400 | 400 |
|
| 401 | 401 |
OutArcIt() { }
|
| 402 | 402 |
|
| 403 | 403 |
OutArcIt(Invalid i) : Arc(i) { }
|
| 404 | 404 |
|
| 405 | 405 |
OutArcIt(const Graph& _graph, const Node& node) |
| 406 | 406 |
: graph(&_graph) {
|
| 407 | 407 |
_graph.firstOut(*this, node); |
| 408 | 408 |
} |
| 409 | 409 |
|
| 410 | 410 |
OutArcIt(const Graph& _graph, const Arc& arc) |
| 411 | 411 |
: Arc(arc), graph(&_graph) {}
|
| 412 | 412 |
|
| 413 | 413 |
OutArcIt& operator++() {
|
| 414 | 414 |
graph->nextOut(*this); |
| 415 | 415 |
return *this; |
| 416 | 416 |
} |
| 417 | 417 |
|
| 418 | 418 |
}; |
| 419 | 419 |
|
| 420 | 420 |
|
| 421 | 421 |
class InArcIt : public Arc {
|
| 422 | 422 |
const Graph* graph; |
| 423 | 423 |
public: |
| 424 | 424 |
|
| 425 | 425 |
InArcIt() { }
|
| 426 | 426 |
|
| 427 | 427 |
InArcIt(Invalid i) : Arc(i) { }
|
| 428 | 428 |
|
| 429 | 429 |
InArcIt(const Graph& _graph, const Node& node) |
| 430 | 430 |
: graph(&_graph) {
|
| 431 | 431 |
_graph.firstIn(*this, node); |
| 432 | 432 |
} |
| 433 | 433 |
|
| 434 | 434 |
InArcIt(const Graph& _graph, const Arc& arc) : |
| 435 | 435 |
Arc(arc), graph(&_graph) {}
|
| 436 | 436 |
|
| 437 | 437 |
InArcIt& operator++() {
|
| 438 | 438 |
graph->nextIn(*this); |
| 439 | 439 |
return *this; |
| 440 | 440 |
} |
| 441 | 441 |
|
| 442 | 442 |
}; |
| 443 | 443 |
|
| 444 | 444 |
|
| 445 | 445 |
class EdgeIt : public Parent::Edge {
|
| 446 | 446 |
const Graph* graph; |
| 447 | 447 |
public: |
| 448 | 448 |
|
| 449 | 449 |
EdgeIt() { }
|
| 450 | 450 |
|
| 451 | 451 |
EdgeIt(Invalid i) : Edge(i) { }
|
| 452 | 452 |
|
| 453 | 453 |
explicit EdgeIt(const Graph& _graph) : graph(&_graph) {
|
| 454 | 454 |
_graph.first(static_cast<Edge&>(*this)); |
| 455 | 455 |
} |
| 456 | 456 |
|
| 457 | 457 |
EdgeIt(const Graph& _graph, const Edge& e) : |
| 458 | 458 |
Edge(e), graph(&_graph) { }
|
| 459 | 459 |
|
| 460 | 460 |
EdgeIt& operator++() {
|
| 461 | 461 |
graph->next(*this); |
| 462 | 462 |
return *this; |
| 463 | 463 |
} |
| 464 | 464 |
|
| 465 | 465 |
}; |
| 466 | 466 |
|
| 467 | 467 |
class IncEdgeIt : public Parent::Edge {
|
| 468 | 468 |
friend class EdgeSetExtender; |
| 469 | 469 |
const Graph* graph; |
| 470 | 470 |
bool direction; |
| 471 | 471 |
public: |
| 472 | 472 |
|
| 473 | 473 |
IncEdgeIt() { }
|
| 474 | 474 |
|
| 475 | 475 |
IncEdgeIt(Invalid i) : Edge(i), direction(false) { }
|
| 476 | 476 |
|
| 477 | 477 |
IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) {
|
| 478 | 478 |
_graph.firstInc(*this, direction, n); |
| 479 | 479 |
} |
| 480 | 480 |
|
| 481 | 481 |
IncEdgeIt(const Graph& _graph, const Edge &ue, const Node &n) |
| 482 | 482 |
: graph(&_graph), Edge(ue) {
|
| 483 | 483 |
direction = (_graph.source(ue) == n); |
| 484 | 484 |
} |
| 485 | 485 |
|
| 486 | 486 |
IncEdgeIt& operator++() {
|
| 487 | 487 |
graph->nextInc(*this, direction); |
| 488 | 488 |
return *this; |
| 489 | 489 |
} |
| 490 | 490 |
}; |
| 491 | 491 |
|
| 492 | 492 |
// \brief Base node of the iterator |
| 493 | 493 |
// |
| 494 | 494 |
// Returns the base node (ie. the source in this case) of the iterator |
| 495 | 495 |
Node baseNode(const OutArcIt &e) const {
|
| 496 | 496 |
return Parent::source(static_cast<const Arc&>(e)); |
| 497 | 497 |
} |
| 498 | 498 |
// \brief Running node of the iterator |
| 499 | 499 |
// |
| 500 | 500 |
// Returns the running node (ie. the target in this case) of the |
| 501 | 501 |
// iterator |
| 502 | 502 |
Node runningNode(const OutArcIt &e) const {
|
| 503 | 503 |
return Parent::target(static_cast<const Arc&>(e)); |
| 504 | 504 |
} |
| 505 | 505 |
|
| 506 | 506 |
// \brief Base node of the iterator |
| 507 | 507 |
// |
| 508 | 508 |
// Returns the base node (ie. the target in this case) of the iterator |
| 509 | 509 |
Node baseNode(const InArcIt &e) const {
|
| 510 | 510 |
return Parent::target(static_cast<const Arc&>(e)); |
| 511 | 511 |
} |
| 512 | 512 |
// \brief Running node of the iterator |
| 513 | 513 |
// |
| 514 | 514 |
// Returns the running node (ie. the source in this case) of the |
| 515 | 515 |
// iterator |
| 516 | 516 |
Node runningNode(const InArcIt &e) const {
|
| 517 | 517 |
return Parent::source(static_cast<const Arc&>(e)); |
| 518 | 518 |
} |
| 519 | 519 |
|
| 520 | 520 |
// Base node of the iterator |
| 521 | 521 |
// |
| 522 | 522 |
// Returns the base node of the iterator |
| 523 | 523 |
Node baseNode(const IncEdgeIt &e) const {
|
| 524 | 524 |
return e.direction ? u(e) : v(e); |
| 525 | 525 |
} |
| 526 | 526 |
// Running node of the iterator |
| 527 | 527 |
// |
| 528 | 528 |
// Returns the running node of the iterator |
| 529 | 529 |
Node runningNode(const IncEdgeIt &e) const {
|
| 530 | 530 |
return e.direction ? v(e) : u(e); |
| 531 | 531 |
} |
| 532 | 532 |
|
| 533 | 533 |
|
| 534 | 534 |
template <typename _Value> |
| 535 | 535 |
class ArcMap |
| 536 | 536 |
: public MapExtender<DefaultMap<Graph, Arc, _Value> > {
|
| 537 | 537 |
typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent; |
| 538 | 538 |
|
| 539 | 539 |
public: |
| 540 | 540 |
explicit ArcMap(const Graph& _g) |
| 541 | 541 |
: Parent(_g) {}
|
| 542 | 542 |
ArcMap(const Graph& _g, const _Value& _v) |
| 543 | 543 |
: Parent(_g, _v) {}
|
| 544 | 544 |
|
| 545 | 545 |
ArcMap& operator=(const ArcMap& cmap) {
|
| 546 | 546 |
return operator=<ArcMap>(cmap); |
| 547 | 547 |
} |
| 548 | 548 |
|
| 549 | 549 |
template <typename CMap> |
| 550 | 550 |
ArcMap& operator=(const CMap& cmap) {
|
| 551 | 551 |
Parent::operator=(cmap); |
| 552 | 552 |
return *this; |
| 553 | 553 |
} |
| 554 | 554 |
|
| 555 | 555 |
}; |
| 556 | 556 |
|
| 557 | 557 |
|
| 558 | 558 |
template <typename _Value> |
| 559 | 559 |
class EdgeMap |
| 560 | 560 |
: public MapExtender<DefaultMap<Graph, Edge, _Value> > {
|
| 561 | 561 |
typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent; |
| 562 | 562 |
|
| 563 | 563 |
public: |
| 564 | 564 |
explicit EdgeMap(const Graph& _g) |
| 565 | 565 |
: Parent(_g) {}
|
| 566 | 566 |
|
| 567 | 567 |
EdgeMap(const Graph& _g, const _Value& _v) |
| 568 | 568 |
: Parent(_g, _v) {}
|
| 569 | 569 |
|
| 570 | 570 |
EdgeMap& operator=(const EdgeMap& cmap) {
|
| 571 | 571 |
return operator=<EdgeMap>(cmap); |
| 572 | 572 |
} |
| 573 | 573 |
|
| 574 | 574 |
template <typename CMap> |
| 575 | 575 |
EdgeMap& operator=(const CMap& cmap) {
|
| 576 | 576 |
Parent::operator=(cmap); |
| 577 | 577 |
return *this; |
| 578 | 578 |
} |
| 579 | 579 |
|
| 580 | 580 |
}; |
| 581 | 581 |
|
| 582 | 582 |
|
| 583 | 583 |
// Alteration extension |
| 584 | 584 |
|
| 585 | 585 |
Edge addEdge(const Node& from, const Node& to) {
|
| 586 | 586 |
Edge edge = Parent::addEdge(from, to); |
| 587 | 587 |
notifier(Edge()).add(edge); |
| 588 | 588 |
std::vector<Arc> arcs; |
| 589 | 589 |
arcs.push_back(Parent::direct(edge, true)); |
| 590 | 590 |
arcs.push_back(Parent::direct(edge, false)); |
| 591 | 591 |
notifier(Arc()).add(arcs); |
| 592 | 592 |
return edge; |
| 593 | 593 |
} |
| 594 | 594 |
|
| 595 | 595 |
void clear() {
|
| 596 | 596 |
notifier(Arc()).clear(); |
| 597 | 597 |
notifier(Edge()).clear(); |
| 598 | 598 |
Parent::clear(); |
| 599 | 599 |
} |
| 600 | 600 |
|
| 601 | 601 |
void erase(const Edge& edge) {
|
| 602 | 602 |
std::vector<Arc> arcs; |
| 603 | 603 |
arcs.push_back(Parent::direct(edge, true)); |
| 604 | 604 |
arcs.push_back(Parent::direct(edge, false)); |
| 605 | 605 |
notifier(Arc()).erase(arcs); |
| 606 | 606 |
notifier(Edge()).erase(edge); |
| 607 | 607 |
Parent::erase(edge); |
| 608 | 608 |
} |
| 609 | 609 |
|
| 610 | 610 |
|
| 611 | 611 |
EdgeSetExtender() {
|
| 612 | 612 |
arc_notifier.setContainer(*this); |
| 613 | 613 |
edge_notifier.setContainer(*this); |
| 614 | 614 |
} |
| 615 | 615 |
|
| 616 | 616 |
~EdgeSetExtender() {
|
| 617 | 617 |
edge_notifier.clear(); |
| 618 | 618 |
arc_notifier.clear(); |
| 619 | 619 |
} |
| 620 | 620 |
|
| 621 | 621 |
}; |
| 622 | 622 |
|
| 623 | 623 |
} |
| 624 | 624 |
|
| 625 | 625 |
#endif |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2010 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_BITS_SOLVER_BITS_H |
| 20 | 20 |
#define LEMON_BITS_SOLVER_BITS_H |
| 21 | 21 |
|
| 22 | 22 |
#include <vector> |
| 23 | 23 |
|
| 24 | 24 |
namespace lemon {
|
| 25 | 25 |
|
| 26 | 26 |
namespace _solver_bits {
|
| 27 | 27 |
|
| 28 | 28 |
class VarIndex {
|
| 29 | 29 |
private: |
| 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 |
/// |
| 198 | 198 |
/// This function inserts the given item into the heap with the |
| 199 | 199 |
/// given priority. |
| 200 | 200 |
/// \param i The item to insert. |
| 201 | 201 |
/// \param p The priority of the item. |
| 202 | 202 |
/// \pre \e i must not be stored in the heap. |
| 203 | 203 |
void push(const Item &i, const Prio &p) {
|
| 204 | 204 |
int idx = _data.size(); |
| 205 | 205 |
_iim[i] = idx; |
| 206 | 206 |
_data.push_back(BucketItem(i, p)); |
| 207 | 207 |
lace(idx); |
| 208 | 208 |
if (Direction::less(p, _minimum)) {
|
| 209 | 209 |
_minimum = p; |
| 210 | 210 |
} |
| 211 | 211 |
} |
| 212 | 212 |
|
| 213 | 213 |
/// \brief Return the item having minimum priority. |
| 214 | 214 |
/// |
| 215 | 215 |
/// This function returns the item having minimum priority. |
| 216 | 216 |
/// \pre The heap must be non-empty. |
| 217 | 217 |
Item top() const {
|
| 218 | 218 |
while (_first[_minimum] == -1) {
|
| 219 | 219 |
Direction::increase(_minimum); |
| 220 | 220 |
} |
| 221 | 221 |
return _data[_first[_minimum]].item; |
| 222 | 222 |
} |
| 223 | 223 |
|
| 224 | 224 |
/// \brief The minimum priority. |
| 225 | 225 |
/// |
| 226 | 226 |
/// This function returns the minimum priority. |
| 227 | 227 |
/// \pre The heap must be non-empty. |
| 228 | 228 |
Prio prio() const {
|
| 229 | 229 |
while (_first[_minimum] == -1) {
|
| 230 | 230 |
Direction::increase(_minimum); |
| 231 | 231 |
} |
| 232 | 232 |
return _minimum; |
| 233 | 233 |
} |
| 234 | 234 |
|
| 235 | 235 |
/// \brief Remove the item having minimum priority. |
| 236 | 236 |
/// |
| 237 | 237 |
/// This function removes the item having minimum priority. |
| 238 | 238 |
/// \pre The heap must be non-empty. |
| 239 | 239 |
void pop() {
|
| 240 | 240 |
while (_first[_minimum] == -1) {
|
| 241 | 241 |
Direction::increase(_minimum); |
| 242 | 242 |
} |
| 243 | 243 |
int idx = _first[_minimum]; |
| 244 | 244 |
_iim[_data[idx].item] = -2; |
| 245 | 245 |
unlace(idx); |
| 246 | 246 |
relocateLast(idx); |
| 247 | 247 |
} |
| 248 | 248 |
|
| 249 | 249 |
/// \brief Remove the given item from the heap. |
| 250 | 250 |
/// |
| 251 | 251 |
/// This function removes the given item from the heap if it is |
| 252 | 252 |
/// already stored. |
| 253 | 253 |
/// \param i The item to delete. |
| 254 | 254 |
/// \pre \e i must be in the heap. |
| 255 | 255 |
void erase(const Item &i) {
|
| 256 | 256 |
int idx = _iim[i]; |
| 257 | 257 |
_iim[_data[idx].item] = -2; |
| 258 | 258 |
unlace(idx); |
| 259 | 259 |
relocateLast(idx); |
| 260 | 260 |
} |
| 261 | 261 |
|
| 262 | 262 |
/// \brief The priority of the given item. |
| 263 | 263 |
/// |
| 264 | 264 |
/// This function returns the priority of the given item. |
| 265 | 265 |
/// \param i The item. |
| 266 | 266 |
/// \pre \e i must be in the heap. |
| 267 | 267 |
Prio operator[](const Item &i) const {
|
| 268 | 268 |
int idx = _iim[i]; |
| 269 | 269 |
return _data[idx].value; |
| 270 | 270 |
} |
| 271 | 271 |
|
| 272 | 272 |
/// \brief Set the priority of an item or insert it, if it is |
| 273 | 273 |
/// not stored in the heap. |
| 274 | 274 |
/// |
| 275 | 275 |
/// This method sets the priority of the given item if it is |
| 276 | 276 |
/// already stored in the heap. Otherwise it inserts the given |
| 277 | 277 |
/// item into the heap with the given priority. |
| 278 | 278 |
/// \param i The item. |
| 279 | 279 |
/// \param p The priority. |
| 280 | 280 |
void set(const Item &i, const Prio &p) {
|
| 281 | 281 |
int idx = _iim[i]; |
| 282 | 282 |
if (idx < 0) {
|
| 283 | 283 |
push(i, p); |
| 284 | 284 |
} else if (Direction::less(p, _data[idx].value)) {
|
| 285 | 285 |
decrease(i, p); |
| 286 | 286 |
} else {
|
| 287 | 287 |
increase(i, p); |
| 288 | 288 |
} |
| 289 | 289 |
} |
| 290 | 290 |
|
| 291 | 291 |
/// \brief Decrease the priority of an item to the given value. |
| 292 | 292 |
/// |
| 293 | 293 |
/// This function decreases the priority of an item to the given value. |
| 294 | 294 |
/// \param i The item. |
| 295 | 295 |
/// \param p The priority. |
| 296 | 296 |
/// \pre \e i must be stored in the heap with priority at least \e p. |
| 297 | 297 |
void decrease(const Item &i, const Prio &p) {
|
| 298 | 298 |
int idx = _iim[i]; |
| 299 | 299 |
unlace(idx); |
| 300 | 300 |
_data[idx].value = p; |
| 301 | 301 |
if (Direction::less(p, _minimum)) {
|
| 302 | 302 |
_minimum = p; |
| 303 | 303 |
} |
| 304 | 304 |
lace(idx); |
| 305 | 305 |
} |
| 306 | 306 |
|
| 307 | 307 |
/// \brief Increase the priority of an item to the given value. |
| 308 | 308 |
/// |
| 309 | 309 |
/// This function increases the priority of an item to the given value. |
| 310 | 310 |
/// \param i The item. |
| 311 | 311 |
/// \param p The priority. |
| 312 | 312 |
/// \pre \e i must be stored in the heap with priority at most \e p. |
| 313 | 313 |
void increase(const Item &i, const Prio &p) {
|
| 314 | 314 |
int idx = _iim[i]; |
| 315 | 315 |
unlace(idx); |
| 316 | 316 |
_data[idx].value = p; |
| 317 | 317 |
lace(idx); |
| 318 | 318 |
} |
| 319 | 319 |
|
| 320 | 320 |
/// \brief Return the state of an item. |
| 321 | 321 |
/// |
| 322 | 322 |
/// This method returns \c PRE_HEAP if the given item has never |
| 323 | 323 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
| 324 | 324 |
/// and \c POST_HEAP otherwise. |
| 325 | 325 |
/// In the latter case it is possible that the item will get back |
| 326 | 326 |
/// to the heap again. |
| 327 | 327 |
/// \param i The item. |
| 328 | 328 |
State state(const Item &i) const {
|
| 329 | 329 |
int idx = _iim[i]; |
| 330 | 330 |
if (idx >= 0) idx = 0; |
| 331 | 331 |
return State(idx); |
| 332 | 332 |
} |
| 333 | 333 |
|
| 334 | 334 |
/// \brief Set the state of an item in the heap. |
| 335 | 335 |
/// |
| 336 | 336 |
/// This function sets the state of the given item in the heap. |
| 337 | 337 |
/// It can be used to manually clear the heap when it is important |
| 338 | 338 |
/// to achive better time complexity. |
| 339 | 339 |
/// \param i The item. |
| 340 | 340 |
/// \param st The state. It should not be \c IN_HEAP. |
| 341 | 341 |
void state(const Item& i, State st) {
|
| 342 | 342 |
switch (st) {
|
| 343 | 343 |
case POST_HEAP: |
| 344 | 344 |
case PRE_HEAP: |
| 345 | 345 |
if (state(i) == IN_HEAP) {
|
| 346 | 346 |
erase(i); |
| 347 | 347 |
} |
| 348 | 348 |
_iim[i] = st; |
| 349 | 349 |
break; |
| 350 | 350 |
case IN_HEAP: |
| 351 | 351 |
break; |
| 352 | 352 |
} |
| 353 | 353 |
} |
| 354 | 354 |
|
| 355 | 355 |
private: |
| 356 | 356 |
|
| 357 | 357 |
struct BucketItem {
|
| 358 | 358 |
BucketItem(const Item& _item, int _value) |
| 359 | 359 |
: item(_item), value(_value) {}
|
| 360 | 360 |
|
| 361 | 361 |
Item item; |
| 362 | 362 |
int value; |
| 363 | 363 |
|
| 364 | 364 |
int prev, next; |
| 365 | 365 |
}; |
| 366 | 366 |
|
| 367 | 367 |
ItemIntMap& _iim; |
| 368 | 368 |
std::vector<int> _first; |
| 369 | 369 |
std::vector<BucketItem> _data; |
| 370 | 370 |
mutable int _minimum; |
| 371 | 371 |
|
| 372 | 372 |
}; // class BucketHeap |
| 373 | 373 |
|
| 374 | 374 |
/// \ingroup heaps |
| 375 | 375 |
/// |
| 376 | 376 |
/// \brief Simplified bucket heap data structure. |
| 377 | 377 |
/// |
| 378 | 378 |
/// This class implements a simplified \e bucket \e heap data |
| 379 | 379 |
/// structure. It does not provide some functionality, but it is |
| 380 | 380 |
/// faster and simpler than BucketHeap. The main difference is |
| 381 | 381 |
/// that BucketHeap stores a doubly-linked list for each key while |
| 382 | 382 |
/// this class stores only simply-linked lists. It supports erasing |
| 383 | 383 |
/// only for the item having minimum priority and it does not support |
| 384 | 384 |
/// key increasing and decreasing. |
| 385 | 385 |
/// |
| 386 | 386 |
/// Note that this implementation does not conform to the |
| 387 | 387 |
/// \ref concepts::Heap "heap concept" due to the lack of some |
| 388 | 388 |
/// functionality. |
| 389 | 389 |
/// |
| 390 | 390 |
/// \tparam IM A read-writable item map with \c int values, used |
| 391 | 391 |
/// internally to handle the cross references. |
| 392 | 392 |
/// \tparam MIN Indicate if the heap is a \e min-heap or a \e max-heap. |
| 393 | 393 |
/// The default is \e min-heap. If this parameter is set to \c false, |
| 394 | 394 |
/// then the comparison is reversed, so the top(), prio() and pop() |
| 395 | 395 |
/// functions deal with the item having maximum priority instead of the |
| 396 | 396 |
/// minimum. |
| 397 | 397 |
/// |
| 398 | 398 |
/// \sa BucketHeap |
| 399 | 399 |
template <typename IM, bool MIN = true > |
| 400 | 400 |
class SimpleBucketHeap {
|
| 401 | 401 |
|
| 402 | 402 |
public: |
| 403 | 403 |
|
| 404 | 404 |
/// Type of the item-int map. |
| 405 | 405 |
typedef IM ItemIntMap; |
| 406 | 406 |
/// Type of the priorities. |
| 407 | 407 |
typedef int Prio; |
| 408 | 408 |
/// Type of the items stored in the heap. |
| 409 | 409 |
typedef typename ItemIntMap::Key Item; |
| 410 | 410 |
/// Type of the item-priority pairs. |
| 411 | 411 |
typedef std::pair<Item,Prio> Pair; |
| 412 | 412 |
|
| 413 | 413 |
private: |
| 414 | 414 |
|
| 415 | 415 |
typedef _bucket_heap_bits::DirectionTraits<MIN> Direction; |
| 416 | 416 |
|
| 417 | 417 |
public: |
| 418 | 418 |
|
| 419 | 419 |
/// \brief Type to represent the states of the items. |
| 420 | 420 |
/// |
| 421 | 421 |
/// Each item has a state associated to it. It can be "in heap", |
| 422 | 422 |
/// "pre-heap" or "post-heap". The latter two are indifferent from the |
| 423 | 423 |
/// heap's point of view, but may be useful to the user. |
| 424 | 424 |
/// |
| 425 | 425 |
/// The item-int map must be initialized in such way that it assigns |
| 426 | 426 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
| 427 | 427 |
enum State {
|
| 428 | 428 |
IN_HEAP = 0, ///< = 0. |
| 429 | 429 |
PRE_HEAP = -1, ///< = -1. |
| 430 | 430 |
POST_HEAP = -2 ///< = -2. |
| 431 | 431 |
}; |
| 432 | 432 |
|
| 433 | 433 |
public: |
| 434 | 434 |
|
| 435 | 435 |
/// \brief Constructor. |
| 436 | 436 |
/// |
| 437 | 437 |
/// Constructor. |
| 438 | 438 |
/// \param map A map that assigns \c int values to the items. |
| 439 | 439 |
/// It is used internally to handle the cross references. |
| 440 | 440 |
/// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item. |
| 441 | 441 |
explicit SimpleBucketHeap(ItemIntMap &map) |
| 442 | 442 |
: _iim(map), _free(-1), _num(0), _minimum(0) {}
|
| 443 | 443 |
|
| 444 | 444 |
/// \brief The number of items stored in the heap. |
| 445 | 445 |
/// |
| 446 | 446 |
/// This function returns the number of items stored in the heap. |
| 447 | 447 |
int size() const { return _num; }
|
| 448 | 448 |
|
| 449 | 449 |
/// \brief Check if the heap is empty. |
| 450 | 450 |
/// |
| 451 | 451 |
/// This function returns \c true if the heap is empty. |
| 452 | 452 |
bool empty() const { return _num == 0; }
|
| 453 | 453 |
|
| 454 | 454 |
/// \brief Make the heap empty. |
| 455 | 455 |
/// |
| 456 | 456 |
/// This functon makes the heap empty. |
| 457 | 457 |
/// It does not change the cross reference map. If you want to reuse |
| 458 | 458 |
/// a heap that is not surely empty, you should first clear it and |
| 459 | 459 |
/// then you should set the cross reference map to \c PRE_HEAP |
| 460 | 460 |
/// for each item. |
| 461 | 461 |
void clear() {
|
| 462 | 462 |
_data.clear(); _first.clear(); _free = -1; _num = 0; _minimum = 0; |
| 463 | 463 |
} |
| 464 | 464 |
|
| 465 | 465 |
/// \brief Insert a pair of item and priority into the heap. |
| 466 | 466 |
/// |
| 467 | 467 |
/// This function inserts \c p.first to the heap with priority |
| 468 | 468 |
/// \c p.second. |
| 469 | 469 |
/// \param p The pair to insert. |
| 470 | 470 |
/// \pre \c p.first must not be stored in the heap. |
| 471 | 471 |
void push(const Pair& p) {
|
| 472 | 472 |
push(p.first, p.second); |
| 473 | 473 |
} |
| 474 | 474 |
|
| 475 | 475 |
/// \brief Insert an item into the heap with the given priority. |
| 476 | 476 |
/// |
| 477 | 477 |
/// This function inserts the given item into the heap with the |
| 478 | 478 |
/// given priority. |
| 479 | 479 |
/// \param i The item to insert. |
| 480 | 480 |
/// \param p The priority of the item. |
| 481 | 481 |
/// \pre \e i must not be stored in the heap. |
| 482 | 482 |
void push(const Item &i, const Prio &p) {
|
| 483 | 483 |
int idx; |
| 484 | 484 |
if (_free == -1) {
|
| 485 | 485 |
idx = _data.size(); |
| 486 | 486 |
_data.push_back(BucketItem(i)); |
| 487 | 487 |
} else {
|
| 488 | 488 |
idx = _free; |
| 489 | 489 |
_free = _data[idx].next; |
| 490 | 490 |
_data[idx].item = i; |
| 491 | 491 |
} |
| 492 | 492 |
_iim[i] = idx; |
| 493 | 493 |
if (p >= int(_first.size())) _first.resize(p + 1, -1); |
| 494 | 494 |
_data[idx].next = _first[p]; |
| 495 | 495 |
_first[p] = idx; |
| 496 | 496 |
if (Direction::less(p, _minimum)) {
|
| 497 | 497 |
_minimum = p; |
| 498 | 498 |
} |
| 499 | 499 |
++_num; |
| 500 | 500 |
} |
| 501 | 501 |
|
| 502 | 502 |
/// \brief Return the item having minimum priority. |
| 503 | 503 |
/// |
| 504 | 504 |
/// This function returns the item having minimum priority. |
| 505 | 505 |
/// \pre The heap must be non-empty. |
| 506 | 506 |
Item top() const {
|
| 507 | 507 |
while (_first[_minimum] == -1) {
|
| 508 | 508 |
Direction::increase(_minimum); |
| 509 | 509 |
} |
| 510 | 510 |
return _data[_first[_minimum]].item; |
| 511 | 511 |
} |
| 512 | 512 |
|
| 513 | 513 |
/// \brief The minimum priority. |
| 514 | 514 |
/// |
| 515 | 515 |
/// This function returns the minimum priority. |
| 516 | 516 |
/// \pre The heap must be non-empty. |
| 517 | 517 |
Prio prio() const {
|
| 518 | 518 |
while (_first[_minimum] == -1) {
|
| 519 | 519 |
Direction::increase(_minimum); |
| 520 | 520 |
} |
| 521 | 521 |
return _minimum; |
| 522 | 522 |
} |
| 523 | 523 |
|
| 524 | 524 |
/// \brief Remove the item having minimum priority. |
| 525 | 525 |
/// |
| 526 | 526 |
/// This function removes the item having minimum priority. |
| 527 | 527 |
/// \pre The heap must be non-empty. |
| 528 | 528 |
void pop() {
|
| 529 | 529 |
while (_first[_minimum] == -1) {
|
| 530 | 530 |
Direction::increase(_minimum); |
| 531 | 531 |
} |
| 532 | 532 |
int idx = _first[_minimum]; |
| 533 | 533 |
_iim[_data[idx].item] = -2; |
| 534 | 534 |
_first[_minimum] = _data[idx].next; |
| 535 | 535 |
_data[idx].next = _free; |
| 536 | 536 |
_free = idx; |
| 537 | 537 |
--_num; |
| 538 | 538 |
} |
| 539 | 539 |
|
| 540 | 540 |
/// \brief The priority of the given item. |
| 541 | 541 |
/// |
| 542 | 542 |
/// This function returns the priority of the given item. |
| 543 | 543 |
/// \param i The item. |
| 544 | 544 |
/// \pre \e i must be in the heap. |
| 545 | 545 |
/// \warning This operator is not a constant time function because |
| 546 | 546 |
/// it scans the whole data structure to find the proper value. |
| 547 | 547 |
Prio operator[](const Item &i) const {
|
| 548 | 548 |
for (int k = 0; k < int(_first.size()); ++k) {
|
| 549 | 549 |
int idx = _first[k]; |
| 550 | 550 |
while (idx != -1) {
|
| 551 | 551 |
if (_data[idx].item == i) {
|
| 552 | 552 |
return k; |
| 553 | 553 |
} |
| 554 | 554 |
idx = _data[idx].next; |
| 555 | 555 |
} |
| 556 | 556 |
} |
| 557 | 557 |
return -1; |
| 558 | 558 |
} |
| 559 | 559 |
|
| 560 | 560 |
/// \brief Return the state of an item. |
| 561 | 561 |
/// |
| 562 | 562 |
/// This method returns \c PRE_HEAP if the given item has never |
| 563 | 563 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
| 564 | 564 |
/// and \c POST_HEAP otherwise. |
| 565 | 565 |
/// In the latter case it is possible that the item will get back |
| 566 | 566 |
/// to the heap again. |
| 567 | 567 |
/// \param i The item. |
| 568 | 568 |
State state(const Item &i) const {
|
| 569 | 569 |
int idx = _iim[i]; |
| 570 | 570 |
if (idx >= 0) idx = 0; |
| 571 | 571 |
return State(idx); |
| 572 | 572 |
} |
| 573 | 573 |
|
| 574 | 574 |
private: |
| 575 | 575 |
|
| 576 | 576 |
struct BucketItem {
|
| 577 | 577 |
BucketItem(const Item& _item) |
| 578 | 578 |
: item(_item) {}
|
| 579 | 579 |
|
| 580 | 580 |
Item item; |
| 581 | 581 |
int next; |
| 582 | 582 |
}; |
| 583 | 583 |
|
| 584 | 584 |
ItemIntMap& _iim; |
| 585 | 585 |
std::vector<int> _first; |
| 586 | 586 |
std::vector<BucketItem> _data; |
| 587 | 587 |
int _free, _num; |
| 588 | 588 |
mutable int _minimum; |
| 589 | 589 |
|
| 590 | 590 |
}; // class SimpleBucketHeap |
| 591 | 591 |
|
| 592 | 592 |
} |
| 593 | 593 |
|
| 594 | 594 |
#endif |
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)