0
3
0
... | ... |
@@ -59,119 +59,114 @@ |
59 | 59 |
return new PredMap(g); |
60 | 60 |
} |
61 | 61 |
|
62 | 62 |
///The type of the map that indicates which nodes are processed. |
63 | 63 |
|
64 | 64 |
///The type of the map that indicates which nodes are processed. |
65 | 65 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
66 | 66 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
67 | 67 |
///Instantiates a ProcessedMap. |
68 | 68 |
|
69 | 69 |
///This function instantiates a ProcessedMap. |
70 | 70 |
///\param g is the digraph, to which |
71 | 71 |
///we would like to define the ProcessedMap |
72 | 72 |
#ifdef DOXYGEN |
73 | 73 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
74 | 74 |
#else |
75 | 75 |
static ProcessedMap *createProcessedMap(const Digraph &) |
76 | 76 |
#endif |
77 | 77 |
{ |
78 | 78 |
return new ProcessedMap(); |
79 | 79 |
} |
80 | 80 |
|
81 | 81 |
///The type of the map that indicates which nodes are reached. |
82 | 82 |
|
83 |
///The type of the map that indicates which nodes are reached. |
|
83 |
///The type of the map that indicates which nodes are reached. |
|
84 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
84 | 85 |
typedef typename Digraph::template NodeMap<bool> ReachedMap; |
85 | 86 |
///Instantiates a ReachedMap. |
86 | 87 |
|
87 | 88 |
///This function instantiates a ReachedMap. |
88 | 89 |
///\param g is the digraph, to which |
89 | 90 |
///we would like to define the ReachedMap. |
90 | 91 |
static ReachedMap *createReachedMap(const Digraph &g) |
91 | 92 |
{ |
92 | 93 |
return new ReachedMap(g); |
93 | 94 |
} |
94 | 95 |
|
95 | 96 |
///The type of the map that stores the distances of the nodes. |
96 | 97 |
|
97 | 98 |
///The type of the map that stores the distances of the nodes. |
98 | 99 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
99 | 100 |
typedef typename Digraph::template NodeMap<int> DistMap; |
100 | 101 |
///Instantiates a DistMap. |
101 | 102 |
|
102 | 103 |
///This function instantiates a DistMap. |
103 | 104 |
///\param g is the digraph, to which we would like to define the |
104 | 105 |
///DistMap. |
105 | 106 |
static DistMap *createDistMap(const Digraph &g) |
106 | 107 |
{ |
107 | 108 |
return new DistMap(g); |
108 | 109 |
} |
109 | 110 |
}; |
110 | 111 |
|
111 | 112 |
///%BFS algorithm class. |
112 | 113 |
|
113 | 114 |
///\ingroup search |
114 | 115 |
///This class provides an efficient implementation of the %BFS algorithm. |
115 | 116 |
/// |
116 | 117 |
///There is also a \ref bfs() "function-type interface" for the BFS |
117 | 118 |
///algorithm, which is convenient in the simplier cases and it can be |
118 | 119 |
///used easier. |
119 | 120 |
/// |
120 | 121 |
///\tparam GR The type of the digraph the algorithm runs on. |
121 |
///The default value is \ref ListDigraph. The value of GR is not used |
|
122 |
///directly by \ref Bfs, it is only passed to \ref BfsDefaultTraits. |
|
123 |
///\tparam TR Traits class to set various data types used by the algorithm. |
|
124 |
///The default traits class is |
|
125 |
///\ref BfsDefaultTraits "BfsDefaultTraits<GR>". |
|
126 |
///See \ref BfsDefaultTraits for the documentation of |
|
127 |
/// |
|
122 |
///The default type is \ref ListDigraph. |
|
128 | 123 |
#ifdef DOXYGEN |
129 | 124 |
template <typename GR, |
130 | 125 |
typename TR> |
131 | 126 |
#else |
132 | 127 |
template <typename GR=ListDigraph, |
133 | 128 |
typename TR=BfsDefaultTraits<GR> > |
134 | 129 |
#endif |
135 | 130 |
class Bfs { |
136 | 131 |
public: |
137 | 132 |
|
138 | 133 |
///The type of the digraph the algorithm runs on. |
139 | 134 |
typedef typename TR::Digraph Digraph; |
140 | 135 |
|
141 | 136 |
///\brief The type of the map that stores the predecessor arcs of the |
142 | 137 |
///shortest paths. |
143 | 138 |
typedef typename TR::PredMap PredMap; |
144 | 139 |
///The type of the map that stores the distances of the nodes. |
145 | 140 |
typedef typename TR::DistMap DistMap; |
146 | 141 |
///The type of the map that indicates which nodes are reached. |
147 | 142 |
typedef typename TR::ReachedMap ReachedMap; |
148 | 143 |
///The type of the map that indicates which nodes are processed. |
149 | 144 |
typedef typename TR::ProcessedMap ProcessedMap; |
150 | 145 |
///The type of the paths. |
151 | 146 |
typedef PredMapPath<Digraph, PredMap> Path; |
152 | 147 |
|
153 |
///The traits class. |
|
148 |
///The \ref BfsDefaultTraits "traits class" of the algorithm. |
|
154 | 149 |
typedef TR Traits; |
155 | 150 |
|
156 | 151 |
private: |
157 | 152 |
|
158 | 153 |
typedef typename Digraph::Node Node; |
159 | 154 |
typedef typename Digraph::NodeIt NodeIt; |
160 | 155 |
typedef typename Digraph::Arc Arc; |
161 | 156 |
typedef typename Digraph::OutArcIt OutArcIt; |
162 | 157 |
|
163 | 158 |
//Pointer to the underlying digraph. |
164 | 159 |
const Digraph *G; |
165 | 160 |
//Pointer to the map of predecessor arcs. |
166 | 161 |
PredMap *_pred; |
167 | 162 |
//Indicates if _pred is locally allocated (true) or not. |
168 | 163 |
bool local_pred; |
169 | 164 |
//Pointer to the map of distances. |
170 | 165 |
DistMap *_dist; |
171 | 166 |
//Indicates if _dist is locally allocated (true) or not. |
172 | 167 |
bool local_dist; |
173 | 168 |
//Pointer to the map of reached status of the nodes. |
174 | 169 |
ReachedMap *_reached; |
175 | 170 |
//Indicates if _reached is locally allocated (true) or not. |
176 | 171 |
bool local_reached; |
177 | 172 |
//Pointer to the map of processed status of the nodes. |
... | ... |
@@ -191,123 +186,127 @@ |
191 | 186 |
_pred = Traits::createPredMap(*G); |
192 | 187 |
} |
193 | 188 |
if(!_dist) { |
194 | 189 |
local_dist = true; |
195 | 190 |
_dist = Traits::createDistMap(*G); |
196 | 191 |
} |
197 | 192 |
if(!_reached) { |
198 | 193 |
local_reached = true; |
199 | 194 |
_reached = Traits::createReachedMap(*G); |
200 | 195 |
} |
201 | 196 |
if(!_processed) { |
202 | 197 |
local_processed = true; |
203 | 198 |
_processed = Traits::createProcessedMap(*G); |
204 | 199 |
} |
205 | 200 |
} |
206 | 201 |
|
207 | 202 |
protected: |
208 | 203 |
|
209 | 204 |
Bfs() {} |
210 | 205 |
|
211 | 206 |
public: |
212 | 207 |
|
213 | 208 |
typedef Bfs Create; |
214 | 209 |
|
215 |
///\name Named |
|
210 |
///\name Named Template Parameters |
|
216 | 211 |
|
217 | 212 |
///@{ |
218 | 213 |
|
219 | 214 |
template <class T> |
220 | 215 |
struct SetPredMapTraits : public Traits { |
221 | 216 |
typedef T PredMap; |
222 | 217 |
static PredMap *createPredMap(const Digraph &) |
223 | 218 |
{ |
224 | 219 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
225 | 220 |
return 0; // ignore warnings |
226 | 221 |
} |
227 | 222 |
}; |
228 | 223 |
///\brief \ref named-templ-param "Named parameter" for setting |
229 | 224 |
///PredMap type. |
230 | 225 |
/// |
231 | 226 |
///\ref named-templ-param "Named parameter" for setting |
232 | 227 |
///PredMap type. |
228 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
|
233 | 229 |
template <class T> |
234 | 230 |
struct SetPredMap : public Bfs< Digraph, SetPredMapTraits<T> > { |
235 | 231 |
typedef Bfs< Digraph, SetPredMapTraits<T> > Create; |
236 | 232 |
}; |
237 | 233 |
|
238 | 234 |
template <class T> |
239 | 235 |
struct SetDistMapTraits : public Traits { |
240 | 236 |
typedef T DistMap; |
241 | 237 |
static DistMap *createDistMap(const Digraph &) |
242 | 238 |
{ |
243 | 239 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
244 | 240 |
return 0; // ignore warnings |
245 | 241 |
} |
246 | 242 |
}; |
247 | 243 |
///\brief \ref named-templ-param "Named parameter" for setting |
248 | 244 |
///DistMap type. |
249 | 245 |
/// |
250 | 246 |
///\ref named-templ-param "Named parameter" for setting |
251 | 247 |
///DistMap type. |
248 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
|
252 | 249 |
template <class T> |
253 | 250 |
struct SetDistMap : public Bfs< Digraph, SetDistMapTraits<T> > { |
254 | 251 |
typedef Bfs< Digraph, SetDistMapTraits<T> > Create; |
255 | 252 |
}; |
256 | 253 |
|
257 | 254 |
template <class T> |
258 | 255 |
struct SetReachedMapTraits : public Traits { |
259 | 256 |
typedef T ReachedMap; |
260 | 257 |
static ReachedMap *createReachedMap(const Digraph &) |
261 | 258 |
{ |
262 | 259 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
263 | 260 |
return 0; // ignore warnings |
264 | 261 |
} |
265 | 262 |
}; |
266 | 263 |
///\brief \ref named-templ-param "Named parameter" for setting |
267 | 264 |
///ReachedMap type. |
268 | 265 |
/// |
269 | 266 |
///\ref named-templ-param "Named parameter" for setting |
270 | 267 |
///ReachedMap type. |
268 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
271 | 269 |
template <class T> |
272 | 270 |
struct SetReachedMap : public Bfs< Digraph, SetReachedMapTraits<T> > { |
273 | 271 |
typedef Bfs< Digraph, SetReachedMapTraits<T> > Create; |
274 | 272 |
}; |
275 | 273 |
|
276 | 274 |
template <class T> |
277 | 275 |
struct SetProcessedMapTraits : public Traits { |
278 | 276 |
typedef T ProcessedMap; |
279 | 277 |
static ProcessedMap *createProcessedMap(const Digraph &) |
280 | 278 |
{ |
281 | 279 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
282 | 280 |
return 0; // ignore warnings |
283 | 281 |
} |
284 | 282 |
}; |
285 | 283 |
///\brief \ref named-templ-param "Named parameter" for setting |
286 | 284 |
///ProcessedMap type. |
287 | 285 |
/// |
288 | 286 |
///\ref named-templ-param "Named parameter" for setting |
289 | 287 |
///ProcessedMap type. |
288 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
|
290 | 289 |
template <class T> |
291 | 290 |
struct SetProcessedMap : public Bfs< Digraph, SetProcessedMapTraits<T> > { |
292 | 291 |
typedef Bfs< Digraph, SetProcessedMapTraits<T> > Create; |
293 | 292 |
}; |
294 | 293 |
|
295 | 294 |
struct SetStandardProcessedMapTraits : public Traits { |
296 | 295 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
297 | 296 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
298 | 297 |
{ |
299 | 298 |
return new ProcessedMap(g); |
300 | 299 |
return 0; // ignore warnings |
301 | 300 |
} |
302 | 301 |
}; |
303 | 302 |
///\brief \ref named-templ-param "Named parameter" for setting |
304 | 303 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
305 | 304 |
/// |
306 | 305 |
///\ref named-templ-param "Named parameter" for setting |
307 | 306 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
308 | 307 |
///If you don't set it explicitly, it will be automatically allocated. |
309 | 308 |
struct SetStandardProcessedMap : |
310 | 309 |
public Bfs< Digraph, SetStandardProcessedMapTraits > { |
311 | 310 |
typedef Bfs< Digraph, SetStandardProcessedMapTraits > Create; |
312 | 311 |
}; |
313 | 312 |
|
... | ... |
@@ -318,132 +317,133 @@ |
318 | 317 |
///Constructor. |
319 | 318 |
|
320 | 319 |
///Constructor. |
321 | 320 |
///\param g The digraph the algorithm runs on. |
322 | 321 |
Bfs(const Digraph &g) : |
323 | 322 |
G(&g), |
324 | 323 |
_pred(NULL), local_pred(false), |
325 | 324 |
_dist(NULL), local_dist(false), |
326 | 325 |
_reached(NULL), local_reached(false), |
327 | 326 |
_processed(NULL), local_processed(false) |
328 | 327 |
{ } |
329 | 328 |
|
330 | 329 |
///Destructor. |
331 | 330 |
~Bfs() |
332 | 331 |
{ |
333 | 332 |
if(local_pred) delete _pred; |
334 | 333 |
if(local_dist) delete _dist; |
335 | 334 |
if(local_reached) delete _reached; |
336 | 335 |
if(local_processed) delete _processed; |
337 | 336 |
} |
338 | 337 |
|
339 | 338 |
///Sets the map that stores the predecessor arcs. |
340 | 339 |
|
341 | 340 |
///Sets the map that stores the predecessor arcs. |
342 |
///If you don't use this function before calling \ref run(), |
|
343 |
///it will allocate one. The destructor deallocates this |
|
344 |
/// |
|
341 |
///If you don't use this function before calling \ref run(Node) "run()" |
|
342 |
///or \ref init(), an instance will be allocated automatically. |
|
343 |
///The destructor deallocates this automatically allocated map, |
|
344 |
///of course. |
|
345 | 345 |
///\return <tt> (*this) </tt> |
346 | 346 |
Bfs &predMap(PredMap &m) |
347 | 347 |
{ |
348 | 348 |
if(local_pred) { |
349 | 349 |
delete _pred; |
350 | 350 |
local_pred=false; |
351 | 351 |
} |
352 | 352 |
_pred = &m; |
353 | 353 |
return *this; |
354 | 354 |
} |
355 | 355 |
|
356 | 356 |
///Sets the map that indicates which nodes are reached. |
357 | 357 |
|
358 | 358 |
///Sets the map that indicates which nodes are reached. |
359 |
///If you don't use this function before calling \ref run(), |
|
360 |
///it will allocate one. The destructor deallocates this |
|
361 |
/// |
|
359 |
///If you don't use this function before calling \ref run(Node) "run()" |
|
360 |
///or \ref init(), an instance will be allocated automatically. |
|
361 |
///The destructor deallocates this automatically allocated map, |
|
362 |
///of course. |
|
362 | 363 |
///\return <tt> (*this) </tt> |
363 | 364 |
Bfs &reachedMap(ReachedMap &m) |
364 | 365 |
{ |
365 | 366 |
if(local_reached) { |
366 | 367 |
delete _reached; |
367 | 368 |
local_reached=false; |
368 | 369 |
} |
369 | 370 |
_reached = &m; |
370 | 371 |
return *this; |
371 | 372 |
} |
372 | 373 |
|
373 | 374 |
///Sets the map that indicates which nodes are processed. |
374 | 375 |
|
375 | 376 |
///Sets the map that indicates which nodes are processed. |
376 |
///If you don't use this function before calling \ref run(), |
|
377 |
///it will allocate one. The destructor deallocates this |
|
378 |
/// |
|
377 |
///If you don't use this function before calling \ref run(Node) "run()" |
|
378 |
///or \ref init(), an instance will be allocated automatically. |
|
379 |
///The destructor deallocates this automatically allocated map, |
|
380 |
///of course. |
|
379 | 381 |
///\return <tt> (*this) </tt> |
380 | 382 |
Bfs &processedMap(ProcessedMap &m) |
381 | 383 |
{ |
382 | 384 |
if(local_processed) { |
383 | 385 |
delete _processed; |
384 | 386 |
local_processed=false; |
385 | 387 |
} |
386 | 388 |
_processed = &m; |
387 | 389 |
return *this; |
388 | 390 |
} |
389 | 391 |
|
390 | 392 |
///Sets the map that stores the distances of the nodes. |
391 | 393 |
|
392 | 394 |
///Sets the map that stores the distances of the nodes calculated by |
393 | 395 |
///the algorithm. |
394 |
///If you don't use this function before calling \ref run(), |
|
395 |
///it will allocate one. The destructor deallocates this |
|
396 |
/// |
|
396 |
///If you don't use this function before calling \ref run(Node) "run()" |
|
397 |
///or \ref init(), an instance will be allocated automatically. |
|
398 |
///The destructor deallocates this automatically allocated map, |
|
399 |
///of course. |
|
397 | 400 |
///\return <tt> (*this) </tt> |
398 | 401 |
Bfs &distMap(DistMap &m) |
399 | 402 |
{ |
400 | 403 |
if(local_dist) { |
401 | 404 |
delete _dist; |
402 | 405 |
local_dist=false; |
403 | 406 |
} |
404 | 407 |
_dist = &m; |
405 | 408 |
return *this; |
406 | 409 |
} |
407 | 410 |
|
408 | 411 |
public: |
409 | 412 |
|
410 |
///\name Execution control |
|
411 |
///The simplest way to execute the algorithm is to use |
|
412 |
///one of the member functions called \ref lemon::Bfs::run() "run()". |
|
413 |
///\n |
|
414 |
///If you need more control on the execution, first you must call |
|
415 |
///\ref lemon::Bfs::init() "init()", then you can add several source |
|
416 |
///nodes with \ref lemon::Bfs::addSource() "addSource()". |
|
417 |
///Finally \ref lemon::Bfs::start() "start()" will perform the |
|
418 |
/// |
|
413 |
///\name Execution Control |
|
414 |
///The simplest way to execute the BFS algorithm is to use one of the |
|
415 |
///member functions called \ref run(Node) "run()".\n |
|
416 |
///If you need more control on the execution, first you have to call |
|
417 |
///\ref init(), then you can add several source nodes with |
|
418 |
///\ref addSource(). Finally the actual path computation can be |
|
419 |
///performed with one of the \ref start() functions. |
|
419 | 420 |
|
420 | 421 |
///@{ |
421 | 422 |
|
423 |
///\brief Initializes the internal data structures. |
|
424 |
/// |
|
422 | 425 |
///Initializes the internal data structures. |
423 |
|
|
424 |
///Initializes the internal data structures. |
|
425 |
/// |
|
426 | 426 |
void init() |
427 | 427 |
{ |
428 | 428 |
create_maps(); |
429 | 429 |
_queue.resize(countNodes(*G)); |
430 | 430 |
_queue_head=_queue_tail=0; |
431 | 431 |
_curr_dist=1; |
432 | 432 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
433 | 433 |
_pred->set(u,INVALID); |
434 | 434 |
_reached->set(u,false); |
435 | 435 |
_processed->set(u,false); |
436 | 436 |
} |
437 | 437 |
} |
438 | 438 |
|
439 | 439 |
///Adds a new source node. |
440 | 440 |
|
441 | 441 |
///Adds a new source node to the set of nodes to be processed. |
442 | 442 |
/// |
443 | 443 |
void addSource(Node s) |
444 | 444 |
{ |
445 | 445 |
if(!(*_reached)[s]) |
446 | 446 |
{ |
447 | 447 |
_reached->set(s,true); |
448 | 448 |
_pred->set(s,INVALID); |
449 | 449 |
_dist->set(s,0); |
... | ... |
@@ -535,58 +535,58 @@ |
535 | 535 |
} |
536 | 536 |
Node n=_queue[_queue_tail++]; |
537 | 537 |
_processed->set(n,true); |
538 | 538 |
Node m; |
539 | 539 |
for(OutArcIt e(*G,n);e!=INVALID;++e) |
540 | 540 |
if(!(*_reached)[m=G->target(e)]) { |
541 | 541 |
_queue[_queue_head++]=m; |
542 | 542 |
_reached->set(m,true); |
543 | 543 |
_pred->set(m,e); |
544 | 544 |
_dist->set(m,_curr_dist); |
545 | 545 |
if (nm[m] && rnode == INVALID) rnode = m; |
546 | 546 |
} |
547 | 547 |
return n; |
548 | 548 |
} |
549 | 549 |
|
550 | 550 |
///The next node to be processed. |
551 | 551 |
|
552 | 552 |
///Returns the next node to be processed or \c INVALID if the queue |
553 | 553 |
///is empty. |
554 | 554 |
Node nextNode() const |
555 | 555 |
{ |
556 | 556 |
return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID; |
557 | 557 |
} |
558 | 558 |
|
559 |
///\brief Returns \c false if there are nodes |
|
560 |
///to be processed. |
|
561 |
/// |
|
562 |
///Returns \c false if there are nodes |
|
563 |
///to be processed |
|
559 |
///Returns \c false if there are nodes to be processed. |
|
560 |
|
|
561 |
///Returns \c false if there are nodes to be processed |
|
562 |
///in the queue. |
|
564 | 563 |
bool emptyQueue() const { return _queue_tail==_queue_head; } |
565 | 564 |
|
566 | 565 |
///Returns the number of the nodes to be processed. |
567 | 566 |
|
568 |
///Returns the number of the nodes to be processed |
|
567 |
///Returns the number of the nodes to be processed |
|
568 |
///in the queue. |
|
569 | 569 |
int queueSize() const { return _queue_head-_queue_tail; } |
570 | 570 |
|
571 | 571 |
///Executes the algorithm. |
572 | 572 |
|
573 | 573 |
///Executes the algorithm. |
574 | 574 |
/// |
575 | 575 |
///This method runs the %BFS algorithm from the root node(s) |
576 | 576 |
///in order to compute the shortest path to each node. |
577 | 577 |
/// |
578 | 578 |
///The algorithm computes |
579 | 579 |
///- the shortest path tree (forest), |
580 | 580 |
///- the distance of each node from the root(s). |
581 | 581 |
/// |
582 | 582 |
///\pre init() must be called and at least one root node should be |
583 | 583 |
///added with addSource() before using this function. |
584 | 584 |
/// |
585 | 585 |
///\note <tt>b.start()</tt> is just a shortcut of the following code. |
586 | 586 |
///\code |
587 | 587 |
/// while ( !b.emptyQueue() ) { |
588 | 588 |
/// b.processNextNode(); |
589 | 589 |
/// } |
590 | 590 |
///\endcode |
591 | 591 |
void start() |
592 | 592 |
{ |
... | ... |
@@ -709,129 +709,130 @@ |
709 | 709 |
/// |
710 | 710 |
///\note <tt>b.run(s)</tt> is just a shortcut of the following code. |
711 | 711 |
///\code |
712 | 712 |
/// b.init(); |
713 | 713 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
714 | 714 |
/// if (!b.reached(n)) { |
715 | 715 |
/// b.addSource(n); |
716 | 716 |
/// b.start(); |
717 | 717 |
/// } |
718 | 718 |
/// } |
719 | 719 |
///\endcode |
720 | 720 |
void run() { |
721 | 721 |
init(); |
722 | 722 |
for (NodeIt n(*G); n != INVALID; ++n) { |
723 | 723 |
if (!reached(n)) { |
724 | 724 |
addSource(n); |
725 | 725 |
start(); |
726 | 726 |
} |
727 | 727 |
} |
728 | 728 |
} |
729 | 729 |
|
730 | 730 |
///@} |
731 | 731 |
|
732 | 732 |
///\name Query Functions |
733 |
///The |
|
733 |
///The results of the BFS algorithm can be obtained using these |
|
734 | 734 |
///functions.\n |
735 |
///Either \ref lemon::Bfs::run() "run()" or \ref lemon::Bfs::start() |
|
736 |
///"start()" must be called before using them. |
|
735 |
///Either \ref run(Node) "run()" or \ref start() should be called |
|
736 |
///before using them. |
|
737 | 737 |
|
738 | 738 |
///@{ |
739 | 739 |
|
740 | 740 |
///The shortest path to a node. |
741 | 741 |
|
742 | 742 |
///Returns the shortest path to a node. |
743 | 743 |
/// |
744 |
///\warning \c t should be |
|
744 |
///\warning \c t should be reached from the root(s). |
|
745 | 745 |
/// |
746 |
///\pre Either \ref run() or \ref start() must be called before |
|
747 |
///using this function. |
|
746 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
747 |
///must be called before using this function. |
|
748 | 748 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
749 | 749 |
|
750 | 750 |
///The distance of a node from the root(s). |
751 | 751 |
|
752 | 752 |
///Returns the distance of a node from the root(s). |
753 | 753 |
/// |
754 |
///\warning If node \c v is not |
|
754 |
///\warning If node \c v is not reached from the root(s), then |
|
755 | 755 |
///the return value of this function is undefined. |
756 | 756 |
/// |
757 |
///\pre Either \ref run() or \ref start() must be called before |
|
758 |
///using this function. |
|
757 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
758 |
///must be called before using this function. |
|
759 | 759 |
int dist(Node v) const { return (*_dist)[v]; } |
760 | 760 |
|
761 | 761 |
///Returns the 'previous arc' of the shortest path tree for a node. |
762 | 762 |
|
763 | 763 |
///This function returns the 'previous arc' of the shortest path |
764 | 764 |
///tree for the node \c v, i.e. it returns the last arc of a |
765 |
///shortest path from the root(s) to \c v. It is \c INVALID if \c v |
|
766 |
///is not reachable from the root(s) or if \c v is a root. |
|
765 |
///shortest path from a root to \c v. It is \c INVALID if \c v |
|
766 |
///is not reached from the root(s) or if \c v is a root. |
|
767 | 767 |
/// |
768 | 768 |
///The shortest path tree used here is equal to the shortest path |
769 | 769 |
///tree used in \ref predNode(). |
770 | 770 |
/// |
771 |
///\pre Either \ref run() or \ref start() must be called before |
|
772 |
///using this function. |
|
771 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
772 |
///must be called before using this function. |
|
773 | 773 |
Arc predArc(Node v) const { return (*_pred)[v];} |
774 | 774 |
|
775 | 775 |
///Returns the 'previous node' of the shortest path tree for a node. |
776 | 776 |
|
777 | 777 |
///This function returns the 'previous node' of the shortest path |
778 | 778 |
///tree for the node \c v, i.e. it returns the last but one node |
779 |
///from a shortest path from the root(s) to \c v. It is \c INVALID |
|
780 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
|
779 |
///from a shortest path from a root to \c v. It is \c INVALID |
|
780 |
///if \c v is not reached from the root(s) or if \c v is a root. |
|
781 | 781 |
/// |
782 | 782 |
///The shortest path tree used here is equal to the shortest path |
783 | 783 |
///tree used in \ref predArc(). |
784 | 784 |
/// |
785 |
///\pre Either \ref run() or \ref start() must be called before |
|
786 |
///using this function. |
|
785 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
786 |
///must be called before using this function. |
|
787 | 787 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
788 | 788 |
G->source((*_pred)[v]); } |
789 | 789 |
|
790 | 790 |
///\brief Returns a const reference to the node map that stores the |
791 | 791 |
/// distances of the nodes. |
792 | 792 |
/// |
793 | 793 |
///Returns a const reference to the node map that stores the distances |
794 | 794 |
///of the nodes calculated by the algorithm. |
795 | 795 |
/// |
796 |
///\pre Either \ref run() or \ref init() |
|
796 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
797 | 797 |
///must be called before using this function. |
798 | 798 |
const DistMap &distMap() const { return *_dist;} |
799 | 799 |
|
800 | 800 |
///\brief Returns a const reference to the node map that stores the |
801 | 801 |
///predecessor arcs. |
802 | 802 |
/// |
803 | 803 |
///Returns a const reference to the node map that stores the predecessor |
804 | 804 |
///arcs, which form the shortest path tree. |
805 | 805 |
/// |
806 |
///\pre Either \ref run() or \ref init() |
|
806 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
807 | 807 |
///must be called before using this function. |
808 | 808 |
const PredMap &predMap() const { return *_pred;} |
809 | 809 |
|
810 |
///Checks if a node is |
|
810 |
///Checks if a node is reached from the root(s). |
|
811 | 811 |
|
812 |
///Returns \c true if \c v is reachable from the root(s). |
|
813 |
///\pre Either \ref run() or \ref start() |
|
812 |
///Returns \c true if \c v is reached from the root(s). |
|
813 |
/// |
|
814 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
814 | 815 |
///must be called before using this function. |
815 | 816 |
bool reached(Node v) const { return (*_reached)[v]; } |
816 | 817 |
|
817 | 818 |
///@} |
818 | 819 |
}; |
819 | 820 |
|
820 | 821 |
///Default traits class of bfs() function. |
821 | 822 |
|
822 | 823 |
///Default traits class of bfs() function. |
823 | 824 |
///\tparam GR Digraph type. |
824 | 825 |
template<class GR> |
825 | 826 |
struct BfsWizardDefaultTraits |
826 | 827 |
{ |
827 | 828 |
///The type of the digraph the algorithm runs on. |
828 | 829 |
typedef GR Digraph; |
829 | 830 |
|
830 | 831 |
///\brief The type of the map that stores the predecessor |
831 | 832 |
///arcs of the shortest paths. |
832 | 833 |
/// |
833 | 834 |
///The type of the map that stores the predecessor |
834 | 835 |
///arcs of the shortest paths. |
835 | 836 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
836 | 837 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
837 | 838 |
///Instantiates a PredMap. |
... | ... |
@@ -935,50 +936,50 @@ |
935 | 936 |
|
936 | 937 |
public: |
937 | 938 |
/// Constructor. |
938 | 939 |
|
939 | 940 |
/// This constructor does not require parameters, therefore it initiates |
940 | 941 |
/// all of the attributes to \c 0. |
941 | 942 |
BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
942 | 943 |
_dist(0), _path(0), _di(0) {} |
943 | 944 |
|
944 | 945 |
/// Constructor. |
945 | 946 |
|
946 | 947 |
/// This constructor requires one parameter, |
947 | 948 |
/// others are initiated to \c 0. |
948 | 949 |
/// \param g The digraph the algorithm runs on. |
949 | 950 |
BfsWizardBase(const GR &g) : |
950 | 951 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
951 | 952 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
952 | 953 |
|
953 | 954 |
}; |
954 | 955 |
|
955 | 956 |
/// Auxiliary class for the function-type interface of BFS algorithm. |
956 | 957 |
|
957 | 958 |
/// This auxiliary class is created to implement the |
958 | 959 |
/// \ref bfs() "function-type interface" of \ref Bfs algorithm. |
959 |
/// It does not have own \ref run() method, it uses the functions |
|
960 |
/// and features of the plain \ref Bfs. |
|
960 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
|
961 |
/// functions and features of the plain \ref Bfs. |
|
961 | 962 |
/// |
962 | 963 |
/// This class should only be used through the \ref bfs() function, |
963 | 964 |
/// which makes it easier to use the algorithm. |
964 | 965 |
template<class TR> |
965 | 966 |
class BfsWizard : public TR |
966 | 967 |
{ |
967 | 968 |
typedef TR Base; |
968 | 969 |
|
969 | 970 |
///The type of the digraph the algorithm runs on. |
970 | 971 |
typedef typename TR::Digraph Digraph; |
971 | 972 |
|
972 | 973 |
typedef typename Digraph::Node Node; |
973 | 974 |
typedef typename Digraph::NodeIt NodeIt; |
974 | 975 |
typedef typename Digraph::Arc Arc; |
975 | 976 |
typedef typename Digraph::OutArcIt OutArcIt; |
976 | 977 |
|
977 | 978 |
///\brief The type of the map that stores the predecessor |
978 | 979 |
///arcs of the shortest paths. |
979 | 980 |
typedef typename TR::PredMap PredMap; |
980 | 981 |
///\brief The type of the map that stores the distances of the nodes. |
981 | 982 |
typedef typename TR::DistMap DistMap; |
982 | 983 |
///\brief The type of the map that indicates which nodes are reached. |
983 | 984 |
typedef typename TR::ReachedMap ReachedMap; |
984 | 985 |
///\brief The type of the map that indicates which nodes are processed. |
... | ... |
@@ -1156,49 +1157,49 @@ |
1156 | 1157 |
///for getting the distance of the target node. |
1157 | 1158 |
BfsWizard dist(const int &d) |
1158 | 1159 |
{ |
1159 | 1160 |
Base::_di=const_cast<int*>(&d); |
1160 | 1161 |
return *this; |
1161 | 1162 |
} |
1162 | 1163 |
|
1163 | 1164 |
}; |
1164 | 1165 |
|
1165 | 1166 |
///Function-type interface for BFS algorithm. |
1166 | 1167 |
|
1167 | 1168 |
/// \ingroup search |
1168 | 1169 |
///Function-type interface for BFS algorithm. |
1169 | 1170 |
/// |
1170 | 1171 |
///This function also has several \ref named-func-param "named parameters", |
1171 | 1172 |
///they are declared as the members of class \ref BfsWizard. |
1172 | 1173 |
///The following examples show how to use these parameters. |
1173 | 1174 |
///\code |
1174 | 1175 |
/// // Compute shortest path from node s to each node |
1175 | 1176 |
/// bfs(g).predMap(preds).distMap(dists).run(s); |
1176 | 1177 |
/// |
1177 | 1178 |
/// // Compute shortest path from s to t |
1178 | 1179 |
/// bool reached = bfs(g).path(p).dist(d).run(s,t); |
1179 | 1180 |
///\endcode |
1180 |
///\warning Don't forget to put the \ref BfsWizard::run() "run()" |
|
1181 |
///\warning Don't forget to put the \ref BfsWizard::run(Node) "run()" |
|
1181 | 1182 |
///to the end of the parameter list. |
1182 | 1183 |
///\sa BfsWizard |
1183 | 1184 |
///\sa Bfs |
1184 | 1185 |
template<class GR> |
1185 | 1186 |
BfsWizard<BfsWizardBase<GR> > |
1186 | 1187 |
bfs(const GR &digraph) |
1187 | 1188 |
{ |
1188 | 1189 |
return BfsWizard<BfsWizardBase<GR> >(digraph); |
1189 | 1190 |
} |
1190 | 1191 |
|
1191 | 1192 |
#ifdef DOXYGEN |
1192 | 1193 |
/// \brief Visitor class for BFS. |
1193 | 1194 |
/// |
1194 | 1195 |
/// This class defines the interface of the BfsVisit events, and |
1195 | 1196 |
/// it could be the base of a real visitor class. |
1196 | 1197 |
template <typename _Digraph> |
1197 | 1198 |
struct BfsVisitor { |
1198 | 1199 |
typedef _Digraph Digraph; |
1199 | 1200 |
typedef typename Digraph::Arc Arc; |
1200 | 1201 |
typedef typename Digraph::Node Node; |
1201 | 1202 |
/// \brief Called for the source node(s) of the BFS. |
1202 | 1203 |
/// |
1203 | 1204 |
/// This function is called for the source node(s) of the BFS. |
1204 | 1205 |
void start(const Node& node) {} |
... | ... |
@@ -1342,115 +1343,113 @@ |
1342 | 1343 |
//Pointer to the map of reached status of the nodes. |
1343 | 1344 |
ReachedMap *_reached; |
1344 | 1345 |
//Indicates if _reached is locally allocated (true) or not. |
1345 | 1346 |
bool local_reached; |
1346 | 1347 |
|
1347 | 1348 |
std::vector<typename Digraph::Node> _list; |
1348 | 1349 |
int _list_front, _list_back; |
1349 | 1350 |
|
1350 | 1351 |
//Creates the maps if necessary. |
1351 | 1352 |
void create_maps() { |
1352 | 1353 |
if(!_reached) { |
1353 | 1354 |
local_reached = true; |
1354 | 1355 |
_reached = Traits::createReachedMap(*_digraph); |
1355 | 1356 |
} |
1356 | 1357 |
} |
1357 | 1358 |
|
1358 | 1359 |
protected: |
1359 | 1360 |
|
1360 | 1361 |
BfsVisit() {} |
1361 | 1362 |
|
1362 | 1363 |
public: |
1363 | 1364 |
|
1364 | 1365 |
typedef BfsVisit Create; |
1365 | 1366 |
|
1366 |
/// \name Named |
|
1367 |
/// \name Named Template Parameters |
|
1367 | 1368 |
|
1368 | 1369 |
///@{ |
1369 | 1370 |
template <class T> |
1370 | 1371 |
struct SetReachedMapTraits : public Traits { |
1371 | 1372 |
typedef T ReachedMap; |
1372 | 1373 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1373 | 1374 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
1374 | 1375 |
return 0; // ignore warnings |
1375 | 1376 |
} |
1376 | 1377 |
}; |
1377 | 1378 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1378 | 1379 |
/// ReachedMap type. |
1379 | 1380 |
/// |
1380 | 1381 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
1381 | 1382 |
template <class T> |
1382 | 1383 |
struct SetReachedMap : public BfsVisit< Digraph, Visitor, |
1383 | 1384 |
SetReachedMapTraits<T> > { |
1384 | 1385 |
typedef BfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
1385 | 1386 |
}; |
1386 | 1387 |
///@} |
1387 | 1388 |
|
1388 | 1389 |
public: |
1389 | 1390 |
|
1390 | 1391 |
/// \brief Constructor. |
1391 | 1392 |
/// |
1392 | 1393 |
/// Constructor. |
1393 | 1394 |
/// |
1394 | 1395 |
/// \param digraph The digraph the algorithm runs on. |
1395 | 1396 |
/// \param visitor The visitor object of the algorithm. |
1396 | 1397 |
BfsVisit(const Digraph& digraph, Visitor& visitor) |
1397 | 1398 |
: _digraph(&digraph), _visitor(&visitor), |
1398 | 1399 |
_reached(0), local_reached(false) {} |
1399 | 1400 |
|
1400 | 1401 |
/// \brief Destructor. |
1401 | 1402 |
~BfsVisit() { |
1402 | 1403 |
if(local_reached) delete _reached; |
1403 | 1404 |
} |
1404 | 1405 |
|
1405 | 1406 |
/// \brief Sets the map that indicates which nodes are reached. |
1406 | 1407 |
/// |
1407 | 1408 |
/// Sets the map that indicates which nodes are reached. |
1408 |
/// If you don't use this function before calling \ref run(), |
|
1409 |
/// it will allocate one. The destructor deallocates this |
|
1410 |
/// |
|
1409 |
/// If you don't use this function before calling \ref run(Node) "run()" |
|
1410 |
/// or \ref init(), an instance will be allocated automatically. |
|
1411 |
/// The destructor deallocates this automatically allocated map, |
|
1412 |
/// of course. |
|
1411 | 1413 |
/// \return <tt> (*this) </tt> |
1412 | 1414 |
BfsVisit &reachedMap(ReachedMap &m) { |
1413 | 1415 |
if(local_reached) { |
1414 | 1416 |
delete _reached; |
1415 | 1417 |
local_reached = false; |
1416 | 1418 |
} |
1417 | 1419 |
_reached = &m; |
1418 | 1420 |
return *this; |
1419 | 1421 |
} |
1420 | 1422 |
|
1421 | 1423 |
public: |
1422 | 1424 |
|
1423 |
/// \name Execution control |
|
1424 |
/// The simplest way to execute the algorithm is to use |
|
1425 |
/// one of the member functions called \ref lemon::BfsVisit::run() |
|
1426 |
/// "run()". |
|
1427 |
/// \n |
|
1428 |
/// If you need more control on the execution, first you must call |
|
1429 |
/// \ref lemon::BfsVisit::init() "init()", then you can add several |
|
1430 |
/// source nodes with \ref lemon::BfsVisit::addSource() "addSource()". |
|
1431 |
/// Finally \ref lemon::BfsVisit::start() "start()" will perform the |
|
1432 |
/// actual path computation. |
|
1425 |
/// \name Execution Control |
|
1426 |
/// The simplest way to execute the BFS algorithm is to use one of the |
|
1427 |
/// member functions called \ref run(Node) "run()".\n |
|
1428 |
/// If you need more control on the execution, first you have to call |
|
1429 |
/// \ref init(), then you can add several source nodes with |
|
1430 |
/// \ref addSource(). Finally the actual path computation can be |
|
1431 |
/// performed with one of the \ref start() functions. |
|
1433 | 1432 |
|
1434 | 1433 |
/// @{ |
1435 | 1434 |
|
1436 | 1435 |
/// \brief Initializes the internal data structures. |
1437 | 1436 |
/// |
1438 | 1437 |
/// Initializes the internal data structures. |
1439 | 1438 |
void init() { |
1440 | 1439 |
create_maps(); |
1441 | 1440 |
_list.resize(countNodes(*_digraph)); |
1442 | 1441 |
_list_front = _list_back = -1; |
1443 | 1442 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1444 | 1443 |
_reached->set(u, false); |
1445 | 1444 |
} |
1446 | 1445 |
} |
1447 | 1446 |
|
1448 | 1447 |
/// \brief Adds a new source node. |
1449 | 1448 |
/// |
1450 | 1449 |
/// Adds a new source node to the set of nodes to be processed. |
1451 | 1450 |
void addSource(Node s) { |
1452 | 1451 |
if(!(*_reached)[s]) { |
1453 | 1452 |
_reached->set(s,true); |
1454 | 1453 |
_visitor->start(s); |
1455 | 1454 |
_visitor->reach(s); |
1456 | 1455 |
_list[++_list_back] = s; |
... | ... |
@@ -1708,45 +1707,46 @@ |
1708 | 1707 |
/// |
1709 | 1708 |
/// \note <tt>b.run(s)</tt> is just a shortcut of the following code. |
1710 | 1709 |
///\code |
1711 | 1710 |
/// b.init(); |
1712 | 1711 |
/// for (NodeIt n(gr); n != INVALID; ++n) { |
1713 | 1712 |
/// if (!b.reached(n)) { |
1714 | 1713 |
/// b.addSource(n); |
1715 | 1714 |
/// b.start(); |
1716 | 1715 |
/// } |
1717 | 1716 |
/// } |
1718 | 1717 |
///\endcode |
1719 | 1718 |
void run() { |
1720 | 1719 |
init(); |
1721 | 1720 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1722 | 1721 |
if (!reached(it)) { |
1723 | 1722 |
addSource(it); |
1724 | 1723 |
start(); |
1725 | 1724 |
} |
1726 | 1725 |
} |
1727 | 1726 |
} |
1728 | 1727 |
|
1729 | 1728 |
///@} |
1730 | 1729 |
|
1731 | 1730 |
/// \name Query Functions |
1732 |
/// The |
|
1731 |
/// The results of the BFS algorithm can be obtained using these |
|
1733 | 1732 |
/// functions.\n |
1734 |
/// Either \ref lemon::BfsVisit::run() "run()" or |
|
1735 |
/// \ref lemon::BfsVisit::start() "start()" must be called before |
|
1736 |
/// |
|
1733 |
/// Either \ref run(Node) "run()" or \ref start() should be called |
|
1734 |
/// before using them. |
|
1735 |
|
|
1737 | 1736 |
///@{ |
1738 | 1737 |
|
1739 |
/// \brief Checks if a node is |
|
1738 |
/// \brief Checks if a node is reached from the root(s). |
|
1740 | 1739 |
/// |
1741 |
/// Returns \c true if \c v is reachable from the root(s). |
|
1742 |
/// \pre Either \ref run() or \ref start() |
|
1740 |
/// Returns \c true if \c v is reached from the root(s). |
|
1741 |
/// |
|
1742 |
/// \pre Either \ref run(Node) "run()" or \ref init() |
|
1743 | 1743 |
/// must be called before using this function. |
1744 | 1744 |
bool reached(Node v) { return (*_reached)[v]; } |
1745 | 1745 |
|
1746 | 1746 |
///@} |
1747 | 1747 |
|
1748 | 1748 |
}; |
1749 | 1749 |
|
1750 | 1750 |
} //END OF NAMESPACE LEMON |
1751 | 1751 |
|
1752 | 1752 |
#endif |
... | ... |
@@ -98,81 +98,75 @@ |
98 | 98 |
///The type of the map that stores the distances of the nodes. |
99 | 99 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
100 | 100 |
typedef typename Digraph::template NodeMap<int> DistMap; |
101 | 101 |
///Instantiates a DistMap. |
102 | 102 |
|
103 | 103 |
///This function instantiates a DistMap. |
104 | 104 |
///\param g is the digraph, to which we would like to define the |
105 | 105 |
///DistMap. |
106 | 106 |
static DistMap *createDistMap(const Digraph &g) |
107 | 107 |
{ |
108 | 108 |
return new DistMap(g); |
109 | 109 |
} |
110 | 110 |
}; |
111 | 111 |
|
112 | 112 |
///%DFS algorithm class. |
113 | 113 |
|
114 | 114 |
///\ingroup search |
115 | 115 |
///This class provides an efficient implementation of the %DFS algorithm. |
116 | 116 |
/// |
117 | 117 |
///There is also a \ref dfs() "function-type interface" for the DFS |
118 | 118 |
///algorithm, which is convenient in the simplier cases and it can be |
119 | 119 |
///used easier. |
120 | 120 |
/// |
121 | 121 |
///\tparam GR The type of the digraph the algorithm runs on. |
122 |
///The default value is \ref ListDigraph. The value of GR is not used |
|
123 |
///directly by \ref Dfs, it is only passed to \ref DfsDefaultTraits. |
|
124 |
///\tparam TR Traits class to set various data types used by the algorithm. |
|
125 |
///The default traits class is |
|
126 |
///\ref DfsDefaultTraits "DfsDefaultTraits<GR>". |
|
127 |
///See \ref DfsDefaultTraits for the documentation of |
|
128 |
/// |
|
122 |
///The default type is \ref ListDigraph. |
|
129 | 123 |
#ifdef DOXYGEN |
130 | 124 |
template <typename GR, |
131 | 125 |
typename TR> |
132 | 126 |
#else |
133 | 127 |
template <typename GR=ListDigraph, |
134 | 128 |
typename TR=DfsDefaultTraits<GR> > |
135 | 129 |
#endif |
136 | 130 |
class Dfs { |
137 | 131 |
public: |
138 | 132 |
|
139 | 133 |
///The type of the digraph the algorithm runs on. |
140 | 134 |
typedef typename TR::Digraph Digraph; |
141 | 135 |
|
142 | 136 |
///\brief The type of the map that stores the predecessor arcs of the |
143 | 137 |
///DFS paths. |
144 | 138 |
typedef typename TR::PredMap PredMap; |
145 | 139 |
///The type of the map that stores the distances of the nodes. |
146 | 140 |
typedef typename TR::DistMap DistMap; |
147 | 141 |
///The type of the map that indicates which nodes are reached. |
148 | 142 |
typedef typename TR::ReachedMap ReachedMap; |
149 | 143 |
///The type of the map that indicates which nodes are processed. |
150 | 144 |
typedef typename TR::ProcessedMap ProcessedMap; |
151 | 145 |
///The type of the paths. |
152 | 146 |
typedef PredMapPath<Digraph, PredMap> Path; |
153 | 147 |
|
154 |
///The traits class. |
|
148 |
///The \ref DfsDefaultTraits "traits class" of the algorithm. |
|
155 | 149 |
typedef TR Traits; |
156 | 150 |
|
157 | 151 |
private: |
158 | 152 |
|
159 | 153 |
typedef typename Digraph::Node Node; |
160 | 154 |
typedef typename Digraph::NodeIt NodeIt; |
161 | 155 |
typedef typename Digraph::Arc Arc; |
162 | 156 |
typedef typename Digraph::OutArcIt OutArcIt; |
163 | 157 |
|
164 | 158 |
//Pointer to the underlying digraph. |
165 | 159 |
const Digraph *G; |
166 | 160 |
//Pointer to the map of predecessor arcs. |
167 | 161 |
PredMap *_pred; |
168 | 162 |
//Indicates if _pred is locally allocated (true) or not. |
169 | 163 |
bool local_pred; |
170 | 164 |
//Pointer to the map of distances. |
171 | 165 |
DistMap *_dist; |
172 | 166 |
//Indicates if _dist is locally allocated (true) or not. |
173 | 167 |
bool local_dist; |
174 | 168 |
//Pointer to the map of reached status of the nodes. |
175 | 169 |
ReachedMap *_reached; |
176 | 170 |
//Indicates if _reached is locally allocated (true) or not. |
177 | 171 |
bool local_reached; |
178 | 172 |
//Pointer to the map of processed status of the nodes. |
... | ... |
@@ -209,105 +203,109 @@ |
209 | 203 |
Dfs() {} |
210 | 204 |
|
211 | 205 |
public: |
212 | 206 |
|
213 | 207 |
typedef Dfs Create; |
214 | 208 |
|
215 | 209 |
///\name Named template parameters |
216 | 210 |
|
217 | 211 |
///@{ |
218 | 212 |
|
219 | 213 |
template <class T> |
220 | 214 |
struct SetPredMapTraits : public Traits { |
221 | 215 |
typedef T PredMap; |
222 | 216 |
static PredMap *createPredMap(const Digraph &) |
223 | 217 |
{ |
224 | 218 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
225 | 219 |
return 0; // ignore warnings |
226 | 220 |
} |
227 | 221 |
}; |
228 | 222 |
///\brief \ref named-templ-param "Named parameter" for setting |
229 | 223 |
///PredMap type. |
230 | 224 |
/// |
231 | 225 |
///\ref named-templ-param "Named parameter" for setting |
232 | 226 |
///PredMap type. |
227 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
|
233 | 228 |
template <class T> |
234 | 229 |
struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > { |
235 | 230 |
typedef Dfs<Digraph, SetPredMapTraits<T> > Create; |
236 | 231 |
}; |
237 | 232 |
|
238 | 233 |
template <class T> |
239 | 234 |
struct SetDistMapTraits : public Traits { |
240 | 235 |
typedef T DistMap; |
241 | 236 |
static DistMap *createDistMap(const Digraph &) |
242 | 237 |
{ |
243 | 238 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
244 | 239 |
return 0; // ignore warnings |
245 | 240 |
} |
246 | 241 |
}; |
247 | 242 |
///\brief \ref named-templ-param "Named parameter" for setting |
248 | 243 |
///DistMap type. |
249 | 244 |
/// |
250 | 245 |
///\ref named-templ-param "Named parameter" for setting |
251 | 246 |
///DistMap type. |
247 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
|
252 | 248 |
template <class T> |
253 | 249 |
struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > { |
254 | 250 |
typedef Dfs<Digraph, SetDistMapTraits<T> > Create; |
255 | 251 |
}; |
256 | 252 |
|
257 | 253 |
template <class T> |
258 | 254 |
struct SetReachedMapTraits : public Traits { |
259 | 255 |
typedef T ReachedMap; |
260 | 256 |
static ReachedMap *createReachedMap(const Digraph &) |
261 | 257 |
{ |
262 | 258 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
263 | 259 |
return 0; // ignore warnings |
264 | 260 |
} |
265 | 261 |
}; |
266 | 262 |
///\brief \ref named-templ-param "Named parameter" for setting |
267 | 263 |
///ReachedMap type. |
268 | 264 |
/// |
269 | 265 |
///\ref named-templ-param "Named parameter" for setting |
270 | 266 |
///ReachedMap type. |
267 |
///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept. |
|
271 | 268 |
template <class T> |
272 | 269 |
struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > { |
273 | 270 |
typedef Dfs< Digraph, SetReachedMapTraits<T> > Create; |
274 | 271 |
}; |
275 | 272 |
|
276 | 273 |
template <class T> |
277 | 274 |
struct SetProcessedMapTraits : public Traits { |
278 | 275 |
typedef T ProcessedMap; |
279 | 276 |
static ProcessedMap *createProcessedMap(const Digraph &) |
280 | 277 |
{ |
281 | 278 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
282 | 279 |
return 0; // ignore warnings |
283 | 280 |
} |
284 | 281 |
}; |
285 | 282 |
///\brief \ref named-templ-param "Named parameter" for setting |
286 | 283 |
///ProcessedMap type. |
287 | 284 |
/// |
288 | 285 |
///\ref named-templ-param "Named parameter" for setting |
289 | 286 |
///ProcessedMap type. |
287 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
|
290 | 288 |
template <class T> |
291 | 289 |
struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > { |
292 | 290 |
typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create; |
293 | 291 |
}; |
294 | 292 |
|
295 | 293 |
struct SetStandardProcessedMapTraits : public Traits { |
296 | 294 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
297 | 295 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
298 | 296 |
{ |
299 | 297 |
return new ProcessedMap(g); |
300 | 298 |
} |
301 | 299 |
}; |
302 | 300 |
///\brief \ref named-templ-param "Named parameter" for setting |
303 | 301 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
304 | 302 |
/// |
305 | 303 |
///\ref named-templ-param "Named parameter" for setting |
306 | 304 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
307 | 305 |
///If you don't set it explicitly, it will be automatically allocated. |
308 | 306 |
struct SetStandardProcessedMap : |
309 | 307 |
public Dfs< Digraph, SetStandardProcessedMapTraits > { |
310 | 308 |
typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create; |
311 | 309 |
}; |
312 | 310 |
|
313 | 311 |
///@} |
... | ... |
@@ -317,153 +315,154 @@ |
317 | 315 |
///Constructor. |
318 | 316 |
|
319 | 317 |
///Constructor. |
320 | 318 |
///\param g The digraph the algorithm runs on. |
321 | 319 |
Dfs(const Digraph &g) : |
322 | 320 |
G(&g), |
323 | 321 |
_pred(NULL), local_pred(false), |
324 | 322 |
_dist(NULL), local_dist(false), |
325 | 323 |
_reached(NULL), local_reached(false), |
326 | 324 |
_processed(NULL), local_processed(false) |
327 | 325 |
{ } |
328 | 326 |
|
329 | 327 |
///Destructor. |
330 | 328 |
~Dfs() |
331 | 329 |
{ |
332 | 330 |
if(local_pred) delete _pred; |
333 | 331 |
if(local_dist) delete _dist; |
334 | 332 |
if(local_reached) delete _reached; |
335 | 333 |
if(local_processed) delete _processed; |
336 | 334 |
} |
337 | 335 |
|
338 | 336 |
///Sets the map that stores the predecessor arcs. |
339 | 337 |
|
340 | 338 |
///Sets the map that stores the predecessor arcs. |
341 |
///If you don't use this function before calling \ref run(), |
|
342 |
///it will allocate one. The destructor deallocates this |
|
343 |
/// |
|
339 |
///If you don't use this function before calling \ref run(Node) "run()" |
|
340 |
///or \ref init(), an instance will be allocated automatically. |
|
341 |
///The destructor deallocates this automatically allocated map, |
|
342 |
///of course. |
|
344 | 343 |
///\return <tt> (*this) </tt> |
345 | 344 |
Dfs &predMap(PredMap &m) |
346 | 345 |
{ |
347 | 346 |
if(local_pred) { |
348 | 347 |
delete _pred; |
349 | 348 |
local_pred=false; |
350 | 349 |
} |
351 | 350 |
_pred = &m; |
352 | 351 |
return *this; |
353 | 352 |
} |
354 | 353 |
|
355 | 354 |
///Sets the map that indicates which nodes are reached. |
356 | 355 |
|
357 | 356 |
///Sets the map that indicates which nodes are reached. |
358 |
///If you don't use this function before calling \ref run(), |
|
359 |
///it will allocate one. The destructor deallocates this |
|
360 |
/// |
|
357 |
///If you don't use this function before calling \ref run(Node) "run()" |
|
358 |
///or \ref init(), an instance will be allocated automatically. |
|
359 |
///The destructor deallocates this automatically allocated map, |
|
360 |
///of course. |
|
361 | 361 |
///\return <tt> (*this) </tt> |
362 | 362 |
Dfs &reachedMap(ReachedMap &m) |
363 | 363 |
{ |
364 | 364 |
if(local_reached) { |
365 | 365 |
delete _reached; |
366 | 366 |
local_reached=false; |
367 | 367 |
} |
368 | 368 |
_reached = &m; |
369 | 369 |
return *this; |
370 | 370 |
} |
371 | 371 |
|
372 | 372 |
///Sets the map that indicates which nodes are processed. |
373 | 373 |
|
374 | 374 |
///Sets the map that indicates which nodes are processed. |
375 |
///If you don't use this function before calling \ref run(), |
|
376 |
///it will allocate one. The destructor deallocates this |
|
377 |
/// |
|
375 |
///If you don't use this function before calling \ref run(Node) "run()" |
|
376 |
///or \ref init(), an instance will be allocated automatically. |
|
377 |
///The destructor deallocates this automatically allocated map, |
|
378 |
///of course. |
|
378 | 379 |
///\return <tt> (*this) </tt> |
379 | 380 |
Dfs &processedMap(ProcessedMap &m) |
380 | 381 |
{ |
381 | 382 |
if(local_processed) { |
382 | 383 |
delete _processed; |
383 | 384 |
local_processed=false; |
384 | 385 |
} |
385 | 386 |
_processed = &m; |
386 | 387 |
return *this; |
387 | 388 |
} |
388 | 389 |
|
389 | 390 |
///Sets the map that stores the distances of the nodes. |
390 | 391 |
|
391 | 392 |
///Sets the map that stores the distances of the nodes calculated by |
392 | 393 |
///the algorithm. |
393 |
///If you don't use this function before calling \ref run(), |
|
394 |
///it will allocate one. The destructor deallocates this |
|
395 |
/// |
|
394 |
///If you don't use this function before calling \ref run(Node) "run()" |
|
395 |
///or \ref init(), an instance will be allocated automatically. |
|
396 |
///The destructor deallocates this automatically allocated map, |
|
397 |
///of course. |
|
396 | 398 |
///\return <tt> (*this) </tt> |
397 | 399 |
Dfs &distMap(DistMap &m) |
398 | 400 |
{ |
399 | 401 |
if(local_dist) { |
400 | 402 |
delete _dist; |
401 | 403 |
local_dist=false; |
402 | 404 |
} |
403 | 405 |
_dist = &m; |
404 | 406 |
return *this; |
405 | 407 |
} |
406 | 408 |
|
407 | 409 |
public: |
408 | 410 |
|
409 |
///\name Execution control |
|
410 |
///The simplest way to execute the algorithm is to use |
|
411 |
///one of the member functions called \ref lemon::Dfs::run() "run()". |
|
412 |
///\n |
|
413 |
///If you need more control on the execution, first you must call |
|
414 |
///\ref lemon::Dfs::init() "init()", then you can add a source node |
|
415 |
///with \ref lemon::Dfs::addSource() "addSource()". |
|
416 |
///Finally \ref lemon::Dfs::start() "start()" will perform the |
|
417 |
/// |
|
411 |
///\name Execution Control |
|
412 |
///The simplest way to execute the DFS algorithm is to use one of the |
|
413 |
///member functions called \ref run(Node) "run()".\n |
|
414 |
///If you need more control on the execution, first you have to call |
|
415 |
///\ref init(), then you can add a source node with \ref addSource() |
|
416 |
///and perform the actual computation with \ref start(). |
|
417 |
///This procedure can be repeated if there are nodes that have not |
|
418 |
///been reached. |
|
418 | 419 |
|
419 | 420 |
///@{ |
420 | 421 |
|
422 |
///\brief Initializes the internal data structures. |
|
423 |
/// |
|
421 | 424 |
///Initializes the internal data structures. |
422 |
|
|
423 |
///Initializes the internal data structures. |
|
424 |
/// |
|
425 | 425 |
void init() |
426 | 426 |
{ |
427 | 427 |
create_maps(); |
428 | 428 |
_stack.resize(countNodes(*G)); |
429 | 429 |
_stack_head=-1; |
430 | 430 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
431 | 431 |
_pred->set(u,INVALID); |
432 | 432 |
_reached->set(u,false); |
433 | 433 |
_processed->set(u,false); |
434 | 434 |
} |
435 | 435 |
} |
436 | 436 |
|
437 | 437 |
///Adds a new source node. |
438 | 438 |
|
439 | 439 |
///Adds a new source node to the set of nodes to be processed. |
440 | 440 |
/// |
441 |
///\pre The stack must be empty. (Otherwise the algorithm gives |
|
442 |
///false results.) |
|
443 |
/// |
|
444 |
///\warning Distances will be wrong (or at least strange) in case of |
|
445 |
/// |
|
441 |
///\pre The stack must be empty. Otherwise the algorithm gives |
|
442 |
///wrong results. (One of the outgoing arcs of all the source nodes |
|
443 |
///except for the last one will not be visited and distances will |
|
444 |
///also be wrong.) |
|
446 | 445 |
void addSource(Node s) |
447 | 446 |
{ |
448 | 447 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
449 | 448 |
if(!(*_reached)[s]) |
450 | 449 |
{ |
451 | 450 |
_reached->set(s,true); |
452 | 451 |
_pred->set(s,INVALID); |
453 | 452 |
OutArcIt e(*G,s); |
454 | 453 |
if(e!=INVALID) { |
455 | 454 |
_stack[++_stack_head]=e; |
456 | 455 |
_dist->set(s,_stack_head); |
457 | 456 |
} |
458 | 457 |
else { |
459 | 458 |
_processed->set(s,true); |
460 | 459 |
_dist->set(s,0); |
461 | 460 |
} |
462 | 461 |
} |
463 | 462 |
} |
464 | 463 |
|
465 | 464 |
///Processes the next arc. |
466 | 465 |
|
467 | 466 |
///Processes the next arc. |
468 | 467 |
/// |
469 | 468 |
///\return The processed arc. |
... | ... |
@@ -485,58 +484,58 @@ |
485 | 484 |
++_stack[_stack_head]; |
486 | 485 |
} |
487 | 486 |
while(_stack_head>=0 && _stack[_stack_head]==INVALID) { |
488 | 487 |
_processed->set(m,true); |
489 | 488 |
--_stack_head; |
490 | 489 |
if(_stack_head>=0) { |
491 | 490 |
m=G->source(_stack[_stack_head]); |
492 | 491 |
++_stack[_stack_head]; |
493 | 492 |
} |
494 | 493 |
} |
495 | 494 |
return e; |
496 | 495 |
} |
497 | 496 |
|
498 | 497 |
///Next arc to be processed. |
499 | 498 |
|
500 | 499 |
///Next arc to be processed. |
501 | 500 |
/// |
502 | 501 |
///\return The next arc to be processed or \c INVALID if the stack |
503 | 502 |
///is empty. |
504 | 503 |
OutArcIt nextArc() const |
505 | 504 |
{ |
506 | 505 |
return _stack_head>=0?_stack[_stack_head]:INVALID; |
507 | 506 |
} |
508 | 507 |
|
509 |
///\brief Returns \c false if there are nodes |
|
510 |
///to be processed. |
|
511 |
/// |
|
512 |
///Returns \c false if there are nodes |
|
513 |
///to be processed |
|
508 |
///Returns \c false if there are nodes to be processed. |
|
509 |
|
|
510 |
///Returns \c false if there are nodes to be processed |
|
511 |
///in the queue (stack). |
|
514 | 512 |
bool emptyQueue() const { return _stack_head<0; } |
515 | 513 |
|
516 | 514 |
///Returns the number of the nodes to be processed. |
517 | 515 |
|
518 |
///Returns the number of the nodes to be processed |
|
516 |
///Returns the number of the nodes to be processed |
|
517 |
///in the queue (stack). |
|
519 | 518 |
int queueSize() const { return _stack_head+1; } |
520 | 519 |
|
521 | 520 |
///Executes the algorithm. |
522 | 521 |
|
523 | 522 |
///Executes the algorithm. |
524 | 523 |
/// |
525 | 524 |
///This method runs the %DFS algorithm from the root node |
526 | 525 |
///in order to compute the DFS path to each node. |
527 | 526 |
/// |
528 | 527 |
/// The algorithm computes |
529 | 528 |
///- the %DFS tree, |
530 | 529 |
///- the distance of each node from the root in the %DFS tree. |
531 | 530 |
/// |
532 | 531 |
///\pre init() must be called and a root node should be |
533 | 532 |
///added with addSource() before using this function. |
534 | 533 |
/// |
535 | 534 |
///\note <tt>d.start()</tt> is just a shortcut of the following code. |
536 | 535 |
///\code |
537 | 536 |
/// while ( !d.emptyQueue() ) { |
538 | 537 |
/// d.processNextArc(); |
539 | 538 |
/// } |
540 | 539 |
///\endcode |
541 | 540 |
void start() |
542 | 541 |
{ |
... | ... |
@@ -616,155 +615,156 @@ |
616 | 615 |
///(it stops searching when \c t is processed) |
617 | 616 |
/// |
618 | 617 |
///\return \c true if \c t is reachable form \c s. |
619 | 618 |
/// |
620 | 619 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is |
621 | 620 |
///just a shortcut of the following code. |
622 | 621 |
///\code |
623 | 622 |
/// d.init(); |
624 | 623 |
/// d.addSource(s); |
625 | 624 |
/// d.start(t); |
626 | 625 |
///\endcode |
627 | 626 |
bool run(Node s,Node t) { |
628 | 627 |
init(); |
629 | 628 |
addSource(s); |
630 | 629 |
start(t); |
631 | 630 |
return reached(t); |
632 | 631 |
} |
633 | 632 |
|
634 | 633 |
///Runs the algorithm to visit all nodes in the digraph. |
635 | 634 |
|
636 | 635 |
///This method runs the %DFS algorithm in order to compute the |
637 | 636 |
///%DFS path to each node. |
638 | 637 |
/// |
639 | 638 |
///The algorithm computes |
640 |
///- the %DFS tree, |
|
641 |
///- the distance of each node from the root in the %DFS tree. |
|
639 |
///- the %DFS tree (forest), |
|
640 |
///- the distance of each node from the root(s) in the %DFS tree. |
|
642 | 641 |
/// |
643 | 642 |
///\note <tt>d.run()</tt> is just a shortcut of the following code. |
644 | 643 |
///\code |
645 | 644 |
/// d.init(); |
646 | 645 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
647 | 646 |
/// if (!d.reached(n)) { |
648 | 647 |
/// d.addSource(n); |
649 | 648 |
/// d.start(); |
650 | 649 |
/// } |
651 | 650 |
/// } |
652 | 651 |
///\endcode |
653 | 652 |
void run() { |
654 | 653 |
init(); |
655 | 654 |
for (NodeIt it(*G); it != INVALID; ++it) { |
656 | 655 |
if (!reached(it)) { |
657 | 656 |
addSource(it); |
658 | 657 |
start(); |
659 | 658 |
} |
660 | 659 |
} |
661 | 660 |
} |
662 | 661 |
|
663 | 662 |
///@} |
664 | 663 |
|
665 | 664 |
///\name Query Functions |
666 |
///The |
|
665 |
///The results of the DFS algorithm can be obtained using these |
|
667 | 666 |
///functions.\n |
668 |
///Either \ref lemon::Dfs::run() "run()" or \ref lemon::Dfs::start() |
|
669 |
///"start()" must be called before using them. |
|
667 |
///Either \ref run(Node) "run()" or \ref start() should be called |
|
668 |
///before using them. |
|
670 | 669 |
|
671 | 670 |
///@{ |
672 | 671 |
|
673 | 672 |
///The DFS path to a node. |
674 | 673 |
|
675 | 674 |
///Returns the DFS path to a node. |
676 | 675 |
/// |
677 |
///\warning \c t should be |
|
676 |
///\warning \c t should be reached from the root(s). |
|
678 | 677 |
/// |
679 |
///\pre Either \ref run() or \ref start() must be called before |
|
680 |
///using this function. |
|
678 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
679 |
///must be called before using this function. |
|
681 | 680 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
682 | 681 |
|
683 |
///The distance of a node from the root. |
|
682 |
///The distance of a node from the root(s). |
|
684 | 683 |
|
685 |
///Returns the distance of a node from the root. |
|
684 |
///Returns the distance of a node from the root(s). |
|
686 | 685 |
/// |
687 |
///\warning If node \c v is not |
|
686 |
///\warning If node \c v is not reached from the root(s), then |
|
688 | 687 |
///the return value of this function is undefined. |
689 | 688 |
/// |
690 |
///\pre Either \ref run() or \ref start() must be called before |
|
691 |
///using this function. |
|
689 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
690 |
///must be called before using this function. |
|
692 | 691 |
int dist(Node v) const { return (*_dist)[v]; } |
693 | 692 |
|
694 | 693 |
///Returns the 'previous arc' of the %DFS tree for a node. |
695 | 694 |
|
696 | 695 |
///This function returns the 'previous arc' of the %DFS tree for the |
697 |
///node \c v, i.e. it returns the last arc of a %DFS path from the |
|
698 |
///root to \c v. It is \c INVALID |
|
699 |
/// |
|
696 |
///node \c v, i.e. it returns the last arc of a %DFS path from a |
|
697 |
///root to \c v. It is \c INVALID if \c v is not reached from the |
|
698 |
///root(s) or if \c v is a root. |
|
700 | 699 |
/// |
701 | 700 |
///The %DFS tree used here is equal to the %DFS tree used in |
702 | 701 |
///\ref predNode(). |
703 | 702 |
/// |
704 |
///\pre Either \ref run() or \ref start() must be called before using |
|
705 |
///this function. |
|
703 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
704 |
///must be called before using this function. |
|
706 | 705 |
Arc predArc(Node v) const { return (*_pred)[v];} |
707 | 706 |
|
708 | 707 |
///Returns the 'previous node' of the %DFS tree. |
709 | 708 |
|
710 | 709 |
///This function returns the 'previous node' of the %DFS |
711 | 710 |
///tree for the node \c v, i.e. it returns the last but one node |
712 |
///from a %DFS path from the root to \c v. It is \c INVALID |
|
713 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
|
711 |
///from a %DFS path from a root to \c v. It is \c INVALID |
|
712 |
///if \c v is not reached from the root(s) or if \c v is a root. |
|
714 | 713 |
/// |
715 | 714 |
///The %DFS tree used here is equal to the %DFS tree used in |
716 | 715 |
///\ref predArc(). |
717 | 716 |
/// |
718 |
///\pre Either \ref run() or \ref start() must be called before |
|
719 |
///using this function. |
|
717 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
718 |
///must be called before using this function. |
|
720 | 719 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
721 | 720 |
G->source((*_pred)[v]); } |
722 | 721 |
|
723 | 722 |
///\brief Returns a const reference to the node map that stores the |
724 | 723 |
///distances of the nodes. |
725 | 724 |
/// |
726 | 725 |
///Returns a const reference to the node map that stores the |
727 | 726 |
///distances of the nodes calculated by the algorithm. |
728 | 727 |
/// |
729 |
///\pre Either \ref run() or \ref init() |
|
728 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
730 | 729 |
///must be called before using this function. |
731 | 730 |
const DistMap &distMap() const { return *_dist;} |
732 | 731 |
|
733 | 732 |
///\brief Returns a const reference to the node map that stores the |
734 | 733 |
///predecessor arcs. |
735 | 734 |
/// |
736 | 735 |
///Returns a const reference to the node map that stores the predecessor |
737 | 736 |
///arcs, which form the DFS tree. |
738 | 737 |
/// |
739 |
///\pre Either \ref run() or \ref init() |
|
738 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
740 | 739 |
///must be called before using this function. |
741 | 740 |
const PredMap &predMap() const { return *_pred;} |
742 | 741 |
|
743 |
///Checks if a node is |
|
742 |
///Checks if a node is reached from the root(s). |
|
744 | 743 |
|
745 |
///Returns \c true if \c v is reachable from the root(s). |
|
746 |
///\pre Either \ref run() or \ref start() |
|
744 |
///Returns \c true if \c v is reached from the root(s). |
|
745 |
/// |
|
746 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
747 | 747 |
///must be called before using this function. |
748 | 748 |
bool reached(Node v) const { return (*_reached)[v]; } |
749 | 749 |
|
750 | 750 |
///@} |
751 | 751 |
}; |
752 | 752 |
|
753 | 753 |
///Default traits class of dfs() function. |
754 | 754 |
|
755 | 755 |
///Default traits class of dfs() function. |
756 | 756 |
///\tparam GR Digraph type. |
757 | 757 |
template<class GR> |
758 | 758 |
struct DfsWizardDefaultTraits |
759 | 759 |
{ |
760 | 760 |
///The type of the digraph the algorithm runs on. |
761 | 761 |
typedef GR Digraph; |
762 | 762 |
|
763 | 763 |
///\brief The type of the map that stores the predecessor |
764 | 764 |
///arcs of the %DFS paths. |
765 | 765 |
/// |
766 | 766 |
///The type of the map that stores the predecessor |
767 | 767 |
///arcs of the %DFS paths. |
768 | 768 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
769 | 769 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
770 | 770 |
///Instantiates a PredMap. |
... | ... |
@@ -868,50 +868,50 @@ |
868 | 868 |
|
869 | 869 |
public: |
870 | 870 |
/// Constructor. |
871 | 871 |
|
872 | 872 |
/// This constructor does not require parameters, therefore it initiates |
873 | 873 |
/// all of the attributes to \c 0. |
874 | 874 |
DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
875 | 875 |
_dist(0), _path(0), _di(0) {} |
876 | 876 |
|
877 | 877 |
/// Constructor. |
878 | 878 |
|
879 | 879 |
/// This constructor requires one parameter, |
880 | 880 |
/// others are initiated to \c 0. |
881 | 881 |
/// \param g The digraph the algorithm runs on. |
882 | 882 |
DfsWizardBase(const GR &g) : |
883 | 883 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
884 | 884 |
_reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
885 | 885 |
|
886 | 886 |
}; |
887 | 887 |
|
888 | 888 |
/// Auxiliary class for the function-type interface of DFS algorithm. |
889 | 889 |
|
890 | 890 |
/// This auxiliary class is created to implement the |
891 | 891 |
/// \ref dfs() "function-type interface" of \ref Dfs algorithm. |
892 |
/// It does not have own \ref run() method, it uses the functions |
|
893 |
/// and features of the plain \ref Dfs. |
|
892 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
|
893 |
/// functions and features of the plain \ref Dfs. |
|
894 | 894 |
/// |
895 | 895 |
/// This class should only be used through the \ref dfs() function, |
896 | 896 |
/// which makes it easier to use the algorithm. |
897 | 897 |
template<class TR> |
898 | 898 |
class DfsWizard : public TR |
899 | 899 |
{ |
900 | 900 |
typedef TR Base; |
901 | 901 |
|
902 | 902 |
///The type of the digraph the algorithm runs on. |
903 | 903 |
typedef typename TR::Digraph Digraph; |
904 | 904 |
|
905 | 905 |
typedef typename Digraph::Node Node; |
906 | 906 |
typedef typename Digraph::NodeIt NodeIt; |
907 | 907 |
typedef typename Digraph::Arc Arc; |
908 | 908 |
typedef typename Digraph::OutArcIt OutArcIt; |
909 | 909 |
|
910 | 910 |
///\brief The type of the map that stores the predecessor |
911 | 911 |
///arcs of the DFS paths. |
912 | 912 |
typedef typename TR::PredMap PredMap; |
913 | 913 |
///\brief The type of the map that stores the distances of the nodes. |
914 | 914 |
typedef typename TR::DistMap DistMap; |
915 | 915 |
///\brief The type of the map that indicates which nodes are reached. |
916 | 916 |
typedef typename TR::ReachedMap ReachedMap; |
917 | 917 |
///\brief The type of the map that indicates which nodes are processed. |
... | ... |
@@ -1089,50 +1089,49 @@ |
1089 | 1089 |
///for getting the distance of the target node. |
1090 | 1090 |
DfsWizard dist(const int &d) |
1091 | 1091 |
{ |
1092 | 1092 |
Base::_di=const_cast<int*>(&d); |
1093 | 1093 |
return *this; |
1094 | 1094 |
} |
1095 | 1095 |
|
1096 | 1096 |
}; |
1097 | 1097 |
|
1098 | 1098 |
///Function-type interface for DFS algorithm. |
1099 | 1099 |
|
1100 | 1100 |
///\ingroup search |
1101 | 1101 |
///Function-type interface for DFS algorithm. |
1102 | 1102 |
/// |
1103 | 1103 |
///This function also has several \ref named-func-param "named parameters", |
1104 | 1104 |
///they are declared as the members of class \ref DfsWizard. |
1105 | 1105 |
///The following examples show how to use these parameters. |
1106 | 1106 |
///\code |
1107 | 1107 |
/// // Compute the DFS tree |
1108 | 1108 |
/// dfs(g).predMap(preds).distMap(dists).run(s); |
1109 | 1109 |
/// |
1110 | 1110 |
/// // Compute the DFS path from s to t |
1111 | 1111 |
/// bool reached = dfs(g).path(p).dist(d).run(s,t); |
1112 | 1112 |
///\endcode |
1113 |
|
|
1114 |
///\warning Don't forget to put the \ref DfsWizard::run() "run()" |
|
1113 |
///\warning Don't forget to put the \ref DfsWizard::run(Node) "run()" |
|
1115 | 1114 |
///to the end of the parameter list. |
1116 | 1115 |
///\sa DfsWizard |
1117 | 1116 |
///\sa Dfs |
1118 | 1117 |
template<class GR> |
1119 | 1118 |
DfsWizard<DfsWizardBase<GR> > |
1120 | 1119 |
dfs(const GR &digraph) |
1121 | 1120 |
{ |
1122 | 1121 |
return DfsWizard<DfsWizardBase<GR> >(digraph); |
1123 | 1122 |
} |
1124 | 1123 |
|
1125 | 1124 |
#ifdef DOXYGEN |
1126 | 1125 |
/// \brief Visitor class for DFS. |
1127 | 1126 |
/// |
1128 | 1127 |
/// This class defines the interface of the DfsVisit events, and |
1129 | 1128 |
/// it could be the base of a real visitor class. |
1130 | 1129 |
template <typename _Digraph> |
1131 | 1130 |
struct DfsVisitor { |
1132 | 1131 |
typedef _Digraph Digraph; |
1133 | 1132 |
typedef typename Digraph::Arc Arc; |
1134 | 1133 |
typedef typename Digraph::Node Node; |
1135 | 1134 |
/// \brief Called for the source node of the DFS. |
1136 | 1135 |
/// |
1137 | 1136 |
/// This function is called for the source node of the DFS. |
1138 | 1137 |
void start(const Node& node) {} |
... | ... |
@@ -1288,139 +1287,137 @@ |
1288 | 1287 |
//Pointer to the map of reached status of the nodes. |
1289 | 1288 |
ReachedMap *_reached; |
1290 | 1289 |
//Indicates if _reached is locally allocated (true) or not. |
1291 | 1290 |
bool local_reached; |
1292 | 1291 |
|
1293 | 1292 |
std::vector<typename Digraph::Arc> _stack; |
1294 | 1293 |
int _stack_head; |
1295 | 1294 |
|
1296 | 1295 |
//Creates the maps if necessary. |
1297 | 1296 |
void create_maps() { |
1298 | 1297 |
if(!_reached) { |
1299 | 1298 |
local_reached = true; |
1300 | 1299 |
_reached = Traits::createReachedMap(*_digraph); |
1301 | 1300 |
} |
1302 | 1301 |
} |
1303 | 1302 |
|
1304 | 1303 |
protected: |
1305 | 1304 |
|
1306 | 1305 |
DfsVisit() {} |
1307 | 1306 |
|
1308 | 1307 |
public: |
1309 | 1308 |
|
1310 | 1309 |
typedef DfsVisit Create; |
1311 | 1310 |
|
1312 |
/// \name Named |
|
1311 |
/// \name Named Template Parameters |
|
1313 | 1312 |
|
1314 | 1313 |
///@{ |
1315 | 1314 |
template <class T> |
1316 | 1315 |
struct SetReachedMapTraits : public Traits { |
1317 | 1316 |
typedef T ReachedMap; |
1318 | 1317 |
static ReachedMap *createReachedMap(const Digraph &digraph) { |
1319 | 1318 |
LEMON_ASSERT(false, "ReachedMap is not initialized"); |
1320 | 1319 |
return 0; // ignore warnings |
1321 | 1320 |
} |
1322 | 1321 |
}; |
1323 | 1322 |
/// \brief \ref named-templ-param "Named parameter" for setting |
1324 | 1323 |
/// ReachedMap type. |
1325 | 1324 |
/// |
1326 | 1325 |
/// \ref named-templ-param "Named parameter" for setting ReachedMap type. |
1327 | 1326 |
template <class T> |
1328 | 1327 |
struct SetReachedMap : public DfsVisit< Digraph, Visitor, |
1329 | 1328 |
SetReachedMapTraits<T> > { |
1330 | 1329 |
typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create; |
1331 | 1330 |
}; |
1332 | 1331 |
///@} |
1333 | 1332 |
|
1334 | 1333 |
public: |
1335 | 1334 |
|
1336 | 1335 |
/// \brief Constructor. |
1337 | 1336 |
/// |
1338 | 1337 |
/// Constructor. |
1339 | 1338 |
/// |
1340 | 1339 |
/// \param digraph The digraph the algorithm runs on. |
1341 | 1340 |
/// \param visitor The visitor object of the algorithm. |
1342 | 1341 |
DfsVisit(const Digraph& digraph, Visitor& visitor) |
1343 | 1342 |
: _digraph(&digraph), _visitor(&visitor), |
1344 | 1343 |
_reached(0), local_reached(false) {} |
1345 | 1344 |
|
1346 | 1345 |
/// \brief Destructor. |
1347 | 1346 |
~DfsVisit() { |
1348 | 1347 |
if(local_reached) delete _reached; |
1349 | 1348 |
} |
1350 | 1349 |
|
1351 | 1350 |
/// \brief Sets the map that indicates which nodes are reached. |
1352 | 1351 |
/// |
1353 | 1352 |
/// Sets the map that indicates which nodes are reached. |
1354 |
/// If you don't use this function before calling \ref run(), |
|
1355 |
/// it will allocate one. The destructor deallocates this |
|
1356 |
/// |
|
1353 |
/// If you don't use this function before calling \ref run(Node) "run()" |
|
1354 |
/// or \ref init(), an instance will be allocated automatically. |
|
1355 |
/// The destructor deallocates this automatically allocated map, |
|
1356 |
/// of course. |
|
1357 | 1357 |
/// \return <tt> (*this) </tt> |
1358 | 1358 |
DfsVisit &reachedMap(ReachedMap &m) { |
1359 | 1359 |
if(local_reached) { |
1360 | 1360 |
delete _reached; |
1361 | 1361 |
local_reached=false; |
1362 | 1362 |
} |
1363 | 1363 |
_reached = &m; |
1364 | 1364 |
return *this; |
1365 | 1365 |
} |
1366 | 1366 |
|
1367 | 1367 |
public: |
1368 | 1368 |
|
1369 |
/// \name Execution control |
|
1370 |
/// The simplest way to execute the algorithm is to use |
|
1371 |
/// one of the member functions called \ref lemon::DfsVisit::run() |
|
1372 |
/// "run()". |
|
1373 |
/// \n |
|
1374 |
/// If you need more control on the execution, first you must call |
|
1375 |
/// \ref lemon::DfsVisit::init() "init()", then you can add several |
|
1376 |
/// source nodes with \ref lemon::DfsVisit::addSource() "addSource()". |
|
1377 |
/// Finally \ref lemon::DfsVisit::start() "start()" will perform the |
|
1378 |
/// actual path computation. |
|
1369 |
/// \name Execution Control |
|
1370 |
/// The simplest way to execute the DFS algorithm is to use one of the |
|
1371 |
/// member functions called \ref run(Node) "run()".\n |
|
1372 |
/// If you need more control on the execution, first you have to call |
|
1373 |
/// \ref init(), then you can add a source node with \ref addSource() |
|
1374 |
/// and perform the actual computation with \ref start(). |
|
1375 |
/// This procedure can be repeated if there are nodes that have not |
|
1376 |
/// been reached. |
|
1379 | 1377 |
|
1380 | 1378 |
/// @{ |
1381 | 1379 |
|
1382 | 1380 |
/// \brief Initializes the internal data structures. |
1383 | 1381 |
/// |
1384 | 1382 |
/// Initializes the internal data structures. |
1385 | 1383 |
void init() { |
1386 | 1384 |
create_maps(); |
1387 | 1385 |
_stack.resize(countNodes(*_digraph)); |
1388 | 1386 |
_stack_head = -1; |
1389 | 1387 |
for (NodeIt u(*_digraph) ; u != INVALID ; ++u) { |
1390 | 1388 |
_reached->set(u, false); |
1391 | 1389 |
} |
1392 | 1390 |
} |
1393 | 1391 |
|
1394 |
///Adds a new source node. |
|
1395 |
|
|
1392 |
/// \brief Adds a new source node. |
|
1393 |
/// |
|
1396 | 1394 |
///Adds a new source node to the set of nodes to be processed. |
1397 | 1395 |
/// |
1398 |
///\pre The stack must be empty. (Otherwise the algorithm gives |
|
1399 |
///false results.) |
|
1400 |
/// |
|
1401 |
///\warning Distances will be wrong (or at least strange) in case of |
|
1402 |
/// |
|
1396 |
/// \pre The stack must be empty. Otherwise the algorithm gives |
|
1397 |
/// wrong results. (One of the outgoing arcs of all the source nodes |
|
1398 |
/// except for the last one will not be visited and distances will |
|
1399 |
/// also be wrong.) |
|
1403 | 1400 |
void addSource(Node s) |
1404 | 1401 |
{ |
1405 | 1402 |
LEMON_DEBUG(emptyQueue(), "The stack is not empty."); |
1406 | 1403 |
if(!(*_reached)[s]) { |
1407 | 1404 |
_reached->set(s,true); |
1408 | 1405 |
_visitor->start(s); |
1409 | 1406 |
_visitor->reach(s); |
1410 | 1407 |
Arc e; |
1411 | 1408 |
_digraph->firstOut(e, s); |
1412 | 1409 |
if (e != INVALID) { |
1413 | 1410 |
_stack[++_stack_head] = e; |
1414 | 1411 |
} else { |
1415 | 1412 |
_visitor->leave(s); |
1416 | 1413 |
} |
1417 | 1414 |
} |
1418 | 1415 |
} |
1419 | 1416 |
|
1420 | 1417 |
/// \brief Processes the next arc. |
1421 | 1418 |
/// |
1422 | 1419 |
/// Processes the next arc. |
1423 | 1420 |
/// |
1424 | 1421 |
/// \return The processed arc. |
1425 | 1422 |
/// |
1426 | 1423 |
/// \pre The stack must not be empty. |
... | ... |
@@ -1568,71 +1565,72 @@ |
1568 | 1565 |
/// (it stops searching when \c t is processed). |
1569 | 1566 |
/// |
1570 | 1567 |
/// \return \c true if \c t is reachable form \c s. |
1571 | 1568 |
/// |
1572 | 1569 |
/// \note Apart from the return value, <tt>d.run(s,t)</tt> is |
1573 | 1570 |
/// just a shortcut of the following code. |
1574 | 1571 |
///\code |
1575 | 1572 |
/// d.init(); |
1576 | 1573 |
/// d.addSource(s); |
1577 | 1574 |
/// d.start(t); |
1578 | 1575 |
///\endcode |
1579 | 1576 |
bool run(Node s,Node t) { |
1580 | 1577 |
init(); |
1581 | 1578 |
addSource(s); |
1582 | 1579 |
start(t); |
1583 | 1580 |
return reached(t); |
1584 | 1581 |
} |
1585 | 1582 |
|
1586 | 1583 |
/// \brief Runs the algorithm to visit all nodes in the digraph. |
1587 | 1584 |
|
1588 | 1585 |
/// This method runs the %DFS algorithm in order to |
1589 | 1586 |
/// compute the %DFS path to each node. |
1590 | 1587 |
/// |
1591 | 1588 |
/// The algorithm computes |
1592 |
/// - the %DFS tree, |
|
1593 |
/// - the distance of each node from the root in the %DFS tree. |
|
1589 |
/// - the %DFS tree (forest), |
|
1590 |
/// - the distance of each node from the root(s) in the %DFS tree. |
|
1594 | 1591 |
/// |
1595 | 1592 |
/// \note <tt>d.run()</tt> is just a shortcut of the following code. |
1596 | 1593 |
///\code |
1597 | 1594 |
/// d.init(); |
1598 | 1595 |
/// for (NodeIt n(digraph); n != INVALID; ++n) { |
1599 | 1596 |
/// if (!d.reached(n)) { |
1600 | 1597 |
/// d.addSource(n); |
1601 | 1598 |
/// d.start(); |
1602 | 1599 |
/// } |
1603 | 1600 |
/// } |
1604 | 1601 |
///\endcode |
1605 | 1602 |
void run() { |
1606 | 1603 |
init(); |
1607 | 1604 |
for (NodeIt it(*_digraph); it != INVALID; ++it) { |
1608 | 1605 |
if (!reached(it)) { |
1609 | 1606 |
addSource(it); |
1610 | 1607 |
start(); |
1611 | 1608 |
} |
1612 | 1609 |
} |
1613 | 1610 |
} |
1614 | 1611 |
|
1615 | 1612 |
///@} |
1616 | 1613 |
|
1617 | 1614 |
/// \name Query Functions |
1618 |
/// The |
|
1615 |
/// The results of the DFS algorithm can be obtained using these |
|
1619 | 1616 |
/// functions.\n |
1620 |
/// Either \ref lemon::DfsVisit::run() "run()" or |
|
1621 |
/// \ref lemon::DfsVisit::start() "start()" must be called before |
|
1622 |
/// |
|
1617 |
/// Either \ref run(Node) "run()" or \ref start() should be called |
|
1618 |
/// before using them. |
|
1619 |
|
|
1623 | 1620 |
///@{ |
1624 | 1621 |
|
1625 |
/// \brief Checks if a node is |
|
1622 |
/// \brief Checks if a node is reached from the root(s). |
|
1626 | 1623 |
/// |
1627 |
/// Returns \c true if \c v is reachable from the root(s). |
|
1628 |
/// \pre Either \ref run() or \ref start() |
|
1624 |
/// Returns \c true if \c v is reached from the root(s). |
|
1625 |
/// |
|
1626 |
/// \pre Either \ref run(Node) "run()" or \ref init() |
|
1629 | 1627 |
/// must be called before using this function. |
1630 | 1628 |
bool reached(Node v) { return (*_reached)[v]; } |
1631 | 1629 |
|
1632 | 1630 |
///@} |
1633 | 1631 |
|
1634 | 1632 |
}; |
1635 | 1633 |
|
1636 | 1634 |
} //END OF NAMESPACE LEMON |
1637 | 1635 |
|
1638 | 1636 |
#endif |
... | ... |
@@ -181,96 +181,89 @@ |
181 | 181 |
///the DistMap |
182 | 182 |
static DistMap *createDistMap(const Digraph &g) |
183 | 183 |
{ |
184 | 184 |
return new DistMap(g); |
185 | 185 |
} |
186 | 186 |
}; |
187 | 187 |
|
188 | 188 |
///%Dijkstra algorithm class. |
189 | 189 |
|
190 | 190 |
/// \ingroup shortest_path |
191 | 191 |
///This class provides an efficient implementation of the %Dijkstra algorithm. |
192 | 192 |
/// |
193 | 193 |
///The arc lengths are passed to the algorithm using a |
194 | 194 |
///\ref concepts::ReadMap "ReadMap", |
195 | 195 |
///so it is easy to change it to any kind of length. |
196 | 196 |
///The type of the length is determined by the |
197 | 197 |
///\ref concepts::ReadMap::Value "Value" of the length map. |
198 | 198 |
///It is also possible to change the underlying priority heap. |
199 | 199 |
/// |
200 | 200 |
///There is also a \ref dijkstra() "function-type interface" for the |
201 | 201 |
///%Dijkstra algorithm, which is convenient in the simplier cases and |
202 | 202 |
///it can be used easier. |
203 | 203 |
/// |
204 | 204 |
///\tparam GR The type of the digraph the algorithm runs on. |
205 |
///The default value is \ref ListDigraph. |
|
206 |
///The value of GR is not used directly by \ref Dijkstra, it is only |
|
207 |
///passed to \ref DijkstraDefaultTraits. |
|
208 |
///\tparam LM A readable arc map that determines the lengths of the |
|
209 |
/// |
|
205 |
///The default type is \ref ListDigraph. |
|
206 |
///\tparam LM A \ref concepts::ReadMap "readable" arc map that specifies |
|
207 |
///the lengths of the arcs. |
|
208 |
///It is read once for each arc, so the map may involve in |
|
210 | 209 |
///relatively time consuming process to compute the arc lengths if |
211 | 210 |
///it is necessary. The default map type is \ref |
212 |
///concepts::Digraph::ArcMap "Digraph::ArcMap<int>". |
|
213 |
///The value of LM is not used directly by \ref Dijkstra, it is only |
|
214 |
///passed to \ref DijkstraDefaultTraits. |
|
215 |
///\tparam TR Traits class to set various data types used by the algorithm. |
|
216 |
///The default traits class is \ref DijkstraDefaultTraits |
|
217 |
///"DijkstraDefaultTraits<GR,LM>". See \ref DijkstraDefaultTraits |
|
218 |
/// |
|
211 |
///concepts::Digraph::ArcMap "GR::ArcMap<int>". |
|
219 | 212 |
#ifdef DOXYGEN |
220 | 213 |
template <typename GR, typename LM, typename TR> |
221 | 214 |
#else |
222 | 215 |
template <typename GR=ListDigraph, |
223 | 216 |
typename LM=typename GR::template ArcMap<int>, |
224 | 217 |
typename TR=DijkstraDefaultTraits<GR,LM> > |
225 | 218 |
#endif |
226 | 219 |
class Dijkstra { |
227 | 220 |
public: |
228 | 221 |
|
229 | 222 |
///The type of the digraph the algorithm runs on. |
230 | 223 |
typedef typename TR::Digraph Digraph; |
231 | 224 |
|
232 | 225 |
///The type of the length of the arcs. |
233 | 226 |
typedef typename TR::LengthMap::Value Value; |
234 | 227 |
///The type of the map that stores the arc lengths. |
235 | 228 |
typedef typename TR::LengthMap LengthMap; |
236 | 229 |
///\brief The type of the map that stores the predecessor arcs of the |
237 | 230 |
///shortest paths. |
238 | 231 |
typedef typename TR::PredMap PredMap; |
239 | 232 |
///The type of the map that stores the distances of the nodes. |
240 | 233 |
typedef typename TR::DistMap DistMap; |
241 | 234 |
///The type of the map that indicates which nodes are processed. |
242 | 235 |
typedef typename TR::ProcessedMap ProcessedMap; |
243 | 236 |
///The type of the paths. |
244 | 237 |
typedef PredMapPath<Digraph, PredMap> Path; |
245 | 238 |
///The cross reference type used for the current heap. |
246 | 239 |
typedef typename TR::HeapCrossRef HeapCrossRef; |
247 | 240 |
///The heap type used by the algorithm. |
248 | 241 |
typedef typename TR::Heap Heap; |
249 | 242 |
///The operation traits class. |
250 | 243 |
typedef typename TR::OperationTraits OperationTraits; |
251 | 244 |
|
252 |
///The traits class. |
|
245 |
///The \ref DijkstraDefaultTraits "traits class" of the algorithm. |
|
253 | 246 |
typedef TR Traits; |
254 | 247 |
|
255 | 248 |
private: |
256 | 249 |
|
257 | 250 |
typedef typename Digraph::Node Node; |
258 | 251 |
typedef typename Digraph::NodeIt NodeIt; |
259 | 252 |
typedef typename Digraph::Arc Arc; |
260 | 253 |
typedef typename Digraph::OutArcIt OutArcIt; |
261 | 254 |
|
262 | 255 |
//Pointer to the underlying digraph. |
263 | 256 |
const Digraph *G; |
264 | 257 |
//Pointer to the length map. |
265 | 258 |
const LengthMap *length; |
266 | 259 |
//Pointer to the map of predecessors arcs. |
267 | 260 |
PredMap *_pred; |
268 | 261 |
//Indicates if _pred is locally allocated (true) or not. |
269 | 262 |
bool local_pred; |
270 | 263 |
//Pointer to the map of distances. |
271 | 264 |
DistMap *_dist; |
272 | 265 |
//Indicates if _dist is locally allocated (true) or not. |
273 | 266 |
bool local_dist; |
274 | 267 |
//Pointer to the map of processed status of the nodes. |
275 | 268 |
ProcessedMap *_processed; |
276 | 269 |
//Indicates if _processed is locally allocated (true) or not. |
... | ... |
@@ -310,157 +303,170 @@ |
310 | 303 |
} |
311 | 304 |
|
312 | 305 |
public: |
313 | 306 |
|
314 | 307 |
typedef Dijkstra Create; |
315 | 308 |
|
316 | 309 |
///\name Named template parameters |
317 | 310 |
|
318 | 311 |
///@{ |
319 | 312 |
|
320 | 313 |
template <class T> |
321 | 314 |
struct SetPredMapTraits : public Traits { |
322 | 315 |
typedef T PredMap; |
323 | 316 |
static PredMap *createPredMap(const Digraph &) |
324 | 317 |
{ |
325 | 318 |
LEMON_ASSERT(false, "PredMap is not initialized"); |
326 | 319 |
return 0; // ignore warnings |
327 | 320 |
} |
328 | 321 |
}; |
329 | 322 |
///\brief \ref named-templ-param "Named parameter" for setting |
330 | 323 |
///PredMap type. |
331 | 324 |
/// |
332 | 325 |
///\ref named-templ-param "Named parameter" for setting |
333 | 326 |
///PredMap type. |
327 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
|
334 | 328 |
template <class T> |
335 | 329 |
struct SetPredMap |
336 | 330 |
: public Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > { |
337 | 331 |
typedef Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > Create; |
338 | 332 |
}; |
339 | 333 |
|
340 | 334 |
template <class T> |
341 | 335 |
struct SetDistMapTraits : public Traits { |
342 | 336 |
typedef T DistMap; |
343 | 337 |
static DistMap *createDistMap(const Digraph &) |
344 | 338 |
{ |
345 | 339 |
LEMON_ASSERT(false, "DistMap is not initialized"); |
346 | 340 |
return 0; // ignore warnings |
347 | 341 |
} |
348 | 342 |
}; |
349 | 343 |
///\brief \ref named-templ-param "Named parameter" for setting |
350 | 344 |
///DistMap type. |
351 | 345 |
/// |
352 | 346 |
///\ref named-templ-param "Named parameter" for setting |
353 | 347 |
///DistMap type. |
348 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
|
354 | 349 |
template <class T> |
355 | 350 |
struct SetDistMap |
356 | 351 |
: public Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > { |
357 | 352 |
typedef Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > Create; |
358 | 353 |
}; |
359 | 354 |
|
360 | 355 |
template <class T> |
361 | 356 |
struct SetProcessedMapTraits : public Traits { |
362 | 357 |
typedef T ProcessedMap; |
363 | 358 |
static ProcessedMap *createProcessedMap(const Digraph &) |
364 | 359 |
{ |
365 | 360 |
LEMON_ASSERT(false, "ProcessedMap is not initialized"); |
366 | 361 |
return 0; // ignore warnings |
367 | 362 |
} |
368 | 363 |
}; |
369 | 364 |
///\brief \ref named-templ-param "Named parameter" for setting |
370 | 365 |
///ProcessedMap type. |
371 | 366 |
/// |
372 | 367 |
///\ref named-templ-param "Named parameter" for setting |
373 | 368 |
///ProcessedMap type. |
369 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
|
374 | 370 |
template <class T> |
375 | 371 |
struct SetProcessedMap |
376 | 372 |
: public Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > { |
377 | 373 |
typedef Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > Create; |
378 | 374 |
}; |
379 | 375 |
|
380 | 376 |
struct SetStandardProcessedMapTraits : public Traits { |
381 | 377 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
382 | 378 |
static ProcessedMap *createProcessedMap(const Digraph &g) |
383 | 379 |
{ |
384 | 380 |
return new ProcessedMap(g); |
385 | 381 |
} |
386 | 382 |
}; |
387 | 383 |
///\brief \ref named-templ-param "Named parameter" for setting |
388 | 384 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
389 | 385 |
/// |
390 | 386 |
///\ref named-templ-param "Named parameter" for setting |
391 | 387 |
///ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
392 | 388 |
///If you don't set it explicitly, it will be automatically allocated. |
393 | 389 |
struct SetStandardProcessedMap |
394 | 390 |
: public Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > { |
395 | 391 |
typedef Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > |
396 | 392 |
Create; |
397 | 393 |
}; |
398 | 394 |
|
399 | 395 |
template <class H, class CR> |
400 | 396 |
struct SetHeapTraits : public Traits { |
401 | 397 |
typedef CR HeapCrossRef; |
402 | 398 |
typedef H Heap; |
403 | 399 |
static HeapCrossRef *createHeapCrossRef(const Digraph &) { |
404 | 400 |
LEMON_ASSERT(false, "HeapCrossRef is not initialized"); |
405 | 401 |
return 0; // ignore warnings |
406 | 402 |
} |
407 | 403 |
static Heap *createHeap(HeapCrossRef &) |
408 | 404 |
{ |
409 | 405 |
LEMON_ASSERT(false, "Heap is not initialized"); |
410 | 406 |
return 0; // ignore warnings |
411 | 407 |
} |
412 | 408 |
}; |
413 | 409 |
///\brief \ref named-templ-param "Named parameter" for setting |
414 |
///heap and cross reference |
|
410 |
///heap and cross reference types |
|
415 | 411 |
/// |
416 | 412 |
///\ref named-templ-param "Named parameter" for setting heap and cross |
417 |
///reference |
|
413 |
///reference types. If this named parameter is used, then external |
|
414 |
///heap and cross reference objects must be passed to the algorithm |
|
415 |
///using the \ref heap() function before calling \ref run(Node) "run()" |
|
416 |
///or \ref init(). |
|
417 |
///\sa SetStandardHeap |
|
418 | 418 |
template <class H, class CR = typename Digraph::template NodeMap<int> > |
419 | 419 |
struct SetHeap |
420 | 420 |
: public Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > { |
421 | 421 |
typedef Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > Create; |
422 | 422 |
}; |
423 | 423 |
|
424 | 424 |
template <class H, class CR> |
425 | 425 |
struct SetStandardHeapTraits : public Traits { |
426 | 426 |
typedef CR HeapCrossRef; |
427 | 427 |
typedef H Heap; |
428 | 428 |
static HeapCrossRef *createHeapCrossRef(const Digraph &G) { |
429 | 429 |
return new HeapCrossRef(G); |
430 | 430 |
} |
431 | 431 |
static Heap *createHeap(HeapCrossRef &R) |
432 | 432 |
{ |
433 | 433 |
return new Heap(R); |
434 | 434 |
} |
435 | 435 |
}; |
436 | 436 |
///\brief \ref named-templ-param "Named parameter" for setting |
437 |
///heap and cross reference |
|
437 |
///heap and cross reference types with automatic allocation |
|
438 | 438 |
/// |
439 | 439 |
///\ref named-templ-param "Named parameter" for setting heap and cross |
440 |
///reference type. It can allocate the heap and the cross reference |
|
441 |
///object if the cross reference's constructor waits for the digraph as |
|
442 |
/// |
|
440 |
///reference types with automatic allocation. |
|
441 |
///They should have standard constructor interfaces to be able to |
|
442 |
///automatically created by the algorithm (i.e. the digraph should be |
|
443 |
///passed to the constructor of the cross reference and the cross |
|
444 |
///reference should be passed to the constructor of the heap). |
|
445 |
///However external heap and cross reference objects could also be |
|
446 |
///passed to the algorithm using the \ref heap() function before |
|
447 |
///calling \ref run(Node) "run()" or \ref init(). |
|
448 |
///\sa SetHeap |
|
443 | 449 |
template <class H, class CR = typename Digraph::template NodeMap<int> > |
444 | 450 |
struct SetStandardHeap |
445 | 451 |
: public Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > { |
446 | 452 |
typedef Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > |
447 | 453 |
Create; |
448 | 454 |
}; |
449 | 455 |
|
450 | 456 |
template <class T> |
451 | 457 |
struct SetOperationTraitsTraits : public Traits { |
452 | 458 |
typedef T OperationTraits; |
453 | 459 |
}; |
454 | 460 |
|
455 | 461 |
/// \brief \ref named-templ-param "Named parameter" for setting |
456 | 462 |
///\c OperationTraits type |
457 | 463 |
/// |
458 | 464 |
///\ref named-templ-param "Named parameter" for setting |
459 | 465 |
///\ref OperationTraits type. |
460 | 466 |
template <class T> |
461 | 467 |
struct SetOperationTraits |
462 | 468 |
: public Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > { |
463 | 469 |
typedef Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > |
464 | 470 |
Create; |
465 | 471 |
}; |
466 | 472 |
|
... | ... |
@@ -488,145 +494,147 @@ |
488 | 494 |
|
489 | 495 |
///Destructor. |
490 | 496 |
~Dijkstra() |
491 | 497 |
{ |
492 | 498 |
if(local_pred) delete _pred; |
493 | 499 |
if(local_dist) delete _dist; |
494 | 500 |
if(local_processed) delete _processed; |
495 | 501 |
if(local_heap_cross_ref) delete _heap_cross_ref; |
496 | 502 |
if(local_heap) delete _heap; |
497 | 503 |
} |
498 | 504 |
|
499 | 505 |
///Sets the length map. |
500 | 506 |
|
501 | 507 |
///Sets the length map. |
502 | 508 |
///\return <tt> (*this) </tt> |
503 | 509 |
Dijkstra &lengthMap(const LengthMap &m) |
504 | 510 |
{ |
505 | 511 |
length = &m; |
506 | 512 |
return *this; |
507 | 513 |
} |
508 | 514 |
|
509 | 515 |
///Sets the map that stores the predecessor arcs. |
510 | 516 |
|
511 | 517 |
///Sets the map that stores the predecessor arcs. |
512 |
///If you don't use this function before calling \ref run(), |
|
513 |
///it will allocate one. The destructor deallocates this |
|
514 |
/// |
|
518 |
///If you don't use this function before calling \ref run(Node) "run()" |
|
519 |
///or \ref init(), an instance will be allocated automatically. |
|
520 |
///The destructor deallocates this automatically allocated map, |
|
521 |
///of course. |
|
515 | 522 |
///\return <tt> (*this) </tt> |
516 | 523 |
Dijkstra &predMap(PredMap &m) |
517 | 524 |
{ |
518 | 525 |
if(local_pred) { |
519 | 526 |
delete _pred; |
520 | 527 |
local_pred=false; |
521 | 528 |
} |
522 | 529 |
_pred = &m; |
523 | 530 |
return *this; |
524 | 531 |
} |
525 | 532 |
|
526 | 533 |
///Sets the map that indicates which nodes are processed. |
527 | 534 |
|
528 | 535 |
///Sets the map that indicates which nodes are processed. |
529 |
///If you don't use this function before calling \ref run(), |
|
530 |
///it will allocate one. The destructor deallocates this |
|
531 |
/// |
|
536 |
///If you don't use this function before calling \ref run(Node) "run()" |
|
537 |
///or \ref init(), an instance will be allocated automatically. |
|
538 |
///The destructor deallocates this automatically allocated map, |
|
539 |
///of course. |
|
532 | 540 |
///\return <tt> (*this) </tt> |
533 | 541 |
Dijkstra &processedMap(ProcessedMap &m) |
534 | 542 |
{ |
535 | 543 |
if(local_processed) { |
536 | 544 |
delete _processed; |
537 | 545 |
local_processed=false; |
538 | 546 |
} |
539 | 547 |
_processed = &m; |
540 | 548 |
return *this; |
541 | 549 |
} |
542 | 550 |
|
543 | 551 |
///Sets the map that stores the distances of the nodes. |
544 | 552 |
|
545 | 553 |
///Sets the map that stores the distances of the nodes calculated by the |
546 | 554 |
///algorithm. |
547 |
///If you don't use this function before calling \ref run(), |
|
548 |
///it will allocate one. The destructor deallocates this |
|
549 |
/// |
|
555 |
///If you don't use this function before calling \ref run(Node) "run()" |
|
556 |
///or \ref init(), an instance will be allocated automatically. |
|
557 |
///The destructor deallocates this automatically allocated map, |
|
558 |
///of course. |
|
550 | 559 |
///\return <tt> (*this) </tt> |
551 | 560 |
Dijkstra &distMap(DistMap &m) |
552 | 561 |
{ |
553 | 562 |
if(local_dist) { |
554 | 563 |
delete _dist; |
555 | 564 |
local_dist=false; |
556 | 565 |
} |
557 | 566 |
_dist = &m; |
558 | 567 |
return *this; |
559 | 568 |
} |
560 | 569 |
|
561 | 570 |
///Sets the heap and the cross reference used by algorithm. |
562 | 571 |
|
563 | 572 |
///Sets the heap and the cross reference used by algorithm. |
564 |
///If you don't use this function before calling \ref run(), |
|
565 |
///it will allocate one. The destructor deallocates this |
|
566 |
/// |
|
573 |
///If you don't use this function before calling \ref run(Node) "run()" |
|
574 |
///or \ref init(), heap and cross reference instances will be |
|
575 |
///allocated automatically. |
|
576 |
///The destructor deallocates these automatically allocated objects, |
|
577 |
///of course. |
|
567 | 578 |
///\return <tt> (*this) </tt> |
568 | 579 |
Dijkstra &heap(Heap& hp, HeapCrossRef &cr) |
569 | 580 |
{ |
570 | 581 |
if(local_heap_cross_ref) { |
571 | 582 |
delete _heap_cross_ref; |
572 | 583 |
local_heap_cross_ref=false; |
573 | 584 |
} |
574 | 585 |
_heap_cross_ref = &cr; |
575 | 586 |
if(local_heap) { |
576 | 587 |
delete _heap; |
577 | 588 |
local_heap=false; |
578 | 589 |
} |
579 | 590 |
_heap = &hp; |
580 | 591 |
return *this; |
581 | 592 |
} |
582 | 593 |
|
583 | 594 |
private: |
584 | 595 |
|
585 | 596 |
void finalizeNodeData(Node v,Value dst) |
586 | 597 |
{ |
587 | 598 |
_processed->set(v,true); |
588 | 599 |
_dist->set(v, dst); |
589 | 600 |
} |
590 | 601 |
|
591 | 602 |
public: |
592 | 603 |
|
593 |
///\name Execution control |
|
594 |
///The simplest way to execute the algorithm is to use one of the |
|
595 |
///member functions called \ref lemon::Dijkstra::run() "run()". |
|
596 |
///\n |
|
597 |
///If you need more control on the execution, first you must call |
|
598 |
///\ref lemon::Dijkstra::init() "init()", then you can add several |
|
599 |
///source nodes with \ref lemon::Dijkstra::addSource() "addSource()". |
|
600 |
///Finally \ref lemon::Dijkstra::start() "start()" will perform the |
|
601 |
/// |
|
604 |
///\name Execution Control |
|
605 |
///The simplest way to execute the %Dijkstra algorithm is to use |
|
606 |
///one of the member functions called \ref run(Node) "run()".\n |
|
607 |
///If you need more control on the execution, first you have to call |
|
608 |
///\ref init(), then you can add several source nodes with |
|
609 |
///\ref addSource(). Finally the actual path computation can be |
|
610 |
///performed with one of the \ref start() functions. |
|
602 | 611 |
|
603 | 612 |
///@{ |
604 | 613 |
|
614 |
///\brief Initializes the internal data structures. |
|
615 |
/// |
|
605 | 616 |
///Initializes the internal data structures. |
606 |
|
|
607 |
///Initializes the internal data structures. |
|
608 |
/// |
|
609 | 617 |
void init() |
610 | 618 |
{ |
611 | 619 |
create_maps(); |
612 | 620 |
_heap->clear(); |
613 | 621 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
614 | 622 |
_pred->set(u,INVALID); |
615 | 623 |
_processed->set(u,false); |
616 | 624 |
_heap_cross_ref->set(u,Heap::PRE_HEAP); |
617 | 625 |
} |
618 | 626 |
} |
619 | 627 |
|
620 | 628 |
///Adds a new source node. |
621 | 629 |
|
622 | 630 |
///Adds a new source node to the priority heap. |
623 | 631 |
///The optional second parameter is the initial distance of the node. |
624 | 632 |
/// |
625 | 633 |
///The function checks if the node has already been added to the heap and |
626 | 634 |
///it is pushed to the heap only if either it was not in the heap |
627 | 635 |
///or the shortest path found till then is shorter than \c dst. |
628 | 636 |
void addSource(Node s,Value dst=OperationTraits::zero()) |
629 | 637 |
{ |
630 | 638 |
if(_heap->state(s) != Heap::IN_HEAP) { |
631 | 639 |
_heap->push(s,dst); |
632 | 640 |
} else if(OperationTraits::less((*_heap)[s], dst)) { |
... | ... |
@@ -660,59 +668,58 @@ |
660 | 668 |
{ |
661 | 669 |
Value newvalue = OperationTraits::plus(oldvalue, (*length)[e]); |
662 | 670 |
if ( OperationTraits::less(newvalue, (*_heap)[w]) ) { |
663 | 671 |
_heap->decrease(w, newvalue); |
664 | 672 |
_pred->set(w,e); |
665 | 673 |
} |
666 | 674 |
} |
667 | 675 |
break; |
668 | 676 |
case Heap::POST_HEAP: |
669 | 677 |
break; |
670 | 678 |
} |
671 | 679 |
} |
672 | 680 |
return v; |
673 | 681 |
} |
674 | 682 |
|
675 | 683 |
///The next node to be processed. |
676 | 684 |
|
677 | 685 |
///Returns the next node to be processed or \c INVALID if the |
678 | 686 |
///priority heap is empty. |
679 | 687 |
Node nextNode() const |
680 | 688 |
{ |
681 | 689 |
return !_heap->empty()?_heap->top():INVALID; |
682 | 690 |
} |
683 | 691 |
|
684 |
///\brief Returns \c false if there are nodes |
|
685 |
///to be processed. |
|
686 |
/// |
|
687 |
///Returns \c false if there are nodes |
|
688 |
///to be processed |
|
692 |
///Returns \c false if there are nodes to be processed. |
|
693 |
|
|
694 |
///Returns \c false if there are nodes to be processed |
|
695 |
///in the priority heap. |
|
689 | 696 |
bool emptyQueue() const { return _heap->empty(); } |
690 | 697 |
|
691 |
///Returns the number of the nodes to be processed |
|
698 |
///Returns the number of the nodes to be processed. |
|
692 | 699 |
|
693 |
///Returns the number of the nodes to be processed in the priority heap. |
|
694 |
/// |
|
700 |
///Returns the number of the nodes to be processed |
|
701 |
///in the priority heap. |
|
695 | 702 |
int queueSize() const { return _heap->size(); } |
696 | 703 |
|
697 | 704 |
///Executes the algorithm. |
698 | 705 |
|
699 | 706 |
///Executes the algorithm. |
700 | 707 |
/// |
701 | 708 |
///This method runs the %Dijkstra algorithm from the root node(s) |
702 | 709 |
///in order to compute the shortest path to each node. |
703 | 710 |
/// |
704 | 711 |
///The algorithm computes |
705 | 712 |
///- the shortest path tree (forest), |
706 | 713 |
///- the distance of each node from the root(s). |
707 | 714 |
/// |
708 | 715 |
///\pre init() must be called and at least one root node should be |
709 | 716 |
///added with addSource() before using this function. |
710 | 717 |
/// |
711 | 718 |
///\note <tt>d.start()</tt> is just a shortcut of the following code. |
712 | 719 |
///\code |
713 | 720 |
/// while ( !d.emptyQueue() ) { |
714 | 721 |
/// d.processNextNode(); |
715 | 722 |
/// } |
716 | 723 |
///\endcode |
717 | 724 |
void start() |
718 | 725 |
{ |
... | ... |
@@ -791,148 +798,150 @@ |
791 | 798 |
|
792 | 799 |
///This method runs the %Dijkstra algorithm from node \c s |
793 | 800 |
///in order to compute the shortest path to node \c t |
794 | 801 |
///(it stops searching when \c t is processed). |
795 | 802 |
/// |
796 | 803 |
///\return \c true if \c t is reachable form \c s. |
797 | 804 |
/// |
798 | 805 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is just a |
799 | 806 |
///shortcut of the following code. |
800 | 807 |
///\code |
801 | 808 |
/// d.init(); |
802 | 809 |
/// d.addSource(s); |
803 | 810 |
/// d.start(t); |
804 | 811 |
///\endcode |
805 | 812 |
bool run(Node s,Node t) { |
806 | 813 |
init(); |
807 | 814 |
addSource(s); |
808 | 815 |
start(t); |
809 | 816 |
return (*_heap_cross_ref)[t] == Heap::POST_HEAP; |
810 | 817 |
} |
811 | 818 |
|
812 | 819 |
///@} |
813 | 820 |
|
814 | 821 |
///\name Query Functions |
815 |
///The |
|
822 |
///The results of the %Dijkstra algorithm can be obtained using these |
|
816 | 823 |
///functions.\n |
817 |
///Either \ref lemon::Dijkstra::run() "run()" or |
|
818 |
///\ref lemon::Dijkstra::start() "start()" must be called before |
|
819 |
/// |
|
824 |
///Either \ref run(Node) "run()" or \ref start() should be called |
|
825 |
///before using them. |
|
820 | 826 |
|
821 | 827 |
///@{ |
822 | 828 |
|
823 | 829 |
///The shortest path to a node. |
824 | 830 |
|
825 | 831 |
///Returns the shortest path to a node. |
826 | 832 |
/// |
827 |
///\warning \c t should be |
|
833 |
///\warning \c t should be reached from the root(s). |
|
828 | 834 |
/// |
829 |
///\pre Either \ref run() or \ref start() must be called before |
|
830 |
///using this function. |
|
835 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
836 |
///must be called before using this function. |
|
831 | 837 |
Path path(Node t) const { return Path(*G, *_pred, t); } |
832 | 838 |
|
833 | 839 |
///The distance of a node from the root(s). |
834 | 840 |
|
835 | 841 |
///Returns the distance of a node from the root(s). |
836 | 842 |
/// |
837 |
///\warning If node \c v is not |
|
843 |
///\warning If node \c v is not reached from the root(s), then |
|
838 | 844 |
///the return value of this function is undefined. |
839 | 845 |
/// |
840 |
///\pre Either \ref run() or \ref start() must be called before |
|
841 |
///using this function. |
|
846 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
847 |
///must be called before using this function. |
|
842 | 848 |
Value dist(Node v) const { return (*_dist)[v]; } |
843 | 849 |
|
844 | 850 |
///Returns the 'previous arc' of the shortest path tree for a node. |
845 | 851 |
|
846 | 852 |
///This function returns the 'previous arc' of the shortest path |
847 | 853 |
///tree for the node \c v, i.e. it returns the last arc of a |
848 |
///shortest path from the root(s) to \c v. It is \c INVALID if \c v |
|
849 |
///is not reachable from the root(s) or if \c v is a root. |
|
854 |
///shortest path from a root to \c v. It is \c INVALID if \c v |
|
855 |
///is not reached from the root(s) or if \c v is a root. |
|
850 | 856 |
/// |
851 | 857 |
///The shortest path tree used here is equal to the shortest path |
852 | 858 |
///tree used in \ref predNode(). |
853 | 859 |
/// |
854 |
///\pre Either \ref run() or \ref start() must be called before |
|
855 |
///using this function. |
|
860 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
861 |
///must be called before using this function. |
|
856 | 862 |
Arc predArc(Node v) const { return (*_pred)[v]; } |
857 | 863 |
|
858 | 864 |
///Returns the 'previous node' of the shortest path tree for a node. |
859 | 865 |
|
860 | 866 |
///This function returns the 'previous node' of the shortest path |
861 | 867 |
///tree for the node \c v, i.e. it returns the last but one node |
862 |
///from a shortest path from the root(s) to \c v. It is \c INVALID |
|
863 |
///if \c v is not reachable from the root(s) or if \c v is a root. |
|
868 |
///from a shortest path from a root to \c v. It is \c INVALID |
|
869 |
///if \c v is not reached from the root(s) or if \c v is a root. |
|
864 | 870 |
/// |
865 | 871 |
///The shortest path tree used here is equal to the shortest path |
866 | 872 |
///tree used in \ref predArc(). |
867 | 873 |
/// |
868 |
///\pre Either \ref run() or \ref start() must be called before |
|
869 |
///using this function. |
|
874 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
875 |
///must be called before using this function. |
|
870 | 876 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
871 | 877 |
G->source((*_pred)[v]); } |
872 | 878 |
|
873 | 879 |
///\brief Returns a const reference to the node map that stores the |
874 | 880 |
///distances of the nodes. |
875 | 881 |
/// |
876 | 882 |
///Returns a const reference to the node map that stores the distances |
877 | 883 |
///of the nodes calculated by the algorithm. |
878 | 884 |
/// |
879 |
///\pre Either \ref run() or \ref init() |
|
885 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
880 | 886 |
///must be called before using this function. |
881 | 887 |
const DistMap &distMap() const { return *_dist;} |
882 | 888 |
|
883 | 889 |
///\brief Returns a const reference to the node map that stores the |
884 | 890 |
///predecessor arcs. |
885 | 891 |
/// |
886 | 892 |
///Returns a const reference to the node map that stores the predecessor |
887 | 893 |
///arcs, which form the shortest path tree. |
888 | 894 |
/// |
889 |
///\pre Either \ref run() or \ref init() |
|
895 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
890 | 896 |
///must be called before using this function. |
891 | 897 |
const PredMap &predMap() const { return *_pred;} |
892 | 898 |
|
893 |
///Checks if a node is |
|
899 |
///Checks if a node is reached from the root(s). |
|
894 | 900 |
|
895 |
///Returns \c true if \c v is reachable from the root(s). |
|
896 |
///\pre Either \ref run() or \ref start() |
|
901 |
///Returns \c true if \c v is reached from the root(s). |
|
902 |
/// |
|
903 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
897 | 904 |
///must be called before using this function. |
898 | 905 |
bool reached(Node v) const { return (*_heap_cross_ref)[v] != |
899 | 906 |
Heap::PRE_HEAP; } |
900 | 907 |
|
901 | 908 |
///Checks if a node is processed. |
902 | 909 |
|
903 | 910 |
///Returns \c true if \c v is processed, i.e. the shortest |
904 | 911 |
///path to \c v has already found. |
905 |
/// |
|
912 |
/// |
|
913 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
906 | 914 |
///must be called before using this function. |
907 | 915 |
bool processed(Node v) const { return (*_heap_cross_ref)[v] == |
908 | 916 |
Heap::POST_HEAP; } |
909 | 917 |
|
910 | 918 |
///The current distance of a node from the root(s). |
911 | 919 |
|
912 | 920 |
///Returns the current distance of a node from the root(s). |
913 | 921 |
///It may be decreased in the following processes. |
914 |
/// |
|
922 |
/// |
|
923 |
///\pre Either \ref run(Node) "run()" or \ref init() |
|
915 | 924 |
///must be called before using this function and |
916 | 925 |
///node \c v must be reached but not necessarily processed. |
917 | 926 |
Value currentDist(Node v) const { |
918 | 927 |
return processed(v) ? (*_dist)[v] : (*_heap)[v]; |
919 | 928 |
} |
920 | 929 |
|
921 | 930 |
///@} |
922 | 931 |
}; |
923 | 932 |
|
924 | 933 |
|
925 | 934 |
///Default traits class of dijkstra() function. |
926 | 935 |
|
927 | 936 |
///Default traits class of dijkstra() function. |
928 | 937 |
///\tparam GR The type of the digraph. |
929 | 938 |
///\tparam LM The type of the length map. |
930 | 939 |
template<class GR, class LM> |
931 | 940 |
struct DijkstraWizardDefaultTraits |
932 | 941 |
{ |
933 | 942 |
///The type of the digraph the algorithm runs on. |
934 | 943 |
typedef GR Digraph; |
935 | 944 |
///The type of the map that stores the arc lengths. |
936 | 945 |
|
937 | 946 |
///The type of the map that stores the arc lengths. |
938 | 947 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
... | ... |
@@ -1073,50 +1082,50 @@ |
1073 | 1082 |
/// Constructor. |
1074 | 1083 |
|
1075 | 1084 |
/// This constructor does not require parameters, therefore it initiates |
1076 | 1085 |
/// all of the attributes to \c 0. |
1077 | 1086 |
DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0), |
1078 | 1087 |
_dist(0), _path(0), _di(0) {} |
1079 | 1088 |
|
1080 | 1089 |
/// Constructor. |
1081 | 1090 |
|
1082 | 1091 |
/// This constructor requires two parameters, |
1083 | 1092 |
/// others are initiated to \c 0. |
1084 | 1093 |
/// \param g The digraph the algorithm runs on. |
1085 | 1094 |
/// \param l The length map. |
1086 | 1095 |
DijkstraWizardBase(const GR &g,const LM &l) : |
1087 | 1096 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
1088 | 1097 |
_length(reinterpret_cast<void*>(const_cast<LM*>(&l))), |
1089 | 1098 |
_processed(0), _pred(0), _dist(0), _path(0), _di(0) {} |
1090 | 1099 |
|
1091 | 1100 |
}; |
1092 | 1101 |
|
1093 | 1102 |
/// Auxiliary class for the function-type interface of Dijkstra algorithm. |
1094 | 1103 |
|
1095 | 1104 |
/// This auxiliary class is created to implement the |
1096 | 1105 |
/// \ref dijkstra() "function-type interface" of \ref Dijkstra algorithm. |
1097 |
/// It does not have own \ref run() method, it uses the functions |
|
1098 |
/// and features of the plain \ref Dijkstra. |
|
1106 |
/// It does not have own \ref run(Node) "run()" method, it uses the |
|
1107 |
/// functions and features of the plain \ref Dijkstra. |
|
1099 | 1108 |
/// |
1100 | 1109 |
/// This class should only be used through the \ref dijkstra() function, |
1101 | 1110 |
/// which makes it easier to use the algorithm. |
1102 | 1111 |
template<class TR> |
1103 | 1112 |
class DijkstraWizard : public TR |
1104 | 1113 |
{ |
1105 | 1114 |
typedef TR Base; |
1106 | 1115 |
|
1107 | 1116 |
///The type of the digraph the algorithm runs on. |
1108 | 1117 |
typedef typename TR::Digraph Digraph; |
1109 | 1118 |
|
1110 | 1119 |
typedef typename Digraph::Node Node; |
1111 | 1120 |
typedef typename Digraph::NodeIt NodeIt; |
1112 | 1121 |
typedef typename Digraph::Arc Arc; |
1113 | 1122 |
typedef typename Digraph::OutArcIt OutArcIt; |
1114 | 1123 |
|
1115 | 1124 |
///The type of the map that stores the arc lengths. |
1116 | 1125 |
typedef typename TR::LengthMap LengthMap; |
1117 | 1126 |
///The type of the length of the arcs. |
1118 | 1127 |
typedef typename LengthMap::Value Value; |
1119 | 1128 |
///\brief The type of the map that stores the predecessor |
1120 | 1129 |
///arcs of the shortest paths. |
1121 | 1130 |
typedef typename TR::PredMap PredMap; |
1122 | 1131 |
///The type of the map that stores the distances of the nodes. |
... | ... |
@@ -1269,38 +1278,38 @@ |
1269 | 1278 |
///for getting the distance of the target node. |
1270 | 1279 |
DijkstraWizard dist(const Value &d) |
1271 | 1280 |
{ |
1272 | 1281 |
Base::_di=reinterpret_cast<void*>(const_cast<Value*>(&d)); |
1273 | 1282 |
return *this; |
1274 | 1283 |
} |
1275 | 1284 |
|
1276 | 1285 |
}; |
1277 | 1286 |
|
1278 | 1287 |
///Function-type interface for Dijkstra algorithm. |
1279 | 1288 |
|
1280 | 1289 |
/// \ingroup shortest_path |
1281 | 1290 |
///Function-type interface for Dijkstra algorithm. |
1282 | 1291 |
/// |
1283 | 1292 |
///This function also has several \ref named-func-param "named parameters", |
1284 | 1293 |
///they are declared as the members of class \ref DijkstraWizard. |
1285 | 1294 |
///The following examples show how to use these parameters. |
1286 | 1295 |
///\code |
1287 | 1296 |
/// // Compute shortest path from node s to each node |
1288 | 1297 |
/// dijkstra(g,length).predMap(preds).distMap(dists).run(s); |
1289 | 1298 |
/// |
1290 | 1299 |
/// // Compute shortest path from s to t |
1291 | 1300 |
/// bool reached = dijkstra(g,length).path(p).dist(d).run(s,t); |
1292 | 1301 |
///\endcode |
1293 |
///\warning Don't forget to put the \ref DijkstraWizard::run() "run()" |
|
1302 |
///\warning Don't forget to put the \ref DijkstraWizard::run(Node) "run()" |
|
1294 | 1303 |
///to the end of the parameter list. |
1295 | 1304 |
///\sa DijkstraWizard |
1296 | 1305 |
///\sa Dijkstra |
1297 | 1306 |
template<class GR, class LM> |
1298 | 1307 |
DijkstraWizard<DijkstraWizardBase<GR,LM> > |
1299 | 1308 |
dijkstra(const GR &digraph, const LM &length) |
1300 | 1309 |
{ |
1301 | 1310 |
return DijkstraWizard<DijkstraWizardBase<GR,LM> >(digraph,length); |
1302 | 1311 |
} |
1303 | 1312 |
|
1304 | 1313 |
} //END OF NAMESPACE LEMON |
1305 | 1314 |
|
1306 | 1315 |
#endif |
0 comments (0 inline)