0
33
0
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 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 |
72 | 72 |
graph algorithms, graph concepts, which couple them, and graph |
73 | 73 |
adaptors. While the previous notions are more or less clear, the |
74 | 74 |
latter one needs further explanation. Graph adaptors are graph classes |
75 | 75 |
which serve for considering graph structures in different ways. |
76 | 76 |
|
77 | 77 |
A short example makes this much clearer. Suppose that we have an |
78 | 78 |
instance \c g of a directed graph type, say ListDigraph and an algorithm |
79 | 79 |
\code |
80 | 80 |
template <typename Digraph> |
81 | 81 |
int algorithm(const Digraph&); |
82 | 82 |
\endcode |
83 | 83 |
is needed to run on the reverse oriented graph. It may be expensive |
84 | 84 |
(in time or in memory usage) to copy \c g with the reversed |
85 | 85 |
arcs. In this case, an adaptor class is used, which (according |
86 | 86 |
to LEMON \ref concepts::Digraph "digraph concepts") works as a digraph. |
87 | 87 |
The adaptor uses the original digraph structure and digraph operations when |
88 | 88 |
methods of the reversed oriented graph are called. This means that the adaptor |
89 | 89 |
have minor memory usage, and do not perform sophisticated algorithmic |
90 | 90 |
actions. The purpose of it is to give a tool for the cases when a |
91 | 91 |
graph have to be used in a specific alteration. If this alteration is |
92 | 92 |
obtained by a usual construction like filtering the node or the arc set or |
93 | 93 |
considering a new orientation, then an adaptor is worthwhile to use. |
94 | 94 |
To come back to the reverse oriented graph, in this situation |
95 | 95 |
\code |
96 | 96 |
template<typename Digraph> class ReverseDigraph; |
97 | 97 |
\endcode |
98 | 98 |
template class can be used. The code looks as follows |
99 | 99 |
\code |
100 | 100 |
ListDigraph g; |
101 | 101 |
ReverseDigraph<ListDigraph> rg(g); |
102 | 102 |
int result = algorithm(rg); |
103 | 103 |
\endcode |
104 | 104 |
During running the algorithm, the original digraph \c g is untouched. |
105 | 105 |
This techniques give rise to an elegant code, and based on stable |
106 | 106 |
graph adaptors, complex algorithms can be implemented easily. |
107 | 107 |
|
108 | 108 |
In flow, circulation and matching problems, the residual |
109 | 109 |
graph is of particular importance. Combining an adaptor implementing |
110 | 110 |
this with shortest path algorithms or minimum mean cycle algorithms, |
111 | 111 |
a range of weighted and cardinality optimization algorithms can be |
112 | 112 |
obtained. For other examples, the interested user is referred to the |
113 | 113 |
detailed documentation of particular adaptors. |
114 | 114 |
|
115 | 115 |
The behavior of graph adaptors can be very different. Some of them keep |
116 | 116 |
capabilities of the original graph while in other cases this would be |
117 | 117 |
meaningless. This means that the concepts that they meet depend |
118 | 118 |
on the graph adaptor, and the wrapped graph. |
119 | 119 |
For example, if an arc of a reversed digraph is deleted, this is carried |
120 | 120 |
out by deleting the corresponding arc of the original digraph, thus the |
121 | 121 |
adaptor modifies the original digraph. |
122 | 122 |
However in case of a residual digraph, this operation has no sense. |
123 | 123 |
|
124 | 124 |
Let us stand one more example here to simplify your work. |
125 | 125 |
ReverseDigraph has constructor |
126 | 126 |
\code |
127 | 127 |
ReverseDigraph(Digraph& digraph); |
128 | 128 |
\endcode |
129 | 129 |
This means that in a situation, when a <tt>const %ListDigraph&</tt> |
130 | 130 |
reference to a graph is given, then it have to be instantiated with |
131 | 131 |
<tt>Digraph=const %ListDigraph</tt>. |
132 | 132 |
\code |
133 | 133 |
int algorithm1(const ListDigraph& g) { |
134 | 134 |
ReverseDigraph<const ListDigraph> rg(g); |
135 | 135 |
return algorithm2(rg); |
136 | 136 |
} |
137 | 137 |
\endcode |
138 | 138 |
*/ |
139 | 139 |
|
140 | 140 |
/** |
141 | 141 |
@defgroup 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 |
*/ |
... | ... |
@@ -721,2875 +721,2876 @@ |
721 | 721 |
/// It can also be specified to be \c const. |
722 | 722 |
/// \tparam NF The type of the node filter map. |
723 | 723 |
/// It must be a \c bool (or convertible) node map of the |
724 | 724 |
/// adapted digraph. The default type is |
725 | 725 |
/// \ref concepts::Digraph::NodeMap "DGR::NodeMap<bool>". |
726 | 726 |
/// \tparam AF The type of the arc filter map. |
727 | 727 |
/// It must be \c bool (or convertible) arc map of the |
728 | 728 |
/// adapted digraph. The default type is |
729 | 729 |
/// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>". |
730 | 730 |
/// |
731 | 731 |
/// \note The \c Node and \c Arc types of this adaptor and the adapted |
732 | 732 |
/// digraph are convertible to each other. |
733 | 733 |
/// |
734 | 734 |
/// \see FilterNodes |
735 | 735 |
/// \see FilterArcs |
736 | 736 |
#ifdef DOXYGEN |
737 | 737 |
template<typename DGR, typename NF, typename AF> |
738 | 738 |
class SubDigraph { |
739 | 739 |
#else |
740 | 740 |
template<typename DGR, |
741 | 741 |
typename NF = typename DGR::template NodeMap<bool>, |
742 | 742 |
typename AF = typename DGR::template ArcMap<bool> > |
743 | 743 |
class SubDigraph : |
744 | 744 |
public DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> > { |
745 | 745 |
#endif |
746 | 746 |
public: |
747 | 747 |
/// The type of the adapted digraph. |
748 | 748 |
typedef DGR Digraph; |
749 | 749 |
/// The type of the node filter map. |
750 | 750 |
typedef NF NodeFilterMap; |
751 | 751 |
/// The type of the arc filter map. |
752 | 752 |
typedef AF ArcFilterMap; |
753 | 753 |
|
754 | 754 |
typedef DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> > |
755 | 755 |
Parent; |
756 | 756 |
|
757 | 757 |
typedef typename Parent::Node Node; |
758 | 758 |
typedef typename Parent::Arc Arc; |
759 | 759 |
|
760 | 760 |
protected: |
761 | 761 |
SubDigraph() { } |
762 | 762 |
public: |
763 | 763 |
|
764 | 764 |
/// \brief Constructor |
765 | 765 |
/// |
766 | 766 |
/// Creates a subdigraph for the given digraph with the |
767 | 767 |
/// given node and arc filter maps. |
768 | 768 |
SubDigraph(DGR& digraph, NF& node_filter, AF& arc_filter) { |
769 | 769 |
Parent::initialize(digraph, node_filter, arc_filter); |
770 | 770 |
} |
771 | 771 |
|
772 | 772 |
/// \brief Sets the status of the given node |
773 | 773 |
/// |
774 | 774 |
/// This function sets the status of the given node. |
775 | 775 |
/// It is done by simply setting the assigned value of \c n |
776 | 776 |
/// to \c v in the node filter map. |
777 | 777 |
void status(const Node& n, bool v) const { Parent::status(n, v); } |
778 | 778 |
|
779 | 779 |
/// \brief Sets the status of the given arc |
780 | 780 |
/// |
781 | 781 |
/// This function sets the status of the given arc. |
782 | 782 |
/// It is done by simply setting the assigned value of \c a |
783 | 783 |
/// to \c v in the arc filter map. |
784 | 784 |
void status(const Arc& a, bool v) const { Parent::status(a, v); } |
785 | 785 |
|
786 | 786 |
/// \brief Returns the status of the given node |
787 | 787 |
/// |
788 | 788 |
/// This function returns the status of the given node. |
789 | 789 |
/// It is \c true if the given node is enabled (i.e. not hidden). |
790 | 790 |
bool status(const Node& n) const { return Parent::status(n); } |
791 | 791 |
|
792 | 792 |
/// \brief Returns the status of the given arc |
793 | 793 |
/// |
794 | 794 |
/// This function returns the status of the given arc. |
795 | 795 |
/// It is \c true if the given arc is enabled (i.e. not hidden). |
796 | 796 |
bool status(const Arc& a) const { return Parent::status(a); } |
797 | 797 |
|
798 | 798 |
/// \brief Disables the given node |
799 | 799 |
/// |
800 | 800 |
/// This function disables the given node in the subdigraph, |
801 | 801 |
/// so the iteration jumps over it. |
802 | 802 |
/// It is the same as \ref status() "status(n, false)". |
803 | 803 |
void disable(const Node& n) const { Parent::status(n, false); } |
804 | 804 |
|
805 | 805 |
/// \brief Disables the given arc |
806 | 806 |
/// |
807 | 807 |
/// This function disables the given arc in the subdigraph, |
808 | 808 |
/// so the iteration jumps over it. |
809 | 809 |
/// It is the same as \ref status() "status(a, false)". |
810 | 810 |
void disable(const Arc& a) const { Parent::status(a, false); } |
811 | 811 |
|
812 | 812 |
/// \brief Enables the given node |
813 | 813 |
/// |
814 | 814 |
/// This function enables the given node in the subdigraph. |
815 | 815 |
/// It is the same as \ref status() "status(n, true)". |
816 | 816 |
void enable(const Node& n) const { Parent::status(n, true); } |
817 | 817 |
|
818 | 818 |
/// \brief Enables the given arc |
819 | 819 |
/// |
820 | 820 |
/// This function enables the given arc in the subdigraph. |
821 | 821 |
/// It is the same as \ref status() "status(a, true)". |
822 | 822 |
void enable(const Arc& a) const { Parent::status(a, true); } |
823 | 823 |
|
824 | 824 |
}; |
825 | 825 |
|
826 | 826 |
/// \brief Returns a read-only SubDigraph adaptor |
827 | 827 |
/// |
828 | 828 |
/// This function just returns a read-only \ref SubDigraph adaptor. |
829 | 829 |
/// \ingroup graph_adaptors |
830 | 830 |
/// \relates SubDigraph |
831 | 831 |
template<typename DGR, typename NF, typename AF> |
832 | 832 |
SubDigraph<const DGR, NF, AF> |
833 | 833 |
subDigraph(const DGR& digraph, |
834 | 834 |
NF& node_filter, AF& arc_filter) { |
835 | 835 |
return SubDigraph<const DGR, NF, AF> |
836 | 836 |
(digraph, node_filter, arc_filter); |
837 | 837 |
} |
838 | 838 |
|
839 | 839 |
template<typename DGR, typename NF, typename AF> |
840 | 840 |
SubDigraph<const DGR, const NF, AF> |
841 | 841 |
subDigraph(const DGR& digraph, |
842 | 842 |
const NF& node_filter, AF& arc_filter) { |
843 | 843 |
return SubDigraph<const DGR, const NF, AF> |
844 | 844 |
(digraph, node_filter, arc_filter); |
845 | 845 |
} |
846 | 846 |
|
847 | 847 |
template<typename DGR, typename NF, typename AF> |
848 | 848 |
SubDigraph<const DGR, NF, const AF> |
849 | 849 |
subDigraph(const DGR& digraph, |
850 | 850 |
NF& node_filter, const AF& arc_filter) { |
851 | 851 |
return SubDigraph<const DGR, NF, const AF> |
852 | 852 |
(digraph, node_filter, arc_filter); |
853 | 853 |
} |
854 | 854 |
|
855 | 855 |
template<typename DGR, typename NF, typename AF> |
856 | 856 |
SubDigraph<const DGR, const NF, const AF> |
857 | 857 |
subDigraph(const DGR& digraph, |
858 | 858 |
const NF& node_filter, const AF& arc_filter) { |
859 | 859 |
return SubDigraph<const DGR, const NF, const AF> |
860 | 860 |
(digraph, node_filter, arc_filter); |
861 | 861 |
} |
862 | 862 |
|
863 | 863 |
|
864 | 864 |
template <typename GR, typename NF, typename EF, bool ch = true> |
865 | 865 |
class SubGraphBase : public GraphAdaptorBase<GR> { |
866 | 866 |
public: |
867 | 867 |
typedef GR Graph; |
868 | 868 |
typedef NF NodeFilterMap; |
869 | 869 |
typedef EF EdgeFilterMap; |
870 | 870 |
|
871 | 871 |
typedef SubGraphBase Adaptor; |
872 | 872 |
typedef GraphAdaptorBase<GR> Parent; |
873 | 873 |
protected: |
874 | 874 |
|
875 | 875 |
NF* _node_filter; |
876 | 876 |
EF* _edge_filter; |
877 | 877 |
|
878 | 878 |
SubGraphBase() |
879 | 879 |
: Parent(), _node_filter(0), _edge_filter(0) { } |
880 | 880 |
|
881 | 881 |
void initialize(GR& graph, NF& node_filter, EF& edge_filter) { |
882 | 882 |
Parent::initialize(graph); |
883 | 883 |
_node_filter = &node_filter; |
884 | 884 |
_edge_filter = &edge_filter; |
885 | 885 |
} |
886 | 886 |
|
887 | 887 |
public: |
888 | 888 |
|
889 | 889 |
typedef typename Parent::Node Node; |
890 | 890 |
typedef typename Parent::Arc Arc; |
891 | 891 |
typedef typename Parent::Edge Edge; |
892 | 892 |
|
893 | 893 |
void first(Node& i) const { |
894 | 894 |
Parent::first(i); |
895 | 895 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
896 | 896 |
} |
897 | 897 |
|
898 | 898 |
void first(Arc& i) const { |
899 | 899 |
Parent::first(i); |
900 | 900 |
while (i!=INVALID && (!(*_edge_filter)[i] |
901 | 901 |
|| !(*_node_filter)[Parent::source(i)] |
902 | 902 |
|| !(*_node_filter)[Parent::target(i)])) |
903 | 903 |
Parent::next(i); |
904 | 904 |
} |
905 | 905 |
|
906 | 906 |
void first(Edge& i) const { |
907 | 907 |
Parent::first(i); |
908 | 908 |
while (i!=INVALID && (!(*_edge_filter)[i] |
909 | 909 |
|| !(*_node_filter)[Parent::u(i)] |
910 | 910 |
|| !(*_node_filter)[Parent::v(i)])) |
911 | 911 |
Parent::next(i); |
912 | 912 |
} |
913 | 913 |
|
914 | 914 |
void firstIn(Arc& i, const Node& n) const { |
915 | 915 |
Parent::firstIn(i, n); |
916 | 916 |
while (i!=INVALID && (!(*_edge_filter)[i] |
917 | 917 |
|| !(*_node_filter)[Parent::source(i)])) |
918 | 918 |
Parent::nextIn(i); |
919 | 919 |
} |
920 | 920 |
|
921 | 921 |
void firstOut(Arc& i, const Node& n) const { |
922 | 922 |
Parent::firstOut(i, n); |
923 | 923 |
while (i!=INVALID && (!(*_edge_filter)[i] |
924 | 924 |
|| !(*_node_filter)[Parent::target(i)])) |
925 | 925 |
Parent::nextOut(i); |
926 | 926 |
} |
927 | 927 |
|
928 | 928 |
void firstInc(Edge& i, bool& d, const Node& n) const { |
929 | 929 |
Parent::firstInc(i, d, n); |
930 | 930 |
while (i!=INVALID && (!(*_edge_filter)[i] |
931 | 931 |
|| !(*_node_filter)[Parent::u(i)] |
932 | 932 |
|| !(*_node_filter)[Parent::v(i)])) |
933 | 933 |
Parent::nextInc(i, d); |
934 | 934 |
} |
935 | 935 |
|
936 | 936 |
void next(Node& i) const { |
937 | 937 |
Parent::next(i); |
938 | 938 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
939 | 939 |
} |
940 | 940 |
|
941 | 941 |
void next(Arc& i) const { |
942 | 942 |
Parent::next(i); |
943 | 943 |
while (i!=INVALID && (!(*_edge_filter)[i] |
944 | 944 |
|| !(*_node_filter)[Parent::source(i)] |
945 | 945 |
|| !(*_node_filter)[Parent::target(i)])) |
946 | 946 |
Parent::next(i); |
947 | 947 |
} |
948 | 948 |
|
949 | 949 |
void next(Edge& i) const { |
950 | 950 |
Parent::next(i); |
951 | 951 |
while (i!=INVALID && (!(*_edge_filter)[i] |
952 | 952 |
|| !(*_node_filter)[Parent::u(i)] |
953 | 953 |
|| !(*_node_filter)[Parent::v(i)])) |
954 | 954 |
Parent::next(i); |
955 | 955 |
} |
956 | 956 |
|
957 | 957 |
void nextIn(Arc& i) const { |
958 | 958 |
Parent::nextIn(i); |
959 | 959 |
while (i!=INVALID && (!(*_edge_filter)[i] |
960 | 960 |
|| !(*_node_filter)[Parent::source(i)])) |
961 | 961 |
Parent::nextIn(i); |
962 | 962 |
} |
963 | 963 |
|
964 | 964 |
void nextOut(Arc& i) const { |
965 | 965 |
Parent::nextOut(i); |
966 | 966 |
while (i!=INVALID && (!(*_edge_filter)[i] |
967 | 967 |
|| !(*_node_filter)[Parent::target(i)])) |
968 | 968 |
Parent::nextOut(i); |
969 | 969 |
} |
970 | 970 |
|
971 | 971 |
void nextInc(Edge& i, bool& d) const { |
972 | 972 |
Parent::nextInc(i, d); |
973 | 973 |
while (i!=INVALID && (!(*_edge_filter)[i] |
974 | 974 |
|| !(*_node_filter)[Parent::u(i)] |
975 | 975 |
|| !(*_node_filter)[Parent::v(i)])) |
976 | 976 |
Parent::nextInc(i, d); |
977 | 977 |
} |
978 | 978 |
|
979 | 979 |
void status(const Node& n, bool v) const { _node_filter->set(n, v); } |
980 | 980 |
void status(const Edge& e, bool v) const { _edge_filter->set(e, v); } |
981 | 981 |
|
982 | 982 |
bool status(const Node& n) const { return (*_node_filter)[n]; } |
983 | 983 |
bool status(const Edge& e) const { return (*_edge_filter)[e]; } |
984 | 984 |
|
985 | 985 |
typedef False NodeNumTag; |
986 | 986 |
typedef False ArcNumTag; |
987 | 987 |
typedef False EdgeNumTag; |
988 | 988 |
|
989 | 989 |
typedef FindArcTagIndicator<Graph> FindArcTag; |
990 | 990 |
Arc findArc(const Node& u, const Node& v, |
991 | 991 |
const Arc& prev = INVALID) const { |
992 | 992 |
if (!(*_node_filter)[u] || !(*_node_filter)[v]) { |
993 | 993 |
return INVALID; |
994 | 994 |
} |
995 | 995 |
Arc arc = Parent::findArc(u, v, prev); |
996 | 996 |
while (arc != INVALID && !(*_edge_filter)[arc]) { |
997 | 997 |
arc = Parent::findArc(u, v, arc); |
998 | 998 |
} |
999 | 999 |
return arc; |
1000 | 1000 |
} |
1001 | 1001 |
|
1002 | 1002 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
1003 | 1003 |
Edge findEdge(const Node& u, const Node& v, |
1004 | 1004 |
const Edge& prev = INVALID) const { |
1005 | 1005 |
if (!(*_node_filter)[u] || !(*_node_filter)[v]) { |
1006 | 1006 |
return INVALID; |
1007 | 1007 |
} |
1008 | 1008 |
Edge edge = Parent::findEdge(u, v, prev); |
1009 | 1009 |
while (edge != INVALID && !(*_edge_filter)[edge]) { |
1010 | 1010 |
edge = Parent::findEdge(u, v, edge); |
1011 | 1011 |
} |
1012 | 1012 |
return edge; |
1013 | 1013 |
} |
1014 | 1014 |
|
1015 | 1015 |
template <typename V> |
1016 | 1016 |
class NodeMap |
1017 | 1017 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
1018 | 1018 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> { |
1019 | 1019 |
public: |
1020 | 1020 |
typedef V Value; |
1021 | 1021 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
1022 | 1022 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent; |
1023 | 1023 |
|
1024 | 1024 |
NodeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor) |
1025 | 1025 |
: Parent(adaptor) {} |
1026 | 1026 |
NodeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value) |
1027 | 1027 |
: Parent(adaptor, value) {} |
1028 | 1028 |
|
1029 | 1029 |
private: |
1030 | 1030 |
NodeMap& operator=(const NodeMap& cmap) { |
1031 | 1031 |
return operator=<NodeMap>(cmap); |
1032 | 1032 |
} |
1033 | 1033 |
|
1034 | 1034 |
template <typename CMap> |
1035 | 1035 |
NodeMap& operator=(const CMap& cmap) { |
1036 | 1036 |
Parent::operator=(cmap); |
1037 | 1037 |
return *this; |
1038 | 1038 |
} |
1039 | 1039 |
}; |
1040 | 1040 |
|
1041 | 1041 |
template <typename V> |
1042 | 1042 |
class ArcMap |
1043 | 1043 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
1044 | 1044 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> { |
1045 | 1045 |
public: |
1046 | 1046 |
typedef V Value; |
1047 | 1047 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
1048 | 1048 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent; |
1049 | 1049 |
|
1050 | 1050 |
ArcMap(const SubGraphBase<GR, NF, EF, ch>& adaptor) |
1051 | 1051 |
: Parent(adaptor) {} |
1052 | 1052 |
ArcMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value) |
1053 | 1053 |
: Parent(adaptor, value) {} |
1054 | 1054 |
|
1055 | 1055 |
private: |
1056 | 1056 |
ArcMap& operator=(const ArcMap& cmap) { |
1057 | 1057 |
return operator=<ArcMap>(cmap); |
1058 | 1058 |
} |
1059 | 1059 |
|
1060 | 1060 |
template <typename CMap> |
1061 | 1061 |
ArcMap& operator=(const CMap& cmap) { |
1062 | 1062 |
Parent::operator=(cmap); |
1063 | 1063 |
return *this; |
1064 | 1064 |
} |
1065 | 1065 |
}; |
1066 | 1066 |
|
1067 | 1067 |
template <typename V> |
1068 | 1068 |
class EdgeMap |
1069 | 1069 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
1070 | 1070 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> { |
1071 | 1071 |
public: |
1072 | 1072 |
typedef V Value; |
1073 | 1073 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
1074 | 1074 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent; |
1075 | 1075 |
|
1076 | 1076 |
EdgeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor) |
1077 | 1077 |
: Parent(adaptor) {} |
1078 | 1078 |
|
1079 | 1079 |
EdgeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value) |
1080 | 1080 |
: Parent(adaptor, value) {} |
1081 | 1081 |
|
1082 | 1082 |
private: |
1083 | 1083 |
EdgeMap& operator=(const EdgeMap& cmap) { |
1084 | 1084 |
return operator=<EdgeMap>(cmap); |
1085 | 1085 |
} |
1086 | 1086 |
|
1087 | 1087 |
template <typename CMap> |
1088 | 1088 |
EdgeMap& operator=(const CMap& cmap) { |
1089 | 1089 |
Parent::operator=(cmap); |
1090 | 1090 |
return *this; |
1091 | 1091 |
} |
1092 | 1092 |
}; |
1093 | 1093 |
|
1094 | 1094 |
}; |
1095 | 1095 |
|
1096 | 1096 |
template <typename GR, typename NF, typename EF> |
1097 | 1097 |
class SubGraphBase<GR, NF, EF, false> |
1098 | 1098 |
: public GraphAdaptorBase<GR> { |
1099 | 1099 |
public: |
1100 | 1100 |
typedef GR Graph; |
1101 | 1101 |
typedef NF NodeFilterMap; |
1102 | 1102 |
typedef EF EdgeFilterMap; |
1103 | 1103 |
|
1104 | 1104 |
typedef SubGraphBase Adaptor; |
1105 | 1105 |
typedef GraphAdaptorBase<GR> Parent; |
1106 | 1106 |
protected: |
1107 | 1107 |
NF* _node_filter; |
1108 | 1108 |
EF* _edge_filter; |
1109 | 1109 |
SubGraphBase() |
1110 | 1110 |
: Parent(), _node_filter(0), _edge_filter(0) { } |
1111 | 1111 |
|
1112 | 1112 |
void initialize(GR& graph, NF& node_filter, EF& edge_filter) { |
1113 | 1113 |
Parent::initialize(graph); |
1114 | 1114 |
_node_filter = &node_filter; |
1115 | 1115 |
_edge_filter = &edge_filter; |
1116 | 1116 |
} |
1117 | 1117 |
|
1118 | 1118 |
public: |
1119 | 1119 |
|
1120 | 1120 |
typedef typename Parent::Node Node; |
1121 | 1121 |
typedef typename Parent::Arc Arc; |
1122 | 1122 |
typedef typename Parent::Edge Edge; |
1123 | 1123 |
|
1124 | 1124 |
void first(Node& i) const { |
1125 | 1125 |
Parent::first(i); |
1126 | 1126 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
1127 | 1127 |
} |
1128 | 1128 |
|
1129 | 1129 |
void first(Arc& i) const { |
1130 | 1130 |
Parent::first(i); |
1131 | 1131 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
1132 | 1132 |
} |
1133 | 1133 |
|
1134 | 1134 |
void first(Edge& i) const { |
1135 | 1135 |
Parent::first(i); |
1136 | 1136 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
1137 | 1137 |
} |
1138 | 1138 |
|
1139 | 1139 |
void firstIn(Arc& i, const Node& n) const { |
1140 | 1140 |
Parent::firstIn(i, n); |
1141 | 1141 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextIn(i); |
1142 | 1142 |
} |
1143 | 1143 |
|
1144 | 1144 |
void firstOut(Arc& i, const Node& n) const { |
1145 | 1145 |
Parent::firstOut(i, n); |
1146 | 1146 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextOut(i); |
1147 | 1147 |
} |
1148 | 1148 |
|
1149 | 1149 |
void firstInc(Edge& i, bool& d, const Node& n) const { |
1150 | 1150 |
Parent::firstInc(i, d, n); |
1151 | 1151 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextInc(i, d); |
1152 | 1152 |
} |
1153 | 1153 |
|
1154 | 1154 |
void next(Node& i) const { |
1155 | 1155 |
Parent::next(i); |
1156 | 1156 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
1157 | 1157 |
} |
1158 | 1158 |
void next(Arc& i) const { |
1159 | 1159 |
Parent::next(i); |
1160 | 1160 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
1161 | 1161 |
} |
1162 | 1162 |
void next(Edge& i) const { |
1163 | 1163 |
Parent::next(i); |
1164 | 1164 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
1165 | 1165 |
} |
1166 | 1166 |
void nextIn(Arc& i) const { |
1167 | 1167 |
Parent::nextIn(i); |
1168 | 1168 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextIn(i); |
1169 | 1169 |
} |
1170 | 1170 |
|
1171 | 1171 |
void nextOut(Arc& i) const { |
1172 | 1172 |
Parent::nextOut(i); |
1173 | 1173 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextOut(i); |
1174 | 1174 |
} |
1175 | 1175 |
void nextInc(Edge& i, bool& d) const { |
1176 | 1176 |
Parent::nextInc(i, d); |
1177 | 1177 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextInc(i, d); |
1178 | 1178 |
} |
1179 | 1179 |
|
1180 | 1180 |
void status(const Node& n, bool v) const { _node_filter->set(n, v); } |
1181 | 1181 |
void status(const Edge& e, bool v) const { _edge_filter->set(e, v); } |
1182 | 1182 |
|
1183 | 1183 |
bool status(const Node& n) const { return (*_node_filter)[n]; } |
1184 | 1184 |
bool status(const Edge& e) const { return (*_edge_filter)[e]; } |
1185 | 1185 |
|
1186 | 1186 |
typedef False NodeNumTag; |
1187 | 1187 |
typedef False ArcNumTag; |
1188 | 1188 |
typedef False EdgeNumTag; |
1189 | 1189 |
|
1190 | 1190 |
typedef FindArcTagIndicator<Graph> FindArcTag; |
1191 | 1191 |
Arc findArc(const Node& u, const Node& v, |
1192 | 1192 |
const Arc& prev = INVALID) const { |
1193 | 1193 |
Arc arc = Parent::findArc(u, v, prev); |
1194 | 1194 |
while (arc != INVALID && !(*_edge_filter)[arc]) { |
1195 | 1195 |
arc = Parent::findArc(u, v, arc); |
1196 | 1196 |
} |
1197 | 1197 |
return arc; |
1198 | 1198 |
} |
1199 | 1199 |
|
1200 | 1200 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
1201 | 1201 |
Edge findEdge(const Node& u, const Node& v, |
1202 | 1202 |
const Edge& prev = INVALID) const { |
1203 | 1203 |
Edge edge = Parent::findEdge(u, v, prev); |
1204 | 1204 |
while (edge != INVALID && !(*_edge_filter)[edge]) { |
1205 | 1205 |
edge = Parent::findEdge(u, v, edge); |
1206 | 1206 |
} |
1207 | 1207 |
return edge; |
1208 | 1208 |
} |
1209 | 1209 |
|
1210 | 1210 |
template <typename V> |
1211 | 1211 |
class NodeMap |
1212 | 1212 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
1213 | 1213 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> { |
1214 | 1214 |
public: |
1215 | 1215 |
typedef V Value; |
1216 | 1216 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
1217 | 1217 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent; |
1218 | 1218 |
|
1219 | 1219 |
NodeMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
1220 | 1220 |
: Parent(adaptor) {} |
1221 | 1221 |
NodeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
1222 | 1222 |
: Parent(adaptor, value) {} |
1223 | 1223 |
|
1224 | 1224 |
private: |
1225 | 1225 |
NodeMap& operator=(const NodeMap& cmap) { |
1226 | 1226 |
return operator=<NodeMap>(cmap); |
1227 | 1227 |
} |
1228 | 1228 |
|
1229 | 1229 |
template <typename CMap> |
1230 | 1230 |
NodeMap& operator=(const CMap& cmap) { |
1231 | 1231 |
Parent::operator=(cmap); |
1232 | 1232 |
return *this; |
1233 | 1233 |
} |
1234 | 1234 |
}; |
1235 | 1235 |
|
1236 | 1236 |
template <typename V> |
1237 | 1237 |
class ArcMap |
1238 | 1238 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
1239 | 1239 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> { |
1240 | 1240 |
public: |
1241 | 1241 |
typedef V Value; |
1242 | 1242 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
1243 | 1243 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent; |
1244 | 1244 |
|
1245 | 1245 |
ArcMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
1246 | 1246 |
: Parent(adaptor) {} |
1247 | 1247 |
ArcMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
1248 | 1248 |
: Parent(adaptor, value) {} |
1249 | 1249 |
|
1250 | 1250 |
private: |
1251 | 1251 |
ArcMap& operator=(const ArcMap& cmap) { |
1252 | 1252 |
return operator=<ArcMap>(cmap); |
1253 | 1253 |
} |
1254 | 1254 |
|
1255 | 1255 |
template <typename CMap> |
1256 | 1256 |
ArcMap& operator=(const CMap& cmap) { |
1257 | 1257 |
Parent::operator=(cmap); |
1258 | 1258 |
return *this; |
1259 | 1259 |
} |
1260 | 1260 |
}; |
1261 | 1261 |
|
1262 | 1262 |
template <typename V> |
1263 | 1263 |
class EdgeMap |
1264 | 1264 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
1265 | 1265 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> { |
1266 | 1266 |
public: |
1267 | 1267 |
typedef V Value; |
1268 | 1268 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
1269 | 1269 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent; |
1270 | 1270 |
|
1271 | 1271 |
EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
1272 | 1272 |
: Parent(adaptor) {} |
1273 | 1273 |
|
1274 | 1274 |
EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
1275 | 1275 |
: Parent(adaptor, value) {} |
1276 | 1276 |
|
1277 | 1277 |
private: |
1278 | 1278 |
EdgeMap& operator=(const EdgeMap& cmap) { |
1279 | 1279 |
return operator=<EdgeMap>(cmap); |
1280 | 1280 |
} |
1281 | 1281 |
|
1282 | 1282 |
template <typename CMap> |
1283 | 1283 |
EdgeMap& operator=(const CMap& cmap) { |
1284 | 1284 |
Parent::operator=(cmap); |
1285 | 1285 |
return *this; |
1286 | 1286 |
} |
1287 | 1287 |
}; |
1288 | 1288 |
|
1289 | 1289 |
}; |
1290 | 1290 |
|
1291 | 1291 |
/// \ingroup graph_adaptors |
1292 | 1292 |
/// |
1293 | 1293 |
/// \brief Adaptor class for hiding nodes and edges in an undirected |
1294 | 1294 |
/// graph. |
1295 | 1295 |
/// |
1296 | 1296 |
/// SubGraph can be used for hiding nodes and edges in a graph. |
1297 | 1297 |
/// A \c bool node map and a \c bool edge map must be specified, which |
1298 | 1298 |
/// define the filters for nodes and edges. |
1299 | 1299 |
/// Only the nodes and edges with \c true filter value are |
1300 | 1300 |
/// shown in the subgraph. The edges that are incident to hidden |
1301 | 1301 |
/// nodes are also filtered out. |
1302 | 1302 |
/// This adaptor conforms to the \ref concepts::Graph "Graph" concept. |
1303 | 1303 |
/// |
1304 | 1304 |
/// The adapted graph can also be modified through this adaptor |
1305 | 1305 |
/// by adding or removing nodes or edges, unless the \c GR template |
1306 | 1306 |
/// parameter is set to be \c const. |
1307 | 1307 |
/// |
1308 | 1308 |
/// \tparam GR The type of the adapted graph. |
1309 | 1309 |
/// It must conform to the \ref concepts::Graph "Graph" concept. |
1310 | 1310 |
/// It can also be specified to be \c const. |
1311 | 1311 |
/// \tparam NF The type of the node filter map. |
1312 | 1312 |
/// It must be a \c bool (or convertible) node map of the |
1313 | 1313 |
/// adapted graph. The default type is |
1314 | 1314 |
/// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>". |
1315 | 1315 |
/// \tparam EF The type of the edge filter map. |
1316 | 1316 |
/// It must be a \c bool (or convertible) edge map of the |
1317 | 1317 |
/// adapted graph. The default type is |
1318 | 1318 |
/// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
1319 | 1319 |
/// |
1320 | 1320 |
/// \note The \c Node, \c Edge and \c Arc types of this adaptor and the |
1321 | 1321 |
/// adapted graph are convertible to each other. |
1322 | 1322 |
/// |
1323 | 1323 |
/// \see FilterNodes |
1324 | 1324 |
/// \see FilterEdges |
1325 | 1325 |
#ifdef DOXYGEN |
1326 | 1326 |
template<typename GR, typename NF, typename EF> |
1327 | 1327 |
class SubGraph { |
1328 | 1328 |
#else |
1329 | 1329 |
template<typename GR, |
1330 | 1330 |
typename NF = typename GR::template NodeMap<bool>, |
1331 | 1331 |
typename EF = typename GR::template EdgeMap<bool> > |
1332 | 1332 |
class SubGraph : |
1333 | 1333 |
public GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> > { |
1334 | 1334 |
#endif |
1335 | 1335 |
public: |
1336 | 1336 |
/// The type of the adapted graph. |
1337 | 1337 |
typedef GR Graph; |
1338 | 1338 |
/// The type of the node filter map. |
1339 | 1339 |
typedef NF NodeFilterMap; |
1340 | 1340 |
/// The type of the edge filter map. |
1341 | 1341 |
typedef EF EdgeFilterMap; |
1342 | 1342 |
|
1343 | 1343 |
typedef GraphAdaptorExtender<SubGraphBase<GR, NF, EF, true> > |
1344 | 1344 |
Parent; |
1345 | 1345 |
|
1346 | 1346 |
typedef typename Parent::Node Node; |
1347 | 1347 |
typedef typename Parent::Edge Edge; |
1348 | 1348 |
|
1349 | 1349 |
protected: |
1350 | 1350 |
SubGraph() { } |
1351 | 1351 |
public: |
1352 | 1352 |
|
1353 | 1353 |
/// \brief Constructor |
1354 | 1354 |
/// |
1355 | 1355 |
/// Creates a subgraph for the given graph with the given node |
1356 | 1356 |
/// and edge filter maps. |
1357 | 1357 |
SubGraph(GR& graph, NF& node_filter, EF& edge_filter) { |
1358 | 1358 |
initialize(graph, node_filter, edge_filter); |
1359 | 1359 |
} |
1360 | 1360 |
|
1361 | 1361 |
/// \brief Sets the status of the given node |
1362 | 1362 |
/// |
1363 | 1363 |
/// This function sets the status of the given node. |
1364 | 1364 |
/// It is done by simply setting the assigned value of \c n |
1365 | 1365 |
/// to \c v in the node filter map. |
1366 | 1366 |
void status(const Node& n, bool v) const { Parent::status(n, v); } |
1367 | 1367 |
|
1368 | 1368 |
/// \brief Sets the status of the given edge |
1369 | 1369 |
/// |
1370 | 1370 |
/// This function sets the status of the given edge. |
1371 | 1371 |
/// It is done by simply setting the assigned value of \c e |
1372 | 1372 |
/// to \c v in the edge filter map. |
1373 | 1373 |
void status(const Edge& e, bool v) const { Parent::status(e, v); } |
1374 | 1374 |
|
1375 | 1375 |
/// \brief Returns the status of the given node |
1376 | 1376 |
/// |
1377 | 1377 |
/// This function returns the status of the given node. |
1378 | 1378 |
/// It is \c true if the given node is enabled (i.e. not hidden). |
1379 | 1379 |
bool status(const Node& n) const { return Parent::status(n); } |
1380 | 1380 |
|
1381 | 1381 |
/// \brief Returns the status of the given edge |
1382 | 1382 |
/// |
1383 | 1383 |
/// This function returns the status of the given edge. |
1384 | 1384 |
/// It is \c true if the given edge is enabled (i.e. not hidden). |
1385 | 1385 |
bool status(const Edge& e) const { return Parent::status(e); } |
1386 | 1386 |
|
1387 | 1387 |
/// \brief Disables the given node |
1388 | 1388 |
/// |
1389 | 1389 |
/// This function disables the given node in the subdigraph, |
1390 | 1390 |
/// so the iteration jumps over it. |
1391 | 1391 |
/// It is the same as \ref status() "status(n, false)". |
1392 | 1392 |
void disable(const Node& n) const { Parent::status(n, false); } |
1393 | 1393 |
|
1394 | 1394 |
/// \brief Disables the given edge |
1395 | 1395 |
/// |
1396 | 1396 |
/// This function disables the given edge in the subgraph, |
1397 | 1397 |
/// so the iteration jumps over it. |
1398 | 1398 |
/// It is the same as \ref status() "status(e, false)". |
1399 | 1399 |
void disable(const Edge& e) const { Parent::status(e, false); } |
1400 | 1400 |
|
1401 | 1401 |
/// \brief Enables the given node |
1402 | 1402 |
/// |
1403 | 1403 |
/// This function enables the given node in the subdigraph. |
1404 | 1404 |
/// It is the same as \ref status() "status(n, true)". |
1405 | 1405 |
void enable(const Node& n) const { Parent::status(n, true); } |
1406 | 1406 |
|
1407 | 1407 |
/// \brief Enables the given edge |
1408 | 1408 |
/// |
1409 | 1409 |
/// This function enables the given edge in the subgraph. |
1410 | 1410 |
/// It is the same as \ref status() "status(e, true)". |
1411 | 1411 |
void enable(const Edge& e) const { Parent::status(e, true); } |
1412 | 1412 |
|
1413 | 1413 |
}; |
1414 | 1414 |
|
1415 | 1415 |
/// \brief Returns a read-only SubGraph adaptor |
1416 | 1416 |
/// |
1417 | 1417 |
/// This function just returns a read-only \ref SubGraph adaptor. |
1418 | 1418 |
/// \ingroup graph_adaptors |
1419 | 1419 |
/// \relates SubGraph |
1420 | 1420 |
template<typename GR, typename NF, typename EF> |
1421 | 1421 |
SubGraph<const GR, NF, EF> |
1422 | 1422 |
subGraph(const GR& graph, NF& node_filter, EF& edge_filter) { |
1423 | 1423 |
return SubGraph<const GR, NF, EF> |
1424 | 1424 |
(graph, node_filter, edge_filter); |
1425 | 1425 |
} |
1426 | 1426 |
|
1427 | 1427 |
template<typename GR, typename NF, typename EF> |
1428 | 1428 |
SubGraph<const GR, const NF, EF> |
1429 | 1429 |
subGraph(const GR& graph, const NF& node_filter, EF& edge_filter) { |
1430 | 1430 |
return SubGraph<const GR, const NF, EF> |
1431 | 1431 |
(graph, node_filter, edge_filter); |
1432 | 1432 |
} |
1433 | 1433 |
|
1434 | 1434 |
template<typename GR, typename NF, typename EF> |
1435 | 1435 |
SubGraph<const GR, NF, const EF> |
1436 | 1436 |
subGraph(const GR& graph, NF& node_filter, const EF& edge_filter) { |
1437 | 1437 |
return SubGraph<const GR, NF, const EF> |
1438 | 1438 |
(graph, node_filter, edge_filter); |
1439 | 1439 |
} |
1440 | 1440 |
|
1441 | 1441 |
template<typename GR, typename NF, typename EF> |
1442 | 1442 |
SubGraph<const GR, const NF, const EF> |
1443 | 1443 |
subGraph(const GR& graph, const NF& node_filter, const EF& edge_filter) { |
1444 | 1444 |
return SubGraph<const GR, const NF, const EF> |
1445 | 1445 |
(graph, node_filter, edge_filter); |
1446 | 1446 |
} |
1447 | 1447 |
|
1448 | 1448 |
|
1449 | 1449 |
/// \ingroup graph_adaptors |
1450 | 1450 |
/// |
1451 | 1451 |
/// \brief Adaptor class for hiding nodes in a digraph or a graph. |
1452 | 1452 |
/// |
1453 | 1453 |
/// FilterNodes adaptor can be used for hiding nodes in a digraph or a |
1454 | 1454 |
/// graph. A \c bool node map must be specified, which defines the filter |
1455 | 1455 |
/// for the nodes. Only the nodes with \c true filter value and the |
1456 | 1456 |
/// arcs/edges incident to nodes both with \c true filter value are shown |
1457 | 1457 |
/// in the subgraph. This adaptor conforms to the \ref concepts::Digraph |
1458 | 1458 |
/// "Digraph" concept or the \ref concepts::Graph "Graph" concept |
1459 | 1459 |
/// depending on the \c GR template parameter. |
1460 | 1460 |
/// |
1461 | 1461 |
/// The adapted (di)graph can also be modified through this adaptor |
1462 | 1462 |
/// by adding or removing nodes or arcs/edges, unless the \c GR template |
1463 | 1463 |
/// parameter is set to be \c const. |
1464 | 1464 |
/// |
1465 | 1465 |
/// \tparam GR The type of the adapted digraph or graph. |
1466 | 1466 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept |
1467 | 1467 |
/// or the \ref concepts::Graph "Graph" concept. |
1468 | 1468 |
/// It can also be specified to be \c const. |
1469 | 1469 |
/// \tparam NF The type of the node filter map. |
1470 | 1470 |
/// It must be a \c bool (or convertible) node map of the |
1471 | 1471 |
/// adapted (di)graph. The default type is |
1472 | 1472 |
/// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>". |
1473 | 1473 |
/// |
1474 | 1474 |
/// \note The \c Node and <tt>Arc/Edge</tt> types of this adaptor and the |
1475 | 1475 |
/// adapted (di)graph are convertible to each other. |
1476 | 1476 |
#ifdef DOXYGEN |
1477 | 1477 |
template<typename GR, typename NF> |
1478 | 1478 |
class FilterNodes { |
1479 | 1479 |
#else |
1480 | 1480 |
template<typename GR, |
1481 | 1481 |
typename NF = typename GR::template NodeMap<bool>, |
1482 | 1482 |
typename Enable = void> |
1483 | 1483 |
class FilterNodes : |
1484 | 1484 |
public DigraphAdaptorExtender< |
1485 | 1485 |
SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >, |
1486 | 1486 |
true> > { |
1487 | 1487 |
#endif |
1488 | 1488 |
public: |
1489 | 1489 |
|
1490 | 1490 |
typedef GR Digraph; |
1491 | 1491 |
typedef NF NodeFilterMap; |
1492 | 1492 |
|
1493 | 1493 |
typedef DigraphAdaptorExtender< |
1494 | 1494 |
SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >, |
1495 | 1495 |
true> > Parent; |
1496 | 1496 |
|
1497 | 1497 |
typedef typename Parent::Node Node; |
1498 | 1498 |
|
1499 | 1499 |
protected: |
1500 | 1500 |
ConstMap<typename Digraph::Arc, Const<bool, true> > const_true_map; |
1501 | 1501 |
|
1502 | 1502 |
FilterNodes() : const_true_map() {} |
1503 | 1503 |
|
1504 | 1504 |
public: |
1505 | 1505 |
|
1506 | 1506 |
/// \brief Constructor |
1507 | 1507 |
/// |
1508 | 1508 |
/// Creates a subgraph for the given digraph or graph with the |
1509 | 1509 |
/// given node filter map. |
1510 | 1510 |
FilterNodes(GR& graph, NF& node_filter) |
1511 | 1511 |
: Parent(), const_true_map() |
1512 | 1512 |
{ |
1513 | 1513 |
Parent::initialize(graph, node_filter, const_true_map); |
1514 | 1514 |
} |
1515 | 1515 |
|
1516 | 1516 |
/// \brief Sets the status of the given node |
1517 | 1517 |
/// |
1518 | 1518 |
/// This function sets the status of the given node. |
1519 | 1519 |
/// It is done by simply setting the assigned value of \c n |
1520 | 1520 |
/// to \c v in the node filter map. |
1521 | 1521 |
void status(const Node& n, bool v) const { Parent::status(n, v); } |
1522 | 1522 |
|
1523 | 1523 |
/// \brief Returns the status of the given node |
1524 | 1524 |
/// |
1525 | 1525 |
/// This function returns the status of the given node. |
1526 | 1526 |
/// It is \c true if the given node is enabled (i.e. not hidden). |
1527 | 1527 |
bool status(const Node& n) const { return Parent::status(n); } |
1528 | 1528 |
|
1529 | 1529 |
/// \brief Disables the given node |
1530 | 1530 |
/// |
1531 | 1531 |
/// This function disables the given node, so the iteration |
1532 | 1532 |
/// jumps over it. |
1533 | 1533 |
/// It is the same as \ref status() "status(n, false)". |
1534 | 1534 |
void disable(const Node& n) const { Parent::status(n, false); } |
1535 | 1535 |
|
1536 | 1536 |
/// \brief Enables the given node |
1537 | 1537 |
/// |
1538 | 1538 |
/// This function enables the given node. |
1539 | 1539 |
/// It is the same as \ref status() "status(n, true)". |
1540 | 1540 |
void enable(const Node& n) const { Parent::status(n, true); } |
1541 | 1541 |
|
1542 | 1542 |
}; |
1543 | 1543 |
|
1544 | 1544 |
template<typename GR, typename NF> |
1545 | 1545 |
class FilterNodes<GR, NF, |
1546 | 1546 |
typename enable_if<UndirectedTagIndicator<GR> >::type> : |
1547 | 1547 |
public GraphAdaptorExtender< |
1548 | 1548 |
SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, |
1549 | 1549 |
true> > { |
1550 | 1550 |
|
1551 | 1551 |
public: |
1552 | 1552 |
typedef GR Graph; |
1553 | 1553 |
typedef NF NodeFilterMap; |
1554 | 1554 |
typedef GraphAdaptorExtender< |
1555 | 1555 |
SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, |
1556 | 1556 |
true> > Parent; |
1557 | 1557 |
|
1558 | 1558 |
typedef typename Parent::Node Node; |
1559 | 1559 |
protected: |
1560 | 1560 |
ConstMap<typename GR::Edge, Const<bool, true> > const_true_map; |
1561 | 1561 |
|
1562 | 1562 |
FilterNodes() : const_true_map() {} |
1563 | 1563 |
|
1564 | 1564 |
public: |
1565 | 1565 |
|
1566 | 1566 |
FilterNodes(GR& graph, NodeFilterMap& node_filter) : |
1567 | 1567 |
Parent(), const_true_map() { |
1568 | 1568 |
Parent::initialize(graph, node_filter, const_true_map); |
1569 | 1569 |
} |
1570 | 1570 |
|
1571 | 1571 |
void status(const Node& n, bool v) const { Parent::status(n, v); } |
1572 | 1572 |
bool status(const Node& n) const { return Parent::status(n); } |
1573 | 1573 |
void disable(const Node& n) const { Parent::status(n, false); } |
1574 | 1574 |
void enable(const Node& n) const { Parent::status(n, true); } |
1575 | 1575 |
|
1576 | 1576 |
}; |
1577 | 1577 |
|
1578 | 1578 |
|
1579 | 1579 |
/// \brief Returns a read-only FilterNodes adaptor |
1580 | 1580 |
/// |
1581 | 1581 |
/// This function just returns a read-only \ref FilterNodes adaptor. |
1582 | 1582 |
/// \ingroup graph_adaptors |
1583 | 1583 |
/// \relates FilterNodes |
1584 | 1584 |
template<typename GR, typename NF> |
1585 | 1585 |
FilterNodes<const GR, NF> |
1586 | 1586 |
filterNodes(const GR& graph, NF& node_filter) { |
1587 | 1587 |
return FilterNodes<const GR, NF>(graph, node_filter); |
1588 | 1588 |
} |
1589 | 1589 |
|
1590 | 1590 |
template<typename GR, typename NF> |
1591 | 1591 |
FilterNodes<const GR, const NF> |
1592 | 1592 |
filterNodes(const GR& graph, const NF& node_filter) { |
1593 | 1593 |
return FilterNodes<const GR, const NF>(graph, node_filter); |
1594 | 1594 |
} |
1595 | 1595 |
|
1596 | 1596 |
/// \ingroup graph_adaptors |
1597 | 1597 |
/// |
1598 | 1598 |
/// \brief Adaptor class for hiding arcs in a digraph. |
1599 | 1599 |
/// |
1600 | 1600 |
/// FilterArcs adaptor can be used for hiding arcs in a digraph. |
1601 | 1601 |
/// A \c bool arc map must be specified, which defines the filter for |
1602 | 1602 |
/// the arcs. Only the arcs with \c true filter value are shown in the |
1603 | 1603 |
/// subdigraph. This adaptor conforms to the \ref concepts::Digraph |
1604 | 1604 |
/// "Digraph" concept. |
1605 | 1605 |
/// |
1606 | 1606 |
/// The adapted digraph can also be modified through this adaptor |
1607 | 1607 |
/// by adding or removing nodes or arcs, unless the \c GR template |
1608 | 1608 |
/// parameter is set to be \c const. |
1609 | 1609 |
/// |
1610 | 1610 |
/// \tparam DGR The type of the adapted digraph. |
1611 | 1611 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
1612 | 1612 |
/// It can also be specified to be \c const. |
1613 | 1613 |
/// \tparam AF The type of the arc filter map. |
1614 | 1614 |
/// It must be a \c bool (or convertible) arc map of the |
1615 | 1615 |
/// adapted digraph. The default type is |
1616 | 1616 |
/// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>". |
1617 | 1617 |
/// |
1618 | 1618 |
/// \note The \c Node and \c Arc types of this adaptor and the adapted |
1619 | 1619 |
/// digraph are convertible to each other. |
1620 | 1620 |
#ifdef DOXYGEN |
1621 | 1621 |
template<typename DGR, |
1622 | 1622 |
typename AF> |
1623 | 1623 |
class FilterArcs { |
1624 | 1624 |
#else |
1625 | 1625 |
template<typename DGR, |
1626 | 1626 |
typename AF = typename DGR::template ArcMap<bool> > |
1627 | 1627 |
class FilterArcs : |
1628 | 1628 |
public DigraphAdaptorExtender< |
1629 | 1629 |
SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >, |
1630 | 1630 |
AF, false> > { |
1631 | 1631 |
#endif |
1632 | 1632 |
public: |
1633 | 1633 |
/// The type of the adapted digraph. |
1634 | 1634 |
typedef DGR Digraph; |
1635 | 1635 |
/// The type of the arc filter map. |
1636 | 1636 |
typedef AF ArcFilterMap; |
1637 | 1637 |
|
1638 | 1638 |
typedef DigraphAdaptorExtender< |
1639 | 1639 |
SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >, |
1640 | 1640 |
AF, false> > Parent; |
1641 | 1641 |
|
1642 | 1642 |
typedef typename Parent::Arc Arc; |
1643 | 1643 |
|
1644 | 1644 |
protected: |
1645 | 1645 |
ConstMap<typename DGR::Node, Const<bool, true> > const_true_map; |
1646 | 1646 |
|
1647 | 1647 |
FilterArcs() : const_true_map() {} |
1648 | 1648 |
|
1649 | 1649 |
public: |
1650 | 1650 |
|
1651 | 1651 |
/// \brief Constructor |
1652 | 1652 |
/// |
1653 | 1653 |
/// Creates a subdigraph for the given digraph with the given arc |
1654 | 1654 |
/// filter map. |
1655 | 1655 |
FilterArcs(DGR& digraph, ArcFilterMap& arc_filter) |
1656 | 1656 |
: Parent(), const_true_map() { |
1657 | 1657 |
Parent::initialize(digraph, const_true_map, arc_filter); |
1658 | 1658 |
} |
1659 | 1659 |
|
1660 | 1660 |
/// \brief Sets the status of the given arc |
1661 | 1661 |
/// |
1662 | 1662 |
/// This function sets the status of the given arc. |
1663 | 1663 |
/// It is done by simply setting the assigned value of \c a |
1664 | 1664 |
/// to \c v in the arc filter map. |
1665 | 1665 |
void status(const Arc& a, bool v) const { Parent::status(a, v); } |
1666 | 1666 |
|
1667 | 1667 |
/// \brief Returns the status of the given arc |
1668 | 1668 |
/// |
1669 | 1669 |
/// This function returns the status of the given arc. |
1670 | 1670 |
/// It is \c true if the given arc is enabled (i.e. not hidden). |
1671 | 1671 |
bool status(const Arc& a) const { return Parent::status(a); } |
1672 | 1672 |
|
1673 | 1673 |
/// \brief Disables the given arc |
1674 | 1674 |
/// |
1675 | 1675 |
/// This function disables the given arc in the subdigraph, |
1676 | 1676 |
/// so the iteration jumps over it. |
1677 | 1677 |
/// It is the same as \ref status() "status(a, false)". |
1678 | 1678 |
void disable(const Arc& a) const { Parent::status(a, false); } |
1679 | 1679 |
|
1680 | 1680 |
/// \brief Enables the given arc |
1681 | 1681 |
/// |
1682 | 1682 |
/// This function enables the given arc in the subdigraph. |
1683 | 1683 |
/// It is the same as \ref status() "status(a, true)". |
1684 | 1684 |
void enable(const Arc& a) const { Parent::status(a, true); } |
1685 | 1685 |
|
1686 | 1686 |
}; |
1687 | 1687 |
|
1688 | 1688 |
/// \brief Returns a read-only FilterArcs adaptor |
1689 | 1689 |
/// |
1690 | 1690 |
/// This function just returns a read-only \ref FilterArcs adaptor. |
1691 | 1691 |
/// \ingroup graph_adaptors |
1692 | 1692 |
/// \relates FilterArcs |
1693 | 1693 |
template<typename DGR, typename AF> |
1694 | 1694 |
FilterArcs<const DGR, AF> |
1695 | 1695 |
filterArcs(const DGR& digraph, AF& arc_filter) { |
1696 | 1696 |
return FilterArcs<const DGR, AF>(digraph, arc_filter); |
1697 | 1697 |
} |
1698 | 1698 |
|
1699 | 1699 |
template<typename DGR, typename AF> |
1700 | 1700 |
FilterArcs<const DGR, const AF> |
1701 | 1701 |
filterArcs(const DGR& digraph, const AF& arc_filter) { |
1702 | 1702 |
return FilterArcs<const DGR, const AF>(digraph, arc_filter); |
1703 | 1703 |
} |
1704 | 1704 |
|
1705 | 1705 |
/// \ingroup graph_adaptors |
1706 | 1706 |
/// |
1707 | 1707 |
/// \brief Adaptor class for hiding edges in a graph. |
1708 | 1708 |
/// |
1709 | 1709 |
/// FilterEdges adaptor can be used for hiding edges in a graph. |
1710 | 1710 |
/// A \c bool edge map must be specified, which defines the filter for |
1711 | 1711 |
/// the edges. Only the edges with \c true filter value are shown in the |
1712 | 1712 |
/// subgraph. This adaptor conforms to the \ref concepts::Graph |
1713 | 1713 |
/// "Graph" concept. |
1714 | 1714 |
/// |
1715 | 1715 |
/// The adapted graph can also be modified through this adaptor |
1716 | 1716 |
/// by adding or removing nodes or edges, unless the \c GR template |
1717 | 1717 |
/// parameter is set to be \c const. |
1718 | 1718 |
/// |
1719 | 1719 |
/// \tparam GR The type of the adapted graph. |
1720 | 1720 |
/// It must conform to the \ref concepts::Graph "Graph" concept. |
1721 | 1721 |
/// It can also be specified to be \c const. |
1722 | 1722 |
/// \tparam EF The type of the edge filter map. |
1723 | 1723 |
/// It must be a \c bool (or convertible) edge map of the |
1724 | 1724 |
/// adapted graph. The default type is |
1725 | 1725 |
/// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
1726 | 1726 |
/// |
1727 | 1727 |
/// \note The \c Node, \c Edge and \c Arc types of this adaptor and the |
1728 | 1728 |
/// adapted graph are convertible to each other. |
1729 | 1729 |
#ifdef DOXYGEN |
1730 | 1730 |
template<typename GR, |
1731 | 1731 |
typename EF> |
1732 | 1732 |
class FilterEdges { |
1733 | 1733 |
#else |
1734 | 1734 |
template<typename GR, |
1735 | 1735 |
typename EF = typename GR::template EdgeMap<bool> > |
1736 | 1736 |
class FilterEdges : |
1737 | 1737 |
public GraphAdaptorExtender< |
1738 | 1738 |
SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true> >, |
1739 | 1739 |
EF, false> > { |
1740 | 1740 |
#endif |
1741 | 1741 |
public: |
1742 | 1742 |
/// The type of the adapted graph. |
1743 | 1743 |
typedef GR Graph; |
1744 | 1744 |
/// The type of the edge filter map. |
1745 | 1745 |
typedef EF EdgeFilterMap; |
1746 | 1746 |
|
1747 | 1747 |
typedef GraphAdaptorExtender< |
1748 | 1748 |
SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true > >, |
1749 | 1749 |
EF, false> > Parent; |
1750 | 1750 |
|
1751 | 1751 |
typedef typename Parent::Edge Edge; |
1752 | 1752 |
|
1753 | 1753 |
protected: |
1754 | 1754 |
ConstMap<typename GR::Node, Const<bool, true> > const_true_map; |
1755 | 1755 |
|
1756 | 1756 |
FilterEdges() : const_true_map(true) { |
1757 | 1757 |
Parent::setNodeFilterMap(const_true_map); |
1758 | 1758 |
} |
1759 | 1759 |
|
1760 | 1760 |
public: |
1761 | 1761 |
|
1762 | 1762 |
/// \brief Constructor |
1763 | 1763 |
/// |
1764 | 1764 |
/// Creates a subgraph for the given graph with the given edge |
1765 | 1765 |
/// filter map. |
1766 | 1766 |
FilterEdges(GR& graph, EF& edge_filter) |
1767 | 1767 |
: Parent(), const_true_map() { |
1768 | 1768 |
Parent::initialize(graph, const_true_map, edge_filter); |
1769 | 1769 |
} |
1770 | 1770 |
|
1771 | 1771 |
/// \brief Sets the status of the given edge |
1772 | 1772 |
/// |
1773 | 1773 |
/// This function sets the status of the given edge. |
1774 | 1774 |
/// It is done by simply setting the assigned value of \c e |
1775 | 1775 |
/// to \c v in the edge filter map. |
1776 | 1776 |
void status(const Edge& e, bool v) const { Parent::status(e, v); } |
1777 | 1777 |
|
1778 | 1778 |
/// \brief Returns the status of the given edge |
1779 | 1779 |
/// |
1780 | 1780 |
/// This function returns the status of the given edge. |
1781 | 1781 |
/// It is \c true if the given edge is enabled (i.e. not hidden). |
1782 | 1782 |
bool status(const Edge& e) const { return Parent::status(e); } |
1783 | 1783 |
|
1784 | 1784 |
/// \brief Disables the given edge |
1785 | 1785 |
/// |
1786 | 1786 |
/// This function disables the given edge in the subgraph, |
1787 | 1787 |
/// so the iteration jumps over it. |
1788 | 1788 |
/// It is the same as \ref status() "status(e, false)". |
1789 | 1789 |
void disable(const Edge& e) const { Parent::status(e, false); } |
1790 | 1790 |
|
1791 | 1791 |
/// \brief Enables the given edge |
1792 | 1792 |
/// |
1793 | 1793 |
/// This function enables the given edge in the subgraph. |
1794 | 1794 |
/// It is the same as \ref status() "status(e, true)". |
1795 | 1795 |
void enable(const Edge& e) const { Parent::status(e, true); } |
1796 | 1796 |
|
1797 | 1797 |
}; |
1798 | 1798 |
|
1799 | 1799 |
/// \brief Returns a read-only FilterEdges adaptor |
1800 | 1800 |
/// |
1801 | 1801 |
/// This function just returns a read-only \ref FilterEdges adaptor. |
1802 | 1802 |
/// \ingroup graph_adaptors |
1803 | 1803 |
/// \relates FilterEdges |
1804 | 1804 |
template<typename GR, typename EF> |
1805 | 1805 |
FilterEdges<const GR, EF> |
1806 | 1806 |
filterEdges(const GR& graph, EF& edge_filter) { |
1807 | 1807 |
return FilterEdges<const GR, EF>(graph, edge_filter); |
1808 | 1808 |
} |
1809 | 1809 |
|
1810 | 1810 |
template<typename GR, typename EF> |
1811 | 1811 |
FilterEdges<const GR, const EF> |
1812 | 1812 |
filterEdges(const GR& graph, const EF& edge_filter) { |
1813 | 1813 |
return FilterEdges<const GR, const EF>(graph, edge_filter); |
1814 | 1814 |
} |
1815 | 1815 |
|
1816 | 1816 |
|
1817 | 1817 |
template <typename DGR> |
1818 | 1818 |
class UndirectorBase { |
1819 | 1819 |
public: |
1820 | 1820 |
typedef DGR Digraph; |
1821 | 1821 |
typedef UndirectorBase Adaptor; |
1822 | 1822 |
|
1823 | 1823 |
typedef True UndirectedTag; |
1824 | 1824 |
|
1825 | 1825 |
typedef typename Digraph::Arc Edge; |
1826 | 1826 |
typedef typename Digraph::Node Node; |
1827 | 1827 |
|
1828 | 1828 |
class Arc : public Edge { |
1829 | 1829 |
friend class UndirectorBase; |
1830 | 1830 |
protected: |
1831 | 1831 |
bool _forward; |
1832 | 1832 |
|
1833 | 1833 |
Arc(const Edge& edge, bool forward) : |
1834 | 1834 |
Edge(edge), _forward(forward) {} |
1835 | 1835 |
|
1836 | 1836 |
public: |
1837 | 1837 |
Arc() {} |
1838 | 1838 |
|
1839 | 1839 |
Arc(Invalid) : Edge(INVALID), _forward(true) {} |
1840 | 1840 |
|
1841 | 1841 |
bool operator==(const Arc &other) const { |
1842 | 1842 |
return _forward == other._forward && |
1843 | 1843 |
static_cast<const Edge&>(*this) == static_cast<const Edge&>(other); |
1844 | 1844 |
} |
1845 | 1845 |
bool operator!=(const Arc &other) const { |
1846 | 1846 |
return _forward != other._forward || |
1847 | 1847 |
static_cast<const Edge&>(*this) != static_cast<const Edge&>(other); |
1848 | 1848 |
} |
1849 | 1849 |
bool operator<(const Arc &other) const { |
1850 | 1850 |
return _forward < other._forward || |
1851 | 1851 |
(_forward == other._forward && |
1852 | 1852 |
static_cast<const Edge&>(*this) < static_cast<const Edge&>(other)); |
1853 | 1853 |
} |
1854 | 1854 |
}; |
1855 | 1855 |
|
1856 | 1856 |
void first(Node& n) const { |
1857 | 1857 |
_digraph->first(n); |
1858 | 1858 |
} |
1859 | 1859 |
|
1860 | 1860 |
void next(Node& n) const { |
1861 | 1861 |
_digraph->next(n); |
1862 | 1862 |
} |
1863 | 1863 |
|
1864 | 1864 |
void first(Arc& a) const { |
1865 | 1865 |
_digraph->first(a); |
1866 | 1866 |
a._forward = true; |
1867 | 1867 |
} |
1868 | 1868 |
|
1869 | 1869 |
void next(Arc& a) const { |
1870 | 1870 |
if (a._forward) { |
1871 | 1871 |
a._forward = false; |
1872 | 1872 |
} else { |
1873 | 1873 |
_digraph->next(a); |
1874 | 1874 |
a._forward = true; |
1875 | 1875 |
} |
1876 | 1876 |
} |
1877 | 1877 |
|
1878 | 1878 |
void first(Edge& e) const { |
1879 | 1879 |
_digraph->first(e); |
1880 | 1880 |
} |
1881 | 1881 |
|
1882 | 1882 |
void next(Edge& e) const { |
1883 | 1883 |
_digraph->next(e); |
1884 | 1884 |
} |
1885 | 1885 |
|
1886 | 1886 |
void firstOut(Arc& a, const Node& n) const { |
1887 | 1887 |
_digraph->firstIn(a, n); |
1888 | 1888 |
if( static_cast<const Edge&>(a) != INVALID ) { |
1889 | 1889 |
a._forward = false; |
1890 | 1890 |
} else { |
1891 | 1891 |
_digraph->firstOut(a, n); |
1892 | 1892 |
a._forward = true; |
1893 | 1893 |
} |
1894 | 1894 |
} |
1895 | 1895 |
void nextOut(Arc &a) const { |
1896 | 1896 |
if (!a._forward) { |
1897 | 1897 |
Node n = _digraph->target(a); |
1898 | 1898 |
_digraph->nextIn(a); |
1899 | 1899 |
if (static_cast<const Edge&>(a) == INVALID ) { |
1900 | 1900 |
_digraph->firstOut(a, n); |
1901 | 1901 |
a._forward = true; |
1902 | 1902 |
} |
1903 | 1903 |
} |
1904 | 1904 |
else { |
1905 | 1905 |
_digraph->nextOut(a); |
1906 | 1906 |
} |
1907 | 1907 |
} |
1908 | 1908 |
|
1909 | 1909 |
void firstIn(Arc &a, const Node &n) const { |
1910 | 1910 |
_digraph->firstOut(a, n); |
1911 | 1911 |
if (static_cast<const Edge&>(a) != INVALID ) { |
1912 | 1912 |
a._forward = false; |
1913 | 1913 |
} else { |
1914 | 1914 |
_digraph->firstIn(a, n); |
1915 | 1915 |
a._forward = true; |
1916 | 1916 |
} |
1917 | 1917 |
} |
1918 | 1918 |
void nextIn(Arc &a) const { |
1919 | 1919 |
if (!a._forward) { |
1920 | 1920 |
Node n = _digraph->source(a); |
1921 | 1921 |
_digraph->nextOut(a); |
1922 | 1922 |
if( static_cast<const Edge&>(a) == INVALID ) { |
1923 | 1923 |
_digraph->firstIn(a, n); |
1924 | 1924 |
a._forward = true; |
1925 | 1925 |
} |
1926 | 1926 |
} |
1927 | 1927 |
else { |
1928 | 1928 |
_digraph->nextIn(a); |
1929 | 1929 |
} |
1930 | 1930 |
} |
1931 | 1931 |
|
1932 | 1932 |
void firstInc(Edge &e, bool &d, const Node &n) const { |
1933 | 1933 |
d = true; |
1934 | 1934 |
_digraph->firstOut(e, n); |
1935 | 1935 |
if (e != INVALID) return; |
1936 | 1936 |
d = false; |
1937 | 1937 |
_digraph->firstIn(e, n); |
1938 | 1938 |
} |
1939 | 1939 |
|
1940 | 1940 |
void nextInc(Edge &e, bool &d) const { |
1941 | 1941 |
if (d) { |
1942 | 1942 |
Node s = _digraph->source(e); |
1943 | 1943 |
_digraph->nextOut(e); |
1944 | 1944 |
if (e != INVALID) return; |
1945 | 1945 |
d = false; |
1946 | 1946 |
_digraph->firstIn(e, s); |
1947 | 1947 |
} else { |
1948 | 1948 |
_digraph->nextIn(e); |
1949 | 1949 |
} |
1950 | 1950 |
} |
1951 | 1951 |
|
1952 | 1952 |
Node u(const Edge& e) const { |
1953 | 1953 |
return _digraph->source(e); |
1954 | 1954 |
} |
1955 | 1955 |
|
1956 | 1956 |
Node v(const Edge& e) const { |
1957 | 1957 |
return _digraph->target(e); |
1958 | 1958 |
} |
1959 | 1959 |
|
1960 | 1960 |
Node source(const Arc &a) const { |
1961 | 1961 |
return a._forward ? _digraph->source(a) : _digraph->target(a); |
1962 | 1962 |
} |
1963 | 1963 |
|
1964 | 1964 |
Node target(const Arc &a) const { |
1965 | 1965 |
return a._forward ? _digraph->target(a) : _digraph->source(a); |
1966 | 1966 |
} |
1967 | 1967 |
|
1968 | 1968 |
static Arc direct(const Edge &e, bool d) { |
1969 | 1969 |
return Arc(e, d); |
1970 | 1970 |
} |
1971 | 1971 |
Arc direct(const Edge &e, const Node& n) const { |
1972 | 1972 |
return Arc(e, _digraph->source(e) == n); |
1973 | 1973 |
} |
1974 | 1974 |
|
1975 | 1975 |
static bool direction(const Arc &a) { return a._forward; } |
1976 | 1976 |
|
1977 | 1977 |
Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); } |
1978 | 1978 |
Arc arcFromId(int ix) const { |
1979 | 1979 |
return direct(_digraph->arcFromId(ix >> 1), bool(ix & 1)); |
1980 | 1980 |
} |
1981 | 1981 |
Edge edgeFromId(int ix) const { return _digraph->arcFromId(ix); } |
1982 | 1982 |
|
1983 | 1983 |
int id(const Node &n) const { return _digraph->id(n); } |
1984 | 1984 |
int id(const Arc &a) const { |
1985 | 1985 |
return (_digraph->id(a) << 1) | (a._forward ? 1 : 0); |
1986 | 1986 |
} |
1987 | 1987 |
int id(const Edge &e) const { return _digraph->id(e); } |
1988 | 1988 |
|
1989 | 1989 |
int maxNodeId() const { return _digraph->maxNodeId(); } |
1990 | 1990 |
int maxArcId() const { return (_digraph->maxArcId() << 1) | 1; } |
1991 | 1991 |
int maxEdgeId() const { return _digraph->maxArcId(); } |
1992 | 1992 |
|
1993 | 1993 |
Node addNode() { return _digraph->addNode(); } |
1994 | 1994 |
Edge addEdge(const Node& u, const Node& v) { |
1995 | 1995 |
return _digraph->addArc(u, v); |
1996 | 1996 |
} |
1997 | 1997 |
|
1998 | 1998 |
void erase(const Node& i) { _digraph->erase(i); } |
1999 | 1999 |
void erase(const Edge& i) { _digraph->erase(i); } |
2000 | 2000 |
|
2001 | 2001 |
void clear() { _digraph->clear(); } |
2002 | 2002 |
|
2003 | 2003 |
typedef NodeNumTagIndicator<Digraph> NodeNumTag; |
2004 | 2004 |
int nodeNum() const { return _digraph->nodeNum(); } |
2005 | 2005 |
|
2006 | 2006 |
typedef ArcNumTagIndicator<Digraph> ArcNumTag; |
2007 | 2007 |
int arcNum() const { return 2 * _digraph->arcNum(); } |
2008 | 2008 |
|
2009 | 2009 |
typedef ArcNumTag EdgeNumTag; |
2010 | 2010 |
int edgeNum() const { return _digraph->arcNum(); } |
2011 | 2011 |
|
2012 | 2012 |
typedef FindArcTagIndicator<Digraph> FindArcTag; |
2013 | 2013 |
Arc findArc(Node s, Node t, Arc p = INVALID) const { |
2014 | 2014 |
if (p == INVALID) { |
2015 | 2015 |
Edge arc = _digraph->findArc(s, t); |
2016 | 2016 |
if (arc != INVALID) return direct(arc, true); |
2017 | 2017 |
arc = _digraph->findArc(t, s); |
2018 | 2018 |
if (arc != INVALID) return direct(arc, false); |
2019 | 2019 |
} else if (direction(p)) { |
2020 | 2020 |
Edge arc = _digraph->findArc(s, t, p); |
2021 | 2021 |
if (arc != INVALID) return direct(arc, true); |
2022 | 2022 |
arc = _digraph->findArc(t, s); |
2023 | 2023 |
if (arc != INVALID) return direct(arc, false); |
2024 | 2024 |
} else { |
2025 | 2025 |
Edge arc = _digraph->findArc(t, s, p); |
2026 | 2026 |
if (arc != INVALID) return direct(arc, false); |
2027 | 2027 |
} |
2028 | 2028 |
return INVALID; |
2029 | 2029 |
} |
2030 | 2030 |
|
2031 | 2031 |
typedef FindArcTag FindEdgeTag; |
2032 | 2032 |
Edge findEdge(Node s, Node t, Edge p = INVALID) const { |
2033 | 2033 |
if (s != t) { |
2034 | 2034 |
if (p == INVALID) { |
2035 | 2035 |
Edge arc = _digraph->findArc(s, t); |
2036 | 2036 |
if (arc != INVALID) return arc; |
2037 | 2037 |
arc = _digraph->findArc(t, s); |
2038 | 2038 |
if (arc != INVALID) return arc; |
2039 | 2039 |
} else if (_digraph->source(p) == s) { |
2040 | 2040 |
Edge arc = _digraph->findArc(s, t, p); |
2041 | 2041 |
if (arc != INVALID) return arc; |
2042 | 2042 |
arc = _digraph->findArc(t, s); |
2043 | 2043 |
if (arc != INVALID) return arc; |
2044 | 2044 |
} else { |
2045 | 2045 |
Edge arc = _digraph->findArc(t, s, p); |
2046 | 2046 |
if (arc != INVALID) return arc; |
2047 | 2047 |
} |
2048 | 2048 |
} else { |
2049 | 2049 |
return _digraph->findArc(s, t, p); |
2050 | 2050 |
} |
2051 | 2051 |
return INVALID; |
2052 | 2052 |
} |
2053 | 2053 |
|
2054 | 2054 |
private: |
2055 | 2055 |
|
2056 | 2056 |
template <typename V> |
2057 | 2057 |
class ArcMapBase { |
2058 | 2058 |
private: |
2059 | 2059 |
|
2060 | 2060 |
typedef typename DGR::template ArcMap<V> MapImpl; |
2061 | 2061 |
|
2062 | 2062 |
public: |
2063 | 2063 |
|
2064 | 2064 |
typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag; |
2065 | 2065 |
|
2066 | 2066 |
typedef V Value; |
2067 | 2067 |
typedef Arc Key; |
2068 | 2068 |
typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReturnValue; |
2069 | 2069 |
typedef typename MapTraits<MapImpl>::ReturnValue ReturnValue; |
2070 | 2070 |
typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReference; |
2071 | 2071 |
typedef typename MapTraits<MapImpl>::ReturnValue Reference; |
2072 | 2072 |
|
2073 | 2073 |
ArcMapBase(const UndirectorBase<DGR>& adaptor) : |
2074 | 2074 |
_forward(*adaptor._digraph), _backward(*adaptor._digraph) {} |
2075 | 2075 |
|
2076 | 2076 |
ArcMapBase(const UndirectorBase<DGR>& adaptor, const V& value) |
2077 | 2077 |
: _forward(*adaptor._digraph, value), |
2078 | 2078 |
_backward(*adaptor._digraph, value) {} |
2079 | 2079 |
|
2080 | 2080 |
void set(const Arc& a, const V& value) { |
2081 | 2081 |
if (direction(a)) { |
2082 | 2082 |
_forward.set(a, value); |
2083 | 2083 |
} else { |
2084 | 2084 |
_backward.set(a, value); |
2085 | 2085 |
} |
2086 | 2086 |
} |
2087 | 2087 |
|
2088 | 2088 |
ConstReturnValue operator[](const Arc& a) const { |
2089 | 2089 |
if (direction(a)) { |
2090 | 2090 |
return _forward[a]; |
2091 | 2091 |
} else { |
2092 | 2092 |
return _backward[a]; |
2093 | 2093 |
} |
2094 | 2094 |
} |
2095 | 2095 |
|
2096 | 2096 |
ReturnValue operator[](const Arc& a) { |
2097 | 2097 |
if (direction(a)) { |
2098 | 2098 |
return _forward[a]; |
2099 | 2099 |
} else { |
2100 | 2100 |
return _backward[a]; |
2101 | 2101 |
} |
2102 | 2102 |
} |
2103 | 2103 |
|
2104 | 2104 |
protected: |
2105 | 2105 |
|
2106 | 2106 |
MapImpl _forward, _backward; |
2107 | 2107 |
|
2108 | 2108 |
}; |
2109 | 2109 |
|
2110 | 2110 |
public: |
2111 | 2111 |
|
2112 | 2112 |
template <typename V> |
2113 | 2113 |
class NodeMap : public DGR::template NodeMap<V> { |
2114 | 2114 |
public: |
2115 | 2115 |
|
2116 | 2116 |
typedef V Value; |
2117 | 2117 |
typedef typename DGR::template NodeMap<Value> Parent; |
2118 | 2118 |
|
2119 | 2119 |
explicit NodeMap(const UndirectorBase<DGR>& adaptor) |
2120 | 2120 |
: Parent(*adaptor._digraph) {} |
2121 | 2121 |
|
2122 | 2122 |
NodeMap(const UndirectorBase<DGR>& adaptor, const V& value) |
2123 | 2123 |
: Parent(*adaptor._digraph, value) { } |
2124 | 2124 |
|
2125 | 2125 |
private: |
2126 | 2126 |
NodeMap& operator=(const NodeMap& cmap) { |
2127 | 2127 |
return operator=<NodeMap>(cmap); |
2128 | 2128 |
} |
2129 | 2129 |
|
2130 | 2130 |
template <typename CMap> |
2131 | 2131 |
NodeMap& operator=(const CMap& cmap) { |
2132 | 2132 |
Parent::operator=(cmap); |
2133 | 2133 |
return *this; |
2134 | 2134 |
} |
2135 | 2135 |
|
2136 | 2136 |
}; |
2137 | 2137 |
|
2138 | 2138 |
template <typename V> |
2139 | 2139 |
class ArcMap |
2140 | 2140 |
: public SubMapExtender<UndirectorBase<DGR>, ArcMapBase<V> > |
2141 | 2141 |
{ |
2142 | 2142 |
public: |
2143 | 2143 |
typedef V Value; |
2144 | 2144 |
typedef SubMapExtender<Adaptor, ArcMapBase<V> > Parent; |
2145 | 2145 |
|
2146 | 2146 |
explicit ArcMap(const UndirectorBase<DGR>& adaptor) |
2147 | 2147 |
: Parent(adaptor) {} |
2148 | 2148 |
|
2149 | 2149 |
ArcMap(const UndirectorBase<DGR>& adaptor, const V& value) |
2150 | 2150 |
: Parent(adaptor, value) {} |
2151 | 2151 |
|
2152 | 2152 |
private: |
2153 | 2153 |
ArcMap& operator=(const ArcMap& cmap) { |
2154 | 2154 |
return operator=<ArcMap>(cmap); |
2155 | 2155 |
} |
2156 | 2156 |
|
2157 | 2157 |
template <typename CMap> |
2158 | 2158 |
ArcMap& operator=(const CMap& cmap) { |
2159 | 2159 |
Parent::operator=(cmap); |
2160 | 2160 |
return *this; |
2161 | 2161 |
} |
2162 | 2162 |
}; |
2163 | 2163 |
|
2164 | 2164 |
template <typename V> |
2165 | 2165 |
class EdgeMap : public Digraph::template ArcMap<V> { |
2166 | 2166 |
public: |
2167 | 2167 |
|
2168 | 2168 |
typedef V Value; |
2169 | 2169 |
typedef typename Digraph::template ArcMap<V> Parent; |
2170 | 2170 |
|
2171 | 2171 |
explicit EdgeMap(const UndirectorBase<DGR>& adaptor) |
2172 | 2172 |
: Parent(*adaptor._digraph) {} |
2173 | 2173 |
|
2174 | 2174 |
EdgeMap(const UndirectorBase<DGR>& adaptor, const V& value) |
2175 | 2175 |
: Parent(*adaptor._digraph, value) {} |
2176 | 2176 |
|
2177 | 2177 |
private: |
2178 | 2178 |
EdgeMap& operator=(const EdgeMap& cmap) { |
2179 | 2179 |
return operator=<EdgeMap>(cmap); |
2180 | 2180 |
} |
2181 | 2181 |
|
2182 | 2182 |
template <typename CMap> |
2183 | 2183 |
EdgeMap& operator=(const CMap& cmap) { |
2184 | 2184 |
Parent::operator=(cmap); |
2185 | 2185 |
return *this; |
2186 | 2186 |
} |
2187 | 2187 |
|
2188 | 2188 |
}; |
2189 | 2189 |
|
2190 | 2190 |
typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier; |
2191 | 2191 |
NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } |
2192 | 2192 |
|
2193 | 2193 |
typedef typename ItemSetTraits<DGR, Edge>::ItemNotifier EdgeNotifier; |
2194 | 2194 |
EdgeNotifier& notifier(Edge) const { return _digraph->notifier(Edge()); } |
2195 | 2195 |
|
2196 | 2196 |
protected: |
2197 | 2197 |
|
2198 | 2198 |
UndirectorBase() : _digraph(0) {} |
2199 | 2199 |
|
2200 | 2200 |
DGR* _digraph; |
2201 | 2201 |
|
2202 | 2202 |
void initialize(DGR& digraph) { |
2203 | 2203 |
_digraph = &digraph; |
2204 | 2204 |
} |
2205 | 2205 |
|
2206 | 2206 |
}; |
2207 | 2207 |
|
2208 | 2208 |
/// \ingroup graph_adaptors |
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); |
2389 | 2387 |
} |
2390 | 2388 |
void nextOut(Arc& i) const { |
2391 | 2389 |
bool d = (*_direction)[i]; |
2392 | 2390 |
_graph->nextInc(i, d); |
2393 | 2391 |
while (i != INVALID && d != (*_direction)[i]) _graph->nextInc(i, d); |
2394 | 2392 |
} |
2395 | 2393 |
|
2396 | 2394 |
Node source(const Arc& e) const { |
2397 | 2395 |
return (*_direction)[e] ? _graph->u(e) : _graph->v(e); |
2398 | 2396 |
} |
2399 | 2397 |
Node target(const Arc& e) const { |
2400 | 2398 |
return (*_direction)[e] ? _graph->v(e) : _graph->u(e); |
2401 | 2399 |
} |
2402 | 2400 |
|
2403 | 2401 |
typedef NodeNumTagIndicator<Graph> NodeNumTag; |
2404 | 2402 |
int nodeNum() const { return _graph->nodeNum(); } |
2405 | 2403 |
|
2406 | 2404 |
typedef EdgeNumTagIndicator<Graph> ArcNumTag; |
2407 | 2405 |
int arcNum() const { return _graph->edgeNum(); } |
2408 | 2406 |
|
2409 | 2407 |
typedef FindEdgeTagIndicator<Graph> FindArcTag; |
2410 | 2408 |
Arc findArc(const Node& u, const Node& v, |
2411 | 2409 |
const Arc& prev = INVALID) const { |
2412 | 2410 |
Arc arc = _graph->findEdge(u, v, prev); |
2413 | 2411 |
while (arc != INVALID && source(arc) != u) { |
2414 | 2412 |
arc = _graph->findEdge(u, v, arc); |
2415 | 2413 |
} |
2416 | 2414 |
return arc; |
2417 | 2415 |
} |
2418 | 2416 |
|
2419 | 2417 |
Node addNode() { |
2420 | 2418 |
return Node(_graph->addNode()); |
2421 | 2419 |
} |
2422 | 2420 |
|
2423 | 2421 |
Arc addArc(const Node& u, const Node& v) { |
2424 | 2422 |
Arc arc = _graph->addEdge(u, v); |
2425 | 2423 |
_direction->set(arc, _graph->u(arc) == u); |
2426 | 2424 |
return arc; |
2427 | 2425 |
} |
2428 | 2426 |
|
2429 | 2427 |
void erase(const Node& i) { _graph->erase(i); } |
2430 | 2428 |
void erase(const Arc& i) { _graph->erase(i); } |
2431 | 2429 |
|
2432 | 2430 |
void clear() { _graph->clear(); } |
2433 | 2431 |
|
2434 | 2432 |
int id(const Node& v) const { return _graph->id(v); } |
2435 | 2433 |
int id(const Arc& e) const { return _graph->id(e); } |
2436 | 2434 |
|
2437 | 2435 |
Node nodeFromId(int idx) const { return _graph->nodeFromId(idx); } |
2438 | 2436 |
Arc arcFromId(int idx) const { return _graph->edgeFromId(idx); } |
2439 | 2437 |
|
2440 | 2438 |
int maxNodeId() const { return _graph->maxNodeId(); } |
2441 | 2439 |
int maxArcId() const { return _graph->maxEdgeId(); } |
2442 | 2440 |
|
2443 | 2441 |
typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier; |
2444 | 2442 |
NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); } |
2445 | 2443 |
|
2446 | 2444 |
typedef typename ItemSetTraits<GR, Arc>::ItemNotifier ArcNotifier; |
2447 | 2445 |
ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); } |
2448 | 2446 |
|
2449 | 2447 |
template <typename V> |
2450 | 2448 |
class NodeMap : public GR::template NodeMap<V> { |
2451 | 2449 |
public: |
2452 | 2450 |
|
2453 | 2451 |
typedef typename GR::template NodeMap<V> Parent; |
2454 | 2452 |
|
2455 | 2453 |
explicit NodeMap(const OrienterBase<GR, DM>& adapter) |
2456 | 2454 |
: Parent(*adapter._graph) {} |
2457 | 2455 |
|
2458 | 2456 |
NodeMap(const OrienterBase<GR, DM>& adapter, const V& value) |
2459 | 2457 |
: Parent(*adapter._graph, value) {} |
2460 | 2458 |
|
2461 | 2459 |
private: |
2462 | 2460 |
NodeMap& operator=(const NodeMap& cmap) { |
2463 | 2461 |
return operator=<NodeMap>(cmap); |
2464 | 2462 |
} |
2465 | 2463 |
|
2466 | 2464 |
template <typename CMap> |
2467 | 2465 |
NodeMap& operator=(const CMap& cmap) { |
2468 | 2466 |
Parent::operator=(cmap); |
2469 | 2467 |
return *this; |
2470 | 2468 |
} |
2471 | 2469 |
|
2472 | 2470 |
}; |
2473 | 2471 |
|
2474 | 2472 |
template <typename V> |
2475 | 2473 |
class ArcMap : public GR::template EdgeMap<V> { |
2476 | 2474 |
public: |
2477 | 2475 |
|
2478 | 2476 |
typedef typename Graph::template EdgeMap<V> Parent; |
2479 | 2477 |
|
2480 | 2478 |
explicit ArcMap(const OrienterBase<GR, DM>& adapter) |
2481 | 2479 |
: Parent(*adapter._graph) { } |
2482 | 2480 |
|
2483 | 2481 |
ArcMap(const OrienterBase<GR, DM>& adapter, const V& value) |
2484 | 2482 |
: Parent(*adapter._graph, value) { } |
2485 | 2483 |
|
2486 | 2484 |
private: |
2487 | 2485 |
ArcMap& operator=(const ArcMap& cmap) { |
2488 | 2486 |
return operator=<ArcMap>(cmap); |
2489 | 2487 |
} |
2490 | 2488 |
|
2491 | 2489 |
template <typename CMap> |
2492 | 2490 |
ArcMap& operator=(const CMap& cmap) { |
2493 | 2491 |
Parent::operator=(cmap); |
2494 | 2492 |
return *this; |
2495 | 2493 |
} |
2496 | 2494 |
}; |
2497 | 2495 |
|
2498 | 2496 |
|
2499 | 2497 |
|
2500 | 2498 |
protected: |
2501 | 2499 |
Graph* _graph; |
2502 | 2500 |
DM* _direction; |
2503 | 2501 |
|
2504 | 2502 |
void initialize(GR& graph, DM& direction) { |
2505 | 2503 |
_graph = &graph; |
2506 | 2504 |
_direction = &direction; |
2507 | 2505 |
} |
2508 | 2506 |
|
2509 | 2507 |
}; |
2510 | 2508 |
|
2511 | 2509 |
/// \ingroup graph_adaptors |
2512 | 2510 |
/// |
2513 | 2511 |
/// \brief Adaptor class for orienting the edges of a graph to get a digraph |
2514 | 2512 |
/// |
2515 | 2513 |
/// Orienter adaptor can be used for orienting the edges of a graph to |
2516 | 2514 |
/// get a digraph. A \c bool edge map of the underlying graph must be |
2517 | 2515 |
/// specified, which define the direction of the arcs in the adaptor. |
2518 | 2516 |
/// The arcs can be easily reversed by the \c reverseArc() member function |
2519 | 2517 |
/// of the adaptor. |
2520 | 2518 |
/// This class conforms to the \ref concepts::Digraph "Digraph" concept. |
2521 | 2519 |
/// |
2522 | 2520 |
/// The adapted graph can also be modified through this adaptor |
2523 | 2521 |
/// by adding or removing nodes or arcs, unless the \c GR template |
2524 | 2522 |
/// parameter is set to be \c const. |
2525 | 2523 |
/// |
2526 | 2524 |
/// \tparam GR The type of the adapted graph. |
2527 | 2525 |
/// It must conform to the \ref concepts::Graph "Graph" concept. |
2528 | 2526 |
/// It can also be specified to be \c const. |
2529 | 2527 |
/// \tparam DM The type of the direction map. |
2530 | 2528 |
/// It must be a \c bool (or convertible) edge map of the |
2531 | 2529 |
/// adapted graph. The default type is |
2532 | 2530 |
/// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
2533 | 2531 |
/// |
2534 | 2532 |
/// \note The \c Node type of this adaptor and the adapted graph are |
2535 | 2533 |
/// convertible to each other, moreover the \c Arc type of the adaptor |
2536 | 2534 |
/// and the \c Edge type of the adapted graph are also convertible to |
2537 | 2535 |
/// each other. |
2538 | 2536 |
#ifdef DOXYGEN |
2539 | 2537 |
template<typename GR, |
2540 | 2538 |
typename DM> |
2541 | 2539 |
class Orienter { |
2542 | 2540 |
#else |
2543 | 2541 |
template<typename GR, |
2544 | 2542 |
typename DM = typename GR::template EdgeMap<bool> > |
2545 | 2543 |
class Orienter : |
2546 | 2544 |
public DigraphAdaptorExtender<OrienterBase<GR, DM> > { |
2547 | 2545 |
#endif |
2548 | 2546 |
public: |
2549 | 2547 |
|
2550 | 2548 |
/// The type of the adapted graph. |
2551 | 2549 |
typedef GR Graph; |
2552 | 2550 |
/// The type of the direction edge map. |
2553 | 2551 |
typedef DM DirectionMap; |
2554 | 2552 |
|
2555 | 2553 |
typedef DigraphAdaptorExtender<OrienterBase<GR, DM> > Parent; |
2556 | 2554 |
typedef typename Parent::Arc Arc; |
2557 | 2555 |
protected: |
2558 | 2556 |
Orienter() { } |
2559 | 2557 |
public: |
2560 | 2558 |
|
2561 | 2559 |
/// \brief Constructor |
2562 | 2560 |
/// |
2563 | 2561 |
/// Constructor of the adaptor. |
2564 | 2562 |
Orienter(GR& graph, DM& direction) { |
2565 | 2563 |
Parent::initialize(graph, direction); |
2566 | 2564 |
} |
2567 | 2565 |
|
2568 | 2566 |
/// \brief Reverses the given arc |
2569 | 2567 |
/// |
2570 | 2568 |
/// This function reverses the given arc. |
2571 | 2569 |
/// It is done by simply negate the assigned value of \c a |
2572 | 2570 |
/// in the direction map. |
2573 | 2571 |
void reverseArc(const Arc& a) { |
2574 | 2572 |
Parent::reverseArc(a); |
2575 | 2573 |
} |
2576 | 2574 |
}; |
2577 | 2575 |
|
2578 | 2576 |
/// \brief Returns a read-only Orienter adaptor |
2579 | 2577 |
/// |
2580 | 2578 |
/// This function just returns a read-only \ref Orienter adaptor. |
2581 | 2579 |
/// \ingroup graph_adaptors |
2582 | 2580 |
/// \relates Orienter |
2583 | 2581 |
template<typename GR, typename DM> |
2584 | 2582 |
Orienter<const GR, DM> |
2585 | 2583 |
orienter(const GR& graph, DM& direction) { |
2586 | 2584 |
return Orienter<const GR, DM>(graph, direction); |
2587 | 2585 |
} |
2588 | 2586 |
|
2589 | 2587 |
template<typename GR, typename DM> |
2590 | 2588 |
Orienter<const GR, const DM> |
2591 | 2589 |
orienter(const GR& graph, const DM& direction) { |
2592 | 2590 |
return Orienter<const GR, const DM>(graph, direction); |
2593 | 2591 |
} |
2594 | 2592 |
|
2595 | 2593 |
namespace _adaptor_bits { |
2596 | 2594 |
|
2597 | 2595 |
template <typename DGR, typename CM, typename FM, typename TL> |
2598 | 2596 |
class ResForwardFilter { |
2599 | 2597 |
public: |
2600 | 2598 |
|
2601 | 2599 |
typedef typename DGR::Arc Key; |
2602 | 2600 |
typedef bool Value; |
2603 | 2601 |
|
2604 | 2602 |
private: |
2605 | 2603 |
|
2606 | 2604 |
const CM* _capacity; |
2607 | 2605 |
const FM* _flow; |
2608 | 2606 |
TL _tolerance; |
2609 | 2607 |
|
2610 | 2608 |
public: |
2611 | 2609 |
|
2612 | 2610 |
ResForwardFilter(const CM& capacity, const FM& flow, |
2613 | 2611 |
const TL& tolerance = TL()) |
2614 | 2612 |
: _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } |
2615 | 2613 |
|
2616 | 2614 |
bool operator[](const typename DGR::Arc& a) const { |
2617 | 2615 |
return _tolerance.positive((*_capacity)[a] - (*_flow)[a]); |
2618 | 2616 |
} |
2619 | 2617 |
}; |
2620 | 2618 |
|
2621 | 2619 |
template<typename DGR,typename CM, typename FM, typename TL> |
2622 | 2620 |
class ResBackwardFilter { |
2623 | 2621 |
public: |
2624 | 2622 |
|
2625 | 2623 |
typedef typename DGR::Arc Key; |
2626 | 2624 |
typedef bool Value; |
2627 | 2625 |
|
2628 | 2626 |
private: |
2629 | 2627 |
|
2630 | 2628 |
const CM* _capacity; |
2631 | 2629 |
const FM* _flow; |
2632 | 2630 |
TL _tolerance; |
2633 | 2631 |
|
2634 | 2632 |
public: |
2635 | 2633 |
|
2636 | 2634 |
ResBackwardFilter(const CM& capacity, const FM& flow, |
2637 | 2635 |
const TL& tolerance = TL()) |
2638 | 2636 |
: _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } |
2639 | 2637 |
|
2640 | 2638 |
bool operator[](const typename DGR::Arc& a) const { |
2641 | 2639 |
return _tolerance.positive((*_flow)[a]); |
2642 | 2640 |
} |
2643 | 2641 |
}; |
2644 | 2642 |
|
2645 | 2643 |
} |
2646 | 2644 |
|
2647 | 2645 |
/// \ingroup graph_adaptors |
2648 | 2646 |
/// |
2649 | 2647 |
/// \brief Adaptor class for composing the residual digraph for directed |
2650 | 2648 |
/// flow and circulation problems. |
2651 | 2649 |
/// |
2652 | 2650 |
/// ResidualDigraph can be used for composing the \e residual digraph |
2653 | 2651 |
/// for directed flow and circulation problems. Let \f$ G=(V, A) \f$ |
2654 | 2652 |
/// be a directed graph and let \f$ F \f$ be a number type. |
2655 | 2653 |
/// Let \f$ flow, cap: A\to F \f$ be functions on the arcs. |
2656 | 2654 |
/// This adaptor implements a digraph structure with node set \f$ V \f$ |
2657 | 2655 |
/// and arc set \f$ A_{forward}\cup A_{backward} \f$, |
2658 | 2656 |
/// where \f$ A_{forward}=\{uv : uv\in A, flow(uv)<cap(uv)\} \f$ and |
2659 | 2657 |
/// \f$ A_{backward}=\{vu : uv\in A, flow(uv)>0\} \f$, i.e. the so |
2660 | 2658 |
/// called residual digraph. |
2661 | 2659 |
/// When the union \f$ A_{forward}\cup A_{backward} \f$ is taken, |
2662 | 2660 |
/// multiplicities are counted, i.e. the adaptor has exactly |
2663 | 2661 |
/// \f$ |A_{forward}| + |A_{backward}|\f$ arcs (it may have parallel |
2664 | 2662 |
/// arcs). |
2665 | 2663 |
/// This class conforms to the \ref concepts::Digraph "Digraph" concept. |
2666 | 2664 |
/// |
2667 | 2665 |
/// \tparam DGR The type of the adapted digraph. |
2668 | 2666 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
2669 | 2667 |
/// It is implicitly \c const. |
2670 | 2668 |
/// \tparam CM The type of the capacity map. |
2671 | 2669 |
/// It must be an arc map of some numerical type, which defines |
2672 | 2670 |
/// the capacities in the flow problem. It is implicitly \c const. |
2673 | 2671 |
/// The default type is |
2674 | 2672 |
/// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
2675 | 2673 |
/// \tparam FM The type of the flow map. |
2676 | 2674 |
/// It must be an arc map of some numerical type, which defines |
2677 | 2675 |
/// the flow values in the flow problem. The default type is \c CM. |
2678 | 2676 |
/// \tparam TL The tolerance type for handling inexact computation. |
2679 | 2677 |
/// The default tolerance type depends on the value type of the |
2680 | 2678 |
/// capacity map. |
2681 | 2679 |
/// |
2682 | 2680 |
/// \note This adaptor is implemented using Undirector and FilterArcs |
2683 | 2681 |
/// adaptors. |
2684 | 2682 |
/// |
2685 | 2683 |
/// \note The \c Node type of this adaptor and the adapted digraph are |
2686 | 2684 |
/// convertible to each other, moreover the \c Arc type of the adaptor |
2687 | 2685 |
/// is convertible to the \c Arc type of the adapted digraph. |
2688 | 2686 |
#ifdef DOXYGEN |
2689 | 2687 |
template<typename DGR, typename CM, typename FM, typename TL> |
2690 | 2688 |
class ResidualDigraph |
2691 | 2689 |
#else |
2692 | 2690 |
template<typename DGR, |
2693 | 2691 |
typename CM = typename DGR::template ArcMap<int>, |
2694 | 2692 |
typename FM = CM, |
2695 | 2693 |
typename TL = Tolerance<typename CM::Value> > |
2696 | 2694 |
class ResidualDigraph |
2697 | 2695 |
: public SubDigraph< |
2698 | 2696 |
Undirector<const DGR>, |
2699 | 2697 |
ConstMap<typename DGR::Node, Const<bool, true> >, |
2700 | 2698 |
typename Undirector<const DGR>::template CombinedArcMap< |
2701 | 2699 |
_adaptor_bits::ResForwardFilter<const DGR, CM, FM, TL>, |
2702 | 2700 |
_adaptor_bits::ResBackwardFilter<const DGR, CM, FM, TL> > > |
2703 | 2701 |
#endif |
2704 | 2702 |
{ |
2705 | 2703 |
public: |
2706 | 2704 |
|
2707 | 2705 |
/// The type of the underlying digraph. |
2708 | 2706 |
typedef DGR Digraph; |
2709 | 2707 |
/// The type of the capacity map. |
2710 | 2708 |
typedef CM CapacityMap; |
2711 | 2709 |
/// The type of the flow map. |
2712 | 2710 |
typedef FM FlowMap; |
2713 | 2711 |
/// The tolerance type. |
2714 | 2712 |
typedef TL Tolerance; |
2715 | 2713 |
|
2716 | 2714 |
typedef typename CapacityMap::Value Value; |
2717 | 2715 |
typedef ResidualDigraph Adaptor; |
2718 | 2716 |
|
2719 | 2717 |
protected: |
2720 | 2718 |
|
2721 | 2719 |
typedef Undirector<const Digraph> Undirected; |
2722 | 2720 |
|
2723 | 2721 |
typedef ConstMap<typename DGR::Node, Const<bool, true> > NodeFilter; |
2724 | 2722 |
|
2725 | 2723 |
typedef _adaptor_bits::ResForwardFilter<const DGR, CM, |
2726 | 2724 |
FM, TL> ForwardFilter; |
2727 | 2725 |
|
2728 | 2726 |
typedef _adaptor_bits::ResBackwardFilter<const DGR, CM, |
2729 | 2727 |
FM, TL> BackwardFilter; |
2730 | 2728 |
|
2731 | 2729 |
typedef typename Undirected:: |
2732 | 2730 |
template CombinedArcMap<ForwardFilter, BackwardFilter> ArcFilter; |
2733 | 2731 |
|
2734 | 2732 |
typedef SubDigraph<Undirected, NodeFilter, ArcFilter> Parent; |
2735 | 2733 |
|
2736 | 2734 |
const CapacityMap* _capacity; |
2737 | 2735 |
FlowMap* _flow; |
2738 | 2736 |
|
2739 | 2737 |
Undirected _graph; |
2740 | 2738 |
NodeFilter _node_filter; |
2741 | 2739 |
ForwardFilter _forward_filter; |
2742 | 2740 |
BackwardFilter _backward_filter; |
2743 | 2741 |
ArcFilter _arc_filter; |
2744 | 2742 |
|
2745 | 2743 |
public: |
2746 | 2744 |
|
2747 | 2745 |
/// \brief Constructor |
2748 | 2746 |
/// |
2749 | 2747 |
/// Constructor of the residual digraph adaptor. The parameters are the |
2750 | 2748 |
/// digraph, the capacity map, the flow map, and a tolerance object. |
2751 | 2749 |
ResidualDigraph(const DGR& digraph, const CM& capacity, |
2752 | 2750 |
FM& flow, const TL& tolerance = Tolerance()) |
2753 | 2751 |
: Parent(), _capacity(&capacity), _flow(&flow), |
2754 | 2752 |
_graph(digraph), _node_filter(), |
2755 | 2753 |
_forward_filter(capacity, flow, tolerance), |
2756 | 2754 |
_backward_filter(capacity, flow, tolerance), |
2757 | 2755 |
_arc_filter(_forward_filter, _backward_filter) |
2758 | 2756 |
{ |
2759 | 2757 |
Parent::initialize(_graph, _node_filter, _arc_filter); |
2760 | 2758 |
} |
2761 | 2759 |
|
2762 | 2760 |
typedef typename Parent::Arc Arc; |
2763 | 2761 |
|
2764 | 2762 |
/// \brief Returns the residual capacity of the given arc. |
2765 | 2763 |
/// |
2766 | 2764 |
/// Returns the residual capacity of the given arc. |
2767 | 2765 |
Value residualCapacity(const Arc& a) const { |
2768 | 2766 |
if (Undirected::direction(a)) { |
2769 | 2767 |
return (*_capacity)[a] - (*_flow)[a]; |
2770 | 2768 |
} else { |
2771 | 2769 |
return (*_flow)[a]; |
2772 | 2770 |
} |
2773 | 2771 |
} |
2774 | 2772 |
|
2775 | 2773 |
/// \brief Augments on the given arc in the residual digraph. |
2776 | 2774 |
/// |
2777 | 2775 |
/// Augments on the given arc in the residual digraph. It increases |
2778 | 2776 |
/// or decreases the flow value on the original arc according to the |
2779 | 2777 |
/// direction of the residual arc. |
2780 | 2778 |
void augment(const Arc& a, const Value& v) const { |
2781 | 2779 |
if (Undirected::direction(a)) { |
2782 | 2780 |
_flow->set(a, (*_flow)[a] + v); |
2783 | 2781 |
} else { |
2784 | 2782 |
_flow->set(a, (*_flow)[a] - v); |
2785 | 2783 |
} |
2786 | 2784 |
} |
2787 | 2785 |
|
2788 | 2786 |
/// \brief Returns \c true if the given residual arc is a forward arc. |
2789 | 2787 |
/// |
2790 | 2788 |
/// Returns \c true if the given residual arc has the same orientation |
2791 | 2789 |
/// as the original arc, i.e. it is a so called forward arc. |
2792 | 2790 |
static bool forward(const Arc& a) { |
2793 | 2791 |
return Undirected::direction(a); |
2794 | 2792 |
} |
2795 | 2793 |
|
2796 | 2794 |
/// \brief Returns \c true if the given residual arc is a backward arc. |
2797 | 2795 |
/// |
2798 | 2796 |
/// Returns \c true if the given residual arc has the opposite orientation |
2799 | 2797 |
/// than the original arc, i.e. it is a so called backward arc. |
2800 | 2798 |
static bool backward(const Arc& a) { |
2801 | 2799 |
return !Undirected::direction(a); |
2802 | 2800 |
} |
2803 | 2801 |
|
2804 | 2802 |
/// \brief Returns the forward oriented residual arc. |
2805 | 2803 |
/// |
2806 | 2804 |
/// Returns the forward oriented residual arc related to the given |
2807 | 2805 |
/// arc of the underlying digraph. |
2808 | 2806 |
static Arc forward(const typename Digraph::Arc& a) { |
2809 | 2807 |
return Undirected::direct(a, true); |
2810 | 2808 |
} |
2811 | 2809 |
|
2812 | 2810 |
/// \brief Returns the backward oriented residual arc. |
2813 | 2811 |
/// |
2814 | 2812 |
/// Returns the backward oriented residual arc related to the given |
2815 | 2813 |
/// arc of the underlying digraph. |
2816 | 2814 |
static Arc backward(const typename Digraph::Arc& a) { |
2817 | 2815 |
return Undirected::direct(a, false); |
2818 | 2816 |
} |
2819 | 2817 |
|
2820 | 2818 |
/// \brief Residual capacity map. |
2821 | 2819 |
/// |
2822 | 2820 |
/// This map adaptor class can be used for obtaining the residual |
2823 | 2821 |
/// capacities as an arc map of the residual digraph. |
2824 | 2822 |
/// Its value type is inherited from the capacity map. |
2825 | 2823 |
class ResidualCapacity { |
2826 | 2824 |
protected: |
2827 | 2825 |
const Adaptor* _adaptor; |
2828 | 2826 |
public: |
2829 | 2827 |
/// The key type of the map |
2830 | 2828 |
typedef Arc Key; |
2831 | 2829 |
/// The value type of the map |
2832 | 2830 |
typedef typename CapacityMap::Value Value; |
2833 | 2831 |
|
2834 | 2832 |
/// Constructor |
2835 | 2833 |
ResidualCapacity(const ResidualDigraph<DGR, CM, FM, TL>& adaptor) |
2836 | 2834 |
: _adaptor(&adaptor) {} |
2837 | 2835 |
|
2838 | 2836 |
/// Returns the value associated with the given residual arc |
2839 | 2837 |
Value operator[](const Arc& a) const { |
2840 | 2838 |
return _adaptor->residualCapacity(a); |
2841 | 2839 |
} |
2842 | 2840 |
|
2843 | 2841 |
}; |
2844 | 2842 |
|
2845 | 2843 |
/// \brief Returns a residual capacity map |
2846 | 2844 |
/// |
2847 | 2845 |
/// This function just returns a residual capacity map. |
2848 | 2846 |
ResidualCapacity residualCapacity() const { |
2849 | 2847 |
return ResidualCapacity(*this); |
2850 | 2848 |
} |
2851 | 2849 |
|
2852 | 2850 |
}; |
2853 | 2851 |
|
2854 | 2852 |
/// \brief Returns a (read-only) Residual adaptor |
2855 | 2853 |
/// |
2856 | 2854 |
/// This function just returns a (read-only) \ref ResidualDigraph adaptor. |
2857 | 2855 |
/// \ingroup graph_adaptors |
2858 | 2856 |
/// \relates ResidualDigraph |
2859 | 2857 |
template<typename DGR, typename CM, typename FM> |
2860 | 2858 |
ResidualDigraph<DGR, CM, FM> |
2861 | 2859 |
residualDigraph(const DGR& digraph, const CM& capacity_map, FM& flow_map) { |
2862 | 2860 |
return ResidualDigraph<DGR, CM, FM> (digraph, capacity_map, flow_map); |
2863 | 2861 |
} |
2864 | 2862 |
|
2865 | 2863 |
|
2866 | 2864 |
template <typename DGR> |
2867 | 2865 |
class SplitNodesBase { |
2868 | 2866 |
public: |
2869 | 2867 |
|
2870 | 2868 |
typedef DGR Digraph; |
2871 | 2869 |
typedef DigraphAdaptorBase<const DGR> Parent; |
2872 | 2870 |
typedef SplitNodesBase Adaptor; |
2873 | 2871 |
|
2874 | 2872 |
typedef typename DGR::Node DigraphNode; |
2875 | 2873 |
typedef typename DGR::Arc DigraphArc; |
2876 | 2874 |
|
2877 | 2875 |
class Node; |
2878 | 2876 |
class Arc; |
2879 | 2877 |
|
2880 | 2878 |
private: |
2881 | 2879 |
|
2882 | 2880 |
template <typename T> class NodeMapBase; |
2883 | 2881 |
template <typename T> class ArcMapBase; |
2884 | 2882 |
|
2885 | 2883 |
public: |
2886 | 2884 |
|
2887 | 2885 |
class Node : public DigraphNode { |
2888 | 2886 |
friend class SplitNodesBase; |
2889 | 2887 |
template <typename T> friend class NodeMapBase; |
2890 | 2888 |
private: |
2891 | 2889 |
|
2892 | 2890 |
bool _in; |
2893 | 2891 |
Node(DigraphNode node, bool in) |
2894 | 2892 |
: DigraphNode(node), _in(in) {} |
2895 | 2893 |
|
2896 | 2894 |
public: |
2897 | 2895 |
|
2898 | 2896 |
Node() {} |
2899 | 2897 |
Node(Invalid) : DigraphNode(INVALID), _in(true) {} |
2900 | 2898 |
|
2901 | 2899 |
bool operator==(const Node& node) const { |
2902 | 2900 |
return DigraphNode::operator==(node) && _in == node._in; |
2903 | 2901 |
} |
2904 | 2902 |
|
2905 | 2903 |
bool operator!=(const Node& node) const { |
2906 | 2904 |
return !(*this == node); |
2907 | 2905 |
} |
2908 | 2906 |
|
2909 | 2907 |
bool operator<(const Node& node) const { |
2910 | 2908 |
return DigraphNode::operator<(node) || |
2911 | 2909 |
(DigraphNode::operator==(node) && _in < node._in); |
2912 | 2910 |
} |
2913 | 2911 |
}; |
2914 | 2912 |
|
2915 | 2913 |
class Arc { |
2916 | 2914 |
friend class SplitNodesBase; |
2917 | 2915 |
template <typename T> friend class ArcMapBase; |
2918 | 2916 |
private: |
2919 | 2917 |
typedef BiVariant<DigraphArc, DigraphNode> ArcImpl; |
2920 | 2918 |
|
2921 | 2919 |
explicit Arc(const DigraphArc& arc) : _item(arc) {} |
2922 | 2920 |
explicit Arc(const DigraphNode& node) : _item(node) {} |
2923 | 2921 |
|
2924 | 2922 |
ArcImpl _item; |
2925 | 2923 |
|
2926 | 2924 |
public: |
2927 | 2925 |
Arc() {} |
2928 | 2926 |
Arc(Invalid) : _item(DigraphArc(INVALID)) {} |
2929 | 2927 |
|
2930 | 2928 |
bool operator==(const Arc& arc) const { |
2931 | 2929 |
if (_item.firstState()) { |
2932 | 2930 |
if (arc._item.firstState()) { |
2933 | 2931 |
return _item.first() == arc._item.first(); |
2934 | 2932 |
} |
2935 | 2933 |
} else { |
2936 | 2934 |
if (arc._item.secondState()) { |
2937 | 2935 |
return _item.second() == arc._item.second(); |
2938 | 2936 |
} |
2939 | 2937 |
} |
2940 | 2938 |
return false; |
2941 | 2939 |
} |
2942 | 2940 |
|
2943 | 2941 |
bool operator!=(const Arc& arc) const { |
2944 | 2942 |
return !(*this == arc); |
2945 | 2943 |
} |
2946 | 2944 |
|
2947 | 2945 |
bool operator<(const Arc& arc) const { |
2948 | 2946 |
if (_item.firstState()) { |
2949 | 2947 |
if (arc._item.firstState()) { |
2950 | 2948 |
return _item.first() < arc._item.first(); |
2951 | 2949 |
} |
2952 | 2950 |
return false; |
2953 | 2951 |
} else { |
2954 | 2952 |
if (arc._item.secondState()) { |
2955 | 2953 |
return _item.second() < arc._item.second(); |
2956 | 2954 |
} |
2957 | 2955 |
return true; |
2958 | 2956 |
} |
2959 | 2957 |
} |
2960 | 2958 |
|
2961 | 2959 |
operator DigraphArc() const { return _item.first(); } |
2962 | 2960 |
operator DigraphNode() const { return _item.second(); } |
2963 | 2961 |
|
2964 | 2962 |
}; |
2965 | 2963 |
|
2966 | 2964 |
void first(Node& n) const { |
2967 | 2965 |
_digraph->first(n); |
2968 | 2966 |
n._in = true; |
2969 | 2967 |
} |
2970 | 2968 |
|
2971 | 2969 |
void next(Node& n) const { |
2972 | 2970 |
if (n._in) { |
2973 | 2971 |
n._in = false; |
2974 | 2972 |
} else { |
2975 | 2973 |
n._in = true; |
2976 | 2974 |
_digraph->next(n); |
2977 | 2975 |
} |
2978 | 2976 |
} |
2979 | 2977 |
|
2980 | 2978 |
void first(Arc& e) const { |
2981 | 2979 |
e._item.setSecond(); |
2982 | 2980 |
_digraph->first(e._item.second()); |
2983 | 2981 |
if (e._item.second() == INVALID) { |
2984 | 2982 |
e._item.setFirst(); |
2985 | 2983 |
_digraph->first(e._item.first()); |
2986 | 2984 |
} |
2987 | 2985 |
} |
2988 | 2986 |
|
2989 | 2987 |
void next(Arc& e) const { |
2990 | 2988 |
if (e._item.secondState()) { |
2991 | 2989 |
_digraph->next(e._item.second()); |
2992 | 2990 |
if (e._item.second() == INVALID) { |
2993 | 2991 |
e._item.setFirst(); |
2994 | 2992 |
_digraph->first(e._item.first()); |
2995 | 2993 |
} |
2996 | 2994 |
} else { |
2997 | 2995 |
_digraph->next(e._item.first()); |
2998 | 2996 |
} |
2999 | 2997 |
} |
3000 | 2998 |
|
3001 | 2999 |
void firstOut(Arc& e, const Node& n) const { |
3002 | 3000 |
if (n._in) { |
3003 | 3001 |
e._item.setSecond(n); |
3004 | 3002 |
} else { |
3005 | 3003 |
e._item.setFirst(); |
3006 | 3004 |
_digraph->firstOut(e._item.first(), n); |
3007 | 3005 |
} |
3008 | 3006 |
} |
3009 | 3007 |
|
3010 | 3008 |
void nextOut(Arc& e) const { |
3011 | 3009 |
if (!e._item.firstState()) { |
3012 | 3010 |
e._item.setFirst(INVALID); |
3013 | 3011 |
} else { |
3014 | 3012 |
_digraph->nextOut(e._item.first()); |
3015 | 3013 |
} |
3016 | 3014 |
} |
3017 | 3015 |
|
3018 | 3016 |
void firstIn(Arc& e, const Node& n) const { |
3019 | 3017 |
if (!n._in) { |
3020 | 3018 |
e._item.setSecond(n); |
3021 | 3019 |
} else { |
3022 | 3020 |
e._item.setFirst(); |
3023 | 3021 |
_digraph->firstIn(e._item.first(), n); |
3024 | 3022 |
} |
3025 | 3023 |
} |
3026 | 3024 |
|
3027 | 3025 |
void nextIn(Arc& e) const { |
3028 | 3026 |
if (!e._item.firstState()) { |
3029 | 3027 |
e._item.setFirst(INVALID); |
3030 | 3028 |
} else { |
3031 | 3029 |
_digraph->nextIn(e._item.first()); |
3032 | 3030 |
} |
3033 | 3031 |
} |
3034 | 3032 |
|
3035 | 3033 |
Node source(const Arc& e) const { |
3036 | 3034 |
if (e._item.firstState()) { |
3037 | 3035 |
return Node(_digraph->source(e._item.first()), false); |
3038 | 3036 |
} else { |
3039 | 3037 |
return Node(e._item.second(), true); |
3040 | 3038 |
} |
3041 | 3039 |
} |
3042 | 3040 |
|
3043 | 3041 |
Node target(const Arc& e) const { |
3044 | 3042 |
if (e._item.firstState()) { |
3045 | 3043 |
return Node(_digraph->target(e._item.first()), true); |
3046 | 3044 |
} else { |
3047 | 3045 |
return Node(e._item.second(), false); |
3048 | 3046 |
} |
3049 | 3047 |
} |
3050 | 3048 |
|
3051 | 3049 |
int id(const Node& n) const { |
3052 | 3050 |
return (_digraph->id(n) << 1) | (n._in ? 0 : 1); |
3053 | 3051 |
} |
3054 | 3052 |
Node nodeFromId(int ix) const { |
3055 | 3053 |
return Node(_digraph->nodeFromId(ix >> 1), (ix & 1) == 0); |
3056 | 3054 |
} |
3057 | 3055 |
int maxNodeId() const { |
3058 | 3056 |
return 2 * _digraph->maxNodeId() + 1; |
3059 | 3057 |
} |
3060 | 3058 |
|
3061 | 3059 |
int id(const Arc& e) const { |
3062 | 3060 |
if (e._item.firstState()) { |
3063 | 3061 |
return _digraph->id(e._item.first()) << 1; |
3064 | 3062 |
} else { |
3065 | 3063 |
return (_digraph->id(e._item.second()) << 1) | 1; |
3066 | 3064 |
} |
3067 | 3065 |
} |
3068 | 3066 |
Arc arcFromId(int ix) const { |
3069 | 3067 |
if ((ix & 1) == 0) { |
3070 | 3068 |
return Arc(_digraph->arcFromId(ix >> 1)); |
3071 | 3069 |
} else { |
3072 | 3070 |
return Arc(_digraph->nodeFromId(ix >> 1)); |
3073 | 3071 |
} |
3074 | 3072 |
} |
3075 | 3073 |
int maxArcId() const { |
3076 | 3074 |
return std::max(_digraph->maxNodeId() << 1, |
3077 | 3075 |
(_digraph->maxArcId() << 1) | 1); |
3078 | 3076 |
} |
3079 | 3077 |
|
3080 | 3078 |
static bool inNode(const Node& n) { |
3081 | 3079 |
return n._in; |
3082 | 3080 |
} |
3083 | 3081 |
|
3084 | 3082 |
static bool outNode(const Node& n) { |
3085 | 3083 |
return !n._in; |
3086 | 3084 |
} |
3087 | 3085 |
|
3088 | 3086 |
static bool origArc(const Arc& e) { |
3089 | 3087 |
return e._item.firstState(); |
3090 | 3088 |
} |
3091 | 3089 |
|
3092 | 3090 |
static bool bindArc(const Arc& e) { |
3093 | 3091 |
return e._item.secondState(); |
3094 | 3092 |
} |
3095 | 3093 |
|
3096 | 3094 |
static Node inNode(const DigraphNode& n) { |
3097 | 3095 |
return Node(n, true); |
3098 | 3096 |
} |
3099 | 3097 |
|
3100 | 3098 |
static Node outNode(const DigraphNode& n) { |
3101 | 3099 |
return Node(n, false); |
3102 | 3100 |
} |
3103 | 3101 |
|
3104 | 3102 |
static Arc arc(const DigraphNode& n) { |
3105 | 3103 |
return Arc(n); |
3106 | 3104 |
} |
3107 | 3105 |
|
3108 | 3106 |
static Arc arc(const DigraphArc& e) { |
3109 | 3107 |
return Arc(e); |
3110 | 3108 |
} |
3111 | 3109 |
|
3112 | 3110 |
typedef True NodeNumTag; |
3113 | 3111 |
int nodeNum() const { |
3114 | 3112 |
return 2 * countNodes(*_digraph); |
3115 | 3113 |
} |
3116 | 3114 |
|
3117 | 3115 |
typedef True ArcNumTag; |
3118 | 3116 |
int arcNum() const { |
3119 | 3117 |
return countArcs(*_digraph) + countNodes(*_digraph); |
3120 | 3118 |
} |
3121 | 3119 |
|
3122 | 3120 |
typedef True FindArcTag; |
3123 | 3121 |
Arc findArc(const Node& u, const Node& v, |
3124 | 3122 |
const Arc& prev = INVALID) const { |
3125 | 3123 |
if (inNode(u) && outNode(v)) { |
3126 | 3124 |
if (static_cast<const DigraphNode&>(u) == |
3127 | 3125 |
static_cast<const DigraphNode&>(v) && prev == INVALID) { |
3128 | 3126 |
return Arc(u); |
3129 | 3127 |
} |
3130 | 3128 |
} |
3131 | 3129 |
else if (outNode(u) && inNode(v)) { |
3132 | 3130 |
return Arc(::lemon::findArc(*_digraph, u, v, prev)); |
3133 | 3131 |
} |
3134 | 3132 |
return INVALID; |
3135 | 3133 |
} |
3136 | 3134 |
|
3137 | 3135 |
private: |
3138 | 3136 |
|
3139 | 3137 |
template <typename V> |
3140 | 3138 |
class NodeMapBase |
3141 | 3139 |
: public MapTraits<typename Parent::template NodeMap<V> > { |
3142 | 3140 |
typedef typename Parent::template NodeMap<V> NodeImpl; |
3143 | 3141 |
public: |
3144 | 3142 |
typedef Node Key; |
3145 | 3143 |
typedef V Value; |
3146 | 3144 |
typedef typename MapTraits<NodeImpl>::ReferenceMapTag ReferenceMapTag; |
3147 | 3145 |
typedef typename MapTraits<NodeImpl>::ReturnValue ReturnValue; |
3148 | 3146 |
typedef typename MapTraits<NodeImpl>::ConstReturnValue ConstReturnValue; |
3149 | 3147 |
typedef typename MapTraits<NodeImpl>::ReturnValue Reference; |
3150 | 3148 |
typedef typename MapTraits<NodeImpl>::ConstReturnValue ConstReference; |
3151 | 3149 |
|
3152 | 3150 |
NodeMapBase(const SplitNodesBase<DGR>& adaptor) |
3153 | 3151 |
: _in_map(*adaptor._digraph), _out_map(*adaptor._digraph) {} |
3154 | 3152 |
NodeMapBase(const SplitNodesBase<DGR>& adaptor, const V& value) |
3155 | 3153 |
: _in_map(*adaptor._digraph, value), |
3156 | 3154 |
_out_map(*adaptor._digraph, value) {} |
3157 | 3155 |
|
3158 | 3156 |
void set(const Node& key, const V& val) { |
3159 | 3157 |
if (SplitNodesBase<DGR>::inNode(key)) { _in_map.set(key, val); } |
3160 | 3158 |
else {_out_map.set(key, val); } |
3161 | 3159 |
} |
3162 | 3160 |
|
3163 | 3161 |
ReturnValue operator[](const Node& key) { |
3164 | 3162 |
if (SplitNodesBase<DGR>::inNode(key)) { return _in_map[key]; } |
3165 | 3163 |
else { return _out_map[key]; } |
3166 | 3164 |
} |
3167 | 3165 |
|
3168 | 3166 |
ConstReturnValue operator[](const Node& key) const { |
3169 | 3167 |
if (Adaptor::inNode(key)) { return _in_map[key]; } |
3170 | 3168 |
else { return _out_map[key]; } |
3171 | 3169 |
} |
3172 | 3170 |
|
3173 | 3171 |
private: |
3174 | 3172 |
NodeImpl _in_map, _out_map; |
3175 | 3173 |
}; |
3176 | 3174 |
|
3177 | 3175 |
template <typename V> |
3178 | 3176 |
class ArcMapBase |
3179 | 3177 |
: public MapTraits<typename Parent::template ArcMap<V> > { |
3180 | 3178 |
typedef typename Parent::template ArcMap<V> ArcImpl; |
3181 | 3179 |
typedef typename Parent::template NodeMap<V> NodeImpl; |
3182 | 3180 |
public: |
3183 | 3181 |
typedef Arc Key; |
3184 | 3182 |
typedef V Value; |
3185 | 3183 |
typedef typename MapTraits<ArcImpl>::ReferenceMapTag ReferenceMapTag; |
3186 | 3184 |
typedef typename MapTraits<ArcImpl>::ReturnValue ReturnValue; |
3187 | 3185 |
typedef typename MapTraits<ArcImpl>::ConstReturnValue ConstReturnValue; |
3188 | 3186 |
typedef typename MapTraits<ArcImpl>::ReturnValue Reference; |
3189 | 3187 |
typedef typename MapTraits<ArcImpl>::ConstReturnValue ConstReference; |
3190 | 3188 |
|
3191 | 3189 |
ArcMapBase(const SplitNodesBase<DGR>& adaptor) |
3192 | 3190 |
: _arc_map(*adaptor._digraph), _node_map(*adaptor._digraph) {} |
3193 | 3191 |
ArcMapBase(const SplitNodesBase<DGR>& adaptor, const V& value) |
3194 | 3192 |
: _arc_map(*adaptor._digraph, value), |
3195 | 3193 |
_node_map(*adaptor._digraph, value) {} |
3196 | 3194 |
|
3197 | 3195 |
void set(const Arc& key, const V& val) { |
3198 | 3196 |
if (SplitNodesBase<DGR>::origArc(key)) { |
3199 | 3197 |
_arc_map.set(static_cast<const DigraphArc&>(key), val); |
3200 | 3198 |
} else { |
3201 | 3199 |
_node_map.set(static_cast<const DigraphNode&>(key), val); |
3202 | 3200 |
} |
3203 | 3201 |
} |
3204 | 3202 |
|
3205 | 3203 |
ReturnValue operator[](const Arc& key) { |
3206 | 3204 |
if (SplitNodesBase<DGR>::origArc(key)) { |
3207 | 3205 |
return _arc_map[static_cast<const DigraphArc&>(key)]; |
3208 | 3206 |
} else { |
3209 | 3207 |
return _node_map[static_cast<const DigraphNode&>(key)]; |
3210 | 3208 |
} |
3211 | 3209 |
} |
3212 | 3210 |
|
3213 | 3211 |
ConstReturnValue operator[](const Arc& key) const { |
3214 | 3212 |
if (SplitNodesBase<DGR>::origArc(key)) { |
3215 | 3213 |
return _arc_map[static_cast<const DigraphArc&>(key)]; |
3216 | 3214 |
} else { |
3217 | 3215 |
return _node_map[static_cast<const DigraphNode&>(key)]; |
3218 | 3216 |
} |
3219 | 3217 |
} |
3220 | 3218 |
|
3221 | 3219 |
private: |
3222 | 3220 |
ArcImpl _arc_map; |
3223 | 3221 |
NodeImpl _node_map; |
3224 | 3222 |
}; |
3225 | 3223 |
|
3226 | 3224 |
public: |
3227 | 3225 |
|
3228 | 3226 |
template <typename V> |
3229 | 3227 |
class NodeMap |
3230 | 3228 |
: public SubMapExtender<SplitNodesBase<DGR>, NodeMapBase<V> > |
3231 | 3229 |
{ |
3232 | 3230 |
public: |
3233 | 3231 |
typedef V Value; |
3234 | 3232 |
typedef SubMapExtender<SplitNodesBase<DGR>, NodeMapBase<Value> > Parent; |
3235 | 3233 |
|
3236 | 3234 |
NodeMap(const SplitNodesBase<DGR>& adaptor) |
3237 | 3235 |
: Parent(adaptor) {} |
3238 | 3236 |
|
3239 | 3237 |
NodeMap(const SplitNodesBase<DGR>& adaptor, const V& value) |
3240 | 3238 |
: Parent(adaptor, value) {} |
3241 | 3239 |
|
3242 | 3240 |
private: |
3243 | 3241 |
NodeMap& operator=(const NodeMap& cmap) { |
3244 | 3242 |
return operator=<NodeMap>(cmap); |
3245 | 3243 |
} |
3246 | 3244 |
|
3247 | 3245 |
template <typename CMap> |
3248 | 3246 |
NodeMap& operator=(const CMap& cmap) { |
3249 | 3247 |
Parent::operator=(cmap); |
3250 | 3248 |
return *this; |
3251 | 3249 |
} |
3252 | 3250 |
}; |
3253 | 3251 |
|
3254 | 3252 |
template <typename V> |
3255 | 3253 |
class ArcMap |
3256 | 3254 |
: public SubMapExtender<SplitNodesBase<DGR>, ArcMapBase<V> > |
3257 | 3255 |
{ |
3258 | 3256 |
public: |
3259 | 3257 |
typedef V Value; |
3260 | 3258 |
typedef SubMapExtender<SplitNodesBase<DGR>, ArcMapBase<Value> > Parent; |
3261 | 3259 |
|
3262 | 3260 |
ArcMap(const SplitNodesBase<DGR>& adaptor) |
3263 | 3261 |
: Parent(adaptor) {} |
3264 | 3262 |
|
3265 | 3263 |
ArcMap(const SplitNodesBase<DGR>& adaptor, const V& value) |
3266 | 3264 |
: Parent(adaptor, value) {} |
3267 | 3265 |
|
3268 | 3266 |
private: |
3269 | 3267 |
ArcMap& operator=(const ArcMap& cmap) { |
3270 | 3268 |
return operator=<ArcMap>(cmap); |
3271 | 3269 |
} |
3272 | 3270 |
|
3273 | 3271 |
template <typename CMap> |
3274 | 3272 |
ArcMap& operator=(const CMap& cmap) { |
3275 | 3273 |
Parent::operator=(cmap); |
3276 | 3274 |
return *this; |
3277 | 3275 |
} |
3278 | 3276 |
}; |
3279 | 3277 |
|
3280 | 3278 |
protected: |
3281 | 3279 |
|
3282 | 3280 |
SplitNodesBase() : _digraph(0) {} |
3283 | 3281 |
|
3284 | 3282 |
DGR* _digraph; |
3285 | 3283 |
|
3286 | 3284 |
void initialize(Digraph& digraph) { |
3287 | 3285 |
_digraph = &digraph; |
3288 | 3286 |
} |
3289 | 3287 |
|
3290 | 3288 |
}; |
3291 | 3289 |
|
3292 | 3290 |
/// \ingroup graph_adaptors |
3293 | 3291 |
/// |
3294 | 3292 |
/// \brief Adaptor class for splitting the nodes of a digraph. |
3295 | 3293 |
/// |
3296 | 3294 |
/// SplitNodes adaptor can be used for splitting each node into an |
3297 | 3295 |
/// \e in-node and an \e out-node in a digraph. Formaly, the adaptor |
3298 | 3296 |
/// replaces each node \f$ u \f$ in the digraph with two nodes, |
3299 | 3297 |
/// namely node \f$ u_{in} \f$ and node \f$ u_{out} \f$. |
3300 | 3298 |
/// If there is a \f$ (v, u) \f$ arc in the original digraph, then the |
3301 | 3299 |
/// new target of the arc will be \f$ u_{in} \f$ and similarly the |
3302 | 3300 |
/// source of each original \f$ (u, v) \f$ arc will be \f$ u_{out} \f$. |
3303 | 3301 |
/// The adaptor adds an additional \e bind \e arc from \f$ u_{in} \f$ |
3304 | 3302 |
/// to \f$ u_{out} \f$ for each node \f$ u \f$ of the original digraph. |
3305 | 3303 |
/// |
3306 | 3304 |
/// The aim of this class is running an algorithm with respect to node |
3307 | 3305 |
/// costs or capacities if the algorithm considers only arc costs or |
3308 | 3306 |
/// capacities directly. |
3309 | 3307 |
/// In this case you can use \c SplitNodes adaptor, and set the node |
3310 | 3308 |
/// costs/capacities of the original digraph to the \e bind \e arcs |
3311 | 3309 |
/// in the adaptor. |
3312 | 3310 |
/// |
3313 | 3311 |
/// \tparam DGR The type of the adapted digraph. |
3314 | 3312 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
3315 | 3313 |
/// It is implicitly \c const. |
3316 | 3314 |
/// |
3317 | 3315 |
/// \note The \c Node type of this adaptor is converible to the \c Node |
3318 | 3316 |
/// type of the adapted digraph. |
3319 | 3317 |
template <typename DGR> |
3320 | 3318 |
#ifdef DOXYGEN |
3321 | 3319 |
class SplitNodes { |
3322 | 3320 |
#else |
3323 | 3321 |
class SplitNodes |
3324 | 3322 |
: public DigraphAdaptorExtender<SplitNodesBase<const DGR> > { |
3325 | 3323 |
#endif |
3326 | 3324 |
public: |
3327 | 3325 |
typedef DGR Digraph; |
3328 | 3326 |
typedef DigraphAdaptorExtender<SplitNodesBase<const DGR> > Parent; |
3329 | 3327 |
|
3330 | 3328 |
typedef typename DGR::Node DigraphNode; |
3331 | 3329 |
typedef typename DGR::Arc DigraphArc; |
3332 | 3330 |
|
3333 | 3331 |
typedef typename Parent::Node Node; |
3334 | 3332 |
typedef typename Parent::Arc Arc; |
3335 | 3333 |
|
3336 | 3334 |
/// \brief Constructor |
3337 | 3335 |
/// |
3338 | 3336 |
/// Constructor of the adaptor. |
3339 | 3337 |
SplitNodes(const DGR& g) { |
3340 | 3338 |
Parent::initialize(g); |
3341 | 3339 |
} |
3342 | 3340 |
|
3343 | 3341 |
/// \brief Returns \c true if the given node is an in-node. |
3344 | 3342 |
/// |
3345 | 3343 |
/// Returns \c true if the given node is an in-node. |
3346 | 3344 |
static bool inNode(const Node& n) { |
3347 | 3345 |
return Parent::inNode(n); |
3348 | 3346 |
} |
3349 | 3347 |
|
3350 | 3348 |
/// \brief Returns \c true if the given node is an out-node. |
3351 | 3349 |
/// |
3352 | 3350 |
/// Returns \c true if the given node is an out-node. |
3353 | 3351 |
static bool outNode(const Node& n) { |
3354 | 3352 |
return Parent::outNode(n); |
3355 | 3353 |
} |
3356 | 3354 |
|
3357 | 3355 |
/// \brief Returns \c true if the given arc is an original arc. |
3358 | 3356 |
/// |
3359 | 3357 |
/// Returns \c true if the given arc is one of the arcs in the |
3360 | 3358 |
/// original digraph. |
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 |
} |
137 | 135 |
|
138 | 136 |
}; |
139 | 137 |
|
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)); |
325 | 323 |
} |
326 | 324 |
|
327 | 325 |
using Parent::direct; |
328 | 326 |
Arc direct(const Edge &e, const Node &s) const { |
329 | 327 |
return Parent::direct(e, Parent::u(e) == s); |
330 | 328 |
} |
331 | 329 |
|
332 | 330 |
typedef AlterationNotifier<EdgeSetExtender, Arc> ArcNotifier; |
333 | 331 |
typedef AlterationNotifier<EdgeSetExtender, Edge> EdgeNotifier; |
334 | 332 |
|
335 | 333 |
|
336 | 334 |
protected: |
337 | 335 |
|
338 | 336 |
mutable ArcNotifier arc_notifier; |
339 | 337 |
mutable EdgeNotifier edge_notifier; |
340 | 338 |
|
341 | 339 |
public: |
342 | 340 |
|
343 | 341 |
using Parent::notifier; |
344 | 342 |
|
345 | 343 |
ArcNotifier& notifier(Arc) const { |
346 | 344 |
return arc_notifier; |
347 | 345 |
} |
348 | 346 |
|
349 | 347 |
EdgeNotifier& notifier(Edge) const { |
350 | 348 |
return edge_notifier; |
351 | 349 |
} |
352 | 350 |
|
353 | 351 |
|
354 | 352 |
class NodeIt : public Node { |
355 | 353 |
const Digraph* digraph; |
356 | 354 |
public: |
357 | 355 |
|
358 | 356 |
NodeIt() {} |
359 | 357 |
|
360 | 358 |
NodeIt(Invalid i) : Node(i) { } |
361 | 359 |
|
362 | 360 |
explicit NodeIt(const Digraph& _graph) : digraph(&_graph) { |
363 | 361 |
_graph.first(static_cast<Node&>(*this)); |
364 | 362 |
} |
365 | 363 |
|
366 | 364 |
NodeIt(const Digraph& _graph, const Node& node) |
367 | 365 |
: Node(node), digraph(&_graph) {} |
368 | 366 |
|
369 | 367 |
NodeIt& operator++() { |
370 | 368 |
digraph->next(*this); |
371 | 369 |
return *this; |
372 | 370 |
} |
373 | 371 |
|
374 | 372 |
}; |
375 | 373 |
|
376 | 374 |
|
377 | 375 |
class ArcIt : public Arc { |
378 | 376 |
const Digraph* digraph; |
379 | 377 |
public: |
380 | 378 |
|
381 | 379 |
ArcIt() { } |
382 | 380 |
|
383 | 381 |
ArcIt(Invalid i) : Arc(i) { } |
384 | 382 |
|
385 | 383 |
explicit ArcIt(const Digraph& _graph) : digraph(&_graph) { |
386 | 384 |
_graph.first(static_cast<Arc&>(*this)); |
387 | 385 |
} |
388 | 386 |
|
389 | 387 |
ArcIt(const Digraph& _graph, const Arc& e) : |
390 | 388 |
Arc(e), digraph(&_graph) { } |
391 | 389 |
|
392 | 390 |
ArcIt& operator++() { |
393 | 391 |
digraph->next(*this); |
394 | 392 |
return *this; |
395 | 393 |
} |
396 | 394 |
|
397 | 395 |
}; |
398 | 396 |
|
399 | 397 |
|
400 | 398 |
class OutArcIt : public Arc { |
401 | 399 |
const Digraph* digraph; |
402 | 400 |
public: |
403 | 401 |
|
404 | 402 |
OutArcIt() { } |
405 | 403 |
|
406 | 404 |
OutArcIt(Invalid i) : Arc(i) { } |
407 | 405 |
|
408 | 406 |
OutArcIt(const Digraph& _graph, const Node& node) |
409 | 407 |
: digraph(&_graph) { |
410 | 408 |
_graph.firstOut(*this, node); |
411 | 409 |
} |
412 | 410 |
|
413 | 411 |
OutArcIt(const Digraph& _graph, const Arc& arc) |
414 | 412 |
: Arc(arc), digraph(&_graph) {} |
415 | 413 |
|
416 | 414 |
OutArcIt& operator++() { |
417 | 415 |
digraph->nextOut(*this); |
418 | 416 |
return *this; |
419 | 417 |
} |
420 | 418 |
|
421 | 419 |
}; |
422 | 420 |
|
423 | 421 |
|
424 | 422 |
class InArcIt : public Arc { |
425 | 423 |
const Digraph* digraph; |
426 | 424 |
public: |
427 | 425 |
|
428 | 426 |
InArcIt() { } |
429 | 427 |
|
430 | 428 |
InArcIt(Invalid i) : Arc(i) { } |
431 | 429 |
|
432 | 430 |
InArcIt(const Digraph& _graph, const Node& node) |
433 | 431 |
: digraph(&_graph) { |
434 | 432 |
_graph.firstIn(*this, node); |
435 | 433 |
} |
436 | 434 |
|
437 | 435 |
InArcIt(const Digraph& _graph, const Arc& arc) : |
438 | 436 |
Arc(arc), digraph(&_graph) {} |
439 | 437 |
|
440 | 438 |
InArcIt& operator++() { |
441 | 439 |
digraph->nextIn(*this); |
442 | 440 |
return *this; |
443 | 441 |
} |
444 | 442 |
|
445 | 443 |
}; |
446 | 444 |
|
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> |
580 | 578 |
EdgeMap& operator=(const CMap& cmap) { |
581 | 579 |
Parent::operator=(cmap); |
582 | 580 |
return *this; |
583 | 581 |
} |
584 | 582 |
|
585 | 583 |
}; |
586 | 584 |
|
587 | 585 |
|
588 | 586 |
// Alteration extension |
589 | 587 |
|
590 | 588 |
Edge addEdge(const Node& from, const Node& to) { |
591 | 589 |
Edge edge = Parent::addEdge(from, to); |
592 | 590 |
notifier(Edge()).add(edge); |
593 | 591 |
std::vector<Arc> arcs; |
594 | 592 |
arcs.push_back(Parent::direct(edge, true)); |
595 | 593 |
arcs.push_back(Parent::direct(edge, false)); |
596 | 594 |
notifier(Arc()).add(arcs); |
597 | 595 |
return edge; |
598 | 596 |
} |
599 | 597 |
|
600 | 598 |
void clear() { |
601 | 599 |
notifier(Arc()).clear(); |
602 | 600 |
notifier(Edge()).clear(); |
603 | 601 |
Parent::clear(); |
604 | 602 |
} |
605 | 603 |
|
606 | 604 |
void erase(const Edge& edge) { |
607 | 605 |
std::vector<Arc> arcs; |
608 | 606 |
arcs.push_back(Parent::direct(edge, true)); |
609 | 607 |
arcs.push_back(Parent::direct(edge, false)); |
610 | 608 |
notifier(Arc()).erase(arcs); |
611 | 609 |
notifier(Edge()).erase(edge); |
612 | 610 |
Parent::erase(edge); |
613 | 611 |
} |
614 | 612 |
|
615 | 613 |
|
616 | 614 |
EdgeSetExtender() { |
617 | 615 |
arc_notifier.setContainer(*this); |
618 | 616 |
edge_notifier.setContainer(*this); |
619 | 617 |
} |
620 | 618 |
|
621 | 619 |
~EdgeSetExtender() { |
622 | 620 |
edge_notifier.clear(); |
623 | 621 |
arc_notifier.clear(); |
624 | 622 |
} |
625 | 623 |
|
626 | 624 |
}; |
627 | 625 |
|
628 | 626 |
} |
629 | 627 |
|
630 | 628 |
#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_CIRCULATION_H |
20 | 20 |
#define LEMON_CIRCULATION_H |
21 | 21 |
|
22 | 22 |
#include <lemon/tolerance.h> |
23 | 23 |
#include <lemon/elevator.h> |
24 | 24 |
|
25 | 25 |
///\ingroup max_flow |
26 | 26 |
///\file |
27 | 27 |
///\brief Push-relabel algorithm for finding a feasible circulation. |
28 | 28 |
/// |
29 | 29 |
namespace lemon { |
30 | 30 |
|
31 | 31 |
/// \brief Default traits class of Circulation class. |
32 | 32 |
/// |
33 | 33 |
/// Default traits class of Circulation class. |
34 | 34 |
/// \tparam GR Digraph type. |
35 | 35 |
/// \tparam LM Lower bound capacity map type. |
36 | 36 |
/// \tparam UM Upper bound capacity map type. |
37 | 37 |
/// \tparam DM Delta map type. |
38 | 38 |
template <typename GR, typename LM, |
39 | 39 |
typename UM, typename DM> |
40 | 40 |
struct CirculationDefaultTraits { |
41 | 41 |
|
42 | 42 |
/// \brief The type of the digraph the algorithm runs on. |
43 | 43 |
typedef GR Digraph; |
44 | 44 |
|
45 | 45 |
/// \brief The type of the map that stores the circulation lower |
46 | 46 |
/// bound. |
47 | 47 |
/// |
48 | 48 |
/// The type of the map that stores the circulation lower bound. |
49 | 49 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
50 | 50 |
typedef LM LCapMap; |
51 | 51 |
|
52 | 52 |
/// \brief The type of the map that stores the circulation upper |
53 | 53 |
/// bound. |
54 | 54 |
/// |
55 | 55 |
/// The type of the map that stores the circulation upper bound. |
56 | 56 |
/// It must meet the \ref concepts::ReadMap "ReadMap" concept. |
57 | 57 |
typedef UM UCapMap; |
58 | 58 |
|
59 | 59 |
/// \brief The type of the map that stores the lower bound for |
60 | 60 |
/// the supply of the nodes. |
61 | 61 |
/// |
62 | 62 |
/// The type of the map that stores the lower bound for the supply |
63 | 63 |
/// of the nodes. It must meet the \ref concepts::ReadMap "ReadMap" |
64 | 64 |
/// concept. |
65 | 65 |
typedef DM DeltaMap; |
66 | 66 |
|
67 | 67 |
/// \brief The type of the flow values. |
68 | 68 |
typedef typename DeltaMap::Value Value; |
69 | 69 |
|
70 | 70 |
/// \brief The type of the map that stores the flow values. |
71 | 71 |
/// |
72 | 72 |
/// The type of the map that stores the flow values. |
73 | 73 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
74 | 74 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
75 | 75 |
|
76 | 76 |
/// \brief Instantiates a FlowMap. |
77 | 77 |
/// |
78 | 78 |
/// This function instantiates a \ref FlowMap. |
79 | 79 |
/// \param digraph The digraph, to which we would like to define |
80 | 80 |
/// the flow map. |
81 | 81 |
static FlowMap* createFlowMap(const Digraph& digraph) { |
82 | 82 |
return new FlowMap(digraph); |
83 | 83 |
} |
84 | 84 |
|
85 | 85 |
/// \brief The elevator type used by the algorithm. |
86 | 86 |
/// |
87 | 87 |
/// The elevator type used by the algorithm. |
88 | 88 |
/// |
89 | 89 |
/// \sa Elevator |
90 | 90 |
/// \sa LinkedElevator |
91 | 91 |
typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator; |
92 | 92 |
|
93 | 93 |
/// \brief Instantiates an Elevator. |
94 | 94 |
/// |
95 | 95 |
/// This function instantiates an \ref Elevator. |
96 | 96 |
/// \param digraph The digraph, to which we would like to define |
97 | 97 |
/// the elevator. |
98 | 98 |
/// \param max_level The maximum level of the elevator. |
99 | 99 |
static Elevator* createElevator(const Digraph& digraph, int max_level) { |
100 | 100 |
return new Elevator(digraph, max_level); |
101 | 101 |
} |
102 | 102 |
|
103 | 103 |
/// \brief The tolerance used by the algorithm |
104 | 104 |
/// |
105 | 105 |
/// The tolerance used by the algorithm to handle inexact computation. |
106 | 106 |
typedef lemon::Tolerance<Value> Tolerance; |
107 | 107 |
|
108 | 108 |
}; |
109 | 109 |
|
110 | 110 |
/** |
111 | 111 |
\brief Push-relabel algorithm for the network circulation problem. |
112 | 112 |
|
113 | 113 |
\ingroup max_flow |
114 | 114 |
This class implements a push-relabel algorithm for the network |
115 | 115 |
circulation problem. |
116 | 116 |
It is to find a feasible circulation when lower and upper bounds |
117 | 117 |
are given for the flow values on the arcs and lower bounds |
118 | 118 |
are given for the supply values of the nodes. |
119 | 119 |
|
120 | 120 |
The exact formulation of this problem is the following. |
121 | 121 |
Let \f$G=(V,A)\f$ be a digraph, |
122 | 122 |
\f$lower, upper: A\rightarrow\mathbf{R}^+_0\f$, |
123 | 123 |
\f$delta: V\rightarrow\mathbf{R}\f$. Find a feasible circulation |
124 | 124 |
\f$f: A\rightarrow\mathbf{R}^+_0\f$ so that |
125 | 125 |
\f[ \sum_{a\in\delta_{out}(v)} f(a) - \sum_{a\in\delta_{in}(v)} f(a) |
126 | 126 |
\geq delta(v) \quad \forall v\in V, \f] |
127 | 127 |
\f[ lower(a)\leq f(a) \leq upper(a) \quad \forall a\in A. \f] |
128 | 128 |
\note \f$delta(v)\f$ specifies a lower bound for the supply of node |
129 | 129 |
\f$v\f$. It can be either positive or negative, however note that |
130 | 130 |
\f$\sum_{v\in V}delta(v)\f$ should be zero or negative in order to |
131 | 131 |
have a feasible solution. |
132 | 132 |
|
133 | 133 |
\note A special case of this problem is when |
134 | 134 |
\f$\sum_{v\in V}delta(v) = 0\f$. Then the supply of each node \f$v\f$ |
135 | 135 |
will be \e equal \e to \f$delta(v)\f$, if a circulation can be found. |
136 | 136 |
Thus a feasible solution for the |
137 | 137 |
\ref min_cost_flow "minimum cost flow" problem can be calculated |
138 | 138 |
in this way. |
139 | 139 |
|
140 | 140 |
\tparam GR The type of the digraph the algorithm runs on. |
141 | 141 |
\tparam LM The type of the lower bound capacity map. The default |
142 | 142 |
map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
143 | 143 |
\tparam UM The type of the upper bound capacity map. The default |
144 | 144 |
map type is \c LM. |
145 | 145 |
\tparam DM The type of the map that stores the lower bound |
146 | 146 |
for the supply of the nodes. The default map type is |
147 | 147 |
\ref concepts::Digraph::NodeMap "GR::NodeMap<UM::Value>". |
148 | 148 |
*/ |
149 | 149 |
#ifdef DOXYGEN |
150 | 150 |
template< typename GR, |
151 | 151 |
typename LM, |
152 | 152 |
typename UM, |
153 | 153 |
typename DM, |
154 | 154 |
typename TR > |
155 | 155 |
#else |
156 | 156 |
template< typename GR, |
157 | 157 |
typename LM = typename GR::template ArcMap<int>, |
158 | 158 |
typename UM = LM, |
159 | 159 |
typename DM = typename GR::template NodeMap<typename UM::Value>, |
160 | 160 |
typename TR = CirculationDefaultTraits<GR, LM, UM, DM> > |
161 | 161 |
#endif |
162 | 162 |
class Circulation { |
163 | 163 |
public: |
164 | 164 |
|
165 | 165 |
///The \ref CirculationDefaultTraits "traits class" of the algorithm. |
166 | 166 |
typedef TR Traits; |
167 | 167 |
///The type of the digraph the algorithm runs on. |
168 | 168 |
typedef typename Traits::Digraph Digraph; |
169 | 169 |
///The type of the flow values. |
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() { |
340 | 340 |
if (_local_flow) { |
341 | 341 |
delete _flow; |
342 | 342 |
} |
343 | 343 |
if (_local_level) { |
344 | 344 |
delete _level; |
345 | 345 |
} |
346 | 346 |
if (_excess) { |
347 | 347 |
delete _excess; |
348 | 348 |
} |
349 | 349 |
} |
350 | 350 |
|
351 | 351 |
public: |
352 | 352 |
|
353 | 353 |
/// Sets the lower bound capacity map. |
354 | 354 |
|
355 | 355 |
/// Sets the lower bound capacity map. |
356 | 356 |
/// \return <tt>(*this)</tt> |
357 | 357 |
Circulation& lowerCapMap(const LCapMap& map) { |
358 | 358 |
_lo = ↦ |
359 | 359 |
return *this; |
360 | 360 |
} |
361 | 361 |
|
362 | 362 |
/// Sets the upper bound capacity map. |
363 | 363 |
|
364 | 364 |
/// Sets the upper bound capacity map. |
365 | 365 |
/// \return <tt>(*this)</tt> |
366 | 366 |
Circulation& upperCapMap(const LCapMap& map) { |
367 | 367 |
_up = ↦ |
368 | 368 |
return *this; |
369 | 369 |
} |
370 | 370 |
|
371 | 371 |
/// Sets the lower bound map for the supply of the nodes. |
372 | 372 |
|
373 | 373 |
/// Sets the lower bound map for the supply of the nodes. |
374 | 374 |
/// \return <tt>(*this)</tt> |
375 | 375 |
Circulation& deltaMap(const DeltaMap& map) { |
376 | 376 |
_delta = ↦ |
377 | 377 |
return *this; |
378 | 378 |
} |
379 | 379 |
|
380 | 380 |
/// \brief Sets the flow map. |
381 | 381 |
/// |
382 | 382 |
/// Sets the flow map. |
383 | 383 |
/// If you don't use this function before calling \ref run() or |
384 | 384 |
/// \ref init(), an instance will be allocated automatically. |
385 | 385 |
/// The destructor deallocates this automatically allocated map, |
386 | 386 |
/// of course. |
387 | 387 |
/// \return <tt>(*this)</tt> |
388 | 388 |
Circulation& flowMap(FlowMap& map) { |
389 | 389 |
if (_local_flow) { |
390 | 390 |
delete _flow; |
391 | 391 |
_local_flow = false; |
392 | 392 |
} |
393 | 393 |
_flow = ↦ |
394 | 394 |
return *this; |
395 | 395 |
} |
396 | 396 |
|
397 | 397 |
/// \brief Sets the elevator used by algorithm. |
398 | 398 |
/// |
399 | 399 |
/// Sets the elevator used by algorithm. |
400 | 400 |
/// If you don't use this function before calling \ref run() or |
401 | 401 |
/// \ref init(), an instance will be allocated automatically. |
402 | 402 |
/// The destructor deallocates this automatically allocated elevator, |
403 | 403 |
/// of course. |
404 | 404 |
/// \return <tt>(*this)</tt> |
405 | 405 |
Circulation& elevator(Elevator& elevator) { |
406 | 406 |
if (_local_level) { |
407 | 407 |
delete _level; |
408 | 408 |
_local_level = false; |
409 | 409 |
} |
410 | 410 |
_level = &elevator; |
411 | 411 |
return *this; |
412 | 412 |
} |
413 | 413 |
|
414 | 414 |
/// \brief Returns a const reference to the elevator. |
415 | 415 |
/// |
416 | 416 |
/// Returns a const reference to the elevator. |
417 | 417 |
/// |
418 | 418 |
/// \pre Either \ref run() or \ref init() must be called before |
419 | 419 |
/// using this function. |
420 | 420 |
const Elevator& elevator() const { |
421 | 421 |
return *_level; |
422 | 422 |
} |
423 | 423 |
|
424 | 424 |
/// \brief Sets the tolerance used by algorithm. |
425 | 425 |
/// |
426 | 426 |
/// Sets the tolerance used by algorithm. |
427 | 427 |
Circulation& tolerance(const Tolerance& tolerance) const { |
428 | 428 |
_tol = tolerance; |
429 | 429 |
return *this; |
430 | 430 |
} |
431 | 431 |
|
432 | 432 |
/// \brief Returns a const reference to the tolerance. |
433 | 433 |
/// |
434 | 434 |
/// Returns a const reference to the tolerance. |
435 | 435 |
const Tolerance& tolerance() const { |
436 | 436 |
return tolerance; |
437 | 437 |
} |
438 | 438 |
|
439 | 439 |
/// \name Execution Control |
440 | 440 |
/// The simplest way to execute the algorithm is to call \ref run().\n |
441 | 441 |
/// If you need more control on the initial solution or the execution, |
442 | 442 |
/// first you have to call one of the \ref init() functions, then |
443 | 443 |
/// the \ref start() function. |
444 | 444 |
|
445 | 445 |
///@{ |
446 | 446 |
|
447 | 447 |
/// Initializes the internal data structures. |
448 | 448 |
|
449 | 449 |
/// Initializes the internal data structures and sets all flow values |
450 | 450 |
/// to the lower bound. |
451 | 451 |
void init() |
452 | 452 |
{ |
453 | 453 |
createStructures(); |
454 | 454 |
|
455 | 455 |
for(NodeIt n(_g);n!=INVALID;++n) { |
456 | 456 |
_excess->set(n, (*_delta)[n]); |
457 | 457 |
} |
458 | 458 |
|
459 | 459 |
for (ArcIt e(_g);e!=INVALID;++e) { |
460 | 460 |
_flow->set(e, (*_lo)[e]); |
461 | 461 |
_excess->set(_g.target(e), (*_excess)[_g.target(e)] + (*_flow)[e]); |
462 | 462 |
_excess->set(_g.source(e), (*_excess)[_g.source(e)] - (*_flow)[e]); |
463 | 463 |
} |
464 | 464 |
|
465 | 465 |
// global relabeling tested, but in general case it provides |
466 | 466 |
// worse performance for random digraphs |
467 | 467 |
_level->initStart(); |
468 | 468 |
for(NodeIt n(_g);n!=INVALID;++n) |
469 | 469 |
_level->initAddItem(n); |
470 | 470 |
_level->initFinish(); |
471 | 471 |
for(NodeIt n(_g);n!=INVALID;++n) |
472 | 472 |
if(_tol.positive((*_excess)[n])) |
473 | 473 |
_level->activate(n); |
474 | 474 |
} |
475 | 475 |
|
476 | 476 |
/// Initializes the internal data structures using a greedy approach. |
477 | 477 |
|
478 | 478 |
/// Initializes the internal data structures using a greedy approach |
479 | 479 |
/// to construct the initial solution. |
480 | 480 |
void greedyInit() |
481 | 481 |
{ |
482 | 482 |
createStructures(); |
483 | 483 |
|
484 | 484 |
for(NodeIt n(_g);n!=INVALID;++n) { |
485 | 485 |
_excess->set(n, (*_delta)[n]); |
486 | 486 |
} |
487 | 487 |
|
488 | 488 |
for (ArcIt e(_g);e!=INVALID;++e) { |
489 | 489 |
if (!_tol.positive((*_excess)[_g.target(e)] + (*_up)[e])) { |
490 | 490 |
_flow->set(e, (*_up)[e]); |
491 | 491 |
_excess->set(_g.target(e), (*_excess)[_g.target(e)] + (*_up)[e]); |
492 | 492 |
_excess->set(_g.source(e), (*_excess)[_g.source(e)] - (*_up)[e]); |
493 | 493 |
} else if (_tol.positive((*_excess)[_g.target(e)] + (*_lo)[e])) { |
494 | 494 |
_flow->set(e, (*_lo)[e]); |
495 | 495 |
_excess->set(_g.target(e), (*_excess)[_g.target(e)] + (*_lo)[e]); |
496 | 496 |
_excess->set(_g.source(e), (*_excess)[_g.source(e)] - (*_lo)[e]); |
497 | 497 |
} else { |
498 | 498 |
Value fc = -(*_excess)[_g.target(e)]; |
499 | 499 |
_flow->set(e, fc); |
500 | 500 |
_excess->set(_g.target(e), 0); |
501 | 501 |
_excess->set(_g.source(e), (*_excess)[_g.source(e)] - fc); |
502 | 502 |
} |
503 | 503 |
} |
504 | 504 |
|
505 | 505 |
_level->initStart(); |
506 | 506 |
for(NodeIt n(_g);n!=INVALID;++n) |
507 | 507 |
_level->initAddItem(n); |
508 | 508 |
_level->initFinish(); |
509 | 509 |
for(NodeIt n(_g);n!=INVALID;++n) |
510 | 510 |
if(_tol.positive((*_excess)[n])) |
511 | 511 |
_level->activate(n); |
512 | 512 |
} |
513 | 513 |
|
514 | 514 |
///Executes the algorithm |
515 | 515 |
|
516 | 516 |
///This function executes the algorithm. |
517 | 517 |
/// |
518 | 518 |
///\return \c true if a feasible circulation is found. |
519 | 519 |
/// |
520 | 520 |
///\sa barrier() |
521 | 521 |
///\sa barrierMap() |
522 | 522 |
bool start() |
523 | 523 |
{ |
524 | 524 |
|
525 | 525 |
Node act; |
526 | 526 |
Node bact=INVALID; |
527 | 527 |
Node last_activated=INVALID; |
528 | 528 |
while((act=_level->highestActive())!=INVALID) { |
529 | 529 |
int actlevel=(*_level)[act]; |
530 | 530 |
int mlevel=_node_num; |
531 | 531 |
Value exc=(*_excess)[act]; |
532 | 532 |
|
533 | 533 |
for(OutArcIt e(_g,act);e!=INVALID; ++e) { |
534 | 534 |
Node v = _g.target(e); |
535 | 535 |
Value fc=(*_up)[e]-(*_flow)[e]; |
536 | 536 |
if(!_tol.positive(fc)) continue; |
537 | 537 |
if((*_level)[v]<actlevel) { |
538 | 538 |
if(!_tol.less(fc, exc)) { |
539 | 539 |
_flow->set(e, (*_flow)[e] + exc); |
540 | 540 |
_excess->set(v, (*_excess)[v] + exc); |
541 | 541 |
if(!_level->active(v) && _tol.positive((*_excess)[v])) |
542 | 542 |
_level->activate(v); |
543 | 543 |
_excess->set(act,0); |
544 | 544 |
_level->deactivate(act); |
545 | 545 |
goto next_l; |
546 | 546 |
} |
547 | 547 |
else { |
548 | 548 |
_flow->set(e, (*_up)[e]); |
549 | 549 |
_excess->set(v, (*_excess)[v] + fc); |
550 | 550 |
if(!_level->active(v) && _tol.positive((*_excess)[v])) |
551 | 551 |
_level->activate(v); |
552 | 552 |
exc-=fc; |
553 | 553 |
} |
554 | 554 |
} |
555 | 555 |
else if((*_level)[v]<mlevel) mlevel=(*_level)[v]; |
556 | 556 |
} |
557 | 557 |
for(InArcIt e(_g,act);e!=INVALID; ++e) { |
558 | 558 |
Node v = _g.source(e); |
559 | 559 |
Value fc=(*_flow)[e]-(*_lo)[e]; |
560 | 560 |
if(!_tol.positive(fc)) continue; |
561 | 561 |
if((*_level)[v]<actlevel) { |
562 | 562 |
if(!_tol.less(fc, exc)) { |
563 | 563 |
_flow->set(e, (*_flow)[e] - exc); |
564 | 564 |
_excess->set(v, (*_excess)[v] + exc); |
565 | 565 |
if(!_level->active(v) && _tol.positive((*_excess)[v])) |
566 | 566 |
_level->activate(v); |
567 | 567 |
_excess->set(act,0); |
568 | 568 |
_level->deactivate(act); |
569 | 569 |
goto next_l; |
570 | 570 |
} |
571 | 571 |
else { |
572 | 572 |
_flow->set(e, (*_lo)[e]); |
573 | 573 |
_excess->set(v, (*_excess)[v] + fc); |
574 | 574 |
if(!_level->active(v) && _tol.positive((*_excess)[v])) |
575 | 575 |
_level->activate(v); |
576 | 576 |
exc-=fc; |
577 | 577 |
} |
578 | 578 |
} |
579 | 579 |
else if((*_level)[v]<mlevel) mlevel=(*_level)[v]; |
580 | 580 |
} |
581 | 581 |
|
582 | 582 |
_excess->set(act, exc); |
583 | 583 |
if(!_tol.positive(exc)) _level->deactivate(act); |
584 | 584 |
else if(mlevel==_node_num) { |
585 | 585 |
_level->liftHighestActiveToTop(); |
586 | 586 |
_el = _node_num; |
587 | 587 |
return false; |
588 | 588 |
} |
589 | 589 |
else { |
590 | 590 |
_level->liftHighestActive(mlevel+1); |
591 | 591 |
if(_level->onLevel(actlevel)==0) { |
592 | 592 |
_el = actlevel; |
593 | 593 |
return false; |
594 | 594 |
} |
595 | 595 |
} |
596 | 596 |
next_l: |
597 | 597 |
; |
598 | 598 |
} |
599 | 599 |
return true; |
600 | 600 |
} |
601 | 601 |
|
602 | 602 |
/// Runs the algorithm. |
603 | 603 |
|
604 | 604 |
/// This function runs the algorithm. |
605 | 605 |
/// |
606 | 606 |
/// \return \c true if a feasible circulation is found. |
607 | 607 |
/// |
608 | 608 |
/// \note Apart from the return value, c.run() is just a shortcut of |
609 | 609 |
/// the following code. |
610 | 610 |
/// \code |
611 | 611 |
/// c.greedyInit(); |
612 | 612 |
/// c.start(); |
613 | 613 |
/// \endcode |
614 | 614 |
bool run() { |
615 | 615 |
greedyInit(); |
616 | 616 |
return start(); |
617 | 617 |
} |
618 | 618 |
|
619 | 619 |
/// @} |
620 | 620 |
|
621 | 621 |
/// \name Query Functions |
622 | 622 |
/// The results of the circulation algorithm can be obtained using |
623 | 623 |
/// these functions.\n |
624 | 624 |
/// Either \ref run() or \ref start() should be called before |
625 | 625 |
/// using them. |
626 | 626 |
|
627 | 627 |
///@{ |
628 | 628 |
|
629 | 629 |
/// \brief Returns the flow on the given arc. |
630 | 630 |
/// |
631 | 631 |
/// Returns the flow on the given arc. |
632 | 632 |
/// |
633 | 633 |
/// \pre Either \ref run() or \ref init() must be called before |
634 | 634 |
/// using this function. |
635 | 635 |
Value flow(const Arc& arc) const { |
636 | 636 |
return (*_flow)[arc]; |
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; |
734 | 734 |
for(NodeIt n(_g);n!=INVALID;++n) |
735 | 735 |
if(barrier(n)) |
736 | 736 |
delta-=(*_delta)[n]; |
737 | 737 |
for(ArcIt e(_g);e!=INVALID;++e) |
738 | 738 |
{ |
739 | 739 |
Node s=_g.source(e); |
740 | 740 |
Node t=_g.target(e); |
741 | 741 |
if(barrier(s)&&!barrier(t)) delta+=(*_up)[e]; |
742 | 742 |
else if(barrier(t)&&!barrier(s)) delta-=(*_lo)[e]; |
743 | 743 |
} |
744 | 744 |
return _tol.negative(delta); |
745 | 745 |
} |
746 | 746 |
|
747 | 747 |
/// @} |
748 | 748 |
|
749 | 749 |
}; |
750 | 750 |
|
751 | 751 |
} |
752 | 752 |
|
753 | 753 |
#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 graph_concepts |
20 | 20 |
///\file |
21 | 21 |
///\brief The concept of Undirected Graphs. |
22 | 22 |
|
23 | 23 |
#ifndef LEMON_CONCEPTS_GRAPH_H |
24 | 24 |
#define LEMON_CONCEPTS_GRAPH_H |
25 | 25 |
|
26 | 26 |
#include <lemon/concepts/graph_components.h> |
27 | 27 |
#include <lemon/core.h> |
28 | 28 |
|
29 | 29 |
namespace lemon { |
30 | 30 |
namespace concepts { |
31 | 31 |
|
32 | 32 |
/// \ingroup graph_concepts |
33 | 33 |
/// |
34 | 34 |
/// \brief Class describing the concept of Undirected Graphs. |
35 | 35 |
/// |
36 | 36 |
/// This class describes the common interface of all Undirected |
37 | 37 |
/// Graphs. |
38 | 38 |
/// |
39 | 39 |
/// As all concept describing classes it provides only interface |
40 | 40 |
/// without any sensible implementation. So any algorithm for |
41 | 41 |
/// undirected graph should compile with this class, but it will not |
42 | 42 |
/// run properly, of course. |
43 | 43 |
/// |
44 | 44 |
/// The LEMON undirected graphs also fulfill the concept of |
45 | 45 |
/// directed graphs (\ref lemon::concepts::Digraph "Digraph |
46 | 46 |
/// Concept"). Each edges can be seen as two opposite |
47 | 47 |
/// directed arc and consequently the undirected graph can be |
48 | 48 |
/// seen as the direceted graph of these directed arcs. The |
49 | 49 |
/// Graph has the Edge inner class for the edges and |
50 | 50 |
/// the Arc type for the directed arcs. The Arc type is |
51 | 51 |
/// convertible to Edge or inherited from it so from a directed |
52 | 52 |
/// arc we can get the represented edge. |
53 | 53 |
/// |
54 | 54 |
/// In the sense of the LEMON each edge has a default |
55 | 55 |
/// direction (it should be in every computer implementation, |
56 | 56 |
/// because the order of edge's nodes defines an |
57 | 57 |
/// orientation). With the default orientation we can define that |
58 | 58 |
/// the directed arc is forward or backward directed. With the \c |
59 | 59 |
/// direction() and \c direct() function we can get the direction |
60 | 60 |
/// of the directed arc and we can direct an edge. |
61 | 61 |
/// |
62 | 62 |
/// The EdgeIt is an iterator for the edges. We can use |
63 | 63 |
/// the EdgeMap to map values for the edges. The InArcIt and |
64 | 64 |
/// OutArcIt iterates on the same edges but with opposite |
65 | 65 |
/// direction. The IncEdgeIt iterates also on the same edges |
66 | 66 |
/// as the OutArcIt and InArcIt but it is not convertible to Arc just |
67 | 67 |
/// to Edge. |
68 | 68 |
class Graph { |
69 | 69 |
public: |
70 | 70 |
/// \brief The undirected graph should be tagged by the |
71 | 71 |
/// UndirectedTag. |
72 | 72 |
/// |
73 | 73 |
/// The undirected graph should be tagged by the UndirectedTag. This |
74 | 74 |
/// tag helps the enable_if technics to make compile time |
75 | 75 |
/// specializations for undirected graphs. |
76 | 76 |
typedef True UndirectedTag; |
77 | 77 |
|
78 | 78 |
/// \brief The base type of node iterators, |
79 | 79 |
/// or in other words, the trivial node iterator. |
80 | 80 |
/// |
81 | 81 |
/// This is the base type of each node iterator, |
82 | 82 |
/// thus each kind of node iterator converts to this. |
83 | 83 |
/// More precisely each kind of node iterator should be inherited |
84 | 84 |
/// from the trivial node iterator. |
85 | 85 |
class Node { |
86 | 86 |
public: |
87 | 87 |
/// Default constructor |
88 | 88 |
|
89 | 89 |
/// @warning The default constructor sets the iterator |
90 | 90 |
/// to an undefined value. |
91 | 91 |
Node() { } |
92 | 92 |
/// Copy constructor. |
93 | 93 |
|
94 | 94 |
/// Copy constructor. |
95 | 95 |
/// |
96 | 96 |
Node(const Node&) { } |
97 | 97 |
|
98 | 98 |
/// Invalid constructor \& conversion. |
99 | 99 |
|
100 | 100 |
/// This constructor initializes the iterator to be invalid. |
101 | 101 |
/// \sa Invalid for more details. |
102 | 102 |
Node(Invalid) { } |
103 | 103 |
/// Equality operator |
104 | 104 |
|
105 | 105 |
/// Two iterators are equal if and only if they point to the |
106 | 106 |
/// same object or both are invalid. |
107 | 107 |
bool operator==(Node) const { return true; } |
108 | 108 |
|
109 | 109 |
/// Inequality operator |
110 | 110 |
|
111 | 111 |
/// \sa operator==(Node n) |
112 | 112 |
/// |
113 | 113 |
bool operator!=(Node) const { return true; } |
114 | 114 |
|
115 | 115 |
/// Artificial ordering operator. |
116 | 116 |
|
117 | 117 |
/// To allow the use of graph descriptors as key type in std::map or |
118 | 118 |
/// similar associative container we require this. |
119 | 119 |
/// |
120 | 120 |
/// \note This operator only have to define some strict ordering of |
121 | 121 |
/// the items; this order has nothing to do with the iteration |
122 | 122 |
/// ordering of the items. |
123 | 123 |
bool operator<(Node) const { return false; } |
124 | 124 |
|
125 | 125 |
}; |
126 | 126 |
|
127 | 127 |
/// This iterator goes through each node. |
128 | 128 |
|
129 | 129 |
/// This iterator goes through each node. |
130 | 130 |
/// Its usage is quite simple, for example you can count the number |
131 | 131 |
/// of nodes in graph \c g of type \c Graph like this: |
132 | 132 |
///\code |
133 | 133 |
/// int count=0; |
134 | 134 |
/// for (Graph::NodeIt n(g); n!=INVALID; ++n) ++count; |
135 | 135 |
///\endcode |
136 | 136 |
class NodeIt : public Node { |
137 | 137 |
public: |
138 | 138 |
/// Default constructor |
139 | 139 |
|
140 | 140 |
/// @warning The default constructor sets the iterator |
141 | 141 |
/// to an undefined value. |
142 | 142 |
NodeIt() { } |
143 | 143 |
/// Copy constructor. |
144 | 144 |
|
145 | 145 |
/// Copy constructor. |
146 | 146 |
/// |
147 | 147 |
NodeIt(const NodeIt& n) : Node(n) { } |
148 | 148 |
/// Invalid constructor \& conversion. |
149 | 149 |
|
150 | 150 |
/// Initialize the iterator to be invalid. |
151 | 151 |
/// \sa Invalid for more details. |
152 | 152 |
NodeIt(Invalid) { } |
153 | 153 |
/// Sets the iterator to the first node. |
154 | 154 |
|
155 | 155 |
/// Sets the iterator to the first node of \c g. |
156 | 156 |
/// |
157 | 157 |
NodeIt(const Graph&) { } |
158 | 158 |
/// Node -> NodeIt conversion. |
159 | 159 |
|
160 | 160 |
/// Sets the iterator to the node of \c the graph pointed by |
161 | 161 |
/// the trivial iterator. |
162 | 162 |
/// This feature necessitates that each time we |
163 | 163 |
/// iterate the arc-set, the iteration order is the same. |
164 | 164 |
NodeIt(const Graph&, const Node&) { } |
165 | 165 |
/// Next node. |
166 | 166 |
|
167 | 167 |
/// Assign the iterator to the next node. |
168 | 168 |
/// |
169 | 169 |
NodeIt& operator++() { return *this; } |
170 | 170 |
}; |
171 | 171 |
|
172 | 172 |
|
173 | 173 |
/// The base type of the edge iterators. |
174 | 174 |
|
175 | 175 |
/// The base type of the edge iterators. |
176 | 176 |
/// |
177 | 177 |
class Edge { |
178 | 178 |
public: |
179 | 179 |
/// Default constructor |
180 | 180 |
|
181 | 181 |
/// @warning The default constructor sets the iterator |
182 | 182 |
/// to an undefined value. |
183 | 183 |
Edge() { } |
184 | 184 |
/// Copy constructor. |
185 | 185 |
|
186 | 186 |
/// Copy constructor. |
187 | 187 |
/// |
188 | 188 |
Edge(const Edge&) { } |
189 | 189 |
/// Initialize the iterator to be invalid. |
190 | 190 |
|
191 | 191 |
/// Initialize the iterator to be invalid. |
192 | 192 |
/// |
193 | 193 |
Edge(Invalid) { } |
194 | 194 |
/// Equality operator |
195 | 195 |
|
196 | 196 |
/// Two iterators are equal if and only if they point to the |
197 | 197 |
/// same object or both are invalid. |
198 | 198 |
bool operator==(Edge) const { return true; } |
199 | 199 |
/// Inequality operator |
200 | 200 |
|
201 | 201 |
/// \sa operator==(Edge n) |
202 | 202 |
/// |
203 | 203 |
bool operator!=(Edge) const { return true; } |
204 | 204 |
|
205 | 205 |
/// Artificial ordering operator. |
206 | 206 |
|
207 | 207 |
/// To allow the use of graph descriptors as key type in std::map or |
208 | 208 |
/// similar associative container we require this. |
209 | 209 |
/// |
210 | 210 |
/// \note This operator only have to define some strict ordering of |
211 | 211 |
/// the items; this order has nothing to do with the iteration |
212 | 212 |
/// ordering of the items. |
213 | 213 |
bool operator<(Edge) const { return false; } |
214 | 214 |
}; |
215 | 215 |
|
216 | 216 |
/// This iterator goes through each edge. |
217 | 217 |
|
218 | 218 |
/// This iterator goes through each edge of a graph. |
219 | 219 |
/// Its usage is quite simple, for example you can count the number |
220 | 220 |
/// of edges in a graph \c g of type \c Graph as follows: |
221 | 221 |
///\code |
222 | 222 |
/// int count=0; |
223 | 223 |
/// for(Graph::EdgeIt e(g); e!=INVALID; ++e) ++count; |
224 | 224 |
///\endcode |
225 | 225 |
class EdgeIt : public Edge { |
226 | 226 |
public: |
227 | 227 |
/// Default constructor |
228 | 228 |
|
229 | 229 |
/// @warning The default constructor sets the iterator |
230 | 230 |
/// to an undefined value. |
231 | 231 |
EdgeIt() { } |
232 | 232 |
/// Copy constructor. |
233 | 233 |
|
234 | 234 |
/// Copy constructor. |
235 | 235 |
/// |
236 | 236 |
EdgeIt(const EdgeIt& e) : Edge(e) { } |
237 | 237 |
/// Initialize the iterator to be invalid. |
238 | 238 |
|
239 | 239 |
/// Initialize the iterator to be invalid. |
240 | 240 |
/// |
241 | 241 |
EdgeIt(Invalid) { } |
242 | 242 |
/// This constructor sets the iterator to the first edge. |
243 | 243 |
|
244 | 244 |
/// This constructor sets the iterator to the first edge. |
245 | 245 |
EdgeIt(const Graph&) { } |
246 | 246 |
/// Edge -> EdgeIt conversion |
247 | 247 |
|
248 | 248 |
/// Sets the iterator to the value of the trivial iterator. |
249 | 249 |
/// This feature necessitates that each time we |
250 | 250 |
/// iterate the edge-set, the iteration order is the |
251 | 251 |
/// same. |
252 | 252 |
EdgeIt(const Graph&, const Edge&) { } |
253 | 253 |
/// Next edge |
254 | 254 |
|
255 | 255 |
/// Assign the iterator to the next edge. |
256 | 256 |
EdgeIt& operator++() { return *this; } |
257 | 257 |
}; |
258 | 258 |
|
259 | 259 |
/// \brief This iterator goes trough the incident undirected |
260 | 260 |
/// arcs of a node. |
261 | 261 |
/// |
262 | 262 |
/// This iterator goes trough the incident edges |
263 | 263 |
/// of a certain node of a graph. You should assume that the |
264 | 264 |
/// loop arcs will be iterated twice. |
265 | 265 |
/// |
266 | 266 |
/// Its usage is quite simple, for example you can compute the |
267 | 267 |
/// degree (i.e. count the number of incident arcs of a node \c n |
268 | 268 |
/// in graph \c g of type \c Graph as follows. |
269 | 269 |
/// |
270 | 270 |
///\code |
271 | 271 |
/// int count=0; |
272 | 272 |
/// for(Graph::IncEdgeIt e(g, n); e!=INVALID; ++e) ++count; |
273 | 273 |
///\endcode |
274 | 274 |
class IncEdgeIt : public Edge { |
275 | 275 |
public: |
276 | 276 |
/// Default constructor |
277 | 277 |
|
278 | 278 |
/// @warning The default constructor sets the iterator |
279 | 279 |
/// to an undefined value. |
280 | 280 |
IncEdgeIt() { } |
281 | 281 |
/// Copy constructor. |
282 | 282 |
|
283 | 283 |
/// Copy constructor. |
284 | 284 |
/// |
285 | 285 |
IncEdgeIt(const IncEdgeIt& e) : Edge(e) { } |
286 | 286 |
/// Initialize the iterator to be invalid. |
287 | 287 |
|
288 | 288 |
/// Initialize the iterator to be invalid. |
289 | 289 |
/// |
290 | 290 |
IncEdgeIt(Invalid) { } |
291 | 291 |
/// This constructor sets the iterator to first incident arc. |
292 | 292 |
|
293 | 293 |
/// This constructor set the iterator to the first incident arc of |
294 | 294 |
/// the node. |
295 | 295 |
IncEdgeIt(const Graph&, const Node&) { } |
296 | 296 |
/// Edge -> IncEdgeIt conversion |
297 | 297 |
|
298 | 298 |
/// Sets the iterator to the value of the trivial iterator \c e. |
299 | 299 |
/// This feature necessitates that each time we |
300 | 300 |
/// iterate the arc-set, the iteration order is the same. |
301 | 301 |
IncEdgeIt(const Graph&, const Edge&) { } |
302 | 302 |
/// Next incident arc |
303 | 303 |
|
304 | 304 |
/// Assign the iterator to the next incident arc |
305 | 305 |
/// of the corresponding node. |
306 | 306 |
IncEdgeIt& operator++() { return *this; } |
307 | 307 |
}; |
308 | 308 |
|
309 | 309 |
/// The directed arc type. |
310 | 310 |
|
311 | 311 |
/// The directed arc type. It can be converted to the |
312 | 312 |
/// edge or it should be inherited from the undirected |
313 | 313 |
/// arc. |
314 | 314 |
class Arc : public Edge { |
315 | 315 |
public: |
316 | 316 |
/// Default constructor |
317 | 317 |
|
318 | 318 |
/// @warning The default constructor sets the iterator |
319 | 319 |
/// to an undefined value. |
320 | 320 |
Arc() { } |
321 | 321 |
/// Copy constructor. |
322 | 322 |
|
323 | 323 |
/// Copy constructor. |
324 | 324 |
/// |
325 | 325 |
Arc(const Arc& e) : Edge(e) { } |
326 | 326 |
/// Initialize the iterator to be invalid. |
327 | 327 |
|
328 | 328 |
/// Initialize the iterator to be invalid. |
329 | 329 |
/// |
330 | 330 |
Arc(Invalid) { } |
331 | 331 |
/// Equality operator |
332 | 332 |
|
333 | 333 |
/// Two iterators are equal if and only if they point to the |
334 | 334 |
/// same object or both are invalid. |
335 | 335 |
bool operator==(Arc) const { return true; } |
336 | 336 |
/// Inequality operator |
337 | 337 |
|
338 | 338 |
/// \sa operator==(Arc n) |
339 | 339 |
/// |
340 | 340 |
bool operator!=(Arc) const { return true; } |
341 | 341 |
|
342 | 342 |
/// Artificial ordering operator. |
343 | 343 |
|
344 | 344 |
/// To allow the use of graph descriptors as key type in std::map or |
345 | 345 |
/// similar associative container we require this. |
346 | 346 |
/// |
347 | 347 |
/// \note This operator only have to define some strict ordering of |
348 | 348 |
/// the items; this order has nothing to do with the iteration |
349 | 349 |
/// ordering of the items. |
350 | 350 |
bool operator<(Arc) const { return false; } |
351 | 351 |
|
352 | 352 |
}; |
353 | 353 |
/// This iterator goes through each directed arc. |
354 | 354 |
|
355 | 355 |
/// This iterator goes through each arc of a graph. |
356 | 356 |
/// Its usage is quite simple, for example you can count the number |
357 | 357 |
/// of arcs in a graph \c g of type \c Graph as follows: |
358 | 358 |
///\code |
359 | 359 |
/// int count=0; |
360 | 360 |
/// for(Graph::ArcIt e(g); e!=INVALID; ++e) ++count; |
361 | 361 |
///\endcode |
362 | 362 |
class ArcIt : public Arc { |
363 | 363 |
public: |
364 | 364 |
/// Default constructor |
365 | 365 |
|
366 | 366 |
/// @warning The default constructor sets the iterator |
367 | 367 |
/// to an undefined value. |
368 | 368 |
ArcIt() { } |
369 | 369 |
/// Copy constructor. |
370 | 370 |
|
371 | 371 |
/// Copy constructor. |
372 | 372 |
/// |
373 | 373 |
ArcIt(const ArcIt& e) : Arc(e) { } |
374 | 374 |
/// Initialize the iterator to be invalid. |
375 | 375 |
|
376 | 376 |
/// Initialize the iterator to be invalid. |
377 | 377 |
/// |
378 | 378 |
ArcIt(Invalid) { } |
379 | 379 |
/// This constructor sets the iterator to the first arc. |
380 | 380 |
|
381 | 381 |
/// This constructor sets the iterator to the first arc of \c g. |
382 | 382 |
///@param g the graph |
383 | 383 |
ArcIt(const Graph &g) { ignore_unused_variable_warning(g); } |
384 | 384 |
/// Arc -> ArcIt conversion |
385 | 385 |
|
386 | 386 |
/// Sets the iterator to the value of the trivial iterator \c e. |
387 | 387 |
/// This feature necessitates that each time we |
388 | 388 |
/// iterate the arc-set, the iteration order is the same. |
389 | 389 |
ArcIt(const Graph&, const Arc&) { } |
390 | 390 |
///Next arc |
391 | 391 |
|
392 | 392 |
/// Assign the iterator to the next arc. |
393 | 393 |
ArcIt& operator++() { return *this; } |
394 | 394 |
}; |
395 | 395 |
|
396 | 396 |
/// This iterator goes trough the outgoing directed arcs of a node. |
397 | 397 |
|
398 | 398 |
/// This iterator goes trough the \e outgoing arcs of a certain node |
399 | 399 |
/// of a graph. |
400 | 400 |
/// Its usage is quite simple, for example you can count the number |
401 | 401 |
/// of outgoing arcs of a node \c n |
402 | 402 |
/// in graph \c g of type \c Graph as follows. |
403 | 403 |
///\code |
404 | 404 |
/// int count=0; |
405 | 405 |
/// for (Graph::OutArcIt e(g, n); e!=INVALID; ++e) ++count; |
406 | 406 |
///\endcode |
407 | 407 |
|
408 | 408 |
class OutArcIt : public Arc { |
409 | 409 |
public: |
410 | 410 |
/// Default constructor |
411 | 411 |
|
412 | 412 |
/// @warning The default constructor sets the iterator |
413 | 413 |
/// to an undefined value. |
414 | 414 |
OutArcIt() { } |
415 | 415 |
/// Copy constructor. |
416 | 416 |
|
417 | 417 |
/// Copy constructor. |
418 | 418 |
/// |
419 | 419 |
OutArcIt(const OutArcIt& e) : Arc(e) { } |
420 | 420 |
/// Initialize the iterator to be invalid. |
421 | 421 |
|
422 | 422 |
/// Initialize the iterator to be invalid. |
423 | 423 |
/// |
424 | 424 |
OutArcIt(Invalid) { } |
425 | 425 |
/// This constructor sets the iterator to the first outgoing arc. |
426 | 426 |
|
427 | 427 |
/// This constructor sets the iterator to the first outgoing arc of |
428 | 428 |
/// the node. |
429 | 429 |
///@param n the node |
430 | 430 |
///@param g the graph |
431 | 431 |
OutArcIt(const Graph& n, const Node& g) { |
432 | 432 |
ignore_unused_variable_warning(n); |
433 | 433 |
ignore_unused_variable_warning(g); |
434 | 434 |
} |
435 | 435 |
/// Arc -> OutArcIt conversion |
436 | 436 |
|
437 | 437 |
/// Sets the iterator to the value of the trivial iterator. |
438 | 438 |
/// This feature necessitates that each time we |
439 | 439 |
/// iterate the arc-set, the iteration order is the same. |
440 | 440 |
OutArcIt(const Graph&, const Arc&) { } |
441 | 441 |
///Next outgoing arc |
442 | 442 |
|
443 | 443 |
/// Assign the iterator to the next |
444 | 444 |
/// outgoing arc of the corresponding node. |
445 | 445 |
OutArcIt& operator++() { return *this; } |
446 | 446 |
}; |
447 | 447 |
|
448 | 448 |
/// This iterator goes trough the incoming directed arcs of a node. |
449 | 449 |
|
450 | 450 |
/// This iterator goes trough the \e incoming arcs of a certain node |
451 | 451 |
/// of a graph. |
452 | 452 |
/// Its usage is quite simple, for example you can count the number |
453 | 453 |
/// of outgoing arcs of a node \c n |
454 | 454 |
/// in graph \c g of type \c Graph as follows. |
455 | 455 |
///\code |
456 | 456 |
/// int count=0; |
457 | 457 |
/// for(Graph::InArcIt e(g, n); e!=INVALID; ++e) ++count; |
458 | 458 |
///\endcode |
459 | 459 |
|
460 | 460 |
class InArcIt : public Arc { |
461 | 461 |
public: |
462 | 462 |
/// Default constructor |
463 | 463 |
|
464 | 464 |
/// @warning The default constructor sets the iterator |
465 | 465 |
/// to an undefined value. |
466 | 466 |
InArcIt() { } |
467 | 467 |
/// Copy constructor. |
468 | 468 |
|
469 | 469 |
/// Copy constructor. |
470 | 470 |
/// |
471 | 471 |
InArcIt(const InArcIt& e) : Arc(e) { } |
472 | 472 |
/// Initialize the iterator to be invalid. |
473 | 473 |
|
474 | 474 |
/// Initialize the iterator to be invalid. |
475 | 475 |
/// |
476 | 476 |
InArcIt(Invalid) { } |
477 | 477 |
/// This constructor sets the iterator to first incoming arc. |
478 | 478 |
|
479 | 479 |
/// This constructor set the iterator to the first incoming arc of |
480 | 480 |
/// the node. |
481 | 481 |
///@param n the node |
482 | 482 |
///@param g the graph |
483 | 483 |
InArcIt(const Graph& g, const Node& n) { |
484 | 484 |
ignore_unused_variable_warning(n); |
485 | 485 |
ignore_unused_variable_warning(g); |
486 | 486 |
} |
487 | 487 |
/// Arc -> InArcIt conversion |
488 | 488 |
|
489 | 489 |
/// Sets the iterator to the value of the trivial iterator \c e. |
490 | 490 |
/// This feature necessitates that each time we |
491 | 491 |
/// iterate the arc-set, the iteration order is the same. |
492 | 492 |
InArcIt(const Graph&, const Arc&) { } |
493 | 493 |
/// Next incoming arc |
494 | 494 |
|
495 | 495 |
/// Assign the iterator to the next inarc of the corresponding node. |
496 | 496 |
/// |
497 | 497 |
InArcIt& operator++() { return *this; } |
498 | 498 |
}; |
499 | 499 |
|
500 | 500 |
/// \brief Read write map of the nodes to type \c T. |
501 | 501 |
/// |
502 | 502 |
/// ReadWrite map of the nodes to type \c T. |
503 | 503 |
/// \sa Reference |
504 | 504 |
template<class T> |
505 | 505 |
class NodeMap : public ReadWriteMap< Node, T > |
506 | 506 |
{ |
507 | 507 |
public: |
508 | 508 |
|
509 | 509 |
///\e |
510 | 510 |
NodeMap(const Graph&) { } |
511 | 511 |
///\e |
512 | 512 |
NodeMap(const Graph&, T) { } |
513 | 513 |
|
514 | 514 |
private: |
515 | 515 |
///Copy constructor |
516 | 516 |
NodeMap(const NodeMap& nm) : ReadWriteMap< Node, T >(nm) { } |
517 | 517 |
///Assignment operator |
518 | 518 |
template <typename CMap> |
519 | 519 |
NodeMap& operator=(const CMap&) { |
520 | 520 |
checkConcept<ReadMap<Node, T>, CMap>(); |
521 | 521 |
return *this; |
522 | 522 |
} |
523 | 523 |
}; |
524 | 524 |
|
525 | 525 |
/// \brief Read write map of the directed arcs to type \c T. |
526 | 526 |
/// |
527 | 527 |
/// Reference map of the directed arcs to type \c T. |
528 | 528 |
/// \sa Reference |
529 | 529 |
template<class T> |
530 | 530 |
class ArcMap : public ReadWriteMap<Arc,T> |
531 | 531 |
{ |
532 | 532 |
public: |
533 | 533 |
|
534 | 534 |
///\e |
535 | 535 |
ArcMap(const Graph&) { } |
536 | 536 |
///\e |
537 | 537 |
ArcMap(const Graph&, T) { } |
538 | 538 |
private: |
539 | 539 |
///Copy constructor |
540 | 540 |
ArcMap(const ArcMap& em) : ReadWriteMap<Arc,T>(em) { } |
541 | 541 |
///Assignment operator |
542 | 542 |
template <typename CMap> |
543 | 543 |
ArcMap& operator=(const CMap&) { |
544 | 544 |
checkConcept<ReadMap<Arc, T>, CMap>(); |
545 | 545 |
return *this; |
546 | 546 |
} |
547 | 547 |
}; |
548 | 548 |
|
549 | 549 |
/// Read write map of the edges to type \c T. |
550 | 550 |
|
551 | 551 |
/// Reference map of the arcs to type \c T. |
552 | 552 |
/// \sa Reference |
553 | 553 |
template<class T> |
554 | 554 |
class EdgeMap : public ReadWriteMap<Edge,T> |
555 | 555 |
{ |
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 {} |
669 | 681 |
void next(Arc&) const {} |
670 | 682 |
|
671 | 683 |
void firstOut(Arc&, Node) const {} |
672 | 684 |
void nextOut(Arc&) const {} |
673 | 685 |
|
674 | 686 |
void firstIn(Arc&, Node) const {} |
675 | 687 |
void nextIn(Arc&) const {} |
676 | 688 |
|
677 | 689 |
void firstInc(Edge &, bool &, const Node &) const {} |
678 | 690 |
void nextInc(Edge &, bool &) const {} |
679 | 691 |
|
680 | 692 |
// The second parameter is dummy. |
681 | 693 |
Node fromId(int, Node) const { return INVALID; } |
682 | 694 |
// The second parameter is dummy. |
683 | 695 |
Edge fromId(int, Edge) const { return INVALID; } |
684 | 696 |
// The second parameter is dummy. |
685 | 697 |
Arc fromId(int, Arc) const { return INVALID; } |
686 | 698 |
|
687 | 699 |
// Dummy parameter. |
688 | 700 |
int maxId(Node) const { return -1; } |
689 | 701 |
// Dummy parameter. |
690 | 702 |
int maxId(Edge) const { return -1; } |
691 | 703 |
// Dummy parameter. |
692 | 704 |
int maxId(Arc) const { return -1; } |
693 | 705 |
|
694 | 706 |
/// \brief Base node of the iterator |
695 | 707 |
/// |
696 | 708 |
/// Returns the base node (the source in this case) of the iterator |
697 | 709 |
Node baseNode(OutArcIt e) const { |
698 | 710 |
return source(e); |
699 | 711 |
} |
700 | 712 |
/// \brief Running node of the iterator |
701 | 713 |
/// |
702 | 714 |
/// Returns the running node (the target in this case) of the |
703 | 715 |
/// iterator |
704 | 716 |
Node runningNode(OutArcIt e) const { |
705 | 717 |
return target(e); |
706 | 718 |
} |
707 | 719 |
|
708 | 720 |
/// \brief Base node of the iterator |
709 | 721 |
/// |
710 | 722 |
/// Returns the base node (the target in this case) of the iterator |
711 | 723 |
Node baseNode(InArcIt e) const { |
712 | 724 |
return target(e); |
713 | 725 |
} |
714 | 726 |
/// \brief Running node of the iterator |
715 | 727 |
/// |
716 | 728 |
/// Returns the running node (the source in this case) of the |
717 | 729 |
/// iterator |
718 | 730 |
Node runningNode(InArcIt e) const { |
719 | 731 |
return source(e); |
720 | 732 |
} |
721 | 733 |
|
722 | 734 |
/// \brief Base node of the iterator |
723 | 735 |
/// |
724 | 736 |
/// Returns the base node of the iterator |
725 | 737 |
Node baseNode(IncEdgeIt) const { |
726 | 738 |
return INVALID; |
727 | 739 |
} |
728 | 740 |
|
729 | 741 |
/// \brief Running node of the iterator |
730 | 742 |
/// |
731 | 743 |
/// Returns the running node of the iterator |
732 | 744 |
Node runningNode(IncEdgeIt) const { |
733 | 745 |
return INVALID; |
734 | 746 |
} |
735 | 747 |
|
736 | 748 |
template <typename _Graph> |
737 | 749 |
struct Constraints { |
738 | 750 |
void constraints() { |
739 | 751 |
checkConcept<IterableGraphComponent<>, _Graph>(); |
740 | 752 |
checkConcept<IDableGraphComponent<>, _Graph>(); |
741 | 753 |
checkConcept<MappableGraphComponent<>, _Graph>(); |
742 | 754 |
} |
743 | 755 |
}; |
744 | 756 |
|
745 | 757 |
}; |
746 | 758 |
|
747 | 759 |
} |
748 | 760 |
|
749 | 761 |
} |
750 | 762 |
|
751 | 763 |
#endif |
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)