0
3
0
... | ... |
@@ -18,182 +18,187 @@ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BFS_H |
20 | 20 |
#define LEMON_BFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 |
///\brief |
|
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 |
|
|
35 |
|
|
36 | 34 |
///Default traits class of Bfs class. |
37 | 35 |
|
38 | 36 |
///Default traits class of Bfs class. |
39 | 37 |
///\tparam GR Digraph type. |
40 | 38 |
template<class GR> |
41 | 39 |
struct BfsDefaultTraits |
42 | 40 |
{ |
43 |
///The |
|
41 |
///The type of the digraph the algorithm runs on. |
|
44 | 42 |
typedef GR Digraph; |
45 |
|
|
43 |
|
|
44 |
///\brief The type of the map that stores the predecessor |
|
46 | 45 |
///arcs of the shortest paths. |
47 | 46 |
/// |
48 |
///The type of the map that stores the |
|
47 |
///The type of the map that stores the predecessor |
|
49 | 48 |
///arcs of the shortest paths. |
50 | 49 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
51 |
/// |
|
52 |
typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap; |
|
53 |
|
|
50 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
|
51 |
///Instantiates a \ref PredMap. |
|
54 | 52 |
|
55 | 53 |
///This function instantiates a \ref PredMap. |
56 |
///\param |
|
54 |
///\param g is the digraph, to which we would like to define the |
|
55 |
///\ref PredMap. |
|
57 | 56 |
///\todo The digraph alone may be insufficient to initialize |
58 |
static PredMap *createPredMap(const |
|
57 |
static PredMap *createPredMap(const Digraph &g) |
|
59 | 58 |
{ |
60 |
return new PredMap( |
|
59 |
return new PredMap(g); |
|
61 | 60 |
} |
61 |
|
|
62 | 62 |
///The type of the map that indicates which nodes are processed. |
63 | 63 |
|
64 | 64 |
///The type of the map that indicates which nodes are processed. |
65 | 65 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
66 |
/// |
|
66 |
///By default it is a NullMap. |
|
67 | 67 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
68 |
///Instantiates a ProcessedMap. |
|
68 |
///Instantiates a \ref ProcessedMap. |
|
69 | 69 |
|
70 | 70 |
///This function instantiates a \ref ProcessedMap. |
71 | 71 |
///\param g is the digraph, to which |
72 | 72 |
///we would like to define the \ref ProcessedMap |
73 | 73 |
#ifdef DOXYGEN |
74 |
static ProcessedMap *createProcessedMap(const |
|
74 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
|
75 | 75 |
#else |
76 |
static ProcessedMap *createProcessedMap(const |
|
76 |
static ProcessedMap *createProcessedMap(const Digraph &) |
|
77 | 77 |
#endif |
78 | 78 |
{ |
79 | 79 |
return new ProcessedMap(); |
80 | 80 |
} |
81 |
|
|
81 | 82 |
///The type of the map that indicates which nodes are reached. |
82 | 83 |
|
83 | 84 |
///The type of the map that indicates which nodes are reached. |
84 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
|
85 |
///\todo named parameter to set this type, function to read and write. |
|
85 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
86 | 86 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
87 |
///Instantiates a ReachedMap. |
|
87 |
///Instantiates a \ref ReachedMap. |
|
88 | 88 |
|
89 | 89 |
///This function instantiates a \ref ReachedMap. |
90 |
///\param |
|
90 |
///\param g is the digraph, to which |
|
91 | 91 |
///we would like to define the \ref ReachedMap. |
92 |
static ReachedMap *createReachedMap(const |
|
92 |
static ReachedMap *createReachedMap(const Digraph &g) |
|
93 | 93 |
{ |
94 |
return new ReachedMap( |
|
94 |
return new ReachedMap(g); |
|
95 | 95 |
} |
96 |
///The type of the map that stores the dists of the nodes. |
|
97 | 96 |
|
98 |
///The type of the map that stores the |
|
97 |
///The type of the map that stores the distances of the nodes. |
|
98 |
|
|
99 |
///The type of the map that stores the distances of the nodes. |
|
99 | 100 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
100 |
/// |
|
101 | 101 |
typedef typename Digraph::template NodeMap<int> DistMap; |
102 |
///Instantiates a DistMap. |
|
102 |
///Instantiates a \ref DistMap. |
|
103 | 103 |
|
104 | 104 |
///This function instantiates a \ref DistMap. |
105 |
///\param G is the digraph, to which we would like to define |
|
106 |
///the \ref DistMap |
|
107 |
|
|
105 |
///\param g is the digraph, to which we would like to define the |
|
106 |
///\ref DistMap. |
|
107 |
static DistMap *createDistMap(const Digraph &g) |
|
108 | 108 |
{ |
109 |
return new DistMap( |
|
109 |
return new DistMap(g); |
|
110 | 110 |
} |
111 | 111 |
}; |
112 | 112 |
|
113 | 113 |
///%BFS algorithm class. |
114 | 114 |
|
115 | 115 |
///\ingroup search |
116 | 116 |
///This class provides an efficient implementation of the %BFS algorithm. |
117 | 117 |
/// |
118 |
///\tparam GR The digraph type the algorithm runs on. The default value is |
|
119 |
///\ref ListDigraph. The value of GR is not used directly by Bfs, it |
|
120 |
///is |
|
118 |
///There is also a \ref bfs() "function type interface" for the BFS |
|
119 |
///algorithm, which is convenient in the simplier cases and it can be |
|
120 |
///used easier. |
|
121 |
/// |
|
122 |
///\tparam GR The type of the digraph the algorithm runs on. |
|
123 |
///The default value is \ref ListDigraph. The value of GR is not used |
|
124 |
///directly by \ref Bfs, it is only passed to \ref BfsDefaultTraits. |
|
121 | 125 |
///\tparam TR Traits class to set various data types used by the algorithm. |
122 | 126 |
///The default traits class is |
123 | 127 |
///\ref BfsDefaultTraits "BfsDefaultTraits<GR>". |
124 | 128 |
///See \ref BfsDefaultTraits for the documentation of |
125 | 129 |
///a Bfs traits class. |
126 |
|
|
127 | 130 |
#ifdef DOXYGEN |
128 | 131 |
template <typename GR, |
129 | 132 |
typename TR> |
130 | 133 |
#else |
131 | 134 |
template <typename GR=ListDigraph, |
132 | 135 |
typename TR=BfsDefaultTraits<GR> > |
133 | 136 |
#endif |
134 | 137 |
class Bfs { |
135 | 138 |
public: |
136 |
/** |
|
137 |
* \brief \ref Exception for uninitialized parameters. |
|
138 |
* |
|
139 |
* This error represents problems in the initialization |
|
140 |
* of the parameters of the algorithms. |
|
141 |
*/ |
|
139 |
///\ref Exception for uninitialized parameters. |
|
140 |
|
|
141 |
///This error represents problems in the initialization of the |
|
142 |
///parameters of the algorithm. |
|
142 | 143 |
class UninitializedParameter : public lemon::UninitializedParameter { |
143 | 144 |
public: |
144 | 145 |
virtual const char* what() const throw() { |
145 | 146 |
return "lemon::Bfs::UninitializedParameter"; |
146 | 147 |
} |
147 | 148 |
}; |
148 | 149 |
|
149 |
typedef TR Traits; |
|
150 |
///The type of the underlying digraph. |
|
150 |
///The type of the digraph the algorithm runs on. |
|
151 | 151 |
typedef typename TR::Digraph Digraph; |
152 | 152 |
|
153 |
///\brief The type of the map that stores the last |
|
154 |
///arcs of the shortest paths. |
|
153 |
///\brief The type of the map that stores the predecessor arcs of the |
|
154 |
///shortest paths. |
|
155 | 155 |
typedef typename TR::PredMap PredMap; |
156 |
///The type of the map |
|
156 |
///The type of the map that stores the distances of the nodes. |
|
157 |
typedef typename TR::DistMap DistMap; |
|
158 |
///The type of the map that indicates which nodes are reached. |
|
157 | 159 |
typedef typename TR::ReachedMap ReachedMap; |
158 |
///The type of the map |
|
160 |
///The type of the map that indicates which nodes are processed. |
|
159 | 161 |
typedef typename TR::ProcessedMap ProcessedMap; |
160 |
///The type of the map that stores the dists of the nodes. |
|
161 |
typedef typename TR::DistMap DistMap; |
|
162 |
///The type of the paths. |
|
163 |
typedef PredMapPath<Digraph, PredMap> Path; |
|
164 |
|
|
165 |
///The traits class. |
|
166 |
typedef TR Traits; |
|
167 |
|
|
162 | 168 |
private: |
163 | 169 |
|
164 | 170 |
typedef typename Digraph::Node Node; |
165 | 171 |
typedef typename Digraph::NodeIt NodeIt; |
166 | 172 |
typedef typename Digraph::Arc Arc; |
167 | 173 |
typedef typename Digraph::OutArcIt OutArcIt; |
168 | 174 |
|
169 |
// |
|
175 |
//Pointer to the underlying digraph. |
|
170 | 176 |
const Digraph *G; |
171 |
|
|
177 |
//Pointer to the map of predecessor arcs. |
|
172 | 178 |
PredMap *_pred; |
173 |
// |
|
179 |
//Indicates if _pred is locally allocated (true) or not. |
|
174 | 180 |
bool local_pred; |
175 |
|
|
181 |
//Pointer to the map of distances. |
|
176 | 182 |
DistMap *_dist; |
177 |
// |
|
183 |
//Indicates if _dist is locally allocated (true) or not. |
|
178 | 184 |
bool local_dist; |
179 |
|
|
185 |
//Pointer to the map of reached status of the nodes. |
|
180 | 186 |
ReachedMap *_reached; |
181 |
// |
|
187 |
//Indicates if _reached is locally allocated (true) or not. |
|
182 | 188 |
bool local_reached; |
183 |
|
|
189 |
//Pointer to the map of processed status of the nodes. |
|
184 | 190 |
ProcessedMap *_processed; |
185 |
// |
|
191 |
//Indicates if _processed is locally allocated (true) or not. |
|
186 | 192 |
bool local_processed; |
187 | 193 |
|
188 | 194 |
std::vector<typename Digraph::Node> _queue; |
189 | 195 |
int _queue_head,_queue_tail,_queue_next_dist; |
190 | 196 |
int _curr_dist; |
191 | 197 |
|
192 | 198 |
///Creates the maps if necessary. |
193 |
|
|
194 | 199 |
///\todo Better memory allocation (instead of new). |
195 | 200 |
void create_maps() |
196 | 201 |
{ |
197 | 202 |
if(!_pred) { |
198 | 203 |
local_pred = true; |
199 | 204 |
_pred = Traits::createPredMap(*G); |
... | ... |
@@ -230,16 +235,16 @@ |
230 | 235 |
static PredMap *createPredMap(const Digraph &) |
231 | 236 |
{ |
232 | 237 |
throw UninitializedParameter(); |
233 | 238 |
} |
234 | 239 |
}; |
235 | 240 |
///\brief \ref named-templ-param "Named parameter" for setting |
236 |
///PredMap type |
|
241 |
///\ref PredMap type. |
|
237 | 242 |
/// |
238 |
///\ref named-templ-param "Named parameter" for setting PredMap type |
|
239 |
/// |
|
243 |
///\ref named-templ-param "Named parameter" for setting |
|
244 |
///\ref PredMap type. |
|
240 | 245 |
template <class T> |
241 | 246 |
struct DefPredMap : public Bfs< Digraph, DefPredMapTraits<T> > { |
242 | 247 |
typedef Bfs< Digraph, DefPredMapTraits<T> > Create; |
243 | 248 |
}; |
244 | 249 |
|
245 | 250 |
template <class T> |
... | ... |
@@ -248,16 +253,16 @@ |
248 | 253 |
static DistMap *createDistMap(const Digraph &) |
249 | 254 |
{ |
250 | 255 |
throw UninitializedParameter(); |
251 | 256 |
} |
252 | 257 |
}; |
253 | 258 |
///\brief \ref named-templ-param "Named parameter" for setting |
254 |
///DistMap type |
|
259 |
///\ref DistMap type. |
|
255 | 260 |
/// |
256 |
///\ref named-templ-param "Named parameter" for setting DistMap type |
|
257 |
/// |
|
261 |
///\ref named-templ-param "Named parameter" for setting |
|
262 |
///\ref DistMap type. |
|
258 | 263 |
template <class T> |
259 | 264 |
struct DefDistMap : public Bfs< Digraph, DefDistMapTraits<T> > { |
260 | 265 |
typedef Bfs< Digraph, DefDistMapTraits<T> > Create; |
261 | 266 |
}; |
262 | 267 |
|
263 | 268 |
template <class T> |
... | ... |
@@ -266,16 +271,16 @@ |
266 | 271 |
static ReachedMap *createReachedMap(const Digraph &) |
267 | 272 |
{ |
268 | 273 |
throw UninitializedParameter(); |
269 | 274 |
} |
270 | 275 |
}; |
271 | 276 |
///\brief \ref named-templ-param "Named parameter" for setting |
272 |
///ReachedMap type |
|
277 |
///\ref ReachedMap type. |
|
273 | 278 |
/// |
274 |
///\ref named-templ-param "Named parameter" for setting ReachedMap type |
|
275 |
/// |
|
279 |
///\ref named-templ-param "Named parameter" for setting |
|
280 |
///\ref ReachedMap type. |
|
276 | 281 |
template <class T> |
277 | 282 |
struct DefReachedMap : public Bfs< Digraph, DefReachedMapTraits<T> > { |
278 | 283 |
typedef Bfs< Digraph, DefReachedMapTraits<T> > Create; |
279 | 284 |
}; |
280 | 285 |
|
281 | 286 |
template <class T> |
... | ... |
@@ -284,33 +289,33 @@ |
284 | 289 |
static ProcessedMap *createProcessedMap(const Digraph &) |
285 | 290 |
{ |
286 | 291 |
throw UninitializedParameter(); |
287 | 292 |
} |
288 | 293 |
}; |
289 | 294 |
///\brief \ref named-templ-param "Named parameter" for setting |
290 |
///ProcessedMap type |
|
295 |
///\ref ProcessedMap type. |
|
291 | 296 |
/// |
292 |
///\ref named-templ-param "Named parameter" for setting ProcessedMap type |
|
293 |
/// |
|
297 |
///\ref named-templ-param "Named parameter" for setting |
|
298 |
///\ref ProcessedMap type. |
|
294 | 299 |
template <class T> |
295 | 300 |
struct DefProcessedMap : public Bfs< Digraph, DefProcessedMapTraits<T> > { |
296 | 301 |
typedef Bfs< Digraph, DefProcessedMapTraits<T> > Create; |
297 | 302 |
}; |
298 | 303 |
|
299 | 304 |
struct DefDigraphProcessedMapTraits : public Traits { |
300 | 305 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
301 |
static ProcessedMap *createProcessedMap(const Digraph & |
|
306 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
|
302 | 307 |
{ |
303 |
return new ProcessedMap( |
|
308 |
return new ProcessedMap(g); |
|
304 | 309 |
} |
305 | 310 |
}; |
306 |
///\brief \ref named-templ-param "Named parameter" |
|
307 |
///for setting the ProcessedMap type to be Digraph::NodeMap<bool>. |
|
311 |
///\brief \ref named-templ-param "Named parameter" for setting |
|
312 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
308 | 313 |
/// |
309 |
///\ref named-templ-param "Named parameter" |
|
310 |
///for setting the ProcessedMap type to be Digraph::NodeMap<bool>. |
|
314 |
///\ref named-templ-param "Named parameter" for setting |
|
315 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
311 | 316 |
///If you don't set it explicitly, it will be automatically allocated. |
312 | 317 |
template <class T> |
313 | 318 |
struct DefProcessedMapToBeDefaultMap : |
314 | 319 |
public Bfs< Digraph, DefDigraphProcessedMapTraits> { |
315 | 320 |
typedef Bfs< Digraph, DefDigraphProcessedMapTraits> Create; |
316 | 321 |
}; |
... | ... |
@@ -318,16 +323,16 @@ |
318 | 323 |
///@} |
319 | 324 |
|
320 | 325 |
public: |
321 | 326 |
|
322 | 327 |
///Constructor. |
323 | 328 |
|
324 |
///\param _G the digraph the algorithm will run on. |
|
325 |
/// |
|
326 |
Bfs(const Digraph& _G) : |
|
327 |
G(&_G), |
|
329 |
///Constructor. |
|
330 |
///\param g The digraph the algorithm runs on. |
|
331 |
Bfs(const Digraph &g) : |
|
332 |
G(&g), |
|
328 | 333 |
_pred(NULL), local_pred(false), |
329 | 334 |
_dist(NULL), local_dist(false), |
330 | 335 |
_reached(NULL), local_reached(false), |
331 | 336 |
_processed(NULL), local_processed(false) |
332 | 337 |
{ } |
333 | 338 |
|
... | ... |
@@ -337,15 +342,15 @@ |
337 | 342 |
if(local_pred) delete _pred; |
338 | 343 |
if(local_dist) delete _dist; |
339 | 344 |
if(local_reached) delete _reached; |
340 | 345 |
if(local_processed) delete _processed; |
341 | 346 |
} |
342 | 347 |
|
343 |
///Sets the map |
|
348 |
///Sets the map that stores the predecessor arcs. |
|
344 | 349 |
|
345 |
///Sets the map |
|
350 |
///Sets the map that stores the predecessor arcs. |
|
346 | 351 |
///If you don't use this function before calling \ref run(), |
347 | 352 |
///it will allocate one. The destructor deallocates this |
348 | 353 |
///automatically allocated map, of course. |
349 | 354 |
///\return <tt> (*this) </tt> |
350 | 355 |
Bfs &predMap(PredMap &m) |
351 | 356 |
{ |
... | ... |
@@ -354,15 +359,15 @@ |
354 | 359 |
local_pred=false; |
355 | 360 |
} |
356 | 361 |
_pred = &m; |
357 | 362 |
return *this; |
358 | 363 |
} |
359 | 364 |
|
360 |
///Sets the map |
|
365 |
///Sets the map that indicates which nodes are reached. |
|
361 | 366 |
|
362 |
///Sets the map |
|
367 |
///Sets the map that indicates which nodes are reached. |
|
363 | 368 |
///If you don't use this function before calling \ref run(), |
364 | 369 |
///it will allocate one. The destructor deallocates this |
365 | 370 |
///automatically allocated map, of course. |
366 | 371 |
///\return <tt> (*this) </tt> |
367 | 372 |
Bfs &reachedMap(ReachedMap &m) |
368 | 373 |
{ |
... | ... |
@@ -371,15 +376,15 @@ |
371 | 376 |
local_reached=false; |
372 | 377 |
} |
373 | 378 |
_reached = &m; |
374 | 379 |
return *this; |
375 | 380 |
} |
376 | 381 |
|
377 |
///Sets the map |
|
382 |
///Sets the map that indicates which nodes are processed. |
|
378 | 383 |
|
379 |
///Sets the map |
|
384 |
///Sets the map that indicates which nodes are processed. |
|
380 | 385 |
///If you don't use this function before calling \ref run(), |
381 | 386 |
///it will allocate one. The destructor deallocates this |
382 | 387 |
///automatically allocated map, of course. |
383 | 388 |
///\return <tt> (*this) </tt> |
384 | 389 |
Bfs &processedMap(ProcessedMap &m) |
385 | 390 |
{ |
... | ... |
@@ -388,15 +393,16 @@ |
388 | 393 |
local_processed=false; |
389 | 394 |
} |
390 | 395 |
_processed = &m; |
391 | 396 |
return *this; |
392 | 397 |
} |
393 | 398 |
|
394 |
///Sets the map |
|
399 |
///Sets the map that stores the distances of the nodes. |
|
395 | 400 |
|
396 |
///Sets the map |
|
401 |
///Sets the map that stores the distances of the nodes calculated by |
|
402 |
///the algorithm. |
|
397 | 403 |
///If you don't use this function before calling \ref run(), |
398 | 404 |
///it will allocate one. The destructor deallocates this |
399 | 405 |
///automatically allocated map, of course. |
400 | 406 |
///\return <tt> (*this) </tt> |
401 | 407 |
Bfs &distMap(DistMap &m) |
402 | 408 |
{ |
... | ... |
@@ -406,26 +412,27 @@ |
406 | 412 |
} |
407 | 413 |
_dist = &m; |
408 | 414 |
return *this; |
409 | 415 |
} |
410 | 416 |
|
411 | 417 |
public: |
418 |
|
|
412 | 419 |
///\name Execution control |
413 | 420 |
///The simplest way to execute the algorithm is to use |
414 |
///one of the member functions called \ |
|
421 |
///one of the member functions called \ref lemon::Bfs::run() "run()". |
|
415 | 422 |
///\n |
416 |
///If you need more control on the execution, |
|
417 |
///first you must call \ref init(), then you can add several source nodes |
|
418 |
///with \ref addSource(). |
|
419 |
///Finally \ref start() will perform the actual path |
|
420 |
/// |
|
423 |
///If you need more control on the execution, first you must call |
|
424 |
///\ref lemon::Bfs::init() "init()", then you can add several source |
|
425 |
///nodes with \ref lemon::Bfs::addSource() "addSource()". |
|
426 |
///Finally \ref lemon::Bfs::start() "start()" will perform the |
|
427 |
///actual path computation. |
|
421 | 428 |
|
422 | 429 |
///@{ |
423 | 430 |
|
424 |
///\brief Initializes the internal data structures. |
|
425 |
/// |
|
431 |
///Initializes the internal data structures. |
|
432 |
|
|
426 | 433 |
///Initializes the internal data structures. |
427 | 434 |
/// |
428 | 435 |
void init() |
429 | 436 |
{ |
430 | 437 |
create_maps(); |
431 | 438 |
_queue.resize(countNodes(*G)); |
... | ... |
@@ -457,13 +464,13 @@ |
457 | 464 |
///Processes the next node. |
458 | 465 |
|
459 | 466 |
///Processes the next node. |
460 | 467 |
/// |
461 | 468 |
///\return The processed node. |
462 | 469 |
/// |
463 |
///\ |
|
470 |
///\pre The queue must not be empty. |
|
464 | 471 |
Node processNextNode() |
465 | 472 |
{ |
466 | 473 |
if(_queue_tail==_queue_next_dist) { |
467 | 474 |
_curr_dist++; |
468 | 475 |
_queue_next_dist=_queue_head; |
469 | 476 |
} |
... | ... |
@@ -479,22 +486,23 @@ |
479 | 486 |
} |
480 | 487 |
return n; |
481 | 488 |
} |
482 | 489 |
|
483 | 490 |
///Processes the next node. |
484 | 491 |
|
485 |
///Processes the next node |
|
492 |
///Processes the next node and checks if the given target node |
|
486 | 493 |
///is reached. If the target node is reachable from the processed |
487 |
///node then the reached parameter will be set true. The reached |
|
488 |
///parameter should be initially false. |
|
494 |
///node, then the \c reach parameter will be set to \c true. |
|
489 | 495 |
/// |
490 | 496 |
///\param target The target node. |
491 |
///\retval reach Indicates |
|
497 |
///\retval reach Indicates if the target node is reached. |
|
498 |
///It should be initially \c false. |
|
499 |
/// |
|
492 | 500 |
///\return The processed node. |
493 | 501 |
/// |
494 |
///\ |
|
502 |
///\pre The queue must not be empty. |
|
495 | 503 |
Node processNextNode(Node target, bool& reach) |
496 | 504 |
{ |
497 | 505 |
if(_queue_tail==_queue_next_dist) { |
498 | 506 |
_curr_dist++; |
499 | 507 |
_queue_next_dist=_queue_head; |
500 | 508 |
} |
... | ... |
@@ -511,22 +519,25 @@ |
511 | 519 |
} |
512 | 520 |
return n; |
513 | 521 |
} |
514 | 522 |
|
515 | 523 |
///Processes the next node. |
516 | 524 |
|
517 |
///Processes the next node. And checks that at least one of |
|
518 |
///reached node has true value in the \c nm node map. If one node |
|
519 |
///with true value is reachable from the processed node then the |
|
520 |
///rnode parameter will be set to the first of such nodes. |
|
525 |
///Processes the next node and checks if at least one of reached |
|
526 |
///nodes has \c true value in the \c nm node map. If one node |
|
527 |
///with \c true value is reachable from the processed node, then the |
|
528 |
///\c rnode parameter will be set to the first of such nodes. |
|
521 | 529 |
/// |
522 |
///\param nm |
|
530 |
///\param nm A \c bool (or convertible) node map that indicates the |
|
531 |
///possible targets. |
|
523 | 532 |
///\retval rnode The reached target node. |
533 |
///It should be initially \c INVALID. |
|
534 |
/// |
|
524 | 535 |
///\return The processed node. |
525 | 536 |
/// |
526 |
///\ |
|
537 |
///\pre The queue must not be empty. |
|
527 | 538 |
template<class NM> |
528 | 539 |
Node processNextNode(const NM& nm, Node& rnode) |
529 | 540 |
{ |
530 | 541 |
if(_queue_tail==_queue_next_dist) { |
531 | 542 |
_curr_dist++; |
532 | 543 |
_queue_next_dist=_queue_head; |
... | ... |
@@ -542,103 +553,130 @@ |
542 | 553 |
_dist->set(m,_curr_dist); |
543 | 554 |
if (nm[m] && rnode == INVALID) rnode = m; |
544 | 555 |
} |
545 | 556 |
return n; |
546 | 557 |
} |
547 | 558 |
|
548 |
/// |
|
559 |
///The next node to be processed. |
|
549 | 560 |
|
550 |
///Next node to be processed. |
|
551 |
/// |
|
552 |
///\return The next node to be processed or INVALID if the queue is |
|
553 |
/// empty. |
|
554 |
|
|
561 |
///Returns the next node to be processed or \c INVALID if the queue |
|
562 |
///is empty. |
|
563 |
Node nextNode() const |
|
555 | 564 |
{ |
556 | 565 |
return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID; |
557 | 566 |
} |
558 | 567 |
|
559 | 568 |
///\brief Returns \c false if there are nodes |
560 |
///to be processed |
|
569 |
///to be processed. |
|
561 | 570 |
/// |
562 | 571 |
///Returns \c false if there are nodes |
563 |
///to be processed in the queue |
|
564 |
bool emptyQueue() { return _queue_tail==_queue_head; } |
|
572 |
///to be processed in the queue. |
|
573 |
bool emptyQueue() const { return _queue_tail==_queue_head; } |
|
574 |
|
|
565 | 575 |
///Returns the number of the nodes to be processed. |
566 | 576 |
|
567 | 577 |
///Returns the number of the nodes to be processed in the queue. |
568 |
int queueSize() { return _queue_head-_queue_tail; } |
|
578 |
int queueSize() const { return _queue_head-_queue_tail; } |
|
569 | 579 |
|
570 | 580 |
///Executes the algorithm. |
571 | 581 |
|
572 | 582 |
///Executes the algorithm. |
573 | 583 |
/// |
574 |
///\pre init() must be called and at least one node should be added |
|
575 |
///with addSource() before using this function. |
|
584 |
///This method runs the %BFS algorithm from the root node(s) |
|
585 |
///in order to compute the shortest path to each node. |
|
576 | 586 |
/// |
577 |
///This method runs the %BFS algorithm from the root node(s) |
|
578 |
///in order to |
|
579 |
///compute the |
|
580 |
///shortest path to each node. The algorithm computes |
|
581 |
///- The shortest path tree. |
|
582 |
///- The distance of each node from the root(s). |
|
587 |
///The algorithm computes |
|
588 |
///- the shortest path tree (forest), |
|
589 |
///- the distance of each node from the root(s). |
|
590 |
/// |
|
591 |
///\pre init() must be called and at least one root node should be |
|
592 |
///added with addSource() before using this function. |
|
593 |
/// |
|
594 |
///\note <tt>b.start()</tt> is just a shortcut of the following code. |
|
595 |
///\code |
|
596 |
/// while ( !b.emptyQueue() ) { |
|
597 |
/// b.processNextNode(); |
|
598 |
/// } |
|
599 |
///\endcode |
|
583 | 600 |
void start() |
584 | 601 |
{ |
585 | 602 |
while ( !emptyQueue() ) processNextNode(); |
586 | 603 |
} |
587 | 604 |
|
588 |
///Executes the algorithm until |
|
605 |
///Executes the algorithm until the given target node is reached. |
|
589 | 606 |
|
590 |
///Executes the algorithm until \c dest is reached. |
|
591 |
/// |
|
592 |
///\pre init() must be called and at least one node should be added |
|
593 |
///with addSource() before using this function. |
|
607 |
///Executes the algorithm until the given target node is reached. |
|
594 | 608 |
/// |
595 | 609 |
///This method runs the %BFS algorithm from the root node(s) |
596 | 610 |
///in order to compute the shortest path to \c dest. |
611 |
/// |
|
597 | 612 |
///The algorithm computes |
598 |
///- The shortest path to \c dest. |
|
599 |
///- The distance of \c dest from the root(s). |
|
613 |
///- the shortest path to \c dest, |
|
614 |
///- the distance of \c dest from the root(s). |
|
615 |
/// |
|
616 |
///\pre init() must be called and at least one root node should be |
|
617 |
///added with addSource() before using this function. |
|
618 |
/// |
|
619 |
///\note <tt>b.start(t)</tt> is just a shortcut of the following code. |
|
620 |
///\code |
|
621 |
/// bool reach = false; |
|
622 |
/// while ( !b.emptyQueue() && !reach ) { |
|
623 |
/// b.processNextNode(t, reach); |
|
624 |
/// } |
|
625 |
///\endcode |
|
600 | 626 |
void start(Node dest) |
601 | 627 |
{ |
602 | 628 |
bool reach = false; |
603 | 629 |
while ( !emptyQueue() && !reach ) processNextNode(dest, reach); |
604 | 630 |
} |
605 | 631 |
|
606 | 632 |
///Executes the algorithm until a condition is met. |
607 | 633 |
|
608 | 634 |
///Executes the algorithm until a condition is met. |
609 | 635 |
/// |
610 |
///\pre init() must be called and at least one node should be added |
|
611 |
///with addSource() before using this function. |
|
636 |
///This method runs the %BFS algorithm from the root node(s) in |
|
637 |
///order to compute the shortest path to a node \c v with |
|
638 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
|
612 | 639 |
/// |
613 |
///\param nm must be a bool (or convertible) node map. The |
|
614 |
///algorithm will stop when it reaches a node \c v with |
|
615 |
/// |
|
640 |
///\param nm A \c bool (or convertible) node map. The algorithm |
|
641 |
///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. |
|
616 | 642 |
/// |
617 | 643 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
618 | 644 |
///\c INVALID if no such node was found. |
619 |
template<class NM> |
|
620 |
Node start(const NM &nm) |
|
645 |
/// |
|
646 |
///\pre init() must be called and at least one root node should be |
|
647 |
///added with addSource() before using this function. |
|
648 |
/// |
|
649 |
///\note <tt>b.start(nm)</tt> is just a shortcut of the following code. |
|
650 |
///\code |
|
651 |
/// Node rnode = INVALID; |
|
652 |
/// while ( !b.emptyQueue() && rnode == INVALID ) { |
|
653 |
/// b.processNextNode(nm, rnode); |
|
654 |
/// } |
|
655 |
/// return rnode; |
|
656 |
///\endcode |
|
657 |
template<class NodeBoolMap> |
|
658 |
Node start(const NodeBoolMap &nm) |
|
621 | 659 |
{ |
622 | 660 |
Node rnode = INVALID; |
623 | 661 |
while ( !emptyQueue() && rnode == INVALID ) { |
624 | 662 |
processNextNode(nm, rnode); |
625 | 663 |
} |
626 | 664 |
return rnode; |
627 | 665 |
} |
628 | 666 |
|
629 |
///Runs |
|
667 |
///Runs the algorithm from the given node. |
|
630 | 668 |
|
631 |
///This method runs the %BFS algorithm from a root node \c s |
|
632 |
///in order to |
|
633 |
///compute the |
|
634 |
///shortest path to each node. The algorithm computes |
|
635 |
///- The shortest path tree. |
|
636 |
///- The distance of each node from the root. |
|
669 |
///This method runs the %BFS algorithm from node \c s |
|
670 |
///in order to compute the shortest path to each node. |
|
637 | 671 |
/// |
638 |
/// |
|
672 |
///The algorithm computes |
|
673 |
///- the shortest path tree, |
|
674 |
///- the distance of each node from the root. |
|
675 |
/// |
|
676 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
|
639 | 677 |
///\code |
640 | 678 |
/// b.init(); |
641 | 679 |
/// b.addSource(s); |
642 | 680 |
/// b.start(); |
643 | 681 |
///\endcode |
644 | 682 |
void run(Node s) { |
... | ... |
@@ -646,201 +684,239 @@ |
646 | 684 |
addSource(s); |
647 | 685 |
start(); |
648 | 686 |
} |
649 | 687 |
|
650 | 688 |
///Finds the shortest path between \c s and \c t. |
651 | 689 |
|
652 |
/// |
|
690 |
///This method runs the %BFS algorithm from node \c s |
|
691 |
///in order to compute the shortest path to \c t. |
|
653 | 692 |
/// |
654 |
///\return The length of the shortest s---t path if there exists one, |
|
655 |
///0 otherwise. |
|
656 |
///\note Apart from the return value, b.run(s) is |
|
657 |
///just a shortcut of the following code. |
|
693 |
///\return The length of the shortest <tt>s</tt>--<tt>t</tt> path, |
|
694 |
///if \c t is reachable form \c s, \c 0 otherwise. |
|
695 |
/// |
|
696 |
///\note Apart from the return value, <tt>b.run(s,t)</tt> is just a |
|
697 |
///shortcut of the following code. |
|
658 | 698 |
///\code |
659 | 699 |
/// b.init(); |
660 | 700 |
/// b.addSource(s); |
661 | 701 |
/// b.start(t); |
662 | 702 |
///\endcode |
663 | 703 |
int run(Node s,Node t) { |
664 | 704 |
init(); |
665 | 705 |
addSource(s); |
666 | 706 |
start(t); |
667 | 707 |
return reached(t) ? _curr_dist : 0; |
668 | 708 |
} |
669 | 709 |
|
710 |
///Runs the algorithm to visit all nodes in the digraph. |
|
711 |
|
|
712 |
///This method runs the %BFS algorithm in order to |
|
713 |
///compute the shortest path to each node. |
|
714 |
/// |
|
715 |
///The algorithm computes |
|
716 |
///- the shortest path tree (forest), |
|
717 |
///- the distance of each node from the root(s). |
|
718 |
/// |
|
719 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
|
720 |
///\code |
|
721 |
/// b.init(); |
|
722 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
|
723 |
/// if (!b.reached(n)) { |
|
724 |
/// b.addSource(n); |
|
725 |
/// b.start(); |
|
726 |
/// } |
|
727 |
/// } |
|
728 |
///\endcode |
|
729 |
void run() { |
|
730 |
init(); |
|
731 |
for (NodeIt n(*G); n != INVALID; ++n) { |
|
732 |
if (!reached(n)) { |
|
733 |
addSource(n); |
|
734 |
start(); |
|
735 |
} |
|
736 |
} |
|
737 |
} |
|
738 |
|
|
670 | 739 |
///@} |
671 | 740 |
|
672 | 741 |
///\name Query Functions |
673 | 742 |
///The result of the %BFS algorithm can be obtained using these |
674 | 743 |
///functions.\n |
675 |
///Before the use of these functions, |
|
676 |
///either run() or start() must be calleb. |
|
744 |
///Either \ref lemon::Bfs::run() "run()" or \ref lemon::Bfs::start() |
|
745 |
///"start()" must be called before using them. |
|
677 | 746 |
|
678 | 747 |
///@{ |
679 | 748 |
|
680 |
|
|
749 |
///The shortest path to a node. |
|
681 | 750 |
|
682 |
///Gives back the shortest path. |
|
683 |
|
|
684 |
///Gives back the shortest path. |
|
685 |
///\pre The \c t should be reachable from the source. |
|
686 |
Path path(Node t) |
|
687 |
{ |
|
688 |
return Path(*G, *_pred, t); |
|
689 |
} |
|
751 |
///Returns the shortest path to a node. |
|
752 |
/// |
|
753 |
///\warning \c t should be reachable from the root(s). |
|
754 |
/// |
|
755 |
///\pre Either \ref run() or \ref start() must be called before |
|
756 |
///using this function. |
|
757 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
|
690 | 758 |
|
691 | 759 |
///The distance of a node from the root(s). |
692 | 760 |
|
693 | 761 |
///Returns the distance of a node from the root(s). |
694 |
///\pre \ref run() must be called before using this function. |
|
695 |
///\warning If node \c v in unreachable from the root(s) the return value |
|
696 |
/// |
|
762 |
/// |
|
763 |
///\warning If node \c v is not reachable from the root(s), then |
|
764 |
///the return value of this function is undefined. |
|
765 |
/// |
|
766 |
///\pre Either \ref run() or \ref start() must be called before |
|
767 |
///using this function. |
|
697 | 768 |
int dist(Node v) const { return (*_dist)[v]; } |
698 | 769 |
|
699 |
///Returns the 'previous arc' of the shortest path tree. |
|
770 |
///Returns the 'previous arc' of the shortest path tree for a node. |
|
700 | 771 |
|
701 |
///For a node \c v it returns the 'previous arc' |
|
702 |
///of the shortest path tree, |
|
703 |
///i.e. it returns the last arc of a shortest path from the root(s) to \c |
|
704 |
///v. It is \ref INVALID |
|
705 |
///if \c v is unreachable from the root(s) or \c v is a root. The |
|
706 |
///shortest path tree used here is equal to the shortest path tree used in |
|
707 |
///\ref predNode(). |
|
708 |
///\pre Either \ref run() or \ref start() must be called before using |
|
709 |
/// |
|
772 |
///This function returns the 'previous arc' of the shortest path |
|
773 |
///tree for the node \c v, i.e. it returns the last arc of a |
|
774 |
///shortest path from the root(s) to \c v. It is \c INVALID if \c v |
|
775 |
///is not reachable from the root(s) or if \c v is a root. |
|
776 |
/// |
|
777 |
///The shortest path tree used here is equal to the shortest path |
|
778 |
///tree used in \ref predNode(). |
|
779 |
/// |
|
780 |
///\pre Either \ref run() or \ref start() must be called before |
|
781 |
///using this function. |
|
710 | 782 |
Arc predArc(Node v) const { return (*_pred)[v];} |
711 | 783 |
|
712 |
///Returns the 'previous node' of the shortest path tree. |
|
784 |
///Returns the 'previous node' of the shortest path tree for a node. |
|
713 | 785 |
|
714 |
///For a node \c v it returns the 'previous node' |
|
715 |
///of the shortest path tree, |
|
716 |
///i.e. it returns the last but one node from a shortest path from the |
|
717 |
///root(a) to \c /v. |
|
718 |
///It is INVALID if \c v is unreachable from the root(s) or |
|
719 |
///if \c v itself a root. |
|
786 |
///This function returns the 'previous node' of the shortest path |
|
787 |
///tree for the node \c v, i.e. it returns the last but one node |
|
788 |
///from a shortest path from the root(s) to \c v. It is \c INVALID |
|
789 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
|
790 |
/// |
|
720 | 791 |
///The shortest path tree used here is equal to the shortest path |
721 | 792 |
///tree used in \ref predArc(). |
793 |
/// |
|
722 | 794 |
///\pre Either \ref run() or \ref start() must be called before |
723 | 795 |
///using this function. |
724 | 796 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
725 | 797 |
G->source((*_pred)[v]); } |
726 | 798 |
|
727 |
///Returns a reference to the NodeMap of distances. |
|
728 |
|
|
729 |
///Returns a reference to the NodeMap of distances. |
|
730 |
///\pre Either \ref run() or \ref init() must |
|
731 |
/// |
|
799 |
///\brief Returns a const reference to the node map that stores the |
|
800 |
/// distances of the nodes. |
|
801 |
/// |
|
802 |
///Returns a const reference to the node map that stores the distances |
|
803 |
///of the nodes calculated by the algorithm. |
|
804 |
/// |
|
805 |
///\pre Either \ref run() or \ref init() |
|
806 |
///must be called before using this function. |
|
732 | 807 |
const DistMap &distMap() const { return *_dist;} |
733 | 808 |
|
734 |
///Returns a reference to the shortest path tree map. |
|
735 |
|
|
736 |
///Returns a reference to the NodeMap of the arcs of the |
|
737 |
///shortest path tree. |
|
809 |
///\brief Returns a const reference to the node map that stores the |
|
810 |
///predecessor arcs. |
|
811 |
/// |
|
812 |
///Returns a const reference to the node map that stores the predecessor |
|
813 |
///arcs, which form the shortest path tree. |
|
814 |
/// |
|
738 | 815 |
///\pre Either \ref run() or \ref init() |
739 | 816 |
///must be called before using this function. |
740 | 817 |
const PredMap &predMap() const { return *_pred;} |
741 | 818 |
|
742 |
///Checks if a node is reachable from the root. |
|
819 |
///Checks if a node is reachable from the root(s). |
|
743 | 820 |
|
744 |
///Returns \c true if \c v is reachable from the root. |
|
745 |
///\warning The source nodes are indicated as unreached. |
|
821 |
///Returns \c true if \c v is reachable from the root(s). |
|
746 | 822 |
///\pre Either \ref run() or \ref start() |
747 | 823 |
///must be called before using this function. |
748 |
/// |
|
749 |
bool reached(Node v) { return (*_reached)[v]; } |
|
824 |
bool reached(Node v) const { return (*_reached)[v]; } |
|
750 | 825 |
|
751 | 826 |
///@} |
752 | 827 |
}; |
753 | 828 |
|
754 |
///Default traits class of |
|
829 |
///Default traits class of bfs() function. |
|
755 | 830 |
|
756 |
///Default traits class of |
|
831 |
///Default traits class of bfs() function. |
|
757 | 832 |
///\tparam GR Digraph type. |
758 | 833 |
template<class GR> |
759 | 834 |
struct BfsWizardDefaultTraits |
760 | 835 |
{ |
761 |
///The |
|
836 |
///The type of the digraph the algorithm runs on. |
|
762 | 837 |
typedef GR Digraph; |
763 |
|
|
838 |
|
|
839 |
///\brief The type of the map that stores the predecessor |
|
764 | 840 |
///arcs of the shortest paths. |
765 | 841 |
/// |
766 |
///The type of the map that stores the |
|
842 |
///The type of the map that stores the predecessor |
|
767 | 843 |
///arcs of the shortest paths. |
768 | 844 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
769 |
/// |
|
770 |
typedef NullMap<typename Digraph::Node,typename GR::Arc> PredMap; |
|
771 |
|
|
845 |
typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap; |
|
846 |
///Instantiates a \ref PredMap. |
|
772 | 847 |
|
773 | 848 |
///This function instantiates a \ref PredMap. |
774 |
///\param g is the digraph, to which we would like to define the |
|
849 |
///\param g is the digraph, to which we would like to define the |
|
850 |
///\ref PredMap. |
|
775 | 851 |
///\todo The digraph alone may be insufficient to initialize |
776 | 852 |
#ifdef DOXYGEN |
777 |
static PredMap *createPredMap(const |
|
853 |
static PredMap *createPredMap(const Digraph &g) |
|
778 | 854 |
#else |
779 |
static PredMap *createPredMap(const |
|
855 |
static PredMap *createPredMap(const Digraph &) |
|
780 | 856 |
#endif |
781 | 857 |
{ |
782 | 858 |
return new PredMap(); |
783 | 859 |
} |
784 | 860 |
|
785 | 861 |
///The type of the map that indicates which nodes are processed. |
786 | 862 |
|
787 | 863 |
///The type of the map that indicates which nodes are processed. |
788 | 864 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
789 |
///\todo named parameter to set this type, function to read and write. |
|
790 | 865 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
791 |
///Instantiates a ProcessedMap. |
|
866 |
///Instantiates a \ref ProcessedMap. |
|
792 | 867 |
|
793 | 868 |
///This function instantiates a \ref ProcessedMap. |
794 | 869 |
///\param g is the digraph, to which |
795 |
///we would like to define the \ref ProcessedMap |
|
870 |
///we would like to define the \ref ProcessedMap. |
|
796 | 871 |
#ifdef DOXYGEN |
797 |
static ProcessedMap *createProcessedMap(const |
|
872 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
|
798 | 873 |
#else |
799 |
static ProcessedMap *createProcessedMap(const |
|
874 |
static ProcessedMap *createProcessedMap(const Digraph &) |
|
800 | 875 |
#endif |
801 | 876 |
{ |
802 | 877 |
return new ProcessedMap(); |
803 | 878 |
} |
879 |
|
|
804 | 880 |
///The type of the map that indicates which nodes are reached. |
805 | 881 |
|
806 | 882 |
///The type of the map that indicates which nodes are reached. |
807 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
|
808 |
///\todo named parameter to set this type, function to read and write. |
|
883 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
809 | 884 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
810 |
///Instantiates a ReachedMap. |
|
885 |
///Instantiates a \ref ReachedMap. |
|
811 | 886 |
|
812 | 887 |
///This function instantiates a \ref ReachedMap. |
813 |
///\param |
|
888 |
///\param g is the digraph, to which |
|
814 | 889 |
///we would like to define the \ref ReachedMap. |
815 |
static ReachedMap *createReachedMap(const |
|
890 |
static ReachedMap *createReachedMap(const Digraph &g) |
|
816 | 891 |
{ |
817 |
return new ReachedMap( |
|
892 |
return new ReachedMap(g); |
|
818 | 893 |
} |
819 |
///The type of the map that stores the dists of the nodes. |
|
820 | 894 |
|
821 |
///The type of the map that stores the |
|
895 |
///The type of the map that stores the distances of the nodes. |
|
896 |
|
|
897 |
///The type of the map that stores the distances of the nodes. |
|
822 | 898 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
823 | 899 |
/// |
824 | 900 |
typedef NullMap<typename Digraph::Node,int> DistMap; |
825 |
///Instantiates a DistMap. |
|
901 |
///Instantiates a \ref DistMap. |
|
826 | 902 |
|
827 | 903 |
///This function instantiates a \ref DistMap. |
828 | 904 |
///\param g is the digraph, to which we would like to define |
829 | 905 |
///the \ref DistMap |
830 | 906 |
#ifdef DOXYGEN |
831 |
static DistMap *createDistMap(const |
|
907 |
static DistMap *createDistMap(const Digraph &g) |
|
832 | 908 |
#else |
833 |
static DistMap *createDistMap(const |
|
909 |
static DistMap *createDistMap(const Digraph &) |
|
834 | 910 |
#endif |
835 | 911 |
{ |
836 | 912 |
return new DistMap(); |
837 | 913 |
} |
838 | 914 |
}; |
839 | 915 |
|
840 |
/// Default traits used by \ref BfsWizard |
|
916 |
/// Default traits class used by \ref BfsWizard |
|
841 | 917 |
|
842 | 918 |
/// To make it easier to use Bfs algorithm |
843 | 919 |
///we have created a wizard class. |
844 | 920 |
/// This \ref BfsWizard class needs default traits, |
845 | 921 |
///as well as the \ref Bfs class. |
846 | 922 |
/// The \ref BfsWizardBase is a class to be the default traits of the |
... | ... |
@@ -848,26 +924,26 @@ |
848 | 924 |
template<class GR> |
849 | 925 |
class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
850 | 926 |
{ |
851 | 927 |
|
852 | 928 |
typedef BfsWizardDefaultTraits<GR> Base; |
853 | 929 |
protected: |
854 |
// |
|
930 |
//The type of the nodes in the digraph. |
|
855 | 931 |
typedef typename Base::Digraph::Node Node; |
856 | 932 |
|
857 |
// |
|
933 |
//Pointer to the digraph the algorithm runs on. |
|
858 | 934 |
void *_g; |
859 |
|
|
935 |
//Pointer to the map of reached nodes. |
|
860 | 936 |
void *_reached; |
861 |
|
|
937 |
//Pointer to the map of processed nodes. |
|
862 | 938 |
void *_processed; |
863 |
|
|
939 |
//Pointer to the map of predecessors arcs. |
|
864 | 940 |
void *_pred; |
865 |
|
|
941 |
//Pointer to the map of distances. |
|
866 | 942 |
void *_dist; |
867 |
|
|
943 |
//Pointer to the source node. |
|
868 | 944 |
Node _source; |
869 | 945 |
|
870 | 946 |
public: |
871 | 947 |
/// Constructor. |
872 | 948 |
|
873 | 949 |
/// This constructor does not require parameters, therefore it initiates |
... | ... |
@@ -877,68 +953,66 @@ |
877 | 953 |
|
878 | 954 |
/// Constructor. |
879 | 955 |
|
880 | 956 |
/// This constructor requires some parameters, |
881 | 957 |
/// listed in the parameters list. |
882 | 958 |
/// Others are initiated to 0. |
883 |
/// \param g is the initial value of \ref _g |
|
884 |
/// \param s is the initial value of \ref _source |
|
959 |
/// \param g The digraph the algorithm runs on. |
|
960 |
/// \param s The source node. |
|
885 | 961 |
BfsWizardBase(const GR &g, Node s=INVALID) : |
886 | 962 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
887 | 963 |
_reached(0), _processed(0), _pred(0), _dist(0), _source(s) {} |
888 | 964 |
|
889 | 965 |
}; |
890 | 966 |
|
891 |
/// |
|
967 |
/// Auxiliary class for the function type interface of BFS algorithm. |
|
892 | 968 |
|
893 |
/// This class is created to make it easier to use Bfs algorithm. |
|
894 |
/// It uses the functions and features of the plain \ref Bfs, |
|
895 |
/// |
|
969 |
/// This auxiliary class is created to implement the function type |
|
970 |
/// interface of \ref Bfs algorithm. It uses the functions and features |
|
971 |
/// of the plain \ref Bfs, but it is much simpler to use it. |
|
972 |
/// It should only be used through the \ref bfs() function, which makes |
|
973 |
/// it easier to use the algorithm. |
|
896 | 974 |
/// |
897 | 975 |
/// Simplicity means that the way to change the types defined |
898 | 976 |
/// in the traits class is based on functions that returns the new class |
899 | 977 |
/// and not on templatable built-in classes. |
900 | 978 |
/// When using the plain \ref Bfs |
901 | 979 |
/// the new class with the modified type comes from |
902 | 980 |
/// the original class by using the :: |
903 | 981 |
/// operator. In the case of \ref BfsWizard only |
904 |
/// a function have to be called and it will |
|
982 |
/// a function have to be called, and it will |
|
905 | 983 |
/// return the needed class. |
906 | 984 |
/// |
907 |
/// It does not have own \ref run method. When its \ref run method is called |
|
908 |
/// it initiates a plain \ref Bfs class, and calls the \ref Bfs::run |
|
909 |
/// |
|
985 |
/// It does not have own \ref run() method. When its \ref run() method |
|
986 |
/// is called, it initiates a plain \ref Bfs object, and calls the |
|
987 |
/// \ref Bfs::run() method of it. |
|
910 | 988 |
template<class TR> |
911 | 989 |
class BfsWizard : public TR |
912 | 990 |
{ |
913 | 991 |
typedef TR Base; |
914 | 992 |
|
915 |
///The type of the |
|
993 |
///The type of the digraph the algorithm runs on. |
|
916 | 994 |
typedef typename TR::Digraph Digraph; |
917 |
|
|
995 |
|
|
918 | 996 |
typedef typename Digraph::Node Node; |
919 |
//\e |
|
920 | 997 |
typedef typename Digraph::NodeIt NodeIt; |
921 |
//\e |
|
922 | 998 |
typedef typename Digraph::Arc Arc; |
923 |
//\e |
|
924 | 999 |
typedef typename Digraph::OutArcIt OutArcIt; |
925 | 1000 |
|
926 |
///\brief The type of the map that stores |
|
927 |
///the reached nodes |
|
928 |
typedef typename TR::ReachedMap ReachedMap; |
|
929 |
///\brief The type of the map that stores |
|
930 |
///the processed nodes |
|
931 |
typedef typename TR::ProcessedMap ProcessedMap; |
|
932 |
///\brief The type of the map that stores the |
|
1001 |
///\brief The type of the map that stores the predecessor |
|
933 | 1002 |
///arcs of the shortest paths. |
934 | 1003 |
typedef typename TR::PredMap PredMap; |
935 |
///The type of the map that stores the |
|
1004 |
///\brief The type of the map that stores the distances of the nodes. |
|
936 | 1005 |
typedef typename TR::DistMap DistMap; |
1006 |
///\brief The type of the map that indicates which nodes are reached. |
|
1007 |
typedef typename TR::ReachedMap ReachedMap; |
|
1008 |
///\brief The type of the map that indicates which nodes are processed. |
|
1009 |
typedef typename TR::ProcessedMap ProcessedMap; |
|
937 | 1010 |
|
938 | 1011 |
public: |
1012 |
|
|
939 | 1013 |
/// Constructor. |
940 | 1014 |
BfsWizard() : TR() {} |
941 | 1015 |
|
942 | 1016 |
/// Constructor that requires parameters. |
943 | 1017 |
|
944 | 1018 |
/// Constructor that requires parameters. |
... | ... |
@@ -948,16 +1022,16 @@ |
948 | 1022 |
|
949 | 1023 |
///Copy constructor |
950 | 1024 |
BfsWizard(const TR &b) : TR(b) {} |
951 | 1025 |
|
952 | 1026 |
~BfsWizard() {} |
953 | 1027 |
|
954 |
///Runs |
|
1028 |
///Runs BFS algorithm from a source node. |
|
955 | 1029 |
|
956 |
///Runs Bfs algorithm from a given node. |
|
957 |
///The node can be given by the \ref source function. |
|
1030 |
///Runs BFS algorithm from a source node. |
|
1031 |
///The node can be given with the \ref source() function. |
|
958 | 1032 |
void run() |
959 | 1033 |
{ |
960 | 1034 |
if(Base::_source==INVALID) throw UninitializedParameter(); |
961 | 1035 |
Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
962 | 1036 |
if(Base::_reached) |
963 | 1037 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
... | ... |
@@ -967,115 +1041,104 @@ |
967 | 1041 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
968 | 1042 |
if(Base::_dist) |
969 | 1043 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
970 | 1044 |
alg.run(Base::_source); |
971 | 1045 |
} |
972 | 1046 |
|
973 |
///Runs |
|
1047 |
///Runs BFS algorithm from the given node. |
|
974 | 1048 |
|
975 |
///Runs |
|
1049 |
///Runs BFS algorithm from the given node. |
|
976 | 1050 |
///\param s is the given source. |
977 | 1051 |
void run(Node s) |
978 | 1052 |
{ |
979 | 1053 |
Base::_source=s; |
980 | 1054 |
run(); |
981 | 1055 |
} |
982 | 1056 |
|
983 |
template<class T> |
|
984 |
struct DefPredMapBase : public Base { |
|
985 |
typedef T PredMap; |
|
986 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
|
987 |
DefPredMapBase(const TR &b) : TR(b) {} |
|
988 |
}; |
|
989 |
|
|
990 |
///\brief \ref named-templ-param "Named parameter" |
|
991 |
///function for setting PredMap |
|
992 |
/// |
|
993 |
/// \ref named-templ-param "Named parameter" |
|
994 |
///function for setting PredMap |
|
995 |
/// |
|
996 |
template<class T> |
|
997 |
BfsWizard<DefPredMapBase<T> > predMap(const T &t) |
|
998 |
{ |
|
999 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1000 |
return BfsWizard<DefPredMapBase<T> >(*this); |
|
1001 |
} |
|
1002 |
|
|
1003 |
|
|
1004 |
template<class T> |
|
1005 |
struct DefReachedMapBase : public Base { |
|
1006 |
typedef T ReachedMap; |
|
1007 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
|
1008 |
DefReachedMapBase(const TR &b) : TR(b) {} |
|
1009 |
}; |
|
1010 |
|
|
1011 |
///\brief \ref named-templ-param "Named parameter" |
|
1012 |
///function for setting ReachedMap |
|
1013 |
/// |
|
1014 |
/// \ref named-templ-param "Named parameter" |
|
1015 |
///function for setting ReachedMap |
|
1016 |
/// |
|
1017 |
template<class T> |
|
1018 |
BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) |
|
1019 |
{ |
|
1020 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1021 |
return BfsWizard<DefReachedMapBase<T> >(*this); |
|
1022 |
} |
|
1023 |
|
|
1024 |
|
|
1025 |
template<class T> |
|
1026 |
struct DefProcessedMapBase : public Base { |
|
1027 |
typedef T ProcessedMap; |
|
1028 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
|
1029 |
DefProcessedMapBase(const TR &b) : TR(b) {} |
|
1030 |
}; |
|
1031 |
|
|
1032 |
///\brief \ref named-templ-param "Named parameter" |
|
1033 |
///function for setting ProcessedMap |
|
1034 |
/// |
|
1035 |
/// \ref named-templ-param "Named parameter" |
|
1036 |
///function for setting ProcessedMap |
|
1037 |
/// |
|
1038 |
template<class T> |
|
1039 |
BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) |
|
1040 |
{ |
|
1041 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1042 |
return BfsWizard<DefProcessedMapBase<T> >(*this); |
|
1043 |
} |
|
1044 |
|
|
1045 |
|
|
1046 |
template<class T> |
|
1047 |
struct DefDistMapBase : public Base { |
|
1048 |
typedef T DistMap; |
|
1049 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
|
1050 |
DefDistMapBase(const TR &b) : TR(b) {} |
|
1051 |
}; |
|
1052 |
|
|
1053 |
///\brief \ref named-templ-param "Named parameter" |
|
1054 |
///function for setting DistMap type |
|
1055 |
/// |
|
1056 |
/// \ref named-templ-param "Named parameter" |
|
1057 |
///function for setting DistMap type |
|
1058 |
/// |
|
1059 |
template<class T> |
|
1060 |
BfsWizard<DefDistMapBase<T> > distMap(const T &t) |
|
1061 |
{ |
|
1062 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1063 |
return BfsWizard<DefDistMapBase<T> >(*this); |
|
1064 |
} |
|
1065 |
|
|
1066 | 1057 |
/// Sets the source node, from which the Bfs algorithm runs. |
1067 | 1058 |
|
1068 | 1059 |
/// Sets the source node, from which the Bfs algorithm runs. |
1069 | 1060 |
/// \param s is the source node. |
1070 | 1061 |
BfsWizard<TR> &source(Node s) |
1071 | 1062 |
{ |
1072 | 1063 |
Base::_source=s; |
1073 | 1064 |
return *this; |
1074 | 1065 |
} |
1075 | 1066 |
|
1067 |
template<class T> |
|
1068 |
struct DefPredMapBase : public Base { |
|
1069 |
typedef T PredMap; |
|
1070 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
|
1071 |
DefPredMapBase(const TR &b) : TR(b) {} |
|
1072 |
}; |
|
1073 |
///\brief \ref named-templ-param "Named parameter" |
|
1074 |
///for setting \ref PredMap object. |
|
1075 |
/// |
|
1076 |
/// \ref named-templ-param "Named parameter" |
|
1077 |
///for setting \ref PredMap object. |
|
1078 |
template<class T> |
|
1079 |
BfsWizard<DefPredMapBase<T> > predMap(const T &t) |
|
1080 |
{ |
|
1081 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1082 |
return BfsWizard<DefPredMapBase<T> >(*this); |
|
1083 |
} |
|
1084 |
|
|
1085 |
template<class T> |
|
1086 |
struct DefReachedMapBase : public Base { |
|
1087 |
typedef T ReachedMap; |
|
1088 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
|
1089 |
DefReachedMapBase(const TR &b) : TR(b) {} |
|
1090 |
}; |
|
1091 |
///\brief \ref named-templ-param "Named parameter" |
|
1092 |
///for setting \ref ReachedMap object. |
|
1093 |
/// |
|
1094 |
/// \ref named-templ-param "Named parameter" |
|
1095 |
///for setting \ref ReachedMap object. |
|
1096 |
template<class T> |
|
1097 |
BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) |
|
1098 |
{ |
|
1099 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1100 |
return BfsWizard<DefReachedMapBase<T> >(*this); |
|
1101 |
} |
|
1102 |
|
|
1103 |
template<class T> |
|
1104 |
struct DefProcessedMapBase : public Base { |
|
1105 |
typedef T ProcessedMap; |
|
1106 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
|
1107 |
DefProcessedMapBase(const TR &b) : TR(b) {} |
|
1108 |
}; |
|
1109 |
///\brief \ref named-templ-param "Named parameter" |
|
1110 |
///for setting \ref ProcessedMap object. |
|
1111 |
/// |
|
1112 |
/// \ref named-templ-param "Named parameter" |
|
1113 |
///for setting \ref ProcessedMap object. |
|
1114 |
template<class T> |
|
1115 |
BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) |
|
1116 |
{ |
|
1117 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1118 |
return BfsWizard<DefProcessedMapBase<T> >(*this); |
|
1119 |
} |
|
1120 |
|
|
1121 |
template<class T> |
|
1122 |
struct DefDistMapBase : public Base { |
|
1123 |
typedef T DistMap; |
|
1124 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
|
1125 |
DefDistMapBase(const TR &b) : TR(b) {} |
|
1126 |
}; |
|
1127 |
///\brief \ref named-templ-param "Named parameter" |
|
1128 |
///for setting \ref DistMap object. |
|
1129 |
/// |
|
1130 |
/// \ref named-templ-param "Named parameter" |
|
1131 |
///for setting \ref DistMap object. |
|
1132 |
template<class T> |
|
1133 |
BfsWizard<DefDistMapBase<T> > distMap(const T &t) |
|
1134 |
{ |
|
1135 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1136 |
return BfsWizard<DefDistMapBase<T> >(*this); |
|
1137 |
} |
|
1138 |
|
|
1076 | 1139 |
}; |
1077 | 1140 |
|
1078 | 1141 |
///Function type interface for Bfs algorithm. |
1079 | 1142 |
|
1080 | 1143 |
/// \ingroup search |
1081 | 1144 |
///Function type interface for Bfs algorithm. |
... | ... |
@@ -1097,125 +1160,124 @@ |
1097 | 1160 |
bfs(const GR &g,typename GR::Node s=INVALID) |
1098 | 1161 |
{ |
1099 | 1162 |
return BfsWizard<BfsWizardBase<GR> >(g,s); |
1100 | 1163 |
} |
1101 | 1164 |
|
1102 | 1165 |
#ifdef DOXYGEN |
1103 |
/// \brief Visitor class for |
|
1166 |
/// \brief Visitor class for BFS. |
|
1104 | 1167 |
/// |
1105 | 1168 |
/// This class defines the interface of the BfsVisit events, and |
1106 |
/// it could be the base of a real |
|
1169 |
/// it could be the base of a real visitor class. |
|
1107 | 1170 |
template <typename _Digraph> |
1108 | 1171 |
struct BfsVisitor { |
1109 | 1172 |
typedef _Digraph Digraph; |
1110 | 1173 |
typedef typename Digraph::Arc Arc; |
1111 | 1174 |
typedef typename Digraph::Node Node; |
1112 |
/// \brief Called |
|
1175 |
/// \brief Called for the source node(s) of the BFS. |
|
1113 | 1176 |
/// |
1114 |
/// It is called when the bfs find an arc which target is not |
|
1115 |
/// reached yet. |
|
1177 |
/// This function is called for the source node(s) of the BFS. |
|
1178 |
void start(const Node& node) {} |
|
1179 |
/// \brief Called when a node is reached first time. |
|
1180 |
/// |
|
1181 |
/// This function is called when a node is reached first time. |
|
1182 |
void reach(const Node& node) {} |
|
1183 |
/// \brief Called when a node is processed. |
|
1184 |
/// |
|
1185 |
/// This function is called when a node is processed. |
|
1186 |
void process(const Node& node) {} |
|
1187 |
/// \brief Called when an arc reaches a new node. |
|
1188 |
/// |
|
1189 |
/// This function is called when the BFS finds an arc whose target node |
|
1190 |
/// is not reached yet. |
|
1116 | 1191 |
void discover(const Arc& arc) {} |
1117 |
/// \brief Called when the node reached first time. |
|
1118 |
/// |
|
1119 |
/// It is Called when the node reached first time. |
|
1120 |
void reach(const Node& node) {} |
|
1121 |
/// \brief Called when |
|
1192 |
/// \brief Called when an arc is examined but its target node is |
|
1122 | 1193 |
/// already discovered. |
1123 | 1194 |
/// |
1124 |
/// |
|
1195 |
/// This function is called when an arc is examined but its target node is |
|
1125 | 1196 |
/// already discovered. |
1126 | 1197 |
void examine(const Arc& arc) {} |
1127 |
/// \brief Called for the source node of the bfs. |
|
1128 |
/// |
|
1129 |
/// It is called for the source node of the bfs. |
|
1130 |
void start(const Node& node) {} |
|
1131 |
/// \brief Called when the node processed. |
|
1132 |
/// |
|
1133 |
/// It is Called when the node processed. |
|
1134 |
void process(const Node& node) {} |
|
1135 | 1198 |
}; |
1136 | 1199 |
#else |
1137 | 1200 |
template <typename _Digraph> |
1138 | 1201 |
struct BfsVisitor { |
1139 | 1202 |
typedef _Digraph Digraph; |
1140 | 1203 |
typedef typename Digraph::Arc Arc; |
1141 | 1204 |
typedef typename Digraph::Node Node; |
1205 |
void start(const Node&) {} |
|
1206 |
void reach(const Node&) {} |
|
1207 |
void process(const Node&) {} |
|
1142 | 1208 |
void discover(const Arc&) {} |
1143 |
void reach(const Node&) {} |
|
1144 | 1209 |
void examine(const Arc&) {} |
1145 |
void start(const Node&) {} |
|
1146 |
void process(const Node&) {} |
|
1147 | 1210 |
|
1148 | 1211 |
template <typename _Visitor> |
1149 | 1212 |
struct Constraints { |
1150 | 1213 |
void constraints() { |
1151 | 1214 |
Arc arc; |
1152 | 1215 |
Node node; |
1216 |
visitor.start(node); |
|
1217 |
visitor.reach(node); |
|
1218 |
visitor.process(node); |
|
1153 | 1219 |
visitor.discover(arc); |
1154 |
visitor.reach(node); |
|
1155 | 1220 |
visitor.examine(arc); |
1156 |
visitor.start(node); |
|
1157 |
visitor.process(node); |
|
1158 | 1221 |
} |
1159 | 1222 |
_Visitor& visitor; |
1160 | 1223 |
}; |
1161 | 1224 |
}; |
1162 | 1225 |
#endif |
1163 | 1226 |
|
1164 | 1227 |
/// \brief Default traits class of BfsVisit class. |
1165 | 1228 |
/// |
1166 | 1229 |
/// Default traits class of BfsVisit class. |
1167 |
/// \tparam _Digraph |
|
1230 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
|
1168 | 1231 |
template<class _Digraph> |
1169 | 1232 |
struct BfsVisitDefaultTraits { |
1170 | 1233 |
|
1171 |
/// \brief The |
|
1234 |
/// \brief The type of the digraph the algorithm runs on. |
|
1172 | 1235 |
typedef _Digraph Digraph; |
1173 | 1236 |
|
1174 | 1237 |
/// \brief The type of the map that indicates which nodes are reached. |
1175 | 1238 |
/// |
1176 | 1239 |
/// The type of the map that indicates which nodes are reached. |
1177 |
/// It must meet the \ref concepts::WriteMap "WriteMap" concept. |
|
1178 |
/// \todo named parameter to set this type, function to read and write. |
|
1240 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
1179 | 1241 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1180 | 1242 |
|
1181 |
/// \brief Instantiates a ReachedMap. |
|
1243 |
/// \brief Instantiates a \ref ReachedMap. |
|
1182 | 1244 |
/// |
1183 | 1245 |
/// This function instantiates a \ref ReachedMap. |
1184 | 1246 |
/// \param digraph is the digraph, to which |
1185 | 1247 |
/// we would like to define the \ref ReachedMap. |
1186 | 1248 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1187 | 1249 |
return new ReachedMap(digraph); |
1188 | 1250 |
} |
1189 | 1251 |
|
1190 | 1252 |
}; |
1191 | 1253 |
|
1192 | 1254 |
/// \ingroup search |
1193 | 1255 |
/// |
1194 |
/// \brief %BFS |
|
1256 |
/// \brief %BFS algorithm class with visitor interface. |
|
1195 | 1257 |
/// |
1196 | 1258 |
/// This class provides an efficient implementation of the %BFS algorithm |
1197 | 1259 |
/// with visitor interface. |
1198 | 1260 |
/// |
1199 | 1261 |
/// The %BfsVisit class provides an alternative interface to the Bfs |
1200 | 1262 |
/// class. It works with callback mechanism, the BfsVisit object calls |
1201 |
/// |
|
1263 |
/// the member functions of the \c Visitor class on every BFS event. |
|
1202 | 1264 |
/// |
1203 |
/// \tparam _Digraph The |
|
1265 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
|
1204 | 1266 |
/// The default value is |
1205 |
/// \ref ListDigraph. The value of _Digraph is not used directly by Bfs, it |
|
1206 |
/// is only passed to \ref BfsDefaultTraits. |
|
1207 |
/// \tparam _Visitor The Visitor object for the algorithm. The |
|
1208 |
/// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty Visitor which |
|
1209 |
/// does not observe the Bfs events. If you want to observe the bfs |
|
1210 |
/// events you should implement your own Visitor class. |
|
1267 |
/// \ref ListDigraph. The value of _Digraph is not used directly by |
|
1268 |
/// \ref BfsVisit, it is only passed to \ref BfsVisitDefaultTraits. |
|
1269 |
/// \tparam _Visitor The Visitor type that is used by the algorithm. |
|
1270 |
/// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty visitor, which |
|
1271 |
/// does not observe the BFS events. If you want to observe the BFS |
|
1272 |
/// events, you should implement your own visitor class. |
|
1211 | 1273 |
/// \tparam _Traits Traits class to set various data types used by the |
1212 | 1274 |
/// algorithm. The default traits class is |
1213 | 1275 |
/// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>". |
1214 | 1276 |
/// See \ref BfsVisitDefaultTraits for the documentation of |
1215 |
/// a |
|
1277 |
/// a BFS visit traits class. |
|
1216 | 1278 |
#ifdef DOXYGEN |
1217 | 1279 |
template <typename _Digraph, typename _Visitor, typename _Traits> |
1218 | 1280 |
#else |
1219 | 1281 |
template <typename _Digraph = ListDigraph, |
1220 | 1282 |
typename _Visitor = BfsVisitor<_Digraph>, |
1221 | 1283 |
typename _Traits = BfsDefaultTraits<_Digraph> > |
... | ... |
@@ -1223,52 +1285,54 @@ |
1223 | 1285 |
class BfsVisit { |
1224 | 1286 |
public: |
1225 | 1287 |
|
1226 | 1288 |
/// \brief \ref Exception for uninitialized parameters. |
1227 | 1289 |
/// |
1228 | 1290 |
/// This error represents problems in the initialization |
1229 |
/// of the parameters of the |
|
1291 |
/// of the parameters of the algorithm. |
|
1230 | 1292 |
class UninitializedParameter : public lemon::UninitializedParameter { |
1231 | 1293 |
public: |
1232 | 1294 |
virtual const char* what() const throw() |
1233 | 1295 |
{ |
1234 | 1296 |
return "lemon::BfsVisit::UninitializedParameter"; |
1235 | 1297 |
} |
1236 | 1298 |
}; |
1237 | 1299 |
|
1300 |
///The traits class. |
|
1238 | 1301 |
typedef _Traits Traits; |
1239 | 1302 |
|
1303 |
///The type of the digraph the algorithm runs on. |
|
1240 | 1304 |
typedef typename Traits::Digraph Digraph; |
1241 | 1305 |
|
1306 |
///The visitor type used by the algorithm. |
|
1242 | 1307 |
typedef _Visitor Visitor; |
1243 | 1308 |
|
1244 |
///The type of the map |
|
1309 |
///The type of the map that indicates which nodes are reached. |
|
1245 | 1310 |
typedef typename Traits::ReachedMap ReachedMap; |
1246 | 1311 |
|
1247 | 1312 |
private: |
1248 | 1313 |
|
1249 | 1314 |
typedef typename Digraph::Node Node; |
1250 | 1315 |
typedef typename Digraph::NodeIt NodeIt; |
1251 | 1316 |
typedef typename Digraph::Arc Arc; |
1252 | 1317 |
typedef typename Digraph::OutArcIt OutArcIt; |
1253 | 1318 |
|
1254 |
// |
|
1319 |
//Pointer to the underlying digraph. |
|
1255 | 1320 |
const Digraph *_digraph; |
1256 |
// |
|
1321 |
//Pointer to the visitor object. |
|
1257 | 1322 |
Visitor *_visitor; |
1258 |
|
|
1323 |
//Pointer to the map of reached status of the nodes. |
|
1259 | 1324 |
ReachedMap *_reached; |
1260 |
// |
|
1325 |
//Indicates if _reached is locally allocated (true) or not. |
|
1261 | 1326 |
bool local_reached; |
1262 | 1327 |
|
1263 | 1328 |
std::vector<typename Digraph::Node> _list; |
1264 | 1329 |
int _list_front, _list_back; |
1265 | 1330 |
|
1266 |
/// \brief Creates the maps if necessary. |
|
1267 |
/// |
|
1268 | 1331 |
/// Creates the maps if necessary. |
1332 |
///\todo Better memory allocation (instead of new). |
|
1269 | 1333 |
void create_maps() { |
1270 | 1334 |
if(!_reached) { |
1271 | 1335 |
local_reached = true; |
1272 | 1336 |
_reached = Traits::createReachedMap(*_digraph); |
1273 | 1337 |
} |
1274 | 1338 |
} |
... | ... |
@@ -1289,15 +1353,15 @@ |
1289 | 1353 |
typedef T ReachedMap; |
1290 | 1354 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1291 | 1355 |
throw UninitializedParameter(); |
1292 | 1356 |
} |
1293 | 1357 |
}; |
1294 | 1358 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1295 |
/// ReachedMap type |
|
1359 |
/// ReachedMap type. |
|
1296 | 1360 |
/// |
1297 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type |
|
1361 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
|
1298 | 1362 |
template <class T> |
1299 | 1363 |
struct DefReachedMap : public BfsVisit< Digraph, Visitor, |
1300 | 1364 |
DefReachedMapTraits<T> > { |
1301 | 1365 |
typedef BfsVisit< Digraph, Visitor, DefReachedMapTraits<T> > Create; |
1302 | 1366 |
}; |
1303 | 1367 |
///@} |
... | ... |
@@ -1305,58 +1369,57 @@ |
1305 | 1369 |
public: |
1306 | 1370 |
|
1307 | 1371 |
/// \brief Constructor. |
1308 | 1372 |
/// |
1309 | 1373 |
/// Constructor. |
1310 | 1374 |
/// |
1311 |
/// \param digraph the digraph the algorithm will run on. |
|
1312 |
/// \param visitor The visitor of the algorithm. |
|
1313 |
/// |
|
1375 |
/// \param digraph The digraph the algorithm runs on. |
|
1376 |
/// \param visitor The visitor object of the algorithm. |
|
1314 | 1377 |
BfsVisit(const Digraph& digraph, Visitor& visitor) |
1315 | 1378 |
: _digraph(&digraph), _visitor(&visitor), |
1316 | 1379 |
_reached(0), local_reached(false) {} |
1317 | 1380 |
|
1318 | 1381 |
/// \brief Destructor. |
1319 |
/// |
|
1320 |
/// Destructor. |
|
1321 | 1382 |
~BfsVisit() { |
1322 | 1383 |
if(local_reached) delete _reached; |
1323 | 1384 |
} |
1324 | 1385 |
|
1325 |
/// \brief Sets the map |
|
1386 |
/// \brief Sets the map that indicates which nodes are reached. |
|
1326 | 1387 |
/// |
1327 |
/// Sets the map |
|
1388 |
/// Sets the map that indicates which nodes are reached. |
|
1328 | 1389 |
/// If you don't use this function before calling \ref run(), |
1329 |
/// it will allocate one. The |
|
1390 |
/// it will allocate one. The destructor deallocates this |
|
1330 | 1391 |
/// automatically allocated map, of course. |
1331 | 1392 |
/// \return <tt> (*this) </tt> |
1332 | 1393 |
BfsVisit &reachedMap(ReachedMap &m) { |
1333 | 1394 |
if(local_reached) { |
1334 | 1395 |
delete _reached; |
1335 | 1396 |
local_reached = false; |
1336 | 1397 |
} |
1337 | 1398 |
_reached = &m; |
1338 | 1399 |
return *this; |
1339 | 1400 |
} |
1340 | 1401 |
|
1341 | 1402 |
public: |
1403 |
|
|
1342 | 1404 |
/// \name Execution control |
1343 | 1405 |
/// The simplest way to execute the algorithm is to use |
1344 |
/// one of the member functions called \ |
|
1406 |
/// one of the member functions called \ref lemon::BfsVisit::run() |
|
1407 |
/// "run()". |
|
1345 | 1408 |
/// \n |
1346 |
/// If you need more control on the execution, |
|
1347 |
/// first you must call \ref init(), then you can adda source node |
|
1348 |
/// with \ref addSource(). |
|
1349 |
/// Finally \ref start() will perform the actual path |
|
1350 |
/// |
|
1409 |
/// If you need more control on the execution, first you must call |
|
1410 |
/// \ref lemon::BfsVisit::init() "init()", then you can add several |
|
1411 |
/// source nodes with \ref lemon::BfsVisit::addSource() "addSource()". |
|
1412 |
/// Finally \ref lemon::BfsVisit::start() "start()" will perform the |
|
1413 |
/// actual path computation. |
|
1351 | 1414 |
|
1352 | 1415 |
/// @{ |
1416 |
|
|
1353 | 1417 |
/// \brief Initializes the internal data structures. |
1354 | 1418 |
/// |
1355 | 1419 |
/// Initializes the internal data structures. |
1356 |
/// |
|
1357 | 1420 |
void init() { |
1358 | 1421 |
create_maps(); |
1359 | 1422 |
_list.resize(countNodes(*_digraph)); |
1360 | 1423 |
_list_front = _list_back = -1; |
1361 | 1424 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1362 | 1425 |
_reached->set(u, false); |
... | ... |
@@ -1378,13 +1441,13 @@ |
1378 | 1441 |
/// \brief Processes the next node. |
1379 | 1442 |
/// |
1380 | 1443 |
/// Processes the next node. |
1381 | 1444 |
/// |
1382 | 1445 |
/// \return The processed node. |
1383 | 1446 |
/// |
1384 |
/// \pre The queue must not be empty |
|
1447 |
/// \pre The queue must not be empty. |
|
1385 | 1448 |
Node processNextNode() { |
1386 | 1449 |
Node n = _list[++_list_front]; |
1387 | 1450 |
_visitor->process(n); |
1388 | 1451 |
Arc e; |
1389 | 1452 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1390 | 1453 |
Node m = _digraph->target(e); |
... | ... |
@@ -1399,22 +1462,23 @@ |
1399 | 1462 |
} |
1400 | 1463 |
return n; |
1401 | 1464 |
} |
1402 | 1465 |
|
1403 | 1466 |
/// \brief Processes the next node. |
1404 | 1467 |
/// |
1405 |
/// Processes the next node |
|
1468 |
/// Processes the next node and checks if the given target node |
|
1406 | 1469 |
/// is reached. If the target node is reachable from the processed |
1407 |
/// node then the reached parameter will be set true. The reached |
|
1408 |
/// parameter should be initially false. |
|
1470 |
/// node, then the \c reach parameter will be set to \c true. |
|
1409 | 1471 |
/// |
1410 | 1472 |
/// \param target The target node. |
1411 |
/// \retval reach Indicates |
|
1473 |
/// \retval reach Indicates if the target node is reached. |
|
1474 |
/// It should be initially \c false. |
|
1475 |
/// |
|
1412 | 1476 |
/// \return The processed node. |
1413 | 1477 |
/// |
1414 |
/// \ |
|
1478 |
/// \pre The queue must not be empty. |
|
1415 | 1479 |
Node processNextNode(Node target, bool& reach) { |
1416 | 1480 |
Node n = _list[++_list_front]; |
1417 | 1481 |
_visitor->process(n); |
1418 | 1482 |
Arc e; |
1419 | 1483 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
1420 | 1484 |
Node m = _digraph->target(e); |
... | ... |
@@ -1430,22 +1494,25 @@ |
1430 | 1494 |
} |
1431 | 1495 |
return n; |
1432 | 1496 |
} |
1433 | 1497 |
|
1434 | 1498 |
/// \brief Processes the next node. |
1435 | 1499 |
/// |
1436 |
/// Processes the next node. And checks that at least one of |
|
1437 |
/// reached node has true value in the \c nm node map. If one node |
|
1438 |
/// with true value is reachable from the processed node then the |
|
1439 |
/// rnode parameter will be set to the first of such nodes. |
|
1500 |
/// Processes the next node and checks if at least one of reached |
|
1501 |
/// nodes has \c true value in the \c nm node map. If one node |
|
1502 |
/// with \c true value is reachable from the processed node, then the |
|
1503 |
/// \c rnode parameter will be set to the first of such nodes. |
|
1440 | 1504 |
/// |
1441 |
/// \param nm |
|
1505 |
/// \param nm A \c bool (or convertible) node map that indicates the |
|
1506 |
/// possible targets. |
|
1442 | 1507 |
/// \retval rnode The reached target node. |
1508 |
/// It should be initially \c INVALID. |
|
1509 |
/// |
|
1443 | 1510 |
/// \return The processed node. |
1444 | 1511 |
/// |
1445 |
/// \ |
|
1512 |
/// \pre The queue must not be empty. |
|
1446 | 1513 |
template <typename NM> |
1447 | 1514 |
Node processNextNode(const NM& nm, Node& rnode) { |
1448 | 1515 |
Node n = _list[++_list_front]; |
1449 | 1516 |
_visitor->process(n); |
1450 | 1517 |
Arc e; |
1451 | 1518 |
for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) { |
... | ... |
@@ -1460,105 +1527,153 @@ |
1460 | 1527 |
_visitor->examine(e); |
1461 | 1528 |
} |
1462 | 1529 |
} |
1463 | 1530 |
return n; |
1464 | 1531 |
} |
1465 | 1532 |
|
1466 |
/// \brief |
|
1533 |
/// \brief The next node to be processed. |
|
1467 | 1534 |
/// |
1468 |
/// Next node to be processed. |
|
1469 |
/// |
|
1470 |
/// \return The next node to be processed or INVALID if the stack is |
|
1471 |
/// empty. |
|
1472 |
|
|
1535 |
/// Returns the next node to be processed or \c INVALID if the queue |
|
1536 |
/// is empty. |
|
1537 |
Node nextNode() const { |
|
1473 | 1538 |
return _list_front != _list_back ? _list[_list_front + 1] : INVALID; |
1474 | 1539 |
} |
1475 | 1540 |
|
1476 | 1541 |
/// \brief Returns \c false if there are nodes |
1477 |
/// to be processed |
|
1542 |
/// to be processed. |
|
1478 | 1543 |
/// |
1479 | 1544 |
/// Returns \c false if there are nodes |
1480 |
/// to be processed in the queue |
|
1481 |
bool emptyQueue() { return _list_front == _list_back; } |
|
1545 |
/// to be processed in the queue. |
|
1546 |
bool emptyQueue() const { return _list_front == _list_back; } |
|
1482 | 1547 |
|
1483 | 1548 |
/// \brief Returns the number of the nodes to be processed. |
1484 | 1549 |
/// |
1485 | 1550 |
/// Returns the number of the nodes to be processed in the queue. |
1486 |
int queueSize() { return _list_back - _list_front; } |
|
1551 |
int queueSize() const { return _list_back - _list_front; } |
|
1487 | 1552 |
|
1488 | 1553 |
/// \brief Executes the algorithm. |
1489 | 1554 |
/// |
1490 | 1555 |
/// Executes the algorithm. |
1491 | 1556 |
/// |
1492 |
/// |
|
1557 |
/// This method runs the %BFS algorithm from the root node(s) |
|
1558 |
/// in order to compute the shortest path to each node. |
|
1559 |
/// |
|
1560 |
/// The algorithm computes |
|
1561 |
/// - the shortest path tree (forest), |
|
1562 |
/// - the distance of each node from the root(s). |
|
1563 |
/// |
|
1564 |
/// \pre init() must be called and at least one root node should be added |
|
1493 | 1565 |
/// with addSource() before using this function. |
1566 |
/// |
|
1567 |
/// \note <tt>b.start()</tt> is just a shortcut of the following code. |
|
1568 |
/// \code |
|
1569 |
/// while ( !b.emptyQueue() ) { |
|
1570 |
/// b.processNextNode(); |
|
1571 |
/// } |
|
1572 |
/// \endcode |
|
1494 | 1573 |
void start() { |
1495 | 1574 |
while ( !emptyQueue() ) processNextNode(); |
1496 | 1575 |
} |
1497 | 1576 |
|
1498 |
/// \brief Executes the algorithm until |
|
1577 |
/// \brief Executes the algorithm until the given target node is reached. |
|
1499 | 1578 |
/// |
1500 |
/// Executes the algorithm until |
|
1579 |
/// Executes the algorithm until the given target node is reached. |
|
1501 | 1580 |
/// |
1502 |
/// \pre init() must be called and at least one node should be added |
|
1503 |
/// with addSource() before using this function. |
|
1581 |
/// This method runs the %BFS algorithm from the root node(s) |
|
1582 |
/// in order to compute the shortest path to \c dest. |
|
1583 |
/// |
|
1584 |
/// The algorithm computes |
|
1585 |
/// - the shortest path to \c dest, |
|
1586 |
/// - the distance of \c dest from the root(s). |
|
1587 |
/// |
|
1588 |
/// \pre init() must be called and at least one root node should be |
|
1589 |
/// added with addSource() before using this function. |
|
1590 |
/// |
|
1591 |
/// \note <tt>b.start(t)</tt> is just a shortcut of the following code. |
|
1592 |
/// \code |
|
1593 |
/// bool reach = false; |
|
1594 |
/// while ( !b.emptyQueue() && !reach ) { |
|
1595 |
/// b.processNextNode(t, reach); |
|
1596 |
/// } |
|
1597 |
/// \endcode |
|
1504 | 1598 |
void start(Node dest) { |
1505 | 1599 |
bool reach = false; |
1506 | 1600 |
while ( !emptyQueue() && !reach ) processNextNode(dest, reach); |
1507 | 1601 |
} |
1508 | 1602 |
|
1509 | 1603 |
/// \brief Executes the algorithm until a condition is met. |
1510 | 1604 |
/// |
1511 | 1605 |
/// Executes the algorithm until a condition is met. |
1512 | 1606 |
/// |
1513 |
/// \pre init() must be called and at least one node should be added |
|
1514 |
/// with addSource() before using this function. |
|
1607 |
/// This method runs the %BFS algorithm from the root node(s) in |
|
1608 |
/// order to compute the shortest path to a node \c v with |
|
1609 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
|
1515 | 1610 |
/// |
1516 | 1611 |
///\param nm must be a bool (or convertible) node map. The |
1517 | 1612 |
///algorithm will stop when it reaches a node \c v with |
1518 | 1613 |
/// <tt>nm[v]</tt> true. |
1519 | 1614 |
/// |
1520 | 1615 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
1521 | 1616 |
///\c INVALID if no such node was found. |
1617 |
/// |
|
1618 |
/// \pre init() must be called and at least one root node should be |
|
1619 |
/// added with addSource() before using this function. |
|
1620 |
/// |
|
1621 |
/// \note <tt>b.start(nm)</tt> is just a shortcut of the following code. |
|
1622 |
/// \code |
|
1623 |
/// Node rnode = INVALID; |
|
1624 |
/// while ( !b.emptyQueue() && rnode == INVALID ) { |
|
1625 |
/// b.processNextNode(nm, rnode); |
|
1626 |
/// } |
|
1627 |
/// return rnode; |
|
1628 |
/// \endcode |
|
1522 | 1629 |
template <typename NM> |
1523 | 1630 |
Node start(const NM &nm) { |
1524 | 1631 |
Node rnode = INVALID; |
1525 | 1632 |
while ( !emptyQueue() && rnode == INVALID ) { |
1526 | 1633 |
processNextNode(nm, rnode); |
1527 | 1634 |
} |
1528 | 1635 |
return rnode; |
1529 | 1636 |
} |
1530 | 1637 |
|
1531 |
/// \brief Runs |
|
1638 |
/// \brief Runs the algorithm from the given node. |
|
1532 | 1639 |
/// |
1533 |
/// This method runs the %BFS algorithm from a root node \c s. |
|
1534 |
/// \note b.run(s) is just a shortcut of the following code. |
|
1640 |
/// This method runs the %BFS algorithm from node \c s |
|
1641 |
/// in order to compute the shortest path to each node. |
|
1642 |
/// |
|
1643 |
/// The algorithm computes |
|
1644 |
/// - the shortest path tree, |
|
1645 |
/// - the distance of each node from the root. |
|
1646 |
/// |
|
1647 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
|
1535 | 1648 |
///\code |
1536 | 1649 |
/// b.init(); |
1537 | 1650 |
/// b.addSource(s); |
1538 | 1651 |
/// b.start(); |
1539 | 1652 |
///\endcode |
1540 | 1653 |
void run(Node s) { |
1541 | 1654 |
init(); |
1542 | 1655 |
addSource(s); |
1543 | 1656 |
start(); |
1544 | 1657 |
} |
1545 | 1658 |
|
1546 |
/// \brief Runs |
|
1659 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
|
1547 | 1660 |
/// |
1548 | 1661 |
/// This method runs the %BFS algorithm in order to |
1549 |
/// compute the %BFS path to each node. The algorithm computes |
|
1550 |
/// - The %BFS tree. |
|
1551 |
/// |
|
1662 |
/// compute the shortest path to each node. |
|
1552 | 1663 |
/// |
1553 |
/// |
|
1664 |
/// The algorithm computes |
|
1665 |
/// - the shortest path tree (forest), |
|
1666 |
/// - the distance of each node from the root(s). |
|
1667 |
/// |
|
1668 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
|
1554 | 1669 |
///\code |
1555 | 1670 |
/// b.init(); |
1556 |
/// for (NodeIt it(digraph); it != INVALID; ++it) { |
|
1557 |
/// if (!b.reached(it)) { |
|
1558 |
/// |
|
1671 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
|
1672 |
/// if (!b.reached(n)) { |
|
1673 |
/// b.addSource(n); |
|
1559 | 1674 |
/// b.start(); |
1560 | 1675 |
/// } |
1561 | 1676 |
/// } |
1562 | 1677 |
///\endcode |
1563 | 1678 |
void run() { |
1564 | 1679 |
init(); |
... | ... |
@@ -1566,30 +1681,31 @@ |
1566 | 1681 |
if (!reached(it)) { |
1567 | 1682 |
addSource(it); |
1568 | 1683 |
start(); |
1569 | 1684 |
} |
1570 | 1685 |
} |
1571 | 1686 |
} |
1687 |
|
|
1572 | 1688 |
///@} |
1573 | 1689 |
|
1574 | 1690 |
/// \name Query Functions |
1575 | 1691 |
/// The result of the %BFS algorithm can be obtained using these |
1576 | 1692 |
/// functions.\n |
1577 |
/// Before the use of these functions, |
|
1578 |
/// either run() or start() must be called. |
|
1693 |
/// Either \ref lemon::BfsVisit::run() "run()" or |
|
1694 |
/// \ref lemon::BfsVisit::start() "start()" must be called before |
|
1695 |
/// using them. |
|
1579 | 1696 |
///@{ |
1580 | 1697 |
|
1581 |
/// \brief Checks if a node is reachable from the root. |
|
1698 |
/// \brief Checks if a node is reachable from the root(s). |
|
1582 | 1699 |
/// |
1583 | 1700 |
/// Returns \c true if \c v is reachable from the root(s). |
1584 |
/// \warning The source nodes are inditated as unreachable. |
|
1585 | 1701 |
/// \pre Either \ref run() or \ref start() |
1586 | 1702 |
/// must be called before using this function. |
1587 |
/// |
|
1588 | 1703 |
bool reached(Node v) { return (*_reached)[v]; } |
1704 |
|
|
1589 | 1705 |
///@} |
1706 |
|
|
1590 | 1707 |
}; |
1591 | 1708 |
|
1592 | 1709 |
} //END OF NAMESPACE LEMON |
1593 | 1710 |
|
1594 | 1711 |
#endif |
1595 |
... | ... |
@@ -18,111 +18,114 @@ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_DFS_H |
20 | 20 |
#define LEMON_DFS_H |
21 | 21 |
|
22 | 22 |
///\ingroup search |
23 | 23 |
///\file |
24 |
///\brief |
|
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 |
#include <lemon/assert.h> |
|
30 | 31 |
#include <lemon/maps.h> |
31 | 32 |
|
32 |
#include <lemon/concept_check.h> |
|
33 |
|
|
34 | 33 |
namespace lemon { |
35 | 34 |
|
36 |
|
|
37 | 35 |
///Default traits class of Dfs class. |
38 | 36 |
|
39 | 37 |
///Default traits class of Dfs class. |
40 | 38 |
///\tparam GR Digraph type. |
41 | 39 |
template<class GR> |
42 | 40 |
struct DfsDefaultTraits |
43 | 41 |
{ |
44 |
///The |
|
42 |
///The type of the digraph the algorithm runs on. |
|
45 | 43 |
typedef GR Digraph; |
46 |
|
|
44 |
|
|
45 |
///\brief The type of the map that stores the predecessor |
|
47 | 46 |
///arcs of the %DFS paths. |
48 | 47 |
/// |
49 |
///The type of the map that stores the |
|
48 |
///The type of the map that stores the predecessor |
|
50 | 49 |
///arcs of the %DFS paths. |
51 | 50 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
52 |
/// |
|
53 |
typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap; |
|
54 |
|
|
51 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
|
52 |
///Instantiates a \ref PredMap. |
|
55 | 53 |
|
56 | 54 |
///This function instantiates a \ref PredMap. |
57 |
///\param |
|
55 |
///\param g is the digraph, to which we would like to define the |
|
56 |
///\ref PredMap. |
|
58 | 57 |
///\todo The digraph alone may be insufficient to initialize |
59 |
static PredMap *createPredMap(const |
|
58 |
static PredMap *createPredMap(const Digraph &g) |
|
60 | 59 |
{ |
61 |
return new PredMap( |
|
60 |
return new PredMap(g); |
|
62 | 61 |
} |
63 | 62 |
|
64 | 63 |
///The type of the map that indicates which nodes are processed. |
65 | 64 |
|
66 | 65 |
///The type of the map that indicates which nodes are processed. |
67 | 66 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
68 |
/// |
|
67 |
///By default it is a NullMap. |
|
69 | 68 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
70 |
///Instantiates a ProcessedMap. |
|
69 |
///Instantiates a \ref ProcessedMap. |
|
71 | 70 |
|
72 | 71 |
///This function instantiates a \ref ProcessedMap. |
73 | 72 |
///\param g is the digraph, to which |
74 | 73 |
///we would like to define the \ref ProcessedMap |
75 | 74 |
#ifdef DOXYGEN |
76 |
static ProcessedMap *createProcessedMap(const |
|
75 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
|
77 | 76 |
#else |
78 |
static ProcessedMap *createProcessedMap(const |
|
77 |
static ProcessedMap *createProcessedMap(const Digraph &) |
|
79 | 78 |
#endif |
80 | 79 |
{ |
81 | 80 |
return new ProcessedMap(); |
82 | 81 |
} |
82 |
|
|
83 | 83 |
///The type of the map that indicates which nodes are reached. |
84 | 84 |
|
85 | 85 |
///The type of the map that indicates which nodes are reached. |
86 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
|
87 |
///\todo named parameter to set this type, function to read and write. |
|
86 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
88 | 87 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
89 |
///Instantiates a ReachedMap. |
|
88 |
///Instantiates a \ref ReachedMap. |
|
90 | 89 |
|
91 | 90 |
///This function instantiates a \ref ReachedMap. |
92 |
///\param |
|
91 |
///\param g is the digraph, to which |
|
93 | 92 |
///we would like to define the \ref ReachedMap. |
94 |
static ReachedMap *createReachedMap(const |
|
93 |
static ReachedMap *createReachedMap(const Digraph &g) |
|
95 | 94 |
{ |
96 |
return new ReachedMap( |
|
95 |
return new ReachedMap(g); |
|
97 | 96 |
} |
98 |
///The type of the map that stores the dists of the nodes. |
|
99 | 97 |
|
100 |
///The type of the map that stores the |
|
98 |
///The type of the map that stores the distances of the nodes. |
|
99 |
|
|
100 |
///The type of the map that stores the distances of the nodes. |
|
101 | 101 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
102 |
/// |
|
103 | 102 |
typedef typename Digraph::template NodeMap<int> DistMap; |
104 |
///Instantiates a DistMap. |
|
103 |
///Instantiates a \ref DistMap. |
|
105 | 104 |
|
106 | 105 |
///This function instantiates a \ref DistMap. |
107 |
///\param G is the digraph, to which we would like to define |
|
108 |
///the \ref DistMap |
|
109 |
|
|
106 |
///\param g is the digraph, to which we would like to define the |
|
107 |
///\ref DistMap. |
|
108 |
static DistMap *createDistMap(const Digraph &g) |
|
110 | 109 |
{ |
111 |
return new DistMap( |
|
110 |
return new DistMap(g); |
|
112 | 111 |
} |
113 | 112 |
}; |
114 | 113 |
|
115 | 114 |
///%DFS algorithm class. |
116 | 115 |
|
117 | 116 |
///\ingroup search |
118 | 117 |
///This class provides an efficient implementation of the %DFS algorithm. |
119 | 118 |
/// |
120 |
///\tparam GR The digraph type the algorithm runs on. The default value is |
|
121 |
///\ref ListDigraph. The value of GR is not used directly by Dfs, it |
|
122 |
///is |
|
119 |
///There is also a \ref dfs() "function type interface" for the DFS |
|
120 |
///algorithm, which is convenient in the simplier cases and it can be |
|
121 |
///used easier. |
|
122 |
/// |
|
123 |
///\tparam GR The type of the digraph the algorithm runs on. |
|
124 |
///The default value is \ref ListDigraph. The value of GR is not used |
|
125 |
///directly by \ref Dfs, it is only passed to \ref DfsDefaultTraits. |
|
123 | 126 |
///\tparam TR Traits class to set various data types used by the algorithm. |
124 | 127 |
///The default traits class is |
125 | 128 |
///\ref DfsDefaultTraits "DfsDefaultTraits<GR>". |
126 | 129 |
///See \ref DfsDefaultTraits for the documentation of |
127 | 130 |
///a Dfs traits class. |
128 | 131 |
#ifdef DOXYGEN |
... | ... |
@@ -131,71 +134,71 @@ |
131 | 134 |
#else |
132 | 135 |
template <typename GR=ListDigraph, |
133 | 136 |
typename TR=DfsDefaultTraits<GR> > |
134 | 137 |
#endif |
135 | 138 |
class Dfs { |
136 | 139 |
public: |
137 |
/** |
|
138 |
* \brief \ref Exception for uninitialized parameters. |
|
139 |
* |
|
140 |
* This error represents problems in the initialization |
|
141 |
* of the parameters of the algorithms. |
|
142 |
*/ |
|
140 |
///\ref Exception for uninitialized parameters. |
|
141 |
|
|
142 |
///This error represents problems in the initialization of the |
|
143 |
///parameters of the algorithm. |
|
143 | 144 |
class UninitializedParameter : public lemon::UninitializedParameter { |
144 | 145 |
public: |
145 | 146 |
virtual const char* what() const throw() { |
146 | 147 |
return "lemon::Dfs::UninitializedParameter"; |
147 | 148 |
} |
148 | 149 |
}; |
149 | 150 |
|
151 |
///The type of the digraph the algorithm runs on. |
|
152 |
typedef typename TR::Digraph Digraph; |
|
153 |
|
|
154 |
///\brief The type of the map that stores the predecessor arcs of the |
|
155 |
///DFS paths. |
|
156 |
typedef typename TR::PredMap PredMap; |
|
157 |
///The type of the map that stores the distances of the nodes. |
|
158 |
typedef typename TR::DistMap DistMap; |
|
159 |
///The type of the map that indicates which nodes are reached. |
|
160 |
typedef typename TR::ReachedMap ReachedMap; |
|
161 |
///The type of the map that indicates which nodes are processed. |
|
162 |
typedef typename TR::ProcessedMap ProcessedMap; |
|
163 |
///The type of the paths. |
|
164 |
typedef PredMapPath<Digraph, PredMap> Path; |
|
165 |
|
|
166 |
///The traits class. |
|
150 | 167 |
typedef TR Traits; |
151 |
///The type of the underlying digraph. |
|
152 |
typedef typename TR::Digraph Digraph; |
|
153 |
|
|
168 |
|
|
169 |
private: |
|
170 |
|
|
154 | 171 |
typedef typename Digraph::Node Node; |
155 |
///\e |
|
156 | 172 |
typedef typename Digraph::NodeIt NodeIt; |
157 |
///\e |
|
158 | 173 |
typedef typename Digraph::Arc Arc; |
159 |
///\e |
|
160 | 174 |
typedef typename Digraph::OutArcIt OutArcIt; |
161 | 175 |
|
162 |
///\brief The type of the map that stores the last |
|
163 |
///arcs of the %DFS paths. |
|
164 |
typedef typename TR::PredMap PredMap; |
|
165 |
///The type of the map indicating which nodes are reached. |
|
166 |
typedef typename TR::ReachedMap ReachedMap; |
|
167 |
///The type of the map indicating which nodes are processed. |
|
168 |
typedef typename TR::ProcessedMap ProcessedMap; |
|
169 |
///The type of the map that stores the dists of the nodes. |
|
170 |
typedef typename TR::DistMap DistMap; |
|
171 |
private: |
|
172 |
// |
|
176 |
//Pointer to the underlying digraph. |
|
173 | 177 |
const Digraph *G; |
174 |
|
|
178 |
//Pointer to the map of predecessor arcs. |
|
175 | 179 |
PredMap *_pred; |
176 |
// |
|
180 |
//Indicates if _pred is locally allocated (true) or not. |
|
177 | 181 |
bool local_pred; |
178 |
|
|
182 |
//Pointer to the map of distances. |
|
179 | 183 |
DistMap *_dist; |
180 |
// |
|
184 |
//Indicates if _dist is locally allocated (true) or not. |
|
181 | 185 |
bool local_dist; |
182 |
|
|
186 |
//Pointer to the map of reached status of the nodes. |
|
183 | 187 |
ReachedMap *_reached; |
184 |
// |
|
188 |
//Indicates if _reached is locally allocated (true) or not. |
|
185 | 189 |
bool local_reached; |
186 |
|
|
190 |
//Pointer to the map of processed status of the nodes. |
|
187 | 191 |
ProcessedMap *_processed; |
188 |
// |
|
192 |
//Indicates if _processed is locally allocated (true) or not. |
|
189 | 193 |
bool local_processed; |
190 | 194 |
|
191 | 195 |
std::vector<typename Digraph::OutArcIt> _stack; |
192 | 196 |
int _stack_head; |
193 | 197 |
|
194 | 198 |
///Creates the maps if necessary. |
195 |
|
|
196 | 199 |
///\todo Better memory allocation (instead of new). |
197 | 200 |
void create_maps() |
198 | 201 |
{ |
199 | 202 |
if(!_pred) { |
200 | 203 |
local_pred = true; |
201 | 204 |
_pred = Traits::createPredMap(*G); |
... | ... |
@@ -226,59 +229,58 @@ |
226 | 229 |
|
227 | 230 |
///@{ |
228 | 231 |
|
229 | 232 |
template <class T> |
230 | 233 |
struct DefPredMapTraits : public Traits { |
231 | 234 |
typedef T PredMap; |
232 |
static PredMap *createPredMap(const Digraph & |
|
235 |
static PredMap *createPredMap(const Digraph &) |
|
233 | 236 |
{ |
234 | 237 |
throw UninitializedParameter(); |
235 | 238 |
} |
236 | 239 |
}; |
237 | 240 |
///\brief \ref named-templ-param "Named parameter" for setting |
238 |
///PredMap type |
|
241 |
///\ref PredMap type. |
|
239 | 242 |
/// |
240 |
///\ref named-templ-param "Named parameter" for setting PredMap type |
|
241 |
/// |
|
243 |
///\ref named-templ-param "Named parameter" for setting |
|
244 |
///\ref PredMap type. |
|
242 | 245 |
template <class T> |
243 | 246 |
struct DefPredMap : public Dfs<Digraph, DefPredMapTraits<T> > { |
244 | 247 |
typedef Dfs<Digraph, DefPredMapTraits<T> > Create; |
245 | 248 |
}; |
246 | 249 |
|
247 |
|
|
248 | 250 |
template <class T> |
249 | 251 |
struct DefDistMapTraits : public Traits { |
250 | 252 |
typedef T DistMap; |
251 | 253 |
static DistMap *createDistMap(const Digraph &) |
252 | 254 |
{ |
253 | 255 |
throw UninitializedParameter(); |
254 | 256 |
} |
255 | 257 |
}; |
256 | 258 |
///\brief \ref named-templ-param "Named parameter" for setting |
257 |
///DistMap type |
|
259 |
///\ref DistMap type. |
|
258 | 260 |
/// |
259 |
///\ref named-templ-param "Named parameter" for setting DistMap |
|
260 |
///type |
|
261 |
///\ref named-templ-param "Named parameter" for setting |
|
262 |
///\ref DistMap type. |
|
261 | 263 |
template <class T> |
262 |
struct DefDistMap { |
|
264 |
struct DefDistMap : public Dfs< Digraph, DefDistMapTraits<T> > { |
|
263 | 265 |
typedef Dfs<Digraph, DefDistMapTraits<T> > Create; |
264 | 266 |
}; |
265 | 267 |
|
266 | 268 |
template <class T> |
267 | 269 |
struct DefReachedMapTraits : public Traits { |
268 | 270 |
typedef T ReachedMap; |
269 | 271 |
static ReachedMap *createReachedMap(const Digraph &) |
270 | 272 |
{ |
271 | 273 |
throw UninitializedParameter(); |
272 | 274 |
} |
273 | 275 |
}; |
274 | 276 |
///\brief \ref named-templ-param "Named parameter" for setting |
275 |
///ReachedMap type |
|
277 |
///\ref ReachedMap type. |
|
276 | 278 |
/// |
277 |
///\ref named-templ-param "Named parameter" for setting ReachedMap type |
|
278 |
/// |
|
279 |
///\ref named-templ-param "Named parameter" for setting |
|
280 |
///\ref ReachedMap type. |
|
279 | 281 |
template <class T> |
280 | 282 |
struct DefReachedMap : public Dfs< Digraph, DefReachedMapTraits<T> > { |
281 | 283 |
typedef Dfs< Digraph, DefReachedMapTraits<T> > Create; |
282 | 284 |
}; |
283 | 285 |
|
284 | 286 |
template <class T> |
... | ... |
@@ -287,50 +289,50 @@ |
287 | 289 |
static ProcessedMap *createProcessedMap(const Digraph &) |
288 | 290 |
{ |
289 | 291 |
throw UninitializedParameter(); |
290 | 292 |
} |
291 | 293 |
}; |
292 | 294 |
///\brief \ref named-templ-param "Named parameter" for setting |
293 |
///ProcessedMap type |
|
295 |
///\ref ProcessedMap type. |
|
294 | 296 |
/// |
295 |
///\ref named-templ-param "Named parameter" for setting ProcessedMap type |
|
296 |
/// |
|
297 |
///\ref named-templ-param "Named parameter" for setting |
|
298 |
///\ref ProcessedMap type. |
|
297 | 299 |
template <class T> |
298 | 300 |
struct DefProcessedMap : public Dfs< Digraph, DefProcessedMapTraits<T> > { |
299 | 301 |
typedef Dfs< Digraph, DefProcessedMapTraits<T> > Create; |
300 | 302 |
}; |
301 | 303 |
|
302 | 304 |
struct DefDigraphProcessedMapTraits : public Traits { |
303 | 305 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
304 |
static ProcessedMap *createProcessedMap(const Digraph & |
|
306 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
|
305 | 307 |
{ |
306 |
return new ProcessedMap( |
|
308 |
return new ProcessedMap(g); |
|
307 | 309 |
} |
308 | 310 |
}; |
309 |
///\brief \ref named-templ-param "Named parameter" |
|
310 |
///for setting the ProcessedMap type to be Digraph::NodeMap<bool>. |
|
311 |
///\brief \ref named-templ-param "Named parameter" for setting |
|
312 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
311 | 313 |
/// |
312 |
///\ref named-templ-param "Named parameter" |
|
313 |
///for setting the ProcessedMap type to be Digraph::NodeMap<bool>. |
|
314 |
/// |
|
314 |
///\ref named-templ-param "Named parameter" for setting |
|
315 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
316 |
///If you don't set it explicitly, it will be automatically allocated. |
|
315 | 317 |
template <class T> |
316 |
|
|
318 |
struct DefProcessedMapToBeDefaultMap : |
|
317 | 319 |
public Dfs< Digraph, DefDigraphProcessedMapTraits> { |
318 | 320 |
typedef Dfs< Digraph, DefDigraphProcessedMapTraits> Create; |
319 | 321 |
}; |
320 | 322 |
|
321 | 323 |
///@} |
322 | 324 |
|
323 | 325 |
public: |
324 | 326 |
|
325 | 327 |
///Constructor. |
326 | 328 |
|
327 |
///\param _G the digraph the algorithm will run on. |
|
328 |
/// |
|
329 |
Dfs(const Digraph& _G) : |
|
330 |
G(&_G), |
|
329 |
///Constructor. |
|
330 |
///\param g The digraph the algorithm runs on. |
|
331 |
Dfs(const Digraph &g) : |
|
332 |
G(&g), |
|
331 | 333 |
_pred(NULL), local_pred(false), |
332 | 334 |
_dist(NULL), local_dist(false), |
333 | 335 |
_reached(NULL), local_reached(false), |
334 | 336 |
_processed(NULL), local_processed(false) |
335 | 337 |
{ } |
336 | 338 |
|
... | ... |
@@ -340,90 +342,92 @@ |
340 | 342 |
if(local_pred) delete _pred; |
341 | 343 |
if(local_dist) delete _dist; |
342 | 344 |
if(local_reached) delete _reached; |
343 | 345 |
if(local_processed) delete _processed; |
344 | 346 |
} |
345 | 347 |
|
346 |
///Sets the map |
|
348 |
///Sets the map that stores the predecessor arcs. |
|
347 | 349 |
|
348 |
///Sets the map |
|
350 |
///Sets the map that stores the predecessor arcs. |
|
349 | 351 |
///If you don't use this function before calling \ref run(), |
350 |
///it will allocate one. The |
|
352 |
///it will allocate one. The destructor deallocates this |
|
351 | 353 |
///automatically allocated map, of course. |
352 | 354 |
///\return <tt> (*this) </tt> |
353 | 355 |
Dfs &predMap(PredMap &m) |
354 | 356 |
{ |
355 | 357 |
if(local_pred) { |
356 | 358 |
delete _pred; |
357 | 359 |
local_pred=false; |
358 | 360 |
} |
359 | 361 |
_pred = &m; |
360 | 362 |
return *this; |
361 | 363 |
} |
362 | 364 |
|
363 |
///Sets the map |
|
365 |
///Sets the map that indicates which nodes are reached. |
|
364 | 366 |
|
365 |
///Sets the map |
|
367 |
///Sets the map that indicates which nodes are reached. |
|
366 | 368 |
///If you don't use this function before calling \ref run(), |
367 |
///it will allocate one. The |
|
369 |
///it will allocate one. The destructor deallocates this |
|
370 |
///automatically allocated map, of course. |
|
371 |
///\return <tt> (*this) </tt> |
|
372 |
Dfs &reachedMap(ReachedMap &m) |
|
373 |
{ |
|
374 |
if(local_reached) { |
|
375 |
delete _reached; |
|
376 |
local_reached=false; |
|
377 |
} |
|
378 |
_reached = &m; |
|
379 |
return *this; |
|
380 |
} |
|
381 |
|
|
382 |
///Sets the map that indicates which nodes are processed. |
|
383 |
|
|
384 |
///Sets the map that indicates which nodes are processed. |
|
385 |
///If you don't use this function before calling \ref run(), |
|
386 |
///it will allocate one. The destructor deallocates this |
|
387 |
///automatically allocated map, of course. |
|
388 |
///\return <tt> (*this) </tt> |
|
389 |
Dfs &processedMap(ProcessedMap &m) |
|
390 |
{ |
|
391 |
if(local_processed) { |
|
392 |
delete _processed; |
|
393 |
local_processed=false; |
|
394 |
} |
|
395 |
_processed = &m; |
|
396 |
return *this; |
|
397 |
} |
|
398 |
|
|
399 |
///Sets the map that stores the distances of the nodes. |
|
400 |
|
|
401 |
///Sets the map that stores the distances of the nodes calculated by |
|
402 |
///the algorithm. |
|
403 |
///If you don't use this function before calling \ref run(), |
|
404 |
///it will allocate one. The destructor deallocates this |
|
368 | 405 |
///automatically allocated map, of course. |
369 | 406 |
///\return <tt> (*this) </tt> |
370 | 407 |
Dfs &distMap(DistMap &m) |
371 | 408 |
{ |
372 | 409 |
if(local_dist) { |
373 | 410 |
delete _dist; |
374 | 411 |
local_dist=false; |
375 | 412 |
} |
376 | 413 |
_dist = &m; |
377 | 414 |
return *this; |
378 | 415 |
} |
379 | 416 |
|
380 |
|
|
417 |
public: |
|
381 | 418 |
|
382 |
///Sets the map indicating if a node is reached. |
|
383 |
///If you don't use this function before calling \ref run(), |
|
384 |
///it will allocate one. The destuctor deallocates this |
|
385 |
///automatically allocated map, of course. |
|
386 |
///\return <tt> (*this) </tt> |
|
387 |
Dfs &reachedMap(ReachedMap &m) |
|
388 |
{ |
|
389 |
if(local_reached) { |
|
390 |
delete _reached; |
|
391 |
local_reached=false; |
|
392 |
} |
|
393 |
_reached = &m; |
|
394 |
return *this; |
|
395 |
} |
|
396 |
|
|
397 |
///Sets the map indicating if a node is processed. |
|
398 |
|
|
399 |
///Sets the map indicating if a node is processed. |
|
400 |
///If you don't use this function before calling \ref run(), |
|
401 |
///it will allocate one. The destuctor deallocates this |
|
402 |
///automatically allocated map, of course. |
|
403 |
///\return <tt> (*this) </tt> |
|
404 |
Dfs &processedMap(ProcessedMap &m) |
|
405 |
{ |
|
406 |
if(local_processed) { |
|
407 |
delete _processed; |
|
408 |
local_processed=false; |
|
409 |
} |
|
410 |
_processed = &m; |
|
411 |
return *this; |
|
412 |
} |
|
413 |
|
|
414 |
public: |
|
415 | 419 |
///\name Execution control |
416 | 420 |
///The simplest way to execute the algorithm is to use |
417 |
///one of the member functions called \ |
|
421 |
///one of the member functions called \ref lemon::Dfs::run() "run()". |
|
418 | 422 |
///\n |
419 |
///If you need more control on the execution, |
|
420 |
///first you must call \ref init(), then you can add a source node |
|
421 |
///with \ref addSource(). |
|
422 |
///Finally \ref start() will perform the actual path |
|
423 |
/// |
|
423 |
///If you need more control on the execution, first you must call |
|
424 |
///\ref lemon::Dfs::init() "init()", then you can add a source node |
|
425 |
///with \ref lemon::Dfs::addSource() "addSource()". |
|
426 |
///Finally \ref lemon::Dfs::start() "start()" will perform the |
|
427 |
///actual path computation. |
|
424 | 428 |
|
425 | 429 |
///@{ |
426 | 430 |
|
427 | 431 |
///Initializes the internal data structures. |
428 | 432 |
|
429 | 433 |
///Initializes the internal data structures. |
... | ... |
@@ -432,26 +436,29 @@ |
432 | 436 |
{ |
433 | 437 |
create_maps(); |
434 | 438 |
_stack.resize(countNodes(*G)); |
435 | 439 |
_stack_head=-1; |
436 | 440 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
437 | 441 |
_pred->set(u,INVALID); |
438 |
// _predNode->set(u,INVALID); |
|
439 | 442 |
_reached->set(u,false); |
440 | 443 |
_processed->set(u,false); |
441 | 444 |
} |
442 | 445 |
} |
443 | 446 |
|
444 | 447 |
///Adds a new source node. |
445 | 448 |
|
446 | 449 |
///Adds a new source node to the set of nodes to be processed. |
447 | 450 |
/// |
448 |
///\warning dists are wrong (or at least strange) |
|
449 |
///in case of multiple sources. |
|
451 |
///\pre The stack must be empty. (Otherwise the algorithm gives |
|
452 |
///false results.) |
|
453 |
/// |
|
454 |
///\warning Distances will be wrong (or at least strange) in case of |
|
455 |
///multiple sources. |
|
450 | 456 |
void addSource(Node s) |
451 | 457 |
{ |
458 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
|
452 | 459 |
if(!(*_reached)[s]) |
453 | 460 |
{ |
454 | 461 |
_reached->set(s,true); |
455 | 462 |
_pred->set(s,INVALID); |
456 | 463 |
OutArcIt e(*G,s); |
457 | 464 |
if(e!=INVALID) { |
... | ... |
@@ -468,13 +475,13 @@ |
468 | 475 |
///Processes the next arc. |
469 | 476 |
|
470 | 477 |
///Processes the next arc. |
471 | 478 |
/// |
472 | 479 |
///\return The processed arc. |
473 | 480 |
/// |
474 |
///\pre The stack must not be empty |
|
481 |
///\pre The stack must not be empty. |
|
475 | 482 |
Arc processNextArc() |
476 | 483 |
{ |
477 | 484 |
Node m; |
478 | 485 |
Arc e=_stack[_stack_head]; |
479 | 486 |
if(!(*_reached)[m=G->target(e)]) { |
480 | 487 |
_pred->set(m,e); |
... | ... |
@@ -494,134 +501,116 @@ |
494 | 501 |
m=G->source(_stack[_stack_head]); |
495 | 502 |
++_stack[_stack_head]; |
496 | 503 |
} |
497 | 504 |
} |
498 | 505 |
return e; |
499 | 506 |
} |
507 |
|
|
500 | 508 |
///Next arc to be processed. |
501 | 509 |
|
502 | 510 |
///Next arc to be processed. |
503 | 511 |
/// |
504 |
///\return The next arc to be processed or INVALID if the stack is |
|
505 |
/// empty. |
|
506 |
|
|
512 |
///\return The next arc to be processed or \c INVALID if the stack |
|
513 |
///is empty. |
|
514 |
OutArcIt nextArc() const |
|
507 | 515 |
{ |
508 | 516 |
return _stack_head>=0?_stack[_stack_head]:INVALID; |
509 | 517 |
} |
510 | 518 |
|
511 | 519 |
///\brief Returns \c false if there are nodes |
512 |
///to be processed |
|
520 |
///to be processed. |
|
513 | 521 |
/// |
514 | 522 |
///Returns \c false if there are nodes |
515 |
///to be processed in the queue |
|
516 |
bool emptyQueue() { return _stack_head<0; } |
|
523 |
///to be processed in the queue (stack). |
|
524 |
bool emptyQueue() const { return _stack_head<0; } |
|
525 |
|
|
517 | 526 |
///Returns the number of the nodes to be processed. |
518 | 527 |
|
519 |
///Returns the number of the nodes to be processed in the queue. |
|
520 |
int queueSize() { return _stack_head+1; } |
|
528 |
///Returns the number of the nodes to be processed in the queue (stack). |
|
529 |
int queueSize() const { return _stack_head+1; } |
|
521 | 530 |
|
522 | 531 |
///Executes the algorithm. |
523 | 532 |
|
524 | 533 |
///Executes the algorithm. |
525 | 534 |
/// |
526 |
///\pre init() must be called and at least one node should be added |
|
527 |
///with addSource() before using this function. |
|
535 |
///This method runs the %DFS algorithm from the root node |
|
536 |
///in order to compute the DFS path to each node. |
|
528 | 537 |
/// |
529 |
///This method runs the %DFS algorithm from the root node(s) |
|
530 |
///in order to |
|
531 |
///compute the |
|
532 |
///%DFS path to each node. The algorithm computes |
|
533 |
///- The %DFS tree. |
|
534 |
///- The distance of each node from the root(s) in the %DFS tree. |
|
538 |
/// The algorithm computes |
|
539 |
///- the %DFS tree, |
|
540 |
///- the distance of each node from the root in the %DFS tree. |
|
535 | 541 |
/// |
542 |
///\pre init() must be called and a root node should be |
|
543 |
///added with addSource() before using this function. |
|
544 |
/// |
|
545 |
///\note <tt>d.start()</tt> is just a shortcut of the following code. |
|
546 |
///\code |
|
547 |
/// while ( !d.emptyQueue() ) { |
|
548 |
/// d.processNextArc(); |
|
549 |
/// } |
|
550 |
///\endcode |
|
536 | 551 |
void start() |
537 | 552 |
{ |
538 | 553 |
while ( !emptyQueue() ) processNextArc(); |
539 | 554 |
} |
540 | 555 |
|
541 |
///Executes the algorithm until |
|
556 |
///Executes the algorithm until the given target node is reached. |
|
542 | 557 |
|
543 |
///Executes the algorithm until |
|
558 |
///Executes the algorithm until the given target node is reached. |
|
544 | 559 |
/// |
545 |
///\pre init() must be called and at least one node should be added |
|
546 |
///with addSource() before using this function. |
|
560 |
///This method runs the %DFS algorithm from the root node |
|
561 |
///in order to compute the DFS path to \c dest. |
|
547 | 562 |
/// |
548 |
///This method runs the %DFS algorithm from the root node(s) |
|
549 |
///in order to |
|
550 |
///compute the |
|
551 |
///%DFS path to \c dest. The algorithm computes |
|
552 |
///- The %DFS path to \c dest. |
|
553 |
///- The distance of \c dest from the root(s) in the %DFS tree. |
|
563 |
///The algorithm computes |
|
564 |
///- the %DFS path to \c dest, |
|
565 |
///- the distance of \c dest from the root in the %DFS tree. |
|
554 | 566 |
/// |
567 |
///\pre init() must be called and a root node should be |
|
568 |
///added with addSource() before using this function. |
|
555 | 569 |
void start(Node dest) |
556 | 570 |
{ |
557 | 571 |
while ( !emptyQueue() && G->target(_stack[_stack_head])!=dest ) |
558 | 572 |
processNextArc(); |
559 | 573 |
} |
560 | 574 |
|
561 | 575 |
///Executes the algorithm until a condition is met. |
562 | 576 |
|
563 | 577 |
///Executes the algorithm until a condition is met. |
564 | 578 |
/// |
565 |
///\pre init() must be called and at least one node should be added |
|
566 |
///with addSource() before using this function. |
|
579 |
///This method runs the %DFS algorithm from the root node |
|
580 |
///until an arc \c a with <tt>am[a]</tt> true is found. |
|
567 | 581 |
/// |
568 |
///\param em must be a bool (or convertible) arc map. The algorithm |
|
569 |
///will stop when it reaches an arc \c e with <tt>em[e]</tt> true. |
|
582 |
///\param am A \c bool (or convertible) arc map. The algorithm |
|
583 |
///will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
|
570 | 584 |
/// |
571 |
///\return The reached arc \c |
|
585 |
///\return The reached arc \c a with <tt>am[a]</tt> true or |
|
572 | 586 |
///\c INVALID if no such arc was found. |
573 | 587 |
/// |
574 |
///\ |
|
588 |
///\pre init() must be called and a root node should be |
|
589 |
///added with addSource() before using this function. |
|
590 |
/// |
|
591 |
///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
|
575 | 592 |
///not a node map. |
576 |
template<class EM> |
|
577 |
Arc start(const EM &em) |
|
593 |
template<class ArcBoolMap> |
|
594 |
Arc start(const ArcBoolMap &am) |
|
578 | 595 |
{ |
579 |
while ( !emptyQueue() && ! |
|
596 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
|
580 | 597 |
processNextArc(); |
581 | 598 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
582 | 599 |
} |
583 | 600 |
|
584 |
///Runs |
|
601 |
///Runs the algorithm from the given node. |
|
585 | 602 |
|
586 |
///This method runs the %DFS algorithm in order to |
|
587 |
///compute the |
|
588 |
///%DFS path to each node. The algorithm computes |
|
589 |
///- The %DFS tree. |
|
590 |
/// |
|
603 |
///This method runs the %DFS algorithm from node \c s |
|
604 |
///in order to compute the DFS path to each node. |
|
591 | 605 |
/// |
592 |
///\note d.run() is just a shortcut of the following code. |
|
593 |
///\code |
|
594 |
/// d.init(); |
|
595 |
/// for (NodeIt it(digraph); it != INVALID; ++it) { |
|
596 |
/// if (!d.reached(it)) { |
|
597 |
/// d.addSource(it); |
|
598 |
/// d.start(); |
|
599 |
/// } |
|
600 |
/// } |
|
601 |
///\endcode |
|
602 |
void run() { |
|
603 |
init(); |
|
604 |
for (NodeIt it(*G); it != INVALID; ++it) { |
|
605 |
if (!reached(it)) { |
|
606 |
addSource(it); |
|
607 |
start(); |
|
608 |
} |
|
609 |
} |
|
610 |
} |
|
611 |
|
|
612 |
///Runs %DFS algorithm from node \c s. |
|
613 |
|
|
614 |
///This method runs the %DFS algorithm from a root node \c s |
|
615 |
///in order to |
|
616 |
///compute the |
|
617 |
///%DFS path to each node. The algorithm computes |
|
618 |
///- The %DFS tree. |
|
619 |
///- The distance of each node from the root in the %DFS tree. |
|
606 |
///The algorithm computes |
|
607 |
///- the %DFS tree, |
|
608 |
///- the distance of each node from the root in the %DFS tree. |
|
620 | 609 |
/// |
621 |
///\note d.run(s) is just a shortcut of the following code. |
|
610 |
///\note <tt>d.run(s)</tt> is just a shortcut of the following code. |
|
622 | 611 |
///\code |
623 | 612 |
/// d.init(); |
624 | 613 |
/// d.addSource(s); |
625 | 614 |
/// d.start(); |
626 | 615 |
///\endcode |
627 | 616 |
void run(Node s) { |
... | ... |
@@ -629,17 +618,19 @@ |
629 | 618 |
addSource(s); |
630 | 619 |
start(); |
631 | 620 |
} |
632 | 621 |
|
633 | 622 |
///Finds the %DFS path between \c s and \c t. |
634 | 623 |
|
635 |
/// |
|
624 |
///This method runs the %DFS algorithm from node \c s |
|
625 |
///in order to compute the DFS path to \c t. |
|
636 | 626 |
/// |
637 |
///\return The length of the %DFS s---t path if there exists one, |
|
638 |
///0 otherwise. |
|
639 |
///\ |
|
627 |
///\return The length of the <tt>s</tt>--<tt>t</tt> DFS path, |
|
628 |
///if \c t is reachable form \c s, \c 0 otherwise. |
|
629 |
/// |
|
630 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is |
|
640 | 631 |
///just a shortcut of the following code. |
641 | 632 |
///\code |
642 | 633 |
/// d.init(); |
643 | 634 |
/// d.addSource(s); |
644 | 635 |
/// d.start(t); |
645 | 636 |
///\endcode |
... | ... |
@@ -647,183 +638,220 @@ |
647 | 638 |
init(); |
648 | 639 |
addSource(s); |
649 | 640 |
start(t); |
650 | 641 |
return reached(t)?_stack_head+1:0; |
651 | 642 |
} |
652 | 643 |
|
644 |
///Runs the algorithm to visit all nodes in the digraph. |
|
645 |
|
|
646 |
///This method runs the %DFS algorithm in order to compute the |
|
647 |
///%DFS path to each node. |
|
648 |
/// |
|
649 |
///The algorithm computes |
|
650 |
///- the %DFS tree, |
|
651 |
///- the distance of each node from the root in the %DFS tree. |
|
652 |
/// |
|
653 |
///\note <tt>d.run()</tt> is just a shortcut of the following code. |
|
654 |
///\code |
|
655 |
/// d.init(); |
|
656 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
|
657 |
/// if (!d.reached(n)) { |
|
658 |
/// d.addSource(n); |
|
659 |
/// d.start(); |
|
660 |
/// } |
|
661 |
/// } |
|
662 |
///\endcode |
|
663 |
void run() { |
|
664 |
init(); |
|
665 |
for (NodeIt it(*G); it != INVALID; ++it) { |
|
666 |
if (!reached(it)) { |
|
667 |
addSource(it); |
|
668 |
start(); |
|
669 |
} |
|
670 |
} |
|
671 |
} |
|
672 |
|
|
653 | 673 |
///@} |
654 | 674 |
|
655 | 675 |
///\name Query Functions |
656 | 676 |
///The result of the %DFS algorithm can be obtained using these |
657 | 677 |
///functions.\n |
658 |
///Before the use of these functions, |
|
659 |
///either run() or start() must be called. |
|
678 |
///Either \ref lemon::Dfs::run() "run()" or \ref lemon::Dfs::start() |
|
679 |
///"start()" must be called before using them. |
|
660 | 680 |
|
661 | 681 |
///@{ |
662 | 682 |
|
663 |
|
|
683 |
///The DFS path to a node. |
|
664 | 684 |
|
665 |
/// |
|
685 |
///Returns the DFS path to a node. |
|
686 |
/// |
|
687 |
///\warning \c t should be reachable from the root. |
|
688 |
/// |
|
689 |
///\pre Either \ref run() or \ref start() must be called before |
|
690 |
///using this function. |
|
691 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
|
666 | 692 |
|
667 |
///Gives back the shortest path. |
|
668 |
///\pre The \c t should be reachable from the source. |
|
669 |
Path path(Node t) |
|
670 |
{ |
|
671 |
return Path(*G, *_pred, t); |
|
672 |
} |
|
693 |
///The distance of a node from the root. |
|
673 | 694 |
|
674 |
///The distance of a node from the root(s). |
|
675 |
|
|
676 |
///Returns the distance of a node from the root(s). |
|
677 |
///\pre \ref run() must be called before using this function. |
|
678 |
///\warning If node \c v is unreachable from the root(s) then the return |
|
679 |
///value of this funcion is undefined. |
|
695 |
///Returns the distance of a node from the root. |
|
696 |
/// |
|
697 |
///\warning If node \c v is not reachable from the root, then |
|
698 |
///the return value of this function is undefined. |
|
699 |
/// |
|
700 |
///\pre Either \ref run() or \ref start() must be called before |
|
701 |
///using this function. |
|
680 | 702 |
int dist(Node v) const { return (*_dist)[v]; } |
681 | 703 |
|
682 |
///Returns the 'previous arc' of the %DFS tree. |
|
704 |
///Returns the 'previous arc' of the %DFS tree for a node. |
|
683 | 705 |
|
684 |
///For a node \c v it returns the 'previous arc' |
|
685 |
///of the %DFS path, |
|
686 |
///i.e. it returns the last arc of a %DFS path from the root(s) to \c |
|
687 |
///v. It is \ref INVALID |
|
688 |
///if \c v is unreachable from the root(s) or \c v is a root. The |
|
689 |
///%DFS tree used here is equal to the %DFS tree used in |
|
706 |
///This function returns the 'previous arc' of the %DFS tree for the |
|
707 |
///node \c v, i.e. it returns the last arc of a %DFS path from the |
|
708 |
///root to \c v. It is \c INVALID |
|
709 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
|
710 |
/// |
|
711 |
///The %DFS tree used here is equal to the %DFS tree used in |
|
690 | 712 |
///\ref predNode(). |
713 |
/// |
|
691 | 714 |
///\pre Either \ref run() or \ref start() must be called before using |
692 | 715 |
///this function. |
693 | 716 |
Arc predArc(Node v) const { return (*_pred)[v];} |
694 | 717 |
|
695 | 718 |
///Returns the 'previous node' of the %DFS tree. |
696 | 719 |
|
697 |
///For a node \c v it returns the 'previous node' |
|
698 |
///of the %DFS tree, |
|
699 |
///i.e. it returns the last but one node from a %DFS path from the |
|
700 |
///root(s) to \c v. |
|
701 |
///It is INVALID if \c v is unreachable from the root(s) or |
|
702 |
///if \c v itself a root. |
|
703 |
///The %DFS tree used here is equal to the %DFS |
|
704 |
///tree used in \ref predArc(). |
|
720 |
///This function returns the 'previous node' of the %DFS |
|
721 |
///tree for the node \c v, i.e. it returns the last but one node |
|
722 |
///from a %DFS path from the root to \c v. It is \c INVALID |
|
723 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
|
724 |
/// |
|
725 |
///The %DFS tree used here is equal to the %DFS tree used in |
|
726 |
///\ref predArc(). |
|
727 |
/// |
|
705 | 728 |
///\pre Either \ref run() or \ref start() must be called before |
706 | 729 |
///using this function. |
707 | 730 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
708 | 731 |
G->source((*_pred)[v]); } |
709 | 732 |
|
710 |
///Returns a reference to the NodeMap of distances. |
|
711 |
|
|
712 |
///Returns a reference to the NodeMap of distances. |
|
713 |
///\pre Either \ref run() or \ref init() must |
|
714 |
/// |
|
733 |
///\brief Returns a const reference to the node map that stores the |
|
734 |
///distances of the nodes. |
|
735 |
/// |
|
736 |
///Returns a const reference to the node map that stores the |
|
737 |
///distances of the nodes calculated by the algorithm. |
|
738 |
/// |
|
739 |
///\pre Either \ref run() or \ref init() |
|
740 |
///must be called before using this function. |
|
715 | 741 |
const DistMap &distMap() const { return *_dist;} |
716 | 742 |
|
717 |
///Returns a reference to the %DFS arc-tree map. |
|
718 |
|
|
719 |
///Returns a reference to the NodeMap of the arcs of the |
|
720 |
///%DFS tree. |
|
743 |
///\brief Returns a const reference to the node map that stores the |
|
744 |
///predecessor arcs. |
|
745 |
/// |
|
746 |
///Returns a const reference to the node map that stores the predecessor |
|
747 |
///arcs, which form the DFS tree. |
|
748 |
/// |
|
721 | 749 |
///\pre Either \ref run() or \ref init() |
722 | 750 |
///must be called before using this function. |
723 | 751 |
const PredMap &predMap() const { return *_pred;} |
724 | 752 |
|
725 |
///Checks if a node is reachable from the root. |
|
753 |
///Checks if a node is reachable from the root(s). |
|
726 | 754 |
|
727 | 755 |
///Returns \c true if \c v is reachable from the root(s). |
728 |
///\warning The source nodes are inditated as unreachable. |
|
729 | 756 |
///\pre Either \ref run() or \ref start() |
730 | 757 |
///must be called before using this function. |
731 |
/// |
|
732 |
bool reached(Node v) { return (*_reached)[v]; } |
|
758 |
bool reached(Node v) const { return (*_reached)[v]; } |
|
733 | 759 |
|
734 | 760 |
///@} |
735 | 761 |
}; |
736 | 762 |
|
737 |
///Default traits class of |
|
763 |
///Default traits class of dfs() function. |
|
738 | 764 |
|
739 |
///Default traits class of |
|
765 |
///Default traits class of dfs() function. |
|
740 | 766 |
///\tparam GR Digraph type. |
741 | 767 |
template<class GR> |
742 | 768 |
struct DfsWizardDefaultTraits |
743 | 769 |
{ |
744 |
///The |
|
770 |
///The type of the digraph the algorithm runs on. |
|
745 | 771 |
typedef GR Digraph; |
746 |
|
|
772 |
|
|
773 |
///\brief The type of the map that stores the predecessor |
|
747 | 774 |
///arcs of the %DFS paths. |
748 | 775 |
/// |
749 |
///The type of the map that stores the |
|
776 |
///The type of the map that stores the predecessor |
|
750 | 777 |
///arcs of the %DFS paths. |
751 | 778 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
752 | 779 |
/// |
753 |
typedef NullMap<typename Digraph::Node,typename GR::Arc> PredMap; |
|
754 |
///Instantiates a PredMap. |
|
780 |
typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap; |
|
781 |
///Instantiates a \ref PredMap. |
|
755 | 782 |
|
756 | 783 |
///This function instantiates a \ref PredMap. |
757 |
///\param g is the digraph, to which we would like to define the |
|
784 |
///\param g is the digraph, to which we would like to define the |
|
785 |
///\ref PredMap. |
|
758 | 786 |
///\todo The digraph alone may be insufficient to initialize |
759 | 787 |
#ifdef DOXYGEN |
760 |
static PredMap *createPredMap(const |
|
788 |
static PredMap *createPredMap(const Digraph &g) |
|
761 | 789 |
#else |
762 |
static PredMap *createPredMap(const |
|
790 |
static PredMap *createPredMap(const Digraph &) |
|
763 | 791 |
#endif |
764 | 792 |
{ |
765 | 793 |
return new PredMap(); |
766 | 794 |
} |
767 | 795 |
|
768 | 796 |
///The type of the map that indicates which nodes are processed. |
769 | 797 |
|
770 | 798 |
///The type of the map that indicates which nodes are processed. |
771 | 799 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
772 |
///\todo named parameter to set this type, function to read and write. |
|
773 | 800 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
774 |
///Instantiates a ProcessedMap. |
|
801 |
///Instantiates a \ref ProcessedMap. |
|
775 | 802 |
|
776 | 803 |
///This function instantiates a \ref ProcessedMap. |
777 | 804 |
///\param g is the digraph, to which |
778 |
///we would like to define the \ref ProcessedMap |
|
805 |
///we would like to define the \ref ProcessedMap. |
|
779 | 806 |
#ifdef DOXYGEN |
780 |
static ProcessedMap *createProcessedMap(const |
|
807 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
|
781 | 808 |
#else |
782 |
static ProcessedMap *createProcessedMap(const |
|
809 |
static ProcessedMap *createProcessedMap(const Digraph &) |
|
783 | 810 |
#endif |
784 | 811 |
{ |
785 | 812 |
return new ProcessedMap(); |
786 | 813 |
} |
814 |
|
|
787 | 815 |
///The type of the map that indicates which nodes are reached. |
788 | 816 |
|
789 | 817 |
///The type of the map that indicates which nodes are reached. |
790 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
|
791 |
///\todo named parameter to set this type, function to read and write. |
|
818 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
792 | 819 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
793 |
///Instantiates a ReachedMap. |
|
820 |
///Instantiates a \ref ReachedMap. |
|
794 | 821 |
|
795 | 822 |
///This function instantiates a \ref ReachedMap. |
796 |
///\param |
|
823 |
///\param g is the digraph, to which |
|
797 | 824 |
///we would like to define the \ref ReachedMap. |
798 |
static ReachedMap *createReachedMap(const |
|
825 |
static ReachedMap *createReachedMap(const Digraph &g) |
|
799 | 826 |
{ |
800 |
return new ReachedMap( |
|
827 |
return new ReachedMap(g); |
|
801 | 828 |
} |
802 |
///The type of the map that stores the dists of the nodes. |
|
803 | 829 |
|
804 |
///The type of the map that stores the |
|
830 |
///The type of the map that stores the distances of the nodes. |
|
831 |
|
|
832 |
///The type of the map that stores the distances of the nodes. |
|
805 | 833 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
806 | 834 |
/// |
807 | 835 |
typedef NullMap<typename Digraph::Node,int> DistMap; |
808 |
///Instantiates a DistMap. |
|
836 |
///Instantiates a \ref DistMap. |
|
809 | 837 |
|
810 | 838 |
///This function instantiates a \ref DistMap. |
811 | 839 |
///\param g is the digraph, to which we would like to define |
812 | 840 |
///the \ref DistMap |
813 | 841 |
#ifdef DOXYGEN |
814 |
static DistMap *createDistMap(const |
|
842 |
static DistMap *createDistMap(const Digraph &g) |
|
815 | 843 |
#else |
816 |
static DistMap *createDistMap(const |
|
844 |
static DistMap *createDistMap(const Digraph &) |
|
817 | 845 |
#endif |
818 | 846 |
{ |
819 | 847 |
return new DistMap(); |
820 | 848 |
} |
821 | 849 |
}; |
822 | 850 |
|
823 |
/// Default traits used by \ref DfsWizard |
|
851 |
/// Default traits class used by \ref DfsWizard |
|
824 | 852 |
|
825 | 853 |
/// To make it easier to use Dfs algorithm |
826 | 854 |
///we have created a wizard class. |
827 | 855 |
/// This \ref DfsWizard class needs default traits, |
828 | 856 |
///as well as the \ref Dfs class. |
829 | 857 |
/// The \ref DfsWizardBase is a class to be the default traits of the |
... | ... |
@@ -831,26 +859,26 @@ |
831 | 859 |
template<class GR> |
832 | 860 |
class DfsWizardBase : public DfsWizardDefaultTraits<GR> |
833 | 861 |
{ |
834 | 862 |
|
835 | 863 |
typedef DfsWizardDefaultTraits<GR> Base; |
836 | 864 |
protected: |
837 |
// |
|
865 |
//The type of the nodes in the digraph. |
|
838 | 866 |
typedef typename Base::Digraph::Node Node; |
839 | 867 |
|
840 |
// |
|
868 |
//Pointer to the digraph the algorithm runs on. |
|
841 | 869 |
void *_g; |
842 |
|
|
870 |
//Pointer to the map of reached nodes. |
|
843 | 871 |
void *_reached; |
844 |
|
|
872 |
//Pointer to the map of processed nodes. |
|
845 | 873 |
void *_processed; |
846 |
|
|
874 |
//Pointer to the map of predecessors arcs. |
|
847 | 875 |
void *_pred; |
848 |
|
|
876 |
//Pointer to the map of distances. |
|
849 | 877 |
void *_dist; |
850 |
|
|
878 |
//Pointer to the source node. |
|
851 | 879 |
Node _source; |
852 | 880 |
|
853 | 881 |
public: |
854 | 882 |
/// Constructor. |
855 | 883 |
|
856 | 884 |
/// This constructor does not require parameters, therefore it initiates |
... | ... |
@@ -860,68 +888,66 @@ |
860 | 888 |
|
861 | 889 |
/// Constructor. |
862 | 890 |
|
863 | 891 |
/// This constructor requires some parameters, |
864 | 892 |
/// listed in the parameters list. |
865 | 893 |
/// Others are initiated to 0. |
866 |
/// \param g is the initial value of \ref _g |
|
867 |
/// \param s is the initial value of \ref _source |
|
894 |
/// \param g The digraph the algorithm runs on. |
|
895 |
/// \param s The source node. |
|
868 | 896 |
DfsWizardBase(const GR &g, Node s=INVALID) : |
869 | 897 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
870 | 898 |
_reached(0), _processed(0), _pred(0), _dist(0), _source(s) {} |
871 | 899 |
|
872 | 900 |
}; |
873 | 901 |
|
874 |
/// |
|
902 |
/// Auxiliary class for the function type interface of DFS algorithm. |
|
875 | 903 |
|
876 |
/// This class is created to make it easier to use the Dfs algorithm. |
|
877 |
/// It uses the functions and features of the plain \ref Dfs, |
|
878 |
/// |
|
904 |
/// This auxiliary class is created to implement the function type |
|
905 |
/// interface of \ref Dfs algorithm. It uses the functions and features |
|
906 |
/// of the plain \ref Dfs, but it is much simpler to use it. |
|
907 |
/// It should only be used through the \ref dfs() function, which makes |
|
908 |
/// it easier to use the algorithm. |
|
879 | 909 |
/// |
880 | 910 |
/// Simplicity means that the way to change the types defined |
881 | 911 |
/// in the traits class is based on functions that returns the new class |
882 | 912 |
/// and not on templatable built-in classes. |
883 | 913 |
/// When using the plain \ref Dfs |
884 | 914 |
/// the new class with the modified type comes from |
885 | 915 |
/// the original class by using the :: |
886 | 916 |
/// operator. In the case of \ref DfsWizard only |
887 |
/// a function have to be called and it will |
|
917 |
/// a function have to be called, and it will |
|
888 | 918 |
/// return the needed class. |
889 | 919 |
/// |
890 |
/// It does not have own \ref run method. When its \ref run method is called |
|
891 |
/// it initiates a plain \ref Dfs object, and calls the \ref Dfs::run |
|
892 |
/// |
|
920 |
/// It does not have own \ref run() method. When its \ref run() method |
|
921 |
/// is called, it initiates a plain \ref Dfs object, and calls the |
|
922 |
/// \ref Dfs::run() method of it. |
|
893 | 923 |
template<class TR> |
894 | 924 |
class DfsWizard : public TR |
895 | 925 |
{ |
896 | 926 |
typedef TR Base; |
897 | 927 |
|
898 |
///The type of the |
|
928 |
///The type of the digraph the algorithm runs on. |
|
899 | 929 |
typedef typename TR::Digraph Digraph; |
900 |
|
|
930 |
|
|
901 | 931 |
typedef typename Digraph::Node Node; |
902 |
//\e |
|
903 | 932 |
typedef typename Digraph::NodeIt NodeIt; |
904 |
//\e |
|
905 | 933 |
typedef typename Digraph::Arc Arc; |
906 |
//\e |
|
907 | 934 |
typedef typename Digraph::OutArcIt OutArcIt; |
908 | 935 |
|
909 |
///\brief The type of the map that stores |
|
910 |
///the reached nodes |
|
936 |
///\brief The type of the map that stores the predecessor |
|
937 |
///arcs of the shortest paths. |
|
938 |
typedef typename TR::PredMap PredMap; |
|
939 |
///\brief The type of the map that stores the distances of the nodes. |
|
940 |
typedef typename TR::DistMap DistMap; |
|
941 |
///\brief The type of the map that indicates which nodes are reached. |
|
911 | 942 |
typedef typename TR::ReachedMap ReachedMap; |
912 |
///\brief The type of the map that stores |
|
913 |
///the processed nodes |
|
943 |
///\brief The type of the map that indicates which nodes are processed. |
|
914 | 944 |
typedef typename TR::ProcessedMap ProcessedMap; |
915 |
///\brief The type of the map that stores the last |
|
916 |
///arcs of the %DFS paths. |
|
917 |
typedef typename TR::PredMap PredMap; |
|
918 |
///The type of the map that stores the distances of the nodes. |
|
919 |
typedef typename TR::DistMap DistMap; |
|
920 | 945 |
|
921 | 946 |
public: |
947 |
|
|
922 | 948 |
/// Constructor. |
923 | 949 |
DfsWizard() : TR() {} |
924 | 950 |
|
925 | 951 |
/// Constructor that requires parameters. |
926 | 952 |
|
927 | 953 |
/// Constructor that requires parameters. |
... | ... |
@@ -931,16 +957,16 @@ |
931 | 957 |
|
932 | 958 |
///Copy constructor |
933 | 959 |
DfsWizard(const TR &b) : TR(b) {} |
934 | 960 |
|
935 | 961 |
~DfsWizard() {} |
936 | 962 |
|
937 |
///Runs |
|
963 |
///Runs DFS algorithm from a source node. |
|
938 | 964 |
|
939 |
///Runs Dfs algorithm from a given node. |
|
940 |
///The node can be given by the \ref source function. |
|
965 |
///Runs DFS algorithm from a source node. |
|
966 |
///The node can be given with the \ref source() function. |
|
941 | 967 |
void run() |
942 | 968 |
{ |
943 | 969 |
if(Base::_source==INVALID) throw UninitializedParameter(); |
944 | 970 |
Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g)); |
945 | 971 |
if(Base::_reached) |
946 | 972 |
alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
... | ... |
@@ -950,77 +976,79 @@ |
950 | 976 |
alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
951 | 977 |
if(Base::_dist) |
952 | 978 |
alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
953 | 979 |
alg.run(Base::_source); |
954 | 980 |
} |
955 | 981 |
|
956 |
///Runs |
|
982 |
///Runs DFS algorithm from the given node. |
|
957 | 983 |
|
958 |
///Runs |
|
984 |
///Runs DFS algorithm from the given node. |
|
959 | 985 |
///\param s is the given source. |
960 | 986 |
void run(Node s) |
961 | 987 |
{ |
962 | 988 |
Base::_source=s; |
963 | 989 |
run(); |
964 | 990 |
} |
965 | 991 |
|
992 |
/// Sets the source node, from which the Dfs algorithm runs. |
|
993 |
|
|
994 |
/// Sets the source node, from which the Dfs algorithm runs. |
|
995 |
/// \param s is the source node. |
|
996 |
DfsWizard<TR> &source(Node s) |
|
997 |
{ |
|
998 |
Base::_source=s; |
|
999 |
return *this; |
|
1000 |
} |
|
1001 |
|
|
966 | 1002 |
template<class T> |
967 | 1003 |
struct DefPredMapBase : public Base { |
968 | 1004 |
typedef T PredMap; |
969 | 1005 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
970 | 1006 |
DefPredMapBase(const TR &b) : TR(b) {} |
971 | 1007 |
}; |
972 |
|
|
973 | 1008 |
///\brief \ref named-templ-param "Named parameter" |
974 |
/// |
|
1009 |
///for setting \ref PredMap object. |
|
975 | 1010 |
/// |
976 | 1011 |
/// \ref named-templ-param "Named parameter" |
977 |
///function for setting PredMap type |
|
978 |
/// |
|
1012 |
///for setting \ref PredMap object. |
|
979 | 1013 |
template<class T> |
980 | 1014 |
DfsWizard<DefPredMapBase<T> > predMap(const T &t) |
981 | 1015 |
{ |
982 | 1016 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
983 | 1017 |
return DfsWizard<DefPredMapBase<T> >(*this); |
984 | 1018 |
} |
985 | 1019 |
|
986 |
|
|
987 | 1020 |
template<class T> |
988 | 1021 |
struct DefReachedMapBase : public Base { |
989 | 1022 |
typedef T ReachedMap; |
990 | 1023 |
static ReachedMap *createReachedMap(const Digraph &) { return 0; }; |
991 | 1024 |
DefReachedMapBase(const TR &b) : TR(b) {} |
992 | 1025 |
}; |
993 |
|
|
994 | 1026 |
///\brief \ref named-templ-param "Named parameter" |
995 |
/// |
|
1027 |
///for setting \ref ReachedMap object. |
|
996 | 1028 |
/// |
997 | 1029 |
/// \ref named-templ-param "Named parameter" |
998 |
///function for setting ReachedMap |
|
999 |
/// |
|
1030 |
///for setting \ref ReachedMap object. |
|
1000 | 1031 |
template<class T> |
1001 | 1032 |
DfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) |
1002 | 1033 |
{ |
1003 | 1034 |
Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1004 | 1035 |
return DfsWizard<DefReachedMapBase<T> >(*this); |
1005 | 1036 |
} |
1006 | 1037 |
|
1007 |
|
|
1008 | 1038 |
template<class T> |
1009 | 1039 |
struct DefProcessedMapBase : public Base { |
1010 | 1040 |
typedef T ProcessedMap; |
1011 | 1041 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
1012 | 1042 |
DefProcessedMapBase(const TR &b) : TR(b) {} |
1013 | 1043 |
}; |
1014 |
|
|
1015 | 1044 |
///\brief \ref named-templ-param "Named parameter" |
1016 |
/// |
|
1045 |
///for setting \ref ProcessedMap object. |
|
1017 | 1046 |
/// |
1018 | 1047 |
/// \ref named-templ-param "Named parameter" |
1019 |
///function for setting ProcessedMap |
|
1020 |
/// |
|
1048 |
///for setting \ref ProcessedMap object. |
|
1021 | 1049 |
template<class T> |
1022 | 1050 |
DfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) |
1023 | 1051 |
{ |
1024 | 1052 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1025 | 1053 |
return DfsWizard<DefProcessedMapBase<T> >(*this); |
1026 | 1054 |
} |
... | ... |
@@ -1028,36 +1056,24 @@ |
1028 | 1056 |
template<class T> |
1029 | 1057 |
struct DefDistMapBase : public Base { |
1030 | 1058 |
typedef T DistMap; |
1031 | 1059 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1032 | 1060 |
DefDistMapBase(const TR &b) : TR(b) {} |
1033 | 1061 |
}; |
1034 |
|
|
1035 | 1062 |
///\brief \ref named-templ-param "Named parameter" |
1036 |
/// |
|
1063 |
///for setting \ref DistMap object. |
|
1037 | 1064 |
/// |
1038 | 1065 |
/// \ref named-templ-param "Named parameter" |
1039 |
///function for setting DistMap type |
|
1040 |
/// |
|
1066 |
///for setting \ref DistMap object. |
|
1041 | 1067 |
template<class T> |
1042 | 1068 |
DfsWizard<DefDistMapBase<T> > distMap(const T &t) |
1043 | 1069 |
{ |
1044 | 1070 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1045 | 1071 |
return DfsWizard<DefDistMapBase<T> >(*this); |
1046 | 1072 |
} |
1047 | 1073 |
|
1048 |
/// Sets the source node, from which the Dfs algorithm runs. |
|
1049 |
|
|
1050 |
/// Sets the source node, from which the Dfs algorithm runs. |
|
1051 |
/// \param s is the source node. |
|
1052 |
DfsWizard<TR> &source(Node s) |
|
1053 |
{ |
|
1054 |
Base::_source=s; |
|
1055 |
return *this; |
|
1056 |
} |
|
1057 |
|
|
1058 | 1074 |
}; |
1059 | 1075 |
|
1060 | 1076 |
///Function type interface for Dfs algorithm. |
1061 | 1077 |
|
1062 | 1078 |
///\ingroup search |
1063 | 1079 |
///Function type interface for Dfs algorithm. |
... | ... |
@@ -1079,139 +1095,136 @@ |
1079 | 1095 |
dfs(const GR &g,typename GR::Node s=INVALID) |
1080 | 1096 |
{ |
1081 | 1097 |
return DfsWizard<DfsWizardBase<GR> >(g,s); |
1082 | 1098 |
} |
1083 | 1099 |
|
1084 | 1100 |
#ifdef DOXYGEN |
1085 |
/// \brief Visitor class for |
|
1101 |
/// \brief Visitor class for DFS. |
|
1086 | 1102 |
/// |
1087 |
/// It gives a simple interface for a functional interface for dfs |
|
1088 |
/// traversal. The traversal on a linear data structure. |
|
1103 |
/// This class defines the interface of the DfsVisit events, and |
|
1104 |
/// it could be the base of a real visitor class. |
|
1089 | 1105 |
template <typename _Digraph> |
1090 | 1106 |
struct DfsVisitor { |
1091 | 1107 |
typedef _Digraph Digraph; |
1092 | 1108 |
typedef typename Digraph::Arc Arc; |
1093 | 1109 |
typedef typename Digraph::Node Node; |
1094 |
/// \brief Called |
|
1110 |
/// \brief Called for the source node of the DFS. |
|
1095 | 1111 |
/// |
1096 |
/// It is called when the dfs find an arc which target is not |
|
1097 |
/// reached yet. |
|
1112 |
/// This function is called for the source node of the DFS. |
|
1113 |
void start(const Node& node) {} |
|
1114 |
/// \brief Called when the source node is leaved. |
|
1115 |
/// |
|
1116 |
/// This function is called when the source node is leaved. |
|
1117 |
void stop(const Node& node) {} |
|
1118 |
/// \brief Called when a node is reached first time. |
|
1119 |
/// |
|
1120 |
/// This function is called when a node is reached first time. |
|
1121 |
void reach(const Node& node) {} |
|
1122 |
/// \brief Called when an arc reaches a new node. |
|
1123 |
/// |
|
1124 |
/// This function is called when the DFS finds an arc whose target node |
|
1125 |
/// is not reached yet. |
|
1098 | 1126 |
void discover(const Arc& arc) {} |
1099 |
/// \brief Called when the node reached first time. |
|
1100 |
/// |
|
1101 |
/// It is Called when the node reached first time. |
|
1102 |
void reach(const Node& node) {} |
|
1103 |
/// \brief Called when we step back on an arc. |
|
1104 |
/// |
|
1105 |
/// It is called when the dfs should step back on the arc. |
|
1106 |
void backtrack(const Arc& arc) {} |
|
1107 |
/// \brief Called when we step back from the node. |
|
1108 |
/// |
|
1109 |
/// It is called when we step back from the node. |
|
1110 |
void leave(const Node& node) {} |
|
1111 |
/// \brief Called when |
|
1127 |
/// \brief Called when an arc is examined but its target node is |
|
1112 | 1128 |
/// already discovered. |
1113 | 1129 |
/// |
1114 |
/// |
|
1130 |
/// This function is called when an arc is examined but its target node is |
|
1115 | 1131 |
/// already discovered. |
1116 | 1132 |
void examine(const Arc& arc) {} |
1117 |
/// \brief Called |
|
1133 |
/// \brief Called when the DFS steps back from a node. |
|
1118 | 1134 |
/// |
1119 |
/// It is called for the source node of the dfs. |
|
1120 |
void start(const Node& node) {} |
|
1121 |
/// |
|
1135 |
/// This function is called when the DFS steps back from a node. |
|
1136 |
void leave(const Node& node) {} |
|
1137 |
/// \brief Called when the DFS steps back on an arc. |
|
1122 | 1138 |
/// |
1123 |
/// It is called when we leave the source node of the dfs. |
|
1124 |
void stop(const Node& node) {} |
|
1125 |
|
|
1139 |
/// This function is called when the DFS steps back on an arc. |
|
1140 |
void backtrack(const Arc& arc) {} |
|
1126 | 1141 |
}; |
1127 | 1142 |
#else |
1128 | 1143 |
template <typename _Digraph> |
1129 | 1144 |
struct DfsVisitor { |
1130 | 1145 |
typedef _Digraph Digraph; |
1131 | 1146 |
typedef typename Digraph::Arc Arc; |
1132 | 1147 |
typedef typename Digraph::Node Node; |
1133 |
void discover(const Arc&) {} |
|
1134 |
void reach(const Node&) {} |
|
1135 |
void backtrack(const Arc&) {} |
|
1136 |
void leave(const Node&) {} |
|
1137 |
void examine(const Arc&) {} |
|
1138 | 1148 |
void start(const Node&) {} |
1139 | 1149 |
void stop(const Node&) {} |
1150 |
void reach(const Node&) {} |
|
1151 |
void discover(const Arc&) {} |
|
1152 |
void examine(const Arc&) {} |
|
1153 |
void leave(const Node&) {} |
|
1154 |
void backtrack(const Arc&) {} |
|
1140 | 1155 |
|
1141 | 1156 |
template <typename _Visitor> |
1142 | 1157 |
struct Constraints { |
1143 | 1158 |
void constraints() { |
1144 | 1159 |
Arc arc; |
1145 | 1160 |
Node node; |
1146 |
visitor.discover(arc); |
|
1147 |
visitor.reach(node); |
|
1148 |
visitor.backtrack(arc); |
|
1149 |
visitor.leave(node); |
|
1150 |
visitor.examine(arc); |
|
1151 | 1161 |
visitor.start(node); |
1152 | 1162 |
visitor.stop(arc); |
1163 |
visitor.reach(node); |
|
1164 |
visitor.discover(arc); |
|
1165 |
visitor.examine(arc); |
|
1166 |
visitor.leave(node); |
|
1167 |
visitor.backtrack(arc); |
|
1153 | 1168 |
} |
1154 | 1169 |
_Visitor& visitor; |
1155 | 1170 |
}; |
1156 | 1171 |
}; |
1157 | 1172 |
#endif |
1158 | 1173 |
|
1159 | 1174 |
/// \brief Default traits class of DfsVisit class. |
1160 | 1175 |
/// |
1161 | 1176 |
/// Default traits class of DfsVisit class. |
1162 |
/// \tparam _Digraph |
|
1177 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
|
1163 | 1178 |
template<class _Digraph> |
1164 | 1179 |
struct DfsVisitDefaultTraits { |
1165 | 1180 |
|
1166 |
/// \brief The |
|
1181 |
/// \brief The type of the digraph the algorithm runs on. |
|
1167 | 1182 |
typedef _Digraph Digraph; |
1168 | 1183 |
|
1169 | 1184 |
/// \brief The type of the map that indicates which nodes are reached. |
1170 | 1185 |
/// |
1171 | 1186 |
/// The type of the map that indicates which nodes are reached. |
1172 |
/// It must meet the \ref concepts::WriteMap "WriteMap" concept. |
|
1173 |
/// \todo named parameter to set this type, function to read and write. |
|
1187 |
/// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
1174 | 1188 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
1175 | 1189 |
|
1176 |
/// \brief Instantiates a ReachedMap. |
|
1190 |
/// \brief Instantiates a \ref ReachedMap. |
|
1177 | 1191 |
/// |
1178 | 1192 |
/// This function instantiates a \ref ReachedMap. |
1179 | 1193 |
/// \param digraph is the digraph, to which |
1180 | 1194 |
/// we would like to define the \ref ReachedMap. |
1181 | 1195 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1182 | 1196 |
return new ReachedMap(digraph); |
1183 | 1197 |
} |
1184 | 1198 |
|
1185 | 1199 |
}; |
1186 | 1200 |
|
1187 |
/// %DFS Visit algorithm class. |
|
1188 |
|
|
1189 | 1201 |
/// \ingroup search |
1202 |
/// |
|
1203 |
/// \brief %DFS algorithm class with visitor interface. |
|
1204 |
/// |
|
1190 | 1205 |
/// This class provides an efficient implementation of the %DFS algorithm |
1191 | 1206 |
/// with visitor interface. |
1192 | 1207 |
/// |
1193 | 1208 |
/// The %DfsVisit class provides an alternative interface to the Dfs |
1194 | 1209 |
/// class. It works with callback mechanism, the DfsVisit object calls |
1195 |
/// |
|
1210 |
/// the member functions of the \c Visitor class on every DFS event. |
|
1196 | 1211 |
/// |
1197 |
/// \tparam _Digraph The |
|
1212 |
/// \tparam _Digraph The type of the digraph the algorithm runs on. |
|
1198 | 1213 |
/// The default value is |
1199 |
/// \ref ListDigraph. The value of _Digraph is not used directly by Dfs, it |
|
1200 |
/// is only passed to \ref DfsDefaultTraits. |
|
1201 |
/// \tparam _Visitor The Visitor object for the algorithm. The |
|
1202 |
/// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty Visitor which |
|
1203 |
/// does not observe the Dfs events. If you want to observe the dfs |
|
1204 |
/// events you should implement your own Visitor class. |
|
1214 |
/// \ref ListDigraph. The value of _Digraph is not used directly by |
|
1215 |
/// \ref DfsVisit, it is only passed to \ref DfsVisitDefaultTraits. |
|
1216 |
/// \tparam _Visitor The Visitor type that is used by the algorithm. |
|
1217 |
/// \ref DfsVisitor "DfsVisitor<_Digraph>" is an empty visitor, which |
|
1218 |
/// does not observe the DFS events. If you want to observe the DFS |
|
1219 |
/// events, you should implement your own visitor class. |
|
1205 | 1220 |
/// \tparam _Traits Traits class to set various data types used by the |
1206 | 1221 |
/// algorithm. The default traits class is |
1207 | 1222 |
/// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<_Digraph>". |
1208 | 1223 |
/// See \ref DfsVisitDefaultTraits for the documentation of |
1209 |
/// a Dfs visit traits class. |
|
1210 |
/// |
|
1211 |
/// |
|
1224 |
/// a DFS visit traits class. |
|
1212 | 1225 |
#ifdef DOXYGEN |
1213 | 1226 |
template <typename _Digraph, typename _Visitor, typename _Traits> |
1214 | 1227 |
#else |
1215 | 1228 |
template <typename _Digraph = ListDigraph, |
1216 | 1229 |
typename _Visitor = DfsVisitor<_Digraph>, |
1217 | 1230 |
typename _Traits = DfsDefaultTraits<_Digraph> > |
... | ... |
@@ -1219,52 +1232,54 @@ |
1219 | 1232 |
class DfsVisit { |
1220 | 1233 |
public: |
1221 | 1234 |
|
1222 | 1235 |
/// \brief \ref Exception for uninitialized parameters. |
1223 | 1236 |
/// |
1224 | 1237 |
/// This error represents problems in the initialization |
1225 |
/// of the parameters of the |
|
1238 |
/// of the parameters of the algorithm. |
|
1226 | 1239 |
class UninitializedParameter : public lemon::UninitializedParameter { |
1227 | 1240 |
public: |
1228 | 1241 |
virtual const char* what() const throw() |
1229 | 1242 |
{ |
1230 | 1243 |
return "lemon::DfsVisit::UninitializedParameter"; |
1231 | 1244 |
} |
1232 | 1245 |
}; |
1233 | 1246 |
|
1247 |
///The traits class. |
|
1234 | 1248 |
typedef _Traits Traits; |
1235 | 1249 |
|
1250 |
///The type of the digraph the algorithm runs on. |
|
1236 | 1251 |
typedef typename Traits::Digraph Digraph; |
1237 | 1252 |
|
1253 |
///The visitor type used by the algorithm. |
|
1238 | 1254 |
typedef _Visitor Visitor; |
1239 | 1255 |
|
1240 |
///The type of the map |
|
1256 |
///The type of the map that indicates which nodes are reached. |
|
1241 | 1257 |
typedef typename Traits::ReachedMap ReachedMap; |
1242 | 1258 |
|
1243 | 1259 |
private: |
1244 | 1260 |
|
1245 | 1261 |
typedef typename Digraph::Node Node; |
1246 | 1262 |
typedef typename Digraph::NodeIt NodeIt; |
1247 | 1263 |
typedef typename Digraph::Arc Arc; |
1248 | 1264 |
typedef typename Digraph::OutArcIt OutArcIt; |
1249 | 1265 |
|
1250 |
// |
|
1266 |
//Pointer to the underlying digraph. |
|
1251 | 1267 |
const Digraph *_digraph; |
1252 |
// |
|
1268 |
//Pointer to the visitor object. |
|
1253 | 1269 |
Visitor *_visitor; |
1254 |
|
|
1270 |
//Pointer to the map of reached status of the nodes. |
|
1255 | 1271 |
ReachedMap *_reached; |
1256 |
// |
|
1272 |
//Indicates if _reached is locally allocated (true) or not. |
|
1257 | 1273 |
bool local_reached; |
1258 | 1274 |
|
1259 | 1275 |
std::vector<typename Digraph::Arc> _stack; |
1260 | 1276 |
int _stack_head; |
1261 | 1277 |
|
1262 |
/// \brief Creates the maps if necessary. |
|
1263 |
/// |
|
1264 | 1278 |
/// Creates the maps if necessary. |
1279 |
///\todo Better memory allocation (instead of new). |
|
1265 | 1280 |
void create_maps() { |
1266 | 1281 |
if(!_reached) { |
1267 | 1282 |
local_reached = true; |
1268 | 1283 |
_reached = Traits::createReachedMap(*_digraph); |
1269 | 1284 |
} |
1270 | 1285 |
} |
... | ... |
@@ -1285,15 +1300,15 @@ |
1285 | 1300 |
typedef T ReachedMap; |
1286 | 1301 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1287 | 1302 |
throw UninitializedParameter(); |
1288 | 1303 |
} |
1289 | 1304 |
}; |
1290 | 1305 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1291 |
/// ReachedMap type |
|
1306 |
/// ReachedMap type. |
|
1292 | 1307 |
/// |
1293 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type |
|
1308 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
|
1294 | 1309 |
template <class T> |
1295 | 1310 |
struct DefReachedMap : public DfsVisit< Digraph, Visitor, |
1296 | 1311 |
DefReachedMapTraits<T> > { |
1297 | 1312 |
typedef DfsVisit< Digraph, Visitor, DefReachedMapTraits<T> > Create; |
1298 | 1313 |
}; |
1299 | 1314 |
///@} |
... | ... |
@@ -1301,71 +1316,78 @@ |
1301 | 1316 |
public: |
1302 | 1317 |
|
1303 | 1318 |
/// \brief Constructor. |
1304 | 1319 |
/// |
1305 | 1320 |
/// Constructor. |
1306 | 1321 |
/// |
1307 |
/// \param digraph the digraph the algorithm will run on. |
|
1308 |
/// \param visitor The visitor of the algorithm. |
|
1309 |
/// |
|
1322 |
/// \param digraph The digraph the algorithm runs on. |
|
1323 |
/// \param visitor The visitor object of the algorithm. |
|
1310 | 1324 |
DfsVisit(const Digraph& digraph, Visitor& visitor) |
1311 | 1325 |
: _digraph(&digraph), _visitor(&visitor), |
1312 | 1326 |
_reached(0), local_reached(false) {} |
1313 | 1327 |
|
1314 | 1328 |
/// \brief Destructor. |
1315 |
/// |
|
1316 |
/// Destructor. |
|
1317 | 1329 |
~DfsVisit() { |
1318 | 1330 |
if(local_reached) delete _reached; |
1319 | 1331 |
} |
1320 | 1332 |
|
1321 |
/// \brief Sets the map |
|
1333 |
/// \brief Sets the map that indicates which nodes are reached. |
|
1322 | 1334 |
/// |
1323 |
/// Sets the map |
|
1335 |
/// Sets the map that indicates which nodes are reached. |
|
1324 | 1336 |
/// If you don't use this function before calling \ref run(), |
1325 |
/// it will allocate one. The |
|
1337 |
/// it will allocate one. The destructor deallocates this |
|
1326 | 1338 |
/// automatically allocated map, of course. |
1327 | 1339 |
/// \return <tt> (*this) </tt> |
1328 | 1340 |
DfsVisit &reachedMap(ReachedMap &m) { |
1329 | 1341 |
if(local_reached) { |
1330 | 1342 |
delete _reached; |
1331 | 1343 |
local_reached=false; |
1332 | 1344 |
} |
1333 | 1345 |
_reached = &m; |
1334 | 1346 |
return *this; |
1335 | 1347 |
} |
1336 | 1348 |
|
1337 | 1349 |
public: |
1350 |
|
|
1338 | 1351 |
/// \name Execution control |
1339 | 1352 |
/// The simplest way to execute the algorithm is to use |
1340 |
/// one of the member functions called \ |
|
1353 |
/// one of the member functions called \ref lemon::DfsVisit::run() |
|
1354 |
/// "run()". |
|
1341 | 1355 |
/// \n |
1342 |
/// If you need more control on the execution, |
|
1343 |
/// first you must call \ref init(), then you can adda source node |
|
1344 |
/// with \ref addSource(). |
|
1345 |
/// Finally \ref start() will perform the actual path |
|
1346 |
/// |
|
1356 |
/// If you need more control on the execution, first you must call |
|
1357 |
/// \ref lemon::DfsVisit::init() "init()", then you can add several |
|
1358 |
/// source nodes with \ref lemon::DfsVisit::addSource() "addSource()". |
|
1359 |
/// Finally \ref lemon::DfsVisit::start() "start()" will perform the |
|
1360 |
/// actual path computation. |
|
1347 | 1361 |
|
1348 | 1362 |
/// @{ |
1363 |
|
|
1349 | 1364 |
/// \brief Initializes the internal data structures. |
1350 | 1365 |
/// |
1351 | 1366 |
/// Initializes the internal data structures. |
1352 |
/// |
|
1353 | 1367 |
void init() { |
1354 | 1368 |
create_maps(); |
1355 | 1369 |
_stack.resize(countNodes(*_digraph)); |
1356 | 1370 |
_stack_head = -1; |
1357 | 1371 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1358 | 1372 |
_reached->set(u, false); |
1359 | 1373 |
} |
1360 | 1374 |
} |
1361 | 1375 |
|
1362 |
/// |
|
1376 |
///Adds a new source node. |
|
1377 |
|
|
1378 |
///Adds a new source node to the set of nodes to be processed. |
|
1363 | 1379 |
/// |
1364 |
/// Adds a new source node to the set of nodes to be processed. |
|
1365 |
void addSource(Node s) { |
|
1380 |
///\pre The stack must be empty. (Otherwise the algorithm gives |
|
1381 |
///false results.) |
|
1382 |
/// |
|
1383 |
///\warning Distances will be wrong (or at least strange) in case of |
|
1384 |
///multiple sources. |
|
1385 |
void addSource(Node s) |
|
1386 |
{ |
|
1387 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
|
1366 | 1388 |
if(!(*_reached)[s]) { |
1367 | 1389 |
_reached->set(s,true); |
1368 | 1390 |
_visitor->start(s); |
1369 | 1391 |
_visitor->reach(s); |
1370 | 1392 |
Arc e; |
1371 | 1393 |
_digraph->firstOut(e, s); |
... | ... |
@@ -1380,13 +1402,13 @@ |
1380 | 1402 |
/// \brief Processes the next arc. |
1381 | 1403 |
/// |
1382 | 1404 |
/// Processes the next arc. |
1383 | 1405 |
/// |
1384 | 1406 |
/// \return The processed arc. |
1385 | 1407 |
/// |
1386 |
/// \pre The stack must not be empty |
|
1408 |
/// \pre The stack must not be empty. |
|
1387 | 1409 |
Arc processNextArc() { |
1388 | 1410 |
Arc e = _stack[_stack_head]; |
1389 | 1411 |
Node m = _digraph->target(e); |
1390 | 1412 |
if(!(*_reached)[m]) { |
1391 | 1413 |
_visitor->discover(e); |
1392 | 1414 |
_visitor->reach(m); |
... | ... |
@@ -1414,99 +1436,153 @@ |
1414 | 1436 |
/// \brief Next arc to be processed. |
1415 | 1437 |
/// |
1416 | 1438 |
/// Next arc to be processed. |
1417 | 1439 |
/// |
1418 | 1440 |
/// \return The next arc to be processed or INVALID if the stack is |
1419 | 1441 |
/// empty. |
1420 |
Arc nextArc() { |
|
1442 |
Arc nextArc() const { |
|
1421 | 1443 |
return _stack_head >= 0 ? _stack[_stack_head] : INVALID; |
1422 | 1444 |
} |
1423 | 1445 |
|
1424 | 1446 |
/// \brief Returns \c false if there are nodes |
1425 |
/// to be processed |
|
1447 |
/// to be processed. |
|
1426 | 1448 |
/// |
1427 | 1449 |
/// Returns \c false if there are nodes |
1428 |
/// to be processed in the queue |
|
1429 |
bool emptyQueue() { return _stack_head < 0; } |
|
1450 |
/// to be processed in the queue (stack). |
|
1451 |
bool emptyQueue() const { return _stack_head < 0; } |
|
1430 | 1452 |
|
1431 | 1453 |
/// \brief Returns the number of the nodes to be processed. |
1432 | 1454 |
/// |
1433 |
/// Returns the number of the nodes to be processed in the queue. |
|
1434 |
int queueSize() { return _stack_head + 1; } |
|
1455 |
/// Returns the number of the nodes to be processed in the queue (stack). |
|
1456 |
int queueSize() const { return _stack_head + 1; } |
|
1435 | 1457 |
|
1436 | 1458 |
/// \brief Executes the algorithm. |
1437 | 1459 |
/// |
1438 | 1460 |
/// Executes the algorithm. |
1439 | 1461 |
/// |
1440 |
/// \pre init() must be called and at least one node should be added |
|
1441 |
/// with addSource() before using this function. |
|
1462 |
/// This method runs the %DFS algorithm from the root node |
|
1463 |
/// in order to compute the %DFS path to each node. |
|
1464 |
/// |
|
1465 |
/// The algorithm computes |
|
1466 |
/// - the %DFS tree, |
|
1467 |
/// - the distance of each node from the root in the %DFS tree. |
|
1468 |
/// |
|
1469 |
/// \pre init() must be called and a root node should be |
|
1470 |
/// added with addSource() before using this function. |
|
1471 |
/// |
|
1472 |
/// \note <tt>d.start()</tt> is just a shortcut of the following code. |
|
1473 |
/// \code |
|
1474 |
/// while ( !d.emptyQueue() ) { |
|
1475 |
/// d.processNextArc(); |
|
1476 |
/// } |
|
1477 |
/// \endcode |
|
1442 | 1478 |
void start() { |
1443 | 1479 |
while ( !emptyQueue() ) processNextArc(); |
1444 | 1480 |
} |
1445 | 1481 |
|
1446 |
/// \brief Executes the algorithm until |
|
1482 |
/// \brief Executes the algorithm until the given target node is reached. |
|
1447 | 1483 |
/// |
1448 |
/// Executes the algorithm until |
|
1484 |
/// Executes the algorithm until the given target node is reached. |
|
1449 | 1485 |
/// |
1450 |
/// |
|
1486 |
/// This method runs the %DFS algorithm from the root node |
|
1487 |
/// in order to compute the DFS path to \c dest. |
|
1488 |
/// |
|
1489 |
/// The algorithm computes |
|
1490 |
/// - the %DFS path to \c dest, |
|
1491 |
/// - the distance of \c dest from the root in the %DFS tree. |
|
1492 |
/// |
|
1493 |
/// \pre init() must be called and a root node should be added |
|
1451 | 1494 |
/// with addSource() before using this function. |
1452 | 1495 |
void start(Node dest) { |
1453 | 1496 |
while ( !emptyQueue() && _digraph->target(_stack[_stack_head]) != dest ) |
1454 | 1497 |
processNextArc(); |
1455 | 1498 |
} |
1456 | 1499 |
|
1457 | 1500 |
/// \brief Executes the algorithm until a condition is met. |
1458 | 1501 |
/// |
1459 | 1502 |
/// Executes the algorithm until a condition is met. |
1460 | 1503 |
/// |
1461 |
/// |
|
1504 |
/// This method runs the %DFS algorithm from the root node |
|
1505 |
/// until an arc \c a with <tt>am[a]</tt> true is found. |
|
1506 |
/// |
|
1507 |
/// \param am A \c bool (or convertible) arc map. The algorithm |
|
1508 |
/// will stop when it reaches an arc \c a with <tt>am[a]</tt> true. |
|
1509 |
/// |
|
1510 |
/// \return The reached arc \c a with <tt>am[a]</tt> true or |
|
1511 |
/// \c INVALID if no such arc was found. |
|
1512 |
/// |
|
1513 |
/// \pre init() must be called and a root node should be added |
|
1462 | 1514 |
/// with addSource() before using this function. |
1463 | 1515 |
/// |
1464 |
/// \param em must be a bool (or convertible) arc map. The algorithm |
|
1465 |
/// will stop when it reaches an arc \c e with <tt>em[e]</tt> true. |
|
1466 |
/// |
|
1467 |
///\return The reached arc \c e with <tt>em[e]</tt> true or |
|
1468 |
///\c INVALID if no such arc was found. |
|
1469 |
/// |
|
1470 |
/// \warning Contrary to \ref Bfs and \ref Dijkstra, \c |
|
1516 |
/// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map, |
|
1471 | 1517 |
/// not a node map. |
1472 |
template <typename EM> |
|
1473 |
Arc start(const EM &em) { |
|
1474 |
|
|
1518 |
template <typename AM> |
|
1519 |
Arc start(const AM &am) { |
|
1520 |
while ( !emptyQueue() && !am[_stack[_stack_head]] ) |
|
1475 | 1521 |
processNextArc(); |
1476 | 1522 |
return emptyQueue() ? INVALID : _stack[_stack_head]; |
1477 | 1523 |
} |
1478 | 1524 |
|
1479 |
/// \brief Runs |
|
1525 |
/// \brief Runs the algorithm from the given node. |
|
1480 | 1526 |
/// |
1481 |
/// This method runs the %DFS algorithm from a root node \c s. |
|
1482 |
/// \note d.run(s) is just a shortcut of the following code. |
|
1527 |
/// This method runs the %DFS algorithm from node \c s. |
|
1528 |
/// in order to compute the DFS path to each node. |
|
1529 |
/// |
|
1530 |
/// The algorithm computes |
|
1531 |
/// - the %DFS tree, |
|
1532 |
/// - the distance of each node from the root in the %DFS tree. |
|
1533 |
/// |
|
1534 |
/// \note <tt>d.run(s)</tt> is just a shortcut of the following code. |
|
1483 | 1535 |
///\code |
1484 | 1536 |
/// d.init(); |
1485 | 1537 |
/// d.addSource(s); |
1486 | 1538 |
/// d.start(); |
1487 | 1539 |
///\endcode |
1488 | 1540 |
void run(Node s) { |
1489 | 1541 |
init(); |
1490 | 1542 |
addSource(s); |
1491 | 1543 |
start(); |
1492 | 1544 |
} |
1493 | 1545 |
|
1494 |
/// \brief |
|
1546 |
/// \brief Finds the %DFS path between \c s and \c t. |
|
1547 |
|
|
1548 |
/// This method runs the %DFS algorithm from node \c s |
|
1549 |
/// in order to compute the DFS path to \c t. |
|
1550 |
/// |
|
1551 |
/// \return The length of the <tt>s</tt>--<tt>t</tt> DFS path, |
|
1552 |
/// if \c t is reachable form \c s, \c 0 otherwise. |
|
1553 |
/// |
|
1554 |
/// \note Apart from the return value, <tt>d.run(s,t)</tt> is |
|
1555 |
/// just a shortcut of the following code. |
|
1556 |
///\code |
|
1557 |
/// d.init(); |
|
1558 |
/// d.addSource(s); |
|
1559 |
/// d.start(t); |
|
1560 |
///\endcode |
|
1561 |
int run(Node s,Node t) { |
|
1562 |
init(); |
|
1563 |
addSource(s); |
|
1564 |
start(t); |
|
1565 |
return reached(t)?_stack_head+1:0; |
|
1566 |
} |
|
1567 |
|
|
1568 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
|
1495 | 1569 |
|
1496 | 1570 |
/// This method runs the %DFS algorithm in order to |
1497 |
/// compute the %DFS path to each node. The algorithm computes |
|
1498 |
/// - The %DFS tree. |
|
1499 |
/// |
|
1571 |
/// compute the %DFS path to each node. |
|
1500 | 1572 |
/// |
1501 |
/// |
|
1573 |
/// The algorithm computes |
|
1574 |
/// - the %DFS tree, |
|
1575 |
/// - the distance of each node from the root in the %DFS tree. |
|
1576 |
/// |
|
1577 |
/// \note <tt>d.run()</tt> is just a shortcut of the following code. |
|
1502 | 1578 |
///\code |
1503 | 1579 |
/// d.init(); |
1504 |
/// for (NodeIt it(digraph); it != INVALID; ++it) { |
|
1505 |
/// if (!d.reached(it)) { |
|
1506 |
/// |
|
1580 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
|
1581 |
/// if (!d.reached(n)) { |
|
1582 |
/// d.addSource(n); |
|
1507 | 1583 |
/// d.start(); |
1508 | 1584 |
/// } |
1509 | 1585 |
/// } |
1510 | 1586 |
///\endcode |
1511 | 1587 |
void run() { |
1512 | 1588 |
init(); |
... | ... |
@@ -1514,30 +1590,31 @@ |
1514 | 1590 |
if (!reached(it)) { |
1515 | 1591 |
addSource(it); |
1516 | 1592 |
start(); |
1517 | 1593 |
} |
1518 | 1594 |
} |
1519 | 1595 |
} |
1596 |
|
|
1520 | 1597 |
///@} |
1521 | 1598 |
|
1522 | 1599 |
/// \name Query Functions |
1523 | 1600 |
/// The result of the %DFS algorithm can be obtained using these |
1524 | 1601 |
/// functions.\n |
1525 |
/// Before the use of these functions, |
|
1526 |
/// either run() or start() must be called. |
|
1602 |
/// Either \ref lemon::DfsVisit::run() "run()" or |
|
1603 |
/// \ref lemon::DfsVisit::start() "start()" must be called before |
|
1604 |
/// using them. |
|
1527 | 1605 |
///@{ |
1528 |
|
|
1606 |
|
|
1607 |
/// \brief Checks if a node is reachable from the root(s). |
|
1529 | 1608 |
/// |
1530 | 1609 |
/// Returns \c true if \c v is reachable from the root(s). |
1531 |
/// \warning The source nodes are inditated as unreachable. |
|
1532 | 1610 |
/// \pre Either \ref run() or \ref start() |
1533 | 1611 |
/// must be called before using this function. |
1534 |
/// |
|
1535 | 1612 |
bool reached(Node v) { return (*_reached)[v]; } |
1613 |
|
|
1536 | 1614 |
///@} |
1615 |
|
|
1537 | 1616 |
}; |
1538 | 1617 |
|
1539 |
|
|
1540 | 1618 |
} //END OF NAMESPACE LEMON |
1541 | 1619 |
|
1542 | 1620 |
#endif |
1543 |
... | ... |
@@ -30,269 +30,276 @@ |
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 |
/// \brief Default |
|
36 |
/// \brief Default operation traits for the Dijkstra algorithm class. |
|
37 | 37 |
/// |
38 |
/// It defines all computational operations and constants which are |
|
39 |
/// used in the Dijkstra algorithm. |
|
38 |
/// This operation traits class defines all computational operations and |
|
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 |
/// \brief Gives back true only if the first value less than the second. |
|
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 |
/// \brief Widest path |
|
56 |
/// \brief Widest path operation traits for the Dijkstra algorithm class. |
|
57 | 57 |
/// |
58 |
/// It defines all computational operations and constants which are |
|
59 |
/// used in the Dijkstra algorithm for widest path computation. |
|
58 |
/// This operation traits class defines all computational operations and |
|
59 |
/// constants which are used in the Dijkstra algorithm for widest path |
|
60 |
/// computation. |
|
61 |
/// |
|
62 |
/// \see DijkstraDefaultOperationTraits |
|
60 | 63 |
template <typename Value> |
61 | 64 |
struct DijkstraWidestPathOperationTraits { |
62 | 65 |
/// \brief Gives back the maximum value of the type. |
63 | 66 |
static Value zero() { |
64 | 67 |
return std::numeric_limits<Value>::max(); |
65 | 68 |
} |
66 | 69 |
/// \brief Gives back the minimum of the given two elements. |
67 | 70 |
static Value plus(const Value& left, const Value& right) { |
68 | 71 |
return std::min(left, right); |
69 | 72 |
} |
70 |
/// \brief Gives back true only if the first value less than the second. |
|
73 |
/// \brief Gives back true only if the first value is less than the second. |
|
71 | 74 |
static bool less(const Value& left, const Value& right) { |
72 | 75 |
return left < right; |
73 | 76 |
} |
74 | 77 |
}; |
75 | 78 |
|
76 | 79 |
///Default traits class of Dijkstra class. |
77 | 80 |
|
78 | 81 |
///Default traits class of Dijkstra class. |
79 |
///\tparam GR Digraph type. |
|
80 |
///\tparam LM Type of length map. |
|
82 |
///\tparam GR The type of the digraph. |
|
83 |
///\tparam LM The type of the length map. |
|
81 | 84 |
template<class GR, class LM> |
82 | 85 |
struct DijkstraDefaultTraits |
83 | 86 |
{ |
84 |
///The |
|
87 |
///The type of the digraph the algorithm runs on. |
|
85 | 88 |
typedef GR Digraph; |
89 |
|
|
86 | 90 |
///The type of the map that stores the arc lengths. |
87 | 91 |
|
88 | 92 |
///The type of the map that stores the arc lengths. |
89 | 93 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
90 | 94 |
typedef LM LengthMap; |
91 |
//The type of the length of the arcs. |
|
95 |
///The type of the length of the arcs. |
|
92 | 96 |
typedef typename LM::Value Value; |
97 |
|
|
93 | 98 |
/// Operation traits for Dijkstra algorithm. |
94 | 99 |
|
95 |
/// |
|
100 |
/// This class defines the operations that are used in the algorithm. |
|
96 | 101 |
/// \see DijkstraDefaultOperationTraits |
97 | 102 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
98 |
/// The cross reference type used by heap. |
|
99 | 103 |
|
104 |
/// The cross reference type used by the heap. |
|
100 | 105 |
|
101 |
/// The cross reference type used by heap. |
|
106 |
/// The cross reference type used by the heap. |
|
102 | 107 |
/// Usually it is \c Digraph::NodeMap<int>. |
103 | 108 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
104 |
///Instantiates a HeapCrossRef. |
|
109 |
///Instantiates a \ref HeapCrossRef. |
|
105 | 110 |
|
106 |
///This function instantiates a \c HeapCrossRef. |
|
107 |
/// \param G is the digraph, to which we would like to define the |
|
108 |
/// HeapCrossRef. |
|
109 |
static HeapCrossRef *createHeapCrossRef(const GR &G) |
|
111 |
///This function instantiates a \ref HeapCrossRef. |
|
112 |
/// \param g is the digraph, to which we would like to define the |
|
113 |
/// \ref HeapCrossRef. |
|
114 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
|
110 | 115 |
{ |
111 |
return new HeapCrossRef( |
|
116 |
return new HeapCrossRef(g); |
|
112 | 117 |
} |
113 | 118 |
|
114 |
///The heap type used by Dijkstra algorithm. |
|
119 |
///The heap type used by the Dijkstra algorithm. |
|
115 | 120 |
|
116 |
///The heap type used by Dijkstra algorithm. |
|
121 |
///The heap type used by the Dijkstra algorithm. |
|
117 | 122 |
/// |
118 | 123 |
///\sa BinHeap |
119 | 124 |
///\sa Dijkstra |
120 | 125 |
typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap; |
126 |
///Instantiates a \ref Heap. |
|
121 | 127 |
|
122 |
|
|
128 |
///This function instantiates a \ref Heap. |
|
129 |
static Heap *createHeap(HeapCrossRef& r) |
|
123 | 130 |
{ |
124 |
return new Heap( |
|
131 |
return new Heap(r); |
|
125 | 132 |
} |
126 | 133 |
|
127 |
///\brief The type of the map that stores the |
|
134 |
///\brief The type of the map that stores the predecessor |
|
128 | 135 |
///arcs of the shortest paths. |
129 | 136 |
/// |
130 |
///The type of the map that stores the |
|
137 |
///The type of the map that stores the predecessor |
|
131 | 138 |
///arcs of the shortest paths. |
132 | 139 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
133 |
/// |
|
134 |
typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap; |
|
135 |
|
|
140 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
|
141 |
///Instantiates a \ref PredMap. |
|
136 | 142 |
|
137 |
///This function instantiates a \c PredMap. |
|
138 |
///\param G is the digraph, to which we would like to define the PredMap. |
|
143 |
///This function instantiates a \ref PredMap. |
|
144 |
///\param g is the digraph, to which we would like to define the |
|
145 |
///\ref PredMap. |
|
139 | 146 |
///\todo The digraph alone may be insufficient for the initialization |
140 |
static PredMap *createPredMap(const |
|
147 |
static PredMap *createPredMap(const Digraph &g) |
|
141 | 148 |
{ |
142 |
return new PredMap( |
|
149 |
return new PredMap(g); |
|
143 | 150 |
} |
144 | 151 |
|
145 |
///The type of the map that |
|
152 |
///The type of the map that indicates which nodes are processed. |
|
146 | 153 |
|
147 |
///The type of the map that |
|
154 |
///The type of the map that indicates which nodes are processed. |
|
148 | 155 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
149 | 156 |
///By default it is a NullMap. |
150 | 157 |
///\todo If it is set to a real map, |
151 | 158 |
///Dijkstra::processed() should read this. |
152 |
///\todo named parameter to set this type, function to read and write. |
|
153 | 159 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
154 |
///Instantiates a ProcessedMap. |
|
160 |
///Instantiates a \ref ProcessedMap. |
|
155 | 161 |
|
156 |
///This function instantiates a \ |
|
162 |
///This function instantiates a \ref ProcessedMap. |
|
157 | 163 |
///\param g is the digraph, to which |
158 |
///we would like to define the \ |
|
164 |
///we would like to define the \ref ProcessedMap |
|
159 | 165 |
#ifdef DOXYGEN |
160 |
static ProcessedMap *createProcessedMap(const |
|
166 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
|
161 | 167 |
#else |
162 |
static ProcessedMap *createProcessedMap(const |
|
168 |
static ProcessedMap *createProcessedMap(const Digraph &) |
|
163 | 169 |
#endif |
164 | 170 |
{ |
165 | 171 |
return new ProcessedMap(); |
166 | 172 |
} |
167 |
///The type of the map that stores the dists of the nodes. |
|
168 | 173 |
|
169 |
///The type of the map that stores the |
|
174 |
///The type of the map that stores the distances of the nodes. |
|
175 |
|
|
176 |
///The type of the map that stores the distances of the nodes. |
|
170 | 177 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
171 |
/// |
|
172 | 178 |
typedef typename Digraph::template NodeMap<typename LM::Value> DistMap; |
173 |
///Instantiates a DistMap. |
|
179 |
///Instantiates a \ref DistMap. |
|
174 | 180 |
|
175 | 181 |
///This function instantiates a \ref DistMap. |
176 |
///\param |
|
182 |
///\param g is the digraph, to which we would like to define |
|
177 | 183 |
///the \ref DistMap |
178 |
static DistMap *createDistMap(const |
|
184 |
static DistMap *createDistMap(const Digraph &g) |
|
179 | 185 |
{ |
180 |
return new DistMap( |
|
186 |
return new DistMap(g); |
|
181 | 187 |
} |
182 | 188 |
}; |
183 | 189 |
|
184 | 190 |
///%Dijkstra algorithm class. |
185 | 191 |
|
186 | 192 |
/// \ingroup shortest_path |
187 |
///This class provides an efficient implementation of %Dijkstra algorithm. |
|
193 |
///This class provides an efficient implementation of the %Dijkstra algorithm. |
|
194 |
/// |
|
188 | 195 |
///The arc lengths are passed to the algorithm using a |
189 | 196 |
///\ref concepts::ReadMap "ReadMap", |
190 | 197 |
///so it is easy to change it to any kind of length. |
191 |
/// |
|
192 | 198 |
///The type of the length is determined by the |
193 | 199 |
///\ref concepts::ReadMap::Value "Value" of the length map. |
194 |
/// |
|
195 | 200 |
///It is also possible to change the underlying priority heap. |
196 | 201 |
/// |
197 |
///\tparam GR The digraph type the algorithm runs on. The default value |
|
198 |
///is \ref ListDigraph. The value of GR is not used directly by |
|
199 |
///Dijkstra, it is only passed to \ref DijkstraDefaultTraits. |
|
200 |
///\tparam LM This read-only ArcMap determines the lengths of the |
|
202 |
///There is also a \ref dijkstra() "function type interface" for the |
|
203 |
///%Dijkstra algorithm, which is convenient in the simplier cases and |
|
204 |
///it can be used easier. |
|
205 |
/// |
|
206 |
///\tparam GR The type of the digraph the algorithm runs on. |
|
207 |
///The default value is \ref ListDigraph. |
|
208 |
///The value of GR is not used directly by \ref Dijkstra, it is only |
|
209 |
///passed to \ref DijkstraDefaultTraits. |
|
210 |
///\tparam LM A readable arc map that determines the lengths of the |
|
201 | 211 |
///arcs. It is read once for each arc, so the map may involve in |
202 |
///relatively time consuming process to compute the arc |
|
212 |
///relatively time consuming process to compute the arc lengths if |
|
203 | 213 |
///it is necessary. The default map type is \ref |
204 |
///concepts::Digraph::ArcMap "Digraph::ArcMap<int>". The value |
|
205 |
///of LM is not used directly by Dijkstra, it is only passed to \ref |
|
206 |
///DijkstraDefaultTraits. |
|
207 |
///\tparam TR Traits class to set |
|
208 |
///various data types used by the algorithm. The default traits |
|
209 |
///class is \ref DijkstraDefaultTraits |
|
210 |
///"DijkstraDefaultTraits<GR,LM>". See \ref |
|
211 |
///DijkstraDefaultTraits for the documentation of a Dijkstra traits |
|
212 |
///class. |
|
213 |
|
|
214 |
///concepts::Digraph::ArcMap "Digraph::ArcMap<int>". |
|
215 |
///The value of LM is not used directly by \ref Dijkstra, it is only |
|
216 |
///passed to \ref DijkstraDefaultTraits. |
|
217 |
///\tparam TR Traits class to set various data types used by the algorithm. |
|
218 |
///The default traits class is \ref DijkstraDefaultTraits |
|
219 |
///"DijkstraDefaultTraits<GR,LM>". See \ref DijkstraDefaultTraits |
|
220 |
///for the documentation of a Dijkstra traits class. |
|
214 | 221 |
#ifdef DOXYGEN |
215 | 222 |
template <typename GR, typename LM, typename TR> |
216 | 223 |
#else |
217 | 224 |
template <typename GR=ListDigraph, |
218 | 225 |
typename LM=typename GR::template ArcMap<int>, |
219 | 226 |
typename TR=DijkstraDefaultTraits<GR,LM> > |
220 | 227 |
#endif |
221 | 228 |
class Dijkstra { |
222 | 229 |
public: |
223 |
/** |
|
224 |
* \brief \ref Exception for uninitialized parameters. |
|
225 |
* |
|
226 |
* This error represents problems in the initialization |
|
227 |
* of the parameters of the algorithms. |
|
228 |
*/ |
|
230 |
///\ref Exception for uninitialized parameters. |
|
231 |
|
|
232 |
///This error represents problems in the initialization of the |
|
233 |
///parameters of the algorithm. |
|
229 | 234 |
class UninitializedParameter : public lemon::UninitializedParameter { |
230 | 235 |
public: |
231 | 236 |
virtual const char* what() const throw() { |
232 | 237 |
return "lemon::Dijkstra::UninitializedParameter"; |
233 | 238 |
} |
234 | 239 |
}; |
235 | 240 |
|
236 |
typedef TR Traits; |
|
237 |
///The type of the underlying digraph. |
|
241 |
///The type of the digraph the algorithm runs on. |
|
238 | 242 |
typedef typename TR::Digraph Digraph; |
239 |
///\e |
|
240 |
typedef typename Digraph::Node Node; |
|
241 |
///\e |
|
242 |
typedef typename Digraph::NodeIt NodeIt; |
|
243 |
///\e |
|
244 |
typedef typename Digraph::Arc Arc; |
|
245 |
///\e |
|
246 |
typedef typename Digraph::OutArcIt OutArcIt; |
|
247 | 243 |
|
248 | 244 |
///The type of the length of the arcs. |
249 | 245 |
typedef typename TR::LengthMap::Value Value; |
250 | 246 |
///The type of the map that stores the arc lengths. |
251 | 247 |
typedef typename TR::LengthMap LengthMap; |
252 |
///\brief The type of the map that stores the last |
|
253 |
///arcs of the shortest paths. |
|
248 |
///\brief The type of the map that stores the predecessor arcs of the |
|
249 |
///shortest paths. |
|
254 | 250 |
typedef typename TR::PredMap PredMap; |
255 |
///The type of the map |
|
251 |
///The type of the map that stores the distances of the nodes. |
|
252 |
typedef typename TR::DistMap DistMap; |
|
253 |
///The type of the map that indicates which nodes are processed. |
|
256 | 254 |
typedef typename TR::ProcessedMap ProcessedMap; |
257 |
///The type of the map that stores the dists of the nodes. |
|
258 |
typedef typename TR::DistMap DistMap; |
|
255 |
///The type of the paths. |
|
256 |
typedef PredMapPath<Digraph, PredMap> Path; |
|
259 | 257 |
///The cross reference type used for the current heap. |
260 | 258 |
typedef typename TR::HeapCrossRef HeapCrossRef; |
261 |
///The heap type used by the |
|
259 |
///The heap type used by the algorithm. |
|
262 | 260 |
typedef typename TR::Heap Heap; |
263 |
///The operation traits. |
|
261 |
///The operation traits class. |
|
264 | 262 |
typedef typename TR::OperationTraits OperationTraits; |
263 |
|
|
264 |
///The traits class. |
|
265 |
typedef TR Traits; |
|
266 |
|
|
265 | 267 |
private: |
266 |
|
|
268 |
|
|
269 |
typedef typename Digraph::Node Node; |
|
270 |
typedef typename Digraph::NodeIt NodeIt; |
|
271 |
typedef typename Digraph::Arc Arc; |
|
272 |
typedef typename Digraph::OutArcIt OutArcIt; |
|
273 |
|
|
274 |
//Pointer to the underlying digraph. |
|
267 | 275 |
const Digraph *G; |
268 |
// |
|
276 |
//Pointer to the length map. |
|
269 | 277 |
const LengthMap *length; |
270 |
|
|
278 |
//Pointer to the map of predecessors arcs. |
|
271 | 279 |
PredMap *_pred; |
272 |
// |
|
280 |
//Indicates if _pred is locally allocated (true) or not. |
|
273 | 281 |
bool local_pred; |
274 |
|
|
282 |
//Pointer to the map of distances. |
|
275 | 283 |
DistMap *_dist; |
276 |
// |
|
284 |
//Indicates if _dist is locally allocated (true) or not. |
|
277 | 285 |
bool local_dist; |
278 |
|
|
286 |
//Pointer to the map of processed status of the nodes. |
|
279 | 287 |
ProcessedMap *_processed; |
280 |
// |
|
288 |
//Indicates if _processed is locally allocated (true) or not. |
|
281 | 289 |
bool local_processed; |
282 |
|
|
290 |
//Pointer to the heap cross references. |
|
283 | 291 |
HeapCrossRef *_heap_cross_ref; |
284 |
// |
|
292 |
//Indicates if _heap_cross_ref is locally allocated (true) or not. |
|
285 | 293 |
bool local_heap_cross_ref; |
286 |
|
|
294 |
//Pointer to the heap. |
|
287 | 295 |
Heap *_heap; |
288 |
// |
|
296 |
//Indicates if _heap is locally allocated (true) or not. |
|
289 | 297 |
bool local_heap; |
290 | 298 |
|
291 | 299 |
///Creates the maps if necessary. |
292 |
|
|
293 | 300 |
///\todo Better memory allocation (instead of new). |
294 | 301 |
void create_maps() |
295 | 302 |
{ |
296 | 303 |
if(!_pred) { |
297 | 304 |
local_pred = true; |
298 | 305 |
_pred = Traits::createPredMap(*G); |
... | ... |
@@ -328,16 +335,17 @@ |
328 | 335 |
typedef T PredMap; |
329 | 336 |
static PredMap *createPredMap(const Digraph &) |
330 | 337 |
{ |
331 | 338 |
throw UninitializedParameter(); |
332 | 339 |
} |
333 | 340 |
}; |
334 |
///\ref named-templ-param "Named parameter" for setting PredMap type |
|
335 |
|
|
336 |
///\ref named-templ-param "Named parameter" for setting |
|
341 |
///\brief \ref named-templ-param "Named parameter" for setting |
|
342 |
///\ref PredMap type. |
|
337 | 343 |
/// |
344 |
///\ref named-templ-param "Named parameter" for setting |
|
345 |
///\ref PredMap type. |
|
338 | 346 |
template <class T> |
339 | 347 |
struct DefPredMap |
340 | 348 |
: public Dijkstra< Digraph, LengthMap, DefPredMapTraits<T> > { |
341 | 349 |
typedef Dijkstra< Digraph, LengthMap, DefPredMapTraits<T> > Create; |
342 | 350 |
}; |
343 | 351 |
|
... | ... |
@@ -346,53 +354,55 @@ |
346 | 354 |
typedef T DistMap; |
347 | 355 |
static DistMap *createDistMap(const Digraph &) |
348 | 356 |
{ |
349 | 357 |
throw UninitializedParameter(); |
350 | 358 |
} |
351 | 359 |
}; |
352 |
///\ref named-templ-param "Named parameter" for setting DistMap type |
|
353 |
|
|
354 |
///\ref named-templ-param "Named parameter" for setting |
|
360 |
///\brief \ref named-templ-param "Named parameter" for setting |
|
361 |
///\ref DistMap type. |
|
355 | 362 |
/// |
363 |
///\ref named-templ-param "Named parameter" for setting |
|
364 |
///\ref DistMap type. |
|
356 | 365 |
template <class T> |
357 | 366 |
struct DefDistMap |
358 | 367 |
: public Dijkstra< Digraph, LengthMap, DefDistMapTraits<T> > { |
359 | 368 |
typedef Dijkstra< Digraph, LengthMap, DefDistMapTraits<T> > Create; |
360 | 369 |
}; |
361 | 370 |
|
362 | 371 |
template <class T> |
363 | 372 |
struct DefProcessedMapTraits : public Traits { |
364 | 373 |
typedef T ProcessedMap; |
365 |
static ProcessedMap *createProcessedMap(const Digraph & |
|
374 |
static ProcessedMap *createProcessedMap(const Digraph &) |
|
366 | 375 |
{ |
367 | 376 |
throw UninitializedParameter(); |
368 | 377 |
} |
369 | 378 |
}; |
370 |
///\ref named-templ-param "Named parameter" for setting ProcessedMap type |
|
371 |
|
|
372 |
///\ref named-templ-param "Named parameter" for setting |
|
379 |
///\brief \ref named-templ-param "Named parameter" for setting |
|
380 |
///\ref ProcessedMap type. |
|
373 | 381 |
/// |
382 |
///\ref named-templ-param "Named parameter" for setting |
|
383 |
///\ref ProcessedMap type. |
|
374 | 384 |
template <class T> |
375 | 385 |
struct DefProcessedMap |
376 | 386 |
: public Dijkstra< Digraph, LengthMap, DefProcessedMapTraits<T> > { |
377 | 387 |
typedef Dijkstra< Digraph, LengthMap, DefProcessedMapTraits<T> > Create; |
378 | 388 |
}; |
379 | 389 |
|
380 | 390 |
struct DefDigraphProcessedMapTraits : public Traits { |
381 | 391 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
382 |
static ProcessedMap *createProcessedMap(const Digraph & |
|
392 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
|
383 | 393 |
{ |
384 |
return new ProcessedMap( |
|
394 |
return new ProcessedMap(g); |
|
385 | 395 |
} |
386 | 396 |
}; |
387 |
///\brief \ref named-templ-param "Named parameter" |
|
388 |
///for setting the ProcessedMap type to be Digraph::NodeMap<bool>. |
|
397 |
///\brief \ref named-templ-param "Named parameter" for setting |
|
398 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
389 | 399 |
/// |
390 |
///\ref named-templ-param "Named parameter" |
|
391 |
///for setting the ProcessedMap type to be Digraph::NodeMap<bool>. |
|
392 |
/// |
|
400 |
///\ref named-templ-param "Named parameter" for setting |
|
401 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
|
402 |
///If you don't set it explicitly, it will be automatically allocated. |
|
393 | 403 |
template <class T> |
394 | 404 |
struct DefProcessedMapToBeDefaultMap |
395 | 405 |
: public Dijkstra< Digraph, LengthMap, DefDigraphProcessedMapTraits> { |
396 | 406 |
typedef Dijkstra< Digraph, LengthMap, DefDigraphProcessedMapTraits> |
397 | 407 |
Create; |
398 | 408 |
}; |
... | ... |
@@ -410,14 +420,13 @@ |
410 | 420 |
} |
411 | 421 |
}; |
412 | 422 |
///\brief \ref named-templ-param "Named parameter" for setting |
413 | 423 |
///heap and cross reference type |
414 | 424 |
/// |
415 | 425 |
///\ref named-templ-param "Named parameter" for setting heap and cross |
416 |
///reference type |
|
417 |
/// |
|
426 |
///reference type. |
|
418 | 427 |
template <class H, class CR = typename Digraph::template NodeMap<int> > |
419 | 428 |
struct DefHeap |
420 | 429 |
: public Dijkstra< Digraph, LengthMap, DefHeapTraits<H, CR> > { |
421 | 430 |
typedef Dijkstra< Digraph, LengthMap, DefHeapTraits<H, CR> > Create; |
422 | 431 |
}; |
423 | 432 |
|
... | ... |
@@ -450,38 +459,38 @@ |
450 | 459 |
template <class T> |
451 | 460 |
struct DefOperationTraitsTraits : public Traits { |
452 | 461 |
typedef T OperationTraits; |
453 | 462 |
}; |
454 | 463 |
|
455 | 464 |
/// \brief \ref named-templ-param "Named parameter" for setting |
456 |
/// OperationTraits type |
|
465 |
///\ref OperationTraits type |
|
457 | 466 |
/// |
458 |
/// \ref named-templ-param "Named parameter" for setting OperationTraits |
|
459 |
/// type |
|
467 |
///\ref named-templ-param "Named parameter" for setting |
|
468 |
///\ref OperationTraits type. |
|
460 | 469 |
template <class T> |
461 | 470 |
struct DefOperationTraits |
462 | 471 |
: public Dijkstra<Digraph, LengthMap, DefOperationTraitsTraits<T> > { |
463 | 472 |
typedef Dijkstra<Digraph, LengthMap, DefOperationTraitsTraits<T> > |
464 | 473 |
Create; |
465 | 474 |
}; |
466 | 475 |
|
467 | 476 |
///@} |
468 | 477 |
|
469 |
|
|
470 | 478 |
protected: |
471 | 479 |
|
472 | 480 |
Dijkstra() {} |
473 | 481 |
|
474 | 482 |
public: |
475 | 483 |
|
476 | 484 |
///Constructor. |
477 | 485 |
|
478 |
///\param _G the digraph the algorithm will run on. |
|
479 |
///\param _length the length map used by the algorithm. |
|
480 |
Dijkstra(const Digraph& _G, const LengthMap& _length) : |
|
481 |
G(&_G), length(&_length), |
|
486 |
///Constructor. |
|
487 |
///\param _g The digraph the algorithm runs on. |
|
488 |
///\param _length The length map used by the algorithm. |
|
489 |
Dijkstra(const Digraph& _g, const LengthMap& _length) : |
|
490 |
G(&_g), length(&_length), |
|
482 | 491 |
_pred(NULL), local_pred(false), |
483 | 492 |
_dist(NULL), local_dist(false), |
484 | 493 |
_processed(NULL), local_processed(false), |
485 | 494 |
_heap_cross_ref(NULL), local_heap_cross_ref(false), |
486 | 495 |
_heap(NULL), local_heap(false) |
487 | 496 |
{ } |
... | ... |
@@ -503,34 +512,52 @@ |
503 | 512 |
Dijkstra &lengthMap(const LengthMap &m) |
504 | 513 |
{ |
505 | 514 |
length = &m; |
506 | 515 |
return *this; |
507 | 516 |
} |
508 | 517 |
|
509 |
///Sets the map |
|
518 |
///Sets the map that stores the predecessor arcs. |
|
510 | 519 |
|
511 |
///Sets the map |
|
520 |
///Sets the map that stores the predecessor arcs. |
|
512 | 521 |
///If you don't use this function before calling \ref run(), |
513 |
///it will allocate one. The |
|
522 |
///it will allocate one. The destructor deallocates this |
|
514 | 523 |
///automatically allocated map, of course. |
515 | 524 |
///\return <tt> (*this) </tt> |
516 | 525 |
Dijkstra &predMap(PredMap &m) |
517 | 526 |
{ |
518 | 527 |
if(local_pred) { |
519 | 528 |
delete _pred; |
520 | 529 |
local_pred=false; |
521 | 530 |
} |
522 | 531 |
_pred = &m; |
523 | 532 |
return *this; |
524 | 533 |
} |
525 | 534 |
|
526 |
///Sets the map |
|
535 |
///Sets the map that indicates which nodes are processed. |
|
527 | 536 |
|
528 |
///Sets the map |
|
537 |
///Sets the map that indicates which nodes are processed. |
|
529 | 538 |
///If you don't use this function before calling \ref run(), |
530 |
///it will allocate one. The |
|
539 |
///it will allocate one. The destructor deallocates this |
|
540 |
///automatically allocated map, of course. |
|
541 |
///\return <tt> (*this) </tt> |
|
542 |
Dijkstra &processedMap(ProcessedMap &m) |
|
543 |
{ |
|
544 |
if(local_processed) { |
|
545 |
delete _processed; |
|
546 |
local_processed=false; |
|
547 |
} |
|
548 |
_processed = &m; |
|
549 |
return *this; |
|
550 |
} |
|
551 |
|
|
552 |
///Sets the map that stores the distances of the nodes. |
|
553 |
|
|
554 |
///Sets the map that stores the distances of the nodes calculated by the |
|
555 |
///algorithm. |
|
556 |
///If you don't use this function before calling \ref run(), |
|
557 |
///it will allocate one. The destructor deallocates this |
|
531 | 558 |
///automatically allocated map, of course. |
532 | 559 |
///\return <tt> (*this) </tt> |
533 | 560 |
Dijkstra &distMap(DistMap &m) |
534 | 561 |
{ |
535 | 562 |
if(local_dist) { |
536 | 563 |
delete _dist; |
... | ... |
@@ -541,13 +568,13 @@ |
541 | 568 |
} |
542 | 569 |
|
543 | 570 |
///Sets the heap and the cross reference used by algorithm. |
544 | 571 |
|
545 | 572 |
///Sets the heap and the cross reference used by algorithm. |
546 | 573 |
///If you don't use this function before calling \ref run(), |
547 |
///it will allocate one. The |
|
574 |
///it will allocate one. The destructor deallocates this |
|
548 | 575 |
///automatically allocated heap and cross reference, of course. |
549 | 576 |
///\return <tt> (*this) </tt> |
550 | 577 |
Dijkstra &heap(Heap& hp, HeapCrossRef &cr) |
551 | 578 |
{ |
552 | 579 |
if(local_heap_cross_ref) { |
553 | 580 |
delete _heap_cross_ref; |
... | ... |
@@ -560,31 +587,30 @@ |
560 | 587 |
} |
561 | 588 |
_heap = &hp; |
562 | 589 |
return *this; |
563 | 590 |
} |
564 | 591 |
|
565 | 592 |
private: |
593 |
|
|
566 | 594 |
void finalizeNodeData(Node v,Value dst) |
567 | 595 |
{ |
568 | 596 |
_processed->set(v,true); |
569 | 597 |
_dist->set(v, dst); |
570 | 598 |
} |
571 | 599 |
|
572 | 600 |
public: |
573 | 601 |
|
574 |
typedef PredMapPath<Digraph, PredMap> Path; |
|
575 |
|
|
576 | 602 |
///\name Execution control |
577 |
///The simplest way to execute the algorithm is to use |
|
578 |
///one of the member functions called \c run(...). |
|
603 |
///The simplest way to execute the algorithm is to use one of the |
|
604 |
///member functions called \ref lemon::Dijkstra::run() "run()". |
|
579 | 605 |
///\n |
580 |
///If you need more control on the execution, |
|
581 |
///first you must call \ref init(), then you can add several source nodes |
|
582 |
///with \ref addSource(). |
|
583 |
///Finally \ref start() will perform the actual path |
|
584 |
/// |
|
606 |
///If you need more control on the execution, first you must call |
|
607 |
///\ref lemon::Dijkstra::init() "init()", then you can add several |
|
608 |
///source nodes with \ref lemon::Dijkstra::addSource() "addSource()". |
|
609 |
///Finally \ref lemon::Dijkstra::start() "start()" will perform the |
|
610 |
///actual path computation. |
|
585 | 611 |
|
586 | 612 |
///@{ |
587 | 613 |
|
588 | 614 |
///Initializes the internal data structures. |
589 | 615 |
|
590 | 616 |
///Initializes the internal data structures. |
... | ... |
@@ -600,16 +626,15 @@ |
600 | 626 |
} |
601 | 627 |
} |
602 | 628 |
|
603 | 629 |
///Adds a new source node. |
604 | 630 |
|
605 | 631 |
///Adds a new source node to the priority heap. |
606 |
/// |
|
607 | 632 |
///The optional second parameter is the initial distance of the node. |
608 | 633 |
/// |
609 |
/// |
|
634 |
///The function checks if the node has already been added to the heap and |
|
610 | 635 |
///it is pushed to the heap only if either it was not in the heap |
611 | 636 |
///or the shortest path found till then is shorter than \c dst. |
612 | 637 |
void addSource(Node s,Value dst=OperationTraits::zero()) |
613 | 638 |
{ |
614 | 639 |
if(_heap->state(s) != Heap::IN_HEAP) { |
615 | 640 |
_heap->push(s,dst); |
... | ... |
@@ -622,13 +647,13 @@ |
622 | 647 |
///Processes the next node in the priority heap |
623 | 648 |
|
624 | 649 |
///Processes the next node in the priority heap. |
625 | 650 |
/// |
626 | 651 |
///\return The processed node. |
627 | 652 |
/// |
628 |
///\warning The priority heap must not be empty |
|
653 |
///\warning The priority heap must not be empty. |
|
629 | 654 |
Node processNextNode() |
630 | 655 |
{ |
631 | 656 |
Node v=_heap->top(); |
632 | 657 |
Value oldvalue=_heap->prio(); |
633 | 658 |
_heap->pop(); |
634 | 659 |
finalizeNodeData(v,oldvalue); |
... | ... |
@@ -653,105 +678,113 @@ |
653 | 678 |
break; |
654 | 679 |
} |
655 | 680 |
} |
656 | 681 |
return v; |
657 | 682 |
} |
658 | 683 |
|
659 |
/// |
|
684 |
///The next node to be processed. |
|
660 | 685 |
|
661 |
///Next node to be processed. |
|
662 |
/// |
|
663 |
///\return The next node to be processed or INVALID if the priority heap |
|
664 |
/// is empty. |
|
665 |
|
|
686 |
///Returns the next node to be processed or \c INVALID if the |
|
687 |
///priority heap is empty. |
|
688 |
Node nextNode() const |
|
666 | 689 |
{ |
667 | 690 |
return !_heap->empty()?_heap->top():INVALID; |
668 | 691 |
} |
669 | 692 |
|
670 | 693 |
///\brief Returns \c false if there are nodes |
671 |
///to be processed |
|
694 |
///to be processed. |
|
672 | 695 |
/// |
673 | 696 |
///Returns \c false if there are nodes |
674 |
///to be processed in the priority heap |
|
675 |
bool emptyQueue() { return _heap->empty(); } |
|
697 |
///to be processed in the priority heap. |
|
698 |
bool emptyQueue() const { return _heap->empty(); } |
|
699 |
|
|
676 | 700 |
///Returns the number of the nodes to be processed in the priority heap |
677 | 701 |
|
678 |
///Returns the number of the nodes to be processed in the priority heap |
|
702 |
///Returns the number of the nodes to be processed in the priority heap. |
|
679 | 703 |
/// |
680 |
int queueSize() { return _heap->size(); } |
|
704 |
int queueSize() const { return _heap->size(); } |
|
681 | 705 |
|
682 | 706 |
///Executes the algorithm. |
683 | 707 |
|
684 | 708 |
///Executes the algorithm. |
685 | 709 |
/// |
686 |
///\pre init() must be called and at least one node should be added |
|
687 |
///with addSource() before using this function. |
|
710 |
///This method runs the %Dijkstra algorithm from the root node(s) |
|
711 |
///in order to compute the shortest path to each node. |
|
712 |
/// |
|
713 |
///The algorithm computes |
|
714 |
///- the shortest path tree (forest), |
|
715 |
///- the distance of each node from the root(s). |
|
716 |
/// |
|
717 |
///\pre init() must be called and at least one root node should be |
|
718 |
///added with addSource() before using this function. |
|
719 |
/// |
|
720 |
///\note <tt>d.start()</tt> is just a shortcut of the following code. |
|
721 |
///\code |
|
722 |
/// while ( !d.emptyQueue() ) { |
|
723 |
/// d.processNextNode(); |
|
724 |
/// } |
|
725 |
///\endcode |
|
726 |
void start() |
|
727 |
{ |
|
728 |
while ( !emptyQueue() ) processNextNode(); |
|
729 |
} |
|
730 |
|
|
731 |
///Executes the algorithm until the given target node is reached. |
|
732 |
|
|
733 |
///Executes the algorithm until the given target node is reached. |
|
688 | 734 |
/// |
689 | 735 |
///This method runs the %Dijkstra algorithm from the root node(s) |
690 |
///in order to |
|
691 |
///compute the |
|
692 |
///shortest path to each node. The algorithm computes |
|
693 |
///- The shortest path tree. |
|
694 |
/// |
|
736 |
///in order to compute the shortest path to \c dest. |
|
695 | 737 |
/// |
696 |
void start() |
|
697 |
{ |
|
698 |
while ( !_heap->empty() ) processNextNode(); |
|
699 |
} |
|
700 |
|
|
701 |
///Executes the algorithm until \c dest is reached. |
|
702 |
|
|
703 |
///Executes the algorithm until \c dest is reached. |
|
738 |
///The algorithm computes |
|
739 |
///- the shortest path to \c dest, |
|
740 |
///- the distance of \c dest from the root(s). |
|
704 | 741 |
/// |
705 |
///\pre init() must be called and at least one node should be added |
|
706 |
///with addSource() before using this function. |
|
707 |
/// |
|
708 |
///This method runs the %Dijkstra algorithm from the root node(s) |
|
709 |
///in order to |
|
710 |
///compute the |
|
711 |
///shortest path to \c dest. The algorithm computes |
|
712 |
///- The shortest path to \c dest. |
|
713 |
///- The distance of \c dest from the root(s). |
|
714 |
/// |
|
742 |
///\pre init() must be called and at least one root node should be |
|
743 |
///added with addSource() before using this function. |
|
715 | 744 |
void start(Node dest) |
716 | 745 |
{ |
717 | 746 |
while ( !_heap->empty() && _heap->top()!=dest ) processNextNode(); |
718 | 747 |
if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio()); |
719 | 748 |
} |
720 | 749 |
|
721 | 750 |
///Executes the algorithm until a condition is met. |
722 | 751 |
|
723 | 752 |
///Executes the algorithm until a condition is met. |
724 | 753 |
/// |
725 |
///\pre init() must be called and at least one node should be added |
|
726 |
///with addSource() before using this function. |
|
754 |
///This method runs the %Dijkstra algorithm from the root node(s) in |
|
755 |
///order to compute the shortest path to a node \c v with |
|
756 |
/// <tt>nm[v]</tt> true, if such a node can be found. |
|
727 | 757 |
/// |
728 |
///\param nm |
|
758 |
///\param nm A \c bool (or convertible) node map. The algorithm |
|
729 | 759 |
///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. |
730 | 760 |
/// |
731 | 761 |
///\return The reached node \c v with <tt>nm[v]</tt> true or |
732 | 762 |
///\c INVALID if no such node was found. |
763 |
/// |
|
764 |
///\pre init() must be called and at least one root node should be |
|
765 |
///added with addSource() before using this function. |
|
733 | 766 |
template<class NodeBoolMap> |
734 | 767 |
Node start(const NodeBoolMap &nm) |
735 | 768 |
{ |
736 | 769 |
while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode(); |
737 | 770 |
if ( _heap->empty() ) return INVALID; |
738 | 771 |
finalizeNodeData(_heap->top(),_heap->prio()); |
739 | 772 |
return _heap->top(); |
740 | 773 |
} |
741 | 774 |
|
742 |
///Runs |
|
775 |
///Runs the algorithm from the given node. |
|
743 | 776 |
|
744 |
///This method runs the %Dijkstra algorithm from a root node \c s |
|
745 |
///in order to |
|
746 |
///compute the |
|
747 |
///shortest path to each node. The algorithm computes |
|
748 |
///- The shortest path tree. |
|
749 |
///- The distance of each node from the root. |
|
777 |
///This method runs the %Dijkstra algorithm from node \c s |
|
778 |
///in order to compute the shortest path to each node. |
|
750 | 779 |
/// |
751 |
/// |
|
780 |
///The algorithm computes |
|
781 |
///- the shortest path tree, |
|
782 |
///- the distance of each node from the root. |
|
783 |
/// |
|
784 |
///\note <tt>d.run(s)</tt> is just a shortcut of the following code. |
|
752 | 785 |
///\code |
753 | 786 |
/// d.init(); |
754 | 787 |
/// d.addSource(s); |
755 | 788 |
/// d.start(); |
756 | 789 |
///\endcode |
757 | 790 |
void run(Node s) { |
... | ... |
@@ -759,18 +792,20 @@ |
759 | 792 |
addSource(s); |
760 | 793 |
start(); |
761 | 794 |
} |
762 | 795 |
|
763 | 796 |
///Finds the shortest path between \c s and \c t. |
764 | 797 |
|
765 |
/// |
|
798 |
///This method runs the %Dijkstra algorithm from node \c s |
|
799 |
///in order to compute the shortest path to \c t. |
|
766 | 800 |
/// |
767 |
///\return The length of the shortest s---t path if there exists one, |
|
768 |
///0 otherwise. |
|
769 |
///\note Apart from the return value, d.run(s) is |
|
770 |
///just a shortcut of the following code. |
|
801 |
///\return The length of the shortest <tt>s</tt>--<tt>t</tt> path, |
|
802 |
///if \c t is reachable form \c s, \c 0 otherwise. |
|
803 |
/// |
|
804 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is just a |
|
805 |
///shortcut of the following code. |
|
771 | 806 |
///\code |
772 | 807 |
/// d.init(); |
773 | 808 |
/// d.addSource(s); |
774 | 809 |
/// d.start(t); |
775 | 810 |
///\endcode |
776 | 811 |
Value run(Node s,Node t) { |
... | ... |
@@ -782,244 +817,265 @@ |
782 | 817 |
|
783 | 818 |
///@} |
784 | 819 |
|
785 | 820 |
///\name Query Functions |
786 | 821 |
///The result of the %Dijkstra algorithm can be obtained using these |
787 | 822 |
///functions.\n |
788 |
///Before the use of these functions, |
|
789 |
///either run() or start() must be called. |
|
823 |
///Either \ref lemon::Dijkstra::run() "run()" or |
|
824 |
///\ref lemon::Dijkstra::start() "start()" must be called before |
|
825 |
///using them. |
|
790 | 826 |
|
791 | 827 |
///@{ |
792 | 828 |
|
793 |
/// |
|
829 |
///The shortest path to a node. |
|
794 | 830 |
|
795 |
///Gives back the shortest path. |
|
796 |
///\pre The \c t should be reachable from the source. |
|
797 |
Path path(Node t) |
|
798 |
{ |
|
799 |
return Path(*G, *_pred, t); |
|
800 |
} |
|
831 |
///Returns the shortest path to a node. |
|
832 |
/// |
|
833 |
///\warning \c t should be reachable from the root(s). |
|
834 |
/// |
|
835 |
///\pre Either \ref run() or \ref start() must be called before |
|
836 |
///using this function. |
|
837 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
|
801 | 838 |
|
802 |
///The distance of a node from the root. |
|
839 |
///The distance of a node from the root(s). |
|
803 | 840 |
|
804 |
///Returns the distance of a node from the root. |
|
805 |
///\pre \ref run() must be called before using this function. |
|
806 |
///\warning If node \c v in unreachable from the root the return value |
|
807 |
///of this funcion is undefined. |
|
841 |
///Returns the distance of a node from the root(s). |
|
842 |
/// |
|
843 |
///\warning If node \c v is not reachable from the root(s), then |
|
844 |
///the return value of this function is undefined. |
|
845 |
/// |
|
846 |
///\pre Either \ref run() or \ref start() must be called before |
|
847 |
///using this function. |
|
808 | 848 |
Value dist(Node v) const { return (*_dist)[v]; } |
809 | 849 |
|
810 |
/// |
|
850 |
///Returns the 'previous arc' of the shortest path tree for a node. |
|
811 | 851 |
|
812 |
///Returns the current distance of a node from the root. |
|
813 |
///It may be decreased in the following processes. |
|
814 |
///\pre \c node should be reached but not processed |
|
815 |
Value currentDist(Node v) const { return (*_heap)[v]; } |
|
816 |
|
|
817 |
///Returns the 'previous arc' of the shortest path tree. |
|
818 |
|
|
819 |
///For a node \c v it returns the 'previous arc' of the shortest path tree, |
|
820 |
///i.e. it returns the last arc of a shortest path from the root to \c |
|
821 |
///v. It is \ref INVALID |
|
822 |
///if \c v is unreachable from the root or if \c v=s. The |
|
823 |
///shortest path tree used here is equal to the shortest path tree used in |
|
824 |
///\ref predNode(). \pre \ref run() must be called before using |
|
825 |
///this function. |
|
852 |
///This function returns the 'previous arc' of the shortest path |
|
853 |
///tree for the node \c v, i.e. it returns the last arc of a |
|
854 |
///shortest path from the root(s) to \c v. It is \c INVALID if \c v |
|
855 |
///is not reachable from the root(s) or if \c v is a root. |
|
856 |
/// |
|
857 |
///The shortest path tree used here is equal to the shortest path |
|
858 |
///tree used in \ref predNode(). |
|
859 |
/// |
|
860 |
///\pre Either \ref run() or \ref start() must be called before |
|
861 |
///using this function. |
|
826 | 862 |
Arc predArc(Node v) const { return (*_pred)[v]; } |
827 | 863 |
|
828 |
///Returns the 'previous node' of the shortest path tree. |
|
864 |
///Returns the 'previous node' of the shortest path tree for a node. |
|
829 | 865 |
|
830 |
///For a node \c v it returns the 'previous node' of the shortest path tree, |
|
831 |
///i.e. it returns the last but one node from a shortest path from the |
|
832 |
///root to \c /v. It is INVALID if \c v is unreachable from the root or if |
|
833 |
///\c v=s. The shortest path tree used here is equal to the shortest path |
|
834 |
/// |
|
866 |
///This function returns the 'previous node' of the shortest path |
|
867 |
///tree for the node \c v, i.e. it returns the last but one node |
|
868 |
///from a shortest path from the root(s) to \c v. It is \c INVALID |
|
869 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
|
870 |
/// |
|
871 |
///The shortest path tree used here is equal to the shortest path |
|
872 |
///tree used in \ref predArc(). |
|
873 |
/// |
|
874 |
///\pre Either \ref run() or \ref start() must be called before |
|
835 | 875 |
///using this function. |
836 | 876 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
837 | 877 |
G->source((*_pred)[v]); } |
838 | 878 |
|
839 |
///Returns a reference to the NodeMap of distances. |
|
840 |
|
|
841 |
///Returns a reference to the NodeMap of distances. \pre \ref run() must |
|
842 |
///be called before using this function. |
|
879 |
///\brief Returns a const reference to the node map that stores the |
|
880 |
///distances of the nodes. |
|
881 |
/// |
|
882 |
///Returns a const reference to the node map that stores the distances |
|
883 |
///of the nodes calculated by the algorithm. |
|
884 |
/// |
|
885 |
///\pre Either \ref run() or \ref init() |
|
886 |
///must be called before using this function. |
|
843 | 887 |
const DistMap &distMap() const { return *_dist;} |
844 | 888 |
|
845 |
///Returns a reference to the shortest path tree map. |
|
846 |
|
|
847 |
///Returns a reference to the NodeMap of the arcs of the |
|
848 |
///shortest path tree. |
|
849 |
///\ |
|
889 |
///\brief Returns a const reference to the node map that stores the |
|
890 |
///predecessor arcs. |
|
891 |
/// |
|
892 |
///Returns a const reference to the node map that stores the predecessor |
|
893 |
///arcs, which form the shortest path tree. |
|
894 |
/// |
|
895 |
///\pre Either \ref run() or \ref init() |
|
896 |
///must be called before using this function. |
|
850 | 897 |
const PredMap &predMap() const { return *_pred;} |
851 | 898 |
|
852 |
///Checks if a node is reachable from the root. |
|
899 |
///Checks if a node is reachable from the root(s). |
|
853 | 900 |
|
854 |
///Returns \c true if \c v is reachable from the root. |
|
855 |
///\warning The source nodes are inditated as unreached. |
|
856 |
///\pre \ref run() must be called before using this function. |
|
857 |
/// |
|
858 |
|
|
901 |
///Returns \c true if \c v is reachable from the root(s). |
|
902 |
///\pre Either \ref run() or \ref start() |
|
903 |
///must be called before using this function. |
|
904 |
bool reached(Node v) const { return (*_heap_cross_ref)[v] != |
|
905 |
Heap::PRE_HEAP; } |
|
859 | 906 |
|
860 | 907 |
///Checks if a node is processed. |
861 | 908 |
|
862 | 909 |
///Returns \c true if \c v is processed, i.e. the shortest |
863 | 910 |
///path to \c v has already found. |
864 |
///\pre \ref run() must be called before using this function. |
|
865 |
/// |
|
866 |
|
|
911 |
///\pre Either \ref run() or \ref start() |
|
912 |
///must be called before using this function. |
|
913 |
bool processed(Node v) const { return (*_heap_cross_ref)[v] == |
|
914 |
Heap::POST_HEAP; } |
|
915 |
|
|
916 |
///The current distance of a node from the root(s). |
|
917 |
|
|
918 |
///Returns the current distance of a node from the root(s). |
|
919 |
///It may be decreased in the following processes. |
|
920 |
///\pre \c v should be reached but not processed. |
|
921 |
Value currentDist(Node v) const { return (*_heap)[v]; } |
|
867 | 922 |
|
868 | 923 |
///@} |
869 | 924 |
}; |
870 | 925 |
|
871 | 926 |
|
927 |
///Default traits class of dijkstra() function. |
|
872 | 928 |
|
873 |
|
|
874 |
|
|
875 |
///Default traits class of Dijkstra function. |
|
876 |
|
|
877 |
///Default traits class of Dijkstra function. |
|
878 |
///\tparam GR Digraph type. |
|
879 |
/// |
|
929 |
///Default traits class of dijkstra() function. |
|
930 |
///\tparam GR The type of the digraph. |
|
931 |
///\tparam LM The type of the length map. |
|
880 | 932 |
template<class GR, class LM> |
881 | 933 |
struct DijkstraWizardDefaultTraits |
882 | 934 |
{ |
883 |
///The |
|
935 |
///The type of the digraph the algorithm runs on. |
|
884 | 936 |
typedef GR Digraph; |
885 | 937 |
///The type of the map that stores the arc lengths. |
886 | 938 |
|
887 | 939 |
///The type of the map that stores the arc lengths. |
888 | 940 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
889 | 941 |
typedef LM LengthMap; |
890 |
//The type of the length of the arcs. |
|
942 |
///The type of the length of the arcs. |
|
891 | 943 |
typedef typename LM::Value Value; |
944 |
|
|
892 | 945 |
/// Operation traits for Dijkstra algorithm. |
893 | 946 |
|
894 |
/// |
|
947 |
/// This class defines the operations that are used in the algorithm. |
|
895 | 948 |
/// \see DijkstraDefaultOperationTraits |
896 | 949 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
897 |
///The heap type used by Dijkstra algorithm. |
|
898 | 950 |
|
899 |
/// The cross reference type used by heap. |
|
951 |
/// The cross reference type used by the heap. |
|
900 | 952 |
|
901 |
/// The cross reference type used by heap. |
|
953 |
/// The cross reference type used by the heap. |
|
902 | 954 |
/// Usually it is \c Digraph::NodeMap<int>. |
903 | 955 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
904 |
///Instantiates a HeapCrossRef. |
|
956 |
///Instantiates a \ref HeapCrossRef. |
|
905 | 957 |
|
906 | 958 |
///This function instantiates a \ref HeapCrossRef. |
907 |
/// \param |
|
959 |
/// \param g is the digraph, to which we would like to define the |
|
908 | 960 |
/// HeapCrossRef. |
909 | 961 |
/// \todo The digraph alone may be insufficient for the initialization |
910 |
static HeapCrossRef *createHeapCrossRef(const |
|
962 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
|
911 | 963 |
{ |
912 |
return new HeapCrossRef( |
|
964 |
return new HeapCrossRef(g); |
|
913 | 965 |
} |
914 | 966 |
|
915 |
///The heap type used by Dijkstra algorithm. |
|
967 |
///The heap type used by the Dijkstra algorithm. |
|
916 | 968 |
|
917 |
///The heap type used by Dijkstra algorithm. |
|
969 |
///The heap type used by the Dijkstra algorithm. |
|
918 | 970 |
/// |
919 | 971 |
///\sa BinHeap |
920 | 972 |
///\sa Dijkstra |
921 |
typedef BinHeap< |
|
973 |
typedef BinHeap<Value, typename Digraph::template NodeMap<int>, |
|
922 | 974 |
std::less<Value> > Heap; |
923 | 975 |
|
924 |
|
|
976 |
///Instantiates a \ref Heap. |
|
977 |
|
|
978 |
///This function instantiates a \ref Heap. |
|
979 |
/// \param r is the HeapCrossRef which is used. |
|
980 |
static Heap *createHeap(HeapCrossRef& r) |
|
925 | 981 |
{ |
926 |
return new Heap( |
|
982 |
return new Heap(r); |
|
927 | 983 |
} |
928 | 984 |
|
929 |
///\brief The type of the map that stores the |
|
985 |
///\brief The type of the map that stores the predecessor |
|
930 | 986 |
///arcs of the shortest paths. |
931 | 987 |
/// |
932 |
///The type of the map that stores the |
|
988 |
///The type of the map that stores the predecessor |
|
933 | 989 |
///arcs of the shortest paths. |
934 | 990 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
935 |
/// |
|
936 |
typedef NullMap <typename GR::Node,typename GR::Arc> PredMap; |
|
937 |
|
|
991 |
typedef NullMap <typename Digraph::Node,typename Digraph::Arc> PredMap; |
|
992 |
///Instantiates a \ref PredMap. |
|
938 | 993 |
|
939 | 994 |
///This function instantiates a \ref PredMap. |
940 |
///\param g is the digraph, to which we would like to define the PredMap. |
|
941 |
///\todo The digraph alone may be insufficient for the initialization |
|
995 |
///\param g is the digraph, to which we would like to define the |
|
996 |
///\ref PredMap. |
|
997 |
///\todo The digraph alone may be insufficient to initialize |
|
942 | 998 |
#ifdef DOXYGEN |
943 |
static PredMap *createPredMap(const |
|
999 |
static PredMap *createPredMap(const Digraph &g) |
|
944 | 1000 |
#else |
945 |
static PredMap *createPredMap(const |
|
1001 |
static PredMap *createPredMap(const Digraph &) |
|
946 | 1002 |
#endif |
947 | 1003 |
{ |
948 | 1004 |
return new PredMap(); |
949 | 1005 |
} |
950 |
///The type of the map that stores whether a nodes is processed. |
|
951 | 1006 |
|
952 |
///The type of the map that |
|
1007 |
///The type of the map that indicates which nodes are processed. |
|
1008 |
|
|
1009 |
///The type of the map that indicates which nodes are processed. |
|
953 | 1010 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
954 | 1011 |
///By default it is a NullMap. |
955 | 1012 |
///\todo If it is set to a real map, |
956 | 1013 |
///Dijkstra::processed() should read this. |
957 | 1014 |
///\todo named parameter to set this type, function to read and write. |
958 | 1015 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
959 |
///Instantiates a ProcessedMap. |
|
1016 |
///Instantiates a \ref ProcessedMap. |
|
960 | 1017 |
|
961 | 1018 |
///This function instantiates a \ref ProcessedMap. |
962 | 1019 |
///\param g is the digraph, to which |
963 |
///we would like to define the \ref ProcessedMap |
|
1020 |
///we would like to define the \ref ProcessedMap. |
|
964 | 1021 |
#ifdef DOXYGEN |
965 |
static ProcessedMap *createProcessedMap(const |
|
1022 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
|
966 | 1023 |
#else |
967 |
static ProcessedMap *createProcessedMap(const |
|
1024 |
static ProcessedMap *createProcessedMap(const Digraph &) |
|
968 | 1025 |
#endif |
969 | 1026 |
{ |
970 | 1027 |
return new ProcessedMap(); |
971 | 1028 |
} |
972 |
///The type of the map that stores the dists of the nodes. |
|
973 | 1029 |
|
974 |
///The type of the map that stores the |
|
1030 |
///The type of the map that stores the distances of the nodes. |
|
1031 |
|
|
1032 |
///The type of the map that stores the distances of the nodes. |
|
975 | 1033 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
976 |
/// |
|
977 |
typedef NullMap<typename Digraph::Node,typename LM::Value> DistMap; |
|
978 |
|
|
1034 |
typedef NullMap<typename Digraph::Node,Value> DistMap; |
|
1035 |
///Instantiates a \ref DistMap. |
|
979 | 1036 |
|
980 | 1037 |
///This function instantiates a \ref DistMap. |
981 | 1038 |
///\param g is the digraph, to which we would like to define |
982 | 1039 |
///the \ref DistMap |
983 | 1040 |
#ifdef DOXYGEN |
984 |
static DistMap *createDistMap(const |
|
1041 |
static DistMap *createDistMap(const Digraph &g) |
|
985 | 1042 |
#else |
986 |
static DistMap *createDistMap(const |
|
1043 |
static DistMap *createDistMap(const Digraph &) |
|
987 | 1044 |
#endif |
988 | 1045 |
{ |
989 | 1046 |
return new DistMap(); |
990 | 1047 |
} |
991 | 1048 |
}; |
992 | 1049 |
|
993 |
/// Default traits used by \ref DijkstraWizard |
|
1050 |
/// Default traits class used by \ref DijkstraWizard |
|
994 | 1051 |
|
995 | 1052 |
/// To make it easier to use Dijkstra algorithm |
996 | 1053 |
///we have created a wizard class. |
997 | 1054 |
/// This \ref DijkstraWizard class needs default traits, |
998 | 1055 |
///as well as the \ref Dijkstra class. |
999 | 1056 |
/// The \ref DijkstraWizardBase is a class to be the default traits of the |
1000 | 1057 |
/// \ref DijkstraWizard class. |
1001 | 1058 |
/// \todo More named parameters are required... |
1002 | 1059 |
template<class GR,class LM> |
1003 | 1060 |
class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM> |
1004 | 1061 |
{ |
1005 |
|
|
1006 | 1062 |
typedef DijkstraWizardDefaultTraits<GR,LM> Base; |
1007 | 1063 |
protected: |
1008 |
// |
|
1064 |
//The type of the nodes in the digraph. |
|
1009 | 1065 |
typedef typename Base::Digraph::Node Node; |
1010 | 1066 |
|
1011 |
// |
|
1067 |
//Pointer to the digraph the algorithm runs on. |
|
1012 | 1068 |
void *_g; |
1013 |
// |
|
1069 |
//Pointer to the length map |
|
1014 | 1070 |
void *_length; |
1015 |
|
|
1071 |
//Pointer to the map of predecessors arcs. |
|
1016 | 1072 |
void *_pred; |
1017 |
|
|
1073 |
//Pointer to the map of distances. |
|
1018 | 1074 |
void *_dist; |
1019 |
|
|
1075 |
//Pointer to the source node. |
|
1020 | 1076 |
Node _source; |
1021 | 1077 |
|
1022 | 1078 |
public: |
1023 | 1079 |
/// Constructor. |
1024 | 1080 |
|
1025 | 1081 |
/// This constructor does not require parameters, therefore it initiates |
... | ... |
@@ -1029,69 +1085,72 @@ |
1029 | 1085 |
|
1030 | 1086 |
/// Constructor. |
1031 | 1087 |
|
1032 | 1088 |
/// This constructor requires some parameters, |
1033 | 1089 |
/// listed in the parameters list. |
1034 | 1090 |
/// Others are initiated to 0. |
1035 |
/// \param g is the initial value of \ref _g |
|
1036 |
/// \param l is the initial value of \ref _length |
|
1037 |
/// \param |
|
1091 |
/// \param g The digraph the algorithm runs on. |
|
1092 |
/// \param l The length map. |
|
1093 |
/// \param s The source node. |
|
1038 | 1094 |
DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) : |
1039 | 1095 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
1040 | 1096 |
_length(reinterpret_cast<void*>(const_cast<LM*>(&l))), |
1041 | 1097 |
_pred(0), _dist(0), _source(s) {} |
1042 | 1098 |
|
1043 | 1099 |
}; |
1044 | 1100 |
|
1045 |
/// |
|
1101 |
/// Auxiliary class for the function type interface of Dijkstra algorithm. |
|
1046 | 1102 |
|
1047 |
/// This class is created to make it easier to use Dijkstra algorithm. |
|
1048 |
/// It uses the functions and features of the plain \ref Dijkstra, |
|
1049 |
/// |
|
1103 |
/// This auxiliary class is created to implement the function type |
|
1104 |
/// interface of \ref Dijkstra algorithm. It uses the functions and features |
|
1105 |
/// of the plain \ref Dijkstra, but it is much simpler to use it. |
|
1106 |
/// It should only be used through the \ref dijkstra() function, which makes |
|
1107 |
/// it easier to use the algorithm. |
|
1050 | 1108 |
/// |
1051 | 1109 |
/// Simplicity means that the way to change the types defined |
1052 | 1110 |
/// in the traits class is based on functions that returns the new class |
1053 | 1111 |
/// and not on templatable built-in classes. |
1054 | 1112 |
/// When using the plain \ref Dijkstra |
1055 | 1113 |
/// the new class with the modified type comes from |
1056 | 1114 |
/// the original class by using the :: |
1057 | 1115 |
/// operator. In the case of \ref DijkstraWizard only |
1058 |
/// a function have to be called and it will |
|
1116 |
/// a function have to be called, and it will |
|
1059 | 1117 |
/// return the needed class. |
1060 | 1118 |
/// |
1061 |
/// It does not have own \ref run method. When its \ref run method is called |
|
1062 |
/// it initiates a plain \ref Dijkstra class, and calls the \ref |
|
1063 |
/// |
|
1119 |
/// It does not have own \ref run() method. When its \ref run() method |
|
1120 |
/// is called, it initiates a plain \ref Dijkstra object, and calls the |
|
1121 |
/// \ref Dijkstra::run() method of it. |
|
1064 | 1122 |
template<class TR> |
1065 | 1123 |
class DijkstraWizard : public TR |
1066 | 1124 |
{ |
1067 | 1125 |
typedef TR Base; |
1068 | 1126 |
|
1069 |
///The type of the |
|
1127 |
///The type of the digraph the algorithm runs on. |
|
1070 | 1128 |
typedef typename TR::Digraph Digraph; |
1071 |
|
|
1129 |
|
|
1072 | 1130 |
typedef typename Digraph::Node Node; |
1073 |
//\e |
|
1074 | 1131 |
typedef typename Digraph::NodeIt NodeIt; |
1075 |
//\e |
|
1076 | 1132 |
typedef typename Digraph::Arc Arc; |
1077 |
//\e |
|
1078 | 1133 |
typedef typename Digraph::OutArcIt OutArcIt; |
1079 | 1134 |
|
1080 | 1135 |
///The type of the map that stores the arc lengths. |
1081 | 1136 |
typedef typename TR::LengthMap LengthMap; |
1082 | 1137 |
///The type of the length of the arcs. |
1083 | 1138 |
typedef typename LengthMap::Value Value; |
1084 |
///\brief The type of the map that stores the |
|
1139 |
///\brief The type of the map that stores the predecessor |
|
1085 | 1140 |
///arcs of the shortest paths. |
1086 | 1141 |
typedef typename TR::PredMap PredMap; |
1087 |
///The type of the map that stores the |
|
1142 |
///The type of the map that stores the distances of the nodes. |
|
1088 | 1143 |
typedef typename TR::DistMap DistMap; |
1144 |
///The type of the map that indicates which nodes are processed. |
|
1145 |
typedef typename TR::ProcessedMap ProcessedMap; |
|
1089 | 1146 |
///The heap type used by the dijkstra algorithm. |
1090 | 1147 |
typedef typename TR::Heap Heap; |
1148 |
|
|
1091 | 1149 |
public: |
1150 |
|
|
1092 | 1151 |
/// Constructor. |
1093 | 1152 |
DijkstraWizard() : TR() {} |
1094 | 1153 |
|
1095 | 1154 |
/// Constructor that requires parameters. |
1096 | 1155 |
|
1097 | 1156 |
/// Constructor that requires parameters. |
... | ... |
@@ -1101,16 +1160,16 @@ |
1101 | 1160 |
|
1102 | 1161 |
///Copy constructor |
1103 | 1162 |
DijkstraWizard(const TR &b) : TR(b) {} |
1104 | 1163 |
|
1105 | 1164 |
~DijkstraWizard() {} |
1106 | 1165 |
|
1107 |
///Runs Dijkstra algorithm from a |
|
1166 |
///Runs Dijkstra algorithm from a source node. |
|
1108 | 1167 |
|
1109 |
///Runs Dijkstra algorithm from a given node. |
|
1110 |
///The node can be given by the \ref source function. |
|
1168 |
///Runs Dijkstra algorithm from a source node. |
|
1169 |
///The node can be given with the \ref source() function. |
|
1111 | 1170 |
void run() |
1112 | 1171 |
{ |
1113 | 1172 |
if(Base::_source==INVALID) throw UninitializedParameter(); |
1114 | 1173 |
Dijkstra<Digraph,LengthMap,TR> |
1115 | 1174 |
dij(*reinterpret_cast<const Digraph*>(Base::_g), |
1116 | 1175 |
*reinterpret_cast<const LengthMap*>(Base::_length)); |
... | ... |
@@ -1126,62 +1185,76 @@ |
1126 | 1185 |
void run(Node s) |
1127 | 1186 |
{ |
1128 | 1187 |
Base::_source=s; |
1129 | 1188 |
run(); |
1130 | 1189 |
} |
1131 | 1190 |
|
1191 |
/// Sets the source node, from which the Dijkstra algorithm runs. |
|
1192 |
|
|
1193 |
/// Sets the source node, from which the Dijkstra algorithm runs. |
|
1194 |
/// \param s is the source node. |
|
1195 |
DijkstraWizard<TR> &source(Node s) |
|
1196 |
{ |
|
1197 |
Base::_source=s; |
|
1198 |
return *this; |
|
1199 |
} |
|
1200 |
|
|
1132 | 1201 |
template<class T> |
1133 | 1202 |
struct DefPredMapBase : public Base { |
1134 | 1203 |
typedef T PredMap; |
1135 | 1204 |
static PredMap *createPredMap(const Digraph &) { return 0; }; |
1136 | 1205 |
DefPredMapBase(const TR &b) : TR(b) {} |
1137 | 1206 |
}; |
1138 |
|
|
1139 | 1207 |
///\brief \ref named-templ-param "Named parameter" |
1140 |
/// |
|
1208 |
///for setting \ref PredMap object. |
|
1141 | 1209 |
/// |
1142 | 1210 |
/// \ref named-templ-param "Named parameter" |
1143 |
///function for setting PredMap type |
|
1144 |
/// |
|
1211 |
///for setting \ref PredMap object. |
|
1145 | 1212 |
template<class T> |
1146 | 1213 |
DijkstraWizard<DefPredMapBase<T> > predMap(const T &t) |
1147 | 1214 |
{ |
1148 | 1215 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1149 | 1216 |
return DijkstraWizard<DefPredMapBase<T> >(*this); |
1150 | 1217 |
} |
1151 | 1218 |
|
1152 | 1219 |
template<class T> |
1220 |
struct DefProcessedMapBase : public Base { |
|
1221 |
typedef T ProcessedMap; |
|
1222 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
|
1223 |
DefProcessedMapBase(const TR &b) : TR(b) {} |
|
1224 |
}; |
|
1225 |
///\brief \ref named-templ-param "Named parameter" |
|
1226 |
///for setting \ref ProcessedMap object. |
|
1227 |
/// |
|
1228 |
/// \ref named-templ-param "Named parameter" |
|
1229 |
///for setting \ref ProcessedMap object. |
|
1230 |
template<class T> |
|
1231 |
DijkstraWizard<DefProcessedMapBase<T> > processedMap(const T &t) |
|
1232 |
{ |
|
1233 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
|
1234 |
return DijkstraWizard<DefProcessedMapBase<T> >(*this); |
|
1235 |
} |
|
1236 |
|
|
1237 |
template<class T> |
|
1153 | 1238 |
struct DefDistMapBase : public Base { |
1154 | 1239 |
typedef T DistMap; |
1155 | 1240 |
static DistMap *createDistMap(const Digraph &) { return 0; }; |
1156 | 1241 |
DefDistMapBase(const TR &b) : TR(b) {} |
1157 | 1242 |
}; |
1158 |
|
|
1159 | 1243 |
///\brief \ref named-templ-param "Named parameter" |
1160 |
/// |
|
1244 |
///for setting \ref DistMap object. |
|
1161 | 1245 |
/// |
1162 | 1246 |
/// \ref named-templ-param "Named parameter" |
1163 |
///function for setting DistMap type |
|
1164 |
/// |
|
1247 |
///for setting \ref DistMap object. |
|
1165 | 1248 |
template<class T> |
1166 | 1249 |
DijkstraWizard<DefDistMapBase<T> > distMap(const T &t) |
1167 | 1250 |
{ |
1168 | 1251 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
1169 | 1252 |
return DijkstraWizard<DefDistMapBase<T> >(*this); |
1170 | 1253 |
} |
1171 | 1254 |
|
1172 |
/// Sets the source node, from which the Dijkstra algorithm runs. |
|
1173 |
|
|
1174 |
/// Sets the source node, from which the Dijkstra algorithm runs. |
|
1175 |
/// \param s is the source node. |
|
1176 |
DijkstraWizard<TR> &source(Node s) |
|
1177 |
{ |
|
1178 |
Base::_source=s; |
|
1179 |
return *this; |
|
1180 |
} |
|
1181 |
|
|
1182 | 1255 |
}; |
1183 | 1256 |
|
1184 | 1257 |
///Function type interface for Dijkstra algorithm. |
1185 | 1258 |
|
1186 | 1259 |
/// \ingroup shortest_path |
1187 | 1260 |
///Function type interface for Dijkstra algorithm. |
0 comments (0 inline)