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. |
70 | 70 |
|
71 | 71 |
The main parts of LEMON are the different graph structures, generic |
72 | 72 |
graph algorithms, graph concepts, which couple them, and graph |
73 | 73 |
adaptors. While the previous notions are more or less clear, the |
74 | 74 |
latter one needs further explanation. Graph adaptors are graph classes |
75 | 75 |
which serve for considering graph structures in different ways. |
76 | 76 |
|
77 | 77 |
A short example makes this much clearer. Suppose that we have an |
78 | 78 |
instance \c g of a directed graph type, say ListDigraph and an algorithm |
79 | 79 |
\code |
80 | 80 |
template <typename Digraph> |
81 | 81 |
int algorithm(const Digraph&); |
82 | 82 |
\endcode |
83 | 83 |
is needed to run on the reverse oriented graph. It may be expensive |
84 | 84 |
(in time or in memory usage) to copy \c g with the reversed |
85 | 85 |
arcs. In this case, an adaptor class is used, which (according |
86 | 86 |
to LEMON \ref concepts::Digraph "digraph concepts") works as a digraph. |
87 | 87 |
The adaptor uses the original digraph structure and digraph operations when |
88 | 88 |
methods of the reversed oriented graph are called. This means that the adaptor |
89 | 89 |
have minor memory usage, and do not perform sophisticated algorithmic |
90 | 90 |
actions. The purpose of it is to give a tool for the cases when a |
91 | 91 |
graph have to be used in a specific alteration. If this alteration is |
92 | 92 |
obtained by a usual construction like filtering the node or the arc set or |
93 | 93 |
considering a new orientation, then an adaptor is worthwhile to use. |
94 | 94 |
To come back to the reverse oriented graph, in this situation |
95 | 95 |
\code |
96 | 96 |
template<typename Digraph> class ReverseDigraph; |
97 | 97 |
\endcode |
98 | 98 |
template class can be used. The code looks as follows |
99 | 99 |
\code |
100 | 100 |
ListDigraph g; |
101 | 101 |
ReverseDigraph<ListDigraph> rg(g); |
102 | 102 |
int result = algorithm(rg); |
103 | 103 |
\endcode |
104 | 104 |
During running the algorithm, the original digraph \c g is untouched. |
105 | 105 |
This techniques give rise to an elegant code, and based on stable |
106 | 106 |
graph adaptors, complex algorithms can be implemented easily. |
107 | 107 |
|
108 | 108 |
In flow, circulation and matching problems, the residual |
109 | 109 |
graph is of particular importance. Combining an adaptor implementing |
110 | 110 |
this with shortest path algorithms or minimum mean cycle algorithms, |
111 | 111 |
a range of weighted and cardinality optimization algorithms can be |
112 | 112 |
obtained. For other examples, the interested user is referred to the |
113 | 113 |
detailed documentation of particular adaptors. |
114 | 114 |
|
115 | 115 |
The behavior of graph adaptors can be very different. Some of them keep |
116 | 116 |
capabilities of the original graph while in other cases this would be |
117 | 117 |
meaningless. This means that the concepts that they meet depend |
118 | 118 |
on the graph adaptor, and the wrapped graph. |
119 | 119 |
For example, if an arc of a reversed digraph is deleted, this is carried |
120 | 120 |
out by deleting the corresponding arc of the original digraph, thus the |
121 | 121 |
adaptor modifies the original digraph. |
122 | 122 |
However in case of a residual digraph, this operation has no sense. |
123 | 123 |
|
124 | 124 |
Let us stand one more example here to simplify your work. |
125 | 125 |
ReverseDigraph has constructor |
126 | 126 |
\code |
127 | 127 |
ReverseDigraph(Digraph& digraph); |
128 | 128 |
\endcode |
129 | 129 |
This means that in a situation, when a <tt>const %ListDigraph&</tt> |
130 | 130 |
reference to a graph is given, then it have to be instantiated with |
131 | 131 |
<tt>Digraph=const %ListDigraph</tt>. |
132 | 132 |
\code |
133 | 133 |
int algorithm1(const ListDigraph& g) { |
134 | 134 |
ReverseDigraph<const ListDigraph> rg(g); |
135 | 135 |
return algorithm2(rg); |
136 | 136 |
} |
137 | 137 |
\endcode |
138 | 138 |
*/ |
139 | 139 |
|
140 | 140 |
/** |
141 | 141 |
@defgroup maps Maps |
142 | 142 |
@ingroup datas |
143 | 143 |
\brief Map structures implemented in LEMON. |
144 | 144 |
|
145 | 145 |
This group contains the map structures implemented in LEMON. |
146 | 146 |
|
147 | 147 |
LEMON provides several special purpose maps and map adaptors that e.g. combine |
148 | 148 |
new maps from existing ones. |
149 | 149 |
|
150 | 150 |
<b>See also:</b> \ref map_concepts "Map Concepts". |
151 | 151 |
*/ |
152 | 152 |
|
153 | 153 |
/** |
154 | 154 |
@defgroup graph_maps Graph Maps |
155 | 155 |
@ingroup maps |
156 | 156 |
\brief Special graph-related maps. |
157 | 157 |
|
158 | 158 |
This group contains maps that are specifically designed to assign |
159 | 159 |
values to the nodes and arcs/edges of graphs. |
160 | 160 |
|
161 | 161 |
If you are looking for the standard graph maps (\c NodeMap, \c ArcMap, |
162 | 162 |
\c EdgeMap), see the \ref graph_concepts "Graph Structure Concepts". |
163 | 163 |
*/ |
164 | 164 |
|
165 | 165 |
/** |
166 | 166 |
\defgroup map_adaptors Map Adaptors |
167 | 167 |
\ingroup maps |
168 | 168 |
\brief Tools to create new maps from existing ones |
169 | 169 |
|
170 | 170 |
This group contains map adaptors that are used to create "implicit" |
171 | 171 |
maps from other maps. |
172 | 172 |
|
173 | 173 |
Most of them are \ref concepts::ReadMap "read-only maps". |
174 | 174 |
They can make arithmetic and logical operations between one or two maps |
175 | 175 |
(negation, shifting, addition, multiplication, logical 'and', 'or', |
176 | 176 |
'not' etc.) or e.g. convert a map to another one of different Value type. |
177 | 177 |
|
178 | 178 |
The typical usage of this classes is passing implicit maps to |
179 | 179 |
algorithms. If a function type algorithm is called then the function |
180 | 180 |
type map adaptors can be used comfortable. For example let's see the |
181 | 181 |
usage of map adaptors with the \c graphToEps() function. |
182 | 182 |
\code |
183 | 183 |
Color nodeColor(int deg) { |
184 | 184 |
if (deg >= 2) { |
185 | 185 |
return Color(0.5, 0.0, 0.5); |
186 | 186 |
} else if (deg == 1) { |
187 | 187 |
return Color(1.0, 0.5, 1.0); |
188 | 188 |
} else { |
189 | 189 |
return Color(0.0, 0.0, 0.0); |
190 | 190 |
} |
191 | 191 |
} |
192 | 192 |
|
193 | 193 |
Digraph::NodeMap<int> degree_map(graph); |
194 | 194 |
|
195 | 195 |
graphToEps(graph, "graph.eps") |
196 | 196 |
.coords(coords).scaleToA4().undirected() |
197 | 197 |
.nodeColors(composeMap(functorToMap(nodeColor), degree_map)) |
198 | 198 |
.run(); |
199 | 199 |
\endcode |
200 | 200 |
The \c functorToMap() function makes an \c int to \c Color map from the |
201 | 201 |
\c nodeColor() function. The \c composeMap() compose the \c degree_map |
202 | 202 |
and the previously created map. The composed map is a proper function to |
203 | 203 |
get the color of each node. |
204 | 204 |
|
205 | 205 |
The usage with class type algorithms is little bit harder. In this |
206 | 206 |
case the function type map adaptors can not be used, because the |
207 | 207 |
function map adaptors give back temporary objects. |
208 | 208 |
\code |
209 | 209 |
Digraph graph; |
210 | 210 |
|
211 | 211 |
typedef Digraph::ArcMap<double> DoubleArcMap; |
212 | 212 |
DoubleArcMap length(graph); |
213 | 213 |
DoubleArcMap speed(graph); |
214 | 214 |
|
215 | 215 |
typedef DivMap<DoubleArcMap, DoubleArcMap> TimeMap; |
216 | 216 |
TimeMap time(length, speed); |
217 | 217 |
|
218 | 218 |
Dijkstra<Digraph, TimeMap> dijkstra(graph, time); |
219 | 219 |
dijkstra.run(source, target); |
220 | 220 |
\endcode |
221 | 221 |
We have a length map and a maximum speed map on the arcs of a digraph. |
222 | 222 |
The minimum time to pass the arc can be calculated as the division of |
223 | 223 |
the two maps which can be done implicitly with the \c DivMap template |
224 | 224 |
class. We use the implicit minimum time map as the length map of the |
225 | 225 |
\c Dijkstra algorithm. |
226 | 226 |
*/ |
227 | 227 |
|
228 | 228 |
/** |
229 | 229 |
@defgroup paths Path Structures |
230 | 230 |
@ingroup datas |
231 | 231 |
\brief %Path structures implemented in LEMON. |
232 | 232 |
|
233 | 233 |
This group contains the path structures implemented in LEMON. |
234 | 234 |
|
235 | 235 |
LEMON provides flexible data structures to work with paths. |
236 | 236 |
All of them have similar interfaces and they can be copied easily with |
237 | 237 |
assignment operators and copy constructors. This makes it easy and |
238 | 238 |
efficient to have e.g. the Dijkstra algorithm to store its result in |
239 | 239 |
any kind of path structure. |
240 | 240 |
|
241 | 241 |
\sa 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 | 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 | 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. |
374 | 374 |
|
375 | 375 |
This group contains the algorithms for calculating matchings in graphs. |
376 | 376 |
The general matching problem is finding a subset of the edges for which |
377 | 377 |
each node has at most one incident edge. |
378 | 378 |
|
379 | 379 |
There are several different algorithms for calculate matchings in |
380 | 380 |
graphs. The goal of the matching optimization |
381 | 381 |
can be finding maximum cardinality, maximum weight or minimum cost |
382 | 382 |
matching. The search can be constrained to find perfect or |
383 | 383 |
maximum cardinality matching. |
384 | 384 |
|
385 | 385 |
The matching algorithms implemented in LEMON: |
386 | 386 |
- \ref MaxMatching Edmond's blossom shrinking algorithm for calculating |
387 | 387 |
maximum cardinality matching in general graphs. |
388 | 388 |
- \ref MaxWeightedMatching Edmond's blossom shrinking algorithm for calculating |
389 | 389 |
maximum weighted matching in general graphs. |
390 | 390 |
- \ref MaxWeightedPerfectMatching |
391 | 391 |
Edmond's blossom shrinking algorithm for calculating maximum weighted |
392 | 392 |
perfect matching in general graphs. |
393 | 393 |
|
394 | 394 |
\image html bipartite_matching.png |
395 | 395 |
\image latex bipartite_matching.eps "Bipartite Matching" width=\textwidth |
396 | 396 |
*/ |
397 | 397 |
|
398 | 398 |
/** |
399 | 399 |
@defgroup spantree Minimum Spanning Tree Algorithms |
400 | 400 |
@ingroup algs |
401 | 401 |
\brief Algorithms for finding minimum cost spanning trees and arborescences. |
402 | 402 |
|
403 | 403 |
This group contains the algorithms for finding minimum cost spanning |
404 | 404 |
trees and arborescences. |
405 | 405 |
*/ |
406 | 406 |
|
407 | 407 |
/** |
408 | 408 |
@defgroup auxalg Auxiliary Algorithms |
409 | 409 |
@ingroup algs |
410 | 410 |
\brief Auxiliary algorithms implemented in LEMON. |
411 | 411 |
|
412 | 412 |
This group contains some algorithms implemented in LEMON |
413 | 413 |
in order to make it easier to implement complex algorithms. |
414 | 414 |
*/ |
415 | 415 |
|
416 | 416 |
/** |
417 | 417 |
@defgroup gen_opt_group General Optimization Tools |
418 | 418 |
\brief This group contains some general optimization frameworks |
419 | 419 |
implemented in LEMON. |
420 | 420 |
|
421 | 421 |
This group contains some general optimization frameworks |
422 | 422 |
implemented in LEMON. |
423 | 423 |
*/ |
424 | 424 |
|
425 | 425 |
/** |
426 | 426 |
@defgroup lp_group Lp and Mip Solvers |
427 | 427 |
@ingroup gen_opt_group |
428 | 428 |
\brief Lp and Mip solver interfaces for LEMON. |
429 | 429 |
|
430 | 430 |
This group contains Lp and Mip solver interfaces for LEMON. The |
431 | 431 |
various LP solvers could be used in the same manner with this |
432 | 432 |
interface. |
433 | 433 |
*/ |
434 | 434 |
|
435 | 435 |
/** |
436 | 436 |
@defgroup utils Tools and Utilities |
437 | 437 |
\brief Tools and utilities for programming in LEMON |
438 | 438 |
|
439 | 439 |
Tools and utilities for programming in LEMON. |
440 | 440 |
*/ |
441 | 441 |
|
442 | 442 |
/** |
443 | 443 |
@defgroup gutils Basic Graph Utilities |
444 | 444 |
@ingroup utils |
445 | 445 |
\brief Simple basic graph utilities. |
446 | 446 |
|
447 | 447 |
This group contains some simple basic graph utilities. |
448 | 448 |
*/ |
449 | 449 |
|
450 | 450 |
/** |
451 | 451 |
@defgroup misc Miscellaneous Tools |
452 | 452 |
@ingroup utils |
453 | 453 |
\brief Tools for development, debugging and testing. |
454 | 454 |
|
455 | 455 |
This group contains several useful tools for development, |
456 | 456 |
debugging and testing. |
457 | 457 |
*/ |
458 | 458 |
|
459 | 459 |
/** |
460 | 460 |
@defgroup timecount Time Measuring and Counting |
461 | 461 |
@ingroup misc |
462 | 462 |
\brief Simple tools for measuring the performance of algorithms. |
463 | 463 |
|
464 | 464 |
This group contains simple tools for measuring the performance |
465 | 465 |
of algorithms. |
466 | 466 |
*/ |
467 | 467 |
|
468 | 468 |
/** |
469 | 469 |
@defgroup exceptions Exceptions |
470 | 470 |
@ingroup utils |
471 | 471 |
\brief Exceptions defined in LEMON. |
472 | 472 |
|
473 | 473 |
This group contains the exceptions defined in LEMON. |
474 | 474 |
*/ |
475 | 475 |
|
476 | 476 |
/** |
477 | 477 |
@defgroup io_group Input-Output |
478 | 478 |
\brief Graph Input-Output methods |
479 | 479 |
|
480 | 480 |
This group contains the tools for importing and exporting graphs |
481 | 481 |
and graph related data. Now it supports the \ref lgf-format |
482 | 482 |
"LEMON Graph Format", the \c DIMACS format and the encapsulated |
483 | 483 |
postscript (EPS) format. |
484 | 484 |
*/ |
485 | 485 |
|
486 | 486 |
/** |
487 | 487 |
@defgroup lemon_io LEMON Graph Format |
488 | 488 |
@ingroup io_group |
489 | 489 |
\brief Reading and writing LEMON Graph Format. |
490 | 490 |
|
491 | 491 |
This group contains methods for reading and writing |
492 | 492 |
\ref lgf-format "LEMON Graph Format". |
493 | 493 |
*/ |
494 | 494 |
|
495 | 495 |
/** |
496 | 496 |
@defgroup eps_io Postscript Exporting |
497 | 497 |
@ingroup io_group |
498 | 498 |
\brief General \c EPS drawer and graph exporter |
499 | 499 |
|
500 | 500 |
This group contains general \c EPS drawing methods and special |
501 | 501 |
graph exporting tools. |
502 | 502 |
*/ |
503 | 503 |
|
504 | 504 |
/** |
505 | 505 |
@defgroup dimacs_group DIMACS format |
506 | 506 |
@ingroup io_group |
507 | 507 |
\brief Read and write files in DIMACS format |
508 | 508 |
|
509 | 509 |
Tools to read a digraph from or write it to a file in DIMACS format data. |
510 | 510 |
*/ |
511 | 511 |
|
512 | 512 |
/** |
513 | 513 |
@defgroup nauty_group NAUTY Format |
514 | 514 |
@ingroup io_group |
515 | 515 |
\brief Read \e Nauty format |
516 | 516 |
|
517 | 517 |
Tool to read graphs from \e Nauty format data. |
518 | 518 |
*/ |
519 | 519 |
|
520 | 520 |
/** |
521 | 521 |
@defgroup concept Concepts |
522 | 522 |
\brief Skeleton classes and concept checking classes |
523 | 523 |
|
524 | 524 |
This group contains the data/algorithm skeletons and concept checking |
525 | 525 |
classes implemented in LEMON. |
526 | 526 |
|
527 | 527 |
The purpose of the classes in this group is fourfold. |
528 | 528 |
|
529 | 529 |
- These classes contain the documentations of the %concepts. In order |
530 | 530 |
to avoid document multiplications, an implementation of a concept |
531 | 531 |
simply refers to the corresponding concept class. |
532 | 532 |
|
533 | 533 |
- These classes declare every functions, <tt>typedef</tt>s etc. an |
534 | 534 |
implementation of the %concepts should provide, however completely |
535 | 535 |
without implementations and real data structures behind the |
536 | 536 |
interface. On the other hand they should provide nothing else. All |
537 | 537 |
the algorithms working on a data structure meeting a certain concept |
538 | 538 |
should compile with these classes. (Though it will not run properly, |
539 | 539 |
of course.) In this way it is easily to check if an algorithm |
540 | 540 |
doesn't use any extra feature of a certain implementation. |
541 | 541 |
|
542 | 542 |
- The concept descriptor classes also provide a <em>checker class</em> |
543 | 543 |
that makes it possible to check whether a certain implementation of a |
544 | 544 |
concept indeed provides all the required features. |
545 | 545 |
|
546 | 546 |
- Finally, They can serve as a skeleton of a new implementation of a concept. |
547 | 547 |
*/ |
548 | 548 |
|
549 | 549 |
/** |
550 | 550 |
@defgroup graph_concepts Graph Structure Concepts |
551 | 551 |
@ingroup concept |
552 | 552 |
\brief Skeleton and concept checking classes for graph structures |
553 | 553 |
|
554 | 554 |
This group contains the skeletons and concept checking classes of LEMON's |
555 | 555 |
graph structures and helper classes used to implement these. |
556 | 556 |
*/ |
557 | 557 |
|
558 | 558 |
/** |
559 | 559 |
@defgroup map_concepts Map Concepts |
560 | 560 |
@ingroup concept |
561 | 561 |
\brief Skeleton and concept checking classes for maps |
562 | 562 |
|
563 | 563 |
This group contains the skeletons and concept checking classes of maps. |
564 | 564 |
*/ |
565 | 565 |
|
566 | 566 |
/** |
567 | 567 |
\anchor demoprograms |
568 | 568 |
|
569 | 569 |
@defgroup demos Demo Programs |
570 | 570 |
|
571 | 571 |
Some demo programs are listed here. Their full source codes can be found in |
572 | 572 |
the \c demo subdirectory of the source tree. |
573 | 573 |
|
574 | 574 |
In order to compile them, use the <tt>make demo</tt> or the |
575 | 575 |
<tt>make check</tt> commands. |
576 | 576 |
*/ |
577 | 577 |
|
578 | 578 |
/** |
579 | 579 |
@defgroup tools Standalone Utility Applications |
580 | 580 |
|
581 | 581 |
Some utility applications are listed here. |
582 | 582 |
|
583 | 583 |
The standard compilation procedure (<tt>./configure;make</tt>) will compile |
584 | 584 |
them, as well. |
585 | 585 |
*/ |
586 | 586 |
|
587 | 587 |
} |
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 |
70 | 70 |
and the target node of the arc, respectively, then come the map |
71 | 71 |
values. The source and target tokens must be node labels. |
72 | 72 |
|
73 | 73 |
\code |
74 | 74 |
@arcs |
75 | 75 |
capacity |
76 | 76 |
1 2 16 |
77 | 77 |
1 3 12 |
78 | 78 |
2 3 18 |
79 | 79 |
\endcode |
80 | 80 |
|
81 | 81 |
If there is no map in the \c \@arcs section at all, then it must be |
82 | 82 |
indicated by a sole '-' sign in the first line. |
83 | 83 |
|
84 | 84 |
\code |
85 | 85 |
@arcs |
86 | 86 |
- |
87 | 87 |
1 2 |
88 | 88 |
1 3 |
89 | 89 |
2 3 |
90 | 90 |
\endcode |
91 | 91 |
|
92 | 92 |
The \c \@edges is just a synonym of \c \@arcs. The \@arcs section can |
93 | 93 |
also store the edge set of an undirected graph. In such case there is |
94 | 94 |
a conventional method for store arc maps in the file, if two columns |
95 | 95 |
have the same caption with \c '+' and \c '-' prefix, then these columns |
96 | 96 |
can be regarded as the values of an arc map. |
97 | 97 |
|
98 | 98 |
The \c \@attributes section contains key-value pairs, each line |
99 | 99 |
consists of two tokens, an attribute name, and then an attribute |
100 | 100 |
value. The value of the attribute could be also a label value of a |
101 | 101 |
node or an edge, or even an edge label prefixed with \c '+' or \c '-', |
102 | 102 |
which regards to the forward or backward directed arc of the |
103 | 103 |
corresponding edge. |
104 | 104 |
|
105 | 105 |
\code |
106 | 106 |
@attributes |
107 | 107 |
source 1 |
108 | 108 |
target 3 |
109 | 109 |
caption "LEMON test digraph" |
110 | 110 |
\endcode |
111 | 111 |
|
112 | 112 |
The \e LGF can contain extra sections, but there is no restriction on |
113 | 113 |
the format of such sections. |
114 | 114 |
|
115 | 115 |
*/ |
116 | 116 |
} |
117 | 117 |
|
118 | 118 |
// LocalWords: whitespace whitespaces |
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 | 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); } |
70 | 70 |
void nextIn(Arc& i) const { _digraph->nextIn(i); } |
71 | 71 |
void nextOut(Arc& i) const { _digraph->nextOut(i); } |
72 | 72 |
|
73 | 73 |
Node source(const Arc& a) const { return _digraph->source(a); } |
74 | 74 |
Node target(const Arc& a) const { return _digraph->target(a); } |
75 | 75 |
|
76 | 76 |
typedef NodeNumTagIndicator<DGR> NodeNumTag; |
77 | 77 |
int nodeNum() const { return _digraph->nodeNum(); } |
78 | 78 |
|
79 | 79 |
typedef ArcNumTagIndicator<DGR> ArcNumTag; |
80 | 80 |
int arcNum() const { return _digraph->arcNum(); } |
81 | 81 |
|
82 | 82 |
typedef FindArcTagIndicator<DGR> FindArcTag; |
83 | 83 |
Arc findArc(const Node& u, const Node& v, const Arc& prev = INVALID) const { |
84 | 84 |
return _digraph->findArc(u, v, prev); |
85 | 85 |
} |
86 | 86 |
|
87 | 87 |
Node addNode() { return _digraph->addNode(); } |
88 | 88 |
Arc addArc(const Node& u, const Node& v) { return _digraph->addArc(u, v); } |
89 | 89 |
|
90 | 90 |
void erase(const Node& n) { _digraph->erase(n); } |
91 | 91 |
void erase(const Arc& a) { _digraph->erase(a); } |
92 | 92 |
|
93 | 93 |
void clear() { _digraph->clear(); } |
94 | 94 |
|
95 | 95 |
int id(const Node& n) const { return _digraph->id(n); } |
96 | 96 |
int id(const Arc& a) const { return _digraph->id(a); } |
97 | 97 |
|
98 | 98 |
Node nodeFromId(int ix) const { return _digraph->nodeFromId(ix); } |
99 | 99 |
Arc arcFromId(int ix) const { return _digraph->arcFromId(ix); } |
100 | 100 |
|
101 | 101 |
int maxNodeId() const { return _digraph->maxNodeId(); } |
102 | 102 |
int maxArcId() const { return _digraph->maxArcId(); } |
103 | 103 |
|
104 | 104 |
typedef typename ItemSetTraits<DGR, Node>::ItemNotifier NodeNotifier; |
105 | 105 |
NodeNotifier& notifier(Node) const { return _digraph->notifier(Node()); } |
106 | 106 |
|
107 | 107 |
typedef typename ItemSetTraits<DGR, Arc>::ItemNotifier ArcNotifier; |
108 | 108 |
ArcNotifier& notifier(Arc) const { return _digraph->notifier(Arc()); } |
109 | 109 |
|
110 | 110 |
template <typename V> |
111 | 111 |
class NodeMap : public DGR::template NodeMap<V> { |
112 | 112 |
typedef typename DGR::template NodeMap<V> Parent; |
113 | 113 |
|
114 | 114 |
public: |
115 | 115 |
explicit NodeMap(const Adaptor& adaptor) |
116 | 116 |
: Parent(*adaptor._digraph) {} |
117 | 117 |
NodeMap(const Adaptor& adaptor, const V& value) |
118 | 118 |
: Parent(*adaptor._digraph, value) { } |
119 | 119 |
|
120 | 120 |
private: |
121 | 121 |
NodeMap& operator=(const NodeMap& cmap) { |
122 | 122 |
return operator=<NodeMap>(cmap); |
123 | 123 |
} |
124 | 124 |
|
125 | 125 |
template <typename CMap> |
126 | 126 |
NodeMap& operator=(const CMap& cmap) { |
127 | 127 |
Parent::operator=(cmap); |
128 | 128 |
return *this; |
129 | 129 |
} |
130 | 130 |
|
131 | 131 |
}; |
132 | 132 |
|
133 | 133 |
template <typename V> |
134 | 134 |
class ArcMap : public DGR::template ArcMap<V> { |
135 | 135 |
typedef typename DGR::template ArcMap<V> Parent; |
136 | 136 |
|
137 | 137 |
public: |
138 | 138 |
explicit ArcMap(const DigraphAdaptorBase<DGR>& adaptor) |
139 | 139 |
: Parent(*adaptor._digraph) {} |
140 | 140 |
ArcMap(const DigraphAdaptorBase<DGR>& adaptor, const V& value) |
141 | 141 |
: Parent(*adaptor._digraph, value) {} |
142 | 142 |
|
143 | 143 |
private: |
144 | 144 |
ArcMap& operator=(const ArcMap& cmap) { |
145 | 145 |
return operator=<ArcMap>(cmap); |
146 | 146 |
} |
147 | 147 |
|
148 | 148 |
template <typename CMap> |
149 | 149 |
ArcMap& operator=(const CMap& cmap) { |
150 | 150 |
Parent::operator=(cmap); |
151 | 151 |
return *this; |
152 | 152 |
} |
153 | 153 |
|
154 | 154 |
}; |
155 | 155 |
|
156 | 156 |
}; |
157 | 157 |
|
158 | 158 |
template<typename GR> |
159 | 159 |
class GraphAdaptorBase { |
160 | 160 |
public: |
161 | 161 |
typedef GR Graph; |
162 | 162 |
|
163 | 163 |
protected: |
164 | 164 |
GR* _graph; |
165 | 165 |
|
166 | 166 |
GraphAdaptorBase() : _graph(0) {} |
167 | 167 |
|
168 | 168 |
void initialize(GR& graph) { _graph = &graph; } |
169 | 169 |
|
170 | 170 |
public: |
171 | 171 |
GraphAdaptorBase(GR& graph) : _graph(&graph) {} |
172 | 172 |
|
173 | 173 |
typedef typename GR::Node Node; |
174 | 174 |
typedef typename GR::Arc Arc; |
175 | 175 |
typedef typename GR::Edge Edge; |
176 | 176 |
|
177 | 177 |
void first(Node& i) const { _graph->first(i); } |
178 | 178 |
void first(Arc& i) const { _graph->first(i); } |
179 | 179 |
void first(Edge& i) const { _graph->first(i); } |
180 | 180 |
void firstIn(Arc& i, const Node& n) const { _graph->firstIn(i, n); } |
181 | 181 |
void firstOut(Arc& i, const Node& n ) const { _graph->firstOut(i, n); } |
182 | 182 |
void firstInc(Edge &i, bool &d, const Node &n) const { |
183 | 183 |
_graph->firstInc(i, d, n); |
184 | 184 |
} |
185 | 185 |
|
186 | 186 |
void next(Node& i) const { _graph->next(i); } |
187 | 187 |
void next(Arc& i) const { _graph->next(i); } |
188 | 188 |
void next(Edge& i) const { _graph->next(i); } |
189 | 189 |
void nextIn(Arc& i) const { _graph->nextIn(i); } |
190 | 190 |
void nextOut(Arc& i) const { _graph->nextOut(i); } |
191 | 191 |
void nextInc(Edge &i, bool &d) const { _graph->nextInc(i, d); } |
192 | 192 |
|
193 | 193 |
Node u(const Edge& e) const { return _graph->u(e); } |
194 | 194 |
Node v(const Edge& e) const { return _graph->v(e); } |
195 | 195 |
|
196 | 196 |
Node source(const Arc& a) const { return _graph->source(a); } |
197 | 197 |
Node target(const Arc& a) const { return _graph->target(a); } |
198 | 198 |
|
199 | 199 |
typedef NodeNumTagIndicator<Graph> NodeNumTag; |
200 | 200 |
int nodeNum() const { return _graph->nodeNum(); } |
201 | 201 |
|
202 | 202 |
typedef ArcNumTagIndicator<Graph> ArcNumTag; |
203 | 203 |
int arcNum() const { return _graph->arcNum(); } |
204 | 204 |
|
205 | 205 |
typedef EdgeNumTagIndicator<Graph> EdgeNumTag; |
206 | 206 |
int edgeNum() const { return _graph->edgeNum(); } |
207 | 207 |
|
208 | 208 |
typedef FindArcTagIndicator<Graph> FindArcTag; |
209 | 209 |
Arc findArc(const Node& u, const Node& v, |
210 | 210 |
const Arc& prev = INVALID) const { |
211 | 211 |
return _graph->findArc(u, v, prev); |
212 | 212 |
} |
213 | 213 |
|
214 | 214 |
typedef FindEdgeTagIndicator<Graph> FindEdgeTag; |
215 | 215 |
Edge findEdge(const Node& u, const Node& v, |
216 | 216 |
const Edge& prev = INVALID) const { |
217 | 217 |
return _graph->findEdge(u, v, prev); |
218 | 218 |
} |
219 | 219 |
|
220 | 220 |
Node addNode() { return _graph->addNode(); } |
221 | 221 |
Edge addEdge(const Node& u, const Node& v) { return _graph->addEdge(u, v); } |
222 | 222 |
|
223 | 223 |
void erase(const Node& i) { _graph->erase(i); } |
224 | 224 |
void erase(const Edge& i) { _graph->erase(i); } |
225 | 225 |
|
226 | 226 |
void clear() { _graph->clear(); } |
227 | 227 |
|
228 | 228 |
bool direction(const Arc& a) const { return _graph->direction(a); } |
229 | 229 |
Arc direct(const Edge& e, bool d) const { return _graph->direct(e, d); } |
230 | 230 |
|
231 | 231 |
int id(const Node& v) const { return _graph->id(v); } |
232 | 232 |
int id(const Arc& a) const { return _graph->id(a); } |
233 | 233 |
int id(const Edge& e) const { return _graph->id(e); } |
234 | 234 |
|
235 | 235 |
Node nodeFromId(int ix) const { return _graph->nodeFromId(ix); } |
236 | 236 |
Arc arcFromId(int ix) const { return _graph->arcFromId(ix); } |
237 | 237 |
Edge edgeFromId(int ix) const { return _graph->edgeFromId(ix); } |
238 | 238 |
|
239 | 239 |
int maxNodeId() const { return _graph->maxNodeId(); } |
240 | 240 |
int maxArcId() const { return _graph->maxArcId(); } |
241 | 241 |
int maxEdgeId() const { return _graph->maxEdgeId(); } |
242 | 242 |
|
243 | 243 |
typedef typename ItemSetTraits<GR, Node>::ItemNotifier NodeNotifier; |
244 | 244 |
NodeNotifier& notifier(Node) const { return _graph->notifier(Node()); } |
245 | 245 |
|
246 | 246 |
typedef typename ItemSetTraits<GR, Arc>::ItemNotifier ArcNotifier; |
247 | 247 |
ArcNotifier& notifier(Arc) const { return _graph->notifier(Arc()); } |
248 | 248 |
|
249 | 249 |
typedef typename ItemSetTraits<GR, Edge>::ItemNotifier EdgeNotifier; |
250 | 250 |
EdgeNotifier& notifier(Edge) const { return _graph->notifier(Edge()); } |
251 | 251 |
|
252 | 252 |
template <typename V> |
253 | 253 |
class NodeMap : public GR::template NodeMap<V> { |
254 | 254 |
typedef typename GR::template NodeMap<V> Parent; |
255 | 255 |
|
256 | 256 |
public: |
257 | 257 |
explicit NodeMap(const GraphAdaptorBase<GR>& adapter) |
258 | 258 |
: Parent(*adapter._graph) {} |
259 | 259 |
NodeMap(const GraphAdaptorBase<GR>& adapter, const V& value) |
260 | 260 |
: Parent(*adapter._graph, value) {} |
261 | 261 |
|
262 | 262 |
private: |
263 | 263 |
NodeMap& operator=(const NodeMap& cmap) { |
264 | 264 |
return operator=<NodeMap>(cmap); |
265 | 265 |
} |
266 | 266 |
|
267 | 267 |
template <typename CMap> |
268 | 268 |
NodeMap& operator=(const CMap& cmap) { |
269 | 269 |
Parent::operator=(cmap); |
270 | 270 |
return *this; |
271 | 271 |
} |
272 | 272 |
|
273 | 273 |
}; |
274 | 274 |
|
275 | 275 |
template <typename V> |
276 | 276 |
class ArcMap : public GR::template ArcMap<V> { |
277 | 277 |
typedef typename GR::template ArcMap<V> Parent; |
278 | 278 |
|
279 | 279 |
public: |
280 | 280 |
explicit ArcMap(const GraphAdaptorBase<GR>& adapter) |
281 | 281 |
: Parent(*adapter._graph) {} |
282 | 282 |
ArcMap(const GraphAdaptorBase<GR>& adapter, const V& value) |
283 | 283 |
: Parent(*adapter._graph, value) {} |
284 | 284 |
|
285 | 285 |
private: |
286 | 286 |
ArcMap& operator=(const ArcMap& cmap) { |
287 | 287 |
return operator=<ArcMap>(cmap); |
288 | 288 |
} |
289 | 289 |
|
290 | 290 |
template <typename CMap> |
291 | 291 |
ArcMap& operator=(const CMap& cmap) { |
292 | 292 |
Parent::operator=(cmap); |
293 | 293 |
return *this; |
294 | 294 |
} |
295 | 295 |
}; |
296 | 296 |
|
297 | 297 |
template <typename V> |
298 | 298 |
class EdgeMap : public GR::template EdgeMap<V> { |
299 | 299 |
typedef typename GR::template EdgeMap<V> Parent; |
300 | 300 |
|
301 | 301 |
public: |
302 | 302 |
explicit EdgeMap(const GraphAdaptorBase<GR>& adapter) |
303 | 303 |
: Parent(*adapter._graph) {} |
304 | 304 |
EdgeMap(const GraphAdaptorBase<GR>& adapter, const V& value) |
305 | 305 |
: Parent(*adapter._graph, value) {} |
306 | 306 |
|
307 | 307 |
private: |
308 | 308 |
EdgeMap& operator=(const EdgeMap& cmap) { |
309 | 309 |
return operator=<EdgeMap>(cmap); |
310 | 310 |
} |
311 | 311 |
|
312 | 312 |
template <typename CMap> |
313 | 313 |
EdgeMap& operator=(const CMap& cmap) { |
314 | 314 |
Parent::operator=(cmap); |
315 | 315 |
return *this; |
316 | 316 |
} |
317 | 317 |
}; |
318 | 318 |
|
319 | 319 |
}; |
320 | 320 |
|
321 | 321 |
template <typename DGR> |
322 | 322 |
class ReverseDigraphBase : public DigraphAdaptorBase<DGR> { |
323 | 323 |
typedef DigraphAdaptorBase<DGR> Parent; |
324 | 324 |
public: |
325 | 325 |
typedef DGR Digraph; |
326 | 326 |
protected: |
327 | 327 |
ReverseDigraphBase() : Parent() { } |
328 | 328 |
public: |
329 | 329 |
typedef typename Parent::Node Node; |
330 | 330 |
typedef typename Parent::Arc Arc; |
331 | 331 |
|
332 | 332 |
void firstIn(Arc& a, const Node& n) const { Parent::firstOut(a, n); } |
333 | 333 |
void firstOut(Arc& a, const Node& n ) const { Parent::firstIn(a, n); } |
334 | 334 |
|
335 | 335 |
void nextIn(Arc& a) const { Parent::nextOut(a); } |
336 | 336 |
void nextOut(Arc& a) const { Parent::nextIn(a); } |
337 | 337 |
|
338 | 338 |
Node source(const Arc& a) const { return Parent::target(a); } |
339 | 339 |
Node target(const Arc& a) const { return Parent::source(a); } |
340 | 340 |
|
341 | 341 |
Arc addArc(const Node& u, const Node& v) { return Parent::addArc(v, u); } |
342 | 342 |
|
343 | 343 |
typedef FindArcTagIndicator<DGR> FindArcTag; |
344 | 344 |
Arc findArc(const Node& u, const Node& v, |
345 | 345 |
const Arc& prev = INVALID) const { |
346 | 346 |
return Parent::findArc(v, u, prev); |
347 | 347 |
} |
348 | 348 |
|
349 | 349 |
}; |
350 | 350 |
|
351 | 351 |
/// \ingroup graph_adaptors |
352 | 352 |
/// |
353 | 353 |
/// \brief Adaptor class for reversing the orientation of the arcs in |
354 | 354 |
/// a digraph. |
355 | 355 |
/// |
356 | 356 |
/// ReverseDigraph can be used for reversing the arcs in a digraph. |
357 | 357 |
/// It conforms to the \ref concepts::Digraph "Digraph" concept. |
358 | 358 |
/// |
359 | 359 |
/// The adapted digraph can also be modified through this adaptor |
360 | 360 |
/// by adding or removing nodes or arcs, unless the \c GR template |
361 | 361 |
/// parameter is set to be \c const. |
362 | 362 |
/// |
363 | 363 |
/// \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 | 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 | 508 |
class NodeMap |
509 | 509 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, ch>, |
510 | 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 | 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 | 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 | 651 |
class NodeMap |
652 | 652 |
: public SubMapExtender<SubDigraphBase<DGR, NF, AF, false>, |
653 | 653 |
LEMON_SCOPE_FIX(DigraphAdaptorBase<DGR>, NodeMap<V>)> { |
654 | 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 | 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 |
743 | 743 |
template<typename DGR, |
744 | 744 |
typename NF = typename DGR::template NodeMap<bool>, |
745 | 745 |
typename AF = typename DGR::template ArcMap<bool> > |
746 | 746 |
class SubDigraph : |
747 | 747 |
public DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> > { |
748 | 748 |
#endif |
749 | 749 |
public: |
750 | 750 |
/// The type of the adapted digraph. |
751 | 751 |
typedef DGR Digraph; |
752 | 752 |
/// The type of the node filter map. |
753 | 753 |
typedef NF NodeFilterMap; |
754 | 754 |
/// The type of the arc filter map. |
755 | 755 |
typedef AF ArcFilterMap; |
756 | 756 |
|
757 | 757 |
typedef DigraphAdaptorExtender<SubDigraphBase<DGR, NF, AF, true> > |
758 | 758 |
Parent; |
759 | 759 |
|
760 | 760 |
typedef typename Parent::Node Node; |
761 | 761 |
typedef typename Parent::Arc Arc; |
762 | 762 |
|
763 | 763 |
protected: |
764 | 764 |
SubDigraph() { } |
765 | 765 |
public: |
766 | 766 |
|
767 | 767 |
/// \brief Constructor |
768 | 768 |
/// |
769 | 769 |
/// Creates a subdigraph for the given digraph with the |
770 | 770 |
/// given node and arc filter maps. |
771 | 771 |
SubDigraph(DGR& digraph, NF& node_filter, AF& arc_filter) { |
772 | 772 |
Parent::initialize(digraph, node_filter, arc_filter); |
773 | 773 |
} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-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", |
70 | 70 |
/// "pre heap" or "post heap". The latter two are indifferent from the |
71 | 71 |
/// heap's point of view, but may be useful to the user. |
72 | 72 |
/// |
73 | 73 |
/// The item-int map must be initialized in such way that it assigns |
74 | 74 |
/// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap. |
75 | 75 |
enum State { |
76 | 76 |
IN_HEAP = 0, ///< = 0. |
77 | 77 |
PRE_HEAP = -1, ///< = -1. |
78 | 78 |
POST_HEAP = -2 ///< = -2. |
79 | 79 |
}; |
80 | 80 |
|
81 | 81 |
private: |
82 | 82 |
std::vector<Pair> _data; |
83 | 83 |
Compare _comp; |
84 | 84 |
ItemIntMap &_iim; |
85 | 85 |
|
86 | 86 |
public: |
87 | 87 |
/// \brief The constructor. |
88 | 88 |
/// |
89 | 89 |
/// The constructor. |
90 | 90 |
/// \param map should be given to the constructor, since it is used |
91 | 91 |
/// internally to handle the cross references. The value of the map |
92 | 92 |
/// must be \c PRE_HEAP (<tt>-1</tt>) for every item. |
93 | 93 |
explicit BinHeap(ItemIntMap &map) : _iim(map) {} |
94 | 94 |
|
95 | 95 |
/// \brief The constructor. |
96 | 96 |
/// |
97 | 97 |
/// The constructor. |
98 | 98 |
/// \param map should be given to the constructor, since it is used |
99 | 99 |
/// internally to handle the cross references. The value of the map |
100 | 100 |
/// should be PRE_HEAP (-1) for each element. |
101 | 101 |
/// |
102 | 102 |
/// \param comp The comparator function object. |
103 | 103 |
BinHeap(ItemIntMap &map, const Compare &comp) |
104 | 104 |
: _iim(map), _comp(comp) {} |
105 | 105 |
|
106 | 106 |
|
107 | 107 |
/// The number of items stored in the heap. |
108 | 108 |
/// |
109 | 109 |
/// \brief Returns the number of items stored in the heap. |
110 | 110 |
int size() const { return _data.size(); } |
111 | 111 |
|
112 | 112 |
/// \brief Checks if the heap stores no items. |
113 | 113 |
/// |
114 | 114 |
/// Returns \c true if and only if the heap stores no items. |
115 | 115 |
bool empty() const { return _data.empty(); } |
116 | 116 |
|
117 | 117 |
/// \brief Make empty this heap. |
118 | 118 |
/// |
119 | 119 |
/// Make empty this heap. It does not change the cross reference map. |
120 | 120 |
/// If you want to reuse what is not surely empty you should first clear |
121 | 121 |
/// the heap and after that you should set the cross reference map for |
122 | 122 |
/// each item to \c PRE_HEAP. |
123 | 123 |
void clear() { |
124 | 124 |
_data.clear(); |
125 | 125 |
} |
126 | 126 |
|
127 | 127 |
private: |
128 | 128 |
static int parent(int i) { return (i-1)/2; } |
129 | 129 |
|
130 | 130 |
static int second_child(int i) { return 2*i+2; } |
131 | 131 |
bool less(const Pair &p1, const Pair &p2) const { |
132 | 132 |
return _comp(p1.second, p2.second); |
133 | 133 |
} |
134 | 134 |
|
135 | 135 |
int bubble_up(int hole, Pair p) { |
136 | 136 |
int par = parent(hole); |
137 | 137 |
while( hole>0 && less(p,_data[par]) ) { |
138 | 138 |
move(_data[par],hole); |
139 | 139 |
hole = par; |
140 | 140 |
par = parent(hole); |
141 | 141 |
} |
142 | 142 |
move(p, hole); |
143 | 143 |
return hole; |
144 | 144 |
} |
145 | 145 |
|
146 | 146 |
int bubble_down(int hole, Pair p, int length) { |
147 | 147 |
int child = second_child(hole); |
148 | 148 |
while(child < length) { |
149 | 149 |
if( less(_data[child-1], _data[child]) ) { |
150 | 150 |
--child; |
151 | 151 |
} |
152 | 152 |
if( !less(_data[child], p) ) |
153 | 153 |
goto ok; |
154 | 154 |
move(_data[child], hole); |
155 | 155 |
hole = child; |
156 | 156 |
child = second_child(hole); |
157 | 157 |
} |
158 | 158 |
child--; |
159 | 159 |
if( child<length && less(_data[child], p) ) { |
160 | 160 |
move(_data[child], hole); |
161 | 161 |
hole=child; |
162 | 162 |
} |
163 | 163 |
ok: |
164 | 164 |
move(p, hole); |
165 | 165 |
return hole; |
166 | 166 |
} |
167 | 167 |
|
168 | 168 |
void move(const Pair &p, int i) { |
169 | 169 |
_data[i] = p; |
170 | 170 |
_iim.set(p.first, i); |
171 | 171 |
} |
172 | 172 |
|
173 | 173 |
public: |
174 | 174 |
/// \brief Insert a pair of item and priority into the heap. |
175 | 175 |
/// |
176 | 176 |
/// Adds \c p.first to the heap with priority \c p.second. |
177 | 177 |
/// \param p The pair to insert. |
178 | 178 |
void push(const Pair &p) { |
179 | 179 |
int n = _data.size(); |
180 | 180 |
_data.resize(n+1); |
181 | 181 |
bubble_up(n, p); |
182 | 182 |
} |
183 | 183 |
|
184 | 184 |
/// \brief Insert an item into the heap with the given heap. |
185 | 185 |
/// |
186 | 186 |
/// Adds \c i to the heap with priority \c p. |
187 | 187 |
/// \param i The item to insert. |
188 | 188 |
/// \param p The priority of the item. |
189 | 189 |
void push(const Item &i, const Prio &p) { push(Pair(i,p)); } |
190 | 190 |
|
191 | 191 |
/// \brief Returns the item with minimum priority relative to \c Compare. |
192 | 192 |
/// |
193 | 193 |
/// This method returns the item with minimum priority relative to \c |
194 | 194 |
/// Compare. |
195 | 195 |
/// \pre The heap must be nonempty. |
196 | 196 |
Item top() const { |
197 | 197 |
return _data[0].first; |
198 | 198 |
} |
199 | 199 |
|
200 | 200 |
/// \brief Returns the minimum priority relative to \c Compare. |
201 | 201 |
/// |
202 | 202 |
/// It returns the minimum priority relative to \c Compare. |
203 | 203 |
/// \pre The heap must be nonempty. |
204 | 204 |
Prio prio() const { |
205 | 205 |
return _data[0].second; |
206 | 206 |
} |
207 | 207 |
|
208 | 208 |
/// \brief Deletes the item with minimum priority relative to \c Compare. |
209 | 209 |
/// |
210 | 210 |
/// This method deletes the item with minimum priority relative to \c |
211 | 211 |
/// Compare from the heap. |
212 | 212 |
/// \pre The heap must be non-empty. |
213 | 213 |
void pop() { |
214 | 214 |
int n = _data.size()-1; |
215 | 215 |
_iim.set(_data[0].first, POST_HEAP); |
216 | 216 |
if (n > 0) { |
217 | 217 |
bubble_down(0, _data[n], n); |
218 | 218 |
} |
219 | 219 |
_data.pop_back(); |
220 | 220 |
} |
221 | 221 |
|
222 | 222 |
/// \brief Deletes \c i from the heap. |
223 | 223 |
/// |
224 | 224 |
/// This method deletes item \c i from the heap. |
225 | 225 |
/// \param i The item to erase. |
226 | 226 |
/// \pre The item should be in the heap. |
227 | 227 |
void erase(const Item &i) { |
228 | 228 |
int h = _iim[i]; |
229 | 229 |
int n = _data.size()-1; |
230 | 230 |
_iim.set(_data[h].first, POST_HEAP); |
231 | 231 |
if( h < n ) { |
232 | 232 |
if ( bubble_up(h, _data[n]) == h) { |
233 | 233 |
bubble_down(h, _data[n], n); |
234 | 234 |
} |
235 | 235 |
} |
236 | 236 |
_data.pop_back(); |
237 | 237 |
} |
238 | 238 |
|
239 | 239 |
|
240 | 240 |
/// \brief Returns the priority of \c i. |
241 | 241 |
/// |
242 | 242 |
/// This function returns the priority of item \c i. |
243 | 243 |
/// \param i The item. |
244 | 244 |
/// \pre \c i must be in the heap. |
245 | 245 |
Prio operator[](const Item &i) const { |
246 | 246 |
int idx = _iim[i]; |
247 | 247 |
return _data[idx].second; |
248 | 248 |
} |
249 | 249 |
|
250 | 250 |
/// \brief \c i gets to the heap with priority \c p independently |
251 | 251 |
/// if \c i was already there. |
252 | 252 |
/// |
253 | 253 |
/// This method calls \ref push(\c i, \c p) if \c i is not stored |
254 | 254 |
/// in the heap and sets the priority of \c i to \c p otherwise. |
255 | 255 |
/// \param i The item. |
256 | 256 |
/// \param p The priority. |
257 | 257 |
void set(const Item &i, const Prio &p) { |
258 | 258 |
int idx = _iim[i]; |
259 | 259 |
if( idx < 0 ) { |
260 | 260 |
push(i,p); |
261 | 261 |
} |
262 | 262 |
else if( _comp(p, _data[idx].second) ) { |
263 | 263 |
bubble_up(idx, Pair(i,p)); |
264 | 264 |
} |
265 | 265 |
else { |
266 | 266 |
bubble_down(idx, Pair(i,p), _data.size()); |
267 | 267 |
} |
268 | 268 |
} |
269 | 269 |
|
270 | 270 |
/// \brief Decreases the priority of \c i to \c p. |
271 | 271 |
/// |
272 | 272 |
/// This method decreases the priority of item \c i to \c p. |
273 | 273 |
/// \param i The item. |
274 | 274 |
/// \param p The priority. |
275 | 275 |
/// \pre \c i must be stored in the heap with priority at least \c |
276 | 276 |
/// p relative to \c Compare. |
277 | 277 |
void decrease(const Item &i, const Prio &p) { |
278 | 278 |
int idx = _iim[i]; |
279 | 279 |
bubble_up(idx, Pair(i,p)); |
280 | 280 |
} |
281 | 281 |
|
282 | 282 |
/// \brief Increases the priority of \c i to \c p. |
283 | 283 |
/// |
284 | 284 |
/// This method sets the priority of item \c i to \c p. |
285 | 285 |
/// \param i The item. |
286 | 286 |
/// \param p The priority. |
287 | 287 |
/// \pre \c i must be stored in the heap with priority at most \c |
288 | 288 |
/// p relative to \c Compare. |
289 | 289 |
void increase(const Item &i, const Prio &p) { |
290 | 290 |
int idx = _iim[i]; |
291 | 291 |
bubble_down(idx, Pair(i,p), _data.size()); |
292 | 292 |
} |
293 | 293 |
|
294 | 294 |
/// \brief Returns if \c item is in, has already been in, or has |
295 | 295 |
/// never been in the heap. |
296 | 296 |
/// |
297 | 297 |
/// This method returns PRE_HEAP if \c item has never been in the |
298 | 298 |
/// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP |
299 | 299 |
/// otherwise. In the latter case it is possible that \c item will |
300 | 300 |
/// get back to the heap again. |
301 | 301 |
/// \param i The item. |
302 | 302 |
State state(const Item &i) const { |
303 | 303 |
int s = _iim[i]; |
304 | 304 |
if( s>=0 ) |
305 | 305 |
s=0; |
306 | 306 |
return State(s); |
307 | 307 |
} |
308 | 308 |
|
309 | 309 |
/// \brief Sets the state of the \c item in the heap. |
310 | 310 |
/// |
311 | 311 |
/// Sets the state of the \c item in the heap. It can be used to |
312 | 312 |
/// manually clear the heap when it is important to achive the |
313 | 313 |
/// better time complexity. |
314 | 314 |
/// \param i The item. |
315 | 315 |
/// \param st The state. It should not be \c IN_HEAP. |
316 | 316 |
void state(const Item& i, State st) { |
317 | 317 |
switch (st) { |
318 | 318 |
case POST_HEAP: |
319 | 319 |
case PRE_HEAP: |
320 | 320 |
if (state(i) == IN_HEAP) { |
321 | 321 |
erase(i); |
322 | 322 |
} |
323 | 323 |
_iim[i] = st; |
324 | 324 |
break; |
325 | 325 |
case IN_HEAP: |
326 | 326 |
break; |
327 | 327 |
} |
328 | 328 |
} |
329 | 329 |
|
330 | 330 |
/// \brief Replaces an item in the heap. |
331 | 331 |
/// |
332 | 332 |
/// The \c i item is replaced with \c j item. The \c i item should |
333 | 333 |
/// be in the heap, while the \c j should be out of the heap. The |
334 | 334 |
/// \c i item will out of the heap and \c j will be in the heap |
335 | 335 |
/// with the same prioriority as prevoiusly the \c i item. |
336 | 336 |
void replace(const Item& i, const Item& j) { |
337 | 337 |
int idx = _iim[i]; |
338 | 338 |
_iim.set(i, _iim[j]); |
339 | 339 |
_iim.set(j, idx); |
340 | 340 |
_data[idx].first = j; |
341 | 341 |
} |
342 | 342 |
|
343 | 343 |
}; // class BinHeap |
344 | 344 |
|
345 | 345 |
} // namespace lemon |
346 | 346 |
|
347 | 347 |
#endif // LEMON_BIN_HEAP_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_BITS_ARRAY_MAP_H |
20 | 20 |
#define LEMON_BITS_ARRAY_MAP_H |
21 | 21 |
|
22 | 22 |
#include <memory> |
23 | 23 |
|
24 | 24 |
#include <lemon/bits/traits.h> |
25 | 25 |
#include <lemon/bits/alteration_notifier.h> |
26 | 26 |
#include <lemon/concept_check.h> |
27 | 27 |
#include <lemon/concepts/maps.h> |
28 | 28 |
|
29 | 29 |
// \ingroup graphbits |
30 | 30 |
// \file |
31 | 31 |
// \brief Graph map based on the array storage. |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
// \ingroup graphbits |
36 | 36 |
// |
37 | 37 |
// \brief Graph map based on the array storage. |
38 | 38 |
// |
39 | 39 |
// The ArrayMap template class is graph map structure that automatically |
40 | 40 |
// updates the map when a key is added to or erased from the graph. |
41 | 41 |
// This map uses the allocators to implement the container functionality. |
42 | 42 |
// |
43 | 43 |
// The template parameters are the Graph, the current Item type and |
44 | 44 |
// the Value type of the map. |
45 | 45 |
template <typename _Graph, typename _Item, typename _Value> |
46 | 46 |
class ArrayMap |
47 | 47 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { |
48 | 48 |
public: |
49 | 49 |
// The graph type. |
50 | 50 |
typedef _Graph GraphType; |
51 | 51 |
// The item type. |
52 | 52 |
typedef _Item Item; |
53 | 53 |
// The reference map tag. |
54 | 54 |
typedef True ReferenceMapTag; |
55 | 55 |
|
56 | 56 |
// The key type of the map. |
57 | 57 |
typedef _Item Key; |
58 | 58 |
// The value type of the map. |
59 | 59 |
typedef _Value Value; |
60 | 60 |
|
61 | 61 |
// The const reference type of the map. |
62 | 62 |
typedef const _Value& ConstReference; |
63 | 63 |
// The reference type of the map. |
64 | 64 |
typedef _Value& Reference; |
65 | 65 |
|
66 | 66 |
// The map type. |
67 | 67 |
typedef ArrayMap Map; |
68 | 68 |
|
69 | 69 |
// The notifier type. |
70 | 70 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
71 | 71 |
|
72 | 72 |
private: |
73 | 73 |
|
74 | 74 |
// The MapBase of the Map which imlements the core regisitry function. |
75 | 75 |
typedef typename Notifier::ObserverBase Parent; |
76 | 76 |
|
77 | 77 |
typedef std::allocator<Value> Allocator; |
78 | 78 |
|
79 | 79 |
public: |
80 | 80 |
|
81 | 81 |
// \brief Graph initialized map constructor. |
82 | 82 |
// |
83 | 83 |
// Graph initialized map constructor. |
84 | 84 |
explicit ArrayMap(const GraphType& graph) { |
85 | 85 |
Parent::attach(graph.notifier(Item())); |
86 | 86 |
allocate_memory(); |
87 | 87 |
Notifier* nf = Parent::notifier(); |
88 | 88 |
Item it; |
89 | 89 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
90 | 90 |
int id = nf->id(it);; |
91 | 91 |
allocator.construct(&(values[id]), Value()); |
92 | 92 |
} |
93 | 93 |
} |
94 | 94 |
|
95 | 95 |
// \brief Constructor to use default value to initialize the map. |
96 | 96 |
// |
97 | 97 |
// It constructs a map and initialize all of the the map. |
98 | 98 |
ArrayMap(const GraphType& graph, const Value& value) { |
99 | 99 |
Parent::attach(graph.notifier(Item())); |
100 | 100 |
allocate_memory(); |
101 | 101 |
Notifier* nf = Parent::notifier(); |
102 | 102 |
Item it; |
103 | 103 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
104 | 104 |
int id = nf->id(it);; |
105 | 105 |
allocator.construct(&(values[id]), value); |
106 | 106 |
} |
107 | 107 |
} |
108 | 108 |
|
109 | 109 |
private: |
110 | 110 |
// \brief Constructor to copy a map of the same map type. |
111 | 111 |
// |
112 | 112 |
// Constructor to copy a map of the same map type. |
113 | 113 |
ArrayMap(const ArrayMap& copy) : Parent() { |
114 | 114 |
if (copy.attached()) { |
115 | 115 |
attach(*copy.notifier()); |
116 | 116 |
} |
117 | 117 |
capacity = copy.capacity; |
118 | 118 |
if (capacity == 0) return; |
119 | 119 |
values = allocator.allocate(capacity); |
120 | 120 |
Notifier* nf = Parent::notifier(); |
121 | 121 |
Item it; |
122 | 122 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
123 | 123 |
int id = nf->id(it);; |
124 | 124 |
allocator.construct(&(values[id]), copy.values[id]); |
125 | 125 |
} |
126 | 126 |
} |
127 | 127 |
|
128 | 128 |
// \brief Assign operator. |
129 | 129 |
// |
130 | 130 |
// This operator assigns for each item in the map the |
131 | 131 |
// value mapped to the same item in the copied map. |
132 | 132 |
// The parameter map should be indiced with the same |
133 | 133 |
// itemset because this assign operator does not change |
134 | 134 |
// the container of the map. |
135 | 135 |
ArrayMap& operator=(const ArrayMap& cmap) { |
136 | 136 |
return operator=<ArrayMap>(cmap); |
137 | 137 |
} |
138 | 138 |
|
139 | 139 |
|
140 | 140 |
// \brief Template assign operator. |
141 | 141 |
// |
142 | 142 |
// The given parameter should conform to the ReadMap |
143 | 143 |
// concecpt and could be indiced by the current item set of |
144 | 144 |
// the NodeMap. In this case the value for each item |
145 | 145 |
// is assigned by the value of the given ReadMap. |
146 | 146 |
template <typename CMap> |
147 | 147 |
ArrayMap& operator=(const CMap& cmap) { |
148 | 148 |
checkConcept<concepts::ReadMap<Key, _Value>, CMap>(); |
149 | 149 |
const typename Parent::Notifier* nf = Parent::notifier(); |
150 | 150 |
Item it; |
151 | 151 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
152 | 152 |
set(it, cmap[it]); |
153 | 153 |
} |
154 | 154 |
return *this; |
155 | 155 |
} |
156 | 156 |
|
157 | 157 |
public: |
158 | 158 |
// \brief The destructor of the map. |
159 | 159 |
// |
160 | 160 |
// The destructor of the map. |
161 | 161 |
virtual ~ArrayMap() { |
162 | 162 |
if (attached()) { |
163 | 163 |
clear(); |
164 | 164 |
detach(); |
165 | 165 |
} |
166 | 166 |
} |
167 | 167 |
|
168 | 168 |
protected: |
169 | 169 |
|
170 | 170 |
using Parent::attach; |
171 | 171 |
using Parent::detach; |
172 | 172 |
using Parent::attached; |
173 | 173 |
|
174 | 174 |
public: |
175 | 175 |
|
176 | 176 |
// \brief The subscript operator. |
177 | 177 |
// |
178 | 178 |
// The subscript operator. The map can be subscripted by the |
179 | 179 |
// actual keys of the graph. |
180 | 180 |
Value& operator[](const Key& key) { |
181 | 181 |
int id = Parent::notifier()->id(key); |
182 | 182 |
return values[id]; |
183 | 183 |
} |
184 | 184 |
|
185 | 185 |
// \brief The const subscript operator. |
186 | 186 |
// |
187 | 187 |
// The const subscript operator. The map can be subscripted by the |
188 | 188 |
// actual keys of the graph. |
189 | 189 |
const Value& operator[](const Key& key) const { |
190 | 190 |
int id = Parent::notifier()->id(key); |
191 | 191 |
return values[id]; |
192 | 192 |
} |
193 | 193 |
|
194 | 194 |
// \brief Setter function of the map. |
195 | 195 |
// |
196 | 196 |
// Setter function of the map. Equivalent with map[key] = val. |
197 | 197 |
// This is a compatibility feature with the not dereferable maps. |
198 | 198 |
void set(const Key& key, const Value& val) { |
199 | 199 |
(*this)[key] = val; |
200 | 200 |
} |
201 | 201 |
|
202 | 202 |
protected: |
203 | 203 |
|
204 | 204 |
// \brief Adds a new key to the map. |
205 | 205 |
// |
206 | 206 |
// It adds a new key to the map. It is called by the observer notifier |
207 | 207 |
// and it overrides the add() member function of the observer base. |
208 | 208 |
virtual void add(const Key& key) { |
209 | 209 |
Notifier* nf = Parent::notifier(); |
210 | 210 |
int id = nf->id(key); |
211 | 211 |
if (id >= capacity) { |
212 | 212 |
int new_capacity = (capacity == 0 ? 1 : capacity); |
213 | 213 |
while (new_capacity <= id) { |
214 | 214 |
new_capacity <<= 1; |
215 | 215 |
} |
216 | 216 |
Value* new_values = allocator.allocate(new_capacity); |
217 | 217 |
Item it; |
218 | 218 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
219 | 219 |
int jd = nf->id(it);; |
220 | 220 |
if (id != jd) { |
221 | 221 |
allocator.construct(&(new_values[jd]), values[jd]); |
222 | 222 |
allocator.destroy(&(values[jd])); |
223 | 223 |
} |
224 | 224 |
} |
225 | 225 |
if (capacity != 0) allocator.deallocate(values, capacity); |
226 | 226 |
values = new_values; |
227 | 227 |
capacity = new_capacity; |
228 | 228 |
} |
229 | 229 |
allocator.construct(&(values[id]), Value()); |
230 | 230 |
} |
231 | 231 |
|
232 | 232 |
// \brief Adds more new keys to the map. |
233 | 233 |
// |
234 | 234 |
// It adds more new keys to the map. It is called by the observer notifier |
235 | 235 |
// and it overrides the add() member function of the observer base. |
236 | 236 |
virtual void add(const std::vector<Key>& keys) { |
237 | 237 |
Notifier* nf = Parent::notifier(); |
238 | 238 |
int max_id = -1; |
239 | 239 |
for (int i = 0; i < int(keys.size()); ++i) { |
240 | 240 |
int id = nf->id(keys[i]); |
241 | 241 |
if (id > max_id) { |
242 | 242 |
max_id = id; |
243 | 243 |
} |
244 | 244 |
} |
245 | 245 |
if (max_id >= capacity) { |
246 | 246 |
int new_capacity = (capacity == 0 ? 1 : capacity); |
247 | 247 |
while (new_capacity <= max_id) { |
248 | 248 |
new_capacity <<= 1; |
249 | 249 |
} |
250 | 250 |
Value* new_values = allocator.allocate(new_capacity); |
251 | 251 |
Item it; |
252 | 252 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
253 | 253 |
int id = nf->id(it); |
254 | 254 |
bool found = false; |
255 | 255 |
for (int i = 0; i < int(keys.size()); ++i) { |
256 | 256 |
int jd = nf->id(keys[i]); |
257 | 257 |
if (id == jd) { |
258 | 258 |
found = true; |
259 | 259 |
break; |
260 | 260 |
} |
261 | 261 |
} |
262 | 262 |
if (found) continue; |
263 | 263 |
allocator.construct(&(new_values[id]), values[id]); |
264 | 264 |
allocator.destroy(&(values[id])); |
265 | 265 |
} |
266 | 266 |
if (capacity != 0) allocator.deallocate(values, capacity); |
267 | 267 |
values = new_values; |
268 | 268 |
capacity = new_capacity; |
269 | 269 |
} |
270 | 270 |
for (int i = 0; i < int(keys.size()); ++i) { |
271 | 271 |
int id = nf->id(keys[i]); |
272 | 272 |
allocator.construct(&(values[id]), Value()); |
273 | 273 |
} |
274 | 274 |
} |
275 | 275 |
|
276 | 276 |
// \brief Erase a key from the map. |
277 | 277 |
// |
278 | 278 |
// Erase a key from the map. It is called by the observer notifier |
279 | 279 |
// and it overrides the erase() member function of the observer base. |
280 | 280 |
virtual void erase(const Key& key) { |
281 | 281 |
int id = Parent::notifier()->id(key); |
282 | 282 |
allocator.destroy(&(values[id])); |
283 | 283 |
} |
284 | 284 |
|
285 | 285 |
// \brief Erase more keys from the map. |
286 | 286 |
// |
287 | 287 |
// Erase more keys from the map. It is called by the observer notifier |
288 | 288 |
// and it overrides the erase() member function of the observer base. |
289 | 289 |
virtual void erase(const std::vector<Key>& keys) { |
290 | 290 |
for (int i = 0; i < int(keys.size()); ++i) { |
291 | 291 |
int id = Parent::notifier()->id(keys[i]); |
292 | 292 |
allocator.destroy(&(values[id])); |
293 | 293 |
} |
294 | 294 |
} |
295 | 295 |
|
296 | 296 |
// \brief Builds the map. |
297 | 297 |
// |
298 | 298 |
// It builds the map. It is called by the observer notifier |
299 | 299 |
// and it overrides the build() member function of the observer base. |
300 | 300 |
virtual void build() { |
301 | 301 |
Notifier* nf = Parent::notifier(); |
302 | 302 |
allocate_memory(); |
303 | 303 |
Item it; |
304 | 304 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
305 | 305 |
int id = nf->id(it);; |
306 | 306 |
allocator.construct(&(values[id]), Value()); |
307 | 307 |
} |
308 | 308 |
} |
309 | 309 |
|
310 | 310 |
// \brief Clear the map. |
311 | 311 |
// |
312 | 312 |
// It erase all items from the map. It is called by the observer notifier |
313 | 313 |
// and it overrides the clear() member function of the observer base. |
314 | 314 |
virtual void clear() { |
315 | 315 |
Notifier* nf = Parent::notifier(); |
316 | 316 |
if (capacity != 0) { |
317 | 317 |
Item it; |
318 | 318 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
319 | 319 |
int id = nf->id(it); |
320 | 320 |
allocator.destroy(&(values[id])); |
321 | 321 |
} |
322 | 322 |
allocator.deallocate(values, capacity); |
323 | 323 |
capacity = 0; |
324 | 324 |
} |
325 | 325 |
} |
326 | 326 |
|
327 | 327 |
private: |
328 | 328 |
|
329 | 329 |
void allocate_memory() { |
330 | 330 |
int max_id = Parent::notifier()->maxId(); |
331 | 331 |
if (max_id == -1) { |
332 | 332 |
capacity = 0; |
333 | 333 |
values = 0; |
334 | 334 |
return; |
335 | 335 |
} |
336 | 336 |
capacity = 1; |
337 | 337 |
while (capacity <= max_id) { |
338 | 338 |
capacity <<= 1; |
339 | 339 |
} |
340 | 340 |
values = allocator.allocate(capacity); |
341 | 341 |
} |
342 | 342 |
|
343 | 343 |
int capacity; |
344 | 344 |
Value* values; |
345 | 345 |
Allocator allocator; |
346 | 346 |
|
347 | 347 |
}; |
348 | 348 |
|
349 | 349 |
} |
350 | 350 |
|
351 | 351 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-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 |
|
70 | 70 |
template <typename _Graph, typename _Item> |
71 | 71 |
struct DefaultMapSelector<_Graph, _Item, unsigned int> { |
72 | 72 |
typedef VectorMap<_Graph, _Item, unsigned int> Map; |
73 | 73 |
}; |
74 | 74 |
|
75 | 75 |
|
76 | 76 |
// short |
77 | 77 |
template <typename _Graph, typename _Item> |
78 | 78 |
struct DefaultMapSelector<_Graph, _Item, signed short> { |
79 | 79 |
typedef VectorMap<_Graph, _Item, signed short> Map; |
80 | 80 |
}; |
81 | 81 |
|
82 | 82 |
template <typename _Graph, typename _Item> |
83 | 83 |
struct DefaultMapSelector<_Graph, _Item, unsigned short> { |
84 | 84 |
typedef VectorMap<_Graph, _Item, unsigned short> Map; |
85 | 85 |
}; |
86 | 86 |
|
87 | 87 |
|
88 | 88 |
// long |
89 | 89 |
template <typename _Graph, typename _Item> |
90 | 90 |
struct DefaultMapSelector<_Graph, _Item, signed long> { |
91 | 91 |
typedef VectorMap<_Graph, _Item, signed long> Map; |
92 | 92 |
}; |
93 | 93 |
|
94 | 94 |
template <typename _Graph, typename _Item> |
95 | 95 |
struct DefaultMapSelector<_Graph, _Item, unsigned long> { |
96 | 96 |
typedef VectorMap<_Graph, _Item, unsigned long> Map; |
97 | 97 |
}; |
98 | 98 |
|
99 | 99 |
|
100 | 100 |
#if defined LEMON_HAVE_LONG_LONG |
101 | 101 |
|
102 | 102 |
// long long |
103 | 103 |
template <typename _Graph, typename _Item> |
104 | 104 |
struct DefaultMapSelector<_Graph, _Item, signed long long> { |
105 | 105 |
typedef VectorMap<_Graph, _Item, signed long long> Map; |
106 | 106 |
}; |
107 | 107 |
|
108 | 108 |
template <typename _Graph, typename _Item> |
109 | 109 |
struct DefaultMapSelector<_Graph, _Item, unsigned long long> { |
110 | 110 |
typedef VectorMap<_Graph, _Item, unsigned long long> Map; |
111 | 111 |
}; |
112 | 112 |
|
113 | 113 |
#endif |
114 | 114 |
|
115 | 115 |
|
116 | 116 |
// float |
117 | 117 |
template <typename _Graph, typename _Item> |
118 | 118 |
struct DefaultMapSelector<_Graph, _Item, float> { |
119 | 119 |
typedef VectorMap<_Graph, _Item, float> Map; |
120 | 120 |
}; |
121 | 121 |
|
122 | 122 |
|
123 | 123 |
// double |
124 | 124 |
template <typename _Graph, typename _Item> |
125 | 125 |
struct DefaultMapSelector<_Graph, _Item, double> { |
126 | 126 |
typedef VectorMap<_Graph, _Item, double> Map; |
127 | 127 |
}; |
128 | 128 |
|
129 | 129 |
|
130 | 130 |
// long double |
131 | 131 |
template <typename _Graph, typename _Item> |
132 | 132 |
struct DefaultMapSelector<_Graph, _Item, long double> { |
133 | 133 |
typedef VectorMap<_Graph, _Item, long double> Map; |
134 | 134 |
}; |
135 | 135 |
|
136 | 136 |
|
137 | 137 |
// pointer |
138 | 138 |
template <typename _Graph, typename _Item, typename _Ptr> |
139 | 139 |
struct DefaultMapSelector<_Graph, _Item, _Ptr*> { |
140 | 140 |
typedef VectorMap<_Graph, _Item, _Ptr*> Map; |
141 | 141 |
}; |
142 | 142 |
|
143 | 143 |
// #else |
144 | 144 |
|
145 | 145 |
// template <typename _Graph, typename _Item, typename _Value> |
146 | 146 |
// struct DefaultMapSelector { |
147 | 147 |
// typedef DebugMap<_Graph, _Item, _Value> Map; |
148 | 148 |
// }; |
149 | 149 |
|
150 | 150 |
// #endif |
151 | 151 |
|
152 | 152 |
// DefaultMap class |
153 | 153 |
template <typename _Graph, typename _Item, typename _Value> |
154 | 154 |
class DefaultMap |
155 | 155 |
: public DefaultMapSelector<_Graph, _Item, _Value>::Map { |
156 | 156 |
typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent; |
157 | 157 |
|
158 | 158 |
public: |
159 | 159 |
typedef DefaultMap<_Graph, _Item, _Value> Map; |
160 | 160 |
|
161 | 161 |
typedef typename Parent::GraphType GraphType; |
162 | 162 |
typedef typename Parent::Value Value; |
163 | 163 |
|
164 | 164 |
explicit DefaultMap(const GraphType& graph) : Parent(graph) {} |
165 | 165 |
DefaultMap(const GraphType& graph, const Value& value) |
166 | 166 |
: Parent(graph, value) {} |
167 | 167 |
|
168 | 168 |
DefaultMap& operator=(const DefaultMap& cmap) { |
169 | 169 |
return operator=<DefaultMap>(cmap); |
170 | 170 |
} |
171 | 171 |
|
172 | 172 |
template <typename CMap> |
173 | 173 |
DefaultMap& operator=(const CMap& cmap) { |
174 | 174 |
Parent::operator=(cmap); |
175 | 175 |
return *this; |
176 | 176 |
} |
177 | 177 |
|
178 | 178 |
}; |
179 | 179 |
|
180 | 180 |
} |
181 | 181 |
|
182 | 182 |
#endif |
1 |
/* -*- C++ -*- |
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
|
2 | 2 |
* |
3 |
* This file is a part of LEMON, a generic C++ optimization library |
|
3 |
* This file is a part of LEMON, a generic C++ optimization library. |
|
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-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 | 94 |
class NodeIt : public Node { |
95 | 95 |
const Digraph* digraph; |
96 | 96 |
public: |
97 | 97 |
|
98 | 98 |
NodeIt() {} |
99 | 99 |
|
100 | 100 |
NodeIt(Invalid i) : Node(i) { } |
101 | 101 |
|
102 | 102 |
explicit NodeIt(const Digraph& _graph) : digraph(&_graph) { |
103 | 103 |
_graph.first(static_cast<Node&>(*this)); |
104 | 104 |
} |
105 | 105 |
|
106 | 106 |
NodeIt(const Digraph& _graph, const Node& node) |
107 | 107 |
: Node(node), digraph(&_graph) {} |
108 | 108 |
|
109 | 109 |
NodeIt& operator++() { |
110 | 110 |
digraph->next(*this); |
111 | 111 |
return *this; |
112 | 112 |
} |
113 | 113 |
|
114 | 114 |
}; |
115 | 115 |
|
116 | 116 |
|
117 | 117 |
class ArcIt : public Arc { |
118 | 118 |
const Digraph* digraph; |
119 | 119 |
public: |
120 | 120 |
|
121 | 121 |
ArcIt() { } |
122 | 122 |
|
123 | 123 |
ArcIt(Invalid i) : Arc(i) { } |
124 | 124 |
|
125 | 125 |
explicit ArcIt(const Digraph& _graph) : digraph(&_graph) { |
126 | 126 |
_graph.first(static_cast<Arc&>(*this)); |
127 | 127 |
} |
128 | 128 |
|
129 | 129 |
ArcIt(const Digraph& _graph, const Arc& e) : |
130 | 130 |
Arc(e), digraph(&_graph) { } |
131 | 131 |
|
132 | 132 |
ArcIt& operator++() { |
133 | 133 |
digraph->next(*this); |
134 | 134 |
return *this; |
135 | 135 |
} |
136 | 136 |
|
137 | 137 |
}; |
138 | 138 |
|
139 | 139 |
|
140 | 140 |
class OutArcIt : public Arc { |
141 | 141 |
const Digraph* digraph; |
142 | 142 |
public: |
143 | 143 |
|
144 | 144 |
OutArcIt() { } |
145 | 145 |
|
146 | 146 |
OutArcIt(Invalid i) : Arc(i) { } |
147 | 147 |
|
148 | 148 |
OutArcIt(const Digraph& _graph, const Node& node) |
149 | 149 |
: digraph(&_graph) { |
150 | 150 |
_graph.firstOut(*this, node); |
151 | 151 |
} |
152 | 152 |
|
153 | 153 |
OutArcIt(const Digraph& _graph, const Arc& arc) |
154 | 154 |
: Arc(arc), digraph(&_graph) {} |
155 | 155 |
|
156 | 156 |
OutArcIt& operator++() { |
157 | 157 |
digraph->nextOut(*this); |
158 | 158 |
return *this; |
159 | 159 |
} |
160 | 160 |
|
161 | 161 |
}; |
162 | 162 |
|
163 | 163 |
|
164 | 164 |
class InArcIt : public Arc { |
165 | 165 |
const Digraph* digraph; |
166 | 166 |
public: |
167 | 167 |
|
168 | 168 |
InArcIt() { } |
169 | 169 |
|
170 | 170 |
InArcIt(Invalid i) : Arc(i) { } |
171 | 171 |
|
172 | 172 |
InArcIt(const Digraph& _graph, const Node& node) |
173 | 173 |
: digraph(&_graph) { |
174 | 174 |
_graph.firstIn(*this, node); |
175 | 175 |
} |
176 | 176 |
|
177 | 177 |
InArcIt(const Digraph& _graph, const Arc& arc) : |
178 | 178 |
Arc(arc), digraph(&_graph) {} |
179 | 179 |
|
180 | 180 |
InArcIt& operator++() { |
181 | 181 |
digraph->nextIn(*this); |
182 | 182 |
return *this; |
183 | 183 |
} |
184 | 184 |
|
185 | 185 |
}; |
186 | 186 |
|
187 | 187 |
// \brief Base node of the iterator |
188 | 188 |
// |
189 | 189 |
// Returns the base node (ie. the source in this case) of the iterator |
190 | 190 |
Node baseNode(const OutArcIt &e) const { |
191 | 191 |
return Parent::source(static_cast<const Arc&>(e)); |
192 | 192 |
} |
193 | 193 |
// \brief Running node of the iterator |
194 | 194 |
// |
195 | 195 |
// Returns the running node (ie. the target in this case) of the |
196 | 196 |
// iterator |
197 | 197 |
Node runningNode(const OutArcIt &e) const { |
198 | 198 |
return Parent::target(static_cast<const Arc&>(e)); |
199 | 199 |
} |
200 | 200 |
|
201 | 201 |
// \brief Base node of the iterator |
202 | 202 |
// |
203 | 203 |
// Returns the base node (ie. the target in this case) of the iterator |
204 | 204 |
Node baseNode(const InArcIt &e) const { |
205 | 205 |
return Parent::target(static_cast<const Arc&>(e)); |
206 | 206 |
} |
207 | 207 |
// \brief Running node of the iterator |
208 | 208 |
// |
209 | 209 |
// Returns the running node (ie. the source in this case) of the |
210 | 210 |
// iterator |
211 | 211 |
Node runningNode(const InArcIt &e) const { |
212 | 212 |
return Parent::source(static_cast<const Arc&>(e)); |
213 | 213 |
} |
214 | 214 |
|
215 | 215 |
using Parent::first; |
216 | 216 |
|
217 | 217 |
// Mappable extension |
218 | 218 |
|
219 | 219 |
template <typename _Value> |
220 | 220 |
class ArcMap |
221 | 221 |
: public MapExtender<DefaultMap<Digraph, Arc, _Value> > { |
222 | 222 |
typedef MapExtender<DefaultMap<Digraph, Arc, _Value> > Parent; |
223 | 223 |
|
224 | 224 |
public: |
225 | 225 |
explicit ArcMap(const Digraph& _g) |
226 | 226 |
: Parent(_g) {} |
227 | 227 |
ArcMap(const Digraph& _g, const _Value& _v) |
228 | 228 |
: Parent(_g, _v) {} |
229 | 229 |
|
230 | 230 |
ArcMap& operator=(const ArcMap& cmap) { |
231 | 231 |
return operator=<ArcMap>(cmap); |
232 | 232 |
} |
233 | 233 |
|
234 | 234 |
template <typename CMap> |
235 | 235 |
ArcMap& operator=(const CMap& cmap) { |
236 | 236 |
Parent::operator=(cmap); |
237 | 237 |
return *this; |
238 | 238 |
} |
239 | 239 |
|
240 | 240 |
}; |
241 | 241 |
|
242 | 242 |
|
243 | 243 |
// Alteration extension |
244 | 244 |
|
245 | 245 |
Arc addArc(const Node& from, const Node& to) { |
246 | 246 |
Arc arc = Parent::addArc(from, to); |
247 | 247 |
notifier(Arc()).add(arc); |
248 | 248 |
return arc; |
249 | 249 |
} |
250 | 250 |
|
251 | 251 |
void clear() { |
252 | 252 |
notifier(Arc()).clear(); |
253 | 253 |
Parent::clear(); |
254 | 254 |
} |
255 | 255 |
|
256 | 256 |
void erase(const Arc& arc) { |
257 | 257 |
notifier(Arc()).erase(arc); |
258 | 258 |
Parent::erase(arc); |
259 | 259 |
} |
260 | 260 |
|
261 | 261 |
ArcSetExtender() { |
262 | 262 |
arc_notifier.setContainer(*this); |
263 | 263 |
} |
264 | 264 |
|
265 | 265 |
~ArcSetExtender() { |
266 | 266 |
arc_notifier.clear(); |
267 | 267 |
} |
268 | 268 |
|
269 | 269 |
}; |
270 | 270 |
|
271 | 271 |
|
272 | 272 |
// \ingroup digraphbits |
273 | 273 |
// |
274 | 274 |
// \brief Extender for the EdgeSets |
275 | 275 |
template <typename Base> |
276 | 276 |
class EdgeSetExtender : public Base { |
277 | 277 |
typedef Base Parent; |
278 | 278 |
|
279 | 279 |
public: |
280 | 280 |
|
281 | 281 |
typedef EdgeSetExtender Graph; |
282 | 282 |
|
283 | 283 |
typedef 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 | 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 | 365 |
NodeIt(const Graph& _graph, const Node& node) |
366 | 366 |
: Node(node), graph(&_graph) {} |
367 | 367 |
|
368 | 368 |
NodeIt& operator++() { |
369 | 369 |
graph->next(*this); |
370 | 370 |
return *this; |
371 | 371 |
} |
372 | 372 |
|
373 | 373 |
}; |
374 | 374 |
|
375 | 375 |
|
376 | 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 | 388 |
ArcIt(const Graph& _graph, const Arc& e) : |
389 | 389 |
Arc(e), graph(&_graph) { } |
390 | 390 |
|
391 | 391 |
ArcIt& operator++() { |
392 | 392 |
graph->next(*this); |
393 | 393 |
return *this; |
394 | 394 |
} |
395 | 395 |
|
396 | 396 |
}; |
397 | 397 |
|
398 | 398 |
|
399 | 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 | 407 |
OutArcIt(const Graph& _graph, const Node& node) |
408 | 408 |
: graph(&_graph) { |
409 | 409 |
_graph.firstOut(*this, node); |
410 | 410 |
} |
411 | 411 |
|
412 | 412 |
OutArcIt(const Graph& _graph, const Arc& arc) |
413 | 413 |
: Arc(arc), graph(&_graph) {} |
414 | 414 |
|
415 | 415 |
OutArcIt& operator++() { |
416 | 416 |
graph->nextOut(*this); |
417 | 417 |
return *this; |
418 | 418 |
} |
419 | 419 |
|
420 | 420 |
}; |
421 | 421 |
|
422 | 422 |
|
423 | 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 | 431 |
InArcIt(const Graph& _graph, const Node& node) |
432 | 432 |
: graph(&_graph) { |
433 | 433 |
_graph.firstIn(*this, node); |
434 | 434 |
} |
435 | 435 |
|
436 | 436 |
InArcIt(const Graph& _graph, const Arc& arc) : |
437 | 437 |
Arc(arc), graph(&_graph) {} |
438 | 438 |
|
439 | 439 |
InArcIt& operator++() { |
440 | 440 |
graph->nextIn(*this); |
441 | 441 |
return *this; |
442 | 442 |
} |
443 | 443 |
|
444 | 444 |
}; |
445 | 445 |
|
446 | 446 |
|
447 | 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 | 459 |
EdgeIt(const Graph& _graph, const Edge& e) : |
460 | 460 |
Edge(e), graph(&_graph) { } |
461 | 461 |
|
462 | 462 |
EdgeIt& operator++() { |
463 | 463 |
graph->next(*this); |
464 | 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 | 484 |
: graph(&_graph), Edge(ue) { |
485 | 485 |
direction = (_graph.source(ue) == n); |
486 | 486 |
} |
487 | 487 |
|
488 | 488 |
IncEdgeIt& operator++() { |
489 | 489 |
graph->nextInc(*this, direction); |
490 | 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 | 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 | 542 |
explicit ArcMap(const Graph& _g) |
543 | 543 |
: Parent(_g) {} |
544 | 544 |
ArcMap(const Graph& _g, const _Value& _v) |
545 | 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 | 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 | 566 |
explicit EdgeMap(const Graph& _g) |
567 | 567 |
: Parent(_g) {} |
568 | 568 |
|
569 | 569 |
EdgeMap(const Graph& _g, const _Value& _v) |
570 | 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 |
|
70 | 70 |
NodeIt() {} |
71 | 71 |
|
72 | 72 |
NodeIt(Invalid i) : Node(i) { } |
73 | 73 |
|
74 | 74 |
explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
75 | 75 |
_adaptor->first(static_cast<Node&>(*this)); |
76 | 76 |
} |
77 | 77 |
|
78 | 78 |
NodeIt(const Adaptor& adaptor, const Node& node) |
79 | 79 |
: Node(node), _adaptor(&adaptor) {} |
80 | 80 |
|
81 | 81 |
NodeIt& operator++() { |
82 | 82 |
_adaptor->next(*this); |
83 | 83 |
return *this; |
84 | 84 |
} |
85 | 85 |
|
86 | 86 |
}; |
87 | 87 |
|
88 | 88 |
|
89 | 89 |
class ArcIt : public Arc { |
90 | 90 |
const Adaptor* _adaptor; |
91 | 91 |
public: |
92 | 92 |
|
93 | 93 |
ArcIt() { } |
94 | 94 |
|
95 | 95 |
ArcIt(Invalid i) : Arc(i) { } |
96 | 96 |
|
97 | 97 |
explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
98 | 98 |
_adaptor->first(static_cast<Arc&>(*this)); |
99 | 99 |
} |
100 | 100 |
|
101 | 101 |
ArcIt(const Adaptor& adaptor, const Arc& e) : |
102 | 102 |
Arc(e), _adaptor(&adaptor) { } |
103 | 103 |
|
104 | 104 |
ArcIt& operator++() { |
105 | 105 |
_adaptor->next(*this); |
106 | 106 |
return *this; |
107 | 107 |
} |
108 | 108 |
|
109 | 109 |
}; |
110 | 110 |
|
111 | 111 |
|
112 | 112 |
class OutArcIt : public Arc { |
113 | 113 |
const Adaptor* _adaptor; |
114 | 114 |
public: |
115 | 115 |
|
116 | 116 |
OutArcIt() { } |
117 | 117 |
|
118 | 118 |
OutArcIt(Invalid i) : Arc(i) { } |
119 | 119 |
|
120 | 120 |
OutArcIt(const Adaptor& adaptor, const Node& node) |
121 | 121 |
: _adaptor(&adaptor) { |
122 | 122 |
_adaptor->firstOut(*this, node); |
123 | 123 |
} |
124 | 124 |
|
125 | 125 |
OutArcIt(const Adaptor& adaptor, const Arc& arc) |
126 | 126 |
: Arc(arc), _adaptor(&adaptor) {} |
127 | 127 |
|
128 | 128 |
OutArcIt& operator++() { |
129 | 129 |
_adaptor->nextOut(*this); |
130 | 130 |
return *this; |
131 | 131 |
} |
132 | 132 |
|
133 | 133 |
}; |
134 | 134 |
|
135 | 135 |
|
136 | 136 |
class InArcIt : public Arc { |
137 | 137 |
const Adaptor* _adaptor; |
138 | 138 |
public: |
139 | 139 |
|
140 | 140 |
InArcIt() { } |
141 | 141 |
|
142 | 142 |
InArcIt(Invalid i) : Arc(i) { } |
143 | 143 |
|
144 | 144 |
InArcIt(const Adaptor& adaptor, const Node& node) |
145 | 145 |
: _adaptor(&adaptor) { |
146 | 146 |
_adaptor->firstIn(*this, node); |
147 | 147 |
} |
148 | 148 |
|
149 | 149 |
InArcIt(const Adaptor& adaptor, const Arc& arc) : |
150 | 150 |
Arc(arc), _adaptor(&adaptor) {} |
151 | 151 |
|
152 | 152 |
InArcIt& operator++() { |
153 | 153 |
_adaptor->nextIn(*this); |
154 | 154 |
return *this; |
155 | 155 |
} |
156 | 156 |
|
157 | 157 |
}; |
158 | 158 |
|
159 | 159 |
Node baseNode(const OutArcIt &e) const { |
160 | 160 |
return Parent::source(e); |
161 | 161 |
} |
162 | 162 |
Node runningNode(const OutArcIt &e) const { |
163 | 163 |
return Parent::target(e); |
164 | 164 |
} |
165 | 165 |
|
166 | 166 |
Node baseNode(const InArcIt &e) const { |
167 | 167 |
return Parent::target(e); |
168 | 168 |
} |
169 | 169 |
Node runningNode(const InArcIt &e) const { |
170 | 170 |
return Parent::source(e); |
171 | 171 |
} |
172 | 172 |
|
173 | 173 |
}; |
174 | 174 |
|
175 | 175 |
template <typename _Graph> |
176 | 176 |
class GraphAdaptorExtender : public _Graph { |
177 | 177 |
typedef _Graph Parent; |
178 | 178 |
|
179 | 179 |
public: |
180 | 180 |
|
181 | 181 |
typedef _Graph Graph; |
182 | 182 |
typedef GraphAdaptorExtender Adaptor; |
183 | 183 |
|
184 | 184 |
typedef True UndirectedTag; |
185 | 185 |
|
186 | 186 |
typedef typename Parent::Node Node; |
187 | 187 |
typedef typename Parent::Arc Arc; |
188 | 188 |
typedef typename Parent::Edge Edge; |
189 | 189 |
|
190 | 190 |
// Graph extension |
191 | 191 |
|
192 | 192 |
int maxId(Node) const { |
193 | 193 |
return Parent::maxNodeId(); |
194 | 194 |
} |
195 | 195 |
|
196 | 196 |
int maxId(Arc) const { |
197 | 197 |
return Parent::maxArcId(); |
198 | 198 |
} |
199 | 199 |
|
200 | 200 |
int maxId(Edge) const { |
201 | 201 |
return Parent::maxEdgeId(); |
202 | 202 |
} |
203 | 203 |
|
204 | 204 |
Node fromId(int id, Node) const { |
205 | 205 |
return Parent::nodeFromId(id); |
206 | 206 |
} |
207 | 207 |
|
208 | 208 |
Arc fromId(int id, Arc) const { |
209 | 209 |
return Parent::arcFromId(id); |
210 | 210 |
} |
211 | 211 |
|
212 | 212 |
Edge fromId(int id, Edge) const { |
213 | 213 |
return Parent::edgeFromId(id); |
214 | 214 |
} |
215 | 215 |
|
216 | 216 |
Node oppositeNode(const Node &n, const Edge &e) const { |
217 | 217 |
if( n == Parent::u(e)) |
218 | 218 |
return Parent::v(e); |
219 | 219 |
else if( n == Parent::v(e)) |
220 | 220 |
return Parent::u(e); |
221 | 221 |
else |
222 | 222 |
return INVALID; |
223 | 223 |
} |
224 | 224 |
|
225 | 225 |
Arc oppositeArc(const Arc &a) const { |
226 | 226 |
return Parent::direct(a, !Parent::direction(a)); |
227 | 227 |
} |
228 | 228 |
|
229 | 229 |
using Parent::direct; |
230 | 230 |
Arc direct(const Edge &e, const Node &s) const { |
231 | 231 |
return Parent::direct(e, Parent::u(e) == s); |
232 | 232 |
} |
233 | 233 |
|
234 | 234 |
|
235 | 235 |
class NodeIt : public Node { |
236 | 236 |
const Adaptor* _adaptor; |
237 | 237 |
public: |
238 | 238 |
|
239 | 239 |
NodeIt() {} |
240 | 240 |
|
241 | 241 |
NodeIt(Invalid i) : Node(i) { } |
242 | 242 |
|
243 | 243 |
explicit NodeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
244 | 244 |
_adaptor->first(static_cast<Node&>(*this)); |
245 | 245 |
} |
246 | 246 |
|
247 | 247 |
NodeIt(const Adaptor& adaptor, const Node& node) |
248 | 248 |
: Node(node), _adaptor(&adaptor) {} |
249 | 249 |
|
250 | 250 |
NodeIt& operator++() { |
251 | 251 |
_adaptor->next(*this); |
252 | 252 |
return *this; |
253 | 253 |
} |
254 | 254 |
|
255 | 255 |
}; |
256 | 256 |
|
257 | 257 |
|
258 | 258 |
class ArcIt : public Arc { |
259 | 259 |
const Adaptor* _adaptor; |
260 | 260 |
public: |
261 | 261 |
|
262 | 262 |
ArcIt() { } |
263 | 263 |
|
264 | 264 |
ArcIt(Invalid i) : Arc(i) { } |
265 | 265 |
|
266 | 266 |
explicit ArcIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
267 | 267 |
_adaptor->first(static_cast<Arc&>(*this)); |
268 | 268 |
} |
269 | 269 |
|
270 | 270 |
ArcIt(const Adaptor& adaptor, const Arc& e) : |
271 | 271 |
Arc(e), _adaptor(&adaptor) { } |
272 | 272 |
|
273 | 273 |
ArcIt& operator++() { |
274 | 274 |
_adaptor->next(*this); |
275 | 275 |
return *this; |
276 | 276 |
} |
277 | 277 |
|
278 | 278 |
}; |
279 | 279 |
|
280 | 280 |
|
281 | 281 |
class OutArcIt : public Arc { |
282 | 282 |
const Adaptor* _adaptor; |
283 | 283 |
public: |
284 | 284 |
|
285 | 285 |
OutArcIt() { } |
286 | 286 |
|
287 | 287 |
OutArcIt(Invalid i) : Arc(i) { } |
288 | 288 |
|
289 | 289 |
OutArcIt(const Adaptor& adaptor, const Node& node) |
290 | 290 |
: _adaptor(&adaptor) { |
291 | 291 |
_adaptor->firstOut(*this, node); |
292 | 292 |
} |
293 | 293 |
|
294 | 294 |
OutArcIt(const Adaptor& adaptor, const Arc& arc) |
295 | 295 |
: Arc(arc), _adaptor(&adaptor) {} |
296 | 296 |
|
297 | 297 |
OutArcIt& operator++() { |
298 | 298 |
_adaptor->nextOut(*this); |
299 | 299 |
return *this; |
300 | 300 |
} |
301 | 301 |
|
302 | 302 |
}; |
303 | 303 |
|
304 | 304 |
|
305 | 305 |
class InArcIt : public Arc { |
306 | 306 |
const Adaptor* _adaptor; |
307 | 307 |
public: |
308 | 308 |
|
309 | 309 |
InArcIt() { } |
310 | 310 |
|
311 | 311 |
InArcIt(Invalid i) : Arc(i) { } |
312 | 312 |
|
313 | 313 |
InArcIt(const Adaptor& adaptor, const Node& node) |
314 | 314 |
: _adaptor(&adaptor) { |
315 | 315 |
_adaptor->firstIn(*this, node); |
316 | 316 |
} |
317 | 317 |
|
318 | 318 |
InArcIt(const Adaptor& adaptor, const Arc& arc) : |
319 | 319 |
Arc(arc), _adaptor(&adaptor) {} |
320 | 320 |
|
321 | 321 |
InArcIt& operator++() { |
322 | 322 |
_adaptor->nextIn(*this); |
323 | 323 |
return *this; |
324 | 324 |
} |
325 | 325 |
|
326 | 326 |
}; |
327 | 327 |
|
328 | 328 |
class EdgeIt : public Parent::Edge { |
329 | 329 |
const Adaptor* _adaptor; |
330 | 330 |
public: |
331 | 331 |
|
332 | 332 |
EdgeIt() { } |
333 | 333 |
|
334 | 334 |
EdgeIt(Invalid i) : Edge(i) { } |
335 | 335 |
|
336 | 336 |
explicit EdgeIt(const Adaptor& adaptor) : _adaptor(&adaptor) { |
337 | 337 |
_adaptor->first(static_cast<Edge&>(*this)); |
338 | 338 |
} |
339 | 339 |
|
340 | 340 |
EdgeIt(const Adaptor& adaptor, const Edge& e) : |
341 | 341 |
Edge(e), _adaptor(&adaptor) { } |
342 | 342 |
|
343 | 343 |
EdgeIt& operator++() { |
344 | 344 |
_adaptor->next(*this); |
345 | 345 |
return *this; |
346 | 346 |
} |
347 | 347 |
|
348 | 348 |
}; |
349 | 349 |
|
350 | 350 |
class IncEdgeIt : public Edge { |
351 | 351 |
friend class GraphAdaptorExtender; |
352 | 352 |
const Adaptor* _adaptor; |
353 | 353 |
bool direction; |
354 | 354 |
public: |
355 | 355 |
|
356 | 356 |
IncEdgeIt() { } |
357 | 357 |
|
358 | 358 |
IncEdgeIt(Invalid i) : Edge(i), direction(false) { } |
359 | 359 |
|
360 | 360 |
IncEdgeIt(const Adaptor& adaptor, const Node &n) : _adaptor(&adaptor) { |
361 | 361 |
_adaptor->firstInc(static_cast<Edge&>(*this), direction, n); |
362 | 362 |
} |
363 | 363 |
|
364 | 364 |
IncEdgeIt(const Adaptor& adaptor, const Edge &e, const Node &n) |
365 | 365 |
: _adaptor(&adaptor), Edge(e) { |
366 | 366 |
direction = (_adaptor->u(e) == n); |
367 | 367 |
} |
368 | 368 |
|
369 | 369 |
IncEdgeIt& operator++() { |
370 | 370 |
_adaptor->nextInc(*this, direction); |
371 | 371 |
return *this; |
372 | 372 |
} |
373 | 373 |
}; |
374 | 374 |
|
375 | 375 |
Node baseNode(const OutArcIt &a) const { |
376 | 376 |
return Parent::source(a); |
377 | 377 |
} |
378 | 378 |
Node runningNode(const OutArcIt &a) const { |
379 | 379 |
return Parent::target(a); |
380 | 380 |
} |
381 | 381 |
|
382 | 382 |
Node baseNode(const InArcIt &a) const { |
383 | 383 |
return Parent::target(a); |
384 | 384 |
} |
385 | 385 |
Node runningNode(const InArcIt &a) const { |
386 | 386 |
return Parent::source(a); |
387 | 387 |
} |
388 | 388 |
|
389 | 389 |
Node baseNode(const IncEdgeIt &e) const { |
390 | 390 |
return e.direction ? Parent::u(e) : Parent::v(e); |
391 | 391 |
} |
392 | 392 |
Node runningNode(const IncEdgeIt &e) const { |
393 | 393 |
return e.direction ? Parent::v(e) : Parent::u(e); |
394 | 394 |
} |
395 | 395 |
|
396 | 396 |
}; |
397 | 397 |
|
398 | 398 |
} |
399 | 399 |
|
400 | 400 |
|
401 | 401 |
#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_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) { |
70 | 70 |
return operator=<MapExtender>(cmap); |
71 | 71 |
} |
72 | 72 |
|
73 | 73 |
template <typename CMap> |
74 | 74 |
MapExtender& operator=(const CMap& cmap) { |
75 | 75 |
Parent::operator=(cmap); |
76 | 76 |
return *this; |
77 | 77 |
} |
78 | 78 |
|
79 | 79 |
public: |
80 | 80 |
class MapIt : public Item { |
81 | 81 |
typedef Item Parent; |
82 | 82 |
|
83 | 83 |
public: |
84 | 84 |
|
85 | 85 |
typedef typename Map::Value Value; |
86 | 86 |
|
87 | 87 |
MapIt() : map(NULL) {} |
88 | 88 |
|
89 | 89 |
MapIt(Invalid i) : Parent(i), map(NULL) {} |
90 | 90 |
|
91 | 91 |
explicit MapIt(Map& _map) : map(&_map) { |
92 | 92 |
map->notifier()->first(*this); |
93 | 93 |
} |
94 | 94 |
|
95 | 95 |
MapIt(const Map& _map, const Item& item) |
96 | 96 |
: Parent(item), map(&_map) {} |
97 | 97 |
|
98 | 98 |
MapIt& operator++() { |
99 | 99 |
map->notifier()->next(*this); |
100 | 100 |
return *this; |
101 | 101 |
} |
102 | 102 |
|
103 | 103 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
104 | 104 |
return (*map)[*this]; |
105 | 105 |
} |
106 | 106 |
|
107 | 107 |
typename MapTraits<Map>::ReturnValue operator*() { |
108 | 108 |
return (*map)[*this]; |
109 | 109 |
} |
110 | 110 |
|
111 | 111 |
void set(const Value& value) { |
112 | 112 |
map->set(*this, value); |
113 | 113 |
} |
114 | 114 |
|
115 | 115 |
protected: |
116 | 116 |
Map* map; |
117 | 117 |
|
118 | 118 |
}; |
119 | 119 |
|
120 | 120 |
class ConstMapIt : public Item { |
121 | 121 |
typedef Item Parent; |
122 | 122 |
|
123 | 123 |
public: |
124 | 124 |
|
125 | 125 |
typedef typename Map::Value Value; |
126 | 126 |
|
127 | 127 |
ConstMapIt() : map(NULL) {} |
128 | 128 |
|
129 | 129 |
ConstMapIt(Invalid i) : Parent(i), map(NULL) {} |
130 | 130 |
|
131 | 131 |
explicit ConstMapIt(Map& _map) : map(&_map) { |
132 | 132 |
map->notifier()->first(*this); |
133 | 133 |
} |
134 | 134 |
|
135 | 135 |
ConstMapIt(const Map& _map, const Item& item) |
136 | 136 |
: Parent(item), map(_map) {} |
137 | 137 |
|
138 | 138 |
ConstMapIt& operator++() { |
139 | 139 |
map->notifier()->next(*this); |
140 | 140 |
return *this; |
141 | 141 |
} |
142 | 142 |
|
143 | 143 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
144 | 144 |
return map[*this]; |
145 | 145 |
} |
146 | 146 |
|
147 | 147 |
protected: |
148 | 148 |
const Map* map; |
149 | 149 |
}; |
150 | 150 |
|
151 | 151 |
class ItemIt : public Item { |
152 | 152 |
typedef Item Parent; |
153 | 153 |
|
154 | 154 |
public: |
155 | 155 |
ItemIt() : map(NULL) {} |
156 | 156 |
|
157 | 157 |
|
158 | 158 |
ItemIt(Invalid i) : Parent(i), map(NULL) {} |
159 | 159 |
|
160 | 160 |
explicit ItemIt(Map& _map) : map(&_map) { |
161 | 161 |
map->notifier()->first(*this); |
162 | 162 |
} |
163 | 163 |
|
164 | 164 |
ItemIt(const Map& _map, const Item& item) |
165 | 165 |
: Parent(item), map(&_map) {} |
166 | 166 |
|
167 | 167 |
ItemIt& operator++() { |
168 | 168 |
map->notifier()->next(*this); |
169 | 169 |
return *this; |
170 | 170 |
} |
171 | 171 |
|
172 | 172 |
protected: |
173 | 173 |
const Map* map; |
174 | 174 |
|
175 | 175 |
}; |
176 | 176 |
}; |
177 | 177 |
|
178 | 178 |
// \ingroup graphbits |
179 | 179 |
// |
180 | 180 |
// \brief Extender for maps which use a subset of the items. |
181 | 181 |
template <typename _Graph, typename _Map> |
182 | 182 |
class SubMapExtender : public _Map { |
183 | 183 |
typedef _Map Parent; |
184 | 184 |
typedef _Graph GraphType; |
185 | 185 |
|
186 | 186 |
public: |
187 | 187 |
|
188 | 188 |
typedef SubMapExtender Map; |
189 | 189 |
typedef typename Parent::Key Item; |
190 | 190 |
|
191 | 191 |
typedef typename Parent::Key Key; |
192 | 192 |
typedef typename Parent::Value Value; |
193 | 193 |
typedef typename Parent::Reference Reference; |
194 | 194 |
typedef typename Parent::ConstReference ConstReference; |
195 | 195 |
|
196 | 196 |
typedef typename Parent::ReferenceMapTag ReferenceMapTag; |
197 | 197 |
|
198 | 198 |
class MapIt; |
199 | 199 |
class ConstMapIt; |
200 | 200 |
|
201 | 201 |
friend class MapIt; |
202 | 202 |
friend class ConstMapIt; |
203 | 203 |
|
204 | 204 |
public: |
205 | 205 |
|
206 | 206 |
SubMapExtender(const GraphType& _graph) |
207 | 207 |
: Parent(_graph), graph(_graph) {} |
208 | 208 |
|
209 | 209 |
SubMapExtender(const GraphType& _graph, const Value& _value) |
210 | 210 |
: Parent(_graph, _value), graph(_graph) {} |
211 | 211 |
|
212 | 212 |
private: |
213 | 213 |
SubMapExtender& operator=(const SubMapExtender& cmap) { |
214 | 214 |
return operator=<MapExtender>(cmap); |
215 | 215 |
} |
216 | 216 |
|
217 | 217 |
template <typename CMap> |
218 | 218 |
SubMapExtender& operator=(const CMap& cmap) { |
219 | 219 |
checkConcept<concepts::ReadMap<Key, Value>, CMap>(); |
220 | 220 |
Item it; |
221 | 221 |
for (graph.first(it); it != INVALID; graph.next(it)) { |
222 | 222 |
Parent::set(it, cmap[it]); |
223 | 223 |
} |
224 | 224 |
return *this; |
225 | 225 |
} |
226 | 226 |
|
227 | 227 |
public: |
228 | 228 |
class MapIt : public Item { |
229 | 229 |
typedef Item Parent; |
230 | 230 |
|
231 | 231 |
public: |
232 | 232 |
typedef typename Map::Value Value; |
233 | 233 |
|
234 | 234 |
MapIt() : map(NULL) {} |
235 | 235 |
|
236 | 236 |
MapIt(Invalid i) : Parent(i), map(NULL) { } |
237 | 237 |
|
238 | 238 |
explicit MapIt(Map& _map) : map(&_map) { |
239 | 239 |
map->graph.first(*this); |
240 | 240 |
} |
241 | 241 |
|
242 | 242 |
MapIt(const Map& _map, const Item& item) |
243 | 243 |
: Parent(item), map(&_map) {} |
244 | 244 |
|
245 | 245 |
MapIt& operator++() { |
246 | 246 |
map->graph.next(*this); |
247 | 247 |
return *this; |
248 | 248 |
} |
249 | 249 |
|
250 | 250 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
251 | 251 |
return (*map)[*this]; |
252 | 252 |
} |
253 | 253 |
|
254 | 254 |
typename MapTraits<Map>::ReturnValue operator*() { |
255 | 255 |
return (*map)[*this]; |
256 | 256 |
} |
257 | 257 |
|
258 | 258 |
void set(const Value& value) { |
259 | 259 |
map->set(*this, value); |
260 | 260 |
} |
261 | 261 |
|
262 | 262 |
protected: |
263 | 263 |
Map* map; |
264 | 264 |
|
265 | 265 |
}; |
266 | 266 |
|
267 | 267 |
class ConstMapIt : public Item { |
268 | 268 |
typedef Item Parent; |
269 | 269 |
|
270 | 270 |
public: |
271 | 271 |
|
272 | 272 |
typedef typename Map::Value Value; |
273 | 273 |
|
274 | 274 |
ConstMapIt() : map(NULL) {} |
275 | 275 |
|
276 | 276 |
ConstMapIt(Invalid i) : Parent(i), map(NULL) { } |
277 | 277 |
|
278 | 278 |
explicit ConstMapIt(Map& _map) : map(&_map) { |
279 | 279 |
map->graph.first(*this); |
280 | 280 |
} |
281 | 281 |
|
282 | 282 |
ConstMapIt(const Map& _map, const Item& item) |
283 | 283 |
: Parent(item), map(&_map) {} |
284 | 284 |
|
285 | 285 |
ConstMapIt& operator++() { |
286 | 286 |
map->graph.next(*this); |
287 | 287 |
return *this; |
288 | 288 |
} |
289 | 289 |
|
290 | 290 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
291 | 291 |
return (*map)[*this]; |
292 | 292 |
} |
293 | 293 |
|
294 | 294 |
protected: |
295 | 295 |
const Map* map; |
296 | 296 |
}; |
297 | 297 |
|
298 | 298 |
class ItemIt : public Item { |
299 | 299 |
typedef Item Parent; |
300 | 300 |
|
301 | 301 |
public: |
302 | 302 |
ItemIt() : map(NULL) {} |
303 | 303 |
|
304 | 304 |
|
305 | 305 |
ItemIt(Invalid i) : Parent(i), map(NULL) { } |
306 | 306 |
|
307 | 307 |
explicit ItemIt(Map& _map) : map(&_map) { |
308 | 308 |
map->graph.first(*this); |
309 | 309 |
} |
310 | 310 |
|
311 | 311 |
ItemIt(const Map& _map, const Item& item) |
312 | 312 |
: Parent(item), map(&_map) {} |
313 | 313 |
|
314 | 314 |
ItemIt& operator++() { |
315 | 315 |
map->graph.next(*this); |
316 | 316 |
return *this; |
317 | 317 |
} |
318 | 318 |
|
319 | 319 |
protected: |
320 | 320 |
const Map* map; |
321 | 321 |
|
322 | 322 |
}; |
323 | 323 |
|
324 | 324 |
private: |
325 | 325 |
|
326 | 326 |
const GraphType& graph; |
327 | 327 |
|
328 | 328 |
}; |
329 | 329 |
|
330 | 330 |
} |
331 | 331 |
|
332 | 332 |
#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_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]); |
70 | 70 |
if (path->predMap[current] == INVALID) current = INVALID; |
71 | 71 |
return *this; |
72 | 72 |
} |
73 | 73 |
|
74 | 74 |
bool operator==(const RevArcIt& e) const { |
75 | 75 |
return current == e.current; |
76 | 76 |
} |
77 | 77 |
|
78 | 78 |
bool operator!=(const RevArcIt& e) const { |
79 | 79 |
return current != e.current; |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
bool operator<(const RevArcIt& e) const { |
83 | 83 |
return current < e.current; |
84 | 84 |
} |
85 | 85 |
|
86 | 86 |
private: |
87 | 87 |
const PredMapPath* path; |
88 | 88 |
typename Digraph::Node current; |
89 | 89 |
}; |
90 | 90 |
|
91 | 91 |
private: |
92 | 92 |
const Digraph& digraph; |
93 | 93 |
const PredMap& predMap; |
94 | 94 |
typename Digraph::Node target; |
95 | 95 |
}; |
96 | 96 |
|
97 | 97 |
|
98 | 98 |
template <typename _Digraph, typename _PredMatrixMap> |
99 | 99 |
class PredMatrixMapPath { |
100 | 100 |
public: |
101 | 101 |
typedef True RevPathTag; |
102 | 102 |
|
103 | 103 |
typedef _Digraph Digraph; |
104 | 104 |
typedef typename Digraph::Arc Arc; |
105 | 105 |
typedef _PredMatrixMap PredMatrixMap; |
106 | 106 |
|
107 | 107 |
PredMatrixMapPath(const Digraph& _digraph, |
108 | 108 |
const PredMatrixMap& _predMatrixMap, |
109 | 109 |
typename Digraph::Node _source, |
110 | 110 |
typename Digraph::Node _target) |
111 | 111 |
: digraph(_digraph), predMatrixMap(_predMatrixMap), |
112 | 112 |
source(_source), target(_target) {} |
113 | 113 |
|
114 | 114 |
int length() const { |
115 | 115 |
int len = 0; |
116 | 116 |
typename Digraph::Node node = target; |
117 | 117 |
typename Digraph::Arc arc; |
118 | 118 |
while ((arc = predMatrixMap(source, node)) != INVALID) { |
119 | 119 |
node = digraph.source(arc); |
120 | 120 |
++len; |
121 | 121 |
} |
122 | 122 |
return len; |
123 | 123 |
} |
124 | 124 |
|
125 | 125 |
bool empty() const { |
126 | 126 |
return predMatrixMap(source, target) == INVALID; |
127 | 127 |
} |
128 | 128 |
|
129 | 129 |
class RevArcIt { |
130 | 130 |
public: |
131 | 131 |
RevArcIt() {} |
132 | 132 |
RevArcIt(Invalid) : path(0), current(INVALID) {} |
133 | 133 |
RevArcIt(const PredMatrixMapPath& _path) |
134 | 134 |
: path(&_path), current(_path.target) { |
135 | 135 |
if (path->predMatrixMap(path->source, current) == INVALID) |
136 | 136 |
current = INVALID; |
137 | 137 |
} |
138 | 138 |
|
139 | 139 |
operator const typename Digraph::Arc() const { |
140 | 140 |
return path->predMatrixMap(path->source, current); |
141 | 141 |
} |
142 | 142 |
|
143 | 143 |
RevArcIt& operator++() { |
144 | 144 |
current = |
145 | 145 |
path->digraph.source(path->predMatrixMap(path->source, current)); |
146 | 146 |
if (path->predMatrixMap(path->source, current) == INVALID) |
147 | 147 |
current = INVALID; |
148 | 148 |
return *this; |
149 | 149 |
} |
150 | 150 |
|
151 | 151 |
bool operator==(const RevArcIt& e) const { |
152 | 152 |
return current == e.current; |
153 | 153 |
} |
154 | 154 |
|
155 | 155 |
bool operator!=(const RevArcIt& e) const { |
156 | 156 |
return current != e.current; |
157 | 157 |
} |
158 | 158 |
|
159 | 159 |
bool operator<(const RevArcIt& e) const { |
160 | 160 |
return current < e.current; |
161 | 161 |
} |
162 | 162 |
|
163 | 163 |
private: |
164 | 164 |
const PredMatrixMapPath* path; |
165 | 165 |
typename Digraph::Node current; |
166 | 166 |
}; |
167 | 167 |
|
168 | 168 |
private: |
169 | 169 |
const Digraph& digraph; |
170 | 170 |
const PredMatrixMap& predMatrixMap; |
171 | 171 |
typename Digraph::Node source; |
172 | 172 |
typename Digraph::Node target; |
173 | 173 |
}; |
174 | 174 |
|
175 | 175 |
} |
176 | 176 |
|
177 | 177 |
#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_SOLVER_BITS_H |
20 | 20 |
#define LEMON_BITS_SOLVER_BITS_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
|
24 | 24 |
namespace lemon { |
25 | 25 |
|
26 | 26 |
namespace _solver_bits { |
27 | 27 |
|
28 | 28 |
class VarIndex { |
29 | 29 |
private: |
30 | 30 |
struct ItemT { |
31 | 31 |
int prev, next; |
32 | 32 |
int index; |
33 | 33 |
}; |
34 | 34 |
std::vector<ItemT> items; |
35 | 35 |
int first_item, last_item, first_free_item; |
36 | 36 |
|
37 | 37 |
std::vector<int> cross; |
38 | 38 |
|
39 | 39 |
public: |
40 | 40 |
|
41 | 41 |
VarIndex() |
42 | 42 |
: first_item(-1), last_item(-1), first_free_item(-1) { |
43 | 43 |
} |
44 | 44 |
|
45 | 45 |
void clear() { |
46 | 46 |
first_item = -1; |
47 | 47 |
first_free_item = -1; |
48 | 48 |
items.clear(); |
49 | 49 |
cross.clear(); |
50 | 50 |
} |
51 | 51 |
|
52 | 52 |
int addIndex(int idx) { |
53 | 53 |
int n; |
54 | 54 |
if (first_free_item == -1) { |
55 | 55 |
n = items.size(); |
56 | 56 |
items.push_back(ItemT()); |
57 | 57 |
} else { |
58 | 58 |
n = first_free_item; |
59 | 59 |
first_free_item = items[n].next; |
60 | 60 |
if (first_free_item != -1) { |
61 | 61 |
items[first_free_item].prev = -1; |
62 | 62 |
} |
63 | 63 |
} |
64 | 64 |
items[n].index = idx; |
65 | 65 |
if (static_cast<int>(cross.size()) <= idx) { |
66 | 66 |
cross.resize(idx + 1, -1); |
67 | 67 |
} |
68 | 68 |
cross[idx] = n; |
69 | 69 |
|
70 | 70 |
items[n].prev = last_item; |
71 | 71 |
items[n].next = -1; |
72 | 72 |
if (last_item != -1) { |
73 | 73 |
items[last_item].next = n; |
74 | 74 |
} else { |
75 | 75 |
first_item = n; |
76 | 76 |
} |
77 | 77 |
last_item = n; |
78 | 78 |
|
79 | 79 |
return n; |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
int addIndex(int idx, int n) { |
83 | 83 |
while (n >= static_cast<int>(items.size())) { |
84 | 84 |
items.push_back(ItemT()); |
85 | 85 |
items.back().prev = -1; |
86 | 86 |
items.back().next = first_free_item; |
87 | 87 |
if (first_free_item != -1) { |
88 | 88 |
items[first_free_item].prev = items.size() - 1; |
89 | 89 |
} |
90 | 90 |
first_free_item = items.size() - 1; |
91 | 91 |
} |
92 | 92 |
if (items[n].next != -1) { |
93 | 93 |
items[items[n].next].prev = items[n].prev; |
94 | 94 |
} |
95 | 95 |
if (items[n].prev != -1) { |
96 | 96 |
items[items[n].prev].next = items[n].next; |
97 | 97 |
} else { |
98 | 98 |
first_free_item = items[n].next; |
99 | 99 |
} |
100 | 100 |
|
101 | 101 |
items[n].index = idx; |
102 | 102 |
if (static_cast<int>(cross.size()) <= idx) { |
103 | 103 |
cross.resize(idx + 1, -1); |
104 | 104 |
} |
105 | 105 |
cross[idx] = n; |
106 | 106 |
|
107 | 107 |
items[n].prev = last_item; |
108 | 108 |
items[n].next = -1; |
109 | 109 |
if (last_item != -1) { |
110 | 110 |
items[last_item].next = n; |
111 | 111 |
} else { |
112 | 112 |
first_item = n; |
113 | 113 |
} |
114 | 114 |
last_item = n; |
115 | 115 |
|
116 | 116 |
return n; |
117 | 117 |
} |
118 | 118 |
|
119 | 119 |
void eraseIndex(int idx) { |
120 | 120 |
int n = cross[idx]; |
121 | 121 |
|
122 | 122 |
if (items[n].prev != -1) { |
123 | 123 |
items[items[n].prev].next = items[n].next; |
124 | 124 |
} else { |
125 | 125 |
first_item = items[n].next; |
126 | 126 |
} |
127 | 127 |
if (items[n].next != -1) { |
128 | 128 |
items[items[n].next].prev = items[n].prev; |
129 | 129 |
} else { |
130 | 130 |
last_item = items[n].prev; |
131 | 131 |
} |
132 | 132 |
|
133 | 133 |
if (first_free_item != -1) { |
134 | 134 |
items[first_free_item].prev = n; |
135 | 135 |
} |
136 | 136 |
items[n].next = first_free_item; |
137 | 137 |
items[n].prev = -1; |
138 | 138 |
first_free_item = n; |
139 | 139 |
|
140 | 140 |
while (!cross.empty() && cross.back() == -1) { |
141 | 141 |
cross.pop_back(); |
142 | 142 |
} |
143 | 143 |
} |
144 | 144 |
|
145 | 145 |
int maxIndex() const { |
146 | 146 |
return cross.size() - 1; |
147 | 147 |
} |
148 | 148 |
|
149 | 149 |
void shiftIndices(int idx) { |
150 | 150 |
for (int i = idx + 1; i < static_cast<int>(cross.size()); ++i) { |
151 | 151 |
cross[i - 1] = cross[i]; |
152 | 152 |
if (cross[i] != -1) { |
153 | 153 |
--items[cross[i]].index; |
154 | 154 |
} |
155 | 155 |
} |
156 | 156 |
cross.back() = -1; |
157 | 157 |
cross.pop_back(); |
158 | 158 |
while (!cross.empty() && cross.back() == -1) { |
159 | 159 |
cross.pop_back(); |
160 | 160 |
} |
161 | 161 |
} |
162 | 162 |
|
163 | 163 |
void relocateIndex(int idx, int jdx) { |
164 | 164 |
cross[idx] = cross[jdx]; |
165 | 165 |
items[cross[jdx]].index = idx; |
166 | 166 |
cross[jdx] = -1; |
167 | 167 |
|
168 | 168 |
while (!cross.empty() && cross.back() == -1) { |
169 | 169 |
cross.pop_back(); |
170 | 170 |
} |
171 | 171 |
} |
172 | 172 |
|
173 | 173 |
int operator[](int idx) const { |
174 | 174 |
return cross[idx]; |
175 | 175 |
} |
176 | 176 |
|
177 | 177 |
int operator()(int fdx) const { |
178 | 178 |
return items[fdx].index; |
179 | 179 |
} |
180 | 180 |
|
181 | 181 |
void firstItem(int& fdx) const { |
182 | 182 |
fdx = first_item; |
183 | 183 |
} |
184 | 184 |
|
185 | 185 |
void nextItem(int& fdx) const { |
186 | 186 |
fdx = items[fdx].next; |
187 | 187 |
} |
188 | 188 |
|
189 | 189 |
}; |
190 | 190 |
} |
191 | 191 |
} |
192 | 192 |
|
193 | 193 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-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 | 62 |
/// The type of the map that stores the signed supply values of the |
63 | 63 |
/// nodes. |
64 | 64 |
/// It must conform to the \ref concepts::ReadMap "ReadMap" concept. |
65 | 65 |
typedef SM SupplyMap; |
66 | 66 |
|
67 | 67 |
/// \brief The type of the flow and supply values. |
68 | 68 |
typedef typename SupplyMap::Value Value; |
69 | 69 |
|
70 | 70 |
/// \brief The type of the map that stores the flow values. |
71 | 71 |
/// |
72 | 72 |
/// The type of the map that stores the flow values. |
73 | 73 |
/// It must conform to the \ref concepts::ReadWriteMap "ReadWriteMap" |
74 | 74 |
/// concept. |
75 | 75 |
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; |
212 | 212 |
int _node_num; |
213 | 213 |
|
214 | 214 |
const LowerMap *_lo; |
215 | 215 |
const UpperMap *_up; |
216 | 216 |
const SupplyMap *_supply; |
217 | 217 |
|
218 | 218 |
FlowMap *_flow; |
219 | 219 |
bool _local_flow; |
220 | 220 |
|
221 | 221 |
Elevator* _level; |
222 | 222 |
bool _local_level; |
223 | 223 |
|
224 | 224 |
typedef typename Digraph::template NodeMap<Value> ExcessMap; |
225 | 225 |
ExcessMap* _excess; |
226 | 226 |
|
227 | 227 |
Tolerance _tol; |
228 | 228 |
int _el; |
229 | 229 |
|
230 | 230 |
public: |
231 | 231 |
|
232 | 232 |
typedef Circulation Create; |
233 | 233 |
|
234 | 234 |
///\name Named Template Parameters |
235 | 235 |
|
236 | 236 |
///@{ |
237 | 237 |
|
238 | 238 |
template <typename T> |
239 | 239 |
struct SetFlowMapTraits : public Traits { |
240 | 240 |
typedef T FlowMap; |
241 | 241 |
static FlowMap *createFlowMap(const Digraph&) { |
242 | 242 |
LEMON_ASSERT(false, "FlowMap is not initialized"); |
243 | 243 |
return 0; // ignore warnings |
244 | 244 |
} |
245 | 245 |
}; |
246 | 246 |
|
247 | 247 |
/// \brief \ref named-templ-param "Named parameter" for setting |
248 | 248 |
/// FlowMap type |
249 | 249 |
/// |
250 | 250 |
/// \ref named-templ-param "Named parameter" for setting FlowMap |
251 | 251 |
/// type. |
252 | 252 |
template <typename T> |
253 | 253 |
struct SetFlowMap |
254 | 254 |
: public Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
255 | 255 |
SetFlowMapTraits<T> > { |
256 | 256 |
typedef Circulation<Digraph, LowerMap, UpperMap, SupplyMap, |
257 | 257 |
SetFlowMapTraits<T> > Create; |
258 | 258 |
}; |
259 | 259 |
|
260 | 260 |
template <typename T> |
261 | 261 |
struct SetElevatorTraits : public Traits { |
262 | 262 |
typedef T Elevator; |
263 | 263 |
static Elevator *createElevator(const Digraph&, int) { |
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 | 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 |
|
393 | 393 |
/// Sets the upper bound (capacity) map. |
394 | 394 |
/// \return <tt>(*this)</tt> |
395 | 395 |
Circulation& upperMap(const UpperMap& map) { |
396 | 396 |
_up = ↦ |
397 | 397 |
return *this; |
398 | 398 |
} |
399 | 399 |
|
400 | 400 |
/// Sets the supply map. |
401 | 401 |
|
402 | 402 |
/// Sets the supply map. |
403 | 403 |
/// \return <tt>(*this)</tt> |
404 | 404 |
Circulation& supplyMap(const SupplyMap& map) { |
405 | 405 |
_supply = ↦ |
406 | 406 |
return *this; |
407 | 407 |
} |
408 | 408 |
|
409 | 409 |
/// \brief Sets the flow map. |
410 | 410 |
/// |
411 | 411 |
/// Sets the flow map. |
412 | 412 |
/// If you don't use this function before calling \ref run() or |
413 | 413 |
/// \ref init(), an instance will be allocated automatically. |
414 | 414 |
/// The destructor deallocates this automatically allocated map, |
415 | 415 |
/// of course. |
416 | 416 |
/// \return <tt>(*this)</tt> |
417 | 417 |
Circulation& flowMap(FlowMap& map) { |
418 | 418 |
if (_local_flow) { |
419 | 419 |
delete _flow; |
420 | 420 |
_local_flow = false; |
421 | 421 |
} |
422 | 422 |
_flow = ↦ |
423 | 423 |
return *this; |
424 | 424 |
} |
425 | 425 |
|
426 | 426 |
/// \brief Sets the elevator used by algorithm. |
427 | 427 |
/// |
428 | 428 |
/// Sets the elevator used by algorithm. |
429 | 429 |
/// If you don't use this function before calling \ref run() or |
430 | 430 |
/// \ref init(), an instance will be allocated automatically. |
431 | 431 |
/// The destructor deallocates this automatically allocated elevator, |
432 | 432 |
/// of course. |
433 | 433 |
/// \return <tt>(*this)</tt> |
434 | 434 |
Circulation& elevator(Elevator& elevator) { |
435 | 435 |
if (_local_level) { |
436 | 436 |
delete _level; |
437 | 437 |
_local_level = false; |
438 | 438 |
} |
439 | 439 |
_level = &elevator; |
440 | 440 |
return *this; |
441 | 441 |
} |
442 | 442 |
|
443 | 443 |
/// \brief Returns a const reference to the elevator. |
444 | 444 |
/// |
445 | 445 |
/// Returns a const reference to the elevator. |
446 | 446 |
/// |
447 | 447 |
/// \pre Either \ref run() or \ref init() must be called before |
448 | 448 |
/// using this function. |
449 | 449 |
const Elevator& elevator() const { |
450 | 450 |
return *_level; |
451 | 451 |
} |
452 | 452 |
|
453 | 453 |
/// \brief Sets the tolerance used by algorithm. |
454 | 454 |
/// |
455 | 455 |
/// Sets the tolerance used by algorithm. |
456 | 456 |
Circulation& tolerance(const Tolerance& tolerance) { |
457 | 457 |
_tol = tolerance; |
458 | 458 |
return *this; |
459 | 459 |
} |
460 | 460 |
|
461 | 461 |
/// \brief Returns a const reference to the tolerance. |
462 | 462 |
/// |
463 | 463 |
/// Returns a const reference to the tolerance. |
464 | 464 |
const Tolerance& tolerance() const { |
465 | 465 |
return _tol; |
466 | 466 |
} |
467 | 467 |
|
468 | 468 |
/// \name Execution Control |
469 | 469 |
/// The simplest way to execute the algorithm is to call \ref run().\n |
470 | 470 |
/// If you need more control on the initial solution or the execution, |
471 | 471 |
/// first you have to call one of the \ref init() functions, then |
472 | 472 |
/// the \ref start() function. |
473 | 473 |
|
474 | 474 |
///@{ |
475 | 475 |
|
476 | 476 |
/// Initializes the internal data structures. |
477 | 477 |
|
478 | 478 |
/// Initializes the internal data structures and sets all flow values |
479 | 479 |
/// to the lower bound. |
480 | 480 |
void init() |
481 | 481 |
{ |
482 | 482 |
LEMON_DEBUG(checkBoundMaps(), |
483 | 483 |
"Upper bounds must be greater or equal to the lower bounds"); |
484 | 484 |
|
485 | 485 |
createStructures(); |
486 | 486 |
|
487 | 487 |
for(NodeIt n(_g);n!=INVALID;++n) { |
488 | 488 |
(*_excess)[n] = (*_supply)[n]; |
489 | 489 |
} |
490 | 490 |
|
491 | 491 |
for (ArcIt e(_g);e!=INVALID;++e) { |
492 | 492 |
_flow->set(e, (*_lo)[e]); |
493 | 493 |
(*_excess)[_g.target(e)] += (*_flow)[e]; |
494 | 494 |
(*_excess)[_g.source(e)] -= (*_flow)[e]; |
495 | 495 |
} |
496 | 496 |
|
497 | 497 |
// global relabeling tested, but in general case it provides |
498 | 498 |
// worse performance for random digraphs |
499 | 499 |
_level->initStart(); |
500 | 500 |
for(NodeIt n(_g);n!=INVALID;++n) |
501 | 501 |
_level->initAddItem(n); |
502 | 502 |
_level->initFinish(); |
503 | 503 |
for(NodeIt n(_g);n!=INVALID;++n) |
504 | 504 |
if(_tol.positive((*_excess)[n])) |
505 | 505 |
_level->activate(n); |
506 | 506 |
} |
507 | 507 |
|
508 | 508 |
/// Initializes the internal data structures using a greedy approach. |
509 | 509 |
|
510 | 510 |
/// Initializes the internal data structures using a greedy approach |
511 | 511 |
/// to construct the initial solution. |
512 | 512 |
void greedyInit() |
513 | 513 |
{ |
514 | 514 |
LEMON_DEBUG(checkBoundMaps(), |
515 | 515 |
"Upper bounds must be greater or equal to the lower bounds"); |
516 | 516 |
|
517 | 517 |
createStructures(); |
518 | 518 |
|
519 | 519 |
for(NodeIt n(_g);n!=INVALID;++n) { |
520 | 520 |
(*_excess)[n] = (*_supply)[n]; |
521 | 521 |
} |
522 | 522 |
|
523 | 523 |
for (ArcIt e(_g);e!=INVALID;++e) { |
524 | 524 |
if (!_tol.less(-(*_excess)[_g.target(e)], (*_up)[e])) { |
525 | 525 |
_flow->set(e, (*_up)[e]); |
526 | 526 |
(*_excess)[_g.target(e)] += (*_up)[e]; |
527 | 527 |
(*_excess)[_g.source(e)] -= (*_up)[e]; |
528 | 528 |
} else if (_tol.less(-(*_excess)[_g.target(e)], (*_lo)[e])) { |
529 | 529 |
_flow->set(e, (*_lo)[e]); |
530 | 530 |
(*_excess)[_g.target(e)] += (*_lo)[e]; |
531 | 531 |
(*_excess)[_g.source(e)] -= (*_lo)[e]; |
532 | 532 |
} else { |
533 | 533 |
Value fc = -(*_excess)[_g.target(e)]; |
534 | 534 |
_flow->set(e, fc); |
535 | 535 |
(*_excess)[_g.target(e)] = 0; |
536 | 536 |
(*_excess)[_g.source(e)] -= fc; |
537 | 537 |
} |
538 | 538 |
} |
539 | 539 |
|
540 | 540 |
_level->initStart(); |
541 | 541 |
for(NodeIt n(_g);n!=INVALID;++n) |
542 | 542 |
_level->initAddItem(n); |
543 | 543 |
_level->initFinish(); |
544 | 544 |
for(NodeIt n(_g);n!=INVALID;++n) |
545 | 545 |
if(_tol.positive((*_excess)[n])) |
546 | 546 |
_level->activate(n); |
547 | 547 |
} |
548 | 548 |
|
549 | 549 |
///Executes the algorithm |
550 | 550 |
|
551 | 551 |
///This function executes the algorithm. |
552 | 552 |
/// |
553 | 553 |
///\return \c true if a feasible circulation is found. |
554 | 554 |
/// |
555 | 555 |
///\sa barrier() |
556 | 556 |
///\sa barrierMap() |
557 | 557 |
bool start() |
558 | 558 |
{ |
559 | 559 |
|
560 | 560 |
Node act; |
561 | 561 |
Node bact=INVALID; |
562 | 562 |
Node last_activated=INVALID; |
563 | 563 |
while((act=_level->highestActive())!=INVALID) { |
564 | 564 |
int actlevel=(*_level)[act]; |
565 | 565 |
int mlevel=_node_num; |
566 | 566 |
Value exc=(*_excess)[act]; |
567 | 567 |
|
568 | 568 |
for(OutArcIt e(_g,act);e!=INVALID; ++e) { |
569 | 569 |
Node v = _g.target(e); |
570 | 570 |
Value fc=(*_up)[e]-(*_flow)[e]; |
571 | 571 |
if(!_tol.positive(fc)) continue; |
572 | 572 |
if((*_level)[v]<actlevel) { |
573 | 573 |
if(!_tol.less(fc, exc)) { |
574 | 574 |
_flow->set(e, (*_flow)[e] + exc); |
575 | 575 |
(*_excess)[v] += exc; |
576 | 576 |
if(!_level->active(v) && _tol.positive((*_excess)[v])) |
577 | 577 |
_level->activate(v); |
578 | 578 |
(*_excess)[act] = 0; |
579 | 579 |
_level->deactivate(act); |
580 | 580 |
goto next_l; |
581 | 581 |
} |
582 | 582 |
else { |
583 | 583 |
_flow->set(e, (*_up)[e]); |
584 | 584 |
(*_excess)[v] += fc; |
585 | 585 |
if(!_level->active(v) && _tol.positive((*_excess)[v])) |
586 | 586 |
_level->activate(v); |
587 | 587 |
exc-=fc; |
588 | 588 |
} |
589 | 589 |
} |
590 | 590 |
else if((*_level)[v]<mlevel) mlevel=(*_level)[v]; |
591 | 591 |
} |
592 | 592 |
for(InArcIt e(_g,act);e!=INVALID; ++e) { |
593 | 593 |
Node v = _g.source(e); |
594 | 594 |
Value fc=(*_flow)[e]-(*_lo)[e]; |
595 | 595 |
if(!_tol.positive(fc)) continue; |
596 | 596 |
if((*_level)[v]<actlevel) { |
597 | 597 |
if(!_tol.less(fc, exc)) { |
598 | 598 |
_flow->set(e, (*_flow)[e] - exc); |
599 | 599 |
(*_excess)[v] += exc; |
600 | 600 |
if(!_level->active(v) && _tol.positive((*_excess)[v])) |
601 | 601 |
_level->activate(v); |
602 | 602 |
(*_excess)[act] = 0; |
603 | 603 |
_level->deactivate(act); |
604 | 604 |
goto next_l; |
605 | 605 |
} |
606 | 606 |
else { |
607 | 607 |
_flow->set(e, (*_lo)[e]); |
608 | 608 |
(*_excess)[v] += fc; |
609 | 609 |
if(!_level->active(v) && _tol.positive((*_excess)[v])) |
610 | 610 |
_level->activate(v); |
611 | 611 |
exc-=fc; |
612 | 612 |
} |
613 | 613 |
} |
614 | 614 |
else if((*_level)[v]<mlevel) mlevel=(*_level)[v]; |
615 | 615 |
} |
616 | 616 |
|
617 | 617 |
(*_excess)[act] = exc; |
618 | 618 |
if(!_tol.positive(exc)) _level->deactivate(act); |
619 | 619 |
else if(mlevel==_node_num) { |
620 | 620 |
_level->liftHighestActiveToTop(); |
621 | 621 |
_el = _node_num; |
622 | 622 |
return false; |
623 | 623 |
} |
624 | 624 |
else { |
625 | 625 |
_level->liftHighestActive(mlevel+1); |
626 | 626 |
if(_level->onLevel(actlevel)==0) { |
627 | 627 |
_el = actlevel; |
628 | 628 |
return false; |
629 | 629 |
} |
630 | 630 |
} |
631 | 631 |
next_l: |
632 | 632 |
; |
633 | 633 |
} |
634 | 634 |
return true; |
635 | 635 |
} |
636 | 636 |
|
637 | 637 |
/// Runs the algorithm. |
638 | 638 |
|
639 | 639 |
/// This function runs the algorithm. |
640 | 640 |
/// |
641 | 641 |
/// \return \c true if a feasible circulation is found. |
642 | 642 |
/// |
643 | 643 |
/// \note Apart from the return value, c.run() is just a shortcut of |
644 | 644 |
/// the following code. |
645 | 645 |
/// \code |
646 | 646 |
/// c.greedyInit(); |
647 | 647 |
/// c.start(); |
648 | 648 |
/// \endcode |
649 | 649 |
bool run() { |
650 | 650 |
greedyInit(); |
651 | 651 |
return start(); |
652 | 652 |
} |
653 | 653 |
|
654 | 654 |
/// @} |
655 | 655 |
|
656 | 656 |
/// \name Query Functions |
657 | 657 |
/// The results of the circulation algorithm can be obtained using |
658 | 658 |
/// these functions.\n |
659 | 659 |
/// Either \ref run() or \ref start() should be called before |
660 | 660 |
/// using them. |
661 | 661 |
|
662 | 662 |
///@{ |
663 | 663 |
|
664 | 664 |
/// \brief Returns the flow value on the given arc. |
665 | 665 |
/// |
666 | 666 |
/// Returns the flow value on the given arc. |
667 | 667 |
/// |
668 | 668 |
/// \pre Either \ref run() or \ref init() must be called before |
669 | 669 |
/// using this function. |
670 | 670 |
Value flow(const Arc& arc) const { |
671 | 671 |
return (*_flow)[arc]; |
672 | 672 |
} |
673 | 673 |
|
674 | 674 |
/// \brief Returns a const reference to the flow map. |
675 | 675 |
/// |
676 | 676 |
/// Returns a const reference to the arc map storing the found flow. |
677 | 677 |
/// |
678 | 678 |
/// \pre Either \ref run() or \ref init() must be called before |
679 | 679 |
/// using this function. |
680 | 680 |
const FlowMap& flowMap() const { |
681 | 681 |
return *_flow; |
682 | 682 |
} |
683 | 683 |
|
684 | 684 |
/** |
685 | 685 |
\brief Returns \c true if the given node is in a barrier. |
686 | 686 |
|
687 | 687 |
Barrier is a set \e B of nodes for which |
688 | 688 |
|
689 | 689 |
\f[ \sum_{uv\in A: u\in B} upper(uv) - |
690 | 690 |
\sum_{uv\in A: v\in B} lower(uv) < \sum_{v\in B} sup(v) \f] |
691 | 691 |
|
692 | 692 |
holds. The existence of a set with this property prooves that a |
693 | 693 |
feasible circualtion cannot exist. |
694 | 694 |
|
695 | 695 |
This function returns \c true if the given node is in the found |
696 | 696 |
barrier. If a feasible circulation is found, the function |
697 | 697 |
gives back \c false for every node. |
698 | 698 |
|
699 | 699 |
\pre Either \ref run() or \ref init() must be called before |
700 | 700 |
using this function. |
701 | 701 |
|
702 | 702 |
\sa barrierMap() |
703 | 703 |
\sa checkBarrier() |
704 | 704 |
*/ |
705 | 705 |
bool barrier(const Node& node) const |
706 | 706 |
{ |
707 | 707 |
return (*_level)[node] >= _el; |
708 | 708 |
} |
709 | 709 |
|
710 | 710 |
/// \brief Gives back a barrier. |
711 | 711 |
/// |
712 | 712 |
/// This function sets \c bar to the characteristic vector of the |
713 | 713 |
/// found barrier. \c bar should be a \ref concepts::WriteMap "writable" |
714 | 714 |
/// node map with \c bool (or convertible) value type. |
715 | 715 |
/// |
716 | 716 |
/// If a feasible circulation is found, the function gives back an |
717 | 717 |
/// empty set, so \c bar[v] will be \c false for all nodes \c v. |
718 | 718 |
/// |
719 | 719 |
/// \note This function calls \ref barrier() for each node, |
720 | 720 |
/// so it runs in O(n) time. |
721 | 721 |
/// |
722 | 722 |
/// \pre Either \ref run() or \ref init() must be called before |
723 | 723 |
/// using this function. |
724 | 724 |
/// |
725 | 725 |
/// \sa barrier() |
726 | 726 |
/// \sa checkBarrier() |
727 | 727 |
template<class BarrierMap> |
728 | 728 |
void barrierMap(BarrierMap &bar) const |
729 | 729 |
{ |
730 | 730 |
for(NodeIt n(_g);n!=INVALID;++n) |
731 | 731 |
bar.set(n, (*_level)[n] >= _el); |
732 | 732 |
} |
733 | 733 |
|
734 | 734 |
/// @} |
735 | 735 |
|
736 | 736 |
/// \name Checker Functions |
737 | 737 |
/// The feasibility of the results can be checked using |
738 | 738 |
/// these functions.\n |
739 | 739 |
/// Either \ref run() or \ref start() should be called before |
740 | 740 |
/// using them. |
741 | 741 |
|
742 | 742 |
///@{ |
743 | 743 |
|
744 | 744 |
///Check if the found flow is a feasible circulation |
745 | 745 |
|
746 | 746 |
///Check if the found flow is a feasible circulation, |
747 | 747 |
/// |
748 | 748 |
bool checkFlow() const { |
749 | 749 |
for(ArcIt e(_g);e!=INVALID;++e) |
750 | 750 |
if((*_flow)[e]<(*_lo)[e]||(*_flow)[e]>(*_up)[e]) return false; |
751 | 751 |
for(NodeIt n(_g);n!=INVALID;++n) |
752 | 752 |
{ |
753 | 753 |
Value dif=-(*_supply)[n]; |
754 | 754 |
for(InArcIt e(_g,n);e!=INVALID;++e) dif-=(*_flow)[e]; |
755 | 755 |
for(OutArcIt e(_g,n);e!=INVALID;++e) dif+=(*_flow)[e]; |
756 | 756 |
if(_tol.negative(dif)) return false; |
757 | 757 |
} |
758 | 758 |
return true; |
759 | 759 |
} |
760 | 760 |
|
761 | 761 |
///Check whether or not the last execution provides a barrier |
762 | 762 |
|
763 | 763 |
///Check whether or not the last execution provides a barrier. |
764 | 764 |
///\sa barrier() |
765 | 765 |
///\sa barrierMap() |
766 | 766 |
bool checkBarrier() const |
767 | 767 |
{ |
768 | 768 |
Value delta=0; |
769 | 769 |
Value inf_cap = std::numeric_limits<Value>::has_infinity ? |
770 | 770 |
std::numeric_limits<Value>::infinity() : |
771 | 771 |
std::numeric_limits<Value>::max(); |
772 | 772 |
for(NodeIt n(_g);n!=INVALID;++n) |
773 | 773 |
if(barrier(n)) |
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"; } |
70 | 70 |
|
71 | 71 |
int ClpLp::_addCol() { |
72 | 72 |
_prob->addColumn(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX, 0.0); |
73 | 73 |
return _prob->numberColumns() - 1; |
74 | 74 |
} |
75 | 75 |
|
76 | 76 |
int ClpLp::_addRow() { |
77 | 77 |
_prob->addRow(0, 0, 0, -COIN_DBL_MAX, COIN_DBL_MAX); |
78 | 78 |
return _prob->numberRows() - 1; |
79 | 79 |
} |
80 | 80 |
|
81 | 81 |
|
82 | 82 |
void ClpLp::_eraseCol(int c) { |
83 | 83 |
_col_names_ref.erase(_prob->getColumnName(c)); |
84 | 84 |
_prob->deleteColumns(1, &c); |
85 | 85 |
} |
86 | 86 |
|
87 | 87 |
void ClpLp::_eraseRow(int r) { |
88 | 88 |
_row_names_ref.erase(_prob->getRowName(r)); |
89 | 89 |
_prob->deleteRows(1, &r); |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
void ClpLp::_eraseColId(int i) { |
93 | 93 |
cols.eraseIndex(i); |
94 | 94 |
cols.shiftIndices(i); |
95 | 95 |
} |
96 | 96 |
|
97 | 97 |
void ClpLp::_eraseRowId(int i) { |
98 | 98 |
rows.eraseIndex(i); |
99 | 99 |
rows.shiftIndices(i); |
100 | 100 |
} |
101 | 101 |
|
102 | 102 |
void ClpLp::_getColName(int c, std::string& name) const { |
103 | 103 |
name = _prob->getColumnName(c); |
104 | 104 |
} |
105 | 105 |
|
106 | 106 |
void ClpLp::_setColName(int c, const std::string& name) { |
107 | 107 |
_prob->setColumnName(c, const_cast<std::string&>(name)); |
108 | 108 |
_col_names_ref[name] = c; |
109 | 109 |
} |
110 | 110 |
|
111 | 111 |
int ClpLp::_colByName(const std::string& name) const { |
112 | 112 |
std::map<std::string, int>::const_iterator it = _col_names_ref.find(name); |
113 | 113 |
return it != _col_names_ref.end() ? it->second : -1; |
114 | 114 |
} |
115 | 115 |
|
116 | 116 |
void ClpLp::_getRowName(int r, std::string& name) const { |
117 | 117 |
name = _prob->getRowName(r); |
118 | 118 |
} |
119 | 119 |
|
120 | 120 |
void ClpLp::_setRowName(int r, const std::string& name) { |
121 | 121 |
_prob->setRowName(r, const_cast<std::string&>(name)); |
122 | 122 |
_row_names_ref[name] = r; |
123 | 123 |
} |
124 | 124 |
|
125 | 125 |
int ClpLp::_rowByName(const std::string& name) const { |
126 | 126 |
std::map<std::string, int>::const_iterator it = _row_names_ref.find(name); |
127 | 127 |
return it != _row_names_ref.end() ? it->second : -1; |
128 | 128 |
} |
129 | 129 |
|
130 | 130 |
|
131 | 131 |
void ClpLp::_setRowCoeffs(int ix, ExprIterator b, ExprIterator e) { |
132 | 132 |
std::map<int, Value> coeffs; |
133 | 133 |
|
134 | 134 |
int n = _prob->clpMatrix()->getNumCols(); |
135 | 135 |
|
136 | 136 |
const int* indices = _prob->clpMatrix()->getIndices(); |
137 | 137 |
const double* elements = _prob->clpMatrix()->getElements(); |
138 | 138 |
|
139 | 139 |
for (int i = 0; i < n; ++i) { |
140 | 140 |
CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[i]; |
141 | 141 |
CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[i]; |
142 | 142 |
|
143 | 143 |
const int* it = std::lower_bound(indices + begin, indices + end, ix); |
144 | 144 |
if (it != indices + end && *it == ix && elements[it - indices] != 0.0) { |
145 | 145 |
coeffs[i] = 0.0; |
146 | 146 |
} |
147 | 147 |
} |
148 | 148 |
|
149 | 149 |
for (ExprIterator it = b; it != e; ++it) { |
150 | 150 |
coeffs[it->first] = it->second; |
151 | 151 |
} |
152 | 152 |
|
153 | 153 |
for (std::map<int, Value>::iterator it = coeffs.begin(); |
154 | 154 |
it != coeffs.end(); ++it) { |
155 | 155 |
_prob->modifyCoefficient(ix, it->first, it->second); |
156 | 156 |
} |
157 | 157 |
} |
158 | 158 |
|
159 | 159 |
void ClpLp::_getRowCoeffs(int ix, InsertIterator b) const { |
160 | 160 |
int n = _prob->clpMatrix()->getNumCols(); |
161 | 161 |
|
162 | 162 |
const int* indices = _prob->clpMatrix()->getIndices(); |
163 | 163 |
const double* elements = _prob->clpMatrix()->getElements(); |
164 | 164 |
|
165 | 165 |
for (int i = 0; i < n; ++i) { |
166 | 166 |
CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[i]; |
167 | 167 |
CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[i]; |
168 | 168 |
|
169 | 169 |
const int* it = std::lower_bound(indices + begin, indices + end, ix); |
170 | 170 |
if (it != indices + end && *it == ix) { |
171 | 171 |
*b = std::make_pair(i, elements[it - indices]); |
172 | 172 |
} |
173 | 173 |
} |
174 | 174 |
} |
175 | 175 |
|
176 | 176 |
void ClpLp::_setColCoeffs(int ix, ExprIterator b, ExprIterator e) { |
177 | 177 |
std::map<int, Value> coeffs; |
178 | 178 |
|
179 | 179 |
CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix]; |
180 | 180 |
CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix]; |
181 | 181 |
|
182 | 182 |
const int* indices = _prob->clpMatrix()->getIndices(); |
183 | 183 |
const double* elements = _prob->clpMatrix()->getElements(); |
184 | 184 |
|
185 | 185 |
for (CoinBigIndex i = begin; i != end; ++i) { |
186 | 186 |
if (elements[i] != 0.0) { |
187 | 187 |
coeffs[indices[i]] = 0.0; |
188 | 188 |
} |
189 | 189 |
} |
190 | 190 |
for (ExprIterator it = b; it != e; ++it) { |
191 | 191 |
coeffs[it->first] = it->second; |
192 | 192 |
} |
193 | 193 |
for (std::map<int, Value>::iterator it = coeffs.begin(); |
194 | 194 |
it != coeffs.end(); ++it) { |
195 | 195 |
_prob->modifyCoefficient(it->first, ix, it->second); |
196 | 196 |
} |
197 | 197 |
} |
198 | 198 |
|
199 | 199 |
void ClpLp::_getColCoeffs(int ix, InsertIterator b) const { |
200 | 200 |
CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix]; |
201 | 201 |
CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix]; |
202 | 202 |
|
203 | 203 |
const int* indices = _prob->clpMatrix()->getIndices(); |
204 | 204 |
const double* elements = _prob->clpMatrix()->getElements(); |
205 | 205 |
|
206 | 206 |
for (CoinBigIndex i = begin; i != end; ++i) { |
207 | 207 |
*b = std::make_pair(indices[i], elements[i]); |
208 | 208 |
++b; |
209 | 209 |
} |
210 | 210 |
} |
211 | 211 |
|
212 | 212 |
void ClpLp::_setCoeff(int ix, int jx, Value value) { |
213 | 213 |
_prob->modifyCoefficient(ix, jx, value); |
214 | 214 |
} |
215 | 215 |
|
216 | 216 |
ClpLp::Value ClpLp::_getCoeff(int ix, int jx) const { |
217 | 217 |
CoinBigIndex begin = _prob->clpMatrix()->getVectorStarts()[ix]; |
218 | 218 |
CoinBigIndex end = begin + _prob->clpMatrix()->getVectorLengths()[ix]; |
219 | 219 |
|
220 | 220 |
const int* indices = _prob->clpMatrix()->getIndices(); |
221 | 221 |
const double* elements = _prob->clpMatrix()->getElements(); |
222 | 222 |
|
223 | 223 |
const int* it = std::lower_bound(indices + begin, indices + end, jx); |
224 | 224 |
if (it != indices + end && *it == jx) { |
225 | 225 |
return elements[it - indices]; |
226 | 226 |
} else { |
227 | 227 |
return 0.0; |
228 | 228 |
} |
229 | 229 |
} |
230 | 230 |
|
231 | 231 |
void ClpLp::_setColLowerBound(int i, Value lo) { |
232 | 232 |
_prob->setColumnLower(i, lo == - INF ? - COIN_DBL_MAX : lo); |
233 | 233 |
} |
234 | 234 |
|
235 | 235 |
ClpLp::Value ClpLp::_getColLowerBound(int i) const { |
236 | 236 |
double val = _prob->getColLower()[i]; |
237 | 237 |
return val == - COIN_DBL_MAX ? - INF : val; |
238 | 238 |
} |
239 | 239 |
|
240 | 240 |
void ClpLp::_setColUpperBound(int i, Value up) { |
241 | 241 |
_prob->setColumnUpper(i, up == INF ? COIN_DBL_MAX : up); |
242 | 242 |
} |
243 | 243 |
|
244 | 244 |
ClpLp::Value ClpLp::_getColUpperBound(int i) const { |
245 | 245 |
double val = _prob->getColUpper()[i]; |
246 | 246 |
return val == COIN_DBL_MAX ? INF : val; |
247 | 247 |
} |
248 | 248 |
|
249 | 249 |
void ClpLp::_setRowLowerBound(int i, Value lo) { |
250 | 250 |
_prob->setRowLower(i, lo == - INF ? - COIN_DBL_MAX : lo); |
251 | 251 |
} |
252 | 252 |
|
253 | 253 |
ClpLp::Value ClpLp::_getRowLowerBound(int i) const { |
254 | 254 |
double val = _prob->getRowLower()[i]; |
255 | 255 |
return val == - COIN_DBL_MAX ? - INF : val; |
256 | 256 |
} |
257 | 257 |
|
258 | 258 |
void ClpLp::_setRowUpperBound(int i, Value up) { |
259 | 259 |
_prob->setRowUpper(i, up == INF ? COIN_DBL_MAX : up); |
260 | 260 |
} |
261 | 261 |
|
262 | 262 |
ClpLp::Value ClpLp::_getRowUpperBound(int i) const { |
263 | 263 |
double val = _prob->getRowUpper()[i]; |
264 | 264 |
return val == COIN_DBL_MAX ? INF : val; |
265 | 265 |
} |
266 | 266 |
|
267 | 267 |
void ClpLp::_setObjCoeffs(ExprIterator b, ExprIterator e) { |
268 | 268 |
int num = _prob->clpMatrix()->getNumCols(); |
269 | 269 |
for (int i = 0; i < num; ++i) { |
270 | 270 |
_prob->setObjectiveCoefficient(i, 0.0); |
271 | 271 |
} |
272 | 272 |
for (ExprIterator it = b; it != e; ++it) { |
273 | 273 |
_prob->setObjectiveCoefficient(it->first, it->second); |
274 | 274 |
} |
275 | 275 |
} |
276 | 276 |
|
277 | 277 |
void ClpLp::_getObjCoeffs(InsertIterator b) const { |
278 | 278 |
int num = _prob->clpMatrix()->getNumCols(); |
279 | 279 |
for (int i = 0; i < num; ++i) { |
280 | 280 |
Value coef = _prob->getObjCoefficients()[i]; |
281 | 281 |
if (coef != 0.0) { |
282 | 282 |
*b = std::make_pair(i, coef); |
283 | 283 |
++b; |
284 | 284 |
} |
285 | 285 |
} |
286 | 286 |
} |
287 | 287 |
|
288 | 288 |
void ClpLp::_setObjCoeff(int i, Value obj_coef) { |
289 | 289 |
_prob->setObjectiveCoefficient(i, obj_coef); |
290 | 290 |
} |
291 | 291 |
|
292 | 292 |
ClpLp::Value ClpLp::_getObjCoeff(int i) const { |
293 | 293 |
return _prob->getObjCoefficients()[i]; |
294 | 294 |
} |
295 | 295 |
|
296 | 296 |
ClpLp::SolveExitStatus ClpLp::_solve() { |
297 | 297 |
return _prob->primal() >= 0 ? SOLVED : UNSOLVED; |
298 | 298 |
} |
299 | 299 |
|
300 | 300 |
ClpLp::SolveExitStatus ClpLp::solvePrimal() { |
301 | 301 |
return _prob->primal() >= 0 ? SOLVED : UNSOLVED; |
302 | 302 |
} |
303 | 303 |
|
304 | 304 |
ClpLp::SolveExitStatus ClpLp::solveDual() { |
305 | 305 |
return _prob->dual() >= 0 ? SOLVED : UNSOLVED; |
306 | 306 |
} |
307 | 307 |
|
308 | 308 |
ClpLp::SolveExitStatus ClpLp::solveBarrier() { |
309 | 309 |
return _prob->barrier() >= 0 ? SOLVED : UNSOLVED; |
310 | 310 |
} |
311 | 311 |
|
312 | 312 |
ClpLp::Value ClpLp::_getPrimal(int i) const { |
313 | 313 |
return _prob->primalColumnSolution()[i]; |
314 | 314 |
} |
315 | 315 |
ClpLp::Value ClpLp::_getPrimalValue() const { |
316 | 316 |
return _prob->objectiveValue(); |
317 | 317 |
} |
318 | 318 |
|
319 | 319 |
ClpLp::Value ClpLp::_getDual(int i) const { |
320 | 320 |
return _prob->dualRowSolution()[i]; |
321 | 321 |
} |
322 | 322 |
|
323 | 323 |
ClpLp::Value ClpLp::_getPrimalRay(int i) const { |
324 | 324 |
if (!_primal_ray) { |
325 | 325 |
_primal_ray = _prob->unboundedRay(); |
326 | 326 |
LEMON_ASSERT(_primal_ray != 0, "Primal ray is not provided"); |
327 | 327 |
} |
328 | 328 |
return _primal_ray[i]; |
329 | 329 |
} |
330 | 330 |
|
331 | 331 |
ClpLp::Value ClpLp::_getDualRay(int i) const { |
332 | 332 |
if (!_dual_ray) { |
333 | 333 |
_dual_ray = _prob->infeasibilityRay(); |
334 | 334 |
LEMON_ASSERT(_dual_ray != 0, "Dual ray is not provided"); |
335 | 335 |
} |
336 | 336 |
return _dual_ray[i]; |
337 | 337 |
} |
338 | 338 |
|
339 | 339 |
ClpLp::VarStatus ClpLp::_getColStatus(int i) const { |
340 | 340 |
switch (_prob->getColumnStatus(i)) { |
341 | 341 |
case ClpSimplex::basic: |
342 | 342 |
return BASIC; |
343 | 343 |
case ClpSimplex::isFree: |
344 | 344 |
return FREE; |
345 | 345 |
case ClpSimplex::atUpperBound: |
346 | 346 |
return UPPER; |
347 | 347 |
case ClpSimplex::atLowerBound: |
348 | 348 |
return LOWER; |
349 | 349 |
case ClpSimplex::isFixed: |
350 | 350 |
return FIXED; |
351 | 351 |
case ClpSimplex::superBasic: |
352 | 352 |
return FREE; |
353 | 353 |
default: |
354 | 354 |
LEMON_ASSERT(false, "Wrong column status"); |
355 | 355 |
return VarStatus(); |
356 | 356 |
} |
357 | 357 |
} |
358 | 358 |
|
359 | 359 |
ClpLp::VarStatus ClpLp::_getRowStatus(int i) const { |
360 | 360 |
switch (_prob->getColumnStatus(i)) { |
361 | 361 |
case ClpSimplex::basic: |
362 | 362 |
return BASIC; |
363 | 363 |
case ClpSimplex::isFree: |
364 | 364 |
return FREE; |
365 | 365 |
case ClpSimplex::atUpperBound: |
366 | 366 |
return UPPER; |
367 | 367 |
case ClpSimplex::atLowerBound: |
368 | 368 |
return LOWER; |
369 | 369 |
case ClpSimplex::isFixed: |
370 | 370 |
return FIXED; |
371 | 371 |
case ClpSimplex::superBasic: |
372 | 372 |
return FREE; |
373 | 373 |
default: |
374 | 374 |
LEMON_ASSERT(false, "Wrong row status"); |
375 | 375 |
return VarStatus(); |
376 | 376 |
} |
377 | 377 |
} |
378 | 378 |
|
379 | 379 |
|
380 | 380 |
ClpLp::ProblemType ClpLp::_getPrimalType() const { |
381 | 381 |
if (_prob->isProvenOptimal()) { |
382 | 382 |
return OPTIMAL; |
383 | 383 |
} else if (_prob->isProvenPrimalInfeasible()) { |
384 | 384 |
return INFEASIBLE; |
385 | 385 |
} else if (_prob->isProvenDualInfeasible()) { |
386 | 386 |
return UNBOUNDED; |
387 | 387 |
} else { |
388 | 388 |
return UNDEFINED; |
389 | 389 |
} |
390 | 390 |
} |
391 | 391 |
|
392 | 392 |
ClpLp::ProblemType ClpLp::_getDualType() const { |
393 | 393 |
if (_prob->isProvenOptimal()) { |
394 | 394 |
return OPTIMAL; |
395 | 395 |
} else if (_prob->isProvenDualInfeasible()) { |
396 | 396 |
return INFEASIBLE; |
397 | 397 |
} else if (_prob->isProvenPrimalInfeasible()) { |
398 | 398 |
return INFEASIBLE; |
399 | 399 |
} else { |
400 | 400 |
return UNDEFINED; |
401 | 401 |
} |
402 | 402 |
} |
403 | 403 |
|
404 | 404 |
void ClpLp::_setSense(ClpLp::Sense sense) { |
405 | 405 |
switch (sense) { |
406 | 406 |
case MIN: |
407 | 407 |
_prob->setOptimizationDirection(1); |
408 | 408 |
break; |
409 | 409 |
case MAX: |
410 | 410 |
_prob->setOptimizationDirection(-1); |
411 | 411 |
break; |
412 | 412 |
} |
413 | 413 |
} |
414 | 414 |
|
415 | 415 |
ClpLp::Sense ClpLp::_getSense() const { |
416 | 416 |
double dir = _prob->optimizationDirection(); |
417 | 417 |
if (dir > 0.0) { |
418 | 418 |
return MIN; |
419 | 419 |
} else { |
420 | 420 |
return MAX; |
421 | 421 |
} |
422 | 422 |
} |
423 | 423 |
|
424 | 424 |
void ClpLp::_clear() { |
425 | 425 |
delete _prob; |
426 | 426 |
_prob = new ClpSimplex(); |
427 | 427 |
rows.clear(); |
428 | 428 |
cols.clear(); |
429 | 429 |
_col_names_ref.clear(); |
430 | 430 |
_clear_temporals(); |
431 | 431 |
} |
432 | 432 |
|
433 | 433 |
void ClpLp::_messageLevel(MessageLevel level) { |
434 | 434 |
switch (level) { |
435 | 435 |
case MESSAGE_NOTHING: |
436 | 436 |
_prob->setLogLevel(0); |
437 | 437 |
break; |
438 | 438 |
case MESSAGE_ERROR: |
439 | 439 |
_prob->setLogLevel(1); |
440 | 440 |
break; |
441 | 441 |
case MESSAGE_WARNING: |
442 | 442 |
_prob->setLogLevel(2); |
443 | 443 |
break; |
444 | 444 |
case MESSAGE_NORMAL: |
445 | 445 |
_prob->setLogLevel(3); |
446 | 446 |
break; |
447 | 447 |
case MESSAGE_VERBOSE: |
448 | 448 |
_prob->setLogLevel(4); |
449 | 449 |
break; |
450 | 450 |
} |
451 | 451 |
} |
452 | 452 |
|
453 | 453 |
} //END OF NAMESPACE LEMON |
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(); |
70 | 70 |
void _clear_temporals(); |
71 | 71 |
|
72 | 72 |
protected: |
73 | 73 |
|
74 | 74 |
virtual const char* _solverName() const; |
75 | 75 |
|
76 | 76 |
virtual int _addCol(); |
77 | 77 |
virtual int _addRow(); |
78 | 78 |
|
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 |
70 | 70 |
/// as a base class of the node iterators, |
71 | 71 |
/// thus they will convert to this type. |
72 | 72 |
class Node { |
73 | 73 |
public: |
74 | 74 |
/// Default constructor |
75 | 75 |
|
76 | 76 |
/// @warning The default constructor sets the iterator |
77 | 77 |
/// to an undefined value. |
78 | 78 |
Node() { } |
79 | 79 |
/// Copy constructor. |
80 | 80 |
|
81 | 81 |
/// Copy constructor. |
82 | 82 |
/// |
83 | 83 |
Node(const Node&) { } |
84 | 84 |
|
85 | 85 |
/// Invalid constructor \& conversion. |
86 | 86 |
|
87 | 87 |
/// This constructor initializes the iterator to be invalid. |
88 | 88 |
/// \sa Invalid for more details. |
89 | 89 |
Node(Invalid) { } |
90 | 90 |
/// Equality operator |
91 | 91 |
|
92 | 92 |
/// Two iterators are equal if and only if they point to the |
93 | 93 |
/// same object or both are invalid. |
94 | 94 |
bool operator==(Node) const { return true; } |
95 | 95 |
|
96 | 96 |
/// Inequality operator |
97 | 97 |
|
98 | 98 |
/// \sa operator==(Node n) |
99 | 99 |
/// |
100 | 100 |
bool operator!=(Node) const { return true; } |
101 | 101 |
|
102 | 102 |
/// Artificial ordering operator. |
103 | 103 |
|
104 | 104 |
/// To allow the use of digraph descriptors as key type in std::map or |
105 | 105 |
/// similar associative container we require this. |
106 | 106 |
/// |
107 | 107 |
/// \note This operator only have to define some strict ordering of |
108 | 108 |
/// the items; this order has nothing to do with the iteration |
109 | 109 |
/// ordering of the items. |
110 | 110 |
bool operator<(Node) const { return false; } |
111 | 111 |
|
112 | 112 |
}; |
113 | 113 |
|
114 | 114 |
/// This iterator goes through each node. |
115 | 115 |
|
116 | 116 |
/// This iterator goes through each node. |
117 | 117 |
/// Its usage is quite simple, for example you can count the number |
118 | 118 |
/// of nodes in digraph \c g of type \c Digraph like this: |
119 | 119 |
///\code |
120 | 120 |
/// int count=0; |
121 | 121 |
/// for (Digraph::NodeIt n(g); n!=INVALID; ++n) ++count; |
122 | 122 |
///\endcode |
123 | 123 |
class NodeIt : public Node { |
124 | 124 |
public: |
125 | 125 |
/// Default constructor |
126 | 126 |
|
127 | 127 |
/// @warning The default constructor sets the iterator |
128 | 128 |
/// to an undefined value. |
129 | 129 |
NodeIt() { } |
130 | 130 |
/// Copy constructor. |
131 | 131 |
|
132 | 132 |
/// Copy constructor. |
133 | 133 |
/// |
134 | 134 |
NodeIt(const NodeIt& n) : Node(n) { } |
135 | 135 |
/// Invalid constructor \& conversion. |
136 | 136 |
|
137 | 137 |
/// Initialize the iterator to be invalid. |
138 | 138 |
/// \sa Invalid for more details. |
139 | 139 |
NodeIt(Invalid) { } |
140 | 140 |
/// Sets the iterator to the first node. |
141 | 141 |
|
142 | 142 |
/// Sets the iterator to the first node of \c g. |
143 | 143 |
/// |
144 | 144 |
NodeIt(const Digraph&) { } |
145 | 145 |
/// Node -> NodeIt conversion. |
146 | 146 |
|
147 | 147 |
/// Sets the iterator to the node of \c the digraph pointed by |
148 | 148 |
/// the trivial iterator. |
149 | 149 |
/// This feature necessitates that each time we |
150 | 150 |
/// iterate the arc-set, the iteration order is the same. |
151 | 151 |
NodeIt(const Digraph&, const Node&) { } |
152 | 152 |
/// Next node. |
153 | 153 |
|
154 | 154 |
/// Assign the iterator to the next node. |
155 | 155 |
/// |
156 | 156 |
NodeIt& operator++() { return *this; } |
157 | 157 |
}; |
158 | 158 |
|
159 | 159 |
|
160 | 160 |
/// Class for identifying an arc of the digraph |
161 | 161 |
|
162 | 162 |
/// This class identifies an arc of the digraph. It also serves |
163 | 163 |
/// as a base class of the arc iterators, |
164 | 164 |
/// thus they will convert to this type. |
165 | 165 |
class Arc { |
166 | 166 |
public: |
167 | 167 |
/// Default constructor |
168 | 168 |
|
169 | 169 |
/// @warning The default constructor sets the iterator |
170 | 170 |
/// to an undefined value. |
171 | 171 |
Arc() { } |
172 | 172 |
/// Copy constructor. |
173 | 173 |
|
174 | 174 |
/// Copy constructor. |
175 | 175 |
/// |
176 | 176 |
Arc(const Arc&) { } |
177 | 177 |
/// Initialize the iterator to be invalid. |
178 | 178 |
|
179 | 179 |
/// Initialize the iterator to be invalid. |
180 | 180 |
/// |
181 | 181 |
Arc(Invalid) { } |
182 | 182 |
/// Equality operator |
183 | 183 |
|
184 | 184 |
/// Two iterators are equal if and only if they point to the |
185 | 185 |
/// same object or both are invalid. |
186 | 186 |
bool operator==(Arc) const { return true; } |
187 | 187 |
/// Inequality operator |
188 | 188 |
|
189 | 189 |
/// \sa operator==(Arc n) |
190 | 190 |
/// |
191 | 191 |
bool operator!=(Arc) const { return true; } |
192 | 192 |
|
193 | 193 |
/// Artificial ordering operator. |
194 | 194 |
|
195 | 195 |
/// To allow the use of digraph descriptors as key type in std::map or |
196 | 196 |
/// similar associative container we require this. |
197 | 197 |
/// |
198 | 198 |
/// \note This operator only have to define some strict ordering of |
199 | 199 |
/// the items; this order has nothing to do with the iteration |
200 | 200 |
/// ordering of the items. |
201 | 201 |
bool operator<(Arc) const { return false; } |
202 | 202 |
}; |
203 | 203 |
|
204 | 204 |
/// This iterator goes trough the outgoing arcs of a node. |
205 | 205 |
|
206 | 206 |
/// This iterator goes trough the \e outgoing arcs of a certain node |
207 | 207 |
/// of a digraph. |
208 | 208 |
/// Its usage is quite simple, for example you can count the number |
209 | 209 |
/// of outgoing arcs of a node \c n |
210 | 210 |
/// in digraph \c g of type \c Digraph as follows. |
211 | 211 |
///\code |
212 | 212 |
/// int count=0; |
213 | 213 |
/// for (Digraph::OutArcIt e(g, n); e!=INVALID; ++e) ++count; |
214 | 214 |
///\endcode |
215 | 215 |
|
216 | 216 |
class OutArcIt : public Arc { |
217 | 217 |
public: |
218 | 218 |
/// Default constructor |
219 | 219 |
|
220 | 220 |
/// @warning The default constructor sets the iterator |
221 | 221 |
/// to an undefined value. |
222 | 222 |
OutArcIt() { } |
223 | 223 |
/// Copy constructor. |
224 | 224 |
|
225 | 225 |
/// Copy constructor. |
226 | 226 |
/// |
227 | 227 |
OutArcIt(const OutArcIt& e) : Arc(e) { } |
228 | 228 |
/// Initialize the iterator to be invalid. |
229 | 229 |
|
230 | 230 |
/// Initialize the iterator to be invalid. |
231 | 231 |
/// |
232 | 232 |
OutArcIt(Invalid) { } |
233 | 233 |
/// This constructor sets the iterator to the first outgoing arc. |
234 | 234 |
|
235 | 235 |
/// This constructor sets the iterator to the first outgoing arc of |
236 | 236 |
/// the node. |
237 | 237 |
OutArcIt(const Digraph&, const Node&) { } |
238 | 238 |
/// Arc -> OutArcIt conversion |
239 | 239 |
|
240 | 240 |
/// Sets the iterator to the value of the trivial iterator. |
241 | 241 |
/// This feature necessitates that each time we |
242 | 242 |
/// iterate the arc-set, the iteration order is the same. |
243 | 243 |
OutArcIt(const Digraph&, const Arc&) { } |
244 | 244 |
///Next outgoing arc |
245 | 245 |
|
246 | 246 |
/// Assign the iterator to the next |
247 | 247 |
/// outgoing arc of the corresponding node. |
248 | 248 |
OutArcIt& operator++() { return *this; } |
249 | 249 |
}; |
250 | 250 |
|
251 | 251 |
/// This iterator goes trough the incoming arcs of a node. |
252 | 252 |
|
253 | 253 |
/// This iterator goes trough the \e incoming arcs of a certain node |
254 | 254 |
/// of a digraph. |
255 | 255 |
/// Its usage is quite simple, for example you can count the number |
256 | 256 |
/// of outgoing arcs of a node \c n |
257 | 257 |
/// in digraph \c g of type \c Digraph as follows. |
258 | 258 |
///\code |
259 | 259 |
/// int count=0; |
260 | 260 |
/// for(Digraph::InArcIt e(g, n); e!=INVALID; ++e) ++count; |
261 | 261 |
///\endcode |
262 | 262 |
|
263 | 263 |
class InArcIt : public Arc { |
264 | 264 |
public: |
265 | 265 |
/// Default constructor |
266 | 266 |
|
267 | 267 |
/// @warning The default constructor sets the iterator |
268 | 268 |
/// to an undefined value. |
269 | 269 |
InArcIt() { } |
270 | 270 |
/// Copy constructor. |
271 | 271 |
|
272 | 272 |
/// Copy constructor. |
273 | 273 |
/// |
274 | 274 |
InArcIt(const InArcIt& e) : Arc(e) { } |
275 | 275 |
/// Initialize the iterator to be invalid. |
276 | 276 |
|
277 | 277 |
/// Initialize the iterator to be invalid. |
278 | 278 |
/// |
279 | 279 |
InArcIt(Invalid) { } |
280 | 280 |
/// This constructor sets the iterator to first incoming arc. |
281 | 281 |
|
282 | 282 |
/// This constructor set the iterator to the first incoming arc of |
283 | 283 |
/// the node. |
284 | 284 |
InArcIt(const Digraph&, const Node&) { } |
285 | 285 |
/// Arc -> InArcIt conversion |
286 | 286 |
|
287 | 287 |
/// Sets the iterator to the value of the trivial iterator \c e. |
288 | 288 |
/// This feature necessitates that each time we |
289 | 289 |
/// iterate the arc-set, the iteration order is the same. |
290 | 290 |
InArcIt(const Digraph&, const Arc&) { } |
291 | 291 |
/// Next incoming arc |
292 | 292 |
|
293 | 293 |
/// Assign the iterator to the next inarc of the corresponding node. |
294 | 294 |
/// |
295 | 295 |
InArcIt& operator++() { return *this; } |
296 | 296 |
}; |
297 | 297 |
/// This iterator goes through each arc. |
298 | 298 |
|
299 | 299 |
/// This iterator goes through each arc of a digraph. |
300 | 300 |
/// Its usage is quite simple, for example you can count the number |
301 | 301 |
/// of arcs in a digraph \c g of type \c Digraph as follows: |
302 | 302 |
///\code |
303 | 303 |
/// int count=0; |
304 | 304 |
/// for(Digraph::ArcIt e(g); e!=INVALID; ++e) ++count; |
305 | 305 |
///\endcode |
306 | 306 |
class ArcIt : public Arc { |
307 | 307 |
public: |
308 | 308 |
/// Default constructor |
309 | 309 |
|
310 | 310 |
/// @warning The default constructor sets the iterator |
311 | 311 |
/// to an undefined value. |
312 | 312 |
ArcIt() { } |
313 | 313 |
/// Copy constructor. |
314 | 314 |
|
315 | 315 |
/// Copy constructor. |
316 | 316 |
/// |
317 | 317 |
ArcIt(const ArcIt& e) : Arc(e) { } |
318 | 318 |
/// Initialize the iterator to be invalid. |
319 | 319 |
|
320 | 320 |
/// Initialize the iterator to be invalid. |
321 | 321 |
/// |
322 | 322 |
ArcIt(Invalid) { } |
323 | 323 |
/// This constructor sets the iterator to the first arc. |
324 | 324 |
|
325 | 325 |
/// This constructor sets the iterator to the first arc of \c g. |
326 | 326 |
///@param g the digraph |
327 | 327 |
ArcIt(const Digraph& g) { ignore_unused_variable_warning(g); } |
328 | 328 |
/// Arc -> ArcIt conversion |
329 | 329 |
|
330 | 330 |
/// Sets the iterator to the value of the trivial iterator \c e. |
331 | 331 |
/// This feature necessitates that each time we |
332 | 332 |
/// iterate the arc-set, the iteration order is the same. |
333 | 333 |
ArcIt(const Digraph&, const Arc&) { } |
334 | 334 |
///Next arc |
335 | 335 |
|
336 | 336 |
/// Assign the iterator to the next arc. |
337 | 337 |
ArcIt& operator++() { return *this; } |
338 | 338 |
}; |
339 | 339 |
///Gives back the target node of an arc. |
340 | 340 |
|
341 | 341 |
///Gives back the target node of an arc. |
342 | 342 |
/// |
343 | 343 |
Node target(Arc) const { return INVALID; } |
344 | 344 |
///Gives back the source node of an arc. |
345 | 345 |
|
346 | 346 |
///Gives back the source node of an arc. |
347 | 347 |
/// |
348 | 348 |
Node source(Arc) const { return INVALID; } |
349 | 349 |
|
350 | 350 |
/// \brief Returns the ID of the node. |
351 | 351 |
int id(Node) const { return -1; } |
352 | 352 |
|
353 | 353 |
/// \brief Returns the ID of the arc. |
354 | 354 |
int id(Arc) const { return -1; } |
355 | 355 |
|
356 | 356 |
/// \brief Returns the node with the given ID. |
357 | 357 |
/// |
358 | 358 |
/// \pre The argument should be a valid node ID in the graph. |
359 | 359 |
Node nodeFromId(int) const { return INVALID; } |
360 | 360 |
|
361 | 361 |
/// \brief Returns the arc with the given ID. |
362 | 362 |
/// |
363 | 363 |
/// \pre The argument should be a valid arc ID in the graph. |
364 | 364 |
Arc arcFromId(int) const { return INVALID; } |
365 | 365 |
|
366 | 366 |
/// \brief Returns an upper bound on the node IDs. |
367 | 367 |
int maxNodeId() const { return -1; } |
368 | 368 |
|
369 | 369 |
/// \brief Returns an upper bound on the arc IDs. |
370 | 370 |
int maxArcId() const { return -1; } |
371 | 371 |
|
372 | 372 |
void first(Node&) const {} |
373 | 373 |
void next(Node&) const {} |
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 | 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 | 41 |
/// and \c Arc (or \c Edge) types should \e not derive from the same |
42 | 42 |
/// base class. For \c Node you should instantiate it with character |
43 | 43 |
/// \c 'n', for \c Arc with \c 'a' and for \c Edge with \c 'e'. |
44 | 44 |
#ifndef DOXYGEN |
45 | 45 |
template <char sel = '0'> |
46 | 46 |
#endif |
47 | 47 |
class GraphItem { |
48 | 48 |
public: |
49 | 49 |
/// \brief Default constructor. |
50 | 50 |
/// |
51 | 51 |
/// Default constructor. |
52 | 52 |
/// \warning The default constructor is not required to set |
53 | 53 |
/// the item to some well-defined value. So you should consider it |
54 | 54 |
/// as uninitialized. |
55 | 55 |
GraphItem() {} |
56 | 56 |
|
57 | 57 |
/// \brief Copy constructor. |
58 | 58 |
/// |
59 | 59 |
/// Copy constructor. |
60 | 60 |
GraphItem(const GraphItem &) {} |
61 | 61 |
|
62 | 62 |
/// \brief Constructor for conversion from \c INVALID. |
63 | 63 |
/// |
64 | 64 |
/// Constructor for conversion from \c INVALID. |
65 | 65 |
/// It initializes the item to be invalid. |
66 | 66 |
/// \sa Invalid for more details. |
67 | 67 |
GraphItem(Invalid) {} |
68 | 68 |
|
69 | 69 |
/// \brief Assignment operator. |
70 | 70 |
/// |
71 | 71 |
/// Assignment operator for the item. |
72 | 72 |
GraphItem& operator=(const GraphItem&) { return *this; } |
73 | 73 |
|
74 | 74 |
/// \brief Assignment operator for INVALID. |
75 | 75 |
/// |
76 | 76 |
/// This operator makes the item invalid. |
77 | 77 |
GraphItem& operator=(Invalid) { return *this; } |
78 | 78 |
|
79 | 79 |
/// \brief Equality operator. |
80 | 80 |
/// |
81 | 81 |
/// Equality operator. |
82 | 82 |
bool operator==(const GraphItem&) const { return false; } |
83 | 83 |
|
84 | 84 |
/// \brief Inequality operator. |
85 | 85 |
/// |
86 | 86 |
/// Inequality operator. |
87 | 87 |
bool operator!=(const GraphItem&) const { return false; } |
88 | 88 |
|
89 | 89 |
/// \brief Ordering operator. |
90 | 90 |
/// |
91 | 91 |
/// This operator defines an ordering of the items. |
92 | 92 |
/// It makes possible to use graph item types as key types in |
93 | 93 |
/// associative containers (e.g. \c std::map). |
94 | 94 |
/// |
95 | 95 |
/// \note This operator only 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 | 125 |
/// It just provides types for nodes and arcs and functions |
126 | 126 |
/// to get the source and the target nodes of arcs. |
127 | 127 |
class BaseDigraphComponent { |
128 | 128 |
public: |
129 | 129 |
|
130 | 130 |
typedef BaseDigraphComponent Digraph; |
131 | 131 |
|
132 | 132 |
/// \brief Node class of the digraph. |
133 | 133 |
/// |
134 | 134 |
/// This class represents the nodes of the digraph. |
135 | 135 |
typedef GraphItem<'n'> Node; |
136 | 136 |
|
137 | 137 |
/// \brief Arc class of the digraph. |
138 | 138 |
/// |
139 | 139 |
/// This class represents the arcs of the digraph. |
140 | 140 |
typedef GraphItem<'a'> Arc; |
141 | 141 |
|
142 | 142 |
/// \brief Return the source node of an arc. |
143 | 143 |
/// |
144 | 144 |
/// This function returns the source node of an arc. |
145 | 145 |
Node source(const Arc&) const { return INVALID; } |
146 | 146 |
|
147 | 147 |
/// \brief Return the target node of an arc. |
148 | 148 |
/// |
149 | 149 |
/// This function returns the target node of an arc. |
150 | 150 |
Node target(const Arc&) const { return INVALID; } |
151 | 151 |
|
152 | 152 |
/// \brief Return the opposite node on the given arc. |
153 | 153 |
/// |
154 | 154 |
/// This function returns the opposite node on the given arc. |
155 | 155 |
Node oppositeNode(const Node&, const Arc&) const { |
156 | 156 |
return INVALID; |
157 | 157 |
} |
158 | 158 |
|
159 | 159 |
template <typename _Digraph> |
160 | 160 |
struct Constraints { |
161 | 161 |
typedef typename _Digraph::Node Node; |
162 | 162 |
typedef typename _Digraph::Arc Arc; |
163 | 163 |
|
164 | 164 |
void constraints() { |
165 | 165 |
checkConcept<GraphItem<'n'>, Node>(); |
166 | 166 |
checkConcept<GraphItem<'a'>, Arc>(); |
167 | 167 |
{ |
168 | 168 |
Node n; |
169 | 169 |
Arc e(INVALID); |
170 | 170 |
n = digraph.source(e); |
171 | 171 |
n = digraph.target(e); |
172 | 172 |
n = digraph.oppositeNode(n, e); |
173 | 173 |
} |
174 | 174 |
} |
175 | 175 |
|
176 | 176 |
const _Digraph& digraph; |
177 | 177 |
}; |
178 | 178 |
}; |
179 | 179 |
|
180 | 180 |
/// \brief Base skeleton class for undirected graphs. |
181 | 181 |
/// |
182 | 182 |
/// This class describes the base interface of undirected graph types. |
183 | 183 |
/// All graph %concepts have to conform to this class. |
184 | 184 |
/// It extends the interface of \ref BaseDigraphComponent with an |
185 | 185 |
/// \c Edge type and functions to get the end nodes of edges, |
186 | 186 |
/// to convert from arcs to edges and to get both direction of edges. |
187 | 187 |
class BaseGraphComponent : public BaseDigraphComponent { |
188 | 188 |
public: |
189 | 189 |
|
190 | 190 |
typedef BaseGraphComponent Graph; |
191 | 191 |
|
192 | 192 |
typedef BaseDigraphComponent::Node Node; |
193 | 193 |
typedef BaseDigraphComponent::Arc Arc; |
194 | 194 |
|
195 | 195 |
/// \brief Undirected edge class of the graph. |
196 | 196 |
/// |
197 | 197 |
/// This class represents the undirected edges of the graph. |
198 | 198 |
/// Undirected graphs can be used as directed graphs, each edge is |
199 | 199 |
/// represented by two opposite directed arcs. |
200 | 200 |
class Edge : public GraphItem<'e'> { |
201 | 201 |
typedef GraphItem<'e'> Parent; |
202 | 202 |
|
203 | 203 |
public: |
204 | 204 |
/// \brief Default constructor. |
205 | 205 |
/// |
206 | 206 |
/// Default constructor. |
207 | 207 |
/// \warning The default constructor is not required to set |
208 | 208 |
/// the item to some well-defined value. So you should consider it |
209 | 209 |
/// as uninitialized. |
210 | 210 |
Edge() {} |
211 | 211 |
|
212 | 212 |
/// \brief Copy constructor. |
213 | 213 |
/// |
214 | 214 |
/// Copy constructor. |
215 | 215 |
Edge(const Edge &) : Parent() {} |
216 | 216 |
|
217 | 217 |
/// \brief Constructor for conversion from \c INVALID. |
218 | 218 |
/// |
219 | 219 |
/// Constructor for conversion from \c INVALID. |
220 | 220 |
/// It initializes the item to be invalid. |
221 | 221 |
/// \sa Invalid for more details. |
222 | 222 |
Edge(Invalid) {} |
223 | 223 |
|
224 | 224 |
/// \brief Constructor for conversion from an arc. |
225 | 225 |
/// |
226 | 226 |
/// Constructor for conversion from an arc. |
227 | 227 |
/// Besides the core graph item functionality each arc should |
228 | 228 |
/// be convertible to the represented edge. |
229 | 229 |
Edge(const Arc&) {} |
230 | 230 |
}; |
231 | 231 |
|
232 | 232 |
/// \brief Return one end node of an edge. |
233 | 233 |
/// |
234 | 234 |
/// This function returns one end node of an edge. |
235 | 235 |
Node u(const Edge&) const { return INVALID; } |
236 | 236 |
|
237 | 237 |
/// \brief Return the other end node of an edge. |
238 | 238 |
/// |
239 | 239 |
/// This function returns the other end node of an edge. |
240 | 240 |
Node v(const Edge&) const { return INVALID; } |
241 | 241 |
|
242 | 242 |
/// \brief Return a directed arc related to an edge. |
243 | 243 |
/// |
244 | 244 |
/// This function returns a directed arc from its direction and the |
245 | 245 |
/// represented edge. |
246 | 246 |
Arc direct(const Edge&, bool) const { return INVALID; } |
247 | 247 |
|
248 | 248 |
/// \brief Return a directed arc related to an edge. |
249 | 249 |
/// |
250 | 250 |
/// This function returns a directed arc from its source node and the |
251 | 251 |
/// represented edge. |
252 | 252 |
Arc direct(const Edge&, const Node&) const { return INVALID; } |
253 | 253 |
|
254 | 254 |
/// \brief Return the direction of the arc. |
255 | 255 |
/// |
256 | 256 |
/// Returns the direction of the arc. Each arc represents an |
257 | 257 |
/// edge with a direction. It gives back the |
258 | 258 |
/// direction. |
259 | 259 |
bool direction(const Arc&) const { return true; } |
260 | 260 |
|
261 | 261 |
/// \brief Return the opposite arc. |
262 | 262 |
/// |
263 | 263 |
/// This function returns the opposite arc, i.e. the arc representing |
264 | 264 |
/// the same edge and has opposite direction. |
265 | 265 |
Arc oppositeArc(const Arc&) const { return INVALID; } |
266 | 266 |
|
267 | 267 |
template <typename _Graph> |
268 | 268 |
struct Constraints { |
269 | 269 |
typedef typename _Graph::Node Node; |
270 | 270 |
typedef typename _Graph::Arc Arc; |
271 | 271 |
typedef typename _Graph::Edge Edge; |
272 | 272 |
|
273 | 273 |
void constraints() { |
274 | 274 |
checkConcept<BaseDigraphComponent, _Graph>(); |
275 | 275 |
checkConcept<GraphItem<'e'>, Edge>(); |
276 | 276 |
{ |
277 | 277 |
Node n; |
278 | 278 |
Edge ue(INVALID); |
279 | 279 |
Arc e; |
280 | 280 |
n = graph.u(ue); |
281 | 281 |
n = graph.v(ue); |
282 | 282 |
e = graph.direct(ue, true); |
283 | 283 |
e = graph.direct(ue, false); |
284 | 284 |
e = graph.direct(ue, n); |
285 | 285 |
e = graph.oppositeArc(e); |
286 | 286 |
ue = e; |
287 | 287 |
bool d = graph.direction(e); |
288 | 288 |
ignore_unused_variable_warning(d); |
289 | 289 |
} |
290 | 290 |
} |
291 | 291 |
|
292 | 292 |
const _Graph& graph; |
293 | 293 |
}; |
294 | 294 |
|
295 | 295 |
}; |
296 | 296 |
|
297 | 297 |
/// \brief Skeleton class for \e idable directed graphs. |
298 | 298 |
/// |
299 | 299 |
/// This class describes the interface of \e idable directed graphs. |
300 | 300 |
/// It extends \ref BaseDigraphComponent with the core ID functions. |
301 | 301 |
/// The ids of the items must be unique and immutable. |
302 | 302 |
/// This concept is part of the Digraph concept. |
303 | 303 |
template <typename BAS = BaseDigraphComponent> |
304 | 304 |
class IDableDigraphComponent : public BAS { |
305 | 305 |
public: |
306 | 306 |
|
307 | 307 |
typedef BAS Base; |
308 | 308 |
typedef typename Base::Node Node; |
309 | 309 |
typedef typename Base::Arc Arc; |
310 | 310 |
|
311 | 311 |
/// \brief Return a unique integer id for the given node. |
312 | 312 |
/// |
313 | 313 |
/// This function returns a unique integer id for the given node. |
314 | 314 |
int id(const Node&) const { return -1; } |
315 | 315 |
|
316 | 316 |
/// \brief Return the node by its unique id. |
317 | 317 |
/// |
318 | 318 |
/// This function returns the node by its unique id. |
319 | 319 |
/// If the digraph does not contain a node with the given id, |
320 | 320 |
/// then the result of the function is undefined. |
321 | 321 |
Node nodeFromId(int) const { return INVALID; } |
322 | 322 |
|
323 | 323 |
/// \brief Return a unique integer id for the given arc. |
324 | 324 |
/// |
325 | 325 |
/// This function returns a unique integer id for the given arc. |
326 | 326 |
int id(const Arc&) const { return -1; } |
327 | 327 |
|
328 | 328 |
/// \brief Return the arc by its unique id. |
329 | 329 |
/// |
330 | 330 |
/// This function returns the arc by its unique id. |
331 | 331 |
/// If the digraph does not contain an arc with the given id, |
332 | 332 |
/// then the result of the function is undefined. |
333 | 333 |
Arc arcFromId(int) const { return INVALID; } |
334 | 334 |
|
335 | 335 |
/// \brief Return an integer greater or equal to the maximum |
336 | 336 |
/// node id. |
337 | 337 |
/// |
338 | 338 |
/// This function returns an integer greater or equal to the |
339 | 339 |
/// maximum node id. |
340 | 340 |
int maxNodeId() const { return -1; } |
341 | 341 |
|
342 | 342 |
/// \brief Return an integer greater or equal to the maximum |
343 | 343 |
/// arc id. |
344 | 344 |
/// |
345 | 345 |
/// This function returns an integer greater or equal to the |
346 | 346 |
/// maximum arc id. |
347 | 347 |
int maxArcId() const { return -1; } |
348 | 348 |
|
349 | 349 |
template <typename _Digraph> |
350 | 350 |
struct Constraints { |
351 | 351 |
|
352 | 352 |
void constraints() { |
353 | 353 |
checkConcept<Base, _Digraph >(); |
354 | 354 |
typename _Digraph::Node node; |
355 | 355 |
node=INVALID; |
356 | 356 |
int nid = digraph.id(node); |
357 | 357 |
nid = digraph.id(node); |
358 | 358 |
node = digraph.nodeFromId(nid); |
359 | 359 |
typename _Digraph::Arc arc; |
360 | 360 |
arc=INVALID; |
361 | 361 |
int eid = digraph.id(arc); |
362 | 362 |
eid = digraph.id(arc); |
363 | 363 |
arc = digraph.arcFromId(eid); |
364 | 364 |
|
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 | 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 | 504 |
/// \brief Concept class for \c InArcIt, \c OutArcIt and |
505 | 505 |
/// \c IncEdgeIt types. |
506 | 506 |
/// |
507 | 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 | 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 | 533 |
/// \brief Constructor that sets the iterator to the first |
534 | 534 |
/// incoming or outgoing arc. |
535 | 535 |
/// |
536 | 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 |
|
601 | 601 |
public: |
602 | 602 |
|
603 | 603 |
typedef BAS Base; |
604 | 604 |
typedef typename Base::Node Node; |
605 | 605 |
typedef typename Base::Arc Arc; |
606 | 606 |
|
607 | 607 |
typedef IterableDigraphComponent Digraph; |
608 | 608 |
|
609 | 609 |
/// \name Base Iteration |
610 | 610 |
/// |
611 | 611 |
/// This interface provides functions for iteration on digraph items. |
612 | 612 |
/// |
613 | 613 |
/// @{ |
614 | 614 |
|
615 | 615 |
/// \brief Return the first node. |
616 | 616 |
/// |
617 | 617 |
/// This function gives back the first node in the iteration order. |
618 | 618 |
void first(Node&) const {} |
619 | 619 |
|
620 | 620 |
/// \brief Return the next node. |
621 | 621 |
/// |
622 | 622 |
/// This function gives back the next node in the iteration order. |
623 | 623 |
void next(Node&) const {} |
624 | 624 |
|
625 | 625 |
/// \brief Return the first arc. |
626 | 626 |
/// |
627 | 627 |
/// This function gives back the first arc in the iteration order. |
628 | 628 |
void first(Arc&) const {} |
629 | 629 |
|
630 | 630 |
/// \brief Return the next arc. |
631 | 631 |
/// |
632 | 632 |
/// This function gives back the next arc in the iteration order. |
633 | 633 |
void next(Arc&) const {} |
634 | 634 |
|
635 | 635 |
/// \brief Return the first arc incomming to the given node. |
636 | 636 |
/// |
637 | 637 |
/// This function gives back the first arc incomming to the |
638 | 638 |
/// given node. |
639 | 639 |
void firstIn(Arc&, const Node&) const {} |
640 | 640 |
|
641 | 641 |
/// \brief Return the next arc incomming to the given node. |
642 | 642 |
/// |
643 | 643 |
/// This function gives back the next arc incomming to the |
644 | 644 |
/// given node. |
645 | 645 |
void nextIn(Arc&) const {} |
646 | 646 |
|
647 | 647 |
/// \brief Return the first arc outgoing form the given node. |
648 | 648 |
/// |
649 | 649 |
/// This function gives back the first arc outgoing form the |
650 | 650 |
/// given node. |
651 | 651 |
void firstOut(Arc&, const Node&) const {} |
652 | 652 |
|
653 | 653 |
/// \brief Return the next arc outgoing form the given node. |
654 | 654 |
/// |
655 | 655 |
/// This function gives back the next arc outgoing form the |
656 | 656 |
/// given node. |
657 | 657 |
void nextOut(Arc&) const {} |
658 | 658 |
|
659 | 659 |
/// @} |
660 | 660 |
|
661 | 661 |
/// \name Class Based Iteration |
662 | 662 |
/// |
663 | 663 |
/// This interface provides iterator classes for digraph items. |
664 | 664 |
/// |
665 | 665 |
/// @{ |
666 | 666 |
|
667 | 667 |
/// \brief This iterator goes through each node. |
668 | 668 |
/// |
669 | 669 |
/// This iterator goes through each node. |
670 | 670 |
/// |
671 | 671 |
typedef GraphItemIt<Digraph, Node> NodeIt; |
672 | 672 |
|
673 | 673 |
/// \brief This iterator goes through each arc. |
674 | 674 |
/// |
675 | 675 |
/// This iterator goes through each arc. |
676 | 676 |
/// |
677 | 677 |
typedef GraphItemIt<Digraph, Arc> ArcIt; |
678 | 678 |
|
679 | 679 |
/// \brief This iterator goes trough the incoming arcs of a node. |
680 | 680 |
/// |
681 | 681 |
/// This iterator goes trough the \e incoming arcs of a certain node |
682 | 682 |
/// of a digraph. |
683 | 683 |
typedef GraphIncIt<Digraph, Arc, Node, 'i'> InArcIt; |
684 | 684 |
|
685 | 685 |
/// \brief This iterator goes trough the outgoing arcs of a node. |
686 | 686 |
/// |
687 | 687 |
/// This iterator goes trough the \e outgoing arcs of a certain node |
688 | 688 |
/// of a digraph. |
689 | 689 |
typedef GraphIncIt<Digraph, Arc, Node, 'o'> OutArcIt; |
690 | 690 |
|
691 | 691 |
/// \brief The base node of the iterator. |
692 | 692 |
/// |
693 | 693 |
/// This function gives back the base node of the iterator. |
694 | 694 |
/// It is always the target node of the pointed arc. |
695 | 695 |
Node baseNode(const InArcIt&) const { return INVALID; } |
696 | 696 |
|
697 | 697 |
/// \brief The running node of the iterator. |
698 | 698 |
/// |
699 | 699 |
/// This function gives back the running node of the iterator. |
700 | 700 |
/// It is always the source node of the pointed arc. |
701 | 701 |
Node runningNode(const InArcIt&) const { return INVALID; } |
702 | 702 |
|
703 | 703 |
/// \brief The base node of the iterator. |
704 | 704 |
/// |
705 | 705 |
/// This function gives back the base node of the iterator. |
706 | 706 |
/// It is always the source node of the pointed arc. |
707 | 707 |
Node baseNode(const OutArcIt&) const { return INVALID; } |
708 | 708 |
|
709 | 709 |
/// \brief The running node of the iterator. |
710 | 710 |
/// |
711 | 711 |
/// This function gives back the running node of the iterator. |
712 | 712 |
/// It is always the target node of the pointed arc. |
713 | 713 |
Node runningNode(const OutArcIt&) const { return INVALID; } |
714 | 714 |
|
715 | 715 |
/// @} |
716 | 716 |
|
717 | 717 |
template <typename _Digraph> |
718 | 718 |
struct Constraints { |
719 | 719 |
void constraints() { |
720 | 720 |
checkConcept<Base, _Digraph>(); |
721 | 721 |
|
722 | 722 |
{ |
723 | 723 |
typename _Digraph::Node node(INVALID); |
724 | 724 |
typename _Digraph::Arc arc(INVALID); |
725 | 725 |
{ |
726 | 726 |
digraph.first(node); |
727 | 727 |
digraph.next(node); |
728 | 728 |
} |
729 | 729 |
{ |
730 | 730 |
digraph.first(arc); |
731 | 731 |
digraph.next(arc); |
732 | 732 |
} |
733 | 733 |
{ |
734 | 734 |
digraph.firstIn(arc, node); |
735 | 735 |
digraph.nextIn(arc); |
736 | 736 |
} |
737 | 737 |
{ |
738 | 738 |
digraph.firstOut(arc, node); |
739 | 739 |
digraph.nextOut(arc); |
740 | 740 |
} |
741 | 741 |
} |
742 | 742 |
|
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. |
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; |
70 | 70 |
const _ReadMap& m; |
71 | 71 |
}; |
72 | 72 |
|
73 | 73 |
}; |
74 | 74 |
|
75 | 75 |
|
76 | 76 |
/// Writable map concept |
77 | 77 |
|
78 | 78 |
/// Writable map concept. |
79 | 79 |
/// |
80 | 80 |
template<typename K, typename T> |
81 | 81 |
class WriteMap |
82 | 82 |
{ |
83 | 83 |
public: |
84 | 84 |
/// The key type of the map. |
85 | 85 |
typedef K Key; |
86 | 86 |
/// \brief The value type of the map. |
87 | 87 |
/// (The type of objects associated with the keys). |
88 | 88 |
typedef T Value; |
89 | 89 |
|
90 | 90 |
/// Sets the value associated with the given key. |
91 | 91 |
void set(const Key &, const Value &) {} |
92 | 92 |
|
93 | 93 |
/// Default constructor. |
94 | 94 |
WriteMap() {} |
95 | 95 |
|
96 | 96 |
template <typename _WriteMap> |
97 | 97 |
struct Constraints { |
98 | 98 |
void constraints() { |
99 | 99 |
m.set(key, val); |
100 | 100 |
m.set(own_key, own_val); |
101 | 101 |
|
102 | 102 |
ignore_unused_variable_warning(key); |
103 | 103 |
ignore_unused_variable_warning(val); |
104 | 104 |
ignore_unused_variable_warning(own_key); |
105 | 105 |
ignore_unused_variable_warning(own_val); |
106 | 106 |
} |
107 | 107 |
const Key& key; |
108 | 108 |
const Value& val; |
109 | 109 |
const typename _WriteMap::Key& own_key; |
110 | 110 |
const typename _WriteMap::Value& own_val; |
111 | 111 |
_WriteMap& m; |
112 | 112 |
}; |
113 | 113 |
}; |
114 | 114 |
|
115 | 115 |
/// Read/writable map concept |
116 | 116 |
|
117 | 117 |
/// Read/writable map concept. |
118 | 118 |
/// |
119 | 119 |
template<typename K, typename T> |
120 | 120 |
class ReadWriteMap : public ReadMap<K,T>, |
121 | 121 |
public WriteMap<K,T> |
122 | 122 |
{ |
123 | 123 |
public: |
124 | 124 |
/// The key type of the map. |
125 | 125 |
typedef K Key; |
126 | 126 |
/// \brief The value type of the map. |
127 | 127 |
/// (The type of objects associated with the keys). |
128 | 128 |
typedef T Value; |
129 | 129 |
|
130 | 130 |
/// Returns the value associated with the given key. |
131 | 131 |
Value operator[](const Key &) const { |
132 | 132 |
return *static_cast<Value *>(0); |
133 | 133 |
} |
134 | 134 |
|
135 | 135 |
/// Sets the value associated with the given key. |
136 | 136 |
void set(const Key &, const Value &) {} |
137 | 137 |
|
138 | 138 |
template<typename _ReadWriteMap> |
139 | 139 |
struct Constraints { |
140 | 140 |
void constraints() { |
141 | 141 |
checkConcept<ReadMap<K, T>, _ReadWriteMap >(); |
142 | 142 |
checkConcept<WriteMap<K, T>, _ReadWriteMap >(); |
143 | 143 |
} |
144 | 144 |
}; |
145 | 145 |
}; |
146 | 146 |
|
147 | 147 |
|
148 | 148 |
/// Dereferable map concept |
149 | 149 |
|
150 | 150 |
/// Dereferable map concept. |
151 | 151 |
/// |
152 | 152 |
template<typename K, typename T, typename R, typename CR> |
153 | 153 |
class ReferenceMap : public ReadWriteMap<K,T> |
154 | 154 |
{ |
155 | 155 |
public: |
156 | 156 |
/// Tag for reference maps. |
157 | 157 |
typedef True ReferenceMapTag; |
158 | 158 |
/// The key type of the map. |
159 | 159 |
typedef K Key; |
160 | 160 |
/// \brief The value type of the map. |
161 | 161 |
/// (The type of objects associated with the keys). |
162 | 162 |
typedef T Value; |
163 | 163 |
/// The reference type of the map. |
164 | 164 |
typedef R Reference; |
165 | 165 |
/// The const reference type of the map. |
166 | 166 |
typedef CR ConstReference; |
167 | 167 |
|
168 | 168 |
public: |
169 | 169 |
|
170 | 170 |
/// Returns a reference to the value associated with the given key. |
171 | 171 |
Reference operator[](const Key &) { |
172 | 172 |
return *static_cast<Value *>(0); |
173 | 173 |
} |
174 | 174 |
|
175 | 175 |
/// Returns a const reference to the value associated with the given key. |
176 | 176 |
ConstReference operator[](const Key &) const { |
177 | 177 |
return *static_cast<Value *>(0); |
178 | 178 |
} |
179 | 179 |
|
180 | 180 |
/// Sets the value associated with the given key. |
181 | 181 |
void set(const Key &k,const Value &t) { operator[](k)=t; } |
182 | 182 |
|
183 | 183 |
template<typename _ReferenceMap> |
184 | 184 |
struct Constraints { |
185 | 185 |
typename enable_if<typename _ReferenceMap::ReferenceMapTag, void>::type |
186 | 186 |
constraints() { |
187 | 187 |
checkConcept<ReadWriteMap<K, T>, _ReferenceMap >(); |
188 | 188 |
ref = m[key]; |
189 | 189 |
m[key] = val; |
190 | 190 |
m[key] = ref; |
191 | 191 |
m[key] = cref; |
192 | 192 |
own_ref = m[own_key]; |
193 | 193 |
m[own_key] = own_val; |
194 | 194 |
m[own_key] = own_ref; |
195 | 195 |
m[own_key] = own_cref; |
196 | 196 |
m[key] = m[own_key]; |
197 | 197 |
m[own_key] = m[key]; |
198 | 198 |
} |
199 | 199 |
const Key& key; |
200 | 200 |
Value& val; |
201 | 201 |
Reference ref; |
202 | 202 |
ConstReference cref; |
203 | 203 |
const typename _ReferenceMap::Key& own_key; |
204 | 204 |
typename _ReferenceMap::Value& own_val; |
205 | 205 |
typename _ReferenceMap::Reference own_ref; |
206 | 206 |
typename _ReferenceMap::ConstReference own_cref; |
207 | 207 |
_ReferenceMap& m; |
208 | 208 |
}; |
209 | 209 |
}; |
210 | 210 |
|
211 | 211 |
// @} |
212 | 212 |
|
213 | 213 |
} //namespace concepts |
214 | 214 |
|
215 | 215 |
} //namespace lemon |
216 | 216 |
|
217 | 217 |
#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_CONNECTIVITY_H |
20 | 20 |
#define LEMON_CONNECTIVITY_H |
21 | 21 |
|
22 | 22 |
#include <lemon/dfs.h> |
23 | 23 |
#include <lemon/bfs.h> |
24 | 24 |
#include <lemon/core.h> |
25 | 25 |
#include <lemon/maps.h> |
26 | 26 |
#include <lemon/adaptors.h> |
27 | 27 |
|
28 | 28 |
#include <lemon/concepts/digraph.h> |
29 | 29 |
#include <lemon/concepts/graph.h> |
30 | 30 |
#include <lemon/concept_check.h> |
31 | 31 |
|
32 | 32 |
#include <stack> |
33 | 33 |
#include <functional> |
34 | 34 |
|
35 | 35 |
/// \ingroup graph_properties |
36 | 36 |
/// \file |
37 | 37 |
/// \brief Connectivity algorithms |
38 | 38 |
/// |
39 | 39 |
/// Connectivity algorithms |
40 | 40 |
|
41 | 41 |
namespace lemon { |
42 | 42 |
|
43 | 43 |
/// \ingroup graph_properties |
44 | 44 |
/// |
45 | 45 |
/// \brief Check whether an undirected graph is connected. |
46 | 46 |
/// |
47 | 47 |
/// This function checks whether the given undirected graph is connected, |
48 | 48 |
/// i.e. there is a path between any two nodes in the graph. |
49 | 49 |
/// |
50 | 50 |
/// \return \c true if the graph is connected. |
51 | 51 |
/// \note By definition, the empty graph is connected. |
52 | 52 |
/// |
53 | 53 |
/// \see countConnectedComponents(), connectedComponents() |
54 | 54 |
/// \see stronglyConnected() |
55 | 55 |
template <typename Graph> |
56 | 56 |
bool connected(const Graph& graph) { |
57 | 57 |
checkConcept<concepts::Graph, Graph>(); |
58 | 58 |
typedef typename Graph::NodeIt NodeIt; |
59 | 59 |
if (NodeIt(graph) == INVALID) return true; |
60 | 60 |
Dfs<Graph> dfs(graph); |
61 | 61 |
dfs.run(NodeIt(graph)); |
62 | 62 |
for (NodeIt it(graph); it != INVALID; ++it) { |
63 | 63 |
if (!dfs.reached(it)) { |
64 | 64 |
return false; |
65 | 65 |
} |
66 | 66 |
} |
67 | 67 |
return true; |
68 | 68 |
} |
69 | 69 |
|
70 | 70 |
/// \ingroup graph_properties |
71 | 71 |
/// |
72 | 72 |
/// \brief Count the number of connected components of an undirected graph |
73 | 73 |
/// |
74 | 74 |
/// This function counts the number of connected components of the given |
75 | 75 |
/// undirected graph. |
76 | 76 |
/// |
77 | 77 |
/// The connected components are the classes of an equivalence relation |
78 | 78 |
/// on the nodes of an undirected graph. Two nodes are in the same class |
79 | 79 |
/// if they are connected with a path. |
80 | 80 |
/// |
81 | 81 |
/// \return The number of connected components. |
82 | 82 |
/// \note By definition, the empty graph consists |
83 | 83 |
/// of zero connected components. |
84 | 84 |
/// |
85 | 85 |
/// \see connected(), connectedComponents() |
86 | 86 |
template <typename Graph> |
87 | 87 |
int countConnectedComponents(const Graph &graph) { |
88 | 88 |
checkConcept<concepts::Graph, Graph>(); |
89 | 89 |
typedef typename Graph::Node Node; |
90 | 90 |
typedef typename Graph::Arc Arc; |
91 | 91 |
|
92 | 92 |
typedef NullMap<Node, Arc> PredMap; |
93 | 93 |
typedef NullMap<Node, int> DistMap; |
94 | 94 |
|
95 | 95 |
int compNum = 0; |
96 | 96 |
typename Bfs<Graph>:: |
97 | 97 |
template SetPredMap<PredMap>:: |
98 | 98 |
template SetDistMap<DistMap>:: |
99 | 99 |
Create bfs(graph); |
100 | 100 |
|
101 | 101 |
PredMap predMap; |
102 | 102 |
bfs.predMap(predMap); |
103 | 103 |
|
104 | 104 |
DistMap distMap; |
105 | 105 |
bfs.distMap(distMap); |
106 | 106 |
|
107 | 107 |
bfs.init(); |
108 | 108 |
for(typename Graph::NodeIt n(graph); n != INVALID; ++n) { |
109 | 109 |
if (!bfs.reached(n)) { |
110 | 110 |
bfs.addSource(n); |
111 | 111 |
bfs.start(); |
112 | 112 |
++compNum; |
113 | 113 |
} |
114 | 114 |
} |
115 | 115 |
return compNum; |
116 | 116 |
} |
117 | 117 |
|
118 | 118 |
/// \ingroup graph_properties |
119 | 119 |
/// |
120 | 120 |
/// \brief Find the connected components of an undirected graph |
121 | 121 |
/// |
122 | 122 |
/// This function finds the connected components of the given undirected |
123 | 123 |
/// graph. |
124 | 124 |
/// |
125 | 125 |
/// The connected components are the classes of an equivalence relation |
126 | 126 |
/// on the nodes of an undirected graph. Two nodes are in the same class |
127 | 127 |
/// if they are connected with a path. |
128 | 128 |
/// |
129 | 129 |
/// \image html connected_components.png |
130 | 130 |
/// \image latex connected_components.eps "Connected components" width=\textwidth |
131 | 131 |
/// |
132 | 132 |
/// \param graph The undirected graph. |
133 | 133 |
/// \retval compMap A writable node map. The values will be set from 0 to |
134 | 134 |
/// the number of the connected components minus one. Each value of the map |
135 | 135 |
/// will be set exactly once, and the values of a certain component will be |
136 | 136 |
/// set continuously. |
137 | 137 |
/// \return The number of connected components. |
138 | 138 |
/// \note By definition, the empty graph consists |
139 | 139 |
/// of zero connected components. |
140 | 140 |
/// |
141 | 141 |
/// \see connected(), countConnectedComponents() |
142 | 142 |
template <class Graph, class NodeMap> |
143 | 143 |
int connectedComponents(const Graph &graph, NodeMap &compMap) { |
144 | 144 |
checkConcept<concepts::Graph, Graph>(); |
145 | 145 |
typedef typename Graph::Node Node; |
146 | 146 |
typedef typename Graph::Arc Arc; |
147 | 147 |
checkConcept<concepts::WriteMap<Node, int>, NodeMap>(); |
148 | 148 |
|
149 | 149 |
typedef NullMap<Node, Arc> PredMap; |
150 | 150 |
typedef NullMap<Node, int> DistMap; |
151 | 151 |
|
152 | 152 |
int compNum = 0; |
153 | 153 |
typename Bfs<Graph>:: |
154 | 154 |
template SetPredMap<PredMap>:: |
155 | 155 |
template SetDistMap<DistMap>:: |
156 | 156 |
Create bfs(graph); |
157 | 157 |
|
158 | 158 |
PredMap predMap; |
159 | 159 |
bfs.predMap(predMap); |
160 | 160 |
|
161 | 161 |
DistMap distMap; |
162 | 162 |
bfs.distMap(distMap); |
163 | 163 |
|
164 | 164 |
bfs.init(); |
165 | 165 |
for(typename Graph::NodeIt n(graph); n != INVALID; ++n) { |
166 | 166 |
if(!bfs.reached(n)) { |
167 | 167 |
bfs.addSource(n); |
168 | 168 |
while (!bfs.emptyQueue()) { |
169 | 169 |
compMap.set(bfs.nextNode(), compNum); |
170 | 170 |
bfs.processNextNode(); |
171 | 171 |
} |
172 | 172 |
++compNum; |
173 | 173 |
} |
174 | 174 |
} |
175 | 175 |
return compNum; |
176 | 176 |
} |
177 | 177 |
|
178 | 178 |
namespace _connectivity_bits { |
179 | 179 |
|
180 | 180 |
template <typename Digraph, typename Iterator > |
181 | 181 |
struct LeaveOrderVisitor : public DfsVisitor<Digraph> { |
182 | 182 |
public: |
183 | 183 |
typedef typename Digraph::Node Node; |
184 | 184 |
LeaveOrderVisitor(Iterator it) : _it(it) {} |
185 | 185 |
|
186 | 186 |
void leave(const Node& node) { |
187 | 187 |
*(_it++) = node; |
188 | 188 |
} |
189 | 189 |
|
190 | 190 |
private: |
191 | 191 |
Iterator _it; |
192 | 192 |
}; |
193 | 193 |
|
194 | 194 |
template <typename Digraph, typename Map> |
195 | 195 |
struct FillMapVisitor : public DfsVisitor<Digraph> { |
196 | 196 |
public: |
197 | 197 |
typedef typename Digraph::Node Node; |
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 | 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 |
} |
378 | 378 |
|
379 | 379 |
/// \ingroup graph_properties |
380 | 380 |
/// |
381 | 381 |
/// \brief Find the strongly connected components of a directed graph |
382 | 382 |
/// |
383 | 383 |
/// This function finds the strongly connected components of the given |
384 | 384 |
/// directed graph. In addition, the numbering of the components will |
385 | 385 |
/// satisfy that there is no arc going from a higher numbered component |
386 | 386 |
/// to a lower one (i.e. it provides a topological order of the components). |
387 | 387 |
/// |
388 | 388 |
/// The strongly connected components are the classes of an |
389 | 389 |
/// equivalence relation on the nodes of a digraph. Two nodes are in |
390 | 390 |
/// the same class if they are connected with directed paths in both |
391 | 391 |
/// direction. |
392 | 392 |
/// |
393 | 393 |
/// \image html strongly_connected_components.png |
394 | 394 |
/// \image latex strongly_connected_components.eps "Strongly connected components" width=\textwidth |
395 | 395 |
/// |
396 | 396 |
/// \param digraph The digraph. |
397 | 397 |
/// \retval compMap A writable node map. The values will be set from 0 to |
398 | 398 |
/// the number of the strongly connected components minus one. Each value |
399 | 399 |
/// of the map will be set exactly once, and the values of a certain |
400 | 400 |
/// component will be set continuously. |
401 | 401 |
/// \return The number of strongly connected components. |
402 | 402 |
/// \note By definition, the empty digraph has zero |
403 | 403 |
/// strongly connected components. |
404 | 404 |
/// |
405 | 405 |
/// \see stronglyConnected(), countStronglyConnectedComponents() |
406 | 406 |
template <typename Digraph, typename NodeMap> |
407 | 407 |
int stronglyConnectedComponents(const Digraph& digraph, NodeMap& compMap) { |
408 | 408 |
checkConcept<concepts::Digraph, Digraph>(); |
409 | 409 |
typedef typename Digraph::Node Node; |
410 | 410 |
typedef typename Digraph::NodeIt NodeIt; |
411 | 411 |
checkConcept<concepts::WriteMap<Node, int>, NodeMap>(); |
412 | 412 |
|
413 | 413 |
using namespace _connectivity_bits; |
414 | 414 |
|
415 | 415 |
typedef std::vector<Node> Container; |
416 | 416 |
typedef typename Container::iterator Iterator; |
417 | 417 |
|
418 | 418 |
Container nodes(countNodes(digraph)); |
419 | 419 |
typedef LeaveOrderVisitor<Digraph, Iterator> Visitor; |
420 | 420 |
Visitor visitor(nodes.begin()); |
421 | 421 |
|
422 | 422 |
DfsVisit<Digraph, Visitor> dfs(digraph, visitor); |
423 | 423 |
dfs.init(); |
424 | 424 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
425 | 425 |
if (!dfs.reached(it)) { |
426 | 426 |
dfs.addSource(it); |
427 | 427 |
dfs.start(); |
428 | 428 |
} |
429 | 429 |
} |
430 | 430 |
|
431 | 431 |
typedef typename Container::reverse_iterator RIterator; |
432 | 432 |
typedef ReverseDigraph<const Digraph> RDigraph; |
433 | 433 |
|
434 | 434 |
RDigraph rdigraph(digraph); |
435 | 435 |
|
436 | 436 |
int compNum = 0; |
437 | 437 |
|
438 | 438 |
typedef FillMapVisitor<RDigraph, NodeMap> RVisitor; |
439 | 439 |
RVisitor rvisitor(compMap, compNum); |
440 | 440 |
|
441 | 441 |
DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor); |
442 | 442 |
|
443 | 443 |
rdfs.init(); |
444 | 444 |
for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) { |
445 | 445 |
if (!rdfs.reached(*it)) { |
446 | 446 |
rdfs.addSource(*it); |
447 | 447 |
rdfs.start(); |
448 | 448 |
++compNum; |
449 | 449 |
} |
450 | 450 |
} |
451 | 451 |
return compNum; |
452 | 452 |
} |
453 | 453 |
|
454 | 454 |
/// \ingroup graph_properties |
455 | 455 |
/// |
456 | 456 |
/// \brief Find the cut arcs of the strongly connected components. |
457 | 457 |
/// |
458 | 458 |
/// This function finds the cut arcs of the strongly connected components |
459 | 459 |
/// of the given digraph. |
460 | 460 |
/// |
461 | 461 |
/// The strongly connected components are the classes of an |
462 | 462 |
/// equivalence relation on the nodes of a digraph. Two nodes are in |
463 | 463 |
/// the same class if they are connected with directed paths in both |
464 | 464 |
/// direction. |
465 | 465 |
/// The strongly connected components are separated by the cut arcs. |
466 | 466 |
/// |
467 | 467 |
/// \param digraph The digraph. |
468 | 468 |
/// \retval cutMap A writable arc map. The values will be set to \c true |
469 | 469 |
/// for the cut arcs (exactly once for each cut arc), and will not be |
470 | 470 |
/// changed for other arcs. |
471 | 471 |
/// \return The number of cut arcs. |
472 | 472 |
/// |
473 | 473 |
/// \see stronglyConnected(), stronglyConnectedComponents() |
474 | 474 |
template <typename Digraph, typename ArcMap> |
475 | 475 |
int stronglyConnectedCutArcs(const Digraph& digraph, ArcMap& cutMap) { |
476 | 476 |
checkConcept<concepts::Digraph, Digraph>(); |
477 | 477 |
typedef typename Digraph::Node Node; |
478 | 478 |
typedef typename Digraph::Arc Arc; |
479 | 479 |
typedef typename Digraph::NodeIt NodeIt; |
480 | 480 |
checkConcept<concepts::WriteMap<Arc, bool>, ArcMap>(); |
481 | 481 |
|
482 | 482 |
using namespace _connectivity_bits; |
483 | 483 |
|
484 | 484 |
typedef std::vector<Node> Container; |
485 | 485 |
typedef typename Container::iterator Iterator; |
486 | 486 |
|
487 | 487 |
Container nodes(countNodes(digraph)); |
488 | 488 |
typedef LeaveOrderVisitor<Digraph, Iterator> Visitor; |
489 | 489 |
Visitor visitor(nodes.begin()); |
490 | 490 |
|
491 | 491 |
DfsVisit<Digraph, Visitor> dfs(digraph, visitor); |
492 | 492 |
dfs.init(); |
493 | 493 |
for (NodeIt it(digraph); it != INVALID; ++it) { |
494 | 494 |
if (!dfs.reached(it)) { |
495 | 495 |
dfs.addSource(it); |
496 | 496 |
dfs.start(); |
497 | 497 |
} |
498 | 498 |
} |
499 | 499 |
|
500 | 500 |
typedef typename Container::reverse_iterator RIterator; |
501 | 501 |
typedef ReverseDigraph<const Digraph> RDigraph; |
502 | 502 |
|
503 | 503 |
RDigraph rdigraph(digraph); |
504 | 504 |
|
505 | 505 |
int cutNum = 0; |
506 | 506 |
|
507 | 507 |
typedef StronglyConnectedCutArcsVisitor<RDigraph, ArcMap> RVisitor; |
508 | 508 |
RVisitor rvisitor(rdigraph, cutMap, cutNum); |
509 | 509 |
|
510 | 510 |
DfsVisit<RDigraph, RVisitor> rdfs(rdigraph, rvisitor); |
511 | 511 |
|
512 | 512 |
rdfs.init(); |
513 | 513 |
for (RIterator it = nodes.rbegin(); it != nodes.rend(); ++it) { |
514 | 514 |
if (!rdfs.reached(*it)) { |
515 | 515 |
rdfs.addSource(*it); |
516 | 516 |
rdfs.start(); |
517 | 517 |
} |
518 | 518 |
} |
519 | 519 |
return cutNum; |
520 | 520 |
} |
521 | 521 |
|
522 | 522 |
namespace _connectivity_bits { |
523 | 523 |
|
524 | 524 |
template <typename Digraph> |
525 | 525 |
class CountBiNodeConnectedComponentsVisitor : public DfsVisitor<Digraph> { |
526 | 526 |
public: |
527 | 527 |
typedef typename Digraph::Node Node; |
528 | 528 |
typedef typename Digraph::Arc Arc; |
529 | 529 |
typedef typename Digraph::Edge Edge; |
530 | 530 |
|
531 | 531 |
CountBiNodeConnectedComponentsVisitor(const Digraph& graph, int &compNum) |
532 | 532 |
: _graph(graph), _compNum(compNum), |
533 | 533 |
_numMap(graph), _retMap(graph), _predMap(graph), _num(0) {} |
534 | 534 |
|
535 | 535 |
void start(const Node& node) { |
536 | 536 |
_predMap.set(node, INVALID); |
537 | 537 |
} |
538 | 538 |
|
539 | 539 |
void reach(const Node& node) { |
540 | 540 |
_numMap.set(node, _num); |
541 | 541 |
_retMap.set(node, _num); |
542 | 542 |
++_num; |
543 | 543 |
} |
544 | 544 |
|
545 | 545 |
void discover(const Arc& edge) { |
546 | 546 |
_predMap.set(_graph.target(edge), _graph.source(edge)); |
547 | 547 |
} |
548 | 548 |
|
549 | 549 |
void examine(const Arc& edge) { |
550 | 550 |
if (_graph.source(edge) == _graph.target(edge) && |
551 | 551 |
_graph.direction(edge)) { |
552 | 552 |
++_compNum; |
553 | 553 |
return; |
554 | 554 |
} |
555 | 555 |
if (_predMap[_graph.source(edge)] == _graph.target(edge)) { |
556 | 556 |
return; |
557 | 557 |
} |
558 | 558 |
if (_retMap[_graph.source(edge)] > _numMap[_graph.target(edge)]) { |
559 | 559 |
_retMap.set(_graph.source(edge), _numMap[_graph.target(edge)]); |
560 | 560 |
} |
561 | 561 |
} |
562 | 562 |
|
563 | 563 |
void backtrack(const Arc& edge) { |
564 | 564 |
if (_retMap[_graph.source(edge)] > _retMap[_graph.target(edge)]) { |
565 | 565 |
_retMap.set(_graph.source(edge), _retMap[_graph.target(edge)]); |
566 | 566 |
} |
567 | 567 |
if (_numMap[_graph.source(edge)] <= _retMap[_graph.target(edge)]) { |
568 | 568 |
++_compNum; |
569 | 569 |
} |
570 | 570 |
} |
571 | 571 |
|
572 | 572 |
private: |
573 | 573 |
const Digraph& _graph; |
574 | 574 |
int& _compNum; |
575 | 575 |
|
576 | 576 |
typename Digraph::template NodeMap<int> _numMap; |
577 | 577 |
typename Digraph::template NodeMap<int> _retMap; |
578 | 578 |
typename Digraph::template NodeMap<Node> _predMap; |
579 | 579 |
int _num; |
580 | 580 |
}; |
581 | 581 |
|
582 | 582 |
template <typename Digraph, typename ArcMap> |
583 | 583 |
class BiNodeConnectedComponentsVisitor : public DfsVisitor<Digraph> { |
584 | 584 |
public: |
585 | 585 |
typedef typename Digraph::Node Node; |
586 | 586 |
typedef typename Digraph::Arc Arc; |
587 | 587 |
typedef typename Digraph::Edge Edge; |
588 | 588 |
|
589 | 589 |
BiNodeConnectedComponentsVisitor(const Digraph& graph, |
590 | 590 |
ArcMap& compMap, int &compNum) |
591 | 591 |
: _graph(graph), _compMap(compMap), _compNum(compNum), |
592 | 592 |
_numMap(graph), _retMap(graph), _predMap(graph), _num(0) {} |
593 | 593 |
|
594 | 594 |
void start(const Node& node) { |
595 | 595 |
_predMap.set(node, INVALID); |
596 | 596 |
} |
597 | 597 |
|
598 | 598 |
void reach(const Node& node) { |
599 | 599 |
_numMap.set(node, _num); |
600 | 600 |
_retMap.set(node, _num); |
601 | 601 |
++_num; |
602 | 602 |
} |
603 | 603 |
|
604 | 604 |
void discover(const Arc& edge) { |
605 | 605 |
Node target = _graph.target(edge); |
606 | 606 |
_predMap.set(target, edge); |
607 | 607 |
_edgeStack.push(edge); |
608 | 608 |
} |
609 | 609 |
|
610 | 610 |
void examine(const Arc& edge) { |
611 | 611 |
Node source = _graph.source(edge); |
612 | 612 |
Node target = _graph.target(edge); |
613 | 613 |
if (source == target && _graph.direction(edge)) { |
614 | 614 |
_compMap.set(edge, _compNum); |
615 | 615 |
++_compNum; |
616 | 616 |
return; |
617 | 617 |
} |
618 | 618 |
if (_numMap[target] < _numMap[source]) { |
619 | 619 |
if (_predMap[source] != _graph.oppositeArc(edge)) { |
620 | 620 |
_edgeStack.push(edge); |
621 | 621 |
} |
622 | 622 |
} |
623 | 623 |
if (_predMap[source] != INVALID && |
624 | 624 |
target == _graph.source(_predMap[source])) { |
625 | 625 |
return; |
626 | 626 |
} |
627 | 627 |
if (_retMap[source] > _numMap[target]) { |
628 | 628 |
_retMap.set(source, _numMap[target]); |
629 | 629 |
} |
630 | 630 |
} |
631 | 631 |
|
632 | 632 |
void backtrack(const Arc& edge) { |
633 | 633 |
Node source = _graph.source(edge); |
634 | 634 |
Node target = _graph.target(edge); |
635 | 635 |
if (_retMap[source] > _retMap[target]) { |
636 | 636 |
_retMap.set(source, _retMap[target]); |
637 | 637 |
} |
638 | 638 |
if (_numMap[source] <= _retMap[target]) { |
639 | 639 |
while (_edgeStack.top() != edge) { |
640 | 640 |
_compMap.set(_edgeStack.top(), _compNum); |
641 | 641 |
_edgeStack.pop(); |
642 | 642 |
} |
643 | 643 |
_compMap.set(edge, _compNum); |
644 | 644 |
_edgeStack.pop(); |
645 | 645 |
++_compNum; |
646 | 646 |
} |
647 | 647 |
} |
648 | 648 |
|
649 | 649 |
private: |
650 | 650 |
const Digraph& _graph; |
651 | 651 |
ArcMap& _compMap; |
652 | 652 |
int& _compNum; |
653 | 653 |
|
654 | 654 |
typename Digraph::template NodeMap<int> _numMap; |
655 | 655 |
typename Digraph::template NodeMap<int> _retMap; |
656 | 656 |
typename Digraph::template NodeMap<Arc> _predMap; |
657 | 657 |
std::stack<Edge> _edgeStack; |
658 | 658 |
int _num; |
659 | 659 |
}; |
660 | 660 |
|
661 | 661 |
|
662 | 662 |
template <typename Digraph, typename NodeMap> |
663 | 663 |
class BiNodeConnectedCutNodesVisitor : public DfsVisitor<Digraph> { |
664 | 664 |
public: |
665 | 665 |
typedef typename Digraph::Node Node; |
666 | 666 |
typedef typename Digraph::Arc Arc; |
667 | 667 |
typedef typename Digraph::Edge Edge; |
668 | 668 |
|
669 | 669 |
BiNodeConnectedCutNodesVisitor(const Digraph& graph, NodeMap& cutMap, |
670 | 670 |
int& cutNum) |
671 | 671 |
: _graph(graph), _cutMap(cutMap), _cutNum(cutNum), |
672 | 672 |
_numMap(graph), _retMap(graph), _predMap(graph), _num(0) {} |
673 | 673 |
|
674 | 674 |
void start(const Node& node) { |
675 | 675 |
_predMap.set(node, INVALID); |
676 | 676 |
rootCut = false; |
677 | 677 |
} |
678 | 678 |
|
679 | 679 |
void reach(const Node& node) { |
680 | 680 |
_numMap.set(node, _num); |
681 | 681 |
_retMap.set(node, _num); |
682 | 682 |
++_num; |
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 | 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 | 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() |
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)