1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2010
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
24 ///\brief DFS algorithm.
26 #include <lemon/list_graph.h>
27 #include <lemon/bits/path_dump.h>
28 #include <lemon/core.h>
29 #include <lemon/error.h>
30 #include <lemon/maps.h>
31 #include <lemon/path.h>
35 ///Default traits class of Dfs class.
37 ///Default traits class of Dfs class.
38 ///\tparam GR Digraph type.
40 struct DfsDefaultTraits
42 ///The type of the digraph the algorithm runs on.
45 ///\brief The type of the map that stores the predecessor
46 ///arcs of the %DFS paths.
48 ///The type of the map that stores the predecessor
49 ///arcs of the %DFS paths.
50 ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
51 typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
52 ///Instantiates a \c PredMap.
54 ///This function instantiates a \ref PredMap.
55 ///\param g is the digraph, to which we would like to define the
57 static PredMap *createPredMap(const Digraph &g)
59 return new PredMap(g);
62 ///The type of the map that indicates which nodes are processed.
64 ///The type of the map that indicates which nodes are processed.
65 ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
66 ///By default, it is a NullMap.
67 typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
68 ///Instantiates a \c ProcessedMap.
70 ///This function instantiates a \ref ProcessedMap.
71 ///\param g is the digraph, to which
72 ///we would like to define the \ref ProcessedMap.
74 static ProcessedMap *createProcessedMap(const Digraph &g)
76 static ProcessedMap *createProcessedMap(const Digraph &)
79 return new ProcessedMap();
82 ///The type of the map that indicates which nodes are reached.
84 ///The type of the map that indicates which nodes are reached.
86 ///the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
87 typedef typename Digraph::template NodeMap<bool> ReachedMap;
88 ///Instantiates a \c ReachedMap.
90 ///This function instantiates a \ref ReachedMap.
91 ///\param g is the digraph, to which
92 ///we would like to define the \ref ReachedMap.
93 static ReachedMap *createReachedMap(const Digraph &g)
95 return new ReachedMap(g);
98 ///The type of the map that stores the distances of the nodes.
100 ///The type of the map that stores the distances of the nodes.
101 ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
102 typedef typename Digraph::template NodeMap<int> DistMap;
103 ///Instantiates a \c DistMap.
105 ///This function instantiates a \ref DistMap.
106 ///\param g is the digraph, to which we would like to define the
108 static DistMap *createDistMap(const Digraph &g)
110 return new DistMap(g);
114 ///%DFS algorithm class.
117 ///This class provides an efficient implementation of the %DFS algorithm.
119 ///There is also a \ref dfs() "function-type interface" for the DFS
120 ///algorithm, which is convenient in the simplier cases and it can be
123 ///\tparam GR The type of the digraph the algorithm runs on.
124 ///The default type is \ref ListDigraph.
125 ///\tparam TR The traits class that defines various types used by the
126 ///algorithm. By default, it is \ref DfsDefaultTraits
127 ///"DfsDefaultTraits<GR>".
128 ///In most cases, this parameter should not be set directly,
129 ///consider to use the named template parameters instead.
131 template <typename GR,
134 template <typename GR=ListDigraph,
135 typename TR=DfsDefaultTraits<GR> >
140 ///The type of the digraph the algorithm runs on.
141 typedef typename TR::Digraph Digraph;
143 ///\brief The type of the map that stores the predecessor arcs of the
145 typedef typename TR::PredMap PredMap;
146 ///The type of the map that stores the distances of the nodes.
147 typedef typename TR::DistMap DistMap;
148 ///The type of the map that indicates which nodes are reached.
149 typedef typename TR::ReachedMap ReachedMap;
150 ///The type of the map that indicates which nodes are processed.
151 typedef typename TR::ProcessedMap ProcessedMap;
152 ///The type of the paths.
153 typedef PredMapPath<Digraph, PredMap> Path;
155 ///The \ref DfsDefaultTraits "traits class" of the algorithm.
160 typedef typename Digraph::Node Node;
161 typedef typename Digraph::NodeIt NodeIt;
162 typedef typename Digraph::Arc Arc;
163 typedef typename Digraph::OutArcIt OutArcIt;
165 //Pointer to the underlying digraph.
167 //Pointer to the map of predecessor arcs.
169 //Indicates if _pred is locally allocated (true) or not.
171 //Pointer to the map of distances.
173 //Indicates if _dist is locally allocated (true) or not.
175 //Pointer to the map of reached status of the nodes.
176 ReachedMap *_reached;
177 //Indicates if _reached is locally allocated (true) or not.
179 //Pointer to the map of processed status of the nodes.
180 ProcessedMap *_processed;
181 //Indicates if _processed is locally allocated (true) or not.
182 bool local_processed;
184 std::vector<typename Digraph::OutArcIt> _stack;
187 //Creates the maps if necessary.
192 _pred = Traits::createPredMap(*G);
196 _dist = Traits::createDistMap(*G);
199 local_reached = true;
200 _reached = Traits::createReachedMap(*G);
203 local_processed = true;
204 _processed = Traits::createProcessedMap(*G);
216 ///\name Named Template Parameters
221 struct SetPredMapTraits : public Traits {
223 static PredMap *createPredMap(const Digraph &)
225 LEMON_ASSERT(false, "PredMap is not initialized");
226 return 0; // ignore warnings
229 ///\brief \ref named-templ-param "Named parameter" for setting
232 ///\ref named-templ-param "Named parameter" for setting
234 ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
236 struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > {
237 typedef Dfs<Digraph, SetPredMapTraits<T> > Create;
241 struct SetDistMapTraits : public Traits {
243 static DistMap *createDistMap(const Digraph &)
245 LEMON_ASSERT(false, "DistMap is not initialized");
246 return 0; // ignore warnings
249 ///\brief \ref named-templ-param "Named parameter" for setting
252 ///\ref named-templ-param "Named parameter" for setting
254 ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
256 struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > {
257 typedef Dfs<Digraph, SetDistMapTraits<T> > Create;
261 struct SetReachedMapTraits : public Traits {
262 typedef T ReachedMap;
263 static ReachedMap *createReachedMap(const Digraph &)
265 LEMON_ASSERT(false, "ReachedMap is not initialized");
266 return 0; // ignore warnings
269 ///\brief \ref named-templ-param "Named parameter" for setting
270 ///\c ReachedMap type.
272 ///\ref named-templ-param "Named parameter" for setting
273 ///\c ReachedMap type.
274 ///It must conform to
275 ///the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
277 struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
278 typedef Dfs< Digraph, SetReachedMapTraits<T> > Create;
282 struct SetProcessedMapTraits : public Traits {
283 typedef T ProcessedMap;
284 static ProcessedMap *createProcessedMap(const Digraph &)
286 LEMON_ASSERT(false, "ProcessedMap is not initialized");
287 return 0; // ignore warnings
290 ///\brief \ref named-templ-param "Named parameter" for setting
291 ///\c ProcessedMap type.
293 ///\ref named-templ-param "Named parameter" for setting
294 ///\c ProcessedMap type.
295 ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
297 struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
298 typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create;
301 struct SetStandardProcessedMapTraits : public Traits {
302 typedef typename Digraph::template NodeMap<bool> ProcessedMap;
303 static ProcessedMap *createProcessedMap(const Digraph &g)
305 return new ProcessedMap(g);
308 ///\brief \ref named-templ-param "Named parameter" for setting
309 ///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
311 ///\ref named-templ-param "Named parameter" for setting
312 ///\c ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
313 ///If you don't set it explicitly, it will be automatically allocated.
314 struct SetStandardProcessedMap :
315 public Dfs< Digraph, SetStandardProcessedMapTraits > {
316 typedef Dfs< Digraph, SetStandardProcessedMapTraits > Create;
326 ///\param g The digraph the algorithm runs on.
327 Dfs(const Digraph &g) :
329 _pred(NULL), local_pred(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_dist) delete _dist;
340 if(local_reached) delete _reached;
341 if(local_processed) delete _processed;
344 ///Sets the map that stores the predecessor arcs.
346 ///Sets the map that stores the predecessor arcs.
347 ///If you don't use this function before calling \ref run(Node) "run()"
348 ///or \ref init(), an instance will be allocated automatically.
349 ///The destructor deallocates this automatically allocated map,
351 ///\return <tt> (*this) </tt>
352 Dfs &predMap(PredMap &m)
362 ///Sets the map that indicates which nodes are reached.
364 ///Sets the map that indicates which nodes are reached.
365 ///If you don't use this function before calling \ref run(Node) "run()"
366 ///or \ref init(), an instance will be allocated automatically.
367 ///The destructor deallocates this automatically allocated map,
369 ///\return <tt> (*this) </tt>
370 Dfs &reachedMap(ReachedMap &m)
380 ///Sets the map that indicates which nodes are processed.
382 ///Sets the map that indicates which nodes are processed.
383 ///If you don't use this function before calling \ref run(Node) "run()"
384 ///or \ref init(), an instance will be allocated automatically.
385 ///The destructor deallocates this automatically allocated map,
387 ///\return <tt> (*this) </tt>
388 Dfs &processedMap(ProcessedMap &m)
390 if(local_processed) {
392 local_processed=false;
398 ///Sets the map that stores the distances of the nodes.
400 ///Sets the map that stores the distances of the nodes calculated by
402 ///If you don't use this function before calling \ref run(Node) "run()"
403 ///or \ref init(), an instance will be allocated automatically.
404 ///The destructor deallocates this automatically allocated map,
406 ///\return <tt> (*this) </tt>
407 Dfs &distMap(DistMap &m)
419 ///\name Execution Control
420 ///The simplest way to execute the DFS algorithm is to use one of the
421 ///member functions called \ref run(Node) "run()".\n
422 ///If you need better control on the execution, you have to call
423 ///\ref init() first, then you can add a source node with \ref addSource()
424 ///and perform the actual computation with \ref start().
425 ///This procedure can be repeated if there are nodes that have not
430 ///\brief Initializes the internal data structures.
432 ///Initializes the internal data structures.
436 _stack.resize(countNodes(*G));
438 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
439 _pred->set(u,INVALID);
440 _reached->set(u,false);
441 _processed->set(u,false);
445 ///Adds a new source node.
447 ///Adds a new source node to the set of nodes to be processed.
449 ///\pre The stack must be empty. Otherwise the algorithm gives
450 ///wrong results. (One of the outgoing arcs of all the source nodes
451 ///except for the last one will not be visited and distances will
453 void addSource(Node s)
455 LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
458 _reached->set(s,true);
459 _pred->set(s,INVALID);
462 _stack[++_stack_head]=e;
463 _dist->set(s,_stack_head);
466 _processed->set(s,true);
472 ///Processes the next arc.
474 ///Processes the next arc.
476 ///\return The processed arc.
478 ///\pre The stack must not be empty.
482 Arc e=_stack[_stack_head];
483 if(!(*_reached)[m=G->target(e)]) {
485 _reached->set(m,true);
487 _stack[_stack_head] = OutArcIt(*G, m);
488 _dist->set(m,_stack_head);
492 ++_stack[_stack_head];
494 while(_stack_head>=0 && _stack[_stack_head]==INVALID) {
495 _processed->set(m,true);
498 m=G->source(_stack[_stack_head]);
499 ++_stack[_stack_head];
505 ///Next arc to be processed.
507 ///Next arc to be processed.
509 ///\return The next arc to be processed or \c INVALID if the stack
511 OutArcIt nextArc() const
513 return _stack_head>=0?_stack[_stack_head]:INVALID;
516 ///Returns \c false if there are nodes to be processed.
518 ///Returns \c false if there are nodes to be processed
519 ///in the queue (stack).
520 bool emptyQueue() const { return _stack_head<0; }
522 ///Returns the number of the nodes to be processed.
524 ///Returns the number of the nodes to be processed
525 ///in the queue (stack).
526 int queueSize() const { return _stack_head+1; }
528 ///Executes the algorithm.
530 ///Executes the algorithm.
532 ///This method runs the %DFS algorithm from the root node
533 ///in order to compute the DFS path to each node.
535 /// The algorithm computes
537 ///- the distance of each node from the root in the %DFS tree.
539 ///\pre init() must be called and a root node should be
540 ///added with addSource() before using this function.
542 ///\note <tt>d.start()</tt> is just a shortcut of the following code.
544 /// while ( !d.emptyQueue() ) {
545 /// d.processNextArc();
550 while ( !emptyQueue() ) processNextArc();
553 ///Executes the algorithm until the given target node is reached.
555 ///Executes the algorithm until the given target node is reached.
557 ///This method runs the %DFS algorithm from the root node
558 ///in order to compute the DFS path to \c t.
560 ///The algorithm computes
561 ///- the %DFS path to \c t,
562 ///- the distance of \c t from the root in the %DFS tree.
564 ///\pre init() must be called and a root node should be
565 ///added with addSource() before using this function.
568 while ( !emptyQueue() && !(*_reached)[t] )
572 ///Executes the algorithm until a condition is met.
574 ///Executes the algorithm until a condition is met.
576 ///This method runs the %DFS algorithm from the root node
577 ///until an arc \c a with <tt>am[a]</tt> true is found.
579 ///\param am A \c bool (or convertible) arc map. The algorithm
580 ///will stop when it reaches an arc \c a with <tt>am[a]</tt> true.
582 ///\return The reached arc \c a with <tt>am[a]</tt> true or
583 ///\c INVALID if no such arc was found.
585 ///\pre init() must be called and a root node should be
586 ///added with addSource() before using this function.
588 ///\warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map,
590 template<class ArcBoolMap>
591 Arc start(const ArcBoolMap &am)
593 while ( !emptyQueue() && !am[_stack[_stack_head]] )
595 return emptyQueue() ? INVALID : _stack[_stack_head];
598 ///Runs the algorithm from the given source node.
600 ///This method runs the %DFS algorithm from node \c s
601 ///in order to compute the DFS path to each node.
603 ///The algorithm computes
605 ///- the distance of each node from the root in the %DFS tree.
607 ///\note <tt>d.run(s)</tt> is just a shortcut of the following code.
619 ///Finds the %DFS path between \c s and \c t.
621 ///This method runs the %DFS algorithm from node \c s
622 ///in order to compute the DFS path to node \c t
623 ///(it stops searching when \c t is processed)
625 ///\return \c true if \c t is reachable form \c s.
627 ///\note Apart from the return value, <tt>d.run(s,t)</tt> is
628 ///just a shortcut of the following code.
634 bool run(Node s,Node t) {
641 ///Runs the algorithm to visit all nodes in the digraph.
643 ///This method runs the %DFS algorithm in order to visit all nodes
646 ///\note <tt>d.run()</tt> is just a shortcut of the following code.
649 /// for (NodeIt n(digraph); n != INVALID; ++n) {
650 /// if (!d.reached(n)) {
658 for (NodeIt it(*G); it != INVALID; ++it) {
668 ///\name Query Functions
669 ///The results of the DFS algorithm can be obtained using these
671 ///Either \ref run(Node) "run()" or \ref start() should be called
672 ///before using them.
676 ///The DFS path to the given node.
678 ///Returns the DFS path to the given node from the root(s).
680 ///\warning \c t should be reached from the root(s).
682 ///\pre Either \ref run(Node) "run()" or \ref init()
683 ///must be called before using this function.
684 Path path(Node t) const { return Path(*G, *_pred, t); }
686 ///The distance of the given node from the root(s).
688 ///Returns the distance of the given node from the root(s).
690 ///\warning If node \c v is not reached from the root(s), then
691 ///the return value of this function is undefined.
693 ///\pre Either \ref run(Node) "run()" or \ref init()
694 ///must be called before using this function.
695 int dist(Node v) const { return (*_dist)[v]; }
697 ///Returns the 'previous arc' of the %DFS tree for the given node.
699 ///This function returns the 'previous arc' of the %DFS tree for the
700 ///node \c v, i.e. it returns the last arc of a %DFS path from a
701 ///root to \c v. It is \c INVALID if \c v is not reached from the
702 ///root(s) or if \c v is a root.
704 ///The %DFS tree used here is equal to the %DFS tree used in
705 ///\ref predNode() and \ref predMap().
707 ///\pre Either \ref run(Node) "run()" or \ref init()
708 ///must be called before using this function.
709 Arc predArc(Node v) const { return (*_pred)[v];}
711 ///Returns the 'previous node' of the %DFS tree for the given node.
713 ///This function returns the 'previous node' of the %DFS
714 ///tree for the node \c v, i.e. it returns the last but one node
715 ///of a %DFS path from a root to \c v. It is \c INVALID
716 ///if \c v is not reached from the root(s) or if \c v is a root.
718 ///The %DFS tree used here is equal to the %DFS tree used in
719 ///\ref predArc() and \ref predMap().
721 ///\pre Either \ref run(Node) "run()" or \ref init()
722 ///must be called before using this function.
723 Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
724 G->source((*_pred)[v]); }
726 ///\brief Returns a const reference to the node map that stores the
727 ///distances of the nodes.
729 ///Returns a const reference to the node map that stores the
730 ///distances of the nodes calculated by the algorithm.
732 ///\pre Either \ref run(Node) "run()" or \ref init()
733 ///must be called before using this function.
734 const DistMap &distMap() const { return *_dist;}
736 ///\brief Returns a const reference to the node map that stores the
739 ///Returns a const reference to the node map that stores the predecessor
740 ///arcs, which form the DFS tree (forest).
742 ///\pre Either \ref run(Node) "run()" or \ref init()
743 ///must be called before using this function.
744 const PredMap &predMap() const { return *_pred;}
746 ///Checks if the given node. node is reached from the root(s).
748 ///Returns \c true if \c v is reached from the root(s).
750 ///\pre Either \ref run(Node) "run()" or \ref init()
751 ///must be called before using this function.
752 bool reached(Node v) const { return (*_reached)[v]; }
757 ///Default traits class of dfs() function.
759 ///Default traits class of dfs() function.
760 ///\tparam GR Digraph type.
762 struct DfsWizardDefaultTraits
764 ///The type of the digraph the algorithm runs on.
767 ///\brief The type of the map that stores the predecessor
768 ///arcs of the %DFS paths.
770 ///The type of the map that stores the predecessor
771 ///arcs of the %DFS paths.
772 ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
773 typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
774 ///Instantiates a PredMap.
776 ///This function instantiates a PredMap.
777 ///\param g is the digraph, to which we would like to define the
779 static PredMap *createPredMap(const Digraph &g)
781 return new PredMap(g);
784 ///The type of the map that indicates which nodes are processed.
786 ///The type of the map that indicates which nodes are processed.
787 ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
788 ///By default, it is a NullMap.
789 typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
790 ///Instantiates a ProcessedMap.
792 ///This function instantiates a ProcessedMap.
793 ///\param g is the digraph, to which
794 ///we would like to define the ProcessedMap.
796 static ProcessedMap *createProcessedMap(const Digraph &g)
798 static ProcessedMap *createProcessedMap(const Digraph &)
801 return new ProcessedMap();
804 ///The type of the map that indicates which nodes are reached.
806 ///The type of the map that indicates which nodes are reached.
807 ///It must conform to
808 ///the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
809 typedef typename Digraph::template NodeMap<bool> ReachedMap;
810 ///Instantiates a ReachedMap.
812 ///This function instantiates a ReachedMap.
813 ///\param g is the digraph, to which
814 ///we would like to define the ReachedMap.
815 static ReachedMap *createReachedMap(const Digraph &g)
817 return new ReachedMap(g);
820 ///The type of the map that stores the distances of the nodes.
822 ///The type of the map that stores the distances of the nodes.
823 ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
824 typedef typename Digraph::template NodeMap<int> DistMap;
825 ///Instantiates a DistMap.
827 ///This function instantiates a DistMap.
828 ///\param g is the digraph, to which we would like to define
830 static DistMap *createDistMap(const Digraph &g)
832 return new DistMap(g);
835 ///The type of the DFS paths.
837 ///The type of the DFS paths.
838 ///It must conform to the \ref concepts::Path "Path" concept.
839 typedef lemon::Path<Digraph> Path;
842 /// Default traits class used by DfsWizard
844 /// Default traits class used by DfsWizard.
845 /// \tparam GR The type of the digraph.
847 class DfsWizardBase : public DfsWizardDefaultTraits<GR>
850 typedef DfsWizardDefaultTraits<GR> Base;
852 //The type of the nodes in the digraph.
853 typedef typename Base::Digraph::Node Node;
855 //Pointer to the digraph the algorithm runs on.
857 //Pointer to the map of reached nodes.
859 //Pointer to the map of processed nodes.
861 //Pointer to the map of predecessors arcs.
863 //Pointer to the map of distances.
865 //Pointer to the DFS path to the target node.
867 //Pointer to the distance of the target node.
873 /// This constructor does not require parameters, it initiates
874 /// all of the attributes to \c 0.
875 DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
876 _dist(0), _path(0), _di(0) {}
880 /// This constructor requires one parameter,
881 /// others are initiated to \c 0.
882 /// \param g The digraph the algorithm runs on.
883 DfsWizardBase(const GR &g) :
884 _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
885 _reached(0), _processed(0), _pred(0), _dist(0), _path(0), _di(0) {}
889 /// Auxiliary class for the function-type interface of DFS algorithm.
891 /// This auxiliary class is created to implement the
892 /// \ref dfs() "function-type interface" of \ref Dfs algorithm.
893 /// It does not have own \ref run(Node) "run()" method, it uses the
894 /// functions and features of the plain \ref Dfs.
896 /// This class should only be used through the \ref dfs() function,
897 /// which makes it easier to use the algorithm.
899 /// \tparam TR The traits class that defines various types used by the
902 class DfsWizard : public TR
906 typedef typename TR::Digraph Digraph;
908 typedef typename Digraph::Node Node;
909 typedef typename Digraph::NodeIt NodeIt;
910 typedef typename Digraph::Arc Arc;
911 typedef typename Digraph::OutArcIt OutArcIt;
913 typedef typename TR::PredMap PredMap;
914 typedef typename TR::DistMap DistMap;
915 typedef typename TR::ReachedMap ReachedMap;
916 typedef typename TR::ProcessedMap ProcessedMap;
917 typedef typename TR::Path Path;
922 DfsWizard() : TR() {}
924 /// Constructor that requires parameters.
926 /// Constructor that requires parameters.
927 /// These parameters will be the default values for the traits class.
928 /// \param g The digraph the algorithm runs on.
929 DfsWizard(const Digraph &g) :
933 DfsWizard(const TR &b) : TR(b) {}
937 ///Runs DFS algorithm from the given source node.
939 ///This method runs DFS algorithm from node \c s
940 ///in order to compute the DFS path to each node.
943 Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
945 alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
947 alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
949 alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
950 if (Base::_processed)
951 alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
958 ///Finds the DFS path between \c s and \c t.
960 ///This method runs DFS algorithm from node \c s
961 ///in order to compute the DFS path to node \c t
962 ///(it stops searching when \c t is processed).
964 ///\return \c true if \c t is reachable form \c s.
965 bool run(Node s, Node t)
967 Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
969 alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
971 alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
973 alg.reachedMap(*reinterpret_cast<ReachedMap*>(Base::_reached));
974 if (Base::_processed)
975 alg.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
978 *reinterpret_cast<Path*>(Base::_path) = alg.path(t);
980 *Base::_di = alg.dist(t);
981 return alg.reached(t);
984 ///Runs DFS algorithm to visit all nodes in the digraph.
986 ///This method runs DFS algorithm in order to visit all nodes
994 struct SetPredMapBase : public Base {
996 static PredMap *createPredMap(const Digraph &) { return 0; };
997 SetPredMapBase(const TR &b) : TR(b) {}
1000 ///\brief \ref named-templ-param "Named parameter" for setting
1001 ///the predecessor map.
1003 ///\ref named-templ-param "Named parameter" function for setting
1004 ///the map that stores the predecessor arcs of the nodes.
1006 DfsWizard<SetPredMapBase<T> > predMap(const T &t)
1008 Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
1009 return DfsWizard<SetPredMapBase<T> >(*this);
1013 struct SetReachedMapBase : public Base {
1014 typedef T ReachedMap;
1015 static ReachedMap *createReachedMap(const Digraph &) { return 0; };
1016 SetReachedMapBase(const TR &b) : TR(b) {}
1019 ///\brief \ref named-templ-param "Named parameter" for setting
1022 ///\ref named-templ-param "Named parameter" function for setting
1023 ///the map that indicates which nodes are reached.
1025 DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
1027 Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
1028 return DfsWizard<SetReachedMapBase<T> >(*this);
1032 struct SetDistMapBase : public Base {
1034 static DistMap *createDistMap(const Digraph &) { return 0; };
1035 SetDistMapBase(const TR &b) : TR(b) {}
1038 ///\brief \ref named-templ-param "Named parameter" for setting
1039 ///the distance map.
1041 ///\ref named-templ-param "Named parameter" function for setting
1042 ///the map that stores the distances of the nodes calculated
1043 ///by the algorithm.
1045 DfsWizard<SetDistMapBase<T> > distMap(const T &t)
1047 Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
1048 return DfsWizard<SetDistMapBase<T> >(*this);
1052 struct SetProcessedMapBase : public Base {
1053 typedef T ProcessedMap;
1054 static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
1055 SetProcessedMapBase(const TR &b) : TR(b) {}
1058 ///\brief \ref named-func-param "Named parameter" for setting
1059 ///the processed map.
1061 ///\ref named-templ-param "Named parameter" function for setting
1062 ///the map that indicates which nodes are processed.
1064 DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
1066 Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
1067 return DfsWizard<SetProcessedMapBase<T> >(*this);
1071 struct SetPathBase : public Base {
1073 SetPathBase(const TR &b) : TR(b) {}
1075 ///\brief \ref named-func-param "Named parameter"
1076 ///for getting the DFS path to the target node.
1078 ///\ref named-func-param "Named parameter"
1079 ///for getting the DFS path to the target node.
1081 DfsWizard<SetPathBase<T> > path(const T &t)
1083 Base::_path=reinterpret_cast<void*>(const_cast<T*>(&t));
1084 return DfsWizard<SetPathBase<T> >(*this);
1087 ///\brief \ref named-func-param "Named parameter"
1088 ///for getting the distance of the target node.
1090 ///\ref named-func-param "Named parameter"
1091 ///for getting the distance of the target node.
1092 DfsWizard dist(const int &d)
1094 Base::_di=const_cast<int*>(&d);
1100 ///Function-type interface for DFS algorithm.
1103 ///Function-type interface for DFS algorithm.
1105 ///This function also has several \ref named-func-param "named parameters",
1106 ///they are declared as the members of class \ref DfsWizard.
1107 ///The following examples show how to use these parameters.
1109 /// // Compute the DFS tree
1110 /// dfs(g).predMap(preds).distMap(dists).run(s);
1112 /// // Compute the DFS path from s to t
1113 /// bool reached = dfs(g).path(p).dist(d).run(s,t);
1115 ///\warning Don't forget to put the \ref DfsWizard::run(Node) "run()"
1116 ///to the end of the parameter list.
1120 DfsWizard<DfsWizardBase<GR> >
1121 dfs(const GR &digraph)
1123 return DfsWizard<DfsWizardBase<GR> >(digraph);
1127 /// \brief Visitor class for DFS.
1129 /// This class defines the interface of the DfsVisit events, and
1130 /// it could be the base of a real visitor class.
1131 template <typename GR>
1134 typedef typename Digraph::Arc Arc;
1135 typedef typename Digraph::Node Node;
1136 /// \brief Called for the source node of the DFS.
1138 /// This function is called for the source node of the DFS.
1139 void start(const Node& node) {}
1140 /// \brief Called when the source node is leaved.
1142 /// This function is called when the source node is leaved.
1143 void stop(const Node& node) {}
1144 /// \brief Called when a node is reached first time.
1146 /// This function is called when a node is reached first time.
1147 void reach(const Node& node) {}
1148 /// \brief Called when an arc reaches a new node.
1150 /// This function is called when the DFS finds an arc whose target node
1151 /// is not reached yet.
1152 void discover(const Arc& arc) {}
1153 /// \brief Called when an arc is examined but its target node is
1154 /// already discovered.
1156 /// This function is called when an arc is examined but its target node is
1157 /// already discovered.
1158 void examine(const Arc& arc) {}
1159 /// \brief Called when the DFS steps back from a node.
1161 /// This function is called when the DFS steps back from a node.
1162 void leave(const Node& node) {}
1163 /// \brief Called when the DFS steps back on an arc.
1165 /// This function is called when the DFS steps back on an arc.
1166 void backtrack(const Arc& arc) {}
1169 template <typename GR>
1172 typedef typename Digraph::Arc Arc;
1173 typedef typename Digraph::Node Node;
1174 void start(const Node&) {}
1175 void stop(const Node&) {}
1176 void reach(const Node&) {}
1177 void discover(const Arc&) {}
1178 void examine(const Arc&) {}
1179 void leave(const Node&) {}
1180 void backtrack(const Arc&) {}
1182 template <typename _Visitor>
1183 struct Constraints {
1184 void constraints() {
1187 visitor.start(node);
1189 visitor.reach(node);
1190 visitor.discover(arc);
1191 visitor.examine(arc);
1192 visitor.leave(node);
1193 visitor.backtrack(arc);
1201 /// \brief Default traits class of DfsVisit class.
1203 /// Default traits class of DfsVisit class.
1204 /// \tparam _Digraph The type of the digraph the algorithm runs on.
1206 struct DfsVisitDefaultTraits {
1208 /// \brief The type of the digraph the algorithm runs on.
1211 /// \brief The type of the map that indicates which nodes are reached.
1213 /// The type of the map that indicates which nodes are reached.
1214 /// It must conform to the
1215 /// \ref concepts::ReadWriteMap "ReadWriteMap" concept.
1216 typedef typename Digraph::template NodeMap<bool> ReachedMap;
1218 /// \brief Instantiates a ReachedMap.
1220 /// This function instantiates a ReachedMap.
1221 /// \param digraph is the digraph, to which
1222 /// we would like to define the ReachedMap.
1223 static ReachedMap *createReachedMap(const Digraph &digraph) {
1224 return new ReachedMap(digraph);
1231 /// \brief DFS algorithm class with visitor interface.
1233 /// This class provides an efficient implementation of the DFS algorithm
1234 /// with visitor interface.
1236 /// The DfsVisit class provides an alternative interface to the Dfs
1237 /// class. It works with callback mechanism, the DfsVisit object calls
1238 /// the member functions of the \c Visitor class on every DFS event.
1240 /// This interface of the DFS algorithm should be used in special cases
1241 /// when extra actions have to be performed in connection with certain
1242 /// events of the DFS algorithm. Otherwise consider to use Dfs or dfs()
1245 /// \tparam GR The type of the digraph the algorithm runs on.
1246 /// The default type is \ref ListDigraph.
1247 /// The value of GR is not used directly by \ref DfsVisit,
1248 /// it is only passed to \ref DfsVisitDefaultTraits.
1249 /// \tparam VS The Visitor type that is used by the algorithm.
1250 /// \ref DfsVisitor "DfsVisitor<GR>" is an empty visitor, which
1251 /// does not observe the DFS events. If you want to observe the DFS
1252 /// events, you should implement your own visitor class.
1253 /// \tparam TR The traits class that defines various types used by the
1254 /// algorithm. By default, it is \ref DfsVisitDefaultTraits
1255 /// "DfsVisitDefaultTraits<GR>".
1256 /// In most cases, this parameter should not be set directly,
1257 /// consider to use the named template parameters instead.
1259 template <typename GR, typename VS, typename TR>
1261 template <typename GR = ListDigraph,
1262 typename VS = DfsVisitor<GR>,
1263 typename TR = DfsVisitDefaultTraits<GR> >
1268 ///The traits class.
1271 ///The type of the digraph the algorithm runs on.
1272 typedef typename Traits::Digraph Digraph;
1274 ///The visitor type used by the algorithm.
1277 ///The type of the map that indicates which nodes are reached.
1278 typedef typename Traits::ReachedMap ReachedMap;
1282 typedef typename Digraph::Node Node;
1283 typedef typename Digraph::NodeIt NodeIt;
1284 typedef typename Digraph::Arc Arc;
1285 typedef typename Digraph::OutArcIt OutArcIt;
1287 //Pointer to the underlying digraph.
1288 const Digraph *_digraph;
1289 //Pointer to the visitor object.
1291 //Pointer to the map of reached status of the nodes.
1292 ReachedMap *_reached;
1293 //Indicates if _reached is locally allocated (true) or not.
1296 std::vector<typename Digraph::Arc> _stack;
1299 //Creates the maps if necessary.
1300 void create_maps() {
1302 local_reached = true;
1303 _reached = Traits::createReachedMap(*_digraph);
1313 typedef DfsVisit Create;
1315 /// \name Named Template Parameters
1319 struct SetReachedMapTraits : public Traits {
1320 typedef T ReachedMap;
1321 static ReachedMap *createReachedMap(const Digraph &digraph) {
1322 LEMON_ASSERT(false, "ReachedMap is not initialized");
1323 return 0; // ignore warnings
1326 /// \brief \ref named-templ-param "Named parameter" for setting
1327 /// ReachedMap type.
1329 /// \ref named-templ-param "Named parameter" for setting ReachedMap type.
1331 struct SetReachedMap : public DfsVisit< Digraph, Visitor,
1332 SetReachedMapTraits<T> > {
1333 typedef DfsVisit< Digraph, Visitor, SetReachedMapTraits<T> > Create;
1339 /// \brief Constructor.
1343 /// \param digraph The digraph the algorithm runs on.
1344 /// \param visitor The visitor object of the algorithm.
1345 DfsVisit(const Digraph& digraph, Visitor& visitor)
1346 : _digraph(&digraph), _visitor(&visitor),
1347 _reached(0), local_reached(false) {}
1349 /// \brief Destructor.
1351 if(local_reached) delete _reached;
1354 /// \brief Sets the map that indicates which nodes are reached.
1356 /// Sets the map that indicates which nodes are reached.
1357 /// If you don't use this function before calling \ref run(Node) "run()"
1358 /// or \ref init(), an instance will be allocated automatically.
1359 /// The destructor deallocates this automatically allocated map,
1361 /// \return <tt> (*this) </tt>
1362 DfsVisit &reachedMap(ReachedMap &m) {
1365 local_reached=false;
1373 /// \name Execution Control
1374 /// The simplest way to execute the DFS algorithm is to use one of the
1375 /// member functions called \ref run(Node) "run()".\n
1376 /// If you need better control on the execution, you have to call
1377 /// \ref init() first, then you can add a source node with \ref addSource()
1378 /// and perform the actual computation with \ref start().
1379 /// This procedure can be repeated if there are nodes that have not
1384 /// \brief Initializes the internal data structures.
1386 /// Initializes the internal data structures.
1389 _stack.resize(countNodes(*_digraph));
1391 for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
1392 _reached->set(u, false);
1396 /// \brief Adds a new source node.
1398 /// Adds a new source node to the set of nodes to be processed.
1400 /// \pre The stack must be empty. Otherwise the algorithm gives
1401 /// wrong results. (One of the outgoing arcs of all the source nodes
1402 /// except for the last one will not be visited and distances will
1404 void addSource(Node s)
1406 LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
1407 if(!(*_reached)[s]) {
1408 _reached->set(s,true);
1412 _digraph->firstOut(e, s);
1414 _stack[++_stack_head] = e;
1422 /// \brief Processes the next arc.
1424 /// Processes the next arc.
1426 /// \return The processed arc.
1428 /// \pre The stack must not be empty.
1429 Arc processNextArc() {
1430 Arc e = _stack[_stack_head];
1431 Node m = _digraph->target(e);
1432 if(!(*_reached)[m]) {
1433 _visitor->discover(e);
1435 _reached->set(m, true);
1436 _digraph->firstOut(_stack[++_stack_head], m);
1438 _visitor->examine(e);
1439 m = _digraph->source(e);
1440 _digraph->nextOut(_stack[_stack_head]);
1442 while (_stack_head>=0 && _stack[_stack_head] == INVALID) {
1445 if (_stack_head >= 0) {
1446 _visitor->backtrack(_stack[_stack_head]);
1447 m = _digraph->source(_stack[_stack_head]);
1448 _digraph->nextOut(_stack[_stack_head]);
1456 /// \brief Next arc to be processed.
1458 /// Next arc to be processed.
1460 /// \return The next arc to be processed or INVALID if the stack is
1462 Arc nextArc() const {
1463 return _stack_head >= 0 ? _stack[_stack_head] : INVALID;
1466 /// \brief Returns \c false if there are nodes
1467 /// to be processed.
1469 /// Returns \c false if there are nodes
1470 /// to be processed in the queue (stack).
1471 bool emptyQueue() const { return _stack_head < 0; }
1473 /// \brief Returns the number of the nodes to be processed.
1475 /// Returns the number of the nodes to be processed in the queue (stack).
1476 int queueSize() const { return _stack_head + 1; }
1478 /// \brief Executes the algorithm.
1480 /// Executes the algorithm.
1482 /// This method runs the %DFS algorithm from the root node
1483 /// in order to compute the %DFS path to each node.
1485 /// The algorithm computes
1486 /// - the %DFS tree,
1487 /// - the distance of each node from the root in the %DFS tree.
1489 /// \pre init() must be called and a root node should be
1490 /// added with addSource() before using this function.
1492 /// \note <tt>d.start()</tt> is just a shortcut of the following code.
1494 /// while ( !d.emptyQueue() ) {
1495 /// d.processNextArc();
1499 while ( !emptyQueue() ) processNextArc();
1502 /// \brief Executes the algorithm until the given target node is reached.
1504 /// Executes the algorithm until the given target node is reached.
1506 /// This method runs the %DFS algorithm from the root node
1507 /// in order to compute the DFS path to \c t.
1509 /// The algorithm computes
1510 /// - the %DFS path to \c t,
1511 /// - the distance of \c t from the root in the %DFS tree.
1513 /// \pre init() must be called and a root node should be added
1514 /// with addSource() before using this function.
1515 void start(Node t) {
1516 while ( !emptyQueue() && !(*_reached)[t] )
1520 /// \brief Executes the algorithm until a condition is met.
1522 /// Executes the algorithm until a condition is met.
1524 /// This method runs the %DFS algorithm from the root node
1525 /// until an arc \c a with <tt>am[a]</tt> true is found.
1527 /// \param am A \c bool (or convertible) arc map. The algorithm
1528 /// will stop when it reaches an arc \c a with <tt>am[a]</tt> true.
1530 /// \return The reached arc \c a with <tt>am[a]</tt> true or
1531 /// \c INVALID if no such arc was found.
1533 /// \pre init() must be called and a root node should be added
1534 /// with addSource() before using this function.
1536 /// \warning Contrary to \ref Bfs and \ref Dijkstra, \c am is an arc map,
1538 template <typename AM>
1539 Arc start(const AM &am) {
1540 while ( !emptyQueue() && !am[_stack[_stack_head]] )
1542 return emptyQueue() ? INVALID : _stack[_stack_head];
1545 /// \brief Runs the algorithm from the given source node.
1547 /// This method runs the %DFS algorithm from node \c s.
1548 /// in order to compute the DFS path to each node.
1550 /// The algorithm computes
1551 /// - the %DFS tree,
1552 /// - the distance of each node from the root in the %DFS tree.
1554 /// \note <tt>d.run(s)</tt> is just a shortcut of the following code.
1566 /// \brief Finds the %DFS path between \c s and \c t.
1568 /// This method runs the %DFS algorithm from node \c s
1569 /// in order to compute the DFS path to node \c t
1570 /// (it stops searching when \c t is processed).
1572 /// \return \c true if \c t is reachable form \c s.
1574 /// \note Apart from the return value, <tt>d.run(s,t)</tt> is
1575 /// just a shortcut of the following code.
1581 bool run(Node s,Node t) {
1588 /// \brief Runs the algorithm to visit all nodes in the digraph.
1590 /// This method runs the %DFS algorithm in order to visit all nodes
1593 /// \note <tt>d.run()</tt> is just a shortcut of the following code.
1596 /// for (NodeIt n(digraph); n != INVALID; ++n) {
1597 /// if (!d.reached(n)) {
1605 for (NodeIt it(*_digraph); it != INVALID; ++it) {
1615 /// \name Query Functions
1616 /// The results of the DFS algorithm can be obtained using these
1618 /// Either \ref run(Node) "run()" or \ref start() should be called
1619 /// before using them.
1623 /// \brief Checks if the given node is reached from the root(s).
1625 /// Returns \c true if \c v is reached from the root(s).
1627 /// \pre Either \ref run(Node) "run()" or \ref init()
1628 /// must be called before using this function.
1629 bool reached(Node v) const { return (*_reached)[v]; }
1635 } //END OF NAMESPACE LEMON