0
14
0
8
12
8
10
4
4
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BFS_H |
20 | 20 |
#define LEMON_BFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 | 24 |
///\brief Bfs algorithm. |
25 | 25 |
|
26 | 26 |
#include <lemon/list_graph.h> |
27 | 27 |
#include <lemon/graph_utils.h> |
28 | 28 |
#include <lemon/bits/path_dump.h> |
29 | 29 |
#include <lemon/bits/invalid.h> |
30 | 30 |
#include <lemon/error.h> |
31 | 31 |
#include <lemon/maps.h> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
|
36 | 36 |
|
37 | 37 |
///Default traits class of Bfs class. |
38 | 38 |
|
39 | 39 |
///Default traits class of Bfs class. |
40 |
///\ |
|
40 |
///\tparam GR Digraph type. |
|
41 | 41 |
template<class GR> |
42 | 42 |
struct BfsDefaultTraits |
43 | 43 |
{ |
44 | 44 |
///The digraph type the algorithm runs on. |
45 | 45 |
typedef GR Digraph; |
46 | 46 |
///\brief The type of the map that stores the last |
47 | 47 |
///arcs of the shortest paths. |
48 | 48 |
/// |
49 | 49 |
///The type of the map that stores the last |
50 | 50 |
///arcs of the shortest paths. |
51 | 51 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
52 | 52 |
/// |
53 | 53 |
typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap; |
54 | 54 |
///Instantiates a PredMap. |
55 | 55 |
|
56 | 56 |
///This function instantiates a \ref PredMap. |
57 | 57 |
///\param G is the digraph, to which we would like to define the PredMap. |
58 | 58 |
///\todo The digraph alone may be insufficient to initialize |
59 | 59 |
static PredMap *createPredMap(const GR &G) |
60 | 60 |
{ |
61 | 61 |
return new PredMap(G); |
62 | 62 |
} |
63 | 63 |
///The type of the map that indicates which nodes are processed. |
64 | 64 |
|
65 | 65 |
///The type of the map that indicates which nodes are processed. |
66 | 66 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
67 | 67 |
///\todo named parameter to set this type, function to read and write. |
68 | 68 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
69 | 69 |
///Instantiates a ProcessedMap. |
70 | 70 |
|
71 | 71 |
///This function instantiates a \ref ProcessedMap. |
72 | 72 |
///\param g is the digraph, to which |
73 | 73 |
///we would like to define the \ref ProcessedMap |
74 | 74 |
#ifdef DOXYGEN |
75 | 75 |
static ProcessedMap *createProcessedMap(const GR &g) |
76 | 76 |
#else |
77 | 77 |
static ProcessedMap *createProcessedMap(const GR &) |
78 | 78 |
#endif |
79 | 79 |
{ |
80 | 80 |
return new ProcessedMap(); |
81 | 81 |
} |
82 | 82 |
///The type of the map that indicates which nodes are reached. |
83 | 83 |
|
84 | 84 |
///The type of the map that indicates which nodes are reached. |
85 | 85 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
86 | 86 |
///\todo named parameter to set this type, function to read and write. |
87 | 87 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
88 | 88 |
///Instantiates a ReachedMap. |
89 | 89 |
|
90 | 90 |
///This function instantiates a \ref ReachedMap. |
91 | 91 |
///\param G is the digraph, to which |
92 | 92 |
///we would like to define the \ref ReachedMap. |
93 | 93 |
static ReachedMap *createReachedMap(const GR &G) |
94 | 94 |
{ |
95 | 95 |
return new ReachedMap(G); |
96 | 96 |
} |
97 | 97 |
///The type of the map that stores the dists of the nodes. |
98 | 98 |
|
99 | 99 |
///The type of the map that stores the dists of the nodes. |
100 | 100 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
101 | 101 |
/// |
102 | 102 |
typedef typename Digraph::template NodeMap<int> DistMap; |
103 | 103 |
///Instantiates a DistMap. |
104 | 104 |
|
105 | 105 |
///This function instantiates a \ref DistMap. |
106 | 106 |
///\param G is the digraph, to which we would like to define the \ref DistMap |
107 | 107 |
static DistMap *createDistMap(const GR &G) |
108 | 108 |
{ |
109 | 109 |
return new DistMap(G); |
110 | 110 |
} |
111 | 111 |
}; |
112 | 112 |
|
113 | 113 |
///%BFS algorithm class. |
114 | 114 |
|
115 | 115 |
///\ingroup search |
116 | 116 |
///This class provides an efficient implementation of the %BFS algorithm. |
117 | 117 |
/// |
118 |
///\ |
|
118 |
///\tparam GR The digraph type the algorithm runs on. The default value is |
|
119 | 119 |
///\ref ListDigraph. The value of GR is not used directly by Bfs, it |
120 | 120 |
///is only passed to \ref BfsDefaultTraits. |
121 |
///\ |
|
121 |
///\tparam TR Traits class to set various data types used by the algorithm. |
|
122 | 122 |
///The default traits class is |
123 | 123 |
///\ref BfsDefaultTraits "BfsDefaultTraits<GR>". |
124 | 124 |
///See \ref BfsDefaultTraits for the documentation of |
125 | 125 |
///a Bfs traits class. |
126 |
/// |
|
127 |
///\author Alpar Juttner |
|
128 | 126 |
|
129 | 127 |
#ifdef DOXYGEN |
130 | 128 |
template <typename GR, |
131 | 129 |
typename TR> |
132 | 130 |
#else |
133 | 131 |
template <typename GR=ListDigraph, |
134 | 132 |
typename TR=BfsDefaultTraits<GR> > |
135 | 133 |
#endif |
136 | 134 |
class Bfs { |
137 | 135 |
public: |
138 | 136 |
/** |
139 | 137 |
* \brief \ref Exception for uninitialized parameters. |
140 | 138 |
* |
141 | 139 |
* This error represents problems in the initialization |
142 | 140 |
* of the parameters of the algorithms. |
143 | 141 |
*/ |
144 | 142 |
class UninitializedParameter : public lemon::UninitializedParameter { |
145 | 143 |
public: |
146 | 144 |
virtual const char* what() const throw() { |
147 | 145 |
return "lemon::Bfs::UninitializedParameter"; |
148 | 146 |
} |
149 | 147 |
}; |
150 | 148 |
|
151 | 149 |
typedef TR Traits; |
152 | 150 |
///The type of the underlying digraph. |
153 | 151 |
typedef typename TR::Digraph Digraph; |
154 | 152 |
|
155 | 153 |
///\brief The type of the map that stores the last |
156 | 154 |
///arcs of the shortest paths. |
157 | 155 |
typedef typename TR::PredMap PredMap; |
158 | 156 |
///The type of the map indicating which nodes are reached. |
159 | 157 |
typedef typename TR::ReachedMap ReachedMap; |
160 | 158 |
///The type of the map indicating which nodes are processed. |
161 | 159 |
typedef typename TR::ProcessedMap ProcessedMap; |
162 | 160 |
///The type of the map that stores the dists of the nodes. |
163 | 161 |
typedef typename TR::DistMap DistMap; |
164 | 162 |
private: |
165 | 163 |
|
166 | 164 |
typedef typename Digraph::Node Node; |
167 | 165 |
typedef typename Digraph::NodeIt NodeIt; |
168 | 166 |
typedef typename Digraph::Arc Arc; |
169 | 167 |
typedef typename Digraph::OutArcIt OutArcIt; |
170 | 168 |
|
171 | 169 |
/// Pointer to the underlying digraph. |
172 | 170 |
const Digraph *G; |
173 | 171 |
///Pointer to the map of predecessors arcs. |
174 | 172 |
PredMap *_pred; |
175 | 173 |
///Indicates if \ref _pred is locally allocated (\c true) or not. |
176 | 174 |
bool local_pred; |
177 | 175 |
///Pointer to the map of distances. |
178 | 176 |
DistMap *_dist; |
179 | 177 |
///Indicates if \ref _dist is locally allocated (\c true) or not. |
180 | 178 |
bool local_dist; |
181 | 179 |
///Pointer to the map of reached status of the nodes. |
182 | 180 |
ReachedMap *_reached; |
183 | 181 |
///Indicates if \ref _reached is locally allocated (\c true) or not. |
184 | 182 |
bool local_reached; |
185 | 183 |
///Pointer to the map of processed status of the nodes. |
186 | 184 |
ProcessedMap *_processed; |
187 | 185 |
///Indicates if \ref _processed is locally allocated (\c true) or not. |
188 | 186 |
bool local_processed; |
189 | 187 |
|
190 | 188 |
std::vector<typename Digraph::Node> _queue; |
191 | 189 |
int _queue_head,_queue_tail,_queue_next_dist; |
192 | 190 |
int _curr_dist; |
193 | 191 |
|
194 | 192 |
///Creates the maps if necessary. |
195 | 193 |
|
196 | 194 |
///\todo Better memory allocation (instead of new). |
197 | 195 |
void create_maps() |
198 | 196 |
{ |
199 | 197 |
if(!_pred) { |
200 | 198 |
local_pred = true; |
201 | 199 |
_pred = Traits::createPredMap(*G); |
202 | 200 |
} |
203 | 201 |
if(!_dist) { |
204 | 202 |
local_dist = true; |
205 | 203 |
_dist = Traits::createDistMap(*G); |
206 | 204 |
} |
207 | 205 |
if(!_reached) { |
208 | 206 |
local_reached = true; |
209 | 207 |
_reached = Traits::createReachedMap(*G); |
210 | 208 |
} |
211 | 209 |
if(!_processed) { |
212 | 210 |
local_processed = true; |
213 | 211 |
_processed = Traits::createProcessedMap(*G); |
214 | 212 |
} |
215 | 213 |
} |
216 | 214 |
|
217 | 215 |
protected: |
218 | 216 |
|
219 | 217 |
Bfs() {} |
220 | 218 |
|
221 | 219 |
public: |
222 | 220 |
|
223 | 221 |
typedef Bfs Create; |
224 | 222 |
|
225 | 223 |
///\name Named template parameters |
226 | 224 |
|
227 | 225 |
///@{ |
228 | 226 |
|
229 | 227 |
template <class T> |
230 | 228 |
struct DefPredMapTraits : public Traits { |
231 | 229 |
typedef T PredMap; |
232 | 230 |
static PredMap *createPredMap(const Digraph &) |
233 | 231 |
{ |
234 | 232 |
throw UninitializedParameter(); |
235 | 233 |
} |
236 | 234 |
}; |
237 | 235 |
///\brief \ref named-templ-param "Named parameter" for setting |
238 | 236 |
///PredMap type |
239 | 237 |
/// |
240 | 238 |
///\ref named-templ-param "Named parameter" for setting PredMap type |
241 | 239 |
/// |
242 | 240 |
template <class T> |
243 | 241 |
struct DefPredMap : public Bfs< Digraph, DefPredMapTraits<T> > { |
244 | 242 |
typedef Bfs< Digraph, DefPredMapTraits<T> > Create; |
245 | 243 |
}; |
246 | 244 |
|
247 | 245 |
template <class T> |
248 | 246 |
struct DefDistMapTraits : public Traits { |
249 | 247 |
typedef T DistMap; |
250 | 248 |
static DistMap *createDistMap(const Digraph &) |
251 | 249 |
{ |
252 | 250 |
throw UninitializedParameter(); |
253 | 251 |
} |
254 | 252 |
}; |
255 | 253 |
///\brief \ref named-templ-param "Named parameter" for setting |
256 | 254 |
///DistMap type |
257 | 255 |
/// |
258 | 256 |
///\ref named-templ-param "Named parameter" for setting DistMap type |
259 | 257 |
/// |
260 | 258 |
template <class T> |
261 | 259 |
struct DefDistMap : public Bfs< Digraph, DefDistMapTraits<T> > { |
262 | 260 |
typedef Bfs< Digraph, DefDistMapTraits<T> > Create; |
263 | 261 |
}; |
264 | 262 |
|
265 | 263 |
template <class T> |
266 | 264 |
struct DefReachedMapTraits : public Traits { |
267 | 265 |
typedef T ReachedMap; |
268 | 266 |
static ReachedMap *createReachedMap(const Digraph &) |
269 | 267 |
{ |
270 | 268 |
throw UninitializedParameter(); |
271 | 269 |
} |
272 | 270 |
}; |
273 | 271 |
///\brief \ref named-templ-param "Named parameter" for setting |
274 | 272 |
///ReachedMap type |
275 | 273 |
/// |
276 | 274 |
///\ref named-templ-param "Named parameter" for setting ReachedMap type |
277 | 275 |
/// |
278 | 276 |
template <class T> |
279 | 277 |
struct DefReachedMap : public Bfs< Digraph, DefReachedMapTraits<T> > { |
280 | 278 |
typedef Bfs< Digraph, DefReachedMapTraits<T> > Create; |
281 | 279 |
}; |
282 | 280 |
|
283 | 281 |
template <class T> |
284 | 282 |
struct DefProcessedMapTraits : public Traits { |
285 | 283 |
typedef T ProcessedMap; |
286 | 284 |
static ProcessedMap *createProcessedMap(const Digraph &) |
287 | 285 |
{ |
288 | 286 |
throw UninitializedParameter(); |
289 | 287 |
} |
290 | 288 |
}; |
291 | 289 |
///\brief \ref named-templ-param "Named parameter" for setting |
292 | 290 |
///ProcessedMap type |
293 | 291 |
/// |
294 | 292 |
///\ref named-templ-param "Named parameter" for setting ProcessedMap type |
295 | 293 |
/// |
296 | 294 |
template <class T> |
297 | 295 |
struct DefProcessedMap : public Bfs< Digraph, DefProcessedMapTraits<T> > { |
298 | 296 |
typedef Bfs< Digraph, DefProcessedMapTraits<T> > Create; |
299 | 297 |
}; |
300 | 298 |
|
301 | 299 |
struct DefDigraphProcessedMapTraits : public Traits { |
302 | 300 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
303 | 301 |
static ProcessedMap *createProcessedMap(const Digraph &G) |
304 | 302 |
{ |
305 | 303 |
return new ProcessedMap(G); |
306 | 304 |
} |
307 | 305 |
}; |
308 | 306 |
///\brief \ref named-templ-param "Named parameter" |
309 | 307 |
///for setting the ProcessedMap type to be Digraph::NodeMap<bool>. |
310 | 308 |
/// |
311 | 309 |
///\ref named-templ-param "Named parameter" |
312 | 310 |
///for setting the ProcessedMap type to be Digraph::NodeMap<bool>. |
313 | 311 |
///If you don't set it explicitly, it will be automatically allocated. |
314 | 312 |
template <class T> |
315 | 313 |
struct DefProcessedMapToBeDefaultMap : |
316 | 314 |
public Bfs< Digraph, DefDigraphProcessedMapTraits> { |
317 | 315 |
typedef Bfs< Digraph, DefDigraphProcessedMapTraits> Create; |
318 | 316 |
}; |
319 | 317 |
|
320 | 318 |
///@} |
321 | 319 |
|
322 | 320 |
public: |
323 | 321 |
|
324 | 322 |
///Constructor. |
325 | 323 |
|
326 | 324 |
///\param _G the digraph the algorithm will run on. |
327 | 325 |
/// |
328 | 326 |
Bfs(const Digraph& _G) : |
329 | 327 |
G(&_G), |
330 | 328 |
_pred(NULL), local_pred(false), |
331 | 329 |
_dist(NULL), local_dist(false), |
332 | 330 |
_reached(NULL), local_reached(false), |
333 | 331 |
_processed(NULL), local_processed(false) |
334 | 332 |
{ } |
335 | 333 |
|
336 | 334 |
///Destructor. |
337 | 335 |
~Bfs() |
338 | 336 |
{ |
339 | 337 |
if(local_pred) delete _pred; |
340 | 338 |
if(local_dist) delete _dist; |
341 | 339 |
if(local_reached) delete _reached; |
342 | 340 |
if(local_processed) delete _processed; |
343 | 341 |
} |
344 | 342 |
|
345 | 343 |
///Sets the map storing the predecessor arcs. |
346 | 344 |
|
347 | 345 |
///Sets the map storing the predecessor arcs. |
348 | 346 |
///If you don't use this function before calling \ref run(), |
349 | 347 |
///it will allocate one. The destructor deallocates this |
350 | 348 |
///automatically allocated map, of course. |
351 | 349 |
///\return <tt> (*this) </tt> |
352 | 350 |
Bfs &predMap(PredMap &m) |
353 | 351 |
{ |
354 | 352 |
if(local_pred) { |
355 | 353 |
delete _pred; |
356 | 354 |
local_pred=false; |
357 | 355 |
} |
358 | 356 |
_pred = &m; |
359 | 357 |
return *this; |
360 | 358 |
} |
361 | 359 |
|
362 | 360 |
///Sets the map indicating the reached nodes. |
363 | 361 |
|
364 | 362 |
///Sets the map indicating the reached nodes. |
365 | 363 |
///If you don't use this function before calling \ref run(), |
366 | 364 |
///it will allocate one. The destructor deallocates this |
367 | 365 |
///automatically allocated map, of course. |
368 | 366 |
///\return <tt> (*this) </tt> |
369 | 367 |
Bfs &reachedMap(ReachedMap &m) |
370 | 368 |
{ |
371 | 369 |
if(local_reached) { |
372 | 370 |
delete _reached; |
373 | 371 |
local_reached=false; |
374 | 372 |
} |
375 | 373 |
_reached = &m; |
376 | 374 |
return *this; |
377 | 375 |
} |
378 | 376 |
|
379 | 377 |
///Sets the map indicating the processed nodes. |
380 | 378 |
|
381 | 379 |
///Sets the map indicating the processed nodes. |
382 | 380 |
///If you don't use this function before calling \ref run(), |
383 | 381 |
///it will allocate one. The destructor deallocates this |
384 | 382 |
///automatically allocated map, of course. |
385 | 383 |
///\return <tt> (*this) </tt> |
386 | 384 |
Bfs &processedMap(ProcessedMap &m) |
387 | 385 |
{ |
388 | 386 |
if(local_processed) { |
389 | 387 |
delete _processed; |
390 | 388 |
local_processed=false; |
391 | 389 |
} |
392 | 390 |
_processed = &m; |
393 | 391 |
return *this; |
394 | 392 |
} |
395 | 393 |
|
396 | 394 |
///Sets the map storing the distances calculated by the algorithm. |
397 | 395 |
|
398 | 396 |
///Sets the map storing the distances calculated by the algorithm. |
399 | 397 |
///If you don't use this function before calling \ref run(), |
400 | 398 |
///it will allocate one. The destructor deallocates this |
401 | 399 |
///automatically allocated map, of course. |
402 | 400 |
///\return <tt> (*this) </tt> |
403 | 401 |
Bfs &distMap(DistMap &m) |
404 | 402 |
{ |
405 | 403 |
if(local_dist) { |
406 | 404 |
delete _dist; |
407 | 405 |
local_dist=false; |
408 | 406 |
} |
409 | 407 |
_dist = &m; |
410 | 408 |
return *this; |
411 | 409 |
} |
412 | 410 |
|
413 | 411 |
public: |
414 | 412 |
///\name Execution control |
415 | 413 |
///The simplest way to execute the algorithm is to use |
416 | 414 |
///one of the member functions called \c run(...). |
417 | 415 |
///\n |
418 | 416 |
///If you need more control on the execution, |
419 | 417 |
///first you must call \ref init(), then you can add several source nodes |
420 | 418 |
///with \ref addSource(). |
421 | 419 |
///Finally \ref start() will perform the actual path |
422 | 420 |
///computation. |
423 | 421 |
|
424 | 422 |
///@{ |
425 | 423 |
|
426 | 424 |
///\brief Initializes the internal data structures. |
427 | 425 |
/// |
428 | 426 |
///Initializes the internal data structures. |
429 | 427 |
/// |
430 | 428 |
void init() |
431 | 429 |
{ |
432 | 430 |
create_maps(); |
433 | 431 |
_queue.resize(countNodes(*G)); |
434 | 432 |
_queue_head=_queue_tail=0; |
435 | 433 |
_curr_dist=1; |
436 | 434 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
437 | 435 |
_pred->set(u,INVALID); |
438 | 436 |
_reached->set(u,false); |
439 | 437 |
_processed->set(u,false); |
440 | 438 |
} |
441 | 439 |
} |
442 | 440 |
|
443 | 441 |
///Adds a new source node. |
444 | 442 |
|
445 | 443 |
///Adds a new source node to the set of nodes to be processed. |
446 | 444 |
/// |
447 | 445 |
void addSource(Node s) |
448 | 446 |
{ |
449 | 447 |
if(!(*_reached)[s]) |
450 | 448 |
{ |
451 | 449 |
_reached->set(s,true); |
452 | 450 |
_pred->set(s,INVALID); |
453 | 451 |
_dist->set(s,0); |
454 | 452 |
_queue[_queue_head++]=s; |
455 | 453 |
_queue_next_dist=_queue_head; |
456 | 454 |
} |
457 | 455 |
} |
458 | 456 |
|
459 | 457 |
///Processes the next node. |
460 | 458 |
|
461 | 459 |
///Processes the next node. |
462 | 460 |
/// |
463 | 461 |
///\return The processed node. |
464 | 462 |
/// |
465 | 463 |
///\warning The queue must not be empty! |
466 | 464 |
Node processNextNode() |
467 | 465 |
{ |
468 | 466 |
if(_queue_tail==_queue_next_dist) { |
469 | 467 |
_curr_dist++; |
470 | 468 |
_queue_next_dist=_queue_head; |
471 | 469 |
} |
472 | 470 |
Node n=_queue[_queue_tail++]; |
473 | 471 |
_processed->set(n,true); |
474 | 472 |
Node m; |
475 | 473 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
476 | 474 |
if(!(*_reached)[m=G->target(e)]) { |
477 | 475 |
_queue[_queue_head++]=m; |
478 | 476 |
_reached->set(m,true); |
479 | 477 |
_pred->set(m,e); |
480 | 478 |
_dist->set(m,_curr_dist); |
481 | 479 |
} |
482 | 480 |
return n; |
483 | 481 |
} |
484 | 482 |
|
485 | 483 |
///Processes the next node. |
486 | 484 |
|
487 | 485 |
///Processes the next node. And checks that the given target node |
488 | 486 |
///is reached. If the target node is reachable from the processed |
489 | 487 |
///node then the reached parameter will be set true. The reached |
490 | 488 |
///parameter should be initially false. |
491 | 489 |
/// |
492 | 490 |
///\param target The target node. |
493 | 491 |
///\retval reach Indicates that the target node is reached. |
494 | 492 |
///\return The processed node. |
495 | 493 |
/// |
496 | 494 |
///\warning The queue must not be empty! |
497 | 495 |
Node processNextNode(Node target, bool& reach) |
498 | 496 |
{ |
499 | 497 |
if(_queue_tail==_queue_next_dist) { |
500 | 498 |
_curr_dist++; |
501 | 499 |
_queue_next_dist=_queue_head; |
502 | 500 |
} |
503 | 501 |
Node n=_queue[_queue_tail++]; |
504 | 502 |
_processed->set(n,true); |
505 | 503 |
Node m; |
506 | 504 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
507 | 505 |
if(!(*_reached)[m=G->target(e)]) { |
508 | 506 |
_queue[_queue_head++]=m; |
509 | 507 |
_reached->set(m,true); |
510 | 508 |
_pred->set(m,e); |
511 | 509 |
_dist->set(m,_curr_dist); |
512 | 510 |
reach = reach || (target == m); |
513 | 511 |
} |
514 | 512 |
return n; |
515 | 513 |
} |
516 | 514 |
|
517 | 515 |
///Processes the next node. |
518 | 516 |
|
519 | 517 |
///Processes the next node. And checks that at least one of |
520 | 518 |
///reached node has true value in the \c nm node map. If one node |
521 | 519 |
///with true value is reachable from the processed node then the |
522 | 520 |
///rnode parameter will be set to the first of such nodes. |
523 | 521 |
/// |
524 | 522 |
///\param nm The node map of possible targets. |
525 | 523 |
///\retval rnode The reached target node. |
526 | 524 |
///\return The processed node. |
527 | 525 |
/// |
528 | 526 |
///\warning The queue must not be empty! |
529 | 527 |
template<class NM> |
530 | 528 |
Node processNextNode(const NM& nm, Node& rnode) |
531 | 529 |
{ |
532 | 530 |
if(_queue_tail==_queue_next_dist) { |
533 | 531 |
_curr_dist++; |
534 | 532 |
_queue_next_dist=_queue_head; |
535 | 533 |
} |
536 | 534 |
Node n=_queue[_queue_tail++]; |
537 | 535 |
_processed->set(n,true); |
538 | 536 |
Node m; |
539 | 537 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
540 | 538 |
if(!(*_reached)[m=G->target(e)]) { |
541 | 539 |
_queue[_queue_head++]=m; |
542 | 540 |
_reached->set(m,true); |
543 | 541 |
_pred->set(m,e); |
544 | 542 |
_dist->set(m,_curr_dist); |
545 | 543 |
if (nm[m] && rnode == INVALID) rnode = m; |
546 | 544 |
} |
547 | 545 |
return n; |
548 | 546 |
} |
549 | 547 |
|
550 | 548 |
///Next node to be processed. |
551 | 549 |
|
552 | 550 |
///Next node to be processed. |
553 | 551 |
/// |
554 | 552 |
///\return The next node to be processed or INVALID if the queue is |
555 | 553 |
/// empty. |
556 | 554 |
Node nextNode() |
557 | 555 |
{ |
558 | 556 |
return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID; |
559 | 557 |
} |
560 | 558 |
|
561 | 559 |
///\brief Returns \c false if there are nodes |
562 | 560 |
///to be processed in the queue |
563 | 561 |
/// |
564 | 562 |
///Returns \c false if there are nodes |
565 | 563 |
///to be processed in the queue |
566 | 564 |
bool emptyQueue() { return _queue_tail==_queue_head; } |
567 | 565 |
///Returns the number of the nodes to be processed. |
568 | 566 |
|
569 | 567 |
///Returns the number of the nodes to be processed in the queue. |
570 | 568 |
int queueSize() { return _queue_head-_queue_tail; } |
571 | 569 |
|
572 | 570 |
///Executes the algorithm. |
573 | 571 |
|
574 | 572 |
///Executes the algorithm. |
575 | 573 |
/// |
576 | 574 |
///\pre init() must be called and at least one node should be added |
577 | 575 |
///with addSource() before using this function. |
578 | 576 |
/// |
579 | 577 |
///This method runs the %BFS algorithm from the root node(s) |
580 | 578 |
///in order to |
581 | 579 |
///compute the |
582 | 580 |
///shortest path to each node. The algorithm computes |
583 | 581 |
///- The shortest path tree. |
584 | 582 |
///- The distance of each node from the root(s). |
585 | 583 |
void start() |
586 | 584 |
{ |
587 | 585 |
while ( !emptyQueue() ) processNextNode(); |
588 | 586 |
} |
589 | 587 |
|
590 | 588 |
///Executes the algorithm until \c dest is reached. |
591 | 589 |
|
592 | 590 |
///Executes the algorithm until \c dest is reached. |
593 | 591 |
/// |
594 | 592 |
///\pre init() must be called and at least one node should be added |
595 | 593 |
///with addSource() before using this function. |
596 | 594 |
/// |
597 | 595 |
///This method runs the %BFS algorithm from the root node(s) |
598 | 596 |
///in order to compute the shortest path to \c dest. |
599 | 597 |
///The algorithm computes |
600 | 598 |
///- The shortest path to \c dest. |
601 | 599 |
///- The distance of \c dest from the root(s). |
602 | 600 |
void start(Node dest) |
603 | 601 |
{ |
604 | 602 |
bool reach = false; |
605 | 603 |
while ( !emptyQueue() && !reach ) processNextNode(dest, reach); |
606 | 604 |
} |
607 | 605 |
|
608 | 606 |
///Executes the algorithm until a condition is met. |
609 | 607 |
|
610 | 608 |
///Executes the algorithm until a condition is met. |
611 | 609 |
/// |
612 | 610 |
///\pre init() must be called and at least one node should be added |
613 | 611 |
///with addSource() before using this function. |
614 | 612 |
/// |
615 | 613 |
///\param nm must be a bool (or convertible) node map. The |
616 | 614 |
///algorithm will stop when it reaches a node \c v with |
617 | 615 |
/// <tt>nm[v]</tt> true. |
618 | 616 |
/// |
619 | 617 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
620 | 618 |
///\c INVALID if no such node was found. |
621 | 619 |
template<class NM> |
622 | 620 |
Node start(const NM &nm) |
623 | 621 |
{ |
624 | 622 |
Node rnode = INVALID; |
625 | 623 |
while ( !emptyQueue() && rnode == INVALID ) { |
626 | 624 |
processNextNode(nm, rnode); |
627 | 625 |
} |
628 | 626 |
return rnode; |
629 | 627 |
} |
630 | 628 |
|
631 | 629 |
///Runs %BFS algorithm from node \c s. |
632 | 630 |
|
633 | 631 |
///This method runs the %BFS algorithm from a root node \c s |
634 | 632 |
///in order to |
635 | 633 |
///compute the |
636 | 634 |
///shortest path to each node. The algorithm computes |
637 | 635 |
///- The shortest path tree. |
638 | 636 |
///- The distance of each node from the root. |
639 | 637 |
/// |
640 | 638 |
///\note b.run(s) is just a shortcut of the following code. |
641 | 639 |
///\code |
642 | 640 |
/// b.init(); |
643 | 641 |
/// b.addSource(s); |
644 | 642 |
/// b.start(); |
645 | 643 |
///\endcode |
646 | 644 |
void run(Node s) { |
647 | 645 |
init(); |
648 | 646 |
addSource(s); |
649 | 647 |
start(); |
650 | 648 |
} |
651 | 649 |
|
652 | 650 |
///Finds the shortest path between \c s and \c t. |
653 | 651 |
|
654 | 652 |
///Finds the shortest path between \c s and \c t. |
655 | 653 |
/// |
656 | 654 |
///\return The length of the shortest s---t path if there exists one, |
657 | 655 |
///0 otherwise. |
658 | 656 |
///\note Apart from the return value, b.run(s) is |
659 | 657 |
///just a shortcut of the following code. |
660 | 658 |
///\code |
661 | 659 |
/// b.init(); |
662 | 660 |
/// b.addSource(s); |
663 | 661 |
/// b.start(t); |
664 | 662 |
///\endcode |
665 | 663 |
int run(Node s,Node t) { |
666 | 664 |
init(); |
667 | 665 |
addSource(s); |
668 | 666 |
start(t); |
669 | 667 |
return reached(t) ? _curr_dist : 0; |
670 | 668 |
} |
671 | 669 |
|
672 | 670 |
///@} |
673 | 671 |
|
674 | 672 |
///\name Query Functions |
675 | 673 |
///The result of the %BFS algorithm can be obtained using these |
676 | 674 |
///functions.\n |
677 | 675 |
///Before the use of these functions, |
678 | 676 |
///either run() or start() must be calleb. |
679 | 677 |
|
680 | 678 |
///@{ |
681 | 679 |
|
682 | 680 |
typedef PredMapPath<Digraph, PredMap> Path; |
683 | 681 |
|
684 | 682 |
///Gives back the shortest path. |
685 | 683 |
|
686 | 684 |
///Gives back the shortest path. |
687 | 685 |
///\pre The \c t should be reachable from the source. |
688 | 686 |
Path path(Node t) |
689 | 687 |
{ |
690 | 688 |
return Path(*G, *_pred, t); |
691 | 689 |
} |
692 | 690 |
|
693 | 691 |
///The distance of a node from the root(s). |
694 | 692 |
|
695 | 693 |
///Returns the distance of a node from the root(s). |
696 | 694 |
///\pre \ref run() must be called before using this function. |
697 | 695 |
///\warning If node \c v in unreachable from the root(s) the return value |
698 | 696 |
///of this function is undefined. |
699 | 697 |
int dist(Node v) const { return (*_dist)[v]; } |
700 | 698 |
|
701 | 699 |
///Returns the 'previous arc' of the shortest path tree. |
702 | 700 |
|
703 | 701 |
///For a node \c v it returns the 'previous arc' |
704 | 702 |
///of the shortest path tree, |
705 | 703 |
///i.e. it returns the last arc of a shortest path from the root(s) to \c |
706 | 704 |
///v. It is \ref INVALID |
707 | 705 |
///if \c v is unreachable from the root(s) or \c v is a root. The |
708 | 706 |
///shortest path tree used here is equal to the shortest path tree used in |
709 | 707 |
///\ref predNode(). |
710 | 708 |
///\pre Either \ref run() or \ref start() must be called before using |
711 | 709 |
///this function. |
712 | 710 |
Arc predArc(Node v) const { return (*_pred)[v];} |
713 | 711 |
|
714 | 712 |
///Returns the 'previous node' of the shortest path tree. |
715 | 713 |
|
716 | 714 |
///For a node \c v it returns the 'previous node' |
717 | 715 |
///of the shortest path tree, |
718 | 716 |
///i.e. it returns the last but one node from a shortest path from the |
719 | 717 |
///root(a) to \c /v. |
720 | 718 |
///It is INVALID if \c v is unreachable from the root(s) or |
721 | 719 |
///if \c v itself a root. |
722 | 720 |
///The shortest path tree used here is equal to the shortest path |
723 | 721 |
///tree used in \ref predArc(). |
724 | 722 |
///\pre Either \ref run() or \ref start() must be called before |
725 | 723 |
///using this function. |
726 | 724 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
727 | 725 |
G->source((*_pred)[v]); } |
728 | 726 |
|
729 | 727 |
///Returns a reference to the NodeMap of distances. |
730 | 728 |
|
731 | 729 |
///Returns a reference to the NodeMap of distances. |
732 | 730 |
///\pre Either \ref run() or \ref init() must |
733 | 731 |
///be called before using this function. |
734 | 732 |
const DistMap &distMap() const { return *_dist;} |
735 | 733 |
|
736 | 734 |
///Returns a reference to the shortest path tree map. |
737 | 735 |
|
738 | 736 |
///Returns a reference to the NodeMap of the arcs of the |
739 | 737 |
///shortest path tree. |
740 | 738 |
///\pre Either \ref run() or \ref init() |
741 | 739 |
///must be called before using this function. |
742 | 740 |
const PredMap &predMap() const { return *_pred;} |
743 | 741 |
|
744 | 742 |
///Checks if a node is reachable from the root. |
745 | 743 |
|
746 | 744 |
///Returns \c true if \c v is reachable from the root. |
747 | 745 |
///\warning The source nodes are indicated as unreached. |
748 | 746 |
///\pre Either \ref run() or \ref start() |
749 | 747 |
///must be called before using this function. |
750 | 748 |
/// |
751 | 749 |
bool reached(Node v) { return (*_reached)[v]; } |
752 | 750 |
|
753 | 751 |
///@} |
754 | 752 |
}; |
755 | 753 |
|
756 | 754 |
///Default traits class of Bfs function. |
757 | 755 |
|
758 | 756 |
///Default traits class of Bfs function. |
759 |
///\ |
|
757 |
///\tparam GR Digraph type. |
|
760 | 758 |
template<class GR> |
761 | 759 |
struct BfsWizardDefaultTraits |
762 | 760 |
{ |
763 | 761 |
///The digraph type the algorithm runs on. |
764 | 762 |
typedef GR Digraph; |
765 | 763 |
///\brief The type of the map that stores the last |
766 | 764 |
///arcs of the shortest paths. |
767 | 765 |
/// |
768 | 766 |
///The type of the map that stores the last |
769 | 767 |
///arcs of the shortest paths. |
770 | 768 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
771 | 769 |
/// |
772 | 770 |
typedef NullMap<typename Digraph::Node,typename GR::Arc> PredMap; |
773 | 771 |
///Instantiates a PredMap. |
774 | 772 |
|
775 | 773 |
///This function instantiates a \ref PredMap. |
776 | 774 |
///\param g is the digraph, to which we would like to define the PredMap. |
777 | 775 |
///\todo The digraph alone may be insufficient to initialize |
778 | 776 |
#ifdef DOXYGEN |
779 | 777 |
static PredMap *createPredMap(const GR &g) |
780 | 778 |
#else |
781 | 779 |
static PredMap *createPredMap(const GR &) |
782 | 780 |
#endif |
783 | 781 |
{ |
784 | 782 |
return new PredMap(); |
785 | 783 |
} |
786 | 784 |
|
787 | 785 |
///The type of the map that indicates which nodes are processed. |
788 | 786 |
|
789 | 787 |
///The type of the map that indicates which nodes are processed. |
790 | 788 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
791 | 789 |
///\todo named parameter to set this type, function to read and write. |
792 | 790 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
793 | 791 |
///Instantiates a ProcessedMap. |
794 | 792 |
|
795 | 793 |
///This function instantiates a \ref ProcessedMap. |
796 | 794 |
///\param g is the digraph, to which |
797 | 795 |
///we would like to define the \ref ProcessedMap |
798 | 796 |
#ifdef DOXYGEN |
799 | 797 |
static ProcessedMap *createProcessedMap(const GR &g) |
800 | 798 |
#else |
801 | 799 |
static ProcessedMap *createProcessedMap(const GR &) |
802 | 800 |
#endif |
803 | 801 |
{ |
804 | 802 |
return new ProcessedMap(); |
805 | 803 |
} |
806 | 804 |
///The type of the map that indicates which nodes are reached. |
807 | 805 |
|
808 | 806 |
///The type of the map that indicates which nodes are reached. |
809 | 807 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
810 | 808 |
///\todo named parameter to set this type, function to read and write. |
811 | 809 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
812 | 810 |
///Instantiates a ReachedMap. |
813 | 811 |
|
814 | 812 |
///This function instantiates a \ref ReachedMap. |
815 | 813 |
///\param G is the digraph, to which |
816 | 814 |
///we would like to define the \ref ReachedMap. |
817 | 815 |
static ReachedMap *createReachedMap(const GR &G) |
818 | 816 |
{ |
819 | 817 |
return new ReachedMap(G); |
820 | 818 |
} |
821 | 819 |
///The type of the map that stores the dists of the nodes. |
822 | 820 |
|
823 | 821 |
///The type of the map that stores the dists of the nodes. |
824 | 822 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
825 | 823 |
/// |
826 | 824 |
typedef NullMap<typename Digraph::Node,int> DistMap; |
827 | 825 |
///Instantiates a DistMap. |
828 | 826 |
|
829 | 827 |
///This function instantiates a \ref DistMap. |
830 | 828 |
///\param g is the digraph, to which we would like to define the \ref DistMap |
831 | 829 |
#ifdef DOXYGEN |
832 | 830 |
static DistMap *createDistMap(const GR &g) |
833 | 831 |
#else |
834 | 832 |
static DistMap *createDistMap(const GR &) |
835 | 833 |
#endif |
836 | 834 |
{ |
837 | 835 |
return new DistMap(); |
838 | 836 |
} |
839 | 837 |
}; |
840 | 838 |
|
841 | 839 |
/// Default traits used by \ref BfsWizard |
842 | 840 |
|
843 | 841 |
/// To make it easier to use Bfs algorithm |
844 | 842 |
///we have created a wizard class. |
845 | 843 |
/// This \ref BfsWizard class needs default traits, |
846 | 844 |
///as well as the \ref Bfs class. |
847 | 845 |
/// The \ref BfsWizardBase is a class to be the default traits of the |
848 | 846 |
/// \ref BfsWizard class. |
849 | 847 |
template<class GR> |
850 | 848 |
class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
851 | 849 |
{ |
852 | 850 |
|
853 | 851 |
typedef BfsWizardDefaultTraits<GR> Base; |
854 | 852 |
protected: |
855 | 853 |
/// Type of the nodes in the digraph. |
856 | 854 |
typedef typename Base::Digraph::Node Node; |
857 | 855 |
|
858 | 856 |
/// Pointer to the underlying digraph. |
859 | 857 |
void *_g; |
860 | 858 |
///Pointer to the map of reached nodes. |
861 | 859 |
void *_reached; |
862 | 860 |
///Pointer to the map of processed nodes. |
863 | 861 |
void *_processed; |
864 | 862 |
///Pointer to the map of predecessors arcs. |
865 | 863 |
void *_pred; |
866 | 864 |
///Pointer to the map of distances. |
867 | 865 |
void *_dist; |
868 | 866 |
///Pointer to the source node. |
869 | 867 |
Node _source; |
870 | 868 |
|
871 | 869 |
public: |
872 | 870 |
/// Constructor. |
873 | 871 |
|
874 | 872 |
/// This constructor does not require parameters, therefore it initiates |
875 | 873 |
/// all of the attributes to default values (0, INVALID). |
876 | 874 |
BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
877 | 875 |
_dist(0), _source(INVALID) {} |
878 | 876 |
|
879 | 877 |
/// Constructor. |
880 | 878 |
|
881 | 879 |
/// This constructor requires some parameters, |
882 | 880 |
/// listed in the parameters list. |
883 | 881 |
/// Others are initiated to 0. |
884 | 882 |
/// \param g is the initial value of \ref _g |
885 | 883 |
/// \param s is the initial value of \ref _source |
886 | 884 |
BfsWizardBase(const GR &g, Node s=INVALID) : |
887 | 885 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
888 | 886 |
_reached(0), _processed(0), _pred(0), _dist(0), _source(s) {} |
889 | 887 |
|
890 | 888 |
}; |
891 | 889 |
|
892 | 890 |
/// A class to make the usage of Bfs algorithm easier |
893 | 891 |
|
894 | 892 |
/// This class is created to make it easier to use Bfs algorithm. |
895 | 893 |
/// It uses the functions and features of the plain \ref Bfs, |
896 | 894 |
/// but it is much simpler to use it. |
897 | 895 |
/// |
898 | 896 |
/// Simplicity means that the way to change the types defined |
899 | 897 |
/// in the traits class is based on functions that returns the new class |
900 | 898 |
/// and not on templatable built-in classes. |
901 | 899 |
/// When using the plain \ref Bfs |
902 | 900 |
/// the new class with the modified type comes from |
903 | 901 |
/// the original class by using the :: |
904 | 902 |
/// operator. In the case of \ref BfsWizard only |
905 | 903 |
/// a function have to be called and it will |
906 | 904 |
/// return the needed class. |
907 | 905 |
/// |
908 | 906 |
/// It does not have own \ref run method. When its \ref run method is called |
909 | 907 |
/// it initiates a plain \ref Bfs class, and calls the \ref Bfs::run |
910 | 908 |
/// method of it. |
911 | 909 |
template<class TR> |
912 | 910 |
class BfsWizard : public TR |
913 | 911 |
{ |
914 | 912 |
typedef TR Base; |
915 | 913 |
|
916 | 914 |
///The type of the underlying digraph. |
917 | 915 |
typedef typename TR::Digraph Digraph; |
918 | 916 |
//\e |
919 | 917 |
typedef typename Digraph::Node Node; |
920 | 918 |
//\e |
921 | 919 |
typedef typename Digraph::NodeIt NodeIt; |
922 | 920 |
//\e |
923 | 921 |
typedef typename Digraph::Arc Arc; |
924 | 922 |
//\e |
925 | 923 |
typedef typename Digraph::OutArcIt OutArcIt; |
926 | 924 |
|
927 | 925 |
///\brief The type of the map that stores |
928 | 926 |
///the reached nodes |
929 | 927 |
typedef typename TR::ReachedMap ReachedMap; |
930 | 928 |
///\brief The type of the map that stores |
931 | 929 |
///the processed nodes |
932 | 930 |
typedef typename TR::ProcessedMap ProcessedMap; |
933 | 931 |
///\brief The type of the map that stores the last |
934 | 932 |
///arcs of the shortest paths. |
935 | 933 |
typedef typename TR::PredMap PredMap; |
936 | 934 |
///The type of the map that stores the dists of the nodes. |
937 | 935 |
typedef typename TR::DistMap DistMap; |
938 | 936 |
|
939 | 937 |
public: |
940 | 938 |
/// Constructor. |
941 | 939 |
BfsWizard() : TR() {} |
942 | 940 |
|
943 | 941 |
/// Constructor that requires parameters. |
944 | 942 |
|
945 | 943 |
/// Constructor that requires parameters. |
946 | 944 |
/// These parameters will be the default values for the traits class. |
947 | 945 |
BfsWizard(const Digraph &g, Node s=INVALID) : |
948 | 946 |
TR(g,s) {} |
949 | 947 |
|
950 | 948 |
///Copy constructor |
951 | 949 |
BfsWizard(const TR &b) : TR(b) {} |
952 | 950 |
|
953 | 951 |
~BfsWizard() {} |
954 | 952 |
|
955 | 953 |
///Runs Bfs algorithm from a given node. |
956 | 954 |
|
957 | 955 |
///Runs Bfs algorithm from a given node. |
958 | 956 |
///The node can be given by the \ref source function. |
959 | 957 |
void run() |
960 | 958 |
{ |
961 | 959 |
if(Base::_source==INVALID) throw UninitializedParameter(); |
962 | 960 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
963 | 961 |
if(Base::_reached) |
964 | 962 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
965 | 963 |
if(Base::_processed) |
966 | 964 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
967 | 965 |
if(Base::_pred) |
968 | 966 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
969 | 967 |
if(Base::_dist) |
970 | 968 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
971 | 969 |
alg.run(Base::_source); |
972 | 970 |
} |
973 | 971 |
|
974 | 972 |
///Runs Bfs algorithm from the given node. |
975 | 973 |
|
976 | 974 |
///Runs Bfs algorithm from the given node. |
977 | 975 |
///\param s is the given source. |
978 | 976 |
void run(Node s) |
979 | 977 |
{ |
980 | 978 |
Base::_source=s; |
981 | 979 |
run(); |
982 | 980 |
} |
983 | 981 |
|
984 | 982 |
template<class T> |
985 | 983 |
struct DefPredMapBase : public Base { |
986 | 984 |
typedef T PredMap; |
987 | 985 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
988 | 986 |
DefPredMapBase(const TR &b) : TR(b) {} |
989 | 987 |
}; |
990 | 988 |
|
991 | 989 |
///\brief \ref named-templ-param "Named parameter" |
992 | 990 |
///function for setting PredMap |
993 | 991 |
/// |
994 | 992 |
/// \ref named-templ-param "Named parameter" |
995 | 993 |
///function for setting PredMap |
996 | 994 |
/// |
997 | 995 |
template<class T> |
998 | 996 |
BfsWizard<DefPredMapBase<T> > predMap(const T &t) |
999 | 997 |
{ |
1000 | 998 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1001 | 999 |
return BfsWizard<DefPredMapBase<T> >(*this); |
1002 | 1000 |
} |
1003 | 1001 |
|
1004 | 1002 |
|
1005 | 1003 |
template<class T> |
1006 | 1004 |
struct DefReachedMapBase : public Base { |
1007 | 1005 |
typedef T ReachedMap; |
1008 | 1006 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
1009 | 1007 |
DefReachedMapBase(const TR &b) : TR(b) {} |
1010 | 1008 |
}; |
1011 | 1009 |
|
1012 | 1010 |
///\brief \ref named-templ-param "Named parameter" |
1013 | 1011 |
///function for setting ReachedMap |
1014 | 1012 |
/// |
1015 | 1013 |
/// \ref named-templ-param "Named parameter" |
1016 | 1014 |
///function for setting ReachedMap |
1017 | 1015 |
/// |
1018 | 1016 |
template<class T> |
1019 | 1017 |
BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) |
1020 | 1018 |
{ |
1021 | 1019 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1022 | 1020 |
return BfsWizard<DefReachedMapBase<T> >(*this); |
1023 | 1021 |
} |
1024 | 1022 |
|
1025 | 1023 |
|
1026 | 1024 |
template<class T> |
1027 | 1025 |
struct DefProcessedMapBase : public Base { |
1028 | 1026 |
typedef T ProcessedMap; |
1029 | 1027 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1030 | 1028 |
DefProcessedMapBase(const TR &b) : TR(b) {} |
1031 | 1029 |
}; |
1032 | 1030 |
|
1033 | 1031 |
///\brief \ref named-templ-param "Named parameter" |
1034 | 1032 |
///function for setting ProcessedMap |
1035 | 1033 |
/// |
1036 | 1034 |
/// \ref named-templ-param "Named parameter" |
1037 | 1035 |
///function for setting ProcessedMap |
1038 | 1036 |
/// |
1039 | 1037 |
template<class T> |
1040 | 1038 |
BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) |
1041 | 1039 |
{ |
1042 | 1040 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1043 | 1041 |
return BfsWizard<DefProcessedMapBase<T> >(*this); |
1044 | 1042 |
} |
1045 | 1043 |
|
1046 | 1044 |
|
1047 | 1045 |
template<class T> |
1048 | 1046 |
struct DefDistMapBase : public Base { |
1049 | 1047 |
typedef T DistMap; |
1050 | 1048 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1051 | 1049 |
DefDistMapBase(const TR &b) : TR(b) {} |
1052 | 1050 |
}; |
1053 | 1051 |
|
1054 | 1052 |
///\brief \ref named-templ-param "Named parameter" |
1055 | 1053 |
///function for setting DistMap type |
1056 | 1054 |
/// |
1057 | 1055 |
/// \ref named-templ-param "Named parameter" |
1058 | 1056 |
///function for setting DistMap type |
1059 | 1057 |
/// |
1060 | 1058 |
template<class T> |
1061 | 1059 |
BfsWizard<DefDistMapBase<T> > distMap(const T &t) |
1062 | 1060 |
{ |
1063 | 1061 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1064 | 1062 |
return BfsWizard<DefDistMapBase<T> >(*this); |
1065 | 1063 |
} |
1066 | 1064 |
|
1067 | 1065 |
/// Sets the source node, from which the Bfs algorithm runs. |
1068 | 1066 |
|
1069 | 1067 |
/// Sets the source node, from which the Bfs algorithm runs. |
1070 | 1068 |
/// \param s is the source node. |
1071 | 1069 |
BfsWizard<TR> &source(Node s) |
1072 | 1070 |
{ |
1073 | 1071 |
Base::_source=s; |
1074 | 1072 |
return *this; |
1075 | 1073 |
} |
1076 | 1074 |
|
1077 | 1075 |
}; |
1078 | 1076 |
|
1079 | 1077 |
///Function type interface for Bfs algorithm. |
1080 | 1078 |
|
1081 | 1079 |
/// \ingroup search |
1082 | 1080 |
///Function type interface for Bfs algorithm. |
1083 | 1081 |
/// |
1084 | 1082 |
///This function also has several |
1085 | 1083 |
///\ref named-templ-func-param "named parameters", |
1086 | 1084 |
///they are declared as the members of class \ref BfsWizard. |
1087 | 1085 |
///The following |
1088 | 1086 |
///example shows how to use these parameters. |
1089 | 1087 |
///\code |
1090 | 1088 |
/// bfs(g,source).predMap(preds).run(); |
1091 | 1089 |
///\endcode |
1092 | 1090 |
///\warning Don't forget to put the \ref BfsWizard::run() "run()" |
1093 | 1091 |
///to the end of the parameter list. |
1094 | 1092 |
///\sa BfsWizard |
1095 | 1093 |
///\sa Bfs |
1096 | 1094 |
template<class GR> |
1097 | 1095 |
BfsWizard<BfsWizardBase<GR> > |
1098 | 1096 |
bfs(const GR &g,typename GR::Node s=INVALID) |
1099 | 1097 |
{ |
1100 | 1098 |
return BfsWizard<BfsWizardBase<GR> >(g,s); |
1101 | 1099 |
} |
1102 | 1100 |
|
1103 | 1101 |
#ifdef DOXYGEN |
1104 | 1102 |
/// \brief Visitor class for bfs. |
1105 | 1103 |
/// |
1106 | 1104 |
/// This class defines the interface of the BfsVisit events, and |
1107 | 1105 |
/// it could be the base of a real Visitor class. |
1108 | 1106 |
template <typename _Digraph> |
1109 | 1107 |
struct BfsVisitor { |
1110 | 1108 |
typedef _Digraph Digraph; |
1111 | 1109 |
typedef typename Digraph::Arc Arc; |
1112 | 1110 |
typedef typename Digraph::Node Node; |
1113 | 1111 |
/// \brief Called when the arc reach a node. |
1114 | 1112 |
/// |
1115 | 1113 |
/// It is called when the bfs find an arc which target is not |
1116 | 1114 |
/// reached yet. |
1117 | 1115 |
void discover(const Arc& arc) {} |
1118 | 1116 |
/// \brief Called when the node reached first time. |
1119 | 1117 |
/// |
1120 | 1118 |
/// It is Called when the node reached first time. |
1121 | 1119 |
void reach(const Node& node) {} |
1122 | 1120 |
/// \brief Called when the arc examined but target of the arc |
1123 | 1121 |
/// already discovered. |
1124 | 1122 |
/// |
1125 | 1123 |
/// It called when the arc examined but the target of the arc |
1126 | 1124 |
/// already discovered. |
1127 | 1125 |
void examine(const Arc& arc) {} |
1128 | 1126 |
/// \brief Called for the source node of the bfs. |
1129 | 1127 |
/// |
1130 | 1128 |
/// It is called for the source node of the bfs. |
1131 | 1129 |
void start(const Node& node) {} |
1132 | 1130 |
/// \brief Called when the node processed. |
1133 | 1131 |
/// |
1134 | 1132 |
/// It is Called when the node processed. |
1135 | 1133 |
void process(const Node& node) {} |
1136 | 1134 |
}; |
1137 | 1135 |
#else |
1138 | 1136 |
template <typename _Digraph> |
1139 | 1137 |
struct BfsVisitor { |
1140 | 1138 |
typedef _Digraph Digraph; |
1141 | 1139 |
typedef typename Digraph::Arc Arc; |
1142 | 1140 |
typedef typename Digraph::Node Node; |
1143 | 1141 |
void discover(const Arc&) {} |
1144 | 1142 |
void reach(const Node&) {} |
1145 | 1143 |
void examine(const Arc&) {} |
1146 | 1144 |
void start(const Node&) {} |
1147 | 1145 |
void process(const Node&) {} |
1148 | 1146 |
|
1149 | 1147 |
template <typename _Visitor> |
1150 | 1148 |
struct Constraints { |
1151 | 1149 |
void constraints() { |
1152 | 1150 |
Arc arc; |
1153 | 1151 |
Node node; |
1154 | 1152 |
visitor.discover(arc); |
1155 | 1153 |
visitor.reach(node); |
1156 | 1154 |
visitor.examine(arc); |
1157 | 1155 |
visitor.start(node); |
1158 | 1156 |
visitor.process(node); |
1159 | 1157 |
} |
1160 | 1158 |
_Visitor& visitor; |
1161 | 1159 |
}; |
1162 | 1160 |
}; |
1163 | 1161 |
#endif |
1164 | 1162 |
|
1165 | 1163 |
/// \brief Default traits class of BfsVisit class. |
1166 | 1164 |
/// |
1167 | 1165 |
/// Default traits class of BfsVisit class. |
1168 |
/// \ |
|
1166 |
/// \tparam _Digraph Digraph type. |
|
1169 | 1167 |
template<class _Digraph> |
1170 | 1168 |
struct BfsVisitDefaultTraits { |
1171 | 1169 |
|
1172 | 1170 |
/// \brief The digraph type the algorithm runs on. |
1173 | 1171 |
typedef _Digraph Digraph; |
1174 | 1172 |
|
1175 | 1173 |
/// \brief The type of the map that indicates which nodes are reached. |
1176 | 1174 |
/// |
1177 | 1175 |
/// The type of the map that indicates which nodes are reached. |
1178 | 1176 |
/// It must meet the \ref concepts::WriteMap "WriteMap" concept. |
1179 | 1177 |
/// \todo named parameter to set this type, function to read and write. |
1180 | 1178 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1181 | 1179 |
|
1182 | 1180 |
/// \brief Instantiates a ReachedMap. |
1183 | 1181 |
/// |
1184 | 1182 |
/// This function instantiates a \ref ReachedMap. |
1185 | 1183 |
/// \param digraph is the digraph, to which |
1186 | 1184 |
/// we would like to define the \ref ReachedMap. |
1187 | 1185 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1188 | 1186 |
return new ReachedMap(digraph); |
1189 | 1187 |
} |
1190 | 1188 |
|
1191 | 1189 |
}; |
1192 | 1190 |
|
1193 | 1191 |
/// \ingroup search |
1194 | 1192 |
/// |
1195 | 1193 |
/// \brief %BFS Visit algorithm class. |
1196 | 1194 |
/// |
1197 | 1195 |
/// This class provides an efficient implementation of the %BFS algorithm |
1198 | 1196 |
/// with visitor interface. |
1199 | 1197 |
/// |
1200 | 1198 |
/// The %BfsVisit class provides an alternative interface to the Bfs |
1201 | 1199 |
/// class. It works with callback mechanism, the BfsVisit object calls |
1202 | 1200 |
/// on every bfs event the \c Visitor class member functions. |
1203 | 1201 |
/// |
1204 |
/// \ |
|
1202 |
/// \tparam _Digraph The digraph type the algorithm runs on. The default value is |
|
1205 | 1203 |
/// \ref ListDigraph. The value of _Digraph is not used directly by Bfs, it |
1206 | 1204 |
/// is only passed to \ref BfsDefaultTraits. |
1207 |
/// \ |
|
1205 |
/// \tparam _Visitor The Visitor object for the algorithm. The |
|
1208 | 1206 |
/// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty Visitor which |
1209 | 1207 |
/// does not observe the Bfs events. If you want to observe the bfs |
1210 | 1208 |
/// events you should implement your own Visitor class. |
1211 |
/// \ |
|
1209 |
/// \tparam _Traits Traits class to set various data types used by the |
|
1212 | 1210 |
/// algorithm. The default traits class is |
1213 | 1211 |
/// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>". |
1214 | 1212 |
/// See \ref BfsVisitDefaultTraits for the documentation of |
1215 | 1213 |
/// a Bfs visit traits class. |
1216 |
/// |
|
1217 |
/// \author Jacint Szabo, Alpar Juttner and Balazs Dezso |
|
1218 | 1214 |
#ifdef DOXYGEN |
1219 | 1215 |
template <typename _Digraph, typename _Visitor, typename _Traits> |
1220 | 1216 |
#else |
1221 | 1217 |
template <typename _Digraph = ListDigraph, |
1222 | 1218 |
typename _Visitor = BfsVisitor<_Digraph>, |
1223 | 1219 |
typename _Traits = BfsDefaultTraits<_Digraph> > |
1224 | 1220 |
#endif |
1225 | 1221 |
class BfsVisit { |
1226 | 1222 |
public: |
1227 | 1223 |
|
1228 | 1224 |
/// \brief \ref Exception for uninitialized parameters. |
1229 | 1225 |
/// |
1230 | 1226 |
/// This error represents problems in the initialization |
1231 | 1227 |
/// of the parameters of the algorithms. |
1232 | 1228 |
class UninitializedParameter : public lemon::UninitializedParameter { |
1233 | 1229 |
public: |
1234 | 1230 |
virtual const char* what() const throw() |
1235 | 1231 |
{ |
1236 | 1232 |
return "lemon::BfsVisit::UninitializedParameter"; |
1237 | 1233 |
} |
1238 | 1234 |
}; |
1239 | 1235 |
|
1240 | 1236 |
typedef _Traits Traits; |
1241 | 1237 |
|
1242 | 1238 |
typedef typename Traits::Digraph Digraph; |
1243 | 1239 |
|
1244 | 1240 |
typedef _Visitor Visitor; |
1245 | 1241 |
|
1246 | 1242 |
///The type of the map indicating which nodes are reached. |
1247 | 1243 |
typedef typename Traits::ReachedMap ReachedMap; |
1248 | 1244 |
|
1249 | 1245 |
private: |
1250 | 1246 |
|
1251 | 1247 |
typedef typename Digraph::Node Node; |
1252 | 1248 |
typedef typename Digraph::NodeIt NodeIt; |
1253 | 1249 |
typedef typename Digraph::Arc Arc; |
1254 | 1250 |
typedef typename Digraph::OutArcIt OutArcIt; |
1255 | 1251 |
|
1256 | 1252 |
/// Pointer to the underlying digraph. |
1257 | 1253 |
const Digraph *_digraph; |
1258 | 1254 |
/// Pointer to the visitor object. |
1259 | 1255 |
Visitor *_visitor; |
1260 | 1256 |
///Pointer to the map of reached status of the nodes. |
1261 | 1257 |
ReachedMap *_reached; |
1262 | 1258 |
///Indicates if \ref _reached is locally allocated (\c true) or not. |
1263 | 1259 |
bool local_reached; |
1264 | 1260 |
|
1265 | 1261 |
std::vector<typename Digraph::Node> _list; |
1266 | 1262 |
int _list_front, _list_back; |
1267 | 1263 |
|
1268 | 1264 |
/// \brief Creates the maps if necessary. |
1269 | 1265 |
/// |
1270 | 1266 |
/// Creates the maps if necessary. |
1271 | 1267 |
void create_maps() { |
1272 | 1268 |
if(!_reached) { |
1273 | 1269 |
local_reached = true; |
1274 | 1270 |
_reached = Traits::createReachedMap(*_digraph); |
1275 | 1271 |
} |
1276 | 1272 |
} |
1277 | 1273 |
|
1278 | 1274 |
protected: |
1279 | 1275 |
|
1280 | 1276 |
BfsVisit() {} |
1281 | 1277 |
|
1282 | 1278 |
public: |
1283 | 1279 |
|
1284 | 1280 |
typedef BfsVisit Create; |
1285 | 1281 |
|
1286 | 1282 |
/// \name Named template parameters |
1287 | 1283 |
|
1288 | 1284 |
///@{ |
1289 | 1285 |
template <class T> |
1290 | 1286 |
struct DefReachedMapTraits : public Traits { |
1291 | 1287 |
typedef T ReachedMap; |
1292 | 1288 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1293 | 1289 |
throw UninitializedParameter(); |
1294 | 1290 |
} |
1295 | 1291 |
}; |
1296 | 1292 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1297 | 1293 |
/// ReachedMap type |
1298 | 1294 |
/// |
1299 | 1295 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type |
1300 | 1296 |
template <class T> |
1301 | 1297 |
struct DefReachedMap : public BfsVisit< Digraph, Visitor, |
1302 | 1298 |
DefReachedMapTraits<T> > { |
1303 | 1299 |
typedef BfsVisit< Digraph, Visitor, DefReachedMapTraits<T> > Create; |
1304 | 1300 |
}; |
1305 | 1301 |
///@} |
1306 | 1302 |
|
1307 | 1303 |
public: |
1308 | 1304 |
|
1309 | 1305 |
/// \brief Constructor. |
1310 | 1306 |
/// |
1311 | 1307 |
/// Constructor. |
1312 | 1308 |
/// |
1313 | 1309 |
/// \param digraph the digraph the algorithm will run on. |
1314 | 1310 |
/// \param visitor The visitor of the algorithm. |
1315 | 1311 |
/// |
1316 | 1312 |
BfsVisit(const Digraph& digraph, Visitor& visitor) |
1317 | 1313 |
: _digraph(&digraph), _visitor(&visitor), |
1318 | 1314 |
_reached(0), local_reached(false) {} |
1319 | 1315 |
|
1320 | 1316 |
/// \brief Destructor. |
1321 | 1317 |
/// |
1322 | 1318 |
/// Destructor. |
1323 | 1319 |
~BfsVisit() { |
1324 | 1320 |
if(local_reached) delete _reached; |
1325 | 1321 |
} |
1326 | 1322 |
|
1327 | 1323 |
/// \brief Sets the map indicating if a node is reached. |
1328 | 1324 |
/// |
1329 | 1325 |
/// Sets the map indicating if a node is reached. |
1330 | 1326 |
/// If you don't use this function before calling \ref run(), |
1331 | 1327 |
/// it will allocate one. The destuctor deallocates this |
1332 | 1328 |
/// automatically allocated map, of course. |
1333 | 1329 |
/// \return <tt> (*this) </tt> |
1334 | 1330 |
BfsVisit &reachedMap(ReachedMap &m) { |
1335 | 1331 |
if(local_reached) { |
1336 | 1332 |
delete _reached; |
1337 | 1333 |
local_reached = false; |
1338 | 1334 |
} |
1339 | 1335 |
_reached = &m; |
1340 | 1336 |
return *this; |
1341 | 1337 |
} |
1342 | 1338 |
|
1343 | 1339 |
public: |
1344 | 1340 |
/// \name Execution control |
1345 | 1341 |
/// The simplest way to execute the algorithm is to use |
1346 | 1342 |
/// one of the member functions called \c run(...). |
1347 | 1343 |
/// \n |
1348 | 1344 |
/// If you need more control on the execution, |
1349 | 1345 |
/// first you must call \ref init(), then you can adda source node |
1350 | 1346 |
/// with \ref addSource(). |
1351 | 1347 |
/// Finally \ref start() will perform the actual path |
1352 | 1348 |
/// computation. |
1353 | 1349 |
|
1354 | 1350 |
/// @{ |
1355 | 1351 |
/// \brief Initializes the internal data structures. |
1356 | 1352 |
/// |
1357 | 1353 |
/// Initializes the internal data structures. |
1358 | 1354 |
/// |
1359 | 1355 |
void init() { |
1360 | 1356 |
create_maps(); |
1361 | 1357 |
_list.resize(countNodes(*_digraph)); |
1362 | 1358 |
_list_front = _list_back = -1; |
1363 | 1359 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1364 | 1360 |
_reached->set(u, false); |
1365 | 1361 |
} |
1366 | 1362 |
} |
1367 | 1363 |
|
1368 | 1364 |
/// \brief Adds a new source node. |
1369 | 1365 |
/// |
1370 | 1366 |
/// Adds a new source node to the set of nodes to be processed. |
1371 | 1367 |
void addSource(Node s) { |
1372 | 1368 |
if(!(*_reached)[s]) { |
1373 | 1369 |
_reached->set(s,true); |
1374 | 1370 |
_visitor->start(s); |
1375 | 1371 |
_visitor->reach(s); |
1376 | 1372 |
_list[++_list_back] = s; |
1377 | 1373 |
} |
1378 | 1374 |
} |
1379 | 1375 |
|
1380 | 1376 |
/// \brief Processes the next node. |
1381 | 1377 |
/// |
1382 | 1378 |
/// Processes the next node. |
1383 | 1379 |
/// |
1384 | 1380 |
/// \return The processed node. |
1385 | 1381 |
/// |
1386 | 1382 |
/// \pre The queue must not be empty! |
1387 | 1383 |
Node processNextNode() { |
1388 | 1384 |
Node n = _list[++_list_front]; |
1389 | 1385 |
_visitor->process(n); |
1390 | 1386 |
Arc e; |
1391 | 1387 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1392 | 1388 |
Node m = _digraph->target(e); |
1393 | 1389 |
if (!(*_reached)[m]) { |
1394 | 1390 |
_visitor->discover(e); |
1395 | 1391 |
_visitor->reach(m); |
1396 | 1392 |
_reached->set(m, true); |
1397 | 1393 |
_list[++_list_back] = m; |
1398 | 1394 |
} else { |
1399 | 1395 |
_visitor->examine(e); |
1400 | 1396 |
} |
1401 | 1397 |
} |
1402 | 1398 |
return n; |
1403 | 1399 |
} |
1404 | 1400 |
|
1405 | 1401 |
/// \brief Processes the next node. |
1406 | 1402 |
/// |
1407 | 1403 |
/// Processes the next node. And checks that the given target node |
1408 | 1404 |
/// is reached. If the target node is reachable from the processed |
1409 | 1405 |
/// node then the reached parameter will be set true. The reached |
1410 | 1406 |
/// parameter should be initially false. |
1411 | 1407 |
/// |
1412 | 1408 |
/// \param target The target node. |
1413 | 1409 |
/// \retval reach Indicates that the target node is reached. |
1414 | 1410 |
/// \return The processed node. |
1415 | 1411 |
/// |
1416 | 1412 |
/// \warning The queue must not be empty! |
1417 | 1413 |
Node processNextNode(Node target, bool& reach) { |
1418 | 1414 |
Node n = _list[++_list_front]; |
1419 | 1415 |
_visitor->process(n); |
1420 | 1416 |
Arc e; |
1421 | 1417 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1422 | 1418 |
Node m = _digraph->target(e); |
1423 | 1419 |
if (!(*_reached)[m]) { |
1424 | 1420 |
_visitor->discover(e); |
1425 | 1421 |
_visitor->reach(m); |
1426 | 1422 |
_reached->set(m, true); |
1427 | 1423 |
_list[++_list_back] = m; |
1428 | 1424 |
reach = reach || (target == m); |
1429 | 1425 |
} else { |
1430 | 1426 |
_visitor->examine(e); |
1431 | 1427 |
} |
1432 | 1428 |
} |
1433 | 1429 |
return n; |
1434 | 1430 |
} |
1435 | 1431 |
|
1436 | 1432 |
/// \brief Processes the next node. |
1437 | 1433 |
/// |
1438 | 1434 |
/// Processes the next node. And checks that at least one of |
1439 | 1435 |
/// reached node has true value in the \c nm node map. If one node |
1440 | 1436 |
/// with true value is reachable from the processed node then the |
1441 | 1437 |
/// rnode parameter will be set to the first of such nodes. |
1442 | 1438 |
/// |
1443 | 1439 |
/// \param nm The node map of possible targets. |
1444 | 1440 |
/// \retval rnode The reached target node. |
1445 | 1441 |
/// \return The processed node. |
1446 | 1442 |
/// |
1447 | 1443 |
/// \warning The queue must not be empty! |
1448 | 1444 |
template <typename NM> |
1449 | 1445 |
Node processNextNode(const NM& nm, Node& rnode) { |
1450 | 1446 |
Node n = _list[++_list_front]; |
1451 | 1447 |
_visitor->process(n); |
1452 | 1448 |
Arc e; |
1453 | 1449 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1454 | 1450 |
Node m = _digraph->target(e); |
1455 | 1451 |
if (!(*_reached)[m]) { |
1456 | 1452 |
_visitor->discover(e); |
1457 | 1453 |
_visitor->reach(m); |
1458 | 1454 |
_reached->set(m, true); |
1459 | 1455 |
_list[++_list_back] = m; |
1460 | 1456 |
if (nm[m] && rnode == INVALID) rnode = m; |
1461 | 1457 |
} else { |
1462 | 1458 |
_visitor->examine(e); |
1463 | 1459 |
} |
1464 | 1460 |
} |
1465 | 1461 |
return n; |
1466 | 1462 |
} |
1467 | 1463 |
|
1468 | 1464 |
/// \brief Next node to be processed. |
1469 | 1465 |
/// |
1470 | 1466 |
/// Next node to be processed. |
1471 | 1467 |
/// |
1472 | 1468 |
/// \return The next node to be processed or INVALID if the stack is |
1473 | 1469 |
/// empty. |
1474 | 1470 |
Node nextNode() { |
1475 | 1471 |
return _list_front != _list_back ? _list[_list_front + 1] : INVALID; |
1476 | 1472 |
} |
1477 | 1473 |
|
1478 | 1474 |
/// \brief Returns \c false if there are nodes |
1479 | 1475 |
/// to be processed in the queue |
1480 | 1476 |
/// |
1481 | 1477 |
/// Returns \c false if there are nodes |
1482 | 1478 |
/// to be processed in the queue |
1483 | 1479 |
bool emptyQueue() { return _list_front == _list_back; } |
1484 | 1480 |
|
1485 | 1481 |
/// \brief Returns the number of the nodes to be processed. |
1486 | 1482 |
/// |
1487 | 1483 |
/// Returns the number of the nodes to be processed in the queue. |
1488 | 1484 |
int queueSize() { return _list_back - _list_front; } |
1489 | 1485 |
|
1490 | 1486 |
/// \brief Executes the algorithm. |
1491 | 1487 |
/// |
1492 | 1488 |
/// Executes the algorithm. |
1493 | 1489 |
/// |
1494 | 1490 |
/// \pre init() must be called and at least one node should be added |
1495 | 1491 |
/// with addSource() before using this function. |
1496 | 1492 |
void start() { |
1497 | 1493 |
while ( !emptyQueue() ) processNextNode(); |
1498 | 1494 |
} |
1499 | 1495 |
|
1500 | 1496 |
/// \brief Executes the algorithm until \c dest is reached. |
1501 | 1497 |
/// |
1502 | 1498 |
/// Executes the algorithm until \c dest is reached. |
1503 | 1499 |
/// |
1504 | 1500 |
/// \pre init() must be called and at least one node should be added |
1505 | 1501 |
/// with addSource() before using this function. |
1506 | 1502 |
void start(Node dest) { |
1507 | 1503 |
bool reach = false; |
1508 | 1504 |
while ( !emptyQueue() && !reach ) processNextNode(dest, reach); |
1509 | 1505 |
} |
1510 | 1506 |
|
1511 | 1507 |
/// \brief Executes the algorithm until a condition is met. |
1512 | 1508 |
/// |
1513 | 1509 |
/// Executes the algorithm until a condition is met. |
1514 | 1510 |
/// |
1515 | 1511 |
/// \pre init() must be called and at least one node should be added |
1516 | 1512 |
/// with addSource() before using this function. |
1517 | 1513 |
/// |
1518 | 1514 |
///\param nm must be a bool (or convertible) node map. The |
1519 | 1515 |
///algorithm will stop when it reaches a node \c v with |
1520 | 1516 |
/// <tt>nm[v]</tt> true. |
1521 | 1517 |
/// |
1522 | 1518 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
1523 | 1519 |
///\c INVALID if no such node was found. |
1524 | 1520 |
template <typename NM> |
1525 | 1521 |
Node start(const NM &nm) { |
1526 | 1522 |
Node rnode = INVALID; |
1527 | 1523 |
while ( !emptyQueue() && rnode == INVALID ) { |
1528 | 1524 |
processNextNode(nm, rnode); |
1529 | 1525 |
} |
1530 | 1526 |
return rnode; |
1531 | 1527 |
} |
1532 | 1528 |
|
1533 | 1529 |
/// \brief Runs %BFSVisit algorithm from node \c s. |
1534 | 1530 |
/// |
1535 | 1531 |
/// This method runs the %BFS algorithm from a root node \c s. |
1536 | 1532 |
/// \note b.run(s) is just a shortcut of the following code. |
1537 | 1533 |
///\code |
1538 | 1534 |
/// b.init(); |
1539 | 1535 |
/// b.addSource(s); |
1540 | 1536 |
/// b.start(); |
1541 | 1537 |
///\endcode |
1542 | 1538 |
void run(Node s) { |
1543 | 1539 |
init(); |
1544 | 1540 |
addSource(s); |
1545 | 1541 |
start(); |
1546 | 1542 |
} |
1547 | 1543 |
|
1548 | 1544 |
/// \brief Runs %BFSVisit algorithm to visit all nodes in the digraph. |
1549 | 1545 |
/// |
1550 | 1546 |
/// This method runs the %BFS algorithm in order to |
1551 | 1547 |
/// compute the %BFS path to each node. The algorithm computes |
1552 | 1548 |
/// - The %BFS tree. |
1553 | 1549 |
/// - The distance of each node from the root in the %BFS tree. |
1554 | 1550 |
/// |
1555 | 1551 |
///\note b.run() is just a shortcut of the following code. |
1556 | 1552 |
///\code |
1557 | 1553 |
/// b.init(); |
1558 | 1554 |
/// for (NodeIt it(digraph); it != INVALID; ++it) { |
1559 | 1555 |
/// if (!b.reached(it)) { |
1560 | 1556 |
/// b.addSource(it); |
1561 | 1557 |
/// b.start(); |
1562 | 1558 |
/// } |
1563 | 1559 |
/// } |
1564 | 1560 |
///\endcode |
1565 | 1561 |
void run() { |
1566 | 1562 |
init(); |
1567 | 1563 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1568 | 1564 |
if (!reached(it)) { |
1569 | 1565 |
addSource(it); |
1570 | 1566 |
start(); |
1571 | 1567 |
} |
1572 | 1568 |
} |
1573 | 1569 |
} |
1574 | 1570 |
///@} |
1575 | 1571 |
|
1576 | 1572 |
/// \name Query Functions |
1577 | 1573 |
/// The result of the %BFS algorithm can be obtained using these |
1578 | 1574 |
/// functions.\n |
1579 | 1575 |
/// Before the use of these functions, |
1580 | 1576 |
/// either run() or start() must be called. |
1581 | 1577 |
///@{ |
1582 | 1578 |
|
1583 | 1579 |
/// \brief Checks if a node is reachable from the root. |
1584 | 1580 |
/// |
1585 | 1581 |
/// Returns \c true if \c v is reachable from the root(s). |
1586 | 1582 |
/// \warning The source nodes are inditated as unreachable. |
1587 | 1583 |
/// \pre Either \ref run() or \ref start() |
1588 | 1584 |
/// must be called before using this function. |
1589 | 1585 |
/// |
1590 | 1586 |
bool reached(Node v) { return (*_reached)[v]; } |
1591 | 1587 |
///@} |
1592 | 1588 |
}; |
1593 | 1589 |
|
1594 | 1590 |
} //END OF NAMESPACE LEMON |
1595 | 1591 |
|
1596 | 1592 |
#endif |
1597 | 1593 |
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BIN_HEAP_H |
20 | 20 |
#define LEMON_BIN_HEAP_H |
21 | 21 |
|
22 | 22 |
///\ingroup auxdat |
23 | 23 |
///\file |
24 | 24 |
///\brief Binary Heap implementation. |
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
#include <utility> |
28 | 28 |
#include <functional> |
29 | 29 |
|
30 | 30 |
namespace lemon { |
31 | 31 |
|
32 | 32 |
///\ingroup auxdat |
33 | 33 |
/// |
34 | 34 |
///\brief A Binary Heap implementation. |
35 | 35 |
/// |
36 | 36 |
///This class implements the \e binary \e heap data structure. A \e heap |
37 | 37 |
///is a data structure for storing items with specified values called \e |
38 | 38 |
///priorities in such a way that finding the item with minimum priority is |
39 | 39 |
///efficient. \c Compare specifies the ordering of the priorities. In a heap |
40 | 40 |
///one can change the priority of an item, add or erase an item, etc. |
41 | 41 |
/// |
42 |
///\param _Prio Type of the priority of the items. |
|
43 |
///\param _ItemIntMap A read and writable Item int map, used internally |
|
42 |
///\tparam _Prio Type of the priority of the items. |
|
43 |
///\tparam _ItemIntMap A read and writable Item int map, used internally |
|
44 | 44 |
///to handle the cross references. |
45 |
///\ |
|
45 |
///\tparam _Compare A class for the ordering of the priorities. The |
|
46 | 46 |
///default is \c std::less<_Prio>. |
47 | 47 |
/// |
48 | 48 |
///\sa FibHeap |
49 | 49 |
///\sa Dijkstra |
50 | 50 |
template <typename _Prio, typename _ItemIntMap, |
51 | 51 |
typename _Compare = std::less<_Prio> > |
52 | 52 |
class BinHeap { |
53 | 53 |
|
54 | 54 |
public: |
55 | 55 |
///\e |
56 | 56 |
typedef _ItemIntMap ItemIntMap; |
57 | 57 |
///\e |
58 | 58 |
typedef _Prio Prio; |
59 | 59 |
///\e |
60 | 60 |
typedef typename ItemIntMap::Key Item; |
61 | 61 |
///\e |
62 | 62 |
typedef std::pair<Item,Prio> Pair; |
63 | 63 |
///\e |
64 | 64 |
typedef _Compare Compare; |
65 | 65 |
|
66 | 66 |
/// \brief Type to represent the items states. |
67 | 67 |
/// |
68 | 68 |
/// Each Item element have a state associated to it. It may be "in heap", |
69 | 69 |
/// "pre heap" or "post heap". The latter two are indifferent from the |
70 | 70 |
/// heap's point of view, but may be useful to the user. |
71 | 71 |
/// |
72 | 72 |
/// The ItemIntMap \e should be initialized in such way that it maps |
73 | 73 |
/// PRE_HEAP (-1) to any element to be put in the heap... |
74 | 74 |
enum State { |
75 | 75 |
IN_HEAP = 0, |
76 | 76 |
PRE_HEAP = -1, |
77 | 77 |
POST_HEAP = -2 |
78 | 78 |
}; |
79 | 79 |
|
80 | 80 |
private: |
81 | 81 |
std::vector<Pair> data; |
82 | 82 |
Compare comp; |
83 | 83 |
ItemIntMap &iim; |
84 | 84 |
|
85 | 85 |
public: |
86 | 86 |
/// \brief The constructor. |
87 | 87 |
/// |
88 | 88 |
/// The constructor. |
89 | 89 |
/// \param _iim should be given to the constructor, since it is used |
90 | 90 |
/// internally to handle the cross references. The value of the map |
91 | 91 |
/// should be PRE_HEAP (-1) for each element. |
92 | 92 |
explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {} |
93 | 93 |
|
94 | 94 |
/// \brief The constructor. |
95 | 95 |
/// |
96 | 96 |
/// The constructor. |
97 | 97 |
/// \param _iim should be given to the constructor, since it is used |
98 | 98 |
/// internally to handle the cross references. The value of the map |
99 | 99 |
/// should be PRE_HEAP (-1) for each element. |
100 | 100 |
/// |
101 | 101 |
/// \param _comp The comparator function object. |
102 | 102 |
BinHeap(ItemIntMap &_iim, const Compare &_comp) |
103 | 103 |
: iim(_iim), comp(_comp) {} |
104 | 104 |
|
105 | 105 |
|
106 | 106 |
/// The number of items stored in the heap. |
107 | 107 |
/// |
108 | 108 |
/// \brief Returns the number of items stored in the heap. |
109 | 109 |
int size() const { return data.size(); } |
110 | 110 |
|
111 | 111 |
/// \brief Checks if the heap stores no items. |
112 | 112 |
/// |
113 | 113 |
/// Returns \c true if and only if the heap stores no items. |
114 | 114 |
bool empty() const { return data.empty(); } |
115 | 115 |
|
116 | 116 |
/// \brief Make empty this heap. |
117 | 117 |
/// |
118 | 118 |
/// Make empty this heap. It does not change the cross reference map. |
119 | 119 |
/// If you want to reuse what is not surely empty you should first clear |
120 | 120 |
/// the heap and after that you should set the cross reference map for |
121 | 121 |
/// each item to \c PRE_HEAP. |
122 | 122 |
void clear() { |
123 | 123 |
data.clear(); |
124 | 124 |
} |
125 | 125 |
|
126 | 126 |
private: |
127 | 127 |
static int parent(int i) { return (i-1)/2; } |
128 | 128 |
|
129 | 129 |
static int second_child(int i) { return 2*i+2; } |
130 | 130 |
bool less(const Pair &p1, const Pair &p2) const { |
131 | 131 |
return comp(p1.second, p2.second); |
132 | 132 |
} |
133 | 133 |
|
134 | 134 |
int bubble_up(int hole, Pair p) { |
135 | 135 |
int par = parent(hole); |
136 | 136 |
while( hole>0 && less(p,data[par]) ) { |
137 | 137 |
move(data[par],hole); |
138 | 138 |
hole = par; |
139 | 139 |
par = parent(hole); |
140 | 140 |
} |
141 | 141 |
move(p, hole); |
142 | 142 |
return hole; |
143 | 143 |
} |
144 | 144 |
|
145 | 145 |
int bubble_down(int hole, Pair p, int length) { |
146 | 146 |
int child = second_child(hole); |
147 | 147 |
while(child < length) { |
148 | 148 |
if( less(data[child-1], data[child]) ) { |
149 | 149 |
--child; |
150 | 150 |
} |
151 | 151 |
if( !less(data[child], p) ) |
152 | 152 |
goto ok; |
153 | 153 |
move(data[child], hole); |
154 | 154 |
hole = child; |
155 | 155 |
child = second_child(hole); |
156 | 156 |
} |
157 | 157 |
child--; |
158 | 158 |
if( child<length && less(data[child], p) ) { |
159 | 159 |
move(data[child], hole); |
160 | 160 |
hole=child; |
161 | 161 |
} |
162 | 162 |
ok: |
163 | 163 |
move(p, hole); |
164 | 164 |
return hole; |
165 | 165 |
} |
166 | 166 |
|
167 | 167 |
void move(const Pair &p, int i) { |
168 | 168 |
data[i] = p; |
169 | 169 |
iim.set(p.first, i); |
170 | 170 |
} |
171 | 171 |
|
172 | 172 |
public: |
173 | 173 |
/// \brief Insert a pair of item and priority into the heap. |
174 | 174 |
/// |
175 | 175 |
/// Adds \c p.first to the heap with priority \c p.second. |
176 | 176 |
/// \param p The pair to insert. |
177 | 177 |
void push(const Pair &p) { |
178 | 178 |
int n = data.size(); |
179 | 179 |
data.resize(n+1); |
180 | 180 |
bubble_up(n, p); |
181 | 181 |
} |
182 | 182 |
|
183 | 183 |
/// \brief Insert an item into the heap with the given heap. |
184 | 184 |
/// |
185 | 185 |
/// Adds \c i to the heap with priority \c p. |
186 | 186 |
/// \param i The item to insert. |
187 | 187 |
/// \param p The priority of the item. |
188 | 188 |
void push(const Item &i, const Prio &p) { push(Pair(i,p)); } |
189 | 189 |
|
190 | 190 |
/// \brief Returns the item with minimum priority relative to \c Compare. |
191 | 191 |
/// |
192 | 192 |
/// This method returns the item with minimum priority relative to \c |
193 | 193 |
/// Compare. |
194 | 194 |
/// \pre The heap must be nonempty. |
195 | 195 |
Item top() const { |
196 | 196 |
return data[0].first; |
197 | 197 |
} |
198 | 198 |
|
199 | 199 |
/// \brief Returns the minimum priority relative to \c Compare. |
200 | 200 |
/// |
201 | 201 |
/// It returns the minimum priority relative to \c Compare. |
202 | 202 |
/// \pre The heap must be nonempty. |
203 | 203 |
Prio prio() const { |
204 | 204 |
return data[0].second; |
205 | 205 |
} |
206 | 206 |
|
207 | 207 |
/// \brief Deletes the item with minimum priority relative to \c Compare. |
208 | 208 |
/// |
209 | 209 |
/// This method deletes the item with minimum priority relative to \c |
210 | 210 |
/// Compare from the heap. |
211 | 211 |
/// \pre The heap must be non-empty. |
212 | 212 |
void pop() { |
213 | 213 |
int n = data.size()-1; |
214 | 214 |
iim.set(data[0].first, POST_HEAP); |
215 | 215 |
if (n > 0) { |
216 | 216 |
bubble_down(0, data[n], n); |
217 | 217 |
} |
218 | 218 |
data.pop_back(); |
219 | 219 |
} |
220 | 220 |
|
221 | 221 |
/// \brief Deletes \c i from the heap. |
222 | 222 |
/// |
223 | 223 |
/// This method deletes item \c i from the heap. |
224 | 224 |
/// \param i The item to erase. |
225 | 225 |
/// \pre The item should be in the heap. |
226 | 226 |
void erase(const Item &i) { |
227 | 227 |
int h = iim[i]; |
228 | 228 |
int n = data.size()-1; |
229 | 229 |
iim.set(data[h].first, POST_HEAP); |
230 | 230 |
if( h < n ) { |
231 | 231 |
if ( bubble_up(h, data[n]) == h) { |
232 | 232 |
bubble_down(h, data[n], n); |
233 | 233 |
} |
234 | 234 |
} |
235 | 235 |
data.pop_back(); |
236 | 236 |
} |
237 | 237 |
|
238 | 238 |
|
239 | 239 |
/// \brief Returns the priority of \c i. |
240 | 240 |
/// |
241 | 241 |
/// This function returns the priority of item \c i. |
242 | 242 |
/// \pre \c i must be in the heap. |
243 | 243 |
/// \param i The item. |
244 | 244 |
Prio operator[](const Item &i) const { |
245 | 245 |
int idx = iim[i]; |
246 | 246 |
return data[idx].second; |
247 | 247 |
} |
248 | 248 |
|
249 | 249 |
/// \brief \c i gets to the heap with priority \c p independently |
250 | 250 |
/// if \c i was already there. |
251 | 251 |
/// |
252 | 252 |
/// This method calls \ref push(\c i, \c p) if \c i is not stored |
253 | 253 |
/// in the heap and sets the priority of \c i to \c p otherwise. |
254 | 254 |
/// \param i The item. |
255 | 255 |
/// \param p The priority. |
256 | 256 |
void set(const Item &i, const Prio &p) { |
257 | 257 |
int idx = iim[i]; |
258 | 258 |
if( idx < 0 ) { |
259 | 259 |
push(i,p); |
260 | 260 |
} |
261 | 261 |
else if( comp(p, data[idx].second) ) { |
262 | 262 |
bubble_up(idx, Pair(i,p)); |
263 | 263 |
} |
264 | 264 |
else { |
265 | 265 |
bubble_down(idx, Pair(i,p), data.size()); |
266 | 266 |
} |
267 | 267 |
} |
268 | 268 |
|
269 | 269 |
/// \brief Decreases the priority of \c i to \c p. |
270 | 270 |
/// |
271 | 271 |
/// This method decreases the priority of item \c i to \c p. |
272 | 272 |
/// \pre \c i must be stored in the heap with priority at least \c |
273 | 273 |
/// p relative to \c Compare. |
274 | 274 |
/// \param i The item. |
275 | 275 |
/// \param p The priority. |
276 | 276 |
void decrease(const Item &i, const Prio &p) { |
277 | 277 |
int idx = iim[i]; |
278 | 278 |
bubble_up(idx, Pair(i,p)); |
279 | 279 |
} |
280 | 280 |
|
281 | 281 |
/// \brief Increases the priority of \c i to \c p. |
282 | 282 |
/// |
283 | 283 |
/// This method sets the priority of item \c i to \c p. |
284 | 284 |
/// \pre \c i must be stored in the heap with priority at most \c |
285 | 285 |
/// p relative to \c Compare. |
286 | 286 |
/// \param i The item. |
287 | 287 |
/// \param p The priority. |
288 | 288 |
void increase(const Item &i, const Prio &p) { |
289 | 289 |
int idx = iim[i]; |
290 | 290 |
bubble_down(idx, Pair(i,p), data.size()); |
291 | 291 |
} |
292 | 292 |
|
293 | 293 |
/// \brief Returns if \c item is in, has already been in, or has |
294 | 294 |
/// never been in the heap. |
295 | 295 |
/// |
296 | 296 |
/// This method returns PRE_HEAP if \c item has never been in the |
297 | 297 |
/// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP |
298 | 298 |
/// otherwise. In the latter case it is possible that \c item will |
299 | 299 |
/// get back to the heap again. |
300 | 300 |
/// \param i The item. |
301 | 301 |
State state(const Item &i) const { |
302 | 302 |
int s = iim[i]; |
303 | 303 |
if( s>=0 ) |
304 | 304 |
s=0; |
305 | 305 |
return State(s); |
306 | 306 |
} |
307 | 307 |
|
308 | 308 |
/// \brief Sets the state of the \c item in the heap. |
309 | 309 |
/// |
310 | 310 |
/// Sets the state of the \c item in the heap. It can be used to |
311 | 311 |
/// manually clear the heap when it is important to achive the |
312 | 312 |
/// better time complexity. |
313 | 313 |
/// \param i The item. |
314 | 314 |
/// \param st The state. It should not be \c IN_HEAP. |
315 | 315 |
void state(const Item& i, State st) { |
316 | 316 |
switch (st) { |
317 | 317 |
case POST_HEAP: |
318 | 318 |
case PRE_HEAP: |
319 | 319 |
if (state(i) == IN_HEAP) { |
320 | 320 |
erase(i); |
321 | 321 |
} |
322 | 322 |
iim[i] = st; |
323 | 323 |
break; |
324 | 324 |
case IN_HEAP: |
325 | 325 |
break; |
326 | 326 |
} |
327 | 327 |
} |
328 | 328 |
|
329 | 329 |
/// \brief Replaces an item in the heap. |
330 | 330 |
/// |
331 | 331 |
/// The \c i item is replaced with \c j item. The \c i item should |
332 | 332 |
/// be in the heap, while the \c j should be out of the heap. The |
333 | 333 |
/// \c i item will out of the heap and \c j will be in the heap |
334 | 334 |
/// with the same prioriority as prevoiusly the \c i item. |
335 | 335 |
void replace(const Item& i, const Item& j) { |
336 | 336 |
int idx = iim[i]; |
337 | 337 |
iim.set(i, iim[j]); |
338 | 338 |
iim.set(j, idx); |
339 | 339 |
data[idx].first = j; |
340 | 340 |
} |
341 | 341 |
|
342 | 342 |
}; // class BinHeap |
343 | 343 |
|
344 | 344 |
} // namespace lemon |
345 | 345 |
|
346 | 346 |
#endif // LEMON_BIN_HEAP_H |
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_ALTERATION_NOTIFIER_H |
20 | 20 |
#define LEMON_BITS_ALTERATION_NOTIFIER_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <list> |
24 | 24 |
|
25 | 25 |
#include <lemon/bits/utility.h> |
26 | 26 |
|
27 | 27 |
///\ingroup graphbits |
28 | 28 |
///\file |
29 | 29 |
///\brief Observer notifier for graph alteration observers. |
30 | 30 |
|
31 | 31 |
namespace lemon { |
32 | 32 |
|
33 | 33 |
/// \ingroup graphbits |
34 | 34 |
/// |
35 | 35 |
/// \brief Notifier class to notify observes about alterations in |
36 | 36 |
/// a container. |
37 | 37 |
/// |
38 | 38 |
/// The simple graph's can be refered as two containers, one node container |
39 | 39 |
/// and one edge container. But they are not standard containers they |
40 | 40 |
/// does not store values directly they are just key continars for more |
41 | 41 |
/// value containers which are the node and edge maps. |
42 | 42 |
/// |
43 | 43 |
/// The graph's node and edge sets can be changed as we add or erase |
44 | 44 |
/// nodes and edges in the graph. Lemon would like to handle easily |
45 | 45 |
/// that the node and edge maps should contain values for all nodes or |
46 | 46 |
/// edges. If we want to check on every indicing if the map contains |
47 | 47 |
/// the current indicing key that cause a drawback in the performance |
48 | 48 |
/// in the library. We use another solution we notify all maps about |
49 | 49 |
/// an alteration in the graph, which cause only drawback on the |
50 | 50 |
/// alteration of the graph. |
51 | 51 |
/// |
52 | 52 |
/// This class provides an interface to the container. The \e first() and \e |
53 | 53 |
/// next() member functions make possible to iterate on the keys of the |
54 | 54 |
/// container. The \e id() function returns an integer id for each key. |
55 | 55 |
/// The \e maxId() function gives back an upper bound of the ids. |
56 | 56 |
/// |
57 | 57 |
/// For the proper functonality of this class, we should notify it |
58 | 58 |
/// about each alteration in the container. The alterations have four type |
59 | 59 |
/// as \e add(), \e erase(), \e build() and \e clear(). The \e add() and |
60 | 60 |
/// \e erase() signals that only one or few items added or erased to or |
61 | 61 |
/// from the graph. If all items are erased from the graph or from an empty |
62 | 62 |
/// graph a new graph is builded then it can be signaled with the |
63 | 63 |
/// clear() and build() members. Important rule that if we erase items |
64 | 64 |
/// from graph we should first signal the alteration and after that erase |
65 | 65 |
/// them from the container, on the other way on item addition we should |
66 | 66 |
/// first extend the container and just after that signal the alteration. |
67 | 67 |
/// |
68 | 68 |
/// The alteration can be observed with a class inherited from the |
69 | 69 |
/// \e ObserverBase nested class. The signals can be handled with |
70 | 70 |
/// overriding the virtual functions defined in the base class. The |
71 | 71 |
/// observer base can be attached to the notifier with the |
72 | 72 |
/// \e attach() member and can be detached with detach() function. The |
73 | 73 |
/// alteration handlers should not call any function which signals |
74 | 74 |
/// an other alteration in the same notifier and should not |
75 | 75 |
/// detach any observer from the notifier. |
76 | 76 |
/// |
77 | 77 |
/// Alteration observers try to be exception safe. If an \e add() or |
78 | 78 |
/// a \e clear() function throws an exception then the remaining |
79 | 79 |
/// observeres will not be notified and the fulfilled additions will |
80 | 80 |
/// be rolled back by calling the \e erase() or \e clear() |
81 | 81 |
/// functions. Thence the \e erase() and \e clear() should not throw |
82 | 82 |
/// exception. Actullay, it can be throw only |
83 | 83 |
/// \ref AlterationObserver::ImmediateDetach ImmediateDetach |
84 | 84 |
/// exception which detach the observer from the notifier. |
85 | 85 |
/// |
86 | 86 |
/// There are some place when the alteration observing is not completly |
87 | 87 |
/// reliable. If we want to carry out the node degree in the graph |
88 | 88 |
/// as in the \ref InDegMap and we use the reverseEdge that cause |
89 | 89 |
/// unreliable functionality. Because the alteration observing signals |
90 | 90 |
/// only erasing and adding but not the reversing it will stores bad |
91 | 91 |
/// degrees. The sub graph adaptors cannot signal the alterations because |
92 | 92 |
/// just a setting in the filter map can modify the graph and this cannot |
93 | 93 |
/// be watched in any way. |
94 | 94 |
/// |
95 | 95 |
/// \param _Container The container which is observed. |
96 | 96 |
/// \param _Item The item type which is obserbved. |
97 |
/// |
|
98 |
/// \author Balazs Dezso |
|
99 | 97 |
|
100 | 98 |
template <typename _Container, typename _Item> |
101 | 99 |
class AlterationNotifier { |
102 | 100 |
public: |
103 | 101 |
|
104 | 102 |
typedef True Notifier; |
105 | 103 |
|
106 | 104 |
typedef _Container Container; |
107 | 105 |
typedef _Item Item; |
108 | 106 |
|
109 | 107 |
/// \brief Exception which can be called from \e clear() and |
110 | 108 |
/// \e erase(). |
111 | 109 |
/// |
112 | 110 |
/// From the \e clear() and \e erase() function only this |
113 | 111 |
/// exception is allowed to throw. The exception immediatly |
114 | 112 |
/// detaches the current observer from the notifier. Because the |
115 | 113 |
/// \e clear() and \e erase() should not throw other exceptions |
116 | 114 |
/// it can be used to invalidate the observer. |
117 | 115 |
struct ImmediateDetach {}; |
118 | 116 |
|
119 | 117 |
/// \brief ObserverBase is the base class for the observers. |
120 | 118 |
/// |
121 | 119 |
/// ObserverBase is the abstract base class for the observers. |
122 | 120 |
/// It will be notified about an item was inserted into or |
123 | 121 |
/// erased from the graph. |
124 | 122 |
/// |
125 | 123 |
/// The observer interface contains some pure virtual functions |
126 | 124 |
/// to override. The add() and erase() functions are |
127 | 125 |
/// to notify the oberver when one item is added or |
128 | 126 |
/// erased. |
129 | 127 |
/// |
130 | 128 |
/// The build() and clear() members are to notify the observer |
131 | 129 |
/// about the container is built from an empty container or |
132 | 130 |
/// is cleared to an empty container. |
133 |
/// |
|
134 |
/// \author Balazs Dezso |
|
135 | 131 |
|
136 | 132 |
class ObserverBase { |
137 | 133 |
protected: |
138 | 134 |
typedef AlterationNotifier Notifier; |
139 | 135 |
|
140 | 136 |
friend class AlterationNotifier; |
141 | 137 |
|
142 | 138 |
/// \brief Default constructor. |
143 | 139 |
/// |
144 | 140 |
/// Default constructor for ObserverBase. |
145 | 141 |
/// |
146 | 142 |
ObserverBase() : _notifier(0) {} |
147 | 143 |
|
148 | 144 |
/// \brief Constructor which attach the observer into notifier. |
149 | 145 |
/// |
150 | 146 |
/// Constructor which attach the observer into notifier. |
151 | 147 |
ObserverBase(AlterationNotifier& nf) { |
152 | 148 |
attach(nf); |
153 | 149 |
} |
154 | 150 |
|
155 | 151 |
/// \brief Constructor which attach the obserever to the same notifier. |
156 | 152 |
/// |
157 | 153 |
/// Constructor which attach the obserever to the same notifier as |
158 | 154 |
/// the other observer is attached to. |
159 | 155 |
ObserverBase(const ObserverBase& copy) { |
160 | 156 |
if (copy.attached()) { |
161 | 157 |
attach(*copy.notifier()); |
162 | 158 |
} |
163 | 159 |
} |
164 | 160 |
|
165 | 161 |
/// \brief Destructor |
166 | 162 |
virtual ~ObserverBase() { |
167 | 163 |
if (attached()) { |
168 | 164 |
detach(); |
169 | 165 |
} |
170 | 166 |
} |
171 | 167 |
|
172 | 168 |
/// \brief Attaches the observer into an AlterationNotifier. |
173 | 169 |
/// |
174 | 170 |
/// This member attaches the observer into an AlterationNotifier. |
175 | 171 |
/// |
176 | 172 |
void attach(AlterationNotifier& nf) { |
177 | 173 |
nf.attach(*this); |
178 | 174 |
} |
179 | 175 |
|
180 | 176 |
/// \brief Detaches the observer into an AlterationNotifier. |
181 | 177 |
/// |
182 | 178 |
/// This member detaches the observer from an AlterationNotifier. |
183 | 179 |
/// |
184 | 180 |
void detach() { |
185 | 181 |
_notifier->detach(*this); |
186 | 182 |
} |
187 | 183 |
|
188 | 184 |
/// \brief Gives back a pointer to the notifier which the map |
189 | 185 |
/// attached into. |
190 | 186 |
/// |
191 | 187 |
/// This function gives back a pointer to the notifier which the map |
192 | 188 |
/// attached into. |
193 | 189 |
/// |
194 | 190 |
Notifier* notifier() const { return const_cast<Notifier*>(_notifier); } |
195 | 191 |
|
196 | 192 |
/// Gives back true when the observer is attached into a notifier. |
197 | 193 |
bool attached() const { return _notifier != 0; } |
198 | 194 |
|
199 | 195 |
private: |
200 | 196 |
|
201 | 197 |
ObserverBase& operator=(const ObserverBase& copy); |
202 | 198 |
|
203 | 199 |
protected: |
204 | 200 |
|
205 | 201 |
Notifier* _notifier; |
206 | 202 |
typename std::list<ObserverBase*>::iterator _index; |
207 | 203 |
|
208 | 204 |
/// \brief The member function to notificate the observer about an |
209 | 205 |
/// item is added to the container. |
210 | 206 |
/// |
211 | 207 |
/// The add() member function notificates the observer about an item |
212 | 208 |
/// is added to the container. It have to be overrided in the |
213 | 209 |
/// subclasses. |
214 | 210 |
virtual void add(const Item&) = 0; |
215 | 211 |
|
216 | 212 |
/// \brief The member function to notificate the observer about |
217 | 213 |
/// more item is added to the container. |
218 | 214 |
/// |
219 | 215 |
/// The add() member function notificates the observer about more item |
220 | 216 |
/// is added to the container. It have to be overrided in the |
221 | 217 |
/// subclasses. |
222 | 218 |
virtual void add(const std::vector<Item>& items) = 0; |
223 | 219 |
|
224 | 220 |
/// \brief The member function to notificate the observer about an |
225 | 221 |
/// item is erased from the container. |
226 | 222 |
/// |
227 | 223 |
/// The erase() member function notificates the observer about an |
228 | 224 |
/// item is erased from the container. It have to be overrided in |
229 | 225 |
/// the subclasses. |
230 | 226 |
virtual void erase(const Item&) = 0; |
231 | 227 |
|
232 | 228 |
/// \brief The member function to notificate the observer about |
233 | 229 |
/// more item is erased from the container. |
234 | 230 |
/// |
235 | 231 |
/// The erase() member function notificates the observer about more item |
236 | 232 |
/// is erased from the container. It have to be overrided in the |
237 | 233 |
/// subclasses. |
238 | 234 |
virtual void erase(const std::vector<Item>& items) = 0; |
239 | 235 |
|
240 | 236 |
/// \brief The member function to notificate the observer about the |
241 | 237 |
/// container is built. |
242 | 238 |
/// |
243 | 239 |
/// The build() member function notificates the observer about the |
244 | 240 |
/// container is built from an empty container. It have to be |
245 | 241 |
/// overrided in the subclasses. |
246 | 242 |
|
247 | 243 |
virtual void build() = 0; |
248 | 244 |
|
249 | 245 |
/// \brief The member function to notificate the observer about all |
250 | 246 |
/// items are erased from the container. |
251 | 247 |
/// |
252 | 248 |
/// The clear() member function notificates the observer about all |
253 | 249 |
/// items are erased from the container. It have to be overrided in |
254 | 250 |
/// the subclasses. |
255 | 251 |
virtual void clear() = 0; |
256 | 252 |
|
257 | 253 |
}; |
258 | 254 |
|
259 | 255 |
protected: |
260 | 256 |
|
261 | 257 |
const Container* container; |
262 | 258 |
|
263 | 259 |
typedef std::list<ObserverBase*> Observers; |
264 | 260 |
Observers _observers; |
265 | 261 |
|
266 | 262 |
|
267 | 263 |
public: |
268 | 264 |
|
269 | 265 |
/// \brief Default constructor. |
270 | 266 |
/// |
271 | 267 |
/// The default constructor of the AlterationNotifier. |
272 | 268 |
/// It creates an empty notifier. |
273 | 269 |
AlterationNotifier() |
274 | 270 |
: container(0) {} |
275 | 271 |
|
276 | 272 |
/// \brief Constructor. |
277 | 273 |
/// |
278 | 274 |
/// Constructor with the observed container parameter. |
279 | 275 |
AlterationNotifier(const Container& _container) |
280 | 276 |
: container(&_container) {} |
281 | 277 |
|
282 | 278 |
/// \brief Copy Constructor of the AlterationNotifier. |
283 | 279 |
/// |
284 | 280 |
/// Copy constructor of the AlterationNotifier. |
285 | 281 |
/// It creates only an empty notifier because the copiable |
286 | 282 |
/// notifier's observers have to be registered still into that notifier. |
287 | 283 |
AlterationNotifier(const AlterationNotifier& _notifier) |
288 | 284 |
: container(_notifier.container) {} |
289 | 285 |
|
290 | 286 |
/// \brief Destructor. |
291 | 287 |
/// |
292 | 288 |
/// Destructor of the AlterationNotifier. |
293 | 289 |
/// |
294 | 290 |
~AlterationNotifier() { |
295 | 291 |
typename Observers::iterator it; |
296 | 292 |
for (it = _observers.begin(); it != _observers.end(); ++it) { |
297 | 293 |
(*it)->_notifier = 0; |
298 | 294 |
} |
299 | 295 |
} |
300 | 296 |
|
301 | 297 |
/// \brief Sets the container. |
302 | 298 |
/// |
303 | 299 |
/// Sets the container. |
304 | 300 |
void setContainer(const Container& _container) { |
305 | 301 |
container = &_container; |
306 | 302 |
} |
307 | 303 |
|
308 | 304 |
protected: |
309 | 305 |
|
310 | 306 |
AlterationNotifier& operator=(const AlterationNotifier&); |
311 | 307 |
|
312 | 308 |
public: |
313 | 309 |
|
314 | 310 |
|
315 | 311 |
|
316 | 312 |
/// \brief First item in the container. |
317 | 313 |
/// |
318 | 314 |
/// Returns the first item in the container. It is |
319 | 315 |
/// for start the iteration on the container. |
320 | 316 |
void first(Item& item) const { |
321 | 317 |
container->first(item); |
322 | 318 |
} |
323 | 319 |
|
324 | 320 |
/// \brief Next item in the container. |
325 | 321 |
/// |
326 | 322 |
/// Returns the next item in the container. It is |
327 | 323 |
/// for iterate on the container. |
328 | 324 |
void next(Item& item) const { |
329 | 325 |
container->next(item); |
330 | 326 |
} |
331 | 327 |
|
332 | 328 |
/// \brief Returns the id of the item. |
333 | 329 |
/// |
334 | 330 |
/// Returns the id of the item provided by the container. |
335 | 331 |
int id(const Item& item) const { |
336 | 332 |
return container->id(item); |
337 | 333 |
} |
338 | 334 |
|
339 | 335 |
/// \brief Returns the maximum id of the container. |
340 | 336 |
/// |
341 | 337 |
/// Returns the maximum id of the container. |
342 | 338 |
int maxId() const { |
343 | 339 |
return container->maxId(Item()); |
344 | 340 |
} |
345 | 341 |
|
346 | 342 |
protected: |
347 | 343 |
|
348 | 344 |
void attach(ObserverBase& observer) { |
349 | 345 |
observer._index = _observers.insert(_observers.begin(), &observer); |
350 | 346 |
observer._notifier = this; |
351 | 347 |
} |
352 | 348 |
|
353 | 349 |
void detach(ObserverBase& observer) { |
354 | 350 |
_observers.erase(observer._index); |
355 | 351 |
observer._index = _observers.end(); |
356 | 352 |
observer._notifier = 0; |
357 | 353 |
} |
358 | 354 |
|
359 | 355 |
public: |
360 | 356 |
|
361 | 357 |
/// \brief Notifies all the registed observers about an item added to |
362 | 358 |
/// the container. |
363 | 359 |
/// |
364 | 360 |
/// It notifies all the registed observers about an item added to |
365 | 361 |
/// the container. |
366 | 362 |
/// |
367 | 363 |
void add(const Item& item) { |
368 | 364 |
typename Observers::reverse_iterator it; |
369 | 365 |
try { |
370 | 366 |
for (it = _observers.rbegin(); it != _observers.rend(); ++it) { |
371 | 367 |
(*it)->add(item); |
372 | 368 |
} |
373 | 369 |
} catch (...) { |
374 | 370 |
typename Observers::iterator jt; |
375 | 371 |
for (jt = it.base(); jt != _observers.end(); ++jt) { |
376 | 372 |
(*jt)->erase(item); |
377 | 373 |
} |
378 | 374 |
throw; |
379 | 375 |
} |
380 | 376 |
} |
381 | 377 |
|
382 | 378 |
/// \brief Notifies all the registed observers about more item added to |
383 | 379 |
/// the container. |
384 | 380 |
/// |
385 | 381 |
/// It notifies all the registed observers about more item added to |
386 | 382 |
/// the container. |
387 | 383 |
/// |
388 | 384 |
void add(const std::vector<Item>& items) { |
389 | 385 |
typename Observers::reverse_iterator it; |
390 | 386 |
try { |
391 | 387 |
for (it = _observers.rbegin(); it != _observers.rend(); ++it) { |
392 | 388 |
(*it)->add(items); |
393 | 389 |
} |
394 | 390 |
} catch (...) { |
395 | 391 |
typename Observers::iterator jt; |
396 | 392 |
for (jt = it.base(); jt != _observers.end(); ++jt) { |
397 | 393 |
(*jt)->erase(items); |
398 | 394 |
} |
399 | 395 |
throw; |
400 | 396 |
} |
401 | 397 |
} |
402 | 398 |
|
403 | 399 |
/// \brief Notifies all the registed observers about an item erased from |
404 | 400 |
/// the container. |
405 | 401 |
/// |
406 | 402 |
/// It notifies all the registed observers about an item erased from |
407 | 403 |
/// the container. |
408 | 404 |
/// |
409 | 405 |
void erase(const Item& item) throw() { |
410 | 406 |
typename Observers::iterator it = _observers.begin(); |
411 | 407 |
while (it != _observers.end()) { |
412 | 408 |
try { |
413 | 409 |
(*it)->erase(item); |
414 | 410 |
++it; |
415 | 411 |
} catch (const ImmediateDetach&) { |
416 | 412 |
it = _observers.erase(it); |
417 | 413 |
(*it)->_index = _observers.end(); |
418 | 414 |
(*it)->_notifier = 0; |
419 | 415 |
} |
420 | 416 |
} |
421 | 417 |
} |
422 | 418 |
|
423 | 419 |
/// \brief Notifies all the registed observers about more item erased |
424 | 420 |
/// from the container. |
425 | 421 |
/// |
426 | 422 |
/// It notifies all the registed observers about more item erased from |
427 | 423 |
/// the container. |
428 | 424 |
/// |
429 | 425 |
void erase(const std::vector<Item>& items) { |
430 | 426 |
typename Observers::iterator it = _observers.begin(); |
431 | 427 |
while (it != _observers.end()) { |
432 | 428 |
try { |
433 | 429 |
(*it)->erase(items); |
434 | 430 |
++it; |
435 | 431 |
} catch (const ImmediateDetach&) { |
436 | 432 |
it = _observers.erase(it); |
437 | 433 |
(*it)->_index = _observers.end(); |
438 | 434 |
(*it)->_notifier = 0; |
439 | 435 |
} |
440 | 436 |
} |
441 | 437 |
} |
442 | 438 |
|
443 | 439 |
/// \brief Notifies all the registed observers about the container is |
444 | 440 |
/// built. |
445 | 441 |
/// |
446 | 442 |
/// Notifies all the registed observers about the container is built |
447 | 443 |
/// from an empty container. |
448 | 444 |
void build() { |
449 | 445 |
typename Observers::reverse_iterator it; |
450 | 446 |
try { |
451 | 447 |
for (it = _observers.rbegin(); it != _observers.rend(); ++it) { |
452 | 448 |
(*it)->build(); |
453 | 449 |
} |
454 | 450 |
} catch (...) { |
455 | 451 |
typename Observers::iterator jt; |
456 | 452 |
for (jt = it.base(); jt != _observers.end(); ++jt) { |
457 | 453 |
(*jt)->clear(); |
458 | 454 |
} |
459 | 455 |
throw; |
460 | 456 |
} |
461 | 457 |
} |
462 | 458 |
|
463 | 459 |
/// \brief Notifies all the registed observers about all items are |
464 | 460 |
/// erased. |
465 | 461 |
/// |
466 | 462 |
/// Notifies all the registed observers about all items are erased |
467 | 463 |
/// from the container. |
468 | 464 |
void clear() { |
469 | 465 |
typename Observers::iterator it = _observers.begin(); |
470 | 466 |
while (it != _observers.end()) { |
471 | 467 |
try { |
472 | 468 |
(*it)->clear(); |
473 | 469 |
++it; |
474 | 470 |
} catch (const ImmediateDetach&) { |
475 | 471 |
it = _observers.erase(it); |
476 | 472 |
(*it)->_index = _observers.end(); |
477 | 473 |
(*it)->_notifier = 0; |
478 | 474 |
} |
479 | 475 |
} |
480 | 476 |
} |
481 | 477 |
}; |
482 | 478 |
|
483 | 479 |
} |
484 | 480 |
|
485 | 481 |
#endif |
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BEZIER_H |
20 | 20 |
#define LEMON_BEZIER_H |
21 | 21 |
|
22 | 22 |
///\ingroup misc |
23 | 23 |
///\file |
24 | 24 |
///\brief Classes to compute with Bezier curves. |
25 | 25 |
/// |
26 | 26 |
///Up to now this file is used internally by \ref graph_to_eps.h |
27 |
/// |
|
28 |
///\author Alpar Juttner |
|
29 | 27 |
|
30 | 28 |
#include<lemon/dim2.h> |
31 | 29 |
|
32 | 30 |
namespace lemon { |
33 | 31 |
namespace dim2 { |
34 | 32 |
|
35 | 33 |
class BezierBase { |
36 | 34 |
public: |
37 | 35 |
typedef Point<double> Point; |
38 | 36 |
protected: |
39 | 37 |
static Point conv(Point x,Point y,double t) {return (1-t)*x+t*y;} |
40 | 38 |
}; |
41 | 39 |
|
42 | 40 |
class Bezier1 : public BezierBase |
43 | 41 |
{ |
44 | 42 |
public: |
45 | 43 |
Point p1,p2; |
46 | 44 |
|
47 | 45 |
Bezier1() {} |
48 | 46 |
Bezier1(Point _p1, Point _p2) :p1(_p1), p2(_p2) {} |
49 | 47 |
|
50 | 48 |
Point operator()(double t) const |
51 | 49 |
{ |
52 | 50 |
// return conv(conv(p1,p2,t),conv(p2,p3,t),t); |
53 | 51 |
return conv(p1,p2,t); |
54 | 52 |
} |
55 | 53 |
Bezier1 before(double t) const |
56 | 54 |
{ |
57 | 55 |
return Bezier1(p1,conv(p1,p2,t)); |
58 | 56 |
} |
59 | 57 |
|
60 | 58 |
Bezier1 after(double t) const |
61 | 59 |
{ |
62 | 60 |
return Bezier1(conv(p1,p2,t),p2); |
63 | 61 |
} |
64 | 62 |
|
65 | 63 |
Bezier1 revert() const { return Bezier1(p2,p1);} |
66 | 64 |
Bezier1 operator()(double a,double b) const { return before(b).after(a/b); } |
67 | 65 |
Point grad() const { return p2-p1; } |
68 | 66 |
Point norm() const { return rot90(p2-p1); } |
69 | 67 |
Point grad(double) const { return grad(); } |
70 | 68 |
Point norm(double t) const { return rot90(grad(t)); } |
71 | 69 |
}; |
72 | 70 |
|
73 | 71 |
class Bezier2 : public BezierBase |
74 | 72 |
{ |
75 | 73 |
public: |
76 | 74 |
Point p1,p2,p3; |
77 | 75 |
|
78 | 76 |
Bezier2() {} |
79 | 77 |
Bezier2(Point _p1, Point _p2, Point _p3) :p1(_p1), p2(_p2), p3(_p3) {} |
80 | 78 |
Bezier2(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,.5)), p3(b.p2) {} |
81 | 79 |
Point operator()(double t) const |
82 | 80 |
{ |
83 | 81 |
// return conv(conv(p1,p2,t),conv(p2,p3,t),t); |
84 | 82 |
return ((1-t)*(1-t))*p1+(2*(1-t)*t)*p2+(t*t)*p3; |
85 | 83 |
} |
86 | 84 |
Bezier2 before(double t) const |
87 | 85 |
{ |
88 | 86 |
Point q(conv(p1,p2,t)); |
89 | 87 |
Point r(conv(p2,p3,t)); |
90 | 88 |
return Bezier2(p1,q,conv(q,r,t)); |
91 | 89 |
} |
92 | 90 |
|
93 | 91 |
Bezier2 after(double t) const |
94 | 92 |
{ |
95 | 93 |
Point q(conv(p1,p2,t)); |
96 | 94 |
Point r(conv(p2,p3,t)); |
97 | 95 |
return Bezier2(conv(q,r,t),r,p3); |
98 | 96 |
} |
99 | 97 |
Bezier2 revert() const { return Bezier2(p3,p2,p1);} |
100 | 98 |
Bezier2 operator()(double a,double b) const { return before(b).after(a/b); } |
101 | 99 |
Bezier1 grad() const { return Bezier1(2.0*(p2-p1),2.0*(p3-p2)); } |
102 | 100 |
Bezier1 norm() const { return Bezier1(2.0*rot90(p2-p1),2.0*rot90(p3-p2)); } |
103 | 101 |
Point grad(double t) const { return grad()(t); } |
104 | 102 |
Point norm(double t) const { return rot90(grad(t)); } |
105 | 103 |
}; |
106 | 104 |
|
107 | 105 |
class Bezier3 : public BezierBase |
108 | 106 |
{ |
109 | 107 |
public: |
110 | 108 |
Point p1,p2,p3,p4; |
111 | 109 |
|
112 | 110 |
Bezier3() {} |
113 | 111 |
Bezier3(Point _p1, Point _p2, Point _p3, Point _p4) |
114 | 112 |
: p1(_p1), p2(_p2), p3(_p3), p4(_p4) {} |
115 | 113 |
Bezier3(const Bezier1 &b) : p1(b.p1), p2(conv(b.p1,b.p2,1.0/3.0)), |
116 | 114 |
p3(conv(b.p1,b.p2,2.0/3.0)), p4(b.p2) {} |
117 | 115 |
Bezier3(const Bezier2 &b) : p1(b.p1), p2(conv(b.p1,b.p2,2.0/3.0)), |
118 | 116 |
p3(conv(b.p2,b.p3,1.0/3.0)), p4(b.p3) {} |
119 | 117 |
|
120 | 118 |
Point operator()(double t) const |
121 | 119 |
{ |
122 | 120 |
// return Bezier2(conv(p1,p2,t),conv(p2,p3,t),conv(p3,p4,t))(t); |
123 | 121 |
return ((1-t)*(1-t)*(1-t))*p1+(3*t*(1-t)*(1-t))*p2+ |
124 | 122 |
(3*t*t*(1-t))*p3+(t*t*t)*p4; |
125 | 123 |
} |
126 | 124 |
Bezier3 before(double t) const |
127 | 125 |
{ |
128 | 126 |
Point p(conv(p1,p2,t)); |
129 | 127 |
Point q(conv(p2,p3,t)); |
130 | 128 |
Point r(conv(p3,p4,t)); |
131 | 129 |
Point a(conv(p,q,t)); |
132 | 130 |
Point b(conv(q,r,t)); |
133 | 131 |
Point c(conv(a,b,t)); |
134 | 132 |
return Bezier3(p1,p,a,c); |
135 | 133 |
} |
136 | 134 |
|
137 | 135 |
Bezier3 after(double t) const |
138 | 136 |
{ |
139 | 137 |
Point p(conv(p1,p2,t)); |
140 | 138 |
Point q(conv(p2,p3,t)); |
141 | 139 |
Point r(conv(p3,p4,t)); |
142 | 140 |
Point a(conv(p,q,t)); |
143 | 141 |
Point b(conv(q,r,t)); |
144 | 142 |
Point c(conv(a,b,t)); |
145 | 143 |
return Bezier3(c,b,r,p4); |
146 | 144 |
} |
147 | 145 |
Bezier3 revert() const { return Bezier3(p4,p3,p2,p1);} |
148 | 146 |
Bezier3 operator()(double a,double b) const { return before(b).after(a/b); } |
149 | 147 |
Bezier2 grad() const { return Bezier2(3.0*(p2-p1),3.0*(p3-p2),3.0*(p4-p3)); } |
150 | 148 |
Bezier2 norm() const { return Bezier2(3.0*rot90(p2-p1), |
151 | 149 |
3.0*rot90(p3-p2), |
152 | 150 |
3.0*rot90(p4-p3)); } |
153 | 151 |
Point grad(double t) const { return grad()(t); } |
154 | 152 |
Point norm(double t) const { return rot90(grad(t)); } |
155 | 153 |
|
156 | 154 |
template<class R,class F,class S,class D> |
157 | 155 |
R recSplit(F &_f,const S &_s,D _d) const |
158 | 156 |
{ |
159 | 157 |
const Point a=(p1+p2)/2; |
160 | 158 |
const Point b=(p2+p3)/2; |
161 | 159 |
const Point c=(p3+p4)/2; |
162 | 160 |
const Point d=(a+b)/2; |
163 | 161 |
const Point e=(b+c)/2; |
164 | 162 |
const Point f=(d+e)/2; |
165 | 163 |
R f1=_f(Bezier3(p1,a,d,e),_d); |
166 | 164 |
R f2=_f(Bezier3(e,d,c,p4),_d); |
167 | 165 |
return _s(f1,f2); |
168 | 166 |
} |
169 | 167 |
|
170 | 168 |
}; |
171 | 169 |
|
172 | 170 |
|
173 | 171 |
} //END OF NAMESPACE dim2 |
174 | 172 |
} //END OF NAMESPACE lemon |
175 | 173 |
|
176 | 174 |
#endif // LEMON_BEZIER_H |
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_VECTOR_MAP_H |
20 | 20 |
#define LEMON_BITS_VECTOR_MAP_H |
21 | 21 |
|
22 | 22 |
#include <vector> |
23 | 23 |
#include <algorithm> |
24 | 24 |
|
25 | 25 |
#include <lemon/bits/traits.h> |
26 | 26 |
#include <lemon/bits/utility.h> |
27 | 27 |
|
28 | 28 |
#include <lemon/bits/alteration_notifier.h> |
29 | 29 |
|
30 | 30 |
#include <lemon/concept_check.h> |
31 | 31 |
#include <lemon/concepts/maps.h> |
32 | 32 |
|
33 | 33 |
///\ingroup graphbits |
34 | 34 |
/// |
35 | 35 |
///\file |
36 | 36 |
///\brief Vector based graph maps. |
37 | 37 |
namespace lemon { |
38 | 38 |
|
39 | 39 |
/// \ingroup graphbits |
40 | 40 |
/// |
41 | 41 |
/// \brief Graph map based on the std::vector storage. |
42 | 42 |
/// |
43 | 43 |
/// The VectorMap template class is graph map structure what |
44 | 44 |
/// automatically updates the map when a key is added to or erased from |
45 | 45 |
/// the map. This map type uses the std::vector to store the values. |
46 | 46 |
/// |
47 |
/// \param Notifier The AlterationNotifier that will notify this map. |
|
48 |
/// \param Item The item type of the graph items. |
|
49 |
/// \param Value The value type of the map. |
|
50 |
/// |
|
51 |
/// \ |
|
47 |
/// \tparam _Notifier The AlterationNotifier that will notify this map. |
|
48 |
/// \tparam _Item The item type of the graph items. |
|
49 |
/// \tparam _Value The value type of the map. |
|
50 |
/// \todo Fix the doc: there is _Graph parameter instead of _Notifier. |
|
52 | 51 |
template <typename _Graph, typename _Item, typename _Value> |
53 | 52 |
class VectorMap |
54 | 53 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { |
55 | 54 |
private: |
56 | 55 |
|
57 | 56 |
/// The container type of the map. |
58 | 57 |
typedef std::vector<_Value> Container; |
59 | 58 |
|
60 | 59 |
public: |
61 | 60 |
|
62 | 61 |
/// The graph type of the map. |
63 | 62 |
typedef _Graph Graph; |
64 | 63 |
/// The item type of the map. |
65 | 64 |
typedef _Item Item; |
66 | 65 |
/// The reference map tag. |
67 | 66 |
typedef True ReferenceMapTag; |
68 | 67 |
|
69 | 68 |
/// The key type of the map. |
70 | 69 |
typedef _Item Key; |
71 | 70 |
/// The value type of the map. |
72 | 71 |
typedef _Value Value; |
73 | 72 |
|
74 | 73 |
/// The notifier type. |
75 | 74 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
76 | 75 |
|
77 | 76 |
/// The map type. |
78 | 77 |
typedef VectorMap Map; |
79 | 78 |
/// The base class of the map. |
80 | 79 |
typedef typename Notifier::ObserverBase Parent; |
81 | 80 |
|
82 | 81 |
/// The reference type of the map; |
83 | 82 |
typedef typename Container::reference Reference; |
84 | 83 |
/// The const reference type of the map; |
85 | 84 |
typedef typename Container::const_reference ConstReference; |
86 | 85 |
|
87 | 86 |
|
88 | 87 |
/// \brief Constructor to attach the new map into the notifier. |
89 | 88 |
/// |
90 | 89 |
/// It constructs a map and attachs it into the notifier. |
91 | 90 |
/// It adds all the items of the graph to the map. |
92 | 91 |
VectorMap(const Graph& graph) { |
93 | 92 |
Parent::attach(graph.notifier(Item())); |
94 | 93 |
container.resize(Parent::notifier()->maxId() + 1); |
95 | 94 |
} |
96 | 95 |
|
97 | 96 |
/// \brief Constructor uses given value to initialize the map. |
98 | 97 |
/// |
99 | 98 |
/// It constructs a map uses a given value to initialize the map. |
100 | 99 |
/// It adds all the items of the graph to the map. |
101 | 100 |
VectorMap(const Graph& graph, const Value& value) { |
102 | 101 |
Parent::attach(graph.notifier(Item())); |
103 | 102 |
container.resize(Parent::notifier()->maxId() + 1, value); |
104 | 103 |
} |
105 | 104 |
|
106 | 105 |
/// \brief Copy constructor |
107 | 106 |
/// |
108 | 107 |
/// Copy constructor. |
109 | 108 |
VectorMap(const VectorMap& _copy) : Parent() { |
110 | 109 |
if (_copy.attached()) { |
111 | 110 |
Parent::attach(*_copy.notifier()); |
112 | 111 |
container = _copy.container; |
113 | 112 |
} |
114 | 113 |
} |
115 | 114 |
|
116 | 115 |
/// \brief Assign operator. |
117 | 116 |
/// |
118 | 117 |
/// This operator assigns for each item in the map the |
119 | 118 |
/// value mapped to the same item in the copied map. |
120 | 119 |
/// The parameter map should be indiced with the same |
121 | 120 |
/// itemset because this assign operator does not change |
122 | 121 |
/// the container of the map. |
123 | 122 |
VectorMap& operator=(const VectorMap& cmap) { |
124 | 123 |
return operator=<VectorMap>(cmap); |
125 | 124 |
} |
126 | 125 |
|
127 | 126 |
|
128 | 127 |
/// \brief Template assign operator. |
129 | 128 |
/// |
130 | 129 |
/// The given parameter should be conform to the ReadMap |
131 | 130 |
/// concecpt and could be indiced by the current item set of |
132 | 131 |
/// the NodeMap. In this case the value for each item |
133 | 132 |
/// is assigned by the value of the given ReadMap. |
134 | 133 |
template <typename CMap> |
135 | 134 |
VectorMap& operator=(const CMap& cmap) { |
136 | 135 |
checkConcept<concepts::ReadMap<Key, _Value>, CMap>(); |
137 | 136 |
const typename Parent::Notifier* nf = Parent::notifier(); |
138 | 137 |
Item it; |
139 | 138 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
140 | 139 |
set(it, cmap[it]); |
141 | 140 |
} |
142 | 141 |
return *this; |
143 | 142 |
} |
144 | 143 |
|
145 | 144 |
public: |
146 | 145 |
|
147 | 146 |
/// \brief The subcript operator. |
148 | 147 |
/// |
149 | 148 |
/// The subscript operator. The map can be subscripted by the |
150 | 149 |
/// actual items of the graph. |
151 | 150 |
Reference operator[](const Key& key) { |
152 | 151 |
return container[Parent::notifier()->id(key)]; |
153 | 152 |
} |
154 | 153 |
|
155 | 154 |
/// \brief The const subcript operator. |
156 | 155 |
/// |
157 | 156 |
/// The const subscript operator. The map can be subscripted by the |
158 | 157 |
/// actual items of the graph. |
159 | 158 |
ConstReference operator[](const Key& key) const { |
160 | 159 |
return container[Parent::notifier()->id(key)]; |
161 | 160 |
} |
162 | 161 |
|
163 | 162 |
|
164 | 163 |
/// \brief The setter function of the map. |
165 | 164 |
/// |
166 | 165 |
/// It the same as operator[](key) = value expression. |
167 | 166 |
void set(const Key& key, const Value& value) { |
168 | 167 |
(*this)[key] = value; |
169 | 168 |
} |
170 | 169 |
|
171 | 170 |
protected: |
172 | 171 |
|
173 | 172 |
/// \brief Adds a new key to the map. |
174 | 173 |
/// |
175 | 174 |
/// It adds a new key to the map. It called by the observer notifier |
176 | 175 |
/// and it overrides the add() member function of the observer base. |
177 | 176 |
virtual void add(const Key& key) { |
178 | 177 |
int id = Parent::notifier()->id(key); |
179 | 178 |
if (id >= int(container.size())) { |
180 | 179 |
container.resize(id + 1); |
181 | 180 |
} |
182 | 181 |
} |
183 | 182 |
|
184 | 183 |
/// \brief Adds more new keys to the map. |
185 | 184 |
/// |
186 | 185 |
/// It adds more new keys to the map. It called by the observer notifier |
187 | 186 |
/// and it overrides the add() member function of the observer base. |
188 | 187 |
virtual void add(const std::vector<Key>& keys) { |
189 | 188 |
int max = container.size() - 1; |
190 | 189 |
for (int i = 0; i < int(keys.size()); ++i) { |
191 | 190 |
int id = Parent::notifier()->id(keys[i]); |
192 | 191 |
if (id >= max) { |
193 | 192 |
max = id; |
194 | 193 |
} |
195 | 194 |
} |
196 | 195 |
container.resize(max + 1); |
197 | 196 |
} |
198 | 197 |
|
199 | 198 |
/// \brief Erase a key from the map. |
200 | 199 |
/// |
201 | 200 |
/// Erase a key from the map. It called by the observer notifier |
202 | 201 |
/// and it overrides the erase() member function of the observer base. |
203 | 202 |
virtual void erase(const Key& key) { |
204 | 203 |
container[Parent::notifier()->id(key)] = Value(); |
205 | 204 |
} |
206 | 205 |
|
207 | 206 |
/// \brief Erase more keys from the map. |
208 | 207 |
/// |
209 | 208 |
/// Erase more keys from the map. It called by the observer notifier |
210 | 209 |
/// and it overrides the erase() member function of the observer base. |
211 | 210 |
virtual void erase(const std::vector<Key>& keys) { |
212 | 211 |
for (int i = 0; i < int(keys.size()); ++i) { |
213 | 212 |
container[Parent::notifier()->id(keys[i])] = Value(); |
214 | 213 |
} |
215 | 214 |
} |
216 | 215 |
|
217 | 216 |
/// \brief Buildes the map. |
218 | 217 |
/// |
219 | 218 |
/// It buildes the map. It called by the observer notifier |
220 | 219 |
/// and it overrides the build() member function of the observer base. |
221 | 220 |
virtual void build() { |
222 | 221 |
int size = Parent::notifier()->maxId() + 1; |
223 | 222 |
container.reserve(size); |
224 | 223 |
container.resize(size); |
225 | 224 |
} |
226 | 225 |
|
227 | 226 |
/// \brief Clear the map. |
228 | 227 |
/// |
229 | 228 |
/// It erase all items from the map. It called by the observer notifier |
230 | 229 |
/// and it overrides the clear() member function of the observer base. |
231 | 230 |
virtual void clear() { |
232 | 231 |
container.clear(); |
233 | 232 |
} |
234 | 233 |
|
235 | 234 |
private: |
236 | 235 |
|
237 | 236 |
Container container; |
238 | 237 |
|
239 | 238 |
}; |
240 | 239 |
|
241 | 240 |
} |
242 | 241 |
|
243 | 242 |
#endif |
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_COLOR_H |
20 | 20 |
#define LEMON_COLOR_H |
21 | 21 |
|
22 | 22 |
#include<vector> |
23 | 23 |
#include<lemon/math.h> |
24 | 24 |
#include<lemon/maps.h> |
25 | 25 |
|
26 | 26 |
|
27 | 27 |
///\ingroup misc |
28 | 28 |
///\file |
29 | 29 |
///\brief Tools to manage RGB colors. |
30 |
/// |
|
31 |
///\author Alpar Juttner |
|
32 | 30 |
|
33 | 31 |
namespace lemon { |
34 | 32 |
|
35 | 33 |
|
36 | 34 |
/// \addtogroup misc |
37 | 35 |
/// @{ |
38 | 36 |
|
39 | 37 |
///Data structure representing RGB colors. |
40 | 38 |
|
41 | 39 |
///Data structure representing RGB colors. |
42 | 40 |
class Color |
43 | 41 |
{ |
44 | 42 |
double _r,_g,_b; |
45 | 43 |
public: |
46 | 44 |
///Default constructor |
47 | 45 |
Color() {} |
48 | 46 |
///Constructor |
49 | 47 |
Color(double r,double g,double b) :_r(r),_g(g),_b(b) {}; |
50 | 48 |
///Set the red component |
51 | 49 |
double & red() {return _r;} |
52 | 50 |
///Return the red component |
53 | 51 |
const double & red() const {return _r;} |
54 | 52 |
///Set the green component |
55 | 53 |
double & green() {return _g;} |
56 | 54 |
///Return the green component |
57 | 55 |
const double & green() const {return _g;} |
58 | 56 |
///Set the blue component |
59 | 57 |
double & blue() {return _b;} |
60 | 58 |
///Return the blue component |
61 | 59 |
const double & blue() const {return _b;} |
62 | 60 |
///Set the color components |
63 | 61 |
void set(double r,double g,double b) { _r=r;_g=g;_b=b; }; |
64 | 62 |
}; |
65 | 63 |
|
66 | 64 |
/// White color constant |
67 | 65 |
extern const Color WHITE; |
68 | 66 |
/// Black color constant |
69 | 67 |
extern const Color BLACK; |
70 | 68 |
/// Red color constant |
71 | 69 |
extern const Color RED; |
72 | 70 |
/// Green color constant |
73 | 71 |
extern const Color GREEN; |
74 | 72 |
/// Blue color constant |
75 | 73 |
extern const Color BLUE; |
76 | 74 |
/// Yellow color constant |
77 | 75 |
extern const Color YELLOW; |
78 | 76 |
/// Magenta color constant |
79 | 77 |
extern const Color MAGENTA; |
80 | 78 |
/// Cyan color constant |
81 | 79 |
extern const Color CYAN; |
82 | 80 |
/// Grey color constant |
83 | 81 |
extern const Color GREY; |
84 | 82 |
/// Dark red color constant |
85 | 83 |
extern const Color DARK_RED; |
86 | 84 |
/// Dark green color constant |
87 | 85 |
extern const Color DARK_GREEN; |
88 | 86 |
/// Drak blue color constant |
89 | 87 |
extern const Color DARK_BLUE; |
90 | 88 |
/// Dark yellow color constant |
91 | 89 |
extern const Color DARK_YELLOW; |
92 | 90 |
/// Dark magenta color constant |
93 | 91 |
extern const Color DARK_MAGENTA; |
94 | 92 |
/// Dark cyan color constant |
95 | 93 |
extern const Color DARK_CYAN; |
96 | 94 |
|
97 | 95 |
///Map <tt>int</tt>s to different \ref Color "Color"s |
98 | 96 |
|
99 | 97 |
///This map assigns one of the predefined \ref Color "Color"s to |
100 | 98 |
///each <tt>int</tt>. It is possible to change the colors as well as |
101 | 99 |
///their number. The integer range is cyclically mapped to the |
102 | 100 |
///provided set of colors. |
103 | 101 |
/// |
104 | 102 |
///This is a true \ref concepts::ReferenceMap "reference map", so |
105 | 103 |
///you can also change the actual colors. |
106 | 104 |
|
107 | 105 |
class Palette : public MapBase<int,Color> |
108 | 106 |
{ |
109 | 107 |
std::vector<Color> colors; |
110 | 108 |
public: |
111 | 109 |
///Constructor |
112 | 110 |
|
113 | 111 |
///Constructor |
114 | 112 |
///\param have_white indicates whether white is amongst the |
115 | 113 |
///provided initial colors (\c true) or not (\c false). If it is true, |
116 | 114 |
///white will be assigned to \c 0. |
117 | 115 |
///\param num the number of the allocated colors. If it is \c -1, |
118 | 116 |
///the default color configuration is set up (26 color plus optionaly the |
119 | 117 |
///white). If \c num is less then 26/27 then the default color |
120 | 118 |
///list is cut. Otherwise the color list is filled repeatedly with |
121 | 119 |
///the default color list. (The colors can be changed later on.) |
122 | 120 |
Palette(bool have_white=false,int num=-1) |
123 | 121 |
{ |
124 | 122 |
if (num==0) return; |
125 | 123 |
do { |
126 | 124 |
if(have_white) colors.push_back(Color(1,1,1)); |
127 | 125 |
|
128 | 126 |
colors.push_back(Color(0,0,0)); |
129 | 127 |
colors.push_back(Color(1,0,0)); |
130 | 128 |
colors.push_back(Color(0,1,0)); |
131 | 129 |
colors.push_back(Color(0,0,1)); |
132 | 130 |
colors.push_back(Color(1,1,0)); |
133 | 131 |
colors.push_back(Color(1,0,1)); |
134 | 132 |
colors.push_back(Color(0,1,1)); |
135 | 133 |
|
136 | 134 |
colors.push_back(Color(.5,0,0)); |
137 | 135 |
colors.push_back(Color(0,.5,0)); |
138 | 136 |
colors.push_back(Color(0,0,.5)); |
139 | 137 |
colors.push_back(Color(.5,.5,0)); |
140 | 138 |
colors.push_back(Color(.5,0,.5)); |
141 | 139 |
colors.push_back(Color(0,.5,.5)); |
142 | 140 |
|
143 | 141 |
colors.push_back(Color(.5,.5,.5)); |
144 | 142 |
colors.push_back(Color(1,.5,.5)); |
145 | 143 |
colors.push_back(Color(.5,1,.5)); |
146 | 144 |
colors.push_back(Color(.5,.5,1)); |
147 | 145 |
colors.push_back(Color(1,1,.5)); |
148 | 146 |
colors.push_back(Color(1,.5,1)); |
149 | 147 |
colors.push_back(Color(.5,1,1)); |
150 | 148 |
|
151 | 149 |
colors.push_back(Color(1,.5,0)); |
152 | 150 |
colors.push_back(Color(.5,1,0)); |
153 | 151 |
colors.push_back(Color(1,0,.5)); |
154 | 152 |
colors.push_back(Color(0,1,.5)); |
155 | 153 |
colors.push_back(Color(0,.5,1)); |
156 | 154 |
colors.push_back(Color(.5,0,1)); |
157 | 155 |
} while(int(colors.size())<num); |
158 | 156 |
// colors.push_back(Color(1,1,1)); |
159 | 157 |
if(num>=0) colors.resize(num); |
160 | 158 |
} |
161 | 159 |
///\e |
162 | 160 |
Color &operator[](int i) |
163 | 161 |
{ |
164 | 162 |
return colors[i%colors.size()]; |
165 | 163 |
} |
166 | 164 |
///\e |
167 | 165 |
const Color &operator[](int i) const |
168 | 166 |
{ |
169 | 167 |
return colors[i%colors.size()]; |
170 | 168 |
} |
171 | 169 |
///\e |
172 | 170 |
void set(int i,const Color &c) |
173 | 171 |
{ |
174 | 172 |
colors[i%colors.size()]=c; |
175 | 173 |
} |
176 | 174 |
///Add a new color to the end of the color list. |
177 | 175 |
void add(const Color &c) |
178 | 176 |
{ |
179 | 177 |
colors.push_back(c); |
180 | 178 |
} |
181 | 179 |
|
182 | 180 |
///Sets the number of the exiting colors. |
183 | 181 |
void resize(int s) { colors.resize(s);} |
184 | 182 |
///Returns the number of the existing colors. |
185 | 183 |
int size() const { return int(colors.size());} |
186 | 184 |
}; |
187 | 185 |
|
188 | 186 |
///Returns a visibly distinct \ref Color |
189 | 187 |
|
190 | 188 |
///Returns a \ref Color which is as different from the given parameter |
191 | 189 |
///as it is possible. |
192 | 190 |
inline Color distantColor(const Color &c) |
193 | 191 |
{ |
194 | 192 |
return Color(c.red()<.5?1:0,c.green()<.5?1:0,c.blue()<.5?1:0); |
195 | 193 |
} |
196 | 194 |
///Returns black for light colors and white for the dark ones. |
197 | 195 |
|
198 | 196 |
///Returns black for light colors and white for the dark ones. |
199 | 197 |
inline Color distantBW(const Color &c){ |
200 | 198 |
return (.2125*c.red()+.7154*c.green()+.0721*c.blue())<.5 ? WHITE : BLACK; |
201 | 199 |
} |
202 | 200 |
|
203 | 201 |
/// @} |
204 | 202 |
|
205 | 203 |
} //END OF NAMESPACE LEMON |
206 | 204 |
|
207 | 205 |
#endif // LEMON_COLOR_H |
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup concept |
20 | 20 |
///\file |
21 | 21 |
///\brief Classes for representing paths in digraphs. |
22 | 22 |
/// |
23 | 23 |
///\todo Iterators have obsolete style |
24 | 24 |
|
25 | 25 |
#ifndef LEMON_CONCEPT_PATH_H |
26 | 26 |
#define LEMON_CONCEPT_PATH_H |
27 | 27 |
|
28 | 28 |
#include <lemon/bits/invalid.h> |
29 | 29 |
#include <lemon/bits/utility.h> |
30 | 30 |
#include <lemon/concept_check.h> |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
namespace concepts { |
34 | 34 |
|
35 | 35 |
/// \addtogroup concept |
36 | 36 |
/// @{ |
37 | 37 |
|
38 | 38 |
/// \brief A skeleton structure for representing directed paths in |
39 | 39 |
/// a digraph. |
40 | 40 |
/// |
41 | 41 |
/// A skeleton structure for representing directed paths in a |
42 | 42 |
/// digraph. |
43 |
/// \ |
|
43 |
/// \tparam _Digraph The digraph type in which the path is. |
|
44 | 44 |
/// |
45 | 45 |
/// In a sense, the path can be treated as a list of arcs. The |
46 | 46 |
/// lemon path type stores just this list. As a consequence it |
47 | 47 |
/// cannot enumerate the nodes in the path and the zero length |
48 | 48 |
/// paths cannot store the source. |
49 | 49 |
/// |
50 | 50 |
template <typename _Digraph> |
51 | 51 |
class Path { |
52 | 52 |
public: |
53 | 53 |
|
54 | 54 |
/// Type of the underlying digraph. |
55 | 55 |
typedef _Digraph Digraph; |
56 | 56 |
/// Arc type of the underlying digraph. |
57 | 57 |
typedef typename Digraph::Arc Arc; |
58 | 58 |
|
59 | 59 |
class ArcIt; |
60 | 60 |
|
61 | 61 |
/// \brief Default constructor |
62 | 62 |
Path() {} |
63 | 63 |
|
64 | 64 |
/// \brief Template constructor |
65 | 65 |
template <typename CPath> |
66 | 66 |
Path(const CPath& cpath) {} |
67 | 67 |
|
68 | 68 |
/// \brief Template assigment |
69 | 69 |
template <typename CPath> |
70 | 70 |
Path& operator=(const CPath& cpath) {} |
71 | 71 |
|
72 | 72 |
/// Length of the path ie. the number of arcs in the path. |
73 | 73 |
int length() const { return 0;} |
74 | 74 |
|
75 | 75 |
/// Returns whether the path is empty. |
76 | 76 |
bool empty() const { return true;} |
77 | 77 |
|
78 | 78 |
/// Resets the path to an empty path. |
79 | 79 |
void clear() {} |
80 | 80 |
|
81 | 81 |
/// \brief Lemon style iterator for path arcs |
82 | 82 |
/// |
83 | 83 |
/// This class is used to iterate on the arcs of the paths. |
84 | 84 |
class ArcIt { |
85 | 85 |
public: |
86 | 86 |
/// Default constructor |
87 | 87 |
ArcIt() {} |
88 | 88 |
/// Invalid constructor |
89 | 89 |
ArcIt(Invalid) {} |
90 | 90 |
/// Constructor for first arc |
91 | 91 |
ArcIt(const Path &) {} |
92 | 92 |
|
93 | 93 |
/// Conversion to Arc |
94 | 94 |
operator Arc() const { return INVALID; } |
95 | 95 |
|
96 | 96 |
/// Next arc |
97 | 97 |
ArcIt& operator++() {return *this;} |
98 | 98 |
|
99 | 99 |
/// Comparison operator |
100 | 100 |
bool operator==(const ArcIt&) const {return true;} |
101 | 101 |
/// Comparison operator |
102 | 102 |
bool operator!=(const ArcIt&) const {return true;} |
103 | 103 |
/// Comparison operator |
104 | 104 |
bool operator<(const ArcIt&) const {return false;} |
105 | 105 |
|
106 | 106 |
}; |
107 | 107 |
|
108 | 108 |
template <typename _Path> |
109 | 109 |
struct Constraints { |
110 | 110 |
void constraints() { |
111 | 111 |
Path<Digraph> pc; |
112 | 112 |
_Path p, pp(pc); |
113 | 113 |
int l = p.length(); |
114 | 114 |
int e = p.empty(); |
115 | 115 |
p.clear(); |
116 | 116 |
|
117 | 117 |
p = pc; |
118 | 118 |
|
119 | 119 |
typename _Path::ArcIt id, ii(INVALID), i(p); |
120 | 120 |
|
121 | 121 |
++i; |
122 | 122 |
typename Digraph::Arc ed = i; |
123 | 123 |
|
124 | 124 |
e = (i == ii); |
125 | 125 |
e = (i != ii); |
126 | 126 |
e = (i < ii); |
127 | 127 |
|
128 | 128 |
ignore_unused_variable_warning(l); |
129 | 129 |
ignore_unused_variable_warning(pp); |
130 | 130 |
ignore_unused_variable_warning(e); |
131 | 131 |
ignore_unused_variable_warning(id); |
132 | 132 |
ignore_unused_variable_warning(ii); |
133 | 133 |
ignore_unused_variable_warning(ed); |
134 | 134 |
} |
135 | 135 |
}; |
136 | 136 |
|
137 | 137 |
}; |
138 | 138 |
|
139 | 139 |
namespace _path_bits { |
140 | 140 |
|
141 | 141 |
template <typename _Digraph, typename _Path, typename RevPathTag = void> |
142 | 142 |
struct PathDumperConstraints { |
143 | 143 |
void constraints() { |
144 | 144 |
int l = p.length(); |
145 | 145 |
int e = p.empty(); |
146 | 146 |
|
147 | 147 |
typename _Path::ArcIt id, i(p); |
148 | 148 |
|
149 | 149 |
++i; |
150 | 150 |
typename _Digraph::Arc ed = i; |
151 | 151 |
|
152 | 152 |
e = (i == INVALID); |
153 | 153 |
e = (i != INVALID); |
154 | 154 |
|
155 | 155 |
ignore_unused_variable_warning(l); |
156 | 156 |
ignore_unused_variable_warning(e); |
157 | 157 |
ignore_unused_variable_warning(id); |
158 | 158 |
ignore_unused_variable_warning(ed); |
159 | 159 |
} |
160 | 160 |
_Path& p; |
161 | 161 |
}; |
162 | 162 |
|
163 | 163 |
template <typename _Digraph, typename _Path> |
164 | 164 |
struct PathDumperConstraints< |
165 | 165 |
_Digraph, _Path, |
166 | 166 |
typename enable_if<typename _Path::RevPathTag, void>::type |
167 | 167 |
> { |
168 | 168 |
void constraints() { |
169 | 169 |
int l = p.length(); |
170 | 170 |
int e = p.empty(); |
171 | 171 |
|
172 | 172 |
typename _Path::RevArcIt id, i(p); |
173 | 173 |
|
174 | 174 |
++i; |
175 | 175 |
typename _Digraph::Arc ed = i; |
176 | 176 |
|
177 | 177 |
e = (i == INVALID); |
178 | 178 |
e = (i != INVALID); |
179 | 179 |
|
180 | 180 |
ignore_unused_variable_warning(l); |
181 | 181 |
ignore_unused_variable_warning(e); |
182 | 182 |
ignore_unused_variable_warning(id); |
183 | 183 |
ignore_unused_variable_warning(ed); |
184 | 184 |
} |
185 | 185 |
_Path& p; |
186 | 186 |
}; |
187 | 187 |
|
188 | 188 |
} |
189 | 189 |
|
190 | 190 |
|
191 | 191 |
/// \brief A skeleton structure for path dumpers. |
192 | 192 |
/// |
193 | 193 |
/// A skeleton structure for path dumpers. The path dumpers are |
194 | 194 |
/// the generalization of the paths. The path dumpers can |
195 | 195 |
/// enumerate the arcs of the path wheter in forward or in |
196 | 196 |
/// backward order. In most time these classes are not used |
197 | 197 |
/// directly rather it used to assign a dumped class to a real |
198 | 198 |
/// path type. |
199 | 199 |
/// |
200 | 200 |
/// The main purpose of this concept is that the shortest path |
201 | 201 |
/// algorithms can enumerate easily the arcs in reverse order. |
202 | 202 |
/// If we would like to give back a real path from these |
203 | 203 |
/// algorithms then we should create a temporarly path object. In |
204 | 204 |
/// Lemon such algorithms gives back a path dumper what can |
205 | 205 |
/// assigned to a real path and the dumpers can be implemented as |
206 | 206 |
/// an adaptor class to the predecessor map. |
207 | 207 |
|
208 |
/// \ |
|
208 |
/// \tparam _Digraph The digraph type in which the path is. |
|
209 | 209 |
/// |
210 | 210 |
/// The paths can be constructed from any path type by a |
211 | 211 |
/// template constructor or a template assignment operator. |
212 | 212 |
/// |
213 | 213 |
template <typename _Digraph> |
214 | 214 |
class PathDumper { |
215 | 215 |
public: |
216 | 216 |
|
217 | 217 |
/// Type of the underlying digraph. |
218 | 218 |
typedef _Digraph Digraph; |
219 | 219 |
/// Arc type of the underlying digraph. |
220 | 220 |
typedef typename Digraph::Arc Arc; |
221 | 221 |
|
222 | 222 |
/// Length of the path ie. the number of arcs in the path. |
223 | 223 |
int length() const { return 0;} |
224 | 224 |
|
225 | 225 |
/// Returns whether the path is empty. |
226 | 226 |
bool empty() const { return true;} |
227 | 227 |
|
228 | 228 |
/// \brief Forward or reverse dumping |
229 | 229 |
/// |
230 | 230 |
/// If the RevPathTag is defined and true then reverse dumping |
231 | 231 |
/// is provided in the path dumper. In this case instead of the |
232 | 232 |
/// ArcIt the RevArcIt iterator should be implemented in the |
233 | 233 |
/// dumper. |
234 | 234 |
typedef False RevPathTag; |
235 | 235 |
|
236 | 236 |
/// \brief Lemon style iterator for path arcs |
237 | 237 |
/// |
238 | 238 |
/// This class is used to iterate on the arcs of the paths. |
239 | 239 |
class ArcIt { |
240 | 240 |
public: |
241 | 241 |
/// Default constructor |
242 | 242 |
ArcIt() {} |
243 | 243 |
/// Invalid constructor |
244 | 244 |
ArcIt(Invalid) {} |
245 | 245 |
/// Constructor for first arc |
246 | 246 |
ArcIt(const PathDumper&) {} |
247 | 247 |
|
248 | 248 |
/// Conversion to Arc |
249 | 249 |
operator Arc() const { return INVALID; } |
250 | 250 |
|
251 | 251 |
/// Next arc |
252 | 252 |
ArcIt& operator++() {return *this;} |
253 | 253 |
|
254 | 254 |
/// Comparison operator |
255 | 255 |
bool operator==(const ArcIt&) const {return true;} |
256 | 256 |
/// Comparison operator |
257 | 257 |
bool operator!=(const ArcIt&) const {return true;} |
258 | 258 |
/// Comparison operator |
259 | 259 |
bool operator<(const ArcIt&) const {return false;} |
260 | 260 |
|
261 | 261 |
}; |
262 | 262 |
|
263 | 263 |
/// \brief Lemon style iterator for path arcs |
264 | 264 |
/// |
265 | 265 |
/// This class is used to iterate on the arcs of the paths in |
266 | 266 |
/// reverse direction. |
267 | 267 |
class RevArcIt { |
268 | 268 |
public: |
269 | 269 |
/// Default constructor |
270 | 270 |
RevArcIt() {} |
271 | 271 |
/// Invalid constructor |
272 | 272 |
RevArcIt(Invalid) {} |
273 | 273 |
/// Constructor for first arc |
274 | 274 |
RevArcIt(const PathDumper &) {} |
275 | 275 |
|
276 | 276 |
/// Conversion to Arc |
277 | 277 |
operator Arc() const { return INVALID; } |
278 | 278 |
|
279 | 279 |
/// Next arc |
280 | 280 |
RevArcIt& operator++() {return *this;} |
281 | 281 |
|
282 | 282 |
/// Comparison operator |
283 | 283 |
bool operator==(const RevArcIt&) const {return true;} |
284 | 284 |
/// Comparison operator |
285 | 285 |
bool operator!=(const RevArcIt&) const {return true;} |
286 | 286 |
/// Comparison operator |
287 | 287 |
bool operator<(const RevArcIt&) const {return false;} |
288 | 288 |
|
289 | 289 |
}; |
290 | 290 |
|
291 | 291 |
template <typename _Path> |
292 | 292 |
struct Constraints { |
293 | 293 |
void constraints() { |
294 | 294 |
function_requires<_path_bits:: |
295 | 295 |
PathDumperConstraints<Digraph, _Path> >(); |
296 | 296 |
} |
297 | 297 |
}; |
298 | 298 |
|
299 | 299 |
}; |
300 | 300 |
|
301 | 301 |
|
302 | 302 |
///@} |
303 | 303 |
} |
304 | 304 |
|
305 | 305 |
} // namespace lemon |
306 | 306 |
|
307 | 307 |
#endif // LEMON_CONCEPT_PATH_H |
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_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/graph_utils.h> |
28 | 28 |
#include <lemon/bits/path_dump.h> |
29 | 29 |
#include <lemon/bits/invalid.h> |
30 | 30 |
#include <lemon/error.h> |
31 | 31 |
#include <lemon/maps.h> |
32 | 32 |
|
33 | 33 |
#include <lemon/concept_check.h> |
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
|
38 | 38 |
///Default traits class of Dfs class. |
39 | 39 |
|
40 | 40 |
///Default traits class of Dfs class. |
41 |
///\ |
|
41 |
///\tparam GR Digraph type. |
|
42 | 42 |
template<class GR> |
43 | 43 |
struct DfsDefaultTraits |
44 | 44 |
{ |
45 | 45 |
///The digraph type the algorithm runs on. |
46 | 46 |
typedef GR Digraph; |
47 | 47 |
///\brief The type of the map that stores the last |
48 | 48 |
///arcs of the %DFS paths. |
49 | 49 |
/// |
50 | 50 |
///The type of the map that stores the last |
51 | 51 |
///arcs of the %DFS paths. |
52 | 52 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
53 | 53 |
/// |
54 | 54 |
typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap; |
55 | 55 |
///Instantiates a PredMap. |
56 | 56 |
|
57 | 57 |
///This function instantiates a \ref PredMap. |
58 | 58 |
///\param G is the digraph, to which we would like to define the PredMap. |
59 | 59 |
///\todo The digraph alone may be insufficient to initialize |
60 | 60 |
static PredMap *createPredMap(const GR &G) |
61 | 61 |
{ |
62 | 62 |
return new PredMap(G); |
63 | 63 |
} |
64 | 64 |
|
65 | 65 |
///The type of the map that indicates which nodes are processed. |
66 | 66 |
|
67 | 67 |
///The type of the map that indicates which nodes are processed. |
68 | 68 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
69 | 69 |
///\todo named parameter to set this type, function to read and write. |
70 | 70 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
71 | 71 |
///Instantiates a ProcessedMap. |
72 | 72 |
|
73 | 73 |
///This function instantiates a \ref ProcessedMap. |
74 | 74 |
///\param g is the digraph, to which |
75 | 75 |
///we would like to define the \ref ProcessedMap |
76 | 76 |
#ifdef DOXYGEN |
77 | 77 |
static ProcessedMap *createProcessedMap(const GR &g) |
78 | 78 |
#else |
79 | 79 |
static ProcessedMap *createProcessedMap(const GR &) |
80 | 80 |
#endif |
81 | 81 |
{ |
82 | 82 |
return new ProcessedMap(); |
83 | 83 |
} |
84 | 84 |
///The type of the map that indicates which nodes are reached. |
85 | 85 |
|
86 | 86 |
///The type of the map that indicates which nodes are reached. |
87 | 87 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
88 | 88 |
///\todo named parameter to set this type, function to read and write. |
89 | 89 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
90 | 90 |
///Instantiates a ReachedMap. |
91 | 91 |
|
92 | 92 |
///This function instantiates a \ref ReachedMap. |
93 | 93 |
///\param G is the digraph, to which |
94 | 94 |
///we would like to define the \ref ReachedMap. |
95 | 95 |
static ReachedMap *createReachedMap(const GR &G) |
96 | 96 |
{ |
97 | 97 |
return new ReachedMap(G); |
98 | 98 |
} |
99 | 99 |
///The type of the map that stores the dists of the nodes. |
100 | 100 |
|
101 | 101 |
///The type of the map that stores the dists of the nodes. |
102 | 102 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
103 | 103 |
/// |
104 | 104 |
typedef typename Digraph::template NodeMap<int> DistMap; |
105 | 105 |
///Instantiates a DistMap. |
106 | 106 |
|
107 | 107 |
///This function instantiates a \ref DistMap. |
108 | 108 |
///\param G is the digraph, to which we would like to define the \ref DistMap |
109 | 109 |
static DistMap *createDistMap(const GR &G) |
110 | 110 |
{ |
111 | 111 |
return new DistMap(G); |
112 | 112 |
} |
113 | 113 |
}; |
114 | 114 |
|
115 | 115 |
///%DFS algorithm class. |
116 | 116 |
|
117 | 117 |
///\ingroup search |
118 | 118 |
///This class provides an efficient implementation of the %DFS algorithm. |
119 | 119 |
/// |
120 |
///\ |
|
120 |
///\tparam GR The digraph type the algorithm runs on. The default value is |
|
121 | 121 |
///\ref ListDigraph. The value of GR is not used directly by Dfs, it |
122 | 122 |
///is only passed to \ref DfsDefaultTraits. |
123 |
///\ |
|
123 |
///\tparam TR Traits class to set various data types used by the algorithm. |
|
124 | 124 |
///The default traits class is |
125 | 125 |
///\ref DfsDefaultTraits "DfsDefaultTraits<GR>". |
126 | 126 |
///See \ref DfsDefaultTraits for the documentation of |
127 | 127 |
///a Dfs traits class. |
128 |
/// |
|
129 |
///\author Jacint Szabo and Alpar Juttner |
|
130 | 128 |
#ifdef DOXYGEN |
131 | 129 |
template <typename GR, |
132 | 130 |
typename TR> |
133 | 131 |
#else |
134 | 132 |
template <typename GR=ListDigraph, |
135 | 133 |
typename TR=DfsDefaultTraits<GR> > |
136 | 134 |
#endif |
137 | 135 |
class Dfs { |
138 | 136 |
public: |
139 | 137 |
/** |
140 | 138 |
* \brief \ref Exception for uninitialized parameters. |
141 | 139 |
* |
142 | 140 |
* This error represents problems in the initialization |
143 | 141 |
* of the parameters of the algorithms. |
144 | 142 |
*/ |
145 | 143 |
class UninitializedParameter : public lemon::UninitializedParameter { |
146 | 144 |
public: |
147 | 145 |
virtual const char* what() const throw() { |
148 | 146 |
return "lemon::Dfs::UninitializedParameter"; |
149 | 147 |
} |
150 | 148 |
}; |
151 | 149 |
|
152 | 150 |
typedef TR Traits; |
153 | 151 |
///The type of the underlying digraph. |
154 | 152 |
typedef typename TR::Digraph Digraph; |
155 | 153 |
///\e |
156 | 154 |
typedef typename Digraph::Node Node; |
157 | 155 |
///\e |
158 | 156 |
typedef typename Digraph::NodeIt NodeIt; |
159 | 157 |
///\e |
160 | 158 |
typedef typename Digraph::Arc Arc; |
161 | 159 |
///\e |
162 | 160 |
typedef typename Digraph::OutArcIt OutArcIt; |
163 | 161 |
|
164 | 162 |
///\brief The type of the map that stores the last |
165 | 163 |
///arcs of the %DFS paths. |
166 | 164 |
typedef typename TR::PredMap PredMap; |
167 | 165 |
///The type of the map indicating which nodes are reached. |
168 | 166 |
typedef typename TR::ReachedMap ReachedMap; |
169 | 167 |
///The type of the map indicating which nodes are processed. |
170 | 168 |
typedef typename TR::ProcessedMap ProcessedMap; |
171 | 169 |
///The type of the map that stores the dists of the nodes. |
172 | 170 |
typedef typename TR::DistMap DistMap; |
173 | 171 |
private: |
174 | 172 |
/// Pointer to the underlying digraph. |
175 | 173 |
const Digraph *G; |
176 | 174 |
///Pointer to the map of predecessors arcs. |
177 | 175 |
PredMap *_pred; |
178 | 176 |
///Indicates if \ref _pred is locally allocated (\c true) or not. |
179 | 177 |
bool local_pred; |
180 | 178 |
///Pointer to the map of distances. |
181 | 179 |
DistMap *_dist; |
182 | 180 |
///Indicates if \ref _dist is locally allocated (\c true) or not. |
183 | 181 |
bool local_dist; |
184 | 182 |
///Pointer to the map of reached status of the nodes. |
185 | 183 |
ReachedMap *_reached; |
186 | 184 |
///Indicates if \ref _reached is locally allocated (\c true) or not. |
187 | 185 |
bool local_reached; |
188 | 186 |
///Pointer to the map of processed status of the nodes. |
189 | 187 |
ProcessedMap *_processed; |
190 | 188 |
///Indicates if \ref _processed is locally allocated (\c true) or not. |
191 | 189 |
bool local_processed; |
192 | 190 |
|
193 | 191 |
std::vector<typename Digraph::OutArcIt> _stack; |
194 | 192 |
int _stack_head; |
195 | 193 |
|
196 | 194 |
///Creates the maps if necessary. |
197 | 195 |
|
198 | 196 |
///\todo Better memory allocation (instead of new). |
199 | 197 |
void create_maps() |
200 | 198 |
{ |
201 | 199 |
if(!_pred) { |
202 | 200 |
local_pred = true; |
203 | 201 |
_pred = Traits::createPredMap(*G); |
204 | 202 |
} |
205 | 203 |
if(!_dist) { |
206 | 204 |
local_dist = true; |
207 | 205 |
_dist = Traits::createDistMap(*G); |
208 | 206 |
} |
209 | 207 |
if(!_reached) { |
210 | 208 |
local_reached = true; |
211 | 209 |
_reached = Traits::createReachedMap(*G); |
212 | 210 |
} |
213 | 211 |
if(!_processed) { |
214 | 212 |
local_processed = true; |
215 | 213 |
_processed = Traits::createProcessedMap(*G); |
216 | 214 |
} |
217 | 215 |
} |
218 | 216 |
|
219 | 217 |
protected: |
220 | 218 |
|
221 | 219 |
Dfs() {} |
222 | 220 |
|
223 | 221 |
public: |
224 | 222 |
|
225 | 223 |
typedef Dfs Create; |
226 | 224 |
|
227 | 225 |
///\name Named template parameters |
228 | 226 |
|
229 | 227 |
///@{ |
230 | 228 |
|
231 | 229 |
template <class T> |
232 | 230 |
struct DefPredMapTraits : public Traits { |
233 | 231 |
typedef T PredMap; |
234 | 232 |
static PredMap *createPredMap(const Digraph &G) |
235 | 233 |
{ |
236 | 234 |
throw UninitializedParameter(); |
237 | 235 |
} |
238 | 236 |
}; |
239 | 237 |
///\brief \ref named-templ-param "Named parameter" for setting |
240 | 238 |
///PredMap type |
241 | 239 |
/// |
242 | 240 |
///\ref named-templ-param "Named parameter" for setting PredMap type |
243 | 241 |
/// |
244 | 242 |
template <class T> |
245 | 243 |
struct DefPredMap : public Dfs<Digraph, DefPredMapTraits<T> > { |
246 | 244 |
typedef Dfs<Digraph, DefPredMapTraits<T> > Create; |
247 | 245 |
}; |
248 | 246 |
|
249 | 247 |
|
250 | 248 |
template <class T> |
251 | 249 |
struct DefDistMapTraits : public Traits { |
252 | 250 |
typedef T DistMap; |
253 | 251 |
static DistMap *createDistMap(const Digraph &) |
254 | 252 |
{ |
255 | 253 |
throw UninitializedParameter(); |
256 | 254 |
} |
257 | 255 |
}; |
258 | 256 |
///\brief \ref named-templ-param "Named parameter" for setting |
259 | 257 |
///DistMap type |
260 | 258 |
/// |
261 | 259 |
///\ref named-templ-param "Named parameter" for setting DistMap |
262 | 260 |
///type |
263 | 261 |
template <class T> |
264 | 262 |
struct DefDistMap { |
265 | 263 |
typedef Dfs<Digraph, DefDistMapTraits<T> > Create; |
266 | 264 |
}; |
267 | 265 |
|
268 | 266 |
template <class T> |
269 | 267 |
struct DefReachedMapTraits : public Traits { |
270 | 268 |
typedef T ReachedMap; |
271 | 269 |
static ReachedMap *createReachedMap(const Digraph &) |
272 | 270 |
{ |
273 | 271 |
throw UninitializedParameter(); |
274 | 272 |
} |
275 | 273 |
}; |
276 | 274 |
///\brief \ref named-templ-param "Named parameter" for setting |
277 | 275 |
///ReachedMap type |
278 | 276 |
/// |
279 | 277 |
///\ref named-templ-param "Named parameter" for setting ReachedMap type |
280 | 278 |
/// |
281 | 279 |
template <class T> |
282 | 280 |
struct DefReachedMap : public Dfs< Digraph, DefReachedMapTraits<T> > { |
283 | 281 |
typedef Dfs< Digraph, DefReachedMapTraits<T> > Create; |
284 | 282 |
}; |
285 | 283 |
|
286 | 284 |
template <class T> |
287 | 285 |
struct DefProcessedMapTraits : public Traits { |
288 | 286 |
typedef T ProcessedMap; |
289 | 287 |
static ProcessedMap *createProcessedMap(const Digraph &) |
290 | 288 |
{ |
291 | 289 |
throw UninitializedParameter(); |
292 | 290 |
} |
293 | 291 |
}; |
294 | 292 |
///\brief \ref named-templ-param "Named parameter" for setting |
295 | 293 |
///ProcessedMap type |
296 | 294 |
/// |
297 | 295 |
///\ref named-templ-param "Named parameter" for setting ProcessedMap type |
298 | 296 |
/// |
299 | 297 |
template <class T> |
300 | 298 |
struct DefProcessedMap : public Dfs< Digraph, DefProcessedMapTraits<T> > { |
301 | 299 |
typedef Dfs< Digraph, DefProcessedMapTraits<T> > Create; |
302 | 300 |
}; |
303 | 301 |
|
304 | 302 |
struct DefDigraphProcessedMapTraits : public Traits { |
305 | 303 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
306 | 304 |
static ProcessedMap *createProcessedMap(const Digraph &G) |
307 | 305 |
{ |
308 | 306 |
return new ProcessedMap(G); |
309 | 307 |
} |
310 | 308 |
}; |
311 | 309 |
///\brief \ref named-templ-param "Named parameter" |
312 | 310 |
///for setting the ProcessedMap type to be Digraph::NodeMap<bool>. |
313 | 311 |
/// |
314 | 312 |
///\ref named-templ-param "Named parameter" |
315 | 313 |
///for setting the ProcessedMap type to be Digraph::NodeMap<bool>. |
316 | 314 |
///If you don't set it explicitely, it will be automatically allocated. |
317 | 315 |
template <class T> |
318 | 316 |
class DefProcessedMapToBeDefaultMap : |
319 | 317 |
public Dfs< Digraph, DefDigraphProcessedMapTraits> { |
320 | 318 |
typedef Dfs< Digraph, DefDigraphProcessedMapTraits> Create; |
321 | 319 |
}; |
322 | 320 |
|
323 | 321 |
///@} |
324 | 322 |
|
325 | 323 |
public: |
326 | 324 |
|
327 | 325 |
///Constructor. |
328 | 326 |
|
329 | 327 |
///\param _G the digraph the algorithm will run on. |
330 | 328 |
/// |
331 | 329 |
Dfs(const Digraph& _G) : |
332 | 330 |
G(&_G), |
333 | 331 |
_pred(NULL), local_pred(false), |
334 | 332 |
_dist(NULL), local_dist(false), |
335 | 333 |
_reached(NULL), local_reached(false), |
336 | 334 |
_processed(NULL), local_processed(false) |
337 | 335 |
{ } |
338 | 336 |
|
339 | 337 |
///Destructor. |
340 | 338 |
~Dfs() |
341 | 339 |
{ |
342 | 340 |
if(local_pred) delete _pred; |
343 | 341 |
if(local_dist) delete _dist; |
344 | 342 |
if(local_reached) delete _reached; |
345 | 343 |
if(local_processed) delete _processed; |
346 | 344 |
} |
347 | 345 |
|
348 | 346 |
///Sets the map storing the predecessor arcs. |
349 | 347 |
|
350 | 348 |
///Sets the map storing the predecessor arcs. |
351 | 349 |
///If you don't use this function before calling \ref run(), |
352 | 350 |
///it will allocate one. The destuctor deallocates this |
353 | 351 |
///automatically allocated map, of course. |
354 | 352 |
///\return <tt> (*this) </tt> |
355 | 353 |
Dfs &predMap(PredMap &m) |
356 | 354 |
{ |
357 | 355 |
if(local_pred) { |
358 | 356 |
delete _pred; |
359 | 357 |
local_pred=false; |
360 | 358 |
} |
361 | 359 |
_pred = &m; |
362 | 360 |
return *this; |
363 | 361 |
} |
364 | 362 |
|
365 | 363 |
///Sets the map storing the distances calculated by the algorithm. |
366 | 364 |
|
367 | 365 |
///Sets the map storing the distances calculated by the algorithm. |
368 | 366 |
///If you don't use this function before calling \ref run(), |
369 | 367 |
///it will allocate one. The destuctor deallocates this |
370 | 368 |
///automatically allocated map, of course. |
371 | 369 |
///\return <tt> (*this) </tt> |
372 | 370 |
Dfs &distMap(DistMap &m) |
373 | 371 |
{ |
374 | 372 |
if(local_dist) { |
375 | 373 |
delete _dist; |
376 | 374 |
local_dist=false; |
377 | 375 |
} |
378 | 376 |
_dist = &m; |
379 | 377 |
return *this; |
380 | 378 |
} |
381 | 379 |
|
382 | 380 |
///Sets the map indicating if a node is reached. |
383 | 381 |
|
384 | 382 |
///Sets the map indicating if a node is reached. |
385 | 383 |
///If you don't use this function before calling \ref run(), |
386 | 384 |
///it will allocate one. The destuctor deallocates this |
387 | 385 |
///automatically allocated map, of course. |
388 | 386 |
///\return <tt> (*this) </tt> |
389 | 387 |
Dfs &reachedMap(ReachedMap &m) |
390 | 388 |
{ |
391 | 389 |
if(local_reached) { |
392 | 390 |
delete _reached; |
393 | 391 |
local_reached=false; |
394 | 392 |
} |
395 | 393 |
_reached = &m; |
396 | 394 |
return *this; |
397 | 395 |
} |
398 | 396 |
|
399 | 397 |
///Sets the map indicating if a node is processed. |
400 | 398 |
|
401 | 399 |
///Sets the map indicating if a node is processed. |
402 | 400 |
///If you don't use this function before calling \ref run(), |
403 | 401 |
///it will allocate one. The destuctor deallocates this |
404 | 402 |
///automatically allocated map, of course. |
405 | 403 |
///\return <tt> (*this) </tt> |
406 | 404 |
Dfs &processedMap(ProcessedMap &m) |
407 | 405 |
{ |
408 | 406 |
if(local_processed) { |
409 | 407 |
delete _processed; |
410 | 408 |
local_processed=false; |
411 | 409 |
} |
412 | 410 |
_processed = &m; |
413 | 411 |
return *this; |
414 | 412 |
} |
415 | 413 |
|
416 | 414 |
public: |
417 | 415 |
///\name Execution control |
418 | 416 |
///The simplest way to execute the algorithm is to use |
419 | 417 |
///one of the member functions called \c run(...). |
420 | 418 |
///\n |
421 | 419 |
///If you need more control on the execution, |
422 | 420 |
///first you must call \ref init(), then you can add a source node |
423 | 421 |
///with \ref addSource(). |
424 | 422 |
///Finally \ref start() will perform the actual path |
425 | 423 |
///computation. |
426 | 424 |
|
427 | 425 |
///@{ |
428 | 426 |
|
429 | 427 |
///Initializes the internal data structures. |
430 | 428 |
|
431 | 429 |
///Initializes the internal data structures. |
432 | 430 |
/// |
433 | 431 |
void init() |
434 | 432 |
{ |
435 | 433 |
create_maps(); |
436 | 434 |
_stack.resize(countNodes(*G)); |
437 | 435 |
_stack_head=-1; |
438 | 436 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
439 | 437 |
_pred->set(u,INVALID); |
440 | 438 |
// _predNode->set(u,INVALID); |
441 | 439 |
_reached->set(u,false); |
442 | 440 |
_processed->set(u,false); |
443 | 441 |
} |
444 | 442 |
} |
445 | 443 |
|
446 | 444 |
///Adds a new source node. |
447 | 445 |
|
448 | 446 |
///Adds a new source node to the set of nodes to be processed. |
449 | 447 |
/// |
450 | 448 |
///\warning dists are wrong (or at least strange) |
451 | 449 |
///in case of multiple sources. |
452 | 450 |
void addSource(Node s) |
453 | 451 |
{ |
454 | 452 |
if(!(*_reached)[s]) |
455 | 453 |
{ |
456 | 454 |
_reached->set(s,true); |
457 | 455 |
_pred->set(s,INVALID); |
458 | 456 |
OutArcIt e(*G,s); |
459 | 457 |
if(e!=INVALID) { |
460 | 458 |
_stack[++_stack_head]=e; |
461 | 459 |
_dist->set(s,_stack_head); |
462 | 460 |
} |
463 | 461 |
else { |
464 | 462 |
_processed->set(s,true); |
465 | 463 |
_dist->set(s,0); |
466 | 464 |
} |
467 | 465 |
} |
468 | 466 |
} |
469 | 467 |
|
470 | 468 |
///Processes the next arc. |
471 | 469 |
|
472 | 470 |
///Processes the next arc. |
473 | 471 |
/// |
474 | 472 |
///\return The processed arc. |
475 | 473 |
/// |
476 | 474 |
///\pre The stack must not be empty! |
477 | 475 |
Arc processNextArc() |
478 | 476 |
{ |
479 | 477 |
Node m; |
480 | 478 |
Arc e=_stack[_stack_head]; |
481 | 479 |
if(!(*_reached)[m=G->target(e)]) { |
482 | 480 |
_pred->set(m,e); |
483 | 481 |
_reached->set(m,true); |
484 | 482 |
++_stack_head; |
485 | 483 |
_stack[_stack_head] = OutArcIt(*G, m); |
486 | 484 |
_dist->set(m,_stack_head); |
487 | 485 |
} |
488 | 486 |
else { |
489 | 487 |
m=G->source(e); |
490 | 488 |
++_stack[_stack_head]; |
491 | 489 |
} |
492 | 490 |
while(_stack_head>=0 && _stack[_stack_head]==INVALID) { |
493 | 491 |
_processed->set(m,true); |
494 | 492 |
--_stack_head; |
495 | 493 |
if(_stack_head>=0) { |
496 | 494 |
m=G->source(_stack[_stack_head]); |
497 | 495 |
++_stack[_stack_head]; |
498 | 496 |
} |
499 | 497 |
} |
500 | 498 |
return e; |
501 | 499 |
} |
502 | 500 |
///Next arc to be processed. |
503 | 501 |
|
504 | 502 |
///Next arc to be processed. |
505 | 503 |
/// |
506 | 504 |
///\return The next arc to be processed or INVALID if the stack is |
507 | 505 |
/// empty. |
508 | 506 |
OutArcIt nextArc() |
509 | 507 |
{ |
510 | 508 |
return _stack_head>=0?_stack[_stack_head]:INVALID; |
511 | 509 |
} |
512 | 510 |
|
513 | 511 |
///\brief Returns \c false if there are nodes |
514 | 512 |
///to be processed in the queue |
515 | 513 |
/// |
516 | 514 |
///Returns \c false if there are nodes |
517 | 515 |
///to be processed in the queue |
518 | 516 |
bool emptyQueue() { return _stack_head<0; } |
519 | 517 |
///Returns the number of the nodes to be processed. |
520 | 518 |
|
521 | 519 |
///Returns the number of the nodes to be processed in the queue. |
522 | 520 |
int queueSize() { return _stack_head+1; } |
523 | 521 |
|
524 | 522 |
///Executes the algorithm. |
525 | 523 |
|
526 | 524 |
///Executes the algorithm. |
527 | 525 |
/// |
528 | 526 |
///\pre init() must be called and at least one node should be added |
529 | 527 |
///with addSource() before using this function. |
530 | 528 |
/// |
531 | 529 |
///This method runs the %DFS algorithm from the root node(s) |
532 | 530 |
///in order to |
533 | 531 |
///compute the |
534 | 532 |
///%DFS path to each node. The algorithm computes |
535 | 533 |
///- The %DFS tree. |
536 | 534 |
///- The distance of each node from the root(s) in the %DFS tree. |
537 | 535 |
/// |
538 | 536 |
void start() |
539 | 537 |
{ |
540 | 538 |
while ( !emptyQueue() ) processNextArc(); |
541 | 539 |
} |
542 | 540 |
|
543 | 541 |
///Executes the algorithm until \c dest is reached. |
544 | 542 |
|
545 | 543 |
///Executes the algorithm until \c dest is reached. |
546 | 544 |
/// |
547 | 545 |
///\pre init() must be called and at least one node should be added |
548 | 546 |
///with addSource() before using this function. |
549 | 547 |
/// |
550 | 548 |
///This method runs the %DFS algorithm from the root node(s) |
551 | 549 |
///in order to |
552 | 550 |
///compute the |
553 | 551 |
///%DFS path to \c dest. The algorithm computes |
554 | 552 |
///- The %DFS path to \c dest. |
555 | 553 |
///- The distance of \c dest from the root(s) in the %DFS tree. |
556 | 554 |
/// |
557 | 555 |
void start(Node dest) |
558 | 556 |
{ |
559 | 557 |
while ( !emptyQueue() && G->target(_stack[_stack_head])!=dest ) |
560 | 558 |
processNextArc(); |
561 | 559 |
} |
562 | 560 |
|
563 | 561 |
///Executes the algorithm until a condition is met. |
564 | 562 |
|
565 | 563 |
///Executes the algorithm until a condition is met. |
566 | 564 |
/// |
567 | 565 |
///\pre init() must be called and at least one node should be added |
568 | 566 |
///with addSource() before using this function. |
569 | 567 |
/// |
570 | 568 |
///\param em must be a bool (or convertible) arc map. The algorithm |
571 | 569 |
///will stop when it reaches an arc \c e with <tt>em[e]</tt> true. |
572 | 570 |
/// |
573 | 571 |
///\return The reached arc \c e with <tt>em[e]</tt> true or |
574 | 572 |
///\c INVALID if no such arc was found. |
575 | 573 |
/// |
576 | 574 |
///\warning Contrary to \ref Bfs and \ref Dijkstra, \c em is an arc map, |
577 | 575 |
///not a node map. |
578 | 576 |
template<class EM> |
579 | 577 |
Arc start(const EM &em) |
580 | 578 |
{ |
581 | 579 |
while ( !emptyQueue() && !em[_stack[_stack_head]] ) |
582 | 580 |
processNextArc(); |
583 | 581 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
584 | 582 |
} |
585 | 583 |
|
586 | 584 |
///Runs %DFS algorithm to visit all nodes in the digraph. |
587 | 585 |
|
588 | 586 |
///This method runs the %DFS algorithm in order to |
589 | 587 |
///compute the |
590 | 588 |
///%DFS path to each node. The algorithm computes |
591 | 589 |
///- The %DFS tree. |
592 | 590 |
///- The distance of each node from the root in the %DFS tree. |
593 | 591 |
/// |
594 | 592 |
///\note d.run() is just a shortcut of the following code. |
595 | 593 |
///\code |
596 | 594 |
/// d.init(); |
597 | 595 |
/// for (NodeIt it(digraph); it != INVALID; ++it) { |
598 | 596 |
/// if (!d.reached(it)) { |
599 | 597 |
/// d.addSource(it); |
600 | 598 |
/// d.start(); |
601 | 599 |
/// } |
602 | 600 |
/// } |
603 | 601 |
///\endcode |
604 | 602 |
void run() { |
605 | 603 |
init(); |
606 | 604 |
for (NodeIt it(*G); it != INVALID; ++it) { |
607 | 605 |
if (!reached(it)) { |
608 | 606 |
addSource(it); |
609 | 607 |
start(); |
610 | 608 |
} |
611 | 609 |
} |
612 | 610 |
} |
613 | 611 |
|
614 | 612 |
///Runs %DFS algorithm from node \c s. |
615 | 613 |
|
616 | 614 |
///This method runs the %DFS algorithm from a root node \c s |
617 | 615 |
///in order to |
618 | 616 |
///compute the |
619 | 617 |
///%DFS path to each node. The algorithm computes |
620 | 618 |
///- The %DFS tree. |
621 | 619 |
///- The distance of each node from the root in the %DFS tree. |
622 | 620 |
/// |
623 | 621 |
///\note d.run(s) is just a shortcut of the following code. |
624 | 622 |
///\code |
625 | 623 |
/// d.init(); |
626 | 624 |
/// d.addSource(s); |
627 | 625 |
/// d.start(); |
628 | 626 |
///\endcode |
629 | 627 |
void run(Node s) { |
630 | 628 |
init(); |
631 | 629 |
addSource(s); |
632 | 630 |
start(); |
633 | 631 |
} |
634 | 632 |
|
635 | 633 |
///Finds the %DFS path between \c s and \c t. |
636 | 634 |
|
637 | 635 |
///Finds the %DFS path between \c s and \c t. |
638 | 636 |
/// |
639 | 637 |
///\return The length of the %DFS s---t path if there exists one, |
640 | 638 |
///0 otherwise. |
641 | 639 |
///\note Apart from the return value, d.run(s,t) is |
642 | 640 |
///just a shortcut of the following code. |
643 | 641 |
///\code |
644 | 642 |
/// d.init(); |
645 | 643 |
/// d.addSource(s); |
646 | 644 |
/// d.start(t); |
647 | 645 |
///\endcode |
648 | 646 |
int run(Node s,Node t) { |
649 | 647 |
init(); |
650 | 648 |
addSource(s); |
651 | 649 |
start(t); |
652 | 650 |
return reached(t)?_stack_head+1:0; |
653 | 651 |
} |
654 | 652 |
|
655 | 653 |
///@} |
656 | 654 |
|
657 | 655 |
///\name Query Functions |
658 | 656 |
///The result of the %DFS algorithm can be obtained using these |
659 | 657 |
///functions.\n |
660 | 658 |
///Before the use of these functions, |
661 | 659 |
///either run() or start() must be called. |
662 | 660 |
|
663 | 661 |
///@{ |
664 | 662 |
|
665 | 663 |
typedef PredMapPath<Digraph, PredMap> Path; |
666 | 664 |
|
667 | 665 |
///Gives back the shortest path. |
668 | 666 |
|
669 | 667 |
///Gives back the shortest path. |
670 | 668 |
///\pre The \c t should be reachable from the source. |
671 | 669 |
Path path(Node t) |
672 | 670 |
{ |
673 | 671 |
return Path(*G, *_pred, t); |
674 | 672 |
} |
675 | 673 |
|
676 | 674 |
///The distance of a node from the root(s). |
677 | 675 |
|
678 | 676 |
///Returns the distance of a node from the root(s). |
679 | 677 |
///\pre \ref run() must be called before using this function. |
680 | 678 |
///\warning If node \c v is unreachable from the root(s) then the return |
681 | 679 |
///value of this funcion is undefined. |
682 | 680 |
int dist(Node v) const { return (*_dist)[v]; } |
683 | 681 |
|
684 | 682 |
///Returns the 'previous arc' of the %DFS tree. |
685 | 683 |
|
686 | 684 |
///For a node \c v it returns the 'previous arc' |
687 | 685 |
///of the %DFS path, |
688 | 686 |
///i.e. it returns the last arc of a %DFS path from the root(s) to \c |
689 | 687 |
///v. It is \ref INVALID |
690 | 688 |
///if \c v is unreachable from the root(s) or \c v is a root. The |
691 | 689 |
///%DFS tree used here is equal to the %DFS tree used in |
692 | 690 |
///\ref predNode(). |
693 | 691 |
///\pre Either \ref run() or \ref start() must be called before using |
694 | 692 |
///this function. |
695 | 693 |
Arc predArc(Node v) const { return (*_pred)[v];} |
696 | 694 |
|
697 | 695 |
///Returns the 'previous node' of the %DFS tree. |
698 | 696 |
|
699 | 697 |
///For a node \c v it returns the 'previous node' |
700 | 698 |
///of the %DFS tree, |
701 | 699 |
///i.e. it returns the last but one node from a %DFS path from the |
702 | 700 |
///root(s) to \c v. |
703 | 701 |
///It is INVALID if \c v is unreachable from the root(s) or |
704 | 702 |
///if \c v itself a root. |
705 | 703 |
///The %DFS tree used here is equal to the %DFS |
706 | 704 |
///tree used in \ref predArc(). |
707 | 705 |
///\pre Either \ref run() or \ref start() must be called before |
708 | 706 |
///using this function. |
709 | 707 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
710 | 708 |
G->source((*_pred)[v]); } |
711 | 709 |
|
712 | 710 |
///Returns a reference to the NodeMap of distances. |
713 | 711 |
|
714 | 712 |
///Returns a reference to the NodeMap of distances. |
715 | 713 |
///\pre Either \ref run() or \ref init() must |
716 | 714 |
///be called before using this function. |
717 | 715 |
const DistMap &distMap() const { return *_dist;} |
718 | 716 |
|
719 | 717 |
///Returns a reference to the %DFS arc-tree map. |
720 | 718 |
|
721 | 719 |
///Returns a reference to the NodeMap of the arcs of the |
722 | 720 |
///%DFS tree. |
723 | 721 |
///\pre Either \ref run() or \ref init() |
724 | 722 |
///must be called before using this function. |
725 | 723 |
const PredMap &predMap() const { return *_pred;} |
726 | 724 |
|
727 | 725 |
///Checks if a node is reachable from the root. |
728 | 726 |
|
729 | 727 |
///Returns \c true if \c v is reachable from the root(s). |
730 | 728 |
///\warning The source nodes are inditated as unreachable. |
731 | 729 |
///\pre Either \ref run() or \ref start() |
732 | 730 |
///must be called before using this function. |
733 | 731 |
/// |
734 | 732 |
bool reached(Node v) { return (*_reached)[v]; } |
735 | 733 |
|
736 | 734 |
///@} |
737 | 735 |
}; |
738 | 736 |
|
739 | 737 |
///Default traits class of Dfs function. |
740 | 738 |
|
741 | 739 |
///Default traits class of Dfs function. |
742 |
///\ |
|
740 |
///\tparam GR Digraph type. |
|
743 | 741 |
template<class GR> |
744 | 742 |
struct DfsWizardDefaultTraits |
745 | 743 |
{ |
746 | 744 |
///The digraph type the algorithm runs on. |
747 | 745 |
typedef GR Digraph; |
748 | 746 |
///\brief The type of the map that stores the last |
749 | 747 |
///arcs of the %DFS paths. |
750 | 748 |
/// |
751 | 749 |
///The type of the map that stores the last |
752 | 750 |
///arcs of the %DFS paths. |
753 | 751 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
754 | 752 |
/// |
755 | 753 |
typedef NullMap<typename Digraph::Node,typename GR::Arc> PredMap; |
756 | 754 |
///Instantiates a PredMap. |
757 | 755 |
|
758 | 756 |
///This function instantiates a \ref PredMap. |
759 | 757 |
///\param g is the digraph, to which we would like to define the PredMap. |
760 | 758 |
///\todo The digraph alone may be insufficient to initialize |
761 | 759 |
#ifdef DOXYGEN |
762 | 760 |
static PredMap *createPredMap(const GR &g) |
763 | 761 |
#else |
764 | 762 |
static PredMap *createPredMap(const GR &) |
765 | 763 |
#endif |
766 | 764 |
{ |
767 | 765 |
return new PredMap(); |
768 | 766 |
} |
769 | 767 |
|
770 | 768 |
///The type of the map that indicates which nodes are processed. |
771 | 769 |
|
772 | 770 |
///The type of the map that indicates which nodes are processed. |
773 | 771 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
774 | 772 |
///\todo named parameter to set this type, function to read and write. |
775 | 773 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
776 | 774 |
///Instantiates a ProcessedMap. |
777 | 775 |
|
778 | 776 |
///This function instantiates a \ref ProcessedMap. |
779 | 777 |
///\param g is the digraph, to which |
780 | 778 |
///we would like to define the \ref ProcessedMap |
781 | 779 |
#ifdef DOXYGEN |
782 | 780 |
static ProcessedMap *createProcessedMap(const GR &g) |
783 | 781 |
#else |
784 | 782 |
static ProcessedMap *createProcessedMap(const GR &) |
785 | 783 |
#endif |
786 | 784 |
{ |
787 | 785 |
return new ProcessedMap(); |
788 | 786 |
} |
789 | 787 |
///The type of the map that indicates which nodes are reached. |
790 | 788 |
|
791 | 789 |
///The type of the map that indicates which nodes are reached. |
792 | 790 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
793 | 791 |
///\todo named parameter to set this type, function to read and write. |
794 | 792 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
795 | 793 |
///Instantiates a ReachedMap. |
796 | 794 |
|
797 | 795 |
///This function instantiates a \ref ReachedMap. |
798 | 796 |
///\param G is the digraph, to which |
799 | 797 |
///we would like to define the \ref ReachedMap. |
800 | 798 |
static ReachedMap *createReachedMap(const GR &G) |
801 | 799 |
{ |
802 | 800 |
return new ReachedMap(G); |
803 | 801 |
} |
804 | 802 |
///The type of the map that stores the dists of the nodes. |
805 | 803 |
|
806 | 804 |
///The type of the map that stores the dists of the nodes. |
807 | 805 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
808 | 806 |
/// |
809 | 807 |
typedef NullMap<typename Digraph::Node,int> DistMap; |
810 | 808 |
///Instantiates a DistMap. |
811 | 809 |
|
812 | 810 |
///This function instantiates a \ref DistMap. |
813 | 811 |
///\param g is the digraph, to which we would like to define the \ref DistMap |
814 | 812 |
#ifdef DOXYGEN |
815 | 813 |
static DistMap *createDistMap(const GR &g) |
816 | 814 |
#else |
817 | 815 |
static DistMap *createDistMap(const GR &) |
818 | 816 |
#endif |
819 | 817 |
{ |
820 | 818 |
return new DistMap(); |
821 | 819 |
} |
822 | 820 |
}; |
823 | 821 |
|
824 | 822 |
/// Default traits used by \ref DfsWizard |
825 | 823 |
|
826 | 824 |
/// To make it easier to use Dfs algorithm |
827 | 825 |
///we have created a wizard class. |
828 | 826 |
/// This \ref DfsWizard class needs default traits, |
829 | 827 |
///as well as the \ref Dfs class. |
830 | 828 |
/// The \ref DfsWizardBase is a class to be the default traits of the |
831 | 829 |
/// \ref DfsWizard class. |
832 | 830 |
template<class GR> |
833 | 831 |
class DfsWizardBase : public DfsWizardDefaultTraits<GR> |
834 | 832 |
{ |
835 | 833 |
|
836 | 834 |
typedef DfsWizardDefaultTraits<GR> Base; |
837 | 835 |
protected: |
838 | 836 |
/// Type of the nodes in the digraph. |
839 | 837 |
typedef typename Base::Digraph::Node Node; |
840 | 838 |
|
841 | 839 |
/// Pointer to the underlying digraph. |
842 | 840 |
void *_g; |
843 | 841 |
///Pointer to the map of reached nodes. |
844 | 842 |
void *_reached; |
845 | 843 |
///Pointer to the map of processed nodes. |
846 | 844 |
void *_processed; |
847 | 845 |
///Pointer to the map of predecessors arcs. |
848 | 846 |
void *_pred; |
849 | 847 |
///Pointer to the map of distances. |
850 | 848 |
void *_dist; |
851 | 849 |
///Pointer to the source node. |
852 | 850 |
Node _source; |
853 | 851 |
|
854 | 852 |
public: |
855 | 853 |
/// Constructor. |
856 | 854 |
|
857 | 855 |
/// This constructor does not require parameters, therefore it initiates |
858 | 856 |
/// all of the attributes to default values (0, INVALID). |
859 | 857 |
DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
860 | 858 |
_dist(0), _source(INVALID) {} |
861 | 859 |
|
862 | 860 |
/// Constructor. |
863 | 861 |
|
864 | 862 |
/// This constructor requires some parameters, |
865 | 863 |
/// listed in the parameters list. |
866 | 864 |
/// Others are initiated to 0. |
867 | 865 |
/// \param g is the initial value of \ref _g |
868 | 866 |
/// \param s is the initial value of \ref _source |
869 | 867 |
DfsWizardBase(const GR &g, Node s=INVALID) : |
870 | 868 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
871 | 869 |
_reached(0), _processed(0), _pred(0), _dist(0), _source(s) {} |
872 | 870 |
|
873 | 871 |
}; |
874 | 872 |
|
875 | 873 |
/// A class to make the usage of the Dfs algorithm easier |
876 | 874 |
|
877 | 875 |
/// This class is created to make it easier to use the Dfs algorithm. |
878 | 876 |
/// It uses the functions and features of the plain \ref Dfs, |
879 | 877 |
/// but it is much simpler to use it. |
880 | 878 |
/// |
881 | 879 |
/// Simplicity means that the way to change the types defined |
882 | 880 |
/// in the traits class is based on functions that returns the new class |
883 | 881 |
/// and not on templatable built-in classes. |
884 | 882 |
/// When using the plain \ref Dfs |
885 | 883 |
/// the new class with the modified type comes from |
886 | 884 |
/// the original class by using the :: |
887 | 885 |
/// operator. In the case of \ref DfsWizard only |
888 | 886 |
/// a function have to be called and it will |
889 | 887 |
/// return the needed class. |
890 | 888 |
/// |
891 | 889 |
/// It does not have own \ref run method. When its \ref run method is called |
892 | 890 |
/// it initiates a plain \ref Dfs object, and calls the \ref Dfs::run |
893 | 891 |
/// method of it. |
894 | 892 |
template<class TR> |
895 | 893 |
class DfsWizard : public TR |
896 | 894 |
{ |
897 | 895 |
typedef TR Base; |
898 | 896 |
|
899 | 897 |
///The type of the underlying digraph. |
900 | 898 |
typedef typename TR::Digraph Digraph; |
901 | 899 |
//\e |
902 | 900 |
typedef typename Digraph::Node Node; |
903 | 901 |
//\e |
904 | 902 |
typedef typename Digraph::NodeIt NodeIt; |
905 | 903 |
//\e |
906 | 904 |
typedef typename Digraph::Arc Arc; |
907 | 905 |
//\e |
908 | 906 |
typedef typename Digraph::OutArcIt OutArcIt; |
909 | 907 |
|
910 | 908 |
///\brief The type of the map that stores |
911 | 909 |
///the reached nodes |
912 | 910 |
typedef typename TR::ReachedMap ReachedMap; |
913 | 911 |
///\brief The type of the map that stores |
914 | 912 |
///the processed nodes |
915 | 913 |
typedef typename TR::ProcessedMap ProcessedMap; |
916 | 914 |
///\brief The type of the map that stores the last |
917 | 915 |
///arcs of the %DFS paths. |
918 | 916 |
typedef typename TR::PredMap PredMap; |
919 | 917 |
///The type of the map that stores the distances of the nodes. |
920 | 918 |
typedef typename TR::DistMap DistMap; |
921 | 919 |
|
922 | 920 |
public: |
923 | 921 |
/// Constructor. |
924 | 922 |
DfsWizard() : TR() {} |
925 | 923 |
|
926 | 924 |
/// Constructor that requires parameters. |
927 | 925 |
|
928 | 926 |
/// Constructor that requires parameters. |
929 | 927 |
/// These parameters will be the default values for the traits class. |
930 | 928 |
DfsWizard(const Digraph &g, Node s=INVALID) : |
931 | 929 |
TR(g,s) {} |
932 | 930 |
|
933 | 931 |
///Copy constructor |
934 | 932 |
DfsWizard(const TR &b) : TR(b) {} |
935 | 933 |
|
936 | 934 |
~DfsWizard() {} |
937 | 935 |
|
938 | 936 |
///Runs Dfs algorithm from a given node. |
939 | 937 |
|
940 | 938 |
///Runs Dfs algorithm from a given node. |
941 | 939 |
///The node can be given by the \ref source function. |
942 | 940 |
void run() |
943 | 941 |
{ |
944 | 942 |
if(Base::_source==INVALID) throw UninitializedParameter(); |
945 | 943 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
946 | 944 |
if(Base::_reached) |
947 | 945 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
948 | 946 |
if(Base::_processed) |
949 | 947 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
950 | 948 |
if(Base::_pred) |
951 | 949 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
952 | 950 |
if(Base::_dist) |
953 | 951 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
954 | 952 |
alg.run(Base::_source); |
955 | 953 |
} |
956 | 954 |
|
957 | 955 |
///Runs Dfs algorithm from the given node. |
958 | 956 |
|
959 | 957 |
///Runs Dfs algorithm from the given node. |
960 | 958 |
///\param s is the given source. |
961 | 959 |
void run(Node s) |
962 | 960 |
{ |
963 | 961 |
Base::_source=s; |
964 | 962 |
run(); |
965 | 963 |
} |
966 | 964 |
|
967 | 965 |
template<class T> |
968 | 966 |
struct DefPredMapBase : public Base { |
969 | 967 |
typedef T PredMap; |
970 | 968 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
971 | 969 |
DefPredMapBase(const TR &b) : TR(b) {} |
972 | 970 |
}; |
973 | 971 |
|
974 | 972 |
///\brief \ref named-templ-param "Named parameter" |
975 | 973 |
///function for setting PredMap type |
976 | 974 |
/// |
977 | 975 |
/// \ref named-templ-param "Named parameter" |
978 | 976 |
///function for setting PredMap type |
979 | 977 |
/// |
980 | 978 |
template<class T> |
981 | 979 |
DfsWizard<DefPredMapBase<T> > predMap(const T &t) |
982 | 980 |
{ |
983 | 981 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
984 | 982 |
return DfsWizard<DefPredMapBase<T> >(*this); |
985 | 983 |
} |
986 | 984 |
|
987 | 985 |
|
988 | 986 |
template<class T> |
989 | 987 |
struct DefReachedMapBase : public Base { |
990 | 988 |
typedef T ReachedMap; |
991 | 989 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
992 | 990 |
DefReachedMapBase(const TR &b) : TR(b) {} |
993 | 991 |
}; |
994 | 992 |
|
995 | 993 |
///\brief \ref named-templ-param "Named parameter" |
996 | 994 |
///function for setting ReachedMap |
997 | 995 |
/// |
998 | 996 |
/// \ref named-templ-param "Named parameter" |
999 | 997 |
///function for setting ReachedMap |
1000 | 998 |
/// |
1001 | 999 |
template<class T> |
1002 | 1000 |
DfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) |
1003 | 1001 |
{ |
1004 | 1002 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1005 | 1003 |
return DfsWizard<DefReachedMapBase<T> >(*this); |
1006 | 1004 |
} |
1007 | 1005 |
|
1008 | 1006 |
|
1009 | 1007 |
template<class T> |
1010 | 1008 |
struct DefProcessedMapBase : public Base { |
1011 | 1009 |
typedef T ProcessedMap; |
1012 | 1010 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1013 | 1011 |
DefProcessedMapBase(const TR &b) : TR(b) {} |
1014 | 1012 |
}; |
1015 | 1013 |
|
1016 | 1014 |
///\brief \ref named-templ-param "Named parameter" |
1017 | 1015 |
///function for setting ProcessedMap |
1018 | 1016 |
/// |
1019 | 1017 |
/// \ref named-templ-param "Named parameter" |
1020 | 1018 |
///function for setting ProcessedMap |
1021 | 1019 |
/// |
1022 | 1020 |
template<class T> |
1023 | 1021 |
DfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) |
1024 | 1022 |
{ |
1025 | 1023 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1026 | 1024 |
return DfsWizard<DefProcessedMapBase<T> >(*this); |
1027 | 1025 |
} |
1028 | 1026 |
|
1029 | 1027 |
template<class T> |
1030 | 1028 |
struct DefDistMapBase : public Base { |
1031 | 1029 |
typedef T DistMap; |
1032 | 1030 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1033 | 1031 |
DefDistMapBase(const TR &b) : TR(b) {} |
1034 | 1032 |
}; |
1035 | 1033 |
|
1036 | 1034 |
///\brief \ref named-templ-param "Named parameter" |
1037 | 1035 |
///function for setting DistMap type |
1038 | 1036 |
/// |
1039 | 1037 |
/// \ref named-templ-param "Named parameter" |
1040 | 1038 |
///function for setting DistMap type |
1041 | 1039 |
/// |
1042 | 1040 |
template<class T> |
1043 | 1041 |
DfsWizard<DefDistMapBase<T> > distMap(const T &t) |
1044 | 1042 |
{ |
1045 | 1043 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1046 | 1044 |
return DfsWizard<DefDistMapBase<T> >(*this); |
1047 | 1045 |
} |
1048 | 1046 |
|
1049 | 1047 |
/// Sets the source node, from which the Dfs algorithm runs. |
1050 | 1048 |
|
1051 | 1049 |
/// Sets the source node, from which the Dfs algorithm runs. |
1052 | 1050 |
/// \param s is the source node. |
1053 | 1051 |
DfsWizard<TR> &source(Node s) |
1054 | 1052 |
{ |
1055 | 1053 |
Base::_source=s; |
1056 | 1054 |
return *this; |
1057 | 1055 |
} |
1058 | 1056 |
|
1059 | 1057 |
}; |
1060 | 1058 |
|
1061 | 1059 |
///Function type interface for Dfs algorithm. |
1062 | 1060 |
|
1063 | 1061 |
///\ingroup search |
1064 | 1062 |
///Function type interface for Dfs algorithm. |
1065 | 1063 |
/// |
1066 | 1064 |
///This function also has several |
1067 | 1065 |
///\ref named-templ-func-param "named parameters", |
1068 | 1066 |
///they are declared as the members of class \ref DfsWizard. |
1069 | 1067 |
///The following |
1070 | 1068 |
///example shows how to use these parameters. |
1071 | 1069 |
///\code |
1072 | 1070 |
/// dfs(g,source).predMap(preds).run(); |
1073 | 1071 |
///\endcode |
1074 | 1072 |
///\warning Don't forget to put the \ref DfsWizard::run() "run()" |
1075 | 1073 |
///to the end of the parameter list. |
1076 | 1074 |
///\sa DfsWizard |
1077 | 1075 |
///\sa Dfs |
1078 | 1076 |
template<class GR> |
1079 | 1077 |
DfsWizard<DfsWizardBase<GR> > |
1080 | 1078 |
dfs(const GR &g,typename GR::Node s=INVALID) |
1081 | 1079 |
{ |
1082 | 1080 |
return DfsWizard<DfsWizardBase<GR> >(g,s); |
1083 | 1081 |
} |
1084 | 1082 |
|
1085 | 1083 |
#ifdef DOXYGEN |
1086 | 1084 |
/// \brief Visitor class for dfs. |
1087 | 1085 |
/// |
1088 | 1086 |
/// It gives a simple interface for a functional interface for dfs |
1089 | 1087 |
/// traversal. The traversal on a linear data structure. |
1090 | 1088 |
template <typename _Digraph> |
1091 | 1089 |
struct DfsVisitor { |
1092 | 1090 |
typedef _Digraph Digraph; |
1093 | 1091 |
typedef typename Digraph::Arc Arc; |
1094 | 1092 |
typedef typename Digraph::Node Node; |
1095 | 1093 |
/// \brief Called when the arc reach a node. |
1096 | 1094 |
/// |
1097 | 1095 |
/// It is called when the dfs find an arc which target is not |
1098 | 1096 |
/// reached yet. |
1099 | 1097 |
void discover(const Arc& arc) {} |
1100 | 1098 |
/// \brief Called when the node reached first time. |
1101 | 1099 |
/// |
1102 | 1100 |
/// It is Called when the node reached first time. |
1103 | 1101 |
void reach(const Node& node) {} |
1104 | 1102 |
/// \brief Called when we step back on an arc. |
1105 | 1103 |
/// |
1106 | 1104 |
/// It is called when the dfs should step back on the arc. |
1107 | 1105 |
void backtrack(const Arc& arc) {} |
1108 | 1106 |
/// \brief Called when we step back from the node. |
1109 | 1107 |
/// |
1110 | 1108 |
/// It is called when we step back from the node. |
1111 | 1109 |
void leave(const Node& node) {} |
1112 | 1110 |
/// \brief Called when the arc examined but target of the arc |
1113 | 1111 |
/// already discovered. |
1114 | 1112 |
/// |
1115 | 1113 |
/// It called when the arc examined but the target of the arc |
1116 | 1114 |
/// already discovered. |
1117 | 1115 |
void examine(const Arc& arc) {} |
1118 | 1116 |
/// \brief Called for the source node of the dfs. |
1119 | 1117 |
/// |
1120 | 1118 |
/// It is called for the source node of the dfs. |
1121 | 1119 |
void start(const Node& node) {} |
1122 | 1120 |
/// \brief Called when we leave the source node of the dfs. |
1123 | 1121 |
/// |
1124 | 1122 |
/// It is called when we leave the source node of the dfs. |
1125 | 1123 |
void stop(const Node& node) {} |
1126 | 1124 |
|
1127 | 1125 |
}; |
1128 | 1126 |
#else |
1129 | 1127 |
template <typename _Digraph> |
1130 | 1128 |
struct DfsVisitor { |
1131 | 1129 |
typedef _Digraph Digraph; |
1132 | 1130 |
typedef typename Digraph::Arc Arc; |
1133 | 1131 |
typedef typename Digraph::Node Node; |
1134 | 1132 |
void discover(const Arc&) {} |
1135 | 1133 |
void reach(const Node&) {} |
1136 | 1134 |
void backtrack(const Arc&) {} |
1137 | 1135 |
void leave(const Node&) {} |
1138 | 1136 |
void examine(const Arc&) {} |
1139 | 1137 |
void start(const Node&) {} |
1140 | 1138 |
void stop(const Node&) {} |
1141 | 1139 |
|
1142 | 1140 |
template <typename _Visitor> |
1143 | 1141 |
struct Constraints { |
1144 | 1142 |
void constraints() { |
1145 | 1143 |
Arc arc; |
1146 | 1144 |
Node node; |
1147 | 1145 |
visitor.discover(arc); |
1148 | 1146 |
visitor.reach(node); |
1149 | 1147 |
visitor.backtrack(arc); |
1150 | 1148 |
visitor.leave(node); |
1151 | 1149 |
visitor.examine(arc); |
1152 | 1150 |
visitor.start(node); |
1153 | 1151 |
visitor.stop(arc); |
1154 | 1152 |
} |
1155 | 1153 |
_Visitor& visitor; |
1156 | 1154 |
}; |
1157 | 1155 |
}; |
1158 | 1156 |
#endif |
1159 | 1157 |
|
1160 | 1158 |
/// \brief Default traits class of DfsVisit class. |
1161 | 1159 |
/// |
1162 | 1160 |
/// Default traits class of DfsVisit class. |
1163 |
/// \ |
|
1161 |
/// \tparam _Digraph Digraph type. |
|
1164 | 1162 |
template<class _Digraph> |
1165 | 1163 |
struct DfsVisitDefaultTraits { |
1166 | 1164 |
|
1167 | 1165 |
/// \brief The digraph type the algorithm runs on. |
1168 | 1166 |
typedef _Digraph Digraph; |
1169 | 1167 |
|
1170 | 1168 |
/// \brief The type of the map that indicates which nodes are reached. |
1171 | 1169 |
/// |
1172 | 1170 |
/// The type of the map that indicates which nodes are reached. |
1173 | 1171 |
/// It must meet the \ref concepts::WriteMap "WriteMap" concept. |
1174 | 1172 |
/// \todo named parameter to set this type, function to read and write. |
1175 | 1173 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1176 | 1174 |
|
1177 | 1175 |
/// \brief Instantiates a ReachedMap. |
1178 | 1176 |
/// |
1179 | 1177 |
/// This function instantiates a \ref ReachedMap. |
1180 | 1178 |
/// \param digraph is the digraph, to which |
1181 | 1179 |
/// we would like to define the \ref ReachedMap. |
1182 | 1180 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1183 | 1181 |
return new ReachedMap(digraph); |
1184 | 1182 |
} |
1185 | 1183 |
|
1186 | 1184 |
}; |
1187 | 1185 |
|
1188 | 1186 |
/// %DFS Visit algorithm class. |
1189 | 1187 |
|
1190 | 1188 |
/// \ingroup search |
1191 | 1189 |
/// This class provides an efficient implementation of the %DFS algorithm |
1192 | 1190 |
/// with visitor interface. |
1193 | 1191 |
/// |
1194 | 1192 |
/// The %DfsVisit class provides an alternative interface to the Dfs |
1195 | 1193 |
/// class. It works with callback mechanism, the DfsVisit object calls |
1196 | 1194 |
/// on every dfs event the \c Visitor class member functions. |
1197 | 1195 |
/// |
1198 |
/// \ |
|
1196 |
/// \tparam _Digraph The digraph type the algorithm runs on. The default value is |
|
1199 | 1197 |
/// \ref ListDigraph. The value of _Digraph is not used directly by Dfs, it |
1200 | 1198 |
/// is only passed to \ref DfsDefaultTraits. |
1201 |
/// \ |
|
1199 |
/// \tparam _Visitor The Visitor object for the algorithm. The |
|
1202 | 1200 |
/// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty Visitor which |
1203 | 1201 |
/// does not observe the Dfs events. If you want to observe the dfs |
1204 | 1202 |
/// events you should implement your own Visitor class. |
1205 |
/// \ |
|
1203 |
/// \tparam _Traits Traits class to set various data types used by the |
|
1206 | 1204 |
/// algorithm. The default traits class is |
1207 | 1205 |
/// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Digraph>". |
1208 | 1206 |
/// See \ref DfsVisitDefaultTraits for the documentation of |
1209 | 1207 |
/// a Dfs visit traits class. |
1210 | 1208 |
/// |
1211 | 1209 |
/// \author Jacint Szabo, Alpar Juttner and Balazs Dezso |
1212 | 1210 |
#ifdef DOXYGEN |
1213 | 1211 |
template <typename _Digraph, typename _Visitor, typename _Traits> |
1214 | 1212 |
#else |
1215 | 1213 |
template <typename _Digraph = ListDigraph, |
1216 | 1214 |
typename _Visitor = DfsVisitor<_Digraph>, |
1217 | 1215 |
typename _Traits = DfsDefaultTraits<_Digraph> > |
1218 | 1216 |
#endif |
1219 | 1217 |
class DfsVisit { |
1220 | 1218 |
public: |
1221 | 1219 |
|
1222 | 1220 |
/// \brief \ref Exception for uninitialized parameters. |
1223 | 1221 |
/// |
1224 | 1222 |
/// This error represents problems in the initialization |
1225 | 1223 |
/// of the parameters of the algorithms. |
1226 | 1224 |
class UninitializedParameter : public lemon::UninitializedParameter { |
1227 | 1225 |
public: |
1228 | 1226 |
virtual const char* what() const throw() |
1229 | 1227 |
{ |
1230 | 1228 |
return "lemon::DfsVisit::UninitializedParameter"; |
1231 | 1229 |
} |
1232 | 1230 |
}; |
1233 | 1231 |
|
1234 | 1232 |
typedef _Traits Traits; |
1235 | 1233 |
|
1236 | 1234 |
typedef typename Traits::Digraph Digraph; |
1237 | 1235 |
|
1238 | 1236 |
typedef _Visitor Visitor; |
1239 | 1237 |
|
1240 | 1238 |
///The type of the map indicating which nodes are reached. |
1241 | 1239 |
typedef typename Traits::ReachedMap ReachedMap; |
1242 | 1240 |
|
1243 | 1241 |
private: |
1244 | 1242 |
|
1245 | 1243 |
typedef typename Digraph::Node Node; |
1246 | 1244 |
typedef typename Digraph::NodeIt NodeIt; |
1247 | 1245 |
typedef typename Digraph::Arc Arc; |
1248 | 1246 |
typedef typename Digraph::OutArcIt OutArcIt; |
1249 | 1247 |
|
1250 | 1248 |
/// Pointer to the underlying digraph. |
1251 | 1249 |
const Digraph *_digraph; |
1252 | 1250 |
/// Pointer to the visitor object. |
1253 | 1251 |
Visitor *_visitor; |
1254 | 1252 |
///Pointer to the map of reached status of the nodes. |
1255 | 1253 |
ReachedMap *_reached; |
1256 | 1254 |
///Indicates if \ref _reached is locally allocated (\c true) or not. |
1257 | 1255 |
bool local_reached; |
1258 | 1256 |
|
1259 | 1257 |
std::vector<typename Digraph::Arc> _stack; |
1260 | 1258 |
int _stack_head; |
1261 | 1259 |
|
1262 | 1260 |
/// \brief Creates the maps if necessary. |
1263 | 1261 |
/// |
1264 | 1262 |
/// Creates the maps if necessary. |
1265 | 1263 |
void create_maps() { |
1266 | 1264 |
if(!_reached) { |
1267 | 1265 |
local_reached = true; |
1268 | 1266 |
_reached = Traits::createReachedMap(*_digraph); |
1269 | 1267 |
} |
1270 | 1268 |
} |
1271 | 1269 |
|
1272 | 1270 |
protected: |
1273 | 1271 |
|
1274 | 1272 |
DfsVisit() {} |
1275 | 1273 |
|
1276 | 1274 |
public: |
1277 | 1275 |
|
1278 | 1276 |
typedef DfsVisit Create; |
1279 | 1277 |
|
1280 | 1278 |
/// \name Named template parameters |
1281 | 1279 |
|
1282 | 1280 |
///@{ |
1283 | 1281 |
template <class T> |
1284 | 1282 |
struct DefReachedMapTraits : public Traits { |
1285 | 1283 |
typedef T ReachedMap; |
1286 | 1284 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1287 | 1285 |
throw UninitializedParameter(); |
1288 | 1286 |
} |
1289 | 1287 |
}; |
1290 | 1288 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1291 | 1289 |
/// ReachedMap type |
1292 | 1290 |
/// |
1293 | 1291 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type |
1294 | 1292 |
template <class T> |
1295 | 1293 |
struct DefReachedMap : public DfsVisit< Digraph, Visitor, |
1296 | 1294 |
DefReachedMapTraits<T> > { |
1297 | 1295 |
typedef DfsVisit< Digraph, Visitor, DefReachedMapTraits<T> > Create; |
1298 | 1296 |
}; |
1299 | 1297 |
///@} |
1300 | 1298 |
|
1301 | 1299 |
public: |
1302 | 1300 |
|
1303 | 1301 |
/// \brief Constructor. |
1304 | 1302 |
/// |
1305 | 1303 |
/// Constructor. |
1306 | 1304 |
/// |
1307 | 1305 |
/// \param digraph the digraph the algorithm will run on. |
1308 | 1306 |
/// \param visitor The visitor of the algorithm. |
1309 | 1307 |
/// |
1310 | 1308 |
DfsVisit(const Digraph& digraph, Visitor& visitor) |
1311 | 1309 |
: _digraph(&digraph), _visitor(&visitor), |
1312 | 1310 |
_reached(0), local_reached(false) {} |
1313 | 1311 |
|
1314 | 1312 |
/// \brief Destructor. |
1315 | 1313 |
/// |
1316 | 1314 |
/// Destructor. |
1317 | 1315 |
~DfsVisit() { |
1318 | 1316 |
if(local_reached) delete _reached; |
1319 | 1317 |
} |
1320 | 1318 |
|
1321 | 1319 |
/// \brief Sets the map indicating if a node is reached. |
1322 | 1320 |
/// |
1323 | 1321 |
/// Sets the map indicating if a node is reached. |
1324 | 1322 |
/// If you don't use this function before calling \ref run(), |
1325 | 1323 |
/// it will allocate one. The destuctor deallocates this |
1326 | 1324 |
/// automatically allocated map, of course. |
1327 | 1325 |
/// \return <tt> (*this) </tt> |
1328 | 1326 |
DfsVisit &reachedMap(ReachedMap &m) { |
1329 | 1327 |
if(local_reached) { |
1330 | 1328 |
delete _reached; |
1331 | 1329 |
local_reached=false; |
1332 | 1330 |
} |
1333 | 1331 |
_reached = &m; |
1334 | 1332 |
return *this; |
1335 | 1333 |
} |
1336 | 1334 |
|
1337 | 1335 |
public: |
1338 | 1336 |
/// \name Execution control |
1339 | 1337 |
/// The simplest way to execute the algorithm is to use |
1340 | 1338 |
/// one of the member functions called \c run(...). |
1341 | 1339 |
/// \n |
1342 | 1340 |
/// If you need more control on the execution, |
1343 | 1341 |
/// first you must call \ref init(), then you can adda source node |
1344 | 1342 |
/// with \ref addSource(). |
1345 | 1343 |
/// Finally \ref start() will perform the actual path |
1346 | 1344 |
/// computation. |
1347 | 1345 |
|
1348 | 1346 |
/// @{ |
1349 | 1347 |
/// \brief Initializes the internal data structures. |
1350 | 1348 |
/// |
1351 | 1349 |
/// Initializes the internal data structures. |
1352 | 1350 |
/// |
1353 | 1351 |
void init() { |
1354 | 1352 |
create_maps(); |
1355 | 1353 |
_stack.resize(countNodes(*_digraph)); |
1356 | 1354 |
_stack_head = -1; |
1357 | 1355 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1358 | 1356 |
_reached->set(u, false); |
1359 | 1357 |
} |
1360 | 1358 |
} |
1361 | 1359 |
|
1362 | 1360 |
/// \brief Adds a new source node. |
1363 | 1361 |
/// |
1364 | 1362 |
/// Adds a new source node to the set of nodes to be processed. |
1365 | 1363 |
void addSource(Node s) { |
1366 | 1364 |
if(!(*_reached)[s]) { |
1367 | 1365 |
_reached->set(s,true); |
1368 | 1366 |
_visitor->start(s); |
1369 | 1367 |
_visitor->reach(s); |
1370 | 1368 |
Arc e; |
1371 | 1369 |
_digraph->firstOut(e, s); |
1372 | 1370 |
if (e != INVALID) { |
1373 | 1371 |
_stack[++_stack_head] = e; |
1374 | 1372 |
} else { |
1375 | 1373 |
_visitor->leave(s); |
1376 | 1374 |
} |
1377 | 1375 |
} |
1378 | 1376 |
} |
1379 | 1377 |
|
1380 | 1378 |
/// \brief Processes the next arc. |
1381 | 1379 |
/// |
1382 | 1380 |
/// Processes the next arc. |
1383 | 1381 |
/// |
1384 | 1382 |
/// \return The processed arc. |
1385 | 1383 |
/// |
1386 | 1384 |
/// \pre The stack must not be empty! |
1387 | 1385 |
Arc processNextArc() { |
1388 | 1386 |
Arc e = _stack[_stack_head]; |
1389 | 1387 |
Node m = _digraph->target(e); |
1390 | 1388 |
if(!(*_reached)[m]) { |
1391 | 1389 |
_visitor->discover(e); |
1392 | 1390 |
_visitor->reach(m); |
1393 | 1391 |
_reached->set(m, true); |
1394 | 1392 |
_digraph->firstOut(_stack[++_stack_head], m); |
1395 | 1393 |
} else { |
1396 | 1394 |
_visitor->examine(e); |
1397 | 1395 |
m = _digraph->source(e); |
1398 | 1396 |
_digraph->nextOut(_stack[_stack_head]); |
1399 | 1397 |
} |
1400 | 1398 |
while (_stack_head>=0 && _stack[_stack_head] == INVALID) { |
1401 | 1399 |
_visitor->leave(m); |
1402 | 1400 |
--_stack_head; |
1403 | 1401 |
if (_stack_head >= 0) { |
1404 | 1402 |
_visitor->backtrack(_stack[_stack_head]); |
1405 | 1403 |
m = _digraph->source(_stack[_stack_head]); |
1406 | 1404 |
_digraph->nextOut(_stack[_stack_head]); |
1407 | 1405 |
} else { |
1408 | 1406 |
_visitor->stop(m); |
1409 | 1407 |
} |
1410 | 1408 |
} |
1411 | 1409 |
return e; |
1412 | 1410 |
} |
1413 | 1411 |
|
1414 | 1412 |
/// \brief Next arc to be processed. |
1415 | 1413 |
/// |
1416 | 1414 |
/// Next arc to be processed. |
1417 | 1415 |
/// |
1418 | 1416 |
/// \return The next arc to be processed or INVALID if the stack is |
1419 | 1417 |
/// empty. |
1420 | 1418 |
Arc nextArc() { |
1421 | 1419 |
return _stack_head >= 0 ? _stack[_stack_head] : INVALID; |
1422 | 1420 |
} |
1423 | 1421 |
|
1424 | 1422 |
/// \brief Returns \c false if there are nodes |
1425 | 1423 |
/// to be processed in the queue |
1426 | 1424 |
/// |
1427 | 1425 |
/// Returns \c false if there are nodes |
1428 | 1426 |
/// to be processed in the queue |
1429 | 1427 |
bool emptyQueue() { return _stack_head < 0; } |
1430 | 1428 |
|
1431 | 1429 |
/// \brief Returns the number of the nodes to be processed. |
1432 | 1430 |
/// |
1433 | 1431 |
/// Returns the number of the nodes to be processed in the queue. |
1434 | 1432 |
int queueSize() { return _stack_head + 1; } |
1435 | 1433 |
|
1436 | 1434 |
/// \brief Executes the algorithm. |
1437 | 1435 |
/// |
1438 | 1436 |
/// Executes the algorithm. |
1439 | 1437 |
/// |
1440 | 1438 |
/// \pre init() must be called and at least one node should be added |
1441 | 1439 |
/// with addSource() before using this function. |
1442 | 1440 |
void start() { |
1443 | 1441 |
while ( !emptyQueue() ) processNextArc(); |
1444 | 1442 |
} |
1445 | 1443 |
|
1446 | 1444 |
/// \brief Executes the algorithm until \c dest is reached. |
1447 | 1445 |
/// |
1448 | 1446 |
/// Executes the algorithm until \c dest is reached. |
1449 | 1447 |
/// |
1450 | 1448 |
/// \pre init() must be called and at least one node should be added |
1451 | 1449 |
/// with addSource() before using this function. |
1452 | 1450 |
void start(Node dest) { |
1453 | 1451 |
while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != dest ) |
1454 | 1452 |
processNextArc(); |
1455 | 1453 |
} |
1456 | 1454 |
|
1457 | 1455 |
/// \brief Executes the algorithm until a condition is met. |
1458 | 1456 |
/// |
1459 | 1457 |
/// Executes the algorithm until a condition is met. |
1460 | 1458 |
/// |
1461 | 1459 |
/// \pre init() must be called and at least one node should be added |
1462 | 1460 |
/// with addSource() before using this function. |
1463 | 1461 |
/// |
1464 | 1462 |
/// \param em must be a bool (or convertible) arc map. The algorithm |
1465 | 1463 |
/// will stop when it reaches an arc \c e with <tt>em[e]</tt> true. |
1466 | 1464 |
/// |
1467 | 1465 |
///\return The reached arc \c e with <tt>em[e]</tt> true or |
1468 | 1466 |
///\c INVALID if no such arc was found. |
1469 | 1467 |
/// |
1470 | 1468 |
/// \warning Contrary to \ref Bfs and \ref Dijkstra, \c em is an arc map, |
1471 | 1469 |
/// not a node map. |
1472 | 1470 |
template <typename EM> |
1473 | 1471 |
Arc start(const EM &em) { |
1474 | 1472 |
while ( !emptyQueue() && !em[_stack[_stack_head]] ) |
1475 | 1473 |
processNextArc(); |
1476 | 1474 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
1477 | 1475 |
} |
1478 | 1476 |
|
1479 | 1477 |
/// \brief Runs %DFSVisit algorithm from node \c s. |
1480 | 1478 |
/// |
1481 | 1479 |
/// This method runs the %DFS algorithm from a root node \c s. |
1482 | 1480 |
/// \note d.run(s) is just a shortcut of the following code. |
1483 | 1481 |
///\code |
1484 | 1482 |
/// d.init(); |
1485 | 1483 |
/// d.addSource(s); |
1486 | 1484 |
/// d.start(); |
1487 | 1485 |
///\endcode |
1488 | 1486 |
void run(Node s) { |
1489 | 1487 |
init(); |
1490 | 1488 |
addSource(s); |
1491 | 1489 |
start(); |
1492 | 1490 |
} |
1493 | 1491 |
|
1494 | 1492 |
/// \brief Runs %DFSVisit algorithm to visit all nodes in the digraph. |
1495 | 1493 |
|
1496 | 1494 |
/// This method runs the %DFS algorithm in order to |
1497 | 1495 |
/// compute the %DFS path to each node. The algorithm computes |
1498 | 1496 |
/// - The %DFS tree. |
1499 | 1497 |
/// - The distance of each node from the root in the %DFS tree. |
1500 | 1498 |
/// |
1501 | 1499 |
///\note d.run() is just a shortcut of the following code. |
1502 | 1500 |
///\code |
1503 | 1501 |
/// d.init(); |
1504 | 1502 |
/// for (NodeIt it(digraph); it != INVALID; ++it) { |
1505 | 1503 |
/// if (!d.reached(it)) { |
1506 | 1504 |
/// d.addSource(it); |
1507 | 1505 |
/// d.start(); |
1508 | 1506 |
/// } |
1509 | 1507 |
/// } |
1510 | 1508 |
///\endcode |
1511 | 1509 |
void run() { |
1512 | 1510 |
init(); |
1513 | 1511 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1514 | 1512 |
if (!reached(it)) { |
1515 | 1513 |
addSource(it); |
1516 | 1514 |
start(); |
1517 | 1515 |
} |
1518 | 1516 |
} |
1519 | 1517 |
} |
1520 | 1518 |
///@} |
1521 | 1519 |
|
1522 | 1520 |
/// \name Query Functions |
1523 | 1521 |
/// The result of the %DFS algorithm can be obtained using these |
1524 | 1522 |
/// functions.\n |
1525 | 1523 |
/// Before the use of these functions, |
1526 | 1524 |
/// either run() or start() must be called. |
1527 | 1525 |
///@{ |
1528 | 1526 |
/// \brief Checks if a node is reachable from the root. |
1529 | 1527 |
/// |
1530 | 1528 |
/// Returns \c true if \c v is reachable from the root(s). |
1531 | 1529 |
/// \warning The source nodes are inditated as unreachable. |
1532 | 1530 |
/// \pre Either \ref run() or \ref start() |
1533 | 1531 |
/// must be called before using this function. |
1534 | 1532 |
/// |
1535 | 1533 |
bool reached(Node v) { return (*_reached)[v]; } |
1536 | 1534 |
///@} |
1537 | 1535 |
}; |
1538 | 1536 |
|
1539 | 1537 |
|
1540 | 1538 |
} //END OF NAMESPACE LEMON |
1541 | 1539 |
|
1542 | 1540 |
#endif |
1543 | 1541 |
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_DIJKSTRA_H |
20 | 20 |
#define LEMON_DIJKSTRA_H |
21 | 21 |
|
22 | 22 |
///\ingroup shortest_path |
23 | 23 |
///\file |
24 | 24 |
///\brief Dijkstra algorithm. |
25 | 25 |
/// |
26 | 26 |
|
27 | 27 |
#include <lemon/list_digraph.h> |
28 | 28 |
#include <lemon/bin_heap.h> |
29 | 29 |
#include <lemon/bits/path_dump.h> |
30 | 30 |
#include <lemon/bits/invalid.h> |
31 | 31 |
#include <lemon/error.h> |
32 | 32 |
#include <lemon/maps.h> |
33 | 33 |
|
34 | 34 |
|
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
/// \brief Default OperationTraits for the Dijkstra algorithm class. |
38 | 38 |
/// |
39 | 39 |
/// It defines all computational operations and constants which are |
40 | 40 |
/// used in the Dijkstra algorithm. |
41 | 41 |
template <typename Value> |
42 | 42 |
struct DijkstraDefaultOperationTraits { |
43 | 43 |
/// \brief Gives back the zero value of the type. |
44 | 44 |
static Value zero() { |
45 | 45 |
return static_cast<Value>(0); |
46 | 46 |
} |
47 | 47 |
/// \brief Gives back the sum of the given two elements. |
48 | 48 |
static Value plus(const Value& left, const Value& right) { |
49 | 49 |
return left + right; |
50 | 50 |
} |
51 | 51 |
/// \brief Gives back true only if the first value less than the second. |
52 | 52 |
static bool less(const Value& left, const Value& right) { |
53 | 53 |
return left < right; |
54 | 54 |
} |
55 | 55 |
}; |
56 | 56 |
|
57 | 57 |
/// \brief Widest path OperationTraits for the Dijkstra algorithm class. |
58 | 58 |
/// |
59 | 59 |
/// It defines all computational operations and constants which are |
60 | 60 |
/// used in the Dijkstra algorithm for widest path computation. |
61 | 61 |
template <typename Value> |
62 | 62 |
struct DijkstraWidestPathOperationTraits { |
63 | 63 |
/// \brief Gives back the maximum value of the type. |
64 | 64 |
static Value zero() { |
65 | 65 |
return std::numeric_limits<Value>::max(); |
66 | 66 |
} |
67 | 67 |
/// \brief Gives back the minimum of the given two elements. |
68 | 68 |
static Value plus(const Value& left, const Value& right) { |
69 | 69 |
return std::min(left, right); |
70 | 70 |
} |
71 | 71 |
/// \brief Gives back true only if the first value less than the second. |
72 | 72 |
static bool less(const Value& left, const Value& right) { |
73 | 73 |
return left < right; |
74 | 74 |
} |
75 | 75 |
}; |
76 | 76 |
|
77 | 77 |
///Default traits class of Dijkstra class. |
78 | 78 |
|
79 | 79 |
///Default traits class of Dijkstra class. |
80 |
///\param GR Digraph type. |
|
81 |
///\param LM Type of length map. |
|
80 |
///\tparam GR Digraph type. |
|
81 |
///\tparam LM Type of length map. |
|
82 | 82 |
template<class GR, class LM> |
83 | 83 |
struct DijkstraDefaultTraits |
84 | 84 |
{ |
85 | 85 |
///The digraph type the algorithm runs on. |
86 | 86 |
typedef GR Digraph; |
87 | 87 |
///The type of the map that stores the arc lengths. |
88 | 88 |
|
89 | 89 |
///The type of the map that stores the arc lengths. |
90 | 90 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
91 | 91 |
typedef LM LengthMap; |
92 | 92 |
//The type of the length of the arcs. |
93 | 93 |
typedef typename LM::Value Value; |
94 | 94 |
/// Operation traits for Dijkstra algorithm. |
95 | 95 |
|
96 | 96 |
/// It defines the used operation by the algorithm. |
97 | 97 |
/// \see DijkstraDefaultOperationTraits |
98 | 98 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
99 | 99 |
/// The cross reference type used by heap. |
100 | 100 |
|
101 | 101 |
|
102 | 102 |
/// The cross reference type used by heap. |
103 | 103 |
/// Usually it is \c Digraph::NodeMap<int>. |
104 | 104 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
105 | 105 |
///Instantiates a HeapCrossRef. |
106 | 106 |
|
107 | 107 |
///This function instantiates a \c HeapCrossRef. |
108 | 108 |
/// \param G is the digraph, to which we would like to define the |
109 | 109 |
/// HeapCrossRef. |
110 | 110 |
static HeapCrossRef *createHeapCrossRef(const GR &G) |
111 | 111 |
{ |
112 | 112 |
return new HeapCrossRef(G); |
113 | 113 |
} |
114 | 114 |
|
115 | 115 |
///The heap type used by Dijkstra algorithm. |
116 | 116 |
|
117 | 117 |
///The heap type used by Dijkstra algorithm. |
118 | 118 |
/// |
119 | 119 |
///\sa BinHeap |
120 | 120 |
///\sa Dijkstra |
121 | 121 |
typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap; |
122 | 122 |
|
123 | 123 |
static Heap *createHeap(HeapCrossRef& R) |
124 | 124 |
{ |
125 | 125 |
return new Heap(R); |
126 | 126 |
} |
127 | 127 |
|
128 | 128 |
///\brief The type of the map that stores the last |
129 | 129 |
///arcs of the shortest paths. |
130 | 130 |
/// |
131 | 131 |
///The type of the map that stores the last |
132 | 132 |
///arcs of the shortest paths. |
133 | 133 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
134 | 134 |
/// |
135 | 135 |
typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap; |
136 | 136 |
///Instantiates a PredMap. |
137 | 137 |
|
138 | 138 |
///This function instantiates a \c PredMap. |
139 | 139 |
///\param G is the digraph, to which we would like to define the PredMap. |
140 | 140 |
///\todo The digraph alone may be insufficient for the initialization |
141 | 141 |
static PredMap *createPredMap(const GR &G) |
142 | 142 |
{ |
143 | 143 |
return new PredMap(G); |
144 | 144 |
} |
145 | 145 |
|
146 | 146 |
///The type of the map that stores whether a nodes is processed. |
147 | 147 |
|
148 | 148 |
///The type of the map that stores whether a nodes is processed. |
149 | 149 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
150 | 150 |
///By default it is a NullMap. |
151 | 151 |
///\todo If it is set to a real map, |
152 | 152 |
///Dijkstra::processed() should read this. |
153 | 153 |
///\todo named parameter to set this type, function to read and write. |
154 | 154 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
155 | 155 |
///Instantiates a ProcessedMap. |
156 | 156 |
|
157 | 157 |
///This function instantiates a \c ProcessedMap. |
158 | 158 |
///\param g is the digraph, to which |
159 | 159 |
///we would like to define the \c ProcessedMap |
160 | 160 |
#ifdef DOXYGEN |
161 | 161 |
static ProcessedMap *createProcessedMap(const GR &g) |
162 | 162 |
#else |
163 | 163 |
static ProcessedMap *createProcessedMap(const GR &) |
164 | 164 |
#endif |
165 | 165 |
{ |
166 | 166 |
return new ProcessedMap(); |
167 | 167 |
} |
168 | 168 |
///The type of the map that stores the dists of the nodes. |
169 | 169 |
|
170 | 170 |
///The type of the map that stores the dists of the nodes. |
171 | 171 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
172 | 172 |
/// |
173 | 173 |
typedef typename Digraph::template NodeMap<typename LM::Value> DistMap; |
174 | 174 |
///Instantiates a DistMap. |
175 | 175 |
|
176 | 176 |
///This function instantiates a \ref DistMap. |
177 | 177 |
///\param G is the digraph, to which we would like to define the \ref DistMap |
178 | 178 |
static DistMap *createDistMap(const GR &G) |
179 | 179 |
{ |
180 | 180 |
return new DistMap(G); |
181 | 181 |
} |
182 | 182 |
}; |
183 | 183 |
|
184 | 184 |
///%Dijkstra algorithm class. |
185 | 185 |
|
186 | 186 |
/// \ingroup shortest_path |
187 | 187 |
///This class provides an efficient implementation of %Dijkstra algorithm. |
188 | 188 |
///The arc lengths are passed to the algorithm using a |
189 | 189 |
///\ref concepts::ReadMap "ReadMap", |
190 | 190 |
///so it is easy to change it to any kind of length. |
191 | 191 |
/// |
192 | 192 |
///The type of the length is determined by the |
193 | 193 |
///\ref concepts::ReadMap::Value "Value" of the length map. |
194 | 194 |
/// |
195 | 195 |
///It is also possible to change the underlying priority heap. |
196 | 196 |
/// |
197 |
///\ |
|
197 |
///\tparam GR The digraph type the algorithm runs on. The default value |
|
198 | 198 |
///is \ref ListDigraph. The value of GR is not used directly by |
199 | 199 |
///Dijkstra, it is only passed to \ref DijkstraDefaultTraits. |
200 |
///\ |
|
200 |
///\tparam LM This read-only ArcMap determines the lengths of the |
|
201 | 201 |
///arcs. It is read once for each arc, so the map may involve in |
202 | 202 |
///relatively time consuming process to compute the arc length if |
203 | 203 |
///it is necessary. The default map type is \ref |
204 | 204 |
///concepts::Digraph::ArcMap "Digraph::ArcMap<int>". The value |
205 | 205 |
///of LM is not used directly by Dijkstra, it is only passed to \ref |
206 |
///DijkstraDefaultTraits. |
|
206 |
///DijkstraDefaultTraits. |
|
207 |
///\tparam TR Traits class to set |
|
207 | 208 |
///various data types used by the algorithm. The default traits |
208 | 209 |
///class is \ref DijkstraDefaultTraits |
209 | 210 |
///"DijkstraDefaultTraits<GR,LM>". See \ref |
210 | 211 |
///DijkstraDefaultTraits for the documentation of a Dijkstra traits |
211 | 212 |
///class. |
212 |
/// |
|
213 |
///\author Jacint Szabo and Alpar Juttner |
|
214 | 213 |
|
215 | 214 |
#ifdef DOXYGEN |
216 | 215 |
template <typename GR, typename LM, typename TR> |
217 | 216 |
#else |
218 | 217 |
template <typename GR=ListDigraph, |
219 | 218 |
typename LM=typename GR::template ArcMap<int>, |
220 | 219 |
typename TR=DijkstraDefaultTraits<GR,LM> > |
221 | 220 |
#endif |
222 | 221 |
class Dijkstra { |
223 | 222 |
public: |
224 | 223 |
/** |
225 | 224 |
* \brief \ref Exception for uninitialized parameters. |
226 | 225 |
* |
227 | 226 |
* This error represents problems in the initialization |
228 | 227 |
* of the parameters of the algorithms. |
229 | 228 |
*/ |
230 | 229 |
class UninitializedParameter : public lemon::UninitializedParameter { |
231 | 230 |
public: |
232 | 231 |
virtual const char* what() const throw() { |
233 | 232 |
return "lemon::Dijkstra::UninitializedParameter"; |
234 | 233 |
} |
235 | 234 |
}; |
236 | 235 |
|
237 | 236 |
typedef TR Traits; |
238 | 237 |
///The type of the underlying digraph. |
239 | 238 |
typedef typename TR::Digraph Digraph; |
240 | 239 |
///\e |
241 | 240 |
typedef typename Digraph::Node Node; |
242 | 241 |
///\e |
243 | 242 |
typedef typename Digraph::NodeIt NodeIt; |
244 | 243 |
///\e |
245 | 244 |
typedef typename Digraph::Arc Arc; |
246 | 245 |
///\e |
247 | 246 |
typedef typename Digraph::OutArcIt OutArcIt; |
248 | 247 |
|
249 | 248 |
///The type of the length of the arcs. |
250 | 249 |
typedef typename TR::LengthMap::Value Value; |
251 | 250 |
///The type of the map that stores the arc lengths. |
252 | 251 |
typedef typename TR::LengthMap LengthMap; |
253 | 252 |
///\brief The type of the map that stores the last |
254 | 253 |
///arcs of the shortest paths. |
255 | 254 |
typedef typename TR::PredMap PredMap; |
256 | 255 |
///The type of the map indicating if a node is processed. |
257 | 256 |
typedef typename TR::ProcessedMap ProcessedMap; |
258 | 257 |
///The type of the map that stores the dists of the nodes. |
259 | 258 |
typedef typename TR::DistMap DistMap; |
260 | 259 |
///The cross reference type used for the current heap. |
261 | 260 |
typedef typename TR::HeapCrossRef HeapCrossRef; |
262 | 261 |
///The heap type used by the dijkstra algorithm. |
263 | 262 |
typedef typename TR::Heap Heap; |
264 | 263 |
///The operation traits. |
265 | 264 |
typedef typename TR::OperationTraits OperationTraits; |
266 | 265 |
private: |
267 | 266 |
/// Pointer to the underlying digraph. |
268 | 267 |
const Digraph *G; |
269 | 268 |
/// Pointer to the length map |
270 | 269 |
const LengthMap *length; |
271 | 270 |
///Pointer to the map of predecessors arcs. |
272 | 271 |
PredMap *_pred; |
273 | 272 |
///Indicates if \ref _pred is locally allocated (\c true) or not. |
274 | 273 |
bool local_pred; |
275 | 274 |
///Pointer to the map of distances. |
276 | 275 |
DistMap *_dist; |
277 | 276 |
///Indicates if \ref _dist is locally allocated (\c true) or not. |
278 | 277 |
bool local_dist; |
279 | 278 |
///Pointer to the map of processed status of the nodes. |
280 | 279 |
ProcessedMap *_processed; |
281 | 280 |
///Indicates if \ref _processed is locally allocated (\c true) or not. |
282 | 281 |
bool local_processed; |
283 | 282 |
///Pointer to the heap cross references. |
284 | 283 |
HeapCrossRef *_heap_cross_ref; |
285 | 284 |
///Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not. |
286 | 285 |
bool local_heap_cross_ref; |
287 | 286 |
///Pointer to the heap. |
288 | 287 |
Heap *_heap; |
289 | 288 |
///Indicates if \ref _heap is locally allocated (\c true) or not. |
290 | 289 |
bool local_heap; |
291 | 290 |
|
292 | 291 |
///Creates the maps if necessary. |
293 | 292 |
|
294 | 293 |
///\todo Better memory allocation (instead of new). |
295 | 294 |
void create_maps() |
296 | 295 |
{ |
297 | 296 |
if(!_pred) { |
298 | 297 |
local_pred = true; |
299 | 298 |
_pred = Traits::createPredMap(*G); |
300 | 299 |
} |
301 | 300 |
if(!_dist) { |
302 | 301 |
local_dist = true; |
303 | 302 |
_dist = Traits::createDistMap(*G); |
304 | 303 |
} |
305 | 304 |
if(!_processed) { |
306 | 305 |
local_processed = true; |
307 | 306 |
_processed = Traits::createProcessedMap(*G); |
308 | 307 |
} |
309 | 308 |
if (!_heap_cross_ref) { |
310 | 309 |
local_heap_cross_ref = true; |
311 | 310 |
_heap_cross_ref = Traits::createHeapCrossRef(*G); |
312 | 311 |
} |
313 | 312 |
if (!_heap) { |
314 | 313 |
local_heap = true; |
315 | 314 |
_heap = Traits::createHeap(*_heap_cross_ref); |
316 | 315 |
} |
317 | 316 |
} |
318 | 317 |
|
319 | 318 |
public : |
320 | 319 |
|
321 | 320 |
typedef Dijkstra Create; |
322 | 321 |
|
323 | 322 |
///\name Named template parameters |
324 | 323 |
|
325 | 324 |
///@{ |
326 | 325 |
|
327 | 326 |
template <class T> |
328 | 327 |
struct DefPredMapTraits : public Traits { |
329 | 328 |
typedef T PredMap; |
330 | 329 |
static PredMap *createPredMap(const Digraph &) |
331 | 330 |
{ |
332 | 331 |
throw UninitializedParameter(); |
333 | 332 |
} |
334 | 333 |
}; |
335 | 334 |
///\ref named-templ-param "Named parameter" for setting PredMap type |
336 | 335 |
|
337 | 336 |
///\ref named-templ-param "Named parameter" for setting PredMap type |
338 | 337 |
/// |
339 | 338 |
template <class T> |
340 | 339 |
struct DefPredMap |
341 | 340 |
: public Dijkstra< Digraph, LengthMap, DefPredMapTraits<T> > { |
342 | 341 |
typedef Dijkstra< Digraph, LengthMap, DefPredMapTraits<T> > Create; |
343 | 342 |
}; |
344 | 343 |
|
345 | 344 |
template <class T> |
346 | 345 |
struct DefDistMapTraits : public Traits { |
347 | 346 |
typedef T DistMap; |
348 | 347 |
static DistMap *createDistMap(const Digraph &) |
349 | 348 |
{ |
350 | 349 |
throw UninitializedParameter(); |
351 | 350 |
} |
352 | 351 |
}; |
353 | 352 |
///\ref named-templ-param "Named parameter" for setting DistMap type |
354 | 353 |
|
355 | 354 |
///\ref named-templ-param "Named parameter" for setting DistMap type |
356 | 355 |
/// |
357 | 356 |
template <class T> |
358 | 357 |
struct DefDistMap |
359 | 358 |
: public Dijkstra< Digraph, LengthMap, DefDistMapTraits<T> > { |
360 | 359 |
typedef Dijkstra< Digraph, LengthMap, DefDistMapTraits<T> > Create; |
361 | 360 |
}; |
362 | 361 |
|
363 | 362 |
template <class T> |
364 | 363 |
struct DefProcessedMapTraits : public Traits { |
365 | 364 |
typedef T ProcessedMap; |
366 | 365 |
static ProcessedMap *createProcessedMap(const Digraph &G) |
367 | 366 |
{ |
368 | 367 |
throw UninitializedParameter(); |
369 | 368 |
} |
370 | 369 |
}; |
371 | 370 |
///\ref named-templ-param "Named parameter" for setting ProcessedMap type |
372 | 371 |
|
373 | 372 |
///\ref named-templ-param "Named parameter" for setting ProcessedMap type |
374 | 373 |
/// |
375 | 374 |
template <class T> |
376 | 375 |
struct DefProcessedMap |
377 | 376 |
: public Dijkstra< Digraph, LengthMap, DefProcessedMapTraits<T> > { |
378 | 377 |
typedef Dijkstra< Digraph, LengthMap, DefProcessedMapTraits<T> > Create; |
379 | 378 |
}; |
380 | 379 |
|
381 | 380 |
struct DefDigraphProcessedMapTraits : public Traits { |
382 | 381 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
383 | 382 |
static ProcessedMap *createProcessedMap(const Digraph &G) |
384 | 383 |
{ |
385 | 384 |
return new ProcessedMap(G); |
386 | 385 |
} |
387 | 386 |
}; |
388 | 387 |
///\brief \ref named-templ-param "Named parameter" |
389 | 388 |
///for setting the ProcessedMap type to be Digraph::NodeMap<bool>. |
390 | 389 |
/// |
391 | 390 |
///\ref named-templ-param "Named parameter" |
392 | 391 |
///for setting the ProcessedMap type to be Digraph::NodeMap<bool>. |
393 | 392 |
///If you don't set it explicitely, it will be automatically allocated. |
394 | 393 |
template <class T> |
395 | 394 |
struct DefProcessedMapToBeDefaultMap |
396 | 395 |
: public Dijkstra< Digraph, LengthMap, DefDigraphProcessedMapTraits> { |
397 | 396 |
typedef Dijkstra< Digraph, LengthMap, DefDigraphProcessedMapTraits> Create; |
398 | 397 |
}; |
399 | 398 |
|
400 | 399 |
template <class H, class CR> |
401 | 400 |
struct DefHeapTraits : public Traits { |
402 | 401 |
typedef CR HeapCrossRef; |
403 | 402 |
typedef H Heap; |
404 | 403 |
static HeapCrossRef *createHeapCrossRef(const Digraph &) { |
405 | 404 |
throw UninitializedParameter(); |
406 | 405 |
} |
407 | 406 |
static Heap *createHeap(HeapCrossRef &) |
408 | 407 |
{ |
409 | 408 |
throw UninitializedParameter(); |
410 | 409 |
} |
411 | 410 |
}; |
412 | 411 |
///\brief \ref named-templ-param "Named parameter" for setting |
413 | 412 |
///heap and cross reference type |
414 | 413 |
/// |
415 | 414 |
///\ref named-templ-param "Named parameter" for setting heap and cross |
416 | 415 |
///reference type |
417 | 416 |
/// |
418 | 417 |
template <class H, class CR = typename Digraph::template NodeMap<int> > |
419 | 418 |
struct DefHeap |
420 | 419 |
: public Dijkstra< Digraph, LengthMap, DefHeapTraits<H, CR> > { |
421 | 420 |
typedef Dijkstra< Digraph, LengthMap, DefHeapTraits<H, CR> > Create; |
422 | 421 |
}; |
423 | 422 |
|
424 | 423 |
template <class H, class CR> |
425 | 424 |
struct DefStandardHeapTraits : public Traits { |
426 | 425 |
typedef CR HeapCrossRef; |
427 | 426 |
typedef H Heap; |
428 | 427 |
static HeapCrossRef *createHeapCrossRef(const Digraph &G) { |
429 | 428 |
return new HeapCrossRef(G); |
430 | 429 |
} |
431 | 430 |
static Heap *createHeap(HeapCrossRef &R) |
432 | 431 |
{ |
433 | 432 |
return new Heap(R); |
434 | 433 |
} |
435 | 434 |
}; |
436 | 435 |
///\brief \ref named-templ-param "Named parameter" for setting |
437 | 436 |
///heap and cross reference type with automatic allocation |
438 | 437 |
/// |
439 | 438 |
///\ref named-templ-param "Named parameter" for setting heap and cross |
440 | 439 |
///reference type. It can allocate the heap and the cross reference |
441 | 440 |
///object if the cross reference's constructor waits for the digraph as |
442 | 441 |
///parameter and the heap's constructor waits for the cross reference. |
443 | 442 |
template <class H, class CR = typename Digraph::template NodeMap<int> > |
444 | 443 |
struct DefStandardHeap |
445 | 444 |
: public Dijkstra< Digraph, LengthMap, DefStandardHeapTraits<H, CR> > { |
446 | 445 |
typedef Dijkstra< Digraph, LengthMap, DefStandardHeapTraits<H, CR> > |
447 | 446 |
Create; |
448 | 447 |
}; |
449 | 448 |
|
450 | 449 |
template <class T> |
451 | 450 |
struct DefOperationTraitsTraits : public Traits { |
452 | 451 |
typedef T OperationTraits; |
453 | 452 |
}; |
454 | 453 |
|
455 | 454 |
/// \brief \ref named-templ-param "Named parameter" for setting |
456 | 455 |
/// OperationTraits type |
457 | 456 |
/// |
458 | 457 |
/// \ref named-templ-param "Named parameter" for setting OperationTraits |
459 | 458 |
/// type |
460 | 459 |
template <class T> |
461 | 460 |
struct DefOperationTraits |
462 | 461 |
: public Dijkstra<Digraph, LengthMap, DefOperationTraitsTraits<T> > { |
463 | 462 |
typedef Dijkstra<Digraph, LengthMap, DefOperationTraitsTraits<T> > |
464 | 463 |
Create; |
465 | 464 |
}; |
466 | 465 |
|
467 | 466 |
///@} |
468 | 467 |
|
469 | 468 |
|
470 | 469 |
protected: |
471 | 470 |
|
472 | 471 |
Dijkstra() {} |
473 | 472 |
|
474 | 473 |
public: |
475 | 474 |
|
476 | 475 |
///Constructor. |
477 | 476 |
|
478 | 477 |
///\param _G the digraph the algorithm will run on. |
479 | 478 |
///\param _length the length map used by the algorithm. |
480 | 479 |
Dijkstra(const Digraph& _G, const LengthMap& _length) : |
481 | 480 |
G(&_G), length(&_length), |
482 | 481 |
_pred(NULL), local_pred(false), |
483 | 482 |
_dist(NULL), local_dist(false), |
484 | 483 |
_processed(NULL), local_processed(false), |
485 | 484 |
_heap_cross_ref(NULL), local_heap_cross_ref(false), |
486 | 485 |
_heap(NULL), local_heap(false) |
487 | 486 |
{ } |
488 | 487 |
|
489 | 488 |
///Destructor. |
490 | 489 |
~Dijkstra() |
491 | 490 |
{ |
492 | 491 |
if(local_pred) delete _pred; |
493 | 492 |
if(local_dist) delete _dist; |
494 | 493 |
if(local_processed) delete _processed; |
495 | 494 |
if(local_heap_cross_ref) delete _heap_cross_ref; |
496 | 495 |
if(local_heap) delete _heap; |
497 | 496 |
} |
498 | 497 |
|
499 | 498 |
///Sets the length map. |
500 | 499 |
|
501 | 500 |
///Sets the length map. |
502 | 501 |
///\return <tt> (*this) </tt> |
503 | 502 |
Dijkstra &lengthMap(const LengthMap &m) |
504 | 503 |
{ |
505 | 504 |
length = &m; |
506 | 505 |
return *this; |
507 | 506 |
} |
508 | 507 |
|
509 | 508 |
///Sets the map storing the predecessor arcs. |
510 | 509 |
|
511 | 510 |
///Sets the map storing the predecessor arcs. |
512 | 511 |
///If you don't use this function before calling \ref run(), |
513 | 512 |
///it will allocate one. The destuctor deallocates this |
514 | 513 |
///automatically allocated map, of course. |
515 | 514 |
///\return <tt> (*this) </tt> |
516 | 515 |
Dijkstra &predMap(PredMap &m) |
517 | 516 |
{ |
518 | 517 |
if(local_pred) { |
519 | 518 |
delete _pred; |
520 | 519 |
local_pred=false; |
521 | 520 |
} |
522 | 521 |
_pred = &m; |
523 | 522 |
return *this; |
524 | 523 |
} |
525 | 524 |
|
526 | 525 |
///Sets the map storing the distances calculated by the algorithm. |
527 | 526 |
|
528 | 527 |
///Sets the map storing the distances calculated by the algorithm. |
529 | 528 |
///If you don't use this function before calling \ref run(), |
530 | 529 |
///it will allocate one. The destuctor deallocates this |
531 | 530 |
///automatically allocated map, of course. |
532 | 531 |
///\return <tt> (*this) </tt> |
533 | 532 |
Dijkstra &distMap(DistMap &m) |
534 | 533 |
{ |
535 | 534 |
if(local_dist) { |
536 | 535 |
delete _dist; |
537 | 536 |
local_dist=false; |
538 | 537 |
} |
539 | 538 |
_dist = &m; |
540 | 539 |
return *this; |
541 | 540 |
} |
542 | 541 |
|
543 | 542 |
///Sets the heap and the cross reference used by algorithm. |
544 | 543 |
|
545 | 544 |
///Sets the heap and the cross reference used by algorithm. |
546 | 545 |
///If you don't use this function before calling \ref run(), |
547 | 546 |
///it will allocate one. The destuctor deallocates this |
548 | 547 |
///automatically allocated heap and cross reference, of course. |
549 | 548 |
///\return <tt> (*this) </tt> |
550 | 549 |
Dijkstra &heap(Heap& hp, HeapCrossRef &cr) |
551 | 550 |
{ |
552 | 551 |
if(local_heap_cross_ref) { |
553 | 552 |
delete _heap_cross_ref; |
554 | 553 |
local_heap_cross_ref=false; |
555 | 554 |
} |
556 | 555 |
_heap_cross_ref = &cr; |
557 | 556 |
if(local_heap) { |
558 | 557 |
delete _heap; |
559 | 558 |
local_heap=false; |
560 | 559 |
} |
561 | 560 |
_heap = &hp; |
562 | 561 |
return *this; |
563 | 562 |
} |
564 | 563 |
|
565 | 564 |
private: |
566 | 565 |
void finalizeNodeData(Node v,Value dst) |
567 | 566 |
{ |
568 | 567 |
_processed->set(v,true); |
569 | 568 |
_dist->set(v, dst); |
570 | 569 |
} |
571 | 570 |
|
572 | 571 |
public: |
573 | 572 |
|
574 | 573 |
typedef PredMapPath<Digraph, PredMap> Path; |
575 | 574 |
|
576 | 575 |
///\name Execution control |
577 | 576 |
///The simplest way to execute the algorithm is to use |
578 | 577 |
///one of the member functions called \c run(...). |
579 | 578 |
///\n |
580 | 579 |
///If you need more control on the execution, |
581 | 580 |
///first you must call \ref init(), then you can add several source nodes |
582 | 581 |
///with \ref addSource(). |
583 | 582 |
///Finally \ref start() will perform the actual path |
584 | 583 |
///computation. |
585 | 584 |
|
586 | 585 |
///@{ |
587 | 586 |
|
588 | 587 |
///Initializes the internal data structures. |
589 | 588 |
|
590 | 589 |
///Initializes the internal data structures. |
591 | 590 |
/// |
592 | 591 |
void init() |
593 | 592 |
{ |
594 | 593 |
create_maps(); |
595 | 594 |
_heap->clear(); |
596 | 595 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
597 | 596 |
_pred->set(u,INVALID); |
598 | 597 |
_processed->set(u,false); |
599 | 598 |
_heap_cross_ref->set(u,Heap::PRE_HEAP); |
600 | 599 |
} |
601 | 600 |
} |
602 | 601 |
|
603 | 602 |
///Adds a new source node. |
604 | 603 |
|
605 | 604 |
///Adds a new source node to the priority heap. |
606 | 605 |
/// |
607 | 606 |
///The optional second parameter is the initial distance of the node. |
608 | 607 |
/// |
609 | 608 |
///It checks if the node has already been added to the heap and |
610 | 609 |
///it is pushed to the heap only if either it was not in the heap |
611 | 610 |
///or the shortest path found till then is shorter than \c dst. |
612 | 611 |
void addSource(Node s,Value dst=OperationTraits::zero()) |
613 | 612 |
{ |
614 | 613 |
if(_heap->state(s) != Heap::IN_HEAP) { |
615 | 614 |
_heap->push(s,dst); |
616 | 615 |
} else if(OperationTraits::less((*_heap)[s], dst)) { |
617 | 616 |
_heap->set(s,dst); |
618 | 617 |
_pred->set(s,INVALID); |
619 | 618 |
} |
620 | 619 |
} |
621 | 620 |
|
622 | 621 |
///Processes the next node in the priority heap |
623 | 622 |
|
624 | 623 |
///Processes the next node in the priority heap. |
625 | 624 |
/// |
626 | 625 |
///\return The processed node. |
627 | 626 |
/// |
628 | 627 |
///\warning The priority heap must not be empty! |
629 | 628 |
Node processNextNode() |
630 | 629 |
{ |
631 | 630 |
Node v=_heap->top(); |
632 | 631 |
Value oldvalue=_heap->prio(); |
633 | 632 |
_heap->pop(); |
634 | 633 |
finalizeNodeData(v,oldvalue); |
635 | 634 |
|
636 | 635 |
for(OutArcIt e(*G,v); e!=INVALID; ++e) { |
637 | 636 |
Node w=G->target(e); |
638 | 637 |
switch(_heap->state(w)) { |
639 | 638 |
case Heap::PRE_HEAP: |
640 | 639 |
_heap->push(w,OperationTraits::plus(oldvalue, (*length)[e])); |
641 | 640 |
_pred->set(w,e); |
642 | 641 |
break; |
643 | 642 |
case Heap::IN_HEAP: |
644 | 643 |
{ |
645 | 644 |
Value newvalue = OperationTraits::plus(oldvalue, (*length)[e]); |
646 | 645 |
if ( OperationTraits::less(newvalue, (*_heap)[w]) ) { |
647 | 646 |
_heap->decrease(w, newvalue); |
648 | 647 |
_pred->set(w,e); |
649 | 648 |
} |
650 | 649 |
} |
651 | 650 |
break; |
652 | 651 |
case Heap::POST_HEAP: |
653 | 652 |
break; |
654 | 653 |
} |
655 | 654 |
} |
656 | 655 |
return v; |
657 | 656 |
} |
658 | 657 |
|
659 | 658 |
///Next node to be processed. |
660 | 659 |
|
661 | 660 |
///Next node to be processed. |
662 | 661 |
/// |
663 | 662 |
///\return The next node to be processed or INVALID if the priority heap |
664 | 663 |
/// is empty. |
665 | 664 |
Node nextNode() |
666 | 665 |
{ |
667 | 666 |
return !_heap->empty()?_heap->top():INVALID; |
668 | 667 |
} |
669 | 668 |
|
670 | 669 |
///\brief Returns \c false if there are nodes |
671 | 670 |
///to be processed in the priority heap |
672 | 671 |
/// |
673 | 672 |
///Returns \c false if there are nodes |
674 | 673 |
///to be processed in the priority heap |
675 | 674 |
bool emptyQueue() { return _heap->empty(); } |
676 | 675 |
///Returns the number of the nodes to be processed in the priority heap |
677 | 676 |
|
678 | 677 |
///Returns the number of the nodes to be processed in the priority heap |
679 | 678 |
/// |
680 | 679 |
int queueSize() { return _heap->size(); } |
681 | 680 |
|
682 | 681 |
///Executes the algorithm. |
683 | 682 |
|
684 | 683 |
///Executes the algorithm. |
685 | 684 |
/// |
686 | 685 |
///\pre init() must be called and at least one node should be added |
687 | 686 |
///with addSource() before using this function. |
688 | 687 |
/// |
689 | 688 |
///This method runs the %Dijkstra algorithm from the root node(s) |
690 | 689 |
///in order to |
691 | 690 |
///compute the |
692 | 691 |
///shortest path to each node. The algorithm computes |
693 | 692 |
///- The shortest path tree. |
694 | 693 |
///- The distance of each node from the root(s). |
695 | 694 |
/// |
696 | 695 |
void start() |
697 | 696 |
{ |
698 | 697 |
while ( !_heap->empty() ) processNextNode(); |
699 | 698 |
} |
700 | 699 |
|
701 | 700 |
///Executes the algorithm until \c dest is reached. |
702 | 701 |
|
703 | 702 |
///Executes the algorithm until \c dest is reached. |
704 | 703 |
/// |
705 | 704 |
///\pre init() must be called and at least one node should be added |
706 | 705 |
///with addSource() before using this function. |
707 | 706 |
/// |
708 | 707 |
///This method runs the %Dijkstra algorithm from the root node(s) |
709 | 708 |
///in order to |
710 | 709 |
///compute the |
711 | 710 |
///shortest path to \c dest. The algorithm computes |
712 | 711 |
///- The shortest path to \c dest. |
713 | 712 |
///- The distance of \c dest from the root(s). |
714 | 713 |
/// |
715 | 714 |
void start(Node dest) |
716 | 715 |
{ |
717 | 716 |
while ( !_heap->empty() && _heap->top()!=dest ) processNextNode(); |
718 | 717 |
if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio()); |
719 | 718 |
} |
720 | 719 |
|
721 | 720 |
///Executes the algorithm until a condition is met. |
722 | 721 |
|
723 | 722 |
///Executes the algorithm until a condition is met. |
724 | 723 |
/// |
725 | 724 |
///\pre init() must be called and at least one node should be added |
726 | 725 |
///with addSource() before using this function. |
727 | 726 |
/// |
728 | 727 |
///\param nm must be a bool (or convertible) node map. The algorithm |
729 | 728 |
///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. |
730 | 729 |
/// |
731 | 730 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
732 | 731 |
///\c INVALID if no such node was found. |
733 | 732 |
template<class NodeBoolMap> |
734 | 733 |
Node start(const NodeBoolMap &nm) |
735 | 734 |
{ |
736 | 735 |
while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode(); |
737 | 736 |
if ( _heap->empty() ) return INVALID; |
738 | 737 |
finalizeNodeData(_heap->top(),_heap->prio()); |
739 | 738 |
return _heap->top(); |
740 | 739 |
} |
741 | 740 |
|
742 | 741 |
///Runs %Dijkstra algorithm from node \c s. |
743 | 742 |
|
744 | 743 |
///This method runs the %Dijkstra algorithm from a root node \c s |
745 | 744 |
///in order to |
746 | 745 |
///compute the |
747 | 746 |
///shortest path to each node. The algorithm computes |
748 | 747 |
///- The shortest path tree. |
749 | 748 |
///- The distance of each node from the root. |
750 | 749 |
/// |
751 | 750 |
///\note d.run(s) is just a shortcut of the following code. |
752 | 751 |
///\code |
753 | 752 |
/// d.init(); |
754 | 753 |
/// d.addSource(s); |
755 | 754 |
/// d.start(); |
756 | 755 |
///\endcode |
757 | 756 |
void run(Node s) { |
758 | 757 |
init(); |
759 | 758 |
addSource(s); |
760 | 759 |
start(); |
761 | 760 |
} |
762 | 761 |
|
763 | 762 |
///Finds the shortest path between \c s and \c t. |
764 | 763 |
|
765 | 764 |
///Finds the shortest path between \c s and \c t. |
766 | 765 |
/// |
767 | 766 |
///\return The length of the shortest s---t path if there exists one, |
768 | 767 |
///0 otherwise. |
769 | 768 |
///\note Apart from the return value, d.run(s) is |
770 | 769 |
///just a shortcut of the following code. |
771 | 770 |
///\code |
772 | 771 |
/// d.init(); |
773 | 772 |
/// d.addSource(s); |
774 | 773 |
/// d.start(t); |
775 | 774 |
///\endcode |
776 | 775 |
Value run(Node s,Node t) { |
777 | 776 |
init(); |
778 | 777 |
addSource(s); |
779 | 778 |
start(t); |
780 | 779 |
return (*_pred)[t]==INVALID?OperationTraits::zero():(*_dist)[t]; |
781 | 780 |
} |
782 | 781 |
|
783 | 782 |
///@} |
784 | 783 |
|
785 | 784 |
///\name Query Functions |
786 | 785 |
///The result of the %Dijkstra algorithm can be obtained using these |
787 | 786 |
///functions.\n |
788 | 787 |
///Before the use of these functions, |
789 | 788 |
///either run() or start() must be called. |
790 | 789 |
|
791 | 790 |
///@{ |
792 | 791 |
|
793 | 792 |
///Gives back the shortest path. |
794 | 793 |
|
795 | 794 |
///Gives back the shortest path. |
796 | 795 |
///\pre The \c t should be reachable from the source. |
797 | 796 |
Path path(Node t) |
798 | 797 |
{ |
799 | 798 |
return Path(*G, *_pred, t); |
800 | 799 |
} |
801 | 800 |
|
802 | 801 |
///The distance of a node from the root. |
803 | 802 |
|
804 | 803 |
///Returns the distance of a node from the root. |
805 | 804 |
///\pre \ref run() must be called before using this function. |
806 | 805 |
///\warning If node \c v in unreachable from the root the return value |
807 | 806 |
///of this funcion is undefined. |
808 | 807 |
Value dist(Node v) const { return (*_dist)[v]; } |
809 | 808 |
|
810 | 809 |
///The current distance of a node from the root. |
811 | 810 |
|
812 | 811 |
///Returns the current distance of a node from the root. |
813 | 812 |
///It may be decreased in the following processes. |
814 | 813 |
///\pre \c node should be reached but not processed |
815 | 814 |
Value currentDist(Node v) const { return (*_heap)[v]; } |
816 | 815 |
|
817 | 816 |
///Returns the 'previous arc' of the shortest path tree. |
818 | 817 |
|
819 | 818 |
///For a node \c v it returns the 'previous arc' of the shortest path tree, |
820 | 819 |
///i.e. it returns the last arc of a shortest path from the root to \c |
821 | 820 |
///v. It is \ref INVALID |
822 | 821 |
///if \c v is unreachable from the root or if \c v=s. The |
823 | 822 |
///shortest path tree used here is equal to the shortest path tree used in |
824 | 823 |
///\ref predNode(). \pre \ref run() must be called before using |
825 | 824 |
///this function. |
826 | 825 |
Arc predArc(Node v) const { return (*_pred)[v]; } |
827 | 826 |
|
828 | 827 |
///Returns the 'previous node' of the shortest path tree. |
829 | 828 |
|
830 | 829 |
///For a node \c v it returns the 'previous node' of the shortest path tree, |
831 | 830 |
///i.e. it returns the last but one node from a shortest path from the |
832 | 831 |
///root to \c /v. It is INVALID if \c v is unreachable from the root or if |
833 | 832 |
///\c v=s. The shortest path tree used here is equal to the shortest path |
834 | 833 |
///tree used in \ref predArc(). \pre \ref run() must be called before |
835 | 834 |
///using this function. |
836 | 835 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
837 | 836 |
G->source((*_pred)[v]); } |
838 | 837 |
|
839 | 838 |
///Returns a reference to the NodeMap of distances. |
840 | 839 |
|
841 | 840 |
///Returns a reference to the NodeMap of distances. \pre \ref run() must |
842 | 841 |
///be called before using this function. |
843 | 842 |
const DistMap &distMap() const { return *_dist;} |
844 | 843 |
|
845 | 844 |
///Returns a reference to the shortest path tree map. |
846 | 845 |
|
847 | 846 |
///Returns a reference to the NodeMap of the arcs of the |
848 | 847 |
///shortest path tree. |
849 | 848 |
///\pre \ref run() must be called before using this function. |
850 | 849 |
const PredMap &predMap() const { return *_pred;} |
851 | 850 |
|
852 | 851 |
///Checks if a node is reachable from the root. |
853 | 852 |
|
854 | 853 |
///Returns \c true if \c v is reachable from the root. |
855 | 854 |
///\warning The source nodes are inditated as unreached. |
856 | 855 |
///\pre \ref run() must be called before using this function. |
857 | 856 |
/// |
858 | 857 |
bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; } |
859 | 858 |
|
860 | 859 |
///Checks if a node is processed. |
861 | 860 |
|
862 | 861 |
///Returns \c true if \c v is processed, i.e. the shortest |
863 | 862 |
///path to \c v has already found. |
864 | 863 |
///\pre \ref run() must be called before using this function. |
865 | 864 |
/// |
866 | 865 |
bool processed(Node v) { return (*_heap_cross_ref)[v] == Heap::POST_HEAP; } |
867 | 866 |
|
868 | 867 |
///@} |
869 | 868 |
}; |
870 | 869 |
|
871 | 870 |
|
872 | 871 |
|
873 | 872 |
|
874 | 873 |
|
875 | 874 |
///Default traits class of Dijkstra function. |
876 | 875 |
|
877 | 876 |
///Default traits class of Dijkstra function. |
878 |
///\param GR Digraph type. |
|
879 |
///\param LM Type of length map. |
|
877 |
///\tparam GR Digraph type. |
|
878 |
///\tparam LM Type of length map. |
|
880 | 879 |
template<class GR, class LM> |
881 | 880 |
struct DijkstraWizardDefaultTraits |
882 | 881 |
{ |
883 | 882 |
///The digraph type the algorithm runs on. |
884 | 883 |
typedef GR Digraph; |
885 | 884 |
///The type of the map that stores the arc lengths. |
886 | 885 |
|
887 | 886 |
///The type of the map that stores the arc lengths. |
888 | 887 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
889 | 888 |
typedef LM LengthMap; |
890 | 889 |
//The type of the length of the arcs. |
891 | 890 |
typedef typename LM::Value Value; |
892 | 891 |
/// Operation traits for Dijkstra algorithm. |
893 | 892 |
|
894 | 893 |
/// It defines the used operation by the algorithm. |
895 | 894 |
/// \see DijkstraDefaultOperationTraits |
896 | 895 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
897 | 896 |
///The heap type used by Dijkstra algorithm. |
898 | 897 |
|
899 | 898 |
/// The cross reference type used by heap. |
900 | 899 |
|
901 | 900 |
/// The cross reference type used by heap. |
902 | 901 |
/// Usually it is \c Digraph::NodeMap<int>. |
903 | 902 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
904 | 903 |
///Instantiates a HeapCrossRef. |
905 | 904 |
|
906 | 905 |
///This function instantiates a \ref HeapCrossRef. |
907 | 906 |
/// \param G is the digraph, to which we would like to define the |
908 | 907 |
/// HeapCrossRef. |
909 | 908 |
/// \todo The digraph alone may be insufficient for the initialization |
910 | 909 |
static HeapCrossRef *createHeapCrossRef(const GR &G) |
911 | 910 |
{ |
912 | 911 |
return new HeapCrossRef(G); |
913 | 912 |
} |
914 | 913 |
|
915 | 914 |
///The heap type used by Dijkstra algorithm. |
916 | 915 |
|
917 | 916 |
///The heap type used by Dijkstra algorithm. |
918 | 917 |
/// |
919 | 918 |
///\sa BinHeap |
920 | 919 |
///\sa Dijkstra |
921 | 920 |
typedef BinHeap<typename LM::Value, typename GR::template NodeMap<int>, |
922 | 921 |
std::less<Value> > Heap; |
923 | 922 |
|
924 | 923 |
static Heap *createHeap(HeapCrossRef& R) |
925 | 924 |
{ |
926 | 925 |
return new Heap(R); |
927 | 926 |
} |
928 | 927 |
|
929 | 928 |
///\brief The type of the map that stores the last |
930 | 929 |
///arcs of the shortest paths. |
931 | 930 |
/// |
932 | 931 |
///The type of the map that stores the last |
933 | 932 |
///arcs of the shortest paths. |
934 | 933 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
935 | 934 |
/// |
936 | 935 |
typedef NullMap <typename GR::Node,typename GR::Arc> PredMap; |
937 | 936 |
///Instantiates a PredMap. |
938 | 937 |
|
939 | 938 |
///This function instantiates a \ref PredMap. |
940 | 939 |
///\param g is the digraph, to which we would like to define the PredMap. |
941 | 940 |
///\todo The digraph alone may be insufficient for the initialization |
942 | 941 |
#ifdef DOXYGEN |
943 | 942 |
static PredMap *createPredMap(const GR &g) |
944 | 943 |
#else |
945 | 944 |
static PredMap *createPredMap(const GR &) |
946 | 945 |
#endif |
947 | 946 |
{ |
948 | 947 |
return new PredMap(); |
949 | 948 |
} |
950 | 949 |
///The type of the map that stores whether a nodes is processed. |
951 | 950 |
|
952 | 951 |
///The type of the map that stores whether a nodes is processed. |
953 | 952 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
954 | 953 |
///By default it is a NullMap. |
955 | 954 |
///\todo If it is set to a real map, |
956 | 955 |
///Dijkstra::processed() should read this. |
957 | 956 |
///\todo named parameter to set this type, function to read and write. |
958 | 957 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
959 | 958 |
///Instantiates a ProcessedMap. |
960 | 959 |
|
961 | 960 |
///This function instantiates a \ref ProcessedMap. |
962 | 961 |
///\param g is the digraph, to which |
963 | 962 |
///we would like to define the \ref ProcessedMap |
964 | 963 |
#ifdef DOXYGEN |
965 | 964 |
static ProcessedMap *createProcessedMap(const GR &g) |
966 | 965 |
#else |
967 | 966 |
static ProcessedMap *createProcessedMap(const GR &) |
968 | 967 |
#endif |
969 | 968 |
{ |
970 | 969 |
return new ProcessedMap(); |
971 | 970 |
} |
972 | 971 |
///The type of the map that stores the dists of the nodes. |
973 | 972 |
|
974 | 973 |
///The type of the map that stores the dists of the nodes. |
975 | 974 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
976 | 975 |
/// |
977 | 976 |
typedef NullMap<typename Digraph::Node,typename LM::Value> DistMap; |
978 | 977 |
///Instantiates a DistMap. |
979 | 978 |
|
980 | 979 |
///This function instantiates a \ref DistMap. |
981 | 980 |
///\param g is the digraph, to which we would like to define the \ref DistMap |
982 | 981 |
#ifdef DOXYGEN |
983 | 982 |
static DistMap *createDistMap(const GR &g) |
984 | 983 |
#else |
985 | 984 |
static DistMap *createDistMap(const GR &) |
986 | 985 |
#endif |
987 | 986 |
{ |
988 | 987 |
return new DistMap(); |
989 | 988 |
} |
990 | 989 |
}; |
991 | 990 |
|
992 | 991 |
/// Default traits used by \ref DijkstraWizard |
993 | 992 |
|
994 | 993 |
/// To make it easier to use Dijkstra algorithm |
995 | 994 |
///we have created a wizard class. |
996 | 995 |
/// This \ref DijkstraWizard class needs default traits, |
997 | 996 |
///as well as the \ref Dijkstra class. |
998 | 997 |
/// The \ref DijkstraWizardBase is a class to be the default traits of the |
999 | 998 |
/// \ref DijkstraWizard class. |
1000 | 999 |
/// \todo More named parameters are required... |
1001 | 1000 |
template<class GR,class LM> |
1002 | 1001 |
class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM> |
1003 | 1002 |
{ |
1004 | 1003 |
|
1005 | 1004 |
typedef DijkstraWizardDefaultTraits<GR,LM> Base; |
1006 | 1005 |
protected: |
1007 | 1006 |
/// Type of the nodes in the digraph. |
1008 | 1007 |
typedef typename Base::Digraph::Node Node; |
1009 | 1008 |
|
1010 | 1009 |
/// Pointer to the underlying digraph. |
1011 | 1010 |
void *_g; |
1012 | 1011 |
/// Pointer to the length map |
1013 | 1012 |
void *_length; |
1014 | 1013 |
///Pointer to the map of predecessors arcs. |
1015 | 1014 |
void *_pred; |
1016 | 1015 |
///Pointer to the map of distances. |
1017 | 1016 |
void *_dist; |
1018 | 1017 |
///Pointer to the source node. |
1019 | 1018 |
Node _source; |
1020 | 1019 |
|
1021 | 1020 |
public: |
1022 | 1021 |
/// Constructor. |
1023 | 1022 |
|
1024 | 1023 |
/// This constructor does not require parameters, therefore it initiates |
1025 | 1024 |
/// all of the attributes to default values (0, INVALID). |
1026 | 1025 |
DijkstraWizardBase() : _g(0), _length(0), _pred(0), |
1027 | 1026 |
_dist(0), _source(INVALID) {} |
1028 | 1027 |
|
1029 | 1028 |
/// Constructor. |
1030 | 1029 |
|
1031 | 1030 |
/// This constructor requires some parameters, |
1032 | 1031 |
/// listed in the parameters list. |
1033 | 1032 |
/// Others are initiated to 0. |
1034 | 1033 |
/// \param g is the initial value of \ref _g |
1035 | 1034 |
/// \param l is the initial value of \ref _length |
1036 | 1035 |
/// \param s is the initial value of \ref _source |
1037 | 1036 |
DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) : |
1038 | 1037 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
1039 | 1038 |
_length(reinterpret_cast<void*>(const_cast<LM*>(&l))), |
1040 | 1039 |
_pred(0), _dist(0), _source(s) {} |
1041 | 1040 |
|
1042 | 1041 |
}; |
1043 | 1042 |
|
1044 | 1043 |
/// A class to make the usage of Dijkstra algorithm easier |
1045 | 1044 |
|
1046 | 1045 |
/// This class is created to make it easier to use Dijkstra algorithm. |
1047 | 1046 |
/// It uses the functions and features of the plain \ref Dijkstra, |
1048 | 1047 |
/// but it is much simpler to use it. |
1049 | 1048 |
/// |
1050 | 1049 |
/// Simplicity means that the way to change the types defined |
1051 | 1050 |
/// in the traits class is based on functions that returns the new class |
1052 | 1051 |
/// and not on templatable built-in classes. |
1053 | 1052 |
/// When using the plain \ref Dijkstra |
1054 | 1053 |
/// the new class with the modified type comes from |
1055 | 1054 |
/// the original class by using the :: |
1056 | 1055 |
/// operator. In the case of \ref DijkstraWizard only |
1057 | 1056 |
/// a function have to be called and it will |
1058 | 1057 |
/// return the needed class. |
1059 | 1058 |
/// |
1060 | 1059 |
/// It does not have own \ref run method. When its \ref run method is called |
1061 | 1060 |
/// it initiates a plain \ref Dijkstra class, and calls the \ref |
1062 | 1061 |
/// Dijkstra::run method of it. |
1063 | 1062 |
template<class TR> |
1064 | 1063 |
class DijkstraWizard : public TR |
1065 | 1064 |
{ |
1066 | 1065 |
typedef TR Base; |
1067 | 1066 |
|
1068 | 1067 |
///The type of the underlying digraph. |
1069 | 1068 |
typedef typename TR::Digraph Digraph; |
1070 | 1069 |
//\e |
1071 | 1070 |
typedef typename Digraph::Node Node; |
1072 | 1071 |
//\e |
1073 | 1072 |
typedef typename Digraph::NodeIt NodeIt; |
1074 | 1073 |
//\e |
1075 | 1074 |
typedef typename Digraph::Arc Arc; |
1076 | 1075 |
//\e |
1077 | 1076 |
typedef typename Digraph::OutArcIt OutArcIt; |
1078 | 1077 |
|
1079 | 1078 |
///The type of the map that stores the arc lengths. |
1080 | 1079 |
typedef typename TR::LengthMap LengthMap; |
1081 | 1080 |
///The type of the length of the arcs. |
1082 | 1081 |
typedef typename LengthMap::Value Value; |
1083 | 1082 |
///\brief The type of the map that stores the last |
1084 | 1083 |
///arcs of the shortest paths. |
1085 | 1084 |
typedef typename TR::PredMap PredMap; |
1086 | 1085 |
///The type of the map that stores the dists of the nodes. |
1087 | 1086 |
typedef typename TR::DistMap DistMap; |
1088 | 1087 |
///The heap type used by the dijkstra algorithm. |
1089 | 1088 |
typedef typename TR::Heap Heap; |
1090 | 1089 |
public: |
1091 | 1090 |
/// Constructor. |
1092 | 1091 |
DijkstraWizard() : TR() {} |
1093 | 1092 |
|
1094 | 1093 |
/// Constructor that requires parameters. |
1095 | 1094 |
|
1096 | 1095 |
/// Constructor that requires parameters. |
1097 | 1096 |
/// These parameters will be the default values for the traits class. |
1098 | 1097 |
DijkstraWizard(const Digraph &g,const LengthMap &l, Node s=INVALID) : |
1099 | 1098 |
TR(g,l,s) {} |
1100 | 1099 |
|
1101 | 1100 |
///Copy constructor |
1102 | 1101 |
DijkstraWizard(const TR &b) : TR(b) {} |
1103 | 1102 |
|
1104 | 1103 |
~DijkstraWizard() {} |
1105 | 1104 |
|
1106 | 1105 |
///Runs Dijkstra algorithm from a given node. |
1107 | 1106 |
|
1108 | 1107 |
///Runs Dijkstra algorithm from a given node. |
1109 | 1108 |
///The node can be given by the \ref source function. |
1110 | 1109 |
void run() |
1111 | 1110 |
{ |
1112 | 1111 |
if(Base::_source==INVALID) throw UninitializedParameter(); |
1113 | 1112 |
Dijkstra<Digraph,LengthMap,TR> |
1114 | 1113 |
dij(*reinterpret_cast<const Digraph*>(Base::_g), |
1115 | 1114 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
1116 | 1115 |
if(Base::_pred) dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1117 | 1116 |
if(Base::_dist) dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1118 | 1117 |
dij.run(Base::_source); |
1119 | 1118 |
} |
1120 | 1119 |
|
1121 | 1120 |
///Runs Dijkstra algorithm from the given node. |
1122 | 1121 |
|
1123 | 1122 |
///Runs Dijkstra algorithm from the given node. |
1124 | 1123 |
///\param s is the given source. |
1125 | 1124 |
void run(Node s) |
1126 | 1125 |
{ |
1127 | 1126 |
Base::_source=s; |
1128 | 1127 |
run(); |
1129 | 1128 |
} |
1130 | 1129 |
|
1131 | 1130 |
template<class T> |
1132 | 1131 |
struct DefPredMapBase : public Base { |
1133 | 1132 |
typedef T PredMap; |
1134 | 1133 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1135 | 1134 |
DefPredMapBase(const TR &b) : TR(b) {} |
1136 | 1135 |
}; |
1137 | 1136 |
|
1138 | 1137 |
///\brief \ref named-templ-param "Named parameter" |
1139 | 1138 |
///function for setting PredMap type |
1140 | 1139 |
/// |
1141 | 1140 |
/// \ref named-templ-param "Named parameter" |
1142 | 1141 |
///function for setting PredMap type |
1143 | 1142 |
/// |
1144 | 1143 |
template<class T> |
1145 | 1144 |
DijkstraWizard<DefPredMapBase<T> > predMap(const T &t) |
1146 | 1145 |
{ |
1147 | 1146 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1148 | 1147 |
return DijkstraWizard<DefPredMapBase<T> >(*this); |
1149 | 1148 |
} |
1150 | 1149 |
|
1151 | 1150 |
template<class T> |
1152 | 1151 |
struct DefDistMapBase : public Base { |
1153 | 1152 |
typedef T DistMap; |
1154 | 1153 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1155 | 1154 |
DefDistMapBase(const TR &b) : TR(b) {} |
1156 | 1155 |
}; |
1157 | 1156 |
|
1158 | 1157 |
///\brief \ref named-templ-param "Named parameter" |
1159 | 1158 |
///function for setting DistMap type |
1160 | 1159 |
/// |
1161 | 1160 |
/// \ref named-templ-param "Named parameter" |
1162 | 1161 |
///function for setting DistMap type |
1163 | 1162 |
/// |
1164 | 1163 |
template<class T> |
1165 | 1164 |
DijkstraWizard<DefDistMapBase<T> > distMap(const T &t) |
1166 | 1165 |
{ |
1167 | 1166 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1168 | 1167 |
return DijkstraWizard<DefDistMapBase<T> >(*this); |
1169 | 1168 |
} |
1170 | 1169 |
|
1171 | 1170 |
/// Sets the source node, from which the Dijkstra algorithm runs. |
1172 | 1171 |
|
1173 | 1172 |
/// Sets the source node, from which the Dijkstra algorithm runs. |
1174 | 1173 |
/// \param s is the source node. |
1175 | 1174 |
DijkstraWizard<TR> &source(Node s) |
1176 | 1175 |
{ |
1177 | 1176 |
Base::_source=s; |
1178 | 1177 |
return *this; |
1179 | 1178 |
} |
1180 | 1179 |
|
1181 | 1180 |
}; |
1182 | 1181 |
|
1183 | 1182 |
///Function type interface for Dijkstra algorithm. |
1184 | 1183 |
|
1185 | 1184 |
/// \ingroup shortest_path |
1186 | 1185 |
///Function type interface for Dijkstra algorithm. |
1187 | 1186 |
/// |
1188 | 1187 |
///This function also has several |
1189 | 1188 |
///\ref named-templ-func-param "named parameters", |
1190 | 1189 |
///they are declared as the members of class \ref DijkstraWizard. |
1191 | 1190 |
///The following |
1192 | 1191 |
///example shows how to use these parameters. |
1193 | 1192 |
///\code |
1194 | 1193 |
/// dijkstra(g,length,source).predMap(preds).run(); |
1195 | 1194 |
///\endcode |
1196 | 1195 |
///\warning Don't forget to put the \ref DijkstraWizard::run() "run()" |
1197 | 1196 |
///to the end of the parameter list. |
1198 | 1197 |
///\sa DijkstraWizard |
1199 | 1198 |
///\sa Dijkstra |
1200 | 1199 |
template<class GR, class LM> |
1201 | 1200 |
DijkstraWizard<DijkstraWizardBase<GR,LM> > |
1202 | 1201 |
dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID) |
1203 | 1202 |
{ |
1204 | 1203 |
return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s); |
1205 | 1204 |
} |
1206 | 1205 |
|
1207 | 1206 |
} //END OF NAMESPACE LEMON |
1208 | 1207 |
|
1209 | 1208 |
#endif |
... | ... |
@@ -35,817 +35,817 @@ |
35 | 35 |
#endif |
36 | 36 |
|
37 | 37 |
#include<lemon/math.h> |
38 | 38 |
#include<lemon/bits/invalid.h> |
39 | 39 |
#include<lemon/dim2.h> |
40 | 40 |
#include<lemon/maps.h> |
41 | 41 |
#include<lemon/color.h> |
42 | 42 |
#include<lemon/bits/bezier.h> |
43 | 43 |
|
44 | 44 |
|
45 | 45 |
///\ingroup eps_io |
46 | 46 |
///\file |
47 | 47 |
///\brief A well configurable tool for visualizing graphs |
48 | 48 |
|
49 | 49 |
namespace lemon { |
50 | 50 |
|
51 | 51 |
namespace _graph_to_eps_bits { |
52 | 52 |
template<class MT> |
53 | 53 |
class _NegY { |
54 | 54 |
public: |
55 | 55 |
typedef typename MT::Key Key; |
56 | 56 |
typedef typename MT::Value Value; |
57 | 57 |
const MT ↦ |
58 | 58 |
int yscale; |
59 | 59 |
_NegY(const MT &m,bool b) : map(m), yscale(1-b*2) {} |
60 | 60 |
Value operator[](Key n) { return Value(map[n].x,map[n].y*yscale);} |
61 | 61 |
}; |
62 | 62 |
} |
63 | 63 |
|
64 | 64 |
///Default traits class of \ref GraphToEps |
65 | 65 |
|
66 | 66 |
///Default traits class of \ref GraphToEps |
67 | 67 |
/// |
68 | 68 |
///\c G is the type of the underlying graph. |
69 | 69 |
template<class G> |
70 | 70 |
struct DefaultGraphToEpsTraits |
71 | 71 |
{ |
72 | 72 |
typedef G Graph; |
73 | 73 |
typedef typename Graph::Node Node; |
74 | 74 |
typedef typename Graph::NodeIt NodeIt; |
75 | 75 |
typedef typename Graph::Arc Arc; |
76 | 76 |
typedef typename Graph::ArcIt ArcIt; |
77 | 77 |
typedef typename Graph::InArcIt InArcIt; |
78 | 78 |
typedef typename Graph::OutArcIt OutArcIt; |
79 | 79 |
|
80 | 80 |
|
81 | 81 |
const Graph &g; |
82 | 82 |
|
83 | 83 |
std::ostream& os; |
84 | 84 |
|
85 | 85 |
typedef ConstMap<typename Graph::Node,dim2::Point<double> > CoordsMapType; |
86 | 86 |
CoordsMapType _coords; |
87 | 87 |
ConstMap<typename Graph::Node,double > _nodeSizes; |
88 | 88 |
ConstMap<typename Graph::Node,int > _nodeShapes; |
89 | 89 |
|
90 | 90 |
ConstMap<typename Graph::Node,Color > _nodeColors; |
91 | 91 |
ConstMap<typename Graph::Arc,Color > _arcColors; |
92 | 92 |
|
93 | 93 |
ConstMap<typename Graph::Arc,double > _arcWidths; |
94 | 94 |
|
95 | 95 |
double _arcWidthScale; |
96 | 96 |
|
97 | 97 |
double _nodeScale; |
98 | 98 |
double _xBorder, _yBorder; |
99 | 99 |
double _scale; |
100 | 100 |
double _nodeBorderQuotient; |
101 | 101 |
|
102 | 102 |
bool _drawArrows; |
103 | 103 |
double _arrowLength, _arrowWidth; |
104 | 104 |
|
105 | 105 |
bool _showNodes, _showArcs; |
106 | 106 |
|
107 | 107 |
bool _enableParallel; |
108 | 108 |
double _parArcDist; |
109 | 109 |
|
110 | 110 |
bool _showNodeText; |
111 | 111 |
ConstMap<typename Graph::Node,bool > _nodeTexts; |
112 | 112 |
double _nodeTextSize; |
113 | 113 |
|
114 | 114 |
bool _showNodePsText; |
115 | 115 |
ConstMap<typename Graph::Node,bool > _nodePsTexts; |
116 | 116 |
char *_nodePsTextsPreamble; |
117 | 117 |
|
118 | 118 |
bool _undirected; |
119 | 119 |
|
120 | 120 |
bool _pleaseRemoveOsStream; |
121 | 121 |
|
122 | 122 |
bool _scaleToA4; |
123 | 123 |
|
124 | 124 |
std::string _title; |
125 | 125 |
std::string _copyright; |
126 | 126 |
|
127 | 127 |
enum NodeTextColorType |
128 | 128 |
{ DIST_COL=0, DIST_BW=1, CUST_COL=2, SAME_COL=3 } _nodeTextColorType; |
129 | 129 |
ConstMap<typename Graph::Node,Color > _nodeTextColors; |
130 | 130 |
|
131 | 131 |
bool _autoNodeScale; |
132 | 132 |
bool _autoArcWidthScale; |
133 | 133 |
|
134 | 134 |
bool _absoluteNodeSizes; |
135 | 135 |
bool _absoluteArcWidths; |
136 | 136 |
|
137 | 137 |
bool _negY; |
138 | 138 |
|
139 | 139 |
bool _preScale; |
140 | 140 |
///Constructor |
141 | 141 |
|
142 | 142 |
///Constructor |
143 | 143 |
///\param _g is a reference to the graph to be printed |
144 | 144 |
///\param _os is a reference to the output stream. |
145 | 145 |
///\param _os is a reference to the output stream. |
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 |
///By default it is <tt>std::cout</tt> |
149 | 149 |
DefaultGraphToEpsTraits(const G &_g,std::ostream& _os=std::cout, |
150 | 150 |
bool _pros=false) : |
151 | 151 |
g(_g), os(_os), |
152 | 152 |
_coords(dim2::Point<double>(1,1)), _nodeSizes(1), _nodeShapes(0), |
153 | 153 |
_nodeColors(WHITE), _arcColors(BLACK), |
154 | 154 |
_arcWidths(1.0), _arcWidthScale(0.003), |
155 | 155 |
_nodeScale(.01), _xBorder(10), _yBorder(10), _scale(1.0), |
156 | 156 |
_nodeBorderQuotient(.1), |
157 | 157 |
_drawArrows(false), _arrowLength(1), _arrowWidth(0.3), |
158 | 158 |
_showNodes(true), _showArcs(true), |
159 | 159 |
_enableParallel(false), _parArcDist(1), |
160 | 160 |
_showNodeText(false), _nodeTexts(false), _nodeTextSize(1), |
161 | 161 |
_showNodePsText(false), _nodePsTexts(false), _nodePsTextsPreamble(0), |
162 | 162 |
_undirected(lemon::UndirectedTagIndicator<G>::value), |
163 | 163 |
_pleaseRemoveOsStream(_pros), _scaleToA4(false), |
164 | 164 |
_nodeTextColorType(SAME_COL), _nodeTextColors(BLACK), |
165 | 165 |
_autoNodeScale(false), |
166 | 166 |
_autoArcWidthScale(false), |
167 | 167 |
_absoluteNodeSizes(false), |
168 | 168 |
_absoluteArcWidths(false), |
169 | 169 |
_negY(false), |
170 | 170 |
_preScale(true) |
171 | 171 |
{} |
172 | 172 |
}; |
173 | 173 |
|
174 | 174 |
///Auxiliary class to implement the named parameters of \ref graphToEps() |
175 | 175 |
|
176 | 176 |
///Auxiliary class to implement the named parameters of \ref graphToEps() |
177 | 177 |
template<class T> class GraphToEps : public T |
178 | 178 |
{ |
179 | 179 |
// Can't believe it is required by the C++ standard |
180 | 180 |
using T::g; |
181 | 181 |
using T::os; |
182 | 182 |
|
183 | 183 |
using T::_coords; |
184 | 184 |
using T::_nodeSizes; |
185 | 185 |
using T::_nodeShapes; |
186 | 186 |
using T::_nodeColors; |
187 | 187 |
using T::_arcColors; |
188 | 188 |
using T::_arcWidths; |
189 | 189 |
|
190 | 190 |
using T::_arcWidthScale; |
191 | 191 |
using T::_nodeScale; |
192 | 192 |
using T::_xBorder; |
193 | 193 |
using T::_yBorder; |
194 | 194 |
using T::_scale; |
195 | 195 |
using T::_nodeBorderQuotient; |
196 | 196 |
|
197 | 197 |
using T::_drawArrows; |
198 | 198 |
using T::_arrowLength; |
199 | 199 |
using T::_arrowWidth; |
200 | 200 |
|
201 | 201 |
using T::_showNodes; |
202 | 202 |
using T::_showArcs; |
203 | 203 |
|
204 | 204 |
using T::_enableParallel; |
205 | 205 |
using T::_parArcDist; |
206 | 206 |
|
207 | 207 |
using T::_showNodeText; |
208 | 208 |
using T::_nodeTexts; |
209 | 209 |
using T::_nodeTextSize; |
210 | 210 |
|
211 | 211 |
using T::_showNodePsText; |
212 | 212 |
using T::_nodePsTexts; |
213 | 213 |
using T::_nodePsTextsPreamble; |
214 | 214 |
|
215 | 215 |
using T::_undirected; |
216 | 216 |
|
217 | 217 |
using T::_pleaseRemoveOsStream; |
218 | 218 |
|
219 | 219 |
using T::_scaleToA4; |
220 | 220 |
|
221 | 221 |
using T::_title; |
222 | 222 |
using T::_copyright; |
223 | 223 |
|
224 | 224 |
using T::NodeTextColorType; |
225 | 225 |
using T::CUST_COL; |
226 | 226 |
using T::DIST_COL; |
227 | 227 |
using T::DIST_BW; |
228 | 228 |
using T::_nodeTextColorType; |
229 | 229 |
using T::_nodeTextColors; |
230 | 230 |
|
231 | 231 |
using T::_autoNodeScale; |
232 | 232 |
using T::_autoArcWidthScale; |
233 | 233 |
|
234 | 234 |
using T::_absoluteNodeSizes; |
235 | 235 |
using T::_absoluteArcWidths; |
236 | 236 |
|
237 | 237 |
|
238 | 238 |
using T::_negY; |
239 | 239 |
using T::_preScale; |
240 | 240 |
|
241 | 241 |
// dradnats ++C eht yb deriuqer si ti eveileb t'naC |
242 | 242 |
|
243 | 243 |
typedef typename T::Graph Graph; |
244 | 244 |
typedef typename Graph::Node Node; |
245 | 245 |
typedef typename Graph::NodeIt NodeIt; |
246 | 246 |
typedef typename Graph::Arc Arc; |
247 | 247 |
typedef typename Graph::ArcIt ArcIt; |
248 | 248 |
typedef typename Graph::InArcIt InArcIt; |
249 | 249 |
typedef typename Graph::OutArcIt OutArcIt; |
250 | 250 |
|
251 | 251 |
static const int INTERPOL_PREC; |
252 | 252 |
static const double A4HEIGHT; |
253 | 253 |
static const double A4WIDTH; |
254 | 254 |
static const double A4BORDER; |
255 | 255 |
|
256 | 256 |
bool dontPrint; |
257 | 257 |
|
258 | 258 |
public: |
259 | 259 |
///Node shapes |
260 | 260 |
|
261 | 261 |
///Node shapes |
262 | 262 |
/// |
263 | 263 |
enum NodeShapes { |
264 | 264 |
/// = 0 |
265 | 265 |
///\image html nodeshape_0.png |
266 | 266 |
///\image latex nodeshape_0.eps "CIRCLE shape (0)" width=2cm |
267 | 267 |
CIRCLE=0, |
268 | 268 |
/// = 1 |
269 | 269 |
///\image html nodeshape_1.png |
270 | 270 |
///\image latex nodeshape_1.eps "SQUARE shape (1)" width=2cm |
271 | 271 |
/// |
272 | 272 |
SQUARE=1, |
273 | 273 |
/// = 2 |
274 | 274 |
///\image html nodeshape_2.png |
275 | 275 |
///\image latex nodeshape_2.eps "DIAMOND shape (2)" width=2cm |
276 | 276 |
/// |
277 | 277 |
DIAMOND=2, |
278 | 278 |
/// = 3 |
279 | 279 |
///\image html nodeshape_3.png |
280 | 280 |
///\image latex nodeshape_2.eps "MALE shape (4)" width=2cm |
281 | 281 |
/// |
282 | 282 |
MALE=3, |
283 | 283 |
/// = 4 |
284 | 284 |
///\image html nodeshape_4.png |
285 | 285 |
///\image latex nodeshape_2.eps "FEMALE shape (4)" width=2cm |
286 | 286 |
/// |
287 | 287 |
FEMALE=4 |
288 | 288 |
}; |
289 | 289 |
|
290 | 290 |
private: |
291 | 291 |
class arcLess { |
292 | 292 |
const Graph &g; |
293 | 293 |
public: |
294 | 294 |
arcLess(const Graph &_g) : g(_g) {} |
295 | 295 |
bool operator()(Arc a,Arc b) const |
296 | 296 |
{ |
297 | 297 |
Node ai=std::min(g.source(a),g.target(a)); |
298 | 298 |
Node aa=std::max(g.source(a),g.target(a)); |
299 | 299 |
Node bi=std::min(g.source(b),g.target(b)); |
300 | 300 |
Node ba=std::max(g.source(b),g.target(b)); |
301 | 301 |
return ai<bi || |
302 | 302 |
(ai==bi && (aa < ba || |
303 | 303 |
(aa==ba && ai==g.source(a) && bi==g.target(b)))); |
304 | 304 |
} |
305 | 305 |
}; |
306 | 306 |
bool isParallel(Arc e,Arc f) const |
307 | 307 |
{ |
308 | 308 |
return (g.source(e)==g.source(f)&& |
309 | 309 |
g.target(e)==g.target(f)) || |
310 | 310 |
(g.source(e)==g.target(f)&& |
311 | 311 |
g.target(e)==g.source(f)); |
312 | 312 |
} |
313 | 313 |
template<class TT> |
314 | 314 |
static std::string psOut(const dim2::Point<TT> &p) |
315 | 315 |
{ |
316 | 316 |
std::ostringstream os; |
317 | 317 |
os << p.x << ' ' << p.y; |
318 | 318 |
return os.str(); |
319 | 319 |
} |
320 | 320 |
static std::string psOut(const Color &c) |
321 | 321 |
{ |
322 | 322 |
std::ostringstream os; |
323 | 323 |
os << c.red() << ' ' << c.green() << ' ' << c.blue(); |
324 | 324 |
return os.str(); |
325 | 325 |
} |
326 | 326 |
|
327 | 327 |
public: |
328 | 328 |
GraphToEps(const T &t) : T(t), dontPrint(false) {}; |
329 | 329 |
|
330 | 330 |
template<class X> struct CoordsTraits : public T { |
331 | 331 |
typedef X CoordsMapType; |
332 | 332 |
const X &_coords; |
333 | 333 |
CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {} |
334 | 334 |
}; |
335 | 335 |
///Sets the map of the node coordinates |
336 | 336 |
|
337 | 337 |
///Sets the map of the node coordinates. |
338 | 338 |
///\param x must be a node map with dim2::Point<double> or |
339 | 339 |
///\ref dim2::Point "dim2::Point<int>" values. |
340 | 340 |
template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) { |
341 | 341 |
dontPrint=true; |
342 | 342 |
return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x)); |
343 | 343 |
} |
344 | 344 |
template<class X> struct NodeSizesTraits : public T { |
345 | 345 |
const X &_nodeSizes; |
346 | 346 |
NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {} |
347 | 347 |
}; |
348 | 348 |
///Sets the map of the node sizes |
349 | 349 |
|
350 | 350 |
///Sets the map of the node sizes |
351 | 351 |
///\param x must be a node map with \c double (or convertible) values. |
352 | 352 |
template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x) |
353 | 353 |
{ |
354 | 354 |
dontPrint=true; |
355 | 355 |
return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x)); |
356 | 356 |
} |
357 | 357 |
template<class X> struct NodeShapesTraits : public T { |
358 | 358 |
const X &_nodeShapes; |
359 | 359 |
NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {} |
360 | 360 |
}; |
361 | 361 |
///Sets the map of the node shapes |
362 | 362 |
|
363 | 363 |
///Sets the map of the node shapes. |
364 | 364 |
///The available shape values |
365 | 365 |
///can be found in \ref NodeShapes "enum NodeShapes". |
366 | 366 |
///\param x must be a node map with \c int (or convertible) values. |
367 | 367 |
///\sa NodeShapes |
368 | 368 |
template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x) |
369 | 369 |
{ |
370 | 370 |
dontPrint=true; |
371 | 371 |
return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x)); |
372 | 372 |
} |
373 | 373 |
template<class X> struct NodeTextsTraits : public T { |
374 | 374 |
const X &_nodeTexts; |
375 | 375 |
NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {} |
376 | 376 |
}; |
377 | 377 |
///Sets the text printed on the nodes |
378 | 378 |
|
379 | 379 |
///Sets the text printed on the nodes |
380 | 380 |
///\param x must be a node map with type that can be pushed to a standard |
381 | 381 |
///ostream. |
382 | 382 |
template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x) |
383 | 383 |
{ |
384 | 384 |
dontPrint=true; |
385 | 385 |
_showNodeText=true; |
386 | 386 |
return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x)); |
387 | 387 |
} |
388 | 388 |
template<class X> struct NodePsTextsTraits : public T { |
389 | 389 |
const X &_nodePsTexts; |
390 | 390 |
NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {} |
391 | 391 |
}; |
392 | 392 |
///Inserts a PostScript block to the nodes |
393 | 393 |
|
394 | 394 |
///With this command it is possible to insert a verbatim PostScript |
395 | 395 |
///block to the nodes. |
396 | 396 |
///The PS current point will be moved to the centre of the node before |
397 | 397 |
///the PostScript block inserted. |
398 | 398 |
/// |
399 | 399 |
///Before and after the block a newline character is inserted so you |
400 | 400 |
///don't have to bother with the separators. |
401 | 401 |
/// |
402 | 402 |
///\param x must be a node map with type that can be pushed to a standard |
403 | 403 |
///ostream. |
404 | 404 |
/// |
405 | 405 |
///\sa nodePsTextsPreamble() |
406 | 406 |
template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x) |
407 | 407 |
{ |
408 | 408 |
dontPrint=true; |
409 | 409 |
_showNodePsText=true; |
410 | 410 |
return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x)); |
411 | 411 |
} |
412 | 412 |
template<class X> struct ArcWidthsTraits : public T { |
413 | 413 |
const X &_arcWidths; |
414 | 414 |
ArcWidthsTraits(const T &t,const X &x) : T(t), _arcWidths(x) {} |
415 | 415 |
}; |
416 | 416 |
///Sets the map of the arc widths |
417 | 417 |
|
418 | 418 |
///Sets the map of the arc widths |
419 |
///\param x must be |
|
419 |
///\param x must be an arc map with \c double (or convertible) values. |
|
420 | 420 |
template<class X> GraphToEps<ArcWidthsTraits<X> > arcWidths(const X &x) |
421 | 421 |
{ |
422 | 422 |
dontPrint=true; |
423 | 423 |
return GraphToEps<ArcWidthsTraits<X> >(ArcWidthsTraits<X>(*this,x)); |
424 | 424 |
} |
425 | 425 |
|
426 | 426 |
template<class X> struct NodeColorsTraits : public T { |
427 | 427 |
const X &_nodeColors; |
428 | 428 |
NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {} |
429 | 429 |
}; |
430 | 430 |
///Sets the map of the node colors |
431 | 431 |
|
432 | 432 |
///Sets the map of the node colors |
433 | 433 |
///\param x must be a node map with \ref Color values. |
434 | 434 |
/// |
435 | 435 |
///\sa Palette |
436 | 436 |
template<class X> GraphToEps<NodeColorsTraits<X> > |
437 | 437 |
nodeColors(const X &x) |
438 | 438 |
{ |
439 | 439 |
dontPrint=true; |
440 | 440 |
return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x)); |
441 | 441 |
} |
442 | 442 |
template<class X> struct NodeTextColorsTraits : public T { |
443 | 443 |
const X &_nodeTextColors; |
444 | 444 |
NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {} |
445 | 445 |
}; |
446 | 446 |
///Sets the map of the node text colors |
447 | 447 |
|
448 | 448 |
///Sets the map of the node text colors |
449 | 449 |
///\param x must be a node map with \ref Color values. |
450 | 450 |
/// |
451 | 451 |
///\sa Palette |
452 | 452 |
template<class X> GraphToEps<NodeTextColorsTraits<X> > |
453 | 453 |
nodeTextColors(const X &x) |
454 | 454 |
{ |
455 | 455 |
dontPrint=true; |
456 | 456 |
_nodeTextColorType=CUST_COL; |
457 | 457 |
return GraphToEps<NodeTextColorsTraits<X> > |
458 | 458 |
(NodeTextColorsTraits<X>(*this,x)); |
459 | 459 |
} |
460 | 460 |
template<class X> struct ArcColorsTraits : public T { |
461 | 461 |
const X &_arcColors; |
462 | 462 |
ArcColorsTraits(const T &t,const X &x) : T(t), _arcColors(x) {} |
463 | 463 |
}; |
464 | 464 |
///Sets the map of the arc colors |
465 | 465 |
|
466 | 466 |
///Sets the map of the arc colors |
467 |
///\param x must be |
|
467 |
///\param x must be an arc map with \ref Color values. |
|
468 | 468 |
/// |
469 | 469 |
///\sa Palette |
470 | 470 |
template<class X> GraphToEps<ArcColorsTraits<X> > |
471 | 471 |
arcColors(const X &x) |
472 | 472 |
{ |
473 | 473 |
dontPrint=true; |
474 | 474 |
return GraphToEps<ArcColorsTraits<X> >(ArcColorsTraits<X>(*this,x)); |
475 | 475 |
} |
476 | 476 |
///Sets a global scale factor for node sizes |
477 | 477 |
|
478 | 478 |
///Sets a global scale factor for node sizes. |
479 | 479 |
/// |
480 | 480 |
/// If nodeSizes() is not given, this function simply sets the node |
481 | 481 |
/// sizes to \c d. If nodeSizes() is given, but |
482 | 482 |
/// autoNodeScale() is not, then the node size given by |
483 | 483 |
/// nodeSizes() will be multiplied by the value \c d. |
484 | 484 |
/// If both nodeSizes() and autoNodeScale() are used, then the |
485 | 485 |
/// node sizes will be scaled in such a way that the greatest size will be |
486 | 486 |
/// equal to \c d. |
487 | 487 |
/// \sa nodeSizes() |
488 | 488 |
/// \sa autoNodeScale() |
489 | 489 |
GraphToEps<T> &nodeScale(double d=.01) {_nodeScale=d;return *this;} |
490 | 490 |
///Turns on/off the automatic node width scaling. |
491 | 491 |
|
492 | 492 |
///Turns on/off the automatic node width scaling. |
493 | 493 |
/// |
494 | 494 |
///\sa nodeScale() |
495 | 495 |
/// |
496 | 496 |
GraphToEps<T> &autoNodeScale(bool b=true) { |
497 | 497 |
_autoNodeScale=b;return *this; |
498 | 498 |
} |
499 | 499 |
|
500 | 500 |
///Turns on/off the absolutematic node width scaling. |
501 | 501 |
|
502 | 502 |
///Turns on/off the absolutematic node width scaling. |
503 | 503 |
/// |
504 | 504 |
///\sa nodeScale() |
505 | 505 |
/// |
506 | 506 |
GraphToEps<T> &absoluteNodeSizes(bool b=true) { |
507 | 507 |
_absoluteNodeSizes=b;return *this; |
508 | 508 |
} |
509 | 509 |
|
510 | 510 |
///Negates the Y coordinates. |
511 | 511 |
|
512 | 512 |
///Negates the Y coordinates. |
513 | 513 |
/// |
514 | 514 |
GraphToEps<T> &negateY(bool b=true) { |
515 | 515 |
_negY=b;return *this; |
516 | 516 |
} |
517 | 517 |
|
518 | 518 |
///Turn on/off pre-scaling |
519 | 519 |
|
520 | 520 |
///By default graphToEps() rescales the whole image in order to avoid |
521 | 521 |
///very big or very small bounding boxes. |
522 | 522 |
/// |
523 | 523 |
///This (p)rescaling can be turned off with this function. |
524 | 524 |
/// |
525 | 525 |
GraphToEps<T> &preScale(bool b=true) { |
526 | 526 |
_preScale=b;return *this; |
527 | 527 |
} |
528 | 528 |
|
529 | 529 |
///Sets a global scale factor for arc widths |
530 | 530 |
|
531 | 531 |
/// Sets a global scale factor for arc widths. |
532 | 532 |
/// |
533 | 533 |
/// If arcWidths() is not given, this function simply sets the arc |
534 | 534 |
/// widths to \c d. If arcWidths() is given, but |
535 | 535 |
/// autoArcWidthScale() is not, then the arc withs given by |
536 | 536 |
/// arcWidths() will be multiplied by the value \c d. |
537 | 537 |
/// If both arcWidths() and autoArcWidthScale() are used, then the |
538 | 538 |
/// arc withs will be scaled in such a way that the greatest width will be |
539 | 539 |
/// equal to \c d. |
540 | 540 |
GraphToEps<T> &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;} |
541 | 541 |
///Turns on/off the automatic arc width scaling. |
542 | 542 |
|
543 | 543 |
///Turns on/off the automatic arc width scaling. |
544 | 544 |
/// |
545 | 545 |
///\sa arcWidthScale() |
546 | 546 |
/// |
547 | 547 |
GraphToEps<T> &autoArcWidthScale(bool b=true) { |
548 | 548 |
_autoArcWidthScale=b;return *this; |
549 | 549 |
} |
550 | 550 |
///Turns on/off the absolutematic arc width scaling. |
551 | 551 |
|
552 | 552 |
///Turns on/off the absolutematic arc width scaling. |
553 | 553 |
/// |
554 | 554 |
///\sa arcWidthScale() |
555 | 555 |
/// |
556 | 556 |
GraphToEps<T> &absoluteArcWidths(bool b=true) { |
557 | 557 |
_absoluteArcWidths=b;return *this; |
558 | 558 |
} |
559 | 559 |
///Sets a global scale factor for the whole picture |
560 | 560 |
|
561 | 561 |
///Sets a global scale factor for the whole picture |
562 | 562 |
/// |
563 | 563 |
|
564 | 564 |
GraphToEps<T> &scale(double d) {_scale=d;return *this;} |
565 | 565 |
///Sets the width of the border around the picture |
566 | 566 |
|
567 | 567 |
///Sets the width of the border around the picture |
568 | 568 |
/// |
569 | 569 |
GraphToEps<T> &border(double b=10) {_xBorder=_yBorder=b;return *this;} |
570 | 570 |
///Sets the width of the border around the picture |
571 | 571 |
|
572 | 572 |
///Sets the width of the border around the picture |
573 | 573 |
/// |
574 | 574 |
GraphToEps<T> &border(double x, double y) { |
575 | 575 |
_xBorder=x;_yBorder=y;return *this; |
576 | 576 |
} |
577 | 577 |
///Sets whether to draw arrows |
578 | 578 |
|
579 | 579 |
///Sets whether to draw arrows |
580 | 580 |
/// |
581 | 581 |
GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;} |
582 | 582 |
///Sets the length of the arrowheads |
583 | 583 |
|
584 | 584 |
///Sets the length of the arrowheads |
585 | 585 |
/// |
586 | 586 |
GraphToEps<T> &arrowLength(double d=1.0) {_arrowLength*=d;return *this;} |
587 | 587 |
///Sets the width of the arrowheads |
588 | 588 |
|
589 | 589 |
///Sets the width of the arrowheads |
590 | 590 |
/// |
591 | 591 |
GraphToEps<T> &arrowWidth(double d=.3) {_arrowWidth*=d;return *this;} |
592 | 592 |
|
593 | 593 |
///Scales the drawing to fit to A4 page |
594 | 594 |
|
595 | 595 |
///Scales the drawing to fit to A4 page |
596 | 596 |
/// |
597 | 597 |
GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;} |
598 | 598 |
|
599 | 599 |
///Enables parallel arcs |
600 | 600 |
|
601 | 601 |
///Enables parallel arcs |
602 | 602 |
GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;} |
603 | 603 |
|
604 | 604 |
///Sets the distance |
605 | 605 |
|
606 | 606 |
///Sets the distance |
607 | 607 |
/// |
608 | 608 |
GraphToEps<T> &parArcDist(double d) {_parArcDist*=d;return *this;} |
609 | 609 |
|
610 | 610 |
///Hides the arcs |
611 | 611 |
|
612 | 612 |
///Hides the arcs |
613 | 613 |
/// |
614 | 614 |
GraphToEps<T> &hideArcs(bool b=true) {_showArcs=!b;return *this;} |
615 | 615 |
///Hides the nodes |
616 | 616 |
|
617 | 617 |
///Hides the nodes |
618 | 618 |
/// |
619 | 619 |
GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;} |
620 | 620 |
|
621 | 621 |
///Sets the size of the node texts |
622 | 622 |
|
623 | 623 |
///Sets the size of the node texts |
624 | 624 |
/// |
625 | 625 |
GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;} |
626 | 626 |
|
627 | 627 |
///Sets the color of the node texts to be different from the node color |
628 | 628 |
|
629 | 629 |
///Sets the color of the node texts to be as different from the node color |
630 | 630 |
///as it is possible |
631 | 631 |
/// |
632 | 632 |
GraphToEps<T> &distantColorNodeTexts() |
633 | 633 |
{_nodeTextColorType=DIST_COL;return *this;} |
634 | 634 |
///Sets the color of the node texts to be black or white and always visible. |
635 | 635 |
|
636 | 636 |
///Sets the color of the node texts to be black or white according to |
637 | 637 |
///which is more |
638 | 638 |
///different from the node color |
639 | 639 |
/// |
640 | 640 |
GraphToEps<T> &distantBWNodeTexts() |
641 | 641 |
{_nodeTextColorType=DIST_BW;return *this;} |
642 | 642 |
|
643 | 643 |
///Gives a preamble block for node Postscript block. |
644 | 644 |
|
645 | 645 |
///Gives a preamble block for node Postscript block. |
646 | 646 |
/// |
647 | 647 |
///\sa nodePsTexts() |
648 | 648 |
GraphToEps<T> & nodePsTextsPreamble(const char *str) { |
649 | 649 |
_nodePsTextsPreamble=str ;return *this; |
650 | 650 |
} |
651 | 651 |
///Sets whether the the graph is undirected |
652 | 652 |
|
653 | 653 |
///Sets whether the the graph is undirected. |
654 | 654 |
/// |
655 | 655 |
///This setting is the default for undirected graphs. |
656 | 656 |
/// |
657 | 657 |
///\sa directed() |
658 | 658 |
GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;} |
659 | 659 |
|
660 | 660 |
///Sets whether the the graph is directed |
661 | 661 |
|
662 | 662 |
///Sets whether the the graph is directed. |
663 | 663 |
///Use it to show the edges as a pair of directed ones. |
664 | 664 |
/// |
665 | 665 |
///This setting is the default for digraphs. |
666 | 666 |
/// |
667 | 667 |
///\sa undirected() |
668 | 668 |
GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;} |
669 | 669 |
|
670 | 670 |
///Sets the title. |
671 | 671 |
|
672 | 672 |
///Sets the title of the generated image, |
673 | 673 |
///namely it inserts a <tt>%%Title:</tt> DSC field to the header of |
674 | 674 |
///the EPS file. |
675 | 675 |
GraphToEps<T> &title(const std::string &t) {_title=t;return *this;} |
676 | 676 |
///Sets the copyright statement. |
677 | 677 |
|
678 | 678 |
///Sets the copyright statement of the generated image, |
679 | 679 |
///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of |
680 | 680 |
///the EPS file. |
681 | 681 |
GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;} |
682 | 682 |
|
683 | 683 |
protected: |
684 | 684 |
bool isInsideNode(dim2::Point<double> p, double r,int t) |
685 | 685 |
{ |
686 | 686 |
switch(t) { |
687 | 687 |
case CIRCLE: |
688 | 688 |
case MALE: |
689 | 689 |
case FEMALE: |
690 | 690 |
return p.normSquare()<=r*r; |
691 | 691 |
case SQUARE: |
692 | 692 |
return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r; |
693 | 693 |
case DIAMOND: |
694 | 694 |
return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r; |
695 | 695 |
} |
696 | 696 |
return false; |
697 | 697 |
} |
698 | 698 |
|
699 | 699 |
public: |
700 | 700 |
~GraphToEps() { } |
701 | 701 |
|
702 | 702 |
///Draws the graph. |
703 | 703 |
|
704 | 704 |
///Like other functions using |
705 | 705 |
///\ref named-templ-func-param "named template parameters", |
706 | 706 |
///this function calls the algorithm itself, i.e. in this case |
707 | 707 |
///it draws the graph. |
708 | 708 |
void run() { |
709 | 709 |
//\todo better 'epsilon' would be nice here. |
710 | 710 |
const double EPSILON=1e-9; |
711 | 711 |
if(dontPrint) return; |
712 | 712 |
|
713 | 713 |
_graph_to_eps_bits::_NegY<typename T::CoordsMapType> |
714 | 714 |
mycoords(_coords,_negY); |
715 | 715 |
|
716 | 716 |
os << "%!PS-Adobe-2.0 EPSF-2.0\n"; |
717 | 717 |
if(_title.size()>0) os << "%%Title: " << _title << '\n'; |
718 | 718 |
if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n'; |
719 | 719 |
// << "%%Copyright: XXXX\n" |
720 | 720 |
os << "%%Creator: LEMON, graphToEps()\n"; |
721 | 721 |
|
722 | 722 |
{ |
723 | 723 |
#ifndef WIN32 |
724 | 724 |
timeval tv; |
725 | 725 |
gettimeofday(&tv, 0); |
726 | 726 |
|
727 | 727 |
char cbuf[26]; |
728 | 728 |
ctime_r(&tv.tv_sec,cbuf); |
729 | 729 |
os << "%%CreationDate: " << cbuf; |
730 | 730 |
#else |
731 | 731 |
SYSTEMTIME time; |
732 | 732 |
char buf1[11], buf2[9], buf3[5]; |
733 | 733 |
|
734 | 734 |
GetSystemTime(&time); |
735 | 735 |
if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time, |
736 | 736 |
"ddd MMM dd", buf1, 11) && |
737 | 737 |
GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time, |
738 | 738 |
"HH':'mm':'ss", buf2, 9) && |
739 | 739 |
GetDateFormat(LOCALE_USER_DEFAULT, 0, &time, |
740 | 740 |
"yyyy", buf3, 5)) { |
741 | 741 |
os << "%%CreationDate: " << buf1 << ' ' |
742 | 742 |
<< buf2 << ' ' << buf3 << std::endl; |
743 | 743 |
} |
744 | 744 |
#endif |
745 | 745 |
} |
746 | 746 |
|
747 | 747 |
if (_autoArcWidthScale) { |
748 | 748 |
double max_w=0; |
749 | 749 |
for(ArcIt e(g);e!=INVALID;++e) |
750 | 750 |
max_w=std::max(double(_arcWidths[e]),max_w); |
751 | 751 |
///\todo better 'epsilon' would be nice here. |
752 | 752 |
if(max_w>EPSILON) { |
753 | 753 |
_arcWidthScale/=max_w; |
754 | 754 |
} |
755 | 755 |
} |
756 | 756 |
|
757 | 757 |
if (_autoNodeScale) { |
758 | 758 |
double max_s=0; |
759 | 759 |
for(NodeIt n(g);n!=INVALID;++n) |
760 | 760 |
max_s=std::max(double(_nodeSizes[n]),max_s); |
761 | 761 |
///\todo better 'epsilon' would be nice here. |
762 | 762 |
if(max_s>EPSILON) { |
763 | 763 |
_nodeScale/=max_s; |
764 | 764 |
} |
765 | 765 |
} |
766 | 766 |
|
767 | 767 |
double diag_len = 1; |
768 | 768 |
if(!(_absoluteNodeSizes&&_absoluteArcWidths)) { |
769 | 769 |
dim2::BoundingBox<double> bb; |
770 | 770 |
for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]); |
771 | 771 |
if (bb.empty()) { |
772 | 772 |
bb = dim2::BoundingBox<double>(dim2::Point<double>(0,0)); |
773 | 773 |
} |
774 | 774 |
diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare()); |
775 | 775 |
if(diag_len<EPSILON) diag_len = 1; |
776 | 776 |
if(!_absoluteNodeSizes) _nodeScale*=diag_len; |
777 | 777 |
if(!_absoluteArcWidths) _arcWidthScale*=diag_len; |
778 | 778 |
} |
779 | 779 |
|
780 | 780 |
dim2::BoundingBox<double> bb; |
781 | 781 |
for(NodeIt n(g);n!=INVALID;++n) { |
782 | 782 |
double ns=_nodeSizes[n]*_nodeScale; |
783 | 783 |
dim2::Point<double> p(ns,ns); |
784 | 784 |
switch(_nodeShapes[n]) { |
785 | 785 |
case CIRCLE: |
786 | 786 |
case SQUARE: |
787 | 787 |
case DIAMOND: |
788 | 788 |
bb.add(p+mycoords[n]); |
789 | 789 |
bb.add(-p+mycoords[n]); |
790 | 790 |
break; |
791 | 791 |
case MALE: |
792 | 792 |
bb.add(-p+mycoords[n]); |
793 | 793 |
bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]); |
794 | 794 |
break; |
795 | 795 |
case FEMALE: |
796 | 796 |
bb.add(p+mycoords[n]); |
797 | 797 |
bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]); |
798 | 798 |
break; |
799 | 799 |
} |
800 | 800 |
} |
801 | 801 |
if (bb.empty()) { |
802 | 802 |
bb = dim2::BoundingBox<double>(dim2::Point<double>(0,0)); |
803 | 803 |
} |
804 | 804 |
|
805 | 805 |
if(_scaleToA4) |
806 | 806 |
os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n"; |
807 | 807 |
else { |
808 | 808 |
if(_preScale) { |
809 | 809 |
//Rescale so that BoundingBox won't be neither to big nor too small. |
810 | 810 |
while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10; |
811 | 811 |
while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10; |
812 | 812 |
} |
813 | 813 |
|
814 | 814 |
os << "%%BoundingBox: " |
815 | 815 |
<< int(floor(bb.left() * _scale - _xBorder)) << ' ' |
816 | 816 |
<< int(floor(bb.bottom() * _scale - _yBorder)) << ' ' |
817 | 817 |
<< int(ceil(bb.right() * _scale + _xBorder)) << ' ' |
818 | 818 |
<< int(ceil(bb.top() * _scale + _yBorder)) << '\n'; |
819 | 819 |
} |
820 | 820 |
|
821 | 821 |
os << "%%EndComments\n"; |
822 | 822 |
|
823 | 823 |
//x1 y1 x2 y2 x3 y3 cr cg cb w |
824 | 824 |
os << "/lb { setlinewidth setrgbcolor newpath moveto\n" |
825 | 825 |
<< " 4 2 roll 1 index 1 index curveto stroke } bind def\n"; |
826 | 826 |
os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke } bind def\n"; |
827 | 827 |
//x y r |
828 | 828 |
os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath } bind def\n"; |
829 | 829 |
//x y r |
830 | 830 |
os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n" |
831 | 831 |
<< " 2 index 1 index sub 2 index 2 index add lineto\n" |
832 | 832 |
<< " 2 index 1 index sub 2 index 2 index sub lineto\n" |
833 | 833 |
<< " 2 index 1 index add 2 index 2 index sub lineto\n" |
834 | 834 |
<< " closepath pop pop pop} bind def\n"; |
835 | 835 |
//x y r |
836 | 836 |
os << "/di { newpath 2 index 1 index add 2 index moveto\n" |
837 | 837 |
<< " 2 index 2 index 2 index add lineto\n" |
838 | 838 |
<< " 2 index 1 index sub 2 index lineto\n" |
839 | 839 |
<< " 2 index 2 index 2 index sub lineto\n" |
840 | 840 |
<< " closepath pop pop pop} bind def\n"; |
841 | 841 |
// x y r cr cg cb |
842 | 842 |
os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n" |
843 | 843 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
844 | 844 |
<< " } bind def\n"; |
845 | 845 |
os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n" |
846 | 846 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n" |
847 | 847 |
<< " } bind def\n"; |
848 | 848 |
os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n" |
849 | 849 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n" |
850 | 850 |
<< " } bind def\n"; |
851 | 851 |
os << "/nfemale { 0 0 0 setrgbcolor 3 index " |
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_GRAPH_UTILS_H |
20 | 20 |
#define LEMON_GRAPH_UTILS_H |
21 | 21 |
|
22 | 22 |
#include <iterator> |
23 | 23 |
#include <vector> |
24 | 24 |
#include <map> |
25 | 25 |
#include <cmath> |
26 | 26 |
#include <algorithm> |
27 | 27 |
|
28 | 28 |
#include <lemon/bits/invalid.h> |
29 | 29 |
#include <lemon/bits/utility.h> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
#include <lemon/bits/traits.h> |
32 | 32 |
|
33 | 33 |
#include <lemon/bits/alteration_notifier.h> |
34 | 34 |
#include <lemon/bits/default_map.h> |
35 | 35 |
|
36 | 36 |
///\ingroup gutils |
37 | 37 |
///\file |
38 | 38 |
///\brief Graph utilities. |
39 | 39 |
|
40 | 40 |
namespace lemon { |
41 | 41 |
|
42 | 42 |
/// \addtogroup gutils |
43 | 43 |
/// @{ |
44 | 44 |
|
45 | 45 |
///Creates convenience typedefs for the digraph types and iterators |
46 | 46 |
|
47 | 47 |
///This \c \#define creates convenience typedefs for the following types |
48 | 48 |
///of \c Digraph: \c Node, \c NodeIt, \c Arc, \c ArcIt, \c InArcIt, |
49 | 49 |
///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, |
50 | 50 |
///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. |
51 | 51 |
/// |
52 | 52 |
///\note If the graph type is a dependent type, ie. the graph type depend |
53 | 53 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
54 | 54 |
///macro. |
55 | 55 |
#define DIGRAPH_TYPEDEFS(Digraph) \ |
56 | 56 |
typedef Digraph::Node Node; \ |
57 | 57 |
typedef Digraph::NodeIt NodeIt; \ |
58 | 58 |
typedef Digraph::Arc Arc; \ |
59 | 59 |
typedef Digraph::ArcIt ArcIt; \ |
60 | 60 |
typedef Digraph::InArcIt InArcIt; \ |
61 | 61 |
typedef Digraph::OutArcIt OutArcIt; \ |
62 | 62 |
typedef Digraph::NodeMap<bool> BoolNodeMap; \ |
63 | 63 |
typedef Digraph::NodeMap<int> IntNodeMap; \ |
64 | 64 |
typedef Digraph::NodeMap<double> DoubleNodeMap; \ |
65 | 65 |
typedef Digraph::ArcMap<bool> BoolArcMap; \ |
66 | 66 |
typedef Digraph::ArcMap<int> IntArcMap; \ |
67 | 67 |
typedef Digraph::ArcMap<double> DoubleArcMap |
68 | 68 |
|
69 | 69 |
///Creates convenience typedefs for the digraph types and iterators |
70 | 70 |
|
71 | 71 |
///\see DIGRAPH_TYPEDEFS |
72 | 72 |
/// |
73 | 73 |
///\note Use this macro, if the graph type is a dependent type, |
74 | 74 |
///ie. the graph type depend on a template parameter. |
75 | 75 |
#define TEMPLATE_DIGRAPH_TYPEDEFS(Digraph) \ |
76 | 76 |
typedef typename Digraph::Node Node; \ |
77 | 77 |
typedef typename Digraph::NodeIt NodeIt; \ |
78 | 78 |
typedef typename Digraph::Arc Arc; \ |
79 | 79 |
typedef typename Digraph::ArcIt ArcIt; \ |
80 | 80 |
typedef typename Digraph::InArcIt InArcIt; \ |
81 | 81 |
typedef typename Digraph::OutArcIt OutArcIt; \ |
82 | 82 |
typedef typename Digraph::template NodeMap<bool> BoolNodeMap; \ |
83 | 83 |
typedef typename Digraph::template NodeMap<int> IntNodeMap; \ |
84 | 84 |
typedef typename Digraph::template NodeMap<double> DoubleNodeMap; \ |
85 | 85 |
typedef typename Digraph::template ArcMap<bool> BoolArcMap; \ |
86 | 86 |
typedef typename Digraph::template ArcMap<int> IntArcMap; \ |
87 | 87 |
typedef typename Digraph::template ArcMap<double> DoubleArcMap |
88 | 88 |
|
89 | 89 |
///Creates convenience typedefs for the graph types and iterators |
90 | 90 |
|
91 | 91 |
///This \c \#define creates the same convenience typedefs as defined |
92 | 92 |
///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates |
93 | 93 |
///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap, |
94 | 94 |
///\c DoubleEdgeMap. |
95 | 95 |
/// |
96 | 96 |
///\note If the graph type is a dependent type, ie. the graph type depend |
97 | 97 |
///on a template parameter, then use \c TEMPLATE_DIGRAPH_TYPEDEFS() |
98 | 98 |
///macro. |
99 | 99 |
#define GRAPH_TYPEDEFS(Graph) \ |
100 | 100 |
DIGRAPH_TYPEDEFS(Graph); \ |
101 | 101 |
typedef Graph::Edge Edge; \ |
102 | 102 |
typedef Graph::EdgeIt EdgeIt; \ |
103 | 103 |
typedef Graph::IncEdgeIt IncEdgeIt; \ |
104 | 104 |
typedef Graph::EdgeMap<bool> BoolEdgeMap; \ |
105 | 105 |
typedef Graph::EdgeMap<int> IntEdgeMap; \ |
106 | 106 |
typedef Graph::EdgeMap<double> DoubleEdgeMap |
107 | 107 |
|
108 | 108 |
///Creates convenience typedefs for the graph types and iterators |
109 | 109 |
|
110 | 110 |
///\see GRAPH_TYPEDEFS |
111 | 111 |
/// |
112 | 112 |
///\note Use this macro, if the graph type is a dependent type, |
113 | 113 |
///ie. the graph type depend on a template parameter. |
114 | 114 |
#define TEMPLATE_GRAPH_TYPEDEFS(Graph) \ |
115 | 115 |
TEMPLATE_DIGRAPH_TYPEDEFS(Graph); \ |
116 | 116 |
typedef typename Graph::Edge Edge; \ |
117 | 117 |
typedef typename Graph::EdgeIt EdgeIt; \ |
118 | 118 |
typedef typename Graph::IncEdgeIt IncEdgeIt; \ |
119 | 119 |
typedef typename Graph::template EdgeMap<bool> BoolEdgeMap; \ |
120 | 120 |
typedef typename Graph::template EdgeMap<int> IntEdgeMap; \ |
121 | 121 |
typedef typename Graph::template EdgeMap<double> DoubleEdgeMap |
122 | 122 |
|
123 | 123 |
/// \brief Function to count the items in the graph. |
124 | 124 |
/// |
125 | 125 |
/// This function counts the items (nodes, arcs etc) in the graph. |
126 | 126 |
/// The complexity of the function is O(n) because |
127 | 127 |
/// it iterates on all of the items. |
128 | 128 |
template <typename Graph, typename Item> |
129 | 129 |
inline int countItems(const Graph& g) { |
130 | 130 |
typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt; |
131 | 131 |
int num = 0; |
132 | 132 |
for (ItemIt it(g); it != INVALID; ++it) { |
133 | 133 |
++num; |
134 | 134 |
} |
135 | 135 |
return num; |
136 | 136 |
} |
137 | 137 |
|
138 | 138 |
// Node counting: |
139 | 139 |
|
140 | 140 |
namespace _graph_utils_bits { |
141 | 141 |
|
142 | 142 |
template <typename Graph, typename Enable = void> |
143 | 143 |
struct CountNodesSelector { |
144 | 144 |
static int count(const Graph &g) { |
145 | 145 |
return countItems<Graph, typename Graph::Node>(g); |
146 | 146 |
} |
147 | 147 |
}; |
148 | 148 |
|
149 | 149 |
template <typename Graph> |
150 | 150 |
struct CountNodesSelector< |
151 | 151 |
Graph, typename |
152 | 152 |
enable_if<typename Graph::NodeNumTag, void>::type> |
153 | 153 |
{ |
154 | 154 |
static int count(const Graph &g) { |
155 | 155 |
return g.nodeNum(); |
156 | 156 |
} |
157 | 157 |
}; |
158 | 158 |
} |
159 | 159 |
|
160 | 160 |
/// \brief Function to count the nodes in the graph. |
161 | 161 |
/// |
162 | 162 |
/// This function counts the nodes in the graph. |
163 | 163 |
/// The complexity of the function is O(n) but for some |
164 | 164 |
/// graph structures it is specialized to run in O(1). |
165 | 165 |
/// |
166 | 166 |
/// If the graph contains a \e nodeNum() member function and a |
167 | 167 |
/// \e NodeNumTag tag then this function calls directly the member |
168 | 168 |
/// function to query the cardinality of the node set. |
169 | 169 |
template <typename Graph> |
170 | 170 |
inline int countNodes(const Graph& g) { |
171 | 171 |
return _graph_utils_bits::CountNodesSelector<Graph>::count(g); |
172 | 172 |
} |
173 | 173 |
|
174 | 174 |
// Arc counting: |
175 | 175 |
|
176 | 176 |
namespace _graph_utils_bits { |
177 | 177 |
|
178 | 178 |
template <typename Graph, typename Enable = void> |
179 | 179 |
struct CountArcsSelector { |
180 | 180 |
static int count(const Graph &g) { |
181 | 181 |
return countItems<Graph, typename Graph::Arc>(g); |
182 | 182 |
} |
183 | 183 |
}; |
184 | 184 |
|
185 | 185 |
template <typename Graph> |
186 | 186 |
struct CountArcsSelector< |
187 | 187 |
Graph, |
188 | 188 |
typename enable_if<typename Graph::ArcNumTag, void>::type> |
189 | 189 |
{ |
190 | 190 |
static int count(const Graph &g) { |
191 | 191 |
return g.arcNum(); |
192 | 192 |
} |
193 | 193 |
}; |
194 | 194 |
} |
195 | 195 |
|
196 | 196 |
/// \brief Function to count the arcs in the graph. |
197 | 197 |
/// |
198 | 198 |
/// This function counts the arcs in the graph. |
199 | 199 |
/// The complexity of the function is O(e) but for some |
200 | 200 |
/// graph structures it is specialized to run in O(1). |
201 | 201 |
/// |
202 | 202 |
/// If the graph contains a \e arcNum() member function and a |
203 | 203 |
/// \e EdgeNumTag tag then this function calls directly the member |
204 | 204 |
/// function to query the cardinality of the arc set. |
205 | 205 |
template <typename Graph> |
206 | 206 |
inline int countArcs(const Graph& g) { |
207 | 207 |
return _graph_utils_bits::CountArcsSelector<Graph>::count(g); |
208 | 208 |
} |
209 | 209 |
|
210 | 210 |
// Edge counting: |
211 | 211 |
namespace _graph_utils_bits { |
212 | 212 |
|
213 | 213 |
template <typename Graph, typename Enable = void> |
214 | 214 |
struct CountEdgesSelector { |
215 | 215 |
static int count(const Graph &g) { |
216 | 216 |
return countItems<Graph, typename Graph::Edge>(g); |
217 | 217 |
} |
218 | 218 |
}; |
219 | 219 |
|
220 | 220 |
template <typename Graph> |
221 | 221 |
struct CountEdgesSelector< |
222 | 222 |
Graph, |
223 | 223 |
typename enable_if<typename Graph::EdgeNumTag, void>::type> |
224 | 224 |
{ |
225 | 225 |
static int count(const Graph &g) { |
226 | 226 |
return g.edgeNum(); |
227 | 227 |
} |
228 | 228 |
}; |
229 | 229 |
} |
230 | 230 |
|
231 | 231 |
/// \brief Function to count the edges in the graph. |
232 | 232 |
/// |
233 | 233 |
/// This function counts the edges in the graph. |
234 | 234 |
/// The complexity of the function is O(m) but for some |
235 | 235 |
/// graph structures it is specialized to run in O(1). |
236 | 236 |
/// |
237 | 237 |
/// If the graph contains a \e edgeNum() member function and a |
238 | 238 |
/// \e EdgeNumTag tag then this function calls directly the member |
239 | 239 |
/// function to query the cardinality of the edge set. |
240 | 240 |
template <typename Graph> |
241 | 241 |
inline int countEdges(const Graph& g) { |
242 | 242 |
return _graph_utils_bits::CountEdgesSelector<Graph>::count(g); |
243 | 243 |
|
244 | 244 |
} |
245 | 245 |
|
246 | 246 |
|
247 | 247 |
template <typename Graph, typename DegIt> |
248 | 248 |
inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) { |
249 | 249 |
int num = 0; |
250 | 250 |
for (DegIt it(_g, _n); it != INVALID; ++it) { |
251 | 251 |
++num; |
252 | 252 |
} |
253 | 253 |
return num; |
254 | 254 |
} |
255 | 255 |
|
256 | 256 |
/// \brief Function to count the number of the out-arcs from node \c n. |
257 | 257 |
/// |
258 | 258 |
/// This function counts the number of the out-arcs from node \c n |
259 | 259 |
/// in the graph. |
260 | 260 |
template <typename Graph> |
261 | 261 |
inline int countOutArcs(const Graph& _g, const typename Graph::Node& _n) { |
262 | 262 |
return countNodeDegree<Graph, typename Graph::OutArcIt>(_g, _n); |
263 | 263 |
} |
264 | 264 |
|
265 | 265 |
/// \brief Function to count the number of the in-arcs to node \c n. |
266 | 266 |
/// |
267 | 267 |
/// This function counts the number of the in-arcs to node \c n |
268 | 268 |
/// in the graph. |
269 | 269 |
template <typename Graph> |
270 | 270 |
inline int countInArcs(const Graph& _g, const typename Graph::Node& _n) { |
271 | 271 |
return countNodeDegree<Graph, typename Graph::InArcIt>(_g, _n); |
272 | 272 |
} |
273 | 273 |
|
274 | 274 |
/// \brief Function to count the number of the inc-edges to node \c n. |
275 | 275 |
/// |
276 | 276 |
/// This function counts the number of the inc-edges to node \c n |
277 | 277 |
/// in the graph. |
278 | 278 |
template <typename Graph> |
279 | 279 |
inline int countIncEdges(const Graph& _g, const typename Graph::Node& _n) { |
280 | 280 |
return countNodeDegree<Graph, typename Graph::IncEdgeIt>(_g, _n); |
281 | 281 |
} |
282 | 282 |
|
283 | 283 |
namespace _graph_utils_bits { |
284 | 284 |
|
285 | 285 |
template <typename Graph, typename Enable = void> |
286 | 286 |
struct FindArcSelector { |
287 | 287 |
typedef typename Graph::Node Node; |
288 | 288 |
typedef typename Graph::Arc Arc; |
289 | 289 |
static Arc find(const Graph &g, Node u, Node v, Arc e) { |
290 | 290 |
if (e == INVALID) { |
291 | 291 |
g.firstOut(e, u); |
292 | 292 |
} else { |
293 | 293 |
g.nextOut(e); |
294 | 294 |
} |
295 | 295 |
while (e != INVALID && g.target(e) != v) { |
296 | 296 |
g.nextOut(e); |
297 | 297 |
} |
298 | 298 |
return e; |
299 | 299 |
} |
300 | 300 |
}; |
301 | 301 |
|
302 | 302 |
template <typename Graph> |
303 | 303 |
struct FindArcSelector< |
304 | 304 |
Graph, |
305 | 305 |
typename enable_if<typename Graph::FindEdgeTag, void>::type> |
306 | 306 |
{ |
307 | 307 |
typedef typename Graph::Node Node; |
308 | 308 |
typedef typename Graph::Arc Arc; |
309 | 309 |
static Arc find(const Graph &g, Node u, Node v, Arc prev) { |
310 | 310 |
return g.findArc(u, v, prev); |
311 | 311 |
} |
312 | 312 |
}; |
313 | 313 |
} |
314 | 314 |
|
315 | 315 |
/// \brief Finds an arc between two nodes of a graph. |
316 | 316 |
/// |
317 | 317 |
/// Finds an arc from node \c u to node \c v in graph \c g. |
318 | 318 |
/// |
319 | 319 |
/// If \c prev is \ref INVALID (this is the default value), then |
320 | 320 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
321 | 321 |
/// the next arc from \c u to \c v after \c prev. |
322 | 322 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
323 | 323 |
/// |
324 | 324 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
325 | 325 |
///\code |
326 | 326 |
/// for(Arc e=findArc(g,u,v);e!=INVALID;e=findArc(g,u,v,e)) { |
327 | 327 |
/// ... |
328 | 328 |
/// } |
329 | 329 |
///\endcode |
330 | 330 |
/// |
331 | 331 |
///\sa ArcLookUp |
332 | 332 |
///\sa AllArcLookUp |
333 | 333 |
///\sa DynArcLookUp |
334 | 334 |
///\sa ConArcIt |
335 | 335 |
template <typename Graph> |
336 | 336 |
inline typename Graph::Arc |
337 | 337 |
findArc(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
338 | 338 |
typename Graph::Arc prev = INVALID) { |
339 | 339 |
return _graph_utils_bits::FindArcSelector<Graph>::find(g, u, v, prev); |
340 | 340 |
} |
341 | 341 |
|
342 | 342 |
/// \brief Iterator for iterating on arcs connected the same nodes. |
343 | 343 |
/// |
344 | 344 |
/// Iterator for iterating on arcs connected the same nodes. It is |
345 | 345 |
/// higher level interface for the findArc() function. You can |
346 | 346 |
/// use it the following way: |
347 | 347 |
///\code |
348 | 348 |
/// for (ConArcIt<Graph> it(g, src, trg); it != INVALID; ++it) { |
349 | 349 |
/// ... |
350 | 350 |
/// } |
351 | 351 |
///\endcode |
352 | 352 |
/// |
353 | 353 |
///\sa findArc() |
354 | 354 |
///\sa ArcLookUp |
355 | 355 |
///\sa AllArcLookUp |
356 | 356 |
///\sa DynArcLookUp |
357 |
/// |
|
358 |
/// \author Balazs Dezso |
|
359 | 357 |
template <typename _Graph> |
360 | 358 |
class ConArcIt : public _Graph::Arc { |
361 | 359 |
public: |
362 | 360 |
|
363 | 361 |
typedef _Graph Graph; |
364 | 362 |
typedef typename Graph::Arc Parent; |
365 | 363 |
|
366 | 364 |
typedef typename Graph::Arc Arc; |
367 | 365 |
typedef typename Graph::Node Node; |
368 | 366 |
|
369 | 367 |
/// \brief Constructor. |
370 | 368 |
/// |
371 | 369 |
/// Construct a new ConArcIt iterating on the arcs which |
372 | 370 |
/// connects the \c u and \c v node. |
373 | 371 |
ConArcIt(const Graph& g, Node u, Node v) : _graph(g) { |
374 | 372 |
Parent::operator=(findArc(_graph, u, v)); |
375 | 373 |
} |
376 | 374 |
|
377 | 375 |
/// \brief Constructor. |
378 | 376 |
/// |
379 | 377 |
/// Construct a new ConArcIt which continues the iterating from |
380 | 378 |
/// the \c e arc. |
381 | 379 |
ConArcIt(const Graph& g, Arc a) : Parent(a), _graph(g) {} |
382 | 380 |
|
383 | 381 |
/// \brief Increment operator. |
384 | 382 |
/// |
385 | 383 |
/// It increments the iterator and gives back the next arc. |
386 | 384 |
ConArcIt& operator++() { |
387 | 385 |
Parent::operator=(findArc(_graph, _graph.source(*this), |
388 | 386 |
_graph.target(*this), *this)); |
389 | 387 |
return *this; |
390 | 388 |
} |
391 | 389 |
private: |
392 | 390 |
const Graph& _graph; |
393 | 391 |
}; |
394 | 392 |
|
395 | 393 |
namespace _graph_utils_bits { |
396 | 394 |
|
397 | 395 |
template <typename Graph, typename Enable = void> |
398 | 396 |
struct FindEdgeSelector { |
399 | 397 |
typedef typename Graph::Node Node; |
400 | 398 |
typedef typename Graph::Edge Edge; |
401 | 399 |
static Edge find(const Graph &g, Node u, Node v, Edge e) { |
402 | 400 |
bool b; |
403 | 401 |
if (u != v) { |
404 | 402 |
if (e == INVALID) { |
405 | 403 |
g.firstInc(e, b, u); |
406 | 404 |
} else { |
407 | 405 |
b = g.source(e) == u; |
408 | 406 |
g.nextInc(e, b); |
409 | 407 |
} |
410 | 408 |
while (e != INVALID && (b ? g.target(e) : g.source(e)) != v) { |
411 | 409 |
g.nextInc(e, b); |
412 | 410 |
} |
413 | 411 |
} else { |
414 | 412 |
if (e == INVALID) { |
415 | 413 |
g.firstInc(e, b, u); |
416 | 414 |
} else { |
417 | 415 |
b = true; |
418 | 416 |
g.nextInc(e, b); |
419 | 417 |
} |
420 | 418 |
while (e != INVALID && (!b || g.target(e) != v)) { |
421 | 419 |
g.nextInc(e, b); |
422 | 420 |
} |
423 | 421 |
} |
424 | 422 |
return e; |
425 | 423 |
} |
426 | 424 |
}; |
427 | 425 |
|
428 | 426 |
template <typename Graph> |
429 | 427 |
struct FindEdgeSelector< |
430 | 428 |
Graph, |
431 | 429 |
typename enable_if<typename Graph::FindEdgeTag, void>::type> |
432 | 430 |
{ |
433 | 431 |
typedef typename Graph::Node Node; |
434 | 432 |
typedef typename Graph::Edge Edge; |
435 | 433 |
static Edge find(const Graph &g, Node u, Node v, Edge prev) { |
436 | 434 |
return g.findEdge(u, v, prev); |
437 | 435 |
} |
438 | 436 |
}; |
439 | 437 |
} |
440 | 438 |
|
441 | 439 |
/// \brief Finds an edge between two nodes of a graph. |
442 | 440 |
/// |
443 | 441 |
/// Finds an edge from node \c u to node \c v in graph \c g. |
444 | 442 |
/// If the node \c u and node \c v is equal then each loop edge |
445 | 443 |
/// will be enumerated once. |
446 | 444 |
/// |
447 | 445 |
/// If \c prev is \ref INVALID (this is the default value), then |
448 | 446 |
/// it finds the first arc from \c u to \c v. Otherwise it looks for |
449 | 447 |
/// the next arc from \c u to \c v after \c prev. |
450 | 448 |
/// \return The found arc or \ref INVALID if there is no such an arc. |
451 | 449 |
/// |
452 | 450 |
/// Thus you can iterate through each arc from \c u to \c v as it follows. |
453 | 451 |
///\code |
454 | 452 |
/// for(Edge e = findEdge(g,u,v); e != INVALID; |
455 | 453 |
/// e = findEdge(g,u,v,e)) { |
456 | 454 |
/// ... |
457 | 455 |
/// } |
458 | 456 |
///\endcode |
459 | 457 |
/// |
460 | 458 |
///\sa ConArcIt |
461 | 459 |
|
462 | 460 |
template <typename Graph> |
463 | 461 |
inline typename Graph::Edge |
464 | 462 |
findEdge(const Graph &g, typename Graph::Node u, typename Graph::Node v, |
465 | 463 |
typename Graph::Edge p = INVALID) { |
466 | 464 |
return _graph_utils_bits::FindEdgeSelector<Graph>::find(g, u, v, p); |
467 | 465 |
} |
468 | 466 |
|
469 | 467 |
/// \brief Iterator for iterating on edges connected the same nodes. |
470 | 468 |
/// |
471 | 469 |
/// Iterator for iterating on edges connected the same nodes. It is |
472 | 470 |
/// higher level interface for the findEdge() function. You can |
473 | 471 |
/// use it the following way: |
474 | 472 |
///\code |
475 | 473 |
/// for (ConEdgeIt<Graph> it(g, src, trg); it != INVALID; ++it) { |
476 | 474 |
/// ... |
477 | 475 |
/// } |
478 | 476 |
///\endcode |
479 | 477 |
/// |
480 | 478 |
///\sa findEdge() |
481 |
/// |
|
482 |
/// \author Balazs Dezso |
|
483 | 479 |
template <typename _Graph> |
484 | 480 |
class ConEdgeIt : public _Graph::Edge { |
485 | 481 |
public: |
486 | 482 |
|
487 | 483 |
typedef _Graph Graph; |
488 | 484 |
typedef typename Graph::Edge Parent; |
489 | 485 |
|
490 | 486 |
typedef typename Graph::Edge Edge; |
491 | 487 |
typedef typename Graph::Node Node; |
492 | 488 |
|
493 | 489 |
/// \brief Constructor. |
494 | 490 |
/// |
495 | 491 |
/// Construct a new ConEdgeIt iterating on the edges which |
496 | 492 |
/// connects the \c u and \c v node. |
497 | 493 |
ConEdgeIt(const Graph& g, Node u, Node v) : _graph(g) { |
498 | 494 |
Parent::operator=(findEdge(_graph, u, v)); |
499 | 495 |
} |
500 | 496 |
|
501 | 497 |
/// \brief Constructor. |
502 | 498 |
/// |
503 | 499 |
/// Construct a new ConEdgeIt which continues the iterating from |
504 | 500 |
/// the \c e edge. |
505 | 501 |
ConEdgeIt(const Graph& g, Edge e) : Parent(e), _graph(g) {} |
506 | 502 |
|
507 | 503 |
/// \brief Increment operator. |
508 | 504 |
/// |
509 | 505 |
/// It increments the iterator and gives back the next edge. |
510 | 506 |
ConEdgeIt& operator++() { |
511 | 507 |
Parent::operator=(findEdge(_graph, _graph.source(*this), |
512 | 508 |
_graph.target(*this), *this)); |
513 | 509 |
return *this; |
514 | 510 |
} |
515 | 511 |
private: |
516 | 512 |
const Graph& _graph; |
517 | 513 |
}; |
518 | 514 |
|
519 | 515 |
namespace _graph_utils_bits { |
520 | 516 |
|
521 | 517 |
template <typename Digraph, typename Item, typename RefMap> |
522 | 518 |
class MapCopyBase { |
523 | 519 |
public: |
524 | 520 |
virtual void copy(const Digraph& from, const RefMap& refMap) = 0; |
525 | 521 |
|
526 | 522 |
virtual ~MapCopyBase() {} |
527 | 523 |
}; |
528 | 524 |
|
529 | 525 |
template <typename Digraph, typename Item, typename RefMap, |
530 | 526 |
typename ToMap, typename FromMap> |
531 | 527 |
class MapCopy : public MapCopyBase<Digraph, Item, RefMap> { |
532 | 528 |
public: |
533 | 529 |
|
534 | 530 |
MapCopy(ToMap& tmap, const FromMap& map) |
535 | 531 |
: _tmap(tmap), _map(map) {} |
536 | 532 |
|
537 | 533 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
538 | 534 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
539 | 535 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
540 | 536 |
_tmap.set(refMap[it], _map[it]); |
541 | 537 |
} |
542 | 538 |
} |
543 | 539 |
|
544 | 540 |
private: |
545 | 541 |
ToMap& _tmap; |
546 | 542 |
const FromMap& _map; |
547 | 543 |
}; |
548 | 544 |
|
549 | 545 |
template <typename Digraph, typename Item, typename RefMap, typename It> |
550 | 546 |
class ItemCopy : public MapCopyBase<Digraph, Item, RefMap> { |
551 | 547 |
public: |
552 | 548 |
|
553 | 549 |
ItemCopy(It& it, const Item& item) : _it(it), _item(item) {} |
554 | 550 |
|
555 | 551 |
virtual void copy(const Digraph&, const RefMap& refMap) { |
556 | 552 |
_it = refMap[_item]; |
557 | 553 |
} |
558 | 554 |
|
559 | 555 |
private: |
560 | 556 |
It& _it; |
561 | 557 |
Item _item; |
562 | 558 |
}; |
563 | 559 |
|
564 | 560 |
template <typename Digraph, typename Item, typename RefMap, typename Ref> |
565 | 561 |
class RefCopy : public MapCopyBase<Digraph, Item, RefMap> { |
566 | 562 |
public: |
567 | 563 |
|
568 | 564 |
RefCopy(Ref& map) : _map(map) {} |
569 | 565 |
|
570 | 566 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
571 | 567 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
572 | 568 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
573 | 569 |
_map.set(it, refMap[it]); |
574 | 570 |
} |
575 | 571 |
} |
576 | 572 |
|
577 | 573 |
private: |
578 | 574 |
Ref& _map; |
579 | 575 |
}; |
580 | 576 |
|
581 | 577 |
template <typename Digraph, typename Item, typename RefMap, |
582 | 578 |
typename CrossRef> |
583 | 579 |
class CrossRefCopy : public MapCopyBase<Digraph, Item, RefMap> { |
584 | 580 |
public: |
585 | 581 |
|
586 | 582 |
CrossRefCopy(CrossRef& cmap) : _cmap(cmap) {} |
587 | 583 |
|
588 | 584 |
virtual void copy(const Digraph& digraph, const RefMap& refMap) { |
589 | 585 |
typedef typename ItemSetTraits<Digraph, Item>::ItemIt ItemIt; |
590 | 586 |
for (ItemIt it(digraph); it != INVALID; ++it) { |
591 | 587 |
_cmap.set(refMap[it], it); |
592 | 588 |
} |
593 | 589 |
} |
594 | 590 |
|
595 | 591 |
private: |
596 | 592 |
CrossRef& _cmap; |
597 | 593 |
}; |
598 | 594 |
|
599 | 595 |
template <typename Digraph, typename Enable = void> |
600 | 596 |
struct DigraphCopySelector { |
601 | 597 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
602 | 598 |
static void copy(Digraph &to, const From& from, |
603 | 599 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
604 | 600 |
for (typename From::NodeIt it(from); it != INVALID; ++it) { |
605 | 601 |
nodeRefMap[it] = to.addNode(); |
606 | 602 |
} |
607 | 603 |
for (typename From::ArcIt it(from); it != INVALID; ++it) { |
608 | 604 |
arcRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
609 | 605 |
nodeRefMap[from.target(it)]); |
610 | 606 |
} |
611 | 607 |
} |
612 | 608 |
}; |
613 | 609 |
|
614 | 610 |
template <typename Digraph> |
615 | 611 |
struct DigraphCopySelector< |
616 | 612 |
Digraph, |
617 | 613 |
typename enable_if<typename Digraph::BuildTag, void>::type> |
618 | 614 |
{ |
619 | 615 |
template <typename From, typename NodeRefMap, typename ArcRefMap> |
620 | 616 |
static void copy(Digraph &to, const From& from, |
621 | 617 |
NodeRefMap& nodeRefMap, ArcRefMap& arcRefMap) { |
622 | 618 |
to.build(from, nodeRefMap, arcRefMap); |
623 | 619 |
} |
624 | 620 |
}; |
625 | 621 |
|
626 | 622 |
template <typename Graph, typename Enable = void> |
627 | 623 |
struct GraphCopySelector { |
628 | 624 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
629 | 625 |
static void copy(Graph &to, const From& from, |
630 | 626 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
631 | 627 |
for (typename From::NodeIt it(from); it != INVALID; ++it) { |
632 | 628 |
nodeRefMap[it] = to.addNode(); |
633 | 629 |
} |
634 | 630 |
for (typename From::EdgeIt it(from); it != INVALID; ++it) { |
635 | 631 |
edgeRefMap[it] = to.addArc(nodeRefMap[from.source(it)], |
636 | 632 |
nodeRefMap[from.target(it)]); |
637 | 633 |
} |
638 | 634 |
} |
639 | 635 |
}; |
640 | 636 |
|
641 | 637 |
template <typename Graph> |
642 | 638 |
struct GraphCopySelector< |
643 | 639 |
Graph, |
644 | 640 |
typename enable_if<typename Graph::BuildTag, void>::type> |
645 | 641 |
{ |
646 | 642 |
template <typename From, typename NodeRefMap, typename EdgeRefMap> |
647 | 643 |
static void copy(Graph &to, const From& from, |
648 | 644 |
NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { |
649 | 645 |
to.build(from, nodeRefMap, edgeRefMap); |
650 | 646 |
} |
651 | 647 |
}; |
652 | 648 |
|
653 | 649 |
} |
654 | 650 |
|
655 | 651 |
/// \brief Class to copy a digraph. |
656 | 652 |
/// |
657 | 653 |
/// Class to copy a digraph to another digraph (duplicate a digraph). The |
658 | 654 |
/// simplest way of using it is through the \c copyDigraph() function. |
659 | 655 |
/// |
660 | 656 |
/// This class not just make a copy of a graph, but it can create |
661 | 657 |
/// references and cross references between the nodes and arcs of |
662 | 658 |
/// the two graphs, it can copy maps for use with the newly created |
663 | 659 |
/// graph and copy nodes and arcs. |
664 | 660 |
/// |
665 | 661 |
/// To make a copy from a graph, first an instance of DigraphCopy |
666 | 662 |
/// should be created, then the data belongs to the graph should |
667 | 663 |
/// assigned to copy. In the end, the \c run() member should be |
668 | 664 |
/// called. |
669 | 665 |
/// |
670 | 666 |
/// The next code copies a graph with several data: |
671 | 667 |
///\code |
672 | 668 |
/// DigraphCopy<NewGraph, OrigGraph> dc(new_graph, orig_graph); |
673 | 669 |
/// // create a reference for the nodes |
674 | 670 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
675 | 671 |
/// dc.nodeRef(nr); |
676 | 672 |
/// // create a cross reference (inverse) for the arcs |
677 | 673 |
/// NewGraph::ArcMap<OrigGraph::Arc> acr(new_graph); |
678 | 674 |
/// dc.arcCrossRef(acr); |
679 | 675 |
/// // copy an arc map |
680 | 676 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
681 | 677 |
/// NewGraph::ArcMap<double> namap(new_graph); |
682 | 678 |
/// dc.arcMap(namap, oamap); |
683 | 679 |
/// // copy a node |
684 | 680 |
/// OrigGraph::Node on; |
685 | 681 |
/// NewGraph::Node nn; |
686 | 682 |
/// dc.node(nn, on); |
687 | 683 |
/// // Executions of copy |
688 | 684 |
/// dc.run(); |
689 | 685 |
///\endcode |
690 | 686 |
template <typename To, typename From> |
691 | 687 |
class DigraphCopy { |
692 | 688 |
private: |
693 | 689 |
|
694 | 690 |
typedef typename From::Node Node; |
695 | 691 |
typedef typename From::NodeIt NodeIt; |
696 | 692 |
typedef typename From::Arc Arc; |
697 | 693 |
typedef typename From::ArcIt ArcIt; |
698 | 694 |
|
699 | 695 |
typedef typename To::Node TNode; |
700 | 696 |
typedef typename To::Arc TArc; |
701 | 697 |
|
702 | 698 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
703 | 699 |
typedef typename From::template ArcMap<TArc> ArcRefMap; |
704 | 700 |
|
705 | 701 |
|
706 | 702 |
public: |
707 | 703 |
|
708 | 704 |
|
709 | 705 |
/// \brief Constructor for the DigraphCopy. |
710 | 706 |
/// |
711 | 707 |
/// It copies the content of the \c _from digraph into the |
712 | 708 |
/// \c _to digraph. |
713 | 709 |
DigraphCopy(To& to, const From& from) |
714 | 710 |
: _from(from), _to(to) {} |
715 | 711 |
|
716 | 712 |
/// \brief Destructor of the DigraphCopy |
717 | 713 |
/// |
718 | 714 |
/// Destructor of the DigraphCopy |
719 | 715 |
~DigraphCopy() { |
720 | 716 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
721 | 717 |
delete _node_maps[i]; |
722 | 718 |
} |
723 | 719 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
724 | 720 |
delete _arc_maps[i]; |
725 | 721 |
} |
726 | 722 |
|
727 | 723 |
} |
728 | 724 |
|
729 | 725 |
/// \brief Copies the node references into the given map. |
730 | 726 |
/// |
731 | 727 |
/// Copies the node references into the given map. The parameter |
732 | 728 |
/// should be a map, which key type is the Node type of the source |
733 | 729 |
/// graph, while the value type is the Node type of the |
734 | 730 |
/// destination graph. |
735 | 731 |
template <typename NodeRef> |
736 | 732 |
DigraphCopy& nodeRef(NodeRef& map) { |
737 | 733 |
_node_maps.push_back(new _graph_utils_bits::RefCopy<From, Node, |
738 | 734 |
NodeRefMap, NodeRef>(map)); |
739 | 735 |
return *this; |
740 | 736 |
} |
741 | 737 |
|
742 | 738 |
/// \brief Copies the node cross references into the given map. |
743 | 739 |
/// |
744 | 740 |
/// Copies the node cross references (reverse references) into |
745 | 741 |
/// the given map. The parameter should be a map, which key type |
746 | 742 |
/// is the Node type of the destination graph, while the value type is |
747 | 743 |
/// the Node type of the source graph. |
748 | 744 |
template <typename NodeCrossRef> |
749 | 745 |
DigraphCopy& nodeCrossRef(NodeCrossRef& map) { |
750 | 746 |
_node_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Node, |
751 | 747 |
NodeRefMap, NodeCrossRef>(map)); |
752 | 748 |
return *this; |
753 | 749 |
} |
754 | 750 |
|
755 | 751 |
/// \brief Make copy of the given map. |
756 | 752 |
/// |
757 | 753 |
/// Makes copy of the given map for the newly created digraph. |
758 | 754 |
/// The new map's key type is the destination graph's node type, |
759 | 755 |
/// and the copied map's key type is the source graph's node type. |
760 | 756 |
template <typename ToMap, typename FromMap> |
761 | 757 |
DigraphCopy& nodeMap(ToMap& tmap, const FromMap& map) { |
762 | 758 |
_node_maps.push_back(new _graph_utils_bits::MapCopy<From, Node, |
763 | 759 |
NodeRefMap, ToMap, FromMap>(tmap, map)); |
764 | 760 |
return *this; |
765 | 761 |
} |
766 | 762 |
|
767 | 763 |
/// \brief Make a copy of the given node. |
768 | 764 |
/// |
769 | 765 |
/// Make a copy of the given node. |
770 | 766 |
DigraphCopy& node(TNode& tnode, const Node& snode) { |
771 | 767 |
_node_maps.push_back(new _graph_utils_bits::ItemCopy<From, Node, |
772 | 768 |
NodeRefMap, TNode>(tnode, snode)); |
773 | 769 |
return *this; |
774 | 770 |
} |
775 | 771 |
|
776 | 772 |
/// \brief Copies the arc references into the given map. |
777 | 773 |
/// |
778 | 774 |
/// Copies the arc references into the given map. |
779 | 775 |
template <typename ArcRef> |
780 | 776 |
DigraphCopy& arcRef(ArcRef& map) { |
781 | 777 |
_arc_maps.push_back(new _graph_utils_bits::RefCopy<From, Arc, |
782 | 778 |
ArcRefMap, ArcRef>(map)); |
783 | 779 |
return *this; |
784 | 780 |
} |
785 | 781 |
|
786 | 782 |
/// \brief Copies the arc cross references into the given map. |
787 | 783 |
/// |
788 | 784 |
/// Copies the arc cross references (reverse references) into |
789 | 785 |
/// the given map. |
790 | 786 |
template <typename ArcCrossRef> |
791 | 787 |
DigraphCopy& arcCrossRef(ArcCrossRef& map) { |
792 | 788 |
_arc_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Arc, |
793 | 789 |
ArcRefMap, ArcCrossRef>(map)); |
794 | 790 |
return *this; |
795 | 791 |
} |
796 | 792 |
|
797 | 793 |
/// \brief Make copy of the given map. |
798 | 794 |
/// |
799 | 795 |
/// Makes copy of the given map for the newly created digraph. |
800 | 796 |
/// The new map's key type is the to digraph's arc type, |
801 | 797 |
/// and the copied map's key type is the from digraph's arc |
802 | 798 |
/// type. |
803 | 799 |
template <typename ToMap, typename FromMap> |
804 | 800 |
DigraphCopy& arcMap(ToMap& tmap, const FromMap& map) { |
805 | 801 |
_arc_maps.push_back(new _graph_utils_bits::MapCopy<From, Arc, |
806 | 802 |
ArcRefMap, ToMap, FromMap>(tmap, map)); |
807 | 803 |
return *this; |
808 | 804 |
} |
809 | 805 |
|
810 | 806 |
/// \brief Make a copy of the given arc. |
811 | 807 |
/// |
812 | 808 |
/// Make a copy of the given arc. |
813 | 809 |
DigraphCopy& arc(TArc& tarc, const Arc& sarc) { |
814 | 810 |
_arc_maps.push_back(new _graph_utils_bits::ItemCopy<From, Arc, |
815 | 811 |
ArcRefMap, TArc>(tarc, sarc)); |
816 | 812 |
return *this; |
817 | 813 |
} |
818 | 814 |
|
819 | 815 |
/// \brief Executes the copies. |
820 | 816 |
/// |
821 | 817 |
/// Executes the copies. |
822 | 818 |
void run() { |
823 | 819 |
NodeRefMap nodeRefMap(_from); |
824 | 820 |
ArcRefMap arcRefMap(_from); |
825 | 821 |
_graph_utils_bits::DigraphCopySelector<To>:: |
826 | 822 |
copy(_to, _from, nodeRefMap, arcRefMap); |
827 | 823 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
828 | 824 |
_node_maps[i]->copy(_from, nodeRefMap); |
829 | 825 |
} |
830 | 826 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
831 | 827 |
_arc_maps[i]->copy(_from, arcRefMap); |
832 | 828 |
} |
833 | 829 |
} |
834 | 830 |
|
835 | 831 |
protected: |
836 | 832 |
|
837 | 833 |
|
838 | 834 |
const From& _from; |
839 | 835 |
To& _to; |
840 | 836 |
|
841 | 837 |
std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > |
842 | 838 |
_node_maps; |
843 | 839 |
|
844 | 840 |
std::vector<_graph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
845 | 841 |
_arc_maps; |
846 | 842 |
|
847 | 843 |
}; |
848 | 844 |
|
849 | 845 |
/// \brief Copy a digraph to another digraph. |
850 | 846 |
/// |
851 | 847 |
/// Copy a digraph to another digraph. The complete usage of the |
852 | 848 |
/// function is detailed in the DigraphCopy class, but a short |
853 | 849 |
/// example shows a basic work: |
854 | 850 |
///\code |
855 | 851 |
/// copyDigraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run(); |
856 | 852 |
///\endcode |
857 | 853 |
/// |
858 | 854 |
/// After the copy the \c nr map will contain the mapping from the |
859 | 855 |
/// nodes of the \c from digraph to the nodes of the \c to digraph and |
860 | 856 |
/// \c ecr will contain the mapping from the arcs of the \c to digraph |
861 | 857 |
/// to the arcs of the \c from digraph. |
862 | 858 |
/// |
863 | 859 |
/// \see DigraphCopy |
864 | 860 |
template <typename To, typename From> |
865 | 861 |
DigraphCopy<To, From> copyDigraph(To& to, const From& from) { |
866 | 862 |
return DigraphCopy<To, From>(to, from); |
867 | 863 |
} |
868 | 864 |
|
869 | 865 |
/// \brief Class to copy a graph. |
870 | 866 |
/// |
871 | 867 |
/// Class to copy a graph to another graph (duplicate a graph). The |
872 | 868 |
/// simplest way of using it is through the \c copyGraph() function. |
873 | 869 |
/// |
874 | 870 |
/// This class not just make a copy of a graph, but it can create |
875 | 871 |
/// references and cross references between the nodes, edges and arcs of |
876 | 872 |
/// the two graphs, it can copy maps for use with the newly created |
877 | 873 |
/// graph and copy nodes, edges and arcs. |
878 | 874 |
/// |
879 | 875 |
/// To make a copy from a graph, first an instance of GraphCopy |
880 | 876 |
/// should be created, then the data belongs to the graph should |
881 | 877 |
/// assigned to copy. In the end, the \c run() member should be |
882 | 878 |
/// called. |
883 | 879 |
/// |
884 | 880 |
/// The next code copies a graph with several data: |
885 | 881 |
///\code |
886 | 882 |
/// GraphCopy<NewGraph, OrigGraph> dc(new_graph, orig_graph); |
887 | 883 |
/// // create a reference for the nodes |
888 | 884 |
/// OrigGraph::NodeMap<NewGraph::Node> nr(orig_graph); |
889 | 885 |
/// dc.nodeRef(nr); |
890 | 886 |
/// // create a cross reference (inverse) for the edges |
891 | 887 |
/// NewGraph::EdgeMap<OrigGraph::Arc> ecr(new_graph); |
892 | 888 |
/// dc.edgeCrossRef(ecr); |
893 | 889 |
/// // copy an arc map |
894 | 890 |
/// OrigGraph::ArcMap<double> oamap(orig_graph); |
895 | 891 |
/// NewGraph::ArcMap<double> namap(new_graph); |
896 | 892 |
/// dc.arcMap(namap, oamap); |
897 | 893 |
/// // copy a node |
898 | 894 |
/// OrigGraph::Node on; |
899 | 895 |
/// NewGraph::Node nn; |
900 | 896 |
/// dc.node(nn, on); |
901 | 897 |
/// // Executions of copy |
902 | 898 |
/// dc.run(); |
903 | 899 |
///\endcode |
904 | 900 |
template <typename To, typename From> |
905 | 901 |
class GraphCopy { |
906 | 902 |
private: |
907 | 903 |
|
908 | 904 |
typedef typename From::Node Node; |
909 | 905 |
typedef typename From::NodeIt NodeIt; |
910 | 906 |
typedef typename From::Arc Arc; |
911 | 907 |
typedef typename From::ArcIt ArcIt; |
912 | 908 |
typedef typename From::Edge Edge; |
913 | 909 |
typedef typename From::EdgeIt EdgeIt; |
914 | 910 |
|
915 | 911 |
typedef typename To::Node TNode; |
916 | 912 |
typedef typename To::Arc TArc; |
917 | 913 |
typedef typename To::Edge TEdge; |
918 | 914 |
|
919 | 915 |
typedef typename From::template NodeMap<TNode> NodeRefMap; |
920 | 916 |
typedef typename From::template EdgeMap<TEdge> EdgeRefMap; |
921 | 917 |
|
922 | 918 |
struct ArcRefMap { |
923 | 919 |
ArcRefMap(const To& to, const From& from, |
924 | 920 |
const EdgeRefMap& edge_ref, const NodeRefMap& node_ref) |
925 | 921 |
: _to(to), _from(from), |
926 | 922 |
_edge_ref(edge_ref), _node_ref(node_ref) {} |
927 | 923 |
|
928 | 924 |
typedef typename From::Arc Key; |
929 | 925 |
typedef typename To::Arc Value; |
930 | 926 |
|
931 | 927 |
Value operator[](const Key& key) const { |
932 | 928 |
bool forward = |
933 | 929 |
(_from.direction(key) == |
934 | 930 |
(_node_ref[_from.source(key)] == _to.source(_edge_ref[key]))); |
935 | 931 |
return _to.direct(_edge_ref[key], forward); |
936 | 932 |
} |
937 | 933 |
|
938 | 934 |
const To& _to; |
939 | 935 |
const From& _from; |
940 | 936 |
const EdgeRefMap& _edge_ref; |
941 | 937 |
const NodeRefMap& _node_ref; |
942 | 938 |
}; |
943 | 939 |
|
944 | 940 |
|
945 | 941 |
public: |
946 | 942 |
|
947 | 943 |
|
948 | 944 |
/// \brief Constructor for the GraphCopy. |
949 | 945 |
/// |
950 | 946 |
/// It copies the content of the \c _from graph into the |
951 | 947 |
/// \c _to graph. |
952 | 948 |
GraphCopy(To& to, const From& from) |
953 | 949 |
: _from(from), _to(to) {} |
954 | 950 |
|
955 | 951 |
/// \brief Destructor of the GraphCopy |
956 | 952 |
/// |
957 | 953 |
/// Destructor of the GraphCopy |
958 | 954 |
~GraphCopy() { |
959 | 955 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
960 | 956 |
delete _node_maps[i]; |
961 | 957 |
} |
962 | 958 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
963 | 959 |
delete _arc_maps[i]; |
964 | 960 |
} |
965 | 961 |
for (int i = 0; i < int(_edge_maps.size()); ++i) { |
966 | 962 |
delete _edge_maps[i]; |
967 | 963 |
} |
968 | 964 |
|
969 | 965 |
} |
970 | 966 |
|
971 | 967 |
/// \brief Copies the node references into the given map. |
972 | 968 |
/// |
973 | 969 |
/// Copies the node references into the given map. |
974 | 970 |
template <typename NodeRef> |
975 | 971 |
GraphCopy& nodeRef(NodeRef& map) { |
976 | 972 |
_node_maps.push_back(new _graph_utils_bits::RefCopy<From, Node, |
977 | 973 |
NodeRefMap, NodeRef>(map)); |
978 | 974 |
return *this; |
979 | 975 |
} |
980 | 976 |
|
981 | 977 |
/// \brief Copies the node cross references into the given map. |
982 | 978 |
/// |
983 | 979 |
/// Copies the node cross references (reverse references) into |
984 | 980 |
/// the given map. |
985 | 981 |
template <typename NodeCrossRef> |
986 | 982 |
GraphCopy& nodeCrossRef(NodeCrossRef& map) { |
987 | 983 |
_node_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Node, |
988 | 984 |
NodeRefMap, NodeCrossRef>(map)); |
989 | 985 |
return *this; |
990 | 986 |
} |
991 | 987 |
|
992 | 988 |
/// \brief Make copy of the given map. |
993 | 989 |
/// |
994 | 990 |
/// Makes copy of the given map for the newly created graph. |
995 | 991 |
/// The new map's key type is the to graph's node type, |
996 | 992 |
/// and the copied map's key type is the from graph's node |
997 | 993 |
/// type. |
998 | 994 |
template <typename ToMap, typename FromMap> |
999 | 995 |
GraphCopy& nodeMap(ToMap& tmap, const FromMap& map) { |
1000 | 996 |
_node_maps.push_back(new _graph_utils_bits::MapCopy<From, Node, |
1001 | 997 |
NodeRefMap, ToMap, FromMap>(tmap, map)); |
1002 | 998 |
return *this; |
1003 | 999 |
} |
1004 | 1000 |
|
1005 | 1001 |
/// \brief Make a copy of the given node. |
1006 | 1002 |
/// |
1007 | 1003 |
/// Make a copy of the given node. |
1008 | 1004 |
GraphCopy& node(TNode& tnode, const Node& snode) { |
1009 | 1005 |
_node_maps.push_back(new _graph_utils_bits::ItemCopy<From, Node, |
1010 | 1006 |
NodeRefMap, TNode>(tnode, snode)); |
1011 | 1007 |
return *this; |
1012 | 1008 |
} |
1013 | 1009 |
|
1014 | 1010 |
/// \brief Copies the arc references into the given map. |
1015 | 1011 |
/// |
1016 | 1012 |
/// Copies the arc references into the given map. |
1017 | 1013 |
template <typename ArcRef> |
1018 | 1014 |
GraphCopy& arcRef(ArcRef& map) { |
1019 | 1015 |
_arc_maps.push_back(new _graph_utils_bits::RefCopy<From, Arc, |
1020 | 1016 |
ArcRefMap, ArcRef>(map)); |
1021 | 1017 |
return *this; |
1022 | 1018 |
} |
1023 | 1019 |
|
1024 | 1020 |
/// \brief Copies the arc cross references into the given map. |
1025 | 1021 |
/// |
1026 | 1022 |
/// Copies the arc cross references (reverse references) into |
1027 | 1023 |
/// the given map. |
1028 | 1024 |
template <typename ArcCrossRef> |
1029 | 1025 |
GraphCopy& arcCrossRef(ArcCrossRef& map) { |
1030 | 1026 |
_arc_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, Arc, |
1031 | 1027 |
ArcRefMap, ArcCrossRef>(map)); |
1032 | 1028 |
return *this; |
1033 | 1029 |
} |
1034 | 1030 |
|
1035 | 1031 |
/// \brief Make copy of the given map. |
1036 | 1032 |
/// |
1037 | 1033 |
/// Makes copy of the given map for the newly created graph. |
1038 | 1034 |
/// The new map's key type is the to graph's arc type, |
1039 | 1035 |
/// and the copied map's key type is the from graph's arc |
1040 | 1036 |
/// type. |
1041 | 1037 |
template <typename ToMap, typename FromMap> |
1042 | 1038 |
GraphCopy& arcMap(ToMap& tmap, const FromMap& map) { |
1043 | 1039 |
_arc_maps.push_back(new _graph_utils_bits::MapCopy<From, Arc, |
1044 | 1040 |
ArcRefMap, ToMap, FromMap>(tmap, map)); |
1045 | 1041 |
return *this; |
1046 | 1042 |
} |
1047 | 1043 |
|
1048 | 1044 |
/// \brief Make a copy of the given arc. |
1049 | 1045 |
/// |
1050 | 1046 |
/// Make a copy of the given arc. |
1051 | 1047 |
GraphCopy& arc(TArc& tarc, const Arc& sarc) { |
1052 | 1048 |
_arc_maps.push_back(new _graph_utils_bits::ItemCopy<From, Arc, |
1053 | 1049 |
ArcRefMap, TArc>(tarc, sarc)); |
1054 | 1050 |
return *this; |
1055 | 1051 |
} |
1056 | 1052 |
|
1057 | 1053 |
/// \brief Copies the edge references into the given map. |
1058 | 1054 |
/// |
1059 | 1055 |
/// Copies the edge references into the given map. |
1060 | 1056 |
template <typename EdgeRef> |
1061 | 1057 |
GraphCopy& edgeRef(EdgeRef& map) { |
1062 | 1058 |
_edge_maps.push_back(new _graph_utils_bits::RefCopy<From, Edge, |
1063 | 1059 |
EdgeRefMap, EdgeRef>(map)); |
1064 | 1060 |
return *this; |
1065 | 1061 |
} |
1066 | 1062 |
|
1067 | 1063 |
/// \brief Copies the edge cross references into the given map. |
1068 | 1064 |
/// |
1069 | 1065 |
/// Copies the edge cross references (reverse |
1070 | 1066 |
/// references) into the given map. |
1071 | 1067 |
template <typename EdgeCrossRef> |
1072 | 1068 |
GraphCopy& edgeCrossRef(EdgeCrossRef& map) { |
1073 | 1069 |
_edge_maps.push_back(new _graph_utils_bits::CrossRefCopy<From, |
1074 | 1070 |
Edge, EdgeRefMap, EdgeCrossRef>(map)); |
1075 | 1071 |
return *this; |
1076 | 1072 |
} |
1077 | 1073 |
|
1078 | 1074 |
/// \brief Make copy of the given map. |
1079 | 1075 |
/// |
1080 | 1076 |
/// Makes copy of the given map for the newly created graph. |
1081 | 1077 |
/// The new map's key type is the to graph's edge type, |
1082 | 1078 |
/// and the copied map's key type is the from graph's edge |
1083 | 1079 |
/// type. |
1084 | 1080 |
template <typename ToMap, typename FromMap> |
1085 | 1081 |
GraphCopy& edgeMap(ToMap& tmap, const FromMap& map) { |
1086 | 1082 |
_edge_maps.push_back(new _graph_utils_bits::MapCopy<From, Edge, |
1087 | 1083 |
EdgeRefMap, ToMap, FromMap>(tmap, map)); |
1088 | 1084 |
return *this; |
1089 | 1085 |
} |
1090 | 1086 |
|
1091 | 1087 |
/// \brief Make a copy of the given edge. |
1092 | 1088 |
/// |
1093 | 1089 |
/// Make a copy of the given edge. |
1094 | 1090 |
GraphCopy& edge(TEdge& tedge, const Edge& sedge) { |
1095 | 1091 |
_edge_maps.push_back(new _graph_utils_bits::ItemCopy<From, Edge, |
1096 | 1092 |
EdgeRefMap, TEdge>(tedge, sedge)); |
1097 | 1093 |
return *this; |
1098 | 1094 |
} |
1099 | 1095 |
|
1100 | 1096 |
/// \brief Executes the copies. |
1101 | 1097 |
/// |
1102 | 1098 |
/// Executes the copies. |
1103 | 1099 |
void run() { |
1104 | 1100 |
NodeRefMap nodeRefMap(_from); |
1105 | 1101 |
EdgeRefMap edgeRefMap(_from); |
1106 | 1102 |
ArcRefMap arcRefMap(_to, _from, edgeRefMap, nodeRefMap); |
1107 | 1103 |
_graph_utils_bits::GraphCopySelector<To>:: |
1108 | 1104 |
copy(_to, _from, nodeRefMap, edgeRefMap); |
1109 | 1105 |
for (int i = 0; i < int(_node_maps.size()); ++i) { |
1110 | 1106 |
_node_maps[i]->copy(_from, nodeRefMap); |
1111 | 1107 |
} |
1112 | 1108 |
for (int i = 0; i < int(_edge_maps.size()); ++i) { |
1113 | 1109 |
_edge_maps[i]->copy(_from, edgeRefMap); |
1114 | 1110 |
} |
1115 | 1111 |
for (int i = 0; i < int(_arc_maps.size()); ++i) { |
1116 | 1112 |
_arc_maps[i]->copy(_from, arcRefMap); |
1117 | 1113 |
} |
1118 | 1114 |
} |
1119 | 1115 |
|
1120 | 1116 |
private: |
1121 | 1117 |
|
1122 | 1118 |
const From& _from; |
1123 | 1119 |
To& _to; |
1124 | 1120 |
|
1125 | 1121 |
std::vector<_graph_utils_bits::MapCopyBase<From, Node, NodeRefMap>* > |
1126 | 1122 |
_node_maps; |
1127 | 1123 |
|
1128 | 1124 |
std::vector<_graph_utils_bits::MapCopyBase<From, Arc, ArcRefMap>* > |
1129 | 1125 |
_arc_maps; |
1130 | 1126 |
|
1131 | 1127 |
std::vector<_graph_utils_bits::MapCopyBase<From, Edge, EdgeRefMap>* > |
1132 | 1128 |
_edge_maps; |
1133 | 1129 |
|
1134 | 1130 |
}; |
1135 | 1131 |
|
1136 | 1132 |
/// \brief Copy a graph to another graph. |
1137 | 1133 |
/// |
1138 | 1134 |
/// Copy a graph to another graph. The complete usage of the |
1139 | 1135 |
/// function is detailed in the GraphCopy class, but a short |
1140 | 1136 |
/// example shows a basic work: |
1141 | 1137 |
///\code |
1142 | 1138 |
/// copyGraph(trg, src).nodeRef(nr).arcCrossRef(ecr).run(); |
1143 | 1139 |
///\endcode |
1144 | 1140 |
/// |
1145 | 1141 |
/// After the copy the \c nr map will contain the mapping from the |
1146 | 1142 |
/// nodes of the \c from graph to the nodes of the \c to graph and |
1147 | 1143 |
/// \c ecr will contain the mapping from the arcs of the \c to graph |
1148 | 1144 |
/// to the arcs of the \c from graph. |
1149 | 1145 |
/// |
1150 | 1146 |
/// \see GraphCopy |
1151 | 1147 |
template <typename To, typename From> |
1152 | 1148 |
GraphCopy<To, From> |
1153 | 1149 |
copyGraph(To& to, const From& from) { |
1154 | 1150 |
return GraphCopy<To, From>(to, from); |
1155 | 1151 |
} |
1156 | 1152 |
|
1157 | 1153 |
/// @} |
1158 | 1154 |
|
1159 | 1155 |
/// \addtogroup graph_maps |
1160 | 1156 |
/// @{ |
1161 | 1157 |
|
1162 | 1158 |
/// Provides an immutable and unique id for each item in the graph. |
1163 | 1159 |
|
1164 | 1160 |
/// The IdMap class provides a unique and immutable id for each item of the |
1165 | 1161 |
/// same type (e.g. node) in the graph. This id is <ul><li>\b unique: |
1166 | 1162 |
/// different items (nodes) get different ids <li>\b immutable: the id of an |
1167 | 1163 |
/// item (node) does not change (even if you delete other nodes). </ul> |
1168 | 1164 |
/// Through this map you get access (i.e. can read) the inner id values of |
1169 | 1165 |
/// the items stored in the graph. This map can be inverted with its member |
1170 | 1166 |
/// class \c InverseMap or with the \c operator() member. |
1171 | 1167 |
/// |
1172 | 1168 |
template <typename _Graph, typename _Item> |
1173 | 1169 |
class IdMap { |
1174 | 1170 |
public: |
1175 | 1171 |
typedef _Graph Graph; |
1176 | 1172 |
typedef int Value; |
1177 | 1173 |
typedef _Item Item; |
1178 | 1174 |
typedef _Item Key; |
1179 | 1175 |
|
1180 | 1176 |
/// \brief Constructor. |
1181 | 1177 |
/// |
1182 | 1178 |
/// Constructor of the map. |
1183 | 1179 |
explicit IdMap(const Graph& graph) : _graph(&graph) {} |
1184 | 1180 |
|
1185 | 1181 |
/// \brief Gives back the \e id of the item. |
1186 | 1182 |
/// |
1187 | 1183 |
/// Gives back the immutable and unique \e id of the item. |
1188 | 1184 |
int operator[](const Item& item) const { return _graph->id(item);} |
1189 | 1185 |
|
1190 | 1186 |
/// \brief Gives back the item by its id. |
1191 | 1187 |
/// |
1192 | 1188 |
/// Gives back the item by its id. |
1193 | 1189 |
Item operator()(int id) { return _graph->fromId(id, Item()); } |
1194 | 1190 |
|
1195 | 1191 |
private: |
1196 | 1192 |
const Graph* _graph; |
1197 | 1193 |
|
1198 | 1194 |
public: |
1199 | 1195 |
|
1200 | 1196 |
/// \brief The class represents the inverse of its owner (IdMap). |
1201 | 1197 |
/// |
1202 | 1198 |
/// The class represents the inverse of its owner (IdMap). |
1203 | 1199 |
/// \see inverse() |
1204 | 1200 |
class InverseMap { |
1205 | 1201 |
public: |
1206 | 1202 |
|
1207 | 1203 |
/// \brief Constructor. |
1208 | 1204 |
/// |
1209 | 1205 |
/// Constructor for creating an id-to-item map. |
1210 | 1206 |
explicit InverseMap(const Graph& graph) : _graph(&graph) {} |
1211 | 1207 |
|
1212 | 1208 |
/// \brief Constructor. |
1213 | 1209 |
/// |
1214 | 1210 |
/// Constructor for creating an id-to-item map. |
1215 | 1211 |
explicit InverseMap(const IdMap& map) : _graph(map._graph) {} |
1216 | 1212 |
|
1217 | 1213 |
/// \brief Gives back the given item from its id. |
1218 | 1214 |
/// |
1219 | 1215 |
/// Gives back the given item from its id. |
1220 | 1216 |
/// |
1221 | 1217 |
Item operator[](int id) const { return _graph->fromId(id, Item());} |
1222 | 1218 |
|
1223 | 1219 |
private: |
1224 | 1220 |
const Graph* _graph; |
1225 | 1221 |
}; |
1226 | 1222 |
|
1227 | 1223 |
/// \brief Gives back the inverse of the map. |
1228 | 1224 |
/// |
1229 | 1225 |
/// Gives back the inverse of the IdMap. |
1230 | 1226 |
InverseMap inverse() const { return InverseMap(*_graph);} |
1231 | 1227 |
|
1232 | 1228 |
}; |
1233 | 1229 |
|
1234 | 1230 |
|
1235 | 1231 |
/// \brief General invertable graph-map type. |
1236 | 1232 |
|
1237 | 1233 |
/// This type provides simple invertable graph-maps. |
1238 | 1234 |
/// The InvertableMap wraps an arbitrary ReadWriteMap |
1239 | 1235 |
/// and if a key is set to a new value then store it |
1240 | 1236 |
/// in the inverse map. |
1241 | 1237 |
/// |
1242 | 1238 |
/// The values of the map can be accessed |
1243 | 1239 |
/// with stl compatible forward iterator. |
1244 | 1240 |
/// |
1245 |
/// \param _Graph The graph type. |
|
1246 |
/// \param _Item The item type of the graph. |
|
1247 |
/// \ |
|
1241 |
/// \tparam _Graph The graph type. |
|
1242 |
/// \tparam _Item The item type of the graph. |
|
1243 |
/// \tparam _Value The value type of the map. |
|
1248 | 1244 |
/// |
1249 | 1245 |
/// \see IterableValueMap |
1250 | 1246 |
template <typename _Graph, typename _Item, typename _Value> |
1251 | 1247 |
class InvertableMap : protected DefaultMap<_Graph, _Item, _Value> { |
1252 | 1248 |
private: |
1253 | 1249 |
|
1254 | 1250 |
typedef DefaultMap<_Graph, _Item, _Value> Map; |
1255 | 1251 |
typedef _Graph Graph; |
1256 | 1252 |
|
1257 | 1253 |
typedef std::map<_Value, _Item> Container; |
1258 | 1254 |
Container _inv_map; |
1259 | 1255 |
|
1260 | 1256 |
public: |
1261 | 1257 |
|
1262 | 1258 |
/// The key type of InvertableMap (Node, Arc, Edge). |
1263 | 1259 |
typedef typename Map::Key Key; |
1264 | 1260 |
/// The value type of the InvertableMap. |
1265 | 1261 |
typedef typename Map::Value Value; |
1266 | 1262 |
|
1267 | 1263 |
|
1268 | 1264 |
|
1269 | 1265 |
/// \brief Constructor. |
1270 | 1266 |
/// |
1271 | 1267 |
/// Construct a new InvertableMap for the graph. |
1272 | 1268 |
/// |
1273 | 1269 |
explicit InvertableMap(const Graph& graph) : Map(graph) {} |
1274 | 1270 |
|
1275 | 1271 |
/// \brief Forward iterator for values. |
1276 | 1272 |
/// |
1277 | 1273 |
/// This iterator is an stl compatible forward |
1278 | 1274 |
/// iterator on the values of the map. The values can |
1279 | 1275 |
/// be accessed in the [beginValue, endValue) range. |
1280 | 1276 |
/// |
1281 | 1277 |
class ValueIterator |
1282 | 1278 |
: public std::iterator<std::forward_iterator_tag, Value> { |
1283 | 1279 |
friend class InvertableMap; |
1284 | 1280 |
private: |
1285 | 1281 |
ValueIterator(typename Container::const_iterator _it) |
1286 | 1282 |
: it(_it) {} |
1287 | 1283 |
public: |
1288 | 1284 |
|
1289 | 1285 |
ValueIterator() {} |
1290 | 1286 |
|
1291 | 1287 |
ValueIterator& operator++() { ++it; return *this; } |
1292 | 1288 |
ValueIterator operator++(int) { |
1293 | 1289 |
ValueIterator tmp(*this); |
1294 | 1290 |
operator++(); |
1295 | 1291 |
return tmp; |
1296 | 1292 |
} |
1297 | 1293 |
|
1298 | 1294 |
const Value& operator*() const { return it->first; } |
1299 | 1295 |
const Value* operator->() const { return &(it->first); } |
1300 | 1296 |
|
1301 | 1297 |
bool operator==(ValueIterator jt) const { return it == jt.it; } |
1302 | 1298 |
bool operator!=(ValueIterator jt) const { return it != jt.it; } |
1303 | 1299 |
|
1304 | 1300 |
private: |
1305 | 1301 |
typename Container::const_iterator it; |
1306 | 1302 |
}; |
1307 | 1303 |
|
1308 | 1304 |
/// \brief Returns an iterator to the first value. |
1309 | 1305 |
/// |
1310 | 1306 |
/// Returns an stl compatible iterator to the |
1311 | 1307 |
/// first value of the map. The values of the |
1312 | 1308 |
/// map can be accessed in the [beginValue, endValue) |
1313 | 1309 |
/// range. |
1314 | 1310 |
ValueIterator beginValue() const { |
1315 | 1311 |
return ValueIterator(_inv_map.begin()); |
1316 | 1312 |
} |
1317 | 1313 |
|
1318 | 1314 |
/// \brief Returns an iterator after the last value. |
1319 | 1315 |
/// |
1320 | 1316 |
/// Returns an stl compatible iterator after the |
1321 | 1317 |
/// last value of the map. The values of the |
1322 | 1318 |
/// map can be accessed in the [beginValue, endValue) |
1323 | 1319 |
/// range. |
1324 | 1320 |
ValueIterator endValue() const { |
1325 | 1321 |
return ValueIterator(_inv_map.end()); |
1326 | 1322 |
} |
1327 | 1323 |
|
1328 | 1324 |
/// \brief The setter function of the map. |
1329 | 1325 |
/// |
1330 | 1326 |
/// Sets the mapped value. |
1331 | 1327 |
void set(const Key& key, const Value& val) { |
1332 | 1328 |
Value oldval = Map::operator[](key); |
1333 | 1329 |
typename Container::iterator it = _inv_map.find(oldval); |
1334 | 1330 |
if (it != _inv_map.end() && it->second == key) { |
1335 | 1331 |
_inv_map.erase(it); |
1336 | 1332 |
} |
1337 | 1333 |
_inv_map.insert(make_pair(val, key)); |
1338 | 1334 |
Map::set(key, val); |
1339 | 1335 |
} |
1340 | 1336 |
|
1341 | 1337 |
/// \brief The getter function of the map. |
1342 | 1338 |
/// |
1343 | 1339 |
/// It gives back the value associated with the key. |
1344 | 1340 |
typename MapTraits<Map>::ConstReturnValue |
1345 | 1341 |
operator[](const Key& key) const { |
1346 | 1342 |
return Map::operator[](key); |
1347 | 1343 |
} |
1348 | 1344 |
|
1349 | 1345 |
/// \brief Gives back the item by its value. |
1350 | 1346 |
/// |
1351 | 1347 |
/// Gives back the item by its value. |
1352 | 1348 |
Key operator()(const Value& key) const { |
1353 | 1349 |
typename Container::const_iterator it = _inv_map.find(key); |
1354 | 1350 |
return it != _inv_map.end() ? it->second : INVALID; |
1355 | 1351 |
} |
1356 | 1352 |
|
1357 | 1353 |
protected: |
1358 | 1354 |
|
1359 | 1355 |
/// \brief Erase the key from the map. |
1360 | 1356 |
/// |
1361 | 1357 |
/// Erase the key to the map. It is called by the |
1362 | 1358 |
/// \c AlterationNotifier. |
1363 | 1359 |
virtual void erase(const Key& key) { |
1364 | 1360 |
Value val = Map::operator[](key); |
1365 | 1361 |
typename Container::iterator it = _inv_map.find(val); |
1366 | 1362 |
if (it != _inv_map.end() && it->second == key) { |
1367 | 1363 |
_inv_map.erase(it); |
1368 | 1364 |
} |
1369 | 1365 |
Map::erase(key); |
1370 | 1366 |
} |
1371 | 1367 |
|
1372 | 1368 |
/// \brief Erase more keys from the map. |
1373 | 1369 |
/// |
1374 | 1370 |
/// Erase more keys from the map. It is called by the |
1375 | 1371 |
/// \c AlterationNotifier. |
1376 | 1372 |
virtual void erase(const std::vector<Key>& keys) { |
1377 | 1373 |
for (int i = 0; i < int(keys.size()); ++i) { |
1378 | 1374 |
Value val = Map::operator[](keys[i]); |
1379 | 1375 |
typename Container::iterator it = _inv_map.find(val); |
1380 | 1376 |
if (it != _inv_map.end() && it->second == keys[i]) { |
1381 | 1377 |
_inv_map.erase(it); |
1382 | 1378 |
} |
1383 | 1379 |
} |
1384 | 1380 |
Map::erase(keys); |
1385 | 1381 |
} |
1386 | 1382 |
|
1387 | 1383 |
/// \brief Clear the keys from the map and inverse map. |
1388 | 1384 |
/// |
1389 | 1385 |
/// Clear the keys from the map and inverse map. It is called by the |
1390 | 1386 |
/// \c AlterationNotifier. |
1391 | 1387 |
virtual void clear() { |
1392 | 1388 |
_inv_map.clear(); |
1393 | 1389 |
Map::clear(); |
1394 | 1390 |
} |
1395 | 1391 |
|
1396 | 1392 |
public: |
1397 | 1393 |
|
1398 | 1394 |
/// \brief The inverse map type. |
1399 | 1395 |
/// |
1400 | 1396 |
/// The inverse of this map. The subscript operator of the map |
1401 | 1397 |
/// gives back always the item what was last assigned to the value. |
1402 | 1398 |
class InverseMap { |
1403 | 1399 |
public: |
1404 | 1400 |
/// \brief Constructor of the InverseMap. |
1405 | 1401 |
/// |
1406 | 1402 |
/// Constructor of the InverseMap. |
1407 | 1403 |
explicit InverseMap(const InvertableMap& inverted) |
1408 | 1404 |
: _inverted(inverted) {} |
1409 | 1405 |
|
1410 | 1406 |
/// The value type of the InverseMap. |
1411 | 1407 |
typedef typename InvertableMap::Key Value; |
1412 | 1408 |
/// The key type of the InverseMap. |
1413 | 1409 |
typedef typename InvertableMap::Value Key; |
1414 | 1410 |
|
1415 | 1411 |
/// \brief Subscript operator. |
1416 | 1412 |
/// |
1417 | 1413 |
/// Subscript operator. It gives back always the item |
1418 | 1414 |
/// what was last assigned to the value. |
1419 | 1415 |
Value operator[](const Key& key) const { |
1420 | 1416 |
return _inverted(key); |
1421 | 1417 |
} |
1422 | 1418 |
|
1423 | 1419 |
private: |
1424 | 1420 |
const InvertableMap& _inverted; |
1425 | 1421 |
}; |
1426 | 1422 |
|
1427 | 1423 |
/// \brief It gives back the just readable inverse map. |
1428 | 1424 |
/// |
1429 | 1425 |
/// It gives back the just readable inverse map. |
1430 | 1426 |
InverseMap inverse() const { |
1431 | 1427 |
return InverseMap(*this); |
1432 | 1428 |
} |
1433 | 1429 |
|
1434 | 1430 |
|
1435 | 1431 |
|
1436 | 1432 |
}; |
1437 | 1433 |
|
1438 | 1434 |
/// \brief Provides a mutable, continuous and unique descriptor for each |
1439 | 1435 |
/// item in the graph. |
1440 | 1436 |
/// |
1441 | 1437 |
/// The DescriptorMap class provides a unique and continuous (but mutable) |
1442 | 1438 |
/// descriptor (id) for each item of the same type (e.g. node) in the |
1443 | 1439 |
/// graph. This id is <ul><li>\b unique: different items (nodes) get |
1444 | 1440 |
/// different ids <li>\b continuous: the range of the ids is the set of |
1445 | 1441 |
/// integers between 0 and \c n-1, where \c n is the number of the items of |
1446 | 1442 |
/// this type (e.g. nodes) (so the id of a node can change if you delete an |
1447 | 1443 |
/// other node, i.e. this id is mutable). </ul> This map can be inverted |
1448 | 1444 |
/// with its member class \c InverseMap, or with the \c operator() member. |
1449 | 1445 |
/// |
1450 |
/// \param _Graph The graph class the \c DescriptorMap belongs to. |
|
1451 |
/// \param _Item The Item is the Key of the Map. It may be Node, Arc or |
|
1446 |
/// \tparam _Graph The graph class the \c DescriptorMap belongs to. |
|
1447 |
/// \tparam _Item The Item is the Key of the Map. It may be Node, Arc or |
|
1452 | 1448 |
/// Edge. |
1453 | 1449 |
template <typename _Graph, typename _Item> |
1454 | 1450 |
class DescriptorMap : protected DefaultMap<_Graph, _Item, int> { |
1455 | 1451 |
|
1456 | 1452 |
typedef _Item Item; |
1457 | 1453 |
typedef DefaultMap<_Graph, _Item, int> Map; |
1458 | 1454 |
|
1459 | 1455 |
public: |
1460 | 1456 |
/// The graph class of DescriptorMap. |
1461 | 1457 |
typedef _Graph Graph; |
1462 | 1458 |
|
1463 | 1459 |
/// The key type of DescriptorMap (Node, Arc, Edge). |
1464 | 1460 |
typedef typename Map::Key Key; |
1465 | 1461 |
/// The value type of DescriptorMap. |
1466 | 1462 |
typedef typename Map::Value Value; |
1467 | 1463 |
|
1468 | 1464 |
/// \brief Constructor. |
1469 | 1465 |
/// |
1470 | 1466 |
/// Constructor for descriptor map. |
1471 | 1467 |
explicit DescriptorMap(const Graph& _graph) : Map(_graph) { |
1472 | 1468 |
Item it; |
1473 | 1469 |
const typename Map::Notifier* nf = Map::notifier(); |
1474 | 1470 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
1475 | 1471 |
Map::set(it, _inv_map.size()); |
1476 | 1472 |
_inv_map.push_back(it); |
1477 | 1473 |
} |
1478 | 1474 |
} |
1479 | 1475 |
|
1480 | 1476 |
protected: |
1481 | 1477 |
|
1482 | 1478 |
/// \brief Add a new key to the map. |
1483 | 1479 |
/// |
1484 | 1480 |
/// Add a new key to the map. It is called by the |
1485 | 1481 |
/// \c AlterationNotifier. |
1486 | 1482 |
virtual void add(const Item& item) { |
1487 | 1483 |
Map::add(item); |
1488 | 1484 |
Map::set(item, _inv_map.size()); |
1489 | 1485 |
_inv_map.push_back(item); |
1490 | 1486 |
} |
1491 | 1487 |
|
1492 | 1488 |
/// \brief Add more new keys to the map. |
1493 | 1489 |
/// |
1494 | 1490 |
/// Add more new keys to the map. It is called by the |
1495 | 1491 |
/// \c AlterationNotifier. |
1496 | 1492 |
virtual void add(const std::vector<Item>& items) { |
1497 | 1493 |
Map::add(items); |
1498 | 1494 |
for (int i = 0; i < int(items.size()); ++i) { |
1499 | 1495 |
Map::set(items[i], _inv_map.size()); |
1500 | 1496 |
_inv_map.push_back(items[i]); |
1501 | 1497 |
} |
1502 | 1498 |
} |
1503 | 1499 |
|
1504 | 1500 |
/// \brief Erase the key from the map. |
1505 | 1501 |
/// |
1506 | 1502 |
/// Erase the key from the map. It is called by the |
1507 | 1503 |
/// \c AlterationNotifier. |
1508 | 1504 |
virtual void erase(const Item& item) { |
1509 | 1505 |
Map::set(_inv_map.back(), Map::operator[](item)); |
1510 | 1506 |
_inv_map[Map::operator[](item)] = _inv_map.back(); |
1511 | 1507 |
_inv_map.pop_back(); |
1512 | 1508 |
Map::erase(item); |
1513 | 1509 |
} |
1514 | 1510 |
|
1515 | 1511 |
/// \brief Erase more keys from the map. |
1516 | 1512 |
/// |
1517 | 1513 |
/// Erase more keys from the map. It is called by the |
1518 | 1514 |
/// \c AlterationNotifier. |
1519 | 1515 |
virtual void erase(const std::vector<Item>& items) { |
1520 | 1516 |
for (int i = 0; i < int(items.size()); ++i) { |
1521 | 1517 |
Map::set(_inv_map.back(), Map::operator[](items[i])); |
1522 | 1518 |
_inv_map[Map::operator[](items[i])] = _inv_map.back(); |
1523 | 1519 |
_inv_map.pop_back(); |
1524 | 1520 |
} |
1525 | 1521 |
Map::erase(items); |
1526 | 1522 |
} |
1527 | 1523 |
|
1528 | 1524 |
/// \brief Build the unique map. |
1529 | 1525 |
/// |
1530 | 1526 |
/// Build the unique map. It is called by the |
1531 | 1527 |
/// \c AlterationNotifier. |
1532 | 1528 |
virtual void build() { |
1533 | 1529 |
Map::build(); |
1534 | 1530 |
Item it; |
1535 | 1531 |
const typename Map::Notifier* nf = Map::notifier(); |
1536 | 1532 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
1537 | 1533 |
Map::set(it, _inv_map.size()); |
1538 | 1534 |
_inv_map.push_back(it); |
1539 | 1535 |
} |
1540 | 1536 |
} |
1541 | 1537 |
|
1542 | 1538 |
/// \brief Clear the keys from the map. |
1543 | 1539 |
/// |
1544 | 1540 |
/// Clear the keys from the map. It is called by the |
1545 | 1541 |
/// \c AlterationNotifier. |
1546 | 1542 |
virtual void clear() { |
1547 | 1543 |
_inv_map.clear(); |
1548 | 1544 |
Map::clear(); |
1549 | 1545 |
} |
1550 | 1546 |
|
1551 | 1547 |
public: |
1552 | 1548 |
|
1553 | 1549 |
/// \brief Returns the maximal value plus one. |
1554 | 1550 |
/// |
1555 | 1551 |
/// Returns the maximal value plus one in the map. |
1556 | 1552 |
unsigned int size() const { |
1557 | 1553 |
return _inv_map.size(); |
1558 | 1554 |
} |
1559 | 1555 |
|
1560 | 1556 |
/// \brief Swaps the position of the two items in the map. |
1561 | 1557 |
/// |
1562 | 1558 |
/// Swaps the position of the two items in the map. |
1563 | 1559 |
void swap(const Item& p, const Item& q) { |
1564 | 1560 |
int pi = Map::operator[](p); |
1565 | 1561 |
int qi = Map::operator[](q); |
1566 | 1562 |
Map::set(p, qi); |
1567 | 1563 |
_inv_map[qi] = p; |
1568 | 1564 |
Map::set(q, pi); |
1569 | 1565 |
_inv_map[pi] = q; |
1570 | 1566 |
} |
1571 | 1567 |
|
1572 | 1568 |
/// \brief Gives back the \e descriptor of the item. |
1573 | 1569 |
/// |
1574 | 1570 |
/// Gives back the mutable and unique \e descriptor of the map. |
1575 | 1571 |
int operator[](const Item& item) const { |
1576 | 1572 |
return Map::operator[](item); |
1577 | 1573 |
} |
1578 | 1574 |
|
1579 | 1575 |
/// \brief Gives back the item by its descriptor. |
1580 | 1576 |
/// |
1581 | 1577 |
/// Gives back th item by its descriptor. |
1582 | 1578 |
Item operator()(int id) const { |
1583 | 1579 |
return _inv_map[id]; |
1584 | 1580 |
} |
1585 | 1581 |
|
1586 | 1582 |
private: |
1587 | 1583 |
|
1588 | 1584 |
typedef std::vector<Item> Container; |
1589 | 1585 |
Container _inv_map; |
1590 | 1586 |
|
1591 | 1587 |
public: |
1592 | 1588 |
/// \brief The inverse map type of DescriptorMap. |
1593 | 1589 |
/// |
1594 | 1590 |
/// The inverse map type of DescriptorMap. |
1595 | 1591 |
class InverseMap { |
1596 | 1592 |
public: |
1597 | 1593 |
/// \brief Constructor of the InverseMap. |
1598 | 1594 |
/// |
1599 | 1595 |
/// Constructor of the InverseMap. |
1600 | 1596 |
explicit InverseMap(const DescriptorMap& inverted) |
1601 | 1597 |
: _inverted(inverted) {} |
1602 | 1598 |
|
1603 | 1599 |
|
1604 | 1600 |
/// The value type of the InverseMap. |
1605 | 1601 |
typedef typename DescriptorMap::Key Value; |
1606 | 1602 |
/// The key type of the InverseMap. |
1607 | 1603 |
typedef typename DescriptorMap::Value Key; |
1608 | 1604 |
|
1609 | 1605 |
/// \brief Subscript operator. |
1610 | 1606 |
/// |
1611 | 1607 |
/// Subscript operator. It gives back the item |
1612 | 1608 |
/// that the descriptor belongs to currently. |
1613 | 1609 |
Value operator[](const Key& key) const { |
1614 | 1610 |
return _inverted(key); |
1615 | 1611 |
} |
1616 | 1612 |
|
1617 | 1613 |
/// \brief Size of the map. |
1618 | 1614 |
/// |
1619 | 1615 |
/// Returns the size of the map. |
1620 | 1616 |
unsigned int size() const { |
1621 | 1617 |
return _inverted.size(); |
1622 | 1618 |
} |
1623 | 1619 |
|
1624 | 1620 |
private: |
1625 | 1621 |
const DescriptorMap& _inverted; |
1626 | 1622 |
}; |
1627 | 1623 |
|
1628 | 1624 |
/// \brief Gives back the inverse of the map. |
1629 | 1625 |
/// |
1630 | 1626 |
/// Gives back the inverse of the map. |
1631 | 1627 |
const InverseMap inverse() const { |
1632 | 1628 |
return InverseMap(*this); |
1633 | 1629 |
} |
1634 | 1630 |
}; |
1635 | 1631 |
|
1636 | 1632 |
/// \brief Returns the source of the given arc. |
1637 | 1633 |
/// |
1638 | 1634 |
/// The SourceMap gives back the source Node of the given arc. |
1639 | 1635 |
/// \see TargetMap |
1640 |
/// \author Balazs Dezso |
|
1641 | 1636 |
template <typename Digraph> |
1642 | 1637 |
class SourceMap { |
1643 | 1638 |
public: |
1644 | 1639 |
|
1645 | 1640 |
typedef typename Digraph::Node Value; |
1646 | 1641 |
typedef typename Digraph::Arc Key; |
1647 | 1642 |
|
1648 | 1643 |
/// \brief Constructor |
1649 | 1644 |
/// |
1650 | 1645 |
/// Constructor |
1651 | 1646 |
/// \param _digraph The digraph that the map belongs to. |
1652 | 1647 |
explicit SourceMap(const Digraph& digraph) : _digraph(digraph) {} |
1653 | 1648 |
|
1654 | 1649 |
/// \brief The subscript operator. |
1655 | 1650 |
/// |
1656 | 1651 |
/// The subscript operator. |
1657 | 1652 |
/// \param arc The arc |
1658 | 1653 |
/// \return The source of the arc |
1659 | 1654 |
Value operator[](const Key& arc) const { |
1660 | 1655 |
return _digraph.source(arc); |
1661 | 1656 |
} |
1662 | 1657 |
|
1663 | 1658 |
private: |
1664 | 1659 |
const Digraph& _digraph; |
1665 | 1660 |
}; |
1666 | 1661 |
|
1667 | 1662 |
/// \brief Returns a \ref SourceMap class. |
1668 | 1663 |
/// |
1669 | 1664 |
/// This function just returns an \ref SourceMap class. |
1670 | 1665 |
/// \relates SourceMap |
1671 | 1666 |
template <typename Digraph> |
1672 | 1667 |
inline SourceMap<Digraph> sourceMap(const Digraph& digraph) { |
1673 | 1668 |
return SourceMap<Digraph>(digraph); |
1674 | 1669 |
} |
1675 | 1670 |
|
1676 | 1671 |
/// \brief Returns the target of the given arc. |
1677 | 1672 |
/// |
1678 | 1673 |
/// The TargetMap gives back the target Node of the given arc. |
1679 | 1674 |
/// \see SourceMap |
1680 |
/// \author Balazs Dezso |
|
1681 | 1675 |
template <typename Digraph> |
1682 | 1676 |
class TargetMap { |
1683 | 1677 |
public: |
1684 | 1678 |
|
1685 | 1679 |
typedef typename Digraph::Node Value; |
1686 | 1680 |
typedef typename Digraph::Arc Key; |
1687 | 1681 |
|
1688 | 1682 |
/// \brief Constructor |
1689 | 1683 |
/// |
1690 | 1684 |
/// Constructor |
1691 | 1685 |
/// \param _digraph The digraph that the map belongs to. |
1692 | 1686 |
explicit TargetMap(const Digraph& digraph) : _digraph(digraph) {} |
1693 | 1687 |
|
1694 | 1688 |
/// \brief The subscript operator. |
1695 | 1689 |
/// |
1696 | 1690 |
/// The subscript operator. |
1697 | 1691 |
/// \param e The arc |
1698 | 1692 |
/// \return The target of the arc |
1699 | 1693 |
Value operator[](const Key& e) const { |
1700 | 1694 |
return _digraph.target(e); |
1701 | 1695 |
} |
1702 | 1696 |
|
1703 | 1697 |
private: |
1704 | 1698 |
const Digraph& _digraph; |
1705 | 1699 |
}; |
1706 | 1700 |
|
1707 | 1701 |
/// \brief Returns a \ref TargetMap class. |
1708 | 1702 |
/// |
1709 | 1703 |
/// This function just returns a \ref TargetMap class. |
1710 | 1704 |
/// \relates TargetMap |
1711 | 1705 |
template <typename Digraph> |
1712 | 1706 |
inline TargetMap<Digraph> targetMap(const Digraph& digraph) { |
1713 | 1707 |
return TargetMap<Digraph>(digraph); |
1714 | 1708 |
} |
1715 | 1709 |
|
1716 | 1710 |
/// \brief Returns the "forward" directed arc view of an edge. |
1717 | 1711 |
/// |
1718 | 1712 |
/// Returns the "forward" directed arc view of an edge. |
1719 | 1713 |
/// \see BackwardMap |
1720 |
/// \author Balazs Dezso |
|
1721 | 1714 |
template <typename Graph> |
1722 | 1715 |
class ForwardMap { |
1723 | 1716 |
public: |
1724 | 1717 |
|
1725 | 1718 |
typedef typename Graph::Arc Value; |
1726 | 1719 |
typedef typename Graph::Edge Key; |
1727 | 1720 |
|
1728 | 1721 |
/// \brief Constructor |
1729 | 1722 |
/// |
1730 | 1723 |
/// Constructor |
1731 | 1724 |
/// \param _graph The graph that the map belongs to. |
1732 | 1725 |
explicit ForwardMap(const Graph& graph) : _graph(graph) {} |
1733 | 1726 |
|
1734 | 1727 |
/// \brief The subscript operator. |
1735 | 1728 |
/// |
1736 | 1729 |
/// The subscript operator. |
1737 | 1730 |
/// \param key An edge |
1738 | 1731 |
/// \return The "forward" directed arc view of edge |
1739 | 1732 |
Value operator[](const Key& key) const { |
1740 | 1733 |
return _graph.direct(key, true); |
1741 | 1734 |
} |
1742 | 1735 |
|
1743 | 1736 |
private: |
1744 | 1737 |
const Graph& _graph; |
1745 | 1738 |
}; |
1746 | 1739 |
|
1747 | 1740 |
/// \brief Returns a \ref ForwardMap class. |
1748 | 1741 |
/// |
1749 | 1742 |
/// This function just returns an \ref ForwardMap class. |
1750 | 1743 |
/// \relates ForwardMap |
1751 | 1744 |
template <typename Graph> |
1752 | 1745 |
inline ForwardMap<Graph> forwardMap(const Graph& graph) { |
1753 | 1746 |
return ForwardMap<Graph>(graph); |
1754 | 1747 |
} |
1755 | 1748 |
|
1756 | 1749 |
/// \brief Returns the "backward" directed arc view of an edge. |
1757 | 1750 |
/// |
1758 | 1751 |
/// Returns the "backward" directed arc view of an edge. |
1759 | 1752 |
/// \see ForwardMap |
1760 |
/// \author Balazs Dezso |
|
1761 | 1753 |
template <typename Graph> |
1762 | 1754 |
class BackwardMap { |
1763 | 1755 |
public: |
1764 | 1756 |
|
1765 | 1757 |
typedef typename Graph::Arc Value; |
1766 | 1758 |
typedef typename Graph::Edge Key; |
1767 | 1759 |
|
1768 | 1760 |
/// \brief Constructor |
1769 | 1761 |
/// |
1770 | 1762 |
/// Constructor |
1771 | 1763 |
/// \param _graph The graph that the map belongs to. |
1772 | 1764 |
explicit BackwardMap(const Graph& graph) : _graph(graph) {} |
1773 | 1765 |
|
1774 | 1766 |
/// \brief The subscript operator. |
1775 | 1767 |
/// |
1776 | 1768 |
/// The subscript operator. |
1777 | 1769 |
/// \param key An edge |
1778 | 1770 |
/// \return The "backward" directed arc view of edge |
1779 | 1771 |
Value operator[](const Key& key) const { |
1780 | 1772 |
return _graph.direct(key, false); |
1781 | 1773 |
} |
1782 | 1774 |
|
1783 | 1775 |
private: |
1784 | 1776 |
const Graph& _graph; |
1785 | 1777 |
}; |
1786 | 1778 |
|
1787 | 1779 |
/// \brief Returns a \ref BackwardMap class |
1788 | 1780 |
|
1789 | 1781 |
/// This function just returns a \ref BackwardMap class. |
1790 | 1782 |
/// \relates BackwardMap |
1791 | 1783 |
template <typename Graph> |
1792 | 1784 |
inline BackwardMap<Graph> backwardMap(const Graph& graph) { |
1793 | 1785 |
return BackwardMap<Graph>(graph); |
1794 | 1786 |
} |
1795 | 1787 |
|
1796 | 1788 |
/// \brief Potential difference map |
1797 | 1789 |
/// |
1798 | 1790 |
/// If there is an potential map on the nodes then we |
1799 | 1791 |
/// can get an arc map as we get the substraction of the |
1800 | 1792 |
/// values of the target and source. |
1801 | 1793 |
template <typename Digraph, typename NodeMap> |
1802 | 1794 |
class PotentialDifferenceMap { |
1803 | 1795 |
public: |
1804 | 1796 |
typedef typename Digraph::Arc Key; |
1805 | 1797 |
typedef typename NodeMap::Value Value; |
1806 | 1798 |
|
1807 | 1799 |
/// \brief Constructor |
1808 | 1800 |
/// |
1809 | 1801 |
/// Contructor of the map |
1810 | 1802 |
explicit PotentialDifferenceMap(const Digraph& digraph, |
1811 | 1803 |
const NodeMap& potential) |
1812 | 1804 |
: _digraph(digraph), _potential(potential) {} |
1813 | 1805 |
|
1814 | 1806 |
/// \brief Const subscription operator |
1815 | 1807 |
/// |
1816 | 1808 |
/// Const subscription operator |
1817 | 1809 |
Value operator[](const Key& arc) const { |
1818 | 1810 |
return _potential[_digraph.target(arc)] - |
1819 | 1811 |
_potential[_digraph.source(arc)]; |
1820 | 1812 |
} |
1821 | 1813 |
|
1822 | 1814 |
private: |
1823 | 1815 |
const Digraph& _digraph; |
1824 | 1816 |
const NodeMap& _potential; |
1825 | 1817 |
}; |
1826 | 1818 |
|
1827 | 1819 |
/// \brief Returns a PotentialDifferenceMap. |
1828 | 1820 |
/// |
1829 | 1821 |
/// This function just returns a PotentialDifferenceMap. |
1830 | 1822 |
/// \relates PotentialDifferenceMap |
1831 | 1823 |
template <typename Digraph, typename NodeMap> |
1832 | 1824 |
PotentialDifferenceMap<Digraph, NodeMap> |
1833 | 1825 |
potentialDifferenceMap(const Digraph& digraph, const NodeMap& potential) { |
1834 | 1826 |
return PotentialDifferenceMap<Digraph, NodeMap>(digraph, potential); |
1835 | 1827 |
} |
1836 | 1828 |
|
1837 | 1829 |
/// \brief Map of the node in-degrees. |
1838 | 1830 |
/// |
1839 | 1831 |
/// This map returns the in-degree of a node. Once it is constructed, |
1840 | 1832 |
/// the degrees are stored in a standard NodeMap, so each query is done |
1841 | 1833 |
/// in constant time. On the other hand, the values are updated automatically |
1842 | 1834 |
/// whenever the digraph changes. |
1843 | 1835 |
/// |
1844 | 1836 |
/// \warning Besides addNode() and addArc(), a digraph structure may provide |
1845 | 1837 |
/// alternative ways to modify the digraph. The correct behavior of InDegMap |
1846 | 1838 |
/// is not guarantied if these additional features are used. For example |
1847 | 1839 |
/// the functions \ref ListDigraph::changeSource() "changeSource()", |
1848 | 1840 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and |
1849 | 1841 |
/// \ref ListDigraph::reverseArc() "reverseArc()" |
1850 | 1842 |
/// of \ref ListDigraph will \e not update the degree values correctly. |
1851 | 1843 |
/// |
1852 | 1844 |
/// \sa OutDegMap |
1853 | 1845 |
|
1854 | 1846 |
template <typename _Digraph> |
1855 | 1847 |
class InDegMap |
1856 | 1848 |
: protected ItemSetTraits<_Digraph, typename _Digraph::Arc> |
1857 | 1849 |
::ItemNotifier::ObserverBase { |
1858 | 1850 |
|
1859 | 1851 |
public: |
1860 | 1852 |
|
1861 | 1853 |
typedef _Digraph Digraph; |
1862 | 1854 |
typedef int Value; |
1863 | 1855 |
typedef typename Digraph::Node Key; |
1864 | 1856 |
|
1865 | 1857 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc> |
1866 | 1858 |
::ItemNotifier::ObserverBase Parent; |
1867 | 1859 |
|
1868 | 1860 |
private: |
1869 | 1861 |
|
1870 | 1862 |
class AutoNodeMap : public DefaultMap<Digraph, Key, int> { |
1871 | 1863 |
public: |
1872 | 1864 |
|
1873 | 1865 |
typedef DefaultMap<Digraph, Key, int> Parent; |
1874 | 1866 |
|
1875 | 1867 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {} |
1876 | 1868 |
|
1877 | 1869 |
virtual void add(const Key& key) { |
1878 | 1870 |
Parent::add(key); |
1879 | 1871 |
Parent::set(key, 0); |
1880 | 1872 |
} |
1881 | 1873 |
|
1882 | 1874 |
virtual void add(const std::vector<Key>& keys) { |
1883 | 1875 |
Parent::add(keys); |
1884 | 1876 |
for (int i = 0; i < int(keys.size()); ++i) { |
1885 | 1877 |
Parent::set(keys[i], 0); |
1886 | 1878 |
} |
1887 | 1879 |
} |
1888 | 1880 |
|
1889 | 1881 |
virtual void build() { |
1890 | 1882 |
Parent::build(); |
1891 | 1883 |
Key it; |
1892 | 1884 |
typename Parent::Notifier* nf = Parent::notifier(); |
1893 | 1885 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
1894 | 1886 |
Parent::set(it, 0); |
1895 | 1887 |
} |
1896 | 1888 |
} |
1897 | 1889 |
}; |
1898 | 1890 |
|
1899 | 1891 |
public: |
1900 | 1892 |
|
1901 | 1893 |
/// \brief Constructor. |
1902 | 1894 |
/// |
1903 | 1895 |
/// Constructor for creating in-degree map. |
1904 | 1896 |
explicit InDegMap(const Digraph& digraph) |
1905 | 1897 |
: _digraph(digraph), _deg(digraph) { |
1906 | 1898 |
Parent::attach(_digraph.notifier(typename Digraph::Arc())); |
1907 | 1899 |
|
1908 | 1900 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
1909 | 1901 |
_deg[it] = countInArcs(_digraph, it); |
1910 | 1902 |
} |
1911 | 1903 |
} |
1912 | 1904 |
|
1913 | 1905 |
/// Gives back the in-degree of a Node. |
1914 | 1906 |
int operator[](const Key& key) const { |
1915 | 1907 |
return _deg[key]; |
1916 | 1908 |
} |
1917 | 1909 |
|
1918 | 1910 |
protected: |
1919 | 1911 |
|
1920 | 1912 |
typedef typename Digraph::Arc Arc; |
1921 | 1913 |
|
1922 | 1914 |
virtual void add(const Arc& arc) { |
1923 | 1915 |
++_deg[_digraph.target(arc)]; |
1924 | 1916 |
} |
1925 | 1917 |
|
1926 | 1918 |
virtual void add(const std::vector<Arc>& arcs) { |
1927 | 1919 |
for (int i = 0; i < int(arcs.size()); ++i) { |
1928 | 1920 |
++_deg[_digraph.target(arcs[i])]; |
1929 | 1921 |
} |
1930 | 1922 |
} |
1931 | 1923 |
|
1932 | 1924 |
virtual void erase(const Arc& arc) { |
1933 | 1925 |
--_deg[_digraph.target(arc)]; |
1934 | 1926 |
} |
1935 | 1927 |
|
1936 | 1928 |
virtual void erase(const std::vector<Arc>& arcs) { |
1937 | 1929 |
for (int i = 0; i < int(arcs.size()); ++i) { |
1938 | 1930 |
--_deg[_digraph.target(arcs[i])]; |
1939 | 1931 |
} |
1940 | 1932 |
} |
1941 | 1933 |
|
1942 | 1934 |
virtual void build() { |
1943 | 1935 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
1944 | 1936 |
_deg[it] = countInArcs(_digraph, it); |
1945 | 1937 |
} |
1946 | 1938 |
} |
1947 | 1939 |
|
1948 | 1940 |
virtual void clear() { |
1949 | 1941 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
1950 | 1942 |
_deg[it] = 0; |
1951 | 1943 |
} |
1952 | 1944 |
} |
1953 | 1945 |
private: |
1954 | 1946 |
|
1955 | 1947 |
const Digraph& _digraph; |
1956 | 1948 |
AutoNodeMap _deg; |
1957 | 1949 |
}; |
1958 | 1950 |
|
1959 | 1951 |
/// \brief Map of the node out-degrees. |
1960 | 1952 |
/// |
1961 | 1953 |
/// This map returns the out-degree of a node. Once it is constructed, |
1962 | 1954 |
/// the degrees are stored in a standard NodeMap, so each query is done |
1963 | 1955 |
/// in constant time. On the other hand, the values are updated automatically |
1964 | 1956 |
/// whenever the digraph changes. |
1965 | 1957 |
/// |
1966 | 1958 |
/// \warning Besides addNode() and addArc(), a digraph structure may provide |
1967 | 1959 |
/// alternative ways to modify the digraph. The correct behavior of OutDegMap |
1968 | 1960 |
/// is not guarantied if these additional features are used. For example |
1969 | 1961 |
/// the functions \ref ListDigraph::changeSource() "changeSource()", |
1970 | 1962 |
/// \ref ListDigraph::changeTarget() "changeTarget()" and |
1971 | 1963 |
/// \ref ListDigraph::reverseArc() "reverseArc()" |
1972 | 1964 |
/// of \ref ListDigraph will \e not update the degree values correctly. |
1973 | 1965 |
/// |
1974 | 1966 |
/// \sa InDegMap |
1975 | 1967 |
|
1976 | 1968 |
template <typename _Digraph> |
1977 | 1969 |
class OutDegMap |
1978 | 1970 |
: protected ItemSetTraits<_Digraph, typename _Digraph::Arc> |
1979 | 1971 |
::ItemNotifier::ObserverBase { |
1980 | 1972 |
|
1981 | 1973 |
public: |
1982 | 1974 |
|
1983 | 1975 |
typedef _Digraph Digraph; |
1984 | 1976 |
typedef int Value; |
1985 | 1977 |
typedef typename Digraph::Node Key; |
1986 | 1978 |
|
1987 | 1979 |
typedef typename ItemSetTraits<Digraph, typename Digraph::Arc> |
1988 | 1980 |
::ItemNotifier::ObserverBase Parent; |
1989 | 1981 |
|
1990 | 1982 |
private: |
1991 | 1983 |
|
1992 | 1984 |
class AutoNodeMap : public DefaultMap<Digraph, Key, int> { |
1993 | 1985 |
public: |
1994 | 1986 |
|
1995 | 1987 |
typedef DefaultMap<Digraph, Key, int> Parent; |
1996 | 1988 |
|
1997 | 1989 |
AutoNodeMap(const Digraph& digraph) : Parent(digraph, 0) {} |
1998 | 1990 |
|
1999 | 1991 |
virtual void add(const Key& key) { |
2000 | 1992 |
Parent::add(key); |
2001 | 1993 |
Parent::set(key, 0); |
2002 | 1994 |
} |
2003 | 1995 |
virtual void add(const std::vector<Key>& keys) { |
2004 | 1996 |
Parent::add(keys); |
2005 | 1997 |
for (int i = 0; i < int(keys.size()); ++i) { |
2006 | 1998 |
Parent::set(keys[i], 0); |
2007 | 1999 |
} |
2008 | 2000 |
} |
2009 | 2001 |
virtual void build() { |
2010 | 2002 |
Parent::build(); |
2011 | 2003 |
Key it; |
2012 | 2004 |
typename Parent::Notifier* nf = Parent::notifier(); |
2013 | 2005 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
2014 | 2006 |
Parent::set(it, 0); |
2015 | 2007 |
} |
2016 | 2008 |
} |
2017 | 2009 |
}; |
2018 | 2010 |
|
2019 | 2011 |
public: |
2020 | 2012 |
|
2021 | 2013 |
/// \brief Constructor. |
2022 | 2014 |
/// |
2023 | 2015 |
/// Constructor for creating out-degree map. |
2024 | 2016 |
explicit OutDegMap(const Digraph& digraph) |
2025 | 2017 |
: _digraph(digraph), _deg(digraph) { |
2026 | 2018 |
Parent::attach(_digraph.notifier(typename Digraph::Arc())); |
2027 | 2019 |
|
2028 | 2020 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
2029 | 2021 |
_deg[it] = countOutArcs(_digraph, it); |
2030 | 2022 |
} |
2031 | 2023 |
} |
2032 | 2024 |
|
2033 | 2025 |
/// Gives back the out-degree of a Node. |
2034 | 2026 |
int operator[](const Key& key) const { |
2035 | 2027 |
return _deg[key]; |
2036 | 2028 |
} |
2037 | 2029 |
|
2038 | 2030 |
protected: |
2039 | 2031 |
|
2040 | 2032 |
typedef typename Digraph::Arc Arc; |
2041 | 2033 |
|
2042 | 2034 |
virtual void add(const Arc& arc) { |
2043 | 2035 |
++_deg[_digraph.source(arc)]; |
2044 | 2036 |
} |
2045 | 2037 |
|
2046 | 2038 |
virtual void add(const std::vector<Arc>& arcs) { |
2047 | 2039 |
for (int i = 0; i < int(arcs.size()); ++i) { |
2048 | 2040 |
++_deg[_digraph.source(arcs[i])]; |
2049 | 2041 |
} |
2050 | 2042 |
} |
2051 | 2043 |
|
2052 | 2044 |
virtual void erase(const Arc& arc) { |
2053 | 2045 |
--_deg[_digraph.source(arc)]; |
2054 | 2046 |
} |
2055 | 2047 |
|
2056 | 2048 |
virtual void erase(const std::vector<Arc>& arcs) { |
2057 | 2049 |
for (int i = 0; i < int(arcs.size()); ++i) { |
2058 | 2050 |
--_deg[_digraph.source(arcs[i])]; |
2059 | 2051 |
} |
2060 | 2052 |
} |
2061 | 2053 |
|
2062 | 2054 |
virtual void build() { |
2063 | 2055 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
2064 | 2056 |
_deg[it] = countOutArcs(_digraph, it); |
2065 | 2057 |
} |
2066 | 2058 |
} |
2067 | 2059 |
|
2068 | 2060 |
virtual void clear() { |
2069 | 2061 |
for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) { |
2070 | 2062 |
_deg[it] = 0; |
2071 | 2063 |
} |
2072 | 2064 |
} |
2073 | 2065 |
private: |
2074 | 2066 |
|
2075 | 2067 |
const Digraph& _digraph; |
2076 | 2068 |
AutoNodeMap _deg; |
2077 | 2069 |
}; |
2078 | 2070 |
|
2079 | 2071 |
|
2080 | 2072 |
///Dynamic arc look up between given endpoints. |
2081 | 2073 |
|
2082 | 2074 |
///\ingroup gutils |
2083 | 2075 |
///Using this class, you can find an arc in a digraph from a given |
2084 | 2076 |
///source to a given target in amortized time <em>O(log d)</em>, |
2085 | 2077 |
///where <em>d</em> is the out-degree of the source node. |
2086 | 2078 |
/// |
2087 | 2079 |
///It is possible to find \e all parallel arcs between two nodes with |
2088 | 2080 |
///the \c findFirst() and \c findNext() members. |
2089 | 2081 |
/// |
2090 | 2082 |
///See the \ref ArcLookUp and \ref AllArcLookUp classes if your |
2091 | 2083 |
///digraph is not changed so frequently. |
2092 | 2084 |
/// |
2093 | 2085 |
///This class uses a self-adjusting binary search tree, Sleator's |
2094 | 2086 |
///and Tarjan's Splay tree for guarantee the logarithmic amortized |
2095 | 2087 |
///time bound for arc lookups. This class also guarantees the |
2096 | 2088 |
///optimal time bound in a constant factor for any distribution of |
2097 | 2089 |
///queries. |
2098 | 2090 |
/// |
2099 |
///\ |
|
2091 |
///\tparam G The type of the underlying digraph. |
|
2100 | 2092 |
/// |
2101 | 2093 |
///\sa ArcLookUp |
2102 | 2094 |
///\sa AllArcLookUp |
2103 | 2095 |
template<class G> |
2104 | 2096 |
class DynArcLookUp |
2105 | 2097 |
: protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase |
2106 | 2098 |
{ |
2107 | 2099 |
public: |
2108 | 2100 |
typedef typename ItemSetTraits<G, typename G::Arc> |
2109 | 2101 |
::ItemNotifier::ObserverBase Parent; |
2110 | 2102 |
|
2111 | 2103 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
2112 | 2104 |
typedef G Digraph; |
2113 | 2105 |
|
2114 | 2106 |
protected: |
2115 | 2107 |
|
2116 | 2108 |
class AutoNodeMap : public DefaultMap<G, Node, Arc> { |
2117 | 2109 |
public: |
2118 | 2110 |
|
2119 | 2111 |
typedef DefaultMap<G, Node, Arc> Parent; |
2120 | 2112 |
|
2121 | 2113 |
AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {} |
2122 | 2114 |
|
2123 | 2115 |
virtual void add(const Node& node) { |
2124 | 2116 |
Parent::add(node); |
2125 | 2117 |
Parent::set(node, INVALID); |
2126 | 2118 |
} |
2127 | 2119 |
|
2128 | 2120 |
virtual void add(const std::vector<Node>& nodes) { |
2129 | 2121 |
Parent::add(nodes); |
2130 | 2122 |
for (int i = 0; i < int(nodes.size()); ++i) { |
2131 | 2123 |
Parent::set(nodes[i], INVALID); |
2132 | 2124 |
} |
2133 | 2125 |
} |
2134 | 2126 |
|
2135 | 2127 |
virtual void build() { |
2136 | 2128 |
Parent::build(); |
2137 | 2129 |
Node it; |
2138 | 2130 |
typename Parent::Notifier* nf = Parent::notifier(); |
2139 | 2131 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
2140 | 2132 |
Parent::set(it, INVALID); |
2141 | 2133 |
} |
2142 | 2134 |
} |
2143 | 2135 |
}; |
2144 | 2136 |
|
2145 | 2137 |
const Digraph &_g; |
2146 | 2138 |
AutoNodeMap _head; |
2147 | 2139 |
typename Digraph::template ArcMap<Arc> _parent; |
2148 | 2140 |
typename Digraph::template ArcMap<Arc> _left; |
2149 | 2141 |
typename Digraph::template ArcMap<Arc> _right; |
2150 | 2142 |
|
2151 | 2143 |
class ArcLess { |
2152 | 2144 |
const Digraph &g; |
2153 | 2145 |
public: |
2154 | 2146 |
ArcLess(const Digraph &_g) : g(_g) {} |
2155 | 2147 |
bool operator()(Arc a,Arc b) const |
2156 | 2148 |
{ |
2157 | 2149 |
return g.target(a)<g.target(b); |
2158 | 2150 |
} |
2159 | 2151 |
}; |
2160 | 2152 |
|
2161 | 2153 |
public: |
2162 | 2154 |
|
2163 | 2155 |
///Constructor |
2164 | 2156 |
|
2165 | 2157 |
///Constructor. |
2166 | 2158 |
/// |
2167 | 2159 |
///It builds up the search database. |
2168 | 2160 |
DynArcLookUp(const Digraph &g) |
2169 | 2161 |
: _g(g),_head(g),_parent(g),_left(g),_right(g) |
2170 | 2162 |
{ |
2171 | 2163 |
Parent::attach(_g.notifier(typename Digraph::Arc())); |
2172 | 2164 |
refresh(); |
2173 | 2165 |
} |
2174 | 2166 |
|
2175 | 2167 |
protected: |
2176 | 2168 |
|
2177 | 2169 |
virtual void add(const Arc& arc) { |
2178 | 2170 |
insert(arc); |
2179 | 2171 |
} |
2180 | 2172 |
|
2181 | 2173 |
virtual void add(const std::vector<Arc>& arcs) { |
2182 | 2174 |
for (int i = 0; i < int(arcs.size()); ++i) { |
2183 | 2175 |
insert(arcs[i]); |
2184 | 2176 |
} |
2185 | 2177 |
} |
2186 | 2178 |
|
2187 | 2179 |
virtual void erase(const Arc& arc) { |
2188 | 2180 |
remove(arc); |
2189 | 2181 |
} |
2190 | 2182 |
|
2191 | 2183 |
virtual void erase(const std::vector<Arc>& arcs) { |
2192 | 2184 |
for (int i = 0; i < int(arcs.size()); ++i) { |
2193 | 2185 |
remove(arcs[i]); |
2194 | 2186 |
} |
2195 | 2187 |
} |
2196 | 2188 |
|
2197 | 2189 |
virtual void build() { |
2198 | 2190 |
refresh(); |
2199 | 2191 |
} |
2200 | 2192 |
|
2201 | 2193 |
virtual void clear() { |
2202 | 2194 |
for(NodeIt n(_g);n!=INVALID;++n) { |
2203 | 2195 |
_head.set(n, INVALID); |
2204 | 2196 |
} |
2205 | 2197 |
} |
2206 | 2198 |
|
2207 | 2199 |
void insert(Arc arc) { |
2208 | 2200 |
Node s = _g.source(arc); |
2209 | 2201 |
Node t = _g.target(arc); |
2210 | 2202 |
_left.set(arc, INVALID); |
2211 | 2203 |
_right.set(arc, INVALID); |
2212 | 2204 |
|
2213 | 2205 |
Arc e = _head[s]; |
2214 | 2206 |
if (e == INVALID) { |
2215 | 2207 |
_head.set(s, arc); |
2216 | 2208 |
_parent.set(arc, INVALID); |
2217 | 2209 |
return; |
2218 | 2210 |
} |
2219 | 2211 |
while (true) { |
2220 | 2212 |
if (t < _g.target(e)) { |
2221 | 2213 |
if (_left[e] == INVALID) { |
2222 | 2214 |
_left.set(e, arc); |
2223 | 2215 |
_parent.set(arc, e); |
2224 | 2216 |
splay(arc); |
2225 | 2217 |
return; |
2226 | 2218 |
} else { |
2227 | 2219 |
e = _left[e]; |
2228 | 2220 |
} |
2229 | 2221 |
} else { |
2230 | 2222 |
if (_right[e] == INVALID) { |
2231 | 2223 |
_right.set(e, arc); |
2232 | 2224 |
_parent.set(arc, e); |
2233 | 2225 |
splay(arc); |
2234 | 2226 |
return; |
2235 | 2227 |
} else { |
2236 | 2228 |
e = _right[e]; |
2237 | 2229 |
} |
2238 | 2230 |
} |
2239 | 2231 |
} |
2240 | 2232 |
} |
2241 | 2233 |
|
2242 | 2234 |
void remove(Arc arc) { |
2243 | 2235 |
if (_left[arc] == INVALID) { |
2244 | 2236 |
if (_right[arc] != INVALID) { |
2245 | 2237 |
_parent.set(_right[arc], _parent[arc]); |
2246 | 2238 |
} |
2247 | 2239 |
if (_parent[arc] != INVALID) { |
2248 | 2240 |
if (_left[_parent[arc]] == arc) { |
2249 | 2241 |
_left.set(_parent[arc], _right[arc]); |
2250 | 2242 |
} else { |
2251 | 2243 |
_right.set(_parent[arc], _right[arc]); |
2252 | 2244 |
} |
2253 | 2245 |
} else { |
2254 | 2246 |
_head.set(_g.source(arc), _right[arc]); |
2255 | 2247 |
} |
2256 | 2248 |
} else if (_right[arc] == INVALID) { |
2257 | 2249 |
_parent.set(_left[arc], _parent[arc]); |
2258 | 2250 |
if (_parent[arc] != INVALID) { |
2259 | 2251 |
if (_left[_parent[arc]] == arc) { |
2260 | 2252 |
_left.set(_parent[arc], _left[arc]); |
2261 | 2253 |
} else { |
2262 | 2254 |
_right.set(_parent[arc], _left[arc]); |
2263 | 2255 |
} |
2264 | 2256 |
} else { |
2265 | 2257 |
_head.set(_g.source(arc), _left[arc]); |
2266 | 2258 |
} |
2267 | 2259 |
} else { |
2268 | 2260 |
Arc e = _left[arc]; |
2269 | 2261 |
if (_right[e] != INVALID) { |
2270 | 2262 |
e = _right[e]; |
2271 | 2263 |
while (_right[e] != INVALID) { |
2272 | 2264 |
e = _right[e]; |
2273 | 2265 |
} |
2274 | 2266 |
Arc s = _parent[e]; |
2275 | 2267 |
_right.set(_parent[e], _left[e]); |
2276 | 2268 |
if (_left[e] != INVALID) { |
2277 | 2269 |
_parent.set(_left[e], _parent[e]); |
2278 | 2270 |
} |
2279 | 2271 |
|
2280 | 2272 |
_left.set(e, _left[arc]); |
2281 | 2273 |
_parent.set(_left[arc], e); |
2282 | 2274 |
_right.set(e, _right[arc]); |
2283 | 2275 |
_parent.set(_right[arc], e); |
2284 | 2276 |
|
2285 | 2277 |
_parent.set(e, _parent[arc]); |
2286 | 2278 |
if (_parent[arc] != INVALID) { |
2287 | 2279 |
if (_left[_parent[arc]] == arc) { |
2288 | 2280 |
_left.set(_parent[arc], e); |
2289 | 2281 |
} else { |
2290 | 2282 |
_right.set(_parent[arc], e); |
2291 | 2283 |
} |
2292 | 2284 |
} |
2293 | 2285 |
splay(s); |
2294 | 2286 |
} else { |
2295 | 2287 |
_right.set(e, _right[arc]); |
2296 | 2288 |
_parent.set(_right[arc], e); |
2297 | 2289 |
|
2298 | 2290 |
if (_parent[arc] != INVALID) { |
2299 | 2291 |
if (_left[_parent[arc]] == arc) { |
2300 | 2292 |
_left.set(_parent[arc], e); |
2301 | 2293 |
} else { |
2302 | 2294 |
_right.set(_parent[arc], e); |
2303 | 2295 |
} |
2304 | 2296 |
} else { |
2305 | 2297 |
_head.set(_g.source(arc), e); |
2306 | 2298 |
} |
2307 | 2299 |
} |
2308 | 2300 |
} |
2309 | 2301 |
} |
2310 | 2302 |
|
2311 | 2303 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
2312 | 2304 |
{ |
2313 | 2305 |
int m=(a+b)/2; |
2314 | 2306 |
Arc me=v[m]; |
2315 | 2307 |
if (a < m) { |
2316 | 2308 |
Arc left = refreshRec(v,a,m-1); |
2317 | 2309 |
_left.set(me, left); |
2318 | 2310 |
_parent.set(left, me); |
2319 | 2311 |
} else { |
2320 | 2312 |
_left.set(me, INVALID); |
2321 | 2313 |
} |
2322 | 2314 |
if (m < b) { |
2323 | 2315 |
Arc right = refreshRec(v,m+1,b); |
2324 | 2316 |
_right.set(me, right); |
2325 | 2317 |
_parent.set(right, me); |
2326 | 2318 |
} else { |
2327 | 2319 |
_right.set(me, INVALID); |
2328 | 2320 |
} |
2329 | 2321 |
return me; |
2330 | 2322 |
} |
2331 | 2323 |
|
2332 | 2324 |
void refresh() { |
2333 | 2325 |
for(NodeIt n(_g);n!=INVALID;++n) { |
2334 | 2326 |
std::vector<Arc> v; |
2335 | 2327 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e); |
2336 | 2328 |
if(v.size()) { |
2337 | 2329 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
2338 | 2330 |
Arc head = refreshRec(v,0,v.size()-1); |
2339 | 2331 |
_head.set(n, head); |
2340 | 2332 |
_parent.set(head, INVALID); |
2341 | 2333 |
} |
2342 | 2334 |
else _head.set(n, INVALID); |
2343 | 2335 |
} |
2344 | 2336 |
} |
2345 | 2337 |
|
2346 | 2338 |
void zig(Arc v) { |
2347 | 2339 |
Arc w = _parent[v]; |
2348 | 2340 |
_parent.set(v, _parent[w]); |
2349 | 2341 |
_parent.set(w, v); |
2350 | 2342 |
_left.set(w, _right[v]); |
2351 | 2343 |
_right.set(v, w); |
2352 | 2344 |
if (_parent[v] != INVALID) { |
2353 | 2345 |
if (_right[_parent[v]] == w) { |
2354 | 2346 |
_right.set(_parent[v], v); |
2355 | 2347 |
} else { |
2356 | 2348 |
_left.set(_parent[v], v); |
2357 | 2349 |
} |
2358 | 2350 |
} |
2359 | 2351 |
if (_left[w] != INVALID){ |
2360 | 2352 |
_parent.set(_left[w], w); |
2361 | 2353 |
} |
2362 | 2354 |
} |
2363 | 2355 |
|
2364 | 2356 |
void zag(Arc v) { |
2365 | 2357 |
Arc w = _parent[v]; |
2366 | 2358 |
_parent.set(v, _parent[w]); |
2367 | 2359 |
_parent.set(w, v); |
2368 | 2360 |
_right.set(w, _left[v]); |
2369 | 2361 |
_left.set(v, w); |
2370 | 2362 |
if (_parent[v] != INVALID){ |
2371 | 2363 |
if (_left[_parent[v]] == w) { |
2372 | 2364 |
_left.set(_parent[v], v); |
2373 | 2365 |
} else { |
2374 | 2366 |
_right.set(_parent[v], v); |
2375 | 2367 |
} |
2376 | 2368 |
} |
2377 | 2369 |
if (_right[w] != INVALID){ |
2378 | 2370 |
_parent.set(_right[w], w); |
2379 | 2371 |
} |
2380 | 2372 |
} |
2381 | 2373 |
|
2382 | 2374 |
void splay(Arc v) { |
2383 | 2375 |
while (_parent[v] != INVALID) { |
2384 | 2376 |
if (v == _left[_parent[v]]) { |
2385 | 2377 |
if (_parent[_parent[v]] == INVALID) { |
2386 | 2378 |
zig(v); |
2387 | 2379 |
} else { |
2388 | 2380 |
if (_parent[v] == _left[_parent[_parent[v]]]) { |
2389 | 2381 |
zig(_parent[v]); |
2390 | 2382 |
zig(v); |
2391 | 2383 |
} else { |
2392 | 2384 |
zig(v); |
2393 | 2385 |
zag(v); |
2394 | 2386 |
} |
2395 | 2387 |
} |
2396 | 2388 |
} else { |
2397 | 2389 |
if (_parent[_parent[v]] == INVALID) { |
2398 | 2390 |
zag(v); |
2399 | 2391 |
} else { |
2400 | 2392 |
if (_parent[v] == _left[_parent[_parent[v]]]) { |
2401 | 2393 |
zag(v); |
2402 | 2394 |
zig(v); |
2403 | 2395 |
} else { |
2404 | 2396 |
zag(_parent[v]); |
2405 | 2397 |
zag(v); |
2406 | 2398 |
} |
2407 | 2399 |
} |
2408 | 2400 |
} |
2409 | 2401 |
} |
2410 | 2402 |
_head[_g.source(v)] = v; |
2411 | 2403 |
} |
2412 | 2404 |
|
2413 | 2405 |
|
2414 | 2406 |
public: |
2415 | 2407 |
|
2416 | 2408 |
///Find an arc between two nodes. |
2417 | 2409 |
|
2418 | 2410 |
///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where |
2419 | 2411 |
/// <em>d</em> is the number of outgoing arcs of \c s. |
2420 | 2412 |
///\param s The source node |
2421 | 2413 |
///\param t The target node |
2422 | 2414 |
///\return An arc from \c s to \c t if there exists, |
2423 | 2415 |
///\ref INVALID otherwise. |
2424 | 2416 |
Arc operator()(Node s, Node t) const |
2425 | 2417 |
{ |
2426 | 2418 |
Arc a = _head[s]; |
2427 | 2419 |
while (true) { |
2428 | 2420 |
if (_g.target(a) == t) { |
2429 | 2421 |
const_cast<DynArcLookUp&>(*this).splay(a); |
2430 | 2422 |
return a; |
2431 | 2423 |
} else if (t < _g.target(a)) { |
2432 | 2424 |
if (_left[a] == INVALID) { |
2433 | 2425 |
const_cast<DynArcLookUp&>(*this).splay(a); |
2434 | 2426 |
return INVALID; |
2435 | 2427 |
} else { |
2436 | 2428 |
a = _left[a]; |
2437 | 2429 |
} |
2438 | 2430 |
} else { |
2439 | 2431 |
if (_right[a] == INVALID) { |
2440 | 2432 |
const_cast<DynArcLookUp&>(*this).splay(a); |
2441 | 2433 |
return INVALID; |
2442 | 2434 |
} else { |
2443 | 2435 |
a = _right[a]; |
2444 | 2436 |
} |
2445 | 2437 |
} |
2446 | 2438 |
} |
2447 | 2439 |
} |
2448 | 2440 |
|
2449 | 2441 |
///Find the first arc between two nodes. |
2450 | 2442 |
|
2451 | 2443 |
///Find the first arc between two nodes in time |
2452 | 2444 |
/// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of |
2453 | 2445 |
/// outgoing arcs of \c s. |
2454 | 2446 |
///\param s The source node |
2455 | 2447 |
///\param t The target node |
2456 | 2448 |
///\return An arc from \c s to \c t if there exists, \ref INVALID |
2457 | 2449 |
/// otherwise. |
2458 | 2450 |
Arc findFirst(Node s, Node t) const |
2459 | 2451 |
{ |
2460 | 2452 |
Arc a = _head[s]; |
2461 | 2453 |
Arc r = INVALID; |
2462 | 2454 |
while (true) { |
2463 | 2455 |
if (_g.target(a) < t) { |
2464 | 2456 |
if (_right[a] == INVALID) { |
2465 | 2457 |
const_cast<DynArcLookUp&>(*this).splay(a); |
2466 | 2458 |
return r; |
2467 | 2459 |
} else { |
2468 | 2460 |
a = _right[a]; |
2469 | 2461 |
} |
2470 | 2462 |
} else { |
2471 | 2463 |
if (_g.target(a) == t) { |
2472 | 2464 |
r = a; |
2473 | 2465 |
} |
2474 | 2466 |
if (_left[a] == INVALID) { |
2475 | 2467 |
const_cast<DynArcLookUp&>(*this).splay(a); |
2476 | 2468 |
return r; |
2477 | 2469 |
} else { |
2478 | 2470 |
a = _left[a]; |
2479 | 2471 |
} |
2480 | 2472 |
} |
2481 | 2473 |
} |
2482 | 2474 |
} |
2483 | 2475 |
|
2484 | 2476 |
///Find the next arc between two nodes. |
2485 | 2477 |
|
2486 | 2478 |
///Find the next arc between two nodes in time |
2487 | 2479 |
/// <em>O(</em>log<em>d)</em>, where <em>d</em> is the number of |
2488 | 2480 |
/// outgoing arcs of \c s. |
2489 | 2481 |
///\param s The source node |
2490 | 2482 |
///\param t The target node |
2491 | 2483 |
///\return An arc from \c s to \c t if there exists, \ref INVALID |
2492 | 2484 |
/// otherwise. |
2493 | 2485 |
|
2494 | 2486 |
///\note If \c e is not the result of the previous \c findFirst() |
2495 | 2487 |
///operation then the amorized time bound can not be guaranteed. |
2496 | 2488 |
#ifdef DOXYGEN |
2497 | 2489 |
Arc findNext(Node s, Node t, Arc a) const |
2498 | 2490 |
#else |
2499 | 2491 |
Arc findNext(Node, Node t, Arc a) const |
2500 | 2492 |
#endif |
2501 | 2493 |
{ |
2502 | 2494 |
if (_right[a] != INVALID) { |
2503 | 2495 |
a = _right[a]; |
2504 | 2496 |
while (_left[a] != INVALID) { |
2505 | 2497 |
a = _left[a]; |
2506 | 2498 |
} |
2507 | 2499 |
const_cast<DynArcLookUp&>(*this).splay(a); |
2508 | 2500 |
} else { |
2509 | 2501 |
while (_parent[a] != INVALID && _right[_parent[a]] == a) { |
2510 | 2502 |
a = _parent[a]; |
2511 | 2503 |
} |
2512 | 2504 |
if (_parent[a] == INVALID) { |
2513 | 2505 |
return INVALID; |
2514 | 2506 |
} else { |
2515 | 2507 |
a = _parent[a]; |
2516 | 2508 |
const_cast<DynArcLookUp&>(*this).splay(a); |
2517 | 2509 |
} |
2518 | 2510 |
} |
2519 | 2511 |
if (_g.target(a) == t) return a; |
2520 | 2512 |
else return INVALID; |
2521 | 2513 |
} |
2522 | 2514 |
|
2523 | 2515 |
}; |
2524 | 2516 |
|
2525 | 2517 |
///Fast arc look up between given endpoints. |
2526 | 2518 |
|
2527 | 2519 |
///\ingroup gutils |
2528 | 2520 |
///Using this class, you can find an arc in a digraph from a given |
2529 | 2521 |
///source to a given target in time <em>O(log d)</em>, |
2530 | 2522 |
///where <em>d</em> is the out-degree of the source node. |
2531 | 2523 |
/// |
2532 | 2524 |
///It is not possible to find \e all parallel arcs between two nodes. |
2533 | 2525 |
///Use \ref AllArcLookUp for this purpose. |
2534 | 2526 |
/// |
2535 | 2527 |
///\warning This class is static, so you should refresh() (or at least |
2536 | 2528 |
///refresh(Node)) this data structure |
2537 | 2529 |
///whenever the digraph changes. This is a time consuming (superlinearly |
2538 | 2530 |
///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs). |
2539 | 2531 |
/// |
2540 |
///\ |
|
2532 |
///\tparam G The type of the underlying digraph. |
|
2541 | 2533 |
/// |
2542 | 2534 |
///\sa DynArcLookUp |
2543 | 2535 |
///\sa AllArcLookUp |
2544 | 2536 |
template<class G> |
2545 | 2537 |
class ArcLookUp |
2546 | 2538 |
{ |
2547 | 2539 |
public: |
2548 | 2540 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
2549 | 2541 |
typedef G Digraph; |
2550 | 2542 |
|
2551 | 2543 |
protected: |
2552 | 2544 |
const Digraph &_g; |
2553 | 2545 |
typename Digraph::template NodeMap<Arc> _head; |
2554 | 2546 |
typename Digraph::template ArcMap<Arc> _left; |
2555 | 2547 |
typename Digraph::template ArcMap<Arc> _right; |
2556 | 2548 |
|
2557 | 2549 |
class ArcLess { |
2558 | 2550 |
const Digraph &g; |
2559 | 2551 |
public: |
2560 | 2552 |
ArcLess(const Digraph &_g) : g(_g) {} |
2561 | 2553 |
bool operator()(Arc a,Arc b) const |
2562 | 2554 |
{ |
2563 | 2555 |
return g.target(a)<g.target(b); |
2564 | 2556 |
} |
2565 | 2557 |
}; |
2566 | 2558 |
|
2567 | 2559 |
public: |
2568 | 2560 |
|
2569 | 2561 |
///Constructor |
2570 | 2562 |
|
2571 | 2563 |
///Constructor. |
2572 | 2564 |
/// |
2573 | 2565 |
///It builds up the search database, which remains valid until the digraph |
2574 | 2566 |
///changes. |
2575 | 2567 |
ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();} |
2576 | 2568 |
|
2577 | 2569 |
private: |
2578 | 2570 |
Arc refreshRec(std::vector<Arc> &v,int a,int b) |
2579 | 2571 |
{ |
2580 | 2572 |
int m=(a+b)/2; |
2581 | 2573 |
Arc me=v[m]; |
2582 | 2574 |
_left[me] = a<m?refreshRec(v,a,m-1):INVALID; |
2583 | 2575 |
_right[me] = m<b?refreshRec(v,m+1,b):INVALID; |
2584 | 2576 |
return me; |
2585 | 2577 |
} |
2586 | 2578 |
public: |
2587 | 2579 |
///Refresh the data structure at a node. |
2588 | 2580 |
|
2589 | 2581 |
///Build up the search database of node \c n. |
2590 | 2582 |
/// |
2591 | 2583 |
///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is |
2592 | 2584 |
///the number of the outgoing arcs of \c n. |
2593 | 2585 |
void refresh(Node n) |
2594 | 2586 |
{ |
2595 | 2587 |
std::vector<Arc> v; |
2596 | 2588 |
for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e); |
2597 | 2589 |
if(v.size()) { |
2598 | 2590 |
std::sort(v.begin(),v.end(),ArcLess(_g)); |
2599 | 2591 |
_head[n]=refreshRec(v,0,v.size()-1); |
2600 | 2592 |
} |
2601 | 2593 |
else _head[n]=INVALID; |
2602 | 2594 |
} |
2603 | 2595 |
///Refresh the full data structure. |
2604 | 2596 |
|
2605 | 2597 |
///Build up the full search database. In fact, it simply calls |
2606 | 2598 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
2607 | 2599 |
/// |
2608 | 2600 |
///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is |
2609 | 2601 |
///the number of the arcs of \c n and <em>D</em> is the maximum |
2610 | 2602 |
///out-degree of the digraph. |
2611 | 2603 |
|
2612 | 2604 |
void refresh() |
2613 | 2605 |
{ |
2614 | 2606 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(n); |
2615 | 2607 |
} |
2616 | 2608 |
|
2617 | 2609 |
///Find an arc between two nodes. |
2618 | 2610 |
|
2619 | 2611 |
///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where |
2620 | 2612 |
/// <em>d</em> is the number of outgoing arcs of \c s. |
2621 | 2613 |
///\param s The source node |
2622 | 2614 |
///\param t The target node |
2623 | 2615 |
///\return An arc from \c s to \c t if there exists, |
2624 | 2616 |
///\ref INVALID otherwise. |
2625 | 2617 |
/// |
2626 | 2618 |
///\warning If you change the digraph, refresh() must be called before using |
2627 | 2619 |
///this operator. If you change the outgoing arcs of |
2628 | 2620 |
///a single node \c n, then |
2629 | 2621 |
///\ref refresh(Node) "refresh(n)" is enough. |
2630 | 2622 |
/// |
2631 | 2623 |
Arc operator()(Node s, Node t) const |
2632 | 2624 |
{ |
2633 | 2625 |
Arc e; |
2634 | 2626 |
for(e=_head[s]; |
2635 | 2627 |
e!=INVALID&&_g.target(e)!=t; |
2636 | 2628 |
e = t < _g.target(e)?_left[e]:_right[e]) ; |
2637 | 2629 |
return e; |
2638 | 2630 |
} |
2639 | 2631 |
|
2640 | 2632 |
}; |
2641 | 2633 |
|
2642 | 2634 |
///Fast look up of all arcs between given endpoints. |
2643 | 2635 |
|
2644 | 2636 |
///\ingroup gutils |
2645 | 2637 |
///This class is the same as \ref ArcLookUp, with the addition |
2646 | 2638 |
///that it makes it possible to find all arcs between given endpoints. |
2647 | 2639 |
/// |
2648 | 2640 |
///\warning This class is static, so you should refresh() (or at least |
2649 | 2641 |
///refresh(Node)) this data structure |
2650 | 2642 |
///whenever the digraph changes. This is a time consuming (superlinearly |
2651 | 2643 |
///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs). |
2652 | 2644 |
/// |
2653 |
///\ |
|
2645 |
///\tparam G The type of the underlying digraph. |
|
2654 | 2646 |
/// |
2655 | 2647 |
///\sa DynArcLookUp |
2656 | 2648 |
///\sa ArcLookUp |
2657 | 2649 |
template<class G> |
2658 | 2650 |
class AllArcLookUp : public ArcLookUp<G> |
2659 | 2651 |
{ |
2660 | 2652 |
using ArcLookUp<G>::_g; |
2661 | 2653 |
using ArcLookUp<G>::_right; |
2662 | 2654 |
using ArcLookUp<G>::_left; |
2663 | 2655 |
using ArcLookUp<G>::_head; |
2664 | 2656 |
|
2665 | 2657 |
TEMPLATE_DIGRAPH_TYPEDEFS(G); |
2666 | 2658 |
typedef G Digraph; |
2667 | 2659 |
|
2668 | 2660 |
typename Digraph::template ArcMap<Arc> _next; |
2669 | 2661 |
|
2670 | 2662 |
Arc refreshNext(Arc head,Arc next=INVALID) |
2671 | 2663 |
{ |
2672 | 2664 |
if(head==INVALID) return next; |
2673 | 2665 |
else { |
2674 | 2666 |
next=refreshNext(_right[head],next); |
2675 | 2667 |
// _next[head]=next; |
2676 | 2668 |
_next[head]=( next!=INVALID && _g.target(next)==_g.target(head)) |
2677 | 2669 |
? next : INVALID; |
2678 | 2670 |
return refreshNext(_left[head],head); |
2679 | 2671 |
} |
2680 | 2672 |
} |
2681 | 2673 |
|
2682 | 2674 |
void refreshNext() |
2683 | 2675 |
{ |
2684 | 2676 |
for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]); |
2685 | 2677 |
} |
2686 | 2678 |
|
2687 | 2679 |
public: |
2688 | 2680 |
///Constructor |
2689 | 2681 |
|
2690 | 2682 |
///Constructor. |
2691 | 2683 |
/// |
2692 | 2684 |
///It builds up the search database, which remains valid until the digraph |
2693 | 2685 |
///changes. |
2694 | 2686 |
AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();} |
2695 | 2687 |
|
2696 | 2688 |
///Refresh the data structure at a node. |
2697 | 2689 |
|
2698 | 2690 |
///Build up the search database of node \c n. |
2699 | 2691 |
/// |
2700 | 2692 |
///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is |
2701 | 2693 |
///the number of the outgoing arcs of \c n. |
2702 | 2694 |
|
2703 | 2695 |
void refresh(Node n) |
2704 | 2696 |
{ |
2705 | 2697 |
ArcLookUp<G>::refresh(n); |
2706 | 2698 |
refreshNext(_head[n]); |
2707 | 2699 |
} |
2708 | 2700 |
|
2709 | 2701 |
///Refresh the full data structure. |
2710 | 2702 |
|
2711 | 2703 |
///Build up the full search database. In fact, it simply calls |
2712 | 2704 |
///\ref refresh(Node) "refresh(n)" for each node \c n. |
2713 | 2705 |
/// |
2714 | 2706 |
///It runs in time <em>O(m</em>log<em>D)</em>, where <em>m</em> is |
2715 | 2707 |
///the number of the arcs of \c n and <em>D</em> is the maximum |
2716 | 2708 |
///out-degree of the digraph. |
2717 | 2709 |
|
2718 | 2710 |
void refresh() |
2719 | 2711 |
{ |
2720 | 2712 |
for(NodeIt n(_g);n!=INVALID;++n) refresh(_head[n]); |
2721 | 2713 |
} |
2722 | 2714 |
|
2723 | 2715 |
///Find an arc between two nodes. |
2724 | 2716 |
|
2725 | 2717 |
///Find an arc between two nodes. |
2726 | 2718 |
///\param s The source node |
2727 | 2719 |
///\param t The target node |
2728 | 2720 |
///\param prev The previous arc between \c s and \c t. It it is INVALID or |
2729 | 2721 |
///not given, the operator finds the first appropriate arc. |
2730 | 2722 |
///\return An arc from \c s to \c t after \c prev or |
2731 | 2723 |
///\ref INVALID if there is no more. |
2732 | 2724 |
/// |
2733 | 2725 |
///For example, you can count the number of arcs from \c u to \c v in the |
2734 | 2726 |
///following way. |
2735 | 2727 |
///\code |
2736 | 2728 |
///AllArcLookUp<ListDigraph> ae(g); |
2737 | 2729 |
///... |
2738 | 2730 |
///int n=0; |
2739 | 2731 |
///for(Arc e=ae(u,v);e!=INVALID;e=ae(u,v,e)) n++; |
2740 | 2732 |
///\endcode |
2741 | 2733 |
/// |
2742 | 2734 |
///Finding the first arc take <em>O(</em>log<em>d)</em> time, where |
2743 | 2735 |
/// <em>d</em> is the number of outgoing arcs of \c s. Then, the |
2744 | 2736 |
///consecutive arcs are found in constant time. |
2745 | 2737 |
/// |
2746 | 2738 |
///\warning If you change the digraph, refresh() must be called before using |
2747 | 2739 |
///this operator. If you change the outgoing arcs of |
2748 | 2740 |
///a single node \c n, then |
2749 | 2741 |
///\ref refresh(Node) "refresh(n)" is enough. |
2750 | 2742 |
/// |
2751 | 2743 |
#ifdef DOXYGEN |
2752 | 2744 |
Arc operator()(Node s, Node t, Arc prev=INVALID) const {} |
2753 | 2745 |
#else |
2754 | 2746 |
using ArcLookUp<G>::operator() ; |
2755 | 2747 |
Arc operator()(Node s, Node t, Arc prev) const |
2756 | 2748 |
{ |
2757 | 2749 |
return prev==INVALID?(*this)(s,t):_next[prev]; |
2758 | 2750 |
} |
2759 | 2751 |
#endif |
2760 | 2752 |
|
2761 | 2753 |
}; |
2762 | 2754 |
|
2763 | 2755 |
/// @} |
2764 | 2756 |
|
2765 | 2757 |
} //END OF NAMESPACE LEMON |
2766 | 2758 |
|
2767 | 2759 |
#endif |
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup paths |
20 | 20 |
///\file |
21 | 21 |
///\brief Classes for representing paths in digraphs. |
22 | 22 |
/// |
23 | 23 |
|
24 | 24 |
#ifndef LEMON_PATH_H |
25 | 25 |
#define LEMON_PATH_H |
26 | 26 |
|
27 | 27 |
#include <vector> |
28 | 28 |
#include <algorithm> |
29 | 29 |
|
30 | 30 |
#include <lemon/error.h> |
31 | 31 |
#include <lemon/bits/invalid.h> |
32 | 32 |
#include <lemon/concepts/path.h> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \addtogroup paths |
37 | 37 |
/// @{ |
38 | 38 |
|
39 | 39 |
|
40 | 40 |
/// \brief A structure for representing directed paths in a digraph. |
41 | 41 |
/// |
42 | 42 |
/// A structure for representing directed path in a digraph. |
43 |
/// \ |
|
43 |
/// \tparam _Digraph The digraph type in which the path is. |
|
44 | 44 |
/// |
45 | 45 |
/// In a sense, the path can be treated as a list of arcs. The |
46 | 46 |
/// lemon path type stores just this list. As a consequence, it |
47 | 47 |
/// cannot enumerate the nodes of the path and the source node of |
48 | 48 |
/// a zero length path is undefined. |
49 | 49 |
/// |
50 | 50 |
/// This implementation is a back and front insertable and erasable |
51 | 51 |
/// path type. It can be indexed in O(1) time. The front and back |
52 | 52 |
/// insertion and erase is done in O(1) (amortized) time. The |
53 | 53 |
/// implementation uses two vectors for storing the front and back |
54 | 54 |
/// insertions. |
55 | 55 |
template <typename _Digraph> |
56 | 56 |
class Path { |
57 | 57 |
public: |
58 | 58 |
|
59 | 59 |
typedef _Digraph Digraph; |
60 | 60 |
typedef typename Digraph::Arc Arc; |
61 | 61 |
|
62 | 62 |
/// \brief Default constructor |
63 | 63 |
/// |
64 | 64 |
/// Default constructor |
65 | 65 |
Path() {} |
66 | 66 |
|
67 | 67 |
/// \brief Template copy constructor |
68 | 68 |
/// |
69 | 69 |
/// This constuctor initializes the path from any other path type. |
70 | 70 |
/// It simply makes a copy of the given path. |
71 | 71 |
template <typename CPath> |
72 | 72 |
Path(const CPath& cpath) { |
73 | 73 |
copyPath(*this, cpath); |
74 | 74 |
} |
75 | 75 |
|
76 | 76 |
/// \brief Template copy assignment |
77 | 77 |
/// |
78 | 78 |
/// This operator makes a copy of a path of any other type. |
79 | 79 |
template <typename CPath> |
80 | 80 |
Path& operator=(const CPath& cpath) { |
81 | 81 |
copyPath(*this, cpath); |
82 | 82 |
return *this; |
83 | 83 |
} |
84 | 84 |
|
85 | 85 |
/// \brief Lemon style iterator for path arcs |
86 | 86 |
/// |
87 | 87 |
/// This class is used to iterate on the arcs of the paths. |
88 | 88 |
class ArcIt { |
89 | 89 |
friend class Path; |
90 | 90 |
public: |
91 | 91 |
/// \brief Default constructor |
92 | 92 |
ArcIt() {} |
93 | 93 |
/// \brief Invalid constructor |
94 | 94 |
ArcIt(Invalid) : path(0), idx(-1) {} |
95 | 95 |
/// \brief Initializate the iterator to the first arc of path |
96 | 96 |
ArcIt(const Path &_path) |
97 | 97 |
: path(&_path), idx(_path.empty() ? -1 : 0) {} |
98 | 98 |
|
99 | 99 |
private: |
100 | 100 |
|
101 | 101 |
ArcIt(const Path &_path, int _idx) |
102 | 102 |
: path(&_path), idx(_idx) {} |
103 | 103 |
|
104 | 104 |
public: |
105 | 105 |
|
106 | 106 |
/// \brief Conversion to Arc |
107 | 107 |
operator const Arc&() const { |
108 | 108 |
return path->nth(idx); |
109 | 109 |
} |
110 | 110 |
|
111 | 111 |
/// \brief Next arc |
112 | 112 |
ArcIt& operator++() { |
113 | 113 |
++idx; |
114 | 114 |
if (idx >= path->length()) idx = -1; |
115 | 115 |
return *this; |
116 | 116 |
} |
117 | 117 |
|
118 | 118 |
/// \brief Comparison operator |
119 | 119 |
bool operator==(const ArcIt& e) const { return idx==e.idx; } |
120 | 120 |
/// \brief Comparison operator |
121 | 121 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; } |
122 | 122 |
/// \brief Comparison operator |
123 | 123 |
bool operator<(const ArcIt& e) const { return idx<e.idx; } |
124 | 124 |
|
125 | 125 |
private: |
126 | 126 |
const Path *path; |
127 | 127 |
int idx; |
128 | 128 |
}; |
129 | 129 |
|
130 | 130 |
/// \brief Length of the path. |
131 | 131 |
int length() const { return head.size() + tail.size(); } |
132 | 132 |
/// \brief Return whether the path is empty. |
133 | 133 |
bool empty() const { return head.empty() && tail.empty(); } |
134 | 134 |
|
135 | 135 |
/// \brief Reset the path to an empty one. |
136 | 136 |
void clear() { head.clear(); tail.clear(); } |
137 | 137 |
|
138 | 138 |
/// \brief The nth arc. |
139 | 139 |
/// |
140 | 140 |
/// \pre n is in the [0..length() - 1] range |
141 | 141 |
const Arc& nth(int n) const { |
142 | 142 |
return n < int(head.size()) ? *(head.rbegin() + n) : |
143 | 143 |
*(tail.begin() + (n - head.size())); |
144 | 144 |
} |
145 | 145 |
|
146 | 146 |
/// \brief Initialize arc iterator to point to the nth arc |
147 | 147 |
/// |
148 | 148 |
/// \pre n is in the [0..length() - 1] range |
149 | 149 |
ArcIt nthIt(int n) const { |
150 | 150 |
return ArcIt(*this, n); |
151 | 151 |
} |
152 | 152 |
|
153 | 153 |
/// \brief The first arc of the path |
154 | 154 |
const Arc& front() const { |
155 | 155 |
return head.empty() ? tail.front() : head.back(); |
156 | 156 |
} |
157 | 157 |
|
158 | 158 |
/// \brief Add a new arc before the current path |
159 | 159 |
void addFront(const Arc& arc) { |
160 | 160 |
head.push_back(arc); |
161 | 161 |
} |
162 | 162 |
|
163 | 163 |
/// \brief Erase the first arc of the path |
164 | 164 |
void eraseFront() { |
165 | 165 |
if (!head.empty()) { |
166 | 166 |
head.pop_back(); |
167 | 167 |
} else { |
168 | 168 |
head.clear(); |
169 | 169 |
int halfsize = tail.size() / 2; |
170 | 170 |
head.resize(halfsize); |
171 | 171 |
std::copy(tail.begin() + 1, tail.begin() + halfsize + 1, |
172 | 172 |
head.rbegin()); |
173 | 173 |
std::copy(tail.begin() + halfsize + 1, tail.end(), tail.begin()); |
174 | 174 |
tail.resize(tail.size() - halfsize - 1); |
175 | 175 |
} |
176 | 176 |
} |
177 | 177 |
|
178 | 178 |
/// \brief The last arc of the path |
179 | 179 |
const Arc& back() const { |
180 | 180 |
return tail.empty() ? head.front() : tail.back(); |
181 | 181 |
} |
182 | 182 |
|
183 | 183 |
/// \brief Add a new arc behind the current path |
184 | 184 |
void addBack(const Arc& arc) { |
185 | 185 |
tail.push_back(arc); |
186 | 186 |
} |
187 | 187 |
|
188 | 188 |
/// \brief Erase the last arc of the path |
189 | 189 |
void eraseBack() { |
190 | 190 |
if (!tail.empty()) { |
191 | 191 |
tail.pop_back(); |
192 | 192 |
} else { |
193 | 193 |
int halfsize = head.size() / 2; |
194 | 194 |
tail.resize(halfsize); |
195 | 195 |
std::copy(head.begin() + 1, head.begin() + halfsize + 1, |
196 | 196 |
tail.rbegin()); |
197 | 197 |
std::copy(head.begin() + halfsize + 1, head.end(), head.begin()); |
198 | 198 |
head.resize(head.size() - halfsize - 1); |
199 | 199 |
} |
200 | 200 |
} |
201 | 201 |
|
202 | 202 |
typedef True BuildTag; |
203 | 203 |
|
204 | 204 |
template <typename CPath> |
205 | 205 |
void build(const CPath& path) { |
206 | 206 |
int len = path.length(); |
207 | 207 |
tail.reserve(len); |
208 | 208 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) { |
209 | 209 |
tail.push_back(it); |
210 | 210 |
} |
211 | 211 |
} |
212 | 212 |
|
213 | 213 |
template <typename CPath> |
214 | 214 |
void buildRev(const CPath& path) { |
215 | 215 |
int len = path.length(); |
216 | 216 |
head.reserve(len); |
217 | 217 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) { |
218 | 218 |
head.push_back(it); |
219 | 219 |
} |
220 | 220 |
} |
221 | 221 |
|
222 | 222 |
protected: |
223 | 223 |
typedef std::vector<Arc> Container; |
224 | 224 |
Container head, tail; |
225 | 225 |
|
226 | 226 |
}; |
227 | 227 |
|
228 | 228 |
/// \brief A structure for representing directed paths in a digraph. |
229 | 229 |
/// |
230 | 230 |
/// A structure for representing directed path in a digraph. |
231 |
/// \ |
|
231 |
/// \tparam _Digraph The digraph type in which the path is. |
|
232 | 232 |
/// |
233 | 233 |
/// In a sense, the path can be treated as a list of arcs. The |
234 | 234 |
/// lemon path type stores just this list. As a consequence it |
235 | 235 |
/// cannot enumerate the nodes in the path and the zero length paths |
236 | 236 |
/// cannot store the source. |
237 | 237 |
/// |
238 | 238 |
/// This implementation is a just back insertable and erasable path |
239 | 239 |
/// type. It can be indexed in O(1) time. The back insertion and |
240 | 240 |
/// erasure is amortized O(1) time. This implementation is faster |
241 | 241 |
/// then the \c Path type because it use just one vector for the |
242 | 242 |
/// arcs. |
243 | 243 |
template <typename _Digraph> |
244 | 244 |
class SimplePath { |
245 | 245 |
public: |
246 | 246 |
|
247 | 247 |
typedef _Digraph Digraph; |
248 | 248 |
typedef typename Digraph::Arc Arc; |
249 | 249 |
|
250 | 250 |
/// \brief Default constructor |
251 | 251 |
/// |
252 | 252 |
/// Default constructor |
253 | 253 |
SimplePath() {} |
254 | 254 |
|
255 | 255 |
/// \brief Template copy constructor |
256 | 256 |
/// |
257 | 257 |
/// This path can be initialized with any other path type. It just |
258 | 258 |
/// makes a copy of the given path. |
259 | 259 |
template <typename CPath> |
260 | 260 |
SimplePath(const CPath& cpath) { |
261 | 261 |
copyPath(*this, cpath); |
262 | 262 |
} |
263 | 263 |
|
264 | 264 |
/// \brief Template copy assignment |
265 | 265 |
/// |
266 | 266 |
/// This path can be initialized with any other path type. It just |
267 | 267 |
/// makes a copy of the given path. |
268 | 268 |
template <typename CPath> |
269 | 269 |
SimplePath& operator=(const CPath& cpath) { |
270 | 270 |
copyPath(*this, cpath); |
271 | 271 |
return *this; |
272 | 272 |
} |
273 | 273 |
|
274 | 274 |
/// \brief Iterator class to iterate on the arcs of the paths |
275 | 275 |
/// |
276 | 276 |
/// This class is used to iterate on the arcs of the paths |
277 | 277 |
/// |
278 | 278 |
/// Of course it converts to Digraph::Arc |
279 | 279 |
class ArcIt { |
280 | 280 |
friend class SimplePath; |
281 | 281 |
public: |
282 | 282 |
/// Default constructor |
283 | 283 |
ArcIt() {} |
284 | 284 |
/// Invalid constructor |
285 | 285 |
ArcIt(Invalid) : path(0), idx(-1) {} |
286 | 286 |
/// \brief Initializate the constructor to the first arc of path |
287 | 287 |
ArcIt(const SimplePath &_path) |
288 | 288 |
: path(&_path), idx(_path.empty() ? -1 : 0) {} |
289 | 289 |
|
290 | 290 |
private: |
291 | 291 |
|
292 | 292 |
/// Constructor with starting point |
293 | 293 |
ArcIt(const SimplePath &_path, int _idx) |
294 | 294 |
: idx(_idx), path(&_path) {} |
295 | 295 |
|
296 | 296 |
public: |
297 | 297 |
|
298 | 298 |
///Conversion to Digraph::Arc |
299 | 299 |
operator const Arc&() const { |
300 | 300 |
return path->nth(idx); |
301 | 301 |
} |
302 | 302 |
|
303 | 303 |
/// Next arc |
304 | 304 |
ArcIt& operator++() { |
305 | 305 |
++idx; |
306 | 306 |
if (idx >= path->length()) idx = -1; |
307 | 307 |
return *this; |
308 | 308 |
} |
309 | 309 |
|
310 | 310 |
/// Comparison operator |
311 | 311 |
bool operator==(const ArcIt& e) const { return idx==e.idx; } |
312 | 312 |
/// Comparison operator |
313 | 313 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; } |
314 | 314 |
/// Comparison operator |
315 | 315 |
bool operator<(const ArcIt& e) const { return idx<e.idx; } |
316 | 316 |
|
317 | 317 |
private: |
318 | 318 |
const SimplePath *path; |
319 | 319 |
int idx; |
320 | 320 |
}; |
321 | 321 |
|
322 | 322 |
/// \brief Length of the path. |
323 | 323 |
int length() const { return data.size(); } |
324 | 324 |
/// \brief Return true if the path is empty. |
325 | 325 |
bool empty() const { return data.empty(); } |
326 | 326 |
|
327 | 327 |
/// \brief Reset the path to an empty one. |
328 | 328 |
void clear() { data.clear(); } |
329 | 329 |
|
330 | 330 |
/// \brief The nth arc. |
331 | 331 |
/// |
332 | 332 |
/// \pre n is in the [0..length() - 1] range |
333 | 333 |
const Arc& nth(int n) const { |
334 | 334 |
return data[n]; |
335 | 335 |
} |
336 | 336 |
|
337 | 337 |
/// \brief Initializes arc iterator to point to the nth arc. |
338 | 338 |
ArcIt nthIt(int n) const { |
339 | 339 |
return ArcIt(*this, n); |
340 | 340 |
} |
341 | 341 |
|
342 | 342 |
/// \brief The first arc of the path. |
343 | 343 |
const Arc& front() const { |
344 | 344 |
return data.front(); |
345 | 345 |
} |
346 | 346 |
|
347 | 347 |
/// \brief The last arc of the path. |
348 | 348 |
const Arc& back() const { |
349 | 349 |
return data.back(); |
350 | 350 |
} |
351 | 351 |
|
352 | 352 |
/// \brief Add a new arc behind the current path. |
353 | 353 |
void addBack(const Arc& arc) { |
354 | 354 |
data.push_back(arc); |
355 | 355 |
} |
356 | 356 |
|
357 | 357 |
/// \brief Erase the last arc of the path |
358 | 358 |
void eraseBack() { |
359 | 359 |
data.pop_back(); |
360 | 360 |
} |
361 | 361 |
|
362 | 362 |
typedef True BuildTag; |
363 | 363 |
|
364 | 364 |
template <typename CPath> |
365 | 365 |
void build(const CPath& path) { |
366 | 366 |
int len = path.length(); |
367 | 367 |
data.resize(len); |
368 | 368 |
int index = 0; |
369 | 369 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) { |
370 | 370 |
data[index] = it;; |
371 | 371 |
++index; |
372 | 372 |
} |
373 | 373 |
} |
374 | 374 |
|
375 | 375 |
template <typename CPath> |
376 | 376 |
void buildRev(const CPath& path) { |
377 | 377 |
int len = path.length(); |
378 | 378 |
data.resize(len); |
379 | 379 |
int index = len; |
380 | 380 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) { |
381 | 381 |
--index; |
382 | 382 |
data[index] = it;; |
383 | 383 |
} |
384 | 384 |
} |
385 | 385 |
|
386 | 386 |
protected: |
387 | 387 |
typedef std::vector<Arc> Container; |
388 | 388 |
Container data; |
389 | 389 |
|
390 | 390 |
}; |
391 | 391 |
|
392 | 392 |
/// \brief A structure for representing directed paths in a digraph. |
393 | 393 |
/// |
394 | 394 |
/// A structure for representing directed path in a digraph. |
395 |
/// \ |
|
395 |
/// \tparam _Digraph The digraph type in which the path is. |
|
396 | 396 |
/// |
397 | 397 |
/// In a sense, the path can be treated as a list of arcs. The |
398 | 398 |
/// lemon path type stores just this list. As a consequence it |
399 | 399 |
/// cannot enumerate the nodes in the path and the zero length paths |
400 | 400 |
/// cannot store the source. |
401 | 401 |
/// |
402 | 402 |
/// This implementation is a back and front insertable and erasable |
403 | 403 |
/// path type. It can be indexed in O(k) time, where k is the rank |
404 | 404 |
/// of the arc in the path. The length can be computed in O(n) |
405 | 405 |
/// time. The front and back insertion and erasure is O(1) time |
406 | 406 |
/// and it can be splited and spliced in O(1) time. |
407 | 407 |
template <typename _Digraph> |
408 | 408 |
class ListPath { |
409 | 409 |
public: |
410 | 410 |
|
411 | 411 |
typedef _Digraph Digraph; |
412 | 412 |
typedef typename Digraph::Arc Arc; |
413 | 413 |
|
414 | 414 |
protected: |
415 | 415 |
|
416 | 416 |
// the std::list<> is incompatible |
417 | 417 |
// hard to create invalid iterator |
418 | 418 |
struct Node { |
419 | 419 |
Arc arc; |
420 | 420 |
Node *next, *prev; |
421 | 421 |
}; |
422 | 422 |
|
423 | 423 |
Node *first, *last; |
424 | 424 |
|
425 | 425 |
std::allocator<Node> alloc; |
426 | 426 |
|
427 | 427 |
public: |
428 | 428 |
|
429 | 429 |
/// \brief Default constructor |
430 | 430 |
/// |
431 | 431 |
/// Default constructor |
432 | 432 |
ListPath() : first(0), last(0) {} |
433 | 433 |
|
434 | 434 |
/// \brief Template copy constructor |
435 | 435 |
/// |
436 | 436 |
/// This path can be initialized with any other path type. It just |
437 | 437 |
/// makes a copy of the given path. |
438 | 438 |
template <typename CPath> |
439 | 439 |
ListPath(const CPath& cpath) : first(0), last(0) { |
440 | 440 |
copyPath(*this, cpath); |
441 | 441 |
} |
442 | 442 |
|
443 | 443 |
/// \brief Destructor of the path |
444 | 444 |
/// |
445 | 445 |
/// Destructor of the path |
446 | 446 |
~ListPath() { |
447 | 447 |
clear(); |
448 | 448 |
} |
449 | 449 |
|
450 | 450 |
/// \brief Template copy assignment |
451 | 451 |
/// |
452 | 452 |
/// This path can be initialized with any other path type. It just |
453 | 453 |
/// makes a copy of the given path. |
454 | 454 |
template <typename CPath> |
455 | 455 |
ListPath& operator=(const CPath& cpath) { |
456 | 456 |
copyPath(*this, cpath); |
457 | 457 |
return *this; |
458 | 458 |
} |
459 | 459 |
|
460 | 460 |
/// \brief Iterator class to iterate on the arcs of the paths |
461 | 461 |
/// |
462 | 462 |
/// This class is used to iterate on the arcs of the paths |
463 | 463 |
/// |
464 | 464 |
/// Of course it converts to Digraph::Arc |
465 | 465 |
class ArcIt { |
466 | 466 |
friend class ListPath; |
467 | 467 |
public: |
468 | 468 |
/// Default constructor |
469 | 469 |
ArcIt() {} |
470 | 470 |
/// Invalid constructor |
471 | 471 |
ArcIt(Invalid) : path(0), node(0) {} |
472 | 472 |
/// \brief Initializate the constructor to the first arc of path |
473 | 473 |
ArcIt(const ListPath &_path) |
474 | 474 |
: path(&_path), node(_path.first) {} |
475 | 475 |
|
476 | 476 |
protected: |
477 | 477 |
|
478 | 478 |
ArcIt(const ListPath &_path, Node *_node) |
479 | 479 |
: path(&_path), node(_node) {} |
480 | 480 |
|
481 | 481 |
|
482 | 482 |
public: |
483 | 483 |
|
484 | 484 |
///Conversion to Digraph::Arc |
485 | 485 |
operator const Arc&() const { |
486 | 486 |
return node->arc; |
487 | 487 |
} |
488 | 488 |
|
489 | 489 |
/// Next arc |
490 | 490 |
ArcIt& operator++() { |
491 | 491 |
node = node->next; |
492 | 492 |
return *this; |
493 | 493 |
} |
494 | 494 |
|
495 | 495 |
/// Comparison operator |
496 | 496 |
bool operator==(const ArcIt& e) const { return node==e.node; } |
497 | 497 |
/// Comparison operator |
498 | 498 |
bool operator!=(const ArcIt& e) const { return node!=e.node; } |
499 | 499 |
/// Comparison operator |
500 | 500 |
bool operator<(const ArcIt& e) const { return node<e.node; } |
501 | 501 |
|
502 | 502 |
private: |
503 | 503 |
const ListPath *path; |
504 | 504 |
Node *node; |
505 | 505 |
}; |
506 | 506 |
|
507 | 507 |
/// \brief The nth arc. |
508 | 508 |
/// |
509 | 509 |
/// This function looks for the nth arc in O(n) time. |
510 | 510 |
/// \pre n is in the [0..length() - 1] range |
511 | 511 |
const Arc& nth(int n) const { |
512 | 512 |
Node *node = first; |
513 | 513 |
for (int i = 0; i < n; ++i) { |
514 | 514 |
node = node->next; |
515 | 515 |
} |
516 | 516 |
return node->arc; |
517 | 517 |
} |
518 | 518 |
|
519 | 519 |
/// \brief Initializes arc iterator to point to the nth arc. |
520 | 520 |
ArcIt nthIt(int n) const { |
521 | 521 |
Node *node = first; |
522 | 522 |
for (int i = 0; i < n; ++i) { |
523 | 523 |
node = node->next; |
524 | 524 |
} |
525 | 525 |
return ArcIt(*this, node); |
526 | 526 |
} |
527 | 527 |
|
528 | 528 |
/// \brief Length of the path. |
529 | 529 |
int length() const { |
530 | 530 |
int len = 0; |
531 | 531 |
Node *node = first; |
532 | 532 |
while (node != 0) { |
533 | 533 |
node = node->next; |
534 | 534 |
++len; |
535 | 535 |
} |
536 | 536 |
return len; |
537 | 537 |
} |
538 | 538 |
|
539 | 539 |
/// \brief Return true if the path is empty. |
540 | 540 |
bool empty() const { return first == 0; } |
541 | 541 |
|
542 | 542 |
/// \brief Reset the path to an empty one. |
543 | 543 |
void clear() { |
544 | 544 |
while (first != 0) { |
545 | 545 |
last = first->next; |
546 | 546 |
alloc.destroy(first); |
547 | 547 |
alloc.deallocate(first, 1); |
548 | 548 |
first = last; |
549 | 549 |
} |
550 | 550 |
} |
551 | 551 |
|
552 | 552 |
/// \brief The first arc of the path |
553 | 553 |
const Arc& front() const { |
554 | 554 |
return first->arc; |
555 | 555 |
} |
556 | 556 |
|
557 | 557 |
/// \brief Add a new arc before the current path |
558 | 558 |
void addFront(const Arc& arc) { |
559 | 559 |
Node *node = alloc.allocate(1); |
560 | 560 |
alloc.construct(node, Node()); |
561 | 561 |
node->prev = 0; |
562 | 562 |
node->next = first; |
563 | 563 |
node->arc = arc; |
564 | 564 |
if (first) { |
565 | 565 |
first->prev = node; |
566 | 566 |
first = node; |
567 | 567 |
} else { |
568 | 568 |
first = last = node; |
569 | 569 |
} |
570 | 570 |
} |
571 | 571 |
|
572 | 572 |
/// \brief Erase the first arc of the path |
573 | 573 |
void eraseFront() { |
574 | 574 |
Node *node = first; |
575 | 575 |
first = first->next; |
576 | 576 |
if (first) { |
577 | 577 |
first->prev = 0; |
578 | 578 |
} else { |
579 | 579 |
last = 0; |
580 | 580 |
} |
581 | 581 |
alloc.destroy(node); |
582 | 582 |
alloc.deallocate(node, 1); |
583 | 583 |
} |
584 | 584 |
|
585 | 585 |
/// \brief The last arc of the path. |
586 | 586 |
const Arc& back() const { |
587 | 587 |
return last->arc; |
588 | 588 |
} |
589 | 589 |
|
590 | 590 |
/// \brief Add a new arc behind the current path. |
591 | 591 |
void addBack(const Arc& arc) { |
592 | 592 |
Node *node = alloc.allocate(1); |
593 | 593 |
alloc.construct(node, Node()); |
594 | 594 |
node->next = 0; |
595 | 595 |
node->prev = last; |
596 | 596 |
node->arc = arc; |
597 | 597 |
if (last) { |
598 | 598 |
last->next = node; |
599 | 599 |
last = node; |
600 | 600 |
} else { |
601 | 601 |
last = first = node; |
602 | 602 |
} |
603 | 603 |
} |
604 | 604 |
|
605 | 605 |
/// \brief Erase the last arc of the path |
606 | 606 |
void eraseBack() { |
607 | 607 |
Node *node = last; |
608 | 608 |
last = last->prev; |
609 | 609 |
if (last) { |
610 | 610 |
last->next = 0; |
611 | 611 |
} else { |
612 | 612 |
first = 0; |
613 | 613 |
} |
614 | 614 |
alloc.destroy(node); |
615 | 615 |
alloc.deallocate(node, 1); |
616 | 616 |
} |
617 | 617 |
|
618 | 618 |
/// \brief Splice a path to the back of the current path. |
619 | 619 |
/// |
620 | 620 |
/// It splices \c tpath to the back of the current path and \c |
621 | 621 |
/// tpath becomes empty. The time complexity of this function is |
622 | 622 |
/// O(1). |
623 | 623 |
void spliceBack(ListPath& tpath) { |
624 | 624 |
if (first) { |
625 | 625 |
if (tpath.first) { |
626 | 626 |
last->next = tpath.first; |
627 | 627 |
tpath.first->prev = last; |
628 | 628 |
last = tpath.last; |
629 | 629 |
} |
630 | 630 |
} else { |
631 | 631 |
first = tpath.first; |
632 | 632 |
last = tpath.last; |
633 | 633 |
} |
634 | 634 |
tpath.first = tpath.last = 0; |
635 | 635 |
} |
636 | 636 |
|
637 | 637 |
/// \brief Splice a path to the front of the current path. |
638 | 638 |
/// |
639 | 639 |
/// It splices \c tpath before the current path and \c tpath |
640 | 640 |
/// becomes empty. The time complexity of this function |
641 | 641 |
/// is O(1). |
642 | 642 |
void spliceFront(ListPath& tpath) { |
643 | 643 |
if (first) { |
644 | 644 |
if (tpath.first) { |
645 | 645 |
first->prev = tpath.last; |
646 | 646 |
tpath.last->next = first; |
647 | 647 |
first = tpath.first; |
648 | 648 |
} |
649 | 649 |
} else { |
650 | 650 |
first = tpath.first; |
651 | 651 |
last = tpath.last; |
652 | 652 |
} |
653 | 653 |
tpath.first = tpath.last = 0; |
654 | 654 |
} |
655 | 655 |
|
656 | 656 |
/// \brief Splice a path into the current path. |
657 | 657 |
/// |
658 | 658 |
/// It splices the \c tpath into the current path before the |
659 | 659 |
/// position of \c it iterator and \c tpath becomes empty. The |
660 | 660 |
/// time complexity of this function is O(1). If the \c it is |
661 | 661 |
/// \c INVALID then it will splice behind the current path. |
662 | 662 |
void splice(ArcIt it, ListPath& tpath) { |
663 | 663 |
if (it.node) { |
664 | 664 |
if (tpath.first) { |
665 | 665 |
tpath.first->prev = it.node->prev; |
666 | 666 |
if (it.node->prev) { |
667 | 667 |
it.node->prev->next = tpath.first; |
668 | 668 |
} else { |
669 | 669 |
first = tpath.first; |
670 | 670 |
} |
671 | 671 |
it.node->prev = tpath.last; |
672 | 672 |
tpath.last->next = it.node; |
673 | 673 |
} |
674 | 674 |
} else { |
675 | 675 |
if (first) { |
676 | 676 |
if (tpath.first) { |
677 | 677 |
last->next = tpath.first; |
678 | 678 |
tpath.first->prev = last; |
679 | 679 |
last = tpath.last; |
680 | 680 |
} |
681 | 681 |
} else { |
682 | 682 |
first = tpath.first; |
683 | 683 |
last = tpath.last; |
684 | 684 |
} |
685 | 685 |
} |
686 | 686 |
tpath.first = tpath.last = 0; |
687 | 687 |
} |
688 | 688 |
|
689 | 689 |
/// \brief Split the current path. |
690 | 690 |
/// |
691 | 691 |
/// It splits the current path into two parts. The part before |
692 | 692 |
/// the iterator \c it will remain in the current path and the part |
693 | 693 |
/// starting with |
694 | 694 |
/// \c it will put into \c tpath. If \c tpath have arcs |
695 | 695 |
/// before the operation they are removed first. The time |
696 | 696 |
/// complexity of this function is O(1) plus the the time of emtying |
697 | 697 |
/// \c tpath. If \c it is \c INVALID then it just clears \c tpath |
698 | 698 |
void split(ArcIt it, ListPath& tpath) { |
699 | 699 |
tpath.clear(); |
700 | 700 |
if (it.node) { |
701 | 701 |
tpath.first = it.node; |
702 | 702 |
tpath.last = last; |
703 | 703 |
if (it.node->prev) { |
704 | 704 |
last = it.node->prev; |
705 | 705 |
last->next = 0; |
706 | 706 |
} else { |
707 | 707 |
first = last = 0; |
708 | 708 |
} |
709 | 709 |
it.node->prev = 0; |
710 | 710 |
} |
711 | 711 |
} |
712 | 712 |
|
713 | 713 |
|
714 | 714 |
typedef True BuildTag; |
715 | 715 |
|
716 | 716 |
template <typename CPath> |
717 | 717 |
void build(const CPath& path) { |
718 | 718 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) { |
719 | 719 |
addBack(it); |
720 | 720 |
} |
721 | 721 |
} |
722 | 722 |
|
723 | 723 |
template <typename CPath> |
724 | 724 |
void buildRev(const CPath& path) { |
725 | 725 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) { |
726 | 726 |
addFront(it); |
727 | 727 |
} |
728 | 728 |
} |
729 | 729 |
|
730 | 730 |
}; |
731 | 731 |
|
732 | 732 |
/// \brief A structure for representing directed paths in a digraph. |
733 | 733 |
/// |
734 | 734 |
/// A structure for representing directed path in a digraph. |
735 |
/// \ |
|
735 |
/// \tparam _Digraph The digraph type in which the path is. |
|
736 | 736 |
/// |
737 | 737 |
/// In a sense, the path can be treated as a list of arcs. The |
738 | 738 |
/// lemon path type stores just this list. As a consequence it |
739 | 739 |
/// cannot enumerate the nodes in the path and the source node of |
740 | 740 |
/// a zero length path is undefined. |
741 | 741 |
/// |
742 | 742 |
/// This implementation is completly static, i.e. it can be copy constucted |
743 | 743 |
/// or copy assigned from another path, but otherwise it cannot be |
744 | 744 |
/// modified. |
745 | 745 |
/// |
746 | 746 |
/// Being the the most memory efficient path type in LEMON, |
747 | 747 |
/// it is intented to be |
748 | 748 |
/// used when you want to store a large number of paths. |
749 | 749 |
template <typename _Digraph> |
750 | 750 |
class StaticPath { |
751 | 751 |
public: |
752 | 752 |
|
753 | 753 |
typedef _Digraph Digraph; |
754 | 754 |
typedef typename Digraph::Arc Arc; |
755 | 755 |
|
756 | 756 |
/// \brief Default constructor |
757 | 757 |
/// |
758 | 758 |
/// Default constructor |
759 | 759 |
StaticPath() : len(0), arcs(0) {} |
760 | 760 |
|
761 | 761 |
/// \brief Template copy constructor |
762 | 762 |
/// |
763 | 763 |
/// This path can be initialized from any other path type. |
764 | 764 |
template <typename CPath> |
765 | 765 |
StaticPath(const CPath& cpath) : arcs(0) { |
766 | 766 |
copyPath(*this, cpath); |
767 | 767 |
} |
768 | 768 |
|
769 | 769 |
/// \brief Destructor of the path |
770 | 770 |
/// |
771 | 771 |
/// Destructor of the path |
772 | 772 |
~StaticPath() { |
773 | 773 |
if (arcs) delete[] arcs; |
774 | 774 |
} |
775 | 775 |
|
776 | 776 |
/// \brief Template copy assignment |
777 | 777 |
/// |
778 | 778 |
/// This path can be made equal to any other path type. It simply |
779 | 779 |
/// makes a copy of the given path. |
780 | 780 |
template <typename CPath> |
781 | 781 |
StaticPath& operator=(const CPath& cpath) { |
782 | 782 |
copyPath(*this, cpath); |
783 | 783 |
return *this; |
784 | 784 |
} |
785 | 785 |
|
786 | 786 |
/// \brief Iterator class to iterate on the arcs of the paths |
787 | 787 |
/// |
788 | 788 |
/// This class is used to iterate on the arcs of the paths |
789 | 789 |
/// |
790 | 790 |
/// Of course it converts to Digraph::Arc |
791 | 791 |
class ArcIt { |
792 | 792 |
friend class StaticPath; |
793 | 793 |
public: |
794 | 794 |
/// Default constructor |
795 | 795 |
ArcIt() {} |
796 | 796 |
/// Invalid constructor |
797 | 797 |
ArcIt(Invalid) : path(0), idx(-1) {} |
798 | 798 |
/// Initializate the constructor to the first arc of path |
799 | 799 |
ArcIt(const StaticPath &_path) |
800 | 800 |
: path(&_path), idx(_path.empty() ? -1 : 0) {} |
801 | 801 |
|
802 | 802 |
private: |
803 | 803 |
|
804 | 804 |
/// Constructor with starting point |
805 | 805 |
ArcIt(const StaticPath &_path, int _idx) |
806 | 806 |
: idx(_idx), path(&_path) {} |
807 | 807 |
|
808 | 808 |
public: |
809 | 809 |
|
810 | 810 |
///Conversion to Digraph::Arc |
811 | 811 |
operator const Arc&() const { |
812 | 812 |
return path->nth(idx); |
813 | 813 |
} |
814 | 814 |
|
815 | 815 |
/// Next arc |
816 | 816 |
ArcIt& operator++() { |
817 | 817 |
++idx; |
818 | 818 |
if (idx >= path->length()) idx = -1; |
819 | 819 |
return *this; |
820 | 820 |
} |
821 | 821 |
|
822 | 822 |
/// Comparison operator |
823 | 823 |
bool operator==(const ArcIt& e) const { return idx==e.idx; } |
824 | 824 |
/// Comparison operator |
825 | 825 |
bool operator!=(const ArcIt& e) const { return idx!=e.idx; } |
826 | 826 |
/// Comparison operator |
827 | 827 |
bool operator<(const ArcIt& e) const { return idx<e.idx; } |
828 | 828 |
|
829 | 829 |
private: |
830 | 830 |
const StaticPath *path; |
831 | 831 |
int idx; |
832 | 832 |
}; |
833 | 833 |
|
834 | 834 |
/// \brief The nth arc. |
835 | 835 |
/// |
836 | 836 |
/// \pre n is in the [0..length() - 1] range |
837 | 837 |
const Arc& nth(int n) const { |
838 | 838 |
return arcs[n]; |
839 | 839 |
} |
840 | 840 |
|
841 | 841 |
/// \brief The arc iterator pointing to the nth arc. |
842 | 842 |
ArcIt nthIt(int n) const { |
843 | 843 |
return ArcIt(*this, n); |
844 | 844 |
} |
845 | 845 |
|
846 | 846 |
/// \brief The length of the path. |
847 | 847 |
int length() const { return len; } |
848 | 848 |
|
849 | 849 |
/// \brief Return true when the path is empty. |
850 | 850 |
int empty() const { return len == 0; } |
851 | 851 |
|
852 | 852 |
/// \break Erase all arcs in the digraph. |
853 | 853 |
void clear() { |
854 | 854 |
len = 0; |
855 | 855 |
if (arcs) delete[] arcs; |
856 | 856 |
arcs = 0; |
857 | 857 |
} |
858 | 858 |
|
859 | 859 |
/// \brief The first arc of the path. |
860 | 860 |
const Arc& front() const { |
861 | 861 |
return arcs[0]; |
862 | 862 |
} |
863 | 863 |
|
864 | 864 |
/// \brief The last arc of the path. |
865 | 865 |
const Arc& back() const { |
866 | 866 |
return arcs[len - 1]; |
867 | 867 |
} |
868 | 868 |
|
869 | 869 |
|
870 | 870 |
typedef True BuildTag; |
871 | 871 |
|
872 | 872 |
template <typename CPath> |
873 | 873 |
void build(const CPath& path) { |
874 | 874 |
len = path.length(); |
875 | 875 |
arcs = new Arc[len]; |
876 | 876 |
int index = 0; |
877 | 877 |
for (typename CPath::ArcIt it(path); it != INVALID; ++it) { |
878 | 878 |
arcs[index] = it; |
879 | 879 |
++index; |
880 | 880 |
} |
881 | 881 |
} |
882 | 882 |
|
883 | 883 |
template <typename CPath> |
884 | 884 |
void buildRev(const CPath& path) { |
885 | 885 |
len = path.length(); |
886 | 886 |
arcs = new Arc[len]; |
887 | 887 |
int index = len; |
888 | 888 |
for (typename CPath::RevArcIt it(path); it != INVALID; ++it) { |
889 | 889 |
--index; |
890 | 890 |
arcs[index] = it; |
891 | 891 |
} |
892 | 892 |
} |
893 | 893 |
|
894 | 894 |
private: |
895 | 895 |
int len; |
896 | 896 |
Arc* arcs; |
897 | 897 |
}; |
898 | 898 |
|
899 | 899 |
/////////////////////////////////////////////////////////////////////// |
900 | 900 |
// Additional utilities |
901 | 901 |
/////////////////////////////////////////////////////////////////////// |
902 | 902 |
|
903 | 903 |
namespace _path_bits { |
904 | 904 |
|
905 | 905 |
template <typename Path, typename Enable = void> |
906 | 906 |
struct RevPathTagIndicator { |
907 | 907 |
static const bool value = false; |
908 | 908 |
}; |
909 | 909 |
|
910 | 910 |
template <typename Path> |
911 | 911 |
struct RevPathTagIndicator< |
912 | 912 |
Path, |
913 | 913 |
typename enable_if<typename Path::RevPathTag, void>::type |
914 | 914 |
> { |
915 | 915 |
static const bool value = true; |
916 | 916 |
}; |
917 | 917 |
|
918 | 918 |
template <typename Path, typename Enable = void> |
919 | 919 |
struct BuildTagIndicator { |
920 | 920 |
static const bool value = false; |
921 | 921 |
}; |
922 | 922 |
|
923 | 923 |
template <typename Path> |
924 | 924 |
struct BuildTagIndicator< |
925 | 925 |
Path, |
926 | 926 |
typename enable_if<typename Path::BuildTag, void>::type |
927 | 927 |
> { |
928 | 928 |
static const bool value = true; |
929 | 929 |
}; |
930 | 930 |
|
931 | 931 |
template <typename Target, typename Source, |
932 | 932 |
bool buildEnable = BuildTagIndicator<Target>::value, |
933 | 933 |
bool revEnable = RevPathTagIndicator<Source>::value> |
934 | 934 |
struct PathCopySelector { |
935 | 935 |
static void copy(Target& target, const Source& source) { |
936 | 936 |
target.clear(); |
937 | 937 |
for (typename Source::ArcIt it(source); it != INVALID; ++it) { |
938 | 938 |
target.addBack(it); |
939 | 939 |
} |
940 | 940 |
} |
941 | 941 |
}; |
942 | 942 |
|
943 | 943 |
template <typename Target, typename Source> |
944 | 944 |
struct PathCopySelector<Target, Source, false, true> { |
945 | 945 |
static void copy(Target& target, const Source& source) { |
946 | 946 |
target.clear(); |
947 | 947 |
for (typename Source::RevArcIt it(source); it != INVALID; ++it) { |
948 | 948 |
target.addFront(it); |
949 | 949 |
} |
950 | 950 |
} |
951 | 951 |
}; |
952 | 952 |
|
953 | 953 |
template <typename Target, typename Source> |
954 | 954 |
struct PathCopySelector<Target, Source, true, false> { |
955 | 955 |
static void copy(Target& target, const Source& source) { |
956 | 956 |
target.clear(); |
957 | 957 |
target.build(source); |
958 | 958 |
} |
959 | 959 |
}; |
960 | 960 |
|
961 | 961 |
template <typename Target, typename Source> |
962 | 962 |
struct PathCopySelector<Target, Source, true, true> { |
963 | 963 |
static void copy(Target& target, const Source& source) { |
964 | 964 |
target.clear(); |
965 | 965 |
target.buildRev(source); |
966 | 966 |
} |
967 | 967 |
}; |
968 | 968 |
|
969 | 969 |
} |
970 | 970 |
|
971 | 971 |
|
972 | 972 |
/// \brief Make a copy of a path. |
973 | 973 |
/// |
974 | 974 |
/// This function makes a copy of a path. |
975 | 975 |
template <typename Target, typename Source> |
976 | 976 |
void copyPath(Target& target, const Source& source) { |
977 | 977 |
checkConcept<concepts::PathDumper<typename Source::Digraph>, Source>(); |
978 | 978 |
_path_bits::PathCopySelector<Target, Source>::copy(target, source); |
979 | 979 |
} |
980 | 980 |
|
981 | 981 |
/// \brief Check the consistency of a path. |
982 | 982 |
/// |
983 | 983 |
/// This function checks that the target of each arc is the same |
984 | 984 |
/// as the source of the next one. |
985 | 985 |
/// |
986 | 986 |
template <typename Digraph, typename Path> |
987 | 987 |
bool checkPath(const Digraph& digraph, const Path& path) { |
988 | 988 |
typename Path::ArcIt it(path); |
989 | 989 |
if (it == INVALID) return true; |
990 | 990 |
typename Digraph::Node node = digraph.target(it); |
991 | 991 |
++it; |
992 | 992 |
while (it != INVALID) { |
993 | 993 |
if (digraph.source(it) != node) return false; |
994 | 994 |
node = digraph.target(it); |
995 | 995 |
++it; |
996 | 996 |
} |
997 | 997 |
return true; |
998 | 998 |
} |
999 | 999 |
|
1000 | 1000 |
/// \brief The source of a path |
1001 | 1001 |
/// |
1002 | 1002 |
/// This function returns the source of the given path. |
1003 | 1003 |
template <typename Digraph, typename Path> |
1004 | 1004 |
typename Digraph::Node pathSource(const Digraph& digraph, const Path& path) { |
1005 | 1005 |
return digraph.source(path.front()); |
1006 | 1006 |
} |
1007 | 1007 |
|
1008 | 1008 |
/// \brief The target of a path |
1009 | 1009 |
/// |
1010 | 1010 |
/// This function returns the target of the given path. |
1011 | 1011 |
template <typename Digraph, typename Path> |
1012 | 1012 |
typename Digraph::Node pathTarget(const Digraph& digraph, const Path& path) { |
1013 | 1013 |
return digraph.target(path.back()); |
1014 | 1014 |
} |
1015 | 1015 |
|
1016 | 1016 |
/// \brief Class which helps to iterate through the nodes of a path |
1017 | 1017 |
/// |
1018 | 1018 |
/// In a sense, the path can be treated as a list of arcs. The |
1019 | 1019 |
/// lemon path type stores only this list. As a consequence, it |
1020 | 1020 |
/// cannot enumerate the nodes in the path and the zero length paths |
1021 | 1021 |
/// cannot have a source node. |
1022 | 1022 |
/// |
1023 | 1023 |
/// This class implements the node iterator of a path structure. To |
1024 | 1024 |
/// provide this feature, the underlying digraph should be passed to |
1025 | 1025 |
/// the constructor of the iterator. |
1026 | 1026 |
template <typename Path> |
1027 | 1027 |
class PathNodeIt { |
1028 | 1028 |
private: |
1029 | 1029 |
const typename Path::Digraph *_digraph; |
1030 | 1030 |
typename Path::ArcIt _it; |
1031 | 1031 |
typename Path::Digraph::Node _nd; |
1032 | 1032 |
|
1033 | 1033 |
public: |
1034 | 1034 |
|
1035 | 1035 |
typedef typename Path::Digraph Digraph; |
1036 | 1036 |
typedef typename Digraph::Node Node; |
1037 | 1037 |
|
1038 | 1038 |
/// Default constructor |
1039 | 1039 |
PathNodeIt() {} |
1040 | 1040 |
/// Invalid constructor |
1041 | 1041 |
PathNodeIt(Invalid) |
1042 | 1042 |
: _digraph(0), _it(INVALID), _nd(INVALID) {} |
1043 | 1043 |
/// Constructor |
1044 | 1044 |
PathNodeIt(const Digraph& digraph, const Path& path) |
1045 | 1045 |
: _digraph(&digraph), _it(path) { |
1046 | 1046 |
_nd = (_it != INVALID ? _digraph->source(_it) : INVALID); |
1047 | 1047 |
} |
1048 | 1048 |
/// Constructor |
1049 | 1049 |
PathNodeIt(const Digraph& digraph, const Path& path, const Node& src) |
1050 | 1050 |
: _digraph(&digraph), _it(path), _nd(src) {} |
1051 | 1051 |
|
1052 | 1052 |
///Conversion to Digraph::Node |
1053 | 1053 |
operator Node() const { |
1054 | 1054 |
return _nd; |
1055 | 1055 |
} |
1056 | 1056 |
|
1057 | 1057 |
/// Next node |
1058 | 1058 |
PathNodeIt& operator++() { |
1059 | 1059 |
if (_it == INVALID) _nd = INVALID; |
1060 | 1060 |
else { |
1061 | 1061 |
_nd = _digraph->target(_it); |
1062 | 1062 |
++_it; |
1063 | 1063 |
} |
1064 | 1064 |
return *this; |
1065 | 1065 |
} |
1066 | 1066 |
|
1067 | 1067 |
/// Comparison operator |
1068 | 1068 |
bool operator==(const PathNodeIt& n) const { |
1069 | 1069 |
return _it == n._it && _nd == n._nd; |
1070 | 1070 |
} |
1071 | 1071 |
/// Comparison operator |
1072 | 1072 |
bool operator!=(const PathNodeIt& n) const { |
1073 | 1073 |
return _it != n._it || _nd != n._nd; |
1074 | 1074 |
} |
1075 | 1075 |
/// Comparison operator |
1076 | 1076 |
bool operator<(const PathNodeIt& n) const { |
1077 | 1077 |
return (_it < n._it && _nd != INVALID); |
1078 | 1078 |
} |
1079 | 1079 |
|
1080 | 1080 |
}; |
1081 | 1081 |
|
1082 | 1082 |
///@} |
1083 | 1083 |
|
1084 | 1084 |
} // namespace lemon |
1085 | 1085 |
|
1086 | 1086 |
#endif // LEMON_PATH_H |
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_SMART_GRAPH_H |
20 | 20 |
#define LEMON_SMART_GRAPH_H |
21 | 21 |
|
22 | 22 |
///\ingroup graphs |
23 | 23 |
///\file |
24 | 24 |
///\brief SmartDigraph and SmartGraph classes. |
25 | 25 |
|
26 | 26 |
#include <vector> |
27 | 27 |
|
28 | 28 |
#include <lemon/bits/invalid.h> |
29 | 29 |
|
30 | 30 |
#include <lemon/bits/base_extender.h> |
31 | 31 |
#include <lemon/bits/graph_extender.h> |
32 | 32 |
|
33 | 33 |
#include <lemon/bits/utility.h> |
34 | 34 |
#include <lemon/error.h> |
35 | 35 |
|
36 | 36 |
#include <lemon/bits/graph_extender.h> |
37 | 37 |
|
38 | 38 |
namespace lemon { |
39 | 39 |
|
40 | 40 |
class SmartDigraph; |
41 | 41 |
///Base of SmartDigraph |
42 | 42 |
|
43 | 43 |
///Base of SmartDigraph |
44 | 44 |
/// |
45 | 45 |
class SmartDigraphBase { |
46 | 46 |
protected: |
47 | 47 |
|
48 | 48 |
struct NodeT |
49 | 49 |
{ |
50 | 50 |
int first_in, first_out; |
51 | 51 |
NodeT() {} |
52 | 52 |
}; |
53 | 53 |
struct ArcT |
54 | 54 |
{ |
55 | 55 |
int target, source, next_in, next_out; |
56 | 56 |
ArcT() {} |
57 | 57 |
}; |
58 | 58 |
|
59 | 59 |
std::vector<NodeT> nodes; |
60 | 60 |
std::vector<ArcT> arcs; |
61 | 61 |
|
62 | 62 |
public: |
63 | 63 |
|
64 | 64 |
typedef SmartDigraphBase Graph; |
65 | 65 |
|
66 | 66 |
class Node; |
67 | 67 |
class Arc; |
68 | 68 |
|
69 | 69 |
public: |
70 | 70 |
|
71 | 71 |
SmartDigraphBase() : nodes(), arcs() { } |
72 | 72 |
SmartDigraphBase(const SmartDigraphBase &_g) |
73 | 73 |
: nodes(_g.nodes), arcs(_g.arcs) { } |
74 | 74 |
|
75 | 75 |
typedef True NodeNumTag; |
76 | 76 |
typedef True EdgeNumTag; |
77 | 77 |
|
78 | 78 |
int nodeNum() const { return nodes.size(); } |
79 | 79 |
int arcNum() const { return arcs.size(); } |
80 | 80 |
|
81 | 81 |
int maxNodeId() const { return nodes.size()-1; } |
82 | 82 |
int maxArcId() const { return arcs.size()-1; } |
83 | 83 |
|
84 | 84 |
Node addNode() { |
85 | 85 |
int n = nodes.size(); |
86 | 86 |
nodes.push_back(NodeT()); |
87 | 87 |
nodes[n].first_in = -1; |
88 | 88 |
nodes[n].first_out = -1; |
89 | 89 |
return Node(n); |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
Arc addArc(Node u, Node v) { |
93 | 93 |
int n = arcs.size(); |
94 | 94 |
arcs.push_back(ArcT()); |
95 | 95 |
arcs[n].source = u._id; |
96 | 96 |
arcs[n].target = v._id; |
97 | 97 |
arcs[n].next_out = nodes[u._id].first_out; |
98 | 98 |
arcs[n].next_in = nodes[v._id].first_in; |
99 | 99 |
nodes[u._id].first_out = nodes[v._id].first_in = n; |
100 | 100 |
|
101 | 101 |
return Arc(n); |
102 | 102 |
} |
103 | 103 |
|
104 | 104 |
void clear() { |
105 | 105 |
arcs.clear(); |
106 | 106 |
nodes.clear(); |
107 | 107 |
} |
108 | 108 |
|
109 | 109 |
Node source(Arc a) const { return Node(arcs[a._id].source); } |
110 | 110 |
Node target(Arc a) const { return Node(arcs[a._id].target); } |
111 | 111 |
|
112 | 112 |
static int id(Node v) { return v._id; } |
113 | 113 |
static int id(Arc a) { return a._id; } |
114 | 114 |
|
115 | 115 |
static Node nodeFromId(int id) { return Node(id);} |
116 | 116 |
static Arc arcFromId(int id) { return Arc(id);} |
117 | 117 |
|
118 | 118 |
bool valid(Node n) const { |
119 | 119 |
return n._id >= 0 && n._id < static_cast<int>(nodes.size()); |
120 | 120 |
} |
121 | 121 |
bool valid(Arc a) const { |
122 | 122 |
return a._id >= 0 && a._id < static_cast<int>(arcs.size()); |
123 | 123 |
} |
124 | 124 |
|
125 | 125 |
class Node { |
126 | 126 |
friend class SmartDigraphBase; |
127 | 127 |
friend class SmartDigraph; |
128 | 128 |
|
129 | 129 |
protected: |
130 | 130 |
int _id; |
131 | 131 |
explicit Node(int id) : _id(id) {} |
132 | 132 |
public: |
133 | 133 |
Node() {} |
134 | 134 |
Node (Invalid) : _id(-1) {} |
135 | 135 |
bool operator==(const Node i) const {return _id == i._id;} |
136 | 136 |
bool operator!=(const Node i) const {return _id != i._id;} |
137 | 137 |
bool operator<(const Node i) const {return _id < i._id;} |
138 | 138 |
}; |
139 | 139 |
|
140 | 140 |
|
141 | 141 |
class Arc { |
142 | 142 |
friend class SmartDigraphBase; |
143 | 143 |
friend class SmartDigraph; |
144 | 144 |
|
145 | 145 |
protected: |
146 | 146 |
int _id; |
147 | 147 |
explicit Arc(int id) : _id(id) {} |
148 | 148 |
public: |
149 | 149 |
Arc() { } |
150 | 150 |
Arc (Invalid) : _id(-1) {} |
151 | 151 |
bool operator==(const Arc i) const {return _id == i._id;} |
152 | 152 |
bool operator!=(const Arc i) const {return _id != i._id;} |
153 | 153 |
bool operator<(const Arc i) const {return _id < i._id;} |
154 | 154 |
}; |
155 | 155 |
|
156 | 156 |
void first(Node& node) const { |
157 | 157 |
node._id = nodes.size() - 1; |
158 | 158 |
} |
159 | 159 |
|
160 | 160 |
static void next(Node& node) { |
161 | 161 |
--node._id; |
162 | 162 |
} |
163 | 163 |
|
164 | 164 |
void first(Arc& arc) const { |
165 | 165 |
arc._id = arcs.size() - 1; |
166 | 166 |
} |
167 | 167 |
|
168 | 168 |
static void next(Arc& arc) { |
169 | 169 |
--arc._id; |
170 | 170 |
} |
171 | 171 |
|
172 | 172 |
void firstOut(Arc& arc, const Node& node) const { |
173 | 173 |
arc._id = nodes[node._id].first_out; |
174 | 174 |
} |
175 | 175 |
|
176 | 176 |
void nextOut(Arc& arc) const { |
177 | 177 |
arc._id = arcs[arc._id].next_out; |
178 | 178 |
} |
179 | 179 |
|
180 | 180 |
void firstIn(Arc& arc, const Node& node) const { |
181 | 181 |
arc._id = nodes[node._id].first_in; |
182 | 182 |
} |
183 | 183 |
|
184 | 184 |
void nextIn(Arc& arc) const { |
185 | 185 |
arc._id = arcs[arc._id].next_in; |
186 | 186 |
} |
187 | 187 |
|
188 | 188 |
}; |
189 | 189 |
|
190 | 190 |
typedef DigraphExtender<SmartDigraphBase> ExtendedSmartDigraphBase; |
191 | 191 |
|
192 | 192 |
///\ingroup graphs |
193 | 193 |
/// |
194 | 194 |
///\brief A smart directed graph class. |
195 | 195 |
/// |
196 | 196 |
///This is a simple and fast digraph implementation. |
197 | 197 |
///It is also quite memory efficient, but at the price |
198 | 198 |
///that <b> it does support only limited (only stack-like) |
199 | 199 |
///node and arc deletions</b>. |
200 | 200 |
///It conforms to the \ref concepts::Digraph "Digraph concept" with |
201 | 201 |
///an important extra feature that its maps are real \ref |
202 | 202 |
///concepts::ReferenceMap "reference map"s. |
203 | 203 |
/// |
204 | 204 |
///\sa concepts::Digraph. |
205 |
/// |
|
206 |
///\author Alpar Juttner |
|
207 | 205 |
class SmartDigraph : public ExtendedSmartDigraphBase { |
208 | 206 |
public: |
209 | 207 |
|
210 | 208 |
typedef ExtendedSmartDigraphBase Parent; |
211 | 209 |
|
212 | 210 |
private: |
213 | 211 |
|
214 | 212 |
///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead. |
215 | 213 |
|
216 | 214 |
///SmartDigraph is \e not copy constructible. Use DigraphCopy() instead. |
217 | 215 |
/// |
218 | 216 |
SmartDigraph(const SmartDigraph &) : ExtendedSmartDigraphBase() {}; |
219 | 217 |
///\brief Assignment of SmartDigraph to another one is \e not allowed. |
220 | 218 |
///Use DigraphCopy() instead. |
221 | 219 |
|
222 | 220 |
///Assignment of SmartDigraph to another one is \e not allowed. |
223 | 221 |
///Use DigraphCopy() instead. |
224 | 222 |
void operator=(const SmartDigraph &) {} |
225 | 223 |
|
226 | 224 |
public: |
227 | 225 |
|
228 | 226 |
/// Constructor |
229 | 227 |
|
230 | 228 |
/// Constructor. |
231 | 229 |
/// |
232 | 230 |
SmartDigraph() {}; |
233 | 231 |
|
234 | 232 |
///Add a new node to the digraph. |
235 | 233 |
|
236 | 234 |
/// \return the new node. |
237 | 235 |
/// |
238 | 236 |
Node addNode() { return Parent::addNode(); } |
239 | 237 |
|
240 | 238 |
///Add a new arc to the digraph. |
241 | 239 |
|
242 | 240 |
///Add a new arc to the digraph with source node \c s |
243 | 241 |
///and target node \c t. |
244 | 242 |
///\return the new arc. |
245 | 243 |
Arc addArc(const Node& s, const Node& t) { |
246 | 244 |
return Parent::addArc(s, t); |
247 | 245 |
} |
248 | 246 |
|
249 | 247 |
/// \brief Using this it is possible to avoid the superfluous memory |
250 | 248 |
/// allocation. |
251 | 249 |
|
252 | 250 |
/// Using this it is possible to avoid the superfluous memory |
253 | 251 |
/// allocation: if you know that the digraph you want to build will |
254 | 252 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
255 | 253 |
/// then it is worth reserving space for this amount before starting |
256 | 254 |
/// to build the digraph. |
257 | 255 |
/// \sa reserveArc |
258 | 256 |
void reserveNode(int n) { nodes.reserve(n); }; |
259 | 257 |
|
260 | 258 |
/// \brief Using this it is possible to avoid the superfluous memory |
261 | 259 |
/// allocation. |
262 | 260 |
|
263 | 261 |
/// Using this it is possible to avoid the superfluous memory |
264 | 262 |
/// allocation: if you know that the digraph you want to build will |
265 | 263 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
266 | 264 |
/// then it is worth reserving space for this amount before starting |
267 | 265 |
/// to build the digraph. |
268 | 266 |
/// \sa reserveNode |
269 | 267 |
void reserveArc(int m) { arcs.reserve(m); }; |
270 | 268 |
|
271 | 269 |
/// \brief Node validity check |
272 | 270 |
/// |
273 | 271 |
/// This function gives back true if the given node is valid, |
274 | 272 |
/// ie. it is a real node of the graph. |
275 | 273 |
/// |
276 | 274 |
/// \warning A removed node (using Snapshot) could become valid again |
277 | 275 |
/// when new nodes are added to the graph. |
278 | 276 |
bool valid(Node n) const { return Parent::valid(n); } |
279 | 277 |
|
280 | 278 |
/// \brief Arc validity check |
281 | 279 |
/// |
282 | 280 |
/// This function gives back true if the given arc is valid, |
283 | 281 |
/// ie. it is a real arc of the graph. |
284 | 282 |
/// |
285 | 283 |
/// \warning A removed arc (using Snapshot) could become valid again |
286 | 284 |
/// when new arcs are added to the graph. |
287 | 285 |
bool valid(Arc a) const { return Parent::valid(a); } |
288 | 286 |
|
289 | 287 |
///Clear the digraph. |
290 | 288 |
|
291 | 289 |
///Erase all the nodes and arcs from the digraph. |
292 | 290 |
/// |
293 | 291 |
void clear() { |
294 | 292 |
Parent::clear(); |
295 | 293 |
} |
296 | 294 |
|
297 | 295 |
///Split a node. |
298 | 296 |
|
299 | 297 |
///This function splits a node. First a new node is added to the digraph, |
300 | 298 |
///then the source of each outgoing arc of \c n is moved to this new node. |
301 | 299 |
///If \c connect is \c true (this is the default value), then a new arc |
302 | 300 |
///from \c n to the newly created node is also added. |
303 | 301 |
///\return The newly created node. |
304 | 302 |
/// |
305 | 303 |
///\note The <tt>Arc</tt>s |
306 | 304 |
///referencing a moved arc remain |
307 | 305 |
///valid. However <tt>InArc</tt>'s and <tt>OutArc</tt>'s |
308 | 306 |
///may be invalidated. |
309 | 307 |
///\warning This functionality cannot be used together with the Snapshot |
310 | 308 |
///feature. |
311 | 309 |
///\todo It could be implemented in a bit faster way. |
312 | 310 |
Node split(Node n, bool connect = true) |
313 | 311 |
{ |
314 | 312 |
Node b = addNode(); |
315 | 313 |
nodes[b._id].first_out=nodes[n._id].first_out; |
316 | 314 |
nodes[n._id].first_out=-1; |
317 | 315 |
for(int i=nodes[b._id].first_out;i!=-1;i++) arcs[i].source=b._id; |
318 | 316 |
if(connect) addArc(n,b); |
319 | 317 |
return b; |
320 | 318 |
} |
321 | 319 |
|
322 | 320 |
public: |
323 | 321 |
|
324 | 322 |
class Snapshot; |
325 | 323 |
|
326 | 324 |
protected: |
327 | 325 |
|
328 | 326 |
void restoreSnapshot(const Snapshot &s) |
329 | 327 |
{ |
330 | 328 |
while(s.arc_num<arcs.size()) { |
331 | 329 |
Arc arc = arcFromId(arcs.size()-1); |
332 | 330 |
Parent::notifier(Arc()).erase(arc); |
333 | 331 |
nodes[arcs.back().source].first_out=arcs.back().next_out; |
334 | 332 |
nodes[arcs.back().target].first_in=arcs.back().next_in; |
335 | 333 |
arcs.pop_back(); |
336 | 334 |
} |
337 | 335 |
while(s.node_num<nodes.size()) { |
338 | 336 |
Node node = nodeFromId(nodes.size()-1); |
339 | 337 |
Parent::notifier(Node()).erase(node); |
340 | 338 |
nodes.pop_back(); |
341 | 339 |
} |
342 | 340 |
} |
343 | 341 |
|
344 | 342 |
public: |
345 | 343 |
|
346 | 344 |
///Class to make a snapshot of the digraph and to restrore to it later. |
347 | 345 |
|
348 | 346 |
///Class to make a snapshot of the digraph and to restrore to it later. |
349 | 347 |
/// |
350 | 348 |
///The newly added nodes and arcs can be removed using the |
351 | 349 |
///restore() function. |
352 | 350 |
///\note After you restore a state, you cannot restore |
353 | 351 |
///a later state, in other word you cannot add again the arcs deleted |
354 | 352 |
///by restore() using another one Snapshot instance. |
355 | 353 |
/// |
356 | 354 |
///\warning If you do not use correctly the snapshot that can cause |
357 | 355 |
///either broken program, invalid state of the digraph, valid but |
358 | 356 |
///not the restored digraph or no change. Because the runtime performance |
359 | 357 |
///the validity of the snapshot is not stored. |
360 | 358 |
class Snapshot |
361 | 359 |
{ |
362 | 360 |
SmartDigraph *_graph; |
363 | 361 |
protected: |
364 | 362 |
friend class SmartDigraph; |
365 | 363 |
unsigned int node_num; |
366 | 364 |
unsigned int arc_num; |
367 | 365 |
public: |
368 | 366 |
///Default constructor. |
369 | 367 |
|
370 | 368 |
///Default constructor. |
371 | 369 |
///To actually make a snapshot you must call save(). |
372 | 370 |
/// |
373 | 371 |
Snapshot() : _graph(0) {} |
374 | 372 |
///Constructor that immediately makes a snapshot |
375 | 373 |
|
376 | 374 |
///This constructor immediately makes a snapshot of the digraph. |
377 | 375 |
///\param _g The digraph we make a snapshot of. |
378 | 376 |
Snapshot(SmartDigraph &graph) : _graph(&graph) { |
379 | 377 |
node_num=_graph->nodes.size(); |
380 | 378 |
arc_num=_graph->arcs.size(); |
381 | 379 |
} |
382 | 380 |
|
383 | 381 |
///Make a snapshot. |
384 | 382 |
|
385 | 383 |
///Make a snapshot of the digraph. |
386 | 384 |
/// |
387 | 385 |
///This function can be called more than once. In case of a repeated |
388 | 386 |
///call, the previous snapshot gets lost. |
389 | 387 |
///\param _g The digraph we make the snapshot of. |
390 | 388 |
void save(SmartDigraph &graph) |
391 | 389 |
{ |
392 | 390 |
_graph=&graph; |
393 | 391 |
node_num=_graph->nodes.size(); |
394 | 392 |
arc_num=_graph->arcs.size(); |
395 | 393 |
} |
396 | 394 |
|
397 | 395 |
///Undo the changes until a snapshot. |
398 | 396 |
|
399 | 397 |
///Undo the changes until a snapshot created by save(). |
400 | 398 |
/// |
401 | 399 |
///\note After you restored a state, you cannot restore |
402 | 400 |
///a later state, in other word you cannot add again the arcs deleted |
403 | 401 |
///by restore(). |
404 | 402 |
void restore() |
405 | 403 |
{ |
406 | 404 |
_graph->restoreSnapshot(*this); |
407 | 405 |
} |
408 | 406 |
}; |
409 | 407 |
}; |
410 | 408 |
|
411 | 409 |
|
412 | 410 |
class SmartGraphBase { |
413 | 411 |
|
414 | 412 |
protected: |
415 | 413 |
|
416 | 414 |
struct NodeT { |
417 | 415 |
int first_out; |
418 | 416 |
}; |
419 | 417 |
|
420 | 418 |
struct ArcT { |
421 | 419 |
int target; |
422 | 420 |
int next_out; |
423 | 421 |
}; |
424 | 422 |
|
425 | 423 |
std::vector<NodeT> nodes; |
426 | 424 |
std::vector<ArcT> arcs; |
427 | 425 |
|
428 | 426 |
int first_free_arc; |
429 | 427 |
|
430 | 428 |
public: |
431 | 429 |
|
432 | 430 |
typedef SmartGraphBase Digraph; |
433 | 431 |
|
434 | 432 |
class Node; |
435 | 433 |
class Arc; |
436 | 434 |
class Edge; |
437 | 435 |
|
438 | 436 |
class Node { |
439 | 437 |
friend class SmartGraphBase; |
440 | 438 |
protected: |
441 | 439 |
|
442 | 440 |
int _id; |
443 | 441 |
explicit Node(int id) { _id = id;} |
444 | 442 |
|
445 | 443 |
public: |
446 | 444 |
Node() {} |
447 | 445 |
Node (Invalid) { _id = -1; } |
448 | 446 |
bool operator==(const Node& node) const {return _id == node._id;} |
449 | 447 |
bool operator!=(const Node& node) const {return _id != node._id;} |
450 | 448 |
bool operator<(const Node& node) const {return _id < node._id;} |
451 | 449 |
}; |
452 | 450 |
|
453 | 451 |
class Edge { |
454 | 452 |
friend class SmartGraphBase; |
455 | 453 |
protected: |
456 | 454 |
|
457 | 455 |
int _id; |
458 | 456 |
explicit Edge(int id) { _id = id;} |
459 | 457 |
|
460 | 458 |
public: |
461 | 459 |
Edge() {} |
462 | 460 |
Edge (Invalid) { _id = -1; } |
463 | 461 |
bool operator==(const Edge& arc) const {return _id == arc._id;} |
464 | 462 |
bool operator!=(const Edge& arc) const {return _id != arc._id;} |
465 | 463 |
bool operator<(const Edge& arc) const {return _id < arc._id;} |
466 | 464 |
}; |
467 | 465 |
|
468 | 466 |
class Arc { |
469 | 467 |
friend class SmartGraphBase; |
470 | 468 |
protected: |
471 | 469 |
|
472 | 470 |
int _id; |
473 | 471 |
explicit Arc(int id) { _id = id;} |
474 | 472 |
|
475 | 473 |
public: |
476 | 474 |
operator Edge() const { return edgeFromId(_id / 2); } |
477 | 475 |
|
478 | 476 |
Arc() {} |
479 | 477 |
Arc (Invalid) { _id = -1; } |
480 | 478 |
bool operator==(const Arc& arc) const {return _id == arc._id;} |
481 | 479 |
bool operator!=(const Arc& arc) const {return _id != arc._id;} |
482 | 480 |
bool operator<(const Arc& arc) const {return _id < arc._id;} |
483 | 481 |
}; |
484 | 482 |
|
485 | 483 |
|
486 | 484 |
|
487 | 485 |
SmartGraphBase() |
488 | 486 |
: nodes(), arcs() {} |
489 | 487 |
|
490 | 488 |
|
491 | 489 |
int maxNodeId() const { return nodes.size()-1; } |
492 | 490 |
int maxEdgeId() const { return arcs.size() / 2 - 1; } |
493 | 491 |
int maxArcId() const { return arcs.size()-1; } |
494 | 492 |
|
495 | 493 |
Node source(Arc e) const { return Node(arcs[e._id ^ 1].target); } |
496 | 494 |
Node target(Arc e) const { return Node(arcs[e._id].target); } |
497 | 495 |
|
498 | 496 |
Node u(Edge e) const { return Node(arcs[2 * e._id].target); } |
499 | 497 |
Node v(Edge e) const { return Node(arcs[2 * e._id + 1].target); } |
500 | 498 |
|
501 | 499 |
static bool direction(Arc e) { |
502 | 500 |
return (e._id & 1) == 1; |
503 | 501 |
} |
504 | 502 |
|
505 | 503 |
static Arc direct(Edge e, bool d) { |
506 | 504 |
return Arc(e._id * 2 + (d ? 1 : 0)); |
507 | 505 |
} |
508 | 506 |
|
509 | 507 |
void first(Node& node) const { |
510 | 508 |
node._id = nodes.size() - 1; |
511 | 509 |
} |
512 | 510 |
|
513 | 511 |
void next(Node& node) const { |
514 | 512 |
--node._id; |
515 | 513 |
} |
516 | 514 |
|
517 | 515 |
void first(Arc& arc) const { |
518 | 516 |
arc._id = arcs.size() - 1; |
519 | 517 |
} |
520 | 518 |
|
521 | 519 |
void next(Arc& arc) const { |
522 | 520 |
--arc._id; |
523 | 521 |
} |
524 | 522 |
|
525 | 523 |
void first(Edge& arc) const { |
526 | 524 |
arc._id = arcs.size() / 2 - 1; |
527 | 525 |
} |
528 | 526 |
|
529 | 527 |
void next(Edge& arc) const { |
530 | 528 |
--arc._id; |
531 | 529 |
} |
532 | 530 |
|
533 | 531 |
void firstOut(Arc &arc, const Node& v) const { |
534 | 532 |
arc._id = nodes[v._id].first_out; |
535 | 533 |
} |
536 | 534 |
void nextOut(Arc &arc) const { |
537 | 535 |
arc._id = arcs[arc._id].next_out; |
538 | 536 |
} |
539 | 537 |
|
540 | 538 |
void firstIn(Arc &arc, const Node& v) const { |
541 | 539 |
arc._id = ((nodes[v._id].first_out) ^ 1); |
542 | 540 |
if (arc._id == -2) arc._id = -1; |
543 | 541 |
} |
544 | 542 |
void nextIn(Arc &arc) const { |
545 | 543 |
arc._id = ((arcs[arc._id ^ 1].next_out) ^ 1); |
546 | 544 |
if (arc._id == -2) arc._id = -1; |
547 | 545 |
} |
548 | 546 |
|
549 | 547 |
void firstInc(Edge &arc, bool& d, const Node& v) const { |
550 | 548 |
int de = nodes[v._id].first_out; |
551 | 549 |
if (de != -1) { |
552 | 550 |
arc._id = de / 2; |
553 | 551 |
d = ((de & 1) == 1); |
554 | 552 |
} else { |
555 | 553 |
arc._id = -1; |
556 | 554 |
d = true; |
557 | 555 |
} |
558 | 556 |
} |
559 | 557 |
void nextInc(Edge &arc, bool& d) const { |
560 | 558 |
int de = (arcs[(arc._id * 2) | (d ? 1 : 0)].next_out); |
561 | 559 |
if (de != -1) { |
562 | 560 |
arc._id = de / 2; |
563 | 561 |
d = ((de & 1) == 1); |
564 | 562 |
} else { |
565 | 563 |
arc._id = -1; |
566 | 564 |
d = true; |
567 | 565 |
} |
568 | 566 |
} |
569 | 567 |
|
570 | 568 |
static int id(Node v) { return v._id; } |
571 | 569 |
static int id(Arc e) { return e._id; } |
572 | 570 |
static int id(Edge e) { return e._id; } |
573 | 571 |
|
574 | 572 |
static Node nodeFromId(int id) { return Node(id);} |
575 | 573 |
static Arc arcFromId(int id) { return Arc(id);} |
576 | 574 |
static Edge edgeFromId(int id) { return Edge(id);} |
577 | 575 |
|
578 | 576 |
bool valid(Node n) const { |
579 | 577 |
return n._id >= 0 && n._id < static_cast<int>(nodes.size()); |
580 | 578 |
} |
581 | 579 |
bool valid(Arc a) const { |
582 | 580 |
return a._id >= 0 && a._id < static_cast<int>(arcs.size()); |
583 | 581 |
} |
584 | 582 |
bool valid(Edge e) const { |
585 | 583 |
return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size()); |
586 | 584 |
} |
587 | 585 |
|
588 | 586 |
Node addNode() { |
589 | 587 |
int n = nodes.size(); |
590 | 588 |
nodes.push_back(NodeT()); |
1 | 1 |
/* -*- C++ -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2008 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_TIME_MEASURE_H |
20 | 20 |
#define LEMON_TIME_MEASURE_H |
21 | 21 |
|
22 | 22 |
///\ingroup timecount |
23 | 23 |
///\file |
24 | 24 |
///\brief Tools for measuring cpu usage |
25 | 25 |
|
26 | 26 |
#ifdef WIN32 |
27 | 27 |
#define WIN32_LEAN_AND_MEAN |
28 | 28 |
#define NOMINMAX |
29 | 29 |
#include <windows.h> |
30 | 30 |
#include <cmath> |
31 | 31 |
#else |
32 | 32 |
#include <sys/times.h> |
33 | 33 |
#include <sys/time.h> |
34 | 34 |
#endif |
35 | 35 |
|
36 | 36 |
#include <string> |
37 | 37 |
#include <fstream> |
38 | 38 |
#include <iostream> |
39 | 39 |
|
40 | 40 |
namespace lemon { |
41 | 41 |
|
42 | 42 |
/// \addtogroup timecount |
43 | 43 |
/// @{ |
44 | 44 |
|
45 | 45 |
/// A class to store (cpu)time instances. |
46 | 46 |
|
47 | 47 |
/// This class stores five time values. |
48 | 48 |
/// - a real time |
49 | 49 |
/// - a user cpu time |
50 | 50 |
/// - a system cpu time |
51 | 51 |
/// - a user cpu time of children |
52 | 52 |
/// - a system cpu time of children |
53 | 53 |
/// |
54 | 54 |
/// TimeStamp's can be added to or substracted from each other and |
55 | 55 |
/// they can be pushed to a stream. |
56 | 56 |
/// |
57 | 57 |
/// In most cases, perhaps the \ref Timer or the \ref TimeReport |
58 | 58 |
/// class is what you want to use instead. |
59 |
/// |
|
60 |
///\author Alpar Juttner |
|
61 | 59 |
|
62 | 60 |
class TimeStamp |
63 | 61 |
{ |
64 | 62 |
double utime; |
65 | 63 |
double stime; |
66 | 64 |
double cutime; |
67 | 65 |
double cstime; |
68 | 66 |
double rtime; |
69 | 67 |
|
70 | 68 |
void _reset() { |
71 | 69 |
utime = stime = cutime = cstime = rtime = 0; |
72 | 70 |
} |
73 | 71 |
|
74 | 72 |
public: |
75 | 73 |
|
76 | 74 |
///Read the current time values of the process |
77 | 75 |
void stamp() |
78 | 76 |
{ |
79 | 77 |
#ifndef WIN32 |
80 | 78 |
timeval tv; |
81 | 79 |
gettimeofday(&tv, 0); |
82 | 80 |
rtime=tv.tv_sec+double(tv.tv_usec)/1e6; |
83 | 81 |
|
84 | 82 |
tms ts; |
85 | 83 |
double tck=sysconf(_SC_CLK_TCK); |
86 | 84 |
times(&ts); |
87 | 85 |
utime=ts.tms_utime/tck; |
88 | 86 |
stime=ts.tms_stime/tck; |
89 | 87 |
cutime=ts.tms_cutime/tck; |
90 | 88 |
cstime=ts.tms_cstime/tck; |
91 | 89 |
#else |
92 | 90 |
static const double ch = 4294967296.0e-7; |
93 | 91 |
static const double cl = 1.0e-7; |
94 | 92 |
|
95 | 93 |
FILETIME system; |
96 | 94 |
GetSystemTimeAsFileTime(&system); |
97 | 95 |
rtime = ch * system.dwHighDateTime + cl * system.dwLowDateTime; |
98 | 96 |
|
99 | 97 |
FILETIME create, exit, kernel, user; |
100 | 98 |
if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) { |
101 | 99 |
utime = ch * user.dwHighDateTime + cl * user.dwLowDateTime; |
102 | 100 |
stime = ch * kernel.dwHighDateTime + cl * kernel.dwLowDateTime; |
103 | 101 |
cutime = 0; |
104 | 102 |
cstime = 0; |
105 | 103 |
} else { |
106 | 104 |
rtime = 0; |
107 | 105 |
utime = 0; |
108 | 106 |
stime = 0; |
109 | 107 |
cutime = 0; |
110 | 108 |
cstime = 0; |
111 | 109 |
} |
112 | 110 |
#endif |
113 | 111 |
} |
114 | 112 |
|
115 | 113 |
/// Constructor initializing with zero |
116 | 114 |
TimeStamp() |
117 | 115 |
{ _reset(); } |
118 | 116 |
///Constructor initializing with the current time values of the process |
119 | 117 |
TimeStamp(void *) { stamp();} |
120 | 118 |
|
121 | 119 |
///Set every time value to zero |
122 | 120 |
TimeStamp &reset() {_reset();return *this;} |
123 | 121 |
|
124 | 122 |
///\e |
125 | 123 |
TimeStamp &operator+=(const TimeStamp &b) |
126 | 124 |
{ |
127 | 125 |
utime+=b.utime; |
128 | 126 |
stime+=b.stime; |
129 | 127 |
cutime+=b.cutime; |
130 | 128 |
cstime+=b.cstime; |
131 | 129 |
rtime+=b.rtime; |
132 | 130 |
return *this; |
133 | 131 |
} |
134 | 132 |
///\e |
135 | 133 |
TimeStamp operator+(const TimeStamp &b) const |
136 | 134 |
{ |
137 | 135 |
TimeStamp t(*this); |
138 | 136 |
return t+=b; |
139 | 137 |
} |
140 | 138 |
///\e |
141 | 139 |
TimeStamp &operator-=(const TimeStamp &b) |
142 | 140 |
{ |
143 | 141 |
utime-=b.utime; |
144 | 142 |
stime-=b.stime; |
145 | 143 |
cutime-=b.cutime; |
146 | 144 |
cstime-=b.cstime; |
147 | 145 |
rtime-=b.rtime; |
148 | 146 |
return *this; |
149 | 147 |
} |
150 | 148 |
///\e |
151 | 149 |
TimeStamp operator-(const TimeStamp &b) const |
152 | 150 |
{ |
153 | 151 |
TimeStamp t(*this); |
154 | 152 |
return t-=b; |
155 | 153 |
} |
156 | 154 |
///\e |
157 | 155 |
TimeStamp &operator*=(double b) |
158 | 156 |
{ |
159 | 157 |
utime*=b; |
160 | 158 |
stime*=b; |
161 | 159 |
cutime*=b; |
162 | 160 |
cstime*=b; |
163 | 161 |
rtime*=b; |
164 | 162 |
return *this; |
165 | 163 |
} |
166 | 164 |
///\e |
167 | 165 |
TimeStamp operator*(double b) const |
168 | 166 |
{ |
169 | 167 |
TimeStamp t(*this); |
170 | 168 |
return t*=b; |
171 | 169 |
} |
172 | 170 |
friend TimeStamp operator*(double b,const TimeStamp &t); |
173 | 171 |
///\e |
174 | 172 |
TimeStamp &operator/=(double b) |
175 | 173 |
{ |
176 | 174 |
utime/=b; |
177 | 175 |
stime/=b; |
178 | 176 |
cutime/=b; |
179 | 177 |
cstime/=b; |
180 | 178 |
rtime/=b; |
181 | 179 |
return *this; |
182 | 180 |
} |
183 | 181 |
///\e |
184 | 182 |
TimeStamp operator/(double b) const |
185 | 183 |
{ |
186 | 184 |
TimeStamp t(*this); |
187 | 185 |
return t/=b; |
188 | 186 |
} |
189 | 187 |
///The time ellapsed since the last call of stamp() |
190 | 188 |
TimeStamp ellapsed() const |
191 | 189 |
{ |
192 | 190 |
TimeStamp t(NULL); |
193 | 191 |
return t-*this; |
194 | 192 |
} |
195 | 193 |
|
196 | 194 |
friend std::ostream& operator<<(std::ostream& os,const TimeStamp &t); |
197 | 195 |
|
198 | 196 |
///Gives back the user time of the process |
199 | 197 |
double userTime() const |
200 | 198 |
{ |
201 | 199 |
return utime; |
202 | 200 |
} |
203 | 201 |
///Gives back the system time of the process |
204 | 202 |
double systemTime() const |
205 | 203 |
{ |
206 | 204 |
return stime; |
207 | 205 |
} |
208 | 206 |
///Gives back the user time of the process' children |
209 | 207 |
|
210 | 208 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
211 | 209 |
/// |
212 | 210 |
double cUserTime() const |
213 | 211 |
{ |
214 | 212 |
return cutime; |
215 | 213 |
} |
216 | 214 |
///Gives back the user time of the process' children |
217 | 215 |
|
218 | 216 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
219 | 217 |
/// |
220 | 218 |
double cSystemTime() const |
221 | 219 |
{ |
222 | 220 |
return cstime; |
223 | 221 |
} |
224 | 222 |
///Gives back the real time |
225 | 223 |
double realTime() const {return rtime;} |
226 | 224 |
}; |
227 | 225 |
|
228 | 226 |
TimeStamp operator*(double b,const TimeStamp &t) |
229 | 227 |
{ |
230 | 228 |
return t*b; |
231 | 229 |
} |
232 | 230 |
|
233 | 231 |
///Prints the time counters |
234 | 232 |
|
235 | 233 |
///Prints the time counters in the following form: |
236 | 234 |
/// |
237 | 235 |
/// <tt>u: XX.XXs s: XX.XXs cu: XX.XXs cs: XX.XXs real: XX.XXs</tt> |
238 | 236 |
/// |
239 | 237 |
/// where the values are the |
240 | 238 |
/// \li \c u: user cpu time, |
241 | 239 |
/// \li \c s: system cpu time, |
242 | 240 |
/// \li \c cu: user cpu time of children, |
243 | 241 |
/// \li \c cs: system cpu time of children, |
244 | 242 |
/// \li \c real: real time. |
245 | 243 |
/// \relates TimeStamp |
246 | 244 |
/// \note On <tt>WIN32</tt> platform the cummulative values are not |
247 | 245 |
/// calculated. |
248 | 246 |
inline std::ostream& operator<<(std::ostream& os,const TimeStamp &t) |
249 | 247 |
{ |
250 | 248 |
os << "u: " << t.userTime() << |
251 | 249 |
"s, s: " << t.systemTime() << |
252 | 250 |
"s, cu: " << t.cUserTime() << |
253 | 251 |
"s, cs: " << t.cSystemTime() << |
254 | 252 |
"s, real: " << t.realTime() << "s"; |
255 | 253 |
return os; |
256 | 254 |
} |
257 | 255 |
|
258 | 256 |
///Class for measuring the cpu time and real time usage of the process |
259 | 257 |
|
260 | 258 |
///Class for measuring the cpu time and real time usage of the process. |
261 | 259 |
///It is quite easy-to-use, here is a short example. |
262 | 260 |
///\code |
263 | 261 |
/// #include<lemon/time_measure.h> |
264 | 262 |
/// #include<iostream> |
265 | 263 |
/// |
266 | 264 |
/// int main() |
267 | 265 |
/// { |
268 | 266 |
/// |
269 | 267 |
/// ... |
270 | 268 |
/// |
271 | 269 |
/// Timer t; |
272 | 270 |
/// doSomething(); |
273 | 271 |
/// std::cout << t << '\n'; |
274 | 272 |
/// t.restart(); |
275 | 273 |
/// doSomethingElse(); |
276 | 274 |
/// std::cout << t << '\n'; |
277 | 275 |
/// |
278 | 276 |
/// ... |
279 | 277 |
/// |
280 | 278 |
/// } |
281 | 279 |
///\endcode |
282 | 280 |
/// |
283 | 281 |
///The \ref Timer can also be \ref stop() "stopped" and |
284 | 282 |
///\ref start() "started" again, so it is possible to compute collected |
285 | 283 |
///running times. |
286 | 284 |
/// |
287 | 285 |
///\warning Depending on the operation system and its actual configuration |
288 | 286 |
///the time counters have a certain (10ms on a typical Linux system) |
289 | 287 |
///granularity. |
290 | 288 |
///Therefore this tool is not appropriate to measure very short times. |
291 | 289 |
///Also, if you start and stop the timer very frequently, it could lead to |
292 | 290 |
///distorted results. |
293 | 291 |
/// |
294 | 292 |
///\note If you want to measure the running time of the execution of a certain |
295 | 293 |
///function, consider the usage of \ref TimeReport instead. |
296 | 294 |
/// |
297 | 295 |
///\todo This shouldn't be Unix (Linux) specific. |
298 | 296 |
///\sa TimeReport |
299 |
/// |
|
300 |
///\author Alpar Juttner |
|
301 | 297 |
class Timer |
302 | 298 |
{ |
303 | 299 |
int _running; //Timer is running iff _running>0; (_running>=0 always holds) |
304 | 300 |
TimeStamp start_time; //This is the relativ start-time if the timer |
305 | 301 |
//is _running, the collected _running time otherwise. |
306 | 302 |
|
307 | 303 |
void _reset() {if(_running) start_time.stamp(); else start_time.reset();} |
308 | 304 |
|
309 | 305 |
public: |
310 | 306 |
///Constructor. |
311 | 307 |
|
312 | 308 |
///\param run indicates whether or not the timer starts immediately. |
313 | 309 |
/// |
314 | 310 |
Timer(bool run=true) :_running(run) {_reset();} |
315 | 311 |
|
316 | 312 |
///\name Control the state of the timer |
317 | 313 |
///Basically a Timer can be either running or stopped, |
318 | 314 |
///but it provides a bit finer control on the execution. |
319 | 315 |
///The \ref Timer also counts the number of \ref start() |
320 | 316 |
///executions, and is stops only after the same amount (or more) |
321 | 317 |
///\ref stop() "stop()"s. This can be useful e.g. to compute the running time |
322 | 318 |
///of recursive functions. |
323 | 319 |
/// |
324 | 320 |
|
325 | 321 |
///@{ |
326 | 322 |
|
327 | 323 |
///Reset and stop the time counters |
328 | 324 |
|
329 | 325 |
///This function resets and stops the time counters |
330 | 326 |
///\sa restart() |
331 | 327 |
void reset() |
332 | 328 |
{ |
333 | 329 |
_running=0; |
334 | 330 |
_reset(); |
335 | 331 |
} |
336 | 332 |
|
337 | 333 |
///Start the time counters |
338 | 334 |
|
339 | 335 |
///This function starts the time counters. |
340 | 336 |
/// |
341 | 337 |
///If the timer is started more than ones, it will remain running |
342 | 338 |
///until the same amount of \ref stop() is called. |
343 | 339 |
///\sa stop() |
344 | 340 |
void start() |
345 | 341 |
{ |
346 | 342 |
if(_running) _running++; |
347 | 343 |
else { |
348 | 344 |
_running=1; |
349 | 345 |
TimeStamp t; |
350 | 346 |
t.stamp(); |
351 | 347 |
start_time=t-start_time; |
352 | 348 |
} |
353 | 349 |
} |
354 | 350 |
|
355 | 351 |
|
356 | 352 |
///Stop the time counters |
357 | 353 |
|
358 | 354 |
///This function stops the time counters. If start() was executed more than |
359 | 355 |
///once, then the same number of stop() execution is necessary the really |
360 | 356 |
///stop the timer. |
361 | 357 |
/// |
362 | 358 |
///\sa halt() |
363 | 359 |
///\sa start() |
364 | 360 |
///\sa restart() |
365 | 361 |
///\sa reset() |
366 | 362 |
|
367 | 363 |
void stop() |
368 | 364 |
{ |
369 | 365 |
if(_running && !--_running) { |
370 | 366 |
TimeStamp t; |
371 | 367 |
t.stamp(); |
372 | 368 |
start_time=t-start_time; |
373 | 369 |
} |
374 | 370 |
} |
375 | 371 |
|
376 | 372 |
///Halt (i.e stop immediately) the time counters |
377 | 373 |
|
378 | 374 |
///This function stops immediately the time counters, i.e. <tt>t.halt()</tt> |
379 | 375 |
///is a faster |
380 | 376 |
///equivalent of the following. |
381 | 377 |
///\code |
382 | 378 |
/// while(t.running()) t.stop() |
383 | 379 |
///\endcode |
384 | 380 |
/// |
385 | 381 |
/// |
386 | 382 |
///\sa stop() |
387 | 383 |
///\sa restart() |
388 | 384 |
///\sa reset() |
389 | 385 |
|
390 | 386 |
void halt() |
391 | 387 |
{ |
392 | 388 |
if(_running) { |
393 | 389 |
_running=0; |
394 | 390 |
TimeStamp t; |
395 | 391 |
t.stamp(); |
396 | 392 |
start_time=t-start_time; |
397 | 393 |
} |
398 | 394 |
} |
399 | 395 |
|
400 | 396 |
///Returns the running state of the timer |
401 | 397 |
|
402 | 398 |
///This function returns the number of stop() exections that is |
403 | 399 |
///necessary to really stop the timer. |
404 | 400 |
///For example the timer |
405 | 401 |
///is running if and only if the return value is \c true |
406 | 402 |
///(i.e. greater than |
407 | 403 |
///zero). |
408 | 404 |
int running() { return _running; } |
409 | 405 |
|
410 | 406 |
|
411 | 407 |
///Restart the time counters |
412 | 408 |
|
413 | 409 |
///This function is a shorthand for |
414 | 410 |
///a reset() and a start() calls. |
415 | 411 |
/// |
416 | 412 |
void restart() |
417 | 413 |
{ |
418 | 414 |
reset(); |
419 | 415 |
start(); |
420 | 416 |
} |
421 | 417 |
|
422 | 418 |
///@} |
423 | 419 |
|
424 | 420 |
///\name Query Functions for the ellapsed time |
425 | 421 |
|
426 | 422 |
///@{ |
427 | 423 |
|
428 | 424 |
///Gives back the ellapsed user time of the process |
429 | 425 |
double userTime() const |
430 | 426 |
{ |
431 | 427 |
return operator TimeStamp().userTime(); |
432 | 428 |
} |
433 | 429 |
///Gives back the ellapsed system time of the process |
434 | 430 |
double systemTime() const |
435 | 431 |
{ |
436 | 432 |
return operator TimeStamp().systemTime(); |
437 | 433 |
} |
438 | 434 |
///Gives back the ellapsed user time of the process' children |
439 | 435 |
|
440 | 436 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
441 | 437 |
/// |
442 | 438 |
double cUserTime() const |
443 | 439 |
{ |
444 | 440 |
return operator TimeStamp().cUserTime(); |
445 | 441 |
} |
446 | 442 |
///Gives back the ellapsed user time of the process' children |
447 | 443 |
|
448 | 444 |
///\note On <tt>WIN32</tt> platform this value is not calculated. |
449 | 445 |
/// |
450 | 446 |
double cSystemTime() const |
451 | 447 |
{ |
452 | 448 |
return operator TimeStamp().cSystemTime(); |
453 | 449 |
} |
454 | 450 |
///Gives back the ellapsed real time |
455 | 451 |
double realTime() const |
456 | 452 |
{ |
457 | 453 |
return operator TimeStamp().realTime(); |
458 | 454 |
} |
459 | 455 |
///Computes the ellapsed time |
460 | 456 |
|
461 | 457 |
///This conversion computes the ellapsed time, therefore you can print |
462 | 458 |
///the ellapsed time like this. |
463 | 459 |
///\code |
464 | 460 |
/// Timer t; |
465 | 461 |
/// doSomething(); |
466 | 462 |
/// std::cout << t << '\n'; |
467 | 463 |
///\endcode |
468 | 464 |
operator TimeStamp () const |
469 | 465 |
{ |
470 | 466 |
TimeStamp t; |
471 | 467 |
t.stamp(); |
472 | 468 |
return _running?t-start_time:start_time; |
473 | 469 |
} |
474 | 470 |
|
475 | 471 |
|
476 | 472 |
///@} |
477 | 473 |
}; |
478 | 474 |
|
479 | 475 |
///Same as \ref Timer but prints a report on destruction. |
480 | 476 |
|
481 | 477 |
///Same as \ref Timer but prints a report on destruction. |
482 | 478 |
///This example shows its usage. |
483 | 479 |
///\code |
484 | 480 |
/// void myAlg(ListGraph &g,int n) |
485 | 481 |
/// { |
486 | 482 |
/// TimeReport tr("Running time of myAlg: "); |
487 | 483 |
/// ... //Here comes the algorithm |
488 | 484 |
/// } |
489 | 485 |
///\endcode |
490 | 486 |
/// |
491 | 487 |
///\sa Timer |
492 | 488 |
///\sa NoTimeReport |
493 | 489 |
///\todo There is no test case for this |
494 | 490 |
class TimeReport : public Timer |
495 | 491 |
{ |
496 | 492 |
std::string _title; |
497 | 493 |
std::ostream &_os; |
498 | 494 |
public: |
499 | 495 |
///\e |
500 | 496 |
|
501 | 497 |
///\param title This text will be printed before the ellapsed time. |
502 | 498 |
///\param os The stream to print the report to. |
503 | 499 |
///\param run Sets whether the timer should start immediately. |
504 | 500 |
|
505 | 501 |
TimeReport(std::string title,std::ostream &os=std::cerr,bool run=true) |
506 | 502 |
: Timer(run), _title(title), _os(os){} |
507 | 503 |
///\e Prints the ellapsed time on destruction. |
508 | 504 |
~TimeReport() |
509 | 505 |
{ |
510 | 506 |
_os << _title << *this << std::endl; |
511 | 507 |
} |
512 | 508 |
}; |
513 | 509 |
|
514 | 510 |
///'Do nothing' version of \ref TimeReport |
515 | 511 |
|
516 | 512 |
///\sa TimeReport |
517 | 513 |
/// |
518 | 514 |
class NoTimeReport |
519 | 515 |
{ |
520 | 516 |
public: |
521 | 517 |
///\e |
522 | 518 |
NoTimeReport(std::string,std::ostream &,bool) {} |
523 | 519 |
///\e |
524 | 520 |
NoTimeReport(std::string,std::ostream &) {} |
525 | 521 |
///\e |
526 | 522 |
NoTimeReport(std::string) {} |
527 | 523 |
///\e Do nothing. |
528 | 524 |
~NoTimeReport() {} |
529 | 525 |
|
530 | 526 |
operator TimeStamp () const { return TimeStamp(); } |
531 | 527 |
void reset() {} |
532 | 528 |
void start() {} |
533 | 529 |
void stop() {} |
534 | 530 |
void halt() {} |
535 | 531 |
int running() { return 0; } |
536 | 532 |
void restart() {} |
537 | 533 |
double userTime() const { return 0; } |
538 | 534 |
double systemTime() const { return 0; } |
539 | 535 |
double cUserTime() const { return 0; } |
540 | 536 |
double cSystemTime() const { return 0; } |
541 | 537 |
double realTime() const { return 0; } |
542 | 538 |
}; |
543 | 539 |
|
544 | 540 |
///Tool to measure the running time more exactly. |
545 | 541 |
|
546 | 542 |
///This function calls \c f several times and returns the average |
547 | 543 |
///running time. The number of the executions will be choosen in such a way |
548 | 544 |
///that the full real running time will be roughly between \c min_time |
549 | 545 |
///and <tt>2*min_time</tt>. |
550 | 546 |
///\param f the function object to be measured. |
551 | 547 |
///\param min_time the minimum total running time. |
552 | 548 |
///\retval num if it is not \c NULL, then the actual |
553 | 549 |
/// number of execution of \c f will be written into <tt>*num</tt>. |
554 | 550 |
///\retval full_time if it is not \c NULL, then the actual |
555 | 551 |
/// total running time will be written into <tt>*full_time</tt>. |
556 | 552 |
///\return The average running time of \c f. |
557 | 553 |
|
558 | 554 |
template<class F> |
559 | 555 |
TimeStamp runningTimeTest(F f,double min_time=10,unsigned int *num = NULL, |
560 | 556 |
TimeStamp *full_time=NULL) |
561 | 557 |
{ |
562 | 558 |
TimeStamp full; |
563 | 559 |
unsigned int total=0; |
564 | 560 |
Timer t; |
565 | 561 |
for(unsigned int tn=1;tn <= 1U<<31 && full.realTime()<=min_time; tn*=2) { |
566 | 562 |
for(;total<tn;total++) f(); |
567 | 563 |
full=t; |
568 | 564 |
} |
569 | 565 |
if(num) *num=total; |
570 | 566 |
if(full_time) *full_time=full; |
571 | 567 |
return full/total; |
572 | 568 |
} |
573 | 569 |
|
574 | 570 |
/// @} |
575 | 571 |
|
576 | 572 |
|
577 | 573 |
} //namespace lemon |
578 | 574 |
|
579 | 575 |
#endif //LEMON_TIME_MEASURE_H |
0 comments (0 inline)