1 | /* -*- C++ -*- |
---|
2 | * lemon/bfs.h - Part of LEMON, a generic C++ optimization library |
---|
3 | * |
---|
4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
6 | * |
---|
7 | * Permission to use, modify and distribute this software is granted |
---|
8 | * provided that this copyright notice appears in all copies. For |
---|
9 | * precise terms see the accompanying LICENSE file. |
---|
10 | * |
---|
11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
12 | * express or implied, and with no claim as to its suitability for any |
---|
13 | * purpose. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | #ifndef LEMON_BFS_H |
---|
18 | #define LEMON_BFS_H |
---|
19 | |
---|
20 | ///\ingroup flowalgs |
---|
21 | ///\file |
---|
22 | ///\brief Bfs algorithm. |
---|
23 | |
---|
24 | #include <lemon/list_graph.h> |
---|
25 | #include <lemon/graph_utils.h> |
---|
26 | #include <lemon/invalid.h> |
---|
27 | #include <lemon/error.h> |
---|
28 | #include <lemon/maps.h> |
---|
29 | |
---|
30 | namespace lemon { |
---|
31 | |
---|
32 | |
---|
33 | |
---|
34 | ///Default traits class of Bfs class. |
---|
35 | |
---|
36 | ///Default traits class of Bfs class. |
---|
37 | ///\param GR Graph type. |
---|
38 | template<class GR> |
---|
39 | struct BfsDefaultTraits |
---|
40 | { |
---|
41 | ///The graph type the algorithm runs on. |
---|
42 | typedef GR Graph; |
---|
43 | ///\brief The type of the map that stores the last |
---|
44 | ///edges of the shortest paths. |
---|
45 | /// |
---|
46 | ///The type of the map that stores the last |
---|
47 | ///edges of the shortest paths. |
---|
48 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
49 | /// |
---|
50 | typedef typename Graph::template NodeMap<typename GR::Edge> PredMap; |
---|
51 | ///Instantiates a PredMap. |
---|
52 | |
---|
53 | ///This function instantiates a \ref PredMap. |
---|
54 | ///\param G is the graph, to which we would like to define the PredMap. |
---|
55 | ///\todo The graph alone may be insufficient to initialize |
---|
56 | static PredMap *createPredMap(const GR &G) |
---|
57 | { |
---|
58 | return new PredMap(G); |
---|
59 | } |
---|
60 | // ///\brief The type of the map that stores the last but one |
---|
61 | // ///nodes of the shortest paths. |
---|
62 | // /// |
---|
63 | // ///The type of the map that stores the last but one |
---|
64 | // ///nodes of the shortest paths. |
---|
65 | // ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
66 | // /// |
---|
67 | // typedef NullMap<typename Graph::Node,typename Graph::Node> PredNodeMap; |
---|
68 | // ///Instantiates a PredNodeMap. |
---|
69 | |
---|
70 | // ///This function instantiates a \ref PredNodeMap. |
---|
71 | // ///\param G is the graph, to which |
---|
72 | // ///we would like to define the \ref PredNodeMap |
---|
73 | // static PredNodeMap *createPredNodeMap(const GR &G) |
---|
74 | // { |
---|
75 | // return new PredNodeMap(); |
---|
76 | // } |
---|
77 | |
---|
78 | ///The type of the map that indicates which nodes are processed. |
---|
79 | |
---|
80 | ///The type of the map that indicates which nodes are processed. |
---|
81 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
82 | ///\todo named parameter to set this type, function to read and write. |
---|
83 | typedef NullMap<typename Graph::Node,bool> ProcessedMap; |
---|
84 | ///Instantiates a ProcessedMap. |
---|
85 | |
---|
86 | ///This function instantiates a \ref ProcessedMap. |
---|
87 | ///\param g is the graph, to which |
---|
88 | ///we would like to define the \ref ProcessedMap |
---|
89 | #ifdef DOXYGEN |
---|
90 | static ProcessedMap *createProcessedMap(const GR &g) |
---|
91 | #else |
---|
92 | static ProcessedMap *createProcessedMap(const GR &) |
---|
93 | #endif |
---|
94 | { |
---|
95 | return new ProcessedMap(); |
---|
96 | } |
---|
97 | ///The type of the map that indicates which nodes are reached. |
---|
98 | |
---|
99 | ///The type of the map that indicates which nodes are reached. |
---|
100 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
101 | ///\todo named parameter to set this type, function to read and write. |
---|
102 | typedef typename Graph::template NodeMap<bool> ReachedMap; |
---|
103 | ///Instantiates a ReachedMap. |
---|
104 | |
---|
105 | ///This function instantiates a \ref ReachedMap. |
---|
106 | ///\param G is the graph, to which |
---|
107 | ///we would like to define the \ref ReachedMap. |
---|
108 | static ReachedMap *createReachedMap(const GR &G) |
---|
109 | { |
---|
110 | return new ReachedMap(G); |
---|
111 | } |
---|
112 | ///The type of the map that stores the dists of the nodes. |
---|
113 | |
---|
114 | ///The type of the map that stores the dists of the nodes. |
---|
115 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
116 | /// |
---|
117 | typedef typename Graph::template NodeMap<int> DistMap; |
---|
118 | ///Instantiates a DistMap. |
---|
119 | |
---|
120 | ///This function instantiates a \ref DistMap. |
---|
121 | ///\param G is the graph, to which we would like to define the \ref DistMap |
---|
122 | static DistMap *createDistMap(const GR &G) |
---|
123 | { |
---|
124 | return new DistMap(G); |
---|
125 | } |
---|
126 | }; |
---|
127 | |
---|
128 | ///%BFS algorithm class. |
---|
129 | |
---|
130 | ///\ingroup flowalgs |
---|
131 | ///This class provides an efficient implementation of the %BFS algorithm. |
---|
132 | /// |
---|
133 | ///\param GR The graph type the algorithm runs on. The default value is |
---|
134 | ///\ref ListGraph. The value of GR is not used directly by Bfs, it |
---|
135 | ///is only passed to \ref BfsDefaultTraits. |
---|
136 | ///\param TR Traits class to set various data types used by the algorithm. |
---|
137 | ///The default traits class is |
---|
138 | ///\ref BfsDefaultTraits "BfsDefaultTraits<GR>". |
---|
139 | ///See \ref BfsDefaultTraits for the documentation of |
---|
140 | ///a Bfs traits class. |
---|
141 | /// |
---|
142 | ///\author Alpar Juttner |
---|
143 | ///\todo A compare object would be nice. |
---|
144 | |
---|
145 | #ifdef DOXYGEN |
---|
146 | template <typename GR, |
---|
147 | typename TR> |
---|
148 | #else |
---|
149 | template <typename GR=ListGraph, |
---|
150 | typename TR=BfsDefaultTraits<GR> > |
---|
151 | #endif |
---|
152 | class Bfs { |
---|
153 | public: |
---|
154 | /** |
---|
155 | * \brief \ref Exception for uninitialized parameters. |
---|
156 | * |
---|
157 | * This error represents problems in the initialization |
---|
158 | * of the parameters of the algorithms. |
---|
159 | */ |
---|
160 | class UninitializedParameter : public lemon::UninitializedParameter { |
---|
161 | public: |
---|
162 | virtual const char* exceptionName() const { |
---|
163 | return "lemon::Bfs::UninitializedParameter"; |
---|
164 | } |
---|
165 | }; |
---|
166 | |
---|
167 | typedef TR Traits; |
---|
168 | ///The type of the underlying graph. |
---|
169 | typedef typename TR::Graph Graph; |
---|
170 | ///\e |
---|
171 | typedef typename Graph::Node Node; |
---|
172 | ///\e |
---|
173 | typedef typename Graph::NodeIt NodeIt; |
---|
174 | ///\e |
---|
175 | typedef typename Graph::Edge Edge; |
---|
176 | ///\e |
---|
177 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
178 | |
---|
179 | ///\brief The type of the map that stores the last |
---|
180 | ///edges of the shortest paths. |
---|
181 | typedef typename TR::PredMap PredMap; |
---|
182 | // ///\brief The type of the map that stores the last but one |
---|
183 | // ///nodes of the shortest paths. |
---|
184 | // typedef typename TR::PredNodeMap PredNodeMap; |
---|
185 | ///The type of the map indicating which nodes are reached. |
---|
186 | typedef typename TR::ReachedMap ReachedMap; |
---|
187 | ///The type of the map indicating which nodes are processed. |
---|
188 | typedef typename TR::ProcessedMap ProcessedMap; |
---|
189 | ///The type of the map that stores the dists of the nodes. |
---|
190 | typedef typename TR::DistMap DistMap; |
---|
191 | private: |
---|
192 | /// Pointer to the underlying graph. |
---|
193 | const Graph *G; |
---|
194 | ///Pointer to the map of predecessors edges. |
---|
195 | PredMap *_pred; |
---|
196 | ///Indicates if \ref _pred is locally allocated (\c true) or not. |
---|
197 | bool local_pred; |
---|
198 | // ///Pointer to the map of predecessors nodes. |
---|
199 | // PredNodeMap *_predNode; |
---|
200 | // ///Indicates if \ref _predNode is locally allocated (\c true) or not. |
---|
201 | // bool local_predNode; |
---|
202 | ///Pointer to the map of distances. |
---|
203 | DistMap *_dist; |
---|
204 | ///Indicates if \ref _dist is locally allocated (\c true) or not. |
---|
205 | bool local_dist; |
---|
206 | ///Pointer to the map of reached status of the nodes. |
---|
207 | ReachedMap *_reached; |
---|
208 | ///Indicates if \ref _reached is locally allocated (\c true) or not. |
---|
209 | bool local_reached; |
---|
210 | ///Pointer to the map of processed status of the nodes. |
---|
211 | ProcessedMap *_processed; |
---|
212 | ///Indicates if \ref _processed is locally allocated (\c true) or not. |
---|
213 | bool local_processed; |
---|
214 | |
---|
215 | std::vector<typename Graph::Node> _queue; |
---|
216 | int _queue_head,_queue_tail,_queue_next_dist; |
---|
217 | int _curr_dist; |
---|
218 | // ///The source node of the last execution. |
---|
219 | // Node source; |
---|
220 | |
---|
221 | ///Creates the maps if necessary. |
---|
222 | |
---|
223 | ///\todo Error if \c G are \c NULL. |
---|
224 | ///\todo Better memory allocation (instead of new). |
---|
225 | void create_maps() |
---|
226 | { |
---|
227 | if(!_pred) { |
---|
228 | local_pred = true; |
---|
229 | _pred = Traits::createPredMap(*G); |
---|
230 | } |
---|
231 | // if(!_predNode) { |
---|
232 | // local_predNode = true; |
---|
233 | // _predNode = Traits::createPredNodeMap(*G); |
---|
234 | // } |
---|
235 | if(!_dist) { |
---|
236 | local_dist = true; |
---|
237 | _dist = Traits::createDistMap(*G); |
---|
238 | } |
---|
239 | if(!_reached) { |
---|
240 | local_reached = true; |
---|
241 | _reached = Traits::createReachedMap(*G); |
---|
242 | } |
---|
243 | if(!_processed) { |
---|
244 | local_processed = true; |
---|
245 | _processed = Traits::createProcessedMap(*G); |
---|
246 | } |
---|
247 | } |
---|
248 | |
---|
249 | public : |
---|
250 | |
---|
251 | ///\name Named template parameters |
---|
252 | |
---|
253 | ///@{ |
---|
254 | |
---|
255 | template <class T> |
---|
256 | struct DefPredMapTraits : public Traits { |
---|
257 | typedef T PredMap; |
---|
258 | static PredMap *createPredMap(const Graph &G) |
---|
259 | { |
---|
260 | throw UninitializedParameter(); |
---|
261 | } |
---|
262 | }; |
---|
263 | ///\ref named-templ-param "Named parameter" for setting PredMap type |
---|
264 | |
---|
265 | ///\ref named-templ-param "Named parameter" for setting PredMap type |
---|
266 | /// |
---|
267 | template <class T> |
---|
268 | class DefPredMap : public Bfs< Graph, DefPredMapTraits<T> > { }; |
---|
269 | |
---|
270 | // template <class T> |
---|
271 | // struct DefPredNodeMapTraits : public Traits { |
---|
272 | // typedef T PredNodeMap; |
---|
273 | // static PredNodeMap *createPredNodeMap(const Graph &G) |
---|
274 | // { |
---|
275 | // throw UninitializedParameter(); |
---|
276 | // } |
---|
277 | // }; |
---|
278 | // ///\ref named-templ-param "Named parameter" for setting PredNodeMap type |
---|
279 | |
---|
280 | // ///\ref named-templ-param "Named parameter" for setting PredNodeMap type |
---|
281 | // /// |
---|
282 | // template <class T> |
---|
283 | // class DefPredNodeMap : public Bfs< Graph, |
---|
284 | // LengthMap, |
---|
285 | // DefPredNodeMapTraits<T> > { }; |
---|
286 | |
---|
287 | template <class T> |
---|
288 | struct DefDistMapTraits : public Traits { |
---|
289 | typedef T DistMap; |
---|
290 | static DistMap *createDistMap(const Graph &G) |
---|
291 | { |
---|
292 | throw UninitializedParameter(); |
---|
293 | } |
---|
294 | }; |
---|
295 | ///\ref named-templ-param "Named parameter" for setting DistMap type |
---|
296 | |
---|
297 | ///\ref named-templ-param "Named parameter" for setting DistMap type |
---|
298 | /// |
---|
299 | template <class T> |
---|
300 | class DefDistMap : public Bfs< Graph, |
---|
301 | DefDistMapTraits<T> > { }; |
---|
302 | |
---|
303 | template <class T> |
---|
304 | struct DefReachedMapTraits : public Traits { |
---|
305 | typedef T ReachedMap; |
---|
306 | static ReachedMap *createReachedMap(const Graph &G) |
---|
307 | { |
---|
308 | throw UninitializedParameter(); |
---|
309 | } |
---|
310 | }; |
---|
311 | ///\ref named-templ-param "Named parameter" for setting ReachedMap type |
---|
312 | |
---|
313 | ///\ref named-templ-param "Named parameter" for setting ReachedMap type |
---|
314 | /// |
---|
315 | template <class T> |
---|
316 | class DefReachedMap : public Bfs< Graph, |
---|
317 | DefReachedMapTraits<T> > { }; |
---|
318 | |
---|
319 | struct DefGraphReachedMapTraits : public Traits { |
---|
320 | typedef typename Graph::template NodeMap<bool> ReachedMap; |
---|
321 | static ReachedMap *createReachedMap(const Graph &G) |
---|
322 | { |
---|
323 | return new ReachedMap(G); |
---|
324 | } |
---|
325 | }; |
---|
326 | template <class T> |
---|
327 | struct DefProcessedMapTraits : public Traits { |
---|
328 | typedef T ProcessedMap; |
---|
329 | static ProcessedMap *createProcessedMap(const Graph &G) |
---|
330 | { |
---|
331 | throw UninitializedParameter(); |
---|
332 | } |
---|
333 | }; |
---|
334 | ///\ref named-templ-param "Named parameter" for setting ProcessedMap type |
---|
335 | |
---|
336 | ///\ref named-templ-param "Named parameter" for setting ProcessedMap type |
---|
337 | /// |
---|
338 | template <class T> |
---|
339 | class DefProcessedMap : public Bfs< Graph, |
---|
340 | DefProcessedMapTraits<T> > { }; |
---|
341 | |
---|
342 | struct DefGraphProcessedMapTraits : public Traits { |
---|
343 | typedef typename Graph::template NodeMap<bool> ProcessedMap; |
---|
344 | static ProcessedMap *createProcessedMap(const Graph &G) |
---|
345 | { |
---|
346 | return new ProcessedMap(G); |
---|
347 | } |
---|
348 | }; |
---|
349 | ///\brief \ref named-templ-param "Named parameter" |
---|
350 | ///for setting the ProcessedMap type to be Graph::NodeMap<bool>. |
---|
351 | /// |
---|
352 | ///\ref named-templ-param "Named parameter" |
---|
353 | ///for setting the ProcessedMap type to be Graph::NodeMap<bool>. |
---|
354 | ///If you don't set it explicitly, it will be automatically allocated. |
---|
355 | template <class T> |
---|
356 | class DefProcessedMapToBeDefaultMap : |
---|
357 | public Bfs< Graph, |
---|
358 | DefGraphProcessedMapTraits> { }; |
---|
359 | |
---|
360 | ///@} |
---|
361 | |
---|
362 | public: |
---|
363 | |
---|
364 | ///Constructor. |
---|
365 | |
---|
366 | ///\param _G the graph the algorithm will run on. |
---|
367 | /// |
---|
368 | Bfs(const Graph& _G) : |
---|
369 | G(&_G), |
---|
370 | _pred(NULL), local_pred(false), |
---|
371 | // _predNode(NULL), local_predNode(false), |
---|
372 | _dist(NULL), local_dist(false), |
---|
373 | _reached(NULL), local_reached(false), |
---|
374 | _processed(NULL), local_processed(false) |
---|
375 | { } |
---|
376 | |
---|
377 | ///Destructor. |
---|
378 | ~Bfs() |
---|
379 | { |
---|
380 | if(local_pred) delete _pred; |
---|
381 | // if(local_predNode) delete _predNode; |
---|
382 | if(local_dist) delete _dist; |
---|
383 | if(local_reached) delete _reached; |
---|
384 | if(local_processed) delete _processed; |
---|
385 | } |
---|
386 | |
---|
387 | ///Sets the map storing the predecessor edges. |
---|
388 | |
---|
389 | ///Sets the map storing the predecessor edges. |
---|
390 | ///If you don't use this function before calling \ref run(), |
---|
391 | ///it will allocate one. The destructor deallocates this |
---|
392 | ///automatically allocated map, of course. |
---|
393 | ///\return <tt> (*this) </tt> |
---|
394 | Bfs &predMap(PredMap &m) |
---|
395 | { |
---|
396 | if(local_pred) { |
---|
397 | delete _pred; |
---|
398 | local_pred=false; |
---|
399 | } |
---|
400 | _pred = &m; |
---|
401 | return *this; |
---|
402 | } |
---|
403 | |
---|
404 | ///Sets the map indicating the reached nodes. |
---|
405 | |
---|
406 | ///Sets the map indicating the reached nodes. |
---|
407 | ///If you don't use this function before calling \ref run(), |
---|
408 | ///it will allocate one. The destructor deallocates this |
---|
409 | ///automatically allocated map, of course. |
---|
410 | ///\return <tt> (*this) </tt> |
---|
411 | Bfs &reachedMap(ReachedMap &m) |
---|
412 | { |
---|
413 | if(local_reached) { |
---|
414 | delete _reached; |
---|
415 | local_reached=false; |
---|
416 | } |
---|
417 | _reached = &m; |
---|
418 | return *this; |
---|
419 | } |
---|
420 | |
---|
421 | ///Sets the map indicating the processed nodes. |
---|
422 | |
---|
423 | ///Sets the map indicating the processed nodes. |
---|
424 | ///If you don't use this function before calling \ref run(), |
---|
425 | ///it will allocate one. The destructor deallocates this |
---|
426 | ///automatically allocated map, of course. |
---|
427 | ///\return <tt> (*this) </tt> |
---|
428 | Bfs &processedMap(ProcessedMap &m) |
---|
429 | { |
---|
430 | if(local_processed) { |
---|
431 | delete _processed; |
---|
432 | local_processed=false; |
---|
433 | } |
---|
434 | _processed = &m; |
---|
435 | return *this; |
---|
436 | } |
---|
437 | |
---|
438 | // ///Sets the map storing the predecessor nodes. |
---|
439 | |
---|
440 | // ///Sets the map storing the predecessor nodes. |
---|
441 | // ///If you don't use this function before calling \ref run(), |
---|
442 | // ///it will allocate one. The destructor deallocates this |
---|
443 | // ///automatically allocated map, of course. |
---|
444 | // ///\return <tt> (*this) </tt> |
---|
445 | // Bfs &predNodeMap(PredNodeMap &m) |
---|
446 | // { |
---|
447 | // if(local_predNode) { |
---|
448 | // delete _predNode; |
---|
449 | // local_predNode=false; |
---|
450 | // } |
---|
451 | // _predNode = &m; |
---|
452 | // return *this; |
---|
453 | // } |
---|
454 | |
---|
455 | ///Sets the map storing the distances calculated by the algorithm. |
---|
456 | |
---|
457 | ///Sets the map storing the distances calculated by the algorithm. |
---|
458 | ///If you don't use this function before calling \ref run(), |
---|
459 | ///it will allocate one. The destructor deallocates this |
---|
460 | ///automatically allocated map, of course. |
---|
461 | ///\return <tt> (*this) </tt> |
---|
462 | Bfs &distMap(DistMap &m) |
---|
463 | { |
---|
464 | if(local_dist) { |
---|
465 | delete _dist; |
---|
466 | local_dist=false; |
---|
467 | } |
---|
468 | _dist = &m; |
---|
469 | return *this; |
---|
470 | } |
---|
471 | |
---|
472 | public: |
---|
473 | ///\name Execution control |
---|
474 | ///The simplest way to execute the algorithm is to use |
---|
475 | ///one of the member functions called \c run(...). |
---|
476 | ///\n |
---|
477 | ///If you need more control on the execution, |
---|
478 | ///first you must call \ref init(), then you can add several source nodes |
---|
479 | ///with \ref addSource(). |
---|
480 | ///Finally \ref start() will perform the actual path |
---|
481 | ///computation. |
---|
482 | |
---|
483 | ///@{ |
---|
484 | |
---|
485 | ///Initializes the internal data structures. |
---|
486 | |
---|
487 | ///Initializes the internal data structures. |
---|
488 | /// |
---|
489 | void init() |
---|
490 | { |
---|
491 | create_maps(); |
---|
492 | _queue.resize(countNodes(*G)); |
---|
493 | _queue_head=_queue_tail=0; |
---|
494 | _curr_dist=1; |
---|
495 | for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
---|
496 | _pred->set(u,INVALID); |
---|
497 | // _predNode->set(u,INVALID); |
---|
498 | _reached->set(u,false); |
---|
499 | _processed->set(u,false); |
---|
500 | } |
---|
501 | } |
---|
502 | |
---|
503 | ///Adds a new source node. |
---|
504 | |
---|
505 | ///Adds a new source node to the set of nodes to be processed. |
---|
506 | /// |
---|
507 | void addSource(Node s) |
---|
508 | { |
---|
509 | if(!(*_reached)[s]) |
---|
510 | { |
---|
511 | _reached->set(s,true); |
---|
512 | _pred->set(s,INVALID); |
---|
513 | _dist->set(s,0); |
---|
514 | _queue[_queue_head++]=s; |
---|
515 | _queue_next_dist=_queue_head; |
---|
516 | } |
---|
517 | } |
---|
518 | |
---|
519 | ///Processes the next node. |
---|
520 | |
---|
521 | ///Processes the next node. |
---|
522 | /// |
---|
523 | ///\return The processed node. |
---|
524 | /// |
---|
525 | ///\warning The queue must not be empty! |
---|
526 | Node processNextNode() |
---|
527 | { |
---|
528 | if(_queue_tail==_queue_next_dist) { |
---|
529 | _curr_dist++; |
---|
530 | _queue_next_dist=_queue_head; |
---|
531 | } |
---|
532 | Node n=_queue[_queue_tail++]; |
---|
533 | _processed->set(n,true); |
---|
534 | Node m; |
---|
535 | for(OutEdgeIt e(*G,n);e!=INVALID;++e) |
---|
536 | if(!(*_reached)[m=G->target(e)]) { |
---|
537 | _queue[_queue_head++]=m; |
---|
538 | _reached->set(m,true); |
---|
539 | _pred->set(m,e); |
---|
540 | // _pred_node->set(m,n); |
---|
541 | _dist->set(m,_curr_dist); |
---|
542 | } |
---|
543 | return n; |
---|
544 | } |
---|
545 | |
---|
546 | ///\brief Returns \c false if there are nodes |
---|
547 | ///to be processed in the queue |
---|
548 | /// |
---|
549 | ///Returns \c false if there are nodes |
---|
550 | ///to be processed in the queue |
---|
551 | bool emptyQueue() { return _queue_tail==_queue_head; } |
---|
552 | ///Returns the number of the nodes to be processed. |
---|
553 | |
---|
554 | ///Returns the number of the nodes to be processed in the queue. |
---|
555 | /// |
---|
556 | int queueSize() { return _queue_head-_queue_tail; } |
---|
557 | |
---|
558 | ///Executes the algorithm. |
---|
559 | |
---|
560 | ///Executes the algorithm. |
---|
561 | /// |
---|
562 | ///\pre init() must be called and at least one node should be added |
---|
563 | ///with addSource() before using this function. |
---|
564 | /// |
---|
565 | ///This method runs the %BFS algorithm from the root node(s) |
---|
566 | ///in order to |
---|
567 | ///compute the |
---|
568 | ///shortest path to each node. The algorithm computes |
---|
569 | ///- The shortest path tree. |
---|
570 | ///- The distance of each node from the root(s). |
---|
571 | /// |
---|
572 | void start() |
---|
573 | { |
---|
574 | while ( !emptyQueue() ) processNextNode(); |
---|
575 | } |
---|
576 | |
---|
577 | ///Executes the algorithm until \c dest is reached. |
---|
578 | |
---|
579 | ///Executes the algorithm until \c dest is reached. |
---|
580 | /// |
---|
581 | ///\pre init() must be called and at least one node should be added |
---|
582 | ///with addSource() before using this function. |
---|
583 | /// |
---|
584 | ///This method runs the %BFS algorithm from the root node(s) |
---|
585 | ///in order to |
---|
586 | ///compute the |
---|
587 | ///shortest path to \c dest. The algorithm computes |
---|
588 | ///- The shortest path to \c dest. |
---|
589 | ///- The distance of \c dest from the root(s). |
---|
590 | /// |
---|
591 | void start(Node dest) |
---|
592 | { |
---|
593 | while ( !emptyQueue() && _queue[_queue_tail]!=dest ) processNextNode(); |
---|
594 | } |
---|
595 | |
---|
596 | ///Executes the algorithm until a condition is met. |
---|
597 | |
---|
598 | ///Executes the algorithm until a condition is met. |
---|
599 | /// |
---|
600 | ///\pre init() must be called and at least one node should be added |
---|
601 | ///with addSource() before using this function. |
---|
602 | /// |
---|
603 | ///\param nm must be a bool (or convertible) node map. The algorithm |
---|
604 | ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>. |
---|
605 | template<class NM> |
---|
606 | void start(const NM &nm) |
---|
607 | { |
---|
608 | while ( !emptyQueue() && !nm[_queue[_queue_tail]] ) processNextNode(); |
---|
609 | } |
---|
610 | |
---|
611 | ///Runs %BFS algorithm from node \c s. |
---|
612 | |
---|
613 | ///This method runs the %BFS algorithm from a root node \c s |
---|
614 | ///in order to |
---|
615 | ///compute the |
---|
616 | ///shortest path to each node. The algorithm computes |
---|
617 | ///- The shortest path tree. |
---|
618 | ///- The distance of each node from the root. |
---|
619 | /// |
---|
620 | ///\note d.run(s) is just a shortcut of the following code. |
---|
621 | ///\code |
---|
622 | /// d.init(); |
---|
623 | /// d.addSource(s); |
---|
624 | /// d.start(); |
---|
625 | ///\endcode |
---|
626 | void run(Node s) { |
---|
627 | init(); |
---|
628 | addSource(s); |
---|
629 | start(); |
---|
630 | } |
---|
631 | |
---|
632 | ///Finds the shortest path between \c s and \c t. |
---|
633 | |
---|
634 | ///Finds the shortest path between \c s and \c t. |
---|
635 | /// |
---|
636 | ///\return The length of the shortest s---t path if there exists one, |
---|
637 | ///0 otherwise. |
---|
638 | ///\note Apart from the return value, d.run(s) is |
---|
639 | ///just a shortcut of the following code. |
---|
640 | ///\code |
---|
641 | /// d.init(); |
---|
642 | /// d.addSource(s); |
---|
643 | /// d.start(t); |
---|
644 | ///\endcode |
---|
645 | int run(Node s,Node t) { |
---|
646 | init(); |
---|
647 | addSource(s); |
---|
648 | start(t); |
---|
649 | return reached(t)?_curr_dist-1+(_queue_tail==_queue_next_dist):0; |
---|
650 | } |
---|
651 | |
---|
652 | ///@} |
---|
653 | |
---|
654 | ///\name Query Functions |
---|
655 | ///The result of the %BFS algorithm can be obtained using these |
---|
656 | ///functions.\n |
---|
657 | ///Before the use of these functions, |
---|
658 | ///either run() or start() must be called. |
---|
659 | |
---|
660 | ///@{ |
---|
661 | |
---|
662 | ///Copies the shortest path to \c t into \c p |
---|
663 | |
---|
664 | ///This function copies the shortest path to \c t into \c p. |
---|
665 | ///If \c t is a source itself or unreachable, then it does not |
---|
666 | ///alter \c p. |
---|
667 | ///\todo Is it the right way to handle unreachable nodes? |
---|
668 | ///\return Returns \c true if a path to \c t was actually copied to \c p, |
---|
669 | ///\c false otherwise. |
---|
670 | ///\sa DirPath |
---|
671 | template<class P> |
---|
672 | bool getPath(P &p,Node t) |
---|
673 | { |
---|
674 | if(reached(t)) { |
---|
675 | p.clear(); |
---|
676 | typename P::Builder b(p); |
---|
677 | for(b.setStartNode(t);pred(t)!=INVALID;t=predNode(t)) |
---|
678 | b.pushFront(pred(t)); |
---|
679 | b.commit(); |
---|
680 | return true; |
---|
681 | } |
---|
682 | return false; |
---|
683 | } |
---|
684 | |
---|
685 | ///The distance of a node from the root(s). |
---|
686 | |
---|
687 | ///Returns the distance of a node from the root(s). |
---|
688 | ///\pre \ref run() must be called before using this function. |
---|
689 | ///\warning If node \c v in unreachable from the root(s) the return value |
---|
690 | ///of this function is undefined. |
---|
691 | int dist(Node v) const { return (*_dist)[v]; } |
---|
692 | |
---|
693 | ///Returns the 'previous edge' of the shortest path tree. |
---|
694 | |
---|
695 | ///For a node \c v it returns the 'previous edge' |
---|
696 | ///of the shortest path tree, |
---|
697 | ///i.e. it returns the last edge of a shortest path from the root(s) to \c |
---|
698 | ///v. It is \ref INVALID |
---|
699 | ///if \c v is unreachable from the root(s) or \c v is a root. The |
---|
700 | ///shortest path tree used here is equal to the shortest path tree used in |
---|
701 | ///\ref predNode(). |
---|
702 | ///\pre Either \ref run() or \ref start() must be called before using |
---|
703 | ///this function. |
---|
704 | ///\todo predEdge could be a better name. |
---|
705 | Edge pred(Node v) const { return (*_pred)[v];} |
---|
706 | |
---|
707 | ///Returns the 'previous node' of the shortest path tree. |
---|
708 | |
---|
709 | ///For a node \c v it returns the 'previous node' |
---|
710 | ///of the shortest path tree, |
---|
711 | ///i.e. it returns the last but one node from a shortest path from the |
---|
712 | ///root(a) to \c /v. |
---|
713 | ///It is INVALID if \c v is unreachable from the root(s) or |
---|
714 | ///if \c v itself a root. |
---|
715 | ///The shortest path tree used here is equal to the shortest path |
---|
716 | ///tree used in \ref pred(). |
---|
717 | ///\pre Either \ref run() or \ref start() must be called before |
---|
718 | ///using this function. |
---|
719 | Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
---|
720 | G->source((*_pred)[v]); } |
---|
721 | |
---|
722 | ///Returns a reference to the NodeMap of distances. |
---|
723 | |
---|
724 | ///Returns a reference to the NodeMap of distances. |
---|
725 | ///\pre Either \ref run() or \ref init() must |
---|
726 | ///be called before using this function. |
---|
727 | const DistMap &distMap() const { return *_dist;} |
---|
728 | |
---|
729 | ///Returns a reference to the shortest path tree map. |
---|
730 | |
---|
731 | ///Returns a reference to the NodeMap of the edges of the |
---|
732 | ///shortest path tree. |
---|
733 | ///\pre Either \ref run() or \ref init() |
---|
734 | ///must be called before using this function. |
---|
735 | const PredMap &predMap() const { return *_pred;} |
---|
736 | |
---|
737 | // ///Returns a reference to the map of nodes of shortest paths. |
---|
738 | |
---|
739 | // ///Returns a reference to the NodeMap of the last but one nodes of the |
---|
740 | // ///shortest path tree. |
---|
741 | // ///\pre \ref run() must be called before using this function. |
---|
742 | // const PredNodeMap &predNodeMap() const { return *_predNode;} |
---|
743 | |
---|
744 | ///Checks if a node is reachable from the root. |
---|
745 | |
---|
746 | ///Returns \c true if \c v is reachable from the root. |
---|
747 | ///\warning The source nodes are indicated as unreached. |
---|
748 | ///\pre Either \ref run() or \ref start() |
---|
749 | ///must be called before using this function. |
---|
750 | /// |
---|
751 | bool reached(Node v) { return (*_reached)[v]; } |
---|
752 | |
---|
753 | ///@} |
---|
754 | }; |
---|
755 | |
---|
756 | ///Default traits class of Bfs function. |
---|
757 | |
---|
758 | ///Default traits class of Bfs function. |
---|
759 | ///\param GR Graph type. |
---|
760 | template<class GR> |
---|
761 | struct BfsWizardDefaultTraits |
---|
762 | { |
---|
763 | ///The graph type the algorithm runs on. |
---|
764 | typedef GR Graph; |
---|
765 | ///\brief The type of the map that stores the last |
---|
766 | ///edges of the shortest paths. |
---|
767 | /// |
---|
768 | ///The type of the map that stores the last |
---|
769 | ///edges of the shortest paths. |
---|
770 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
771 | /// |
---|
772 | typedef NullMap<typename Graph::Node,typename GR::Edge> PredMap; |
---|
773 | ///Instantiates a PredMap. |
---|
774 | |
---|
775 | ///This function instantiates a \ref PredMap. |
---|
776 | ///\param g is the graph, to which we would like to define the PredMap. |
---|
777 | ///\todo The graph alone may be insufficient to initialize |
---|
778 | #ifdef DOXYGEN |
---|
779 | static PredMap *createPredMap(const GR &g) |
---|
780 | #else |
---|
781 | static PredMap *createPredMap(const GR &) |
---|
782 | #endif |
---|
783 | { |
---|
784 | return new PredMap(); |
---|
785 | } |
---|
786 | // ///\brief The type of the map that stores the last but one |
---|
787 | // ///nodes of the shortest paths. |
---|
788 | // /// |
---|
789 | // ///The type of the map that stores the last but one |
---|
790 | // ///nodes of the shortest paths. |
---|
791 | // ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
792 | // /// |
---|
793 | // typedef NullMap<typename Graph::Node,typename Graph::Node> PredNodeMap; |
---|
794 | // ///Instantiates a PredNodeMap. |
---|
795 | |
---|
796 | // ///This function instantiates a \ref PredNodeMap. |
---|
797 | // ///\param G is the graph, to which |
---|
798 | // ///we would like to define the \ref PredNodeMap |
---|
799 | // static PredNodeMap *createPredNodeMap(const GR &G) |
---|
800 | // { |
---|
801 | // return new PredNodeMap(); |
---|
802 | // } |
---|
803 | |
---|
804 | ///The type of the map that indicates which nodes are processed. |
---|
805 | |
---|
806 | ///The type of the map that indicates which nodes are processed. |
---|
807 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
808 | ///\todo named parameter to set this type, function to read and write. |
---|
809 | typedef NullMap<typename Graph::Node,bool> ProcessedMap; |
---|
810 | ///Instantiates a ProcessedMap. |
---|
811 | |
---|
812 | ///This function instantiates a \ref ProcessedMap. |
---|
813 | ///\param g is the graph, to which |
---|
814 | ///we would like to define the \ref ProcessedMap |
---|
815 | #ifdef DOXYGEN |
---|
816 | static ProcessedMap *createProcessedMap(const GR &g) |
---|
817 | #else |
---|
818 | static ProcessedMap *createProcessedMap(const GR &) |
---|
819 | #endif |
---|
820 | { |
---|
821 | return new ProcessedMap(); |
---|
822 | } |
---|
823 | ///The type of the map that indicates which nodes are reached. |
---|
824 | |
---|
825 | ///The type of the map that indicates which nodes are reached. |
---|
826 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
827 | ///\todo named parameter to set this type, function to read and write. |
---|
828 | typedef typename Graph::template NodeMap<bool> ReachedMap; |
---|
829 | ///Instantiates a ReachedMap. |
---|
830 | |
---|
831 | ///This function instantiates a \ref ReachedMap. |
---|
832 | ///\param G is the graph, to which |
---|
833 | ///we would like to define the \ref ReachedMap. |
---|
834 | static ReachedMap *createReachedMap(const GR &G) |
---|
835 | { |
---|
836 | return new ReachedMap(G); |
---|
837 | } |
---|
838 | ///The type of the map that stores the dists of the nodes. |
---|
839 | |
---|
840 | ///The type of the map that stores the dists of the nodes. |
---|
841 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
842 | /// |
---|
843 | typedef NullMap<typename Graph::Node,int> DistMap; |
---|
844 | ///Instantiates a DistMap. |
---|
845 | |
---|
846 | ///This function instantiates a \ref DistMap. |
---|
847 | ///\param g is the graph, to which we would like to define the \ref DistMap |
---|
848 | #ifdef DOXYGEN |
---|
849 | static DistMap *createDistMap(const GR &g) |
---|
850 | #else |
---|
851 | static DistMap *createDistMap(const GR &) |
---|
852 | #endif |
---|
853 | { |
---|
854 | return new DistMap(); |
---|
855 | } |
---|
856 | }; |
---|
857 | |
---|
858 | /// Default traits used by \ref BfsWizard |
---|
859 | |
---|
860 | /// To make it easier to use Bfs algorithm |
---|
861 | ///we have created a wizard class. |
---|
862 | /// This \ref BfsWizard class needs default traits, |
---|
863 | ///as well as the \ref Bfs class. |
---|
864 | /// The \ref BfsWizardBase is a class to be the default traits of the |
---|
865 | /// \ref BfsWizard class. |
---|
866 | template<class GR> |
---|
867 | class BfsWizardBase : public BfsWizardDefaultTraits<GR> |
---|
868 | { |
---|
869 | |
---|
870 | typedef BfsWizardDefaultTraits<GR> Base; |
---|
871 | protected: |
---|
872 | /// Type of the nodes in the graph. |
---|
873 | typedef typename Base::Graph::Node Node; |
---|
874 | |
---|
875 | /// Pointer to the underlying graph. |
---|
876 | void *_g; |
---|
877 | ///Pointer to the map of reached nodes. |
---|
878 | void *_reached; |
---|
879 | ///Pointer to the map of processed nodes. |
---|
880 | void *_processed; |
---|
881 | ///Pointer to the map of predecessors edges. |
---|
882 | void *_pred; |
---|
883 | // ///Pointer to the map of predecessors nodes. |
---|
884 | // void *_predNode; |
---|
885 | ///Pointer to the map of distances. |
---|
886 | void *_dist; |
---|
887 | ///Pointer to the source node. |
---|
888 | Node _source; |
---|
889 | |
---|
890 | public: |
---|
891 | /// Constructor. |
---|
892 | |
---|
893 | /// This constructor does not require parameters, therefore it initiates |
---|
894 | /// all of the attributes to default values (0, INVALID). |
---|
895 | BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0), |
---|
896 | // _predNode(0), |
---|
897 | _dist(0), _source(INVALID) {} |
---|
898 | |
---|
899 | /// Constructor. |
---|
900 | |
---|
901 | /// This constructor requires some parameters, |
---|
902 | /// listed in the parameters list. |
---|
903 | /// Others are initiated to 0. |
---|
904 | /// \param g is the initial value of \ref _g |
---|
905 | /// \param s is the initial value of \ref _source |
---|
906 | BfsWizardBase(const GR &g, Node s=INVALID) : |
---|
907 | _g((void *)&g), _reached(0), _processed(0), _pred(0), |
---|
908 | // _predNode(0), |
---|
909 | _dist(0), _source(s) {} |
---|
910 | |
---|
911 | }; |
---|
912 | |
---|
913 | /// A class to make the usage of Bfs algorithm easier |
---|
914 | |
---|
915 | /// This class is created to make it easier to use Bfs algorithm. |
---|
916 | /// It uses the functions and features of the plain \ref Bfs, |
---|
917 | /// but it is much simpler to use it. |
---|
918 | /// |
---|
919 | /// Simplicity means that the way to change the types defined |
---|
920 | /// in the traits class is based on functions that returns the new class |
---|
921 | /// and not on templatable built-in classes. |
---|
922 | /// When using the plain \ref Bfs |
---|
923 | /// the new class with the modified type comes from |
---|
924 | /// the original class by using the :: |
---|
925 | /// operator. In the case of \ref BfsWizard only |
---|
926 | /// a function have to be called and it will |
---|
927 | /// return the needed class. |
---|
928 | /// |
---|
929 | /// It does not have own \ref run method. When its \ref run method is called |
---|
930 | /// it initiates a plain \ref Bfs class, and calls the \ref Bfs::run |
---|
931 | /// method of it. |
---|
932 | template<class TR> |
---|
933 | class BfsWizard : public TR |
---|
934 | { |
---|
935 | typedef TR Base; |
---|
936 | |
---|
937 | ///The type of the underlying graph. |
---|
938 | typedef typename TR::Graph Graph; |
---|
939 | //\e |
---|
940 | typedef typename Graph::Node Node; |
---|
941 | //\e |
---|
942 | typedef typename Graph::NodeIt NodeIt; |
---|
943 | //\e |
---|
944 | typedef typename Graph::Edge Edge; |
---|
945 | //\e |
---|
946 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
947 | |
---|
948 | ///\brief The type of the map that stores |
---|
949 | ///the reached nodes |
---|
950 | typedef typename TR::ReachedMap ReachedMap; |
---|
951 | ///\brief The type of the map that stores |
---|
952 | ///the processed nodes |
---|
953 | typedef typename TR::ProcessedMap ProcessedMap; |
---|
954 | ///\brief The type of the map that stores the last |
---|
955 | ///edges of the shortest paths. |
---|
956 | typedef typename TR::PredMap PredMap; |
---|
957 | // ///\brief The type of the map that stores the last but one |
---|
958 | // ///nodes of the shortest paths. |
---|
959 | // typedef typename TR::PredNodeMap PredNodeMap; |
---|
960 | ///The type of the map that stores the dists of the nodes. |
---|
961 | typedef typename TR::DistMap DistMap; |
---|
962 | |
---|
963 | public: |
---|
964 | /// Constructor. |
---|
965 | BfsWizard() : TR() {} |
---|
966 | |
---|
967 | /// Constructor that requires parameters. |
---|
968 | |
---|
969 | /// Constructor that requires parameters. |
---|
970 | /// These parameters will be the default values for the traits class. |
---|
971 | BfsWizard(const Graph &g, Node s=INVALID) : |
---|
972 | TR(g,s) {} |
---|
973 | |
---|
974 | ///Copy constructor |
---|
975 | BfsWizard(const TR &b) : TR(b) {} |
---|
976 | |
---|
977 | ~BfsWizard() {} |
---|
978 | |
---|
979 | ///Runs Bfs algorithm from a given node. |
---|
980 | |
---|
981 | ///Runs Bfs algorithm from a given node. |
---|
982 | ///The node can be given by the \ref source function. |
---|
983 | void run() |
---|
984 | { |
---|
985 | if(Base::_source==INVALID) throw UninitializedParameter(); |
---|
986 | Bfs<Graph,TR> alg(*(Graph*)Base::_g); |
---|
987 | if(Base::_reached) |
---|
988 | alg.reachedMap(*(ReachedMap*)Base::_reached); |
---|
989 | if(Base::_processed) alg.processedMap(*(ProcessedMap*)Base::_processed); |
---|
990 | if(Base::_pred) alg.predMap(*(PredMap*)Base::_pred); |
---|
991 | // if(Base::_predNode) alg.predNodeMap(*(PredNodeMap*)Base::_predNode); |
---|
992 | if(Base::_dist) alg.distMap(*(DistMap*)Base::_dist); |
---|
993 | alg.run(Base::_source); |
---|
994 | } |
---|
995 | |
---|
996 | ///Runs Bfs algorithm from the given node. |
---|
997 | |
---|
998 | ///Runs Bfs algorithm from the given node. |
---|
999 | ///\param s is the given source. |
---|
1000 | void run(Node s) |
---|
1001 | { |
---|
1002 | Base::_source=s; |
---|
1003 | run(); |
---|
1004 | } |
---|
1005 | |
---|
1006 | template<class T> |
---|
1007 | struct DefPredMapBase : public Base { |
---|
1008 | typedef T PredMap; |
---|
1009 | static PredMap *createPredMap(const Graph &) { return 0; }; |
---|
1010 | DefPredMapBase(const TR &b) : TR(b) {} |
---|
1011 | }; |
---|
1012 | |
---|
1013 | ///\brief \ref named-templ-param "Named parameter" |
---|
1014 | ///function for setting PredMap |
---|
1015 | /// |
---|
1016 | /// \ref named-templ-param "Named parameter" |
---|
1017 | ///function for setting PredMap |
---|
1018 | /// |
---|
1019 | template<class T> |
---|
1020 | BfsWizard<DefPredMapBase<T> > predMap(const T &t) |
---|
1021 | { |
---|
1022 | Base::_pred=(void *)&t; |
---|
1023 | return BfsWizard<DefPredMapBase<T> >(*this); |
---|
1024 | } |
---|
1025 | |
---|
1026 | |
---|
1027 | template<class T> |
---|
1028 | struct DefReachedMapBase : public Base { |
---|
1029 | typedef T ReachedMap; |
---|
1030 | static ReachedMap *createReachedMap(const Graph &) { return 0; }; |
---|
1031 | DefReachedMapBase(const TR &b) : TR(b) {} |
---|
1032 | }; |
---|
1033 | |
---|
1034 | ///\brief \ref named-templ-param "Named parameter" |
---|
1035 | ///function for setting ReachedMap |
---|
1036 | /// |
---|
1037 | /// \ref named-templ-param "Named parameter" |
---|
1038 | ///function for setting ReachedMap |
---|
1039 | /// |
---|
1040 | template<class T> |
---|
1041 | BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) |
---|
1042 | { |
---|
1043 | Base::_pred=(void *)&t; |
---|
1044 | return BfsWizard<DefReachedMapBase<T> >(*this); |
---|
1045 | } |
---|
1046 | |
---|
1047 | |
---|
1048 | template<class T> |
---|
1049 | struct DefProcessedMapBase : public Base { |
---|
1050 | typedef T ProcessedMap; |
---|
1051 | static ProcessedMap *createProcessedMap(const Graph &) { return 0; }; |
---|
1052 | DefProcessedMapBase(const TR &b) : TR(b) {} |
---|
1053 | }; |
---|
1054 | |
---|
1055 | ///\brief \ref named-templ-param "Named parameter" |
---|
1056 | ///function for setting ProcessedMap |
---|
1057 | /// |
---|
1058 | /// \ref named-templ-param "Named parameter" |
---|
1059 | ///function for setting ProcessedMap |
---|
1060 | /// |
---|
1061 | template<class T> |
---|
1062 | BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) |
---|
1063 | { |
---|
1064 | Base::_pred=(void *)&t; |
---|
1065 | return BfsWizard<DefProcessedMapBase<T> >(*this); |
---|
1066 | } |
---|
1067 | |
---|
1068 | |
---|
1069 | // template<class T> |
---|
1070 | // struct DefPredNodeMapBase : public Base { |
---|
1071 | // typedef T PredNodeMap; |
---|
1072 | // static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; }; |
---|
1073 | // DefPredNodeMapBase(const TR &b) : TR(b) {} |
---|
1074 | // }; |
---|
1075 | |
---|
1076 | // ///\brief \ref named-templ-param "Named parameter" |
---|
1077 | // ///function for setting PredNodeMap type |
---|
1078 | // /// |
---|
1079 | // /// \ref named-templ-param "Named parameter" |
---|
1080 | // ///function for setting PredNodeMap type |
---|
1081 | // /// |
---|
1082 | // template<class T> |
---|
1083 | // BfsWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t) |
---|
1084 | // { |
---|
1085 | // Base::_predNode=(void *)&t; |
---|
1086 | // return BfsWizard<DefPredNodeMapBase<T> >(*this); |
---|
1087 | // } |
---|
1088 | |
---|
1089 | template<class T> |
---|
1090 | struct DefDistMapBase : public Base { |
---|
1091 | typedef T DistMap; |
---|
1092 | static DistMap *createDistMap(const Graph &) { return 0; }; |
---|
1093 | DefDistMapBase(const TR &b) : TR(b) {} |
---|
1094 | }; |
---|
1095 | |
---|
1096 | ///\brief \ref named-templ-param "Named parameter" |
---|
1097 | ///function for setting DistMap type |
---|
1098 | /// |
---|
1099 | /// \ref named-templ-param "Named parameter" |
---|
1100 | ///function for setting DistMap type |
---|
1101 | /// |
---|
1102 | template<class T> |
---|
1103 | BfsWizard<DefDistMapBase<T> > distMap(const T &t) |
---|
1104 | { |
---|
1105 | Base::_dist=(void *)&t; |
---|
1106 | return BfsWizard<DefDistMapBase<T> >(*this); |
---|
1107 | } |
---|
1108 | |
---|
1109 | /// Sets the source node, from which the Bfs algorithm runs. |
---|
1110 | |
---|
1111 | /// Sets the source node, from which the Bfs algorithm runs. |
---|
1112 | /// \param s is the source node. |
---|
1113 | BfsWizard<TR> &source(Node s) |
---|
1114 | { |
---|
1115 | Base::_source=s; |
---|
1116 | return *this; |
---|
1117 | } |
---|
1118 | |
---|
1119 | }; |
---|
1120 | |
---|
1121 | ///Function type interface for Bfs algorithm. |
---|
1122 | |
---|
1123 | /// \ingroup flowalgs |
---|
1124 | ///Function type interface for Bfs algorithm. |
---|
1125 | /// |
---|
1126 | ///This function also has several |
---|
1127 | ///\ref named-templ-func-param "named parameters", |
---|
1128 | ///they are declared as the members of class \ref BfsWizard. |
---|
1129 | ///The following |
---|
1130 | ///example shows how to use these parameters. |
---|
1131 | ///\code |
---|
1132 | /// bfs(g,source).predMap(preds).run(); |
---|
1133 | ///\endcode |
---|
1134 | ///\warning Don't forget to put the \ref BfsWizard::run() "run()" |
---|
1135 | ///to the end of the parameter list. |
---|
1136 | ///\sa BfsWizard |
---|
1137 | ///\sa Bfs |
---|
1138 | template<class GR> |
---|
1139 | BfsWizard<BfsWizardBase<GR> > |
---|
1140 | bfs(const GR &g,typename GR::Node s=INVALID) |
---|
1141 | { |
---|
1142 | return BfsWizard<BfsWizardBase<GR> >(g,s); |
---|
1143 | } |
---|
1144 | |
---|
1145 | } //END OF NAMESPACE LEMON |
---|
1146 | |
---|
1147 | #endif |
---|
1148 | |
---|