lemon/dfs.h
changeset 1497 529e48eb9786
parent 1438 826bdac3525a
child 1516 4aeda8d11d5e
equal deleted inserted replaced
1:364b7160f164 2:983d9b232f44
   497     
   497     
   498     ///Adds a new source node.
   498     ///Adds a new source node.
   499 
   499 
   500     ///Adds a new source node to the set of nodes to be processed.
   500     ///Adds a new source node to the set of nodes to be processed.
   501     ///
   501     ///
   502     ///\bug dist's are wrong (or at least strange) in case of multiple sources.
   502     ///\bug dists are wrong (or at least strange) in case of multiple sources.
   503     void addSource(Node s)
   503     void addSource(Node s)
   504     {
   504     {
   505       if(!(*_reached)[s])
   505       if(!(*_reached)[s])
   506 	{
   506 	{
   507 	  _reached->set(s,true);
   507 	  _reached->set(s,true);
   514     
   514     
   515     ///Processes the next node.
   515     ///Processes the next node.
   516 
   516 
   517     ///Processes the next node.
   517     ///Processes the next node.
   518     ///
   518     ///
   519     ///\warning The stack must not be empty!
   519     ///\pre The stack must not be empty!
   520     void processNextEdge()
   520     void processNextEdge()
   521     { 
   521     { 
   522       Node m;
   522       Node m;
   523       Edge e=_stack[_stack_head];
   523       Edge e=_stack[_stack_head];
   524       if(!(*_reached)[m=G->target(e)]) {
   524       if(!(*_reached)[m=G->target(e)]) {
   563     ///This method runs the %DFS algorithm from the root node(s)
   563     ///This method runs the %DFS algorithm from the root node(s)
   564     ///in order to
   564     ///in order to
   565     ///compute the
   565     ///compute the
   566     ///%DFS path to each node. The algorithm computes
   566     ///%DFS path to each node. The algorithm computes
   567     ///- The %DFS tree.
   567     ///- The %DFS tree.
   568     ///- The distance of each node from the root(s).
   568     ///- The distance of each node from the root(s) in the %DFS tree.
   569     ///
   569     ///
   570     void start()
   570     void start()
   571     {
   571     {
   572       while ( !emptyQueue() ) processNextEdge();
   572       while ( !emptyQueue() ) processNextEdge();
   573     }
   573     }
   582     ///This method runs the %DFS algorithm from the root node(s)
   582     ///This method runs the %DFS algorithm from the root node(s)
   583     ///in order to
   583     ///in order to
   584     ///compute the
   584     ///compute the
   585     ///%DFS path to \c dest. The algorithm computes
   585     ///%DFS path to \c dest. The algorithm computes
   586     ///- The %DFS path to \c  dest.
   586     ///- The %DFS path to \c  dest.
   587     ///- The distance of \c dest from the root(s).
   587     ///- The distance of \c dest from the root(s) in the %DFS tree.
   588     ///
   588     ///
   589     void start(Node dest)
   589     void start(Node dest)
   590     {
   590     {
   591       while ( !emptyQueue() && G->target(_stack[_stack_head])!=dest ) 
   591       while ( !emptyQueue() && G->target(_stack[_stack_head])!=dest ) 
   592 	processNextEdge();
   592 	processNextEdge();
   599     ///\pre init() must be called and at least one node should be added
   599     ///\pre init() must be called and at least one node should be added
   600     ///with addSource() before using this function.
   600     ///with addSource() before using this function.
   601     ///
   601     ///
   602     ///\param nm must be a bool (or convertible) edge map. The algorithm
   602     ///\param nm must be a bool (or convertible) edge map. The algorithm
   603     ///will stop when it reaches an edge \c e with <tt>nm[e]==true</tt>.
   603     ///will stop when it reaches an edge \c e with <tt>nm[e]==true</tt>.
       
   604     ///
   604     ///\warning Contrary to \ref Dfs and \ref Dijkstra, \c nm is an edge map,
   605     ///\warning Contrary to \ref Dfs and \ref Dijkstra, \c nm is an edge map,
   605     ///not a node map.
   606     ///not a node map.
   606     template<class NM>
   607     template<class NM>
   607       void start(const NM &nm)
   608       void start(const NM &nm)
   608       {
   609       {
   614     ///This method runs the %DFS algorithm from a root node \c s
   615     ///This method runs the %DFS algorithm from a root node \c s
   615     ///in order to
   616     ///in order to
   616     ///compute the
   617     ///compute the
   617     ///%DFS path to each node. The algorithm computes
   618     ///%DFS path to each node. The algorithm computes
   618     ///- The %DFS tree.
   619     ///- The %DFS tree.
   619     ///- The distance of each node from the root.
   620     ///- The distance of each node from the root in the %DFS tree.
   620     ///
   621     ///
   621     ///\note d.run(s) is just a shortcut of the following code.
   622     ///\note d.run(s) is just a shortcut of the following code.
   622     ///\code
   623     ///\code
   623     ///  d.init();
   624     ///  d.init();
   624     ///  d.addSource(s);
   625     ///  d.addSource(s);
   660     
   661     
   661     ///@{
   662     ///@{
   662 
   663 
   663     ///Copies the path to \c t on the DFS tree into \c p
   664     ///Copies the path to \c t on the DFS tree into \c p
   664     
   665     
   665     ///This function copies the path on the DFS tree to \c t into \c p.
   666     ///This function copies the path to \c t on the DFS tree  into \c p.
   666     ///If \c t is a source itself or unreachable, then it does not
   667     ///If \c t is a source itself or unreachable, then it does not
   667     ///alter \c p.
   668     ///alter \c p.
   668     ///\todo Is this the right way to handle unreachable nodes?
   669     ///\todo Is this the right way to handle unreachable nodes?
   669     ///
   670     ///
   670     ///\return Returns \c true if a path to \c t was actually copied to \c p,
   671     ///\return Returns \c true if a path to \c t was actually copied to \c p,
   898 //       _predNode(0),
   899 //       _predNode(0),
   899       _dist(0), _source(s) {}
   900       _dist(0), _source(s) {}
   900 
   901 
   901   };
   902   };
   902   
   903   
   903   /// A class to make the usage of Dfs algorithm easier
   904   /// A class to make the usage of the Dfs algorithm easier
   904 
   905 
   905   /// This class is created to make it easier to use Dfs algorithm.
   906   /// This class is created to make it easier to use the Dfs algorithm.
   906   /// It uses the functions and features of the plain \ref Dfs,
   907   /// It uses the functions and features of the plain \ref Dfs,
   907   /// but it is much simpler to use it.
   908   /// but it is much simpler to use it.
   908   ///
   909   ///
   909   /// Simplicity means that the way to change the types defined
   910   /// Simplicity means that the way to change the types defined
   910   /// in the traits class is based on functions that returns the new class
   911   /// in the traits class is based on functions that returns the new class
   945     ///edges of the %DFS paths.
   946     ///edges of the %DFS paths.
   946     typedef typename TR::PredMap PredMap;
   947     typedef typename TR::PredMap PredMap;
   947 //     ///\brief The type of the map that stores the last but one
   948 //     ///\brief The type of the map that stores the last but one
   948 //     ///nodes of the %DFS paths.
   949 //     ///nodes of the %DFS paths.
   949 //     typedef typename TR::PredNodeMap PredNodeMap;
   950 //     typedef typename TR::PredNodeMap PredNodeMap;
   950     ///The type of the map that stores the dists of the nodes.
   951     ///The type of the map that stores the distances of the nodes.
   951     typedef typename TR::DistMap DistMap;
   952     typedef typename TR::DistMap DistMap;
   952 
   953 
   953 public:
   954 public:
   954     /// Constructor.
   955     /// Constructor.
   955     DfsWizard() : TR() {}
   956     DfsWizard() : TR() {}