0
33
0
35
35
77
76
76
75
16
16
31
31
27
25
23
23
35
35
30
31
14
14
16
15
13
13
432
356
17
14
17
17
26
25
16
11
24
18
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 | 5 |
* Copyright (C) 2003-2009 |
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 |
This group |
|
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 |
... | ... |
@@ -97,591 +97,591 @@ |
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 semi_adaptors Semi-Adaptor Classes for Graphs |
142 | 142 |
@ingroup graphs |
143 | 143 |
\brief Graph types between real graphs and graph adaptors. |
144 | 144 |
|
145 |
This group |
|
145 |
This group contains some graph types between real graphs and graph adaptors. |
|
146 | 146 |
These classes wrap graphs to give new functionality as the adaptors do it. |
147 | 147 |
On the other hand they are not light-weight structures as the adaptors. |
148 | 148 |
*/ |
149 | 149 |
|
150 | 150 |
/** |
151 | 151 |
@defgroup maps Maps |
152 | 152 |
@ingroup datas |
153 | 153 |
\brief Map structures implemented in LEMON. |
154 | 154 |
|
155 |
This group |
|
155 |
This group contains the map structures implemented in LEMON. |
|
156 | 156 |
|
157 | 157 |
LEMON provides several special purpose maps and map adaptors that e.g. combine |
158 | 158 |
new maps from existing ones. |
159 | 159 |
|
160 | 160 |
<b>See also:</b> \ref map_concepts "Map Concepts". |
161 | 161 |
*/ |
162 | 162 |
|
163 | 163 |
/** |
164 | 164 |
@defgroup graph_maps Graph Maps |
165 | 165 |
@ingroup maps |
166 | 166 |
\brief Special graph-related maps. |
167 | 167 |
|
168 |
This group |
|
168 |
This group contains maps that are specifically designed to assign |
|
169 | 169 |
values to the nodes and arcs/edges of graphs. |
170 | 170 |
|
171 | 171 |
If you are looking for the standard graph maps (\c NodeMap, \c ArcMap, |
172 | 172 |
\c EdgeMap), see the \ref graph_concepts "Graph Structure Concepts". |
173 | 173 |
*/ |
174 | 174 |
|
175 | 175 |
/** |
176 | 176 |
\defgroup map_adaptors Map Adaptors |
177 | 177 |
\ingroup maps |
178 | 178 |
\brief Tools to create new maps from existing ones |
179 | 179 |
|
180 |
This group |
|
180 |
This group contains map adaptors that are used to create "implicit" |
|
181 | 181 |
maps from other maps. |
182 | 182 |
|
183 | 183 |
Most of them are \ref concepts::ReadMap "read-only maps". |
184 | 184 |
They can make arithmetic and logical operations between one or two maps |
185 | 185 |
(negation, shifting, addition, multiplication, logical 'and', 'or', |
186 | 186 |
'not' etc.) or e.g. convert a map to another one of different Value type. |
187 | 187 |
|
188 | 188 |
The typical usage of this classes is passing implicit maps to |
189 | 189 |
algorithms. If a function type algorithm is called then the function |
190 | 190 |
type map adaptors can be used comfortable. For example let's see the |
191 | 191 |
usage of map adaptors with the \c graphToEps() function. |
192 | 192 |
\code |
193 | 193 |
Color nodeColor(int deg) { |
194 | 194 |
if (deg >= 2) { |
195 | 195 |
return Color(0.5, 0.0, 0.5); |
196 | 196 |
} else if (deg == 1) { |
197 | 197 |
return Color(1.0, 0.5, 1.0); |
198 | 198 |
} else { |
199 | 199 |
return Color(0.0, 0.0, 0.0); |
200 | 200 |
} |
201 | 201 |
} |
202 | 202 |
|
203 | 203 |
Digraph::NodeMap<int> degree_map(graph); |
204 | 204 |
|
205 | 205 |
graphToEps(graph, "graph.eps") |
206 | 206 |
.coords(coords).scaleToA4().undirected() |
207 | 207 |
.nodeColors(composeMap(functorToMap(nodeColor), degree_map)) |
208 | 208 |
.run(); |
209 | 209 |
\endcode |
210 | 210 |
The \c functorToMap() function makes an \c int to \c Color map from the |
211 | 211 |
\c nodeColor() function. The \c composeMap() compose the \c degree_map |
212 | 212 |
and the previously created map. The composed map is a proper function to |
213 | 213 |
get the color of each node. |
214 | 214 |
|
215 | 215 |
The usage with class type algorithms is little bit harder. In this |
216 | 216 |
case the function type map adaptors can not be used, because the |
217 | 217 |
function map adaptors give back temporary objects. |
218 | 218 |
\code |
219 | 219 |
Digraph graph; |
220 | 220 |
|
221 | 221 |
typedef Digraph::ArcMap<double> DoubleArcMap; |
222 | 222 |
DoubleArcMap length(graph); |
223 | 223 |
DoubleArcMap speed(graph); |
224 | 224 |
|
225 | 225 |
typedef DivMap<DoubleArcMap, DoubleArcMap> TimeMap; |
226 | 226 |
TimeMap time(length, speed); |
227 | 227 |
|
228 | 228 |
Dijkstra<Digraph, TimeMap> dijkstra(graph, time); |
229 | 229 |
dijkstra.run(source, target); |
230 | 230 |
\endcode |
231 | 231 |
We have a length map and a maximum speed map on the arcs of a digraph. |
232 | 232 |
The minimum time to pass the arc can be calculated as the division of |
233 | 233 |
the two maps which can be done implicitly with the \c DivMap template |
234 | 234 |
class. We use the implicit minimum time map as the length map of the |
235 | 235 |
\c Dijkstra algorithm. |
236 | 236 |
*/ |
237 | 237 |
|
238 | 238 |
/** |
239 | 239 |
@defgroup matrices Matrices |
240 | 240 |
@ingroup datas |
241 | 241 |
\brief Two dimensional data storages implemented in LEMON. |
242 | 242 |
|
243 |
This group |
|
243 |
This group contains two dimensional data storages implemented in LEMON. |
|
244 | 244 |
*/ |
245 | 245 |
|
246 | 246 |
/** |
247 | 247 |
@defgroup paths Path Structures |
248 | 248 |
@ingroup datas |
249 | 249 |
\brief %Path structures implemented in LEMON. |
250 | 250 |
|
251 |
This group |
|
251 |
This group contains the path structures implemented in LEMON. |
|
252 | 252 |
|
253 | 253 |
LEMON provides flexible data structures to work with paths. |
254 | 254 |
All of them have similar interfaces and they can be copied easily with |
255 | 255 |
assignment operators and copy constructors. This makes it easy and |
256 | 256 |
efficient to have e.g. the Dijkstra algorithm to store its result in |
257 | 257 |
any kind of path structure. |
258 | 258 |
|
259 | 259 |
\sa lemon::concepts::Path |
260 | 260 |
*/ |
261 | 261 |
|
262 | 262 |
/** |
263 | 263 |
@defgroup auxdat Auxiliary Data Structures |
264 | 264 |
@ingroup datas |
265 | 265 |
\brief Auxiliary data structures implemented in LEMON. |
266 | 266 |
|
267 |
This group |
|
267 |
This group contains some data structures implemented in LEMON in |
|
268 | 268 |
order to make it easier to implement combinatorial algorithms. |
269 | 269 |
*/ |
270 | 270 |
|
271 | 271 |
/** |
272 | 272 |
@defgroup algs Algorithms |
273 |
\brief This group |
|
273 |
\brief This group contains the several algorithms |
|
274 | 274 |
implemented in LEMON. |
275 | 275 |
|
276 |
This group |
|
276 |
This group contains the several algorithms |
|
277 | 277 |
implemented in LEMON. |
278 | 278 |
*/ |
279 | 279 |
|
280 | 280 |
/** |
281 | 281 |
@defgroup search Graph Search |
282 | 282 |
@ingroup algs |
283 | 283 |
\brief Common graph search algorithms. |
284 | 284 |
|
285 |
This group |
|
285 |
This group contains the common graph search algorithms, namely |
|
286 | 286 |
\e breadth-first \e search (BFS) and \e depth-first \e search (DFS). |
287 | 287 |
*/ |
288 | 288 |
|
289 | 289 |
/** |
290 | 290 |
@defgroup shortest_path Shortest Path Algorithms |
291 | 291 |
@ingroup algs |
292 | 292 |
\brief Algorithms for finding shortest paths. |
293 | 293 |
|
294 |
This group |
|
294 |
This group contains the algorithms for finding shortest paths in digraphs. |
|
295 | 295 |
|
296 | 296 |
- \ref Dijkstra algorithm for finding shortest paths from a source node |
297 | 297 |
when all arc lengths are non-negative. |
298 | 298 |
- \ref BellmanFord "Bellman-Ford" algorithm for finding shortest paths |
299 | 299 |
from a source node when arc lenghts can be either positive or negative, |
300 | 300 |
but the digraph should not contain directed cycles with negative total |
301 | 301 |
length. |
302 | 302 |
- \ref FloydWarshall "Floyd-Warshall" and \ref Johnson "Johnson" algorithms |
303 | 303 |
for solving the \e all-pairs \e shortest \e paths \e problem when arc |
304 | 304 |
lenghts can be either positive or negative, but the digraph should |
305 | 305 |
not contain directed cycles with negative total length. |
306 | 306 |
- \ref Suurballe A successive shortest path algorithm for finding |
307 | 307 |
arc-disjoint paths between two nodes having minimum total length. |
308 | 308 |
*/ |
309 | 309 |
|
310 | 310 |
/** |
311 | 311 |
@defgroup max_flow Maximum Flow Algorithms |
312 | 312 |
@ingroup algs |
313 | 313 |
\brief Algorithms for finding maximum flows. |
314 | 314 |
|
315 |
This group |
|
315 |
This group contains the algorithms for finding maximum flows and |
|
316 | 316 |
feasible circulations. |
317 | 317 |
|
318 | 318 |
The \e maximum \e flow \e problem is to find a flow of maximum value between |
319 | 319 |
a single source and a single target. Formally, there is a \f$G=(V,A)\f$ |
320 | 320 |
digraph, a \f$cap:A\rightarrow\mathbf{R}^+_0\f$ capacity function and |
321 | 321 |
\f$s, t \in V\f$ source and target nodes. |
322 | 322 |
A maximum flow is an \f$f:A\rightarrow\mathbf{R}^+_0\f$ solution of the |
323 | 323 |
following optimization problem. |
324 | 324 |
|
325 | 325 |
\f[ \max\sum_{a\in\delta_{out}(s)}f(a) - \sum_{a\in\delta_{in}(s)}f(a) \f] |
326 | 326 |
\f[ \sum_{a\in\delta_{out}(v)} f(a) = \sum_{a\in\delta_{in}(v)} f(a) |
327 | 327 |
\qquad \forall v\in V\setminus\{s,t\} \f] |
328 | 328 |
\f[ 0 \leq f(a) \leq cap(a) \qquad \forall a\in A \f] |
329 | 329 |
|
330 | 330 |
LEMON contains several algorithms for solving maximum flow problems: |
331 | 331 |
- \ref EdmondsKarp Edmonds-Karp algorithm. |
332 | 332 |
- \ref Preflow Goldberg-Tarjan's preflow push-relabel algorithm. |
333 | 333 |
- \ref DinitzSleatorTarjan Dinitz's blocking flow algorithm with dynamic trees. |
334 | 334 |
- \ref GoldbergTarjan Preflow push-relabel algorithm with dynamic trees. |
335 | 335 |
|
336 | 336 |
In most cases the \ref Preflow "Preflow" algorithm provides the |
337 | 337 |
fastest method for computing a maximum flow. All implementations |
338 | 338 |
provides functions to also query the minimum cut, which is the dual |
339 | 339 |
problem of the maximum flow. |
340 | 340 |
*/ |
341 | 341 |
|
342 | 342 |
/** |
343 | 343 |
@defgroup min_cost_flow Minimum Cost Flow Algorithms |
344 | 344 |
@ingroup algs |
345 | 345 |
|
346 | 346 |
\brief Algorithms for finding minimum cost flows and circulations. |
347 | 347 |
|
348 |
This group |
|
348 |
This group contains the algorithms for finding minimum cost flows and |
|
349 | 349 |
circulations. |
350 | 350 |
|
351 | 351 |
The \e minimum \e cost \e flow \e problem is to find a feasible flow of |
352 | 352 |
minimum total cost from a set of supply nodes to a set of demand nodes |
353 | 353 |
in a network with capacity constraints and arc costs. |
354 | 354 |
Formally, let \f$G=(V,A)\f$ be a digraph, |
355 | 355 |
\f$lower, upper: A\rightarrow\mathbf{Z}^+_0\f$ denote the lower and |
356 | 356 |
upper bounds for the flow values on the arcs, |
357 | 357 |
\f$cost: A\rightarrow\mathbf{Z}^+_0\f$ denotes the cost per unit flow |
358 | 358 |
on the arcs, and |
359 | 359 |
\f$supply: V\rightarrow\mathbf{Z}\f$ denotes the supply/demand values |
360 | 360 |
of the nodes. |
361 | 361 |
A minimum cost flow is an \f$f:A\rightarrow\mathbf{R}^+_0\f$ solution of |
362 | 362 |
the following optimization problem. |
363 | 363 |
|
364 | 364 |
\f[ \min\sum_{a\in A} f(a) cost(a) \f] |
365 | 365 |
\f[ \sum_{a\in\delta_{out}(v)} f(a) - \sum_{a\in\delta_{in}(v)} f(a) = |
366 | 366 |
supply(v) \qquad \forall v\in V \f] |
367 | 367 |
\f[ lower(a) \leq f(a) \leq upper(a) \qquad \forall a\in A \f] |
368 | 368 |
|
369 | 369 |
LEMON contains several algorithms for solving minimum cost flow problems: |
370 | 370 |
- \ref CycleCanceling Cycle-canceling algorithms. |
371 | 371 |
- \ref CapacityScaling Successive shortest path algorithm with optional |
372 | 372 |
capacity scaling. |
373 | 373 |
- \ref CostScaling Push-relabel and augment-relabel algorithms based on |
374 | 374 |
cost scaling. |
375 | 375 |
- \ref NetworkSimplex Primal network simplex algorithm with various |
376 | 376 |
pivot strategies. |
377 | 377 |
*/ |
378 | 378 |
|
379 | 379 |
/** |
380 | 380 |
@defgroup min_cut Minimum Cut Algorithms |
381 | 381 |
@ingroup algs |
382 | 382 |
|
383 | 383 |
\brief Algorithms for finding minimum cut in graphs. |
384 | 384 |
|
385 |
This group |
|
385 |
This group contains the algorithms for finding minimum cut in graphs. |
|
386 | 386 |
|
387 | 387 |
The \e minimum \e cut \e problem is to find a non-empty and non-complete |
388 | 388 |
\f$X\f$ subset of the nodes with minimum overall capacity on |
389 | 389 |
outgoing arcs. Formally, there is a \f$G=(V,A)\f$ digraph, a |
390 | 390 |
\f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function. The minimum |
391 | 391 |
cut is the \f$X\f$ solution of the next optimization problem: |
392 | 392 |
|
393 | 393 |
\f[ \min_{X \subset V, X\not\in \{\emptyset, V\}} |
394 | 394 |
\sum_{uv\in A, u\in X, v\not\in X}cap(uv) \f] |
395 | 395 |
|
396 | 396 |
LEMON contains several algorithms related to minimum cut problems: |
397 | 397 |
|
398 | 398 |
- \ref HaoOrlin "Hao-Orlin algorithm" for calculating minimum cut |
399 | 399 |
in directed graphs. |
400 | 400 |
- \ref NagamochiIbaraki "Nagamochi-Ibaraki algorithm" for |
401 | 401 |
calculating minimum cut in undirected graphs. |
402 |
- \ref |
|
402 |
- \ref GomoryHu "Gomory-Hu tree computation" for calculating |
|
403 | 403 |
all-pairs minimum cut in undirected graphs. |
404 | 404 |
|
405 | 405 |
If you want to find minimum cut just between two distinict nodes, |
406 | 406 |
see the \ref max_flow "maximum flow problem". |
407 | 407 |
*/ |
408 | 408 |
|
409 | 409 |
/** |
410 | 410 |
@defgroup graph_prop Connectivity and Other Graph Properties |
411 | 411 |
@ingroup algs |
412 | 412 |
\brief Algorithms for discovering the graph properties |
413 | 413 |
|
414 |
This group |
|
414 |
This group contains the algorithms for discovering the graph properties |
|
415 | 415 |
like connectivity, bipartiteness, euler property, simplicity etc. |
416 | 416 |
|
417 | 417 |
\image html edge_biconnected_components.png |
418 | 418 |
\image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth |
419 | 419 |
*/ |
420 | 420 |
|
421 | 421 |
/** |
422 | 422 |
@defgroup planar Planarity Embedding and Drawing |
423 | 423 |
@ingroup algs |
424 | 424 |
\brief Algorithms for planarity checking, embedding and drawing |
425 | 425 |
|
426 |
This group |
|
426 |
This group contains the algorithms for planarity checking, |
|
427 | 427 |
embedding and drawing. |
428 | 428 |
|
429 | 429 |
\image html planar.png |
430 | 430 |
\image latex planar.eps "Plane graph" width=\textwidth |
431 | 431 |
*/ |
432 | 432 |
|
433 | 433 |
/** |
434 | 434 |
@defgroup matching Matching Algorithms |
435 | 435 |
@ingroup algs |
436 | 436 |
\brief Algorithms for finding matchings in graphs and bipartite graphs. |
437 | 437 |
|
438 | 438 |
This group contains algorithm objects and functions to calculate |
439 | 439 |
matchings in graphs and bipartite graphs. The general matching problem is |
440 | 440 |
finding a subset of the arcs which does not shares common endpoints. |
441 | 441 |
|
442 | 442 |
There are several different algorithms for calculate matchings in |
443 | 443 |
graphs. The matching problems in bipartite graphs are generally |
444 | 444 |
easier than in general graphs. The goal of the matching optimization |
445 | 445 |
can be finding maximum cardinality, maximum weight or minimum cost |
446 | 446 |
matching. The search can be constrained to find perfect or |
447 | 447 |
maximum cardinality matching. |
448 | 448 |
|
449 | 449 |
The matching algorithms implemented in LEMON: |
450 | 450 |
- \ref MaxBipartiteMatching Hopcroft-Karp augmenting path algorithm |
451 | 451 |
for calculating maximum cardinality matching in bipartite graphs. |
452 | 452 |
- \ref PrBipartiteMatching Push-relabel algorithm |
453 | 453 |
for calculating maximum cardinality matching in bipartite graphs. |
454 | 454 |
- \ref MaxWeightedBipartiteMatching |
455 | 455 |
Successive shortest path algorithm for calculating maximum weighted |
456 | 456 |
matching and maximum weighted bipartite matching in bipartite graphs. |
457 | 457 |
- \ref MinCostMaxBipartiteMatching |
458 | 458 |
Successive shortest path algorithm for calculating minimum cost maximum |
459 | 459 |
matching in bipartite graphs. |
460 | 460 |
- \ref MaxMatching Edmond's blossom shrinking algorithm for calculating |
461 | 461 |
maximum cardinality matching in general graphs. |
462 | 462 |
- \ref MaxWeightedMatching Edmond's blossom shrinking algorithm for calculating |
463 | 463 |
maximum weighted matching in general graphs. |
464 | 464 |
- \ref MaxWeightedPerfectMatching |
465 | 465 |
Edmond's blossom shrinking algorithm for calculating maximum weighted |
466 | 466 |
perfect matching in general graphs. |
467 | 467 |
|
468 | 468 |
\image html bipartite_matching.png |
469 | 469 |
\image latex bipartite_matching.eps "Bipartite Matching" width=\textwidth |
470 | 470 |
*/ |
471 | 471 |
|
472 | 472 |
/** |
473 | 473 |
@defgroup spantree Minimum Spanning Tree Algorithms |
474 | 474 |
@ingroup algs |
475 | 475 |
\brief Algorithms for finding a minimum cost spanning tree in a graph. |
476 | 476 |
|
477 |
This group |
|
477 |
This group contains the algorithms for finding a minimum cost spanning |
|
478 | 478 |
tree in a graph. |
479 | 479 |
*/ |
480 | 480 |
|
481 | 481 |
/** |
482 | 482 |
@defgroup auxalg Auxiliary Algorithms |
483 | 483 |
@ingroup algs |
484 | 484 |
\brief Auxiliary algorithms implemented in LEMON. |
485 | 485 |
|
486 |
This group |
|
486 |
This group contains some algorithms implemented in LEMON |
|
487 | 487 |
in order to make it easier to implement complex algorithms. |
488 | 488 |
*/ |
489 | 489 |
|
490 | 490 |
/** |
491 | 491 |
@defgroup approx Approximation Algorithms |
492 | 492 |
@ingroup algs |
493 | 493 |
\brief Approximation algorithms. |
494 | 494 |
|
495 |
This group |
|
495 |
This group contains the approximation and heuristic algorithms |
|
496 | 496 |
implemented in LEMON. |
497 | 497 |
*/ |
498 | 498 |
|
499 | 499 |
/** |
500 | 500 |
@defgroup gen_opt_group General Optimization Tools |
501 |
\brief This group |
|
501 |
\brief This group contains some general optimization frameworks |
|
502 | 502 |
implemented in LEMON. |
503 | 503 |
|
504 |
This group |
|
504 |
This group contains some general optimization frameworks |
|
505 | 505 |
implemented in LEMON. |
506 | 506 |
*/ |
507 | 507 |
|
508 | 508 |
/** |
509 | 509 |
@defgroup lp_group Lp and Mip Solvers |
510 | 510 |
@ingroup gen_opt_group |
511 | 511 |
\brief Lp and Mip solver interfaces for LEMON. |
512 | 512 |
|
513 |
This group |
|
513 |
This group contains Lp and Mip solver interfaces for LEMON. The |
|
514 | 514 |
various LP solvers could be used in the same manner with this |
515 | 515 |
interface. |
516 | 516 |
*/ |
517 | 517 |
|
518 | 518 |
/** |
519 | 519 |
@defgroup lp_utils Tools for Lp and Mip Solvers |
520 | 520 |
@ingroup lp_group |
521 | 521 |
\brief Helper tools to the Lp and Mip solvers. |
522 | 522 |
|
523 | 523 |
This group adds some helper tools to general optimization framework |
524 | 524 |
implemented in LEMON. |
525 | 525 |
*/ |
526 | 526 |
|
527 | 527 |
/** |
528 | 528 |
@defgroup metah Metaheuristics |
529 | 529 |
@ingroup gen_opt_group |
530 | 530 |
\brief Metaheuristics for LEMON library. |
531 | 531 |
|
532 |
This group |
|
532 |
This group contains some metaheuristic optimization tools. |
|
533 | 533 |
*/ |
534 | 534 |
|
535 | 535 |
/** |
536 | 536 |
@defgroup utils Tools and Utilities |
537 | 537 |
\brief Tools and utilities for programming in LEMON |
538 | 538 |
|
539 | 539 |
Tools and utilities for programming in LEMON. |
540 | 540 |
*/ |
541 | 541 |
|
542 | 542 |
/** |
543 | 543 |
@defgroup gutils Basic Graph Utilities |
544 | 544 |
@ingroup utils |
545 | 545 |
\brief Simple basic graph utilities. |
546 | 546 |
|
547 |
This group |
|
547 |
This group contains some simple basic graph utilities. |
|
548 | 548 |
*/ |
549 | 549 |
|
550 | 550 |
/** |
551 | 551 |
@defgroup misc Miscellaneous Tools |
552 | 552 |
@ingroup utils |
553 | 553 |
\brief Tools for development, debugging and testing. |
554 | 554 |
|
555 |
This group |
|
555 |
This group contains several useful tools for development, |
|
556 | 556 |
debugging and testing. |
557 | 557 |
*/ |
558 | 558 |
|
559 | 559 |
/** |
560 | 560 |
@defgroup timecount Time Measuring and Counting |
561 | 561 |
@ingroup misc |
562 | 562 |
\brief Simple tools for measuring the performance of algorithms. |
563 | 563 |
|
564 |
This group |
|
564 |
This group contains simple tools for measuring the performance |
|
565 | 565 |
of algorithms. |
566 | 566 |
*/ |
567 | 567 |
|
568 | 568 |
/** |
569 | 569 |
@defgroup exceptions Exceptions |
570 | 570 |
@ingroup utils |
571 | 571 |
\brief Exceptions defined in LEMON. |
572 | 572 |
|
573 |
This group |
|
573 |
This group contains the exceptions defined in LEMON. |
|
574 | 574 |
*/ |
575 | 575 |
|
576 | 576 |
/** |
577 | 577 |
@defgroup io_group Input-Output |
578 | 578 |
\brief Graph Input-Output methods |
579 | 579 |
|
580 |
This group |
|
580 |
This group contains the tools for importing and exporting graphs |
|
581 | 581 |
and graph related data. Now it supports the \ref lgf-format |
582 | 582 |
"LEMON Graph Format", the \c DIMACS format and the encapsulated |
583 | 583 |
postscript (EPS) format. |
584 | 584 |
*/ |
585 | 585 |
|
586 | 586 |
/** |
587 | 587 |
@defgroup lemon_io LEMON Graph Format |
588 | 588 |
@ingroup io_group |
589 | 589 |
\brief Reading and writing LEMON Graph Format. |
590 | 590 |
|
591 |
This group |
|
591 |
This group contains methods for reading and writing |
|
592 | 592 |
\ref lgf-format "LEMON Graph Format". |
593 | 593 |
*/ |
594 | 594 |
|
595 | 595 |
/** |
596 | 596 |
@defgroup eps_io Postscript Exporting |
597 | 597 |
@ingroup io_group |
598 | 598 |
\brief General \c EPS drawer and graph exporter |
599 | 599 |
|
600 |
This group |
|
600 |
This group contains general \c EPS drawing methods and special |
|
601 | 601 |
graph exporting tools. |
602 | 602 |
*/ |
603 | 603 |
|
604 | 604 |
/** |
605 | 605 |
@defgroup dimacs_group DIMACS format |
606 | 606 |
@ingroup io_group |
607 | 607 |
\brief Read and write files in DIMACS format |
608 | 608 |
|
609 | 609 |
Tools to read a digraph from or write it to a file in DIMACS format data. |
610 | 610 |
*/ |
611 | 611 |
|
612 | 612 |
/** |
613 | 613 |
@defgroup nauty_group NAUTY Format |
614 | 614 |
@ingroup io_group |
615 | 615 |
\brief Read \e Nauty format |
616 | 616 |
|
617 | 617 |
Tool to read graphs from \e Nauty format data. |
618 | 618 |
*/ |
619 | 619 |
|
620 | 620 |
/** |
621 | 621 |
@defgroup concept Concepts |
622 | 622 |
\brief Skeleton classes and concept checking classes |
623 | 623 |
|
624 |
This group |
|
624 |
This group contains the data/algorithm skeletons and concept checking |
|
625 | 625 |
classes implemented in LEMON. |
626 | 626 |
|
627 | 627 |
The purpose of the classes in this group is fourfold. |
628 | 628 |
|
629 | 629 |
- These classes contain the documentations of the %concepts. In order |
630 | 630 |
to avoid document multiplications, an implementation of a concept |
631 | 631 |
simply refers to the corresponding concept class. |
632 | 632 |
|
633 | 633 |
- These classes declare every functions, <tt>typedef</tt>s etc. an |
634 | 634 |
implementation of the %concepts should provide, however completely |
635 | 635 |
without implementations and real data structures behind the |
636 | 636 |
interface. On the other hand they should provide nothing else. All |
637 | 637 |
the algorithms working on a data structure meeting a certain concept |
638 | 638 |
should compile with these classes. (Though it will not run properly, |
639 | 639 |
of course.) In this way it is easily to check if an algorithm |
640 | 640 |
doesn't use any extra feature of a certain implementation. |
641 | 641 |
|
642 | 642 |
- The concept descriptor classes also provide a <em>checker class</em> |
643 | 643 |
that makes it possible to check whether a certain implementation of a |
644 | 644 |
concept indeed provides all the required features. |
645 | 645 |
|
646 | 646 |
- Finally, They can serve as a skeleton of a new implementation of a concept. |
647 | 647 |
*/ |
648 | 648 |
|
649 | 649 |
/** |
650 | 650 |
@defgroup graph_concepts Graph Structure Concepts |
651 | 651 |
@ingroup concept |
652 | 652 |
\brief Skeleton and concept checking classes for graph structures |
653 | 653 |
|
654 |
This group |
|
654 |
This group contains the skeletons and concept checking classes of LEMON's |
|
655 | 655 |
graph structures and helper classes used to implement these. |
656 | 656 |
*/ |
657 | 657 |
|
658 | 658 |
/** |
659 | 659 |
@defgroup map_concepts Map Concepts |
660 | 660 |
@ingroup concept |
661 | 661 |
\brief Skeleton and concept checking classes for maps |
662 | 662 |
|
663 |
This group |
|
663 |
This group contains the skeletons and concept checking classes of maps. |
|
664 | 664 |
*/ |
665 | 665 |
|
666 | 666 |
/** |
667 | 667 |
\anchor demoprograms |
668 | 668 |
|
669 | 669 |
@defgroup demos Demo Programs |
670 | 670 |
|
671 | 671 |
Some demo programs are listed here. Their full source codes can be found in |
672 | 672 |
the \c demo subdirectory of the source tree. |
673 | 673 |
|
674 | 674 |
It order to compile them, use <tt>--enable-demo</tt> configure option when |
675 | 675 |
build the library. |
676 | 676 |
*/ |
677 | 677 |
|
678 | 678 |
/** |
679 | 679 |
@defgroup tools Standalone Utility Applications |
680 | 680 |
|
681 | 681 |
Some utility applications are listed here. |
682 | 682 |
|
683 | 683 |
The standard compilation procedure (<tt>./configure;make</tt>) will compile |
684 | 684 |
them, as well. |
685 | 685 |
*/ |
686 | 686 |
|
687 | 687 |
} |
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 | 5 |
* Copyright (C) 2003-2009 |
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 |
\subsection whatis What is LEMON |
25 | 25 |
|
26 | 26 |
LEMON stands for |
27 | 27 |
<b>L</b>ibrary of <b>E</b>fficient <b>M</b>odels |
28 | 28 |
and <b>O</b>ptimization in <b>N</b>etworks. |
29 | 29 |
It is a C++ template |
30 | 30 |
library aimed at combinatorial optimization tasks which |
31 | 31 |
often involve in working |
32 | 32 |
with graphs. |
33 | 33 |
|
34 | 34 |
<b> |
35 | 35 |
LEMON is an <a class="el" href="http://opensource.org/">open source</a> |
36 | 36 |
project. |
37 | 37 |
You are free to use it in your commercial or |
38 | 38 |
non-commercial applications under very permissive |
39 | 39 |
\ref license "license terms". |
40 | 40 |
</b> |
41 | 41 |
|
42 | 42 |
\subsection howtoread How to read the documentation |
43 | 43 |
|
44 | 44 |
If you want to get a quick start and see the most important features then |
45 | 45 |
take a look at our \ref quicktour |
46 | 46 |
"Quick Tour to LEMON" which will guide you along. |
47 | 47 |
|
48 |
If you already feel like using our library, see the page that tells you |
|
49 |
\ref getstart "How to start using LEMON". |
|
50 |
|
|
51 |
If you |
|
52 |
want to see how LEMON works, see |
|
53 |
some \ref demoprograms "demo programs". |
|
48 |
If you already feel like using our library, see the |
|
49 |
<a class="el" href="http://lemon.cs.elte.hu/pub/tutorial/">LEMON Tutorial</a>. |
|
54 | 50 |
|
55 | 51 |
If you know what you are looking for then try to find it under the |
56 |
<a class="el" href="modules.html">Modules</a> |
|
57 |
section. |
|
52 |
<a class="el" href="modules.html">Modules</a> section. |
|
58 | 53 |
|
59 | 54 |
If you are a user of the old (0.x) series of LEMON, please check out the |
60 | 55 |
\ref migration "Migration Guide" for the backward incompatibilities. |
61 | 56 |
*/ |
... | ... |
@@ -2209,180 +2209,178 @@ |
2209 | 2209 |
/// |
2210 | 2210 |
/// \brief Adaptor class for viewing a digraph as an undirected graph. |
2211 | 2211 |
/// |
2212 | 2212 |
/// Undirector adaptor can be used for viewing a digraph as an undirected |
2213 | 2213 |
/// graph. All arcs of the underlying digraph are showed in the |
2214 | 2214 |
/// adaptor as an edge (and also as a pair of arcs, of course). |
2215 | 2215 |
/// This adaptor conforms to the \ref concepts::Graph "Graph" concept. |
2216 | 2216 |
/// |
2217 | 2217 |
/// The adapted digraph can also be modified through this adaptor |
2218 | 2218 |
/// by adding or removing nodes or edges, unless the \c GR template |
2219 | 2219 |
/// parameter is set to be \c const. |
2220 | 2220 |
/// |
2221 | 2221 |
/// \tparam DGR The type of the adapted digraph. |
2222 | 2222 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
2223 | 2223 |
/// It can also be specified to be \c const. |
2224 | 2224 |
/// |
2225 | 2225 |
/// \note The \c Node type of this adaptor and the adapted digraph are |
2226 | 2226 |
/// convertible to each other, moreover the \c Edge type of the adaptor |
2227 | 2227 |
/// and the \c Arc type of the adapted digraph are also convertible to |
2228 | 2228 |
/// each other. |
2229 | 2229 |
/// (Thus the \c Arc type of the adaptor is convertible to the \c Arc type |
2230 | 2230 |
/// of the adapted digraph.) |
2231 | 2231 |
template<typename DGR> |
2232 | 2232 |
#ifdef DOXYGEN |
2233 | 2233 |
class Undirector { |
2234 | 2234 |
#else |
2235 | 2235 |
class Undirector : |
2236 | 2236 |
public GraphAdaptorExtender<UndirectorBase<DGR> > { |
2237 | 2237 |
#endif |
2238 | 2238 |
public: |
2239 | 2239 |
/// The type of the adapted digraph. |
2240 | 2240 |
typedef DGR Digraph; |
2241 | 2241 |
typedef GraphAdaptorExtender<UndirectorBase<DGR> > Parent; |
2242 | 2242 |
protected: |
2243 | 2243 |
Undirector() { } |
2244 | 2244 |
public: |
2245 | 2245 |
|
2246 | 2246 |
/// \brief Constructor |
2247 | 2247 |
/// |
2248 | 2248 |
/// Creates an undirected graph from the given digraph. |
2249 | 2249 |
Undirector(DGR& digraph) { |
2250 | 2250 |
initialize(digraph); |
2251 | 2251 |
} |
2252 | 2252 |
|
2253 | 2253 |
/// \brief Arc map combined from two original arc maps |
2254 | 2254 |
/// |
2255 | 2255 |
/// This map adaptor class adapts two arc maps of the underlying |
2256 | 2256 |
/// digraph to get an arc map of the undirected graph. |
2257 |
/// Its value type is inherited from the first arc map type |
|
2258 |
/// (\c %ForwardMap). |
|
2259 |
|
|
2257 |
/// Its value type is inherited from the first arc map type (\c FW). |
|
2258 |
/// \tparam FW The type of the "foward" arc map. |
|
2259 |
/// \tparam BK The type of the "backward" arc map. |
|
2260 |
template <typename FW, typename BK> |
|
2260 | 2261 |
class CombinedArcMap { |
2261 | 2262 |
public: |
2262 | 2263 |
|
2263 | 2264 |
/// The key type of the map |
2264 | 2265 |
typedef typename Parent::Arc Key; |
2265 | 2266 |
/// The value type of the map |
2266 |
typedef typename ForwardMap::Value Value; |
|
2267 |
|
|
2268 |
typedef typename MapTraits<ForwardMap>::ReferenceMapTag ReferenceMapTag; |
|
2269 |
|
|
2270 |
typedef typename MapTraits<ForwardMap>::ReturnValue ReturnValue; |
|
2271 |
typedef typename MapTraits<ForwardMap>::ConstReturnValue ConstReturnValue; |
|
2272 |
typedef typename MapTraits<ForwardMap>::ReturnValue Reference; |
|
2273 |
typedef typename MapTraits<ForwardMap>::ConstReturnValue ConstReference; |
|
2267 |
typedef typename FW::Value Value; |
|
2268 |
|
|
2269 |
typedef typename MapTraits<FW>::ReferenceMapTag ReferenceMapTag; |
|
2270 |
|
|
2271 |
typedef typename MapTraits<FW>::ReturnValue ReturnValue; |
|
2272 |
typedef typename MapTraits<FW>::ConstReturnValue ConstReturnValue; |
|
2273 |
typedef typename MapTraits<FW>::ReturnValue Reference; |
|
2274 |
typedef typename MapTraits<FW>::ConstReturnValue ConstReference; |
|
2274 | 2275 |
|
2275 | 2276 |
/// Constructor |
2276 |
CombinedArcMap( |
|
2277 |
CombinedArcMap(FW& forward, BK& backward) |
|
2277 | 2278 |
: _forward(&forward), _backward(&backward) {} |
2278 | 2279 |
|
2279 | 2280 |
/// Sets the value associated with the given key. |
2280 | 2281 |
void set(const Key& e, const Value& a) { |
2281 | 2282 |
if (Parent::direction(e)) { |
2282 | 2283 |
_forward->set(e, a); |
2283 | 2284 |
} else { |
2284 | 2285 |
_backward->set(e, a); |
2285 | 2286 |
} |
2286 | 2287 |
} |
2287 | 2288 |
|
2288 | 2289 |
/// Returns the value associated with the given key. |
2289 | 2290 |
ConstReturnValue operator[](const Key& e) const { |
2290 | 2291 |
if (Parent::direction(e)) { |
2291 | 2292 |
return (*_forward)[e]; |
2292 | 2293 |
} else { |
2293 | 2294 |
return (*_backward)[e]; |
2294 | 2295 |
} |
2295 | 2296 |
} |
2296 | 2297 |
|
2297 | 2298 |
/// Returns a reference to the value associated with the given key. |
2298 | 2299 |
ReturnValue operator[](const Key& e) { |
2299 | 2300 |
if (Parent::direction(e)) { |
2300 | 2301 |
return (*_forward)[e]; |
2301 | 2302 |
} else { |
2302 | 2303 |
return (*_backward)[e]; |
2303 | 2304 |
} |
2304 | 2305 |
} |
2305 | 2306 |
|
2306 | 2307 |
protected: |
2307 | 2308 |
|
2308 |
ForwardMap* _forward; |
|
2309 |
BackwardMap* _backward; |
|
2309 |
FW* _forward; |
|
2310 |
BK* _backward; |
|
2310 | 2311 |
|
2311 | 2312 |
}; |
2312 | 2313 |
|
2313 | 2314 |
/// \brief Returns a combined arc map |
2314 | 2315 |
/// |
2315 | 2316 |
/// This function just returns a combined arc map. |
2316 |
template <typename ForwardMap, typename BackwardMap> |
|
2317 |
static CombinedArcMap<ForwardMap, BackwardMap> |
|
2318 |
combinedArcMap(ForwardMap& forward, BackwardMap& backward) { |
|
2319 |
return CombinedArcMap<ForwardMap, BackwardMap>(forward, backward); |
|
2317 |
template <typename FW, typename BK> |
|
2318 |
static CombinedArcMap<FW, BK> |
|
2319 |
combinedArcMap(FW& forward, BK& backward) { |
|
2320 |
return CombinedArcMap<FW, BK>(forward, backward); |
|
2320 | 2321 |
} |
2321 | 2322 |
|
2322 |
template <typename ForwardMap, typename BackwardMap> |
|
2323 |
static CombinedArcMap<const ForwardMap, BackwardMap> |
|
2324 |
combinedArcMap(const ForwardMap& forward, BackwardMap& backward) { |
|
2325 |
return CombinedArcMap<const ForwardMap, |
|
2326 |
|
|
2323 |
template <typename FW, typename BK> |
|
2324 |
static CombinedArcMap<const FW, BK> |
|
2325 |
combinedArcMap(const FW& forward, BK& backward) { |
|
2326 |
return CombinedArcMap<const FW, BK>(forward, backward); |
|
2327 | 2327 |
} |
2328 | 2328 |
|
2329 |
template <typename ForwardMap, typename BackwardMap> |
|
2330 |
static CombinedArcMap<ForwardMap, const BackwardMap> |
|
2331 |
combinedArcMap(ForwardMap& forward, const BackwardMap& backward) { |
|
2332 |
return CombinedArcMap<ForwardMap, |
|
2333 |
|
|
2329 |
template <typename FW, typename BK> |
|
2330 |
static CombinedArcMap<FW, const BK> |
|
2331 |
combinedArcMap(FW& forward, const BK& backward) { |
|
2332 |
return CombinedArcMap<FW, const BK>(forward, backward); |
|
2334 | 2333 |
} |
2335 | 2334 |
|
2336 |
template <typename ForwardMap, typename BackwardMap> |
|
2337 |
static CombinedArcMap<const ForwardMap, const BackwardMap> |
|
2338 |
combinedArcMap(const ForwardMap& forward, const BackwardMap& backward) { |
|
2339 |
return CombinedArcMap<const ForwardMap, |
|
2340 |
|
|
2335 |
template <typename FW, typename BK> |
|
2336 |
static CombinedArcMap<const FW, const BK> |
|
2337 |
combinedArcMap(const FW& forward, const BK& backward) { |
|
2338 |
return CombinedArcMap<const FW, const BK>(forward, backward); |
|
2341 | 2339 |
} |
2342 | 2340 |
|
2343 | 2341 |
}; |
2344 | 2342 |
|
2345 | 2343 |
/// \brief Returns a read-only Undirector adaptor |
2346 | 2344 |
/// |
2347 | 2345 |
/// This function just returns a read-only \ref Undirector adaptor. |
2348 | 2346 |
/// \ingroup graph_adaptors |
2349 | 2347 |
/// \relates Undirector |
2350 | 2348 |
template<typename DGR> |
2351 | 2349 |
Undirector<const DGR> undirector(const DGR& digraph) { |
2352 | 2350 |
return Undirector<const DGR>(digraph); |
2353 | 2351 |
} |
2354 | 2352 |
|
2355 | 2353 |
|
2356 | 2354 |
template <typename GR, typename DM> |
2357 | 2355 |
class OrienterBase { |
2358 | 2356 |
public: |
2359 | 2357 |
|
2360 | 2358 |
typedef GR Graph; |
2361 | 2359 |
typedef DM DirectionMap; |
2362 | 2360 |
|
2363 | 2361 |
typedef typename GR::Node Node; |
2364 | 2362 |
typedef typename GR::Edge Arc; |
2365 | 2363 |
|
2366 | 2364 |
void reverseArc(const Arc& arc) { |
2367 | 2365 |
_direction->set(arc, !(*_direction)[arc]); |
2368 | 2366 |
} |
2369 | 2367 |
|
2370 | 2368 |
void first(Node& i) const { _graph->first(i); } |
2371 | 2369 |
void first(Arc& i) const { _graph->first(i); } |
2372 | 2370 |
void firstIn(Arc& i, const Node& n) const { |
2373 | 2371 |
bool d = true; |
2374 | 2372 |
_graph->firstInc(i, d, n); |
2375 | 2373 |
while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d); |
2376 | 2374 |
} |
2377 | 2375 |
void firstOut(Arc& i, const Node& n ) const { |
2378 | 2376 |
bool d = true; |
2379 | 2377 |
_graph->firstInc(i, d, n); |
2380 | 2378 |
while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d); |
2381 | 2379 |
} |
2382 | 2380 |
|
2383 | 2381 |
void next(Node& i) const { _graph->next(i); } |
2384 | 2382 |
void next(Arc& i) const { _graph->next(i); } |
2385 | 2383 |
void nextIn(Arc& i) const { |
2386 | 2384 |
bool d = !(*_direction)[i]; |
2387 | 2385 |
_graph->nextInc(i, d); |
2388 | 2386 |
while (i != INVALID && d == (*_direction)[i]) _graph->nextInc(i, d); |
... | ... |
@@ -3361,235 +3359,238 @@ |
3361 | 3359 |
static bool origArc(const Arc& a) { |
3362 | 3360 |
return Parent::origArc(a); |
3363 | 3361 |
} |
3364 | 3362 |
|
3365 | 3363 |
/// \brief Returns \c true if the given arc is a bind arc. |
3366 | 3364 |
/// |
3367 | 3365 |
/// Returns \c true if the given arc is a bind arc, i.e. it connects |
3368 | 3366 |
/// an in-node and an out-node. |
3369 | 3367 |
static bool bindArc(const Arc& a) { |
3370 | 3368 |
return Parent::bindArc(a); |
3371 | 3369 |
} |
3372 | 3370 |
|
3373 | 3371 |
/// \brief Returns the in-node created from the given original node. |
3374 | 3372 |
/// |
3375 | 3373 |
/// Returns the in-node created from the given original node. |
3376 | 3374 |
static Node inNode(const DigraphNode& n) { |
3377 | 3375 |
return Parent::inNode(n); |
3378 | 3376 |
} |
3379 | 3377 |
|
3380 | 3378 |
/// \brief Returns the out-node created from the given original node. |
3381 | 3379 |
/// |
3382 | 3380 |
/// Returns the out-node created from the given original node. |
3383 | 3381 |
static Node outNode(const DigraphNode& n) { |
3384 | 3382 |
return Parent::outNode(n); |
3385 | 3383 |
} |
3386 | 3384 |
|
3387 | 3385 |
/// \brief Returns the bind arc that corresponds to the given |
3388 | 3386 |
/// original node. |
3389 | 3387 |
/// |
3390 | 3388 |
/// Returns the bind arc in the adaptor that corresponds to the given |
3391 | 3389 |
/// original node, i.e. the arc connecting the in-node and out-node |
3392 | 3390 |
/// of \c n. |
3393 | 3391 |
static Arc arc(const DigraphNode& n) { |
3394 | 3392 |
return Parent::arc(n); |
3395 | 3393 |
} |
3396 | 3394 |
|
3397 | 3395 |
/// \brief Returns the arc that corresponds to the given original arc. |
3398 | 3396 |
/// |
3399 | 3397 |
/// Returns the arc in the adaptor that corresponds to the given |
3400 | 3398 |
/// original arc. |
3401 | 3399 |
static Arc arc(const DigraphArc& a) { |
3402 | 3400 |
return Parent::arc(a); |
3403 | 3401 |
} |
3404 | 3402 |
|
3405 | 3403 |
/// \brief Node map combined from two original node maps |
3406 | 3404 |
/// |
3407 | 3405 |
/// This map adaptor class adapts two node maps of the original digraph |
3408 | 3406 |
/// to get a node map of the split digraph. |
3409 |
/// Its value type is inherited from the first node map type |
|
3410 |
/// (\c InNodeMap). |
|
3411 |
|
|
3407 |
/// Its value type is inherited from the first node map type (\c IN). |
|
3408 |
/// \tparam IN The type of the node map for the in-nodes. |
|
3409 |
/// \tparam OUT The type of the node map for the out-nodes. |
|
3410 |
template <typename IN, typename OUT> |
|
3412 | 3411 |
class CombinedNodeMap { |
3413 | 3412 |
public: |
3414 | 3413 |
|
3415 | 3414 |
/// The key type of the map |
3416 | 3415 |
typedef Node Key; |
3417 | 3416 |
/// The value type of the map |
3418 |
typedef typename InNodeMap::Value Value; |
|
3419 |
|
|
3420 |
typedef typename MapTraits<InNodeMap>::ReferenceMapTag ReferenceMapTag; |
|
3421 |
typedef typename MapTraits<InNodeMap>::ReturnValue ReturnValue; |
|
3422 |
typedef typename MapTraits<InNodeMap>::ConstReturnValue ConstReturnValue; |
|
3423 |
typedef typename MapTraits<InNodeMap>::ReturnValue Reference; |
|
3424 |
typedef typename |
|
3417 |
typedef typename IN::Value Value; |
|
3418 |
|
|
3419 |
typedef typename MapTraits<IN>::ReferenceMapTag ReferenceMapTag; |
|
3420 |
typedef typename MapTraits<IN>::ReturnValue ReturnValue; |
|
3421 |
typedef typename MapTraits<IN>::ConstReturnValue ConstReturnValue; |
|
3422 |
typedef typename MapTraits<IN>::ReturnValue Reference; |
|
3423 |
typedef typename MapTraits<IN>::ConstReturnValue ConstReference; |
|
3425 | 3424 |
|
3426 | 3425 |
/// Constructor |
3427 |
CombinedNodeMap( |
|
3426 |
CombinedNodeMap(IN& in_map, OUT& out_map) |
|
3428 | 3427 |
: _in_map(in_map), _out_map(out_map) {} |
3429 | 3428 |
|
3430 | 3429 |
/// Returns the value associated with the given key. |
3431 | 3430 |
Value operator[](const Key& key) const { |
3432 | 3431 |
if (SplitNodesBase<const DGR>::inNode(key)) { |
3433 | 3432 |
return _in_map[key]; |
3434 | 3433 |
} else { |
3435 | 3434 |
return _out_map[key]; |
3436 | 3435 |
} |
3437 | 3436 |
} |
3438 | 3437 |
|
3439 | 3438 |
/// Returns a reference to the value associated with the given key. |
3440 | 3439 |
Value& operator[](const Key& key) { |
3441 | 3440 |
if (SplitNodesBase<const DGR>::inNode(key)) { |
3442 | 3441 |
return _in_map[key]; |
3443 | 3442 |
} else { |
3444 | 3443 |
return _out_map[key]; |
3445 | 3444 |
} |
3446 | 3445 |
} |
3447 | 3446 |
|
3448 | 3447 |
/// Sets the value associated with the given key. |
3449 | 3448 |
void set(const Key& key, const Value& value) { |
3450 | 3449 |
if (SplitNodesBase<const DGR>::inNode(key)) { |
3451 | 3450 |
_in_map.set(key, value); |
3452 | 3451 |
} else { |
3453 | 3452 |
_out_map.set(key, value); |
3454 | 3453 |
} |
3455 | 3454 |
} |
3456 | 3455 |
|
3457 | 3456 |
private: |
3458 | 3457 |
|
3459 |
InNodeMap& _in_map; |
|
3460 |
OutNodeMap& _out_map; |
|
3458 |
IN& _in_map; |
|
3459 |
OUT& _out_map; |
|
3461 | 3460 |
|
3462 | 3461 |
}; |
3463 | 3462 |
|
3464 | 3463 |
|
3465 | 3464 |
/// \brief Returns a combined node map |
3466 | 3465 |
/// |
3467 | 3466 |
/// This function just returns a combined node map. |
3468 |
template <typename InNodeMap, typename OutNodeMap> |
|
3469 |
static CombinedNodeMap<InNodeMap, OutNodeMap> |
|
3470 |
combinedNodeMap(InNodeMap& in_map, OutNodeMap& out_map) { |
|
3471 |
return CombinedNodeMap<InNodeMap, OutNodeMap>(in_map, out_map); |
|
3467 |
template <typename IN, typename OUT> |
|
3468 |
static CombinedNodeMap<IN, OUT> |
|
3469 |
combinedNodeMap(IN& in_map, OUT& out_map) { |
|
3470 |
return CombinedNodeMap<IN, OUT>(in_map, out_map); |
|
3472 | 3471 |
} |
3473 | 3472 |
|
3474 |
template <typename InNodeMap, typename OutNodeMap> |
|
3475 |
static CombinedNodeMap<const InNodeMap, OutNodeMap> |
|
3476 |
combinedNodeMap(const InNodeMap& in_map, OutNodeMap& out_map) { |
|
3477 |
return CombinedNodeMap<const InNodeMap, OutNodeMap>(in_map, out_map); |
|
3473 |
template <typename IN, typename OUT> |
|
3474 |
static CombinedNodeMap<const IN, OUT> |
|
3475 |
combinedNodeMap(const IN& in_map, OUT& out_map) { |
|
3476 |
return CombinedNodeMap<const IN, OUT>(in_map, out_map); |
|
3478 | 3477 |
} |
3479 | 3478 |
|
3480 |
template <typename InNodeMap, typename OutNodeMap> |
|
3481 |
static CombinedNodeMap<InNodeMap, const OutNodeMap> |
|
3482 |
combinedNodeMap(InNodeMap& in_map, const OutNodeMap& out_map) { |
|
3483 |
return CombinedNodeMap<InNodeMap, const OutNodeMap>(in_map, out_map); |
|
3479 |
template <typename IN, typename OUT> |
|
3480 |
static CombinedNodeMap<IN, const OUT> |
|
3481 |
combinedNodeMap(IN& in_map, const OUT& out_map) { |
|
3482 |
return CombinedNodeMap<IN, const OUT>(in_map, out_map); |
|
3484 | 3483 |
} |
3485 | 3484 |
|
3486 |
template <typename InNodeMap, typename OutNodeMap> |
|
3487 |
static CombinedNodeMap<const InNodeMap, const OutNodeMap> |
|
3488 |
combinedNodeMap(const InNodeMap& in_map, const OutNodeMap& out_map) { |
|
3489 |
return CombinedNodeMap<const InNodeMap, |
|
3490 |
|
|
3485 |
template <typename IN, typename OUT> |
|
3486 |
static CombinedNodeMap<const IN, const OUT> |
|
3487 |
combinedNodeMap(const IN& in_map, const OUT& out_map) { |
|
3488 |
return CombinedNodeMap<const IN, const OUT>(in_map, out_map); |
|
3491 | 3489 |
} |
3492 | 3490 |
|
3493 | 3491 |
/// \brief Arc map combined from an arc map and a node map of the |
3494 | 3492 |
/// original digraph. |
3495 | 3493 |
/// |
3496 | 3494 |
/// This map adaptor class adapts an arc map and a node map of the |
3497 | 3495 |
/// original digraph to get an arc map of the split digraph. |
3498 |
/// Its value type is inherited from the original arc map type |
|
3499 |
/// (\c ArcMap). |
|
3500 |
|
|
3496 |
/// Its value type is inherited from the original arc map type (\c AM). |
|
3497 |
/// \tparam AM The type of the arc map. |
|
3498 |
/// \tparam NM the type of the node map. |
|
3499 |
template <typename AM, typename NM> |
|
3501 | 3500 |
class CombinedArcMap { |
3502 | 3501 |
public: |
3503 | 3502 |
|
3504 | 3503 |
/// The key type of the map |
3505 | 3504 |
typedef Arc Key; |
3506 | 3505 |
/// The value type of the map |
3507 |
typedef typename ArcMap::Value Value; |
|
3508 |
|
|
3509 |
typedef typename MapTraits<ArcMap>::ReferenceMapTag ReferenceMapTag; |
|
3510 |
typedef typename MapTraits<ArcMap>::ReturnValue ReturnValue; |
|
3511 |
typedef typename MapTraits<ArcMap>::ConstReturnValue ConstReturnValue; |
|
3512 |
typedef typename MapTraits<ArcMap>::ReturnValue Reference; |
|
3513 |
typedef typename |
|
3506 |
typedef typename AM::Value Value; |
|
3507 |
|
|
3508 |
typedef typename MapTraits<AM>::ReferenceMapTag ReferenceMapTag; |
|
3509 |
typedef typename MapTraits<AM>::ReturnValue ReturnValue; |
|
3510 |
typedef typename MapTraits<AM>::ConstReturnValue ConstReturnValue; |
|
3511 |
typedef typename MapTraits<AM>::ReturnValue Reference; |
|
3512 |
typedef typename MapTraits<AM>::ConstReturnValue ConstReference; |
|
3514 | 3513 |
|
3515 | 3514 |
/// Constructor |
3516 |
CombinedArcMap( |
|
3515 |
CombinedArcMap(AM& arc_map, NM& node_map) |
|
3517 | 3516 |
: _arc_map(arc_map), _node_map(node_map) {} |
3518 | 3517 |
|
3519 | 3518 |
/// Returns the value associated with the given key. |
3520 | 3519 |
Value operator[](const Key& arc) const { |
3521 | 3520 |
if (SplitNodesBase<const DGR>::origArc(arc)) { |
3522 | 3521 |
return _arc_map[arc]; |
3523 | 3522 |
} else { |
3524 | 3523 |
return _node_map[arc]; |
3525 | 3524 |
} |
3526 | 3525 |
} |
3527 | 3526 |
|
3528 | 3527 |
/// Returns a reference to the value associated with the given key. |
3529 | 3528 |
Value& operator[](const Key& arc) { |
3530 | 3529 |
if (SplitNodesBase<const DGR>::origArc(arc)) { |
3531 | 3530 |
return _arc_map[arc]; |
3532 | 3531 |
} else { |
3533 | 3532 |
return _node_map[arc]; |
3534 | 3533 |
} |
3535 | 3534 |
} |
3536 | 3535 |
|
3537 | 3536 |
/// Sets the value associated with the given key. |
3538 | 3537 |
void set(const Arc& arc, const Value& val) { |
3539 | 3538 |
if (SplitNodesBase<const DGR>::origArc(arc)) { |
3540 | 3539 |
_arc_map.set(arc, val); |
3541 | 3540 |
} else { |
3542 | 3541 |
_node_map.set(arc, val); |
3543 | 3542 |
} |
3544 | 3543 |
} |
3545 | 3544 |
|
3546 | 3545 |
private: |
3547 |
ArcMap& _arc_map; |
|
3548 |
NodeMap& _node_map; |
|
3546 |
|
|
3547 |
AM& _arc_map; |
|
3548 |
NM& _node_map; |
|
3549 |
|
|
3549 | 3550 |
}; |
3550 | 3551 |
|
3551 | 3552 |
/// \brief Returns a combined arc map |
3552 | 3553 |
/// |
3553 | 3554 |
/// This function just returns a combined arc map. |
3554 | 3555 |
template <typename ArcMap, typename NodeMap> |
3555 | 3556 |
static CombinedArcMap<ArcMap, NodeMap> |
3556 | 3557 |
combinedArcMap(ArcMap& arc_map, NodeMap& node_map) { |
3557 | 3558 |
return CombinedArcMap<ArcMap, NodeMap>(arc_map, node_map); |
3558 | 3559 |
} |
3559 | 3560 |
|
3560 | 3561 |
template <typename ArcMap, typename NodeMap> |
3561 | 3562 |
static CombinedArcMap<const ArcMap, NodeMap> |
3562 | 3563 |
combinedArcMap(const ArcMap& arc_map, NodeMap& node_map) { |
3563 | 3564 |
return CombinedArcMap<const ArcMap, NodeMap>(arc_map, node_map); |
3564 | 3565 |
} |
3565 | 3566 |
|
3566 | 3567 |
template <typename ArcMap, typename NodeMap> |
3567 | 3568 |
static CombinedArcMap<ArcMap, const NodeMap> |
3568 | 3569 |
combinedArcMap(ArcMap& arc_map, const NodeMap& node_map) { |
3569 | 3570 |
return CombinedArcMap<ArcMap, const NodeMap>(arc_map, node_map); |
3570 | 3571 |
} |
3571 | 3572 |
|
3572 | 3573 |
template <typename ArcMap, typename NodeMap> |
3573 | 3574 |
static CombinedArcMap<const ArcMap, const NodeMap> |
3574 | 3575 |
combinedArcMap(const ArcMap& arc_map, const NodeMap& node_map) { |
3575 | 3576 |
return CombinedArcMap<const ArcMap, const NodeMap>(arc_map, node_map); |
3576 | 3577 |
} |
3577 | 3578 |
|
3578 | 3579 |
}; |
3579 | 3580 |
|
3580 | 3581 |
/// \brief Returns a (read-only) SplitNodes adaptor |
3581 | 3582 |
/// |
3582 | 3583 |
/// This function just returns a (read-only) \ref SplitNodes adaptor. |
3583 | 3584 |
/// \ingroup graph_adaptors |
3584 | 3585 |
/// \relates SplitNodes |
3585 | 3586 |
template<typename DGR> |
3586 | 3587 |
SplitNodes<DGR> |
3587 | 3588 |
splitNodes(const DGR& digraph) { |
3588 | 3589 |
return SplitNodes<DGR>(digraph); |
3589 | 3590 |
} |
3590 | 3591 |
|
3591 | 3592 |
#undef LEMON_SCOPE_FIX |
3592 | 3593 |
|
3593 | 3594 |
} //namespace lemon |
3594 | 3595 |
|
3595 | 3596 |
#endif //LEMON_ADAPTORS_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
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_BIN_HEAP_H |
20 | 20 |
#define LEMON_BIN_HEAP_H |
21 | 21 |
|
22 | 22 |
///\ingroup auxdat |
23 | 23 |
///\file |
24 | 24 |
///\brief Binary 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 |
///\ingroup auxdat |
33 | 33 |
/// |
34 | 34 |
///\brief A Binary Heap implementation. |
35 | 35 |
/// |
36 |
///This class implements the \e binary \e heap data structure. A \e heap |
|
37 |
///is a data structure for storing items with specified values called \e |
|
38 |
///priorities in such a way that finding the item with minimum priority is |
|
39 |
///efficient. \c Compare specifies the ordering of the priorities. In a heap |
|
40 |
/// |
|
36 |
///This class implements the \e binary \e heap data structure. |
|
37 |
/// |
|
38 |
///A \e heap is a data structure for storing items with specified values |
|
39 |
///called \e priorities in such a way that finding the item with minimum |
|
40 |
///priority is efficient. \c Comp specifies the ordering of the priorities. |
|
41 |
///In a heap one can change the priority of an item, add or erase an |
|
42 |
///item, etc. |
|
41 | 43 |
/// |
42 |
///\tparam _Prio Type of the priority of the items. |
|
43 |
///\tparam _ItemIntMap A read and writable Item int map, used internally |
|
44 |
///\tparam PR Type of the priority of the items. |
|
45 |
///\tparam IM A read and writable item map with int values, used internally |
|
44 | 46 |
///to handle the cross references. |
45 |
///\tparam _Compare A class for the ordering of the priorities. The |
|
46 |
///default is \c std::less<_Prio>. |
|
47 |
///\tparam Comp A functor class for the ordering of the priorities. |
|
48 |
///The default is \c std::less<PR>. |
|
47 | 49 |
/// |
48 | 50 |
///\sa FibHeap |
49 | 51 |
///\sa Dijkstra |
50 |
template <typename _Prio, typename _ItemIntMap, |
|
51 |
typename _Compare = std::less<_Prio> > |
|
52 |
template <typename PR, typename IM, typename Comp = std::less<PR> > |
|
52 | 53 |
class BinHeap { |
53 | 54 |
|
54 | 55 |
public: |
55 | 56 |
///\e |
56 |
typedef |
|
57 |
typedef IM ItemIntMap; |
|
57 | 58 |
///\e |
58 |
typedef |
|
59 |
typedef PR Prio; |
|
59 | 60 |
///\e |
60 | 61 |
typedef typename ItemIntMap::Key Item; |
61 | 62 |
///\e |
62 | 63 |
typedef std::pair<Item,Prio> Pair; |
63 | 64 |
///\e |
64 |
typedef |
|
65 |
typedef Comp Compare; |
|
65 | 66 |
|
66 | 67 |
/// \brief Type to represent the items states. |
67 | 68 |
/// |
68 | 69 |
/// Each Item element have a state associated to it. It may be "in heap", |
69 | 70 |
/// "pre heap" or "post heap". The latter two are indifferent from the |
70 | 71 |
/// heap's point of view, but may be useful to the user. |
71 | 72 |
/// |
72 |
/// The ItemIntMap \e should be initialized in such way that it maps |
|
73 |
/// PRE_HEAP (-1) to any element to be put in the heap... |
|
73 |
/// The item-int map must be initialized in such way that it assigns |
|
74 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
|
74 | 75 |
enum State { |
75 |
IN_HEAP = 0, |
|
76 |
PRE_HEAP = -1, |
|
77 |
|
|
76 |
IN_HEAP = 0, ///< \e |
|
77 |
PRE_HEAP = -1, ///< \e |
|
78 |
POST_HEAP = -2 ///< \e |
|
78 | 79 |
}; |
79 | 80 |
|
80 | 81 |
private: |
81 |
std::vector<Pair> data; |
|
82 |
Compare comp; |
|
83 |
|
|
82 |
std::vector<Pair> _data; |
|
83 |
Compare _comp; |
|
84 |
ItemIntMap &_iim; |
|
84 | 85 |
|
85 | 86 |
public: |
86 | 87 |
/// \brief The constructor. |
87 | 88 |
/// |
88 | 89 |
/// The constructor. |
89 |
/// \param |
|
90 |
/// \param map should be given to the constructor, since it is used |
|
90 | 91 |
/// internally to handle the cross references. The value of the map |
91 |
/// should be PRE_HEAP (-1) for each element. |
|
92 |
explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {} |
|
92 |
/// must be \c PRE_HEAP (<tt>-1</tt>) for every item. |
|
93 |
explicit BinHeap(ItemIntMap &map) : _iim(map) {} |
|
93 | 94 |
|
94 | 95 |
/// \brief The constructor. |
95 | 96 |
/// |
96 | 97 |
/// The constructor. |
97 |
/// \param |
|
98 |
/// \param map should be given to the constructor, since it is used |
|
98 | 99 |
/// internally to handle the cross references. The value of the map |
99 | 100 |
/// should be PRE_HEAP (-1) for each element. |
100 | 101 |
/// |
101 |
/// \param _comp The comparator function object. |
|
102 |
BinHeap(ItemIntMap &_iim, const Compare &_comp) |
|
103 |
|
|
102 |
/// \param comp The comparator function object. |
|
103 |
BinHeap(ItemIntMap &map, const Compare &comp) |
|
104 |
: _iim(map), _comp(comp) {} |
|
104 | 105 |
|
105 | 106 |
|
106 | 107 |
/// The number of items stored in the heap. |
107 | 108 |
/// |
108 | 109 |
/// \brief Returns the number of items stored in the heap. |
109 |
int size() const { return |
|
110 |
int size() const { return _data.size(); } |
|
110 | 111 |
|
111 | 112 |
/// \brief Checks if the heap stores no items. |
112 | 113 |
/// |
113 | 114 |
/// Returns \c true if and only if the heap stores no items. |
114 |
bool empty() const { return |
|
115 |
bool empty() const { return _data.empty(); } |
|
115 | 116 |
|
116 | 117 |
/// \brief Make empty this heap. |
117 | 118 |
/// |
118 | 119 |
/// Make empty this heap. It does not change the cross reference map. |
119 | 120 |
/// If you want to reuse what is not surely empty you should first clear |
120 | 121 |
/// the heap and after that you should set the cross reference map for |
121 | 122 |
/// each item to \c PRE_HEAP. |
122 | 123 |
void clear() { |
123 |
|
|
124 |
_data.clear(); |
|
124 | 125 |
} |
125 | 126 |
|
126 | 127 |
private: |
127 | 128 |
static int parent(int i) { return (i-1)/2; } |
128 | 129 |
|
129 | 130 |
static int second_child(int i) { return 2*i+2; } |
130 | 131 |
bool less(const Pair &p1, const Pair &p2) const { |
131 |
return |
|
132 |
return _comp(p1.second, p2.second); |
|
132 | 133 |
} |
133 | 134 |
|
134 | 135 |
int bubble_up(int hole, Pair p) { |
135 | 136 |
int par = parent(hole); |
136 |
while( hole>0 && less(p,data[par]) ) { |
|
137 |
move(data[par],hole); |
|
137 |
while( hole>0 && less(p,_data[par]) ) { |
|
138 |
move(_data[par],hole); |
|
138 | 139 |
hole = par; |
139 | 140 |
par = parent(hole); |
140 | 141 |
} |
141 | 142 |
move(p, hole); |
142 | 143 |
return hole; |
143 | 144 |
} |
144 | 145 |
|
145 | 146 |
int bubble_down(int hole, Pair p, int length) { |
146 | 147 |
int child = second_child(hole); |
147 | 148 |
while(child < length) { |
148 |
if( less( |
|
149 |
if( less(_data[child-1], _data[child]) ) { |
|
149 | 150 |
--child; |
150 | 151 |
} |
151 |
if( !less( |
|
152 |
if( !less(_data[child], p) ) |
|
152 | 153 |
goto ok; |
153 |
move( |
|
154 |
move(_data[child], hole); |
|
154 | 155 |
hole = child; |
155 | 156 |
child = second_child(hole); |
156 | 157 |
} |
157 | 158 |
child--; |
158 |
if( child<length && less(data[child], p) ) { |
|
159 |
move(data[child], hole); |
|
159 |
if( child<length && less(_data[child], p) ) { |
|
160 |
move(_data[child], hole); |
|
160 | 161 |
hole=child; |
161 | 162 |
} |
162 | 163 |
ok: |
163 | 164 |
move(p, hole); |
164 | 165 |
return hole; |
165 | 166 |
} |
166 | 167 |
|
167 | 168 |
void move(const Pair &p, int i) { |
168 |
data[i] = p; |
|
169 |
iim.set(p.first, i); |
|
169 |
_data[i] = p; |
|
170 |
_iim.set(p.first, i); |
|
170 | 171 |
} |
171 | 172 |
|
172 | 173 |
public: |
173 | 174 |
/// \brief Insert a pair of item and priority into the heap. |
174 | 175 |
/// |
175 | 176 |
/// Adds \c p.first to the heap with priority \c p.second. |
176 | 177 |
/// \param p The pair to insert. |
177 | 178 |
void push(const Pair &p) { |
178 |
int n = data.size(); |
|
179 |
data.resize(n+1); |
|
179 |
int n = _data.size(); |
|
180 |
_data.resize(n+1); |
|
180 | 181 |
bubble_up(n, p); |
181 | 182 |
} |
182 | 183 |
|
183 | 184 |
/// \brief Insert an item into the heap with the given heap. |
184 | 185 |
/// |
185 | 186 |
/// Adds \c i to the heap with priority \c p. |
186 | 187 |
/// \param i The item to insert. |
187 | 188 |
/// \param p The priority of the item. |
188 | 189 |
void push(const Item &i, const Prio &p) { push(Pair(i,p)); } |
189 | 190 |
|
190 | 191 |
/// \brief Returns the item with minimum priority relative to \c Compare. |
191 | 192 |
/// |
192 | 193 |
/// This method returns the item with minimum priority relative to \c |
193 | 194 |
/// Compare. |
194 | 195 |
/// \pre The heap must be nonempty. |
195 | 196 |
Item top() const { |
196 |
return |
|
197 |
return _data[0].first; |
|
197 | 198 |
} |
198 | 199 |
|
199 | 200 |
/// \brief Returns the minimum priority relative to \c Compare. |
200 | 201 |
/// |
201 | 202 |
/// It returns the minimum priority relative to \c Compare. |
202 | 203 |
/// \pre The heap must be nonempty. |
203 | 204 |
Prio prio() const { |
204 |
return |
|
205 |
return _data[0].second; |
|
205 | 206 |
} |
206 | 207 |
|
207 | 208 |
/// \brief Deletes the item with minimum priority relative to \c Compare. |
208 | 209 |
/// |
209 | 210 |
/// This method deletes the item with minimum priority relative to \c |
210 | 211 |
/// Compare from the heap. |
211 | 212 |
/// \pre The heap must be non-empty. |
212 | 213 |
void pop() { |
213 |
int n = data.size()-1; |
|
214 |
iim.set(data[0].first, POST_HEAP); |
|
214 |
int n = _data.size()-1; |
|
215 |
_iim.set(_data[0].first, POST_HEAP); |
|
215 | 216 |
if (n > 0) { |
216 |
bubble_down(0, |
|
217 |
bubble_down(0, _data[n], n); |
|
217 | 218 |
} |
218 |
|
|
219 |
_data.pop_back(); |
|
219 | 220 |
} |
220 | 221 |
|
221 | 222 |
/// \brief Deletes \c i from the heap. |
222 | 223 |
/// |
223 | 224 |
/// This method deletes item \c i from the heap. |
224 | 225 |
/// \param i The item to erase. |
225 | 226 |
/// \pre The item should be in the heap. |
226 | 227 |
void erase(const Item &i) { |
227 |
int h = iim[i]; |
|
228 |
int n = data.size()-1; |
|
229 |
|
|
228 |
int h = _iim[i]; |
|
229 |
int n = _data.size()-1; |
|
230 |
_iim.set(_data[h].first, POST_HEAP); |
|
230 | 231 |
if( h < n ) { |
231 |
if ( bubble_up(h, data[n]) == h) { |
|
232 |
bubble_down(h, data[n], n); |
|
232 |
if ( bubble_up(h, _data[n]) == h) { |
|
233 |
bubble_down(h, _data[n], n); |
|
233 | 234 |
} |
234 | 235 |
} |
235 |
|
|
236 |
_data.pop_back(); |
|
236 | 237 |
} |
237 | 238 |
|
238 | 239 |
|
239 | 240 |
/// \brief Returns the priority of \c i. |
240 | 241 |
/// |
241 | 242 |
/// This function returns the priority of item \c i. |
243 |
/// \param i The item. |
|
242 | 244 |
/// \pre \c i must be in the heap. |
243 |
/// \param i The item. |
|
244 | 245 |
Prio operator[](const Item &i) const { |
245 |
int idx = iim[i]; |
|
246 |
return data[idx].second; |
|
246 |
int idx = _iim[i]; |
|
247 |
return _data[idx].second; |
|
247 | 248 |
} |
248 | 249 |
|
249 | 250 |
/// \brief \c i gets to the heap with priority \c p independently |
250 | 251 |
/// if \c i was already there. |
251 | 252 |
/// |
252 | 253 |
/// This method calls \ref push(\c i, \c p) if \c i is not stored |
253 | 254 |
/// in the heap and sets the priority of \c i to \c p otherwise. |
254 | 255 |
/// \param i The item. |
255 | 256 |
/// \param p The priority. |
256 | 257 |
void set(const Item &i, const Prio &p) { |
257 |
int idx = |
|
258 |
int idx = _iim[i]; |
|
258 | 259 |
if( idx < 0 ) { |
259 | 260 |
push(i,p); |
260 | 261 |
} |
261 |
else if( |
|
262 |
else if( _comp(p, _data[idx].second) ) { |
|
262 | 263 |
bubble_up(idx, Pair(i,p)); |
263 | 264 |
} |
264 | 265 |
else { |
265 |
bubble_down(idx, Pair(i,p), |
|
266 |
bubble_down(idx, Pair(i,p), _data.size()); |
|
266 | 267 |
} |
267 | 268 |
} |
268 | 269 |
|
269 | 270 |
/// \brief Decreases the priority of \c i to \c p. |
270 | 271 |
/// |
271 | 272 |
/// This method decreases the priority of item \c i to \c p. |
273 |
/// \param i The item. |
|
274 |
/// \param p The priority. |
|
272 | 275 |
/// \pre \c i must be stored in the heap with priority at least \c |
273 | 276 |
/// p relative to \c Compare. |
274 |
/// \param i The item. |
|
275 |
/// \param p The priority. |
|
276 | 277 |
void decrease(const Item &i, const Prio &p) { |
277 |
int idx = |
|
278 |
int idx = _iim[i]; |
|
278 | 279 |
bubble_up(idx, Pair(i,p)); |
279 | 280 |
} |
280 | 281 |
|
281 | 282 |
/// \brief Increases the priority of \c i to \c p. |
282 | 283 |
/// |
283 | 284 |
/// This method sets the priority of item \c i to \c p. |
285 |
/// \param i The item. |
|
286 |
/// \param p The priority. |
|
284 | 287 |
/// \pre \c i must be stored in the heap with priority at most \c |
285 | 288 |
/// p relative to \c Compare. |
286 |
/// \param i The item. |
|
287 |
/// \param p The priority. |
|
288 | 289 |
void increase(const Item &i, const Prio &p) { |
289 |
int idx = iim[i]; |
|
290 |
bubble_down(idx, Pair(i,p), data.size()); |
|
290 |
int idx = _iim[i]; |
|
291 |
bubble_down(idx, Pair(i,p), _data.size()); |
|
291 | 292 |
} |
292 | 293 |
|
293 | 294 |
/// \brief Returns if \c item is in, has already been in, or has |
294 | 295 |
/// never been in the heap. |
295 | 296 |
/// |
296 | 297 |
/// This method returns PRE_HEAP if \c item has never been in the |
297 | 298 |
/// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP |
298 | 299 |
/// otherwise. In the latter case it is possible that \c item will |
299 | 300 |
/// get back to the heap again. |
300 | 301 |
/// \param i The item. |
301 | 302 |
State state(const Item &i) const { |
302 |
int s = |
|
303 |
int s = _iim[i]; |
|
303 | 304 |
if( s>=0 ) |
304 | 305 |
s=0; |
305 | 306 |
return State(s); |
306 | 307 |
} |
307 | 308 |
|
308 | 309 |
/// \brief Sets the state of the \c item in the heap. |
309 | 310 |
/// |
310 | 311 |
/// Sets the state of the \c item in the heap. It can be used to |
311 | 312 |
/// manually clear the heap when it is important to achive the |
312 | 313 |
/// better time complexity. |
313 | 314 |
/// \param i The item. |
314 | 315 |
/// \param st The state. It should not be \c IN_HEAP. |
315 | 316 |
void state(const Item& i, State st) { |
316 | 317 |
switch (st) { |
317 | 318 |
case POST_HEAP: |
318 | 319 |
case PRE_HEAP: |
319 | 320 |
if (state(i) == IN_HEAP) { |
320 | 321 |
erase(i); |
321 | 322 |
} |
322 |
|
|
323 |
_iim[i] = st; |
|
323 | 324 |
break; |
324 | 325 |
case IN_HEAP: |
325 | 326 |
break; |
326 | 327 |
} |
327 | 328 |
} |
328 | 329 |
|
329 | 330 |
/// \brief Replaces an item in the heap. |
330 | 331 |
/// |
331 | 332 |
/// The \c i item is replaced with \c j item. The \c i item should |
332 | 333 |
/// be in the heap, while the \c j should be out of the heap. The |
333 | 334 |
/// \c i item will out of the heap and \c j will be in the heap |
334 | 335 |
/// with the same prioriority as prevoiusly the \c i item. |
335 | 336 |
void replace(const Item& i, const Item& j) { |
336 |
int idx = iim[i]; |
|
337 |
iim.set(i, iim[j]); |
|
338 |
iim.set(j, idx); |
|
339 |
data[idx].first = j; |
|
337 |
int idx = _iim[i]; |
|
338 |
_iim.set(i, _iim[j]); |
|
339 |
_iim.set(j, idx); |
|
340 |
_data[idx].first = j; |
|
340 | 341 |
} |
341 | 342 |
|
342 | 343 |
}; // class BinHeap |
343 | 344 |
|
344 | 345 |
} // namespace lemon |
345 | 346 |
|
346 | 347 |
#endif // LEMON_BIN_HEAP_H |
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
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 |
///\ingroup digraphbits |
|
28 |
///\file |
|
29 |
|
|
27 |
//\ingroup digraphbits |
|
28 |
//\file |
|
29 |
//\brief Extenders for the arc set types |
|
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 |
/// \ingroup digraphbits |
|
33 |
/// |
|
34 |
// |
|
32 |
// \ingroup digraphbits |
|
33 |
// |
|
34 |
// \brief Extender for the ArcSets |
|
35 | 35 |
template <typename Base> |
36 | 36 |
class ArcSetExtender : public Base { |
37 | 37 |
public: |
38 | 38 |
|
39 | 39 |
typedef Base Parent; |
40 | 40 |
typedef ArcSetExtender Digraph; |
41 | 41 |
|
42 | 42 |
// Base extensions |
43 | 43 |
|
44 | 44 |
typedef typename Parent::Node Node; |
45 | 45 |
typedef typename Parent::Arc Arc; |
46 | 46 |
|
47 | 47 |
int maxId(Node) const { |
48 | 48 |
return Parent::maxNodeId(); |
49 | 49 |
} |
50 | 50 |
|
51 | 51 |
int maxId(Arc) const { |
52 | 52 |
return Parent::maxArcId(); |
53 | 53 |
} |
54 | 54 |
|
55 | 55 |
Node fromId(int id, Node) const { |
56 | 56 |
return Parent::nodeFromId(id); |
57 | 57 |
} |
58 | 58 |
|
59 | 59 |
Arc fromId(int id, Arc) const { |
60 | 60 |
return Parent::arcFromId(id); |
61 | 61 |
} |
62 | 62 |
|
63 | 63 |
Node oppositeNode(const Node &n, const Arc &e) const { |
64 | 64 |
if (n == Parent::source(e)) |
65 | 65 |
return Parent::target(e); |
66 | 66 |
else if(n==Parent::target(e)) |
67 | 67 |
return Parent::source(e); |
68 | 68 |
else |
69 | 69 |
return INVALID; |
70 | 70 |
} |
71 | 71 |
|
72 | 72 |
|
73 | 73 |
// Alteration notifier extensions |
74 | 74 |
|
75 |
|
|
75 |
// The arc observer registry. |
|
76 | 76 |
typedef AlterationNotifier<ArcSetExtender, Arc> ArcNotifier; |
77 | 77 |
|
78 | 78 |
protected: |
79 | 79 |
|
80 | 80 |
mutable ArcNotifier arc_notifier; |
81 | 81 |
|
82 | 82 |
public: |
83 | 83 |
|
84 | 84 |
using Parent::notifier; |
85 | 85 |
|
86 |
/// \brief Gives back the arc alteration notifier. |
|
87 |
/// |
|
88 |
|
|
86 |
// Gives back the arc alteration notifier. |
|
89 | 87 |
ArcNotifier& notifier(Arc) const { |
90 | 88 |
return arc_notifier; |
91 | 89 |
} |
92 | 90 |
|
93 | 91 |
// Iterable extensions |
94 | 92 |
|
95 | 93 |
class NodeIt : public Node { |
96 | 94 |
const Digraph* digraph; |
97 | 95 |
public: |
98 | 96 |
|
99 | 97 |
NodeIt() {} |
100 | 98 |
|
101 | 99 |
NodeIt(Invalid i) : Node(i) { } |
102 | 100 |
|
103 | 101 |
explicit NodeIt(const Digraph& _graph) : digraph(&_graph) { |
104 | 102 |
_graph.first(static_cast<Node&>(*this)); |
105 | 103 |
} |
106 | 104 |
|
107 | 105 |
NodeIt(const Digraph& _graph, const Node& node) |
108 | 106 |
: Node(node), digraph(&_graph) {} |
109 | 107 |
|
110 | 108 |
NodeIt& operator++() { |
111 | 109 |
digraph->next(*this); |
112 | 110 |
return *this; |
113 | 111 |
} |
114 | 112 |
|
115 | 113 |
}; |
116 | 114 |
|
117 | 115 |
|
118 | 116 |
class ArcIt : public Arc { |
119 | 117 |
const Digraph* digraph; |
120 | 118 |
public: |
121 | 119 |
|
122 | 120 |
ArcIt() { } |
123 | 121 |
|
124 | 122 |
ArcIt(Invalid i) : Arc(i) { } |
125 | 123 |
|
126 | 124 |
explicit ArcIt(const Digraph& _graph) : digraph(&_graph) { |
127 | 125 |
_graph.first(static_cast<Arc&>(*this)); |
128 | 126 |
} |
129 | 127 |
|
130 | 128 |
ArcIt(const Digraph& _graph, const Arc& e) : |
131 | 129 |
Arc(e), digraph(&_graph) { } |
132 | 130 |
|
133 | 131 |
ArcIt& operator++() { |
134 | 132 |
digraph->next(*this); |
135 | 133 |
return *this; |
136 | 134 |
} |
... | ... |
@@ -140,185 +138,185 @@ |
140 | 138 |
|
141 | 139 |
class OutArcIt : public Arc { |
142 | 140 |
const Digraph* digraph; |
143 | 141 |
public: |
144 | 142 |
|
145 | 143 |
OutArcIt() { } |
146 | 144 |
|
147 | 145 |
OutArcIt(Invalid i) : Arc(i) { } |
148 | 146 |
|
149 | 147 |
OutArcIt(const Digraph& _graph, const Node& node) |
150 | 148 |
: digraph(&_graph) { |
151 | 149 |
_graph.firstOut(*this, node); |
152 | 150 |
} |
153 | 151 |
|
154 | 152 |
OutArcIt(const Digraph& _graph, const Arc& arc) |
155 | 153 |
: Arc(arc), digraph(&_graph) {} |
156 | 154 |
|
157 | 155 |
OutArcIt& operator++() { |
158 | 156 |
digraph->nextOut(*this); |
159 | 157 |
return *this; |
160 | 158 |
} |
161 | 159 |
|
162 | 160 |
}; |
163 | 161 |
|
164 | 162 |
|
165 | 163 |
class InArcIt : public Arc { |
166 | 164 |
const Digraph* digraph; |
167 | 165 |
public: |
168 | 166 |
|
169 | 167 |
InArcIt() { } |
170 | 168 |
|
171 | 169 |
InArcIt(Invalid i) : Arc(i) { } |
172 | 170 |
|
173 | 171 |
InArcIt(const Digraph& _graph, const Node& node) |
174 | 172 |
: digraph(&_graph) { |
175 | 173 |
_graph.firstIn(*this, node); |
176 | 174 |
} |
177 | 175 |
|
178 | 176 |
InArcIt(const Digraph& _graph, const Arc& arc) : |
179 | 177 |
Arc(arc), digraph(&_graph) {} |
180 | 178 |
|
181 | 179 |
InArcIt& operator++() { |
182 | 180 |
digraph->nextIn(*this); |
183 | 181 |
return *this; |
184 | 182 |
} |
185 | 183 |
|
186 | 184 |
}; |
187 | 185 |
|
188 |
/// \brief Base node of the iterator |
|
189 |
/// |
|
190 |
// |
|
186 |
// \brief Base node of the iterator |
|
187 |
// |
|
188 |
// Returns the base node (ie. the source in this case) of the iterator |
|
191 | 189 |
Node baseNode(const OutArcIt &e) const { |
192 | 190 |
return Parent::source(static_cast<const Arc&>(e)); |
193 | 191 |
} |
194 |
/// \brief Running node of the iterator |
|
195 |
/// |
|
196 |
/// Returns the running node (ie. the target in this case) of the |
|
197 |
/// iterator |
|
192 |
// \brief Running node of the iterator |
|
193 |
// |
|
194 |
// Returns the running node (ie. the target in this case) of the |
|
195 |
// iterator |
|
198 | 196 |
Node runningNode(const OutArcIt &e) const { |
199 | 197 |
return Parent::target(static_cast<const Arc&>(e)); |
200 | 198 |
} |
201 | 199 |
|
202 |
/// \brief Base node of the iterator |
|
203 |
/// |
|
204 |
// |
|
200 |
// \brief Base node of the iterator |
|
201 |
// |
|
202 |
// Returns the base node (ie. the target in this case) of the iterator |
|
205 | 203 |
Node baseNode(const InArcIt &e) const { |
206 | 204 |
return Parent::target(static_cast<const Arc&>(e)); |
207 | 205 |
} |
208 |
/// \brief Running node of the iterator |
|
209 |
/// |
|
210 |
/// Returns the running node (ie. the source in this case) of the |
|
211 |
/// iterator |
|
206 |
// \brief Running node of the iterator |
|
207 |
// |
|
208 |
// Returns the running node (ie. the source in this case) of the |
|
209 |
// iterator |
|
212 | 210 |
Node runningNode(const InArcIt &e) const { |
213 | 211 |
return Parent::source(static_cast<const Arc&>(e)); |
214 | 212 |
} |
215 | 213 |
|
216 | 214 |
using Parent::first; |
217 | 215 |
|
218 | 216 |
// Mappable extension |
219 | 217 |
|
220 | 218 |
template <typename _Value> |
221 | 219 |
class ArcMap |
222 | 220 |
: public MapExtender<DefaultMap<Digraph, Arc, _Value> > { |
223 | 221 |
public: |
224 | 222 |
typedef ArcSetExtender Digraph; |
225 | 223 |
typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent; |
226 | 224 |
|
227 | 225 |
explicit ArcMap(const Digraph& _g) |
228 | 226 |
: Parent(_g) {} |
229 | 227 |
ArcMap(const Digraph& _g, const _Value& _v) |
230 | 228 |
: Parent(_g, _v) {} |
231 | 229 |
|
232 | 230 |
ArcMap& operator=(const ArcMap& cmap) { |
233 | 231 |
return operator=<ArcMap>(cmap); |
234 | 232 |
} |
235 | 233 |
|
236 | 234 |
template <typename CMap> |
237 | 235 |
ArcMap& operator=(const CMap& cmap) { |
238 | 236 |
Parent::operator=(cmap); |
239 | 237 |
return *this; |
240 | 238 |
} |
241 | 239 |
|
242 | 240 |
}; |
243 | 241 |
|
244 | 242 |
|
245 | 243 |
// Alteration extension |
246 | 244 |
|
247 | 245 |
Arc addArc(const Node& from, const Node& to) { |
248 | 246 |
Arc arc = Parent::addArc(from, to); |
249 | 247 |
notifier(Arc()).add(arc); |
250 | 248 |
return arc; |
251 | 249 |
} |
252 | 250 |
|
253 | 251 |
void clear() { |
254 | 252 |
notifier(Arc()).clear(); |
255 | 253 |
Parent::clear(); |
256 | 254 |
} |
257 | 255 |
|
258 | 256 |
void erase(const Arc& arc) { |
259 | 257 |
notifier(Arc()).erase(arc); |
260 | 258 |
Parent::erase(arc); |
261 | 259 |
} |
262 | 260 |
|
263 | 261 |
ArcSetExtender() { |
264 | 262 |
arc_notifier.setContainer(*this); |
265 | 263 |
} |
266 | 264 |
|
267 | 265 |
~ArcSetExtender() { |
268 | 266 |
arc_notifier.clear(); |
269 | 267 |
} |
270 | 268 |
|
271 | 269 |
}; |
272 | 270 |
|
273 | 271 |
|
274 |
/// \ingroup digraphbits |
|
275 |
/// |
|
276 |
// |
|
272 |
// \ingroup digraphbits |
|
273 |
// |
|
274 |
// \brief Extender for the EdgeSets |
|
277 | 275 |
template <typename Base> |
278 | 276 |
class EdgeSetExtender : public Base { |
279 | 277 |
|
280 | 278 |
public: |
281 | 279 |
|
282 | 280 |
typedef Base Parent; |
283 | 281 |
typedef EdgeSetExtender Digraph; |
284 | 282 |
|
285 | 283 |
typedef typename Parent::Node Node; |
286 | 284 |
typedef typename Parent::Arc Arc; |
287 | 285 |
typedef typename Parent::Edge Edge; |
288 | 286 |
|
289 | 287 |
|
290 | 288 |
int maxId(Node) const { |
291 | 289 |
return Parent::maxNodeId(); |
292 | 290 |
} |
293 | 291 |
|
294 | 292 |
int maxId(Arc) const { |
295 | 293 |
return Parent::maxArcId(); |
296 | 294 |
} |
297 | 295 |
|
298 | 296 |
int maxId(Edge) const { |
299 | 297 |
return Parent::maxEdgeId(); |
300 | 298 |
} |
301 | 299 |
|
302 | 300 |
Node fromId(int id, Node) const { |
303 | 301 |
return Parent::nodeFromId(id); |
304 | 302 |
} |
305 | 303 |
|
306 | 304 |
Arc fromId(int id, Arc) const { |
307 | 305 |
return Parent::arcFromId(id); |
308 | 306 |
} |
309 | 307 |
|
310 | 308 |
Edge fromId(int id, Edge) const { |
311 | 309 |
return Parent::edgeFromId(id); |
312 | 310 |
} |
313 | 311 |
|
314 | 312 |
Node oppositeNode(const Node &n, const Edge &e) const { |
315 | 313 |
if( n == Parent::u(e)) |
316 | 314 |
return Parent::v(e); |
317 | 315 |
else if( n == Parent::v(e)) |
318 | 316 |
return Parent::u(e); |
319 | 317 |
else |
320 | 318 |
return INVALID; |
321 | 319 |
} |
322 | 320 |
|
323 | 321 |
Arc oppositeArc(const Arc &e) const { |
324 | 322 |
return Parent::direct(e, !Parent::direction(e)); |
... | ... |
@@ -447,133 +445,133 @@ |
447 | 445 |
|
448 | 446 |
class EdgeIt : public Parent::Edge { |
449 | 447 |
const Digraph* digraph; |
450 | 448 |
public: |
451 | 449 |
|
452 | 450 |
EdgeIt() { } |
453 | 451 |
|
454 | 452 |
EdgeIt(Invalid i) : Edge(i) { } |
455 | 453 |
|
456 | 454 |
explicit EdgeIt(const Digraph& _graph) : digraph(&_graph) { |
457 | 455 |
_graph.first(static_cast<Edge&>(*this)); |
458 | 456 |
} |
459 | 457 |
|
460 | 458 |
EdgeIt(const Digraph& _graph, const Edge& e) : |
461 | 459 |
Edge(e), digraph(&_graph) { } |
462 | 460 |
|
463 | 461 |
EdgeIt& operator++() { |
464 | 462 |
digraph->next(*this); |
465 | 463 |
return *this; |
466 | 464 |
} |
467 | 465 |
|
468 | 466 |
}; |
469 | 467 |
|
470 | 468 |
class IncEdgeIt : public Parent::Edge { |
471 | 469 |
friend class EdgeSetExtender; |
472 | 470 |
const Digraph* digraph; |
473 | 471 |
bool direction; |
474 | 472 |
public: |
475 | 473 |
|
476 | 474 |
IncEdgeIt() { } |
477 | 475 |
|
478 | 476 |
IncEdgeIt(Invalid i) : Edge(i), direction(false) { } |
479 | 477 |
|
480 | 478 |
IncEdgeIt(const Digraph& _graph, const Node &n) : digraph(&_graph) { |
481 | 479 |
_graph.firstInc(*this, direction, n); |
482 | 480 |
} |
483 | 481 |
|
484 | 482 |
IncEdgeIt(const Digraph& _graph, const Edge &ue, const Node &n) |
485 | 483 |
: digraph(&_graph), Edge(ue) { |
486 | 484 |
direction = (_graph.source(ue) == n); |
487 | 485 |
} |
488 | 486 |
|
489 | 487 |
IncEdgeIt& operator++() { |
490 | 488 |
digraph->nextInc(*this, direction); |
491 | 489 |
return *this; |
492 | 490 |
} |
493 | 491 |
}; |
494 | 492 |
|
495 |
/// \brief Base node of the iterator |
|
496 |
/// |
|
497 |
// |
|
493 |
// \brief Base node of the iterator |
|
494 |
// |
|
495 |
// Returns the base node (ie. the source in this case) of the iterator |
|
498 | 496 |
Node baseNode(const OutArcIt &e) const { |
499 | 497 |
return Parent::source(static_cast<const Arc&>(e)); |
500 | 498 |
} |
501 |
/// \brief Running node of the iterator |
|
502 |
/// |
|
503 |
/// Returns the running node (ie. the target in this case) of the |
|
504 |
/// iterator |
|
499 |
// \brief Running node of the iterator |
|
500 |
// |
|
501 |
// Returns the running node (ie. the target in this case) of the |
|
502 |
// iterator |
|
505 | 503 |
Node runningNode(const OutArcIt &e) const { |
506 | 504 |
return Parent::target(static_cast<const Arc&>(e)); |
507 | 505 |
} |
508 | 506 |
|
509 |
/// \brief Base node of the iterator |
|
510 |
/// |
|
511 |
// |
|
507 |
// \brief Base node of the iterator |
|
508 |
// |
|
509 |
// Returns the base node (ie. the target in this case) of the iterator |
|
512 | 510 |
Node baseNode(const InArcIt &e) const { |
513 | 511 |
return Parent::target(static_cast<const Arc&>(e)); |
514 | 512 |
} |
515 |
/// \brief Running node of the iterator |
|
516 |
/// |
|
517 |
/// Returns the running node (ie. the source in this case) of the |
|
518 |
/// iterator |
|
513 |
// \brief Running node of the iterator |
|
514 |
// |
|
515 |
// Returns the running node (ie. the source in this case) of the |
|
516 |
// iterator |
|
519 | 517 |
Node runningNode(const InArcIt &e) const { |
520 | 518 |
return Parent::source(static_cast<const Arc&>(e)); |
521 | 519 |
} |
522 | 520 |
|
523 |
/// Base node of the iterator |
|
524 |
/// |
|
525 |
// |
|
521 |
// Base node of the iterator |
|
522 |
// |
|
523 |
// Returns the base node of the iterator |
|
526 | 524 |
Node baseNode(const IncEdgeIt &e) const { |
527 | 525 |
return e.direction ? u(e) : v(e); |
528 | 526 |
} |
529 |
/// Running node of the iterator |
|
530 |
/// |
|
531 |
// |
|
527 |
// Running node of the iterator |
|
528 |
// |
|
529 |
// Returns the running node of the iterator |
|
532 | 530 |
Node runningNode(const IncEdgeIt &e) const { |
533 | 531 |
return e.direction ? v(e) : u(e); |
534 | 532 |
} |
535 | 533 |
|
536 | 534 |
|
537 | 535 |
template <typename _Value> |
538 | 536 |
class ArcMap |
539 | 537 |
: public MapExtender<DefaultMap<Digraph, Arc, _Value> > { |
540 | 538 |
public: |
541 | 539 |
typedef EdgeSetExtender Digraph; |
542 | 540 |
typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent; |
543 | 541 |
|
544 | 542 |
ArcMap(const Digraph& _g) |
545 | 543 |
: Parent(_g) {} |
546 | 544 |
ArcMap(const Digraph& _g, const _Value& _v) |
547 | 545 |
: Parent(_g, _v) {} |
548 | 546 |
|
549 | 547 |
ArcMap& operator=(const ArcMap& cmap) { |
550 | 548 |
return operator=<ArcMap>(cmap); |
551 | 549 |
} |
552 | 550 |
|
553 | 551 |
template <typename CMap> |
554 | 552 |
ArcMap& operator=(const CMap& cmap) { |
555 | 553 |
Parent::operator=(cmap); |
556 | 554 |
return *this; |
557 | 555 |
} |
558 | 556 |
|
559 | 557 |
}; |
560 | 558 |
|
561 | 559 |
|
562 | 560 |
template <typename _Value> |
563 | 561 |
class EdgeMap |
564 | 562 |
: public MapExtender<DefaultMap<Digraph, Edge, _Value> > { |
565 | 563 |
public: |
566 | 564 |
typedef EdgeSetExtender Digraph; |
567 | 565 |
typedef MapExtender<DefaultMap<Digraph, Edge, _Value> > Parent; |
568 | 566 |
|
569 | 567 |
EdgeMap(const Digraph& _g) |
570 | 568 |
: Parent(_g) {} |
571 | 569 |
|
572 | 570 |
EdgeMap(const Digraph& _g, const _Value& _v) |
573 | 571 |
: Parent(_g, _v) {} |
574 | 572 |
|
575 | 573 |
EdgeMap& operator=(const EdgeMap& cmap) { |
576 | 574 |
return operator=<EdgeMap>(cmap); |
577 | 575 |
} |
578 | 576 |
|
579 | 577 |
template <typename CMap> |
... | ... |
@@ -170,170 +170,170 @@ |
170 | 170 |
typedef typename Traits::Value Value; |
171 | 171 |
|
172 | 172 |
/// The type of the lower bound capacity map. |
173 | 173 |
typedef typename Traits::LCapMap LCapMap; |
174 | 174 |
/// The type of the upper bound capacity map. |
175 | 175 |
typedef typename Traits::UCapMap UCapMap; |
176 | 176 |
/// \brief The type of the map that stores the lower bound for |
177 | 177 |
/// the supply of the nodes. |
178 | 178 |
typedef typename Traits::DeltaMap DeltaMap; |
179 | 179 |
///The type of the flow map. |
180 | 180 |
typedef typename Traits::FlowMap FlowMap; |
181 | 181 |
|
182 | 182 |
///The type of the elevator. |
183 | 183 |
typedef typename Traits::Elevator Elevator; |
184 | 184 |
///The type of the tolerance. |
185 | 185 |
typedef typename Traits::Tolerance Tolerance; |
186 | 186 |
|
187 | 187 |
private: |
188 | 188 |
|
189 | 189 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
190 | 190 |
|
191 | 191 |
const Digraph &_g; |
192 | 192 |
int _node_num; |
193 | 193 |
|
194 | 194 |
const LCapMap *_lo; |
195 | 195 |
const UCapMap *_up; |
196 | 196 |
const DeltaMap *_delta; |
197 | 197 |
|
198 | 198 |
FlowMap *_flow; |
199 | 199 |
bool _local_flow; |
200 | 200 |
|
201 | 201 |
Elevator* _level; |
202 | 202 |
bool _local_level; |
203 | 203 |
|
204 | 204 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
205 | 205 |
ExcessMap* _excess; |
206 | 206 |
|
207 | 207 |
Tolerance _tol; |
208 | 208 |
int _el; |
209 | 209 |
|
210 | 210 |
public: |
211 | 211 |
|
212 | 212 |
typedef Circulation Create; |
213 | 213 |
|
214 | 214 |
///\name Named Template Parameters |
215 | 215 |
|
216 | 216 |
///@{ |
217 | 217 |
|
218 |
template <typename |
|
218 |
template <typename T> |
|
219 | 219 |
struct SetFlowMapTraits : public Traits { |
220 |
typedef |
|
220 |
typedef T FlowMap; |
|
221 | 221 |
static FlowMap *createFlowMap(const Digraph&) { |
222 | 222 |
LEMON_ASSERT(false, "FlowMap is not initialized"); |
223 | 223 |
return 0; // ignore warnings |
224 | 224 |
} |
225 | 225 |
}; |
226 | 226 |
|
227 | 227 |
/// \brief \ref named-templ-param "Named parameter" for setting |
228 | 228 |
/// FlowMap type |
229 | 229 |
/// |
230 | 230 |
/// \ref named-templ-param "Named parameter" for setting FlowMap |
231 | 231 |
/// type. |
232 |
template <typename |
|
232 |
template <typename T> |
|
233 | 233 |
struct SetFlowMap |
234 | 234 |
: public Circulation<Digraph, LCapMap, UCapMap, DeltaMap, |
235 |
SetFlowMapTraits< |
|
235 |
SetFlowMapTraits<T> > { |
|
236 | 236 |
typedef Circulation<Digraph, LCapMap, UCapMap, DeltaMap, |
237 |
SetFlowMapTraits< |
|
237 |
SetFlowMapTraits<T> > Create; |
|
238 | 238 |
}; |
239 | 239 |
|
240 |
template <typename |
|
240 |
template <typename T> |
|
241 | 241 |
struct SetElevatorTraits : public Traits { |
242 |
typedef |
|
242 |
typedef T Elevator; |
|
243 | 243 |
static Elevator *createElevator(const Digraph&, int) { |
244 | 244 |
LEMON_ASSERT(false, "Elevator is not initialized"); |
245 | 245 |
return 0; // ignore warnings |
246 | 246 |
} |
247 | 247 |
}; |
248 | 248 |
|
249 | 249 |
/// \brief \ref named-templ-param "Named parameter" for setting |
250 | 250 |
/// Elevator type |
251 | 251 |
/// |
252 | 252 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
253 | 253 |
/// type. If this named parameter is used, then an external |
254 | 254 |
/// elevator object must be passed to the algorithm using the |
255 | 255 |
/// \ref elevator(Elevator&) "elevator()" function before calling |
256 | 256 |
/// \ref run() or \ref init(). |
257 | 257 |
/// \sa SetStandardElevator |
258 |
template <typename |
|
258 |
template <typename T> |
|
259 | 259 |
struct SetElevator |
260 | 260 |
: public Circulation<Digraph, LCapMap, UCapMap, DeltaMap, |
261 |
SetElevatorTraits< |
|
261 |
SetElevatorTraits<T> > { |
|
262 | 262 |
typedef Circulation<Digraph, LCapMap, UCapMap, DeltaMap, |
263 |
SetElevatorTraits< |
|
263 |
SetElevatorTraits<T> > Create; |
|
264 | 264 |
}; |
265 | 265 |
|
266 |
template <typename |
|
266 |
template <typename T> |
|
267 | 267 |
struct SetStandardElevatorTraits : public Traits { |
268 |
typedef |
|
268 |
typedef T Elevator; |
|
269 | 269 |
static Elevator *createElevator(const Digraph& digraph, int max_level) { |
270 | 270 |
return new Elevator(digraph, max_level); |
271 | 271 |
} |
272 | 272 |
}; |
273 | 273 |
|
274 | 274 |
/// \brief \ref named-templ-param "Named parameter" for setting |
275 | 275 |
/// Elevator type with automatic allocation |
276 | 276 |
/// |
277 | 277 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
278 | 278 |
/// type with automatic allocation. |
279 | 279 |
/// The Elevator should have standard constructor interface to be |
280 | 280 |
/// able to automatically created by the algorithm (i.e. the |
281 | 281 |
/// digraph and the maximum level should be passed to it). |
282 | 282 |
/// However an external elevator object could also be passed to the |
283 | 283 |
/// algorithm with the \ref elevator(Elevator&) "elevator()" function |
284 | 284 |
/// before calling \ref run() or \ref init(). |
285 | 285 |
/// \sa SetElevator |
286 |
template <typename |
|
286 |
template <typename T> |
|
287 | 287 |
struct SetStandardElevator |
288 | 288 |
: public Circulation<Digraph, LCapMap, UCapMap, DeltaMap, |
289 |
SetStandardElevatorTraits< |
|
289 |
SetStandardElevatorTraits<T> > { |
|
290 | 290 |
typedef Circulation<Digraph, LCapMap, UCapMap, DeltaMap, |
291 |
SetStandardElevatorTraits< |
|
291 |
SetStandardElevatorTraits<T> > Create; |
|
292 | 292 |
}; |
293 | 293 |
|
294 | 294 |
/// @} |
295 | 295 |
|
296 | 296 |
protected: |
297 | 297 |
|
298 | 298 |
Circulation() {} |
299 | 299 |
|
300 | 300 |
public: |
301 | 301 |
|
302 | 302 |
/// The constructor of the class. |
303 | 303 |
|
304 | 304 |
/// The constructor of the class. |
305 | 305 |
/// \param g The digraph the algorithm runs on. |
306 | 306 |
/// \param lo The lower bound capacity of the arcs. |
307 | 307 |
/// \param up The upper bound capacity of the arcs. |
308 | 308 |
/// \param delta The lower bound for the supply of the nodes. |
309 | 309 |
Circulation(const Digraph &g,const LCapMap &lo, |
310 | 310 |
const UCapMap &up,const DeltaMap &delta) |
311 | 311 |
: _g(g), _node_num(), |
312 | 312 |
_lo(&lo),_up(&up),_delta(&delta),_flow(0),_local_flow(false), |
313 | 313 |
_level(0), _local_level(false), _excess(0), _el() {} |
314 | 314 |
|
315 | 315 |
/// Destructor. |
316 | 316 |
~Circulation() { |
317 | 317 |
destroyStructures(); |
318 | 318 |
} |
319 | 319 |
|
320 | 320 |
|
321 | 321 |
private: |
322 | 322 |
|
323 | 323 |
void createStructures() { |
324 | 324 |
_node_num = _el = countNodes(_g); |
325 | 325 |
|
326 | 326 |
if (!_flow) { |
327 | 327 |
_flow = Traits::createFlowMap(_g); |
328 | 328 |
_local_flow = true; |
329 | 329 |
} |
330 | 330 |
if (!_level) { |
331 | 331 |
_level = Traits::createElevator(_g, _node_num); |
332 | 332 |
_local_level = true; |
333 | 333 |
} |
334 | 334 |
if (!_excess) { |
335 | 335 |
_excess = new ExcessMap(_g); |
336 | 336 |
} |
337 | 337 |
} |
338 | 338 |
|
339 | 339 |
void destroyStructures() { |
... | ... |
@@ -637,97 +637,97 @@ |
637 | 637 |
} |
638 | 638 |
|
639 | 639 |
/// \brief Returns a const reference to the flow map. |
640 | 640 |
/// |
641 | 641 |
/// Returns a const reference to the arc map storing the found flow. |
642 | 642 |
/// |
643 | 643 |
/// \pre Either \ref run() or \ref init() must be called before |
644 | 644 |
/// using this function. |
645 | 645 |
const FlowMap& flowMap() const { |
646 | 646 |
return *_flow; |
647 | 647 |
} |
648 | 648 |
|
649 | 649 |
/** |
650 | 650 |
\brief Returns \c true if the given node is in a barrier. |
651 | 651 |
|
652 | 652 |
Barrier is a set \e B of nodes for which |
653 | 653 |
|
654 | 654 |
\f[ \sum_{a\in\delta_{out}(B)} upper(a) - |
655 | 655 |
\sum_{a\in\delta_{in}(B)} lower(a) < \sum_{v\in B}delta(v) \f] |
656 | 656 |
|
657 | 657 |
holds. The existence of a set with this property prooves that a |
658 | 658 |
feasible circualtion cannot exist. |
659 | 659 |
|
660 | 660 |
This function returns \c true if the given node is in the found |
661 | 661 |
barrier. If a feasible circulation is found, the function |
662 | 662 |
gives back \c false for every node. |
663 | 663 |
|
664 | 664 |
\pre Either \ref run() or \ref init() must be called before |
665 | 665 |
using this function. |
666 | 666 |
|
667 | 667 |
\sa barrierMap() |
668 | 668 |
\sa checkBarrier() |
669 | 669 |
*/ |
670 | 670 |
bool barrier(const Node& node) const |
671 | 671 |
{ |
672 | 672 |
return (*_level)[node] >= _el; |
673 | 673 |
} |
674 | 674 |
|
675 | 675 |
/// \brief Gives back a barrier. |
676 | 676 |
/// |
677 | 677 |
/// This function sets \c bar to the characteristic vector of the |
678 | 678 |
/// found barrier. \c bar should be a \ref concepts::WriteMap "writable" |
679 | 679 |
/// node map with \c bool (or convertible) value type. |
680 | 680 |
/// |
681 | 681 |
/// If a feasible circulation is found, the function gives back an |
682 | 682 |
/// empty set, so \c bar[v] will be \c false for all nodes \c v. |
683 | 683 |
/// |
684 | 684 |
/// \note This function calls \ref barrier() for each node, |
685 |
/// so it runs in |
|
685 |
/// so it runs in O(n) time. |
|
686 | 686 |
/// |
687 | 687 |
/// \pre Either \ref run() or \ref init() must be called before |
688 | 688 |
/// using this function. |
689 | 689 |
/// |
690 | 690 |
/// \sa barrier() |
691 | 691 |
/// \sa checkBarrier() |
692 | 692 |
template<class BarrierMap> |
693 | 693 |
void barrierMap(BarrierMap &bar) const |
694 | 694 |
{ |
695 | 695 |
for(NodeIt n(_g);n!=INVALID;++n) |
696 | 696 |
bar.set(n, (*_level)[n] >= _el); |
697 | 697 |
} |
698 | 698 |
|
699 | 699 |
/// @} |
700 | 700 |
|
701 | 701 |
/// \name Checker Functions |
702 | 702 |
/// The feasibility of the results can be checked using |
703 | 703 |
/// these functions.\n |
704 | 704 |
/// Either \ref run() or \ref start() should be called before |
705 | 705 |
/// using them. |
706 | 706 |
|
707 | 707 |
///@{ |
708 | 708 |
|
709 | 709 |
///Check if the found flow is a feasible circulation |
710 | 710 |
|
711 | 711 |
///Check if the found flow is a feasible circulation, |
712 | 712 |
/// |
713 | 713 |
bool checkFlow() const { |
714 | 714 |
for(ArcIt e(_g);e!=INVALID;++e) |
715 | 715 |
if((*_flow)[e]<(*_lo)[e]||(*_flow)[e]>(*_up)[e]) return false; |
716 | 716 |
for(NodeIt n(_g);n!=INVALID;++n) |
717 | 717 |
{ |
718 | 718 |
Value dif=-(*_delta)[n]; |
719 | 719 |
for(InArcIt e(_g,n);e!=INVALID;++e) dif-=(*_flow)[e]; |
720 | 720 |
for(OutArcIt e(_g,n);e!=INVALID;++e) dif+=(*_flow)[e]; |
721 | 721 |
if(_tol.negative(dif)) return false; |
722 | 722 |
} |
723 | 723 |
return true; |
724 | 724 |
} |
725 | 725 |
|
726 | 726 |
///Check whether or not the last execution provides a barrier |
727 | 727 |
|
728 | 728 |
///Check whether or not the last execution provides a barrier. |
729 | 729 |
///\sa barrier() |
730 | 730 |
///\sa barrierMap() |
731 | 731 |
bool checkBarrier() const |
732 | 732 |
{ |
733 | 733 |
Value delta=0; |
... | ... |
@@ -556,113 +556,125 @@ |
556 | 556 |
public: |
557 | 557 |
|
558 | 558 |
///\e |
559 | 559 |
EdgeMap(const Graph&) { } |
560 | 560 |
///\e |
561 | 561 |
EdgeMap(const Graph&, T) { } |
562 | 562 |
private: |
563 | 563 |
///Copy constructor |
564 | 564 |
EdgeMap(const EdgeMap& em) : ReadWriteMap<Edge,T>(em) {} |
565 | 565 |
///Assignment operator |
566 | 566 |
template <typename CMap> |
567 | 567 |
EdgeMap& operator=(const CMap&) { |
568 | 568 |
checkConcept<ReadMap<Edge, T>, CMap>(); |
569 | 569 |
return *this; |
570 | 570 |
} |
571 | 571 |
}; |
572 | 572 |
|
573 | 573 |
/// \brief Direct the given edge. |
574 | 574 |
/// |
575 | 575 |
/// Direct the given edge. The returned arc source |
576 | 576 |
/// will be the given node. |
577 | 577 |
Arc direct(const Edge&, const Node&) const { |
578 | 578 |
return INVALID; |
579 | 579 |
} |
580 | 580 |
|
581 | 581 |
/// \brief Direct the given edge. |
582 | 582 |
/// |
583 | 583 |
/// Direct the given edge. The returned arc |
584 | 584 |
/// represents the given edge and the direction comes |
585 | 585 |
/// from the bool parameter. The source of the edge and |
586 | 586 |
/// the directed arc is the same when the given bool is true. |
587 | 587 |
Arc direct(const Edge&, bool) const { |
588 | 588 |
return INVALID; |
589 | 589 |
} |
590 | 590 |
|
591 | 591 |
/// \brief Returns true if the arc has default orientation. |
592 | 592 |
/// |
593 | 593 |
/// Returns whether the given directed arc is same orientation as |
594 | 594 |
/// the corresponding edge's default orientation. |
595 | 595 |
bool direction(Arc) const { return true; } |
596 | 596 |
|
597 | 597 |
/// \brief Returns the opposite directed arc. |
598 | 598 |
/// |
599 | 599 |
/// Returns the opposite directed arc. |
600 | 600 |
Arc oppositeArc(Arc) const { return INVALID; } |
601 | 601 |
|
602 | 602 |
/// \brief Opposite node on an arc |
603 | 603 |
/// |
604 |
/// \return |
|
604 |
/// \return The opposite of the given node on the given edge. |
|
605 | 605 |
Node oppositeNode(Node, Edge) const { return INVALID; } |
606 | 606 |
|
607 | 607 |
/// \brief First node of the edge. |
608 | 608 |
/// |
609 |
/// \return |
|
609 |
/// \return The first node of the given edge. |
|
610 | 610 |
/// |
611 | 611 |
/// Naturally edges don't have direction and thus |
612 |
/// don't have source and target node. But we use these two methods |
|
613 |
/// to query the two nodes of the arc. The direction of the arc |
|
614 |
/// |
|
612 |
/// don't have source and target node. However we use \c u() and \c v() |
|
613 |
/// methods to query the two nodes of the arc. The direction of the |
|
614 |
/// arc which arises this way is called the inherent direction of the |
|
615 | 615 |
/// edge, and is used to define the "default" direction |
616 | 616 |
/// of the directed versions of the arcs. |
617 |
/// \sa |
|
617 |
/// \sa v() |
|
618 |
/// \sa direction() |
|
618 | 619 |
Node u(Edge) const { return INVALID; } |
619 | 620 |
|
620 | 621 |
/// \brief Second node of the edge. |
622 |
/// |
|
623 |
/// \return The second node of the given edge. |
|
624 |
/// |
|
625 |
/// Naturally edges don't have direction and thus |
|
626 |
/// don't have source and target node. However we use \c u() and \c v() |
|
627 |
/// methods to query the two nodes of the arc. The direction of the |
|
628 |
/// arc which arises this way is called the inherent direction of the |
|
629 |
/// edge, and is used to define the "default" direction |
|
630 |
/// of the directed versions of the arcs. |
|
631 |
/// \sa u() |
|
632 |
/// \sa direction() |
|
621 | 633 |
Node v(Edge) const { return INVALID; } |
622 | 634 |
|
623 | 635 |
/// \brief Source node of the directed arc. |
624 | 636 |
Node source(Arc) const { return INVALID; } |
625 | 637 |
|
626 | 638 |
/// \brief Target node of the directed arc. |
627 | 639 |
Node target(Arc) const { return INVALID; } |
628 | 640 |
|
629 | 641 |
/// \brief Returns the id of the node. |
630 | 642 |
int id(Node) const { return -1; } |
631 | 643 |
|
632 | 644 |
/// \brief Returns the id of the edge. |
633 | 645 |
int id(Edge) const { return -1; } |
634 | 646 |
|
635 | 647 |
/// \brief Returns the id of the arc. |
636 | 648 |
int id(Arc) const { return -1; } |
637 | 649 |
|
638 | 650 |
/// \brief Returns the node with the given id. |
639 | 651 |
/// |
640 | 652 |
/// \pre The argument should be a valid node id in the graph. |
641 | 653 |
Node nodeFromId(int) const { return INVALID; } |
642 | 654 |
|
643 | 655 |
/// \brief Returns the edge with the given id. |
644 | 656 |
/// |
645 | 657 |
/// \pre The argument should be a valid edge id in the graph. |
646 | 658 |
Edge edgeFromId(int) const { return INVALID; } |
647 | 659 |
|
648 | 660 |
/// \brief Returns the arc with the given id. |
649 | 661 |
/// |
650 | 662 |
/// \pre The argument should be a valid arc id in the graph. |
651 | 663 |
Arc arcFromId(int) const { return INVALID; } |
652 | 664 |
|
653 | 665 |
/// \brief Returns an upper bound on the node IDs. |
654 | 666 |
int maxNodeId() const { return -1; } |
655 | 667 |
|
656 | 668 |
/// \brief Returns an upper bound on the edge IDs. |
657 | 669 |
int maxEdgeId() const { return -1; } |
658 | 670 |
|
659 | 671 |
/// \brief Returns an upper bound on the arc IDs. |
660 | 672 |
int maxArcId() const { return -1; } |
661 | 673 |
|
662 | 674 |
void first(Node&) const {} |
663 | 675 |
void next(Node&) const {} |
664 | 676 |
|
665 | 677 |
void first(Edge&) const {} |
666 | 678 |
void next(Edge&) const {} |
667 | 679 |
|
668 | 680 |
void first(Arc&) const {} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup graph_concepts |
20 | 20 |
///\file |
21 | 21 |
///\brief The concept of graph components. |
22 | 22 |
|
23 |
|
|
24 | 23 |
#ifndef LEMON_CONCEPTS_GRAPH_COMPONENTS_H |
25 | 24 |
#define LEMON_CONCEPTS_GRAPH_COMPONENTS_H |
26 | 25 |
|
27 | 26 |
#include <lemon/core.h> |
28 | 27 |
#include <lemon/concepts/maps.h> |
29 | 28 |
|
30 | 29 |
#include <lemon/bits/alteration_notifier.h> |
31 | 30 |
|
32 | 31 |
namespace lemon { |
33 | 32 |
namespace concepts { |
34 | 33 |
|
35 | 34 |
/// \brief Skeleton class for graph Node and Arc types |
36 | 35 |
/// |
37 | 36 |
/// This class describes the interface of Node and Arc (and Edge |
38 | 37 |
/// in undirected graphs) subtypes of graph types. |
39 | 38 |
/// |
40 | 39 |
/// \note This class is a template class so that we can use it to |
41 | 40 |
/// create graph skeleton classes. The reason for this is than Node |
42 | 41 |
/// and Arc types should \em not derive from the same base class. |
43 | 42 |
/// For Node you should instantiate it with character 'n' and for Arc |
44 | 43 |
/// with 'a'. |
45 | 44 |
|
46 | 45 |
#ifndef DOXYGEN |
47 |
template <char |
|
46 |
template <char sel = '0'> |
|
48 | 47 |
#endif |
49 | 48 |
class GraphItem { |
50 | 49 |
public: |
51 | 50 |
/// \brief Default constructor. |
52 | 51 |
/// |
53 | 52 |
/// \warning The default constructor is not required to set |
54 | 53 |
/// the item to some well-defined value. So you should consider it |
55 | 54 |
/// as uninitialized. |
56 | 55 |
GraphItem() {} |
57 | 56 |
/// \brief Copy constructor. |
58 | 57 |
/// |
59 | 58 |
/// Copy constructor. |
60 | 59 |
/// |
61 | 60 |
GraphItem(const GraphItem &) {} |
62 | 61 |
/// \brief Invalid constructor \& conversion. |
63 | 62 |
/// |
64 | 63 |
/// This constructor initializes the item to be invalid. |
65 | 64 |
/// \sa Invalid for more details. |
66 | 65 |
GraphItem(Invalid) {} |
67 | 66 |
/// \brief Assign operator for nodes. |
68 | 67 |
/// |
69 | 68 |
/// The nodes are assignable. |
70 | 69 |
/// |
71 | 70 |
GraphItem& operator=(GraphItem const&) { return *this; } |
72 | 71 |
/// \brief Equality operator. |
73 | 72 |
/// |
74 | 73 |
/// Two iterators are equal if and only if they represents the |
75 | 74 |
/// same node in the graph or both are invalid. |
76 | 75 |
bool operator==(GraphItem) const { return false; } |
77 | 76 |
/// \brief Inequality operator. |
78 | 77 |
/// |
79 | 78 |
/// \sa operator==(const Node& n) |
80 | 79 |
/// |
81 | 80 |
bool operator!=(GraphItem) const { return false; } |
82 | 81 |
|
83 | 82 |
/// \brief Artificial ordering operator. |
84 | 83 |
/// |
85 | 84 |
/// To allow the use of graph descriptors as key type in std::map or |
86 | 85 |
/// similar associative container we require this. |
87 | 86 |
/// |
88 | 87 |
/// \note This operator only have to define some strict ordering of |
89 | 88 |
/// the items; this order has nothing to do with the iteration |
90 | 89 |
/// ordering of the items. |
91 | 90 |
bool operator<(GraphItem) const { return false; } |
92 | 91 |
|
93 | 92 |
template<typename _GraphItem> |
94 | 93 |
struct Constraints { |
95 | 94 |
void constraints() { |
... | ... |
@@ -251,377 +250,377 @@ |
251 | 250 |
/// same edge and has opposite direction. |
252 | 251 |
Arc oppositeArc(const Arc&) const { return INVALID;} |
253 | 252 |
|
254 | 253 |
/// \brief Gives back one ending of an edge. |
255 | 254 |
/// |
256 | 255 |
/// Gives back one ending of an edge. |
257 | 256 |
Node u(const Edge&) const { return INVALID;} |
258 | 257 |
|
259 | 258 |
/// \brief Gives back the other ending of an edge. |
260 | 259 |
/// |
261 | 260 |
/// Gives back the other ending of an edge. |
262 | 261 |
Node v(const Edge&) const { return INVALID;} |
263 | 262 |
|
264 | 263 |
template <typename _Graph> |
265 | 264 |
struct Constraints { |
266 | 265 |
typedef typename _Graph::Node Node; |
267 | 266 |
typedef typename _Graph::Arc Arc; |
268 | 267 |
typedef typename _Graph::Edge Edge; |
269 | 268 |
|
270 | 269 |
void constraints() { |
271 | 270 |
checkConcept<BaseDigraphComponent, _Graph>(); |
272 | 271 |
checkConcept<GraphItem<'u'>, Edge>(); |
273 | 272 |
{ |
274 | 273 |
Node n; |
275 | 274 |
Edge ue(INVALID); |
276 | 275 |
Arc e; |
277 | 276 |
n = graph.u(ue); |
278 | 277 |
n = graph.v(ue); |
279 | 278 |
e = graph.direct(ue, true); |
280 | 279 |
e = graph.direct(ue, n); |
281 | 280 |
e = graph.oppositeArc(e); |
282 | 281 |
ue = e; |
283 | 282 |
bool d = graph.direction(e); |
284 | 283 |
ignore_unused_variable_warning(d); |
285 | 284 |
} |
286 | 285 |
} |
287 | 286 |
|
288 | 287 |
const _Graph& graph; |
289 | 288 |
}; |
290 | 289 |
|
291 | 290 |
}; |
292 | 291 |
|
293 | 292 |
/// \brief An empty idable base digraph class. |
294 | 293 |
/// |
295 | 294 |
/// This class provides beside the core digraph features |
296 | 295 |
/// core id functions for the digraph structure. |
297 | 296 |
/// The most of the base digraphs should conform to this concept. |
298 | 297 |
/// The id's are unique and immutable. |
299 |
template <typename _Base = BaseDigraphComponent> |
|
300 |
class IDableDigraphComponent : public _Base { |
|
298 |
template <typename BAS = BaseDigraphComponent> |
|
299 |
class IDableDigraphComponent : public BAS { |
|
301 | 300 |
public: |
302 | 301 |
|
303 |
typedef |
|
302 |
typedef BAS Base; |
|
304 | 303 |
typedef typename Base::Node Node; |
305 | 304 |
typedef typename Base::Arc Arc; |
306 | 305 |
|
307 | 306 |
/// \brief Gives back an unique integer id for the Node. |
308 | 307 |
/// |
309 | 308 |
/// Gives back an unique integer id for the Node. |
310 | 309 |
/// |
311 | 310 |
int id(const Node&) const { return -1;} |
312 | 311 |
|
313 | 312 |
/// \brief Gives back the node by the unique id. |
314 | 313 |
/// |
315 | 314 |
/// Gives back the node by the unique id. |
316 | 315 |
/// If the digraph does not contain node with the given id |
317 | 316 |
/// then the result of the function is undetermined. |
318 | 317 |
Node nodeFromId(int) const { return INVALID;} |
319 | 318 |
|
320 | 319 |
/// \brief Gives back an unique integer id for the Arc. |
321 | 320 |
/// |
322 | 321 |
/// Gives back an unique integer id for the Arc. |
323 | 322 |
/// |
324 | 323 |
int id(const Arc&) const { return -1;} |
325 | 324 |
|
326 | 325 |
/// \brief Gives back the arc by the unique id. |
327 | 326 |
/// |
328 | 327 |
/// Gives back the arc by the unique id. |
329 | 328 |
/// If the digraph does not contain arc with the given id |
330 | 329 |
/// then the result of the function is undetermined. |
331 | 330 |
Arc arcFromId(int) const { return INVALID;} |
332 | 331 |
|
333 | 332 |
/// \brief Gives back an integer greater or equal to the maximum |
334 | 333 |
/// Node id. |
335 | 334 |
/// |
336 | 335 |
/// Gives back an integer greater or equal to the maximum Node |
337 | 336 |
/// id. |
338 | 337 |
int maxNodeId() const { return -1;} |
339 | 338 |
|
340 | 339 |
/// \brief Gives back an integer greater or equal to the maximum |
341 | 340 |
/// Arc id. |
342 | 341 |
/// |
343 | 342 |
/// Gives back an integer greater or equal to the maximum Arc |
344 | 343 |
/// id. |
345 | 344 |
int maxArcId() const { return -1;} |
346 | 345 |
|
347 | 346 |
template <typename _Digraph> |
348 | 347 |
struct Constraints { |
349 | 348 |
|
350 | 349 |
void constraints() { |
351 | 350 |
checkConcept<Base, _Digraph >(); |
352 | 351 |
typename _Digraph::Node node; |
353 | 352 |
int nid = digraph.id(node); |
354 | 353 |
nid = digraph.id(node); |
355 | 354 |
node = digraph.nodeFromId(nid); |
356 | 355 |
typename _Digraph::Arc arc; |
357 | 356 |
int eid = digraph.id(arc); |
358 | 357 |
eid = digraph.id(arc); |
359 | 358 |
arc = digraph.arcFromId(eid); |
360 | 359 |
|
361 | 360 |
nid = digraph.maxNodeId(); |
362 | 361 |
ignore_unused_variable_warning(nid); |
363 | 362 |
eid = digraph.maxArcId(); |
364 | 363 |
ignore_unused_variable_warning(eid); |
365 | 364 |
} |
366 | 365 |
|
367 | 366 |
const _Digraph& digraph; |
368 | 367 |
}; |
369 | 368 |
}; |
370 | 369 |
|
371 | 370 |
/// \brief An empty idable base undirected graph class. |
372 | 371 |
/// |
373 | 372 |
/// This class provides beside the core undirected graph features |
374 | 373 |
/// core id functions for the undirected graph structure. The |
375 | 374 |
/// most of the base undirected graphs should conform to this |
376 | 375 |
/// concept. The id's are unique and immutable. |
377 |
template <typename _Base = BaseGraphComponent> |
|
378 |
class IDableGraphComponent : public IDableDigraphComponent<_Base> { |
|
376 |
template <typename BAS = BaseGraphComponent> |
|
377 |
class IDableGraphComponent : public IDableDigraphComponent<BAS> { |
|
379 | 378 |
public: |
380 | 379 |
|
381 |
typedef |
|
380 |
typedef BAS Base; |
|
382 | 381 |
typedef typename Base::Edge Edge; |
383 | 382 |
|
384 |
using IDableDigraphComponent< |
|
383 |
using IDableDigraphComponent<Base>::id; |
|
385 | 384 |
|
386 | 385 |
/// \brief Gives back an unique integer id for the Edge. |
387 | 386 |
/// |
388 | 387 |
/// Gives back an unique integer id for the Edge. |
389 | 388 |
/// |
390 | 389 |
int id(const Edge&) const { return -1;} |
391 | 390 |
|
392 | 391 |
/// \brief Gives back the edge by the unique id. |
393 | 392 |
/// |
394 | 393 |
/// Gives back the edge by the unique id. If the |
395 | 394 |
/// graph does not contain arc with the given id then the |
396 | 395 |
/// result of the function is undetermined. |
397 | 396 |
Edge edgeFromId(int) const { return INVALID;} |
398 | 397 |
|
399 | 398 |
/// \brief Gives back an integer greater or equal to the maximum |
400 | 399 |
/// Edge id. |
401 | 400 |
/// |
402 | 401 |
/// Gives back an integer greater or equal to the maximum Edge |
403 | 402 |
/// id. |
404 | 403 |
int maxEdgeId() const { return -1;} |
405 | 404 |
|
406 | 405 |
template <typename _Graph> |
407 | 406 |
struct Constraints { |
408 | 407 |
|
409 | 408 |
void constraints() { |
410 | 409 |
checkConcept<Base, _Graph >(); |
411 | 410 |
checkConcept<IDableDigraphComponent<Base>, _Graph >(); |
412 | 411 |
typename _Graph::Edge edge; |
413 | 412 |
int ueid = graph.id(edge); |
414 | 413 |
ueid = graph.id(edge); |
415 | 414 |
edge = graph.edgeFromId(ueid); |
416 | 415 |
ueid = graph.maxEdgeId(); |
417 | 416 |
ignore_unused_variable_warning(ueid); |
418 | 417 |
} |
419 | 418 |
|
420 | 419 |
const _Graph& graph; |
421 | 420 |
}; |
422 | 421 |
}; |
423 | 422 |
|
424 | 423 |
/// \brief Skeleton class for graph NodeIt and ArcIt |
425 | 424 |
/// |
426 | 425 |
/// Skeleton class for graph NodeIt and ArcIt. |
427 | 426 |
/// |
428 |
template <typename _Graph, typename _Item> |
|
429 |
class GraphItemIt : public _Item { |
|
427 |
template <typename GR, typename Item> |
|
428 |
class GraphItemIt : public Item { |
|
430 | 429 |
public: |
431 | 430 |
/// \brief Default constructor. |
432 | 431 |
/// |
433 | 432 |
/// @warning The default constructor sets the iterator |
434 | 433 |
/// to an undefined value. |
435 | 434 |
GraphItemIt() {} |
436 | 435 |
/// \brief Copy constructor. |
437 | 436 |
/// |
438 | 437 |
/// Copy constructor. |
439 | 438 |
/// |
440 | 439 |
GraphItemIt(const GraphItemIt& ) {} |
441 | 440 |
/// \brief Sets the iterator to the first item. |
442 | 441 |
/// |
443 | 442 |
/// Sets the iterator to the first item of \c the graph. |
444 | 443 |
/// |
445 |
explicit GraphItemIt(const |
|
444 |
explicit GraphItemIt(const GR&) {} |
|
446 | 445 |
/// \brief Invalid constructor \& conversion. |
447 | 446 |
/// |
448 | 447 |
/// This constructor initializes the item to be invalid. |
449 | 448 |
/// \sa Invalid for more details. |
450 | 449 |
GraphItemIt(Invalid) {} |
451 | 450 |
/// \brief Assign operator for items. |
452 | 451 |
/// |
453 | 452 |
/// The items are assignable. |
454 | 453 |
/// |
455 | 454 |
GraphItemIt& operator=(const GraphItemIt&) { return *this; } |
456 | 455 |
/// \brief Next item. |
457 | 456 |
/// |
458 | 457 |
/// Assign the iterator to the next item. |
459 | 458 |
/// |
460 | 459 |
GraphItemIt& operator++() { return *this; } |
461 | 460 |
/// \brief Equality operator |
462 | 461 |
/// |
463 | 462 |
/// Two iterators are equal if and only if they point to the |
464 | 463 |
/// same object or both are invalid. |
465 | 464 |
bool operator==(const GraphItemIt&) const { return true;} |
466 | 465 |
/// \brief Inequality operator |
467 | 466 |
/// |
468 | 467 |
/// \sa operator==(Node n) |
469 | 468 |
/// |
470 | 469 |
bool operator!=(const GraphItemIt&) const { return true;} |
471 | 470 |
|
472 | 471 |
template<typename _GraphItemIt> |
473 | 472 |
struct Constraints { |
474 | 473 |
void constraints() { |
475 | 474 |
_GraphItemIt it1(g); |
476 | 475 |
_GraphItemIt it2; |
477 | 476 |
|
478 | 477 |
it2 = ++it1; |
479 | 478 |
++it2 = it1; |
480 | 479 |
++(++it1); |
481 | 480 |
|
482 |
|
|
481 |
Item bi = it1; |
|
483 | 482 |
bi = it2; |
484 | 483 |
} |
485 |
|
|
484 |
GR& g; |
|
486 | 485 |
}; |
487 | 486 |
}; |
488 | 487 |
|
489 | 488 |
/// \brief Skeleton class for graph InArcIt and OutArcIt |
490 | 489 |
/// |
491 | 490 |
/// \note Because InArcIt and OutArcIt may not inherit from the same |
492 |
/// base class, the _selector is a additional template parameter. For |
|
493 |
/// InArcIt you should instantiate it with character 'i' and for |
|
491 |
/// base class, the \c sel is a additional template parameter (selector). |
|
492 |
/// For InArcIt you should instantiate it with character 'i' and for |
|
494 | 493 |
/// OutArcIt with 'o'. |
495 |
template <typename _Graph, |
|
496 |
typename _Item = typename _Graph::Arc, |
|
497 |
typename _Base = typename _Graph::Node, |
|
498 |
char _selector = '0'> |
|
499 |
|
|
494 |
template <typename GR, |
|
495 |
typename Item = typename GR::Arc, |
|
496 |
typename Base = typename GR::Node, |
|
497 |
char sel = '0'> |
|
498 |
class GraphIncIt : public Item { |
|
500 | 499 |
public: |
501 | 500 |
/// \brief Default constructor. |
502 | 501 |
/// |
503 | 502 |
/// @warning The default constructor sets the iterator |
504 | 503 |
/// to an undefined value. |
505 | 504 |
GraphIncIt() {} |
506 | 505 |
/// \brief Copy constructor. |
507 | 506 |
/// |
508 | 507 |
/// Copy constructor. |
509 | 508 |
/// |
510 |
GraphIncIt(GraphIncIt const& gi) : |
|
509 |
GraphIncIt(GraphIncIt const& gi) : Item(gi) {} |
|
511 | 510 |
/// \brief Sets the iterator to the first arc incoming into or outgoing |
512 | 511 |
/// from the node. |
513 | 512 |
/// |
514 | 513 |
/// Sets the iterator to the first arc incoming into or outgoing |
515 | 514 |
/// from the node. |
516 | 515 |
/// |
517 |
explicit GraphIncIt(const |
|
516 |
explicit GraphIncIt(const GR&, const Base&) {} |
|
518 | 517 |
/// \brief Invalid constructor \& conversion. |
519 | 518 |
/// |
520 | 519 |
/// This constructor initializes the item to be invalid. |
521 | 520 |
/// \sa Invalid for more details. |
522 | 521 |
GraphIncIt(Invalid) {} |
523 | 522 |
/// \brief Assign operator for iterators. |
524 | 523 |
/// |
525 | 524 |
/// The iterators are assignable. |
526 | 525 |
/// |
527 | 526 |
GraphIncIt& operator=(GraphIncIt const&) { return *this; } |
528 | 527 |
/// \brief Next item. |
529 | 528 |
/// |
530 | 529 |
/// Assign the iterator to the next item. |
531 | 530 |
/// |
532 | 531 |
GraphIncIt& operator++() { return *this; } |
533 | 532 |
|
534 | 533 |
/// \brief Equality operator |
535 | 534 |
/// |
536 | 535 |
/// Two iterators are equal if and only if they point to the |
537 | 536 |
/// same object or both are invalid. |
538 | 537 |
bool operator==(const GraphIncIt&) const { return true;} |
539 | 538 |
|
540 | 539 |
/// \brief Inequality operator |
541 | 540 |
/// |
542 | 541 |
/// \sa operator==(Node n) |
543 | 542 |
/// |
544 | 543 |
bool operator!=(const GraphIncIt&) const { return true;} |
545 | 544 |
|
546 | 545 |
template <typename _GraphIncIt> |
547 | 546 |
struct Constraints { |
548 | 547 |
void constraints() { |
549 |
checkConcept<GraphItem< |
|
548 |
checkConcept<GraphItem<sel>, _GraphIncIt>(); |
|
550 | 549 |
_GraphIncIt it1(graph, node); |
551 | 550 |
_GraphIncIt it2; |
552 | 551 |
|
553 | 552 |
it2 = ++it1; |
554 | 553 |
++it2 = it1; |
555 | 554 |
++(++it1); |
556 |
|
|
555 |
Item e = it1; |
|
557 | 556 |
e = it2; |
558 | 557 |
|
559 | 558 |
} |
560 | 559 |
|
561 |
_Item arc; |
|
562 |
_Base node; |
|
563 |
|
|
560 |
Item arc; |
|
561 |
Base node; |
|
562 |
GR graph; |
|
564 | 563 |
_GraphIncIt it; |
565 | 564 |
}; |
566 | 565 |
}; |
567 | 566 |
|
568 | 567 |
|
569 | 568 |
/// \brief An empty iterable digraph class. |
570 | 569 |
/// |
571 | 570 |
/// This class provides beside the core digraph features |
572 | 571 |
/// iterator based iterable interface for the digraph structure. |
573 | 572 |
/// This concept is part of the Digraph concept. |
574 |
template <typename _Base = BaseDigraphComponent> |
|
575 |
class IterableDigraphComponent : public _Base { |
|
573 |
template <typename BAS = BaseDigraphComponent> |
|
574 |
class IterableDigraphComponent : public BAS { |
|
576 | 575 |
|
577 | 576 |
public: |
578 | 577 |
|
579 |
typedef |
|
578 |
typedef BAS Base; |
|
580 | 579 |
typedef typename Base::Node Node; |
581 | 580 |
typedef typename Base::Arc Arc; |
582 | 581 |
|
583 | 582 |
typedef IterableDigraphComponent Digraph; |
584 | 583 |
|
585 | 584 |
/// \name Base iteration |
586 | 585 |
/// |
587 | 586 |
/// This interface provides functions for iteration on digraph items |
588 | 587 |
/// |
589 | 588 |
/// @{ |
590 | 589 |
|
591 | 590 |
/// \brief Gives back the first node in the iterating order. |
592 | 591 |
/// |
593 | 592 |
/// Gives back the first node in the iterating order. |
594 | 593 |
/// |
595 | 594 |
void first(Node&) const {} |
596 | 595 |
|
597 | 596 |
/// \brief Gives back the next node in the iterating order. |
598 | 597 |
/// |
599 | 598 |
/// Gives back the next node in the iterating order. |
600 | 599 |
/// |
601 | 600 |
void next(Node&) const {} |
602 | 601 |
|
603 | 602 |
/// \brief Gives back the first arc in the iterating order. |
604 | 603 |
/// |
605 | 604 |
/// Gives back the first arc in the iterating order. |
606 | 605 |
/// |
607 | 606 |
void first(Arc&) const {} |
608 | 607 |
|
609 | 608 |
/// \brief Gives back the next arc in the iterating order. |
610 | 609 |
/// |
611 | 610 |
/// Gives back the next arc in the iterating order. |
612 | 611 |
/// |
613 | 612 |
void next(Arc&) const {} |
614 | 613 |
|
615 | 614 |
|
616 | 615 |
/// \brief Gives back the first of the arcs point to the given |
617 | 616 |
/// node. |
618 | 617 |
/// |
619 | 618 |
/// Gives back the first of the arcs point to the given node. |
620 | 619 |
/// |
621 | 620 |
void firstIn(Arc&, const Node&) const {} |
622 | 621 |
|
623 | 622 |
/// \brief Gives back the next of the arcs points to the given |
624 | 623 |
/// node. |
625 | 624 |
/// |
626 | 625 |
/// Gives back the next of the arcs points to the given node. |
627 | 626 |
/// |
... | ... |
@@ -711,787 +710,784 @@ |
711 | 710 |
digraph.first(node); |
712 | 711 |
digraph.next(node); |
713 | 712 |
} |
714 | 713 |
{ |
715 | 714 |
digraph.first(arc); |
716 | 715 |
digraph.next(arc); |
717 | 716 |
} |
718 | 717 |
{ |
719 | 718 |
digraph.firstIn(arc, node); |
720 | 719 |
digraph.nextIn(arc); |
721 | 720 |
} |
722 | 721 |
{ |
723 | 722 |
digraph.firstOut(arc, node); |
724 | 723 |
digraph.nextOut(arc); |
725 | 724 |
} |
726 | 725 |
} |
727 | 726 |
|
728 | 727 |
{ |
729 | 728 |
checkConcept<GraphItemIt<_Digraph, typename _Digraph::Arc>, |
730 | 729 |
typename _Digraph::ArcIt >(); |
731 | 730 |
checkConcept<GraphItemIt<_Digraph, typename _Digraph::Node>, |
732 | 731 |
typename _Digraph::NodeIt >(); |
733 | 732 |
checkConcept<GraphIncIt<_Digraph, typename _Digraph::Arc, |
734 | 733 |
typename _Digraph::Node, 'i'>, typename _Digraph::InArcIt>(); |
735 | 734 |
checkConcept<GraphIncIt<_Digraph, typename _Digraph::Arc, |
736 | 735 |
typename _Digraph::Node, 'o'>, typename _Digraph::OutArcIt>(); |
737 | 736 |
|
738 | 737 |
typename _Digraph::Node n; |
739 | 738 |
typename _Digraph::InArcIt ieit(INVALID); |
740 | 739 |
typename _Digraph::OutArcIt oeit(INVALID); |
741 | 740 |
n = digraph.baseNode(ieit); |
742 | 741 |
n = digraph.runningNode(ieit); |
743 | 742 |
n = digraph.baseNode(oeit); |
744 | 743 |
n = digraph.runningNode(oeit); |
745 | 744 |
ignore_unused_variable_warning(n); |
746 | 745 |
} |
747 | 746 |
} |
748 | 747 |
|
749 | 748 |
const _Digraph& digraph; |
750 | 749 |
|
751 | 750 |
}; |
752 | 751 |
}; |
753 | 752 |
|
754 | 753 |
/// \brief An empty iterable undirected graph class. |
755 | 754 |
/// |
756 | 755 |
/// This class provides beside the core graph features iterator |
757 | 756 |
/// based iterable interface for the undirected graph structure. |
758 | 757 |
/// This concept is part of the Graph concept. |
759 |
template <typename _Base = BaseGraphComponent> |
|
760 |
class IterableGraphComponent : public IterableDigraphComponent<_Base> { |
|
758 |
template <typename BAS = BaseGraphComponent> |
|
759 |
class IterableGraphComponent : public IterableDigraphComponent<BAS> { |
|
761 | 760 |
public: |
762 | 761 |
|
763 |
typedef |
|
762 |
typedef BAS Base; |
|
764 | 763 |
typedef typename Base::Node Node; |
765 | 764 |
typedef typename Base::Arc Arc; |
766 | 765 |
typedef typename Base::Edge Edge; |
767 | 766 |
|
768 | 767 |
|
769 | 768 |
typedef IterableGraphComponent Graph; |
770 | 769 |
|
771 | 770 |
/// \name Base iteration |
772 | 771 |
/// |
773 | 772 |
/// This interface provides functions for iteration on graph items |
774 | 773 |
/// @{ |
775 | 774 |
|
776 |
using IterableDigraphComponent<_Base>::first; |
|
777 |
using IterableDigraphComponent<_Base>::next; |
|
775 |
using IterableDigraphComponent<Base>::first; |
|
776 |
using IterableDigraphComponent<Base>::next; |
|
778 | 777 |
|
779 | 778 |
/// \brief Gives back the first edge in the iterating |
780 | 779 |
/// order. |
781 | 780 |
/// |
782 | 781 |
/// Gives back the first edge in the iterating order. |
783 | 782 |
/// |
784 | 783 |
void first(Edge&) const {} |
785 | 784 |
|
786 | 785 |
/// \brief Gives back the next edge in the iterating |
787 | 786 |
/// order. |
788 | 787 |
/// |
789 | 788 |
/// Gives back the next edge in the iterating order. |
790 | 789 |
/// |
791 | 790 |
void next(Edge&) const {} |
792 | 791 |
|
793 | 792 |
|
794 | 793 |
/// \brief Gives back the first of the edges from the |
795 | 794 |
/// given node. |
796 | 795 |
/// |
797 | 796 |
/// Gives back the first of the edges from the given |
798 | 797 |
/// node. The bool parameter gives back that direction which |
799 | 798 |
/// gives a good direction of the edge so the source of the |
800 | 799 |
/// directed arc is the given node. |
801 | 800 |
void firstInc(Edge&, bool&, const Node&) const {} |
802 | 801 |
|
803 | 802 |
/// \brief Gives back the next of the edges from the |
804 | 803 |
/// given node. |
805 | 804 |
/// |
806 | 805 |
/// Gives back the next of the edges from the given |
807 | 806 |
/// node. The bool parameter should be used as the \c firstInc() |
808 | 807 |
/// use it. |
809 | 808 |
void nextInc(Edge&, bool&) const {} |
810 | 809 |
|
811 |
using IterableDigraphComponent<_Base>::baseNode; |
|
812 |
using IterableDigraphComponent<_Base>::runningNode; |
|
810 |
using IterableDigraphComponent<Base>::baseNode; |
|
811 |
using IterableDigraphComponent<Base>::runningNode; |
|
813 | 812 |
|
814 | 813 |
/// @} |
815 | 814 |
|
816 | 815 |
/// \name Class based iteration |
817 | 816 |
/// |
818 | 817 |
/// This interface provides functions for iteration on graph items |
819 | 818 |
/// |
820 | 819 |
/// @{ |
821 | 820 |
|
822 | 821 |
/// \brief This iterator goes through each node. |
823 | 822 |
/// |
824 | 823 |
/// This iterator goes through each node. |
825 | 824 |
typedef GraphItemIt<Graph, Edge> EdgeIt; |
826 | 825 |
/// \brief This iterator goes trough the incident arcs of a |
827 | 826 |
/// node. |
828 | 827 |
/// |
829 | 828 |
/// This iterator goes trough the incident arcs of a certain |
830 | 829 |
/// node of a graph. |
831 | 830 |
typedef GraphIncIt<Graph, Edge, Node, 'u'> IncEdgeIt; |
832 | 831 |
/// \brief The base node of the iterator. |
833 | 832 |
/// |
834 | 833 |
/// Gives back the base node of the iterator. |
835 | 834 |
Node baseNode(const IncEdgeIt&) const { return INVALID; } |
836 | 835 |
|
837 | 836 |
/// \brief The running node of the iterator. |
838 | 837 |
/// |
839 | 838 |
/// Gives back the running node of the iterator. |
840 | 839 |
Node runningNode(const IncEdgeIt&) const { return INVALID; } |
841 | 840 |
|
842 | 841 |
/// @} |
843 | 842 |
|
844 | 843 |
template <typename _Graph> |
845 | 844 |
struct Constraints { |
846 | 845 |
void constraints() { |
847 | 846 |
checkConcept<IterableDigraphComponent<Base>, _Graph>(); |
848 | 847 |
|
849 | 848 |
{ |
850 | 849 |
typename _Graph::Node node(INVALID); |
851 | 850 |
typename _Graph::Edge edge(INVALID); |
852 | 851 |
bool dir; |
853 | 852 |
{ |
854 | 853 |
graph.first(edge); |
855 | 854 |
graph.next(edge); |
856 | 855 |
} |
857 | 856 |
{ |
858 | 857 |
graph.firstInc(edge, dir, node); |
859 | 858 |
graph.nextInc(edge, dir); |
860 | 859 |
} |
861 | 860 |
|
862 | 861 |
} |
863 | 862 |
|
864 | 863 |
{ |
865 | 864 |
checkConcept<GraphItemIt<_Graph, typename _Graph::Edge>, |
866 | 865 |
typename _Graph::EdgeIt >(); |
867 | 866 |
checkConcept<GraphIncIt<_Graph, typename _Graph::Edge, |
868 | 867 |
typename _Graph::Node, 'u'>, typename _Graph::IncEdgeIt>(); |
869 | 868 |
|
870 | 869 |
typename _Graph::Node n; |
871 | 870 |
typename _Graph::IncEdgeIt ueit(INVALID); |
872 | 871 |
n = graph.baseNode(ueit); |
873 | 872 |
n = graph.runningNode(ueit); |
874 | 873 |
} |
875 | 874 |
} |
876 | 875 |
|
877 | 876 |
const _Graph& graph; |
878 |
|
|
879 | 877 |
}; |
880 | 878 |
}; |
881 | 879 |
|
882 | 880 |
/// \brief An empty alteration notifier digraph class. |
883 | 881 |
/// |
884 | 882 |
/// This class provides beside the core digraph features alteration |
885 | 883 |
/// notifier interface for the digraph structure. This implements |
886 | 884 |
/// an observer-notifier pattern for each digraph item. More |
887 | 885 |
/// obsevers can be registered into the notifier and whenever an |
888 | 886 |
/// alteration occured in the digraph all the observers will |
889 | 887 |
/// notified about it. |
890 |
template <typename _Base = BaseDigraphComponent> |
|
891 |
class AlterableDigraphComponent : public _Base { |
|
888 |
template <typename BAS = BaseDigraphComponent> |
|
889 |
class AlterableDigraphComponent : public BAS { |
|
892 | 890 |
public: |
893 | 891 |
|
894 |
typedef |
|
892 |
typedef BAS Base; |
|
895 | 893 |
typedef typename Base::Node Node; |
896 | 894 |
typedef typename Base::Arc Arc; |
897 | 895 |
|
898 | 896 |
|
899 | 897 |
/// The node observer registry. |
900 | 898 |
typedef AlterationNotifier<AlterableDigraphComponent, Node> |
901 | 899 |
NodeNotifier; |
902 | 900 |
/// The arc observer registry. |
903 | 901 |
typedef AlterationNotifier<AlterableDigraphComponent, Arc> |
904 | 902 |
ArcNotifier; |
905 | 903 |
|
906 | 904 |
/// \brief Gives back the node alteration notifier. |
907 | 905 |
/// |
908 | 906 |
/// Gives back the node alteration notifier. |
909 | 907 |
NodeNotifier& notifier(Node) const { |
910 | 908 |
return NodeNotifier(); |
911 | 909 |
} |
912 | 910 |
|
913 | 911 |
/// \brief Gives back the arc alteration notifier. |
914 | 912 |
/// |
915 | 913 |
/// Gives back the arc alteration notifier. |
916 | 914 |
ArcNotifier& notifier(Arc) const { |
917 | 915 |
return ArcNotifier(); |
918 | 916 |
} |
919 | 917 |
|
920 | 918 |
template <typename _Digraph> |
921 | 919 |
struct Constraints { |
922 | 920 |
void constraints() { |
923 | 921 |
checkConcept<Base, _Digraph>(); |
924 | 922 |
typename _Digraph::NodeNotifier& nn |
925 | 923 |
= digraph.notifier(typename _Digraph::Node()); |
926 | 924 |
|
927 | 925 |
typename _Digraph::ArcNotifier& en |
928 | 926 |
= digraph.notifier(typename _Digraph::Arc()); |
929 | 927 |
|
930 | 928 |
ignore_unused_variable_warning(nn); |
931 | 929 |
ignore_unused_variable_warning(en); |
932 | 930 |
} |
933 | 931 |
|
934 | 932 |
const _Digraph& digraph; |
935 | 933 |
|
936 | 934 |
}; |
937 | 935 |
|
938 | 936 |
}; |
939 | 937 |
|
940 | 938 |
/// \brief An empty alteration notifier undirected graph class. |
941 | 939 |
/// |
942 | 940 |
/// This class provides beside the core graph features alteration |
943 | 941 |
/// notifier interface for the graph structure. This implements |
944 | 942 |
/// an observer-notifier pattern for each graph item. More |
945 | 943 |
/// obsevers can be registered into the notifier and whenever an |
946 | 944 |
/// alteration occured in the graph all the observers will |
947 | 945 |
/// notified about it. |
948 |
template <typename _Base = BaseGraphComponent> |
|
949 |
class AlterableGraphComponent : public AlterableDigraphComponent<_Base> { |
|
946 |
template <typename BAS = BaseGraphComponent> |
|
947 |
class AlterableGraphComponent : public AlterableDigraphComponent<BAS> { |
|
950 | 948 |
public: |
951 | 949 |
|
952 |
typedef |
|
950 |
typedef BAS Base; |
|
953 | 951 |
typedef typename Base::Edge Edge; |
954 | 952 |
|
955 | 953 |
|
956 | 954 |
/// The arc observer registry. |
957 | 955 |
typedef AlterationNotifier<AlterableGraphComponent, Edge> |
958 | 956 |
EdgeNotifier; |
959 | 957 |
|
960 | 958 |
/// \brief Gives back the arc alteration notifier. |
961 | 959 |
/// |
962 | 960 |
/// Gives back the arc alteration notifier. |
963 | 961 |
EdgeNotifier& notifier(Edge) const { |
964 | 962 |
return EdgeNotifier(); |
965 | 963 |
} |
966 | 964 |
|
967 | 965 |
template <typename _Graph> |
968 | 966 |
struct Constraints { |
969 | 967 |
void constraints() { |
970 | 968 |
checkConcept<AlterableGraphComponent<Base>, _Graph>(); |
971 | 969 |
typename _Graph::EdgeNotifier& uen |
972 | 970 |
= graph.notifier(typename _Graph::Edge()); |
973 | 971 |
ignore_unused_variable_warning(uen); |
974 | 972 |
} |
975 | 973 |
|
976 | 974 |
const _Graph& graph; |
977 |
|
|
978 | 975 |
}; |
979 |
|
|
980 | 976 |
}; |
981 | 977 |
|
982 | 978 |
/// \brief Class describing the concept of graph maps |
983 | 979 |
/// |
984 | 980 |
/// This class describes the common interface of the graph maps |
985 | 981 |
/// (NodeMap, ArcMap), that is maps that can be used to |
986 | 982 |
/// associate data to graph descriptors (nodes or arcs). |
987 |
template <typename _Graph, typename _Item, typename _Value> |
|
988 |
class GraphMap : public ReadWriteMap<_Item, _Value> { |
|
983 |
template <typename GR, typename K, typename V> |
|
984 |
class GraphMap : public ReadWriteMap<K, V> { |
|
989 | 985 |
public: |
990 | 986 |
|
991 |
typedef ReadWriteMap< |
|
987 |
typedef ReadWriteMap<K, V> Parent; |
|
992 | 988 |
|
993 | 989 |
/// The graph type of the map. |
994 |
typedef |
|
990 |
typedef GR Graph; |
|
995 | 991 |
/// The key type of the map. |
996 |
typedef |
|
992 |
typedef K Key; |
|
997 | 993 |
/// The value type of the map. |
998 |
typedef |
|
994 |
typedef V Value; |
|
999 | 995 |
|
1000 | 996 |
/// \brief Construct a new map. |
1001 | 997 |
/// |
1002 | 998 |
/// Construct a new map for the graph. |
1003 | 999 |
explicit GraphMap(const Graph&) {} |
1004 | 1000 |
/// \brief Construct a new map with default value. |
1005 | 1001 |
/// |
1006 | 1002 |
/// Construct a new map for the graph and initalise the values. |
1007 | 1003 |
GraphMap(const Graph&, const Value&) {} |
1008 | 1004 |
|
1009 | 1005 |
private: |
1010 | 1006 |
/// \brief Copy constructor. |
1011 | 1007 |
/// |
1012 | 1008 |
/// Copy Constructor. |
1013 | 1009 |
GraphMap(const GraphMap&) : Parent() {} |
1014 | 1010 |
|
1015 | 1011 |
/// \brief Assign operator. |
1016 | 1012 |
/// |
1017 | 1013 |
/// Assign operator. It does not mofify the underlying graph, |
1018 | 1014 |
/// it just iterates on the current item set and set the map |
1019 | 1015 |
/// with the value returned by the assigned map. |
1020 | 1016 |
template <typename CMap> |
1021 | 1017 |
GraphMap& operator=(const CMap&) { |
1022 | 1018 |
checkConcept<ReadMap<Key, Value>, CMap>(); |
1023 | 1019 |
return *this; |
1024 | 1020 |
} |
1025 | 1021 |
|
1026 | 1022 |
public: |
1027 | 1023 |
template<typename _Map> |
1028 | 1024 |
struct Constraints { |
1029 | 1025 |
void constraints() { |
1030 | 1026 |
checkConcept<ReadWriteMap<Key, Value>, _Map >(); |
1031 | 1027 |
// Construction with a graph parameter |
1032 | 1028 |
_Map a(g); |
1033 | 1029 |
// Constructor with a graph and a default value parameter |
1034 | 1030 |
_Map a2(g,t); |
1035 | 1031 |
// Copy constructor. |
1036 | 1032 |
// _Map b(c); |
1037 | 1033 |
|
1038 | 1034 |
// ReadMap<Key, Value> cmap; |
1039 | 1035 |
// b = cmap; |
1040 | 1036 |
|
1041 | 1037 |
ignore_unused_variable_warning(a); |
1042 | 1038 |
ignore_unused_variable_warning(a2); |
1043 | 1039 |
// ignore_unused_variable_warning(b); |
1044 | 1040 |
} |
1045 | 1041 |
|
1046 | 1042 |
const _Map &c; |
1047 | 1043 |
const Graph &g; |
1048 | 1044 |
const typename GraphMap::Value &t; |
1049 | 1045 |
}; |
1050 | 1046 |
|
1051 | 1047 |
}; |
1052 | 1048 |
|
1053 | 1049 |
/// \brief An empty mappable digraph class. |
1054 | 1050 |
/// |
1055 | 1051 |
/// This class provides beside the core digraph features |
1056 | 1052 |
/// map interface for the digraph structure. |
1057 | 1053 |
/// This concept is part of the Digraph concept. |
1058 |
template <typename _Base = BaseDigraphComponent> |
|
1059 |
class MappableDigraphComponent : public _Base { |
|
1054 |
template <typename BAS = BaseDigraphComponent> |
|
1055 |
class MappableDigraphComponent : public BAS { |
|
1060 | 1056 |
public: |
1061 | 1057 |
|
1062 |
typedef |
|
1058 |
typedef BAS Base; |
|
1063 | 1059 |
typedef typename Base::Node Node; |
1064 | 1060 |
typedef typename Base::Arc Arc; |
1065 | 1061 |
|
1066 | 1062 |
typedef MappableDigraphComponent Digraph; |
1067 | 1063 |
|
1068 | 1064 |
/// \brief ReadWrite map of the nodes. |
1069 | 1065 |
/// |
1070 | 1066 |
/// ReadWrite map of the nodes. |
1071 | 1067 |
/// |
1072 |
template <typename _Value> |
|
1073 |
class NodeMap : public GraphMap<Digraph, Node, _Value> { |
|
1068 |
template <typename V> |
|
1069 |
class NodeMap : public GraphMap<Digraph, Node, V> { |
|
1074 | 1070 |
public: |
1075 |
typedef GraphMap<MappableDigraphComponent, Node, |
|
1071 |
typedef GraphMap<MappableDigraphComponent, Node, V> Parent; |
|
1076 | 1072 |
|
1077 | 1073 |
/// \brief Construct a new map. |
1078 | 1074 |
/// |
1079 | 1075 |
/// Construct a new map for the digraph. |
1080 | 1076 |
explicit NodeMap(const MappableDigraphComponent& digraph) |
1081 | 1077 |
: Parent(digraph) {} |
1082 | 1078 |
|
1083 | 1079 |
/// \brief Construct a new map with default value. |
1084 | 1080 |
/// |
1085 | 1081 |
/// Construct a new map for the digraph and initalise the values. |
1086 |
NodeMap(const MappableDigraphComponent& digraph, const |
|
1082 |
NodeMap(const MappableDigraphComponent& digraph, const V& value) |
|
1087 | 1083 |
: Parent(digraph, value) {} |
1088 | 1084 |
|
1089 | 1085 |
private: |
1090 | 1086 |
/// \brief Copy constructor. |
1091 | 1087 |
/// |
1092 | 1088 |
/// Copy Constructor. |
1093 | 1089 |
NodeMap(const NodeMap& nm) : Parent(nm) {} |
1094 | 1090 |
|
1095 | 1091 |
/// \brief Assign operator. |
1096 | 1092 |
/// |
1097 | 1093 |
/// Assign operator. |
1098 | 1094 |
template <typename CMap> |
1099 | 1095 |
NodeMap& operator=(const CMap&) { |
1100 |
checkConcept<ReadMap<Node, |
|
1096 |
checkConcept<ReadMap<Node, V>, CMap>(); |
|
1101 | 1097 |
return *this; |
1102 | 1098 |
} |
1103 | 1099 |
|
1104 | 1100 |
}; |
1105 | 1101 |
|
1106 | 1102 |
/// \brief ReadWrite map of the arcs. |
1107 | 1103 |
/// |
1108 | 1104 |
/// ReadWrite map of the arcs. |
1109 | 1105 |
/// |
1110 |
template <typename _Value> |
|
1111 |
class ArcMap : public GraphMap<Digraph, Arc, _Value> { |
|
1106 |
template <typename V> |
|
1107 |
class ArcMap : public GraphMap<Digraph, Arc, V> { |
|
1112 | 1108 |
public: |
1113 |
typedef GraphMap<MappableDigraphComponent, Arc, |
|
1109 |
typedef GraphMap<MappableDigraphComponent, Arc, V> Parent; |
|
1114 | 1110 |
|
1115 | 1111 |
/// \brief Construct a new map. |
1116 | 1112 |
/// |
1117 | 1113 |
/// Construct a new map for the digraph. |
1118 | 1114 |
explicit ArcMap(const MappableDigraphComponent& digraph) |
1119 | 1115 |
: Parent(digraph) {} |
1120 | 1116 |
|
1121 | 1117 |
/// \brief Construct a new map with default value. |
1122 | 1118 |
/// |
1123 | 1119 |
/// Construct a new map for the digraph and initalise the values. |
1124 |
ArcMap(const MappableDigraphComponent& digraph, const |
|
1120 |
ArcMap(const MappableDigraphComponent& digraph, const V& value) |
|
1125 | 1121 |
: Parent(digraph, value) {} |
1126 | 1122 |
|
1127 | 1123 |
private: |
1128 | 1124 |
/// \brief Copy constructor. |
1129 | 1125 |
/// |
1130 | 1126 |
/// Copy Constructor. |
1131 | 1127 |
ArcMap(const ArcMap& nm) : Parent(nm) {} |
1132 | 1128 |
|
1133 | 1129 |
/// \brief Assign operator. |
1134 | 1130 |
/// |
1135 | 1131 |
/// Assign operator. |
1136 | 1132 |
template <typename CMap> |
1137 | 1133 |
ArcMap& operator=(const CMap&) { |
1138 |
checkConcept<ReadMap<Arc, |
|
1134 |
checkConcept<ReadMap<Arc, V>, CMap>(); |
|
1139 | 1135 |
return *this; |
1140 | 1136 |
} |
1141 | 1137 |
|
1142 | 1138 |
}; |
1143 | 1139 |
|
1144 | 1140 |
|
1145 | 1141 |
template <typename _Digraph> |
1146 | 1142 |
struct Constraints { |
1147 | 1143 |
|
1148 | 1144 |
struct Dummy { |
1149 | 1145 |
int value; |
1150 | 1146 |
Dummy() : value(0) {} |
1151 | 1147 |
Dummy(int _v) : value(_v) {} |
1152 | 1148 |
}; |
1153 | 1149 |
|
1154 | 1150 |
void constraints() { |
1155 | 1151 |
checkConcept<Base, _Digraph>(); |
1156 | 1152 |
{ // int map test |
1157 | 1153 |
typedef typename _Digraph::template NodeMap<int> IntNodeMap; |
1158 | 1154 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Node, int>, |
1159 | 1155 |
IntNodeMap >(); |
1160 | 1156 |
} { // bool map test |
1161 | 1157 |
typedef typename _Digraph::template NodeMap<bool> BoolNodeMap; |
1162 | 1158 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Node, bool>, |
1163 | 1159 |
BoolNodeMap >(); |
1164 | 1160 |
} { // Dummy map test |
1165 | 1161 |
typedef typename _Digraph::template NodeMap<Dummy> DummyNodeMap; |
1166 | 1162 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Node, Dummy>, |
1167 | 1163 |
DummyNodeMap >(); |
1168 | 1164 |
} |
1169 | 1165 |
|
1170 | 1166 |
{ // int map test |
1171 | 1167 |
typedef typename _Digraph::template ArcMap<int> IntArcMap; |
1172 | 1168 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, int>, |
1173 | 1169 |
IntArcMap >(); |
1174 | 1170 |
} { // bool map test |
1175 | 1171 |
typedef typename _Digraph::template ArcMap<bool> BoolArcMap; |
1176 | 1172 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, bool>, |
1177 | 1173 |
BoolArcMap >(); |
1178 | 1174 |
} { // Dummy map test |
1179 | 1175 |
typedef typename _Digraph::template ArcMap<Dummy> DummyArcMap; |
1180 | 1176 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, Dummy>, |
1181 | 1177 |
DummyArcMap >(); |
1182 | 1178 |
} |
1183 | 1179 |
} |
1184 | 1180 |
|
1185 | 1181 |
_Digraph& digraph; |
1186 | 1182 |
}; |
1187 | 1183 |
}; |
1188 | 1184 |
|
1189 | 1185 |
/// \brief An empty mappable base bipartite graph class. |
1190 | 1186 |
/// |
1191 | 1187 |
/// This class provides beside the core graph features |
1192 | 1188 |
/// map interface for the graph structure. |
1193 | 1189 |
/// This concept is part of the Graph concept. |
1194 |
template <typename _Base = BaseGraphComponent> |
|
1195 |
class MappableGraphComponent : public MappableDigraphComponent<_Base> { |
|
1190 |
template <typename BAS = BaseGraphComponent> |
|
1191 |
class MappableGraphComponent : public MappableDigraphComponent<BAS> { |
|
1196 | 1192 |
public: |
1197 | 1193 |
|
1198 |
typedef |
|
1194 |
typedef BAS Base; |
|
1199 | 1195 |
typedef typename Base::Edge Edge; |
1200 | 1196 |
|
1201 | 1197 |
typedef MappableGraphComponent Graph; |
1202 | 1198 |
|
1203 | 1199 |
/// \brief ReadWrite map of the edges. |
1204 | 1200 |
/// |
1205 | 1201 |
/// ReadWrite map of the edges. |
1206 | 1202 |
/// |
1207 |
template <typename _Value> |
|
1208 |
class EdgeMap : public GraphMap<Graph, Edge, _Value> { |
|
1203 |
template <typename V> |
|
1204 |
class EdgeMap : public GraphMap<Graph, Edge, V> { |
|
1209 | 1205 |
public: |
1210 |
typedef GraphMap<MappableGraphComponent, Edge, |
|
1206 |
typedef GraphMap<MappableGraphComponent, Edge, V> Parent; |
|
1211 | 1207 |
|
1212 | 1208 |
/// \brief Construct a new map. |
1213 | 1209 |
/// |
1214 | 1210 |
/// Construct a new map for the graph. |
1215 | 1211 |
explicit EdgeMap(const MappableGraphComponent& graph) |
1216 | 1212 |
: Parent(graph) {} |
1217 | 1213 |
|
1218 | 1214 |
/// \brief Construct a new map with default value. |
1219 | 1215 |
/// |
1220 | 1216 |
/// Construct a new map for the graph and initalise the values. |
1221 |
EdgeMap(const MappableGraphComponent& graph, const |
|
1217 |
EdgeMap(const MappableGraphComponent& graph, const V& value) |
|
1222 | 1218 |
: Parent(graph, value) {} |
1223 | 1219 |
|
1224 | 1220 |
private: |
1225 | 1221 |
/// \brief Copy constructor. |
1226 | 1222 |
/// |
1227 | 1223 |
/// Copy Constructor. |
1228 | 1224 |
EdgeMap(const EdgeMap& nm) : Parent(nm) {} |
1229 | 1225 |
|
1230 | 1226 |
/// \brief Assign operator. |
1231 | 1227 |
/// |
1232 | 1228 |
/// Assign operator. |
1233 | 1229 |
template <typename CMap> |
1234 | 1230 |
EdgeMap& operator=(const CMap&) { |
1235 |
checkConcept<ReadMap<Edge, |
|
1231 |
checkConcept<ReadMap<Edge, V>, CMap>(); |
|
1236 | 1232 |
return *this; |
1237 | 1233 |
} |
1238 | 1234 |
|
1239 | 1235 |
}; |
1240 | 1236 |
|
1241 | 1237 |
|
1242 | 1238 |
template <typename _Graph> |
1243 | 1239 |
struct Constraints { |
1244 | 1240 |
|
1245 | 1241 |
struct Dummy { |
1246 | 1242 |
int value; |
1247 | 1243 |
Dummy() : value(0) {} |
1248 | 1244 |
Dummy(int _v) : value(_v) {} |
1249 | 1245 |
}; |
1250 | 1246 |
|
1251 | 1247 |
void constraints() { |
1252 | 1248 |
checkConcept<MappableGraphComponent<Base>, _Graph>(); |
1253 | 1249 |
|
1254 | 1250 |
{ // int map test |
1255 | 1251 |
typedef typename _Graph::template EdgeMap<int> IntEdgeMap; |
1256 | 1252 |
checkConcept<GraphMap<_Graph, typename _Graph::Edge, int>, |
1257 | 1253 |
IntEdgeMap >(); |
1258 | 1254 |
} { // bool map test |
1259 | 1255 |
typedef typename _Graph::template EdgeMap<bool> BoolEdgeMap; |
1260 | 1256 |
checkConcept<GraphMap<_Graph, typename _Graph::Edge, bool>, |
1261 | 1257 |
BoolEdgeMap >(); |
1262 | 1258 |
} { // Dummy map test |
1263 | 1259 |
typedef typename _Graph::template EdgeMap<Dummy> DummyEdgeMap; |
1264 | 1260 |
checkConcept<GraphMap<_Graph, typename _Graph::Edge, Dummy>, |
1265 | 1261 |
DummyEdgeMap >(); |
1266 | 1262 |
} |
1267 | 1263 |
} |
1268 | 1264 |
|
1269 | 1265 |
_Graph& graph; |
1270 | 1266 |
}; |
1271 | 1267 |
}; |
1272 | 1268 |
|
1273 | 1269 |
/// \brief An empty extendable digraph class. |
1274 | 1270 |
/// |
1275 | 1271 |
/// This class provides beside the core digraph features digraph |
1276 | 1272 |
/// extendable interface for the digraph structure. The main |
1277 | 1273 |
/// difference between the base and this interface is that the |
1278 | 1274 |
/// digraph alterations should handled already on this level. |
1279 |
template <typename _Base = BaseDigraphComponent> |
|
1280 |
class ExtendableDigraphComponent : public _Base { |
|
1275 |
template <typename BAS = BaseDigraphComponent> |
|
1276 |
class ExtendableDigraphComponent : public BAS { |
|
1281 | 1277 |
public: |
1282 |
typedef |
|
1278 |
typedef BAS Base; |
|
1283 | 1279 |
|
1284 |
typedef typename _Base::Node Node; |
|
1285 |
typedef typename _Base::Arc Arc; |
|
1280 |
typedef typename Base::Node Node; |
|
1281 |
typedef typename Base::Arc Arc; |
|
1286 | 1282 |
|
1287 | 1283 |
/// \brief Adds a new node to the digraph. |
1288 | 1284 |
/// |
1289 | 1285 |
/// Adds a new node to the digraph. |
1290 | 1286 |
/// |
1291 | 1287 |
Node addNode() { |
1292 | 1288 |
return INVALID; |
1293 | 1289 |
} |
1294 | 1290 |
|
1295 | 1291 |
/// \brief Adds a new arc connects the given two nodes. |
1296 | 1292 |
/// |
1297 | 1293 |
/// Adds a new arc connects the the given two nodes. |
1298 | 1294 |
Arc addArc(const Node&, const Node&) { |
1299 | 1295 |
return INVALID; |
1300 | 1296 |
} |
1301 | 1297 |
|
1302 | 1298 |
template <typename _Digraph> |
1303 | 1299 |
struct Constraints { |
1304 | 1300 |
void constraints() { |
1305 | 1301 |
checkConcept<Base, _Digraph>(); |
1306 | 1302 |
typename _Digraph::Node node_a, node_b; |
1307 | 1303 |
node_a = digraph.addNode(); |
1308 | 1304 |
node_b = digraph.addNode(); |
1309 | 1305 |
typename _Digraph::Arc arc; |
1310 | 1306 |
arc = digraph.addArc(node_a, node_b); |
1311 | 1307 |
} |
1312 | 1308 |
|
1313 | 1309 |
_Digraph& digraph; |
1314 | 1310 |
}; |
1315 | 1311 |
}; |
1316 | 1312 |
|
1317 | 1313 |
/// \brief An empty extendable base undirected graph class. |
1318 | 1314 |
/// |
1319 | 1315 |
/// This class provides beside the core undirected graph features |
1320 | 1316 |
/// core undircted graph extend interface for the graph structure. |
1321 | 1317 |
/// The main difference between the base and this interface is |
1322 | 1318 |
/// that the graph alterations should handled already on this |
1323 | 1319 |
/// level. |
1324 |
template <typename _Base = BaseGraphComponent> |
|
1325 |
class ExtendableGraphComponent : public _Base { |
|
1320 |
template <typename BAS = BaseGraphComponent> |
|
1321 |
class ExtendableGraphComponent : public BAS { |
|
1326 | 1322 |
public: |
1327 | 1323 |
|
1328 |
typedef _Base Base; |
|
1329 |
typedef typename _Base::Node Node; |
|
1330 |
typedef |
|
1324 |
typedef BAS Base; |
|
1325 |
typedef typename Base::Node Node; |
|
1326 |
typedef typename Base::Edge Edge; |
|
1331 | 1327 |
|
1332 | 1328 |
/// \brief Adds a new node to the graph. |
1333 | 1329 |
/// |
1334 | 1330 |
/// Adds a new node to the graph. |
1335 | 1331 |
/// |
1336 | 1332 |
Node addNode() { |
1337 | 1333 |
return INVALID; |
1338 | 1334 |
} |
1339 | 1335 |
|
1340 | 1336 |
/// \brief Adds a new arc connects the given two nodes. |
1341 | 1337 |
/// |
1342 | 1338 |
/// Adds a new arc connects the the given two nodes. |
1343 | 1339 |
Edge addArc(const Node&, const Node&) { |
1344 | 1340 |
return INVALID; |
1345 | 1341 |
} |
1346 | 1342 |
|
1347 | 1343 |
template <typename _Graph> |
1348 | 1344 |
struct Constraints { |
1349 | 1345 |
void constraints() { |
1350 | 1346 |
checkConcept<Base, _Graph>(); |
1351 | 1347 |
typename _Graph::Node node_a, node_b; |
1352 | 1348 |
node_a = graph.addNode(); |
1353 | 1349 |
node_b = graph.addNode(); |
1354 | 1350 |
typename _Graph::Edge edge; |
1355 | 1351 |
edge = graph.addEdge(node_a, node_b); |
1356 | 1352 |
} |
1357 | 1353 |
|
1358 | 1354 |
_Graph& graph; |
1359 | 1355 |
}; |
1360 | 1356 |
}; |
1361 | 1357 |
|
1362 | 1358 |
/// \brief An empty erasable digraph class. |
1363 | 1359 |
/// |
1364 | 1360 |
/// This class provides beside the core digraph features core erase |
1365 | 1361 |
/// functions for the digraph structure. The main difference between |
1366 | 1362 |
/// the base and this interface is that the digraph alterations |
1367 | 1363 |
/// should handled already on this level. |
1368 |
template <typename _Base = BaseDigraphComponent> |
|
1369 |
class ErasableDigraphComponent : public _Base { |
|
1364 |
template <typename BAS = BaseDigraphComponent> |
|
1365 |
class ErasableDigraphComponent : public BAS { |
|
1370 | 1366 |
public: |
1371 | 1367 |
|
1372 |
typedef |
|
1368 |
typedef BAS Base; |
|
1373 | 1369 |
typedef typename Base::Node Node; |
1374 | 1370 |
typedef typename Base::Arc Arc; |
1375 | 1371 |
|
1376 | 1372 |
/// \brief Erase a node from the digraph. |
1377 | 1373 |
/// |
1378 | 1374 |
/// Erase a node from the digraph. This function should |
1379 | 1375 |
/// erase all arcs connecting to the node. |
1380 | 1376 |
void erase(const Node&) {} |
1381 | 1377 |
|
1382 | 1378 |
/// \brief Erase an arc from the digraph. |
1383 | 1379 |
/// |
1384 | 1380 |
/// Erase an arc from the digraph. |
1385 | 1381 |
/// |
1386 | 1382 |
void erase(const Arc&) {} |
1387 | 1383 |
|
1388 | 1384 |
template <typename _Digraph> |
1389 | 1385 |
struct Constraints { |
1390 | 1386 |
void constraints() { |
1391 | 1387 |
checkConcept<Base, _Digraph>(); |
1392 | 1388 |
typename _Digraph::Node node; |
1393 | 1389 |
digraph.erase(node); |
1394 | 1390 |
typename _Digraph::Arc arc; |
1395 | 1391 |
digraph.erase(arc); |
1396 | 1392 |
} |
1397 | 1393 |
|
1398 | 1394 |
_Digraph& digraph; |
1399 | 1395 |
}; |
1400 | 1396 |
}; |
1401 | 1397 |
|
1402 | 1398 |
/// \brief An empty erasable base undirected graph class. |
1403 | 1399 |
/// |
1404 | 1400 |
/// This class provides beside the core undirected graph features |
1405 | 1401 |
/// core erase functions for the undirceted graph structure. The |
1406 | 1402 |
/// main difference between the base and this interface is that |
1407 | 1403 |
/// the graph alterations should handled already on this level. |
1408 |
template <typename _Base = BaseGraphComponent> |
|
1409 |
class ErasableGraphComponent : public _Base { |
|
1404 |
template <typename BAS = BaseGraphComponent> |
|
1405 |
class ErasableGraphComponent : public BAS { |
|
1410 | 1406 |
public: |
1411 | 1407 |
|
1412 |
typedef |
|
1408 |
typedef BAS Base; |
|
1413 | 1409 |
typedef typename Base::Node Node; |
1414 | 1410 |
typedef typename Base::Edge Edge; |
1415 | 1411 |
|
1416 | 1412 |
/// \brief Erase a node from the graph. |
1417 | 1413 |
/// |
1418 | 1414 |
/// Erase a node from the graph. This function should erase |
1419 | 1415 |
/// arcs connecting to the node. |
1420 | 1416 |
void erase(const Node&) {} |
1421 | 1417 |
|
1422 | 1418 |
/// \brief Erase an arc from the graph. |
1423 | 1419 |
/// |
1424 | 1420 |
/// Erase an arc from the graph. |
1425 | 1421 |
/// |
1426 | 1422 |
void erase(const Edge&) {} |
1427 | 1423 |
|
1428 | 1424 |
template <typename _Graph> |
1429 | 1425 |
struct Constraints { |
1430 | 1426 |
void constraints() { |
1431 | 1427 |
checkConcept<Base, _Graph>(); |
1432 | 1428 |
typename _Graph::Node node; |
1433 | 1429 |
graph.erase(node); |
1434 | 1430 |
typename _Graph::Edge edge; |
1435 | 1431 |
graph.erase(edge); |
1436 | 1432 |
} |
1437 | 1433 |
|
1438 | 1434 |
_Graph& graph; |
1439 | 1435 |
}; |
1440 | 1436 |
}; |
1441 | 1437 |
|
1442 | 1438 |
/// \brief An empty clearable base digraph class. |
1443 | 1439 |
/// |
1444 | 1440 |
/// This class provides beside the core digraph features core clear |
1445 | 1441 |
/// functions for the digraph structure. The main difference between |
1446 | 1442 |
/// the base and this interface is that the digraph alterations |
1447 | 1443 |
/// should handled already on this level. |
1448 |
template <typename _Base = BaseDigraphComponent> |
|
1449 |
class ClearableDigraphComponent : public _Base { |
|
1444 |
template <typename BAS = BaseDigraphComponent> |
|
1445 |
class ClearableDigraphComponent : public BAS { |
|
1450 | 1446 |
public: |
1451 | 1447 |
|
1452 |
typedef |
|
1448 |
typedef BAS Base; |
|
1453 | 1449 |
|
1454 | 1450 |
/// \brief Erase all nodes and arcs from the digraph. |
1455 | 1451 |
/// |
1456 | 1452 |
/// Erase all nodes and arcs from the digraph. |
1457 | 1453 |
/// |
1458 | 1454 |
void clear() {} |
1459 | 1455 |
|
1460 | 1456 |
template <typename _Digraph> |
1461 | 1457 |
struct Constraints { |
1462 | 1458 |
void constraints() { |
1463 | 1459 |
checkConcept<Base, _Digraph>(); |
1464 | 1460 |
digraph.clear(); |
1465 | 1461 |
} |
1466 | 1462 |
|
1467 | 1463 |
_Digraph digraph; |
1468 | 1464 |
}; |
1469 | 1465 |
}; |
1470 | 1466 |
|
1471 | 1467 |
/// \brief An empty clearable base undirected graph class. |
1472 | 1468 |
/// |
1473 | 1469 |
/// This class provides beside the core undirected graph features |
1474 | 1470 |
/// core clear functions for the undirected graph structure. The |
1475 | 1471 |
/// main difference between the base and this interface is that |
1476 | 1472 |
/// the graph alterations should handled already on this level. |
1477 |
template <typename _Base = BaseGraphComponent> |
|
1478 |
class ClearableGraphComponent : public ClearableDigraphComponent<_Base> { |
|
1473 |
template <typename BAS = BaseGraphComponent> |
|
1474 |
class ClearableGraphComponent : public ClearableDigraphComponent<BAS> { |
|
1479 | 1475 |
public: |
1480 | 1476 |
|
1481 |
typedef |
|
1477 |
typedef BAS Base; |
|
1482 | 1478 |
|
1483 | 1479 |
template <typename _Graph> |
1484 | 1480 |
struct Constraints { |
1485 | 1481 |
void constraints() { |
1486 | 1482 |
checkConcept<ClearableGraphComponent<Base>, _Graph>(); |
1487 | 1483 |
} |
1488 | 1484 |
|
1489 | 1485 |
_Graph graph; |
1490 | 1486 |
}; |
1491 | 1487 |
}; |
1492 | 1488 |
|
1493 | 1489 |
} |
1494 | 1490 |
|
1495 | 1491 |
} |
1496 | 1492 |
|
1497 | 1493 |
#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 | 5 |
* Copyright (C) 2003-2009 |
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 concept |
20 | 20 |
///\file |
21 | 21 |
///\brief The concept of heaps. |
22 | 22 |
|
23 | 23 |
#ifndef LEMON_CONCEPTS_HEAP_H |
24 | 24 |
#define LEMON_CONCEPTS_HEAP_H |
25 | 25 |
|
26 | 26 |
#include <lemon/core.h> |
27 | 27 |
#include <lemon/concept_check.h> |
28 | 28 |
|
29 | 29 |
namespace lemon { |
30 | 30 |
|
31 | 31 |
namespace concepts { |
32 | 32 |
|
33 | 33 |
/// \addtogroup concept |
34 | 34 |
/// @{ |
35 | 35 |
|
36 | 36 |
/// \brief The heap concept. |
37 | 37 |
/// |
38 |
/// Concept class describing the main interface of heaps. |
|
39 |
template <typename Priority, typename ItemIntMap> |
|
38 |
/// Concept class describing the main interface of heaps. A \e heap |
|
39 |
/// is a data structure for storing items with specified values called |
|
40 |
/// \e priorities in such a way that finding the item with minimum |
|
41 |
/// priority is efficient. In a heap one can change the priority of an |
|
42 |
/// item, add or erase an item, etc. |
|
43 |
/// |
|
44 |
/// \tparam PR Type of the priority of the items. |
|
45 |
/// \tparam IM A read and writable item map with int values, used |
|
46 |
/// internally to handle the cross references. |
|
47 |
/// \tparam Comp A functor class for the ordering of the priorities. |
|
48 |
/// The default is \c std::less<PR>. |
|
49 |
#ifdef DOXYGEN |
|
50 |
template <typename PR, typename IM, typename Comp = std::less<PR> > |
|
51 |
#else |
|
52 |
template <typename PR, typename IM> |
|
53 |
#endif |
|
40 | 54 |
class Heap { |
41 | 55 |
public: |
42 | 56 |
|
57 |
/// Type of the item-int map. |
|
58 |
typedef IM ItemIntMap; |
|
59 |
/// Type of the priorities. |
|
60 |
typedef PR Prio; |
|
43 | 61 |
/// Type of the items stored in the heap. |
44 | 62 |
typedef typename ItemIntMap::Key Item; |
45 | 63 |
|
46 |
/// Type of the priorities. |
|
47 |
typedef Priority Prio; |
|
48 |
|
|
49 | 64 |
/// \brief Type to represent the states of the items. |
50 | 65 |
/// |
51 | 66 |
/// Each item has a state associated to it. It can be "in heap", |
52 | 67 |
/// "pre heap" or "post heap". The later two are indifferent |
53 | 68 |
/// from the point of view of the heap, but may be useful for |
54 | 69 |
/// the user. |
55 | 70 |
/// |
56 |
/// The \c ItemIntMap must be initialized in such a way, that it |
|
57 |
/// assigns \c PRE_HEAP (<tt>-1</tt>) to every item. |
|
71 |
/// The item-int map must be initialized in such way that it assigns |
|
72 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
|
58 | 73 |
enum State { |
59 |
IN_HEAP = 0, |
|
60 |
PRE_HEAP = -1, |
|
61 |
|
|
74 |
IN_HEAP = 0, ///< The "in heap" state constant. |
|
75 |
PRE_HEAP = -1, ///< The "pre heap" state constant. |
|
76 |
POST_HEAP = -2 ///< The "post heap" state constant. |
|
62 | 77 |
}; |
63 | 78 |
|
64 | 79 |
/// \brief The constructor. |
65 | 80 |
/// |
66 | 81 |
/// The constructor. |
67 | 82 |
/// \param map A map that assigns \c int values to keys of type |
68 | 83 |
/// \c Item. It is used internally by the heap implementations to |
69 | 84 |
/// handle the cross references. The assigned value must be |
70 | 85 |
/// \c PRE_HEAP (<tt>-1</tt>) for every item. |
71 | 86 |
explicit Heap(ItemIntMap &map) {} |
72 | 87 |
|
73 | 88 |
/// \brief The number of items stored in the heap. |
74 | 89 |
/// |
75 | 90 |
/// Returns the number of items stored in the heap. |
76 | 91 |
int size() const { return 0; } |
77 | 92 |
|
78 | 93 |
/// \brief Checks if the heap is empty. |
79 | 94 |
/// |
80 | 95 |
/// Returns \c true if the heap is empty. |
81 | 96 |
bool empty() const { return false; } |
82 | 97 |
|
83 | 98 |
/// \brief Makes the heap empty. |
84 | 99 |
/// |
85 | 100 |
/// Makes the heap empty. |
86 | 101 |
void clear(); |
87 | 102 |
|
88 | 103 |
/// \brief Inserts an item into the heap with the given priority. |
89 | 104 |
/// |
90 | 105 |
/// Inserts the given item into the heap with the given priority. |
91 | 106 |
/// \param i The item to insert. |
92 | 107 |
/// \param p The priority of the item. |
93 | 108 |
void push(const Item &i, const Prio &p) {} |
94 | 109 |
|
95 | 110 |
/// \brief Returns the item having minimum priority. |
96 | 111 |
/// |
97 | 112 |
/// Returns the item having minimum priority. |
98 | 113 |
/// \pre The heap must be non-empty. |
99 | 114 |
Item top() const {} |
100 | 115 |
|
101 | 116 |
/// \brief The minimum priority. |
102 | 117 |
/// |
103 | 118 |
/// Returns the minimum priority. |
104 | 119 |
/// \pre The heap must be non-empty. |
105 | 120 |
Prio prio() const {} |
106 | 121 |
|
107 | 122 |
/// \brief Removes the item having minimum priority. |
108 | 123 |
/// |
109 | 124 |
/// Removes the item having minimum priority. |
110 | 125 |
/// \pre The heap must be non-empty. |
111 | 126 |
void pop() {} |
112 | 127 |
|
113 | 128 |
/// \brief Removes an item from the heap. |
114 | 129 |
/// |
115 | 130 |
/// Removes the given item from the heap if it is already stored. |
116 | 131 |
/// \param i The item to delete. |
117 | 132 |
void erase(const Item &i) {} |
118 | 133 |
|
119 | 134 |
/// \brief The priority of an item. |
120 | 135 |
/// |
121 | 136 |
/// Returns the priority of the given item. |
137 |
/// \param i The item. |
|
122 | 138 |
/// \pre \c i must be in the heap. |
123 |
/// \param i The item. |
|
124 | 139 |
Prio operator[](const Item &i) const {} |
125 | 140 |
|
126 | 141 |
/// \brief Sets the priority of an item or inserts it, if it is |
127 | 142 |
/// not stored in the heap. |
128 | 143 |
/// |
129 | 144 |
/// This method sets the priority of the given item if it is |
130 | 145 |
/// already stored in the heap. |
131 | 146 |
/// Otherwise it inserts the given item with the given priority. |
132 | 147 |
/// |
133 | 148 |
/// \param i The item. |
134 | 149 |
/// \param p The priority. |
135 | 150 |
void set(const Item &i, const Prio &p) {} |
136 | 151 |
|
137 | 152 |
/// \brief Decreases the priority of an item to the given value. |
138 | 153 |
/// |
139 | 154 |
/// Decreases the priority of an item to the given value. |
140 |
/// \pre \c i must be stored in the heap with priority at least \c p. |
|
141 | 155 |
/// \param i The item. |
142 | 156 |
/// \param p The priority. |
157 |
/// \pre \c i must be stored in the heap with priority at least \c p. |
|
143 | 158 |
void decrease(const Item &i, const Prio &p) {} |
144 | 159 |
|
145 | 160 |
/// \brief Increases the priority of an item to the given value. |
146 | 161 |
/// |
147 | 162 |
/// Increases the priority of an item to the given value. |
148 |
/// \pre \c i must be stored in the heap with priority at most \c p. |
|
149 | 163 |
/// \param i The item. |
150 | 164 |
/// \param p The priority. |
165 |
/// \pre \c i must be stored in the heap with priority at most \c p. |
|
151 | 166 |
void increase(const Item &i, const Prio &p) {} |
152 | 167 |
|
153 | 168 |
/// \brief Returns if an item is in, has already been in, or has |
154 | 169 |
/// never been in the heap. |
155 | 170 |
/// |
156 | 171 |
/// This method returns \c PRE_HEAP if the given item has never |
157 | 172 |
/// been in the heap, \c IN_HEAP if it is in the heap at the moment, |
158 | 173 |
/// and \c POST_HEAP otherwise. |
159 | 174 |
/// In the latter case it is possible that the item will get back |
160 | 175 |
/// to the heap again. |
161 | 176 |
/// \param i The item. |
162 | 177 |
State state(const Item &i) const {} |
163 | 178 |
|
164 | 179 |
/// \brief Sets the state of an item in the heap. |
165 | 180 |
/// |
166 | 181 |
/// Sets the state of the given item in the heap. It can be used |
167 | 182 |
/// to manually clear the heap when it is important to achive the |
168 | 183 |
/// better time complexity. |
169 | 184 |
/// \param i The item. |
170 | 185 |
/// \param st The state. It should not be \c IN_HEAP. |
171 | 186 |
void state(const Item& i, State st) {} |
172 | 187 |
|
173 | 188 |
|
174 | 189 |
template <typename _Heap> |
175 | 190 |
struct Constraints { |
176 | 191 |
public: |
177 | 192 |
void constraints() { |
178 | 193 |
typedef typename _Heap::Item OwnItem; |
179 | 194 |
typedef typename _Heap::Prio OwnPrio; |
180 | 195 |
typedef typename _Heap::State OwnState; |
181 | 196 |
|
182 | 197 |
Item item; |
183 | 198 |
Prio prio; |
184 | 199 |
item=Item(); |
185 | 200 |
prio=Prio(); |
186 | 201 |
ignore_unused_variable_warning(item); |
187 | 202 |
ignore_unused_variable_warning(prio); |
188 | 203 |
|
189 | 204 |
OwnItem own_item; |
190 | 205 |
OwnPrio own_prio; |
191 | 206 |
OwnState own_state; |
192 | 207 |
own_item=Item(); |
193 | 208 |
own_prio=Prio(); |
194 | 209 |
ignore_unused_variable_warning(own_item); |
195 | 210 |
ignore_unused_variable_warning(own_prio); |
196 | 211 |
ignore_unused_variable_warning(own_state); |
197 | 212 |
|
198 | 213 |
_Heap heap1(map); |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
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 concept |
20 | 20 |
///\file |
21 | 21 |
///\brief Classes for representing paths in digraphs. |
22 | 22 |
/// |
23 | 23 |
|
24 | 24 |
#ifndef LEMON_CONCEPTS_PATH_H |
25 | 25 |
#define LEMON_CONCEPTS_PATH_H |
26 | 26 |
|
27 | 27 |
#include <lemon/core.h> |
28 | 28 |
#include <lemon/concept_check.h> |
29 | 29 |
|
30 | 30 |
namespace lemon { |
31 | 31 |
namespace concepts { |
32 | 32 |
|
33 | 33 |
/// \addtogroup concept |
34 | 34 |
/// @{ |
35 | 35 |
|
36 | 36 |
/// \brief A skeleton structure for representing directed paths in |
37 | 37 |
/// a digraph. |
38 | 38 |
/// |
39 | 39 |
/// A skeleton structure for representing directed paths in a |
40 | 40 |
/// digraph. |
41 |
/// \tparam |
|
41 |
/// \tparam GR The digraph type in which the path is. |
|
42 | 42 |
/// |
43 | 43 |
/// In a sense, the path can be treated as a list of arcs. The |
44 | 44 |
/// lemon path type stores just this list. As a consequence it |
45 | 45 |
/// cannot enumerate the nodes in the path and the zero length |
46 | 46 |
/// paths cannot store the source. |
47 | 47 |
/// |
48 |
template <typename |
|
48 |
template <typename GR> |
|
49 | 49 |
class Path { |
50 | 50 |
public: |
51 | 51 |
|
52 | 52 |
/// Type of the underlying digraph. |
53 |
typedef |
|
53 |
typedef GR Digraph; |
|
54 | 54 |
/// Arc type of the underlying digraph. |
55 | 55 |
typedef typename Digraph::Arc Arc; |
56 | 56 |
|
57 | 57 |
class ArcIt; |
58 | 58 |
|
59 | 59 |
/// \brief Default constructor |
60 | 60 |
Path() {} |
61 | 61 |
|
62 | 62 |
/// \brief Template constructor |
63 | 63 |
template <typename CPath> |
64 | 64 |
Path(const CPath& cpath) {} |
65 | 65 |
|
66 | 66 |
/// \brief Template assigment |
67 | 67 |
template <typename CPath> |
68 | 68 |
Path& operator=(const CPath& cpath) { |
69 | 69 |
ignore_unused_variable_warning(cpath); |
70 | 70 |
return *this; |
71 | 71 |
} |
72 | 72 |
|
73 | 73 |
/// Length of the path ie. the number of arcs in the path. |
74 | 74 |
int length() const { return 0;} |
75 | 75 |
|
76 | 76 |
/// Returns whether the path is empty. |
77 | 77 |
bool empty() const { return true;} |
78 | 78 |
|
79 | 79 |
/// Resets the path to an empty path. |
80 | 80 |
void clear() {} |
81 | 81 |
|
82 | 82 |
/// \brief LEMON style iterator for path arcs |
83 | 83 |
/// |
84 | 84 |
/// This class is used to iterate on the arcs of the paths. |
85 | 85 |
class ArcIt { |
86 | 86 |
public: |
87 | 87 |
/// Default constructor |
88 | 88 |
ArcIt() {} |
89 | 89 |
/// Invalid constructor |
90 | 90 |
ArcIt(Invalid) {} |
91 | 91 |
/// Constructor for first arc |
92 | 92 |
ArcIt(const Path &) {} |
93 | 93 |
|
94 | 94 |
/// Conversion to Arc |
95 | 95 |
operator Arc() const { return INVALID; } |
96 | 96 |
|
97 | 97 |
/// Next arc |
98 | 98 |
ArcIt& operator++() {return *this;} |
99 | 99 |
|
100 | 100 |
/// Comparison operator |
101 | 101 |
bool operator==(const ArcIt&) const {return true;} |
... | ... |
@@ -160,108 +160,107 @@ |
160 | 160 |
} |
161 | 161 |
_Path& p; |
162 | 162 |
}; |
163 | 163 |
|
164 | 164 |
template <typename _Digraph, typename _Path> |
165 | 165 |
struct PathDumperConstraints< |
166 | 166 |
_Digraph, _Path, |
167 | 167 |
typename enable_if<typename _Path::RevPathTag, void>::type |
168 | 168 |
> { |
169 | 169 |
void constraints() { |
170 | 170 |
int l = p.length(); |
171 | 171 |
int e = p.empty(); |
172 | 172 |
|
173 | 173 |
typename _Path::RevArcIt id, i(p); |
174 | 174 |
|
175 | 175 |
++i; |
176 | 176 |
typename _Digraph::Arc ed = i; |
177 | 177 |
|
178 | 178 |
e = (i == INVALID); |
179 | 179 |
e = (i != INVALID); |
180 | 180 |
|
181 | 181 |
ignore_unused_variable_warning(l); |
182 | 182 |
ignore_unused_variable_warning(e); |
183 | 183 |
ignore_unused_variable_warning(id); |
184 | 184 |
ignore_unused_variable_warning(ed); |
185 | 185 |
} |
186 | 186 |
_Path& p; |
187 | 187 |
}; |
188 | 188 |
|
189 | 189 |
} |
190 | 190 |
|
191 | 191 |
|
192 | 192 |
/// \brief A skeleton structure for path dumpers. |
193 | 193 |
/// |
194 | 194 |
/// A skeleton structure for path dumpers. The path dumpers are |
195 | 195 |
/// the generalization of the paths. The path dumpers can |
196 | 196 |
/// enumerate the arcs of the path wheter in forward or in |
197 | 197 |
/// backward order. In most time these classes are not used |
198 | 198 |
/// directly rather it used to assign a dumped class to a real |
199 | 199 |
/// path type. |
200 | 200 |
/// |
201 | 201 |
/// The main purpose of this concept is that the shortest path |
202 | 202 |
/// algorithms can enumerate easily the arcs in reverse order. |
203 | 203 |
/// If we would like to give back a real path from these |
204 | 204 |
/// algorithms then we should create a temporarly path object. In |
205 | 205 |
/// LEMON such algorithms gives back a path dumper what can |
206 | 206 |
/// assigned to a real path and the dumpers can be implemented as |
207 | 207 |
/// an adaptor class to the predecessor map. |
208 |
|
|
209 |
/// \tparam _Digraph The digraph type in which the path is. |
|
208 |
/// |
|
209 |
/// \tparam GR The digraph type in which the path is. |
|
210 | 210 |
/// |
211 | 211 |
/// The paths can be constructed from any path type by a |
212 | 212 |
/// template constructor or a template assignment operator. |
213 |
/// |
|
214 |
template <typename _Digraph> |
|
213 |
template <typename GR> |
|
215 | 214 |
class PathDumper { |
216 | 215 |
public: |
217 | 216 |
|
218 | 217 |
/// Type of the underlying digraph. |
219 |
typedef |
|
218 |
typedef GR Digraph; |
|
220 | 219 |
/// Arc type of the underlying digraph. |
221 | 220 |
typedef typename Digraph::Arc Arc; |
222 | 221 |
|
223 | 222 |
/// Length of the path ie. the number of arcs in the path. |
224 | 223 |
int length() const { return 0;} |
225 | 224 |
|
226 | 225 |
/// Returns whether the path is empty. |
227 | 226 |
bool empty() const { return true;} |
228 | 227 |
|
229 | 228 |
/// \brief Forward or reverse dumping |
230 | 229 |
/// |
231 | 230 |
/// If the RevPathTag is defined and true then reverse dumping |
232 | 231 |
/// is provided in the path dumper. In this case instead of the |
233 | 232 |
/// ArcIt the RevArcIt iterator should be implemented in the |
234 | 233 |
/// dumper. |
235 | 234 |
typedef False RevPathTag; |
236 | 235 |
|
237 | 236 |
/// \brief LEMON style iterator for path arcs |
238 | 237 |
/// |
239 | 238 |
/// This class is used to iterate on the arcs of the paths. |
240 | 239 |
class ArcIt { |
241 | 240 |
public: |
242 | 241 |
/// Default constructor |
243 | 242 |
ArcIt() {} |
244 | 243 |
/// Invalid constructor |
245 | 244 |
ArcIt(Invalid) {} |
246 | 245 |
/// Constructor for first arc |
247 | 246 |
ArcIt(const PathDumper&) {} |
248 | 247 |
|
249 | 248 |
/// Conversion to Arc |
250 | 249 |
operator Arc() const { return INVALID; } |
251 | 250 |
|
252 | 251 |
/// Next arc |
253 | 252 |
ArcIt& operator++() {return *this;} |
254 | 253 |
|
255 | 254 |
/// Comparison operator |
256 | 255 |
bool operator==(const ArcIt&) const {return true;} |
257 | 256 |
/// Comparison operator |
258 | 257 |
bool operator!=(const ArcIt&) const {return true;} |
259 | 258 |
/// Comparison operator |
260 | 259 |
bool operator<(const ArcIt&) const {return false;} |
261 | 260 |
|
262 | 261 |
}; |
263 | 262 |
|
264 | 263 |
/// \brief LEMON style iterator for path arcs |
265 | 264 |
/// |
266 | 265 |
/// This class is used to iterate on the arcs of the paths in |
267 | 266 |
/// reverse direction. |
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 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_CONNECTIVITY_H |
20 | 20 |
#define LEMON_CONNECTIVITY_H |
21 | 21 |
|
22 | 22 |
#include <lemon/dfs.h> |
23 | 23 |
#include <lemon/bfs.h> |
24 | 24 |
#include <lemon/core.h> |
25 | 25 |
#include <lemon/maps.h> |
26 | 26 |
#include <lemon/adaptors.h> |
27 | 27 |
|
28 | 28 |
#include <lemon/concepts/digraph.h> |
29 | 29 |
#include <lemon/concepts/graph.h> |
30 | 30 |
#include <lemon/concept_check.h> |
31 | 31 |
|
32 | 32 |
#include <stack> |
33 | 33 |
#include <functional> |
34 | 34 |
|
35 | 35 |
/// \ingroup connectivity |
36 | 36 |
/// \file |
37 | 37 |
/// \brief Connectivity algorithms |
38 | 38 |
/// |
39 | 39 |
/// Connectivity algorithms |
40 | 40 |
|
41 | 41 |
namespace lemon { |
42 | 42 |
|
43 | 43 |
/// \ingroup connectivity |
44 | 44 |
/// |
45 | 45 |
/// \brief Check whether the given undirected graph is connected. |
46 | 46 |
/// |
47 | 47 |
/// Check whether the given undirected graph is connected. |
48 | 48 |
/// \param graph The undirected graph. |
49 |
/// \return |
|
49 |
/// \return \c true when there is path between any two nodes in the graph. |
|
50 | 50 |
/// \note By definition, the empty graph is connected. |
51 | 51 |
template <typename Graph> |
52 | 52 |
bool connected(const Graph& graph) { |
53 | 53 |
checkConcept<concepts::Graph, Graph>(); |
54 | 54 |
typedef typename Graph::NodeIt NodeIt; |
55 | 55 |
if (NodeIt(graph) == INVALID) return true; |
56 | 56 |
Dfs<Graph> dfs(graph); |
57 | 57 |
dfs.run(NodeIt(graph)); |
58 | 58 |
for (NodeIt it(graph); it != INVALID; ++it) { |
59 | 59 |
if (!dfs.reached(it)) { |
60 | 60 |
return false; |
61 | 61 |
} |
62 | 62 |
} |
63 | 63 |
return true; |
64 | 64 |
} |
65 | 65 |
|
66 | 66 |
/// \ingroup connectivity |
67 | 67 |
/// |
68 | 68 |
/// \brief Count the number of connected components of an undirected graph |
69 | 69 |
/// |
70 | 70 |
/// Count the number of connected components of an undirected graph |
71 | 71 |
/// |
72 | 72 |
/// \param graph The graph. It must be undirected. |
73 | 73 |
/// \return The number of components |
74 | 74 |
/// \note By definition, the empty graph consists |
75 | 75 |
/// of zero connected components. |
76 | 76 |
template <typename Graph> |
77 | 77 |
int countConnectedComponents(const Graph &graph) { |
78 | 78 |
checkConcept<concepts::Graph, Graph>(); |
79 | 79 |
typedef typename Graph::Node Node; |
80 | 80 |
typedef typename Graph::Arc Arc; |
81 | 81 |
|
82 | 82 |
typedef NullMap<Node, Arc> PredMap; |
83 | 83 |
typedef NullMap<Node, int> DistMap; |
84 | 84 |
|
85 | 85 |
int compNum = 0; |
86 | 86 |
typename Bfs<Graph>:: |
87 | 87 |
template SetPredMap<PredMap>:: |
88 | 88 |
template SetDistMap<DistMap>:: |
89 | 89 |
Create bfs(graph); |
90 | 90 |
|
91 | 91 |
PredMap predMap; |
92 | 92 |
bfs.predMap(predMap); |
93 | 93 |
|
94 | 94 |
DistMap distMap; |
95 | 95 |
bfs.distMap(distMap); |
96 | 96 |
|
97 | 97 |
bfs.init(); |
... | ... |
@@ -189,97 +189,97 @@ |
189 | 189 |
|
190 | 190 |
template <typename Digraph, typename ArcMap> |
191 | 191 |
struct StronglyConnectedCutArcsVisitor : public DfsVisitor<Digraph> { |
192 | 192 |
public: |
193 | 193 |
typedef typename Digraph::Node Node; |
194 | 194 |
typedef typename Digraph::Arc Arc; |
195 | 195 |
|
196 | 196 |
StronglyConnectedCutArcsVisitor(const Digraph& digraph, |
197 | 197 |
ArcMap& cutMap, |
198 | 198 |
int& cutNum) |
199 | 199 |
: _digraph(digraph), _cutMap(cutMap), _cutNum(cutNum), |
200 | 200 |
_compMap(digraph, -1), _num(-1) { |
201 | 201 |
} |
202 | 202 |
|
203 | 203 |
void start(const Node&) { |
204 | 204 |
++_num; |
205 | 205 |
} |
206 | 206 |
|
207 | 207 |
void reach(const Node& node) { |
208 | 208 |
_compMap.set(node, _num); |
209 | 209 |
} |
210 | 210 |
|
211 | 211 |
void examine(const Arc& arc) { |
212 | 212 |
if (_compMap[_digraph.source(arc)] != |
213 | 213 |
_compMap[_digraph.target(arc)]) { |
214 | 214 |
_cutMap.set(arc, true); |
215 | 215 |
++_cutNum; |
216 | 216 |
} |
217 | 217 |
} |
218 | 218 |
private: |
219 | 219 |
const Digraph& _digraph; |
220 | 220 |
ArcMap& _cutMap; |
221 | 221 |
int& _cutNum; |
222 | 222 |
|
223 | 223 |
typename Digraph::template NodeMap<int> _compMap; |
224 | 224 |
int _num; |
225 | 225 |
}; |
226 | 226 |
|
227 | 227 |
} |
228 | 228 |
|
229 | 229 |
|
230 | 230 |
/// \ingroup connectivity |
231 | 231 |
/// |
232 | 232 |
/// \brief Check whether the given directed graph is strongly connected. |
233 | 233 |
/// |
234 | 234 |
/// Check whether the given directed graph is strongly connected. The |
235 | 235 |
/// graph is strongly connected when any two nodes of the graph are |
236 | 236 |
/// connected with directed paths in both direction. |
237 |
/// \return |
|
237 |
/// \return \c false when the graph is not strongly connected. |
|
238 | 238 |
/// \see connected |
239 | 239 |
/// |
240 | 240 |
/// \note By definition, the empty graph is strongly connected. |
241 | 241 |
template <typename Digraph> |
242 | 242 |
bool stronglyConnected(const Digraph& digraph) { |
243 | 243 |
checkConcept<concepts::Digraph, Digraph>(); |
244 | 244 |
|
245 | 245 |
typedef typename Digraph::Node Node; |
246 | 246 |
typedef typename Digraph::NodeIt NodeIt; |
247 | 247 |
|
248 | 248 |
typename Digraph::Node source = NodeIt(digraph); |
249 | 249 |
if (source == INVALID) return true; |
250 | 250 |
|
251 | 251 |
using namespace _connectivity_bits; |
252 | 252 |
|
253 | 253 |
typedef DfsVisitor<Digraph> Visitor; |
254 | 254 |
Visitor visitor; |
255 | 255 |
|
256 | 256 |
DfsVisit<Digraph, Visitor> dfs(digraph, visitor); |
257 | 257 |
dfs.init(); |
258 | 258 |
dfs.addSource(source); |
259 | 259 |
dfs.start(); |
260 | 260 |
|
261 | 261 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
262 | 262 |
if (!dfs.reached(it)) { |
263 | 263 |
return false; |
264 | 264 |
} |
265 | 265 |
} |
266 | 266 |
|
267 | 267 |
typedef ReverseDigraph<const Digraph> RDigraph; |
268 | 268 |
typedef typename RDigraph::NodeIt RNodeIt; |
269 | 269 |
RDigraph rdigraph(digraph); |
270 | 270 |
|
271 | 271 |
typedef DfsVisitor<Digraph> RVisitor; |
272 | 272 |
RVisitor rvisitor; |
273 | 273 |
|
274 | 274 |
DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor); |
275 | 275 |
rdfs.init(); |
276 | 276 |
rdfs.addSource(source); |
277 | 277 |
rdfs.start(); |
278 | 278 |
|
279 | 279 |
for (RNodeIt it(rdigraph); it != INVALID; ++it) { |
280 | 280 |
if (!rdfs.reached(it)) { |
281 | 281 |
return false; |
282 | 282 |
} |
283 | 283 |
} |
284 | 284 |
|
285 | 285 |
return true; |
... | ... |
@@ -664,97 +664,97 @@ |
664 | 664 |
void backtrack(const Arc& edge) { |
665 | 665 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
666 | 666 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
667 | 667 |
} |
668 | 668 |
if (_numMap[_graph.source(edge)] <= _retMap[_graph.target(edge)]) { |
669 | 669 |
if (_predMap[_graph.source(edge)] != INVALID) { |
670 | 670 |
if (!_cutMap[_graph.source(edge)]) { |
671 | 671 |
_cutMap.set(_graph.source(edge), true); |
672 | 672 |
++_cutNum; |
673 | 673 |
} |
674 | 674 |
} else if (rootCut) { |
675 | 675 |
if (!_cutMap[_graph.source(edge)]) { |
676 | 676 |
_cutMap.set(_graph.source(edge), true); |
677 | 677 |
++_cutNum; |
678 | 678 |
} |
679 | 679 |
} else { |
680 | 680 |
rootCut = true; |
681 | 681 |
} |
682 | 682 |
} |
683 | 683 |
} |
684 | 684 |
|
685 | 685 |
private: |
686 | 686 |
const Digraph& _graph; |
687 | 687 |
NodeMap& _cutMap; |
688 | 688 |
int& _cutNum; |
689 | 689 |
|
690 | 690 |
typename Digraph::template NodeMap<int> _numMap; |
691 | 691 |
typename Digraph::template NodeMap<int> _retMap; |
692 | 692 |
typename Digraph::template NodeMap<Node> _predMap; |
693 | 693 |
std::stack<Edge> _edgeStack; |
694 | 694 |
int _num; |
695 | 695 |
bool rootCut; |
696 | 696 |
}; |
697 | 697 |
|
698 | 698 |
} |
699 | 699 |
|
700 | 700 |
template <typename Graph> |
701 | 701 |
int countBiNodeConnectedComponents(const Graph& graph); |
702 | 702 |
|
703 | 703 |
/// \ingroup connectivity |
704 | 704 |
/// |
705 | 705 |
/// \brief Checks the graph is bi-node-connected. |
706 | 706 |
/// |
707 | 707 |
/// This function checks that the undirected graph is bi-node-connected |
708 | 708 |
/// graph. The graph is bi-node-connected if any two undirected edge is |
709 | 709 |
/// on same circle. |
710 | 710 |
/// |
711 | 711 |
/// \param graph The graph. |
712 |
/// \return |
|
712 |
/// \return \c true when the graph bi-node-connected. |
|
713 | 713 |
template <typename Graph> |
714 | 714 |
bool biNodeConnected(const Graph& graph) { |
715 | 715 |
return countBiNodeConnectedComponents(graph) <= 1; |
716 | 716 |
} |
717 | 717 |
|
718 | 718 |
/// \ingroup connectivity |
719 | 719 |
/// |
720 | 720 |
/// \brief Count the biconnected components. |
721 | 721 |
/// |
722 | 722 |
/// This function finds the bi-node-connected components in an undirected |
723 | 723 |
/// graph. The biconnected components are the classes of an equivalence |
724 | 724 |
/// relation on the undirected edges. Two undirected edge is in relationship |
725 | 725 |
/// when they are on same circle. |
726 | 726 |
/// |
727 | 727 |
/// \param graph The graph. |
728 | 728 |
/// \return The number of components. |
729 | 729 |
template <typename Graph> |
730 | 730 |
int countBiNodeConnectedComponents(const Graph& graph) { |
731 | 731 |
checkConcept<concepts::Graph, Graph>(); |
732 | 732 |
typedef typename Graph::NodeIt NodeIt; |
733 | 733 |
|
734 | 734 |
using namespace _connectivity_bits; |
735 | 735 |
|
736 | 736 |
typedef CountBiNodeConnectedComponentsVisitor<Graph> Visitor; |
737 | 737 |
|
738 | 738 |
int compNum = 0; |
739 | 739 |
Visitor visitor(graph, compNum); |
740 | 740 |
|
741 | 741 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
742 | 742 |
dfs.init(); |
743 | 743 |
|
744 | 744 |
for (NodeIt it(graph); it != INVALID; ++it) { |
745 | 745 |
if (!dfs.reached(it)) { |
746 | 746 |
dfs.addSource(it); |
747 | 747 |
dfs.start(); |
748 | 748 |
} |
749 | 749 |
} |
750 | 750 |
return compNum; |
751 | 751 |
} |
752 | 752 |
|
753 | 753 |
/// \ingroup connectivity |
754 | 754 |
/// |
755 | 755 |
/// \brief Find the bi-node-connected components. |
756 | 756 |
/// |
757 | 757 |
/// This function finds the bi-node-connected components in an undirected |
758 | 758 |
/// graph. The bi-node-connected components are the classes of an equivalence |
759 | 759 |
/// relation on the undirected edges. Two undirected edge are in relationship |
760 | 760 |
/// when they are on same circle. |
... | ... |
@@ -1185,356 +1185,356 @@ |
1185 | 1185 |
/// |
1186 | 1186 |
/// Sort the nodes of a DAG into topolgical order. |
1187 | 1187 |
/// |
1188 | 1188 |
/// \param graph The graph. It must be directed and acyclic. |
1189 | 1189 |
/// \retval order A writable node map. The values will be set from 0 to |
1190 | 1190 |
/// the number of the nodes in the graph minus one. Each values of the map |
1191 | 1191 |
/// will be set exactly once, the values will be set descending order. |
1192 | 1192 |
/// |
1193 | 1193 |
/// \see checkedTopologicalSort |
1194 | 1194 |
/// \see dag |
1195 | 1195 |
template <typename Digraph, typename NodeMap> |
1196 | 1196 |
void topologicalSort(const Digraph& graph, NodeMap& order) { |
1197 | 1197 |
using namespace _connectivity_bits; |
1198 | 1198 |
|
1199 | 1199 |
checkConcept<concepts::Digraph, Digraph>(); |
1200 | 1200 |
checkConcept<concepts::WriteMap<typename Digraph::Node, int>, NodeMap>(); |
1201 | 1201 |
|
1202 | 1202 |
typedef typename Digraph::Node Node; |
1203 | 1203 |
typedef typename Digraph::NodeIt NodeIt; |
1204 | 1204 |
typedef typename Digraph::Arc Arc; |
1205 | 1205 |
|
1206 | 1206 |
TopologicalSortVisitor<Digraph, NodeMap> |
1207 | 1207 |
visitor(order, countNodes(graph)); |
1208 | 1208 |
|
1209 | 1209 |
DfsVisit<Digraph, TopologicalSortVisitor<Digraph, NodeMap> > |
1210 | 1210 |
dfs(graph, visitor); |
1211 | 1211 |
|
1212 | 1212 |
dfs.init(); |
1213 | 1213 |
for (NodeIt it(graph); it != INVALID; ++it) { |
1214 | 1214 |
if (!dfs.reached(it)) { |
1215 | 1215 |
dfs.addSource(it); |
1216 | 1216 |
dfs.start(); |
1217 | 1217 |
} |
1218 | 1218 |
} |
1219 | 1219 |
} |
1220 | 1220 |
|
1221 | 1221 |
/// \ingroup connectivity |
1222 | 1222 |
/// |
1223 | 1223 |
/// \brief Sort the nodes of a DAG into topolgical order. |
1224 | 1224 |
/// |
1225 | 1225 |
/// Sort the nodes of a DAG into topolgical order. It also checks |
1226 | 1226 |
/// that the given graph is DAG. |
1227 | 1227 |
/// |
1228 | 1228 |
/// \param digraph The graph. It must be directed and acyclic. |
1229 | 1229 |
/// \retval order A readable - writable node map. The values will be set |
1230 | 1230 |
/// from 0 to the number of the nodes in the graph minus one. Each values |
1231 | 1231 |
/// of the map will be set exactly once, the values will be set descending |
1232 | 1232 |
/// order. |
1233 |
/// \return |
|
1233 |
/// \return \c false when the graph is not DAG. |
|
1234 | 1234 |
/// |
1235 | 1235 |
/// \see topologicalSort |
1236 | 1236 |
/// \see dag |
1237 | 1237 |
template <typename Digraph, typename NodeMap> |
1238 | 1238 |
bool checkedTopologicalSort(const Digraph& digraph, NodeMap& order) { |
1239 | 1239 |
using namespace _connectivity_bits; |
1240 | 1240 |
|
1241 | 1241 |
checkConcept<concepts::Digraph, Digraph>(); |
1242 | 1242 |
checkConcept<concepts::ReadWriteMap<typename Digraph::Node, int>, |
1243 | 1243 |
NodeMap>(); |
1244 | 1244 |
|
1245 | 1245 |
typedef typename Digraph::Node Node; |
1246 | 1246 |
typedef typename Digraph::NodeIt NodeIt; |
1247 | 1247 |
typedef typename Digraph::Arc Arc; |
1248 | 1248 |
|
1249 | 1249 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
1250 | 1250 |
order.set(it, -1); |
1251 | 1251 |
} |
1252 | 1252 |
|
1253 | 1253 |
TopologicalSortVisitor<Digraph, NodeMap> |
1254 | 1254 |
visitor(order, countNodes(digraph)); |
1255 | 1255 |
|
1256 | 1256 |
DfsVisit<Digraph, TopologicalSortVisitor<Digraph, NodeMap> > |
1257 | 1257 |
dfs(digraph, visitor); |
1258 | 1258 |
|
1259 | 1259 |
dfs.init(); |
1260 | 1260 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
1261 | 1261 |
if (!dfs.reached(it)) { |
1262 | 1262 |
dfs.addSource(it); |
1263 | 1263 |
while (!dfs.emptyQueue()) { |
1264 | 1264 |
Arc arc = dfs.nextArc(); |
1265 | 1265 |
Node target = digraph.target(arc); |
1266 | 1266 |
if (dfs.reached(target) && order[target] == -1) { |
1267 | 1267 |
return false; |
1268 | 1268 |
} |
1269 | 1269 |
dfs.processNextArc(); |
1270 | 1270 |
} |
1271 | 1271 |
} |
1272 | 1272 |
} |
1273 | 1273 |
return true; |
1274 | 1274 |
} |
1275 | 1275 |
|
1276 | 1276 |
/// \ingroup connectivity |
1277 | 1277 |
/// |
1278 | 1278 |
/// \brief Check that the given directed graph is a DAG. |
1279 | 1279 |
/// |
1280 | 1280 |
/// Check that the given directed graph is a DAG. The DAG is |
1281 | 1281 |
/// an Directed Acyclic Digraph. |
1282 |
/// \return |
|
1282 |
/// \return \c false when the graph is not DAG. |
|
1283 | 1283 |
/// \see acyclic |
1284 | 1284 |
template <typename Digraph> |
1285 | 1285 |
bool dag(const Digraph& digraph) { |
1286 | 1286 |
|
1287 | 1287 |
checkConcept<concepts::Digraph, Digraph>(); |
1288 | 1288 |
|
1289 | 1289 |
typedef typename Digraph::Node Node; |
1290 | 1290 |
typedef typename Digraph::NodeIt NodeIt; |
1291 | 1291 |
typedef typename Digraph::Arc Arc; |
1292 | 1292 |
|
1293 | 1293 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
1294 | 1294 |
|
1295 | 1295 |
typename Dfs<Digraph>::template SetProcessedMap<ProcessedMap>:: |
1296 | 1296 |
Create dfs(digraph); |
1297 | 1297 |
|
1298 | 1298 |
ProcessedMap processed(digraph); |
1299 | 1299 |
dfs.processedMap(processed); |
1300 | 1300 |
|
1301 | 1301 |
dfs.init(); |
1302 | 1302 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
1303 | 1303 |
if (!dfs.reached(it)) { |
1304 | 1304 |
dfs.addSource(it); |
1305 | 1305 |
while (!dfs.emptyQueue()) { |
1306 | 1306 |
Arc edge = dfs.nextArc(); |
1307 | 1307 |
Node target = digraph.target(edge); |
1308 | 1308 |
if (dfs.reached(target) && !processed[target]) { |
1309 | 1309 |
return false; |
1310 | 1310 |
} |
1311 | 1311 |
dfs.processNextArc(); |
1312 | 1312 |
} |
1313 | 1313 |
} |
1314 | 1314 |
} |
1315 | 1315 |
return true; |
1316 | 1316 |
} |
1317 | 1317 |
|
1318 | 1318 |
/// \ingroup connectivity |
1319 | 1319 |
/// |
1320 | 1320 |
/// \brief Check that the given undirected graph is acyclic. |
1321 | 1321 |
/// |
1322 | 1322 |
/// Check that the given undirected graph acyclic. |
1323 | 1323 |
/// \param graph The undirected graph. |
1324 |
/// \return |
|
1324 |
/// \return \c true when there is no circle in the graph. |
|
1325 | 1325 |
/// \see dag |
1326 | 1326 |
template <typename Graph> |
1327 | 1327 |
bool acyclic(const Graph& graph) { |
1328 | 1328 |
checkConcept<concepts::Graph, Graph>(); |
1329 | 1329 |
typedef typename Graph::Node Node; |
1330 | 1330 |
typedef typename Graph::NodeIt NodeIt; |
1331 | 1331 |
typedef typename Graph::Arc Arc; |
1332 | 1332 |
Dfs<Graph> dfs(graph); |
1333 | 1333 |
dfs.init(); |
1334 | 1334 |
for (NodeIt it(graph); it != INVALID; ++it) { |
1335 | 1335 |
if (!dfs.reached(it)) { |
1336 | 1336 |
dfs.addSource(it); |
1337 | 1337 |
while (!dfs.emptyQueue()) { |
1338 | 1338 |
Arc edge = dfs.nextArc(); |
1339 | 1339 |
Node source = graph.source(edge); |
1340 | 1340 |
Node target = graph.target(edge); |
1341 | 1341 |
if (dfs.reached(target) && |
1342 | 1342 |
dfs.predArc(source) != graph.oppositeArc(edge)) { |
1343 | 1343 |
return false; |
1344 | 1344 |
} |
1345 | 1345 |
dfs.processNextArc(); |
1346 | 1346 |
} |
1347 | 1347 |
} |
1348 | 1348 |
} |
1349 | 1349 |
return true; |
1350 | 1350 |
} |
1351 | 1351 |
|
1352 | 1352 |
/// \ingroup connectivity |
1353 | 1353 |
/// |
1354 | 1354 |
/// \brief Check that the given undirected graph is tree. |
1355 | 1355 |
/// |
1356 | 1356 |
/// Check that the given undirected graph is tree. |
1357 | 1357 |
/// \param graph The undirected graph. |
1358 |
/// \return |
|
1358 |
/// \return \c true when the graph is acyclic and connected. |
|
1359 | 1359 |
template <typename Graph> |
1360 | 1360 |
bool tree(const Graph& graph) { |
1361 | 1361 |
checkConcept<concepts::Graph, Graph>(); |
1362 | 1362 |
typedef typename Graph::Node Node; |
1363 | 1363 |
typedef typename Graph::NodeIt NodeIt; |
1364 | 1364 |
typedef typename Graph::Arc Arc; |
1365 | 1365 |
Dfs<Graph> dfs(graph); |
1366 | 1366 |
dfs.init(); |
1367 | 1367 |
dfs.addSource(NodeIt(graph)); |
1368 | 1368 |
while (!dfs.emptyQueue()) { |
1369 | 1369 |
Arc edge = dfs.nextArc(); |
1370 | 1370 |
Node source = graph.source(edge); |
1371 | 1371 |
Node target = graph.target(edge); |
1372 | 1372 |
if (dfs.reached(target) && |
1373 | 1373 |
dfs.predArc(source) != graph.oppositeArc(edge)) { |
1374 | 1374 |
return false; |
1375 | 1375 |
} |
1376 | 1376 |
dfs.processNextArc(); |
1377 | 1377 |
} |
1378 | 1378 |
for (NodeIt it(graph); it != INVALID; ++it) { |
1379 | 1379 |
if (!dfs.reached(it)) { |
1380 | 1380 |
return false; |
1381 | 1381 |
} |
1382 | 1382 |
} |
1383 | 1383 |
return true; |
1384 | 1384 |
} |
1385 | 1385 |
|
1386 | 1386 |
namespace _connectivity_bits { |
1387 | 1387 |
|
1388 | 1388 |
template <typename Digraph> |
1389 | 1389 |
class BipartiteVisitor : public BfsVisitor<Digraph> { |
1390 | 1390 |
public: |
1391 | 1391 |
typedef typename Digraph::Arc Arc; |
1392 | 1392 |
typedef typename Digraph::Node Node; |
1393 | 1393 |
|
1394 | 1394 |
BipartiteVisitor(const Digraph& graph, bool& bipartite) |
1395 | 1395 |
: _graph(graph), _part(graph), _bipartite(bipartite) {} |
1396 | 1396 |
|
1397 | 1397 |
void start(const Node& node) { |
1398 | 1398 |
_part[node] = true; |
1399 | 1399 |
} |
1400 | 1400 |
void discover(const Arc& edge) { |
1401 | 1401 |
_part.set(_graph.target(edge), !_part[_graph.source(edge)]); |
1402 | 1402 |
} |
1403 | 1403 |
void examine(const Arc& edge) { |
1404 | 1404 |
_bipartite = _bipartite && |
1405 | 1405 |
_part[_graph.target(edge)] != _part[_graph.source(edge)]; |
1406 | 1406 |
} |
1407 | 1407 |
|
1408 | 1408 |
private: |
1409 | 1409 |
|
1410 | 1410 |
const Digraph& _graph; |
1411 | 1411 |
typename Digraph::template NodeMap<bool> _part; |
1412 | 1412 |
bool& _bipartite; |
1413 | 1413 |
}; |
1414 | 1414 |
|
1415 | 1415 |
template <typename Digraph, typename PartMap> |
1416 | 1416 |
class BipartitePartitionsVisitor : public BfsVisitor<Digraph> { |
1417 | 1417 |
public: |
1418 | 1418 |
typedef typename Digraph::Arc Arc; |
1419 | 1419 |
typedef typename Digraph::Node Node; |
1420 | 1420 |
|
1421 | 1421 |
BipartitePartitionsVisitor(const Digraph& graph, |
1422 | 1422 |
PartMap& part, bool& bipartite) |
1423 | 1423 |
: _graph(graph), _part(part), _bipartite(bipartite) {} |
1424 | 1424 |
|
1425 | 1425 |
void start(const Node& node) { |
1426 | 1426 |
_part.set(node, true); |
1427 | 1427 |
} |
1428 | 1428 |
void discover(const Arc& edge) { |
1429 | 1429 |
_part.set(_graph.target(edge), !_part[_graph.source(edge)]); |
1430 | 1430 |
} |
1431 | 1431 |
void examine(const Arc& edge) { |
1432 | 1432 |
_bipartite = _bipartite && |
1433 | 1433 |
_part[_graph.target(edge)] != _part[_graph.source(edge)]; |
1434 | 1434 |
} |
1435 | 1435 |
|
1436 | 1436 |
private: |
1437 | 1437 |
|
1438 | 1438 |
const Digraph& _graph; |
1439 | 1439 |
PartMap& _part; |
1440 | 1440 |
bool& _bipartite; |
1441 | 1441 |
}; |
1442 | 1442 |
} |
1443 | 1443 |
|
1444 | 1444 |
/// \ingroup connectivity |
1445 | 1445 |
/// |
1446 | 1446 |
/// \brief Check if the given undirected graph is bipartite or not |
1447 | 1447 |
/// |
1448 | 1448 |
/// The function checks if the given undirected \c graph graph is bipartite |
1449 | 1449 |
/// or not. The \ref Bfs algorithm is used to calculate the result. |
1450 | 1450 |
/// \param graph The undirected graph. |
1451 |
/// \return |
|
1451 |
/// \return \c true if \c graph is bipartite, \c false otherwise. |
|
1452 | 1452 |
/// \sa bipartitePartitions |
1453 | 1453 |
template<typename Graph> |
1454 | 1454 |
inline bool bipartite(const Graph &graph){ |
1455 | 1455 |
using namespace _connectivity_bits; |
1456 | 1456 |
|
1457 | 1457 |
checkConcept<concepts::Graph, Graph>(); |
1458 | 1458 |
|
1459 | 1459 |
typedef typename Graph::NodeIt NodeIt; |
1460 | 1460 |
typedef typename Graph::ArcIt ArcIt; |
1461 | 1461 |
|
1462 | 1462 |
bool bipartite = true; |
1463 | 1463 |
|
1464 | 1464 |
BipartiteVisitor<Graph> |
1465 | 1465 |
visitor(graph, bipartite); |
1466 | 1466 |
BfsVisit<Graph, BipartiteVisitor<Graph> > |
1467 | 1467 |
bfs(graph, visitor); |
1468 | 1468 |
bfs.init(); |
1469 | 1469 |
for(NodeIt it(graph); it != INVALID; ++it) { |
1470 | 1470 |
if(!bfs.reached(it)){ |
1471 | 1471 |
bfs.addSource(it); |
1472 | 1472 |
while (!bfs.emptyQueue()) { |
1473 | 1473 |
bfs.processNextNode(); |
1474 | 1474 |
if (!bipartite) return false; |
1475 | 1475 |
} |
1476 | 1476 |
} |
1477 | 1477 |
} |
1478 | 1478 |
return true; |
1479 | 1479 |
} |
1480 | 1480 |
|
1481 | 1481 |
/// \ingroup connectivity |
1482 | 1482 |
/// |
1483 | 1483 |
/// \brief Check if the given undirected graph is bipartite or not |
1484 | 1484 |
/// |
1485 | 1485 |
/// The function checks if the given undirected graph is bipartite |
1486 | 1486 |
/// or not. The \ref Bfs algorithm is used to calculate the result. |
1487 | 1487 |
/// During the execution, the \c partMap will be set as the two |
1488 | 1488 |
/// partitions of the graph. |
1489 | 1489 |
/// \param graph The undirected graph. |
1490 | 1490 |
/// \retval partMap A writable bool map of nodes. It will be set as the |
1491 | 1491 |
/// two partitions of the graph. |
1492 |
/// \return |
|
1492 |
/// \return \c true if \c graph is bipartite, \c false otherwise. |
|
1493 | 1493 |
template<typename Graph, typename NodeMap> |
1494 | 1494 |
inline bool bipartitePartitions(const Graph &graph, NodeMap &partMap){ |
1495 | 1495 |
using namespace _connectivity_bits; |
1496 | 1496 |
|
1497 | 1497 |
checkConcept<concepts::Graph, Graph>(); |
1498 | 1498 |
|
1499 | 1499 |
typedef typename Graph::Node Node; |
1500 | 1500 |
typedef typename Graph::NodeIt NodeIt; |
1501 | 1501 |
typedef typename Graph::ArcIt ArcIt; |
1502 | 1502 |
|
1503 | 1503 |
bool bipartite = true; |
1504 | 1504 |
|
1505 | 1505 |
BipartitePartitionsVisitor<Graph, NodeMap> |
1506 | 1506 |
visitor(graph, partMap, bipartite); |
1507 | 1507 |
BfsVisit<Graph, BipartitePartitionsVisitor<Graph, NodeMap> > |
1508 | 1508 |
bfs(graph, visitor); |
1509 | 1509 |
bfs.init(); |
1510 | 1510 |
for(NodeIt it(graph); it != INVALID; ++it) { |
1511 | 1511 |
if(!bfs.reached(it)){ |
1512 | 1512 |
bfs.addSource(it); |
1513 | 1513 |
while (!bfs.emptyQueue()) { |
1514 | 1514 |
bfs.processNextNode(); |
1515 | 1515 |
if (!bipartite) return false; |
1516 | 1516 |
} |
1517 | 1517 |
} |
1518 | 1518 |
} |
1519 | 1519 |
return true; |
1520 | 1520 |
} |
1521 | 1521 |
|
1522 | 1522 |
/// \brief Returns true when there are not loop edges in the graph. |
1523 | 1523 |
/// |
1524 | 1524 |
/// Returns true when there are not loop edges in the graph. |
1525 | 1525 |
template <typename Digraph> |
1526 | 1526 |
bool loopFree(const Digraph& digraph) { |
1527 | 1527 |
for (typename Digraph::ArcIt it(digraph); it != INVALID; ++it) { |
1528 | 1528 |
if (digraph.source(it) == digraph.target(it)) return false; |
1529 | 1529 |
} |
1530 | 1530 |
return true; |
1531 | 1531 |
} |
1532 | 1532 |
|
1533 | 1533 |
/// \brief Returns true when there are not parallel edges in the graph. |
1534 | 1534 |
/// |
1535 | 1535 |
/// Returns true when there are not parallel edges in the graph. |
1536 | 1536 |
template <typename Digraph> |
1537 | 1537 |
bool parallelFree(const Digraph& digraph) { |
1538 | 1538 |
typename Digraph::template NodeMap<bool> reached(digraph, false); |
1539 | 1539 |
for (typename Digraph::NodeIt n(digraph); n != INVALID; ++n) { |
1540 | 1540 |
for (typename Digraph::OutArcIt a(digraph, n); a != INVALID; ++a) { |
... | ... |
@@ -989,101 +989,101 @@ |
989 | 989 |
static Arc find(const Graph &g, Node u, Node v, Arc prev) { |
990 | 990 |
return g.findArc(u, v, prev); |
991 | 991 |
} |
992 | 992 |
}; |
993 | 993 |
} |
994 | 994 |
|
995 | 995 |
/// \brief Find an arc between two nodes of a digraph. |
996 | 996 |
/// |
997 | 997 |
/// This function finds an arc from node \c u to node \c v in the |
998 | 998 |
/// digraph \c g. |
999 | 999 |
/// |
1000 | 1000 |
/// If \c prev is \ref INVALID (this is the default value), then |
1001 | 1001 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
1002 | 1002 |
/// the next arc from \c u to \c v after \c prev. |
1003 | 1003 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
1004 | 1004 |
/// |
1005 | 1005 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
1006 | 1006 |
///\code |
1007 | 1007 |
/// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) { |
1008 | 1008 |
/// ... |
1009 | 1009 |
/// } |
1010 | 1010 |
///\endcode |
1011 | 1011 |
/// |
1012 | 1012 |
/// \note \ref ConArcIt provides iterator interface for the same |
1013 | 1013 |
/// functionality. |
1014 | 1014 |
/// |
1015 | 1015 |
///\sa ConArcIt |
1016 | 1016 |
///\sa ArcLookUp, AllArcLookUp, DynArcLookUp |
1017 | 1017 |
template <typename Graph> |
1018 | 1018 |
inline typename Graph::Arc |
1019 | 1019 |
findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
1020 | 1020 |
typename Graph::Arc prev = INVALID) { |
1021 | 1021 |
return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev); |
1022 | 1022 |
} |
1023 | 1023 |
|
1024 | 1024 |
/// \brief Iterator for iterating on parallel arcs connecting the same nodes. |
1025 | 1025 |
/// |
1026 | 1026 |
/// Iterator for iterating on parallel arcs connecting the same nodes. It is |
1027 | 1027 |
/// a higher level interface for the \ref findArc() function. You can |
1028 | 1028 |
/// use it the following way: |
1029 | 1029 |
///\code |
1030 | 1030 |
/// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) { |
1031 | 1031 |
/// ... |
1032 | 1032 |
/// } |
1033 | 1033 |
///\endcode |
1034 | 1034 |
/// |
1035 | 1035 |
///\sa findArc() |
1036 | 1036 |
///\sa ArcLookUp, AllArcLookUp, DynArcLookUp |
1037 |
template <typename _Graph> |
|
1038 |
class ConArcIt : public _Graph::Arc { |
|
1037 |
template <typename GR> |
|
1038 |
class ConArcIt : public GR::Arc { |
|
1039 | 1039 |
public: |
1040 | 1040 |
|
1041 |
typedef |
|
1041 |
typedef GR Graph; |
|
1042 | 1042 |
typedef typename Graph::Arc Parent; |
1043 | 1043 |
|
1044 | 1044 |
typedef typename Graph::Arc Arc; |
1045 | 1045 |
typedef typename Graph::Node Node; |
1046 | 1046 |
|
1047 | 1047 |
/// \brief Constructor. |
1048 | 1048 |
/// |
1049 | 1049 |
/// Construct a new ConArcIt iterating on the arcs that |
1050 | 1050 |
/// connects nodes \c u and \c v. |
1051 | 1051 |
ConArcIt(const Graph& g, Node u, Node v) : _graph(g) { |
1052 | 1052 |
Parent::operator=(findArc(_graph, u, v)); |
1053 | 1053 |
} |
1054 | 1054 |
|
1055 | 1055 |
/// \brief Constructor. |
1056 | 1056 |
/// |
1057 | 1057 |
/// Construct a new ConArcIt that continues the iterating from arc \c a. |
1058 | 1058 |
ConArcIt(const Graph& g, Arc a) : Parent(a), _graph(g) {} |
1059 | 1059 |
|
1060 | 1060 |
/// \brief Increment operator. |
1061 | 1061 |
/// |
1062 | 1062 |
/// It increments the iterator and gives back the next arc. |
1063 | 1063 |
ConArcIt& operator++() { |
1064 | 1064 |
Parent::operator=(findArc(_graph, _graph.source(*this), |
1065 | 1065 |
_graph.target(*this), *this)); |
1066 | 1066 |
return *this; |
1067 | 1067 |
} |
1068 | 1068 |
private: |
1069 | 1069 |
const Graph& _graph; |
1070 | 1070 |
}; |
1071 | 1071 |
|
1072 | 1072 |
namespace _core_bits { |
1073 | 1073 |
|
1074 | 1074 |
template <typename Graph, typename Enable = void> |
1075 | 1075 |
struct FindEdgeSelector { |
1076 | 1076 |
typedef typename Graph::Node Node; |
1077 | 1077 |
typedef typename Graph::Edge Edge; |
1078 | 1078 |
static Edge find(const Graph &g, Node u, Node v, Edge e) { |
1079 | 1079 |
bool b; |
1080 | 1080 |
if (u != v) { |
1081 | 1081 |
if (e == INVALID) { |
1082 | 1082 |
g.firstInc(e, b, u); |
1083 | 1083 |
} else { |
1084 | 1084 |
b = g.u(e) == u; |
1085 | 1085 |
g.nextInc(e, b); |
1086 | 1086 |
} |
1087 | 1087 |
while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) { |
1088 | 1088 |
g.nextInc(e, b); |
1089 | 1089 |
} |
... | ... |
@@ -1112,173 +1112,173 @@ |
1112 | 1112 |
static Edge find(const Graph &g, Node u, Node v, Edge prev) { |
1113 | 1113 |
return g.findEdge(u, v, prev); |
1114 | 1114 |
} |
1115 | 1115 |
}; |
1116 | 1116 |
} |
1117 | 1117 |
|
1118 | 1118 |
/// \brief Find an edge between two nodes of a graph. |
1119 | 1119 |
/// |
1120 | 1120 |
/// This function finds an edge from node \c u to node \c v in graph \c g. |
1121 | 1121 |
/// If node \c u and node \c v is equal then each loop edge |
1122 | 1122 |
/// will be enumerated once. |
1123 | 1123 |
/// |
1124 | 1124 |
/// If \c prev is \ref INVALID (this is the default value), then |
1125 | 1125 |
/// it finds the first edge from \c u to \c v. Otherwise it looks for |
1126 | 1126 |
/// the next edge from \c u to \c v after \c prev. |
1127 | 1127 |
/// \return The found edge or \ref INVALID if there is no such an edge. |
1128 | 1128 |
/// |
1129 | 1129 |
/// Thus you can iterate through each edge between \c u and \c v |
1130 | 1130 |
/// as it follows. |
1131 | 1131 |
///\code |
1132 | 1132 |
/// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) { |
1133 | 1133 |
/// ... |
1134 | 1134 |
/// } |
1135 | 1135 |
///\endcode |
1136 | 1136 |
/// |
1137 | 1137 |
/// \note \ref ConEdgeIt provides iterator interface for the same |
1138 | 1138 |
/// functionality. |
1139 | 1139 |
/// |
1140 | 1140 |
///\sa ConEdgeIt |
1141 | 1141 |
template <typename Graph> |
1142 | 1142 |
inline typename Graph::Edge |
1143 | 1143 |
findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
1144 | 1144 |
typename Graph::Edge p = INVALID) { |
1145 | 1145 |
return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p); |
1146 | 1146 |
} |
1147 | 1147 |
|
1148 | 1148 |
/// \brief Iterator for iterating on parallel edges connecting the same nodes. |
1149 | 1149 |
/// |
1150 | 1150 |
/// Iterator for iterating on parallel edges connecting the same nodes. |
1151 | 1151 |
/// It is a higher level interface for the findEdge() function. You can |
1152 | 1152 |
/// use it the following way: |
1153 | 1153 |
///\code |
1154 | 1154 |
/// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) { |
1155 | 1155 |
/// ... |
1156 | 1156 |
/// } |
1157 | 1157 |
///\endcode |
1158 | 1158 |
/// |
1159 | 1159 |
///\sa findEdge() |
1160 |
template <typename _Graph> |
|
1161 |
class ConEdgeIt : public _Graph::Edge { |
|
1160 |
template <typename GR> |
|
1161 |
class ConEdgeIt : public GR::Edge { |
|
1162 | 1162 |
public: |
1163 | 1163 |
|
1164 |
typedef |
|
1164 |
typedef GR Graph; |
|
1165 | 1165 |
typedef typename Graph::Edge Parent; |
1166 | 1166 |
|
1167 | 1167 |
typedef typename Graph::Edge Edge; |
1168 | 1168 |
typedef typename Graph::Node Node; |
1169 | 1169 |
|
1170 | 1170 |
/// \brief Constructor. |
1171 | 1171 |
/// |
1172 | 1172 |
/// Construct a new ConEdgeIt iterating on the edges that |
1173 | 1173 |
/// connects nodes \c u and \c v. |
1174 | 1174 |
ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g), _u(u), _v(v) { |
1175 | 1175 |
Parent::operator=(findEdge(_graph, _u, _v)); |
1176 | 1176 |
} |
1177 | 1177 |
|
1178 | 1178 |
/// \brief Constructor. |
1179 | 1179 |
/// |
1180 | 1180 |
/// Construct a new ConEdgeIt that continues iterating from edge \c e. |
1181 | 1181 |
ConEdgeIt(const Graph& g, Edge e) : Parent(e), _graph(g) {} |
1182 | 1182 |
|
1183 | 1183 |
/// \brief Increment operator. |
1184 | 1184 |
/// |
1185 | 1185 |
/// It increments the iterator and gives back the next edge. |
1186 | 1186 |
ConEdgeIt& operator++() { |
1187 | 1187 |
Parent::operator=(findEdge(_graph, _u, _v, *this)); |
1188 | 1188 |
return *this; |
1189 | 1189 |
} |
1190 | 1190 |
private: |
1191 | 1191 |
const Graph& _graph; |
1192 | 1192 |
Node _u, _v; |
1193 | 1193 |
}; |
1194 | 1194 |
|
1195 | 1195 |
|
1196 | 1196 |
///Dynamic arc look-up between given endpoints. |
1197 | 1197 |
|
1198 | 1198 |
///Using this class, you can find an arc in a digraph from a given |
1199 | 1199 |
///source to a given target in amortized time <em>O</em>(log<em>d</em>), |
1200 | 1200 |
///where <em>d</em> is the out-degree of the source node. |
1201 | 1201 |
/// |
1202 | 1202 |
///It is possible to find \e all parallel arcs between two nodes with |
1203 | 1203 |
///the \c operator() member. |
1204 | 1204 |
/// |
1205 | 1205 |
///This is a dynamic data structure. Consider to use \ref ArcLookUp or |
1206 | 1206 |
///\ref AllArcLookUp if your digraph is not changed so frequently. |
1207 | 1207 |
/// |
1208 | 1208 |
///This class uses a self-adjusting binary search tree, the Splay tree |
1209 | 1209 |
///of Sleator and Tarjan to guarantee the logarithmic amortized |
1210 | 1210 |
///time bound for arc look-ups. This class also guarantees the |
1211 | 1211 |
///optimal time bound in a constant factor for any distribution of |
1212 | 1212 |
///queries. |
1213 | 1213 |
/// |
1214 |
///\tparam |
|
1214 |
///\tparam GR The type of the underlying digraph. |
|
1215 | 1215 |
/// |
1216 | 1216 |
///\sa ArcLookUp |
1217 | 1217 |
///\sa AllArcLookUp |
1218 |
template< |
|
1218 |
template <typename GR> |
|
1219 | 1219 |
class DynArcLookUp |
1220 |
: protected ItemSetTraits< |
|
1220 |
: protected ItemSetTraits<GR, typename GR::Arc>::ItemNotifier::ObserverBase |
|
1221 | 1221 |
{ |
1222 | 1222 |
public: |
1223 |
typedef typename ItemSetTraits< |
|
1223 |
typedef typename ItemSetTraits<GR, typename GR::Arc> |
|
1224 | 1224 |
::ItemNotifier::ObserverBase Parent; |
1225 | 1225 |
|
1226 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
|
1227 |
typedef G Digraph; |
|
1226 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
|
1227 |
typedef GR Digraph; |
|
1228 | 1228 |
|
1229 | 1229 |
protected: |
1230 | 1230 |
|
1231 |
class AutoNodeMap : public ItemSetTraits< |
|
1231 |
class AutoNodeMap : public ItemSetTraits<GR, Node>::template Map<Arc>::Type { |
|
1232 | 1232 |
public: |
1233 | 1233 |
|
1234 |
typedef typename ItemSetTraits< |
|
1234 |
typedef typename ItemSetTraits<GR, Node>::template Map<Arc>::Type Parent; |
|
1235 | 1235 |
|
1236 |
AutoNodeMap(const |
|
1236 |
AutoNodeMap(const GR& digraph) : Parent(digraph, INVALID) {} |
|
1237 | 1237 |
|
1238 | 1238 |
virtual void add(const Node& node) { |
1239 | 1239 |
Parent::add(node); |
1240 | 1240 |
Parent::set(node, INVALID); |
1241 | 1241 |
} |
1242 | 1242 |
|
1243 | 1243 |
virtual void add(const std::vector<Node>& nodes) { |
1244 | 1244 |
Parent::add(nodes); |
1245 | 1245 |
for (int i = 0; i < int(nodes.size()); ++i) { |
1246 | 1246 |
Parent::set(nodes[i], INVALID); |
1247 | 1247 |
} |
1248 | 1248 |
} |
1249 | 1249 |
|
1250 | 1250 |
virtual void build() { |
1251 | 1251 |
Parent::build(); |
1252 | 1252 |
Node it; |
1253 | 1253 |
typename Parent::Notifier* nf = Parent::notifier(); |
1254 | 1254 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
1255 | 1255 |
Parent::set(it, INVALID); |
1256 | 1256 |
} |
1257 | 1257 |
} |
1258 | 1258 |
}; |
1259 | 1259 |
|
1260 | 1260 |
const Digraph &_g; |
1261 | 1261 |
AutoNodeMap _head; |
1262 | 1262 |
typename Digraph::template ArcMap<Arc> _parent; |
1263 | 1263 |
typename Digraph::template ArcMap<Arc> _left; |
1264 | 1264 |
typename Digraph::template ArcMap<Arc> _right; |
1265 | 1265 |
|
1266 | 1266 |
class ArcLess { |
1267 | 1267 |
const Digraph &g; |
1268 | 1268 |
public: |
1269 | 1269 |
ArcLess(const Digraph &_g) : g(_g) {} |
1270 | 1270 |
bool operator()(Arc a,Arc b) const |
1271 | 1271 |
{ |
1272 | 1272 |
return g.target(a)<g.target(b); |
1273 | 1273 |
} |
1274 | 1274 |
}; |
1275 | 1275 |
|
1276 | 1276 |
public: |
1277 | 1277 |
|
1278 | 1278 |
///Constructor |
1279 | 1279 |
|
1280 | 1280 |
///Constructor. |
1281 | 1281 |
/// |
1282 | 1282 |
///It builds up the search database. |
1283 | 1283 |
DynArcLookUp(const Digraph &g) |
1284 | 1284 |
: _g(g),_head(g),_parent(g),_left(g),_right(g) |
... | ... |
@@ -1578,106 +1578,106 @@ |
1578 | 1578 |
if (_left[a] == INVALID) { |
1579 | 1579 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1580 | 1580 |
return r; |
1581 | 1581 |
} else { |
1582 | 1582 |
a = _left[a]; |
1583 | 1583 |
} |
1584 | 1584 |
} |
1585 | 1585 |
} |
1586 | 1586 |
} else { |
1587 | 1587 |
Arc a = p; |
1588 | 1588 |
if (_right[a] != INVALID) { |
1589 | 1589 |
a = _right[a]; |
1590 | 1590 |
while (_left[a] != INVALID) { |
1591 | 1591 |
a = _left[a]; |
1592 | 1592 |
} |
1593 | 1593 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1594 | 1594 |
} else { |
1595 | 1595 |
while (_parent[a] != INVALID && _right[_parent[a]] == a) { |
1596 | 1596 |
a = _parent[a]; |
1597 | 1597 |
} |
1598 | 1598 |
if (_parent[a] == INVALID) { |
1599 | 1599 |
return INVALID; |
1600 | 1600 |
} else { |
1601 | 1601 |
a = _parent[a]; |
1602 | 1602 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1603 | 1603 |
} |
1604 | 1604 |
} |
1605 | 1605 |
if (_g.target(a) == t) return a; |
1606 | 1606 |
else return INVALID; |
1607 | 1607 |
} |
1608 | 1608 |
} |
1609 | 1609 |
|
1610 | 1610 |
}; |
1611 | 1611 |
|
1612 | 1612 |
///Fast arc look-up between given endpoints. |
1613 | 1613 |
|
1614 | 1614 |
///Using this class, you can find an arc in a digraph from a given |
1615 | 1615 |
///source to a given target in time <em>O</em>(log<em>d</em>), |
1616 | 1616 |
///where <em>d</em> is the out-degree of the source node. |
1617 | 1617 |
/// |
1618 | 1618 |
///It is not possible to find \e all parallel arcs between two nodes. |
1619 | 1619 |
///Use \ref AllArcLookUp for this purpose. |
1620 | 1620 |
/// |
1621 | 1621 |
///\warning This class is static, so you should call refresh() (or at |
1622 | 1622 |
///least refresh(Node)) to refresh this data structure whenever the |
1623 | 1623 |
///digraph changes. This is a time consuming (superlinearly proportional |
1624 | 1624 |
///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs). |
1625 | 1625 |
/// |
1626 |
///\tparam |
|
1626 |
///\tparam GR The type of the underlying digraph. |
|
1627 | 1627 |
/// |
1628 | 1628 |
///\sa DynArcLookUp |
1629 | 1629 |
///\sa AllArcLookUp |
1630 |
template<class |
|
1630 |
template<class GR> |
|
1631 | 1631 |
class ArcLookUp |
1632 | 1632 |
{ |
1633 | 1633 |
public: |
1634 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
|
1635 |
typedef G Digraph; |
|
1634 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
|
1635 |
typedef GR Digraph; |
|
1636 | 1636 |
|
1637 | 1637 |
protected: |
1638 | 1638 |
const Digraph &_g; |
1639 | 1639 |
typename Digraph::template NodeMap<Arc> _head; |
1640 | 1640 |
typename Digraph::template ArcMap<Arc> _left; |
1641 | 1641 |
typename Digraph::template ArcMap<Arc> _right; |
1642 | 1642 |
|
1643 | 1643 |
class ArcLess { |
1644 | 1644 |
const Digraph &g; |
1645 | 1645 |
public: |
1646 | 1646 |
ArcLess(const Digraph &_g) : g(_g) {} |
1647 | 1647 |
bool operator()(Arc a,Arc b) const |
1648 | 1648 |
{ |
1649 | 1649 |
return g.target(a)<g.target(b); |
1650 | 1650 |
} |
1651 | 1651 |
}; |
1652 | 1652 |
|
1653 | 1653 |
public: |
1654 | 1654 |
|
1655 | 1655 |
///Constructor |
1656 | 1656 |
|
1657 | 1657 |
///Constructor. |
1658 | 1658 |
/// |
1659 | 1659 |
///It builds up the search database, which remains valid until the digraph |
1660 | 1660 |
///changes. |
1661 | 1661 |
ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();} |
1662 | 1662 |
|
1663 | 1663 |
private: |
1664 | 1664 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
1665 | 1665 |
{ |
1666 | 1666 |
int m=(a+b)/2; |
1667 | 1667 |
Arc me=v[m]; |
1668 | 1668 |
_left[me] = a<m?refreshRec(v,a,m-1):INVALID; |
1669 | 1669 |
_right[me] = m<b?refreshRec(v,m+1,b):INVALID; |
1670 | 1670 |
return me; |
1671 | 1671 |
} |
1672 | 1672 |
public: |
1673 | 1673 |
///Refresh the search data structure at a node. |
1674 | 1674 |
|
1675 | 1675 |
///Build up the search database of node \c n. |
1676 | 1676 |
/// |
1677 | 1677 |
///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> |
1678 | 1678 |
///is the number of the outgoing arcs of \c n. |
1679 | 1679 |
void refresh(Node n) |
1680 | 1680 |
{ |
1681 | 1681 |
std::vector<Arc> v; |
1682 | 1682 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e); |
1683 | 1683 |
if(v.size()) { |
... | ... |
@@ -1688,159 +1688,159 @@ |
1688 | 1688 |
} |
1689 | 1689 |
///Refresh the full data structure. |
1690 | 1690 |
|
1691 | 1691 |
///Build up the full search database. In fact, it simply calls |
1692 | 1692 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
1693 | 1693 |
/// |
1694 | 1694 |
///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is |
1695 | 1695 |
///the number of the arcs in the digraph and <em>D</em> is the maximum |
1696 | 1696 |
///out-degree of the digraph. |
1697 | 1697 |
void refresh() |
1698 | 1698 |
{ |
1699 | 1699 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(n); |
1700 | 1700 |
} |
1701 | 1701 |
|
1702 | 1702 |
///Find an arc between two nodes. |
1703 | 1703 |
|
1704 | 1704 |
///Find an arc between two nodes in time <em>O</em>(log<em>d</em>), |
1705 | 1705 |
///where <em>d</em> is the number of outgoing arcs of \c s. |
1706 | 1706 |
///\param s The source node. |
1707 | 1707 |
///\param t The target node. |
1708 | 1708 |
///\return An arc from \c s to \c t if there exists, |
1709 | 1709 |
///\ref INVALID otherwise. |
1710 | 1710 |
/// |
1711 | 1711 |
///\warning If you change the digraph, refresh() must be called before using |
1712 | 1712 |
///this operator. If you change the outgoing arcs of |
1713 | 1713 |
///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough. |
1714 | 1714 |
Arc operator()(Node s, Node t) const |
1715 | 1715 |
{ |
1716 | 1716 |
Arc e; |
1717 | 1717 |
for(e=_head[s]; |
1718 | 1718 |
e!=INVALID&&_g.target(e)!=t; |
1719 | 1719 |
e = t < _g.target(e)?_left[e]:_right[e]) ; |
1720 | 1720 |
return e; |
1721 | 1721 |
} |
1722 | 1722 |
|
1723 | 1723 |
}; |
1724 | 1724 |
|
1725 | 1725 |
///Fast look-up of all arcs between given endpoints. |
1726 | 1726 |
|
1727 | 1727 |
///This class is the same as \ref ArcLookUp, with the addition |
1728 | 1728 |
///that it makes it possible to find all parallel arcs between given |
1729 | 1729 |
///endpoints. |
1730 | 1730 |
/// |
1731 | 1731 |
///\warning This class is static, so you should call refresh() (or at |
1732 | 1732 |
///least refresh(Node)) to refresh this data structure whenever the |
1733 | 1733 |
///digraph changes. This is a time consuming (superlinearly proportional |
1734 | 1734 |
///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs). |
1735 | 1735 |
/// |
1736 |
///\tparam |
|
1736 |
///\tparam GR The type of the underlying digraph. |
|
1737 | 1737 |
/// |
1738 | 1738 |
///\sa DynArcLookUp |
1739 | 1739 |
///\sa ArcLookUp |
1740 |
template<class G> |
|
1741 |
class AllArcLookUp : public ArcLookUp<G> |
|
1740 |
template<class GR> |
|
1741 |
class AllArcLookUp : public ArcLookUp<GR> |
|
1742 | 1742 |
{ |
1743 |
using ArcLookUp<G>::_g; |
|
1744 |
using ArcLookUp<G>::_right; |
|
1745 |
using ArcLookUp<G>::_left; |
|
1746 |
using ArcLookUp<G>::_head; |
|
1743 |
using ArcLookUp<GR>::_g; |
|
1744 |
using ArcLookUp<GR>::_right; |
|
1745 |
using ArcLookUp<GR>::_left; |
|
1746 |
using ArcLookUp<GR>::_head; |
|
1747 | 1747 |
|
1748 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
|
1749 |
typedef G Digraph; |
|
1748 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
|
1749 |
typedef GR Digraph; |
|
1750 | 1750 |
|
1751 | 1751 |
typename Digraph::template ArcMap<Arc> _next; |
1752 | 1752 |
|
1753 | 1753 |
Arc refreshNext(Arc head,Arc next=INVALID) |
1754 | 1754 |
{ |
1755 | 1755 |
if(head==INVALID) return next; |
1756 | 1756 |
else { |
1757 | 1757 |
next=refreshNext(_right[head],next); |
1758 | 1758 |
_next[head]=( next!=INVALID && _g.target(next)==_g.target(head)) |
1759 | 1759 |
? next : INVALID; |
1760 | 1760 |
return refreshNext(_left[head],head); |
1761 | 1761 |
} |
1762 | 1762 |
} |
1763 | 1763 |
|
1764 | 1764 |
void refreshNext() |
1765 | 1765 |
{ |
1766 | 1766 |
for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]); |
1767 | 1767 |
} |
1768 | 1768 |
|
1769 | 1769 |
public: |
1770 | 1770 |
///Constructor |
1771 | 1771 |
|
1772 | 1772 |
///Constructor. |
1773 | 1773 |
/// |
1774 | 1774 |
///It builds up the search database, which remains valid until the digraph |
1775 | 1775 |
///changes. |
1776 |
AllArcLookUp(const Digraph &g) : ArcLookUp< |
|
1776 |
AllArcLookUp(const Digraph &g) : ArcLookUp<GR>(g), _next(g) {refreshNext();} |
|
1777 | 1777 |
|
1778 | 1778 |
///Refresh the data structure at a node. |
1779 | 1779 |
|
1780 | 1780 |
///Build up the search database of node \c n. |
1781 | 1781 |
/// |
1782 | 1782 |
///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is |
1783 | 1783 |
///the number of the outgoing arcs of \c n. |
1784 | 1784 |
void refresh(Node n) |
1785 | 1785 |
{ |
1786 |
ArcLookUp< |
|
1786 |
ArcLookUp<GR>::refresh(n); |
|
1787 | 1787 |
refreshNext(_head[n]); |
1788 | 1788 |
} |
1789 | 1789 |
|
1790 | 1790 |
///Refresh the full data structure. |
1791 | 1791 |
|
1792 | 1792 |
///Build up the full search database. In fact, it simply calls |
1793 | 1793 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
1794 | 1794 |
/// |
1795 | 1795 |
///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is |
1796 | 1796 |
///the number of the arcs in the digraph and <em>D</em> is the maximum |
1797 | 1797 |
///out-degree of the digraph. |
1798 | 1798 |
void refresh() |
1799 | 1799 |
{ |
1800 | 1800 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]); |
1801 | 1801 |
} |
1802 | 1802 |
|
1803 | 1803 |
///Find an arc between two nodes. |
1804 | 1804 |
|
1805 | 1805 |
///Find an arc between two nodes. |
1806 | 1806 |
///\param s The source node. |
1807 | 1807 |
///\param t The target node. |
1808 | 1808 |
///\param prev The previous arc between \c s and \c t. It it is INVALID or |
1809 | 1809 |
///not given, the operator finds the first appropriate arc. |
1810 | 1810 |
///\return An arc from \c s to \c t after \c prev or |
1811 | 1811 |
///\ref INVALID if there is no more. |
1812 | 1812 |
/// |
1813 | 1813 |
///For example, you can count the number of arcs from \c u to \c v in the |
1814 | 1814 |
///following way. |
1815 | 1815 |
///\code |
1816 | 1816 |
///AllArcLookUp<ListDigraph> ae(g); |
1817 | 1817 |
///... |
1818 | 1818 |
///int n = 0; |
1819 | 1819 |
///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++; |
1820 | 1820 |
///\endcode |
1821 | 1821 |
/// |
1822 | 1822 |
///Finding the first arc take <em>O</em>(log<em>d</em>) time, |
1823 | 1823 |
///where <em>d</em> is the number of outgoing arcs of \c s. Then the |
1824 | 1824 |
///consecutive arcs are found in constant time. |
1825 | 1825 |
/// |
1826 | 1826 |
///\warning If you change the digraph, refresh() must be called before using |
1827 | 1827 |
///this operator. If you change the outgoing arcs of |
1828 | 1828 |
///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough. |
1829 | 1829 |
/// |
1830 | 1830 |
#ifdef DOXYGEN |
1831 | 1831 |
Arc operator()(Node s, Node t, Arc prev=INVALID) const {} |
1832 | 1832 |
#else |
1833 |
using ArcLookUp< |
|
1833 |
using ArcLookUp<GR>::operator() ; |
|
1834 | 1834 |
Arc operator()(Node s, Node t, Arc prev) const |
1835 | 1835 |
{ |
1836 | 1836 |
return prev==INVALID?(*this)(s,t):_next[prev]; |
1837 | 1837 |
} |
1838 | 1838 |
#endif |
1839 | 1839 |
|
1840 | 1840 |
}; |
1841 | 1841 |
|
1842 | 1842 |
/// @} |
1843 | 1843 |
|
1844 | 1844 |
} //namespace lemon |
1845 | 1845 |
|
1846 | 1846 |
#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 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_DIJKSTRA_H |
20 | 20 |
#define LEMON_DIJKSTRA_H |
21 | 21 |
|
22 | 22 |
///\ingroup shortest_path |
23 | 23 |
///\file |
24 | 24 |
///\brief Dijkstra algorithm. |
25 | 25 |
|
26 | 26 |
#include <limits> |
27 | 27 |
#include <lemon/list_graph.h> |
28 | 28 |
#include <lemon/bin_heap.h> |
29 | 29 |
#include <lemon/bits/path_dump.h> |
30 | 30 |
#include <lemon/core.h> |
31 | 31 |
#include <lemon/error.h> |
32 | 32 |
#include <lemon/maps.h> |
33 | 33 |
#include <lemon/path.h> |
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
/// \brief Default operation traits for the Dijkstra algorithm class. |
38 | 38 |
/// |
39 | 39 |
/// This operation traits class defines all computational operations and |
40 | 40 |
/// constants which are used in the Dijkstra algorithm. |
41 |
template <typename |
|
41 |
template <typename V> |
|
42 | 42 |
struct DijkstraDefaultOperationTraits { |
43 |
/// \e |
|
44 |
typedef V Value; |
|
43 | 45 |
/// \brief Gives back the zero value of the type. |
44 | 46 |
static Value zero() { |
45 | 47 |
return static_cast<Value>(0); |
46 | 48 |
} |
47 | 49 |
/// \brief Gives back the sum of the given two elements. |
48 | 50 |
static Value plus(const Value& left, const Value& right) { |
49 | 51 |
return left + right; |
50 | 52 |
} |
51 | 53 |
/// \brief Gives back true only if the first value is less than the second. |
52 | 54 |
static bool less(const Value& left, const Value& right) { |
53 | 55 |
return left < right; |
54 | 56 |
} |
55 | 57 |
}; |
56 | 58 |
|
57 | 59 |
///Default traits class of Dijkstra class. |
58 | 60 |
|
59 | 61 |
///Default traits class of Dijkstra class. |
60 | 62 |
///\tparam GR The type of the digraph. |
61 |
///\tparam LM The type of the length map. |
|
62 |
template<class GR, class LM> |
|
63 |
///\tparam LEN The type of the length map. |
|
64 |
template<typename GR, typename LEN> |
|
63 | 65 |
struct DijkstraDefaultTraits |
64 | 66 |
{ |
65 | 67 |
///The type of the digraph the algorithm runs on. |
66 | 68 |
typedef GR Digraph; |
67 | 69 |
|
68 | 70 |
///The type of the map that stores the arc lengths. |
69 | 71 |
|
70 | 72 |
///The type of the map that stores the arc lengths. |
71 | 73 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
72 |
typedef |
|
74 |
typedef LEN LengthMap; |
|
73 | 75 |
///The type of the length of the arcs. |
74 |
typedef typename |
|
76 |
typedef typename LEN::Value Value; |
|
75 | 77 |
|
76 | 78 |
/// Operation traits for %Dijkstra algorithm. |
77 | 79 |
|
78 | 80 |
/// This class defines the operations that are used in the algorithm. |
79 | 81 |
/// \see DijkstraDefaultOperationTraits |
80 | 82 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
81 | 83 |
|
82 | 84 |
/// The cross reference type used by the heap. |
83 | 85 |
|
84 | 86 |
/// The cross reference type used by the heap. |
85 | 87 |
/// Usually it is \c Digraph::NodeMap<int>. |
86 | 88 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
87 | 89 |
///Instantiates a \c HeapCrossRef. |
88 | 90 |
|
89 | 91 |
///This function instantiates a \ref HeapCrossRef. |
90 | 92 |
/// \param g is the digraph, to which we would like to define the |
91 | 93 |
/// \ref HeapCrossRef. |
92 | 94 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
93 | 95 |
{ |
94 | 96 |
return new HeapCrossRef(g); |
95 | 97 |
} |
96 | 98 |
|
97 | 99 |
///The heap type used by the %Dijkstra algorithm. |
98 | 100 |
|
99 | 101 |
///The heap type used by the Dijkstra algorithm. |
100 | 102 |
/// |
101 | 103 |
///\sa BinHeap |
102 | 104 |
///\sa Dijkstra |
103 |
typedef BinHeap<typename |
|
105 |
typedef BinHeap<typename LEN::Value, HeapCrossRef, std::less<Value> > Heap; |
|
104 | 106 |
///Instantiates a \c Heap. |
105 | 107 |
|
106 | 108 |
///This function instantiates a \ref Heap. |
107 | 109 |
static Heap *createHeap(HeapCrossRef& r) |
108 | 110 |
{ |
109 | 111 |
return new Heap(r); |
110 | 112 |
} |
111 | 113 |
|
112 | 114 |
///\brief The type of the map that stores the predecessor |
113 | 115 |
///arcs of the shortest paths. |
114 | 116 |
/// |
115 | 117 |
///The type of the map that stores the predecessor |
116 | 118 |
///arcs of the shortest paths. |
117 | 119 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
118 | 120 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
119 | 121 |
///Instantiates a \c PredMap. |
120 | 122 |
|
121 | 123 |
///This function instantiates a \ref PredMap. |
122 | 124 |
///\param g is the digraph, to which we would like to define the |
123 | 125 |
///\ref PredMap. |
124 | 126 |
static PredMap *createPredMap(const Digraph &g) |
125 | 127 |
{ |
126 | 128 |
return new PredMap(g); |
127 | 129 |
} |
128 | 130 |
|
129 | 131 |
///The type of the map that indicates which nodes are processed. |
130 | 132 |
|
131 | 133 |
///The type of the map that indicates which nodes are processed. |
132 | 134 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
133 | 135 |
///By default it is a NullMap. |
134 | 136 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
135 | 137 |
///Instantiates a \c ProcessedMap. |
136 | 138 |
|
137 | 139 |
///This function instantiates a \ref ProcessedMap. |
138 | 140 |
///\param g is the digraph, to which |
139 | 141 |
///we would like to define the \ref ProcessedMap. |
140 | 142 |
#ifdef DOXYGEN |
141 | 143 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
142 | 144 |
#else |
143 | 145 |
static ProcessedMap *createProcessedMap(const Digraph &) |
144 | 146 |
#endif |
145 | 147 |
{ |
146 | 148 |
return new ProcessedMap(); |
147 | 149 |
} |
148 | 150 |
|
149 | 151 |
///The type of the map that stores the distances of the nodes. |
150 | 152 |
|
151 | 153 |
///The type of the map that stores the distances of the nodes. |
152 | 154 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
153 |
typedef typename Digraph::template NodeMap<typename |
|
155 |
typedef typename Digraph::template NodeMap<typename LEN::Value> DistMap; |
|
154 | 156 |
///Instantiates a \c DistMap. |
155 | 157 |
|
156 | 158 |
///This function instantiates a \ref DistMap. |
157 | 159 |
///\param g is the digraph, to which we would like to define |
158 | 160 |
///the \ref DistMap. |
159 | 161 |
static DistMap *createDistMap(const Digraph &g) |
160 | 162 |
{ |
161 | 163 |
return new DistMap(g); |
162 | 164 |
} |
163 | 165 |
}; |
164 | 166 |
|
165 | 167 |
///%Dijkstra algorithm class. |
166 | 168 |
|
167 | 169 |
/// \ingroup shortest_path |
168 | 170 |
///This class provides an efficient implementation of the %Dijkstra algorithm. |
169 | 171 |
/// |
170 | 172 |
///The arc lengths are passed to the algorithm using a |
171 | 173 |
///\ref concepts::ReadMap "ReadMap", |
172 | 174 |
///so it is easy to change it to any kind of length. |
173 | 175 |
///The type of the length is determined by the |
174 | 176 |
///\ref concepts::ReadMap::Value "Value" of the length map. |
175 | 177 |
///It is also possible to change the underlying priority heap. |
176 | 178 |
/// |
177 | 179 |
///There is also a \ref dijkstra() "function-type interface" for the |
178 | 180 |
///%Dijkstra algorithm, which is convenient in the simplier cases and |
179 | 181 |
///it can be used easier. |
180 | 182 |
/// |
181 | 183 |
///\tparam GR The type of the digraph the algorithm runs on. |
182 | 184 |
///The default type is \ref ListDigraph. |
183 |
///\tparam |
|
185 |
///\tparam LEN A \ref concepts::ReadMap "readable" arc map that specifies |
|
184 | 186 |
///the lengths of the arcs. |
185 | 187 |
///It is read once for each arc, so the map may involve in |
186 | 188 |
///relatively time consuming process to compute the arc lengths if |
187 | 189 |
///it is necessary. The default map type is \ref |
188 | 190 |
///concepts::Digraph::ArcMap "GR::ArcMap<int>". |
189 | 191 |
#ifdef DOXYGEN |
190 |
template <typename GR, typename |
|
192 |
template <typename GR, typename LEN, typename TR> |
|
191 | 193 |
#else |
192 | 194 |
template <typename GR=ListDigraph, |
193 |
typename LM=typename GR::template ArcMap<int>, |
|
194 |
typename TR=DijkstraDefaultTraits<GR,LM> > |
|
195 |
typename LEN=typename GR::template ArcMap<int>, |
|
196 |
typename TR=DijkstraDefaultTraits<GR,LEN> > |
|
195 | 197 |
#endif |
196 | 198 |
class Dijkstra { |
197 | 199 |
public: |
198 | 200 |
|
199 | 201 |
///The type of the digraph the algorithm runs on. |
200 | 202 |
typedef typename TR::Digraph Digraph; |
201 | 203 |
|
202 | 204 |
///The type of the length of the arcs. |
203 | 205 |
typedef typename TR::LengthMap::Value Value; |
204 | 206 |
///The type of the map that stores the arc lengths. |
205 | 207 |
typedef typename TR::LengthMap LengthMap; |
206 | 208 |
///\brief The type of the map that stores the predecessor arcs of the |
207 | 209 |
///shortest paths. |
208 | 210 |
typedef typename TR::PredMap PredMap; |
209 | 211 |
///The type of the map that stores the distances of the nodes. |
210 | 212 |
typedef typename TR::DistMap DistMap; |
211 | 213 |
///The type of the map that indicates which nodes are processed. |
212 | 214 |
typedef typename TR::ProcessedMap ProcessedMap; |
213 | 215 |
///The type of the paths. |
214 | 216 |
typedef PredMapPath<Digraph, PredMap> Path; |
215 | 217 |
///The cross reference type used for the current heap. |
216 | 218 |
typedef typename TR::HeapCrossRef HeapCrossRef; |
217 | 219 |
///The heap type used by the algorithm. |
218 | 220 |
typedef typename TR::Heap Heap; |
219 | 221 |
///\brief The \ref DijkstraDefaultOperationTraits "operation traits class" |
220 | 222 |
///of the algorithm. |
221 | 223 |
typedef typename TR::OperationTraits OperationTraits; |
222 | 224 |
|
223 | 225 |
///The \ref DijkstraDefaultTraits "traits class" of the algorithm. |
224 | 226 |
typedef TR Traits; |
225 | 227 |
|
226 | 228 |
private: |
227 | 229 |
|
228 | 230 |
typedef typename Digraph::Node Node; |
229 | 231 |
typedef typename Digraph::NodeIt NodeIt; |
230 | 232 |
typedef typename Digraph::Arc Arc; |
231 | 233 |
typedef typename Digraph::OutArcIt OutArcIt; |
232 | 234 |
|
233 | 235 |
//Pointer to the underlying digraph. |
234 | 236 |
const Digraph *G; |
235 | 237 |
//Pointer to the length map. |
236 | 238 |
const LengthMap *_length; |
237 | 239 |
//Pointer to the map of predecessors arcs. |
238 | 240 |
PredMap *_pred; |
239 | 241 |
//Indicates if _pred is locally allocated (true) or not. |
240 | 242 |
bool local_pred; |
241 | 243 |
//Pointer to the map of distances. |
242 | 244 |
DistMap *_dist; |
... | ... |
@@ -868,256 +870,256 @@ |
868 | 870 |
///predecessor arcs. |
869 | 871 |
/// |
870 | 872 |
///Returns a const reference to the node map that stores the predecessor |
871 | 873 |
///arcs, which form the shortest path tree. |
872 | 874 |
/// |
873 | 875 |
///\pre Either \ref run(Node) "run()" or \ref init() |
874 | 876 |
///must be called before using this function. |
875 | 877 |
const PredMap &predMap() const { return *_pred;} |
876 | 878 |
|
877 | 879 |
///Checks if a node is reached from the root(s). |
878 | 880 |
|
879 | 881 |
///Returns \c true if \c v is reached from the root(s). |
880 | 882 |
/// |
881 | 883 |
///\pre Either \ref run(Node) "run()" or \ref init() |
882 | 884 |
///must be called before using this function. |
883 | 885 |
bool reached(Node v) const { return (*_heap_cross_ref)[v] != |
884 | 886 |
Heap::PRE_HEAP; } |
885 | 887 |
|
886 | 888 |
///Checks if a node is processed. |
887 | 889 |
|
888 | 890 |
///Returns \c true if \c v is processed, i.e. the shortest |
889 | 891 |
///path to \c v has already found. |
890 | 892 |
/// |
891 | 893 |
///\pre Either \ref run(Node) "run()" or \ref init() |
892 | 894 |
///must be called before using this function. |
893 | 895 |
bool processed(Node v) const { return (*_heap_cross_ref)[v] == |
894 | 896 |
Heap::POST_HEAP; } |
895 | 897 |
|
896 | 898 |
///The current distance of a node from the root(s). |
897 | 899 |
|
898 | 900 |
///Returns the current distance of a node from the root(s). |
899 | 901 |
///It may be decreased in the following processes. |
900 | 902 |
/// |
901 | 903 |
///\pre Either \ref run(Node) "run()" or \ref init() |
902 | 904 |
///must be called before using this function and |
903 | 905 |
///node \c v must be reached but not necessarily processed. |
904 | 906 |
Value currentDist(Node v) const { |
905 | 907 |
return processed(v) ? (*_dist)[v] : (*_heap)[v]; |
906 | 908 |
} |
907 | 909 |
|
908 | 910 |
///@} |
909 | 911 |
}; |
910 | 912 |
|
911 | 913 |
|
912 | 914 |
///Default traits class of dijkstra() function. |
913 | 915 |
|
914 | 916 |
///Default traits class of dijkstra() function. |
915 | 917 |
///\tparam GR The type of the digraph. |
916 |
///\tparam LM The type of the length map. |
|
917 |
template<class GR, class LM> |
|
918 |
///\tparam LEN The type of the length map. |
|
919 |
template<class GR, class LEN> |
|
918 | 920 |
struct DijkstraWizardDefaultTraits |
919 | 921 |
{ |
920 | 922 |
///The type of the digraph the algorithm runs on. |
921 | 923 |
typedef GR Digraph; |
922 | 924 |
///The type of the map that stores the arc lengths. |
923 | 925 |
|
924 | 926 |
///The type of the map that stores the arc lengths. |
925 | 927 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
926 |
typedef |
|
928 |
typedef LEN LengthMap; |
|
927 | 929 |
///The type of the length of the arcs. |
928 |
typedef typename |
|
930 |
typedef typename LEN::Value Value; |
|
929 | 931 |
|
930 | 932 |
/// Operation traits for Dijkstra algorithm. |
931 | 933 |
|
932 | 934 |
/// This class defines the operations that are used in the algorithm. |
933 | 935 |
/// \see DijkstraDefaultOperationTraits |
934 | 936 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
935 | 937 |
|
936 | 938 |
/// The cross reference type used by the heap. |
937 | 939 |
|
938 | 940 |
/// The cross reference type used by the heap. |
939 | 941 |
/// Usually it is \c Digraph::NodeMap<int>. |
940 | 942 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
941 | 943 |
///Instantiates a \ref HeapCrossRef. |
942 | 944 |
|
943 | 945 |
///This function instantiates a \ref HeapCrossRef. |
944 | 946 |
/// \param g is the digraph, to which we would like to define the |
945 | 947 |
/// HeapCrossRef. |
946 | 948 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
947 | 949 |
{ |
948 | 950 |
return new HeapCrossRef(g); |
949 | 951 |
} |
950 | 952 |
|
951 | 953 |
///The heap type used by the Dijkstra algorithm. |
952 | 954 |
|
953 | 955 |
///The heap type used by the Dijkstra algorithm. |
954 | 956 |
/// |
955 | 957 |
///\sa BinHeap |
956 | 958 |
///\sa Dijkstra |
957 | 959 |
typedef BinHeap<Value, typename Digraph::template NodeMap<int>, |
958 | 960 |
std::less<Value> > Heap; |
959 | 961 |
|
960 | 962 |
///Instantiates a \ref Heap. |
961 | 963 |
|
962 | 964 |
///This function instantiates a \ref Heap. |
963 | 965 |
/// \param r is the HeapCrossRef which is used. |
964 | 966 |
static Heap *createHeap(HeapCrossRef& r) |
965 | 967 |
{ |
966 | 968 |
return new Heap(r); |
967 | 969 |
} |
968 | 970 |
|
969 | 971 |
///\brief The type of the map that stores the predecessor |
970 | 972 |
///arcs of the shortest paths. |
971 | 973 |
/// |
972 | 974 |
///The type of the map that stores the predecessor |
973 | 975 |
///arcs of the shortest paths. |
974 | 976 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
975 | 977 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
976 | 978 |
///Instantiates a PredMap. |
977 | 979 |
|
978 | 980 |
///This function instantiates a PredMap. |
979 | 981 |
///\param g is the digraph, to which we would like to define the |
980 | 982 |
///PredMap. |
981 | 983 |
static PredMap *createPredMap(const Digraph &g) |
982 | 984 |
{ |
983 | 985 |
return new PredMap(g); |
984 | 986 |
} |
985 | 987 |
|
986 | 988 |
///The type of the map that indicates which nodes are processed. |
987 | 989 |
|
988 | 990 |
///The type of the map that indicates which nodes are processed. |
989 | 991 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
990 | 992 |
///By default it is a NullMap. |
991 | 993 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
992 | 994 |
///Instantiates a ProcessedMap. |
993 | 995 |
|
994 | 996 |
///This function instantiates a ProcessedMap. |
995 | 997 |
///\param g is the digraph, to which |
996 | 998 |
///we would like to define the ProcessedMap. |
997 | 999 |
#ifdef DOXYGEN |
998 | 1000 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
999 | 1001 |
#else |
1000 | 1002 |
static ProcessedMap *createProcessedMap(const Digraph &) |
1001 | 1003 |
#endif |
1002 | 1004 |
{ |
1003 | 1005 |
return new ProcessedMap(); |
1004 | 1006 |
} |
1005 | 1007 |
|
1006 | 1008 |
///The type of the map that stores the distances of the nodes. |
1007 | 1009 |
|
1008 | 1010 |
///The type of the map that stores the distances of the nodes. |
1009 | 1011 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
1010 |
typedef typename Digraph::template NodeMap<typename |
|
1012 |
typedef typename Digraph::template NodeMap<typename LEN::Value> DistMap; |
|
1011 | 1013 |
///Instantiates a DistMap. |
1012 | 1014 |
|
1013 | 1015 |
///This function instantiates a DistMap. |
1014 | 1016 |
///\param g is the digraph, to which we would like to define |
1015 | 1017 |
///the DistMap |
1016 | 1018 |
static DistMap *createDistMap(const Digraph &g) |
1017 | 1019 |
{ |
1018 | 1020 |
return new DistMap(g); |
1019 | 1021 |
} |
1020 | 1022 |
|
1021 | 1023 |
///The type of the shortest paths. |
1022 | 1024 |
|
1023 | 1025 |
///The type of the shortest paths. |
1024 | 1026 |
///It must meet the \ref concepts::Path "Path" concept. |
1025 | 1027 |
typedef lemon::Path<Digraph> Path; |
1026 | 1028 |
}; |
1027 | 1029 |
|
1028 | 1030 |
/// Default traits class used by DijkstraWizard |
1029 | 1031 |
|
1030 | 1032 |
/// To make it easier to use Dijkstra algorithm |
1031 | 1033 |
/// we have created a wizard class. |
1032 | 1034 |
/// This \ref DijkstraWizard class needs default traits, |
1033 | 1035 |
/// as well as the \ref Dijkstra class. |
1034 | 1036 |
/// The \ref DijkstraWizardBase is a class to be the default traits of the |
1035 | 1037 |
/// \ref DijkstraWizard class. |
1036 |
template<class GR,class LM> |
|
1037 |
class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM> |
|
1038 |
template<typename GR, typename LEN> |
|
1039 |
class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LEN> |
|
1038 | 1040 |
{ |
1039 |
typedef DijkstraWizardDefaultTraits<GR, |
|
1041 |
typedef DijkstraWizardDefaultTraits<GR,LEN> Base; |
|
1040 | 1042 |
protected: |
1041 | 1043 |
//The type of the nodes in the digraph. |
1042 | 1044 |
typedef typename Base::Digraph::Node Node; |
1043 | 1045 |
|
1044 | 1046 |
//Pointer to the digraph the algorithm runs on. |
1045 | 1047 |
void *_g; |
1046 | 1048 |
//Pointer to the length map. |
1047 | 1049 |
void *_length; |
1048 | 1050 |
//Pointer to the map of processed nodes. |
1049 | 1051 |
void *_processed; |
1050 | 1052 |
//Pointer to the map of predecessors arcs. |
1051 | 1053 |
void *_pred; |
1052 | 1054 |
//Pointer to the map of distances. |
1053 | 1055 |
void *_dist; |
1054 | 1056 |
//Pointer to the shortest path to the target node. |
1055 | 1057 |
void *_path; |
1056 | 1058 |
//Pointer to the distance of the target node. |
1057 | 1059 |
void *_di; |
1058 | 1060 |
|
1059 | 1061 |
public: |
1060 | 1062 |
/// Constructor. |
1061 | 1063 |
|
1062 | 1064 |
/// This constructor does not require parameters, therefore it initiates |
1063 | 1065 |
/// all of the attributes to \c 0. |
1064 | 1066 |
DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0), |
1065 | 1067 |
_dist(0), _path(0), _di(0) {} |
1066 | 1068 |
|
1067 | 1069 |
/// Constructor. |
1068 | 1070 |
|
1069 | 1071 |
/// This constructor requires two parameters, |
1070 | 1072 |
/// others are initiated to \c 0. |
1071 | 1073 |
/// \param g The digraph the algorithm runs on. |
1072 | 1074 |
/// \param l The length map. |
1073 |
DijkstraWizardBase(const GR &g,const |
|
1075 |
DijkstraWizardBase(const GR &g,const LEN &l) : |
|
1074 | 1076 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
1075 |
_length(reinterpret_cast<void*>(const_cast< |
|
1077 |
_length(reinterpret_cast<void*>(const_cast<LEN*>(&l))), |
|
1076 | 1078 |
_processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
1077 | 1079 |
|
1078 | 1080 |
}; |
1079 | 1081 |
|
1080 | 1082 |
/// Auxiliary class for the function-type interface of Dijkstra algorithm. |
1081 | 1083 |
|
1082 | 1084 |
/// This auxiliary class is created to implement the |
1083 | 1085 |
/// \ref dijkstra() "function-type interface" of \ref Dijkstra algorithm. |
1084 | 1086 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
1085 | 1087 |
/// functions and features of the plain \ref Dijkstra. |
1086 | 1088 |
/// |
1087 | 1089 |
/// This class should only be used through the \ref dijkstra() function, |
1088 | 1090 |
/// which makes it easier to use the algorithm. |
1089 | 1091 |
template<class TR> |
1090 | 1092 |
class DijkstraWizard : public TR |
1091 | 1093 |
{ |
1092 | 1094 |
typedef TR Base; |
1093 | 1095 |
|
1094 | 1096 |
///The type of the digraph the algorithm runs on. |
1095 | 1097 |
typedef typename TR::Digraph Digraph; |
1096 | 1098 |
|
1097 | 1099 |
typedef typename Digraph::Node Node; |
1098 | 1100 |
typedef typename Digraph::NodeIt NodeIt; |
1099 | 1101 |
typedef typename Digraph::Arc Arc; |
1100 | 1102 |
typedef typename Digraph::OutArcIt OutArcIt; |
1101 | 1103 |
|
1102 | 1104 |
///The type of the map that stores the arc lengths. |
1103 | 1105 |
typedef typename TR::LengthMap LengthMap; |
1104 | 1106 |
///The type of the length of the arcs. |
1105 | 1107 |
typedef typename LengthMap::Value Value; |
1106 | 1108 |
///\brief The type of the map that stores the predecessor |
1107 | 1109 |
///arcs of the shortest paths. |
1108 | 1110 |
typedef typename TR::PredMap PredMap; |
1109 | 1111 |
///The type of the map that stores the distances of the nodes. |
1110 | 1112 |
typedef typename TR::DistMap DistMap; |
1111 | 1113 |
///The type of the map that indicates which nodes are processed. |
1112 | 1114 |
typedef typename TR::ProcessedMap ProcessedMap; |
1113 | 1115 |
///The type of the shortest paths |
1114 | 1116 |
typedef typename TR::Path Path; |
1115 | 1117 |
///The heap type used by the dijkstra algorithm. |
1116 | 1118 |
typedef typename TR::Heap Heap; |
1117 | 1119 |
|
1118 | 1120 |
public: |
1119 | 1121 |
|
1120 | 1122 |
/// Constructor. |
1121 | 1123 |
DijkstraWizard() : TR() {} |
1122 | 1124 |
|
1123 | 1125 |
/// Constructor that requires parameters. |
... | ... |
@@ -1236,58 +1238,58 @@ |
1236 | 1238 |
struct SetPathBase : public Base { |
1237 | 1239 |
typedef T Path; |
1238 | 1240 |
SetPathBase(const TR &b) : TR(b) {} |
1239 | 1241 |
}; |
1240 | 1242 |
///\brief \ref named-func-param "Named parameter" |
1241 | 1243 |
///for getting the shortest path to the target node. |
1242 | 1244 |
/// |
1243 | 1245 |
///\ref named-func-param "Named parameter" |
1244 | 1246 |
///for getting the shortest path to the target node. |
1245 | 1247 |
template<class T> |
1246 | 1248 |
DijkstraWizard<SetPathBase<T> > path(const T &t) |
1247 | 1249 |
{ |
1248 | 1250 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1249 | 1251 |
return DijkstraWizard<SetPathBase<T> >(*this); |
1250 | 1252 |
} |
1251 | 1253 |
|
1252 | 1254 |
///\brief \ref named-func-param "Named parameter" |
1253 | 1255 |
///for getting the distance of the target node. |
1254 | 1256 |
/// |
1255 | 1257 |
///\ref named-func-param "Named parameter" |
1256 | 1258 |
///for getting the distance of the target node. |
1257 | 1259 |
DijkstraWizard dist(const Value &d) |
1258 | 1260 |
{ |
1259 | 1261 |
Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d)); |
1260 | 1262 |
return *this; |
1261 | 1263 |
} |
1262 | 1264 |
|
1263 | 1265 |
}; |
1264 | 1266 |
|
1265 | 1267 |
///Function-type interface for Dijkstra algorithm. |
1266 | 1268 |
|
1267 | 1269 |
/// \ingroup shortest_path |
1268 | 1270 |
///Function-type interface for Dijkstra algorithm. |
1269 | 1271 |
/// |
1270 | 1272 |
///This function also has several \ref named-func-param "named parameters", |
1271 | 1273 |
///they are declared as the members of class \ref DijkstraWizard. |
1272 | 1274 |
///The following examples show how to use these parameters. |
1273 | 1275 |
///\code |
1274 | 1276 |
/// // Compute shortest path from node s to each node |
1275 | 1277 |
/// dijkstra(g,length).predMap(preds).distMap(dists).run(s); |
1276 | 1278 |
/// |
1277 | 1279 |
/// // Compute shortest path from s to t |
1278 | 1280 |
/// bool reached = dijkstra(g,length).path(p).dist(d).run(s,t); |
1279 | 1281 |
///\endcode |
1280 | 1282 |
///\warning Don't forget to put the \ref DijkstraWizard::run(Node) "run()" |
1281 | 1283 |
///to the end of the parameter list. |
1282 | 1284 |
///\sa DijkstraWizard |
1283 | 1285 |
///\sa Dijkstra |
1284 |
template<class GR, class LM> |
|
1285 |
DijkstraWizard<DijkstraWizardBase<GR,LM> > |
|
1286 |
|
|
1286 |
template<typename GR, typename LEN> |
|
1287 |
DijkstraWizard<DijkstraWizardBase<GR,LEN> > |
|
1288 |
dijkstra(const GR &digraph, const LEN &length) |
|
1287 | 1289 |
{ |
1288 |
return DijkstraWizard<DijkstraWizardBase<GR, |
|
1290 |
return DijkstraWizard<DijkstraWizardBase<GR,LEN> >(digraph,length); |
|
1289 | 1291 |
} |
1290 | 1292 |
|
1291 | 1293 |
} //END OF NAMESPACE LEMON |
1292 | 1294 |
|
1293 | 1295 |
#endif |
... | ... |
@@ -210,178 +210,178 @@ |
210 | 210 |
class NodeMap : public GR::template NodeMap<V> { |
211 | 211 |
public: |
212 | 212 |
|
213 | 213 |
typedef typename GR::template NodeMap<V> Parent; |
214 | 214 |
|
215 | 215 |
explicit NodeMap(const ListArcSetBase<GR>& arcset) |
216 | 216 |
: Parent(*arcset._graph) {} |
217 | 217 |
|
218 | 218 |
NodeMap(const ListArcSetBase<GR>& arcset, const V& value) |
219 | 219 |
: Parent(*arcset._graph, value) {} |
220 | 220 |
|
221 | 221 |
NodeMap& operator=(const NodeMap& cmap) { |
222 | 222 |
return operator=<NodeMap>(cmap); |
223 | 223 |
} |
224 | 224 |
|
225 | 225 |
template <typename CMap> |
226 | 226 |
NodeMap& operator=(const CMap& cmap) { |
227 | 227 |
Parent::operator=(cmap); |
228 | 228 |
return *this; |
229 | 229 |
} |
230 | 230 |
}; |
231 | 231 |
|
232 | 232 |
}; |
233 | 233 |
|
234 | 234 |
/// \ingroup semi_adaptors |
235 | 235 |
/// |
236 | 236 |
/// \brief Digraph using a node set of another digraph or graph and |
237 | 237 |
/// an own arc set. |
238 | 238 |
/// |
239 | 239 |
/// This structure can be used to establish another directed graph |
240 | 240 |
/// over a node set of an existing one. This class uses the same |
241 | 241 |
/// Node type as the underlying graph, and each valid node of the |
242 | 242 |
/// original graph is valid in this arc set, therefore the node |
243 | 243 |
/// objects of the original graph can be used directly with this |
244 | 244 |
/// class. The node handling functions (id handling, observing, and |
245 | 245 |
/// iterators) works equivalently as in the original graph. |
246 | 246 |
/// |
247 | 247 |
/// This implementation is based on doubly-linked lists, from each |
248 | 248 |
/// node the outgoing and the incoming arcs make up lists, therefore |
249 | 249 |
/// one arc can be erased in constant time. It also makes possible, |
250 | 250 |
/// that node can be removed from the underlying graph, in this case |
251 | 251 |
/// all arcs incident to the given node is erased from the arc set. |
252 | 252 |
/// |
253 | 253 |
/// \param GR The type of the graph which shares its node set with |
254 | 254 |
/// this class. Its interface must conform to the |
255 | 255 |
/// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph" |
256 | 256 |
/// concept. |
257 | 257 |
/// |
258 |
/// This class |
|
258 |
/// This class fully conforms to the \ref concepts::Digraph |
|
259 | 259 |
/// "Digraph" concept. |
260 | 260 |
template <typename GR> |
261 | 261 |
class ListArcSet : public ArcSetExtender<ListArcSetBase<GR> > { |
262 | 262 |
|
263 | 263 |
public: |
264 | 264 |
|
265 | 265 |
typedef ArcSetExtender<ListArcSetBase<GR> > Parent; |
266 | 266 |
|
267 | 267 |
typedef typename Parent::Node Node; |
268 | 268 |
typedef typename Parent::Arc Arc; |
269 | 269 |
|
270 | 270 |
typedef GR Graph; |
271 | 271 |
|
272 | 272 |
|
273 | 273 |
typedef typename Parent::NodesImplBase NodesImplBase; |
274 | 274 |
|
275 | 275 |
void eraseNode(const Node& node) { |
276 | 276 |
Arc arc; |
277 | 277 |
Parent::firstOut(arc, node); |
278 | 278 |
while (arc != INVALID ) { |
279 | 279 |
erase(arc); |
280 | 280 |
Parent::firstOut(arc, node); |
281 | 281 |
} |
282 | 282 |
|
283 | 283 |
Parent::firstIn(arc, node); |
284 | 284 |
while (arc != INVALID ) { |
285 | 285 |
erase(arc); |
286 | 286 |
Parent::firstIn(arc, node); |
287 | 287 |
} |
288 | 288 |
} |
289 | 289 |
|
290 | 290 |
void clearNodes() { |
291 | 291 |
Parent::clear(); |
292 | 292 |
} |
293 | 293 |
|
294 | 294 |
class NodesImpl : public NodesImplBase { |
295 | 295 |
public: |
296 | 296 |
typedef NodesImplBase Parent; |
297 | 297 |
|
298 | 298 |
NodesImpl(const GR& graph, ListArcSet& arcset) |
299 | 299 |
: Parent(graph), _arcset(arcset) {} |
300 | 300 |
|
301 | 301 |
virtual ~NodesImpl() {} |
302 | 302 |
|
303 | 303 |
protected: |
304 | 304 |
|
305 | 305 |
virtual void erase(const Node& node) { |
306 | 306 |
_arcset.eraseNode(node); |
307 | 307 |
Parent::erase(node); |
308 | 308 |
} |
309 | 309 |
virtual void erase(const std::vector<Node>& nodes) { |
310 | 310 |
for (int i = 0; i < int(nodes.size()); ++i) { |
311 | 311 |
_arcset.eraseNode(nodes[i]); |
312 | 312 |
} |
313 | 313 |
Parent::erase(nodes); |
314 | 314 |
} |
315 | 315 |
virtual void clear() { |
316 | 316 |
_arcset.clearNodes(); |
317 | 317 |
Parent::clear(); |
318 | 318 |
} |
319 | 319 |
|
320 | 320 |
private: |
321 | 321 |
ListArcSet& _arcset; |
322 | 322 |
}; |
323 | 323 |
|
324 | 324 |
NodesImpl _nodes; |
325 | 325 |
|
326 | 326 |
public: |
327 | 327 |
|
328 | 328 |
/// \brief Constructor of the ArcSet. |
329 | 329 |
/// |
330 | 330 |
/// Constructor of the ArcSet. |
331 | 331 |
ListArcSet(const GR& graph) : _nodes(graph, *this) { |
332 | 332 |
Parent::initalize(graph, _nodes); |
333 | 333 |
} |
334 | 334 |
|
335 | 335 |
/// \brief Add a new arc to the digraph. |
336 | 336 |
/// |
337 | 337 |
/// Add a new arc to the digraph with source node \c s |
338 | 338 |
/// and target node \c t. |
339 |
/// \return |
|
339 |
/// \return The new arc. |
|
340 | 340 |
Arc addArc(const Node& s, const Node& t) { |
341 | 341 |
return Parent::addArc(s, t); |
342 | 342 |
} |
343 | 343 |
|
344 | 344 |
/// \brief Erase an arc from the digraph. |
345 | 345 |
/// |
346 | 346 |
/// Erase an arc \c a from the digraph. |
347 | 347 |
void erase(const Arc& a) { |
348 | 348 |
return Parent::erase(a); |
349 | 349 |
} |
350 | 350 |
|
351 | 351 |
}; |
352 | 352 |
|
353 | 353 |
template <typename GR> |
354 | 354 |
class ListEdgeSetBase { |
355 | 355 |
public: |
356 | 356 |
|
357 | 357 |
typedef GR Graph; |
358 | 358 |
typedef typename GR::Node Node; |
359 | 359 |
typedef typename GR::NodeIt NodeIt; |
360 | 360 |
|
361 | 361 |
protected: |
362 | 362 |
|
363 | 363 |
struct NodeT { |
364 | 364 |
int first_out; |
365 | 365 |
NodeT() : first_out(-1) {} |
366 | 366 |
}; |
367 | 367 |
|
368 | 368 |
typedef typename ItemSetTraits<GR, Node>:: |
369 | 369 |
template Map<NodeT>::Type NodesImplBase; |
370 | 370 |
|
371 | 371 |
NodesImplBase* _nodes; |
372 | 372 |
|
373 | 373 |
struct ArcT { |
374 | 374 |
Node target; |
375 | 375 |
int prev_out, next_out; |
376 | 376 |
ArcT() : prev_out(-1), next_out(-1) {} |
377 | 377 |
}; |
378 | 378 |
|
379 | 379 |
std::vector<ArcT> arcs; |
380 | 380 |
|
381 | 381 |
int first_arc; |
382 | 382 |
int first_free_arc; |
383 | 383 |
|
384 | 384 |
const GR* _graph; |
385 | 385 |
|
386 | 386 |
void initalize(const GR& graph, NodesImplBase& nodes) { |
387 | 387 |
_graph = &graph; |
... | ... |
@@ -639,174 +639,174 @@ |
639 | 639 |
class NodeMap : public GR::template NodeMap<V> { |
640 | 640 |
public: |
641 | 641 |
|
642 | 642 |
typedef typename GR::template NodeMap<V> Parent; |
643 | 643 |
|
644 | 644 |
explicit NodeMap(const ListEdgeSetBase<GR>& arcset) |
645 | 645 |
: Parent(*arcset._graph) {} |
646 | 646 |
|
647 | 647 |
NodeMap(const ListEdgeSetBase<GR>& arcset, const V& value) |
648 | 648 |
: Parent(*arcset._graph, value) {} |
649 | 649 |
|
650 | 650 |
NodeMap& operator=(const NodeMap& cmap) { |
651 | 651 |
return operator=<NodeMap>(cmap); |
652 | 652 |
} |
653 | 653 |
|
654 | 654 |
template <typename CMap> |
655 | 655 |
NodeMap& operator=(const CMap& cmap) { |
656 | 656 |
Parent::operator=(cmap); |
657 | 657 |
return *this; |
658 | 658 |
} |
659 | 659 |
}; |
660 | 660 |
|
661 | 661 |
}; |
662 | 662 |
|
663 | 663 |
/// \ingroup semi_adaptors |
664 | 664 |
/// |
665 | 665 |
/// \brief Graph using a node set of another digraph or graph and an |
666 | 666 |
/// own edge set. |
667 | 667 |
/// |
668 | 668 |
/// This structure can be used to establish another graph over a |
669 | 669 |
/// node set of an existing one. This class uses the same Node type |
670 | 670 |
/// as the underlying graph, and each valid node of the original |
671 | 671 |
/// graph is valid in this arc set, therefore the node objects of |
672 | 672 |
/// the original graph can be used directly with this class. The |
673 | 673 |
/// node handling functions (id handling, observing, and iterators) |
674 | 674 |
/// works equivalently as in the original graph. |
675 | 675 |
/// |
676 | 676 |
/// This implementation is based on doubly-linked lists, from each |
677 | 677 |
/// node the incident edges make up lists, therefore one edge can be |
678 | 678 |
/// erased in constant time. It also makes possible, that node can |
679 | 679 |
/// be removed from the underlying graph, in this case all edges |
680 | 680 |
/// incident to the given node is erased from the arc set. |
681 | 681 |
/// |
682 | 682 |
/// \param GR The type of the graph which shares its node set |
683 | 683 |
/// with this class. Its interface must conform to the |
684 | 684 |
/// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph" |
685 | 685 |
/// concept. |
686 | 686 |
/// |
687 |
/// This class |
|
687 |
/// This class fully conforms to the \ref concepts::Graph "Graph" |
|
688 | 688 |
/// concept. |
689 | 689 |
template <typename GR> |
690 | 690 |
class ListEdgeSet : public EdgeSetExtender<ListEdgeSetBase<GR> > { |
691 | 691 |
|
692 | 692 |
public: |
693 | 693 |
|
694 | 694 |
typedef EdgeSetExtender<ListEdgeSetBase<GR> > Parent; |
695 | 695 |
|
696 | 696 |
typedef typename Parent::Node Node; |
697 | 697 |
typedef typename Parent::Arc Arc; |
698 | 698 |
typedef typename Parent::Edge Edge; |
699 | 699 |
|
700 | 700 |
typedef GR Graph; |
701 | 701 |
|
702 | 702 |
|
703 | 703 |
typedef typename Parent::NodesImplBase NodesImplBase; |
704 | 704 |
|
705 | 705 |
void eraseNode(const Node& node) { |
706 | 706 |
Arc arc; |
707 | 707 |
Parent::firstOut(arc, node); |
708 | 708 |
while (arc != INVALID ) { |
709 | 709 |
erase(arc); |
710 | 710 |
Parent::firstOut(arc, node); |
711 | 711 |
} |
712 | 712 |
|
713 | 713 |
} |
714 | 714 |
|
715 | 715 |
void clearNodes() { |
716 | 716 |
Parent::clear(); |
717 | 717 |
} |
718 | 718 |
|
719 | 719 |
class NodesImpl : public NodesImplBase { |
720 | 720 |
public: |
721 | 721 |
typedef NodesImplBase Parent; |
722 | 722 |
|
723 | 723 |
NodesImpl(const GR& graph, ListEdgeSet& arcset) |
724 | 724 |
: Parent(graph), _arcset(arcset) {} |
725 | 725 |
|
726 | 726 |
virtual ~NodesImpl() {} |
727 | 727 |
|
728 | 728 |
protected: |
729 | 729 |
|
730 | 730 |
virtual void erase(const Node& node) { |
731 | 731 |
_arcset.eraseNode(node); |
732 | 732 |
Parent::erase(node); |
733 | 733 |
} |
734 | 734 |
virtual void erase(const std::vector<Node>& nodes) { |
735 | 735 |
for (int i = 0; i < int(nodes.size()); ++i) { |
736 | 736 |
_arcset.eraseNode(nodes[i]); |
737 | 737 |
} |
738 | 738 |
Parent::erase(nodes); |
739 | 739 |
} |
740 | 740 |
virtual void clear() { |
741 | 741 |
_arcset.clearNodes(); |
742 | 742 |
Parent::clear(); |
743 | 743 |
} |
744 | 744 |
|
745 | 745 |
private: |
746 | 746 |
ListEdgeSet& _arcset; |
747 | 747 |
}; |
748 | 748 |
|
749 | 749 |
NodesImpl _nodes; |
750 | 750 |
|
751 | 751 |
public: |
752 | 752 |
|
753 | 753 |
/// \brief Constructor of the EdgeSet. |
754 | 754 |
/// |
755 | 755 |
/// Constructor of the EdgeSet. |
756 | 756 |
ListEdgeSet(const GR& graph) : _nodes(graph, *this) { |
757 | 757 |
Parent::initalize(graph, _nodes); |
758 | 758 |
} |
759 | 759 |
|
760 | 760 |
/// \brief Add a new edge to the graph. |
761 | 761 |
/// |
762 | 762 |
/// Add a new edge to the graph with node \c u |
763 | 763 |
/// and node \c v endpoints. |
764 |
/// \return |
|
764 |
/// \return The new edge. |
|
765 | 765 |
Edge addEdge(const Node& u, const Node& v) { |
766 | 766 |
return Parent::addEdge(u, v); |
767 | 767 |
} |
768 | 768 |
|
769 | 769 |
/// \brief Erase an edge from the graph. |
770 | 770 |
/// |
771 | 771 |
/// Erase the edge \c e from the graph. |
772 | 772 |
void erase(const Edge& e) { |
773 | 773 |
return Parent::erase(e); |
774 | 774 |
} |
775 | 775 |
|
776 | 776 |
}; |
777 | 777 |
|
778 | 778 |
template <typename GR> |
779 | 779 |
class SmartArcSetBase { |
780 | 780 |
public: |
781 | 781 |
|
782 | 782 |
typedef GR Graph; |
783 | 783 |
typedef typename Graph::Node Node; |
784 | 784 |
typedef typename Graph::NodeIt NodeIt; |
785 | 785 |
|
786 | 786 |
protected: |
787 | 787 |
|
788 | 788 |
struct NodeT { |
789 | 789 |
int first_out, first_in; |
790 | 790 |
NodeT() : first_out(-1), first_in(-1) {} |
791 | 791 |
}; |
792 | 792 |
|
793 | 793 |
typedef typename ItemSetTraits<GR, Node>:: |
794 | 794 |
template Map<NodeT>::Type NodesImplBase; |
795 | 795 |
|
796 | 796 |
NodesImplBase* _nodes; |
797 | 797 |
|
798 | 798 |
struct ArcT { |
799 | 799 |
Node source, target; |
800 | 800 |
int next_out, next_in; |
801 | 801 |
ArcT() {} |
802 | 802 |
}; |
803 | 803 |
|
804 | 804 |
std::vector<ArcT> arcs; |
805 | 805 |
|
806 | 806 |
const GR* _graph; |
807 | 807 |
|
808 | 808 |
void initalize(const GR& graph, NodesImplBase& nodes) { |
809 | 809 |
_graph = &graph; |
810 | 810 |
_nodes = &nodes; |
811 | 811 |
} |
812 | 812 |
|
... | ... |
@@ -907,186 +907,186 @@ |
907 | 907 |
explicit NodeMap(const SmartArcSetBase<GR>& arcset) |
908 | 908 |
: Parent(*arcset._graph) { } |
909 | 909 |
|
910 | 910 |
NodeMap(const SmartArcSetBase<GR>& arcset, const V& value) |
911 | 911 |
: Parent(*arcset._graph, value) { } |
912 | 912 |
|
913 | 913 |
NodeMap& operator=(const NodeMap& cmap) { |
914 | 914 |
return operator=<NodeMap>(cmap); |
915 | 915 |
} |
916 | 916 |
|
917 | 917 |
template <typename CMap> |
918 | 918 |
NodeMap& operator=(const CMap& cmap) { |
919 | 919 |
Parent::operator=(cmap); |
920 | 920 |
return *this; |
921 | 921 |
} |
922 | 922 |
}; |
923 | 923 |
|
924 | 924 |
}; |
925 | 925 |
|
926 | 926 |
|
927 | 927 |
/// \ingroup semi_adaptors |
928 | 928 |
/// |
929 | 929 |
/// \brief Digraph using a node set of another digraph or graph and |
930 | 930 |
/// an own arc set. |
931 | 931 |
/// |
932 | 932 |
/// This structure can be used to establish another directed graph |
933 | 933 |
/// over a node set of an existing one. This class uses the same |
934 | 934 |
/// Node type as the underlying graph, and each valid node of the |
935 | 935 |
/// original graph is valid in this arc set, therefore the node |
936 | 936 |
/// objects of the original graph can be used directly with this |
937 | 937 |
/// class. The node handling functions (id handling, observing, and |
938 | 938 |
/// iterators) works equivalently as in the original graph. |
939 | 939 |
/// |
940 | 940 |
/// \param GR The type of the graph which shares its node set with |
941 | 941 |
/// this class. Its interface must conform to the |
942 | 942 |
/// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph" |
943 | 943 |
/// concept. |
944 | 944 |
/// |
945 | 945 |
/// This implementation is slightly faster than the \c ListArcSet, |
946 | 946 |
/// because it uses continuous storage for arcs and it uses just |
947 | 947 |
/// single-linked lists for enumerate outgoing and incoming |
948 | 948 |
/// arcs. Therefore the arcs cannot be erased from the arc sets. |
949 | 949 |
/// |
950 | 950 |
/// \warning If a node is erased from the underlying graph and this |
951 | 951 |
/// node is the source or target of one arc in the arc set, then |
952 | 952 |
/// the arc set is invalidated, and it cannot be used anymore. The |
953 | 953 |
/// validity can be checked with the \c valid() member function. |
954 | 954 |
/// |
955 |
/// This class |
|
955 |
/// This class fully conforms to the \ref concepts::Digraph |
|
956 | 956 |
/// "Digraph" concept. |
957 | 957 |
template <typename GR> |
958 | 958 |
class SmartArcSet : public ArcSetExtender<SmartArcSetBase<GR> > { |
959 | 959 |
|
960 | 960 |
public: |
961 | 961 |
|
962 | 962 |
typedef ArcSetExtender<SmartArcSetBase<GR> > Parent; |
963 | 963 |
|
964 | 964 |
typedef typename Parent::Node Node; |
965 | 965 |
typedef typename Parent::Arc Arc; |
966 | 966 |
|
967 | 967 |
typedef GR Graph; |
968 | 968 |
|
969 | 969 |
protected: |
970 | 970 |
|
971 | 971 |
typedef typename Parent::NodesImplBase NodesImplBase; |
972 | 972 |
|
973 | 973 |
void eraseNode(const Node& node) { |
974 | 974 |
if (typename Parent::InArcIt(*this, node) == INVALID && |
975 | 975 |
typename Parent::OutArcIt(*this, node) == INVALID) { |
976 | 976 |
return; |
977 | 977 |
} |
978 | 978 |
throw typename NodesImplBase::Notifier::ImmediateDetach(); |
979 | 979 |
} |
980 | 980 |
|
981 | 981 |
void clearNodes() { |
982 | 982 |
Parent::clear(); |
983 | 983 |
} |
984 | 984 |
|
985 | 985 |
class NodesImpl : public NodesImplBase { |
986 | 986 |
public: |
987 | 987 |
typedef NodesImplBase Parent; |
988 | 988 |
|
989 | 989 |
NodesImpl(const GR& graph, SmartArcSet& arcset) |
990 | 990 |
: Parent(graph), _arcset(arcset) {} |
991 | 991 |
|
992 | 992 |
virtual ~NodesImpl() {} |
993 | 993 |
|
994 | 994 |
bool attached() const { |
995 | 995 |
return Parent::attached(); |
996 | 996 |
} |
997 | 997 |
|
998 | 998 |
protected: |
999 | 999 |
|
1000 | 1000 |
virtual void erase(const Node& node) { |
1001 | 1001 |
try { |
1002 | 1002 |
_arcset.eraseNode(node); |
1003 | 1003 |
Parent::erase(node); |
1004 | 1004 |
} catch (const typename NodesImplBase::Notifier::ImmediateDetach&) { |
1005 | 1005 |
Parent::clear(); |
1006 | 1006 |
throw; |
1007 | 1007 |
} |
1008 | 1008 |
} |
1009 | 1009 |
virtual void erase(const std::vector<Node>& nodes) { |
1010 | 1010 |
try { |
1011 | 1011 |
for (int i = 0; i < int(nodes.size()); ++i) { |
1012 | 1012 |
_arcset.eraseNode(nodes[i]); |
1013 | 1013 |
} |
1014 | 1014 |
Parent::erase(nodes); |
1015 | 1015 |
} catch (const typename NodesImplBase::Notifier::ImmediateDetach&) { |
1016 | 1016 |
Parent::clear(); |
1017 | 1017 |
throw; |
1018 | 1018 |
} |
1019 | 1019 |
} |
1020 | 1020 |
virtual void clear() { |
1021 | 1021 |
_arcset.clearNodes(); |
1022 | 1022 |
Parent::clear(); |
1023 | 1023 |
} |
1024 | 1024 |
|
1025 | 1025 |
private: |
1026 | 1026 |
SmartArcSet& _arcset; |
1027 | 1027 |
}; |
1028 | 1028 |
|
1029 | 1029 |
NodesImpl _nodes; |
1030 | 1030 |
|
1031 | 1031 |
public: |
1032 | 1032 |
|
1033 | 1033 |
/// \brief Constructor of the ArcSet. |
1034 | 1034 |
/// |
1035 | 1035 |
/// Constructor of the ArcSet. |
1036 | 1036 |
SmartArcSet(const GR& graph) : _nodes(graph, *this) { |
1037 | 1037 |
Parent::initalize(graph, _nodes); |
1038 | 1038 |
} |
1039 | 1039 |
|
1040 | 1040 |
/// \brief Add a new arc to the digraph. |
1041 | 1041 |
/// |
1042 | 1042 |
/// Add a new arc to the digraph with source node \c s |
1043 | 1043 |
/// and target node \c t. |
1044 |
/// \return |
|
1044 |
/// \return The new arc. |
|
1045 | 1045 |
Arc addArc(const Node& s, const Node& t) { |
1046 | 1046 |
return Parent::addArc(s, t); |
1047 | 1047 |
} |
1048 | 1048 |
|
1049 | 1049 |
/// \brief Validity check |
1050 | 1050 |
/// |
1051 | 1051 |
/// This functions gives back false if the ArcSet is |
1052 | 1052 |
/// invalidated. It occurs when a node in the underlying graph is |
1053 | 1053 |
/// erased and it is not isolated in the ArcSet. |
1054 | 1054 |
bool valid() const { |
1055 | 1055 |
return _nodes.attached(); |
1056 | 1056 |
} |
1057 | 1057 |
|
1058 | 1058 |
}; |
1059 | 1059 |
|
1060 | 1060 |
|
1061 | 1061 |
template <typename GR> |
1062 | 1062 |
class SmartEdgeSetBase { |
1063 | 1063 |
public: |
1064 | 1064 |
|
1065 | 1065 |
typedef GR Graph; |
1066 | 1066 |
typedef typename GR::Node Node; |
1067 | 1067 |
typedef typename GR::NodeIt NodeIt; |
1068 | 1068 |
|
1069 | 1069 |
protected: |
1070 | 1070 |
|
1071 | 1071 |
struct NodeT { |
1072 | 1072 |
int first_out; |
1073 | 1073 |
NodeT() : first_out(-1) {} |
1074 | 1074 |
}; |
1075 | 1075 |
|
1076 | 1076 |
typedef typename ItemSetTraits<GR, Node>:: |
1077 | 1077 |
template Map<NodeT>::Type NodesImplBase; |
1078 | 1078 |
|
1079 | 1079 |
NodesImplBase* _nodes; |
1080 | 1080 |
|
1081 | 1081 |
struct ArcT { |
1082 | 1082 |
Node target; |
1083 | 1083 |
int next_out; |
1084 | 1084 |
ArcT() {} |
1085 | 1085 |
}; |
1086 | 1086 |
|
1087 | 1087 |
std::vector<ArcT> arcs; |
1088 | 1088 |
|
1089 | 1089 |
const GR* _graph; |
1090 | 1090 |
|
1091 | 1091 |
void initalize(const GR& graph, NodesImplBase& nodes) { |
1092 | 1092 |
_graph = &graph; |
... | ... |
@@ -1255,156 +1255,156 @@ |
1255 | 1255 |
|
1256 | 1256 |
explicit NodeMap(const SmartEdgeSetBase<GR>& arcset) |
1257 | 1257 |
: Parent(*arcset._graph) { } |
1258 | 1258 |
|
1259 | 1259 |
NodeMap(const SmartEdgeSetBase<GR>& arcset, const V& value) |
1260 | 1260 |
: Parent(*arcset._graph, value) { } |
1261 | 1261 |
|
1262 | 1262 |
NodeMap& operator=(const NodeMap& cmap) { |
1263 | 1263 |
return operator=<NodeMap>(cmap); |
1264 | 1264 |
} |
1265 | 1265 |
|
1266 | 1266 |
template <typename CMap> |
1267 | 1267 |
NodeMap& operator=(const CMap& cmap) { |
1268 | 1268 |
Parent::operator=(cmap); |
1269 | 1269 |
return *this; |
1270 | 1270 |
} |
1271 | 1271 |
}; |
1272 | 1272 |
|
1273 | 1273 |
}; |
1274 | 1274 |
|
1275 | 1275 |
/// \ingroup semi_adaptors |
1276 | 1276 |
/// |
1277 | 1277 |
/// \brief Graph using a node set of another digraph or graph and an |
1278 | 1278 |
/// own edge set. |
1279 | 1279 |
/// |
1280 | 1280 |
/// This structure can be used to establish another graph over a |
1281 | 1281 |
/// node set of an existing one. This class uses the same Node type |
1282 | 1282 |
/// as the underlying graph, and each valid node of the original |
1283 | 1283 |
/// graph is valid in this arc set, therefore the node objects of |
1284 | 1284 |
/// the original graph can be used directly with this class. The |
1285 | 1285 |
/// node handling functions (id handling, observing, and iterators) |
1286 | 1286 |
/// works equivalently as in the original graph. |
1287 | 1287 |
/// |
1288 | 1288 |
/// \param GR The type of the graph which shares its node set |
1289 | 1289 |
/// with this class. Its interface must conform to the |
1290 | 1290 |
/// \ref concepts::Digraph "Digraph" or \ref concepts::Graph "Graph" |
1291 | 1291 |
/// concept. |
1292 | 1292 |
/// |
1293 | 1293 |
/// This implementation is slightly faster than the \c ListEdgeSet, |
1294 | 1294 |
/// because it uses continuous storage for edges and it uses just |
1295 | 1295 |
/// single-linked lists for enumerate incident edges. Therefore the |
1296 | 1296 |
/// edges cannot be erased from the edge sets. |
1297 | 1297 |
/// |
1298 | 1298 |
/// \warning If a node is erased from the underlying graph and this |
1299 | 1299 |
/// node is incident to one edge in the edge set, then the edge set |
1300 | 1300 |
/// is invalidated, and it cannot be used anymore. The validity can |
1301 | 1301 |
/// be checked with the \c valid() member function. |
1302 | 1302 |
/// |
1303 |
/// This class |
|
1303 |
/// This class fully conforms to the \ref concepts::Graph |
|
1304 | 1304 |
/// "Graph" concept. |
1305 | 1305 |
template <typename GR> |
1306 | 1306 |
class SmartEdgeSet : public EdgeSetExtender<SmartEdgeSetBase<GR> > { |
1307 | 1307 |
|
1308 | 1308 |
public: |
1309 | 1309 |
|
1310 | 1310 |
typedef EdgeSetExtender<SmartEdgeSetBase<GR> > Parent; |
1311 | 1311 |
|
1312 | 1312 |
typedef typename Parent::Node Node; |
1313 | 1313 |
typedef typename Parent::Arc Arc; |
1314 | 1314 |
typedef typename Parent::Edge Edge; |
1315 | 1315 |
|
1316 | 1316 |
typedef GR Graph; |
1317 | 1317 |
|
1318 | 1318 |
protected: |
1319 | 1319 |
|
1320 | 1320 |
typedef typename Parent::NodesImplBase NodesImplBase; |
1321 | 1321 |
|
1322 | 1322 |
void eraseNode(const Node& node) { |
1323 | 1323 |
if (typename Parent::IncEdgeIt(*this, node) == INVALID) { |
1324 | 1324 |
return; |
1325 | 1325 |
} |
1326 | 1326 |
throw typename NodesImplBase::Notifier::ImmediateDetach(); |
1327 | 1327 |
} |
1328 | 1328 |
|
1329 | 1329 |
void clearNodes() { |
1330 | 1330 |
Parent::clear(); |
1331 | 1331 |
} |
1332 | 1332 |
|
1333 | 1333 |
class NodesImpl : public NodesImplBase { |
1334 | 1334 |
public: |
1335 | 1335 |
typedef NodesImplBase Parent; |
1336 | 1336 |
|
1337 | 1337 |
NodesImpl(const GR& graph, SmartEdgeSet& arcset) |
1338 | 1338 |
: Parent(graph), _arcset(arcset) {} |
1339 | 1339 |
|
1340 | 1340 |
virtual ~NodesImpl() {} |
1341 | 1341 |
|
1342 | 1342 |
bool attached() const { |
1343 | 1343 |
return Parent::attached(); |
1344 | 1344 |
} |
1345 | 1345 |
|
1346 | 1346 |
protected: |
1347 | 1347 |
|
1348 | 1348 |
virtual void erase(const Node& node) { |
1349 | 1349 |
try { |
1350 | 1350 |
_arcset.eraseNode(node); |
1351 | 1351 |
Parent::erase(node); |
1352 | 1352 |
} catch (const typename NodesImplBase::Notifier::ImmediateDetach&) { |
1353 | 1353 |
Parent::clear(); |
1354 | 1354 |
throw; |
1355 | 1355 |
} |
1356 | 1356 |
} |
1357 | 1357 |
virtual void erase(const std::vector<Node>& nodes) { |
1358 | 1358 |
try { |
1359 | 1359 |
for (int i = 0; i < int(nodes.size()); ++i) { |
1360 | 1360 |
_arcset.eraseNode(nodes[i]); |
1361 | 1361 |
} |
1362 | 1362 |
Parent::erase(nodes); |
1363 | 1363 |
} catch (const typename NodesImplBase::Notifier::ImmediateDetach&) { |
1364 | 1364 |
Parent::clear(); |
1365 | 1365 |
throw; |
1366 | 1366 |
} |
1367 | 1367 |
} |
1368 | 1368 |
virtual void clear() { |
1369 | 1369 |
_arcset.clearNodes(); |
1370 | 1370 |
Parent::clear(); |
1371 | 1371 |
} |
1372 | 1372 |
|
1373 | 1373 |
private: |
1374 | 1374 |
SmartEdgeSet& _arcset; |
1375 | 1375 |
}; |
1376 | 1376 |
|
1377 | 1377 |
NodesImpl _nodes; |
1378 | 1378 |
|
1379 | 1379 |
public: |
1380 | 1380 |
|
1381 | 1381 |
/// \brief Constructor of the EdgeSet. |
1382 | 1382 |
/// |
1383 | 1383 |
/// Constructor of the EdgeSet. |
1384 | 1384 |
SmartEdgeSet(const GR& graph) : _nodes(graph, *this) { |
1385 | 1385 |
Parent::initalize(graph, _nodes); |
1386 | 1386 |
} |
1387 | 1387 |
|
1388 | 1388 |
/// \brief Add a new edge to the graph. |
1389 | 1389 |
/// |
1390 | 1390 |
/// Add a new edge to the graph with node \c u |
1391 | 1391 |
/// and node \c v endpoints. |
1392 |
/// \return |
|
1392 |
/// \return The new edge. |
|
1393 | 1393 |
Edge addEdge(const Node& u, const Node& v) { |
1394 | 1394 |
return Parent::addEdge(u, v); |
1395 | 1395 |
} |
1396 | 1396 |
|
1397 | 1397 |
/// \brief Validity check |
1398 | 1398 |
/// |
1399 | 1399 |
/// This functions gives back false if the EdgeSet is |
1400 | 1400 |
/// invalidated. It occurs when a node in the underlying graph is |
1401 | 1401 |
/// erased and it is not isolated in the EdgeSet. |
1402 | 1402 |
bool valid() const { |
1403 | 1403 |
return _nodes.attached(); |
1404 | 1404 |
} |
1405 | 1405 |
|
1406 | 1406 |
}; |
1407 | 1407 |
|
1408 | 1408 |
} |
1409 | 1409 |
|
1410 | 1410 |
#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 | 5 |
* Copyright (C) 2003-2009 |
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_ELEVATOR_H |
20 | 20 |
#define LEMON_ELEVATOR_H |
21 | 21 |
|
22 | 22 |
///\ingroup auxdat |
23 | 23 |
///\file |
24 | 24 |
///\brief Elevator class |
25 | 25 |
/// |
26 | 26 |
///Elevator class implements an efficient data structure |
27 | 27 |
///for labeling items in push-relabel type algorithms. |
28 | 28 |
/// |
29 | 29 |
|
30 | 30 |
#include <lemon/core.h> |
31 | 31 |
#include <lemon/bits/traits.h> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
///Class for handling "labels" in push-relabel type algorithms. |
36 | 36 |
|
37 | 37 |
///A class for handling "labels" in push-relabel type algorithms. |
38 | 38 |
/// |
39 | 39 |
///\ingroup auxdat |
40 | 40 |
///Using this class you can assign "labels" (nonnegative integer numbers) |
41 | 41 |
///to the edges or nodes of a graph, manipulate and query them through |
42 | 42 |
///operations typically arising in "push-relabel" type algorithms. |
43 | 43 |
/// |
44 | 44 |
///Each item is either \em active or not, and you can also choose a |
45 | 45 |
///highest level active item. |
46 | 46 |
/// |
47 | 47 |
///\sa LinkedElevator |
48 | 48 |
/// |
49 |
///\param Graph Type of the underlying graph. |
|
50 |
///\param Item Type of the items the data is assigned to (Graph::Node, |
|
51 |
///Graph::Arc, Graph::Edge). |
|
52 |
template<class Graph, class Item> |
|
49 |
///\param GR Type of the underlying graph. |
|
50 |
///\param Item Type of the items the data is assigned to (\c GR::Node, |
|
51 |
///\c GR::Arc or \c GR::Edge). |
|
52 |
template<class GR, class Item> |
|
53 | 53 |
class Elevator |
54 | 54 |
{ |
55 | 55 |
public: |
56 | 56 |
|
57 | 57 |
typedef Item Key; |
58 | 58 |
typedef int Value; |
59 | 59 |
|
60 | 60 |
private: |
61 | 61 |
|
62 | 62 |
typedef Item *Vit; |
63 |
typedef typename ItemSetTraits<Graph,Item>::template Map<Vit>::Type VitMap; |
|
64 |
typedef typename ItemSetTraits<Graph,Item>::template Map<int>::Type IntMap; |
|
63 |
typedef typename ItemSetTraits<GR,Item>::template Map<Vit>::Type VitMap; |
|
64 |
typedef typename ItemSetTraits<GR,Item>::template Map<int>::Type IntMap; |
|
65 | 65 |
|
66 |
const |
|
66 |
const GR &_g; |
|
67 | 67 |
int _max_level; |
68 | 68 |
int _item_num; |
69 | 69 |
VitMap _where; |
70 | 70 |
IntMap _level; |
71 | 71 |
std::vector<Item> _items; |
72 | 72 |
std::vector<Vit> _first; |
73 | 73 |
std::vector<Vit> _last_active; |
74 | 74 |
|
75 | 75 |
int _highest_active; |
76 | 76 |
|
77 | 77 |
void copy(Item i, Vit p) |
78 | 78 |
{ |
79 | 79 |
_where.set(*p=i,p); |
80 | 80 |
} |
81 | 81 |
void copy(Vit s, Vit p) |
82 | 82 |
{ |
83 | 83 |
if(s!=p) |
84 | 84 |
{ |
85 | 85 |
Item i=*s; |
86 | 86 |
*p=i; |
87 | 87 |
_where.set(i,p); |
88 | 88 |
} |
89 | 89 |
} |
90 | 90 |
void swap(Vit i, Vit j) |
91 | 91 |
{ |
92 | 92 |
Item ti=*i; |
93 | 93 |
Vit ct = _where[ti]; |
94 | 94 |
_where.set(ti,_where[*i=*j]); |
95 | 95 |
_where.set(*j,ct); |
96 | 96 |
*j=ti; |
97 | 97 |
} |
98 | 98 |
|
99 | 99 |
public: |
100 | 100 |
|
101 | 101 |
///Constructor with given maximum level. |
102 | 102 |
|
103 | 103 |
///Constructor with given maximum level. |
104 | 104 |
/// |
105 | 105 |
///\param graph The underlying graph. |
106 | 106 |
///\param max_level The maximum allowed level. |
107 | 107 |
///Set the range of the possible labels to <tt>[0..max_level]</tt>. |
108 |
Elevator(const |
|
108 |
Elevator(const GR &graph,int max_level) : |
|
109 | 109 |
_g(graph), |
110 | 110 |
_max_level(max_level), |
111 | 111 |
_item_num(_max_level), |
112 | 112 |
_where(graph), |
113 | 113 |
_level(graph,0), |
114 | 114 |
_items(_max_level), |
115 | 115 |
_first(_max_level+2), |
116 | 116 |
_last_active(_max_level+2), |
117 | 117 |
_highest_active(-1) {} |
118 | 118 |
///Constructor. |
119 | 119 |
|
120 | 120 |
///Constructor. |
121 | 121 |
/// |
122 | 122 |
///\param graph The underlying graph. |
123 | 123 |
///Set the range of the possible labels to <tt>[0..max_level]</tt>, |
124 | 124 |
///where \c max_level is equal to the number of labeled items in the graph. |
125 |
Elevator(const |
|
125 |
Elevator(const GR &graph) : |
|
126 | 126 |
_g(graph), |
127 |
_max_level(countItems< |
|
127 |
_max_level(countItems<GR, Item>(graph)), |
|
128 | 128 |
_item_num(_max_level), |
129 | 129 |
_where(graph), |
130 | 130 |
_level(graph,0), |
131 | 131 |
_items(_max_level), |
132 | 132 |
_first(_max_level+2), |
133 | 133 |
_last_active(_max_level+2), |
134 | 134 |
_highest_active(-1) |
135 | 135 |
{ |
136 | 136 |
} |
137 | 137 |
|
138 | 138 |
///Activate item \c i. |
139 | 139 |
|
140 | 140 |
///Activate item \c i. |
141 | 141 |
///\pre Item \c i shouldn't be active before. |
142 | 142 |
void activate(Item i) |
143 | 143 |
{ |
144 | 144 |
const int l=_level[i]; |
145 | 145 |
swap(_where[i],++_last_active[l]); |
146 | 146 |
if(l>_highest_active) _highest_active=l; |
147 | 147 |
} |
148 | 148 |
|
149 | 149 |
///Deactivate item \c i. |
150 | 150 |
|
151 | 151 |
///Deactivate item \c i. |
152 | 152 |
///\pre Item \c i must be active before. |
153 | 153 |
void deactivate(Item i) |
154 | 154 |
{ |
155 | 155 |
swap(_where[i],_last_active[_level[i]]--); |
156 | 156 |
while(_highest_active>=0 && |
157 | 157 |
_last_active[_highest_active]<_first[_highest_active]) |
158 | 158 |
_highest_active--; |
159 | 159 |
} |
160 | 160 |
|
161 | 161 |
///Query whether item \c i is active |
162 | 162 |
bool active(Item i) const { return _where[i]<=_last_active[_level[i]]; } |
163 | 163 |
|
164 | 164 |
///Return the level of item \c i. |
165 | 165 |
int operator[](Item i) const { return _level[i]; } |
166 | 166 |
|
167 | 167 |
///Return the number of items on level \c l. |
168 | 168 |
int onLevel(int l) const |
169 | 169 |
{ |
170 | 170 |
return _first[l+1]-_first[l]; |
171 | 171 |
} |
172 | 172 |
///Return true if level \c l is empty. |
173 | 173 |
bool emptyLevel(int l) const |
174 | 174 |
{ |
175 | 175 |
return _first[l+1]-_first[l]==0; |
... | ... |
@@ -385,206 +385,206 @@ |
385 | 385 |
_level.set(i,_max_level - 1); |
386 | 386 |
} |
387 | 387 |
|
388 | 388 |
///Lift all items on and above the given level to the top level. |
389 | 389 |
|
390 | 390 |
///This function lifts all items on and above level \c l to the top |
391 | 391 |
///level and deactivates them. |
392 | 392 |
void liftToTop(int l) |
393 | 393 |
{ |
394 | 394 |
const Vit f=_first[l]; |
395 | 395 |
const Vit tl=_first[_max_level]; |
396 | 396 |
for(Vit i=f;i!=tl;++i) |
397 | 397 |
_level.set(*i,_max_level); |
398 | 398 |
for(int i=l;i<=_max_level;i++) |
399 | 399 |
{ |
400 | 400 |
_first[i]=f; |
401 | 401 |
_last_active[i]=f-1; |
402 | 402 |
} |
403 | 403 |
for(_highest_active=l-1; |
404 | 404 |
_highest_active>=0 && |
405 | 405 |
_last_active[_highest_active]<_first[_highest_active]; |
406 | 406 |
_highest_active--) ; |
407 | 407 |
} |
408 | 408 |
|
409 | 409 |
private: |
410 | 410 |
int _init_lev; |
411 | 411 |
Vit _init_num; |
412 | 412 |
|
413 | 413 |
public: |
414 | 414 |
|
415 | 415 |
///\name Initialization |
416 | 416 |
///Using these functions you can initialize the levels of the items. |
417 | 417 |
///\n |
418 | 418 |
///The initialization must be started with calling \c initStart(). |
419 | 419 |
///Then the items should be listed level by level starting with the |
420 | 420 |
///lowest one (level 0) using \c initAddItem() and \c initNewLevel(). |
421 | 421 |
///Finally \c initFinish() must be called. |
422 | 422 |
///The items not listed are put on the highest level. |
423 | 423 |
///@{ |
424 | 424 |
|
425 | 425 |
///Start the initialization process. |
426 | 426 |
void initStart() |
427 | 427 |
{ |
428 | 428 |
_init_lev=0; |
429 | 429 |
_init_num=&_items[0]; |
430 | 430 |
_first[0]=&_items[0]; |
431 | 431 |
_last_active[0]=&_items[0]-1; |
432 | 432 |
Vit n=&_items[0]; |
433 |
for(typename ItemSetTraits< |
|
433 |
for(typename ItemSetTraits<GR,Item>::ItemIt i(_g);i!=INVALID;++i) |
|
434 | 434 |
{ |
435 | 435 |
*n=i; |
436 | 436 |
_where.set(i,n); |
437 | 437 |
_level.set(i,_max_level); |
438 | 438 |
++n; |
439 | 439 |
} |
440 | 440 |
} |
441 | 441 |
|
442 | 442 |
///Add an item to the current level. |
443 | 443 |
void initAddItem(Item i) |
444 | 444 |
{ |
445 | 445 |
swap(_where[i],_init_num); |
446 | 446 |
_level.set(i,_init_lev); |
447 | 447 |
++_init_num; |
448 | 448 |
} |
449 | 449 |
|
450 | 450 |
///Start a new level. |
451 | 451 |
|
452 | 452 |
///Start a new level. |
453 | 453 |
///It shouldn't be used before the items on level 0 are listed. |
454 | 454 |
void initNewLevel() |
455 | 455 |
{ |
456 | 456 |
_init_lev++; |
457 | 457 |
_first[_init_lev]=_init_num; |
458 | 458 |
_last_active[_init_lev]=_init_num-1; |
459 | 459 |
} |
460 | 460 |
|
461 | 461 |
///Finalize the initialization process. |
462 | 462 |
void initFinish() |
463 | 463 |
{ |
464 | 464 |
for(_init_lev++;_init_lev<=_max_level;_init_lev++) |
465 | 465 |
{ |
466 | 466 |
_first[_init_lev]=_init_num; |
467 | 467 |
_last_active[_init_lev]=_init_num-1; |
468 | 468 |
} |
469 | 469 |
_first[_max_level+1]=&_items[0]+_item_num; |
470 | 470 |
_last_active[_max_level+1]=&_items[0]+_item_num-1; |
471 | 471 |
_highest_active = -1; |
472 | 472 |
} |
473 | 473 |
|
474 | 474 |
///@} |
475 | 475 |
|
476 | 476 |
}; |
477 | 477 |
|
478 | 478 |
///Class for handling "labels" in push-relabel type algorithms. |
479 | 479 |
|
480 | 480 |
///A class for handling "labels" in push-relabel type algorithms. |
481 | 481 |
/// |
482 | 482 |
///\ingroup auxdat |
483 | 483 |
///Using this class you can assign "labels" (nonnegative integer numbers) |
484 | 484 |
///to the edges or nodes of a graph, manipulate and query them through |
485 | 485 |
///operations typically arising in "push-relabel" type algorithms. |
486 | 486 |
/// |
487 | 487 |
///Each item is either \em active or not, and you can also choose a |
488 | 488 |
///highest level active item. |
489 | 489 |
/// |
490 | 490 |
///\sa Elevator |
491 | 491 |
/// |
492 |
///\param Graph Type of the underlying graph. |
|
493 |
///\param Item Type of the items the data is assigned to (Graph::Node, |
|
494 |
///Graph::Arc, Graph::Edge). |
|
495 |
template <class Graph, class Item> |
|
492 |
///\param GR Type of the underlying graph. |
|
493 |
///\param Item Type of the items the data is assigned to (\c GR::Node, |
|
494 |
///\c GR::Arc or \c GR::Edge). |
|
495 |
template <class GR, class Item> |
|
496 | 496 |
class LinkedElevator { |
497 | 497 |
public: |
498 | 498 |
|
499 | 499 |
typedef Item Key; |
500 | 500 |
typedef int Value; |
501 | 501 |
|
502 | 502 |
private: |
503 | 503 |
|
504 |
typedef typename ItemSetTraits< |
|
504 |
typedef typename ItemSetTraits<GR,Item>:: |
|
505 | 505 |
template Map<Item>::Type ItemMap; |
506 |
typedef typename ItemSetTraits< |
|
506 |
typedef typename ItemSetTraits<GR,Item>:: |
|
507 | 507 |
template Map<int>::Type IntMap; |
508 |
typedef typename ItemSetTraits< |
|
508 |
typedef typename ItemSetTraits<GR,Item>:: |
|
509 | 509 |
template Map<bool>::Type BoolMap; |
510 | 510 |
|
511 |
const |
|
511 |
const GR &_graph; |
|
512 | 512 |
int _max_level; |
513 | 513 |
int _item_num; |
514 | 514 |
std::vector<Item> _first, _last; |
515 | 515 |
ItemMap _prev, _next; |
516 | 516 |
int _highest_active; |
517 | 517 |
IntMap _level; |
518 | 518 |
BoolMap _active; |
519 | 519 |
|
520 | 520 |
public: |
521 | 521 |
///Constructor with given maximum level. |
522 | 522 |
|
523 | 523 |
///Constructor with given maximum level. |
524 | 524 |
/// |
525 | 525 |
///\param graph The underlying graph. |
526 | 526 |
///\param max_level The maximum allowed level. |
527 | 527 |
///Set the range of the possible labels to <tt>[0..max_level]</tt>. |
528 |
LinkedElevator(const |
|
528 |
LinkedElevator(const GR& graph, int max_level) |
|
529 | 529 |
: _graph(graph), _max_level(max_level), _item_num(_max_level), |
530 | 530 |
_first(_max_level + 1), _last(_max_level + 1), |
531 | 531 |
_prev(graph), _next(graph), |
532 | 532 |
_highest_active(-1), _level(graph), _active(graph) {} |
533 | 533 |
|
534 | 534 |
///Constructor. |
535 | 535 |
|
536 | 536 |
///Constructor. |
537 | 537 |
/// |
538 | 538 |
///\param graph The underlying graph. |
539 | 539 |
///Set the range of the possible labels to <tt>[0..max_level]</tt>, |
540 | 540 |
///where \c max_level is equal to the number of labeled items in the graph. |
541 |
LinkedElevator(const Graph& graph) |
|
542 |
: _graph(graph), _max_level(countItems<Graph, Item>(graph)), |
|
541 |
LinkedElevator(const GR& graph) |
|
542 |
: _graph(graph), _max_level(countItems<GR, Item>(graph)), |
|
543 | 543 |
_item_num(_max_level), |
544 | 544 |
_first(_max_level + 1), _last(_max_level + 1), |
545 | 545 |
_prev(graph, INVALID), _next(graph, INVALID), |
546 | 546 |
_highest_active(-1), _level(graph), _active(graph) {} |
547 | 547 |
|
548 | 548 |
|
549 | 549 |
///Activate item \c i. |
550 | 550 |
|
551 | 551 |
///Activate item \c i. |
552 | 552 |
///\pre Item \c i shouldn't be active before. |
553 | 553 |
void activate(Item i) { |
554 | 554 |
_active.set(i, true); |
555 | 555 |
|
556 | 556 |
int level = _level[i]; |
557 | 557 |
if (level > _highest_active) { |
558 | 558 |
_highest_active = level; |
559 | 559 |
} |
560 | 560 |
|
561 | 561 |
if (_prev[i] == INVALID || _active[_prev[i]]) return; |
562 | 562 |
//unlace |
563 | 563 |
_next.set(_prev[i], _next[i]); |
564 | 564 |
if (_next[i] != INVALID) { |
565 | 565 |
_prev.set(_next[i], _prev[i]); |
566 | 566 |
} else { |
567 | 567 |
_last[level] = _prev[i]; |
568 | 568 |
} |
569 | 569 |
//lace |
570 | 570 |
_next.set(i, _first[level]); |
571 | 571 |
_prev.set(_first[level], i); |
572 | 572 |
_prev.set(i, INVALID); |
573 | 573 |
_first[level] = i; |
574 | 574 |
|
575 | 575 |
} |
576 | 576 |
|
577 | 577 |
///Deactivate item \c i. |
578 | 578 |
|
579 | 579 |
///Deactivate item \c i. |
580 | 580 |
///\pre Item \c i must be active before. |
581 | 581 |
void deactivate(Item i) { |
582 | 582 |
_active.set(i, false); |
583 | 583 |
int level = _level[i]; |
584 | 584 |
|
585 | 585 |
if (_next[i] == INVALID || !_active[_next[i]]) |
586 | 586 |
goto find_highest_level; |
587 | 587 |
|
588 | 588 |
//unlace |
589 | 589 |
_prev.set(_next[i], _prev[i]); |
590 | 590 |
if (_prev[i] != INVALID) { |
... | ... |
@@ -890,93 +890,93 @@ |
890 | 890 |
void dirtyTopButOne(Item i) { |
891 | 891 |
_level.set(i, _max_level - 1); |
892 | 892 |
} |
893 | 893 |
|
894 | 894 |
///Lift all items on and above the given level to the top level. |
895 | 895 |
|
896 | 896 |
///This function lifts all items on and above level \c l to the top |
897 | 897 |
///level and deactivates them. |
898 | 898 |
void liftToTop(int l) { |
899 | 899 |
for (int i = l + 1; _first[i] != INVALID; ++i) { |
900 | 900 |
Item n = _first[i]; |
901 | 901 |
while (n != INVALID) { |
902 | 902 |
_level.set(n, _max_level); |
903 | 903 |
n = _next[n]; |
904 | 904 |
} |
905 | 905 |
_first[i] = INVALID; |
906 | 906 |
_last[i] = INVALID; |
907 | 907 |
} |
908 | 908 |
if (_highest_active > l - 1) { |
909 | 909 |
_highest_active = l - 1; |
910 | 910 |
while (_highest_active >= 0 && activeFree(_highest_active)) |
911 | 911 |
--_highest_active; |
912 | 912 |
} |
913 | 913 |
} |
914 | 914 |
|
915 | 915 |
private: |
916 | 916 |
|
917 | 917 |
int _init_level; |
918 | 918 |
|
919 | 919 |
public: |
920 | 920 |
|
921 | 921 |
///\name Initialization |
922 | 922 |
///Using these functions you can initialize the levels of the items. |
923 | 923 |
///\n |
924 | 924 |
///The initialization must be started with calling \c initStart(). |
925 | 925 |
///Then the items should be listed level by level starting with the |
926 | 926 |
///lowest one (level 0) using \c initAddItem() and \c initNewLevel(). |
927 | 927 |
///Finally \c initFinish() must be called. |
928 | 928 |
///The items not listed are put on the highest level. |
929 | 929 |
///@{ |
930 | 930 |
|
931 | 931 |
///Start the initialization process. |
932 | 932 |
void initStart() { |
933 | 933 |
|
934 | 934 |
for (int i = 0; i <= _max_level; ++i) { |
935 | 935 |
_first[i] = _last[i] = INVALID; |
936 | 936 |
} |
937 | 937 |
_init_level = 0; |
938 |
for(typename ItemSetTraits< |
|
938 |
for(typename ItemSetTraits<GR,Item>::ItemIt i(_graph); |
|
939 | 939 |
i != INVALID; ++i) { |
940 | 940 |
_level.set(i, _max_level); |
941 | 941 |
_active.set(i, false); |
942 | 942 |
} |
943 | 943 |
} |
944 | 944 |
|
945 | 945 |
///Add an item to the current level. |
946 | 946 |
void initAddItem(Item i) { |
947 | 947 |
_level.set(i, _init_level); |
948 | 948 |
if (_last[_init_level] == INVALID) { |
949 | 949 |
_first[_init_level] = i; |
950 | 950 |
_last[_init_level] = i; |
951 | 951 |
_prev.set(i, INVALID); |
952 | 952 |
_next.set(i, INVALID); |
953 | 953 |
} else { |
954 | 954 |
_prev.set(i, _last[_init_level]); |
955 | 955 |
_next.set(i, INVALID); |
956 | 956 |
_next.set(_last[_init_level], i); |
957 | 957 |
_last[_init_level] = i; |
958 | 958 |
} |
959 | 959 |
} |
960 | 960 |
|
961 | 961 |
///Start a new level. |
962 | 962 |
|
963 | 963 |
///Start a new level. |
964 | 964 |
///It shouldn't be used before the items on level 0 are listed. |
965 | 965 |
void initNewLevel() { |
966 | 966 |
++_init_level; |
967 | 967 |
} |
968 | 968 |
|
969 | 969 |
///Finalize the initialization process. |
970 | 970 |
void initFinish() { |
971 | 971 |
_highest_active = -1; |
972 | 972 |
} |
973 | 973 |
|
974 | 974 |
///@} |
975 | 975 |
|
976 | 976 |
}; |
977 | 977 |
|
978 | 978 |
|
979 | 979 |
} //END OF NAMESPACE LEMON |
980 | 980 |
|
981 | 981 |
#endif |
982 | 982 |
... | ... |
@@ -9,256 +9,256 @@ |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_EULER_H |
20 | 20 |
#define LEMON_EULER_H |
21 | 21 |
|
22 | 22 |
#include<lemon/core.h> |
23 | 23 |
#include<lemon/adaptors.h> |
24 | 24 |
#include<lemon/connectivity.h> |
25 | 25 |
#include <list> |
26 | 26 |
|
27 | 27 |
/// \ingroup graph_prop |
28 | 28 |
/// \file |
29 | 29 |
/// \brief Euler tour |
30 | 30 |
/// |
31 | 31 |
///This file provides an Euler tour iterator and ways to check |
32 | 32 |
///if a digraph is euler. |
33 | 33 |
|
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
///Euler iterator for digraphs. |
38 | 38 |
|
39 | 39 |
/// \ingroup graph_prop |
40 | 40 |
///This iterator converts to the \c Arc type of the digraph and using |
41 | 41 |
///operator ++, it provides an Euler tour of a \e directed |
42 | 42 |
///graph (if there exists). |
43 | 43 |
/// |
44 | 44 |
///For example |
45 | 45 |
///if the given digraph is Euler (i.e it has only one nontrivial component |
46 | 46 |
///and the in-degree is equal to the out-degree for all nodes), |
47 | 47 |
///the following code will put the arcs of \c g |
48 | 48 |
///to the vector \c et according to an |
49 | 49 |
///Euler tour of \c g. |
50 | 50 |
///\code |
51 | 51 |
/// std::vector<ListDigraph::Arc> et; |
52 | 52 |
/// for(DiEulerIt<ListDigraph> e(g),e!=INVALID;++e) |
53 | 53 |
/// et.push_back(e); |
54 | 54 |
///\endcode |
55 | 55 |
///If \c g is not Euler then the resulted tour will not be full or closed. |
56 | 56 |
///\sa EulerIt |
57 |
template< |
|
57 |
template<typename GR> |
|
58 | 58 |
class DiEulerIt |
59 | 59 |
{ |
60 |
typedef typename Digraph::Node Node; |
|
61 |
typedef typename Digraph::NodeIt NodeIt; |
|
62 |
typedef typename Digraph::Arc Arc; |
|
63 |
typedef typename Digraph::ArcIt ArcIt; |
|
64 |
typedef typename Digraph::OutArcIt OutArcIt; |
|
65 |
typedef typename Digraph::InArcIt InArcIt; |
|
60 |
typedef typename GR::Node Node; |
|
61 |
typedef typename GR::NodeIt NodeIt; |
|
62 |
typedef typename GR::Arc Arc; |
|
63 |
typedef typename GR::ArcIt ArcIt; |
|
64 |
typedef typename GR::OutArcIt OutArcIt; |
|
65 |
typedef typename GR::InArcIt InArcIt; |
|
66 | 66 |
|
67 |
const Digraph &g; |
|
68 |
typename Digraph::template NodeMap<OutArcIt> nedge; |
|
67 |
const GR &g; |
|
68 |
typename GR::template NodeMap<OutArcIt> nedge; |
|
69 | 69 |
std::list<Arc> euler; |
70 | 70 |
|
71 | 71 |
public: |
72 | 72 |
|
73 | 73 |
///Constructor |
74 | 74 |
|
75 |
///\param |
|
75 |
///\param gr A digraph. |
|
76 | 76 |
///\param start The starting point of the tour. If it is not given |
77 | 77 |
/// the tour will start from the first node. |
78 |
DiEulerIt(const Digraph &_g,typename Digraph::Node start=INVALID) |
|
79 |
: g(_g), nedge(g) |
|
78 |
DiEulerIt(const GR &gr, typename GR::Node start = INVALID) |
|
79 |
: g(gr), nedge(g) |
|
80 | 80 |
{ |
81 | 81 |
if(start==INVALID) start=NodeIt(g); |
82 | 82 |
for(NodeIt n(g);n!=INVALID;++n) nedge[n]=OutArcIt(g,n); |
83 | 83 |
while(nedge[start]!=INVALID) { |
84 | 84 |
euler.push_back(nedge[start]); |
85 | 85 |
Node next=g.target(nedge[start]); |
86 | 86 |
++nedge[start]; |
87 | 87 |
start=next; |
88 | 88 |
} |
89 | 89 |
} |
90 | 90 |
|
91 | 91 |
///Arc Conversion |
92 | 92 |
operator Arc() { return euler.empty()?INVALID:euler.front(); } |
93 | 93 |
bool operator==(Invalid) { return euler.empty(); } |
94 | 94 |
bool operator!=(Invalid) { return !euler.empty(); } |
95 | 95 |
|
96 | 96 |
///Next arc of the tour |
97 | 97 |
DiEulerIt &operator++() { |
98 | 98 |
Node s=g.target(euler.front()); |
99 | 99 |
euler.pop_front(); |
100 | 100 |
//This produces a warning.Strange. |
101 | 101 |
//std::list<Arc>::iterator next=euler.begin(); |
102 | 102 |
typename std::list<Arc>::iterator next=euler.begin(); |
103 | 103 |
while(nedge[s]!=INVALID) { |
104 | 104 |
euler.insert(next,nedge[s]); |
105 | 105 |
Node n=g.target(nedge[s]); |
106 | 106 |
++nedge[s]; |
107 | 107 |
s=n; |
108 | 108 |
} |
109 | 109 |
return *this; |
110 | 110 |
} |
111 | 111 |
///Postfix incrementation |
112 | 112 |
|
113 | 113 |
///\warning This incrementation |
114 | 114 |
///returns an \c Arc, not an \ref DiEulerIt, as one may |
115 | 115 |
///expect. |
116 | 116 |
Arc operator++(int) |
117 | 117 |
{ |
118 | 118 |
Arc e=*this; |
119 | 119 |
++(*this); |
120 | 120 |
return e; |
121 | 121 |
} |
122 | 122 |
}; |
123 | 123 |
|
124 | 124 |
///Euler iterator for graphs. |
125 | 125 |
|
126 | 126 |
/// \ingroup graph_prop |
127 | 127 |
///This iterator converts to the \c Arc (or \c Edge) |
128 | 128 |
///type of the digraph and using |
129 | 129 |
///operator ++, it provides an Euler tour of an undirected |
130 | 130 |
///digraph (if there exists). |
131 | 131 |
/// |
132 | 132 |
///For example |
133 | 133 |
///if the given digraph if Euler (i.e it has only one nontrivial component |
134 | 134 |
///and the degree of each node is even), |
135 | 135 |
///the following code will print the arc IDs according to an |
136 | 136 |
///Euler tour of \c g. |
137 | 137 |
///\code |
138 | 138 |
/// for(EulerIt<ListGraph> e(g),e!=INVALID;++e) { |
139 | 139 |
/// std::cout << g.id(Edge(e)) << std::eol; |
140 | 140 |
/// } |
141 | 141 |
///\endcode |
142 | 142 |
///Although the iterator provides an Euler tour of an graph, |
143 | 143 |
///it still returns Arcs in order to indicate the direction of the tour. |
144 | 144 |
///(But Arc will convert to Edges, of course). |
145 | 145 |
/// |
146 | 146 |
///If \c g is not Euler then the resulted tour will not be full or closed. |
147 | 147 |
///\sa EulerIt |
148 |
template< |
|
148 |
template<typename GR> |
|
149 | 149 |
class EulerIt |
150 | 150 |
{ |
151 |
typedef typename Digraph::Node Node; |
|
152 |
typedef typename Digraph::NodeIt NodeIt; |
|
153 |
typedef typename Digraph::Arc Arc; |
|
154 |
typedef typename Digraph::Edge Edge; |
|
155 |
typedef typename Digraph::ArcIt ArcIt; |
|
156 |
typedef typename Digraph::OutArcIt OutArcIt; |
|
157 |
typedef typename |
|
151 |
typedef typename GR::Node Node; |
|
152 |
typedef typename GR::NodeIt NodeIt; |
|
153 |
typedef typename GR::Arc Arc; |
|
154 |
typedef typename GR::Edge Edge; |
|
155 |
typedef typename GR::ArcIt ArcIt; |
|
156 |
typedef typename GR::OutArcIt OutArcIt; |
|
157 |
typedef typename GR::InArcIt InArcIt; |
|
158 | 158 |
|
159 |
const Digraph &g; |
|
160 |
typename Digraph::template NodeMap<OutArcIt> nedge; |
|
161 |
|
|
159 |
const GR &g; |
|
160 |
typename GR::template NodeMap<OutArcIt> nedge; |
|
161 |
typename GR::template EdgeMap<bool> visited; |
|
162 | 162 |
std::list<Arc> euler; |
163 | 163 |
|
164 | 164 |
public: |
165 | 165 |
|
166 | 166 |
///Constructor |
167 | 167 |
|
168 |
///\param |
|
168 |
///\param gr An graph. |
|
169 | 169 |
///\param start The starting point of the tour. If it is not given |
170 | 170 |
/// the tour will start from the first node. |
171 |
EulerIt(const Digraph &_g,typename Digraph::Node start=INVALID) |
|
172 |
: g(_g), nedge(g), visited(g,false) |
|
171 |
EulerIt(const GR &gr, typename GR::Node start = INVALID) |
|
172 |
: g(gr), nedge(g), visited(g, false) |
|
173 | 173 |
{ |
174 | 174 |
if(start==INVALID) start=NodeIt(g); |
175 | 175 |
for(NodeIt n(g);n!=INVALID;++n) nedge[n]=OutArcIt(g,n); |
176 | 176 |
while(nedge[start]!=INVALID) { |
177 | 177 |
euler.push_back(nedge[start]); |
178 | 178 |
visited[nedge[start]]=true; |
179 | 179 |
Node next=g.target(nedge[start]); |
180 | 180 |
++nedge[start]; |
181 | 181 |
start=next; |
182 | 182 |
while(nedge[start]!=INVALID && visited[nedge[start]]) ++nedge[start]; |
183 | 183 |
} |
184 | 184 |
} |
185 | 185 |
|
186 | 186 |
///Arc Conversion |
187 | 187 |
operator Arc() const { return euler.empty()?INVALID:euler.front(); } |
188 | 188 |
///Arc Conversion |
189 | 189 |
operator Edge() const { return euler.empty()?INVALID:euler.front(); } |
190 | 190 |
///\e |
191 | 191 |
bool operator==(Invalid) const { return euler.empty(); } |
192 | 192 |
///\e |
193 | 193 |
bool operator!=(Invalid) const { return !euler.empty(); } |
194 | 194 |
|
195 | 195 |
///Next arc of the tour |
196 | 196 |
EulerIt &operator++() { |
197 | 197 |
Node s=g.target(euler.front()); |
198 | 198 |
euler.pop_front(); |
199 | 199 |
typename std::list<Arc>::iterator next=euler.begin(); |
200 | 200 |
|
201 | 201 |
while(nedge[s]!=INVALID) { |
202 | 202 |
while(nedge[s]!=INVALID && visited[nedge[s]]) ++nedge[s]; |
203 | 203 |
if(nedge[s]==INVALID) break; |
204 | 204 |
else { |
205 | 205 |
euler.insert(next,nedge[s]); |
206 | 206 |
visited[nedge[s]]=true; |
207 | 207 |
Node n=g.target(nedge[s]); |
208 | 208 |
++nedge[s]; |
209 | 209 |
s=n; |
210 | 210 |
} |
211 | 211 |
} |
212 | 212 |
return *this; |
213 | 213 |
} |
214 | 214 |
|
215 | 215 |
///Postfix incrementation |
216 | 216 |
|
217 | 217 |
///\warning This incrementation |
218 | 218 |
///returns an \c Arc, not an \ref EulerIt, as one may |
219 | 219 |
///expect. |
220 | 220 |
Arc operator++(int) |
221 | 221 |
{ |
222 | 222 |
Arc e=*this; |
223 | 223 |
++(*this); |
224 | 224 |
return e; |
225 | 225 |
} |
226 | 226 |
}; |
227 | 227 |
|
228 | 228 |
|
229 | 229 |
///Checks if the graph is Eulerian |
230 | 230 |
|
231 | 231 |
/// \ingroup graph_prop |
232 | 232 |
///Checks if the graph is Eulerian. It works for both directed and undirected |
233 | 233 |
///graphs. |
234 | 234 |
///\note By definition, a digraph is called \e Eulerian if |
235 | 235 |
///and only if it is connected and the number of its incoming and outgoing |
236 | 236 |
///arcs are the same for each node. |
237 | 237 |
///Similarly, an undirected graph is called \e Eulerian if |
238 | 238 |
///and only if it is connected and the number of incident arcs is even |
239 | 239 |
///for each node. <em>Therefore, there are digraphs which are not Eulerian, |
240 | 240 |
///but still have an Euler tour</em>. |
241 |
template< |
|
241 |
template<typename GR> |
|
242 | 242 |
#ifdef DOXYGEN |
243 | 243 |
bool |
244 | 244 |
#else |
245 |
typename enable_if<UndirectedTagIndicator<Digraph>,bool>::type |
|
246 |
eulerian(const Digraph &g) |
|
245 |
typename enable_if<UndirectedTagIndicator<GR>,bool>::type |
|
246 |
eulerian(const GR &g) |
|
247 | 247 |
{ |
248 |
for(typename |
|
248 |
for(typename GR::NodeIt n(g);n!=INVALID;++n) |
|
249 | 249 |
if(countIncEdges(g,n)%2) return false; |
250 | 250 |
return connected(g); |
251 | 251 |
} |
252 |
template<class Digraph> |
|
253 |
typename disable_if<UndirectedTagIndicator<Digraph>,bool>::type |
|
252 |
template<class GR> |
|
253 |
typename disable_if<UndirectedTagIndicator<GR>,bool>::type |
|
254 | 254 |
#endif |
255 |
eulerian(const |
|
255 |
eulerian(const GR &g) |
|
256 | 256 |
{ |
257 |
for(typename |
|
257 |
for(typename GR::NodeIt n(g);n!=INVALID;++n) |
|
258 | 258 |
if(countInArcs(g,n)!=countOutArcs(g,n)) return false; |
259 |
return connected(Undirector<const |
|
259 |
return connected(Undirector<const GR>(g)); |
|
260 | 260 |
} |
261 | 261 |
|
262 | 262 |
} |
263 | 263 |
|
264 | 264 |
#endif |
... | ... |
@@ -19,192 +19,191 @@ |
19 | 19 |
#ifndef LEMON_GRAPH_TO_EPS_H |
20 | 20 |
#define LEMON_GRAPH_TO_EPS_H |
21 | 21 |
|
22 | 22 |
#include<iostream> |
23 | 23 |
#include<fstream> |
24 | 24 |
#include<sstream> |
25 | 25 |
#include<algorithm> |
26 | 26 |
#include<vector> |
27 | 27 |
|
28 | 28 |
#ifndef WIN32 |
29 | 29 |
#include<sys/time.h> |
30 | 30 |
#include<ctime> |
31 | 31 |
#else |
32 | 32 |
#include<lemon/bits/windows.h> |
33 | 33 |
#endif |
34 | 34 |
|
35 | 35 |
#include<lemon/math.h> |
36 | 36 |
#include<lemon/core.h> |
37 | 37 |
#include<lemon/dim2.h> |
38 | 38 |
#include<lemon/maps.h> |
39 | 39 |
#include<lemon/color.h> |
40 | 40 |
#include<lemon/bits/bezier.h> |
41 | 41 |
#include<lemon/error.h> |
42 | 42 |
|
43 | 43 |
|
44 | 44 |
///\ingroup eps_io |
45 | 45 |
///\file |
46 | 46 |
///\brief A well configurable tool for visualizing graphs |
47 | 47 |
|
48 | 48 |
namespace lemon { |
49 | 49 |
|
50 | 50 |
namespace _graph_to_eps_bits { |
51 | 51 |
template<class MT> |
52 | 52 |
class _NegY { |
53 | 53 |
public: |
54 | 54 |
typedef typename MT::Key Key; |
55 | 55 |
typedef typename MT::Value Value; |
56 | 56 |
const MT ↦ |
57 | 57 |
int yscale; |
58 | 58 |
_NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {} |
59 | 59 |
Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);} |
60 | 60 |
}; |
61 | 61 |
} |
62 | 62 |
|
63 | 63 |
///Default traits class of GraphToEps |
64 | 64 |
|
65 | 65 |
///Default traits class of \ref GraphToEps. |
66 | 66 |
/// |
67 |
///\c G is the type of the underlying graph. |
|
68 |
template<class G> |
|
67 |
///\param GR is the type of the underlying graph. |
|
68 |
template<class GR> |
|
69 | 69 |
struct DefaultGraphToEpsTraits |
70 | 70 |
{ |
71 |
typedef |
|
71 |
typedef GR Graph; |
|
72 | 72 |
typedef typename Graph::Node Node; |
73 | 73 |
typedef typename Graph::NodeIt NodeIt; |
74 | 74 |
typedef typename Graph::Arc Arc; |
75 | 75 |
typedef typename Graph::ArcIt ArcIt; |
76 | 76 |
typedef typename Graph::InArcIt InArcIt; |
77 | 77 |
typedef typename Graph::OutArcIt OutArcIt; |
78 | 78 |
|
79 | 79 |
|
80 | 80 |
const Graph &g; |
81 | 81 |
|
82 | 82 |
std::ostream& os; |
83 | 83 |
|
84 | 84 |
typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType; |
85 | 85 |
CoordsMapType _coords; |
86 | 86 |
ConstMap<typename Graph::Node,double > _nodeSizes; |
87 | 87 |
ConstMap<typename Graph::Node,int > _nodeShapes; |
88 | 88 |
|
89 | 89 |
ConstMap<typename Graph::Node,Color > _nodeColors; |
90 | 90 |
ConstMap<typename Graph::Arc,Color > _arcColors; |
91 | 91 |
|
92 | 92 |
ConstMap<typename Graph::Arc,double > _arcWidths; |
93 | 93 |
|
94 | 94 |
double _arcWidthScale; |
95 | 95 |
|
96 | 96 |
double _nodeScale; |
97 | 97 |
double _xBorder, _yBorder; |
98 | 98 |
double _scale; |
99 | 99 |
double _nodeBorderQuotient; |
100 | 100 |
|
101 | 101 |
bool _drawArrows; |
102 | 102 |
double _arrowLength, _arrowWidth; |
103 | 103 |
|
104 | 104 |
bool _showNodes, _showArcs; |
105 | 105 |
|
106 | 106 |
bool _enableParallel; |
107 | 107 |
double _parArcDist; |
108 | 108 |
|
109 | 109 |
bool _showNodeText; |
110 | 110 |
ConstMap<typename Graph::Node,bool > _nodeTexts; |
111 | 111 |
double _nodeTextSize; |
112 | 112 |
|
113 | 113 |
bool _showNodePsText; |
114 | 114 |
ConstMap<typename Graph::Node,bool > _nodePsTexts; |
115 | 115 |
char *_nodePsTextsPreamble; |
116 | 116 |
|
117 | 117 |
bool _undirected; |
118 | 118 |
|
119 | 119 |
bool _pleaseRemoveOsStream; |
120 | 120 |
|
121 | 121 |
bool _scaleToA4; |
122 | 122 |
|
123 | 123 |
std::string _title; |
124 | 124 |
std::string _copyright; |
125 | 125 |
|
126 | 126 |
enum NodeTextColorType |
127 | 127 |
{ DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType; |
128 | 128 |
ConstMap<typename Graph::Node,Color > _nodeTextColors; |
129 | 129 |
|
130 | 130 |
bool _autoNodeScale; |
131 | 131 |
bool _autoArcWidthScale; |
132 | 132 |
|
133 | 133 |
bool _absoluteNodeSizes; |
134 | 134 |
bool _absoluteArcWidths; |
135 | 135 |
|
136 | 136 |
bool _negY; |
137 | 137 |
|
138 | 138 |
bool _preScale; |
139 | 139 |
///Constructor |
140 | 140 |
|
141 | 141 |
///Constructor |
142 |
///\param _g Reference to the graph to be printed. |
|
143 |
///\param _os Reference to the output stream. |
|
144 |
///\param |
|
142 |
///\param gr Reference to the graph to be printed. |
|
143 |
///\param ost Reference to the output stream. |
|
145 | 144 |
///By default it is <tt>std::cout</tt>. |
146 |
///\param |
|
145 |
///\param pros If it is \c true, then the \c ostream referenced by \c os |
|
147 | 146 |
///will be explicitly deallocated by the destructor. |
148 |
DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout, |
|
149 |
bool _pros=false) : |
|
150 |
|
|
147 |
DefaultGraphToEpsTraits(const GR &gr, std::ostream& ost = std::cout, |
|
148 |
bool pros = false) : |
|
149 |
g(gr), os(ost), |
|
151 | 150 |
_coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0), |
152 | 151 |
_nodeColors(WHITE), _arcColors(BLACK), |
153 | 152 |
_arcWidths(1.0), _arcWidthScale(0.003), |
154 | 153 |
_nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0), |
155 | 154 |
_nodeBorderQuotient(.1), |
156 | 155 |
_drawArrows(false), _arrowLength(1), _arrowWidth(0.3), |
157 | 156 |
_showNodes(true), _showArcs(true), |
158 | 157 |
_enableParallel(false), _parArcDist(1), |
159 | 158 |
_showNodeText(false), _nodeTexts(false), _nodeTextSize(1), |
160 | 159 |
_showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0), |
161 |
_undirected(lemon::UndirectedTagIndicator<G>::value), |
|
162 |
_pleaseRemoveOsStream(_pros), _scaleToA4(false), |
|
160 |
_undirected(lemon::UndirectedTagIndicator<GR>::value), |
|
161 |
_pleaseRemoveOsStream(pros), _scaleToA4(false), |
|
163 | 162 |
_nodeTextColorType(SAME_COL), _nodeTextColors(BLACK), |
164 | 163 |
_autoNodeScale(false), |
165 | 164 |
_autoArcWidthScale(false), |
166 | 165 |
_absoluteNodeSizes(false), |
167 | 166 |
_absoluteArcWidths(false), |
168 | 167 |
_negY(false), |
169 | 168 |
_preScale(true) |
170 | 169 |
{} |
171 | 170 |
}; |
172 | 171 |
|
173 | 172 |
///Auxiliary class to implement the named parameters of \ref graphToEps() |
174 | 173 |
|
175 | 174 |
///Auxiliary class to implement the named parameters of \ref graphToEps(). |
176 | 175 |
/// |
177 | 176 |
///For detailed examples see the \ref graph_to_eps_demo.cc demo file. |
178 | 177 |
template<class T> class GraphToEps : public T |
179 | 178 |
{ |
180 | 179 |
// Can't believe it is required by the C++ standard |
181 | 180 |
using T::g; |
182 | 181 |
using T::os; |
183 | 182 |
|
184 | 183 |
using T::_coords; |
185 | 184 |
using T::_nodeSizes; |
186 | 185 |
using T::_nodeShapes; |
187 | 186 |
using T::_nodeColors; |
188 | 187 |
using T::_arcColors; |
189 | 188 |
using T::_arcWidths; |
190 | 189 |
|
191 | 190 |
using T::_arcWidthScale; |
192 | 191 |
using T::_nodeScale; |
193 | 192 |
using T::_xBorder; |
194 | 193 |
using T::_yBorder; |
195 | 194 |
using T::_scale; |
196 | 195 |
using T::_nodeBorderQuotient; |
197 | 196 |
|
198 | 197 |
using T::_drawArrows; |
199 | 198 |
using T::_arrowLength; |
200 | 199 |
using T::_arrowWidth; |
201 | 200 |
|
202 | 201 |
using T::_showNodes; |
203 | 202 |
using T::_showArcs; |
204 | 203 |
|
205 | 204 |
using T::_enableParallel; |
206 | 205 |
using T::_parArcDist; |
207 | 206 |
|
208 | 207 |
using T::_showNodeText; |
209 | 208 |
using T::_nodeTexts; |
210 | 209 |
using T::_nodeTextSize; |
... | ... |
@@ -1089,102 +1088,102 @@ |
1089 | 1088 |
///An alias for absoluteArcWidths() |
1090 | 1089 |
GraphToEps<T> &absoluteEdgeWidths(bool b=true) |
1091 | 1090 |
{ |
1092 | 1091 |
return absoluteArcWidths(b); |
1093 | 1092 |
} |
1094 | 1093 |
|
1095 | 1094 |
///An alias for parArcDist() |
1096 | 1095 |
GraphToEps<T> &parEdgeDist(double d) {return parArcDist(d);} |
1097 | 1096 |
|
1098 | 1097 |
///An alias for hideArcs() |
1099 | 1098 |
GraphToEps<T> &hideEdges(bool b=true) {return hideArcs(b);} |
1100 | 1099 |
|
1101 | 1100 |
///@} |
1102 | 1101 |
}; |
1103 | 1102 |
|
1104 | 1103 |
template<class T> |
1105 | 1104 |
const int GraphToEps<T>::INTERPOL_PREC = 20; |
1106 | 1105 |
template<class T> |
1107 | 1106 |
const double GraphToEps<T>::A4HEIGHT = 841.8897637795276; |
1108 | 1107 |
template<class T> |
1109 | 1108 |
const double GraphToEps<T>::A4WIDTH = 595.275590551181; |
1110 | 1109 |
template<class T> |
1111 | 1110 |
const double GraphToEps<T>::A4BORDER = 15; |
1112 | 1111 |
|
1113 | 1112 |
|
1114 | 1113 |
///Generates an EPS file from a graph |
1115 | 1114 |
|
1116 | 1115 |
///\ingroup eps_io |
1117 | 1116 |
///Generates an EPS file from a graph. |
1118 | 1117 |
///\param g Reference to the graph to be printed. |
1119 | 1118 |
///\param os Reference to the output stream. |
1120 | 1119 |
///By default it is <tt>std::cout</tt>. |
1121 | 1120 |
/// |
1122 | 1121 |
///This function also has a lot of |
1123 | 1122 |
///\ref named-templ-func-param "named parameters", |
1124 | 1123 |
///they are declared as the members of class \ref GraphToEps. The following |
1125 | 1124 |
///example shows how to use these parameters. |
1126 | 1125 |
///\code |
1127 | 1126 |
/// graphToEps(g,os).scale(10).coords(coords) |
1128 | 1127 |
/// .nodeScale(2).nodeSizes(sizes) |
1129 | 1128 |
/// .arcWidthScale(.4).run(); |
1130 | 1129 |
///\endcode |
1131 | 1130 |
/// |
1132 | 1131 |
///For more detailed examples see the \ref graph_to_eps_demo.cc demo file. |
1133 | 1132 |
/// |
1134 | 1133 |
///\warning Don't forget to put the \ref GraphToEps::run() "run()" |
1135 | 1134 |
///to the end of the parameter list. |
1136 | 1135 |
///\sa GraphToEps |
1137 |
///\sa graphToEps(G &g, const char *file_name) |
|
1138 |
template<class G> |
|
1139 |
GraphToEps<DefaultGraphToEpsTraits<G> > |
|
1140 |
graphToEps(G &g, std::ostream& os=std::cout) |
|
1136 |
///\sa graphToEps(GR &g, const char *file_name) |
|
1137 |
template<class GR> |
|
1138 |
GraphToEps<DefaultGraphToEpsTraits<GR> > |
|
1139 |
graphToEps(GR &g, std::ostream& os=std::cout) |
|
1141 | 1140 |
{ |
1142 | 1141 |
return |
1143 |
GraphToEps<DefaultGraphToEpsTraits< |
|
1142 |
GraphToEps<DefaultGraphToEpsTraits<GR> >(DefaultGraphToEpsTraits<GR>(g,os)); |
|
1144 | 1143 |
} |
1145 | 1144 |
|
1146 | 1145 |
///Generates an EPS file from a graph |
1147 | 1146 |
|
1148 | 1147 |
///\ingroup eps_io |
1149 | 1148 |
///This function does the same as |
1150 |
///\ref graphToEps( |
|
1149 |
///\ref graphToEps(GR &g,std::ostream& os) |
|
1151 | 1150 |
///but it writes its output into the file \c file_name |
1152 | 1151 |
///instead of a stream. |
1153 |
///\sa graphToEps(G &g, std::ostream& os) |
|
1154 |
template<class G> |
|
1155 |
GraphToEps<DefaultGraphToEpsTraits<G> > |
|
1156 |
graphToEps(G &g,const char *file_name) |
|
1152 |
///\sa graphToEps(GR &g, std::ostream& os) |
|
1153 |
template<class GR> |
|
1154 |
GraphToEps<DefaultGraphToEpsTraits<GR> > |
|
1155 |
graphToEps(GR &g,const char *file_name) |
|
1157 | 1156 |
{ |
1158 | 1157 |
std::ostream* os = new std::ofstream(file_name); |
1159 | 1158 |
if (!(*os)) { |
1160 | 1159 |
delete os; |
1161 | 1160 |
throw IoError("Cannot write file", file_name); |
1162 | 1161 |
} |
1163 |
return GraphToEps<DefaultGraphToEpsTraits<G> > |
|
1164 |
(DefaultGraphToEpsTraits<G>(g,*os,true)); |
|
1162 |
return GraphToEps<DefaultGraphToEpsTraits<GR> > |
|
1163 |
(DefaultGraphToEpsTraits<GR>(g,*os,true)); |
|
1165 | 1164 |
} |
1166 | 1165 |
|
1167 | 1166 |
///Generates an EPS file from a graph |
1168 | 1167 |
|
1169 | 1168 |
///\ingroup eps_io |
1170 | 1169 |
///This function does the same as |
1171 |
///\ref graphToEps( |
|
1170 |
///\ref graphToEps(GR &g,std::ostream& os) |
|
1172 | 1171 |
///but it writes its output into the file \c file_name |
1173 | 1172 |
///instead of a stream. |
1174 |
///\sa graphToEps(G &g, std::ostream& os) |
|
1175 |
template<class G> |
|
1176 |
GraphToEps<DefaultGraphToEpsTraits<G> > |
|
1177 |
graphToEps(G &g,const std::string& file_name) |
|
1173 |
///\sa graphToEps(GR &g, std::ostream& os) |
|
1174 |
template<class GR> |
|
1175 |
GraphToEps<DefaultGraphToEpsTraits<GR> > |
|
1176 |
graphToEps(GR &g,const std::string& file_name) |
|
1178 | 1177 |
{ |
1179 | 1178 |
std::ostream* os = new std::ofstream(file_name.c_str()); |
1180 | 1179 |
if (!(*os)) { |
1181 | 1180 |
delete os; |
1182 | 1181 |
throw IoError("Cannot write file", file_name); |
1183 | 1182 |
} |
1184 |
return GraphToEps<DefaultGraphToEpsTraits<G> > |
|
1185 |
(DefaultGraphToEpsTraits<G>(g,*os,true)); |
|
1183 |
return GraphToEps<DefaultGraphToEpsTraits<GR> > |
|
1184 |
(DefaultGraphToEpsTraits<GR>(g,*os,true)); |
|
1186 | 1185 |
} |
1187 | 1186 |
|
1188 | 1187 |
} //END OF NAMESPACE LEMON |
1189 | 1188 |
|
1190 | 1189 |
#endif // LEMON_GRAPH_TO_EPS_H |
... | ... |
@@ -451,97 +451,97 @@ |
451 | 451 |
|
452 | 452 |
Arc down(Node n) const { |
453 | 453 |
if (n._id >= _width) { |
454 | 454 |
return Arc((n._id - _width) << 1); |
455 | 455 |
} else { |
456 | 456 |
return INVALID; |
457 | 457 |
} |
458 | 458 |
} |
459 | 459 |
|
460 | 460 |
private: |
461 | 461 |
int _width, _height; |
462 | 462 |
int _node_num, _edge_num; |
463 | 463 |
int _edge_limit; |
464 | 464 |
}; |
465 | 465 |
|
466 | 466 |
|
467 | 467 |
typedef GraphExtender<GridGraphBase> ExtendedGridGraphBase; |
468 | 468 |
|
469 | 469 |
/// \ingroup graphs |
470 | 470 |
/// |
471 | 471 |
/// \brief Grid graph class |
472 | 472 |
/// |
473 | 473 |
/// This class implements a special graph type. The nodes of the |
474 | 474 |
/// graph can be indexed by two integer \c (i,j) value where \c i is |
475 | 475 |
/// in the \c [0..width()-1] range and j is in the \c |
476 | 476 |
/// [0..height()-1] range. Two nodes are connected in the graph if |
477 | 477 |
/// the indexes differ exactly on one position and exactly one is |
478 | 478 |
/// the difference. The nodes of the graph can be indexed by position |
479 | 479 |
/// with the \c operator()() function. The positions of the nodes can be |
480 | 480 |
/// get with \c pos(), \c col() and \c row() members. The outgoing |
481 | 481 |
/// arcs can be retrieved with the \c right(), \c up(), \c left() |
482 | 482 |
/// and \c down() functions, where the bottom-left corner is the |
483 | 483 |
/// origin. |
484 | 484 |
/// |
485 | 485 |
/// \image html grid_graph.png |
486 | 486 |
/// \image latex grid_graph.eps "Grid graph" width=\textwidth |
487 | 487 |
/// |
488 | 488 |
/// A short example about the basic usage: |
489 | 489 |
///\code |
490 | 490 |
/// GridGraph graph(rows, cols); |
491 | 491 |
/// GridGraph::NodeMap<int> val(graph); |
492 | 492 |
/// for (int i = 0; i < graph.width(); ++i) { |
493 | 493 |
/// for (int j = 0; j < graph.height(); ++j) { |
494 | 494 |
/// val[graph(i, j)] = i + j; |
495 | 495 |
/// } |
496 | 496 |
/// } |
497 | 497 |
///\endcode |
498 | 498 |
/// |
499 |
/// This graph type |
|
499 |
/// This graph type fully conforms to the \ref concepts::Graph |
|
500 | 500 |
/// "Graph" concept, and it also has an important extra feature |
501 | 501 |
/// that its maps are real \ref concepts::ReferenceMap |
502 | 502 |
/// "reference map"s. |
503 | 503 |
class GridGraph : public ExtendedGridGraphBase { |
504 | 504 |
public: |
505 | 505 |
|
506 | 506 |
typedef ExtendedGridGraphBase Parent; |
507 | 507 |
|
508 | 508 |
/// \brief Map to get the indices of the nodes as dim2::Point<int>. |
509 | 509 |
/// |
510 | 510 |
/// Map to get the indices of the nodes as dim2::Point<int>. |
511 | 511 |
class IndexMap { |
512 | 512 |
public: |
513 | 513 |
/// \brief The key type of the map |
514 | 514 |
typedef GridGraph::Node Key; |
515 | 515 |
/// \brief The value type of the map |
516 | 516 |
typedef dim2::Point<int> Value; |
517 | 517 |
|
518 | 518 |
/// \brief Constructor |
519 | 519 |
/// |
520 | 520 |
/// Constructor |
521 | 521 |
IndexMap(const GridGraph& graph) : _graph(graph) {} |
522 | 522 |
|
523 | 523 |
/// \brief The subscript operator |
524 | 524 |
/// |
525 | 525 |
/// The subscript operator. |
526 | 526 |
Value operator[](Key key) const { |
527 | 527 |
return _graph.pos(key); |
528 | 528 |
} |
529 | 529 |
|
530 | 530 |
private: |
531 | 531 |
const GridGraph& _graph; |
532 | 532 |
}; |
533 | 533 |
|
534 | 534 |
/// \brief Map to get the column of the nodes. |
535 | 535 |
/// |
536 | 536 |
/// Map to get the column of the nodes. |
537 | 537 |
class ColMap { |
538 | 538 |
public: |
539 | 539 |
/// \brief The key type of the map |
540 | 540 |
typedef GridGraph::Node Key; |
541 | 541 |
/// \brief The value type of the map |
542 | 542 |
typedef int Value; |
543 | 543 |
|
544 | 544 |
/// \brief Constructor |
545 | 545 |
/// |
546 | 546 |
/// Constructor |
547 | 547 |
ColMap(const GridGraph& graph) : _graph(graph) {} |
... | ... |
@@ -12,117 +12,117 @@ |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_HAO_ORLIN_H |
20 | 20 |
#define LEMON_HAO_ORLIN_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <list> |
24 | 24 |
#include <limits> |
25 | 25 |
|
26 | 26 |
#include <lemon/maps.h> |
27 | 27 |
#include <lemon/core.h> |
28 | 28 |
#include <lemon/tolerance.h> |
29 | 29 |
|
30 | 30 |
/// \file |
31 | 31 |
/// \ingroup min_cut |
32 | 32 |
/// \brief Implementation of the Hao-Orlin algorithm. |
33 | 33 |
/// |
34 | 34 |
/// Implementation of the Hao-Orlin algorithm class for testing network |
35 | 35 |
/// reliability. |
36 | 36 |
|
37 | 37 |
namespace lemon { |
38 | 38 |
|
39 | 39 |
/// \ingroup min_cut |
40 | 40 |
/// |
41 | 41 |
/// \brief %Hao-Orlin algorithm to find a minimum cut in directed graphs. |
42 | 42 |
/// |
43 | 43 |
/// Hao-Orlin calculates a minimum cut in a directed graph |
44 | 44 |
/// \f$D=(V,A)\f$. It takes a fixed node \f$ source \in V \f$ and |
45 | 45 |
/// consists of two phases: in the first phase it determines a |
46 | 46 |
/// minimum cut with \f$ source \f$ on the source-side (i.e. a set |
47 | 47 |
/// \f$ X\subsetneq V \f$ with \f$ source \in X \f$ and minimal |
48 | 48 |
/// out-degree) and in the second phase it determines a minimum cut |
49 | 49 |
/// with \f$ source \f$ on the sink-side (i.e. a set |
50 | 50 |
/// \f$ X\subsetneq V \f$ with \f$ source \notin X \f$ and minimal |
51 | 51 |
/// out-degree). Obviously, the smaller of these two cuts will be a |
52 | 52 |
/// minimum cut of \f$ D \f$. The algorithm is a modified |
53 | 53 |
/// push-relabel preflow algorithm and our implementation calculates |
54 | 54 |
/// the minimum cut in \f$ O(n^2\sqrt{m}) \f$ time (we use the |
55 | 55 |
/// highest-label rule), or in \f$O(nm)\f$ for unit capacities. The |
56 | 56 |
/// purpose of such algorithm is testing network reliability. For an |
57 | 57 |
/// undirected graph you can run just the first phase of the |
58 | 58 |
/// algorithm or you can use the algorithm of Nagamochi and Ibaraki |
59 | 59 |
/// which solves the undirected problem in |
60 |
/// \f$ O(nm + n^2 \log |
|
60 |
/// \f$ O(nm + n^2 \log n) \f$ time: it is implemented in the |
|
61 | 61 |
/// NagamochiIbaraki algorithm class. |
62 | 62 |
/// |
63 |
/// \param _Digraph is the graph type of the algorithm. |
|
64 |
/// \param _CapacityMap is an edge map of capacities which should |
|
65 |
/// be any numreric type. The default type is _Digraph::ArcMap<int>. |
|
66 |
/// \param _Tolerance is the handler of the inexact computation. The |
|
67 |
/// |
|
63 |
/// \param GR The digraph class the algorithm runs on. |
|
64 |
/// \param CAP An arc map of capacities which can be any numreric type. |
|
65 |
/// The default type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
|
66 |
/// \param TOL Tolerance class for handling inexact computations. The |
|
67 |
/// default tolerance type is \ref Tolerance "Tolerance<CAP::Value>". |
|
68 | 68 |
#ifdef DOXYGEN |
69 |
template <typename |
|
69 |
template <typename GR, typename CAP, typename TOL> |
|
70 | 70 |
#else |
71 |
template <typename _Digraph, |
|
72 |
typename _CapacityMap = typename _Digraph::template ArcMap<int>, |
|
73 |
|
|
71 |
template <typename GR, |
|
72 |
typename CAP = typename GR::template ArcMap<int>, |
|
73 |
typename TOL = Tolerance<typename CAP::Value> > |
|
74 | 74 |
#endif |
75 | 75 |
class HaoOrlin { |
76 | 76 |
private: |
77 | 77 |
|
78 |
typedef _Digraph Digraph; |
|
79 |
typedef _CapacityMap CapacityMap; |
|
80 |
typedef |
|
78 |
typedef GR Digraph; |
|
79 |
typedef CAP CapacityMap; |
|
80 |
typedef TOL Tolerance; |
|
81 | 81 |
|
82 | 82 |
typedef typename CapacityMap::Value Value; |
83 | 83 |
|
84 | 84 |
TEMPLATE_GRAPH_TYPEDEFS(Digraph); |
85 | 85 |
|
86 | 86 |
const Digraph& _graph; |
87 | 87 |
const CapacityMap* _capacity; |
88 | 88 |
|
89 | 89 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
90 | 90 |
FlowMap* _flow; |
91 | 91 |
|
92 | 92 |
Node _source; |
93 | 93 |
|
94 | 94 |
int _node_num; |
95 | 95 |
|
96 | 96 |
// Bucketing structure |
97 | 97 |
std::vector<Node> _first, _last; |
98 | 98 |
typename Digraph::template NodeMap<Node>* _next; |
99 | 99 |
typename Digraph::template NodeMap<Node>* _prev; |
100 | 100 |
typename Digraph::template NodeMap<bool>* _active; |
101 | 101 |
typename Digraph::template NodeMap<int>* _bucket; |
102 | 102 |
|
103 | 103 |
std::vector<bool> _dormant; |
104 | 104 |
|
105 | 105 |
std::list<std::list<int> > _sets; |
106 | 106 |
std::list<int>::iterator _highest; |
107 | 107 |
|
108 | 108 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
109 | 109 |
ExcessMap* _excess; |
110 | 110 |
|
111 | 111 |
typedef typename Digraph::template NodeMap<bool> SourceSetMap; |
112 | 112 |
SourceSetMap* _source_set; |
113 | 113 |
|
114 | 114 |
Value _min_cut; |
115 | 115 |
|
116 | 116 |
typedef typename Digraph::template NodeMap<bool> MinCutMap; |
117 | 117 |
MinCutMap* _min_cut_map; |
118 | 118 |
|
119 | 119 |
Tolerance _tolerance; |
120 | 120 |
|
121 | 121 |
public: |
122 | 122 |
|
123 | 123 |
/// \brief Constructor |
124 | 124 |
/// |
125 | 125 |
/// Constructor of the algorithm class. |
126 | 126 |
HaoOrlin(const Digraph& graph, const CapacityMap& capacity, |
127 | 127 |
const Tolerance& tolerance = Tolerance()) : |
128 | 128 |
_graph(graph), _capacity(&capacity), _flow(0), _source(), |
... | ... |
@@ -772,97 +772,97 @@ |
772 | 772 |
} |
773 | 773 |
} |
774 | 774 |
new_target = _last[_sets.back().back()]; |
775 | 775 |
} |
776 | 776 |
|
777 | 777 |
_bucket->set(target, 0); |
778 | 778 |
|
779 | 779 |
_source_set->set(target, true); |
780 | 780 |
for (InArcIt a(_graph, target); a != INVALID; ++a) { |
781 | 781 |
Value rem = (*_capacity)[a] - (*_flow)[a]; |
782 | 782 |
if (!_tolerance.positive(rem)) continue; |
783 | 783 |
Node v = _graph.source(a); |
784 | 784 |
if (!(*_active)[v] && !(*_source_set)[v]) { |
785 | 785 |
activate(v); |
786 | 786 |
} |
787 | 787 |
_excess->set(v, (*_excess)[v] + rem); |
788 | 788 |
_flow->set(a, (*_capacity)[a]); |
789 | 789 |
} |
790 | 790 |
|
791 | 791 |
for (OutArcIt a(_graph, target); a != INVALID; ++a) { |
792 | 792 |
Value rem = (*_flow)[a]; |
793 | 793 |
if (!_tolerance.positive(rem)) continue; |
794 | 794 |
Node v = _graph.target(a); |
795 | 795 |
if (!(*_active)[v] && !(*_source_set)[v]) { |
796 | 796 |
activate(v); |
797 | 797 |
} |
798 | 798 |
_excess->set(v, (*_excess)[v] + rem); |
799 | 799 |
_flow->set(a, 0); |
800 | 800 |
} |
801 | 801 |
|
802 | 802 |
target = new_target; |
803 | 803 |
if ((*_active)[target]) { |
804 | 804 |
deactivate(target); |
805 | 805 |
} |
806 | 806 |
|
807 | 807 |
_highest = _sets.back().begin(); |
808 | 808 |
while (_highest != _sets.back().end() && |
809 | 809 |
!(*_active)[_first[*_highest]]) { |
810 | 810 |
++_highest; |
811 | 811 |
} |
812 | 812 |
} |
813 | 813 |
} |
814 | 814 |
} |
815 | 815 |
|
816 | 816 |
public: |
817 | 817 |
|
818 | 818 |
/// \name Execution control |
819 | 819 |
/// The simplest way to execute the algorithm is to use |
820 |
/// one of the member functions called \ |
|
820 |
/// one of the member functions called \ref run(). |
|
821 | 821 |
/// \n |
822 | 822 |
/// If you need more control on the execution, |
823 | 823 |
/// first you must call \ref init(), then the \ref calculateIn() or |
824 | 824 |
/// \ref calculateOut() functions. |
825 | 825 |
|
826 | 826 |
/// @{ |
827 | 827 |
|
828 | 828 |
/// \brief Initializes the internal data structures. |
829 | 829 |
/// |
830 | 830 |
/// Initializes the internal data structures. It creates |
831 | 831 |
/// the maps, residual graph adaptors and some bucket structures |
832 | 832 |
/// for the algorithm. |
833 | 833 |
void init() { |
834 | 834 |
init(NodeIt(_graph)); |
835 | 835 |
} |
836 | 836 |
|
837 | 837 |
/// \brief Initializes the internal data structures. |
838 | 838 |
/// |
839 | 839 |
/// Initializes the internal data structures. It creates |
840 | 840 |
/// the maps, residual graph adaptor and some bucket structures |
841 | 841 |
/// for the algorithm. Node \c source is used as the push-relabel |
842 | 842 |
/// algorithm's source. |
843 | 843 |
void init(const Node& source) { |
844 | 844 |
_source = source; |
845 | 845 |
|
846 | 846 |
_node_num = countNodes(_graph); |
847 | 847 |
|
848 | 848 |
_first.resize(_node_num); |
849 | 849 |
_last.resize(_node_num); |
850 | 850 |
|
851 | 851 |
_dormant.resize(_node_num); |
852 | 852 |
|
853 | 853 |
if (!_flow) { |
854 | 854 |
_flow = new FlowMap(_graph); |
855 | 855 |
} |
856 | 856 |
if (!_next) { |
857 | 857 |
_next = new typename Digraph::template NodeMap<Node>(_graph); |
858 | 858 |
} |
859 | 859 |
if (!_prev) { |
860 | 860 |
_prev = new typename Digraph::template NodeMap<Node>(_graph); |
861 | 861 |
} |
862 | 862 |
if (!_active) { |
863 | 863 |
_active = new typename Digraph::template NodeMap<bool>(_graph); |
864 | 864 |
} |
865 | 865 |
if (!_bucket) { |
866 | 866 |
_bucket = new typename Digraph::template NodeMap<int>(_graph); |
867 | 867 |
} |
868 | 868 |
if (!_excess) { |
... | ... |
@@ -246,97 +246,97 @@ |
246 | 246 |
return Arc((edge._id << 1) | (dir ? 1 : 0)); |
247 | 247 |
} |
248 | 248 |
|
249 | 249 |
int dimension() const { |
250 | 250 |
return _dim; |
251 | 251 |
} |
252 | 252 |
|
253 | 253 |
bool projection(Node node, int n) const { |
254 | 254 |
return static_cast<bool>(node._id & (1 << n)); |
255 | 255 |
} |
256 | 256 |
|
257 | 257 |
int dimension(Edge edge) const { |
258 | 258 |
return edge._id >> (_dim-1); |
259 | 259 |
} |
260 | 260 |
|
261 | 261 |
int dimension(Arc arc) const { |
262 | 262 |
return arc._id >> _dim; |
263 | 263 |
} |
264 | 264 |
|
265 | 265 |
int index(Node node) const { |
266 | 266 |
return node._id; |
267 | 267 |
} |
268 | 268 |
|
269 | 269 |
Node operator()(int ix) const { |
270 | 270 |
return Node(ix); |
271 | 271 |
} |
272 | 272 |
|
273 | 273 |
private: |
274 | 274 |
int _dim; |
275 | 275 |
int _node_num, _edge_num; |
276 | 276 |
}; |
277 | 277 |
|
278 | 278 |
|
279 | 279 |
typedef GraphExtender<HypercubeGraphBase> ExtendedHypercubeGraphBase; |
280 | 280 |
|
281 | 281 |
/// \ingroup graphs |
282 | 282 |
/// |
283 | 283 |
/// \brief Hypercube graph class |
284 | 284 |
/// |
285 | 285 |
/// This class implements a special graph type. The nodes of the graph |
286 | 286 |
/// are indiced with integers with at most \c dim binary digits. |
287 | 287 |
/// Two nodes are connected in the graph if and only if their indices |
288 | 288 |
/// differ only on one position in the binary form. |
289 | 289 |
/// |
290 | 290 |
/// \note The type of the indices is chosen to \c int for efficiency |
291 | 291 |
/// reasons. Thus the maximum dimension of this implementation is 26 |
292 | 292 |
/// (assuming that the size of \c int is 32 bit). |
293 | 293 |
/// |
294 |
/// This graph type |
|
294 |
/// This graph type fully conforms to the \ref concepts::Graph |
|
295 | 295 |
/// "Graph" concept, and it also has an important extra feature |
296 | 296 |
/// that its maps are real \ref concepts::ReferenceMap |
297 | 297 |
/// "reference map"s. |
298 | 298 |
class HypercubeGraph : public ExtendedHypercubeGraphBase { |
299 | 299 |
public: |
300 | 300 |
|
301 | 301 |
typedef ExtendedHypercubeGraphBase Parent; |
302 | 302 |
|
303 | 303 |
/// \brief Constructs a hypercube graph with \c dim dimensions. |
304 | 304 |
/// |
305 | 305 |
/// Constructs a hypercube graph with \c dim dimensions. |
306 | 306 |
HypercubeGraph(int dim) { construct(dim); } |
307 | 307 |
|
308 | 308 |
/// \brief The number of dimensions. |
309 | 309 |
/// |
310 | 310 |
/// Gives back the number of dimensions. |
311 | 311 |
int dimension() const { |
312 | 312 |
return Parent::dimension(); |
313 | 313 |
} |
314 | 314 |
|
315 | 315 |
/// \brief Returns \c true if the n'th bit of the node is one. |
316 | 316 |
/// |
317 | 317 |
/// Returns \c true if the n'th bit of the node is one. |
318 | 318 |
bool projection(Node node, int n) const { |
319 | 319 |
return Parent::projection(node, n); |
320 | 320 |
} |
321 | 321 |
|
322 | 322 |
/// \brief The dimension id of an edge. |
323 | 323 |
/// |
324 | 324 |
/// Gives back the dimension id of the given edge. |
325 | 325 |
/// It is in the [0..dim-1] range. |
326 | 326 |
int dimension(Edge edge) const { |
327 | 327 |
return Parent::dimension(edge); |
328 | 328 |
} |
329 | 329 |
|
330 | 330 |
/// \brief The dimension id of an arc. |
331 | 331 |
/// |
332 | 332 |
/// Gives back the dimension id of the given arc. |
333 | 333 |
/// It is in the [0..dim-1] range. |
334 | 334 |
int dimension(Arc arc) const { |
335 | 335 |
return Parent::dimension(arc); |
336 | 336 |
} |
337 | 337 |
|
338 | 338 |
/// \brief The index of a node. |
339 | 339 |
/// |
340 | 340 |
/// Gives back the index of the given node. |
341 | 341 |
/// The lower bits of the integer describes the node. |
342 | 342 |
int index(Node node) const { |
... | ... |
@@ -403,106 +403,106 @@ |
403 | 403 |
/// \brief \ref lgf-format "LGF" reader for directed graphs |
404 | 404 |
/// |
405 | 405 |
/// This utility reads an \ref lgf-format "LGF" file. |
406 | 406 |
/// |
407 | 407 |
/// The reading method does a batch processing. The user creates a |
408 | 408 |
/// reader object, then various reading rules can be added to the |
409 | 409 |
/// reader, and eventually the reading is executed with the \c run() |
410 | 410 |
/// member function. A map reading rule can be added to the reader |
411 | 411 |
/// with the \c nodeMap() or \c arcMap() members. An optional |
412 | 412 |
/// converter parameter can also be added as a standard functor |
413 | 413 |
/// converting from \c std::string to the value type of the map. If it |
414 | 414 |
/// is set, it will determine how the tokens in the file should be |
415 | 415 |
/// converted to the value type of the map. If the functor is not set, |
416 | 416 |
/// then a default conversion will be used. One map can be read into |
417 | 417 |
/// multiple map objects at the same time. The \c attribute(), \c |
418 | 418 |
/// node() and \c arc() functions are used to add attribute reading |
419 | 419 |
/// rules. |
420 | 420 |
/// |
421 | 421 |
///\code |
422 | 422 |
/// DigraphReader<Digraph>(digraph, std::cin). |
423 | 423 |
/// nodeMap("coordinates", coord_map). |
424 | 424 |
/// arcMap("capacity", cap_map). |
425 | 425 |
/// node("source", src). |
426 | 426 |
/// node("target", trg). |
427 | 427 |
/// attribute("caption", caption). |
428 | 428 |
/// run(); |
429 | 429 |
///\endcode |
430 | 430 |
/// |
431 | 431 |
/// By default the reader uses the first section in the file of the |
432 | 432 |
/// proper type. If a section has an optional name, then it can be |
433 | 433 |
/// selected for reading by giving an optional name parameter to the |
434 | 434 |
/// \c nodes(), \c arcs() or \c attributes() functions. |
435 | 435 |
/// |
436 | 436 |
/// The \c useNodes() and \c useArcs() functions are used to tell the reader |
437 | 437 |
/// that the nodes or arcs should not be constructed (added to the |
438 | 438 |
/// graph) during the reading, but instead the label map of the items |
439 | 439 |
/// are given as a parameter of these functions. An |
440 | 440 |
/// application of these functions is multipass reading, which is |
441 | 441 |
/// important if two \c \@arcs sections must be read from the |
442 | 442 |
/// file. In this case the first phase would read the node set and one |
443 | 443 |
/// of the arc sets, while the second phase would read the second arc |
444 | 444 |
/// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet). |
445 | 445 |
/// The previously read label node map should be passed to the \c |
446 | 446 |
/// useNodes() functions. Another application of multipass reading when |
447 | 447 |
/// paths are given as a node map or an arc map. |
448 | 448 |
/// It is impossible to read this in |
449 | 449 |
/// a single pass, because the arcs are not constructed when the node |
450 | 450 |
/// maps are read. |
451 |
template <typename |
|
451 |
template <typename GR> |
|
452 | 452 |
class DigraphReader { |
453 | 453 |
public: |
454 | 454 |
|
455 |
typedef |
|
455 |
typedef GR Digraph; |
|
456 |
|
|
457 |
private: |
|
458 |
|
|
456 | 459 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
457 | 460 |
|
458 |
private: |
|
459 |
|
|
460 |
|
|
461 | 461 |
std::istream* _is; |
462 | 462 |
bool local_is; |
463 | 463 |
std::string _filename; |
464 | 464 |
|
465 | 465 |
Digraph& _digraph; |
466 | 466 |
|
467 | 467 |
std::string _nodes_caption; |
468 | 468 |
std::string _arcs_caption; |
469 | 469 |
std::string _attributes_caption; |
470 | 470 |
|
471 | 471 |
typedef std::map<std::string, Node> NodeIndex; |
472 | 472 |
NodeIndex _node_index; |
473 | 473 |
typedef std::map<std::string, Arc> ArcIndex; |
474 | 474 |
ArcIndex _arc_index; |
475 | 475 |
|
476 | 476 |
typedef std::vector<std::pair<std::string, |
477 | 477 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps; |
478 | 478 |
NodeMaps _node_maps; |
479 | 479 |
|
480 | 480 |
typedef std::vector<std::pair<std::string, |
481 | 481 |
_reader_bits::MapStorageBase<Arc>*> >ArcMaps; |
482 | 482 |
ArcMaps _arc_maps; |
483 | 483 |
|
484 | 484 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
485 | 485 |
Attributes; |
486 | 486 |
Attributes _attributes; |
487 | 487 |
|
488 | 488 |
bool _use_nodes; |
489 | 489 |
bool _use_arcs; |
490 | 490 |
|
491 | 491 |
bool _skip_nodes; |
492 | 492 |
bool _skip_arcs; |
493 | 493 |
|
494 | 494 |
int line_num; |
495 | 495 |
std::istringstream line; |
496 | 496 |
|
497 | 497 |
public: |
498 | 498 |
|
499 | 499 |
/// \brief Constructor |
500 | 500 |
/// |
501 | 501 |
/// Construct a directed graph reader, which reads from the given |
502 | 502 |
/// input stream. |
503 | 503 |
DigraphReader(Digraph& digraph, std::istream& is = std::cin) |
504 | 504 |
: _is(&is), local_is(false), _digraph(digraph), |
505 | 505 |
_use_nodes(false), _use_arcs(false), |
506 | 506 |
_skip_nodes(false), _skip_arcs(false) {} |
507 | 507 |
|
508 | 508 |
/// \brief Constructor |
... | ... |
@@ -1201,105 +1201,106 @@ |
1201 | 1201 |
|
1202 | 1202 |
/// \brief Return a \ref DigraphReader class |
1203 | 1203 |
/// |
1204 | 1204 |
/// This function just returns a \ref DigraphReader class. |
1205 | 1205 |
/// \relates DigraphReader |
1206 | 1206 |
template <typename Digraph> |
1207 | 1207 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, |
1208 | 1208 |
const std::string& fn) { |
1209 | 1209 |
DigraphReader<Digraph> tmp(digraph, fn); |
1210 | 1210 |
return tmp; |
1211 | 1211 |
} |
1212 | 1212 |
|
1213 | 1213 |
/// \brief Return a \ref DigraphReader class |
1214 | 1214 |
/// |
1215 | 1215 |
/// This function just returns a \ref DigraphReader class. |
1216 | 1216 |
/// \relates DigraphReader |
1217 | 1217 |
template <typename Digraph> |
1218 | 1218 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, const char* fn) { |
1219 | 1219 |
DigraphReader<Digraph> tmp(digraph, fn); |
1220 | 1220 |
return tmp; |
1221 | 1221 |
} |
1222 | 1222 |
|
1223 | 1223 |
template <typename Graph> |
1224 | 1224 |
class GraphReader; |
1225 | 1225 |
|
1226 | 1226 |
template <typename Graph> |
1227 | 1227 |
GraphReader<Graph> graphReader(Graph& graph, |
1228 | 1228 |
std::istream& is = std::cin); |
1229 | 1229 |
template <typename Graph> |
1230 | 1230 |
GraphReader<Graph> graphReader(Graph& graph, const std::string& fn); |
1231 | 1231 |
template <typename Graph> |
1232 | 1232 |
GraphReader<Graph> graphReader(Graph& graph, const char *fn); |
1233 | 1233 |
|
1234 | 1234 |
/// \ingroup lemon_io |
1235 | 1235 |
/// |
1236 | 1236 |
/// \brief \ref lgf-format "LGF" reader for undirected graphs |
1237 | 1237 |
/// |
1238 | 1238 |
/// This utility reads an \ref lgf-format "LGF" file. |
1239 | 1239 |
/// |
1240 | 1240 |
/// It can be used almost the same way as \c DigraphReader. |
1241 | 1241 |
/// The only difference is that this class can handle edges and |
1242 | 1242 |
/// edge maps as well as arcs and arc maps. |
1243 | 1243 |
/// |
1244 | 1244 |
/// The columns in the \c \@edges (or \c \@arcs) section are the |
1245 | 1245 |
/// edge maps. However, if there are two maps with the same name |
1246 | 1246 |
/// prefixed with \c '+' and \c '-', then these can be read into an |
1247 | 1247 |
/// arc map. Similarly, an attribute can be read into an arc, if |
1248 | 1248 |
/// it's value is an edge label prefixed with \c '+' or \c '-'. |
1249 |
template <typename |
|
1249 |
template <typename GR> |
|
1250 | 1250 |
class GraphReader { |
1251 | 1251 |
public: |
1252 | 1252 |
|
1253 |
typedef |
|
1253 |
typedef GR Graph; |
|
1254 |
|
|
1255 |
private: |
|
1256 |
|
|
1254 | 1257 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
1255 | 1258 |
|
1256 |
private: |
|
1257 |
|
|
1258 | 1259 |
std::istream* _is; |
1259 | 1260 |
bool local_is; |
1260 | 1261 |
std::string _filename; |
1261 | 1262 |
|
1262 | 1263 |
Graph& _graph; |
1263 | 1264 |
|
1264 | 1265 |
std::string _nodes_caption; |
1265 | 1266 |
std::string _edges_caption; |
1266 | 1267 |
std::string _attributes_caption; |
1267 | 1268 |
|
1268 | 1269 |
typedef std::map<std::string, Node> NodeIndex; |
1269 | 1270 |
NodeIndex _node_index; |
1270 | 1271 |
typedef std::map<std::string, Edge> EdgeIndex; |
1271 | 1272 |
EdgeIndex _edge_index; |
1272 | 1273 |
|
1273 | 1274 |
typedef std::vector<std::pair<std::string, |
1274 | 1275 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps; |
1275 | 1276 |
NodeMaps _node_maps; |
1276 | 1277 |
|
1277 | 1278 |
typedef std::vector<std::pair<std::string, |
1278 | 1279 |
_reader_bits::MapStorageBase<Edge>*> > EdgeMaps; |
1279 | 1280 |
EdgeMaps _edge_maps; |
1280 | 1281 |
|
1281 | 1282 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
1282 | 1283 |
Attributes; |
1283 | 1284 |
Attributes _attributes; |
1284 | 1285 |
|
1285 | 1286 |
bool _use_nodes; |
1286 | 1287 |
bool _use_edges; |
1287 | 1288 |
|
1288 | 1289 |
bool _skip_nodes; |
1289 | 1290 |
bool _skip_edges; |
1290 | 1291 |
|
1291 | 1292 |
int line_num; |
1292 | 1293 |
std::istringstream line; |
1293 | 1294 |
|
1294 | 1295 |
public: |
1295 | 1296 |
|
1296 | 1297 |
/// \brief Constructor |
1297 | 1298 |
/// |
1298 | 1299 |
/// Construct an undirected graph reader, which reads from the given |
1299 | 1300 |
/// input stream. |
1300 | 1301 |
GraphReader(Graph& graph, std::istream& is = std::cin) |
1301 | 1302 |
: _is(&is), local_is(false), _graph(graph), |
1302 | 1303 |
_use_nodes(false), _use_edges(false), |
1303 | 1304 |
_skip_nodes(false), _skip_edges(false) {} |
1304 | 1305 |
|
1305 | 1306 |
/// \brief Constructor |
... | ... |
@@ -1311,102 +1312,102 @@ |
1311 | 1312 |
_filename(fn), _graph(graph), |
1312 | 1313 |
_use_nodes(false), _use_edges(false), |
1313 | 1314 |
_skip_nodes(false), _skip_edges(false) { |
1314 | 1315 |
if (!(*_is)) { |
1315 | 1316 |
delete _is; |
1316 | 1317 |
throw IoError("Cannot open file", fn); |
1317 | 1318 |
} |
1318 | 1319 |
} |
1319 | 1320 |
|
1320 | 1321 |
/// \brief Constructor |
1321 | 1322 |
/// |
1322 | 1323 |
/// Construct an undirected graph reader, which reads from the given |
1323 | 1324 |
/// file. |
1324 | 1325 |
GraphReader(Graph& graph, const char* fn) |
1325 | 1326 |
: _is(new std::ifstream(fn)), local_is(true), |
1326 | 1327 |
_filename(fn), _graph(graph), |
1327 | 1328 |
_use_nodes(false), _use_edges(false), |
1328 | 1329 |
_skip_nodes(false), _skip_edges(false) { |
1329 | 1330 |
if (!(*_is)) { |
1330 | 1331 |
delete _is; |
1331 | 1332 |
throw IoError("Cannot open file", fn); |
1332 | 1333 |
} |
1333 | 1334 |
} |
1334 | 1335 |
|
1335 | 1336 |
/// \brief Destructor |
1336 | 1337 |
~GraphReader() { |
1337 | 1338 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
1338 | 1339 |
it != _node_maps.end(); ++it) { |
1339 | 1340 |
delete it->second; |
1340 | 1341 |
} |
1341 | 1342 |
|
1342 | 1343 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
1343 | 1344 |
it != _edge_maps.end(); ++it) { |
1344 | 1345 |
delete it->second; |
1345 | 1346 |
} |
1346 | 1347 |
|
1347 | 1348 |
for (typename Attributes::iterator it = _attributes.begin(); |
1348 | 1349 |
it != _attributes.end(); ++it) { |
1349 | 1350 |
delete it->second; |
1350 | 1351 |
} |
1351 | 1352 |
|
1352 | 1353 |
if (local_is) { |
1353 | 1354 |
delete _is; |
1354 | 1355 |
} |
1355 | 1356 |
|
1356 | 1357 |
} |
1357 | 1358 |
|
1358 | 1359 |
private: |
1359 |
template <typename GR> |
|
1360 |
friend GraphReader<GR> graphReader(GR& graph, std::istream& is); |
|
1361 |
template <typename GR> |
|
1362 |
friend GraphReader<GR> graphReader(GR& graph, const std::string& fn); |
|
1363 |
template <typename GR> |
|
1364 |
friend GraphReader<GR> graphReader(GR& graph, const char *fn); |
|
1360 |
template <typename Graph> |
|
1361 |
friend GraphReader<Graph> graphReader(Graph& graph, std::istream& is); |
|
1362 |
template <typename Graph> |
|
1363 |
friend GraphReader<Graph> graphReader(Graph& graph, const std::string& fn); |
|
1364 |
template <typename Graph> |
|
1365 |
friend GraphReader<Graph> graphReader(Graph& graph, const char *fn); |
|
1365 | 1366 |
|
1366 | 1367 |
GraphReader(GraphReader& other) |
1367 | 1368 |
: _is(other._is), local_is(other.local_is), _graph(other._graph), |
1368 | 1369 |
_use_nodes(other._use_nodes), _use_edges(other._use_edges), |
1369 | 1370 |
_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) { |
1370 | 1371 |
|
1371 | 1372 |
other._is = 0; |
1372 | 1373 |
other.local_is = false; |
1373 | 1374 |
|
1374 | 1375 |
_node_index.swap(other._node_index); |
1375 | 1376 |
_edge_index.swap(other._edge_index); |
1376 | 1377 |
|
1377 | 1378 |
_node_maps.swap(other._node_maps); |
1378 | 1379 |
_edge_maps.swap(other._edge_maps); |
1379 | 1380 |
_attributes.swap(other._attributes); |
1380 | 1381 |
|
1381 | 1382 |
_nodes_caption = other._nodes_caption; |
1382 | 1383 |
_edges_caption = other._edges_caption; |
1383 | 1384 |
_attributes_caption = other._attributes_caption; |
1384 | 1385 |
|
1385 | 1386 |
} |
1386 | 1387 |
|
1387 | 1388 |
GraphReader& operator=(const GraphReader&); |
1388 | 1389 |
|
1389 | 1390 |
public: |
1390 | 1391 |
|
1391 | 1392 |
/// \name Reading rules |
1392 | 1393 |
/// @{ |
1393 | 1394 |
|
1394 | 1395 |
/// \brief Node map reading rule |
1395 | 1396 |
/// |
1396 | 1397 |
/// Add a node map reading rule to the reader. |
1397 | 1398 |
template <typename Map> |
1398 | 1399 |
GraphReader& nodeMap(const std::string& caption, Map& map) { |
1399 | 1400 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
1400 | 1401 |
_reader_bits::MapStorageBase<Node>* storage = |
1401 | 1402 |
new _reader_bits::MapStorage<Node, Map>(map); |
1402 | 1403 |
_node_maps.push_back(std::make_pair(caption, storage)); |
1403 | 1404 |
return *this; |
1404 | 1405 |
} |
1405 | 1406 |
|
1406 | 1407 |
/// \brief Node map reading rule |
1407 | 1408 |
/// |
1408 | 1409 |
/// Add a node map reading rule with specialized converter to the |
1409 | 1410 |
/// reader. |
1410 | 1411 |
template <typename Map, typename Converter> |
1411 | 1412 |
GraphReader& nodeMap(const std::string& caption, Map& map, |
1412 | 1413 |
const Converter& converter = Converter()) { |
... | ... |
@@ -361,101 +361,101 @@ |
361 | 361 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
362 | 362 |
const char* fn); |
363 | 363 |
|
364 | 364 |
|
365 | 365 |
/// \ingroup lemon_io |
366 | 366 |
/// |
367 | 367 |
/// \brief \ref lgf-format "LGF" writer for directed graphs |
368 | 368 |
/// |
369 | 369 |
/// This utility writes an \ref lgf-format "LGF" file. |
370 | 370 |
/// |
371 | 371 |
/// The writing method does a batch processing. The user creates a |
372 | 372 |
/// writer object, then various writing rules can be added to the |
373 | 373 |
/// writer, and eventually the writing is executed with the \c run() |
374 | 374 |
/// member function. A map writing rule can be added to the writer |
375 | 375 |
/// with the \c nodeMap() or \c arcMap() members. An optional |
376 | 376 |
/// converter parameter can also be added as a standard functor |
377 | 377 |
/// converting from the value type of the map to \c std::string. If it |
378 | 378 |
/// is set, it will determine how the value type of the map is written to |
379 | 379 |
/// the output stream. If the functor is not set, then a default |
380 | 380 |
/// conversion will be used. The \c attribute(), \c node() and \c |
381 | 381 |
/// arc() functions are used to add attribute writing rules. |
382 | 382 |
/// |
383 | 383 |
///\code |
384 | 384 |
/// DigraphWriter<Digraph>(digraph, std::cout). |
385 | 385 |
/// nodeMap("coordinates", coord_map). |
386 | 386 |
/// nodeMap("size", size). |
387 | 387 |
/// nodeMap("title", title). |
388 | 388 |
/// arcMap("capacity", cap_map). |
389 | 389 |
/// node("source", src). |
390 | 390 |
/// node("target", trg). |
391 | 391 |
/// attribute("caption", caption). |
392 | 392 |
/// run(); |
393 | 393 |
///\endcode |
394 | 394 |
/// |
395 | 395 |
/// |
396 | 396 |
/// By default, the writer does not write additional captions to the |
397 | 397 |
/// sections, but they can be give as an optional parameter of |
398 | 398 |
/// the \c nodes(), \c arcs() or \c |
399 | 399 |
/// attributes() functions. |
400 | 400 |
/// |
401 | 401 |
/// The \c skipNodes() and \c skipArcs() functions forbid the |
402 | 402 |
/// writing of the sections. If two arc sections should be written |
403 | 403 |
/// to the output, it can be done in two passes, the first pass |
404 | 404 |
/// writes the node section and the first arc section, then the |
405 | 405 |
/// second pass skips the node section and writes just the arc |
406 | 406 |
/// section to the stream. The output stream can be retrieved with |
407 | 407 |
/// the \c ostream() function, hence the second pass can append its |
408 | 408 |
/// output to the output of the first pass. |
409 |
template <typename |
|
409 |
template <typename GR> |
|
410 | 410 |
class DigraphWriter { |
411 | 411 |
public: |
412 | 412 |
|
413 |
typedef |
|
413 |
typedef GR Digraph; |
|
414 | 414 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
415 | 415 |
|
416 | 416 |
private: |
417 | 417 |
|
418 | 418 |
|
419 | 419 |
std::ostream* _os; |
420 | 420 |
bool local_os; |
421 | 421 |
|
422 | 422 |
const Digraph& _digraph; |
423 | 423 |
|
424 | 424 |
std::string _nodes_caption; |
425 | 425 |
std::string _arcs_caption; |
426 | 426 |
std::string _attributes_caption; |
427 | 427 |
|
428 | 428 |
typedef std::map<Node, std::string> NodeIndex; |
429 | 429 |
NodeIndex _node_index; |
430 | 430 |
typedef std::map<Arc, std::string> ArcIndex; |
431 | 431 |
ArcIndex _arc_index; |
432 | 432 |
|
433 | 433 |
typedef std::vector<std::pair<std::string, |
434 | 434 |
_writer_bits::MapStorageBase<Node>* > > NodeMaps; |
435 | 435 |
NodeMaps _node_maps; |
436 | 436 |
|
437 | 437 |
typedef std::vector<std::pair<std::string, |
438 | 438 |
_writer_bits::MapStorageBase<Arc>* > >ArcMaps; |
439 | 439 |
ArcMaps _arc_maps; |
440 | 440 |
|
441 | 441 |
typedef std::vector<std::pair<std::string, |
442 | 442 |
_writer_bits::ValueStorageBase*> > Attributes; |
443 | 443 |
Attributes _attributes; |
444 | 444 |
|
445 | 445 |
bool _skip_nodes; |
446 | 446 |
bool _skip_arcs; |
447 | 447 |
|
448 | 448 |
public: |
449 | 449 |
|
450 | 450 |
/// \brief Constructor |
451 | 451 |
/// |
452 | 452 |
/// Construct a directed graph writer, which writes to the given |
453 | 453 |
/// output stream. |
454 | 454 |
DigraphWriter(const Digraph& digraph, std::ostream& os = std::cout) |
455 | 455 |
: _os(&os), local_os(false), _digraph(digraph), |
456 | 456 |
_skip_nodes(false), _skip_arcs(false) {} |
457 | 457 |
|
458 | 458 |
/// \brief Constructor |
459 | 459 |
/// |
460 | 460 |
/// Construct a directed graph writer, which writes to the given |
461 | 461 |
/// output file. |
... | ... |
@@ -929,204 +929,204 @@ |
929 | 929 |
/// \brief Return a \ref DigraphWriter class |
930 | 930 |
/// |
931 | 931 |
/// This function just returns a \ref DigraphWriter class. |
932 | 932 |
/// \relates DigraphWriter |
933 | 933 |
template <typename Digraph> |
934 | 934 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
935 | 935 |
const std::string& fn) { |
936 | 936 |
DigraphWriter<Digraph> tmp(digraph, fn); |
937 | 937 |
return tmp; |
938 | 938 |
} |
939 | 939 |
|
940 | 940 |
/// \brief Return a \ref DigraphWriter class |
941 | 941 |
/// |
942 | 942 |
/// This function just returns a \ref DigraphWriter class. |
943 | 943 |
/// \relates DigraphWriter |
944 | 944 |
template <typename Digraph> |
945 | 945 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
946 | 946 |
const char* fn) { |
947 | 947 |
DigraphWriter<Digraph> tmp(digraph, fn); |
948 | 948 |
return tmp; |
949 | 949 |
} |
950 | 950 |
|
951 | 951 |
template <typename Graph> |
952 | 952 |
class GraphWriter; |
953 | 953 |
|
954 | 954 |
template <typename Graph> |
955 | 955 |
GraphWriter<Graph> graphWriter(const Graph& graph, |
956 | 956 |
std::ostream& os = std::cout); |
957 | 957 |
template <typename Graph> |
958 | 958 |
GraphWriter<Graph> graphWriter(const Graph& graph, const std::string& fn); |
959 | 959 |
template <typename Graph> |
960 | 960 |
GraphWriter<Graph> graphWriter(const Graph& graph, const char* fn); |
961 | 961 |
|
962 | 962 |
/// \ingroup lemon_io |
963 | 963 |
/// |
964 | 964 |
/// \brief \ref lgf-format "LGF" writer for directed graphs |
965 | 965 |
/// |
966 | 966 |
/// This utility writes an \ref lgf-format "LGF" file. |
967 | 967 |
/// |
968 | 968 |
/// It can be used almost the same way as \c DigraphWriter. |
969 | 969 |
/// The only difference is that this class can handle edges and |
970 | 970 |
/// edge maps as well as arcs and arc maps. |
971 | 971 |
/// |
972 | 972 |
/// The arc maps are written into the file as two columns, the |
973 | 973 |
/// caption of the columns are the name of the map prefixed with \c |
974 | 974 |
/// '+' and \c '-'. The arcs are written into the \c \@attributes |
975 | 975 |
/// section as a \c '+' or a \c '-' prefix (depends on the direction |
976 | 976 |
/// of the arc) and the label of corresponding edge. |
977 |
template <typename |
|
977 |
template <typename GR> |
|
978 | 978 |
class GraphWriter { |
979 | 979 |
public: |
980 | 980 |
|
981 |
typedef |
|
981 |
typedef GR Graph; |
|
982 | 982 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
983 | 983 |
|
984 | 984 |
private: |
985 | 985 |
|
986 | 986 |
|
987 | 987 |
std::ostream* _os; |
988 | 988 |
bool local_os; |
989 | 989 |
|
990 | 990 |
const Graph& _graph; |
991 | 991 |
|
992 | 992 |
std::string _nodes_caption; |
993 | 993 |
std::string _edges_caption; |
994 | 994 |
std::string _attributes_caption; |
995 | 995 |
|
996 | 996 |
typedef std::map<Node, std::string> NodeIndex; |
997 | 997 |
NodeIndex _node_index; |
998 | 998 |
typedef std::map<Edge, std::string> EdgeIndex; |
999 | 999 |
EdgeIndex _edge_index; |
1000 | 1000 |
|
1001 | 1001 |
typedef std::vector<std::pair<std::string, |
1002 | 1002 |
_writer_bits::MapStorageBase<Node>* > > NodeMaps; |
1003 | 1003 |
NodeMaps _node_maps; |
1004 | 1004 |
|
1005 | 1005 |
typedef std::vector<std::pair<std::string, |
1006 | 1006 |
_writer_bits::MapStorageBase<Edge>* > >EdgeMaps; |
1007 | 1007 |
EdgeMaps _edge_maps; |
1008 | 1008 |
|
1009 | 1009 |
typedef std::vector<std::pair<std::string, |
1010 | 1010 |
_writer_bits::ValueStorageBase*> > Attributes; |
1011 | 1011 |
Attributes _attributes; |
1012 | 1012 |
|
1013 | 1013 |
bool _skip_nodes; |
1014 | 1014 |
bool _skip_edges; |
1015 | 1015 |
|
1016 | 1016 |
public: |
1017 | 1017 |
|
1018 | 1018 |
/// \brief Constructor |
1019 | 1019 |
/// |
1020 | 1020 |
/// Construct a directed graph writer, which writes to the given |
1021 | 1021 |
/// output stream. |
1022 | 1022 |
GraphWriter(const Graph& graph, std::ostream& os = std::cout) |
1023 | 1023 |
: _os(&os), local_os(false), _graph(graph), |
1024 | 1024 |
_skip_nodes(false), _skip_edges(false) {} |
1025 | 1025 |
|
1026 | 1026 |
/// \brief Constructor |
1027 | 1027 |
/// |
1028 | 1028 |
/// Construct a directed graph writer, which writes to the given |
1029 | 1029 |
/// output file. |
1030 | 1030 |
GraphWriter(const Graph& graph, const std::string& fn) |
1031 | 1031 |
: _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph), |
1032 | 1032 |
_skip_nodes(false), _skip_edges(false) { |
1033 | 1033 |
if (!(*_os)) { |
1034 | 1034 |
delete _os; |
1035 | 1035 |
throw IoError("Cannot write file", fn); |
1036 | 1036 |
} |
1037 | 1037 |
} |
1038 | 1038 |
|
1039 | 1039 |
/// \brief Constructor |
1040 | 1040 |
/// |
1041 | 1041 |
/// Construct a directed graph writer, which writes to the given |
1042 | 1042 |
/// output file. |
1043 | 1043 |
GraphWriter(const Graph& graph, const char* fn) |
1044 | 1044 |
: _os(new std::ofstream(fn)), local_os(true), _graph(graph), |
1045 | 1045 |
_skip_nodes(false), _skip_edges(false) { |
1046 | 1046 |
if (!(*_os)) { |
1047 | 1047 |
delete _os; |
1048 | 1048 |
throw IoError("Cannot write file", fn); |
1049 | 1049 |
} |
1050 | 1050 |
} |
1051 | 1051 |
|
1052 | 1052 |
/// \brief Destructor |
1053 | 1053 |
~GraphWriter() { |
1054 | 1054 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
1055 | 1055 |
it != _node_maps.end(); ++it) { |
1056 | 1056 |
delete it->second; |
1057 | 1057 |
} |
1058 | 1058 |
|
1059 | 1059 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
1060 | 1060 |
it != _edge_maps.end(); ++it) { |
1061 | 1061 |
delete it->second; |
1062 | 1062 |
} |
1063 | 1063 |
|
1064 | 1064 |
for (typename Attributes::iterator it = _attributes.begin(); |
1065 | 1065 |
it != _attributes.end(); ++it) { |
1066 | 1066 |
delete it->second; |
1067 | 1067 |
} |
1068 | 1068 |
|
1069 | 1069 |
if (local_os) { |
1070 | 1070 |
delete _os; |
1071 | 1071 |
} |
1072 | 1072 |
} |
1073 | 1073 |
|
1074 | 1074 |
private: |
1075 | 1075 |
|
1076 |
template <typename GR> |
|
1077 |
friend GraphWriter<GR> graphWriter(const GR& graph, |
|
1078 |
std::ostream& os); |
|
1079 |
template <typename GR> |
|
1080 |
friend GraphWriter<GR> graphWriter(const GR& graph, |
|
1081 |
const std::string& fn); |
|
1082 |
template <typename GR> |
|
1083 |
friend GraphWriter<GR> graphWriter(const GR& graph, |
|
1084 |
|
|
1076 |
template <typename Graph> |
|
1077 |
friend GraphWriter<Graph> graphWriter(const Graph& graph, |
|
1078 |
std::ostream& os); |
|
1079 |
template <typename Graph> |
|
1080 |
friend GraphWriter<Graph> graphWriter(const Graph& graph, |
|
1081 |
const std::string& fn); |
|
1082 |
template <typename Graph> |
|
1083 |
friend GraphWriter<Graph> graphWriter(const Graph& graph, |
|
1084 |
const char *fn); |
|
1085 | 1085 |
|
1086 | 1086 |
GraphWriter(GraphWriter& other) |
1087 | 1087 |
: _os(other._os), local_os(other.local_os), _graph(other._graph), |
1088 | 1088 |
_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) { |
1089 | 1089 |
|
1090 | 1090 |
other._os = 0; |
1091 | 1091 |
other.local_os = false; |
1092 | 1092 |
|
1093 | 1093 |
_node_index.swap(other._node_index); |
1094 | 1094 |
_edge_index.swap(other._edge_index); |
1095 | 1095 |
|
1096 | 1096 |
_node_maps.swap(other._node_maps); |
1097 | 1097 |
_edge_maps.swap(other._edge_maps); |
1098 | 1098 |
_attributes.swap(other._attributes); |
1099 | 1099 |
|
1100 | 1100 |
_nodes_caption = other._nodes_caption; |
1101 | 1101 |
_edges_caption = other._edges_caption; |
1102 | 1102 |
_attributes_caption = other._attributes_caption; |
1103 | 1103 |
} |
1104 | 1104 |
|
1105 | 1105 |
GraphWriter& operator=(const GraphWriter&); |
1106 | 1106 |
|
1107 | 1107 |
public: |
1108 | 1108 |
|
1109 | 1109 |
/// \name Writing rules |
1110 | 1110 |
/// @{ |
1111 | 1111 |
|
1112 | 1112 |
/// \brief Node map writing rule |
1113 | 1113 |
/// |
1114 | 1114 |
/// Add a node map writing rule to the writer. |
1115 | 1115 |
template <typename Map> |
1116 | 1116 |
GraphWriter& nodeMap(const std::string& caption, const Map& map) { |
1117 | 1117 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
1118 | 1118 |
_writer_bits::MapStorageBase<Node>* storage = |
1119 | 1119 |
new _writer_bits::MapStorage<Node, Map>(map); |
1120 | 1120 |
_node_maps.push_back(std::make_pair(caption, storage)); |
1121 | 1121 |
return *this; |
1122 | 1122 |
} |
1123 | 1123 |
|
1124 | 1124 |
/// \brief Node map writing rule |
1125 | 1125 |
/// |
1126 | 1126 |
/// Add a node map writing rule with specialized converter to the |
1127 | 1127 |
/// writer. |
1128 | 1128 |
template <typename Map, typename Converter> |
1129 | 1129 |
GraphWriter& nodeMap(const std::string& caption, const Map& map, |
1130 | 1130 |
const Converter& converter = Converter()) { |
1131 | 1131 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
1132 | 1132 |
_writer_bits::MapStorageBase<Node>* storage = |
... | ... |
@@ -306,104 +306,104 @@ |
306 | 306 |
|
307 | 307 |
typedef DigraphExtender<ListDigraphBase> ExtendedListDigraphBase; |
308 | 308 |
|
309 | 309 |
/// \addtogroup graphs |
310 | 310 |
/// @{ |
311 | 311 |
|
312 | 312 |
///A general directed graph structure. |
313 | 313 |
|
314 | 314 |
///\ref ListDigraph is a simple and fast <em>directed graph</em> |
315 | 315 |
///implementation based on static linked lists that are stored in |
316 | 316 |
///\c std::vector structures. |
317 | 317 |
/// |
318 | 318 |
///It conforms to the \ref concepts::Digraph "Digraph concept" and it |
319 | 319 |
///also provides several useful additional functionalities. |
320 | 320 |
///Most of the member functions and nested classes are documented |
321 | 321 |
///only in the concept class. |
322 | 322 |
/// |
323 | 323 |
///An important extra feature of this digraph implementation is that |
324 | 324 |
///its maps are real \ref concepts::ReferenceMap "reference map"s. |
325 | 325 |
/// |
326 | 326 |
///\sa concepts::Digraph |
327 | 327 |
|
328 | 328 |
class ListDigraph : public ExtendedListDigraphBase { |
329 | 329 |
private: |
330 | 330 |
///ListDigraph is \e not copy constructible. Use copyDigraph() instead. |
331 | 331 |
|
332 | 332 |
///ListDigraph is \e not copy constructible. Use copyDigraph() instead. |
333 | 333 |
/// |
334 | 334 |
ListDigraph(const ListDigraph &) :ExtendedListDigraphBase() {}; |
335 | 335 |
///\brief Assignment of ListDigraph to another one is \e not allowed. |
336 | 336 |
///Use copyDigraph() instead. |
337 | 337 |
|
338 | 338 |
///Assignment of ListDigraph to another one is \e not allowed. |
339 | 339 |
///Use copyDigraph() instead. |
340 | 340 |
void operator=(const ListDigraph &) {} |
341 | 341 |
public: |
342 | 342 |
|
343 | 343 |
typedef ExtendedListDigraphBase Parent; |
344 | 344 |
|
345 | 345 |
/// Constructor |
346 | 346 |
|
347 | 347 |
/// Constructor. |
348 | 348 |
/// |
349 | 349 |
ListDigraph() {} |
350 | 350 |
|
351 | 351 |
///Add a new node to the digraph. |
352 | 352 |
|
353 | 353 |
///Add a new node to the digraph. |
354 |
///\return |
|
354 |
///\return The new node. |
|
355 | 355 |
Node addNode() { return Parent::addNode(); } |
356 | 356 |
|
357 | 357 |
///Add a new arc to the digraph. |
358 | 358 |
|
359 | 359 |
///Add a new arc to the digraph with source node \c s |
360 | 360 |
///and target node \c t. |
361 |
///\return |
|
361 |
///\return The new arc. |
|
362 | 362 |
Arc addArc(const Node& s, const Node& t) { |
363 | 363 |
return Parent::addArc(s, t); |
364 | 364 |
} |
365 | 365 |
|
366 | 366 |
///\brief Erase a node from the digraph. |
367 | 367 |
/// |
368 | 368 |
///Erase a node from the digraph. |
369 | 369 |
/// |
370 | 370 |
void erase(const Node& n) { Parent::erase(n); } |
371 | 371 |
|
372 | 372 |
///\brief Erase an arc from the digraph. |
373 | 373 |
/// |
374 | 374 |
///Erase an arc from the digraph. |
375 | 375 |
/// |
376 | 376 |
void erase(const Arc& a) { Parent::erase(a); } |
377 | 377 |
|
378 | 378 |
/// Node validity check |
379 | 379 |
|
380 | 380 |
/// This function gives back true if the given node is valid, |
381 | 381 |
/// ie. it is a real node of the graph. |
382 | 382 |
/// |
383 | 383 |
/// \warning A Node pointing to a removed item |
384 | 384 |
/// could become valid again later if new nodes are |
385 | 385 |
/// added to the graph. |
386 | 386 |
bool valid(Node n) const { return Parent::valid(n); } |
387 | 387 |
|
388 | 388 |
/// Arc validity check |
389 | 389 |
|
390 | 390 |
/// This function gives back true if the given arc is valid, |
391 | 391 |
/// ie. it is a real arc of the graph. |
392 | 392 |
/// |
393 | 393 |
/// \warning An Arc pointing to a removed item |
394 | 394 |
/// could become valid again later if new nodes are |
395 | 395 |
/// added to the graph. |
396 | 396 |
bool valid(Arc a) const { return Parent::valid(a); } |
397 | 397 |
|
398 | 398 |
/// Change the target of \c a to \c n |
399 | 399 |
|
400 | 400 |
/// Change the target of \c a to \c n |
401 | 401 |
/// |
402 | 402 |
///\note The <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s referencing |
403 | 403 |
///the changed arc remain valid. However <tt>InArcIt</tt>s are |
404 | 404 |
///invalidated. |
405 | 405 |
/// |
406 | 406 |
///\warning This functionality cannot be used together with the Snapshot |
407 | 407 |
///feature. |
408 | 408 |
void changeTarget(Arc a, Node n) { |
409 | 409 |
Parent::changeTarget(a,n); |
... | ... |
@@ -1163,104 +1163,104 @@ |
1163 | 1163 |
|
1164 | 1164 |
|
1165 | 1165 |
/// \addtogroup graphs |
1166 | 1166 |
/// @{ |
1167 | 1167 |
|
1168 | 1168 |
///A general undirected graph structure. |
1169 | 1169 |
|
1170 | 1170 |
///\ref ListGraph is a simple and fast <em>undirected graph</em> |
1171 | 1171 |
///implementation based on static linked lists that are stored in |
1172 | 1172 |
///\c std::vector structures. |
1173 | 1173 |
/// |
1174 | 1174 |
///It conforms to the \ref concepts::Graph "Graph concept" and it |
1175 | 1175 |
///also provides several useful additional functionalities. |
1176 | 1176 |
///Most of the member functions and nested classes are documented |
1177 | 1177 |
///only in the concept class. |
1178 | 1178 |
/// |
1179 | 1179 |
///An important extra feature of this graph implementation is that |
1180 | 1180 |
///its maps are real \ref concepts::ReferenceMap "reference map"s. |
1181 | 1181 |
/// |
1182 | 1182 |
///\sa concepts::Graph |
1183 | 1183 |
|
1184 | 1184 |
class ListGraph : public ExtendedListGraphBase { |
1185 | 1185 |
private: |
1186 | 1186 |
///ListGraph is \e not copy constructible. Use copyGraph() instead. |
1187 | 1187 |
|
1188 | 1188 |
///ListGraph is \e not copy constructible. Use copyGraph() instead. |
1189 | 1189 |
/// |
1190 | 1190 |
ListGraph(const ListGraph &) :ExtendedListGraphBase() {}; |
1191 | 1191 |
///\brief Assignment of ListGraph to another one is \e not allowed. |
1192 | 1192 |
///Use copyGraph() instead. |
1193 | 1193 |
|
1194 | 1194 |
///Assignment of ListGraph to another one is \e not allowed. |
1195 | 1195 |
///Use copyGraph() instead. |
1196 | 1196 |
void operator=(const ListGraph &) {} |
1197 | 1197 |
public: |
1198 | 1198 |
/// Constructor |
1199 | 1199 |
|
1200 | 1200 |
/// Constructor. |
1201 | 1201 |
/// |
1202 | 1202 |
ListGraph() {} |
1203 | 1203 |
|
1204 | 1204 |
typedef ExtendedListGraphBase Parent; |
1205 | 1205 |
|
1206 | 1206 |
typedef Parent::OutArcIt IncEdgeIt; |
1207 | 1207 |
|
1208 | 1208 |
/// \brief Add a new node to the graph. |
1209 | 1209 |
/// |
1210 | 1210 |
/// Add a new node to the graph. |
1211 |
/// \return |
|
1211 |
/// \return The new node. |
|
1212 | 1212 |
Node addNode() { return Parent::addNode(); } |
1213 | 1213 |
|
1214 | 1214 |
/// \brief Add a new edge to the graph. |
1215 | 1215 |
/// |
1216 | 1216 |
/// Add a new edge to the graph with source node \c s |
1217 | 1217 |
/// and target node \c t. |
1218 |
/// \return |
|
1218 |
/// \return The new edge. |
|
1219 | 1219 |
Edge addEdge(const Node& s, const Node& t) { |
1220 | 1220 |
return Parent::addEdge(s, t); |
1221 | 1221 |
} |
1222 | 1222 |
|
1223 | 1223 |
/// \brief Erase a node from the graph. |
1224 | 1224 |
/// |
1225 | 1225 |
/// Erase a node from the graph. |
1226 | 1226 |
/// |
1227 | 1227 |
void erase(const Node& n) { Parent::erase(n); } |
1228 | 1228 |
|
1229 | 1229 |
/// \brief Erase an edge from the graph. |
1230 | 1230 |
/// |
1231 | 1231 |
/// Erase an edge from the graph. |
1232 | 1232 |
/// |
1233 | 1233 |
void erase(const Edge& e) { Parent::erase(e); } |
1234 | 1234 |
/// Node validity check |
1235 | 1235 |
|
1236 | 1236 |
/// This function gives back true if the given node is valid, |
1237 | 1237 |
/// ie. it is a real node of the graph. |
1238 | 1238 |
/// |
1239 | 1239 |
/// \warning A Node pointing to a removed item |
1240 | 1240 |
/// could become valid again later if new nodes are |
1241 | 1241 |
/// added to the graph. |
1242 | 1242 |
bool valid(Node n) const { return Parent::valid(n); } |
1243 | 1243 |
/// Arc validity check |
1244 | 1244 |
|
1245 | 1245 |
/// This function gives back true if the given arc is valid, |
1246 | 1246 |
/// ie. it is a real arc of the graph. |
1247 | 1247 |
/// |
1248 | 1248 |
/// \warning An Arc pointing to a removed item |
1249 | 1249 |
/// could become valid again later if new edges are |
1250 | 1250 |
/// added to the graph. |
1251 | 1251 |
bool valid(Arc a) const { return Parent::valid(a); } |
1252 | 1252 |
/// Edge validity check |
1253 | 1253 |
|
1254 | 1254 |
/// This function gives back true if the given edge is valid, |
1255 | 1255 |
/// ie. it is a real arc of the graph. |
1256 | 1256 |
/// |
1257 | 1257 |
/// \warning A Edge pointing to a removed item |
1258 | 1258 |
/// could become valid again later if new edges are |
1259 | 1259 |
/// added to the graph. |
1260 | 1260 |
bool valid(Edge e) const { return Parent::valid(e); } |
1261 | 1261 |
/// \brief Change the end \c u of \c e to \c n |
1262 | 1262 |
/// |
1263 | 1263 |
/// This function changes the end \c u of \c e to node \c n. |
1264 | 1264 |
/// |
1265 | 1265 |
///\note The <tt>EdgeIt</tt>s and <tt>ArcIt</tt>s referencing the |
1266 | 1266 |
///changed edge are invalidated and if the changed node is the |
... | ... |
@@ -18,283 +18,286 @@ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_MAPS_H |
20 | 20 |
#define LEMON_MAPS_H |
21 | 21 |
|
22 | 22 |
#include <iterator> |
23 | 23 |
#include <functional> |
24 | 24 |
#include <vector> |
25 | 25 |
|
26 | 26 |
#include <lemon/core.h> |
27 | 27 |
|
28 | 28 |
///\file |
29 | 29 |
///\ingroup maps |
30 | 30 |
///\brief Miscellaneous property maps |
31 | 31 |
|
32 | 32 |
#include <map> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \addtogroup maps |
37 | 37 |
/// @{ |
38 | 38 |
|
39 | 39 |
/// Base class of maps. |
40 | 40 |
|
41 | 41 |
/// Base class of maps. It provides the necessary type definitions |
42 | 42 |
/// required by the map %concepts. |
43 | 43 |
template<typename K, typename V> |
44 | 44 |
class MapBase { |
45 | 45 |
public: |
46 | 46 |
/// \brief The key type of the map. |
47 | 47 |
typedef K Key; |
48 | 48 |
/// \brief The value type of the map. |
49 | 49 |
/// (The type of objects associated with the keys). |
50 | 50 |
typedef V Value; |
51 | 51 |
}; |
52 | 52 |
|
53 | 53 |
|
54 | 54 |
/// Null map. (a.k.a. DoNothingMap) |
55 | 55 |
|
56 | 56 |
/// This map can be used if you have to provide a map only for |
57 | 57 |
/// its type definitions, or if you have to provide a writable map, |
58 | 58 |
/// but data written to it is not required (i.e. it will be sent to |
59 | 59 |
/// <tt>/dev/null</tt>). |
60 | 60 |
/// It conforms the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
61 | 61 |
/// |
62 | 62 |
/// \sa ConstMap |
63 | 63 |
template<typename K, typename V> |
64 | 64 |
class NullMap : public MapBase<K, V> { |
65 | 65 |
public: |
66 |
typedef MapBase<K, V> Parent; |
|
67 |
typedef typename Parent::Key Key; |
|
68 |
|
|
66 |
///\e |
|
67 |
typedef K Key; |
|
68 |
///\e |
|
69 |
typedef V Value; |
|
69 | 70 |
|
70 | 71 |
/// Gives back a default constructed element. |
71 | 72 |
Value operator[](const Key&) const { return Value(); } |
72 | 73 |
/// Absorbs the value. |
73 | 74 |
void set(const Key&, const Value&) {} |
74 | 75 |
}; |
75 | 76 |
|
76 | 77 |
/// Returns a \c NullMap class |
77 | 78 |
|
78 | 79 |
/// This function just returns a \c NullMap class. |
79 | 80 |
/// \relates NullMap |
80 | 81 |
template <typename K, typename V> |
81 | 82 |
NullMap<K, V> nullMap() { |
82 | 83 |
return NullMap<K, V>(); |
83 | 84 |
} |
84 | 85 |
|
85 | 86 |
|
86 | 87 |
/// Constant map. |
87 | 88 |
|
88 | 89 |
/// This \ref concepts::ReadMap "readable map" assigns a specified |
89 | 90 |
/// value to each key. |
90 | 91 |
/// |
91 | 92 |
/// In other aspects it is equivalent to \c NullMap. |
92 | 93 |
/// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap" |
93 | 94 |
/// concept, but it absorbs the data written to it. |
94 | 95 |
/// |
95 | 96 |
/// The simplest way of using this map is through the constMap() |
96 | 97 |
/// function. |
97 | 98 |
/// |
98 | 99 |
/// \sa NullMap |
99 | 100 |
/// \sa IdentityMap |
100 | 101 |
template<typename K, typename V> |
101 | 102 |
class ConstMap : public MapBase<K, V> { |
102 | 103 |
private: |
103 | 104 |
V _value; |
104 | 105 |
public: |
105 |
typedef MapBase<K, V> Parent; |
|
106 |
typedef typename Parent::Key Key; |
|
107 |
|
|
106 |
///\e |
|
107 |
typedef K Key; |
|
108 |
///\e |
|
109 |
typedef V Value; |
|
108 | 110 |
|
109 | 111 |
/// Default constructor |
110 | 112 |
|
111 | 113 |
/// Default constructor. |
112 | 114 |
/// The value of the map will be default constructed. |
113 | 115 |
ConstMap() {} |
114 | 116 |
|
115 | 117 |
/// Constructor with specified initial value |
116 | 118 |
|
117 | 119 |
/// Constructor with specified initial value. |
118 | 120 |
/// \param v The initial value of the map. |
119 | 121 |
ConstMap(const Value &v) : _value(v) {} |
120 | 122 |
|
121 | 123 |
/// Gives back the specified value. |
122 | 124 |
Value operator[](const Key&) const { return _value; } |
123 | 125 |
|
124 | 126 |
/// Absorbs the value. |
125 | 127 |
void set(const Key&, const Value&) {} |
126 | 128 |
|
127 | 129 |
/// Sets the value that is assigned to each key. |
128 | 130 |
void setAll(const Value &v) { |
129 | 131 |
_value = v; |
130 | 132 |
} |
131 | 133 |
|
132 | 134 |
template<typename V1> |
133 | 135 |
ConstMap(const ConstMap<K, V1> &, const Value &v) : _value(v) {} |
134 | 136 |
}; |
135 | 137 |
|
136 | 138 |
/// Returns a \c ConstMap class |
137 | 139 |
|
138 | 140 |
/// This function just returns a \c ConstMap class. |
139 | 141 |
/// \relates ConstMap |
140 | 142 |
template<typename K, typename V> |
141 | 143 |
inline ConstMap<K, V> constMap(const V &v) { |
142 | 144 |
return ConstMap<K, V>(v); |
143 | 145 |
} |
144 | 146 |
|
145 | 147 |
template<typename K, typename V> |
146 | 148 |
inline ConstMap<K, V> constMap() { |
147 | 149 |
return ConstMap<K, V>(); |
148 | 150 |
} |
149 | 151 |
|
150 | 152 |
|
151 | 153 |
template<typename T, T v> |
152 | 154 |
struct Const {}; |
153 | 155 |
|
154 | 156 |
/// Constant map with inlined constant value. |
155 | 157 |
|
156 | 158 |
/// This \ref concepts::ReadMap "readable map" assigns a specified |
157 | 159 |
/// value to each key. |
158 | 160 |
/// |
159 | 161 |
/// In other aspects it is equivalent to \c NullMap. |
160 | 162 |
/// So it conforms the \ref concepts::ReadWriteMap "ReadWriteMap" |
161 | 163 |
/// concept, but it absorbs the data written to it. |
162 | 164 |
/// |
163 | 165 |
/// The simplest way of using this map is through the constMap() |
164 | 166 |
/// function. |
165 | 167 |
/// |
166 | 168 |
/// \sa NullMap |
167 | 169 |
/// \sa IdentityMap |
168 | 170 |
template<typename K, typename V, V v> |
169 | 171 |
class ConstMap<K, Const<V, v> > : public MapBase<K, V> { |
170 | 172 |
public: |
171 |
typedef MapBase<K, V> Parent; |
|
172 |
typedef typename Parent::Key Key; |
|
173 |
|
|
173 |
///\e |
|
174 |
typedef K Key; |
|
175 |
///\e |
|
176 |
typedef V Value; |
|
174 | 177 |
|
175 | 178 |
/// Constructor. |
176 | 179 |
ConstMap() {} |
177 | 180 |
|
178 | 181 |
/// Gives back the specified value. |
179 | 182 |
Value operator[](const Key&) const { return v; } |
180 | 183 |
|
181 | 184 |
/// Absorbs the value. |
182 | 185 |
void set(const Key&, const Value&) {} |
183 | 186 |
}; |
184 | 187 |
|
185 | 188 |
/// Returns a \c ConstMap class with inlined constant value |
186 | 189 |
|
187 | 190 |
/// This function just returns a \c ConstMap class with inlined |
188 | 191 |
/// constant value. |
189 | 192 |
/// \relates ConstMap |
190 | 193 |
template<typename K, typename V, V v> |
191 | 194 |
inline ConstMap<K, Const<V, v> > constMap() { |
192 | 195 |
return ConstMap<K, Const<V, v> >(); |
193 | 196 |
} |
194 | 197 |
|
195 | 198 |
|
196 | 199 |
/// Identity map. |
197 | 200 |
|
198 | 201 |
/// This \ref concepts::ReadMap "read-only map" gives back the given |
199 | 202 |
/// key as value without any modification. |
200 | 203 |
/// |
201 | 204 |
/// \sa ConstMap |
202 | 205 |
template <typename T> |
203 | 206 |
class IdentityMap : public MapBase<T, T> { |
204 | 207 |
public: |
205 |
typedef MapBase<T, T> Parent; |
|
206 |
typedef typename Parent::Key Key; |
|
207 |
|
|
208 |
///\e |
|
209 |
typedef T Key; |
|
210 |
///\e |
|
211 |
typedef T Value; |
|
208 | 212 |
|
209 | 213 |
/// Gives back the given value without any modification. |
210 | 214 |
Value operator[](const Key &k) const { |
211 | 215 |
return k; |
212 | 216 |
} |
213 | 217 |
}; |
214 | 218 |
|
215 | 219 |
/// Returns an \c IdentityMap class |
216 | 220 |
|
217 | 221 |
/// This function just returns an \c IdentityMap class. |
218 | 222 |
/// \relates IdentityMap |
219 | 223 |
template<typename T> |
220 | 224 |
inline IdentityMap<T> identityMap() { |
221 | 225 |
return IdentityMap<T>(); |
222 | 226 |
} |
223 | 227 |
|
224 | 228 |
|
225 | 229 |
/// \brief Map for storing values for integer keys from the range |
226 | 230 |
/// <tt>[0..size-1]</tt>. |
227 | 231 |
/// |
228 | 232 |
/// This map is essentially a wrapper for \c std::vector. It assigns |
229 | 233 |
/// values to integer keys from the range <tt>[0..size-1]</tt>. |
230 | 234 |
/// It can be used with some data structures, for example |
231 | 235 |
/// \c UnionFind, \c BinHeap, when the used items are small |
232 | 236 |
/// integers. This map conforms the \ref concepts::ReferenceMap |
233 | 237 |
/// "ReferenceMap" concept. |
234 | 238 |
/// |
235 | 239 |
/// The simplest way of using this map is through the rangeMap() |
236 | 240 |
/// function. |
237 | 241 |
template <typename V> |
238 | 242 |
class RangeMap : public MapBase<int, V> { |
239 | 243 |
template <typename V1> |
240 | 244 |
friend class RangeMap; |
241 | 245 |
private: |
242 | 246 |
|
243 | 247 |
typedef std::vector<V> Vector; |
244 | 248 |
Vector _vector; |
245 | 249 |
|
246 | 250 |
public: |
247 | 251 |
|
248 |
typedef MapBase<int, V> Parent; |
|
249 | 252 |
/// Key type |
250 |
typedef |
|
253 |
typedef int Key; |
|
251 | 254 |
/// Value type |
252 |
typedef |
|
255 |
typedef V Value; |
|
253 | 256 |
/// Reference type |
254 | 257 |
typedef typename Vector::reference Reference; |
255 | 258 |
/// Const reference type |
256 | 259 |
typedef typename Vector::const_reference ConstReference; |
257 | 260 |
|
258 | 261 |
typedef True ReferenceMapTag; |
259 | 262 |
|
260 | 263 |
public: |
261 | 264 |
|
262 | 265 |
/// Constructor with specified default value. |
263 | 266 |
RangeMap(int size = 0, const Value &value = Value()) |
264 | 267 |
: _vector(size, value) {} |
265 | 268 |
|
266 | 269 |
/// Constructs the map from an appropriate \c std::vector. |
267 | 270 |
template <typename V1> |
268 | 271 |
RangeMap(const std::vector<V1>& vector) |
269 | 272 |
: _vector(vector.begin(), vector.end()) {} |
270 | 273 |
|
271 | 274 |
/// Constructs the map from another \c RangeMap. |
272 | 275 |
template <typename V1> |
273 | 276 |
RangeMap(const RangeMap<V1> &c) |
274 | 277 |
: _vector(c._vector.begin(), c._vector.end()) {} |
275 | 278 |
|
276 | 279 |
/// Returns the size of the map. |
277 | 280 |
int size() { |
278 | 281 |
return _vector.size(); |
279 | 282 |
} |
280 | 283 |
|
281 | 284 |
/// Resizes the map. |
282 | 285 |
|
283 | 286 |
/// Resizes the underlying \c std::vector container, so changes the |
284 | 287 |
/// keyset of the map. |
285 | 288 |
/// \param size The new size of the map. The new keyset will be the |
286 | 289 |
/// range <tt>[0..size-1]</tt>. |
287 | 290 |
/// \param value The default value to assign to the new keys. |
288 | 291 |
void resize(int size, const Value &value = Value()) { |
289 | 292 |
_vector.resize(size, value); |
290 | 293 |
} |
291 | 294 |
|
292 | 295 |
private: |
293 | 296 |
|
294 | 297 |
RangeMap& operator=(const RangeMap&); |
295 | 298 |
|
296 | 299 |
public: |
297 | 300 |
|
298 | 301 |
///\e |
299 | 302 |
Reference operator[](const Key &k) { |
300 | 303 |
return _vector[k]; |
... | ... |
@@ -308,117 +311,116 @@ |
308 | 311 |
///\e |
309 | 312 |
void set(const Key &k, const Value &v) { |
310 | 313 |
_vector[k] = v; |
311 | 314 |
} |
312 | 315 |
}; |
313 | 316 |
|
314 | 317 |
/// Returns a \c RangeMap class |
315 | 318 |
|
316 | 319 |
/// This function just returns a \c RangeMap class. |
317 | 320 |
/// \relates RangeMap |
318 | 321 |
template<typename V> |
319 | 322 |
inline RangeMap<V> rangeMap(int size = 0, const V &value = V()) { |
320 | 323 |
return RangeMap<V>(size, value); |
321 | 324 |
} |
322 | 325 |
|
323 | 326 |
/// \brief Returns a \c RangeMap class created from an appropriate |
324 | 327 |
/// \c std::vector |
325 | 328 |
|
326 | 329 |
/// This function just returns a \c RangeMap class created from an |
327 | 330 |
/// appropriate \c std::vector. |
328 | 331 |
/// \relates RangeMap |
329 | 332 |
template<typename V> |
330 | 333 |
inline RangeMap<V> rangeMap(const std::vector<V> &vector) { |
331 | 334 |
return RangeMap<V>(vector); |
332 | 335 |
} |
333 | 336 |
|
334 | 337 |
|
335 | 338 |
/// Map type based on \c std::map |
336 | 339 |
|
337 | 340 |
/// This map is essentially a wrapper for \c std::map with addition |
338 | 341 |
/// that you can specify a default value for the keys that are not |
339 | 342 |
/// stored actually. This value can be different from the default |
340 | 343 |
/// contructed value (i.e. \c %Value()). |
341 | 344 |
/// This type conforms the \ref concepts::ReferenceMap "ReferenceMap" |
342 | 345 |
/// concept. |
343 | 346 |
/// |
344 | 347 |
/// This map is useful if a default value should be assigned to most of |
345 | 348 |
/// the keys and different values should be assigned only to a few |
346 | 349 |
/// keys (i.e. the map is "sparse"). |
347 | 350 |
/// The name of this type also refers to this important usage. |
348 | 351 |
/// |
349 | 352 |
/// Apart form that this map can be used in many other cases since it |
350 | 353 |
/// is based on \c std::map, which is a general associative container. |
351 | 354 |
/// However keep in mind that it is usually not as efficient as other |
352 | 355 |
/// maps. |
353 | 356 |
/// |
354 | 357 |
/// The simplest way of using this map is through the sparseMap() |
355 | 358 |
/// function. |
356 |
template <typename K, typename V, typename |
|
359 |
template <typename K, typename V, typename Comp = std::less<K> > |
|
357 | 360 |
class SparseMap : public MapBase<K, V> { |
358 | 361 |
template <typename K1, typename V1, typename C1> |
359 | 362 |
friend class SparseMap; |
360 | 363 |
public: |
361 | 364 |
|
362 |
typedef MapBase<K, V> Parent; |
|
363 | 365 |
/// Key type |
364 |
typedef |
|
366 |
typedef K Key; |
|
365 | 367 |
/// Value type |
366 |
typedef |
|
368 |
typedef V Value; |
|
367 | 369 |
/// Reference type |
368 | 370 |
typedef Value& Reference; |
369 | 371 |
/// Const reference type |
370 | 372 |
typedef const Value& ConstReference; |
371 | 373 |
|
372 | 374 |
typedef True ReferenceMapTag; |
373 | 375 |
|
374 | 376 |
private: |
375 | 377 |
|
376 |
typedef std::map<K, V, |
|
378 |
typedef std::map<K, V, Comp> Map; |
|
377 | 379 |
Map _map; |
378 | 380 |
Value _value; |
379 | 381 |
|
380 | 382 |
public: |
381 | 383 |
|
382 | 384 |
/// \brief Constructor with specified default value. |
383 | 385 |
SparseMap(const Value &value = Value()) : _value(value) {} |
384 | 386 |
/// \brief Constructs the map from an appropriate \c std::map, and |
385 | 387 |
/// explicitly specifies a default value. |
386 | 388 |
template <typename V1, typename Comp1> |
387 | 389 |
SparseMap(const std::map<Key, V1, Comp1> &map, |
388 | 390 |
const Value &value = Value()) |
389 | 391 |
: _map(map.begin(), map.end()), _value(value) {} |
390 | 392 |
|
391 | 393 |
/// \brief Constructs the map from another \c SparseMap. |
392 | 394 |
template<typename V1, typename Comp1> |
393 | 395 |
SparseMap(const SparseMap<Key, V1, Comp1> &c) |
394 | 396 |
: _map(c._map.begin(), c._map.end()), _value(c._value) {} |
395 | 397 |
|
396 | 398 |
private: |
397 | 399 |
|
398 | 400 |
SparseMap& operator=(const SparseMap&); |
399 | 401 |
|
400 | 402 |
public: |
401 | 403 |
|
402 | 404 |
///\e |
403 | 405 |
Reference operator[](const Key &k) { |
404 | 406 |
typename Map::iterator it = _map.lower_bound(k); |
405 | 407 |
if (it != _map.end() && !_map.key_comp()(k, it->first)) |
406 | 408 |
return it->second; |
407 | 409 |
else |
408 | 410 |
return _map.insert(it, std::make_pair(k, _value))->second; |
409 | 411 |
} |
410 | 412 |
|
411 | 413 |
///\e |
412 | 414 |
ConstReference operator[](const Key &k) const { |
413 | 415 |
typename Map::const_iterator it = _map.find(k); |
414 | 416 |
if (it != _map.end()) |
415 | 417 |
return it->second; |
416 | 418 |
else |
417 | 419 |
return _value; |
418 | 420 |
} |
419 | 421 |
|
420 | 422 |
///\e |
421 | 423 |
void set(const Key &k, const Value &v) { |
422 | 424 |
typename Map::iterator it = _map.lower_bound(k); |
423 | 425 |
if (it != _map.end() && !_map.key_comp()(k, it->first)) |
424 | 426 |
it->second = v; |
... | ... |
@@ -444,1712 +446,1760 @@ |
444 | 446 |
} |
445 | 447 |
|
446 | 448 |
template<typename K, typename V> |
447 | 449 |
inline SparseMap<K, V, std::less<K> > sparseMap(const V& value = V()) { |
448 | 450 |
return SparseMap<K, V, std::less<K> >(value); |
449 | 451 |
} |
450 | 452 |
|
451 | 453 |
/// \brief Returns a \c SparseMap class created from an appropriate |
452 | 454 |
/// \c std::map |
453 | 455 |
|
454 | 456 |
/// This function just returns a \c SparseMap class created from an |
455 | 457 |
/// appropriate \c std::map. |
456 | 458 |
/// \relates SparseMap |
457 | 459 |
template<typename K, typename V, typename Compare> |
458 | 460 |
inline SparseMap<K, V, Compare> |
459 | 461 |
sparseMap(const std::map<K, V, Compare> &map, const V& value = V()) |
460 | 462 |
{ |
461 | 463 |
return SparseMap<K, V, Compare>(map, value); |
462 | 464 |
} |
463 | 465 |
|
464 | 466 |
/// @} |
465 | 467 |
|
466 | 468 |
/// \addtogroup map_adaptors |
467 | 469 |
/// @{ |
468 | 470 |
|
469 | 471 |
/// Composition of two maps |
470 | 472 |
|
471 | 473 |
/// This \ref concepts::ReadMap "read-only map" returns the |
472 | 474 |
/// composition of two given maps. That is to say, if \c m1 is of |
473 | 475 |
/// type \c M1 and \c m2 is of \c M2, then for |
474 | 476 |
/// \code |
475 | 477 |
/// ComposeMap<M1, M2> cm(m1,m2); |
476 | 478 |
/// \endcode |
477 | 479 |
/// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>. |
478 | 480 |
/// |
479 | 481 |
/// The \c Key type of the map is inherited from \c M2 and the |
480 | 482 |
/// \c Value type is from \c M1. |
481 | 483 |
/// \c M2::Value must be convertible to \c M1::Key. |
482 | 484 |
/// |
483 | 485 |
/// The simplest way of using this map is through the composeMap() |
484 | 486 |
/// function. |
485 | 487 |
/// |
486 | 488 |
/// \sa CombineMap |
487 | 489 |
template <typename M1, typename M2> |
488 | 490 |
class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> { |
489 | 491 |
const M1 &_m1; |
490 | 492 |
const M2 &_m2; |
491 | 493 |
public: |
492 |
typedef MapBase<typename M2::Key, typename M1::Value> Parent; |
|
493 |
typedef typename Parent::Key Key; |
|
494 |
|
|
494 |
///\e |
|
495 |
typedef typename M2::Key Key; |
|
496 |
///\e |
|
497 |
typedef typename M1::Value Value; |
|
495 | 498 |
|
496 | 499 |
/// Constructor |
497 | 500 |
ComposeMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
498 | 501 |
|
499 |
/// |
|
502 |
///\e |
|
500 | 503 |
typename MapTraits<M1>::ConstReturnValue |
501 | 504 |
operator[](const Key &k) const { return _m1[_m2[k]]; } |
502 | 505 |
}; |
503 | 506 |
|
504 | 507 |
/// Returns a \c ComposeMap class |
505 | 508 |
|
506 | 509 |
/// This function just returns a \c ComposeMap class. |
507 | 510 |
/// |
508 | 511 |
/// If \c m1 and \c m2 are maps and the \c Value type of \c m2 is |
509 | 512 |
/// convertible to the \c Key of \c m1, then <tt>composeMap(m1,m2)[x]</tt> |
510 | 513 |
/// will be equal to <tt>m1[m2[x]]</tt>. |
511 | 514 |
/// |
512 | 515 |
/// \relates ComposeMap |
513 | 516 |
template <typename M1, typename M2> |
514 | 517 |
inline ComposeMap<M1, M2> composeMap(const M1 &m1, const M2 &m2) { |
515 | 518 |
return ComposeMap<M1, M2>(m1, m2); |
516 | 519 |
} |
517 | 520 |
|
518 | 521 |
|
519 | 522 |
/// Combination of two maps using an STL (binary) functor. |
520 | 523 |
|
521 | 524 |
/// This \ref concepts::ReadMap "read-only map" takes two maps and a |
522 | 525 |
/// binary functor and returns the combination of the two given maps |
523 | 526 |
/// using the functor. |
524 | 527 |
/// That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2 |
525 | 528 |
/// and \c f is of \c F, then for |
526 | 529 |
/// \code |
527 | 530 |
/// CombineMap<M1,M2,F,V> cm(m1,m2,f); |
528 | 531 |
/// \endcode |
529 | 532 |
/// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt>. |
530 | 533 |
/// |
531 | 534 |
/// The \c Key type of the map is inherited from \c M1 (\c M1::Key |
532 | 535 |
/// must be convertible to \c M2::Key) and the \c Value type is \c V. |
533 | 536 |
/// \c M2::Value and \c M1::Value must be convertible to the |
534 | 537 |
/// corresponding input parameter of \c F and the return type of \c F |
535 | 538 |
/// must be convertible to \c V. |
536 | 539 |
/// |
537 | 540 |
/// The simplest way of using this map is through the combineMap() |
538 | 541 |
/// function. |
539 | 542 |
/// |
540 | 543 |
/// \sa ComposeMap |
541 | 544 |
template<typename M1, typename M2, typename F, |
542 | 545 |
typename V = typename F::result_type> |
543 | 546 |
class CombineMap : public MapBase<typename M1::Key, V> { |
544 | 547 |
const M1 &_m1; |
545 | 548 |
const M2 &_m2; |
546 | 549 |
F _f; |
547 | 550 |
public: |
548 |
typedef MapBase<typename M1::Key, V> Parent; |
|
549 |
typedef typename Parent::Key Key; |
|
550 |
|
|
551 |
///\e |
|
552 |
typedef typename M1::Key Key; |
|
553 |
///\e |
|
554 |
typedef V Value; |
|
551 | 555 |
|
552 | 556 |
/// Constructor |
553 | 557 |
CombineMap(const M1 &m1, const M2 &m2, const F &f = F()) |
554 | 558 |
: _m1(m1), _m2(m2), _f(f) {} |
555 |
/// |
|
559 |
///\e |
|
556 | 560 |
Value operator[](const Key &k) const { return _f(_m1[k],_m2[k]); } |
557 | 561 |
}; |
558 | 562 |
|
559 | 563 |
/// Returns a \c CombineMap class |
560 | 564 |
|
561 | 565 |
/// This function just returns a \c CombineMap class. |
562 | 566 |
/// |
563 | 567 |
/// For example, if \c m1 and \c m2 are both maps with \c double |
564 | 568 |
/// values, then |
565 | 569 |
/// \code |
566 | 570 |
/// combineMap(m1,m2,std::plus<double>()) |
567 | 571 |
/// \endcode |
568 | 572 |
/// is equivalent to |
569 | 573 |
/// \code |
570 | 574 |
/// addMap(m1,m2) |
571 | 575 |
/// \endcode |
572 | 576 |
/// |
573 | 577 |
/// This function is specialized for adaptable binary function |
574 | 578 |
/// classes and C++ functions. |
575 | 579 |
/// |
576 | 580 |
/// \relates CombineMap |
577 | 581 |
template<typename M1, typename M2, typename F, typename V> |
578 | 582 |
inline CombineMap<M1, M2, F, V> |
579 | 583 |
combineMap(const M1 &m1, const M2 &m2, const F &f) { |
580 | 584 |
return CombineMap<M1, M2, F, V>(m1,m2,f); |
581 | 585 |
} |
582 | 586 |
|
583 | 587 |
template<typename M1, typename M2, typename F> |
584 | 588 |
inline CombineMap<M1, M2, F, typename F::result_type> |
585 | 589 |
combineMap(const M1 &m1, const M2 &m2, const F &f) { |
586 | 590 |
return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f); |
587 | 591 |
} |
588 | 592 |
|
589 | 593 |
template<typename M1, typename M2, typename K1, typename K2, typename V> |
590 | 594 |
inline CombineMap<M1, M2, V (*)(K1, K2), V> |
591 | 595 |
combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) { |
592 | 596 |
return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f); |
593 | 597 |
} |
594 | 598 |
|
595 | 599 |
|
596 | 600 |
/// Converts an STL style (unary) functor to a map |
597 | 601 |
|
598 | 602 |
/// This \ref concepts::ReadMap "read-only map" returns the value |
599 | 603 |
/// of a given functor. Actually, it just wraps the functor and |
600 | 604 |
/// provides the \c Key and \c Value typedefs. |
601 | 605 |
/// |
602 | 606 |
/// Template parameters \c K and \c V will become its \c Key and |
603 | 607 |
/// \c Value. In most cases they have to be given explicitly because |
604 | 608 |
/// a functor typically does not provide \c argument_type and |
605 | 609 |
/// \c result_type typedefs. |
606 | 610 |
/// Parameter \c F is the type of the used functor. |
607 | 611 |
/// |
608 | 612 |
/// The simplest way of using this map is through the functorToMap() |
609 | 613 |
/// function. |
610 | 614 |
/// |
611 | 615 |
/// \sa MapToFunctor |
612 | 616 |
template<typename F, |
613 | 617 |
typename K = typename F::argument_type, |
614 | 618 |
typename V = typename F::result_type> |
615 | 619 |
class FunctorToMap : public MapBase<K, V> { |
616 | 620 |
F _f; |
617 | 621 |
public: |
618 |
typedef MapBase<K, V> Parent; |
|
619 |
typedef typename Parent::Key Key; |
|
620 |
|
|
622 |
///\e |
|
623 |
typedef K Key; |
|
624 |
///\e |
|
625 |
typedef V Value; |
|
621 | 626 |
|
622 | 627 |
/// Constructor |
623 | 628 |
FunctorToMap(const F &f = F()) : _f(f) {} |
624 |
/// |
|
629 |
///\e |
|
625 | 630 |
Value operator[](const Key &k) const { return _f(k); } |
626 | 631 |
}; |
627 | 632 |
|
628 | 633 |
/// Returns a \c FunctorToMap class |
629 | 634 |
|
630 | 635 |
/// This function just returns a \c FunctorToMap class. |
631 | 636 |
/// |
632 | 637 |
/// This function is specialized for adaptable binary function |
633 | 638 |
/// classes and C++ functions. |
634 | 639 |
/// |
635 | 640 |
/// \relates FunctorToMap |
636 | 641 |
template<typename K, typename V, typename F> |
637 | 642 |
inline FunctorToMap<F, K, V> functorToMap(const F &f) { |
638 | 643 |
return FunctorToMap<F, K, V>(f); |
639 | 644 |
} |
640 | 645 |
|
641 | 646 |
template <typename F> |
642 | 647 |
inline FunctorToMap<F, typename F::argument_type, typename F::result_type> |
643 | 648 |
functorToMap(const F &f) |
644 | 649 |
{ |
645 | 650 |
return FunctorToMap<F, typename F::argument_type, |
646 | 651 |
typename F::result_type>(f); |
647 | 652 |
} |
648 | 653 |
|
649 | 654 |
template <typename K, typename V> |
650 | 655 |
inline FunctorToMap<V (*)(K), K, V> functorToMap(V (*f)(K)) { |
651 | 656 |
return FunctorToMap<V (*)(K), K, V>(f); |
652 | 657 |
} |
653 | 658 |
|
654 | 659 |
|
655 | 660 |
/// Converts a map to an STL style (unary) functor |
656 | 661 |
|
657 | 662 |
/// This class converts a map to an STL style (unary) functor. |
658 | 663 |
/// That is it provides an <tt>operator()</tt> to read its values. |
659 | 664 |
/// |
660 | 665 |
/// For the sake of convenience it also works as a usual |
661 | 666 |
/// \ref concepts::ReadMap "readable map", i.e. <tt>operator[]</tt> |
662 | 667 |
/// and the \c Key and \c Value typedefs also exist. |
663 | 668 |
/// |
664 | 669 |
/// The simplest way of using this map is through the mapToFunctor() |
665 | 670 |
/// function. |
666 | 671 |
/// |
667 | 672 |
///\sa FunctorToMap |
668 | 673 |
template <typename M> |
669 | 674 |
class MapToFunctor : public MapBase<typename M::Key, typename M::Value> { |
670 | 675 |
const M &_m; |
671 | 676 |
public: |
672 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
|
673 |
typedef typename Parent::Key Key; |
|
674 |
typedef typename Parent::Value Value; |
|
675 |
|
|
676 |
typedef typename Parent::Key argument_type; |
|
677 |
typedef typename Parent::Value result_type; |
|
677 |
///\e |
|
678 |
typedef typename M::Key Key; |
|
679 |
///\e |
|
680 |
typedef typename M::Value Value; |
|
681 |
|
|
682 |
typedef typename M::Key argument_type; |
|
683 |
typedef typename M::Value result_type; |
|
678 | 684 |
|
679 | 685 |
/// Constructor |
680 | 686 |
MapToFunctor(const M &m) : _m(m) {} |
681 |
/// |
|
687 |
///\e |
|
682 | 688 |
Value operator()(const Key &k) const { return _m[k]; } |
683 |
/// |
|
689 |
///\e |
|
684 | 690 |
Value operator[](const Key &k) const { return _m[k]; } |
685 | 691 |
}; |
686 | 692 |
|
687 | 693 |
/// Returns a \c MapToFunctor class |
688 | 694 |
|
689 | 695 |
/// This function just returns a \c MapToFunctor class. |
690 | 696 |
/// \relates MapToFunctor |
691 | 697 |
template<typename M> |
692 | 698 |
inline MapToFunctor<M> mapToFunctor(const M &m) { |
693 | 699 |
return MapToFunctor<M>(m); |
694 | 700 |
} |
695 | 701 |
|
696 | 702 |
|
697 | 703 |
/// \brief Map adaptor to convert the \c Value type of a map to |
698 | 704 |
/// another type using the default conversion. |
699 | 705 |
|
700 | 706 |
/// Map adaptor to convert the \c Value type of a \ref concepts::ReadMap |
701 | 707 |
/// "readable map" to another type using the default conversion. |
702 | 708 |
/// The \c Key type of it is inherited from \c M and the \c Value |
703 | 709 |
/// type is \c V. |
704 | 710 |
/// This type conforms the \ref concepts::ReadMap "ReadMap" concept. |
705 | 711 |
/// |
706 | 712 |
/// The simplest way of using this map is through the convertMap() |
707 | 713 |
/// function. |
708 | 714 |
template <typename M, typename V> |
709 | 715 |
class ConvertMap : public MapBase<typename M::Key, V> { |
710 | 716 |
const M &_m; |
711 | 717 |
public: |
712 |
typedef MapBase<typename M::Key, V> Parent; |
|
713 |
typedef typename Parent::Key Key; |
|
714 |
|
|
718 |
///\e |
|
719 |
typedef typename M::Key Key; |
|
720 |
///\e |
|
721 |
typedef V Value; |
|
715 | 722 |
|
716 | 723 |
/// Constructor |
717 | 724 |
|
718 | 725 |
/// Constructor. |
719 | 726 |
/// \param m The underlying map. |
720 | 727 |
ConvertMap(const M &m) : _m(m) {} |
721 | 728 |
|
722 |
/// |
|
729 |
///\e |
|
723 | 730 |
Value operator[](const Key &k) const { return _m[k]; } |
724 | 731 |
}; |
725 | 732 |
|
726 | 733 |
/// Returns a \c ConvertMap class |
727 | 734 |
|
728 | 735 |
/// This function just returns a \c ConvertMap class. |
729 | 736 |
/// \relates ConvertMap |
730 | 737 |
template<typename V, typename M> |
731 | 738 |
inline ConvertMap<M, V> convertMap(const M &map) { |
732 | 739 |
return ConvertMap<M, V>(map); |
733 | 740 |
} |
734 | 741 |
|
735 | 742 |
|
736 | 743 |
/// Applies all map setting operations to two maps |
737 | 744 |
|
738 | 745 |
/// This map has two \ref concepts::WriteMap "writable map" parameters |
739 | 746 |
/// and each write request will be passed to both of them. |
740 | 747 |
/// If \c M1 is also \ref concepts::ReadMap "readable", then the read |
741 | 748 |
/// operations will return the corresponding values of \c M1. |
742 | 749 |
/// |
743 | 750 |
/// The \c Key and \c Value types are inherited from \c M1. |
744 | 751 |
/// The \c Key and \c Value of \c M2 must be convertible from those |
745 | 752 |
/// of \c M1. |
746 | 753 |
/// |
747 | 754 |
/// The simplest way of using this map is through the forkMap() |
748 | 755 |
/// function. |
749 | 756 |
template<typename M1, typename M2> |
750 | 757 |
class ForkMap : public MapBase<typename M1::Key, typename M1::Value> { |
751 | 758 |
M1 &_m1; |
752 | 759 |
M2 &_m2; |
753 | 760 |
public: |
754 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
|
755 |
typedef typename Parent::Key Key; |
|
756 |
|
|
761 |
///\e |
|
762 |
typedef typename M1::Key Key; |
|
763 |
///\e |
|
764 |
typedef typename M1::Value Value; |
|
757 | 765 |
|
758 | 766 |
/// Constructor |
759 | 767 |
ForkMap(M1 &m1, M2 &m2) : _m1(m1), _m2(m2) {} |
760 | 768 |
/// Returns the value associated with the given key in the first map. |
761 | 769 |
Value operator[](const Key &k) const { return _m1[k]; } |
762 | 770 |
/// Sets the value associated with the given key in both maps. |
763 | 771 |
void set(const Key &k, const Value &v) { _m1.set(k,v); _m2.set(k,v); } |
764 | 772 |
}; |
765 | 773 |
|
766 | 774 |
/// Returns a \c ForkMap class |
767 | 775 |
|
768 | 776 |
/// This function just returns a \c ForkMap class. |
769 | 777 |
/// \relates ForkMap |
770 | 778 |
template <typename M1, typename M2> |
771 | 779 |
inline ForkMap<M1,M2> forkMap(M1 &m1, M2 &m2) { |
772 | 780 |
return ForkMap<M1,M2>(m1,m2); |
773 | 781 |
} |
774 | 782 |
|
775 | 783 |
|
776 | 784 |
/// Sum of two maps |
777 | 785 |
|
778 | 786 |
/// This \ref concepts::ReadMap "read-only map" returns the sum |
779 | 787 |
/// of the values of the two given maps. |
780 | 788 |
/// Its \c Key and \c Value types are inherited from \c M1. |
781 | 789 |
/// The \c Key and \c Value of \c M2 must be convertible to those of |
782 | 790 |
/// \c M1. |
783 | 791 |
/// |
784 | 792 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
785 | 793 |
/// \code |
786 | 794 |
/// AddMap<M1,M2> am(m1,m2); |
787 | 795 |
/// \endcode |
788 | 796 |
/// <tt>am[x]</tt> will be equal to <tt>m1[x]+m2[x]</tt>. |
789 | 797 |
/// |
790 | 798 |
/// The simplest way of using this map is through the addMap() |
791 | 799 |
/// function. |
792 | 800 |
/// |
793 | 801 |
/// \sa SubMap, MulMap, DivMap |
794 | 802 |
/// \sa ShiftMap, ShiftWriteMap |
795 | 803 |
template<typename M1, typename M2> |
796 | 804 |
class AddMap : public MapBase<typename M1::Key, typename M1::Value> { |
797 | 805 |
const M1 &_m1; |
798 | 806 |
const M2 &_m2; |
799 | 807 |
public: |
800 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
|
801 |
typedef typename Parent::Key Key; |
|
802 |
|
|
808 |
///\e |
|
809 |
typedef typename M1::Key Key; |
|
810 |
///\e |
|
811 |
typedef typename M1::Value Value; |
|
803 | 812 |
|
804 | 813 |
/// Constructor |
805 | 814 |
AddMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
806 |
/// |
|
815 |
///\e |
|
807 | 816 |
Value operator[](const Key &k) const { return _m1[k]+_m2[k]; } |
808 | 817 |
}; |
809 | 818 |
|
810 | 819 |
/// Returns an \c AddMap class |
811 | 820 |
|
812 | 821 |
/// This function just returns an \c AddMap class. |
813 | 822 |
/// |
814 | 823 |
/// For example, if \c m1 and \c m2 are both maps with \c double |
815 | 824 |
/// values, then <tt>addMap(m1,m2)[x]</tt> will be equal to |
816 | 825 |
/// <tt>m1[x]+m2[x]</tt>. |
817 | 826 |
/// |
818 | 827 |
/// \relates AddMap |
819 | 828 |
template<typename M1, typename M2> |
820 | 829 |
inline AddMap<M1, M2> addMap(const M1 &m1, const M2 &m2) { |
821 | 830 |
return AddMap<M1, M2>(m1,m2); |
822 | 831 |
} |
823 | 832 |
|
824 | 833 |
|
825 | 834 |
/// Difference of two maps |
826 | 835 |
|
827 | 836 |
/// This \ref concepts::ReadMap "read-only map" returns the difference |
828 | 837 |
/// of the values of the two given maps. |
829 | 838 |
/// Its \c Key and \c Value types are inherited from \c M1. |
830 | 839 |
/// The \c Key and \c Value of \c M2 must be convertible to those of |
831 | 840 |
/// \c M1. |
832 | 841 |
/// |
833 | 842 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
834 | 843 |
/// \code |
835 | 844 |
/// SubMap<M1,M2> sm(m1,m2); |
836 | 845 |
/// \endcode |
837 | 846 |
/// <tt>sm[x]</tt> will be equal to <tt>m1[x]-m2[x]</tt>. |
838 | 847 |
/// |
839 | 848 |
/// The simplest way of using this map is through the subMap() |
840 | 849 |
/// function. |
841 | 850 |
/// |
842 | 851 |
/// \sa AddMap, MulMap, DivMap |
843 | 852 |
template<typename M1, typename M2> |
844 | 853 |
class SubMap : public MapBase<typename M1::Key, typename M1::Value> { |
845 | 854 |
const M1 &_m1; |
846 | 855 |
const M2 &_m2; |
847 | 856 |
public: |
848 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
|
849 |
typedef typename Parent::Key Key; |
|
850 |
|
|
857 |
///\e |
|
858 |
typedef typename M1::Key Key; |
|
859 |
///\e |
|
860 |
typedef typename M1::Value Value; |
|
851 | 861 |
|
852 | 862 |
/// Constructor |
853 | 863 |
SubMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
854 |
/// |
|
864 |
///\e |
|
855 | 865 |
Value operator[](const Key &k) const { return _m1[k]-_m2[k]; } |
856 | 866 |
}; |
857 | 867 |
|
858 | 868 |
/// Returns a \c SubMap class |
859 | 869 |
|
860 | 870 |
/// This function just returns a \c SubMap class. |
861 | 871 |
/// |
862 | 872 |
/// For example, if \c m1 and \c m2 are both maps with \c double |
863 | 873 |
/// values, then <tt>subMap(m1,m2)[x]</tt> will be equal to |
864 | 874 |
/// <tt>m1[x]-m2[x]</tt>. |
865 | 875 |
/// |
866 | 876 |
/// \relates SubMap |
867 | 877 |
template<typename M1, typename M2> |
868 | 878 |
inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) { |
869 | 879 |
return SubMap<M1, M2>(m1,m2); |
870 | 880 |
} |
871 | 881 |
|
872 | 882 |
|
873 | 883 |
/// Product of two maps |
874 | 884 |
|
875 | 885 |
/// This \ref concepts::ReadMap "read-only map" returns the product |
876 | 886 |
/// of the values of the two given maps. |
877 | 887 |
/// Its \c Key and \c Value types are inherited from \c M1. |
878 | 888 |
/// The \c Key and \c Value of \c M2 must be convertible to those of |
879 | 889 |
/// \c M1. |
880 | 890 |
/// |
881 | 891 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
882 | 892 |
/// \code |
883 | 893 |
/// MulMap<M1,M2> mm(m1,m2); |
884 | 894 |
/// \endcode |
885 | 895 |
/// <tt>mm[x]</tt> will be equal to <tt>m1[x]*m2[x]</tt>. |
886 | 896 |
/// |
887 | 897 |
/// The simplest way of using this map is through the mulMap() |
888 | 898 |
/// function. |
889 | 899 |
/// |
890 | 900 |
/// \sa AddMap, SubMap, DivMap |
891 | 901 |
/// \sa ScaleMap, ScaleWriteMap |
892 | 902 |
template<typename M1, typename M2> |
893 | 903 |
class MulMap : public MapBase<typename M1::Key, typename M1::Value> { |
894 | 904 |
const M1 &_m1; |
895 | 905 |
const M2 &_m2; |
896 | 906 |
public: |
897 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
|
898 |
typedef typename Parent::Key Key; |
|
899 |
|
|
907 |
///\e |
|
908 |
typedef typename M1::Key Key; |
|
909 |
///\e |
|
910 |
typedef typename M1::Value Value; |
|
900 | 911 |
|
901 | 912 |
/// Constructor |
902 | 913 |
MulMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {} |
903 |
/// |
|
914 |
///\e |
|
904 | 915 |
Value operator[](const Key &k) const { return _m1[k]*_m2[k]; } |
905 | 916 |
}; |
906 | 917 |
|
907 | 918 |
/// Returns a \c MulMap class |
908 | 919 |
|
909 | 920 |
/// This function just returns a \c MulMap class. |
910 | 921 |
/// |
911 | 922 |
/// For example, if \c m1 and \c m2 are both maps with \c double |
912 | 923 |
/// values, then <tt>mulMap(m1,m2)[x]</tt> will be equal to |
913 | 924 |
/// <tt>m1[x]*m2[x]</tt>. |
914 | 925 |
/// |
915 | 926 |
/// \relates MulMap |
916 | 927 |
template<typename M1, typename M2> |
917 | 928 |
inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) { |
918 | 929 |
return MulMap<M1, M2>(m1,m2); |
919 | 930 |
} |
920 | 931 |
|
921 | 932 |
|
922 | 933 |
/// Quotient of two maps |
923 | 934 |
|
924 | 935 |
/// This \ref concepts::ReadMap "read-only map" returns the quotient |
925 | 936 |
/// of the values of the two given maps. |
926 | 937 |
/// Its \c Key and \c Value types are inherited from \c M1. |
927 | 938 |
/// The \c Key and \c Value of \c M2 must be convertible to those of |
928 | 939 |
/// \c M1. |
929 | 940 |
/// |
930 | 941 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
931 | 942 |
/// \code |
932 | 943 |
/// DivMap<M1,M2> dm(m1,m2); |
933 | 944 |
/// \endcode |
934 | 945 |
/// <tt>dm[x]</tt> will be equal to <tt>m1[x]/m2[x]</tt>. |
935 | 946 |
/// |
936 | 947 |
/// The simplest way of using this map is through the divMap() |
937 | 948 |
/// function. |
938 | 949 |
/// |
939 | 950 |
/// \sa AddMap, SubMap, MulMap |
940 | 951 |
template<typename M1, typename M2> |
941 | 952 |
class DivMap : public MapBase<typename M1::Key, typename M1::Value> { |
942 | 953 |
const M1 &_m1; |
943 | 954 |
const M2 &_m2; |
944 | 955 |
public: |
945 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
|
946 |
typedef typename Parent::Key Key; |
|
947 |
|
|
956 |
///\e |
|
957 |
typedef typename M1::Key Key; |
|
958 |
///\e |
|
959 |
typedef typename M1::Value Value; |
|
948 | 960 |
|
949 | 961 |
/// Constructor |
950 | 962 |
DivMap(const M1 &m1,const M2 &m2) : _m1(m1), _m2(m2) {} |
951 |
/// |
|
963 |
///\e |
|
952 | 964 |
Value operator[](const Key &k) const { return _m1[k]/_m2[k]; } |
953 | 965 |
}; |
954 | 966 |
|
955 | 967 |
/// Returns a \c DivMap class |
956 | 968 |
|
957 | 969 |
/// This function just returns a \c DivMap class. |
958 | 970 |
/// |
959 | 971 |
/// For example, if \c m1 and \c m2 are both maps with \c double |
960 | 972 |
/// values, then <tt>divMap(m1,m2)[x]</tt> will be equal to |
961 | 973 |
/// <tt>m1[x]/m2[x]</tt>. |
962 | 974 |
/// |
963 | 975 |
/// \relates DivMap |
964 | 976 |
template<typename M1, typename M2> |
965 | 977 |
inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) { |
966 | 978 |
return DivMap<M1, M2>(m1,m2); |
967 | 979 |
} |
968 | 980 |
|
969 | 981 |
|
970 | 982 |
/// Shifts a map with a constant. |
971 | 983 |
|
972 | 984 |
/// This \ref concepts::ReadMap "read-only map" returns the sum of |
973 | 985 |
/// the given map and a constant value (i.e. it shifts the map with |
974 | 986 |
/// the constant). Its \c Key and \c Value are inherited from \c M. |
975 | 987 |
/// |
976 | 988 |
/// Actually, |
977 | 989 |
/// \code |
978 | 990 |
/// ShiftMap<M> sh(m,v); |
979 | 991 |
/// \endcode |
980 | 992 |
/// is equivalent to |
981 | 993 |
/// \code |
982 | 994 |
/// ConstMap<M::Key, M::Value> cm(v); |
983 | 995 |
/// AddMap<M, ConstMap<M::Key, M::Value> > sh(m,cm); |
984 | 996 |
/// \endcode |
985 | 997 |
/// |
986 | 998 |
/// The simplest way of using this map is through the shiftMap() |
987 | 999 |
/// function. |
988 | 1000 |
/// |
989 | 1001 |
/// \sa ShiftWriteMap |
990 | 1002 |
template<typename M, typename C = typename M::Value> |
991 | 1003 |
class ShiftMap : public MapBase<typename M::Key, typename M::Value> { |
992 | 1004 |
const M &_m; |
993 | 1005 |
C _v; |
994 | 1006 |
public: |
995 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
|
996 |
typedef typename Parent::Key Key; |
|
997 |
|
|
1007 |
///\e |
|
1008 |
typedef typename M::Key Key; |
|
1009 |
///\e |
|
1010 |
typedef typename M::Value Value; |
|
998 | 1011 |
|
999 | 1012 |
/// Constructor |
1000 | 1013 |
|
1001 | 1014 |
/// Constructor. |
1002 | 1015 |
/// \param m The undelying map. |
1003 | 1016 |
/// \param v The constant value. |
1004 | 1017 |
ShiftMap(const M &m, const C &v) : _m(m), _v(v) {} |
1005 |
/// |
|
1018 |
///\e |
|
1006 | 1019 |
Value operator[](const Key &k) const { return _m[k]+_v; } |
1007 | 1020 |
}; |
1008 | 1021 |
|
1009 | 1022 |
/// Shifts a map with a constant (read-write version). |
1010 | 1023 |
|
1011 | 1024 |
/// This \ref concepts::ReadWriteMap "read-write map" returns the sum |
1012 | 1025 |
/// of the given map and a constant value (i.e. it shifts the map with |
1013 | 1026 |
/// the constant). Its \c Key and \c Value are inherited from \c M. |
1014 | 1027 |
/// It makes also possible to write the map. |
1015 | 1028 |
/// |
1016 | 1029 |
/// The simplest way of using this map is through the shiftWriteMap() |
1017 | 1030 |
/// function. |
1018 | 1031 |
/// |
1019 | 1032 |
/// \sa ShiftMap |
1020 | 1033 |
template<typename M, typename C = typename M::Value> |
1021 | 1034 |
class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> { |
1022 | 1035 |
M &_m; |
1023 | 1036 |
C _v; |
1024 | 1037 |
public: |
1025 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
|
1026 |
typedef typename Parent::Key Key; |
|
1027 |
|
|
1038 |
///\e |
|
1039 |
typedef typename M::Key Key; |
|
1040 |
///\e |
|
1041 |
typedef typename M::Value Value; |
|
1028 | 1042 |
|
1029 | 1043 |
/// Constructor |
1030 | 1044 |
|
1031 | 1045 |
/// Constructor. |
1032 | 1046 |
/// \param m The undelying map. |
1033 | 1047 |
/// \param v The constant value. |
1034 | 1048 |
ShiftWriteMap(M &m, const C &v) : _m(m), _v(v) {} |
1035 |
/// |
|
1049 |
///\e |
|
1036 | 1050 |
Value operator[](const Key &k) const { return _m[k]+_v; } |
1037 |
/// |
|
1051 |
///\e |
|
1038 | 1052 |
void set(const Key &k, const Value &v) { _m.set(k, v-_v); } |
1039 | 1053 |
}; |
1040 | 1054 |
|
1041 | 1055 |
/// Returns a \c ShiftMap class |
1042 | 1056 |
|
1043 | 1057 |
/// This function just returns a \c ShiftMap class. |
1044 | 1058 |
/// |
1045 | 1059 |
/// For example, if \c m is a map with \c double values and \c v is |
1046 | 1060 |
/// \c double, then <tt>shiftMap(m,v)[x]</tt> will be equal to |
1047 | 1061 |
/// <tt>m[x]+v</tt>. |
1048 | 1062 |
/// |
1049 | 1063 |
/// \relates ShiftMap |
1050 | 1064 |
template<typename M, typename C> |
1051 | 1065 |
inline ShiftMap<M, C> shiftMap(const M &m, const C &v) { |
1052 | 1066 |
return ShiftMap<M, C>(m,v); |
1053 | 1067 |
} |
1054 | 1068 |
|
1055 | 1069 |
/// Returns a \c ShiftWriteMap class |
1056 | 1070 |
|
1057 | 1071 |
/// This function just returns a \c ShiftWriteMap class. |
1058 | 1072 |
/// |
1059 | 1073 |
/// For example, if \c m is a map with \c double values and \c v is |
1060 | 1074 |
/// \c double, then <tt>shiftWriteMap(m,v)[x]</tt> will be equal to |
1061 | 1075 |
/// <tt>m[x]+v</tt>. |
1062 | 1076 |
/// Moreover it makes also possible to write the map. |
1063 | 1077 |
/// |
1064 | 1078 |
/// \relates ShiftWriteMap |
1065 | 1079 |
template<typename M, typename C> |
1066 | 1080 |
inline ShiftWriteMap<M, C> shiftWriteMap(M &m, const C &v) { |
1067 | 1081 |
return ShiftWriteMap<M, C>(m,v); |
1068 | 1082 |
} |
1069 | 1083 |
|
1070 | 1084 |
|
1071 | 1085 |
/// Scales a map with a constant. |
1072 | 1086 |
|
1073 | 1087 |
/// This \ref concepts::ReadMap "read-only map" returns the value of |
1074 | 1088 |
/// the given map multiplied from the left side with a constant value. |
1075 | 1089 |
/// Its \c Key and \c Value are inherited from \c M. |
1076 | 1090 |
/// |
1077 | 1091 |
/// Actually, |
1078 | 1092 |
/// \code |
1079 | 1093 |
/// ScaleMap<M> sc(m,v); |
1080 | 1094 |
/// \endcode |
1081 | 1095 |
/// is equivalent to |
1082 | 1096 |
/// \code |
1083 | 1097 |
/// ConstMap<M::Key, M::Value> cm(v); |
1084 | 1098 |
/// MulMap<ConstMap<M::Key, M::Value>, M> sc(cm,m); |
1085 | 1099 |
/// \endcode |
1086 | 1100 |
/// |
1087 | 1101 |
/// The simplest way of using this map is through the scaleMap() |
1088 | 1102 |
/// function. |
1089 | 1103 |
/// |
1090 | 1104 |
/// \sa ScaleWriteMap |
1091 | 1105 |
template<typename M, typename C = typename M::Value> |
1092 | 1106 |
class ScaleMap : public MapBase<typename M::Key, typename M::Value> { |
1093 | 1107 |
const M &_m; |
1094 | 1108 |
C _v; |
1095 | 1109 |
public: |
1096 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
|
1097 |
typedef typename Parent::Key Key; |
|
1098 |
|
|
1110 |
///\e |
|
1111 |
typedef typename M::Key Key; |
|
1112 |
///\e |
|
1113 |
typedef typename M::Value Value; |
|
1099 | 1114 |
|
1100 | 1115 |
/// Constructor |
1101 | 1116 |
|
1102 | 1117 |
/// Constructor. |
1103 | 1118 |
/// \param m The undelying map. |
1104 | 1119 |
/// \param v The constant value. |
1105 | 1120 |
ScaleMap(const M &m, const C &v) : _m(m), _v(v) {} |
1106 |
/// |
|
1121 |
///\e |
|
1107 | 1122 |
Value operator[](const Key &k) const { return _v*_m[k]; } |
1108 | 1123 |
}; |
1109 | 1124 |
|
1110 | 1125 |
/// Scales a map with a constant (read-write version). |
1111 | 1126 |
|
1112 | 1127 |
/// This \ref concepts::ReadWriteMap "read-write map" returns the value of |
1113 | 1128 |
/// the given map multiplied from the left side with a constant value. |
1114 | 1129 |
/// Its \c Key and \c Value are inherited from \c M. |
1115 | 1130 |
/// It can also be used as write map if the \c / operator is defined |
1116 | 1131 |
/// between \c Value and \c C and the given multiplier is not zero. |
1117 | 1132 |
/// |
1118 | 1133 |
/// The simplest way of using this map is through the scaleWriteMap() |
1119 | 1134 |
/// function. |
1120 | 1135 |
/// |
1121 | 1136 |
/// \sa ScaleMap |
1122 | 1137 |
template<typename M, typename C = typename M::Value> |
1123 | 1138 |
class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> { |
1124 | 1139 |
M &_m; |
1125 | 1140 |
C _v; |
1126 | 1141 |
public: |
1127 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
|
1128 |
typedef typename Parent::Key Key; |
|
1129 |
|
|
1142 |
///\e |
|
1143 |
typedef typename M::Key Key; |
|
1144 |
///\e |
|
1145 |
typedef typename M::Value Value; |
|
1130 | 1146 |
|
1131 | 1147 |
/// Constructor |
1132 | 1148 |
|
1133 | 1149 |
/// Constructor. |
1134 | 1150 |
/// \param m The undelying map. |
1135 | 1151 |
/// \param v The constant value. |
1136 | 1152 |
ScaleWriteMap(M &m, const C &v) : _m(m), _v(v) {} |
1137 |
/// |
|
1153 |
///\e |
|
1138 | 1154 |
Value operator[](const Key &k) const { return _v*_m[k]; } |
1139 |
/// |
|
1155 |
///\e |
|
1140 | 1156 |
void set(const Key &k, const Value &v) { _m.set(k, v/_v); } |
1141 | 1157 |
}; |
1142 | 1158 |
|
1143 | 1159 |
/// Returns a \c ScaleMap class |
1144 | 1160 |
|
1145 | 1161 |
/// This function just returns a \c ScaleMap class. |
1146 | 1162 |
/// |
1147 | 1163 |
/// For example, if \c m is a map with \c double values and \c v is |
1148 | 1164 |
/// \c double, then <tt>scaleMap(m,v)[x]</tt> will be equal to |
1149 | 1165 |
/// <tt>v*m[x]</tt>. |
1150 | 1166 |
/// |
1151 | 1167 |
/// \relates ScaleMap |
1152 | 1168 |
template<typename M, typename C> |
1153 | 1169 |
inline ScaleMap<M, C> scaleMap(const M &m, const C &v) { |
1154 | 1170 |
return ScaleMap<M, C>(m,v); |
1155 | 1171 |
} |
1156 | 1172 |
|
1157 | 1173 |
/// Returns a \c ScaleWriteMap class |
1158 | 1174 |
|
1159 | 1175 |
/// This function just returns a \c ScaleWriteMap class. |
1160 | 1176 |
/// |
1161 | 1177 |
/// For example, if \c m is a map with \c double values and \c v is |
1162 | 1178 |
/// \c double, then <tt>scaleWriteMap(m,v)[x]</tt> will be equal to |
1163 | 1179 |
/// <tt>v*m[x]</tt>. |
1164 | 1180 |
/// Moreover it makes also possible to write the map. |
1165 | 1181 |
/// |
1166 | 1182 |
/// \relates ScaleWriteMap |
1167 | 1183 |
template<typename M, typename C> |
1168 | 1184 |
inline ScaleWriteMap<M, C> scaleWriteMap(M &m, const C &v) { |
1169 | 1185 |
return ScaleWriteMap<M, C>(m,v); |
1170 | 1186 |
} |
1171 | 1187 |
|
1172 | 1188 |
|
1173 | 1189 |
/// Negative of a map |
1174 | 1190 |
|
1175 | 1191 |
/// This \ref concepts::ReadMap "read-only map" returns the negative |
1176 | 1192 |
/// of the values of the given map (using the unary \c - operator). |
1177 | 1193 |
/// Its \c Key and \c Value are inherited from \c M. |
1178 | 1194 |
/// |
1179 | 1195 |
/// If M::Value is \c int, \c double etc., then |
1180 | 1196 |
/// \code |
1181 | 1197 |
/// NegMap<M> neg(m); |
1182 | 1198 |
/// \endcode |
1183 | 1199 |
/// is equivalent to |
1184 | 1200 |
/// \code |
1185 | 1201 |
/// ScaleMap<M> neg(m,-1); |
1186 | 1202 |
/// \endcode |
1187 | 1203 |
/// |
1188 | 1204 |
/// The simplest way of using this map is through the negMap() |
1189 | 1205 |
/// function. |
1190 | 1206 |
/// |
1191 | 1207 |
/// \sa NegWriteMap |
1192 | 1208 |
template<typename M> |
1193 | 1209 |
class NegMap : public MapBase<typename M::Key, typename M::Value> { |
1194 | 1210 |
const M& _m; |
1195 | 1211 |
public: |
1196 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
|
1197 |
typedef typename Parent::Key Key; |
|
1198 |
|
|
1212 |
///\e |
|
1213 |
typedef typename M::Key Key; |
|
1214 |
///\e |
|
1215 |
typedef typename M::Value Value; |
|
1199 | 1216 |
|
1200 | 1217 |
/// Constructor |
1201 | 1218 |
NegMap(const M &m) : _m(m) {} |
1202 |
/// |
|
1219 |
///\e |
|
1203 | 1220 |
Value operator[](const Key &k) const { return -_m[k]; } |
1204 | 1221 |
}; |
1205 | 1222 |
|
1206 | 1223 |
/// Negative of a map (read-write version) |
1207 | 1224 |
|
1208 | 1225 |
/// This \ref concepts::ReadWriteMap "read-write map" returns the |
1209 | 1226 |
/// negative of the values of the given map (using the unary \c - |
1210 | 1227 |
/// operator). |
1211 | 1228 |
/// Its \c Key and \c Value are inherited from \c M. |
1212 | 1229 |
/// It makes also possible to write the map. |
1213 | 1230 |
/// |
1214 | 1231 |
/// If M::Value is \c int, \c double etc., then |
1215 | 1232 |
/// \code |
1216 | 1233 |
/// NegWriteMap<M> neg(m); |
1217 | 1234 |
/// \endcode |
1218 | 1235 |
/// is equivalent to |
1219 | 1236 |
/// \code |
1220 | 1237 |
/// ScaleWriteMap<M> neg(m,-1); |
1221 | 1238 |
/// \endcode |
1222 | 1239 |
/// |
1223 | 1240 |
/// The simplest way of using this map is through the negWriteMap() |
1224 | 1241 |
/// function. |
1225 | 1242 |
/// |
1226 | 1243 |
/// \sa NegMap |
1227 | 1244 |
template<typename M> |
1228 | 1245 |
class NegWriteMap : public MapBase<typename M::Key, typename M::Value> { |
1229 | 1246 |
M &_m; |
1230 | 1247 |
public: |
1231 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
|
1232 |
typedef typename Parent::Key Key; |
|
1233 |
|
|
1248 |
///\e |
|
1249 |
typedef typename M::Key Key; |
|
1250 |
///\e |
|
1251 |
typedef typename M::Value Value; |
|
1234 | 1252 |
|
1235 | 1253 |
/// Constructor |
1236 | 1254 |
NegWriteMap(M &m) : _m(m) {} |
1237 |
/// |
|
1255 |
///\e |
|
1238 | 1256 |
Value operator[](const Key &k) const { return -_m[k]; } |
1239 |
/// |
|
1257 |
///\e |
|
1240 | 1258 |
void set(const Key &k, const Value &v) { _m.set(k, -v); } |
1241 | 1259 |
}; |
1242 | 1260 |
|
1243 | 1261 |
/// Returns a \c NegMap class |
1244 | 1262 |
|
1245 | 1263 |
/// This function just returns a \c NegMap class. |
1246 | 1264 |
/// |
1247 | 1265 |
/// For example, if \c m is a map with \c double values, then |
1248 | 1266 |
/// <tt>negMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>. |
1249 | 1267 |
/// |
1250 | 1268 |
/// \relates NegMap |
1251 | 1269 |
template <typename M> |
1252 | 1270 |
inline NegMap<M> negMap(const M &m) { |
1253 | 1271 |
return NegMap<M>(m); |
1254 | 1272 |
} |
1255 | 1273 |
|
1256 | 1274 |
/// Returns a \c NegWriteMap class |
1257 | 1275 |
|
1258 | 1276 |
/// This function just returns a \c NegWriteMap class. |
1259 | 1277 |
/// |
1260 | 1278 |
/// For example, if \c m is a map with \c double values, then |
1261 | 1279 |
/// <tt>negWriteMap(m)[x]</tt> will be equal to <tt>-m[x]</tt>. |
1262 | 1280 |
/// Moreover it makes also possible to write the map. |
1263 | 1281 |
/// |
1264 | 1282 |
/// \relates NegWriteMap |
1265 | 1283 |
template <typename M> |
1266 | 1284 |
inline NegWriteMap<M> negWriteMap(M &m) { |
1267 | 1285 |
return NegWriteMap<M>(m); |
1268 | 1286 |
} |
1269 | 1287 |
|
1270 | 1288 |
|
1271 | 1289 |
/// Absolute value of a map |
1272 | 1290 |
|
1273 | 1291 |
/// This \ref concepts::ReadMap "read-only map" returns the absolute |
1274 | 1292 |
/// value of the values of the given map. |
1275 | 1293 |
/// Its \c Key and \c Value are inherited from \c M. |
1276 | 1294 |
/// \c Value must be comparable to \c 0 and the unary \c - |
1277 | 1295 |
/// operator must be defined for it, of course. |
1278 | 1296 |
/// |
1279 | 1297 |
/// The simplest way of using this map is through the absMap() |
1280 | 1298 |
/// function. |
1281 | 1299 |
template<typename M> |
1282 | 1300 |
class AbsMap : public MapBase<typename M::Key, typename M::Value> { |
1283 | 1301 |
const M &_m; |
1284 | 1302 |
public: |
1285 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
|
1286 |
typedef typename Parent::Key Key; |
|
1287 |
|
|
1303 |
///\e |
|
1304 |
typedef typename M::Key Key; |
|
1305 |
///\e |
|
1306 |
typedef typename M::Value Value; |
|
1288 | 1307 |
|
1289 | 1308 |
/// Constructor |
1290 | 1309 |
AbsMap(const M &m) : _m(m) {} |
1291 |
/// |
|
1310 |
///\e |
|
1292 | 1311 |
Value operator[](const Key &k) const { |
1293 | 1312 |
Value tmp = _m[k]; |
1294 | 1313 |
return tmp >= 0 ? tmp : -tmp; |
1295 | 1314 |
} |
1296 | 1315 |
|
1297 | 1316 |
}; |
1298 | 1317 |
|
1299 | 1318 |
/// Returns an \c AbsMap class |
1300 | 1319 |
|
1301 | 1320 |
/// This function just returns an \c AbsMap class. |
1302 | 1321 |
/// |
1303 | 1322 |
/// For example, if \c m is a map with \c double values, then |
1304 | 1323 |
/// <tt>absMap(m)[x]</tt> will be equal to <tt>m[x]</tt> if |
1305 | 1324 |
/// it is positive or zero and <tt>-m[x]</tt> if <tt>m[x]</tt> is |
1306 | 1325 |
/// negative. |
1307 | 1326 |
/// |
1308 | 1327 |
/// \relates AbsMap |
1309 | 1328 |
template<typename M> |
1310 | 1329 |
inline AbsMap<M> absMap(const M &m) { |
1311 | 1330 |
return AbsMap<M>(m); |
1312 | 1331 |
} |
1313 | 1332 |
|
1314 | 1333 |
/// @} |
1315 | 1334 |
|
1316 | 1335 |
// Logical maps and map adaptors: |
1317 | 1336 |
|
1318 | 1337 |
/// \addtogroup maps |
1319 | 1338 |
/// @{ |
1320 | 1339 |
|
1321 | 1340 |
/// Constant \c true map. |
1322 | 1341 |
|
1323 | 1342 |
/// This \ref concepts::ReadMap "read-only map" assigns \c true to |
1324 | 1343 |
/// each key. |
1325 | 1344 |
/// |
1326 | 1345 |
/// Note that |
1327 | 1346 |
/// \code |
1328 | 1347 |
/// TrueMap<K> tm; |
1329 | 1348 |
/// \endcode |
1330 | 1349 |
/// is equivalent to |
1331 | 1350 |
/// \code |
1332 | 1351 |
/// ConstMap<K,bool> tm(true); |
1333 | 1352 |
/// \endcode |
1334 | 1353 |
/// |
1335 | 1354 |
/// \sa FalseMap |
1336 | 1355 |
/// \sa ConstMap |
1337 | 1356 |
template <typename K> |
1338 | 1357 |
class TrueMap : public MapBase<K, bool> { |
1339 | 1358 |
public: |
1340 |
typedef MapBase<K, bool> Parent; |
|
1341 |
typedef typename Parent::Key Key; |
|
1342 |
|
|
1359 |
///\e |
|
1360 |
typedef K Key; |
|
1361 |
///\e |
|
1362 |
typedef bool Value; |
|
1343 | 1363 |
|
1344 | 1364 |
/// Gives back \c true. |
1345 | 1365 |
Value operator[](const Key&) const { return true; } |
1346 | 1366 |
}; |
1347 | 1367 |
|
1348 | 1368 |
/// Returns a \c TrueMap class |
1349 | 1369 |
|
1350 | 1370 |
/// This function just returns a \c TrueMap class. |
1351 | 1371 |
/// \relates TrueMap |
1352 | 1372 |
template<typename K> |
1353 | 1373 |
inline TrueMap<K> trueMap() { |
1354 | 1374 |
return TrueMap<K>(); |
1355 | 1375 |
} |
1356 | 1376 |
|
1357 | 1377 |
|
1358 | 1378 |
/// Constant \c false map. |
1359 | 1379 |
|
1360 | 1380 |
/// This \ref concepts::ReadMap "read-only map" assigns \c false to |
1361 | 1381 |
/// each key. |
1362 | 1382 |
/// |
1363 | 1383 |
/// Note that |
1364 | 1384 |
/// \code |
1365 | 1385 |
/// FalseMap<K> fm; |
1366 | 1386 |
/// \endcode |
1367 | 1387 |
/// is equivalent to |
1368 | 1388 |
/// \code |
1369 | 1389 |
/// ConstMap<K,bool> fm(false); |
1370 | 1390 |
/// \endcode |
1371 | 1391 |
/// |
1372 | 1392 |
/// \sa TrueMap |
1373 | 1393 |
/// \sa ConstMap |
1374 | 1394 |
template <typename K> |
1375 | 1395 |
class FalseMap : public MapBase<K, bool> { |
1376 | 1396 |
public: |
1377 |
typedef MapBase<K, bool> Parent; |
|
1378 |
typedef typename Parent::Key Key; |
|
1379 |
|
|
1397 |
///\e |
|
1398 |
typedef K Key; |
|
1399 |
///\e |
|
1400 |
typedef bool Value; |
|
1380 | 1401 |
|
1381 | 1402 |
/// Gives back \c false. |
1382 | 1403 |
Value operator[](const Key&) const { return false; } |
1383 | 1404 |
}; |
1384 | 1405 |
|
1385 | 1406 |
/// Returns a \c FalseMap class |
1386 | 1407 |
|
1387 | 1408 |
/// This function just returns a \c FalseMap class. |
1388 | 1409 |
/// \relates FalseMap |
1389 | 1410 |
template<typename K> |
1390 | 1411 |
inline FalseMap<K> falseMap() { |
1391 | 1412 |
return FalseMap<K>(); |
1392 | 1413 |
} |
1393 | 1414 |
|
1394 | 1415 |
/// @} |
1395 | 1416 |
|
1396 | 1417 |
/// \addtogroup map_adaptors |
1397 | 1418 |
/// @{ |
1398 | 1419 |
|
1399 | 1420 |
/// Logical 'and' of two maps |
1400 | 1421 |
|
1401 | 1422 |
/// This \ref concepts::ReadMap "read-only map" returns the logical |
1402 | 1423 |
/// 'and' of the values of the two given maps. |
1403 | 1424 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is |
1404 | 1425 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key. |
1405 | 1426 |
/// |
1406 | 1427 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
1407 | 1428 |
/// \code |
1408 | 1429 |
/// AndMap<M1,M2> am(m1,m2); |
1409 | 1430 |
/// \endcode |
1410 | 1431 |
/// <tt>am[x]</tt> will be equal to <tt>m1[x]&&m2[x]</tt>. |
1411 | 1432 |
/// |
1412 | 1433 |
/// The simplest way of using this map is through the andMap() |
1413 | 1434 |
/// function. |
1414 | 1435 |
/// |
1415 | 1436 |
/// \sa OrMap |
1416 | 1437 |
/// \sa NotMap, NotWriteMap |
1417 | 1438 |
template<typename M1, typename M2> |
1418 | 1439 |
class AndMap : public MapBase<typename M1::Key, bool> { |
1419 | 1440 |
const M1 &_m1; |
1420 | 1441 |
const M2 &_m2; |
1421 | 1442 |
public: |
1422 |
typedef MapBase<typename M1::Key, bool> Parent; |
|
1423 |
typedef typename Parent::Key Key; |
|
1424 |
|
|
1443 |
///\e |
|
1444 |
typedef typename M1::Key Key; |
|
1445 |
///\e |
|
1446 |
typedef bool Value; |
|
1425 | 1447 |
|
1426 | 1448 |
/// Constructor |
1427 | 1449 |
AndMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
1428 |
/// |
|
1450 |
///\e |
|
1429 | 1451 |
Value operator[](const Key &k) const { return _m1[k]&&_m2[k]; } |
1430 | 1452 |
}; |
1431 | 1453 |
|
1432 | 1454 |
/// Returns an \c AndMap class |
1433 | 1455 |
|
1434 | 1456 |
/// This function just returns an \c AndMap class. |
1435 | 1457 |
/// |
1436 | 1458 |
/// For example, if \c m1 and \c m2 are both maps with \c bool values, |
1437 | 1459 |
/// then <tt>andMap(m1,m2)[x]</tt> will be equal to |
1438 | 1460 |
/// <tt>m1[x]&&m2[x]</tt>. |
1439 | 1461 |
/// |
1440 | 1462 |
/// \relates AndMap |
1441 | 1463 |
template<typename M1, typename M2> |
1442 | 1464 |
inline AndMap<M1, M2> andMap(const M1 &m1, const M2 &m2) { |
1443 | 1465 |
return AndMap<M1, M2>(m1,m2); |
1444 | 1466 |
} |
1445 | 1467 |
|
1446 | 1468 |
|
1447 | 1469 |
/// Logical 'or' of two maps |
1448 | 1470 |
|
1449 | 1471 |
/// This \ref concepts::ReadMap "read-only map" returns the logical |
1450 | 1472 |
/// 'or' of the values of the two given maps. |
1451 | 1473 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is |
1452 | 1474 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key. |
1453 | 1475 |
/// |
1454 | 1476 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
1455 | 1477 |
/// \code |
1456 | 1478 |
/// OrMap<M1,M2> om(m1,m2); |
1457 | 1479 |
/// \endcode |
1458 | 1480 |
/// <tt>om[x]</tt> will be equal to <tt>m1[x]||m2[x]</tt>. |
1459 | 1481 |
/// |
1460 | 1482 |
/// The simplest way of using this map is through the orMap() |
1461 | 1483 |
/// function. |
1462 | 1484 |
/// |
1463 | 1485 |
/// \sa AndMap |
1464 | 1486 |
/// \sa NotMap, NotWriteMap |
1465 | 1487 |
template<typename M1, typename M2> |
1466 | 1488 |
class OrMap : public MapBase<typename M1::Key, bool> { |
1467 | 1489 |
const M1 &_m1; |
1468 | 1490 |
const M2 &_m2; |
1469 | 1491 |
public: |
1470 |
typedef MapBase<typename M1::Key, bool> Parent; |
|
1471 |
typedef typename Parent::Key Key; |
|
1472 |
|
|
1492 |
///\e |
|
1493 |
typedef typename M1::Key Key; |
|
1494 |
///\e |
|
1495 |
typedef bool Value; |
|
1473 | 1496 |
|
1474 | 1497 |
/// Constructor |
1475 | 1498 |
OrMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
1476 |
/// |
|
1499 |
///\e |
|
1477 | 1500 |
Value operator[](const Key &k) const { return _m1[k]||_m2[k]; } |
1478 | 1501 |
}; |
1479 | 1502 |
|
1480 | 1503 |
/// Returns an \c OrMap class |
1481 | 1504 |
|
1482 | 1505 |
/// This function just returns an \c OrMap class. |
1483 | 1506 |
/// |
1484 | 1507 |
/// For example, if \c m1 and \c m2 are both maps with \c bool values, |
1485 | 1508 |
/// then <tt>orMap(m1,m2)[x]</tt> will be equal to |
1486 | 1509 |
/// <tt>m1[x]||m2[x]</tt>. |
1487 | 1510 |
/// |
1488 | 1511 |
/// \relates OrMap |
1489 | 1512 |
template<typename M1, typename M2> |
1490 | 1513 |
inline OrMap<M1, M2> orMap(const M1 &m1, const M2 &m2) { |
1491 | 1514 |
return OrMap<M1, M2>(m1,m2); |
1492 | 1515 |
} |
1493 | 1516 |
|
1494 | 1517 |
|
1495 | 1518 |
/// Logical 'not' of a map |
1496 | 1519 |
|
1497 | 1520 |
/// This \ref concepts::ReadMap "read-only map" returns the logical |
1498 | 1521 |
/// negation of the values of the given map. |
1499 | 1522 |
/// Its \c Key is inherited from \c M and its \c Value is \c bool. |
1500 | 1523 |
/// |
1501 | 1524 |
/// The simplest way of using this map is through the notMap() |
1502 | 1525 |
/// function. |
1503 | 1526 |
/// |
1504 | 1527 |
/// \sa NotWriteMap |
1505 | 1528 |
template <typename M> |
1506 | 1529 |
class NotMap : public MapBase<typename M::Key, bool> { |
1507 | 1530 |
const M &_m; |
1508 | 1531 |
public: |
1509 |
typedef MapBase<typename M::Key, bool> Parent; |
|
1510 |
typedef typename Parent::Key Key; |
|
1511 |
|
|
1532 |
///\e |
|
1533 |
typedef typename M::Key Key; |
|
1534 |
///\e |
|
1535 |
typedef bool Value; |
|
1512 | 1536 |
|
1513 | 1537 |
/// Constructor |
1514 | 1538 |
NotMap(const M &m) : _m(m) {} |
1515 |
/// |
|
1539 |
///\e |
|
1516 | 1540 |
Value operator[](const Key &k) const { return !_m[k]; } |
1517 | 1541 |
}; |
1518 | 1542 |
|
1519 | 1543 |
/// Logical 'not' of a map (read-write version) |
1520 | 1544 |
|
1521 | 1545 |
/// This \ref concepts::ReadWriteMap "read-write map" returns the |
1522 | 1546 |
/// logical negation of the values of the given map. |
1523 | 1547 |
/// Its \c Key is inherited from \c M and its \c Value is \c bool. |
1524 | 1548 |
/// It makes also possible to write the map. When a value is set, |
1525 | 1549 |
/// the opposite value is set to the original map. |
1526 | 1550 |
/// |
1527 | 1551 |
/// The simplest way of using this map is through the notWriteMap() |
1528 | 1552 |
/// function. |
1529 | 1553 |
/// |
1530 | 1554 |
/// \sa NotMap |
1531 | 1555 |
template <typename M> |
1532 | 1556 |
class NotWriteMap : public MapBase<typename M::Key, bool> { |
1533 | 1557 |
M &_m; |
1534 | 1558 |
public: |
1535 |
typedef MapBase<typename M::Key, bool> Parent; |
|
1536 |
typedef typename Parent::Key Key; |
|
1537 |
|
|
1559 |
///\e |
|
1560 |
typedef typename M::Key Key; |
|
1561 |
///\e |
|
1562 |
typedef bool Value; |
|
1538 | 1563 |
|
1539 | 1564 |
/// Constructor |
1540 | 1565 |
NotWriteMap(M &m) : _m(m) {} |
1541 |
/// |
|
1566 |
///\e |
|
1542 | 1567 |
Value operator[](const Key &k) const { return !_m[k]; } |
1543 |
/// |
|
1568 |
///\e |
|
1544 | 1569 |
void set(const Key &k, bool v) { _m.set(k, !v); } |
1545 | 1570 |
}; |
1546 | 1571 |
|
1547 | 1572 |
/// Returns a \c NotMap class |
1548 | 1573 |
|
1549 | 1574 |
/// This function just returns a \c NotMap class. |
1550 | 1575 |
/// |
1551 | 1576 |
/// For example, if \c m is a map with \c bool values, then |
1552 | 1577 |
/// <tt>notMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>. |
1553 | 1578 |
/// |
1554 | 1579 |
/// \relates NotMap |
1555 | 1580 |
template <typename M> |
1556 | 1581 |
inline NotMap<M> notMap(const M &m) { |
1557 | 1582 |
return NotMap<M>(m); |
1558 | 1583 |
} |
1559 | 1584 |
|
1560 | 1585 |
/// Returns a \c NotWriteMap class |
1561 | 1586 |
|
1562 | 1587 |
/// This function just returns a \c NotWriteMap class. |
1563 | 1588 |
/// |
1564 | 1589 |
/// For example, if \c m is a map with \c bool values, then |
1565 | 1590 |
/// <tt>notWriteMap(m)[x]</tt> will be equal to <tt>!m[x]</tt>. |
1566 | 1591 |
/// Moreover it makes also possible to write the map. |
1567 | 1592 |
/// |
1568 | 1593 |
/// \relates NotWriteMap |
1569 | 1594 |
template <typename M> |
1570 | 1595 |
inline NotWriteMap<M> notWriteMap(M &m) { |
1571 | 1596 |
return NotWriteMap<M>(m); |
1572 | 1597 |
} |
1573 | 1598 |
|
1574 | 1599 |
|
1575 | 1600 |
/// Combination of two maps using the \c == operator |
1576 | 1601 |
|
1577 | 1602 |
/// This \ref concepts::ReadMap "read-only map" assigns \c true to |
1578 | 1603 |
/// the keys for which the corresponding values of the two maps are |
1579 | 1604 |
/// equal. |
1580 | 1605 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is |
1581 | 1606 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key. |
1582 | 1607 |
/// |
1583 | 1608 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
1584 | 1609 |
/// \code |
1585 | 1610 |
/// EqualMap<M1,M2> em(m1,m2); |
1586 | 1611 |
/// \endcode |
1587 | 1612 |
/// <tt>em[x]</tt> will be equal to <tt>m1[x]==m2[x]</tt>. |
1588 | 1613 |
/// |
1589 | 1614 |
/// The simplest way of using this map is through the equalMap() |
1590 | 1615 |
/// function. |
1591 | 1616 |
/// |
1592 | 1617 |
/// \sa LessMap |
1593 | 1618 |
template<typename M1, typename M2> |
1594 | 1619 |
class EqualMap : public MapBase<typename M1::Key, bool> { |
1595 | 1620 |
const M1 &_m1; |
1596 | 1621 |
const M2 &_m2; |
1597 | 1622 |
public: |
1598 |
typedef MapBase<typename M1::Key, bool> Parent; |
|
1599 |
typedef typename Parent::Key Key; |
|
1600 |
|
|
1623 |
///\e |
|
1624 |
typedef typename M1::Key Key; |
|
1625 |
///\e |
|
1626 |
typedef bool Value; |
|
1601 | 1627 |
|
1602 | 1628 |
/// Constructor |
1603 | 1629 |
EqualMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
1604 |
/// |
|
1630 |
///\e |
|
1605 | 1631 |
Value operator[](const Key &k) const { return _m1[k]==_m2[k]; } |
1606 | 1632 |
}; |
1607 | 1633 |
|
1608 | 1634 |
/// Returns an \c EqualMap class |
1609 | 1635 |
|
1610 | 1636 |
/// This function just returns an \c EqualMap class. |
1611 | 1637 |
/// |
1612 | 1638 |
/// For example, if \c m1 and \c m2 are maps with keys and values of |
1613 | 1639 |
/// the same type, then <tt>equalMap(m1,m2)[x]</tt> will be equal to |
1614 | 1640 |
/// <tt>m1[x]==m2[x]</tt>. |
1615 | 1641 |
/// |
1616 | 1642 |
/// \relates EqualMap |
1617 | 1643 |
template<typename M1, typename M2> |
1618 | 1644 |
inline EqualMap<M1, M2> equalMap(const M1 &m1, const M2 &m2) { |
1619 | 1645 |
return EqualMap<M1, M2>(m1,m2); |
1620 | 1646 |
} |
1621 | 1647 |
|
1622 | 1648 |
|
1623 | 1649 |
/// Combination of two maps using the \c < operator |
1624 | 1650 |
|
1625 | 1651 |
/// This \ref concepts::ReadMap "read-only map" assigns \c true to |
1626 | 1652 |
/// the keys for which the corresponding value of the first map is |
1627 | 1653 |
/// less then the value of the second map. |
1628 | 1654 |
/// Its \c Key type is inherited from \c M1 and its \c Value type is |
1629 | 1655 |
/// \c bool. \c M2::Key must be convertible to \c M1::Key. |
1630 | 1656 |
/// |
1631 | 1657 |
/// If \c m1 is of type \c M1 and \c m2 is of \c M2, then for |
1632 | 1658 |
/// \code |
1633 | 1659 |
/// LessMap<M1,M2> lm(m1,m2); |
1634 | 1660 |
/// \endcode |
1635 | 1661 |
/// <tt>lm[x]</tt> will be equal to <tt>m1[x]<m2[x]</tt>. |
1636 | 1662 |
/// |
1637 | 1663 |
/// The simplest way of using this map is through the lessMap() |
1638 | 1664 |
/// function. |
1639 | 1665 |
/// |
1640 | 1666 |
/// \sa EqualMap |
1641 | 1667 |
template<typename M1, typename M2> |
1642 | 1668 |
class LessMap : public MapBase<typename M1::Key, bool> { |
1643 | 1669 |
const M1 &_m1; |
1644 | 1670 |
const M2 &_m2; |
1645 | 1671 |
public: |
1646 |
typedef MapBase<typename M1::Key, bool> Parent; |
|
1647 |
typedef typename Parent::Key Key; |
|
1648 |
|
|
1672 |
///\e |
|
1673 |
typedef typename M1::Key Key; |
|
1674 |
///\e |
|
1675 |
typedef bool Value; |
|
1649 | 1676 |
|
1650 | 1677 |
/// Constructor |
1651 | 1678 |
LessMap(const M1 &m1, const M2 &m2) : _m1(m1), _m2(m2) {} |
1652 |
/// |
|
1679 |
///\e |
|
1653 | 1680 |
Value operator[](const Key &k) const { return _m1[k]<_m2[k]; } |
1654 | 1681 |
}; |
1655 | 1682 |
|
1656 | 1683 |
/// Returns an \c LessMap class |
1657 | 1684 |
|
1658 | 1685 |
/// This function just returns an \c LessMap class. |
1659 | 1686 |
/// |
1660 | 1687 |
/// For example, if \c m1 and \c m2 are maps with keys and values of |
1661 | 1688 |
/// the same type, then <tt>lessMap(m1,m2)[x]</tt> will be equal to |
1662 | 1689 |
/// <tt>m1[x]<m2[x]</tt>. |
1663 | 1690 |
/// |
1664 | 1691 |
/// \relates LessMap |
1665 | 1692 |
template<typename M1, typename M2> |
1666 | 1693 |
inline LessMap<M1, M2> lessMap(const M1 &m1, const M2 &m2) { |
1667 | 1694 |
return LessMap<M1, M2>(m1,m2); |
1668 | 1695 |
} |
1669 | 1696 |
|
1670 | 1697 |
namespace _maps_bits { |
1671 | 1698 |
|
1672 | 1699 |
template <typename _Iterator, typename Enable = void> |
1673 | 1700 |
struct IteratorTraits { |
1674 | 1701 |
typedef typename std::iterator_traits<_Iterator>::value_type Value; |
1675 | 1702 |
}; |
1676 | 1703 |
|
1677 | 1704 |
template <typename _Iterator> |
1678 | 1705 |
struct IteratorTraits<_Iterator, |
1679 | 1706 |
typename exists<typename _Iterator::container_type>::type> |
1680 | 1707 |
{ |
1681 | 1708 |
typedef typename _Iterator::container_type::value_type Value; |
1682 | 1709 |
}; |
1683 | 1710 |
|
1684 | 1711 |
} |
1685 | 1712 |
|
1686 | 1713 |
/// @} |
1687 | 1714 |
|
1688 | 1715 |
/// \addtogroup maps |
1689 | 1716 |
/// @{ |
1690 | 1717 |
|
1691 | 1718 |
/// \brief Writable bool map for logging each \c true assigned element |
1692 | 1719 |
/// |
1693 | 1720 |
/// A \ref concepts::WriteMap "writable" bool map for logging |
1694 | 1721 |
/// each \c true assigned element, i.e it copies subsequently each |
1695 | 1722 |
/// keys set to \c true to the given iterator. |
1696 | 1723 |
/// The most important usage of it is storing certain nodes or arcs |
1697 | 1724 |
/// that were marked \c true by an algorithm. |
1698 | 1725 |
/// |
1699 | 1726 |
/// There are several algorithms that provide solutions through bool |
1700 | 1727 |
/// maps and most of them assign \c true at most once for each key. |
1701 | 1728 |
/// In these cases it is a natural request to store each \c true |
1702 | 1729 |
/// assigned elements (in order of the assignment), which can be |
1703 | 1730 |
/// easily done with LoggerBoolMap. |
1704 | 1731 |
/// |
1705 | 1732 |
/// The simplest way of using this map is through the loggerBoolMap() |
1706 | 1733 |
/// function. |
1707 | 1734 |
/// |
1708 |
/// \tparam It The type of the iterator. |
|
1709 |
/// \tparam Ke The key type of the map. The default value set |
|
1735 |
/// \tparam IT The type of the iterator. |
|
1736 |
/// \tparam KEY The key type of the map. The default value set |
|
1710 | 1737 |
/// according to the iterator type should work in most cases. |
1711 | 1738 |
/// |
1712 | 1739 |
/// \note The container of the iterator must contain enough space |
1713 | 1740 |
/// for the elements or the iterator should be an inserter iterator. |
1714 | 1741 |
#ifdef DOXYGEN |
1715 |
template <typename |
|
1742 |
template <typename IT, typename KEY> |
|
1716 | 1743 |
#else |
1717 |
template <typename It, |
|
1718 |
typename Ke=typename _maps_bits::IteratorTraits<It>::Value> |
|
1744 |
template <typename IT, |
|
1745 |
typename KEY = typename _maps_bits::IteratorTraits<IT>::Value> |
|
1719 | 1746 |
#endif |
1720 |
class LoggerBoolMap { |
|
1747 |
class LoggerBoolMap : public MapBase<KEY, bool> { |
|
1721 | 1748 |
public: |
1722 |
typedef It Iterator; |
|
1723 |
|
|
1724 |
|
|
1749 |
|
|
1750 |
///\e |
|
1751 |
typedef KEY Key; |
|
1752 |
///\e |
|
1725 | 1753 |
typedef bool Value; |
1754 |
///\e |
|
1755 |
typedef IT Iterator; |
|
1726 | 1756 |
|
1727 | 1757 |
/// Constructor |
1728 | 1758 |
LoggerBoolMap(Iterator it) |
1729 | 1759 |
: _begin(it), _end(it) {} |
1730 | 1760 |
|
1731 | 1761 |
/// Gives back the given iterator set for the first key |
1732 | 1762 |
Iterator begin() const { |
1733 | 1763 |
return _begin; |
1734 | 1764 |
} |
1735 | 1765 |
|
1736 | 1766 |
/// Gives back the the 'after the last' iterator |
1737 | 1767 |
Iterator end() const { |
1738 | 1768 |
return _end; |
1739 | 1769 |
} |
1740 | 1770 |
|
1741 | 1771 |
/// The set function of the map |
1742 | 1772 |
void set(const Key& key, Value value) { |
1743 | 1773 |
if (value) { |
1744 | 1774 |
*_end++ = key; |
1745 | 1775 |
} |
1746 | 1776 |
} |
1747 | 1777 |
|
1748 | 1778 |
private: |
1749 | 1779 |
Iterator _begin; |
1750 | 1780 |
Iterator _end; |
1751 | 1781 |
}; |
1752 | 1782 |
|
1753 | 1783 |
/// Returns a \c LoggerBoolMap class |
1754 | 1784 |
|
1755 | 1785 |
/// This function just returns a \c LoggerBoolMap class. |
1756 | 1786 |
/// |
1757 | 1787 |
/// The most important usage of it is storing certain nodes or arcs |
1758 | 1788 |
/// that were marked \c true by an algorithm. |
1759 | 1789 |
/// For example it makes easier to store the nodes in the processing |
1760 | 1790 |
/// order of Dfs algorithm, as the following examples show. |
1761 | 1791 |
/// \code |
1762 | 1792 |
/// std::vector<Node> v; |
1763 | 1793 |
/// dfs(g,s).processedMap(loggerBoolMap(std::back_inserter(v))).run(); |
1764 | 1794 |
/// \endcode |
1765 | 1795 |
/// \code |
1766 | 1796 |
/// std::vector<Node> v(countNodes(g)); |
1767 | 1797 |
/// dfs(g,s).processedMap(loggerBoolMap(v.begin())).run(); |
1768 | 1798 |
/// \endcode |
1769 | 1799 |
/// |
1770 | 1800 |
/// \note The container of the iterator must contain enough space |
1771 | 1801 |
/// for the elements or the iterator should be an inserter iterator. |
1772 | 1802 |
/// |
1773 | 1803 |
/// \note LoggerBoolMap is just \ref concepts::WriteMap "writable", so |
1774 | 1804 |
/// it cannot be used when a readable map is needed, for example as |
1775 | 1805 |
/// \c ReachedMap for \c Bfs, \c Dfs and \c Dijkstra algorithms. |
1776 | 1806 |
/// |
1777 | 1807 |
/// \relates LoggerBoolMap |
1778 | 1808 |
template<typename Iterator> |
1779 | 1809 |
inline LoggerBoolMap<Iterator> loggerBoolMap(Iterator it) { |
1780 | 1810 |
return LoggerBoolMap<Iterator>(it); |
1781 | 1811 |
} |
1782 | 1812 |
|
1783 | 1813 |
/// @} |
1784 | 1814 |
|
1785 | 1815 |
/// \addtogroup graph_maps |
1786 | 1816 |
/// @{ |
1787 | 1817 |
|
1788 |
/// Provides an immutable and unique id for each item in the graph. |
|
1789 |
|
|
1790 |
/// The IdMap class provides a unique and immutable id for each item of the |
|
1791 |
/// same type (e.g. node) in the graph. This id is <ul><li>\b unique: |
|
1792 |
/// different items (nodes) get different ids <li>\b immutable: the id of an |
|
1793 |
/// item (node) does not change (even if you delete other nodes). </ul> |
|
1794 |
/// Through this map you get access (i.e. can read) the inner id values of |
|
1795 |
/// the items stored in the graph. This map can be inverted with its member |
|
1818 |
/// \brief Provides an immutable and unique id for each item in a graph. |
|
1819 |
/// |
|
1820 |
/// IdMap provides a unique and immutable id for each item of the |
|
1821 |
/// same type (\c Node, \c Arc or \c Edge) in a graph. This id is |
|
1822 |
/// - \b unique: different items get different ids, |
|
1823 |
/// - \b immutable: the id of an item does not change (even if you |
|
1824 |
/// delete other nodes). |
|
1825 |
/// |
|
1826 |
/// Using this map you get access (i.e. can read) the inner id values of |
|
1827 |
/// the items stored in the graph, which is returned by the \c id() |
|
1828 |
/// function of the graph. This map can be inverted with its member |
|
1796 | 1829 |
/// class \c InverseMap or with the \c operator() member. |
1797 | 1830 |
/// |
1798 |
template <typename _Graph, typename _Item> |
|
1799 |
class IdMap { |
|
1831 |
/// \tparam GR The graph type. |
|
1832 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
|
1833 |
/// \c GR::Edge). |
|
1834 |
/// |
|
1835 |
/// \see DescriptorMap |
|
1836 |
template <typename GR, typename K> |
|
1837 |
class IdMap : public MapBase<K, int> { |
|
1800 | 1838 |
public: |
1801 |
|
|
1839 |
/// The graph type of IdMap. |
|
1840 |
typedef GR Graph; |
|
1841 |
/// The key type of IdMap (\c Node, \c Arc or \c Edge). |
|
1842 |
typedef K Item; |
|
1843 |
/// The key type of IdMap (\c Node, \c Arc or \c Edge). |
|
1844 |
typedef K Key; |
|
1845 |
/// The value type of IdMap. |
|
1802 | 1846 |
typedef int Value; |
1803 |
typedef _Item Item; |
|
1804 |
typedef _Item Key; |
|
1805 | 1847 |
|
1806 | 1848 |
/// \brief Constructor. |
1807 | 1849 |
/// |
1808 | 1850 |
/// Constructor of the map. |
1809 | 1851 |
explicit IdMap(const Graph& graph) : _graph(&graph) {} |
1810 | 1852 |
|
1811 | 1853 |
/// \brief Gives back the \e id of the item. |
1812 | 1854 |
/// |
1813 | 1855 |
/// Gives back the immutable and unique \e id of the item. |
1814 | 1856 |
int operator[](const Item& item) const { return _graph->id(item);} |
1815 | 1857 |
|
1816 |
/// \brief Gives back the item by its id. |
|
1858 |
/// \brief Gives back the \e item by its id. |
|
1817 | 1859 |
/// |
1818 |
/// Gives back the item by its id. |
|
1860 |
/// Gives back the \e item by its id. |
|
1819 | 1861 |
Item operator()(int id) { return _graph->fromId(id, Item()); } |
1820 | 1862 |
|
1821 | 1863 |
private: |
1822 | 1864 |
const Graph* _graph; |
1823 | 1865 |
|
1824 | 1866 |
public: |
1825 | 1867 |
|
1826 |
/// \brief |
|
1868 |
/// \brief This class represents the inverse of its owner (IdMap). |
|
1827 | 1869 |
/// |
1828 |
/// |
|
1870 |
/// This class represents the inverse of its owner (IdMap). |
|
1829 | 1871 |
/// \see inverse() |
1830 | 1872 |
class InverseMap { |
1831 | 1873 |
public: |
1832 | 1874 |
|
1833 | 1875 |
/// \brief Constructor. |
1834 | 1876 |
/// |
1835 | 1877 |
/// Constructor for creating an id-to-item map. |
1836 | 1878 |
explicit InverseMap(const Graph& graph) : _graph(&graph) {} |
1837 | 1879 |
|
1838 | 1880 |
/// \brief Constructor. |
1839 | 1881 |
/// |
1840 | 1882 |
/// Constructor for creating an id-to-item map. |
1841 | 1883 |
explicit InverseMap(const IdMap& map) : _graph(map._graph) {} |
1842 | 1884 |
|
1843 | 1885 |
/// \brief Gives back the given item from its id. |
1844 | 1886 |
/// |
1845 | 1887 |
/// Gives back the given item from its id. |
1846 |
/// |
|
1847 | 1888 |
Item operator[](int id) const { return _graph->fromId(id, Item());} |
1848 | 1889 |
|
1849 | 1890 |
private: |
1850 | 1891 |
const Graph* _graph; |
1851 | 1892 |
}; |
1852 | 1893 |
|
1853 | 1894 |
/// \brief Gives back the inverse of the map. |
1854 | 1895 |
/// |
1855 | 1896 |
/// Gives back the inverse of the IdMap. |
1856 | 1897 |
InverseMap inverse() const { return InverseMap(*_graph);} |
1857 |
|
|
1858 | 1898 |
}; |
1859 | 1899 |
|
1860 | 1900 |
|
1861 |
/// \brief General invertable graph-map type. |
|
1862 |
|
|
1863 |
/// This type provides simple invertable graph-maps. |
|
1864 |
/// The InvertableMap wraps an arbitrary ReadWriteMap |
|
1901 |
/// \brief General invertable graph map type. |
|
1902 |
|
|
1903 |
/// This class provides simple invertable graph maps. |
|
1904 |
/// It wraps an arbitrary \ref concepts::ReadWriteMap "ReadWriteMap" |
|
1865 | 1905 |
/// and if a key is set to a new value then store it |
1866 | 1906 |
/// in the inverse map. |
1867 | 1907 |
/// |
1868 | 1908 |
/// The values of the map can be accessed |
1869 | 1909 |
/// with stl compatible forward iterator. |
1870 | 1910 |
/// |
1871 |
/// \tparam _Graph The graph type. |
|
1872 |
/// \tparam _Item The item type of the graph. |
|
1873 |
/// \tparam |
|
1911 |
/// \tparam GR The graph type. |
|
1912 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
|
1913 |
/// \c GR::Edge). |
|
1914 |
/// \tparam V The value type of the map. |
|
1874 | 1915 |
/// |
1875 | 1916 |
/// \see IterableValueMap |
1876 |
template <typename |
|
1917 |
template <typename GR, typename K, typename V> |
|
1877 | 1918 |
class InvertableMap |
1878 |
: protected ItemSetTraits< |
|
1919 |
: protected ItemSetTraits<GR, K>::template Map<V>::Type { |
|
1879 | 1920 |
private: |
1880 | 1921 |
|
1881 |
typedef typename ItemSetTraits<_Graph, _Item>:: |
|
1882 |
template Map<_Value>::Type Map; |
|
1883 |
typedef _Graph Graph; |
|
1884 |
|
|
1885 |
typedef |
|
1922 |
typedef typename ItemSetTraits<GR, K>:: |
|
1923 |
template Map<V>::Type Map; |
|
1924 |
|
|
1925 |
typedef std::map<V, K> Container; |
|
1886 | 1926 |
Container _inv_map; |
1887 | 1927 |
|
1888 | 1928 |
public: |
1889 | 1929 |
|
1890 |
/// The key type of InvertableMap (Node, Arc, Edge). |
|
1891 |
typedef typename Map::Key Key; |
|
1892 |
/// The value type of the InvertableMap. |
|
1893 |
typedef typename Map::Value Value; |
|
1930 |
/// The graph type of InvertableMap. |
|
1931 |
typedef GR Graph; |
|
1932 |
/// The key type of InvertableMap (\c Node, \c Arc or \c Edge). |
|
1933 |
typedef K Item; |
|
1934 |
/// The key type of InvertableMap (\c Node, \c Arc or \c Edge). |
|
1935 |
typedef K Key; |
|
1936 |
/// The value type of InvertableMap. |
|
1937 |
typedef V Value; |
|
1894 | 1938 |
|
1895 | 1939 |
/// \brief Constructor. |
1896 | 1940 |
/// |
1897 |
/// Construct a new InvertableMap for the graph. |
|
1898 |
/// |
|
1941 |
/// Construct a new InvertableMap for the given graph. |
|
1899 | 1942 |
explicit InvertableMap(const Graph& graph) : Map(graph) {} |
1900 | 1943 |
|
1901 | 1944 |
/// \brief Forward iterator for values. |
1902 | 1945 |
/// |
1903 | 1946 |
/// This iterator is an stl compatible forward |
1904 | 1947 |
/// iterator on the values of the map. The values can |
1905 |
/// be accessed in the [beginValue, endValue) range. |
|
1906 |
/// |
|
1948 |
/// be accessed in the <tt>[beginValue, endValue)</tt> range. |
|
1907 | 1949 |
class ValueIterator |
1908 | 1950 |
: public std::iterator<std::forward_iterator_tag, Value> { |
1909 | 1951 |
friend class InvertableMap; |
1910 | 1952 |
private: |
1911 | 1953 |
ValueIterator(typename Container::const_iterator _it) |
1912 | 1954 |
: it(_it) {} |
1913 | 1955 |
public: |
1914 | 1956 |
|
1915 | 1957 |
ValueIterator() {} |
1916 | 1958 |
|
1917 | 1959 |
ValueIterator& operator++() { ++it; return *this; } |
1918 | 1960 |
ValueIterator operator++(int) { |
1919 | 1961 |
ValueIterator tmp(*this); |
1920 | 1962 |
operator++(); |
1921 | 1963 |
return tmp; |
1922 | 1964 |
} |
1923 | 1965 |
|
1924 | 1966 |
const Value& operator*() const { return it->first; } |
1925 | 1967 |
const Value* operator->() const { return &(it->first); } |
1926 | 1968 |
|
1927 | 1969 |
bool operator==(ValueIterator jt) const { return it == jt.it; } |
1928 | 1970 |
bool operator!=(ValueIterator jt) const { return it != jt.it; } |
1929 | 1971 |
|
1930 | 1972 |
private: |
1931 | 1973 |
typename Container::const_iterator it; |
1932 | 1974 |
}; |
1933 | 1975 |
|
1934 | 1976 |
/// \brief Returns an iterator to the first value. |
1935 | 1977 |
/// |
1936 | 1978 |
/// Returns an stl compatible iterator to the |
1937 | 1979 |
/// first value of the map. The values of the |
1938 |
/// map can be accessed in the [beginValue, endValue) |
|
1980 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt> |
|
1939 | 1981 |
/// range. |
1940 | 1982 |
ValueIterator beginValue() const { |
1941 | 1983 |
return ValueIterator(_inv_map.begin()); |
1942 | 1984 |
} |
1943 | 1985 |
|
1944 | 1986 |
/// \brief Returns an iterator after the last value. |
1945 | 1987 |
/// |
1946 | 1988 |
/// Returns an stl compatible iterator after the |
1947 | 1989 |
/// last value of the map. The values of the |
1948 |
/// map can be accessed in the [beginValue, endValue) |
|
1990 |
/// map can be accessed in the <tt>[beginValue, endValue)</tt> |
|
1949 | 1991 |
/// range. |
1950 | 1992 |
ValueIterator endValue() const { |
1951 | 1993 |
return ValueIterator(_inv_map.end()); |
1952 | 1994 |
} |
1953 | 1995 |
|
1954 |
/// \brief |
|
1996 |
/// \brief Sets the value associated with the given key. |
|
1955 | 1997 |
/// |
1956 |
/// Sets the |
|
1998 |
/// Sets the value associated with the given key. |
|
1957 | 1999 |
void set(const Key& key, const Value& val) { |
1958 | 2000 |
Value oldval = Map::operator[](key); |
1959 | 2001 |
typename Container::iterator it = _inv_map.find(oldval); |
1960 | 2002 |
if (it != _inv_map.end() && it->second == key) { |
1961 | 2003 |
_inv_map.erase(it); |
1962 | 2004 |
} |
1963 | 2005 |
_inv_map.insert(make_pair(val, key)); |
1964 | 2006 |
Map::set(key, val); |
1965 | 2007 |
} |
1966 | 2008 |
|
1967 |
/// \brief |
|
2009 |
/// \brief Returns the value associated with the given key. |
|
1968 | 2010 |
/// |
1969 |
/// |
|
2011 |
/// Returns the value associated with the given key. |
|
1970 | 2012 |
typename MapTraits<Map>::ConstReturnValue |
1971 | 2013 |
operator[](const Key& key) const { |
1972 | 2014 |
return Map::operator[](key); |
1973 | 2015 |
} |
1974 | 2016 |
|
1975 | 2017 |
/// \brief Gives back the item by its value. |
1976 | 2018 |
/// |
1977 | 2019 |
/// Gives back the item by its value. |
1978 | 2020 |
Key operator()(const Value& key) const { |
1979 | 2021 |
typename Container::const_iterator it = _inv_map.find(key); |
1980 | 2022 |
return it != _inv_map.end() ? it->second : INVALID; |
1981 | 2023 |
} |
1982 | 2024 |
|
1983 | 2025 |
protected: |
1984 | 2026 |
|
1985 |
/// \brief Erase the key from the map. |
|
2027 |
/// \brief Erase the key from the map and the inverse map. |
|
1986 | 2028 |
/// |
1987 |
/// Erase the key |
|
2029 |
/// Erase the key from the map and the inverse map. It is called by the |
|
1988 | 2030 |
/// \c AlterationNotifier. |
1989 | 2031 |
virtual void erase(const Key& key) { |
1990 | 2032 |
Value val = Map::operator[](key); |
1991 | 2033 |
typename Container::iterator it = _inv_map.find(val); |
1992 | 2034 |
if (it != _inv_map.end() && it->second == key) { |
1993 | 2035 |
_inv_map.erase(it); |
1994 | 2036 |
} |
1995 | 2037 |
Map::erase(key); |
1996 | 2038 |
} |
1997 | 2039 |
|
1998 |
/// \brief Erase more keys from the map. |
|
2040 |
/// \brief Erase more keys from the map and the inverse map. |
|
1999 | 2041 |
/// |
2000 |
/// Erase more keys from the map. It is called by the |
|
2042 |
/// Erase more keys from the map and the inverse map. It is called by the |
|
2001 | 2043 |
/// \c AlterationNotifier. |
2002 | 2044 |
virtual void erase(const std::vector<Key>& keys) { |
2003 | 2045 |
for (int i = 0; i < int(keys.size()); ++i) { |
2004 | 2046 |
Value val = Map::operator[](keys[i]); |
2005 | 2047 |
typename Container::iterator it = _inv_map.find(val); |
2006 | 2048 |
if (it != _inv_map.end() && it->second == keys[i]) { |
2007 | 2049 |
_inv_map.erase(it); |
2008 | 2050 |
} |
2009 | 2051 |
} |
2010 | 2052 |
Map::erase(keys); |
2011 | 2053 |
} |
2012 | 2054 |
|
2013 |
/// \brief Clear the keys from the map and inverse map. |
|
2055 |
/// \brief Clear the keys from the map and the inverse map. |
|
2014 | 2056 |
/// |
2015 |
/// Clear the keys from the map and inverse map. It is called by the |
|
2057 |
/// Clear the keys from the map and the inverse map. It is called by the |
|
2016 | 2058 |
/// \c AlterationNotifier. |
2017 | 2059 |
virtual void clear() { |
2018 | 2060 |
_inv_map.clear(); |
2019 | 2061 |
Map::clear(); |
2020 | 2062 |
} |
2021 | 2063 |
|
2022 | 2064 |
public: |
2023 | 2065 |
|
2024 | 2066 |
/// \brief The inverse map type. |
2025 | 2067 |
/// |
2026 | 2068 |
/// The inverse of this map. The subscript operator of the map |
2027 |
/// gives back |
|
2069 |
/// gives back the item that was last assigned to the value. |
|
2028 | 2070 |
class InverseMap { |
2029 | 2071 |
public: |
2030 |
/// \brief Constructor |
|
2072 |
/// \brief Constructor |
|
2031 | 2073 |
/// |
2032 | 2074 |
/// Constructor of the InverseMap. |
2033 | 2075 |
explicit InverseMap(const InvertableMap& inverted) |
2034 | 2076 |
: _inverted(inverted) {} |
2035 | 2077 |
|
2036 | 2078 |
/// The value type of the InverseMap. |
2037 | 2079 |
typedef typename InvertableMap::Key Value; |
2038 | 2080 |
/// The key type of the InverseMap. |
2039 | 2081 |
typedef typename InvertableMap::Value Key; |
2040 | 2082 |
|
2041 | 2083 |
/// \brief Subscript operator. |
2042 | 2084 |
/// |
2043 |
/// Subscript operator. It gives back always the item |
|
2044 |
/// what was last assigned to the value. |
|
2085 |
/// Subscript operator. It gives back the item |
|
2086 |
/// that was last assigned to the given value. |
|
2045 | 2087 |
Value operator[](const Key& key) const { |
2046 | 2088 |
return _inverted(key); |
2047 | 2089 |
} |
2048 | 2090 |
|
2049 | 2091 |
private: |
2050 | 2092 |
const InvertableMap& _inverted; |
2051 | 2093 |
}; |
2052 | 2094 |
|
2053 |
/// \brief It gives back the |
|
2095 |
/// \brief It gives back the read-only inverse map. |
|
2054 | 2096 |
/// |
2055 |
/// It gives back the |
|
2097 |
/// It gives back the read-only inverse map. |
|
2056 | 2098 |
InverseMap inverse() const { |
2057 | 2099 |
return InverseMap(*this); |
2058 | 2100 |
} |
2059 | 2101 |
|
2060 | 2102 |
}; |
2061 | 2103 |
|
2062 | 2104 |
/// \brief Provides a mutable, continuous and unique descriptor for each |
2063 |
/// item in |
|
2105 |
/// item in a graph. |
|
2064 | 2106 |
/// |
2065 |
/// The DescriptorMap class provides a unique and continuous (but mutable) |
|
2066 |
/// descriptor (id) for each item of the same type (e.g. node) in the |
|
2067 |
/// graph. This id is <ul><li>\b unique: different items (nodes) get |
|
2068 |
/// different ids <li>\b continuous: the range of the ids is the set of |
|
2069 |
/// integers between 0 and \c n-1, where \c n is the number of the items of |
|
2070 |
/// this type (e.g. nodes) (so the id of a node can change if you delete an |
|
2071 |
/// other node, i.e. this id is mutable). </ul> This map can be inverted |
|
2072 |
/// with its member class \c InverseMap, or with the \c operator() member. |
|
2107 |
/// DescriptorMap provides a unique and continuous (but mutable) |
|
2108 |
/// descriptor (id) for each item of the same type (\c Node, \c Arc or |
|
2109 |
/// \c Edge) in a graph. This id is |
|
2110 |
/// - \b unique: different items get different ids, |
|
2111 |
/// - \b continuous: the range of the ids is the set of integers |
|
2112 |
/// between 0 and \c n-1, where \c n is the number of the items of |
|
2113 |
/// this type (\c Node, \c Arc or \c Edge). So the id of an item can |
|
2114 |
/// change if you delete an other item of the same type, i.e. this |
|
2115 |
/// id is mutable. |
|
2073 | 2116 |
/// |
2074 |
/// \tparam _Graph The graph class the \c DescriptorMap belongs to. |
|
2075 |
/// \tparam _Item The Item is the Key of the Map. It may be Node, Arc or |
|
2076 |
/// Edge. |
|
2077 |
template <typename _Graph, typename _Item> |
|
2117 |
/// Thus this id is not (necessarily) the same as what can get using |
|
2118 |
/// the \c id() function of the graph or \ref IdMap. |
|
2119 |
/// This map can be inverted with its member class \c InverseMap, |
|
2120 |
/// or with the \c operator() member. |
|
2121 |
/// |
|
2122 |
/// \tparam GR The graph type. |
|
2123 |
/// \tparam K The key type of the map (\c GR::Node, \c GR::Arc or |
|
2124 |
/// \c GR::Edge). |
|
2125 |
/// |
|
2126 |
/// \see IdMap |
|
2127 |
template <typename GR, typename K> |
|
2078 | 2128 |
class DescriptorMap |
2079 |
: protected ItemSetTraits<_Graph, _Item>::template Map<int>::Type { |
|
2080 |
|
|
2081 |
typedef _Item Item; |
|
2082 |
typedef typename ItemSetTraits<_Graph, _Item>::template Map<int>::Type Map; |
|
2129 |
: protected ItemSetTraits<GR, K>::template Map<int>::Type { |
|
2130 |
|
|
2131 |
typedef typename ItemSetTraits<GR, K>::template Map<int>::Type Map; |
|
2083 | 2132 |
|
2084 | 2133 |
public: |
2085 |
/// The graph class of DescriptorMap. |
|
2086 |
typedef _Graph Graph; |
|
2087 |
|
|
2088 |
/// The key type of DescriptorMap (Node, Arc, Edge). |
|
2089 |
|
|
2134 |
/// The graph type of DescriptorMap. |
|
2135 |
typedef GR Graph; |
|
2136 |
/// The key type of DescriptorMap (\c Node, \c Arc or \c Edge). |
|
2137 |
typedef K Item; |
|
2138 |
/// The key type of DescriptorMap (\c Node, \c Arc or \c Edge). |
|
2139 |
typedef K Key; |
|
2090 | 2140 |
/// The value type of DescriptorMap. |
2091 |
typedef |
|
2141 |
typedef int Value; |
|
2092 | 2142 |
|
2093 | 2143 |
/// \brief Constructor. |
2094 | 2144 |
/// |
2095 | 2145 |
/// Constructor for descriptor map. |
2096 |
explicit DescriptorMap(const Graph& |
|
2146 |
explicit DescriptorMap(const Graph& gr) : Map(gr) { |
|
2097 | 2147 |
Item it; |
2098 | 2148 |
const typename Map::Notifier* nf = Map::notifier(); |
2099 | 2149 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
2100 | 2150 |
Map::set(it, _inv_map.size()); |
2101 | 2151 |
_inv_map.push_back(it); |
2102 | 2152 |
} |
2103 | 2153 |
} |
2104 | 2154 |
|
2105 | 2155 |
protected: |
2106 | 2156 |
|
2107 |
/// \brief |
|
2157 |
/// \brief Adds a new key to the map. |
|
2108 | 2158 |
/// |
2109 | 2159 |
/// Add a new key to the map. It is called by the |
2110 | 2160 |
/// \c AlterationNotifier. |
2111 | 2161 |
virtual void add(const Item& item) { |
2112 | 2162 |
Map::add(item); |
2113 | 2163 |
Map::set(item, _inv_map.size()); |
2114 | 2164 |
_inv_map.push_back(item); |
2115 | 2165 |
} |
2116 | 2166 |
|
2117 | 2167 |
/// \brief Add more new keys to the map. |
2118 | 2168 |
/// |
2119 | 2169 |
/// Add more new keys to the map. It is called by the |
2120 | 2170 |
/// \c AlterationNotifier. |
2121 | 2171 |
virtual void add(const std::vector<Item>& items) { |
2122 | 2172 |
Map::add(items); |
2123 | 2173 |
for (int i = 0; i < int(items.size()); ++i) { |
2124 | 2174 |
Map::set(items[i], _inv_map.size()); |
2125 | 2175 |
_inv_map.push_back(items[i]); |
2126 | 2176 |
} |
2127 | 2177 |
} |
2128 | 2178 |
|
2129 | 2179 |
/// \brief Erase the key from the map. |
2130 | 2180 |
/// |
2131 | 2181 |
/// Erase the key from the map. It is called by the |
2132 | 2182 |
/// \c AlterationNotifier. |
2133 | 2183 |
virtual void erase(const Item& item) { |
2134 | 2184 |
Map::set(_inv_map.back(), Map::operator[](item)); |
2135 | 2185 |
_inv_map[Map::operator[](item)] = _inv_map.back(); |
2136 | 2186 |
_inv_map.pop_back(); |
2137 | 2187 |
Map::erase(item); |
2138 | 2188 |
} |
2139 | 2189 |
|
2140 | 2190 |
/// \brief Erase more keys from the map. |
2141 | 2191 |
/// |
2142 | 2192 |
/// Erase more keys from the map. It is called by the |
2143 | 2193 |
/// \c AlterationNotifier. |
2144 | 2194 |
virtual void erase(const std::vector<Item>& items) { |
2145 | 2195 |
for (int i = 0; i < int(items.size()); ++i) { |
2146 | 2196 |
Map::set(_inv_map.back(), Map::operator[](items[i])); |
2147 | 2197 |
_inv_map[Map::operator[](items[i])] = _inv_map.back(); |
2148 | 2198 |
_inv_map.pop_back(); |
2149 | 2199 |
} |
2150 | 2200 |
Map::erase(items); |
2151 | 2201 |
} |
2152 | 2202 |
|
2153 | 2203 |
/// \brief Build the unique map. |
2154 | 2204 |
/// |
2155 | 2205 |
/// Build the unique map. It is called by the |
... | ... |
@@ -2169,539 +2219,565 @@ |
2169 | 2219 |
/// Clear the keys from the map. It is called by the |
2170 | 2220 |
/// \c AlterationNotifier. |
2171 | 2221 |
virtual void clear() { |
2172 | 2222 |
_inv_map.clear(); |
2173 | 2223 |
Map::clear(); |
2174 | 2224 |
} |
2175 | 2225 |
|
2176 | 2226 |
public: |
2177 | 2227 |
|
2178 | 2228 |
/// \brief Returns the maximal value plus one. |
2179 | 2229 |
/// |
2180 | 2230 |
/// Returns the maximal value plus one in the map. |
2181 | 2231 |
unsigned int size() const { |
2182 | 2232 |
return _inv_map.size(); |
2183 | 2233 |
} |
2184 | 2234 |
|
2185 | 2235 |
/// \brief Swaps the position of the two items in the map. |
2186 | 2236 |
/// |
2187 | 2237 |
/// Swaps the position of the two items in the map. |
2188 | 2238 |
void swap(const Item& p, const Item& q) { |
2189 | 2239 |
int pi = Map::operator[](p); |
2190 | 2240 |
int qi = Map::operator[](q); |
2191 | 2241 |
Map::set(p, qi); |
2192 | 2242 |
_inv_map[qi] = p; |
2193 | 2243 |
Map::set(q, pi); |
2194 | 2244 |
_inv_map[pi] = q; |
2195 | 2245 |
} |
2196 | 2246 |
|
2197 | 2247 |
/// \brief Gives back the \e descriptor of the item. |
2198 | 2248 |
/// |
2199 | 2249 |
/// Gives back the mutable and unique \e descriptor of the map. |
2200 | 2250 |
int operator[](const Item& item) const { |
2201 | 2251 |
return Map::operator[](item); |
2202 | 2252 |
} |
2203 | 2253 |
|
2204 | 2254 |
/// \brief Gives back the item by its descriptor. |
2205 | 2255 |
/// |
2206 | 2256 |
/// Gives back th item by its descriptor. |
2207 | 2257 |
Item operator()(int id) const { |
2208 | 2258 |
return _inv_map[id]; |
2209 | 2259 |
} |
2210 | 2260 |
|
2211 | 2261 |
private: |
2212 | 2262 |
|
2213 | 2263 |
typedef std::vector<Item> Container; |
2214 | 2264 |
Container _inv_map; |
2215 | 2265 |
|
2216 | 2266 |
public: |
2267 |
|
|
2217 | 2268 |
/// \brief The inverse map type of DescriptorMap. |
2218 | 2269 |
/// |
2219 | 2270 |
/// The inverse map type of DescriptorMap. |
2220 | 2271 |
class InverseMap { |
2221 | 2272 |
public: |
2222 |
/// \brief Constructor |
|
2273 |
/// \brief Constructor |
|
2223 | 2274 |
/// |
2224 | 2275 |
/// Constructor of the InverseMap. |
2225 | 2276 |
explicit InverseMap(const DescriptorMap& inverted) |
2226 | 2277 |
: _inverted(inverted) {} |
2227 | 2278 |
|
2228 | 2279 |
|
2229 | 2280 |
/// The value type of the InverseMap. |
2230 | 2281 |
typedef typename DescriptorMap::Key Value; |
2231 | 2282 |
/// The key type of the InverseMap. |
2232 | 2283 |
typedef typename DescriptorMap::Value Key; |
2233 | 2284 |
|
2234 | 2285 |
/// \brief Subscript operator. |
2235 | 2286 |
/// |
2236 | 2287 |
/// Subscript operator. It gives back the item |
2237 |
/// that the descriptor belongs to |
|
2288 |
/// that the descriptor currently belongs to. |
|
2238 | 2289 |
Value operator[](const Key& key) const { |
2239 | 2290 |
return _inverted(key); |
2240 | 2291 |
} |
2241 | 2292 |
|
2242 | 2293 |
/// \brief Size of the map. |
2243 | 2294 |
/// |
2244 | 2295 |
/// Returns the size of the map. |
2245 | 2296 |
unsigned int size() const { |
2246 | 2297 |
return _inverted.size(); |
2247 | 2298 |
} |
2248 | 2299 |
|
2249 | 2300 |
private: |
2250 | 2301 |
const DescriptorMap& _inverted; |
2251 | 2302 |
}; |
2252 | 2303 |
|
2253 | 2304 |
/// \brief Gives back the inverse of the map. |
2254 | 2305 |
/// |
2255 | 2306 |
/// Gives back the inverse of the map. |
2256 | 2307 |
const InverseMap inverse() const { |
2257 | 2308 |
return InverseMap(*this); |
2258 | 2309 |
} |
2259 | 2310 |
}; |
2260 | 2311 |
|
2261 |
/// \brief |
|
2312 |
/// \brief Map of the source nodes of arcs in a digraph. |
|
2262 | 2313 |
/// |
2263 |
/// |
|
2314 |
/// SourceMap provides access for the source node of each arc in a digraph, |
|
2315 |
/// which is returned by the \c source() function of the digraph. |
|
2316 |
/// \tparam GR The digraph type. |
|
2264 | 2317 |
/// \see TargetMap |
2265 |
template <typename |
|
2318 |
template <typename GR> |
|
2266 | 2319 |
class SourceMap { |
2267 | 2320 |
public: |
2268 | 2321 |
|
2269 |
typedef typename Digraph::Node Value; |
|
2270 |
typedef typename Digraph::Arc Key; |
|
2322 |
///\e |
|
2323 |
typedef typename GR::Arc Key; |
|
2324 |
///\e |
|
2325 |
typedef typename GR::Node Value; |
|
2271 | 2326 |
|
2272 | 2327 |
/// \brief Constructor |
2273 | 2328 |
/// |
2274 |
/// Constructor |
|
2329 |
/// Constructor. |
|
2275 | 2330 |
/// \param digraph The digraph that the map belongs to. |
2276 |
explicit SourceMap(const Digraph& digraph) : _digraph(digraph) {} |
|
2277 |
|
|
2278 |
|
|
2331 |
explicit SourceMap(const GR& digraph) : _graph(digraph) {} |
|
2332 |
|
|
2333 |
/// \brief Returns the source node of the given arc. |
|
2279 | 2334 |
/// |
2280 |
/// The subscript operator. |
|
2281 |
/// \param arc The arc |
|
2282 |
/// |
|
2335 |
/// Returns the source node of the given arc. |
|
2283 | 2336 |
Value operator[](const Key& arc) const { |
2284 |
return |
|
2337 |
return _graph.source(arc); |
|
2285 | 2338 |
} |
2286 | 2339 |
|
2287 | 2340 |
private: |
2288 |
const |
|
2341 |
const GR& _graph; |
|
2289 | 2342 |
}; |
2290 | 2343 |
|
2291 | 2344 |
/// \brief Returns a \c SourceMap class. |
2292 | 2345 |
/// |
2293 | 2346 |
/// This function just returns an \c SourceMap class. |
2294 | 2347 |
/// \relates SourceMap |
2295 |
template <typename Digraph> |
|
2296 |
inline SourceMap<Digraph> sourceMap(const Digraph& digraph) { |
|
2297 |
|
|
2348 |
template <typename GR> |
|
2349 |
inline SourceMap<GR> sourceMap(const GR& graph) { |
|
2350 |
return SourceMap<GR>(graph); |
|
2298 | 2351 |
} |
2299 | 2352 |
|
2300 |
/// \brief |
|
2353 |
/// \brief Map of the target nodes of arcs in a digraph. |
|
2301 | 2354 |
/// |
2302 |
/// |
|
2355 |
/// TargetMap provides access for the target node of each arc in a digraph, |
|
2356 |
/// which is returned by the \c target() function of the digraph. |
|
2357 |
/// \tparam GR The digraph type. |
|
2303 | 2358 |
/// \see SourceMap |
2304 |
template <typename |
|
2359 |
template <typename GR> |
|
2305 | 2360 |
class TargetMap { |
2306 | 2361 |
public: |
2307 | 2362 |
|
2308 |
typedef typename Digraph::Node Value; |
|
2309 |
typedef typename Digraph::Arc Key; |
|
2363 |
///\e |
|
2364 |
typedef typename GR::Arc Key; |
|
2365 |
///\e |
|
2366 |
typedef typename GR::Node Value; |
|
2310 | 2367 |
|
2311 | 2368 |
/// \brief Constructor |
2312 | 2369 |
/// |
2313 |
/// Constructor |
|
2370 |
/// Constructor. |
|
2314 | 2371 |
/// \param digraph The digraph that the map belongs to. |
2315 |
explicit TargetMap(const Digraph& digraph) : _digraph(digraph) {} |
|
2316 |
|
|
2317 |
|
|
2372 |
explicit TargetMap(const GR& digraph) : _graph(digraph) {} |
|
2373 |
|
|
2374 |
/// \brief Returns the target node of the given arc. |
|
2318 | 2375 |
/// |
2319 |
/// The subscript operator. |
|
2320 |
/// \param e The arc |
|
2321 |
/// |
|
2376 |
/// Returns the target node of the given arc. |
|
2322 | 2377 |
Value operator[](const Key& e) const { |
2323 |
return |
|
2378 |
return _graph.target(e); |
|
2324 | 2379 |
} |
2325 | 2380 |
|
2326 | 2381 |
private: |
2327 |
const |
|
2382 |
const GR& _graph; |
|
2328 | 2383 |
}; |
2329 | 2384 |
|
2330 | 2385 |
/// \brief Returns a \c TargetMap class. |
2331 | 2386 |
/// |
2332 | 2387 |
/// This function just returns a \c TargetMap class. |
2333 | 2388 |
/// \relates TargetMap |
2334 |
template <typename Digraph> |
|
2335 |
inline TargetMap<Digraph> targetMap(const Digraph& digraph) { |
|
2336 |
|
|
2389 |
template <typename GR> |
|
2390 |
inline TargetMap<GR> targetMap(const GR& graph) { |
|
2391 |
return TargetMap<GR>(graph); |
|
2337 | 2392 |
} |
2338 | 2393 |
|
2339 |
/// \brief |
|
2394 |
/// \brief Map of the "forward" directed arc view of edges in a graph. |
|
2340 | 2395 |
/// |
2341 |
/// |
|
2396 |
/// ForwardMap provides access for the "forward" directed arc view of |
|
2397 |
/// each edge in a graph, which is returned by the \c direct() function |
|
2398 |
/// of the graph with \c true parameter. |
|
2399 |
/// \tparam GR The graph type. |
|
2342 | 2400 |
/// \see BackwardMap |
2343 |
template <typename |
|
2401 |
template <typename GR> |
|
2344 | 2402 |
class ForwardMap { |
2345 | 2403 |
public: |
2346 | 2404 |
|
2347 |
typedef typename Graph::Arc Value; |
|
2348 |
typedef typename Graph::Edge Key; |
|
2405 |
typedef typename GR::Arc Value; |
|
2406 |
typedef typename GR::Edge Key; |
|
2349 | 2407 |
|
2350 | 2408 |
/// \brief Constructor |
2351 | 2409 |
/// |
2352 |
/// Constructor |
|
2410 |
/// Constructor. |
|
2353 | 2411 |
/// \param graph The graph that the map belongs to. |
2354 |
explicit ForwardMap(const Graph& graph) : _graph(graph) {} |
|
2355 |
|
|
2356 |
|
|
2412 |
explicit ForwardMap(const GR& graph) : _graph(graph) {} |
|
2413 |
|
|
2414 |
/// \brief Returns the "forward" directed arc view of the given edge. |
|
2357 | 2415 |
/// |
2358 |
/// The subscript operator. |
|
2359 |
/// \param key An edge |
|
2360 |
/// |
|
2416 |
/// Returns the "forward" directed arc view of the given edge. |
|
2361 | 2417 |
Value operator[](const Key& key) const { |
2362 | 2418 |
return _graph.direct(key, true); |
2363 | 2419 |
} |
2364 | 2420 |
|
2365 | 2421 |
private: |
2366 |
const |
|
2422 |
const GR& _graph; |
|
2367 | 2423 |
}; |
2368 | 2424 |
|
2369 | 2425 |
/// \brief Returns a \c ForwardMap class. |
2370 | 2426 |
/// |
2371 | 2427 |
/// This function just returns an \c ForwardMap class. |
2372 | 2428 |
/// \relates ForwardMap |
2373 |
template <typename Graph> |
|
2374 |
inline ForwardMap<Graph> forwardMap(const Graph& graph) { |
|
2375 |
|
|
2429 |
template <typename GR> |
|
2430 |
inline ForwardMap<GR> forwardMap(const GR& graph) { |
|
2431 |
return ForwardMap<GR>(graph); |
|
2376 | 2432 |
} |
2377 | 2433 |
|
2378 |
/// \brief |
|
2434 |
/// \brief Map of the "backward" directed arc view of edges in a graph. |
|
2379 | 2435 |
/// |
2380 |
/// |
|
2436 |
/// BackwardMap provides access for the "backward" directed arc view of |
|
2437 |
/// each edge in a graph, which is returned by the \c direct() function |
|
2438 |
/// of the graph with \c false parameter. |
|
2439 |
/// \tparam GR The graph type. |
|
2381 | 2440 |
/// \see ForwardMap |
2382 |
template <typename |
|
2441 |
template <typename GR> |
|
2383 | 2442 |
class BackwardMap { |
2384 | 2443 |
public: |
2385 | 2444 |
|
2386 |
typedef typename Graph::Arc Value; |
|
2387 |
typedef typename Graph::Edge Key; |
|
2445 |
typedef typename GR::Arc Value; |
|
2446 |
typedef typename GR::Edge Key; |
|
2388 | 2447 |
|
2389 | 2448 |
/// \brief Constructor |
2390 | 2449 |
/// |
2391 |
/// Constructor |
|
2450 |
/// Constructor. |
|
2392 | 2451 |
/// \param graph The graph that the map belongs to. |
2393 |
explicit BackwardMap(const Graph& graph) : _graph(graph) {} |
|
2394 |
|
|
2395 |
|
|
2452 |
explicit BackwardMap(const GR& graph) : _graph(graph) {} |
|
2453 |
|
|
2454 |
/// \brief Returns the "backward" directed arc view of the given edge. |
|
2396 | 2455 |
/// |
2397 |
/// The subscript operator. |
|
2398 |
/// \param key An edge |
|
2399 |
/// |
|
2456 |
/// Returns the "backward" directed arc view of the given edge. |
|
2400 | 2457 |
Value operator[](const Key& key) const { |
2401 | 2458 |
return _graph.direct(key, false); |
2402 | 2459 |
} |
2403 | 2460 |
|
2404 | 2461 |
private: |
2405 |
const |
|
2462 |
const GR& _graph; |
|
2406 | 2463 |
}; |
2407 | 2464 |
|
2408 | 2465 |
/// \brief Returns a \c BackwardMap class |
2409 | 2466 |
|
2410 | 2467 |
/// This function just returns a \c BackwardMap class. |
2411 | 2468 |
/// \relates BackwardMap |
2412 |
template <typename Graph> |
|
2413 |
inline BackwardMap<Graph> backwardMap(const Graph& graph) { |
|
2414 |
|
|
2469 |
template <typename GR> |
|
2470 |
inline BackwardMap<GR> backwardMap(const GR& graph) { |
|
2471 |
return BackwardMap<GR>(graph); |
|
2415 | 2472 |
} |
2416 | 2473 |
|
2417 |
/// \brief Potential difference map |
|
2418 |
/// |
|
2419 |
/// If there is an potential map on the nodes then we |
|
2420 |
/// can get an arc map as we get the substraction of the |
|
2421 |
/// values of the target and source. |
|
2422 |
template <typename Digraph, typename NodeMap> |
|
2423 |
class PotentialDifferenceMap { |
|
2424 |
public: |
|
2425 |
typedef typename Digraph::Arc Key; |
|
2426 |
typedef typename NodeMap::Value Value; |
|
2427 |
|
|
2428 |
/// \brief Constructor |
|
2429 |
/// |
|
2430 |
/// Contructor of the map |
|
2431 |
explicit PotentialDifferenceMap(const Digraph& digraph, |
|
2432 |
const NodeMap& potential) |
|
2433 |
: _digraph(digraph), _potential(potential) {} |
|
2434 |
|
|
2435 |
/// \brief Const subscription operator |
|
2436 |
/// |
|
2437 |
/// Const subscription operator |
|
2438 |
Value operator[](const Key& arc) const { |
|
2439 |
return _potential[_digraph.target(arc)] - |
|
2440 |
_potential[_digraph.source(arc)]; |
|
2441 |
} |
|
2442 |
|
|
2443 |
private: |
|
2444 |
const Digraph& _digraph; |
|
2445 |
const NodeMap& _potential; |
|
2446 |
}; |
|
2447 |
|
|
2448 |
/// \brief Returns a PotentialDifferenceMap. |
|
2449 |
/// |
|
2450 |
/// This function just returns a PotentialDifferenceMap. |
|
2451 |
/// \relates PotentialDifferenceMap |
|
2452 |
template <typename Digraph, typename NodeMap> |
|
2453 |
PotentialDifferenceMap<Digraph, NodeMap> |
|
2454 |
potentialDifferenceMap(const Digraph& digraph, const NodeMap& potential) { |
|
2455 |
return PotentialDifferenceMap<Digraph, NodeMap>(digraph, potential); |
|
2456 |
} |
|
2457 |
|
|
2458 |
/// \brief Map of the node in-degrees. |
|
2474 |
/// \brief Map of the in-degrees of nodes in a digraph. |
|
2459 | 2475 |
/// |
2460 | 2476 |
/// This map returns the in-degree of a node. Once it is constructed, |
2461 |
/// the degrees are stored in a standard NodeMap, so each query is done |
|
2477 |
/// the degrees are stored in a standard \c NodeMap, so each query is done |
|
2462 | 2478 |
/// in constant time. On the other hand, the values are updated automatically |
2463 | 2479 |
/// whenever the digraph changes. |
2464 | 2480 |
/// |
2465 |
/// \warning Besides addNode() and addArc(), a digraph structure may provide |
|
2466 |
/// alternative ways to modify the digraph. The correct behavior of InDegMap |
|
2467 |
/// is not guarantied if these additional features are used. For example |
|
2468 |
/// the functions \ref ListDigraph::changeSource() "changeSource()", |
|
2481 |
/// \warning Besides \c addNode() and \c addArc(), a digraph structure |
|
2482 |
/// may provide alternative ways to modify the digraph. |
|
2483 |
/// The correct behavior of InDegMap is not guarantied if these additional |
|
2484 |
/// features are used. For example the functions |
|
2485 |
/// \ref ListDigraph::changeSource() "changeSource()", |
|
2469 | 2486 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and |
2470 | 2487 |
/// \ref ListDigraph::reverseArc() "reverseArc()" |
2471 | 2488 |
/// of \ref ListDigraph will \e not update the degree values correctly. |
2472 | 2489 |
/// |
2473 | 2490 |
/// \sa OutDegMap |
2474 |
|
|
2475 |
template <typename _Digraph> |
|
2491 |
template <typename GR> |
|
2476 | 2492 |
class InDegMap |
2477 |
: protected ItemSetTraits< |
|
2493 |
: protected ItemSetTraits<GR, typename GR::Arc> |
|
2478 | 2494 |
::ItemNotifier::ObserverBase { |
2479 | 2495 |
|
2480 | 2496 |
public: |
2481 |
|
|
2482 |
typedef _Digraph Digraph; |
|
2497 |
|
|
2498 |
/// The digraph type |
|
2499 |
typedef GR Digraph; |
|
2500 |
/// The key type |
|
2501 |
typedef typename Digraph::Node Key; |
|
2502 |
/// The value type |
|
2483 | 2503 |
typedef int Value; |
2484 |
typedef typename Digraph::Node Key; |
|
2485 | 2504 |
|
2486 | 2505 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc> |
2487 | 2506 |
::ItemNotifier::ObserverBase Parent; |
2488 | 2507 |
|
2489 | 2508 |
private: |
2490 | 2509 |
|
2491 | 2510 |
class AutoNodeMap |
2492 | 2511 |
: public ItemSetTraits<Digraph, Key>::template Map<int>::Type { |
2493 | 2512 |
public: |
2494 | 2513 |
|
2495 | 2514 |
typedef typename ItemSetTraits<Digraph, Key>:: |
2496 | 2515 |
template Map<int>::Type Parent; |
2497 | 2516 |
|
2498 | 2517 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {} |
2499 | 2518 |
|
2500 | 2519 |
virtual void add(const Key& key) { |
2501 | 2520 |
Parent::add(key); |
2502 | 2521 |
Parent::set(key, 0); |
2503 | 2522 |
} |
2504 | 2523 |
|
2505 | 2524 |
virtual void add(const std::vector<Key>& keys) { |
2506 | 2525 |
Parent::add(keys); |
2507 | 2526 |
for (int i = 0; i < int(keys.size()); ++i) { |
2508 | 2527 |
Parent::set(keys[i], 0); |
2509 | 2528 |
} |
2510 | 2529 |
} |
2511 | 2530 |
|
2512 | 2531 |
virtual void build() { |
2513 | 2532 |
Parent::build(); |
2514 | 2533 |
Key it; |
2515 | 2534 |
typename Parent::Notifier* nf = Parent::notifier(); |
2516 | 2535 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
2517 | 2536 |
Parent::set(it, 0); |
2518 | 2537 |
} |
2519 | 2538 |
} |
2520 | 2539 |
}; |
2521 | 2540 |
|
2522 | 2541 |
public: |
2523 | 2542 |
|
2524 | 2543 |
/// \brief Constructor. |
2525 | 2544 |
/// |
2526 |
/// Constructor for creating in-degree map. |
|
2527 |
explicit InDegMap(const Digraph& digraph) |
|
2528 |
|
|
2545 |
/// Constructor for creating an in-degree map. |
|
2546 |
explicit InDegMap(const Digraph& graph) |
|
2547 |
: _digraph(graph), _deg(graph) { |
|
2529 | 2548 |
Parent::attach(_digraph.notifier(typename Digraph::Arc())); |
2530 | 2549 |
|
2531 | 2550 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
2532 | 2551 |
_deg[it] = countInArcs(_digraph, it); |
2533 | 2552 |
} |
2534 | 2553 |
} |
2535 | 2554 |
|
2555 |
/// \brief Gives back the in-degree of a Node. |
|
2556 |
/// |
|
2536 | 2557 |
/// Gives back the in-degree of a Node. |
2537 | 2558 |
int operator[](const Key& key) const { |
2538 | 2559 |
return _deg[key]; |
2539 | 2560 |
} |
2540 | 2561 |
|
2541 | 2562 |
protected: |
2542 | 2563 |
|
2543 | 2564 |
typedef typename Digraph::Arc Arc; |
2544 | 2565 |
|
2545 | 2566 |
virtual void add(const Arc& arc) { |
2546 | 2567 |
++_deg[_digraph.target(arc)]; |
2547 | 2568 |
} |
2548 | 2569 |
|
2549 | 2570 |
virtual void add(const std::vector<Arc>& arcs) { |
2550 | 2571 |
for (int i = 0; i < int(arcs.size()); ++i) { |
2551 | 2572 |
++_deg[_digraph.target(arcs[i])]; |
2552 | 2573 |
} |
2553 | 2574 |
} |
2554 | 2575 |
|
2555 | 2576 |
virtual void erase(const Arc& arc) { |
2556 | 2577 |
--_deg[_digraph.target(arc)]; |
2557 | 2578 |
} |
2558 | 2579 |
|
2559 | 2580 |
virtual void erase(const std::vector<Arc>& arcs) { |
2560 | 2581 |
for (int i = 0; i < int(arcs.size()); ++i) { |
2561 | 2582 |
--_deg[_digraph.target(arcs[i])]; |
2562 | 2583 |
} |
2563 | 2584 |
} |
2564 | 2585 |
|
2565 | 2586 |
virtual void build() { |
2566 | 2587 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
2567 | 2588 |
_deg[it] = countInArcs(_digraph, it); |
2568 | 2589 |
} |
2569 | 2590 |
} |
2570 | 2591 |
|
2571 | 2592 |
virtual void clear() { |
2572 | 2593 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
2573 | 2594 |
_deg[it] = 0; |
2574 | 2595 |
} |
2575 | 2596 |
} |
2576 | 2597 |
private: |
2577 | 2598 |
|
2578 | 2599 |
const Digraph& _digraph; |
2579 | 2600 |
AutoNodeMap _deg; |
2580 | 2601 |
}; |
2581 | 2602 |
|
2582 |
/// \brief Map of the |
|
2603 |
/// \brief Map of the out-degrees of nodes in a digraph. |
|
2583 | 2604 |
/// |
2584 | 2605 |
/// This map returns the out-degree of a node. Once it is constructed, |
2585 |
/// the degrees are stored in a standard NodeMap, so each query is done |
|
2606 |
/// the degrees are stored in a standard \c NodeMap, so each query is done |
|
2586 | 2607 |
/// in constant time. On the other hand, the values are updated automatically |
2587 | 2608 |
/// whenever the digraph changes. |
2588 | 2609 |
/// |
2589 |
/// \warning Besides addNode() and addArc(), a digraph structure may provide |
|
2590 |
/// alternative ways to modify the digraph. The correct behavior of OutDegMap |
|
2591 |
/// is not guarantied if these additional features are used. For example |
|
2592 |
/// the functions \ref ListDigraph::changeSource() "changeSource()", |
|
2610 |
/// \warning Besides \c addNode() and \c addArc(), a digraph structure |
|
2611 |
/// may provide alternative ways to modify the digraph. |
|
2612 |
/// The correct behavior of OutDegMap is not guarantied if these additional |
|
2613 |
/// features are used. For example the functions |
|
2614 |
/// \ref ListDigraph::changeSource() "changeSource()", |
|
2593 | 2615 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and |
2594 | 2616 |
/// \ref ListDigraph::reverseArc() "reverseArc()" |
2595 | 2617 |
/// of \ref ListDigraph will \e not update the degree values correctly. |
2596 | 2618 |
/// |
2597 | 2619 |
/// \sa InDegMap |
2598 |
|
|
2599 |
template <typename _Digraph> |
|
2620 |
template <typename GR> |
|
2600 | 2621 |
class OutDegMap |
2601 |
: protected ItemSetTraits< |
|
2622 |
: protected ItemSetTraits<GR, typename GR::Arc> |
|
2602 | 2623 |
::ItemNotifier::ObserverBase { |
2603 | 2624 |
|
2604 | 2625 |
public: |
2605 | 2626 |
|
2606 |
|
|
2627 |
/// The digraph type |
|
2628 |
typedef GR Digraph; |
|
2629 |
/// The key type |
|
2630 |
typedef typename Digraph::Node Key; |
|
2631 |
/// The value type |
|
2607 | 2632 |
typedef int Value; |
2608 |
typedef typename Digraph::Node Key; |
|
2609 | 2633 |
|
2610 | 2634 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc> |
2611 | 2635 |
::ItemNotifier::ObserverBase Parent; |
2612 | 2636 |
|
2613 | 2637 |
private: |
2614 | 2638 |
|
2615 | 2639 |
class AutoNodeMap |
2616 | 2640 |
: public ItemSetTraits<Digraph, Key>::template Map<int>::Type { |
2617 | 2641 |
public: |
2618 | 2642 |
|
2619 | 2643 |
typedef typename ItemSetTraits<Digraph, Key>:: |
2620 | 2644 |
template Map<int>::Type Parent; |
2621 | 2645 |
|
2622 | 2646 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {} |
2623 | 2647 |
|
2624 | 2648 |
virtual void add(const Key& key) { |
2625 | 2649 |
Parent::add(key); |
2626 | 2650 |
Parent::set(key, 0); |
2627 | 2651 |
} |
2628 | 2652 |
virtual void add(const std::vector<Key>& keys) { |
2629 | 2653 |
Parent::add(keys); |
2630 | 2654 |
for (int i = 0; i < int(keys.size()); ++i) { |
2631 | 2655 |
Parent::set(keys[i], 0); |
2632 | 2656 |
} |
2633 | 2657 |
} |
2634 | 2658 |
virtual void build() { |
2635 | 2659 |
Parent::build(); |
2636 | 2660 |
Key it; |
2637 | 2661 |
typename Parent::Notifier* nf = Parent::notifier(); |
2638 | 2662 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
2639 | 2663 |
Parent::set(it, 0); |
2640 | 2664 |
} |
2641 | 2665 |
} |
2642 | 2666 |
}; |
2643 | 2667 |
|
2644 | 2668 |
public: |
2645 | 2669 |
|
2646 | 2670 |
/// \brief Constructor. |
2647 | 2671 |
/// |
2648 |
/// Constructor for creating out-degree map. |
|
2649 |
explicit OutDegMap(const Digraph& digraph) |
|
2650 |
|
|
2672 |
/// Constructor for creating an out-degree map. |
|
2673 |
explicit OutDegMap(const Digraph& graph) |
|
2674 |
: _digraph(graph), _deg(graph) { |
|
2651 | 2675 |
Parent::attach(_digraph.notifier(typename Digraph::Arc())); |
2652 | 2676 |
|
2653 | 2677 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
2654 | 2678 |
_deg[it] = countOutArcs(_digraph, it); |
2655 | 2679 |
} |
2656 | 2680 |
} |
2657 | 2681 |
|
2682 |
/// \brief Gives back the out-degree of a Node. |
|
2683 |
/// |
|
2658 | 2684 |
/// Gives back the out-degree of a Node. |
2659 | 2685 |
int operator[](const Key& key) const { |
2660 | 2686 |
return _deg[key]; |
2661 | 2687 |
} |
2662 | 2688 |
|
2663 | 2689 |
protected: |
2664 | 2690 |
|
2665 | 2691 |
typedef typename Digraph::Arc Arc; |
2666 | 2692 |
|
2667 | 2693 |
virtual void add(const Arc& arc) { |
2668 | 2694 |
++_deg[_digraph.source(arc)]; |
2669 | 2695 |
} |
2670 | 2696 |
|
2671 | 2697 |
virtual void add(const std::vector<Arc>& arcs) { |
2672 | 2698 |
for (int i = 0; i < int(arcs.size()); ++i) { |
2673 | 2699 |
++_deg[_digraph.source(arcs[i])]; |
2674 | 2700 |
} |
2675 | 2701 |
} |
2676 | 2702 |
|
2677 | 2703 |
virtual void erase(const Arc& arc) { |
2678 | 2704 |
--_deg[_digraph.source(arc)]; |
2679 | 2705 |
} |
2680 | 2706 |
|
2681 | 2707 |
virtual void erase(const std::vector<Arc>& arcs) { |
2682 | 2708 |
for (int i = 0; i < int(arcs.size()); ++i) { |
2683 | 2709 |
--_deg[_digraph.source(arcs[i])]; |
2684 | 2710 |
} |
2685 | 2711 |
} |
2686 | 2712 |
|
2687 | 2713 |
virtual void build() { |
2688 | 2714 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
2689 | 2715 |
_deg[it] = countOutArcs(_digraph, it); |
2690 | 2716 |
} |
2691 | 2717 |
} |
2692 | 2718 |
|
2693 | 2719 |
virtual void clear() { |
2694 | 2720 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
2695 | 2721 |
_deg[it] = 0; |
2696 | 2722 |
} |
2697 | 2723 |
} |
2698 | 2724 |
private: |
2699 | 2725 |
|
2700 | 2726 |
const Digraph& _digraph; |
2701 | 2727 |
AutoNodeMap _deg; |
2702 | 2728 |
}; |
2703 | 2729 |
|
2730 |
/// \brief Potential difference map |
|
2731 |
/// |
|
2732 |
/// PotentialMap returns the difference between the potentials of the |
|
2733 |
/// source and target nodes of each arc in a digraph, i.e. it returns |
|
2734 |
/// \code |
|
2735 |
/// potential[gr.target(arc)] - potential[gr.source(arc)]. |
|
2736 |
/// \endcode |
|
2737 |
/// \tparam GR The digraph type. |
|
2738 |
/// \tparam POT A node map storing the potentials. |
|
2739 |
template <typename GR, typename POT> |
|
2740 |
class PotentialDifferenceMap { |
|
2741 |
public: |
|
2742 |
/// Key type |
|
2743 |
typedef typename GR::Arc Key; |
|
2744 |
/// Value type |
|
2745 |
typedef typename POT::Value Value; |
|
2746 |
|
|
2747 |
/// \brief Constructor |
|
2748 |
/// |
|
2749 |
/// Contructor of the map. |
|
2750 |
explicit PotentialDifferenceMap(const GR& gr, |
|
2751 |
const POT& potential) |
|
2752 |
: _digraph(gr), _potential(potential) {} |
|
2753 |
|
|
2754 |
/// \brief Returns the potential difference for the given arc. |
|
2755 |
/// |
|
2756 |
/// Returns the potential difference for the given arc, i.e. |
|
2757 |
/// \code |
|
2758 |
/// potential[gr.target(arc)] - potential[gr.source(arc)]. |
|
2759 |
/// \endcode |
|
2760 |
Value operator[](const Key& arc) const { |
|
2761 |
return _potential[_digraph.target(arc)] - |
|
2762 |
_potential[_digraph.source(arc)]; |
|
2763 |
} |
|
2764 |
|
|
2765 |
private: |
|
2766 |
const GR& _digraph; |
|
2767 |
const POT& _potential; |
|
2768 |
}; |
|
2769 |
|
|
2770 |
/// \brief Returns a PotentialDifferenceMap. |
|
2771 |
/// |
|
2772 |
/// This function just returns a PotentialDifferenceMap. |
|
2773 |
/// \relates PotentialDifferenceMap |
|
2774 |
template <typename GR, typename POT> |
|
2775 |
PotentialDifferenceMap<GR, POT> |
|
2776 |
potentialDifferenceMap(const GR& gr, const POT& potential) { |
|
2777 |
return PotentialDifferenceMap<GR, POT>(gr, potential); |
|
2778 |
} |
|
2779 |
|
|
2704 | 2780 |
/// @} |
2705 | 2781 |
} |
2706 | 2782 |
|
2707 | 2783 |
#endif // LEMON_MAPS_H |
... | ... |
@@ -10,102 +10,102 @@ |
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_MAX_MATCHING_H |
20 | 20 |
#define LEMON_MAX_MATCHING_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <queue> |
24 | 24 |
#include <set> |
25 | 25 |
#include <limits> |
26 | 26 |
|
27 | 27 |
#include <lemon/core.h> |
28 | 28 |
#include <lemon/unionfind.h> |
29 | 29 |
#include <lemon/bin_heap.h> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
|
32 | 32 |
///\ingroup matching |
33 | 33 |
///\file |
34 | 34 |
///\brief Maximum matching algorithms in general graphs. |
35 | 35 |
|
36 | 36 |
namespace lemon { |
37 | 37 |
|
38 | 38 |
/// \ingroup matching |
39 | 39 |
/// |
40 | 40 |
/// \brief Edmonds' alternating forest maximum matching algorithm. |
41 | 41 |
/// |
42 | 42 |
/// This class implements Edmonds' alternating forest matching |
43 | 43 |
/// algorithm. The algorithm can be started from an arbitrary initial |
44 | 44 |
/// matching (the default is the empty one) |
45 | 45 |
/// |
46 | 46 |
/// The dual solution of the problem is a map of the nodes to |
47 | 47 |
/// MaxMatching::Status, having values \c EVEN/D, \c ODD/A and \c |
48 | 48 |
/// MATCHED/C showing the Gallai-Edmonds decomposition of the |
49 | 49 |
/// graph. The nodes in \c EVEN/D induce a graph with |
50 | 50 |
/// factor-critical components, the nodes in \c ODD/A form the |
51 | 51 |
/// barrier, and the nodes in \c MATCHED/C induce a graph having a |
52 | 52 |
/// perfect matching. The number of the factor-critical components |
53 | 53 |
/// minus the number of barrier nodes is a lower bound on the |
54 | 54 |
/// unmatched nodes, and the matching is optimal if and only if this bound is |
55 | 55 |
/// tight. This decomposition can be attained by calling \c |
56 | 56 |
/// decomposition() after running the algorithm. |
57 | 57 |
/// |
58 |
/// \param _Graph The graph type the algorithm runs on. |
|
59 |
template <typename _Graph> |
|
58 |
/// \param GR The graph type the algorithm runs on. |
|
59 |
template <typename GR> |
|
60 | 60 |
class MaxMatching { |
61 | 61 |
public: |
62 | 62 |
|
63 |
typedef |
|
63 |
typedef GR Graph; |
|
64 | 64 |
typedef typename Graph::template NodeMap<typename Graph::Arc> |
65 | 65 |
MatchingMap; |
66 | 66 |
|
67 | 67 |
///\brief Indicates the Gallai-Edmonds decomposition of the graph. |
68 | 68 |
/// |
69 | 69 |
///Indicates the Gallai-Edmonds decomposition of the graph. The |
70 | 70 |
///nodes with Status \c EVEN/D induce a graph with factor-critical |
71 | 71 |
///components, the nodes in \c ODD/A form the canonical barrier, |
72 | 72 |
///and the nodes in \c MATCHED/C induce a graph having a perfect |
73 | 73 |
///matching. |
74 | 74 |
enum Status { |
75 | 75 |
EVEN = 1, D = 1, MATCHED = 0, C = 0, ODD = -1, A = -1, UNMATCHED = -2 |
76 | 76 |
}; |
77 | 77 |
|
78 | 78 |
typedef typename Graph::template NodeMap<Status> StatusMap; |
79 | 79 |
|
80 | 80 |
private: |
81 | 81 |
|
82 | 82 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
83 | 83 |
|
84 | 84 |
typedef UnionFindEnum<IntNodeMap> BlossomSet; |
85 | 85 |
typedef ExtendFindEnum<IntNodeMap> TreeSet; |
86 | 86 |
typedef RangeMap<Node> NodeIntMap; |
87 | 87 |
typedef MatchingMap EarMap; |
88 | 88 |
typedef std::vector<Node> NodeQueue; |
89 | 89 |
|
90 | 90 |
const Graph& _graph; |
91 | 91 |
MatchingMap* _matching; |
92 | 92 |
StatusMap* _status; |
93 | 93 |
|
94 | 94 |
EarMap* _ear; |
95 | 95 |
|
96 | 96 |
IntNodeMap* _blossom_set_index; |
97 | 97 |
BlossomSet* _blossom_set; |
98 | 98 |
NodeIntMap* _blossom_rep; |
99 | 99 |
|
100 | 100 |
IntNodeMap* _tree_set_index; |
101 | 101 |
TreeSet* _tree_set; |
102 | 102 |
|
103 | 103 |
NodeQueue _node_queue; |
104 | 104 |
int _process, _postpone, _last; |
105 | 105 |
|
106 | 106 |
int _node_num; |
107 | 107 |
|
108 | 108 |
private: |
109 | 109 |
|
110 | 110 |
void createStructures() { |
111 | 111 |
_node_num = countNodes(_graph); |
... | ... |
@@ -418,97 +418,97 @@ |
418 | 418 |
/// functions first, then you can start the algorithm with the \ref |
419 | 419 |
/// startSparse() or startDense() functions. |
420 | 420 |
|
421 | 421 |
///@{ |
422 | 422 |
|
423 | 423 |
/// \brief Sets the actual matching to the empty matching. |
424 | 424 |
/// |
425 | 425 |
/// Sets the actual matching to the empty matching. |
426 | 426 |
/// |
427 | 427 |
void init() { |
428 | 428 |
createStructures(); |
429 | 429 |
for(NodeIt n(_graph); n != INVALID; ++n) { |
430 | 430 |
_matching->set(n, INVALID); |
431 | 431 |
_status->set(n, UNMATCHED); |
432 | 432 |
} |
433 | 433 |
} |
434 | 434 |
|
435 | 435 |
///\brief Finds an initial matching in a greedy way |
436 | 436 |
/// |
437 | 437 |
///It finds an initial matching in a greedy way. |
438 | 438 |
void greedyInit() { |
439 | 439 |
createStructures(); |
440 | 440 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
441 | 441 |
_matching->set(n, INVALID); |
442 | 442 |
_status->set(n, UNMATCHED); |
443 | 443 |
} |
444 | 444 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
445 | 445 |
if ((*_matching)[n] == INVALID) { |
446 | 446 |
for (OutArcIt a(_graph, n); a != INVALID ; ++a) { |
447 | 447 |
Node v = _graph.target(a); |
448 | 448 |
if ((*_matching)[v] == INVALID && v != n) { |
449 | 449 |
_matching->set(n, a); |
450 | 450 |
_status->set(n, MATCHED); |
451 | 451 |
_matching->set(v, _graph.oppositeArc(a)); |
452 | 452 |
_status->set(v, MATCHED); |
453 | 453 |
break; |
454 | 454 |
} |
455 | 455 |
} |
456 | 456 |
} |
457 | 457 |
} |
458 | 458 |
} |
459 | 459 |
|
460 | 460 |
|
461 | 461 |
/// \brief Initialize the matching from a map containing. |
462 | 462 |
/// |
463 | 463 |
/// Initialize the matching from a \c bool valued \c Edge map. This |
464 | 464 |
/// map must have the property that there are no two incident edges |
465 | 465 |
/// with true value, ie. it contains a matching. |
466 |
/// \return |
|
466 |
/// \return \c true if the map contains a matching. |
|
467 | 467 |
template <typename MatchingMap> |
468 | 468 |
bool matchingInit(const MatchingMap& matching) { |
469 | 469 |
createStructures(); |
470 | 470 |
|
471 | 471 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
472 | 472 |
_matching->set(n, INVALID); |
473 | 473 |
_status->set(n, UNMATCHED); |
474 | 474 |
} |
475 | 475 |
for(EdgeIt e(_graph); e!=INVALID; ++e) { |
476 | 476 |
if (matching[e]) { |
477 | 477 |
|
478 | 478 |
Node u = _graph.u(e); |
479 | 479 |
if ((*_matching)[u] != INVALID) return false; |
480 | 480 |
_matching->set(u, _graph.direct(e, true)); |
481 | 481 |
_status->set(u, MATCHED); |
482 | 482 |
|
483 | 483 |
Node v = _graph.v(e); |
484 | 484 |
if ((*_matching)[v] != INVALID) return false; |
485 | 485 |
_matching->set(v, _graph.direct(e, false)); |
486 | 486 |
_status->set(v, MATCHED); |
487 | 487 |
} |
488 | 488 |
} |
489 | 489 |
return true; |
490 | 490 |
} |
491 | 491 |
|
492 | 492 |
/// \brief Starts Edmonds' algorithm |
493 | 493 |
/// |
494 | 494 |
/// If runs the original Edmonds' algorithm. |
495 | 495 |
void startSparse() { |
496 | 496 |
for(NodeIt n(_graph); n != INVALID; ++n) { |
497 | 497 |
if ((*_status)[n] == UNMATCHED) { |
498 | 498 |
(*_blossom_rep)[_blossom_set->insert(n)] = n; |
499 | 499 |
_tree_set->insert(n); |
500 | 500 |
_status->set(n, EVEN); |
501 | 501 |
processSparse(n); |
502 | 502 |
} |
503 | 503 |
} |
504 | 504 |
} |
505 | 505 |
|
506 | 506 |
/// \brief Starts Edmonds' algorithm. |
507 | 507 |
/// |
508 | 508 |
/// It runs Edmonds' algorithm with a heuristic of postponing |
509 | 509 |
/// shrinks, therefore resulting in a faster algorithm for dense graphs. |
510 | 510 |
void startDense() { |
511 | 511 |
for(NodeIt n(_graph); n != INVALID; ++n) { |
512 | 512 |
if ((*_status)[n] == UNMATCHED) { |
513 | 513 |
(*_blossom_rep)[_blossom_set->insert(n)] = n; |
514 | 514 |
_tree_set->insert(n); |
... | ... |
@@ -568,137 +568,140 @@ |
568 | 568 |
/// INVALID if the \c node is not covered by the actual matching. |
569 | 569 |
Arc matching(const Node& n) const { |
570 | 570 |
return (*_matching)[n]; |
571 | 571 |
} |
572 | 572 |
|
573 | 573 |
///\brief Returns the mate of a node in the actual matching. |
574 | 574 |
/// |
575 | 575 |
///Returns the mate of a \c node in the actual matching or |
576 | 576 |
///INVALID if the \c node is not covered by the actual matching. |
577 | 577 |
Node mate(const Node& n) const { |
578 | 578 |
return (*_matching)[n] != INVALID ? |
579 | 579 |
_graph.target((*_matching)[n]) : INVALID; |
580 | 580 |
} |
581 | 581 |
|
582 | 582 |
/// @} |
583 | 583 |
|
584 | 584 |
/// \name Dual solution |
585 | 585 |
/// Functions to get the dual solution, ie. the decomposition. |
586 | 586 |
|
587 | 587 |
/// @{ |
588 | 588 |
|
589 | 589 |
/// \brief Returns the class of the node in the Edmonds-Gallai |
590 | 590 |
/// decomposition. |
591 | 591 |
/// |
592 | 592 |
/// Returns the class of the node in the Edmonds-Gallai |
593 | 593 |
/// decomposition. |
594 | 594 |
Status decomposition(const Node& n) const { |
595 | 595 |
return (*_status)[n]; |
596 | 596 |
} |
597 | 597 |
|
598 | 598 |
/// \brief Returns true when the node is in the barrier. |
599 | 599 |
/// |
600 | 600 |
/// Returns true when the node is in the barrier. |
601 | 601 |
bool barrier(const Node& n) const { |
602 | 602 |
return (*_status)[n] == ODD; |
603 | 603 |
} |
604 | 604 |
|
605 | 605 |
/// @} |
606 | 606 |
|
607 | 607 |
}; |
608 | 608 |
|
609 | 609 |
/// \ingroup matching |
610 | 610 |
/// |
611 | 611 |
/// \brief Weighted matching in general graphs |
612 | 612 |
/// |
613 | 613 |
/// This class provides an efficient implementation of Edmond's |
614 | 614 |
/// maximum weighted matching algorithm. The implementation is based |
615 | 615 |
/// on extensive use of priority queues and provides |
616 |
/// \f$O(nm\log |
|
616 |
/// \f$O(nm\log n)\f$ time complexity. |
|
617 | 617 |
/// |
618 | 618 |
/// The maximum weighted matching problem is to find undirected |
619 | 619 |
/// edges in the graph with maximum overall weight and no two of |
620 | 620 |
/// them shares their ends. The problem can be formulated with the |
621 | 621 |
/// following linear program. |
622 | 622 |
/// \f[ \sum_{e \in \delta(u)}x_e \le 1 \quad \forall u\in V\f] |
623 | 623 |
/** \f[ \sum_{e \in \gamma(B)}x_e \le \frac{\vert B \vert - 1}{2} |
624 | 624 |
\quad \forall B\in\mathcal{O}\f] */ |
625 | 625 |
/// \f[x_e \ge 0\quad \forall e\in E\f] |
626 | 626 |
/// \f[\max \sum_{e\in E}x_ew_e\f] |
627 | 627 |
/// where \f$\delta(X)\f$ is the set of edges incident to a node in |
628 | 628 |
/// \f$X\f$, \f$\gamma(X)\f$ is the set of edges with both ends in |
629 | 629 |
/// \f$X\f$ and \f$\mathcal{O}\f$ is the set of odd cardinality |
630 | 630 |
/// subsets of the nodes. |
631 | 631 |
/// |
632 | 632 |
/// The algorithm calculates an optimal matching and a proof of the |
633 | 633 |
/// optimality. The solution of the dual problem can be used to check |
634 | 634 |
/// the result of the algorithm. The dual linear problem is the |
635 | 635 |
/** \f[ y_u + y_v + \sum_{B \in \mathcal{O}, uv \in \gamma(B)} |
636 | 636 |
z_B \ge w_{uv} \quad \forall uv\in E\f] */ |
637 | 637 |
/// \f[y_u \ge 0 \quad \forall u \in V\f] |
638 | 638 |
/// \f[z_B \ge 0 \quad \forall B \in \mathcal{O}\f] |
639 | 639 |
/** \f[\min \sum_{u \in V}y_u + \sum_{B \in \mathcal{O}} |
640 | 640 |
\frac{\vert B \vert - 1}{2}z_B\f] */ |
641 | 641 |
/// |
642 | 642 |
/// The algorithm can be executed with \c run() or the \c init() and |
643 | 643 |
/// then the \c start() member functions. After it the matching can |
644 | 644 |
/// be asked with \c matching() or mate() functions. The dual |
645 | 645 |
/// solution can be get with \c nodeValue(), \c blossomNum() and \c |
646 | 646 |
/// blossomValue() members and \ref MaxWeightedMatching::BlossomIt |
647 | 647 |
/// "BlossomIt" nested class, which is able to iterate on the nodes |
648 | 648 |
/// of a blossom. If the value type is integral then the dual |
649 | 649 |
/// solution is multiplied by \ref MaxWeightedMatching::dualScale "4". |
650 |
template <typename _Graph, |
|
651 |
typename _WeightMap = typename _Graph::template EdgeMap<int> > |
|
650 |
template <typename GR, |
|
651 |
typename WM = typename GR::template EdgeMap<int> > |
|
652 | 652 |
class MaxWeightedMatching { |
653 | 653 |
public: |
654 | 654 |
|
655 |
typedef _Graph Graph; |
|
656 |
typedef _WeightMap WeightMap; |
|
655 |
///\e |
|
656 |
typedef GR Graph; |
|
657 |
///\e |
|
658 |
typedef WM WeightMap; |
|
659 |
///\e |
|
657 | 660 |
typedef typename WeightMap::Value Value; |
658 | 661 |
|
659 | 662 |
/// \brief Scaling factor for dual solution |
660 | 663 |
/// |
661 | 664 |
/// Scaling factor for dual solution, it is equal to 4 or 1 |
662 | 665 |
/// according to the value type. |
663 | 666 |
static const int dualScale = |
664 | 667 |
std::numeric_limits<Value>::is_integer ? 4 : 1; |
665 | 668 |
|
666 | 669 |
typedef typename Graph::template NodeMap<typename Graph::Arc> |
667 | 670 |
MatchingMap; |
668 | 671 |
|
669 | 672 |
private: |
670 | 673 |
|
671 | 674 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
672 | 675 |
|
673 | 676 |
typedef typename Graph::template NodeMap<Value> NodePotential; |
674 | 677 |
typedef std::vector<Node> BlossomNodeList; |
675 | 678 |
|
676 | 679 |
struct BlossomVariable { |
677 | 680 |
int begin, end; |
678 | 681 |
Value value; |
679 | 682 |
|
680 | 683 |
BlossomVariable(int _begin, int _end, Value _value) |
681 | 684 |
: begin(_begin), end(_end), value(_value) {} |
682 | 685 |
|
683 | 686 |
}; |
684 | 687 |
|
685 | 688 |
typedef std::vector<BlossomVariable> BlossomPotential; |
686 | 689 |
|
687 | 690 |
const Graph& _graph; |
688 | 691 |
const WeightMap& _weight; |
689 | 692 |
|
690 | 693 |
MatchingMap* _matching; |
691 | 694 |
|
692 | 695 |
NodePotential* _node_potential; |
693 | 696 |
|
694 | 697 |
BlossomPotential _blossom_potential; |
695 | 698 |
BlossomNodeList _blossom_node_list; |
696 | 699 |
|
697 | 700 |
int _node_num; |
698 | 701 |
int _blossom_num; |
699 | 702 |
|
700 | 703 |
typedef RangeMap<int> IntIntMap; |
701 | 704 |
|
702 | 705 |
enum Status { |
703 | 706 |
EVEN = -1, MATCHED = 0, ODD = 1, UNMATCHED = -2 |
704 | 707 |
}; |
... | ... |
@@ -1912,136 +1915,136 @@ |
1912 | 1915 |
: _algorithm(&algorithm) |
1913 | 1916 |
{ |
1914 | 1917 |
_index = _algorithm->_blossom_potential[variable].begin; |
1915 | 1918 |
_last = _algorithm->_blossom_potential[variable].end; |
1916 | 1919 |
} |
1917 | 1920 |
|
1918 | 1921 |
/// \brief Conversion to node. |
1919 | 1922 |
/// |
1920 | 1923 |
/// Conversion to node. |
1921 | 1924 |
operator Node() const { |
1922 | 1925 |
return _algorithm->_blossom_node_list[_index]; |
1923 | 1926 |
} |
1924 | 1927 |
|
1925 | 1928 |
/// \brief Increment operator. |
1926 | 1929 |
/// |
1927 | 1930 |
/// Increment operator. |
1928 | 1931 |
BlossomIt& operator++() { |
1929 | 1932 |
++_index; |
1930 | 1933 |
return *this; |
1931 | 1934 |
} |
1932 | 1935 |
|
1933 | 1936 |
/// \brief Validity checking |
1934 | 1937 |
/// |
1935 | 1938 |
/// Checks whether the iterator is invalid. |
1936 | 1939 |
bool operator==(Invalid) const { return _index == _last; } |
1937 | 1940 |
|
1938 | 1941 |
/// \brief Validity checking |
1939 | 1942 |
/// |
1940 | 1943 |
/// Checks whether the iterator is valid. |
1941 | 1944 |
bool operator!=(Invalid) const { return _index != _last; } |
1942 | 1945 |
|
1943 | 1946 |
private: |
1944 | 1947 |
const MaxWeightedMatching* _algorithm; |
1945 | 1948 |
int _last; |
1946 | 1949 |
int _index; |
1947 | 1950 |
}; |
1948 | 1951 |
|
1949 | 1952 |
/// @} |
1950 | 1953 |
|
1951 | 1954 |
}; |
1952 | 1955 |
|
1953 | 1956 |
/// \ingroup matching |
1954 | 1957 |
/// |
1955 | 1958 |
/// \brief Weighted perfect matching in general graphs |
1956 | 1959 |
/// |
1957 | 1960 |
/// This class provides an efficient implementation of Edmond's |
1958 | 1961 |
/// maximum weighted perfect matching algorithm. The implementation |
1959 | 1962 |
/// is based on extensive use of priority queues and provides |
1960 |
/// \f$O(nm\log |
|
1963 |
/// \f$O(nm\log n)\f$ time complexity. |
|
1961 | 1964 |
/// |
1962 | 1965 |
/// The maximum weighted matching problem is to find undirected |
1963 | 1966 |
/// edges in the graph with maximum overall weight and no two of |
1964 | 1967 |
/// them shares their ends and covers all nodes. The problem can be |
1965 | 1968 |
/// formulated with the following linear program. |
1966 | 1969 |
/// \f[ \sum_{e \in \delta(u)}x_e = 1 \quad \forall u\in V\f] |
1967 | 1970 |
/** \f[ \sum_{e \in \gamma(B)}x_e \le \frac{\vert B \vert - 1}{2} |
1968 | 1971 |
\quad \forall B\in\mathcal{O}\f] */ |
1969 | 1972 |
/// \f[x_e \ge 0\quad \forall e\in E\f] |
1970 | 1973 |
/// \f[\max \sum_{e\in E}x_ew_e\f] |
1971 | 1974 |
/// where \f$\delta(X)\f$ is the set of edges incident to a node in |
1972 | 1975 |
/// \f$X\f$, \f$\gamma(X)\f$ is the set of edges with both ends in |
1973 | 1976 |
/// \f$X\f$ and \f$\mathcal{O}\f$ is the set of odd cardinality |
1974 | 1977 |
/// subsets of the nodes. |
1975 | 1978 |
/// |
1976 | 1979 |
/// The algorithm calculates an optimal matching and a proof of the |
1977 | 1980 |
/// optimality. The solution of the dual problem can be used to check |
1978 | 1981 |
/// the result of the algorithm. The dual linear problem is the |
1979 | 1982 |
/** \f[ y_u + y_v + \sum_{B \in \mathcal{O}, uv \in \gamma(B)}z_B \ge |
1980 | 1983 |
w_{uv} \quad \forall uv\in E\f] */ |
1981 | 1984 |
/// \f[z_B \ge 0 \quad \forall B \in \mathcal{O}\f] |
1982 | 1985 |
/** \f[\min \sum_{u \in V}y_u + \sum_{B \in \mathcal{O}} |
1983 | 1986 |
\frac{\vert B \vert - 1}{2}z_B\f] */ |
1984 | 1987 |
/// |
1985 | 1988 |
/// The algorithm can be executed with \c run() or the \c init() and |
1986 | 1989 |
/// then the \c start() member functions. After it the matching can |
1987 | 1990 |
/// be asked with \c matching() or mate() functions. The dual |
1988 | 1991 |
/// solution can be get with \c nodeValue(), \c blossomNum() and \c |
1989 | 1992 |
/// blossomValue() members and \ref MaxWeightedMatching::BlossomIt |
1990 | 1993 |
/// "BlossomIt" nested class which is able to iterate on the nodes |
1991 | 1994 |
/// of a blossom. If the value type is integral then the dual |
1992 | 1995 |
/// solution is multiplied by \ref MaxWeightedMatching::dualScale "4". |
1993 |
template <typename _Graph, |
|
1994 |
typename _WeightMap = typename _Graph::template EdgeMap<int> > |
|
1996 |
template <typename GR, |
|
1997 |
typename WM = typename GR::template EdgeMap<int> > |
|
1995 | 1998 |
class MaxWeightedPerfectMatching { |
1996 | 1999 |
public: |
1997 | 2000 |
|
1998 |
typedef _Graph Graph; |
|
1999 |
typedef _WeightMap WeightMap; |
|
2001 |
typedef GR Graph; |
|
2002 |
typedef WM WeightMap; |
|
2000 | 2003 |
typedef typename WeightMap::Value Value; |
2001 | 2004 |
|
2002 | 2005 |
/// \brief Scaling factor for dual solution |
2003 | 2006 |
/// |
2004 | 2007 |
/// Scaling factor for dual solution, it is equal to 4 or 1 |
2005 | 2008 |
/// according to the value type. |
2006 | 2009 |
static const int dualScale = |
2007 | 2010 |
std::numeric_limits<Value>::is_integer ? 4 : 1; |
2008 | 2011 |
|
2009 | 2012 |
typedef typename Graph::template NodeMap<typename Graph::Arc> |
2010 | 2013 |
MatchingMap; |
2011 | 2014 |
|
2012 | 2015 |
private: |
2013 | 2016 |
|
2014 | 2017 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
2015 | 2018 |
|
2016 | 2019 |
typedef typename Graph::template NodeMap<Value> NodePotential; |
2017 | 2020 |
typedef std::vector<Node> BlossomNodeList; |
2018 | 2021 |
|
2019 | 2022 |
struct BlossomVariable { |
2020 | 2023 |
int begin, end; |
2021 | 2024 |
Value value; |
2022 | 2025 |
|
2023 | 2026 |
BlossomVariable(int _begin, int _end, Value _value) |
2024 | 2027 |
: begin(_begin), end(_end), value(_value) {} |
2025 | 2028 |
|
2026 | 2029 |
}; |
2027 | 2030 |
|
2028 | 2031 |
typedef std::vector<BlossomVariable> BlossomPotential; |
2029 | 2032 |
|
2030 | 2033 |
const Graph& _graph; |
2031 | 2034 |
const WeightMap& _weight; |
2032 | 2035 |
|
2033 | 2036 |
MatchingMap* _matching; |
2034 | 2037 |
|
2035 | 2038 |
NodePotential* _node_potential; |
2036 | 2039 |
|
2037 | 2040 |
BlossomPotential _blossom_potential; |
2038 | 2041 |
BlossomNodeList _blossom_node_list; |
2039 | 2042 |
|
2040 | 2043 |
int _node_num; |
2041 | 2044 |
int _blossom_num; |
2042 | 2045 |
|
2043 | 2046 |
typedef RangeMap<int> IntIntMap; |
2044 | 2047 |
|
2045 | 2048 |
enum Status { |
2046 | 2049 |
EVEN = -1, MATCHED = 0, ODD = 1 |
2047 | 2050 |
}; |
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 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_MIN_COST_ARBORESCENCE_H |
20 | 20 |
#define LEMON_MIN_COST_ARBORESCENCE_H |
21 | 21 |
|
22 | 22 |
///\ingroup spantree |
23 | 23 |
///\file |
24 | 24 |
///\brief Minimum Cost Arborescence algorithm. |
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
|
28 | 28 |
#include <lemon/list_graph.h> |
29 | 29 |
#include <lemon/bin_heap.h> |
30 | 30 |
#include <lemon/assert.h> |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
|
35 | 35 |
/// \brief Default traits class for MinCostArborescence class. |
36 | 36 |
/// |
37 | 37 |
/// Default traits class for MinCostArborescence class. |
38 |
/// \param _Digraph Digraph type. |
|
39 |
/// \param _CostMap Type of cost map. |
|
40 |
|
|
38 |
/// \param GR Digraph type. |
|
39 |
/// \param CM Type of cost map. |
|
40 |
template <class GR, class CM> |
|
41 | 41 |
struct MinCostArborescenceDefaultTraits{ |
42 | 42 |
|
43 | 43 |
/// \brief The digraph type the algorithm runs on. |
44 |
typedef |
|
44 |
typedef GR Digraph; |
|
45 | 45 |
|
46 | 46 |
/// \brief The type of the map that stores the arc costs. |
47 | 47 |
/// |
48 | 48 |
/// The type of the map that stores the arc costs. |
49 | 49 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
50 |
typedef |
|
50 |
typedef CM CostMap; |
|
51 | 51 |
|
52 | 52 |
/// \brief The value type of the costs. |
53 | 53 |
/// |
54 | 54 |
/// The value type of the costs. |
55 | 55 |
typedef typename CostMap::Value Value; |
56 | 56 |
|
57 | 57 |
/// \brief The type of the map that stores which arcs are in the |
58 | 58 |
/// arborescence. |
59 | 59 |
/// |
60 | 60 |
/// The type of the map that stores which arcs are in the |
61 | 61 |
/// arborescence. It must meet the \ref concepts::WriteMap |
62 | 62 |
/// "WriteMap" concept. Initially it will be set to false on each |
63 | 63 |
/// arc. After it will set all arborescence arcs once. |
64 | 64 |
typedef typename Digraph::template ArcMap<bool> ArborescenceMap; |
65 | 65 |
|
66 |
/// \brief Instantiates a ArborescenceMap. |
|
66 |
/// \brief Instantiates a \c ArborescenceMap. |
|
67 | 67 |
/// |
68 |
/// This function instantiates a \ |
|
68 |
/// This function instantiates a \c ArborescenceMap. |
|
69 | 69 |
/// \param digraph is the graph, to which we would like to |
70 |
/// calculate the ArborescenceMap. |
|
70 |
/// calculate the \c ArborescenceMap. |
|
71 | 71 |
static ArborescenceMap *createArborescenceMap(const Digraph &digraph){ |
72 | 72 |
return new ArborescenceMap(digraph); |
73 | 73 |
} |
74 | 74 |
|
75 |
/// \brief The type of the PredMap |
|
75 |
/// \brief The type of the \c PredMap |
|
76 | 76 |
/// |
77 |
/// The type of the PredMap. It is a node map with an arc value type. |
|
77 |
/// The type of the \c PredMap. It is a node map with an arc value type. |
|
78 | 78 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
79 | 79 |
|
80 |
/// \brief Instantiates a PredMap. |
|
80 |
/// \brief Instantiates a \c PredMap. |
|
81 | 81 |
/// |
82 |
/// This function instantiates a \ref PredMap. |
|
83 |
/// \param _digraph is the digraph, to which we would like to define the |
|
84 |
/// PredMap. |
|
82 |
/// This function instantiates a \c PredMap. |
|
83 |
/// \param digraph The digraph to which we would like to define the |
|
84 |
/// \c PredMap. |
|
85 | 85 |
static PredMap *createPredMap(const Digraph &digraph){ |
86 | 86 |
return new PredMap(digraph); |
87 | 87 |
} |
88 | 88 |
|
89 | 89 |
}; |
90 | 90 |
|
91 | 91 |
/// \ingroup spantree |
92 | 92 |
/// |
93 | 93 |
/// \brief %MinCostArborescence algorithm class. |
94 | 94 |
/// |
95 | 95 |
/// This class provides an efficient implementation of |
96 | 96 |
/// %MinCostArborescence algorithm. The arborescence is a tree |
97 | 97 |
/// which is directed from a given source node of the digraph. One or |
98 | 98 |
/// more sources should be given for the algorithm and it will calculate |
99 | 99 |
/// the minimum cost subgraph which are union of arborescences with the |
100 | 100 |
/// given sources and spans all the nodes which are reachable from the |
101 |
/// sources. The time complexity of the algorithm is |
|
101 |
/// sources. The time complexity of the algorithm is O(n<sup>2</sup>+e). |
|
102 | 102 |
/// |
103 | 103 |
/// The algorithm provides also an optimal dual solution, therefore |
104 | 104 |
/// the optimality of the solution can be checked. |
105 | 105 |
/// |
106 |
/// \param |
|
106 |
/// \param GR The digraph type the algorithm runs on. The default value |
|
107 | 107 |
/// is \ref ListDigraph. |
108 |
/// \param |
|
108 |
/// \param CM This read-only ArcMap determines the costs of the |
|
109 | 109 |
/// arcs. It is read once for each arc, so the map may involve in |
110 | 110 |
/// relatively time consuming process to compute the arc cost if |
111 | 111 |
/// it is necessary. The default map type is \ref |
112 | 112 |
/// concepts::Digraph::ArcMap "Digraph::ArcMap<int>". |
113 |
/// \param |
|
113 |
/// \param TR Traits class to set various data types used |
|
114 | 114 |
/// by the algorithm. The default traits class is |
115 | 115 |
/// \ref MinCostArborescenceDefaultTraits |
116 |
/// "MinCostArborescenceDefaultTraits< |
|
116 |
/// "MinCostArborescenceDefaultTraits<GR, CM>". See \ref |
|
117 | 117 |
/// MinCostArborescenceDefaultTraits for the documentation of a |
118 | 118 |
/// MinCostArborescence traits class. |
119 |
/// |
|
120 |
/// \author Balazs Dezso |
|
121 | 119 |
#ifndef DOXYGEN |
122 |
template <typename _Digraph = ListDigraph, |
|
123 |
typename _CostMap = typename _Digraph::template ArcMap<int>, |
|
124 |
typename _Traits = |
|
125 |
MinCostArborescenceDefaultTraits<_Digraph, _CostMap> > |
|
120 |
template <typename GR = ListDigraph, |
|
121 |
typename CM = typename GR::template ArcMap<int>, |
|
122 |
typename TR = |
|
123 |
MinCostArborescenceDefaultTraits<GR, CM> > |
|
126 | 124 |
#else |
127 |
template <typename |
|
125 |
template <typename GR, typename CM, typedef TR> |
|
128 | 126 |
#endif |
129 | 127 |
class MinCostArborescence { |
130 | 128 |
public: |
131 | 129 |
|
132 | 130 |
/// The traits. |
133 |
typedef |
|
131 |
typedef TR Traits; |
|
134 | 132 |
/// The type of the underlying digraph. |
135 | 133 |
typedef typename Traits::Digraph Digraph; |
136 | 134 |
/// The type of the map that stores the arc costs. |
137 | 135 |
typedef typename Traits::CostMap CostMap; |
138 | 136 |
///The type of the costs of the arcs. |
139 | 137 |
typedef typename Traits::Value Value; |
140 | 138 |
///The type of the predecessor map. |
141 | 139 |
typedef typename Traits::PredMap PredMap; |
142 | 140 |
///The type of the map that stores which arcs are in the arborescence. |
143 | 141 |
typedef typename Traits::ArborescenceMap ArborescenceMap; |
144 | 142 |
|
145 | 143 |
typedef MinCostArborescence Create; |
146 | 144 |
|
147 | 145 |
private: |
148 | 146 |
|
149 | 147 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
150 | 148 |
|
151 | 149 |
struct CostArc { |
152 | 150 |
|
153 | 151 |
Arc arc; |
154 | 152 |
Value value; |
155 | 153 |
|
156 | 154 |
CostArc() {} |
157 | 155 |
CostArc(Arc _arc, Value _value) : arc(_arc), value(_value) {} |
158 | 156 |
|
159 | 157 |
}; |
160 | 158 |
|
161 | 159 |
const Digraph *_digraph; |
162 | 160 |
const CostMap *_cost; |
163 | 161 |
|
164 | 162 |
PredMap *_pred; |
165 | 163 |
bool local_pred; |
166 | 164 |
|
167 | 165 |
ArborescenceMap *_arborescence; |
168 | 166 |
bool local_arborescence; |
169 | 167 |
|
170 | 168 |
typedef typename Digraph::template ArcMap<int> ArcOrder; |
171 | 169 |
ArcOrder *_arc_order; |
172 | 170 |
|
173 | 171 |
typedef typename Digraph::template NodeMap<int> NodeOrder; |
174 | 172 |
NodeOrder *_node_order; |
175 | 173 |
|
176 | 174 |
typedef typename Digraph::template NodeMap<CostArc> CostArcMap; |
177 | 175 |
CostArcMap *_cost_arcs; |
178 | 176 |
|
179 | 177 |
struct StackLevel { |
180 | 178 |
|
181 | 179 |
std::vector<CostArc> arcs; |
... | ... |
@@ -395,126 +393,126 @@ |
395 | 393 |
/// \name Named template parameters |
396 | 394 |
|
397 | 395 |
/// @{ |
398 | 396 |
|
399 | 397 |
template <class T> |
400 | 398 |
struct DefArborescenceMapTraits : public Traits { |
401 | 399 |
typedef T ArborescenceMap; |
402 | 400 |
static ArborescenceMap *createArborescenceMap(const Digraph &) |
403 | 401 |
{ |
404 | 402 |
LEMON_ASSERT(false, "ArborescenceMap is not initialized"); |
405 | 403 |
return 0; // ignore warnings |
406 | 404 |
} |
407 | 405 |
}; |
408 | 406 |
|
409 | 407 |
/// \brief \ref named-templ-param "Named parameter" for |
410 | 408 |
/// setting ArborescenceMap type |
411 | 409 |
/// |
412 | 410 |
/// \ref named-templ-param "Named parameter" for setting |
413 | 411 |
/// ArborescenceMap type |
414 | 412 |
template <class T> |
415 | 413 |
struct DefArborescenceMap |
416 | 414 |
: public MinCostArborescence<Digraph, CostMap, |
417 | 415 |
DefArborescenceMapTraits<T> > { |
418 | 416 |
}; |
419 | 417 |
|
420 | 418 |
template <class T> |
421 | 419 |
struct DefPredMapTraits : public Traits { |
422 | 420 |
typedef T PredMap; |
423 | 421 |
static PredMap *createPredMap(const Digraph &) |
424 | 422 |
{ |
425 | 423 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
426 | 424 |
} |
427 | 425 |
}; |
428 | 426 |
|
429 | 427 |
/// \brief \ref named-templ-param "Named parameter" for |
430 | 428 |
/// setting PredMap type |
431 | 429 |
/// |
432 | 430 |
/// \ref named-templ-param "Named parameter" for setting |
433 | 431 |
/// PredMap type |
434 | 432 |
template <class T> |
435 | 433 |
struct DefPredMap |
436 | 434 |
: public MinCostArborescence<Digraph, CostMap, DefPredMapTraits<T> > { |
437 | 435 |
}; |
438 | 436 |
|
439 | 437 |
/// @} |
440 | 438 |
|
441 | 439 |
/// \brief Constructor. |
442 | 440 |
/// |
443 |
/// \param _digraph The digraph the algorithm will run on. |
|
444 |
/// \param _cost The cost map used by the algorithm. |
|
441 |
/// \param digraph The digraph the algorithm will run on. |
|
442 |
/// \param cost The cost map used by the algorithm. |
|
445 | 443 |
MinCostArborescence(const Digraph& digraph, const CostMap& cost) |
446 | 444 |
: _digraph(&digraph), _cost(&cost), _pred(0), local_pred(false), |
447 | 445 |
_arborescence(0), local_arborescence(false), |
448 | 446 |
_arc_order(0), _node_order(0), _cost_arcs(0), |
449 | 447 |
_heap_cross_ref(0), _heap(0) {} |
450 | 448 |
|
451 | 449 |
/// \brief Destructor. |
452 | 450 |
~MinCostArborescence() { |
453 | 451 |
destroyStructures(); |
454 | 452 |
} |
455 | 453 |
|
456 | 454 |
/// \brief Sets the arborescence map. |
457 | 455 |
/// |
458 | 456 |
/// Sets the arborescence map. |
459 |
/// \return |
|
457 |
/// \return <tt>(*this)</tt> |
|
460 | 458 |
MinCostArborescence& arborescenceMap(ArborescenceMap& m) { |
461 | 459 |
if (local_arborescence) { |
462 | 460 |
delete _arborescence; |
463 | 461 |
} |
464 | 462 |
local_arborescence = false; |
465 | 463 |
_arborescence = &m; |
466 | 464 |
return *this; |
467 | 465 |
} |
468 | 466 |
|
469 | 467 |
/// \brief Sets the arborescence map. |
470 | 468 |
/// |
471 | 469 |
/// Sets the arborescence map. |
472 |
/// \return |
|
470 |
/// \return <tt>(*this)</tt> |
|
473 | 471 |
MinCostArborescence& predMap(PredMap& m) { |
474 | 472 |
if (local_pred) { |
475 | 473 |
delete _pred; |
476 | 474 |
} |
477 | 475 |
local_pred = false; |
478 | 476 |
_pred = &m; |
479 | 477 |
return *this; |
480 | 478 |
} |
481 | 479 |
|
482 | 480 |
/// \name Query Functions |
483 | 481 |
/// The result of the %MinCostArborescence algorithm can be obtained |
484 | 482 |
/// using these functions.\n |
485 | 483 |
/// Before the use of these functions, |
486 | 484 |
/// either run() or start() must be called. |
487 | 485 |
|
488 | 486 |
/// @{ |
489 | 487 |
|
490 | 488 |
/// \brief Returns a reference to the arborescence map. |
491 | 489 |
/// |
492 | 490 |
/// Returns a reference to the arborescence map. |
493 | 491 |
const ArborescenceMap& arborescenceMap() const { |
494 | 492 |
return *_arborescence; |
495 | 493 |
} |
496 | 494 |
|
497 | 495 |
/// \brief Returns true if the arc is in the arborescence. |
498 | 496 |
/// |
499 | 497 |
/// Returns true if the arc is in the arborescence. |
500 | 498 |
/// \param arc The arc of the digraph. |
501 | 499 |
/// \pre \ref run() must be called before using this function. |
502 | 500 |
bool arborescence(Arc arc) const { |
503 | 501 |
return (*_pred)[_digraph->target(arc)] == arc; |
504 | 502 |
} |
505 | 503 |
|
506 | 504 |
/// \brief Returns a reference to the pred map. |
507 | 505 |
/// |
508 | 506 |
/// Returns a reference to the pred map. |
509 | 507 |
const PredMap& predMap() const { |
510 | 508 |
return *_pred; |
511 | 509 |
} |
512 | 510 |
|
513 | 511 |
/// \brief Returns the predecessor arc of the given node. |
514 | 512 |
/// |
515 | 513 |
/// Returns the predecessor arc of the given node. |
516 | 514 |
Arc pred(Node node) const { |
517 | 515 |
return (*_pred)[node]; |
518 | 516 |
} |
519 | 517 |
|
520 | 518 |
/// \brief Returns the cost of the arborescence. |
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 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup paths |
20 | 20 |
///\file |
21 | 21 |
///\brief Classes for representing paths in digraphs. |
22 | 22 |
/// |
23 | 23 |
|
24 | 24 |
#ifndef LEMON_PATH_H |
25 | 25 |
#define LEMON_PATH_H |
26 | 26 |
|
27 | 27 |
#include <vector> |
28 | 28 |
#include <algorithm> |
29 | 29 |
|
30 | 30 |
#include <lemon/error.h> |
31 | 31 |
#include <lemon/core.h> |
32 | 32 |
#include <lemon/concepts/path.h> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \addtogroup paths |
37 | 37 |
/// @{ |
38 | 38 |
|
39 | 39 |
|
40 | 40 |
/// \brief A structure for representing directed paths in a digraph. |
41 | 41 |
/// |
42 | 42 |
/// A structure for representing directed path in a digraph. |
43 |
/// \tparam |
|
43 |
/// \tparam GR The digraph type in which the path is. |
|
44 | 44 |
/// |
45 | 45 |
/// In a sense, the path can be treated as a list of arcs. The |
46 | 46 |
/// lemon path type stores just this list. As a consequence, it |
47 | 47 |
/// cannot enumerate the nodes of the path and the source node of |
48 | 48 |
/// a zero length path is undefined. |
49 | 49 |
/// |
50 | 50 |
/// This implementation is a back and front insertable and erasable |
51 | 51 |
/// path type. It can be indexed in O(1) time. The front and back |
52 | 52 |
/// insertion and erase is done in O(1) (amortized) time. The |
53 | 53 |
/// implementation uses two vectors for storing the front and back |
54 | 54 |
/// insertions. |
55 |
template <typename |
|
55 |
template <typename GR> |
|
56 | 56 |
class Path { |
57 | 57 |
public: |
58 | 58 |
|
59 |
typedef |
|
59 |
typedef GR Digraph; |
|
60 | 60 |
typedef typename Digraph::Arc Arc; |
61 | 61 |
|
62 | 62 |
/// \brief Default constructor |
63 | 63 |
/// |
64 | 64 |
/// Default constructor |
65 | 65 |
Path() {} |
66 | 66 |
|
67 | 67 |
/// \brief Template copy constructor |
68 | 68 |
/// |
69 | 69 |
/// This constuctor initializes the path from any other path type. |
70 | 70 |
/// It simply makes a copy of the given path. |
71 | 71 |
template <typename CPath> |
72 | 72 |
Path(const CPath& cpath) { |
73 | 73 |
copyPath(*this, cpath); |
74 | 74 |
} |
75 | 75 |
|
76 | 76 |
/// \brief Template copy assignment |
77 | 77 |
/// |
78 | 78 |
/// This operator makes a copy of a path of any other type. |
79 | 79 |
template <typename CPath> |
80 | 80 |
Path& operator=(const CPath& cpath) { |
81 | 81 |
copyPath(*this, cpath); |
82 | 82 |
return *this; |
83 | 83 |
} |
84 | 84 |
|
85 | 85 |
/// \brief LEMON style iterator for path arcs |
86 | 86 |
/// |
87 | 87 |
/// This class is used to iterate on the arcs of the paths. |
88 | 88 |
class ArcIt { |
89 | 89 |
friend class Path; |
90 | 90 |
public: |
91 | 91 |
/// \brief Default constructor |
92 | 92 |
ArcIt() {} |
93 | 93 |
/// \brief Invalid constructor |
94 | 94 |
ArcIt(Invalid) : path(0), idx(-1) {} |
95 | 95 |
/// \brief Initializate the iterator to the first arc of path |
96 | 96 |
ArcIt(const Path &_path) |
97 | 97 |
: path(&_path), idx(_path.empty() ? -1 : 0) {} |
98 | 98 |
|
99 | 99 |
private: |
100 | 100 |
|
101 | 101 |
ArcIt(const Path &_path, int _idx) |
102 | 102 |
: path(&_path), idx(_idx) {} |
103 | 103 |
|
104 | 104 |
public: |
105 | 105 |
|
106 | 106 |
/// \brief Conversion to Arc |
107 | 107 |
operator const Arc&() const { |
108 | 108 |
return path->nth(idx); |
109 | 109 |
} |
110 | 110 |
|
111 | 111 |
/// \brief Next arc |
112 | 112 |
ArcIt& operator++() { |
113 | 113 |
++idx; |
114 | 114 |
if (idx >= path->length()) idx = -1; |
115 | 115 |
return *this; |
116 | 116 |
} |
117 | 117 |
|
118 | 118 |
/// \brief Comparison operator |
119 | 119 |
bool operator==(const ArcIt& e) const { return idx==e.idx; } |
120 | 120 |
/// \brief Comparison operator |
121 | 121 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; } |
122 | 122 |
/// \brief Comparison operator |
123 | 123 |
bool operator<(const ArcIt& e) const { return idx<e.idx; } |
124 | 124 |
|
125 | 125 |
private: |
126 | 126 |
const Path *path; |
127 | 127 |
int idx; |
128 | 128 |
}; |
129 | 129 |
|
130 | 130 |
/// \brief Length of the path. |
131 | 131 |
int length() const { return head.size() + tail.size(); } |
132 | 132 |
/// \brief Return whether the path is empty. |
133 | 133 |
bool empty() const { return head.empty() && tail.empty(); } |
134 | 134 |
|
135 | 135 |
/// \brief Reset the path to an empty one. |
136 | 136 |
void clear() { head.clear(); tail.clear(); } |
137 | 137 |
|
138 | 138 |
/// \brief The nth arc. |
139 | 139 |
/// |
140 |
/// \pre n is in the [0..length() - 1] range |
|
140 |
/// \pre \c n is in the <tt>[0..length() - 1]</tt> range. |
|
141 | 141 |
const Arc& nth(int n) const { |
142 | 142 |
return n < int(head.size()) ? *(head.rbegin() + n) : |
143 | 143 |
*(tail.begin() + (n - head.size())); |
144 | 144 |
} |
145 | 145 |
|
146 | 146 |
/// \brief Initialize arc iterator to point to the nth arc |
147 | 147 |
/// |
148 |
/// \pre n is in the [0..length() - 1] range |
|
148 |
/// \pre \c n is in the <tt>[0..length() - 1]</tt> range. |
|
149 | 149 |
ArcIt nthIt(int n) const { |
150 | 150 |
return ArcIt(*this, n); |
151 | 151 |
} |
152 | 152 |
|
153 | 153 |
/// \brief The first arc of the path |
154 | 154 |
const Arc& front() const { |
155 | 155 |
return head.empty() ? tail.front() : head.back(); |
156 | 156 |
} |
157 | 157 |
|
158 | 158 |
/// \brief Add a new arc before the current path |
159 | 159 |
void addFront(const Arc& arc) { |
160 | 160 |
head.push_back(arc); |
161 | 161 |
} |
162 | 162 |
|
163 | 163 |
/// \brief Erase the first arc of the path |
164 | 164 |
void eraseFront() { |
165 | 165 |
if (!head.empty()) { |
166 | 166 |
head.pop_back(); |
167 | 167 |
} else { |
168 | 168 |
head.clear(); |
169 | 169 |
int halfsize = tail.size() / 2; |
170 | 170 |
head.resize(halfsize); |
171 | 171 |
std::copy(tail.begin() + 1, tail.begin() + halfsize + 1, |
172 | 172 |
head.rbegin()); |
173 | 173 |
std::copy(tail.begin() + halfsize + 1, tail.end(), tail.begin()); |
174 | 174 |
tail.resize(tail.size() - halfsize - 1); |
175 | 175 |
} |
176 | 176 |
} |
177 | 177 |
|
178 | 178 |
/// \brief The last arc of the path |
179 | 179 |
const Arc& back() const { |
180 | 180 |
return tail.empty() ? head.front() : tail.back(); |
181 | 181 |
} |
182 | 182 |
|
183 | 183 |
/// \brief Add a new arc behind the current path |
184 | 184 |
void addBack(const Arc& arc) { |
185 | 185 |
tail.push_back(arc); |
186 | 186 |
} |
187 | 187 |
|
188 | 188 |
/// \brief Erase the last arc of the path |
189 | 189 |
void eraseBack() { |
190 | 190 |
if (!tail.empty()) { |
191 | 191 |
tail.pop_back(); |
192 | 192 |
} else { |
193 | 193 |
int halfsize = head.size() / 2; |
194 | 194 |
tail.resize(halfsize); |
195 | 195 |
std::copy(head.begin() + 1, head.begin() + halfsize + 1, |
196 | 196 |
tail.rbegin()); |
197 | 197 |
std::copy(head.begin() + halfsize + 1, head.end(), head.begin()); |
198 | 198 |
head.resize(head.size() - halfsize - 1); |
199 | 199 |
} |
200 | 200 |
} |
201 | 201 |
|
202 | 202 |
typedef True BuildTag; |
203 | 203 |
|
204 | 204 |
template <typename CPath> |
205 | 205 |
void build(const CPath& path) { |
206 | 206 |
int len = path.length(); |
207 | 207 |
tail.reserve(len); |
208 | 208 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) { |
209 | 209 |
tail.push_back(it); |
210 | 210 |
} |
211 | 211 |
} |
212 | 212 |
|
213 | 213 |
template <typename CPath> |
214 | 214 |
void buildRev(const CPath& path) { |
215 | 215 |
int len = path.length(); |
216 | 216 |
head.reserve(len); |
217 | 217 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) { |
218 | 218 |
head.push_back(it); |
219 | 219 |
} |
220 | 220 |
} |
221 | 221 |
|
222 | 222 |
protected: |
223 | 223 |
typedef std::vector<Arc> Container; |
224 | 224 |
Container head, tail; |
225 | 225 |
|
226 | 226 |
}; |
227 | 227 |
|
228 | 228 |
/// \brief A structure for representing directed paths in a digraph. |
229 | 229 |
/// |
230 | 230 |
/// A structure for representing directed path in a digraph. |
231 |
/// \tparam |
|
231 |
/// \tparam GR The digraph type in which the path is. |
|
232 | 232 |
/// |
233 | 233 |
/// In a sense, the path can be treated as a list of arcs. The |
234 | 234 |
/// lemon path type stores just this list. As a consequence it |
235 | 235 |
/// cannot enumerate the nodes in the path and the zero length paths |
236 | 236 |
/// cannot store the source. |
237 | 237 |
/// |
238 | 238 |
/// This implementation is a just back insertable and erasable path |
239 | 239 |
/// type. It can be indexed in O(1) time. The back insertion and |
240 | 240 |
/// erasure is amortized O(1) time. This implementation is faster |
241 | 241 |
/// then the \c Path type because it use just one vector for the |
242 | 242 |
/// arcs. |
243 |
template <typename |
|
243 |
template <typename GR> |
|
244 | 244 |
class SimplePath { |
245 | 245 |
public: |
246 | 246 |
|
247 |
typedef |
|
247 |
typedef GR Digraph; |
|
248 | 248 |
typedef typename Digraph::Arc Arc; |
249 | 249 |
|
250 | 250 |
/// \brief Default constructor |
251 | 251 |
/// |
252 | 252 |
/// Default constructor |
253 | 253 |
SimplePath() {} |
254 | 254 |
|
255 | 255 |
/// \brief Template copy constructor |
256 | 256 |
/// |
257 | 257 |
/// This path can be initialized with any other path type. It just |
258 | 258 |
/// makes a copy of the given path. |
259 | 259 |
template <typename CPath> |
260 | 260 |
SimplePath(const CPath& cpath) { |
261 | 261 |
copyPath(*this, cpath); |
262 | 262 |
} |
263 | 263 |
|
264 | 264 |
/// \brief Template copy assignment |
265 | 265 |
/// |
266 | 266 |
/// This path can be initialized with any other path type. It just |
267 | 267 |
/// makes a copy of the given path. |
268 | 268 |
template <typename CPath> |
269 | 269 |
SimplePath& operator=(const CPath& cpath) { |
270 | 270 |
copyPath(*this, cpath); |
271 | 271 |
return *this; |
272 | 272 |
} |
273 | 273 |
|
274 | 274 |
/// \brief Iterator class to iterate on the arcs of the paths |
275 | 275 |
/// |
276 | 276 |
/// This class is used to iterate on the arcs of the paths |
277 | 277 |
/// |
278 | 278 |
/// Of course it converts to Digraph::Arc |
279 | 279 |
class ArcIt { |
280 | 280 |
friend class SimplePath; |
281 | 281 |
public: |
282 | 282 |
/// Default constructor |
283 | 283 |
ArcIt() {} |
284 | 284 |
/// Invalid constructor |
285 | 285 |
ArcIt(Invalid) : path(0), idx(-1) {} |
286 | 286 |
/// \brief Initializate the constructor to the first arc of path |
287 | 287 |
ArcIt(const SimplePath &_path) |
288 | 288 |
: path(&_path), idx(_path.empty() ? -1 : 0) {} |
289 | 289 |
|
290 | 290 |
private: |
291 | 291 |
|
292 | 292 |
/// Constructor with starting point |
293 | 293 |
ArcIt(const SimplePath &_path, int _idx) |
294 | 294 |
: idx(_idx), path(&_path) {} |
295 | 295 |
|
296 | 296 |
public: |
297 | 297 |
|
298 | 298 |
///Conversion to Digraph::Arc |
299 | 299 |
operator const Arc&() const { |
300 | 300 |
return path->nth(idx); |
301 | 301 |
} |
302 | 302 |
|
303 | 303 |
/// Next arc |
304 | 304 |
ArcIt& operator++() { |
305 | 305 |
++idx; |
306 | 306 |
if (idx >= path->length()) idx = -1; |
307 | 307 |
return *this; |
308 | 308 |
} |
309 | 309 |
|
310 | 310 |
/// Comparison operator |
311 | 311 |
bool operator==(const ArcIt& e) const { return idx==e.idx; } |
312 | 312 |
/// Comparison operator |
313 | 313 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; } |
314 | 314 |
/// Comparison operator |
315 | 315 |
bool operator<(const ArcIt& e) const { return idx<e.idx; } |
316 | 316 |
|
317 | 317 |
private: |
318 | 318 |
const SimplePath *path; |
319 | 319 |
int idx; |
320 | 320 |
}; |
321 | 321 |
|
322 | 322 |
/// \brief Length of the path. |
323 | 323 |
int length() const { return data.size(); } |
324 | 324 |
/// \brief Return true if the path is empty. |
325 | 325 |
bool empty() const { return data.empty(); } |
326 | 326 |
|
327 | 327 |
/// \brief Reset the path to an empty one. |
328 | 328 |
void clear() { data.clear(); } |
329 | 329 |
|
330 | 330 |
/// \brief The nth arc. |
331 | 331 |
/// |
332 |
/// \pre n is in the [0..length() - 1] range |
|
332 |
/// \pre \c n is in the <tt>[0..length() - 1]</tt> range. |
|
333 | 333 |
const Arc& nth(int n) const { |
334 | 334 |
return data[n]; |
335 | 335 |
} |
336 | 336 |
|
337 | 337 |
/// \brief Initializes arc iterator to point to the nth arc. |
338 | 338 |
ArcIt nthIt(int n) const { |
339 | 339 |
return ArcIt(*this, n); |
340 | 340 |
} |
341 | 341 |
|
342 | 342 |
/// \brief The first arc of the path. |
343 | 343 |
const Arc& front() const { |
344 | 344 |
return data.front(); |
345 | 345 |
} |
346 | 346 |
|
347 | 347 |
/// \brief The last arc of the path. |
348 | 348 |
const Arc& back() const { |
349 | 349 |
return data.back(); |
350 | 350 |
} |
351 | 351 |
|
352 | 352 |
/// \brief Add a new arc behind the current path. |
353 | 353 |
void addBack(const Arc& arc) { |
354 | 354 |
data.push_back(arc); |
355 | 355 |
} |
356 | 356 |
|
357 | 357 |
/// \brief Erase the last arc of the path |
358 | 358 |
void eraseBack() { |
359 | 359 |
data.pop_back(); |
360 | 360 |
} |
361 | 361 |
|
362 | 362 |
typedef True BuildTag; |
363 | 363 |
|
364 | 364 |
template <typename CPath> |
365 | 365 |
void build(const CPath& path) { |
366 | 366 |
int len = path.length(); |
367 | 367 |
data.resize(len); |
368 | 368 |
int index = 0; |
369 | 369 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) { |
370 | 370 |
data[index] = it;; |
371 | 371 |
++index; |
372 | 372 |
} |
373 | 373 |
} |
374 | 374 |
|
375 | 375 |
template <typename CPath> |
376 | 376 |
void buildRev(const CPath& path) { |
377 | 377 |
int len = path.length(); |
378 | 378 |
data.resize(len); |
379 | 379 |
int index = len; |
380 | 380 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) { |
381 | 381 |
--index; |
382 | 382 |
data[index] = it;; |
383 | 383 |
} |
384 | 384 |
} |
385 | 385 |
|
386 | 386 |
protected: |
387 | 387 |
typedef std::vector<Arc> Container; |
388 | 388 |
Container data; |
389 | 389 |
|
390 | 390 |
}; |
391 | 391 |
|
392 | 392 |
/// \brief A structure for representing directed paths in a digraph. |
393 | 393 |
/// |
394 | 394 |
/// A structure for representing directed path in a digraph. |
395 |
/// \tparam |
|
395 |
/// \tparam GR The digraph type in which the path is. |
|
396 | 396 |
/// |
397 | 397 |
/// In a sense, the path can be treated as a list of arcs. The |
398 | 398 |
/// lemon path type stores just this list. As a consequence it |
399 | 399 |
/// cannot enumerate the nodes in the path and the zero length paths |
400 | 400 |
/// cannot store the source. |
401 | 401 |
/// |
402 | 402 |
/// This implementation is a back and front insertable and erasable |
403 | 403 |
/// path type. It can be indexed in O(k) time, where k is the rank |
404 | 404 |
/// of the arc in the path. The length can be computed in O(n) |
405 | 405 |
/// time. The front and back insertion and erasure is O(1) time |
406 | 406 |
/// and it can be splited and spliced in O(1) time. |
407 |
template <typename |
|
407 |
template <typename GR> |
|
408 | 408 |
class ListPath { |
409 | 409 |
public: |
410 | 410 |
|
411 |
typedef |
|
411 |
typedef GR Digraph; |
|
412 | 412 |
typedef typename Digraph::Arc Arc; |
413 | 413 |
|
414 | 414 |
protected: |
415 | 415 |
|
416 | 416 |
// the std::list<> is incompatible |
417 | 417 |
// hard to create invalid iterator |
418 | 418 |
struct Node { |
419 | 419 |
Arc arc; |
420 | 420 |
Node *next, *prev; |
421 | 421 |
}; |
422 | 422 |
|
423 | 423 |
Node *first, *last; |
424 | 424 |
|
425 | 425 |
std::allocator<Node> alloc; |
426 | 426 |
|
427 | 427 |
public: |
428 | 428 |
|
429 | 429 |
/// \brief Default constructor |
430 | 430 |
/// |
431 | 431 |
/// Default constructor |
432 | 432 |
ListPath() : first(0), last(0) {} |
433 | 433 |
|
434 | 434 |
/// \brief Template copy constructor |
435 | 435 |
/// |
436 | 436 |
/// This path can be initialized with any other path type. It just |
437 | 437 |
/// makes a copy of the given path. |
438 | 438 |
template <typename CPath> |
439 | 439 |
ListPath(const CPath& cpath) : first(0), last(0) { |
440 | 440 |
copyPath(*this, cpath); |
441 | 441 |
} |
442 | 442 |
|
443 | 443 |
/// \brief Destructor of the path |
444 | 444 |
/// |
445 | 445 |
/// Destructor of the path |
446 | 446 |
~ListPath() { |
447 | 447 |
clear(); |
448 | 448 |
} |
449 | 449 |
|
450 | 450 |
/// \brief Template copy assignment |
451 | 451 |
/// |
452 | 452 |
/// This path can be initialized with any other path type. It just |
453 | 453 |
/// makes a copy of the given path. |
454 | 454 |
template <typename CPath> |
455 | 455 |
ListPath& operator=(const CPath& cpath) { |
456 | 456 |
copyPath(*this, cpath); |
457 | 457 |
return *this; |
458 | 458 |
} |
459 | 459 |
|
... | ... |
@@ -462,97 +462,97 @@ |
462 | 462 |
/// This class is used to iterate on the arcs of the paths |
463 | 463 |
/// |
464 | 464 |
/// Of course it converts to Digraph::Arc |
465 | 465 |
class ArcIt { |
466 | 466 |
friend class ListPath; |
467 | 467 |
public: |
468 | 468 |
/// Default constructor |
469 | 469 |
ArcIt() {} |
470 | 470 |
/// Invalid constructor |
471 | 471 |
ArcIt(Invalid) : path(0), node(0) {} |
472 | 472 |
/// \brief Initializate the constructor to the first arc of path |
473 | 473 |
ArcIt(const ListPath &_path) |
474 | 474 |
: path(&_path), node(_path.first) {} |
475 | 475 |
|
476 | 476 |
protected: |
477 | 477 |
|
478 | 478 |
ArcIt(const ListPath &_path, Node *_node) |
479 | 479 |
: path(&_path), node(_node) {} |
480 | 480 |
|
481 | 481 |
|
482 | 482 |
public: |
483 | 483 |
|
484 | 484 |
///Conversion to Digraph::Arc |
485 | 485 |
operator const Arc&() const { |
486 | 486 |
return node->arc; |
487 | 487 |
} |
488 | 488 |
|
489 | 489 |
/// Next arc |
490 | 490 |
ArcIt& operator++() { |
491 | 491 |
node = node->next; |
492 | 492 |
return *this; |
493 | 493 |
} |
494 | 494 |
|
495 | 495 |
/// Comparison operator |
496 | 496 |
bool operator==(const ArcIt& e) const { return node==e.node; } |
497 | 497 |
/// Comparison operator |
498 | 498 |
bool operator!=(const ArcIt& e) const { return node!=e.node; } |
499 | 499 |
/// Comparison operator |
500 | 500 |
bool operator<(const ArcIt& e) const { return node<e.node; } |
501 | 501 |
|
502 | 502 |
private: |
503 | 503 |
const ListPath *path; |
504 | 504 |
Node *node; |
505 | 505 |
}; |
506 | 506 |
|
507 | 507 |
/// \brief The nth arc. |
508 | 508 |
/// |
509 | 509 |
/// This function looks for the nth arc in O(n) time. |
510 |
/// \pre n is in the [0..length() - 1] range |
|
510 |
/// \pre \c n is in the <tt>[0..length() - 1]</tt> range. |
|
511 | 511 |
const Arc& nth(int n) const { |
512 | 512 |
Node *node = first; |
513 | 513 |
for (int i = 0; i < n; ++i) { |
514 | 514 |
node = node->next; |
515 | 515 |
} |
516 | 516 |
return node->arc; |
517 | 517 |
} |
518 | 518 |
|
519 | 519 |
/// \brief Initializes arc iterator to point to the nth arc. |
520 | 520 |
ArcIt nthIt(int n) const { |
521 | 521 |
Node *node = first; |
522 | 522 |
for (int i = 0; i < n; ++i) { |
523 | 523 |
node = node->next; |
524 | 524 |
} |
525 | 525 |
return ArcIt(*this, node); |
526 | 526 |
} |
527 | 527 |
|
528 | 528 |
/// \brief Length of the path. |
529 | 529 |
int length() const { |
530 | 530 |
int len = 0; |
531 | 531 |
Node *node = first; |
532 | 532 |
while (node != 0) { |
533 | 533 |
node = node->next; |
534 | 534 |
++len; |
535 | 535 |
} |
536 | 536 |
return len; |
537 | 537 |
} |
538 | 538 |
|
539 | 539 |
/// \brief Return true if the path is empty. |
540 | 540 |
bool empty() const { return first == 0; } |
541 | 541 |
|
542 | 542 |
/// \brief Reset the path to an empty one. |
543 | 543 |
void clear() { |
544 | 544 |
while (first != 0) { |
545 | 545 |
last = first->next; |
546 | 546 |
alloc.destroy(first); |
547 | 547 |
alloc.deallocate(first, 1); |
548 | 548 |
first = last; |
549 | 549 |
} |
550 | 550 |
} |
551 | 551 |
|
552 | 552 |
/// \brief The first arc of the path |
553 | 553 |
const Arc& front() const { |
554 | 554 |
return first->arc; |
555 | 555 |
} |
556 | 556 |
|
557 | 557 |
/// \brief Add a new arc before the current path |
558 | 558 |
void addFront(const Arc& arc) { |
... | ... |
@@ -687,198 +687,198 @@ |
687 | 687 |
} |
688 | 688 |
|
689 | 689 |
/// \brief Split the current path. |
690 | 690 |
/// |
691 | 691 |
/// It splits the current path into two parts. The part before |
692 | 692 |
/// the iterator \c it will remain in the current path and the part |
693 | 693 |
/// starting with |
694 | 694 |
/// \c it will put into \c tpath. If \c tpath have arcs |
695 | 695 |
/// before the operation they are removed first. The time |
696 | 696 |
/// complexity of this function is O(1) plus the the time of emtying |
697 | 697 |
/// \c tpath. If \c it is \c INVALID then it just clears \c tpath |
698 | 698 |
void split(ArcIt it, ListPath& tpath) { |
699 | 699 |
tpath.clear(); |
700 | 700 |
if (it.node) { |
701 | 701 |
tpath.first = it.node; |
702 | 702 |
tpath.last = last; |
703 | 703 |
if (it.node->prev) { |
704 | 704 |
last = it.node->prev; |
705 | 705 |
last->next = 0; |
706 | 706 |
} else { |
707 | 707 |
first = last = 0; |
708 | 708 |
} |
709 | 709 |
it.node->prev = 0; |
710 | 710 |
} |
711 | 711 |
} |
712 | 712 |
|
713 | 713 |
|
714 | 714 |
typedef True BuildTag; |
715 | 715 |
|
716 | 716 |
template <typename CPath> |
717 | 717 |
void build(const CPath& path) { |
718 | 718 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) { |
719 | 719 |
addBack(it); |
720 | 720 |
} |
721 | 721 |
} |
722 | 722 |
|
723 | 723 |
template <typename CPath> |
724 | 724 |
void buildRev(const CPath& path) { |
725 | 725 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) { |
726 | 726 |
addFront(it); |
727 | 727 |
} |
728 | 728 |
} |
729 | 729 |
|
730 | 730 |
}; |
731 | 731 |
|
732 | 732 |
/// \brief A structure for representing directed paths in a digraph. |
733 | 733 |
/// |
734 | 734 |
/// A structure for representing directed path in a digraph. |
735 |
/// \tparam |
|
735 |
/// \tparam GR The digraph type in which the path is. |
|
736 | 736 |
/// |
737 | 737 |
/// In a sense, the path can be treated as a list of arcs. The |
738 | 738 |
/// lemon path type stores just this list. As a consequence it |
739 | 739 |
/// cannot enumerate the nodes in the path and the source node of |
740 | 740 |
/// a zero length path is undefined. |
741 | 741 |
/// |
742 | 742 |
/// This implementation is completly static, i.e. it can be copy constucted |
743 | 743 |
/// or copy assigned from another path, but otherwise it cannot be |
744 | 744 |
/// modified. |
745 | 745 |
/// |
746 | 746 |
/// Being the the most memory efficient path type in LEMON, |
747 | 747 |
/// it is intented to be |
748 | 748 |
/// used when you want to store a large number of paths. |
749 |
template <typename |
|
749 |
template <typename GR> |
|
750 | 750 |
class StaticPath { |
751 | 751 |
public: |
752 | 752 |
|
753 |
typedef |
|
753 |
typedef GR Digraph; |
|
754 | 754 |
typedef typename Digraph::Arc Arc; |
755 | 755 |
|
756 | 756 |
/// \brief Default constructor |
757 | 757 |
/// |
758 | 758 |
/// Default constructor |
759 | 759 |
StaticPath() : len(0), arcs(0) {} |
760 | 760 |
|
761 | 761 |
/// \brief Template copy constructor |
762 | 762 |
/// |
763 | 763 |
/// This path can be initialized from any other path type. |
764 | 764 |
template <typename CPath> |
765 | 765 |
StaticPath(const CPath& cpath) : arcs(0) { |
766 | 766 |
copyPath(*this, cpath); |
767 | 767 |
} |
768 | 768 |
|
769 | 769 |
/// \brief Destructor of the path |
770 | 770 |
/// |
771 | 771 |
/// Destructor of the path |
772 | 772 |
~StaticPath() { |
773 | 773 |
if (arcs) delete[] arcs; |
774 | 774 |
} |
775 | 775 |
|
776 | 776 |
/// \brief Template copy assignment |
777 | 777 |
/// |
778 | 778 |
/// This path can be made equal to any other path type. It simply |
779 | 779 |
/// makes a copy of the given path. |
780 | 780 |
template <typename CPath> |
781 | 781 |
StaticPath& operator=(const CPath& cpath) { |
782 | 782 |
copyPath(*this, cpath); |
783 | 783 |
return *this; |
784 | 784 |
} |
785 | 785 |
|
786 | 786 |
/// \brief Iterator class to iterate on the arcs of the paths |
787 | 787 |
/// |
788 | 788 |
/// This class is used to iterate on the arcs of the paths |
789 | 789 |
/// |
790 | 790 |
/// Of course it converts to Digraph::Arc |
791 | 791 |
class ArcIt { |
792 | 792 |
friend class StaticPath; |
793 | 793 |
public: |
794 | 794 |
/// Default constructor |
795 | 795 |
ArcIt() {} |
796 | 796 |
/// Invalid constructor |
797 | 797 |
ArcIt(Invalid) : path(0), idx(-1) {} |
798 | 798 |
/// Initializate the constructor to the first arc of path |
799 | 799 |
ArcIt(const StaticPath &_path) |
800 | 800 |
: path(&_path), idx(_path.empty() ? -1 : 0) {} |
801 | 801 |
|
802 | 802 |
private: |
803 | 803 |
|
804 | 804 |
/// Constructor with starting point |
805 | 805 |
ArcIt(const StaticPath &_path, int _idx) |
806 | 806 |
: idx(_idx), path(&_path) {} |
807 | 807 |
|
808 | 808 |
public: |
809 | 809 |
|
810 | 810 |
///Conversion to Digraph::Arc |
811 | 811 |
operator const Arc&() const { |
812 | 812 |
return path->nth(idx); |
813 | 813 |
} |
814 | 814 |
|
815 | 815 |
/// Next arc |
816 | 816 |
ArcIt& operator++() { |
817 | 817 |
++idx; |
818 | 818 |
if (idx >= path->length()) idx = -1; |
819 | 819 |
return *this; |
820 | 820 |
} |
821 | 821 |
|
822 | 822 |
/// Comparison operator |
823 | 823 |
bool operator==(const ArcIt& e) const { return idx==e.idx; } |
824 | 824 |
/// Comparison operator |
825 | 825 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; } |
826 | 826 |
/// Comparison operator |
827 | 827 |
bool operator<(const ArcIt& e) const { return idx<e.idx; } |
828 | 828 |
|
829 | 829 |
private: |
830 | 830 |
const StaticPath *path; |
831 | 831 |
int idx; |
832 | 832 |
}; |
833 | 833 |
|
834 | 834 |
/// \brief The nth arc. |
835 | 835 |
/// |
836 |
/// \pre n is in the [0..length() - 1] range |
|
836 |
/// \pre \c n is in the <tt>[0..length() - 1]</tt> range. |
|
837 | 837 |
const Arc& nth(int n) const { |
838 | 838 |
return arcs[n]; |
839 | 839 |
} |
840 | 840 |
|
841 | 841 |
/// \brief The arc iterator pointing to the nth arc. |
842 | 842 |
ArcIt nthIt(int n) const { |
843 | 843 |
return ArcIt(*this, n); |
844 | 844 |
} |
845 | 845 |
|
846 | 846 |
/// \brief The length of the path. |
847 | 847 |
int length() const { return len; } |
848 | 848 |
|
849 | 849 |
/// \brief Return true when the path is empty. |
850 | 850 |
int empty() const { return len == 0; } |
851 | 851 |
|
852 | 852 |
/// \brief Erase all arcs in the digraph. |
853 | 853 |
void clear() { |
854 | 854 |
len = 0; |
855 | 855 |
if (arcs) delete[] arcs; |
856 | 856 |
arcs = 0; |
857 | 857 |
} |
858 | 858 |
|
859 | 859 |
/// \brief The first arc of the path. |
860 | 860 |
const Arc& front() const { |
861 | 861 |
return arcs[0]; |
862 | 862 |
} |
863 | 863 |
|
864 | 864 |
/// \brief The last arc of the path. |
865 | 865 |
const Arc& back() const { |
866 | 866 |
return arcs[len - 1]; |
867 | 867 |
} |
868 | 868 |
|
869 | 869 |
|
870 | 870 |
typedef True BuildTag; |
871 | 871 |
|
872 | 872 |
template <typename CPath> |
873 | 873 |
void build(const CPath& path) { |
874 | 874 |
len = path.length(); |
875 | 875 |
arcs = new Arc[len]; |
876 | 876 |
int index = 0; |
877 | 877 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) { |
878 | 878 |
arcs[index] = it; |
879 | 879 |
++index; |
880 | 880 |
} |
881 | 881 |
} |
882 | 882 |
|
883 | 883 |
template <typename CPath> |
884 | 884 |
void buildRev(const CPath& path) { |
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 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_PREFLOW_H |
20 | 20 |
#define LEMON_PREFLOW_H |
21 | 21 |
|
22 | 22 |
#include <lemon/tolerance.h> |
23 | 23 |
#include <lemon/elevator.h> |
24 | 24 |
|
25 | 25 |
/// \file |
26 | 26 |
/// \ingroup max_flow |
27 | 27 |
/// \brief Implementation of the preflow algorithm. |
28 | 28 |
|
29 | 29 |
namespace lemon { |
30 | 30 |
|
31 | 31 |
/// \brief Default traits class of Preflow class. |
32 | 32 |
/// |
33 | 33 |
/// Default traits class of Preflow class. |
34 | 34 |
/// \tparam GR Digraph type. |
35 |
/// \tparam CM Capacity map type. |
|
36 |
template <typename GR, typename CM> |
|
35 |
/// \tparam CAP Capacity map type. |
|
36 |
template <typename GR, typename CAP> |
|
37 | 37 |
struct PreflowDefaultTraits { |
38 | 38 |
|
39 | 39 |
/// \brief The type of the digraph the algorithm runs on. |
40 | 40 |
typedef GR Digraph; |
41 | 41 |
|
42 | 42 |
/// \brief The type of the map that stores the arc capacities. |
43 | 43 |
/// |
44 | 44 |
/// The type of the map that stores the arc capacities. |
45 | 45 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
46 |
typedef |
|
46 |
typedef CAP CapacityMap; |
|
47 | 47 |
|
48 | 48 |
/// \brief The type of the flow values. |
49 | 49 |
typedef typename CapacityMap::Value Value; |
50 | 50 |
|
51 | 51 |
/// \brief The type of the map that stores the flow values. |
52 | 52 |
/// |
53 | 53 |
/// The type of the map that stores the flow values. |
54 | 54 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
55 | 55 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
56 | 56 |
|
57 | 57 |
/// \brief Instantiates a FlowMap. |
58 | 58 |
/// |
59 | 59 |
/// This function instantiates a \ref FlowMap. |
60 | 60 |
/// \param digraph The digraph, to which we would like to define |
61 | 61 |
/// the flow map. |
62 | 62 |
static FlowMap* createFlowMap(const Digraph& digraph) { |
63 | 63 |
return new FlowMap(digraph); |
64 | 64 |
} |
65 | 65 |
|
66 | 66 |
/// \brief The elevator type used by Preflow algorithm. |
67 | 67 |
/// |
68 | 68 |
/// The elevator type used by Preflow algorithm. |
69 | 69 |
/// |
70 | 70 |
/// \sa Elevator |
71 | 71 |
/// \sa LinkedElevator |
72 | 72 |
typedef LinkedElevator<Digraph, typename Digraph::Node> Elevator; |
73 | 73 |
|
74 | 74 |
/// \brief Instantiates an Elevator. |
75 | 75 |
/// |
76 | 76 |
/// This function instantiates an \ref Elevator. |
77 | 77 |
/// \param digraph The digraph, to which we would like to define |
78 | 78 |
/// the elevator. |
79 | 79 |
/// \param max_level The maximum level of the elevator. |
80 | 80 |
static Elevator* createElevator(const Digraph& digraph, int max_level) { |
81 | 81 |
return new Elevator(digraph, max_level); |
82 | 82 |
} |
83 | 83 |
|
84 | 84 |
/// \brief The tolerance used by the algorithm |
85 | 85 |
/// |
86 | 86 |
/// The tolerance used by the algorithm to handle inexact computation. |
87 | 87 |
typedef lemon::Tolerance<Value> Tolerance; |
88 | 88 |
|
89 | 89 |
}; |
90 | 90 |
|
91 | 91 |
|
92 | 92 |
/// \ingroup max_flow |
93 | 93 |
/// |
94 | 94 |
/// \brief %Preflow algorithm class. |
95 | 95 |
/// |
96 | 96 |
/// This class provides an implementation of Goldberg-Tarjan's \e preflow |
97 |
/// \e push-relabel algorithm producing a flow of maximum value in a |
|
98 |
/// digraph. The preflow algorithms are the fastest known maximum |
|
97 |
/// \e push-relabel algorithm producing a \ref max_flow |
|
98 |
/// "flow of maximum value" in a digraph. |
|
99 |
/// The preflow algorithms are the fastest known maximum |
|
99 | 100 |
/// flow algorithms. The current implementation use a mixture of the |
100 | 101 |
/// \e "highest label" and the \e "bound decrease" heuristics. |
101 | 102 |
/// The worst case time complexity of the algorithm is \f$O(n^2\sqrt{e})\f$. |
102 | 103 |
/// |
103 | 104 |
/// The algorithm consists of two phases. After the first phase |
104 | 105 |
/// the maximum flow value and the minimum cut is obtained. The |
105 | 106 |
/// second phase constructs a feasible maximum flow on each arc. |
106 | 107 |
/// |
107 | 108 |
/// \tparam GR The type of the digraph the algorithm runs on. |
108 |
/// \tparam |
|
109 |
/// \tparam CAP The type of the capacity map. The default map |
|
109 | 110 |
/// type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
110 | 111 |
#ifdef DOXYGEN |
111 |
template <typename GR, typename |
|
112 |
template <typename GR, typename CAP, typename TR> |
|
112 | 113 |
#else |
113 | 114 |
template <typename GR, |
114 |
typename CM = typename GR::template ArcMap<int>, |
|
115 |
typename TR = PreflowDefaultTraits<GR, CM> > |
|
115 |
typename CAP = typename GR::template ArcMap<int>, |
|
116 |
typename TR = PreflowDefaultTraits<GR, CAP> > |
|
116 | 117 |
#endif |
117 | 118 |
class Preflow { |
118 | 119 |
public: |
119 | 120 |
|
120 | 121 |
///The \ref PreflowDefaultTraits "traits class" of the algorithm. |
121 | 122 |
typedef TR Traits; |
122 | 123 |
///The type of the digraph the algorithm runs on. |
123 | 124 |
typedef typename Traits::Digraph Digraph; |
124 | 125 |
///The type of the capacity map. |
125 | 126 |
typedef typename Traits::CapacityMap CapacityMap; |
126 | 127 |
///The type of the flow values. |
127 | 128 |
typedef typename Traits::Value Value; |
128 | 129 |
|
129 | 130 |
///The type of the flow map. |
130 | 131 |
typedef typename Traits::FlowMap FlowMap; |
131 | 132 |
///The type of the elevator. |
132 | 133 |
typedef typename Traits::Elevator Elevator; |
133 | 134 |
///The type of the tolerance. |
134 | 135 |
typedef typename Traits::Tolerance Tolerance; |
135 | 136 |
|
136 | 137 |
private: |
137 | 138 |
|
138 | 139 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
139 | 140 |
|
140 | 141 |
const Digraph& _graph; |
141 | 142 |
const CapacityMap* _capacity; |
142 | 143 |
|
143 | 144 |
int _node_num; |
144 | 145 |
|
145 | 146 |
Node _source, _target; |
146 | 147 |
|
147 | 148 |
FlowMap* _flow; |
148 | 149 |
bool _local_flow; |
149 | 150 |
|
150 | 151 |
Elevator* _level; |
151 | 152 |
bool _local_level; |
152 | 153 |
|
153 | 154 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
154 | 155 |
ExcessMap* _excess; |
155 | 156 |
|
156 | 157 |
Tolerance _tolerance; |
157 | 158 |
|
158 | 159 |
bool _phase; |
159 | 160 |
|
160 | 161 |
|
161 | 162 |
void createStructures() { |
162 | 163 |
_node_num = countNodes(_graph); |
163 | 164 |
|
164 | 165 |
if (!_flow) { |
165 | 166 |
_flow = Traits::createFlowMap(_graph); |
166 | 167 |
_local_flow = true; |
167 | 168 |
} |
168 | 169 |
if (!_level) { |
169 | 170 |
_level = Traits::createElevator(_graph, _node_num); |
170 | 171 |
_local_level = true; |
171 | 172 |
} |
172 | 173 |
if (!_excess) { |
173 | 174 |
_excess = new ExcessMap(_graph); |
174 | 175 |
} |
175 | 176 |
} |
176 | 177 |
|
177 | 178 |
void destroyStructures() { |
178 | 179 |
if (_local_flow) { |
179 | 180 |
delete _flow; |
180 | 181 |
} |
181 | 182 |
if (_local_level) { |
182 | 183 |
delete _level; |
183 | 184 |
} |
184 | 185 |
if (_excess) { |
185 | 186 |
delete _excess; |
186 | 187 |
} |
187 | 188 |
} |
188 | 189 |
|
189 | 190 |
public: |
190 | 191 |
|
191 | 192 |
typedef Preflow Create; |
192 | 193 |
|
193 | 194 |
///\name Named Template Parameters |
194 | 195 |
|
195 | 196 |
///@{ |
196 | 197 |
|
197 |
template <typename |
|
198 |
template <typename T> |
|
198 | 199 |
struct SetFlowMapTraits : public Traits { |
199 |
typedef |
|
200 |
typedef T FlowMap; |
|
200 | 201 |
static FlowMap *createFlowMap(const Digraph&) { |
201 | 202 |
LEMON_ASSERT(false, "FlowMap is not initialized"); |
202 | 203 |
return 0; // ignore warnings |
203 | 204 |
} |
204 | 205 |
}; |
205 | 206 |
|
206 | 207 |
/// \brief \ref named-templ-param "Named parameter" for setting |
207 | 208 |
/// FlowMap type |
208 | 209 |
/// |
209 | 210 |
/// \ref named-templ-param "Named parameter" for setting FlowMap |
210 | 211 |
/// type. |
211 |
template <typename |
|
212 |
template <typename T> |
|
212 | 213 |
struct SetFlowMap |
213 |
: public Preflow<Digraph, CapacityMap, SetFlowMapTraits< |
|
214 |
: public Preflow<Digraph, CapacityMap, SetFlowMapTraits<T> > { |
|
214 | 215 |
typedef Preflow<Digraph, CapacityMap, |
215 |
SetFlowMapTraits< |
|
216 |
SetFlowMapTraits<T> > Create; |
|
216 | 217 |
}; |
217 | 218 |
|
218 |
template <typename |
|
219 |
template <typename T> |
|
219 | 220 |
struct SetElevatorTraits : public Traits { |
220 |
typedef |
|
221 |
typedef T Elevator; |
|
221 | 222 |
static Elevator *createElevator(const Digraph&, int) { |
222 | 223 |
LEMON_ASSERT(false, "Elevator is not initialized"); |
223 | 224 |
return 0; // ignore warnings |
224 | 225 |
} |
225 | 226 |
}; |
226 | 227 |
|
227 | 228 |
/// \brief \ref named-templ-param "Named parameter" for setting |
228 | 229 |
/// Elevator type |
229 | 230 |
/// |
230 | 231 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
231 | 232 |
/// type. If this named parameter is used, then an external |
232 | 233 |
/// elevator object must be passed to the algorithm using the |
233 | 234 |
/// \ref elevator(Elevator&) "elevator()" function before calling |
234 | 235 |
/// \ref run() or \ref init(). |
235 | 236 |
/// \sa SetStandardElevator |
236 |
template <typename |
|
237 |
template <typename T> |
|
237 | 238 |
struct SetElevator |
238 |
: public Preflow<Digraph, CapacityMap, SetElevatorTraits< |
|
239 |
: public Preflow<Digraph, CapacityMap, SetElevatorTraits<T> > { |
|
239 | 240 |
typedef Preflow<Digraph, CapacityMap, |
240 |
SetElevatorTraits< |
|
241 |
SetElevatorTraits<T> > Create; |
|
241 | 242 |
}; |
242 | 243 |
|
243 |
template <typename |
|
244 |
template <typename T> |
|
244 | 245 |
struct SetStandardElevatorTraits : public Traits { |
245 |
typedef |
|
246 |
typedef T Elevator; |
|
246 | 247 |
static Elevator *createElevator(const Digraph& digraph, int max_level) { |
247 | 248 |
return new Elevator(digraph, max_level); |
248 | 249 |
} |
249 | 250 |
}; |
250 | 251 |
|
251 | 252 |
/// \brief \ref named-templ-param "Named parameter" for setting |
252 | 253 |
/// Elevator type with automatic allocation |
253 | 254 |
/// |
254 | 255 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
255 | 256 |
/// type with automatic allocation. |
256 | 257 |
/// The Elevator should have standard constructor interface to be |
257 | 258 |
/// able to automatically created by the algorithm (i.e. the |
258 | 259 |
/// digraph and the maximum level should be passed to it). |
259 | 260 |
/// However an external elevator object could also be passed to the |
260 | 261 |
/// algorithm with the \ref elevator(Elevator&) "elevator()" function |
261 | 262 |
/// before calling \ref run() or \ref init(). |
262 | 263 |
/// \sa SetElevator |
263 |
template <typename |
|
264 |
template <typename T> |
|
264 | 265 |
struct SetStandardElevator |
265 | 266 |
: public Preflow<Digraph, CapacityMap, |
266 |
SetStandardElevatorTraits< |
|
267 |
SetStandardElevatorTraits<T> > { |
|
267 | 268 |
typedef Preflow<Digraph, CapacityMap, |
268 |
SetStandardElevatorTraits< |
|
269 |
SetStandardElevatorTraits<T> > Create; |
|
269 | 270 |
}; |
270 | 271 |
|
271 | 272 |
/// @} |
272 | 273 |
|
273 | 274 |
protected: |
274 | 275 |
|
275 | 276 |
Preflow() {} |
276 | 277 |
|
277 | 278 |
public: |
278 | 279 |
|
279 | 280 |
|
280 | 281 |
/// \brief The constructor of the class. |
281 | 282 |
/// |
282 | 283 |
/// The constructor of the class. |
283 | 284 |
/// \param digraph The digraph the algorithm runs on. |
284 | 285 |
/// \param capacity The capacity of the arcs. |
285 | 286 |
/// \param source The source node. |
286 | 287 |
/// \param target The target node. |
287 | 288 |
Preflow(const Digraph& digraph, const CapacityMap& capacity, |
288 | 289 |
Node source, Node target) |
289 | 290 |
: _graph(digraph), _capacity(&capacity), |
290 | 291 |
_node_num(0), _source(source), _target(target), |
291 | 292 |
_flow(0), _local_flow(false), |
292 | 293 |
_level(0), _local_level(false), |
293 | 294 |
_excess(0), _tolerance(), _phase() {} |
294 | 295 |
|
295 | 296 |
/// \brief Destructor. |
296 | 297 |
/// |
297 | 298 |
/// Destructor. |
298 | 299 |
~Preflow() { |
299 | 300 |
destroyStructures(); |
300 | 301 |
} |
301 | 302 |
|
302 | 303 |
/// \brief Sets the capacity map. |
303 | 304 |
/// |
304 | 305 |
/// Sets the capacity map. |
305 | 306 |
/// \return <tt>(*this)</tt> |
306 | 307 |
Preflow& capacityMap(const CapacityMap& map) { |
307 | 308 |
_capacity = ↦ |
308 | 309 |
return *this; |
309 | 310 |
} |
310 | 311 |
|
311 | 312 |
/// \brief Sets the flow map. |
312 | 313 |
/// |
313 | 314 |
/// Sets the flow map. |
314 | 315 |
/// If you don't use this function before calling \ref run() or |
315 | 316 |
/// \ref init(), an instance will be allocated automatically. |
316 | 317 |
/// The destructor deallocates this automatically allocated map, |
... | ... |
@@ -901,64 +902,64 @@ |
901 | 902 |
} |
902 | 903 |
|
903 | 904 |
/// \brief Returns the flow on the given arc. |
904 | 905 |
/// |
905 | 906 |
/// Returns the flow on the given arc. This method can |
906 | 907 |
/// be called after the second phase of the algorithm. |
907 | 908 |
/// |
908 | 909 |
/// \pre Either \ref run() or \ref init() must be called before |
909 | 910 |
/// using this function. |
910 | 911 |
Value flow(const Arc& arc) const { |
911 | 912 |
return (*_flow)[arc]; |
912 | 913 |
} |
913 | 914 |
|
914 | 915 |
/// \brief Returns a const reference to the flow map. |
915 | 916 |
/// |
916 | 917 |
/// Returns a const reference to the arc map storing the found flow. |
917 | 918 |
/// This method can be called after the second phase of the algorithm. |
918 | 919 |
/// |
919 | 920 |
/// \pre Either \ref run() or \ref init() must be called before |
920 | 921 |
/// using this function. |
921 | 922 |
const FlowMap& flowMap() const { |
922 | 923 |
return *_flow; |
923 | 924 |
} |
924 | 925 |
|
925 | 926 |
/// \brief Returns \c true when the node is on the source side of the |
926 | 927 |
/// minimum cut. |
927 | 928 |
/// |
928 | 929 |
/// Returns true when the node is on the source side of the found |
929 | 930 |
/// minimum cut. This method can be called both after running \ref |
930 | 931 |
/// startFirstPhase() and \ref startSecondPhase(). |
931 | 932 |
/// |
932 | 933 |
/// \pre Either \ref run() or \ref init() must be called before |
933 | 934 |
/// using this function. |
934 | 935 |
bool minCut(const Node& node) const { |
935 | 936 |
return ((*_level)[node] == _level->maxLevel()) == _phase; |
936 | 937 |
} |
937 | 938 |
|
938 | 939 |
/// \brief Gives back a minimum value cut. |
939 | 940 |
/// |
940 | 941 |
/// Sets \c cutMap to the characteristic vector of a minimum value |
941 | 942 |
/// cut. \c cutMap should be a \ref concepts::WriteMap "writable" |
942 | 943 |
/// node map with \c bool (or convertible) value type. |
943 | 944 |
/// |
944 | 945 |
/// This method can be called both after running \ref startFirstPhase() |
945 | 946 |
/// and \ref startSecondPhase(). The result after the second phase |
946 | 947 |
/// could be slightly different if inexact computation is used. |
947 | 948 |
/// |
948 | 949 |
/// \note This function calls \ref minCut() for each node, so it runs in |
949 |
/// |
|
950 |
/// O(n) time. |
|
950 | 951 |
/// |
951 | 952 |
/// \pre Either \ref run() or \ref init() must be called before |
952 | 953 |
/// using this function. |
953 | 954 |
template <typename CutMap> |
954 | 955 |
void minCutMap(CutMap& cutMap) const { |
955 | 956 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
956 | 957 |
cutMap.set(n, minCut(n)); |
957 | 958 |
} |
958 | 959 |
} |
959 | 960 |
|
960 | 961 |
/// @} |
961 | 962 |
}; |
962 | 963 |
} |
963 | 964 |
|
964 | 965 |
#endif |
... | ... |
@@ -160,101 +160,101 @@ |
160 | 160 |
|
161 | 161 |
Value mask = 0; |
162 | 162 |
int max_digit = 0; |
163 | 163 |
|
164 | 164 |
Iterator it; |
165 | 165 |
for (it = first; it != last; ++it) { |
166 | 166 |
while ((mask | functor(*it)) != mask) { |
167 | 167 |
++max_digit; |
168 | 168 |
mask <<= 1; mask |= 1; |
169 | 169 |
} |
170 | 170 |
} |
171 | 171 |
radixIntroSort(first, last, functor, 1 << max_digit); |
172 | 172 |
} |
173 | 173 |
|
174 | 174 |
|
175 | 175 |
template <typename Value, |
176 | 176 |
bool sign = std::numeric_limits<Value>::is_signed > |
177 | 177 |
struct RadixSortSelector { |
178 | 178 |
template <typename Iterator, typename Functor> |
179 | 179 |
static void sort(Iterator first, Iterator last, Functor functor) { |
180 | 180 |
radixSignedSort<Value>(first, last, functor); |
181 | 181 |
} |
182 | 182 |
}; |
183 | 183 |
|
184 | 184 |
template <typename Value> |
185 | 185 |
struct RadixSortSelector<Value, false> { |
186 | 186 |
template <typename Iterator, typename Functor> |
187 | 187 |
static void sort(Iterator first, Iterator last, Functor functor) { |
188 | 188 |
radixUnsignedSort<Value>(first, last, functor); |
189 | 189 |
} |
190 | 190 |
}; |
191 | 191 |
|
192 | 192 |
} |
193 | 193 |
|
194 | 194 |
/// \ingroup auxalg |
195 | 195 |
/// |
196 | 196 |
/// \brief Sorts the STL compatible range into ascending order. |
197 | 197 |
/// |
198 | 198 |
/// The \c radixSort sorts an STL compatible range into ascending |
199 | 199 |
/// order. The radix sort algorithm can sort items which are mapped |
200 | 200 |
/// to integers with an adaptable unary function \c functor and the |
201 | 201 |
/// order will be ascending according to these mapped values. |
202 | 202 |
/// |
203 | 203 |
/// It is also possible to use a normal function instead |
204 | 204 |
/// of the functor object. If the functor is not given it will use |
205 | 205 |
/// the identity function instead. |
206 | 206 |
/// |
207 | 207 |
/// This is a special quick sort algorithm where the pivot |
208 |
/// values to split the items are choosen to be \f$ 2^k \f$ for each \c k. |
|
209 |
/// Therefore, the time complexity of the |
|
210 |
/// algorithm is \f$ O(\log(c)n) \f$ and it uses \f$ O(\log(c)) \f$, |
|
211 |
/// additional space, where \c c is the maximal value and \c n is the |
|
212 |
/// |
|
208 |
/// values to split the items are choosen to be 2<sup>k</sup> |
|
209 |
/// for each \c k. |
|
210 |
/// Therefore, the time complexity of the algorithm is O(log(c)*n) and |
|
211 |
/// it uses O(log(c)) additional space, where \c c is the maximal value |
|
212 |
/// and \c n is the number of the items in the container. |
|
213 | 213 |
/// |
214 | 214 |
/// \param first The begin of the given range. |
215 | 215 |
/// \param last The end of the given range. |
216 | 216 |
/// \param functor An adaptible unary function or a normal function |
217 | 217 |
/// which maps the items to any integer type which can be either |
218 | 218 |
/// signed or unsigned. |
219 | 219 |
/// |
220 | 220 |
/// \sa stableRadixSort() |
221 | 221 |
template <typename Iterator, typename Functor> |
222 | 222 |
void radixSort(Iterator first, Iterator last, Functor functor) { |
223 | 223 |
using namespace _radix_sort_bits; |
224 | 224 |
typedef typename Functor::result_type Value; |
225 | 225 |
RadixSortSelector<Value>::sort(first, last, functor); |
226 | 226 |
} |
227 | 227 |
|
228 | 228 |
template <typename Iterator, typename Value, typename Key> |
229 | 229 |
void radixSort(Iterator first, Iterator last, Value (*functor)(Key)) { |
230 | 230 |
using namespace _radix_sort_bits; |
231 | 231 |
RadixSortSelector<Value>::sort(first, last, functor); |
232 | 232 |
} |
233 | 233 |
|
234 | 234 |
template <typename Iterator, typename Value, typename Key> |
235 | 235 |
void radixSort(Iterator first, Iterator last, Value& (*functor)(Key)) { |
236 | 236 |
using namespace _radix_sort_bits; |
237 | 237 |
RadixSortSelector<Value>::sort(first, last, functor); |
238 | 238 |
} |
239 | 239 |
|
240 | 240 |
template <typename Iterator, typename Value, typename Key> |
241 | 241 |
void radixSort(Iterator first, Iterator last, Value (*functor)(Key&)) { |
242 | 242 |
using namespace _radix_sort_bits; |
243 | 243 |
RadixSortSelector<Value>::sort(first, last, functor); |
244 | 244 |
} |
245 | 245 |
|
246 | 246 |
template <typename Iterator, typename Value, typename Key> |
247 | 247 |
void radixSort(Iterator first, Iterator last, Value& (*functor)(Key&)) { |
248 | 248 |
using namespace _radix_sort_bits; |
249 | 249 |
RadixSortSelector<Value>::sort(first, last, functor); |
250 | 250 |
} |
251 | 251 |
|
252 | 252 |
template <typename Iterator> |
253 | 253 |
void radixSort(Iterator first, Iterator last) { |
254 | 254 |
using namespace _radix_sort_bits; |
255 | 255 |
typedef typename std::iterator_traits<Iterator>::value_type Value; |
256 | 256 |
RadixSortSelector<Value>::sort(first, last, Identity<Value>()); |
257 | 257 |
} |
258 | 258 |
|
259 | 259 |
namespace _radix_sort_bits { |
260 | 260 |
|
... | ... |
@@ -385,100 +385,100 @@ |
385 | 385 |
if (dir) { |
386 | 386 |
std::copy(buffer, buffer + length, first); |
387 | 387 |
} else { |
388 | 388 |
std::copy(buffer + length, buffer + 2 * length, first); |
389 | 389 |
} |
390 | 390 |
} catch (...) { |
391 | 391 |
allocator.deallocate(buffer, 2 * length); |
392 | 392 |
throw; |
393 | 393 |
} |
394 | 394 |
allocator.deallocate(buffer, 2 * length); |
395 | 395 |
} |
396 | 396 |
|
397 | 397 |
|
398 | 398 |
|
399 | 399 |
template <typename Value, |
400 | 400 |
bool sign = std::numeric_limits<Value>::is_signed > |
401 | 401 |
struct StableRadixSortSelector { |
402 | 402 |
template <typename Iterator, typename Functor> |
403 | 403 |
static void sort(Iterator first, Iterator last, Functor functor) { |
404 | 404 |
stableRadixSignedSort<Value>(first, last, functor); |
405 | 405 |
} |
406 | 406 |
}; |
407 | 407 |
|
408 | 408 |
template <typename Value> |
409 | 409 |
struct StableRadixSortSelector<Value, false> { |
410 | 410 |
template <typename Iterator, typename Functor> |
411 | 411 |
static void sort(Iterator first, Iterator last, Functor functor) { |
412 | 412 |
stableRadixUnsignedSort<Value>(first, last, functor); |
413 | 413 |
} |
414 | 414 |
}; |
415 | 415 |
|
416 | 416 |
} |
417 | 417 |
|
418 | 418 |
/// \ingroup auxalg |
419 | 419 |
/// |
420 | 420 |
/// \brief Sorts the STL compatible range into ascending order in a stable |
421 | 421 |
/// way. |
422 | 422 |
/// |
423 | 423 |
/// This function sorts an STL compatible range into ascending |
424 | 424 |
/// order according to an integer mapping in the same as radixSort() does. |
425 | 425 |
/// |
426 | 426 |
/// This sorting algorithm is stable, i.e. the order of two equal |
427 | 427 |
/// elements remains the same after the sorting. |
428 | 428 |
/// |
429 | 429 |
/// This sort algorithm use a radix forward sort on the |
430 | 430 |
/// bytes of the integer number. The algorithm sorts the items |
431 | 431 |
/// byte-by-byte. First, it counts how many times a byte value occurs |
432 | 432 |
/// in the container, then it copies the corresponding items to |
433 |
/// another container in asceding order in |
|
433 |
/// another container in asceding order in O(n) time. |
|
434 | 434 |
/// |
435 |
/// The time complexity of the algorithm is \f$ O(\log(c)n) \f$ and |
|
436 |
/// it uses \f$ O(n) \f$, additional space, where \c c is the |
|
435 |
/// The time complexity of the algorithm is O(log(c)*n) and |
|
436 |
/// it uses O(n) additional space, where \c c is the |
|
437 | 437 |
/// maximal value and \c n is the number of the items in the |
438 | 438 |
/// container. |
439 | 439 |
/// |
440 | 440 |
|
441 | 441 |
/// \param first The begin of the given range. |
442 | 442 |
/// \param last The end of the given range. |
443 | 443 |
/// \param functor An adaptible unary function or a normal function |
444 | 444 |
/// which maps the items to any integer type which can be either |
445 | 445 |
/// signed or unsigned. |
446 | 446 |
/// \sa radixSort() |
447 | 447 |
template <typename Iterator, typename Functor> |
448 | 448 |
void stableRadixSort(Iterator first, Iterator last, Functor functor) { |
449 | 449 |
using namespace _radix_sort_bits; |
450 | 450 |
typedef typename Functor::result_type Value; |
451 | 451 |
StableRadixSortSelector<Value>::sort(first, last, functor); |
452 | 452 |
} |
453 | 453 |
|
454 | 454 |
template <typename Iterator, typename Value, typename Key> |
455 | 455 |
void stableRadixSort(Iterator first, Iterator last, Value (*functor)(Key)) { |
456 | 456 |
using namespace _radix_sort_bits; |
457 | 457 |
StableRadixSortSelector<Value>::sort(first, last, functor); |
458 | 458 |
} |
459 | 459 |
|
460 | 460 |
template <typename Iterator, typename Value, typename Key> |
461 | 461 |
void stableRadixSort(Iterator first, Iterator last, Value& (*functor)(Key)) { |
462 | 462 |
using namespace _radix_sort_bits; |
463 | 463 |
StableRadixSortSelector<Value>::sort(first, last, functor); |
464 | 464 |
} |
465 | 465 |
|
466 | 466 |
template <typename Iterator, typename Value, typename Key> |
467 | 467 |
void stableRadixSort(Iterator first, Iterator last, Value (*functor)(Key&)) { |
468 | 468 |
using namespace _radix_sort_bits; |
469 | 469 |
StableRadixSortSelector<Value>::sort(first, last, functor); |
470 | 470 |
} |
471 | 471 |
|
472 | 472 |
template <typename Iterator, typename Value, typename Key> |
473 | 473 |
void stableRadixSort(Iterator first, Iterator last, Value& (*functor)(Key&)) { |
474 | 474 |
using namespace _radix_sort_bits; |
475 | 475 |
StableRadixSortSelector<Value>::sort(first, last, functor); |
476 | 476 |
} |
477 | 477 |
|
478 | 478 |
template <typename Iterator> |
479 | 479 |
void stableRadixSort(Iterator first, Iterator last) { |
480 | 480 |
using namespace _radix_sort_bits; |
481 | 481 |
typedef typename std::iterator_traits<Iterator>::value_type Value; |
482 | 482 |
StableRadixSortSelector<Value>::sort(first, last, Identity<Value>()); |
483 | 483 |
} |
484 | 484 |
... | ... |
@@ -558,139 +558,139 @@ |
558 | 558 |
/// \brief Copy constructor |
559 | 559 |
/// |
560 | 560 |
/// Copy constructor. The generated sequence will be identical to |
561 | 561 |
/// the other sequence. It can be used to save the current state |
562 | 562 |
/// of the generator and later use it to generate the same |
563 | 563 |
/// sequence. |
564 | 564 |
Random(const Random& other) { |
565 | 565 |
core.copyState(other.core); |
566 | 566 |
} |
567 | 567 |
|
568 | 568 |
/// \brief Assign operator |
569 | 569 |
/// |
570 | 570 |
/// Assign operator. The generated sequence will be identical to |
571 | 571 |
/// the other sequence. It can be used to save the current state |
572 | 572 |
/// of the generator and later use it to generate the same |
573 | 573 |
/// sequence. |
574 | 574 |
Random& operator=(const Random& other) { |
575 | 575 |
if (&other != this) { |
576 | 576 |
core.copyState(other.core); |
577 | 577 |
} |
578 | 578 |
return *this; |
579 | 579 |
} |
580 | 580 |
|
581 | 581 |
/// \brief Seeding random sequence |
582 | 582 |
/// |
583 | 583 |
/// Seeding the random sequence. The current number type will be |
584 | 584 |
/// converted to the architecture word type. |
585 | 585 |
template <typename Number> |
586 | 586 |
void seed(Number seed) { |
587 | 587 |
_random_bits::Initializer<Number, Word>::init(core, seed); |
588 | 588 |
} |
589 | 589 |
|
590 | 590 |
/// \brief Seeding random sequence |
591 | 591 |
/// |
592 | 592 |
/// Seeding the random sequence. The given range should contain |
593 | 593 |
/// any number type and the numbers will be converted to the |
594 | 594 |
/// architecture word type. |
595 | 595 |
template <typename Iterator> |
596 | 596 |
void seed(Iterator begin, Iterator end) { |
597 | 597 |
typedef typename std::iterator_traits<Iterator>::value_type Number; |
598 | 598 |
_random_bits::Initializer<Number, Word>::init(core, begin, end); |
599 | 599 |
} |
600 | 600 |
|
601 | 601 |
/// \brief Seeding from file or from process id and time |
602 | 602 |
/// |
603 | 603 |
/// By default, this function calls the \c seedFromFile() member |
604 | 604 |
/// function with the <tt>/dev/urandom</tt> file. If it does not success, |
605 | 605 |
/// it uses the \c seedFromTime(). |
606 |
/// \return Currently always true. |
|
606 |
/// \return Currently always \c true. |
|
607 | 607 |
bool seed() { |
608 | 608 |
#ifndef WIN32 |
609 | 609 |
if (seedFromFile("/dev/urandom", 0)) return true; |
610 | 610 |
#endif |
611 | 611 |
if (seedFromTime()) return true; |
612 | 612 |
return false; |
613 | 613 |
} |
614 | 614 |
|
615 | 615 |
/// \brief Seeding from file |
616 | 616 |
/// |
617 | 617 |
/// Seeding the random sequence from file. The linux kernel has two |
618 | 618 |
/// devices, <tt>/dev/random</tt> and <tt>/dev/urandom</tt> which |
619 | 619 |
/// could give good seed values for pseudo random generators (The |
620 | 620 |
/// difference between two devices is that the <tt>random</tt> may |
621 | 621 |
/// block the reading operation while the kernel can give good |
622 | 622 |
/// source of randomness, while the <tt>urandom</tt> does not |
623 | 623 |
/// block the input, but it could give back bytes with worse |
624 | 624 |
/// entropy). |
625 | 625 |
/// \param file The source file |
626 | 626 |
/// \param offset The offset, from the file read. |
627 |
/// \return |
|
627 |
/// \return \c true when the seeding successes. |
|
628 | 628 |
#ifndef WIN32 |
629 | 629 |
bool seedFromFile(const std::string& file = "/dev/urandom", int offset = 0) |
630 | 630 |
#else |
631 | 631 |
bool seedFromFile(const std::string& file = "", int offset = 0) |
632 | 632 |
#endif |
633 | 633 |
{ |
634 | 634 |
std::ifstream rs(file.c_str()); |
635 | 635 |
const int size = 4; |
636 | 636 |
Word buf[size]; |
637 | 637 |
if (offset != 0 && !rs.seekg(offset)) return false; |
638 | 638 |
if (!rs.read(reinterpret_cast<char*>(buf), sizeof(buf))) return false; |
639 | 639 |
seed(buf, buf + size); |
640 | 640 |
return true; |
641 | 641 |
} |
642 | 642 |
|
643 | 643 |
/// \brief Seding from process id and time |
644 | 644 |
/// |
645 | 645 |
/// Seding from process id and time. This function uses the |
646 | 646 |
/// current process id and the current time for initialize the |
647 | 647 |
/// random sequence. |
648 |
/// \return Currently always true. |
|
648 |
/// \return Currently always \c true. |
|
649 | 649 |
bool seedFromTime() { |
650 | 650 |
#ifndef WIN32 |
651 | 651 |
timeval tv; |
652 | 652 |
gettimeofday(&tv, 0); |
653 | 653 |
seed(getpid() + tv.tv_sec + tv.tv_usec); |
654 | 654 |
#else |
655 | 655 |
seed(bits::getWinRndSeed()); |
656 | 656 |
#endif |
657 | 657 |
return true; |
658 | 658 |
} |
659 | 659 |
|
660 | 660 |
/// @} |
661 | 661 |
|
662 | 662 |
///\name Uniform distributions |
663 | 663 |
/// |
664 | 664 |
/// @{ |
665 | 665 |
|
666 | 666 |
/// \brief Returns a random real number from the range [0, 1) |
667 | 667 |
/// |
668 | 668 |
/// It returns a random real number from the range [0, 1). The |
669 | 669 |
/// default Number type is \c double. |
670 | 670 |
template <typename Number> |
671 | 671 |
Number real() { |
672 | 672 |
return _random_bits::RealConversion<Number, Word>::convert(core); |
673 | 673 |
} |
674 | 674 |
|
675 | 675 |
double real() { |
676 | 676 |
return real<double>(); |
677 | 677 |
} |
678 | 678 |
|
679 | 679 |
/// \brief Returns a random real number from the range [0, 1) |
680 | 680 |
/// |
681 | 681 |
/// It returns a random double from the range [0, 1). |
682 | 682 |
double operator()() { |
683 | 683 |
return real<double>(); |
684 | 684 |
} |
685 | 685 |
|
686 | 686 |
/// \brief Returns a random real number from the range [0, b) |
687 | 687 |
/// |
688 | 688 |
/// It returns a random real number from the range [0, b). |
689 | 689 |
double operator()(double b) { |
690 | 690 |
return real<double>() * b; |
691 | 691 |
} |
692 | 692 |
|
693 | 693 |
/// \brief Returns a random real number from the range [a, b) |
694 | 694 |
/// |
695 | 695 |
/// It returns a random real number from the range [a, b). |
696 | 696 |
double operator()(double a, double b) { |
... | ... |
@@ -180,105 +180,105 @@ |
180 | 180 |
} |
181 | 181 |
|
182 | 182 |
}; |
183 | 183 |
|
184 | 184 |
typedef DigraphExtender<SmartDigraphBase> ExtendedSmartDigraphBase; |
185 | 185 |
|
186 | 186 |
///\ingroup graphs |
187 | 187 |
/// |
188 | 188 |
///\brief A smart directed graph class. |
189 | 189 |
/// |
190 | 190 |
///This is a simple and fast digraph implementation. |
191 | 191 |
///It is also quite memory efficient, but at the price |
192 | 192 |
///that <b> it does support only limited (only stack-like) |
193 | 193 |
///node and arc deletions</b>. |
194 | 194 |
///It conforms to the \ref concepts::Digraph "Digraph concept" with |
195 | 195 |
///an important extra feature that its maps are real \ref |
196 | 196 |
///concepts::ReferenceMap "reference map"s. |
197 | 197 |
/// |
198 | 198 |
///\sa concepts::Digraph. |
199 | 199 |
class SmartDigraph : public ExtendedSmartDigraphBase { |
200 | 200 |
public: |
201 | 201 |
|
202 | 202 |
typedef ExtendedSmartDigraphBase Parent; |
203 | 203 |
|
204 | 204 |
private: |
205 | 205 |
|
206 | 206 |
///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead. |
207 | 207 |
|
208 | 208 |
///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead. |
209 | 209 |
/// |
210 | 210 |
SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {}; |
211 | 211 |
///\brief Assignment of SmartDigraph to another one is \e not allowed. |
212 | 212 |
///Use DigraphCopy() instead. |
213 | 213 |
|
214 | 214 |
///Assignment of SmartDigraph to another one is \e not allowed. |
215 | 215 |
///Use DigraphCopy() instead. |
216 | 216 |
void operator=(const SmartDigraph &) {} |
217 | 217 |
|
218 | 218 |
public: |
219 | 219 |
|
220 | 220 |
/// Constructor |
221 | 221 |
|
222 | 222 |
/// Constructor. |
223 | 223 |
/// |
224 | 224 |
SmartDigraph() {}; |
225 | 225 |
|
226 | 226 |
///Add a new node to the digraph. |
227 | 227 |
|
228 |
/// \return the new node. |
|
229 |
/// |
|
228 |
/// Add a new node to the digraph. |
|
229 |
/// \return The new node. |
|
230 | 230 |
Node addNode() { return Parent::addNode(); } |
231 | 231 |
|
232 | 232 |
///Add a new arc to the digraph. |
233 | 233 |
|
234 | 234 |
///Add a new arc to the digraph with source node \c s |
235 | 235 |
///and target node \c t. |
236 |
///\return |
|
236 |
///\return The new arc. |
|
237 | 237 |
Arc addArc(const Node& s, const Node& t) { |
238 | 238 |
return Parent::addArc(s, t); |
239 | 239 |
} |
240 | 240 |
|
241 | 241 |
/// \brief Using this it is possible to avoid the superfluous memory |
242 | 242 |
/// allocation. |
243 | 243 |
|
244 | 244 |
/// Using this it is possible to avoid the superfluous memory |
245 | 245 |
/// allocation: if you know that the digraph you want to build will |
246 | 246 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
247 | 247 |
/// then it is worth reserving space for this amount before starting |
248 | 248 |
/// to build the digraph. |
249 | 249 |
/// \sa reserveArc |
250 | 250 |
void reserveNode(int n) { nodes.reserve(n); }; |
251 | 251 |
|
252 | 252 |
/// \brief Using this it is possible to avoid the superfluous memory |
253 | 253 |
/// allocation. |
254 | 254 |
|
255 | 255 |
/// Using this it is possible to avoid the superfluous memory |
256 | 256 |
/// allocation: if you know that the digraph you want to build will |
257 | 257 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
258 | 258 |
/// then it is worth reserving space for this amount before starting |
259 | 259 |
/// to build the digraph. |
260 | 260 |
/// \sa reserveNode |
261 | 261 |
void reserveArc(int m) { arcs.reserve(m); }; |
262 | 262 |
|
263 | 263 |
/// \brief Node validity check |
264 | 264 |
/// |
265 | 265 |
/// This function gives back true if the given node is valid, |
266 | 266 |
/// ie. it is a real node of the graph. |
267 | 267 |
/// |
268 | 268 |
/// \warning A removed node (using Snapshot) could become valid again |
269 | 269 |
/// when new nodes are added to the graph. |
270 | 270 |
bool valid(Node n) const { return Parent::valid(n); } |
271 | 271 |
|
272 | 272 |
/// \brief Arc validity check |
273 | 273 |
/// |
274 | 274 |
/// This function gives back true if the given arc is valid, |
275 | 275 |
/// ie. it is a real arc of the graph. |
276 | 276 |
/// |
277 | 277 |
/// \warning A removed arc (using Snapshot) could become valid again |
278 | 278 |
/// when new arcs are added to the graph. |
279 | 279 |
bool valid(Arc a) const { return Parent::valid(a); } |
280 | 280 |
|
281 | 281 |
///Clear the digraph. |
282 | 282 |
|
283 | 283 |
///Erase all the nodes and arcs from the digraph. |
284 | 284 |
/// |
... | ... |
@@ -621,105 +621,105 @@ |
621 | 621 |
|
622 | 622 |
typedef GraphExtender<SmartGraphBase> ExtendedSmartGraphBase; |
623 | 623 |
|
624 | 624 |
/// \ingroup graphs |
625 | 625 |
/// |
626 | 626 |
/// \brief A smart undirected graph class. |
627 | 627 |
/// |
628 | 628 |
/// This is a simple and fast graph implementation. |
629 | 629 |
/// It is also quite memory efficient, but at the price |
630 | 630 |
/// that <b> it does support only limited (only stack-like) |
631 | 631 |
/// node and arc deletions</b>. |
632 | 632 |
/// Except from this it conforms to |
633 | 633 |
/// the \ref concepts::Graph "Graph concept". |
634 | 634 |
/// |
635 | 635 |
/// It also has an |
636 | 636 |
/// important extra feature that |
637 | 637 |
/// its maps are real \ref concepts::ReferenceMap "reference map"s. |
638 | 638 |
/// |
639 | 639 |
/// \sa concepts::Graph. |
640 | 640 |
/// |
641 | 641 |
class SmartGraph : public ExtendedSmartGraphBase { |
642 | 642 |
private: |
643 | 643 |
|
644 | 644 |
///SmartGraph is \e not copy constructible. Use GraphCopy() instead. |
645 | 645 |
|
646 | 646 |
///SmartGraph is \e not copy constructible. Use GraphCopy() instead. |
647 | 647 |
/// |
648 | 648 |
SmartGraph(const SmartGraph &) : ExtendedSmartGraphBase() {}; |
649 | 649 |
|
650 | 650 |
///\brief Assignment of SmartGraph to another one is \e not allowed. |
651 | 651 |
///Use GraphCopy() instead. |
652 | 652 |
|
653 | 653 |
///Assignment of SmartGraph to another one is \e not allowed. |
654 | 654 |
///Use GraphCopy() instead. |
655 | 655 |
void operator=(const SmartGraph &) {} |
656 | 656 |
|
657 | 657 |
public: |
658 | 658 |
|
659 | 659 |
typedef ExtendedSmartGraphBase Parent; |
660 | 660 |
|
661 | 661 |
/// Constructor |
662 | 662 |
|
663 | 663 |
/// Constructor. |
664 | 664 |
/// |
665 | 665 |
SmartGraph() {} |
666 | 666 |
|
667 | 667 |
///Add a new node to the graph. |
668 | 668 |
|
669 |
/// \return the new node. |
|
670 |
/// |
|
669 |
/// Add a new node to the graph. |
|
670 |
/// \return The new node. |
|
671 | 671 |
Node addNode() { return Parent::addNode(); } |
672 | 672 |
|
673 | 673 |
///Add a new edge to the graph. |
674 | 674 |
|
675 | 675 |
///Add a new edge to the graph with node \c s |
676 | 676 |
///and \c t. |
677 |
///\return |
|
677 |
///\return The new edge. |
|
678 | 678 |
Edge addEdge(const Node& s, const Node& t) { |
679 | 679 |
return Parent::addEdge(s, t); |
680 | 680 |
} |
681 | 681 |
|
682 | 682 |
/// \brief Node validity check |
683 | 683 |
/// |
684 | 684 |
/// This function gives back true if the given node is valid, |
685 | 685 |
/// ie. it is a real node of the graph. |
686 | 686 |
/// |
687 | 687 |
/// \warning A removed node (using Snapshot) could become valid again |
688 | 688 |
/// when new nodes are added to the graph. |
689 | 689 |
bool valid(Node n) const { return Parent::valid(n); } |
690 | 690 |
|
691 | 691 |
/// \brief Arc validity check |
692 | 692 |
/// |
693 | 693 |
/// This function gives back true if the given arc is valid, |
694 | 694 |
/// ie. it is a real arc of the graph. |
695 | 695 |
/// |
696 | 696 |
/// \warning A removed arc (using Snapshot) could become valid again |
697 | 697 |
/// when new edges are added to the graph. |
698 | 698 |
bool valid(Arc a) const { return Parent::valid(a); } |
699 | 699 |
|
700 | 700 |
/// \brief Edge validity check |
701 | 701 |
/// |
702 | 702 |
/// This function gives back true if the given edge is valid, |
703 | 703 |
/// ie. it is a real edge of the graph. |
704 | 704 |
/// |
705 | 705 |
/// \warning A removed edge (using Snapshot) could become valid again |
706 | 706 |
/// when new edges are added to the graph. |
707 | 707 |
bool valid(Edge e) const { return Parent::valid(e); } |
708 | 708 |
|
709 | 709 |
///Clear the graph. |
710 | 710 |
|
711 | 711 |
///Erase all the nodes and edges from the graph. |
712 | 712 |
/// |
713 | 713 |
void clear() { |
714 | 714 |
Parent::clear(); |
715 | 715 |
} |
716 | 716 |
|
717 | 717 |
public: |
718 | 718 |
|
719 | 719 |
class Snapshot; |
720 | 720 |
|
721 | 721 |
protected: |
722 | 722 |
|
723 | 723 |
void saveSnapshot(Snapshot &s) |
724 | 724 |
{ |
725 | 725 |
s._graph = this; |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_SUURBALLE_H |
20 | 20 |
#define LEMON_SUURBALLE_H |
21 | 21 |
|
22 | 22 |
///\ingroup shortest_path |
23 | 23 |
///\file |
24 | 24 |
///\brief An algorithm for finding arc-disjoint paths between two |
25 | 25 |
/// nodes having minimum total length. |
26 | 26 |
|
27 | 27 |
#include <vector> |
28 | 28 |
#include <lemon/bin_heap.h> |
29 | 29 |
#include <lemon/path.h> |
30 | 30 |
#include <lemon/list_graph.h> |
31 | 31 |
#include <lemon/maps.h> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
/// \addtogroup shortest_path |
36 | 36 |
/// @{ |
37 | 37 |
|
38 | 38 |
/// \brief Algorithm for finding arc-disjoint paths between two nodes |
39 | 39 |
/// having minimum total length. |
40 | 40 |
/// |
41 | 41 |
/// \ref lemon::Suurballe "Suurballe" implements an algorithm for |
42 | 42 |
/// finding arc-disjoint paths having minimum total length (cost) |
43 | 43 |
/// from a given source node to a given target node in a digraph. |
44 | 44 |
/// |
45 | 45 |
/// In fact, this implementation is the specialization of the |
46 | 46 |
/// \ref CapacityScaling "successive shortest path" algorithm. |
47 | 47 |
/// |
48 |
/// \tparam |
|
48 |
/// \tparam GR The digraph type the algorithm runs on. |
|
49 | 49 |
/// The default value is \c ListDigraph. |
50 |
/// \tparam |
|
50 |
/// \tparam LEN The type of the length (cost) map. |
|
51 | 51 |
/// The default value is <tt>Digraph::ArcMap<int></tt>. |
52 | 52 |
/// |
53 | 53 |
/// \warning Length values should be \e non-negative \e integers. |
54 | 54 |
/// |
55 | 55 |
/// \note For finding node-disjoint paths this algorithm can be used |
56 | 56 |
/// with \ref SplitNodes. |
57 | 57 |
#ifdef DOXYGEN |
58 |
template <typename |
|
58 |
template <typename GR, typename LEN> |
|
59 | 59 |
#else |
60 |
template < typename Digraph = ListDigraph, |
|
61 |
typename LengthMap = typename Digraph::template ArcMap<int> > |
|
60 |
template < typename GR = ListDigraph, |
|
61 |
typename LEN = typename GR::template ArcMap<int> > |
|
62 | 62 |
#endif |
63 | 63 |
class Suurballe |
64 | 64 |
{ |
65 |
TEMPLATE_DIGRAPH_TYPEDEFS( |
|
65 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
|
66 | 66 |
|
67 |
typedef typename LengthMap::Value Length; |
|
68 | 67 |
typedef ConstMap<Arc, int> ConstArcMap; |
69 |
typedef typename |
|
68 |
typedef typename GR::template NodeMap<Arc> PredMap; |
|
70 | 69 |
|
71 | 70 |
public: |
72 | 71 |
|
72 |
/// The type of the digraph the algorithm runs on. |
|
73 |
typedef GR Digraph; |
|
74 |
/// The type of the length map. |
|
75 |
typedef LEN LengthMap; |
|
76 |
/// The type of the lengths. |
|
77 |
typedef typename LengthMap::Value Length; |
|
73 | 78 |
/// The type of the flow map. |
74 | 79 |
typedef typename Digraph::template ArcMap<int> FlowMap; |
75 | 80 |
/// The type of the potential map. |
76 | 81 |
typedef typename Digraph::template NodeMap<Length> PotentialMap; |
77 | 82 |
/// The type of the path structures. |
78 | 83 |
typedef SimplePath<Digraph> Path; |
79 | 84 |
|
80 | 85 |
private: |
81 | 86 |
|
82 | 87 |
/// \brief Special implementation of the Dijkstra algorithm |
83 | 88 |
/// for finding shortest paths in the residual network. |
84 | 89 |
/// |
85 | 90 |
/// \ref ResidualDijkstra is a special implementation of the |
86 | 91 |
/// \ref Dijkstra algorithm for finding shortest paths in the |
87 | 92 |
/// residual network of the digraph with respect to the reduced arc |
88 | 93 |
/// lengths and modifying the node potentials according to the |
89 | 94 |
/// distance of the nodes. |
90 | 95 |
class ResidualDijkstra |
91 | 96 |
{ |
92 | 97 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
93 | 98 |
typedef BinHeap<Length, HeapCrossRef> Heap; |
94 | 99 |
|
95 | 100 |
private: |
96 | 101 |
|
97 | 102 |
// The digraph the algorithm runs on |
98 | 103 |
const Digraph &_graph; |
99 | 104 |
|
100 | 105 |
// The main maps |
101 | 106 |
const FlowMap &_flow; |
102 | 107 |
const LengthMap &_length; |
103 | 108 |
PotentialMap &_potential; |
104 | 109 |
|
105 | 110 |
// The distance map |
106 | 111 |
PotentialMap _dist; |
107 | 112 |
// The pred arc map |
108 | 113 |
PredMap &_pred; |
109 | 114 |
// The processed (i.e. permanently labeled) nodes |
110 | 115 |
std::vector<Node> _proc_nodes; |
111 | 116 |
|
112 | 117 |
Node _s; |
113 | 118 |
Node _t; |
114 | 119 |
|
115 | 120 |
public: |
116 | 121 |
|
117 | 122 |
/// Constructor. |
118 | 123 |
ResidualDijkstra( const Digraph &digraph, |
119 | 124 |
const FlowMap &flow, |
120 | 125 |
const LengthMap &length, |
... | ... |
@@ -211,114 +216,114 @@ |
211 | 216 |
bool _local_potential; |
212 | 217 |
|
213 | 218 |
// The source node |
214 | 219 |
Node _source; |
215 | 220 |
// The target node |
216 | 221 |
Node _target; |
217 | 222 |
|
218 | 223 |
// Container to store the found paths |
219 | 224 |
std::vector< SimplePath<Digraph> > paths; |
220 | 225 |
int _path_num; |
221 | 226 |
|
222 | 227 |
// The pred arc map |
223 | 228 |
PredMap _pred; |
224 | 229 |
// Implementation of the Dijkstra algorithm for finding augmenting |
225 | 230 |
// shortest paths in the residual network |
226 | 231 |
ResidualDijkstra *_dijkstra; |
227 | 232 |
|
228 | 233 |
public: |
229 | 234 |
|
230 | 235 |
/// \brief Constructor. |
231 | 236 |
/// |
232 | 237 |
/// Constructor. |
233 | 238 |
/// |
234 | 239 |
/// \param digraph The digraph the algorithm runs on. |
235 | 240 |
/// \param length The length (cost) values of the arcs. |
236 | 241 |
/// \param s The source node. |
237 | 242 |
/// \param t The target node. |
238 | 243 |
Suurballe( const Digraph &digraph, |
239 | 244 |
const LengthMap &length, |
240 | 245 |
Node s, Node t ) : |
241 | 246 |
_graph(digraph), _length(length), _flow(0), _local_flow(false), |
242 | 247 |
_potential(0), _local_potential(false), _source(s), _target(t), |
243 | 248 |
_pred(digraph) {} |
244 | 249 |
|
245 | 250 |
/// Destructor. |
246 | 251 |
~Suurballe() { |
247 | 252 |
if (_local_flow) delete _flow; |
248 | 253 |
if (_local_potential) delete _potential; |
249 | 254 |
delete _dijkstra; |
250 | 255 |
} |
251 | 256 |
|
252 | 257 |
/// \brief Set the flow map. |
253 | 258 |
/// |
254 | 259 |
/// This function sets the flow map. |
255 | 260 |
/// |
256 | 261 |
/// The found flow contains only 0 and 1 values. It is the union of |
257 | 262 |
/// the found arc-disjoint paths. |
258 | 263 |
/// |
259 |
/// \return |
|
264 |
/// \return <tt>(*this)</tt> |
|
260 | 265 |
Suurballe& flowMap(FlowMap &map) { |
261 | 266 |
if (_local_flow) { |
262 | 267 |
delete _flow; |
263 | 268 |
_local_flow = false; |
264 | 269 |
} |
265 | 270 |
_flow = ↦ |
266 | 271 |
return *this; |
267 | 272 |
} |
268 | 273 |
|
269 | 274 |
/// \brief Set the potential map. |
270 | 275 |
/// |
271 | 276 |
/// This function sets the potential map. |
272 | 277 |
/// |
273 | 278 |
/// The potentials provide the dual solution of the underlying |
274 | 279 |
/// minimum cost flow problem. |
275 | 280 |
/// |
276 |
/// \return |
|
281 |
/// \return <tt>(*this)</tt> |
|
277 | 282 |
Suurballe& potentialMap(PotentialMap &map) { |
278 | 283 |
if (_local_potential) { |
279 | 284 |
delete _potential; |
280 | 285 |
_local_potential = false; |
281 | 286 |
} |
282 | 287 |
_potential = ↦ |
283 | 288 |
return *this; |
284 | 289 |
} |
285 | 290 |
|
286 | 291 |
/// \name Execution control |
287 | 292 |
/// The simplest way to execute the algorithm is to call the run() |
288 | 293 |
/// function. |
289 | 294 |
/// \n |
290 | 295 |
/// If you only need the flow that is the union of the found |
291 | 296 |
/// arc-disjoint paths, you may call init() and findFlow(). |
292 | 297 |
|
293 | 298 |
/// @{ |
294 | 299 |
|
295 | 300 |
/// \brief Run the algorithm. |
296 | 301 |
/// |
297 | 302 |
/// This function runs the algorithm. |
298 | 303 |
/// |
299 | 304 |
/// \param k The number of paths to be found. |
300 | 305 |
/// |
301 | 306 |
/// \return \c k if there are at least \c k arc-disjoint paths from |
302 | 307 |
/// \c s to \c t in the digraph. Otherwise it returns the number of |
303 | 308 |
/// arc-disjoint paths found. |
304 | 309 |
/// |
305 | 310 |
/// \note Apart from the return value, <tt>s.run(k)</tt> is just a |
306 | 311 |
/// shortcut of the following code. |
307 | 312 |
/// \code |
308 | 313 |
/// s.init(); |
309 | 314 |
/// s.findFlow(k); |
310 | 315 |
/// s.findPaths(); |
311 | 316 |
/// \endcode |
312 | 317 |
int run(int k = 2) { |
313 | 318 |
init(); |
314 | 319 |
findFlow(k); |
315 | 320 |
findPaths(); |
316 | 321 |
return _path_num; |
317 | 322 |
} |
318 | 323 |
|
319 | 324 |
/// \brief Initialize the algorithm. |
320 | 325 |
/// |
321 | 326 |
/// This function initializes the algorithm. |
322 | 327 |
void init() { |
323 | 328 |
// Initialize maps |
324 | 329 |
if (!_flow) { |
... | ... |
@@ -413,91 +418,91 @@ |
413 | 418 |
/// |
414 | 419 |
/// This function returns a const reference to the arc map storing |
415 | 420 |
/// the flow that is the union of the found arc-disjoint paths. |
416 | 421 |
/// |
417 | 422 |
/// \pre \ref run() or \ref findFlow() must be called before using |
418 | 423 |
/// this function. |
419 | 424 |
const FlowMap& flowMap() const { |
420 | 425 |
return *_flow; |
421 | 426 |
} |
422 | 427 |
|
423 | 428 |
/// \brief Return a const reference to the node map storing the |
424 | 429 |
/// found potentials (the dual solution). |
425 | 430 |
/// |
426 | 431 |
/// This function returns a const reference to the node map storing |
427 | 432 |
/// the found potentials that provide the dual solution of the |
428 | 433 |
/// underlying minimum cost flow problem. |
429 | 434 |
/// |
430 | 435 |
/// \pre \ref run() or \ref findFlow() must be called before using |
431 | 436 |
/// this function. |
432 | 437 |
const PotentialMap& potentialMap() const { |
433 | 438 |
return *_potential; |
434 | 439 |
} |
435 | 440 |
|
436 | 441 |
/// \brief Return the flow on the given arc. |
437 | 442 |
/// |
438 | 443 |
/// This function returns the flow on the given arc. |
439 | 444 |
/// It is \c 1 if the arc is involved in one of the found paths, |
440 | 445 |
/// otherwise it is \c 0. |
441 | 446 |
/// |
442 | 447 |
/// \pre \ref run() or \ref findFlow() must be called before using |
443 | 448 |
/// this function. |
444 | 449 |
int flow(const Arc& arc) const { |
445 | 450 |
return (*_flow)[arc]; |
446 | 451 |
} |
447 | 452 |
|
448 | 453 |
/// \brief Return the potential of the given node. |
449 | 454 |
/// |
450 | 455 |
/// This function returns the potential of the given node. |
451 | 456 |
/// |
452 | 457 |
/// \pre \ref run() or \ref findFlow() must be called before using |
453 | 458 |
/// this function. |
454 | 459 |
Length potential(const Node& node) const { |
455 | 460 |
return (*_potential)[node]; |
456 | 461 |
} |
457 | 462 |
|
458 | 463 |
/// \brief Return the total length (cost) of the found paths (flow). |
459 | 464 |
/// |
460 | 465 |
/// This function returns the total length (cost) of the found paths |
461 |
/// (flow). The complexity of the function is |
|
466 |
/// (flow). The complexity of the function is O(e). |
|
462 | 467 |
/// |
463 | 468 |
/// \pre \ref run() or \ref findFlow() must be called before using |
464 | 469 |
/// this function. |
465 | 470 |
Length totalLength() const { |
466 | 471 |
Length c = 0; |
467 | 472 |
for (ArcIt e(_graph); e != INVALID; ++e) |
468 | 473 |
c += (*_flow)[e] * _length[e]; |
469 | 474 |
return c; |
470 | 475 |
} |
471 | 476 |
|
472 | 477 |
/// \brief Return the number of the found paths. |
473 | 478 |
/// |
474 | 479 |
/// This function returns the number of the found paths. |
475 | 480 |
/// |
476 | 481 |
/// \pre \ref run() or \ref findFlow() must be called before using |
477 | 482 |
/// this function. |
478 | 483 |
int pathNum() const { |
479 | 484 |
return _path_num; |
480 | 485 |
} |
481 | 486 |
|
482 | 487 |
/// \brief Return a const reference to the specified path. |
483 | 488 |
/// |
484 | 489 |
/// This function returns a const reference to the specified path. |
485 | 490 |
/// |
486 | 491 |
/// \param i The function returns the \c i-th path. |
487 | 492 |
/// \c i must be between \c 0 and <tt>%pathNum()-1</tt>. |
488 | 493 |
/// |
489 | 494 |
/// \pre \ref run() or \ref findPaths() must be called before using |
490 | 495 |
/// this function. |
491 | 496 |
Path path(int i) const { |
492 | 497 |
return paths[i]; |
493 | 498 |
} |
494 | 499 |
|
495 | 500 |
/// @} |
496 | 501 |
|
497 | 502 |
}; //class Suurballe |
498 | 503 |
|
499 | 504 |
///@} |
500 | 505 |
|
501 | 506 |
} //namespace lemon |
502 | 507 |
|
503 | 508 |
#endif //LEMON_SUURBALLE_H |
... | ... |
@@ -6,101 +6,103 @@ |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_UNION_FIND_H |
20 | 20 |
#define LEMON_UNION_FIND_H |
21 | 21 |
|
22 | 22 |
//!\ingroup auxdat |
23 | 23 |
//!\file |
24 | 24 |
//!\brief Union-Find data structures. |
25 | 25 |
//! |
26 | 26 |
|
27 | 27 |
#include <vector> |
28 | 28 |
#include <list> |
29 | 29 |
#include <utility> |
30 | 30 |
#include <algorithm> |
31 | 31 |
#include <functional> |
32 | 32 |
|
33 | 33 |
#include <lemon/core.h> |
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
/// \ingroup auxdat |
38 | 38 |
/// |
39 | 39 |
/// \brief A \e Union-Find data structure implementation |
40 | 40 |
/// |
41 | 41 |
/// The class implements the \e Union-Find data structure. |
42 | 42 |
/// The union operation uses rank heuristic, while |
43 | 43 |
/// the find operation uses path compression. |
44 | 44 |
/// This is a very simple but efficient implementation, providing |
45 | 45 |
/// only four methods: join (union), find, insert and size. |
46 | 46 |
/// For more features see the \ref UnionFindEnum class. |
47 | 47 |
/// |
48 | 48 |
/// It is primarily used in Kruskal algorithm for finding minimal |
49 | 49 |
/// cost spanning tree in a graph. |
50 | 50 |
/// \sa kruskal() |
51 | 51 |
/// |
52 | 52 |
/// \pre You need to add all the elements by the \ref insert() |
53 | 53 |
/// method. |
54 |
template <typename |
|
54 |
template <typename IM> |
|
55 | 55 |
class UnionFind { |
56 | 56 |
public: |
57 | 57 |
|
58 |
|
|
58 |
///\e |
|
59 |
typedef IM ItemIntMap; |
|
60 |
///\e |
|
59 | 61 |
typedef typename ItemIntMap::Key Item; |
60 | 62 |
|
61 | 63 |
private: |
62 | 64 |
// If the items vector stores negative value for an item then |
63 | 65 |
// that item is root item and it has -items[it] component size. |
64 | 66 |
// Else the items[it] contains the index of the parent. |
65 | 67 |
std::vector<int> items; |
66 | 68 |
ItemIntMap& index; |
67 | 69 |
|
68 | 70 |
bool rep(int idx) const { |
69 | 71 |
return items[idx] < 0; |
70 | 72 |
} |
71 | 73 |
|
72 | 74 |
int repIndex(int idx) const { |
73 | 75 |
int k = idx; |
74 | 76 |
while (!rep(k)) { |
75 | 77 |
k = items[k] ; |
76 | 78 |
} |
77 | 79 |
while (idx != k) { |
78 | 80 |
int next = items[idx]; |
79 | 81 |
const_cast<int&>(items[idx]) = k; |
80 | 82 |
idx = next; |
81 | 83 |
} |
82 | 84 |
return k; |
83 | 85 |
} |
84 | 86 |
|
85 | 87 |
public: |
86 | 88 |
|
87 | 89 |
/// \brief Constructor |
88 | 90 |
/// |
89 | 91 |
/// Constructor of the UnionFind class. You should give an item to |
90 | 92 |
/// integer map which will be used from the data structure. If you |
91 | 93 |
/// modify directly this map that may cause segmentation fault, |
92 | 94 |
/// invalid data structure, or infinite loop when you use again |
93 | 95 |
/// the union-find. |
94 | 96 |
UnionFind(ItemIntMap& m) : index(m) {} |
95 | 97 |
|
96 | 98 |
/// \brief Returns the index of the element's component. |
97 | 99 |
/// |
98 | 100 |
/// The method returns the index of the element's component. |
99 | 101 |
/// This is an integer between zero and the number of inserted elements. |
100 | 102 |
/// |
101 | 103 |
int find(const Item& a) { |
102 | 104 |
return repIndex(index[a]); |
103 | 105 |
} |
104 | 106 |
|
105 | 107 |
/// \brief Clears the union-find data structure |
106 | 108 |
/// |
... | ... |
@@ -125,101 +127,103 @@ |
125 | 127 |
/// |
126 | 128 |
/// This is the \e union operation of the Union-Find structure. |
127 | 129 |
/// Joins the component of element \e a and component of |
128 | 130 |
/// element \e b. If \e a and \e b are in the same component then |
129 | 131 |
/// it returns false otherwise it returns true. |
130 | 132 |
bool join(const Item& a, const Item& b) { |
131 | 133 |
int ka = repIndex(index[a]); |
132 | 134 |
int kb = repIndex(index[b]); |
133 | 135 |
|
134 | 136 |
if ( ka == kb ) |
135 | 137 |
return false; |
136 | 138 |
|
137 | 139 |
if (items[ka] < items[kb]) { |
138 | 140 |
items[ka] += items[kb]; |
139 | 141 |
items[kb] = ka; |
140 | 142 |
} else { |
141 | 143 |
items[kb] += items[ka]; |
142 | 144 |
items[ka] = kb; |
143 | 145 |
} |
144 | 146 |
return true; |
145 | 147 |
} |
146 | 148 |
|
147 | 149 |
/// \brief Returns the size of the component of element \e a. |
148 | 150 |
/// |
149 | 151 |
/// Returns the size of the component of element \e a. |
150 | 152 |
int size(const Item& a) { |
151 | 153 |
int k = repIndex(index[a]); |
152 | 154 |
return - items[k]; |
153 | 155 |
} |
154 | 156 |
|
155 | 157 |
}; |
156 | 158 |
|
157 | 159 |
/// \ingroup auxdat |
158 | 160 |
/// |
159 | 161 |
/// \brief A \e Union-Find data structure implementation which |
160 | 162 |
/// is able to enumerate the components. |
161 | 163 |
/// |
162 | 164 |
/// The class implements a \e Union-Find data structure |
163 | 165 |
/// which is able to enumerate the components and the items in |
164 | 166 |
/// a component. If you don't need this feature then perhaps it's |
165 | 167 |
/// better to use the \ref UnionFind class which is more efficient. |
166 | 168 |
/// |
167 | 169 |
/// The union operation uses rank heuristic, while |
168 | 170 |
/// the find operation uses path compression. |
169 | 171 |
/// |
170 | 172 |
/// \pre You need to add all the elements by the \ref insert() |
171 | 173 |
/// method. |
172 | 174 |
/// |
173 |
template <typename |
|
175 |
template <typename IM> |
|
174 | 176 |
class UnionFindEnum { |
175 | 177 |
public: |
176 | 178 |
|
177 |
|
|
179 |
///\e |
|
180 |
typedef IM ItemIntMap; |
|
181 |
///\e |
|
178 | 182 |
typedef typename ItemIntMap::Key Item; |
179 | 183 |
|
180 | 184 |
private: |
181 | 185 |
|
182 | 186 |
ItemIntMap& index; |
183 | 187 |
|
184 | 188 |
// If the parent stores negative value for an item then that item |
185 | 189 |
// is root item and it has ~(items[it].parent) component id. Else |
186 | 190 |
// the items[it].parent contains the index of the parent. |
187 | 191 |
// |
188 | 192 |
// The \c next and \c prev provides the double-linked |
189 | 193 |
// cyclic list of one component's items. |
190 | 194 |
struct ItemT { |
191 | 195 |
int parent; |
192 | 196 |
Item item; |
193 | 197 |
|
194 | 198 |
int next, prev; |
195 | 199 |
}; |
196 | 200 |
|
197 | 201 |
std::vector<ItemT> items; |
198 | 202 |
int firstFreeItem; |
199 | 203 |
|
200 | 204 |
struct ClassT { |
201 | 205 |
int size; |
202 | 206 |
int firstItem; |
203 | 207 |
int next, prev; |
204 | 208 |
}; |
205 | 209 |
|
206 | 210 |
std::vector<ClassT> classes; |
207 | 211 |
int firstClass, firstFreeClass; |
208 | 212 |
|
209 | 213 |
int newClass() { |
210 | 214 |
if (firstFreeClass == -1) { |
211 | 215 |
int cdx = classes.size(); |
212 | 216 |
classes.push_back(ClassT()); |
213 | 217 |
return cdx; |
214 | 218 |
} else { |
215 | 219 |
int cdx = firstFreeClass; |
216 | 220 |
firstFreeClass = classes[firstFreeClass].next; |
217 | 221 |
return cdx; |
218 | 222 |
} |
219 | 223 |
} |
220 | 224 |
|
221 | 225 |
int newItem() { |
222 | 226 |
if (firstFreeItem == -1) { |
223 | 227 |
int idx = items.size(); |
224 | 228 |
items.push_back(ItemT()); |
225 | 229 |
return idx; |
... | ... |
@@ -582,101 +586,103 @@ |
582 | 586 |
/// |
583 | 587 |
/// It steps to the next item in the class. |
584 | 588 |
ItemIt& operator++() { |
585 | 589 |
idx = unionFind->items[idx].next; |
586 | 590 |
if (idx == fdx) idx = -1; |
587 | 591 |
return *this; |
588 | 592 |
} |
589 | 593 |
|
590 | 594 |
/// \brief Conversion operator |
591 | 595 |
/// |
592 | 596 |
/// It converts the iterator to the current item. |
593 | 597 |
operator const Item&() const { |
594 | 598 |
return unionFind->items[idx].item; |
595 | 599 |
} |
596 | 600 |
|
597 | 601 |
/// \brief Equality operator |
598 | 602 |
/// |
599 | 603 |
/// Equality operator |
600 | 604 |
bool operator==(const ItemIt& i) { |
601 | 605 |
return i.idx == idx; |
602 | 606 |
} |
603 | 607 |
|
604 | 608 |
/// \brief Inequality operator |
605 | 609 |
/// |
606 | 610 |
/// Inequality operator |
607 | 611 |
bool operator!=(const ItemIt& i) { |
608 | 612 |
return i.idx != idx; |
609 | 613 |
} |
610 | 614 |
|
611 | 615 |
private: |
612 | 616 |
const UnionFindEnum* unionFind; |
613 | 617 |
int idx, fdx; |
614 | 618 |
}; |
615 | 619 |
|
616 | 620 |
}; |
617 | 621 |
|
618 | 622 |
/// \ingroup auxdat |
619 | 623 |
/// |
620 | 624 |
/// \brief A \e Extend-Find data structure implementation which |
621 | 625 |
/// is able to enumerate the components. |
622 | 626 |
/// |
623 | 627 |
/// The class implements an \e Extend-Find data structure which is |
624 | 628 |
/// able to enumerate the components and the items in a |
625 | 629 |
/// component. The data structure is a simplification of the |
626 | 630 |
/// Union-Find structure, and it does not allow to merge two components. |
627 | 631 |
/// |
628 | 632 |
/// \pre You need to add all the elements by the \ref insert() |
629 | 633 |
/// method. |
630 |
template <typename |
|
634 |
template <typename IM> |
|
631 | 635 |
class ExtendFindEnum { |
632 | 636 |
public: |
633 | 637 |
|
634 |
|
|
638 |
///\e |
|
639 |
typedef IM ItemIntMap; |
|
640 |
///\e |
|
635 | 641 |
typedef typename ItemIntMap::Key Item; |
636 | 642 |
|
637 | 643 |
private: |
638 | 644 |
|
639 | 645 |
ItemIntMap& index; |
640 | 646 |
|
641 | 647 |
struct ItemT { |
642 | 648 |
int cls; |
643 | 649 |
Item item; |
644 | 650 |
int next, prev; |
645 | 651 |
}; |
646 | 652 |
|
647 | 653 |
std::vector<ItemT> items; |
648 | 654 |
int firstFreeItem; |
649 | 655 |
|
650 | 656 |
struct ClassT { |
651 | 657 |
int firstItem; |
652 | 658 |
int next, prev; |
653 | 659 |
}; |
654 | 660 |
|
655 | 661 |
std::vector<ClassT> classes; |
656 | 662 |
|
657 | 663 |
int firstClass, firstFreeClass; |
658 | 664 |
|
659 | 665 |
int newClass() { |
660 | 666 |
if (firstFreeClass != -1) { |
661 | 667 |
int cdx = firstFreeClass; |
662 | 668 |
firstFreeClass = classes[cdx].next; |
663 | 669 |
return cdx; |
664 | 670 |
} else { |
665 | 671 |
classes.push_back(ClassT()); |
666 | 672 |
return classes.size() - 1; |
667 | 673 |
} |
668 | 674 |
} |
669 | 675 |
|
670 | 676 |
int newItem() { |
671 | 677 |
if (firstFreeItem != -1) { |
672 | 678 |
int idx = firstFreeItem; |
673 | 679 |
firstFreeItem = items[idx].next; |
674 | 680 |
return idx; |
675 | 681 |
} else { |
676 | 682 |
items.push_back(ItemT()); |
677 | 683 |
return items.size() - 1; |
678 | 684 |
} |
679 | 685 |
} |
680 | 686 |
|
681 | 687 |
public: |
682 | 688 |
|
... | ... |
@@ -903,108 +909,108 @@ |
903 | 909 |
operator const Item&() const { |
904 | 910 |
return extendFind->items[idx].item; |
905 | 911 |
} |
906 | 912 |
|
907 | 913 |
/// \brief Equality operator |
908 | 914 |
/// |
909 | 915 |
/// Equality operator |
910 | 916 |
bool operator==(const ItemIt& i) { |
911 | 917 |
return i.idx == idx; |
912 | 918 |
} |
913 | 919 |
|
914 | 920 |
/// \brief Inequality operator |
915 | 921 |
/// |
916 | 922 |
/// Inequality operator |
917 | 923 |
bool operator!=(const ItemIt& i) { |
918 | 924 |
return i.idx != idx; |
919 | 925 |
} |
920 | 926 |
|
921 | 927 |
private: |
922 | 928 |
const ExtendFindEnum* extendFind; |
923 | 929 |
int idx, fdx; |
924 | 930 |
}; |
925 | 931 |
|
926 | 932 |
}; |
927 | 933 |
|
928 | 934 |
/// \ingroup auxdat |
929 | 935 |
/// |
930 | 936 |
/// \brief A \e Union-Find data structure implementation which |
931 | 937 |
/// is able to store a priority for each item and retrieve the minimum of |
932 | 938 |
/// each class. |
933 | 939 |
/// |
934 | 940 |
/// A \e Union-Find data structure implementation which is able to |
935 | 941 |
/// store a priority for each item and retrieve the minimum of each |
936 | 942 |
/// class. In addition, it supports the joining and splitting the |
937 | 943 |
/// components. If you don't need this feature then you makes |
938 | 944 |
/// better to use the \ref UnionFind class which is more efficient. |
939 | 945 |
/// |
940 | 946 |
/// The union-find data strcuture based on a (2, 16)-tree with a |
941 | 947 |
/// tournament minimum selection on the internal nodes. The insert |
942 | 948 |
/// operation takes O(1), the find, set, decrease and increase takes |
943 | 949 |
/// O(log(n)), where n is the number of nodes in the current |
944 | 950 |
/// component. The complexity of join and split is O(log(n)*k), |
945 | 951 |
/// where n is the sum of the number of the nodes and k is the |
946 | 952 |
/// number of joined components or the number of the components |
947 | 953 |
/// after the split. |
948 | 954 |
/// |
949 | 955 |
/// \pre You need to add all the elements by the \ref insert() |
950 | 956 |
/// method. |
951 |
/// |
|
952 |
template <typename _Value, typename _ItemIntMap, |
|
953 |
|
|
957 |
template <typename V, typename IM, typename Comp = std::less<V> > |
|
954 | 958 |
class HeapUnionFind { |
955 | 959 |
public: |
956 | 960 |
|
957 |
typedef _Value Value; |
|
958 |
typedef typename _ItemIntMap::Key Item; |
|
959 |
|
|
960 |
typedef _ItemIntMap ItemIntMap; |
|
961 |
|
|
962 |
typedef _Comp Comp; |
|
961 |
///\e |
|
962 |
typedef V Value; |
|
963 |
///\e |
|
964 |
typedef typename IM::Key Item; |
|
965 |
///\e |
|
966 |
typedef IM ItemIntMap; |
|
967 |
///\e |
|
968 |
typedef Comp Compare; |
|
963 | 969 |
|
964 | 970 |
private: |
965 | 971 |
|
966 | 972 |
static const int cmax = 16; |
967 | 973 |
|
968 | 974 |
ItemIntMap& index; |
969 | 975 |
|
970 | 976 |
struct ClassNode { |
971 | 977 |
int parent; |
972 | 978 |
int depth; |
973 | 979 |
|
974 | 980 |
int left, right; |
975 | 981 |
int next, prev; |
976 | 982 |
}; |
977 | 983 |
|
978 | 984 |
int first_class; |
979 | 985 |
int first_free_class; |
980 | 986 |
std::vector<ClassNode> classes; |
981 | 987 |
|
982 | 988 |
int newClass() { |
983 | 989 |
if (first_free_class < 0) { |
984 | 990 |
int id = classes.size(); |
985 | 991 |
classes.push_back(ClassNode()); |
986 | 992 |
return id; |
987 | 993 |
} else { |
988 | 994 |
int id = first_free_class; |
989 | 995 |
first_free_class = classes[id].next; |
990 | 996 |
return id; |
991 | 997 |
} |
992 | 998 |
} |
993 | 999 |
|
994 | 1000 |
void deleteClass(int id) { |
995 | 1001 |
classes[id].next = first_free_class; |
996 | 1002 |
first_free_class = id; |
997 | 1003 |
} |
998 | 1004 |
|
999 | 1005 |
struct ItemNode { |
1000 | 1006 |
int parent; |
1001 | 1007 |
Item item; |
1002 | 1008 |
Value prio; |
1003 | 1009 |
int next, prev; |
1004 | 1010 |
int left, right; |
1005 | 1011 |
int size; |
1006 | 1012 |
}; |
1007 | 1013 |
|
1008 | 1014 |
int first_free_node; |
1009 | 1015 |
std::vector<ItemNode> nodes; |
1010 | 1016 |
|
... | ... |
@@ -1556,158 +1562,158 @@ |
1556 | 1562 |
classes[first_class].prev = classes[id].right; |
1557 | 1563 |
first_class = classes[id].left; |
1558 | 1564 |
|
1559 | 1565 |
if (classes[id].next != -1) { |
1560 | 1566 |
classes[classes[id].next].prev = classes[id].prev; |
1561 | 1567 |
} |
1562 | 1568 |
classes[classes[id].prev].next = classes[id].next; |
1563 | 1569 |
|
1564 | 1570 |
deleteClass(id); |
1565 | 1571 |
} |
1566 | 1572 |
|
1567 | 1573 |
{ |
1568 | 1574 |
for (int i = 1; i < int(cs.size()); ++i) { |
1569 | 1575 |
int l = classes[cs[i]].depth; |
1570 | 1576 |
while (nodes[nodes[l].parent].left == l) { |
1571 | 1577 |
l = nodes[l].parent; |
1572 | 1578 |
} |
1573 | 1579 |
int r = l; |
1574 | 1580 |
while (nodes[l].parent >= 0) { |
1575 | 1581 |
l = nodes[l].parent; |
1576 | 1582 |
int new_node = newNode(); |
1577 | 1583 |
|
1578 | 1584 |
nodes[new_node].prev = -1; |
1579 | 1585 |
nodes[new_node].next = -1; |
1580 | 1586 |
|
1581 | 1587 |
split(r, new_node); |
1582 | 1588 |
pushAfter(l, new_node); |
1583 | 1589 |
setPrio(l); |
1584 | 1590 |
setPrio(new_node); |
1585 | 1591 |
r = new_node; |
1586 | 1592 |
} |
1587 | 1593 |
classes[cs[i]].parent = ~r; |
1588 | 1594 |
classes[cs[i]].depth = classes[~(nodes[l].parent)].depth; |
1589 | 1595 |
nodes[r].parent = ~cs[i]; |
1590 | 1596 |
|
1591 | 1597 |
nodes[l].next = -1; |
1592 | 1598 |
nodes[r].prev = -1; |
1593 | 1599 |
|
1594 | 1600 |
repairRight(~(nodes[l].parent)); |
1595 | 1601 |
repairLeft(cs[i]); |
1596 | 1602 |
|
1597 | 1603 |
*out++ = cs[i]; |
1598 | 1604 |
} |
1599 | 1605 |
} |
1600 | 1606 |
} |
1601 | 1607 |
|
1602 | 1608 |
/// \brief Gives back the priority of the current item. |
1603 | 1609 |
/// |
1604 |
/// |
|
1610 |
/// Gives back the priority of the current item. |
|
1605 | 1611 |
const Value& operator[](const Item& item) const { |
1606 | 1612 |
return nodes[index[item]].prio; |
1607 | 1613 |
} |
1608 | 1614 |
|
1609 | 1615 |
/// \brief Sets the priority of the current item. |
1610 | 1616 |
/// |
1611 | 1617 |
/// Sets the priority of the current item. |
1612 | 1618 |
void set(const Item& item, const Value& prio) { |
1613 | 1619 |
if (comp(prio, nodes[index[item]].prio)) { |
1614 | 1620 |
decrease(item, prio); |
1615 | 1621 |
} else if (!comp(prio, nodes[index[item]].prio)) { |
1616 | 1622 |
increase(item, prio); |
1617 | 1623 |
} |
1618 | 1624 |
} |
1619 | 1625 |
|
1620 | 1626 |
/// \brief Increase the priority of the current item. |
1621 | 1627 |
/// |
1622 | 1628 |
/// Increase the priority of the current item. |
1623 | 1629 |
void increase(const Item& item, const Value& prio) { |
1624 | 1630 |
int id = index[item]; |
1625 | 1631 |
int kd = nodes[id].parent; |
1626 | 1632 |
nodes[id].prio = prio; |
1627 | 1633 |
while (kd >= 0 && nodes[kd].item == item) { |
1628 | 1634 |
setPrio(kd); |
1629 | 1635 |
kd = nodes[kd].parent; |
1630 | 1636 |
} |
1631 | 1637 |
} |
1632 | 1638 |
|
1633 | 1639 |
/// \brief Increase the priority of the current item. |
1634 | 1640 |
/// |
1635 | 1641 |
/// Increase the priority of the current item. |
1636 | 1642 |
void decrease(const Item& item, const Value& prio) { |
1637 | 1643 |
int id = index[item]; |
1638 | 1644 |
int kd = nodes[id].parent; |
1639 | 1645 |
nodes[id].prio = prio; |
1640 | 1646 |
while (kd >= 0 && less(id, kd)) { |
1641 | 1647 |
nodes[kd].prio = prio; |
1642 | 1648 |
nodes[kd].item = item; |
1643 | 1649 |
kd = nodes[kd].parent; |
1644 | 1650 |
} |
1645 | 1651 |
} |
1646 | 1652 |
|
1647 | 1653 |
/// \brief Gives back the minimum priority of the class. |
1648 | 1654 |
/// |
1649 |
/// |
|
1655 |
/// Gives back the minimum priority of the class. |
|
1650 | 1656 |
const Value& classPrio(int cls) const { |
1651 | 1657 |
return nodes[~(classes[cls].parent)].prio; |
1652 | 1658 |
} |
1653 | 1659 |
|
1654 | 1660 |
/// \brief Gives back the minimum priority item of the class. |
1655 | 1661 |
/// |
1656 | 1662 |
/// \return Gives back the minimum priority item of the class. |
1657 | 1663 |
const Item& classTop(int cls) const { |
1658 | 1664 |
return nodes[~(classes[cls].parent)].item; |
1659 | 1665 |
} |
1660 | 1666 |
|
1661 | 1667 |
/// \brief Gives back a representant item of the class. |
1662 | 1668 |
/// |
1669 |
/// Gives back a representant item of the class. |
|
1663 | 1670 |
/// The representant is indpendent from the priorities of the |
1664 | 1671 |
/// items. |
1665 |
/// \return Gives back a representant item of the class. |
|
1666 | 1672 |
const Item& classRep(int id) const { |
1667 | 1673 |
int parent = classes[id].parent; |
1668 | 1674 |
return nodes[parent >= 0 ? classes[id].depth : leftNode(id)].item; |
1669 | 1675 |
} |
1670 | 1676 |
|
1671 | 1677 |
/// \brief LEMON style iterator for the items of a class. |
1672 | 1678 |
/// |
1673 | 1679 |
/// ClassIt is a lemon style iterator for the components. It iterates |
1674 | 1680 |
/// on the items of a class. By example if you want to iterate on |
1675 | 1681 |
/// each items of each classes then you may write the next code. |
1676 | 1682 |
///\code |
1677 | 1683 |
/// for (ClassIt cit(huf); cit != INVALID; ++cit) { |
1678 | 1684 |
/// std::cout << "Class: "; |
1679 | 1685 |
/// for (ItemIt iit(huf, cit); iit != INVALID; ++iit) { |
1680 | 1686 |
/// std::cout << toString(iit) << ' ' << std::endl; |
1681 | 1687 |
/// } |
1682 | 1688 |
/// std::cout << std::endl; |
1683 | 1689 |
/// } |
1684 | 1690 |
///\endcode |
1685 | 1691 |
class ItemIt { |
1686 | 1692 |
private: |
1687 | 1693 |
|
1688 | 1694 |
const HeapUnionFind* _huf; |
1689 | 1695 |
int _id, _lid; |
1690 | 1696 |
|
1691 | 1697 |
public: |
1692 | 1698 |
|
1693 | 1699 |
/// \brief Default constructor |
1694 | 1700 |
/// |
1695 | 1701 |
/// Default constructor |
1696 | 1702 |
ItemIt() {} |
1697 | 1703 |
|
1698 | 1704 |
ItemIt(const HeapUnionFind& huf, int cls) : _huf(&huf) { |
1699 | 1705 |
int id = cls; |
1700 | 1706 |
int parent = _huf->classes[id].parent; |
1701 | 1707 |
if (parent >= 0) { |
1702 | 1708 |
_id = _huf->classes[id].depth; |
1703 | 1709 |
if (_huf->classes[id].next != -1) { |
1704 | 1710 |
_lid = _huf->classes[_huf->classes[id].next].depth; |
1705 | 1711 |
} else { |
1706 | 1712 |
_lid = -1; |
1707 | 1713 |
} |
1708 | 1714 |
} else { |
1709 | 1715 |
_id = _huf->leftNode(id); |
1710 | 1716 |
_lid = -1; |
1711 | 1717 |
} |
1712 | 1718 |
} |
1713 | 1719 |
|
0 comments (0 inline)