lemon/dfs.h
changeset 947 cfdbf1574403
parent 937 17e36e175725
parent 877 141f9c0db4a3
child 944 02c93d1f00d7
child 964 7fdaa05a69a1
equal deleted inserted replaced
39:9cb5df99ef3d 40:3cea1518b40e
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library.
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     4  *
     5  * Copyright (C) 2003-2009
     5  * Copyright (C) 2003-2010
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     8  *
     9  * Permission to use, modify and distribute this software is granted
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    10  * provided that this copyright notice appears in all copies. For
    45     ///\brief The type of the map that stores the predecessor
    45     ///\brief The type of the map that stores the predecessor
    46     ///arcs of the %DFS paths.
    46     ///arcs of the %DFS paths.
    47     ///
    47     ///
    48     ///The type of the map that stores the predecessor
    48     ///The type of the map that stores the predecessor
    49     ///arcs of the %DFS paths.
    49     ///arcs of the %DFS paths.
    50     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    50     ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
    51     typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
    51     typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
    52     ///Instantiates a \c PredMap.
    52     ///Instantiates a \c PredMap.
    53 
    53 
    54     ///This function instantiates a \ref PredMap.
    54     ///This function instantiates a \ref PredMap.
    55     ///\param g is the digraph, to which we would like to define the
    55     ///\param g is the digraph, to which we would like to define the
    60     }
    60     }
    61 
    61 
    62     ///The type of the map that indicates which nodes are processed.
    62     ///The type of the map that indicates which nodes are processed.
    63 
    63 
    64     ///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 meet the \ref concepts::WriteMap "WriteMap" concept.
    65     ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
       
    66     ///By default, it is a NullMap.
    66     typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
    67     typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
    67     ///Instantiates a \c ProcessedMap.
    68     ///Instantiates a \c ProcessedMap.
    68 
    69 
    69     ///This function instantiates a \ref ProcessedMap.
    70     ///This function instantiates a \ref ProcessedMap.
    70     ///\param g is the digraph, to which
    71     ///\param g is the digraph, to which
    79     }
    80     }
    80 
    81 
    81     ///The type of the map that indicates which nodes are reached.
    82     ///The type of the map that indicates which nodes are reached.
    82 
    83 
    83     ///The type of the map that indicates which nodes are reached.
    84     ///The type of the map that indicates which nodes are reached.
    84     ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
    85     ///It must conform to
       
    86     ///the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
    85     typedef typename Digraph::template NodeMap<bool> ReachedMap;
    87     typedef typename Digraph::template NodeMap<bool> ReachedMap;
    86     ///Instantiates a \c ReachedMap.
    88     ///Instantiates a \c ReachedMap.
    87 
    89 
    88     ///This function instantiates a \ref ReachedMap.
    90     ///This function instantiates a \ref ReachedMap.
    89     ///\param g is the digraph, to which
    91     ///\param g is the digraph, to which
    94     }
    96     }
    95 
    97 
    96     ///The type of the map that stores the distances of the nodes.
    98     ///The type of the map that stores the distances of the nodes.
    97 
    99 
    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.
    99     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   101     ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
   100     typedef typename Digraph::template NodeMap<int> DistMap;
   102     typedef typename Digraph::template NodeMap<int> DistMap;
   101     ///Instantiates a \c DistMap.
   103     ///Instantiates a \c DistMap.
   102 
   104 
   103     ///This function instantiates a \ref DistMap.
   105     ///This function instantiates a \ref DistMap.
   104     ///\param g is the digraph, to which we would like to define the
   106     ///\param g is the digraph, to which we would like to define the
   118   ///algorithm, which is convenient in the simplier cases and it can be
   120   ///algorithm, which is convenient in the simplier cases and it can be
   119   ///used easier.
   121   ///used easier.
   120   ///
   122   ///
   121   ///\tparam GR The type of the digraph the algorithm runs on.
   123   ///\tparam GR The type of the digraph the algorithm runs on.
   122   ///The default type is \ref ListDigraph.
   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.
   123 #ifdef DOXYGEN
   130 #ifdef DOXYGEN
   124   template <typename GR,
   131   template <typename GR,
   125             typename TR>
   132             typename TR>
   126 #else
   133 #else
   127   template <typename GR=ListDigraph,
   134   template <typename GR=ListDigraph,
   222     ///\brief \ref named-templ-param "Named parameter" for setting
   229     ///\brief \ref named-templ-param "Named parameter" for setting
   223     ///\c PredMap type.
   230     ///\c PredMap type.
   224     ///
   231     ///
   225     ///\ref named-templ-param "Named parameter" for setting
   232     ///\ref named-templ-param "Named parameter" for setting
   226     ///\c PredMap type.
   233     ///\c PredMap type.
   227     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   234     ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
   228     template <class T>
   235     template <class T>
   229     struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > {
   236     struct SetPredMap : public Dfs<Digraph, SetPredMapTraits<T> > {
   230       typedef Dfs<Digraph, SetPredMapTraits<T> > Create;
   237       typedef Dfs<Digraph, SetPredMapTraits<T> > Create;
   231     };
   238     };
   232 
   239 
   242     ///\brief \ref named-templ-param "Named parameter" for setting
   249     ///\brief \ref named-templ-param "Named parameter" for setting
   243     ///\c DistMap type.
   250     ///\c DistMap type.
   244     ///
   251     ///
   245     ///\ref named-templ-param "Named parameter" for setting
   252     ///\ref named-templ-param "Named parameter" for setting
   246     ///\c DistMap type.
   253     ///\c DistMap type.
   247     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   254     ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
   248     template <class T>
   255     template <class T>
   249     struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > {
   256     struct SetDistMap : public Dfs< Digraph, SetDistMapTraits<T> > {
   250       typedef Dfs<Digraph, SetDistMapTraits<T> > Create;
   257       typedef Dfs<Digraph, SetDistMapTraits<T> > Create;
   251     };
   258     };
   252 
   259 
   262     ///\brief \ref named-templ-param "Named parameter" for setting
   269     ///\brief \ref named-templ-param "Named parameter" for setting
   263     ///\c ReachedMap type.
   270     ///\c ReachedMap type.
   264     ///
   271     ///
   265     ///\ref named-templ-param "Named parameter" for setting
   272     ///\ref named-templ-param "Named parameter" for setting
   266     ///\c ReachedMap type.
   273     ///\c ReachedMap type.
   267     ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
   274     ///It must conform to
       
   275     ///the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
   268     template <class T>
   276     template <class T>
   269     struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
   277     struct SetReachedMap : public Dfs< Digraph, SetReachedMapTraits<T> > {
   270       typedef Dfs< Digraph, SetReachedMapTraits<T> > Create;
   278       typedef Dfs< Digraph, SetReachedMapTraits<T> > Create;
   271     };
   279     };
   272 
   280 
   282     ///\brief \ref named-templ-param "Named parameter" for setting
   290     ///\brief \ref named-templ-param "Named parameter" for setting
   283     ///\c ProcessedMap type.
   291     ///\c ProcessedMap type.
   284     ///
   292     ///
   285     ///\ref named-templ-param "Named parameter" for setting
   293     ///\ref named-templ-param "Named parameter" for setting
   286     ///\c ProcessedMap type.
   294     ///\c ProcessedMap type.
   287     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   295     ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
   288     template <class T>
   296     template <class T>
   289     struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
   297     struct SetProcessedMap : public Dfs< Digraph, SetProcessedMapTraits<T> > {
   290       typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create;
   298       typedef Dfs< Digraph, SetProcessedMapTraits<T> > Create;
   291     };
   299     };
   292 
   300 
   409   public:
   417   public:
   410 
   418 
   411     ///\name Execution Control
   419     ///\name Execution Control
   412     ///The simplest way to execute the DFS algorithm is to use one of the
   420     ///The simplest way to execute the DFS algorithm is to use one of the
   413     ///member functions called \ref run(Node) "run()".\n
   421     ///member functions called \ref run(Node) "run()".\n
   414     ///If you need more control on the execution, first you have to call
   422     ///If you need better control on the execution, you have to call
   415     ///\ref init(), then you can add a source node with \ref addSource()
   423     ///\ref init() first, then you can add a source node with \ref addSource()
   416     ///and perform the actual computation with \ref start().
   424     ///and perform the actual computation with \ref start().
   417     ///This procedure can be repeated if there are nodes that have not
   425     ///This procedure can be repeated if there are nodes that have not
   418     ///been reached.
   426     ///been reached.
   419 
   427 
   420     ///@{
   428     ///@{
   630       return reached(t);
   638       return reached(t);
   631     }
   639     }
   632 
   640 
   633     ///Runs the algorithm to visit all nodes in the digraph.
   641     ///Runs the algorithm to visit all nodes in the digraph.
   634 
   642 
   635     ///This method runs the %DFS algorithm in order to compute the
   643     ///This method runs the %DFS algorithm in order to visit all nodes
   636     ///%DFS path to each node.
   644     ///in the digraph.
   637     ///
       
   638     ///The algorithm computes
       
   639     ///- the %DFS tree (forest),
       
   640     ///- the distance of each node from the root(s) in the %DFS tree.
       
   641     ///
   645     ///
   642     ///\note <tt>d.run()</tt> is just a shortcut of the following code.
   646     ///\note <tt>d.run()</tt> is just a shortcut of the following code.
   643     ///\code
   647     ///\code
   644     ///  d.init();
   648     ///  d.init();
   645     ///  for (NodeIt n(digraph); n != INVALID; ++n) {
   649     ///  for (NodeIt n(digraph); n != INVALID; ++n) {
   667     ///Either \ref run(Node) "run()" or \ref start() should be called
   671     ///Either \ref run(Node) "run()" or \ref start() should be called
   668     ///before using them.
   672     ///before using them.
   669 
   673 
   670     ///@{
   674     ///@{
   671 
   675 
   672     ///The DFS path to a node.
   676     ///The DFS path to the given node.
   673 
   677 
   674     ///Returns the DFS path to a node.
   678     ///Returns the DFS path to the given node from the root(s).
   675     ///
   679     ///
   676     ///\warning \c t should be reached from the root(s).
   680     ///\warning \c t should be reached from the root(s).
   677     ///
   681     ///
   678     ///\pre Either \ref run(Node) "run()" or \ref init()
   682     ///\pre Either \ref run(Node) "run()" or \ref init()
   679     ///must be called before using this function.
   683     ///must be called before using this function.
   680     Path path(Node t) const { return Path(*G, *_pred, t); }
   684     Path path(Node t) const { return Path(*G, *_pred, t); }
   681 
   685 
   682     ///The distance of a node from the root(s).
   686     ///The distance of the given node from the root(s).
   683 
   687 
   684     ///Returns the distance of a node from the root(s).
   688     ///Returns the distance of the given node from the root(s).
   685     ///
   689     ///
   686     ///\warning If node \c v is not reached from the root(s), then
   690     ///\warning If node \c v is not reached from the root(s), then
   687     ///the return value of this function is undefined.
   691     ///the return value of this function is undefined.
   688     ///
   692     ///
   689     ///\pre Either \ref run(Node) "run()" or \ref init()
   693     ///\pre Either \ref run(Node) "run()" or \ref init()
   690     ///must be called before using this function.
   694     ///must be called before using this function.
   691     int dist(Node v) const { return (*_dist)[v]; }
   695     int dist(Node v) const { return (*_dist)[v]; }
   692 
   696 
   693     ///Returns the 'previous arc' of the %DFS tree for a node.
   697     ///Returns the 'previous arc' of the %DFS tree for the given node.
   694 
   698 
   695     ///This function returns the 'previous arc' of the %DFS tree for the
   699     ///This function returns the 'previous arc' of the %DFS tree for the
   696     ///node \c v, i.e. it returns the last arc of a %DFS path from a
   700     ///node \c v, i.e. it returns the last arc of a %DFS path from a
   697     ///root to \c v. It is \c INVALID if \c v is not reached from the
   701     ///root to \c v. It is \c INVALID if \c v is not reached from the
   698     ///root(s) or if \c v is a root.
   702     ///root(s) or if \c v is a root.
   699     ///
   703     ///
   700     ///The %DFS tree used here is equal to the %DFS tree used in
   704     ///The %DFS tree used here is equal to the %DFS tree used in
   701     ///\ref predNode().
   705     ///\ref predNode() and \ref predMap().
   702     ///
   706     ///
   703     ///\pre Either \ref run(Node) "run()" or \ref init()
   707     ///\pre Either \ref run(Node) "run()" or \ref init()
   704     ///must be called before using this function.
   708     ///must be called before using this function.
   705     Arc predArc(Node v) const { return (*_pred)[v];}
   709     Arc predArc(Node v) const { return (*_pred)[v];}
   706 
   710 
   707     ///Returns the 'previous node' of the %DFS tree.
   711     ///Returns the 'previous node' of the %DFS tree for the given node.
   708 
   712 
   709     ///This function returns the 'previous node' of the %DFS
   713     ///This function returns the 'previous node' of the %DFS
   710     ///tree for the node \c v, i.e. it returns the last but one node
   714     ///tree for the node \c v, i.e. it returns the last but one node
   711     ///from a %DFS path from a root to \c v. It is \c INVALID
   715     ///of a %DFS path from a root to \c v. It is \c INVALID
   712     ///if \c v is not reached from the root(s) or if \c v is a root.
   716     ///if \c v is not reached from the root(s) or if \c v is a root.
   713     ///
   717     ///
   714     ///The %DFS tree used here is equal to the %DFS tree used in
   718     ///The %DFS tree used here is equal to the %DFS tree used in
   715     ///\ref predArc().
   719     ///\ref predArc() and \ref predMap().
   716     ///
   720     ///
   717     ///\pre Either \ref run(Node) "run()" or \ref init()
   721     ///\pre Either \ref run(Node) "run()" or \ref init()
   718     ///must be called before using this function.
   722     ///must be called before using this function.
   719     Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
   723     Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
   720                                   G->source((*_pred)[v]); }
   724                                   G->source((*_pred)[v]); }
   731 
   735 
   732     ///\brief Returns a const reference to the node map that stores the
   736     ///\brief Returns a const reference to the node map that stores the
   733     ///predecessor arcs.
   737     ///predecessor arcs.
   734     ///
   738     ///
   735     ///Returns a const reference to the node map that stores the predecessor
   739     ///Returns a const reference to the node map that stores the predecessor
   736     ///arcs, which form the DFS tree.
   740     ///arcs, which form the DFS tree (forest).
   737     ///
   741     ///
   738     ///\pre Either \ref run(Node) "run()" or \ref init()
   742     ///\pre Either \ref run(Node) "run()" or \ref init()
   739     ///must be called before using this function.
   743     ///must be called before using this function.
   740     const PredMap &predMap() const { return *_pred;}
   744     const PredMap &predMap() const { return *_pred;}
   741 
   745 
   742     ///Checks if a node is reached from the root(s).
   746     ///Checks if the given node. node is reached from the root(s).
   743 
   747 
   744     ///Returns \c true if \c v is reached from the root(s).
   748     ///Returns \c true if \c v is reached from the root(s).
   745     ///
   749     ///
   746     ///\pre Either \ref run(Node) "run()" or \ref init()
   750     ///\pre Either \ref run(Node) "run()" or \ref init()
   747     ///must be called before using this function.
   751     ///must be called before using this function.
   763     ///\brief The type of the map that stores the predecessor
   767     ///\brief The type of the map that stores the predecessor
   764     ///arcs of the %DFS paths.
   768     ///arcs of the %DFS paths.
   765     ///
   769     ///
   766     ///The type of the map that stores the predecessor
   770     ///The type of the map that stores the predecessor
   767     ///arcs of the %DFS paths.
   771     ///arcs of the %DFS paths.
   768     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   772     ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
   769     typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
   773     typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
   770     ///Instantiates a PredMap.
   774     ///Instantiates a PredMap.
   771 
   775 
   772     ///This function instantiates a PredMap.
   776     ///This function instantiates a PredMap.
   773     ///\param g is the digraph, to which we would like to define the
   777     ///\param g is the digraph, to which we would like to define the
   778     }
   782     }
   779 
   783 
   780     ///The type of the map that indicates which nodes are processed.
   784     ///The type of the map that indicates which nodes are processed.
   781 
   785 
   782     ///The type of the map that indicates which nodes are processed.
   786     ///The type of the map that indicates which nodes are processed.
   783     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   787     ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
   784     ///By default it is a NullMap.
   788     ///By default, it is a NullMap.
   785     typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
   789     typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
   786     ///Instantiates a ProcessedMap.
   790     ///Instantiates a ProcessedMap.
   787 
   791 
   788     ///This function instantiates a ProcessedMap.
   792     ///This function instantiates a ProcessedMap.
   789     ///\param g is the digraph, to which
   793     ///\param g is the digraph, to which
   798     }
   802     }
   799 
   803 
   800     ///The type of the map that indicates which nodes are reached.
   804     ///The type of the map that indicates which nodes are reached.
   801 
   805 
   802     ///The type of the map that indicates which nodes are reached.
   806     ///The type of the map that indicates which nodes are reached.
   803     ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
   807     ///It must conform to
       
   808     ///the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
   804     typedef typename Digraph::template NodeMap<bool> ReachedMap;
   809     typedef typename Digraph::template NodeMap<bool> ReachedMap;
   805     ///Instantiates a ReachedMap.
   810     ///Instantiates a ReachedMap.
   806 
   811 
   807     ///This function instantiates a ReachedMap.
   812     ///This function instantiates a ReachedMap.
   808     ///\param g is the digraph, to which
   813     ///\param g is the digraph, to which
   813     }
   818     }
   814 
   819 
   815     ///The type of the map that stores the distances of the nodes.
   820     ///The type of the map that stores the distances of the nodes.
   816 
   821 
   817     ///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.
   818     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   823     ///It must conform to the \ref concepts::WriteMap "WriteMap" concept.
   819     typedef typename Digraph::template NodeMap<int> DistMap;
   824     typedef typename Digraph::template NodeMap<int> DistMap;
   820     ///Instantiates a DistMap.
   825     ///Instantiates a DistMap.
   821 
   826 
   822     ///This function instantiates a DistMap.
   827     ///This function instantiates a DistMap.
   823     ///\param g is the digraph, to which we would like to define
   828     ///\param g is the digraph, to which we would like to define
   828     }
   833     }
   829 
   834 
   830     ///The type of the DFS paths.
   835     ///The type of the DFS paths.
   831 
   836 
   832     ///The type of the DFS paths.
   837     ///The type of the DFS paths.
   833     ///It must meet the \ref concepts::Path "Path" concept.
   838     ///It must conform to the \ref concepts::Path "Path" concept.
   834     typedef lemon::Path<Digraph> Path;
   839     typedef lemon::Path<Digraph> Path;
   835   };
   840   };
   836 
   841 
   837   /// Default traits class used by DfsWizard
   842   /// Default traits class used by DfsWizard
   838 
   843 
   839   /// To make it easier to use Dfs algorithm
   844   /// Default traits class used by DfsWizard.
   840   /// we have created a wizard class.
   845   /// \tparam GR The type of the digraph.
   841   /// This \ref DfsWizard class needs default traits,
       
   842   /// as well as the \ref Dfs class.
       
   843   /// The \ref DfsWizardBase is a class to be the default traits of the
       
   844   /// \ref DfsWizard class.
       
   845   template<class GR>
   846   template<class GR>
   846   class DfsWizardBase : public DfsWizardDefaultTraits<GR>
   847   class DfsWizardBase : public DfsWizardDefaultTraits<GR>
   847   {
   848   {
   848 
   849 
   849     typedef DfsWizardDefaultTraits<GR> Base;
   850     typedef DfsWizardDefaultTraits<GR> Base;
   867     int *_di;
   868     int *_di;
   868 
   869 
   869     public:
   870     public:
   870     /// Constructor.
   871     /// Constructor.
   871 
   872 
   872     /// This constructor does not require parameters, therefore it initiates
   873     /// This constructor does not require parameters, it initiates
   873     /// all of the attributes to \c 0.
   874     /// all of the attributes to \c 0.
   874     DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
   875     DfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
   875                       _dist(0), _path(0), _di(0) {}
   876                       _dist(0), _path(0), _di(0) {}
   876 
   877 
   877     /// Constructor.
   878     /// Constructor.
   892   /// It does not have own \ref run(Node) "run()" method, it uses the
   893   /// It does not have own \ref run(Node) "run()" method, it uses the
   893   /// functions and features of the plain \ref Dfs.
   894   /// functions and features of the plain \ref Dfs.
   894   ///
   895   ///
   895   /// This class should only be used through the \ref dfs() function,
   896   /// This class should only be used through the \ref dfs() function,
   896   /// which makes it easier to use the algorithm.
   897   /// which makes it easier to use the algorithm.
       
   898   ///
       
   899   /// \tparam TR The traits class that defines various types used by the
       
   900   /// algorithm.
   897   template<class TR>
   901   template<class TR>
   898   class DfsWizard : public TR
   902   class DfsWizard : public TR
   899   {
   903   {
   900     typedef TR Base;
   904     typedef TR Base;
   901 
   905 
   902     ///The type of the digraph the algorithm runs on.
       
   903     typedef typename TR::Digraph Digraph;
   906     typedef typename TR::Digraph Digraph;
   904 
   907 
   905     typedef typename Digraph::Node Node;
   908     typedef typename Digraph::Node Node;
   906     typedef typename Digraph::NodeIt NodeIt;
   909     typedef typename Digraph::NodeIt NodeIt;
   907     typedef typename Digraph::Arc Arc;
   910     typedef typename Digraph::Arc Arc;
   908     typedef typename Digraph::OutArcIt OutArcIt;
   911     typedef typename Digraph::OutArcIt OutArcIt;
   909 
   912 
   910     ///\brief The type of the map that stores the predecessor
       
   911     ///arcs of the DFS paths.
       
   912     typedef typename TR::PredMap PredMap;
   913     typedef typename TR::PredMap PredMap;
   913     ///\brief The type of the map that stores the distances of the nodes.
       
   914     typedef typename TR::DistMap DistMap;
   914     typedef typename TR::DistMap DistMap;
   915     ///\brief The type of the map that indicates which nodes are reached.
       
   916     typedef typename TR::ReachedMap ReachedMap;
   915     typedef typename TR::ReachedMap ReachedMap;
   917     ///\brief The type of the map that indicates which nodes are processed.
       
   918     typedef typename TR::ProcessedMap ProcessedMap;
   916     typedef typename TR::ProcessedMap ProcessedMap;
   919     ///The type of the DFS paths
       
   920     typedef typename TR::Path Path;
   917     typedef typename TR::Path Path;
   921 
   918 
   922   public:
   919   public:
   923 
   920 
   924     /// Constructor.
   921     /// Constructor.
   984       return alg.reached(t);
   981       return alg.reached(t);
   985       }
   982       }
   986 
   983 
   987     ///Runs DFS algorithm to visit all nodes in the digraph.
   984     ///Runs DFS algorithm to visit all nodes in the digraph.
   988 
   985 
   989     ///This method runs DFS algorithm in order to compute
   986     ///This method runs DFS algorithm in order to visit all nodes
   990     ///the DFS path to each node.
   987     ///in the digraph.
   991     void run()
   988     void run()
   992     {
   989     {
   993       run(INVALID);
   990       run(INVALID);
   994     }
   991     }
   995 
   992 
   997     struct SetPredMapBase : public Base {
   994     struct SetPredMapBase : public Base {
   998       typedef T PredMap;
   995       typedef T PredMap;
   999       static PredMap *createPredMap(const Digraph &) { return 0; };
   996       static PredMap *createPredMap(const Digraph &) { return 0; };
  1000       SetPredMapBase(const TR &b) : TR(b) {}
   997       SetPredMapBase(const TR &b) : TR(b) {}
  1001     };
   998     };
  1002     ///\brief \ref named-func-param "Named parameter"
   999 
  1003     ///for setting PredMap object.
  1000     ///\brief \ref named-templ-param "Named parameter" for setting
  1004     ///
  1001     ///the predecessor map.
  1005     ///\ref named-func-param "Named parameter"
  1002     ///
  1006     ///for setting PredMap object.
  1003     ///\ref named-templ-param "Named parameter" function for setting
       
  1004     ///the map that stores the predecessor arcs of the nodes.
  1007     template<class T>
  1005     template<class T>
  1008     DfsWizard<SetPredMapBase<T> > predMap(const T &t)
  1006     DfsWizard<SetPredMapBase<T> > predMap(const T &t)
  1009     {
  1007     {
  1010       Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
  1008       Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
  1011       return DfsWizard<SetPredMapBase<T> >(*this);
  1009       return DfsWizard<SetPredMapBase<T> >(*this);
  1015     struct SetReachedMapBase : public Base {
  1013     struct SetReachedMapBase : public Base {
  1016       typedef T ReachedMap;
  1014       typedef T ReachedMap;
  1017       static ReachedMap *createReachedMap(const Digraph &) { return 0; };
  1015       static ReachedMap *createReachedMap(const Digraph &) { return 0; };
  1018       SetReachedMapBase(const TR &b) : TR(b) {}
  1016       SetReachedMapBase(const TR &b) : TR(b) {}
  1019     };
  1017     };
  1020     ///\brief \ref named-func-param "Named parameter"
  1018 
  1021     ///for setting ReachedMap object.
  1019     ///\brief \ref named-templ-param "Named parameter" for setting
  1022     ///
  1020     ///the reached map.
  1023     /// \ref named-func-param "Named parameter"
  1021     ///
  1024     ///for setting ReachedMap object.
  1022     ///\ref named-templ-param "Named parameter" function for setting
       
  1023     ///the map that indicates which nodes are reached.
  1025     template<class T>
  1024     template<class T>
  1026     DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
  1025     DfsWizard<SetReachedMapBase<T> > reachedMap(const T &t)
  1027     {
  1026     {
  1028       Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
  1027       Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
  1029       return DfsWizard<SetReachedMapBase<T> >(*this);
  1028       return DfsWizard<SetReachedMapBase<T> >(*this);
  1033     struct SetDistMapBase : public Base {
  1032     struct SetDistMapBase : public Base {
  1034       typedef T DistMap;
  1033       typedef T DistMap;
  1035       static DistMap *createDistMap(const Digraph &) { return 0; };
  1034       static DistMap *createDistMap(const Digraph &) { return 0; };
  1036       SetDistMapBase(const TR &b) : TR(b) {}
  1035       SetDistMapBase(const TR &b) : TR(b) {}
  1037     };
  1036     };
  1038     ///\brief \ref named-func-param "Named parameter"
  1037 
  1039     ///for setting DistMap object.
  1038     ///\brief \ref named-templ-param "Named parameter" for setting
  1040     ///
  1039     ///the distance map.
  1041     /// \ref named-func-param "Named parameter"
  1040     ///
  1042     ///for setting DistMap object.
  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.
  1043     template<class T>
  1044     template<class T>
  1044     DfsWizard<SetDistMapBase<T> > distMap(const T &t)
  1045     DfsWizard<SetDistMapBase<T> > distMap(const T &t)
  1045     {
  1046     {
  1046       Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
  1047       Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
  1047       return DfsWizard<SetDistMapBase<T> >(*this);
  1048       return DfsWizard<SetDistMapBase<T> >(*this);
  1051     struct SetProcessedMapBase : public Base {
  1052     struct SetProcessedMapBase : public Base {
  1052       typedef T ProcessedMap;
  1053       typedef T ProcessedMap;
  1053       static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
  1054       static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
  1054       SetProcessedMapBase(const TR &b) : TR(b) {}
  1055       SetProcessedMapBase(const TR &b) : TR(b) {}
  1055     };
  1056     };
  1056     ///\brief \ref named-func-param "Named parameter"
  1057 
  1057     ///for setting ProcessedMap object.
  1058     ///\brief \ref named-func-param "Named parameter" for setting
  1058     ///
  1059     ///the processed map.
  1059     /// \ref named-func-param "Named parameter"
  1060     ///
  1060     ///for setting ProcessedMap object.
  1061     ///\ref named-templ-param "Named parameter" function for setting
       
  1062     ///the map that indicates which nodes are processed.
  1061     template<class T>
  1063     template<class T>
  1062     DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
  1064     DfsWizard<SetProcessedMapBase<T> > processedMap(const T &t)
  1063     {
  1065     {
  1064       Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
  1066       Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
  1065       return DfsWizard<SetProcessedMapBase<T> >(*this);
  1067       return DfsWizard<SetProcessedMapBase<T> >(*this);
  1206     typedef GR Digraph;
  1208     typedef GR Digraph;
  1207 
  1209 
  1208     /// \brief The type of the map that indicates which nodes are reached.
  1210     /// \brief The type of the map that indicates which nodes are reached.
  1209     ///
  1211     ///
  1210     /// The type of the map that indicates which nodes are reached.
  1212     /// The type of the map that indicates which nodes are reached.
  1211     /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
  1213     /// It must conform to the
       
  1214     /// \ref concepts::ReadWriteMap "ReadWriteMap" concept.
  1212     typedef typename Digraph::template NodeMap<bool> ReachedMap;
  1215     typedef typename Digraph::template NodeMap<bool> ReachedMap;
  1213 
  1216 
  1214     /// \brief Instantiates a ReachedMap.
  1217     /// \brief Instantiates a ReachedMap.
  1215     ///
  1218     ///
  1216     /// This function instantiates a ReachedMap.
  1219     /// This function instantiates a ReachedMap.
  1244   /// it is only passed to \ref DfsVisitDefaultTraits.
  1247   /// it is only passed to \ref DfsVisitDefaultTraits.
  1245   /// \tparam VS The Visitor type that is used by the algorithm.
  1248   /// \tparam VS The Visitor type that is used by the algorithm.
  1246   /// \ref DfsVisitor "DfsVisitor<GR>" is an empty visitor, which
  1249   /// \ref DfsVisitor "DfsVisitor<GR>" is an empty visitor, which
  1247   /// does not observe the DFS events. If you want to observe the DFS
  1250   /// does not observe the DFS events. If you want to observe the DFS
  1248   /// events, you should implement your own visitor class.
  1251   /// events, you should implement your own visitor class.
  1249   /// \tparam TR Traits class to set various data types used by the
  1252   /// \tparam TR The traits class that defines various types used by the
  1250   /// algorithm. The default traits class is
  1253   /// algorithm. By default, it is \ref DfsVisitDefaultTraits
  1251   /// \ref DfsVisitDefaultTraits "DfsVisitDefaultTraits<GR>".
  1254   /// "DfsVisitDefaultTraits<GR>".
  1252   /// See \ref DfsVisitDefaultTraits for the documentation of
  1255   /// In most cases, this parameter should not be set directly,
  1253   /// a DFS visit traits class.
  1256   /// consider to use the named template parameters instead.
  1254 #ifdef DOXYGEN
  1257 #ifdef DOXYGEN
  1255   template <typename GR, typename VS, typename TR>
  1258   template <typename GR, typename VS, typename TR>
  1256 #else
  1259 #else
  1257   template <typename GR = ListDigraph,
  1260   template <typename GR = ListDigraph,
  1258             typename VS = DfsVisitor<GR>,
  1261             typename VS = DfsVisitor<GR>,
  1367   public:
  1370   public:
  1368 
  1371 
  1369     /// \name Execution Control
  1372     /// \name Execution Control
  1370     /// The simplest way to execute the DFS algorithm is to use one of the
  1373     /// The simplest way to execute the DFS algorithm is to use one of the
  1371     /// member functions called \ref run(Node) "run()".\n
  1374     /// member functions called \ref run(Node) "run()".\n
  1372     /// If you need more control on the execution, first you have to call
  1375     /// If you need better control on the execution, you have to call
  1373     /// \ref init(), then you can add a source node with \ref addSource()
  1376     /// \ref init() first, then you can add a source node with \ref addSource()
  1374     /// and perform the actual computation with \ref start().
  1377     /// and perform the actual computation with \ref start().
  1375     /// This procedure can be repeated if there are nodes that have not
  1378     /// This procedure can be repeated if there are nodes that have not
  1376     /// been reached.
  1379     /// been reached.
  1377 
  1380 
  1378     /// @{
  1381     /// @{
  1581       return reached(t);
  1584       return reached(t);
  1582     }
  1585     }
  1583 
  1586 
  1584     /// \brief Runs the algorithm to visit all nodes in the digraph.
  1587     /// \brief Runs the algorithm to visit all nodes in the digraph.
  1585 
  1588 
  1586     /// This method runs the %DFS algorithm in order to
  1589     /// This method runs the %DFS algorithm in order to visit all nodes
  1587     /// compute the %DFS path to each node.
  1590     /// in the digraph.
  1588     ///
       
  1589     /// The algorithm computes
       
  1590     /// - the %DFS tree (forest),
       
  1591     /// - the distance of each node from the root(s) in the %DFS tree.
       
  1592     ///
  1591     ///
  1593     /// \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.
  1594     ///\code
  1593     ///\code
  1595     ///   d.init();
  1594     ///   d.init();
  1596     ///   for (NodeIt n(digraph); n != INVALID; ++n) {
  1595     ///   for (NodeIt n(digraph); n != INVALID; ++n) {
  1618     /// Either \ref run(Node) "run()" or \ref start() should be called
  1617     /// Either \ref run(Node) "run()" or \ref start() should be called
  1619     /// before using them.
  1618     /// before using them.
  1620 
  1619 
  1621     ///@{
  1620     ///@{
  1622 
  1621 
  1623     /// \brief Checks if a node is reached from the root(s).
  1622     /// \brief Checks if the given node is reached from the root(s).
  1624     ///
  1623     ///
  1625     /// Returns \c true if \c v is reached from the root(s).
  1624     /// Returns \c true if \c v is reached from the root(s).
  1626     ///
  1625     ///
  1627     /// \pre Either \ref run(Node) "run()" or \ref init()
  1626     /// \pre Either \ref run(Node) "run()" or \ref init()
  1628     /// must be called before using this function.
  1627     /// must be called before using this function.