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