2 * lemon/dfs.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, 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);
61 ///The type of the map that indicates which nodes are processed.
63 ///The type of the map that indicates which nodes are processed.
64 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
65 ///\todo named parameter to set this type, function to read and write.
66 typedef NullMap<typename Graph::Node,bool> ProcessedMap;
67 ///Instantiates a ProcessedMap.
69 ///This function instantiates a \ref ProcessedMap.
70 ///\param g is the graph, to which
71 ///we would like to define the \ref ProcessedMap
73 static ProcessedMap *createProcessedMap(const GR &g)
75 static ProcessedMap *createProcessedMap(const GR &)
78 return new ProcessedMap();
80 ///The type of the map that indicates which nodes are reached.
82 ///The type of the map that indicates which nodes are reached.
83 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
84 ///\todo named parameter to set this type, function to read and write.
85 typedef typename Graph::template NodeMap<bool> ReachedMap;
86 ///Instantiates a ReachedMap.
88 ///This function instantiates a \ref ReachedMap.
89 ///\param G is the graph, to which
90 ///we would like to define the \ref ReachedMap.
91 static ReachedMap *createReachedMap(const GR &G)
93 return new ReachedMap(G);
95 ///The type of the map that stores the dists of the nodes.
97 ///The type of the map that stores the dists of the nodes.
98 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
100 typedef typename Graph::template NodeMap<int> DistMap;
101 ///Instantiates a DistMap.
103 ///This function instantiates a \ref DistMap.
104 ///\param G is the graph, to which we would like to define the \ref DistMap
105 static DistMap *createDistMap(const GR &G)
107 return new DistMap(G);
111 ///%DFS algorithm class.
114 ///This class provides an efficient implementation of the %DFS algorithm.
116 ///\param GR The graph type the algorithm runs on. The default value is
117 ///\ref ListGraph. The value of GR is not used directly by Dfs, it
118 ///is only passed to \ref DfsDefaultTraits.
119 ///\param TR Traits class to set various data types used by the algorithm.
120 ///The default traits class is
121 ///\ref DfsDefaultTraits "DfsDefaultTraits<GR>".
122 ///See \ref DfsDefaultTraits for the documentation of
123 ///a Dfs traits class.
125 ///\author Jacint Szabo and Alpar Juttner
126 ///\todo A compare object would be nice.
129 template <typename GR,
132 template <typename GR=ListGraph,
133 typename TR=DfsDefaultTraits<GR> >
138 * \brief \ref Exception for uninitialized parameters.
140 * This error represents problems in the initialization
141 * of the parameters of the algorithms.
143 class UninitializedParameter : public lemon::UninitializedParameter {
145 virtual const char* exceptionName() const {
146 return "lemon::Dfs::UninitializedParameter";
151 ///The type of the underlying graph.
152 typedef typename TR::Graph Graph;
154 typedef typename Graph::Node Node;
156 typedef typename Graph::NodeIt NodeIt;
158 typedef typename Graph::Edge Edge;
160 typedef typename Graph::OutEdgeIt OutEdgeIt;
162 ///\brief The type of the map that stores the last
163 ///edges of the %DFS paths.
164 typedef typename TR::PredMap PredMap;
165 ///The type of the map indicating which nodes are reached.
166 typedef typename TR::ReachedMap ReachedMap;
167 ///The type of the map indicating which nodes are processed.
168 typedef typename TR::ProcessedMap ProcessedMap;
169 ///The type of the map that stores the dists of the nodes.
170 typedef typename TR::DistMap DistMap;
172 /// Pointer to the underlying graph.
174 ///Pointer to the map of predecessors edges.
176 ///Indicates if \ref _pred is locally allocated (\c true) or not.
178 ///Pointer to the map of distances.
180 ///Indicates if \ref _dist is locally allocated (\c true) or not.
182 ///Pointer to the map of reached status of the nodes.
183 ReachedMap *_reached;
184 ///Indicates if \ref _reached is locally allocated (\c true) or not.
186 ///Pointer to the map of processed status of the nodes.
187 ProcessedMap *_processed;
188 ///Indicates if \ref _processed is locally allocated (\c true) or not.
189 bool local_processed;
191 std::vector<typename Graph::OutEdgeIt> _stack;
194 ///Creates the maps if necessary.
196 ///\todo Error if \c G are \c NULL.
197 ///\todo Better memory allocation (instead of new).
202 _pred = Traits::createPredMap(*G);
206 _dist = Traits::createDistMap(*G);
209 local_reached = true;
210 _reached = Traits::createReachedMap(*G);
213 local_processed = true;
214 _processed = Traits::createProcessedMap(*G);
226 ///\name Named template parameters
231 struct DefPredMapTraits : public Traits {
233 static PredMap *createPredMap(const Graph &G)
235 throw UninitializedParameter();
238 ///\ref named-templ-param "Named parameter" for setting PredMap type
240 ///\ref named-templ-param "Named parameter" for setting PredMap type
243 struct DefPredMap : public Dfs<Graph, DefPredMapTraits<T> > {
244 typedef Dfs<Graph, DefPredMapTraits<T> > Create;
249 struct DefDistMapTraits : public Traits {
251 static DistMap *createDistMap(const Graph &G)
253 throw UninitializedParameter();
256 ///\ref named-templ-param "Named parameter" for setting DistMap type
258 ///\ref named-templ-param "Named parameter" for setting DistMap type
262 typedef Dfs<Graph, DefDistMapTraits<T> > Create;
266 struct DefReachedMapTraits : public Traits {
267 typedef T ReachedMap;
268 static ReachedMap *createReachedMap(const Graph &G)
270 throw UninitializedParameter();
273 ///\ref named-templ-param "Named parameter" for setting ReachedMap type
275 ///\ref named-templ-param "Named parameter" for setting ReachedMap type
278 struct DefReachedMap {
279 typedef Dfs< Graph, DefReachedMapTraits<T> > Create;
283 struct DefProcessedMapTraits : public Traits {
284 typedef T ProcessedMap;
285 static ProcessedMap *createProcessedMap(const Graph &G)
287 throw UninitializedParameter();
290 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
292 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
295 struct DefProcessedMap : public Dfs< Graph, DefProcessedMapTraits<T> > {
296 typedef Dfs< Graph, DefProcessedMapTraits<T> > Create;
299 struct DefGraphProcessedMapTraits : public Traits {
300 typedef typename Graph::template NodeMap<bool> ProcessedMap;
301 static ProcessedMap *createProcessedMap(const Graph &G)
303 return new ProcessedMap(G);
306 ///\brief \ref named-templ-param "Named parameter"
307 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
309 ///\ref named-templ-param "Named parameter"
310 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
311 ///If you don't set it explicitely, it will be automatically allocated.
313 class DefProcessedMapToBeDefaultMap :
314 public Dfs< Graph, DefGraphProcessedMapTraits> {
315 typedef Dfs< Graph, DefGraphProcessedMapTraits> Create;
324 ///\param _G the graph the algorithm will run on.
326 Dfs(const Graph& _G) :
328 _pred(NULL), local_pred(false),
329 // _predNode(NULL), local_predNode(false),
330 _dist(NULL), local_dist(false),
331 _reached(NULL), local_reached(false),
332 _processed(NULL), local_processed(false)
338 if(local_pred) delete _pred;
339 // if(local_predNode) delete _predNode;
340 if(local_dist) delete _dist;
341 if(local_reached) delete _reached;
342 if(local_processed) delete _processed;
345 ///Sets the map storing the predecessor edges.
347 ///Sets the map storing the predecessor edges.
348 ///If you don't use this function before calling \ref run(),
349 ///it will allocate one. The destuctor deallocates this
350 ///automatically allocated map, of course.
351 ///\return <tt> (*this) </tt>
352 Dfs &predMap(PredMap &m)
362 // ///Sets the map storing the predecessor nodes.
364 // ///Sets the map storing the predecessor nodes.
365 // ///If you don't use this function before calling \ref run(),
366 // ///it will allocate one. The destuctor deallocates this
367 // ///automatically allocated map, of course.
368 // ///\return <tt> (*this) </tt>
369 // Dfs &predNodeMap(PredNodeMap &m)
371 // if(local_predNode) {
373 // local_predNode=false;
379 ///Sets the map storing the distances calculated by the algorithm.
381 ///Sets the map storing the distances calculated by the algorithm.
382 ///If you don't use this function before calling \ref run(),
383 ///it will allocate one. The destuctor deallocates this
384 ///automatically allocated map, of course.
385 ///\return <tt> (*this) </tt>
386 Dfs &distMap(DistMap &m)
396 ///Sets the map indicating if a node is reached.
398 ///Sets the map indicating if a node is reached.
399 ///If you don't use this function before calling \ref run(),
400 ///it will allocate one. The destuctor deallocates this
401 ///automatically allocated map, of course.
402 ///\return <tt> (*this) </tt>
403 Dfs &reachedMap(ReachedMap &m)
413 ///Sets the map indicating if a node is processed.
415 ///Sets the map indicating if a node is processed.
416 ///If you don't use this function before calling \ref run(),
417 ///it will allocate one. The destuctor deallocates this
418 ///automatically allocated map, of course.
419 ///\return <tt> (*this) </tt>
420 Dfs &processedMap(ProcessedMap &m)
422 if(local_processed) {
424 local_processed=false;
431 ///\name Execution control
432 ///The simplest way to execute the algorithm is to use
433 ///one of the member functions called \c run(...).
435 ///If you need more control on the execution,
436 ///first you must call \ref init(), then you can add several source nodes
437 ///with \ref addSource().
438 ///Finally \ref start() will perform the actual path
443 ///Initializes the internal data structures.
445 ///Initializes the internal data structures.
450 _stack.resize(countNodes(*G));
452 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
453 _pred->set(u,INVALID);
454 // _predNode->set(u,INVALID);
455 _reached->set(u,false);
456 _processed->set(u,false);
460 ///Adds a new source node.
462 ///Adds a new source node to the set of nodes to be processed.
464 ///\bug dists are wrong (or at least strange) in case of multiple sources.
465 void addSource(Node s)
469 _reached->set(s,true);
470 _pred->set(s,INVALID);
471 // _predNode->set(u,INVALID);
474 _stack[++_stack_head]=e;
475 _dist->set(s,_stack_head);
478 _processed->set(s,true);
484 ///Processes the next edge.
486 ///Processes the next edge.
488 ///\return The processed edge.
490 ///\pre The stack must not be empty!
491 Edge processNextEdge()
494 Edge e=_stack[_stack_head];
495 if(!(*_reached)[m=G->target(e)]) {
497 _reached->set(m,true);
498 // _pred_node->set(m,G->source(e));
500 _stack[_stack_head] = OutEdgeIt(*G, m);
501 _dist->set(m,_stack_head);
505 ++_stack[_stack_head];
507 //'m' is now the (original) source of the _stack[_stack_head]
508 while(_stack_head>=0 && _stack[_stack_head]==INVALID) {
509 _processed->set(m,true);
512 m=G->source(_stack[_stack_head]);
513 ++_stack[_stack_head];
518 ///Next edge to be processed.
520 ///Next edge to be processed.
522 ///\return The next edge to be processed or INVALID if the stack is
526 return _stack_head>=0?_stack[_stack_head]:INVALID;
529 ///\brief Returns \c false if there are nodes
530 ///to be processed in the queue
532 ///Returns \c false if there are nodes
533 ///to be processed in the queue
535 ///\todo This should be called emptyStack() or some "neutral" name.
536 bool emptyQueue() { return _stack_head<0; }
537 ///Returns the number of the nodes to be processed.
539 ///Returns the number of the nodes to be processed in the queue.
541 ///\todo This should be called stackSize() or some "neutral" name.
542 int queueSize() { return _stack_head+1; }
544 ///Executes the algorithm.
546 ///Executes the algorithm.
548 ///\pre init() must be called and at least one node should be added
549 ///with addSource() before using this function.
551 ///This method runs the %DFS algorithm from the root node(s)
554 ///%DFS path to each node. The algorithm computes
556 ///- The distance of each node from the root(s) in the %DFS tree.
560 while ( !emptyQueue() ) processNextEdge();
563 ///Executes the algorithm until \c dest is reached.
565 ///Executes the algorithm until \c dest is reached.
567 ///\pre init() must be called and at least one node should be added
568 ///with addSource() before using this function.
570 ///This method runs the %DFS algorithm from the root node(s)
573 ///%DFS path to \c dest. The algorithm computes
574 ///- The %DFS path to \c dest.
575 ///- The distance of \c dest from the root(s) in the %DFS tree.
577 void start(Node dest)
579 while ( !emptyQueue() && G->target(_stack[_stack_head])!=dest )
583 ///Executes the algorithm until a condition is met.
585 ///Executes the algorithm until a condition is met.
587 ///\pre init() must be called and at least one node should be added
588 ///with addSource() before using this function.
590 ///\param nm must be a bool (or convertible) edge map. The algorithm
591 ///will stop when it reaches an edge \c e with <tt>nm[e]==true</tt>.
593 ///\warning Contrary to \ref Dfs and \ref Dijkstra, \c nm is an edge map,
596 void start(const NM &nm)
598 while ( !emptyQueue() && !nm[_stack[_stack_head]] ) processNextEdge();
601 ///Runs %DFS algorithm from node \c s.
603 ///This method runs the %DFS algorithm from a root node \c s
606 ///%DFS path to each node. The algorithm computes
608 ///- The distance of each node from the root in the %DFS tree.
610 ///\note d.run(s) is just a shortcut of the following code.
622 ///Finds the %DFS path between \c s and \c t.
624 ///Finds the %DFS path between \c s and \c t.
626 ///\return The length of the %DFS s---t path if there exists one,
628 ///\note Apart from the return value, d.run(s,t) is
629 ///just a shortcut of the following code.
635 int run(Node s,Node t) {
639 return reached(t)?_stack_head+1:0;
644 ///\name Query Functions
645 ///The result of the %DFS algorithm can be obtained using these
647 ///Before the use of these functions,
648 ///either run() or start() must be called.
652 ///Copies the path to \c t on the DFS tree into \c p
654 ///This function copies the path to \c t on the DFS tree into \c p.
655 ///If \c t is a source itself or unreachable, then it does not
657 ///\todo Is this the right way to handle unreachable nodes?
659 ///\return Returns \c true if a path to \c t was actually copied to \c p,
660 ///\c false otherwise.
663 bool getPath(P &p,Node t)
667 typename P::Builder b(p);
668 for(b.setStartNode(t);pred(t)!=INVALID;t=predNode(t))
669 b.pushFront(pred(t));
676 ///The distance of a node from the root(s).
678 ///Returns the distance of a node from the root(s).
679 ///\pre \ref run() must be called before using this function.
680 ///\warning If node \c v is unreachable from the root(s) then the return value
681 ///of this funcion is undefined.
682 int dist(Node v) const { return (*_dist)[v]; }
684 ///Returns the 'previous edge' of the %DFS tree.
686 ///For a node \c v it returns the 'previous edge'
688 ///i.e. it returns the last edge of a %DFS path from the root(s) to \c
689 ///v. It is \ref INVALID
690 ///if \c v is unreachable from the root(s) or \c v is a root. The
691 ///%DFS tree used here is equal to the %DFS tree used in
693 ///\pre Either \ref run() or \ref start() must be called before using
695 ///\todo predEdge could be a better name.
696 Edge pred(Node v) const { return (*_pred)[v];}
698 ///Returns the 'previous node' of the %DFS tree.
700 ///For a node \c v it returns the 'previous node'
702 ///i.e. it returns the last but one node from a %DFS path from the
704 ///It is INVALID if \c v is unreachable from the root(s) or
705 ///if \c v itself a root.
706 ///The %DFS tree used here is equal to the %DFS
707 ///tree used in \ref pred().
708 ///\pre Either \ref run() or \ref start() must be called before
709 ///using this function.
710 Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
711 G->source((*_pred)[v]); }
713 ///Returns a reference to the NodeMap of distances.
715 ///Returns a reference to the NodeMap of distances.
716 ///\pre Either \ref run() or \ref init() must
717 ///be called before using this function.
718 const DistMap &distMap() const { return *_dist;}
720 ///Returns a reference to the %DFS edge-tree map.
722 ///Returns a reference to the NodeMap of the edges of the
724 ///\pre Either \ref run() or \ref init()
725 ///must be called before using this function.
726 const PredMap &predMap() const { return *_pred;}
728 // ///Returns a reference to the map of nodes of %DFS paths.
730 // ///Returns a reference to the NodeMap of the last but one nodes of the
732 // ///\pre \ref run() must be called before using this function.
733 // const PredNodeMap &predNodeMap() const { return *_predNode;}
735 ///Checks if a node is reachable from the root.
737 ///Returns \c true if \c v is reachable from the root(s).
738 ///\warning The source nodes are inditated as unreachable.
739 ///\pre Either \ref run() or \ref start()
740 ///must be called before using this function.
742 bool reached(Node v) { return (*_reached)[v]; }
747 ///Default traits class of Dfs function.
749 ///Default traits class of Dfs function.
750 ///\param GR Graph type.
752 struct DfsWizardDefaultTraits
754 ///The graph type the algorithm runs on.
756 ///\brief The type of the map that stores the last
757 ///edges of the %DFS paths.
759 ///The type of the map that stores the last
760 ///edges of the %DFS paths.
761 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
763 typedef NullMap<typename Graph::Node,typename GR::Edge> PredMap;
764 ///Instantiates a PredMap.
766 ///This function instantiates a \ref PredMap.
767 ///\param g is the graph, to which we would like to define the PredMap.
768 ///\todo The graph alone may be insufficient to initialize
770 static PredMap *createPredMap(const GR &g)
772 static PredMap *createPredMap(const GR &)
775 return new PredMap();
777 // ///\brief The type of the map that stores the last but one
778 // ///nodes of the %DFS paths.
780 // ///The type of the map that stores the last but one
781 // ///nodes of the %DFS paths.
782 // ///It must meet the \ref concept::WriteMap "WriteMap" concept.
784 // typedef NullMap<typename Graph::Node,typename Graph::Node> PredNodeMap;
785 // ///Instantiates a PredNodeMap.
787 // ///This function instantiates a \ref PredNodeMap.
788 // ///\param G is the graph, to which
789 // ///we would like to define the \ref PredNodeMap
790 // static PredNodeMap *createPredNodeMap(const GR &G)
792 // return new PredNodeMap();
795 ///The type of the map that indicates which nodes are processed.
797 ///The type of the map that indicates which nodes are processed.
798 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
799 ///\todo named parameter to set this type, function to read and write.
800 typedef NullMap<typename Graph::Node,bool> ProcessedMap;
801 ///Instantiates a ProcessedMap.
803 ///This function instantiates a \ref ProcessedMap.
804 ///\param g is the graph, to which
805 ///we would like to define the \ref ProcessedMap
807 static ProcessedMap *createProcessedMap(const GR &g)
809 static ProcessedMap *createProcessedMap(const GR &)
812 return new ProcessedMap();
814 ///The type of the map that indicates which nodes are reached.
816 ///The type of the map that indicates which nodes are reached.
817 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
818 ///\todo named parameter to set this type, function to read and write.
819 typedef typename Graph::template NodeMap<bool> ReachedMap;
820 ///Instantiates a ReachedMap.
822 ///This function instantiates a \ref ReachedMap.
823 ///\param G is the graph, to which
824 ///we would like to define the \ref ReachedMap.
825 static ReachedMap *createReachedMap(const GR &G)
827 return new ReachedMap(G);
829 ///The type of the map that stores the dists of the nodes.
831 ///The type of the map that stores the dists of the nodes.
832 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
834 typedef NullMap<typename Graph::Node,int> DistMap;
835 ///Instantiates a DistMap.
837 ///This function instantiates a \ref DistMap.
838 ///\param g is the graph, to which we would like to define the \ref DistMap
840 static DistMap *createDistMap(const GR &g)
842 static DistMap *createDistMap(const GR &)
845 return new DistMap();
849 /// Default traits used by \ref DfsWizard
851 /// To make it easier to use Dfs algorithm
852 ///we have created a wizard class.
853 /// This \ref DfsWizard class needs default traits,
854 ///as well as the \ref Dfs class.
855 /// The \ref DfsWizardBase is a class to be the default traits of the
856 /// \ref DfsWizard class.
858 class DfsWizardBase : public DfsWizardDefaultTraits<GR>
861 typedef DfsWizardDefaultTraits<GR> Base;
863 /// Type of the nodes in the graph.
864 typedef typename Base::Graph::Node Node;
866 /// Pointer to the underlying graph.
868 ///Pointer to the map of reached nodes.
870 ///Pointer to the map of processed nodes.
872 ///Pointer to the map of predecessors edges.
874 // ///Pointer to the map of predecessors nodes.
876 ///Pointer to the map of distances.
878 ///Pointer to the source node.
884 /// This constructor does not require parameters, therefore it initiates
885 /// all of the attributes to default values (0, INVALID).
886 DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
888 _dist(0), _source(INVALID) {}
892 /// This constructor requires some parameters,
893 /// listed in the parameters list.
894 /// Others are initiated to 0.
895 /// \param g is the initial value of \ref _g
896 /// \param s is the initial value of \ref _source
897 DfsWizardBase(const GR &g, Node s=INVALID) :
898 _g((void *)&g), _reached(0), _processed(0), _pred(0),
900 _dist(0), _source(s) {}
904 /// A class to make the usage of the Dfs algorithm easier
906 /// This class is created to make it easier to use the Dfs algorithm.
907 /// It uses the functions and features of the plain \ref Dfs,
908 /// but it is much simpler to use it.
910 /// Simplicity means that the way to change the types defined
911 /// in the traits class is based on functions that returns the new class
912 /// and not on templatable built-in classes.
913 /// When using the plain \ref Dfs
914 /// the new class with the modified type comes from
915 /// the original class by using the ::
916 /// operator. In the case of \ref DfsWizard only
917 /// a function have to be called and it will
918 /// return the needed class.
920 /// It does not have own \ref run method. When its \ref run method is called
921 /// it initiates a plain \ref Dfs object, and calls the \ref Dfs::run
924 class DfsWizard : public TR
928 ///The type of the underlying graph.
929 typedef typename TR::Graph Graph;
931 typedef typename Graph::Node Node;
933 typedef typename Graph::NodeIt NodeIt;
935 typedef typename Graph::Edge Edge;
937 typedef typename Graph::OutEdgeIt OutEdgeIt;
939 ///\brief The type of the map that stores
941 typedef typename TR::ReachedMap ReachedMap;
942 ///\brief The type of the map that stores
943 ///the processed nodes
944 typedef typename TR::ProcessedMap ProcessedMap;
945 ///\brief The type of the map that stores the last
946 ///edges of the %DFS paths.
947 typedef typename TR::PredMap PredMap;
948 // ///\brief The type of the map that stores the last but one
949 // ///nodes of the %DFS paths.
950 // typedef typename TR::PredNodeMap PredNodeMap;
951 ///The type of the map that stores the distances of the nodes.
952 typedef typename TR::DistMap DistMap;
956 DfsWizard() : TR() {}
958 /// Constructor that requires parameters.
960 /// Constructor that requires parameters.
961 /// These parameters will be the default values for the traits class.
962 DfsWizard(const Graph &g, Node s=INVALID) :
966 DfsWizard(const TR &b) : TR(b) {}
970 ///Runs Dfs algorithm from a given node.
972 ///Runs Dfs algorithm from a given node.
973 ///The node can be given by the \ref source function.
976 if(Base::_source==INVALID) throw UninitializedParameter();
977 Dfs<Graph,TR> alg(*(Graph*)Base::_g);
978 if(Base::_reached) alg.reachedMap(*(ReachedMap*)Base::_reached);
979 if(Base::_processed) alg.processedMap(*(ProcessedMap*)Base::_processed);
980 if(Base::_pred) alg.predMap(*(PredMap*)Base::_pred);
981 // if(Base::_predNode) alg.predNodeMap(*(PredNodeMap*)Base::_predNode);
982 if(Base::_dist) alg.distMap(*(DistMap*)Base::_dist);
983 alg.run(Base::_source);
986 ///Runs Dfs algorithm from the given node.
988 ///Runs Dfs algorithm from the given node.
989 ///\param s is the given source.
997 struct DefPredMapBase : public Base {
999 static PredMap *createPredMap(const Graph &) { return 0; };
1000 DefPredMapBase(const TR &b) : TR(b) {}
1003 ///\brief \ref named-templ-param "Named parameter"
1004 ///function for setting PredMap type
1006 /// \ref named-templ-param "Named parameter"
1007 ///function for setting PredMap type
1010 DfsWizard<DefPredMapBase<T> > predMap(const T &t)
1012 Base::_pred=(void *)&t;
1013 return DfsWizard<DefPredMapBase<T> >(*this);
1018 struct DefReachedMapBase : public Base {
1019 typedef T ReachedMap;
1020 static ReachedMap *createReachedMap(const Graph &) { return 0; };
1021 DefReachedMapBase(const TR &b) : TR(b) {}
1024 ///\brief \ref named-templ-param "Named parameter"
1025 ///function for setting ReachedMap
1027 /// \ref named-templ-param "Named parameter"
1028 ///function for setting ReachedMap
1031 DfsWizard<DefReachedMapBase<T> > reachedMap(const T &t)
1033 Base::_pred=(void *)&t;
1034 return DfsWizard<DefReachedMapBase<T> >(*this);
1039 struct DefProcessedMapBase : public Base {
1040 typedef T ProcessedMap;
1041 static ProcessedMap *createProcessedMap(const Graph &) { return 0; };
1042 DefProcessedMapBase(const TR &b) : TR(b) {}
1045 ///\brief \ref named-templ-param "Named parameter"
1046 ///function for setting ProcessedMap
1048 /// \ref named-templ-param "Named parameter"
1049 ///function for setting ProcessedMap
1052 DfsWizard<DefProcessedMapBase<T> > processedMap(const T &t)
1054 Base::_pred=(void *)&t;
1055 return DfsWizard<DefProcessedMapBase<T> >(*this);
1059 // template<class T>
1060 // struct DefPredNodeMapBase : public Base {
1061 // typedef T PredNodeMap;
1062 // static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; };
1063 // DefPredNodeMapBase(const TR &b) : TR(b) {}
1066 // ///\brief \ref named-templ-param "Named parameter"
1067 // ///function for setting PredNodeMap type
1069 // /// \ref named-templ-param "Named parameter"
1070 // ///function for setting PredNodeMap type
1072 // template<class T>
1073 // DfsWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t)
1075 // Base::_predNode=(void *)&t;
1076 // return DfsWizard<DefPredNodeMapBase<T> >(*this);
1080 struct DefDistMapBase : public Base {
1082 static DistMap *createDistMap(const Graph &) { return 0; };
1083 DefDistMapBase(const TR &b) : TR(b) {}
1086 ///\brief \ref named-templ-param "Named parameter"
1087 ///function for setting DistMap type
1089 /// \ref named-templ-param "Named parameter"
1090 ///function for setting DistMap type
1093 DfsWizard<DefDistMapBase<T> > distMap(const T &t)
1095 Base::_dist=(void *)&t;
1096 return DfsWizard<DefDistMapBase<T> >(*this);
1099 /// Sets the source node, from which the Dfs algorithm runs.
1101 /// Sets the source node, from which the Dfs algorithm runs.
1102 /// \param s is the source node.
1103 DfsWizard<TR> &source(Node s)
1111 ///Function type interface for Dfs algorithm.
1113 /// \ingroup flowalgs
1114 ///Function type interface for Dfs algorithm.
1116 ///This function also has several
1117 ///\ref named-templ-func-param "named parameters",
1118 ///they are declared as the members of class \ref DfsWizard.
1120 ///example shows how to use these parameters.
1122 /// dfs(g,source).predMap(preds).run();
1124 ///\warning Don't forget to put the \ref DfsWizard::run() "run()"
1125 ///to the end of the parameter list.
1129 DfsWizard<DfsWizardBase<GR> >
1130 dfs(const GR &g,typename GR::Node s=INVALID)
1132 return DfsWizard<DfsWizardBase<GR> >(g,s);
1135 } //END OF NAMESPACE LEMON