lemon/dfs.h
changeset 418 ad483acf1654
parent 319 5e12d7734036
child 419 9afe81e4c543
child 420 6a2a33ad261b
equal deleted inserted replaced
21:e43c2bec5a78 22:df6a2babf106
   117   ///There is also a \ref dfs() "function-type interface" for the DFS
   117   ///There is also a \ref dfs() "function-type interface" for the DFS
   118   ///algorithm, which is convenient in the simplier cases and it can be
   118   ///algorithm, which is convenient in the simplier cases and it can be
   119   ///used easier.
   119   ///used easier.
   120   ///
   120   ///
   121   ///\tparam GR The type of the digraph the algorithm runs on.
   121   ///\tparam GR The type of the digraph the algorithm runs on.
   122   ///The default value is \ref ListDigraph. The value of GR is not used
   122   ///The default type is \ref ListDigraph.
   123   ///directly by \ref Dfs, it is only passed to \ref DfsDefaultTraits.
       
   124   ///\tparam TR Traits class to set various data types used by the algorithm.
       
   125   ///The default traits class is
       
   126   ///\ref DfsDefaultTraits "DfsDefaultTraits<GR>".
       
   127   ///See \ref DfsDefaultTraits for the documentation of
       
   128   ///a Dfs traits class.
       
   129 #ifdef DOXYGEN
   123 #ifdef DOXYGEN
   130   template <typename GR,
   124   template <typename GR,
   131             typename TR>
   125             typename TR>
   132 #else
   126 #else
   133   template <typename GR=ListDigraph,
   127   template <typename GR=ListDigraph,
   149     ///The type of the map that indicates which nodes are processed.
   143     ///The type of the map that indicates which nodes are processed.
   150     typedef typename TR::ProcessedMap ProcessedMap;
   144     typedef typename TR::ProcessedMap ProcessedMap;
   151     ///The type of the paths.
   145     ///The type of the paths.
   152     typedef PredMapPath<Digraph, PredMap> Path;
   146     typedef PredMapPath<Digraph, PredMap> Path;
   153 
   147 
   154     ///The traits class.
   148     ///The \ref DfsDefaultTraits "traits class" of the algorithm.
   155     typedef TR Traits;
   149     typedef TR Traits;
   156 
   150 
   157   private:
   151   private:
   158 
   152 
   159     typedef typename Digraph::Node Node;
   153     typedef typename Digraph::Node Node;
   228     ///\brief \ref named-templ-param "Named parameter" for setting
   222     ///\brief \ref named-templ-param "Named parameter" for setting
   229     ///PredMap type.
   223     ///PredMap type.
   230     ///
   224     ///
   231     ///\ref named-templ-param "Named parameter" for setting
   225     ///\ref named-templ-param "Named parameter" for setting
   232     ///PredMap type.
   226     ///PredMap type.
       
   227     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   233     template <class T>
   228     template <class T>
   234     struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > {
   229     struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > {
   235       typedef Dfs<Digraph, SetPredMapTraits<T> > Create;
   230       typedef Dfs<Digraph, SetPredMapTraits<T> > Create;
   236     };
   231     };
   237 
   232 
   247     ///\brief \ref named-templ-param "Named parameter" for setting
   242     ///\brief \ref named-templ-param "Named parameter" for setting
   248     ///DistMap type.
   243     ///DistMap type.
   249     ///
   244     ///
   250     ///\ref named-templ-param "Named parameter" for setting
   245     ///\ref named-templ-param "Named parameter" for setting
   251     ///DistMap type.
   246     ///DistMap type.
       
   247     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   252     template <class T>
   248     template <class T>
   253     struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > {
   249     struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > {
   254       typedef Dfs<Digraph, SetDistMapTraits<T> > Create;
   250       typedef Dfs<Digraph, SetDistMapTraits<T> > Create;
   255     };
   251     };
   256 
   252 
   266     ///\brief \ref named-templ-param "Named parameter" for setting
   262     ///\brief \ref named-templ-param "Named parameter" for setting
   267     ///ReachedMap type.
   263     ///ReachedMap type.
   268     ///
   264     ///
   269     ///\ref named-templ-param "Named parameter" for setting
   265     ///\ref named-templ-param "Named parameter" for setting
   270     ///ReachedMap type.
   266     ///ReachedMap type.
       
   267     ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
   271     template <class T>
   268     template <class T>
   272     struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
   269     struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
   273       typedef Dfs< Digraph, SetReachedMapTraits<T> > Create;
   270       typedef Dfs< Digraph, SetReachedMapTraits<T> > Create;
   274     };
   271     };
   275 
   272 
   285     ///\brief \ref named-templ-param "Named parameter" for setting
   282     ///\brief \ref named-templ-param "Named parameter" for setting
   286     ///ProcessedMap type.
   283     ///ProcessedMap type.
   287     ///
   284     ///
   288     ///\ref named-templ-param "Named parameter" for setting
   285     ///\ref named-templ-param "Named parameter" for setting
   289     ///ProcessedMap type.
   286     ///ProcessedMap type.
       
   287     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   290     template <class T>
   288     template <class T>
   291     struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
   289     struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
   292       typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create;
   290       typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create;
   293     };
   291     };
   294 
   292 
   336     }
   334     }
   337 
   335 
   338     ///Sets the map that stores the predecessor arcs.
   336     ///Sets the map that stores the predecessor arcs.
   339 
   337 
   340     ///Sets the map that stores the predecessor arcs.
   338     ///Sets the map that stores the predecessor arcs.
   341     ///If you don't use this function before calling \ref run(),
   339     ///If you don't use this function before calling \ref run(Node) "run()"
   342     ///it will allocate one. The destructor deallocates this
   340     ///or \ref init(), an instance will be allocated automatically.
   343     ///automatically allocated map, of course.
   341     ///The destructor deallocates this automatically allocated map,
       
   342     ///of course.
   344     ///\return <tt> (*this) </tt>
   343     ///\return <tt> (*this) </tt>
   345     Dfs &predMap(PredMap &m)
   344     Dfs &predMap(PredMap &m)
   346     {
   345     {
   347       if(local_pred) {
   346       if(local_pred) {
   348         delete _pred;
   347         delete _pred;
   353     }
   352     }
   354 
   353 
   355     ///Sets the map that indicates which nodes are reached.
   354     ///Sets the map that indicates which nodes are reached.
   356 
   355 
   357     ///Sets the map that indicates which nodes are reached.
   356     ///Sets the map that indicates which nodes are reached.
   358     ///If you don't use this function before calling \ref run(),
   357     ///If you don't use this function before calling \ref run(Node) "run()"
   359     ///it will allocate one. The destructor deallocates this
   358     ///or \ref init(), an instance will be allocated automatically.
   360     ///automatically allocated map, of course.
   359     ///The destructor deallocates this automatically allocated map,
       
   360     ///of course.
   361     ///\return <tt> (*this) </tt>
   361     ///\return <tt> (*this) </tt>
   362     Dfs &reachedMap(ReachedMap &m)
   362     Dfs &reachedMap(ReachedMap &m)
   363     {
   363     {
   364       if(local_reached) {
   364       if(local_reached) {
   365         delete _reached;
   365         delete _reached;
   370     }
   370     }
   371 
   371 
   372     ///Sets the map that indicates which nodes are processed.
   372     ///Sets the map that indicates which nodes are processed.
   373 
   373 
   374     ///Sets the map that indicates which nodes are processed.
   374     ///Sets the map that indicates which nodes are processed.
   375     ///If you don't use this function before calling \ref run(),
   375     ///If you don't use this function before calling \ref run(Node) "run()"
   376     ///it will allocate one. The destructor deallocates this
   376     ///or \ref init(), an instance will be allocated automatically.
   377     ///automatically allocated map, of course.
   377     ///The destructor deallocates this automatically allocated map,
       
   378     ///of course.
   378     ///\return <tt> (*this) </tt>
   379     ///\return <tt> (*this) </tt>
   379     Dfs &processedMap(ProcessedMap &m)
   380     Dfs &processedMap(ProcessedMap &m)
   380     {
   381     {
   381       if(local_processed) {
   382       if(local_processed) {
   382         delete _processed;
   383         delete _processed;
   388 
   389 
   389     ///Sets the map that stores the distances of the nodes.
   390     ///Sets the map that stores the distances of the nodes.
   390 
   391 
   391     ///Sets the map that stores the distances of the nodes calculated by
   392     ///Sets the map that stores the distances of the nodes calculated by
   392     ///the algorithm.
   393     ///the algorithm.
   393     ///If you don't use this function before calling \ref run(),
   394     ///If you don't use this function before calling \ref run(Node) "run()"
   394     ///it will allocate one. The destructor deallocates this
   395     ///or \ref init(), an instance will be allocated automatically.
   395     ///automatically allocated map, of course.
   396     ///The destructor deallocates this automatically allocated map,
       
   397     ///of course.
   396     ///\return <tt> (*this) </tt>
   398     ///\return <tt> (*this) </tt>
   397     Dfs &distMap(DistMap &m)
   399     Dfs &distMap(DistMap &m)
   398     {
   400     {
   399       if(local_dist) {
   401       if(local_dist) {
   400         delete _dist;
   402         delete _dist;
   404       return *this;
   406       return *this;
   405     }
   407     }
   406 
   408 
   407   public:
   409   public:
   408 
   410 
   409     ///\name Execution control
   411     ///\name Execution Control
   410     ///The simplest way to execute the algorithm is to use
   412     ///The simplest way to execute the DFS algorithm is to use one of the
   411     ///one of the member functions called \ref lemon::Dfs::run() "run()".
   413     ///member functions called \ref run(Node) "run()".\n
   412     ///\n
   414     ///If you need more control on the execution, first you have to call
   413     ///If you need more control on the execution, first you must call
   415     ///\ref init(), then you can add a source node with \ref addSource()
   414     ///\ref lemon::Dfs::init() "init()", then you can add a source node
   416     ///and perform the actual computation with \ref start().
   415     ///with \ref lemon::Dfs::addSource() "addSource()".
   417     ///This procedure can be repeated if there are nodes that have not
   416     ///Finally \ref lemon::Dfs::start() "start()" will perform the
   418     ///been reached.
   417     ///actual path computation.
       
   418 
   419 
   419     ///@{
   420     ///@{
   420 
   421 
       
   422     ///\brief Initializes the internal data structures.
       
   423     ///
   421     ///Initializes the internal data structures.
   424     ///Initializes the internal data structures.
   422 
       
   423     ///Initializes the internal data structures.
       
   424     ///
       
   425     void init()
   425     void init()
   426     {
   426     {
   427       create_maps();
   427       create_maps();
   428       _stack.resize(countNodes(*G));
   428       _stack.resize(countNodes(*G));
   429       _stack_head=-1;
   429       _stack_head=-1;
   436 
   436 
   437     ///Adds a new source node.
   437     ///Adds a new source node.
   438 
   438 
   439     ///Adds a new source node to the set of nodes to be processed.
   439     ///Adds a new source node to the set of nodes to be processed.
   440     ///
   440     ///
   441     ///\pre The stack must be empty. (Otherwise the algorithm gives
   441     ///\pre The stack must be empty. Otherwise the algorithm gives
   442     ///false results.)
   442     ///wrong results. (One of the outgoing arcs of all the source nodes
   443     ///
   443     ///except for the last one will not be visited and distances will
   444     ///\warning Distances will be wrong (or at least strange) in case of
   444     ///also be wrong.)
   445     ///multiple sources.
       
   446     void addSource(Node s)
   445     void addSource(Node s)
   447     {
   446     {
   448       LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
   447       LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
   449       if(!(*_reached)[s])
   448       if(!(*_reached)[s])
   450         {
   449         {
   504     OutArcIt nextArc() const
   503     OutArcIt nextArc() const
   505     {
   504     {
   506       return _stack_head>=0?_stack[_stack_head]:INVALID;
   505       return _stack_head>=0?_stack[_stack_head]:INVALID;
   507     }
   506     }
   508 
   507 
   509     ///\brief Returns \c false if there are nodes
   508     ///Returns \c false if there are nodes to be processed.
   510     ///to be processed.
   509 
   511     ///
   510     ///Returns \c false if there are nodes to be processed
   512     ///Returns \c false if there are nodes
   511     ///in the queue (stack).
   513     ///to be processed in the queue (stack).
       
   514     bool emptyQueue() const { return _stack_head<0; }
   512     bool emptyQueue() const { return _stack_head<0; }
   515 
   513 
   516     ///Returns the number of the nodes to be processed.
   514     ///Returns the number of the nodes to be processed.
   517 
   515 
   518     ///Returns the number of the nodes to be processed in the queue (stack).
   516     ///Returns the number of the nodes to be processed
       
   517     ///in the queue (stack).
   519     int queueSize() const { return _stack_head+1; }
   518     int queueSize() const { return _stack_head+1; }
   520 
   519 
   521     ///Executes the algorithm.
   520     ///Executes the algorithm.
   522 
   521 
   523     ///Executes the algorithm.
   522     ///Executes the algorithm.
   635 
   634 
   636     ///This method runs the %DFS algorithm in order to compute the
   635     ///This method runs the %DFS algorithm in order to compute the
   637     ///%DFS path to each node.
   636     ///%DFS path to each node.
   638     ///
   637     ///
   639     ///The algorithm computes
   638     ///The algorithm computes
   640     ///- the %DFS tree,
   639     ///- the %DFS tree (forest),
   641     ///- the distance of each node from the root in the %DFS tree.
   640     ///- the distance of each node from the root(s) in the %DFS tree.
   642     ///
   641     ///
   643     ///\note <tt>d.run()</tt> is just a shortcut of the following code.
   642     ///\note <tt>d.run()</tt> is just a shortcut of the following code.
   644     ///\code
   643     ///\code
   645     ///  d.init();
   644     ///  d.init();
   646     ///  for (NodeIt n(digraph); n != INVALID; ++n) {
   645     ///  for (NodeIt n(digraph); n != INVALID; ++n) {
   661     }
   660     }
   662 
   661 
   663     ///@}
   662     ///@}
   664 
   663 
   665     ///\name Query Functions
   664     ///\name Query Functions
   666     ///The result of the %DFS algorithm can be obtained using these
   665     ///The results of the DFS algorithm can be obtained using these
   667     ///functions.\n
   666     ///functions.\n
   668     ///Either \ref lemon::Dfs::run() "run()" or \ref lemon::Dfs::start()
   667     ///Either \ref run(Node) "run()" or \ref start() should be called
   669     ///"start()" must be called before using them.
   668     ///before using them.
   670 
   669 
   671     ///@{
   670     ///@{
   672 
   671 
   673     ///The DFS path to a node.
   672     ///The DFS path to a node.
   674 
   673 
   675     ///Returns the DFS path to a node.
   674     ///Returns the DFS path to a node.
   676     ///
   675     ///
   677     ///\warning \c t should be reachable from the root.
   676     ///\warning \c t should be reached from the root(s).
   678     ///
   677     ///
   679     ///\pre Either \ref run() or \ref start() must be called before
   678     ///\pre Either \ref run(Node) "run()" or \ref init()
   680     ///using this function.
   679     ///must be called before using this function.
   681     Path path(Node t) const { return Path(*G, *_pred, t); }
   680     Path path(Node t) const { return Path(*G, *_pred, t); }
   682 
   681 
   683     ///The distance of a node from the root.
   682     ///The distance of a node from the root(s).
   684 
   683 
   685     ///Returns the distance of a node from the root.
   684     ///Returns the distance of a node from the root(s).
   686     ///
   685     ///
   687     ///\warning If node \c v is not reachable from the root, then
   686     ///\warning If node \c v is not reached from the root(s), then
   688     ///the return value of this function is undefined.
   687     ///the return value of this function is undefined.
   689     ///
   688     ///
   690     ///\pre Either \ref run() or \ref start() must be called before
   689     ///\pre Either \ref run(Node) "run()" or \ref init()
   691     ///using this function.
   690     ///must be called before using this function.
   692     int dist(Node v) const { return (*_dist)[v]; }
   691     int dist(Node v) const { return (*_dist)[v]; }
   693 
   692 
   694     ///Returns the 'previous arc' of the %DFS tree for a node.
   693     ///Returns the 'previous arc' of the %DFS tree for a node.
   695 
   694 
   696     ///This function returns the 'previous arc' of the %DFS tree for the
   695     ///This function returns the 'previous arc' of the %DFS tree for the
   697     ///node \c v, i.e. it returns the last arc of a %DFS path from the
   696     ///node \c v, i.e. it returns the last arc of a %DFS path from a
   698     ///root to \c v. It is \c INVALID
   697     ///root to \c v. It is \c INVALID if \c v is not reached from the
   699     ///if \c v is not reachable from the root(s) or if \c v is a root.
   698     ///root(s) or if \c v is a root.
   700     ///
   699     ///
   701     ///The %DFS tree used here is equal to the %DFS tree used in
   700     ///The %DFS tree used here is equal to the %DFS tree used in
   702     ///\ref predNode().
   701     ///\ref predNode().
   703     ///
   702     ///
   704     ///\pre Either \ref run() or \ref start() must be called before using
   703     ///\pre Either \ref run(Node) "run()" or \ref init()
   705     ///this function.
   704     ///must be called before using this function.
   706     Arc predArc(Node v) const { return (*_pred)[v];}
   705     Arc predArc(Node v) const { return (*_pred)[v];}
   707 
   706 
   708     ///Returns the 'previous node' of the %DFS tree.
   707     ///Returns the 'previous node' of the %DFS tree.
   709 
   708 
   710     ///This function returns the 'previous node' of the %DFS
   709     ///This function returns the 'previous node' of the %DFS
   711     ///tree for the node \c v, i.e. it returns the last but one node
   710     ///tree for the node \c v, i.e. it returns the last but one node
   712     ///from a %DFS path from the root to \c v. It is \c INVALID
   711     ///from a %DFS path from a root to \c v. It is \c INVALID
   713     ///if \c v is not reachable from the root(s) or if \c v is a root.
   712     ///if \c v is not reached from the root(s) or if \c v is a root.
   714     ///
   713     ///
   715     ///The %DFS tree used here is equal to the %DFS tree used in
   714     ///The %DFS tree used here is equal to the %DFS tree used in
   716     ///\ref predArc().
   715     ///\ref predArc().
   717     ///
   716     ///
   718     ///\pre Either \ref run() or \ref start() must be called before
   717     ///\pre Either \ref run(Node) "run()" or \ref init()
   719     ///using this function.
   718     ///must be called before using this function.
   720     Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
   719     Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
   721                                   G->source((*_pred)[v]); }
   720                                   G->source((*_pred)[v]); }
   722 
   721 
   723     ///\brief Returns a const reference to the node map that stores the
   722     ///\brief Returns a const reference to the node map that stores the
   724     ///distances of the nodes.
   723     ///distances of the nodes.
   725     ///
   724     ///
   726     ///Returns a const reference to the node map that stores the
   725     ///Returns a const reference to the node map that stores the
   727     ///distances of the nodes calculated by the algorithm.
   726     ///distances of the nodes calculated by the algorithm.
   728     ///
   727     ///
   729     ///\pre Either \ref run() or \ref init()
   728     ///\pre Either \ref run(Node) "run()" or \ref init()
   730     ///must be called before using this function.
   729     ///must be called before using this function.
   731     const DistMap &distMap() const { return *_dist;}
   730     const DistMap &distMap() const { return *_dist;}
   732 
   731 
   733     ///\brief Returns a const reference to the node map that stores the
   732     ///\brief Returns a const reference to the node map that stores the
   734     ///predecessor arcs.
   733     ///predecessor arcs.
   735     ///
   734     ///
   736     ///Returns a const reference to the node map that stores the predecessor
   735     ///Returns a const reference to the node map that stores the predecessor
   737     ///arcs, which form the DFS tree.
   736     ///arcs, which form the DFS tree.
   738     ///
   737     ///
   739     ///\pre Either \ref run() or \ref init()
   738     ///\pre Either \ref run(Node) "run()" or \ref init()
   740     ///must be called before using this function.
   739     ///must be called before using this function.
   741     const PredMap &predMap() const { return *_pred;}
   740     const PredMap &predMap() const { return *_pred;}
   742 
   741 
   743     ///Checks if a node is reachable from the root(s).
   742     ///Checks if a node is reached from the root(s).
   744 
   743 
   745     ///Returns \c true if \c v is reachable from the root(s).
   744     ///Returns \c true if \c v is reached from the root(s).
   746     ///\pre Either \ref run() or \ref start()
   745     ///
       
   746     ///\pre Either \ref run(Node) "run()" or \ref init()
   747     ///must be called before using this function.
   747     ///must be called before using this function.
   748     bool reached(Node v) const { return (*_reached)[v]; }
   748     bool reached(Node v) const { return (*_reached)[v]; }
   749 
   749 
   750     ///@}
   750     ///@}
   751   };
   751   };
   887 
   887 
   888   /// Auxiliary class for the function-type interface of DFS algorithm.
   888   /// Auxiliary class for the function-type interface of DFS algorithm.
   889 
   889 
   890   /// This auxiliary class is created to implement the
   890   /// This auxiliary class is created to implement the
   891   /// \ref dfs() "function-type interface" of \ref Dfs algorithm.
   891   /// \ref dfs() "function-type interface" of \ref Dfs algorithm.
   892   /// It does not have own \ref run() method, it uses the functions
   892   /// It does not have own \ref run(Node) "run()" method, it uses the
   893   /// and features of the plain \ref Dfs.
   893   /// functions and features of the plain \ref Dfs.
   894   ///
   894   ///
   895   /// This class should only be used through the \ref dfs() function,
   895   /// This class should only be used through the \ref dfs() function,
   896   /// which makes it easier to use the algorithm.
   896   /// which makes it easier to use the algorithm.
   897   template<class TR>
   897   template<class TR>
   898   class DfsWizard : public TR
   898   class DfsWizard : public TR
  1108   ///  dfs(g).predMap(preds).distMap(dists).run(s);
  1108   ///  dfs(g).predMap(preds).distMap(dists).run(s);
  1109   ///
  1109   ///
  1110   ///  // Compute the DFS path from s to t
  1110   ///  // Compute the DFS path from s to t
  1111   ///  bool reached = dfs(g).path(p).dist(d).run(s,t);
  1111   ///  bool reached = dfs(g).path(p).dist(d).run(s,t);
  1112   ///\endcode
  1112   ///\endcode
  1113 
  1113   ///\warning Don't forget to put the \ref DfsWizard::run(Node) "run()"
  1114   ///\warning Don't forget to put the \ref DfsWizard::run() "run()"
       
  1115   ///to the end of the parameter list.
  1114   ///to the end of the parameter list.
  1116   ///\sa DfsWizard
  1115   ///\sa DfsWizard
  1117   ///\sa Dfs
  1116   ///\sa Dfs
  1118   template<class GR>
  1117   template<class GR>
  1119   DfsWizard<DfsWizardBase<GR> >
  1118   DfsWizard<DfsWizardBase<GR> >
  1307 
  1306 
  1308   public:
  1307   public:
  1309 
  1308 
  1310     typedef DfsVisit Create;
  1309     typedef DfsVisit Create;
  1311 
  1310 
  1312     /// \name Named template parameters
  1311     /// \name Named Template Parameters
  1313 
  1312 
  1314     ///@{
  1313     ///@{
  1315     template <class T>
  1314     template <class T>
  1316     struct SetReachedMapTraits : public Traits {
  1315     struct SetReachedMapTraits : public Traits {
  1317       typedef T ReachedMap;
  1316       typedef T ReachedMap;
  1349     }
  1348     }
  1350 
  1349 
  1351     /// \brief Sets the map that indicates which nodes are reached.
  1350     /// \brief Sets the map that indicates which nodes are reached.
  1352     ///
  1351     ///
  1353     /// Sets the map that indicates which nodes are reached.
  1352     /// Sets the map that indicates which nodes are reached.
  1354     /// If you don't use this function before calling \ref run(),
  1353     /// If you don't use this function before calling \ref run(Node) "run()"
  1355     /// it will allocate one. The destructor deallocates this
  1354     /// or \ref init(), an instance will be allocated automatically.
  1356     /// automatically allocated map, of course.
  1355     /// The destructor deallocates this automatically allocated map,
       
  1356     /// of course.
  1357     /// \return <tt> (*this) </tt>
  1357     /// \return <tt> (*this) </tt>
  1358     DfsVisit &reachedMap(ReachedMap &m) {
  1358     DfsVisit &reachedMap(ReachedMap &m) {
  1359       if(local_reached) {
  1359       if(local_reached) {
  1360         delete _reached;
  1360         delete _reached;
  1361         local_reached=false;
  1361         local_reached=false;
  1364       return *this;
  1364       return *this;
  1365     }
  1365     }
  1366 
  1366 
  1367   public:
  1367   public:
  1368 
  1368 
  1369     /// \name Execution control
  1369     /// \name Execution Control
  1370     /// The simplest way to execute the algorithm is to use
  1370     /// The simplest way to execute the DFS algorithm is to use one of the
  1371     /// one of the member functions called \ref lemon::DfsVisit::run()
  1371     /// member functions called \ref run(Node) "run()".\n
  1372     /// "run()".
  1372     /// If you need more control on the execution, first you have to call
  1373     /// \n
  1373     /// \ref init(), then you can add a source node with \ref addSource()
  1374     /// If you need more control on the execution, first you must call
  1374     /// and perform the actual computation with \ref start().
  1375     /// \ref lemon::DfsVisit::init() "init()", then you can add several
  1375     /// This procedure can be repeated if there are nodes that have not
  1376     /// source nodes with \ref lemon::DfsVisit::addSource() "addSource()".
  1376     /// been reached.
  1377     /// Finally \ref lemon::DfsVisit::start() "start()" will perform the
       
  1378     /// actual path computation.
       
  1379 
  1377 
  1380     /// @{
  1378     /// @{
  1381 
  1379 
  1382     /// \brief Initializes the internal data structures.
  1380     /// \brief Initializes the internal data structures.
  1383     ///
  1381     ///
  1389       for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
  1387       for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
  1390         _reached->set(u, false);
  1388         _reached->set(u, false);
  1391       }
  1389       }
  1392     }
  1390     }
  1393 
  1391 
  1394     ///Adds a new source node.
  1392     /// \brief Adds a new source node.
  1395 
  1393     ///
  1396     ///Adds a new source node to the set of nodes to be processed.
  1394     /// Adds a new source node to the set of nodes to be processed.
  1397     ///
  1395     ///
  1398     ///\pre The stack must be empty. (Otherwise the algorithm gives
  1396     /// \pre The stack must be empty. Otherwise the algorithm gives
  1399     ///false results.)
  1397     /// wrong results. (One of the outgoing arcs of all the source nodes
  1400     ///
  1398     /// except for the last one will not be visited and distances will
  1401     ///\warning Distances will be wrong (or at least strange) in case of
  1399     /// also be wrong.)
  1402     ///multiple sources.
       
  1403     void addSource(Node s)
  1400     void addSource(Node s)
  1404     {
  1401     {
  1405       LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
  1402       LEMON_DEBUG(emptyQueue(), "The stack is not empty.");
  1406       if(!(*_reached)[s]) {
  1403       if(!(*_reached)[s]) {
  1407           _reached->set(s,true);
  1404           _reached->set(s,true);
  1587 
  1584 
  1588     /// This method runs the %DFS algorithm in order to
  1585     /// This method runs the %DFS algorithm in order to
  1589     /// compute the %DFS path to each node.
  1586     /// compute the %DFS path to each node.
  1590     ///
  1587     ///
  1591     /// The algorithm computes
  1588     /// The algorithm computes
  1592     /// - the %DFS tree,
  1589     /// - the %DFS tree (forest),
  1593     /// - the distance of each node from the root in the %DFS tree.
  1590     /// - the distance of each node from the root(s) in the %DFS tree.
  1594     ///
  1591     ///
  1595     /// \note <tt>d.run()</tt> is just a shortcut of the following code.
  1592     /// \note <tt>d.run()</tt> is just a shortcut of the following code.
  1596     ///\code
  1593     ///\code
  1597     ///   d.init();
  1594     ///   d.init();
  1598     ///   for (NodeIt n(digraph); n != INVALID; ++n) {
  1595     ///   for (NodeIt n(digraph); n != INVALID; ++n) {
  1613     }
  1610     }
  1614 
  1611 
  1615     ///@}
  1612     ///@}
  1616 
  1613 
  1617     /// \name Query Functions
  1614     /// \name Query Functions
  1618     /// The result of the %DFS algorithm can be obtained using these
  1615     /// The results of the DFS algorithm can be obtained using these
  1619     /// functions.\n
  1616     /// functions.\n
  1620     /// Either \ref lemon::DfsVisit::run() "run()" or
  1617     /// Either \ref run(Node) "run()" or \ref start() should be called
  1621     /// \ref lemon::DfsVisit::start() "start()" must be called before
  1618     /// before using them.
  1622     /// using them.
  1619 
  1623     ///@{
  1620     ///@{
  1624 
  1621 
  1625     /// \brief Checks if a node is reachable from the root(s).
  1622     /// \brief Checks if a node is reached from the root(s).
  1626     ///
  1623     ///
  1627     /// Returns \c true if \c v is reachable from the root(s).
  1624     /// Returns \c true if \c v is reached from the root(s).
  1628     /// \pre Either \ref run() or \ref start()
  1625     ///
       
  1626     /// \pre Either \ref run(Node) "run()" or \ref init()
  1629     /// must be called before using this function.
  1627     /// must be called before using this function.
  1630     bool reached(Node v) { return (*_reached)[v]; }
  1628     bool reached(Node v) { return (*_reached)[v]; }
  1631 
  1629 
  1632     ///@}
  1630     ///@}
  1633 
  1631