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 |
/** |
20 | 20 |
\dir demo |
21 | 21 |
\brief A collection of demo applications. |
22 | 22 |
|
23 | 23 |
This directory contains several simple demo applications, mainly |
24 | 24 |
for educational purposes. |
25 | 25 |
*/ |
26 | 26 |
|
27 | 27 |
/** |
28 | 28 |
\dir doc |
29 | 29 |
\brief Auxiliary (and the whole generated) documentation. |
30 | 30 |
|
31 | 31 |
This directory contains some auxiliary pages and the whole generated |
32 | 32 |
documentation. |
33 | 33 |
*/ |
34 | 34 |
|
35 | 35 |
/** |
36 | 36 |
\dir test |
37 | 37 |
\brief Test programs. |
38 | 38 |
|
39 | 39 |
This directory contains several test programs that check the consistency |
40 | 40 |
of the code. |
41 | 41 |
*/ |
42 | 42 |
|
43 | 43 |
/** |
44 | 44 |
\dir tools |
45 | 45 |
\brief Some useful executables. |
46 | 46 |
|
47 | 47 |
This directory contains the sources of some useful complete executables. |
48 | 48 |
*/ |
49 | 49 |
|
50 | 50 |
/** |
51 | 51 |
\dir lemon |
52 | 52 |
\brief Base include directory of LEMON. |
53 | 53 |
|
54 | 54 |
This is the base directory of LEMON includes, so each include file must be |
55 | 55 |
prefixed with this, e.g. |
56 | 56 |
\code |
57 | 57 |
#include<lemon/list_graph.h> |
58 | 58 |
#include<lemon/dijkstra.h> |
59 | 59 |
\endcode |
60 | 60 |
*/ |
61 | 61 |
|
62 | 62 |
/** |
63 | 63 |
\dir concepts |
64 | 64 |
\brief Concept descriptors and checking classes. |
65 | 65 |
|
66 | 66 |
This directory contains the concept descriptors and concept checking tools. |
67 | 67 |
For more information see the \ref concept "Concepts" module. |
68 | 68 |
*/ |
69 | 69 |
|
70 | 70 |
/** |
71 | 71 |
\dir bits |
72 | 72 |
\brief Auxiliary tools for implementation. |
73 | 73 |
|
74 |
This directory contains some auxiliary classes for implementing graphs, |
|
74 |
This directory contains some auxiliary classes for implementing graphs, |
|
75 | 75 |
maps and some other classes. |
76 | 76 |
As a user you typically don't have to deal with these files. |
77 | 77 |
*/ |
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 |
/** |
20 | 20 |
@defgroup datas Data Structures |
21 | 21 |
This group describes the several data structures implemented in LEMON. |
22 | 22 |
*/ |
23 | 23 |
|
24 | 24 |
/** |
25 | 25 |
@defgroup graphs Graph Structures |
26 | 26 |
@ingroup datas |
27 | 27 |
\brief Graph structures implemented in LEMON. |
28 | 28 |
|
29 | 29 |
The implementation of combinatorial algorithms heavily relies on |
30 | 30 |
efficient graph implementations. LEMON offers data structures which are |
31 | 31 |
planned to be easily used in an experimental phase of implementation studies, |
32 | 32 |
and thereafter the program code can be made efficient by small modifications. |
33 | 33 |
|
34 | 34 |
The most efficient implementation of diverse applications require the |
35 | 35 |
usage of different physical graph implementations. These differences |
36 | 36 |
appear in the size of graph we require to handle, memory or time usage |
37 | 37 |
limitations or in the set of operations through which the graph can be |
38 | 38 |
accessed. LEMON provides several physical graph structures to meet |
39 | 39 |
the diverging requirements of the possible users. In order to save on |
40 | 40 |
running time or on memory usage, some structures may fail to provide |
41 | 41 |
some graph features like arc/edge or node deletion. |
42 | 42 |
|
43 | 43 |
You are free to use the graph structure that fit your requirements |
44 | 44 |
the best, most graph algorithms and auxiliary data structures can be used |
45 | 45 |
with any graph structure. |
46 | 46 |
|
47 | 47 |
<b>See also:</b> \ref graph_concepts "Graph Structure Concepts". |
48 | 48 |
*/ |
49 | 49 |
|
50 | 50 |
/** |
51 | 51 |
@defgroup maps Maps |
52 | 52 |
@ingroup datas |
53 | 53 |
\brief Map structures implemented in LEMON. |
54 | 54 |
|
55 | 55 |
This group describes the map structures implemented in LEMON. |
56 | 56 |
|
57 | 57 |
LEMON provides several special purpose maps and map adaptors that e.g. combine |
58 | 58 |
new maps from existing ones. |
59 | 59 |
|
60 | 60 |
<b>See also:</b> \ref map_concepts "Map Concepts". |
61 | 61 |
*/ |
62 | 62 |
|
63 | 63 |
/** |
64 | 64 |
@defgroup graph_maps Graph Maps |
65 | 65 |
@ingroup maps |
66 | 66 |
\brief Special graph-related maps. |
67 | 67 |
|
68 | 68 |
This group describes maps that are specifically designed to assign |
69 | 69 |
values to the nodes and arcs of graphs. |
70 | 70 |
*/ |
71 | 71 |
|
72 | 72 |
/** |
73 | 73 |
\defgroup map_adaptors Map Adaptors |
74 | 74 |
\ingroup maps |
75 | 75 |
\brief Tools to create new maps from existing ones |
76 | 76 |
|
77 | 77 |
This group describes map adaptors that are used to create "implicit" |
78 | 78 |
maps from other maps. |
79 | 79 |
|
80 | 80 |
Most of them are \ref lemon::concepts::ReadMap "read-only maps". |
81 | 81 |
They can make arithmetic and logical operations between one or two maps |
82 | 82 |
(negation, shifting, addition, multiplication, logical 'and', 'or', |
83 | 83 |
'not' etc.) or e.g. convert a map to another one of different Value type. |
84 | 84 |
|
85 | 85 |
The typical usage of this classes is passing implicit maps to |
86 | 86 |
algorithms. If a function type algorithm is called then the function |
87 | 87 |
type map adaptors can be used comfortable. For example let's see the |
88 | 88 |
usage of map adaptors with the \c graphToEps() function. |
89 | 89 |
\code |
90 | 90 |
Color nodeColor(int deg) { |
91 | 91 |
if (deg >= 2) { |
92 | 92 |
return Color(0.5, 0.0, 0.5); |
93 | 93 |
} else if (deg == 1) { |
94 | 94 |
return Color(1.0, 0.5, 1.0); |
95 | 95 |
} else { |
96 | 96 |
return Color(0.0, 0.0, 0.0); |
97 | 97 |
} |
98 | 98 |
} |
99 | 99 |
|
100 | 100 |
Digraph::NodeMap<int> degree_map(graph); |
101 | 101 |
|
102 | 102 |
graphToEps(graph, "graph.eps") |
103 | 103 |
.coords(coords).scaleToA4().undirected() |
104 | 104 |
.nodeColors(composeMap(functorToMap(nodeColor), degree_map)) |
105 | 105 |
.run(); |
106 | 106 |
\endcode |
107 | 107 |
The \c functorToMap() function makes an \c int to \c Color map from the |
108 | 108 |
\c nodeColor() function. The \c composeMap() compose the \c degree_map |
109 | 109 |
and the previously created map. The composed map is a proper function to |
110 | 110 |
get the color of each node. |
111 | 111 |
|
112 | 112 |
The usage with class type algorithms is little bit harder. In this |
113 | 113 |
case the function type map adaptors can not be used, because the |
114 | 114 |
function map adaptors give back temporary objects. |
115 | 115 |
\code |
116 | 116 |
Digraph graph; |
117 | 117 |
|
118 | 118 |
typedef Digraph::ArcMap<double> DoubleArcMap; |
119 | 119 |
DoubleArcMap length(graph); |
120 | 120 |
DoubleArcMap speed(graph); |
121 | 121 |
|
122 | 122 |
typedef DivMap<DoubleArcMap, DoubleArcMap> TimeMap; |
123 | 123 |
TimeMap time(length, speed); |
124 | 124 |
|
125 | 125 |
Dijkstra<Digraph, TimeMap> dijkstra(graph, time); |
126 | 126 |
dijkstra.run(source, target); |
127 | 127 |
\endcode |
128 | 128 |
We have a length map and a maximum speed map on the arcs of a digraph. |
129 | 129 |
The minimum time to pass the arc can be calculated as the division of |
130 | 130 |
the two maps which can be done implicitly with the \c DivMap template |
131 | 131 |
class. We use the implicit minimum time map as the length map of the |
132 | 132 |
\c Dijkstra algorithm. |
133 | 133 |
*/ |
134 | 134 |
|
135 | 135 |
/** |
136 | 136 |
@defgroup paths Path Structures |
137 | 137 |
@ingroup datas |
138 | 138 |
\brief %Path structures implemented in LEMON. |
139 | 139 |
|
140 | 140 |
This group describes the path structures implemented in LEMON. |
141 | 141 |
|
142 | 142 |
LEMON provides flexible data structures to work with paths. |
143 | 143 |
All of them have similar interfaces and they can be copied easily with |
144 | 144 |
assignment operators and copy constructors. This makes it easy and |
145 | 145 |
efficient to have e.g. the Dijkstra algorithm to store its result in |
146 | 146 |
any kind of path structure. |
147 | 147 |
|
148 | 148 |
\sa lemon::concepts::Path |
149 | 149 |
*/ |
150 | 150 |
|
151 | 151 |
/** |
152 | 152 |
@defgroup auxdat Auxiliary Data Structures |
153 | 153 |
@ingroup datas |
154 | 154 |
\brief Auxiliary data structures implemented in LEMON. |
155 | 155 |
|
156 | 156 |
This group describes some data structures implemented in LEMON in |
157 | 157 |
order to make it easier to implement combinatorial algorithms. |
158 | 158 |
*/ |
159 | 159 |
|
160 | 160 |
/** |
161 | 161 |
@defgroup algs Algorithms |
162 | 162 |
\brief This group describes the several algorithms |
163 | 163 |
implemented in LEMON. |
164 | 164 |
|
165 | 165 |
This group describes the several algorithms |
166 | 166 |
implemented in LEMON. |
167 | 167 |
*/ |
168 | 168 |
|
169 | 169 |
/** |
170 | 170 |
@defgroup search Graph Search |
171 | 171 |
@ingroup algs |
172 | 172 |
\brief Common graph search algorithms. |
173 | 173 |
|
174 | 174 |
This group describes the common graph search algorithms like |
175 | 175 |
Breadth-First Search (BFS) and Depth-First Search (DFS). |
176 | 176 |
*/ |
177 | 177 |
|
178 | 178 |
/** |
179 | 179 |
@defgroup shortest_path Shortest Path Algorithms |
180 | 180 |
@ingroup algs |
181 | 181 |
\brief Algorithms for finding shortest paths. |
182 | 182 |
|
183 | 183 |
This group describes the algorithms for finding shortest paths in graphs. |
184 | 184 |
*/ |
185 | 185 |
|
186 | 186 |
/** |
187 | 187 |
@defgroup spantree Minimum Spanning Tree Algorithms |
188 | 188 |
@ingroup algs |
189 | 189 |
\brief Algorithms for finding a minimum cost spanning tree in a graph. |
190 | 190 |
|
191 | 191 |
This group describes the algorithms for finding a minimum cost spanning |
192 | 192 |
tree in a graph |
193 | 193 |
*/ |
194 | 194 |
|
195 | 195 |
/** |
196 | 196 |
@defgroup utils Tools and Utilities |
197 | 197 |
\brief Tools and utilities for programming in LEMON |
198 | 198 |
|
199 | 199 |
Tools and utilities for programming in LEMON. |
200 | 200 |
*/ |
201 | 201 |
|
202 | 202 |
/** |
203 | 203 |
@defgroup gutils Basic Graph Utilities |
204 | 204 |
@ingroup utils |
205 | 205 |
\brief Simple basic graph utilities. |
206 | 206 |
|
207 | 207 |
This group describes some simple basic graph utilities. |
208 | 208 |
*/ |
209 | 209 |
|
210 | 210 |
/** |
211 | 211 |
@defgroup misc Miscellaneous Tools |
212 | 212 |
@ingroup utils |
213 | 213 |
\brief Tools for development, debugging and testing. |
214 | 214 |
|
215 | 215 |
This group describes several useful tools for development, |
216 | 216 |
debugging and testing. |
217 | 217 |
*/ |
218 | 218 |
|
219 | 219 |
/** |
220 | 220 |
@defgroup timecount Time Measuring and Counting |
221 | 221 |
@ingroup misc |
222 | 222 |
\brief Simple tools for measuring the performance of algorithms. |
223 | 223 |
|
224 | 224 |
This group describes simple tools for measuring the performance |
225 | 225 |
of algorithms. |
226 | 226 |
*/ |
227 | 227 |
|
228 | 228 |
/** |
229 | 229 |
@defgroup exceptions Exceptions |
230 | 230 |
@ingroup utils |
231 | 231 |
\brief Exceptions defined in LEMON. |
232 | 232 |
|
233 | 233 |
This group describes the exceptions defined in LEMON. |
234 | 234 |
*/ |
235 | 235 |
|
236 | 236 |
/** |
237 | 237 |
@defgroup io_group Input-Output |
238 | 238 |
\brief Graph Input-Output methods |
239 | 239 |
|
240 | 240 |
This group describes the tools for importing and exporting graphs |
241 | 241 |
and graph related data. Now it supports the LEMON format |
242 | 242 |
and the encapsulated postscript (EPS) format. |
243 | 243 |
postscript (EPS) format. |
244 | 244 |
*/ |
245 | 245 |
|
246 | 246 |
/** |
247 | 247 |
@defgroup lemon_io LEMON Input-Output |
248 | 248 |
@ingroup io_group |
249 | 249 |
\brief Reading and writing LEMON Graph Format. |
250 | 250 |
|
251 | 251 |
This group describes methods for reading and writing |
252 | 252 |
\ref lgf-format "LEMON Graph Format". |
253 | 253 |
*/ |
254 | 254 |
|
255 | 255 |
/** |
256 | 256 |
@defgroup eps_io Postscript Exporting |
257 | 257 |
@ingroup io_group |
258 | 258 |
\brief General \c EPS drawer and graph exporter |
259 | 259 |
|
260 | 260 |
This group describes general \c EPS drawing methods and special |
261 | 261 |
graph exporting tools. |
262 | 262 |
*/ |
263 | 263 |
|
264 | 264 |
/** |
265 | 265 |
@defgroup concept Concepts |
266 | 266 |
\brief Skeleton classes and concept checking classes |
267 | 267 |
|
268 | 268 |
This group describes the data/algorithm skeletons and concept checking |
269 | 269 |
classes implemented in LEMON. |
270 | 270 |
|
271 | 271 |
The purpose of the classes in this group is fourfold. |
272 | 272 |
|
273 | 273 |
- These classes contain the documentations of the %concepts. In order |
274 | 274 |
to avoid document multiplications, an implementation of a concept |
275 | 275 |
simply refers to the corresponding concept class. |
276 | 276 |
|
277 | 277 |
- These classes declare every functions, <tt>typedef</tt>s etc. an |
278 | 278 |
implementation of the %concepts should provide, however completely |
279 | 279 |
without implementations and real data structures behind the |
280 | 280 |
interface. On the other hand they should provide nothing else. All |
281 | 281 |
the algorithms working on a data structure meeting a certain concept |
282 | 282 |
should compile with these classes. (Though it will not run properly, |
283 | 283 |
of course.) In this way it is easily to check if an algorithm |
284 | 284 |
doesn't use any extra feature of a certain implementation. |
285 | 285 |
|
286 | 286 |
- The concept descriptor classes also provide a <em>checker class</em> |
287 | 287 |
that makes it possible to check whether a certain implementation of a |
288 | 288 |
concept indeed provides all the required features. |
289 | 289 |
|
290 | 290 |
- Finally, They can serve as a skeleton of a new implementation of a concept. |
291 | 291 |
*/ |
292 | 292 |
|
293 | 293 |
/** |
294 | 294 |
@defgroup graph_concepts Graph Structure Concepts |
295 | 295 |
@ingroup concept |
296 | 296 |
\brief Skeleton and concept checking classes for graph structures |
297 | 297 |
|
298 | 298 |
This group describes the skeletons and concept checking classes of LEMON's |
299 | 299 |
graph structures and helper classes used to implement these. |
300 | 300 |
*/ |
301 | 301 |
|
302 | 302 |
/** |
303 | 303 |
@defgroup map_concepts Map Concepts |
304 | 304 |
@ingroup concept |
305 | 305 |
\brief Skeleton and concept checking classes for maps |
306 |
|
|
306 |
|
|
307 | 307 |
This group describes the skeletons and concept checking classes of maps. |
308 | 308 |
*/ |
309 | 309 |
|
310 | 310 |
/** |
311 | 311 |
\anchor demoprograms |
312 | 312 |
|
313 | 313 |
@defgroup demos Demo programs |
314 | 314 |
|
315 | 315 |
Some demo programs are listed here. Their full source codes can be found in |
316 | 316 |
the \c demo subdirectory of the source tree. |
317 | 317 |
|
318 | 318 |
It order to compile them, use <tt>--enable-demo</tt> configure option when |
319 | 319 |
build the library. |
320 | 320 |
*/ |
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 |
///\file |
20 | 20 |
///\brief Some basic non-inline functions and static global data. |
21 | 21 |
|
22 | 22 |
#include<lemon/tolerance.h> |
23 | 23 |
#include<lemon/core.h> |
24 | 24 |
namespace lemon { |
25 | 25 |
|
26 | 26 |
float Tolerance<float>::def_epsilon = static_cast<float>(1e-4); |
27 | 27 |
double Tolerance<double>::def_epsilon = 1e-10; |
28 | 28 |
long double Tolerance<long double>::def_epsilon = 1e-14; |
29 | 29 |
|
30 | 30 |
#ifndef LEMON_ONLY_TEMPLATES |
31 | 31 |
const Invalid INVALID = Invalid(); |
32 | 32 |
#endif |
33 | 33 |
|
34 | 34 |
} //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_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 |
public: |
157 | 157 |
typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent; |
158 | 158 |
typedef DefaultMap<_Graph, _Item, _Value> Map; |
159 | 159 |
|
160 | 160 |
typedef typename Parent::Graph Graph; |
161 | 161 |
typedef typename Parent::Value Value; |
162 | 162 |
|
163 | 163 |
explicit DefaultMap(const Graph& graph) : Parent(graph) {} |
164 | 164 |
DefaultMap(const Graph& graph, const Value& value) |
165 | 165 |
: Parent(graph, value) {} |
166 | 166 |
|
167 | 167 |
DefaultMap& operator=(const DefaultMap& cmap) { |
168 | 168 |
return operator=<DefaultMap>(cmap); |
169 | 169 |
} |
170 | 170 |
|
171 | 171 |
template <typename CMap> |
172 | 172 |
DefaultMap& operator=(const CMap& cmap) { |
173 | 173 |
Parent::operator=(cmap); |
174 | 174 |
return *this; |
175 | 175 |
} |
176 | 176 |
|
177 | 177 |
}; |
178 | 178 |
|
179 | 179 |
} |
180 | 180 |
|
181 | 181 |
#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 |
public: |
40 | 40 |
|
41 | 41 |
typedef _Map Parent; |
42 | 42 |
typedef MapExtender Map; |
43 | 43 |
|
44 | 44 |
|
45 | 45 |
typedef typename Parent::Graph Graph; |
46 | 46 |
typedef typename Parent::Key Item; |
47 | 47 |
|
48 | 48 |
typedef typename Parent::Key Key; |
49 | 49 |
typedef typename Parent::Value Value; |
50 | 50 |
|
51 | 51 |
class MapIt; |
52 | 52 |
class ConstMapIt; |
53 | 53 |
|
54 | 54 |
friend class MapIt; |
55 | 55 |
friend class ConstMapIt; |
56 | 56 |
|
57 | 57 |
public: |
58 | 58 |
|
59 | 59 |
MapExtender(const Graph& graph) |
60 | 60 |
: Parent(graph) {} |
61 | 61 |
|
62 | 62 |
MapExtender(const Graph& graph, const Value& value) |
63 | 63 |
: Parent(graph, value) {} |
64 | 64 |
|
65 | 65 |
private: |
66 | 66 |
MapExtender& operator=(const MapExtender& cmap) { |
67 | 67 |
return operator=<MapExtender>(cmap); |
68 | 68 |
} |
69 | 69 |
|
70 | 70 |
template <typename CMap> |
71 | 71 |
MapExtender& operator=(const CMap& cmap) { |
72 | 72 |
Parent::operator=(cmap); |
73 | 73 |
return *this; |
74 | 74 |
} |
75 | 75 |
|
76 | 76 |
public: |
77 | 77 |
class MapIt : public Item { |
78 | 78 |
public: |
79 | 79 |
|
80 | 80 |
typedef Item Parent; |
81 | 81 |
typedef typename Map::Value Value; |
82 | 82 |
|
83 | 83 |
MapIt() : map(NULL) {} |
84 | 84 |
|
85 | 85 |
MapIt(Invalid i) : Parent(i), map(NULL) {} |
86 | 86 |
|
87 | 87 |
explicit MapIt(Map& _map) : map(&_map) { |
88 | 88 |
map->notifier()->first(*this); |
89 | 89 |
} |
90 | 90 |
|
91 | 91 |
MapIt(const Map& _map, const Item& item) |
92 | 92 |
: Parent(item), map(&_map) {} |
93 | 93 |
|
94 | 94 |
MapIt& operator++() { |
95 | 95 |
map->notifier()->next(*this); |
96 | 96 |
return *this; |
97 | 97 |
} |
98 | 98 |
|
99 | 99 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
100 | 100 |
return (*map)[*this]; |
101 | 101 |
} |
102 | 102 |
|
103 | 103 |
typename MapTraits<Map>::ReturnValue operator*() { |
104 | 104 |
return (*map)[*this]; |
105 | 105 |
} |
106 | 106 |
|
107 | 107 |
void set(const Value& value) { |
108 | 108 |
map->set(*this, value); |
109 | 109 |
} |
110 | 110 |
|
111 | 111 |
protected: |
112 | 112 |
Map* map; |
113 | 113 |
|
114 | 114 |
}; |
115 | 115 |
|
116 | 116 |
class ConstMapIt : public Item { |
117 | 117 |
public: |
118 | 118 |
|
119 | 119 |
typedef Item Parent; |
120 | 120 |
|
121 | 121 |
typedef typename Map::Value Value; |
122 | 122 |
|
123 | 123 |
ConstMapIt() : map(NULL) {} |
124 | 124 |
|
125 | 125 |
ConstMapIt(Invalid i) : Parent(i), map(NULL) {} |
126 | 126 |
|
127 | 127 |
explicit ConstMapIt(Map& _map) : map(&_map) { |
128 | 128 |
map->notifier()->first(*this); |
129 | 129 |
} |
130 | 130 |
|
131 | 131 |
ConstMapIt(const Map& _map, const Item& item) |
132 | 132 |
: Parent(item), map(_map) {} |
133 | 133 |
|
134 | 134 |
ConstMapIt& operator++() { |
135 | 135 |
map->notifier()->next(*this); |
136 | 136 |
return *this; |
137 | 137 |
} |
138 | 138 |
|
139 | 139 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
140 | 140 |
return map[*this]; |
141 | 141 |
} |
142 | 142 |
|
143 | 143 |
protected: |
144 | 144 |
const Map* map; |
145 | 145 |
}; |
146 | 146 |
|
147 | 147 |
class ItemIt : public Item { |
148 | 148 |
public: |
149 | 149 |
|
150 | 150 |
typedef Item Parent; |
151 | 151 |
|
152 | 152 |
ItemIt() : map(NULL) {} |
153 | 153 |
|
154 | 154 |
ItemIt(Invalid i) : Parent(i), map(NULL) {} |
155 | 155 |
|
156 | 156 |
explicit ItemIt(Map& _map) : map(&_map) { |
157 | 157 |
map->notifier()->first(*this); |
158 | 158 |
} |
159 | 159 |
|
160 | 160 |
ItemIt(const Map& _map, const Item& item) |
161 | 161 |
: Parent(item), map(&_map) {} |
162 | 162 |
|
163 | 163 |
ItemIt& operator++() { |
164 | 164 |
map->notifier()->next(*this); |
165 | 165 |
return *this; |
166 | 166 |
} |
167 | 167 |
|
168 | 168 |
protected: |
169 | 169 |
const Map* map; |
170 | 170 |
|
171 | 171 |
}; |
172 | 172 |
}; |
173 | 173 |
|
174 | 174 |
// \ingroup graphbits |
175 | 175 |
// |
176 | 176 |
// \brief Extender for maps which use a subset of the items. |
177 | 177 |
template <typename _Graph, typename _Map> |
178 | 178 |
class SubMapExtender : public _Map { |
179 | 179 |
public: |
180 | 180 |
|
181 | 181 |
typedef _Map Parent; |
182 | 182 |
typedef SubMapExtender Map; |
183 | 183 |
|
184 | 184 |
typedef _Graph Graph; |
185 | 185 |
|
186 | 186 |
typedef typename Parent::Key Item; |
187 | 187 |
|
188 | 188 |
typedef typename Parent::Key Key; |
189 | 189 |
typedef typename Parent::Value Value; |
190 | 190 |
|
191 | 191 |
class MapIt; |
192 | 192 |
class ConstMapIt; |
193 | 193 |
|
194 | 194 |
friend class MapIt; |
195 | 195 |
friend class ConstMapIt; |
196 | 196 |
|
197 | 197 |
public: |
198 | 198 |
|
199 | 199 |
SubMapExtender(const Graph& _graph) |
200 | 200 |
: Parent(_graph), graph(_graph) {} |
201 | 201 |
|
202 | 202 |
SubMapExtender(const Graph& _graph, const Value& _value) |
203 | 203 |
: Parent(_graph, _value), graph(_graph) {} |
204 | 204 |
|
205 | 205 |
private: |
206 | 206 |
SubMapExtender& operator=(const SubMapExtender& cmap) { |
207 | 207 |
return operator=<MapExtender>(cmap); |
208 | 208 |
} |
209 | 209 |
|
210 | 210 |
template <typename CMap> |
211 | 211 |
SubMapExtender& operator=(const CMap& cmap) { |
212 | 212 |
checkConcept<concepts::ReadMap<Key, Value>, CMap>(); |
213 | 213 |
Item it; |
214 | 214 |
for (graph.first(it); it != INVALID; graph.next(it)) { |
215 | 215 |
Parent::set(it, cmap[it]); |
216 | 216 |
} |
217 | 217 |
return *this; |
218 | 218 |
} |
219 | 219 |
|
220 | 220 |
public: |
221 | 221 |
class MapIt : public Item { |
222 | 222 |
public: |
223 | 223 |
|
224 | 224 |
typedef Item Parent; |
225 | 225 |
typedef typename Map::Value Value; |
226 | 226 |
|
227 | 227 |
MapIt() : map(NULL) {} |
228 | 228 |
|
229 | 229 |
MapIt(Invalid i) : Parent(i), map(NULL) { } |
230 | 230 |
|
231 | 231 |
explicit MapIt(Map& _map) : map(&_map) { |
232 | 232 |
map->graph.first(*this); |
233 | 233 |
} |
234 | 234 |
|
235 | 235 |
MapIt(const Map& _map, const Item& item) |
236 | 236 |
: Parent(item), map(&_map) {} |
237 | 237 |
|
238 | 238 |
MapIt& operator++() { |
239 | 239 |
map->graph.next(*this); |
240 | 240 |
return *this; |
241 | 241 |
} |
242 | 242 |
|
243 | 243 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
244 | 244 |
return (*map)[*this]; |
245 | 245 |
} |
246 | 246 |
|
247 | 247 |
typename MapTraits<Map>::ReturnValue operator*() { |
248 | 248 |
return (*map)[*this]; |
249 | 249 |
} |
250 | 250 |
|
251 | 251 |
void set(const Value& value) { |
252 | 252 |
map->set(*this, value); |
253 | 253 |
} |
254 | 254 |
|
255 | 255 |
protected: |
256 | 256 |
Map* map; |
257 | 257 |
|
258 | 258 |
}; |
259 | 259 |
|
260 | 260 |
class ConstMapIt : public Item { |
261 | 261 |
public: |
262 | 262 |
|
263 | 263 |
typedef Item Parent; |
264 | 264 |
|
265 | 265 |
typedef typename Map::Value Value; |
266 | 266 |
|
267 | 267 |
ConstMapIt() : map(NULL) {} |
268 | 268 |
|
269 | 269 |
ConstMapIt(Invalid i) : Parent(i), map(NULL) { } |
270 | 270 |
|
271 | 271 |
explicit ConstMapIt(Map& _map) : map(&_map) { |
272 | 272 |
map->graph.first(*this); |
273 | 273 |
} |
274 | 274 |
|
275 | 275 |
ConstMapIt(const Map& _map, const Item& item) |
276 | 276 |
: Parent(item), map(&_map) {} |
277 | 277 |
|
278 | 278 |
ConstMapIt& operator++() { |
279 | 279 |
map->graph.next(*this); |
280 | 280 |
return *this; |
281 | 281 |
} |
282 | 282 |
|
283 | 283 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
284 | 284 |
return (*map)[*this]; |
285 | 285 |
} |
286 | 286 |
|
287 | 287 |
protected: |
288 | 288 |
const Map* map; |
289 | 289 |
}; |
290 | 290 |
|
291 | 291 |
class ItemIt : public Item { |
292 | 292 |
public: |
293 | 293 |
|
294 | 294 |
typedef Item Parent; |
295 | 295 |
|
296 | 296 |
ItemIt() : map(NULL) {} |
297 | 297 |
|
298 | 298 |
ItemIt(Invalid i) : Parent(i), map(NULL) { } |
299 | 299 |
|
300 | 300 |
explicit ItemIt(Map& _map) : map(&_map) { |
301 | 301 |
map->graph.first(*this); |
302 | 302 |
} |
303 | 303 |
|
304 | 304 |
ItemIt(const Map& _map, const Item& item) |
305 | 305 |
: Parent(item), map(&_map) {} |
306 | 306 |
|
307 | 307 |
ItemIt& operator++() { |
308 | 308 |
map->graph.next(*this); |
309 | 309 |
return *this; |
310 | 310 |
} |
311 | 311 |
|
312 | 312 |
protected: |
313 | 313 |
const Map* map; |
314 | 314 |
|
315 | 315 |
}; |
316 | 316 |
|
317 | 317 |
private: |
318 | 318 |
|
319 | 319 |
const Graph& graph; |
320 | 320 |
|
321 | 321 |
}; |
322 | 322 |
|
323 | 323 |
} |
324 | 324 |
|
325 | 325 |
#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_PRED_MAP_PATH_H |
20 | 20 |
#define LEMON_BITS_PRED_MAP_PATH_H |
21 | 21 |
|
22 | 22 |
namespace lemon { |
23 | 23 |
|
24 | 24 |
template <typename _Digraph, typename _PredMap> |
25 | 25 |
class PredMapPath { |
26 | 26 |
public: |
27 | 27 |
typedef True RevPathTag; |
28 | 28 |
|
29 | 29 |
typedef _Digraph Digraph; |
30 | 30 |
typedef typename Digraph::Arc Arc; |
31 | 31 |
typedef _PredMap PredMap; |
32 | 32 |
|
33 | 33 |
PredMapPath(const Digraph& _digraph, const PredMap& _predMap, |
34 | 34 |
typename Digraph::Node _target) |
35 | 35 |
: digraph(_digraph), predMap(_predMap), target(_target) {} |
36 | 36 |
|
37 | 37 |
int length() const { |
38 | 38 |
int len = 0; |
39 | 39 |
typename Digraph::Node node = target; |
40 | 40 |
typename Digraph::Arc arc; |
41 | 41 |
while ((arc = predMap[node]) != INVALID) { |
42 | 42 |
node = digraph.source(arc); |
43 | 43 |
++len; |
44 | 44 |
} |
45 | 45 |
return len; |
46 | 46 |
} |
47 | 47 |
|
48 | 48 |
bool empty() const { |
49 | 49 |
return predMap[target] == INVALID; |
50 | 50 |
} |
51 | 51 |
|
52 | 52 |
class RevArcIt { |
53 | 53 |
public: |
54 | 54 |
RevArcIt() {} |
55 | 55 |
RevArcIt(Invalid) : path(0), current(INVALID) {} |
56 | 56 |
RevArcIt(const PredMapPath& _path) |
57 | 57 |
: path(&_path), current(_path.target) { |
58 | 58 |
if (path->predMap[current] == INVALID) current = INVALID; |
59 | 59 |
} |
60 | 60 |
|
61 | 61 |
operator const typename Digraph::Arc() const { |
62 | 62 |
return path->predMap[current]; |
63 | 63 |
} |
64 | 64 |
|
65 | 65 |
RevArcIt& operator++() { |
66 | 66 |
current = path->digraph.source(path->predMap[current]); |
67 | 67 |
if (path->predMap[current] == INVALID) current = INVALID; |
68 | 68 |
return *this; |
69 | 69 |
} |
70 | 70 |
|
71 | 71 |
bool operator==(const RevArcIt& e) const { |
72 | 72 |
return current == e.current; |
73 | 73 |
} |
74 | 74 |
|
75 | 75 |
bool operator!=(const RevArcIt& e) const { |
76 | 76 |
return current != e.current; |
77 | 77 |
} |
78 | 78 |
|
79 | 79 |
bool operator<(const RevArcIt& e) const { |
80 | 80 |
return current < e.current; |
81 | 81 |
} |
82 | 82 |
|
83 | 83 |
private: |
84 | 84 |
const PredMapPath* path; |
85 | 85 |
typename Digraph::Node current; |
86 | 86 |
}; |
87 | 87 |
|
88 | 88 |
private: |
89 | 89 |
const Digraph& digraph; |
90 | 90 |
const PredMap& predMap; |
91 | 91 |
typename Digraph::Node target; |
92 | 92 |
}; |
93 | 93 |
|
94 | 94 |
|
95 | 95 |
template <typename _Digraph, typename _PredMatrixMap> |
96 | 96 |
class PredMatrixMapPath { |
97 | 97 |
public: |
98 | 98 |
typedef True RevPathTag; |
99 | 99 |
|
100 | 100 |
typedef _Digraph Digraph; |
101 | 101 |
typedef typename Digraph::Arc Arc; |
102 | 102 |
typedef _PredMatrixMap PredMatrixMap; |
103 | 103 |
|
104 | 104 |
PredMatrixMapPath(const Digraph& _digraph, |
105 | 105 |
const PredMatrixMap& _predMatrixMap, |
106 | 106 |
typename Digraph::Node _source, |
107 | 107 |
typename Digraph::Node _target) |
108 | 108 |
: digraph(_digraph), predMatrixMap(_predMatrixMap), |
109 | 109 |
source(_source), target(_target) {} |
110 | 110 |
|
111 | 111 |
int length() const { |
112 | 112 |
int len = 0; |
113 | 113 |
typename Digraph::Node node = target; |
114 | 114 |
typename Digraph::Arc arc; |
115 | 115 |
while ((arc = predMatrixMap(source, node)) != INVALID) { |
116 | 116 |
node = digraph.source(arc); |
117 | 117 |
++len; |
118 | 118 |
} |
119 | 119 |
return len; |
120 | 120 |
} |
121 | 121 |
|
122 | 122 |
bool empty() const { |
123 | 123 |
return predMatrixMap(source, target) == INVALID; |
124 | 124 |
} |
125 | 125 |
|
126 | 126 |
class RevArcIt { |
127 | 127 |
public: |
128 | 128 |
RevArcIt() {} |
129 | 129 |
RevArcIt(Invalid) : path(0), current(INVALID) {} |
130 | 130 |
RevArcIt(const PredMatrixMapPath& _path) |
131 | 131 |
: path(&_path), current(_path.target) { |
132 | 132 |
if (path->predMatrixMap(path->source, current) == INVALID) |
133 | 133 |
current = INVALID; |
134 | 134 |
} |
135 | 135 |
|
136 | 136 |
operator const typename Digraph::Arc() const { |
137 | 137 |
return path->predMatrixMap(path->source, current); |
138 | 138 |
} |
139 | 139 |
|
140 | 140 |
RevArcIt& operator++() { |
141 | 141 |
current = |
142 | 142 |
path->digraph.source(path->predMatrixMap(path->source, current)); |
143 | 143 |
if (path->predMatrixMap(path->source, current) == INVALID) |
144 | 144 |
current = INVALID; |
145 | 145 |
return *this; |
146 | 146 |
} |
147 | 147 |
|
148 | 148 |
bool operator==(const RevArcIt& e) const { |
149 | 149 |
return current == e.current; |
150 | 150 |
} |
151 | 151 |
|
152 | 152 |
bool operator!=(const RevArcIt& e) const { |
153 | 153 |
return current != e.current; |
154 | 154 |
} |
155 | 155 |
|
156 | 156 |
bool operator<(const RevArcIt& e) const { |
157 | 157 |
return current < e.current; |
158 | 158 |
} |
159 | 159 |
|
160 | 160 |
private: |
161 | 161 |
const PredMatrixMapPath* path; |
162 | 162 |
typename Digraph::Node current; |
163 | 163 |
}; |
164 | 164 |
|
165 | 165 |
private: |
166 | 166 |
const Digraph& digraph; |
167 | 167 |
const PredMatrixMap& predMatrixMap; |
168 | 168 |
typename Digraph::Node source; |
169 | 169 |
typename Digraph::Node target; |
170 | 170 |
}; |
171 | 171 |
|
172 | 172 |
} |
173 | 173 |
|
174 | 174 |
#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 |
#include <sys/times.h> |
44 | 44 |
#include <sys/time.h> |
45 | 45 |
#endif |
46 | 46 |
|
47 | 47 |
#include <cmath> |
48 | 48 |
#include <sstream> |
49 | 49 |
|
50 | 50 |
namespace lemon { |
51 | 51 |
namespace bits { |
52 | 52 |
void getWinProcTimes(double &rtime, |
53 | 53 |
double &utime, double &stime, |
54 | 54 |
double &cutime, double &cstime) |
55 | 55 |
{ |
56 | 56 |
#ifdef WIN32 |
57 | 57 |
static const double ch = 4294967296.0e-7; |
58 | 58 |
static const double cl = 1.0e-7; |
59 | 59 |
|
60 | 60 |
FILETIME system; |
61 | 61 |
GetSystemTimeAsFileTime(&system); |
62 | 62 |
rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime; |
63 | 63 |
|
64 | 64 |
FILETIME create, exit, kernel, user; |
65 | 65 |
if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) { |
66 | 66 |
utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime; |
67 | 67 |
stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime; |
68 | 68 |
cutime = 0; |
69 | 69 |
cstime = 0; |
70 | 70 |
} else { |
71 | 71 |
rtime = 0; |
72 | 72 |
utime = 0; |
73 | 73 |
stime = 0; |
74 | 74 |
cutime = 0; |
75 | 75 |
cstime = 0; |
76 | 76 |
} |
77 | 77 |
#else |
78 | 78 |
timeval tv; |
79 | 79 |
gettimeofday(&tv, 0); |
80 | 80 |
rtime=tv.tv_sec+double(tv.tv_usec)/1e6; |
81 | 81 |
|
82 | 82 |
tms ts; |
83 | 83 |
double tck=sysconf(_SC_CLK_TCK); |
84 | 84 |
times(&ts); |
85 | 85 |
utime=ts.tms_utime/tck; |
86 | 86 |
stime=ts.tms_stime/tck; |
87 | 87 |
cutime=ts.tms_cutime/tck; |
88 | 88 |
cstime=ts.tms_cstime/tck; |
89 | 89 |
#endif |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
std::string getWinFormattedDate() |
93 | 93 |
{ |
94 | 94 |
std::ostringstream os; |
95 | 95 |
#ifdef WIN32 |
96 | 96 |
SYSTEMTIME time; |
97 | 97 |
GetSystemTime(&time); |
98 | 98 |
char buf1[11], buf2[9], buf3[5]; |
99 |
|
|
99 |
if (GetDateFormat(MY_LOCALE, 0, &time, |
|
100 | 100 |
("ddd MMM dd"), buf1, 11) && |
101 | 101 |
GetTimeFormat(MY_LOCALE, 0, &time, |
102 | 102 |
("HH':'mm':'ss"), buf2, 9) && |
103 | 103 |
GetDateFormat(MY_LOCALE, 0, &time, |
104 | 104 |
("yyyy"), buf3, 5)) { |
105 | 105 |
os << buf1 << ' ' << buf2 << ' ' << buf3; |
106 | 106 |
} |
107 | 107 |
else os << "unknown"; |
108 | 108 |
#else |
109 | 109 |
timeval tv; |
110 | 110 |
gettimeofday(&tv, 0); |
111 | 111 |
|
112 | 112 |
char cbuf[26]; |
113 | 113 |
ctime_r(&tv.tv_sec,cbuf); |
114 | 114 |
os << cbuf; |
115 | 115 |
#endif |
116 | 116 |
return os.str(); |
117 | 117 |
} |
118 | 118 |
|
119 | 119 |
int getWinRndSeed() |
120 | 120 |
{ |
121 | 121 |
#ifdef WIN32 |
122 | 122 |
FILETIME time; |
123 | 123 |
GetSystemTimeAsFileTime(&time); |
124 | 124 |
return GetCurrentProcessId() + time.dwHighDateTime + time.dwLowDateTime; |
125 | 125 |
#else |
126 | 126 |
timeval tv; |
127 | 127 |
gettimeofday(&tv, 0); |
128 | 128 |
return getpid() + tv.tv_sec + tv.tv_usec; |
129 | 129 |
#endif |
130 | 130 |
} |
131 | 131 |
} |
132 | 132 |
} |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 |
* Copyright (C) 2003- |
|
5 |
* Copyright (C) 2003-2011 |
|
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_CORE_H |
20 | 20 |
#define LEMON_CORE_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <algorithm> |
24 | 24 |
|
25 | 25 |
#include <lemon/config.h> |
26 | 26 |
#include <lemon/bits/enable_if.h> |
27 | 27 |
#include <lemon/bits/traits.h> |
28 | 28 |
#include <lemon/assert.h> |
29 | 29 |
|
30 | 30 |
///\file |
31 | 31 |
///\brief LEMON core utilities. |
32 | 32 |
/// |
33 | 33 |
///This header file contains core utilities for LEMON. |
34 | 34 |
///It is automatically included by all graph types, therefore it usually |
35 | 35 |
///do not have to be included directly. |
36 | 36 |
|
37 | 37 |
namespace lemon { |
38 | 38 |
|
39 | 39 |
/// \brief Dummy type to make it easier to create invalid iterators. |
40 | 40 |
/// |
41 | 41 |
/// Dummy type to make it easier to create invalid iterators. |
42 | 42 |
/// See \ref INVALID for the usage. |
43 | 43 |
struct Invalid { |
44 | 44 |
public: |
45 | 45 |
bool operator==(Invalid) { return true; } |
46 | 46 |
bool operator!=(Invalid) { return false; } |
47 | 47 |
bool operator< (Invalid) { return false; } |
48 | 48 |
}; |
49 | 49 |
|
50 | 50 |
/// \brief Invalid iterators. |
51 | 51 |
/// |
52 | 52 |
/// \ref Invalid is a global type that converts to each iterator |
53 | 53 |
/// in such a way that the value of the target iterator will be invalid. |
54 | 54 |
#ifdef LEMON_ONLY_TEMPLATES |
55 | 55 |
const Invalid INVALID = Invalid(); |
56 | 56 |
#else |
57 | 57 |
extern const Invalid INVALID; |
58 | 58 |
#endif |
59 | 59 |
|
60 | 60 |
/// \addtogroup gutils |
61 | 61 |
/// @{ |
62 | 62 |
|
63 | 63 |
///Create convenience typedefs for the digraph types and iterators |
64 | 64 |
|
65 | 65 |
///This \c \#define creates convenient type definitions for the following |
66 | 66 |
///types of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
67 | 67 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
68 | 68 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
69 | 69 |
/// |
70 | 70 |
///\note If the graph type is a dependent type, ie. the graph type depend |
71 | 71 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
72 | 72 |
///macro. |
73 | 73 |
#define DIGRAPH_TYPEDEFS(Digraph) \ |
74 | 74 |
typedef Digraph::Node Node; \ |
75 | 75 |
typedef Digraph::NodeIt NodeIt; \ |
76 | 76 |
typedef Digraph::Arc Arc; \ |
77 | 77 |
typedef Digraph::ArcIt ArcIt; \ |
78 | 78 |
typedef Digraph::InArcIt InArcIt; \ |
79 | 79 |
typedef Digraph::OutArcIt OutArcIt; \ |
80 | 80 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \ |
81 | 81 |
typedef Digraph::NodeMap<int> IntNodeMap; \ |
82 | 82 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \ |
83 | 83 |
typedef Digraph::ArcMap<bool> BoolArcMap; \ |
84 | 84 |
typedef Digraph::ArcMap<int> IntArcMap; \ |
85 | 85 |
typedef Digraph::ArcMap<double> DoubleArcMap |
86 | 86 |
|
87 | 87 |
///Create convenience typedefs for the digraph types and iterators |
88 | 88 |
|
89 | 89 |
///\see DIGRAPH_TYPEDEFS |
90 | 90 |
/// |
91 | 91 |
///\note Use this macro, if the graph type is a dependent type, |
92 | 92 |
///ie. the graph type depend on a template parameter. |
93 | 93 |
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \ |
94 | 94 |
typedef typename Digraph::Node Node; \ |
95 | 95 |
typedef typename Digraph::NodeIt NodeIt; \ |
96 | 96 |
typedef typename Digraph::Arc Arc; \ |
97 | 97 |
typedef typename Digraph::ArcIt ArcIt; \ |
98 | 98 |
typedef typename Digraph::InArcIt InArcIt; \ |
99 | 99 |
typedef typename Digraph::OutArcIt OutArcIt; \ |
100 | 100 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \ |
101 | 101 |
typedef typename Digraph::template NodeMap<int> IntNodeMap; \ |
102 | 102 |
typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \ |
103 | 103 |
typedef typename Digraph::template ArcMap<bool> BoolArcMap; \ |
104 | 104 |
typedef typename Digraph::template ArcMap<int> IntArcMap; \ |
105 | 105 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap |
106 | 106 |
|
107 | 107 |
///Create convenience typedefs for the graph types and iterators |
108 | 108 |
|
109 | 109 |
///This \c \#define creates the same convenient type definitions as defined |
110 | 110 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates |
111 | 111 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap, |
112 | 112 |
///\c DoubleEdgeMap. |
113 | 113 |
/// |
114 | 114 |
///\note If the graph type is a dependent type, ie. the graph type depend |
115 | 115 |
///on a template parameter, then use \c TEMPLATE_GRAPH_TYPEDEFS() |
116 | 116 |
///macro. |
117 | 117 |
#define GRAPH_TYPEDEFS(Graph) \ |
118 | 118 |
DIGRAPH_TYPEDEFS(Graph); \ |
119 | 119 |
typedef Graph::Edge Edge; \ |
120 | 120 |
typedef Graph::EdgeIt EdgeIt; \ |
121 | 121 |
typedef Graph::IncEdgeIt IncEdgeIt; \ |
122 | 122 |
typedef Graph::EdgeMap<bool> BoolEdgeMap; \ |
123 | 123 |
typedef Graph::EdgeMap<int> IntEdgeMap; \ |
124 | 124 |
typedef Graph::EdgeMap<double> DoubleEdgeMap |
125 | 125 |
|
126 | 126 |
///Create convenience typedefs for the graph types and iterators |
127 | 127 |
|
128 | 128 |
///\see GRAPH_TYPEDEFS |
129 | 129 |
/// |
130 | 130 |
///\note Use this macro, if the graph type is a dependent type, |
131 | 131 |
///ie. the graph type depend on a template parameter. |
132 | 132 |
#define TEMPLATE_GRAPH_TYPEDEFS(Graph) \ |
133 | 133 |
TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \ |
134 | 134 |
typedef typename Graph::Edge Edge; \ |
135 | 135 |
typedef typename Graph::EdgeIt EdgeIt; \ |
136 | 136 |
typedef typename Graph::IncEdgeIt IncEdgeIt; \ |
137 | 137 |
typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \ |
138 | 138 |
typedef typename Graph::template EdgeMap<int> IntEdgeMap; \ |
139 | 139 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap |
140 | 140 |
|
141 | 141 |
/// \brief Function to count the items in a graph. |
142 | 142 |
/// |
143 | 143 |
/// This function counts the items (nodes, arcs etc.) in a graph. |
144 | 144 |
/// The complexity of the function is linear because |
145 | 145 |
/// it iterates on all of the items. |
146 | 146 |
template <typename Graph, typename Item> |
147 | 147 |
inline int countItems(const Graph& g) { |
148 | 148 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
149 | 149 |
int num = 0; |
150 | 150 |
for (ItemIt it(g); it != INVALID; ++it) { |
151 | 151 |
++num; |
152 | 152 |
} |
153 | 153 |
return num; |
154 | 154 |
} |
155 | 155 |
|
156 | 156 |
// Node counting: |
157 | 157 |
|
158 | 158 |
namespace _core_bits { |
159 | 159 |
|
160 | 160 |
template <typename Graph, typename Enable = void> |
161 | 161 |
struct CountNodesSelector { |
162 | 162 |
static int count(const Graph &g) { |
163 | 163 |
return countItems<Graph, typename Graph::Node>(g); |
164 | 164 |
} |
165 | 165 |
}; |
166 | 166 |
|
167 | 167 |
template <typename Graph> |
168 | 168 |
struct CountNodesSelector< |
169 | 169 |
Graph, typename |
170 | 170 |
enable_if<typename Graph::NodeNumTag, void>::type> |
171 | 171 |
{ |
172 | 172 |
static int count(const Graph &g) { |
173 | 173 |
return g.nodeNum(); |
174 | 174 |
} |
175 | 175 |
}; |
176 | 176 |
} |
177 | 177 |
|
178 | 178 |
/// \brief Function to count the nodes in the graph. |
179 | 179 |
/// |
180 | 180 |
/// This function counts the nodes in the graph. |
181 | 181 |
/// The complexity of the function is <em>O</em>(<em>n</em>), but for some |
182 | 182 |
/// graph structures it is specialized to run in <em>O</em>(1). |
183 | 183 |
/// |
184 | 184 |
/// \note If the graph contains a \c nodeNum() member function and a |
185 | 185 |
/// \c NodeNumTag tag then this function calls directly the member |
186 | 186 |
/// function to query the cardinality of the node set. |
187 | 187 |
template <typename Graph> |
188 | 188 |
inline int countNodes(const Graph& g) { |
189 | 189 |
return _core_bits::CountNodesSelector<Graph>::count(g); |
190 | 190 |
} |
191 | 191 |
|
192 | 192 |
// Arc counting: |
193 | 193 |
|
194 | 194 |
namespace _core_bits { |
195 | 195 |
|
196 | 196 |
template <typename Graph, typename Enable = void> |
197 | 197 |
struct CountArcsSelector { |
198 | 198 |
static int count(const Graph &g) { |
199 | 199 |
return countItems<Graph, typename Graph::Arc>(g); |
200 | 200 |
} |
201 | 201 |
}; |
202 | 202 |
|
203 | 203 |
template <typename Graph> |
204 | 204 |
struct CountArcsSelector< |
205 | 205 |
Graph, |
206 | 206 |
typename enable_if<typename Graph::ArcNumTag, void>::type> |
207 | 207 |
{ |
208 | 208 |
static int count(const Graph &g) { |
209 | 209 |
return g.arcNum(); |
210 | 210 |
} |
211 | 211 |
}; |
212 | 212 |
} |
213 | 213 |
|
214 | 214 |
/// \brief Function to count the arcs in the graph. |
215 | 215 |
/// |
216 | 216 |
/// This function counts the arcs in the graph. |
217 | 217 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some |
218 | 218 |
/// graph structures it is specialized to run in <em>O</em>(1). |
219 | 219 |
/// |
220 | 220 |
/// \note If the graph contains a \c arcNum() member function and a |
221 | 221 |
/// \c ArcNumTag tag then this function calls directly the member |
222 | 222 |
/// function to query the cardinality of the arc set. |
223 | 223 |
template <typename Graph> |
224 | 224 |
inline int countArcs(const Graph& g) { |
225 | 225 |
return _core_bits::CountArcsSelector<Graph>::count(g); |
226 | 226 |
} |
227 | 227 |
|
228 | 228 |
// Edge counting: |
229 | 229 |
|
230 | 230 |
namespace _core_bits { |
231 | 231 |
|
232 | 232 |
template <typename Graph, typename Enable = void> |
233 | 233 |
struct CountEdgesSelector { |
234 | 234 |
static int count(const Graph &g) { |
235 | 235 |
return countItems<Graph, typename Graph::Edge>(g); |
236 | 236 |
} |
237 | 237 |
}; |
238 | 238 |
|
239 | 239 |
template <typename Graph> |
240 | 240 |
struct CountEdgesSelector< |
241 | 241 |
Graph, |
242 | 242 |
typename enable_if<typename Graph::EdgeNumTag, void>::type> |
243 | 243 |
{ |
244 | 244 |
static int count(const Graph &g) { |
245 | 245 |
return g.edgeNum(); |
246 | 246 |
} |
247 | 247 |
}; |
248 | 248 |
} |
249 | 249 |
|
250 | 250 |
/// \brief Function to count the edges in the graph. |
251 | 251 |
/// |
252 | 252 |
/// This function counts the edges in the graph. |
253 | 253 |
/// The complexity of the function is <em>O</em>(<em>m</em>), but for some |
254 | 254 |
/// graph structures it is specialized to run in <em>O</em>(1). |
255 | 255 |
/// |
256 | 256 |
/// \note If the graph contains a \c edgeNum() member function and a |
257 | 257 |
/// \c EdgeNumTag tag then this function calls directly the member |
258 | 258 |
/// function to query the cardinality of the edge set. |
259 | 259 |
template <typename Graph> |
260 | 260 |
inline int countEdges(const Graph& g) { |
261 | 261 |
return _core_bits::CountEdgesSelector<Graph>::count(g); |
262 | 262 |
|
263 | 263 |
} |
264 | 264 |
|
265 | 265 |
|
266 | 266 |
template <typename Graph, typename DegIt> |
267 | 267 |
inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) { |
268 | 268 |
int num = 0; |
269 | 269 |
for (DegIt it(_g, _n); it != INVALID; ++it) { |
270 | 270 |
++num; |
271 | 271 |
} |
272 | 272 |
return num; |
273 | 273 |
} |
274 | 274 |
|
275 | 275 |
/// \brief Function to count the number of the out-arcs from node \c n. |
276 | 276 |
/// |
277 | 277 |
/// This function counts the number of the out-arcs from node \c n |
278 | 278 |
/// in the graph \c g. |
279 | 279 |
template <typename Graph> |
280 | 280 |
inline int countOutArcs(const Graph& g, const typename Graph::Node& n) { |
281 | 281 |
return countNodeDegree<Graph, typename Graph::OutArcIt>(g, n); |
282 | 282 |
} |
283 | 283 |
|
284 | 284 |
/// \brief Function to count the number of the in-arcs to node \c n. |
285 | 285 |
/// |
286 | 286 |
/// This function counts the number of the in-arcs to node \c n |
287 | 287 |
/// in the graph \c g. |
288 | 288 |
template <typename Graph> |
289 | 289 |
inline int countInArcs(const Graph& g, const typename Graph::Node& n) { |
290 | 290 |
return countNodeDegree<Graph, typename Graph::InArcIt>(g, n); |
291 | 291 |
} |
292 | 292 |
|
293 | 293 |
/// \brief Function to count the number of the inc-edges to node \c n. |
294 | 294 |
/// |
295 | 295 |
/// This function counts the number of the inc-edges to node \c n |
296 | 296 |
/// in the undirected graph \c g. |
297 | 297 |
template <typename Graph> |
298 | 298 |
inline int countIncEdges(const Graph& g, const typename Graph::Node& n) { |
299 | 299 |
return countNodeDegree<Graph, typename Graph::IncEdgeIt>(g, n); |
300 | 300 |
} |
301 | 301 |
|
302 | 302 |
namespace _core_bits { |
303 | 303 |
|
304 | 304 |
template <typename Digraph, typename Item, typename RefMap> |
305 | 305 |
class MapCopyBase { |
306 | 306 |
public: |
307 | 307 |
virtual void copy(const Digraph& from, const RefMap& refMap) = 0; |
308 | 308 |
|
309 | 309 |
virtual ~MapCopyBase() {} |
310 | 310 |
}; |
311 | 311 |
|
312 | 312 |
template <typename Digraph, typename Item, typename RefMap, |
313 | 313 |
typename FromMap, typename ToMap> |
314 | 314 |
class MapCopy : public MapCopyBase<Digraph, Item, RefMap> { |
315 | 315 |
public: |
316 | 316 |
|
317 | 317 |
MapCopy(const FromMap& map, ToMap& tmap) |
318 | 318 |
: _map(map), _tmap(tmap) {} |
319 | 319 |
|
320 | 320 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
321 | 321 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
322 | 322 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
323 | 323 |
_tmap.set(refMap[it], _map[it]); |
324 | 324 |
} |
325 | 325 |
} |
326 | 326 |
|
327 | 327 |
private: |
328 | 328 |
const FromMap& _map; |
329 | 329 |
ToMap& _tmap; |
330 | 330 |
}; |
331 | 331 |
|
332 | 332 |
template <typename Digraph, typename Item, typename RefMap, typename It> |
333 | 333 |
class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> { |
334 | 334 |
public: |
335 | 335 |
|
336 | 336 |
ItemCopy(const Item& item, It& it) : _item(item), _it(it) {} |
337 | 337 |
|
338 | 338 |
virtual void copy(const Digraph&, const RefMap& refMap) { |
339 | 339 |
_it = refMap[_item]; |
340 | 340 |
} |
341 | 341 |
|
342 | 342 |
private: |
343 | 343 |
Item _item; |
344 | 344 |
It& _it; |
345 | 345 |
}; |
346 | 346 |
|
347 | 347 |
template <typename Digraph, typename Item, typename RefMap, typename Ref> |
348 | 348 |
class RefCopy : public MapCopyBase<Digraph, Item, RefMap> { |
349 | 349 |
public: |
350 | 350 |
|
351 | 351 |
RefCopy(Ref& map) : _map(map) {} |
352 | 352 |
|
353 | 353 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
354 | 354 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
355 | 355 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
356 | 356 |
_map.set(it, refMap[it]); |
357 | 357 |
} |
358 | 358 |
} |
359 | 359 |
|
360 | 360 |
private: |
361 | 361 |
Ref& _map; |
362 | 362 |
}; |
363 | 363 |
|
364 | 364 |
template <typename Digraph, typename Item, typename RefMap, |
365 | 365 |
typename CrossRef> |
366 | 366 |
class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> { |
367 | 367 |
public: |
368 | 368 |
|
369 | 369 |
CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {} |
370 | 370 |
|
371 | 371 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
372 | 372 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
373 | 373 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
374 | 374 |
_cmap.set(refMap[it], it); |
375 | 375 |
} |
376 | 376 |
} |
377 | 377 |
|
378 | 378 |
private: |
379 | 379 |
CrossRef& _cmap; |
380 | 380 |
}; |
381 | 381 |
|
382 | 382 |
template <typename Digraph, typename Enable = void> |
383 | 383 |
struct DigraphCopySelector { |
384 | 384 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
385 | 385 |
static void copy(const From& from, Digraph &to, |
386 | 386 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
387 | 387 |
to.clear(); |
388 | 388 |
for (typename From::NodeIt it(from); it != INVALID; ++it) { |
389 | 389 |
nodeRefMap[it] = to.addNode(); |
390 | 390 |
} |
391 | 391 |
for (typename From::ArcIt it(from); it != INVALID; ++it) { |
392 | 392 |
arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
393 | 393 |
nodeRefMap[from.target(it)]); |
394 | 394 |
} |
395 | 395 |
} |
396 | 396 |
}; |
397 | 397 |
|
398 | 398 |
template <typename Digraph> |
399 | 399 |
struct DigraphCopySelector< |
400 | 400 |
Digraph, |
401 | 401 |
typename enable_if<typename Digraph::BuildTag, void>::type> |
402 | 402 |
{ |
403 | 403 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
404 | 404 |
static void copy(const From& from, Digraph &to, |
405 | 405 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
406 | 406 |
to.build(from, nodeRefMap, arcRefMap); |
407 | 407 |
} |
408 | 408 |
}; |
409 | 409 |
|
410 | 410 |
template <typename Graph, typename Enable = void> |
411 | 411 |
struct GraphCopySelector { |
412 | 412 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
413 | 413 |
static void copy(const From& from, Graph &to, |
414 | 414 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
415 | 415 |
to.clear(); |
416 | 416 |
for (typename From::NodeIt it(from); it != INVALID; ++it) { |
417 | 417 |
nodeRefMap[it] = to.addNode(); |
418 | 418 |
} |
419 | 419 |
for (typename From::EdgeIt it(from); it != INVALID; ++it) { |
420 | 420 |
edgeRefMap[it] = to.addEdge(nodeRefMap[from.u(it)], |
421 | 421 |
nodeRefMap[from.v(it)]); |
422 | 422 |
} |
423 | 423 |
} |
424 | 424 |
}; |
425 | 425 |
|
426 | 426 |
template <typename Graph> |
427 | 427 |
struct GraphCopySelector< |
428 | 428 |
Graph, |
429 | 429 |
typename enable_if<typename Graph::BuildTag, void>::type> |
430 | 430 |
{ |
431 | 431 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
432 | 432 |
static void copy(const From& from, Graph &to, |
433 | 433 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
434 | 434 |
to.build(from, nodeRefMap, edgeRefMap); |
435 | 435 |
} |
436 | 436 |
}; |
437 | 437 |
|
438 | 438 |
} |
439 | 439 |
|
440 | 440 |
/// \brief Class to copy a digraph. |
441 | 441 |
/// |
442 | 442 |
/// Class to copy a digraph to another digraph (duplicate a digraph). The |
443 | 443 |
/// simplest way of using it is through the \c digraphCopy() function. |
444 | 444 |
/// |
445 | 445 |
/// This class not only make a copy of a digraph, but it can create |
446 | 446 |
/// references and cross references between the nodes and arcs of |
447 | 447 |
/// the two digraphs, and it can copy maps to use with the newly created |
448 | 448 |
/// digraph. |
449 | 449 |
/// |
450 | 450 |
/// To make a copy from a digraph, first an instance of DigraphCopy |
451 | 451 |
/// should be created, then the data belongs to the digraph should |
452 | 452 |
/// assigned to copy. In the end, the \c run() member should be |
453 | 453 |
/// called. |
454 | 454 |
/// |
455 | 455 |
/// The next code copies a digraph with several data: |
456 | 456 |
///\code |
457 | 457 |
/// DigraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph); |
458 | 458 |
/// // Create references for the nodes |
459 | 459 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
460 | 460 |
/// cg.nodeRef(nr); |
461 | 461 |
/// // Create cross references (inverse) for the arcs |
462 | 462 |
/// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph); |
463 | 463 |
/// cg.arcCrossRef(acr); |
464 | 464 |
/// // Copy an arc map |
465 | 465 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
466 | 466 |
/// NewGraph::ArcMap<double> namap(new_graph); |
467 | 467 |
/// cg.arcMap(oamap, namap); |
468 | 468 |
/// // Copy a node |
469 | 469 |
/// OrigGraph::Node on; |
470 | 470 |
/// NewGraph::Node nn; |
471 | 471 |
/// cg.node(on, nn); |
472 | 472 |
/// // Execute copying |
473 | 473 |
/// cg.run(); |
474 | 474 |
///\endcode |
475 | 475 |
template <typename From, typename To> |
476 | 476 |
class DigraphCopy { |
477 | 477 |
private: |
478 | 478 |
|
479 | 479 |
typedef typename From::Node Node; |
480 | 480 |
typedef typename From::NodeIt NodeIt; |
481 | 481 |
typedef typename From::Arc Arc; |
482 | 482 |
typedef typename From::ArcIt ArcIt; |
483 | 483 |
|
484 | 484 |
typedef typename To::Node TNode; |
485 | 485 |
typedef typename To::Arc TArc; |
486 | 486 |
|
487 | 487 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
488 | 488 |
typedef typename From::template ArcMap<TArc> ArcRefMap; |
489 | 489 |
|
490 | 490 |
public: |
491 | 491 |
|
492 | 492 |
/// \brief Constructor of DigraphCopy. |
493 | 493 |
/// |
494 | 494 |
/// Constructor of DigraphCopy for copying the content of the |
495 | 495 |
/// \c from digraph into the \c to digraph. |
496 | 496 |
DigraphCopy(const From& from, To& to) |
497 | 497 |
: _from(from), _to(to) {} |
498 | 498 |
|
499 | 499 |
/// \brief Destructor of DigraphCopy |
500 | 500 |
/// |
501 | 501 |
/// Destructor of DigraphCopy. |
502 | 502 |
~DigraphCopy() { |
503 | 503 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
504 | 504 |
delete _node_maps[i]; |
505 | 505 |
} |
506 | 506 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
507 | 507 |
delete _arc_maps[i]; |
508 | 508 |
} |
509 | 509 |
|
510 | 510 |
} |
511 | 511 |
|
512 | 512 |
/// \brief Copy the node references into the given map. |
513 | 513 |
/// |
514 | 514 |
/// This function copies the node references into the given map. |
515 | 515 |
/// The parameter should be a map, whose key type is the Node type of |
516 | 516 |
/// the source digraph, while the value type is the Node type of the |
517 | 517 |
/// destination digraph. |
518 | 518 |
template <typename NodeRef> |
519 | 519 |
DigraphCopy& nodeRef(NodeRef& map) { |
520 | 520 |
_node_maps.push_back(new _core_bits::RefCopy<From, Node, |
521 | 521 |
NodeRefMap, NodeRef>(map)); |
522 | 522 |
return *this; |
523 | 523 |
} |
524 | 524 |
|
525 | 525 |
/// \brief Copy the node cross references into the given map. |
526 | 526 |
/// |
527 | 527 |
/// This function copies the node cross references (reverse references) |
528 | 528 |
/// into the given map. The parameter should be a map, whose key type |
529 | 529 |
/// is the Node type of the destination digraph, while the value type is |
530 | 530 |
/// the Node type of the source digraph. |
531 | 531 |
template <typename NodeCrossRef> |
532 | 532 |
DigraphCopy& nodeCrossRef(NodeCrossRef& map) { |
533 | 533 |
_node_maps.push_back(new _core_bits::CrossRefCopy<From, Node, |
534 | 534 |
NodeRefMap, NodeCrossRef>(map)); |
535 | 535 |
return *this; |
536 | 536 |
} |
537 | 537 |
|
538 | 538 |
/// \brief Make a copy of the given node map. |
539 | 539 |
/// |
540 | 540 |
/// This function makes a copy of the given node map for the newly |
541 | 541 |
/// created digraph. |
542 | 542 |
/// The key type of the new map \c tmap should be the Node type of the |
543 | 543 |
/// destination digraph, and the key type of the original map \c map |
544 | 544 |
/// should be the Node type of the source digraph. |
545 | 545 |
template <typename FromMap, typename ToMap> |
546 | 546 |
DigraphCopy& nodeMap(const FromMap& map, ToMap& tmap) { |
547 | 547 |
_node_maps.push_back(new _core_bits::MapCopy<From, Node, |
548 | 548 |
NodeRefMap, FromMap, ToMap>(map, tmap)); |
549 | 549 |
return *this; |
550 | 550 |
} |
551 | 551 |
|
552 | 552 |
/// \brief Make a copy of the given node. |
553 | 553 |
/// |
554 | 554 |
/// This function makes a copy of the given node. |
555 | 555 |
DigraphCopy& node(const Node& node, TNode& tnode) { |
556 | 556 |
_node_maps.push_back(new _core_bits::ItemCopy<From, Node, |
557 | 557 |
NodeRefMap, TNode>(node, tnode)); |
558 | 558 |
return *this; |
559 | 559 |
} |
560 | 560 |
|
561 | 561 |
/// \brief Copy the arc references into the given map. |
562 | 562 |
/// |
563 | 563 |
/// This function copies the arc references into the given map. |
564 | 564 |
/// The parameter should be a map, whose key type is the Arc type of |
565 | 565 |
/// the source digraph, while the value type is the Arc type of the |
566 | 566 |
/// destination digraph. |
567 | 567 |
template <typename ArcRef> |
568 | 568 |
DigraphCopy& arcRef(ArcRef& map) { |
569 | 569 |
_arc_maps.push_back(new _core_bits::RefCopy<From, Arc, |
570 | 570 |
ArcRefMap, ArcRef>(map)); |
571 | 571 |
return *this; |
572 | 572 |
} |
573 | 573 |
|
574 | 574 |
/// \brief Copy the arc cross references into the given map. |
575 | 575 |
/// |
576 | 576 |
/// This function copies the arc cross references (reverse references) |
577 | 577 |
/// into the given map. The parameter should be a map, whose key type |
578 | 578 |
/// is the Arc type of the destination digraph, while the value type is |
579 | 579 |
/// the Arc type of the source digraph. |
580 | 580 |
template <typename ArcCrossRef> |
581 | 581 |
DigraphCopy& arcCrossRef(ArcCrossRef& map) { |
582 | 582 |
_arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc, |
583 | 583 |
ArcRefMap, ArcCrossRef>(map)); |
584 | 584 |
return *this; |
585 | 585 |
} |
586 | 586 |
|
587 | 587 |
/// \brief Make a copy of the given arc map. |
588 | 588 |
/// |
589 | 589 |
/// This function makes a copy of the given arc map for the newly |
590 | 590 |
/// created digraph. |
591 | 591 |
/// The key type of the new map \c tmap should be the Arc type of the |
592 | 592 |
/// destination digraph, and the key type of the original map \c map |
593 | 593 |
/// should be the Arc type of the source digraph. |
594 | 594 |
template <typename FromMap, typename ToMap> |
595 | 595 |
DigraphCopy& arcMap(const FromMap& map, ToMap& tmap) { |
596 | 596 |
_arc_maps.push_back(new _core_bits::MapCopy<From, Arc, |
597 | 597 |
ArcRefMap, FromMap, ToMap>(map, tmap)); |
598 | 598 |
return *this; |
599 | 599 |
} |
600 | 600 |
|
601 | 601 |
/// \brief Make a copy of the given arc. |
602 | 602 |
/// |
603 | 603 |
/// This function makes a copy of the given arc. |
604 | 604 |
DigraphCopy& arc(const Arc& arc, TArc& tarc) { |
605 | 605 |
_arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, |
606 | 606 |
ArcRefMap, TArc>(arc, tarc)); |
607 | 607 |
return *this; |
608 | 608 |
} |
609 | 609 |
|
610 | 610 |
/// \brief Execute copying. |
611 | 611 |
/// |
612 | 612 |
/// This function executes the copying of the digraph along with the |
613 | 613 |
/// copying of the assigned data. |
614 | 614 |
void run() { |
615 | 615 |
NodeRefMap nodeRefMap(_from); |
616 | 616 |
ArcRefMap arcRefMap(_from); |
617 | 617 |
_core_bits::DigraphCopySelector<To>:: |
618 | 618 |
copy(_from, _to, nodeRefMap, arcRefMap); |
619 | 619 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
620 | 620 |
_node_maps[i]->copy(_from, nodeRefMap); |
621 | 621 |
} |
622 | 622 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
623 | 623 |
_arc_maps[i]->copy(_from, arcRefMap); |
624 | 624 |
} |
625 | 625 |
} |
626 | 626 |
|
627 | 627 |
protected: |
628 | 628 |
|
629 | 629 |
const From& _from; |
630 | 630 |
To& _to; |
631 | 631 |
|
632 | 632 |
std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* > |
633 | 633 |
_node_maps; |
634 | 634 |
|
635 | 635 |
std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
636 | 636 |
_arc_maps; |
637 | 637 |
|
638 | 638 |
}; |
639 | 639 |
|
640 | 640 |
/// \brief Copy a digraph to another digraph. |
641 | 641 |
/// |
642 | 642 |
/// This function copies a digraph to another digraph. |
643 | 643 |
/// The complete usage of it is detailed in the DigraphCopy class, but |
644 | 644 |
/// a short example shows a basic work: |
645 | 645 |
///\code |
646 | 646 |
/// digraphCopy(src, trg).nodeRef(nr).arcCrossRef(acr).run(); |
647 | 647 |
///\endcode |
648 | 648 |
/// |
649 | 649 |
/// After the copy the \c nr map will contain the mapping from the |
650 | 650 |
/// nodes of the \c from digraph to the nodes of the \c to digraph and |
651 | 651 |
/// \c acr will contain the mapping from the arcs of the \c to digraph |
652 | 652 |
/// to the arcs of the \c from digraph. |
653 | 653 |
/// |
654 | 654 |
/// \see DigraphCopy |
655 | 655 |
template <typename From, typename To> |
656 | 656 |
DigraphCopy<From, To> digraphCopy(const From& from, To& to) { |
657 | 657 |
return DigraphCopy<From, To>(from, to); |
658 | 658 |
} |
659 | 659 |
|
660 | 660 |
/// \brief Class to copy a graph. |
661 | 661 |
/// |
662 | 662 |
/// Class to copy a graph to another graph (duplicate a graph). The |
663 | 663 |
/// simplest way of using it is through the \c graphCopy() function. |
664 | 664 |
/// |
665 | 665 |
/// This class not only make a copy of a graph, but it can create |
666 | 666 |
/// references and cross references between the nodes, edges and arcs of |
667 | 667 |
/// the two graphs, and it can copy maps for using with the newly created |
668 | 668 |
/// graph. |
669 | 669 |
/// |
670 | 670 |
/// To make a copy from a graph, first an instance of GraphCopy |
671 | 671 |
/// should be created, then the data belongs to the graph should |
672 | 672 |
/// assigned to copy. In the end, the \c run() member should be |
673 | 673 |
/// called. |
674 | 674 |
/// |
675 | 675 |
/// The next code copies a graph with several data: |
676 | 676 |
///\code |
677 | 677 |
/// GraphCopy<OrigGraph, NewGraph> cg(orig_graph, new_graph); |
678 | 678 |
/// // Create references for the nodes |
679 | 679 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
680 | 680 |
/// cg.nodeRef(nr); |
681 | 681 |
/// // Create cross references (inverse) for the edges |
682 | 682 |
/// NewGraph::EdgeMap<OrigGraph::Edge> ecr(new_graph); |
683 | 683 |
/// cg.edgeCrossRef(ecr); |
684 | 684 |
/// // Copy an edge map |
685 | 685 |
/// OrigGraph::EdgeMap<double> oemap(orig_graph); |
686 | 686 |
/// NewGraph::EdgeMap<double> nemap(new_graph); |
687 | 687 |
/// cg.edgeMap(oemap, nemap); |
688 | 688 |
/// // Copy a node |
689 | 689 |
/// OrigGraph::Node on; |
690 | 690 |
/// NewGraph::Node nn; |
691 | 691 |
/// cg.node(on, nn); |
692 | 692 |
/// // Execute copying |
693 | 693 |
/// cg.run(); |
694 | 694 |
///\endcode |
695 | 695 |
template <typename From, typename To> |
696 | 696 |
class GraphCopy { |
697 | 697 |
private: |
698 | 698 |
|
699 | 699 |
typedef typename From::Node Node; |
700 | 700 |
typedef typename From::NodeIt NodeIt; |
701 | 701 |
typedef typename From::Arc Arc; |
702 | 702 |
typedef typename From::ArcIt ArcIt; |
703 | 703 |
typedef typename From::Edge Edge; |
704 | 704 |
typedef typename From::EdgeIt EdgeIt; |
705 | 705 |
|
706 | 706 |
typedef typename To::Node TNode; |
707 | 707 |
typedef typename To::Arc TArc; |
708 | 708 |
typedef typename To::Edge TEdge; |
709 | 709 |
|
710 | 710 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
711 | 711 |
typedef typename From::template EdgeMap<TEdge> EdgeRefMap; |
712 | 712 |
|
713 | 713 |
struct ArcRefMap { |
714 | 714 |
ArcRefMap(const From& from, const To& to, |
715 | 715 |
const EdgeRefMap& edge_ref, const NodeRefMap& node_ref) |
716 | 716 |
: _from(from), _to(to), |
717 | 717 |
_edge_ref(edge_ref), _node_ref(node_ref) {} |
718 | 718 |
|
719 | 719 |
typedef typename From::Arc Key; |
720 | 720 |
typedef typename To::Arc Value; |
721 | 721 |
|
722 | 722 |
Value operator[](const Key& key) const { |
723 | 723 |
bool forward = _from.u(key) != _from.v(key) ? |
724 | 724 |
_node_ref[_from.source(key)] == |
725 | 725 |
_to.source(_to.direct(_edge_ref[key], true)) : |
726 | 726 |
_from.direction(key); |
727 | 727 |
return _to.direct(_edge_ref[key], forward); |
728 | 728 |
} |
729 | 729 |
|
730 | 730 |
const From& _from; |
731 | 731 |
const To& _to; |
732 | 732 |
const EdgeRefMap& _edge_ref; |
733 | 733 |
const NodeRefMap& _node_ref; |
734 | 734 |
}; |
735 | 735 |
|
736 | 736 |
public: |
737 | 737 |
|
738 | 738 |
/// \brief Constructor of GraphCopy. |
739 | 739 |
/// |
740 | 740 |
/// Constructor of GraphCopy for copying the content of the |
741 | 741 |
/// \c from graph into the \c to graph. |
742 | 742 |
GraphCopy(const From& from, To& to) |
743 | 743 |
: _from(from), _to(to) {} |
744 | 744 |
|
745 | 745 |
/// \brief Destructor of GraphCopy |
746 | 746 |
/// |
747 | 747 |
/// Destructor of GraphCopy. |
748 | 748 |
~GraphCopy() { |
749 | 749 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
750 | 750 |
delete _node_maps[i]; |
751 | 751 |
} |
752 | 752 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
753 | 753 |
delete _arc_maps[i]; |
754 | 754 |
} |
755 | 755 |
for (int i = 0; i < int(_edge_maps.size()); ++i) { |
756 | 756 |
delete _edge_maps[i]; |
757 | 757 |
} |
758 | 758 |
} |
759 | 759 |
|
760 | 760 |
/// \brief Copy the node references into the given map. |
761 | 761 |
/// |
762 | 762 |
/// This function copies the node references into the given map. |
763 | 763 |
/// The parameter should be a map, whose key type is the Node type of |
764 | 764 |
/// the source graph, while the value type is the Node type of the |
765 | 765 |
/// destination graph. |
766 | 766 |
template <typename NodeRef> |
767 | 767 |
GraphCopy& nodeRef(NodeRef& map) { |
768 | 768 |
_node_maps.push_back(new _core_bits::RefCopy<From, Node, |
769 | 769 |
NodeRefMap, NodeRef>(map)); |
770 | 770 |
return *this; |
771 | 771 |
} |
772 | 772 |
|
773 | 773 |
/// \brief Copy the node cross references into the given map. |
774 | 774 |
/// |
775 | 775 |
/// This function copies the node cross references (reverse references) |
776 | 776 |
/// into the given map. The parameter should be a map, whose key type |
777 | 777 |
/// is the Node type of the destination graph, while the value type is |
778 | 778 |
/// the Node type of the source graph. |
779 | 779 |
template <typename NodeCrossRef> |
780 | 780 |
GraphCopy& nodeCrossRef(NodeCrossRef& map) { |
781 | 781 |
_node_maps.push_back(new _core_bits::CrossRefCopy<From, Node, |
782 | 782 |
NodeRefMap, NodeCrossRef>(map)); |
783 | 783 |
return *this; |
784 | 784 |
} |
785 | 785 |
|
786 | 786 |
/// \brief Make a copy of the given node map. |
787 | 787 |
/// |
788 | 788 |
/// This function makes a copy of the given node map for the newly |
789 | 789 |
/// created graph. |
790 | 790 |
/// The key type of the new map \c tmap should be the Node type of the |
791 | 791 |
/// destination graph, and the key type of the original map \c map |
792 | 792 |
/// should be the Node type of the source graph. |
793 | 793 |
template <typename FromMap, typename ToMap> |
794 | 794 |
GraphCopy& nodeMap(const FromMap& map, ToMap& tmap) { |
795 | 795 |
_node_maps.push_back(new _core_bits::MapCopy<From, Node, |
796 | 796 |
NodeRefMap, FromMap, ToMap>(map, tmap)); |
797 | 797 |
return *this; |
798 | 798 |
} |
799 | 799 |
|
800 | 800 |
/// \brief Make a copy of the given node. |
801 | 801 |
/// |
802 | 802 |
/// This function makes a copy of the given node. |
803 | 803 |
GraphCopy& node(const Node& node, TNode& tnode) { |
804 | 804 |
_node_maps.push_back(new _core_bits::ItemCopy<From, Node, |
805 | 805 |
NodeRefMap, TNode>(node, tnode)); |
806 | 806 |
return *this; |
807 | 807 |
} |
808 | 808 |
|
809 | 809 |
/// \brief Copy the arc references into the given map. |
810 | 810 |
/// |
811 | 811 |
/// This function copies the arc references into the given map. |
812 | 812 |
/// The parameter should be a map, whose key type is the Arc type of |
813 | 813 |
/// the source graph, while the value type is the Arc type of the |
814 | 814 |
/// destination graph. |
815 | 815 |
template <typename ArcRef> |
816 | 816 |
GraphCopy& arcRef(ArcRef& map) { |
817 | 817 |
_arc_maps.push_back(new _core_bits::RefCopy<From, Arc, |
818 | 818 |
ArcRefMap, ArcRef>(map)); |
819 | 819 |
return *this; |
820 | 820 |
} |
821 | 821 |
|
822 | 822 |
/// \brief Copy the arc cross references into the given map. |
823 | 823 |
/// |
824 | 824 |
/// This function copies the arc cross references (reverse references) |
825 | 825 |
/// into the given map. The parameter should be a map, whose key type |
826 | 826 |
/// is the Arc type of the destination graph, while the value type is |
827 | 827 |
/// the Arc type of the source graph. |
828 | 828 |
template <typename ArcCrossRef> |
829 | 829 |
GraphCopy& arcCrossRef(ArcCrossRef& map) { |
830 | 830 |
_arc_maps.push_back(new _core_bits::CrossRefCopy<From, Arc, |
831 | 831 |
ArcRefMap, ArcCrossRef>(map)); |
832 | 832 |
return *this; |
833 | 833 |
} |
834 | 834 |
|
835 | 835 |
/// \brief Make a copy of the given arc map. |
836 | 836 |
/// |
837 | 837 |
/// This function makes a copy of the given arc map for the newly |
838 | 838 |
/// created graph. |
839 | 839 |
/// The key type of the new map \c tmap should be the Arc type of the |
840 | 840 |
/// destination graph, and the key type of the original map \c map |
841 | 841 |
/// should be the Arc type of the source graph. |
842 | 842 |
template <typename FromMap, typename ToMap> |
843 | 843 |
GraphCopy& arcMap(const FromMap& map, ToMap& tmap) { |
844 | 844 |
_arc_maps.push_back(new _core_bits::MapCopy<From, Arc, |
845 | 845 |
ArcRefMap, FromMap, ToMap>(map, tmap)); |
846 | 846 |
return *this; |
847 | 847 |
} |
848 | 848 |
|
849 | 849 |
/// \brief Make a copy of the given arc. |
850 | 850 |
/// |
851 | 851 |
/// This function makes a copy of the given arc. |
852 | 852 |
GraphCopy& arc(const Arc& arc, TArc& tarc) { |
853 | 853 |
_arc_maps.push_back(new _core_bits::ItemCopy<From, Arc, |
854 | 854 |
ArcRefMap, TArc>(arc, tarc)); |
855 | 855 |
return *this; |
856 | 856 |
} |
857 | 857 |
|
858 | 858 |
/// \brief Copy the edge references into the given map. |
859 | 859 |
/// |
860 | 860 |
/// This function copies the edge references into the given map. |
861 | 861 |
/// The parameter should be a map, whose key type is the Edge type of |
862 | 862 |
/// the source graph, while the value type is the Edge type of the |
863 | 863 |
/// destination graph. |
864 | 864 |
template <typename EdgeRef> |
865 | 865 |
GraphCopy& edgeRef(EdgeRef& map) { |
866 | 866 |
_edge_maps.push_back(new _core_bits::RefCopy<From, Edge, |
867 | 867 |
EdgeRefMap, EdgeRef>(map)); |
868 | 868 |
return *this; |
869 | 869 |
} |
870 | 870 |
|
871 | 871 |
/// \brief Copy the edge cross references into the given map. |
872 | 872 |
/// |
873 | 873 |
/// This function copies the edge cross references (reverse references) |
874 | 874 |
/// into the given map. The parameter should be a map, whose key type |
875 | 875 |
/// is the Edge type of the destination graph, while the value type is |
876 | 876 |
/// the Edge type of the source graph. |
877 | 877 |
template <typename EdgeCrossRef> |
878 | 878 |
GraphCopy& edgeCrossRef(EdgeCrossRef& map) { |
879 | 879 |
_edge_maps.push_back(new _core_bits::CrossRefCopy<From, |
880 | 880 |
Edge, EdgeRefMap, EdgeCrossRef>(map)); |
881 | 881 |
return *this; |
882 | 882 |
} |
883 | 883 |
|
884 | 884 |
/// \brief Make a copy of the given edge map. |
885 | 885 |
/// |
886 | 886 |
/// This function makes a copy of the given edge map for the newly |
887 | 887 |
/// created graph. |
888 | 888 |
/// The key type of the new map \c tmap should be the Edge type of the |
889 | 889 |
/// destination graph, and the key type of the original map \c map |
890 | 890 |
/// should be the Edge type of the source graph. |
891 | 891 |
template <typename FromMap, typename ToMap> |
892 | 892 |
GraphCopy& edgeMap(const FromMap& map, ToMap& tmap) { |
893 | 893 |
_edge_maps.push_back(new _core_bits::MapCopy<From, Edge, |
894 | 894 |
EdgeRefMap, FromMap, ToMap>(map, tmap)); |
895 | 895 |
return *this; |
896 | 896 |
} |
897 | 897 |
|
898 | 898 |
/// \brief Make a copy of the given edge. |
899 | 899 |
/// |
900 | 900 |
/// This function makes a copy of the given edge. |
901 | 901 |
GraphCopy& edge(const Edge& edge, TEdge& tedge) { |
902 | 902 |
_edge_maps.push_back(new _core_bits::ItemCopy<From, Edge, |
903 | 903 |
EdgeRefMap, TEdge>(edge, tedge)); |
904 | 904 |
return *this; |
905 | 905 |
} |
906 | 906 |
|
907 | 907 |
/// \brief Execute copying. |
908 | 908 |
/// |
909 | 909 |
/// This function executes the copying of the graph along with the |
910 | 910 |
/// copying of the assigned data. |
911 | 911 |
void run() { |
912 | 912 |
NodeRefMap nodeRefMap(_from); |
913 | 913 |
EdgeRefMap edgeRefMap(_from); |
914 | 914 |
ArcRefMap arcRefMap(_from, _to, edgeRefMap, nodeRefMap); |
915 | 915 |
_core_bits::GraphCopySelector<To>:: |
916 | 916 |
copy(_from, _to, nodeRefMap, edgeRefMap); |
917 | 917 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
918 | 918 |
_node_maps[i]->copy(_from, nodeRefMap); |
919 | 919 |
} |
920 | 920 |
for (int i = 0; i < int(_edge_maps.size()); ++i) { |
921 | 921 |
_edge_maps[i]->copy(_from, edgeRefMap); |
922 | 922 |
} |
923 | 923 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
924 | 924 |
_arc_maps[i]->copy(_from, arcRefMap); |
925 | 925 |
} |
926 | 926 |
} |
927 | 927 |
|
928 | 928 |
private: |
929 | 929 |
|
930 | 930 |
const From& _from; |
931 | 931 |
To& _to; |
932 | 932 |
|
933 | 933 |
std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* > |
934 | 934 |
_node_maps; |
935 | 935 |
|
936 | 936 |
std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
937 | 937 |
_arc_maps; |
938 | 938 |
|
939 | 939 |
std::vector<_core_bits::MapCopyBase<From, Edge, EdgeRefMap>* > |
940 | 940 |
_edge_maps; |
941 | 941 |
|
942 | 942 |
}; |
943 | 943 |
|
944 | 944 |
/// \brief Copy a graph to another graph. |
945 | 945 |
/// |
946 | 946 |
/// This function copies a graph to another graph. |
947 | 947 |
/// The complete usage of it is detailed in the GraphCopy class, |
948 | 948 |
/// but a short example shows a basic work: |
949 | 949 |
///\code |
950 | 950 |
/// graphCopy(src, trg).nodeRef(nr).edgeCrossRef(ecr).run(); |
951 | 951 |
///\endcode |
952 | 952 |
/// |
953 | 953 |
/// After the copy the \c nr map will contain the mapping from the |
954 | 954 |
/// nodes of the \c from graph to the nodes of the \c to graph and |
955 | 955 |
/// \c ecr will contain the mapping from the edges of the \c to graph |
956 | 956 |
/// to the edges of the \c from graph. |
957 | 957 |
/// |
958 | 958 |
/// \see GraphCopy |
959 | 959 |
template <typename From, typename To> |
960 | 960 |
GraphCopy<From, To> |
961 | 961 |
graphCopy(const From& from, To& to) { |
962 | 962 |
return GraphCopy<From, To>(from, to); |
963 | 963 |
} |
964 | 964 |
|
965 | 965 |
namespace _core_bits { |
966 | 966 |
|
967 | 967 |
template <typename Graph, typename Enable = void> |
968 | 968 |
struct FindArcSelector { |
969 | 969 |
typedef typename Graph::Node Node; |
970 | 970 |
typedef typename Graph::Arc Arc; |
971 | 971 |
static Arc find(const Graph &g, Node u, Node v, Arc e) { |
972 | 972 |
if (e == INVALID) { |
973 | 973 |
g.firstOut(e, u); |
974 | 974 |
} else { |
975 | 975 |
g.nextOut(e); |
976 | 976 |
} |
977 | 977 |
while (e != INVALID && g.target(e) != v) { |
978 | 978 |
g.nextOut(e); |
979 | 979 |
} |
980 | 980 |
return e; |
981 | 981 |
} |
982 | 982 |
}; |
983 | 983 |
|
984 | 984 |
template <typename Graph> |
985 | 985 |
struct FindArcSelector< |
986 | 986 |
Graph, |
987 | 987 |
typename enable_if<typename Graph::FindArcTag, void>::type> |
988 | 988 |
{ |
989 | 989 |
typedef typename Graph::Node Node; |
990 | 990 |
typedef typename Graph::Arc Arc; |
991 | 991 |
static Arc find(const Graph &g, Node u, Node v, Arc prev) { |
992 | 992 |
return g.findArc(u, v, prev); |
993 | 993 |
} |
994 | 994 |
}; |
995 | 995 |
} |
996 | 996 |
|
997 | 997 |
/// \brief Find an arc between two nodes of a digraph. |
998 | 998 |
/// |
999 | 999 |
/// This function finds an arc from node \c u to node \c v in the |
1000 | 1000 |
/// digraph \c g. |
1001 | 1001 |
/// |
1002 | 1002 |
/// If \c prev is \ref INVALID (this is the default value), then |
1003 | 1003 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
1004 | 1004 |
/// the next arc from \c u to \c v after \c prev. |
1005 | 1005 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
1006 | 1006 |
/// |
1007 | 1007 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
1008 | 1008 |
///\code |
1009 | 1009 |
/// for(Arc e = findArc(g,u,v); e != INVALID; e = findArc(g,u,v,e)) { |
1010 | 1010 |
/// ... |
1011 | 1011 |
/// } |
1012 | 1012 |
///\endcode |
1013 | 1013 |
/// |
1014 | 1014 |
/// \note \ref ConArcIt provides iterator interface for the same |
1015 | 1015 |
/// functionality. |
1016 | 1016 |
/// |
1017 | 1017 |
///\sa ConArcIt |
1018 | 1018 |
///\sa ArcLookUp, AllArcLookUp, DynArcLookUp |
1019 | 1019 |
template <typename Graph> |
1020 | 1020 |
inline typename Graph::Arc |
1021 | 1021 |
findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
1022 | 1022 |
typename Graph::Arc prev = INVALID) { |
1023 | 1023 |
return _core_bits::FindArcSelector<Graph>::find(g, u, v, prev); |
1024 | 1024 |
} |
1025 | 1025 |
|
1026 | 1026 |
/// \brief Iterator for iterating on parallel arcs connecting the same nodes. |
1027 | 1027 |
/// |
1028 | 1028 |
/// Iterator for iterating on parallel arcs connecting the same nodes. It is |
1029 | 1029 |
/// a higher level interface for the \ref findArc() function. You can |
1030 | 1030 |
/// use it the following way: |
1031 | 1031 |
///\code |
1032 | 1032 |
/// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) { |
1033 | 1033 |
/// ... |
1034 | 1034 |
/// } |
1035 | 1035 |
///\endcode |
1036 | 1036 |
/// |
1037 | 1037 |
///\sa findArc() |
1038 | 1038 |
///\sa ArcLookUp, AllArcLookUp, DynArcLookUp |
1039 | 1039 |
template <typename _Graph> |
1040 | 1040 |
class ConArcIt : public _Graph::Arc { |
1041 | 1041 |
public: |
1042 | 1042 |
|
1043 | 1043 |
typedef _Graph Graph; |
1044 | 1044 |
typedef typename Graph::Arc Parent; |
1045 | 1045 |
|
1046 | 1046 |
typedef typename Graph::Arc Arc; |
1047 | 1047 |
typedef typename Graph::Node Node; |
1048 | 1048 |
|
1049 | 1049 |
/// \brief Constructor. |
1050 | 1050 |
/// |
1051 | 1051 |
/// Construct a new ConArcIt iterating on the arcs that |
1052 | 1052 |
/// connects nodes \c u and \c v. |
1053 | 1053 |
ConArcIt(const Graph& g, Node u, Node v) : _graph(g) { |
1054 | 1054 |
Parent::operator=(findArc(_graph, u, v)); |
1055 | 1055 |
} |
1056 | 1056 |
|
1057 | 1057 |
/// \brief Constructor. |
1058 | 1058 |
/// |
1059 | 1059 |
/// Construct a new ConArcIt that continues the iterating from arc \c a. |
1060 | 1060 |
ConArcIt(const Graph& g, Arc a) : Parent(a), _graph(g) {} |
1061 | 1061 |
|
1062 | 1062 |
/// \brief Increment operator. |
1063 | 1063 |
/// |
1064 | 1064 |
/// It increments the iterator and gives back the next arc. |
1065 | 1065 |
ConArcIt& operator++() { |
1066 | 1066 |
Parent::operator=(findArc(_graph, _graph.source(*this), |
1067 | 1067 |
_graph.target(*this), *this)); |
1068 | 1068 |
return *this; |
1069 | 1069 |
} |
1070 | 1070 |
private: |
1071 | 1071 |
const Graph& _graph; |
1072 | 1072 |
}; |
1073 | 1073 |
|
1074 | 1074 |
namespace _core_bits { |
1075 | 1075 |
|
1076 | 1076 |
template <typename Graph, typename Enable = void> |
1077 | 1077 |
struct FindEdgeSelector { |
1078 | 1078 |
typedef typename Graph::Node Node; |
1079 | 1079 |
typedef typename Graph::Edge Edge; |
1080 | 1080 |
static Edge find(const Graph &g, Node u, Node v, Edge e) { |
1081 | 1081 |
bool b; |
1082 | 1082 |
if (u != v) { |
1083 | 1083 |
if (e == INVALID) { |
1084 | 1084 |
g.firstInc(e, b, u); |
1085 | 1085 |
} else { |
1086 | 1086 |
b = g.u(e) == u; |
1087 | 1087 |
g.nextInc(e, b); |
1088 | 1088 |
} |
1089 | 1089 |
while (e != INVALID && (b ? g.v(e) : g.u(e)) != v) { |
1090 | 1090 |
g.nextInc(e, b); |
1091 | 1091 |
} |
1092 | 1092 |
} else { |
1093 | 1093 |
if (e == INVALID) { |
1094 | 1094 |
g.firstInc(e, b, u); |
1095 | 1095 |
} else { |
1096 | 1096 |
b = true; |
1097 | 1097 |
g.nextInc(e, b); |
1098 | 1098 |
} |
1099 | 1099 |
while (e != INVALID && (!b || g.v(e) != v)) { |
1100 | 1100 |
g.nextInc(e, b); |
1101 | 1101 |
} |
1102 | 1102 |
} |
1103 | 1103 |
return e; |
1104 | 1104 |
} |
1105 | 1105 |
}; |
1106 | 1106 |
|
1107 | 1107 |
template <typename Graph> |
1108 | 1108 |
struct FindEdgeSelector< |
1109 | 1109 |
Graph, |
1110 | 1110 |
typename enable_if<typename Graph::FindEdgeTag, void>::type> |
1111 | 1111 |
{ |
1112 | 1112 |
typedef typename Graph::Node Node; |
1113 | 1113 |
typedef typename Graph::Edge Edge; |
1114 | 1114 |
static Edge find(const Graph &g, Node u, Node v, Edge prev) { |
1115 | 1115 |
return g.findEdge(u, v, prev); |
1116 | 1116 |
} |
1117 | 1117 |
}; |
1118 | 1118 |
} |
1119 | 1119 |
|
1120 | 1120 |
/// \brief Find an edge between two nodes of a graph. |
1121 | 1121 |
/// |
1122 | 1122 |
/// This function finds an edge from node \c u to node \c v in graph \c g. |
1123 | 1123 |
/// If node \c u and node \c v is equal then each loop edge |
1124 | 1124 |
/// will be enumerated once. |
1125 | 1125 |
/// |
1126 | 1126 |
/// If \c prev is \ref INVALID (this is the default value), then |
1127 | 1127 |
/// it finds the first edge from \c u to \c v. Otherwise it looks for |
1128 | 1128 |
/// the next edge from \c u to \c v after \c prev. |
1129 | 1129 |
/// \return The found edge or \ref INVALID if there is no such an edge. |
1130 | 1130 |
/// |
1131 | 1131 |
/// Thus you can iterate through each edge between \c u and \c v |
1132 | 1132 |
/// as it follows. |
1133 | 1133 |
///\code |
1134 | 1134 |
/// for(Edge e = findEdge(g,u,v); e != INVALID; e = findEdge(g,u,v,e)) { |
1135 | 1135 |
/// ... |
1136 | 1136 |
/// } |
1137 | 1137 |
///\endcode |
1138 | 1138 |
/// |
1139 | 1139 |
/// \note \ref ConEdgeIt provides iterator interface for the same |
1140 | 1140 |
/// functionality. |
1141 | 1141 |
/// |
1142 | 1142 |
///\sa ConEdgeIt |
1143 | 1143 |
template <typename Graph> |
1144 | 1144 |
inline typename Graph::Edge |
1145 | 1145 |
findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
1146 | 1146 |
typename Graph::Edge p = INVALID) { |
1147 | 1147 |
return _core_bits::FindEdgeSelector<Graph>::find(g, u, v, p); |
1148 | 1148 |
} |
1149 | 1149 |
|
1150 | 1150 |
/// \brief Iterator for iterating on parallel edges connecting the same nodes. |
1151 | 1151 |
/// |
1152 | 1152 |
/// Iterator for iterating on parallel edges connecting the same nodes. |
1153 | 1153 |
/// It is a higher level interface for the findEdge() function. You can |
1154 | 1154 |
/// use it the following way: |
1155 | 1155 |
///\code |
1156 | 1156 |
/// for (ConEdgeIt<Graph> it(g, u, v); it != INVALID; ++it) { |
1157 | 1157 |
/// ... |
1158 | 1158 |
/// } |
1159 | 1159 |
///\endcode |
1160 | 1160 |
/// |
1161 | 1161 |
///\sa findEdge() |
1162 | 1162 |
template <typename _Graph> |
1163 | 1163 |
class ConEdgeIt : public _Graph::Edge { |
1164 | 1164 |
public: |
1165 | 1165 |
|
1166 | 1166 |
typedef _Graph Graph; |
1167 | 1167 |
typedef typename Graph::Edge Parent; |
1168 | 1168 |
|
1169 | 1169 |
typedef typename Graph::Edge Edge; |
1170 | 1170 |
typedef typename Graph::Node Node; |
1171 | 1171 |
|
1172 | 1172 |
/// \brief Constructor. |
1173 | 1173 |
/// |
1174 | 1174 |
/// Construct a new ConEdgeIt iterating on the edges that |
1175 | 1175 |
/// connects nodes \c u and \c v. |
1176 | 1176 |
ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g), _u(u), _v(v) { |
1177 | 1177 |
Parent::operator=(findEdge(_graph, _u, _v)); |
1178 | 1178 |
} |
1179 | 1179 |
|
1180 | 1180 |
/// \brief Constructor. |
1181 | 1181 |
/// |
1182 | 1182 |
/// Construct a new ConEdgeIt that continues iterating from edge \c e. |
1183 | 1183 |
ConEdgeIt(const Graph& g, Edge e) : Parent(e), _graph(g) {} |
1184 | 1184 |
|
1185 | 1185 |
/// \brief Increment operator. |
1186 | 1186 |
/// |
1187 | 1187 |
/// It increments the iterator and gives back the next edge. |
1188 | 1188 |
ConEdgeIt& operator++() { |
1189 | 1189 |
Parent::operator=(findEdge(_graph, _u, _v, *this)); |
1190 | 1190 |
return *this; |
1191 | 1191 |
} |
1192 | 1192 |
private: |
1193 | 1193 |
const Graph& _graph; |
1194 | 1194 |
Node _u, _v; |
1195 | 1195 |
}; |
1196 | 1196 |
|
1197 | 1197 |
|
1198 | 1198 |
///Dynamic arc look-up between given endpoints. |
1199 | 1199 |
|
1200 | 1200 |
///Using this class, you can find an arc in a digraph from a given |
1201 | 1201 |
///source to a given target in amortized time <em>O</em>(log<em>d</em>), |
1202 | 1202 |
///where <em>d</em> is the out-degree of the source node. |
1203 | 1203 |
/// |
1204 | 1204 |
///It is possible to find \e all parallel arcs between two nodes with |
1205 | 1205 |
///the \c operator() member. |
1206 | 1206 |
/// |
1207 | 1207 |
///This is a dynamic data structure. Consider to use \ref ArcLookUp or |
1208 | 1208 |
///\ref AllArcLookUp if your digraph is not changed so frequently. |
1209 | 1209 |
/// |
1210 | 1210 |
///This class uses a self-adjusting binary search tree, the Splay tree |
1211 | 1211 |
///of Sleator and Tarjan to guarantee the logarithmic amortized |
1212 | 1212 |
///time bound for arc look-ups. This class also guarantees the |
1213 | 1213 |
///optimal time bound in a constant factor for any distribution of |
1214 | 1214 |
///queries. |
1215 | 1215 |
/// |
1216 | 1216 |
///\tparam G The type of the underlying digraph. |
1217 | 1217 |
/// |
1218 | 1218 |
///\sa ArcLookUp |
1219 | 1219 |
///\sa AllArcLookUp |
1220 | 1220 |
template<class G> |
1221 | 1221 |
class DynArcLookUp |
1222 | 1222 |
: protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase |
1223 | 1223 |
{ |
1224 | 1224 |
public: |
1225 | 1225 |
typedef typename ItemSetTraits<G, typename G::Arc> |
1226 | 1226 |
::ItemNotifier::ObserverBase Parent; |
1227 | 1227 |
|
1228 | 1228 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
1229 | 1229 |
typedef G Digraph; |
1230 | 1230 |
|
1231 | 1231 |
protected: |
1232 | 1232 |
|
1233 | 1233 |
class AutoNodeMap : public ItemSetTraits<G, Node>::template Map<Arc>::Type { |
1234 | 1234 |
public: |
1235 | 1235 |
|
1236 | 1236 |
typedef typename ItemSetTraits<G, Node>::template Map<Arc>::Type Parent; |
1237 | 1237 |
|
1238 | 1238 |
AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {} |
1239 | 1239 |
|
1240 | 1240 |
virtual void add(const Node& node) { |
1241 | 1241 |
Parent::add(node); |
1242 | 1242 |
Parent::set(node, INVALID); |
1243 | 1243 |
} |
1244 | 1244 |
|
1245 | 1245 |
virtual void add(const std::vector<Node>& nodes) { |
1246 | 1246 |
Parent::add(nodes); |
1247 | 1247 |
for (int i = 0; i < int(nodes.size()); ++i) { |
1248 | 1248 |
Parent::set(nodes[i], INVALID); |
1249 | 1249 |
} |
1250 | 1250 |
} |
1251 | 1251 |
|
1252 | 1252 |
virtual void build() { |
1253 | 1253 |
Parent::build(); |
1254 | 1254 |
Node it; |
1255 | 1255 |
typename Parent::Notifier* nf = Parent::notifier(); |
1256 | 1256 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
1257 | 1257 |
Parent::set(it, INVALID); |
1258 | 1258 |
} |
1259 | 1259 |
} |
1260 | 1260 |
}; |
1261 | 1261 |
|
1262 | 1262 |
const Digraph &_g; |
1263 | 1263 |
AutoNodeMap _head; |
1264 | 1264 |
typename Digraph::template ArcMap<Arc> _parent; |
1265 | 1265 |
typename Digraph::template ArcMap<Arc> _left; |
1266 | 1266 |
typename Digraph::template ArcMap<Arc> _right; |
1267 | 1267 |
|
1268 | 1268 |
class ArcLess { |
1269 | 1269 |
const Digraph &g; |
1270 | 1270 |
public: |
1271 | 1271 |
ArcLess(const Digraph &_g) : g(_g) {} |
1272 | 1272 |
bool operator()(Arc a,Arc b) const |
1273 | 1273 |
{ |
1274 | 1274 |
return g.target(a)<g.target(b); |
1275 | 1275 |
} |
1276 | 1276 |
}; |
1277 | 1277 |
|
1278 | 1278 |
public: |
1279 | 1279 |
|
1280 | 1280 |
///Constructor |
1281 | 1281 |
|
1282 | 1282 |
///Constructor. |
1283 | 1283 |
/// |
1284 | 1284 |
///It builds up the search database. |
1285 | 1285 |
DynArcLookUp(const Digraph &g) |
1286 | 1286 |
: _g(g),_head(g),_parent(g),_left(g),_right(g) |
1287 | 1287 |
{ |
1288 | 1288 |
Parent::attach(_g.notifier(typename Digraph::Arc())); |
1289 | 1289 |
refresh(); |
1290 | 1290 |
} |
1291 | 1291 |
|
1292 | 1292 |
protected: |
1293 | 1293 |
|
1294 | 1294 |
virtual void add(const Arc& arc) { |
1295 | 1295 |
insert(arc); |
1296 | 1296 |
} |
1297 | 1297 |
|
1298 | 1298 |
virtual void add(const std::vector<Arc>& arcs) { |
1299 | 1299 |
for (int i = 0; i < int(arcs.size()); ++i) { |
1300 | 1300 |
insert(arcs[i]); |
1301 | 1301 |
} |
1302 | 1302 |
} |
1303 | 1303 |
|
1304 | 1304 |
virtual void erase(const Arc& arc) { |
1305 | 1305 |
remove(arc); |
1306 | 1306 |
} |
1307 | 1307 |
|
1308 | 1308 |
virtual void erase(const std::vector<Arc>& arcs) { |
1309 | 1309 |
for (int i = 0; i < int(arcs.size()); ++i) { |
1310 | 1310 |
remove(arcs[i]); |
1311 | 1311 |
} |
1312 | 1312 |
} |
1313 | 1313 |
|
1314 | 1314 |
virtual void build() { |
1315 | 1315 |
refresh(); |
1316 | 1316 |
} |
1317 | 1317 |
|
1318 | 1318 |
virtual void clear() { |
1319 | 1319 |
for(NodeIt n(_g);n!=INVALID;++n) { |
1320 | 1320 |
_head.set(n, INVALID); |
1321 | 1321 |
} |
1322 | 1322 |
} |
1323 | 1323 |
|
1324 | 1324 |
void insert(Arc arc) { |
1325 | 1325 |
Node s = _g.source(arc); |
1326 | 1326 |
Node t = _g.target(arc); |
1327 | 1327 |
_left.set(arc, INVALID); |
1328 | 1328 |
_right.set(arc, INVALID); |
1329 | 1329 |
|
1330 | 1330 |
Arc e = _head[s]; |
1331 | 1331 |
if (e == INVALID) { |
1332 | 1332 |
_head.set(s, arc); |
1333 | 1333 |
_parent.set(arc, INVALID); |
1334 | 1334 |
return; |
1335 | 1335 |
} |
1336 | 1336 |
while (true) { |
1337 | 1337 |
if (t < _g.target(e)) { |
1338 | 1338 |
if (_left[e] == INVALID) { |
1339 | 1339 |
_left.set(e, arc); |
1340 | 1340 |
_parent.set(arc, e); |
1341 | 1341 |
splay(arc); |
1342 | 1342 |
return; |
1343 | 1343 |
} else { |
1344 | 1344 |
e = _left[e]; |
1345 | 1345 |
} |
1346 | 1346 |
} else { |
1347 | 1347 |
if (_right[e] == INVALID) { |
1348 | 1348 |
_right.set(e, arc); |
1349 | 1349 |
_parent.set(arc, e); |
1350 | 1350 |
splay(arc); |
1351 | 1351 |
return; |
1352 | 1352 |
} else { |
1353 | 1353 |
e = _right[e]; |
1354 | 1354 |
} |
1355 | 1355 |
} |
1356 | 1356 |
} |
1357 | 1357 |
} |
1358 | 1358 |
|
1359 | 1359 |
void remove(Arc arc) { |
1360 | 1360 |
if (_left[arc] == INVALID) { |
1361 | 1361 |
if (_right[arc] != INVALID) { |
1362 | 1362 |
_parent.set(_right[arc], _parent[arc]); |
1363 | 1363 |
} |
1364 | 1364 |
if (_parent[arc] != INVALID) { |
1365 | 1365 |
if (_left[_parent[arc]] == arc) { |
1366 | 1366 |
_left.set(_parent[arc], _right[arc]); |
1367 | 1367 |
} else { |
1368 | 1368 |
_right.set(_parent[arc], _right[arc]); |
1369 | 1369 |
} |
1370 | 1370 |
} else { |
1371 | 1371 |
_head.set(_g.source(arc), _right[arc]); |
1372 | 1372 |
} |
1373 | 1373 |
} else if (_right[arc] == INVALID) { |
1374 | 1374 |
_parent.set(_left[arc], _parent[arc]); |
1375 | 1375 |
if (_parent[arc] != INVALID) { |
1376 | 1376 |
if (_left[_parent[arc]] == arc) { |
1377 | 1377 |
_left.set(_parent[arc], _left[arc]); |
1378 | 1378 |
} else { |
1379 | 1379 |
_right.set(_parent[arc], _left[arc]); |
1380 | 1380 |
} |
1381 | 1381 |
} else { |
1382 | 1382 |
_head.set(_g.source(arc), _left[arc]); |
1383 | 1383 |
} |
1384 | 1384 |
} else { |
1385 | 1385 |
Arc e = _left[arc]; |
1386 | 1386 |
if (_right[e] != INVALID) { |
1387 | 1387 |
e = _right[e]; |
1388 | 1388 |
while (_right[e] != INVALID) { |
1389 | 1389 |
e = _right[e]; |
1390 | 1390 |
} |
1391 | 1391 |
Arc s = _parent[e]; |
1392 | 1392 |
_right.set(_parent[e], _left[e]); |
1393 | 1393 |
if (_left[e] != INVALID) { |
1394 | 1394 |
_parent.set(_left[e], _parent[e]); |
1395 | 1395 |
} |
1396 | 1396 |
|
1397 | 1397 |
_left.set(e, _left[arc]); |
1398 | 1398 |
_parent.set(_left[arc], e); |
1399 | 1399 |
_right.set(e, _right[arc]); |
1400 | 1400 |
_parent.set(_right[arc], e); |
1401 | 1401 |
|
1402 | 1402 |
_parent.set(e, _parent[arc]); |
1403 | 1403 |
if (_parent[arc] != INVALID) { |
1404 | 1404 |
if (_left[_parent[arc]] == arc) { |
1405 | 1405 |
_left.set(_parent[arc], e); |
1406 | 1406 |
} else { |
1407 | 1407 |
_right.set(_parent[arc], e); |
1408 | 1408 |
} |
1409 | 1409 |
} |
1410 | 1410 |
splay(s); |
1411 | 1411 |
} else { |
1412 | 1412 |
_right.set(e, _right[arc]); |
1413 | 1413 |
_parent.set(_right[arc], e); |
1414 | 1414 |
_parent.set(e, _parent[arc]); |
1415 | 1415 |
|
1416 | 1416 |
if (_parent[arc] != INVALID) { |
1417 | 1417 |
if (_left[_parent[arc]] == arc) { |
1418 | 1418 |
_left.set(_parent[arc], e); |
1419 | 1419 |
} else { |
1420 | 1420 |
_right.set(_parent[arc], e); |
1421 | 1421 |
} |
1422 | 1422 |
} else { |
1423 | 1423 |
_head.set(_g.source(arc), e); |
1424 | 1424 |
} |
1425 | 1425 |
} |
1426 | 1426 |
} |
1427 | 1427 |
} |
1428 | 1428 |
|
1429 | 1429 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
1430 | 1430 |
{ |
1431 | 1431 |
int m=(a+b)/2; |
1432 | 1432 |
Arc me=v[m]; |
1433 | 1433 |
if (a < m) { |
1434 | 1434 |
Arc left = refreshRec(v,a,m-1); |
1435 | 1435 |
_left.set(me, left); |
1436 | 1436 |
_parent.set(left, me); |
1437 | 1437 |
} else { |
1438 | 1438 |
_left.set(me, INVALID); |
1439 | 1439 |
} |
1440 | 1440 |
if (m < b) { |
1441 | 1441 |
Arc right = refreshRec(v,m+1,b); |
1442 | 1442 |
_right.set(me, right); |
1443 | 1443 |
_parent.set(right, me); |
1444 | 1444 |
} else { |
1445 | 1445 |
_right.set(me, INVALID); |
1446 | 1446 |
} |
1447 | 1447 |
return me; |
1448 | 1448 |
} |
1449 | 1449 |
|
1450 | 1450 |
void refresh() { |
1451 | 1451 |
for(NodeIt n(_g);n!=INVALID;++n) { |
1452 | 1452 |
std::vector<Arc> v; |
1453 | 1453 |
for(OutArcIt a(_g,n);a!=INVALID;++a) v.push_back(a); |
1454 | 1454 |
if (!v.empty()) { |
1455 | 1455 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
1456 | 1456 |
Arc head = refreshRec(v,0,v.size()-1); |
1457 | 1457 |
_head.set(n, head); |
1458 | 1458 |
_parent.set(head, INVALID); |
1459 | 1459 |
} |
1460 | 1460 |
else _head.set(n, INVALID); |
1461 | 1461 |
} |
1462 | 1462 |
} |
1463 | 1463 |
|
1464 | 1464 |
void zig(Arc v) { |
1465 | 1465 |
Arc w = _parent[v]; |
1466 | 1466 |
_parent.set(v, _parent[w]); |
1467 | 1467 |
_parent.set(w, v); |
1468 | 1468 |
_left.set(w, _right[v]); |
1469 | 1469 |
_right.set(v, w); |
1470 | 1470 |
if (_parent[v] != INVALID) { |
1471 | 1471 |
if (_right[_parent[v]] == w) { |
1472 | 1472 |
_right.set(_parent[v], v); |
1473 | 1473 |
} else { |
1474 | 1474 |
_left.set(_parent[v], v); |
1475 | 1475 |
} |
1476 | 1476 |
} |
1477 | 1477 |
if (_left[w] != INVALID){ |
1478 | 1478 |
_parent.set(_left[w], w); |
1479 | 1479 |
} |
1480 | 1480 |
} |
1481 | 1481 |
|
1482 | 1482 |
void zag(Arc v) { |
1483 | 1483 |
Arc w = _parent[v]; |
1484 | 1484 |
_parent.set(v, _parent[w]); |
1485 | 1485 |
_parent.set(w, v); |
1486 | 1486 |
_right.set(w, _left[v]); |
1487 | 1487 |
_left.set(v, w); |
1488 | 1488 |
if (_parent[v] != INVALID){ |
1489 | 1489 |
if (_left[_parent[v]] == w) { |
1490 | 1490 |
_left.set(_parent[v], v); |
1491 | 1491 |
} else { |
1492 | 1492 |
_right.set(_parent[v], v); |
1493 | 1493 |
} |
1494 | 1494 |
} |
1495 | 1495 |
if (_right[w] != INVALID){ |
1496 | 1496 |
_parent.set(_right[w], w); |
1497 | 1497 |
} |
1498 | 1498 |
} |
1499 | 1499 |
|
1500 | 1500 |
void splay(Arc v) { |
1501 | 1501 |
while (_parent[v] != INVALID) { |
1502 | 1502 |
if (v == _left[_parent[v]]) { |
1503 | 1503 |
if (_parent[_parent[v]] == INVALID) { |
1504 | 1504 |
zig(v); |
1505 | 1505 |
} else { |
1506 | 1506 |
if (_parent[v] == _left[_parent[_parent[v]]]) { |
1507 | 1507 |
zig(_parent[v]); |
1508 | 1508 |
zig(v); |
1509 | 1509 |
} else { |
1510 | 1510 |
zig(v); |
1511 | 1511 |
zag(v); |
1512 | 1512 |
} |
1513 | 1513 |
} |
1514 | 1514 |
} else { |
1515 | 1515 |
if (_parent[_parent[v]] == INVALID) { |
1516 | 1516 |
zag(v); |
1517 | 1517 |
} else { |
1518 | 1518 |
if (_parent[v] == _left[_parent[_parent[v]]]) { |
1519 | 1519 |
zag(v); |
1520 | 1520 |
zig(v); |
1521 | 1521 |
} else { |
1522 | 1522 |
zag(_parent[v]); |
1523 | 1523 |
zag(v); |
1524 | 1524 |
} |
1525 | 1525 |
} |
1526 | 1526 |
} |
1527 | 1527 |
} |
1528 | 1528 |
_head[_g.source(v)] = v; |
1529 | 1529 |
} |
1530 | 1530 |
|
1531 | 1531 |
|
1532 | 1532 |
public: |
1533 | 1533 |
|
1534 | 1534 |
///Find an arc between two nodes. |
1535 | 1535 |
|
1536 | 1536 |
///Find an arc between two nodes. |
1537 | 1537 |
///\param s The source node. |
1538 | 1538 |
///\param t The target node. |
1539 | 1539 |
///\param p The previous arc between \c s and \c t. It it is INVALID or |
1540 | 1540 |
///not given, the operator finds the first appropriate arc. |
1541 | 1541 |
///\return An arc from \c s to \c t after \c p or |
1542 | 1542 |
///\ref INVALID if there is no more. |
1543 | 1543 |
/// |
1544 | 1544 |
///For example, you can count the number of arcs from \c u to \c v in the |
1545 | 1545 |
///following way. |
1546 | 1546 |
///\code |
1547 | 1547 |
///DynArcLookUp<ListDigraph> ae(g); |
1548 | 1548 |
///... |
1549 | 1549 |
///int n = 0; |
1550 | 1550 |
///for(Arc a = ae(u,v); a != INVALID; a = ae(u,v,a)) n++; |
1551 | 1551 |
///\endcode |
1552 | 1552 |
/// |
1553 | 1553 |
///Finding the arcs take at most <em>O</em>(log<em>d</em>) |
1554 | 1554 |
///amortized time, specifically, the time complexity of the lookups |
1555 | 1555 |
///is equal to the optimal search tree implementation for the |
1556 | 1556 |
///current query distribution in a constant factor. |
1557 | 1557 |
/// |
1558 | 1558 |
///\note This is a dynamic data structure, therefore the data |
1559 | 1559 |
///structure is updated after each graph alteration. Thus although |
1560 | 1560 |
///this data structure is theoretically faster than \ref ArcLookUp |
1561 | 1561 |
///and \ref AllArcLookUp, it often provides worse performance than |
1562 | 1562 |
///them. |
1563 | 1563 |
Arc operator()(Node s, Node t, Arc p = INVALID) const { |
1564 | 1564 |
if (p == INVALID) { |
1565 | 1565 |
Arc a = _head[s]; |
1566 | 1566 |
if (a == INVALID) return INVALID; |
1567 | 1567 |
Arc r = INVALID; |
1568 | 1568 |
while (true) { |
1569 | 1569 |
if (_g.target(a) < t) { |
1570 | 1570 |
if (_right[a] == INVALID) { |
1571 | 1571 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1572 | 1572 |
return r; |
1573 | 1573 |
} else { |
1574 | 1574 |
a = _right[a]; |
1575 | 1575 |
} |
1576 | 1576 |
} else { |
1577 | 1577 |
if (_g.target(a) == t) { |
1578 | 1578 |
r = a; |
1579 | 1579 |
} |
1580 | 1580 |
if (_left[a] == INVALID) { |
1581 | 1581 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1582 | 1582 |
return r; |
1583 | 1583 |
} else { |
1584 | 1584 |
a = _left[a]; |
1585 | 1585 |
} |
1586 | 1586 |
} |
1587 | 1587 |
} |
1588 | 1588 |
} else { |
1589 | 1589 |
Arc a = p; |
1590 | 1590 |
if (_right[a] != INVALID) { |
1591 | 1591 |
a = _right[a]; |
1592 | 1592 |
while (_left[a] != INVALID) { |
1593 | 1593 |
a = _left[a]; |
1594 | 1594 |
} |
1595 | 1595 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1596 | 1596 |
} else { |
1597 | 1597 |
while (_parent[a] != INVALID && _right[_parent[a]] == a) { |
1598 | 1598 |
a = _parent[a]; |
1599 | 1599 |
} |
1600 | 1600 |
if (_parent[a] == INVALID) { |
1601 | 1601 |
return INVALID; |
1602 | 1602 |
} else { |
1603 | 1603 |
a = _parent[a]; |
1604 | 1604 |
const_cast<DynArcLookUp&>(*this).splay(a); |
1605 | 1605 |
} |
1606 | 1606 |
} |
1607 | 1607 |
if (_g.target(a) == t) return a; |
1608 | 1608 |
else return INVALID; |
1609 | 1609 |
} |
1610 | 1610 |
} |
1611 | 1611 |
|
1612 | 1612 |
}; |
1613 | 1613 |
|
1614 | 1614 |
///Fast arc look-up between given endpoints. |
1615 | 1615 |
|
1616 | 1616 |
///Using this class, you can find an arc in a digraph from a given |
1617 | 1617 |
///source to a given target in time <em>O</em>(log<em>d</em>), |
1618 | 1618 |
///where <em>d</em> is the out-degree of the source node. |
1619 | 1619 |
/// |
1620 | 1620 |
///It is not possible to find \e all parallel arcs between two nodes. |
1621 | 1621 |
///Use \ref AllArcLookUp for this purpose. |
1622 | 1622 |
/// |
1623 | 1623 |
///\warning This class is static, so you should call refresh() (or at |
1624 | 1624 |
///least refresh(Node)) to refresh this data structure whenever the |
1625 | 1625 |
///digraph changes. This is a time consuming (superlinearly proportional |
1626 | 1626 |
///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs). |
1627 | 1627 |
/// |
1628 | 1628 |
///\tparam G The type of the underlying digraph. |
1629 | 1629 |
/// |
1630 | 1630 |
///\sa DynArcLookUp |
1631 | 1631 |
///\sa AllArcLookUp |
1632 | 1632 |
template<class G> |
1633 | 1633 |
class ArcLookUp |
1634 | 1634 |
{ |
1635 | 1635 |
public: |
1636 | 1636 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
1637 | 1637 |
typedef G Digraph; |
1638 | 1638 |
|
1639 | 1639 |
protected: |
1640 | 1640 |
const Digraph &_g; |
1641 | 1641 |
typename Digraph::template NodeMap<Arc> _head; |
1642 | 1642 |
typename Digraph::template ArcMap<Arc> _left; |
1643 | 1643 |
typename Digraph::template ArcMap<Arc> _right; |
1644 | 1644 |
|
1645 | 1645 |
class ArcLess { |
1646 | 1646 |
const Digraph &g; |
1647 | 1647 |
public: |
1648 | 1648 |
ArcLess(const Digraph &_g) : g(_g) {} |
1649 | 1649 |
bool operator()(Arc a,Arc b) const |
1650 | 1650 |
{ |
1651 | 1651 |
return g.target(a)<g.target(b); |
1652 | 1652 |
} |
1653 | 1653 |
}; |
1654 | 1654 |
|
1655 | 1655 |
public: |
1656 | 1656 |
|
1657 | 1657 |
///Constructor |
1658 | 1658 |
|
1659 | 1659 |
///Constructor. |
1660 | 1660 |
/// |
1661 | 1661 |
///It builds up the search database, which remains valid until the digraph |
1662 | 1662 |
///changes. |
1663 | 1663 |
ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();} |
1664 | 1664 |
|
1665 | 1665 |
private: |
1666 | 1666 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
1667 | 1667 |
{ |
1668 | 1668 |
int m=(a+b)/2; |
1669 | 1669 |
Arc me=v[m]; |
1670 | 1670 |
_left[me] = a<m?refreshRec(v,a,m-1):INVALID; |
1671 | 1671 |
_right[me] = m<b?refreshRec(v,m+1,b):INVALID; |
1672 | 1672 |
return me; |
1673 | 1673 |
} |
1674 | 1674 |
public: |
1675 | 1675 |
///Refresh the search data structure at a node. |
1676 | 1676 |
|
1677 | 1677 |
///Build up the search database of node \c n. |
1678 | 1678 |
/// |
1679 | 1679 |
///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> |
1680 | 1680 |
///is the number of the outgoing arcs of \c n. |
1681 | 1681 |
void refresh(Node n) |
1682 | 1682 |
{ |
1683 | 1683 |
std::vector<Arc> v; |
1684 | 1684 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e); |
1685 | 1685 |
if(v.size()) { |
1686 | 1686 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
1687 | 1687 |
_head[n]=refreshRec(v,0,v.size()-1); |
1688 | 1688 |
} |
1689 | 1689 |
else _head[n]=INVALID; |
1690 | 1690 |
} |
1691 | 1691 |
///Refresh the full data structure. |
1692 | 1692 |
|
1693 | 1693 |
///Build up the full search database. In fact, it simply calls |
1694 | 1694 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
1695 | 1695 |
/// |
1696 | 1696 |
///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is |
1697 | 1697 |
///the number of the arcs in the digraph and <em>D</em> is the maximum |
1698 | 1698 |
///out-degree of the digraph. |
1699 | 1699 |
void refresh() |
1700 | 1700 |
{ |
1701 | 1701 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(n); |
1702 | 1702 |
} |
1703 | 1703 |
|
1704 | 1704 |
///Find an arc between two nodes. |
1705 | 1705 |
|
1706 | 1706 |
///Find an arc between two nodes in time <em>O</em>(log<em>d</em>), |
1707 | 1707 |
///where <em>d</em> is the number of outgoing arcs of \c s. |
1708 | 1708 |
///\param s The source node. |
1709 | 1709 |
///\param t The target node. |
1710 | 1710 |
///\return An arc from \c s to \c t if there exists, |
1711 | 1711 |
///\ref INVALID otherwise. |
1712 | 1712 |
/// |
1713 | 1713 |
///\warning If you change the digraph, refresh() must be called before using |
1714 | 1714 |
///this operator. If you change the outgoing arcs of |
1715 | 1715 |
///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough. |
1716 | 1716 |
Arc operator()(Node s, Node t) const |
1717 | 1717 |
{ |
1718 | 1718 |
Arc e; |
1719 | 1719 |
for(e=_head[s]; |
1720 | 1720 |
e!=INVALID&&_g.target(e)!=t; |
1721 | 1721 |
e = t < _g.target(e)?_left[e]:_right[e]) ; |
1722 | 1722 |
return e; |
1723 | 1723 |
} |
1724 | 1724 |
|
1725 | 1725 |
}; |
1726 | 1726 |
|
1727 | 1727 |
///Fast look-up of all arcs between given endpoints. |
1728 | 1728 |
|
1729 | 1729 |
///This class is the same as \ref ArcLookUp, with the addition |
1730 | 1730 |
///that it makes it possible to find all parallel arcs between given |
1731 | 1731 |
///endpoints. |
1732 | 1732 |
/// |
1733 | 1733 |
///\warning This class is static, so you should call refresh() (or at |
1734 | 1734 |
///least refresh(Node)) to refresh this data structure whenever the |
1735 | 1735 |
///digraph changes. This is a time consuming (superlinearly proportional |
1736 | 1736 |
///(<em>O</em>(<em>m</em> log<em>m</em>)) to the number of arcs). |
1737 | 1737 |
/// |
1738 | 1738 |
///\tparam G The type of the underlying digraph. |
1739 | 1739 |
/// |
1740 | 1740 |
///\sa DynArcLookUp |
1741 | 1741 |
///\sa ArcLookUp |
1742 | 1742 |
template<class G> |
1743 | 1743 |
class AllArcLookUp : public ArcLookUp<G> |
1744 | 1744 |
{ |
1745 | 1745 |
using ArcLookUp<G>::_g; |
1746 | 1746 |
using ArcLookUp<G>::_right; |
1747 | 1747 |
using ArcLookUp<G>::_left; |
1748 | 1748 |
using ArcLookUp<G>::_head; |
1749 | 1749 |
|
1750 | 1750 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
1751 | 1751 |
typedef G Digraph; |
1752 | 1752 |
|
1753 | 1753 |
typename Digraph::template ArcMap<Arc> _next; |
1754 | 1754 |
|
1755 | 1755 |
Arc refreshNext(Arc head,Arc next=INVALID) |
1756 | 1756 |
{ |
1757 | 1757 |
if(head==INVALID) return next; |
1758 | 1758 |
else { |
1759 | 1759 |
next=refreshNext(_right[head],next); |
1760 | 1760 |
_next[head]=( next!=INVALID && _g.target(next)==_g.target(head)) |
1761 | 1761 |
? next : INVALID; |
1762 | 1762 |
return refreshNext(_left[head],head); |
1763 | 1763 |
} |
1764 | 1764 |
} |
1765 | 1765 |
|
1766 | 1766 |
void refreshNext() |
1767 | 1767 |
{ |
1768 | 1768 |
for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]); |
1769 | 1769 |
} |
1770 | 1770 |
|
1771 | 1771 |
public: |
1772 | 1772 |
///Constructor |
1773 | 1773 |
|
1774 | 1774 |
///Constructor. |
1775 | 1775 |
/// |
1776 | 1776 |
///It builds up the search database, which remains valid until the digraph |
1777 | 1777 |
///changes. |
1778 | 1778 |
AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();} |
1779 | 1779 |
|
1780 | 1780 |
///Refresh the data structure at a node. |
1781 | 1781 |
|
1782 | 1782 |
///Build up the search database of node \c n. |
1783 | 1783 |
/// |
1784 | 1784 |
///It runs in time <em>O</em>(<em>d</em> log<em>d</em>), where <em>d</em> is |
1785 | 1785 |
///the number of the outgoing arcs of \c n. |
1786 | 1786 |
void refresh(Node n) |
1787 | 1787 |
{ |
1788 | 1788 |
ArcLookUp<G>::refresh(n); |
1789 | 1789 |
refreshNext(_head[n]); |
1790 | 1790 |
} |
1791 | 1791 |
|
1792 | 1792 |
///Refresh the full data structure. |
1793 | 1793 |
|
1794 | 1794 |
///Build up the full search database. In fact, it simply calls |
1795 | 1795 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
1796 | 1796 |
/// |
1797 | 1797 |
///It runs in time <em>O</em>(<em>m</em> log<em>D</em>), where <em>m</em> is |
1798 | 1798 |
///the number of the arcs in the digraph and <em>D</em> is the maximum |
1799 | 1799 |
///out-degree of the digraph. |
1800 | 1800 |
void refresh() |
1801 | 1801 |
{ |
1802 | 1802 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]); |
1803 | 1803 |
} |
1804 | 1804 |
|
1805 | 1805 |
///Find an arc between two nodes. |
1806 | 1806 |
|
1807 | 1807 |
///Find an arc between two nodes. |
1808 | 1808 |
///\param s The source node. |
1809 | 1809 |
///\param t The target node. |
1810 | 1810 |
///\param prev The previous arc between \c s and \c t. It it is INVALID or |
1811 | 1811 |
///not given, the operator finds the first appropriate arc. |
1812 | 1812 |
///\return An arc from \c s to \c t after \c prev or |
1813 | 1813 |
///\ref INVALID if there is no more. |
1814 | 1814 |
/// |
1815 | 1815 |
///For example, you can count the number of arcs from \c u to \c v in the |
1816 | 1816 |
///following way. |
1817 | 1817 |
///\code |
1818 | 1818 |
///AllArcLookUp<ListDigraph> ae(g); |
1819 | 1819 |
///... |
1820 | 1820 |
///int n = 0; |
1821 | 1821 |
///for(Arc a = ae(u,v); a != INVALID; a=ae(u,v,a)) n++; |
1822 | 1822 |
///\endcode |
1823 | 1823 |
/// |
1824 | 1824 |
///Finding the first arc take <em>O</em>(log<em>d</em>) time, |
1825 | 1825 |
///where <em>d</em> is the number of outgoing arcs of \c s. Then the |
1826 | 1826 |
///consecutive arcs are found in constant time. |
1827 | 1827 |
/// |
1828 | 1828 |
///\warning If you change the digraph, refresh() must be called before using |
1829 | 1829 |
///this operator. If you change the outgoing arcs of |
1830 | 1830 |
///a single node \c n, then \ref refresh(Node) "refresh(n)" is enough. |
1831 | 1831 |
/// |
1832 | 1832 |
#ifdef DOXYGEN |
1833 | 1833 |
Arc operator()(Node s, Node t, Arc prev=INVALID) const {} |
1834 | 1834 |
#else |
1835 | 1835 |
using ArcLookUp<G>::operator() ; |
1836 | 1836 |
Arc operator()(Node s, Node t, Arc prev) const |
1837 | 1837 |
{ |
1838 | 1838 |
return prev==INVALID?(*this)(s,t):_next[prev]; |
1839 | 1839 |
} |
1840 | 1840 |
#endif |
1841 | 1841 |
|
1842 | 1842 |
}; |
1843 | 1843 |
|
1844 | 1844 |
/// @} |
1845 | 1845 |
|
1846 | 1846 |
} //namespace lemon |
1847 | 1847 |
|
1848 | 1848 |
#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_DFS_H |
20 | 20 |
#define LEMON_DFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 | 24 |
///\brief DFS algorithm. |
25 | 25 |
|
26 | 26 |
#include <lemon/list_graph.h> |
27 | 27 |
#include <lemon/bits/path_dump.h> |
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/error.h> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/path.h> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
///Default traits class of Dfs class. |
36 | 36 |
|
37 | 37 |
///Default traits class of Dfs class. |
38 | 38 |
///\tparam GR Digraph type. |
39 | 39 |
template<class GR> |
40 | 40 |
struct DfsDefaultTraits |
41 | 41 |
{ |
42 | 42 |
///The type of the digraph the algorithm runs on. |
43 | 43 |
typedef GR Digraph; |
44 | 44 |
|
45 | 45 |
///\brief The type of the map that stores the predecessor |
46 | 46 |
///arcs of the %DFS paths. |
47 | 47 |
/// |
48 | 48 |
///The type of the map that stores the predecessor |
49 | 49 |
///arcs of the %DFS paths. |
50 | 50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
52 | 52 |
///Instantiates a PredMap. |
53 | 53 |
|
54 | 54 |
///This function instantiates a PredMap. |
55 | 55 |
///\param g is the digraph, to which we would like to define the |
56 | 56 |
///PredMap. |
57 | 57 |
static PredMap *createPredMap(const Digraph &g) |
58 | 58 |
{ |
59 | 59 |
return new PredMap(g); |
60 | 60 |
} |
61 | 61 |
|
62 | 62 |
///The type of the map that indicates which nodes are processed. |
63 | 63 |
|
64 | 64 |
///The type of the map that indicates which nodes are processed. |
65 | 65 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
66 | 66 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
67 | 67 |
///Instantiates a ProcessedMap. |
68 | 68 |
|
69 | 69 |
///This function instantiates a ProcessedMap. |
70 | 70 |
///\param g is the digraph, to which |
71 | 71 |
///we would like to define the ProcessedMap |
72 | 72 |
#ifdef DOXYGEN |
73 | 73 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
74 | 74 |
#else |
75 | 75 |
static ProcessedMap *createProcessedMap(const Digraph &) |
76 | 76 |
#endif |
77 | 77 |
{ |
78 | 78 |
return new ProcessedMap(); |
79 | 79 |
} |
80 | 80 |
|
81 | 81 |
///The type of the map that indicates which nodes are reached. |
82 | 82 |
|
83 | 83 |
///The type of the map that indicates which nodes are reached. |
84 | 84 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
85 | 85 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
86 | 86 |
///Instantiates a ReachedMap. |
87 | 87 |
|
88 | 88 |
///This function instantiates a ReachedMap. |
89 | 89 |
///\param g is the digraph, to which |
90 | 90 |
///we would like to define the ReachedMap. |
91 | 91 |
static ReachedMap *createReachedMap(const Digraph &g) |
92 | 92 |
{ |
93 | 93 |
return new ReachedMap(g); |
94 | 94 |
} |
95 | 95 |
|
96 | 96 |
///The type of the map that stores the distances of the nodes. |
97 | 97 |
|
98 | 98 |
///The type of the map that stores the distances of the nodes. |
99 | 99 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
100 | 100 |
typedef typename Digraph::template NodeMap<int> DistMap; |
101 | 101 |
///Instantiates a DistMap. |
102 | 102 |
|
103 | 103 |
///This function instantiates a DistMap. |
104 | 104 |
///\param g is the digraph, to which we would like to define the |
105 | 105 |
///DistMap. |
106 | 106 |
static DistMap *createDistMap(const Digraph &g) |
107 | 107 |
{ |
108 | 108 |
return new DistMap(g); |
109 | 109 |
} |
110 | 110 |
}; |
111 | 111 |
|
112 | 112 |
///%DFS algorithm class. |
113 | 113 |
|
114 | 114 |
///\ingroup search |
115 | 115 |
///This class provides an efficient implementation of the %DFS algorithm. |
116 | 116 |
/// |
117 | 117 |
///There is also a \ref dfs() "function-type interface" for the DFS |
118 | 118 |
///algorithm, which is convenient in the simplier cases and it can be |
119 | 119 |
///used easier. |
120 | 120 |
/// |
121 | 121 |
///\tparam GR The type of the digraph the algorithm runs on. |
122 | 122 |
///The default value is \ref ListDigraph. The value of GR is not used |
123 | 123 |
///directly by \ref Dfs, it is only passed to \ref DfsDefaultTraits. |
124 | 124 |
///\tparam TR Traits class to set various data types used by the algorithm. |
125 | 125 |
///The default traits class is |
126 | 126 |
///\ref DfsDefaultTraits "DfsDefaultTraits<GR>". |
127 | 127 |
///See \ref DfsDefaultTraits for the documentation of |
128 | 128 |
///a Dfs traits class. |
129 | 129 |
#ifdef DOXYGEN |
130 | 130 |
template <typename GR, |
131 | 131 |
typename TR> |
132 | 132 |
#else |
133 | 133 |
template <typename GR=ListDigraph, |
134 | 134 |
typename TR=DfsDefaultTraits<GR> > |
135 | 135 |
#endif |
136 | 136 |
class Dfs { |
137 | 137 |
public: |
138 | 138 |
|
139 | 139 |
///The type of the digraph the algorithm runs on. |
140 | 140 |
typedef typename TR::Digraph Digraph; |
141 | 141 |
|
142 | 142 |
///\brief The type of the map that stores the predecessor arcs of the |
143 | 143 |
///DFS paths. |
144 | 144 |
typedef typename TR::PredMap PredMap; |
145 | 145 |
///The type of the map that stores the distances of the nodes. |
146 | 146 |
typedef typename TR::DistMap DistMap; |
147 | 147 |
///The type of the map that indicates which nodes are reached. |
148 | 148 |
typedef typename TR::ReachedMap ReachedMap; |
149 | 149 |
///The type of the map that indicates which nodes are processed. |
150 | 150 |
typedef typename TR::ProcessedMap ProcessedMap; |
151 | 151 |
///The type of the paths. |
152 | 152 |
typedef PredMapPath<Digraph, PredMap> Path; |
153 | 153 |
|
154 | 154 |
///The traits class. |
155 | 155 |
typedef TR Traits; |
156 | 156 |
|
157 | 157 |
private: |
158 | 158 |
|
159 | 159 |
typedef typename Digraph::Node Node; |
160 | 160 |
typedef typename Digraph::NodeIt NodeIt; |
161 | 161 |
typedef typename Digraph::Arc Arc; |
162 | 162 |
typedef typename Digraph::OutArcIt OutArcIt; |
163 | 163 |
|
164 | 164 |
//Pointer to the underlying digraph. |
165 | 165 |
const Digraph *G; |
166 | 166 |
//Pointer to the map of predecessor arcs. |
167 | 167 |
PredMap *_pred; |
168 | 168 |
//Indicates if _pred is locally allocated (true) or not. |
169 | 169 |
bool local_pred; |
170 | 170 |
//Pointer to the map of distances. |
171 | 171 |
DistMap *_dist; |
172 | 172 |
//Indicates if _dist is locally allocated (true) or not. |
173 | 173 |
bool local_dist; |
174 | 174 |
//Pointer to the map of reached status of the nodes. |
175 | 175 |
ReachedMap *_reached; |
176 | 176 |
//Indicates if _reached is locally allocated (true) or not. |
177 | 177 |
bool local_reached; |
178 | 178 |
//Pointer to the map of processed status of the nodes. |
179 | 179 |
ProcessedMap *_processed; |
180 | 180 |
//Indicates if _processed is locally allocated (true) or not. |
181 | 181 |
bool local_processed; |
182 | 182 |
|
183 | 183 |
std::vector<typename Digraph::OutArcIt> _stack; |
184 | 184 |
int _stack_head; |
185 | 185 |
|
186 | 186 |
//Creates the maps if necessary. |
187 | 187 |
void create_maps() |
188 | 188 |
{ |
189 | 189 |
if(!_pred) { |
190 | 190 |
local_pred = true; |
191 | 191 |
_pred = Traits::createPredMap(*G); |
192 | 192 |
} |
193 | 193 |
if(!_dist) { |
194 | 194 |
local_dist = true; |
195 | 195 |
_dist = Traits::createDistMap(*G); |
196 | 196 |
} |
197 | 197 |
if(!_reached) { |
198 | 198 |
local_reached = true; |
199 | 199 |
_reached = Traits::createReachedMap(*G); |
200 | 200 |
} |
201 | 201 |
if(!_processed) { |
202 | 202 |
local_processed = true; |
203 | 203 |
_processed = Traits::createProcessedMap(*G); |
204 | 204 |
} |
205 | 205 |
} |
206 | 206 |
|
207 | 207 |
protected: |
208 | 208 |
|
209 | 209 |
Dfs() {} |
210 | 210 |
|
211 | 211 |
public: |
212 | 212 |
|
213 | 213 |
typedef Dfs Create; |
214 | 214 |
|
215 | 215 |
///\name Named template parameters |
216 | 216 |
|
217 | 217 |
///@{ |
218 | 218 |
|
219 | 219 |
template <class T> |
220 | 220 |
struct SetPredMapTraits : public Traits { |
221 | 221 |
typedef T PredMap; |
222 | 222 |
static PredMap *createPredMap(const Digraph &) |
223 | 223 |
{ |
224 | 224 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
225 | 225 |
return 0; // ignore warnings |
226 | 226 |
} |
227 | 227 |
}; |
228 | 228 |
///\brief \ref named-templ-param "Named parameter" for setting |
229 | 229 |
///PredMap type. |
230 | 230 |
/// |
231 | 231 |
///\ref named-templ-param "Named parameter" for setting |
232 | 232 |
///PredMap type. |
233 | 233 |
template <class T> |
234 | 234 |
struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > { |
235 | 235 |
typedef Dfs<Digraph, SetPredMapTraits<T> > Create; |
236 | 236 |
}; |
237 | 237 |
|
238 | 238 |
template <class T> |
239 | 239 |
struct SetDistMapTraits : public Traits { |
240 | 240 |
typedef T DistMap; |
241 | 241 |
static DistMap *createDistMap(const Digraph &) |
242 | 242 |
{ |
243 | 243 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
244 | 244 |
return 0; // ignore warnings |
245 | 245 |
} |
246 | 246 |
}; |
247 | 247 |
///\brief \ref named-templ-param "Named parameter" for setting |
248 | 248 |
///DistMap type. |
249 | 249 |
/// |
250 | 250 |
///\ref named-templ-param "Named parameter" for setting |
251 | 251 |
///DistMap type. |
252 | 252 |
template <class T> |
253 | 253 |
struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > { |
254 | 254 |
typedef Dfs<Digraph, SetDistMapTraits<T> > Create; |
255 | 255 |
}; |
256 | 256 |
|
257 | 257 |
template <class T> |
258 | 258 |
struct SetReachedMapTraits : public Traits { |
259 | 259 |
typedef T ReachedMap; |
260 | 260 |
static ReachedMap *createReachedMap(const Digraph &) |
261 | 261 |
{ |
262 | 262 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
263 | 263 |
return 0; // ignore warnings |
264 | 264 |
} |
265 | 265 |
}; |
266 | 266 |
///\brief \ref named-templ-param "Named parameter" for setting |
267 | 267 |
///ReachedMap type. |
268 | 268 |
/// |
269 | 269 |
///\ref named-templ-param "Named parameter" for setting |
270 | 270 |
///ReachedMap type. |
271 | 271 |
template <class T> |
272 | 272 |
struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > { |
273 | 273 |
typedef Dfs< Digraph, SetReachedMapTraits<T> > Create; |
274 | 274 |
}; |
275 | 275 |
|
276 | 276 |
template <class T> |
277 | 277 |
struct SetProcessedMapTraits : public Traits { |
278 | 278 |
typedef T ProcessedMap; |
279 | 279 |
static ProcessedMap *createProcessedMap(const Digraph &) |
280 | 280 |
{ |
281 | 281 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
282 | 282 |
return 0; // ignore warnings |
283 | 283 |
} |
284 | 284 |
}; |
285 | 285 |
///\brief \ref named-templ-param "Named parameter" for setting |
286 | 286 |
///ProcessedMap type. |
287 | 287 |
/// |
288 | 288 |
///\ref named-templ-param "Named parameter" for setting |
289 | 289 |
///ProcessedMap type. |
290 | 290 |
template <class T> |
291 | 291 |
struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > { |
292 | 292 |
typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create; |
293 | 293 |
}; |
294 | 294 |
|
295 | 295 |
struct SetStandardProcessedMapTraits : public Traits { |
296 | 296 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
297 | 297 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
298 | 298 |
{ |
299 | 299 |
return new ProcessedMap(g); |
300 | 300 |
} |
301 | 301 |
}; |
302 | 302 |
///\brief \ref named-templ-param "Named parameter" for setting |
303 | 303 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
304 | 304 |
/// |
305 | 305 |
///\ref named-templ-param "Named parameter" for setting |
306 | 306 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
307 | 307 |
///If you don't set it explicitly, it will be automatically allocated. |
308 | 308 |
struct SetStandardProcessedMap : |
309 | 309 |
public Dfs< Digraph, SetStandardProcessedMapTraits > { |
310 | 310 |
typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create; |
311 | 311 |
}; |
312 | 312 |
|
313 | 313 |
///@} |
314 | 314 |
|
315 | 315 |
public: |
316 | 316 |
|
317 | 317 |
///Constructor. |
318 | 318 |
|
319 | 319 |
///Constructor. |
320 | 320 |
///\param g The digraph the algorithm runs on. |
321 | 321 |
Dfs(const Digraph &g) : |
322 | 322 |
G(&g), |
323 | 323 |
_pred(NULL), local_pred(false), |
324 | 324 |
_dist(NULL), local_dist(false), |
325 | 325 |
_reached(NULL), local_reached(false), |
326 | 326 |
_processed(NULL), local_processed(false) |
327 | 327 |
{ } |
328 | 328 |
|
329 | 329 |
///Destructor. |
330 | 330 |
~Dfs() |
331 | 331 |
{ |
332 | 332 |
if(local_pred) delete _pred; |
333 | 333 |
if(local_dist) delete _dist; |
334 | 334 |
if(local_reached) delete _reached; |
335 | 335 |
if(local_processed) delete _processed; |
336 | 336 |
} |
337 | 337 |
|
338 | 338 |
///Sets the map that stores the predecessor arcs. |
339 | 339 |
|
340 | 340 |
///Sets the map that stores the predecessor arcs. |
341 | 341 |
///If you don't use this function before calling \ref run(), |
342 | 342 |
///it will allocate one. The destructor deallocates this |
343 | 343 |
///automatically allocated map, of course. |
344 | 344 |
///\return <tt> (*this) </tt> |
345 | 345 |
Dfs &predMap(PredMap &m) |
346 | 346 |
{ |
347 | 347 |
if(local_pred) { |
348 | 348 |
delete _pred; |
349 | 349 |
local_pred=false; |
350 | 350 |
} |
351 | 351 |
_pred = &m; |
352 | 352 |
return *this; |
353 | 353 |
} |
354 | 354 |
|
355 | 355 |
///Sets the map that indicates which nodes are reached. |
356 | 356 |
|
357 | 357 |
///Sets the map that indicates which nodes are reached. |
358 | 358 |
///If you don't use this function before calling \ref run(), |
359 | 359 |
///it will allocate one. The destructor deallocates this |
360 | 360 |
///automatically allocated map, of course. |
361 | 361 |
///\return <tt> (*this) </tt> |
362 | 362 |
Dfs &reachedMap(ReachedMap &m) |
363 | 363 |
{ |
364 | 364 |
if(local_reached) { |
365 | 365 |
delete _reached; |
366 | 366 |
local_reached=false; |
367 | 367 |
} |
368 | 368 |
_reached = &m; |
369 | 369 |
return *this; |
370 | 370 |
} |
371 | 371 |
|
372 | 372 |
///Sets the map that indicates which nodes are processed. |
373 | 373 |
|
374 | 374 |
///Sets the map that indicates which nodes are processed. |
375 | 375 |
///If you don't use this function before calling \ref run(), |
376 | 376 |
///it will allocate one. The destructor deallocates this |
377 | 377 |
///automatically allocated map, of course. |
378 | 378 |
///\return <tt> (*this) </tt> |
379 | 379 |
Dfs &processedMap(ProcessedMap &m) |
380 | 380 |
{ |
381 | 381 |
if(local_processed) { |
382 | 382 |
delete _processed; |
383 | 383 |
local_processed=false; |
384 | 384 |
} |
385 | 385 |
_processed = &m; |
386 | 386 |
return *this; |
387 | 387 |
} |
388 | 388 |
|
389 | 389 |
///Sets the map that stores the distances of the nodes. |
390 | 390 |
|
391 | 391 |
///Sets the map that stores the distances of the nodes calculated by |
392 | 392 |
///the algorithm. |
393 | 393 |
///If you don't use this function before calling \ref run(), |
394 | 394 |
///it will allocate one. The destructor deallocates this |
395 | 395 |
///automatically allocated map, of course. |
396 | 396 |
///\return <tt> (*this) </tt> |
397 | 397 |
Dfs &distMap(DistMap &m) |
398 | 398 |
{ |
399 | 399 |
if(local_dist) { |
400 | 400 |
delete _dist; |
401 | 401 |
local_dist=false; |
402 | 402 |
} |
403 | 403 |
_dist = &m; |
404 | 404 |
return *this; |
405 | 405 |
} |
406 | 406 |
|
407 | 407 |
public: |
408 | 408 |
|
409 | 409 |
///\name Execution control |
410 | 410 |
///The simplest way to execute the algorithm is to use |
411 | 411 |
///one of the member functions called \ref lemon::Dfs::run() "run()". |
412 | 412 |
///\n |
413 | 413 |
///If you need more control on the execution, first you must call |
414 | 414 |
///\ref lemon::Dfs::init() "init()", then you can add a source node |
415 | 415 |
///with \ref lemon::Dfs::addSource() "addSource()". |
416 | 416 |
///Finally \ref lemon::Dfs::start() "start()" will perform the |
417 | 417 |
///actual path computation. |
418 | 418 |
|
419 | 419 |
///@{ |
420 | 420 |
|
421 | 421 |
///Initializes the internal data structures. |
422 | 422 |
|
423 | 423 |
///Initializes the internal data structures. |
424 | 424 |
/// |
425 | 425 |
void init() |
426 | 426 |
{ |
427 | 427 |
create_maps(); |
428 | 428 |
_stack.resize(countNodes(*G)); |
429 | 429 |
_stack_head=-1; |
430 | 430 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
431 | 431 |
_pred->set(u,INVALID); |
432 | 432 |
_reached->set(u,false); |
433 | 433 |
_processed->set(u,false); |
434 | 434 |
} |
435 | 435 |
} |
436 | 436 |
|
437 | 437 |
///Adds a new source node. |
438 | 438 |
|
439 | 439 |
///Adds a new source node to the set of nodes to be processed. |
440 | 440 |
/// |
441 | 441 |
///\pre The stack must be empty. (Otherwise the algorithm gives |
442 | 442 |
///false results.) |
443 | 443 |
/// |
444 | 444 |
///\warning Distances will be wrong (or at least strange) in case of |
445 | 445 |
///multiple sources. |
446 | 446 |
void addSource(Node s) |
447 | 447 |
{ |
448 | 448 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
449 | 449 |
if(!(*_reached)[s]) |
450 | 450 |
{ |
451 | 451 |
_reached->set(s,true); |
452 | 452 |
_pred->set(s,INVALID); |
453 | 453 |
OutArcIt e(*G,s); |
454 | 454 |
if(e!=INVALID) { |
455 | 455 |
_stack[++_stack_head]=e; |
456 | 456 |
_dist->set(s,_stack_head); |
457 | 457 |
} |
458 | 458 |
else { |
459 | 459 |
_processed->set(s,true); |
460 | 460 |
_dist->set(s,0); |
461 | 461 |
} |
462 | 462 |
} |
463 | 463 |
} |
464 | 464 |
|
465 | 465 |
///Processes the next arc. |
466 | 466 |
|
467 | 467 |
///Processes the next arc. |
468 | 468 |
/// |
469 | 469 |
///\return The processed arc. |
470 | 470 |
/// |
471 | 471 |
///\pre The stack must not be empty. |
472 | 472 |
Arc processNextArc() |
473 | 473 |
{ |
474 | 474 |
Node m; |
475 | 475 |
Arc e=_stack[_stack_head]; |
476 | 476 |
if(!(*_reached)[m=G->target(e)]) { |
477 | 477 |
_pred->set(m,e); |
478 | 478 |
_reached->set(m,true); |
479 | 479 |
++_stack_head; |
480 | 480 |
_stack[_stack_head] = OutArcIt(*G, m); |
481 | 481 |
_dist->set(m,_stack_head); |
482 | 482 |
} |
483 | 483 |
else { |
484 | 484 |
m=G->source(e); |
485 | 485 |
++_stack[_stack_head]; |
486 | 486 |
} |
487 | 487 |
while(_stack_head>=0 && _stack[_stack_head]==INVALID) { |
488 | 488 |
_processed->set(m,true); |
489 | 489 |
--_stack_head; |
490 | 490 |
if(_stack_head>=0) { |
491 | 491 |
m=G->source(_stack[_stack_head]); |
492 | 492 |
++_stack[_stack_head]; |
493 | 493 |
} |
494 | 494 |
} |
495 | 495 |
return e; |
496 | 496 |
} |
497 | 497 |
|
498 | 498 |
///Next arc to be processed. |
499 | 499 |
|
500 | 500 |
///Next arc to be processed. |
501 | 501 |
/// |
502 | 502 |
///\return The next arc to be processed or \c INVALID if the stack |
503 | 503 |
///is empty. |
504 | 504 |
OutArcIt nextArc() const |
505 | 505 |
{ |
506 | 506 |
return _stack_head>=0?_stack[_stack_head]:INVALID; |
507 | 507 |
} |
508 | 508 |
|
509 | 509 |
///\brief Returns \c false if there are nodes |
510 | 510 |
///to be processed. |
511 | 511 |
/// |
512 | 512 |
///Returns \c false if there are nodes |
513 | 513 |
///to be processed in the queue (stack). |
514 | 514 |
bool emptyQueue() const { return _stack_head<0; } |
515 | 515 |
|
516 | 516 |
///Returns the number of the nodes to be processed. |
517 | 517 |
|
518 | 518 |
///Returns the number of the nodes to be processed in the queue (stack). |
519 | 519 |
int queueSize() const { return _stack_head+1; } |
520 | 520 |
|
521 | 521 |
///Executes the algorithm. |
522 | 522 |
|
523 | 523 |
///Executes the algorithm. |
524 | 524 |
/// |
525 | 525 |
///This method runs the %DFS algorithm from the root node |
526 | 526 |
///in order to compute the DFS path to each node. |
527 | 527 |
/// |
528 | 528 |
/// The algorithm computes |
529 | 529 |
///- the %DFS tree, |
530 | 530 |
///- the distance of each node from the root in the %DFS tree. |
531 | 531 |
/// |
532 | 532 |
///\pre init() must be called and a root node should be |
533 | 533 |
///added with addSource() before using this function. |
534 | 534 |
/// |
535 | 535 |
///\note <tt>d.start()</tt> is just a shortcut of the following code. |
536 | 536 |
///\code |
537 | 537 |
/// while ( !d.emptyQueue() ) { |
538 | 538 |
/// d.processNextArc(); |
539 | 539 |
/// } |
540 | 540 |
///\endcode |
541 | 541 |
void start() |
542 | 542 |
{ |
543 | 543 |
while ( !emptyQueue() ) processNextArc(); |
544 | 544 |
} |
545 | 545 |
|
546 | 546 |
///Executes the algorithm until the given target node is reached. |
547 | 547 |
|
548 | 548 |
///Executes the algorithm until the given target node is reached. |
549 | 549 |
/// |
550 | 550 |
///This method runs the %DFS algorithm from the root node |
551 | 551 |
///in order to compute the DFS path to \c t. |
552 | 552 |
/// |
553 | 553 |
///The algorithm computes |
554 | 554 |
///- the %DFS path to \c t, |
555 | 555 |
///- the distance of \c t from the root in the %DFS tree. |
556 | 556 |
/// |
557 | 557 |
///\pre init() must be called and a root node should be |
558 | 558 |
///added with addSource() before using this function. |
559 | 559 |
void start(Node t) |
560 | 560 |
{ |
561 | 561 |
while ( !emptyQueue() && !(*_reached)[t] ) |
562 | 562 |
processNextArc(); |
563 | 563 |
} |
564 | 564 |
|
565 | 565 |
///Executes the algorithm until a condition is met. |
566 | 566 |
|
567 | 567 |
///Executes the algorithm until a condition is met. |
568 | 568 |
/// |
569 | 569 |
///This method runs the %DFS algorithm from the root node |
570 | 570 |
///until an arc \c a with <tt>am[a]</tt> true is found. |
571 | 571 |
/// |
572 | 572 |
///\param am A \c bool (or convertible) arc map. The algorithm |
573 | 573 |
///will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
574 | 574 |
/// |
575 | 575 |
///\return The reached arc \c a with <tt>am[a]</tt> true or |
576 | 576 |
///\c INVALID if no such arc was found. |
577 | 577 |
/// |
578 | 578 |
///\pre init() must be called and a root node should be |
579 | 579 |
///added with addSource() before using this function. |
580 | 580 |
/// |
581 | 581 |
///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
582 | 582 |
///not a node map. |
583 | 583 |
template<class ArcBoolMap> |
584 | 584 |
Arc start(const ArcBoolMap &am) |
585 | 585 |
{ |
586 | 586 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
587 | 587 |
processNextArc(); |
588 | 588 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
589 | 589 |
} |
590 | 590 |
|
591 | 591 |
///Runs the algorithm from the given source node. |
592 | 592 |
|
593 | 593 |
///This method runs the %DFS algorithm from node \c s |
594 | 594 |
///in order to compute the DFS path to each node. |
595 | 595 |
/// |
596 | 596 |
///The algorithm computes |
597 | 597 |
///- the %DFS tree, |
598 | 598 |
///- the distance of each node from the root in the %DFS tree. |
599 | 599 |
/// |
600 | 600 |
///\note <tt>d.run(s)</tt> is just a shortcut of the following code. |
601 | 601 |
///\code |
602 | 602 |
/// d.init(); |
603 | 603 |
/// d.addSource(s); |
604 | 604 |
/// d.start(); |
605 | 605 |
///\endcode |
606 | 606 |
void run(Node s) { |
607 | 607 |
init(); |
608 | 608 |
addSource(s); |
609 | 609 |
start(); |
610 | 610 |
} |
611 | 611 |
|
612 | 612 |
///Finds the %DFS path between \c s and \c t. |
613 | 613 |
|
614 | 614 |
///This method runs the %DFS algorithm from node \c s |
615 | 615 |
///in order to compute the DFS path to node \c t |
616 | 616 |
///(it stops searching when \c t is processed) |
617 | 617 |
/// |
618 | 618 |
///\return \c true if \c t is reachable form \c s. |
619 | 619 |
/// |
620 | 620 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is |
621 | 621 |
///just a shortcut of the following code. |
622 | 622 |
///\code |
623 | 623 |
/// d.init(); |
624 | 624 |
/// d.addSource(s); |
625 | 625 |
/// d.start(t); |
626 | 626 |
///\endcode |
627 | 627 |
bool run(Node s,Node t) { |
628 | 628 |
init(); |
629 | 629 |
addSource(s); |
630 | 630 |
start(t); |
631 | 631 |
return reached(t); |
632 | 632 |
} |
633 | 633 |
|
634 | 634 |
///Runs the algorithm to visit all nodes in the digraph. |
635 | 635 |
|
636 | 636 |
///This method runs the %DFS algorithm in order to compute the |
637 | 637 |
///%DFS path to each node. |
638 | 638 |
/// |
639 | 639 |
///The algorithm computes |
640 | 640 |
///- the %DFS tree, |
641 | 641 |
///- the distance of each node from the root in the %DFS tree. |
642 | 642 |
/// |
643 | 643 |
///\note <tt>d.run()</tt> is just a shortcut of the following code. |
644 | 644 |
///\code |
645 | 645 |
/// d.init(); |
646 | 646 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
647 | 647 |
/// if (!d.reached(n)) { |
648 | 648 |
/// d.addSource(n); |
649 | 649 |
/// d.start(); |
650 | 650 |
/// } |
651 | 651 |
/// } |
652 | 652 |
///\endcode |
653 | 653 |
void run() { |
654 | 654 |
init(); |
655 | 655 |
for (NodeIt it(*G); it != INVALID; ++it) { |
656 | 656 |
if (!reached(it)) { |
657 | 657 |
addSource(it); |
658 | 658 |
start(); |
659 | 659 |
} |
660 | 660 |
} |
661 | 661 |
} |
662 | 662 |
|
663 | 663 |
///@} |
664 | 664 |
|
665 | 665 |
///\name Query Functions |
666 | 666 |
///The result of the %DFS algorithm can be obtained using these |
667 | 667 |
///functions.\n |
668 | 668 |
///Either \ref lemon::Dfs::run() "run()" or \ref lemon::Dfs::start() |
669 | 669 |
///"start()" must be called before using them. |
670 | 670 |
|
671 | 671 |
///@{ |
672 | 672 |
|
673 | 673 |
///The DFS path to a node. |
674 | 674 |
|
675 | 675 |
///Returns the DFS path to a node. |
676 | 676 |
/// |
677 | 677 |
///\warning \c t should be reachable from the root. |
678 | 678 |
/// |
679 | 679 |
///\pre Either \ref run() or \ref start() must be called before |
680 | 680 |
///using this function. |
681 | 681 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
682 | 682 |
|
683 | 683 |
///The distance of a node from the root. |
684 | 684 |
|
685 | 685 |
///Returns the distance of a node from the root. |
686 | 686 |
/// |
687 | 687 |
///\warning If node \c v is not reachable from the root, then |
688 | 688 |
///the return value of this function is undefined. |
689 | 689 |
/// |
690 | 690 |
///\pre Either \ref run() or \ref start() must be called before |
691 | 691 |
///using this function. |
692 | 692 |
int dist(Node v) const { return (*_dist)[v]; } |
693 | 693 |
|
694 | 694 |
///Returns the 'previous arc' of the %DFS tree for a node. |
695 | 695 |
|
696 | 696 |
///This function returns the 'previous arc' of the %DFS tree for the |
697 | 697 |
///node \c v, i.e. it returns the last arc of a %DFS path from the |
698 | 698 |
///root to \c v. It is \c INVALID |
699 | 699 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
700 | 700 |
/// |
701 | 701 |
///The %DFS tree used here is equal to the %DFS tree used in |
702 | 702 |
///\ref predNode(). |
703 | 703 |
/// |
704 | 704 |
///\pre Either \ref run() or \ref start() must be called before using |
705 | 705 |
///this function. |
706 | 706 |
Arc predArc(Node v) const { return (*_pred)[v];} |
707 | 707 |
|
708 | 708 |
///Returns the 'previous node' of the %DFS tree. |
709 | 709 |
|
710 | 710 |
///This function returns the 'previous node' of the %DFS |
711 | 711 |
///tree for the node \c v, i.e. it returns the last but one node |
712 | 712 |
///from a %DFS path from the root to \c v. It is \c INVALID |
713 | 713 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
714 | 714 |
/// |
715 | 715 |
///The %DFS tree used here is equal to the %DFS tree used in |
716 | 716 |
///\ref predArc(). |
717 | 717 |
/// |
718 | 718 |
///\pre Either \ref run() or \ref start() must be called before |
719 | 719 |
///using this function. |
720 | 720 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
721 | 721 |
G->source((*_pred)[v]); } |
722 | 722 |
|
723 | 723 |
///\brief Returns a const reference to the node map that stores the |
724 | 724 |
///distances of the nodes. |
725 | 725 |
/// |
726 | 726 |
///Returns a const reference to the node map that stores the |
727 | 727 |
///distances of the nodes calculated by the algorithm. |
728 | 728 |
/// |
729 | 729 |
///\pre Either \ref run() or \ref init() |
730 | 730 |
///must be called before using this function. |
731 | 731 |
const DistMap &distMap() const { return *_dist;} |
732 | 732 |
|
733 | 733 |
///\brief Returns a const reference to the node map that stores the |
734 | 734 |
///predecessor arcs. |
735 | 735 |
/// |
736 | 736 |
///Returns a const reference to the node map that stores the predecessor |
737 | 737 |
///arcs, which form the DFS tree. |
738 | 738 |
/// |
739 | 739 |
///\pre Either \ref run() or \ref init() |
740 | 740 |
///must be called before using this function. |
741 | 741 |
const PredMap &predMap() const { return *_pred;} |
742 | 742 |
|
743 | 743 |
///Checks if a node is reachable from the root(s). |
744 | 744 |
|
745 | 745 |
///Returns \c true if \c v is reachable from the root(s). |
746 | 746 |
///\pre Either \ref run() or \ref start() |
747 | 747 |
///must be called before using this function. |
748 | 748 |
bool reached(Node v) const { return (*_reached)[v]; } |
749 | 749 |
|
750 | 750 |
///@} |
751 | 751 |
}; |
752 | 752 |
|
753 | 753 |
///Default traits class of dfs() function. |
754 | 754 |
|
755 | 755 |
///Default traits class of dfs() function. |
756 | 756 |
///\tparam GR Digraph type. |
757 | 757 |
template<class GR> |
758 | 758 |
struct DfsWizardDefaultTraits |
759 | 759 |
{ |
760 | 760 |
///The type of the digraph the algorithm runs on. |
761 | 761 |
typedef GR Digraph; |
762 | 762 |
|
763 | 763 |
///\brief The type of the map that stores the predecessor |
764 | 764 |
///arcs of the %DFS paths. |
765 | 765 |
/// |
766 | 766 |
///The type of the map that stores the predecessor |
767 | 767 |
///arcs of the %DFS paths. |
768 | 768 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
769 | 769 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
770 | 770 |
///Instantiates a PredMap. |
771 | 771 |
|
772 | 772 |
///This function instantiates a PredMap. |
773 | 773 |
///\param g is the digraph, to which we would like to define the |
774 | 774 |
///PredMap. |
775 | 775 |
static PredMap *createPredMap(const Digraph &g) |
776 | 776 |
{ |
777 | 777 |
return new PredMap(g); |
778 | 778 |
} |
779 | 779 |
|
780 | 780 |
///The type of the map that indicates which nodes are processed. |
781 | 781 |
|
782 | 782 |
///The type of the map that indicates which nodes are processed. |
783 | 783 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
784 | 784 |
///By default it is a NullMap. |
785 | 785 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
786 | 786 |
///Instantiates a ProcessedMap. |
787 | 787 |
|
788 | 788 |
///This function instantiates a ProcessedMap. |
789 | 789 |
///\param g is the digraph, to which |
790 | 790 |
///we would like to define the ProcessedMap. |
791 | 791 |
#ifdef DOXYGEN |
792 | 792 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
793 | 793 |
#else |
794 | 794 |
static ProcessedMap *createProcessedMap(const Digraph &) |
795 | 795 |
#endif |
796 | 796 |
{ |
797 | 797 |
return new ProcessedMap(); |
798 | 798 |
} |
799 | 799 |
|
800 | 800 |
///The type of the map that indicates which nodes are reached. |
801 | 801 |
|
802 | 802 |
///The type of the map that indicates which nodes are reached. |
803 | 803 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
804 | 804 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
805 | 805 |
///Instantiates a ReachedMap. |
806 | 806 |
|
807 | 807 |
///This function instantiates a ReachedMap. |
808 | 808 |
///\param g is the digraph, to which |
809 | 809 |
///we would like to define the ReachedMap. |
810 | 810 |
static ReachedMap *createReachedMap(const Digraph &g) |
811 | 811 |
{ |
812 | 812 |
return new ReachedMap(g); |
813 | 813 |
} |
814 | 814 |
|
815 | 815 |
///The type of the map that stores the distances of the nodes. |
816 | 816 |
|
817 | 817 |
///The type of the map that stores the distances of the nodes. |
818 | 818 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
819 | 819 |
typedef typename Digraph::template NodeMap<int> DistMap; |
820 | 820 |
///Instantiates a DistMap. |
821 | 821 |
|
822 | 822 |
///This function instantiates a DistMap. |
823 | 823 |
///\param g is the digraph, to which we would like to define |
824 | 824 |
///the DistMap |
825 | 825 |
static DistMap *createDistMap(const Digraph &g) |
826 | 826 |
{ |
827 | 827 |
return new DistMap(g); |
828 | 828 |
} |
829 | 829 |
|
830 | 830 |
///The type of the DFS paths. |
831 | 831 |
|
832 | 832 |
///The type of the DFS paths. |
833 | 833 |
///It must meet the \ref concepts::Path "Path" concept. |
834 | 834 |
typedef lemon::Path<Digraph> Path; |
835 | 835 |
}; |
836 | 836 |
|
837 | 837 |
/// Default traits class used by DfsWizard |
838 | 838 |
|
839 | 839 |
/// To make it easier to use Dfs algorithm |
840 | 840 |
/// we have created a wizard class. |
841 | 841 |
/// This \ref DfsWizard class needs default traits, |
842 | 842 |
/// as well as the \ref Dfs class. |
843 | 843 |
/// The \ref DfsWizardBase is a class to be the default traits of the |
844 | 844 |
/// \ref DfsWizard class. |
845 | 845 |
template<class GR> |
846 | 846 |
class DfsWizardBase : public DfsWizardDefaultTraits<GR> |
847 | 847 |
{ |
848 | 848 |
|
849 | 849 |
typedef DfsWizardDefaultTraits<GR> Base; |
850 | 850 |
protected: |
851 | 851 |
//The type of the nodes in the digraph. |
852 | 852 |
typedef typename Base::Digraph::Node Node; |
853 | 853 |
|
854 | 854 |
//Pointer to the digraph the algorithm runs on. |
855 | 855 |
void *_g; |
856 | 856 |
//Pointer to the map of reached nodes. |
857 | 857 |
void *_reached; |
858 | 858 |
//Pointer to the map of processed nodes. |
859 | 859 |
void *_processed; |
860 | 860 |
//Pointer to the map of predecessors arcs. |
861 | 861 |
void *_pred; |
862 | 862 |
//Pointer to the map of distances. |
863 | 863 |
void *_dist; |
864 | 864 |
//Pointer to the DFS path to the target node. |
865 | 865 |
void *_path; |
866 | 866 |
//Pointer to the distance of the target node. |
867 | 867 |
int *_di; |
868 | 868 |
|
869 | 869 |
public: |
870 | 870 |
/// Constructor. |
871 | 871 |
|
872 | 872 |
/// This constructor does not require parameters, therefore it initiates |
873 | 873 |
/// all of the attributes to \c 0. |
874 | 874 |
DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
875 | 875 |
_dist(0), _path(0), _di(0) {} |
876 | 876 |
|
877 | 877 |
/// Constructor. |
878 | 878 |
|
879 | 879 |
/// This constructor requires one parameter, |
880 | 880 |
/// others are initiated to \c 0. |
881 | 881 |
/// \param g The digraph the algorithm runs on. |
882 | 882 |
DfsWizardBase(const GR &g) : |
883 | 883 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
884 | 884 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
885 | 885 |
|
886 | 886 |
}; |
887 | 887 |
|
888 | 888 |
/// Auxiliary class for the function-type interface of DFS algorithm. |
889 | 889 |
|
890 | 890 |
/// This auxiliary class is created to implement the |
891 | 891 |
/// \ref dfs() "function-type interface" of \ref Dfs algorithm. |
892 | 892 |
/// It does not have own \ref run() method, it uses the functions |
893 | 893 |
/// and features of the plain \ref Dfs. |
894 | 894 |
/// |
895 | 895 |
/// This class should only be used through the \ref dfs() function, |
896 | 896 |
/// which makes it easier to use the algorithm. |
897 | 897 |
template<class TR> |
898 | 898 |
class DfsWizard : public TR |
899 | 899 |
{ |
900 | 900 |
typedef TR Base; |
901 | 901 |
|
902 | 902 |
///The type of the digraph the algorithm runs on. |
903 | 903 |
typedef typename TR::Digraph Digraph; |
904 | 904 |
|
905 | 905 |
typedef typename Digraph::Node Node; |
906 | 906 |
typedef typename Digraph::NodeIt NodeIt; |
907 | 907 |
typedef typename Digraph::Arc Arc; |
908 | 908 |
typedef typename Digraph::OutArcIt OutArcIt; |
909 | 909 |
|
910 | 910 |
///\brief The type of the map that stores the predecessor |
911 | 911 |
///arcs of the DFS paths. |
912 | 912 |
typedef typename TR::PredMap PredMap; |
913 | 913 |
///\brief The type of the map that stores the distances of the nodes. |
914 | 914 |
typedef typename TR::DistMap DistMap; |
915 | 915 |
///\brief The type of the map that indicates which nodes are reached. |
916 | 916 |
typedef typename TR::ReachedMap ReachedMap; |
917 | 917 |
///\brief The type of the map that indicates which nodes are processed. |
918 | 918 |
typedef typename TR::ProcessedMap ProcessedMap; |
919 | 919 |
///The type of the DFS paths |
920 | 920 |
typedef typename TR::Path Path; |
921 | 921 |
|
922 | 922 |
public: |
923 | 923 |
|
924 | 924 |
/// Constructor. |
925 | 925 |
DfsWizard() : TR() {} |
926 | 926 |
|
927 | 927 |
/// Constructor that requires parameters. |
928 | 928 |
|
929 | 929 |
/// Constructor that requires parameters. |
930 | 930 |
/// These parameters will be the default values for the traits class. |
931 | 931 |
/// \param g The digraph the algorithm runs on. |
932 | 932 |
DfsWizard(const Digraph &g) : |
933 | 933 |
TR(g) {} |
934 | 934 |
|
935 | 935 |
///Copy constructor |
936 | 936 |
DfsWizard(const TR &b) : TR(b) {} |
937 | 937 |
|
938 | 938 |
~DfsWizard() {} |
939 | 939 |
|
940 | 940 |
///Runs DFS algorithm from the given source node. |
941 | 941 |
|
942 | 942 |
///This method runs DFS algorithm from node \c s |
943 | 943 |
///in order to compute the DFS path to each node. |
944 | 944 |
void run(Node s) |
945 | 945 |
{ |
946 | 946 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
947 | 947 |
if (Base::_pred) |
948 | 948 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
949 | 949 |
if (Base::_dist) |
950 | 950 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
951 | 951 |
if (Base::_reached) |
952 | 952 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
953 | 953 |
if (Base::_processed) |
954 | 954 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
955 | 955 |
if (s!=INVALID) |
956 | 956 |
alg.run(s); |
957 | 957 |
else |
958 | 958 |
alg.run(); |
959 | 959 |
} |
960 | 960 |
|
961 | 961 |
///Finds the DFS path between \c s and \c t. |
962 | 962 |
|
963 | 963 |
///This method runs DFS algorithm from node \c s |
964 | 964 |
///in order to compute the DFS path to node \c t |
965 | 965 |
///(it stops searching when \c t is processed). |
966 | 966 |
/// |
967 | 967 |
///\return \c true if \c t is reachable form \c s. |
968 | 968 |
bool run(Node s, Node t) |
969 | 969 |
{ |
970 | 970 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
971 | 971 |
if (Base::_pred) |
972 | 972 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
973 | 973 |
if (Base::_dist) |
974 | 974 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
975 | 975 |
if (Base::_reached) |
976 | 976 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
977 | 977 |
if (Base::_processed) |
978 | 978 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
979 | 979 |
alg.run(s,t); |
980 | 980 |
if (Base::_path) |
981 | 981 |
*reinterpret_cast<Path*>(Base::_path) = alg.path(t); |
982 | 982 |
if (Base::_di) |
983 | 983 |
*Base::_di = alg.dist(t); |
984 | 984 |
return alg.reached(t); |
985 | 985 |
} |
986 | 986 |
|
987 | 987 |
///Runs DFS algorithm to visit all nodes in the digraph. |
988 | 988 |
|
989 | 989 |
///This method runs DFS algorithm in order to compute |
990 | 990 |
///the DFS path to each node. |
991 | 991 |
void run() |
992 | 992 |
{ |
993 | 993 |
run(INVALID); |
994 | 994 |
} |
995 | 995 |
|
996 | 996 |
template<class T> |
997 | 997 |
struct SetPredMapBase : public Base { |
998 | 998 |
typedef T PredMap; |
999 | 999 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1000 | 1000 |
SetPredMapBase(const TR &b) : TR(b) {} |
1001 | 1001 |
}; |
1002 | 1002 |
///\brief \ref named-func-param "Named parameter" |
1003 | 1003 |
///for setting PredMap object. |
1004 | 1004 |
/// |
1005 | 1005 |
///\ref named-func-param "Named parameter" |
1006 | 1006 |
///for setting PredMap object. |
1007 | 1007 |
template<class T> |
1008 | 1008 |
DfsWizard<SetPredMapBase<T> > predMap(const T &t) |
1009 | 1009 |
{ |
1010 | 1010 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1011 | 1011 |
return DfsWizard<SetPredMapBase<T> >(*this); |
1012 | 1012 |
} |
1013 | 1013 |
|
1014 | 1014 |
template<class T> |
1015 | 1015 |
struct SetReachedMapBase : public Base { |
1016 | 1016 |
typedef T ReachedMap; |
1017 | 1017 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
1018 | 1018 |
SetReachedMapBase(const TR &b) : TR(b) {} |
1019 | 1019 |
}; |
1020 | 1020 |
///\brief \ref named-func-param "Named parameter" |
1021 | 1021 |
///for setting ReachedMap object. |
1022 | 1022 |
/// |
1023 | 1023 |
/// \ref named-func-param "Named parameter" |
1024 | 1024 |
///for setting ReachedMap object. |
1025 | 1025 |
template<class T> |
1026 | 1026 |
DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
1027 | 1027 |
{ |
1028 | 1028 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1029 | 1029 |
return DfsWizard<SetReachedMapBase<T> >(*this); |
1030 | 1030 |
} |
1031 | 1031 |
|
1032 | 1032 |
template<class T> |
1033 | 1033 |
struct SetDistMapBase : public Base { |
1034 | 1034 |
typedef T DistMap; |
1035 | 1035 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1036 | 1036 |
SetDistMapBase(const TR &b) : TR(b) {} |
1037 | 1037 |
}; |
1038 | 1038 |
///\brief \ref named-func-param "Named parameter" |
1039 | 1039 |
///for setting DistMap object. |
1040 | 1040 |
/// |
1041 | 1041 |
/// \ref named-func-param "Named parameter" |
1042 | 1042 |
///for setting DistMap object. |
1043 | 1043 |
template<class T> |
1044 | 1044 |
DfsWizard<SetDistMapBase<T> > distMap(const T &t) |
1045 | 1045 |
{ |
1046 | 1046 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1047 | 1047 |
return DfsWizard<SetDistMapBase<T> >(*this); |
1048 | 1048 |
} |
1049 | 1049 |
|
1050 | 1050 |
template<class T> |
1051 | 1051 |
struct SetProcessedMapBase : public Base { |
1052 | 1052 |
typedef T ProcessedMap; |
1053 | 1053 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1054 | 1054 |
SetProcessedMapBase(const TR &b) : TR(b) {} |
1055 | 1055 |
}; |
1056 | 1056 |
///\brief \ref named-func-param "Named parameter" |
1057 | 1057 |
///for setting ProcessedMap object. |
1058 | 1058 |
/// |
1059 | 1059 |
/// \ref named-func-param "Named parameter" |
1060 | 1060 |
///for setting ProcessedMap object. |
1061 | 1061 |
template<class T> |
1062 | 1062 |
DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
1063 | 1063 |
{ |
1064 | 1064 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1065 | 1065 |
return DfsWizard<SetProcessedMapBase<T> >(*this); |
1066 | 1066 |
} |
1067 | 1067 |
|
1068 | 1068 |
template<class T> |
1069 | 1069 |
struct SetPathBase : public Base { |
1070 | 1070 |
typedef T Path; |
1071 | 1071 |
SetPathBase(const TR &b) : TR(b) {} |
1072 | 1072 |
}; |
1073 | 1073 |
///\brief \ref named-func-param "Named parameter" |
1074 | 1074 |
///for getting the DFS path to the target node. |
1075 | 1075 |
/// |
1076 | 1076 |
///\ref named-func-param "Named parameter" |
1077 | 1077 |
///for getting the DFS path to the target node. |
1078 | 1078 |
template<class T> |
1079 | 1079 |
DfsWizard<SetPathBase<T> > path(const T &t) |
1080 | 1080 |
{ |
1081 | 1081 |
Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1082 | 1082 |
return DfsWizard<SetPathBase<T> >(*this); |
1083 | 1083 |
} |
1084 | 1084 |
|
1085 | 1085 |
///\brief \ref named-func-param "Named parameter" |
1086 | 1086 |
///for getting the distance of the target node. |
1087 | 1087 |
/// |
1088 | 1088 |
///\ref named-func-param "Named parameter" |
1089 | 1089 |
///for getting the distance of the target node. |
1090 | 1090 |
DfsWizard dist(const int &d) |
1091 | 1091 |
{ |
1092 | 1092 |
Base::_di=const_cast<int*>(&d); |
1093 | 1093 |
return *this; |
1094 | 1094 |
} |
1095 | 1095 |
|
1096 | 1096 |
}; |
1097 | 1097 |
|
1098 | 1098 |
///Function-type interface for DFS algorithm. |
1099 | 1099 |
|
1100 | 1100 |
///\ingroup search |
1101 | 1101 |
///Function-type interface for DFS algorithm. |
1102 | 1102 |
/// |
1103 | 1103 |
///This function also has several \ref named-func-param "named parameters", |
1104 | 1104 |
///they are declared as the members of class \ref DfsWizard. |
1105 | 1105 |
///The following examples show how to use these parameters. |
1106 | 1106 |
///\code |
1107 | 1107 |
/// // Compute the DFS tree |
1108 | 1108 |
/// dfs(g).predMap(preds).distMap(dists).run(s); |
1109 | 1109 |
/// |
1110 | 1110 |
/// // Compute the DFS path from s to t |
1111 | 1111 |
/// bool reached = dfs(g).path(p).dist(d).run(s,t); |
1112 | 1112 |
///\endcode |
1113 | 1113 |
|
1114 | 1114 |
///\warning Don't forget to put the \ref DfsWizard::run() "run()" |
1115 | 1115 |
///to the end of the parameter list. |
1116 | 1116 |
///\sa DfsWizard |
1117 | 1117 |
///\sa Dfs |
1118 | 1118 |
template<class GR> |
1119 | 1119 |
DfsWizard<DfsWizardBase<GR> > |
1120 | 1120 |
dfs(const GR &digraph) |
1121 | 1121 |
{ |
1122 | 1122 |
return DfsWizard<DfsWizardBase<GR> >(digraph); |
1123 | 1123 |
} |
1124 | 1124 |
|
1125 | 1125 |
#ifdef DOXYGEN |
1126 | 1126 |
/// \brief Visitor class for DFS. |
1127 | 1127 |
/// |
1128 | 1128 |
/// This class defines the interface of the DfsVisit events, and |
1129 | 1129 |
/// it could be the base of a real visitor class. |
1130 | 1130 |
template <typename _Digraph> |
1131 | 1131 |
struct DfsVisitor { |
1132 | 1132 |
typedef _Digraph Digraph; |
1133 | 1133 |
typedef typename Digraph::Arc Arc; |
1134 | 1134 |
typedef typename Digraph::Node Node; |
1135 | 1135 |
/// \brief Called for the source node of the DFS. |
1136 | 1136 |
/// |
1137 | 1137 |
/// This function is called for the source node of the DFS. |
1138 | 1138 |
void start(const Node& node) {} |
1139 | 1139 |
/// \brief Called when the source node is leaved. |
1140 | 1140 |
/// |
1141 | 1141 |
/// This function is called when the source node is leaved. |
1142 | 1142 |
void stop(const Node& node) {} |
1143 | 1143 |
/// \brief Called when a node is reached first time. |
1144 | 1144 |
/// |
1145 | 1145 |
/// This function is called when a node is reached first time. |
1146 | 1146 |
void reach(const Node& node) {} |
1147 | 1147 |
/// \brief Called when an arc reaches a new node. |
1148 | 1148 |
/// |
1149 | 1149 |
/// This function is called when the DFS finds an arc whose target node |
1150 | 1150 |
/// is not reached yet. |
1151 | 1151 |
void discover(const Arc& arc) {} |
1152 | 1152 |
/// \brief Called when an arc is examined but its target node is |
1153 | 1153 |
/// already discovered. |
1154 | 1154 |
/// |
1155 | 1155 |
/// This function is called when an arc is examined but its target node is |
1156 | 1156 |
/// already discovered. |
1157 | 1157 |
void examine(const Arc& arc) {} |
1158 | 1158 |
/// \brief Called when the DFS steps back from a node. |
1159 | 1159 |
/// |
1160 | 1160 |
/// This function is called when the DFS steps back from a node. |
1161 | 1161 |
void leave(const Node& node) {} |
1162 | 1162 |
/// \brief Called when the DFS steps back on an arc. |
1163 | 1163 |
/// |
1164 | 1164 |
/// This function is called when the DFS steps back on an arc. |
1165 | 1165 |
void backtrack(const Arc& arc) {} |
1166 | 1166 |
}; |
1167 | 1167 |
#else |
1168 | 1168 |
template <typename _Digraph> |
1169 | 1169 |
struct DfsVisitor { |
1170 | 1170 |
typedef _Digraph Digraph; |
1171 | 1171 |
typedef typename Digraph::Arc Arc; |
1172 | 1172 |
typedef typename Digraph::Node Node; |
1173 | 1173 |
void start(const Node&) {} |
1174 | 1174 |
void stop(const Node&) {} |
1175 | 1175 |
void reach(const Node&) {} |
1176 | 1176 |
void discover(const Arc&) {} |
1177 | 1177 |
void examine(const Arc&) {} |
1178 | 1178 |
void leave(const Node&) {} |
1179 | 1179 |
void backtrack(const Arc&) {} |
1180 | 1180 |
|
1181 | 1181 |
template <typename _Visitor> |
1182 | 1182 |
struct Constraints { |
1183 | 1183 |
void constraints() { |
1184 | 1184 |
Arc arc; |
1185 | 1185 |
Node node; |
1186 | 1186 |
visitor.start(node); |
1187 | 1187 |
visitor.stop(arc); |
1188 | 1188 |
visitor.reach(node); |
1189 | 1189 |
visitor.discover(arc); |
1190 | 1190 |
visitor.examine(arc); |
1191 | 1191 |
visitor.leave(node); |
1192 | 1192 |
visitor.backtrack(arc); |
1193 | 1193 |
} |
1194 | 1194 |
_Visitor& visitor; |
1195 | 1195 |
}; |
1196 | 1196 |
}; |
1197 | 1197 |
#endif |
1198 | 1198 |
|
1199 | 1199 |
/// \brief Default traits class of DfsVisit class. |
1200 | 1200 |
/// |
1201 | 1201 |
/// Default traits class of DfsVisit class. |
1202 | 1202 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1203 | 1203 |
template<class _Digraph> |
1204 | 1204 |
struct DfsVisitDefaultTraits { |
1205 | 1205 |
|
1206 | 1206 |
/// \brief The type of the digraph the algorithm runs on. |
1207 | 1207 |
typedef _Digraph Digraph; |
1208 | 1208 |
|
1209 | 1209 |
/// \brief The type of the map that indicates which nodes are reached. |
1210 | 1210 |
/// |
1211 | 1211 |
/// The type of the map that indicates which nodes are reached. |
1212 | 1212 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
1213 | 1213 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1214 | 1214 |
|
1215 | 1215 |
/// \brief Instantiates a ReachedMap. |
1216 | 1216 |
/// |
1217 | 1217 |
/// This function instantiates a ReachedMap. |
1218 | 1218 |
/// \param digraph is the digraph, to which |
1219 | 1219 |
/// we would like to define the ReachedMap. |
1220 | 1220 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1221 | 1221 |
return new ReachedMap(digraph); |
1222 | 1222 |
} |
1223 | 1223 |
|
1224 | 1224 |
}; |
1225 | 1225 |
|
1226 | 1226 |
/// \ingroup search |
1227 | 1227 |
/// |
1228 | 1228 |
/// \brief %DFS algorithm class with visitor interface. |
1229 | 1229 |
/// |
1230 | 1230 |
/// This class provides an efficient implementation of the %DFS algorithm |
1231 | 1231 |
/// with visitor interface. |
1232 | 1232 |
/// |
1233 | 1233 |
/// The %DfsVisit class provides an alternative interface to the Dfs |
1234 | 1234 |
/// class. It works with callback mechanism, the DfsVisit object calls |
1235 | 1235 |
/// the member functions of the \c Visitor class on every DFS event. |
1236 | 1236 |
/// |
1237 | 1237 |
/// This interface of the DFS algorithm should be used in special cases |
1238 | 1238 |
/// when extra actions have to be performed in connection with certain |
1239 | 1239 |
/// events of the DFS algorithm. Otherwise consider to use Dfs or dfs() |
1240 | 1240 |
/// instead. |
1241 | 1241 |
/// |
1242 | 1242 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1243 | 1243 |
/// The default value is |
1244 | 1244 |
/// \ref ListDigraph. The value of _Digraph is not used directly by |
1245 | 1245 |
/// \ref DfsVisit, it is only passed to \ref DfsVisitDefaultTraits. |
1246 | 1246 |
/// \tparam _Visitor The Visitor type that is used by the algorithm. |
1247 | 1247 |
/// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty visitor, which |
1248 | 1248 |
/// does not observe the DFS events. If you want to observe the DFS |
1249 | 1249 |
/// events, you should implement your own visitor class. |
1250 | 1250 |
/// \tparam _Traits Traits class to set various data types used by the |
1251 | 1251 |
/// algorithm. The default traits class is |
1252 | 1252 |
/// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Digraph>". |
1253 | 1253 |
/// See \ref DfsVisitDefaultTraits for the documentation of |
1254 | 1254 |
/// a DFS visit traits class. |
1255 | 1255 |
#ifdef DOXYGEN |
1256 | 1256 |
template <typename _Digraph, typename _Visitor, typename _Traits> |
1257 | 1257 |
#else |
1258 | 1258 |
template <typename _Digraph = ListDigraph, |
1259 | 1259 |
typename _Visitor = DfsVisitor<_Digraph>, |
1260 | 1260 |
typename _Traits = DfsVisitDefaultTraits<_Digraph> > |
1261 | 1261 |
#endif |
1262 | 1262 |
class DfsVisit { |
1263 | 1263 |
public: |
1264 | 1264 |
|
1265 | 1265 |
///The traits class. |
1266 | 1266 |
typedef _Traits Traits; |
1267 | 1267 |
|
1268 | 1268 |
///The type of the digraph the algorithm runs on. |
1269 | 1269 |
typedef typename Traits::Digraph Digraph; |
1270 | 1270 |
|
1271 | 1271 |
///The visitor type used by the algorithm. |
1272 | 1272 |
typedef _Visitor Visitor; |
1273 | 1273 |
|
1274 | 1274 |
///The type of the map that indicates which nodes are reached. |
1275 | 1275 |
typedef typename Traits::ReachedMap ReachedMap; |
1276 | 1276 |
|
1277 | 1277 |
private: |
1278 | 1278 |
|
1279 | 1279 |
typedef typename Digraph::Node Node; |
1280 | 1280 |
typedef typename Digraph::NodeIt NodeIt; |
1281 | 1281 |
typedef typename Digraph::Arc Arc; |
1282 | 1282 |
typedef typename Digraph::OutArcIt OutArcIt; |
1283 | 1283 |
|
1284 | 1284 |
//Pointer to the underlying digraph. |
1285 | 1285 |
const Digraph *_digraph; |
1286 | 1286 |
//Pointer to the visitor object. |
1287 | 1287 |
Visitor *_visitor; |
1288 | 1288 |
//Pointer to the map of reached status of the nodes. |
1289 | 1289 |
ReachedMap *_reached; |
1290 | 1290 |
//Indicates if _reached is locally allocated (true) or not. |
1291 | 1291 |
bool local_reached; |
1292 | 1292 |
|
1293 | 1293 |
std::vector<typename Digraph::Arc> _stack; |
1294 | 1294 |
int _stack_head; |
1295 | 1295 |
|
1296 | 1296 |
//Creates the maps if necessary. |
1297 | 1297 |
void create_maps() { |
1298 | 1298 |
if(!_reached) { |
1299 | 1299 |
local_reached = true; |
1300 | 1300 |
_reached = Traits::createReachedMap(*_digraph); |
1301 | 1301 |
} |
1302 | 1302 |
} |
1303 | 1303 |
|
1304 | 1304 |
protected: |
1305 | 1305 |
|
1306 | 1306 |
DfsVisit() {} |
1307 | 1307 |
|
1308 | 1308 |
public: |
1309 | 1309 |
|
1310 | 1310 |
typedef DfsVisit Create; |
1311 | 1311 |
|
1312 | 1312 |
/// \name Named template parameters |
1313 | 1313 |
|
1314 | 1314 |
///@{ |
1315 | 1315 |
template <class T> |
1316 | 1316 |
struct SetReachedMapTraits : public Traits { |
1317 | 1317 |
typedef T ReachedMap; |
1318 | 1318 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1319 | 1319 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
1320 | 1320 |
return 0; // ignore warnings |
1321 | 1321 |
} |
1322 | 1322 |
}; |
1323 | 1323 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1324 | 1324 |
/// ReachedMap type. |
1325 | 1325 |
/// |
1326 | 1326 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
1327 | 1327 |
template <class T> |
1328 | 1328 |
struct SetReachedMap : public DfsVisit< Digraph, Visitor, |
1329 | 1329 |
SetReachedMapTraits<T> > { |
1330 | 1330 |
typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
1331 | 1331 |
}; |
1332 | 1332 |
///@} |
1333 | 1333 |
|
1334 | 1334 |
public: |
1335 | 1335 |
|
1336 | 1336 |
/// \brief Constructor. |
1337 | 1337 |
/// |
1338 | 1338 |
/// Constructor. |
1339 | 1339 |
/// |
1340 | 1340 |
/// \param digraph The digraph the algorithm runs on. |
1341 | 1341 |
/// \param visitor The visitor object of the algorithm. |
1342 | 1342 |
DfsVisit(const Digraph& digraph, Visitor& visitor) |
1343 | 1343 |
: _digraph(&digraph), _visitor(&visitor), |
1344 | 1344 |
_reached(0), local_reached(false) {} |
1345 | 1345 |
|
1346 | 1346 |
/// \brief Destructor. |
1347 | 1347 |
~DfsVisit() { |
1348 | 1348 |
if(local_reached) delete _reached; |
1349 | 1349 |
} |
1350 | 1350 |
|
1351 | 1351 |
/// \brief Sets the map that indicates which nodes are reached. |
1352 | 1352 |
/// |
1353 | 1353 |
/// Sets the map that indicates which nodes are reached. |
1354 | 1354 |
/// If you don't use this function before calling \ref run(), |
1355 | 1355 |
/// it will allocate one. The destructor deallocates this |
1356 | 1356 |
/// automatically allocated map, of course. |
1357 | 1357 |
/// \return <tt> (*this) </tt> |
1358 | 1358 |
DfsVisit &reachedMap(ReachedMap &m) { |
1359 | 1359 |
if(local_reached) { |
1360 | 1360 |
delete _reached; |
1361 | 1361 |
local_reached=false; |
1362 | 1362 |
} |
1363 | 1363 |
_reached = &m; |
1364 | 1364 |
return *this; |
1365 | 1365 |
} |
1366 | 1366 |
|
1367 | 1367 |
public: |
1368 | 1368 |
|
1369 | 1369 |
/// \name Execution control |
1370 | 1370 |
/// The simplest way to execute the algorithm is to use |
1371 | 1371 |
/// one of the member functions called \ref lemon::DfsVisit::run() |
1372 | 1372 |
/// "run()". |
1373 | 1373 |
/// \n |
1374 | 1374 |
/// If you need more control on the execution, first you must call |
1375 | 1375 |
/// \ref lemon::DfsVisit::init() "init()", then you can add several |
1376 | 1376 |
/// source nodes with \ref lemon::DfsVisit::addSource() "addSource()". |
1377 | 1377 |
/// Finally \ref lemon::DfsVisit::start() "start()" will perform the |
1378 | 1378 |
/// actual path computation. |
1379 | 1379 |
|
1380 | 1380 |
/// @{ |
1381 | 1381 |
|
1382 | 1382 |
/// \brief Initializes the internal data structures. |
1383 | 1383 |
/// |
1384 | 1384 |
/// Initializes the internal data structures. |
1385 | 1385 |
void init() { |
1386 | 1386 |
create_maps(); |
1387 | 1387 |
_stack.resize(countNodes(*_digraph)); |
1388 | 1388 |
_stack_head = -1; |
1389 | 1389 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1390 | 1390 |
_reached->set(u, false); |
1391 | 1391 |
} |
1392 | 1392 |
} |
1393 | 1393 |
|
1394 | 1394 |
///Adds a new source node. |
1395 | 1395 |
|
1396 | 1396 |
///Adds a new source node to the set of nodes to be processed. |
1397 | 1397 |
/// |
1398 | 1398 |
///\pre The stack must be empty. (Otherwise the algorithm gives |
1399 | 1399 |
///false results.) |
1400 | 1400 |
/// |
1401 | 1401 |
///\warning Distances will be wrong (or at least strange) in case of |
1402 | 1402 |
///multiple sources. |
1403 | 1403 |
void addSource(Node s) |
1404 | 1404 |
{ |
1405 | 1405 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
1406 | 1406 |
if(!(*_reached)[s]) { |
1407 | 1407 |
_reached->set(s,true); |
1408 | 1408 |
_visitor->start(s); |
1409 | 1409 |
_visitor->reach(s); |
1410 | 1410 |
Arc e; |
1411 | 1411 |
_digraph->firstOut(e, s); |
1412 | 1412 |
if (e != INVALID) { |
1413 | 1413 |
_stack[++_stack_head] = e; |
1414 | 1414 |
} else { |
1415 | 1415 |
_visitor->leave(s); |
1416 | 1416 |
_visitor->stop(s); |
1417 | 1417 |
} |
1418 | 1418 |
} |
1419 | 1419 |
} |
1420 | 1420 |
|
1421 | 1421 |
/// \brief Processes the next arc. |
1422 | 1422 |
/// |
1423 | 1423 |
/// Processes the next arc. |
1424 | 1424 |
/// |
1425 | 1425 |
/// \return The processed arc. |
1426 | 1426 |
/// |
1427 | 1427 |
/// \pre The stack must not be empty. |
1428 | 1428 |
Arc processNextArc() { |
1429 | 1429 |
Arc e = _stack[_stack_head]; |
1430 | 1430 |
Node m = _digraph->target(e); |
1431 | 1431 |
if(!(*_reached)[m]) { |
1432 | 1432 |
_visitor->discover(e); |
1433 | 1433 |
_visitor->reach(m); |
1434 | 1434 |
_reached->set(m, true); |
1435 | 1435 |
_digraph->firstOut(_stack[++_stack_head], m); |
1436 | 1436 |
} else { |
1437 | 1437 |
_visitor->examine(e); |
1438 | 1438 |
m = _digraph->source(e); |
1439 | 1439 |
_digraph->nextOut(_stack[_stack_head]); |
1440 | 1440 |
} |
1441 | 1441 |
while (_stack_head>=0 && _stack[_stack_head] == INVALID) { |
1442 | 1442 |
_visitor->leave(m); |
1443 | 1443 |
--_stack_head; |
1444 | 1444 |
if (_stack_head >= 0) { |
1445 | 1445 |
_visitor->backtrack(_stack[_stack_head]); |
1446 | 1446 |
m = _digraph->source(_stack[_stack_head]); |
1447 | 1447 |
_digraph->nextOut(_stack[_stack_head]); |
1448 | 1448 |
} else { |
1449 | 1449 |
_visitor->stop(m); |
1450 | 1450 |
} |
1451 | 1451 |
} |
1452 | 1452 |
return e; |
1453 | 1453 |
} |
1454 | 1454 |
|
1455 | 1455 |
/// \brief Next arc to be processed. |
1456 | 1456 |
/// |
1457 | 1457 |
/// Next arc to be processed. |
1458 | 1458 |
/// |
1459 | 1459 |
/// \return The next arc to be processed or INVALID if the stack is |
1460 | 1460 |
/// empty. |
1461 | 1461 |
Arc nextArc() const { |
1462 | 1462 |
return _stack_head >= 0 ? _stack[_stack_head] : INVALID; |
1463 | 1463 |
} |
1464 | 1464 |
|
1465 | 1465 |
/// \brief Returns \c false if there are nodes |
1466 | 1466 |
/// to be processed. |
1467 | 1467 |
/// |
1468 | 1468 |
/// Returns \c false if there are nodes |
1469 | 1469 |
/// to be processed in the queue (stack). |
1470 | 1470 |
bool emptyQueue() const { return _stack_head < 0; } |
1471 | 1471 |
|
1472 | 1472 |
/// \brief Returns the number of the nodes to be processed. |
1473 | 1473 |
/// |
1474 | 1474 |
/// Returns the number of the nodes to be processed in the queue (stack). |
1475 | 1475 |
int queueSize() const { return _stack_head + 1; } |
1476 | 1476 |
|
1477 | 1477 |
/// \brief Executes the algorithm. |
1478 | 1478 |
/// |
1479 | 1479 |
/// Executes the algorithm. |
1480 | 1480 |
/// |
1481 | 1481 |
/// This method runs the %DFS algorithm from the root node |
1482 | 1482 |
/// in order to compute the %DFS path to each node. |
1483 | 1483 |
/// |
1484 | 1484 |
/// The algorithm computes |
1485 | 1485 |
/// - the %DFS tree, |
1486 | 1486 |
/// - the distance of each node from the root in the %DFS tree. |
1487 | 1487 |
/// |
1488 | 1488 |
/// \pre init() must be called and a root node should be |
1489 | 1489 |
/// added with addSource() before using this function. |
1490 | 1490 |
/// |
1491 | 1491 |
/// \note <tt>d.start()</tt> is just a shortcut of the following code. |
1492 | 1492 |
/// \code |
1493 | 1493 |
/// while ( !d.emptyQueue() ) { |
1494 | 1494 |
/// d.processNextArc(); |
1495 | 1495 |
/// } |
1496 | 1496 |
/// \endcode |
1497 | 1497 |
void start() { |
1498 | 1498 |
while ( !emptyQueue() ) processNextArc(); |
1499 | 1499 |
} |
1500 | 1500 |
|
1501 | 1501 |
/// \brief Executes the algorithm until the given target node is reached. |
1502 | 1502 |
/// |
1503 | 1503 |
/// Executes the algorithm until the given target node is reached. |
1504 | 1504 |
/// |
1505 | 1505 |
/// This method runs the %DFS algorithm from the root node |
1506 | 1506 |
/// in order to compute the DFS path to \c t. |
1507 | 1507 |
/// |
1508 | 1508 |
/// The algorithm computes |
1509 | 1509 |
/// - the %DFS path to \c t, |
1510 | 1510 |
/// - the distance of \c t from the root in the %DFS tree. |
1511 | 1511 |
/// |
1512 | 1512 |
/// \pre init() must be called and a root node should be added |
1513 | 1513 |
/// with addSource() before using this function. |
1514 | 1514 |
void start(Node t) { |
1515 | 1515 |
while ( !emptyQueue() && !(*_reached)[t] ) |
1516 | 1516 |
processNextArc(); |
1517 | 1517 |
} |
1518 | 1518 |
|
1519 | 1519 |
/// \brief Executes the algorithm until a condition is met. |
1520 | 1520 |
/// |
1521 | 1521 |
/// Executes the algorithm until a condition is met. |
1522 | 1522 |
/// |
1523 | 1523 |
/// This method runs the %DFS algorithm from the root node |
1524 | 1524 |
/// until an arc \c a with <tt>am[a]</tt> true is found. |
1525 | 1525 |
/// |
1526 | 1526 |
/// \param am A \c bool (or convertible) arc map. The algorithm |
1527 | 1527 |
/// will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
1528 | 1528 |
/// |
1529 | 1529 |
/// \return The reached arc \c a with <tt>am[a]</tt> true or |
1530 | 1530 |
/// \c INVALID if no such arc was found. |
1531 | 1531 |
/// |
1532 | 1532 |
/// \pre init() must be called and a root node should be added |
1533 | 1533 |
/// with addSource() before using this function. |
1534 | 1534 |
/// |
1535 | 1535 |
/// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
1536 | 1536 |
/// not a node map. |
1537 | 1537 |
template <typename AM> |
1538 | 1538 |
Arc start(const AM &am) { |
1539 | 1539 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
1540 | 1540 |
processNextArc(); |
1541 | 1541 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
1542 | 1542 |
} |
1543 | 1543 |
|
1544 | 1544 |
/// \brief Runs the algorithm from the given source node. |
1545 | 1545 |
/// |
1546 | 1546 |
/// This method runs the %DFS algorithm from node \c s. |
1547 | 1547 |
/// in order to compute the DFS path to each node. |
1548 | 1548 |
/// |
1549 | 1549 |
/// The algorithm computes |
1550 | 1550 |
/// - the %DFS tree, |
1551 | 1551 |
/// - the distance of each node from the root in the %DFS tree. |
1552 | 1552 |
/// |
1553 | 1553 |
/// \note <tt>d.run(s)</tt> is just a shortcut of the following code. |
1554 | 1554 |
///\code |
1555 | 1555 |
/// d.init(); |
1556 | 1556 |
/// d.addSource(s); |
1557 | 1557 |
/// d.start(); |
1558 | 1558 |
///\endcode |
1559 | 1559 |
void run(Node s) { |
1560 | 1560 |
init(); |
1561 | 1561 |
addSource(s); |
1562 | 1562 |
start(); |
1563 | 1563 |
} |
1564 | 1564 |
|
1565 | 1565 |
/// \brief Finds the %DFS path between \c s and \c t. |
1566 | 1566 |
|
1567 | 1567 |
/// This method runs the %DFS algorithm from node \c s |
1568 | 1568 |
/// in order to compute the DFS path to node \c t |
1569 | 1569 |
/// (it stops searching when \c t is processed). |
1570 | 1570 |
/// |
1571 | 1571 |
/// \return \c true if \c t is reachable form \c s. |
1572 | 1572 |
/// |
1573 | 1573 |
/// \note Apart from the return value, <tt>d.run(s,t)</tt> is |
1574 | 1574 |
/// just a shortcut of the following code. |
1575 | 1575 |
///\code |
1576 | 1576 |
/// d.init(); |
1577 | 1577 |
/// d.addSource(s); |
1578 | 1578 |
/// d.start(t); |
1579 | 1579 |
///\endcode |
1580 | 1580 |
bool run(Node s,Node t) { |
1581 | 1581 |
init(); |
1582 | 1582 |
addSource(s); |
1583 | 1583 |
start(t); |
1584 | 1584 |
return reached(t); |
1585 | 1585 |
} |
1586 | 1586 |
|
1587 | 1587 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
1588 | 1588 |
|
1589 | 1589 |
/// This method runs the %DFS algorithm in order to |
1590 | 1590 |
/// compute the %DFS path to each node. |
1591 | 1591 |
/// |
1592 | 1592 |
/// The algorithm computes |
1593 | 1593 |
/// - the %DFS tree, |
1594 | 1594 |
/// - the distance of each node from the root in the %DFS tree. |
1595 | 1595 |
/// |
1596 | 1596 |
/// \note <tt>d.run()</tt> is just a shortcut of the following code. |
1597 | 1597 |
///\code |
1598 | 1598 |
/// d.init(); |
1599 | 1599 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
1600 | 1600 |
/// if (!d.reached(n)) { |
1601 | 1601 |
/// d.addSource(n); |
1602 | 1602 |
/// d.start(); |
1603 | 1603 |
/// } |
1604 | 1604 |
/// } |
1605 | 1605 |
///\endcode |
1606 | 1606 |
void run() { |
1607 | 1607 |
init(); |
1608 | 1608 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1609 | 1609 |
if (!reached(it)) { |
1610 | 1610 |
addSource(it); |
1611 | 1611 |
start(); |
1612 | 1612 |
} |
1613 | 1613 |
} |
1614 | 1614 |
} |
1615 | 1615 |
|
1616 | 1616 |
///@} |
1617 | 1617 |
|
1618 | 1618 |
/// \name Query Functions |
1619 | 1619 |
/// The result of the %DFS algorithm can be obtained using these |
1620 | 1620 |
/// functions.\n |
1621 | 1621 |
/// Either \ref lemon::DfsVisit::run() "run()" or |
1622 | 1622 |
/// \ref lemon::DfsVisit::start() "start()" must be called before |
1623 | 1623 |
/// using them. |
1624 | 1624 |
///@{ |
1625 | 1625 |
|
1626 | 1626 |
/// \brief Checks if a node is reachable from the root(s). |
1627 | 1627 |
/// |
1628 | 1628 |
/// Returns \c true if \c v is reachable from the root(s). |
1629 | 1629 |
/// \pre Either \ref run() or \ref start() |
1630 | 1630 |
/// must be called before using this function. |
1631 | 1631 |
bool reached(Node v) { return (*_reached)[v]; } |
1632 | 1632 |
|
1633 | 1633 |
///@} |
1634 | 1634 |
|
1635 | 1635 |
}; |
1636 | 1636 |
|
1637 | 1637 |
} //END OF NAMESPACE LEMON |
1638 | 1638 |
|
1639 | 1639 |
#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_GRAPH_TO_EPS_H |
20 | 20 |
#define LEMON_GRAPH_TO_EPS_H |
21 | 21 |
|
22 | 22 |
#include<iostream> |
23 | 23 |
#include<fstream> |
24 | 24 |
#include<sstream> |
25 | 25 |
#include<algorithm> |
26 | 26 |
#include<vector> |
27 | 27 |
|
28 | 28 |
#ifndef WIN32 |
29 | 29 |
#include<sys/time.h> |
30 | 30 |
#include<ctime> |
31 | 31 |
#else |
32 | 32 |
#include<lemon/bits/windows.h> |
33 | 33 |
#endif |
34 | 34 |
|
35 | 35 |
#include<lemon/math.h> |
36 | 36 |
#include<lemon/core.h> |
37 | 37 |
#include<lemon/dim2.h> |
38 | 38 |
#include<lemon/maps.h> |
39 | 39 |
#include<lemon/color.h> |
40 | 40 |
#include<lemon/bits/bezier.h> |
41 | 41 |
#include<lemon/error.h> |
42 | 42 |
|
43 | 43 |
|
44 | 44 |
///\ingroup eps_io |
45 | 45 |
///\file |
46 | 46 |
///\brief A well configurable tool for visualizing graphs |
47 | 47 |
|
48 | 48 |
namespace lemon { |
49 | 49 |
|
50 | 50 |
namespace _graph_to_eps_bits { |
51 | 51 |
template<class MT> |
52 | 52 |
class _NegY { |
53 | 53 |
public: |
54 | 54 |
typedef typename MT::Key Key; |
55 | 55 |
typedef typename MT::Value Value; |
56 | 56 |
const MT ↦ |
57 | 57 |
int yscale; |
58 | 58 |
_NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {} |
59 | 59 |
Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);} |
60 | 60 |
}; |
61 | 61 |
} |
62 | 62 |
|
63 | 63 |
///Default traits class of GraphToEps |
64 | 64 |
|
65 | 65 |
///Default traits class of \ref GraphToEps. |
66 | 66 |
/// |
67 | 67 |
///\c G is the type of the underlying graph. |
68 | 68 |
template<class G> |
69 | 69 |
struct DefaultGraphToEpsTraits |
70 | 70 |
{ |
71 | 71 |
typedef G Graph; |
72 | 72 |
typedef typename Graph::Node Node; |
73 | 73 |
typedef typename Graph::NodeIt NodeIt; |
74 | 74 |
typedef typename Graph::Arc Arc; |
75 | 75 |
typedef typename Graph::ArcIt ArcIt; |
76 | 76 |
typedef typename Graph::InArcIt InArcIt; |
77 | 77 |
typedef typename Graph::OutArcIt OutArcIt; |
78 | 78 |
|
79 | 79 |
|
80 | 80 |
const Graph &g; |
81 | 81 |
|
82 | 82 |
std::ostream& os; |
83 | 83 |
|
84 | 84 |
typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType; |
85 | 85 |
CoordsMapType _coords; |
86 | 86 |
ConstMap<typename Graph::Node,double > _nodeSizes; |
87 | 87 |
ConstMap<typename Graph::Node,int > _nodeShapes; |
88 | 88 |
|
89 | 89 |
ConstMap<typename Graph::Node,Color > _nodeColors; |
90 | 90 |
ConstMap<typename Graph::Arc,Color > _arcColors; |
91 | 91 |
|
92 | 92 |
ConstMap<typename Graph::Arc,double > _arcWidths; |
93 | 93 |
|
94 | 94 |
double _arcWidthScale; |
95 | 95 |
|
96 | 96 |
double _nodeScale; |
97 | 97 |
double _xBorder, _yBorder; |
98 | 98 |
double _scale; |
99 | 99 |
double _nodeBorderQuotient; |
100 | 100 |
|
101 | 101 |
bool _drawArrows; |
102 | 102 |
double _arrowLength, _arrowWidth; |
103 | 103 |
|
104 | 104 |
bool _showNodes, _showArcs; |
105 | 105 |
|
106 | 106 |
bool _enableParallel; |
107 | 107 |
double _parArcDist; |
108 | 108 |
|
109 | 109 |
bool _showNodeText; |
110 | 110 |
ConstMap<typename Graph::Node,bool > _nodeTexts; |
111 | 111 |
double _nodeTextSize; |
112 | 112 |
|
113 | 113 |
bool _showNodePsText; |
114 | 114 |
ConstMap<typename Graph::Node,bool > _nodePsTexts; |
115 | 115 |
char *_nodePsTextsPreamble; |
116 | 116 |
|
117 | 117 |
bool _undirected; |
118 | 118 |
|
119 | 119 |
bool _pleaseRemoveOsStream; |
120 | 120 |
|
121 | 121 |
bool _scaleToA4; |
122 | 122 |
|
123 | 123 |
std::string _title; |
124 | 124 |
std::string _copyright; |
125 | 125 |
|
126 | 126 |
enum NodeTextColorType |
127 | 127 |
{ DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType; |
128 | 128 |
ConstMap<typename Graph::Node,Color > _nodeTextColors; |
129 | 129 |
|
130 | 130 |
bool _autoNodeScale; |
131 | 131 |
bool _autoArcWidthScale; |
132 | 132 |
|
133 | 133 |
bool _absoluteNodeSizes; |
134 | 134 |
bool _absoluteArcWidths; |
135 | 135 |
|
136 | 136 |
bool _negY; |
137 | 137 |
|
138 | 138 |
bool _preScale; |
139 | 139 |
///Constructor |
140 | 140 |
|
141 | 141 |
///Constructor |
142 | 142 |
///\param _g Reference to the graph to be printed. |
143 | 143 |
///\param _os Reference to the output stream. |
144 | 144 |
///\param _os Reference to the output stream. |
145 | 145 |
///By default it is <tt>std::cout</tt>. |
146 | 146 |
///\param _pros If it is \c true, then the \c ostream referenced by \c _os |
147 | 147 |
///will be explicitly deallocated by the destructor. |
148 | 148 |
DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout, |
149 | 149 |
bool _pros=false) : |
150 | 150 |
g(_g), os(_os), |
151 | 151 |
_coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0), |
152 | 152 |
_nodeColors(WHITE), _arcColors(BLACK), |
153 | 153 |
_arcWidths(1.0), _arcWidthScale(0.003), |
154 | 154 |
_nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0), |
155 | 155 |
_nodeBorderQuotient(.1), |
156 | 156 |
_drawArrows(false), _arrowLength(1), _arrowWidth(0.3), |
157 | 157 |
_showNodes(true), _showArcs(true), |
158 | 158 |
_enableParallel(false), _parArcDist(1), |
159 | 159 |
_showNodeText(false), _nodeTexts(false), _nodeTextSize(1), |
160 | 160 |
_showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0), |
161 | 161 |
_undirected(lemon::UndirectedTagIndicator<G>::value), |
162 | 162 |
_pleaseRemoveOsStream(_pros), _scaleToA4(false), |
163 | 163 |
_nodeTextColorType(SAME_COL), _nodeTextColors(BLACK), |
164 | 164 |
_autoNodeScale(false), |
165 | 165 |
_autoArcWidthScale(false), |
166 | 166 |
_absoluteNodeSizes(false), |
167 | 167 |
_absoluteArcWidths(false), |
168 | 168 |
_negY(false), |
169 | 169 |
_preScale(true) |
170 | 170 |
{} |
171 | 171 |
}; |
172 | 172 |
|
173 | 173 |
///Auxiliary class to implement the named parameters of \ref graphToEps() |
174 | 174 |
|
175 | 175 |
///Auxiliary class to implement the named parameters of \ref graphToEps(). |
176 | 176 |
/// |
177 | 177 |
///For detailed examples see the \ref graph_to_eps_demo.cc demo file. |
178 | 178 |
template<class T> class GraphToEps : public T |
179 | 179 |
{ |
180 | 180 |
// Can't believe it is required by the C++ standard |
181 | 181 |
using T::g; |
182 | 182 |
using T::os; |
183 | 183 |
|
184 | 184 |
using T::_coords; |
185 | 185 |
using T::_nodeSizes; |
186 | 186 |
using T::_nodeShapes; |
187 | 187 |
using T::_nodeColors; |
188 | 188 |
using T::_arcColors; |
189 | 189 |
using T::_arcWidths; |
190 | 190 |
|
191 | 191 |
using T::_arcWidthScale; |
192 | 192 |
using T::_nodeScale; |
193 | 193 |
using T::_xBorder; |
194 | 194 |
using T::_yBorder; |
195 | 195 |
using T::_scale; |
196 | 196 |
using T::_nodeBorderQuotient; |
197 | 197 |
|
198 | 198 |
using T::_drawArrows; |
199 | 199 |
using T::_arrowLength; |
200 | 200 |
using T::_arrowWidth; |
201 | 201 |
|
202 | 202 |
using T::_showNodes; |
203 | 203 |
using T::_showArcs; |
204 | 204 |
|
205 | 205 |
using T::_enableParallel; |
206 | 206 |
using T::_parArcDist; |
207 | 207 |
|
208 | 208 |
using T::_showNodeText; |
209 | 209 |
using T::_nodeTexts; |
210 | 210 |
using T::_nodeTextSize; |
211 | 211 |
|
212 | 212 |
using T::_showNodePsText; |
213 | 213 |
using T::_nodePsTexts; |
214 | 214 |
using T::_nodePsTextsPreamble; |
215 | 215 |
|
216 | 216 |
using T::_undirected; |
217 | 217 |
|
218 | 218 |
using T::_pleaseRemoveOsStream; |
219 | 219 |
|
220 | 220 |
using T::_scaleToA4; |
221 | 221 |
|
222 | 222 |
using T::_title; |
223 | 223 |
using T::_copyright; |
224 | 224 |
|
225 | 225 |
using T::NodeTextColorType; |
226 | 226 |
using T::CUST_COL; |
227 | 227 |
using T::DIST_COL; |
228 | 228 |
using T::DIST_BW; |
229 | 229 |
using T::_nodeTextColorType; |
230 | 230 |
using T::_nodeTextColors; |
231 | 231 |
|
232 | 232 |
using T::_autoNodeScale; |
233 | 233 |
using T::_autoArcWidthScale; |
234 | 234 |
|
235 | 235 |
using T::_absoluteNodeSizes; |
236 | 236 |
using T::_absoluteArcWidths; |
237 | 237 |
|
238 | 238 |
|
239 | 239 |
using T::_negY; |
240 | 240 |
using T::_preScale; |
241 | 241 |
|
242 | 242 |
// dradnats ++C eht yb deriuqer si ti eveileb t'naC |
243 | 243 |
|
244 | 244 |
typedef typename T::Graph Graph; |
245 | 245 |
typedef typename Graph::Node Node; |
246 | 246 |
typedef typename Graph::NodeIt NodeIt; |
247 | 247 |
typedef typename Graph::Arc Arc; |
248 | 248 |
typedef typename Graph::ArcIt ArcIt; |
249 | 249 |
typedef typename Graph::InArcIt InArcIt; |
250 | 250 |
typedef typename Graph::OutArcIt OutArcIt; |
251 | 251 |
|
252 | 252 |
static const int INTERPOL_PREC; |
253 | 253 |
static const double A4HEIGHT; |
254 | 254 |
static const double A4WIDTH; |
255 | 255 |
static const double A4BORDER; |
256 | 256 |
|
257 | 257 |
bool dontPrint; |
258 | 258 |
|
259 | 259 |
public: |
260 | 260 |
///Node shapes |
261 | 261 |
|
262 | 262 |
///Node shapes. |
263 | 263 |
/// |
264 | 264 |
enum NodeShapes { |
265 | 265 |
/// = 0 |
266 | 266 |
///\image html nodeshape_0.png |
267 | 267 |
///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm |
268 | 268 |
CIRCLE=0, |
269 | 269 |
/// = 1 |
270 | 270 |
///\image html nodeshape_1.png |
271 | 271 |
///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm |
272 | 272 |
/// |
273 | 273 |
SQUARE=1, |
274 | 274 |
/// = 2 |
275 | 275 |
///\image html nodeshape_2.png |
276 | 276 |
///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm |
277 | 277 |
/// |
278 | 278 |
DIAMOND=2, |
279 | 279 |
/// = 3 |
280 | 280 |
///\image html nodeshape_3.png |
281 | 281 |
///\image latex nodeshape_2.eps "MALE shape (4)" width=2cm |
282 | 282 |
/// |
283 | 283 |
MALE=3, |
284 | 284 |
/// = 4 |
285 | 285 |
///\image html nodeshape_4.png |
286 | 286 |
///\image latex nodeshape_2.eps "FEMALE shape (4)" width=2cm |
287 | 287 |
/// |
288 | 288 |
FEMALE=4 |
289 | 289 |
}; |
290 | 290 |
|
291 | 291 |
private: |
292 | 292 |
class arcLess { |
293 | 293 |
const Graph &g; |
294 | 294 |
public: |
295 | 295 |
arcLess(const Graph &_g) : g(_g) {} |
296 | 296 |
bool operator()(Arc a,Arc b) const |
297 | 297 |
{ |
298 | 298 |
Node ai=std::min(g.source(a),g.target(a)); |
299 | 299 |
Node aa=std::max(g.source(a),g.target(a)); |
300 | 300 |
Node bi=std::min(g.source(b),g.target(b)); |
301 | 301 |
Node ba=std::max(g.source(b),g.target(b)); |
302 | 302 |
return ai<bi || |
303 | 303 |
(ai==bi && (aa < ba || |
304 | 304 |
(aa==ba && ai==g.source(a) && bi==g.target(b)))); |
305 | 305 |
} |
306 | 306 |
}; |
307 | 307 |
bool isParallel(Arc e,Arc f) const |
308 | 308 |
{ |
309 | 309 |
return (g.source(e)==g.source(f)&& |
310 | 310 |
g.target(e)==g.target(f)) || |
311 | 311 |
(g.source(e)==g.target(f)&& |
312 | 312 |
g.target(e)==g.source(f)); |
313 | 313 |
} |
314 | 314 |
template<class TT> |
315 | 315 |
static std::string psOut(const dim2::Point<TT> &p) |
316 | 316 |
{ |
317 | 317 |
std::ostringstream os; |
318 | 318 |
os << p.x << ' ' << p.y; |
319 | 319 |
return os.str(); |
320 | 320 |
} |
321 | 321 |
static std::string psOut(const Color &c) |
322 | 322 |
{ |
323 | 323 |
std::ostringstream os; |
324 | 324 |
os << c.red() << ' ' << c.green() << ' ' << c.blue(); |
325 | 325 |
return os.str(); |
326 | 326 |
} |
327 | 327 |
|
328 | 328 |
public: |
329 | 329 |
GraphToEps(const T &t) : T(t), dontPrint(false) {}; |
330 | 330 |
|
331 | 331 |
template<class X> struct CoordsTraits : public T { |
332 | 332 |
typedef X CoordsMapType; |
333 | 333 |
const X &_coords; |
334 | 334 |
CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {} |
335 | 335 |
}; |
336 | 336 |
///Sets the map of the node coordinates |
337 | 337 |
|
338 | 338 |
///Sets the map of the node coordinates. |
339 | 339 |
///\param x must be a node map with \ref dim2::Point "dim2::Point<double>" or |
340 | 340 |
///\ref dim2::Point "dim2::Point<int>" values. |
341 | 341 |
template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) { |
342 | 342 |
dontPrint=true; |
343 | 343 |
return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x)); |
344 | 344 |
} |
345 | 345 |
template<class X> struct NodeSizesTraits : public T { |
346 | 346 |
const X &_nodeSizes; |
347 | 347 |
NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {} |
348 | 348 |
}; |
349 | 349 |
///Sets the map of the node sizes |
350 | 350 |
|
351 | 351 |
///Sets the map of the node sizes. |
352 | 352 |
///\param x must be a node map with \c double (or convertible) values. |
353 | 353 |
template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x) |
354 | 354 |
{ |
355 | 355 |
dontPrint=true; |
356 | 356 |
return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x)); |
357 | 357 |
} |
358 | 358 |
template<class X> struct NodeShapesTraits : public T { |
359 | 359 |
const X &_nodeShapes; |
360 | 360 |
NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {} |
361 | 361 |
}; |
362 | 362 |
///Sets the map of the node shapes |
363 | 363 |
|
364 | 364 |
///Sets the map of the node shapes. |
365 | 365 |
///The available shape values |
366 | 366 |
///can be found in \ref NodeShapes "enum NodeShapes". |
367 | 367 |
///\param x must be a node map with \c int (or convertible) values. |
368 | 368 |
///\sa NodeShapes |
369 | 369 |
template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x) |
370 | 370 |
{ |
371 | 371 |
dontPrint=true; |
372 | 372 |
return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x)); |
373 | 373 |
} |
374 | 374 |
template<class X> struct NodeTextsTraits : public T { |
375 | 375 |
const X &_nodeTexts; |
376 | 376 |
NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {} |
377 | 377 |
}; |
378 | 378 |
///Sets the text printed on the nodes |
379 | 379 |
|
380 | 380 |
///Sets the text printed on the nodes. |
381 | 381 |
///\param x must be a node map with type that can be pushed to a standard |
382 | 382 |
///\c ostream. |
383 | 383 |
template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x) |
384 | 384 |
{ |
385 | 385 |
dontPrint=true; |
386 | 386 |
_showNodeText=true; |
387 | 387 |
return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x)); |
388 | 388 |
} |
389 | 389 |
template<class X> struct NodePsTextsTraits : public T { |
390 | 390 |
const X &_nodePsTexts; |
391 | 391 |
NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {} |
392 | 392 |
}; |
393 | 393 |
///Inserts a PostScript block to the nodes |
394 | 394 |
|
395 | 395 |
///With this command it is possible to insert a verbatim PostScript |
396 | 396 |
///block to the nodes. |
397 | 397 |
///The PS current point will be moved to the center of the node before |
398 | 398 |
///the PostScript block inserted. |
399 | 399 |
/// |
400 | 400 |
///Before and after the block a newline character is inserted so you |
401 | 401 |
///don't have to bother with the separators. |
402 | 402 |
/// |
403 | 403 |
///\param x must be a node map with type that can be pushed to a standard |
404 | 404 |
///\c ostream. |
405 | 405 |
/// |
406 | 406 |
///\sa nodePsTextsPreamble() |
407 | 407 |
template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x) |
408 | 408 |
{ |
409 | 409 |
dontPrint=true; |
410 | 410 |
_showNodePsText=true; |
411 | 411 |
return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x)); |
412 | 412 |
} |
413 | 413 |
template<class X> struct ArcWidthsTraits : public T { |
414 | 414 |
const X &_arcWidths; |
415 | 415 |
ArcWidthsTraits(const T &t,const X &x) : T(t), _arcWidths(x) {} |
416 | 416 |
}; |
417 | 417 |
///Sets the map of the arc widths |
418 | 418 |
|
419 | 419 |
///Sets the map of the arc widths. |
420 | 420 |
///\param x must be an arc map with \c double (or convertible) values. |
421 | 421 |
template<class X> GraphToEps<ArcWidthsTraits<X> > arcWidths(const X &x) |
422 | 422 |
{ |
423 | 423 |
dontPrint=true; |
424 | 424 |
return GraphToEps<ArcWidthsTraits<X> >(ArcWidthsTraits<X>(*this,x)); |
425 | 425 |
} |
426 | 426 |
|
427 | 427 |
template<class X> struct NodeColorsTraits : public T { |
428 | 428 |
const X &_nodeColors; |
429 | 429 |
NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {} |
430 | 430 |
}; |
431 | 431 |
///Sets the map of the node colors |
432 | 432 |
|
433 | 433 |
///Sets the map of the node colors. |
434 | 434 |
///\param x must be a node map with \ref Color values. |
435 | 435 |
/// |
436 | 436 |
///\sa Palette |
437 | 437 |
template<class X> GraphToEps<NodeColorsTraits<X> > |
438 | 438 |
nodeColors(const X &x) |
439 | 439 |
{ |
440 | 440 |
dontPrint=true; |
441 | 441 |
return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x)); |
442 | 442 |
} |
443 | 443 |
template<class X> struct NodeTextColorsTraits : public T { |
444 | 444 |
const X &_nodeTextColors; |
445 | 445 |
NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {} |
446 | 446 |
}; |
447 | 447 |
///Sets the map of the node text colors |
448 | 448 |
|
449 | 449 |
///Sets the map of the node text colors. |
450 | 450 |
///\param x must be a node map with \ref Color values. |
451 | 451 |
/// |
452 | 452 |
///\sa Palette |
453 | 453 |
template<class X> GraphToEps<NodeTextColorsTraits<X> > |
454 | 454 |
nodeTextColors(const X &x) |
455 | 455 |
{ |
456 | 456 |
dontPrint=true; |
457 | 457 |
_nodeTextColorType=CUST_COL; |
458 | 458 |
return GraphToEps<NodeTextColorsTraits<X> > |
459 | 459 |
(NodeTextColorsTraits<X>(*this,x)); |
460 | 460 |
} |
461 | 461 |
template<class X> struct ArcColorsTraits : public T { |
462 | 462 |
const X &_arcColors; |
463 | 463 |
ArcColorsTraits(const T &t,const X &x) : T(t), _arcColors(x) {} |
464 | 464 |
}; |
465 | 465 |
///Sets the map of the arc colors |
466 | 466 |
|
467 | 467 |
///Sets the map of the arc colors. |
468 | 468 |
///\param x must be an arc map with \ref Color values. |
469 | 469 |
/// |
470 | 470 |
///\sa Palette |
471 | 471 |
template<class X> GraphToEps<ArcColorsTraits<X> > |
472 | 472 |
arcColors(const X &x) |
473 | 473 |
{ |
474 | 474 |
dontPrint=true; |
475 | 475 |
return GraphToEps<ArcColorsTraits<X> >(ArcColorsTraits<X>(*this,x)); |
476 | 476 |
} |
477 | 477 |
///Sets a global scale factor for node sizes |
478 | 478 |
|
479 | 479 |
///Sets a global scale factor for node sizes. |
480 | 480 |
/// |
481 | 481 |
/// If nodeSizes() is not given, this function simply sets the node |
482 | 482 |
/// sizes to \c d. If nodeSizes() is given, but |
483 | 483 |
/// autoNodeScale() is not, then the node size given by |
484 | 484 |
/// nodeSizes() will be multiplied by the value \c d. |
485 | 485 |
/// If both nodeSizes() and autoNodeScale() are used, then the |
486 | 486 |
/// node sizes will be scaled in such a way that the greatest size will be |
487 | 487 |
/// equal to \c d. |
488 | 488 |
/// \sa nodeSizes() |
489 | 489 |
/// \sa autoNodeScale() |
490 | 490 |
GraphToEps<T> &nodeScale(double d=.01) {_nodeScale=d;return *this;} |
491 | 491 |
///Turns on/off the automatic node size scaling. |
492 | 492 |
|
493 | 493 |
///Turns on/off the automatic node size scaling. |
494 | 494 |
/// |
495 | 495 |
///\sa nodeScale() |
496 | 496 |
/// |
497 | 497 |
GraphToEps<T> &autoNodeScale(bool b=true) { |
498 | 498 |
_autoNodeScale=b;return *this; |
499 | 499 |
} |
500 | 500 |
|
501 | 501 |
///Turns on/off the absolutematic node size scaling. |
502 | 502 |
|
503 | 503 |
///Turns on/off the absolutematic node size scaling. |
504 | 504 |
/// |
505 | 505 |
///\sa nodeScale() |
506 | 506 |
/// |
507 | 507 |
GraphToEps<T> &absoluteNodeSizes(bool b=true) { |
508 | 508 |
_absoluteNodeSizes=b;return *this; |
509 | 509 |
} |
510 | 510 |
|
511 | 511 |
///Negates the Y coordinates. |
512 | 512 |
GraphToEps<T> &negateY(bool b=true) { |
513 | 513 |
_negY=b;return *this; |
514 | 514 |
} |
515 | 515 |
|
516 | 516 |
///Turn on/off pre-scaling |
517 | 517 |
|
518 | 518 |
///By default graphToEps() rescales the whole image in order to avoid |
519 | 519 |
///very big or very small bounding boxes. |
520 | 520 |
/// |
521 | 521 |
///This (p)rescaling can be turned off with this function. |
522 | 522 |
/// |
523 | 523 |
GraphToEps<T> &preScale(bool b=true) { |
524 | 524 |
_preScale=b;return *this; |
525 | 525 |
} |
526 | 526 |
|
527 | 527 |
///Sets a global scale factor for arc widths |
528 | 528 |
|
529 | 529 |
/// Sets a global scale factor for arc widths. |
530 | 530 |
/// |
531 | 531 |
/// If arcWidths() is not given, this function simply sets the arc |
532 | 532 |
/// widths to \c d. If arcWidths() is given, but |
533 | 533 |
/// autoArcWidthScale() is not, then the arc withs given by |
534 | 534 |
/// arcWidths() will be multiplied by the value \c d. |
535 | 535 |
/// If both arcWidths() and autoArcWidthScale() are used, then the |
536 | 536 |
/// arc withs will be scaled in such a way that the greatest width will be |
537 | 537 |
/// equal to \c d. |
538 | 538 |
GraphToEps<T> &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;} |
539 | 539 |
///Turns on/off the automatic arc width scaling. |
540 | 540 |
|
541 | 541 |
///Turns on/off the automatic arc width scaling. |
542 | 542 |
/// |
543 | 543 |
///\sa arcWidthScale() |
544 | 544 |
/// |
545 | 545 |
GraphToEps<T> &autoArcWidthScale(bool b=true) { |
546 | 546 |
_autoArcWidthScale=b;return *this; |
547 | 547 |
} |
548 | 548 |
///Turns on/off the absolutematic arc width scaling. |
549 | 549 |
|
550 | 550 |
///Turns on/off the absolutematic arc width scaling. |
551 | 551 |
/// |
552 | 552 |
///\sa arcWidthScale() |
553 | 553 |
/// |
554 | 554 |
GraphToEps<T> &absoluteArcWidths(bool b=true) { |
555 | 555 |
_absoluteArcWidths=b;return *this; |
556 | 556 |
} |
557 | 557 |
///Sets a global scale factor for the whole picture |
558 | 558 |
GraphToEps<T> &scale(double d) {_scale=d;return *this;} |
559 | 559 |
///Sets the width of the border around the picture |
560 | 560 |
GraphToEps<T> &border(double b=10) {_xBorder=_yBorder=b;return *this;} |
561 | 561 |
///Sets the width of the border around the picture |
562 | 562 |
GraphToEps<T> &border(double x, double y) { |
563 | 563 |
_xBorder=x;_yBorder=y;return *this; |
564 | 564 |
} |
565 | 565 |
///Sets whether to draw arrows |
566 | 566 |
GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;} |
567 | 567 |
///Sets the length of the arrowheads |
568 | 568 |
GraphToEps<T> &arrowLength(double d=1.0) {_arrowLength*=d;return *this;} |
569 | 569 |
///Sets the width of the arrowheads |
570 | 570 |
GraphToEps<T> &arrowWidth(double d=.3) {_arrowWidth*=d;return *this;} |
571 | 571 |
|
572 | 572 |
///Scales the drawing to fit to A4 page |
573 | 573 |
GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;} |
574 | 574 |
|
575 | 575 |
///Enables parallel arcs |
576 | 576 |
GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;} |
577 | 577 |
|
578 | 578 |
///Sets the distance between parallel arcs |
579 | 579 |
GraphToEps<T> &parArcDist(double d) {_parArcDist*=d;return *this;} |
580 | 580 |
|
581 | 581 |
///Hides the arcs |
582 | 582 |
GraphToEps<T> &hideArcs(bool b=true) {_showArcs=!b;return *this;} |
583 | 583 |
///Hides the nodes |
584 | 584 |
GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;} |
585 | 585 |
|
586 | 586 |
///Sets the size of the node texts |
587 | 587 |
GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;} |
588 | 588 |
|
589 | 589 |
///Sets the color of the node texts to be different from the node color |
590 | 590 |
|
591 | 591 |
///Sets the color of the node texts to be as different from the node color |
592 | 592 |
///as it is possible. |
593 | 593 |
GraphToEps<T> &distantColorNodeTexts() |
594 | 594 |
{_nodeTextColorType=DIST_COL;return *this;} |
595 | 595 |
///Sets the color of the node texts to be black or white and always visible. |
596 | 596 |
|
597 | 597 |
///Sets the color of the node texts to be black or white according to |
598 | 598 |
///which is more different from the node color. |
599 | 599 |
GraphToEps<T> &distantBWNodeTexts() |
600 | 600 |
{_nodeTextColorType=DIST_BW;return *this;} |
601 | 601 |
|
602 | 602 |
///Gives a preamble block for node Postscript block. |
603 | 603 |
|
604 | 604 |
///Gives a preamble block for node Postscript block. |
605 | 605 |
/// |
606 | 606 |
///\sa nodePsTexts() |
607 | 607 |
GraphToEps<T> & nodePsTextsPreamble(const char *str) { |
608 | 608 |
_nodePsTextsPreamble=str ;return *this; |
609 | 609 |
} |
610 | 610 |
///Sets whether the graph is undirected |
611 | 611 |
|
612 | 612 |
///Sets whether the graph is undirected. |
613 | 613 |
/// |
614 | 614 |
///This setting is the default for undirected graphs. |
615 | 615 |
/// |
616 | 616 |
///\sa directed() |
617 | 617 |
GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;} |
618 | 618 |
|
619 | 619 |
///Sets whether the graph is directed |
620 | 620 |
|
621 | 621 |
///Sets whether the graph is directed. |
622 | 622 |
///Use it to show the edges as a pair of directed ones. |
623 | 623 |
/// |
624 | 624 |
///This setting is the default for digraphs. |
625 | 625 |
/// |
626 | 626 |
///\sa undirected() |
627 | 627 |
GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;} |
628 | 628 |
|
629 | 629 |
///Sets the title. |
630 | 630 |
|
631 | 631 |
///Sets the title of the generated image, |
632 | 632 |
///namely it inserts a <tt>%%Title:</tt> DSC field to the header of |
633 | 633 |
///the EPS file. |
634 | 634 |
GraphToEps<T> &title(const std::string &t) {_title=t;return *this;} |
635 | 635 |
///Sets the copyright statement. |
636 | 636 |
|
637 | 637 |
///Sets the copyright statement of the generated image, |
638 | 638 |
///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of |
639 | 639 |
///the EPS file. |
640 | 640 |
GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;} |
641 | 641 |
|
642 | 642 |
protected: |
643 | 643 |
bool isInsideNode(dim2::Point<double> p, double r,int t) |
644 | 644 |
{ |
645 | 645 |
switch(t) { |
646 | 646 |
case CIRCLE: |
647 | 647 |
case MALE: |
648 | 648 |
case FEMALE: |
649 | 649 |
return p.normSquare()<=r*r; |
650 | 650 |
case SQUARE: |
651 | 651 |
return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r; |
652 | 652 |
case DIAMOND: |
653 | 653 |
return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r; |
654 | 654 |
} |
655 | 655 |
return false; |
656 | 656 |
} |
657 | 657 |
|
658 | 658 |
public: |
659 | 659 |
~GraphToEps() { } |
660 | 660 |
|
661 | 661 |
///Draws the graph. |
662 | 662 |
|
663 | 663 |
///Like other functions using |
664 | 664 |
///\ref named-templ-func-param "named template parameters", |
665 | 665 |
///this function calls the algorithm itself, i.e. in this case |
666 | 666 |
///it draws the graph. |
667 | 667 |
void run() { |
668 | 668 |
const double EPSILON=1e-9; |
669 | 669 |
if(dontPrint) return; |
670 | 670 |
|
671 | 671 |
_graph_to_eps_bits::_NegY<typename T::CoordsMapType> |
672 | 672 |
mycoords(_coords,_negY); |
673 | 673 |
|
674 | 674 |
os << "%!PS-Adobe-2.0 EPSF-2.0\n"; |
675 | 675 |
if(_title.size()>0) os << "%%Title: " << _title << '\n'; |
676 | 676 |
if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n'; |
677 | 677 |
os << "%%Creator: LEMON, graphToEps()\n"; |
678 | 678 |
|
679 | 679 |
{ |
680 | 680 |
os << "%%CreationDate: "; |
681 | 681 |
#ifndef WIN32 |
682 | 682 |
timeval tv; |
683 | 683 |
gettimeofday(&tv, 0); |
684 | 684 |
|
685 | 685 |
char cbuf[26]; |
686 | 686 |
ctime_r(&tv.tv_sec,cbuf); |
687 | 687 |
os << cbuf; |
688 | 688 |
#else |
689 | 689 |
os << bits::getWinFormattedDate(); |
690 | 690 |
os << std::endl; |
691 | 691 |
#endif |
692 | 692 |
} |
693 | 693 |
|
694 | 694 |
if (_autoArcWidthScale) { |
695 | 695 |
double max_w=0; |
696 | 696 |
for(ArcIt e(g);e!=INVALID;++e) |
697 | 697 |
max_w=std::max(double(_arcWidths[e]),max_w); |
698 | 698 |
if(max_w>EPSILON) { |
699 | 699 |
_arcWidthScale/=max_w; |
700 | 700 |
} |
701 | 701 |
} |
702 | 702 |
|
703 | 703 |
if (_autoNodeScale) { |
704 | 704 |
double max_s=0; |
705 | 705 |
for(NodeIt n(g);n!=INVALID;++n) |
706 | 706 |
max_s=std::max(double(_nodeSizes[n]),max_s); |
707 | 707 |
if(max_s>EPSILON) { |
708 | 708 |
_nodeScale/=max_s; |
709 | 709 |
} |
710 | 710 |
} |
711 | 711 |
|
712 | 712 |
double diag_len = 1; |
713 | 713 |
if(!(_absoluteNodeSizes&&_absoluteArcWidths)) { |
714 | 714 |
dim2::Box<double> bb; |
715 | 715 |
for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]); |
716 | 716 |
if (bb.empty()) { |
717 | 717 |
bb = dim2::Box<double>(dim2::Point<double>(0,0)); |
718 | 718 |
} |
719 | 719 |
diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare()); |
720 | 720 |
if(diag_len<EPSILON) diag_len = 1; |
721 | 721 |
if(!_absoluteNodeSizes) _nodeScale*=diag_len; |
722 | 722 |
if(!_absoluteArcWidths) _arcWidthScale*=diag_len; |
723 | 723 |
} |
724 | 724 |
|
725 | 725 |
dim2::Box<double> bb; |
726 | 726 |
for(NodeIt n(g);n!=INVALID;++n) { |
727 | 727 |
double ns=_nodeSizes[n]*_nodeScale; |
728 | 728 |
dim2::Point<double> p(ns,ns); |
729 | 729 |
switch(_nodeShapes[n]) { |
730 | 730 |
case CIRCLE: |
731 | 731 |
case SQUARE: |
732 | 732 |
case DIAMOND: |
733 | 733 |
bb.add(p+mycoords[n]); |
734 | 734 |
bb.add(-p+mycoords[n]); |
735 | 735 |
break; |
736 | 736 |
case MALE: |
737 | 737 |
bb.add(-p+mycoords[n]); |
738 | 738 |
bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]); |
739 | 739 |
break; |
740 | 740 |
case FEMALE: |
741 | 741 |
bb.add(p+mycoords[n]); |
742 | 742 |
bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]); |
743 | 743 |
break; |
744 | 744 |
} |
745 | 745 |
} |
746 | 746 |
if (bb.empty()) { |
747 | 747 |
bb = dim2::Box<double>(dim2::Point<double>(0,0)); |
748 | 748 |
} |
749 | 749 |
|
750 | 750 |
if(_scaleToA4) |
751 | 751 |
os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n"; |
752 | 752 |
else { |
753 | 753 |
if(_preScale) { |
754 | 754 |
//Rescale so that BoundingBox won't be neither to big nor too small. |
755 | 755 |
while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10; |
756 | 756 |
while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10; |
757 | 757 |
} |
758 | 758 |
|
759 | 759 |
os << "%%BoundingBox: " |
760 | 760 |
<< int(floor(bb.left() * _scale - _xBorder)) << ' ' |
761 | 761 |
<< int(floor(bb.bottom() * _scale - _yBorder)) << ' ' |
762 | 762 |
<< int(ceil(bb.right() * _scale + _xBorder)) << ' ' |
763 | 763 |
<< int(ceil(bb.top() * _scale + _yBorder)) << '\n'; |
764 | 764 |
} |
765 | 765 |
|
766 | 766 |
os << "%%EndComments\n"; |
767 | 767 |
|
768 | 768 |
//x1 y1 x2 y2 x3 y3 cr cg cb w |
769 | 769 |
os << "/lb { setlinewidth setrgbcolor newpath moveto\n" |
770 | 770 |
<< " 4 2 roll 1 index 1 index curveto stroke } bind def\n"; |
771 | 771 |
os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke }" |
772 | 772 |
<< " bind def\n"; |
773 | 773 |
//x y r |
774 | 774 |
os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath }" |
775 | 775 |
<< " bind def\n"; |
776 | 776 |
//x y r |
777 | 777 |
os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n" |
778 | 778 |
<< " 2 index 1 index sub 2 index 2 index add lineto\n" |
779 | 779 |
<< " 2 index 1 index sub 2 index 2 index sub lineto\n" |
780 | 780 |
<< " 2 index 1 index add 2 index 2 index sub lineto\n" |
781 | 781 |
<< " closepath pop pop pop} bind def\n"; |
782 | 782 |
//x y r |
783 | 783 |
os << "/di { newpath 2 index 1 index add 2 index moveto\n" |
784 | 784 |
<< " 2 index 2 index 2 index add lineto\n" |
785 | 785 |
<< " 2 index 1 index sub 2 index lineto\n" |
786 | 786 |
<< " 2 index 2 index 2 index sub lineto\n" |
787 | 787 |
<< " closepath pop pop pop} bind def\n"; |
788 | 788 |
// x y r cr cg cb |
789 | 789 |
os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n" |
790 | 790 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
791 | 791 |
<< " } bind def\n"; |
792 | 792 |
os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n" |
793 | 793 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n" |
794 | 794 |
<< " } bind def\n"; |
795 | 795 |
os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n" |
796 | 796 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n" |
797 | 797 |
<< " } bind def\n"; |
798 | 798 |
os << "/nfemale { 0 0 0 setrgbcolor 3 index " |
799 | 799 |
<< _nodeBorderQuotient/(1+_nodeBorderQuotient) |
800 | 800 |
<< " 1.5 mul mul setlinewidth\n" |
801 | 801 |
<< " newpath 5 index 5 index moveto " |
802 | 802 |
<< "5 index 5 index 5 index 3.01 mul sub\n" |
803 | 803 |
<< " lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub" |
804 | 804 |
<< " moveto\n" |
805 | 805 |
<< " 5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto " |
806 | 806 |
<< "stroke\n" |
807 | 807 |
<< " 5 index 5 index 5 index c fill\n" |
808 | 808 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
809 | 809 |
<< " } bind def\n"; |
810 | 810 |
os << "/nmale {\n" |
811 | 811 |
<< " 0 0 0 setrgbcolor 3 index " |
812 | 812 |
<< _nodeBorderQuotient/(1+_nodeBorderQuotient) |
813 | 813 |
<<" 1.5 mul mul setlinewidth\n" |
814 | 814 |
<< " newpath 5 index 5 index moveto\n" |
815 | 815 |
<< " 5 index 4 index 1 mul 1.5 mul add\n" |
816 | 816 |
<< " 5 index 5 index 3 sqrt 1.5 mul mul add\n" |
817 | 817 |
<< " 1 index 1 index lineto\n" |
818 | 818 |
<< " 1 index 1 index 7 index sub moveto\n" |
819 | 819 |
<< " 1 index 1 index lineto\n" |
820 | 820 |
<< " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub" |
821 | 821 |
<< " lineto\n" |
822 | 822 |
<< " stroke\n" |
823 | 823 |
<< " 5 index 5 index 5 index c fill\n" |
824 | 824 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
825 | 825 |
<< " } bind def\n"; |
826 | 826 |
|
827 | 827 |
|
828 | 828 |
os << "/arrl " << _arrowLength << " def\n"; |
829 | 829 |
os << "/arrw " << _arrowWidth << " def\n"; |
830 | 830 |
// l dx_norm dy_norm |
831 | 831 |
os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n"; |
832 | 832 |
//len w dx_norm dy_norm x1 y1 cr cg cb |
833 | 833 |
os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx " |
834 | 834 |
<< "exch def\n" |
835 | 835 |
<< " /w exch def /len exch def\n" |
836 | 836 |
//<< "0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke" |
837 | 837 |
<< " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n" |
838 | 838 |
<< " len w sub arrl sub dx dy lrl\n" |
839 | 839 |
<< " arrw dy dx neg lrl\n" |
840 | 840 |
<< " dx arrl w add mul dy w 2 div arrw add mul sub\n" |
841 | 841 |
<< " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n" |
842 | 842 |
<< " dx arrl w add mul neg dy w 2 div arrw add mul sub\n" |
843 | 843 |
<< " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n" |
844 | 844 |
<< " arrw dy dx neg lrl\n" |
845 | 845 |
<< " len w sub arrl sub neg dx dy lrl\n" |
846 | 846 |
<< " closepath fill } bind def\n"; |
847 | 847 |
os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n" |
848 | 848 |
<< " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n"; |
849 | 849 |
|
850 | 850 |
os << "\ngsave\n"; |
851 | 851 |
if(_scaleToA4) |
852 | 852 |
if(bb.height()>bb.width()) { |
853 | 853 |
double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(), |
854 | 854 |
(A4WIDTH-2*A4BORDER)/bb.width()); |
855 | 855 |
os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' ' |
856 | 856 |
<< ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER |
857 | 857 |
<< " translate\n" |
858 | 858 |
<< sc << " dup scale\n" |
859 | 859 |
<< -bb.left() << ' ' << -bb.bottom() << " translate\n"; |
860 | 860 |
} |
861 | 861 |
else { |
862 | 862 |
double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(), |
863 | 863 |
(A4WIDTH-2*A4BORDER)/bb.height()); |
864 | 864 |
os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' ' |
865 | 865 |
<< ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER |
866 | 866 |
<< " translate\n" |
867 | 867 |
<< sc << " dup scale\n90 rotate\n" |
868 | 868 |
<< -bb.left() << ' ' << -bb.top() << " translate\n"; |
869 | 869 |
} |
870 | 870 |
else if(_scale!=1.0) os << _scale << " dup scale\n"; |
871 | 871 |
|
872 | 872 |
if(_showArcs) { |
873 | 873 |
os << "%Arcs:\ngsave\n"; |
874 | 874 |
if(_enableParallel) { |
875 | 875 |
std::vector<Arc> el; |
876 | 876 |
for(ArcIt e(g);e!=INVALID;++e) |
877 | 877 |
if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0 |
878 | 878 |
&&g.source(e)!=g.target(e)) |
879 | 879 |
el.push_back(e); |
880 | 880 |
std::sort(el.begin(),el.end(),arcLess(g)); |
881 | 881 |
|
882 | 882 |
typename std::vector<Arc>::iterator j; |
883 | 883 |
for(typename std::vector<Arc>::iterator i=el.begin();i!=el.end();i=j) { |
884 | 884 |
for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ; |
885 | 885 |
|
886 | 886 |
double sw=0; |
887 | 887 |
for(typename std::vector<Arc>::iterator e=i;e!=j;++e) |
888 | 888 |
sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist; |
889 | 889 |
sw-=_parArcDist; |
890 | 890 |
sw/=-2.0; |
891 | 891 |
dim2::Point<double> |
892 | 892 |
dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]); |
893 | 893 |
double l=std::sqrt(dvec.normSquare()); |
894 | 894 |
dim2::Point<double> d(dvec/std::max(l,EPSILON)); |
895 | 895 |
dim2::Point<double> m; |
896 | 896 |
// m=dim2::Point<double>(mycoords[g.target(*i)]+ |
897 | 897 |
// mycoords[g.source(*i)])/2.0; |
898 | 898 |
|
899 | 899 |
// m=dim2::Point<double>(mycoords[g.source(*i)])+ |
900 | 900 |
// dvec*(double(_nodeSizes[g.source(*i)])/ |
901 | 901 |
// (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)])); |
902 | 902 |
|
903 | 903 |
m=dim2::Point<double>(mycoords[g.source(*i)])+ |
904 | 904 |
d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0; |
905 | 905 |
|
906 | 906 |
for(typename std::vector<Arc>::iterator e=i;e!=j;++e) { |
907 | 907 |
sw+=_arcWidths[*e]*_arcWidthScale/2.0; |
908 | 908 |
dim2::Point<double> mm=m+rot90(d)*sw/.75; |
909 | 909 |
if(_drawArrows) { |
910 | 910 |
int node_shape; |
911 | 911 |
dim2::Point<double> s=mycoords[g.source(*e)]; |
912 | 912 |
dim2::Point<double> t=mycoords[g.target(*e)]; |
913 | 913 |
double rn=_nodeSizes[g.target(*e)]*_nodeScale; |
914 | 914 |
node_shape=_nodeShapes[g.target(*e)]; |
915 | 915 |
dim2::Bezier3 bez(s,mm,mm,t); |
916 | 916 |
double t1=0,t2=1; |
917 | 917 |
for(int ii=0;ii<INTERPOL_PREC;++ii) |
918 | 918 |
if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2; |
919 | 919 |
else t1=(t1+t2)/2; |
920 | 920 |
dim2::Point<double> apoint=bez((t1+t2)/2); |
921 | 921 |
rn = _arrowLength+_arcWidths[*e]*_arcWidthScale; |
922 | 922 |
rn*=rn; |
923 | 923 |
t2=(t1+t2)/2;t1=0; |
924 | 924 |
for(int ii=0;ii<INTERPOL_PREC;++ii) |
925 | 925 |
if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2; |
926 | 926 |
else t2=(t1+t2)/2; |
927 | 927 |
dim2::Point<double> linend=bez((t1+t2)/2); |
928 | 928 |
bez=bez.before((t1+t2)/2); |
929 | 929 |
// rn=_nodeSizes[g.source(*e)]*_nodeScale; |
930 | 930 |
// node_shape=_nodeShapes[g.source(*e)]; |
931 | 931 |
// t1=0;t2=1; |
932 | 932 |
// for(int i=0;i<INTERPOL_PREC;++i) |
933 | 933 |
// if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) |
934 | 934 |
// t1=(t1+t2)/2; |
935 | 935 |
// else t2=(t1+t2)/2; |
936 | 936 |
// bez=bez.after((t1+t2)/2); |
937 | 937 |
os << _arcWidths[*e]*_arcWidthScale << " setlinewidth " |
938 | 938 |
<< _arcColors[*e].red() << ' ' |
939 | 939 |
<< _arcColors[*e].green() << ' ' |
940 | 940 |
<< _arcColors[*e].blue() << " setrgbcolor newpath\n" |
941 | 941 |
<< bez.p1.x << ' ' << bez.p1.y << " moveto\n" |
942 | 942 |
<< bez.p2.x << ' ' << bez.p2.y << ' ' |
943 | 943 |
<< bez.p3.x << ' ' << bez.p3.y << ' ' |
944 | 944 |
<< bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n"; |
945 | 945 |
dim2::Point<double> dd(rot90(linend-apoint)); |
946 | 946 |
dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/ |
947 | 947 |
std::sqrt(dd.normSquare()); |
948 | 948 |
os << "newpath " << psOut(apoint) << " moveto " |
949 | 949 |
<< psOut(linend+dd) << " lineto " |
950 | 950 |
<< psOut(linend-dd) << " lineto closepath fill\n"; |
951 | 951 |
} |
952 | 952 |
else { |
953 | 953 |
os << mycoords[g.source(*e)].x << ' ' |
954 | 954 |
<< mycoords[g.source(*e)].y << ' ' |
955 | 955 |
<< mm.x << ' ' << mm.y << ' ' |
956 | 956 |
<< mycoords[g.target(*e)].x << ' ' |
957 | 957 |
<< mycoords[g.target(*e)].y << ' ' |
958 | 958 |
<< _arcColors[*e].red() << ' ' |
959 | 959 |
<< _arcColors[*e].green() << ' ' |
960 | 960 |
<< _arcColors[*e].blue() << ' ' |
961 | 961 |
<< _arcWidths[*e]*_arcWidthScale << " lb\n"; |
962 | 962 |
} |
963 | 963 |
sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist; |
964 | 964 |
} |
965 | 965 |
} |
966 | 966 |
} |
967 | 967 |
else for(ArcIt e(g);e!=INVALID;++e) |
968 | 968 |
if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0 |
969 | 969 |
&&g.source(e)!=g.target(e)) { |
970 | 970 |
if(_drawArrows) { |
971 | 971 |
dim2::Point<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]); |
972 | 972 |
double rn=_nodeSizes[g.target(e)]*_nodeScale; |
973 | 973 |
int node_shape=_nodeShapes[g.target(e)]; |
974 | 974 |
double t1=0,t2=1; |
975 | 975 |
for(int i=0;i<INTERPOL_PREC;++i) |
976 | 976 |
if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2; |
977 | 977 |
else t2=(t1+t2)/2; |
978 | 978 |
double l=std::sqrt(d.normSquare()); |
979 | 979 |
d/=l; |
980 | 980 |
|
981 | 981 |
os << l*(1-(t1+t2)/2) << ' ' |
982 | 982 |
<< _arcWidths[e]*_arcWidthScale << ' ' |
983 | 983 |
<< d.x << ' ' << d.y << ' ' |
984 | 984 |
<< mycoords[g.source(e)].x << ' ' |
985 | 985 |
<< mycoords[g.source(e)].y << ' ' |
986 | 986 |
<< _arcColors[e].red() << ' ' |
987 | 987 |
<< _arcColors[e].green() << ' ' |
988 | 988 |
<< _arcColors[e].blue() << " arr\n"; |
989 | 989 |
} |
990 | 990 |
else os << mycoords[g.source(e)].x << ' ' |
991 | 991 |
<< mycoords[g.source(e)].y << ' ' |
992 | 992 |
<< mycoords[g.target(e)].x << ' ' |
993 | 993 |
<< mycoords[g.target(e)].y << ' ' |
994 | 994 |
<< _arcColors[e].red() << ' ' |
995 | 995 |
<< _arcColors[e].green() << ' ' |
996 | 996 |
<< _arcColors[e].blue() << ' ' |
997 | 997 |
<< _arcWidths[e]*_arcWidthScale << " l\n"; |
998 | 998 |
} |
999 | 999 |
os << "grestore\n"; |
1000 | 1000 |
} |
1001 | 1001 |
if(_showNodes) { |
1002 | 1002 |
os << "%Nodes:\ngsave\n"; |
1003 | 1003 |
for(NodeIt n(g);n!=INVALID;++n) { |
1004 | 1004 |
os << mycoords[n].x << ' ' << mycoords[n].y << ' ' |
1005 | 1005 |
<< _nodeSizes[n]*_nodeScale << ' ' |
1006 | 1006 |
<< _nodeColors[n].red() << ' ' |
1007 | 1007 |
<< _nodeColors[n].green() << ' ' |
1008 | 1008 |
<< _nodeColors[n].blue() << ' '; |
1009 | 1009 |
switch(_nodeShapes[n]) { |
1010 | 1010 |
case CIRCLE: |
1011 | 1011 |
os<< "nc";break; |
1012 | 1012 |
case SQUARE: |
1013 | 1013 |
os<< "nsq";break; |
1014 | 1014 |
case DIAMOND: |
1015 | 1015 |
os<< "ndi";break; |
1016 | 1016 |
case MALE: |
1017 | 1017 |
os<< "nmale";break; |
1018 | 1018 |
case FEMALE: |
1019 | 1019 |
os<< "nfemale";break; |
1020 | 1020 |
} |
1021 | 1021 |
os<<'\n'; |
1022 | 1022 |
} |
1023 | 1023 |
os << "grestore\n"; |
1024 | 1024 |
} |
1025 | 1025 |
if(_showNodeText) { |
1026 | 1026 |
os << "%Node texts:\ngsave\n"; |
1027 | 1027 |
os << "/fosi " << _nodeTextSize << " def\n"; |
1028 | 1028 |
os << "(Helvetica) findfont fosi scalefont setfont\n"; |
1029 | 1029 |
for(NodeIt n(g);n!=INVALID;++n) { |
1030 | 1030 |
switch(_nodeTextColorType) { |
1031 | 1031 |
case DIST_COL: |
1032 | 1032 |
os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n"; |
1033 | 1033 |
break; |
1034 | 1034 |
case DIST_BW: |
1035 | 1035 |
os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n"; |
1036 | 1036 |
break; |
1037 | 1037 |
case CUST_COL: |
1038 | 1038 |
os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n"; |
1039 | 1039 |
break; |
1040 | 1040 |
default: |
1041 | 1041 |
os << "0 0 0 setrgbcolor\n"; |
1042 | 1042 |
} |
1043 | 1043 |
os << mycoords[n].x << ' ' << mycoords[n].y |
1044 | 1044 |
<< " (" << _nodeTexts[n] << ") cshow\n"; |
1045 | 1045 |
} |
1046 | 1046 |
os << "grestore\n"; |
1047 | 1047 |
} |
1048 | 1048 |
if(_showNodePsText) { |
1049 | 1049 |
os << "%Node PS blocks:\ngsave\n"; |
1050 | 1050 |
for(NodeIt n(g);n!=INVALID;++n) |
1051 | 1051 |
os << mycoords[n].x << ' ' << mycoords[n].y |
1052 | 1052 |
<< " moveto\n" << _nodePsTexts[n] << "\n"; |
1053 | 1053 |
os << "grestore\n"; |
1054 | 1054 |
} |
1055 | 1055 |
|
1056 | 1056 |
os << "grestore\nshowpage\n"; |
1057 | 1057 |
|
1058 | 1058 |
//CleanUp: |
1059 | 1059 |
if(_pleaseRemoveOsStream) {delete &os;} |
1060 | 1060 |
} |
1061 | 1061 |
|
1062 | 1062 |
///\name Aliases |
1063 | 1063 |
///These are just some aliases to other parameter setting functions. |
1064 | 1064 |
|
1065 | 1065 |
///@{ |
1066 | 1066 |
|
1067 | 1067 |
///An alias for arcWidths() |
1068 | 1068 |
template<class X> GraphToEps<ArcWidthsTraits<X> > edgeWidths(const X &x) |
1069 | 1069 |
{ |
1070 | 1070 |
return arcWidths(x); |
1071 | 1071 |
} |
1072 | 1072 |
|
1073 | 1073 |
///An alias for arcColors() |
1074 | 1074 |
template<class X> GraphToEps<ArcColorsTraits<X> > |
1075 | 1075 |
edgeColors(const X &x) |
1076 | 1076 |
{ |
1077 | 1077 |
return arcColors(x); |
1078 | 1078 |
} |
1079 | 1079 |
|
1080 | 1080 |
///An alias for arcWidthScale() |
1081 | 1081 |
GraphToEps<T> &edgeWidthScale(double d) {return arcWidthScale(d);} |
1082 | 1082 |
|
1083 | 1083 |
///An alias for autoArcWidthScale() |
1084 | 1084 |
GraphToEps<T> &autoEdgeWidthScale(bool b=true) |
1085 | 1085 |
{ |
1086 | 1086 |
return autoArcWidthScale(b); |
1087 | 1087 |
} |
1088 | 1088 |
|
1089 | 1089 |
///An alias for absoluteArcWidths() |
1090 | 1090 |
GraphToEps<T> &absoluteEdgeWidths(bool b=true) |
1091 | 1091 |
{ |
1092 | 1092 |
return absoluteArcWidths(b); |
1093 | 1093 |
} |
1094 | 1094 |
|
1095 | 1095 |
///An alias for parArcDist() |
1096 | 1096 |
GraphToEps<T> &parEdgeDist(double d) {return parArcDist(d);} |
1097 | 1097 |
|
1098 | 1098 |
///An alias for hideArcs() |
1099 | 1099 |
GraphToEps<T> &hideEdges(bool b=true) {return hideArcs(b);} |
1100 | 1100 |
|
1101 | 1101 |
///@} |
1102 | 1102 |
}; |
1103 | 1103 |
|
1104 | 1104 |
template<class T> |
1105 | 1105 |
const int GraphToEps<T>::INTERPOL_PREC = 20; |
1106 | 1106 |
template<class T> |
1107 | 1107 |
const double GraphToEps<T>::A4HEIGHT = 841.8897637795276; |
1108 | 1108 |
template<class T> |
1109 | 1109 |
const double GraphToEps<T>::A4WIDTH = 595.275590551181; |
1110 | 1110 |
template<class T> |
1111 | 1111 |
const double GraphToEps<T>::A4BORDER = 15; |
1112 | 1112 |
|
1113 | 1113 |
|
1114 | 1114 |
///Generates an EPS file from a graph |
1115 | 1115 |
|
1116 | 1116 |
///\ingroup eps_io |
1117 | 1117 |
///Generates an EPS file from a graph. |
1118 | 1118 |
///\param g Reference to the graph to be printed. |
1119 | 1119 |
///\param os Reference to the output stream. |
1120 | 1120 |
///By default it is <tt>std::cout</tt>. |
1121 | 1121 |
/// |
1122 | 1122 |
///This function also has a lot of |
1123 | 1123 |
///\ref named-templ-func-param "named parameters", |
1124 | 1124 |
///they are declared as the members of class \ref GraphToEps. The following |
1125 | 1125 |
///example shows how to use these parameters. |
1126 | 1126 |
///\code |
1127 | 1127 |
/// graphToEps(g,os).scale(10).coords(coords) |
1128 | 1128 |
/// .nodeScale(2).nodeSizes(sizes) |
1129 | 1129 |
/// .arcWidthScale(.4).run(); |
1130 | 1130 |
///\endcode |
1131 | 1131 |
/// |
1132 | 1132 |
///For more detailed examples see the \ref graph_to_eps_demo.cc demo file. |
1133 | 1133 |
/// |
1134 | 1134 |
///\warning Don't forget to put the \ref GraphToEps::run() "run()" |
1135 | 1135 |
///to the end of the parameter list. |
1136 | 1136 |
///\sa GraphToEps |
1137 | 1137 |
///\sa graphToEps(G &g, const char *file_name) |
1138 | 1138 |
template<class G> |
1139 | 1139 |
GraphToEps<DefaultGraphToEpsTraits<G> > |
1140 | 1140 |
graphToEps(G &g, std::ostream& os=std::cout) |
1141 | 1141 |
{ |
1142 | 1142 |
return |
1143 | 1143 |
GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os)); |
1144 | 1144 |
} |
1145 | 1145 |
|
1146 | 1146 |
///Generates an EPS file from a graph |
1147 | 1147 |
|
1148 | 1148 |
///\ingroup eps_io |
1149 | 1149 |
///This function does the same as |
1150 | 1150 |
///\ref graphToEps(G &g,std::ostream& os) |
1151 | 1151 |
///but it writes its output into the file \c file_name |
1152 | 1152 |
///instead of a stream. |
1153 | 1153 |
///\sa graphToEps(G &g, std::ostream& os) |
1154 | 1154 |
template<class G> |
1155 | 1155 |
GraphToEps<DefaultGraphToEpsTraits<G> > |
1156 | 1156 |
graphToEps(G &g,const char *file_name) |
1157 | 1157 |
{ |
1158 | 1158 |
std::ostream* os = new std::ofstream(file_name); |
1159 | 1159 |
if (!(*os)) { |
1160 | 1160 |
delete os; |
1161 | 1161 |
throw IoError("Cannot write file", file_name); |
1162 | 1162 |
} |
1163 | 1163 |
return GraphToEps<DefaultGraphToEpsTraits<G> > |
1164 | 1164 |
(DefaultGraphToEpsTraits<G>(g,*os,true)); |
1165 | 1165 |
} |
1166 | 1166 |
|
1167 | 1167 |
///Generates an EPS file from a graph |
1168 | 1168 |
|
1169 | 1169 |
///\ingroup eps_io |
1170 | 1170 |
///This function does the same as |
1171 | 1171 |
///\ref graphToEps(G &g,std::ostream& os) |
1172 | 1172 |
///but it writes its output into the file \c file_name |
1173 | 1173 |
///instead of a stream. |
1174 | 1174 |
///\sa graphToEps(G &g, std::ostream& os) |
1175 | 1175 |
template<class G> |
1176 | 1176 |
GraphToEps<DefaultGraphToEpsTraits<G> > |
1177 | 1177 |
graphToEps(G &g,const std::string& file_name) |
1178 | 1178 |
{ |
1179 | 1179 |
std::ostream* os = new std::ofstream(file_name.c_str()); |
1180 | 1180 |
if (!(*os)) { |
1181 | 1181 |
delete os; |
1182 | 1182 |
throw IoError("Cannot write file", file_name); |
1183 | 1183 |
} |
1184 | 1184 |
return GraphToEps<DefaultGraphToEpsTraits<G> > |
1185 | 1185 |
(DefaultGraphToEpsTraits<G>(g,*os,true)); |
1186 | 1186 |
} |
1187 | 1187 |
|
1188 | 1188 |
} //END OF NAMESPACE LEMON |
1189 | 1189 |
|
1190 | 1190 |
#endif // LEMON_GRAPH_TO_EPS_H |
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)