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