!
!
!
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
namespace lemon { |
20 | 20 |
|
21 | 21 |
/** |
22 | 22 |
@defgroup datas Data Structures |
23 | 23 |
This group contains the several data structures implemented in LEMON. |
24 | 24 |
*/ |
25 | 25 |
|
26 | 26 |
/** |
27 | 27 |
@defgroup graphs Graph Structures |
28 | 28 |
@ingroup datas |
29 | 29 |
\brief Graph structures implemented in LEMON. |
30 | 30 |
|
31 | 31 |
The implementation of combinatorial algorithms heavily relies on |
32 | 32 |
efficient graph implementations. LEMON offers data structures which are |
33 | 33 |
planned to be easily used in an experimental phase of implementation studies, |
34 | 34 |
and thereafter the program code can be made efficient by small modifications. |
35 | 35 |
|
36 | 36 |
The most efficient implementation of diverse applications require the |
37 | 37 |
usage of different physical graph implementations. These differences |
38 | 38 |
appear in the size of graph we require to handle, memory or time usage |
39 | 39 |
limitations or in the set of operations through which the graph can be |
40 | 40 |
accessed. LEMON provides several physical graph structures to meet |
41 | 41 |
the diverging requirements of the possible users. In order to save on |
42 | 42 |
running time or on memory usage, some structures may fail to provide |
43 | 43 |
some graph features like arc/edge or node deletion. |
44 | 44 |
|
45 | 45 |
Alteration of standard containers need a very limited number of |
46 | 46 |
operations, these together satisfy the everyday requirements. |
47 | 47 |
In the case of graph structures, different operations are needed which do |
48 | 48 |
not alter the physical graph, but gives another view. If some nodes or |
49 | 49 |
arcs have to be hidden or the reverse oriented graph have to be used, then |
50 | 50 |
this is the case. It also may happen that in a flow implementation |
51 | 51 |
the residual graph can be accessed by another algorithm, or a node-set |
52 | 52 |
is to be shrunk for another algorithm. |
53 | 53 |
LEMON also provides a variety of graphs for these requirements called |
54 | 54 |
\ref graph_adaptors "graph adaptors". Adaptors cannot be used alone but only |
55 | 55 |
in conjunction with other graph representations. |
56 | 56 |
|
57 | 57 |
You are free to use the graph structure that fit your requirements |
58 | 58 |
the best, most graph algorithms and auxiliary data structures can be used |
59 | 59 |
with any graph structure. |
60 | 60 |
|
61 | 61 |
<b>See also:</b> \ref graph_concepts "Graph Structure Concepts". |
62 | 62 |
*/ |
63 | 63 |
|
64 | 64 |
/** |
65 | 65 |
@defgroup graph_adaptors Adaptor Classes for Graphs |
66 | 66 |
@ingroup graphs |
67 | 67 |
\brief Adaptor classes for digraphs and graphs |
68 | 68 |
|
69 | 69 |
This group contains several useful adaptor classes for digraphs and graphs. |
... | ... |
@@ -214,160 +214,160 @@ |
214 | 214 |
|
215 | 215 |
typedef DivMap<DoubleArcMap, DoubleArcMap> TimeMap; |
216 | 216 |
TimeMap time(length, speed); |
217 | 217 |
|
218 | 218 |
Dijkstra<Digraph, TimeMap> dijkstra(graph, time); |
219 | 219 |
dijkstra.run(source, target); |
220 | 220 |
\endcode |
221 | 221 |
We have a length map and a maximum speed map on the arcs of a digraph. |
222 | 222 |
The minimum time to pass the arc can be calculated as the division of |
223 | 223 |
the two maps which can be done implicitly with the \c DivMap template |
224 | 224 |
class. We use the implicit minimum time map as the length map of the |
225 | 225 |
\c Dijkstra algorithm. |
226 | 226 |
*/ |
227 | 227 |
|
228 | 228 |
/** |
229 | 229 |
@defgroup paths Path Structures |
230 | 230 |
@ingroup datas |
231 | 231 |
\brief %Path structures implemented in LEMON. |
232 | 232 |
|
233 | 233 |
This group contains the path structures implemented in LEMON. |
234 | 234 |
|
235 | 235 |
LEMON provides flexible data structures to work with paths. |
236 | 236 |
All of them have similar interfaces and they can be copied easily with |
237 | 237 |
assignment operators and copy constructors. This makes it easy and |
238 | 238 |
efficient to have e.g. the Dijkstra algorithm to store its result in |
239 | 239 |
any kind of path structure. |
240 | 240 |
|
241 | 241 |
\sa lemon::concepts::Path |
242 | 242 |
*/ |
243 | 243 |
|
244 | 244 |
/** |
245 | 245 |
@defgroup auxdat Auxiliary Data Structures |
246 | 246 |
@ingroup datas |
247 | 247 |
\brief Auxiliary data structures implemented in LEMON. |
248 | 248 |
|
249 | 249 |
This group contains some data structures implemented in LEMON in |
250 | 250 |
order to make it easier to implement combinatorial algorithms. |
251 | 251 |
*/ |
252 | 252 |
|
253 | 253 |
/** |
254 | 254 |
@defgroup algs Algorithms |
255 | 255 |
\brief This group contains the several algorithms |
256 | 256 |
implemented in LEMON. |
257 | 257 |
|
258 | 258 |
This group contains the several algorithms |
259 | 259 |
implemented in LEMON. |
260 | 260 |
*/ |
261 | 261 |
|
262 | 262 |
/** |
263 | 263 |
@defgroup search Graph Search |
264 | 264 |
@ingroup algs |
265 | 265 |
\brief Common graph search algorithms. |
266 | 266 |
|
267 | 267 |
This group contains the common graph search algorithms, namely |
268 | 268 |
\e breadth-first \e search (BFS) and \e depth-first \e search (DFS). |
269 | 269 |
*/ |
270 | 270 |
|
271 | 271 |
/** |
272 | 272 |
@defgroup shortest_path Shortest Path Algorithms |
273 | 273 |
@ingroup algs |
274 | 274 |
\brief Algorithms for finding shortest paths. |
275 | 275 |
|
276 | 276 |
This group contains the algorithms for finding shortest paths in digraphs. |
277 | 277 |
|
278 |
- \ref Dijkstra Dijkstra's algorithm for finding shortest paths from a |
|
278 |
- \ref Dijkstra Dijkstra's algorithm for finding shortest paths from a |
|
279 | 279 |
source node when all arc lengths are non-negative. |
280 | 280 |
- \ref Suurballe A successive shortest path algorithm for finding |
281 | 281 |
arc-disjoint paths between two nodes having minimum total length. |
282 | 282 |
*/ |
283 | 283 |
|
284 | 284 |
/** |
285 | 285 |
@defgroup max_flow Maximum Flow Algorithms |
286 | 286 |
@ingroup algs |
287 | 287 |
\brief Algorithms for finding maximum flows. |
288 | 288 |
|
289 | 289 |
This group contains the algorithms for finding maximum flows and |
290 | 290 |
feasible circulations. |
291 | 291 |
|
292 | 292 |
The \e maximum \e flow \e problem is to find a flow of maximum value between |
293 | 293 |
a single source and a single target. Formally, there is a \f$G=(V,A)\f$ |
294 | 294 |
digraph, a \f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function and |
295 | 295 |
\f$s, t \in V\f$ source and target nodes. |
296 | 296 |
A maximum flow is an \f$f: A\rightarrow\mathbf{R}^+_0\f$ solution of the |
297 | 297 |
following optimization problem. |
298 | 298 |
|
299 | 299 |
\f[ \max\sum_{sv\in A} f(sv) - \sum_{vs\in A} f(vs) \f] |
300 | 300 |
\f[ \sum_{uv\in A} f(uv) = \sum_{vu\in A} f(vu) |
301 | 301 |
\quad \forall u\in V\setminus\{s,t\} \f] |
302 | 302 |
\f[ 0 \leq f(uv) \leq cap(uv) \quad \forall uv\in A \f] |
303 | 303 |
|
304 | 304 |
\ref Preflow implements the preflow push-relabel algorithm of Goldberg and |
305 | 305 |
Tarjan for solving this problem. It also provides functions to query the |
306 | 306 |
minimum cut, which is the dual problem of maximum flow. |
307 | 307 |
|
308 | 308 |
|
309 |
\ref Circulation is a preflow push-relabel algorithm implemented directly |
|
309 |
\ref Circulation is a preflow push-relabel algorithm implemented directly |
|
310 | 310 |
for finding feasible circulations, which is a somewhat different problem, |
311 | 311 |
but it is strongly related to maximum flow. |
312 | 312 |
For more information, see \ref Circulation. |
313 | 313 |
*/ |
314 | 314 |
|
315 | 315 |
/** |
316 | 316 |
@defgroup min_cost_flow_algs Minimum Cost Flow Algorithms |
317 | 317 |
@ingroup algs |
318 | 318 |
|
319 | 319 |
\brief Algorithms for finding minimum cost flows and circulations. |
320 | 320 |
|
321 | 321 |
This group contains the algorithms for finding minimum cost flows and |
322 | 322 |
circulations. For more information about this problem and its dual |
323 | 323 |
solution see \ref min_cost_flow "Minimum Cost Flow Problem". |
324 | 324 |
|
325 | 325 |
\ref NetworkSimplex is an efficient implementation of the primal Network |
326 | 326 |
Simplex algorithm for finding minimum cost flows. It also provides dual |
327 | 327 |
solution (node potentials), if an optimal flow is found. |
328 | 328 |
*/ |
329 | 329 |
|
330 | 330 |
/** |
331 | 331 |
@defgroup min_cut Minimum Cut Algorithms |
332 | 332 |
@ingroup algs |
333 | 333 |
|
334 | 334 |
\brief Algorithms for finding minimum cut in graphs. |
335 | 335 |
|
336 | 336 |
This group contains the algorithms for finding minimum cut in graphs. |
337 | 337 |
|
338 | 338 |
The \e minimum \e cut \e problem is to find a non-empty and non-complete |
339 | 339 |
\f$X\f$ subset of the nodes with minimum overall capacity on |
340 | 340 |
outgoing arcs. Formally, there is a \f$G=(V,A)\f$ digraph, a |
341 | 341 |
\f$cap: A\rightarrow\mathbf{R}^+_0\f$ capacity function. The minimum |
342 | 342 |
cut is the \f$X\f$ solution of the next optimization problem: |
343 | 343 |
|
344 | 344 |
\f[ \min_{X \subset V, X\not\in \{\emptyset, V\}} |
345 | 345 |
\sum_{uv\in A, u\in X, v\not\in X}cap(uv) \f] |
346 | 346 |
|
347 | 347 |
LEMON contains several algorithms related to minimum cut problems: |
348 | 348 |
|
349 | 349 |
- \ref HaoOrlin "Hao-Orlin algorithm" for calculating minimum cut |
350 | 350 |
in directed graphs. |
351 | 351 |
- \ref GomoryHu "Gomory-Hu tree computation" for calculating |
352 | 352 |
all-pairs minimum cut in undirected graphs. |
353 | 353 |
|
354 | 354 |
If you want to find minimum cut just between two distinict nodes, |
355 | 355 |
see the \ref max_flow "maximum flow problem". |
356 | 356 |
*/ |
357 | 357 |
|
358 | 358 |
/** |
359 | 359 |
@defgroup graph_properties Connectivity and Other Graph Properties |
360 | 360 |
@ingroup algs |
361 | 361 |
\brief Algorithms for discovering the graph properties |
362 | 362 |
|
363 | 363 |
This group contains the algorithms for discovering the graph properties |
364 | 364 |
like connectivity, bipartiteness, euler property, simplicity etc. |
365 | 365 |
|
366 | 366 |
\image html edge_biconnected_components.png |
367 | 367 |
\image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth |
368 | 368 |
*/ |
369 | 369 |
|
370 | 370 |
/** |
371 | 371 |
@defgroup matching Matching Algorithms |
372 | 372 |
@ingroup algs |
373 | 373 |
\brief Algorithms for finding matchings in graphs and bipartite graphs. |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
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 |
|
23 | 23 |
|
24 | 24 |
\page lgf-format LEMON Graph Format (LGF) |
25 | 25 |
|
26 | 26 |
The \e LGF is a <em>column oriented</em> |
27 | 27 |
file format for storing graphs and associated data like |
28 | 28 |
node and edge maps. |
29 | 29 |
|
30 | 30 |
Each line with \c '#' first non-whitespace |
31 | 31 |
character is considered as a comment line. |
32 | 32 |
|
33 | 33 |
Otherwise the file consists of sections starting with |
34 | 34 |
a header line. The header lines starts with an \c '@' character followed by the |
35 | 35 |
type of section. The standard section types are \c \@nodes, \c |
36 | 36 |
\@arcs and \c \@edges |
37 | 37 |
and \@attributes. Each header line may also have an optional |
38 | 38 |
\e name, which can be use to distinguish the sections of the same |
39 | 39 |
type. |
40 | 40 |
|
41 | 41 |
The standard sections are column oriented, each line consists of |
42 | 42 |
<em>token</em>s separated by whitespaces. A token can be \e plain or |
43 | 43 |
\e quoted. A plain token is just a sequence of non-whitespace characters, |
44 | 44 |
while a quoted token is a |
45 | 45 |
character sequence surrounded by double quotes, and it can also |
46 | 46 |
contain whitespaces and escape sequences. |
47 | 47 |
|
48 | 48 |
The \c \@nodes section describes a set of nodes and associated |
49 | 49 |
maps. The first is a header line, its columns are the names of the |
50 | 50 |
maps appearing in the following lines. |
51 | 51 |
One of the maps must be called \c |
52 | 52 |
"label", which plays special role in the file. |
53 | 53 |
The following |
54 | 54 |
non-empty lines until the next section describes nodes of the |
55 | 55 |
graph. Each line contains the values of the node maps |
56 | 56 |
associated to the current node. |
57 | 57 |
|
58 | 58 |
\code |
59 | 59 |
@nodes |
60 | 60 |
label coordinates size title |
61 | 61 |
1 (10,20) 10 "First node" |
62 | 62 |
2 (80,80) 8 "Second node" |
63 | 63 |
3 (40,10) 10 "Third node" |
64 | 64 |
\endcode |
65 | 65 |
|
66 | 66 |
The \c \@arcs section is very similar to the \c \@nodes section, it |
67 | 67 |
again starts with a header line describing the names of the maps, but |
68 | 68 |
the \c "label" map is not obligatory here. The following lines |
69 | 69 |
describe the arcs. The first two tokens of each line are the source |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
namespace lemon { |
20 | 20 |
|
21 | 21 |
/** |
22 | 22 |
\page min_cost_flow Minimum Cost Flow Problem |
23 | 23 |
|
24 | 24 |
\section mcf_def Definition (GEQ form) |
25 | 25 |
|
26 | 26 |
The \e minimum \e cost \e flow \e problem is to find a feasible flow of |
27 | 27 |
minimum total cost from a set of supply nodes to a set of demand nodes |
28 | 28 |
in a network with capacity constraints (lower and upper bounds) |
29 | 29 |
and arc costs. |
30 | 30 |
|
31 | 31 |
Formally, let \f$G=(V,A)\f$ be a digraph, \f$lower: A\rightarrow\mathbf{R}\f$, |
32 | 32 |
\f$upper: A\rightarrow\mathbf{R}\cup\{+\infty\}\f$ denote the lower and |
33 | 33 |
upper bounds for the flow values on the arcs, for which |
34 | 34 |
\f$lower(uv) \leq upper(uv)\f$ must hold for all \f$uv\in A\f$, |
35 | 35 |
\f$cost: A\rightarrow\mathbf{R}\f$ denotes the cost per unit flow |
36 | 36 |
on the arcs and \f$sup: V\rightarrow\mathbf{R}\f$ denotes the |
37 | 37 |
signed supply values of the nodes. |
38 | 38 |
If \f$sup(u)>0\f$, then \f$u\f$ is a supply node with \f$sup(u)\f$ |
39 | 39 |
supply, if \f$sup(u)<0\f$, then \f$u\f$ is a demand node with |
40 | 40 |
\f$-sup(u)\f$ demand. |
41 | 41 |
A minimum cost flow is an \f$f: A\rightarrow\mathbf{R}\f$ solution |
42 | 42 |
of the following optimization problem. |
43 | 43 |
|
44 | 44 |
\f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f] |
45 | 45 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \geq |
46 | 46 |
sup(u) \quad \forall u\in V \f] |
47 | 47 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f] |
48 | 48 |
|
49 | 49 |
The sum of the supply values, i.e. \f$\sum_{u\in V} sup(u)\f$ must be |
50 | 50 |
zero or negative in order to have a feasible solution (since the sum |
51 | 51 |
of the expressions on the left-hand side of the inequalities is zero). |
52 | 52 |
It means that the total demand must be greater or equal to the total |
53 | 53 |
supply and all the supplies have to be carried out from the supply nodes, |
54 | 54 |
but there could be demands that are not satisfied. |
55 | 55 |
If \f$\sum_{u\in V} sup(u)\f$ is zero, then all the supply/demand |
56 | 56 |
constraints have to be satisfied with equality, i.e. all demands |
57 | 57 |
have to be satisfied and all supplies have to be used. |
58 | 58 |
|
59 | 59 |
|
60 | 60 |
\section mcf_algs Algorithms |
61 | 61 |
|
62 | 62 |
LEMON contains several algorithms for solving this problem, for more |
63 | 63 |
information see \ref min_cost_flow_algs "Minimum Cost Flow Algorithms". |
64 | 64 |
|
65 | 65 |
A feasible solution for this problem can be found using \ref Circulation. |
66 | 66 |
|
67 | 67 |
|
68 | 68 |
\section mcf_dual Dual Solution |
69 | 69 |
|
70 | 70 |
The dual solution of the minimum cost flow problem is represented by |
71 | 71 |
node potentials \f$\pi: V\rightarrow\mathbf{R}\f$. |
72 | 72 |
An \f$f: A\rightarrow\mathbf{R}\f$ primal feasible solution is optimal |
73 | 73 |
if and only if for some \f$\pi: V\rightarrow\mathbf{R}\f$ node potentials |
74 | 74 |
the following \e complementary \e slackness optimality conditions hold. |
75 | 75 |
|
76 | 76 |
- For all \f$uv\in A\f$ arcs: |
77 | 77 |
- if \f$cost^\pi(uv)>0\f$, then \f$f(uv)=lower(uv)\f$; |
78 | 78 |
- if \f$lower(uv)<f(uv)<upper(uv)\f$, then \f$cost^\pi(uv)=0\f$; |
79 | 79 |
- if \f$cost^\pi(uv)<0\f$, then \f$f(uv)=upper(uv)\f$. |
80 | 80 |
- For all \f$u\in V\f$ nodes: |
81 | 81 |
- \f$\pi(u)<=0\f$; |
82 | 82 |
- if \f$\sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \neq sup(u)\f$, |
83 | 83 |
then \f$\pi(u)=0\f$. |
84 |
|
|
84 |
|
|
85 | 85 |
Here \f$cost^\pi(uv)\f$ denotes the \e reduced \e cost of the arc |
86 | 86 |
\f$uv\in A\f$ with respect to the potential function \f$\pi\f$, i.e. |
87 | 87 |
\f[ cost^\pi(uv) = cost(uv) + \pi(u) - \pi(v).\f] |
88 | 88 |
|
89 | 89 |
All algorithms provide dual solution (node potentials), as well, |
90 | 90 |
if an optimal flow is found. |
91 | 91 |
|
92 | 92 |
|
93 | 93 |
\section mcf_eq Equality Form |
94 | 94 |
|
95 | 95 |
The above \ref mcf_def "definition" is actually more general than the |
96 | 96 |
usual formulation of the minimum cost flow problem, in which strict |
97 | 97 |
equalities are required in the supply/demand contraints. |
98 | 98 |
|
99 | 99 |
\f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f] |
100 | 100 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) = |
101 | 101 |
sup(u) \quad \forall u\in V \f] |
102 | 102 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f] |
103 | 103 |
|
104 | 104 |
However if the sum of the supply values is zero, then these two problems |
105 | 105 |
are equivalent. |
106 | 106 |
The \ref min_cost_flow_algs "algorithms" in LEMON support the general |
107 | 107 |
form, so if you need the equality form, you have to ensure this additional |
108 | 108 |
contraint manually. |
109 | 109 |
|
110 | 110 |
|
111 | 111 |
\section mcf_leq Opposite Inequalites (LEQ Form) |
112 | 112 |
|
113 | 113 |
Another possible definition of the minimum cost flow problem is |
114 | 114 |
when there are <em>"less or equal"</em> (LEQ) supply/demand constraints, |
115 | 115 |
instead of the <em>"greater or equal"</em> (GEQ) constraints. |
116 | 116 |
|
117 | 117 |
\f[ \min\sum_{uv\in A} f(uv) \cdot cost(uv) \f] |
118 | 118 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \leq |
119 | 119 |
sup(u) \quad \forall u\in V \f] |
120 | 120 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A \f] |
121 | 121 |
|
122 |
It means that the total demand must be less or equal to the |
|
122 |
It means that the total demand must be less or equal to the |
|
123 | 123 |
total supply (i.e. \f$\sum_{u\in V} sup(u)\f$ must be zero or |
124 | 124 |
positive) and all the demands have to be satisfied, but there |
125 | 125 |
could be supplies that are not carried out from the supply |
126 | 126 |
nodes. |
127 | 127 |
The equality form is also a special case of this form, of course. |
128 | 128 |
|
129 | 129 |
You could easily transform this case to the \ref mcf_def "GEQ form" |
130 | 130 |
of the problem by reversing the direction of the arcs and taking the |
131 | 131 |
negative of the supply values (e.g. using \ref ReverseDigraph and |
132 | 132 |
\ref NegMap adaptors). |
133 | 133 |
However \ref NetworkSimplex algorithm also supports this form directly |
134 | 134 |
for the sake of convenience. |
135 | 135 |
|
136 | 136 |
Note that the optimality conditions for this supply constraint type are |
137 | 137 |
slightly differ from the conditions that are discussed for the GEQ form, |
138 | 138 |
namely the potentials have to be non-negative instead of non-positive. |
139 | 139 |
An \f$f: A\rightarrow\mathbf{R}\f$ feasible solution of this problem |
140 | 140 |
is optimal if and only if for some \f$\pi: V\rightarrow\mathbf{R}\f$ |
141 | 141 |
node potentials the following conditions hold. |
142 | 142 |
|
143 | 143 |
- For all \f$uv\in A\f$ arcs: |
144 | 144 |
- if \f$cost^\pi(uv)>0\f$, then \f$f(uv)=lower(uv)\f$; |
145 | 145 |
- if \f$lower(uv)<f(uv)<upper(uv)\f$, then \f$cost^\pi(uv)=0\f$; |
146 | 146 |
- if \f$cost^\pi(uv)<0\f$, then \f$f(uv)=upper(uv)\f$. |
147 | 147 |
- For all \f$u\in V\f$ nodes: |
148 | 148 |
- \f$\pi(u)>=0\f$; |
149 | 149 |
- if \f$\sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) \neq sup(u)\f$, |
150 | 150 |
then \f$\pi(u)=0\f$. |
151 | 151 |
|
152 | 152 |
*/ |
153 | 153 |
} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_ADAPTORS_H |
20 | 20 |
#define LEMON_ADAPTORS_H |
21 | 21 |
|
22 | 22 |
/// \ingroup graph_adaptors |
23 | 23 |
/// \file |
24 | 24 |
/// \brief Adaptor classes for digraphs and graphs |
25 | 25 |
/// |
26 | 26 |
/// This file contains several useful adaptors for digraphs and graphs. |
27 | 27 |
|
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/maps.h> |
30 | 30 |
#include <lemon/bits/variant.h> |
31 | 31 |
|
32 | 32 |
#include <lemon/bits/graph_adaptor_extender.h> |
33 | 33 |
#include <lemon/bits/map_extender.h> |
34 | 34 |
#include <lemon/tolerance.h> |
35 | 35 |
|
36 | 36 |
#include <algorithm> |
37 | 37 |
|
38 | 38 |
namespace lemon { |
39 | 39 |
|
40 | 40 |
#ifdef _MSC_VER |
41 | 41 |
#define LEMON_SCOPE_FIX(OUTER, NESTED) OUTER::NESTED |
42 | 42 |
#else |
43 | 43 |
#define LEMON_SCOPE_FIX(OUTER, NESTED) typename OUTER::template NESTED |
44 | 44 |
#endif |
45 | 45 |
|
46 | 46 |
template<typename DGR> |
47 | 47 |
class DigraphAdaptorBase { |
48 | 48 |
public: |
49 | 49 |
typedef DGR Digraph; |
50 | 50 |
typedef DigraphAdaptorBase Adaptor; |
51 | 51 |
|
52 | 52 |
protected: |
53 | 53 |
DGR* _digraph; |
54 | 54 |
DigraphAdaptorBase() : _digraph(0) { } |
55 | 55 |
void initialize(DGR& digraph) { _digraph = &digraph; } |
56 | 56 |
|
57 | 57 |
public: |
58 | 58 |
DigraphAdaptorBase(DGR& digraph) : _digraph(&digraph) { } |
59 | 59 |
|
60 | 60 |
typedef typename DGR::Node Node; |
61 | 61 |
typedef typename DGR::Arc Arc; |
62 | 62 |
|
63 | 63 |
void first(Node& i) const { _digraph->first(i); } |
64 | 64 |
void first(Arc& i) const { _digraph->first(i); } |
65 | 65 |
void firstIn(Arc& i, const Node& n) const { _digraph->firstIn(i, n); } |
66 | 66 |
void firstOut(Arc& i, const Node& n ) const { _digraph->firstOut(i, n); } |
67 | 67 |
|
68 | 68 |
void next(Node& i) const { _digraph->next(i); } |
69 | 69 |
void next(Arc& i) const { _digraph->next(i); } |
... | ... |
@@ -357,386 +357,386 @@ |
357 | 357 |
/// It conforms to the \ref concepts::Digraph "Digraph" concept. |
358 | 358 |
/// |
359 | 359 |
/// The adapted digraph can also be modified through this adaptor |
360 | 360 |
/// by adding or removing nodes or arcs, unless the \c GR template |
361 | 361 |
/// parameter is set to be \c const. |
362 | 362 |
/// |
363 | 363 |
/// \tparam DGR The type of the adapted digraph. |
364 | 364 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
365 | 365 |
/// It can also be specified to be \c const. |
366 | 366 |
/// |
367 | 367 |
/// \note The \c Node and \c Arc types of this adaptor and the adapted |
368 | 368 |
/// digraph are convertible to each other. |
369 | 369 |
template<typename DGR> |
370 | 370 |
#ifdef DOXYGEN |
371 | 371 |
class ReverseDigraph { |
372 | 372 |
#else |
373 | 373 |
class ReverseDigraph : |
374 | 374 |
public DigraphAdaptorExtender<ReverseDigraphBase<DGR> > { |
375 | 375 |
#endif |
376 | 376 |
typedef DigraphAdaptorExtender<ReverseDigraphBase<DGR> > Parent; |
377 | 377 |
public: |
378 | 378 |
/// The type of the adapted digraph. |
379 | 379 |
typedef DGR Digraph; |
380 | 380 |
protected: |
381 | 381 |
ReverseDigraph() { } |
382 | 382 |
public: |
383 | 383 |
|
384 | 384 |
/// \brief Constructor |
385 | 385 |
/// |
386 | 386 |
/// Creates a reverse digraph adaptor for the given digraph. |
387 | 387 |
explicit ReverseDigraph(DGR& digraph) { |
388 | 388 |
Parent::initialize(digraph); |
389 | 389 |
} |
390 | 390 |
}; |
391 | 391 |
|
392 | 392 |
/// \brief Returns a read-only ReverseDigraph adaptor |
393 | 393 |
/// |
394 | 394 |
/// This function just returns a read-only \ref ReverseDigraph adaptor. |
395 | 395 |
/// \ingroup graph_adaptors |
396 | 396 |
/// \relates ReverseDigraph |
397 | 397 |
template<typename DGR> |
398 | 398 |
ReverseDigraph<const DGR> reverseDigraph(const DGR& digraph) { |
399 | 399 |
return ReverseDigraph<const DGR>(digraph); |
400 | 400 |
} |
401 | 401 |
|
402 | 402 |
|
403 | 403 |
template <typename DGR, typename NF, typename AF, bool ch = true> |
404 | 404 |
class SubDigraphBase : public DigraphAdaptorBase<DGR> { |
405 | 405 |
typedef DigraphAdaptorBase<DGR> Parent; |
406 | 406 |
public: |
407 | 407 |
typedef DGR Digraph; |
408 | 408 |
typedef NF NodeFilterMap; |
409 | 409 |
typedef AF ArcFilterMap; |
410 | 410 |
|
411 | 411 |
typedef SubDigraphBase Adaptor; |
412 | 412 |
protected: |
413 | 413 |
NF* _node_filter; |
414 | 414 |
AF* _arc_filter; |
415 | 415 |
SubDigraphBase() |
416 | 416 |
: Parent(), _node_filter(0), _arc_filter(0) { } |
417 | 417 |
|
418 | 418 |
void initialize(DGR& digraph, NF& node_filter, AF& arc_filter) { |
419 | 419 |
Parent::initialize(digraph); |
420 | 420 |
_node_filter = &node_filter; |
421 |
_arc_filter = &arc_filter; |
|
421 |
_arc_filter = &arc_filter; |
|
422 | 422 |
} |
423 | 423 |
|
424 | 424 |
public: |
425 | 425 |
|
426 | 426 |
typedef typename Parent::Node Node; |
427 | 427 |
typedef typename Parent::Arc Arc; |
428 | 428 |
|
429 | 429 |
void first(Node& i) const { |
430 | 430 |
Parent::first(i); |
431 | 431 |
while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); |
432 | 432 |
} |
433 | 433 |
|
434 | 434 |
void first(Arc& i) const { |
435 | 435 |
Parent::first(i); |
436 | 436 |
while (i != INVALID && (!(*_arc_filter)[i] |
437 | 437 |
|| !(*_node_filter)[Parent::source(i)] |
438 | 438 |
|| !(*_node_filter)[Parent::target(i)])) |
439 | 439 |
Parent::next(i); |
440 | 440 |
} |
441 | 441 |
|
442 | 442 |
void firstIn(Arc& i, const Node& n) const { |
443 | 443 |
Parent::firstIn(i, n); |
444 | 444 |
while (i != INVALID && (!(*_arc_filter)[i] |
445 | 445 |
|| !(*_node_filter)[Parent::source(i)])) |
446 | 446 |
Parent::nextIn(i); |
447 | 447 |
} |
448 | 448 |
|
449 | 449 |
void firstOut(Arc& i, const Node& n) const { |
450 | 450 |
Parent::firstOut(i, n); |
451 | 451 |
while (i != INVALID && (!(*_arc_filter)[i] |
452 | 452 |
|| !(*_node_filter)[Parent::target(i)])) |
453 | 453 |
Parent::nextOut(i); |
454 | 454 |
} |
455 | 455 |
|
456 | 456 |
void next(Node& i) const { |
457 | 457 |
Parent::next(i); |
458 | 458 |
while (i != INVALID && !(*_node_filter)[i]) Parent::next(i); |
459 | 459 |
} |
460 | 460 |
|
461 | 461 |
void next(Arc& i) const { |
462 | 462 |
Parent::next(i); |
463 | 463 |
while (i != INVALID && (!(*_arc_filter)[i] |
464 | 464 |
|| !(*_node_filter)[Parent::source(i)] |
465 | 465 |
|| !(*_node_filter)[Parent::target(i)])) |
466 | 466 |
Parent::next(i); |
467 | 467 |
} |
468 | 468 |
|
469 | 469 |
void nextIn(Arc& i) const { |
470 | 470 |
Parent::nextIn(i); |
471 | 471 |
while (i != INVALID && (!(*_arc_filter)[i] |
472 | 472 |
|| !(*_node_filter)[Parent::source(i)])) |
473 | 473 |
Parent::nextIn(i); |
474 | 474 |
} |
475 | 475 |
|
476 | 476 |
void nextOut(Arc& i) const { |
477 | 477 |
Parent::nextOut(i); |
478 | 478 |
while (i != INVALID && (!(*_arc_filter)[i] |
479 | 479 |
|| !(*_node_filter)[Parent::target(i)])) |
480 | 480 |
Parent::nextOut(i); |
481 | 481 |
} |
482 | 482 |
|
483 | 483 |
void status(const Node& n, bool v) const { _node_filter->set(n, v); } |
484 | 484 |
void status(const Arc& a, bool v) const { _arc_filter->set(a, v); } |
485 | 485 |
|
486 | 486 |
bool status(const Node& n) const { return (*_node_filter)[n]; } |
487 | 487 |
bool status(const Arc& a) const { return (*_arc_filter)[a]; } |
488 | 488 |
|
489 | 489 |
typedef False NodeNumTag; |
490 | 490 |
typedef False ArcNumTag; |
491 | 491 |
|
492 | 492 |
typedef FindArcTagIndicator<DGR> FindArcTag; |
493 | 493 |
Arc findArc(const Node& source, const Node& target, |
494 | 494 |
const Arc& prev = INVALID) const { |
495 | 495 |
if (!(*_node_filter)[source] || !(*_node_filter)[target]) { |
496 | 496 |
return INVALID; |
497 | 497 |
} |
498 | 498 |
Arc arc = Parent::findArc(source, target, prev); |
499 | 499 |
while (arc != INVALID && !(*_arc_filter)[arc]) { |
500 | 500 |
arc = Parent::findArc(source, target, arc); |
501 | 501 |
} |
502 | 502 |
return arc; |
503 | 503 |
} |
504 | 504 |
|
505 | 505 |
public: |
506 | 506 |
|
507 | 507 |
template <typename V> |
508 |
class NodeMap |
|
509 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
|
510 |
|
|
508 |
class NodeMap |
|
509 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
|
510 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> { |
|
511 | 511 |
typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
512 |
|
|
512 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> Parent; |
|
513 | 513 |
|
514 | 514 |
public: |
515 | 515 |
typedef V Value; |
516 | 516 |
|
517 | 517 |
NodeMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor) |
518 | 518 |
: Parent(adaptor) {} |
519 | 519 |
NodeMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor, const V& value) |
520 | 520 |
: Parent(adaptor, value) {} |
521 | 521 |
|
522 | 522 |
private: |
523 | 523 |
NodeMap& operator=(const NodeMap& cmap) { |
524 | 524 |
return operator=<NodeMap>(cmap); |
525 | 525 |
} |
526 | 526 |
|
527 | 527 |
template <typename CMap> |
528 | 528 |
NodeMap& operator=(const CMap& cmap) { |
529 | 529 |
Parent::operator=(cmap); |
530 | 530 |
return *this; |
531 | 531 |
} |
532 | 532 |
}; |
533 | 533 |
|
534 | 534 |
template <typename V> |
535 |
class ArcMap |
|
535 |
class ArcMap |
|
536 | 536 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
537 |
|
|
537 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> { |
|
538 | 538 |
typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
539 | 539 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent; |
540 | 540 |
|
541 | 541 |
public: |
542 | 542 |
typedef V Value; |
543 | 543 |
|
544 | 544 |
ArcMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor) |
545 | 545 |
: Parent(adaptor) {} |
546 | 546 |
ArcMap(const SubDigraphBase<DGR, NF, AF, ch>& adaptor, const V& value) |
547 | 547 |
: Parent(adaptor, value) {} |
548 | 548 |
|
549 | 549 |
private: |
550 | 550 |
ArcMap& operator=(const ArcMap& cmap) { |
551 | 551 |
return operator=<ArcMap>(cmap); |
552 | 552 |
} |
553 | 553 |
|
554 | 554 |
template <typename CMap> |
555 | 555 |
ArcMap& operator=(const CMap& cmap) { |
556 | 556 |
Parent::operator=(cmap); |
557 | 557 |
return *this; |
558 | 558 |
} |
559 | 559 |
}; |
560 | 560 |
|
561 | 561 |
}; |
562 | 562 |
|
563 | 563 |
template <typename DGR, typename NF, typename AF> |
564 | 564 |
class SubDigraphBase<DGR, NF, AF, false> |
565 | 565 |
: public DigraphAdaptorBase<DGR> { |
566 | 566 |
typedef DigraphAdaptorBase<DGR> Parent; |
567 | 567 |
public: |
568 | 568 |
typedef DGR Digraph; |
569 | 569 |
typedef NF NodeFilterMap; |
570 | 570 |
typedef AF ArcFilterMap; |
571 | 571 |
|
572 | 572 |
typedef SubDigraphBase Adaptor; |
573 | 573 |
protected: |
574 | 574 |
NF* _node_filter; |
575 | 575 |
AF* _arc_filter; |
576 | 576 |
SubDigraphBase() |
577 | 577 |
: Parent(), _node_filter(0), _arc_filter(0) { } |
578 | 578 |
|
579 | 579 |
void initialize(DGR& digraph, NF& node_filter, AF& arc_filter) { |
580 | 580 |
Parent::initialize(digraph); |
581 | 581 |
_node_filter = &node_filter; |
582 |
_arc_filter = &arc_filter; |
|
582 |
_arc_filter = &arc_filter; |
|
583 | 583 |
} |
584 | 584 |
|
585 | 585 |
public: |
586 | 586 |
|
587 | 587 |
typedef typename Parent::Node Node; |
588 | 588 |
typedef typename Parent::Arc Arc; |
589 | 589 |
|
590 | 590 |
void first(Node& i) const { |
591 | 591 |
Parent::first(i); |
592 | 592 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
593 | 593 |
} |
594 | 594 |
|
595 | 595 |
void first(Arc& i) const { |
596 | 596 |
Parent::first(i); |
597 | 597 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); |
598 | 598 |
} |
599 | 599 |
|
600 | 600 |
void firstIn(Arc& i, const Node& n) const { |
601 | 601 |
Parent::firstIn(i, n); |
602 | 602 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); |
603 | 603 |
} |
604 | 604 |
|
605 | 605 |
void firstOut(Arc& i, const Node& n) const { |
606 | 606 |
Parent::firstOut(i, n); |
607 | 607 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); |
608 | 608 |
} |
609 | 609 |
|
610 | 610 |
void next(Node& i) const { |
611 | 611 |
Parent::next(i); |
612 | 612 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
613 | 613 |
} |
614 | 614 |
void next(Arc& i) const { |
615 | 615 |
Parent::next(i); |
616 | 616 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::next(i); |
617 | 617 |
} |
618 | 618 |
void nextIn(Arc& i) const { |
619 | 619 |
Parent::nextIn(i); |
620 | 620 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextIn(i); |
621 | 621 |
} |
622 | 622 |
|
623 | 623 |
void nextOut(Arc& i) const { |
624 | 624 |
Parent::nextOut(i); |
625 | 625 |
while (i!=INVALID && !(*_arc_filter)[i]) Parent::nextOut(i); |
626 | 626 |
} |
627 | 627 |
|
628 | 628 |
void status(const Node& n, bool v) const { _node_filter->set(n, v); } |
629 | 629 |
void status(const Arc& a, bool v) const { _arc_filter->set(a, v); } |
630 | 630 |
|
631 | 631 |
bool status(const Node& n) const { return (*_node_filter)[n]; } |
632 | 632 |
bool status(const Arc& a) const { return (*_arc_filter)[a]; } |
633 | 633 |
|
634 | 634 |
typedef False NodeNumTag; |
635 | 635 |
typedef False ArcNumTag; |
636 | 636 |
|
637 | 637 |
typedef FindArcTagIndicator<DGR> FindArcTag; |
638 | 638 |
Arc findArc(const Node& source, const Node& target, |
639 | 639 |
const Arc& prev = INVALID) const { |
640 | 640 |
if (!(*_node_filter)[source] || !(*_node_filter)[target]) { |
641 | 641 |
return INVALID; |
642 | 642 |
} |
643 | 643 |
Arc arc = Parent::findArc(source, target, prev); |
644 | 644 |
while (arc != INVALID && !(*_arc_filter)[arc]) { |
645 | 645 |
arc = Parent::findArc(source, target, arc); |
646 | 646 |
} |
647 | 647 |
return arc; |
648 | 648 |
} |
649 | 649 |
|
650 | 650 |
template <typename V> |
651 |
class NodeMap |
|
651 |
class NodeMap |
|
652 | 652 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
653 | 653 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> { |
654 |
typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
|
654 |
typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
|
655 | 655 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> Parent; |
656 | 656 |
|
657 | 657 |
public: |
658 | 658 |
typedef V Value; |
659 | 659 |
|
660 | 660 |
NodeMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor) |
661 | 661 |
: Parent(adaptor) {} |
662 | 662 |
NodeMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor, const V& value) |
663 | 663 |
: Parent(adaptor, value) {} |
664 | 664 |
|
665 | 665 |
private: |
666 | 666 |
NodeMap& operator=(const NodeMap& cmap) { |
667 | 667 |
return operator=<NodeMap>(cmap); |
668 | 668 |
} |
669 | 669 |
|
670 | 670 |
template <typename CMap> |
671 | 671 |
NodeMap& operator=(const CMap& cmap) { |
672 | 672 |
Parent::operator=(cmap); |
673 | 673 |
return *this; |
674 | 674 |
} |
675 | 675 |
}; |
676 | 676 |
|
677 | 677 |
template <typename V> |
678 |
class ArcMap |
|
678 |
class ArcMap |
|
679 | 679 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
680 | 680 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> { |
681 | 681 |
typedef SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
682 | 682 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, ArcMap<V>)> Parent; |
683 | 683 |
|
684 | 684 |
public: |
685 | 685 |
typedef V Value; |
686 | 686 |
|
687 | 687 |
ArcMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor) |
688 | 688 |
: Parent(adaptor) {} |
689 | 689 |
ArcMap(const SubDigraphBase<DGR, NF, AF, false>& adaptor, const V& value) |
690 | 690 |
: Parent(adaptor, value) {} |
691 | 691 |
|
692 | 692 |
private: |
693 | 693 |
ArcMap& operator=(const ArcMap& cmap) { |
694 | 694 |
return operator=<ArcMap>(cmap); |
695 | 695 |
} |
696 | 696 |
|
697 | 697 |
template <typename CMap> |
698 | 698 |
ArcMap& operator=(const CMap& cmap) { |
699 | 699 |
Parent::operator=(cmap); |
700 | 700 |
return *this; |
701 | 701 |
} |
702 | 702 |
}; |
703 | 703 |
|
704 | 704 |
}; |
705 | 705 |
|
706 | 706 |
/// \ingroup graph_adaptors |
707 | 707 |
/// |
708 | 708 |
/// \brief Adaptor class for hiding nodes and arcs in a digraph |
709 | 709 |
/// |
710 | 710 |
/// SubDigraph can be used for hiding nodes and arcs in a digraph. |
711 | 711 |
/// A \c bool node map and a \c bool arc map must be specified, which |
712 | 712 |
/// define the filters for nodes and arcs. |
713 | 713 |
/// Only the nodes and arcs with \c true filter value are |
714 | 714 |
/// shown in the subdigraph. The arcs that are incident to hidden |
715 | 715 |
/// nodes are also filtered out. |
716 | 716 |
/// This adaptor conforms to the \ref concepts::Digraph "Digraph" concept. |
717 | 717 |
/// |
718 | 718 |
/// The adapted digraph can also be modified through this adaptor |
719 | 719 |
/// by adding or removing nodes or arcs, unless the \c GR template |
720 | 720 |
/// parameter is set to be \c const. |
721 | 721 |
/// |
722 | 722 |
/// \tparam DGR The type of the adapted digraph. |
723 | 723 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
724 | 724 |
/// It can also be specified to be \c const. |
725 | 725 |
/// \tparam NF The type of the node filter map. |
726 | 726 |
/// It must be a \c bool (or convertible) node map of the |
727 | 727 |
/// adapted digraph. The default type is |
728 | 728 |
/// \ref concepts::Digraph::NodeMap "DGR::NodeMap<bool>". |
729 | 729 |
/// \tparam AF The type of the arc filter map. |
730 | 730 |
/// It must be \c bool (or convertible) arc map of the |
731 | 731 |
/// adapted digraph. The default type is |
732 | 732 |
/// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>". |
733 | 733 |
/// |
734 | 734 |
/// \note The \c Node and \c Arc types of this adaptor and the adapted |
735 | 735 |
/// digraph are convertible to each other. |
736 | 736 |
/// |
737 | 737 |
/// \see FilterNodes |
738 | 738 |
/// \see FilterArcs |
739 | 739 |
#ifdef DOXYGEN |
740 | 740 |
template<typename DGR, typename NF, typename AF> |
741 | 741 |
class SubDigraph { |
742 | 742 |
#else |
... | ... |
@@ -955,385 +955,385 @@ |
955 | 955 |
|| !(*_node_filter)[Parent::u(i)] |
956 | 956 |
|| !(*_node_filter)[Parent::v(i)])) |
957 | 957 |
Parent::next(i); |
958 | 958 |
} |
959 | 959 |
|
960 | 960 |
void nextIn(Arc& i) const { |
961 | 961 |
Parent::nextIn(i); |
962 | 962 |
while (i!=INVALID && (!(*_edge_filter)[i] |
963 | 963 |
|| !(*_node_filter)[Parent::source(i)])) |
964 | 964 |
Parent::nextIn(i); |
965 | 965 |
} |
966 | 966 |
|
967 | 967 |
void nextOut(Arc& i) const { |
968 | 968 |
Parent::nextOut(i); |
969 | 969 |
while (i!=INVALID && (!(*_edge_filter)[i] |
970 | 970 |
|| !(*_node_filter)[Parent::target(i)])) |
971 | 971 |
Parent::nextOut(i); |
972 | 972 |
} |
973 | 973 |
|
974 | 974 |
void nextInc(Edge& i, bool& d) const { |
975 | 975 |
Parent::nextInc(i, d); |
976 | 976 |
while (i!=INVALID && (!(*_edge_filter)[i] |
977 | 977 |
|| !(*_node_filter)[Parent::u(i)] |
978 | 978 |
|| !(*_node_filter)[Parent::v(i)])) |
979 | 979 |
Parent::nextInc(i, d); |
980 | 980 |
} |
981 | 981 |
|
982 | 982 |
void status(const Node& n, bool v) const { _node_filter->set(n, v); } |
983 | 983 |
void status(const Edge& e, bool v) const { _edge_filter->set(e, v); } |
984 | 984 |
|
985 | 985 |
bool status(const Node& n) const { return (*_node_filter)[n]; } |
986 | 986 |
bool status(const Edge& e) const { return (*_edge_filter)[e]; } |
987 | 987 |
|
988 | 988 |
typedef False NodeNumTag; |
989 | 989 |
typedef False ArcNumTag; |
990 | 990 |
typedef False EdgeNumTag; |
991 | 991 |
|
992 | 992 |
typedef FindArcTagIndicator<Graph> FindArcTag; |
993 | 993 |
Arc findArc(const Node& u, const Node& v, |
994 | 994 |
const Arc& prev = INVALID) const { |
995 | 995 |
if (!(*_node_filter)[u] || !(*_node_filter)[v]) { |
996 | 996 |
return INVALID; |
997 | 997 |
} |
998 | 998 |
Arc arc = Parent::findArc(u, v, prev); |
999 | 999 |
while (arc != INVALID && !(*_edge_filter)[arc]) { |
1000 | 1000 |
arc = Parent::findArc(u, v, arc); |
1001 | 1001 |
} |
1002 | 1002 |
return arc; |
1003 | 1003 |
} |
1004 | 1004 |
|
1005 | 1005 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
1006 | 1006 |
Edge findEdge(const Node& u, const Node& v, |
1007 | 1007 |
const Edge& prev = INVALID) const { |
1008 | 1008 |
if (!(*_node_filter)[u] || !(*_node_filter)[v]) { |
1009 | 1009 |
return INVALID; |
1010 | 1010 |
} |
1011 | 1011 |
Edge edge = Parent::findEdge(u, v, prev); |
1012 | 1012 |
while (edge != INVALID && !(*_edge_filter)[edge]) { |
1013 | 1013 |
edge = Parent::findEdge(u, v, edge); |
1014 | 1014 |
} |
1015 | 1015 |
return edge; |
1016 | 1016 |
} |
1017 | 1017 |
|
1018 | 1018 |
template <typename V> |
1019 |
class NodeMap |
|
1019 |
class NodeMap |
|
1020 | 1020 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
1021 | 1021 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> { |
1022 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
|
1022 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
|
1023 | 1023 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent; |
1024 | 1024 |
|
1025 | 1025 |
public: |
1026 | 1026 |
typedef V Value; |
1027 | 1027 |
|
1028 | 1028 |
NodeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor) |
1029 | 1029 |
: Parent(adaptor) {} |
1030 | 1030 |
NodeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value) |
1031 | 1031 |
: Parent(adaptor, value) {} |
1032 | 1032 |
|
1033 | 1033 |
private: |
1034 | 1034 |
NodeMap& operator=(const NodeMap& cmap) { |
1035 | 1035 |
return operator=<NodeMap>(cmap); |
1036 | 1036 |
} |
1037 | 1037 |
|
1038 | 1038 |
template <typename CMap> |
1039 | 1039 |
NodeMap& operator=(const CMap& cmap) { |
1040 | 1040 |
Parent::operator=(cmap); |
1041 | 1041 |
return *this; |
1042 | 1042 |
} |
1043 | 1043 |
}; |
1044 | 1044 |
|
1045 | 1045 |
template <typename V> |
1046 |
class ArcMap |
|
1046 |
class ArcMap |
|
1047 | 1047 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
1048 | 1048 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> { |
1049 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
|
1049 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
|
1050 | 1050 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent; |
1051 | 1051 |
|
1052 | 1052 |
public: |
1053 | 1053 |
typedef V Value; |
1054 | 1054 |
|
1055 | 1055 |
ArcMap(const SubGraphBase<GR, NF, EF, ch>& adaptor) |
1056 | 1056 |
: Parent(adaptor) {} |
1057 | 1057 |
ArcMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value) |
1058 | 1058 |
: Parent(adaptor, value) {} |
1059 | 1059 |
|
1060 | 1060 |
private: |
1061 | 1061 |
ArcMap& operator=(const ArcMap& cmap) { |
1062 | 1062 |
return operator=<ArcMap>(cmap); |
1063 | 1063 |
} |
1064 | 1064 |
|
1065 | 1065 |
template <typename CMap> |
1066 | 1066 |
ArcMap& operator=(const CMap& cmap) { |
1067 | 1067 |
Parent::operator=(cmap); |
1068 | 1068 |
return *this; |
1069 | 1069 |
} |
1070 | 1070 |
}; |
1071 | 1071 |
|
1072 | 1072 |
template <typename V> |
1073 |
class EdgeMap |
|
1073 |
class EdgeMap |
|
1074 | 1074 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
1075 | 1075 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> { |
1076 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
|
1076 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, ch>, |
|
1077 | 1077 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent; |
1078 | 1078 |
|
1079 | 1079 |
public: |
1080 | 1080 |
typedef V Value; |
1081 | 1081 |
|
1082 | 1082 |
EdgeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor) |
1083 | 1083 |
: Parent(adaptor) {} |
1084 | 1084 |
|
1085 | 1085 |
EdgeMap(const SubGraphBase<GR, NF, EF, ch>& adaptor, const V& value) |
1086 | 1086 |
: Parent(adaptor, value) {} |
1087 | 1087 |
|
1088 | 1088 |
private: |
1089 | 1089 |
EdgeMap& operator=(const EdgeMap& cmap) { |
1090 | 1090 |
return operator=<EdgeMap>(cmap); |
1091 | 1091 |
} |
1092 | 1092 |
|
1093 | 1093 |
template <typename CMap> |
1094 | 1094 |
EdgeMap& operator=(const CMap& cmap) { |
1095 | 1095 |
Parent::operator=(cmap); |
1096 | 1096 |
return *this; |
1097 | 1097 |
} |
1098 | 1098 |
}; |
1099 | 1099 |
|
1100 | 1100 |
}; |
1101 | 1101 |
|
1102 | 1102 |
template <typename GR, typename NF, typename EF> |
1103 | 1103 |
class SubGraphBase<GR, NF, EF, false> |
1104 | 1104 |
: public GraphAdaptorBase<GR> { |
1105 | 1105 |
typedef GraphAdaptorBase<GR> Parent; |
1106 | 1106 |
public: |
1107 | 1107 |
typedef GR Graph; |
1108 | 1108 |
typedef NF NodeFilterMap; |
1109 | 1109 |
typedef EF EdgeFilterMap; |
1110 | 1110 |
|
1111 | 1111 |
typedef SubGraphBase Adaptor; |
1112 | 1112 |
protected: |
1113 | 1113 |
NF* _node_filter; |
1114 | 1114 |
EF* _edge_filter; |
1115 |
SubGraphBase() |
|
1116 |
: Parent(), _node_filter(0), _edge_filter(0) { } |
|
1115 |
SubGraphBase() |
|
1116 |
: Parent(), _node_filter(0), _edge_filter(0) { } |
|
1117 | 1117 |
|
1118 | 1118 |
void initialize(GR& graph, NF& node_filter, EF& edge_filter) { |
1119 | 1119 |
Parent::initialize(graph); |
1120 | 1120 |
_node_filter = &node_filter; |
1121 | 1121 |
_edge_filter = &edge_filter; |
1122 | 1122 |
} |
1123 | 1123 |
|
1124 | 1124 |
public: |
1125 | 1125 |
|
1126 | 1126 |
typedef typename Parent::Node Node; |
1127 | 1127 |
typedef typename Parent::Arc Arc; |
1128 | 1128 |
typedef typename Parent::Edge Edge; |
1129 | 1129 |
|
1130 | 1130 |
void first(Node& i) const { |
1131 | 1131 |
Parent::first(i); |
1132 | 1132 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
1133 | 1133 |
} |
1134 | 1134 |
|
1135 | 1135 |
void first(Arc& i) const { |
1136 | 1136 |
Parent::first(i); |
1137 | 1137 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
1138 | 1138 |
} |
1139 | 1139 |
|
1140 | 1140 |
void first(Edge& i) const { |
1141 | 1141 |
Parent::first(i); |
1142 | 1142 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
1143 | 1143 |
} |
1144 | 1144 |
|
1145 | 1145 |
void firstIn(Arc& i, const Node& n) const { |
1146 | 1146 |
Parent::firstIn(i, n); |
1147 | 1147 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextIn(i); |
1148 | 1148 |
} |
1149 | 1149 |
|
1150 | 1150 |
void firstOut(Arc& i, const Node& n) const { |
1151 | 1151 |
Parent::firstOut(i, n); |
1152 | 1152 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextOut(i); |
1153 | 1153 |
} |
1154 | 1154 |
|
1155 | 1155 |
void firstInc(Edge& i, bool& d, const Node& n) const { |
1156 | 1156 |
Parent::firstInc(i, d, n); |
1157 | 1157 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextInc(i, d); |
1158 | 1158 |
} |
1159 | 1159 |
|
1160 | 1160 |
void next(Node& i) const { |
1161 | 1161 |
Parent::next(i); |
1162 | 1162 |
while (i!=INVALID && !(*_node_filter)[i]) Parent::next(i); |
1163 | 1163 |
} |
1164 | 1164 |
void next(Arc& i) const { |
1165 | 1165 |
Parent::next(i); |
1166 | 1166 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
1167 | 1167 |
} |
1168 | 1168 |
void next(Edge& i) const { |
1169 | 1169 |
Parent::next(i); |
1170 | 1170 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::next(i); |
1171 | 1171 |
} |
1172 | 1172 |
void nextIn(Arc& i) const { |
1173 | 1173 |
Parent::nextIn(i); |
1174 | 1174 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextIn(i); |
1175 | 1175 |
} |
1176 | 1176 |
|
1177 | 1177 |
void nextOut(Arc& i) const { |
1178 | 1178 |
Parent::nextOut(i); |
1179 | 1179 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextOut(i); |
1180 | 1180 |
} |
1181 | 1181 |
void nextInc(Edge& i, bool& d) const { |
1182 | 1182 |
Parent::nextInc(i, d); |
1183 | 1183 |
while (i!=INVALID && !(*_edge_filter)[i]) Parent::nextInc(i, d); |
1184 | 1184 |
} |
1185 | 1185 |
|
1186 | 1186 |
void status(const Node& n, bool v) const { _node_filter->set(n, v); } |
1187 | 1187 |
void status(const Edge& e, bool v) const { _edge_filter->set(e, v); } |
1188 | 1188 |
|
1189 | 1189 |
bool status(const Node& n) const { return (*_node_filter)[n]; } |
1190 | 1190 |
bool status(const Edge& e) const { return (*_edge_filter)[e]; } |
1191 | 1191 |
|
1192 | 1192 |
typedef False NodeNumTag; |
1193 | 1193 |
typedef False ArcNumTag; |
1194 | 1194 |
typedef False EdgeNumTag; |
1195 | 1195 |
|
1196 | 1196 |
typedef FindArcTagIndicator<Graph> FindArcTag; |
1197 | 1197 |
Arc findArc(const Node& u, const Node& v, |
1198 | 1198 |
const Arc& prev = INVALID) const { |
1199 | 1199 |
Arc arc = Parent::findArc(u, v, prev); |
1200 | 1200 |
while (arc != INVALID && !(*_edge_filter)[arc]) { |
1201 | 1201 |
arc = Parent::findArc(u, v, arc); |
1202 | 1202 |
} |
1203 | 1203 |
return arc; |
1204 | 1204 |
} |
1205 | 1205 |
|
1206 | 1206 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
1207 | 1207 |
Edge findEdge(const Node& u, const Node& v, |
1208 | 1208 |
const Edge& prev = INVALID) const { |
1209 | 1209 |
Edge edge = Parent::findEdge(u, v, prev); |
1210 | 1210 |
while (edge != INVALID && !(*_edge_filter)[edge]) { |
1211 | 1211 |
edge = Parent::findEdge(u, v, edge); |
1212 | 1212 |
} |
1213 | 1213 |
return edge; |
1214 | 1214 |
} |
1215 | 1215 |
|
1216 | 1216 |
template <typename V> |
1217 |
class NodeMap |
|
1217 |
class NodeMap |
|
1218 | 1218 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
1219 | 1219 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> { |
1220 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
|
1220 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
|
1221 | 1221 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, NodeMap<V>)> Parent; |
1222 | 1222 |
|
1223 | 1223 |
public: |
1224 | 1224 |
typedef V Value; |
1225 | 1225 |
|
1226 | 1226 |
NodeMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
1227 | 1227 |
: Parent(adaptor) {} |
1228 | 1228 |
NodeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
1229 | 1229 |
: Parent(adaptor, value) {} |
1230 | 1230 |
|
1231 | 1231 |
private: |
1232 | 1232 |
NodeMap& operator=(const NodeMap& cmap) { |
1233 | 1233 |
return operator=<NodeMap>(cmap); |
1234 | 1234 |
} |
1235 | 1235 |
|
1236 | 1236 |
template <typename CMap> |
1237 | 1237 |
NodeMap& operator=(const CMap& cmap) { |
1238 | 1238 |
Parent::operator=(cmap); |
1239 | 1239 |
return *this; |
1240 | 1240 |
} |
1241 | 1241 |
}; |
1242 | 1242 |
|
1243 | 1243 |
template <typename V> |
1244 |
class ArcMap |
|
1244 |
class ArcMap |
|
1245 | 1245 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
1246 | 1246 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> { |
1247 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
|
1247 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
|
1248 | 1248 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, ArcMap<V>)> Parent; |
1249 | 1249 |
|
1250 | 1250 |
public: |
1251 | 1251 |
typedef V Value; |
1252 | 1252 |
|
1253 | 1253 |
ArcMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
1254 | 1254 |
: Parent(adaptor) {} |
1255 | 1255 |
ArcMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
1256 | 1256 |
: Parent(adaptor, value) {} |
1257 | 1257 |
|
1258 | 1258 |
private: |
1259 | 1259 |
ArcMap& operator=(const ArcMap& cmap) { |
1260 | 1260 |
return operator=<ArcMap>(cmap); |
1261 | 1261 |
} |
1262 | 1262 |
|
1263 | 1263 |
template <typename CMap> |
1264 | 1264 |
ArcMap& operator=(const CMap& cmap) { |
1265 | 1265 |
Parent::operator=(cmap); |
1266 | 1266 |
return *this; |
1267 | 1267 |
} |
1268 | 1268 |
}; |
1269 | 1269 |
|
1270 | 1270 |
template <typename V> |
1271 |
class EdgeMap |
|
1271 |
class EdgeMap |
|
1272 | 1272 |
: public SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
1273 | 1273 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> { |
1274 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
|
1275 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent; |
|
1274 |
typedef SubMapExtender<SubGraphBase<GR, NF, EF, false>, |
|
1275 |
LEMON_SCOPE_FIX(GraphAdaptorBase<GR>, EdgeMap<V>)> Parent; |
|
1276 | 1276 |
|
1277 | 1277 |
public: |
1278 | 1278 |
typedef V Value; |
1279 | 1279 |
|
1280 | 1280 |
EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor) |
1281 | 1281 |
: Parent(adaptor) {} |
1282 | 1282 |
|
1283 | 1283 |
EdgeMap(const SubGraphBase<GR, NF, EF, false>& adaptor, const V& value) |
1284 | 1284 |
: Parent(adaptor, value) {} |
1285 | 1285 |
|
1286 | 1286 |
private: |
1287 | 1287 |
EdgeMap& operator=(const EdgeMap& cmap) { |
1288 | 1288 |
return operator=<EdgeMap>(cmap); |
1289 | 1289 |
} |
1290 | 1290 |
|
1291 | 1291 |
template <typename CMap> |
1292 | 1292 |
EdgeMap& operator=(const CMap& cmap) { |
1293 | 1293 |
Parent::operator=(cmap); |
1294 | 1294 |
return *this; |
1295 | 1295 |
} |
1296 | 1296 |
}; |
1297 | 1297 |
|
1298 | 1298 |
}; |
1299 | 1299 |
|
1300 | 1300 |
/// \ingroup graph_adaptors |
1301 | 1301 |
/// |
1302 | 1302 |
/// \brief Adaptor class for hiding nodes and edges in an undirected |
1303 | 1303 |
/// graph. |
1304 | 1304 |
/// |
1305 | 1305 |
/// SubGraph can be used for hiding nodes and edges in a graph. |
1306 | 1306 |
/// A \c bool node map and a \c bool edge map must be specified, which |
1307 | 1307 |
/// define the filters for nodes and edges. |
1308 | 1308 |
/// Only the nodes and edges with \c true filter value are |
1309 | 1309 |
/// shown in the subgraph. The edges that are incident to hidden |
1310 | 1310 |
/// nodes are also filtered out. |
1311 | 1311 |
/// This adaptor conforms to the \ref concepts::Graph "Graph" concept. |
1312 | 1312 |
/// |
1313 | 1313 |
/// The adapted graph can also be modified through this adaptor |
1314 | 1314 |
/// by adding or removing nodes or edges, unless the \c GR template |
1315 | 1315 |
/// parameter is set to be \c const. |
1316 | 1316 |
/// |
1317 | 1317 |
/// \tparam GR The type of the adapted graph. |
1318 | 1318 |
/// It must conform to the \ref concepts::Graph "Graph" concept. |
1319 | 1319 |
/// It can also be specified to be \c const. |
1320 | 1320 |
/// \tparam NF The type of the node filter map. |
1321 | 1321 |
/// It must be a \c bool (or convertible) node map of the |
1322 | 1322 |
/// adapted graph. The default type is |
1323 | 1323 |
/// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>". |
1324 | 1324 |
/// \tparam EF The type of the edge filter map. |
1325 | 1325 |
/// It must be a \c bool (or convertible) edge map of the |
1326 | 1326 |
/// adapted graph. The default type is |
1327 | 1327 |
/// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
1328 | 1328 |
/// |
1329 | 1329 |
/// \note The \c Node, \c Edge and \c Arc types of this adaptor and the |
1330 | 1330 |
/// adapted graph are convertible to each other. |
1331 | 1331 |
/// |
1332 | 1332 |
/// \see FilterNodes |
1333 | 1333 |
/// \see FilterEdges |
1334 | 1334 |
#ifdef DOXYGEN |
1335 | 1335 |
template<typename GR, typename NF, typename EF> |
1336 | 1336 |
class SubGraph { |
1337 | 1337 |
#else |
1338 | 1338 |
template<typename GR, |
1339 | 1339 |
typename NF = typename GR::template NodeMap<bool>, |
... | ... |
@@ -1434,479 +1434,479 @@ |
1434 | 1434 |
} |
1435 | 1435 |
|
1436 | 1436 |
template<typename GR, typename NF, typename EF> |
1437 | 1437 |
SubGraph<const GR, const NF, EF> |
1438 | 1438 |
subGraph(const GR& graph, const NF& node_filter, EF& edge_filter) { |
1439 | 1439 |
return SubGraph<const GR, const NF, EF> |
1440 | 1440 |
(graph, node_filter, edge_filter); |
1441 | 1441 |
} |
1442 | 1442 |
|
1443 | 1443 |
template<typename GR, typename NF, typename EF> |
1444 | 1444 |
SubGraph<const GR, NF, const EF> |
1445 | 1445 |
subGraph(const GR& graph, NF& node_filter, const EF& edge_filter) { |
1446 | 1446 |
return SubGraph<const GR, NF, const EF> |
1447 | 1447 |
(graph, node_filter, edge_filter); |
1448 | 1448 |
} |
1449 | 1449 |
|
1450 | 1450 |
template<typename GR, typename NF, typename EF> |
1451 | 1451 |
SubGraph<const GR, const NF, const EF> |
1452 | 1452 |
subGraph(const GR& graph, const NF& node_filter, const EF& edge_filter) { |
1453 | 1453 |
return SubGraph<const GR, const NF, const EF> |
1454 | 1454 |
(graph, node_filter, edge_filter); |
1455 | 1455 |
} |
1456 | 1456 |
|
1457 | 1457 |
|
1458 | 1458 |
/// \ingroup graph_adaptors |
1459 | 1459 |
/// |
1460 | 1460 |
/// \brief Adaptor class for hiding nodes in a digraph or a graph. |
1461 | 1461 |
/// |
1462 | 1462 |
/// FilterNodes adaptor can be used for hiding nodes in a digraph or a |
1463 | 1463 |
/// graph. A \c bool node map must be specified, which defines the filter |
1464 | 1464 |
/// for the nodes. Only the nodes with \c true filter value and the |
1465 | 1465 |
/// arcs/edges incident to nodes both with \c true filter value are shown |
1466 | 1466 |
/// in the subgraph. This adaptor conforms to the \ref concepts::Digraph |
1467 | 1467 |
/// "Digraph" concept or the \ref concepts::Graph "Graph" concept |
1468 | 1468 |
/// depending on the \c GR template parameter. |
1469 | 1469 |
/// |
1470 | 1470 |
/// The adapted (di)graph can also be modified through this adaptor |
1471 | 1471 |
/// by adding or removing nodes or arcs/edges, unless the \c GR template |
1472 | 1472 |
/// parameter is set to be \c const. |
1473 | 1473 |
/// |
1474 | 1474 |
/// \tparam GR The type of the adapted digraph or graph. |
1475 | 1475 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept |
1476 | 1476 |
/// or the \ref concepts::Graph "Graph" concept. |
1477 | 1477 |
/// It can also be specified to be \c const. |
1478 | 1478 |
/// \tparam NF The type of the node filter map. |
1479 | 1479 |
/// It must be a \c bool (or convertible) node map of the |
1480 | 1480 |
/// adapted (di)graph. The default type is |
1481 | 1481 |
/// \ref concepts::Graph::NodeMap "GR::NodeMap<bool>". |
1482 | 1482 |
/// |
1483 | 1483 |
/// \note The \c Node and <tt>Arc/Edge</tt> types of this adaptor and the |
1484 | 1484 |
/// adapted (di)graph are convertible to each other. |
1485 | 1485 |
#ifdef DOXYGEN |
1486 | 1486 |
template<typename GR, typename NF> |
1487 | 1487 |
class FilterNodes { |
1488 | 1488 |
#else |
1489 | 1489 |
template<typename GR, |
1490 | 1490 |
typename NF = typename GR::template NodeMap<bool>, |
1491 | 1491 |
typename Enable = void> |
1492 | 1492 |
class FilterNodes : |
1493 | 1493 |
public DigraphAdaptorExtender< |
1494 | 1494 |
SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >, |
1495 | 1495 |
true> > { |
1496 | 1496 |
#endif |
1497 | 1497 |
typedef DigraphAdaptorExtender< |
1498 |
SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >, |
|
1498 |
SubDigraphBase<GR, NF, ConstMap<typename GR::Arc, Const<bool, true> >, |
|
1499 | 1499 |
true> > Parent; |
1500 | 1500 |
|
1501 | 1501 |
public: |
1502 | 1502 |
|
1503 | 1503 |
typedef GR Digraph; |
1504 | 1504 |
typedef NF NodeFilterMap; |
1505 | 1505 |
|
1506 | 1506 |
typedef typename Parent::Node Node; |
1507 | 1507 |
|
1508 | 1508 |
protected: |
1509 | 1509 |
ConstMap<typename Digraph::Arc, Const<bool, true> > const_true_map; |
1510 | 1510 |
|
1511 | 1511 |
FilterNodes() : const_true_map() {} |
1512 | 1512 |
|
1513 | 1513 |
public: |
1514 | 1514 |
|
1515 | 1515 |
/// \brief Constructor |
1516 | 1516 |
/// |
1517 | 1517 |
/// Creates a subgraph for the given digraph or graph with the |
1518 | 1518 |
/// given node filter map. |
1519 |
FilterNodes(GR& graph, NF& node_filter) |
|
1519 |
FilterNodes(GR& graph, NF& node_filter) |
|
1520 | 1520 |
: Parent(), const_true_map() |
1521 | 1521 |
{ |
1522 | 1522 |
Parent::initialize(graph, node_filter, const_true_map); |
1523 | 1523 |
} |
1524 | 1524 |
|
1525 | 1525 |
/// \brief Sets the status of the given node |
1526 | 1526 |
/// |
1527 | 1527 |
/// This function sets the status of the given node. |
1528 | 1528 |
/// It is done by simply setting the assigned value of \c n |
1529 | 1529 |
/// to \c v in the node filter map. |
1530 | 1530 |
void status(const Node& n, bool v) const { Parent::status(n, v); } |
1531 | 1531 |
|
1532 | 1532 |
/// \brief Returns the status of the given node |
1533 | 1533 |
/// |
1534 | 1534 |
/// This function returns the status of the given node. |
1535 | 1535 |
/// It is \c true if the given node is enabled (i.e. not hidden). |
1536 | 1536 |
bool status(const Node& n) const { return Parent::status(n); } |
1537 | 1537 |
|
1538 | 1538 |
/// \brief Disables the given node |
1539 | 1539 |
/// |
1540 | 1540 |
/// This function disables the given node, so the iteration |
1541 | 1541 |
/// jumps over it. |
1542 | 1542 |
/// It is the same as \ref status() "status(n, false)". |
1543 | 1543 |
void disable(const Node& n) const { Parent::status(n, false); } |
1544 | 1544 |
|
1545 | 1545 |
/// \brief Enables the given node |
1546 | 1546 |
/// |
1547 | 1547 |
/// This function enables the given node. |
1548 | 1548 |
/// It is the same as \ref status() "status(n, true)". |
1549 | 1549 |
void enable(const Node& n) const { Parent::status(n, true); } |
1550 | 1550 |
|
1551 | 1551 |
}; |
1552 | 1552 |
|
1553 | 1553 |
template<typename GR, typename NF> |
1554 | 1554 |
class FilterNodes<GR, NF, |
1555 | 1555 |
typename enable_if<UndirectedTagIndicator<GR> >::type> : |
1556 | 1556 |
public GraphAdaptorExtender< |
1557 |
SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, |
|
1557 |
SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, |
|
1558 | 1558 |
true> > { |
1559 | 1559 |
|
1560 | 1560 |
typedef GraphAdaptorExtender< |
1561 |
SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, |
|
1561 |
SubGraphBase<GR, NF, ConstMap<typename GR::Edge, Const<bool, true> >, |
|
1562 | 1562 |
true> > Parent; |
1563 | 1563 |
|
1564 | 1564 |
public: |
1565 | 1565 |
|
1566 | 1566 |
typedef GR Graph; |
1567 | 1567 |
typedef NF NodeFilterMap; |
1568 | 1568 |
|
1569 | 1569 |
typedef typename Parent::Node Node; |
1570 | 1570 |
|
1571 | 1571 |
protected: |
1572 | 1572 |
ConstMap<typename GR::Edge, Const<bool, true> > const_true_map; |
1573 | 1573 |
|
1574 | 1574 |
FilterNodes() : const_true_map() {} |
1575 | 1575 |
|
1576 | 1576 |
public: |
1577 | 1577 |
|
1578 | 1578 |
FilterNodes(GR& graph, NodeFilterMap& node_filter) : |
1579 | 1579 |
Parent(), const_true_map() { |
1580 | 1580 |
Parent::initialize(graph, node_filter, const_true_map); |
1581 | 1581 |
} |
1582 | 1582 |
|
1583 | 1583 |
void status(const Node& n, bool v) const { Parent::status(n, v); } |
1584 | 1584 |
bool status(const Node& n) const { return Parent::status(n); } |
1585 | 1585 |
void disable(const Node& n) const { Parent::status(n, false); } |
1586 | 1586 |
void enable(const Node& n) const { Parent::status(n, true); } |
1587 | 1587 |
|
1588 | 1588 |
}; |
1589 | 1589 |
|
1590 | 1590 |
|
1591 | 1591 |
/// \brief Returns a read-only FilterNodes adaptor |
1592 | 1592 |
/// |
1593 | 1593 |
/// This function just returns a read-only \ref FilterNodes adaptor. |
1594 | 1594 |
/// \ingroup graph_adaptors |
1595 | 1595 |
/// \relates FilterNodes |
1596 | 1596 |
template<typename GR, typename NF> |
1597 | 1597 |
FilterNodes<const GR, NF> |
1598 | 1598 |
filterNodes(const GR& graph, NF& node_filter) { |
1599 | 1599 |
return FilterNodes<const GR, NF>(graph, node_filter); |
1600 | 1600 |
} |
1601 | 1601 |
|
1602 | 1602 |
template<typename GR, typename NF> |
1603 | 1603 |
FilterNodes<const GR, const NF> |
1604 | 1604 |
filterNodes(const GR& graph, const NF& node_filter) { |
1605 | 1605 |
return FilterNodes<const GR, const NF>(graph, node_filter); |
1606 | 1606 |
} |
1607 | 1607 |
|
1608 | 1608 |
/// \ingroup graph_adaptors |
1609 | 1609 |
/// |
1610 | 1610 |
/// \brief Adaptor class for hiding arcs in a digraph. |
1611 | 1611 |
/// |
1612 | 1612 |
/// FilterArcs adaptor can be used for hiding arcs in a digraph. |
1613 | 1613 |
/// A \c bool arc map must be specified, which defines the filter for |
1614 | 1614 |
/// the arcs. Only the arcs with \c true filter value are shown in the |
1615 | 1615 |
/// subdigraph. This adaptor conforms to the \ref concepts::Digraph |
1616 | 1616 |
/// "Digraph" concept. |
1617 | 1617 |
/// |
1618 | 1618 |
/// The adapted digraph can also be modified through this adaptor |
1619 | 1619 |
/// by adding or removing nodes or arcs, unless the \c GR template |
1620 | 1620 |
/// parameter is set to be \c const. |
1621 | 1621 |
/// |
1622 | 1622 |
/// \tparam DGR The type of the adapted digraph. |
1623 | 1623 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
1624 | 1624 |
/// It can also be specified to be \c const. |
1625 | 1625 |
/// \tparam AF The type of the arc filter map. |
1626 | 1626 |
/// It must be a \c bool (or convertible) arc map of the |
1627 | 1627 |
/// adapted digraph. The default type is |
1628 | 1628 |
/// \ref concepts::Digraph::ArcMap "DGR::ArcMap<bool>". |
1629 | 1629 |
/// |
1630 | 1630 |
/// \note The \c Node and \c Arc types of this adaptor and the adapted |
1631 | 1631 |
/// digraph are convertible to each other. |
1632 | 1632 |
#ifdef DOXYGEN |
1633 | 1633 |
template<typename DGR, |
1634 | 1634 |
typename AF> |
1635 | 1635 |
class FilterArcs { |
1636 | 1636 |
#else |
1637 | 1637 |
template<typename DGR, |
1638 | 1638 |
typename AF = typename DGR::template ArcMap<bool> > |
1639 | 1639 |
class FilterArcs : |
1640 | 1640 |
public DigraphAdaptorExtender< |
1641 | 1641 |
SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >, |
1642 | 1642 |
AF, false> > { |
1643 | 1643 |
#endif |
1644 | 1644 |
typedef DigraphAdaptorExtender< |
1645 |
SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >, |
|
1645 |
SubDigraphBase<DGR, ConstMap<typename DGR::Node, Const<bool, true> >, |
|
1646 | 1646 |
AF, false> > Parent; |
1647 | 1647 |
|
1648 | 1648 |
public: |
1649 | 1649 |
|
1650 | 1650 |
/// The type of the adapted digraph. |
1651 | 1651 |
typedef DGR Digraph; |
1652 | 1652 |
/// The type of the arc filter map. |
1653 | 1653 |
typedef AF ArcFilterMap; |
1654 | 1654 |
|
1655 | 1655 |
typedef typename Parent::Arc Arc; |
1656 | 1656 |
|
1657 | 1657 |
protected: |
1658 | 1658 |
ConstMap<typename DGR::Node, Const<bool, true> > const_true_map; |
1659 | 1659 |
|
1660 | 1660 |
FilterArcs() : const_true_map() {} |
1661 | 1661 |
|
1662 | 1662 |
public: |
1663 | 1663 |
|
1664 | 1664 |
/// \brief Constructor |
1665 | 1665 |
/// |
1666 | 1666 |
/// Creates a subdigraph for the given digraph with the given arc |
1667 | 1667 |
/// filter map. |
1668 | 1668 |
FilterArcs(DGR& digraph, ArcFilterMap& arc_filter) |
1669 | 1669 |
: Parent(), const_true_map() { |
1670 | 1670 |
Parent::initialize(digraph, const_true_map, arc_filter); |
1671 | 1671 |
} |
1672 | 1672 |
|
1673 | 1673 |
/// \brief Sets the status of the given arc |
1674 | 1674 |
/// |
1675 | 1675 |
/// This function sets the status of the given arc. |
1676 | 1676 |
/// It is done by simply setting the assigned value of \c a |
1677 | 1677 |
/// to \c v in the arc filter map. |
1678 | 1678 |
void status(const Arc& a, bool v) const { Parent::status(a, v); } |
1679 | 1679 |
|
1680 | 1680 |
/// \brief Returns the status of the given arc |
1681 | 1681 |
/// |
1682 | 1682 |
/// This function returns the status of the given arc. |
1683 | 1683 |
/// It is \c true if the given arc is enabled (i.e. not hidden). |
1684 | 1684 |
bool status(const Arc& a) const { return Parent::status(a); } |
1685 | 1685 |
|
1686 | 1686 |
/// \brief Disables the given arc |
1687 | 1687 |
/// |
1688 | 1688 |
/// This function disables the given arc in the subdigraph, |
1689 | 1689 |
/// so the iteration jumps over it. |
1690 | 1690 |
/// It is the same as \ref status() "status(a, false)". |
1691 | 1691 |
void disable(const Arc& a) const { Parent::status(a, false); } |
1692 | 1692 |
|
1693 | 1693 |
/// \brief Enables the given arc |
1694 | 1694 |
/// |
1695 | 1695 |
/// This function enables the given arc in the subdigraph. |
1696 | 1696 |
/// It is the same as \ref status() "status(a, true)". |
1697 | 1697 |
void enable(const Arc& a) const { Parent::status(a, true); } |
1698 | 1698 |
|
1699 | 1699 |
}; |
1700 | 1700 |
|
1701 | 1701 |
/// \brief Returns a read-only FilterArcs adaptor |
1702 | 1702 |
/// |
1703 | 1703 |
/// This function just returns a read-only \ref FilterArcs adaptor. |
1704 | 1704 |
/// \ingroup graph_adaptors |
1705 | 1705 |
/// \relates FilterArcs |
1706 | 1706 |
template<typename DGR, typename AF> |
1707 | 1707 |
FilterArcs<const DGR, AF> |
1708 | 1708 |
filterArcs(const DGR& digraph, AF& arc_filter) { |
1709 | 1709 |
return FilterArcs<const DGR, AF>(digraph, arc_filter); |
1710 | 1710 |
} |
1711 | 1711 |
|
1712 | 1712 |
template<typename DGR, typename AF> |
1713 | 1713 |
FilterArcs<const DGR, const AF> |
1714 | 1714 |
filterArcs(const DGR& digraph, const AF& arc_filter) { |
1715 | 1715 |
return FilterArcs<const DGR, const AF>(digraph, arc_filter); |
1716 | 1716 |
} |
1717 | 1717 |
|
1718 | 1718 |
/// \ingroup graph_adaptors |
1719 | 1719 |
/// |
1720 | 1720 |
/// \brief Adaptor class for hiding edges in a graph. |
1721 | 1721 |
/// |
1722 | 1722 |
/// FilterEdges adaptor can be used for hiding edges in a graph. |
1723 | 1723 |
/// A \c bool edge map must be specified, which defines the filter for |
1724 | 1724 |
/// the edges. Only the edges with \c true filter value are shown in the |
1725 | 1725 |
/// subgraph. This adaptor conforms to the \ref concepts::Graph |
1726 | 1726 |
/// "Graph" concept. |
1727 | 1727 |
/// |
1728 | 1728 |
/// The adapted graph can also be modified through this adaptor |
1729 | 1729 |
/// by adding or removing nodes or edges, unless the \c GR template |
1730 | 1730 |
/// parameter is set to be \c const. |
1731 | 1731 |
/// |
1732 | 1732 |
/// \tparam GR The type of the adapted graph. |
1733 | 1733 |
/// It must conform to the \ref concepts::Graph "Graph" concept. |
1734 | 1734 |
/// It can also be specified to be \c const. |
1735 | 1735 |
/// \tparam EF The type of the edge filter map. |
1736 | 1736 |
/// It must be a \c bool (or convertible) edge map of the |
1737 | 1737 |
/// adapted graph. The default type is |
1738 | 1738 |
/// \ref concepts::Graph::EdgeMap "GR::EdgeMap<bool>". |
1739 | 1739 |
/// |
1740 | 1740 |
/// \note The \c Node, \c Edge and \c Arc types of this adaptor and the |
1741 | 1741 |
/// adapted graph are convertible to each other. |
1742 | 1742 |
#ifdef DOXYGEN |
1743 | 1743 |
template<typename GR, |
1744 | 1744 |
typename EF> |
1745 | 1745 |
class FilterEdges { |
1746 | 1746 |
#else |
1747 | 1747 |
template<typename GR, |
1748 | 1748 |
typename EF = typename GR::template EdgeMap<bool> > |
1749 | 1749 |
class FilterEdges : |
1750 | 1750 |
public GraphAdaptorExtender< |
1751 |
SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true> >, |
|
1751 |
SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true> >, |
|
1752 | 1752 |
EF, false> > { |
1753 | 1753 |
#endif |
1754 | 1754 |
typedef GraphAdaptorExtender< |
1755 |
SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true > >, |
|
1755 |
SubGraphBase<GR, ConstMap<typename GR::Node, Const<bool, true > >, |
|
1756 | 1756 |
EF, false> > Parent; |
1757 | 1757 |
|
1758 | 1758 |
public: |
1759 | 1759 |
|
1760 | 1760 |
/// The type of the adapted graph. |
1761 | 1761 |
typedef GR Graph; |
1762 | 1762 |
/// The type of the edge filter map. |
1763 | 1763 |
typedef EF EdgeFilterMap; |
1764 | 1764 |
|
1765 | 1765 |
typedef typename Parent::Edge Edge; |
1766 | 1766 |
|
1767 | 1767 |
protected: |
1768 | 1768 |
ConstMap<typename GR::Node, Const<bool, true> > const_true_map; |
1769 | 1769 |
|
1770 | 1770 |
FilterEdges() : const_true_map(true) { |
1771 | 1771 |
Parent::setNodeFilterMap(const_true_map); |
1772 | 1772 |
} |
1773 | 1773 |
|
1774 | 1774 |
public: |
1775 | 1775 |
|
1776 | 1776 |
/// \brief Constructor |
1777 | 1777 |
/// |
1778 | 1778 |
/// Creates a subgraph for the given graph with the given edge |
1779 | 1779 |
/// filter map. |
1780 |
FilterEdges(GR& graph, EF& edge_filter) |
|
1780 |
FilterEdges(GR& graph, EF& edge_filter) |
|
1781 | 1781 |
: Parent(), const_true_map() { |
1782 | 1782 |
Parent::initialize(graph, const_true_map, edge_filter); |
1783 | 1783 |
} |
1784 | 1784 |
|
1785 | 1785 |
/// \brief Sets the status of the given edge |
1786 | 1786 |
/// |
1787 | 1787 |
/// This function sets the status of the given edge. |
1788 | 1788 |
/// It is done by simply setting the assigned value of \c e |
1789 | 1789 |
/// to \c v in the edge filter map. |
1790 | 1790 |
void status(const Edge& e, bool v) const { Parent::status(e, v); } |
1791 | 1791 |
|
1792 | 1792 |
/// \brief Returns the status of the given edge |
1793 | 1793 |
/// |
1794 | 1794 |
/// This function returns the status of the given edge. |
1795 | 1795 |
/// It is \c true if the given edge is enabled (i.e. not hidden). |
1796 | 1796 |
bool status(const Edge& e) const { return Parent::status(e); } |
1797 | 1797 |
|
1798 | 1798 |
/// \brief Disables the given edge |
1799 | 1799 |
/// |
1800 | 1800 |
/// This function disables the given edge in the subgraph, |
1801 | 1801 |
/// so the iteration jumps over it. |
1802 | 1802 |
/// It is the same as \ref status() "status(e, false)". |
1803 | 1803 |
void disable(const Edge& e) const { Parent::status(e, false); } |
1804 | 1804 |
|
1805 | 1805 |
/// \brief Enables the given edge |
1806 | 1806 |
/// |
1807 | 1807 |
/// This function enables the given edge in the subgraph. |
1808 | 1808 |
/// It is the same as \ref status() "status(e, true)". |
1809 | 1809 |
void enable(const Edge& e) const { Parent::status(e, true); } |
1810 | 1810 |
|
1811 | 1811 |
}; |
1812 | 1812 |
|
1813 | 1813 |
/// \brief Returns a read-only FilterEdges adaptor |
1814 | 1814 |
/// |
1815 | 1815 |
/// This function just returns a read-only \ref FilterEdges adaptor. |
1816 | 1816 |
/// \ingroup graph_adaptors |
1817 | 1817 |
/// \relates FilterEdges |
1818 | 1818 |
template<typename GR, typename EF> |
1819 | 1819 |
FilterEdges<const GR, EF> |
1820 | 1820 |
filterEdges(const GR& graph, EF& edge_filter) { |
1821 | 1821 |
return FilterEdges<const GR, EF>(graph, edge_filter); |
1822 | 1822 |
} |
1823 | 1823 |
|
1824 | 1824 |
template<typename GR, typename EF> |
1825 | 1825 |
FilterEdges<const GR, const EF> |
1826 | 1826 |
filterEdges(const GR& graph, const EF& edge_filter) { |
1827 | 1827 |
return FilterEdges<const GR, const EF>(graph, edge_filter); |
1828 | 1828 |
} |
1829 | 1829 |
|
1830 | 1830 |
|
1831 | 1831 |
template <typename DGR> |
1832 | 1832 |
class UndirectorBase { |
1833 | 1833 |
public: |
1834 | 1834 |
typedef DGR Digraph; |
1835 | 1835 |
typedef UndirectorBase Adaptor; |
1836 | 1836 |
|
1837 | 1837 |
typedef True UndirectedTag; |
1838 | 1838 |
|
1839 | 1839 |
typedef typename Digraph::Arc Edge; |
1840 | 1840 |
typedef typename Digraph::Node Node; |
1841 | 1841 |
|
1842 | 1842 |
class Arc { |
1843 | 1843 |
friend class UndirectorBase; |
1844 | 1844 |
protected: |
1845 | 1845 |
Edge _edge; |
1846 | 1846 |
bool _forward; |
1847 | 1847 |
|
1848 |
Arc(const Edge& edge, bool forward) |
|
1848 |
Arc(const Edge& edge, bool forward) |
|
1849 | 1849 |
: _edge(edge), _forward(forward) {} |
1850 | 1850 |
|
1851 | 1851 |
public: |
1852 | 1852 |
Arc() {} |
1853 | 1853 |
|
1854 | 1854 |
Arc(Invalid) : _edge(INVALID), _forward(true) {} |
1855 | 1855 |
|
1856 | 1856 |
operator const Edge&() const { return _edge; } |
1857 | 1857 |
|
1858 | 1858 |
bool operator==(const Arc &other) const { |
1859 | 1859 |
return _forward == other._forward && _edge == other._edge; |
1860 | 1860 |
} |
1861 | 1861 |
bool operator!=(const Arc &other) const { |
1862 | 1862 |
return _forward != other._forward || _edge != other._edge; |
1863 | 1863 |
} |
1864 | 1864 |
bool operator<(const Arc &other) const { |
1865 | 1865 |
return _forward < other._forward || |
1866 | 1866 |
(_forward == other._forward && _edge < other._edge); |
1867 | 1867 |
} |
1868 | 1868 |
}; |
1869 | 1869 |
|
1870 | 1870 |
void first(Node& n) const { |
1871 | 1871 |
_digraph->first(n); |
1872 | 1872 |
} |
1873 | 1873 |
|
1874 | 1874 |
void next(Node& n) const { |
1875 | 1875 |
_digraph->next(n); |
1876 | 1876 |
} |
1877 | 1877 |
|
1878 | 1878 |
void first(Arc& a) const { |
1879 | 1879 |
_digraph->first(a._edge); |
1880 | 1880 |
a._forward = true; |
1881 | 1881 |
} |
1882 | 1882 |
|
1883 | 1883 |
void next(Arc& a) const { |
1884 | 1884 |
if (a._forward) { |
1885 | 1885 |
a._forward = false; |
1886 | 1886 |
} else { |
1887 | 1887 |
_digraph->next(a._edge); |
1888 | 1888 |
a._forward = true; |
1889 | 1889 |
} |
1890 | 1890 |
} |
1891 | 1891 |
|
1892 | 1892 |
void first(Edge& e) const { |
1893 | 1893 |
_digraph->first(e); |
1894 | 1894 |
} |
1895 | 1895 |
|
1896 | 1896 |
void next(Edge& e) const { |
1897 | 1897 |
_digraph->next(e); |
1898 | 1898 |
} |
1899 | 1899 |
|
1900 | 1900 |
void firstOut(Arc& a, const Node& n) const { |
1901 | 1901 |
_digraph->firstIn(a._edge, n); |
1902 | 1902 |
if (a._edge != INVALID ) { |
1903 | 1903 |
a._forward = false; |
1904 | 1904 |
} else { |
1905 | 1905 |
_digraph->firstOut(a._edge, n); |
1906 | 1906 |
a._forward = true; |
1907 | 1907 |
} |
1908 | 1908 |
} |
1909 | 1909 |
void nextOut(Arc &a) const { |
1910 | 1910 |
if (!a._forward) { |
1911 | 1911 |
Node n = _digraph->target(a._edge); |
1912 | 1912 |
_digraph->nextIn(a._edge); |
... | ... |
@@ -2024,247 +2024,247 @@ |
2024 | 2024 |
Arc findArc(Node s, Node t, Arc p = INVALID) const { |
2025 | 2025 |
if (p == INVALID) { |
2026 | 2026 |
Edge arc = _digraph->findArc(s, t); |
2027 | 2027 |
if (arc != INVALID) return direct(arc, true); |
2028 | 2028 |
arc = _digraph->findArc(t, s); |
2029 | 2029 |
if (arc != INVALID) return direct(arc, false); |
2030 | 2030 |
} else if (direction(p)) { |
2031 | 2031 |
Edge arc = _digraph->findArc(s, t, p); |
2032 | 2032 |
if (arc != INVALID) return direct(arc, true); |
2033 | 2033 |
arc = _digraph->findArc(t, s); |
2034 | 2034 |
if (arc != INVALID) return direct(arc, false); |
2035 | 2035 |
} else { |
2036 | 2036 |
Edge arc = _digraph->findArc(t, s, p); |
2037 | 2037 |
if (arc != INVALID) return direct(arc, false); |
2038 | 2038 |
} |
2039 | 2039 |
return INVALID; |
2040 | 2040 |
} |
2041 | 2041 |
|
2042 | 2042 |
typedef FindArcTag FindEdgeTag; |
2043 | 2043 |
Edge findEdge(Node s, Node t, Edge p = INVALID) const { |
2044 | 2044 |
if (s != t) { |
2045 | 2045 |
if (p == INVALID) { |
2046 | 2046 |
Edge arc = _digraph->findArc(s, t); |
2047 | 2047 |
if (arc != INVALID) return arc; |
2048 | 2048 |
arc = _digraph->findArc(t, s); |
2049 | 2049 |
if (arc != INVALID) return arc; |
2050 | 2050 |
} else if (_digraph->source(p) == s) { |
2051 | 2051 |
Edge arc = _digraph->findArc(s, t, p); |
2052 | 2052 |
if (arc != INVALID) return arc; |
2053 | 2053 |
arc = _digraph->findArc(t, s); |
2054 | 2054 |
if (arc != INVALID) return arc; |
2055 | 2055 |
} else { |
2056 | 2056 |
Edge arc = _digraph->findArc(t, s, p); |
2057 | 2057 |
if (arc != INVALID) return arc; |
2058 | 2058 |
} |
2059 | 2059 |
} else { |
2060 | 2060 |
return _digraph->findArc(s, t, p); |
2061 | 2061 |
} |
2062 | 2062 |
return INVALID; |
2063 | 2063 |
} |
2064 | 2064 |
|
2065 | 2065 |
private: |
2066 | 2066 |
|
2067 | 2067 |
template <typename V> |
2068 | 2068 |
class ArcMapBase { |
2069 | 2069 |
private: |
2070 | 2070 |
|
2071 | 2071 |
typedef typename DGR::template ArcMap<V> MapImpl; |
2072 | 2072 |
|
2073 | 2073 |
public: |
2074 | 2074 |
|
2075 | 2075 |
typedef typename MapTraits<MapImpl>::ReferenceMapTag ReferenceMapTag; |
2076 | 2076 |
|
2077 | 2077 |
typedef V Value; |
2078 | 2078 |
typedef Arc Key; |
2079 | 2079 |
typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReturnValue; |
2080 | 2080 |
typedef typename MapTraits<MapImpl>::ReturnValue ReturnValue; |
2081 | 2081 |
typedef typename MapTraits<MapImpl>::ConstReturnValue ConstReference; |
2082 | 2082 |
typedef typename MapTraits<MapImpl>::ReturnValue Reference; |
2083 | 2083 |
|
2084 | 2084 |
ArcMapBase(const UndirectorBase<DGR>& adaptor) : |
2085 | 2085 |
_forward(*adaptor._digraph), _backward(*adaptor._digraph) {} |
2086 | 2086 |
|
2087 | 2087 |
ArcMapBase(const UndirectorBase<DGR>& adaptor, const V& value) |
2088 |
: _forward(*adaptor._digraph, value), |
|
2088 |
: _forward(*adaptor._digraph, value), |
|
2089 | 2089 |
_backward(*adaptor._digraph, value) {} |
2090 | 2090 |
|
2091 | 2091 |
void set(const Arc& a, const V& value) { |
2092 | 2092 |
if (direction(a)) { |
2093 | 2093 |
_forward.set(a, value); |
2094 | 2094 |
} else { |
2095 | 2095 |
_backward.set(a, value); |
2096 | 2096 |
} |
2097 | 2097 |
} |
2098 | 2098 |
|
2099 | 2099 |
ConstReturnValue operator[](const Arc& a) const { |
2100 | 2100 |
if (direction(a)) { |
2101 | 2101 |
return _forward[a]; |
2102 | 2102 |
} else { |
2103 | 2103 |
return _backward[a]; |
2104 | 2104 |
} |
2105 | 2105 |
} |
2106 | 2106 |
|
2107 | 2107 |
ReturnValue operator[](const Arc& a) { |
2108 | 2108 |
if (direction(a)) { |
2109 | 2109 |
return _forward[a]; |
2110 | 2110 |
} else { |
2111 | 2111 |
return _backward[a]; |
2112 | 2112 |
} |
2113 | 2113 |
} |
2114 | 2114 |
|
2115 | 2115 |
protected: |
2116 | 2116 |
|
2117 | 2117 |
MapImpl _forward, _backward; |
2118 | 2118 |
|
2119 | 2119 |
}; |
2120 | 2120 |
|
2121 | 2121 |
public: |
2122 | 2122 |
|
2123 | 2123 |
template <typename V> |
2124 | 2124 |
class NodeMap : public DGR::template NodeMap<V> { |
2125 | 2125 |
typedef typename DGR::template NodeMap<V> Parent; |
2126 | 2126 |
|
2127 | 2127 |
public: |
2128 | 2128 |
typedef V Value; |
2129 | 2129 |
|
2130 | 2130 |
explicit NodeMap(const UndirectorBase<DGR>& adaptor) |
2131 | 2131 |
: Parent(*adaptor._digraph) {} |
2132 | 2132 |
|
2133 | 2133 |
NodeMap(const UndirectorBase<DGR>& adaptor, const V& value) |
2134 | 2134 |
: Parent(*adaptor._digraph, value) { } |
2135 | 2135 |
|
2136 | 2136 |
private: |
2137 | 2137 |
NodeMap& operator=(const NodeMap& cmap) { |
2138 | 2138 |
return operator=<NodeMap>(cmap); |
2139 | 2139 |
} |
2140 | 2140 |
|
2141 | 2141 |
template <typename CMap> |
2142 | 2142 |
NodeMap& operator=(const CMap& cmap) { |
2143 | 2143 |
Parent::operator=(cmap); |
2144 | 2144 |
return *this; |
2145 | 2145 |
} |
2146 | 2146 |
|
2147 | 2147 |
}; |
2148 | 2148 |
|
2149 | 2149 |
template <typename V> |
2150 | 2150 |
class ArcMap |
2151 | 2151 |
: public SubMapExtender<UndirectorBase<DGR>, ArcMapBase<V> > { |
2152 | 2152 |
typedef SubMapExtender<UndirectorBase<DGR>, ArcMapBase<V> > Parent; |
2153 | 2153 |
|
2154 | 2154 |
public: |
2155 | 2155 |
typedef V Value; |
2156 | 2156 |
|
2157 | 2157 |
explicit ArcMap(const UndirectorBase<DGR>& adaptor) |
2158 | 2158 |
: Parent(adaptor) {} |
2159 | 2159 |
|
2160 | 2160 |
ArcMap(const UndirectorBase<DGR>& adaptor, const V& value) |
2161 | 2161 |
: Parent(adaptor, value) {} |
2162 | 2162 |
|
2163 | 2163 |
private: |
2164 | 2164 |
ArcMap& operator=(const ArcMap& cmap) { |
2165 | 2165 |
return operator=<ArcMap>(cmap); |
2166 | 2166 |
} |
2167 | 2167 |
|
2168 | 2168 |
template <typename CMap> |
2169 | 2169 |
ArcMap& operator=(const CMap& cmap) { |
2170 | 2170 |
Parent::operator=(cmap); |
2171 | 2171 |
return *this; |
2172 | 2172 |
} |
2173 | 2173 |
}; |
2174 | 2174 |
|
2175 | 2175 |
template <typename V> |
2176 | 2176 |
class EdgeMap : public Digraph::template ArcMap<V> { |
2177 | 2177 |
typedef typename Digraph::template ArcMap<V> Parent; |
2178 | 2178 |
|
2179 | 2179 |
public: |
2180 | 2180 |
typedef V Value; |
2181 | 2181 |
|
2182 | 2182 |
explicit EdgeMap(const UndirectorBase<DGR>& adaptor) |
2183 | 2183 |
: Parent(*adaptor._digraph) {} |
2184 | 2184 |
|
2185 | 2185 |
EdgeMap(const UndirectorBase<DGR>& adaptor, const V& value) |
2186 | 2186 |
: Parent(*adaptor._digraph, value) {} |
2187 | 2187 |
|
2188 | 2188 |
private: |
2189 | 2189 |
EdgeMap& operator=(const EdgeMap& cmap) { |
2190 | 2190 |
return operator=<EdgeMap>(cmap); |
2191 | 2191 |
} |
2192 | 2192 |
|
2193 | 2193 |
template <typename CMap> |
2194 | 2194 |
EdgeMap& operator=(const CMap& cmap) { |
2195 | 2195 |
Parent::operator=(cmap); |
2196 | 2196 |
return *this; |
2197 | 2197 |
} |
2198 | 2198 |
|
2199 | 2199 |
}; |
2200 | 2200 |
|
2201 | 2201 |
typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier; |
2202 | 2202 |
NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } |
2203 | 2203 |
|
2204 | 2204 |
typedef typename ItemSetTraits<DGR, Edge>::ItemNotifier EdgeNotifier; |
2205 | 2205 |
EdgeNotifier& notifier(Edge) const { return _digraph->notifier(Edge()); } |
2206 |
|
|
2206 |
|
|
2207 | 2207 |
typedef EdgeNotifier ArcNotifier; |
2208 | 2208 |
ArcNotifier& notifier(Arc) const { return _digraph->notifier(Edge()); } |
2209 | 2209 |
|
2210 | 2210 |
protected: |
2211 | 2211 |
|
2212 | 2212 |
UndirectorBase() : _digraph(0) {} |
2213 | 2213 |
|
2214 | 2214 |
DGR* _digraph; |
2215 | 2215 |
|
2216 | 2216 |
void initialize(DGR& digraph) { |
2217 | 2217 |
_digraph = &digraph; |
2218 | 2218 |
} |
2219 | 2219 |
|
2220 | 2220 |
}; |
2221 | 2221 |
|
2222 | 2222 |
/// \ingroup graph_adaptors |
2223 | 2223 |
/// |
2224 | 2224 |
/// \brief Adaptor class for viewing a digraph as an undirected graph. |
2225 | 2225 |
/// |
2226 | 2226 |
/// Undirector adaptor can be used for viewing a digraph as an undirected |
2227 | 2227 |
/// graph. All arcs of the underlying digraph are showed in the |
2228 | 2228 |
/// adaptor as an edge (and also as a pair of arcs, of course). |
2229 | 2229 |
/// This adaptor conforms to the \ref concepts::Graph "Graph" concept. |
2230 | 2230 |
/// |
2231 | 2231 |
/// The adapted digraph can also be modified through this adaptor |
2232 | 2232 |
/// by adding or removing nodes or edges, unless the \c GR template |
2233 | 2233 |
/// parameter is set to be \c const. |
2234 | 2234 |
/// |
2235 | 2235 |
/// \tparam DGR The type of the adapted digraph. |
2236 | 2236 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
2237 | 2237 |
/// It can also be specified to be \c const. |
2238 | 2238 |
/// |
2239 | 2239 |
/// \note The \c Node type of this adaptor and the adapted digraph are |
2240 | 2240 |
/// convertible to each other, moreover the \c Edge type of the adaptor |
2241 | 2241 |
/// and the \c Arc type of the adapted digraph are also convertible to |
2242 | 2242 |
/// each other. |
2243 | 2243 |
/// (Thus the \c Arc type of the adaptor is convertible to the \c Arc type |
2244 | 2244 |
/// of the adapted digraph.) |
2245 | 2245 |
template<typename DGR> |
2246 | 2246 |
#ifdef DOXYGEN |
2247 | 2247 |
class Undirector { |
2248 | 2248 |
#else |
2249 | 2249 |
class Undirector : |
2250 | 2250 |
public GraphAdaptorExtender<UndirectorBase<DGR> > { |
2251 | 2251 |
#endif |
2252 | 2252 |
typedef GraphAdaptorExtender<UndirectorBase<DGR> > Parent; |
2253 | 2253 |
public: |
2254 | 2254 |
/// The type of the adapted digraph. |
2255 | 2255 |
typedef DGR Digraph; |
2256 | 2256 |
protected: |
2257 | 2257 |
Undirector() { } |
2258 | 2258 |
public: |
2259 | 2259 |
|
2260 | 2260 |
/// \brief Constructor |
2261 | 2261 |
/// |
2262 | 2262 |
/// Creates an undirected graph from the given digraph. |
2263 | 2263 |
Undirector(DGR& digraph) { |
2264 | 2264 |
initialize(digraph); |
2265 | 2265 |
} |
2266 | 2266 |
|
2267 | 2267 |
/// \brief Arc map combined from two original arc maps |
2268 | 2268 |
/// |
2269 | 2269 |
/// This map adaptor class adapts two arc maps of the underlying |
2270 | 2270 |
/// digraph to get an arc map of the undirected graph. |
... | ... |
@@ -2646,268 +2646,268 @@ |
2646 | 2646 |
TL _tolerance; |
2647 | 2647 |
|
2648 | 2648 |
public: |
2649 | 2649 |
|
2650 | 2650 |
ResBackwardFilter(const CM& capacity, const FM& flow, |
2651 | 2651 |
const TL& tolerance = TL()) |
2652 | 2652 |
: _capacity(&capacity), _flow(&flow), _tolerance(tolerance) { } |
2653 | 2653 |
|
2654 | 2654 |
bool operator[](const typename DGR::Arc& a) const { |
2655 | 2655 |
return _tolerance.positive((*_flow)[a]); |
2656 | 2656 |
} |
2657 | 2657 |
}; |
2658 | 2658 |
|
2659 | 2659 |
} |
2660 | 2660 |
|
2661 | 2661 |
/// \ingroup graph_adaptors |
2662 | 2662 |
/// |
2663 | 2663 |
/// \brief Adaptor class for composing the residual digraph for directed |
2664 | 2664 |
/// flow and circulation problems. |
2665 | 2665 |
/// |
2666 | 2666 |
/// ResidualDigraph can be used for composing the \e residual digraph |
2667 | 2667 |
/// for directed flow and circulation problems. Let \f$ G=(V, A) \f$ |
2668 | 2668 |
/// be a directed graph and let \f$ F \f$ be a number type. |
2669 | 2669 |
/// Let \f$ flow, cap: A\to F \f$ be functions on the arcs. |
2670 | 2670 |
/// This adaptor implements a digraph structure with node set \f$ V \f$ |
2671 | 2671 |
/// and arc set \f$ A_{forward}\cup A_{backward} \f$, |
2672 | 2672 |
/// where \f$ A_{forward}=\{uv : uv\in A, flow(uv)<cap(uv)\} \f$ and |
2673 | 2673 |
/// \f$ A_{backward}=\{vu : uv\in A, flow(uv)>0\} \f$, i.e. the so |
2674 | 2674 |
/// called residual digraph. |
2675 | 2675 |
/// When the union \f$ A_{forward}\cup A_{backward} \f$ is taken, |
2676 | 2676 |
/// multiplicities are counted, i.e. the adaptor has exactly |
2677 | 2677 |
/// \f$ |A_{forward}| + |A_{backward}|\f$ arcs (it may have parallel |
2678 | 2678 |
/// arcs). |
2679 | 2679 |
/// This class conforms to the \ref concepts::Digraph "Digraph" concept. |
2680 | 2680 |
/// |
2681 | 2681 |
/// \tparam DGR The type of the adapted digraph. |
2682 | 2682 |
/// It must conform to the \ref concepts::Digraph "Digraph" concept. |
2683 | 2683 |
/// It is implicitly \c const. |
2684 | 2684 |
/// \tparam CM The type of the capacity map. |
2685 | 2685 |
/// It must be an arc map of some numerical type, which defines |
2686 | 2686 |
/// the capacities in the flow problem. It is implicitly \c const. |
2687 | 2687 |
/// The default type is |
2688 | 2688 |
/// \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
2689 | 2689 |
/// \tparam FM The type of the flow map. |
2690 | 2690 |
/// It must be an arc map of some numerical type, which defines |
2691 | 2691 |
/// the flow values in the flow problem. The default type is \c CM. |
2692 | 2692 |
/// \tparam TL The tolerance type for handling inexact computation. |
2693 | 2693 |
/// The default tolerance type depends on the value type of the |
2694 | 2694 |
/// capacity map. |
2695 | 2695 |
/// |
2696 | 2696 |
/// \note This adaptor is implemented using Undirector and FilterArcs |
2697 | 2697 |
/// adaptors. |
2698 | 2698 |
/// |
2699 | 2699 |
/// \note The \c Node type of this adaptor and the adapted digraph are |
2700 | 2700 |
/// convertible to each other, moreover the \c Arc type of the adaptor |
2701 | 2701 |
/// is convertible to the \c Arc type of the adapted digraph. |
2702 | 2702 |
#ifdef DOXYGEN |
2703 | 2703 |
template<typename DGR, typename CM, typename FM, typename TL> |
2704 | 2704 |
class ResidualDigraph |
2705 | 2705 |
#else |
2706 | 2706 |
template<typename DGR, |
2707 | 2707 |
typename CM = typename DGR::template ArcMap<int>, |
2708 | 2708 |
typename FM = CM, |
2709 | 2709 |
typename TL = Tolerance<typename CM::Value> > |
2710 |
class ResidualDigraph |
|
2710 |
class ResidualDigraph |
|
2711 | 2711 |
: public SubDigraph< |
2712 | 2712 |
Undirector<const DGR>, |
2713 | 2713 |
ConstMap<typename DGR::Node, Const<bool, true> >, |
2714 | 2714 |
typename Undirector<const DGR>::template CombinedArcMap< |
2715 | 2715 |
_adaptor_bits::ResForwardFilter<const DGR, CM, FM, TL>, |
2716 | 2716 |
_adaptor_bits::ResBackwardFilter<const DGR, CM, FM, TL> > > |
2717 | 2717 |
#endif |
2718 | 2718 |
{ |
2719 | 2719 |
public: |
2720 | 2720 |
|
2721 | 2721 |
/// The type of the underlying digraph. |
2722 | 2722 |
typedef DGR Digraph; |
2723 | 2723 |
/// The type of the capacity map. |
2724 | 2724 |
typedef CM CapacityMap; |
2725 | 2725 |
/// The type of the flow map. |
2726 | 2726 |
typedef FM FlowMap; |
2727 | 2727 |
/// The tolerance type. |
2728 | 2728 |
typedef TL Tolerance; |
2729 | 2729 |
|
2730 | 2730 |
typedef typename CapacityMap::Value Value; |
2731 | 2731 |
typedef ResidualDigraph Adaptor; |
2732 | 2732 |
|
2733 | 2733 |
protected: |
2734 | 2734 |
|
2735 | 2735 |
typedef Undirector<const Digraph> Undirected; |
2736 | 2736 |
|
2737 | 2737 |
typedef ConstMap<typename DGR::Node, Const<bool, true> > NodeFilter; |
2738 | 2738 |
|
2739 | 2739 |
typedef _adaptor_bits::ResForwardFilter<const DGR, CM, |
2740 | 2740 |
FM, TL> ForwardFilter; |
2741 | 2741 |
|
2742 | 2742 |
typedef _adaptor_bits::ResBackwardFilter<const DGR, CM, |
2743 | 2743 |
FM, TL> BackwardFilter; |
2744 | 2744 |
|
2745 | 2745 |
typedef typename Undirected:: |
2746 | 2746 |
template CombinedArcMap<ForwardFilter, BackwardFilter> ArcFilter; |
2747 | 2747 |
|
2748 | 2748 |
typedef SubDigraph<Undirected, NodeFilter, ArcFilter> Parent; |
2749 | 2749 |
|
2750 | 2750 |
const CapacityMap* _capacity; |
2751 | 2751 |
FlowMap* _flow; |
2752 | 2752 |
|
2753 | 2753 |
Undirected _graph; |
2754 | 2754 |
NodeFilter _node_filter; |
2755 | 2755 |
ForwardFilter _forward_filter; |
2756 | 2756 |
BackwardFilter _backward_filter; |
2757 | 2757 |
ArcFilter _arc_filter; |
2758 | 2758 |
|
2759 | 2759 |
public: |
2760 | 2760 |
|
2761 | 2761 |
/// \brief Constructor |
2762 | 2762 |
/// |
2763 | 2763 |
/// Constructor of the residual digraph adaptor. The parameters are the |
2764 | 2764 |
/// digraph, the capacity map, the flow map, and a tolerance object. |
2765 | 2765 |
ResidualDigraph(const DGR& digraph, const CM& capacity, |
2766 | 2766 |
FM& flow, const TL& tolerance = Tolerance()) |
2767 |
: Parent(), _capacity(&capacity), _flow(&flow), |
|
2767 |
: Parent(), _capacity(&capacity), _flow(&flow), |
|
2768 | 2768 |
_graph(digraph), _node_filter(), |
2769 | 2769 |
_forward_filter(capacity, flow, tolerance), |
2770 | 2770 |
_backward_filter(capacity, flow, tolerance), |
2771 | 2771 |
_arc_filter(_forward_filter, _backward_filter) |
2772 | 2772 |
{ |
2773 | 2773 |
Parent::initialize(_graph, _node_filter, _arc_filter); |
2774 | 2774 |
} |
2775 | 2775 |
|
2776 | 2776 |
typedef typename Parent::Arc Arc; |
2777 | 2777 |
|
2778 | 2778 |
/// \brief Returns the residual capacity of the given arc. |
2779 | 2779 |
/// |
2780 | 2780 |
/// Returns the residual capacity of the given arc. |
2781 | 2781 |
Value residualCapacity(const Arc& a) const { |
2782 | 2782 |
if (Undirected::direction(a)) { |
2783 | 2783 |
return (*_capacity)[a] - (*_flow)[a]; |
2784 | 2784 |
} else { |
2785 | 2785 |
return (*_flow)[a]; |
2786 | 2786 |
} |
2787 | 2787 |
} |
2788 | 2788 |
|
2789 | 2789 |
/// \brief Augments on the given arc in the residual digraph. |
2790 | 2790 |
/// |
2791 | 2791 |
/// Augments on the given arc in the residual digraph. It increases |
2792 | 2792 |
/// or decreases the flow value on the original arc according to the |
2793 | 2793 |
/// direction of the residual arc. |
2794 | 2794 |
void augment(const Arc& a, const Value& v) const { |
2795 | 2795 |
if (Undirected::direction(a)) { |
2796 | 2796 |
_flow->set(a, (*_flow)[a] + v); |
2797 | 2797 |
} else { |
2798 | 2798 |
_flow->set(a, (*_flow)[a] - v); |
2799 | 2799 |
} |
2800 | 2800 |
} |
2801 | 2801 |
|
2802 | 2802 |
/// \brief Returns \c true if the given residual arc is a forward arc. |
2803 | 2803 |
/// |
2804 | 2804 |
/// Returns \c true if the given residual arc has the same orientation |
2805 | 2805 |
/// as the original arc, i.e. it is a so called forward arc. |
2806 | 2806 |
static bool forward(const Arc& a) { |
2807 | 2807 |
return Undirected::direction(a); |
2808 | 2808 |
} |
2809 | 2809 |
|
2810 | 2810 |
/// \brief Returns \c true if the given residual arc is a backward arc. |
2811 | 2811 |
/// |
2812 | 2812 |
/// Returns \c true if the given residual arc has the opposite orientation |
2813 | 2813 |
/// than the original arc, i.e. it is a so called backward arc. |
2814 | 2814 |
static bool backward(const Arc& a) { |
2815 | 2815 |
return !Undirected::direction(a); |
2816 | 2816 |
} |
2817 | 2817 |
|
2818 | 2818 |
/// \brief Returns the forward oriented residual arc. |
2819 | 2819 |
/// |
2820 | 2820 |
/// Returns the forward oriented residual arc related to the given |
2821 | 2821 |
/// arc of the underlying digraph. |
2822 | 2822 |
static Arc forward(const typename Digraph::Arc& a) { |
2823 | 2823 |
return Undirected::direct(a, true); |
2824 | 2824 |
} |
2825 | 2825 |
|
2826 | 2826 |
/// \brief Returns the backward oriented residual arc. |
2827 | 2827 |
/// |
2828 | 2828 |
/// Returns the backward oriented residual arc related to the given |
2829 | 2829 |
/// arc of the underlying digraph. |
2830 | 2830 |
static Arc backward(const typename Digraph::Arc& a) { |
2831 | 2831 |
return Undirected::direct(a, false); |
2832 | 2832 |
} |
2833 | 2833 |
|
2834 | 2834 |
/// \brief Residual capacity map. |
2835 | 2835 |
/// |
2836 | 2836 |
/// This map adaptor class can be used for obtaining the residual |
2837 | 2837 |
/// capacities as an arc map of the residual digraph. |
2838 | 2838 |
/// Its value type is inherited from the capacity map. |
2839 | 2839 |
class ResidualCapacity { |
2840 | 2840 |
protected: |
2841 | 2841 |
const Adaptor* _adaptor; |
2842 | 2842 |
public: |
2843 | 2843 |
/// The key type of the map |
2844 | 2844 |
typedef Arc Key; |
2845 | 2845 |
/// The value type of the map |
2846 | 2846 |
typedef typename CapacityMap::Value Value; |
2847 | 2847 |
|
2848 | 2848 |
/// Constructor |
2849 |
ResidualCapacity(const ResidualDigraph<DGR, CM, FM, TL>& adaptor) |
|
2849 |
ResidualCapacity(const ResidualDigraph<DGR, CM, FM, TL>& adaptor) |
|
2850 | 2850 |
: _adaptor(&adaptor) {} |
2851 | 2851 |
|
2852 | 2852 |
/// Returns the value associated with the given residual arc |
2853 | 2853 |
Value operator[](const Arc& a) const { |
2854 | 2854 |
return _adaptor->residualCapacity(a); |
2855 | 2855 |
} |
2856 | 2856 |
|
2857 | 2857 |
}; |
2858 | 2858 |
|
2859 | 2859 |
/// \brief Returns a residual capacity map |
2860 | 2860 |
/// |
2861 | 2861 |
/// This function just returns a residual capacity map. |
2862 | 2862 |
ResidualCapacity residualCapacity() const { |
2863 | 2863 |
return ResidualCapacity(*this); |
2864 | 2864 |
} |
2865 | 2865 |
|
2866 | 2866 |
}; |
2867 | 2867 |
|
2868 | 2868 |
/// \brief Returns a (read-only) Residual adaptor |
2869 | 2869 |
/// |
2870 | 2870 |
/// This function just returns a (read-only) \ref ResidualDigraph adaptor. |
2871 | 2871 |
/// \ingroup graph_adaptors |
2872 | 2872 |
/// \relates ResidualDigraph |
2873 | 2873 |
template<typename DGR, typename CM, typename FM> |
2874 | 2874 |
ResidualDigraph<DGR, CM, FM> |
2875 | 2875 |
residualDigraph(const DGR& digraph, const CM& capacity_map, FM& flow_map) { |
2876 | 2876 |
return ResidualDigraph<DGR, CM, FM> (digraph, capacity_map, flow_map); |
2877 | 2877 |
} |
2878 | 2878 |
|
2879 | 2879 |
|
2880 | 2880 |
template <typename DGR> |
2881 | 2881 |
class SplitNodesBase { |
2882 | 2882 |
typedef DigraphAdaptorBase<const DGR> Parent; |
2883 | 2883 |
|
2884 | 2884 |
public: |
2885 | 2885 |
|
2886 | 2886 |
typedef DGR Digraph; |
2887 | 2887 |
typedef SplitNodesBase Adaptor; |
2888 | 2888 |
|
2889 | 2889 |
typedef typename DGR::Node DigraphNode; |
2890 | 2890 |
typedef typename DGR::Arc DigraphArc; |
2891 | 2891 |
|
2892 | 2892 |
class Node; |
2893 | 2893 |
class Arc; |
2894 | 2894 |
|
2895 | 2895 |
private: |
2896 | 2896 |
|
2897 | 2897 |
template <typename T> class NodeMapBase; |
2898 | 2898 |
template <typename T> class ArcMapBase; |
2899 | 2899 |
|
2900 | 2900 |
public: |
2901 | 2901 |
|
2902 | 2902 |
class Node : public DigraphNode { |
2903 | 2903 |
friend class SplitNodesBase; |
2904 | 2904 |
template <typename T> friend class NodeMapBase; |
2905 | 2905 |
private: |
2906 | 2906 |
|
2907 | 2907 |
bool _in; |
2908 | 2908 |
Node(DigraphNode node, bool in) |
2909 | 2909 |
: DigraphNode(node), _in(in) {} |
2910 | 2910 |
|
2911 | 2911 |
public: |
2912 | 2912 |
|
2913 | 2913 |
Node() {} |
... | ... |
@@ -3362,129 +3362,129 @@ |
3362 | 3362 |
static bool inNode(const Node& n) { |
3363 | 3363 |
return Parent::inNode(n); |
3364 | 3364 |
} |
3365 | 3365 |
|
3366 | 3366 |
/// \brief Returns \c true if the given node is an out-node. |
3367 | 3367 |
/// |
3368 | 3368 |
/// Returns \c true if the given node is an out-node. |
3369 | 3369 |
static bool outNode(const Node& n) { |
3370 | 3370 |
return Parent::outNode(n); |
3371 | 3371 |
} |
3372 | 3372 |
|
3373 | 3373 |
/// \brief Returns \c true if the given arc is an original arc. |
3374 | 3374 |
/// |
3375 | 3375 |
/// Returns \c true if the given arc is one of the arcs in the |
3376 | 3376 |
/// original digraph. |
3377 | 3377 |
static bool origArc(const Arc& a) { |
3378 | 3378 |
return Parent::origArc(a); |
3379 | 3379 |
} |
3380 | 3380 |
|
3381 | 3381 |
/// \brief Returns \c true if the given arc is a bind arc. |
3382 | 3382 |
/// |
3383 | 3383 |
/// Returns \c true if the given arc is a bind arc, i.e. it connects |
3384 | 3384 |
/// an in-node and an out-node. |
3385 | 3385 |
static bool bindArc(const Arc& a) { |
3386 | 3386 |
return Parent::bindArc(a); |
3387 | 3387 |
} |
3388 | 3388 |
|
3389 | 3389 |
/// \brief Returns the in-node created from the given original node. |
3390 | 3390 |
/// |
3391 | 3391 |
/// Returns the in-node created from the given original node. |
3392 | 3392 |
static Node inNode(const DigraphNode& n) { |
3393 | 3393 |
return Parent::inNode(n); |
3394 | 3394 |
} |
3395 | 3395 |
|
3396 | 3396 |
/// \brief Returns the out-node created from the given original node. |
3397 | 3397 |
/// |
3398 | 3398 |
/// Returns the out-node created from the given original node. |
3399 | 3399 |
static Node outNode(const DigraphNode& n) { |
3400 | 3400 |
return Parent::outNode(n); |
3401 | 3401 |
} |
3402 | 3402 |
|
3403 | 3403 |
/// \brief Returns the bind arc that corresponds to the given |
3404 | 3404 |
/// original node. |
3405 | 3405 |
/// |
3406 | 3406 |
/// Returns the bind arc in the adaptor that corresponds to the given |
3407 | 3407 |
/// original node, i.e. the arc connecting the in-node and out-node |
3408 | 3408 |
/// of \c n. |
3409 | 3409 |
static Arc arc(const DigraphNode& n) { |
3410 | 3410 |
return Parent::arc(n); |
3411 | 3411 |
} |
3412 | 3412 |
|
3413 | 3413 |
/// \brief Returns the arc that corresponds to the given original arc. |
3414 | 3414 |
/// |
3415 | 3415 |
/// Returns the arc in the adaptor that corresponds to the given |
3416 | 3416 |
/// original arc. |
3417 | 3417 |
static Arc arc(const DigraphArc& a) { |
3418 | 3418 |
return Parent::arc(a); |
3419 | 3419 |
} |
3420 | 3420 |
|
3421 | 3421 |
/// \brief Node map combined from two original node maps |
3422 | 3422 |
/// |
3423 | 3423 |
/// This map adaptor class adapts two node maps of the original digraph |
3424 | 3424 |
/// to get a node map of the split digraph. |
3425 | 3425 |
/// Its value type is inherited from the first node map type (\c IN). |
3426 |
/// \tparam IN The type of the node map for the in-nodes. |
|
3426 |
/// \tparam IN The type of the node map for the in-nodes. |
|
3427 | 3427 |
/// \tparam OUT The type of the node map for the out-nodes. |
3428 | 3428 |
template <typename IN, typename OUT> |
3429 | 3429 |
class CombinedNodeMap { |
3430 | 3430 |
public: |
3431 | 3431 |
|
3432 | 3432 |
/// The key type of the map |
3433 | 3433 |
typedef Node Key; |
3434 | 3434 |
/// The value type of the map |
3435 | 3435 |
typedef typename IN::Value Value; |
3436 | 3436 |
|
3437 | 3437 |
typedef typename MapTraits<IN>::ReferenceMapTag ReferenceMapTag; |
3438 | 3438 |
typedef typename MapTraits<IN>::ReturnValue ReturnValue; |
3439 | 3439 |
typedef typename MapTraits<IN>::ConstReturnValue ConstReturnValue; |
3440 | 3440 |
typedef typename MapTraits<IN>::ReturnValue Reference; |
3441 | 3441 |
typedef typename MapTraits<IN>::ConstReturnValue ConstReference; |
3442 | 3442 |
|
3443 | 3443 |
/// Constructor |
3444 | 3444 |
CombinedNodeMap(IN& in_map, OUT& out_map) |
3445 | 3445 |
: _in_map(in_map), _out_map(out_map) {} |
3446 | 3446 |
|
3447 | 3447 |
/// Returns the value associated with the given key. |
3448 | 3448 |
Value operator[](const Key& key) const { |
3449 | 3449 |
if (SplitNodesBase<const DGR>::inNode(key)) { |
3450 | 3450 |
return _in_map[key]; |
3451 | 3451 |
} else { |
3452 | 3452 |
return _out_map[key]; |
3453 | 3453 |
} |
3454 | 3454 |
} |
3455 | 3455 |
|
3456 | 3456 |
/// Returns a reference to the value associated with the given key. |
3457 | 3457 |
Value& operator[](const Key& key) { |
3458 | 3458 |
if (SplitNodesBase<const DGR>::inNode(key)) { |
3459 | 3459 |
return _in_map[key]; |
3460 | 3460 |
} else { |
3461 | 3461 |
return _out_map[key]; |
3462 | 3462 |
} |
3463 | 3463 |
} |
3464 | 3464 |
|
3465 | 3465 |
/// Sets the value associated with the given key. |
3466 | 3466 |
void set(const Key& key, const Value& value) { |
3467 | 3467 |
if (SplitNodesBase<const DGR>::inNode(key)) { |
3468 | 3468 |
_in_map.set(key, value); |
3469 | 3469 |
} else { |
3470 | 3470 |
_out_map.set(key, value); |
3471 | 3471 |
} |
3472 | 3472 |
} |
3473 | 3473 |
|
3474 | 3474 |
private: |
3475 | 3475 |
|
3476 | 3476 |
IN& _in_map; |
3477 | 3477 |
OUT& _out_map; |
3478 | 3478 |
|
3479 | 3479 |
}; |
3480 | 3480 |
|
3481 | 3481 |
|
3482 | 3482 |
/// \brief Returns a combined node map |
3483 | 3483 |
/// |
3484 | 3484 |
/// This function just returns a combined node map. |
3485 | 3485 |
template <typename IN, typename OUT> |
3486 | 3486 |
static CombinedNodeMap<IN, OUT> |
3487 | 3487 |
combinedNodeMap(IN& in_map, OUT& out_map) { |
3488 | 3488 |
return CombinedNodeMap<IN, OUT>(in_map, out_map); |
3489 | 3489 |
} |
3490 | 3490 |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
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 | 36 |
///This class implements the \e binary \e heap data structure. |
37 | 37 |
/// |
38 | 38 |
///A \e heap is a data structure for storing items with specified values |
39 | 39 |
///called \e priorities in such a way that finding the item with minimum |
40 | 40 |
///priority is efficient. \c CMP specifies the ordering of the priorities. |
41 | 41 |
///In a heap one can change the priority of an item, add or erase an |
42 | 42 |
///item, etc. |
43 | 43 |
/// |
44 | 44 |
///\tparam PR Type of the priority of the items. |
45 | 45 |
///\tparam IM A read and writable item map with int values, used internally |
46 | 46 |
///to handle the cross references. |
47 | 47 |
///\tparam CMP A functor class for the ordering of the priorities. |
48 | 48 |
///The default is \c std::less<PR>. |
49 | 49 |
/// |
50 | 50 |
///\sa FibHeap |
51 | 51 |
///\sa Dijkstra |
52 | 52 |
template <typename PR, typename IM, typename CMP = std::less<PR> > |
53 | 53 |
class BinHeap { |
54 | 54 |
|
55 | 55 |
public: |
56 | 56 |
///\e |
57 | 57 |
typedef IM ItemIntMap; |
58 | 58 |
///\e |
59 | 59 |
typedef PR Prio; |
60 | 60 |
///\e |
61 | 61 |
typedef typename ItemIntMap::Key Item; |
62 | 62 |
///\e |
63 | 63 |
typedef std::pair<Item,Prio> Pair; |
64 | 64 |
///\e |
65 | 65 |
typedef CMP Compare; |
66 | 66 |
|
67 | 67 |
/// \brief Type to represent the items states. |
68 | 68 |
/// |
69 | 69 |
/// Each Item element have a state associated to it. It may be "in heap", |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_ARRAY_MAP_H |
20 | 20 |
#define LEMON_BITS_ARRAY_MAP_H |
21 | 21 |
|
22 | 22 |
#include <memory> |
23 | 23 |
|
24 | 24 |
#include <lemon/bits/traits.h> |
25 | 25 |
#include <lemon/bits/alteration_notifier.h> |
26 | 26 |
#include <lemon/concept_check.h> |
27 | 27 |
#include <lemon/concepts/maps.h> |
28 | 28 |
|
29 | 29 |
// \ingroup graphbits |
30 | 30 |
// \file |
31 | 31 |
// \brief Graph map based on the array storage. |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
// \ingroup graphbits |
36 | 36 |
// |
37 | 37 |
// \brief Graph map based on the array storage. |
38 | 38 |
// |
39 | 39 |
// The ArrayMap template class is graph map structure that automatically |
40 | 40 |
// updates the map when a key is added to or erased from the graph. |
41 | 41 |
// This map uses the allocators to implement the container functionality. |
42 | 42 |
// |
43 | 43 |
// The template parameters are the Graph, the current Item type and |
44 | 44 |
// the Value type of the map. |
45 | 45 |
template <typename _Graph, typename _Item, typename _Value> |
46 | 46 |
class ArrayMap |
47 | 47 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { |
48 | 48 |
public: |
49 | 49 |
// The graph type. |
50 | 50 |
typedef _Graph GraphType; |
51 | 51 |
// The item type. |
52 | 52 |
typedef _Item Item; |
53 | 53 |
// The reference map tag. |
54 | 54 |
typedef True ReferenceMapTag; |
55 | 55 |
|
56 | 56 |
// The key type of the map. |
57 | 57 |
typedef _Item Key; |
58 | 58 |
// The value type of the map. |
59 | 59 |
typedef _Value Value; |
60 | 60 |
|
61 | 61 |
// The const reference type of the map. |
62 | 62 |
typedef const _Value& ConstReference; |
63 | 63 |
// The reference type of the map. |
64 | 64 |
typedef _Value& Reference; |
65 | 65 |
|
66 | 66 |
// The map type. |
67 | 67 |
typedef ArrayMap Map; |
68 | 68 |
|
69 | 69 |
// The notifier type. |
70 | 70 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
71 | 71 |
|
72 | 72 |
private: |
73 |
|
|
73 |
|
|
74 | 74 |
// The MapBase of the Map which imlements the core regisitry function. |
75 | 75 |
typedef typename Notifier::ObserverBase Parent; |
76 | 76 |
|
77 | 77 |
typedef std::allocator<Value> Allocator; |
78 | 78 |
|
79 | 79 |
public: |
80 | 80 |
|
81 | 81 |
// \brief Graph initialized map constructor. |
82 | 82 |
// |
83 | 83 |
// Graph initialized map constructor. |
84 | 84 |
explicit ArrayMap(const GraphType& graph) { |
85 | 85 |
Parent::attach(graph.notifier(Item())); |
86 | 86 |
allocate_memory(); |
87 | 87 |
Notifier* nf = Parent::notifier(); |
88 | 88 |
Item it; |
89 | 89 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
90 | 90 |
int id = nf->id(it);; |
91 | 91 |
allocator.construct(&(values[id]), Value()); |
92 | 92 |
} |
93 | 93 |
} |
94 | 94 |
|
95 | 95 |
// \brief Constructor to use default value to initialize the map. |
96 | 96 |
// |
97 | 97 |
// It constructs a map and initialize all of the the map. |
98 | 98 |
ArrayMap(const GraphType& graph, const Value& value) { |
99 | 99 |
Parent::attach(graph.notifier(Item())); |
100 | 100 |
allocate_memory(); |
101 | 101 |
Notifier* nf = Parent::notifier(); |
102 | 102 |
Item it; |
103 | 103 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
104 | 104 |
int id = nf->id(it);; |
105 | 105 |
allocator.construct(&(values[id]), value); |
106 | 106 |
} |
107 | 107 |
} |
108 | 108 |
|
109 | 109 |
private: |
110 | 110 |
// \brief Constructor to copy a map of the same map type. |
111 | 111 |
// |
112 | 112 |
// Constructor to copy a map of the same map type. |
113 | 113 |
ArrayMap(const ArrayMap& copy) : Parent() { |
114 | 114 |
if (copy.attached()) { |
115 | 115 |
attach(*copy.notifier()); |
116 | 116 |
} |
117 | 117 |
capacity = copy.capacity; |
118 | 118 |
if (capacity == 0) return; |
119 | 119 |
values = allocator.allocate(capacity); |
120 | 120 |
Notifier* nf = Parent::notifier(); |
121 | 121 |
Item it; |
122 | 122 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
123 | 123 |
int id = nf->id(it);; |
124 | 124 |
allocator.construct(&(values[id]), copy.values[id]); |
125 | 125 |
} |
126 | 126 |
} |
127 | 127 |
|
128 | 128 |
// \brief Assign operator. |
129 | 129 |
// |
130 | 130 |
// This operator assigns for each item in the map the |
131 | 131 |
// value mapped to the same item in the copied map. |
132 | 132 |
// The parameter map should be indiced with the same |
133 | 133 |
// itemset because this assign operator does not change |
134 | 134 |
// the container of the map. |
135 | 135 |
ArrayMap& operator=(const ArrayMap& cmap) { |
136 | 136 |
return operator=<ArrayMap>(cmap); |
137 | 137 |
} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_DEFAULT_MAP_H |
20 | 20 |
#define LEMON_BITS_DEFAULT_MAP_H |
21 | 21 |
|
22 | 22 |
#include <lemon/config.h> |
23 | 23 |
#include <lemon/bits/array_map.h> |
24 | 24 |
#include <lemon/bits/vector_map.h> |
25 | 25 |
//#include <lemon/bits/debug_map.h> |
26 | 26 |
|
27 | 27 |
//\ingroup graphbits |
28 | 28 |
//\file |
29 | 29 |
//\brief Graph maps that construct and destruct their elements dynamically. |
30 | 30 |
|
31 | 31 |
namespace lemon { |
32 | 32 |
|
33 | 33 |
|
34 | 34 |
//#ifndef LEMON_USE_DEBUG_MAP |
35 | 35 |
|
36 | 36 |
template <typename _Graph, typename _Item, typename _Value> |
37 | 37 |
struct DefaultMapSelector { |
38 | 38 |
typedef ArrayMap<_Graph, _Item, _Value> Map; |
39 | 39 |
}; |
40 | 40 |
|
41 | 41 |
// bool |
42 | 42 |
template <typename _Graph, typename _Item> |
43 | 43 |
struct DefaultMapSelector<_Graph, _Item, bool> { |
44 | 44 |
typedef VectorMap<_Graph, _Item, bool> Map; |
45 | 45 |
}; |
46 | 46 |
|
47 | 47 |
// char |
48 | 48 |
template <typename _Graph, typename _Item> |
49 | 49 |
struct DefaultMapSelector<_Graph, _Item, char> { |
50 | 50 |
typedef VectorMap<_Graph, _Item, char> Map; |
51 | 51 |
}; |
52 | 52 |
|
53 | 53 |
template <typename _Graph, typename _Item> |
54 | 54 |
struct DefaultMapSelector<_Graph, _Item, signed char> { |
55 | 55 |
typedef VectorMap<_Graph, _Item, signed char> Map; |
56 | 56 |
}; |
57 | 57 |
|
58 | 58 |
template <typename _Graph, typename _Item> |
59 | 59 |
struct DefaultMapSelector<_Graph, _Item, unsigned char> { |
60 | 60 |
typedef VectorMap<_Graph, _Item, unsigned char> Map; |
61 | 61 |
}; |
62 | 62 |
|
63 | 63 |
|
64 | 64 |
// int |
65 | 65 |
template <typename _Graph, typename _Item> |
66 | 66 |
struct DefaultMapSelector<_Graph, _Item, signed int> { |
67 | 67 |
typedef VectorMap<_Graph, _Item, signed int> Map; |
68 | 68 |
}; |
69 | 69 |
|
... | ... |
@@ -96,87 +96,87 @@ |
96 | 96 |
typedef VectorMap<_Graph, _Item, unsigned long> Map; |
97 | 97 |
}; |
98 | 98 |
|
99 | 99 |
|
100 | 100 |
#if defined LEMON_HAVE_LONG_LONG |
101 | 101 |
|
102 | 102 |
// long long |
103 | 103 |
template <typename _Graph, typename _Item> |
104 | 104 |
struct DefaultMapSelector<_Graph, _Item, signed long long> { |
105 | 105 |
typedef VectorMap<_Graph, _Item, signed long long> Map; |
106 | 106 |
}; |
107 | 107 |
|
108 | 108 |
template <typename _Graph, typename _Item> |
109 | 109 |
struct DefaultMapSelector<_Graph, _Item, unsigned long long> { |
110 | 110 |
typedef VectorMap<_Graph, _Item, unsigned long long> Map; |
111 | 111 |
}; |
112 | 112 |
|
113 | 113 |
#endif |
114 | 114 |
|
115 | 115 |
|
116 | 116 |
// float |
117 | 117 |
template <typename _Graph, typename _Item> |
118 | 118 |
struct DefaultMapSelector<_Graph, _Item, float> { |
119 | 119 |
typedef VectorMap<_Graph, _Item, float> Map; |
120 | 120 |
}; |
121 | 121 |
|
122 | 122 |
|
123 | 123 |
// double |
124 | 124 |
template <typename _Graph, typename _Item> |
125 | 125 |
struct DefaultMapSelector<_Graph, _Item, double> { |
126 | 126 |
typedef VectorMap<_Graph, _Item, double> Map; |
127 | 127 |
}; |
128 | 128 |
|
129 | 129 |
|
130 | 130 |
// long double |
131 | 131 |
template <typename _Graph, typename _Item> |
132 | 132 |
struct DefaultMapSelector<_Graph, _Item, long double> { |
133 | 133 |
typedef VectorMap<_Graph, _Item, long double> Map; |
134 | 134 |
}; |
135 | 135 |
|
136 | 136 |
|
137 | 137 |
// pointer |
138 | 138 |
template <typename _Graph, typename _Item, typename _Ptr> |
139 | 139 |
struct DefaultMapSelector<_Graph, _Item, _Ptr*> { |
140 | 140 |
typedef VectorMap<_Graph, _Item, _Ptr*> Map; |
141 | 141 |
}; |
142 | 142 |
|
143 | 143 |
// #else |
144 | 144 |
|
145 | 145 |
// template <typename _Graph, typename _Item, typename _Value> |
146 | 146 |
// struct DefaultMapSelector { |
147 | 147 |
// typedef DebugMap<_Graph, _Item, _Value> Map; |
148 | 148 |
// }; |
149 | 149 |
|
150 | 150 |
// #endif |
151 | 151 |
|
152 | 152 |
// DefaultMap class |
153 | 153 |
template <typename _Graph, typename _Item, typename _Value> |
154 | 154 |
class DefaultMap |
155 | 155 |
: public DefaultMapSelector<_Graph, _Item, _Value>::Map { |
156 | 156 |
typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent; |
157 | 157 |
|
158 | 158 |
public: |
159 | 159 |
typedef DefaultMap<_Graph, _Item, _Value> Map; |
160 |
|
|
160 |
|
|
161 | 161 |
typedef typename Parent::GraphType GraphType; |
162 | 162 |
typedef typename Parent::Value Value; |
163 | 163 |
|
164 | 164 |
explicit DefaultMap(const GraphType& graph) : Parent(graph) {} |
165 | 165 |
DefaultMap(const GraphType& graph, const Value& value) |
166 | 166 |
: Parent(graph, value) {} |
167 | 167 |
|
168 | 168 |
DefaultMap& operator=(const DefaultMap& cmap) { |
169 | 169 |
return operator=<DefaultMap>(cmap); |
170 | 170 |
} |
171 | 171 |
|
172 | 172 |
template <typename CMap> |
173 | 173 |
DefaultMap& operator=(const CMap& cmap) { |
174 | 174 |
Parent::operator=(cmap); |
175 | 175 |
return *this; |
176 | 176 |
} |
177 | 177 |
|
178 | 178 |
}; |
179 | 179 |
|
180 | 180 |
} |
181 | 181 |
|
182 | 182 |
#endif |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_EDGE_SET_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_EDGE_SET_EXTENDER_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
#include <lemon/error.h> |
24 | 24 |
#include <lemon/bits/default_map.h> |
25 | 25 |
#include <lemon/bits/map_extender.h> |
26 | 26 |
|
27 | 27 |
//\ingroup digraphbits |
28 | 28 |
//\file |
29 | 29 |
//\brief Extenders for the arc set types |
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 | 32 |
// \ingroup digraphbits |
33 | 33 |
// |
34 | 34 |
// \brief Extender for the ArcSets |
35 | 35 |
template <typename Base> |
36 | 36 |
class ArcSetExtender : public Base { |
37 | 37 |
typedef Base Parent; |
38 | 38 |
|
39 | 39 |
public: |
40 | 40 |
|
41 | 41 |
typedef ArcSetExtender Digraph; |
42 | 42 |
|
43 | 43 |
// Base extensions |
44 | 44 |
|
45 | 45 |
typedef typename Parent::Node Node; |
46 | 46 |
typedef typename Parent::Arc Arc; |
47 | 47 |
|
48 | 48 |
int maxId(Node) const { |
49 | 49 |
return Parent::maxNodeId(); |
50 | 50 |
} |
51 | 51 |
|
52 | 52 |
int maxId(Arc) const { |
53 | 53 |
return Parent::maxArcId(); |
54 | 54 |
} |
55 | 55 |
|
56 | 56 |
Node fromId(int id, Node) const { |
57 | 57 |
return Parent::nodeFromId(id); |
58 | 58 |
} |
59 | 59 |
|
60 | 60 |
Arc fromId(int id, Arc) const { |
61 | 61 |
return Parent::arcFromId(id); |
62 | 62 |
} |
63 | 63 |
|
64 | 64 |
Node oppositeNode(const Node &n, const Arc &e) const { |
65 | 65 |
if (n == Parent::source(e)) |
66 |
|
|
66 |
return Parent::target(e); |
|
67 | 67 |
else if(n==Parent::target(e)) |
68 |
|
|
68 |
return Parent::source(e); |
|
69 | 69 |
else |
70 |
|
|
70 |
return INVALID; |
|
71 | 71 |
} |
72 | 72 |
|
73 | 73 |
|
74 | 74 |
// Alteration notifier extensions |
75 | 75 |
|
76 | 76 |
// The arc observer registry. |
77 | 77 |
typedef AlterationNotifier<ArcSetExtender, Arc> ArcNotifier; |
78 | 78 |
|
79 | 79 |
protected: |
80 | 80 |
|
81 | 81 |
mutable ArcNotifier arc_notifier; |
82 | 82 |
|
83 | 83 |
public: |
84 | 84 |
|
85 | 85 |
using Parent::notifier; |
86 | 86 |
|
87 | 87 |
// Gives back the arc alteration notifier. |
88 | 88 |
ArcNotifier& notifier(Arc) const { |
89 | 89 |
return arc_notifier; |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
// Iterable extensions |
93 | 93 |
|
94 |
class NodeIt : public Node { |
|
94 |
class NodeIt : public Node { |
|
95 | 95 |
const Digraph* digraph; |
96 | 96 |
public: |
97 | 97 |
|
98 | 98 |
NodeIt() {} |
99 | 99 |
|
100 | 100 |
NodeIt(Invalid i) : Node(i) { } |
101 | 101 |
|
102 | 102 |
explicit NodeIt(const Digraph& _graph) : digraph(&_graph) { |
103 |
|
|
103 |
_graph.first(static_cast<Node&>(*this)); |
|
104 | 104 |
} |
105 | 105 |
|
106 |
NodeIt(const Digraph& _graph, const Node& node) |
|
107 |
: Node(node), digraph(&_graph) {} |
|
106 |
NodeIt(const Digraph& _graph, const Node& node) |
|
107 |
: Node(node), digraph(&_graph) {} |
|
108 | 108 |
|
109 |
NodeIt& operator++() { |
|
110 |
digraph->next(*this); |
|
111 |
|
|
109 |
NodeIt& operator++() { |
|
110 |
digraph->next(*this); |
|
111 |
return *this; |
|
112 | 112 |
} |
113 | 113 |
|
114 | 114 |
}; |
115 | 115 |
|
116 | 116 |
|
117 |
class ArcIt : public Arc { |
|
117 |
class ArcIt : public Arc { |
|
118 | 118 |
const Digraph* digraph; |
119 | 119 |
public: |
120 | 120 |
|
121 | 121 |
ArcIt() { } |
122 | 122 |
|
123 | 123 |
ArcIt(Invalid i) : Arc(i) { } |
124 | 124 |
|
125 | 125 |
explicit ArcIt(const Digraph& _graph) : digraph(&_graph) { |
126 |
|
|
126 |
_graph.first(static_cast<Arc&>(*this)); |
|
127 | 127 |
} |
128 | 128 |
|
129 |
ArcIt(const Digraph& _graph, const Arc& e) : |
|
130 |
Arc(e), digraph(&_graph) { } |
|
129 |
ArcIt(const Digraph& _graph, const Arc& e) : |
|
130 |
Arc(e), digraph(&_graph) { } |
|
131 | 131 |
|
132 |
ArcIt& operator++() { |
|
133 |
digraph->next(*this); |
|
134 |
|
|
132 |
ArcIt& operator++() { |
|
133 |
digraph->next(*this); |
|
134 |
return *this; |
|
135 | 135 |
} |
136 | 136 |
|
137 | 137 |
}; |
138 | 138 |
|
139 | 139 |
|
140 |
class OutArcIt : public Arc { |
|
140 |
class OutArcIt : public Arc { |
|
141 | 141 |
const Digraph* digraph; |
142 | 142 |
public: |
143 | 143 |
|
144 | 144 |
OutArcIt() { } |
145 | 145 |
|
146 | 146 |
OutArcIt(Invalid i) : Arc(i) { } |
147 | 147 |
|
148 |
OutArcIt(const Digraph& _graph, const Node& node) |
|
149 |
: digraph(&_graph) { |
|
150 |
|
|
148 |
OutArcIt(const Digraph& _graph, const Node& node) |
|
149 |
: digraph(&_graph) { |
|
150 |
_graph.firstOut(*this, node); |
|
151 | 151 |
} |
152 | 152 |
|
153 |
OutArcIt(const Digraph& _graph, const Arc& arc) |
|
154 |
: Arc(arc), digraph(&_graph) {} |
|
153 |
OutArcIt(const Digraph& _graph, const Arc& arc) |
|
154 |
: Arc(arc), digraph(&_graph) {} |
|
155 | 155 |
|
156 |
OutArcIt& operator++() { |
|
157 |
digraph->nextOut(*this); |
|
158 |
|
|
156 |
OutArcIt& operator++() { |
|
157 |
digraph->nextOut(*this); |
|
158 |
return *this; |
|
159 | 159 |
} |
160 | 160 |
|
161 | 161 |
}; |
162 | 162 |
|
163 | 163 |
|
164 |
class InArcIt : public Arc { |
|
164 |
class InArcIt : public Arc { |
|
165 | 165 |
const Digraph* digraph; |
166 | 166 |
public: |
167 | 167 |
|
168 | 168 |
InArcIt() { } |
169 | 169 |
|
170 | 170 |
InArcIt(Invalid i) : Arc(i) { } |
171 | 171 |
|
172 |
InArcIt(const Digraph& _graph, const Node& node) |
|
173 |
: digraph(&_graph) { |
|
174 |
|
|
172 |
InArcIt(const Digraph& _graph, const Node& node) |
|
173 |
: digraph(&_graph) { |
|
174 |
_graph.firstIn(*this, node); |
|
175 | 175 |
} |
176 | 176 |
|
177 |
InArcIt(const Digraph& _graph, const Arc& arc) : |
|
178 |
Arc(arc), digraph(&_graph) {} |
|
177 |
InArcIt(const Digraph& _graph, const Arc& arc) : |
|
178 |
Arc(arc), digraph(&_graph) {} |
|
179 | 179 |
|
180 |
InArcIt& operator++() { |
|
181 |
digraph->nextIn(*this); |
|
182 |
|
|
180 |
InArcIt& operator++() { |
|
181 |
digraph->nextIn(*this); |
|
182 |
return *this; |
|
183 | 183 |
} |
184 | 184 |
|
185 | 185 |
}; |
186 | 186 |
|
187 | 187 |
// \brief Base node of the iterator |
188 | 188 |
// |
189 | 189 |
// Returns the base node (ie. the source in this case) of the iterator |
190 | 190 |
Node baseNode(const OutArcIt &e) const { |
191 | 191 |
return Parent::source(static_cast<const Arc&>(e)); |
192 | 192 |
} |
193 | 193 |
// \brief Running node of the iterator |
194 | 194 |
// |
195 | 195 |
// Returns the running node (ie. the target in this case) of the |
196 | 196 |
// iterator |
197 | 197 |
Node runningNode(const OutArcIt &e) const { |
198 | 198 |
return Parent::target(static_cast<const Arc&>(e)); |
199 | 199 |
} |
200 | 200 |
|
201 | 201 |
// \brief Base node of the iterator |
202 | 202 |
// |
203 | 203 |
// Returns the base node (ie. the target in this case) of the iterator |
204 | 204 |
Node baseNode(const InArcIt &e) const { |
205 | 205 |
return Parent::target(static_cast<const Arc&>(e)); |
206 | 206 |
} |
207 | 207 |
// \brief Running node of the iterator |
208 | 208 |
// |
209 | 209 |
// Returns the running node (ie. the source in this case) of the |
210 | 210 |
// iterator |
211 | 211 |
Node runningNode(const InArcIt &e) const { |
212 | 212 |
return Parent::source(static_cast<const Arc&>(e)); |
213 | 213 |
} |
214 | 214 |
|
215 | 215 |
using Parent::first; |
216 | 216 |
|
217 | 217 |
// Mappable extension |
218 |
|
|
218 |
|
|
219 | 219 |
template <typename _Value> |
220 |
class ArcMap |
|
220 |
class ArcMap |
|
221 | 221 |
: public MapExtender<DefaultMap<Digraph, Arc, _Value> > { |
222 | 222 |
typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent; |
223 | 223 |
|
224 | 224 |
public: |
225 |
explicit ArcMap(const Digraph& _g) |
|
226 |
: Parent(_g) {} |
|
227 |
ArcMap(const Digraph& _g, const _Value& _v) |
|
228 |
: Parent(_g, _v) {} |
|
225 |
explicit ArcMap(const Digraph& _g) |
|
226 |
: Parent(_g) {} |
|
227 |
ArcMap(const Digraph& _g, const _Value& _v) |
|
228 |
: Parent(_g, _v) {} |
|
229 | 229 |
|
230 | 230 |
ArcMap& operator=(const ArcMap& cmap) { |
231 |
|
|
231 |
return operator=<ArcMap>(cmap); |
|
232 | 232 |
} |
233 | 233 |
|
234 | 234 |
template <typename CMap> |
235 | 235 |
ArcMap& operator=(const CMap& cmap) { |
236 | 236 |
Parent::operator=(cmap); |
237 |
|
|
237 |
return *this; |
|
238 | 238 |
} |
239 | 239 |
|
240 | 240 |
}; |
241 | 241 |
|
242 | 242 |
|
243 | 243 |
// Alteration extension |
244 | 244 |
|
245 | 245 |
Arc addArc(const Node& from, const Node& to) { |
246 | 246 |
Arc arc = Parent::addArc(from, to); |
247 | 247 |
notifier(Arc()).add(arc); |
248 | 248 |
return arc; |
249 | 249 |
} |
250 |
|
|
250 |
|
|
251 | 251 |
void clear() { |
252 | 252 |
notifier(Arc()).clear(); |
253 | 253 |
Parent::clear(); |
254 | 254 |
} |
255 | 255 |
|
256 | 256 |
void erase(const Arc& arc) { |
257 | 257 |
notifier(Arc()).erase(arc); |
258 | 258 |
Parent::erase(arc); |
259 | 259 |
} |
260 | 260 |
|
261 | 261 |
ArcSetExtender() { |
262 | 262 |
arc_notifier.setContainer(*this); |
263 | 263 |
} |
264 | 264 |
|
265 | 265 |
~ArcSetExtender() { |
266 | 266 |
arc_notifier.clear(); |
267 | 267 |
} |
268 | 268 |
|
269 | 269 |
}; |
270 | 270 |
|
271 | 271 |
|
272 | 272 |
// \ingroup digraphbits |
273 | 273 |
// |
274 | 274 |
// \brief Extender for the EdgeSets |
275 | 275 |
template <typename Base> |
276 | 276 |
class EdgeSetExtender : public Base { |
277 | 277 |
typedef Base Parent; |
278 | 278 |
|
279 | 279 |
public: |
280 | 280 |
|
281 | 281 |
typedef EdgeSetExtender Graph; |
282 | 282 |
|
283 | 283 |
typedef True UndirectedTag; |
284 | 284 |
|
285 | 285 |
typedef typename Parent::Node Node; |
286 | 286 |
typedef typename Parent::Arc Arc; |
287 | 287 |
typedef typename Parent::Edge Edge; |
288 | 288 |
|
289 | 289 |
int maxId(Node) const { |
290 | 290 |
return Parent::maxNodeId(); |
291 | 291 |
} |
292 | 292 |
|
293 | 293 |
int maxId(Arc) const { |
294 | 294 |
return Parent::maxArcId(); |
295 | 295 |
} |
296 | 296 |
|
297 | 297 |
int maxId(Edge) const { |
298 | 298 |
return Parent::maxEdgeId(); |
299 | 299 |
} |
300 | 300 |
|
301 | 301 |
Node fromId(int id, Node) const { |
302 | 302 |
return Parent::nodeFromId(id); |
303 | 303 |
} |
304 | 304 |
|
305 | 305 |
Arc fromId(int id, Arc) const { |
306 | 306 |
return Parent::arcFromId(id); |
307 | 307 |
} |
308 | 308 |
|
309 | 309 |
Edge fromId(int id, Edge) const { |
310 | 310 |
return Parent::edgeFromId(id); |
311 | 311 |
} |
312 | 312 |
|
313 | 313 |
Node oppositeNode(const Node &n, const Edge &e) const { |
314 | 314 |
if( n == Parent::u(e)) |
315 |
|
|
315 |
return Parent::v(e); |
|
316 | 316 |
else if( n == Parent::v(e)) |
317 |
|
|
317 |
return Parent::u(e); |
|
318 | 318 |
else |
319 |
|
|
319 |
return INVALID; |
|
320 | 320 |
} |
321 | 321 |
|
322 | 322 |
Arc oppositeArc(const Arc &e) const { |
323 | 323 |
return Parent::direct(e, !Parent::direction(e)); |
324 | 324 |
} |
325 | 325 |
|
326 | 326 |
using Parent::direct; |
327 | 327 |
Arc direct(const Edge &e, const Node &s) const { |
328 | 328 |
return Parent::direct(e, Parent::u(e) == s); |
329 | 329 |
} |
330 | 330 |
|
331 | 331 |
typedef AlterationNotifier<EdgeSetExtender, Arc> ArcNotifier; |
332 | 332 |
typedef AlterationNotifier<EdgeSetExtender, Edge> EdgeNotifier; |
333 | 333 |
|
334 | 334 |
|
335 | 335 |
protected: |
336 | 336 |
|
337 | 337 |
mutable ArcNotifier arc_notifier; |
338 | 338 |
mutable EdgeNotifier edge_notifier; |
339 | 339 |
|
340 | 340 |
public: |
341 | 341 |
|
342 | 342 |
using Parent::notifier; |
343 |
|
|
343 |
|
|
344 | 344 |
ArcNotifier& notifier(Arc) const { |
345 | 345 |
return arc_notifier; |
346 | 346 |
} |
347 | 347 |
|
348 | 348 |
EdgeNotifier& notifier(Edge) const { |
349 | 349 |
return edge_notifier; |
350 | 350 |
} |
351 | 351 |
|
352 | 352 |
|
353 |
class NodeIt : public Node { |
|
353 |
class NodeIt : public Node { |
|
354 | 354 |
const Graph* graph; |
355 | 355 |
public: |
356 | 356 |
|
357 | 357 |
NodeIt() {} |
358 | 358 |
|
359 | 359 |
NodeIt(Invalid i) : Node(i) { } |
360 | 360 |
|
361 | 361 |
explicit NodeIt(const Graph& _graph) : graph(&_graph) { |
362 |
|
|
362 |
_graph.first(static_cast<Node&>(*this)); |
|
363 | 363 |
} |
364 | 364 |
|
365 |
NodeIt(const Graph& _graph, const Node& node) |
|
366 |
: Node(node), graph(&_graph) {} |
|
365 |
NodeIt(const Graph& _graph, const Node& node) |
|
366 |
: Node(node), graph(&_graph) {} |
|
367 | 367 |
|
368 |
NodeIt& operator++() { |
|
369 |
graph->next(*this); |
|
370 |
|
|
368 |
NodeIt& operator++() { |
|
369 |
graph->next(*this); |
|
370 |
return *this; |
|
371 | 371 |
} |
372 | 372 |
|
373 | 373 |
}; |
374 | 374 |
|
375 | 375 |
|
376 |
class ArcIt : public Arc { |
|
376 |
class ArcIt : public Arc { |
|
377 | 377 |
const Graph* graph; |
378 | 378 |
public: |
379 | 379 |
|
380 | 380 |
ArcIt() { } |
381 | 381 |
|
382 | 382 |
ArcIt(Invalid i) : Arc(i) { } |
383 | 383 |
|
384 | 384 |
explicit ArcIt(const Graph& _graph) : graph(&_graph) { |
385 |
|
|
385 |
_graph.first(static_cast<Arc&>(*this)); |
|
386 | 386 |
} |
387 | 387 |
|
388 |
ArcIt(const Graph& _graph, const Arc& e) : |
|
389 |
Arc(e), graph(&_graph) { } |
|
388 |
ArcIt(const Graph& _graph, const Arc& e) : |
|
389 |
Arc(e), graph(&_graph) { } |
|
390 | 390 |
|
391 |
ArcIt& operator++() { |
|
392 |
graph->next(*this); |
|
393 |
|
|
391 |
ArcIt& operator++() { |
|
392 |
graph->next(*this); |
|
393 |
return *this; |
|
394 | 394 |
} |
395 | 395 |
|
396 | 396 |
}; |
397 | 397 |
|
398 | 398 |
|
399 |
class OutArcIt : public Arc { |
|
399 |
class OutArcIt : public Arc { |
|
400 | 400 |
const Graph* graph; |
401 | 401 |
public: |
402 | 402 |
|
403 | 403 |
OutArcIt() { } |
404 | 404 |
|
405 | 405 |
OutArcIt(Invalid i) : Arc(i) { } |
406 | 406 |
|
407 |
OutArcIt(const Graph& _graph, const Node& node) |
|
408 |
: graph(&_graph) { |
|
409 |
|
|
407 |
OutArcIt(const Graph& _graph, const Node& node) |
|
408 |
: graph(&_graph) { |
|
409 |
_graph.firstOut(*this, node); |
|
410 | 410 |
} |
411 | 411 |
|
412 |
OutArcIt(const Graph& _graph, const Arc& arc) |
|
413 |
: Arc(arc), graph(&_graph) {} |
|
412 |
OutArcIt(const Graph& _graph, const Arc& arc) |
|
413 |
: Arc(arc), graph(&_graph) {} |
|
414 | 414 |
|
415 |
OutArcIt& operator++() { |
|
416 |
graph->nextOut(*this); |
|
417 |
|
|
415 |
OutArcIt& operator++() { |
|
416 |
graph->nextOut(*this); |
|
417 |
return *this; |
|
418 | 418 |
} |
419 | 419 |
|
420 | 420 |
}; |
421 | 421 |
|
422 | 422 |
|
423 |
class InArcIt : public Arc { |
|
423 |
class InArcIt : public Arc { |
|
424 | 424 |
const Graph* graph; |
425 | 425 |
public: |
426 | 426 |
|
427 | 427 |
InArcIt() { } |
428 | 428 |
|
429 | 429 |
InArcIt(Invalid i) : Arc(i) { } |
430 | 430 |
|
431 |
InArcIt(const Graph& _graph, const Node& node) |
|
432 |
: graph(&_graph) { |
|
433 |
|
|
431 |
InArcIt(const Graph& _graph, const Node& node) |
|
432 |
: graph(&_graph) { |
|
433 |
_graph.firstIn(*this, node); |
|
434 | 434 |
} |
435 | 435 |
|
436 |
InArcIt(const Graph& _graph, const Arc& arc) : |
|
437 |
Arc(arc), graph(&_graph) {} |
|
436 |
InArcIt(const Graph& _graph, const Arc& arc) : |
|
437 |
Arc(arc), graph(&_graph) {} |
|
438 | 438 |
|
439 |
InArcIt& operator++() { |
|
440 |
graph->nextIn(*this); |
|
441 |
|
|
439 |
InArcIt& operator++() { |
|
440 |
graph->nextIn(*this); |
|
441 |
return *this; |
|
442 | 442 |
} |
443 | 443 |
|
444 | 444 |
}; |
445 | 445 |
|
446 | 446 |
|
447 |
class EdgeIt : public Parent::Edge { |
|
447 |
class EdgeIt : public Parent::Edge { |
|
448 | 448 |
const Graph* graph; |
449 | 449 |
public: |
450 | 450 |
|
451 | 451 |
EdgeIt() { } |
452 | 452 |
|
453 | 453 |
EdgeIt(Invalid i) : Edge(i) { } |
454 | 454 |
|
455 | 455 |
explicit EdgeIt(const Graph& _graph) : graph(&_graph) { |
456 |
|
|
456 |
_graph.first(static_cast<Edge&>(*this)); |
|
457 | 457 |
} |
458 | 458 |
|
459 |
EdgeIt(const Graph& _graph, const Edge& e) : |
|
460 |
Edge(e), graph(&_graph) { } |
|
459 |
EdgeIt(const Graph& _graph, const Edge& e) : |
|
460 |
Edge(e), graph(&_graph) { } |
|
461 | 461 |
|
462 |
EdgeIt& operator++() { |
|
463 |
graph->next(*this); |
|
464 |
|
|
462 |
EdgeIt& operator++() { |
|
463 |
graph->next(*this); |
|
464 |
return *this; |
|
465 | 465 |
} |
466 | 466 |
|
467 | 467 |
}; |
468 | 468 |
|
469 | 469 |
class IncEdgeIt : public Parent::Edge { |
470 | 470 |
friend class EdgeSetExtender; |
471 | 471 |
const Graph* graph; |
472 | 472 |
bool direction; |
473 | 473 |
public: |
474 | 474 |
|
475 | 475 |
IncEdgeIt() { } |
476 | 476 |
|
477 | 477 |
IncEdgeIt(Invalid i) : Edge(i), direction(false) { } |
478 | 478 |
|
479 | 479 |
IncEdgeIt(const Graph& _graph, const Node &n) : graph(&_graph) { |
480 |
|
|
480 |
_graph.firstInc(*this, direction, n); |
|
481 | 481 |
} |
482 | 482 |
|
483 | 483 |
IncEdgeIt(const Graph& _graph, const Edge &ue, const Node &n) |
484 |
: graph(&_graph), Edge(ue) { |
|
485 |
direction = (_graph.source(ue) == n); |
|
484 |
: graph(&_graph), Edge(ue) { |
|
485 |
direction = (_graph.source(ue) == n); |
|
486 | 486 |
} |
487 | 487 |
|
488 | 488 |
IncEdgeIt& operator++() { |
489 |
graph->nextInc(*this, direction); |
|
490 |
return *this; |
|
489 |
graph->nextInc(*this, direction); |
|
490 |
return *this; |
|
491 | 491 |
} |
492 | 492 |
}; |
493 | 493 |
|
494 | 494 |
// \brief Base node of the iterator |
495 | 495 |
// |
496 | 496 |
// Returns the base node (ie. the source in this case) of the iterator |
497 | 497 |
Node baseNode(const OutArcIt &e) const { |
498 | 498 |
return Parent::source(static_cast<const Arc&>(e)); |
499 | 499 |
} |
500 | 500 |
// \brief Running node of the iterator |
501 | 501 |
// |
502 | 502 |
// Returns the running node (ie. the target in this case) of the |
503 | 503 |
// iterator |
504 | 504 |
Node runningNode(const OutArcIt &e) const { |
505 | 505 |
return Parent::target(static_cast<const Arc&>(e)); |
506 | 506 |
} |
507 | 507 |
|
508 | 508 |
// \brief Base node of the iterator |
509 | 509 |
// |
510 | 510 |
// Returns the base node (ie. the target in this case) of the iterator |
511 | 511 |
Node baseNode(const InArcIt &e) const { |
512 | 512 |
return Parent::target(static_cast<const Arc&>(e)); |
513 | 513 |
} |
514 | 514 |
// \brief Running node of the iterator |
515 | 515 |
// |
516 | 516 |
// Returns the running node (ie. the source in this case) of the |
517 | 517 |
// iterator |
518 | 518 |
Node runningNode(const InArcIt &e) const { |
519 | 519 |
return Parent::source(static_cast<const Arc&>(e)); |
520 | 520 |
} |
521 | 521 |
|
522 | 522 |
// Base node of the iterator |
523 | 523 |
// |
524 | 524 |
// Returns the base node of the iterator |
525 | 525 |
Node baseNode(const IncEdgeIt &e) const { |
526 | 526 |
return e.direction ? u(e) : v(e); |
527 | 527 |
} |
528 | 528 |
// Running node of the iterator |
529 | 529 |
// |
530 | 530 |
// Returns the running node of the iterator |
531 | 531 |
Node runningNode(const IncEdgeIt &e) const { |
532 | 532 |
return e.direction ? v(e) : u(e); |
533 | 533 |
} |
534 | 534 |
|
535 | 535 |
|
536 | 536 |
template <typename _Value> |
537 |
class ArcMap |
|
537 |
class ArcMap |
|
538 | 538 |
: public MapExtender<DefaultMap<Graph, Arc, _Value> > { |
539 | 539 |
typedef MapExtender<DefaultMap<Graph, Arc, _Value> > Parent; |
540 | 540 |
|
541 | 541 |
public: |
542 |
explicit ArcMap(const Graph& _g) |
|
543 |
: Parent(_g) {} |
|
544 |
ArcMap(const Graph& _g, const _Value& _v) |
|
545 |
: Parent(_g, _v) {} |
|
542 |
explicit ArcMap(const Graph& _g) |
|
543 |
: Parent(_g) {} |
|
544 |
ArcMap(const Graph& _g, const _Value& _v) |
|
545 |
: Parent(_g, _v) {} |
|
546 | 546 |
|
547 | 547 |
ArcMap& operator=(const ArcMap& cmap) { |
548 |
|
|
548 |
return operator=<ArcMap>(cmap); |
|
549 | 549 |
} |
550 | 550 |
|
551 | 551 |
template <typename CMap> |
552 | 552 |
ArcMap& operator=(const CMap& cmap) { |
553 | 553 |
Parent::operator=(cmap); |
554 |
|
|
554 |
return *this; |
|
555 | 555 |
} |
556 | 556 |
|
557 | 557 |
}; |
558 | 558 |
|
559 | 559 |
|
560 | 560 |
template <typename _Value> |
561 |
class EdgeMap |
|
561 |
class EdgeMap |
|
562 | 562 |
: public MapExtender<DefaultMap<Graph, Edge, _Value> > { |
563 | 563 |
typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent; |
564 | 564 |
|
565 | 565 |
public: |
566 |
explicit EdgeMap(const Graph& _g) |
|
567 |
: Parent(_g) {} |
|
566 |
explicit EdgeMap(const Graph& _g) |
|
567 |
: Parent(_g) {} |
|
568 | 568 |
|
569 |
EdgeMap(const Graph& _g, const _Value& _v) |
|
570 |
: Parent(_g, _v) {} |
|
569 |
EdgeMap(const Graph& _g, const _Value& _v) |
|
570 |
: Parent(_g, _v) {} |
|
571 | 571 |
|
572 | 572 |
EdgeMap& operator=(const EdgeMap& cmap) { |
573 |
|
|
573 |
return operator=<EdgeMap>(cmap); |
|
574 | 574 |
} |
575 | 575 |
|
576 | 576 |
template <typename CMap> |
577 | 577 |
EdgeMap& operator=(const CMap& cmap) { |
578 | 578 |
Parent::operator=(cmap); |
579 |
|
|
579 |
return *this; |
|
580 | 580 |
} |
581 | 581 |
|
582 | 582 |
}; |
583 | 583 |
|
584 | 584 |
|
585 | 585 |
// Alteration extension |
586 | 586 |
|
587 | 587 |
Edge addEdge(const Node& from, const Node& to) { |
588 | 588 |
Edge edge = Parent::addEdge(from, to); |
589 | 589 |
notifier(Edge()).add(edge); |
590 | 590 |
std::vector<Arc> arcs; |
591 | 591 |
arcs.push_back(Parent::direct(edge, true)); |
592 | 592 |
arcs.push_back(Parent::direct(edge, false)); |
593 | 593 |
notifier(Arc()).add(arcs); |
594 | 594 |
return edge; |
595 | 595 |
} |
596 |
|
|
596 |
|
|
597 | 597 |
void clear() { |
598 | 598 |
notifier(Arc()).clear(); |
599 | 599 |
notifier(Edge()).clear(); |
600 | 600 |
Parent::clear(); |
601 | 601 |
} |
602 | 602 |
|
603 | 603 |
void erase(const Edge& edge) { |
604 | 604 |
std::vector<Arc> arcs; |
605 | 605 |
arcs.push_back(Parent::direct(edge, true)); |
606 | 606 |
arcs.push_back(Parent::direct(edge, false)); |
607 | 607 |
notifier(Arc()).erase(arcs); |
608 | 608 |
notifier(Edge()).erase(edge); |
609 | 609 |
Parent::erase(edge); |
610 | 610 |
} |
611 | 611 |
|
612 | 612 |
|
613 | 613 |
EdgeSetExtender() { |
614 | 614 |
arc_notifier.setContainer(*this); |
615 | 615 |
edge_notifier.setContainer(*this); |
616 | 616 |
} |
617 | 617 |
|
618 | 618 |
~EdgeSetExtender() { |
619 | 619 |
edge_notifier.clear(); |
620 | 620 |
arc_notifier.clear(); |
621 | 621 |
} |
622 |
|
|
622 |
|
|
623 | 623 |
}; |
624 | 624 |
|
625 | 625 |
} |
626 | 626 |
|
627 | 627 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
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_GRAPH_ADAPTOR_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_GRAPH_ADAPTOR_EXTENDER_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
#include <lemon/error.h> |
24 | 24 |
|
25 | 25 |
namespace lemon { |
26 | 26 |
|
27 | 27 |
template <typename _Digraph> |
28 | 28 |
class DigraphAdaptorExtender : public _Digraph { |
29 | 29 |
typedef _Digraph Parent; |
30 | 30 |
|
31 | 31 |
public: |
32 | 32 |
|
33 | 33 |
typedef _Digraph Digraph; |
34 | 34 |
typedef DigraphAdaptorExtender Adaptor; |
35 | 35 |
|
36 | 36 |
// Base extensions |
37 | 37 |
|
38 | 38 |
typedef typename Parent::Node Node; |
39 | 39 |
typedef typename Parent::Arc Arc; |
40 | 40 |
|
41 | 41 |
int maxId(Node) const { |
42 | 42 |
return Parent::maxNodeId(); |
43 | 43 |
} |
44 | 44 |
|
45 | 45 |
int maxId(Arc) const { |
46 | 46 |
return Parent::maxArcId(); |
47 | 47 |
} |
48 | 48 |
|
49 | 49 |
Node fromId(int id, Node) const { |
50 | 50 |
return Parent::nodeFromId(id); |
51 | 51 |
} |
52 | 52 |
|
53 | 53 |
Arc fromId(int id, Arc) const { |
54 | 54 |
return Parent::arcFromId(id); |
55 | 55 |
} |
56 | 56 |
|
57 | 57 |
Node oppositeNode(const Node &n, const Arc &e) const { |
58 | 58 |
if (n == Parent::source(e)) |
59 | 59 |
return Parent::target(e); |
60 | 60 |
else if(n==Parent::target(e)) |
61 | 61 |
return Parent::source(e); |
62 | 62 |
else |
63 | 63 |
return INVALID; |
64 | 64 |
} |
65 | 65 |
|
66 | 66 |
class NodeIt : public Node { |
67 | 67 |
const Adaptor* _adaptor; |
68 | 68 |
public: |
69 | 69 |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
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_MAP_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_MAP_EXTENDER_H |
21 | 21 |
|
22 | 22 |
#include <iterator> |
23 | 23 |
|
24 | 24 |
#include <lemon/bits/traits.h> |
25 | 25 |
|
26 | 26 |
#include <lemon/concept_check.h> |
27 | 27 |
#include <lemon/concepts/maps.h> |
28 | 28 |
|
29 | 29 |
//\file |
30 | 30 |
//\brief Extenders for iterable maps. |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
// \ingroup graphbits |
35 | 35 |
// |
36 | 36 |
// \brief Extender for maps |
37 | 37 |
template <typename _Map> |
38 | 38 |
class MapExtender : public _Map { |
39 | 39 |
typedef _Map Parent; |
40 | 40 |
typedef typename Parent::GraphType GraphType; |
41 | 41 |
|
42 | 42 |
public: |
43 | 43 |
|
44 | 44 |
typedef MapExtender Map; |
45 | 45 |
typedef typename Parent::Key Item; |
46 | 46 |
|
47 | 47 |
typedef typename Parent::Key Key; |
48 | 48 |
typedef typename Parent::Value Value; |
49 | 49 |
typedef typename Parent::Reference Reference; |
50 | 50 |
typedef typename Parent::ConstReference ConstReference; |
51 | 51 |
|
52 | 52 |
typedef typename Parent::ReferenceMapTag ReferenceMapTag; |
53 | 53 |
|
54 | 54 |
class MapIt; |
55 | 55 |
class ConstMapIt; |
56 | 56 |
|
57 | 57 |
friend class MapIt; |
58 | 58 |
friend class ConstMapIt; |
59 | 59 |
|
60 | 60 |
public: |
61 | 61 |
|
62 | 62 |
MapExtender(const GraphType& graph) |
63 | 63 |
: Parent(graph) {} |
64 | 64 |
|
65 | 65 |
MapExtender(const GraphType& graph, const Value& value) |
66 | 66 |
: Parent(graph, value) {} |
67 | 67 |
|
68 | 68 |
private: |
69 | 69 |
MapExtender& operator=(const MapExtender& cmap) { |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
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_PATH_DUMP_H |
20 | 20 |
#define LEMON_BITS_PATH_DUMP_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
#include <lemon/concept_check.h> |
24 | 24 |
|
25 | 25 |
namespace lemon { |
26 | 26 |
|
27 | 27 |
template <typename _Digraph, typename _PredMap> |
28 | 28 |
class PredMapPath { |
29 | 29 |
public: |
30 | 30 |
typedef True RevPathTag; |
31 | 31 |
|
32 | 32 |
typedef _Digraph Digraph; |
33 | 33 |
typedef typename Digraph::Arc Arc; |
34 | 34 |
typedef _PredMap PredMap; |
35 | 35 |
|
36 | 36 |
PredMapPath(const Digraph& _digraph, const PredMap& _predMap, |
37 | 37 |
typename Digraph::Node _target) |
38 | 38 |
: digraph(_digraph), predMap(_predMap), target(_target) {} |
39 | 39 |
|
40 | 40 |
int length() const { |
41 | 41 |
int len = 0; |
42 | 42 |
typename Digraph::Node node = target; |
43 | 43 |
typename Digraph::Arc arc; |
44 | 44 |
while ((arc = predMap[node]) != INVALID) { |
45 | 45 |
node = digraph.source(arc); |
46 | 46 |
++len; |
47 | 47 |
} |
48 | 48 |
return len; |
49 | 49 |
} |
50 | 50 |
|
51 | 51 |
bool empty() const { |
52 | 52 |
return predMap[target] == INVALID; |
53 | 53 |
} |
54 | 54 |
|
55 | 55 |
class RevArcIt { |
56 | 56 |
public: |
57 | 57 |
RevArcIt() {} |
58 | 58 |
RevArcIt(Invalid) : path(0), current(INVALID) {} |
59 | 59 |
RevArcIt(const PredMapPath& _path) |
60 | 60 |
: path(&_path), current(_path.target) { |
61 | 61 |
if (path->predMap[current] == INVALID) current = INVALID; |
62 | 62 |
} |
63 | 63 |
|
64 | 64 |
operator const typename Digraph::Arc() const { |
65 | 65 |
return path->predMap[current]; |
66 | 66 |
} |
67 | 67 |
|
68 | 68 |
RevArcIt& operator++() { |
69 | 69 |
current = path->digraph.source(path->predMap[current]); |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_SOLVER_BITS_H |
20 | 20 |
#define LEMON_BITS_SOLVER_BITS_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
|
24 | 24 |
namespace lemon { |
25 | 25 |
|
26 | 26 |
namespace _solver_bits { |
27 | 27 |
|
28 | 28 |
class VarIndex { |
29 | 29 |
private: |
30 | 30 |
struct ItemT { |
31 | 31 |
int prev, next; |
32 | 32 |
int index; |
33 | 33 |
}; |
34 | 34 |
std::vector<ItemT> items; |
35 | 35 |
int first_item, last_item, first_free_item; |
36 | 36 |
|
37 | 37 |
std::vector<int> cross; |
38 | 38 |
|
39 | 39 |
public: |
40 | 40 |
|
41 | 41 |
VarIndex() |
42 | 42 |
: first_item(-1), last_item(-1), first_free_item(-1) { |
43 | 43 |
} |
44 | 44 |
|
45 | 45 |
void clear() { |
46 | 46 |
first_item = -1; |
47 | 47 |
first_free_item = -1; |
48 | 48 |
items.clear(); |
49 | 49 |
cross.clear(); |
50 | 50 |
} |
51 | 51 |
|
52 | 52 |
int addIndex(int idx) { |
53 | 53 |
int n; |
54 | 54 |
if (first_free_item == -1) { |
55 | 55 |
n = items.size(); |
56 | 56 |
items.push_back(ItemT()); |
57 | 57 |
} else { |
58 | 58 |
n = first_free_item; |
59 | 59 |
first_free_item = items[n].next; |
60 | 60 |
if (first_free_item != -1) { |
61 | 61 |
items[first_free_item].prev = -1; |
62 | 62 |
} |
63 | 63 |
} |
64 | 64 |
items[n].index = idx; |
65 | 65 |
if (static_cast<int>(cross.size()) <= idx) { |
66 | 66 |
cross.resize(idx + 1, -1); |
67 | 67 |
} |
68 | 68 |
cross[idx] = n; |
69 | 69 |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\file |
20 | 20 |
///\brief Some basic non-inline functions and static global data. |
21 | 21 |
|
22 | 22 |
#include<lemon/bits/windows.h> |
23 | 23 |
|
24 | 24 |
#ifdef WIN32 |
25 | 25 |
#ifndef WIN32_LEAN_AND_MEAN |
26 | 26 |
#define WIN32_LEAN_AND_MEAN |
27 | 27 |
#endif |
28 | 28 |
#ifndef NOMINMAX |
29 | 29 |
#define NOMINMAX |
30 | 30 |
#endif |
31 | 31 |
#ifdef UNICODE |
32 | 32 |
#undef UNICODE |
33 | 33 |
#endif |
34 | 34 |
#include <windows.h> |
35 | 35 |
#ifdef LOCALE_INVARIANT |
36 | 36 |
#define MY_LOCALE LOCALE_INVARIANT |
37 | 37 |
#else |
38 | 38 |
#define MY_LOCALE LOCALE_NEUTRAL |
39 | 39 |
#endif |
40 | 40 |
#else |
41 | 41 |
#include <unistd.h> |
42 | 42 |
#include <ctime> |
43 | 43 |
#ifndef WIN32 |
44 | 44 |
#include <sys/times.h> |
45 | 45 |
#endif |
46 | 46 |
#include <sys/time.h> |
47 | 47 |
#endif |
48 | 48 |
|
49 | 49 |
#include <cmath> |
50 | 50 |
#include <sstream> |
51 | 51 |
|
52 | 52 |
namespace lemon { |
53 | 53 |
namespace bits { |
54 | 54 |
void getWinProcTimes(double &rtime, |
55 | 55 |
double &utime, double &stime, |
56 | 56 |
double &cutime, double &cstime) |
57 | 57 |
{ |
58 | 58 |
#ifdef WIN32 |
59 | 59 |
static const double ch = 4294967296.0e-7; |
60 | 60 |
static const double cl = 1.0e-7; |
61 | 61 |
|
62 | 62 |
FILETIME system; |
63 | 63 |
GetSystemTimeAsFileTime(&system); |
64 | 64 |
rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime; |
65 | 65 |
|
66 | 66 |
FILETIME create, exit, kernel, user; |
67 | 67 |
if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) { |
68 | 68 |
utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime; |
69 | 69 |
stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime; |
70 | 70 |
cutime = 0; |
71 | 71 |
cstime = 0; |
72 | 72 |
} else { |
73 | 73 |
rtime = 0; |
74 | 74 |
utime = 0; |
75 | 75 |
stime = 0; |
76 | 76 |
cutime = 0; |
77 | 77 |
cstime = 0; |
78 | 78 |
} |
79 | 79 |
#else |
80 | 80 |
timeval tv; |
81 | 81 |
gettimeofday(&tv, 0); |
82 | 82 |
rtime=tv.tv_sec+double(tv.tv_usec)/1e6; |
83 | 83 |
|
84 | 84 |
tms ts; |
85 | 85 |
double tck=sysconf(_SC_CLK_TCK); |
86 | 86 |
times(&ts); |
87 | 87 |
utime=ts.tms_utime/tck; |
88 | 88 |
stime=ts.tms_stime/tck; |
89 | 89 |
cutime=ts.tms_cutime/tck; |
90 | 90 |
cstime=ts.tms_cstime/tck; |
91 | 91 |
#endif |
92 | 92 |
} |
93 | 93 |
|
94 | 94 |
std::string getWinFormattedDate() |
95 | 95 |
{ |
96 | 96 |
std::ostringstream os; |
97 | 97 |
#ifdef WIN32 |
98 | 98 |
SYSTEMTIME time; |
99 | 99 |
GetSystemTime(&time); |
100 | 100 |
char buf1[11], buf2[9], buf3[5]; |
101 |
|
|
101 |
if (GetDateFormat(MY_LOCALE, 0, &time, |
|
102 | 102 |
("ddd MMM dd"), buf1, 11) && |
103 | 103 |
GetTimeFormat(MY_LOCALE, 0, &time, |
104 | 104 |
("HH':'mm':'ss"), buf2, 9) && |
105 | 105 |
GetDateFormat(MY_LOCALE, 0, &time, |
106 | 106 |
("yyyy"), buf3, 5)) { |
107 | 107 |
os << buf1 << ' ' << buf2 << ' ' << buf3; |
108 | 108 |
} |
109 | 109 |
else os << "unknown"; |
110 | 110 |
#else |
111 | 111 |
timeval tv; |
112 | 112 |
gettimeofday(&tv, 0); |
113 | 113 |
|
114 | 114 |
char cbuf[26]; |
115 | 115 |
ctime_r(&tv.tv_sec,cbuf); |
116 | 116 |
os << cbuf; |
117 | 117 |
#endif |
118 | 118 |
return os.str(); |
119 | 119 |
} |
120 | 120 |
|
121 | 121 |
int getWinRndSeed() |
122 | 122 |
{ |
123 | 123 |
#ifdef WIN32 |
124 | 124 |
FILETIME time; |
125 | 125 |
GetSystemTimeAsFileTime(&time); |
126 | 126 |
return GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime; |
127 | 127 |
#else |
128 | 128 |
timeval tv; |
129 | 129 |
gettimeofday(&tv, 0); |
130 | 130 |
return getpid() + tv.tv_sec + tv.tv_usec; |
131 | 131 |
#endif |
132 | 132 |
} |
133 | 133 |
} |
134 | 134 |
} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
// -*- C++ -*- |
20 | 20 |
#ifndef LEMON_CBC_H |
21 | 21 |
#define LEMON_CBC_H |
22 | 22 |
|
23 | 23 |
///\file |
24 | 24 |
///\brief Header of the LEMON-CBC mip solver interface. |
25 | 25 |
///\ingroup lp_group |
26 | 26 |
|
27 | 27 |
#include <lemon/lp_base.h> |
28 | 28 |
|
29 | 29 |
class CoinModel; |
30 | 30 |
class OsiSolverInterface; |
31 | 31 |
class CbcModel; |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
/// \brief Interface for the CBC MIP solver |
36 | 36 |
/// |
37 | 37 |
/// This class implements an interface for the CBC MIP solver. |
38 | 38 |
///\ingroup lp_group |
39 | 39 |
class CbcMip : public MipSolver { |
40 | 40 |
protected: |
41 | 41 |
|
42 | 42 |
CoinModel *_prob; |
43 | 43 |
OsiSolverInterface *_osi_solver; |
44 | 44 |
CbcModel *_cbc_model; |
45 | 45 |
|
46 | 46 |
public: |
47 | 47 |
|
48 | 48 |
/// \e |
49 | 49 |
CbcMip(); |
50 | 50 |
/// \e |
51 | 51 |
CbcMip(const CbcMip&); |
52 | 52 |
/// \e |
53 | 53 |
~CbcMip(); |
54 | 54 |
/// \e |
55 | 55 |
virtual CbcMip* newSolver() const; |
56 | 56 |
/// \e |
57 | 57 |
virtual CbcMip* cloneSolver() const; |
58 | 58 |
|
59 | 59 |
protected: |
60 | 60 |
|
61 | 61 |
virtual const char* _solverName() const; |
62 | 62 |
|
63 | 63 |
virtual int _addCol(); |
64 | 64 |
virtual int _addRow(); |
65 | 65 |
|
66 | 66 |
virtual void _eraseCol(int i); |
67 | 67 |
virtual void _eraseRow(int i); |
68 | 68 |
|
69 | 69 |
virtual void _eraseColId(int i); |
70 | 70 |
virtual void _eraseRowId(int i); |
71 | 71 |
|
72 | 72 |
virtual void _getColName(int col, std::string& name) const; |
73 | 73 |
virtual void _setColName(int col, const std::string& name); |
74 | 74 |
virtual int _colByName(const std::string& name) const; |
75 | 75 |
|
76 | 76 |
virtual void _getRowName(int row, std::string& name) const; |
77 | 77 |
virtual void _setRowName(int row, const std::string& name); |
78 | 78 |
virtual int _rowByName(const std::string& name) const; |
79 | 79 |
|
80 | 80 |
virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e); |
81 | 81 |
virtual void _getRowCoeffs(int i, InsertIterator b) const; |
82 | 82 |
|
83 | 83 |
virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e); |
84 | 84 |
virtual void _getColCoeffs(int i, InsertIterator b) const; |
85 | 85 |
|
86 | 86 |
virtual void _setCoeff(int row, int col, Value value); |
87 | 87 |
virtual Value _getCoeff(int row, int col) const; |
88 | 88 |
|
89 | 89 |
virtual void _setColLowerBound(int i, Value value); |
90 | 90 |
virtual Value _getColLowerBound(int i) const; |
91 | 91 |
virtual void _setColUpperBound(int i, Value value); |
92 | 92 |
virtual Value _getColUpperBound(int i) const; |
93 | 93 |
|
94 | 94 |
virtual void _setRowLowerBound(int i, Value value); |
95 | 95 |
virtual Value _getRowLowerBound(int i) const; |
96 | 96 |
virtual void _setRowUpperBound(int i, Value value); |
97 | 97 |
virtual Value _getRowUpperBound(int i) const; |
98 | 98 |
|
99 | 99 |
virtual void _setObjCoeffs(ExprIterator b, ExprIterator e); |
100 | 100 |
virtual void _getObjCoeffs(InsertIterator b) const; |
101 | 101 |
|
102 | 102 |
virtual void _setObjCoeff(int i, Value obj_coef); |
103 | 103 |
virtual Value _getObjCoeff(int i) const; |
104 | 104 |
|
105 | 105 |
virtual void _setSense(Sense sense); |
106 | 106 |
virtual Sense _getSense() const; |
107 | 107 |
|
108 | 108 |
virtual ColTypes _getColType(int col) const; |
109 | 109 |
virtual void _setColType(int col, ColTypes col_type); |
110 | 110 |
|
111 | 111 |
virtual SolveExitStatus _solve(); |
112 | 112 |
virtual ProblemType _getType() const; |
113 | 113 |
virtual Value _getSol(int i) const; |
114 | 114 |
virtual Value _getSolValue() const; |
115 | 115 |
|
116 | 116 |
virtual void _clear(); |
117 | 117 |
|
118 | 118 |
virtual void _messageLevel(MessageLevel level); |
119 | 119 |
void _applyMessageLevel(); |
120 | 120 |
|
121 | 121 |
int _message_level; |
122 | 122 |
|
123 |
|
|
123 |
|
|
124 | 124 |
|
125 | 125 |
}; |
126 | 126 |
|
127 | 127 |
} |
128 | 128 |
|
129 | 129 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_CIRCULATION_H |
20 | 20 |
#define LEMON_CIRCULATION_H |
21 | 21 |
|
22 | 22 |
#include <lemon/tolerance.h> |
23 | 23 |
#include <lemon/elevator.h> |
24 | 24 |
#include <limits> |
25 | 25 |
|
26 | 26 |
///\ingroup max_flow |
27 | 27 |
///\file |
28 | 28 |
///\brief Push-relabel algorithm for finding a feasible circulation. |
29 | 29 |
/// |
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 | 32 |
/// \brief Default traits class of Circulation class. |
33 | 33 |
/// |
34 | 34 |
/// Default traits class of Circulation class. |
35 | 35 |
/// |
36 | 36 |
/// \tparam GR Type of the digraph the algorithm runs on. |
37 | 37 |
/// \tparam LM The type of the lower bound map. |
38 | 38 |
/// \tparam UM The type of the upper bound (capacity) map. |
39 | 39 |
/// \tparam SM The type of the supply map. |
40 | 40 |
template <typename GR, typename LM, |
41 | 41 |
typename UM, typename SM> |
42 | 42 |
struct CirculationDefaultTraits { |
43 | 43 |
|
44 | 44 |
/// \brief The type of the digraph the algorithm runs on. |
45 | 45 |
typedef GR Digraph; |
46 | 46 |
|
47 | 47 |
/// \brief The type of the lower bound map. |
48 | 48 |
/// |
49 | 49 |
/// The type of the map that stores the lower bounds on the arcs. |
50 | 50 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
51 | 51 |
typedef LM LowerMap; |
52 | 52 |
|
53 | 53 |
/// \brief The type of the upper bound (capacity) map. |
54 | 54 |
/// |
55 | 55 |
/// The type of the map that stores the upper bounds (capacities) |
56 | 56 |
/// on the arcs. |
57 | 57 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
58 | 58 |
typedef UM UpperMap; |
59 | 59 |
|
60 | 60 |
/// \brief The type of supply map. |
61 | 61 |
/// |
62 |
/// The type of the map that stores the signed supply values of the |
|
63 |
/// nodes. |
|
62 |
/// The type of the map that stores the signed supply values of the |
|
63 |
/// nodes. |
|
64 | 64 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
65 | 65 |
typedef SM SupplyMap; |
66 | 66 |
|
67 | 67 |
/// \brief The type of the flow and supply values. |
68 | 68 |
typedef typename SupplyMap::Value Value; |
69 | 69 |
|
70 | 70 |
/// \brief The type of the map that stores the flow values. |
71 | 71 |
/// |
72 | 72 |
/// The type of the map that stores the flow values. |
73 | 73 |
/// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" |
74 | 74 |
/// concept. |
75 | 75 |
typedef typename Digraph::template ArcMap<Value> FlowMap; |
76 | 76 |
|
77 | 77 |
/// \brief Instantiates a FlowMap. |
78 | 78 |
/// |
79 | 79 |
/// This function instantiates a \ref FlowMap. |
80 | 80 |
/// \param digraph The digraph for which we would like to define |
81 | 81 |
/// the flow map. |
82 | 82 |
static FlowMap* createFlowMap(const Digraph& digraph) { |
83 | 83 |
return new FlowMap(digraph); |
84 | 84 |
} |
85 | 85 |
|
86 | 86 |
/// \brief The elevator type used by the algorithm. |
87 | 87 |
/// |
88 | 88 |
/// The elevator type used by the algorithm. |
89 | 89 |
/// |
90 | 90 |
/// \sa Elevator |
91 | 91 |
/// \sa LinkedElevator |
92 | 92 |
typedef lemon::Elevator<Digraph, typename Digraph::Node> Elevator; |
93 | 93 |
|
94 | 94 |
/// \brief Instantiates an Elevator. |
95 | 95 |
/// |
96 | 96 |
/// This function instantiates an \ref Elevator. |
97 | 97 |
/// \param digraph The digraph for which we would like to define |
98 | 98 |
/// the elevator. |
99 | 99 |
/// \param max_level The maximum level of the elevator. |
100 | 100 |
static Elevator* createElevator(const Digraph& digraph, int max_level) { |
101 | 101 |
return new Elevator(digraph, max_level); |
102 | 102 |
} |
103 | 103 |
|
104 | 104 |
/// \brief The tolerance used by the algorithm |
105 | 105 |
/// |
106 | 106 |
/// The tolerance used by the algorithm to handle inexact computation. |
107 | 107 |
typedef lemon::Tolerance<Value> Tolerance; |
108 | 108 |
|
109 | 109 |
}; |
110 | 110 |
|
111 | 111 |
/** |
112 | 112 |
\brief Push-relabel algorithm for the network circulation problem. |
113 | 113 |
|
114 | 114 |
\ingroup max_flow |
115 | 115 |
This class implements a push-relabel algorithm for the \e network |
116 | 116 |
\e circulation problem. |
117 | 117 |
It is to find a feasible circulation when lower and upper bounds |
118 | 118 |
are given for the flow values on the arcs and lower bounds are |
119 | 119 |
given for the difference between the outgoing and incoming flow |
120 | 120 |
at the nodes. |
121 | 121 |
|
122 | 122 |
The exact formulation of this problem is the following. |
123 | 123 |
Let \f$G=(V,A)\f$ be a digraph, \f$lower: A\rightarrow\mathbf{R}\f$ |
124 | 124 |
\f$upper: A\rightarrow\mathbf{R}\cup\{\infty\}\f$ denote the lower and |
125 | 125 |
upper bounds on the arcs, for which \f$lower(uv) \leq upper(uv)\f$ |
126 | 126 |
holds for all \f$uv\in A\f$, and \f$sup: V\rightarrow\mathbf{R}\f$ |
127 | 127 |
denotes the signed supply values of the nodes. |
128 | 128 |
If \f$sup(u)>0\f$, then \f$u\f$ is a supply node with \f$sup(u)\f$ |
129 | 129 |
supply, if \f$sup(u)<0\f$, then \f$u\f$ is a demand node with |
130 | 130 |
\f$-sup(u)\f$ demand. |
131 | 131 |
A feasible circulation is an \f$f: A\rightarrow\mathbf{R}\f$ |
132 | 132 |
solution of the following problem. |
133 | 133 |
|
134 | 134 |
\f[ \sum_{uv\in A} f(uv) - \sum_{vu\in A} f(vu) |
135 | 135 |
\geq sup(u) \quad \forall u\in V, \f] |
136 | 136 |
\f[ lower(uv) \leq f(uv) \leq upper(uv) \quad \forall uv\in A. \f] |
137 |
|
|
137 |
|
|
138 | 138 |
The sum of the supply values, i.e. \f$\sum_{u\in V} sup(u)\f$ must be |
139 | 139 |
zero or negative in order to have a feasible solution (since the sum |
140 | 140 |
of the expressions on the left-hand side of the inequalities is zero). |
141 | 141 |
It means that the total demand must be greater or equal to the total |
142 | 142 |
supply and all the supplies have to be carried out from the supply nodes, |
143 | 143 |
but there could be demands that are not satisfied. |
144 | 144 |
If \f$\sum_{u\in V} sup(u)\f$ is zero, then all the supply/demand |
145 | 145 |
constraints have to be satisfied with equality, i.e. all demands |
146 | 146 |
have to be satisfied and all supplies have to be used. |
147 |
|
|
147 |
|
|
148 | 148 |
If you need the opposite inequalities in the supply/demand constraints |
149 | 149 |
(i.e. the total demand is less than the total supply and all the demands |
150 | 150 |
have to be satisfied while there could be supplies that are not used), |
151 | 151 |
then you could easily transform the problem to the above form by reversing |
152 | 152 |
the direction of the arcs and taking the negative of the supply values |
153 | 153 |
(e.g. using \ref ReverseDigraph and \ref NegMap adaptors). |
154 | 154 |
|
155 | 155 |
This algorithm either calculates a feasible circulation, or provides |
156 | 156 |
a \ref barrier() "barrier", which prooves that a feasible soultion |
157 | 157 |
cannot exist. |
158 | 158 |
|
159 | 159 |
Note that this algorithm also provides a feasible solution for the |
160 | 160 |
\ref min_cost_flow "minimum cost flow problem". |
161 | 161 |
|
162 | 162 |
\tparam GR The type of the digraph the algorithm runs on. |
163 | 163 |
\tparam LM The type of the lower bound map. The default |
164 | 164 |
map type is \ref concepts::Digraph::ArcMap "GR::ArcMap<int>". |
165 | 165 |
\tparam UM The type of the upper bound (capacity) map. |
166 | 166 |
The default map type is \c LM. |
167 | 167 |
\tparam SM The type of the supply map. The default map type is |
168 | 168 |
\ref concepts::Digraph::NodeMap "GR::NodeMap<UM::Value>". |
169 | 169 |
*/ |
170 | 170 |
#ifdef DOXYGEN |
171 | 171 |
template< typename GR, |
172 | 172 |
typename LM, |
173 | 173 |
typename UM, |
174 | 174 |
typename SM, |
175 | 175 |
typename TR > |
176 | 176 |
#else |
177 | 177 |
template< typename GR, |
178 | 178 |
typename LM = typename GR::template ArcMap<int>, |
179 | 179 |
typename UM = LM, |
180 | 180 |
typename SM = typename GR::template NodeMap<typename UM::Value>, |
181 | 181 |
typename TR = CirculationDefaultTraits<GR, LM, UM, SM> > |
182 | 182 |
#endif |
183 | 183 |
class Circulation { |
184 | 184 |
public: |
185 | 185 |
|
186 | 186 |
///The \ref CirculationDefaultTraits "traits class" of the algorithm. |
187 | 187 |
typedef TR Traits; |
188 | 188 |
///The type of the digraph the algorithm runs on. |
189 | 189 |
typedef typename Traits::Digraph Digraph; |
190 | 190 |
///The type of the flow and supply values. |
191 | 191 |
typedef typename Traits::Value Value; |
192 | 192 |
|
193 | 193 |
///The type of the lower bound map. |
194 | 194 |
typedef typename Traits::LowerMap LowerMap; |
195 | 195 |
///The type of the upper bound (capacity) map. |
196 | 196 |
typedef typename Traits::UpperMap UpperMap; |
197 | 197 |
///The type of the supply map. |
198 | 198 |
typedef typename Traits::SupplyMap SupplyMap; |
199 | 199 |
///The type of the flow map. |
200 | 200 |
typedef typename Traits::FlowMap FlowMap; |
201 | 201 |
|
202 | 202 |
///The type of the elevator. |
203 | 203 |
typedef typename Traits::Elevator Elevator; |
204 | 204 |
///The type of the tolerance. |
205 | 205 |
typedef typename Traits::Tolerance Tolerance; |
206 | 206 |
|
207 | 207 |
private: |
208 | 208 |
|
209 | 209 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
210 | 210 |
|
211 | 211 |
const Digraph &_g; |
... | ... |
@@ -264,129 +264,129 @@ |
264 | 264 |
LEMON_ASSERT(false, "Elevator is not initialized"); |
265 | 265 |
return 0; // ignore warnings |
266 | 266 |
} |
267 | 267 |
}; |
268 | 268 |
|
269 | 269 |
/// \brief \ref named-templ-param "Named parameter" for setting |
270 | 270 |
/// Elevator type |
271 | 271 |
/// |
272 | 272 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
273 | 273 |
/// type. If this named parameter is used, then an external |
274 | 274 |
/// elevator object must be passed to the algorithm using the |
275 | 275 |
/// \ref elevator(Elevator&) "elevator()" function before calling |
276 | 276 |
/// \ref run() or \ref init(). |
277 | 277 |
/// \sa SetStandardElevator |
278 | 278 |
template <typename T> |
279 | 279 |
struct SetElevator |
280 | 280 |
: public Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
281 | 281 |
SetElevatorTraits<T> > { |
282 | 282 |
typedef Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
283 | 283 |
SetElevatorTraits<T> > Create; |
284 | 284 |
}; |
285 | 285 |
|
286 | 286 |
template <typename T> |
287 | 287 |
struct SetStandardElevatorTraits : public Traits { |
288 | 288 |
typedef T Elevator; |
289 | 289 |
static Elevator *createElevator(const Digraph& digraph, int max_level) { |
290 | 290 |
return new Elevator(digraph, max_level); |
291 | 291 |
} |
292 | 292 |
}; |
293 | 293 |
|
294 | 294 |
/// \brief \ref named-templ-param "Named parameter" for setting |
295 | 295 |
/// Elevator type with automatic allocation |
296 | 296 |
/// |
297 | 297 |
/// \ref named-templ-param "Named parameter" for setting Elevator |
298 | 298 |
/// type with automatic allocation. |
299 | 299 |
/// The Elevator should have standard constructor interface to be |
300 | 300 |
/// able to automatically created by the algorithm (i.e. the |
301 | 301 |
/// digraph and the maximum level should be passed to it). |
302 | 302 |
/// However an external elevator object could also be passed to the |
303 | 303 |
/// algorithm with the \ref elevator(Elevator&) "elevator()" function |
304 | 304 |
/// before calling \ref run() or \ref init(). |
305 | 305 |
/// \sa SetElevator |
306 | 306 |
template <typename T> |
307 | 307 |
struct SetStandardElevator |
308 | 308 |
: public Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
309 | 309 |
SetStandardElevatorTraits<T> > { |
310 | 310 |
typedef Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
311 | 311 |
SetStandardElevatorTraits<T> > Create; |
312 | 312 |
}; |
313 | 313 |
|
314 | 314 |
/// @} |
315 | 315 |
|
316 | 316 |
protected: |
317 | 317 |
|
318 | 318 |
Circulation() {} |
319 | 319 |
|
320 | 320 |
public: |
321 | 321 |
|
322 | 322 |
/// Constructor. |
323 | 323 |
|
324 | 324 |
/// The constructor of the class. |
325 | 325 |
/// |
326 | 326 |
/// \param graph The digraph the algorithm runs on. |
327 | 327 |
/// \param lower The lower bounds for the flow values on the arcs. |
328 |
/// \param upper The upper bounds (capacities) for the flow values |
|
328 |
/// \param upper The upper bounds (capacities) for the flow values |
|
329 | 329 |
/// on the arcs. |
330 | 330 |
/// \param supply The signed supply values of the nodes. |
331 | 331 |
Circulation(const Digraph &graph, const LowerMap &lower, |
332 | 332 |
const UpperMap &upper, const SupplyMap &supply) |
333 | 333 |
: _g(graph), _lo(&lower), _up(&upper), _supply(&supply), |
334 | 334 |
_flow(NULL), _local_flow(false), _level(NULL), _local_level(false), |
335 | 335 |
_excess(NULL) {} |
336 | 336 |
|
337 | 337 |
/// Destructor. |
338 | 338 |
~Circulation() { |
339 | 339 |
destroyStructures(); |
340 | 340 |
} |
341 | 341 |
|
342 | 342 |
|
343 | 343 |
private: |
344 | 344 |
|
345 | 345 |
bool checkBoundMaps() { |
346 | 346 |
for (ArcIt e(_g);e!=INVALID;++e) { |
347 | 347 |
if (_tol.less((*_up)[e], (*_lo)[e])) return false; |
348 | 348 |
} |
349 | 349 |
return true; |
350 | 350 |
} |
351 | 351 |
|
352 | 352 |
void createStructures() { |
353 | 353 |
_node_num = _el = countNodes(_g); |
354 | 354 |
|
355 | 355 |
if (!_flow) { |
356 | 356 |
_flow = Traits::createFlowMap(_g); |
357 | 357 |
_local_flow = true; |
358 | 358 |
} |
359 | 359 |
if (!_level) { |
360 | 360 |
_level = Traits::createElevator(_g, _node_num); |
361 | 361 |
_local_level = true; |
362 | 362 |
} |
363 | 363 |
if (!_excess) { |
364 | 364 |
_excess = new ExcessMap(_g); |
365 | 365 |
} |
366 | 366 |
} |
367 | 367 |
|
368 | 368 |
void destroyStructures() { |
369 | 369 |
if (_local_flow) { |
370 | 370 |
delete _flow; |
371 | 371 |
} |
372 | 372 |
if (_local_level) { |
373 | 373 |
delete _level; |
374 | 374 |
} |
375 | 375 |
if (_excess) { |
376 | 376 |
delete _excess; |
377 | 377 |
} |
378 | 378 |
} |
379 | 379 |
|
380 | 380 |
public: |
381 | 381 |
|
382 | 382 |
/// Sets the lower bound map. |
383 | 383 |
|
384 | 384 |
/// Sets the lower bound map. |
385 | 385 |
/// \return <tt>(*this)</tt> |
386 | 386 |
Circulation& lowerMap(const LowerMap& map) { |
387 | 387 |
_lo = ↦ |
388 | 388 |
return *this; |
389 | 389 |
} |
390 | 390 |
|
391 | 391 |
/// Sets the upper bound (capacity) map. |
392 | 392 |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <lemon/clp.h> |
20 | 20 |
#include <coin/ClpSimplex.hpp> |
21 | 21 |
|
22 | 22 |
namespace lemon { |
23 | 23 |
|
24 | 24 |
ClpLp::ClpLp() { |
25 | 25 |
_prob = new ClpSimplex(); |
26 | 26 |
_init_temporals(); |
27 | 27 |
messageLevel(MESSAGE_NOTHING); |
28 | 28 |
} |
29 | 29 |
|
30 | 30 |
ClpLp::ClpLp(const ClpLp& other) { |
31 | 31 |
_prob = new ClpSimplex(*other._prob); |
32 | 32 |
rows = other.rows; |
33 | 33 |
cols = other.cols; |
34 | 34 |
_init_temporals(); |
35 | 35 |
messageLevel(MESSAGE_NOTHING); |
36 | 36 |
} |
37 | 37 |
|
38 | 38 |
ClpLp::~ClpLp() { |
39 | 39 |
delete _prob; |
40 | 40 |
_clear_temporals(); |
41 | 41 |
} |
42 | 42 |
|
43 | 43 |
void ClpLp::_init_temporals() { |
44 | 44 |
_primal_ray = 0; |
45 | 45 |
_dual_ray = 0; |
46 | 46 |
} |
47 | 47 |
|
48 | 48 |
void ClpLp::_clear_temporals() { |
49 | 49 |
if (_primal_ray) { |
50 | 50 |
delete[] _primal_ray; |
51 | 51 |
_primal_ray = 0; |
52 | 52 |
} |
53 | 53 |
if (_dual_ray) { |
54 | 54 |
delete[] _dual_ray; |
55 | 55 |
_dual_ray = 0; |
56 | 56 |
} |
57 | 57 |
} |
58 | 58 |
|
59 | 59 |
ClpLp* ClpLp::newSolver() const { |
60 | 60 |
ClpLp* newlp = new ClpLp; |
61 | 61 |
return newlp; |
62 | 62 |
} |
63 | 63 |
|
64 | 64 |
ClpLp* ClpLp::cloneSolver() const { |
65 | 65 |
ClpLp* copylp = new ClpLp(*this); |
66 | 66 |
return copylp; |
67 | 67 |
} |
68 | 68 |
|
69 | 69 |
const char* ClpLp::_solverName() const { return "ClpLp"; } |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_CLP_H |
20 | 20 |
#define LEMON_CLP_H |
21 | 21 |
|
22 | 22 |
///\file |
23 | 23 |
///\brief Header of the LEMON-CLP lp solver interface. |
24 | 24 |
|
25 | 25 |
#include <vector> |
26 | 26 |
#include <string> |
27 | 27 |
|
28 | 28 |
#include <lemon/lp_base.h> |
29 | 29 |
|
30 | 30 |
class ClpSimplex; |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
/// \ingroup lp_group |
35 | 35 |
/// |
36 | 36 |
/// \brief Interface for the CLP solver |
37 | 37 |
/// |
38 | 38 |
/// This class implements an interface for the Clp LP solver. The |
39 | 39 |
/// Clp library is an object oriented lp solver library developed at |
40 | 40 |
/// the IBM. The CLP is part of the COIN-OR package and it can be |
41 | 41 |
/// used with Common Public License. |
42 | 42 |
class ClpLp : public LpSolver { |
43 | 43 |
protected: |
44 | 44 |
|
45 | 45 |
ClpSimplex* _prob; |
46 | 46 |
|
47 | 47 |
std::map<std::string, int> _col_names_ref; |
48 | 48 |
std::map<std::string, int> _row_names_ref; |
49 | 49 |
|
50 | 50 |
public: |
51 | 51 |
|
52 | 52 |
/// \e |
53 | 53 |
ClpLp(); |
54 | 54 |
/// \e |
55 | 55 |
ClpLp(const ClpLp&); |
56 | 56 |
/// \e |
57 | 57 |
~ClpLp(); |
58 | 58 |
|
59 | 59 |
/// \e |
60 | 60 |
virtual ClpLp* newSolver() const; |
61 | 61 |
/// \e |
62 | 62 |
virtual ClpLp* cloneSolver() const; |
63 | 63 |
|
64 | 64 |
protected: |
65 | 65 |
|
66 | 66 |
mutable double* _primal_ray; |
67 | 67 |
mutable double* _dual_ray; |
68 | 68 |
|
69 | 69 |
void _init_temporals(); |
... | ... |
@@ -76,88 +76,88 @@ |
76 | 76 |
virtual int _addCol(); |
77 | 77 |
virtual int _addRow(); |
78 | 78 |
|
79 | 79 |
virtual void _eraseCol(int i); |
80 | 80 |
virtual void _eraseRow(int i); |
81 | 81 |
|
82 | 82 |
virtual void _eraseColId(int i); |
83 | 83 |
virtual void _eraseRowId(int i); |
84 | 84 |
|
85 | 85 |
virtual void _getColName(int col, std::string& name) const; |
86 | 86 |
virtual void _setColName(int col, const std::string& name); |
87 | 87 |
virtual int _colByName(const std::string& name) const; |
88 | 88 |
|
89 | 89 |
virtual void _getRowName(int row, std::string& name) const; |
90 | 90 |
virtual void _setRowName(int row, const std::string& name); |
91 | 91 |
virtual int _rowByName(const std::string& name) const; |
92 | 92 |
|
93 | 93 |
virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e); |
94 | 94 |
virtual void _getRowCoeffs(int i, InsertIterator b) const; |
95 | 95 |
|
96 | 96 |
virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e); |
97 | 97 |
virtual void _getColCoeffs(int i, InsertIterator b) const; |
98 | 98 |
|
99 | 99 |
virtual void _setCoeff(int row, int col, Value value); |
100 | 100 |
virtual Value _getCoeff(int row, int col) const; |
101 | 101 |
|
102 | 102 |
virtual void _setColLowerBound(int i, Value value); |
103 | 103 |
virtual Value _getColLowerBound(int i) const; |
104 | 104 |
virtual void _setColUpperBound(int i, Value value); |
105 | 105 |
virtual Value _getColUpperBound(int i) const; |
106 | 106 |
|
107 | 107 |
virtual void _setRowLowerBound(int i, Value value); |
108 | 108 |
virtual Value _getRowLowerBound(int i) const; |
109 | 109 |
virtual void _setRowUpperBound(int i, Value value); |
110 | 110 |
virtual Value _getRowUpperBound(int i) const; |
111 | 111 |
|
112 | 112 |
virtual void _setObjCoeffs(ExprIterator, ExprIterator); |
113 | 113 |
virtual void _getObjCoeffs(InsertIterator) const; |
114 | 114 |
|
115 | 115 |
virtual void _setObjCoeff(int i, Value obj_coef); |
116 | 116 |
virtual Value _getObjCoeff(int i) const; |
117 | 117 |
|
118 | 118 |
virtual void _setSense(Sense sense); |
119 | 119 |
virtual Sense _getSense() const; |
120 | 120 |
|
121 | 121 |
virtual SolveExitStatus _solve(); |
122 | 122 |
|
123 | 123 |
virtual Value _getPrimal(int i) const; |
124 | 124 |
virtual Value _getDual(int i) const; |
125 | 125 |
|
126 | 126 |
virtual Value _getPrimalValue() const; |
127 | 127 |
|
128 | 128 |
virtual Value _getPrimalRay(int i) const; |
129 | 129 |
virtual Value _getDualRay(int i) const; |
130 | 130 |
|
131 | 131 |
virtual VarStatus _getColStatus(int i) const; |
132 | 132 |
virtual VarStatus _getRowStatus(int i) const; |
133 | 133 |
|
134 | 134 |
virtual ProblemType _getPrimalType() const; |
135 | 135 |
virtual ProblemType _getDualType() const; |
136 | 136 |
|
137 | 137 |
virtual void _clear(); |
138 | 138 |
|
139 | 139 |
virtual void _messageLevel(MessageLevel); |
140 |
|
|
140 |
|
|
141 | 141 |
public: |
142 | 142 |
|
143 | 143 |
///Solves LP with primal simplex method. |
144 | 144 |
SolveExitStatus solvePrimal(); |
145 | 145 |
|
146 | 146 |
///Solves LP with dual simplex method. |
147 | 147 |
SolveExitStatus solveDual(); |
148 | 148 |
|
149 | 149 |
///Solves LP with barrier method. |
150 | 150 |
SolveExitStatus solveBarrier(); |
151 | 151 |
|
152 | 152 |
///Returns the constraint identifier understood by CLP. |
153 | 153 |
int clpRow(Row r) const { return rows(id(r)); } |
154 | 154 |
|
155 | 155 |
///Returns the variable identifier understood by CLP. |
156 | 156 |
int clpCol(Col c) const { return cols(id(c)); } |
157 | 157 |
|
158 | 158 |
}; |
159 | 159 |
|
160 | 160 |
} //END OF NAMESPACE LEMON |
161 | 161 |
|
162 | 162 |
#endif //LEMON_CLP_H |
163 | 163 |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_CONCEPTS_DIGRAPH_H |
20 | 20 |
#define LEMON_CONCEPTS_DIGRAPH_H |
21 | 21 |
|
22 | 22 |
///\ingroup graph_concepts |
23 | 23 |
///\file |
24 | 24 |
///\brief The concept of directed graphs. |
25 | 25 |
|
26 | 26 |
#include <lemon/core.h> |
27 | 27 |
#include <lemon/concepts/maps.h> |
28 | 28 |
#include <lemon/concept_check.h> |
29 | 29 |
#include <lemon/concepts/graph_components.h> |
30 | 30 |
|
31 | 31 |
namespace lemon { |
32 | 32 |
namespace concepts { |
33 | 33 |
|
34 | 34 |
/// \ingroup graph_concepts |
35 | 35 |
/// |
36 | 36 |
/// \brief Class describing the concept of directed graphs. |
37 | 37 |
/// |
38 | 38 |
/// This class describes the \ref concept "concept" of the |
39 | 39 |
/// immutable directed digraphs. |
40 | 40 |
/// |
41 | 41 |
/// Note that actual digraph implementation like @ref ListDigraph or |
42 | 42 |
/// @ref SmartDigraph may have several additional functionality. |
43 | 43 |
/// |
44 | 44 |
/// \sa concept |
45 | 45 |
class Digraph { |
46 | 46 |
private: |
47 | 47 |
///Digraphs are \e not copy constructible. Use DigraphCopy() instead. |
48 | 48 |
|
49 | 49 |
///Digraphs are \e not copy constructible. Use DigraphCopy() instead. |
50 | 50 |
/// |
51 | 51 |
Digraph(const Digraph &) {}; |
52 | 52 |
///\brief Assignment of \ref Digraph "Digraph"s to another ones are |
53 | 53 |
///\e not allowed. Use DigraphCopy() instead. |
54 | 54 |
|
55 | 55 |
///Assignment of \ref Digraph "Digraph"s to another ones are |
56 | 56 |
///\e not allowed. Use DigraphCopy() instead. |
57 | 57 |
|
58 | 58 |
void operator=(const Digraph &) {} |
59 | 59 |
public: |
60 | 60 |
///\e |
61 | 61 |
|
62 | 62 |
/// Defalult constructor. |
63 | 63 |
|
64 | 64 |
/// Defalult constructor. |
65 | 65 |
/// |
66 | 66 |
Digraph() { } |
67 | 67 |
/// Class for identifying a node of the digraph |
68 | 68 |
|
69 | 69 |
/// This class identifies a node of the digraph. It also serves |
... | ... |
@@ -374,115 +374,115 @@ |
374 | 374 |
|
375 | 375 |
void first(Arc&) const {} |
376 | 376 |
void next(Arc&) const {} |
377 | 377 |
|
378 | 378 |
|
379 | 379 |
void firstIn(Arc&, const Node&) const {} |
380 | 380 |
void nextIn(Arc&) const {} |
381 | 381 |
|
382 | 382 |
void firstOut(Arc&, const Node&) const {} |
383 | 383 |
void nextOut(Arc&) const {} |
384 | 384 |
|
385 | 385 |
// The second parameter is dummy. |
386 | 386 |
Node fromId(int, Node) const { return INVALID; } |
387 | 387 |
// The second parameter is dummy. |
388 | 388 |
Arc fromId(int, Arc) const { return INVALID; } |
389 | 389 |
|
390 | 390 |
// Dummy parameter. |
391 | 391 |
int maxId(Node) const { return -1; } |
392 | 392 |
// Dummy parameter. |
393 | 393 |
int maxId(Arc) const { return -1; } |
394 | 394 |
|
395 | 395 |
/// \brief The base node of the iterator. |
396 | 396 |
/// |
397 | 397 |
/// Gives back the base node of the iterator. |
398 | 398 |
/// It is always the target of the pointed arc. |
399 | 399 |
Node baseNode(const InArcIt&) const { return INVALID; } |
400 | 400 |
|
401 | 401 |
/// \brief The running node of the iterator. |
402 | 402 |
/// |
403 | 403 |
/// Gives back the running node of the iterator. |
404 | 404 |
/// It is always the source of the pointed arc. |
405 | 405 |
Node runningNode(const InArcIt&) const { return INVALID; } |
406 | 406 |
|
407 | 407 |
/// \brief The base node of the iterator. |
408 | 408 |
/// |
409 | 409 |
/// Gives back the base node of the iterator. |
410 | 410 |
/// It is always the source of the pointed arc. |
411 | 411 |
Node baseNode(const OutArcIt&) const { return INVALID; } |
412 | 412 |
|
413 | 413 |
/// \brief The running node of the iterator. |
414 | 414 |
/// |
415 | 415 |
/// Gives back the running node of the iterator. |
416 | 416 |
/// It is always the target of the pointed arc. |
417 | 417 |
Node runningNode(const OutArcIt&) const { return INVALID; } |
418 | 418 |
|
419 | 419 |
/// \brief The opposite node on the given arc. |
420 | 420 |
/// |
421 | 421 |
/// Gives back the opposite node on the given arc. |
422 | 422 |
Node oppositeNode(const Node&, const Arc&) const { return INVALID; } |
423 | 423 |
|
424 | 424 |
/// \brief Reference map of the nodes to type \c T. |
425 | 425 |
/// |
426 | 426 |
/// Reference map of the nodes to type \c T. |
427 | 427 |
template<class T> |
428 | 428 |
class NodeMap : public ReferenceMap<Node, T, T&, const T&> { |
429 | 429 |
public: |
430 | 430 |
|
431 | 431 |
///\e |
432 | 432 |
NodeMap(const Digraph&) { } |
433 | 433 |
///\e |
434 | 434 |
NodeMap(const Digraph&, T) { } |
435 | 435 |
|
436 | 436 |
private: |
437 | 437 |
///Copy constructor |
438 |
NodeMap(const NodeMap& nm) : |
|
438 |
NodeMap(const NodeMap& nm) : |
|
439 | 439 |
ReferenceMap<Node, T, T&, const T&>(nm) { } |
440 | 440 |
///Assignment operator |
441 | 441 |
template <typename CMap> |
442 | 442 |
NodeMap& operator=(const CMap&) { |
443 | 443 |
checkConcept<ReadMap<Node, T>, CMap>(); |
444 | 444 |
return *this; |
445 | 445 |
} |
446 | 446 |
}; |
447 | 447 |
|
448 | 448 |
/// \brief Reference map of the arcs to type \c T. |
449 | 449 |
/// |
450 | 450 |
/// Reference map of the arcs to type \c T. |
451 | 451 |
template<class T> |
452 | 452 |
class ArcMap : public ReferenceMap<Arc, T, T&, const T&> { |
453 | 453 |
public: |
454 | 454 |
|
455 | 455 |
///\e |
456 | 456 |
ArcMap(const Digraph&) { } |
457 | 457 |
///\e |
458 | 458 |
ArcMap(const Digraph&, T) { } |
459 | 459 |
private: |
460 | 460 |
///Copy constructor |
461 | 461 |
ArcMap(const ArcMap& em) : |
462 | 462 |
ReferenceMap<Arc, T, T&, const T&>(em) { } |
463 | 463 |
///Assignment operator |
464 | 464 |
template <typename CMap> |
465 | 465 |
ArcMap& operator=(const CMap&) { |
466 | 466 |
checkConcept<ReadMap<Arc, T>, CMap>(); |
467 | 467 |
return *this; |
468 | 468 |
} |
469 | 469 |
}; |
470 | 470 |
|
471 | 471 |
template <typename _Digraph> |
472 | 472 |
struct Constraints { |
473 | 473 |
void constraints() { |
474 | 474 |
checkConcept<BaseDigraphComponent, _Digraph>(); |
475 | 475 |
checkConcept<IterableDigraphComponent<>, _Digraph>(); |
476 | 476 |
checkConcept<IDableDigraphComponent<>, _Digraph>(); |
477 | 477 |
checkConcept<MappableDigraphComponent<>, _Digraph>(); |
478 | 478 |
} |
479 | 479 |
}; |
480 | 480 |
|
481 | 481 |
}; |
482 | 482 |
|
483 | 483 |
} //namespace concepts |
484 | 484 |
} //namespace lemon |
485 | 485 |
|
486 | 486 |
|
487 | 487 |
|
488 | 488 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup graph_concepts |
20 | 20 |
///\file |
21 | 21 |
///\brief The concept of graph components. |
22 | 22 |
|
23 | 23 |
#ifndef LEMON_CONCEPTS_GRAPH_COMPONENTS_H |
24 | 24 |
#define LEMON_CONCEPTS_GRAPH_COMPONENTS_H |
25 | 25 |
|
26 | 26 |
#include <lemon/core.h> |
27 | 27 |
#include <lemon/concepts/maps.h> |
28 | 28 |
|
29 | 29 |
#include <lemon/bits/alteration_notifier.h> |
30 | 30 |
|
31 | 31 |
namespace lemon { |
32 | 32 |
namespace concepts { |
33 | 33 |
|
34 | 34 |
/// \brief Concept class for \c Node, \c Arc and \c Edge types. |
35 | 35 |
/// |
36 | 36 |
/// This class describes the concept of \c Node, \c Arc and \c Edge |
37 | 37 |
/// subtypes of digraph and graph types. |
38 | 38 |
/// |
39 | 39 |
/// \note This class is a template class so that we can use it to |
40 | 40 |
/// create graph skeleton classes. The reason for this is that \c Node |
41 |
/// and \c Arc (or \c Edge) types should \e not derive from the same |
|
41 |
/// and \c Arc (or \c Edge) types should \e not derive from the same |
|
42 | 42 |
/// base class. For \c Node you should instantiate it with character |
43 | 43 |
/// \c 'n', for \c Arc with \c 'a' and for \c Edge with \c 'e'. |
44 | 44 |
#ifndef DOXYGEN |
45 | 45 |
template <char sel = '0'> |
46 | 46 |
#endif |
47 | 47 |
class GraphItem { |
48 | 48 |
public: |
49 | 49 |
/// \brief Default constructor. |
50 | 50 |
/// |
51 | 51 |
/// Default constructor. |
52 | 52 |
/// \warning The default constructor is not required to set |
53 | 53 |
/// the item to some well-defined value. So you should consider it |
54 | 54 |
/// as uninitialized. |
55 | 55 |
GraphItem() {} |
56 | 56 |
|
57 | 57 |
/// \brief Copy constructor. |
58 | 58 |
/// |
59 | 59 |
/// Copy constructor. |
60 | 60 |
GraphItem(const GraphItem &) {} |
61 | 61 |
|
62 | 62 |
/// \brief Constructor for conversion from \c INVALID. |
63 | 63 |
/// |
64 | 64 |
/// Constructor for conversion from \c INVALID. |
65 | 65 |
/// It initializes the item to be invalid. |
66 | 66 |
/// \sa Invalid for more details. |
67 | 67 |
GraphItem(Invalid) {} |
68 | 68 |
|
69 | 69 |
/// \brief Assignment operator. |
70 | 70 |
/// |
71 | 71 |
/// Assignment operator for the item. |
72 | 72 |
GraphItem& operator=(const GraphItem&) { return *this; } |
73 | 73 |
|
74 | 74 |
/// \brief Assignment operator for INVALID. |
75 | 75 |
/// |
76 | 76 |
/// This operator makes the item invalid. |
77 | 77 |
GraphItem& operator=(Invalid) { return *this; } |
78 | 78 |
|
79 | 79 |
/// \brief Equality operator. |
80 | 80 |
/// |
81 | 81 |
/// Equality operator. |
82 | 82 |
bool operator==(const GraphItem&) const { return false; } |
83 | 83 |
|
84 | 84 |
/// \brief Inequality operator. |
85 | 85 |
/// |
86 | 86 |
/// Inequality operator. |
87 | 87 |
bool operator!=(const GraphItem&) const { return false; } |
88 | 88 |
|
89 | 89 |
/// \brief Ordering operator. |
90 | 90 |
/// |
91 | 91 |
/// This operator defines an ordering of the items. |
92 |
/// It makes possible to use graph item types as key types in |
|
92 |
/// It makes possible to use graph item types as key types in |
|
93 | 93 |
/// associative containers (e.g. \c std::map). |
94 | 94 |
/// |
95 | 95 |
/// \note This operator only have to define some strict ordering of |
96 | 96 |
/// the items; this order has nothing to do with the iteration |
97 | 97 |
/// ordering of the items. |
98 | 98 |
bool operator<(const GraphItem&) const { return false; } |
99 | 99 |
|
100 | 100 |
template<typename _GraphItem> |
101 | 101 |
struct Constraints { |
102 | 102 |
void constraints() { |
103 | 103 |
_GraphItem i1; |
104 | 104 |
i1=INVALID; |
105 | 105 |
_GraphItem i2 = i1; |
106 | 106 |
_GraphItem i3 = INVALID; |
107 | 107 |
|
108 | 108 |
i1 = i2 = i3; |
109 | 109 |
|
110 | 110 |
bool b; |
111 | 111 |
b = (ia == ib) && (ia != ib); |
112 | 112 |
b = (ia == INVALID) && (ib != INVALID); |
113 | 113 |
b = (ia < ib); |
114 | 114 |
} |
115 | 115 |
|
116 | 116 |
const _GraphItem &ia; |
117 | 117 |
const _GraphItem &ib; |
118 | 118 |
}; |
119 | 119 |
}; |
120 | 120 |
|
121 | 121 |
/// \brief Base skeleton class for directed graphs. |
122 | 122 |
/// |
123 | 123 |
/// This class describes the base interface of directed graph types. |
124 | 124 |
/// All digraph %concepts have to conform to this class. |
125 |
/// It just provides types for nodes and arcs and functions |
|
125 |
/// It just provides types for nodes and arcs and functions |
|
126 | 126 |
/// to get the source and the target nodes of arcs. |
127 | 127 |
class BaseDigraphComponent { |
128 | 128 |
public: |
129 | 129 |
|
130 | 130 |
typedef BaseDigraphComponent Digraph; |
131 | 131 |
|
132 | 132 |
/// \brief Node class of the digraph. |
133 | 133 |
/// |
134 | 134 |
/// This class represents the nodes of the digraph. |
135 | 135 |
typedef GraphItem<'n'> Node; |
136 | 136 |
|
137 | 137 |
/// \brief Arc class of the digraph. |
138 | 138 |
/// |
139 | 139 |
/// This class represents the arcs of the digraph. |
140 | 140 |
typedef GraphItem<'a'> Arc; |
141 | 141 |
|
142 | 142 |
/// \brief Return the source node of an arc. |
143 | 143 |
/// |
144 | 144 |
/// This function returns the source node of an arc. |
145 | 145 |
Node source(const Arc&) const { return INVALID; } |
146 | 146 |
|
147 | 147 |
/// \brief Return the target node of an arc. |
148 | 148 |
/// |
149 | 149 |
/// This function returns the target node of an arc. |
150 | 150 |
Node target(const Arc&) const { return INVALID; } |
151 | 151 |
|
152 | 152 |
/// \brief Return the opposite node on the given arc. |
153 | 153 |
/// |
154 | 154 |
/// This function returns the opposite node on the given arc. |
155 | 155 |
Node oppositeNode(const Node&, const Arc&) const { |
156 | 156 |
return INVALID; |
157 | 157 |
} |
158 | 158 |
|
159 | 159 |
template <typename _Digraph> |
160 | 160 |
struct Constraints { |
161 | 161 |
typedef typename _Digraph::Node Node; |
162 | 162 |
typedef typename _Digraph::Arc Arc; |
163 | 163 |
|
164 | 164 |
void constraints() { |
165 | 165 |
checkConcept<GraphItem<'n'>, Node>(); |
166 | 166 |
checkConcept<GraphItem<'a'>, Arc>(); |
167 | 167 |
{ |
168 | 168 |
Node n; |
169 | 169 |
Arc e(INVALID); |
170 | 170 |
n = digraph.source(e); |
171 | 171 |
n = digraph.target(e); |
172 | 172 |
n = digraph.oppositeNode(n, e); |
173 | 173 |
} |
174 | 174 |
} |
175 | 175 |
|
176 | 176 |
const _Digraph& digraph; |
177 | 177 |
}; |
178 | 178 |
}; |
179 | 179 |
|
180 | 180 |
/// \brief Base skeleton class for undirected graphs. |
181 | 181 |
/// |
182 | 182 |
/// This class describes the base interface of undirected graph types. |
183 | 183 |
/// All graph %concepts have to conform to this class. |
184 | 184 |
/// It extends the interface of \ref BaseDigraphComponent with an |
185 | 185 |
/// \c Edge type and functions to get the end nodes of edges, |
186 | 186 |
/// to convert from arcs to edges and to get both direction of edges. |
187 | 187 |
class BaseGraphComponent : public BaseDigraphComponent { |
188 | 188 |
public: |
189 | 189 |
|
... | ... |
@@ -365,236 +365,236 @@ |
365 | 365 |
nid = digraph.maxNodeId(); |
366 | 366 |
ignore_unused_variable_warning(nid); |
367 | 367 |
eid = digraph.maxArcId(); |
368 | 368 |
ignore_unused_variable_warning(eid); |
369 | 369 |
} |
370 | 370 |
|
371 | 371 |
const _Digraph& digraph; |
372 | 372 |
}; |
373 | 373 |
}; |
374 | 374 |
|
375 | 375 |
/// \brief Skeleton class for \e idable undirected graphs. |
376 | 376 |
/// |
377 | 377 |
/// This class describes the interface of \e idable undirected |
378 | 378 |
/// graphs. It extends \ref IDableDigraphComponent with the core ID |
379 | 379 |
/// functions of undirected graphs. |
380 | 380 |
/// The ids of the items must be unique and immutable. |
381 | 381 |
/// This concept is part of the Graph concept. |
382 | 382 |
template <typename BAS = BaseGraphComponent> |
383 | 383 |
class IDableGraphComponent : public IDableDigraphComponent<BAS> { |
384 | 384 |
public: |
385 | 385 |
|
386 | 386 |
typedef BAS Base; |
387 | 387 |
typedef typename Base::Edge Edge; |
388 | 388 |
|
389 | 389 |
using IDableDigraphComponent<Base>::id; |
390 | 390 |
|
391 | 391 |
/// \brief Return a unique integer id for the given edge. |
392 | 392 |
/// |
393 | 393 |
/// This function returns a unique integer id for the given edge. |
394 | 394 |
int id(const Edge&) const { return -1; } |
395 | 395 |
|
396 | 396 |
/// \brief Return the edge by its unique id. |
397 | 397 |
/// |
398 | 398 |
/// This function returns the edge by its unique id. |
399 | 399 |
/// If the graph does not contain an edge with the given id, |
400 | 400 |
/// then the result of the function is undefined. |
401 | 401 |
Edge edgeFromId(int) const { return INVALID; } |
402 | 402 |
|
403 | 403 |
/// \brief Return an integer greater or equal to the maximum |
404 | 404 |
/// edge id. |
405 | 405 |
/// |
406 | 406 |
/// This function returns an integer greater or equal to the |
407 | 407 |
/// maximum edge id. |
408 | 408 |
int maxEdgeId() const { return -1; } |
409 | 409 |
|
410 | 410 |
template <typename _Graph> |
411 | 411 |
struct Constraints { |
412 | 412 |
|
413 | 413 |
void constraints() { |
414 | 414 |
checkConcept<IDableDigraphComponent<Base>, _Graph >(); |
415 | 415 |
typename _Graph::Edge edge; |
416 | 416 |
int ueid = graph.id(edge); |
417 | 417 |
ueid = graph.id(edge); |
418 | 418 |
edge = graph.edgeFromId(ueid); |
419 | 419 |
ueid = graph.maxEdgeId(); |
420 | 420 |
ignore_unused_variable_warning(ueid); |
421 | 421 |
} |
422 | 422 |
|
423 | 423 |
const _Graph& graph; |
424 | 424 |
}; |
425 | 425 |
}; |
426 | 426 |
|
427 | 427 |
/// \brief Concept class for \c NodeIt, \c ArcIt and \c EdgeIt types. |
428 | 428 |
/// |
429 |
/// This class describes the concept of \c NodeIt, \c ArcIt and |
|
429 |
/// This class describes the concept of \c NodeIt, \c ArcIt and |
|
430 | 430 |
/// \c EdgeIt subtypes of digraph and graph types. |
431 | 431 |
template <typename GR, typename Item> |
432 | 432 |
class GraphItemIt : public Item { |
433 | 433 |
public: |
434 | 434 |
/// \brief Default constructor. |
435 | 435 |
/// |
436 | 436 |
/// Default constructor. |
437 | 437 |
/// \warning The default constructor is not required to set |
438 | 438 |
/// the iterator to some well-defined value. So you should consider it |
439 | 439 |
/// as uninitialized. |
440 | 440 |
GraphItemIt() {} |
441 | 441 |
|
442 | 442 |
/// \brief Copy constructor. |
443 | 443 |
/// |
444 | 444 |
/// Copy constructor. |
445 | 445 |
GraphItemIt(const GraphItemIt& it) : Item(it) {} |
446 | 446 |
|
447 | 447 |
/// \brief Constructor that sets the iterator to the first item. |
448 | 448 |
/// |
449 | 449 |
/// Constructor that sets the iterator to the first item. |
450 | 450 |
explicit GraphItemIt(const GR&) {} |
451 | 451 |
|
452 | 452 |
/// \brief Constructor for conversion from \c INVALID. |
453 | 453 |
/// |
454 | 454 |
/// Constructor for conversion from \c INVALID. |
455 | 455 |
/// It initializes the iterator to be invalid. |
456 | 456 |
/// \sa Invalid for more details. |
457 | 457 |
GraphItemIt(Invalid) {} |
458 | 458 |
|
459 | 459 |
/// \brief Assignment operator. |
460 | 460 |
/// |
461 | 461 |
/// Assignment operator for the iterator. |
462 | 462 |
GraphItemIt& operator=(const GraphItemIt&) { return *this; } |
463 | 463 |
|
464 | 464 |
/// \brief Increment the iterator. |
465 | 465 |
/// |
466 | 466 |
/// This operator increments the iterator, i.e. assigns it to the |
467 | 467 |
/// next item. |
468 | 468 |
GraphItemIt& operator++() { return *this; } |
469 |
|
|
469 |
|
|
470 | 470 |
/// \brief Equality operator |
471 | 471 |
/// |
472 | 472 |
/// Equality operator. |
473 | 473 |
/// Two iterators are equal if and only if they point to the |
474 | 474 |
/// same object or both are invalid. |
475 | 475 |
bool operator==(const GraphItemIt&) const { return true;} |
476 | 476 |
|
477 | 477 |
/// \brief Inequality operator |
478 | 478 |
/// |
479 | 479 |
/// Inequality operator. |
480 | 480 |
/// Two iterators are equal if and only if they point to the |
481 | 481 |
/// same object or both are invalid. |
482 | 482 |
bool operator!=(const GraphItemIt&) const { return true;} |
483 | 483 |
|
484 | 484 |
template<typename _GraphItemIt> |
485 | 485 |
struct Constraints { |
486 | 486 |
void constraints() { |
487 | 487 |
checkConcept<GraphItem<>, _GraphItemIt>(); |
488 | 488 |
_GraphItemIt it1(g); |
489 | 489 |
_GraphItemIt it2; |
490 | 490 |
_GraphItemIt it3 = it1; |
491 | 491 |
_GraphItemIt it4 = INVALID; |
492 | 492 |
|
493 | 493 |
it2 = ++it1; |
494 | 494 |
++it2 = it1; |
495 | 495 |
++(++it1); |
496 | 496 |
|
497 | 497 |
Item bi = it1; |
498 | 498 |
bi = it2; |
499 | 499 |
} |
500 | 500 |
const GR& g; |
501 | 501 |
}; |
502 | 502 |
}; |
503 | 503 |
|
504 |
/// \brief Concept class for \c InArcIt, \c OutArcIt and |
|
504 |
/// \brief Concept class for \c InArcIt, \c OutArcIt and |
|
505 | 505 |
/// \c IncEdgeIt types. |
506 | 506 |
/// |
507 |
/// This class describes the concept of \c InArcIt, \c OutArcIt |
|
507 |
/// This class describes the concept of \c InArcIt, \c OutArcIt |
|
508 | 508 |
/// and \c IncEdgeIt subtypes of digraph and graph types. |
509 | 509 |
/// |
510 | 510 |
/// \note Since these iterator classes do not inherit from the same |
511 | 511 |
/// base class, there is an additional template parameter (selector) |
512 |
/// \c sel. For \c InArcIt you should instantiate it with character |
|
512 |
/// \c sel. For \c InArcIt you should instantiate it with character |
|
513 | 513 |
/// \c 'i', for \c OutArcIt with \c 'o' and for \c IncEdgeIt with \c 'e'. |
514 | 514 |
template <typename GR, |
515 | 515 |
typename Item = typename GR::Arc, |
516 | 516 |
typename Base = typename GR::Node, |
517 | 517 |
char sel = '0'> |
518 | 518 |
class GraphIncIt : public Item { |
519 | 519 |
public: |
520 | 520 |
/// \brief Default constructor. |
521 | 521 |
/// |
522 | 522 |
/// Default constructor. |
523 | 523 |
/// \warning The default constructor is not required to set |
524 | 524 |
/// the iterator to some well-defined value. So you should consider it |
525 | 525 |
/// as uninitialized. |
526 | 526 |
GraphIncIt() {} |
527 | 527 |
|
528 | 528 |
/// \brief Copy constructor. |
529 | 529 |
/// |
530 | 530 |
/// Copy constructor. |
531 | 531 |
GraphIncIt(const GraphIncIt& it) : Item(it) {} |
532 | 532 |
|
533 |
/// \brief Constructor that sets the iterator to the first |
|
533 |
/// \brief Constructor that sets the iterator to the first |
|
534 | 534 |
/// incoming or outgoing arc. |
535 | 535 |
/// |
536 |
/// Constructor that sets the iterator to the first arc |
|
536 |
/// Constructor that sets the iterator to the first arc |
|
537 | 537 |
/// incoming to or outgoing from the given node. |
538 | 538 |
explicit GraphIncIt(const GR&, const Base&) {} |
539 | 539 |
|
540 | 540 |
/// \brief Constructor for conversion from \c INVALID. |
541 | 541 |
/// |
542 | 542 |
/// Constructor for conversion from \c INVALID. |
543 | 543 |
/// It initializes the iterator to be invalid. |
544 | 544 |
/// \sa Invalid for more details. |
545 | 545 |
GraphIncIt(Invalid) {} |
546 | 546 |
|
547 | 547 |
/// \brief Assignment operator. |
548 | 548 |
/// |
549 | 549 |
/// Assignment operator for the iterator. |
550 | 550 |
GraphIncIt& operator=(const GraphIncIt&) { return *this; } |
551 | 551 |
|
552 | 552 |
/// \brief Increment the iterator. |
553 | 553 |
/// |
554 | 554 |
/// This operator increments the iterator, i.e. assigns it to the |
555 | 555 |
/// next arc incoming to or outgoing from the given node. |
556 | 556 |
GraphIncIt& operator++() { return *this; } |
557 | 557 |
|
558 | 558 |
/// \brief Equality operator |
559 | 559 |
/// |
560 | 560 |
/// Equality operator. |
561 | 561 |
/// Two iterators are equal if and only if they point to the |
562 | 562 |
/// same object or both are invalid. |
563 | 563 |
bool operator==(const GraphIncIt&) const { return true;} |
564 | 564 |
|
565 | 565 |
/// \brief Inequality operator |
566 | 566 |
/// |
567 | 567 |
/// Inequality operator. |
568 | 568 |
/// Two iterators are equal if and only if they point to the |
569 | 569 |
/// same object or both are invalid. |
570 | 570 |
bool operator!=(const GraphIncIt&) const { return true;} |
571 | 571 |
|
572 | 572 |
template <typename _GraphIncIt> |
573 | 573 |
struct Constraints { |
574 | 574 |
void constraints() { |
575 | 575 |
checkConcept<GraphItem<sel>, _GraphIncIt>(); |
576 | 576 |
_GraphIncIt it1(graph, node); |
577 | 577 |
_GraphIncIt it2; |
578 | 578 |
_GraphIncIt it3 = it1; |
579 | 579 |
_GraphIncIt it4 = INVALID; |
580 | 580 |
|
581 | 581 |
it2 = ++it1; |
582 | 582 |
++it2 = it1; |
583 | 583 |
++(++it1); |
584 | 584 |
Item e = it1; |
585 | 585 |
e = it2; |
586 | 586 |
} |
587 | 587 |
const Base& node; |
588 | 588 |
const GR& graph; |
589 | 589 |
}; |
590 | 590 |
}; |
591 | 591 |
|
592 | 592 |
/// \brief Skeleton class for iterable directed graphs. |
593 | 593 |
/// |
594 | 594 |
/// This class describes the interface of iterable directed |
595 | 595 |
/// graphs. It extends \ref BaseDigraphComponent with the core |
596 | 596 |
/// iterable interface. |
597 | 597 |
/// This concept is part of the Digraph concept. |
598 | 598 |
template <typename BAS = BaseDigraphComponent> |
599 | 599 |
class IterableDigraphComponent : public BAS { |
600 | 600 |
|
... | ... |
@@ -743,138 +743,138 @@ |
743 | 743 |
{ |
744 | 744 |
checkConcept<GraphItemIt<_Digraph, typename _Digraph::Arc>, |
745 | 745 |
typename _Digraph::ArcIt >(); |
746 | 746 |
checkConcept<GraphItemIt<_Digraph, typename _Digraph::Node>, |
747 | 747 |
typename _Digraph::NodeIt >(); |
748 | 748 |
checkConcept<GraphIncIt<_Digraph, typename _Digraph::Arc, |
749 | 749 |
typename _Digraph::Node, 'i'>, typename _Digraph::InArcIt>(); |
750 | 750 |
checkConcept<GraphIncIt<_Digraph, typename _Digraph::Arc, |
751 | 751 |
typename _Digraph::Node, 'o'>, typename _Digraph::OutArcIt>(); |
752 | 752 |
|
753 | 753 |
typename _Digraph::Node n; |
754 | 754 |
const typename _Digraph::InArcIt iait(INVALID); |
755 | 755 |
const typename _Digraph::OutArcIt oait(INVALID); |
756 | 756 |
n = digraph.baseNode(iait); |
757 | 757 |
n = digraph.runningNode(iait); |
758 | 758 |
n = digraph.baseNode(oait); |
759 | 759 |
n = digraph.runningNode(oait); |
760 | 760 |
ignore_unused_variable_warning(n); |
761 | 761 |
} |
762 | 762 |
} |
763 | 763 |
|
764 | 764 |
const _Digraph& digraph; |
765 | 765 |
}; |
766 | 766 |
}; |
767 | 767 |
|
768 | 768 |
/// \brief Skeleton class for iterable undirected graphs. |
769 | 769 |
/// |
770 | 770 |
/// This class describes the interface of iterable undirected |
771 | 771 |
/// graphs. It extends \ref IterableDigraphComponent with the core |
772 | 772 |
/// iterable interface of undirected graphs. |
773 | 773 |
/// This concept is part of the Graph concept. |
774 | 774 |
template <typename BAS = BaseGraphComponent> |
775 | 775 |
class IterableGraphComponent : public IterableDigraphComponent<BAS> { |
776 | 776 |
public: |
777 | 777 |
|
778 | 778 |
typedef BAS Base; |
779 | 779 |
typedef typename Base::Node Node; |
780 | 780 |
typedef typename Base::Arc Arc; |
781 | 781 |
typedef typename Base::Edge Edge; |
782 | 782 |
|
783 | 783 |
|
784 | 784 |
typedef IterableGraphComponent Graph; |
785 | 785 |
|
786 | 786 |
/// \name Base Iteration |
787 | 787 |
/// |
788 | 788 |
/// This interface provides functions for iteration on edges. |
789 | 789 |
/// |
790 | 790 |
/// @{ |
791 | 791 |
|
792 | 792 |
using IterableDigraphComponent<Base>::first; |
793 | 793 |
using IterableDigraphComponent<Base>::next; |
794 | 794 |
|
795 | 795 |
/// \brief Return the first edge. |
796 | 796 |
/// |
797 | 797 |
/// This function gives back the first edge in the iteration order. |
798 | 798 |
void first(Edge&) const {} |
799 | 799 |
|
800 | 800 |
/// \brief Return the next edge. |
801 | 801 |
/// |
802 | 802 |
/// This function gives back the next edge in the iteration order. |
803 | 803 |
void next(Edge&) const {} |
804 | 804 |
|
805 | 805 |
/// \brief Return the first edge incident to the given node. |
806 | 806 |
/// |
807 |
/// This function gives back the first edge incident to the given |
|
807 |
/// This function gives back the first edge incident to the given |
|
808 | 808 |
/// node. The bool parameter gives back the direction for which the |
809 |
/// source node of the directed arc representing the edge is the |
|
809 |
/// source node of the directed arc representing the edge is the |
|
810 | 810 |
/// given node. |
811 | 811 |
void firstInc(Edge&, bool&, const Node&) const {} |
812 | 812 |
|
813 | 813 |
/// \brief Gives back the next of the edges from the |
814 | 814 |
/// given node. |
815 | 815 |
/// |
816 |
/// This function gives back the next edge incident to the given |
|
816 |
/// This function gives back the next edge incident to the given |
|
817 | 817 |
/// node. The bool parameter should be used as \c firstInc() use it. |
818 | 818 |
void nextInc(Edge&, bool&) const {} |
819 | 819 |
|
820 | 820 |
using IterableDigraphComponent<Base>::baseNode; |
821 | 821 |
using IterableDigraphComponent<Base>::runningNode; |
822 | 822 |
|
823 | 823 |
/// @} |
824 | 824 |
|
825 | 825 |
/// \name Class Based Iteration |
826 | 826 |
/// |
827 | 827 |
/// This interface provides iterator classes for edges. |
828 | 828 |
/// |
829 | 829 |
/// @{ |
830 | 830 |
|
831 | 831 |
/// \brief This iterator goes through each edge. |
832 | 832 |
/// |
833 | 833 |
/// This iterator goes through each edge. |
834 | 834 |
typedef GraphItemIt<Graph, Edge> EdgeIt; |
835 | 835 |
|
836 | 836 |
/// \brief This iterator goes trough the incident edges of a |
837 | 837 |
/// node. |
838 | 838 |
/// |
839 | 839 |
/// This iterator goes trough the incident edges of a certain |
840 | 840 |
/// node of a graph. |
841 | 841 |
typedef GraphIncIt<Graph, Edge, Node, 'e'> IncEdgeIt; |
842 | 842 |
|
843 | 843 |
/// \brief The base node of the iterator. |
844 | 844 |
/// |
845 | 845 |
/// This function gives back the base node of the iterator. |
846 | 846 |
Node baseNode(const IncEdgeIt&) const { return INVALID; } |
847 | 847 |
|
848 | 848 |
/// \brief The running node of the iterator. |
849 | 849 |
/// |
850 | 850 |
/// This function gives back the running node of the iterator. |
851 | 851 |
Node runningNode(const IncEdgeIt&) const { return INVALID; } |
852 | 852 |
|
853 | 853 |
/// @} |
854 | 854 |
|
855 | 855 |
template <typename _Graph> |
856 | 856 |
struct Constraints { |
857 | 857 |
void constraints() { |
858 | 858 |
checkConcept<IterableDigraphComponent<Base>, _Graph>(); |
859 | 859 |
|
860 | 860 |
{ |
861 | 861 |
typename _Graph::Node node(INVALID); |
862 | 862 |
typename _Graph::Edge edge(INVALID); |
863 | 863 |
bool dir; |
864 | 864 |
{ |
865 | 865 |
graph.first(edge); |
866 | 866 |
graph.next(edge); |
867 | 867 |
} |
868 | 868 |
{ |
869 | 869 |
graph.firstInc(edge, dir, node); |
870 | 870 |
graph.nextInc(edge, dir); |
871 | 871 |
} |
872 | 872 |
|
873 | 873 |
} |
874 | 874 |
|
875 | 875 |
{ |
876 | 876 |
checkConcept<GraphItemIt<_Graph, typename _Graph::Edge>, |
877 | 877 |
typename _Graph::EdgeIt >(); |
878 | 878 |
checkConcept<GraphIncIt<_Graph, typename _Graph::Edge, |
879 | 879 |
typename _Graph::Node, 'e'>, typename _Graph::IncEdgeIt>(); |
880 | 880 |
|
... | ... |
@@ -929,207 +929,207 @@ |
929 | 929 |
} |
930 | 930 |
|
931 | 931 |
template <typename _Digraph> |
932 | 932 |
struct Constraints { |
933 | 933 |
void constraints() { |
934 | 934 |
checkConcept<Base, _Digraph>(); |
935 | 935 |
typename _Digraph::NodeNotifier& nn |
936 | 936 |
= digraph.notifier(typename _Digraph::Node()); |
937 | 937 |
|
938 | 938 |
typename _Digraph::ArcNotifier& en |
939 | 939 |
= digraph.notifier(typename _Digraph::Arc()); |
940 | 940 |
|
941 | 941 |
ignore_unused_variable_warning(nn); |
942 | 942 |
ignore_unused_variable_warning(en); |
943 | 943 |
} |
944 | 944 |
|
945 | 945 |
const _Digraph& digraph; |
946 | 946 |
}; |
947 | 947 |
}; |
948 | 948 |
|
949 | 949 |
/// \brief Skeleton class for alterable undirected graphs. |
950 | 950 |
/// |
951 | 951 |
/// This class describes the interface of alterable undirected |
952 | 952 |
/// graphs. It extends \ref AlterableDigraphComponent with the alteration |
953 | 953 |
/// notifier interface of undirected graphs. It implements |
954 | 954 |
/// an observer-notifier pattern for the edges. More |
955 | 955 |
/// obsevers can be registered into the notifier and whenever an |
956 | 956 |
/// alteration occured in the graph all the observers will be |
957 | 957 |
/// notified about it. |
958 | 958 |
template <typename BAS = BaseGraphComponent> |
959 | 959 |
class AlterableGraphComponent : public AlterableDigraphComponent<BAS> { |
960 | 960 |
public: |
961 | 961 |
|
962 | 962 |
typedef BAS Base; |
963 | 963 |
typedef typename Base::Edge Edge; |
964 | 964 |
|
965 | 965 |
|
966 | 966 |
/// Edge alteration notifier class. |
967 | 967 |
typedef AlterationNotifier<AlterableGraphComponent, Edge> |
968 | 968 |
EdgeNotifier; |
969 | 969 |
|
970 | 970 |
/// \brief Return the edge alteration notifier. |
971 | 971 |
/// |
972 | 972 |
/// This function gives back the edge alteration notifier. |
973 | 973 |
EdgeNotifier& notifier(Edge) const { |
974 | 974 |
return EdgeNotifier(); |
975 | 975 |
} |
976 | 976 |
|
977 | 977 |
template <typename _Graph> |
978 | 978 |
struct Constraints { |
979 | 979 |
void constraints() { |
980 | 980 |
checkConcept<AlterableDigraphComponent<Base>, _Graph>(); |
981 | 981 |
typename _Graph::EdgeNotifier& uen |
982 | 982 |
= graph.notifier(typename _Graph::Edge()); |
983 | 983 |
ignore_unused_variable_warning(uen); |
984 | 984 |
} |
985 | 985 |
|
986 | 986 |
const _Graph& graph; |
987 | 987 |
}; |
988 | 988 |
}; |
989 | 989 |
|
990 | 990 |
/// \brief Concept class for standard graph maps. |
991 | 991 |
/// |
992 | 992 |
/// This class describes the concept of standard graph maps, i.e. |
993 |
/// the \c NodeMap, \c ArcMap and \c EdgeMap subtypes of digraph and |
|
993 |
/// the \c NodeMap, \c ArcMap and \c EdgeMap subtypes of digraph and |
|
994 | 994 |
/// graph types, which can be used for associating data to graph items. |
995 | 995 |
/// The standard graph maps must conform to the ReferenceMap concept. |
996 | 996 |
template <typename GR, typename K, typename V> |
997 | 997 |
class GraphMap : public ReferenceMap<K, V, V&, const V&> { |
998 | 998 |
typedef ReferenceMap<K, V, V&, const V&> Parent; |
999 | 999 |
|
1000 | 1000 |
public: |
1001 | 1001 |
|
1002 | 1002 |
/// The key type of the map. |
1003 | 1003 |
typedef K Key; |
1004 | 1004 |
/// The value type of the map. |
1005 | 1005 |
typedef V Value; |
1006 | 1006 |
/// The reference type of the map. |
1007 | 1007 |
typedef Value& Reference; |
1008 | 1008 |
/// The const reference type of the map. |
1009 | 1009 |
typedef const Value& ConstReference; |
1010 | 1010 |
|
1011 | 1011 |
// The reference map tag. |
1012 | 1012 |
typedef True ReferenceMapTag; |
1013 | 1013 |
|
1014 | 1014 |
/// \brief Construct a new map. |
1015 | 1015 |
/// |
1016 | 1016 |
/// Construct a new map for the graph. |
1017 | 1017 |
explicit GraphMap(const GR&) {} |
1018 | 1018 |
/// \brief Construct a new map with default value. |
1019 | 1019 |
/// |
1020 | 1020 |
/// Construct a new map for the graph and initalize the values. |
1021 | 1021 |
GraphMap(const GR&, const Value&) {} |
1022 | 1022 |
|
1023 | 1023 |
private: |
1024 | 1024 |
/// \brief Copy constructor. |
1025 | 1025 |
/// |
1026 | 1026 |
/// Copy Constructor. |
1027 | 1027 |
GraphMap(const GraphMap&) : Parent() {} |
1028 | 1028 |
|
1029 | 1029 |
/// \brief Assignment operator. |
1030 | 1030 |
/// |
1031 | 1031 |
/// Assignment operator. It does not mofify the underlying graph, |
1032 | 1032 |
/// it just iterates on the current item set and set the map |
1033 | 1033 |
/// with the value returned by the assigned map. |
1034 | 1034 |
template <typename CMap> |
1035 | 1035 |
GraphMap& operator=(const CMap&) { |
1036 | 1036 |
checkConcept<ReadMap<Key, Value>, CMap>(); |
1037 | 1037 |
return *this; |
1038 | 1038 |
} |
1039 | 1039 |
|
1040 | 1040 |
public: |
1041 | 1041 |
template<typename _Map> |
1042 | 1042 |
struct Constraints { |
1043 | 1043 |
void constraints() { |
1044 | 1044 |
checkConcept |
1045 | 1045 |
<ReferenceMap<Key, Value, Value&, const Value&>, _Map>(); |
1046 | 1046 |
_Map m1(g); |
1047 | 1047 |
_Map m2(g,t); |
1048 |
|
|
1048 |
|
|
1049 | 1049 |
// Copy constructor |
1050 | 1050 |
// _Map m3(m); |
1051 | 1051 |
|
1052 | 1052 |
// Assignment operator |
1053 | 1053 |
// ReadMap<Key, Value> cmap; |
1054 | 1054 |
// m3 = cmap; |
1055 | 1055 |
|
1056 | 1056 |
ignore_unused_variable_warning(m1); |
1057 | 1057 |
ignore_unused_variable_warning(m2); |
1058 | 1058 |
// ignore_unused_variable_warning(m3); |
1059 | 1059 |
} |
1060 | 1060 |
|
1061 | 1061 |
const _Map &m; |
1062 | 1062 |
const GR &g; |
1063 | 1063 |
const typename GraphMap::Value &t; |
1064 | 1064 |
}; |
1065 | 1065 |
|
1066 | 1066 |
}; |
1067 | 1067 |
|
1068 | 1068 |
/// \brief Skeleton class for mappable directed graphs. |
1069 | 1069 |
/// |
1070 | 1070 |
/// This class describes the interface of mappable directed graphs. |
1071 |
/// It extends \ref BaseDigraphComponent with the standard digraph |
|
1071 |
/// It extends \ref BaseDigraphComponent with the standard digraph |
|
1072 | 1072 |
/// map classes, namely \c NodeMap and \c ArcMap. |
1073 | 1073 |
/// This concept is part of the Digraph concept. |
1074 | 1074 |
template <typename BAS = BaseDigraphComponent> |
1075 | 1075 |
class MappableDigraphComponent : public BAS { |
1076 | 1076 |
public: |
1077 | 1077 |
|
1078 | 1078 |
typedef BAS Base; |
1079 | 1079 |
typedef typename Base::Node Node; |
1080 | 1080 |
typedef typename Base::Arc Arc; |
1081 | 1081 |
|
1082 | 1082 |
typedef MappableDigraphComponent Digraph; |
1083 | 1083 |
|
1084 | 1084 |
/// \brief Standard graph map for the nodes. |
1085 | 1085 |
/// |
1086 | 1086 |
/// Standard graph map for the nodes. |
1087 | 1087 |
/// It conforms to the ReferenceMap concept. |
1088 | 1088 |
template <typename V> |
1089 | 1089 |
class NodeMap : public GraphMap<MappableDigraphComponent, Node, V> { |
1090 | 1090 |
typedef GraphMap<MappableDigraphComponent, Node, V> Parent; |
1091 | 1091 |
|
1092 | 1092 |
public: |
1093 | 1093 |
/// \brief Construct a new map. |
1094 | 1094 |
/// |
1095 | 1095 |
/// Construct a new map for the digraph. |
1096 | 1096 |
explicit NodeMap(const MappableDigraphComponent& digraph) |
1097 | 1097 |
: Parent(digraph) {} |
1098 | 1098 |
|
1099 | 1099 |
/// \brief Construct a new map with default value. |
1100 | 1100 |
/// |
1101 | 1101 |
/// Construct a new map for the digraph and initalize the values. |
1102 | 1102 |
NodeMap(const MappableDigraphComponent& digraph, const V& value) |
1103 | 1103 |
: Parent(digraph, value) {} |
1104 | 1104 |
|
1105 | 1105 |
private: |
1106 | 1106 |
/// \brief Copy constructor. |
1107 | 1107 |
/// |
1108 | 1108 |
/// Copy Constructor. |
1109 | 1109 |
NodeMap(const NodeMap& nm) : Parent(nm) {} |
1110 | 1110 |
|
1111 | 1111 |
/// \brief Assignment operator. |
1112 | 1112 |
/// |
1113 | 1113 |
/// Assignment operator. |
1114 | 1114 |
template <typename CMap> |
1115 | 1115 |
NodeMap& operator=(const CMap&) { |
1116 | 1116 |
checkConcept<ReadMap<Node, V>, CMap>(); |
1117 | 1117 |
return *this; |
1118 | 1118 |
} |
1119 | 1119 |
|
1120 | 1120 |
}; |
1121 | 1121 |
|
1122 | 1122 |
/// \brief Standard graph map for the arcs. |
1123 | 1123 |
/// |
1124 | 1124 |
/// Standard graph map for the arcs. |
1125 | 1125 |
/// It conforms to the ReferenceMap concept. |
1126 | 1126 |
template <typename V> |
1127 | 1127 |
class ArcMap : public GraphMap<MappableDigraphComponent, Arc, V> { |
1128 | 1128 |
typedef GraphMap<MappableDigraphComponent, Arc, V> Parent; |
1129 | 1129 |
|
1130 | 1130 |
public: |
1131 | 1131 |
/// \brief Construct a new map. |
1132 | 1132 |
/// |
1133 | 1133 |
/// Construct a new map for the digraph. |
1134 | 1134 |
explicit ArcMap(const MappableDigraphComponent& digraph) |
1135 | 1135 |
: Parent(digraph) {} |
... | ... |
@@ -1144,341 +1144,341 @@ |
1144 | 1144 |
/// \brief Copy constructor. |
1145 | 1145 |
/// |
1146 | 1146 |
/// Copy Constructor. |
1147 | 1147 |
ArcMap(const ArcMap& nm) : Parent(nm) {} |
1148 | 1148 |
|
1149 | 1149 |
/// \brief Assignment operator. |
1150 | 1150 |
/// |
1151 | 1151 |
/// Assignment operator. |
1152 | 1152 |
template <typename CMap> |
1153 | 1153 |
ArcMap& operator=(const CMap&) { |
1154 | 1154 |
checkConcept<ReadMap<Arc, V>, CMap>(); |
1155 | 1155 |
return *this; |
1156 | 1156 |
} |
1157 | 1157 |
|
1158 | 1158 |
}; |
1159 | 1159 |
|
1160 | 1160 |
|
1161 | 1161 |
template <typename _Digraph> |
1162 | 1162 |
struct Constraints { |
1163 | 1163 |
|
1164 | 1164 |
struct Dummy { |
1165 | 1165 |
int value; |
1166 | 1166 |
Dummy() : value(0) {} |
1167 | 1167 |
Dummy(int _v) : value(_v) {} |
1168 | 1168 |
}; |
1169 | 1169 |
|
1170 | 1170 |
void constraints() { |
1171 | 1171 |
checkConcept<Base, _Digraph>(); |
1172 | 1172 |
{ // int map test |
1173 | 1173 |
typedef typename _Digraph::template NodeMap<int> IntNodeMap; |
1174 | 1174 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Node, int>, |
1175 | 1175 |
IntNodeMap >(); |
1176 | 1176 |
} { // bool map test |
1177 | 1177 |
typedef typename _Digraph::template NodeMap<bool> BoolNodeMap; |
1178 | 1178 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Node, bool>, |
1179 | 1179 |
BoolNodeMap >(); |
1180 | 1180 |
} { // Dummy map test |
1181 | 1181 |
typedef typename _Digraph::template NodeMap<Dummy> DummyNodeMap; |
1182 | 1182 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Node, Dummy>, |
1183 | 1183 |
DummyNodeMap >(); |
1184 | 1184 |
} |
1185 | 1185 |
|
1186 | 1186 |
{ // int map test |
1187 | 1187 |
typedef typename _Digraph::template ArcMap<int> IntArcMap; |
1188 | 1188 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, int>, |
1189 | 1189 |
IntArcMap >(); |
1190 | 1190 |
} { // bool map test |
1191 | 1191 |
typedef typename _Digraph::template ArcMap<bool> BoolArcMap; |
1192 | 1192 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, bool>, |
1193 | 1193 |
BoolArcMap >(); |
1194 | 1194 |
} { // Dummy map test |
1195 | 1195 |
typedef typename _Digraph::template ArcMap<Dummy> DummyArcMap; |
1196 | 1196 |
checkConcept<GraphMap<_Digraph, typename _Digraph::Arc, Dummy>, |
1197 | 1197 |
DummyArcMap >(); |
1198 | 1198 |
} |
1199 | 1199 |
} |
1200 | 1200 |
|
1201 | 1201 |
const _Digraph& digraph; |
1202 | 1202 |
}; |
1203 | 1203 |
}; |
1204 | 1204 |
|
1205 | 1205 |
/// \brief Skeleton class for mappable undirected graphs. |
1206 | 1206 |
/// |
1207 | 1207 |
/// This class describes the interface of mappable undirected graphs. |
1208 |
/// It extends \ref MappableDigraphComponent with the standard graph |
|
1208 |
/// It extends \ref MappableDigraphComponent with the standard graph |
|
1209 | 1209 |
/// map class for edges (\c EdgeMap). |
1210 | 1210 |
/// This concept is part of the Graph concept. |
1211 | 1211 |
template <typename BAS = BaseGraphComponent> |
1212 | 1212 |
class MappableGraphComponent : public MappableDigraphComponent<BAS> { |
1213 | 1213 |
public: |
1214 | 1214 |
|
1215 | 1215 |
typedef BAS Base; |
1216 | 1216 |
typedef typename Base::Edge Edge; |
1217 | 1217 |
|
1218 | 1218 |
typedef MappableGraphComponent Graph; |
1219 | 1219 |
|
1220 | 1220 |
/// \brief Standard graph map for the edges. |
1221 | 1221 |
/// |
1222 | 1222 |
/// Standard graph map for the edges. |
1223 | 1223 |
/// It conforms to the ReferenceMap concept. |
1224 | 1224 |
template <typename V> |
1225 | 1225 |
class EdgeMap : public GraphMap<MappableGraphComponent, Edge, V> { |
1226 | 1226 |
typedef GraphMap<MappableGraphComponent, Edge, V> Parent; |
1227 | 1227 |
|
1228 | 1228 |
public: |
1229 | 1229 |
/// \brief Construct a new map. |
1230 | 1230 |
/// |
1231 | 1231 |
/// Construct a new map for the graph. |
1232 | 1232 |
explicit EdgeMap(const MappableGraphComponent& graph) |
1233 | 1233 |
: Parent(graph) {} |
1234 | 1234 |
|
1235 | 1235 |
/// \brief Construct a new map with default value. |
1236 | 1236 |
/// |
1237 | 1237 |
/// Construct a new map for the graph and initalize the values. |
1238 | 1238 |
EdgeMap(const MappableGraphComponent& graph, const V& value) |
1239 | 1239 |
: Parent(graph, value) {} |
1240 | 1240 |
|
1241 | 1241 |
private: |
1242 | 1242 |
/// \brief Copy constructor. |
1243 | 1243 |
/// |
1244 | 1244 |
/// Copy Constructor. |
1245 | 1245 |
EdgeMap(const EdgeMap& nm) : Parent(nm) {} |
1246 | 1246 |
|
1247 | 1247 |
/// \brief Assignment operator. |
1248 | 1248 |
/// |
1249 | 1249 |
/// Assignment operator. |
1250 | 1250 |
template <typename CMap> |
1251 | 1251 |
EdgeMap& operator=(const CMap&) { |
1252 | 1252 |
checkConcept<ReadMap<Edge, V>, CMap>(); |
1253 | 1253 |
return *this; |
1254 | 1254 |
} |
1255 | 1255 |
|
1256 | 1256 |
}; |
1257 | 1257 |
|
1258 | 1258 |
|
1259 | 1259 |
template <typename _Graph> |
1260 | 1260 |
struct Constraints { |
1261 | 1261 |
|
1262 | 1262 |
struct Dummy { |
1263 | 1263 |
int value; |
1264 | 1264 |
Dummy() : value(0) {} |
1265 | 1265 |
Dummy(int _v) : value(_v) {} |
1266 | 1266 |
}; |
1267 | 1267 |
|
1268 | 1268 |
void constraints() { |
1269 | 1269 |
checkConcept<MappableDigraphComponent<Base>, _Graph>(); |
1270 | 1270 |
|
1271 | 1271 |
{ // int map test |
1272 | 1272 |
typedef typename _Graph::template EdgeMap<int> IntEdgeMap; |
1273 | 1273 |
checkConcept<GraphMap<_Graph, typename _Graph::Edge, int>, |
1274 | 1274 |
IntEdgeMap >(); |
1275 | 1275 |
} { // bool map test |
1276 | 1276 |
typedef typename _Graph::template EdgeMap<bool> BoolEdgeMap; |
1277 | 1277 |
checkConcept<GraphMap<_Graph, typename _Graph::Edge, bool>, |
1278 | 1278 |
BoolEdgeMap >(); |
1279 | 1279 |
} { // Dummy map test |
1280 | 1280 |
typedef typename _Graph::template EdgeMap<Dummy> DummyEdgeMap; |
1281 | 1281 |
checkConcept<GraphMap<_Graph, typename _Graph::Edge, Dummy>, |
1282 | 1282 |
DummyEdgeMap >(); |
1283 | 1283 |
} |
1284 | 1284 |
} |
1285 | 1285 |
|
1286 | 1286 |
const _Graph& graph; |
1287 | 1287 |
}; |
1288 | 1288 |
}; |
1289 | 1289 |
|
1290 | 1290 |
/// \brief Skeleton class for extendable directed graphs. |
1291 | 1291 |
/// |
1292 | 1292 |
/// This class describes the interface of extendable directed graphs. |
1293 |
/// It extends \ref BaseDigraphComponent with functions for adding |
|
1293 |
/// It extends \ref BaseDigraphComponent with functions for adding |
|
1294 | 1294 |
/// nodes and arcs to the digraph. |
1295 | 1295 |
/// This concept requires \ref AlterableDigraphComponent. |
1296 | 1296 |
template <typename BAS = BaseDigraphComponent> |
1297 | 1297 |
class ExtendableDigraphComponent : public BAS { |
1298 | 1298 |
public: |
1299 | 1299 |
typedef BAS Base; |
1300 | 1300 |
|
1301 | 1301 |
typedef typename Base::Node Node; |
1302 | 1302 |
typedef typename Base::Arc Arc; |
1303 | 1303 |
|
1304 | 1304 |
/// \brief Add a new node to the digraph. |
1305 | 1305 |
/// |
1306 | 1306 |
/// This function adds a new node to the digraph. |
1307 | 1307 |
Node addNode() { |
1308 | 1308 |
return INVALID; |
1309 | 1309 |
} |
1310 | 1310 |
|
1311 | 1311 |
/// \brief Add a new arc connecting the given two nodes. |
1312 | 1312 |
/// |
1313 | 1313 |
/// This function adds a new arc connecting the given two nodes |
1314 | 1314 |
/// of the digraph. |
1315 | 1315 |
Arc addArc(const Node&, const Node&) { |
1316 | 1316 |
return INVALID; |
1317 | 1317 |
} |
1318 | 1318 |
|
1319 | 1319 |
template <typename _Digraph> |
1320 | 1320 |
struct Constraints { |
1321 | 1321 |
void constraints() { |
1322 | 1322 |
checkConcept<Base, _Digraph>(); |
1323 | 1323 |
typename _Digraph::Node node_a, node_b; |
1324 | 1324 |
node_a = digraph.addNode(); |
1325 | 1325 |
node_b = digraph.addNode(); |
1326 | 1326 |
typename _Digraph::Arc arc; |
1327 | 1327 |
arc = digraph.addArc(node_a, node_b); |
1328 | 1328 |
} |
1329 | 1329 |
|
1330 | 1330 |
_Digraph& digraph; |
1331 | 1331 |
}; |
1332 | 1332 |
}; |
1333 | 1333 |
|
1334 | 1334 |
/// \brief Skeleton class for extendable undirected graphs. |
1335 | 1335 |
/// |
1336 | 1336 |
/// This class describes the interface of extendable undirected graphs. |
1337 |
/// It extends \ref BaseGraphComponent with functions for adding |
|
1337 |
/// It extends \ref BaseGraphComponent with functions for adding |
|
1338 | 1338 |
/// nodes and edges to the graph. |
1339 | 1339 |
/// This concept requires \ref AlterableGraphComponent. |
1340 | 1340 |
template <typename BAS = BaseGraphComponent> |
1341 | 1341 |
class ExtendableGraphComponent : public BAS { |
1342 | 1342 |
public: |
1343 | 1343 |
|
1344 | 1344 |
typedef BAS Base; |
1345 | 1345 |
typedef typename Base::Node Node; |
1346 | 1346 |
typedef typename Base::Edge Edge; |
1347 | 1347 |
|
1348 | 1348 |
/// \brief Add a new node to the digraph. |
1349 | 1349 |
/// |
1350 | 1350 |
/// This function adds a new node to the digraph. |
1351 | 1351 |
Node addNode() { |
1352 | 1352 |
return INVALID; |
1353 | 1353 |
} |
1354 | 1354 |
|
1355 | 1355 |
/// \brief Add a new edge connecting the given two nodes. |
1356 | 1356 |
/// |
1357 | 1357 |
/// This function adds a new edge connecting the given two nodes |
1358 | 1358 |
/// of the graph. |
1359 | 1359 |
Edge addEdge(const Node&, const Node&) { |
1360 | 1360 |
return INVALID; |
1361 | 1361 |
} |
1362 | 1362 |
|
1363 | 1363 |
template <typename _Graph> |
1364 | 1364 |
struct Constraints { |
1365 | 1365 |
void constraints() { |
1366 | 1366 |
checkConcept<Base, _Graph>(); |
1367 | 1367 |
typename _Graph::Node node_a, node_b; |
1368 | 1368 |
node_a = graph.addNode(); |
1369 | 1369 |
node_b = graph.addNode(); |
1370 | 1370 |
typename _Graph::Edge edge; |
1371 | 1371 |
edge = graph.addEdge(node_a, node_b); |
1372 | 1372 |
} |
1373 | 1373 |
|
1374 | 1374 |
_Graph& graph; |
1375 | 1375 |
}; |
1376 | 1376 |
}; |
1377 | 1377 |
|
1378 | 1378 |
/// \brief Skeleton class for erasable directed graphs. |
1379 | 1379 |
/// |
1380 | 1380 |
/// This class describes the interface of erasable directed graphs. |
1381 |
/// It extends \ref BaseDigraphComponent with functions for removing |
|
1381 |
/// It extends \ref BaseDigraphComponent with functions for removing |
|
1382 | 1382 |
/// nodes and arcs from the digraph. |
1383 | 1383 |
/// This concept requires \ref AlterableDigraphComponent. |
1384 | 1384 |
template <typename BAS = BaseDigraphComponent> |
1385 | 1385 |
class ErasableDigraphComponent : public BAS { |
1386 | 1386 |
public: |
1387 | 1387 |
|
1388 | 1388 |
typedef BAS Base; |
1389 | 1389 |
typedef typename Base::Node Node; |
1390 | 1390 |
typedef typename Base::Arc Arc; |
1391 | 1391 |
|
1392 | 1392 |
/// \brief Erase a node from the digraph. |
1393 | 1393 |
/// |
1394 |
/// This function erases the given node from the digraph and all arcs |
|
1394 |
/// This function erases the given node from the digraph and all arcs |
|
1395 | 1395 |
/// connected to the node. |
1396 | 1396 |
void erase(const Node&) {} |
1397 | 1397 |
|
1398 | 1398 |
/// \brief Erase an arc from the digraph. |
1399 | 1399 |
/// |
1400 | 1400 |
/// This function erases the given arc from the digraph. |
1401 | 1401 |
void erase(const Arc&) {} |
1402 | 1402 |
|
1403 | 1403 |
template <typename _Digraph> |
1404 | 1404 |
struct Constraints { |
1405 | 1405 |
void constraints() { |
1406 | 1406 |
checkConcept<Base, _Digraph>(); |
1407 | 1407 |
const typename _Digraph::Node node(INVALID); |
1408 | 1408 |
digraph.erase(node); |
1409 | 1409 |
const typename _Digraph::Arc arc(INVALID); |
1410 | 1410 |
digraph.erase(arc); |
1411 | 1411 |
} |
1412 | 1412 |
|
1413 | 1413 |
_Digraph& digraph; |
1414 | 1414 |
}; |
1415 | 1415 |
}; |
1416 | 1416 |
|
1417 | 1417 |
/// \brief Skeleton class for erasable undirected graphs. |
1418 | 1418 |
/// |
1419 | 1419 |
/// This class describes the interface of erasable undirected graphs. |
1420 |
/// It extends \ref BaseGraphComponent with functions for removing |
|
1420 |
/// It extends \ref BaseGraphComponent with functions for removing |
|
1421 | 1421 |
/// nodes and edges from the graph. |
1422 | 1422 |
/// This concept requires \ref AlterableGraphComponent. |
1423 | 1423 |
template <typename BAS = BaseGraphComponent> |
1424 | 1424 |
class ErasableGraphComponent : public BAS { |
1425 | 1425 |
public: |
1426 | 1426 |
|
1427 | 1427 |
typedef BAS Base; |
1428 | 1428 |
typedef typename Base::Node Node; |
1429 | 1429 |
typedef typename Base::Edge Edge; |
1430 | 1430 |
|
1431 | 1431 |
/// \brief Erase a node from the graph. |
1432 | 1432 |
/// |
1433 | 1433 |
/// This function erases the given node from the graph and all edges |
1434 | 1434 |
/// connected to the node. |
1435 | 1435 |
void erase(const Node&) {} |
1436 | 1436 |
|
1437 | 1437 |
/// \brief Erase an edge from the digraph. |
1438 | 1438 |
/// |
1439 | 1439 |
/// This function erases the given edge from the digraph. |
1440 | 1440 |
void erase(const Edge&) {} |
1441 | 1441 |
|
1442 | 1442 |
template <typename _Graph> |
1443 | 1443 |
struct Constraints { |
1444 | 1444 |
void constraints() { |
1445 | 1445 |
checkConcept<Base, _Graph>(); |
1446 | 1446 |
const typename _Graph::Node node(INVALID); |
1447 | 1447 |
graph.erase(node); |
1448 | 1448 |
const typename _Graph::Edge edge(INVALID); |
1449 | 1449 |
graph.erase(edge); |
1450 | 1450 |
} |
1451 | 1451 |
|
1452 | 1452 |
_Graph& graph; |
1453 | 1453 |
}; |
1454 | 1454 |
}; |
1455 | 1455 |
|
1456 | 1456 |
/// \brief Skeleton class for clearable directed graphs. |
1457 | 1457 |
/// |
1458 | 1458 |
/// This class describes the interface of clearable directed graphs. |
1459 | 1459 |
/// It extends \ref BaseDigraphComponent with a function for clearing |
1460 | 1460 |
/// the digraph. |
1461 | 1461 |
/// This concept requires \ref AlterableDigraphComponent. |
1462 | 1462 |
template <typename BAS = BaseDigraphComponent> |
1463 | 1463 |
class ClearableDigraphComponent : public BAS { |
1464 | 1464 |
public: |
1465 | 1465 |
|
1466 | 1466 |
typedef BAS Base; |
1467 | 1467 |
|
1468 | 1468 |
/// \brief Erase all nodes and arcs from the digraph. |
1469 | 1469 |
/// |
1470 | 1470 |
/// This function erases all nodes and arcs from the digraph. |
1471 | 1471 |
void clear() {} |
1472 | 1472 |
|
1473 | 1473 |
template <typename _Digraph> |
1474 | 1474 |
struct Constraints { |
1475 | 1475 |
void constraints() { |
1476 | 1476 |
checkConcept<Base, _Digraph>(); |
1477 | 1477 |
digraph.clear(); |
1478 | 1478 |
} |
1479 | 1479 |
|
1480 | 1480 |
_Digraph& digraph; |
1481 | 1481 |
}; |
1482 | 1482 |
}; |
1483 | 1483 |
|
1484 | 1484 |
/// \brief Skeleton class for clearable undirected graphs. |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_CONCEPTS_MAPS_H |
20 | 20 |
#define LEMON_CONCEPTS_MAPS_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
#include <lemon/concept_check.h> |
24 | 24 |
|
25 | 25 |
///\ingroup map_concepts |
26 | 26 |
///\file |
27 | 27 |
///\brief The concept of maps. |
28 | 28 |
|
29 | 29 |
namespace lemon { |
30 | 30 |
|
31 | 31 |
namespace concepts { |
32 | 32 |
|
33 | 33 |
/// \addtogroup map_concepts |
34 | 34 |
/// @{ |
35 | 35 |
|
36 | 36 |
/// Readable map concept |
37 | 37 |
|
38 | 38 |
/// Readable map concept. |
39 | 39 |
/// |
40 | 40 |
template<typename K, typename T> |
41 | 41 |
class ReadMap |
42 | 42 |
{ |
43 | 43 |
public: |
44 | 44 |
/// The key type of the map. |
45 | 45 |
typedef K Key; |
46 | 46 |
/// \brief The value type of the map. |
47 | 47 |
/// (The type of objects associated with the keys). |
48 | 48 |
typedef T Value; |
49 | 49 |
|
50 | 50 |
/// Returns the value associated with the given key. |
51 | 51 |
Value operator[](const Key &) const { |
52 | 52 |
return *static_cast<Value *>(0); |
53 | 53 |
} |
54 | 54 |
|
55 | 55 |
template<typename _ReadMap> |
56 | 56 |
struct Constraints { |
57 | 57 |
void constraints() { |
58 | 58 |
Value val = m[key]; |
59 | 59 |
val = m[key]; |
60 | 60 |
typename _ReadMap::Value own_val = m[own_key]; |
61 | 61 |
own_val = m[own_key]; |
62 | 62 |
|
63 | 63 |
ignore_unused_variable_warning(key); |
64 | 64 |
ignore_unused_variable_warning(val); |
65 | 65 |
ignore_unused_variable_warning(own_key); |
66 | 66 |
ignore_unused_variable_warning(own_val); |
67 | 67 |
} |
68 | 68 |
const Key& key; |
69 | 69 |
const typename _ReadMap::Key& own_key; |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_CONNECTIVITY_H |
20 | 20 |
#define LEMON_CONNECTIVITY_H |
21 | 21 |
|
22 | 22 |
#include <lemon/dfs.h> |
23 | 23 |
#include <lemon/bfs.h> |
24 | 24 |
#include <lemon/core.h> |
25 | 25 |
#include <lemon/maps.h> |
26 | 26 |
#include <lemon/adaptors.h> |
27 | 27 |
|
28 | 28 |
#include <lemon/concepts/digraph.h> |
29 | 29 |
#include <lemon/concepts/graph.h> |
30 | 30 |
#include <lemon/concept_check.h> |
31 | 31 |
|
32 | 32 |
#include <stack> |
33 | 33 |
#include <functional> |
34 | 34 |
|
35 | 35 |
/// \ingroup graph_properties |
36 | 36 |
/// \file |
37 | 37 |
/// \brief Connectivity algorithms |
38 | 38 |
/// |
39 | 39 |
/// Connectivity algorithms |
40 | 40 |
|
41 | 41 |
namespace lemon { |
42 | 42 |
|
43 | 43 |
/// \ingroup graph_properties |
44 | 44 |
/// |
45 | 45 |
/// \brief Check whether an undirected graph is connected. |
46 | 46 |
/// |
47 | 47 |
/// This function checks whether the given undirected graph is connected, |
48 | 48 |
/// i.e. there is a path between any two nodes in the graph. |
49 | 49 |
/// |
50 | 50 |
/// \return \c true if the graph is connected. |
51 | 51 |
/// \note By definition, the empty graph is connected. |
52 | 52 |
/// |
53 | 53 |
/// \see countConnectedComponents(), connectedComponents() |
54 | 54 |
/// \see stronglyConnected() |
55 | 55 |
template <typename Graph> |
56 | 56 |
bool connected(const Graph& graph) { |
57 | 57 |
checkConcept<concepts::Graph, Graph>(); |
58 | 58 |
typedef typename Graph::NodeIt NodeIt; |
59 | 59 |
if (NodeIt(graph) == INVALID) return true; |
60 | 60 |
Dfs<Graph> dfs(graph); |
61 | 61 |
dfs.run(NodeIt(graph)); |
62 | 62 |
for (NodeIt it(graph); it != INVALID; ++it) { |
63 | 63 |
if (!dfs.reached(it)) { |
64 | 64 |
return false; |
65 | 65 |
} |
66 | 66 |
} |
67 | 67 |
return true; |
68 | 68 |
} |
69 | 69 |
|
... | ... |
@@ -197,181 +197,181 @@ |
197 | 197 |
typedef typename Digraph::Node Node; |
198 | 198 |
typedef typename Map::Value Value; |
199 | 199 |
|
200 | 200 |
FillMapVisitor(Map& map, Value& value) |
201 | 201 |
: _map(map), _value(value) {} |
202 | 202 |
|
203 | 203 |
void reach(const Node& node) { |
204 | 204 |
_map.set(node, _value); |
205 | 205 |
} |
206 | 206 |
private: |
207 | 207 |
Map& _map; |
208 | 208 |
Value& _value; |
209 | 209 |
}; |
210 | 210 |
|
211 | 211 |
template <typename Digraph, typename ArcMap> |
212 | 212 |
struct StronglyConnectedCutArcsVisitor : public DfsVisitor<Digraph> { |
213 | 213 |
public: |
214 | 214 |
typedef typename Digraph::Node Node; |
215 | 215 |
typedef typename Digraph::Arc Arc; |
216 | 216 |
|
217 | 217 |
StronglyConnectedCutArcsVisitor(const Digraph& digraph, |
218 | 218 |
ArcMap& cutMap, |
219 | 219 |
int& cutNum) |
220 | 220 |
: _digraph(digraph), _cutMap(cutMap), _cutNum(cutNum), |
221 | 221 |
_compMap(digraph, -1), _num(-1) { |
222 | 222 |
} |
223 | 223 |
|
224 | 224 |
void start(const Node&) { |
225 | 225 |
++_num; |
226 | 226 |
} |
227 | 227 |
|
228 | 228 |
void reach(const Node& node) { |
229 | 229 |
_compMap.set(node, _num); |
230 | 230 |
} |
231 | 231 |
|
232 | 232 |
void examine(const Arc& arc) { |
233 | 233 |
if (_compMap[_digraph.source(arc)] != |
234 | 234 |
_compMap[_digraph.target(arc)]) { |
235 | 235 |
_cutMap.set(arc, true); |
236 | 236 |
++_cutNum; |
237 | 237 |
} |
238 | 238 |
} |
239 | 239 |
private: |
240 | 240 |
const Digraph& _digraph; |
241 | 241 |
ArcMap& _cutMap; |
242 | 242 |
int& _cutNum; |
243 | 243 |
|
244 | 244 |
typename Digraph::template NodeMap<int> _compMap; |
245 | 245 |
int _num; |
246 | 246 |
}; |
247 | 247 |
|
248 | 248 |
} |
249 | 249 |
|
250 | 250 |
|
251 | 251 |
/// \ingroup graph_properties |
252 | 252 |
/// |
253 | 253 |
/// \brief Check whether a directed graph is strongly connected. |
254 | 254 |
/// |
255 | 255 |
/// This function checks whether the given directed graph is strongly |
256 | 256 |
/// connected, i.e. any two nodes of the digraph are |
257 | 257 |
/// connected with directed paths in both direction. |
258 | 258 |
/// |
259 | 259 |
/// \return \c true if the digraph is strongly connected. |
260 | 260 |
/// \note By definition, the empty digraph is strongly connected. |
261 |
/// |
|
261 |
/// |
|
262 | 262 |
/// \see countStronglyConnectedComponents(), stronglyConnectedComponents() |
263 | 263 |
/// \see connected() |
264 | 264 |
template <typename Digraph> |
265 | 265 |
bool stronglyConnected(const Digraph& digraph) { |
266 | 266 |
checkConcept<concepts::Digraph, Digraph>(); |
267 | 267 |
|
268 | 268 |
typedef typename Digraph::Node Node; |
269 | 269 |
typedef typename Digraph::NodeIt NodeIt; |
270 | 270 |
|
271 | 271 |
typename Digraph::Node source = NodeIt(digraph); |
272 | 272 |
if (source == INVALID) return true; |
273 | 273 |
|
274 | 274 |
using namespace _connectivity_bits; |
275 | 275 |
|
276 | 276 |
typedef DfsVisitor<Digraph> Visitor; |
277 | 277 |
Visitor visitor; |
278 | 278 |
|
279 | 279 |
DfsVisit<Digraph, Visitor> dfs(digraph, visitor); |
280 | 280 |
dfs.init(); |
281 | 281 |
dfs.addSource(source); |
282 | 282 |
dfs.start(); |
283 | 283 |
|
284 | 284 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
285 | 285 |
if (!dfs.reached(it)) { |
286 | 286 |
return false; |
287 | 287 |
} |
288 | 288 |
} |
289 | 289 |
|
290 | 290 |
typedef ReverseDigraph<const Digraph> RDigraph; |
291 | 291 |
typedef typename RDigraph::NodeIt RNodeIt; |
292 | 292 |
RDigraph rdigraph(digraph); |
293 | 293 |
|
294 | 294 |
typedef DfsVisitor<RDigraph> RVisitor; |
295 | 295 |
RVisitor rvisitor; |
296 | 296 |
|
297 | 297 |
DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor); |
298 | 298 |
rdfs.init(); |
299 | 299 |
rdfs.addSource(source); |
300 | 300 |
rdfs.start(); |
301 | 301 |
|
302 | 302 |
for (RNodeIt it(rdigraph); it != INVALID; ++it) { |
303 | 303 |
if (!rdfs.reached(it)) { |
304 | 304 |
return false; |
305 | 305 |
} |
306 | 306 |
} |
307 | 307 |
|
308 | 308 |
return true; |
309 | 309 |
} |
310 | 310 |
|
311 | 311 |
/// \ingroup graph_properties |
312 | 312 |
/// |
313 |
/// \brief Count the number of strongly connected components of a |
|
313 |
/// \brief Count the number of strongly connected components of a |
|
314 | 314 |
/// directed graph |
315 | 315 |
/// |
316 | 316 |
/// This function counts the number of strongly connected components of |
317 | 317 |
/// the given directed graph. |
318 | 318 |
/// |
319 | 319 |
/// The strongly connected components are the classes of an |
320 | 320 |
/// equivalence relation on the nodes of a digraph. Two nodes are in |
321 | 321 |
/// the same class if they are connected with directed paths in both |
322 | 322 |
/// direction. |
323 | 323 |
/// |
324 | 324 |
/// \return The number of strongly connected components. |
325 | 325 |
/// \note By definition, the empty digraph has zero |
326 | 326 |
/// strongly connected components. |
327 | 327 |
/// |
328 | 328 |
/// \see stronglyConnected(), stronglyConnectedComponents() |
329 | 329 |
template <typename Digraph> |
330 | 330 |
int countStronglyConnectedComponents(const Digraph& digraph) { |
331 | 331 |
checkConcept<concepts::Digraph, Digraph>(); |
332 | 332 |
|
333 | 333 |
using namespace _connectivity_bits; |
334 | 334 |
|
335 | 335 |
typedef typename Digraph::Node Node; |
336 | 336 |
typedef typename Digraph::Arc Arc; |
337 | 337 |
typedef typename Digraph::NodeIt NodeIt; |
338 | 338 |
typedef typename Digraph::ArcIt ArcIt; |
339 | 339 |
|
340 | 340 |
typedef std::vector<Node> Container; |
341 | 341 |
typedef typename Container::iterator Iterator; |
342 | 342 |
|
343 | 343 |
Container nodes(countNodes(digraph)); |
344 | 344 |
typedef LeaveOrderVisitor<Digraph, Iterator> Visitor; |
345 | 345 |
Visitor visitor(nodes.begin()); |
346 | 346 |
|
347 | 347 |
DfsVisit<Digraph, Visitor> dfs(digraph, visitor); |
348 | 348 |
dfs.init(); |
349 | 349 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
350 | 350 |
if (!dfs.reached(it)) { |
351 | 351 |
dfs.addSource(it); |
352 | 352 |
dfs.start(); |
353 | 353 |
} |
354 | 354 |
} |
355 | 355 |
|
356 | 356 |
typedef typename Container::reverse_iterator RIterator; |
357 | 357 |
typedef ReverseDigraph<const Digraph> RDigraph; |
358 | 358 |
|
359 | 359 |
RDigraph rdigraph(digraph); |
360 | 360 |
|
361 | 361 |
typedef DfsVisitor<Digraph> RVisitor; |
362 | 362 |
RVisitor rvisitor; |
363 | 363 |
|
364 | 364 |
DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor); |
365 | 365 |
|
366 | 366 |
int compNum = 0; |
367 | 367 |
|
368 | 368 |
rdfs.init(); |
369 | 369 |
for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) { |
370 | 370 |
if (!rdfs.reached(*it)) { |
371 | 371 |
rdfs.addSource(*it); |
372 | 372 |
rdfs.start(); |
373 | 373 |
++compNum; |
374 | 374 |
} |
375 | 375 |
} |
376 | 376 |
return compNum; |
377 | 377 |
} |
... | ... |
@@ -683,243 +683,243 @@ |
683 | 683 |
} |
684 | 684 |
|
685 | 685 |
void discover(const Arc& edge) { |
686 | 686 |
_predMap.set(_graph.target(edge), _graph.source(edge)); |
687 | 687 |
} |
688 | 688 |
|
689 | 689 |
void examine(const Arc& edge) { |
690 | 690 |
if (_graph.source(edge) == _graph.target(edge) && |
691 | 691 |
_graph.direction(edge)) { |
692 | 692 |
if (!_cutMap[_graph.source(edge)]) { |
693 | 693 |
_cutMap.set(_graph.source(edge), true); |
694 | 694 |
++_cutNum; |
695 | 695 |
} |
696 | 696 |
return; |
697 | 697 |
} |
698 | 698 |
if (_predMap[_graph.source(edge)] == _graph.target(edge)) return; |
699 | 699 |
if (_retMap[_graph.source(edge)] > _numMap[_graph.target(edge)]) { |
700 | 700 |
_retMap.set(_graph.source(edge), _numMap[_graph.target(edge)]); |
701 | 701 |
} |
702 | 702 |
} |
703 | 703 |
|
704 | 704 |
void backtrack(const Arc& edge) { |
705 | 705 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
706 | 706 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
707 | 707 |
} |
708 | 708 |
if (_numMap[_graph.source(edge)] <= _retMap[_graph.target(edge)]) { |
709 | 709 |
if (_predMap[_graph.source(edge)] != INVALID) { |
710 | 710 |
if (!_cutMap[_graph.source(edge)]) { |
711 | 711 |
_cutMap.set(_graph.source(edge), true); |
712 | 712 |
++_cutNum; |
713 | 713 |
} |
714 | 714 |
} else if (rootCut) { |
715 | 715 |
if (!_cutMap[_graph.source(edge)]) { |
716 | 716 |
_cutMap.set(_graph.source(edge), true); |
717 | 717 |
++_cutNum; |
718 | 718 |
} |
719 | 719 |
} else { |
720 | 720 |
rootCut = true; |
721 | 721 |
} |
722 | 722 |
} |
723 | 723 |
} |
724 | 724 |
|
725 | 725 |
private: |
726 | 726 |
const Digraph& _graph; |
727 | 727 |
NodeMap& _cutMap; |
728 | 728 |
int& _cutNum; |
729 | 729 |
|
730 | 730 |
typename Digraph::template NodeMap<int> _numMap; |
731 | 731 |
typename Digraph::template NodeMap<int> _retMap; |
732 | 732 |
typename Digraph::template NodeMap<Node> _predMap; |
733 | 733 |
std::stack<Edge> _edgeStack; |
734 | 734 |
int _num; |
735 | 735 |
bool rootCut; |
736 | 736 |
}; |
737 | 737 |
|
738 | 738 |
} |
739 | 739 |
|
740 | 740 |
template <typename Graph> |
741 | 741 |
int countBiNodeConnectedComponents(const Graph& graph); |
742 | 742 |
|
743 | 743 |
/// \ingroup graph_properties |
744 | 744 |
/// |
745 | 745 |
/// \brief Check whether an undirected graph is bi-node-connected. |
746 | 746 |
/// |
747 |
/// This function checks whether the given undirected graph is |
|
747 |
/// This function checks whether the given undirected graph is |
|
748 | 748 |
/// bi-node-connected, i.e. any two edges are on same circle. |
749 | 749 |
/// |
750 | 750 |
/// \return \c true if the graph bi-node-connected. |
751 | 751 |
/// \note By definition, the empty graph is bi-node-connected. |
752 | 752 |
/// |
753 | 753 |
/// \see countBiNodeConnectedComponents(), biNodeConnectedComponents() |
754 | 754 |
template <typename Graph> |
755 | 755 |
bool biNodeConnected(const Graph& graph) { |
756 | 756 |
return countBiNodeConnectedComponents(graph) <= 1; |
757 | 757 |
} |
758 | 758 |
|
759 | 759 |
/// \ingroup graph_properties |
760 | 760 |
/// |
761 |
/// \brief Count the number of bi-node-connected components of an |
|
761 |
/// \brief Count the number of bi-node-connected components of an |
|
762 | 762 |
/// undirected graph. |
763 | 763 |
/// |
764 | 764 |
/// This function counts the number of bi-node-connected components of |
765 | 765 |
/// the given undirected graph. |
766 | 766 |
/// |
767 | 767 |
/// The bi-node-connected components are the classes of an equivalence |
768 | 768 |
/// relation on the edges of a undirected graph. Two edges are in the |
769 | 769 |
/// same class if they are on same circle. |
770 | 770 |
/// |
771 | 771 |
/// \return The number of bi-node-connected components. |
772 | 772 |
/// |
773 | 773 |
/// \see biNodeConnected(), biNodeConnectedComponents() |
774 | 774 |
template <typename Graph> |
775 | 775 |
int countBiNodeConnectedComponents(const Graph& graph) { |
776 | 776 |
checkConcept<concepts::Graph, Graph>(); |
777 | 777 |
typedef typename Graph::NodeIt NodeIt; |
778 | 778 |
|
779 | 779 |
using namespace _connectivity_bits; |
780 | 780 |
|
781 | 781 |
typedef CountBiNodeConnectedComponentsVisitor<Graph> Visitor; |
782 | 782 |
|
783 | 783 |
int compNum = 0; |
784 | 784 |
Visitor visitor(graph, compNum); |
785 | 785 |
|
786 | 786 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
787 | 787 |
dfs.init(); |
788 | 788 |
|
789 | 789 |
for (NodeIt it(graph); it != INVALID; ++it) { |
790 | 790 |
if (!dfs.reached(it)) { |
791 | 791 |
dfs.addSource(it); |
792 | 792 |
dfs.start(); |
793 | 793 |
} |
794 | 794 |
} |
795 | 795 |
return compNum; |
796 | 796 |
} |
797 | 797 |
|
798 | 798 |
/// \ingroup graph_properties |
799 | 799 |
/// |
800 | 800 |
/// \brief Find the bi-node-connected components of an undirected graph. |
801 | 801 |
/// |
802 | 802 |
/// This function finds the bi-node-connected components of the given |
803 | 803 |
/// undirected graph. |
804 | 804 |
/// |
805 | 805 |
/// The bi-node-connected components are the classes of an equivalence |
806 | 806 |
/// relation on the edges of a undirected graph. Two edges are in the |
807 | 807 |
/// same class if they are on same circle. |
808 | 808 |
/// |
809 | 809 |
/// \image html node_biconnected_components.png |
810 | 810 |
/// \image latex node_biconnected_components.eps "bi-node-connected components" width=\textwidth |
811 | 811 |
/// |
812 | 812 |
/// \param graph The undirected graph. |
813 | 813 |
/// \retval compMap A writable edge map. The values will be set from 0 |
814 | 814 |
/// to the number of the bi-node-connected components minus one. Each |
815 |
/// value of the map will be set exactly once, and the values of a |
|
815 |
/// value of the map will be set exactly once, and the values of a |
|
816 | 816 |
/// certain component will be set continuously. |
817 | 817 |
/// \return The number of bi-node-connected components. |
818 | 818 |
/// |
819 | 819 |
/// \see biNodeConnected(), countBiNodeConnectedComponents() |
820 | 820 |
template <typename Graph, typename EdgeMap> |
821 | 821 |
int biNodeConnectedComponents(const Graph& graph, |
822 | 822 |
EdgeMap& compMap) { |
823 | 823 |
checkConcept<concepts::Graph, Graph>(); |
824 | 824 |
typedef typename Graph::NodeIt NodeIt; |
825 | 825 |
typedef typename Graph::Edge Edge; |
826 | 826 |
checkConcept<concepts::WriteMap<Edge, int>, EdgeMap>(); |
827 | 827 |
|
828 | 828 |
using namespace _connectivity_bits; |
829 | 829 |
|
830 | 830 |
typedef BiNodeConnectedComponentsVisitor<Graph, EdgeMap> Visitor; |
831 | 831 |
|
832 | 832 |
int compNum = 0; |
833 | 833 |
Visitor visitor(graph, compMap, compNum); |
834 | 834 |
|
835 | 835 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
836 | 836 |
dfs.init(); |
837 | 837 |
|
838 | 838 |
for (NodeIt it(graph); it != INVALID; ++it) { |
839 | 839 |
if (!dfs.reached(it)) { |
840 | 840 |
dfs.addSource(it); |
841 | 841 |
dfs.start(); |
842 | 842 |
} |
843 | 843 |
} |
844 | 844 |
return compNum; |
845 | 845 |
} |
846 | 846 |
|
847 | 847 |
/// \ingroup graph_properties |
848 | 848 |
/// |
849 | 849 |
/// \brief Find the bi-node-connected cut nodes in an undirected graph. |
850 | 850 |
/// |
851 | 851 |
/// This function finds the bi-node-connected cut nodes in the given |
852 | 852 |
/// undirected graph. |
853 | 853 |
/// |
854 | 854 |
/// The bi-node-connected components are the classes of an equivalence |
855 | 855 |
/// relation on the edges of a undirected graph. Two edges are in the |
856 | 856 |
/// same class if they are on same circle. |
857 | 857 |
/// The bi-node-connected components are separted by the cut nodes of |
858 | 858 |
/// the components. |
859 | 859 |
/// |
860 | 860 |
/// \param graph The undirected graph. |
861 |
/// \retval cutMap A writable node map. The values will be set to |
|
861 |
/// \retval cutMap A writable node map. The values will be set to |
|
862 | 862 |
/// \c true for the nodes that separate two or more components |
863 | 863 |
/// (exactly once for each cut node), and will not be changed for |
864 | 864 |
/// other nodes. |
865 | 865 |
/// \return The number of the cut nodes. |
866 | 866 |
/// |
867 | 867 |
/// \see biNodeConnected(), biNodeConnectedComponents() |
868 | 868 |
template <typename Graph, typename NodeMap> |
869 | 869 |
int biNodeConnectedCutNodes(const Graph& graph, NodeMap& cutMap) { |
870 | 870 |
checkConcept<concepts::Graph, Graph>(); |
871 | 871 |
typedef typename Graph::Node Node; |
872 | 872 |
typedef typename Graph::NodeIt NodeIt; |
873 | 873 |
checkConcept<concepts::WriteMap<Node, bool>, NodeMap>(); |
874 | 874 |
|
875 | 875 |
using namespace _connectivity_bits; |
876 | 876 |
|
877 | 877 |
typedef BiNodeConnectedCutNodesVisitor<Graph, NodeMap> Visitor; |
878 | 878 |
|
879 | 879 |
int cutNum = 0; |
880 | 880 |
Visitor visitor(graph, cutMap, cutNum); |
881 | 881 |
|
882 | 882 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
883 | 883 |
dfs.init(); |
884 | 884 |
|
885 | 885 |
for (NodeIt it(graph); it != INVALID; ++it) { |
886 | 886 |
if (!dfs.reached(it)) { |
887 | 887 |
dfs.addSource(it); |
888 | 888 |
dfs.start(); |
889 | 889 |
} |
890 | 890 |
} |
891 | 891 |
return cutNum; |
892 | 892 |
} |
893 | 893 |
|
894 | 894 |
namespace _connectivity_bits { |
895 | 895 |
|
896 | 896 |
template <typename Digraph> |
897 | 897 |
class CountBiEdgeConnectedComponentsVisitor : public DfsVisitor<Digraph> { |
898 | 898 |
public: |
899 | 899 |
typedef typename Digraph::Node Node; |
900 | 900 |
typedef typename Digraph::Arc Arc; |
901 | 901 |
typedef typename Digraph::Edge Edge; |
902 | 902 |
|
903 | 903 |
CountBiEdgeConnectedComponentsVisitor(const Digraph& graph, int &compNum) |
904 | 904 |
: _graph(graph), _compNum(compNum), |
905 | 905 |
_numMap(graph), _retMap(graph), _predMap(graph), _num(0) {} |
906 | 906 |
|
907 | 907 |
void start(const Node& node) { |
908 | 908 |
_predMap.set(node, INVALID); |
909 | 909 |
} |
910 | 910 |
|
911 | 911 |
void reach(const Node& node) { |
912 | 912 |
_numMap.set(node, _num); |
913 | 913 |
_retMap.set(node, _num); |
914 | 914 |
++_num; |
915 | 915 |
} |
916 | 916 |
|
917 | 917 |
void leave(const Node& node) { |
918 | 918 |
if (_numMap[node] <= _retMap[node]) { |
919 | 919 |
++_compNum; |
920 | 920 |
} |
921 | 921 |
} |
922 | 922 |
|
923 | 923 |
void discover(const Arc& edge) { |
924 | 924 |
_predMap.set(_graph.target(edge), edge); |
925 | 925 |
} |
... | ... |
@@ -1024,236 +1024,236 @@ |
1024 | 1024 |
typedef typename Digraph::Edge Edge; |
1025 | 1025 |
|
1026 | 1026 |
BiEdgeConnectedCutEdgesVisitor(const Digraph& graph, |
1027 | 1027 |
ArcMap& cutMap, int &cutNum) |
1028 | 1028 |
: _graph(graph), _cutMap(cutMap), _cutNum(cutNum), |
1029 | 1029 |
_numMap(graph), _retMap(graph), _predMap(graph), _num(0) {} |
1030 | 1030 |
|
1031 | 1031 |
void start(const Node& node) { |
1032 | 1032 |
_predMap[node] = INVALID; |
1033 | 1033 |
} |
1034 | 1034 |
|
1035 | 1035 |
void reach(const Node& node) { |
1036 | 1036 |
_numMap.set(node, _num); |
1037 | 1037 |
_retMap.set(node, _num); |
1038 | 1038 |
++_num; |
1039 | 1039 |
} |
1040 | 1040 |
|
1041 | 1041 |
void leave(const Node& node) { |
1042 | 1042 |
if (_numMap[node] <= _retMap[node]) { |
1043 | 1043 |
if (_predMap[node] != INVALID) { |
1044 | 1044 |
_cutMap.set(_predMap[node], true); |
1045 | 1045 |
++_cutNum; |
1046 | 1046 |
} |
1047 | 1047 |
} |
1048 | 1048 |
} |
1049 | 1049 |
|
1050 | 1050 |
void discover(const Arc& edge) { |
1051 | 1051 |
_predMap.set(_graph.target(edge), edge); |
1052 | 1052 |
} |
1053 | 1053 |
|
1054 | 1054 |
void examine(const Arc& edge) { |
1055 | 1055 |
if (_predMap[_graph.source(edge)] == _graph.oppositeArc(edge)) { |
1056 | 1056 |
return; |
1057 | 1057 |
} |
1058 | 1058 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
1059 | 1059 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
1060 | 1060 |
} |
1061 | 1061 |
} |
1062 | 1062 |
|
1063 | 1063 |
void backtrack(const Arc& edge) { |
1064 | 1064 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
1065 | 1065 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
1066 | 1066 |
} |
1067 | 1067 |
} |
1068 | 1068 |
|
1069 | 1069 |
private: |
1070 | 1070 |
const Digraph& _graph; |
1071 | 1071 |
ArcMap& _cutMap; |
1072 | 1072 |
int& _cutNum; |
1073 | 1073 |
|
1074 | 1074 |
typename Digraph::template NodeMap<int> _numMap; |
1075 | 1075 |
typename Digraph::template NodeMap<int> _retMap; |
1076 | 1076 |
typename Digraph::template NodeMap<Arc> _predMap; |
1077 | 1077 |
int _num; |
1078 | 1078 |
}; |
1079 | 1079 |
} |
1080 | 1080 |
|
1081 | 1081 |
template <typename Graph> |
1082 | 1082 |
int countBiEdgeConnectedComponents(const Graph& graph); |
1083 | 1083 |
|
1084 | 1084 |
/// \ingroup graph_properties |
1085 | 1085 |
/// |
1086 | 1086 |
/// \brief Check whether an undirected graph is bi-edge-connected. |
1087 | 1087 |
/// |
1088 |
/// This function checks whether the given undirected graph is |
|
1088 |
/// This function checks whether the given undirected graph is |
|
1089 | 1089 |
/// bi-edge-connected, i.e. any two nodes are connected with at least |
1090 | 1090 |
/// two edge-disjoint paths. |
1091 | 1091 |
/// |
1092 | 1092 |
/// \return \c true if the graph is bi-edge-connected. |
1093 | 1093 |
/// \note By definition, the empty graph is bi-edge-connected. |
1094 | 1094 |
/// |
1095 | 1095 |
/// \see countBiEdgeConnectedComponents(), biEdgeConnectedComponents() |
1096 | 1096 |
template <typename Graph> |
1097 | 1097 |
bool biEdgeConnected(const Graph& graph) { |
1098 | 1098 |
return countBiEdgeConnectedComponents(graph) <= 1; |
1099 | 1099 |
} |
1100 | 1100 |
|
1101 | 1101 |
/// \ingroup graph_properties |
1102 | 1102 |
/// |
1103 | 1103 |
/// \brief Count the number of bi-edge-connected components of an |
1104 | 1104 |
/// undirected graph. |
1105 | 1105 |
/// |
1106 | 1106 |
/// This function counts the number of bi-edge-connected components of |
1107 | 1107 |
/// the given undirected graph. |
1108 | 1108 |
/// |
1109 | 1109 |
/// The bi-edge-connected components are the classes of an equivalence |
1110 | 1110 |
/// relation on the nodes of an undirected graph. Two nodes are in the |
1111 | 1111 |
/// same class if they are connected with at least two edge-disjoint |
1112 | 1112 |
/// paths. |
1113 | 1113 |
/// |
1114 | 1114 |
/// \return The number of bi-edge-connected components. |
1115 | 1115 |
/// |
1116 | 1116 |
/// \see biEdgeConnected(), biEdgeConnectedComponents() |
1117 | 1117 |
template <typename Graph> |
1118 | 1118 |
int countBiEdgeConnectedComponents(const Graph& graph) { |
1119 | 1119 |
checkConcept<concepts::Graph, Graph>(); |
1120 | 1120 |
typedef typename Graph::NodeIt NodeIt; |
1121 | 1121 |
|
1122 | 1122 |
using namespace _connectivity_bits; |
1123 | 1123 |
|
1124 | 1124 |
typedef CountBiEdgeConnectedComponentsVisitor<Graph> Visitor; |
1125 | 1125 |
|
1126 | 1126 |
int compNum = 0; |
1127 | 1127 |
Visitor visitor(graph, compNum); |
1128 | 1128 |
|
1129 | 1129 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
1130 | 1130 |
dfs.init(); |
1131 | 1131 |
|
1132 | 1132 |
for (NodeIt it(graph); it != INVALID; ++it) { |
1133 | 1133 |
if (!dfs.reached(it)) { |
1134 | 1134 |
dfs.addSource(it); |
1135 | 1135 |
dfs.start(); |
1136 | 1136 |
} |
1137 | 1137 |
} |
1138 | 1138 |
return compNum; |
1139 | 1139 |
} |
1140 | 1140 |
|
1141 | 1141 |
/// \ingroup graph_properties |
1142 | 1142 |
/// |
1143 | 1143 |
/// \brief Find the bi-edge-connected components of an undirected graph. |
1144 | 1144 |
/// |
1145 | 1145 |
/// This function finds the bi-edge-connected components of the given |
1146 | 1146 |
/// undirected graph. |
1147 | 1147 |
/// |
1148 | 1148 |
/// The bi-edge-connected components are the classes of an equivalence |
1149 | 1149 |
/// relation on the nodes of an undirected graph. Two nodes are in the |
1150 | 1150 |
/// same class if they are connected with at least two edge-disjoint |
1151 | 1151 |
/// paths. |
1152 | 1152 |
/// |
1153 | 1153 |
/// \image html edge_biconnected_components.png |
1154 | 1154 |
/// \image latex edge_biconnected_components.eps "bi-edge-connected components" width=\textwidth |
1155 | 1155 |
/// |
1156 | 1156 |
/// \param graph The undirected graph. |
1157 | 1157 |
/// \retval compMap A writable node map. The values will be set from 0 to |
1158 | 1158 |
/// the number of the bi-edge-connected components minus one. Each value |
1159 | 1159 |
/// of the map will be set exactly once, and the values of a certain |
1160 | 1160 |
/// component will be set continuously. |
1161 | 1161 |
/// \return The number of bi-edge-connected components. |
1162 | 1162 |
/// |
1163 | 1163 |
/// \see biEdgeConnected(), countBiEdgeConnectedComponents() |
1164 | 1164 |
template <typename Graph, typename NodeMap> |
1165 | 1165 |
int biEdgeConnectedComponents(const Graph& graph, NodeMap& compMap) { |
1166 | 1166 |
checkConcept<concepts::Graph, Graph>(); |
1167 | 1167 |
typedef typename Graph::NodeIt NodeIt; |
1168 | 1168 |
typedef typename Graph::Node Node; |
1169 | 1169 |
checkConcept<concepts::WriteMap<Node, int>, NodeMap>(); |
1170 | 1170 |
|
1171 | 1171 |
using namespace _connectivity_bits; |
1172 | 1172 |
|
1173 | 1173 |
typedef BiEdgeConnectedComponentsVisitor<Graph, NodeMap> Visitor; |
1174 | 1174 |
|
1175 | 1175 |
int compNum = 0; |
1176 | 1176 |
Visitor visitor(graph, compMap, compNum); |
1177 | 1177 |
|
1178 | 1178 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
1179 | 1179 |
dfs.init(); |
1180 | 1180 |
|
1181 | 1181 |
for (NodeIt it(graph); it != INVALID; ++it) { |
1182 | 1182 |
if (!dfs.reached(it)) { |
1183 | 1183 |
dfs.addSource(it); |
1184 | 1184 |
dfs.start(); |
1185 | 1185 |
} |
1186 | 1186 |
} |
1187 | 1187 |
return compNum; |
1188 | 1188 |
} |
1189 | 1189 |
|
1190 | 1190 |
/// \ingroup graph_properties |
1191 | 1191 |
/// |
1192 | 1192 |
/// \brief Find the bi-edge-connected cut edges in an undirected graph. |
1193 | 1193 |
/// |
1194 | 1194 |
/// This function finds the bi-edge-connected cut edges in the given |
1195 |
/// undirected graph. |
|
1195 |
/// undirected graph. |
|
1196 | 1196 |
/// |
1197 | 1197 |
/// The bi-edge-connected components are the classes of an equivalence |
1198 | 1198 |
/// relation on the nodes of an undirected graph. Two nodes are in the |
1199 | 1199 |
/// same class if they are connected with at least two edge-disjoint |
1200 | 1200 |
/// paths. |
1201 | 1201 |
/// The bi-edge-connected components are separted by the cut edges of |
1202 | 1202 |
/// the components. |
1203 | 1203 |
/// |
1204 | 1204 |
/// \param graph The undirected graph. |
1205 | 1205 |
/// \retval cutMap A writable edge map. The values will be set to \c true |
1206 | 1206 |
/// for the cut edges (exactly once for each cut edge), and will not be |
1207 | 1207 |
/// changed for other edges. |
1208 | 1208 |
/// \return The number of cut edges. |
1209 | 1209 |
/// |
1210 | 1210 |
/// \see biEdgeConnected(), biEdgeConnectedComponents() |
1211 | 1211 |
template <typename Graph, typename EdgeMap> |
1212 | 1212 |
int biEdgeConnectedCutEdges(const Graph& graph, EdgeMap& cutMap) { |
1213 | 1213 |
checkConcept<concepts::Graph, Graph>(); |
1214 | 1214 |
typedef typename Graph::NodeIt NodeIt; |
1215 | 1215 |
typedef typename Graph::Edge Edge; |
1216 | 1216 |
checkConcept<concepts::WriteMap<Edge, bool>, EdgeMap>(); |
1217 | 1217 |
|
1218 | 1218 |
using namespace _connectivity_bits; |
1219 | 1219 |
|
1220 | 1220 |
typedef BiEdgeConnectedCutEdgesVisitor<Graph, EdgeMap> Visitor; |
1221 | 1221 |
|
1222 | 1222 |
int cutNum = 0; |
1223 | 1223 |
Visitor visitor(graph, cutMap, cutNum); |
1224 | 1224 |
|
1225 | 1225 |
DfsVisit<Graph, Visitor> dfs(graph, visitor); |
1226 | 1226 |
dfs.init(); |
1227 | 1227 |
|
1228 | 1228 |
for (NodeIt it(graph); it != INVALID; ++it) { |
1229 | 1229 |
if (!dfs.reached(it)) { |
1230 | 1230 |
dfs.addSource(it); |
1231 | 1231 |
dfs.start(); |
1232 | 1232 |
} |
1233 | 1233 |
} |
1234 | 1234 |
return cutNum; |
1235 | 1235 |
} |
1236 | 1236 |
|
1237 | 1237 |
|
1238 | 1238 |
namespace _connectivity_bits { |
1239 | 1239 |
|
1240 | 1240 |
template <typename Digraph, typename IntNodeMap> |
1241 | 1241 |
class TopologicalSortVisitor : public DfsVisitor<Digraph> { |
1242 | 1242 |
public: |
1243 | 1243 |
typedef typename Digraph::Node Node; |
1244 | 1244 |
typedef typename Digraph::Arc edge; |
1245 | 1245 |
|
1246 | 1246 |
TopologicalSortVisitor(IntNodeMap& order, int num) |
1247 | 1247 |
: _order(order), _num(num) {} |
1248 | 1248 |
|
1249 | 1249 |
void leave(const Node& node) { |
1250 | 1250 |
_order.set(node, --_num); |
1251 | 1251 |
} |
1252 | 1252 |
|
1253 | 1253 |
private: |
1254 | 1254 |
IntNodeMap& _order; |
1255 | 1255 |
int _num; |
1256 | 1256 |
}; |
1257 | 1257 |
|
1258 | 1258 |
} |
1259 | 1259 |
|
... | ... |
@@ -1288,129 +1288,129 @@ |
1288 | 1288 |
dfs.addSource(it); |
1289 | 1289 |
while (!dfs.emptyQueue()) { |
1290 | 1290 |
Arc arc = dfs.nextArc(); |
1291 | 1291 |
Node target = digraph.target(arc); |
1292 | 1292 |
if (dfs.reached(target) && !processed[target]) { |
1293 | 1293 |
return false; |
1294 | 1294 |
} |
1295 | 1295 |
dfs.processNextArc(); |
1296 | 1296 |
} |
1297 | 1297 |
} |
1298 | 1298 |
} |
1299 | 1299 |
return true; |
1300 | 1300 |
} |
1301 | 1301 |
|
1302 | 1302 |
/// \ingroup graph_properties |
1303 | 1303 |
/// |
1304 | 1304 |
/// \brief Sort the nodes of a DAG into topolgical order. |
1305 | 1305 |
/// |
1306 | 1306 |
/// This function sorts the nodes of the given acyclic digraph (DAG) |
1307 | 1307 |
/// into topolgical order. |
1308 | 1308 |
/// |
1309 | 1309 |
/// \param digraph The digraph, which must be DAG. |
1310 | 1310 |
/// \retval order A writable node map. The values will be set from 0 to |
1311 | 1311 |
/// the number of the nodes in the digraph minus one. Each value of the |
1312 | 1312 |
/// map will be set exactly once, and the values will be set descending |
1313 | 1313 |
/// order. |
1314 | 1314 |
/// |
1315 | 1315 |
/// \see dag(), checkedTopologicalSort() |
1316 | 1316 |
template <typename Digraph, typename NodeMap> |
1317 | 1317 |
void topologicalSort(const Digraph& digraph, NodeMap& order) { |
1318 | 1318 |
using namespace _connectivity_bits; |
1319 | 1319 |
|
1320 | 1320 |
checkConcept<concepts::Digraph, Digraph>(); |
1321 | 1321 |
checkConcept<concepts::WriteMap<typename Digraph::Node, int>, NodeMap>(); |
1322 | 1322 |
|
1323 | 1323 |
typedef typename Digraph::Node Node; |
1324 | 1324 |
typedef typename Digraph::NodeIt NodeIt; |
1325 | 1325 |
typedef typename Digraph::Arc Arc; |
1326 | 1326 |
|
1327 | 1327 |
TopologicalSortVisitor<Digraph, NodeMap> |
1328 | 1328 |
visitor(order, countNodes(digraph)); |
1329 | 1329 |
|
1330 | 1330 |
DfsVisit<Digraph, TopologicalSortVisitor<Digraph, NodeMap> > |
1331 | 1331 |
dfs(digraph, visitor); |
1332 | 1332 |
|
1333 | 1333 |
dfs.init(); |
1334 | 1334 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
1335 | 1335 |
if (!dfs.reached(it)) { |
1336 | 1336 |
dfs.addSource(it); |
1337 | 1337 |
dfs.start(); |
1338 | 1338 |
} |
1339 | 1339 |
} |
1340 | 1340 |
} |
1341 | 1341 |
|
1342 | 1342 |
/// \ingroup graph_properties |
1343 | 1343 |
/// |
1344 | 1344 |
/// \brief Sort the nodes of a DAG into topolgical order. |
1345 | 1345 |
/// |
1346 | 1346 |
/// This function sorts the nodes of the given acyclic digraph (DAG) |
1347 | 1347 |
/// into topolgical order and also checks whether the given digraph |
1348 | 1348 |
/// is DAG. |
1349 | 1349 |
/// |
1350 | 1350 |
/// \param digraph The digraph. |
1351 | 1351 |
/// \retval order A readable and writable node map. The values will be |
1352 |
/// set from 0 to the number of the nodes in the digraph minus one. |
|
1352 |
/// set from 0 to the number of the nodes in the digraph minus one. |
|
1353 | 1353 |
/// Each value of the map will be set exactly once, and the values will |
1354 | 1354 |
/// be set descending order. |
1355 | 1355 |
/// \return \c false if the digraph is not DAG. |
1356 | 1356 |
/// |
1357 | 1357 |
/// \see dag(), topologicalSort() |
1358 | 1358 |
template <typename Digraph, typename NodeMap> |
1359 | 1359 |
bool checkedTopologicalSort(const Digraph& digraph, NodeMap& order) { |
1360 | 1360 |
using namespace _connectivity_bits; |
1361 | 1361 |
|
1362 | 1362 |
checkConcept<concepts::Digraph, Digraph>(); |
1363 | 1363 |
checkConcept<concepts::ReadWriteMap<typename Digraph::Node, int>, |
1364 | 1364 |
NodeMap>(); |
1365 | 1365 |
|
1366 | 1366 |
typedef typename Digraph::Node Node; |
1367 | 1367 |
typedef typename Digraph::NodeIt NodeIt; |
1368 | 1368 |
typedef typename Digraph::Arc Arc; |
1369 | 1369 |
|
1370 | 1370 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
1371 | 1371 |
order.set(it, -1); |
1372 | 1372 |
} |
1373 | 1373 |
|
1374 | 1374 |
TopologicalSortVisitor<Digraph, NodeMap> |
1375 | 1375 |
visitor(order, countNodes(digraph)); |
1376 | 1376 |
|
1377 | 1377 |
DfsVisit<Digraph, TopologicalSortVisitor<Digraph, NodeMap> > |
1378 | 1378 |
dfs(digraph, visitor); |
1379 | 1379 |
|
1380 | 1380 |
dfs.init(); |
1381 | 1381 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
1382 | 1382 |
if (!dfs.reached(it)) { |
1383 | 1383 |
dfs.addSource(it); |
1384 | 1384 |
while (!dfs.emptyQueue()) { |
1385 | 1385 |
Arc arc = dfs.nextArc(); |
1386 | 1386 |
Node target = digraph.target(arc); |
1387 | 1387 |
if (dfs.reached(target) && order[target] == -1) { |
1388 | 1388 |
return false; |
1389 | 1389 |
} |
1390 | 1390 |
dfs.processNextArc(); |
1391 | 1391 |
} |
1392 | 1392 |
} |
1393 | 1393 |
} |
1394 | 1394 |
return true; |
1395 | 1395 |
} |
1396 | 1396 |
|
1397 | 1397 |
/// \ingroup graph_properties |
1398 | 1398 |
/// |
1399 | 1399 |
/// \brief Check whether an undirected graph is acyclic. |
1400 | 1400 |
/// |
1401 | 1401 |
/// This function checks whether the given undirected graph is acyclic. |
1402 | 1402 |
/// \return \c true if there is no cycle in the graph. |
1403 | 1403 |
/// \see dag() |
1404 | 1404 |
template <typename Graph> |
1405 | 1405 |
bool acyclic(const Graph& graph) { |
1406 | 1406 |
checkConcept<concepts::Graph, Graph>(); |
1407 | 1407 |
typedef typename Graph::Node Node; |
1408 | 1408 |
typedef typename Graph::NodeIt NodeIt; |
1409 | 1409 |
typedef typename Graph::Arc Arc; |
1410 | 1410 |
Dfs<Graph> dfs(graph); |
1411 | 1411 |
dfs.init(); |
1412 | 1412 |
for (NodeIt it(graph); it != INVALID; ++it) { |
1413 | 1413 |
if (!dfs.reached(it)) { |
1414 | 1414 |
dfs.addSource(it); |
1415 | 1415 |
while (!dfs.emptyQueue()) { |
1416 | 1416 |
Arc arc = dfs.nextArc(); |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_CORE_H |
20 | 20 |
#define LEMON_CORE_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <algorithm> |
24 | 24 |
|
25 | 25 |
#include <lemon/config.h> |
26 | 26 |
#include <lemon/bits/enable_if.h> |
27 | 27 |
#include <lemon/bits/traits.h> |
28 | 28 |
#include <lemon/assert.h> |
29 | 29 |
|
30 | 30 |
// Disable the following warnings when compiling with MSVC: |
31 | 31 |
// C4250: 'class1' : inherits 'class2::member' via dominance |
32 | 32 |
// C4355: 'this' : used in base member initializer list |
33 | 33 |
// C4503: 'function' : decorated name length exceeded, name was truncated |
34 | 34 |
// C4800: 'type' : forcing value to bool 'true' or 'false' (performance warning) |
35 | 35 |
// C4996: 'function': was declared deprecated |
36 | 36 |
#ifdef _MSC_VER |
37 | 37 |
#pragma warning( disable : 4250 4355 4503 4800 4996 ) |
38 | 38 |
#endif |
39 | 39 |
|
40 | 40 |
///\file |
41 | 41 |
///\brief LEMON core utilities. |
42 | 42 |
/// |
43 | 43 |
///This header file contains core utilities for LEMON. |
44 | 44 |
///It is automatically included by all graph types, therefore it usually |
45 | 45 |
///do not have to be included directly. |
46 | 46 |
|
47 | 47 |
namespace lemon { |
48 | 48 |
|
49 | 49 |
/// \brief Dummy type to make it easier to create invalid iterators. |
50 | 50 |
/// |
51 | 51 |
/// Dummy type to make it easier to create invalid iterators. |
52 | 52 |
/// See \ref INVALID for the usage. |
53 | 53 |
struct Invalid { |
54 | 54 |
public: |
55 | 55 |
bool operator==(Invalid) { return true; } |
56 | 56 |
bool operator!=(Invalid) { return false; } |
57 | 57 |
bool operator< (Invalid) { return false; } |
58 | 58 |
}; |
59 | 59 |
|
60 | 60 |
/// \brief Invalid iterators. |
61 | 61 |
/// |
62 | 62 |
/// \ref Invalid is a global type that converts to each iterator |
63 | 63 |
/// in such a way that the value of the target iterator will be invalid. |
64 | 64 |
#ifdef LEMON_ONLY_TEMPLATES |
65 | 65 |
const Invalid INVALID = Invalid(); |
66 | 66 |
#else |
67 | 67 |
extern const Invalid INVALID; |
68 | 68 |
#endif |
69 | 69 |
|
... | ... |
@@ -1180,168 +1180,169 @@ |
1180 | 1180 |
/// \brief Constructor. |
1181 | 1181 |
/// |
1182 | 1182 |
/// Construct a new ConEdgeIt iterating on the edges that |
1183 | 1183 |
/// connects nodes \c u and \c v. |
1184 | 1184 |
ConEdgeIt(const GR& g, Node u, Node v) : _graph(g), _u(u), _v(v) { |
1185 | 1185 |
Parent::operator=(findEdge(_graph, _u, _v)); |
1186 | 1186 |
} |
1187 | 1187 |
|
1188 | 1188 |
/// \brief Constructor. |
1189 | 1189 |
/// |
1190 | 1190 |
/// Construct a new ConEdgeIt that continues iterating from edge \c e. |
1191 | 1191 |
ConEdgeIt(const GR& g, Edge e) : Parent(e), _graph(g) {} |
1192 | 1192 |
|
1193 | 1193 |
/// \brief Increment operator. |
1194 | 1194 |
/// |
1195 | 1195 |
/// It increments the iterator and gives back the next edge. |
1196 | 1196 |
ConEdgeIt& operator++() { |
1197 | 1197 |
Parent::operator=(findEdge(_graph, _u, _v, *this)); |
1198 | 1198 |
return *this; |
1199 | 1199 |
} |
1200 | 1200 |
private: |
1201 | 1201 |
const GR& _graph; |
1202 | 1202 |
Node _u, _v; |
1203 | 1203 |
}; |
1204 | 1204 |
|
1205 | 1205 |
|
1206 | 1206 |
///Dynamic arc look-up between given endpoints. |
1207 | 1207 |
|
1208 | 1208 |
///Using this class, you can find an arc in a digraph from a given |
1209 | 1209 |
///source to a given target in amortized time <em>O</em>(log<em>d</em>), |
1210 | 1210 |
///where <em>d</em> is the out-degree of the source node. |
1211 | 1211 |
/// |
1212 | 1212 |
///It is possible to find \e all parallel arcs between two nodes with |
1213 | 1213 |
///the \c operator() member. |
1214 | 1214 |
/// |
1215 | 1215 |
///This is a dynamic data structure. Consider to use \ref ArcLookUp or |
1216 | 1216 |
///\ref AllArcLookUp if your digraph is not changed so frequently. |
1217 | 1217 |
/// |
1218 | 1218 |
///This class uses a self-adjusting binary search tree, the Splay tree |
1219 | 1219 |
///of Sleator and Tarjan to guarantee the logarithmic amortized |
1220 | 1220 |
///time bound for arc look-ups. This class also guarantees the |
1221 | 1221 |
///optimal time bound in a constant factor for any distribution of |
1222 | 1222 |
///queries. |
1223 | 1223 |
/// |
1224 | 1224 |
///\tparam GR The type of the underlying digraph. |
1225 | 1225 |
/// |
1226 | 1226 |
///\sa ArcLookUp |
1227 | 1227 |
///\sa AllArcLookUp |
1228 | 1228 |
template <typename GR> |
1229 | 1229 |
class DynArcLookUp |
1230 | 1230 |
: protected ItemSetTraits<GR, typename GR::Arc>::ItemNotifier::ObserverBase |
1231 | 1231 |
{ |
1232 | 1232 |
typedef typename ItemSetTraits<GR, typename GR::Arc> |
1233 | 1233 |
::ItemNotifier::ObserverBase Parent; |
1234 | 1234 |
|
1235 | 1235 |
TEMPLATE_DIGRAPH_TYPEDEFS(GR); |
1236 | 1236 |
|
1237 | 1237 |
public: |
1238 | 1238 |
|
1239 | 1239 |
/// The Digraph type |
1240 | 1240 |
typedef GR Digraph; |
1241 | 1241 |
|
1242 | 1242 |
protected: |
1243 | 1243 |
|
1244 |
class AutoNodeMap : |
|
1244 |
class AutoNodeMap : |
|
1245 |
public ItemSetTraits<GR, Node>::template Map<Arc>::Type { |
|
1245 | 1246 |
typedef typename ItemSetTraits<GR, Node>::template Map<Arc>::Type Parent; |
1246 | 1247 |
|
1247 | 1248 |
public: |
1248 | 1249 |
|
1249 | 1250 |
AutoNodeMap(const GR& digraph) : Parent(digraph, INVALID) {} |
1250 | 1251 |
|
1251 | 1252 |
virtual void add(const Node& node) { |
1252 | 1253 |
Parent::add(node); |
1253 | 1254 |
Parent::set(node, INVALID); |
1254 | 1255 |
} |
1255 | 1256 |
|
1256 | 1257 |
virtual void add(const std::vector<Node>& nodes) { |
1257 | 1258 |
Parent::add(nodes); |
1258 | 1259 |
for (int i = 0; i < int(nodes.size()); ++i) { |
1259 | 1260 |
Parent::set(nodes[i], INVALID); |
1260 | 1261 |
} |
1261 | 1262 |
} |
1262 | 1263 |
|
1263 | 1264 |
virtual void build() { |
1264 | 1265 |
Parent::build(); |
1265 | 1266 |
Node it; |
1266 | 1267 |
typename Parent::Notifier* nf = Parent::notifier(); |
1267 | 1268 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
1268 | 1269 |
Parent::set(it, INVALID); |
1269 | 1270 |
} |
1270 | 1271 |
} |
1271 | 1272 |
}; |
1272 | 1273 |
|
1273 | 1274 |
class ArcLess { |
1274 | 1275 |
const Digraph &g; |
1275 | 1276 |
public: |
1276 | 1277 |
ArcLess(const Digraph &_g) : g(_g) {} |
1277 | 1278 |
bool operator()(Arc a,Arc b) const |
1278 | 1279 |
{ |
1279 | 1280 |
return g.target(a)<g.target(b); |
1280 | 1281 |
} |
1281 | 1282 |
}; |
1282 | 1283 |
|
1283 |
protected: |
|
1284 |
protected: |
|
1284 | 1285 |
|
1285 | 1286 |
const Digraph &_g; |
1286 | 1287 |
AutoNodeMap _head; |
1287 | 1288 |
typename Digraph::template ArcMap<Arc> _parent; |
1288 | 1289 |
typename Digraph::template ArcMap<Arc> _left; |
1289 | 1290 |
typename Digraph::template ArcMap<Arc> _right; |
1290 | 1291 |
|
1291 | 1292 |
public: |
1292 | 1293 |
|
1293 | 1294 |
///Constructor |
1294 | 1295 |
|
1295 | 1296 |
///Constructor. |
1296 | 1297 |
/// |
1297 | 1298 |
///It builds up the search database. |
1298 | 1299 |
DynArcLookUp(const Digraph &g) |
1299 | 1300 |
: _g(g),_head(g),_parent(g),_left(g),_right(g) |
1300 | 1301 |
{ |
1301 | 1302 |
Parent::attach(_g.notifier(typename Digraph::Arc())); |
1302 | 1303 |
refresh(); |
1303 | 1304 |
} |
1304 | 1305 |
|
1305 | 1306 |
protected: |
1306 | 1307 |
|
1307 | 1308 |
virtual void add(const Arc& arc) { |
1308 | 1309 |
insert(arc); |
1309 | 1310 |
} |
1310 | 1311 |
|
1311 | 1312 |
virtual void add(const std::vector<Arc>& arcs) { |
1312 | 1313 |
for (int i = 0; i < int(arcs.size()); ++i) { |
1313 | 1314 |
insert(arcs[i]); |
1314 | 1315 |
} |
1315 | 1316 |
} |
1316 | 1317 |
|
1317 | 1318 |
virtual void erase(const Arc& arc) { |
1318 | 1319 |
remove(arc); |
1319 | 1320 |
} |
1320 | 1321 |
|
1321 | 1322 |
virtual void erase(const std::vector<Arc>& arcs) { |
1322 | 1323 |
for (int i = 0; i < int(arcs.size()); ++i) { |
1323 | 1324 |
remove(arcs[i]); |
1324 | 1325 |
} |
1325 | 1326 |
} |
1326 | 1327 |
|
1327 | 1328 |
virtual void build() { |
1328 | 1329 |
refresh(); |
1329 | 1330 |
} |
1330 | 1331 |
|
1331 | 1332 |
virtual void clear() { |
1332 | 1333 |
for(NodeIt n(_g);n!=INVALID;++n) { |
1333 | 1334 |
_head[n] = INVALID; |
1334 | 1335 |
} |
1335 | 1336 |
} |
1336 | 1337 |
|
1337 | 1338 |
void insert(Arc arc) { |
1338 | 1339 |
Node s = _g.source(arc); |
1339 | 1340 |
Node t = _g.target(arc); |
1340 | 1341 |
_left[arc] = INVALID; |
1341 | 1342 |
_right[arc] = INVALID; |
1342 | 1343 |
|
1343 | 1344 |
Arc e = _head[s]; |
1344 | 1345 |
if (e == INVALID) { |
1345 | 1346 |
_head[s] = arc; |
1346 | 1347 |
_parent[arc] = INVALID; |
1347 | 1348 |
return; |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#include <iostream> |
20 | 20 |
#include <vector> |
21 | 21 |
#include <cstring> |
22 | 22 |
|
23 | 23 |
#include <lemon/cplex.h> |
24 | 24 |
|
25 | 25 |
extern "C" { |
26 | 26 |
#include <ilcplex/cplex.h> |
27 | 27 |
} |
28 | 28 |
|
29 | 29 |
|
30 | 30 |
///\file |
31 | 31 |
///\brief Implementation of the LEMON-CPLEX lp solver interface. |
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
CplexEnv::LicenseError::LicenseError(int status) { |
35 | 35 |
if (!CPXgeterrorstring(0, status, _message)) { |
36 | 36 |
std::strcpy(_message, "Cplex unknown error"); |
37 | 37 |
} |
38 | 38 |
} |
39 | 39 |
|
40 | 40 |
CplexEnv::CplexEnv() { |
41 | 41 |
int status; |
42 | 42 |
_cnt = new int; |
43 | 43 |
_env = CPXopenCPLEX(&status); |
44 | 44 |
if (_env == 0) { |
45 | 45 |
delete _cnt; |
46 | 46 |
_cnt = 0; |
47 | 47 |
throw LicenseError(status); |
48 | 48 |
} |
49 | 49 |
} |
50 | 50 |
|
51 | 51 |
CplexEnv::CplexEnv(const CplexEnv& other) { |
52 | 52 |
_env = other._env; |
53 | 53 |
_cnt = other._cnt; |
54 | 54 |
++(*_cnt); |
55 | 55 |
} |
56 | 56 |
|
57 | 57 |
CplexEnv& CplexEnv::operator=(const CplexEnv& other) { |
58 | 58 |
_env = other._env; |
59 | 59 |
_cnt = other._cnt; |
60 | 60 |
++(*_cnt); |
61 | 61 |
return *this; |
62 | 62 |
} |
63 | 63 |
|
64 | 64 |
CplexEnv::~CplexEnv() { |
65 | 65 |
--(*_cnt); |
66 | 66 |
if (*_cnt == 0) { |
67 | 67 |
delete _cnt; |
68 | 68 |
CPXcloseCPLEX(&_env); |
69 | 69 |
} |
... | ... |
@@ -395,129 +395,129 @@ |
395 | 395 |
*b = std::make_pair(i, x[i]); |
396 | 396 |
++b; |
397 | 397 |
} |
398 | 398 |
} |
399 | 399 |
} |
400 | 400 |
|
401 | 401 |
void CplexBase::_setObjCoeff(int i, Value obj_coef) |
402 | 402 |
{ |
403 | 403 |
CPXchgobj(cplexEnv(), _prob, 1, &i, &obj_coef); |
404 | 404 |
} |
405 | 405 |
|
406 | 406 |
CplexBase::Value CplexBase::_getObjCoeff(int i) const |
407 | 407 |
{ |
408 | 408 |
Value x; |
409 | 409 |
CPXgetobj(cplexEnv(), _prob, &x, i, i); |
410 | 410 |
return x; |
411 | 411 |
} |
412 | 412 |
|
413 | 413 |
void CplexBase::_setSense(CplexBase::Sense sense) { |
414 | 414 |
switch (sense) { |
415 | 415 |
case MIN: |
416 | 416 |
CPXchgobjsen(cplexEnv(), _prob, CPX_MIN); |
417 | 417 |
break; |
418 | 418 |
case MAX: |
419 | 419 |
CPXchgobjsen(cplexEnv(), _prob, CPX_MAX); |
420 | 420 |
break; |
421 | 421 |
} |
422 | 422 |
} |
423 | 423 |
|
424 | 424 |
CplexBase::Sense CplexBase::_getSense() const { |
425 | 425 |
switch (CPXgetobjsen(cplexEnv(), _prob)) { |
426 | 426 |
case CPX_MIN: |
427 | 427 |
return MIN; |
428 | 428 |
case CPX_MAX: |
429 | 429 |
return MAX; |
430 | 430 |
default: |
431 | 431 |
LEMON_ASSERT(false, "Invalid sense"); |
432 | 432 |
return CplexBase::Sense(); |
433 | 433 |
} |
434 | 434 |
} |
435 | 435 |
|
436 | 436 |
void CplexBase::_clear() { |
437 | 437 |
CPXfreeprob(cplexEnv(),&_prob); |
438 | 438 |
int status; |
439 | 439 |
_prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem"); |
440 | 440 |
rows.clear(); |
441 | 441 |
cols.clear(); |
442 | 442 |
} |
443 | 443 |
|
444 | 444 |
void CplexBase::_messageLevel(MessageLevel level) { |
445 | 445 |
switch (level) { |
446 | 446 |
case MESSAGE_NOTHING: |
447 | 447 |
_message_enabled = false; |
448 | 448 |
break; |
449 | 449 |
case MESSAGE_ERROR: |
450 | 450 |
case MESSAGE_WARNING: |
451 | 451 |
case MESSAGE_NORMAL: |
452 | 452 |
case MESSAGE_VERBOSE: |
453 | 453 |
_message_enabled = true; |
454 | 454 |
break; |
455 | 455 |
} |
456 | 456 |
} |
457 | 457 |
|
458 | 458 |
void CplexBase::_applyMessageLevel() { |
459 |
CPXsetintparam(cplexEnv(), CPX_PARAM_SCRIND, |
|
459 |
CPXsetintparam(cplexEnv(), CPX_PARAM_SCRIND, |
|
460 | 460 |
_message_enabled ? CPX_ON : CPX_OFF); |
461 | 461 |
} |
462 | 462 |
|
463 | 463 |
// CplexLp members |
464 | 464 |
|
465 | 465 |
CplexLp::CplexLp() |
466 | 466 |
: LpBase(), LpSolver(), CplexBase() {} |
467 | 467 |
|
468 | 468 |
CplexLp::CplexLp(const CplexEnv& env) |
469 | 469 |
: LpBase(), LpSolver(), CplexBase(env) {} |
470 | 470 |
|
471 | 471 |
CplexLp::CplexLp(const CplexLp& other) |
472 | 472 |
: LpBase(), LpSolver(), CplexBase(other) {} |
473 | 473 |
|
474 | 474 |
CplexLp::~CplexLp() {} |
475 | 475 |
|
476 | 476 |
CplexLp* CplexLp::newSolver() const { return new CplexLp; } |
477 | 477 |
CplexLp* CplexLp::cloneSolver() const {return new CplexLp(*this); } |
478 | 478 |
|
479 | 479 |
const char* CplexLp::_solverName() const { return "CplexLp"; } |
480 | 480 |
|
481 | 481 |
void CplexLp::_clear_temporals() { |
482 | 482 |
_col_status.clear(); |
483 | 483 |
_row_status.clear(); |
484 | 484 |
_primal_ray.clear(); |
485 | 485 |
_dual_ray.clear(); |
486 | 486 |
} |
487 | 487 |
|
488 | 488 |
// The routine returns zero unless an error occurred during the |
489 | 489 |
// optimization. Examples of errors include exhausting available |
490 | 490 |
// memory (CPXERR_NO_MEMORY) or encountering invalid data in the |
491 | 491 |
// CPLEX problem object (CPXERR_NO_PROBLEM). Exceeding a |
492 | 492 |
// user-specified CPLEX limit, or proving the model infeasible or |
493 | 493 |
// unbounded, are not considered errors. Note that a zero return |
494 | 494 |
// value does not necessarily mean that a solution exists. Use query |
495 | 495 |
// routines CPXsolninfo, CPXgetstat, and CPXsolution to obtain |
496 | 496 |
// further information about the status of the optimization. |
497 | 497 |
CplexLp::SolveExitStatus CplexLp::convertStatus(int status) { |
498 | 498 |
#if CPX_VERSION >= 800 |
499 | 499 |
if (status == 0) { |
500 | 500 |
switch (CPXgetstat(cplexEnv(), _prob)) { |
501 | 501 |
case CPX_STAT_OPTIMAL: |
502 | 502 |
case CPX_STAT_INFEASIBLE: |
503 | 503 |
case CPX_STAT_UNBOUNDED: |
504 | 504 |
return SOLVED; |
505 | 505 |
default: |
506 | 506 |
return UNSOLVED; |
507 | 507 |
} |
508 | 508 |
} else { |
509 | 509 |
return UNSOLVED; |
510 | 510 |
} |
511 | 511 |
#else |
512 | 512 |
if (status == 0) { |
513 | 513 |
//We want to exclude some cases |
514 | 514 |
switch (CPXgetstat(cplexEnv(), _prob)) { |
515 | 515 |
case CPX_OBJ_LIM: |
516 | 516 |
case CPX_IT_LIM_FEAS: |
517 | 517 |
case CPX_IT_LIM_INFEAS: |
518 | 518 |
case CPX_TIME_LIM_FEAS: |
519 | 519 |
case CPX_TIME_LIM_INFEAS: |
520 | 520 |
return UNSOLVED; |
521 | 521 |
default: |
522 | 522 |
return SOLVED; |
523 | 523 |
} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_DFS_H |
20 | 20 |
#define LEMON_DFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 | 24 |
///\brief DFS algorithm. |
25 | 25 |
|
26 | 26 |
#include <lemon/list_graph.h> |
27 | 27 |
#include <lemon/bits/path_dump.h> |
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/error.h> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/path.h> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
///Default traits class of Dfs class. |
36 | 36 |
|
37 | 37 |
///Default traits class of Dfs class. |
38 | 38 |
///\tparam GR Digraph type. |
39 | 39 |
template<class GR> |
40 | 40 |
struct DfsDefaultTraits |
41 | 41 |
{ |
42 | 42 |
///The type of the digraph the algorithm runs on. |
43 | 43 |
typedef GR Digraph; |
44 | 44 |
|
45 | 45 |
///\brief The type of the map that stores the predecessor |
46 | 46 |
///arcs of the %DFS paths. |
47 | 47 |
/// |
48 | 48 |
///The type of the map that stores the predecessor |
49 | 49 |
///arcs of the %DFS paths. |
50 | 50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
52 | 52 |
///Instantiates a \c PredMap. |
53 | 53 |
|
54 | 54 |
///This function instantiates a \ref PredMap. |
55 | 55 |
///\param g is the digraph, to which we would like to define the |
56 | 56 |
///\ref PredMap. |
57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
58 | 58 |
{ |
59 | 59 |
return new PredMap(g); |
60 | 60 |
} |
61 | 61 |
|
62 | 62 |
///The type of the map that indicates which nodes are processed. |
63 | 63 |
|
64 | 64 |
///The type of the map that indicates which nodes are processed. |
65 | 65 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
66 | 66 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
67 | 67 |
///Instantiates a \c ProcessedMap. |
68 | 68 |
|
69 | 69 |
///This function instantiates a \ref ProcessedMap. |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_DIMACS_H |
20 | 20 |
#define LEMON_DIMACS_H |
21 | 21 |
|
22 | 22 |
#include <iostream> |
23 | 23 |
#include <string> |
24 | 24 |
#include <vector> |
25 | 25 |
#include <limits> |
26 | 26 |
#include <lemon/maps.h> |
27 | 27 |
#include <lemon/error.h> |
28 | 28 |
/// \ingroup dimacs_group |
29 | 29 |
/// \file |
30 | 30 |
/// \brief DIMACS file format reader. |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
/// \addtogroup dimacs_group |
35 | 35 |
/// @{ |
36 | 36 |
|
37 | 37 |
/// DIMACS file type descriptor. |
38 | 38 |
struct DimacsDescriptor |
39 | 39 |
{ |
40 | 40 |
///\brief DIMACS file type enum |
41 | 41 |
/// |
42 | 42 |
///DIMACS file type enum. |
43 | 43 |
enum Type { |
44 | 44 |
NONE, ///< Undefined type. |
45 | 45 |
MIN, ///< DIMACS file type for minimum cost flow problems. |
46 | 46 |
MAX, ///< DIMACS file type for maximum flow problems. |
47 | 47 |
SP, ///< DIMACS file type for shostest path problems. |
48 | 48 |
MAT ///< DIMACS file type for plain graphs and matching problems. |
49 | 49 |
}; |
50 | 50 |
///The file type |
51 | 51 |
Type type; |
52 | 52 |
///The number of nodes in the graph |
53 | 53 |
int nodeNum; |
54 | 54 |
///The number of edges in the graph |
55 | 55 |
int edgeNum; |
56 | 56 |
int lineShift; |
57 | 57 |
///Constructor. It sets the type to \c NONE. |
58 | 58 |
DimacsDescriptor() : type(NONE) {} |
59 | 59 |
}; |
60 | 60 |
|
61 | 61 |
///Discover the type of a DIMACS file |
62 | 62 |
|
63 | 63 |
///This function starts seeking the beginning of the given file for the |
64 |
///problem type and size info. |
|
64 |
///problem type and size info. |
|
65 | 65 |
///The found data is returned in a special struct that can be evaluated |
66 | 66 |
///and passed to the appropriate reader function. |
67 | 67 |
DimacsDescriptor dimacsType(std::istream& is) |
68 | 68 |
{ |
69 | 69 |
DimacsDescriptor r; |
70 | 70 |
std::string problem,str; |
71 | 71 |
char c; |
72 | 72 |
r.lineShift=0; |
73 | 73 |
while (is >> c) |
74 | 74 |
switch(c) |
75 | 75 |
{ |
76 | 76 |
case 'p': |
77 | 77 |
if(is >> problem >> r.nodeNum >> r.edgeNum) |
78 | 78 |
{ |
79 | 79 |
getline(is, str); |
80 | 80 |
r.lineShift++; |
81 | 81 |
if(problem=="min") r.type=DimacsDescriptor::MIN; |
82 | 82 |
else if(problem=="max") r.type=DimacsDescriptor::MAX; |
83 | 83 |
else if(problem=="sp") r.type=DimacsDescriptor::SP; |
84 | 84 |
else if(problem=="mat") r.type=DimacsDescriptor::MAT; |
85 | 85 |
else throw FormatError("Unknown problem type"); |
86 | 86 |
return r; |
87 | 87 |
} |
88 | 88 |
else |
89 | 89 |
{ |
90 | 90 |
throw FormatError("Missing or wrong problem type declaration."); |
91 | 91 |
} |
92 | 92 |
break; |
93 | 93 |
case 'c': |
94 | 94 |
getline(is, str); |
95 | 95 |
r.lineShift++; |
96 | 96 |
break; |
97 | 97 |
default: |
98 | 98 |
throw FormatError("Unknown DIMACS declaration."); |
99 | 99 |
} |
100 | 100 |
throw FormatError("Missing problem type declaration."); |
101 | 101 |
} |
102 | 102 |
|
103 | 103 |
|
104 | 104 |
/// \brief DIMACS minimum cost flow reader function. |
105 | 105 |
/// |
106 | 106 |
/// This function reads a minimum cost flow instance from DIMACS format, |
107 | 107 |
/// i.e. from a DIMACS file having a line starting with |
108 | 108 |
/// \code |
109 | 109 |
/// p min |
110 | 110 |
/// \endcode |
111 | 111 |
/// At the beginning, \c g is cleared by \c g.clear(). The supply |
112 | 112 |
/// amount of the nodes are written to the \c supply node map |
113 | 113 |
/// (they are signed values). The lower bounds, capacities and costs |
114 | 114 |
/// of the arcs are written to the \c lower, \c capacity and \c cost |
115 | 115 |
/// arc maps. |
116 | 116 |
/// |
117 | 117 |
/// If the capacity of an arc is less than the lower bound, it will |
118 | 118 |
/// be set to "infinite" instead. The actual value of "infinite" is |
119 | 119 |
/// contolled by the \c infty parameter. If it is 0 (the default value), |
120 | 120 |
/// \c std::numeric_limits<Capacity>::infinity() will be used if available, |
121 | 121 |
/// \c std::numeric_limits<Capacity>::max() otherwise. If \c infty is set to |
122 | 122 |
/// a non-zero value, that value will be used as "infinite". |
123 | 123 |
/// |
124 | 124 |
/// If the file type was previously evaluated by dimacsType(), then |
125 | 125 |
/// the descriptor struct should be given by the \c dest parameter. |
126 | 126 |
template <typename Digraph, typename LowerMap, |
127 | 127 |
typename CapacityMap, typename CostMap, |
128 | 128 |
typename SupplyMap> |
... | ... |
@@ -151,298 +151,298 @@ |
151 | 151 |
supply.set(nodes[k], 0); |
152 | 152 |
} |
153 | 153 |
|
154 | 154 |
typename SupplyMap::Value sup; |
155 | 155 |
typename CapacityMap::Value low; |
156 | 156 |
typename CapacityMap::Value cap; |
157 | 157 |
typename CostMap::Value co; |
158 | 158 |
typedef typename CapacityMap::Value Capacity; |
159 | 159 |
if(infty==0) |
160 | 160 |
infty = std::numeric_limits<Capacity>::has_infinity ? |
161 | 161 |
std::numeric_limits<Capacity>::infinity() : |
162 | 162 |
std::numeric_limits<Capacity>::max(); |
163 | 163 |
|
164 | 164 |
while (is >> c) { |
165 | 165 |
switch (c) { |
166 | 166 |
case 'c': // comment line |
167 | 167 |
getline(is, str); |
168 | 168 |
break; |
169 | 169 |
case 'n': // node definition line |
170 | 170 |
is >> i >> sup; |
171 | 171 |
getline(is, str); |
172 | 172 |
supply.set(nodes[i], sup); |
173 | 173 |
break; |
174 | 174 |
case 'a': // arc definition line |
175 | 175 |
is >> i >> j >> low >> cap >> co; |
176 | 176 |
getline(is, str); |
177 | 177 |
e = g.addArc(nodes[i], nodes[j]); |
178 | 178 |
lower.set(e, low); |
179 | 179 |
if (cap >= low) |
180 | 180 |
capacity.set(e, cap); |
181 | 181 |
else |
182 | 182 |
capacity.set(e, infty); |
183 | 183 |
cost.set(e, co); |
184 | 184 |
break; |
185 | 185 |
} |
186 | 186 |
} |
187 | 187 |
} |
188 | 188 |
|
189 | 189 |
template<typename Digraph, typename CapacityMap> |
190 | 190 |
void _readDimacs(std::istream& is, |
191 | 191 |
Digraph &g, |
192 | 192 |
CapacityMap& capacity, |
193 | 193 |
typename Digraph::Node &s, |
194 | 194 |
typename Digraph::Node &t, |
195 | 195 |
typename CapacityMap::Value infty = 0, |
196 | 196 |
DimacsDescriptor desc=DimacsDescriptor()) { |
197 | 197 |
g.clear(); |
198 | 198 |
s=t=INVALID; |
199 | 199 |
std::vector<typename Digraph::Node> nodes; |
200 | 200 |
typename Digraph::Arc e; |
201 | 201 |
char c, d; |
202 | 202 |
int i, j; |
203 | 203 |
typename CapacityMap::Value _cap; |
204 | 204 |
std::string str; |
205 | 205 |
nodes.resize(desc.nodeNum + 1); |
206 | 206 |
for (int k = 1; k <= desc.nodeNum; ++k) { |
207 | 207 |
nodes[k] = g.addNode(); |
208 | 208 |
} |
209 | 209 |
typedef typename CapacityMap::Value Capacity; |
210 | 210 |
|
211 | 211 |
if(infty==0) |
212 | 212 |
infty = std::numeric_limits<Capacity>::has_infinity ? |
213 | 213 |
std::numeric_limits<Capacity>::infinity() : |
214 | 214 |
std::numeric_limits<Capacity>::max(); |
215 |
|
|
215 |
|
|
216 | 216 |
while (is >> c) { |
217 | 217 |
switch (c) { |
218 | 218 |
case 'c': // comment line |
219 | 219 |
getline(is, str); |
220 | 220 |
break; |
221 | 221 |
case 'n': // node definition line |
222 | 222 |
if (desc.type==DimacsDescriptor::SP) { // shortest path problem |
223 | 223 |
is >> i; |
224 | 224 |
getline(is, str); |
225 | 225 |
s = nodes[i]; |
226 | 226 |
} |
227 | 227 |
if (desc.type==DimacsDescriptor::MAX) { // max flow problem |
228 | 228 |
is >> i >> d; |
229 | 229 |
getline(is, str); |
230 | 230 |
if (d == 's') s = nodes[i]; |
231 | 231 |
if (d == 't') t = nodes[i]; |
232 | 232 |
} |
233 | 233 |
break; |
234 | 234 |
case 'a': // arc definition line |
235 | 235 |
if (desc.type==DimacsDescriptor::SP) { |
236 | 236 |
is >> i >> j >> _cap; |
237 | 237 |
getline(is, str); |
238 | 238 |
e = g.addArc(nodes[i], nodes[j]); |
239 | 239 |
capacity.set(e, _cap); |
240 |
} |
|
240 |
} |
|
241 | 241 |
else if (desc.type==DimacsDescriptor::MAX) { |
242 | 242 |
is >> i >> j >> _cap; |
243 | 243 |
getline(is, str); |
244 | 244 |
e = g.addArc(nodes[i], nodes[j]); |
245 | 245 |
if (_cap >= 0) |
246 | 246 |
capacity.set(e, _cap); |
247 | 247 |
else |
248 | 248 |
capacity.set(e, infty); |
249 | 249 |
} |
250 | 250 |
else { |
251 | 251 |
is >> i >> j; |
252 | 252 |
getline(is, str); |
253 | 253 |
g.addArc(nodes[i], nodes[j]); |
254 | 254 |
} |
255 | 255 |
break; |
256 | 256 |
} |
257 | 257 |
} |
258 | 258 |
} |
259 | 259 |
|
260 | 260 |
/// \brief DIMACS maximum flow reader function. |
261 | 261 |
/// |
262 | 262 |
/// This function reads a maximum flow instance from DIMACS format, |
263 | 263 |
/// i.e. from a DIMACS file having a line starting with |
264 | 264 |
/// \code |
265 | 265 |
/// p max |
266 | 266 |
/// \endcode |
267 | 267 |
/// At the beginning, \c g is cleared by \c g.clear(). The arc |
268 | 268 |
/// capacities are written to the \c capacity arc map and \c s and |
269 | 269 |
/// \c t are set to the source and the target nodes. |
270 | 270 |
/// |
271 | 271 |
/// If the capacity of an arc is negative, it will |
272 | 272 |
/// be set to "infinite" instead. The actual value of "infinite" is |
273 | 273 |
/// contolled by the \c infty parameter. If it is 0 (the default value), |
274 | 274 |
/// \c std::numeric_limits<Capacity>::infinity() will be used if available, |
275 | 275 |
/// \c std::numeric_limits<Capacity>::max() otherwise. If \c infty is set to |
276 | 276 |
/// a non-zero value, that value will be used as "infinite". |
277 | 277 |
/// |
278 | 278 |
/// If the file type was previously evaluated by dimacsType(), then |
279 | 279 |
/// the descriptor struct should be given by the \c dest parameter. |
280 | 280 |
template<typename Digraph, typename CapacityMap> |
281 | 281 |
void readDimacsMax(std::istream& is, |
282 | 282 |
Digraph &g, |
283 | 283 |
CapacityMap& capacity, |
284 | 284 |
typename Digraph::Node &s, |
285 | 285 |
typename Digraph::Node &t, |
286 | 286 |
typename CapacityMap::Value infty = 0, |
287 | 287 |
DimacsDescriptor desc=DimacsDescriptor()) { |
288 | 288 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
289 | 289 |
if(desc.type!=DimacsDescriptor::MAX) |
290 | 290 |
throw FormatError("Problem type mismatch"); |
291 | 291 |
_readDimacs(is,g,capacity,s,t,infty,desc); |
292 | 292 |
} |
293 | 293 |
|
294 | 294 |
/// \brief DIMACS shortest path reader function. |
295 | 295 |
/// |
296 | 296 |
/// This function reads a shortest path instance from DIMACS format, |
297 | 297 |
/// i.e. from a DIMACS file having a line starting with |
298 | 298 |
/// \code |
299 | 299 |
/// p sp |
300 | 300 |
/// \endcode |
301 | 301 |
/// At the beginning, \c g is cleared by \c g.clear(). The arc |
302 | 302 |
/// lengths are written to the \c length arc map and \c s is set to the |
303 | 303 |
/// source node. |
304 | 304 |
/// |
305 | 305 |
/// If the file type was previously evaluated by dimacsType(), then |
306 | 306 |
/// the descriptor struct should be given by the \c dest parameter. |
307 | 307 |
template<typename Digraph, typename LengthMap> |
308 | 308 |
void readDimacsSp(std::istream& is, |
309 | 309 |
Digraph &g, |
310 | 310 |
LengthMap& length, |
311 | 311 |
typename Digraph::Node &s, |
312 | 312 |
DimacsDescriptor desc=DimacsDescriptor()) { |
313 | 313 |
typename Digraph::Node t; |
314 | 314 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
315 | 315 |
if(desc.type!=DimacsDescriptor::SP) |
316 | 316 |
throw FormatError("Problem type mismatch"); |
317 | 317 |
_readDimacs(is, g, length, s, t, 0, desc); |
318 | 318 |
} |
319 | 319 |
|
320 | 320 |
/// \brief DIMACS capacitated digraph reader function. |
321 | 321 |
/// |
322 | 322 |
/// This function reads an arc capacitated digraph instance from |
323 | 323 |
/// DIMACS 'max' or 'sp' format. |
324 | 324 |
/// At the beginning, \c g is cleared by \c g.clear() |
325 | 325 |
/// and the arc capacities/lengths are written to the \c capacity |
326 | 326 |
/// arc map. |
327 | 327 |
/// |
328 | 328 |
/// In case of the 'max' format, if the capacity of an arc is negative, |
329 | 329 |
/// it will |
330 | 330 |
/// be set to "infinite" instead. The actual value of "infinite" is |
331 | 331 |
/// contolled by the \c infty parameter. If it is 0 (the default value), |
332 | 332 |
/// \c std::numeric_limits<Capacity>::infinity() will be used if available, |
333 | 333 |
/// \c std::numeric_limits<Capacity>::max() otherwise. If \c infty is set to |
334 | 334 |
/// a non-zero value, that value will be used as "infinite". |
335 | 335 |
/// |
336 | 336 |
/// If the file type was previously evaluated by dimacsType(), then |
337 | 337 |
/// the descriptor struct should be given by the \c dest parameter. |
338 | 338 |
template<typename Digraph, typename CapacityMap> |
339 | 339 |
void readDimacsCap(std::istream& is, |
340 | 340 |
Digraph &g, |
341 | 341 |
CapacityMap& capacity, |
342 | 342 |
typename CapacityMap::Value infty = 0, |
343 | 343 |
DimacsDescriptor desc=DimacsDescriptor()) { |
344 | 344 |
typename Digraph::Node u,v; |
345 | 345 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
346 | 346 |
if(desc.type!=DimacsDescriptor::MAX || desc.type!=DimacsDescriptor::SP) |
347 | 347 |
throw FormatError("Problem type mismatch"); |
348 | 348 |
_readDimacs(is, g, capacity, u, v, infty, desc); |
349 | 349 |
} |
350 | 350 |
|
351 | 351 |
template<typename Graph> |
352 | 352 |
typename enable_if<lemon::UndirectedTagIndicator<Graph>,void>::type |
353 | 353 |
_addArcEdge(Graph &g, typename Graph::Node s, typename Graph::Node t, |
354 | 354 |
dummy<0> = 0) |
355 | 355 |
{ |
356 | 356 |
g.addEdge(s,t); |
357 | 357 |
} |
358 | 358 |
template<typename Graph> |
359 | 359 |
typename disable_if<lemon::UndirectedTagIndicator<Graph>,void>::type |
360 | 360 |
_addArcEdge(Graph &g, typename Graph::Node s, typename Graph::Node t, |
361 | 361 |
dummy<1> = 1) |
362 | 362 |
{ |
363 | 363 |
g.addArc(s,t); |
364 | 364 |
} |
365 |
|
|
365 |
|
|
366 | 366 |
/// \brief DIMACS plain (di)graph reader function. |
367 | 367 |
/// |
368 | 368 |
/// This function reads a plain (di)graph without any designated nodes |
369 |
/// and maps (e.g. a matching instance) from DIMACS format, i.e. from |
|
369 |
/// and maps (e.g. a matching instance) from DIMACS format, i.e. from |
|
370 | 370 |
/// DIMACS files having a line starting with |
371 | 371 |
/// \code |
372 | 372 |
/// p mat |
373 | 373 |
/// \endcode |
374 | 374 |
/// At the beginning, \c g is cleared by \c g.clear(). |
375 | 375 |
/// |
376 | 376 |
/// If the file type was previously evaluated by dimacsType(), then |
377 | 377 |
/// the descriptor struct should be given by the \c dest parameter. |
378 | 378 |
template<typename Graph> |
379 | 379 |
void readDimacsMat(std::istream& is, Graph &g, |
380 | 380 |
DimacsDescriptor desc=DimacsDescriptor()) |
381 | 381 |
{ |
382 | 382 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is); |
383 | 383 |
if(desc.type!=DimacsDescriptor::MAT) |
384 | 384 |
throw FormatError("Problem type mismatch"); |
385 | 385 |
|
386 | 386 |
g.clear(); |
387 | 387 |
std::vector<typename Graph::Node> nodes; |
388 | 388 |
char c; |
389 | 389 |
int i, j; |
390 | 390 |
std::string str; |
391 | 391 |
nodes.resize(desc.nodeNum + 1); |
392 | 392 |
for (int k = 1; k <= desc.nodeNum; ++k) { |
393 | 393 |
nodes[k] = g.addNode(); |
394 | 394 |
} |
395 |
|
|
395 |
|
|
396 | 396 |
while (is >> c) { |
397 | 397 |
switch (c) { |
398 | 398 |
case 'c': // comment line |
399 | 399 |
getline(is, str); |
400 | 400 |
break; |
401 | 401 |
case 'n': // node definition line |
402 | 402 |
break; |
403 | 403 |
case 'a': // arc definition line |
404 | 404 |
is >> i >> j; |
405 | 405 |
getline(is, str); |
406 | 406 |
_addArcEdge(g,nodes[i], nodes[j]); |
407 | 407 |
break; |
408 | 408 |
} |
409 | 409 |
} |
410 | 410 |
} |
411 | 411 |
|
412 | 412 |
/// DIMACS plain digraph writer function. |
413 | 413 |
/// |
414 | 414 |
/// This function writes a digraph without any designated nodes and |
415 | 415 |
/// maps into DIMACS format, i.e. into DIMACS file having a line |
416 | 416 |
/// starting with |
417 | 417 |
/// \code |
418 | 418 |
/// p mat |
419 | 419 |
/// \endcode |
420 | 420 |
/// If \c comment is not empty, then it will be printed in the first line |
421 | 421 |
/// prefixed by 'c'. |
422 | 422 |
template<typename Digraph> |
423 | 423 |
void writeDimacsMat(std::ostream& os, const Digraph &g, |
424 | 424 |
std::string comment="") { |
425 | 425 |
typedef typename Digraph::NodeIt NodeIt; |
426 | 426 |
typedef typename Digraph::ArcIt ArcIt; |
427 | 427 |
|
428 | 428 |
if(!comment.empty()) |
429 | 429 |
os << "c " << comment << std::endl; |
430 | 430 |
os << "p mat " << g.nodeNum() << " " << g.arcNum() << std::endl; |
431 | 431 |
|
432 | 432 |
typename Digraph::template NodeMap<int> nodes(g); |
433 | 433 |
int i = 1; |
434 | 434 |
for(NodeIt v(g); v != INVALID; ++v) { |
435 | 435 |
nodes.set(v, i); |
436 | 436 |
++i; |
437 | 437 |
} |
438 | 438 |
for(ArcIt e(g); e != INVALID; ++e) { |
439 | 439 |
os << "a " << nodes[g.source(e)] << " " << nodes[g.target(e)] |
440 | 440 |
<< std::endl; |
441 | 441 |
} |
442 | 442 |
} |
443 | 443 |
|
444 | 444 |
/// @} |
445 | 445 |
|
446 | 446 |
} //namespace lemon |
447 | 447 |
|
448 | 448 |
#endif //LEMON_DIMACS_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 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_EDGE_SET_H |
20 | 20 |
#define LEMON_EDGE_SET_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
#include <lemon/bits/edge_set_extender.h> |
24 | 24 |
|
25 | 25 |
/// \ingroup graphs |
26 | 26 |
/// \file |
27 | 27 |
/// \brief ArcSet and EdgeSet classes. |
28 | 28 |
/// |
29 | 29 |
/// Graphs which use another graph's node-set as own. |
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 | 32 |
template <typename GR> |
33 | 33 |
class ListArcSetBase { |
34 | 34 |
public: |
35 | 35 |
|
36 | 36 |
typedef typename GR::Node Node; |
37 | 37 |
typedef typename GR::NodeIt NodeIt; |
38 | 38 |
|
39 | 39 |
protected: |
40 | 40 |
|
41 | 41 |
struct NodeT { |
42 | 42 |
int first_out, first_in; |
43 | 43 |
NodeT() : first_out(-1), first_in(-1) {} |
44 | 44 |
}; |
45 | 45 |
|
46 | 46 |
typedef typename ItemSetTraits<GR, Node>:: |
47 | 47 |
template Map<NodeT>::Type NodesImplBase; |
48 | 48 |
|
49 | 49 |
NodesImplBase* _nodes; |
50 | 50 |
|
51 | 51 |
struct ArcT { |
52 | 52 |
Node source, target; |
53 | 53 |
int next_out, next_in; |
54 | 54 |
int prev_out, prev_in; |
55 | 55 |
ArcT() : prev_out(-1), prev_in(-1) {} |
56 | 56 |
}; |
57 | 57 |
|
58 | 58 |
std::vector<ArcT> arcs; |
59 | 59 |
|
60 | 60 |
int first_arc; |
61 | 61 |
int first_free_arc; |
62 | 62 |
|
63 | 63 |
const GR* _graph; |
64 | 64 |
|
65 | 65 |
void initalize(const GR& graph, NodesImplBase& nodes) { |
66 | 66 |
_graph = &graph; |
67 | 67 |
_nodes = &nodes; |
68 | 68 |
} |
69 | 69 |
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)