1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-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/bits/path_dump.h> |
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/error.h> |
30 | 30 |
#include <lemon/maps.h> |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
///Default traits class of Bfs class. |
35 | 35 |
|
36 | 36 |
///Default traits class of Bfs class. |
37 | 37 |
///\tparam GR Digraph type. |
38 | 38 |
template<class GR> |
39 | 39 |
struct BfsDefaultTraits |
40 | 40 |
{ |
41 | 41 |
///The type of the digraph the algorithm runs on. |
42 | 42 |
typedef GR Digraph; |
43 | 43 |
|
44 | 44 |
///\brief The type of the map that stores the predecessor |
45 | 45 |
///arcs of the shortest paths. |
46 | 46 |
/// |
47 | 47 |
///The type of the map that stores the predecessor |
48 | 48 |
///arcs of the shortest paths. |
49 | 49 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
50 | 50 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
51 | 51 |
///Instantiates a \ref PredMap. |
52 | 52 |
|
53 | 53 |
///This function instantiates a \ref PredMap. |
54 | 54 |
///\param g is the digraph, to which we would like to define the |
55 | 55 |
///\ref PredMap. |
56 |
///\todo The digraph alone may be insufficient to initialize |
|
57 | 56 |
static PredMap *createPredMap(const Digraph &g) |
58 | 57 |
{ |
59 | 58 |
return new PredMap(g); |
60 | 59 |
} |
61 | 60 |
|
62 | 61 |
///The type of the map that indicates which nodes are processed. |
63 | 62 |
|
64 | 63 |
///The type of the map that indicates which nodes are processed. |
65 | 64 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
66 |
///By default it is a NullMap. |
|
67 | 65 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
68 | 66 |
///Instantiates a \ref ProcessedMap. |
69 | 67 |
|
70 | 68 |
///This function instantiates a \ref ProcessedMap. |
71 | 69 |
///\param g is the digraph, to which |
72 | 70 |
///we would like to define the \ref ProcessedMap |
73 | 71 |
#ifdef DOXYGEN |
74 | 72 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
75 | 73 |
#else |
76 | 74 |
static ProcessedMap *createProcessedMap(const Digraph &) |
77 | 75 |
#endif |
78 | 76 |
{ |
79 | 77 |
return new ProcessedMap(); |
80 | 78 |
} |
81 | 79 |
|
82 | 80 |
///The type of the map that indicates which nodes are reached. |
83 | 81 |
|
84 | 82 |
///The type of the map that indicates which nodes are reached. |
85 | 83 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
86 | 84 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
87 | 85 |
///Instantiates a \ref ReachedMap. |
88 | 86 |
|
89 | 87 |
///This function instantiates a \ref ReachedMap. |
90 | 88 |
///\param g is the digraph, to which |
91 | 89 |
///we would like to define the \ref ReachedMap. |
92 | 90 |
static ReachedMap *createReachedMap(const Digraph &g) |
93 | 91 |
{ |
94 | 92 |
return new ReachedMap(g); |
95 | 93 |
} |
96 | 94 |
|
97 | 95 |
///The type of the map that stores the distances of the nodes. |
98 | 96 |
|
99 | 97 |
///The type of the map that stores the distances of the nodes. |
100 | 98 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
101 | 99 |
typedef typename Digraph::template NodeMap<int> DistMap; |
102 | 100 |
///Instantiates a \ref DistMap. |
103 | 101 |
|
104 | 102 |
///This function instantiates a \ref DistMap. |
105 | 103 |
///\param g is the digraph, to which we would like to define the |
106 | 104 |
///\ref DistMap. |
107 | 105 |
static DistMap *createDistMap(const Digraph &g) |
108 | 106 |
{ |
109 | 107 |
return new DistMap(g); |
110 | 108 |
} |
111 | 109 |
}; |
112 | 110 |
|
113 | 111 |
///%BFS algorithm class. |
114 | 112 |
|
115 | 113 |
///\ingroup search |
116 | 114 |
///This class provides an efficient implementation of the %BFS algorithm. |
117 | 115 |
/// |
118 | 116 |
///There is also a \ref bfs() "function type interface" for the BFS |
119 | 117 |
///algorithm, which is convenient in the simplier cases and it can be |
120 | 118 |
///used easier. |
121 | 119 |
/// |
122 | 120 |
///\tparam GR The type of the digraph the algorithm runs on. |
123 | 121 |
///The default value is \ref ListDigraph. The value of GR is not used |
124 | 122 |
///directly by \ref Bfs, it is only passed to \ref BfsDefaultTraits. |
125 | 123 |
///\tparam TR Traits class to set various data types used by the algorithm. |
126 | 124 |
///The default traits class is |
127 | 125 |
///\ref BfsDefaultTraits "BfsDefaultTraits<GR>". |
128 | 126 |
///See \ref BfsDefaultTraits for the documentation of |
129 | 127 |
///a Bfs traits class. |
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=BfsDefaultTraits<GR> > |
136 | 134 |
#endif |
137 | 135 |
class Bfs { |
138 | 136 |
public: |
139 | 137 |
///\ref Exception for uninitialized parameters. |
140 | 138 |
|
141 | 139 |
///This error represents problems in the initialization of the |
142 | 140 |
///parameters of the algorithm. |
143 | 141 |
class UninitializedParameter : public lemon::UninitializedParameter { |
144 | 142 |
public: |
145 | 143 |
virtual const char* what() const throw() { |
146 | 144 |
return "lemon::Bfs::UninitializedParameter"; |
147 | 145 |
} |
148 | 146 |
}; |
149 | 147 |
|
150 | 148 |
///The type of the digraph the algorithm runs on. |
151 | 149 |
typedef typename TR::Digraph Digraph; |
152 | 150 |
|
153 | 151 |
///\brief The type of the map that stores the predecessor arcs of the |
154 | 152 |
///shortest paths. |
155 | 153 |
typedef typename TR::PredMap PredMap; |
156 | 154 |
///The type of the map that stores the distances of the nodes. |
157 | 155 |
typedef typename TR::DistMap DistMap; |
158 | 156 |
///The type of the map that indicates which nodes are reached. |
159 | 157 |
typedef typename TR::ReachedMap ReachedMap; |
160 | 158 |
///The type of the map that indicates which nodes are processed. |
161 | 159 |
typedef typename TR::ProcessedMap ProcessedMap; |
162 | 160 |
///The type of the paths. |
163 | 161 |
typedef PredMapPath<Digraph, PredMap> Path; |
164 | 162 |
|
165 | 163 |
///The traits class. |
166 | 164 |
typedef TR Traits; |
167 | 165 |
|
168 | 166 |
private: |
169 | 167 |
|
170 | 168 |
typedef typename Digraph::Node Node; |
171 | 169 |
typedef typename Digraph::NodeIt NodeIt; |
172 | 170 |
typedef typename Digraph::Arc Arc; |
173 | 171 |
typedef typename Digraph::OutArcIt OutArcIt; |
174 | 172 |
|
175 | 173 |
//Pointer to the underlying digraph. |
176 | 174 |
const Digraph *G; |
177 | 175 |
//Pointer to the map of predecessor arcs. |
178 | 176 |
PredMap *_pred; |
179 | 177 |
//Indicates if _pred is locally allocated (true) or not. |
180 | 178 |
bool local_pred; |
181 | 179 |
//Pointer to the map of distances. |
182 | 180 |
DistMap *_dist; |
183 | 181 |
//Indicates if _dist is locally allocated (true) or not. |
184 | 182 |
bool local_dist; |
185 | 183 |
//Pointer to the map of reached status of the nodes. |
186 | 184 |
ReachedMap *_reached; |
187 | 185 |
//Indicates if _reached is locally allocated (true) or not. |
188 | 186 |
bool local_reached; |
189 | 187 |
//Pointer to the map of processed status of the nodes. |
190 | 188 |
ProcessedMap *_processed; |
191 | 189 |
//Indicates if _processed is locally allocated (true) or not. |
192 | 190 |
bool local_processed; |
193 | 191 |
|
194 | 192 |
std::vector<typename Digraph::Node> _queue; |
195 | 193 |
int _queue_head,_queue_tail,_queue_next_dist; |
196 | 194 |
int _curr_dist; |
197 | 195 |
|
198 |
///Creates the maps if necessary. |
|
199 |
///\todo Better memory allocation (instead of new). |
|
196 |
//Creates the maps if necessary. |
|
200 | 197 |
void create_maps() |
201 | 198 |
{ |
202 | 199 |
if(!_pred) { |
203 | 200 |
local_pred = true; |
204 | 201 |
_pred = Traits::createPredMap(*G); |
205 | 202 |
} |
206 | 203 |
if(!_dist) { |
207 | 204 |
local_dist = true; |
208 | 205 |
_dist = Traits::createDistMap(*G); |
209 | 206 |
} |
210 | 207 |
if(!_reached) { |
211 | 208 |
local_reached = true; |
212 | 209 |
_reached = Traits::createReachedMap(*G); |
213 | 210 |
} |
214 | 211 |
if(!_processed) { |
215 | 212 |
local_processed = true; |
216 | 213 |
_processed = Traits::createProcessedMap(*G); |
217 | 214 |
} |
218 | 215 |
} |
219 | 216 |
|
220 | 217 |
protected: |
221 | 218 |
|
222 | 219 |
Bfs() {} |
223 | 220 |
|
224 | 221 |
public: |
225 | 222 |
|
226 | 223 |
typedef Bfs Create; |
227 | 224 |
|
228 | 225 |
///\name Named template parameters |
229 | 226 |
|
230 | 227 |
///@{ |
231 | 228 |
|
232 | 229 |
template <class T> |
233 | 230 |
struct SetPredMapTraits : public Traits { |
234 | 231 |
typedef T PredMap; |
235 | 232 |
static PredMap *createPredMap(const Digraph &) |
236 | 233 |
{ |
237 | 234 |
throw UninitializedParameter(); |
238 | 235 |
} |
239 | 236 |
}; |
240 | 237 |
///\brief \ref named-templ-param "Named parameter" for setting |
241 | 238 |
///\ref PredMap type. |
242 | 239 |
/// |
243 | 240 |
///\ref named-templ-param "Named parameter" for setting |
244 | 241 |
///\ref PredMap type. |
245 | 242 |
template <class T> |
246 | 243 |
struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > { |
247 | 244 |
typedef Bfs< Digraph, SetPredMapTraits<T> > Create; |
248 | 245 |
}; |
249 | 246 |
|
250 | 247 |
template <class T> |
251 | 248 |
struct SetDistMapTraits : public Traits { |
252 | 249 |
typedef T DistMap; |
253 | 250 |
static DistMap *createDistMap(const Digraph &) |
254 | 251 |
{ |
255 | 252 |
throw UninitializedParameter(); |
256 | 253 |
} |
257 | 254 |
}; |
258 | 255 |
///\brief \ref named-templ-param "Named parameter" for setting |
259 | 256 |
///\ref DistMap type. |
260 | 257 |
/// |
261 | 258 |
///\ref named-templ-param "Named parameter" for setting |
262 | 259 |
///\ref DistMap type. |
263 | 260 |
template <class T> |
264 | 261 |
struct SetDistMap : public Bfs< Digraph, SetDistMapTraits<T> > { |
265 | 262 |
typedef Bfs< Digraph, SetDistMapTraits<T> > Create; |
266 | 263 |
}; |
267 | 264 |
|
268 | 265 |
template <class T> |
269 | 266 |
struct SetReachedMapTraits : public Traits { |
270 | 267 |
typedef T ReachedMap; |
271 | 268 |
static ReachedMap *createReachedMap(const Digraph &) |
272 | 269 |
{ |
273 | 270 |
throw UninitializedParameter(); |
274 | 271 |
} |
275 | 272 |
}; |
276 | 273 |
///\brief \ref named-templ-param "Named parameter" for setting |
277 | 274 |
///\ref ReachedMap type. |
278 | 275 |
/// |
279 | 276 |
///\ref named-templ-param "Named parameter" for setting |
280 | 277 |
///\ref ReachedMap type. |
281 | 278 |
template <class T> |
282 | 279 |
struct SetReachedMap : public Bfs< Digraph, SetReachedMapTraits<T> > { |
283 | 280 |
typedef Bfs< Digraph, SetReachedMapTraits<T> > Create; |
284 | 281 |
}; |
285 | 282 |
|
286 | 283 |
template <class T> |
287 | 284 |
struct SetProcessedMapTraits : public Traits { |
288 | 285 |
typedef T ProcessedMap; |
289 | 286 |
static ProcessedMap *createProcessedMap(const Digraph &) |
290 | 287 |
{ |
291 | 288 |
throw UninitializedParameter(); |
292 | 289 |
} |
293 | 290 |
}; |
294 | 291 |
///\brief \ref named-templ-param "Named parameter" for setting |
295 | 292 |
///\ref ProcessedMap type. |
296 | 293 |
/// |
297 | 294 |
///\ref named-templ-param "Named parameter" for setting |
298 | 295 |
///\ref ProcessedMap type. |
299 | 296 |
template <class T> |
300 | 297 |
struct SetProcessedMap : public Bfs< Digraph, SetProcessedMapTraits<T> > { |
301 | 298 |
typedef Bfs< Digraph, SetProcessedMapTraits<T> > Create; |
302 | 299 |
}; |
303 | 300 |
|
304 | 301 |
struct SetStandardProcessedMapTraits : public Traits { |
305 | 302 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
306 | 303 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
307 | 304 |
{ |
308 | 305 |
return new ProcessedMap(g); |
309 | 306 |
} |
310 | 307 |
}; |
311 | 308 |
///\brief \ref named-templ-param "Named parameter" for setting |
312 | 309 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
313 | 310 |
/// |
314 | 311 |
///\ref named-templ-param "Named parameter" for setting |
315 | 312 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
316 | 313 |
///If you don't set it explicitly, it will be automatically allocated. |
317 | 314 |
struct SetStandardProcessedMap : |
318 | 315 |
public Bfs< Digraph, SetStandardProcessedMapTraits > { |
319 | 316 |
typedef Bfs< Digraph, SetStandardProcessedMapTraits > Create; |
320 | 317 |
}; |
321 | 318 |
|
322 | 319 |
///@} |
323 | 320 |
|
324 | 321 |
public: |
325 | 322 |
|
326 | 323 |
///Constructor. |
327 | 324 |
|
328 | 325 |
///Constructor. |
329 | 326 |
///\param g The digraph the algorithm runs on. |
330 | 327 |
Bfs(const Digraph &g) : |
331 | 328 |
G(&g), |
332 | 329 |
_pred(NULL), local_pred(false), |
333 | 330 |
_dist(NULL), local_dist(false), |
334 | 331 |
_reached(NULL), local_reached(false), |
335 | 332 |
_processed(NULL), local_processed(false) |
336 | 333 |
{ } |
337 | 334 |
|
338 | 335 |
///Destructor. |
339 | 336 |
~Bfs() |
340 | 337 |
{ |
341 | 338 |
if(local_pred) delete _pred; |
342 | 339 |
if(local_dist) delete _dist; |
343 | 340 |
if(local_reached) delete _reached; |
344 | 341 |
if(local_processed) delete _processed; |
345 | 342 |
} |
346 | 343 |
|
347 | 344 |
///Sets the map that stores the predecessor arcs. |
348 | 345 |
|
349 | 346 |
///Sets the map that stores the predecessor arcs. |
350 | 347 |
///If you don't use this function before calling \ref run(), |
351 | 348 |
///it will allocate one. The destructor deallocates this |
352 | 349 |
///automatically allocated map, of course. |
353 | 350 |
///\return <tt> (*this) </tt> |
354 | 351 |
Bfs &predMap(PredMap &m) |
355 | 352 |
{ |
356 | 353 |
if(local_pred) { |
357 | 354 |
delete _pred; |
358 | 355 |
local_pred=false; |
359 | 356 |
} |
360 | 357 |
_pred = &m; |
361 | 358 |
return *this; |
362 | 359 |
} |
363 | 360 |
|
364 | 361 |
///Sets the map that indicates which nodes are reached. |
365 | 362 |
|
366 | 363 |
///Sets the map that indicates which nodes are reached. |
367 | 364 |
///If you don't use this function before calling \ref run(), |
368 | 365 |
///it will allocate one. The destructor deallocates this |
369 | 366 |
///automatically allocated map, of course. |
370 | 367 |
///\return <tt> (*this) </tt> |
371 | 368 |
Bfs &reachedMap(ReachedMap &m) |
372 | 369 |
{ |
373 | 370 |
if(local_reached) { |
374 | 371 |
delete _reached; |
375 | 372 |
local_reached=false; |
376 | 373 |
} |
377 | 374 |
_reached = &m; |
378 | 375 |
return *this; |
379 | 376 |
} |
380 | 377 |
|
381 | 378 |
///Sets the map that indicates which nodes are processed. |
382 | 379 |
|
383 | 380 |
///Sets the map that indicates which nodes are processed. |
384 | 381 |
///If you don't use this function before calling \ref run(), |
385 | 382 |
///it will allocate one. The destructor deallocates this |
386 | 383 |
///automatically allocated map, of course. |
387 | 384 |
///\return <tt> (*this) </tt> |
388 | 385 |
Bfs &processedMap(ProcessedMap &m) |
389 | 386 |
{ |
390 | 387 |
if(local_processed) { |
391 | 388 |
delete _processed; |
392 | 389 |
local_processed=false; |
393 | 390 |
} |
394 | 391 |
_processed = &m; |
395 | 392 |
return *this; |
396 | 393 |
} |
397 | 394 |
|
398 | 395 |
///Sets the map that stores the distances of the nodes. |
399 | 396 |
|
400 | 397 |
///Sets the map that stores the distances of the nodes calculated by |
401 | 398 |
///the algorithm. |
402 | 399 |
///If you don't use this function before calling \ref run(), |
403 | 400 |
///it will allocate one. The destructor deallocates this |
404 | 401 |
///automatically allocated map, of course. |
405 | 402 |
///\return <tt> (*this) </tt> |
406 | 403 |
Bfs &distMap(DistMap &m) |
407 | 404 |
{ |
408 | 405 |
if(local_dist) { |
409 | 406 |
delete _dist; |
410 | 407 |
local_dist=false; |
411 | 408 |
} |
412 | 409 |
_dist = &m; |
413 | 410 |
return *this; |
414 | 411 |
} |
415 | 412 |
|
416 | 413 |
public: |
417 | 414 |
|
418 | 415 |
///\name Execution control |
419 | 416 |
///The simplest way to execute the algorithm is to use |
420 | 417 |
///one of the member functions called \ref lemon::Bfs::run() "run()". |
421 | 418 |
///\n |
422 | 419 |
///If you need more control on the execution, first you must call |
423 | 420 |
///\ref lemon::Bfs::init() "init()", then you can add several source |
424 | 421 |
///nodes with \ref lemon::Bfs::addSource() "addSource()". |
425 | 422 |
///Finally \ref lemon::Bfs::start() "start()" will perform the |
426 | 423 |
///actual path computation. |
427 | 424 |
|
428 | 425 |
///@{ |
429 | 426 |
|
430 | 427 |
///Initializes the internal data structures. |
431 | 428 |
|
432 | 429 |
///Initializes the internal data structures. |
433 | 430 |
/// |
434 | 431 |
void init() |
435 | 432 |
{ |
436 | 433 |
create_maps(); |
437 | 434 |
_queue.resize(countNodes(*G)); |
438 | 435 |
_queue_head=_queue_tail=0; |
439 | 436 |
_curr_dist=1; |
440 | 437 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
441 | 438 |
_pred->set(u,INVALID); |
442 | 439 |
_reached->set(u,false); |
443 | 440 |
_processed->set(u,false); |
444 | 441 |
} |
445 | 442 |
} |
446 | 443 |
|
447 | 444 |
///Adds a new source node. |
448 | 445 |
|
449 | 446 |
///Adds a new source node to the set of nodes to be processed. |
450 | 447 |
/// |
451 | 448 |
void addSource(Node s) |
452 | 449 |
{ |
453 | 450 |
if(!(*_reached)[s]) |
454 | 451 |
{ |
455 | 452 |
_reached->set(s,true); |
456 | 453 |
_pred->set(s,INVALID); |
457 | 454 |
_dist->set(s,0); |
458 | 455 |
_queue[_queue_head++]=s; |
459 | 456 |
_queue_next_dist=_queue_head; |
460 | 457 |
} |
461 | 458 |
} |
462 | 459 |
|
463 | 460 |
///Processes the next node. |
464 | 461 |
|
465 | 462 |
///Processes the next node. |
466 | 463 |
/// |
467 | 464 |
///\return The processed node. |
468 | 465 |
/// |
469 | 466 |
///\pre The queue must not be empty. |
470 | 467 |
Node processNextNode() |
471 | 468 |
{ |
472 | 469 |
if(_queue_tail==_queue_next_dist) { |
473 | 470 |
_curr_dist++; |
474 | 471 |
_queue_next_dist=_queue_head; |
475 | 472 |
} |
476 | 473 |
Node n=_queue[_queue_tail++]; |
477 | 474 |
_processed->set(n,true); |
478 | 475 |
Node m; |
479 | 476 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
480 | 477 |
if(!(*_reached)[m=G->target(e)]) { |
481 | 478 |
_queue[_queue_head++]=m; |
482 | 479 |
_reached->set(m,true); |
483 | 480 |
_pred->set(m,e); |
484 | 481 |
_dist->set(m,_curr_dist); |
485 | 482 |
} |
486 | 483 |
return n; |
487 | 484 |
} |
488 | 485 |
|
489 | 486 |
///Processes the next node. |
490 | 487 |
|
491 | 488 |
///Processes the next node and checks if the given target node |
492 | 489 |
///is reached. If the target node is reachable from the processed |
493 | 490 |
///node, then the \c reach parameter will be set to \c true. |
494 | 491 |
/// |
495 | 492 |
///\param target The target node. |
496 | 493 |
///\retval reach Indicates if the target node is reached. |
497 | 494 |
///It should be initially \c false. |
498 | 495 |
/// |
499 | 496 |
///\return The processed node. |
500 | 497 |
/// |
501 | 498 |
///\pre The queue must not be empty. |
502 | 499 |
Node processNextNode(Node target, bool& reach) |
503 | 500 |
{ |
504 | 501 |
if(_queue_tail==_queue_next_dist) { |
505 | 502 |
_curr_dist++; |
506 | 503 |
_queue_next_dist=_queue_head; |
507 | 504 |
} |
508 | 505 |
Node n=_queue[_queue_tail++]; |
509 | 506 |
_processed->set(n,true); |
510 | 507 |
Node m; |
511 | 508 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
512 | 509 |
if(!(*_reached)[m=G->target(e)]) { |
513 | 510 |
_queue[_queue_head++]=m; |
514 | 511 |
_reached->set(m,true); |
515 | 512 |
_pred->set(m,e); |
516 | 513 |
_dist->set(m,_curr_dist); |
517 | 514 |
reach = reach || (target == m); |
518 | 515 |
} |
519 | 516 |
return n; |
520 | 517 |
} |
521 | 518 |
|
522 | 519 |
///Processes the next node. |
523 | 520 |
|
524 | 521 |
///Processes the next node and checks if at least one of reached |
525 | 522 |
///nodes has \c true value in the \c nm node map. If one node |
526 | 523 |
///with \c true value is reachable from the processed node, then the |
527 | 524 |
///\c rnode parameter will be set to the first of such nodes. |
528 | 525 |
/// |
529 | 526 |
///\param nm A \c bool (or convertible) node map that indicates the |
530 | 527 |
///possible targets. |
531 | 528 |
///\retval rnode The reached target node. |
532 | 529 |
///It should be initially \c INVALID. |
533 | 530 |
/// |
534 | 531 |
///\return The processed node. |
535 | 532 |
/// |
536 | 533 |
///\pre The queue must not be empty. |
537 | 534 |
template<class NM> |
538 | 535 |
Node processNextNode(const NM& nm, Node& rnode) |
539 | 536 |
{ |
540 | 537 |
if(_queue_tail==_queue_next_dist) { |
541 | 538 |
_curr_dist++; |
542 | 539 |
_queue_next_dist=_queue_head; |
543 | 540 |
} |
544 | 541 |
Node n=_queue[_queue_tail++]; |
545 | 542 |
_processed->set(n,true); |
546 | 543 |
Node m; |
547 | 544 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
548 | 545 |
if(!(*_reached)[m=G->target(e)]) { |
549 | 546 |
_queue[_queue_head++]=m; |
550 | 547 |
_reached->set(m,true); |
551 | 548 |
_pred->set(m,e); |
552 | 549 |
_dist->set(m,_curr_dist); |
553 | 550 |
if (nm[m] && rnode == INVALID) rnode = m; |
554 | 551 |
} |
555 | 552 |
return n; |
556 | 553 |
} |
557 | 554 |
|
558 | 555 |
///The next node to be processed. |
559 | 556 |
|
560 | 557 |
///Returns the next node to be processed or \c INVALID if the queue |
561 | 558 |
///is empty. |
562 | 559 |
Node nextNode() const |
563 | 560 |
{ |
564 | 561 |
return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID; |
565 | 562 |
} |
566 | 563 |
|
567 | 564 |
///\brief Returns \c false if there are nodes |
568 | 565 |
///to be processed. |
569 | 566 |
/// |
570 | 567 |
///Returns \c false if there are nodes |
571 | 568 |
///to be processed in the queue. |
572 | 569 |
bool emptyQueue() const { return _queue_tail==_queue_head; } |
573 | 570 |
|
574 | 571 |
///Returns the number of the nodes to be processed. |
575 | 572 |
|
576 | 573 |
///Returns the number of the nodes to be processed in the queue. |
577 | 574 |
int queueSize() const { return _queue_head-_queue_tail; } |
578 | 575 |
|
579 | 576 |
///Executes the algorithm. |
580 | 577 |
|
581 | 578 |
///Executes the algorithm. |
582 | 579 |
/// |
583 | 580 |
///This method runs the %BFS algorithm from the root node(s) |
584 | 581 |
///in order to compute the shortest path to each node. |
585 | 582 |
/// |
586 | 583 |
///The algorithm computes |
587 | 584 |
///- the shortest path tree (forest), |
588 | 585 |
///- the distance of each node from the root(s). |
589 | 586 |
/// |
590 | 587 |
///\pre init() must be called and at least one root node should be |
591 | 588 |
///added with addSource() before using this function. |
592 | 589 |
/// |
593 | 590 |
///\note <tt>b.start()</tt> is just a shortcut of the following code. |
594 | 591 |
///\code |
595 | 592 |
/// while ( !b.emptyQueue() ) { |
596 | 593 |
/// b.processNextNode(); |
597 | 594 |
/// } |
598 | 595 |
///\endcode |
599 | 596 |
void start() |
600 | 597 |
{ |
601 | 598 |
while ( !emptyQueue() ) processNextNode(); |
602 | 599 |
} |
603 | 600 |
|
604 | 601 |
///Executes the algorithm until the given target node is reached. |
605 | 602 |
|
606 | 603 |
///Executes the algorithm until the given target node is reached. |
607 | 604 |
/// |
608 | 605 |
///This method runs the %BFS algorithm from the root node(s) |
609 | 606 |
///in order to compute the shortest path to \c dest. |
610 | 607 |
/// |
611 | 608 |
///The algorithm computes |
612 | 609 |
///- the shortest path to \c dest, |
613 | 610 |
///- the distance of \c dest from the root(s). |
614 | 611 |
/// |
615 | 612 |
///\pre init() must be called and at least one root node should be |
616 | 613 |
///added with addSource() before using this function. |
617 | 614 |
/// |
618 | 615 |
///\note <tt>b.start(t)</tt> is just a shortcut of the following code. |
619 | 616 |
///\code |
620 | 617 |
/// bool reach = false; |
621 | 618 |
/// while ( !b.emptyQueue() && !reach ) { |
622 | 619 |
/// b.processNextNode(t, reach); |
623 | 620 |
/// } |
624 | 621 |
///\endcode |
625 | 622 |
void start(Node dest) |
626 | 623 |
{ |
627 | 624 |
bool reach = false; |
628 | 625 |
while ( !emptyQueue() && !reach ) processNextNode(dest, reach); |
629 | 626 |
} |
630 | 627 |
|
631 | 628 |
///Executes the algorithm until a condition is met. |
632 | 629 |
|
633 | 630 |
///Executes the algorithm until a condition is met. |
634 | 631 |
/// |
635 | 632 |
///This method runs the %BFS algorithm from the root node(s) in |
636 | 633 |
///order to compute the shortest path to a node \c v with |
637 | 634 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
638 | 635 |
/// |
639 | 636 |
///\param nm A \c bool (or convertible) node map. The algorithm |
640 | 637 |
///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. |
641 | 638 |
/// |
642 | 639 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
643 | 640 |
///\c INVALID if no such node was found. |
644 | 641 |
/// |
645 | 642 |
///\pre init() must be called and at least one root node should be |
646 | 643 |
///added with addSource() before using this function. |
647 | 644 |
/// |
648 | 645 |
///\note <tt>b.start(nm)</tt> is just a shortcut of the following code. |
649 | 646 |
///\code |
650 | 647 |
/// Node rnode = INVALID; |
651 | 648 |
/// while ( !b.emptyQueue() && rnode == INVALID ) { |
652 | 649 |
/// b.processNextNode(nm, rnode); |
653 | 650 |
/// } |
654 | 651 |
/// return rnode; |
655 | 652 |
///\endcode |
656 | 653 |
template<class NodeBoolMap> |
657 | 654 |
Node start(const NodeBoolMap &nm) |
658 | 655 |
{ |
659 | 656 |
Node rnode = INVALID; |
660 | 657 |
while ( !emptyQueue() && rnode == INVALID ) { |
661 | 658 |
processNextNode(nm, rnode); |
662 | 659 |
} |
663 | 660 |
return rnode; |
664 | 661 |
} |
665 | 662 |
|
666 | 663 |
///Runs the algorithm from the given node. |
667 | 664 |
|
668 | 665 |
///This method runs the %BFS algorithm from node \c s |
669 | 666 |
///in order to compute the shortest path to each node. |
670 | 667 |
/// |
671 | 668 |
///The algorithm computes |
672 | 669 |
///- the shortest path tree, |
673 | 670 |
///- the distance of each node from the root. |
674 | 671 |
/// |
675 | 672 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
676 | 673 |
///\code |
677 | 674 |
/// b.init(); |
678 | 675 |
/// b.addSource(s); |
679 | 676 |
/// b.start(); |
680 | 677 |
///\endcode |
681 | 678 |
void run(Node s) { |
682 | 679 |
init(); |
683 | 680 |
addSource(s); |
684 | 681 |
start(); |
685 | 682 |
} |
686 | 683 |
|
687 | 684 |
///Finds the shortest path between \c s and \c t. |
688 | 685 |
|
689 | 686 |
///This method runs the %BFS algorithm from node \c s |
690 | 687 |
///in order to compute the shortest path to \c t. |
691 | 688 |
/// |
692 | 689 |
///\return The length of the shortest <tt>s</tt>--<tt>t</tt> path, |
693 | 690 |
///if \c t is reachable form \c s, \c 0 otherwise. |
694 | 691 |
/// |
695 | 692 |
///\note Apart from the return value, <tt>b.run(s,t)</tt> is just a |
696 | 693 |
///shortcut of the following code. |
697 | 694 |
///\code |
698 | 695 |
/// b.init(); |
699 | 696 |
/// b.addSource(s); |
700 | 697 |
/// b.start(t); |
701 | 698 |
///\endcode |
702 | 699 |
int run(Node s,Node t) { |
703 | 700 |
init(); |
704 | 701 |
addSource(s); |
705 | 702 |
start(t); |
706 | 703 |
return reached(t) ? _curr_dist : 0; |
707 | 704 |
} |
708 | 705 |
|
709 | 706 |
///Runs the algorithm to visit all nodes in the digraph. |
710 | 707 |
|
711 | 708 |
///This method runs the %BFS algorithm in order to |
712 | 709 |
///compute the shortest path to each node. |
713 | 710 |
/// |
714 | 711 |
///The algorithm computes |
715 | 712 |
///- the shortest path tree (forest), |
716 | 713 |
///- the distance of each node from the root(s). |
717 | 714 |
/// |
718 | 715 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
719 | 716 |
///\code |
720 | 717 |
/// b.init(); |
721 | 718 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
722 | 719 |
/// if (!b.reached(n)) { |
723 | 720 |
/// b.addSource(n); |
724 | 721 |
/// b.start(); |
725 | 722 |
/// } |
726 | 723 |
/// } |
727 | 724 |
///\endcode |
728 | 725 |
void run() { |
729 | 726 |
init(); |
730 | 727 |
for (NodeIt n(*G); n != INVALID; ++n) { |
731 | 728 |
if (!reached(n)) { |
732 | 729 |
addSource(n); |
733 | 730 |
start(); |
734 | 731 |
} |
735 | 732 |
} |
736 | 733 |
} |
737 | 734 |
|
738 | 735 |
///@} |
739 | 736 |
|
740 | 737 |
///\name Query Functions |
741 | 738 |
///The result of the %BFS algorithm can be obtained using these |
742 | 739 |
///functions.\n |
743 | 740 |
///Either \ref lemon::Bfs::run() "run()" or \ref lemon::Bfs::start() |
744 | 741 |
///"start()" must be called before using them. |
745 | 742 |
|
746 | 743 |
///@{ |
747 | 744 |
|
748 | 745 |
///The shortest path to a node. |
749 | 746 |
|
750 | 747 |
///Returns the shortest path to a node. |
751 | 748 |
/// |
752 | 749 |
///\warning \c t should be reachable from the root(s). |
753 | 750 |
/// |
754 | 751 |
///\pre Either \ref run() or \ref start() must be called before |
755 | 752 |
///using this function. |
756 | 753 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
757 | 754 |
|
758 | 755 |
///The distance of a node from the root(s). |
759 | 756 |
|
760 | 757 |
///Returns the distance of a node from the root(s). |
761 | 758 |
/// |
762 | 759 |
///\warning If node \c v is not reachable from the root(s), then |
763 | 760 |
///the return value of this function is undefined. |
764 | 761 |
/// |
765 | 762 |
///\pre Either \ref run() or \ref start() must be called before |
766 | 763 |
///using this function. |
767 | 764 |
int dist(Node v) const { return (*_dist)[v]; } |
768 | 765 |
|
769 | 766 |
///Returns the 'previous arc' of the shortest path tree for a node. |
770 | 767 |
|
771 | 768 |
///This function returns the 'previous arc' of the shortest path |
772 | 769 |
///tree for the node \c v, i.e. it returns the last arc of a |
773 | 770 |
///shortest path from the root(s) to \c v. It is \c INVALID if \c v |
774 | 771 |
///is not reachable from the root(s) or if \c v is a root. |
775 | 772 |
/// |
776 | 773 |
///The shortest path tree used here is equal to the shortest path |
777 | 774 |
///tree used in \ref predNode(). |
778 | 775 |
/// |
779 | 776 |
///\pre Either \ref run() or \ref start() must be called before |
780 | 777 |
///using this function. |
781 | 778 |
Arc predArc(Node v) const { return (*_pred)[v];} |
782 | 779 |
|
783 | 780 |
///Returns the 'previous node' of the shortest path tree for a node. |
784 | 781 |
|
785 | 782 |
///This function returns the 'previous node' of the shortest path |
786 | 783 |
///tree for the node \c v, i.e. it returns the last but one node |
787 | 784 |
///from a shortest path from the root(s) to \c v. It is \c INVALID |
788 | 785 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
789 | 786 |
/// |
790 | 787 |
///The shortest path tree used here is equal to the shortest path |
791 | 788 |
///tree used in \ref predArc(). |
792 | 789 |
/// |
793 | 790 |
///\pre Either \ref run() or \ref start() must be called before |
794 | 791 |
///using this function. |
795 | 792 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
796 | 793 |
G->source((*_pred)[v]); } |
797 | 794 |
|
798 | 795 |
///\brief Returns a const reference to the node map that stores the |
799 | 796 |
/// distances of the nodes. |
800 | 797 |
/// |
801 | 798 |
///Returns a const reference to the node map that stores the distances |
802 | 799 |
///of the nodes calculated by the algorithm. |
803 | 800 |
/// |
804 | 801 |
///\pre Either \ref run() or \ref init() |
805 | 802 |
///must be called before using this function. |
806 | 803 |
const DistMap &distMap() const { return *_dist;} |
807 | 804 |
|
808 | 805 |
///\brief Returns a const reference to the node map that stores the |
809 | 806 |
///predecessor arcs. |
810 | 807 |
/// |
811 | 808 |
///Returns a const reference to the node map that stores the predecessor |
812 | 809 |
///arcs, which form the shortest path tree. |
813 | 810 |
/// |
814 | 811 |
///\pre Either \ref run() or \ref init() |
815 | 812 |
///must be called before using this function. |
816 | 813 |
const PredMap &predMap() const { return *_pred;} |
817 | 814 |
|
818 | 815 |
///Checks if a node is reachable from the root(s). |
819 | 816 |
|
820 | 817 |
///Returns \c true if \c v is reachable from the root(s). |
821 | 818 |
///\pre Either \ref run() or \ref start() |
822 | 819 |
///must be called before using this function. |
823 | 820 |
bool reached(Node v) const { return (*_reached)[v]; } |
824 | 821 |
|
825 | 822 |
///@} |
826 | 823 |
}; |
827 | 824 |
|
828 | 825 |
///Default traits class of bfs() function. |
829 | 826 |
|
830 | 827 |
///Default traits class of bfs() function. |
831 | 828 |
///\tparam GR Digraph type. |
832 | 829 |
template<class GR> |
833 | 830 |
struct BfsWizardDefaultTraits |
834 | 831 |
{ |
835 | 832 |
///The type of the digraph the algorithm runs on. |
836 | 833 |
typedef GR Digraph; |
837 | 834 |
|
838 | 835 |
///\brief The type of the map that stores the predecessor |
839 | 836 |
///arcs of the shortest paths. |
840 | 837 |
/// |
841 | 838 |
///The type of the map that stores the predecessor |
842 | 839 |
///arcs of the shortest paths. |
843 | 840 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
844 | 841 |
typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap; |
845 | 842 |
///Instantiates a \ref PredMap. |
846 | 843 |
|
847 | 844 |
///This function instantiates a \ref PredMap. |
848 | 845 |
///\param g is the digraph, to which we would like to define the |
849 | 846 |
///\ref PredMap. |
850 |
///\todo The digraph alone may be insufficient to initialize |
|
851 | 847 |
#ifdef DOXYGEN |
852 | 848 |
static PredMap *createPredMap(const Digraph &g) |
853 | 849 |
#else |
854 | 850 |
static PredMap *createPredMap(const Digraph &) |
855 | 851 |
#endif |
856 | 852 |
{ |
857 | 853 |
return new PredMap(); |
858 | 854 |
} |
859 | 855 |
|
860 | 856 |
///The type of the map that indicates which nodes are processed. |
861 | 857 |
|
862 | 858 |
///The type of the map that indicates which nodes are processed. |
863 | 859 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
864 | 860 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
865 | 861 |
///Instantiates a \ref ProcessedMap. |
866 | 862 |
|
867 | 863 |
///This function instantiates a \ref ProcessedMap. |
868 | 864 |
///\param g is the digraph, to which |
869 | 865 |
///we would like to define the \ref ProcessedMap. |
870 | 866 |
#ifdef DOXYGEN |
871 | 867 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
872 | 868 |
#else |
873 | 869 |
static ProcessedMap *createProcessedMap(const Digraph &) |
874 | 870 |
#endif |
875 | 871 |
{ |
876 | 872 |
return new ProcessedMap(); |
877 | 873 |
} |
878 | 874 |
|
879 | 875 |
///The type of the map that indicates which nodes are reached. |
880 | 876 |
|
881 | 877 |
///The type of the map that indicates which nodes are reached. |
882 | 878 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
883 | 879 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
884 | 880 |
///Instantiates a \ref ReachedMap. |
885 | 881 |
|
886 | 882 |
///This function instantiates a \ref ReachedMap. |
887 | 883 |
///\param g is the digraph, to which |
888 | 884 |
///we would like to define the \ref ReachedMap. |
889 | 885 |
static ReachedMap *createReachedMap(const Digraph &g) |
890 | 886 |
{ |
891 | 887 |
return new ReachedMap(g); |
892 | 888 |
} |
893 | 889 |
|
894 | 890 |
///The type of the map that stores the distances of the nodes. |
895 | 891 |
|
896 | 892 |
///The type of the map that stores the distances of the nodes. |
897 | 893 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
898 | 894 |
/// |
899 | 895 |
typedef NullMap<typename Digraph::Node,int> DistMap; |
900 | 896 |
///Instantiates a \ref DistMap. |
901 | 897 |
|
902 | 898 |
///This function instantiates a \ref DistMap. |
903 | 899 |
///\param g is the digraph, to which we would like to define |
904 | 900 |
///the \ref DistMap |
905 | 901 |
#ifdef DOXYGEN |
906 | 902 |
static DistMap *createDistMap(const Digraph &g) |
907 | 903 |
#else |
908 | 904 |
static DistMap *createDistMap(const Digraph &) |
909 | 905 |
#endif |
910 | 906 |
{ |
911 | 907 |
return new DistMap(); |
912 | 908 |
} |
913 | 909 |
}; |
914 | 910 |
|
915 | 911 |
/// Default traits class used by \ref BfsWizard |
916 | 912 |
|
917 | 913 |
/// To make it easier to use Bfs algorithm |
918 | 914 |
/// we have created a wizard class. |
919 | 915 |
/// This \ref BfsWizard class needs default traits, |
920 | 916 |
/// as well as the \ref Bfs class. |
921 | 917 |
/// The \ref BfsWizardBase is a class to be the default traits of the |
922 | 918 |
/// \ref BfsWizard class. |
923 | 919 |
template<class GR> |
924 | 920 |
class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
925 | 921 |
{ |
926 | 922 |
|
927 | 923 |
typedef BfsWizardDefaultTraits<GR> Base; |
928 | 924 |
protected: |
929 | 925 |
//The type of the nodes in the digraph. |
930 | 926 |
typedef typename Base::Digraph::Node Node; |
931 | 927 |
|
932 | 928 |
//Pointer to the digraph the algorithm runs on. |
933 | 929 |
void *_g; |
934 | 930 |
//Pointer to the map of reached nodes. |
935 | 931 |
void *_reached; |
936 | 932 |
//Pointer to the map of processed nodes. |
937 | 933 |
void *_processed; |
938 | 934 |
//Pointer to the map of predecessors arcs. |
939 | 935 |
void *_pred; |
940 | 936 |
//Pointer to the map of distances. |
941 | 937 |
void *_dist; |
942 | 938 |
//Pointer to the source node. |
943 | 939 |
Node _source; |
944 | 940 |
|
945 | 941 |
public: |
946 | 942 |
/// Constructor. |
947 | 943 |
|
948 | 944 |
/// This constructor does not require parameters, therefore it initiates |
949 | 945 |
/// all of the attributes to default values (0, INVALID). |
950 | 946 |
BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
951 | 947 |
_dist(0), _source(INVALID) {} |
952 | 948 |
|
953 | 949 |
/// Constructor. |
954 | 950 |
|
955 | 951 |
/// This constructor requires some parameters, |
956 | 952 |
/// listed in the parameters list. |
957 | 953 |
/// Others are initiated to 0. |
958 | 954 |
/// \param g The digraph the algorithm runs on. |
959 | 955 |
/// \param s The source node. |
960 | 956 |
BfsWizardBase(const GR &g, Node s=INVALID) : |
961 | 957 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
962 | 958 |
_reached(0), _processed(0), _pred(0), _dist(0), _source(s) {} |
963 | 959 |
|
964 | 960 |
}; |
965 | 961 |
|
966 | 962 |
/// Auxiliary class for the function type interface of BFS algorithm. |
967 | 963 |
|
968 | 964 |
/// This auxiliary class is created to implement the function type |
969 | 965 |
/// interface of \ref Bfs algorithm. It uses the functions and features |
970 | 966 |
/// of the plain \ref Bfs, but it is much simpler to use it. |
971 | 967 |
/// It should only be used through the \ref bfs() function, which makes |
972 | 968 |
/// it easier to use the algorithm. |
973 | 969 |
/// |
974 | 970 |
/// Simplicity means that the way to change the types defined |
975 | 971 |
/// in the traits class is based on functions that returns the new class |
976 | 972 |
/// and not on templatable built-in classes. |
977 | 973 |
/// When using the plain \ref Bfs |
978 | 974 |
/// the new class with the modified type comes from |
979 | 975 |
/// the original class by using the :: |
980 | 976 |
/// operator. In the case of \ref BfsWizard only |
981 | 977 |
/// a function have to be called, and it will |
982 | 978 |
/// return the needed class. |
983 | 979 |
/// |
984 | 980 |
/// It does not have own \ref run() method. When its \ref run() method |
985 | 981 |
/// is called, it initiates a plain \ref Bfs object, and calls the |
986 | 982 |
/// \ref Bfs::run() method of it. |
987 | 983 |
template<class TR> |
988 | 984 |
class BfsWizard : public TR |
989 | 985 |
{ |
990 | 986 |
typedef TR Base; |
991 | 987 |
|
992 | 988 |
///The type of the digraph the algorithm runs on. |
993 | 989 |
typedef typename TR::Digraph Digraph; |
994 | 990 |
|
995 | 991 |
typedef typename Digraph::Node Node; |
996 | 992 |
typedef typename Digraph::NodeIt NodeIt; |
997 | 993 |
typedef typename Digraph::Arc Arc; |
998 | 994 |
typedef typename Digraph::OutArcIt OutArcIt; |
999 | 995 |
|
1000 | 996 |
///\brief The type of the map that stores the predecessor |
1001 | 997 |
///arcs of the shortest paths. |
1002 | 998 |
typedef typename TR::PredMap PredMap; |
1003 | 999 |
///\brief The type of the map that stores the distances of the nodes. |
1004 | 1000 |
typedef typename TR::DistMap DistMap; |
1005 | 1001 |
///\brief The type of the map that indicates which nodes are reached. |
1006 | 1002 |
typedef typename TR::ReachedMap ReachedMap; |
1007 | 1003 |
///\brief The type of the map that indicates which nodes are processed. |
1008 | 1004 |
typedef typename TR::ProcessedMap ProcessedMap; |
1009 | 1005 |
|
1010 | 1006 |
public: |
1011 | 1007 |
|
1012 | 1008 |
/// Constructor. |
1013 | 1009 |
BfsWizard() : TR() {} |
1014 | 1010 |
|
1015 | 1011 |
/// Constructor that requires parameters. |
1016 | 1012 |
|
1017 | 1013 |
/// Constructor that requires parameters. |
1018 | 1014 |
/// These parameters will be the default values for the traits class. |
1019 | 1015 |
BfsWizard(const Digraph &g, Node s=INVALID) : |
1020 | 1016 |
TR(g,s) {} |
1021 | 1017 |
|
1022 | 1018 |
///Copy constructor |
1023 | 1019 |
BfsWizard(const TR &b) : TR(b) {} |
1024 | 1020 |
|
1025 | 1021 |
~BfsWizard() {} |
1026 | 1022 |
|
1027 | 1023 |
///Runs BFS algorithm from a source node. |
1028 | 1024 |
|
1029 | 1025 |
///Runs BFS algorithm from a source node. |
1030 | 1026 |
///The node can be given with the \ref source() function. |
1031 | 1027 |
void run() |
1032 | 1028 |
{ |
1033 | 1029 |
if(Base::_source==INVALID) throw UninitializedParameter(); |
1034 | 1030 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
1035 | 1031 |
if(Base::_reached) |
1036 | 1032 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
1037 | 1033 |
if(Base::_processed) |
1038 | 1034 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1039 | 1035 |
if(Base::_pred) |
1040 | 1036 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1041 | 1037 |
if(Base::_dist) |
1042 | 1038 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1043 | 1039 |
alg.run(Base::_source); |
1044 | 1040 |
} |
1045 | 1041 |
|
1046 | 1042 |
///Runs BFS algorithm from the given node. |
1047 | 1043 |
|
1048 | 1044 |
///Runs BFS algorithm from the given node. |
1049 | 1045 |
///\param s is the given source. |
1050 | 1046 |
void run(Node s) |
1051 | 1047 |
{ |
1052 | 1048 |
Base::_source=s; |
1053 | 1049 |
run(); |
1054 | 1050 |
} |
1055 | 1051 |
|
1056 | 1052 |
/// Sets the source node, from which the Bfs algorithm runs. |
1057 | 1053 |
|
1058 | 1054 |
/// Sets the source node, from which the Bfs algorithm runs. |
1059 | 1055 |
/// \param s is the source node. |
1060 | 1056 |
BfsWizard<TR> &source(Node s) |
1061 | 1057 |
{ |
1062 | 1058 |
Base::_source=s; |
1063 | 1059 |
return *this; |
1064 | 1060 |
} |
1065 | 1061 |
|
1066 | 1062 |
template<class T> |
1067 | 1063 |
struct SetPredMapBase : public Base { |
1068 | 1064 |
typedef T PredMap; |
1069 | 1065 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1070 | 1066 |
SetPredMapBase(const TR &b) : TR(b) {} |
1071 | 1067 |
}; |
1072 | 1068 |
///\brief \ref named-templ-param "Named parameter" |
1073 | 1069 |
///for setting \ref PredMap object. |
1074 | 1070 |
/// |
1075 | 1071 |
/// \ref named-templ-param "Named parameter" |
1076 | 1072 |
///for setting \ref PredMap object. |
1077 | 1073 |
template<class T> |
1078 | 1074 |
BfsWizard<SetPredMapBase<T> > predMap(const T &t) |
1079 | 1075 |
{ |
1080 | 1076 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1081 | 1077 |
return BfsWizard<SetPredMapBase<T> >(*this); |
1082 | 1078 |
} |
1083 | 1079 |
|
1084 | 1080 |
template<class T> |
1085 | 1081 |
struct SetReachedMapBase : public Base { |
1086 | 1082 |
typedef T ReachedMap; |
1087 | 1083 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
1088 | 1084 |
SetReachedMapBase(const TR &b) : TR(b) {} |
1089 | 1085 |
}; |
1090 | 1086 |
///\brief \ref named-templ-param "Named parameter" |
1091 | 1087 |
///for setting \ref ReachedMap object. |
1092 | 1088 |
/// |
1093 | 1089 |
/// \ref named-templ-param "Named parameter" |
1094 | 1090 |
///for setting \ref ReachedMap object. |
1095 | 1091 |
template<class T> |
1096 | 1092 |
BfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
1097 | 1093 |
{ |
1098 | 1094 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1099 | 1095 |
return BfsWizard<SetReachedMapBase<T> >(*this); |
1100 | 1096 |
} |
1101 | 1097 |
|
1102 | 1098 |
template<class T> |
1103 | 1099 |
struct SetProcessedMapBase : public Base { |
1104 | 1100 |
typedef T ProcessedMap; |
1105 | 1101 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1106 | 1102 |
SetProcessedMapBase(const TR &b) : TR(b) {} |
1107 | 1103 |
}; |
1108 | 1104 |
///\brief \ref named-templ-param "Named parameter" |
1109 | 1105 |
///for setting \ref ProcessedMap object. |
1110 | 1106 |
/// |
1111 | 1107 |
/// \ref named-templ-param "Named parameter" |
1112 | 1108 |
///for setting \ref ProcessedMap object. |
1113 | 1109 |
template<class T> |
1114 | 1110 |
BfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
1115 | 1111 |
{ |
1116 | 1112 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1117 | 1113 |
return BfsWizard<SetProcessedMapBase<T> >(*this); |
1118 | 1114 |
} |
1119 | 1115 |
|
1120 | 1116 |
template<class T> |
1121 | 1117 |
struct SetDistMapBase : public Base { |
1122 | 1118 |
typedef T DistMap; |
1123 | 1119 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1124 | 1120 |
SetDistMapBase(const TR &b) : TR(b) {} |
1125 | 1121 |
}; |
1126 | 1122 |
///\brief \ref named-templ-param "Named parameter" |
1127 | 1123 |
///for setting \ref DistMap object. |
1128 | 1124 |
/// |
1129 | 1125 |
/// \ref named-templ-param "Named parameter" |
1130 | 1126 |
///for setting \ref DistMap object. |
1131 | 1127 |
template<class T> |
1132 | 1128 |
BfsWizard<SetDistMapBase<T> > distMap(const T &t) |
1133 | 1129 |
{ |
1134 | 1130 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1135 | 1131 |
return BfsWizard<SetDistMapBase<T> >(*this); |
1136 | 1132 |
} |
1137 | 1133 |
|
1138 | 1134 |
}; |
1139 | 1135 |
|
1140 | 1136 |
///Function type interface for Bfs algorithm. |
1141 | 1137 |
|
1142 | 1138 |
/// \ingroup search |
1143 | 1139 |
///Function type interface for Bfs algorithm. |
1144 | 1140 |
/// |
1145 | 1141 |
///This function also has several |
1146 | 1142 |
///\ref named-templ-func-param "named parameters", |
1147 | 1143 |
///they are declared as the members of class \ref BfsWizard. |
1148 | 1144 |
///The following |
1149 | 1145 |
///example shows how to use these parameters. |
1150 | 1146 |
///\code |
1151 | 1147 |
/// bfs(g,source).predMap(preds).run(); |
1152 | 1148 |
///\endcode |
1153 | 1149 |
///\warning Don't forget to put the \ref BfsWizard::run() "run()" |
1154 | 1150 |
///to the end of the parameter list. |
1155 | 1151 |
///\sa BfsWizard |
1156 | 1152 |
///\sa Bfs |
1157 | 1153 |
template<class GR> |
1158 | 1154 |
BfsWizard<BfsWizardBase<GR> > |
1159 | 1155 |
bfs(const GR &g,typename GR::Node s=INVALID) |
1160 | 1156 |
{ |
1161 | 1157 |
return BfsWizard<BfsWizardBase<GR> >(g,s); |
1162 | 1158 |
} |
1163 | 1159 |
|
1164 | 1160 |
#ifdef DOXYGEN |
1165 | 1161 |
/// \brief Visitor class for BFS. |
1166 | 1162 |
/// |
1167 | 1163 |
/// This class defines the interface of the BfsVisit events, and |
1168 | 1164 |
/// it could be the base of a real visitor class. |
1169 | 1165 |
template <typename _Digraph> |
1170 | 1166 |
struct BfsVisitor { |
1171 | 1167 |
typedef _Digraph Digraph; |
1172 | 1168 |
typedef typename Digraph::Arc Arc; |
1173 | 1169 |
typedef typename Digraph::Node Node; |
1174 | 1170 |
/// \brief Called for the source node(s) of the BFS. |
1175 | 1171 |
/// |
1176 | 1172 |
/// This function is called for the source node(s) of the BFS. |
1177 | 1173 |
void start(const Node& node) {} |
1178 | 1174 |
/// \brief Called when a node is reached first time. |
1179 | 1175 |
/// |
1180 | 1176 |
/// This function is called when a node is reached first time. |
1181 | 1177 |
void reach(const Node& node) {} |
1182 | 1178 |
/// \brief Called when a node is processed. |
1183 | 1179 |
/// |
1184 | 1180 |
/// This function is called when a node is processed. |
1185 | 1181 |
void process(const Node& node) {} |
1186 | 1182 |
/// \brief Called when an arc reaches a new node. |
1187 | 1183 |
/// |
1188 | 1184 |
/// This function is called when the BFS finds an arc whose target node |
1189 | 1185 |
/// is not reached yet. |
1190 | 1186 |
void discover(const Arc& arc) {} |
1191 | 1187 |
/// \brief Called when an arc is examined but its target node is |
1192 | 1188 |
/// already discovered. |
1193 | 1189 |
/// |
1194 | 1190 |
/// This function is called when an arc is examined but its target node is |
1195 | 1191 |
/// already discovered. |
1196 | 1192 |
void examine(const Arc& arc) {} |
1197 | 1193 |
}; |
1198 | 1194 |
#else |
1199 | 1195 |
template <typename _Digraph> |
1200 | 1196 |
struct BfsVisitor { |
1201 | 1197 |
typedef _Digraph Digraph; |
1202 | 1198 |
typedef typename Digraph::Arc Arc; |
1203 | 1199 |
typedef typename Digraph::Node Node; |
1204 | 1200 |
void start(const Node&) {} |
1205 | 1201 |
void reach(const Node&) {} |
1206 | 1202 |
void process(const Node&) {} |
1207 | 1203 |
void discover(const Arc&) {} |
1208 | 1204 |
void examine(const Arc&) {} |
1209 | 1205 |
|
1210 | 1206 |
template <typename _Visitor> |
1211 | 1207 |
struct Constraints { |
1212 | 1208 |
void constraints() { |
1213 | 1209 |
Arc arc; |
1214 | 1210 |
Node node; |
1215 | 1211 |
visitor.start(node); |
1216 | 1212 |
visitor.reach(node); |
1217 | 1213 |
visitor.process(node); |
1218 | 1214 |
visitor.discover(arc); |
1219 | 1215 |
visitor.examine(arc); |
1220 | 1216 |
} |
1221 | 1217 |
_Visitor& visitor; |
1222 | 1218 |
}; |
1223 | 1219 |
}; |
1224 | 1220 |
#endif |
1225 | 1221 |
|
1226 | 1222 |
/// \brief Default traits class of BfsVisit class. |
1227 | 1223 |
/// |
1228 | 1224 |
/// Default traits class of BfsVisit class. |
1229 | 1225 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1230 | 1226 |
template<class _Digraph> |
1231 | 1227 |
struct BfsVisitDefaultTraits { |
1232 | 1228 |
|
1233 | 1229 |
/// \brief The type of the digraph the algorithm runs on. |
1234 | 1230 |
typedef _Digraph Digraph; |
1235 | 1231 |
|
1236 | 1232 |
/// \brief The type of the map that indicates which nodes are reached. |
1237 | 1233 |
/// |
1238 | 1234 |
/// The type of the map that indicates which nodes are reached. |
1239 | 1235 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
1240 | 1236 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1241 | 1237 |
|
1242 | 1238 |
/// \brief Instantiates a \ref ReachedMap. |
1243 | 1239 |
/// |
1244 | 1240 |
/// This function instantiates a \ref ReachedMap. |
1245 | 1241 |
/// \param digraph is the digraph, to which |
1246 | 1242 |
/// we would like to define the \ref ReachedMap. |
1247 | 1243 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1248 | 1244 |
return new ReachedMap(digraph); |
1249 | 1245 |
} |
1250 | 1246 |
|
1251 | 1247 |
}; |
1252 | 1248 |
|
1253 | 1249 |
/// \ingroup search |
1254 | 1250 |
/// |
1255 | 1251 |
/// \brief %BFS algorithm class with visitor interface. |
1256 | 1252 |
/// |
1257 | 1253 |
/// This class provides an efficient implementation of the %BFS algorithm |
1258 | 1254 |
/// with visitor interface. |
1259 | 1255 |
/// |
1260 | 1256 |
/// The %BfsVisit class provides an alternative interface to the Bfs |
1261 | 1257 |
/// class. It works with callback mechanism, the BfsVisit object calls |
1262 | 1258 |
/// the member functions of the \c Visitor class on every BFS event. |
1263 | 1259 |
/// |
1264 | 1260 |
/// This interface of the BFS algorithm should be used in special cases |
1265 | 1261 |
/// when extra actions have to be performed in connection with certain |
1266 | 1262 |
/// events of the BFS algorithm. Otherwise consider to use Bfs or bfs() |
1267 | 1263 |
/// instead. |
1268 | 1264 |
/// |
1269 | 1265 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1270 | 1266 |
/// The default value is |
1271 | 1267 |
/// \ref ListDigraph. The value of _Digraph is not used directly by |
1272 | 1268 |
/// \ref BfsVisit, it is only passed to \ref BfsVisitDefaultTraits. |
1273 | 1269 |
/// \tparam _Visitor The Visitor type that is used by the algorithm. |
1274 | 1270 |
/// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty visitor, which |
1275 | 1271 |
/// does not observe the BFS events. If you want to observe the BFS |
1276 | 1272 |
/// events, you should implement your own visitor class. |
1277 | 1273 |
/// \tparam _Traits Traits class to set various data types used by the |
1278 | 1274 |
/// algorithm. The default traits class is |
1279 | 1275 |
/// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>". |
1280 | 1276 |
/// See \ref BfsVisitDefaultTraits for the documentation of |
1281 | 1277 |
/// a BFS visit traits class. |
1282 | 1278 |
#ifdef DOXYGEN |
1283 | 1279 |
template <typename _Digraph, typename _Visitor, typename _Traits> |
1284 | 1280 |
#else |
1285 | 1281 |
template <typename _Digraph = ListDigraph, |
1286 | 1282 |
typename _Visitor = BfsVisitor<_Digraph>, |
1287 | 1283 |
typename _Traits = BfsDefaultTraits<_Digraph> > |
1288 | 1284 |
#endif |
1289 | 1285 |
class BfsVisit { |
1290 | 1286 |
public: |
1291 | 1287 |
|
1292 | 1288 |
/// \brief \ref Exception for uninitialized parameters. |
1293 | 1289 |
/// |
1294 | 1290 |
/// This error represents problems in the initialization |
1295 | 1291 |
/// of the parameters of the algorithm. |
1296 | 1292 |
class UninitializedParameter : public lemon::UninitializedParameter { |
1297 | 1293 |
public: |
1298 | 1294 |
virtual const char* what() const throw() |
1299 | 1295 |
{ |
1300 | 1296 |
return "lemon::BfsVisit::UninitializedParameter"; |
1301 | 1297 |
} |
1302 | 1298 |
}; |
1303 | 1299 |
|
1304 | 1300 |
///The traits class. |
1305 | 1301 |
typedef _Traits Traits; |
1306 | 1302 |
|
1307 | 1303 |
///The type of the digraph the algorithm runs on. |
1308 | 1304 |
typedef typename Traits::Digraph Digraph; |
1309 | 1305 |
|
1310 | 1306 |
///The visitor type used by the algorithm. |
1311 | 1307 |
typedef _Visitor Visitor; |
1312 | 1308 |
|
1313 | 1309 |
///The type of the map that indicates which nodes are reached. |
1314 | 1310 |
typedef typename Traits::ReachedMap ReachedMap; |
1315 | 1311 |
|
1316 | 1312 |
private: |
1317 | 1313 |
|
1318 | 1314 |
typedef typename Digraph::Node Node; |
1319 | 1315 |
typedef typename Digraph::NodeIt NodeIt; |
1320 | 1316 |
typedef typename Digraph::Arc Arc; |
1321 | 1317 |
typedef typename Digraph::OutArcIt OutArcIt; |
1322 | 1318 |
|
1323 | 1319 |
//Pointer to the underlying digraph. |
1324 | 1320 |
const Digraph *_digraph; |
1325 | 1321 |
//Pointer to the visitor object. |
1326 | 1322 |
Visitor *_visitor; |
1327 | 1323 |
//Pointer to the map of reached status of the nodes. |
1328 | 1324 |
ReachedMap *_reached; |
1329 | 1325 |
//Indicates if _reached is locally allocated (true) or not. |
1330 | 1326 |
bool local_reached; |
1331 | 1327 |
|
1332 | 1328 |
std::vector<typename Digraph::Node> _list; |
1333 | 1329 |
int _list_front, _list_back; |
1334 | 1330 |
|
1335 |
///Creates the maps if necessary. |
|
1336 |
///\todo Better memory allocation (instead of new). |
|
1331 |
//Creates the maps if necessary. |
|
1337 | 1332 |
void create_maps() { |
1338 | 1333 |
if(!_reached) { |
1339 | 1334 |
local_reached = true; |
1340 | 1335 |
_reached = Traits::createReachedMap(*_digraph); |
1341 | 1336 |
} |
1342 | 1337 |
} |
1343 | 1338 |
|
1344 | 1339 |
protected: |
1345 | 1340 |
|
1346 | 1341 |
BfsVisit() {} |
1347 | 1342 |
|
1348 | 1343 |
public: |
1349 | 1344 |
|
1350 | 1345 |
typedef BfsVisit Create; |
1351 | 1346 |
|
1352 | 1347 |
/// \name Named template parameters |
1353 | 1348 |
|
1354 | 1349 |
///@{ |
1355 | 1350 |
template <class T> |
1356 | 1351 |
struct SetReachedMapTraits : public Traits { |
1357 | 1352 |
typedef T ReachedMap; |
1358 | 1353 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1359 | 1354 |
throw UninitializedParameter(); |
1360 | 1355 |
} |
1361 | 1356 |
}; |
1362 | 1357 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1363 | 1358 |
/// ReachedMap type. |
1364 | 1359 |
/// |
1365 | 1360 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
1366 | 1361 |
template <class T> |
1367 | 1362 |
struct SetReachedMap : public BfsVisit< Digraph, Visitor, |
1368 | 1363 |
SetReachedMapTraits<T> > { |
1369 | 1364 |
typedef BfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
1370 | 1365 |
}; |
1371 | 1366 |
///@} |
1372 | 1367 |
|
1373 | 1368 |
public: |
1374 | 1369 |
|
1375 | 1370 |
/// \brief Constructor. |
1376 | 1371 |
/// |
1377 | 1372 |
/// Constructor. |
1378 | 1373 |
/// |
1379 | 1374 |
/// \param digraph The digraph the algorithm runs on. |
1380 | 1375 |
/// \param visitor The visitor object of the algorithm. |
1381 | 1376 |
BfsVisit(const Digraph& digraph, Visitor& visitor) |
1382 | 1377 |
: _digraph(&digraph), _visitor(&visitor), |
1383 | 1378 |
_reached(0), local_reached(false) {} |
1384 | 1379 |
|
1385 | 1380 |
/// \brief Destructor. |
1386 | 1381 |
~BfsVisit() { |
1387 | 1382 |
if(local_reached) delete _reached; |
1388 | 1383 |
} |
1389 | 1384 |
|
1390 | 1385 |
/// \brief Sets the map that indicates which nodes are reached. |
1391 | 1386 |
/// |
1392 | 1387 |
/// Sets the map that indicates which nodes are reached. |
1393 | 1388 |
/// If you don't use this function before calling \ref run(), |
1394 | 1389 |
/// it will allocate one. The destructor deallocates this |
1395 | 1390 |
/// automatically allocated map, of course. |
1396 | 1391 |
/// \return <tt> (*this) </tt> |
1397 | 1392 |
BfsVisit &reachedMap(ReachedMap &m) { |
1398 | 1393 |
if(local_reached) { |
1399 | 1394 |
delete _reached; |
1400 | 1395 |
local_reached = false; |
1401 | 1396 |
} |
1402 | 1397 |
_reached = &m; |
1403 | 1398 |
return *this; |
1404 | 1399 |
} |
1405 | 1400 |
|
1406 | 1401 |
public: |
1407 | 1402 |
|
1408 | 1403 |
/// \name Execution control |
1409 | 1404 |
/// The simplest way to execute the algorithm is to use |
1410 | 1405 |
/// one of the member functions called \ref lemon::BfsVisit::run() |
1411 | 1406 |
/// "run()". |
1412 | 1407 |
/// \n |
1413 | 1408 |
/// If you need more control on the execution, first you must call |
1414 | 1409 |
/// \ref lemon::BfsVisit::init() "init()", then you can add several |
1415 | 1410 |
/// source nodes with \ref lemon::BfsVisit::addSource() "addSource()". |
1416 | 1411 |
/// Finally \ref lemon::BfsVisit::start() "start()" will perform the |
1417 | 1412 |
/// actual path computation. |
1418 | 1413 |
|
1419 | 1414 |
/// @{ |
1420 | 1415 |
|
1421 | 1416 |
/// \brief Initializes the internal data structures. |
1422 | 1417 |
/// |
1423 | 1418 |
/// Initializes the internal data structures. |
1424 | 1419 |
void init() { |
1425 | 1420 |
create_maps(); |
1426 | 1421 |
_list.resize(countNodes(*_digraph)); |
1427 | 1422 |
_list_front = _list_back = -1; |
1428 | 1423 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1429 | 1424 |
_reached->set(u, false); |
1430 | 1425 |
} |
1431 | 1426 |
} |
1432 | 1427 |
|
1433 | 1428 |
/// \brief Adds a new source node. |
1434 | 1429 |
/// |
1435 | 1430 |
/// Adds a new source node to the set of nodes to be processed. |
1436 | 1431 |
void addSource(Node s) { |
1437 | 1432 |
if(!(*_reached)[s]) { |
1438 | 1433 |
_reached->set(s,true); |
1439 | 1434 |
_visitor->start(s); |
1440 | 1435 |
_visitor->reach(s); |
1441 | 1436 |
_list[++_list_back] = s; |
1442 | 1437 |
} |
1443 | 1438 |
} |
1444 | 1439 |
|
1445 | 1440 |
/// \brief Processes the next node. |
1446 | 1441 |
/// |
1447 | 1442 |
/// Processes the next node. |
1448 | 1443 |
/// |
1449 | 1444 |
/// \return The processed node. |
1450 | 1445 |
/// |
1451 | 1446 |
/// \pre The queue must not be empty. |
1452 | 1447 |
Node processNextNode() { |
1453 | 1448 |
Node n = _list[++_list_front]; |
1454 | 1449 |
_visitor->process(n); |
1455 | 1450 |
Arc e; |
1456 | 1451 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1457 | 1452 |
Node m = _digraph->target(e); |
1458 | 1453 |
if (!(*_reached)[m]) { |
1459 | 1454 |
_visitor->discover(e); |
1460 | 1455 |
_visitor->reach(m); |
1461 | 1456 |
_reached->set(m, true); |
1462 | 1457 |
_list[++_list_back] = m; |
1463 | 1458 |
} else { |
1464 | 1459 |
_visitor->examine(e); |
1465 | 1460 |
} |
1466 | 1461 |
} |
1467 | 1462 |
return n; |
1468 | 1463 |
} |
1469 | 1464 |
|
1470 | 1465 |
/// \brief Processes the next node. |
1471 | 1466 |
/// |
1472 | 1467 |
/// Processes the next node and checks if the given target node |
1473 | 1468 |
/// is reached. If the target node is reachable from the processed |
1474 | 1469 |
/// node, then the \c reach parameter will be set to \c true. |
1475 | 1470 |
/// |
1476 | 1471 |
/// \param target The target node. |
1477 | 1472 |
/// \retval reach Indicates if the target node is reached. |
1478 | 1473 |
/// It should be initially \c false. |
1479 | 1474 |
/// |
1480 | 1475 |
/// \return The processed node. |
1481 | 1476 |
/// |
1482 | 1477 |
/// \pre The queue must not be empty. |
1483 | 1478 |
Node processNextNode(Node target, bool& reach) { |
1484 | 1479 |
Node n = _list[++_list_front]; |
1485 | 1480 |
_visitor->process(n); |
1486 | 1481 |
Arc e; |
1487 | 1482 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1488 | 1483 |
Node m = _digraph->target(e); |
1489 | 1484 |
if (!(*_reached)[m]) { |
1490 | 1485 |
_visitor->discover(e); |
1491 | 1486 |
_visitor->reach(m); |
1492 | 1487 |
_reached->set(m, true); |
1493 | 1488 |
_list[++_list_back] = m; |
1494 | 1489 |
reach = reach || (target == m); |
1495 | 1490 |
} else { |
1496 | 1491 |
_visitor->examine(e); |
1497 | 1492 |
} |
1498 | 1493 |
} |
1499 | 1494 |
return n; |
1500 | 1495 |
} |
1501 | 1496 |
|
1502 | 1497 |
/// \brief Processes the next node. |
1503 | 1498 |
/// |
1504 | 1499 |
/// Processes the next node and checks if at least one of reached |
1505 | 1500 |
/// nodes has \c true value in the \c nm node map. If one node |
1506 | 1501 |
/// with \c true value is reachable from the processed node, then the |
1507 | 1502 |
/// \c rnode parameter will be set to the first of such nodes. |
1508 | 1503 |
/// |
1509 | 1504 |
/// \param nm A \c bool (or convertible) node map that indicates the |
1510 | 1505 |
/// possible targets. |
1511 | 1506 |
/// \retval rnode The reached target node. |
1512 | 1507 |
/// It should be initially \c INVALID. |
1513 | 1508 |
/// |
1514 | 1509 |
/// \return The processed node. |
1515 | 1510 |
/// |
1516 | 1511 |
/// \pre The queue must not be empty. |
1517 | 1512 |
template <typename NM> |
1518 | 1513 |
Node processNextNode(const NM& nm, Node& rnode) { |
1519 | 1514 |
Node n = _list[++_list_front]; |
1520 | 1515 |
_visitor->process(n); |
1521 | 1516 |
Arc e; |
1522 | 1517 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1523 | 1518 |
Node m = _digraph->target(e); |
1524 | 1519 |
if (!(*_reached)[m]) { |
1525 | 1520 |
_visitor->discover(e); |
1526 | 1521 |
_visitor->reach(m); |
1527 | 1522 |
_reached->set(m, true); |
1528 | 1523 |
_list[++_list_back] = m; |
1529 | 1524 |
if (nm[m] && rnode == INVALID) rnode = m; |
1530 | 1525 |
} else { |
1531 | 1526 |
_visitor->examine(e); |
1532 | 1527 |
} |
1533 | 1528 |
} |
1534 | 1529 |
return n; |
1535 | 1530 |
} |
1536 | 1531 |
|
1537 | 1532 |
/// \brief The next node to be processed. |
1538 | 1533 |
/// |
1539 | 1534 |
/// Returns the next node to be processed or \c INVALID if the queue |
1540 | 1535 |
/// is empty. |
1541 | 1536 |
Node nextNode() const { |
1542 | 1537 |
return _list_front != _list_back ? _list[_list_front + 1] : INVALID; |
1543 | 1538 |
} |
1544 | 1539 |
|
1545 | 1540 |
/// \brief Returns \c false if there are nodes |
1546 | 1541 |
/// to be processed. |
1547 | 1542 |
/// |
1548 | 1543 |
/// Returns \c false if there are nodes |
1549 | 1544 |
/// to be processed in the queue. |
1550 | 1545 |
bool emptyQueue() const { return _list_front == _list_back; } |
1551 | 1546 |
|
1552 | 1547 |
/// \brief Returns the number of the nodes to be processed. |
1553 | 1548 |
/// |
1554 | 1549 |
/// Returns the number of the nodes to be processed in the queue. |
1555 | 1550 |
int queueSize() const { return _list_back - _list_front; } |
1556 | 1551 |
|
1557 | 1552 |
/// \brief Executes the algorithm. |
1558 | 1553 |
/// |
1559 | 1554 |
/// Executes the algorithm. |
1560 | 1555 |
/// |
1561 | 1556 |
/// This method runs the %BFS algorithm from the root node(s) |
1562 | 1557 |
/// in order to compute the shortest path to each node. |
1563 | 1558 |
/// |
1564 | 1559 |
/// The algorithm computes |
1565 | 1560 |
/// - the shortest path tree (forest), |
1566 | 1561 |
/// - the distance of each node from the root(s). |
1567 | 1562 |
/// |
1568 | 1563 |
/// \pre init() must be called and at least one root node should be added |
1569 | 1564 |
/// with addSource() before using this function. |
1570 | 1565 |
/// |
1571 | 1566 |
/// \note <tt>b.start()</tt> is just a shortcut of the following code. |
1572 | 1567 |
/// \code |
1573 | 1568 |
/// while ( !b.emptyQueue() ) { |
1574 | 1569 |
/// b.processNextNode(); |
1575 | 1570 |
/// } |
1576 | 1571 |
/// \endcode |
1577 | 1572 |
void start() { |
1578 | 1573 |
while ( !emptyQueue() ) processNextNode(); |
1579 | 1574 |
} |
1580 | 1575 |
|
1581 | 1576 |
/// \brief Executes the algorithm until the given target node is reached. |
1582 | 1577 |
/// |
1583 | 1578 |
/// Executes the algorithm until the given target node is reached. |
1584 | 1579 |
/// |
1585 | 1580 |
/// This method runs the %BFS algorithm from the root node(s) |
1586 | 1581 |
/// in order to compute the shortest path to \c dest. |
1587 | 1582 |
/// |
1588 | 1583 |
/// The algorithm computes |
1589 | 1584 |
/// - the shortest path to \c dest, |
1590 | 1585 |
/// - the distance of \c dest from the root(s). |
1591 | 1586 |
/// |
1592 | 1587 |
/// \pre init() must be called and at least one root node should be |
1593 | 1588 |
/// added with addSource() before using this function. |
1594 | 1589 |
/// |
1595 | 1590 |
/// \note <tt>b.start(t)</tt> is just a shortcut of the following code. |
1596 | 1591 |
/// \code |
1597 | 1592 |
/// bool reach = false; |
1598 | 1593 |
/// while ( !b.emptyQueue() && !reach ) { |
1599 | 1594 |
/// b.processNextNode(t, reach); |
1600 | 1595 |
/// } |
1601 | 1596 |
/// \endcode |
1602 | 1597 |
void start(Node dest) { |
1603 | 1598 |
bool reach = false; |
1604 | 1599 |
while ( !emptyQueue() && !reach ) processNextNode(dest, reach); |
1605 | 1600 |
} |
1606 | 1601 |
|
1607 | 1602 |
/// \brief Executes the algorithm until a condition is met. |
1608 | 1603 |
/// |
1609 | 1604 |
/// Executes the algorithm until a condition is met. |
1610 | 1605 |
/// |
1611 | 1606 |
/// This method runs the %BFS algorithm from the root node(s) in |
1612 | 1607 |
/// order to compute the shortest path to a node \c v with |
1613 | 1608 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
1614 | 1609 |
/// |
1615 | 1610 |
/// \param nm must be a bool (or convertible) node map. The |
1616 | 1611 |
/// algorithm will stop when it reaches a node \c v with |
1617 | 1612 |
/// <tt>nm[v]</tt> true. |
1618 | 1613 |
/// |
1619 | 1614 |
/// \return The reached node \c v with <tt>nm[v]</tt> true or |
1620 | 1615 |
/// \c INVALID if no such node was found. |
1621 | 1616 |
/// |
1622 | 1617 |
/// \pre init() must be called and at least one root node should be |
1623 | 1618 |
/// added with addSource() before using this function. |
1624 | 1619 |
/// |
1625 | 1620 |
/// \note <tt>b.start(nm)</tt> is just a shortcut of the following code. |
1626 | 1621 |
/// \code |
1627 | 1622 |
/// Node rnode = INVALID; |
1628 | 1623 |
/// while ( !b.emptyQueue() && rnode == INVALID ) { |
1629 | 1624 |
/// b.processNextNode(nm, rnode); |
1630 | 1625 |
/// } |
1631 | 1626 |
/// return rnode; |
1632 | 1627 |
/// \endcode |
1633 | 1628 |
template <typename NM> |
1634 | 1629 |
Node start(const NM &nm) { |
1635 | 1630 |
Node rnode = INVALID; |
1636 | 1631 |
while ( !emptyQueue() && rnode == INVALID ) { |
1637 | 1632 |
processNextNode(nm, rnode); |
1638 | 1633 |
} |
1639 | 1634 |
return rnode; |
1640 | 1635 |
} |
1641 | 1636 |
|
1642 | 1637 |
/// \brief Runs the algorithm from the given node. |
1643 | 1638 |
/// |
1644 | 1639 |
/// This method runs the %BFS algorithm from node \c s |
1645 | 1640 |
/// in order to compute the shortest path to each node. |
1646 | 1641 |
/// |
1647 | 1642 |
/// The algorithm computes |
1648 | 1643 |
/// - the shortest path tree, |
1649 | 1644 |
/// - the distance of each node from the root. |
1650 | 1645 |
/// |
1651 | 1646 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
1652 | 1647 |
///\code |
1653 | 1648 |
/// b.init(); |
1654 | 1649 |
/// b.addSource(s); |
1655 | 1650 |
/// b.start(); |
1656 | 1651 |
///\endcode |
1657 | 1652 |
void run(Node s) { |
1658 | 1653 |
init(); |
1659 | 1654 |
addSource(s); |
1660 | 1655 |
start(); |
1661 | 1656 |
} |
1662 | 1657 |
|
1663 | 1658 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
1664 | 1659 |
/// |
1665 | 1660 |
/// This method runs the %BFS algorithm in order to |
1666 | 1661 |
/// compute the shortest path to each node. |
1667 | 1662 |
/// |
1668 | 1663 |
/// The algorithm computes |
1669 | 1664 |
/// - the shortest path tree (forest), |
1670 | 1665 |
/// - the distance of each node from the root(s). |
1671 | 1666 |
/// |
1672 | 1667 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
1673 | 1668 |
///\code |
1674 | 1669 |
/// b.init(); |
1675 | 1670 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
1676 | 1671 |
/// if (!b.reached(n)) { |
1677 | 1672 |
/// b.addSource(n); |
1678 | 1673 |
/// b.start(); |
1679 | 1674 |
/// } |
1680 | 1675 |
/// } |
1681 | 1676 |
///\endcode |
1682 | 1677 |
void run() { |
1683 | 1678 |
init(); |
1684 | 1679 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1685 | 1680 |
if (!reached(it)) { |
1686 | 1681 |
addSource(it); |
1687 | 1682 |
start(); |
1688 | 1683 |
} |
1689 | 1684 |
} |
1690 | 1685 |
} |
1691 | 1686 |
|
1692 | 1687 |
///@} |
1693 | 1688 |
|
1694 | 1689 |
/// \name Query Functions |
1695 | 1690 |
/// The result of the %BFS algorithm can be obtained using these |
1696 | 1691 |
/// functions.\n |
1697 | 1692 |
/// Either \ref lemon::BfsVisit::run() "run()" or |
1698 | 1693 |
/// \ref lemon::BfsVisit::start() "start()" must be called before |
1699 | 1694 |
/// using them. |
1700 | 1695 |
///@{ |
1701 | 1696 |
|
1702 | 1697 |
/// \brief Checks if a node is reachable from the root(s). |
1703 | 1698 |
/// |
1704 | 1699 |
/// Returns \c true if \c v is reachable from the root(s). |
1705 | 1700 |
/// \pre Either \ref run() or \ref start() |
1706 | 1701 |
/// must be called before using this function. |
1707 | 1702 |
bool reached(Node v) { return (*_reached)[v]; } |
1708 | 1703 |
|
1709 | 1704 |
///@} |
1710 | 1705 |
|
1711 | 1706 |
}; |
1712 | 1707 |
|
1713 | 1708 |
} //END OF NAMESPACE LEMON |
1714 | 1709 |
|
1715 | 1710 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-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_BASE_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_BASE_EXTENDER_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
#include <lemon/error.h> |
24 | 24 |
|
25 | 25 |
#include <lemon/bits/map_extender.h> |
26 | 26 |
#include <lemon/bits/default_map.h> |
27 | 27 |
|
28 | 28 |
#include <lemon/concept_check.h> |
29 | 29 |
#include <lemon/concepts/maps.h> |
30 | 30 |
|
31 | 31 |
///\ingroup digraphbits |
32 | 32 |
///\file |
33 | 33 |
///\brief Extenders for the digraph types |
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \ingroup digraphbits |
37 | 37 |
/// |
38 | 38 |
/// \brief BaseDigraph to BaseGraph extender |
39 | 39 |
template <typename Base> |
40 | 40 |
class UndirDigraphExtender : public Base { |
41 | 41 |
|
42 | 42 |
public: |
43 | 43 |
|
44 | 44 |
typedef Base Parent; |
45 | 45 |
typedef typename Parent::Arc Edge; |
46 | 46 |
typedef typename Parent::Node Node; |
47 | 47 |
|
48 | 48 |
typedef True UndirectedTag; |
49 | 49 |
|
50 | 50 |
class Arc : public Edge { |
51 | 51 |
friend class UndirDigraphExtender; |
52 | 52 |
|
53 | 53 |
protected: |
54 | 54 |
bool forward; |
55 | 55 |
|
56 | 56 |
Arc(const Edge &ue, bool _forward) : |
57 | 57 |
Edge(ue), forward(_forward) {} |
58 | 58 |
|
59 | 59 |
public: |
60 | 60 |
Arc() {} |
61 | 61 |
|
62 | 62 |
// Invalid arc constructor |
63 | 63 |
Arc(Invalid i) : Edge(i), forward(true) {} |
64 | 64 |
|
65 | 65 |
bool operator==(const Arc &that) const { |
66 | 66 |
return forward==that.forward && Edge(*this)==Edge(that); |
67 | 67 |
} |
68 | 68 |
bool operator!=(const Arc &that) const { |
69 | 69 |
return forward!=that.forward || Edge(*this)!=Edge(that); |
70 | 70 |
} |
71 | 71 |
bool operator<(const Arc &that) const { |
72 | 72 |
return forward<that.forward || |
73 | 73 |
(!(that.forward<forward) && Edge(*this)<Edge(that)); |
74 | 74 |
} |
75 | 75 |
}; |
76 | 76 |
|
77 | 77 |
/// First node of the edge |
78 | 78 |
Node u(const Edge &e) const { |
79 | 79 |
return Parent::source(e); |
80 | 80 |
} |
81 | 81 |
|
82 | 82 |
/// Source of the given arc |
83 | 83 |
Node source(const Arc &e) const { |
84 | 84 |
return e.forward ? Parent::source(e) : Parent::target(e); |
85 | 85 |
} |
86 | 86 |
|
87 | 87 |
/// Second node of the edge |
88 | 88 |
Node v(const Edge &e) const { |
89 | 89 |
return Parent::target(e); |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
/// Target of the given arc |
93 | 93 |
Node target(const Arc &e) const { |
94 | 94 |
return e.forward ? Parent::target(e) : Parent::source(e); |
95 | 95 |
} |
96 | 96 |
|
97 | 97 |
/// \brief Directed arc from an edge. |
98 | 98 |
/// |
99 | 99 |
/// Returns a directed arc corresponding to the specified edge. |
100 | 100 |
/// If the given bool is true, the first node of the given edge and |
101 | 101 |
/// the source node of the returned arc are the same. |
102 | 102 |
static Arc direct(const Edge &e, bool d) { |
103 | 103 |
return Arc(e, d); |
104 | 104 |
} |
105 | 105 |
|
106 | 106 |
/// Returns whether the given directed arc has the same orientation |
107 | 107 |
/// as the corresponding edge. |
108 |
/// |
|
109 |
/// \todo reference to the corresponding point of the undirected digraph |
|
110 |
/// concept. "What does the direction of an edge mean?" |
|
111 | 108 |
static bool direction(const Arc &a) { return a.forward; } |
112 | 109 |
|
113 | 110 |
using Parent::first; |
114 | 111 |
using Parent::next; |
115 | 112 |
|
116 | 113 |
void first(Arc &e) const { |
117 | 114 |
Parent::first(e); |
118 | 115 |
e.forward=true; |
119 | 116 |
} |
120 | 117 |
|
121 | 118 |
void next(Arc &e) const { |
122 | 119 |
if( e.forward ) { |
123 | 120 |
e.forward = false; |
124 | 121 |
} |
125 | 122 |
else { |
126 | 123 |
Parent::next(e); |
127 | 124 |
e.forward = true; |
128 | 125 |
} |
129 | 126 |
} |
130 | 127 |
|
131 | 128 |
void firstOut(Arc &e, const Node &n) const { |
132 | 129 |
Parent::firstIn(e,n); |
133 | 130 |
if( Edge(e) != INVALID ) { |
134 | 131 |
e.forward = false; |
135 | 132 |
} |
136 | 133 |
else { |
137 | 134 |
Parent::firstOut(e,n); |
138 | 135 |
e.forward = true; |
139 | 136 |
} |
140 | 137 |
} |
141 | 138 |
void nextOut(Arc &e) const { |
142 | 139 |
if( ! e.forward ) { |
143 | 140 |
Node n = Parent::target(e); |
144 | 141 |
Parent::nextIn(e); |
145 | 142 |
if( Edge(e) == INVALID ) { |
146 | 143 |
Parent::firstOut(e, n); |
147 | 144 |
e.forward = true; |
148 | 145 |
} |
149 | 146 |
} |
150 | 147 |
else { |
151 | 148 |
Parent::nextOut(e); |
152 | 149 |
} |
153 | 150 |
} |
154 | 151 |
|
155 | 152 |
void firstIn(Arc &e, const Node &n) const { |
156 | 153 |
Parent::firstOut(e,n); |
157 | 154 |
if( Edge(e) != INVALID ) { |
158 | 155 |
e.forward = false; |
159 | 156 |
} |
160 | 157 |
else { |
161 | 158 |
Parent::firstIn(e,n); |
162 | 159 |
e.forward = true; |
163 | 160 |
} |
164 | 161 |
} |
165 | 162 |
void nextIn(Arc &e) const { |
166 | 163 |
if( ! e.forward ) { |
167 | 164 |
Node n = Parent::source(e); |
168 | 165 |
Parent::nextOut(e); |
169 | 166 |
if( Edge(e) == INVALID ) { |
170 | 167 |
Parent::firstIn(e, n); |
171 | 168 |
e.forward = true; |
172 | 169 |
} |
173 | 170 |
} |
174 | 171 |
else { |
175 | 172 |
Parent::nextIn(e); |
176 | 173 |
} |
177 | 174 |
} |
178 | 175 |
|
179 | 176 |
void firstInc(Edge &e, bool &d, const Node &n) const { |
180 | 177 |
d = true; |
181 | 178 |
Parent::firstOut(e, n); |
182 | 179 |
if (e != INVALID) return; |
183 | 180 |
d = false; |
184 | 181 |
Parent::firstIn(e, n); |
185 | 182 |
} |
186 | 183 |
|
187 | 184 |
void nextInc(Edge &e, bool &d) const { |
188 | 185 |
if (d) { |
189 | 186 |
Node s = Parent::source(e); |
190 | 187 |
Parent::nextOut(e); |
191 | 188 |
if (e != INVALID) return; |
192 | 189 |
d = false; |
193 | 190 |
Parent::firstIn(e, s); |
194 | 191 |
} else { |
195 | 192 |
Parent::nextIn(e); |
196 | 193 |
} |
197 | 194 |
} |
198 | 195 |
|
199 | 196 |
Node nodeFromId(int ix) const { |
200 | 197 |
return Parent::nodeFromId(ix); |
201 | 198 |
} |
202 | 199 |
|
203 | 200 |
Arc arcFromId(int ix) const { |
204 | 201 |
return direct(Parent::arcFromId(ix >> 1), bool(ix & 1)); |
205 | 202 |
} |
206 | 203 |
|
207 | 204 |
Edge edgeFromId(int ix) const { |
208 | 205 |
return Parent::arcFromId(ix); |
209 | 206 |
} |
210 | 207 |
|
211 | 208 |
int id(const Node &n) const { |
212 | 209 |
return Parent::id(n); |
213 | 210 |
} |
214 | 211 |
|
215 | 212 |
int id(const Edge &e) const { |
216 | 213 |
return Parent::id(e); |
217 | 214 |
} |
218 | 215 |
|
219 | 216 |
int id(const Arc &e) const { |
220 | 217 |
return 2 * Parent::id(e) + int(e.forward); |
221 | 218 |
} |
222 | 219 |
|
223 | 220 |
int maxNodeId() const { |
224 | 221 |
return Parent::maxNodeId(); |
225 | 222 |
} |
226 | 223 |
|
227 | 224 |
int maxArcId() const { |
228 | 225 |
return 2 * Parent::maxArcId() + 1; |
229 | 226 |
} |
230 | 227 |
|
231 | 228 |
int maxEdgeId() const { |
232 | 229 |
return Parent::maxArcId(); |
233 | 230 |
} |
234 | 231 |
|
235 | 232 |
int arcNum() const { |
236 | 233 |
return 2 * Parent::arcNum(); |
237 | 234 |
} |
238 | 235 |
|
239 | 236 |
int edgeNum() const { |
240 | 237 |
return Parent::arcNum(); |
241 | 238 |
} |
242 | 239 |
|
243 | 240 |
Arc findArc(Node s, Node t, Arc p = INVALID) const { |
244 | 241 |
if (p == INVALID) { |
245 | 242 |
Edge arc = Parent::findArc(s, t); |
246 | 243 |
if (arc != INVALID) return direct(arc, true); |
247 | 244 |
arc = Parent::findArc(t, s); |
248 | 245 |
if (arc != INVALID) return direct(arc, false); |
249 | 246 |
} else if (direction(p)) { |
250 | 247 |
Edge arc = Parent::findArc(s, t, p); |
251 | 248 |
if (arc != INVALID) return direct(arc, true); |
252 | 249 |
arc = Parent::findArc(t, s); |
253 | 250 |
if (arc != INVALID) return direct(arc, false); |
254 | 251 |
} else { |
255 | 252 |
Edge arc = Parent::findArc(t, s, p); |
256 | 253 |
if (arc != INVALID) return direct(arc, false); |
257 | 254 |
} |
258 | 255 |
return INVALID; |
259 | 256 |
} |
260 | 257 |
|
261 | 258 |
Edge findEdge(Node s, Node t, Edge p = INVALID) const { |
262 | 259 |
if (s != t) { |
263 | 260 |
if (p == INVALID) { |
264 | 261 |
Edge arc = Parent::findArc(s, t); |
265 | 262 |
if (arc != INVALID) return arc; |
266 | 263 |
arc = Parent::findArc(t, s); |
267 | 264 |
if (arc != INVALID) return arc; |
268 | 265 |
} else if (Parent::s(p) == s) { |
269 | 266 |
Edge arc = Parent::findArc(s, t, p); |
270 | 267 |
if (arc != INVALID) return arc; |
271 | 268 |
arc = Parent::findArc(t, s); |
272 | 269 |
if (arc != INVALID) return arc; |
273 | 270 |
} else { |
274 | 271 |
Edge arc = Parent::findArc(t, s, p); |
275 | 272 |
if (arc != INVALID) return arc; |
276 | 273 |
} |
277 | 274 |
} else { |
278 | 275 |
return Parent::findArc(s, t, p); |
279 | 276 |
} |
280 | 277 |
return INVALID; |
281 | 278 |
} |
282 | 279 |
}; |
283 | 280 |
|
284 | 281 |
template <typename Base> |
285 | 282 |
class BidirBpGraphExtender : public Base { |
286 | 283 |
public: |
287 | 284 |
typedef Base Parent; |
288 | 285 |
typedef BidirBpGraphExtender Digraph; |
289 | 286 |
|
290 | 287 |
typedef typename Parent::Node Node; |
291 | 288 |
typedef typename Parent::Edge Edge; |
292 | 289 |
|
293 | 290 |
|
294 | 291 |
using Parent::first; |
295 | 292 |
using Parent::next; |
296 | 293 |
|
297 | 294 |
using Parent::id; |
298 | 295 |
|
299 | 296 |
class Red : public Node { |
300 | 297 |
friend class BidirBpGraphExtender; |
301 | 298 |
public: |
302 | 299 |
Red() {} |
303 | 300 |
Red(const Node& node) : Node(node) { |
304 | 301 |
LEMON_ASSERT(Parent::red(node) || node == INVALID, |
305 | 302 |
typename Parent::NodeSetError()); |
306 | 303 |
} |
307 | 304 |
Red& operator=(const Node& node) { |
308 | 305 |
LEMON_ASSERT(Parent::red(node) || node == INVALID, |
309 | 306 |
typename Parent::NodeSetError()); |
310 | 307 |
Node::operator=(node); |
311 | 308 |
return *this; |
312 | 309 |
} |
313 | 310 |
Red(Invalid) : Node(INVALID) {} |
314 | 311 |
Red& operator=(Invalid) { |
315 | 312 |
Node::operator=(INVALID); |
316 | 313 |
return *this; |
317 | 314 |
} |
318 | 315 |
}; |
319 | 316 |
|
320 | 317 |
void first(Red& node) const { |
321 | 318 |
Parent::firstRed(static_cast<Node&>(node)); |
322 | 319 |
} |
323 | 320 |
void next(Red& node) const { |
324 | 321 |
Parent::nextRed(static_cast<Node&>(node)); |
325 | 322 |
} |
326 | 323 |
|
327 | 324 |
int id(const Red& node) const { |
328 | 325 |
return Parent::redId(node); |
329 | 326 |
} |
330 | 327 |
|
331 | 328 |
class Blue : public Node { |
332 | 329 |
friend class BidirBpGraphExtender; |
333 | 330 |
public: |
334 | 331 |
Blue() {} |
335 | 332 |
Blue(const Node& node) : Node(node) { |
336 | 333 |
LEMON_ASSERT(Parent::blue(node) || node == INVALID, |
337 | 334 |
typename Parent::NodeSetError()); |
338 | 335 |
} |
339 | 336 |
Blue& operator=(const Node& node) { |
340 | 337 |
LEMON_ASSERT(Parent::blue(node) || node == INVALID, |
341 | 338 |
typename Parent::NodeSetError()); |
342 | 339 |
Node::operator=(node); |
343 | 340 |
return *this; |
344 | 341 |
} |
345 | 342 |
Blue(Invalid) : Node(INVALID) {} |
346 | 343 |
Blue& operator=(Invalid) { |
347 | 344 |
Node::operator=(INVALID); |
348 | 345 |
return *this; |
349 | 346 |
} |
350 | 347 |
}; |
351 | 348 |
|
352 | 349 |
void first(Blue& node) const { |
353 | 350 |
Parent::firstBlue(static_cast<Node&>(node)); |
354 | 351 |
} |
355 | 352 |
void next(Blue& node) const { |
356 | 353 |
Parent::nextBlue(static_cast<Node&>(node)); |
357 | 354 |
} |
358 | 355 |
|
359 | 356 |
int id(const Blue& node) const { |
360 | 357 |
return Parent::redId(node); |
361 | 358 |
} |
362 | 359 |
|
363 | 360 |
Node source(const Edge& arc) const { |
364 | 361 |
return red(arc); |
365 | 362 |
} |
366 | 363 |
Node target(const Edge& arc) const { |
367 | 364 |
return blue(arc); |
368 | 365 |
} |
369 | 366 |
|
370 | 367 |
void firstInc(Edge& arc, bool& dir, const Node& node) const { |
371 | 368 |
if (Parent::red(node)) { |
372 | 369 |
Parent::firstFromRed(arc, node); |
373 | 370 |
dir = true; |
374 | 371 |
} else { |
375 | 372 |
Parent::firstFromBlue(arc, node); |
376 | 373 |
dir = static_cast<Edge&>(arc) == INVALID; |
377 | 374 |
} |
378 | 375 |
} |
379 | 376 |
void nextInc(Edge& arc, bool& dir) const { |
380 | 377 |
if (dir) { |
381 | 378 |
Parent::nextFromRed(arc); |
382 | 379 |
} else { |
383 | 380 |
Parent::nextFromBlue(arc); |
384 | 381 |
if (arc == INVALID) dir = true; |
385 | 382 |
} |
386 | 383 |
} |
387 | 384 |
|
388 | 385 |
class Arc : public Edge { |
389 | 386 |
friend class BidirBpGraphExtender; |
390 | 387 |
protected: |
391 | 388 |
bool forward; |
392 | 389 |
|
393 | 390 |
Arc(const Edge& arc, bool _forward) |
394 | 391 |
: Edge(arc), forward(_forward) {} |
395 | 392 |
|
396 | 393 |
public: |
397 | 394 |
Arc() {} |
398 | 395 |
Arc (Invalid) : Edge(INVALID), forward(true) {} |
399 | 396 |
bool operator==(const Arc& i) const { |
400 | 397 |
return Edge::operator==(i) && forward == i.forward; |
401 | 398 |
} |
402 | 399 |
bool operator!=(const Arc& i) const { |
403 | 400 |
return Edge::operator!=(i) || forward != i.forward; |
404 | 401 |
} |
405 | 402 |
bool operator<(const Arc& i) const { |
406 | 403 |
return Edge::operator<(i) || |
407 | 404 |
(!(i.forward<forward) && Edge(*this)<Edge(i)); |
408 | 405 |
} |
409 | 406 |
}; |
410 | 407 |
|
411 | 408 |
void first(Arc& arc) const { |
412 | 409 |
Parent::first(static_cast<Edge&>(arc)); |
413 | 410 |
arc.forward = true; |
414 | 411 |
} |
415 | 412 |
|
416 | 413 |
void next(Arc& arc) const { |
417 | 414 |
if (!arc.forward) { |
418 | 415 |
Parent::next(static_cast<Edge&>(arc)); |
419 | 416 |
} |
420 | 417 |
arc.forward = !arc.forward; |
421 | 418 |
} |
422 | 419 |
|
423 | 420 |
void firstOut(Arc& arc, const Node& node) const { |
424 | 421 |
if (Parent::red(node)) { |
425 | 422 |
Parent::firstFromRed(arc, node); |
426 | 423 |
arc.forward = true; |
427 | 424 |
} else { |
428 | 425 |
Parent::firstFromBlue(arc, node); |
429 | 426 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
430 | 427 |
} |
431 | 428 |
} |
432 | 429 |
void nextOut(Arc& arc) const { |
433 | 430 |
if (arc.forward) { |
434 | 431 |
Parent::nextFromRed(arc); |
435 | 432 |
} else { |
436 | 433 |
Parent::nextFromBlue(arc); |
437 | 434 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
438 | 435 |
} |
439 | 436 |
} |
440 | 437 |
|
441 | 438 |
void firstIn(Arc& arc, const Node& node) const { |
442 | 439 |
if (Parent::blue(node)) { |
443 | 440 |
Parent::firstFromBlue(arc, node); |
444 | 441 |
arc.forward = true; |
445 | 442 |
} else { |
446 | 443 |
Parent::firstFromRed(arc, node); |
447 | 444 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
448 | 445 |
} |
449 | 446 |
} |
450 | 447 |
void nextIn(Arc& arc) const { |
451 | 448 |
if (arc.forward) { |
452 | 449 |
Parent::nextFromBlue(arc); |
453 | 450 |
} else { |
454 | 451 |
Parent::nextFromRed(arc); |
455 | 452 |
arc.forward = static_cast<Edge&>(arc) == INVALID; |
456 | 453 |
} |
457 | 454 |
} |
458 | 455 |
|
459 | 456 |
Node source(const Arc& arc) const { |
460 | 457 |
return arc.forward ? Parent::red(arc) : Parent::blue(arc); |
461 | 458 |
} |
462 | 459 |
Node target(const Arc& arc) const { |
463 | 460 |
return arc.forward ? Parent::blue(arc) : Parent::red(arc); |
464 | 461 |
} |
465 | 462 |
|
466 | 463 |
int id(const Arc& arc) const { |
467 | 464 |
return (Parent::id(static_cast<const Edge&>(arc)) << 1) + |
468 | 465 |
(arc.forward ? 0 : 1); |
469 | 466 |
} |
470 | 467 |
Arc arcFromId(int ix) const { |
471 | 468 |
return Arc(Parent::fromEdgeId(ix >> 1), (ix & 1) == 0); |
472 | 469 |
} |
473 | 470 |
int maxArcId() const { |
474 | 471 |
return (Parent::maxEdgeId() << 1) + 1; |
475 | 472 |
} |
476 | 473 |
|
477 | 474 |
bool direction(const Arc& arc) const { |
478 | 475 |
return arc.forward; |
479 | 476 |
} |
480 | 477 |
|
481 | 478 |
Arc direct(const Edge& arc, bool dir) const { |
482 | 479 |
return Arc(arc, dir); |
483 | 480 |
} |
484 | 481 |
|
485 | 482 |
int arcNum() const { |
486 | 483 |
return 2 * Parent::edgeNum(); |
487 | 484 |
} |
488 | 485 |
|
489 | 486 |
int edgeNum() const { |
490 | 487 |
return Parent::edgeNum(); |
491 | 488 |
} |
492 | 489 |
|
493 | 490 |
|
494 | 491 |
}; |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-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/core.h> |
26 | 26 |
#include <lemon/bits/alteration_notifier.h> |
27 | 27 |
|
28 | 28 |
#include <lemon/concept_check.h> |
29 | 29 |
#include <lemon/concepts/maps.h> |
30 | 30 |
|
31 | 31 |
///\ingroup graphbits |
32 | 32 |
/// |
33 | 33 |
///\file |
34 | 34 |
///\brief Vector based graph maps. |
35 | 35 |
namespace lemon { |
36 | 36 |
|
37 | 37 |
/// \ingroup graphbits |
38 | 38 |
/// |
39 | 39 |
/// \brief Graph map based on the std::vector storage. |
40 | 40 |
/// |
41 | 41 |
/// The VectorMap template class is graph map structure what |
42 | 42 |
/// automatically updates the map when a key is added to or erased from |
43 | 43 |
/// the map. This map type uses the std::vector to store the values. |
44 | 44 |
/// |
45 |
/// \tparam |
|
45 |
/// \tparam _Graph The graph this map is attached to. |
|
46 | 46 |
/// \tparam _Item The item type of the graph items. |
47 | 47 |
/// \tparam _Value The value type of the map. |
48 |
/// \todo Fix the doc: there is _Graph parameter instead of _Notifier. |
|
49 | 48 |
template <typename _Graph, typename _Item, typename _Value> |
50 | 49 |
class VectorMap |
51 | 50 |
: public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { |
52 | 51 |
private: |
53 | 52 |
|
54 | 53 |
/// The container type of the map. |
55 | 54 |
typedef std::vector<_Value> Container; |
56 | 55 |
|
57 | 56 |
public: |
58 | 57 |
|
59 | 58 |
/// The graph type of the map. |
60 | 59 |
typedef _Graph Graph; |
61 | 60 |
/// The item type of the map. |
62 | 61 |
typedef _Item Item; |
63 | 62 |
/// The reference map tag. |
64 | 63 |
typedef True ReferenceMapTag; |
65 | 64 |
|
66 | 65 |
/// The key type of the map. |
67 | 66 |
typedef _Item Key; |
68 | 67 |
/// The value type of the map. |
69 | 68 |
typedef _Value Value; |
70 | 69 |
|
71 | 70 |
/// The notifier type. |
72 | 71 |
typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; |
73 | 72 |
|
74 | 73 |
/// The map type. |
75 | 74 |
typedef VectorMap Map; |
76 | 75 |
/// The base class of the map. |
77 | 76 |
typedef typename Notifier::ObserverBase Parent; |
78 | 77 |
|
79 | 78 |
/// The reference type of the map; |
80 | 79 |
typedef typename Container::reference Reference; |
81 | 80 |
/// The const reference type of the map; |
82 | 81 |
typedef typename Container::const_reference ConstReference; |
83 | 82 |
|
84 | 83 |
|
85 | 84 |
/// \brief Constructor to attach the new map into the notifier. |
86 | 85 |
/// |
87 | 86 |
/// It constructs a map and attachs it into the notifier. |
88 | 87 |
/// It adds all the items of the graph to the map. |
89 | 88 |
VectorMap(const Graph& graph) { |
90 | 89 |
Parent::attach(graph.notifier(Item())); |
91 | 90 |
container.resize(Parent::notifier()->maxId() + 1); |
92 | 91 |
} |
93 | 92 |
|
94 | 93 |
/// \brief Constructor uses given value to initialize the map. |
95 | 94 |
/// |
96 | 95 |
/// It constructs a map uses a given value to initialize the map. |
97 | 96 |
/// It adds all the items of the graph to the map. |
98 | 97 |
VectorMap(const Graph& graph, const Value& value) { |
99 | 98 |
Parent::attach(graph.notifier(Item())); |
100 | 99 |
container.resize(Parent::notifier()->maxId() + 1, value); |
101 | 100 |
} |
102 | 101 |
|
103 | 102 |
private: |
104 | 103 |
/// \brief Copy constructor |
105 | 104 |
/// |
106 | 105 |
/// Copy constructor. |
107 | 106 |
VectorMap(const VectorMap& _copy) : Parent() { |
108 | 107 |
if (_copy.attached()) { |
109 | 108 |
Parent::attach(*_copy.notifier()); |
110 | 109 |
container = _copy.container; |
111 | 110 |
} |
112 | 111 |
} |
113 | 112 |
|
114 | 113 |
/// \brief Assign operator. |
115 | 114 |
/// |
116 | 115 |
/// This operator assigns for each item in the map the |
117 | 116 |
/// value mapped to the same item in the copied map. |
118 | 117 |
/// The parameter map should be indiced with the same |
119 | 118 |
/// itemset because this assign operator does not change |
120 | 119 |
/// the container of the map. |
121 | 120 |
VectorMap& operator=(const VectorMap& cmap) { |
122 | 121 |
return operator=<VectorMap>(cmap); |
123 | 122 |
} |
124 | 123 |
|
125 | 124 |
|
126 | 125 |
/// \brief Template assign operator. |
127 | 126 |
/// |
128 | 127 |
/// The given parameter should be conform to the ReadMap |
129 | 128 |
/// concecpt and could be indiced by the current item set of |
130 | 129 |
/// the NodeMap. In this case the value for each item |
131 | 130 |
/// is assigned by the value of the given ReadMap. |
132 | 131 |
template <typename CMap> |
133 | 132 |
VectorMap& operator=(const CMap& cmap) { |
134 | 133 |
checkConcept<concepts::ReadMap<Key, _Value>, CMap>(); |
135 | 134 |
const typename Parent::Notifier* nf = Parent::notifier(); |
136 | 135 |
Item it; |
137 | 136 |
for (nf->first(it); it != INVALID; nf->next(it)) { |
138 | 137 |
set(it, cmap[it]); |
139 | 138 |
} |
140 | 139 |
return *this; |
141 | 140 |
} |
142 | 141 |
|
143 | 142 |
public: |
144 | 143 |
|
145 | 144 |
/// \brief The subcript operator. |
146 | 145 |
/// |
147 | 146 |
/// The subscript operator. The map can be subscripted by the |
148 | 147 |
/// actual items of the graph. |
149 | 148 |
Reference operator[](const Key& key) { |
150 | 149 |
return container[Parent::notifier()->id(key)]; |
151 | 150 |
} |
152 | 151 |
|
153 | 152 |
/// \brief The const subcript operator. |
154 | 153 |
/// |
155 | 154 |
/// The const subscript operator. The map can be subscripted by the |
156 | 155 |
/// actual items of the graph. |
157 | 156 |
ConstReference operator[](const Key& key) const { |
158 | 157 |
return container[Parent::notifier()->id(key)]; |
159 | 158 |
} |
160 | 159 |
|
161 | 160 |
|
162 | 161 |
/// \brief The setter function of the map. |
163 | 162 |
/// |
164 | 163 |
/// It the same as operator[](key) = value expression. |
165 | 164 |
void set(const Key& key, const Value& value) { |
166 | 165 |
(*this)[key] = value; |
167 | 166 |
} |
168 | 167 |
|
169 | 168 |
protected: |
170 | 169 |
|
171 | 170 |
/// \brief Adds a new key to the map. |
172 | 171 |
/// |
173 | 172 |
/// It adds a new key to the map. It called by the observer notifier |
174 | 173 |
/// and it overrides the add() member function of the observer base. |
175 | 174 |
virtual void add(const Key& key) { |
176 | 175 |
int id = Parent::notifier()->id(key); |
177 | 176 |
if (id >= int(container.size())) { |
178 | 177 |
container.resize(id + 1); |
179 | 178 |
} |
180 | 179 |
} |
181 | 180 |
|
182 | 181 |
/// \brief Adds more new keys to the map. |
183 | 182 |
/// |
184 | 183 |
/// It adds more new keys to the map. It called by the observer notifier |
185 | 184 |
/// and it overrides the add() member function of the observer base. |
186 | 185 |
virtual void add(const std::vector<Key>& keys) { |
187 | 186 |
int max = container.size() - 1; |
188 | 187 |
for (int i = 0; i < int(keys.size()); ++i) { |
189 | 188 |
int id = Parent::notifier()->id(keys[i]); |
190 | 189 |
if (id >= max) { |
191 | 190 |
max = id; |
192 | 191 |
} |
193 | 192 |
} |
194 | 193 |
container.resize(max + 1); |
195 | 194 |
} |
196 | 195 |
|
197 | 196 |
/// \brief Erase a key from the map. |
198 | 197 |
/// |
199 | 198 |
/// Erase a key from the map. It called by the observer notifier |
200 | 199 |
/// and it overrides the erase() member function of the observer base. |
201 | 200 |
virtual void erase(const Key& key) { |
202 | 201 |
container[Parent::notifier()->id(key)] = Value(); |
203 | 202 |
} |
204 | 203 |
|
205 | 204 |
/// \brief Erase more keys from the map. |
206 | 205 |
/// |
207 | 206 |
/// Erase more keys from the map. It called by the observer notifier |
208 | 207 |
/// and it overrides the erase() member function of the observer base. |
209 | 208 |
virtual void erase(const std::vector<Key>& keys) { |
210 | 209 |
for (int i = 0; i < int(keys.size()); ++i) { |
211 | 210 |
container[Parent::notifier()->id(keys[i])] = Value(); |
212 | 211 |
} |
213 | 212 |
} |
214 | 213 |
|
215 | 214 |
/// \brief Buildes the map. |
216 | 215 |
/// |
217 | 216 |
/// It buildes the map. It called by the observer notifier |
218 | 217 |
/// and it overrides the build() member function of the observer base. |
219 | 218 |
virtual void build() { |
220 | 219 |
int size = Parent::notifier()->maxId() + 1; |
221 | 220 |
container.reserve(size); |
222 | 221 |
container.resize(size); |
223 | 222 |
} |
224 | 223 |
|
225 | 224 |
/// \brief Clear the map. |
226 | 225 |
/// |
227 | 226 |
/// It erase all items from the map. It called by the observer notifier |
228 | 227 |
/// and it overrides the clear() member function of the observer base. |
229 | 228 |
virtual void clear() { |
230 | 229 |
container.clear(); |
231 | 230 |
} |
232 | 231 |
|
233 | 232 |
private: |
234 | 233 |
|
235 | 234 |
Container container; |
236 | 235 |
|
237 | 236 |
}; |
238 | 237 |
|
239 | 238 |
} |
240 | 239 |
|
241 | 240 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-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 |
// This file contains a modified version of the concept checking |
20 | 20 |
// utility from BOOST. |
21 | 21 |
// See the appropriate copyright notice below. |
22 | 22 |
|
23 | 23 |
// (C) Copyright Jeremy Siek 2000. |
24 | 24 |
// Distributed under the Boost Software License, Version 1.0. (See |
25 | 25 |
// accompanying file LICENSE_1_0.txt or copy at |
26 | 26 |
// http://www.boost.org/LICENSE_1_0.txt) |
27 | 27 |
// |
28 | 28 |
// Revision History: |
29 | 29 |
// 05 May 2001: Workarounds for HP aCC from Thomas Matelich. (Jeremy Siek) |
30 | 30 |
// 02 April 2001: Removed limits header altogether. (Jeremy Siek) |
31 | 31 |
// 01 April 2001: Modified to use new <boost/limits.hpp> header. (JMaddock) |
32 | 32 |
// |
33 | 33 |
|
34 | 34 |
// See http://www.boost.org/libs/concept_check for documentation. |
35 | 35 |
|
36 | 36 |
///\file |
37 | 37 |
///\brief Basic utilities for concept checking. |
38 | 38 |
/// |
39 |
///\todo Are we still using BOOST concept checking utility? |
|
40 |
///Is the BOOST copyright notice necessary? |
|
41 | 39 |
|
42 | 40 |
#ifndef LEMON_CONCEPT_CHECK_H |
43 | 41 |
#define LEMON_CONCEPT_CHECK_H |
44 | 42 |
|
45 | 43 |
namespace lemon { |
46 | 44 |
|
47 | 45 |
/* |
48 | 46 |
"inline" is used for ignore_unused_variable_warning() |
49 | 47 |
and function_requires() to make sure there is no |
50 | 48 |
overtarget with g++. |
51 | 49 |
*/ |
52 | 50 |
|
53 | 51 |
template <class T> inline void ignore_unused_variable_warning(const T&) { } |
54 | 52 |
|
55 | 53 |
///\e |
56 | 54 |
template <class Concept> |
57 | 55 |
inline void function_requires() |
58 | 56 |
{ |
59 | 57 |
#if !defined(NDEBUG) |
60 | 58 |
void (Concept::*x)() = & Concept::constraints; |
61 | 59 |
ignore_unused_variable_warning(x); |
62 | 60 |
#endif |
63 | 61 |
} |
64 | 62 |
|
65 | 63 |
///\e |
66 | 64 |
template <typename Concept, typename Type> |
67 | 65 |
inline void checkConcept() { |
68 | 66 |
#if !defined(NDEBUG) |
69 | 67 |
typedef typename Concept::template Constraints<Type> ConceptCheck; |
70 | 68 |
void (ConceptCheck::*x)() = & ConceptCheck::constraints; |
71 | 69 |
ignore_unused_variable_warning(x); |
72 | 70 |
#endif |
73 | 71 |
} |
74 | 72 |
|
75 | 73 |
} // namespace lemon |
76 | 74 |
|
77 | 75 |
#endif // LEMON_CONCEPT_CHECK_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-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 |
///\todo Iterators have obsolete style |
|
24 | 23 |
|
25 | 24 |
#ifndef LEMON_CONCEPT_PATH_H |
26 | 25 |
#define LEMON_CONCEPT_PATH_H |
27 | 26 |
|
28 | 27 |
#include <lemon/core.h> |
29 | 28 |
#include <lemon/concept_check.h> |
30 | 29 |
|
31 | 30 |
namespace lemon { |
32 | 31 |
namespace concepts { |
33 | 32 |
|
34 | 33 |
/// \addtogroup concept |
35 | 34 |
/// @{ |
36 | 35 |
|
37 | 36 |
/// \brief A skeleton structure for representing directed paths in |
38 | 37 |
/// a digraph. |
39 | 38 |
/// |
40 | 39 |
/// A skeleton structure for representing directed paths in a |
41 | 40 |
/// digraph. |
42 | 41 |
/// \tparam _Digraph The digraph type in which the path is. |
43 | 42 |
/// |
44 | 43 |
/// In a sense, the path can be treated as a list of arcs. The |
45 | 44 |
/// lemon path type stores just this list. As a consequence it |
46 | 45 |
/// cannot enumerate the nodes in the path and the zero length |
47 | 46 |
/// paths cannot store the source. |
48 | 47 |
/// |
49 | 48 |
template <typename _Digraph> |
50 | 49 |
class Path { |
51 | 50 |
public: |
52 | 51 |
|
53 | 52 |
/// Type of the underlying digraph. |
54 | 53 |
typedef _Digraph Digraph; |
55 | 54 |
/// Arc type of the underlying digraph. |
56 | 55 |
typedef typename Digraph::Arc Arc; |
57 | 56 |
|
58 | 57 |
class ArcIt; |
59 | 58 |
|
60 | 59 |
/// \brief Default constructor |
61 | 60 |
Path() {} |
62 | 61 |
|
63 | 62 |
/// \brief Template constructor |
64 | 63 |
template <typename CPath> |
65 | 64 |
Path(const CPath& cpath) {} |
66 | 65 |
|
67 | 66 |
/// \brief Template assigment |
68 | 67 |
template <typename CPath> |
69 | 68 |
Path& operator=(const CPath& cpath) {} |
70 | 69 |
|
71 | 70 |
/// Length of the path ie. the number of arcs in the path. |
72 | 71 |
int length() const { return 0;} |
73 | 72 |
|
74 | 73 |
/// Returns whether the path is empty. |
75 | 74 |
bool empty() const { return true;} |
76 | 75 |
|
77 | 76 |
/// Resets the path to an empty path. |
78 | 77 |
void clear() {} |
79 | 78 |
|
80 | 79 |
/// \brief LEMON style iterator for path arcs |
81 | 80 |
/// |
82 | 81 |
/// This class is used to iterate on the arcs of the paths. |
83 | 82 |
class ArcIt { |
84 | 83 |
public: |
85 | 84 |
/// Default constructor |
86 | 85 |
ArcIt() {} |
87 | 86 |
/// Invalid constructor |
88 | 87 |
ArcIt(Invalid) {} |
89 | 88 |
/// Constructor for first arc |
90 | 89 |
ArcIt(const Path &) {} |
91 | 90 |
|
92 | 91 |
/// Conversion to Arc |
93 | 92 |
operator Arc() const { return INVALID; } |
94 | 93 |
|
95 | 94 |
/// Next arc |
96 | 95 |
ArcIt& operator++() {return *this;} |
97 | 96 |
|
98 | 97 |
/// Comparison operator |
99 | 98 |
bool operator==(const ArcIt&) const {return true;} |
100 | 99 |
/// Comparison operator |
101 | 100 |
bool operator!=(const ArcIt&) const {return true;} |
102 | 101 |
/// Comparison operator |
103 | 102 |
bool operator<(const ArcIt&) const {return false;} |
104 | 103 |
|
105 | 104 |
}; |
106 | 105 |
|
107 | 106 |
template <typename _Path> |
108 | 107 |
struct Constraints { |
109 | 108 |
void constraints() { |
110 | 109 |
Path<Digraph> pc; |
111 | 110 |
_Path p, pp(pc); |
112 | 111 |
int l = p.length(); |
113 | 112 |
int e = p.empty(); |
114 | 113 |
p.clear(); |
115 | 114 |
|
116 | 115 |
p = pc; |
117 | 116 |
|
118 | 117 |
typename _Path::ArcIt id, ii(INVALID), i(p); |
119 | 118 |
|
120 | 119 |
++i; |
121 | 120 |
typename Digraph::Arc ed = i; |
122 | 121 |
|
123 | 122 |
e = (i == ii); |
124 | 123 |
e = (i != ii); |
125 | 124 |
e = (i < ii); |
126 | 125 |
|
127 | 126 |
ignore_unused_variable_warning(l); |
128 | 127 |
ignore_unused_variable_warning(pp); |
129 | 128 |
ignore_unused_variable_warning(e); |
130 | 129 |
ignore_unused_variable_warning(id); |
131 | 130 |
ignore_unused_variable_warning(ii); |
132 | 131 |
ignore_unused_variable_warning(ed); |
133 | 132 |
} |
134 | 133 |
}; |
135 | 134 |
|
136 | 135 |
}; |
137 | 136 |
|
138 | 137 |
namespace _path_bits { |
139 | 138 |
|
140 | 139 |
template <typename _Digraph, typename _Path, typename RevPathTag = void> |
141 | 140 |
struct PathDumperConstraints { |
142 | 141 |
void constraints() { |
143 | 142 |
int l = p.length(); |
144 | 143 |
int e = p.empty(); |
145 | 144 |
|
146 | 145 |
typename _Path::ArcIt id, i(p); |
147 | 146 |
|
148 | 147 |
++i; |
149 | 148 |
typename _Digraph::Arc ed = i; |
150 | 149 |
|
151 | 150 |
e = (i == INVALID); |
152 | 151 |
e = (i != INVALID); |
153 | 152 |
|
154 | 153 |
ignore_unused_variable_warning(l); |
155 | 154 |
ignore_unused_variable_warning(e); |
156 | 155 |
ignore_unused_variable_warning(id); |
157 | 156 |
ignore_unused_variable_warning(ed); |
158 | 157 |
} |
159 | 158 |
_Path& p; |
160 | 159 |
}; |
161 | 160 |
|
162 | 161 |
template <typename _Digraph, typename _Path> |
163 | 162 |
struct PathDumperConstraints< |
164 | 163 |
_Digraph, _Path, |
165 | 164 |
typename enable_if<typename _Path::RevPathTag, void>::type |
166 | 165 |
> { |
167 | 166 |
void constraints() { |
168 | 167 |
int l = p.length(); |
169 | 168 |
int e = p.empty(); |
170 | 169 |
|
171 | 170 |
typename _Path::RevArcIt id, i(p); |
172 | 171 |
|
173 | 172 |
++i; |
174 | 173 |
typename _Digraph::Arc ed = i; |
175 | 174 |
|
176 | 175 |
e = (i == INVALID); |
177 | 176 |
e = (i != INVALID); |
178 | 177 |
|
179 | 178 |
ignore_unused_variable_warning(l); |
180 | 179 |
ignore_unused_variable_warning(e); |
181 | 180 |
ignore_unused_variable_warning(id); |
182 | 181 |
ignore_unused_variable_warning(ed); |
183 | 182 |
} |
184 | 183 |
_Path& p; |
185 | 184 |
}; |
186 | 185 |
|
187 | 186 |
} |
188 | 187 |
|
189 | 188 |
|
190 | 189 |
/// \brief A skeleton structure for path dumpers. |
191 | 190 |
/// |
192 | 191 |
/// A skeleton structure for path dumpers. The path dumpers are |
193 | 192 |
/// the generalization of the paths. The path dumpers can |
194 | 193 |
/// enumerate the arcs of the path wheter in forward or in |
195 | 194 |
/// backward order. In most time these classes are not used |
196 | 195 |
/// directly rather it used to assign a dumped class to a real |
197 | 196 |
/// path type. |
198 | 197 |
/// |
199 | 198 |
/// The main purpose of this concept is that the shortest path |
200 | 199 |
/// algorithms can enumerate easily the arcs in reverse order. |
201 | 200 |
/// If we would like to give back a real path from these |
202 | 201 |
/// algorithms then we should create a temporarly path object. In |
203 | 202 |
/// LEMON such algorithms gives back a path dumper what can |
204 | 203 |
/// assigned to a real path and the dumpers can be implemented as |
205 | 204 |
/// an adaptor class to the predecessor map. |
206 | 205 |
|
207 | 206 |
/// \tparam _Digraph The digraph type in which the path is. |
208 | 207 |
/// |
209 | 208 |
/// The paths can be constructed from any path type by a |
210 | 209 |
/// template constructor or a template assignment operator. |
211 | 210 |
/// |
212 | 211 |
template <typename _Digraph> |
213 | 212 |
class PathDumper { |
214 | 213 |
public: |
215 | 214 |
|
216 | 215 |
/// Type of the underlying digraph. |
217 | 216 |
typedef _Digraph Digraph; |
218 | 217 |
/// Arc type of the underlying digraph. |
219 | 218 |
typedef typename Digraph::Arc Arc; |
220 | 219 |
|
221 | 220 |
/// Length of the path ie. the number of arcs in the path. |
222 | 221 |
int length() const { return 0;} |
223 | 222 |
|
224 | 223 |
/// Returns whether the path is empty. |
225 | 224 |
bool empty() const { return true;} |
226 | 225 |
|
227 | 226 |
/// \brief Forward or reverse dumping |
228 | 227 |
/// |
229 | 228 |
/// If the RevPathTag is defined and true then reverse dumping |
230 | 229 |
/// is provided in the path dumper. In this case instead of the |
231 | 230 |
/// ArcIt the RevArcIt iterator should be implemented in the |
232 | 231 |
/// dumper. |
233 | 232 |
typedef False RevPathTag; |
234 | 233 |
|
235 | 234 |
/// \brief LEMON style iterator for path arcs |
236 | 235 |
/// |
237 | 236 |
/// This class is used to iterate on the arcs of the paths. |
238 | 237 |
class ArcIt { |
239 | 238 |
public: |
240 | 239 |
/// Default constructor |
241 | 240 |
ArcIt() {} |
242 | 241 |
/// Invalid constructor |
243 | 242 |
ArcIt(Invalid) {} |
244 | 243 |
/// Constructor for first arc |
245 | 244 |
ArcIt(const PathDumper&) {} |
246 | 245 |
|
247 | 246 |
/// Conversion to Arc |
248 | 247 |
operator Arc() const { return INVALID; } |
249 | 248 |
|
250 | 249 |
/// Next arc |
251 | 250 |
ArcIt& operator++() {return *this;} |
252 | 251 |
|
253 | 252 |
/// Comparison operator |
254 | 253 |
bool operator==(const ArcIt&) const {return true;} |
255 | 254 |
/// Comparison operator |
256 | 255 |
bool operator!=(const ArcIt&) const {return true;} |
257 | 256 |
/// Comparison operator |
258 | 257 |
bool operator<(const ArcIt&) const {return false;} |
259 | 258 |
|
260 | 259 |
}; |
261 | 260 |
|
262 | 261 |
/// \brief LEMON style iterator for path arcs |
263 | 262 |
/// |
264 | 263 |
/// This class is used to iterate on the arcs of the paths in |
265 | 264 |
/// reverse direction. |
266 | 265 |
class RevArcIt { |
267 | 266 |
public: |
268 | 267 |
/// Default constructor |
269 | 268 |
RevArcIt() {} |
270 | 269 |
/// Invalid constructor |
271 | 270 |
RevArcIt(Invalid) {} |
272 | 271 |
/// Constructor for first arc |
273 | 272 |
RevArcIt(const PathDumper &) {} |
274 | 273 |
|
275 | 274 |
/// Conversion to Arc |
276 | 275 |
operator Arc() const { return INVALID; } |
277 | 276 |
|
278 | 277 |
/// Next arc |
279 | 278 |
RevArcIt& operator++() {return *this;} |
280 | 279 |
|
281 | 280 |
/// Comparison operator |
282 | 281 |
bool operator==(const RevArcIt&) const {return true;} |
283 | 282 |
/// Comparison operator |
284 | 283 |
bool operator!=(const RevArcIt&) const {return true;} |
285 | 284 |
/// Comparison operator |
286 | 285 |
bool operator<(const RevArcIt&) const {return false;} |
287 | 286 |
|
288 | 287 |
}; |
289 | 288 |
|
290 | 289 |
template <typename _Path> |
291 | 290 |
struct Constraints { |
292 | 291 |
void constraints() { |
293 | 292 |
function_requires<_path_bits:: |
294 | 293 |
PathDumperConstraints<Digraph, _Path> >(); |
295 | 294 |
} |
296 | 295 |
}; |
297 | 296 |
|
298 | 297 |
}; |
299 | 298 |
|
300 | 299 |
|
301 | 300 |
///@} |
302 | 301 |
} |
303 | 302 |
|
304 | 303 |
} // namespace lemon |
305 | 304 |
|
306 | 305 |
#endif // LEMON_CONCEPT_PATH_H |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-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/bits/path_dump.h> |
28 | 28 |
#include <lemon/core.h> |
29 | 29 |
#include <lemon/error.h> |
30 | 30 |
#include <lemon/assert.h> |
31 | 31 |
#include <lemon/maps.h> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
///Default traits class of Dfs class. |
36 | 36 |
|
37 | 37 |
///Default traits class of Dfs class. |
38 | 38 |
///\tparam GR Digraph type. |
39 | 39 |
template<class GR> |
40 | 40 |
struct DfsDefaultTraits |
41 | 41 |
{ |
42 | 42 |
///The type of the digraph the algorithm runs on. |
43 | 43 |
typedef GR Digraph; |
44 | 44 |
|
45 | 45 |
///\brief The type of the map that stores the predecessor |
46 | 46 |
///arcs of the %DFS paths. |
47 | 47 |
/// |
48 | 48 |
///The type of the map that stores the predecessor |
49 | 49 |
///arcs of the %DFS paths. |
50 | 50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
51 | 51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
52 | 52 |
///Instantiates a \ref PredMap. |
53 | 53 |
|
54 | 54 |
///This function instantiates a \ref PredMap. |
55 | 55 |
///\param g is the digraph, to which we would like to define the |
56 | 56 |
///\ref PredMap. |
57 |
///\todo The digraph alone may be insufficient to initialize |
|
58 | 57 |
static PredMap *createPredMap(const Digraph &g) |
59 | 58 |
{ |
60 | 59 |
return new PredMap(g); |
61 | 60 |
} |
62 | 61 |
|
63 | 62 |
///The type of the map that indicates which nodes are processed. |
64 | 63 |
|
65 | 64 |
///The type of the map that indicates which nodes are processed. |
66 | 65 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
67 |
///By default it is a NullMap. |
|
68 | 66 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
69 | 67 |
///Instantiates a \ref ProcessedMap. |
70 | 68 |
|
71 | 69 |
///This function instantiates a \ref ProcessedMap. |
72 | 70 |
///\param g is the digraph, to which |
73 | 71 |
///we would like to define the \ref ProcessedMap |
74 | 72 |
#ifdef DOXYGEN |
75 | 73 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
76 | 74 |
#else |
77 | 75 |
static ProcessedMap *createProcessedMap(const Digraph &) |
78 | 76 |
#endif |
79 | 77 |
{ |
80 | 78 |
return new ProcessedMap(); |
81 | 79 |
} |
82 | 80 |
|
83 | 81 |
///The type of the map that indicates which nodes are reached. |
84 | 82 |
|
85 | 83 |
///The type of the map that indicates which nodes are reached. |
86 | 84 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
87 | 85 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
88 | 86 |
///Instantiates a \ref ReachedMap. |
89 | 87 |
|
90 | 88 |
///This function instantiates a \ref ReachedMap. |
91 | 89 |
///\param g is the digraph, to which |
92 | 90 |
///we would like to define the \ref ReachedMap. |
93 | 91 |
static ReachedMap *createReachedMap(const Digraph &g) |
94 | 92 |
{ |
95 | 93 |
return new ReachedMap(g); |
96 | 94 |
} |
97 | 95 |
|
98 | 96 |
///The type of the map that stores the distances of the nodes. |
99 | 97 |
|
100 | 98 |
///The type of the map that stores the distances of the nodes. |
101 | 99 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
102 | 100 |
typedef typename Digraph::template NodeMap<int> DistMap; |
103 | 101 |
///Instantiates a \ref DistMap. |
104 | 102 |
|
105 | 103 |
///This function instantiates a \ref DistMap. |
106 | 104 |
///\param g is the digraph, to which we would like to define the |
107 | 105 |
///\ref DistMap. |
108 | 106 |
static DistMap *createDistMap(const Digraph &g) |
109 | 107 |
{ |
110 | 108 |
return new DistMap(g); |
111 | 109 |
} |
112 | 110 |
}; |
113 | 111 |
|
114 | 112 |
///%DFS algorithm class. |
115 | 113 |
|
116 | 114 |
///\ingroup search |
117 | 115 |
///This class provides an efficient implementation of the %DFS algorithm. |
118 | 116 |
/// |
119 | 117 |
///There is also a \ref dfs() "function type interface" for the DFS |
120 | 118 |
///algorithm, which is convenient in the simplier cases and it can be |
121 | 119 |
///used easier. |
122 | 120 |
/// |
123 | 121 |
///\tparam GR The type of the digraph the algorithm runs on. |
124 | 122 |
///The default value is \ref ListDigraph. The value of GR is not used |
125 | 123 |
///directly by \ref Dfs, it is only passed to \ref DfsDefaultTraits. |
126 | 124 |
///\tparam TR Traits class to set various data types used by the algorithm. |
127 | 125 |
///The default traits class is |
128 | 126 |
///\ref DfsDefaultTraits "DfsDefaultTraits<GR>". |
129 | 127 |
///See \ref DfsDefaultTraits for the documentation of |
130 | 128 |
///a Dfs traits class. |
131 | 129 |
#ifdef DOXYGEN |
132 | 130 |
template <typename GR, |
133 | 131 |
typename TR> |
134 | 132 |
#else |
135 | 133 |
template <typename GR=ListDigraph, |
136 | 134 |
typename TR=DfsDefaultTraits<GR> > |
137 | 135 |
#endif |
138 | 136 |
class Dfs { |
139 | 137 |
public: |
140 | 138 |
///\ref Exception for uninitialized parameters. |
141 | 139 |
|
142 | 140 |
///This error represents problems in the initialization of the |
143 | 141 |
///parameters of the algorithm. |
144 | 142 |
class UninitializedParameter : public lemon::UninitializedParameter { |
145 | 143 |
public: |
146 | 144 |
virtual const char* what() const throw() { |
147 | 145 |
return "lemon::Dfs::UninitializedParameter"; |
148 | 146 |
} |
149 | 147 |
}; |
150 | 148 |
|
151 | 149 |
///The type of the digraph the algorithm runs on. |
152 | 150 |
typedef typename TR::Digraph Digraph; |
153 | 151 |
|
154 | 152 |
///\brief The type of the map that stores the predecessor arcs of the |
155 | 153 |
///DFS paths. |
156 | 154 |
typedef typename TR::PredMap PredMap; |
157 | 155 |
///The type of the map that stores the distances of the nodes. |
158 | 156 |
typedef typename TR::DistMap DistMap; |
159 | 157 |
///The type of the map that indicates which nodes are reached. |
160 | 158 |
typedef typename TR::ReachedMap ReachedMap; |
161 | 159 |
///The type of the map that indicates which nodes are processed. |
162 | 160 |
typedef typename TR::ProcessedMap ProcessedMap; |
163 | 161 |
///The type of the paths. |
164 | 162 |
typedef PredMapPath<Digraph, PredMap> Path; |
165 | 163 |
|
166 | 164 |
///The traits class. |
167 | 165 |
typedef TR Traits; |
168 | 166 |
|
169 | 167 |
private: |
170 | 168 |
|
171 | 169 |
typedef typename Digraph::Node Node; |
172 | 170 |
typedef typename Digraph::NodeIt NodeIt; |
173 | 171 |
typedef typename Digraph::Arc Arc; |
174 | 172 |
typedef typename Digraph::OutArcIt OutArcIt; |
175 | 173 |
|
176 | 174 |
//Pointer to the underlying digraph. |
177 | 175 |
const Digraph *G; |
178 | 176 |
//Pointer to the map of predecessor arcs. |
179 | 177 |
PredMap *_pred; |
180 | 178 |
//Indicates if _pred is locally allocated (true) or not. |
181 | 179 |
bool local_pred; |
182 | 180 |
//Pointer to the map of distances. |
183 | 181 |
DistMap *_dist; |
184 | 182 |
//Indicates if _dist is locally allocated (true) or not. |
185 | 183 |
bool local_dist; |
186 | 184 |
//Pointer to the map of reached status of the nodes. |
187 | 185 |
ReachedMap *_reached; |
188 | 186 |
//Indicates if _reached is locally allocated (true) or not. |
189 | 187 |
bool local_reached; |
190 | 188 |
//Pointer to the map of processed status of the nodes. |
191 | 189 |
ProcessedMap *_processed; |
192 | 190 |
//Indicates if _processed is locally allocated (true) or not. |
193 | 191 |
bool local_processed; |
194 | 192 |
|
195 | 193 |
std::vector<typename Digraph::OutArcIt> _stack; |
196 | 194 |
int _stack_head; |
197 | 195 |
|
198 |
///Creates the maps if necessary. |
|
199 |
///\todo Better memory allocation (instead of new). |
|
196 |
//Creates the maps if necessary. |
|
200 | 197 |
void create_maps() |
201 | 198 |
{ |
202 | 199 |
if(!_pred) { |
203 | 200 |
local_pred = true; |
204 | 201 |
_pred = Traits::createPredMap(*G); |
205 | 202 |
} |
206 | 203 |
if(!_dist) { |
207 | 204 |
local_dist = true; |
208 | 205 |
_dist = Traits::createDistMap(*G); |
209 | 206 |
} |
210 | 207 |
if(!_reached) { |
211 | 208 |
local_reached = true; |
212 | 209 |
_reached = Traits::createReachedMap(*G); |
213 | 210 |
} |
214 | 211 |
if(!_processed) { |
215 | 212 |
local_processed = true; |
216 | 213 |
_processed = Traits::createProcessedMap(*G); |
217 | 214 |
} |
218 | 215 |
} |
219 | 216 |
|
220 | 217 |
protected: |
221 | 218 |
|
222 | 219 |
Dfs() {} |
223 | 220 |
|
224 | 221 |
public: |
225 | 222 |
|
226 | 223 |
typedef Dfs Create; |
227 | 224 |
|
228 | 225 |
///\name Named template parameters |
229 | 226 |
|
230 | 227 |
///@{ |
231 | 228 |
|
232 | 229 |
template <class T> |
233 | 230 |
struct SetPredMapTraits : public Traits { |
234 | 231 |
typedef T PredMap; |
235 | 232 |
static PredMap *createPredMap(const Digraph &) |
236 | 233 |
{ |
237 | 234 |
throw UninitializedParameter(); |
238 | 235 |
} |
239 | 236 |
}; |
240 | 237 |
///\brief \ref named-templ-param "Named parameter" for setting |
241 | 238 |
///\ref PredMap type. |
242 | 239 |
/// |
243 | 240 |
///\ref named-templ-param "Named parameter" for setting |
244 | 241 |
///\ref PredMap type. |
245 | 242 |
template <class T> |
246 | 243 |
struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > { |
247 | 244 |
typedef Dfs<Digraph, SetPredMapTraits<T> > Create; |
248 | 245 |
}; |
249 | 246 |
|
250 | 247 |
template <class T> |
251 | 248 |
struct SetDistMapTraits : public Traits { |
252 | 249 |
typedef T DistMap; |
253 | 250 |
static DistMap *createDistMap(const Digraph &) |
254 | 251 |
{ |
255 | 252 |
throw UninitializedParameter(); |
256 | 253 |
} |
257 | 254 |
}; |
258 | 255 |
///\brief \ref named-templ-param "Named parameter" for setting |
259 | 256 |
///\ref DistMap type. |
260 | 257 |
/// |
261 | 258 |
///\ref named-templ-param "Named parameter" for setting |
262 | 259 |
///\ref DistMap type. |
263 | 260 |
template <class T> |
264 | 261 |
struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > { |
265 | 262 |
typedef Dfs<Digraph, SetDistMapTraits<T> > Create; |
266 | 263 |
}; |
267 | 264 |
|
268 | 265 |
template <class T> |
269 | 266 |
struct SetReachedMapTraits : public Traits { |
270 | 267 |
typedef T ReachedMap; |
271 | 268 |
static ReachedMap *createReachedMap(const Digraph &) |
272 | 269 |
{ |
273 | 270 |
throw UninitializedParameter(); |
274 | 271 |
} |
275 | 272 |
}; |
276 | 273 |
///\brief \ref named-templ-param "Named parameter" for setting |
277 | 274 |
///\ref ReachedMap type. |
278 | 275 |
/// |
279 | 276 |
///\ref named-templ-param "Named parameter" for setting |
280 | 277 |
///\ref ReachedMap type. |
281 | 278 |
template <class T> |
282 | 279 |
struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > { |
283 | 280 |
typedef Dfs< Digraph, SetReachedMapTraits<T> > Create; |
284 | 281 |
}; |
285 | 282 |
|
286 | 283 |
template <class T> |
287 | 284 |
struct SetProcessedMapTraits : public Traits { |
288 | 285 |
typedef T ProcessedMap; |
289 | 286 |
static ProcessedMap *createProcessedMap(const Digraph &) |
290 | 287 |
{ |
291 | 288 |
throw UninitializedParameter(); |
292 | 289 |
} |
293 | 290 |
}; |
294 | 291 |
///\brief \ref named-templ-param "Named parameter" for setting |
295 | 292 |
///\ref ProcessedMap type. |
296 | 293 |
/// |
297 | 294 |
///\ref named-templ-param "Named parameter" for setting |
298 | 295 |
///\ref ProcessedMap type. |
299 | 296 |
template <class T> |
300 | 297 |
struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > { |
301 | 298 |
typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create; |
302 | 299 |
}; |
303 | 300 |
|
304 | 301 |
struct SetStandardProcessedMapTraits : public Traits { |
305 | 302 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
306 | 303 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
307 | 304 |
{ |
308 | 305 |
return new ProcessedMap(g); |
309 | 306 |
} |
310 | 307 |
}; |
311 | 308 |
///\brief \ref named-templ-param "Named parameter" for setting |
312 | 309 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
313 | 310 |
/// |
314 | 311 |
///\ref named-templ-param "Named parameter" for setting |
315 | 312 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
316 | 313 |
///If you don't set it explicitly, it will be automatically allocated. |
317 | 314 |
struct SetStandardProcessedMap : |
318 | 315 |
public Dfs< Digraph, SetStandardProcessedMapTraits > { |
319 | 316 |
typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create; |
320 | 317 |
}; |
321 | 318 |
|
322 | 319 |
///@} |
323 | 320 |
|
324 | 321 |
public: |
325 | 322 |
|
326 | 323 |
///Constructor. |
327 | 324 |
|
328 | 325 |
///Constructor. |
329 | 326 |
///\param g The digraph the algorithm runs on. |
330 | 327 |
Dfs(const Digraph &g) : |
331 | 328 |
G(&g), |
332 | 329 |
_pred(NULL), local_pred(false), |
333 | 330 |
_dist(NULL), local_dist(false), |
334 | 331 |
_reached(NULL), local_reached(false), |
335 | 332 |
_processed(NULL), local_processed(false) |
336 | 333 |
{ } |
337 | 334 |
|
338 | 335 |
///Destructor. |
339 | 336 |
~Dfs() |
340 | 337 |
{ |
341 | 338 |
if(local_pred) delete _pred; |
342 | 339 |
if(local_dist) delete _dist; |
343 | 340 |
if(local_reached) delete _reached; |
344 | 341 |
if(local_processed) delete _processed; |
345 | 342 |
} |
346 | 343 |
|
347 | 344 |
///Sets the map that stores the predecessor arcs. |
348 | 345 |
|
349 | 346 |
///Sets the map that stores the predecessor arcs. |
350 | 347 |
///If you don't use this function before calling \ref run(), |
351 | 348 |
///it will allocate one. The destructor deallocates this |
352 | 349 |
///automatically allocated map, of course. |
353 | 350 |
///\return <tt> (*this) </tt> |
354 | 351 |
Dfs &predMap(PredMap &m) |
355 | 352 |
{ |
356 | 353 |
if(local_pred) { |
357 | 354 |
delete _pred; |
358 | 355 |
local_pred=false; |
359 | 356 |
} |
360 | 357 |
_pred = &m; |
361 | 358 |
return *this; |
362 | 359 |
} |
363 | 360 |
|
364 | 361 |
///Sets the map that indicates which nodes are reached. |
365 | 362 |
|
366 | 363 |
///Sets the map that indicates which nodes are reached. |
367 | 364 |
///If you don't use this function before calling \ref run(), |
368 | 365 |
///it will allocate one. The destructor deallocates this |
369 | 366 |
///automatically allocated map, of course. |
370 | 367 |
///\return <tt> (*this) </tt> |
371 | 368 |
Dfs &reachedMap(ReachedMap &m) |
372 | 369 |
{ |
373 | 370 |
if(local_reached) { |
374 | 371 |
delete _reached; |
375 | 372 |
local_reached=false; |
376 | 373 |
} |
377 | 374 |
_reached = &m; |
378 | 375 |
return *this; |
379 | 376 |
} |
380 | 377 |
|
381 | 378 |
///Sets the map that indicates which nodes are processed. |
382 | 379 |
|
383 | 380 |
///Sets the map that indicates which nodes are processed. |
384 | 381 |
///If you don't use this function before calling \ref run(), |
385 | 382 |
///it will allocate one. The destructor deallocates this |
386 | 383 |
///automatically allocated map, of course. |
387 | 384 |
///\return <tt> (*this) </tt> |
388 | 385 |
Dfs &processedMap(ProcessedMap &m) |
389 | 386 |
{ |
390 | 387 |
if(local_processed) { |
391 | 388 |
delete _processed; |
392 | 389 |
local_processed=false; |
393 | 390 |
} |
394 | 391 |
_processed = &m; |
395 | 392 |
return *this; |
396 | 393 |
} |
397 | 394 |
|
398 | 395 |
///Sets the map that stores the distances of the nodes. |
399 | 396 |
|
400 | 397 |
///Sets the map that stores the distances of the nodes calculated by |
401 | 398 |
///the algorithm. |
402 | 399 |
///If you don't use this function before calling \ref run(), |
403 | 400 |
///it will allocate one. The destructor deallocates this |
404 | 401 |
///automatically allocated map, of course. |
405 | 402 |
///\return <tt> (*this) </tt> |
406 | 403 |
Dfs &distMap(DistMap &m) |
407 | 404 |
{ |
408 | 405 |
if(local_dist) { |
409 | 406 |
delete _dist; |
410 | 407 |
local_dist=false; |
411 | 408 |
} |
412 | 409 |
_dist = &m; |
413 | 410 |
return *this; |
414 | 411 |
} |
415 | 412 |
|
416 | 413 |
public: |
417 | 414 |
|
418 | 415 |
///\name Execution control |
419 | 416 |
///The simplest way to execute the algorithm is to use |
420 | 417 |
///one of the member functions called \ref lemon::Dfs::run() "run()". |
421 | 418 |
///\n |
422 | 419 |
///If you need more control on the execution, first you must call |
423 | 420 |
///\ref lemon::Dfs::init() "init()", then you can add a source node |
424 | 421 |
///with \ref lemon::Dfs::addSource() "addSource()". |
425 | 422 |
///Finally \ref lemon::Dfs::start() "start()" will perform the |
426 | 423 |
///actual path computation. |
427 | 424 |
|
428 | 425 |
///@{ |
429 | 426 |
|
430 | 427 |
///Initializes the internal data structures. |
431 | 428 |
|
432 | 429 |
///Initializes the internal data structures. |
433 | 430 |
/// |
434 | 431 |
void init() |
435 | 432 |
{ |
436 | 433 |
create_maps(); |
437 | 434 |
_stack.resize(countNodes(*G)); |
438 | 435 |
_stack_head=-1; |
439 | 436 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
440 | 437 |
_pred->set(u,INVALID); |
441 | 438 |
_reached->set(u,false); |
442 | 439 |
_processed->set(u,false); |
443 | 440 |
} |
444 | 441 |
} |
445 | 442 |
|
446 | 443 |
///Adds a new source node. |
447 | 444 |
|
448 | 445 |
///Adds a new source node to the set of nodes to be processed. |
449 | 446 |
/// |
450 | 447 |
///\pre The stack must be empty. (Otherwise the algorithm gives |
451 | 448 |
///false results.) |
452 | 449 |
/// |
453 | 450 |
///\warning Distances will be wrong (or at least strange) in case of |
454 | 451 |
///multiple sources. |
455 | 452 |
void addSource(Node s) |
456 | 453 |
{ |
457 | 454 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
458 | 455 |
if(!(*_reached)[s]) |
459 | 456 |
{ |
460 | 457 |
_reached->set(s,true); |
461 | 458 |
_pred->set(s,INVALID); |
462 | 459 |
OutArcIt e(*G,s); |
463 | 460 |
if(e!=INVALID) { |
464 | 461 |
_stack[++_stack_head]=e; |
465 | 462 |
_dist->set(s,_stack_head); |
466 | 463 |
} |
467 | 464 |
else { |
468 | 465 |
_processed->set(s,true); |
469 | 466 |
_dist->set(s,0); |
470 | 467 |
} |
471 | 468 |
} |
472 | 469 |
} |
473 | 470 |
|
474 | 471 |
///Processes the next arc. |
475 | 472 |
|
476 | 473 |
///Processes the next arc. |
477 | 474 |
/// |
478 | 475 |
///\return The processed arc. |
479 | 476 |
/// |
480 | 477 |
///\pre The stack must not be empty. |
481 | 478 |
Arc processNextArc() |
482 | 479 |
{ |
483 | 480 |
Node m; |
484 | 481 |
Arc e=_stack[_stack_head]; |
485 | 482 |
if(!(*_reached)[m=G->target(e)]) { |
486 | 483 |
_pred->set(m,e); |
487 | 484 |
_reached->set(m,true); |
488 | 485 |
++_stack_head; |
489 | 486 |
_stack[_stack_head] = OutArcIt(*G, m); |
490 | 487 |
_dist->set(m,_stack_head); |
491 | 488 |
} |
492 | 489 |
else { |
493 | 490 |
m=G->source(e); |
494 | 491 |
++_stack[_stack_head]; |
495 | 492 |
} |
496 | 493 |
while(_stack_head>=0 && _stack[_stack_head]==INVALID) { |
497 | 494 |
_processed->set(m,true); |
498 | 495 |
--_stack_head; |
499 | 496 |
if(_stack_head>=0) { |
500 | 497 |
m=G->source(_stack[_stack_head]); |
501 | 498 |
++_stack[_stack_head]; |
502 | 499 |
} |
503 | 500 |
} |
504 | 501 |
return e; |
505 | 502 |
} |
506 | 503 |
|
507 | 504 |
///Next arc to be processed. |
508 | 505 |
|
509 | 506 |
///Next arc to be processed. |
510 | 507 |
/// |
511 | 508 |
///\return The next arc to be processed or \c INVALID if the stack |
512 | 509 |
///is empty. |
513 | 510 |
OutArcIt nextArc() const |
514 | 511 |
{ |
515 | 512 |
return _stack_head>=0?_stack[_stack_head]:INVALID; |
516 | 513 |
} |
517 | 514 |
|
518 | 515 |
///\brief Returns \c false if there are nodes |
519 | 516 |
///to be processed. |
520 | 517 |
/// |
521 | 518 |
///Returns \c false if there are nodes |
522 | 519 |
///to be processed in the queue (stack). |
523 | 520 |
bool emptyQueue() const { return _stack_head<0; } |
524 | 521 |
|
525 | 522 |
///Returns the number of the nodes to be processed. |
526 | 523 |
|
527 | 524 |
///Returns the number of the nodes to be processed in the queue (stack). |
528 | 525 |
int queueSize() const { return _stack_head+1; } |
529 | 526 |
|
530 | 527 |
///Executes the algorithm. |
531 | 528 |
|
532 | 529 |
///Executes the algorithm. |
533 | 530 |
/// |
534 | 531 |
///This method runs the %DFS algorithm from the root node |
535 | 532 |
///in order to compute the DFS path to each node. |
536 | 533 |
/// |
537 | 534 |
/// The algorithm computes |
538 | 535 |
///- the %DFS tree, |
539 | 536 |
///- the distance of each node from the root in the %DFS tree. |
540 | 537 |
/// |
541 | 538 |
///\pre init() must be called and a root node should be |
542 | 539 |
///added with addSource() before using this function. |
543 | 540 |
/// |
544 | 541 |
///\note <tt>d.start()</tt> is just a shortcut of the following code. |
545 | 542 |
///\code |
546 | 543 |
/// while ( !d.emptyQueue() ) { |
547 | 544 |
/// d.processNextArc(); |
548 | 545 |
/// } |
549 | 546 |
///\endcode |
550 | 547 |
void start() |
551 | 548 |
{ |
552 | 549 |
while ( !emptyQueue() ) processNextArc(); |
553 | 550 |
} |
554 | 551 |
|
555 | 552 |
///Executes the algorithm until the given target node is reached. |
556 | 553 |
|
557 | 554 |
///Executes the algorithm until the given target node is reached. |
558 | 555 |
/// |
559 | 556 |
///This method runs the %DFS algorithm from the root node |
560 | 557 |
///in order to compute the DFS path to \c dest. |
561 | 558 |
/// |
562 | 559 |
///The algorithm computes |
563 | 560 |
///- the %DFS path to \c dest, |
564 | 561 |
///- the distance of \c dest from the root in the %DFS tree. |
565 | 562 |
/// |
566 | 563 |
///\pre init() must be called and a root node should be |
567 | 564 |
///added with addSource() before using this function. |
568 | 565 |
void start(Node dest) |
569 | 566 |
{ |
570 | 567 |
while ( !emptyQueue() && G->target(_stack[_stack_head])!=dest ) |
571 | 568 |
processNextArc(); |
572 | 569 |
} |
573 | 570 |
|
574 | 571 |
///Executes the algorithm until a condition is met. |
575 | 572 |
|
576 | 573 |
///Executes the algorithm until a condition is met. |
577 | 574 |
/// |
578 | 575 |
///This method runs the %DFS algorithm from the root node |
579 | 576 |
///until an arc \c a with <tt>am[a]</tt> true is found. |
580 | 577 |
/// |
581 | 578 |
///\param am A \c bool (or convertible) arc map. The algorithm |
582 | 579 |
///will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
583 | 580 |
/// |
584 | 581 |
///\return The reached arc \c a with <tt>am[a]</tt> true or |
585 | 582 |
///\c INVALID if no such arc was found. |
586 | 583 |
/// |
587 | 584 |
///\pre init() must be called and a root node should be |
588 | 585 |
///added with addSource() before using this function. |
589 | 586 |
/// |
590 | 587 |
///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
591 | 588 |
///not a node map. |
592 | 589 |
template<class ArcBoolMap> |
593 | 590 |
Arc start(const ArcBoolMap &am) |
594 | 591 |
{ |
595 | 592 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
596 | 593 |
processNextArc(); |
597 | 594 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
598 | 595 |
} |
599 | 596 |
|
600 | 597 |
///Runs the algorithm from the given node. |
601 | 598 |
|
602 | 599 |
///This method runs the %DFS algorithm from node \c s |
603 | 600 |
///in order to compute the DFS path to each node. |
604 | 601 |
/// |
605 | 602 |
///The algorithm computes |
606 | 603 |
///- the %DFS tree, |
607 | 604 |
///- the distance of each node from the root in the %DFS tree. |
608 | 605 |
/// |
609 | 606 |
///\note <tt>d.run(s)</tt> is just a shortcut of the following code. |
610 | 607 |
///\code |
611 | 608 |
/// d.init(); |
612 | 609 |
/// d.addSource(s); |
613 | 610 |
/// d.start(); |
614 | 611 |
///\endcode |
615 | 612 |
void run(Node s) { |
616 | 613 |
init(); |
617 | 614 |
addSource(s); |
618 | 615 |
start(); |
619 | 616 |
} |
620 | 617 |
|
621 | 618 |
///Finds the %DFS path between \c s and \c t. |
622 | 619 |
|
623 | 620 |
///This method runs the %DFS algorithm from node \c s |
624 | 621 |
///in order to compute the DFS path to \c t. |
625 | 622 |
/// |
626 | 623 |
///\return The length of the <tt>s</tt>--<tt>t</tt> DFS path, |
627 | 624 |
///if \c t is reachable form \c s, \c 0 otherwise. |
628 | 625 |
/// |
629 | 626 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is |
630 | 627 |
///just a shortcut of the following code. |
631 | 628 |
///\code |
632 | 629 |
/// d.init(); |
633 | 630 |
/// d.addSource(s); |
634 | 631 |
/// d.start(t); |
635 | 632 |
///\endcode |
636 | 633 |
int run(Node s,Node t) { |
637 | 634 |
init(); |
638 | 635 |
addSource(s); |
639 | 636 |
start(t); |
640 | 637 |
return reached(t)?_stack_head+1:0; |
641 | 638 |
} |
642 | 639 |
|
643 | 640 |
///Runs the algorithm to visit all nodes in the digraph. |
644 | 641 |
|
645 | 642 |
///This method runs the %DFS algorithm in order to compute the |
646 | 643 |
///%DFS path to each node. |
647 | 644 |
/// |
648 | 645 |
///The algorithm computes |
649 | 646 |
///- the %DFS tree, |
650 | 647 |
///- the distance of each node from the root in the %DFS tree. |
651 | 648 |
/// |
652 | 649 |
///\note <tt>d.run()</tt> is just a shortcut of the following code. |
653 | 650 |
///\code |
654 | 651 |
/// d.init(); |
655 | 652 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
656 | 653 |
/// if (!d.reached(n)) { |
657 | 654 |
/// d.addSource(n); |
658 | 655 |
/// d.start(); |
659 | 656 |
/// } |
660 | 657 |
/// } |
661 | 658 |
///\endcode |
662 | 659 |
void run() { |
663 | 660 |
init(); |
664 | 661 |
for (NodeIt it(*G); it != INVALID; ++it) { |
665 | 662 |
if (!reached(it)) { |
666 | 663 |
addSource(it); |
667 | 664 |
start(); |
668 | 665 |
} |
669 | 666 |
} |
670 | 667 |
} |
671 | 668 |
|
672 | 669 |
///@} |
673 | 670 |
|
674 | 671 |
///\name Query Functions |
675 | 672 |
///The result of the %DFS algorithm can be obtained using these |
676 | 673 |
///functions.\n |
677 | 674 |
///Either \ref lemon::Dfs::run() "run()" or \ref lemon::Dfs::start() |
678 | 675 |
///"start()" must be called before using them. |
679 | 676 |
|
680 | 677 |
///@{ |
681 | 678 |
|
682 | 679 |
///The DFS path to a node. |
683 | 680 |
|
684 | 681 |
///Returns the DFS path to a node. |
685 | 682 |
/// |
686 | 683 |
///\warning \c t should be reachable from the root. |
687 | 684 |
/// |
688 | 685 |
///\pre Either \ref run() or \ref start() must be called before |
689 | 686 |
///using this function. |
690 | 687 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
691 | 688 |
|
692 | 689 |
///The distance of a node from the root. |
693 | 690 |
|
694 | 691 |
///Returns the distance of a node from the root. |
695 | 692 |
/// |
696 | 693 |
///\warning If node \c v is not reachable from the root, then |
697 | 694 |
///the return value of this function is undefined. |
698 | 695 |
/// |
699 | 696 |
///\pre Either \ref run() or \ref start() must be called before |
700 | 697 |
///using this function. |
701 | 698 |
int dist(Node v) const { return (*_dist)[v]; } |
702 | 699 |
|
703 | 700 |
///Returns the 'previous arc' of the %DFS tree for a node. |
704 | 701 |
|
705 | 702 |
///This function returns the 'previous arc' of the %DFS tree for the |
706 | 703 |
///node \c v, i.e. it returns the last arc of a %DFS path from the |
707 | 704 |
///root to \c v. It is \c INVALID |
708 | 705 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
709 | 706 |
/// |
710 | 707 |
///The %DFS tree used here is equal to the %DFS tree used in |
711 | 708 |
///\ref predNode(). |
712 | 709 |
/// |
713 | 710 |
///\pre Either \ref run() or \ref start() must be called before using |
714 | 711 |
///this function. |
715 | 712 |
Arc predArc(Node v) const { return (*_pred)[v];} |
716 | 713 |
|
717 | 714 |
///Returns the 'previous node' of the %DFS tree. |
718 | 715 |
|
719 | 716 |
///This function returns the 'previous node' of the %DFS |
720 | 717 |
///tree for the node \c v, i.e. it returns the last but one node |
721 | 718 |
///from a %DFS path from the root to \c v. It is \c INVALID |
722 | 719 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
723 | 720 |
/// |
724 | 721 |
///The %DFS tree used here is equal to the %DFS tree used in |
725 | 722 |
///\ref predArc(). |
726 | 723 |
/// |
727 | 724 |
///\pre Either \ref run() or \ref start() must be called before |
728 | 725 |
///using this function. |
729 | 726 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
730 | 727 |
G->source((*_pred)[v]); } |
731 | 728 |
|
732 | 729 |
///\brief Returns a const reference to the node map that stores the |
733 | 730 |
///distances of the nodes. |
734 | 731 |
/// |
735 | 732 |
///Returns a const reference to the node map that stores the |
736 | 733 |
///distances of the nodes calculated by the algorithm. |
737 | 734 |
/// |
738 | 735 |
///\pre Either \ref run() or \ref init() |
739 | 736 |
///must be called before using this function. |
740 | 737 |
const DistMap &distMap() const { return *_dist;} |
741 | 738 |
|
742 | 739 |
///\brief Returns a const reference to the node map that stores the |
743 | 740 |
///predecessor arcs. |
744 | 741 |
/// |
745 | 742 |
///Returns a const reference to the node map that stores the predecessor |
746 | 743 |
///arcs, which form the DFS tree. |
747 | 744 |
/// |
748 | 745 |
///\pre Either \ref run() or \ref init() |
749 | 746 |
///must be called before using this function. |
750 | 747 |
const PredMap &predMap() const { return *_pred;} |
751 | 748 |
|
752 | 749 |
///Checks if a node is reachable from the root(s). |
753 | 750 |
|
754 | 751 |
///Returns \c true if \c v is reachable from the root(s). |
755 | 752 |
///\pre Either \ref run() or \ref start() |
756 | 753 |
///must be called before using this function. |
757 | 754 |
bool reached(Node v) const { return (*_reached)[v]; } |
758 | 755 |
|
759 | 756 |
///@} |
760 | 757 |
}; |
761 | 758 |
|
762 | 759 |
///Default traits class of dfs() function. |
763 | 760 |
|
764 | 761 |
///Default traits class of dfs() function. |
765 | 762 |
///\tparam GR Digraph type. |
766 | 763 |
template<class GR> |
767 | 764 |
struct DfsWizardDefaultTraits |
768 | 765 |
{ |
769 | 766 |
///The type of the digraph the algorithm runs on. |
770 | 767 |
typedef GR Digraph; |
771 | 768 |
|
772 | 769 |
///\brief The type of the map that stores the predecessor |
773 | 770 |
///arcs of the %DFS paths. |
774 | 771 |
/// |
775 | 772 |
///The type of the map that stores the predecessor |
776 | 773 |
///arcs of the %DFS paths. |
777 | 774 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
778 | 775 |
/// |
779 | 776 |
typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap; |
780 | 777 |
///Instantiates a \ref PredMap. |
781 | 778 |
|
782 | 779 |
///This function instantiates a \ref PredMap. |
783 | 780 |
///\param g is the digraph, to which we would like to define the |
784 | 781 |
///\ref PredMap. |
785 |
///\todo The digraph alone may be insufficient to initialize |
|
786 | 782 |
#ifdef DOXYGEN |
787 | 783 |
static PredMap *createPredMap(const Digraph &g) |
788 | 784 |
#else |
789 | 785 |
static PredMap *createPredMap(const Digraph &) |
790 | 786 |
#endif |
791 | 787 |
{ |
792 | 788 |
return new PredMap(); |
793 | 789 |
} |
794 | 790 |
|
795 | 791 |
///The type of the map that indicates which nodes are processed. |
796 | 792 |
|
797 | 793 |
///The type of the map that indicates which nodes are processed. |
798 | 794 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
799 | 795 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
800 | 796 |
///Instantiates a \ref ProcessedMap. |
801 | 797 |
|
802 | 798 |
///This function instantiates a \ref ProcessedMap. |
803 | 799 |
///\param g is the digraph, to which |
804 | 800 |
///we would like to define the \ref ProcessedMap. |
805 | 801 |
#ifdef DOXYGEN |
806 | 802 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
807 | 803 |
#else |
808 | 804 |
static ProcessedMap *createProcessedMap(const Digraph &) |
809 | 805 |
#endif |
810 | 806 |
{ |
811 | 807 |
return new ProcessedMap(); |
812 | 808 |
} |
813 | 809 |
|
814 | 810 |
///The type of the map that indicates which nodes are reached. |
815 | 811 |
|
816 | 812 |
///The type of the map that indicates which nodes are reached. |
817 | 813 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
818 | 814 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
819 | 815 |
///Instantiates a \ref ReachedMap. |
820 | 816 |
|
821 | 817 |
///This function instantiates a \ref ReachedMap. |
822 | 818 |
///\param g is the digraph, to which |
823 | 819 |
///we would like to define the \ref ReachedMap. |
824 | 820 |
static ReachedMap *createReachedMap(const Digraph &g) |
825 | 821 |
{ |
826 | 822 |
return new ReachedMap(g); |
827 | 823 |
} |
828 | 824 |
|
829 | 825 |
///The type of the map that stores the distances of the nodes. |
830 | 826 |
|
831 | 827 |
///The type of the map that stores the distances of the nodes. |
832 | 828 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
833 | 829 |
/// |
834 | 830 |
typedef NullMap<typename Digraph::Node,int> DistMap; |
835 | 831 |
///Instantiates a \ref DistMap. |
836 | 832 |
|
837 | 833 |
///This function instantiates a \ref DistMap. |
838 | 834 |
///\param g is the digraph, to which we would like to define |
839 | 835 |
///the \ref DistMap |
840 | 836 |
#ifdef DOXYGEN |
841 | 837 |
static DistMap *createDistMap(const Digraph &g) |
842 | 838 |
#else |
843 | 839 |
static DistMap *createDistMap(const Digraph &) |
844 | 840 |
#endif |
845 | 841 |
{ |
846 | 842 |
return new DistMap(); |
847 | 843 |
} |
848 | 844 |
}; |
849 | 845 |
|
850 | 846 |
/// Default traits class used by \ref DfsWizard |
851 | 847 |
|
852 | 848 |
/// To make it easier to use Dfs algorithm |
853 | 849 |
/// we have created a wizard class. |
854 | 850 |
/// This \ref DfsWizard class needs default traits, |
855 | 851 |
/// as well as the \ref Dfs class. |
856 | 852 |
/// The \ref DfsWizardBase is a class to be the default traits of the |
857 | 853 |
/// \ref DfsWizard class. |
858 | 854 |
template<class GR> |
859 | 855 |
class DfsWizardBase : public DfsWizardDefaultTraits<GR> |
860 | 856 |
{ |
861 | 857 |
|
862 | 858 |
typedef DfsWizardDefaultTraits<GR> Base; |
863 | 859 |
protected: |
864 | 860 |
//The type of the nodes in the digraph. |
865 | 861 |
typedef typename Base::Digraph::Node Node; |
866 | 862 |
|
867 | 863 |
//Pointer to the digraph the algorithm runs on. |
868 | 864 |
void *_g; |
869 | 865 |
//Pointer to the map of reached nodes. |
870 | 866 |
void *_reached; |
871 | 867 |
//Pointer to the map of processed nodes. |
872 | 868 |
void *_processed; |
873 | 869 |
//Pointer to the map of predecessors arcs. |
874 | 870 |
void *_pred; |
875 | 871 |
//Pointer to the map of distances. |
876 | 872 |
void *_dist; |
877 | 873 |
//Pointer to the source node. |
878 | 874 |
Node _source; |
879 | 875 |
|
880 | 876 |
public: |
881 | 877 |
/// Constructor. |
882 | 878 |
|
883 | 879 |
/// This constructor does not require parameters, therefore it initiates |
884 | 880 |
/// all of the attributes to default values (0, INVALID). |
885 | 881 |
DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
886 | 882 |
_dist(0), _source(INVALID) {} |
887 | 883 |
|
888 | 884 |
/// Constructor. |
889 | 885 |
|
890 | 886 |
/// This constructor requires some parameters, |
891 | 887 |
/// listed in the parameters list. |
892 | 888 |
/// Others are initiated to 0. |
893 | 889 |
/// \param g The digraph the algorithm runs on. |
894 | 890 |
/// \param s The source node. |
895 | 891 |
DfsWizardBase(const GR &g, Node s=INVALID) : |
896 | 892 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
897 | 893 |
_reached(0), _processed(0), _pred(0), _dist(0), _source(s) {} |
898 | 894 |
|
899 | 895 |
}; |
900 | 896 |
|
901 | 897 |
/// Auxiliary class for the function type interface of DFS algorithm. |
902 | 898 |
|
903 | 899 |
/// This auxiliary class is created to implement the function type |
904 | 900 |
/// interface of \ref Dfs algorithm. It uses the functions and features |
905 | 901 |
/// of the plain \ref Dfs, but it is much simpler to use it. |
906 | 902 |
/// It should only be used through the \ref dfs() function, which makes |
907 | 903 |
/// it easier to use the algorithm. |
908 | 904 |
/// |
909 | 905 |
/// Simplicity means that the way to change the types defined |
910 | 906 |
/// in the traits class is based on functions that returns the new class |
911 | 907 |
/// and not on templatable built-in classes. |
912 | 908 |
/// When using the plain \ref Dfs |
913 | 909 |
/// the new class with the modified type comes from |
914 | 910 |
/// the original class by using the :: |
915 | 911 |
/// operator. In the case of \ref DfsWizard only |
916 | 912 |
/// a function have to be called, and it will |
917 | 913 |
/// return the needed class. |
918 | 914 |
/// |
919 | 915 |
/// It does not have own \ref run() method. When its \ref run() method |
920 | 916 |
/// is called, it initiates a plain \ref Dfs object, and calls the |
921 | 917 |
/// \ref Dfs::run() method of it. |
922 | 918 |
template<class TR> |
923 | 919 |
class DfsWizard : public TR |
924 | 920 |
{ |
925 | 921 |
typedef TR Base; |
926 | 922 |
|
927 | 923 |
///The type of the digraph the algorithm runs on. |
928 | 924 |
typedef typename TR::Digraph Digraph; |
929 | 925 |
|
930 | 926 |
typedef typename Digraph::Node Node; |
931 | 927 |
typedef typename Digraph::NodeIt NodeIt; |
932 | 928 |
typedef typename Digraph::Arc Arc; |
933 | 929 |
typedef typename Digraph::OutArcIt OutArcIt; |
934 | 930 |
|
935 | 931 |
///\brief The type of the map that stores the predecessor |
936 | 932 |
///arcs of the shortest paths. |
937 | 933 |
typedef typename TR::PredMap PredMap; |
938 | 934 |
///\brief The type of the map that stores the distances of the nodes. |
939 | 935 |
typedef typename TR::DistMap DistMap; |
940 | 936 |
///\brief The type of the map that indicates which nodes are reached. |
941 | 937 |
typedef typename TR::ReachedMap ReachedMap; |
942 | 938 |
///\brief The type of the map that indicates which nodes are processed. |
943 | 939 |
typedef typename TR::ProcessedMap ProcessedMap; |
944 | 940 |
|
945 | 941 |
public: |
946 | 942 |
|
947 | 943 |
/// Constructor. |
948 | 944 |
DfsWizard() : TR() {} |
949 | 945 |
|
950 | 946 |
/// Constructor that requires parameters. |
951 | 947 |
|
952 | 948 |
/// Constructor that requires parameters. |
953 | 949 |
/// These parameters will be the default values for the traits class. |
954 | 950 |
DfsWizard(const Digraph &g, Node s=INVALID) : |
955 | 951 |
TR(g,s) {} |
956 | 952 |
|
957 | 953 |
///Copy constructor |
958 | 954 |
DfsWizard(const TR &b) : TR(b) {} |
959 | 955 |
|
960 | 956 |
~DfsWizard() {} |
961 | 957 |
|
962 | 958 |
///Runs DFS algorithm from a source node. |
963 | 959 |
|
964 | 960 |
///Runs DFS algorithm from a source node. |
965 | 961 |
///The node can be given with the \ref source() function. |
966 | 962 |
void run() |
967 | 963 |
{ |
968 | 964 |
if(Base::_source==INVALID) throw UninitializedParameter(); |
969 | 965 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
970 | 966 |
if(Base::_reached) |
971 | 967 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
972 | 968 |
if(Base::_processed) |
973 | 969 |
alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
974 | 970 |
if(Base::_pred) |
975 | 971 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
976 | 972 |
if(Base::_dist) |
977 | 973 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
978 | 974 |
alg.run(Base::_source); |
979 | 975 |
} |
980 | 976 |
|
981 | 977 |
///Runs DFS algorithm from the given node. |
982 | 978 |
|
983 | 979 |
///Runs DFS algorithm from the given node. |
984 | 980 |
///\param s is the given source. |
985 | 981 |
void run(Node s) |
986 | 982 |
{ |
987 | 983 |
Base::_source=s; |
988 | 984 |
run(); |
989 | 985 |
} |
990 | 986 |
|
991 | 987 |
/// Sets the source node, from which the Dfs algorithm runs. |
992 | 988 |
|
993 | 989 |
/// Sets the source node, from which the Dfs algorithm runs. |
994 | 990 |
/// \param s is the source node. |
995 | 991 |
DfsWizard<TR> &source(Node s) |
996 | 992 |
{ |
997 | 993 |
Base::_source=s; |
998 | 994 |
return *this; |
999 | 995 |
} |
1000 | 996 |
|
1001 | 997 |
template<class T> |
1002 | 998 |
struct SetPredMapBase : public Base { |
1003 | 999 |
typedef T PredMap; |
1004 | 1000 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1005 | 1001 |
SetPredMapBase(const TR &b) : TR(b) {} |
1006 | 1002 |
}; |
1007 | 1003 |
///\brief \ref named-templ-param "Named parameter" |
1008 | 1004 |
///for setting \ref PredMap object. |
1009 | 1005 |
/// |
1010 | 1006 |
///\ref named-templ-param "Named parameter" |
1011 | 1007 |
///for setting \ref PredMap object. |
1012 | 1008 |
template<class T> |
1013 | 1009 |
DfsWizard<SetPredMapBase<T> > predMap(const T &t) |
1014 | 1010 |
{ |
1015 | 1011 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1016 | 1012 |
return DfsWizard<SetPredMapBase<T> >(*this); |
1017 | 1013 |
} |
1018 | 1014 |
|
1019 | 1015 |
template<class T> |
1020 | 1016 |
struct SetReachedMapBase : public Base { |
1021 | 1017 |
typedef T ReachedMap; |
1022 | 1018 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
1023 | 1019 |
SetReachedMapBase(const TR &b) : TR(b) {} |
1024 | 1020 |
}; |
1025 | 1021 |
///\brief \ref named-templ-param "Named parameter" |
1026 | 1022 |
///for setting \ref ReachedMap object. |
1027 | 1023 |
/// |
1028 | 1024 |
/// \ref named-templ-param "Named parameter" |
1029 | 1025 |
///for setting \ref ReachedMap object. |
1030 | 1026 |
template<class T> |
1031 | 1027 |
DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t) |
1032 | 1028 |
{ |
1033 | 1029 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1034 | 1030 |
return DfsWizard<SetReachedMapBase<T> >(*this); |
1035 | 1031 |
} |
1036 | 1032 |
|
1037 | 1033 |
template<class T> |
1038 | 1034 |
struct SetProcessedMapBase : public Base { |
1039 | 1035 |
typedef T ProcessedMap; |
1040 | 1036 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1041 | 1037 |
SetProcessedMapBase(const TR &b) : TR(b) {} |
1042 | 1038 |
}; |
1043 | 1039 |
///\brief \ref named-templ-param "Named parameter" |
1044 | 1040 |
///for setting \ref ProcessedMap object. |
1045 | 1041 |
/// |
1046 | 1042 |
/// \ref named-templ-param "Named parameter" |
1047 | 1043 |
///for setting \ref ProcessedMap object. |
1048 | 1044 |
template<class T> |
1049 | 1045 |
DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
1050 | 1046 |
{ |
1051 | 1047 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1052 | 1048 |
return DfsWizard<SetProcessedMapBase<T> >(*this); |
1053 | 1049 |
} |
1054 | 1050 |
|
1055 | 1051 |
template<class T> |
1056 | 1052 |
struct SetDistMapBase : public Base { |
1057 | 1053 |
typedef T DistMap; |
1058 | 1054 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1059 | 1055 |
SetDistMapBase(const TR &b) : TR(b) {} |
1060 | 1056 |
}; |
1061 | 1057 |
///\brief \ref named-templ-param "Named parameter" |
1062 | 1058 |
///for setting \ref DistMap object. |
1063 | 1059 |
/// |
1064 | 1060 |
///\ref named-templ-param "Named parameter" |
1065 | 1061 |
///for setting \ref DistMap object. |
1066 | 1062 |
template<class T> |
1067 | 1063 |
DfsWizard<SetDistMapBase<T> > distMap(const T &t) |
1068 | 1064 |
{ |
1069 | 1065 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1070 | 1066 |
return DfsWizard<SetDistMapBase<T> >(*this); |
1071 | 1067 |
} |
1072 | 1068 |
|
1073 | 1069 |
}; |
1074 | 1070 |
|
1075 | 1071 |
///Function type interface for Dfs algorithm. |
1076 | 1072 |
|
1077 | 1073 |
///\ingroup search |
1078 | 1074 |
///Function type interface for Dfs algorithm. |
1079 | 1075 |
/// |
1080 | 1076 |
///This function also has several |
1081 | 1077 |
///\ref named-templ-func-param "named parameters", |
1082 | 1078 |
///they are declared as the members of class \ref DfsWizard. |
1083 | 1079 |
///The following |
1084 | 1080 |
///example shows how to use these parameters. |
1085 | 1081 |
///\code |
1086 | 1082 |
/// dfs(g,source).predMap(preds).run(); |
1087 | 1083 |
///\endcode |
1088 | 1084 |
///\warning Don't forget to put the \ref DfsWizard::run() "run()" |
1089 | 1085 |
///to the end of the parameter list. |
1090 | 1086 |
///\sa DfsWizard |
1091 | 1087 |
///\sa Dfs |
1092 | 1088 |
template<class GR> |
1093 | 1089 |
DfsWizard<DfsWizardBase<GR> > |
1094 | 1090 |
dfs(const GR &g,typename GR::Node s=INVALID) |
1095 | 1091 |
{ |
1096 | 1092 |
return DfsWizard<DfsWizardBase<GR> >(g,s); |
1097 | 1093 |
} |
1098 | 1094 |
|
1099 | 1095 |
#ifdef DOXYGEN |
1100 | 1096 |
/// \brief Visitor class for DFS. |
1101 | 1097 |
/// |
1102 | 1098 |
/// This class defines the interface of the DfsVisit events, and |
1103 | 1099 |
/// it could be the base of a real visitor class. |
1104 | 1100 |
template <typename _Digraph> |
1105 | 1101 |
struct DfsVisitor { |
1106 | 1102 |
typedef _Digraph Digraph; |
1107 | 1103 |
typedef typename Digraph::Arc Arc; |
1108 | 1104 |
typedef typename Digraph::Node Node; |
1109 | 1105 |
/// \brief Called for the source node of the DFS. |
1110 | 1106 |
/// |
1111 | 1107 |
/// This function is called for the source node of the DFS. |
1112 | 1108 |
void start(const Node& node) {} |
1113 | 1109 |
/// \brief Called when the source node is leaved. |
1114 | 1110 |
/// |
1115 | 1111 |
/// This function is called when the source node is leaved. |
1116 | 1112 |
void stop(const Node& node) {} |
1117 | 1113 |
/// \brief Called when a node is reached first time. |
1118 | 1114 |
/// |
1119 | 1115 |
/// This function is called when a node is reached first time. |
1120 | 1116 |
void reach(const Node& node) {} |
1121 | 1117 |
/// \brief Called when an arc reaches a new node. |
1122 | 1118 |
/// |
1123 | 1119 |
/// This function is called when the DFS finds an arc whose target node |
1124 | 1120 |
/// is not reached yet. |
1125 | 1121 |
void discover(const Arc& arc) {} |
1126 | 1122 |
/// \brief Called when an arc is examined but its target node is |
1127 | 1123 |
/// already discovered. |
1128 | 1124 |
/// |
1129 | 1125 |
/// This function is called when an arc is examined but its target node is |
1130 | 1126 |
/// already discovered. |
1131 | 1127 |
void examine(const Arc& arc) {} |
1132 | 1128 |
/// \brief Called when the DFS steps back from a node. |
1133 | 1129 |
/// |
1134 | 1130 |
/// This function is called when the DFS steps back from a node. |
1135 | 1131 |
void leave(const Node& node) {} |
1136 | 1132 |
/// \brief Called when the DFS steps back on an arc. |
1137 | 1133 |
/// |
1138 | 1134 |
/// This function is called when the DFS steps back on an arc. |
1139 | 1135 |
void backtrack(const Arc& arc) {} |
1140 | 1136 |
}; |
1141 | 1137 |
#else |
1142 | 1138 |
template <typename _Digraph> |
1143 | 1139 |
struct DfsVisitor { |
1144 | 1140 |
typedef _Digraph Digraph; |
1145 | 1141 |
typedef typename Digraph::Arc Arc; |
1146 | 1142 |
typedef typename Digraph::Node Node; |
1147 | 1143 |
void start(const Node&) {} |
1148 | 1144 |
void stop(const Node&) {} |
1149 | 1145 |
void reach(const Node&) {} |
1150 | 1146 |
void discover(const Arc&) {} |
1151 | 1147 |
void examine(const Arc&) {} |
1152 | 1148 |
void leave(const Node&) {} |
1153 | 1149 |
void backtrack(const Arc&) {} |
1154 | 1150 |
|
1155 | 1151 |
template <typename _Visitor> |
1156 | 1152 |
struct Constraints { |
1157 | 1153 |
void constraints() { |
1158 | 1154 |
Arc arc; |
1159 | 1155 |
Node node; |
1160 | 1156 |
visitor.start(node); |
1161 | 1157 |
visitor.stop(arc); |
1162 | 1158 |
visitor.reach(node); |
1163 | 1159 |
visitor.discover(arc); |
1164 | 1160 |
visitor.examine(arc); |
1165 | 1161 |
visitor.leave(node); |
1166 | 1162 |
visitor.backtrack(arc); |
1167 | 1163 |
} |
1168 | 1164 |
_Visitor& visitor; |
1169 | 1165 |
}; |
1170 | 1166 |
}; |
1171 | 1167 |
#endif |
1172 | 1168 |
|
1173 | 1169 |
/// \brief Default traits class of DfsVisit class. |
1174 | 1170 |
/// |
1175 | 1171 |
/// Default traits class of DfsVisit class. |
1176 | 1172 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1177 | 1173 |
template<class _Digraph> |
1178 | 1174 |
struct DfsVisitDefaultTraits { |
1179 | 1175 |
|
1180 | 1176 |
/// \brief The type of the digraph the algorithm runs on. |
1181 | 1177 |
typedef _Digraph Digraph; |
1182 | 1178 |
|
1183 | 1179 |
/// \brief The type of the map that indicates which nodes are reached. |
1184 | 1180 |
/// |
1185 | 1181 |
/// The type of the map that indicates which nodes are reached. |
1186 | 1182 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
1187 | 1183 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1188 | 1184 |
|
1189 | 1185 |
/// \brief Instantiates a \ref ReachedMap. |
1190 | 1186 |
/// |
1191 | 1187 |
/// This function instantiates a \ref ReachedMap. |
1192 | 1188 |
/// \param digraph is the digraph, to which |
1193 | 1189 |
/// we would like to define the \ref ReachedMap. |
1194 | 1190 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1195 | 1191 |
return new ReachedMap(digraph); |
1196 | 1192 |
} |
1197 | 1193 |
|
1198 | 1194 |
}; |
1199 | 1195 |
|
1200 | 1196 |
/// \ingroup search |
1201 | 1197 |
/// |
1202 | 1198 |
/// \brief %DFS algorithm class with visitor interface. |
1203 | 1199 |
/// |
1204 | 1200 |
/// This class provides an efficient implementation of the %DFS algorithm |
1205 | 1201 |
/// with visitor interface. |
1206 | 1202 |
/// |
1207 | 1203 |
/// The %DfsVisit class provides an alternative interface to the Dfs |
1208 | 1204 |
/// class. It works with callback mechanism, the DfsVisit object calls |
1209 | 1205 |
/// the member functions of the \c Visitor class on every DFS event. |
1210 | 1206 |
/// |
1211 | 1207 |
/// This interface of the DFS algorithm should be used in special cases |
1212 | 1208 |
/// when extra actions have to be performed in connection with certain |
1213 | 1209 |
/// events of the DFS algorithm. Otherwise consider to use Dfs or dfs() |
1214 | 1210 |
/// instead. |
1215 | 1211 |
/// |
1216 | 1212 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
1217 | 1213 |
/// The default value is |
1218 | 1214 |
/// \ref ListDigraph. The value of _Digraph is not used directly by |
1219 | 1215 |
/// \ref DfsVisit, it is only passed to \ref DfsVisitDefaultTraits. |
1220 | 1216 |
/// \tparam _Visitor The Visitor type that is used by the algorithm. |
1221 | 1217 |
/// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty visitor, which |
1222 | 1218 |
/// does not observe the DFS events. If you want to observe the DFS |
1223 | 1219 |
/// events, you should implement your own visitor class. |
1224 | 1220 |
/// \tparam _Traits Traits class to set various data types used by the |
1225 | 1221 |
/// algorithm. The default traits class is |
1226 | 1222 |
/// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Digraph>". |
1227 | 1223 |
/// See \ref DfsVisitDefaultTraits for the documentation of |
1228 | 1224 |
/// a DFS visit traits class. |
1229 | 1225 |
#ifdef DOXYGEN |
1230 | 1226 |
template <typename _Digraph, typename _Visitor, typename _Traits> |
1231 | 1227 |
#else |
1232 | 1228 |
template <typename _Digraph = ListDigraph, |
1233 | 1229 |
typename _Visitor = DfsVisitor<_Digraph>, |
1234 | 1230 |
typename _Traits = DfsDefaultTraits<_Digraph> > |
1235 | 1231 |
#endif |
1236 | 1232 |
class DfsVisit { |
1237 | 1233 |
public: |
1238 | 1234 |
|
1239 | 1235 |
/// \brief \ref Exception for uninitialized parameters. |
1240 | 1236 |
/// |
1241 | 1237 |
/// This error represents problems in the initialization |
1242 | 1238 |
/// of the parameters of the algorithm. |
1243 | 1239 |
class UninitializedParameter : public lemon::UninitializedParameter { |
1244 | 1240 |
public: |
1245 | 1241 |
virtual const char* what() const throw() |
1246 | 1242 |
{ |
1247 | 1243 |
return "lemon::DfsVisit::UninitializedParameter"; |
1248 | 1244 |
} |
1249 | 1245 |
}; |
1250 | 1246 |
|
1251 | 1247 |
///The traits class. |
1252 | 1248 |
typedef _Traits Traits; |
1253 | 1249 |
|
1254 | 1250 |
///The type of the digraph the algorithm runs on. |
1255 | 1251 |
typedef typename Traits::Digraph Digraph; |
1256 | 1252 |
|
1257 | 1253 |
///The visitor type used by the algorithm. |
1258 | 1254 |
typedef _Visitor Visitor; |
1259 | 1255 |
|
1260 | 1256 |
///The type of the map that indicates which nodes are reached. |
1261 | 1257 |
typedef typename Traits::ReachedMap ReachedMap; |
1262 | 1258 |
|
1263 | 1259 |
private: |
1264 | 1260 |
|
1265 | 1261 |
typedef typename Digraph::Node Node; |
1266 | 1262 |
typedef typename Digraph::NodeIt NodeIt; |
1267 | 1263 |
typedef typename Digraph::Arc Arc; |
1268 | 1264 |
typedef typename Digraph::OutArcIt OutArcIt; |
1269 | 1265 |
|
1270 | 1266 |
//Pointer to the underlying digraph. |
1271 | 1267 |
const Digraph *_digraph; |
1272 | 1268 |
//Pointer to the visitor object. |
1273 | 1269 |
Visitor *_visitor; |
1274 | 1270 |
//Pointer to the map of reached status of the nodes. |
1275 | 1271 |
ReachedMap *_reached; |
1276 | 1272 |
//Indicates if _reached is locally allocated (true) or not. |
1277 | 1273 |
bool local_reached; |
1278 | 1274 |
|
1279 | 1275 |
std::vector<typename Digraph::Arc> _stack; |
1280 | 1276 |
int _stack_head; |
1281 | 1277 |
|
1282 |
///Creates the maps if necessary. |
|
1283 |
///\todo Better memory allocation (instead of new). |
|
1278 |
//Creates the maps if necessary. |
|
1284 | 1279 |
void create_maps() { |
1285 | 1280 |
if(!_reached) { |
1286 | 1281 |
local_reached = true; |
1287 | 1282 |
_reached = Traits::createReachedMap(*_digraph); |
1288 | 1283 |
} |
1289 | 1284 |
} |
1290 | 1285 |
|
1291 | 1286 |
protected: |
1292 | 1287 |
|
1293 | 1288 |
DfsVisit() {} |
1294 | 1289 |
|
1295 | 1290 |
public: |
1296 | 1291 |
|
1297 | 1292 |
typedef DfsVisit Create; |
1298 | 1293 |
|
1299 | 1294 |
/// \name Named template parameters |
1300 | 1295 |
|
1301 | 1296 |
///@{ |
1302 | 1297 |
template <class T> |
1303 | 1298 |
struct SetReachedMapTraits : public Traits { |
1304 | 1299 |
typedef T ReachedMap; |
1305 | 1300 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1306 | 1301 |
throw UninitializedParameter(); |
1307 | 1302 |
} |
1308 | 1303 |
}; |
1309 | 1304 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1310 | 1305 |
/// ReachedMap type. |
1311 | 1306 |
/// |
1312 | 1307 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
1313 | 1308 |
template <class T> |
1314 | 1309 |
struct SetReachedMap : public DfsVisit< Digraph, Visitor, |
1315 | 1310 |
SetReachedMapTraits<T> > { |
1316 | 1311 |
typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
1317 | 1312 |
}; |
1318 | 1313 |
///@} |
1319 | 1314 |
|
1320 | 1315 |
public: |
1321 | 1316 |
|
1322 | 1317 |
/// \brief Constructor. |
1323 | 1318 |
/// |
1324 | 1319 |
/// Constructor. |
1325 | 1320 |
/// |
1326 | 1321 |
/// \param digraph The digraph the algorithm runs on. |
1327 | 1322 |
/// \param visitor The visitor object of the algorithm. |
1328 | 1323 |
DfsVisit(const Digraph& digraph, Visitor& visitor) |
1329 | 1324 |
: _digraph(&digraph), _visitor(&visitor), |
1330 | 1325 |
_reached(0), local_reached(false) {} |
1331 | 1326 |
|
1332 | 1327 |
/// \brief Destructor. |
1333 | 1328 |
~DfsVisit() { |
1334 | 1329 |
if(local_reached) delete _reached; |
1335 | 1330 |
} |
1336 | 1331 |
|
1337 | 1332 |
/// \brief Sets the map that indicates which nodes are reached. |
1338 | 1333 |
/// |
1339 | 1334 |
/// Sets the map that indicates which nodes are reached. |
1340 | 1335 |
/// If you don't use this function before calling \ref run(), |
1341 | 1336 |
/// it will allocate one. The destructor deallocates this |
1342 | 1337 |
/// automatically allocated map, of course. |
1343 | 1338 |
/// \return <tt> (*this) </tt> |
1344 | 1339 |
DfsVisit &reachedMap(ReachedMap &m) { |
1345 | 1340 |
if(local_reached) { |
1346 | 1341 |
delete _reached; |
1347 | 1342 |
local_reached=false; |
1348 | 1343 |
} |
1349 | 1344 |
_reached = &m; |
1350 | 1345 |
return *this; |
1351 | 1346 |
} |
1352 | 1347 |
|
1353 | 1348 |
public: |
1354 | 1349 |
|
1355 | 1350 |
/// \name Execution control |
1356 | 1351 |
/// The simplest way to execute the algorithm is to use |
1357 | 1352 |
/// one of the member functions called \ref lemon::DfsVisit::run() |
1358 | 1353 |
/// "run()". |
1359 | 1354 |
/// \n |
1360 | 1355 |
/// If you need more control on the execution, first you must call |
1361 | 1356 |
/// \ref lemon::DfsVisit::init() "init()", then you can add several |
1362 | 1357 |
/// source nodes with \ref lemon::DfsVisit::addSource() "addSource()". |
1363 | 1358 |
/// Finally \ref lemon::DfsVisit::start() "start()" will perform the |
1364 | 1359 |
/// actual path computation. |
1365 | 1360 |
|
1366 | 1361 |
/// @{ |
1367 | 1362 |
|
1368 | 1363 |
/// \brief Initializes the internal data structures. |
1369 | 1364 |
/// |
1370 | 1365 |
/// Initializes the internal data structures. |
1371 | 1366 |
void init() { |
1372 | 1367 |
create_maps(); |
1373 | 1368 |
_stack.resize(countNodes(*_digraph)); |
1374 | 1369 |
_stack_head = -1; |
1375 | 1370 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1376 | 1371 |
_reached->set(u, false); |
1377 | 1372 |
} |
1378 | 1373 |
} |
1379 | 1374 |
|
1380 | 1375 |
///Adds a new source node. |
1381 | 1376 |
|
1382 | 1377 |
///Adds a new source node to the set of nodes to be processed. |
1383 | 1378 |
/// |
1384 | 1379 |
///\pre The stack must be empty. (Otherwise the algorithm gives |
1385 | 1380 |
///false results.) |
1386 | 1381 |
/// |
1387 | 1382 |
///\warning Distances will be wrong (or at least strange) in case of |
1388 | 1383 |
///multiple sources. |
1389 | 1384 |
void addSource(Node s) |
1390 | 1385 |
{ |
1391 | 1386 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
1392 | 1387 |
if(!(*_reached)[s]) { |
1393 | 1388 |
_reached->set(s,true); |
1394 | 1389 |
_visitor->start(s); |
1395 | 1390 |
_visitor->reach(s); |
1396 | 1391 |
Arc e; |
1397 | 1392 |
_digraph->firstOut(e, s); |
1398 | 1393 |
if (e != INVALID) { |
1399 | 1394 |
_stack[++_stack_head] = e; |
1400 | 1395 |
} else { |
1401 | 1396 |
_visitor->leave(s); |
1402 | 1397 |
} |
1403 | 1398 |
} |
1404 | 1399 |
} |
1405 | 1400 |
|
1406 | 1401 |
/// \brief Processes the next arc. |
1407 | 1402 |
/// |
1408 | 1403 |
/// Processes the next arc. |
1409 | 1404 |
/// |
1410 | 1405 |
/// \return The processed arc. |
1411 | 1406 |
/// |
1412 | 1407 |
/// \pre The stack must not be empty. |
1413 | 1408 |
Arc processNextArc() { |
1414 | 1409 |
Arc e = _stack[_stack_head]; |
1415 | 1410 |
Node m = _digraph->target(e); |
1416 | 1411 |
if(!(*_reached)[m]) { |
1417 | 1412 |
_visitor->discover(e); |
1418 | 1413 |
_visitor->reach(m); |
1419 | 1414 |
_reached->set(m, true); |
1420 | 1415 |
_digraph->firstOut(_stack[++_stack_head], m); |
1421 | 1416 |
} else { |
1422 | 1417 |
_visitor->examine(e); |
1423 | 1418 |
m = _digraph->source(e); |
1424 | 1419 |
_digraph->nextOut(_stack[_stack_head]); |
1425 | 1420 |
} |
1426 | 1421 |
while (_stack_head>=0 && _stack[_stack_head] == INVALID) { |
1427 | 1422 |
_visitor->leave(m); |
1428 | 1423 |
--_stack_head; |
1429 | 1424 |
if (_stack_head >= 0) { |
1430 | 1425 |
_visitor->backtrack(_stack[_stack_head]); |
1431 | 1426 |
m = _digraph->source(_stack[_stack_head]); |
1432 | 1427 |
_digraph->nextOut(_stack[_stack_head]); |
1433 | 1428 |
} else { |
1434 | 1429 |
_visitor->stop(m); |
1435 | 1430 |
} |
1436 | 1431 |
} |
1437 | 1432 |
return e; |
1438 | 1433 |
} |
1439 | 1434 |
|
1440 | 1435 |
/// \brief Next arc to be processed. |
1441 | 1436 |
/// |
1442 | 1437 |
/// Next arc to be processed. |
1443 | 1438 |
/// |
1444 | 1439 |
/// \return The next arc to be processed or INVALID if the stack is |
1445 | 1440 |
/// empty. |
1446 | 1441 |
Arc nextArc() const { |
1447 | 1442 |
return _stack_head >= 0 ? _stack[_stack_head] : INVALID; |
1448 | 1443 |
} |
1449 | 1444 |
|
1450 | 1445 |
/// \brief Returns \c false if there are nodes |
1451 | 1446 |
/// to be processed. |
1452 | 1447 |
/// |
1453 | 1448 |
/// Returns \c false if there are nodes |
1454 | 1449 |
/// to be processed in the queue (stack). |
1455 | 1450 |
bool emptyQueue() const { return _stack_head < 0; } |
1456 | 1451 |
|
1457 | 1452 |
/// \brief Returns the number of the nodes to be processed. |
1458 | 1453 |
/// |
1459 | 1454 |
/// Returns the number of the nodes to be processed in the queue (stack). |
1460 | 1455 |
int queueSize() const { return _stack_head + 1; } |
1461 | 1456 |
|
1462 | 1457 |
/// \brief Executes the algorithm. |
1463 | 1458 |
/// |
1464 | 1459 |
/// Executes the algorithm. |
1465 | 1460 |
/// |
1466 | 1461 |
/// This method runs the %DFS algorithm from the root node |
1467 | 1462 |
/// in order to compute the %DFS path to each node. |
1468 | 1463 |
/// |
1469 | 1464 |
/// The algorithm computes |
1470 | 1465 |
/// - the %DFS tree, |
1471 | 1466 |
/// - the distance of each node from the root in the %DFS tree. |
1472 | 1467 |
/// |
1473 | 1468 |
/// \pre init() must be called and a root node should be |
1474 | 1469 |
/// added with addSource() before using this function. |
1475 | 1470 |
/// |
1476 | 1471 |
/// \note <tt>d.start()</tt> is just a shortcut of the following code. |
1477 | 1472 |
/// \code |
1478 | 1473 |
/// while ( !d.emptyQueue() ) { |
1479 | 1474 |
/// d.processNextArc(); |
1480 | 1475 |
/// } |
1481 | 1476 |
/// \endcode |
1482 | 1477 |
void start() { |
1483 | 1478 |
while ( !emptyQueue() ) processNextArc(); |
1484 | 1479 |
} |
1485 | 1480 |
|
1486 | 1481 |
/// \brief Executes the algorithm until the given target node is reached. |
1487 | 1482 |
/// |
1488 | 1483 |
/// Executes the algorithm until the given target node is reached. |
1489 | 1484 |
/// |
1490 | 1485 |
/// This method runs the %DFS algorithm from the root node |
1491 | 1486 |
/// in order to compute the DFS path to \c dest. |
1492 | 1487 |
/// |
1493 | 1488 |
/// The algorithm computes |
1494 | 1489 |
/// - the %DFS path to \c dest, |
1495 | 1490 |
/// - the distance of \c dest from the root in the %DFS tree. |
1496 | 1491 |
/// |
1497 | 1492 |
/// \pre init() must be called and a root node should be added |
1498 | 1493 |
/// with addSource() before using this function. |
1499 | 1494 |
void start(Node dest) { |
1500 | 1495 |
while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != dest ) |
1501 | 1496 |
processNextArc(); |
1502 | 1497 |
} |
1503 | 1498 |
|
1504 | 1499 |
/// \brief Executes the algorithm until a condition is met. |
1505 | 1500 |
/// |
1506 | 1501 |
/// Executes the algorithm until a condition is met. |
1507 | 1502 |
/// |
1508 | 1503 |
/// This method runs the %DFS algorithm from the root node |
1509 | 1504 |
/// until an arc \c a with <tt>am[a]</tt> true is found. |
1510 | 1505 |
/// |
1511 | 1506 |
/// \param am A \c bool (or convertible) arc map. The algorithm |
1512 | 1507 |
/// will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
1513 | 1508 |
/// |
1514 | 1509 |
/// \return The reached arc \c a with <tt>am[a]</tt> true or |
1515 | 1510 |
/// \c INVALID if no such arc was found. |
1516 | 1511 |
/// |
1517 | 1512 |
/// \pre init() must be called and a root node should be added |
1518 | 1513 |
/// with addSource() before using this function. |
1519 | 1514 |
/// |
1520 | 1515 |
/// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
1521 | 1516 |
/// not a node map. |
1522 | 1517 |
template <typename AM> |
1523 | 1518 |
Arc start(const AM &am) { |
1524 | 1519 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
1525 | 1520 |
processNextArc(); |
1526 | 1521 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
1527 | 1522 |
} |
1528 | 1523 |
|
1529 | 1524 |
/// \brief Runs the algorithm from the given node. |
1530 | 1525 |
/// |
1531 | 1526 |
/// This method runs the %DFS algorithm from node \c s. |
1532 | 1527 |
/// in order to compute the DFS path to each node. |
1533 | 1528 |
/// |
1534 | 1529 |
/// The algorithm computes |
1535 | 1530 |
/// - the %DFS tree, |
1536 | 1531 |
/// - the distance of each node from the root in the %DFS tree. |
1537 | 1532 |
/// |
1538 | 1533 |
/// \note <tt>d.run(s)</tt> is just a shortcut of the following code. |
1539 | 1534 |
///\code |
1540 | 1535 |
/// d.init(); |
1541 | 1536 |
/// d.addSource(s); |
1542 | 1537 |
/// d.start(); |
1543 | 1538 |
///\endcode |
1544 | 1539 |
void run(Node s) { |
1545 | 1540 |
init(); |
1546 | 1541 |
addSource(s); |
1547 | 1542 |
start(); |
1548 | 1543 |
} |
1549 | 1544 |
|
1550 | 1545 |
/// \brief Finds the %DFS path between \c s and \c t. |
1551 | 1546 |
|
1552 | 1547 |
/// This method runs the %DFS algorithm from node \c s |
1553 | 1548 |
/// in order to compute the DFS path to \c t. |
1554 | 1549 |
/// |
1555 | 1550 |
/// \return The length of the <tt>s</tt>--<tt>t</tt> DFS path, |
1556 | 1551 |
/// if \c t is reachable form \c s, \c 0 otherwise. |
1557 | 1552 |
/// |
1558 | 1553 |
/// \note Apart from the return value, <tt>d.run(s,t)</tt> is |
1559 | 1554 |
/// just a shortcut of the following code. |
1560 | 1555 |
///\code |
1561 | 1556 |
/// d.init(); |
1562 | 1557 |
/// d.addSource(s); |
1563 | 1558 |
/// d.start(t); |
1564 | 1559 |
///\endcode |
1565 | 1560 |
int run(Node s,Node t) { |
1566 | 1561 |
init(); |
1567 | 1562 |
addSource(s); |
1568 | 1563 |
start(t); |
1569 | 1564 |
return reached(t)?_stack_head+1:0; |
1570 | 1565 |
} |
1571 | 1566 |
|
1572 | 1567 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
1573 | 1568 |
|
1574 | 1569 |
/// This method runs the %DFS algorithm in order to |
1575 | 1570 |
/// compute the %DFS path to each node. |
1576 | 1571 |
/// |
1577 | 1572 |
/// The algorithm computes |
1578 | 1573 |
/// - the %DFS tree, |
1579 | 1574 |
/// - the distance of each node from the root in the %DFS tree. |
1580 | 1575 |
/// |
1581 | 1576 |
/// \note <tt>d.run()</tt> is just a shortcut of the following code. |
1582 | 1577 |
///\code |
1583 | 1578 |
/// d.init(); |
1584 | 1579 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
1585 | 1580 |
/// if (!d.reached(n)) { |
1586 | 1581 |
/// d.addSource(n); |
1587 | 1582 |
/// d.start(); |
1588 | 1583 |
/// } |
1589 | 1584 |
/// } |
1590 | 1585 |
///\endcode |
1591 | 1586 |
void run() { |
1592 | 1587 |
init(); |
1593 | 1588 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1594 | 1589 |
if (!reached(it)) { |
1595 | 1590 |
addSource(it); |
1596 | 1591 |
start(); |
1597 | 1592 |
} |
1598 | 1593 |
} |
1599 | 1594 |
} |
1600 | 1595 |
|
1601 | 1596 |
///@} |
1602 | 1597 |
|
1603 | 1598 |
/// \name Query Functions |
1604 | 1599 |
/// The result of the %DFS algorithm can be obtained using these |
1605 | 1600 |
/// functions.\n |
1606 | 1601 |
/// Either \ref lemon::DfsVisit::run() "run()" or |
1607 | 1602 |
/// \ref lemon::DfsVisit::start() "start()" must be called before |
1608 | 1603 |
/// using them. |
1609 | 1604 |
///@{ |
1610 | 1605 |
|
1611 | 1606 |
/// \brief Checks if a node is reachable from the root(s). |
1612 | 1607 |
/// |
1613 | 1608 |
/// Returns \c true if \c v is reachable from the root(s). |
1614 | 1609 |
/// \pre Either \ref run() or \ref start() |
1615 | 1610 |
/// must be called before using this function. |
1616 | 1611 |
bool reached(Node v) { return (*_reached)[v]; } |
1617 | 1612 |
|
1618 | 1613 |
///@} |
1619 | 1614 |
|
1620 | 1615 |
}; |
1621 | 1616 |
|
1622 | 1617 |
} //END OF NAMESPACE LEMON |
1623 | 1618 |
|
1624 | 1619 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-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 |
#include <limits> |
27 | 27 |
#include <lemon/list_graph.h> |
28 | 28 |
#include <lemon/bin_heap.h> |
29 | 29 |
#include <lemon/bits/path_dump.h> |
30 | 30 |
#include <lemon/core.h> |
31 | 31 |
#include <lemon/error.h> |
32 | 32 |
#include <lemon/maps.h> |
33 | 33 |
|
34 | 34 |
namespace lemon { |
35 | 35 |
|
36 | 36 |
/// \brief Default operation traits for the Dijkstra algorithm class. |
37 | 37 |
/// |
38 | 38 |
/// This operation traits class defines all computational operations and |
39 | 39 |
/// constants which are used in the Dijkstra algorithm. |
40 | 40 |
template <typename Value> |
41 | 41 |
struct DijkstraDefaultOperationTraits { |
42 | 42 |
/// \brief Gives back the zero value of the type. |
43 | 43 |
static Value zero() { |
44 | 44 |
return static_cast<Value>(0); |
45 | 45 |
} |
46 | 46 |
/// \brief Gives back the sum of the given two elements. |
47 | 47 |
static Value plus(const Value& left, const Value& right) { |
48 | 48 |
return left + right; |
49 | 49 |
} |
50 | 50 |
/// \brief Gives back true only if the first value is less than the second. |
51 | 51 |
static bool less(const Value& left, const Value& right) { |
52 | 52 |
return left < right; |
53 | 53 |
} |
54 | 54 |
}; |
55 | 55 |
|
56 | 56 |
/// \brief Widest path operation traits for the Dijkstra algorithm class. |
57 | 57 |
/// |
58 | 58 |
/// This operation traits class defines all computational operations and |
59 | 59 |
/// constants which are used in the Dijkstra algorithm for widest path |
60 | 60 |
/// computation. |
61 | 61 |
/// |
62 | 62 |
/// \see DijkstraDefaultOperationTraits |
63 | 63 |
template <typename Value> |
64 | 64 |
struct DijkstraWidestPathOperationTraits { |
65 | 65 |
/// \brief Gives back the maximum value of the type. |
66 | 66 |
static Value zero() { |
67 | 67 |
return std::numeric_limits<Value>::max(); |
68 | 68 |
} |
69 | 69 |
/// \brief Gives back the minimum of the given two elements. |
70 | 70 |
static Value plus(const Value& left, const Value& right) { |
71 | 71 |
return std::min(left, right); |
72 | 72 |
} |
73 | 73 |
/// \brief Gives back true only if the first value is less than the second. |
74 | 74 |
static bool less(const Value& left, const Value& right) { |
75 | 75 |
return left < right; |
76 | 76 |
} |
77 | 77 |
}; |
78 | 78 |
|
79 | 79 |
///Default traits class of Dijkstra class. |
80 | 80 |
|
81 | 81 |
///Default traits class of Dijkstra class. |
82 | 82 |
///\tparam GR The type of the digraph. |
83 | 83 |
///\tparam LM The type of the length map. |
84 | 84 |
template<class GR, class LM> |
85 | 85 |
struct DijkstraDefaultTraits |
86 | 86 |
{ |
87 | 87 |
///The type of the digraph the algorithm runs on. |
88 | 88 |
typedef GR Digraph; |
89 | 89 |
|
90 | 90 |
///The type of the map that stores the arc lengths. |
91 | 91 |
|
92 | 92 |
///The type of the map that stores the arc lengths. |
93 | 93 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
94 | 94 |
typedef LM LengthMap; |
95 | 95 |
///The type of the length of the arcs. |
96 | 96 |
typedef typename LM::Value Value; |
97 | 97 |
|
98 | 98 |
/// Operation traits for Dijkstra algorithm. |
99 | 99 |
|
100 | 100 |
/// This class defines the operations that are used in the algorithm. |
101 | 101 |
/// \see DijkstraDefaultOperationTraits |
102 | 102 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
103 | 103 |
|
104 | 104 |
/// The cross reference type used by the heap. |
105 | 105 |
|
106 | 106 |
/// The cross reference type used by the heap. |
107 | 107 |
/// Usually it is \c Digraph::NodeMap<int>. |
108 | 108 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
109 | 109 |
///Instantiates a \ref HeapCrossRef. |
110 | 110 |
|
111 | 111 |
///This function instantiates a \ref HeapCrossRef. |
112 | 112 |
/// \param g is the digraph, to which we would like to define the |
113 | 113 |
/// \ref HeapCrossRef. |
114 | 114 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
115 | 115 |
{ |
116 | 116 |
return new HeapCrossRef(g); |
117 | 117 |
} |
118 | 118 |
|
119 | 119 |
///The heap type used by the Dijkstra algorithm. |
120 | 120 |
|
121 | 121 |
///The heap type used by the Dijkstra algorithm. |
122 | 122 |
/// |
123 | 123 |
///\sa BinHeap |
124 | 124 |
///\sa Dijkstra |
125 | 125 |
typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap; |
126 | 126 |
///Instantiates a \ref Heap. |
127 | 127 |
|
128 | 128 |
///This function instantiates a \ref Heap. |
129 | 129 |
static Heap *createHeap(HeapCrossRef& r) |
130 | 130 |
{ |
131 | 131 |
return new Heap(r); |
132 | 132 |
} |
133 | 133 |
|
134 | 134 |
///\brief The type of the map that stores the predecessor |
135 | 135 |
///arcs of the shortest paths. |
136 | 136 |
/// |
137 | 137 |
///The type of the map that stores the predecessor |
138 | 138 |
///arcs of the shortest paths. |
139 | 139 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
140 | 140 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
141 | 141 |
///Instantiates a \ref PredMap. |
142 | 142 |
|
143 | 143 |
///This function instantiates a \ref PredMap. |
144 | 144 |
///\param g is the digraph, to which we would like to define the |
145 | 145 |
///\ref PredMap. |
146 |
///\todo The digraph alone may be insufficient for the initialization |
|
147 | 146 |
static PredMap *createPredMap(const Digraph &g) |
148 | 147 |
{ |
149 | 148 |
return new PredMap(g); |
150 | 149 |
} |
151 | 150 |
|
152 | 151 |
///The type of the map that indicates which nodes are processed. |
153 | 152 |
|
154 | 153 |
///The type of the map that indicates which nodes are processed. |
155 | 154 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
156 | 155 |
///By default it is a NullMap. |
157 |
///\todo If it is set to a real map, |
|
158 |
///Dijkstra::processed() should read this. |
|
159 | 156 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
160 | 157 |
///Instantiates a \ref ProcessedMap. |
161 | 158 |
|
162 | 159 |
///This function instantiates a \ref ProcessedMap. |
163 | 160 |
///\param g is the digraph, to which |
164 | 161 |
///we would like to define the \ref ProcessedMap |
165 | 162 |
#ifdef DOXYGEN |
166 | 163 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
167 | 164 |
#else |
168 | 165 |
static ProcessedMap *createProcessedMap(const Digraph &) |
169 | 166 |
#endif |
170 | 167 |
{ |
171 | 168 |
return new ProcessedMap(); |
172 | 169 |
} |
173 | 170 |
|
174 | 171 |
///The type of the map that stores the distances of the nodes. |
175 | 172 |
|
176 | 173 |
///The type of the map that stores the distances of the nodes. |
177 | 174 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
178 | 175 |
typedef typename Digraph::template NodeMap<typename LM::Value> DistMap; |
179 | 176 |
///Instantiates a \ref DistMap. |
180 | 177 |
|
181 | 178 |
///This function instantiates a \ref DistMap. |
182 | 179 |
///\param g is the digraph, to which we would like to define |
183 | 180 |
///the \ref DistMap |
184 | 181 |
static DistMap *createDistMap(const Digraph &g) |
185 | 182 |
{ |
186 | 183 |
return new DistMap(g); |
187 | 184 |
} |
188 | 185 |
}; |
189 | 186 |
|
190 | 187 |
///%Dijkstra algorithm class. |
191 | 188 |
|
192 | 189 |
/// \ingroup shortest_path |
193 | 190 |
///This class provides an efficient implementation of the %Dijkstra algorithm. |
194 | 191 |
/// |
195 | 192 |
///The arc lengths are passed to the algorithm using a |
196 | 193 |
///\ref concepts::ReadMap "ReadMap", |
197 | 194 |
///so it is easy to change it to any kind of length. |
198 | 195 |
///The type of the length is determined by the |
199 | 196 |
///\ref concepts::ReadMap::Value "Value" of the length map. |
200 | 197 |
///It is also possible to change the underlying priority heap. |
201 | 198 |
/// |
202 | 199 |
///There is also a \ref dijkstra() "function type interface" for the |
203 | 200 |
///%Dijkstra algorithm, which is convenient in the simplier cases and |
204 | 201 |
///it can be used easier. |
205 | 202 |
/// |
206 | 203 |
///\tparam GR The type of the digraph the algorithm runs on. |
207 | 204 |
///The default value is \ref ListDigraph. |
208 | 205 |
///The value of GR is not used directly by \ref Dijkstra, it is only |
209 | 206 |
///passed to \ref DijkstraDefaultTraits. |
210 | 207 |
///\tparam LM A readable arc map that determines the lengths of the |
211 | 208 |
///arcs. It is read once for each arc, so the map may involve in |
212 | 209 |
///relatively time consuming process to compute the arc lengths if |
213 | 210 |
///it is necessary. The default map type is \ref |
214 | 211 |
///concepts::Digraph::ArcMap "Digraph::ArcMap<int>". |
215 | 212 |
///The value of LM is not used directly by \ref Dijkstra, it is only |
216 | 213 |
///passed to \ref DijkstraDefaultTraits. |
217 | 214 |
///\tparam TR Traits class to set various data types used by the algorithm. |
218 | 215 |
///The default traits class is \ref DijkstraDefaultTraits |
219 | 216 |
///"DijkstraDefaultTraits<GR,LM>". See \ref DijkstraDefaultTraits |
220 | 217 |
///for the documentation of a Dijkstra traits class. |
221 | 218 |
#ifdef DOXYGEN |
222 | 219 |
template <typename GR, typename LM, typename TR> |
223 | 220 |
#else |
224 | 221 |
template <typename GR=ListDigraph, |
225 | 222 |
typename LM=typename GR::template ArcMap<int>, |
226 | 223 |
typename TR=DijkstraDefaultTraits<GR,LM> > |
227 | 224 |
#endif |
228 | 225 |
class Dijkstra { |
229 | 226 |
public: |
230 | 227 |
///\ref Exception for uninitialized parameters. |
231 | 228 |
|
232 | 229 |
///This error represents problems in the initialization of the |
233 | 230 |
///parameters of the algorithm. |
234 | 231 |
class UninitializedParameter : public lemon::UninitializedParameter { |
235 | 232 |
public: |
236 | 233 |
virtual const char* what() const throw() { |
237 | 234 |
return "lemon::Dijkstra::UninitializedParameter"; |
238 | 235 |
} |
239 | 236 |
}; |
240 | 237 |
|
241 | 238 |
///The type of the digraph the algorithm runs on. |
242 | 239 |
typedef typename TR::Digraph Digraph; |
243 | 240 |
|
244 | 241 |
///The type of the length of the arcs. |
245 | 242 |
typedef typename TR::LengthMap::Value Value; |
246 | 243 |
///The type of the map that stores the arc lengths. |
247 | 244 |
typedef typename TR::LengthMap LengthMap; |
248 | 245 |
///\brief The type of the map that stores the predecessor arcs of the |
249 | 246 |
///shortest paths. |
250 | 247 |
typedef typename TR::PredMap PredMap; |
251 | 248 |
///The type of the map that stores the distances of the nodes. |
252 | 249 |
typedef typename TR::DistMap DistMap; |
253 | 250 |
///The type of the map that indicates which nodes are processed. |
254 | 251 |
typedef typename TR::ProcessedMap ProcessedMap; |
255 | 252 |
///The type of the paths. |
256 | 253 |
typedef PredMapPath<Digraph, PredMap> Path; |
257 | 254 |
///The cross reference type used for the current heap. |
258 | 255 |
typedef typename TR::HeapCrossRef HeapCrossRef; |
259 | 256 |
///The heap type used by the algorithm. |
260 | 257 |
typedef typename TR::Heap Heap; |
261 | 258 |
///The operation traits class. |
262 | 259 |
typedef typename TR::OperationTraits OperationTraits; |
263 | 260 |
|
264 | 261 |
///The traits class. |
265 | 262 |
typedef TR Traits; |
266 | 263 |
|
267 | 264 |
private: |
268 | 265 |
|
269 | 266 |
typedef typename Digraph::Node Node; |
270 | 267 |
typedef typename Digraph::NodeIt NodeIt; |
271 | 268 |
typedef typename Digraph::Arc Arc; |
272 | 269 |
typedef typename Digraph::OutArcIt OutArcIt; |
273 | 270 |
|
274 | 271 |
//Pointer to the underlying digraph. |
275 | 272 |
const Digraph *G; |
276 | 273 |
//Pointer to the length map. |
277 | 274 |
const LengthMap *length; |
278 | 275 |
//Pointer to the map of predecessors arcs. |
279 | 276 |
PredMap *_pred; |
280 | 277 |
//Indicates if _pred is locally allocated (true) or not. |
281 | 278 |
bool local_pred; |
282 | 279 |
//Pointer to the map of distances. |
283 | 280 |
DistMap *_dist; |
284 | 281 |
//Indicates if _dist is locally allocated (true) or not. |
285 | 282 |
bool local_dist; |
286 | 283 |
//Pointer to the map of processed status of the nodes. |
287 | 284 |
ProcessedMap *_processed; |
288 | 285 |
//Indicates if _processed is locally allocated (true) or not. |
289 | 286 |
bool local_processed; |
290 | 287 |
//Pointer to the heap cross references. |
291 | 288 |
HeapCrossRef *_heap_cross_ref; |
292 | 289 |
//Indicates if _heap_cross_ref is locally allocated (true) or not. |
293 | 290 |
bool local_heap_cross_ref; |
294 | 291 |
//Pointer to the heap. |
295 | 292 |
Heap *_heap; |
296 | 293 |
//Indicates if _heap is locally allocated (true) or not. |
297 | 294 |
bool local_heap; |
298 | 295 |
|
299 |
///Creates the maps if necessary. |
|
300 |
///\todo Better memory allocation (instead of new). |
|
296 |
//Creates the maps if necessary. |
|
301 | 297 |
void create_maps() |
302 | 298 |
{ |
303 | 299 |
if(!_pred) { |
304 | 300 |
local_pred = true; |
305 | 301 |
_pred = Traits::createPredMap(*G); |
306 | 302 |
} |
307 | 303 |
if(!_dist) { |
308 | 304 |
local_dist = true; |
309 | 305 |
_dist = Traits::createDistMap(*G); |
310 | 306 |
} |
311 | 307 |
if(!_processed) { |
312 | 308 |
local_processed = true; |
313 | 309 |
_processed = Traits::createProcessedMap(*G); |
314 | 310 |
} |
315 | 311 |
if (!_heap_cross_ref) { |
316 | 312 |
local_heap_cross_ref = true; |
317 | 313 |
_heap_cross_ref = Traits::createHeapCrossRef(*G); |
318 | 314 |
} |
319 | 315 |
if (!_heap) { |
320 | 316 |
local_heap = true; |
321 | 317 |
_heap = Traits::createHeap(*_heap_cross_ref); |
322 | 318 |
} |
323 | 319 |
} |
324 | 320 |
|
325 | 321 |
public: |
326 | 322 |
|
327 | 323 |
typedef Dijkstra Create; |
328 | 324 |
|
329 | 325 |
///\name Named template parameters |
330 | 326 |
|
331 | 327 |
///@{ |
332 | 328 |
|
333 | 329 |
template <class T> |
334 | 330 |
struct SetPredMapTraits : public Traits { |
335 | 331 |
typedef T PredMap; |
336 | 332 |
static PredMap *createPredMap(const Digraph &) |
337 | 333 |
{ |
338 | 334 |
throw UninitializedParameter(); |
339 | 335 |
} |
340 | 336 |
}; |
341 | 337 |
///\brief \ref named-templ-param "Named parameter" for setting |
342 | 338 |
///\ref PredMap type. |
343 | 339 |
/// |
344 | 340 |
///\ref named-templ-param "Named parameter" for setting |
345 | 341 |
///\ref PredMap type. |
346 | 342 |
template <class T> |
347 | 343 |
struct SetPredMap |
348 | 344 |
: public Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > { |
349 | 345 |
typedef Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > Create; |
350 | 346 |
}; |
351 | 347 |
|
352 | 348 |
template <class T> |
353 | 349 |
struct SetDistMapTraits : public Traits { |
354 | 350 |
typedef T DistMap; |
355 | 351 |
static DistMap *createDistMap(const Digraph &) |
356 | 352 |
{ |
357 | 353 |
throw UninitializedParameter(); |
358 | 354 |
} |
359 | 355 |
}; |
360 | 356 |
///\brief \ref named-templ-param "Named parameter" for setting |
361 | 357 |
///\ref DistMap type. |
362 | 358 |
/// |
363 | 359 |
///\ref named-templ-param "Named parameter" for setting |
364 | 360 |
///\ref DistMap type. |
365 | 361 |
template <class T> |
366 | 362 |
struct SetDistMap |
367 | 363 |
: public Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > { |
368 | 364 |
typedef Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > Create; |
369 | 365 |
}; |
370 | 366 |
|
371 | 367 |
template <class T> |
372 | 368 |
struct SetProcessedMapTraits : public Traits { |
373 | 369 |
typedef T ProcessedMap; |
374 | 370 |
static ProcessedMap *createProcessedMap(const Digraph &) |
375 | 371 |
{ |
376 | 372 |
throw UninitializedParameter(); |
377 | 373 |
} |
378 | 374 |
}; |
379 | 375 |
///\brief \ref named-templ-param "Named parameter" for setting |
380 | 376 |
///\ref ProcessedMap type. |
381 | 377 |
/// |
382 | 378 |
///\ref named-templ-param "Named parameter" for setting |
383 | 379 |
///\ref ProcessedMap type. |
384 | 380 |
template <class T> |
385 | 381 |
struct SetProcessedMap |
386 | 382 |
: public Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > { |
387 | 383 |
typedef Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > Create; |
388 | 384 |
}; |
389 | 385 |
|
390 | 386 |
struct SetStandardProcessedMapTraits : public Traits { |
391 | 387 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
392 | 388 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
393 | 389 |
{ |
394 | 390 |
return new ProcessedMap(g); |
395 | 391 |
} |
396 | 392 |
}; |
397 | 393 |
///\brief \ref named-templ-param "Named parameter" for setting |
398 | 394 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
399 | 395 |
/// |
400 | 396 |
///\ref named-templ-param "Named parameter" for setting |
401 | 397 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
402 | 398 |
///If you don't set it explicitly, it will be automatically allocated. |
403 | 399 |
struct SetStandardProcessedMap |
404 | 400 |
: public Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > { |
405 | 401 |
typedef Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > |
406 | 402 |
Create; |
407 | 403 |
}; |
408 | 404 |
|
409 | 405 |
template <class H, class CR> |
410 | 406 |
struct SetHeapTraits : public Traits { |
411 | 407 |
typedef CR HeapCrossRef; |
412 | 408 |
typedef H Heap; |
413 | 409 |
static HeapCrossRef *createHeapCrossRef(const Digraph &) { |
414 | 410 |
throw UninitializedParameter(); |
415 | 411 |
} |
416 | 412 |
static Heap *createHeap(HeapCrossRef &) |
417 | 413 |
{ |
418 | 414 |
throw UninitializedParameter(); |
419 | 415 |
} |
420 | 416 |
}; |
421 | 417 |
///\brief \ref named-templ-param "Named parameter" for setting |
422 | 418 |
///heap and cross reference type |
423 | 419 |
/// |
424 | 420 |
///\ref named-templ-param "Named parameter" for setting heap and cross |
425 | 421 |
///reference type. |
426 | 422 |
template <class H, class CR = typename Digraph::template NodeMap<int> > |
427 | 423 |
struct SetHeap |
428 | 424 |
: public Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > { |
429 | 425 |
typedef Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > Create; |
430 | 426 |
}; |
431 | 427 |
|
432 | 428 |
template <class H, class CR> |
433 | 429 |
struct SetStandardHeapTraits : public Traits { |
434 | 430 |
typedef CR HeapCrossRef; |
435 | 431 |
typedef H Heap; |
436 | 432 |
static HeapCrossRef *createHeapCrossRef(const Digraph &G) { |
437 | 433 |
return new HeapCrossRef(G); |
438 | 434 |
} |
439 | 435 |
static Heap *createHeap(HeapCrossRef &R) |
440 | 436 |
{ |
441 | 437 |
return new Heap(R); |
442 | 438 |
} |
443 | 439 |
}; |
444 | 440 |
///\brief \ref named-templ-param "Named parameter" for setting |
445 | 441 |
///heap and cross reference type with automatic allocation |
446 | 442 |
/// |
447 | 443 |
///\ref named-templ-param "Named parameter" for setting heap and cross |
448 | 444 |
///reference type. It can allocate the heap and the cross reference |
449 | 445 |
///object if the cross reference's constructor waits for the digraph as |
450 | 446 |
///parameter and the heap's constructor waits for the cross reference. |
451 | 447 |
template <class H, class CR = typename Digraph::template NodeMap<int> > |
452 | 448 |
struct SetStandardHeap |
453 | 449 |
: public Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > { |
454 | 450 |
typedef Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > |
455 | 451 |
Create; |
456 | 452 |
}; |
457 | 453 |
|
458 | 454 |
template <class T> |
459 | 455 |
struct SetOperationTraitsTraits : public Traits { |
460 | 456 |
typedef T OperationTraits; |
461 | 457 |
}; |
462 | 458 |
|
463 | 459 |
/// \brief \ref named-templ-param "Named parameter" for setting |
464 | 460 |
///\ref OperationTraits type |
465 | 461 |
/// |
466 | 462 |
///\ref named-templ-param "Named parameter" for setting |
467 | 463 |
///\ref OperationTraits type. |
468 | 464 |
template <class T> |
469 | 465 |
struct SetOperationTraits |
470 | 466 |
: public Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > { |
471 | 467 |
typedef Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > |
472 | 468 |
Create; |
473 | 469 |
}; |
474 | 470 |
|
475 | 471 |
///@} |
476 | 472 |
|
477 | 473 |
protected: |
478 | 474 |
|
479 | 475 |
Dijkstra() {} |
480 | 476 |
|
481 | 477 |
public: |
482 | 478 |
|
483 | 479 |
///Constructor. |
484 | 480 |
|
485 | 481 |
///Constructor. |
486 | 482 |
///\param _g The digraph the algorithm runs on. |
487 | 483 |
///\param _length The length map used by the algorithm. |
488 | 484 |
Dijkstra(const Digraph& _g, const LengthMap& _length) : |
489 | 485 |
G(&_g), length(&_length), |
490 | 486 |
_pred(NULL), local_pred(false), |
491 | 487 |
_dist(NULL), local_dist(false), |
492 | 488 |
_processed(NULL), local_processed(false), |
493 | 489 |
_heap_cross_ref(NULL), local_heap_cross_ref(false), |
494 | 490 |
_heap(NULL), local_heap(false) |
495 | 491 |
{ } |
496 | 492 |
|
497 | 493 |
///Destructor. |
498 | 494 |
~Dijkstra() |
499 | 495 |
{ |
500 | 496 |
if(local_pred) delete _pred; |
501 | 497 |
if(local_dist) delete _dist; |
502 | 498 |
if(local_processed) delete _processed; |
503 | 499 |
if(local_heap_cross_ref) delete _heap_cross_ref; |
504 | 500 |
if(local_heap) delete _heap; |
505 | 501 |
} |
506 | 502 |
|
507 | 503 |
///Sets the length map. |
508 | 504 |
|
509 | 505 |
///Sets the length map. |
510 | 506 |
///\return <tt> (*this) </tt> |
511 | 507 |
Dijkstra &lengthMap(const LengthMap &m) |
512 | 508 |
{ |
513 | 509 |
length = &m; |
514 | 510 |
return *this; |
515 | 511 |
} |
516 | 512 |
|
517 | 513 |
///Sets the map that stores the predecessor arcs. |
518 | 514 |
|
519 | 515 |
///Sets the map that stores the predecessor arcs. |
520 | 516 |
///If you don't use this function before calling \ref run(), |
521 | 517 |
///it will allocate one. The destructor deallocates this |
522 | 518 |
///automatically allocated map, of course. |
523 | 519 |
///\return <tt> (*this) </tt> |
524 | 520 |
Dijkstra &predMap(PredMap &m) |
525 | 521 |
{ |
526 | 522 |
if(local_pred) { |
527 | 523 |
delete _pred; |
528 | 524 |
local_pred=false; |
529 | 525 |
} |
530 | 526 |
_pred = &m; |
531 | 527 |
return *this; |
532 | 528 |
} |
533 | 529 |
|
534 | 530 |
///Sets the map that indicates which nodes are processed. |
535 | 531 |
|
536 | 532 |
///Sets the map that indicates which nodes are processed. |
537 | 533 |
///If you don't use this function before calling \ref run(), |
538 | 534 |
///it will allocate one. The destructor deallocates this |
539 | 535 |
///automatically allocated map, of course. |
540 | 536 |
///\return <tt> (*this) </tt> |
541 | 537 |
Dijkstra &processedMap(ProcessedMap &m) |
542 | 538 |
{ |
543 | 539 |
if(local_processed) { |
544 | 540 |
delete _processed; |
545 | 541 |
local_processed=false; |
546 | 542 |
} |
547 | 543 |
_processed = &m; |
548 | 544 |
return *this; |
549 | 545 |
} |
550 | 546 |
|
551 | 547 |
///Sets the map that stores the distances of the nodes. |
552 | 548 |
|
553 | 549 |
///Sets the map that stores the distances of the nodes calculated by the |
554 | 550 |
///algorithm. |
555 | 551 |
///If you don't use this function before calling \ref run(), |
556 | 552 |
///it will allocate one. The destructor deallocates this |
557 | 553 |
///automatically allocated map, of course. |
558 | 554 |
///\return <tt> (*this) </tt> |
559 | 555 |
Dijkstra &distMap(DistMap &m) |
560 | 556 |
{ |
561 | 557 |
if(local_dist) { |
562 | 558 |
delete _dist; |
563 | 559 |
local_dist=false; |
564 | 560 |
} |
565 | 561 |
_dist = &m; |
566 | 562 |
return *this; |
567 | 563 |
} |
568 | 564 |
|
569 | 565 |
///Sets the heap and the cross reference used by algorithm. |
570 | 566 |
|
571 | 567 |
///Sets the heap and the cross reference used by algorithm. |
572 | 568 |
///If you don't use this function before calling \ref run(), |
573 | 569 |
///it will allocate one. The destructor deallocates this |
574 | 570 |
///automatically allocated heap and cross reference, of course. |
575 | 571 |
///\return <tt> (*this) </tt> |
576 | 572 |
Dijkstra &heap(Heap& hp, HeapCrossRef &cr) |
577 | 573 |
{ |
578 | 574 |
if(local_heap_cross_ref) { |
579 | 575 |
delete _heap_cross_ref; |
580 | 576 |
local_heap_cross_ref=false; |
581 | 577 |
} |
582 | 578 |
_heap_cross_ref = &cr; |
583 | 579 |
if(local_heap) { |
584 | 580 |
delete _heap; |
585 | 581 |
local_heap=false; |
586 | 582 |
} |
587 | 583 |
_heap = &hp; |
588 | 584 |
return *this; |
589 | 585 |
} |
590 | 586 |
|
591 | 587 |
private: |
592 | 588 |
|
593 | 589 |
void finalizeNodeData(Node v,Value dst) |
594 | 590 |
{ |
595 | 591 |
_processed->set(v,true); |
596 | 592 |
_dist->set(v, dst); |
597 | 593 |
} |
598 | 594 |
|
599 | 595 |
public: |
600 | 596 |
|
601 | 597 |
///\name Execution control |
602 | 598 |
///The simplest way to execute the algorithm is to use one of the |
603 | 599 |
///member functions called \ref lemon::Dijkstra::run() "run()". |
604 | 600 |
///\n |
605 | 601 |
///If you need more control on the execution, first you must call |
606 | 602 |
///\ref lemon::Dijkstra::init() "init()", then you can add several |
607 | 603 |
///source nodes with \ref lemon::Dijkstra::addSource() "addSource()". |
608 | 604 |
///Finally \ref lemon::Dijkstra::start() "start()" will perform the |
609 | 605 |
///actual path computation. |
610 | 606 |
|
611 | 607 |
///@{ |
612 | 608 |
|
613 | 609 |
///Initializes the internal data structures. |
614 | 610 |
|
615 | 611 |
///Initializes the internal data structures. |
616 | 612 |
/// |
617 | 613 |
void init() |
618 | 614 |
{ |
619 | 615 |
create_maps(); |
620 | 616 |
_heap->clear(); |
621 | 617 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
622 | 618 |
_pred->set(u,INVALID); |
623 | 619 |
_processed->set(u,false); |
624 | 620 |
_heap_cross_ref->set(u,Heap::PRE_HEAP); |
625 | 621 |
} |
626 | 622 |
} |
627 | 623 |
|
628 | 624 |
///Adds a new source node. |
629 | 625 |
|
630 | 626 |
///Adds a new source node to the priority heap. |
631 | 627 |
///The optional second parameter is the initial distance of the node. |
632 | 628 |
/// |
633 | 629 |
///The function checks if the node has already been added to the heap and |
634 | 630 |
///it is pushed to the heap only if either it was not in the heap |
635 | 631 |
///or the shortest path found till then is shorter than \c dst. |
636 | 632 |
void addSource(Node s,Value dst=OperationTraits::zero()) |
637 | 633 |
{ |
638 | 634 |
if(_heap->state(s) != Heap::IN_HEAP) { |
639 | 635 |
_heap->push(s,dst); |
640 | 636 |
} else if(OperationTraits::less((*_heap)[s], dst)) { |
641 | 637 |
_heap->set(s,dst); |
642 | 638 |
_pred->set(s,INVALID); |
643 | 639 |
} |
644 | 640 |
} |
645 | 641 |
|
646 | 642 |
///Processes the next node in the priority heap |
647 | 643 |
|
648 | 644 |
///Processes the next node in the priority heap. |
649 | 645 |
/// |
650 | 646 |
///\return The processed node. |
651 | 647 |
/// |
652 | 648 |
///\warning The priority heap must not be empty. |
653 | 649 |
Node processNextNode() |
654 | 650 |
{ |
655 | 651 |
Node v=_heap->top(); |
656 | 652 |
Value oldvalue=_heap->prio(); |
657 | 653 |
_heap->pop(); |
658 | 654 |
finalizeNodeData(v,oldvalue); |
659 | 655 |
|
660 | 656 |
for(OutArcIt e(*G,v); e!=INVALID; ++e) { |
661 | 657 |
Node w=G->target(e); |
662 | 658 |
switch(_heap->state(w)) { |
663 | 659 |
case Heap::PRE_HEAP: |
664 | 660 |
_heap->push(w,OperationTraits::plus(oldvalue, (*length)[e])); |
665 | 661 |
_pred->set(w,e); |
666 | 662 |
break; |
667 | 663 |
case Heap::IN_HEAP: |
668 | 664 |
{ |
669 | 665 |
Value newvalue = OperationTraits::plus(oldvalue, (*length)[e]); |
670 | 666 |
if ( OperationTraits::less(newvalue, (*_heap)[w]) ) { |
671 | 667 |
_heap->decrease(w, newvalue); |
672 | 668 |
_pred->set(w,e); |
673 | 669 |
} |
674 | 670 |
} |
675 | 671 |
break; |
676 | 672 |
case Heap::POST_HEAP: |
677 | 673 |
break; |
678 | 674 |
} |
679 | 675 |
} |
680 | 676 |
return v; |
681 | 677 |
} |
682 | 678 |
|
683 | 679 |
///The next node to be processed. |
684 | 680 |
|
685 | 681 |
///Returns the next node to be processed or \c INVALID if the |
686 | 682 |
///priority heap is empty. |
687 | 683 |
Node nextNode() const |
688 | 684 |
{ |
689 | 685 |
return !_heap->empty()?_heap->top():INVALID; |
690 | 686 |
} |
691 | 687 |
|
692 | 688 |
///\brief Returns \c false if there are nodes |
693 | 689 |
///to be processed. |
694 | 690 |
/// |
695 | 691 |
///Returns \c false if there are nodes |
696 | 692 |
///to be processed in the priority heap. |
697 | 693 |
bool emptyQueue() const { return _heap->empty(); } |
698 | 694 |
|
699 | 695 |
///Returns the number of the nodes to be processed in the priority heap |
700 | 696 |
|
701 | 697 |
///Returns the number of the nodes to be processed in the priority heap. |
702 | 698 |
/// |
703 | 699 |
int queueSize() const { return _heap->size(); } |
704 | 700 |
|
705 | 701 |
///Executes the algorithm. |
706 | 702 |
|
707 | 703 |
///Executes the algorithm. |
708 | 704 |
/// |
709 | 705 |
///This method runs the %Dijkstra algorithm from the root node(s) |
710 | 706 |
///in order to compute the shortest path to each node. |
711 | 707 |
/// |
712 | 708 |
///The algorithm computes |
713 | 709 |
///- the shortest path tree (forest), |
714 | 710 |
///- the distance of each node from the root(s). |
715 | 711 |
/// |
716 | 712 |
///\pre init() must be called and at least one root node should be |
717 | 713 |
///added with addSource() before using this function. |
718 | 714 |
/// |
719 | 715 |
///\note <tt>d.start()</tt> is just a shortcut of the following code. |
720 | 716 |
///\code |
721 | 717 |
/// while ( !d.emptyQueue() ) { |
722 | 718 |
/// d.processNextNode(); |
723 | 719 |
/// } |
724 | 720 |
///\endcode |
725 | 721 |
void start() |
726 | 722 |
{ |
727 | 723 |
while ( !emptyQueue() ) processNextNode(); |
728 | 724 |
} |
729 | 725 |
|
730 | 726 |
///Executes the algorithm until the given target node is reached. |
731 | 727 |
|
732 | 728 |
///Executes the algorithm until the given target node is reached. |
733 | 729 |
/// |
734 | 730 |
///This method runs the %Dijkstra algorithm from the root node(s) |
735 | 731 |
///in order to compute the shortest path to \c dest. |
736 | 732 |
/// |
737 | 733 |
///The algorithm computes |
738 | 734 |
///- the shortest path to \c dest, |
739 | 735 |
///- the distance of \c dest from the root(s). |
740 | 736 |
/// |
741 | 737 |
///\pre init() must be called and at least one root node should be |
742 | 738 |
///added with addSource() before using this function. |
743 | 739 |
void start(Node dest) |
744 | 740 |
{ |
745 | 741 |
while ( !_heap->empty() && _heap->top()!=dest ) processNextNode(); |
746 | 742 |
if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio()); |
747 | 743 |
} |
748 | 744 |
|
749 | 745 |
///Executes the algorithm until a condition is met. |
750 | 746 |
|
751 | 747 |
///Executes the algorithm until a condition is met. |
752 | 748 |
/// |
753 | 749 |
///This method runs the %Dijkstra algorithm from the root node(s) in |
754 | 750 |
///order to compute the shortest path to a node \c v with |
755 | 751 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
756 | 752 |
/// |
757 | 753 |
///\param nm A \c bool (or convertible) node map. The algorithm |
758 | 754 |
///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. |
759 | 755 |
/// |
760 | 756 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
761 | 757 |
///\c INVALID if no such node was found. |
762 | 758 |
/// |
763 | 759 |
///\pre init() must be called and at least one root node should be |
764 | 760 |
///added with addSource() before using this function. |
765 | 761 |
template<class NodeBoolMap> |
766 | 762 |
Node start(const NodeBoolMap &nm) |
767 | 763 |
{ |
768 | 764 |
while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode(); |
769 | 765 |
if ( _heap->empty() ) return INVALID; |
770 | 766 |
finalizeNodeData(_heap->top(),_heap->prio()); |
771 | 767 |
return _heap->top(); |
772 | 768 |
} |
773 | 769 |
|
774 | 770 |
///Runs the algorithm from the given node. |
775 | 771 |
|
776 | 772 |
///This method runs the %Dijkstra algorithm from node \c s |
777 | 773 |
///in order to compute the shortest path to each node. |
778 | 774 |
/// |
779 | 775 |
///The algorithm computes |
780 | 776 |
///- the shortest path tree, |
781 | 777 |
///- the distance of each node from the root. |
782 | 778 |
/// |
783 | 779 |
///\note <tt>d.run(s)</tt> is just a shortcut of the following code. |
784 | 780 |
///\code |
785 | 781 |
/// d.init(); |
786 | 782 |
/// d.addSource(s); |
787 | 783 |
/// d.start(); |
788 | 784 |
///\endcode |
789 | 785 |
void run(Node s) { |
790 | 786 |
init(); |
791 | 787 |
addSource(s); |
792 | 788 |
start(); |
793 | 789 |
} |
794 | 790 |
|
795 | 791 |
///Finds the shortest path between \c s and \c t. |
796 | 792 |
|
797 | 793 |
///This method runs the %Dijkstra algorithm from node \c s |
798 | 794 |
///in order to compute the shortest path to \c t. |
799 | 795 |
/// |
800 | 796 |
///\return The length of the shortest <tt>s</tt>--<tt>t</tt> path, |
801 | 797 |
///if \c t is reachable form \c s, \c 0 otherwise. |
802 | 798 |
/// |
803 | 799 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is just a |
804 | 800 |
///shortcut of the following code. |
805 | 801 |
///\code |
806 | 802 |
/// d.init(); |
807 | 803 |
/// d.addSource(s); |
808 | 804 |
/// d.start(t); |
809 | 805 |
///\endcode |
810 | 806 |
Value run(Node s,Node t) { |
811 | 807 |
init(); |
812 | 808 |
addSource(s); |
813 | 809 |
start(t); |
814 | 810 |
return (*_pred)[t]==INVALID?OperationTraits::zero():(*_dist)[t]; |
815 | 811 |
} |
816 | 812 |
|
817 | 813 |
///@} |
818 | 814 |
|
819 | 815 |
///\name Query Functions |
820 | 816 |
///The result of the %Dijkstra algorithm can be obtained using these |
821 | 817 |
///functions.\n |
822 | 818 |
///Either \ref lemon::Dijkstra::run() "run()" or |
823 | 819 |
///\ref lemon::Dijkstra::start() "start()" must be called before |
824 | 820 |
///using them. |
825 | 821 |
|
826 | 822 |
///@{ |
827 | 823 |
|
828 | 824 |
///The shortest path to a node. |
829 | 825 |
|
830 | 826 |
///Returns the shortest path to a node. |
831 | 827 |
/// |
832 | 828 |
///\warning \c t should be reachable from the root(s). |
833 | 829 |
/// |
834 | 830 |
///\pre Either \ref run() or \ref start() must be called before |
835 | 831 |
///using this function. |
836 | 832 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
837 | 833 |
|
838 | 834 |
///The distance of a node from the root(s). |
839 | 835 |
|
840 | 836 |
///Returns the distance of a node from the root(s). |
841 | 837 |
/// |
842 | 838 |
///\warning If node \c v is not reachable from the root(s), then |
843 | 839 |
///the return value of this function is undefined. |
844 | 840 |
/// |
845 | 841 |
///\pre Either \ref run() or \ref start() must be called before |
846 | 842 |
///using this function. |
847 | 843 |
Value dist(Node v) const { return (*_dist)[v]; } |
848 | 844 |
|
849 | 845 |
///Returns the 'previous arc' of the shortest path tree for a node. |
850 | 846 |
|
851 | 847 |
///This function returns the 'previous arc' of the shortest path |
852 | 848 |
///tree for the node \c v, i.e. it returns the last arc of a |
853 | 849 |
///shortest path from the root(s) to \c v. It is \c INVALID if \c v |
854 | 850 |
///is not reachable from the root(s) or if \c v is a root. |
855 | 851 |
/// |
856 | 852 |
///The shortest path tree used here is equal to the shortest path |
857 | 853 |
///tree used in \ref predNode(). |
858 | 854 |
/// |
859 | 855 |
///\pre Either \ref run() or \ref start() must be called before |
860 | 856 |
///using this function. |
861 | 857 |
Arc predArc(Node v) const { return (*_pred)[v]; } |
862 | 858 |
|
863 | 859 |
///Returns the 'previous node' of the shortest path tree for a node. |
864 | 860 |
|
865 | 861 |
///This function returns the 'previous node' of the shortest path |
866 | 862 |
///tree for the node \c v, i.e. it returns the last but one node |
867 | 863 |
///from a shortest path from the root(s) to \c v. It is \c INVALID |
868 | 864 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
869 | 865 |
/// |
870 | 866 |
///The shortest path tree used here is equal to the shortest path |
871 | 867 |
///tree used in \ref predArc(). |
872 | 868 |
/// |
873 | 869 |
///\pre Either \ref run() or \ref start() must be called before |
874 | 870 |
///using this function. |
875 | 871 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
876 | 872 |
G->source((*_pred)[v]); } |
877 | 873 |
|
878 | 874 |
///\brief Returns a const reference to the node map that stores the |
879 | 875 |
///distances of the nodes. |
880 | 876 |
/// |
881 | 877 |
///Returns a const reference to the node map that stores the distances |
882 | 878 |
///of the nodes calculated by the algorithm. |
883 | 879 |
/// |
884 | 880 |
///\pre Either \ref run() or \ref init() |
885 | 881 |
///must be called before using this function. |
886 | 882 |
const DistMap &distMap() const { return *_dist;} |
887 | 883 |
|
888 | 884 |
///\brief Returns a const reference to the node map that stores the |
889 | 885 |
///predecessor arcs. |
890 | 886 |
/// |
891 | 887 |
///Returns a const reference to the node map that stores the predecessor |
892 | 888 |
///arcs, which form the shortest path tree. |
893 | 889 |
/// |
894 | 890 |
///\pre Either \ref run() or \ref init() |
895 | 891 |
///must be called before using this function. |
896 | 892 |
const PredMap &predMap() const { return *_pred;} |
897 | 893 |
|
898 | 894 |
///Checks if a node is reachable from the root(s). |
899 | 895 |
|
900 | 896 |
///Returns \c true if \c v is reachable from the root(s). |
901 | 897 |
///\pre Either \ref run() or \ref start() |
902 | 898 |
///must be called before using this function. |
903 | 899 |
bool reached(Node v) const { return (*_heap_cross_ref)[v] != |
904 | 900 |
Heap::PRE_HEAP; } |
905 | 901 |
|
906 | 902 |
///Checks if a node is processed. |
907 | 903 |
|
908 | 904 |
///Returns \c true if \c v is processed, i.e. the shortest |
909 | 905 |
///path to \c v has already found. |
910 | 906 |
///\pre Either \ref run() or \ref start() |
911 | 907 |
///must be called before using this function. |
912 | 908 |
bool processed(Node v) const { return (*_heap_cross_ref)[v] == |
913 | 909 |
Heap::POST_HEAP; } |
914 | 910 |
|
915 | 911 |
///The current distance of a node from the root(s). |
916 | 912 |
|
917 | 913 |
///Returns the current distance of a node from the root(s). |
918 | 914 |
///It may be decreased in the following processes. |
919 | 915 |
///\pre \c v should be reached but not processed. |
920 | 916 |
Value currentDist(Node v) const { return (*_heap)[v]; } |
921 | 917 |
|
922 | 918 |
///@} |
923 | 919 |
}; |
924 | 920 |
|
925 | 921 |
|
926 | 922 |
///Default traits class of dijkstra() function. |
927 | 923 |
|
928 | 924 |
///Default traits class of dijkstra() function. |
929 | 925 |
///\tparam GR The type of the digraph. |
930 | 926 |
///\tparam LM The type of the length map. |
931 | 927 |
template<class GR, class LM> |
932 | 928 |
struct DijkstraWizardDefaultTraits |
933 | 929 |
{ |
934 | 930 |
///The type of the digraph the algorithm runs on. |
935 | 931 |
typedef GR Digraph; |
936 | 932 |
///The type of the map that stores the arc lengths. |
937 | 933 |
|
938 | 934 |
///The type of the map that stores the arc lengths. |
939 | 935 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
940 | 936 |
typedef LM LengthMap; |
941 | 937 |
///The type of the length of the arcs. |
942 | 938 |
typedef typename LM::Value Value; |
943 | 939 |
|
944 | 940 |
/// Operation traits for Dijkstra algorithm. |
945 | 941 |
|
946 | 942 |
/// This class defines the operations that are used in the algorithm. |
947 | 943 |
/// \see DijkstraDefaultOperationTraits |
948 | 944 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
949 | 945 |
|
950 | 946 |
/// The cross reference type used by the heap. |
951 | 947 |
|
952 | 948 |
/// The cross reference type used by the heap. |
953 | 949 |
/// Usually it is \c Digraph::NodeMap<int>. |
954 | 950 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
955 | 951 |
///Instantiates a \ref HeapCrossRef. |
956 | 952 |
|
957 | 953 |
///This function instantiates a \ref HeapCrossRef. |
958 | 954 |
/// \param g is the digraph, to which we would like to define the |
959 | 955 |
/// HeapCrossRef. |
960 |
/// \todo The digraph alone may be insufficient for the initialization |
|
961 | 956 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
962 | 957 |
{ |
963 | 958 |
return new HeapCrossRef(g); |
964 | 959 |
} |
965 | 960 |
|
966 | 961 |
///The heap type used by the Dijkstra algorithm. |
967 | 962 |
|
968 | 963 |
///The heap type used by the Dijkstra algorithm. |
969 | 964 |
/// |
970 | 965 |
///\sa BinHeap |
971 | 966 |
///\sa Dijkstra |
972 | 967 |
typedef BinHeap<Value, typename Digraph::template NodeMap<int>, |
973 | 968 |
std::less<Value> > Heap; |
974 | 969 |
|
975 | 970 |
///Instantiates a \ref Heap. |
976 | 971 |
|
977 | 972 |
///This function instantiates a \ref Heap. |
978 | 973 |
/// \param r is the HeapCrossRef which is used. |
979 | 974 |
static Heap *createHeap(HeapCrossRef& r) |
980 | 975 |
{ |
981 | 976 |
return new Heap(r); |
982 | 977 |
} |
983 | 978 |
|
984 | 979 |
///\brief The type of the map that stores the predecessor |
985 | 980 |
///arcs of the shortest paths. |
986 | 981 |
/// |
987 | 982 |
///The type of the map that stores the predecessor |
988 | 983 |
///arcs of the shortest paths. |
989 | 984 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
990 | 985 |
typedef NullMap <typename Digraph::Node,typename Digraph::Arc> PredMap; |
991 | 986 |
///Instantiates a \ref PredMap. |
992 | 987 |
|
993 | 988 |
///This function instantiates a \ref PredMap. |
994 | 989 |
///\param g is the digraph, to which we would like to define the |
995 | 990 |
///\ref PredMap. |
996 |
///\todo The digraph alone may be insufficient to initialize |
|
997 | 991 |
#ifdef DOXYGEN |
998 | 992 |
static PredMap *createPredMap(const Digraph &g) |
999 | 993 |
#else |
1000 | 994 |
static PredMap *createPredMap(const Digraph &) |
1001 | 995 |
#endif |
1002 | 996 |
{ |
1003 | 997 |
return new PredMap(); |
1004 | 998 |
} |
1005 | 999 |
|
1006 | 1000 |
///The type of the map that indicates which nodes are processed. |
1007 | 1001 |
|
1008 | 1002 |
///The type of the map that indicates which nodes are processed. |
1009 | 1003 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
1010 | 1004 |
///By default it is a NullMap. |
1011 |
///\todo If it is set to a real map, |
|
1012 |
///Dijkstra::processed() should read this. |
|
1013 |
///\todo named parameter to set this type, function to read and write. |
|
1014 | 1005 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
1015 | 1006 |
///Instantiates a \ref ProcessedMap. |
1016 | 1007 |
|
1017 | 1008 |
///This function instantiates a \ref ProcessedMap. |
1018 | 1009 |
///\param g is the digraph, to which |
1019 | 1010 |
///we would like to define the \ref ProcessedMap. |
1020 | 1011 |
#ifdef DOXYGEN |
1021 | 1012 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
1022 | 1013 |
#else |
1023 | 1014 |
static ProcessedMap *createProcessedMap(const Digraph &) |
1024 | 1015 |
#endif |
1025 | 1016 |
{ |
1026 | 1017 |
return new ProcessedMap(); |
1027 | 1018 |
} |
1028 | 1019 |
|
1029 | 1020 |
///The type of the map that stores the distances of the nodes. |
1030 | 1021 |
|
1031 | 1022 |
///The type of the map that stores the distances of the nodes. |
1032 | 1023 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
1033 | 1024 |
typedef NullMap<typename Digraph::Node,Value> DistMap; |
1034 | 1025 |
///Instantiates a \ref DistMap. |
1035 | 1026 |
|
1036 | 1027 |
///This function instantiates a \ref DistMap. |
1037 | 1028 |
///\param g is the digraph, to which we would like to define |
1038 | 1029 |
///the \ref DistMap |
1039 | 1030 |
#ifdef DOXYGEN |
1040 | 1031 |
static DistMap *createDistMap(const Digraph &g) |
1041 | 1032 |
#else |
1042 | 1033 |
static DistMap *createDistMap(const Digraph &) |
1043 | 1034 |
#endif |
1044 | 1035 |
{ |
1045 | 1036 |
return new DistMap(); |
1046 | 1037 |
} |
1047 | 1038 |
}; |
1048 | 1039 |
|
1049 | 1040 |
/// Default traits class used by \ref DijkstraWizard |
1050 | 1041 |
|
1051 | 1042 |
/// To make it easier to use Dijkstra algorithm |
1052 | 1043 |
/// we have created a wizard class. |
1053 | 1044 |
/// This \ref DijkstraWizard class needs default traits, |
1054 | 1045 |
/// as well as the \ref Dijkstra class. |
1055 | 1046 |
/// The \ref DijkstraWizardBase is a class to be the default traits of the |
1056 | 1047 |
/// \ref DijkstraWizard class. |
1057 |
/// \todo More named parameters are required... |
|
1058 | 1048 |
template<class GR,class LM> |
1059 | 1049 |
class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM> |
1060 | 1050 |
{ |
1061 | 1051 |
typedef DijkstraWizardDefaultTraits<GR,LM> Base; |
1062 | 1052 |
protected: |
1063 | 1053 |
//The type of the nodes in the digraph. |
1064 | 1054 |
typedef typename Base::Digraph::Node Node; |
1065 | 1055 |
|
1066 | 1056 |
//Pointer to the digraph the algorithm runs on. |
1067 | 1057 |
void *_g; |
1068 | 1058 |
//Pointer to the length map |
1069 | 1059 |
void *_length; |
1070 | 1060 |
//Pointer to the map of processed nodes. |
1071 | 1061 |
void *_processed; |
1072 | 1062 |
//Pointer to the map of predecessors arcs. |
1073 | 1063 |
void *_pred; |
1074 | 1064 |
//Pointer to the map of distances. |
1075 | 1065 |
void *_dist; |
1076 | 1066 |
//Pointer to the source node. |
1077 | 1067 |
Node _source; |
1078 | 1068 |
|
1079 | 1069 |
public: |
1080 | 1070 |
/// Constructor. |
1081 | 1071 |
|
1082 | 1072 |
/// This constructor does not require parameters, therefore it initiates |
1083 | 1073 |
/// all of the attributes to default values (0, INVALID). |
1084 | 1074 |
DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0), |
1085 | 1075 |
_dist(0), _source(INVALID) {} |
1086 | 1076 |
|
1087 | 1077 |
/// Constructor. |
1088 | 1078 |
|
1089 | 1079 |
/// This constructor requires some parameters, |
1090 | 1080 |
/// listed in the parameters list. |
1091 | 1081 |
/// Others are initiated to 0. |
1092 | 1082 |
/// \param g The digraph the algorithm runs on. |
1093 | 1083 |
/// \param l The length map. |
1094 | 1084 |
/// \param s The source node. |
1095 | 1085 |
DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) : |
1096 | 1086 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
1097 | 1087 |
_length(reinterpret_cast<void*>(const_cast<LM*>(&l))), |
1098 | 1088 |
_processed(0), _pred(0), _dist(0), _source(s) {} |
1099 | 1089 |
|
1100 | 1090 |
}; |
1101 | 1091 |
|
1102 | 1092 |
/// Auxiliary class for the function type interface of Dijkstra algorithm. |
1103 | 1093 |
|
1104 | 1094 |
/// This auxiliary class is created to implement the function type |
1105 | 1095 |
/// interface of \ref Dijkstra algorithm. It uses the functions and features |
1106 | 1096 |
/// of the plain \ref Dijkstra, but it is much simpler to use it. |
1107 | 1097 |
/// It should only be used through the \ref dijkstra() function, which makes |
1108 | 1098 |
/// it easier to use the algorithm. |
1109 | 1099 |
/// |
1110 | 1100 |
/// Simplicity means that the way to change the types defined |
1111 | 1101 |
/// in the traits class is based on functions that returns the new class |
1112 | 1102 |
/// and not on templatable built-in classes. |
1113 | 1103 |
/// When using the plain \ref Dijkstra |
1114 | 1104 |
/// the new class with the modified type comes from |
1115 | 1105 |
/// the original class by using the :: |
1116 | 1106 |
/// operator. In the case of \ref DijkstraWizard only |
1117 | 1107 |
/// a function have to be called, and it will |
1118 | 1108 |
/// return the needed class. |
1119 | 1109 |
/// |
1120 | 1110 |
/// It does not have own \ref run() method. When its \ref run() method |
1121 | 1111 |
/// is called, it initiates a plain \ref Dijkstra object, and calls the |
1122 | 1112 |
/// \ref Dijkstra::run() method of it. |
1123 | 1113 |
template<class TR> |
1124 | 1114 |
class DijkstraWizard : public TR |
1125 | 1115 |
{ |
1126 | 1116 |
typedef TR Base; |
1127 | 1117 |
|
1128 | 1118 |
///The type of the digraph the algorithm runs on. |
1129 | 1119 |
typedef typename TR::Digraph Digraph; |
1130 | 1120 |
|
1131 | 1121 |
typedef typename Digraph::Node Node; |
1132 | 1122 |
typedef typename Digraph::NodeIt NodeIt; |
1133 | 1123 |
typedef typename Digraph::Arc Arc; |
1134 | 1124 |
typedef typename Digraph::OutArcIt OutArcIt; |
1135 | 1125 |
|
1136 | 1126 |
///The type of the map that stores the arc lengths. |
1137 | 1127 |
typedef typename TR::LengthMap LengthMap; |
1138 | 1128 |
///The type of the length of the arcs. |
1139 | 1129 |
typedef typename LengthMap::Value Value; |
1140 | 1130 |
///\brief The type of the map that stores the predecessor |
1141 | 1131 |
///arcs of the shortest paths. |
1142 | 1132 |
typedef typename TR::PredMap PredMap; |
1143 | 1133 |
///The type of the map that stores the distances of the nodes. |
1144 | 1134 |
typedef typename TR::DistMap DistMap; |
1145 | 1135 |
///The type of the map that indicates which nodes are processed. |
1146 | 1136 |
typedef typename TR::ProcessedMap ProcessedMap; |
1147 | 1137 |
///The heap type used by the dijkstra algorithm. |
1148 | 1138 |
typedef typename TR::Heap Heap; |
1149 | 1139 |
|
1150 | 1140 |
public: |
1151 | 1141 |
|
1152 | 1142 |
/// Constructor. |
1153 | 1143 |
DijkstraWizard() : TR() {} |
1154 | 1144 |
|
1155 | 1145 |
/// Constructor that requires parameters. |
1156 | 1146 |
|
1157 | 1147 |
/// Constructor that requires parameters. |
1158 | 1148 |
/// These parameters will be the default values for the traits class. |
1159 | 1149 |
DijkstraWizard(const Digraph &g,const LengthMap &l, Node s=INVALID) : |
1160 | 1150 |
TR(g,l,s) {} |
1161 | 1151 |
|
1162 | 1152 |
///Copy constructor |
1163 | 1153 |
DijkstraWizard(const TR &b) : TR(b) {} |
1164 | 1154 |
|
1165 | 1155 |
~DijkstraWizard() {} |
1166 | 1156 |
|
1167 | 1157 |
///Runs Dijkstra algorithm from a source node. |
1168 | 1158 |
|
1169 | 1159 |
///Runs Dijkstra algorithm from a source node. |
1170 | 1160 |
///The node can be given with the \ref source() function. |
1171 | 1161 |
void run() |
1172 | 1162 |
{ |
1173 | 1163 |
if(Base::_source==INVALID) throw UninitializedParameter(); |
1174 | 1164 |
Dijkstra<Digraph,LengthMap,TR> |
1175 | 1165 |
dij(*reinterpret_cast<const Digraph*>(Base::_g), |
1176 | 1166 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
1177 | 1167 |
if(Base::_processed) |
1178 | 1168 |
dij.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
1179 | 1169 |
if(Base::_pred) |
1180 | 1170 |
dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
1181 | 1171 |
if(Base::_dist) |
1182 | 1172 |
dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
1183 | 1173 |
dij.run(Base::_source); |
1184 | 1174 |
} |
1185 | 1175 |
|
1186 | 1176 |
///Runs Dijkstra algorithm from the given node. |
1187 | 1177 |
|
1188 | 1178 |
///Runs Dijkstra algorithm from the given node. |
1189 | 1179 |
///\param s is the given source. |
1190 | 1180 |
void run(Node s) |
1191 | 1181 |
{ |
1192 | 1182 |
Base::_source=s; |
1193 | 1183 |
run(); |
1194 | 1184 |
} |
1195 | 1185 |
|
1196 | 1186 |
/// Sets the source node, from which the Dijkstra algorithm runs. |
1197 | 1187 |
|
1198 | 1188 |
/// Sets the source node, from which the Dijkstra algorithm runs. |
1199 | 1189 |
/// \param s is the source node. |
1200 | 1190 |
DijkstraWizard<TR> &source(Node s) |
1201 | 1191 |
{ |
1202 | 1192 |
Base::_source=s; |
1203 | 1193 |
return *this; |
1204 | 1194 |
} |
1205 | 1195 |
|
1206 | 1196 |
template<class T> |
1207 | 1197 |
struct SetPredMapBase : public Base { |
1208 | 1198 |
typedef T PredMap; |
1209 | 1199 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1210 | 1200 |
SetPredMapBase(const TR &b) : TR(b) {} |
1211 | 1201 |
}; |
1212 | 1202 |
///\brief \ref named-templ-param "Named parameter" |
1213 | 1203 |
///for setting \ref PredMap object. |
1214 | 1204 |
/// |
1215 | 1205 |
///\ref named-templ-param "Named parameter" |
1216 | 1206 |
///for setting \ref PredMap object. |
1217 | 1207 |
template<class T> |
1218 | 1208 |
DijkstraWizard<SetPredMapBase<T> > predMap(const T &t) |
1219 | 1209 |
{ |
1220 | 1210 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1221 | 1211 |
return DijkstraWizard<SetPredMapBase<T> >(*this); |
1222 | 1212 |
} |
1223 | 1213 |
|
1224 | 1214 |
template<class T> |
1225 | 1215 |
struct SetProcessedMapBase : public Base { |
1226 | 1216 |
typedef T ProcessedMap; |
1227 | 1217 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1228 | 1218 |
SetProcessedMapBase(const TR &b) : TR(b) {} |
1229 | 1219 |
}; |
1230 | 1220 |
///\brief \ref named-templ-param "Named parameter" |
1231 | 1221 |
///for setting \ref ProcessedMap object. |
1232 | 1222 |
/// |
1233 | 1223 |
/// \ref named-templ-param "Named parameter" |
1234 | 1224 |
///for setting \ref ProcessedMap object. |
1235 | 1225 |
template<class T> |
1236 | 1226 |
DijkstraWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
1237 | 1227 |
{ |
1238 | 1228 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1239 | 1229 |
return DijkstraWizard<SetProcessedMapBase<T> >(*this); |
1240 | 1230 |
} |
1241 | 1231 |
|
1242 | 1232 |
template<class T> |
1243 | 1233 |
struct SetDistMapBase : public Base { |
1244 | 1234 |
typedef T DistMap; |
1245 | 1235 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1246 | 1236 |
SetDistMapBase(const TR &b) : TR(b) {} |
1247 | 1237 |
}; |
1248 | 1238 |
///\brief \ref named-templ-param "Named parameter" |
1249 | 1239 |
///for setting \ref DistMap object. |
1250 | 1240 |
/// |
1251 | 1241 |
///\ref named-templ-param "Named parameter" |
1252 | 1242 |
///for setting \ref DistMap object. |
1253 | 1243 |
template<class T> |
1254 | 1244 |
DijkstraWizard<SetDistMapBase<T> > distMap(const T &t) |
1255 | 1245 |
{ |
1256 | 1246 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1257 | 1247 |
return DijkstraWizard<SetDistMapBase<T> >(*this); |
1258 | 1248 |
} |
1259 | 1249 |
|
1260 | 1250 |
}; |
1261 | 1251 |
|
1262 | 1252 |
///Function type interface for Dijkstra algorithm. |
1263 | 1253 |
|
1264 | 1254 |
/// \ingroup shortest_path |
1265 | 1255 |
///Function type interface for Dijkstra algorithm. |
1266 | 1256 |
/// |
1267 | 1257 |
///This function also has several |
1268 | 1258 |
///\ref named-templ-func-param "named parameters", |
1269 | 1259 |
///they are declared as the members of class \ref DijkstraWizard. |
1270 | 1260 |
///The following |
1271 | 1261 |
///example shows how to use these parameters. |
1272 | 1262 |
///\code |
1273 | 1263 |
/// dijkstra(g,length,source).predMap(preds).run(); |
1274 | 1264 |
///\endcode |
1275 | 1265 |
///\warning Don't forget to put the \ref DijkstraWizard::run() "run()" |
1276 | 1266 |
///to the end of the parameter list. |
1277 | 1267 |
///\sa DijkstraWizard |
1278 | 1268 |
///\sa Dijkstra |
1279 | 1269 |
template<class GR, class LM> |
1280 | 1270 |
DijkstraWizard<DijkstraWizardBase<GR,LM> > |
1281 | 1271 |
dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID) |
1282 | 1272 |
{ |
1283 | 1273 |
return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s); |
1284 | 1274 |
} |
1285 | 1275 |
|
1286 | 1276 |
} //END OF NAMESPACE LEMON |
1287 | 1277 |
|
1288 | 1278 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-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_ERROR_H |
20 | 20 |
#define LEMON_ERROR_H |
21 | 21 |
|
22 | 22 |
/// \ingroup exceptions |
23 | 23 |
/// \file |
24 | 24 |
/// \brief Basic exception classes and error handling. |
25 | 25 |
|
26 | 26 |
#include <exception> |
27 | 27 |
#include <string> |
28 | 28 |
#include <sstream> |
29 | 29 |
#include <iostream> |
30 | 30 |
#include <cstdlib> |
31 | 31 |
#include <memory> |
32 | 32 |
|
33 | 33 |
namespace lemon { |
34 | 34 |
|
35 | 35 |
/// \addtogroup exceptions |
36 | 36 |
/// @{ |
37 | 37 |
|
38 | 38 |
/// \brief Exception safe wrapper class. |
39 | 39 |
/// |
40 | 40 |
/// Exception safe wrapper class to implement the members of exceptions. |
41 | 41 |
template <typename _Type> |
42 | 42 |
class ExceptionMember { |
43 | 43 |
public: |
44 | 44 |
typedef _Type Type; |
45 | 45 |
|
46 | 46 |
ExceptionMember() throw() { |
47 | 47 |
try { |
48 | 48 |
ptr.reset(new Type()); |
49 | 49 |
} catch (...) {} |
50 | 50 |
} |
51 | 51 |
|
52 | 52 |
ExceptionMember(const Type& type) throw() { |
53 | 53 |
try { |
54 | 54 |
ptr.reset(new Type()); |
55 | 55 |
if (ptr.get() == 0) return; |
56 | 56 |
*ptr = type; |
57 | 57 |
} catch (...) {} |
58 | 58 |
} |
59 | 59 |
|
60 | 60 |
ExceptionMember(const ExceptionMember& copy) throw() { |
61 | 61 |
try { |
62 | 62 |
if (!copy.valid()) return; |
63 | 63 |
ptr.reset(new Type()); |
64 | 64 |
if (ptr.get() == 0) return; |
65 | 65 |
*ptr = copy.get(); |
66 | 66 |
} catch (...) {} |
67 | 67 |
} |
68 | 68 |
|
69 | 69 |
ExceptionMember& operator=(const ExceptionMember& copy) throw() { |
70 | 70 |
if (ptr.get() == 0) return; |
71 | 71 |
try { |
72 | 72 |
if (!copy.valid()) return; |
73 | 73 |
*ptr = copy.get(); |
74 | 74 |
} catch (...) {} |
75 | 75 |
} |
76 | 76 |
|
77 | 77 |
void set(const Type& type) throw() { |
78 | 78 |
if (ptr.get() == 0) return; |
79 | 79 |
try { |
80 | 80 |
*ptr = type; |
81 | 81 |
} catch (...) {} |
82 | 82 |
} |
83 | 83 |
|
84 | 84 |
const Type& get() const { |
85 | 85 |
return *ptr; |
86 | 86 |
} |
87 | 87 |
|
88 | 88 |
bool valid() const throw() { |
89 | 89 |
return ptr.get() != 0; |
90 | 90 |
} |
91 | 91 |
|
92 | 92 |
private: |
93 | 93 |
std::auto_ptr<_Type> ptr; |
94 | 94 |
}; |
95 | 95 |
|
96 | 96 |
/// Exception-safe convenient error message builder class. |
97 | 97 |
|
98 | 98 |
/// Helper class which provides a convenient ostream-like (operator << |
99 | 99 |
/// based) interface to create a string message. Mostly useful in |
100 | 100 |
/// exception classes (therefore the name). |
101 | 101 |
class ErrorMessage { |
102 | 102 |
protected: |
103 | 103 |
///\e |
104 | 104 |
|
105 |
///\todo The good solution is boost::shared_ptr... |
|
106 |
/// |
|
107 | 105 |
mutable std::auto_ptr<std::ostringstream> buf; |
108 | 106 |
|
109 | 107 |
///\e |
110 | 108 |
bool init() throw() { |
111 | 109 |
try { |
112 | 110 |
buf.reset(new std::ostringstream); |
113 | 111 |
} |
114 | 112 |
catch(...) { |
115 | 113 |
buf.reset(); |
116 | 114 |
} |
117 | 115 |
return buf.get(); |
118 | 116 |
} |
119 | 117 |
|
120 | 118 |
public: |
121 | 119 |
|
122 | 120 |
///\e |
123 | 121 |
ErrorMessage() throw() { init(); } |
124 | 122 |
|
125 | 123 |
ErrorMessage(const ErrorMessage& em) throw() : buf(em.buf) { } |
126 | 124 |
|
127 | 125 |
///\e |
128 | 126 |
ErrorMessage(const char *msg) throw() { |
129 | 127 |
init(); |
130 | 128 |
*this << msg; |
131 | 129 |
} |
132 | 130 |
|
133 | 131 |
///\e |
134 | 132 |
ErrorMessage(const std::string &msg) throw() { |
135 | 133 |
init(); |
136 | 134 |
*this << msg; |
137 | 135 |
} |
138 | 136 |
|
139 | 137 |
///\e |
140 | 138 |
template <typename T> |
141 | 139 |
ErrorMessage& operator<<(const T &t) throw() { |
142 | 140 |
if( ! buf.get() ) return *this; |
143 | 141 |
|
144 | 142 |
try { |
145 | 143 |
*buf << t; |
146 | 144 |
} |
147 | 145 |
catch(...) { |
148 | 146 |
buf.reset(); |
149 | 147 |
} |
150 | 148 |
return *this; |
151 | 149 |
} |
152 | 150 |
|
153 | 151 |
///\e |
154 | 152 |
const char* message() throw() { |
155 | 153 |
if( ! buf.get() ) return 0; |
156 | 154 |
|
157 | 155 |
const char* mes = 0; |
158 | 156 |
try { |
159 | 157 |
mes = buf->str().c_str(); |
160 | 158 |
} |
161 | 159 |
catch(...) {} |
162 | 160 |
return mes; |
163 | 161 |
} |
164 | 162 |
|
165 | 163 |
}; |
166 | 164 |
|
167 | 165 |
/// Generic exception class. |
168 | 166 |
|
169 | 167 |
/// Base class for exceptions used in LEMON. |
170 | 168 |
/// |
171 | 169 |
class Exception : public std::exception { |
172 | 170 |
public: |
173 | 171 |
///\e |
174 | 172 |
Exception() {} |
175 | 173 |
///\e |
176 | 174 |
virtual ~Exception() throw() {} |
177 | 175 |
///\e |
178 | 176 |
virtual const char* what() const throw() { |
179 | 177 |
return "lemon::Exception"; |
180 | 178 |
} |
181 | 179 |
}; |
182 | 180 |
|
183 | 181 |
/// One of the two main subclasses of \ref Exception. |
184 | 182 |
|
185 | 183 |
/// Logic errors represent problems in the internal logic of a program; |
186 | 184 |
/// in theory, these are preventable, and even detectable before the |
187 | 185 |
/// program runs (e.g. violations of class invariants). |
188 | 186 |
/// |
189 | 187 |
/// A typical example for this is \ref UninitializedParameter. |
190 | 188 |
class LogicError : public Exception { |
191 | 189 |
public: |
192 | 190 |
virtual const char* what() const throw() { |
193 | 191 |
return "lemon::LogicError"; |
194 | 192 |
} |
195 | 193 |
}; |
196 | 194 |
|
197 | 195 |
/// \ref Exception for uninitialized parameters. |
198 | 196 |
|
199 | 197 |
/// This error represents problems in the initialization |
200 | 198 |
/// of the parameters of the algorithms. |
201 | 199 |
class UninitializedParameter : public LogicError { |
202 | 200 |
public: |
203 | 201 |
virtual const char* what() const throw() { |
204 | 202 |
return "lemon::UninitializedParameter"; |
205 | 203 |
} |
206 | 204 |
}; |
207 | 205 |
|
208 | 206 |
|
209 | 207 |
/// One of the two main subclasses of \ref Exception. |
210 | 208 |
|
211 | 209 |
/// Runtime errors represent problems outside the scope of a program; |
212 | 210 |
/// they cannot be easily predicted and can generally only be caught |
213 | 211 |
/// as the program executes. |
214 | 212 |
class RuntimeError : public Exception { |
215 | 213 |
public: |
216 | 214 |
virtual const char* what() const throw() { |
217 | 215 |
return "lemon::RuntimeError"; |
218 | 216 |
} |
219 | 217 |
}; |
220 | 218 |
|
221 | 219 |
///\e |
222 | 220 |
class RangeError : public RuntimeError { |
223 | 221 |
public: |
224 | 222 |
virtual const char* what() const throw() { |
225 | 223 |
return "lemon::RangeError"; |
226 | 224 |
} |
227 | 225 |
}; |
228 | 226 |
|
229 | 227 |
///\e |
230 | 228 |
class IoError : public RuntimeError { |
231 | 229 |
public: |
232 | 230 |
virtual const char* what() const throw() { |
233 | 231 |
return "lemon::IoError"; |
234 | 232 |
} |
235 | 233 |
}; |
236 | 234 |
|
237 | 235 |
///\e |
238 | 236 |
class DataFormatError : public IoError { |
239 | 237 |
protected: |
240 | 238 |
ExceptionMember<std::string> _message; |
241 | 239 |
ExceptionMember<std::string> _file; |
242 | 240 |
int _line; |
243 | 241 |
|
244 | 242 |
mutable ExceptionMember<std::string> _message_holder; |
245 | 243 |
public: |
246 | 244 |
|
247 | 245 |
DataFormatError(const DataFormatError &dfe) : |
248 | 246 |
IoError(dfe), _message(dfe._message), _file(dfe._file), |
249 | 247 |
_line(dfe._line) {} |
250 | 248 |
|
251 | 249 |
///\e |
252 | 250 |
explicit DataFormatError(const char *the_message) |
253 | 251 |
: _message(the_message), _line(0) {} |
254 | 252 |
|
255 | 253 |
///\e |
256 | 254 |
DataFormatError(const std::string &file_name, int line_num, |
257 | 255 |
const char *the_message) |
258 | 256 |
: _message(the_message), _line(line_num) { file(file_name); } |
259 | 257 |
|
260 | 258 |
///\e |
261 | 259 |
void line(int ln) { _line = ln; } |
262 | 260 |
///\e |
263 | 261 |
void message(const std::string& msg) { _message.set(msg); } |
264 | 262 |
///\e |
265 | 263 |
void file(const std::string &fl) { _file.set(fl); } |
266 | 264 |
|
267 | 265 |
///\e |
268 | 266 |
int line() const { return _line; } |
269 | 267 |
///\e |
270 | 268 |
const char* message() const { |
271 | 269 |
if (_message.valid() && !_message.get().empty()) { |
272 | 270 |
return _message.get().c_str(); |
273 | 271 |
} else { |
274 | 272 |
return 0; |
275 | 273 |
} |
276 | 274 |
} |
277 | 275 |
|
278 | 276 |
/// \brief Returns the filename. |
279 | 277 |
/// |
280 | 278 |
/// Returns \e null if the filename was not specified. |
281 | 279 |
const char* file() const { |
282 | 280 |
if (_file.valid() && !_file.get().empty()) { |
283 | 281 |
return _file.get().c_str(); |
284 | 282 |
} else { |
285 | 283 |
return 0; |
286 | 284 |
} |
287 | 285 |
} |
288 | 286 |
|
289 | 287 |
///\e |
290 | 288 |
virtual const char* what() const throw() { |
291 | 289 |
try { |
292 | 290 |
std::ostringstream ostr; |
293 | 291 |
ostr << "lemon:DataFormatError" << ": "; |
294 | 292 |
if (message()) ostr << message(); |
295 | 293 |
if( file() || line() != 0 ) { |
296 | 294 |
ostr << " ("; |
297 | 295 |
if( file() ) ostr << "in file '" << file() << "'"; |
298 | 296 |
if( file() && line() != 0 ) ostr << " "; |
299 | 297 |
if( line() != 0 ) ostr << "at line " << line(); |
300 | 298 |
ostr << ")"; |
301 | 299 |
} |
302 | 300 |
_message_holder.set(ostr.str()); |
303 | 301 |
} |
304 | 302 |
catch (...) {} |
305 | 303 |
if( _message_holder.valid()) return _message_holder.get().c_str(); |
306 | 304 |
return "lemon:DataFormatError"; |
307 | 305 |
} |
308 | 306 |
|
309 | 307 |
virtual ~DataFormatError() throw() {} |
310 | 308 |
}; |
311 | 309 |
|
312 | 310 |
///\e |
313 | 311 |
class FileOpenError : public IoError { |
314 | 312 |
protected: |
315 | 313 |
ExceptionMember<std::string> _file; |
316 | 314 |
|
317 | 315 |
mutable ExceptionMember<std::string> _message_holder; |
318 | 316 |
public: |
319 | 317 |
|
320 | 318 |
FileOpenError(const FileOpenError &foe) : |
321 | 319 |
IoError(foe), _file(foe._file) {} |
322 | 320 |
|
323 | 321 |
///\e |
324 | 322 |
explicit FileOpenError(const std::string& fl) |
325 | 323 |
: _file(fl) {} |
326 | 324 |
|
327 | 325 |
|
328 | 326 |
///\e |
329 | 327 |
void file(const std::string &fl) { _file.set(fl); } |
330 | 328 |
|
331 | 329 |
/// \brief Returns the filename. |
332 | 330 |
/// |
333 | 331 |
/// Returns \e null if the filename was not specified. |
334 | 332 |
const char* file() const { |
335 | 333 |
if (_file.valid() && !_file.get().empty()) { |
336 | 334 |
return _file.get().c_str(); |
337 | 335 |
} else { |
338 | 336 |
return 0; |
339 | 337 |
} |
340 | 338 |
} |
341 | 339 |
|
342 | 340 |
///\e |
343 | 341 |
virtual const char* what() const throw() { |
344 | 342 |
try { |
345 | 343 |
std::ostringstream ostr; |
346 | 344 |
ostr << "lemon::FileOpenError" << ": "; |
347 | 345 |
ostr << "Cannot open file - " << file(); |
348 | 346 |
_message_holder.set(ostr.str()); |
349 | 347 |
} |
350 | 348 |
catch (...) {} |
351 | 349 |
if( _message_holder.valid()) return _message_holder.get().c_str(); |
352 | 350 |
return "lemon::FileOpenError"; |
353 | 351 |
} |
354 | 352 |
virtual ~FileOpenError() throw() {} |
355 | 353 |
}; |
356 | 354 |
|
357 | 355 |
class IoParameterError : public IoError { |
358 | 356 |
protected: |
359 | 357 |
ExceptionMember<std::string> _message; |
360 | 358 |
ExceptionMember<std::string> _file; |
361 | 359 |
|
362 | 360 |
mutable ExceptionMember<std::string> _message_holder; |
363 | 361 |
public: |
364 | 362 |
|
365 | 363 |
IoParameterError(const IoParameterError &ile) : |
366 | 364 |
IoError(ile), _message(ile._message), _file(ile._file) {} |
367 | 365 |
|
368 | 366 |
///\e |
369 | 367 |
explicit IoParameterError(const char *the_message) |
370 | 368 |
: _message(the_message) {} |
371 | 369 |
|
372 | 370 |
///\e |
373 | 371 |
IoParameterError(const char *file_name, const char *the_message) |
374 | 372 |
: _message(the_message), _file(file_name) {} |
375 | 373 |
|
376 | 374 |
///\e |
377 | 375 |
void message(const std::string& msg) { _message.set(msg); } |
378 | 376 |
///\e |
379 | 377 |
void file(const std::string &fl) { _file.set(fl); } |
380 | 378 |
|
381 | 379 |
///\e |
382 | 380 |
const char* message() const { |
383 | 381 |
if (_message.valid()) { |
384 | 382 |
return _message.get().c_str(); |
385 | 383 |
} else { |
386 | 384 |
return 0; |
387 | 385 |
} |
388 | 386 |
} |
389 | 387 |
|
390 | 388 |
/// \brief Returns the filename. |
391 | 389 |
/// |
392 | 390 |
/// Returns \c 0 if the filename was not specified. |
393 | 391 |
const char* file() const { |
394 | 392 |
if (_file.valid()) { |
395 | 393 |
return _file.get().c_str(); |
396 | 394 |
} else { |
397 | 395 |
return 0; |
398 | 396 |
} |
399 | 397 |
} |
400 | 398 |
|
401 | 399 |
///\e |
402 | 400 |
virtual const char* what() const throw() { |
403 | 401 |
try { |
404 | 402 |
std::ostringstream ostr; |
405 | 403 |
if (message()) ostr << message(); |
406 | 404 |
if (file()) ostr << "(when reading file '" << file() << "')"; |
407 | 405 |
_message_holder.set(ostr.str()); |
408 | 406 |
} |
409 | 407 |
catch (...) {} |
410 | 408 |
if( _message_holder.valid() ) return _message_holder.get().c_str(); |
411 | 409 |
return "lemon:IoParameterError"; |
412 | 410 |
} |
413 | 411 |
virtual ~IoParameterError() throw() {} |
414 | 412 |
}; |
415 | 413 |
|
416 | 414 |
/// @} |
417 | 415 |
|
418 | 416 |
} |
419 | 417 |
|
420 | 418 |
#endif // LEMON_ERROR_H |
... | ... |
@@ -285,912 +285,907 @@ |
285 | 285 |
/// = 4 |
286 | 286 |
///\image html nodeshape_4.png |
287 | 287 |
///\image latex nodeshape_2.eps "FEMALE shape (4)" width=2cm |
288 | 288 |
/// |
289 | 289 |
FEMALE=4 |
290 | 290 |
}; |
291 | 291 |
|
292 | 292 |
private: |
293 | 293 |
class arcLess { |
294 | 294 |
const Graph &g; |
295 | 295 |
public: |
296 | 296 |
arcLess(const Graph &_g) : g(_g) {} |
297 | 297 |
bool operator()(Arc a,Arc b) const |
298 | 298 |
{ |
299 | 299 |
Node ai=std::min(g.source(a),g.target(a)); |
300 | 300 |
Node aa=std::max(g.source(a),g.target(a)); |
301 | 301 |
Node bi=std::min(g.source(b),g.target(b)); |
302 | 302 |
Node ba=std::max(g.source(b),g.target(b)); |
303 | 303 |
return ai<bi || |
304 | 304 |
(ai==bi && (aa < ba || |
305 | 305 |
(aa==ba && ai==g.source(a) && bi==g.target(b)))); |
306 | 306 |
} |
307 | 307 |
}; |
308 | 308 |
bool isParallel(Arc e,Arc f) const |
309 | 309 |
{ |
310 | 310 |
return (g.source(e)==g.source(f)&& |
311 | 311 |
g.target(e)==g.target(f)) || |
312 | 312 |
(g.source(e)==g.target(f)&& |
313 | 313 |
g.target(e)==g.source(f)); |
314 | 314 |
} |
315 | 315 |
template<class TT> |
316 | 316 |
static std::string psOut(const dim2::Point<TT> &p) |
317 | 317 |
{ |
318 | 318 |
std::ostringstream os; |
319 | 319 |
os << p.x << ' ' << p.y; |
320 | 320 |
return os.str(); |
321 | 321 |
} |
322 | 322 |
static std::string psOut(const Color &c) |
323 | 323 |
{ |
324 | 324 |
std::ostringstream os; |
325 | 325 |
os << c.red() << ' ' << c.green() << ' ' << c.blue(); |
326 | 326 |
return os.str(); |
327 | 327 |
} |
328 | 328 |
|
329 | 329 |
public: |
330 | 330 |
GraphToEps(const T &t) : T(t), dontPrint(false) {}; |
331 | 331 |
|
332 | 332 |
template<class X> struct CoordsTraits : public T { |
333 | 333 |
typedef X CoordsMapType; |
334 | 334 |
const X &_coords; |
335 | 335 |
CoordsTraits(const T &t,const X &x) : T(t), _coords(x) {} |
336 | 336 |
}; |
337 | 337 |
///Sets the map of the node coordinates |
338 | 338 |
|
339 | 339 |
///Sets the map of the node coordinates. |
340 | 340 |
///\param x must be a node map with \ref dim2::Point "dim2::Point<double>" or |
341 | 341 |
///\ref dim2::Point "dim2::Point<int>" values. |
342 | 342 |
template<class X> GraphToEps<CoordsTraits<X> > coords(const X &x) { |
343 | 343 |
dontPrint=true; |
344 | 344 |
return GraphToEps<CoordsTraits<X> >(CoordsTraits<X>(*this,x)); |
345 | 345 |
} |
346 | 346 |
template<class X> struct NodeSizesTraits : public T { |
347 | 347 |
const X &_nodeSizes; |
348 | 348 |
NodeSizesTraits(const T &t,const X &x) : T(t), _nodeSizes(x) {} |
349 | 349 |
}; |
350 | 350 |
///Sets the map of the node sizes |
351 | 351 |
|
352 | 352 |
///Sets the map of the node sizes. |
353 | 353 |
///\param x must be a node map with \c double (or convertible) values. |
354 | 354 |
template<class X> GraphToEps<NodeSizesTraits<X> > nodeSizes(const X &x) |
355 | 355 |
{ |
356 | 356 |
dontPrint=true; |
357 | 357 |
return GraphToEps<NodeSizesTraits<X> >(NodeSizesTraits<X>(*this,x)); |
358 | 358 |
} |
359 | 359 |
template<class X> struct NodeShapesTraits : public T { |
360 | 360 |
const X &_nodeShapes; |
361 | 361 |
NodeShapesTraits(const T &t,const X &x) : T(t), _nodeShapes(x) {} |
362 | 362 |
}; |
363 | 363 |
///Sets the map of the node shapes |
364 | 364 |
|
365 | 365 |
///Sets the map of the node shapes. |
366 | 366 |
///The available shape values |
367 | 367 |
///can be found in \ref NodeShapes "enum NodeShapes". |
368 | 368 |
///\param x must be a node map with \c int (or convertible) values. |
369 | 369 |
///\sa NodeShapes |
370 | 370 |
template<class X> GraphToEps<NodeShapesTraits<X> > nodeShapes(const X &x) |
371 | 371 |
{ |
372 | 372 |
dontPrint=true; |
373 | 373 |
return GraphToEps<NodeShapesTraits<X> >(NodeShapesTraits<X>(*this,x)); |
374 | 374 |
} |
375 | 375 |
template<class X> struct NodeTextsTraits : public T { |
376 | 376 |
const X &_nodeTexts; |
377 | 377 |
NodeTextsTraits(const T &t,const X &x) : T(t), _nodeTexts(x) {} |
378 | 378 |
}; |
379 | 379 |
///Sets the text printed on the nodes |
380 | 380 |
|
381 | 381 |
///Sets the text printed on the nodes. |
382 | 382 |
///\param x must be a node map with type that can be pushed to a standard |
383 | 383 |
///\c ostream. |
384 | 384 |
template<class X> GraphToEps<NodeTextsTraits<X> > nodeTexts(const X &x) |
385 | 385 |
{ |
386 | 386 |
dontPrint=true; |
387 | 387 |
_showNodeText=true; |
388 | 388 |
return GraphToEps<NodeTextsTraits<X> >(NodeTextsTraits<X>(*this,x)); |
389 | 389 |
} |
390 | 390 |
template<class X> struct NodePsTextsTraits : public T { |
391 | 391 |
const X &_nodePsTexts; |
392 | 392 |
NodePsTextsTraits(const T &t,const X &x) : T(t), _nodePsTexts(x) {} |
393 | 393 |
}; |
394 | 394 |
///Inserts a PostScript block to the nodes |
395 | 395 |
|
396 | 396 |
///With this command it is possible to insert a verbatim PostScript |
397 | 397 |
///block to the nodes. |
398 | 398 |
///The PS current point will be moved to the center of the node before |
399 | 399 |
///the PostScript block inserted. |
400 | 400 |
/// |
401 | 401 |
///Before and after the block a newline character is inserted so you |
402 | 402 |
///don't have to bother with the separators. |
403 | 403 |
/// |
404 | 404 |
///\param x must be a node map with type that can be pushed to a standard |
405 | 405 |
///\c ostream. |
406 | 406 |
/// |
407 | 407 |
///\sa nodePsTextsPreamble() |
408 | 408 |
template<class X> GraphToEps<NodePsTextsTraits<X> > nodePsTexts(const X &x) |
409 | 409 |
{ |
410 | 410 |
dontPrint=true; |
411 | 411 |
_showNodePsText=true; |
412 | 412 |
return GraphToEps<NodePsTextsTraits<X> >(NodePsTextsTraits<X>(*this,x)); |
413 | 413 |
} |
414 | 414 |
template<class X> struct ArcWidthsTraits : public T { |
415 | 415 |
const X &_arcWidths; |
416 | 416 |
ArcWidthsTraits(const T &t,const X &x) : T(t), _arcWidths(x) {} |
417 | 417 |
}; |
418 | 418 |
///Sets the map of the arc widths |
419 | 419 |
|
420 | 420 |
///Sets the map of the arc widths. |
421 | 421 |
///\param x must be an arc map with \c double (or convertible) values. |
422 | 422 |
template<class X> GraphToEps<ArcWidthsTraits<X> > arcWidths(const X &x) |
423 | 423 |
{ |
424 | 424 |
dontPrint=true; |
425 | 425 |
return GraphToEps<ArcWidthsTraits<X> >(ArcWidthsTraits<X>(*this,x)); |
426 | 426 |
} |
427 | 427 |
|
428 | 428 |
template<class X> struct NodeColorsTraits : public T { |
429 | 429 |
const X &_nodeColors; |
430 | 430 |
NodeColorsTraits(const T &t,const X &x) : T(t), _nodeColors(x) {} |
431 | 431 |
}; |
432 | 432 |
///Sets the map of the node colors |
433 | 433 |
|
434 | 434 |
///Sets the map of the node colors. |
435 | 435 |
///\param x must be a node map with \ref Color values. |
436 | 436 |
/// |
437 | 437 |
///\sa Palette |
438 | 438 |
template<class X> GraphToEps<NodeColorsTraits<X> > |
439 | 439 |
nodeColors(const X &x) |
440 | 440 |
{ |
441 | 441 |
dontPrint=true; |
442 | 442 |
return GraphToEps<NodeColorsTraits<X> >(NodeColorsTraits<X>(*this,x)); |
443 | 443 |
} |
444 | 444 |
template<class X> struct NodeTextColorsTraits : public T { |
445 | 445 |
const X &_nodeTextColors; |
446 | 446 |
NodeTextColorsTraits(const T &t,const X &x) : T(t), _nodeTextColors(x) {} |
447 | 447 |
}; |
448 | 448 |
///Sets the map of the node text colors |
449 | 449 |
|
450 | 450 |
///Sets the map of the node text colors. |
451 | 451 |
///\param x must be a node map with \ref Color values. |
452 | 452 |
/// |
453 | 453 |
///\sa Palette |
454 | 454 |
template<class X> GraphToEps<NodeTextColorsTraits<X> > |
455 | 455 |
nodeTextColors(const X &x) |
456 | 456 |
{ |
457 | 457 |
dontPrint=true; |
458 | 458 |
_nodeTextColorType=CUST_COL; |
459 | 459 |
return GraphToEps<NodeTextColorsTraits<X> > |
460 | 460 |
(NodeTextColorsTraits<X>(*this,x)); |
461 | 461 |
} |
462 | 462 |
template<class X> struct ArcColorsTraits : public T { |
463 | 463 |
const X &_arcColors; |
464 | 464 |
ArcColorsTraits(const T &t,const X &x) : T(t), _arcColors(x) {} |
465 | 465 |
}; |
466 | 466 |
///Sets the map of the arc colors |
467 | 467 |
|
468 | 468 |
///Sets the map of the arc colors. |
469 | 469 |
///\param x must be an arc map with \ref Color values. |
470 | 470 |
/// |
471 | 471 |
///\sa Palette |
472 | 472 |
template<class X> GraphToEps<ArcColorsTraits<X> > |
473 | 473 |
arcColors(const X &x) |
474 | 474 |
{ |
475 | 475 |
dontPrint=true; |
476 | 476 |
return GraphToEps<ArcColorsTraits<X> >(ArcColorsTraits<X>(*this,x)); |
477 | 477 |
} |
478 | 478 |
///Sets a global scale factor for node sizes |
479 | 479 |
|
480 | 480 |
///Sets a global scale factor for node sizes. |
481 | 481 |
/// |
482 | 482 |
/// If nodeSizes() is not given, this function simply sets the node |
483 | 483 |
/// sizes to \c d. If nodeSizes() is given, but |
484 | 484 |
/// autoNodeScale() is not, then the node size given by |
485 | 485 |
/// nodeSizes() will be multiplied by the value \c d. |
486 | 486 |
/// If both nodeSizes() and autoNodeScale() are used, then the |
487 | 487 |
/// node sizes will be scaled in such a way that the greatest size will be |
488 | 488 |
/// equal to \c d. |
489 | 489 |
/// \sa nodeSizes() |
490 | 490 |
/// \sa autoNodeScale() |
491 | 491 |
GraphToEps<T> &nodeScale(double d=.01) {_nodeScale=d;return *this;} |
492 | 492 |
///Turns on/off the automatic node size scaling. |
493 | 493 |
|
494 | 494 |
///Turns on/off the automatic node size scaling. |
495 | 495 |
/// |
496 | 496 |
///\sa nodeScale() |
497 | 497 |
/// |
498 | 498 |
GraphToEps<T> &autoNodeScale(bool b=true) { |
499 | 499 |
_autoNodeScale=b;return *this; |
500 | 500 |
} |
501 | 501 |
|
502 | 502 |
///Turns on/off the absolutematic node size scaling. |
503 | 503 |
|
504 | 504 |
///Turns on/off the absolutematic node size scaling. |
505 | 505 |
/// |
506 | 506 |
///\sa nodeScale() |
507 | 507 |
/// |
508 | 508 |
GraphToEps<T> &absoluteNodeSizes(bool b=true) { |
509 | 509 |
_absoluteNodeSizes=b;return *this; |
510 | 510 |
} |
511 | 511 |
|
512 | 512 |
///Negates the Y coordinates. |
513 | 513 |
GraphToEps<T> &negateY(bool b=true) { |
514 | 514 |
_negY=b;return *this; |
515 | 515 |
} |
516 | 516 |
|
517 | 517 |
///Turn on/off pre-scaling |
518 | 518 |
|
519 | 519 |
///By default graphToEps() rescales the whole image in order to avoid |
520 | 520 |
///very big or very small bounding boxes. |
521 | 521 |
/// |
522 | 522 |
///This (p)rescaling can be turned off with this function. |
523 | 523 |
/// |
524 | 524 |
GraphToEps<T> &preScale(bool b=true) { |
525 | 525 |
_preScale=b;return *this; |
526 | 526 |
} |
527 | 527 |
|
528 | 528 |
///Sets a global scale factor for arc widths |
529 | 529 |
|
530 | 530 |
/// Sets a global scale factor for arc widths. |
531 | 531 |
/// |
532 | 532 |
/// If arcWidths() is not given, this function simply sets the arc |
533 | 533 |
/// widths to \c d. If arcWidths() is given, but |
534 | 534 |
/// autoArcWidthScale() is not, then the arc withs given by |
535 | 535 |
/// arcWidths() will be multiplied by the value \c d. |
536 | 536 |
/// If both arcWidths() and autoArcWidthScale() are used, then the |
537 | 537 |
/// arc withs will be scaled in such a way that the greatest width will be |
538 | 538 |
/// equal to \c d. |
539 | 539 |
GraphToEps<T> &arcWidthScale(double d=.003) {_arcWidthScale=d;return *this;} |
540 | 540 |
///Turns on/off the automatic arc width scaling. |
541 | 541 |
|
542 | 542 |
///Turns on/off the automatic arc width scaling. |
543 | 543 |
/// |
544 | 544 |
///\sa arcWidthScale() |
545 | 545 |
/// |
546 | 546 |
GraphToEps<T> &autoArcWidthScale(bool b=true) { |
547 | 547 |
_autoArcWidthScale=b;return *this; |
548 | 548 |
} |
549 | 549 |
///Turns on/off the absolutematic arc width scaling. |
550 | 550 |
|
551 | 551 |
///Turns on/off the absolutematic arc width scaling. |
552 | 552 |
/// |
553 | 553 |
///\sa arcWidthScale() |
554 | 554 |
/// |
555 | 555 |
GraphToEps<T> &absoluteArcWidths(bool b=true) { |
556 | 556 |
_absoluteArcWidths=b;return *this; |
557 | 557 |
} |
558 | 558 |
///Sets a global scale factor for the whole picture |
559 | 559 |
GraphToEps<T> &scale(double d) {_scale=d;return *this;} |
560 | 560 |
///Sets the width of the border around the picture |
561 | 561 |
GraphToEps<T> &border(double b=10) {_xBorder=_yBorder=b;return *this;} |
562 | 562 |
///Sets the width of the border around the picture |
563 | 563 |
GraphToEps<T> &border(double x, double y) { |
564 | 564 |
_xBorder=x;_yBorder=y;return *this; |
565 | 565 |
} |
566 | 566 |
///Sets whether to draw arrows |
567 | 567 |
GraphToEps<T> &drawArrows(bool b=true) {_drawArrows=b;return *this;} |
568 | 568 |
///Sets the length of the arrowheads |
569 | 569 |
GraphToEps<T> &arrowLength(double d=1.0) {_arrowLength*=d;return *this;} |
570 | 570 |
///Sets the width of the arrowheads |
571 | 571 |
GraphToEps<T> &arrowWidth(double d=.3) {_arrowWidth*=d;return *this;} |
572 | 572 |
|
573 | 573 |
///Scales the drawing to fit to A4 page |
574 | 574 |
GraphToEps<T> &scaleToA4() {_scaleToA4=true;return *this;} |
575 | 575 |
|
576 | 576 |
///Enables parallel arcs |
577 | 577 |
GraphToEps<T> &enableParallel(bool b=true) {_enableParallel=b;return *this;} |
578 | 578 |
|
579 | 579 |
///Sets the distance between parallel arcs |
580 | 580 |
GraphToEps<T> &parArcDist(double d) {_parArcDist*=d;return *this;} |
581 | 581 |
|
582 | 582 |
///Hides the arcs |
583 | 583 |
GraphToEps<T> &hideArcs(bool b=true) {_showArcs=!b;return *this;} |
584 | 584 |
///Hides the nodes |
585 | 585 |
GraphToEps<T> &hideNodes(bool b=true) {_showNodes=!b;return *this;} |
586 | 586 |
|
587 | 587 |
///Sets the size of the node texts |
588 | 588 |
GraphToEps<T> &nodeTextSize(double d) {_nodeTextSize=d;return *this;} |
589 | 589 |
|
590 | 590 |
///Sets the color of the node texts to be different from the node color |
591 | 591 |
|
592 | 592 |
///Sets the color of the node texts to be as different from the node color |
593 | 593 |
///as it is possible. |
594 | 594 |
GraphToEps<T> &distantColorNodeTexts() |
595 | 595 |
{_nodeTextColorType=DIST_COL;return *this;} |
596 | 596 |
///Sets the color of the node texts to be black or white and always visible. |
597 | 597 |
|
598 | 598 |
///Sets the color of the node texts to be black or white according to |
599 | 599 |
///which is more different from the node color. |
600 | 600 |
GraphToEps<T> &distantBWNodeTexts() |
601 | 601 |
{_nodeTextColorType=DIST_BW;return *this;} |
602 | 602 |
|
603 | 603 |
///Gives a preamble block for node Postscript block. |
604 | 604 |
|
605 | 605 |
///Gives a preamble block for node Postscript block. |
606 | 606 |
/// |
607 | 607 |
///\sa nodePsTexts() |
608 | 608 |
GraphToEps<T> & nodePsTextsPreamble(const char *str) { |
609 | 609 |
_nodePsTextsPreamble=str ;return *this; |
610 | 610 |
} |
611 | 611 |
///Sets whether the graph is undirected |
612 | 612 |
|
613 | 613 |
///Sets whether the graph is undirected. |
614 | 614 |
/// |
615 | 615 |
///This setting is the default for undirected graphs. |
616 | 616 |
/// |
617 | 617 |
///\sa directed() |
618 | 618 |
GraphToEps<T> &undirected(bool b=true) {_undirected=b;return *this;} |
619 | 619 |
|
620 | 620 |
///Sets whether the graph is directed |
621 | 621 |
|
622 | 622 |
///Sets whether the graph is directed. |
623 | 623 |
///Use it to show the edges as a pair of directed ones. |
624 | 624 |
/// |
625 | 625 |
///This setting is the default for digraphs. |
626 | 626 |
/// |
627 | 627 |
///\sa undirected() |
628 | 628 |
GraphToEps<T> &directed(bool b=true) {_undirected=!b;return *this;} |
629 | 629 |
|
630 | 630 |
///Sets the title. |
631 | 631 |
|
632 | 632 |
///Sets the title of the generated image, |
633 | 633 |
///namely it inserts a <tt>%%Title:</tt> DSC field to the header of |
634 | 634 |
///the EPS file. |
635 | 635 |
GraphToEps<T> &title(const std::string &t) {_title=t;return *this;} |
636 | 636 |
///Sets the copyright statement. |
637 | 637 |
|
638 | 638 |
///Sets the copyright statement of the generated image, |
639 | 639 |
///namely it inserts a <tt>%%Copyright:</tt> DSC field to the header of |
640 | 640 |
///the EPS file. |
641 | 641 |
GraphToEps<T> ©right(const std::string &t) {_copyright=t;return *this;} |
642 | 642 |
|
643 | 643 |
protected: |
644 | 644 |
bool isInsideNode(dim2::Point<double> p, double r,int t) |
645 | 645 |
{ |
646 | 646 |
switch(t) { |
647 | 647 |
case CIRCLE: |
648 | 648 |
case MALE: |
649 | 649 |
case FEMALE: |
650 | 650 |
return p.normSquare()<=r*r; |
651 | 651 |
case SQUARE: |
652 | 652 |
return p.x<=r&&p.x>=-r&&p.y<=r&&p.y>=-r; |
653 | 653 |
case DIAMOND: |
654 | 654 |
return p.x+p.y<=r && p.x-p.y<=r && -p.x+p.y<=r && -p.x-p.y<=r; |
655 | 655 |
} |
656 | 656 |
return false; |
657 | 657 |
} |
658 | 658 |
|
659 | 659 |
public: |
660 | 660 |
~GraphToEps() { } |
661 | 661 |
|
662 | 662 |
///Draws the graph. |
663 | 663 |
|
664 | 664 |
///Like other functions using |
665 | 665 |
///\ref named-templ-func-param "named template parameters", |
666 | 666 |
///this function calls the algorithm itself, i.e. in this case |
667 | 667 |
///it draws the graph. |
668 | 668 |
void run() { |
669 |
//\todo better 'epsilon' would be nice here. |
|
670 | 669 |
const double EPSILON=1e-9; |
671 | 670 |
if(dontPrint) return; |
672 | 671 |
|
673 | 672 |
_graph_to_eps_bits::_NegY<typename T::CoordsMapType> |
674 | 673 |
mycoords(_coords,_negY); |
675 | 674 |
|
676 | 675 |
os << "%!PS-Adobe-2.0 EPSF-2.0\n"; |
677 | 676 |
if(_title.size()>0) os << "%%Title: " << _title << '\n'; |
678 | 677 |
if(_copyright.size()>0) os << "%%Copyright: " << _copyright << '\n'; |
679 | 678 |
os << "%%Creator: LEMON, graphToEps()\n"; |
680 | 679 |
|
681 | 680 |
{ |
682 | 681 |
#ifndef WIN32 |
683 | 682 |
timeval tv; |
684 | 683 |
gettimeofday(&tv, 0); |
685 | 684 |
|
686 | 685 |
char cbuf[26]; |
687 | 686 |
ctime_r(&tv.tv_sec,cbuf); |
688 | 687 |
os << "%%CreationDate: " << cbuf; |
689 | 688 |
#else |
690 | 689 |
SYSTEMTIME time; |
691 | 690 |
char buf1[11], buf2[9], buf3[5]; |
692 | 691 |
|
693 | 692 |
GetSystemTime(&time); |
694 | 693 |
if (GetDateFormat(LOCALE_USER_DEFAULT, 0, &time, |
695 | 694 |
"ddd MMM dd", buf1, 11) && |
696 | 695 |
GetTimeFormat(LOCALE_USER_DEFAULT, 0, &time, |
697 | 696 |
"HH':'mm':'ss", buf2, 9) && |
698 | 697 |
GetDateFormat(LOCALE_USER_DEFAULT, 0, &time, |
699 | 698 |
"yyyy", buf3, 5)) { |
700 | 699 |
os << "%%CreationDate: " << buf1 << ' ' |
701 | 700 |
<< buf2 << ' ' << buf3 << std::endl; |
702 | 701 |
} |
703 | 702 |
#endif |
704 | 703 |
} |
705 | 704 |
|
706 | 705 |
if (_autoArcWidthScale) { |
707 | 706 |
double max_w=0; |
708 | 707 |
for(ArcIt e(g);e!=INVALID;++e) |
709 | 708 |
max_w=std::max(double(_arcWidths[e]),max_w); |
710 |
//\todo better 'epsilon' would be nice here. |
|
711 | 709 |
if(max_w>EPSILON) { |
712 | 710 |
_arcWidthScale/=max_w; |
713 | 711 |
} |
714 | 712 |
} |
715 | 713 |
|
716 | 714 |
if (_autoNodeScale) { |
717 | 715 |
double max_s=0; |
718 | 716 |
for(NodeIt n(g);n!=INVALID;++n) |
719 | 717 |
max_s=std::max(double(_nodeSizes[n]),max_s); |
720 |
//\todo better 'epsilon' would be nice here. |
|
721 | 718 |
if(max_s>EPSILON) { |
722 | 719 |
_nodeScale/=max_s; |
723 | 720 |
} |
724 | 721 |
} |
725 | 722 |
|
726 | 723 |
double diag_len = 1; |
727 | 724 |
if(!(_absoluteNodeSizes&&_absoluteArcWidths)) { |
728 | 725 |
dim2::Box<double> bb; |
729 | 726 |
for(NodeIt n(g);n!=INVALID;++n) bb.add(mycoords[n]); |
730 | 727 |
if (bb.empty()) { |
731 | 728 |
bb = dim2::Box<double>(dim2::Point<double>(0,0)); |
732 | 729 |
} |
733 | 730 |
diag_len = std::sqrt((bb.bottomLeft()-bb.topRight()).normSquare()); |
734 | 731 |
if(diag_len<EPSILON) diag_len = 1; |
735 | 732 |
if(!_absoluteNodeSizes) _nodeScale*=diag_len; |
736 | 733 |
if(!_absoluteArcWidths) _arcWidthScale*=diag_len; |
737 | 734 |
} |
738 | 735 |
|
739 | 736 |
dim2::Box<double> bb; |
740 | 737 |
for(NodeIt n(g);n!=INVALID;++n) { |
741 | 738 |
double ns=_nodeSizes[n]*_nodeScale; |
742 | 739 |
dim2::Point<double> p(ns,ns); |
743 | 740 |
switch(_nodeShapes[n]) { |
744 | 741 |
case CIRCLE: |
745 | 742 |
case SQUARE: |
746 | 743 |
case DIAMOND: |
747 | 744 |
bb.add(p+mycoords[n]); |
748 | 745 |
bb.add(-p+mycoords[n]); |
749 | 746 |
break; |
750 | 747 |
case MALE: |
751 | 748 |
bb.add(-p+mycoords[n]); |
752 | 749 |
bb.add(dim2::Point<double>(1.5*ns,1.5*std::sqrt(3.0)*ns)+mycoords[n]); |
753 | 750 |
break; |
754 | 751 |
case FEMALE: |
755 | 752 |
bb.add(p+mycoords[n]); |
756 | 753 |
bb.add(dim2::Point<double>(-ns,-3.01*ns)+mycoords[n]); |
757 | 754 |
break; |
758 | 755 |
} |
759 | 756 |
} |
760 | 757 |
if (bb.empty()) { |
761 | 758 |
bb = dim2::Box<double>(dim2::Point<double>(0,0)); |
762 | 759 |
} |
763 | 760 |
|
764 | 761 |
if(_scaleToA4) |
765 | 762 |
os <<"%%BoundingBox: 0 0 596 842\n%%DocumentPaperSizes: a4\n"; |
766 | 763 |
else { |
767 | 764 |
if(_preScale) { |
768 | 765 |
//Rescale so that BoundingBox won't be neither to big nor too small. |
769 | 766 |
while(bb.height()*_scale>1000||bb.width()*_scale>1000) _scale/=10; |
770 | 767 |
while(bb.height()*_scale<100||bb.width()*_scale<100) _scale*=10; |
771 | 768 |
} |
772 | 769 |
|
773 | 770 |
os << "%%BoundingBox: " |
774 | 771 |
<< int(floor(bb.left() * _scale - _xBorder)) << ' ' |
775 | 772 |
<< int(floor(bb.bottom() * _scale - _yBorder)) << ' ' |
776 | 773 |
<< int(ceil(bb.right() * _scale + _xBorder)) << ' ' |
777 | 774 |
<< int(ceil(bb.top() * _scale + _yBorder)) << '\n'; |
778 | 775 |
} |
779 | 776 |
|
780 | 777 |
os << "%%EndComments\n"; |
781 | 778 |
|
782 | 779 |
//x1 y1 x2 y2 x3 y3 cr cg cb w |
783 | 780 |
os << "/lb { setlinewidth setrgbcolor newpath moveto\n" |
784 | 781 |
<< " 4 2 roll 1 index 1 index curveto stroke } bind def\n"; |
785 | 782 |
os << "/l { setlinewidth setrgbcolor newpath moveto lineto stroke }" |
786 | 783 |
<< " bind def\n"; |
787 | 784 |
//x y r |
788 | 785 |
os << "/c { newpath dup 3 index add 2 index moveto 0 360 arc closepath }" |
789 | 786 |
<< " bind def\n"; |
790 | 787 |
//x y r |
791 | 788 |
os << "/sq { newpath 2 index 1 index add 2 index 2 index add moveto\n" |
792 | 789 |
<< " 2 index 1 index sub 2 index 2 index add lineto\n" |
793 | 790 |
<< " 2 index 1 index sub 2 index 2 index sub lineto\n" |
794 | 791 |
<< " 2 index 1 index add 2 index 2 index sub lineto\n" |
795 | 792 |
<< " closepath pop pop pop} bind def\n"; |
796 | 793 |
//x y r |
797 | 794 |
os << "/di { newpath 2 index 1 index add 2 index moveto\n" |
798 | 795 |
<< " 2 index 2 index 2 index add lineto\n" |
799 | 796 |
<< " 2 index 1 index sub 2 index lineto\n" |
800 | 797 |
<< " 2 index 2 index 2 index sub lineto\n" |
801 | 798 |
<< " closepath pop pop pop} bind def\n"; |
802 | 799 |
// x y r cr cg cb |
803 | 800 |
os << "/nc { 0 0 0 setrgbcolor 5 index 5 index 5 index c fill\n" |
804 | 801 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
805 | 802 |
<< " } bind def\n"; |
806 | 803 |
os << "/nsq { 0 0 0 setrgbcolor 5 index 5 index 5 index sq fill\n" |
807 | 804 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div sq fill\n" |
808 | 805 |
<< " } bind def\n"; |
809 | 806 |
os << "/ndi { 0 0 0 setrgbcolor 5 index 5 index 5 index di fill\n" |
810 | 807 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div di fill\n" |
811 | 808 |
<< " } bind def\n"; |
812 | 809 |
os << "/nfemale { 0 0 0 setrgbcolor 3 index " |
813 | 810 |
<< _nodeBorderQuotient/(1+_nodeBorderQuotient) |
814 | 811 |
<< " 1.5 mul mul setlinewidth\n" |
815 | 812 |
<< " newpath 5 index 5 index moveto " |
816 | 813 |
<< "5 index 5 index 5 index 3.01 mul sub\n" |
817 | 814 |
<< " lineto 5 index 4 index .7 mul sub 5 index 5 index 2.2 mul sub" |
818 | 815 |
<< " moveto\n" |
819 | 816 |
<< " 5 index 4 index .7 mul add 5 index 5 index 2.2 mul sub lineto " |
820 | 817 |
<< "stroke\n" |
821 | 818 |
<< " 5 index 5 index 5 index c fill\n" |
822 | 819 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
823 | 820 |
<< " } bind def\n"; |
824 | 821 |
os << "/nmale {\n" |
825 | 822 |
<< " 0 0 0 setrgbcolor 3 index " |
826 | 823 |
<< _nodeBorderQuotient/(1+_nodeBorderQuotient) |
827 | 824 |
<<" 1.5 mul mul setlinewidth\n" |
828 | 825 |
<< " newpath 5 index 5 index moveto\n" |
829 | 826 |
<< " 5 index 4 index 1 mul 1.5 mul add\n" |
830 | 827 |
<< " 5 index 5 index 3 sqrt 1.5 mul mul add\n" |
831 | 828 |
<< " 1 index 1 index lineto\n" |
832 | 829 |
<< " 1 index 1 index 7 index sub moveto\n" |
833 | 830 |
<< " 1 index 1 index lineto\n" |
834 | 831 |
<< " exch 5 index 3 sqrt .5 mul mul sub exch 5 index .5 mul sub" |
835 | 832 |
<< " lineto\n" |
836 | 833 |
<< " stroke\n" |
837 | 834 |
<< " 5 index 5 index 5 index c fill\n" |
838 | 835 |
<< " setrgbcolor " << 1+_nodeBorderQuotient << " div c fill\n" |
839 | 836 |
<< " } bind def\n"; |
840 | 837 |
|
841 | 838 |
|
842 | 839 |
os << "/arrl " << _arrowLength << " def\n"; |
843 | 840 |
os << "/arrw " << _arrowWidth << " def\n"; |
844 | 841 |
// l dx_norm dy_norm |
845 | 842 |
os << "/lrl { 2 index mul exch 2 index mul exch rlineto pop} bind def\n"; |
846 | 843 |
//len w dx_norm dy_norm x1 y1 cr cg cb |
847 | 844 |
os << "/arr { setrgbcolor /y1 exch def /x1 exch def /dy exch def /dx " |
848 | 845 |
<< "exch def\n" |
849 | 846 |
<< " /w exch def /len exch def\n" |
850 | 847 |
//<< "0.1 setlinewidth x1 y1 moveto dx len mul dy len mul rlineto stroke" |
851 | 848 |
<< " newpath x1 dy w 2 div mul add y1 dx w 2 div mul sub moveto\n" |
852 | 849 |
<< " len w sub arrl sub dx dy lrl\n" |
853 | 850 |
<< " arrw dy dx neg lrl\n" |
854 | 851 |
<< " dx arrl w add mul dy w 2 div arrw add mul sub\n" |
855 | 852 |
<< " dy arrl w add mul dx w 2 div arrw add mul add rlineto\n" |
856 | 853 |
<< " dx arrl w add mul neg dy w 2 div arrw add mul sub\n" |
857 | 854 |
<< " dy arrl w add mul neg dx w 2 div arrw add mul add rlineto\n" |
858 | 855 |
<< " arrw dy dx neg lrl\n" |
859 | 856 |
<< " len w sub arrl sub neg dx dy lrl\n" |
860 | 857 |
<< " closepath fill } bind def\n"; |
861 | 858 |
os << "/cshow { 2 index 2 index moveto dup stringwidth pop\n" |
862 | 859 |
<< " neg 2 div fosi .35 mul neg rmoveto show pop pop} def\n"; |
863 | 860 |
|
864 | 861 |
os << "\ngsave\n"; |
865 | 862 |
if(_scaleToA4) |
866 | 863 |
if(bb.height()>bb.width()) { |
867 | 864 |
double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.height(), |
868 | 865 |
(A4WIDTH-2*A4BORDER)/bb.width()); |
869 | 866 |
os << ((A4WIDTH -2*A4BORDER)-sc*bb.width())/2 + A4BORDER << ' ' |
870 | 867 |
<< ((A4HEIGHT-2*A4BORDER)-sc*bb.height())/2 + A4BORDER |
871 | 868 |
<< " translate\n" |
872 | 869 |
<< sc << " dup scale\n" |
873 | 870 |
<< -bb.left() << ' ' << -bb.bottom() << " translate\n"; |
874 | 871 |
} |
875 | 872 |
else { |
876 |
//\todo Verify centering |
|
877 | 873 |
double sc= std::min((A4HEIGHT-2*A4BORDER)/bb.width(), |
878 | 874 |
(A4WIDTH-2*A4BORDER)/bb.height()); |
879 | 875 |
os << ((A4WIDTH -2*A4BORDER)-sc*bb.height())/2 + A4BORDER << ' ' |
880 | 876 |
<< ((A4HEIGHT-2*A4BORDER)-sc*bb.width())/2 + A4BORDER |
881 | 877 |
<< " translate\n" |
882 | 878 |
<< sc << " dup scale\n90 rotate\n" |
883 | 879 |
<< -bb.left() << ' ' << -bb.top() << " translate\n"; |
884 | 880 |
} |
885 | 881 |
else if(_scale!=1.0) os << _scale << " dup scale\n"; |
886 | 882 |
|
887 | 883 |
if(_showArcs) { |
888 | 884 |
os << "%Arcs:\ngsave\n"; |
889 | 885 |
if(_enableParallel) { |
890 | 886 |
std::vector<Arc> el; |
891 | 887 |
for(ArcIt e(g);e!=INVALID;++e) |
892 | 888 |
if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0 |
893 | 889 |
&&g.source(e)!=g.target(e)) |
894 | 890 |
el.push_back(e); |
895 | 891 |
std::sort(el.begin(),el.end(),arcLess(g)); |
896 | 892 |
|
897 | 893 |
typename std::vector<Arc>::iterator j; |
898 | 894 |
for(typename std::vector<Arc>::iterator i=el.begin();i!=el.end();i=j) { |
899 | 895 |
for(j=i+1;j!=el.end()&&isParallel(*i,*j);++j) ; |
900 | 896 |
|
901 | 897 |
double sw=0; |
902 | 898 |
for(typename std::vector<Arc>::iterator e=i;e!=j;++e) |
903 | 899 |
sw+=_arcWidths[*e]*_arcWidthScale+_parArcDist; |
904 | 900 |
sw-=_parArcDist; |
905 | 901 |
sw/=-2.0; |
906 | 902 |
dim2::Point<double> |
907 | 903 |
dvec(mycoords[g.target(*i)]-mycoords[g.source(*i)]); |
908 | 904 |
double l=std::sqrt(dvec.normSquare()); |
909 |
//\todo better 'epsilon' would be nice here. |
|
910 | 905 |
dim2::Point<double> d(dvec/std::max(l,EPSILON)); |
911 | 906 |
dim2::Point<double> m; |
912 | 907 |
// m=dim2::Point<double>(mycoords[g.target(*i)]+ |
913 | 908 |
// mycoords[g.source(*i)])/2.0; |
914 | 909 |
|
915 | 910 |
// m=dim2::Point<double>(mycoords[g.source(*i)])+ |
916 | 911 |
// dvec*(double(_nodeSizes[g.source(*i)])/ |
917 | 912 |
// (_nodeSizes[g.source(*i)]+_nodeSizes[g.target(*i)])); |
918 | 913 |
|
919 | 914 |
m=dim2::Point<double>(mycoords[g.source(*i)])+ |
920 | 915 |
d*(l+_nodeSizes[g.source(*i)]-_nodeSizes[g.target(*i)])/2.0; |
921 | 916 |
|
922 | 917 |
for(typename std::vector<Arc>::iterator e=i;e!=j;++e) { |
923 | 918 |
sw+=_arcWidths[*e]*_arcWidthScale/2.0; |
924 | 919 |
dim2::Point<double> mm=m+rot90(d)*sw/.75; |
925 | 920 |
if(_drawArrows) { |
926 | 921 |
int node_shape; |
927 | 922 |
dim2::Point<double> s=mycoords[g.source(*e)]; |
928 | 923 |
dim2::Point<double> t=mycoords[g.target(*e)]; |
929 | 924 |
double rn=_nodeSizes[g.target(*e)]*_nodeScale; |
930 | 925 |
node_shape=_nodeShapes[g.target(*e)]; |
931 | 926 |
dim2::Bezier3 bez(s,mm,mm,t); |
932 | 927 |
double t1=0,t2=1; |
933 | 928 |
for(int ii=0;ii<INTERPOL_PREC;++ii) |
934 | 929 |
if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) t2=(t1+t2)/2; |
935 | 930 |
else t1=(t1+t2)/2; |
936 | 931 |
dim2::Point<double> apoint=bez((t1+t2)/2); |
937 | 932 |
rn = _arrowLength+_arcWidths[*e]*_arcWidthScale; |
938 | 933 |
rn*=rn; |
939 | 934 |
t2=(t1+t2)/2;t1=0; |
940 | 935 |
for(int ii=0;ii<INTERPOL_PREC;++ii) |
941 | 936 |
if((bez((t1+t2)/2)-apoint).normSquare()>rn) t1=(t1+t2)/2; |
942 | 937 |
else t2=(t1+t2)/2; |
943 | 938 |
dim2::Point<double> linend=bez((t1+t2)/2); |
944 | 939 |
bez=bez.before((t1+t2)/2); |
945 | 940 |
// rn=_nodeSizes[g.source(*e)]*_nodeScale; |
946 | 941 |
// node_shape=_nodeShapes[g.source(*e)]; |
947 | 942 |
// t1=0;t2=1; |
948 | 943 |
// for(int i=0;i<INTERPOL_PREC;++i) |
949 | 944 |
// if(isInsideNode(bez((t1+t2)/2)-t,rn,node_shape)) |
950 | 945 |
// t1=(t1+t2)/2; |
951 | 946 |
// else t2=(t1+t2)/2; |
952 | 947 |
// bez=bez.after((t1+t2)/2); |
953 | 948 |
os << _arcWidths[*e]*_arcWidthScale << " setlinewidth " |
954 | 949 |
<< _arcColors[*e].red() << ' ' |
955 | 950 |
<< _arcColors[*e].green() << ' ' |
956 | 951 |
<< _arcColors[*e].blue() << " setrgbcolor newpath\n" |
957 | 952 |
<< bez.p1.x << ' ' << bez.p1.y << " moveto\n" |
958 | 953 |
<< bez.p2.x << ' ' << bez.p2.y << ' ' |
959 | 954 |
<< bez.p3.x << ' ' << bez.p3.y << ' ' |
960 | 955 |
<< bez.p4.x << ' ' << bez.p4.y << " curveto stroke\n"; |
961 | 956 |
dim2::Point<double> dd(rot90(linend-apoint)); |
962 | 957 |
dd*=(.5*_arcWidths[*e]*_arcWidthScale+_arrowWidth)/ |
963 | 958 |
std::sqrt(dd.normSquare()); |
964 | 959 |
os << "newpath " << psOut(apoint) << " moveto " |
965 | 960 |
<< psOut(linend+dd) << " lineto " |
966 | 961 |
<< psOut(linend-dd) << " lineto closepath fill\n"; |
967 | 962 |
} |
968 | 963 |
else { |
969 | 964 |
os << mycoords[g.source(*e)].x << ' ' |
970 | 965 |
<< mycoords[g.source(*e)].y << ' ' |
971 | 966 |
<< mm.x << ' ' << mm.y << ' ' |
972 | 967 |
<< mycoords[g.target(*e)].x << ' ' |
973 | 968 |
<< mycoords[g.target(*e)].y << ' ' |
974 | 969 |
<< _arcColors[*e].red() << ' ' |
975 | 970 |
<< _arcColors[*e].green() << ' ' |
976 | 971 |
<< _arcColors[*e].blue() << ' ' |
977 | 972 |
<< _arcWidths[*e]*_arcWidthScale << " lb\n"; |
978 | 973 |
} |
979 | 974 |
sw+=_arcWidths[*e]*_arcWidthScale/2.0+_parArcDist; |
980 | 975 |
} |
981 | 976 |
} |
982 | 977 |
} |
983 | 978 |
else for(ArcIt e(g);e!=INVALID;++e) |
984 | 979 |
if((!_undirected||g.source(e)<g.target(e))&&_arcWidths[e]>0 |
985 | 980 |
&&g.source(e)!=g.target(e)) { |
986 | 981 |
if(_drawArrows) { |
987 | 982 |
dim2::Point<double> d(mycoords[g.target(e)]-mycoords[g.source(e)]); |
988 | 983 |
double rn=_nodeSizes[g.target(e)]*_nodeScale; |
989 | 984 |
int node_shape=_nodeShapes[g.target(e)]; |
990 | 985 |
double t1=0,t2=1; |
991 | 986 |
for(int i=0;i<INTERPOL_PREC;++i) |
992 | 987 |
if(isInsideNode((-(t1+t2)/2)*d,rn,node_shape)) t1=(t1+t2)/2; |
993 | 988 |
else t2=(t1+t2)/2; |
994 | 989 |
double l=std::sqrt(d.normSquare()); |
995 | 990 |
d/=l; |
996 | 991 |
|
997 | 992 |
os << l*(1-(t1+t2)/2) << ' ' |
998 | 993 |
<< _arcWidths[e]*_arcWidthScale << ' ' |
999 | 994 |
<< d.x << ' ' << d.y << ' ' |
1000 | 995 |
<< mycoords[g.source(e)].x << ' ' |
1001 | 996 |
<< mycoords[g.source(e)].y << ' ' |
1002 | 997 |
<< _arcColors[e].red() << ' ' |
1003 | 998 |
<< _arcColors[e].green() << ' ' |
1004 | 999 |
<< _arcColors[e].blue() << " arr\n"; |
1005 | 1000 |
} |
1006 | 1001 |
else os << mycoords[g.source(e)].x << ' ' |
1007 | 1002 |
<< mycoords[g.source(e)].y << ' ' |
1008 | 1003 |
<< mycoords[g.target(e)].x << ' ' |
1009 | 1004 |
<< mycoords[g.target(e)].y << ' ' |
1010 | 1005 |
<< _arcColors[e].red() << ' ' |
1011 | 1006 |
<< _arcColors[e].green() << ' ' |
1012 | 1007 |
<< _arcColors[e].blue() << ' ' |
1013 | 1008 |
<< _arcWidths[e]*_arcWidthScale << " l\n"; |
1014 | 1009 |
} |
1015 | 1010 |
os << "grestore\n"; |
1016 | 1011 |
} |
1017 | 1012 |
if(_showNodes) { |
1018 | 1013 |
os << "%Nodes:\ngsave\n"; |
1019 | 1014 |
for(NodeIt n(g);n!=INVALID;++n) { |
1020 | 1015 |
os << mycoords[n].x << ' ' << mycoords[n].y << ' ' |
1021 | 1016 |
<< _nodeSizes[n]*_nodeScale << ' ' |
1022 | 1017 |
<< _nodeColors[n].red() << ' ' |
1023 | 1018 |
<< _nodeColors[n].green() << ' ' |
1024 | 1019 |
<< _nodeColors[n].blue() << ' '; |
1025 | 1020 |
switch(_nodeShapes[n]) { |
1026 | 1021 |
case CIRCLE: |
1027 | 1022 |
os<< "nc";break; |
1028 | 1023 |
case SQUARE: |
1029 | 1024 |
os<< "nsq";break; |
1030 | 1025 |
case DIAMOND: |
1031 | 1026 |
os<< "ndi";break; |
1032 | 1027 |
case MALE: |
1033 | 1028 |
os<< "nmale";break; |
1034 | 1029 |
case FEMALE: |
1035 | 1030 |
os<< "nfemale";break; |
1036 | 1031 |
} |
1037 | 1032 |
os<<'\n'; |
1038 | 1033 |
} |
1039 | 1034 |
os << "grestore\n"; |
1040 | 1035 |
} |
1041 | 1036 |
if(_showNodeText) { |
1042 | 1037 |
os << "%Node texts:\ngsave\n"; |
1043 | 1038 |
os << "/fosi " << _nodeTextSize << " def\n"; |
1044 | 1039 |
os << "(Helvetica) findfont fosi scalefont setfont\n"; |
1045 | 1040 |
for(NodeIt n(g);n!=INVALID;++n) { |
1046 | 1041 |
switch(_nodeTextColorType) { |
1047 | 1042 |
case DIST_COL: |
1048 | 1043 |
os << psOut(distantColor(_nodeColors[n])) << " setrgbcolor\n"; |
1049 | 1044 |
break; |
1050 | 1045 |
case DIST_BW: |
1051 | 1046 |
os << psOut(distantBW(_nodeColors[n])) << " setrgbcolor\n"; |
1052 | 1047 |
break; |
1053 | 1048 |
case CUST_COL: |
1054 | 1049 |
os << psOut(distantColor(_nodeTextColors[n])) << " setrgbcolor\n"; |
1055 | 1050 |
break; |
1056 | 1051 |
default: |
1057 | 1052 |
os << "0 0 0 setrgbcolor\n"; |
1058 | 1053 |
} |
1059 | 1054 |
os << mycoords[n].x << ' ' << mycoords[n].y |
1060 | 1055 |
<< " (" << _nodeTexts[n] << ") cshow\n"; |
1061 | 1056 |
} |
1062 | 1057 |
os << "grestore\n"; |
1063 | 1058 |
} |
1064 | 1059 |
if(_showNodePsText) { |
1065 | 1060 |
os << "%Node PS blocks:\ngsave\n"; |
1066 | 1061 |
for(NodeIt n(g);n!=INVALID;++n) |
1067 | 1062 |
os << mycoords[n].x << ' ' << mycoords[n].y |
1068 | 1063 |
<< " moveto\n" << _nodePsTexts[n] << "\n"; |
1069 | 1064 |
os << "grestore\n"; |
1070 | 1065 |
} |
1071 | 1066 |
|
1072 | 1067 |
os << "grestore\nshowpage\n"; |
1073 | 1068 |
|
1074 | 1069 |
//CleanUp: |
1075 | 1070 |
if(_pleaseRemoveOsStream) {delete &os;} |
1076 | 1071 |
} |
1077 | 1072 |
|
1078 | 1073 |
///\name Aliases |
1079 | 1074 |
///These are just some aliases to other parameter setting functions. |
1080 | 1075 |
|
1081 | 1076 |
///@{ |
1082 | 1077 |
|
1083 | 1078 |
///An alias for arcWidths() |
1084 | 1079 |
template<class X> GraphToEps<ArcWidthsTraits<X> > edgeWidths(const X &x) |
1085 | 1080 |
{ |
1086 | 1081 |
return arcWidths(x); |
1087 | 1082 |
} |
1088 | 1083 |
|
1089 | 1084 |
///An alias for arcColors() |
1090 | 1085 |
template<class X> GraphToEps<ArcColorsTraits<X> > |
1091 | 1086 |
edgeColors(const X &x) |
1092 | 1087 |
{ |
1093 | 1088 |
return arcColors(x); |
1094 | 1089 |
} |
1095 | 1090 |
|
1096 | 1091 |
///An alias for arcWidthScale() |
1097 | 1092 |
GraphToEps<T> &edgeWidthScale(double d) {return arcWidthScale(d);} |
1098 | 1093 |
|
1099 | 1094 |
///An alias for autoArcWidthScale() |
1100 | 1095 |
GraphToEps<T> &autoEdgeWidthScale(bool b=true) |
1101 | 1096 |
{ |
1102 | 1097 |
return autoArcWidthScale(b); |
1103 | 1098 |
} |
1104 | 1099 |
|
1105 | 1100 |
///An alias for absoluteArcWidths() |
1106 | 1101 |
GraphToEps<T> &absoluteEdgeWidths(bool b=true) |
1107 | 1102 |
{ |
1108 | 1103 |
return absoluteArcWidths(b); |
1109 | 1104 |
} |
1110 | 1105 |
|
1111 | 1106 |
///An alias for parArcDist() |
1112 | 1107 |
GraphToEps<T> &parEdgeDist(double d) {return parArcDist(d);} |
1113 | 1108 |
|
1114 | 1109 |
///An alias for hideArcs() |
1115 | 1110 |
GraphToEps<T> &hideEdges(bool b=true) {return hideArcs(b);} |
1116 | 1111 |
|
1117 | 1112 |
///@} |
1118 | 1113 |
}; |
1119 | 1114 |
|
1120 | 1115 |
template<class T> |
1121 | 1116 |
const int GraphToEps<T>::INTERPOL_PREC = 20; |
1122 | 1117 |
template<class T> |
1123 | 1118 |
const double GraphToEps<T>::A4HEIGHT = 841.8897637795276; |
1124 | 1119 |
template<class T> |
1125 | 1120 |
const double GraphToEps<T>::A4WIDTH = 595.275590551181; |
1126 | 1121 |
template<class T> |
1127 | 1122 |
const double GraphToEps<T>::A4BORDER = 15; |
1128 | 1123 |
|
1129 | 1124 |
|
1130 | 1125 |
///Generates an EPS file from a graph |
1131 | 1126 |
|
1132 | 1127 |
///\ingroup eps_io |
1133 | 1128 |
///Generates an EPS file from a graph. |
1134 | 1129 |
///\param g Reference to the graph to be printed. |
1135 | 1130 |
///\param os Reference to the output stream. |
1136 | 1131 |
///By default it is <tt>std::cout</tt>. |
1137 | 1132 |
/// |
1138 | 1133 |
///This function also has a lot of |
1139 | 1134 |
///\ref named-templ-func-param "named parameters", |
1140 | 1135 |
///they are declared as the members of class \ref GraphToEps. The following |
1141 | 1136 |
///example shows how to use these parameters. |
1142 | 1137 |
///\code |
1143 | 1138 |
/// graphToEps(g,os).scale(10).coords(coords) |
1144 | 1139 |
/// .nodeScale(2).nodeSizes(sizes) |
1145 | 1140 |
/// .arcWidthScale(.4).run(); |
1146 | 1141 |
///\endcode |
1147 | 1142 |
/// |
1148 | 1143 |
///For more detailed examples see the \ref graph_to_eps_demo.cc demo file. |
1149 | 1144 |
/// |
1150 | 1145 |
///\warning Don't forget to put the \ref GraphToEps::run() "run()" |
1151 | 1146 |
///to the end of the parameter list. |
1152 | 1147 |
///\sa GraphToEps |
1153 | 1148 |
///\sa graphToEps(G &g, const char *file_name) |
1154 | 1149 |
template<class G> |
1155 | 1150 |
GraphToEps<DefaultGraphToEpsTraits<G> > |
1156 | 1151 |
graphToEps(G &g, std::ostream& os=std::cout) |
1157 | 1152 |
{ |
1158 | 1153 |
return |
1159 | 1154 |
GraphToEps<DefaultGraphToEpsTraits<G> >(DefaultGraphToEpsTraits<G>(g,os)); |
1160 | 1155 |
} |
1161 | 1156 |
|
1162 | 1157 |
///Generates an EPS file from a graph |
1163 | 1158 |
|
1164 | 1159 |
///\ingroup eps_io |
1165 | 1160 |
///This function does the same as |
1166 | 1161 |
///\ref graphToEps(G &g,std::ostream& os) |
1167 | 1162 |
///but it writes its output into the file \c file_name |
1168 | 1163 |
///instead of a stream. |
1169 | 1164 |
///\sa graphToEps(G &g, std::ostream& os) |
1170 | 1165 |
template<class G> |
1171 | 1166 |
GraphToEps<DefaultGraphToEpsTraits<G> > |
1172 | 1167 |
graphToEps(G &g,const char *file_name) |
1173 | 1168 |
{ |
1174 | 1169 |
return GraphToEps<DefaultGraphToEpsTraits<G> > |
1175 | 1170 |
(DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name),true)); |
1176 | 1171 |
} |
1177 | 1172 |
|
1178 | 1173 |
///Generates an EPS file from a graph |
1179 | 1174 |
|
1180 | 1175 |
///\ingroup eps_io |
1181 | 1176 |
///This function does the same as |
1182 | 1177 |
///\ref graphToEps(G &g,std::ostream& os) |
1183 | 1178 |
///but it writes its output into the file \c file_name |
1184 | 1179 |
///instead of a stream. |
1185 | 1180 |
///\sa graphToEps(G &g, std::ostream& os) |
1186 | 1181 |
template<class G> |
1187 | 1182 |
GraphToEps<DefaultGraphToEpsTraits<G> > |
1188 | 1183 |
graphToEps(G &g,const std::string& file_name) |
1189 | 1184 |
{ |
1190 | 1185 |
return GraphToEps<DefaultGraphToEpsTraits<G> > |
1191 | 1186 |
(DefaultGraphToEpsTraits<G>(g,*new std::ofstream(file_name.c_str()),true)); |
1192 | 1187 |
} |
1193 | 1188 |
|
1194 | 1189 |
} //END OF NAMESPACE LEMON |
1195 | 1190 |
|
1196 | 1191 |
#endif // LEMON_GRAPH_TO_EPS_H |
... | ... |
@@ -120,772 +120,770 @@ |
120 | 120 |
n = nodes[n].next) {} |
121 | 121 |
arc.id = (n == -1) ? -1 : nodes[n].first_in; |
122 | 122 |
} |
123 | 123 |
|
124 | 124 |
void next(Arc& arc) const { |
125 | 125 |
if (arcs[arc.id].next_in != -1) { |
126 | 126 |
arc.id = arcs[arc.id].next_in; |
127 | 127 |
} else { |
128 | 128 |
int n; |
129 | 129 |
for(n = nodes[arcs[arc.id].target].next; |
130 | 130 |
n!=-1 && nodes[n].first_in == -1; |
131 | 131 |
n = nodes[n].next) {} |
132 | 132 |
arc.id = (n == -1) ? -1 : nodes[n].first_in; |
133 | 133 |
} |
134 | 134 |
} |
135 | 135 |
|
136 | 136 |
void firstOut(Arc &e, const Node& v) const { |
137 | 137 |
e.id = nodes[v.id].first_out; |
138 | 138 |
} |
139 | 139 |
void nextOut(Arc &e) const { |
140 | 140 |
e.id=arcs[e.id].next_out; |
141 | 141 |
} |
142 | 142 |
|
143 | 143 |
void firstIn(Arc &e, const Node& v) const { |
144 | 144 |
e.id = nodes[v.id].first_in; |
145 | 145 |
} |
146 | 146 |
void nextIn(Arc &e) const { |
147 | 147 |
e.id=arcs[e.id].next_in; |
148 | 148 |
} |
149 | 149 |
|
150 | 150 |
|
151 | 151 |
static int id(Node v) { return v.id; } |
152 | 152 |
static int id(Arc e) { return e.id; } |
153 | 153 |
|
154 | 154 |
static Node nodeFromId(int id) { return Node(id);} |
155 | 155 |
static Arc arcFromId(int id) { return Arc(id);} |
156 | 156 |
|
157 | 157 |
bool valid(Node n) const { |
158 | 158 |
return n.id >= 0 && n.id < static_cast<int>(nodes.size()) && |
159 | 159 |
nodes[n.id].prev != -2; |
160 | 160 |
} |
161 | 161 |
|
162 | 162 |
bool valid(Arc a) const { |
163 | 163 |
return a.id >= 0 && a.id < static_cast<int>(arcs.size()) && |
164 | 164 |
arcs[a.id].prev_in != -2; |
165 | 165 |
} |
166 | 166 |
|
167 | 167 |
Node addNode() { |
168 | 168 |
int n; |
169 | 169 |
|
170 | 170 |
if(first_free_node==-1) { |
171 | 171 |
n = nodes.size(); |
172 | 172 |
nodes.push_back(NodeT()); |
173 | 173 |
} else { |
174 | 174 |
n = first_free_node; |
175 | 175 |
first_free_node = nodes[n].next; |
176 | 176 |
} |
177 | 177 |
|
178 | 178 |
nodes[n].next = first_node; |
179 | 179 |
if(first_node != -1) nodes[first_node].prev = n; |
180 | 180 |
first_node = n; |
181 | 181 |
nodes[n].prev = -1; |
182 | 182 |
|
183 | 183 |
nodes[n].first_in = nodes[n].first_out = -1; |
184 | 184 |
|
185 | 185 |
return Node(n); |
186 | 186 |
} |
187 | 187 |
|
188 | 188 |
Arc addArc(Node u, Node v) { |
189 | 189 |
int n; |
190 | 190 |
|
191 | 191 |
if (first_free_arc == -1) { |
192 | 192 |
n = arcs.size(); |
193 | 193 |
arcs.push_back(ArcT()); |
194 | 194 |
} else { |
195 | 195 |
n = first_free_arc; |
196 | 196 |
first_free_arc = arcs[n].next_in; |
197 | 197 |
} |
198 | 198 |
|
199 | 199 |
arcs[n].source = u.id; |
200 | 200 |
arcs[n].target = v.id; |
201 | 201 |
|
202 | 202 |
arcs[n].next_out = nodes[u.id].first_out; |
203 | 203 |
if(nodes[u.id].first_out != -1) { |
204 | 204 |
arcs[nodes[u.id].first_out].prev_out = n; |
205 | 205 |
} |
206 | 206 |
|
207 | 207 |
arcs[n].next_in = nodes[v.id].first_in; |
208 | 208 |
if(nodes[v.id].first_in != -1) { |
209 | 209 |
arcs[nodes[v.id].first_in].prev_in = n; |
210 | 210 |
} |
211 | 211 |
|
212 | 212 |
arcs[n].prev_in = arcs[n].prev_out = -1; |
213 | 213 |
|
214 | 214 |
nodes[u.id].first_out = nodes[v.id].first_in = n; |
215 | 215 |
|
216 | 216 |
return Arc(n); |
217 | 217 |
} |
218 | 218 |
|
219 | 219 |
void erase(const Node& node) { |
220 | 220 |
int n = node.id; |
221 | 221 |
|
222 | 222 |
if(nodes[n].next != -1) { |
223 | 223 |
nodes[nodes[n].next].prev = nodes[n].prev; |
224 | 224 |
} |
225 | 225 |
|
226 | 226 |
if(nodes[n].prev != -1) { |
227 | 227 |
nodes[nodes[n].prev].next = nodes[n].next; |
228 | 228 |
} else { |
229 | 229 |
first_node = nodes[n].next; |
230 | 230 |
} |
231 | 231 |
|
232 | 232 |
nodes[n].next = first_free_node; |
233 | 233 |
first_free_node = n; |
234 | 234 |
nodes[n].prev = -2; |
235 | 235 |
|
236 | 236 |
} |
237 | 237 |
|
238 | 238 |
void erase(const Arc& arc) { |
239 | 239 |
int n = arc.id; |
240 | 240 |
|
241 | 241 |
if(arcs[n].next_in!=-1) { |
242 | 242 |
arcs[arcs[n].next_in].prev_in = arcs[n].prev_in; |
243 | 243 |
} |
244 | 244 |
|
245 | 245 |
if(arcs[n].prev_in!=-1) { |
246 | 246 |
arcs[arcs[n].prev_in].next_in = arcs[n].next_in; |
247 | 247 |
} else { |
248 | 248 |
nodes[arcs[n].target].first_in = arcs[n].next_in; |
249 | 249 |
} |
250 | 250 |
|
251 | 251 |
|
252 | 252 |
if(arcs[n].next_out!=-1) { |
253 | 253 |
arcs[arcs[n].next_out].prev_out = arcs[n].prev_out; |
254 | 254 |
} |
255 | 255 |
|
256 | 256 |
if(arcs[n].prev_out!=-1) { |
257 | 257 |
arcs[arcs[n].prev_out].next_out = arcs[n].next_out; |
258 | 258 |
} else { |
259 | 259 |
nodes[arcs[n].source].first_out = arcs[n].next_out; |
260 | 260 |
} |
261 | 261 |
|
262 | 262 |
arcs[n].next_in = first_free_arc; |
263 | 263 |
first_free_arc = n; |
264 | 264 |
arcs[n].prev_in = -2; |
265 | 265 |
} |
266 | 266 |
|
267 | 267 |
void clear() { |
268 | 268 |
arcs.clear(); |
269 | 269 |
nodes.clear(); |
270 | 270 |
first_node = first_free_node = first_free_arc = -1; |
271 | 271 |
} |
272 | 272 |
|
273 | 273 |
protected: |
274 | 274 |
void changeTarget(Arc e, Node n) |
275 | 275 |
{ |
276 | 276 |
if(arcs[e.id].next_in != -1) |
277 | 277 |
arcs[arcs[e.id].next_in].prev_in = arcs[e.id].prev_in; |
278 | 278 |
if(arcs[e.id].prev_in != -1) |
279 | 279 |
arcs[arcs[e.id].prev_in].next_in = arcs[e.id].next_in; |
280 | 280 |
else nodes[arcs[e.id].target].first_in = arcs[e.id].next_in; |
281 | 281 |
if (nodes[n.id].first_in != -1) { |
282 | 282 |
arcs[nodes[n.id].first_in].prev_in = e.id; |
283 | 283 |
} |
284 | 284 |
arcs[e.id].target = n.id; |
285 | 285 |
arcs[e.id].prev_in = -1; |
286 | 286 |
arcs[e.id].next_in = nodes[n.id].first_in; |
287 | 287 |
nodes[n.id].first_in = e.id; |
288 | 288 |
} |
289 | 289 |
void changeSource(Arc e, Node n) |
290 | 290 |
{ |
291 | 291 |
if(arcs[e.id].next_out != -1) |
292 | 292 |
arcs[arcs[e.id].next_out].prev_out = arcs[e.id].prev_out; |
293 | 293 |
if(arcs[e.id].prev_out != -1) |
294 | 294 |
arcs[arcs[e.id].prev_out].next_out = arcs[e.id].next_out; |
295 | 295 |
else nodes[arcs[e.id].source].first_out = arcs[e.id].next_out; |
296 | 296 |
if (nodes[n.id].first_out != -1) { |
297 | 297 |
arcs[nodes[n.id].first_out].prev_out = e.id; |
298 | 298 |
} |
299 | 299 |
arcs[e.id].source = n.id; |
300 | 300 |
arcs[e.id].prev_out = -1; |
301 | 301 |
arcs[e.id].next_out = nodes[n.id].first_out; |
302 | 302 |
nodes[n.id].first_out = e.id; |
303 | 303 |
} |
304 | 304 |
|
305 | 305 |
}; |
306 | 306 |
|
307 | 307 |
typedef DigraphExtender<ListDigraphBase> ExtendedListDigraphBase; |
308 | 308 |
|
309 | 309 |
/// \addtogroup graphs |
310 | 310 |
/// @{ |
311 | 311 |
|
312 | 312 |
///A general directed graph structure. |
313 | 313 |
|
314 | 314 |
///\ref ListDigraph is a simple and fast <em>directed graph</em> |
315 | 315 |
///implementation based on static linked lists that are stored in |
316 | 316 |
///\c std::vector structures. |
317 | 317 |
/// |
318 | 318 |
///It conforms to the \ref concepts::Digraph "Digraph concept" and it |
319 | 319 |
///also provides several useful additional functionalities. |
320 | 320 |
///Most of the member functions and nested classes are documented |
321 | 321 |
///only in the concept class. |
322 | 322 |
/// |
323 | 323 |
///An important extra feature of this digraph implementation is that |
324 | 324 |
///its maps are real \ref concepts::ReferenceMap "reference map"s. |
325 | 325 |
/// |
326 | 326 |
///\sa concepts::Digraph |
327 | 327 |
|
328 | 328 |
class ListDigraph : public ExtendedListDigraphBase { |
329 | 329 |
private: |
330 | 330 |
///ListDigraph is \e not copy constructible. Use copyDigraph() instead. |
331 | 331 |
|
332 | 332 |
///ListDigraph is \e not copy constructible. Use copyDigraph() instead. |
333 | 333 |
/// |
334 | 334 |
ListDigraph(const ListDigraph &) :ExtendedListDigraphBase() {}; |
335 | 335 |
///\brief Assignment of ListDigraph to another one is \e not allowed. |
336 | 336 |
///Use copyDigraph() instead. |
337 | 337 |
|
338 | 338 |
///Assignment of ListDigraph to another one is \e not allowed. |
339 | 339 |
///Use copyDigraph() instead. |
340 | 340 |
void operator=(const ListDigraph &) {} |
341 | 341 |
public: |
342 | 342 |
|
343 | 343 |
typedef ExtendedListDigraphBase Parent; |
344 | 344 |
|
345 | 345 |
/// Constructor |
346 | 346 |
|
347 | 347 |
/// Constructor. |
348 | 348 |
/// |
349 | 349 |
ListDigraph() {} |
350 | 350 |
|
351 | 351 |
///Add a new node to the digraph. |
352 | 352 |
|
353 | 353 |
///Add a new node to the digraph. |
354 | 354 |
///\return the new node. |
355 | 355 |
Node addNode() { return Parent::addNode(); } |
356 | 356 |
|
357 | 357 |
///Add a new arc to the digraph. |
358 | 358 |
|
359 | 359 |
///Add a new arc to the digraph with source node \c s |
360 | 360 |
///and target node \c t. |
361 | 361 |
///\return the new arc. |
362 | 362 |
Arc addArc(const Node& s, const Node& t) { |
363 | 363 |
return Parent::addArc(s, t); |
364 | 364 |
} |
365 | 365 |
|
366 | 366 |
///\brief Erase a node from the digraph. |
367 | 367 |
/// |
368 | 368 |
///Erase a node from the digraph. |
369 | 369 |
/// |
370 | 370 |
void erase(const Node& n) { Parent::erase(n); } |
371 | 371 |
|
372 | 372 |
///\brief Erase an arc from the digraph. |
373 | 373 |
/// |
374 | 374 |
///Erase an arc from the digraph. |
375 | 375 |
/// |
376 | 376 |
void erase(const Arc& a) { Parent::erase(a); } |
377 | 377 |
|
378 | 378 |
/// Node validity check |
379 | 379 |
|
380 | 380 |
/// This function gives back true if the given node is valid, |
381 | 381 |
/// ie. it is a real node of the graph. |
382 | 382 |
/// |
383 | 383 |
/// \warning A Node pointing to a removed item |
384 | 384 |
/// could become valid again later if new nodes are |
385 | 385 |
/// added to the graph. |
386 | 386 |
bool valid(Node n) const { return Parent::valid(n); } |
387 | 387 |
|
388 | 388 |
/// Arc validity check |
389 | 389 |
|
390 | 390 |
/// This function gives back true if the given arc is valid, |
391 | 391 |
/// ie. it is a real arc of the graph. |
392 | 392 |
/// |
393 | 393 |
/// \warning An Arc pointing to a removed item |
394 | 394 |
/// could become valid again later if new nodes are |
395 | 395 |
/// added to the graph. |
396 | 396 |
bool valid(Arc a) const { return Parent::valid(a); } |
397 | 397 |
|
398 | 398 |
/// Change the target of \c a to \c n |
399 | 399 |
|
400 | 400 |
/// Change the target of \c a to \c n |
401 | 401 |
/// |
402 | 402 |
///\note The <tt>ArcIt</tt>s and <tt>OutArcIt</tt>s referencing |
403 | 403 |
///the changed arc remain valid. However <tt>InArcIt</tt>s are |
404 | 404 |
///invalidated. |
405 | 405 |
/// |
406 | 406 |
///\warning This functionality cannot be used together with the Snapshot |
407 | 407 |
///feature. |
408 | 408 |
void changeTarget(Arc a, Node n) { |
409 | 409 |
Parent::changeTarget(a,n); |
410 | 410 |
} |
411 | 411 |
/// Change the source of \c a to \c n |
412 | 412 |
|
413 | 413 |
/// Change the source of \c a to \c n |
414 | 414 |
/// |
415 | 415 |
///\note The <tt>InArcIt</tt>s referencing the changed arc remain |
416 | 416 |
///valid. However the <tt>ArcIt<tt>s and <tt>OutArcIt</tt>s are |
417 | 417 |
///invalidated. |
418 | 418 |
/// |
419 | 419 |
///\warning This functionality cannot be used together with the Snapshot |
420 | 420 |
///feature. |
421 | 421 |
void changeSource(Arc a, Node n) { |
422 | 422 |
Parent::changeSource(a,n); |
423 | 423 |
} |
424 | 424 |
|
425 | 425 |
/// Invert the direction of an arc. |
426 | 426 |
|
427 | 427 |
///\note The <tt>ArcIt</tt>s referencing the changed arc remain |
428 | 428 |
///valid. However <tt>OutArcIt</tt>s and <tt>InArcIt</tt>s are |
429 | 429 |
///invalidated. |
430 | 430 |
/// |
431 | 431 |
///\warning This functionality cannot be used together with the Snapshot |
432 | 432 |
///feature. |
433 | 433 |
void reverseArc(Arc e) { |
434 | 434 |
Node t=target(e); |
435 | 435 |
changeTarget(e,source(e)); |
436 | 436 |
changeSource(e,t); |
437 | 437 |
} |
438 | 438 |
|
439 | 439 |
/// Reserve memory for nodes. |
440 | 440 |
|
441 | 441 |
/// Using this function it is possible to avoid the superfluous memory |
442 | 442 |
/// allocation: if you know that the digraph you want to build will |
443 | 443 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
444 | 444 |
/// then it is worth reserving space for this amount before starting |
445 | 445 |
/// to build the digraph. |
446 | 446 |
/// \sa reserveArc |
447 | 447 |
void reserveNode(int n) { nodes.reserve(n); }; |
448 | 448 |
|
449 | 449 |
/// Reserve memory for arcs. |
450 | 450 |
|
451 | 451 |
/// Using this function it is possible to avoid the superfluous memory |
452 | 452 |
/// allocation: if you know that the digraph you want to build will |
453 | 453 |
/// be very large (e.g. it will contain millions of nodes and/or arcs) |
454 | 454 |
/// then it is worth reserving space for this amount before starting |
455 | 455 |
/// to build the digraph. |
456 | 456 |
/// \sa reserveNode |
457 | 457 |
void reserveArc(int m) { arcs.reserve(m); }; |
458 | 458 |
|
459 | 459 |
///Contract two nodes. |
460 | 460 |
|
461 | 461 |
///This function contracts two nodes. |
462 | 462 |
///Node \p b will be removed but instead of deleting |
463 | 463 |
///incident arcs, they will be joined to \p a. |
464 | 464 |
///The last parameter \p r controls whether to remove loops. \c true |
465 | 465 |
///means that loops will be removed. |
466 | 466 |
/// |
467 | 467 |
///\note The <tt>ArcIt</tt>s referencing a moved arc remain |
468 | 468 |
///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s |
469 | 469 |
///may be invalidated. |
470 | 470 |
/// |
471 | 471 |
///\warning This functionality cannot be used together with the Snapshot |
472 | 472 |
///feature. |
473 | 473 |
void contract(Node a, Node b, bool r = true) |
474 | 474 |
{ |
475 | 475 |
for(OutArcIt e(*this,b);e!=INVALID;) { |
476 | 476 |
OutArcIt f=e; |
477 | 477 |
++f; |
478 | 478 |
if(r && target(e)==a) erase(e); |
479 | 479 |
else changeSource(e,a); |
480 | 480 |
e=f; |
481 | 481 |
} |
482 | 482 |
for(InArcIt e(*this,b);e!=INVALID;) { |
483 | 483 |
InArcIt f=e; |
484 | 484 |
++f; |
485 | 485 |
if(r && source(e)==a) erase(e); |
486 | 486 |
else changeTarget(e,a); |
487 | 487 |
e=f; |
488 | 488 |
} |
489 | 489 |
erase(b); |
490 | 490 |
} |
491 | 491 |
|
492 | 492 |
///Split a node. |
493 | 493 |
|
494 | 494 |
///This function splits a node. First a new node is added to the digraph, |
495 | 495 |
///then the source of each outgoing arc of \c n is moved to this new node. |
496 | 496 |
///If \c connect is \c true (this is the default value), then a new arc |
497 | 497 |
///from \c n to the newly created node is also added. |
498 | 498 |
///\return The newly created node. |
499 | 499 |
/// |
500 | 500 |
///\note The <tt>ArcIt</tt>s referencing a moved arc remain |
501 | 501 |
///valid. However <tt>InArcIt</tt>s and <tt>OutArcIt</tt>s may |
502 | 502 |
///be invalidated. |
503 | 503 |
/// |
504 |
///\warning This functionality cannot be used |
|
504 |
///\warning This functionality cannot be used in conjunction with the |
|
505 | 505 |
///Snapshot feature. |
506 |
/// |
|
507 |
///\todo It could be implemented in a bit faster way. |
|
508 | 506 |
Node split(Node n, bool connect = true) { |
509 | 507 |
Node b = addNode(); |
510 | 508 |
for(OutArcIt e(*this,n);e!=INVALID;) { |
511 | 509 |
OutArcIt f=e; |
512 | 510 |
++f; |
513 | 511 |
changeSource(e,b); |
514 | 512 |
e=f; |
515 | 513 |
} |
516 | 514 |
if (connect) addArc(n,b); |
517 | 515 |
return b; |
518 | 516 |
} |
519 | 517 |
|
520 | 518 |
///Split an arc. |
521 | 519 |
|
522 | 520 |
///This function splits an arc. First a new node \c b is added to |
523 | 521 |
///the digraph, then the original arc is re-targeted to \c |
524 | 522 |
///b. Finally an arc from \c b to the original target is added. |
525 | 523 |
/// |
526 | 524 |
///\return The newly created node. |
527 | 525 |
/// |
528 | 526 |
///\warning This functionality cannot be used together with the |
529 | 527 |
///Snapshot feature. |
530 | 528 |
Node split(Arc e) { |
531 | 529 |
Node b = addNode(); |
532 | 530 |
addArc(b,target(e)); |
533 | 531 |
changeTarget(e,b); |
534 | 532 |
return b; |
535 | 533 |
} |
536 | 534 |
|
537 | 535 |
/// \brief Class to make a snapshot of the digraph and restore |
538 | 536 |
/// it later. |
539 | 537 |
/// |
540 | 538 |
/// Class to make a snapshot of the digraph and restore it later. |
541 | 539 |
/// |
542 | 540 |
/// The newly added nodes and arcs can be removed using the |
543 | 541 |
/// restore() function. |
544 | 542 |
/// |
545 | 543 |
/// \warning Arc and node deletions and other modifications (e.g. |
546 | 544 |
/// contracting, splitting, reversing arcs or nodes) cannot be |
547 | 545 |
/// restored. These events invalidate the snapshot. |
548 | 546 |
class Snapshot { |
549 | 547 |
protected: |
550 | 548 |
|
551 | 549 |
typedef Parent::NodeNotifier NodeNotifier; |
552 | 550 |
|
553 | 551 |
class NodeObserverProxy : public NodeNotifier::ObserverBase { |
554 | 552 |
public: |
555 | 553 |
|
556 | 554 |
NodeObserverProxy(Snapshot& _snapshot) |
557 | 555 |
: snapshot(_snapshot) {} |
558 | 556 |
|
559 | 557 |
using NodeNotifier::ObserverBase::attach; |
560 | 558 |
using NodeNotifier::ObserverBase::detach; |
561 | 559 |
using NodeNotifier::ObserverBase::attached; |
562 | 560 |
|
563 | 561 |
protected: |
564 | 562 |
|
565 | 563 |
virtual void add(const Node& node) { |
566 | 564 |
snapshot.addNode(node); |
567 | 565 |
} |
568 | 566 |
virtual void add(const std::vector<Node>& nodes) { |
569 | 567 |
for (int i = nodes.size() - 1; i >= 0; ++i) { |
570 | 568 |
snapshot.addNode(nodes[i]); |
571 | 569 |
} |
572 | 570 |
} |
573 | 571 |
virtual void erase(const Node& node) { |
574 | 572 |
snapshot.eraseNode(node); |
575 | 573 |
} |
576 | 574 |
virtual void erase(const std::vector<Node>& nodes) { |
577 | 575 |
for (int i = 0; i < int(nodes.size()); ++i) { |
578 | 576 |
snapshot.eraseNode(nodes[i]); |
579 | 577 |
} |
580 | 578 |
} |
581 | 579 |
virtual void build() { |
582 | 580 |
Node node; |
583 | 581 |
std::vector<Node> nodes; |
584 | 582 |
for (notifier()->first(node); node != INVALID; |
585 | 583 |
notifier()->next(node)) { |
586 | 584 |
nodes.push_back(node); |
587 | 585 |
} |
588 | 586 |
for (int i = nodes.size() - 1; i >= 0; --i) { |
589 | 587 |
snapshot.addNode(nodes[i]); |
590 | 588 |
} |
591 | 589 |
} |
592 | 590 |
virtual void clear() { |
593 | 591 |
Node node; |
594 | 592 |
for (notifier()->first(node); node != INVALID; |
595 | 593 |
notifier()->next(node)) { |
596 | 594 |
snapshot.eraseNode(node); |
597 | 595 |
} |
598 | 596 |
} |
599 | 597 |
|
600 | 598 |
Snapshot& snapshot; |
601 | 599 |
}; |
602 | 600 |
|
603 | 601 |
class ArcObserverProxy : public ArcNotifier::ObserverBase { |
604 | 602 |
public: |
605 | 603 |
|
606 | 604 |
ArcObserverProxy(Snapshot& _snapshot) |
607 | 605 |
: snapshot(_snapshot) {} |
608 | 606 |
|
609 | 607 |
using ArcNotifier::ObserverBase::attach; |
610 | 608 |
using ArcNotifier::ObserverBase::detach; |
611 | 609 |
using ArcNotifier::ObserverBase::attached; |
612 | 610 |
|
613 | 611 |
protected: |
614 | 612 |
|
615 | 613 |
virtual void add(const Arc& arc) { |
616 | 614 |
snapshot.addArc(arc); |
617 | 615 |
} |
618 | 616 |
virtual void add(const std::vector<Arc>& arcs) { |
619 | 617 |
for (int i = arcs.size() - 1; i >= 0; ++i) { |
620 | 618 |
snapshot.addArc(arcs[i]); |
621 | 619 |
} |
622 | 620 |
} |
623 | 621 |
virtual void erase(const Arc& arc) { |
624 | 622 |
snapshot.eraseArc(arc); |
625 | 623 |
} |
626 | 624 |
virtual void erase(const std::vector<Arc>& arcs) { |
627 | 625 |
for (int i = 0; i < int(arcs.size()); ++i) { |
628 | 626 |
snapshot.eraseArc(arcs[i]); |
629 | 627 |
} |
630 | 628 |
} |
631 | 629 |
virtual void build() { |
632 | 630 |
Arc arc; |
633 | 631 |
std::vector<Arc> arcs; |
634 | 632 |
for (notifier()->first(arc); arc != INVALID; |
635 | 633 |
notifier()->next(arc)) { |
636 | 634 |
arcs.push_back(arc); |
637 | 635 |
} |
638 | 636 |
for (int i = arcs.size() - 1; i >= 0; --i) { |
639 | 637 |
snapshot.addArc(arcs[i]); |
640 | 638 |
} |
641 | 639 |
} |
642 | 640 |
virtual void clear() { |
643 | 641 |
Arc arc; |
644 | 642 |
for (notifier()->first(arc); arc != INVALID; |
645 | 643 |
notifier()->next(arc)) { |
646 | 644 |
snapshot.eraseArc(arc); |
647 | 645 |
} |
648 | 646 |
} |
649 | 647 |
|
650 | 648 |
Snapshot& snapshot; |
651 | 649 |
}; |
652 | 650 |
|
653 | 651 |
ListDigraph *digraph; |
654 | 652 |
|
655 | 653 |
NodeObserverProxy node_observer_proxy; |
656 | 654 |
ArcObserverProxy arc_observer_proxy; |
657 | 655 |
|
658 | 656 |
std::list<Node> added_nodes; |
659 | 657 |
std::list<Arc> added_arcs; |
660 | 658 |
|
661 | 659 |
|
662 | 660 |
void addNode(const Node& node) { |
663 | 661 |
added_nodes.push_front(node); |
664 | 662 |
} |
665 | 663 |
void eraseNode(const Node& node) { |
666 | 664 |
std::list<Node>::iterator it = |
667 | 665 |
std::find(added_nodes.begin(), added_nodes.end(), node); |
668 | 666 |
if (it == added_nodes.end()) { |
669 | 667 |
clear(); |
670 | 668 |
arc_observer_proxy.detach(); |
671 | 669 |
throw NodeNotifier::ImmediateDetach(); |
672 | 670 |
} else { |
673 | 671 |
added_nodes.erase(it); |
674 | 672 |
} |
675 | 673 |
} |
676 | 674 |
|
677 | 675 |
void addArc(const Arc& arc) { |
678 | 676 |
added_arcs.push_front(arc); |
679 | 677 |
} |
680 | 678 |
void eraseArc(const Arc& arc) { |
681 | 679 |
std::list<Arc>::iterator it = |
682 | 680 |
std::find(added_arcs.begin(), added_arcs.end(), arc); |
683 | 681 |
if (it == added_arcs.end()) { |
684 | 682 |
clear(); |
685 | 683 |
node_observer_proxy.detach(); |
686 | 684 |
throw ArcNotifier::ImmediateDetach(); |
687 | 685 |
} else { |
688 | 686 |
added_arcs.erase(it); |
689 | 687 |
} |
690 | 688 |
} |
691 | 689 |
|
692 | 690 |
void attach(ListDigraph &_digraph) { |
693 | 691 |
digraph = &_digraph; |
694 | 692 |
node_observer_proxy.attach(digraph->notifier(Node())); |
695 | 693 |
arc_observer_proxy.attach(digraph->notifier(Arc())); |
696 | 694 |
} |
697 | 695 |
|
698 | 696 |
void detach() { |
699 | 697 |
node_observer_proxy.detach(); |
700 | 698 |
arc_observer_proxy.detach(); |
701 | 699 |
} |
702 | 700 |
|
703 | 701 |
bool attached() const { |
704 | 702 |
return node_observer_proxy.attached(); |
705 | 703 |
} |
706 | 704 |
|
707 | 705 |
void clear() { |
708 | 706 |
added_nodes.clear(); |
709 | 707 |
added_arcs.clear(); |
710 | 708 |
} |
711 | 709 |
|
712 | 710 |
public: |
713 | 711 |
|
714 | 712 |
/// \brief Default constructor. |
715 | 713 |
/// |
716 | 714 |
/// Default constructor. |
717 | 715 |
/// To actually make a snapshot you must call save(). |
718 | 716 |
Snapshot() |
719 | 717 |
: digraph(0), node_observer_proxy(*this), |
720 | 718 |
arc_observer_proxy(*this) {} |
721 | 719 |
|
722 | 720 |
/// \brief Constructor that immediately makes a snapshot. |
723 | 721 |
/// |
724 | 722 |
/// This constructor immediately makes a snapshot of the digraph. |
725 | 723 |
/// \param _digraph The digraph we make a snapshot of. |
726 | 724 |
Snapshot(ListDigraph &_digraph) |
727 | 725 |
: node_observer_proxy(*this), |
728 | 726 |
arc_observer_proxy(*this) { |
729 | 727 |
attach(_digraph); |
730 | 728 |
} |
731 | 729 |
|
732 | 730 |
/// \brief Make a snapshot. |
733 | 731 |
/// |
734 | 732 |
/// Make a snapshot of the digraph. |
735 | 733 |
/// |
736 | 734 |
/// This function can be called more than once. In case of a repeated |
737 | 735 |
/// call, the previous snapshot gets lost. |
738 | 736 |
/// \param _digraph The digraph we make the snapshot of. |
739 | 737 |
void save(ListDigraph &_digraph) { |
740 | 738 |
if (attached()) { |
741 | 739 |
detach(); |
742 | 740 |
clear(); |
743 | 741 |
} |
744 | 742 |
attach(_digraph); |
745 | 743 |
} |
746 | 744 |
|
747 | 745 |
/// \brief Undo the changes until the last snapshot. |
748 | 746 |
// |
749 | 747 |
/// Undo the changes until the last snapshot created by save(). |
750 | 748 |
void restore() { |
751 | 749 |
detach(); |
752 | 750 |
for(std::list<Arc>::iterator it = added_arcs.begin(); |
753 | 751 |
it != added_arcs.end(); ++it) { |
754 | 752 |
digraph->erase(*it); |
755 | 753 |
} |
756 | 754 |
for(std::list<Node>::iterator it = added_nodes.begin(); |
757 | 755 |
it != added_nodes.end(); ++it) { |
758 | 756 |
digraph->erase(*it); |
759 | 757 |
} |
760 | 758 |
clear(); |
761 | 759 |
} |
762 | 760 |
|
763 | 761 |
/// \brief Gives back true when the snapshot is valid. |
764 | 762 |
/// |
765 | 763 |
/// Gives back true when the snapshot is valid. |
766 | 764 |
bool valid() const { |
767 | 765 |
return attached(); |
768 | 766 |
} |
769 | 767 |
}; |
770 | 768 |
|
771 | 769 |
}; |
772 | 770 |
|
773 | 771 |
///@} |
774 | 772 |
|
775 | 773 |
class ListGraphBase { |
776 | 774 |
|
777 | 775 |
protected: |
778 | 776 |
|
779 | 777 |
struct NodeT { |
780 | 778 |
int first_out; |
781 | 779 |
int prev, next; |
782 | 780 |
}; |
783 | 781 |
|
784 | 782 |
struct ArcT { |
785 | 783 |
int target; |
786 | 784 |
int prev_out, next_out; |
787 | 785 |
}; |
788 | 786 |
|
789 | 787 |
std::vector<NodeT> nodes; |
790 | 788 |
|
791 | 789 |
int first_node; |
792 | 790 |
|
793 | 791 |
int first_free_node; |
794 | 792 |
|
795 | 793 |
std::vector<ArcT> arcs; |
796 | 794 |
|
797 | 795 |
int first_free_arc; |
798 | 796 |
|
799 | 797 |
public: |
800 | 798 |
|
801 | 799 |
typedef ListGraphBase Digraph; |
802 | 800 |
|
803 | 801 |
class Node; |
804 | 802 |
class Arc; |
805 | 803 |
class Edge; |
806 | 804 |
|
807 | 805 |
class Node { |
808 | 806 |
friend class ListGraphBase; |
809 | 807 |
protected: |
810 | 808 |
|
811 | 809 |
int id; |
812 | 810 |
explicit Node(int pid) { id = pid;} |
813 | 811 |
|
814 | 812 |
public: |
815 | 813 |
Node() {} |
816 | 814 |
Node (Invalid) { id = -1; } |
817 | 815 |
bool operator==(const Node& node) const {return id == node.id;} |
818 | 816 |
bool operator!=(const Node& node) const {return id != node.id;} |
819 | 817 |
bool operator<(const Node& node) const {return id < node.id;} |
820 | 818 |
}; |
821 | 819 |
|
822 | 820 |
class Edge { |
823 | 821 |
friend class ListGraphBase; |
824 | 822 |
protected: |
825 | 823 |
|
826 | 824 |
int id; |
827 | 825 |
explicit Edge(int pid) { id = pid;} |
828 | 826 |
|
829 | 827 |
public: |
830 | 828 |
Edge() {} |
831 | 829 |
Edge (Invalid) { id = -1; } |
832 | 830 |
bool operator==(const Edge& edge) const {return id == edge.id;} |
833 | 831 |
bool operator!=(const Edge& edge) const {return id != edge.id;} |
834 | 832 |
bool operator<(const Edge& edge) const {return id < edge.id;} |
835 | 833 |
}; |
836 | 834 |
|
837 | 835 |
class Arc { |
838 | 836 |
friend class ListGraphBase; |
839 | 837 |
protected: |
840 | 838 |
|
841 | 839 |
int id; |
842 | 840 |
explicit Arc(int pid) { id = pid;} |
843 | 841 |
|
844 | 842 |
public: |
845 | 843 |
operator Edge() const { |
846 | 844 |
return id != -1 ? edgeFromId(id / 2) : INVALID; |
847 | 845 |
} |
848 | 846 |
|
849 | 847 |
Arc() {} |
850 | 848 |
Arc (Invalid) { id = -1; } |
851 | 849 |
bool operator==(const Arc& arc) const {return id == arc.id;} |
852 | 850 |
bool operator!=(const Arc& arc) const {return id != arc.id;} |
853 | 851 |
bool operator<(const Arc& arc) const {return id < arc.id;} |
854 | 852 |
}; |
855 | 853 |
|
856 | 854 |
|
857 | 855 |
|
858 | 856 |
ListGraphBase() |
859 | 857 |
: nodes(), first_node(-1), |
860 | 858 |
first_free_node(-1), arcs(), first_free_arc(-1) {} |
861 | 859 |
|
862 | 860 |
|
863 | 861 |
int maxNodeId() const { return nodes.size()-1; } |
864 | 862 |
int maxEdgeId() const { return arcs.size() / 2 - 1; } |
865 | 863 |
int maxArcId() const { return arcs.size()-1; } |
866 | 864 |
|
867 | 865 |
Node source(Arc e) const { return Node(arcs[e.id ^ 1].target); } |
868 | 866 |
Node target(Arc e) const { return Node(arcs[e.id].target); } |
869 | 867 |
|
870 | 868 |
Node u(Edge e) const { return Node(arcs[2 * e.id].target); } |
871 | 869 |
Node v(Edge e) const { return Node(arcs[2 * e.id + 1].target); } |
872 | 870 |
|
873 | 871 |
static bool direction(Arc e) { |
874 | 872 |
return (e.id & 1) == 1; |
875 | 873 |
} |
876 | 874 |
|
877 | 875 |
static Arc direct(Edge e, bool d) { |
878 | 876 |
return Arc(e.id * 2 + (d ? 1 : 0)); |
879 | 877 |
} |
880 | 878 |
|
881 | 879 |
void first(Node& node) const { |
882 | 880 |
node.id = first_node; |
883 | 881 |
} |
884 | 882 |
|
885 | 883 |
void next(Node& node) const { |
886 | 884 |
node.id = nodes[node.id].next; |
887 | 885 |
} |
888 | 886 |
|
889 | 887 |
void first(Arc& e) const { |
890 | 888 |
int n = first_node; |
891 | 889 |
while (n != -1 && nodes[n].first_out == -1) { |
Changeset was too big and was cut off... Show full diff
0 comments (0 inline)