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