A strange compilation failure (under cygwin) is fixed.
Version 0.3 should be fixed as well.
2 * src/lemon/dfs.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
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.
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
22 ///\brief Dfs algorithm.
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>
34 ///Default traits class of Dfs class.
36 ///Default traits class of Dfs class.
37 ///\param GR Graph type.
39 struct DfsDefaultTraits
41 ///The graph type the algorithm runs on.
43 ///\brief The type of the map that stores the last
44 ///edges of the %DFS paths.
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.
50 typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
51 ///Instantiates a PredMap.
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)
58 return new PredMap(G);
60 // ///\brief The type of the map that stores the last but one
61 // ///nodes of the %DFS paths.
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.
67 // typedef NullMap<typename Graph::Node,typename Graph::Node> PredNodeMap;
68 // ///Instantiates a PredNodeMap.
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)
75 // return new PredNodeMap();
78 ///The type of the map that indicates which nodes are processed.
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.
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 &G)
91 return new ProcessedMap();
93 ///The type of the map that indicates which nodes are reached.
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.
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)
106 return new ReachedMap(G);
108 ///The type of the map that stores the dists of the nodes.
110 ///The type of the map that stores the dists of the nodes.
111 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
113 typedef typename Graph::template NodeMap<int> DistMap;
114 ///Instantiates a DistMap.
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)
120 return new DistMap(G);
124 ///%DFS algorithm class.
127 ///This class provides an efficient implementation of the %DFS algorithm.
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.
138 ///\author Jacint Szabo and Alpar Juttner
139 ///\todo A compare object would be nice.
142 template <typename GR,
145 template <typename GR=ListGraph,
146 typename TR=DfsDefaultTraits<GR> >
151 * \brief \ref Exception for uninitialized parameters.
153 * This error represents problems in the initialization
154 * of the parameters of the algorithms.
156 class UninitializedParameter : public lemon::UninitializedParameter {
158 virtual const char* exceptionName() const {
159 return "lemon::Dfs::UninitializedParameter";
164 ///The type of the underlying graph.
165 typedef typename TR::Graph Graph;
167 typedef typename Graph::Node Node;
169 typedef typename Graph::NodeIt NodeIt;
171 typedef typename Graph::Edge Edge;
173 typedef typename Graph::OutEdgeIt OutEdgeIt;
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;
188 /// Pointer to the underlying graph.
190 ///Pointer to the map of predecessors edges.
192 ///Indicates if \ref _pred is locally allocated (\c true) or not.
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.
200 ///Indicates if \ref _dist is locally allocated (\c true) or not.
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.
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;
211 std::vector<typename Graph::OutEdgeIt> _stack;
213 // ///The source node of the last execution.
216 ///Creates the maps if necessary.
218 ///\todo Error if \c G are \c NULL.
219 ///\todo Better memory allocation (instead of new).
224 _pred = Traits::createPredMap(*G);
227 // local_predNode = true;
228 // _predNode = Traits::createPredNodeMap(*G);
232 _dist = Traits::createDistMap(*G);
235 local_reached = true;
236 _reached = Traits::createReachedMap(*G);
239 local_processed = true;
240 _processed = Traits::createProcessedMap(*G);
246 ///\name Named template parameters
251 struct DefPredMapTraits : public Traits {
253 static PredMap *createPredMap(const Graph &G)
255 throw UninitializedParameter();
258 ///\ref named-templ-param "Named parameter" for setting PredMap type
260 ///\ref named-templ-param "Named parameter" for setting PredMap type
263 class DefPredMap : public Dfs< Graph,
264 DefPredMapTraits<T> > { };
266 // template <class T>
267 // struct DefPredNodeMapTraits : public Traits {
268 // typedef T PredNodeMap;
269 // static PredNodeMap *createPredNodeMap(const Graph &G)
271 // throw UninitializedParameter();
274 // ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
276 // ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
278 // template <class T>
279 // class DefPredNodeMap : public Dfs< Graph,
281 // DefPredNodeMapTraits<T> > { };
284 struct DefDistMapTraits : public Traits {
286 static DistMap *createDistMap(const Graph &G)
288 throw UninitializedParameter();
291 ///\ref named-templ-param "Named parameter" for setting DistMap type
293 ///\ref named-templ-param "Named parameter" for setting DistMap type
296 class DefDistMap : public Dfs< Graph,
297 DefDistMapTraits<T> > { };
300 struct DefReachedMapTraits : public Traits {
301 typedef T ReachedMap;
302 static ReachedMap *createReachedMap(const Graph &G)
304 throw UninitializedParameter();
307 ///\ref named-templ-param "Named parameter" for setting ReachedMap type
309 ///\ref named-templ-param "Named parameter" for setting ReachedMap type
312 class DefReachedMap : public Dfs< Graph,
313 DefReachedMapTraits<T> > { };
315 struct DefGraphReachedMapTraits : public Traits {
316 typedef typename Graph::template NodeMap<bool> ReachedMap;
317 static ReachedMap *createReachedMap(const Graph &G)
319 return new ReachedMap(G);
323 struct DefProcessedMapTraits : public Traits {
324 typedef T ProcessedMap;
325 static ProcessedMap *createProcessedMap(const Graph &G)
327 throw UninitializedParameter();
330 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
332 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
335 class DefProcessedMap : public Dfs< Graph,
336 DefProcessedMapTraits<T> > { };
338 struct DefGraphProcessedMapTraits : public Traits {
339 typedef typename Graph::template NodeMap<bool> ProcessedMap;
340 static ProcessedMap *createProcessedMap(const Graph &G)
342 return new ProcessedMap(G);
345 ///\brief \ref named-templ-param "Named parameter"
346 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
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.
352 class DefProcessedMapToBeDefaultMap :
354 DefGraphProcessedMapTraits> { };
362 ///\param _G the graph the algorithm will run on.
364 Dfs(const Graph& _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)
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;
383 ///Sets the map storing the predecessor edges.
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)
400 // ///Sets the map storing the predecessor nodes.
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)
409 // if(local_predNode) {
411 // local_predNode=false;
417 ///Sets the map storing the distances calculated by the algorithm.
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)
434 ///Sets the map indicating if a node is reached.
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)
451 ///Sets the map indicating if a node is processed.
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)
460 if(local_processed) {
462 local_processed=false;
469 ///\name Execution control
470 ///The simplest way to execute the algorithm is to use
471 ///one of the member functions called \c run(...).
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
481 ///Initializes the internal data structures.
483 ///Initializes the internal data structures.
488 _stack.resize(countNodes(*G));
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);
498 ///Adds a new source node.
500 ///Adds a new source node to the set of nodes to be processed.
502 ///\bug dist's are wrong (or at least strange) in case of multiple sources.
503 void addSource(Node s)
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);
515 ///Processes the next node.
517 ///Processes the next node.
519 ///\warning The stack must not be empty!
520 void processNextEdge()
523 Edge e=_stack[_stack_head];
524 if(!(*_reached)[m=G->target(e)]) {
526 _reached->set(m,true);
527 // _pred_node->set(m,G->source(e));
528 _stack[++_stack_head] = OutEdgeIt(*G, m);
529 _dist->set(m,_stack_head);
533 while(_stack_head>=0 &&
534 (n=G->source(_stack[_stack_head]),
535 ++_stack[_stack_head]==INVALID))
537 _processed->set(n,true);
543 ///\brief Returns \c false if there are nodes
544 ///to be processed in the queue
546 ///Returns \c false if there are nodes
547 ///to be processed in the queue
548 bool emptyQueue() { return _stack_head<0; }
549 ///Returns the number of the nodes to be processed.
551 ///Returns the number of the nodes to be processed in the queue.
553 int queueSize() { return _stack_head+1; }
555 ///Executes the algorithm.
557 ///Executes the algorithm.
559 ///\pre init() must be called and at least one node should be added
560 ///with addSource() before using this function.
562 ///This method runs the %DFS algorithm from the root node(s)
565 ///%DFS path to each node. The algorithm computes
567 ///- The distance of each node from the root(s).
571 while ( !emptyQueue() ) processNextEdge();
574 ///Executes the algorithm until \c dest is reached.
576 ///Executes the algorithm until \c dest is reached.
578 ///\pre init() must be called and at least one node should be added
579 ///with addSource() before using this function.
581 ///This method runs the %DFS algorithm from the root node(s)
584 ///%DFS path to \c dest. The algorithm computes
585 ///- The %DFS path to \c dest.
586 ///- The distance of \c dest from the root(s).
588 void start(Node dest)
590 while ( !emptyQueue() && _queue[_queue_tail]!=dest ) processNextEdge();
593 ///Executes the algorithm until a condition is met.
595 ///Executes the algorithm until a condition is met.
597 ///\pre init() must be called and at least one node should be added
598 ///with addSource() before using this function.
600 ///\param nm must be a bool (or convertible) node map. The algorithm
601 ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
603 void start(const NM &nm)
605 while ( !emptyQueue() && !nm[_queue[_queue_tail]] ) processNextEdge();
608 ///Runs %DFS algorithm from node \c s.
610 ///This method runs the %DFS algorithm from a root node \c s
613 ///%DFS path to each node. The algorithm computes
615 ///- The distance of each node from the root.
617 ///\note d.run(s) is just a shortcut of the following code.
629 ///Finds the %DFS path between \c s and \c t.
631 ///Finds the %DFS path between \c s and \c t.
633 ///\return The length of the %DFS s---t path if there exists one,
635 ///\note Apart from the return value, d.run(s) is
636 ///just a shortcut of the following code.
642 int run(Node s,Node t) {
646 return reached(t)?_curr_dist-1+(_queue_tail==_queue_next_dist):0;
651 ///\name Query Functions
652 ///The result of the %DFS algorithm can be obtained using these
654 ///Before the use of these functions,
655 ///either run() or start() must be called.
659 ///The distance of a node from the root(s).
661 ///Returns the distance of a node from the root(s).
662 ///\pre \ref run() must be called before using this function.
663 ///\warning If node \c v in unreachable from the root(s) the return value
664 ///of this funcion is undefined.
665 int dist(Node v) const { return (*_dist)[v]; }
667 ///Returns the 'previous edge' of the %DFS tree.
669 ///For a node \c v it returns the 'previous edge'
671 ///i.e. it returns the last edge of a %DFS path from the root(s) to \c
672 ///v. It is \ref INVALID
673 ///if \c v is unreachable from the root(s) or \c v is a root. The
674 ///%DFS tree used here is equal to the %DFS tree used in
675 ///\ref predNode(Node v).
676 ///\pre Either \ref run() or \ref start() must be called before using
678 ///\todo predEdge could be a better name.
679 Edge pred(Node v) const { return (*_pred)[v];}
681 ///Returns the 'previous node' of the %DFS tree.
683 ///For a node \c v it returns the 'previous node'
685 ///i.e. it returns the last but one node from a %DFS path from the
687 ///It is INVALID if \c v is unreachable from the root(s) or
688 ///if \c v itself a root.
689 ///The %DFS tree used here is equal to the %DFS
690 ///tree used in \ref pred(Node v).
691 ///\pre Either \ref run() or \ref start() must be called before
692 ///using this function.
693 Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
694 G->source((*_pred)[v]); }
696 ///Returns a reference to the NodeMap of distances.
698 ///Returns a reference to the NodeMap of distances.
699 ///\pre Either \ref run() or \ref init() must
700 ///be called before using this function.
701 const DistMap &distMap() const { return *_dist;}
703 ///Returns a reference to the %DFS edge-tree map.
705 ///Returns a reference to the NodeMap of the edges of the
707 ///\pre Either \ref run() or \ref init()
708 ///must be called before using this function.
709 const PredMap &predMap() const { return *_pred;}
711 // ///Returns a reference to the map of nodes of %DFS paths.
713 // ///Returns a reference to the NodeMap of the last but one nodes of the
715 // ///\pre \ref run() must be called before using this function.
716 // const PredNodeMap &predNodeMap() const { return *_predNode;}
718 ///Checks if a node is reachable from the root.
720 ///Returns \c true if \c v is reachable from the root.
721 ///\warning The source nodes are inditated as unreached.
722 ///\pre Either \ref run() or \ref start()
723 ///must be called before using this function.
725 bool reached(Node v) { return (*_reached)[v]; }
730 ///Default traits class of Dfs function.
732 ///Default traits class of Dfs function.
733 ///\param GR Graph type.
735 struct DfsWizardDefaultTraits
737 ///The graph type the algorithm runs on.
739 ///\brief The type of the map that stores the last
740 ///edges of the %DFS paths.
742 ///The type of the map that stores the last
743 ///edges of the %DFS paths.
744 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
746 typedef NullMap<typename Graph::Node,typename GR::Edge> PredMap;
747 ///Instantiates a PredMap.
749 ///This function instantiates a \ref PredMap.
750 ///\param G is the graph, to which we would like to define the PredMap.
751 ///\todo The graph alone may be insufficient to initialize
752 static PredMap *createPredMap(const GR &G)
754 return new PredMap();
756 // ///\brief The type of the map that stores the last but one
757 // ///nodes of the %DFS paths.
759 // ///The type of the map that stores the last but one
760 // ///nodes of the %DFS paths.
761 // ///It must meet the \ref concept::WriteMap "WriteMap" concept.
763 // typedef NullMap<typename Graph::Node,typename Graph::Node> PredNodeMap;
764 // ///Instantiates a PredNodeMap.
766 // ///This function instantiates a \ref PredNodeMap.
767 // ///\param G is the graph, to which
768 // ///we would like to define the \ref PredNodeMap
769 // static PredNodeMap *createPredNodeMap(const GR &G)
771 // return new PredNodeMap();
774 ///The type of the map that indicates which nodes are processed.
776 ///The type of the map that indicates which nodes are processed.
777 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
778 ///\todo named parameter to set this type, function to read and write.
779 typedef NullMap<typename Graph::Node,bool> ProcessedMap;
780 ///Instantiates a ProcessedMap.
782 ///This function instantiates a \ref ProcessedMap.
783 ///\param G is the graph, to which
784 ///we would like to define the \ref ProcessedMap
785 static ProcessedMap *createProcessedMap(const GR &G)
787 return new ProcessedMap();
789 ///The type of the map that indicates which nodes are reached.
791 ///The type of the map that indicates which nodes are reached.
792 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
793 ///\todo named parameter to set this type, function to read and write.
794 typedef typename Graph::template NodeMap<bool> ReachedMap;
795 ///Instantiates a ReachedMap.
797 ///This function instantiates a \ref ReachedMap.
798 ///\param G is the graph, to which
799 ///we would like to define the \ref ReachedMap.
800 static ReachedMap *createReachedMap(const GR &G)
802 return new ReachedMap(G);
804 ///The type of the map that stores the dists of the nodes.
806 ///The type of the map that stores the dists of the nodes.
807 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
809 typedef NullMap<typename Graph::Node,int> DistMap;
810 ///Instantiates a DistMap.
812 ///This function instantiates a \ref DistMap.
813 ///\param G is the graph, to which we would like to define the \ref DistMap
814 static DistMap *createDistMap(const GR &G)
816 return new DistMap();
820 /// Default traits used by \ref DfsWizard
822 /// To make it easier to use Dfs algorithm
823 ///we have created a wizard class.
824 /// This \ref DfsWizard class needs default traits,
825 ///as well as the \ref Dfs class.
826 /// The \ref DfsWizardBase is a class to be the default traits of the
827 /// \ref DfsWizard class.
829 class DfsWizardBase : public DfsWizardDefaultTraits<GR>
832 typedef DfsWizardDefaultTraits<GR> Base;
834 /// Type of the nodes in the graph.
835 typedef typename Base::Graph::Node Node;
837 /// Pointer to the underlying graph.
839 ///Pointer to the map of reached nodes.
841 ///Pointer to the map of processed nodes.
843 ///Pointer to the map of predecessors edges.
845 // ///Pointer to the map of predecessors nodes.
847 ///Pointer to the map of distances.
849 ///Pointer to the source node.
855 /// This constructor does not require parameters, therefore it initiates
856 /// all of the attributes to default values (0, INVALID).
857 DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
859 _dist(0), _source(INVALID) {}
863 /// This constructor requires some parameters,
864 /// listed in the parameters list.
865 /// Others are initiated to 0.
866 /// \param g is the initial value of \ref _g
867 /// \param s is the initial value of \ref _source
868 DfsWizardBase(const GR &g, Node s=INVALID) :
869 _g((void *)&g), _reached(0), _processed(0), _pred(0),
871 _dist(0), _source(s) {}
875 /// A class to make the usage of Dfs algorithm easier
877 /// This class is created to make it easier to use Dfs algorithm.
878 /// It uses the functions and features of the plain \ref Dfs,
879 /// but it is much simpler to use it.
881 /// Simplicity means that the way to change the types defined
882 /// in the traits class is based on functions that returns the new class
883 /// and not on templatable built-in classes.
884 /// When using the plain \ref Dfs
885 /// the new class with the modified type comes from
886 /// the original class by using the ::
887 /// operator. In the case of \ref DfsWizard only
888 /// a function have to be called and it will
889 /// return the needed class.
891 /// It does not have own \ref run method. When its \ref run method is called
892 /// it initiates a plain \ref Dfs class, and calls the \ref Dfs::run
895 class DfsWizard : public TR
899 ///The type of the underlying graph.
900 typedef typename TR::Graph Graph;
902 typedef typename Graph::Node Node;
904 typedef typename Graph::NodeIt NodeIt;
906 typedef typename Graph::Edge Edge;
908 typedef typename Graph::OutEdgeIt OutEdgeIt;
910 ///\brief The type of the map that stores
912 typedef typename TR::ReachedMap ReachedMap;
913 ///\brief The type of the map that stores
914 ///the processed nodes
915 typedef typename TR::ProcessedMap ProcessedMap;
916 ///\brief The type of the map that stores the last
917 ///edges of the %DFS paths.
918 typedef typename TR::PredMap PredMap;
919 // ///\brief The type of the map that stores the last but one
920 // ///nodes of the %DFS paths.
921 // typedef typename TR::PredNodeMap PredNodeMap;
922 ///The type of the map that stores the dists of the nodes.
923 typedef typename TR::DistMap DistMap;
927 DfsWizard() : TR() {}
929 /// Constructor that requires parameters.
931 /// Constructor that requires parameters.
932 /// These parameters will be the default values for the traits class.
933 DfsWizard(const Graph &g, Node s=INVALID) :
937 DfsWizard(const TR &b) : TR(b) {}
941 ///Runs Dfs algorithm from a given node.
943 ///Runs Dfs algorithm from a given node.
944 ///The node can be given by the \ref source function.
947 if(Base::_source==INVALID) throw UninitializedParameter();
948 Dfs<Graph,TR> alg(*(Graph*)Base::_g);
949 if(Base::_reached) alg.reachedMap(*(ReachedMap*)Base::_reached);
950 if(Base::_processed) alg.processedMap(*(ProcessedMap*)Base::_processed);
951 if(Base::_pred) alg.predMap(*(PredMap*)Base::_pred);
952 // if(Base::_predNode) alg.predNodeMap(*(PredNodeMap*)Base::_predNode);
953 if(Base::_dist) alg.distMap(*(DistMap*)Base::_dist);
954 alg.run(Base::_source);
957 ///Runs Dfs algorithm from the given node.
959 ///Runs Dfs algorithm from the given node.
960 ///\param s is the given source.
968 struct DefPredMapBase : public Base {
970 static PredMap *createPredMap(const Graph &G) { return 0; };
971 DefPredMapBase(const Base &b) : Base(b) {}
974 ///\brief \ref named-templ-param "Named parameter"
975 ///function for setting PredMap type
977 /// \ref named-templ-param "Named parameter"
978 ///function for setting PredMap type
981 DfsWizard<DefPredMapBase<T> > predMap(const T &t)
983 Base::_pred=(void *)&t;
984 return DfsWizard<DefPredMapBase<T> >(*this);
989 struct DefReachedMapBase : public Base {
990 typedef T ReachedMap;
991 static ReachedMap *createReachedMap(const Graph &G) { return 0; };
992 DefReachedMapBase(const Base &b) : Base(b) {}
995 ///\brief \ref named-templ-param "Named parameter"
996 ///function for setting ReachedMap
998 /// \ref named-templ-param "Named parameter"
999 ///function for setting ReachedMap
1002 DfsWizard<DefReachedMapBase<T> > reachedMap(const T &t)
1004 Base::_pred=(void *)&t;
1005 return DfsWizard<DefReachedMapBase<T> >(*this);
1010 struct DefProcessedMapBase : public Base {
1011 typedef T ProcessedMap;
1012 static ProcessedMap *createProcessedMap(const Graph &G) { return 0; };
1013 DefProcessedMapBase(const Base &b) : Base(b) {}
1016 ///\brief \ref named-templ-param "Named parameter"
1017 ///function for setting ProcessedMap
1019 /// \ref named-templ-param "Named parameter"
1020 ///function for setting ProcessedMap
1023 DfsWizard<DefProcessedMapBase<T> > processedMap(const T &t)
1025 Base::_pred=(void *)&t;
1026 return DfsWizard<DefProcessedMapBase<T> >(*this);
1030 // template<class T>
1031 // struct DefPredNodeMapBase : public Base {
1032 // typedef T PredNodeMap;
1033 // static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; };
1034 // DefPredNodeMapBase(const Base &b) : Base(b) {}
1037 // ///\brief \ref named-templ-param "Named parameter"
1038 // ///function for setting PredNodeMap type
1040 // /// \ref named-templ-param "Named parameter"
1041 // ///function for setting PredNodeMap type
1043 // template<class T>
1044 // DfsWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t)
1046 // Base::_predNode=(void *)&t;
1047 // return DfsWizard<DefPredNodeMapBase<T> >(*this);
1051 struct DefDistMapBase : public Base {
1053 static DistMap *createDistMap(const Graph &G) { return 0; };
1054 DefDistMapBase(const Base &b) : Base(b) {}
1057 ///\brief \ref named-templ-param "Named parameter"
1058 ///function for setting DistMap type
1060 /// \ref named-templ-param "Named parameter"
1061 ///function for setting DistMap type
1064 DfsWizard<DefDistMapBase<T> > distMap(const T &t)
1066 Base::_dist=(void *)&t;
1067 return DfsWizard<DefDistMapBase<T> >(*this);
1070 /// Sets the source node, from which the Dfs algorithm runs.
1072 /// Sets the source node, from which the Dfs algorithm runs.
1073 /// \param s is the source node.
1074 DfsWizard<TR> &source(Node s)
1082 ///Function type interface for Dfs algorithm.
1084 /// \ingroup flowalgs
1085 ///Function type interface for Dfs algorithm.
1087 ///This function also has several
1088 ///\ref named-templ-func-param "named parameters",
1089 ///they are declared as the members of class \ref DfsWizard.
1091 ///example shows how to use these parameters.
1093 /// dfs(g,source).predMap(preds).run();
1095 ///\warning Don't forget to put the \ref DfsWizard::run() "run()"
1096 ///to the end of the parameter list.
1100 DfsWizard<DfsWizardBase<GR> >
1101 dfs(const GR &g,typename GR::Node s=INVALID)
1103 return DfsWizard<DfsWizardBase<GR> >(g,s);
1106 } //END OF NAMESPACE LEMON