1 | /* -*- C++ -*- |
---|
2 | * |
---|
3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
4 | * |
---|
5 | * Copyright (C) 2003-2007 |
---|
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 reach 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& reach) |
---|
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 | reach = reach || (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 node map. If one node |
---|
519 | ///with true value is reachable from the processed node then the |
---|
520 | ///rnode parameter will be set to the first of such nodes. |
---|
521 | /// |
---|
522 | ///\param nm The node map of possible targets. |
---|
523 | ///\retval rnode The reached target node. |
---|
524 | ///\return The processed node. |
---|
525 | /// |
---|
526 | ///\warning The queue must not be empty! |
---|
527 | template<class NM> |
---|
528 | Node processNextNode(const NM& nm, Node& rnode) |
---|
529 | { |
---|
530 | if(_queue_tail==_queue_next_dist) { |
---|
531 | _curr_dist++; |
---|
532 | _queue_next_dist=_queue_head; |
---|
533 | } |
---|
534 | Node n=_queue[_queue_tail++]; |
---|
535 | _processed->set(n,true); |
---|
536 | Node m; |
---|
537 | for(OutEdgeIt e(*G,n);e!=INVALID;++e) |
---|
538 | if(!(*_reached)[m=G->target(e)]) { |
---|
539 | _queue[_queue_head++]=m; |
---|
540 | _reached->set(m,true); |
---|
541 | _pred->set(m,e); |
---|
542 | _dist->set(m,_curr_dist); |
---|
543 | if (nm[m] && rnode == INVALID) rnode = m; |
---|
544 | } |
---|
545 | return n; |
---|
546 | } |
---|
547 | |
---|
548 | ///Next node to be processed. |
---|
549 | |
---|
550 | ///Next node to be processed. |
---|
551 | /// |
---|
552 | ///\return The next node to be processed or INVALID if the queue is |
---|
553 | /// empty. |
---|
554 | Node nextNode() |
---|
555 | { |
---|
556 | return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID; |
---|
557 | } |
---|
558 | |
---|
559 | ///\brief Returns \c false if there are nodes |
---|
560 | ///to be processed in the queue |
---|
561 | /// |
---|
562 | ///Returns \c false if there are nodes |
---|
563 | ///to be processed in the queue |
---|
564 | bool emptyQueue() { return _queue_tail==_queue_head; } |
---|
565 | ///Returns the number of the nodes to be processed. |
---|
566 | |
---|
567 | ///Returns the number of the nodes to be processed in the queue. |
---|
568 | int queueSize() { return _queue_head-_queue_tail; } |
---|
569 | |
---|
570 | ///Executes the algorithm. |
---|
571 | |
---|
572 | ///Executes the algorithm. |
---|
573 | /// |
---|
574 | ///\pre init() must be called and at least one node should be added |
---|
575 | ///with addSource() before using this function. |
---|
576 | /// |
---|
577 | ///This method runs the %BFS algorithm from the root node(s) |
---|
578 | ///in order to |
---|
579 | ///compute the |
---|
580 | ///shortest path to each node. The algorithm computes |
---|
581 | ///- The shortest path tree. |
---|
582 | ///- The distance of each node from the root(s). |
---|
583 | void start() |
---|
584 | { |
---|
585 | while ( !emptyQueue() ) processNextNode(); |
---|
586 | } |
---|
587 | |
---|
588 | ///Executes the algorithm until \c dest is reached. |
---|
589 | |
---|
590 | ///Executes the algorithm until \c dest is reached. |
---|
591 | /// |
---|
592 | ///\pre init() must be called and at least one node should be added |
---|
593 | ///with addSource() before using this function. |
---|
594 | /// |
---|
595 | ///This method runs the %BFS algorithm from the root node(s) |
---|
596 | ///in order to |
---|
597 | ///compute the |
---|
598 | ///shortest path to \c dest. The algorithm computes |
---|
599 | ///- The shortest path to \c dest. |
---|
600 | ///- The distance of \c dest from the root(s). |
---|
601 | void start(Node dest) |
---|
602 | { |
---|
603 | bool reach = false; |
---|
604 | while ( !emptyQueue() && !reach ) processNextNode(dest, reach); |
---|
605 | } |
---|
606 | |
---|
607 | ///Executes the algorithm until a condition is met. |
---|
608 | |
---|
609 | ///Executes the algorithm until a condition is met. |
---|
610 | /// |
---|
611 | ///\pre init() must be called and at least one node should be added |
---|
612 | ///with addSource() before using this function. |
---|
613 | /// |
---|
614 | ///\param nm must be a bool (or convertible) node map. The |
---|
615 | ///algorithm will stop when it reaches a node \c v with |
---|
616 | /// <tt>nm[v]</tt> true. |
---|
617 | /// |
---|
618 | ///\return The reached node \c v with <tt>nm[v]<\tt> true or |
---|
619 | ///\c INVALID if no such node was found. |
---|
620 | template<class NM> |
---|
621 | Node start(const NM &nm) |
---|
622 | { |
---|
623 | Node rnode = INVALID; |
---|
624 | while ( !emptyQueue() && rnode == INVALID ) { |
---|
625 | processNextNode(nm, rnode); |
---|
626 | } |
---|
627 | return rnode; |
---|
628 | } |
---|
629 | |
---|
630 | ///Runs %BFS algorithm from node \c s. |
---|
631 | |
---|
632 | ///This method runs the %BFS algorithm from a root node \c s |
---|
633 | ///in order to |
---|
634 | ///compute the |
---|
635 | ///shortest path to each node. The algorithm computes |
---|
636 | ///- The shortest path tree. |
---|
637 | ///- The distance of each node from the root. |
---|
638 | /// |
---|
639 | ///\note b.run(s) is just a shortcut of the following code. |
---|
640 | ///\code |
---|
641 | /// b.init(); |
---|
642 | /// b.addSource(s); |
---|
643 | /// b.start(); |
---|
644 | ///\endcode |
---|
645 | void run(Node s) { |
---|
646 | init(); |
---|
647 | addSource(s); |
---|
648 | start(); |
---|
649 | } |
---|
650 | |
---|
651 | ///Finds the shortest path between \c s and \c t. |
---|
652 | |
---|
653 | ///Finds the shortest path between \c s and \c t. |
---|
654 | /// |
---|
655 | ///\return The length of the shortest s---t path if there exists one, |
---|
656 | ///0 otherwise. |
---|
657 | ///\note Apart from the return value, b.run(s) is |
---|
658 | ///just a shortcut of the following code. |
---|
659 | ///\code |
---|
660 | /// b.init(); |
---|
661 | /// b.addSource(s); |
---|
662 | /// b.start(t); |
---|
663 | ///\endcode |
---|
664 | int run(Node s,Node t) { |
---|
665 | init(); |
---|
666 | addSource(s); |
---|
667 | start(t); |
---|
668 | return reached(t) ? _curr_dist : 0; |
---|
669 | } |
---|
670 | |
---|
671 | ///@} |
---|
672 | |
---|
673 | ///\name Query Functions |
---|
674 | ///The result of the %BFS algorithm can be obtained using these |
---|
675 | ///functions.\n |
---|
676 | ///Before the use of these functions, |
---|
677 | ///either run() or start() must be calleb. |
---|
678 | |
---|
679 | ///@{ |
---|
680 | |
---|
681 | typedef PredMapPath<Graph, PredMap> Path; |
---|
682 | |
---|
683 | ///Gives back the shortest path. |
---|
684 | |
---|
685 | ///Gives back the shortest path. |
---|
686 | ///\pre The \c t should be reachable from the source. |
---|
687 | Path path(Node t) |
---|
688 | { |
---|
689 | return Path(*G, *_pred, t); |
---|
690 | } |
---|
691 | |
---|
692 | ///The distance of a node from the root(s). |
---|
693 | |
---|
694 | ///Returns the distance of a node from the root(s). |
---|
695 | ///\pre \ref run() must be called before using this function. |
---|
696 | ///\warning If node \c v in unreachable from the root(s) the return value |
---|
697 | ///of this function is undefined. |
---|
698 | int dist(Node v) const { return (*_dist)[v]; } |
---|
699 | |
---|
700 | ///Returns the 'previous edge' of the shortest path tree. |
---|
701 | |
---|
702 | ///For a node \c v it returns the 'previous edge' |
---|
703 | ///of the shortest path tree, |
---|
704 | ///i.e. it returns the last edge of a shortest path from the root(s) to \c |
---|
705 | ///v. It is \ref INVALID |
---|
706 | ///if \c v is unreachable from the root(s) or \c v is a root. The |
---|
707 | ///shortest path tree used here is equal to the shortest path tree used in |
---|
708 | ///\ref predNode(). |
---|
709 | ///\pre Either \ref run() or \ref start() must be called before using |
---|
710 | ///this function. |
---|
711 | Edge predEdge(Node v) const { return (*_pred)[v];} |
---|
712 | |
---|
713 | ///Returns the 'previous node' of the shortest path tree. |
---|
714 | |
---|
715 | ///For a node \c v it returns the 'previous node' |
---|
716 | ///of the shortest path tree, |
---|
717 | ///i.e. it returns the last but one node from a shortest path from the |
---|
718 | ///root(a) to \c /v. |
---|
719 | ///It is INVALID if \c v is unreachable from the root(s) or |
---|
720 | ///if \c v itself a root. |
---|
721 | ///The shortest path tree used here is equal to the shortest path |
---|
722 | ///tree used in \ref predEdge(). |
---|
723 | ///\pre Either \ref run() or \ref start() must be called before |
---|
724 | ///using this function. |
---|
725 | Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
---|
726 | G->source((*_pred)[v]); } |
---|
727 | |
---|
728 | ///Returns a reference to the NodeMap of distances. |
---|
729 | |
---|
730 | ///Returns a reference to the NodeMap of distances. |
---|
731 | ///\pre Either \ref run() or \ref init() must |
---|
732 | ///be called before using this function. |
---|
733 | const DistMap &distMap() const { return *_dist;} |
---|
734 | |
---|
735 | ///Returns a reference to the shortest path tree map. |
---|
736 | |
---|
737 | ///Returns a reference to the NodeMap of the edges of the |
---|
738 | ///shortest path tree. |
---|
739 | ///\pre Either \ref run() or \ref init() |
---|
740 | ///must be called before using this function. |
---|
741 | const PredMap &predMap() const { return *_pred;} |
---|
742 | |
---|
743 | ///Checks if a node is reachable from the root. |
---|
744 | |
---|
745 | ///Returns \c true if \c v is reachable from the root. |
---|
746 | ///\warning The source nodes are indicated as unreached. |
---|
747 | ///\pre Either \ref run() or \ref start() |
---|
748 | ///must be called before using this function. |
---|
749 | /// |
---|
750 | bool reached(Node v) { return (*_reached)[v]; } |
---|
751 | |
---|
752 | ///@} |
---|
753 | }; |
---|
754 | |
---|
755 | ///Default traits class of Bfs function. |
---|
756 | |
---|
757 | ///Default traits class of Bfs function. |
---|
758 | ///\param GR Graph type. |
---|
759 | template<class GR> |
---|
760 | struct BfsWizardDefaultTraits |
---|
761 | { |
---|
762 | ///The graph type the algorithm runs on. |
---|
763 | typedef GR Graph; |
---|
764 | ///\brief The type of the map that stores the last |
---|
765 | ///edges of the shortest paths. |
---|
766 | /// |
---|
767 | ///The type of the map that stores the last |
---|
768 | ///edges of the shortest paths. |
---|
769 | ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
---|
770 | /// |
---|
771 | typedef NullMap<typename Graph::Node,typename GR::Edge> PredMap; |
---|
772 | ///Instantiates a PredMap. |
---|
773 | |
---|
774 | ///This function instantiates a \ref PredMap. |
---|
775 | ///\param g is the graph, to which we would like to define the PredMap. |
---|
776 | ///\todo The graph alone may be insufficient to initialize |
---|
777 | #ifdef DOXYGEN |
---|
778 | static PredMap *createPredMap(const GR &g) |
---|
779 | #else |
---|
780 | static PredMap *createPredMap(const GR &) |
---|
781 | #endif |
---|
782 | { |
---|
783 | return new PredMap(); |
---|
784 | } |
---|
785 | |
---|
786 | ///The type of the map that indicates which nodes are processed. |
---|
787 | |
---|
788 | ///The type of the map that indicates which nodes are processed. |
---|
789 | ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
---|
790 | ///\todo named parameter to set this type, function to read and write. |
---|
791 | typedef NullMap<typename Graph::Node,bool> ProcessedMap; |
---|
792 | ///Instantiates a ProcessedMap. |
---|
793 | |
---|
794 | ///This function instantiates a \ref ProcessedMap. |
---|
795 | ///\param g is the graph, to which |
---|
796 | ///we would like to define the \ref ProcessedMap |
---|
797 | #ifdef DOXYGEN |
---|
798 | static ProcessedMap *createProcessedMap(const GR &g) |
---|
799 | #else |
---|
800 | static ProcessedMap *createProcessedMap(const GR &) |
---|
801 | #endif |
---|
802 | { |
---|
803 | return new ProcessedMap(); |
---|
804 | } |
---|
805 | ///The type of the map that indicates which nodes are reached. |
---|
806 | |
---|
807 | ///The type of the map that indicates which nodes are reached. |
---|
808 | ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
---|
809 | ///\todo named parameter to set this type, function to read and write. |
---|
810 | typedef typename Graph::template NodeMap<bool> ReachedMap; |
---|
811 | ///Instantiates a ReachedMap. |
---|
812 | |
---|
813 | ///This function instantiates a \ref ReachedMap. |
---|
814 | ///\param G is the graph, to which |
---|
815 | ///we would like to define the \ref ReachedMap. |
---|
816 | static ReachedMap *createReachedMap(const GR &G) |
---|
817 | { |
---|
818 | return new ReachedMap(G); |
---|
819 | } |
---|
820 | ///The type of the map that stores the dists of the nodes. |
---|
821 | |
---|
822 | ///The type of the map that stores the dists of the nodes. |
---|
823 | ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
---|
824 | /// |
---|
825 | typedef NullMap<typename Graph::Node,int> DistMap; |
---|
826 | ///Instantiates a DistMap. |
---|
827 | |
---|
828 | ///This function instantiates a \ref DistMap. |
---|
829 | ///\param g is the graph, to which we would like to define the \ref DistMap |
---|
830 | #ifdef DOXYGEN |
---|
831 | static DistMap *createDistMap(const GR &g) |
---|
832 | #else |
---|
833 | static DistMap *createDistMap(const GR &) |
---|
834 | #endif |
---|
835 | { |
---|
836 | return new DistMap(); |
---|
837 | } |
---|
838 | }; |
---|
839 | |
---|
840 | /// Default traits used by \ref BfsWizard |
---|
841 | |
---|
842 | /// To make it easier to use Bfs algorithm |
---|
843 | ///we have created a wizard class. |
---|
844 | /// This \ref BfsWizard class needs default traits, |
---|
845 | ///as well as the \ref Bfs class. |
---|
846 | /// The \ref BfsWizardBase is a class to be the default traits of the |
---|
847 | /// \ref BfsWizard class. |
---|
848 | template<class GR> |
---|
849 | class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
---|
850 | { |
---|
851 | |
---|
852 | typedef BfsWizardDefaultTraits<GR> Base; |
---|
853 | protected: |
---|
854 | /// Type of the nodes in the graph. |
---|
855 | typedef typename Base::Graph::Node Node; |
---|
856 | |
---|
857 | /// Pointer to the underlying graph. |
---|
858 | void *_g; |
---|
859 | ///Pointer to the map of reached nodes. |
---|
860 | void *_reached; |
---|
861 | ///Pointer to the map of processed nodes. |
---|
862 | void *_processed; |
---|
863 | ///Pointer to the map of predecessors edges. |
---|
864 | void *_pred; |
---|
865 | ///Pointer to the map of distances. |
---|
866 | void *_dist; |
---|
867 | ///Pointer to the source node. |
---|
868 | Node _source; |
---|
869 | |
---|
870 | public: |
---|
871 | /// Constructor. |
---|
872 | |
---|
873 | /// This constructor does not require parameters, therefore it initiates |
---|
874 | /// all of the attributes to default values (0, INVALID). |
---|
875 | BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
---|
876 | _dist(0), _source(INVALID) {} |
---|
877 | |
---|
878 | /// Constructor. |
---|
879 | |
---|
880 | /// This constructor requires some parameters, |
---|
881 | /// listed in the parameters list. |
---|
882 | /// Others are initiated to 0. |
---|
883 | /// \param g is the initial value of \ref _g |
---|
884 | /// \param s is the initial value of \ref _source |
---|
885 | BfsWizardBase(const GR &g, Node s=INVALID) : |
---|
886 | _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
---|
887 | _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {} |
---|
888 | |
---|
889 | }; |
---|
890 | |
---|
891 | /// A class to make the usage of Bfs algorithm easier |
---|
892 | |
---|
893 | /// This class is created to make it easier to use Bfs algorithm. |
---|
894 | /// It uses the functions and features of the plain \ref Bfs, |
---|
895 | /// but it is much simpler to use it. |
---|
896 | /// |
---|
897 | /// Simplicity means that the way to change the types defined |
---|
898 | /// in the traits class is based on functions that returns the new class |
---|
899 | /// and not on templatable built-in classes. |
---|
900 | /// When using the plain \ref Bfs |
---|
901 | /// the new class with the modified type comes from |
---|
902 | /// the original class by using the :: |
---|
903 | /// operator. In the case of \ref BfsWizard only |
---|
904 | /// a function have to be called and it will |
---|
905 | /// return the needed class. |
---|
906 | /// |
---|
907 | /// It does not have own \ref run method. When its \ref run method is called |
---|
908 | /// it initiates a plain \ref Bfs class, and calls the \ref Bfs::run |
---|
909 | /// method of it. |
---|
910 | template<class TR> |
---|
911 | class BfsWizard : public TR |
---|
912 | { |
---|
913 | typedef TR Base; |
---|
914 | |
---|
915 | ///The type of the underlying graph. |
---|
916 | typedef typename TR::Graph Graph; |
---|
917 | //\e |
---|
918 | typedef typename Graph::Node Node; |
---|
919 | //\e |
---|
920 | typedef typename Graph::NodeIt NodeIt; |
---|
921 | //\e |
---|
922 | typedef typename Graph::Edge Edge; |
---|
923 | //\e |
---|
924 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
925 | |
---|
926 | ///\brief The type of the map that stores |
---|
927 | ///the reached nodes |
---|
928 | typedef typename TR::ReachedMap ReachedMap; |
---|
929 | ///\brief The type of the map that stores |
---|
930 | ///the processed nodes |
---|
931 | typedef typename TR::ProcessedMap ProcessedMap; |
---|
932 | ///\brief The type of the map that stores the last |
---|
933 | ///edges of the shortest paths. |
---|
934 | typedef typename TR::PredMap PredMap; |
---|
935 | ///The type of the map that stores the dists of the nodes. |
---|
936 | typedef typename TR::DistMap DistMap; |
---|
937 | |
---|
938 | public: |
---|
939 | /// Constructor. |
---|
940 | BfsWizard() : TR() {} |
---|
941 | |
---|
942 | /// Constructor that requires parameters. |
---|
943 | |
---|
944 | /// Constructor that requires parameters. |
---|
945 | /// These parameters will be the default values for the traits class. |
---|
946 | BfsWizard(const Graph &g, Node s=INVALID) : |
---|
947 | TR(g,s) {} |
---|
948 | |
---|
949 | ///Copy constructor |
---|
950 | BfsWizard(const TR &b) : TR(b) {} |
---|
951 | |
---|
952 | ~BfsWizard() {} |
---|
953 | |
---|
954 | ///Runs Bfs algorithm from a given node. |
---|
955 | |
---|
956 | ///Runs Bfs algorithm from a given node. |
---|
957 | ///The node can be given by the \ref source function. |
---|
958 | void run() |
---|
959 | { |
---|
960 | if(Base::_source==INVALID) throw UninitializedParameter(); |
---|
961 | Bfs<Graph,TR> alg(*reinterpret_cast<const Graph*>(Base::_g)); |
---|
962 | if(Base::_reached) |
---|
963 | alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached)); |
---|
964 | if(Base::_processed) |
---|
965 | alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
---|
966 | if(Base::_pred) |
---|
967 | alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
---|
968 | if(Base::_dist) |
---|
969 | alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
---|
970 | alg.run(Base::_source); |
---|
971 | } |
---|
972 | |
---|
973 | ///Runs Bfs algorithm from the given node. |
---|
974 | |
---|
975 | ///Runs Bfs algorithm from the given node. |
---|
976 | ///\param s is the given source. |
---|
977 | void run(Node s) |
---|
978 | { |
---|
979 | Base::_source=s; |
---|
980 | run(); |
---|
981 | } |
---|
982 | |
---|
983 | template<class T> |
---|
984 | struct DefPredMapBase : public Base { |
---|
985 | typedef T PredMap; |
---|
986 | static PredMap *createPredMap(const Graph &) { return 0; }; |
---|
987 | DefPredMapBase(const TR &b) : TR(b) {} |
---|
988 | }; |
---|
989 | |
---|
990 | ///\brief \ref named-templ-param "Named parameter" |
---|
991 | ///function for setting PredMap |
---|
992 | /// |
---|
993 | /// \ref named-templ-param "Named parameter" |
---|
994 | ///function for setting PredMap |
---|
995 | /// |
---|
996 | template<class T> |
---|
997 | BfsWizard<DefPredMapBase<T> > predMap(const T &t) |
---|
998 | { |
---|
999 | Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
---|
1000 | return BfsWizard<DefPredMapBase<T> >(*this); |
---|
1001 | } |
---|
1002 | |
---|
1003 | |
---|
1004 | template<class T> |
---|
1005 | struct DefReachedMapBase : public Base { |
---|
1006 | typedef T ReachedMap; |
---|
1007 | static ReachedMap *createReachedMap(const Graph &) { return 0; }; |
---|
1008 | DefReachedMapBase(const TR &b) : TR(b) {} |
---|
1009 | }; |
---|
1010 | |
---|
1011 | ///\brief \ref named-templ-param "Named parameter" |
---|
1012 | ///function for setting ReachedMap |
---|
1013 | /// |
---|
1014 | /// \ref named-templ-param "Named parameter" |
---|
1015 | ///function for setting ReachedMap |
---|
1016 | /// |
---|
1017 | template<class T> |
---|
1018 | BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) |
---|
1019 | { |
---|
1020 | Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
---|
1021 | return BfsWizard<DefReachedMapBase<T> >(*this); |
---|
1022 | } |
---|
1023 | |
---|
1024 | |
---|
1025 | template<class T> |
---|
1026 | struct DefProcessedMapBase : public Base { |
---|
1027 | typedef T ProcessedMap; |
---|
1028 | static ProcessedMap *createProcessedMap(const Graph &) { return 0; }; |
---|
1029 | DefProcessedMapBase(const TR &b) : TR(b) {} |
---|
1030 | }; |
---|
1031 | |
---|
1032 | ///\brief \ref named-templ-param "Named parameter" |
---|
1033 | ///function for setting ProcessedMap |
---|
1034 | /// |
---|
1035 | /// \ref named-templ-param "Named parameter" |
---|
1036 | ///function for setting ProcessedMap |
---|
1037 | /// |
---|
1038 | template<class T> |
---|
1039 | BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) |
---|
1040 | { |
---|
1041 | Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
---|
1042 | return BfsWizard<DefProcessedMapBase<T> >(*this); |
---|
1043 | } |
---|
1044 | |
---|
1045 | |
---|
1046 | template<class T> |
---|
1047 | struct DefDistMapBase : public Base { |
---|
1048 | typedef T DistMap; |
---|
1049 | static DistMap *createDistMap(const Graph &) { return 0; }; |
---|
1050 | DefDistMapBase(const TR &b) : TR(b) {} |
---|
1051 | }; |
---|
1052 | |
---|
1053 | ///\brief \ref named-templ-param "Named parameter" |
---|
1054 | ///function for setting DistMap type |
---|
1055 | /// |
---|
1056 | /// \ref named-templ-param "Named parameter" |
---|
1057 | ///function for setting DistMap type |
---|
1058 | /// |
---|
1059 | template<class T> |
---|
1060 | BfsWizard<DefDistMapBase<T> > distMap(const T &t) |
---|
1061 | { |
---|
1062 | Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
---|
1063 | return BfsWizard<DefDistMapBase<T> >(*this); |
---|
1064 | } |
---|
1065 | |
---|
1066 | /// Sets the source node, from which the Bfs algorithm runs. |
---|
1067 | |
---|
1068 | /// Sets the source node, from which the Bfs algorithm runs. |
---|
1069 | /// \param s is the source node. |
---|
1070 | BfsWizard<TR> &source(Node s) |
---|
1071 | { |
---|
1072 | Base::_source=s; |
---|
1073 | return *this; |
---|
1074 | } |
---|
1075 | |
---|
1076 | }; |
---|
1077 | |
---|
1078 | ///Function type interface for Bfs algorithm. |
---|
1079 | |
---|
1080 | /// \ingroup search |
---|
1081 | ///Function type interface for Bfs algorithm. |
---|
1082 | /// |
---|
1083 | ///This function also has several |
---|
1084 | ///\ref named-templ-func-param "named parameters", |
---|
1085 | ///they are declared as the members of class \ref BfsWizard. |
---|
1086 | ///The following |
---|
1087 | ///example shows how to use these parameters. |
---|
1088 | ///\code |
---|
1089 | /// bfs(g,source).predMap(preds).run(); |
---|
1090 | ///\endcode |
---|
1091 | ///\warning Don't forget to put the \ref BfsWizard::run() "run()" |
---|
1092 | ///to the end of the parameter list. |
---|
1093 | ///\sa BfsWizard |
---|
1094 | ///\sa Bfs |
---|
1095 | template<class GR> |
---|
1096 | BfsWizard<BfsWizardBase<GR> > |
---|
1097 | bfs(const GR &g,typename GR::Node s=INVALID) |
---|
1098 | { |
---|
1099 | return BfsWizard<BfsWizardBase<GR> >(g,s); |
---|
1100 | } |
---|
1101 | |
---|
1102 | #ifdef DOXYGEN |
---|
1103 | /// \brief Visitor class for bfs. |
---|
1104 | /// |
---|
1105 | /// It gives a simple interface for a functional interface for bfs |
---|
1106 | /// traversal. The traversal on a linear data structure. |
---|
1107 | template <typename _Graph> |
---|
1108 | struct BfsVisitor { |
---|
1109 | typedef _Graph Graph; |
---|
1110 | typedef typename Graph::Edge Edge; |
---|
1111 | typedef typename Graph::Node Node; |
---|
1112 | /// \brief Called when the edge reach a node. |
---|
1113 | /// |
---|
1114 | /// It is called when the bfs find an edge which target is not |
---|
1115 | /// reached yet. |
---|
1116 | void discover(const Edge& edge) {} |
---|
1117 | /// \brief Called when the node reached first time. |
---|
1118 | /// |
---|
1119 | /// It is Called when the node reached first time. |
---|
1120 | void reach(const Node& node) {} |
---|
1121 | /// \brief Called when the edge examined but target of the edge |
---|
1122 | /// already discovered. |
---|
1123 | /// |
---|
1124 | /// It called when the edge examined but the target of the edge |
---|
1125 | /// already discovered. |
---|
1126 | void examine(const Edge& edge) {} |
---|
1127 | /// \brief Called for the source node of the bfs. |
---|
1128 | /// |
---|
1129 | /// It is called for the source node of the bfs. |
---|
1130 | void start(const Node& node) {} |
---|
1131 | /// \brief Called when the node processed. |
---|
1132 | /// |
---|
1133 | /// It is Called when the node processed. |
---|
1134 | void process(const Node& node) {} |
---|
1135 | }; |
---|
1136 | #else |
---|
1137 | template <typename _Graph> |
---|
1138 | struct BfsVisitor { |
---|
1139 | typedef _Graph Graph; |
---|
1140 | typedef typename Graph::Edge Edge; |
---|
1141 | typedef typename Graph::Node Node; |
---|
1142 | void discover(const Edge&) {} |
---|
1143 | void reach(const Node&) {} |
---|
1144 | void examine(const Edge&) {} |
---|
1145 | void start(const Node&) {} |
---|
1146 | void process(const Node&) {} |
---|
1147 | |
---|
1148 | template <typename _Visitor> |
---|
1149 | struct Constraints { |
---|
1150 | void constraints() { |
---|
1151 | Edge edge; |
---|
1152 | Node node; |
---|
1153 | visitor.discover(edge); |
---|
1154 | visitor.reach(node); |
---|
1155 | visitor.examine(edge); |
---|
1156 | visitor.start(node); |
---|
1157 | visitor.process(node); |
---|
1158 | } |
---|
1159 | _Visitor& visitor; |
---|
1160 | }; |
---|
1161 | }; |
---|
1162 | #endif |
---|
1163 | |
---|
1164 | /// \brief Default traits class of BfsVisit class. |
---|
1165 | /// |
---|
1166 | /// Default traits class of BfsVisit class. |
---|
1167 | /// \param _Graph Graph type. |
---|
1168 | template<class _Graph> |
---|
1169 | struct BfsVisitDefaultTraits { |
---|
1170 | |
---|
1171 | /// \brief The graph type the algorithm runs on. |
---|
1172 | typedef _Graph Graph; |
---|
1173 | |
---|
1174 | /// \brief The type of the map that indicates which nodes are reached. |
---|
1175 | /// |
---|
1176 | /// The type of the map that indicates which nodes are reached. |
---|
1177 | /// It must meet the \ref concepts::WriteMap "WriteMap" concept. |
---|
1178 | /// \todo named parameter to set this type, function to read and write. |
---|
1179 | typedef typename Graph::template NodeMap<bool> ReachedMap; |
---|
1180 | |
---|
1181 | /// \brief Instantiates a ReachedMap. |
---|
1182 | /// |
---|
1183 | /// This function instantiates a \ref ReachedMap. |
---|
1184 | /// \param graph is the graph, to which |
---|
1185 | /// we would like to define the \ref ReachedMap. |
---|
1186 | static ReachedMap *createReachedMap(const Graph &graph) { |
---|
1187 | return new ReachedMap(graph); |
---|
1188 | } |
---|
1189 | |
---|
1190 | }; |
---|
1191 | |
---|
1192 | /// %BFS Visit algorithm class. |
---|
1193 | |
---|
1194 | /// \ingroup search |
---|
1195 | /// This class provides an efficient implementation of the %BFS algorithm |
---|
1196 | /// with visitor interface. |
---|
1197 | /// |
---|
1198 | /// The %BfsVisit class provides an alternative interface to the Bfs |
---|
1199 | /// class. It works with callback mechanism, the BfsVisit object calls |
---|
1200 | /// on every bfs event the \c Visitor class member functions. |
---|
1201 | /// |
---|
1202 | /// \param _Graph The graph type the algorithm runs on. The default value is |
---|
1203 | /// \ref ListGraph. The value of _Graph is not used directly by Bfs, it |
---|
1204 | /// is only passed to \ref BfsDefaultTraits. |
---|
1205 | /// \param _Visitor The Visitor object for the algorithm. The |
---|
1206 | /// \ref BfsVisitor "BfsVisitor<_Graph>" is an empty Visitor which |
---|
1207 | /// does not observe the Bfs events. If you want to observe the bfs |
---|
1208 | /// events you should implement your own Visitor class. |
---|
1209 | /// \param _Traits Traits class to set various data types used by the |
---|
1210 | /// algorithm. The default traits class is |
---|
1211 | /// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Graph>". |
---|
1212 | /// See \ref BfsVisitDefaultTraits for the documentation of |
---|
1213 | /// a Bfs visit traits class. |
---|
1214 | /// |
---|
1215 | /// \author Jacint Szabo, Alpar Juttner and Balazs Dezso |
---|
1216 | #ifdef DOXYGEN |
---|
1217 | template <typename _Graph, typename _Visitor, typename _Traits> |
---|
1218 | #else |
---|
1219 | template <typename _Graph = ListGraph, |
---|
1220 | typename _Visitor = BfsVisitor<_Graph>, |
---|
1221 | typename _Traits = BfsDefaultTraits<_Graph> > |
---|
1222 | #endif |
---|
1223 | class BfsVisit { |
---|
1224 | public: |
---|
1225 | |
---|
1226 | /// \brief \ref Exception for uninitialized parameters. |
---|
1227 | /// |
---|
1228 | /// This error represents problems in the initialization |
---|
1229 | /// of the parameters of the algorithms. |
---|
1230 | class UninitializedParameter : public lemon::UninitializedParameter { |
---|
1231 | public: |
---|
1232 | virtual const char* what() const throw() |
---|
1233 | { |
---|
1234 | return "lemon::BfsVisit::UninitializedParameter"; |
---|
1235 | } |
---|
1236 | }; |
---|
1237 | |
---|
1238 | typedef _Traits Traits; |
---|
1239 | |
---|
1240 | typedef typename Traits::Graph Graph; |
---|
1241 | |
---|
1242 | typedef _Visitor Visitor; |
---|
1243 | |
---|
1244 | ///The type of the map indicating which nodes are reached. |
---|
1245 | typedef typename Traits::ReachedMap ReachedMap; |
---|
1246 | |
---|
1247 | private: |
---|
1248 | |
---|
1249 | typedef typename Graph::Node Node; |
---|
1250 | typedef typename Graph::NodeIt NodeIt; |
---|
1251 | typedef typename Graph::Edge Edge; |
---|
1252 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
1253 | |
---|
1254 | /// Pointer to the underlying graph. |
---|
1255 | const Graph *_graph; |
---|
1256 | /// Pointer to the visitor object. |
---|
1257 | Visitor *_visitor; |
---|
1258 | ///Pointer to the map of reached status of the nodes. |
---|
1259 | ReachedMap *_reached; |
---|
1260 | ///Indicates if \ref _reached is locally allocated (\c true) or not. |
---|
1261 | bool local_reached; |
---|
1262 | |
---|
1263 | std::vector<typename Graph::Node> _list; |
---|
1264 | int _list_front, _list_back; |
---|
1265 | |
---|
1266 | /// \brief Creates the maps if necessary. |
---|
1267 | /// |
---|
1268 | /// Creates the maps if necessary. |
---|
1269 | void create_maps() { |
---|
1270 | if(!_reached) { |
---|
1271 | local_reached = true; |
---|
1272 | _reached = Traits::createReachedMap(*_graph); |
---|
1273 | } |
---|
1274 | } |
---|
1275 | |
---|
1276 | protected: |
---|
1277 | |
---|
1278 | BfsVisit() {} |
---|
1279 | |
---|
1280 | public: |
---|
1281 | |
---|
1282 | typedef BfsVisit Create; |
---|
1283 | |
---|
1284 | /// \name Named template parameters |
---|
1285 | |
---|
1286 | ///@{ |
---|
1287 | template <class T> |
---|
1288 | struct DefReachedMapTraits : public Traits { |
---|
1289 | typedef T ReachedMap; |
---|
1290 | static ReachedMap *createReachedMap(const Graph &graph) { |
---|
1291 | throw UninitializedParameter(); |
---|
1292 | } |
---|
1293 | }; |
---|
1294 | /// \brief \ref named-templ-param "Named parameter" for setting |
---|
1295 | /// ReachedMap type |
---|
1296 | /// |
---|
1297 | /// \ref named-templ-param "Named parameter" for setting ReachedMap type |
---|
1298 | template <class T> |
---|
1299 | struct DefReachedMap : public BfsVisit< Graph, Visitor, |
---|
1300 | DefReachedMapTraits<T> > { |
---|
1301 | typedef BfsVisit< Graph, Visitor, DefReachedMapTraits<T> > Create; |
---|
1302 | }; |
---|
1303 | ///@} |
---|
1304 | |
---|
1305 | public: |
---|
1306 | |
---|
1307 | /// \brief Constructor. |
---|
1308 | /// |
---|
1309 | /// Constructor. |
---|
1310 | /// |
---|
1311 | /// \param graph the graph the algorithm will run on. |
---|
1312 | /// \param visitor The visitor of the algorithm. |
---|
1313 | /// |
---|
1314 | BfsVisit(const Graph& graph, Visitor& visitor) |
---|
1315 | : _graph(&graph), _visitor(&visitor), |
---|
1316 | _reached(0), local_reached(false) {} |
---|
1317 | |
---|
1318 | /// \brief Destructor. |
---|
1319 | /// |
---|
1320 | /// Destructor. |
---|
1321 | ~BfsVisit() { |
---|
1322 | if(local_reached) delete _reached; |
---|
1323 | } |
---|
1324 | |
---|
1325 | /// \brief Sets the map indicating if a node is reached. |
---|
1326 | /// |
---|
1327 | /// Sets the map indicating if a node is reached. |
---|
1328 | /// If you don't use this function before calling \ref run(), |
---|
1329 | /// it will allocate one. The destuctor deallocates this |
---|
1330 | /// automatically allocated map, of course. |
---|
1331 | /// \return <tt> (*this) </tt> |
---|
1332 | BfsVisit &reachedMap(ReachedMap &m) { |
---|
1333 | if(local_reached) { |
---|
1334 | delete _reached; |
---|
1335 | local_reached = false; |
---|
1336 | } |
---|
1337 | _reached = &m; |
---|
1338 | return *this; |
---|
1339 | } |
---|
1340 | |
---|
1341 | public: |
---|
1342 | /// \name Execution control |
---|
1343 | /// The simplest way to execute the algorithm is to use |
---|
1344 | /// one of the member functions called \c run(...). |
---|
1345 | /// \n |
---|
1346 | /// If you need more control on the execution, |
---|
1347 | /// first you must call \ref init(), then you can adda source node |
---|
1348 | /// with \ref addSource(). |
---|
1349 | /// Finally \ref start() will perform the actual path |
---|
1350 | /// computation. |
---|
1351 | |
---|
1352 | /// @{ |
---|
1353 | /// \brief Initializes the internal data structures. |
---|
1354 | /// |
---|
1355 | /// Initializes the internal data structures. |
---|
1356 | /// |
---|
1357 | void init() { |
---|
1358 | create_maps(); |
---|
1359 | _list.resize(countNodes(*_graph)); |
---|
1360 | _list_front = _list_back = -1; |
---|
1361 | for (NodeIt u(*_graph) ; u != INVALID ; ++u) { |
---|
1362 | _reached->set(u, false); |
---|
1363 | } |
---|
1364 | } |
---|
1365 | |
---|
1366 | /// \brief Adds a new source node. |
---|
1367 | /// |
---|
1368 | /// Adds a new source node to the set of nodes to be processed. |
---|
1369 | void addSource(Node s) { |
---|
1370 | if(!(*_reached)[s]) { |
---|
1371 | _reached->set(s,true); |
---|
1372 | _visitor->start(s); |
---|
1373 | _visitor->reach(s); |
---|
1374 | _list[++_list_back] = s; |
---|
1375 | } |
---|
1376 | } |
---|
1377 | |
---|
1378 | /// \brief Processes the next node. |
---|
1379 | /// |
---|
1380 | /// Processes the next node. |
---|
1381 | /// |
---|
1382 | /// \return The processed node. |
---|
1383 | /// |
---|
1384 | /// \pre The queue must not be empty! |
---|
1385 | Node processNextNode() { |
---|
1386 | Node n = _list[++_list_front]; |
---|
1387 | _visitor->process(n); |
---|
1388 | Edge e; |
---|
1389 | for (_graph->firstOut(e, n); e != INVALID; _graph->nextOut(e)) { |
---|
1390 | Node m = _graph->target(e); |
---|
1391 | if (!(*_reached)[m]) { |
---|
1392 | _visitor->discover(e); |
---|
1393 | _visitor->reach(m); |
---|
1394 | _reached->set(m, true); |
---|
1395 | _list[++_list_back] = m; |
---|
1396 | } else { |
---|
1397 | _visitor->examine(e); |
---|
1398 | } |
---|
1399 | } |
---|
1400 | return n; |
---|
1401 | } |
---|
1402 | |
---|
1403 | /// \brief Processes the next node. |
---|
1404 | /// |
---|
1405 | /// Processes the next node. And checks that the given target node |
---|
1406 | /// is reached. If the target node is reachable from the processed |
---|
1407 | /// node then the reached parameter will be set true. The reached |
---|
1408 | /// parameter should be initially false. |
---|
1409 | /// |
---|
1410 | /// \param target The target node. |
---|
1411 | /// \retval reach Indicates that the target node is reached. |
---|
1412 | /// \return The processed node. |
---|
1413 | /// |
---|
1414 | /// \warning The queue must not be empty! |
---|
1415 | Node processNextNode(Node target, bool& reach) { |
---|
1416 | Node n = _list[++_list_front]; |
---|
1417 | _visitor->process(n); |
---|
1418 | Edge e; |
---|
1419 | for (_graph->firstOut(e, n); e != INVALID; _graph->nextOut(e)) { |
---|
1420 | Node m = _graph->target(e); |
---|
1421 | if (!(*_reached)[m]) { |
---|
1422 | _visitor->discover(e); |
---|
1423 | _visitor->reach(m); |
---|
1424 | _reached->set(m, true); |
---|
1425 | _list[++_list_back] = m; |
---|
1426 | reach = reach || (target == m); |
---|
1427 | } else { |
---|
1428 | _visitor->examine(e); |
---|
1429 | } |
---|
1430 | } |
---|
1431 | return n; |
---|
1432 | } |
---|
1433 | |
---|
1434 | /// \brief Processes the next node. |
---|
1435 | /// |
---|
1436 | /// Processes the next node. And checks that at least one of |
---|
1437 | /// reached node has true value in the \c nm node map. If one node |
---|
1438 | /// with true value is reachable from the processed node then the |
---|
1439 | /// rnode parameter will be set to the first of such nodes. |
---|
1440 | /// |
---|
1441 | /// \param nm The node map of possible targets. |
---|
1442 | /// \retval rnode The reached target node. |
---|
1443 | /// \return The processed node. |
---|
1444 | /// |
---|
1445 | /// \warning The queue must not be empty! |
---|
1446 | template <typename NM> |
---|
1447 | Node processNextNode(const NM& nm, Node& rnode) { |
---|
1448 | Node n = _list[++_list_front]; |
---|
1449 | _visitor->process(n); |
---|
1450 | Edge e; |
---|
1451 | for (_graph->firstOut(e, n); e != INVALID; _graph->nextOut(e)) { |
---|
1452 | Node m = _graph->target(e); |
---|
1453 | if (!(*_reached)[m]) { |
---|
1454 | _visitor->discover(e); |
---|
1455 | _visitor->reach(m); |
---|
1456 | _reached->set(m, true); |
---|
1457 | _list[++_list_back] = m; |
---|
1458 | if (nm[m] && rnode == INVALID) rnode = m; |
---|
1459 | } else { |
---|
1460 | _visitor->examine(e); |
---|
1461 | } |
---|
1462 | } |
---|
1463 | return n; |
---|
1464 | } |
---|
1465 | |
---|
1466 | /// \brief Next node to be processed. |
---|
1467 | /// |
---|
1468 | /// Next node to be processed. |
---|
1469 | /// |
---|
1470 | /// \return The next node to be processed or INVALID if the stack is |
---|
1471 | /// empty. |
---|
1472 | Node nextNode() { |
---|
1473 | return _list_front != _list_back ? _list[_list_front + 1] : INVALID; |
---|
1474 | } |
---|
1475 | |
---|
1476 | /// \brief Returns \c false if there are nodes |
---|
1477 | /// to be processed in the queue |
---|
1478 | /// |
---|
1479 | /// Returns \c false if there are nodes |
---|
1480 | /// to be processed in the queue |
---|
1481 | bool emptyQueue() { return _list_front == _list_back; } |
---|
1482 | |
---|
1483 | /// \brief Returns the number of the nodes to be processed. |
---|
1484 | /// |
---|
1485 | /// Returns the number of the nodes to be processed in the queue. |
---|
1486 | int queueSize() { return _list_back - _list_front; } |
---|
1487 | |
---|
1488 | /// \brief Executes the algorithm. |
---|
1489 | /// |
---|
1490 | /// Executes the algorithm. |
---|
1491 | /// |
---|
1492 | /// \pre init() must be called and at least one node should be added |
---|
1493 | /// with addSource() before using this function. |
---|
1494 | void start() { |
---|
1495 | while ( !emptyQueue() ) processNextNode(); |
---|
1496 | } |
---|
1497 | |
---|
1498 | /// \brief Executes the algorithm until \c dest is reached. |
---|
1499 | /// |
---|
1500 | /// Executes the algorithm until \c dest is reached. |
---|
1501 | /// |
---|
1502 | /// \pre init() must be called and at least one node should be added |
---|
1503 | /// with addSource() before using this function. |
---|
1504 | void start(Node dest) { |
---|
1505 | bool reach = false; |
---|
1506 | while ( !emptyQueue() && !reach ) processNextNode(dest, reach); |
---|
1507 | } |
---|
1508 | |
---|
1509 | /// \brief Executes the algorithm until a condition is met. |
---|
1510 | /// |
---|
1511 | /// Executes the algorithm until a condition is met. |
---|
1512 | /// |
---|
1513 | /// \pre init() must be called and at least one node should be added |
---|
1514 | /// with addSource() before using this function. |
---|
1515 | /// |
---|
1516 | ///\param nm must be a bool (or convertible) node map. The |
---|
1517 | ///algorithm will stop when it reaches a node \c v with |
---|
1518 | /// <tt>nm[v]</tt> true. |
---|
1519 | /// |
---|
1520 | ///\return The reached node \c v with <tt>nm[v]<\tt> true or |
---|
1521 | ///\c INVALID if no such node was found. |
---|
1522 | template <typename NM> |
---|
1523 | Node start(const NM &nm) { |
---|
1524 | Node rnode = INVALID; |
---|
1525 | while ( !emptyQueue() && rnode == INVALID ) { |
---|
1526 | processNextNode(nm, rnode); |
---|
1527 | } |
---|
1528 | return rnode; |
---|
1529 | } |
---|
1530 | |
---|
1531 | /// \brief Runs %BFSVisit algorithm from node \c s. |
---|
1532 | /// |
---|
1533 | /// This method runs the %BFS algorithm from a root node \c s. |
---|
1534 | /// \note b.run(s) is just a shortcut of the following code. |
---|
1535 | ///\code |
---|
1536 | /// b.init(); |
---|
1537 | /// b.addSource(s); |
---|
1538 | /// b.start(); |
---|
1539 | ///\endcode |
---|
1540 | void run(Node s) { |
---|
1541 | init(); |
---|
1542 | addSource(s); |
---|
1543 | start(); |
---|
1544 | } |
---|
1545 | |
---|
1546 | /// \brief Runs %BFSVisit algorithm to visit all nodes in the graph. |
---|
1547 | /// |
---|
1548 | /// This method runs the %BFS algorithm in order to |
---|
1549 | /// compute the %BFS path to each node. The algorithm computes |
---|
1550 | /// - The %BFS tree. |
---|
1551 | /// - The distance of each node from the root in the %BFS tree. |
---|
1552 | /// |
---|
1553 | ///\note b.run() is just a shortcut of the following code. |
---|
1554 | ///\code |
---|
1555 | /// b.init(); |
---|
1556 | /// for (NodeIt it(graph); it != INVALID; ++it) { |
---|
1557 | /// if (!b.reached(it)) { |
---|
1558 | /// b.addSource(it); |
---|
1559 | /// b.start(); |
---|
1560 | /// } |
---|
1561 | /// } |
---|
1562 | ///\endcode |
---|
1563 | void run() { |
---|
1564 | init(); |
---|
1565 | for (NodeIt it(*_graph); it != INVALID; ++it) { |
---|
1566 | if (!reached(it)) { |
---|
1567 | addSource(it); |
---|
1568 | start(); |
---|
1569 | } |
---|
1570 | } |
---|
1571 | } |
---|
1572 | ///@} |
---|
1573 | |
---|
1574 | /// \name Query Functions |
---|
1575 | /// The result of the %BFS algorithm can be obtained using these |
---|
1576 | /// functions.\n |
---|
1577 | /// Before the use of these functions, |
---|
1578 | /// either run() or start() must be called. |
---|
1579 | ///@{ |
---|
1580 | |
---|
1581 | /// \brief Checks if a node is reachable from the root. |
---|
1582 | /// |
---|
1583 | /// Returns \c true if \c v is reachable from the root(s). |
---|
1584 | /// \warning The source nodes are inditated as unreachable. |
---|
1585 | /// \pre Either \ref run() or \ref start() |
---|
1586 | /// must be called before using this function. |
---|
1587 | /// |
---|
1588 | bool reached(Node v) { return (*_reached)[v]; } |
---|
1589 | ///@} |
---|
1590 | }; |
---|
1591 | |
---|
1592 | } //END OF NAMESPACE LEMON |
---|
1593 | |
---|
1594 | #endif |
---|
1595 | |
---|