lemon/bfs.h
changeset 244 c30731a37f91
parent 210 81cfc04531e8
child 247 f1158744a112
equal deleted inserted replaced
4:c02f00ff8144 6:0c45546c0b3e
    19 #ifndef LEMON_BFS_H
    19 #ifndef LEMON_BFS_H
    20 #define LEMON_BFS_H
    20 #define LEMON_BFS_H
    21 
    21 
    22 ///\ingroup search
    22 ///\ingroup search
    23 ///\file
    23 ///\file
    24 ///\brief Bfs algorithm.
    24 ///\brief BFS algorithm.
    25 
    25 
    26 #include <lemon/list_graph.h>
    26 #include <lemon/list_graph.h>
    27 #include <lemon/graph_utils.h>
    27 #include <lemon/graph_utils.h>
    28 #include <lemon/bits/path_dump.h>
    28 #include <lemon/bits/path_dump.h>
    29 #include <lemon/bits/invalid.h>
    29 #include <lemon/bits/invalid.h>
    30 #include <lemon/error.h>
    30 #include <lemon/error.h>
    31 #include <lemon/maps.h>
    31 #include <lemon/maps.h>
    32 
    32 
    33 namespace lemon {
    33 namespace lemon {
    34 
    34 
    35 
       
    36 
       
    37   ///Default traits class of Bfs class.
    35   ///Default traits class of Bfs class.
    38 
    36 
    39   ///Default traits class of Bfs class.
    37   ///Default traits class of Bfs class.
    40   ///\tparam GR Digraph type.
    38   ///\tparam GR Digraph type.
    41   template<class GR>
    39   template<class GR>
    42   struct BfsDefaultTraits
    40   struct BfsDefaultTraits
    43   {
    41   {
    44     ///The digraph type the algorithm runs on.
    42     ///The type of the digraph the algorithm runs on.
    45     typedef GR Digraph;
    43     typedef GR Digraph;
    46     ///\brief The type of the map that stores the last
    44 
       
    45     ///\brief The type of the map that stores the predecessor
    47     ///arcs of the shortest paths.
    46     ///arcs of the shortest paths.
    48     ///
    47     ///
    49     ///The type of the map that stores the last
    48     ///The type of the map that stores the predecessor
    50     ///arcs of the shortest paths.
    49     ///arcs of the shortest paths.
    51     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    50     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    52     ///
    51     typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
    53     typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap;
    52     ///Instantiates a \ref PredMap.
    54     ///Instantiates a PredMap.
       
    55 
    53 
    56     ///This function instantiates a \ref PredMap.
    54     ///This function instantiates a \ref PredMap.
    57     ///\param G is the digraph, to which we would like to define the PredMap.
    55     ///\param g is the digraph, to which we would like to define the
       
    56     ///\ref PredMap.
    58     ///\todo The digraph alone may be insufficient to initialize
    57     ///\todo The digraph alone may be insufficient to initialize
    59     static PredMap *createPredMap(const GR &G)
    58     static PredMap *createPredMap(const Digraph &g)
    60     {
    59     {
    61       return new PredMap(G);
    60       return new PredMap(g);
    62     }
    61     }
       
    62 
    63     ///The type of the map that indicates which nodes are processed.
    63     ///The type of the map that indicates which nodes are processed.
    64 
    64 
    65     ///The type of the map that indicates which nodes are processed.
    65     ///The type of the map that indicates which nodes are processed.
    66     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    66     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    67     ///\todo named parameter to set this type, function to read and write.
    67     ///By default it is a NullMap.
    68     typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
    68     typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
    69     ///Instantiates a ProcessedMap.
    69     ///Instantiates a \ref ProcessedMap.
    70 
    70 
    71     ///This function instantiates a \ref ProcessedMap.
    71     ///This function instantiates a \ref ProcessedMap.
    72     ///\param g is the digraph, to which
    72     ///\param g is the digraph, to which
    73     ///we would like to define the \ref ProcessedMap
    73     ///we would like to define the \ref ProcessedMap
    74 #ifdef DOXYGEN
    74 #ifdef DOXYGEN
    75     static ProcessedMap *createProcessedMap(const GR &g)
    75     static ProcessedMap *createProcessedMap(const Digraph &g)
    76 #else
    76 #else
    77     static ProcessedMap *createProcessedMap(const GR &)
    77     static ProcessedMap *createProcessedMap(const Digraph &)
    78 #endif
    78 #endif
    79     {
    79     {
    80       return new ProcessedMap();
    80       return new ProcessedMap();
    81     }
    81     }
       
    82 
    82     ///The type of the map that indicates which nodes are reached.
    83     ///The type of the map that indicates which nodes are reached.
    83 
    84 
    84     ///The type of the map that indicates which nodes are reached.
    85     ///The type of the map that indicates which nodes are reached.
       
    86     ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
       
    87     typedef typename Digraph::template NodeMap<bool> ReachedMap;
       
    88     ///Instantiates a \ref ReachedMap.
       
    89 
       
    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)
       
    94     {
       
    95       return new ReachedMap(g);
       
    96     }
       
    97 
       
    98     ///The type of the map that stores the distances of the nodes.
       
    99 
       
   100     ///The type of the map that stores the distances of the nodes.
    85     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   101     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    86     ///\todo named parameter to set this type, function to read and write.
       
    87     typedef typename Digraph::template NodeMap<bool> ReachedMap;
       
    88     ///Instantiates a ReachedMap.
       
    89 
       
    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 GR &G)
       
    94     {
       
    95       return new ReachedMap(G);
       
    96     }
       
    97     ///The type of the map that stores the dists of the nodes.
       
    98 
       
    99     ///The type of the map that stores the dists of the nodes.
       
   100     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
       
   101     ///
       
   102     typedef typename Digraph::template NodeMap<int> DistMap;
   102     typedef typename Digraph::template NodeMap<int> DistMap;
   103     ///Instantiates a DistMap.
   103     ///Instantiates a \ref DistMap.
   104 
   104 
   105     ///This function instantiates a \ref DistMap.
   105     ///This function instantiates a \ref DistMap.
   106     ///\param G is the digraph, to which we would like to define
   106     ///\param g is the digraph, to which we would like to define the
   107     ///the \ref DistMap
   107     ///\ref DistMap.
   108     static DistMap *createDistMap(const GR &G)
   108     static DistMap *createDistMap(const Digraph &g)
   109     {
   109     {
   110       return new DistMap(G);
   110       return new DistMap(g);
   111     }
   111     }
   112   };
   112   };
   113 
   113 
   114   ///%BFS algorithm class.
   114   ///%BFS algorithm class.
   115 
   115 
   116   ///\ingroup search
   116   ///\ingroup search
   117   ///This class provides an efficient implementation of the %BFS algorithm.
   117   ///This class provides an efficient implementation of the %BFS algorithm.
   118   ///
   118   ///
   119   ///\tparam GR The digraph type the algorithm runs on. The default value is
   119   ///There is also a \ref bfs() "function type interface" for the BFS
   120   ///\ref ListDigraph. The value of GR is not used directly by Bfs, it
   120   ///algorithm, which is convenient in the simplier cases and it can be
   121   ///is only passed to \ref BfsDefaultTraits.
   121   ///used easier.
       
   122   ///
       
   123   ///\tparam GR The type of the digraph the algorithm runs on.
       
   124   ///The default value is \ref ListDigraph. The value of GR is not used
       
   125   ///directly by \ref Bfs, it is only passed to \ref BfsDefaultTraits.
   122   ///\tparam TR Traits class to set various data types used by the algorithm.
   126   ///\tparam TR Traits class to set various data types used by the algorithm.
   123   ///The default traits class is
   127   ///The default traits class is
   124   ///\ref BfsDefaultTraits "BfsDefaultTraits<GR>".
   128   ///\ref BfsDefaultTraits "BfsDefaultTraits<GR>".
   125   ///See \ref BfsDefaultTraits for the documentation of
   129   ///See \ref BfsDefaultTraits for the documentation of
   126   ///a Bfs traits class.
   130   ///a Bfs traits class.
   127 
       
   128 #ifdef DOXYGEN
   131 #ifdef DOXYGEN
   129   template <typename GR,
   132   template <typename GR,
   130             typename TR>
   133             typename TR>
   131 #else
   134 #else
   132   template <typename GR=ListDigraph,
   135   template <typename GR=ListDigraph,
   133             typename TR=BfsDefaultTraits<GR> >
   136             typename TR=BfsDefaultTraits<GR> >
   134 #endif
   137 #endif
   135   class Bfs {
   138   class Bfs {
   136   public:
   139   public:
   137     /**
   140     ///\ref Exception for uninitialized parameters.
   138      * \brief \ref Exception for uninitialized parameters.
   141 
   139      *
   142     ///This error represents problems in the initialization of the
   140      * This error represents problems in the initialization
   143     ///parameters of the algorithm.
   141      * of the parameters of the algorithms.
       
   142      */
       
   143     class UninitializedParameter : public lemon::UninitializedParameter {
   144     class UninitializedParameter : public lemon::UninitializedParameter {
   144     public:
   145     public:
   145       virtual const char* what() const throw() {
   146       virtual const char* what() const throw() {
   146         return "lemon::Bfs::UninitializedParameter";
   147         return "lemon::Bfs::UninitializedParameter";
   147       }
   148       }
   148     };
   149     };
   149 
   150 
       
   151     ///The type of the digraph the algorithm runs on.
       
   152     typedef typename TR::Digraph Digraph;
       
   153 
       
   154     ///\brief The type of the map that stores the predecessor arcs of the
       
   155     ///shortest paths.
       
   156     typedef typename TR::PredMap PredMap;
       
   157     ///The type of the map that stores the distances of the nodes.
       
   158     typedef typename TR::DistMap DistMap;
       
   159     ///The type of the map that indicates which nodes are reached.
       
   160     typedef typename TR::ReachedMap ReachedMap;
       
   161     ///The type of the map that indicates which nodes are processed.
       
   162     typedef typename TR::ProcessedMap ProcessedMap;
       
   163     ///The type of the paths.
       
   164     typedef PredMapPath<Digraph, PredMap> Path;
       
   165 
       
   166     ///The traits class.
   150     typedef TR Traits;
   167     typedef TR Traits;
   151     ///The type of the underlying digraph.
   168 
   152     typedef typename TR::Digraph Digraph;
       
   153 
       
   154     ///\brief The type of the map that stores the last
       
   155     ///arcs of the shortest paths.
       
   156     typedef typename TR::PredMap PredMap;
       
   157     ///The type of the map indicating which nodes are reached.
       
   158     typedef typename TR::ReachedMap ReachedMap;
       
   159     ///The type of the map indicating which nodes are processed.
       
   160     typedef typename TR::ProcessedMap ProcessedMap;
       
   161     ///The type of the map that stores the dists of the nodes.
       
   162     typedef typename TR::DistMap DistMap;
       
   163   private:
   169   private:
   164 
   170 
   165     typedef typename Digraph::Node Node;
   171     typedef typename Digraph::Node Node;
   166     typedef typename Digraph::NodeIt NodeIt;
   172     typedef typename Digraph::NodeIt NodeIt;
   167     typedef typename Digraph::Arc Arc;
   173     typedef typename Digraph::Arc Arc;
   168     typedef typename Digraph::OutArcIt OutArcIt;
   174     typedef typename Digraph::OutArcIt OutArcIt;
   169 
   175 
   170     /// Pointer to the underlying digraph.
   176     //Pointer to the underlying digraph.
   171     const Digraph *G;
   177     const Digraph *G;
   172     ///Pointer to the map of predecessors arcs.
   178     //Pointer to the map of predecessor arcs.
   173     PredMap *_pred;
   179     PredMap *_pred;
   174     ///Indicates if \ref _pred is locally allocated (\c true) or not.
   180     //Indicates if _pred is locally allocated (true) or not.
   175     bool local_pred;
   181     bool local_pred;
   176     ///Pointer to the map of distances.
   182     //Pointer to the map of distances.
   177     DistMap *_dist;
   183     DistMap *_dist;
   178     ///Indicates if \ref _dist is locally allocated (\c true) or not.
   184     //Indicates if _dist is locally allocated (true) or not.
   179     bool local_dist;
   185     bool local_dist;
   180     ///Pointer to the map of reached status of the nodes.
   186     //Pointer to the map of reached status of the nodes.
   181     ReachedMap *_reached;
   187     ReachedMap *_reached;
   182     ///Indicates if \ref _reached is locally allocated (\c true) or not.
   188     //Indicates if _reached is locally allocated (true) or not.
   183     bool local_reached;
   189     bool local_reached;
   184     ///Pointer to the map of processed status of the nodes.
   190     //Pointer to the map of processed status of the nodes.
   185     ProcessedMap *_processed;
   191     ProcessedMap *_processed;
   186     ///Indicates if \ref _processed is locally allocated (\c true) or not.
   192     //Indicates if _processed is locally allocated (true) or not.
   187     bool local_processed;
   193     bool local_processed;
   188 
   194 
   189     std::vector<typename Digraph::Node> _queue;
   195     std::vector<typename Digraph::Node> _queue;
   190     int _queue_head,_queue_tail,_queue_next_dist;
   196     int _queue_head,_queue_tail,_queue_next_dist;
   191     int _curr_dist;
   197     int _curr_dist;
   192 
   198 
   193     ///Creates the maps if necessary.
   199     ///Creates the maps if necessary.
   194 
       
   195     ///\todo Better memory allocation (instead of new).
   200     ///\todo Better memory allocation (instead of new).
   196     void create_maps()
   201     void create_maps()
   197     {
   202     {
   198       if(!_pred) {
   203       if(!_pred) {
   199         local_pred = true;
   204         local_pred = true;
   232       {
   237       {
   233         throw UninitializedParameter();
   238         throw UninitializedParameter();
   234       }
   239       }
   235     };
   240     };
   236     ///\brief \ref named-templ-param "Named parameter" for setting
   241     ///\brief \ref named-templ-param "Named parameter" for setting
   237     ///PredMap type
   242     ///\ref PredMap type.
   238     ///
   243     ///
   239     ///\ref named-templ-param "Named parameter" for setting PredMap type
   244     ///\ref named-templ-param "Named parameter" for setting
   240     ///
   245     ///\ref PredMap type.
   241     template <class T>
   246     template <class T>
   242     struct DefPredMap : public Bfs< Digraph, DefPredMapTraits<T> > {
   247     struct DefPredMap : public Bfs< Digraph, DefPredMapTraits<T> > {
   243       typedef Bfs< Digraph, DefPredMapTraits<T> > Create;
   248       typedef Bfs< Digraph, DefPredMapTraits<T> > Create;
   244     };
   249     };
   245 
   250 
   250       {
   255       {
   251         throw UninitializedParameter();
   256         throw UninitializedParameter();
   252       }
   257       }
   253     };
   258     };
   254     ///\brief \ref named-templ-param "Named parameter" for setting
   259     ///\brief \ref named-templ-param "Named parameter" for setting
   255     ///DistMap type
   260     ///\ref DistMap type.
   256     ///
   261     ///
   257     ///\ref named-templ-param "Named parameter" for setting DistMap type
   262     ///\ref named-templ-param "Named parameter" for setting
   258     ///
   263     ///\ref DistMap type.
   259     template <class T>
   264     template <class T>
   260     struct DefDistMap : public Bfs< Digraph, DefDistMapTraits<T> > {
   265     struct DefDistMap : public Bfs< Digraph, DefDistMapTraits<T> > {
   261       typedef Bfs< Digraph, DefDistMapTraits<T> > Create;
   266       typedef Bfs< Digraph, DefDistMapTraits<T> > Create;
   262     };
   267     };
   263 
   268 
   268       {
   273       {
   269         throw UninitializedParameter();
   274         throw UninitializedParameter();
   270       }
   275       }
   271     };
   276     };
   272     ///\brief \ref named-templ-param "Named parameter" for setting
   277     ///\brief \ref named-templ-param "Named parameter" for setting
   273     ///ReachedMap type
   278     ///\ref ReachedMap type.
   274     ///
   279     ///
   275     ///\ref named-templ-param "Named parameter" for setting ReachedMap type
   280     ///\ref named-templ-param "Named parameter" for setting
   276     ///
   281     ///\ref ReachedMap type.
   277     template <class T>
   282     template <class T>
   278     struct DefReachedMap : public Bfs< Digraph, DefReachedMapTraits<T> > {
   283     struct DefReachedMap : public Bfs< Digraph, DefReachedMapTraits<T> > {
   279       typedef Bfs< Digraph, DefReachedMapTraits<T> > Create;
   284       typedef Bfs< Digraph, DefReachedMapTraits<T> > Create;
   280     };
   285     };
   281 
   286 
   286       {
   291       {
   287         throw UninitializedParameter();
   292         throw UninitializedParameter();
   288       }
   293       }
   289     };
   294     };
   290     ///\brief \ref named-templ-param "Named parameter" for setting
   295     ///\brief \ref named-templ-param "Named parameter" for setting
   291     ///ProcessedMap type
   296     ///\ref ProcessedMap type.
   292     ///
   297     ///
   293     ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
   298     ///\ref named-templ-param "Named parameter" for setting
   294     ///
   299     ///\ref ProcessedMap type.
   295     template <class T>
   300     template <class T>
   296     struct DefProcessedMap : public Bfs< Digraph, DefProcessedMapTraits<T> > {
   301     struct DefProcessedMap : public Bfs< Digraph, DefProcessedMapTraits<T> > {
   297       typedef Bfs< Digraph, DefProcessedMapTraits<T> > Create;
   302       typedef Bfs< Digraph, DefProcessedMapTraits<T> > Create;
   298     };
   303     };
   299 
   304 
   300     struct DefDigraphProcessedMapTraits : public Traits {
   305     struct DefDigraphProcessedMapTraits : public Traits {
   301       typedef typename Digraph::template NodeMap<bool> ProcessedMap;
   306       typedef typename Digraph::template NodeMap<bool> ProcessedMap;
   302       static ProcessedMap *createProcessedMap(const Digraph &G)
   307       static ProcessedMap *createProcessedMap(const Digraph &g)
   303       {
   308       {
   304         return new ProcessedMap(G);
   309         return new ProcessedMap(g);
   305       }
   310       }
   306     };
   311     };
   307     ///\brief \ref named-templ-param "Named parameter"
   312     ///\brief \ref named-templ-param "Named parameter" for setting
   308     ///for setting the ProcessedMap type to be Digraph::NodeMap<bool>.
   313     ///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
   309     ///
   314     ///
   310     ///\ref named-templ-param "Named parameter"
   315     ///\ref named-templ-param "Named parameter" for setting
   311     ///for setting the ProcessedMap type to be Digraph::NodeMap<bool>.
   316     ///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
   312     ///If you don't set it explicitly, it will be automatically allocated.
   317     ///If you don't set it explicitly, it will be automatically allocated.
   313     template <class T>
   318     template <class T>
   314     struct DefProcessedMapToBeDefaultMap :
   319     struct DefProcessedMapToBeDefaultMap :
   315       public Bfs< Digraph, DefDigraphProcessedMapTraits> {
   320       public Bfs< Digraph, DefDigraphProcessedMapTraits> {
   316       typedef Bfs< Digraph, DefDigraphProcessedMapTraits> Create;
   321       typedef Bfs< Digraph, DefDigraphProcessedMapTraits> Create;
   320 
   325 
   321   public:
   326   public:
   322 
   327 
   323     ///Constructor.
   328     ///Constructor.
   324 
   329 
   325     ///\param _G the digraph the algorithm will run on.
   330     ///Constructor.
   326     ///
   331     ///\param g The digraph the algorithm runs on.
   327     Bfs(const Digraph& _G) :
   332     Bfs(const Digraph &g) :
   328       G(&_G),
   333       G(&g),
   329       _pred(NULL), local_pred(false),
   334       _pred(NULL), local_pred(false),
   330       _dist(NULL), local_dist(false),
   335       _dist(NULL), local_dist(false),
   331       _reached(NULL), local_reached(false),
   336       _reached(NULL), local_reached(false),
   332       _processed(NULL), local_processed(false)
   337       _processed(NULL), local_processed(false)
   333     { }
   338     { }
   339       if(local_dist) delete _dist;
   344       if(local_dist) delete _dist;
   340       if(local_reached) delete _reached;
   345       if(local_reached) delete _reached;
   341       if(local_processed) delete _processed;
   346       if(local_processed) delete _processed;
   342     }
   347     }
   343 
   348 
   344     ///Sets the map storing the predecessor arcs.
   349     ///Sets the map that stores the predecessor arcs.
   345 
   350 
   346     ///Sets the map storing the predecessor arcs.
   351     ///Sets the map that stores the predecessor arcs.
   347     ///If you don't use this function before calling \ref run(),
   352     ///If you don't use this function before calling \ref run(),
   348     ///it will allocate one. The destructor deallocates this
   353     ///it will allocate one. The destructor deallocates this
   349     ///automatically allocated map, of course.
   354     ///automatically allocated map, of course.
   350     ///\return <tt> (*this) </tt>
   355     ///\return <tt> (*this) </tt>
   351     Bfs &predMap(PredMap &m)
   356     Bfs &predMap(PredMap &m)
   356       }
   361       }
   357       _pred = &m;
   362       _pred = &m;
   358       return *this;
   363       return *this;
   359     }
   364     }
   360 
   365 
   361     ///Sets the map indicating the reached nodes.
   366     ///Sets the map that indicates which nodes are reached.
   362 
   367 
   363     ///Sets the map indicating the reached nodes.
   368     ///Sets the map that indicates which nodes are reached.
   364     ///If you don't use this function before calling \ref run(),
   369     ///If you don't use this function before calling \ref run(),
   365     ///it will allocate one. The destructor deallocates this
   370     ///it will allocate one. The destructor deallocates this
   366     ///automatically allocated map, of course.
   371     ///automatically allocated map, of course.
   367     ///\return <tt> (*this) </tt>
   372     ///\return <tt> (*this) </tt>
   368     Bfs &reachedMap(ReachedMap &m)
   373     Bfs &reachedMap(ReachedMap &m)
   373       }
   378       }
   374       _reached = &m;
   379       _reached = &m;
   375       return *this;
   380       return *this;
   376     }
   381     }
   377 
   382 
   378     ///Sets the map indicating the processed nodes.
   383     ///Sets the map that indicates which nodes are processed.
   379 
   384 
   380     ///Sets the map indicating the processed nodes.
   385     ///Sets the map that indicates which nodes are processed.
   381     ///If you don't use this function before calling \ref run(),
   386     ///If you don't use this function before calling \ref run(),
   382     ///it will allocate one. The destructor deallocates this
   387     ///it will allocate one. The destructor deallocates this
   383     ///automatically allocated map, of course.
   388     ///automatically allocated map, of course.
   384     ///\return <tt> (*this) </tt>
   389     ///\return <tt> (*this) </tt>
   385     Bfs &processedMap(ProcessedMap &m)
   390     Bfs &processedMap(ProcessedMap &m)
   390       }
   395       }
   391       _processed = &m;
   396       _processed = &m;
   392       return *this;
   397       return *this;
   393     }
   398     }
   394 
   399 
   395     ///Sets the map storing the distances calculated by the algorithm.
   400     ///Sets the map that stores the distances of the nodes.
   396 
   401 
   397     ///Sets the map storing the distances calculated by the algorithm.
   402     ///Sets the map that stores the distances of the nodes calculated by
       
   403     ///the algorithm.
   398     ///If you don't use this function before calling \ref run(),
   404     ///If you don't use this function before calling \ref run(),
   399     ///it will allocate one. The destructor deallocates this
   405     ///it will allocate one. The destructor deallocates this
   400     ///automatically allocated map, of course.
   406     ///automatically allocated map, of course.
   401     ///\return <tt> (*this) </tt>
   407     ///\return <tt> (*this) </tt>
   402     Bfs &distMap(DistMap &m)
   408     Bfs &distMap(DistMap &m)
   408       _dist = &m;
   414       _dist = &m;
   409       return *this;
   415       return *this;
   410     }
   416     }
   411 
   417 
   412   public:
   418   public:
       
   419 
   413     ///\name Execution control
   420     ///\name Execution control
   414     ///The simplest way to execute the algorithm is to use
   421     ///The simplest way to execute the algorithm is to use
   415     ///one of the member functions called \c run(...).
   422     ///one of the member functions called \ref lemon::Bfs::run() "run()".
   416     ///\n
   423     ///\n
   417     ///If you need more control on the execution,
   424     ///If you need more control on the execution, first you must call
   418     ///first you must call \ref init(), then you can add several source nodes
   425     ///\ref lemon::Bfs::init() "init()", then you can add several source
   419     ///with \ref addSource().
   426     ///nodes with \ref lemon::Bfs::addSource() "addSource()".
   420     ///Finally \ref start() will perform the actual path
   427     ///Finally \ref lemon::Bfs::start() "start()" will perform the
   421     ///computation.
   428     ///actual path computation.
   422 
   429 
   423     ///@{
   430     ///@{
   424 
   431 
   425     ///\brief Initializes the internal data structures.
   432     ///Initializes the internal data structures.
   426     ///
   433 
   427     ///Initializes the internal data structures.
   434     ///Initializes the internal data structures.
   428     ///
   435     ///
   429     void init()
   436     void init()
   430     {
   437     {
   431       create_maps();
   438       create_maps();
   459 
   466 
   460     ///Processes the next node.
   467     ///Processes the next node.
   461     ///
   468     ///
   462     ///\return The processed node.
   469     ///\return The processed node.
   463     ///
   470     ///
   464     ///\warning The queue must not be empty!
   471     ///\pre The queue must not be empty.
   465     Node processNextNode()
   472     Node processNextNode()
   466     {
   473     {
   467       if(_queue_tail==_queue_next_dist) {
   474       if(_queue_tail==_queue_next_dist) {
   468         _curr_dist++;
   475         _curr_dist++;
   469         _queue_next_dist=_queue_head;
   476         _queue_next_dist=_queue_head;
   481       return n;
   488       return n;
   482     }
   489     }
   483 
   490 
   484     ///Processes the next node.
   491     ///Processes the next node.
   485 
   492 
   486     ///Processes the next node. And checks that the given target node
   493     ///Processes the next node and checks if the given target node
   487     ///is reached. If the target node is reachable from the processed
   494     ///is reached. If the target node is reachable from the processed
   488     ///node then the reached parameter will be set true. The reached
   495     ///node, then the \c reach parameter will be set to \c true.
   489     ///parameter should be initially false.
       
   490     ///
   496     ///
   491     ///\param target The target node.
   497     ///\param target The target node.
   492     ///\retval reach Indicates that the target node is reached.
   498     ///\retval reach Indicates if the target node is reached.
       
   499     ///It should be initially \c false.
       
   500     ///
   493     ///\return The processed node.
   501     ///\return The processed node.
   494     ///
   502     ///
   495     ///\warning The queue must not be empty!
   503     ///\pre The queue must not be empty.
   496     Node processNextNode(Node target, bool& reach)
   504     Node processNextNode(Node target, bool& reach)
   497     {
   505     {
   498       if(_queue_tail==_queue_next_dist) {
   506       if(_queue_tail==_queue_next_dist) {
   499         _curr_dist++;
   507         _curr_dist++;
   500         _queue_next_dist=_queue_head;
   508         _queue_next_dist=_queue_head;
   513       return n;
   521       return n;
   514     }
   522     }
   515 
   523 
   516     ///Processes the next node.
   524     ///Processes the next node.
   517 
   525 
   518     ///Processes the next node. And checks that at least one of
   526     ///Processes the next node and checks if at least one of reached
   519     ///reached node has true value in the \c nm node map. If one node
   527     ///nodes has \c true value in the \c nm node map. If one node
   520     ///with true value is reachable from the processed node then the
   528     ///with \c true value is reachable from the processed node, then the
   521     ///rnode parameter will be set to the first of such nodes.
   529     ///\c rnode parameter will be set to the first of such nodes.
   522     ///
   530     ///
   523     ///\param nm The node map of possible targets.
   531     ///\param nm A \c bool (or convertible) node map that indicates the
       
   532     ///possible targets.
   524     ///\retval rnode The reached target node.
   533     ///\retval rnode The reached target node.
       
   534     ///It should be initially \c INVALID.
       
   535     ///
   525     ///\return The processed node.
   536     ///\return The processed node.
   526     ///
   537     ///
   527     ///\warning The queue must not be empty!
   538     ///\pre The queue must not be empty.
   528     template<class NM>
   539     template<class NM>
   529     Node processNextNode(const NM& nm, Node& rnode)
   540     Node processNextNode(const NM& nm, Node& rnode)
   530     {
   541     {
   531       if(_queue_tail==_queue_next_dist) {
   542       if(_queue_tail==_queue_next_dist) {
   532         _curr_dist++;
   543         _curr_dist++;
   544           if (nm[m] && rnode == INVALID) rnode = m;
   555           if (nm[m] && rnode == INVALID) rnode = m;
   545         }
   556         }
   546       return n;
   557       return n;
   547     }
   558     }
   548 
   559 
   549     ///Next node to be processed.
   560     ///The next node to be processed.
   550 
   561 
   551     ///Next node to be processed.
   562     ///Returns the next node to be processed or \c INVALID if the queue
   552     ///
   563     ///is empty.
   553     ///\return The next node to be processed or INVALID if the queue is
   564     Node nextNode() const
   554     /// empty.
       
   555     Node nextNode()
       
   556     {
   565     {
   557       return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID;
   566       return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID;
   558     }
   567     }
   559 
   568 
   560     ///\brief Returns \c false if there are nodes
   569     ///\brief Returns \c false if there are nodes
   561     ///to be processed in the queue
   570     ///to be processed.
   562     ///
   571     ///
   563     ///Returns \c false if there are nodes
   572     ///Returns \c false if there are nodes
   564     ///to be processed in the queue
   573     ///to be processed in the queue.
   565     bool emptyQueue() { return _queue_tail==_queue_head; }
   574     bool emptyQueue() const { return _queue_tail==_queue_head; }
       
   575 
   566     ///Returns the number of the nodes to be processed.
   576     ///Returns the number of the nodes to be processed.
   567 
   577 
   568     ///Returns the number of the nodes to be processed in the queue.
   578     ///Returns the number of the nodes to be processed in the queue.
   569     int queueSize() { return _queue_head-_queue_tail; }
   579     int queueSize() const { return _queue_head-_queue_tail; }
   570 
   580 
   571     ///Executes the algorithm.
   581     ///Executes the algorithm.
   572 
   582 
   573     ///Executes the algorithm.
   583     ///Executes the algorithm.
   574     ///
   584     ///
   575     ///\pre init() must be called and at least one node should be added
       
   576     ///with addSource() before using this function.
       
   577     ///
       
   578     ///This method runs the %BFS algorithm from the root node(s)
   585     ///This method runs the %BFS algorithm from the root node(s)
   579     ///in order to
   586     ///in order to compute the shortest path to each node.
   580     ///compute the
   587     ///
   581     ///shortest path to each node. The algorithm computes
   588     ///The algorithm computes
   582     ///- The shortest path tree.
   589     ///- the shortest path tree (forest),
   583     ///- The distance of each node from the root(s).
   590     ///- the distance of each node from the root(s).
       
   591     ///
       
   592     ///\pre init() must be called and at least one root node should be
       
   593     ///added with addSource() before using this function.
       
   594     ///
       
   595     ///\note <tt>b.start()</tt> is just a shortcut of the following code.
       
   596     ///\code
       
   597     ///  while ( !b.emptyQueue() ) {
       
   598     ///    b.processNextNode();
       
   599     ///  }
       
   600     ///\endcode
   584     void start()
   601     void start()
   585     {
   602     {
   586       while ( !emptyQueue() ) processNextNode();
   603       while ( !emptyQueue() ) processNextNode();
   587     }
   604     }
   588 
   605 
   589     ///Executes the algorithm until \c dest is reached.
   606     ///Executes the algorithm until the given target node is reached.
   590 
   607 
   591     ///Executes the algorithm until \c dest is reached.
   608     ///Executes the algorithm until the given target node is reached.
   592     ///
       
   593     ///\pre init() must be called and at least one node should be added
       
   594     ///with addSource() before using this function.
       
   595     ///
   609     ///
   596     ///This method runs the %BFS algorithm from the root node(s)
   610     ///This method runs the %BFS algorithm from the root node(s)
   597     ///in order to compute the shortest path to \c dest.
   611     ///in order to compute the shortest path to \c dest.
       
   612     ///
   598     ///The algorithm computes
   613     ///The algorithm computes
   599     ///- The shortest path to \c  dest.
   614     ///- the shortest path to \c dest,
   600     ///- The distance of \c dest from the root(s).
   615     ///- the distance of \c dest from the root(s).
       
   616     ///
       
   617     ///\pre init() must be called and at least one root node should be
       
   618     ///added with addSource() before using this function.
       
   619     ///
       
   620     ///\note <tt>b.start(t)</tt> is just a shortcut of the following code.
       
   621     ///\code
       
   622     ///  bool reach = false;
       
   623     ///  while ( !b.emptyQueue() && !reach ) {
       
   624     ///    b.processNextNode(t, reach);
       
   625     ///  }
       
   626     ///\endcode
   601     void start(Node dest)
   627     void start(Node dest)
   602     {
   628     {
   603       bool reach = false;
   629       bool reach = false;
   604       while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
   630       while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
   605     }
   631     }
   606 
   632 
   607     ///Executes the algorithm until a condition is met.
   633     ///Executes the algorithm until a condition is met.
   608 
   634 
   609     ///Executes the algorithm until a condition is met.
   635     ///Executes the algorithm until a condition is met.
   610     ///
   636     ///
   611     ///\pre init() must be called and at least one node should be added
   637     ///This method runs the %BFS algorithm from the root node(s) in
   612     ///with addSource() before using this function.
   638     ///order to compute the shortest path to a node \c v with
   613     ///
   639     /// <tt>nm[v]</tt> true, if such a node can be found.
   614     ///\param nm must be a bool (or convertible) node map. The
   640     ///
   615     ///algorithm will stop when it reaches a node \c v with
   641     ///\param nm A \c bool (or convertible) node map. The algorithm
   616     /// <tt>nm[v]</tt> true.
   642     ///will stop when it reaches a node \c v with <tt>nm[v]</tt> true.
   617     ///
   643     ///
   618     ///\return The reached node \c v with <tt>nm[v]</tt> true or
   644     ///\return The reached node \c v with <tt>nm[v]</tt> true or
   619     ///\c INVALID if no such node was found.
   645     ///\c INVALID if no such node was found.
   620     template<class NM>
   646     ///
   621     Node start(const NM &nm)
   647     ///\pre init() must be called and at least one root node should be
       
   648     ///added with addSource() before using this function.
       
   649     ///
       
   650     ///\note <tt>b.start(nm)</tt> is just a shortcut of the following code.
       
   651     ///\code
       
   652     ///  Node rnode = INVALID;
       
   653     ///  while ( !b.emptyQueue() && rnode == INVALID ) {
       
   654     ///    b.processNextNode(nm, rnode);
       
   655     ///  }
       
   656     ///  return rnode;
       
   657     ///\endcode
       
   658     template<class NodeBoolMap>
       
   659     Node start(const NodeBoolMap &nm)
   622     {
   660     {
   623       Node rnode = INVALID;
   661       Node rnode = INVALID;
   624       while ( !emptyQueue() && rnode == INVALID ) {
   662       while ( !emptyQueue() && rnode == INVALID ) {
   625         processNextNode(nm, rnode);
   663         processNextNode(nm, rnode);
   626       }
   664       }
   627       return rnode;
   665       return rnode;
   628     }
   666     }
   629 
   667 
   630     ///Runs %BFS algorithm from node \c s.
   668     ///Runs the algorithm from the given node.
   631 
   669 
   632     ///This method runs the %BFS algorithm from a root node \c s
   670     ///This method runs the %BFS algorithm from node \c s
   633     ///in order to
   671     ///in order to compute the shortest path to each node.
   634     ///compute the
   672     ///
   635     ///shortest path to each node. The algorithm computes
   673     ///The algorithm computes
   636     ///- The shortest path tree.
   674     ///- the shortest path tree,
   637     ///- The distance of each node from the root.
   675     ///- the distance of each node from the root.
   638     ///
   676     ///
   639     ///\note b.run(s) is just a shortcut of the following code.
   677     ///\note <tt>b.run(s)</tt> is just a shortcut of the following code.
   640     ///\code
   678     ///\code
   641     ///  b.init();
   679     ///  b.init();
   642     ///  b.addSource(s);
   680     ///  b.addSource(s);
   643     ///  b.start();
   681     ///  b.start();
   644     ///\endcode
   682     ///\endcode
   648       start();
   686       start();
   649     }
   687     }
   650 
   688 
   651     ///Finds the shortest path between \c s and \c t.
   689     ///Finds the shortest path between \c s and \c t.
   652 
   690 
   653     ///Finds the shortest path between \c s and \c t.
   691     ///This method runs the %BFS algorithm from node \c s
   654     ///
   692     ///in order to compute the shortest path to \c t.
   655     ///\return The length of the shortest s---t path if there exists one,
   693     ///
   656     ///0 otherwise.
   694     ///\return The length of the shortest <tt>s</tt>--<tt>t</tt> path,
   657     ///\note Apart from the return value, b.run(s) is
   695     ///if \c t is reachable form \c s, \c 0 otherwise.
   658     ///just a shortcut of the following code.
   696     ///
       
   697     ///\note Apart from the return value, <tt>b.run(s,t)</tt> is just a
       
   698     ///shortcut of the following code.
   659     ///\code
   699     ///\code
   660     ///  b.init();
   700     ///  b.init();
   661     ///  b.addSource(s);
   701     ///  b.addSource(s);
   662     ///  b.start(t);
   702     ///  b.start(t);
   663     ///\endcode
   703     ///\endcode
   666       addSource(s);
   706       addSource(s);
   667       start(t);
   707       start(t);
   668       return reached(t) ? _curr_dist : 0;
   708       return reached(t) ? _curr_dist : 0;
   669     }
   709     }
   670 
   710 
       
   711     ///Runs the algorithm to visit all nodes in the digraph.
       
   712 
       
   713     ///This method runs the %BFS algorithm in order to
       
   714     ///compute the shortest path to each node.
       
   715     ///
       
   716     ///The algorithm computes
       
   717     ///- the shortest path tree (forest),
       
   718     ///- the distance of each node from the root(s).
       
   719     ///
       
   720     ///\note <tt>b.run(s)</tt> is just a shortcut of the following code.
       
   721     ///\code
       
   722     ///  b.init();
       
   723     ///  for (NodeIt n(gr); n != INVALID; ++n) {
       
   724     ///    if (!b.reached(n)) {
       
   725     ///      b.addSource(n);
       
   726     ///      b.start();
       
   727     ///    }
       
   728     ///  }
       
   729     ///\endcode
       
   730     void run() {
       
   731       init();
       
   732       for (NodeIt n(*G); n != INVALID; ++n) {
       
   733         if (!reached(n)) {
       
   734           addSource(n);
       
   735           start();
       
   736         }
       
   737       }
       
   738     }
       
   739 
   671     ///@}
   740     ///@}
   672 
   741 
   673     ///\name Query Functions
   742     ///\name Query Functions
   674     ///The result of the %BFS algorithm can be obtained using these
   743     ///The result of the %BFS algorithm can be obtained using these
   675     ///functions.\n
   744     ///functions.\n
   676     ///Before the use of these functions,
   745     ///Either \ref lemon::Bfs::run() "run()" or \ref lemon::Bfs::start()
   677     ///either run() or start() must be calleb.
   746     ///"start()" must be called before using them.
   678 
   747 
   679     ///@{
   748     ///@{
   680 
   749 
   681     typedef PredMapPath<Digraph, PredMap> Path;
   750     ///The shortest path to a node.
   682 
   751 
   683     ///Gives back the shortest path.
   752     ///Returns the shortest path to a node.
   684 
   753     ///
   685     ///Gives back the shortest path.
   754     ///\warning \c t should be reachable from the root(s).
   686     ///\pre The \c t should be reachable from the source.
   755     ///
   687     Path path(Node t)
   756     ///\pre Either \ref run() or \ref start() must be called before
   688     {
   757     ///using this function.
   689       return Path(*G, *_pred, t);
   758     Path path(Node t) const { return Path(*G, *_pred, t); }
   690     }
       
   691 
   759 
   692     ///The distance of a node from the root(s).
   760     ///The distance of a node from the root(s).
   693 
   761 
   694     ///Returns the distance of a node from the root(s).
   762     ///Returns the distance of a node from the root(s).
   695     ///\pre \ref run() must be called before using this function.
   763     ///
   696     ///\warning If node \c v in unreachable from the root(s) the return value
   764     ///\warning If node \c v is not reachable from the root(s), then
   697     ///of this function is undefined.
   765     ///the return value of this function is undefined.
       
   766     ///
       
   767     ///\pre Either \ref run() or \ref start() must be called before
       
   768     ///using this function.
   698     int dist(Node v) const { return (*_dist)[v]; }
   769     int dist(Node v) const { return (*_dist)[v]; }
   699 
   770 
   700     ///Returns the 'previous arc' of the shortest path tree.
   771     ///Returns the 'previous arc' of the shortest path tree for a node.
   701 
   772 
   702     ///For a node \c v it returns the 'previous arc'
   773     ///This function returns the 'previous arc' of the shortest path
   703     ///of the shortest path tree,
   774     ///tree for the node \c v, i.e. it returns the last arc of a
   704     ///i.e. it returns the last arc of a shortest path from the root(s) to \c
   775     ///shortest path from the root(s) to \c v. It is \c INVALID if \c v
   705     ///v. It is \ref INVALID
   776     ///is not reachable from the root(s) or if \c v is a root.
   706     ///if \c v is unreachable from the root(s) or \c v is a root. The
   777     ///
   707     ///shortest path tree used here is equal to the shortest path tree used in
   778     ///The shortest path tree used here is equal to the shortest path
   708     ///\ref predNode().
   779     ///tree used in \ref predNode().
   709     ///\pre Either \ref run() or \ref start() must be called before using
   780     ///
   710     ///this function.
   781     ///\pre Either \ref run() or \ref start() must be called before
       
   782     ///using this function.
   711     Arc predArc(Node v) const { return (*_pred)[v];}
   783     Arc predArc(Node v) const { return (*_pred)[v];}
   712 
   784 
   713     ///Returns the 'previous node' of the shortest path tree.
   785     ///Returns the 'previous node' of the shortest path tree for a node.
   714 
   786 
   715     ///For a node \c v it returns the 'previous node'
   787     ///This function returns the 'previous node' of the shortest path
   716     ///of the shortest path tree,
   788     ///tree for the node \c v, i.e. it returns the last but one node
   717     ///i.e. it returns the last but one node from a shortest path from the
   789     ///from a shortest path from the root(s) to \c v. It is \c INVALID
   718     ///root(a) to \c /v.
   790     ///if \c v is not reachable from the root(s) or if \c v is a root.
   719     ///It is INVALID if \c v is unreachable from the root(s) or
   791     ///
   720     ///if \c v itself a root.
       
   721     ///The shortest path tree used here is equal to the shortest path
   792     ///The shortest path tree used here is equal to the shortest path
   722     ///tree used in \ref predArc().
   793     ///tree used in \ref predArc().
       
   794     ///
   723     ///\pre Either \ref run() or \ref start() must be called before
   795     ///\pre Either \ref run() or \ref start() must be called before
   724     ///using this function.
   796     ///using this function.
   725     Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
   797     Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
   726                                   G->source((*_pred)[v]); }
   798                                   G->source((*_pred)[v]); }
   727 
   799 
   728     ///Returns a reference to the NodeMap of distances.
   800     ///\brief Returns a const reference to the node map that stores the
   729 
   801     /// distances of the nodes.
   730     ///Returns a reference to the NodeMap of distances.
   802     ///
   731     ///\pre Either \ref run() or \ref init() must
   803     ///Returns a const reference to the node map that stores the distances
   732     ///be called before using this function.
   804     ///of the nodes calculated by the algorithm.
       
   805     ///
       
   806     ///\pre Either \ref run() or \ref init()
       
   807     ///must be called before using this function.
   733     const DistMap &distMap() const { return *_dist;}
   808     const DistMap &distMap() const { return *_dist;}
   734 
   809 
   735     ///Returns a reference to the shortest path tree map.
   810     ///\brief Returns a const reference to the node map that stores the
   736 
   811     ///predecessor arcs.
   737     ///Returns a reference to the NodeMap of the arcs of the
   812     ///
   738     ///shortest path tree.
   813     ///Returns a const reference to the node map that stores the predecessor
       
   814     ///arcs, which form the shortest path tree.
       
   815     ///
   739     ///\pre Either \ref run() or \ref init()
   816     ///\pre Either \ref run() or \ref init()
   740     ///must be called before using this function.
   817     ///must be called before using this function.
   741     const PredMap &predMap() const { return *_pred;}
   818     const PredMap &predMap() const { return *_pred;}
   742 
   819 
   743     ///Checks if a node is reachable from the root.
   820     ///Checks if a node is reachable from the root(s).
   744 
   821 
   745     ///Returns \c true if \c v is reachable from the root.
   822     ///Returns \c true if \c v is reachable from the root(s).
   746     ///\warning The source nodes are indicated as unreached.
       
   747     ///\pre Either \ref run() or \ref start()
   823     ///\pre Either \ref run() or \ref start()
   748     ///must be called before using this function.
   824     ///must be called before using this function.
   749     ///
   825     bool reached(Node v) const { return (*_reached)[v]; }
   750     bool reached(Node v) { return (*_reached)[v]; }
       
   751 
   826 
   752     ///@}
   827     ///@}
   753   };
   828   };
   754 
   829 
   755   ///Default traits class of Bfs function.
   830   ///Default traits class of bfs() function.
   756 
   831 
   757   ///Default traits class of Bfs function.
   832   ///Default traits class of bfs() function.
   758   ///\tparam GR Digraph type.
   833   ///\tparam GR Digraph type.
   759   template<class GR>
   834   template<class GR>
   760   struct BfsWizardDefaultTraits
   835   struct BfsWizardDefaultTraits
   761   {
   836   {
   762     ///The digraph type the algorithm runs on.
   837     ///The type of the digraph the algorithm runs on.
   763     typedef GR Digraph;
   838     typedef GR Digraph;
   764     ///\brief The type of the map that stores the last
   839 
       
   840     ///\brief The type of the map that stores the predecessor
   765     ///arcs of the shortest paths.
   841     ///arcs of the shortest paths.
   766     ///
   842     ///
   767     ///The type of the map that stores the last
   843     ///The type of the map that stores the predecessor
   768     ///arcs of the shortest paths.
   844     ///arcs of the shortest paths.
   769     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   845     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   770     ///
   846     typedef NullMap<typename Digraph::Node,typename Digraph::Arc> PredMap;
   771     typedef NullMap<typename Digraph::Node,typename GR::Arc> PredMap;
   847     ///Instantiates a \ref PredMap.
   772     ///Instantiates a PredMap.
       
   773 
   848 
   774     ///This function instantiates a \ref PredMap.
   849     ///This function instantiates a \ref PredMap.
   775     ///\param g is the digraph, to which we would like to define the PredMap.
   850     ///\param g is the digraph, to which we would like to define the
       
   851     ///\ref PredMap.
   776     ///\todo The digraph alone may be insufficient to initialize
   852     ///\todo The digraph alone may be insufficient to initialize
   777 #ifdef DOXYGEN
   853 #ifdef DOXYGEN
   778     static PredMap *createPredMap(const GR &g)
   854     static PredMap *createPredMap(const Digraph &g)
   779 #else
   855 #else
   780     static PredMap *createPredMap(const GR &)
   856     static PredMap *createPredMap(const Digraph &)
   781 #endif
   857 #endif
   782     {
   858     {
   783       return new PredMap();
   859       return new PredMap();
   784     }
   860     }
   785 
   861 
   786     ///The type of the map that indicates which nodes are processed.
   862     ///The type of the map that indicates which nodes are processed.
   787 
   863 
   788     ///The type of the map that indicates which nodes are processed.
   864     ///The type of the map that indicates which nodes are processed.
   789     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   865     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   790     ///\todo named parameter to set this type, function to read and write.
       
   791     typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
   866     typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
   792     ///Instantiates a ProcessedMap.
   867     ///Instantiates a \ref ProcessedMap.
   793 
   868 
   794     ///This function instantiates a \ref ProcessedMap.
   869     ///This function instantiates a \ref ProcessedMap.
   795     ///\param g is the digraph, to which
   870     ///\param g is the digraph, to which
   796     ///we would like to define the \ref ProcessedMap
   871     ///we would like to define the \ref ProcessedMap.
   797 #ifdef DOXYGEN
   872 #ifdef DOXYGEN
   798     static ProcessedMap *createProcessedMap(const GR &g)
   873     static ProcessedMap *createProcessedMap(const Digraph &g)
   799 #else
   874 #else
   800     static ProcessedMap *createProcessedMap(const GR &)
   875     static ProcessedMap *createProcessedMap(const Digraph &)
   801 #endif
   876 #endif
   802     {
   877     {
   803       return new ProcessedMap();
   878       return new ProcessedMap();
   804     }
   879     }
       
   880 
   805     ///The type of the map that indicates which nodes are reached.
   881     ///The type of the map that indicates which nodes are reached.
   806 
   882 
   807     ///The type of the map that indicates which nodes are reached.
   883     ///The type of the map that indicates which nodes are reached.
       
   884     ///It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
       
   885     typedef typename Digraph::template NodeMap<bool> ReachedMap;
       
   886     ///Instantiates a \ref ReachedMap.
       
   887 
       
   888     ///This function instantiates a \ref ReachedMap.
       
   889     ///\param g is the digraph, to which
       
   890     ///we would like to define the \ref ReachedMap.
       
   891     static ReachedMap *createReachedMap(const Digraph &g)
       
   892     {
       
   893       return new ReachedMap(g);
       
   894     }
       
   895 
       
   896     ///The type of the map that stores the distances of the nodes.
       
   897 
       
   898     ///The type of the map that stores the distances of the nodes.
   808     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   899     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   809     ///\todo named parameter to set this type, function to read and write.
       
   810     typedef typename Digraph::template NodeMap<bool> ReachedMap;
       
   811     ///Instantiates a ReachedMap.
       
   812 
       
   813     ///This function instantiates a \ref ReachedMap.
       
   814     ///\param G is the digraph, to which
       
   815     ///we would like to define the \ref ReachedMap.
       
   816     static ReachedMap *createReachedMap(const GR &G)
       
   817     {
       
   818       return new ReachedMap(G);
       
   819     }
       
   820     ///The type of the map that stores the dists of the nodes.
       
   821 
       
   822     ///The type of the map that stores the dists of the nodes.
       
   823     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
       
   824     ///
   900     ///
   825     typedef NullMap<typename Digraph::Node,int> DistMap;
   901     typedef NullMap<typename Digraph::Node,int> DistMap;
   826     ///Instantiates a DistMap.
   902     ///Instantiates a \ref DistMap.
   827 
   903 
   828     ///This function instantiates a \ref DistMap.
   904     ///This function instantiates a \ref DistMap.
   829     ///\param g is the digraph, to which we would like to define
   905     ///\param g is the digraph, to which we would like to define
   830     ///the \ref DistMap
   906     ///the \ref DistMap
   831 #ifdef DOXYGEN
   907 #ifdef DOXYGEN
   832     static DistMap *createDistMap(const GR &g)
   908     static DistMap *createDistMap(const Digraph &g)
   833 #else
   909 #else
   834     static DistMap *createDistMap(const GR &)
   910     static DistMap *createDistMap(const Digraph &)
   835 #endif
   911 #endif
   836     {
   912     {
   837       return new DistMap();
   913       return new DistMap();
   838     }
   914     }
   839   };
   915   };
   840 
   916 
   841   /// Default traits used by \ref BfsWizard
   917   /// Default traits class used by \ref BfsWizard
   842 
   918 
   843   /// To make it easier to use Bfs algorithm
   919   /// To make it easier to use Bfs algorithm
   844   ///we have created a wizard class.
   920   /// we have created a wizard class.
   845   /// This \ref BfsWizard class needs default traits,
   921   /// This \ref BfsWizard class needs default traits,
   846   ///as well as the \ref Bfs class.
   922   /// as well as the \ref Bfs class.
   847   /// The \ref BfsWizardBase is a class to be the default traits of the
   923   /// The \ref BfsWizardBase is a class to be the default traits of the
   848   /// \ref BfsWizard class.
   924   /// \ref BfsWizard class.
   849   template<class GR>
   925   template<class GR>
   850   class BfsWizardBase : public BfsWizardDefaultTraits<GR>
   926   class BfsWizardBase : public BfsWizardDefaultTraits<GR>
   851   {
   927   {
   852 
   928 
   853     typedef BfsWizardDefaultTraits<GR> Base;
   929     typedef BfsWizardDefaultTraits<GR> Base;
   854   protected:
   930   protected:
   855     /// Type of the nodes in the digraph.
   931     //The type of the nodes in the digraph.
   856     typedef typename Base::Digraph::Node Node;
   932     typedef typename Base::Digraph::Node Node;
   857 
   933 
   858     /// Pointer to the underlying digraph.
   934     //Pointer to the digraph the algorithm runs on.
   859     void *_g;
   935     void *_g;
   860     ///Pointer to the map of reached nodes.
   936     //Pointer to the map of reached nodes.
   861     void *_reached;
   937     void *_reached;
   862     ///Pointer to the map of processed nodes.
   938     //Pointer to the map of processed nodes.
   863     void *_processed;
   939     void *_processed;
   864     ///Pointer to the map of predecessors arcs.
   940     //Pointer to the map of predecessors arcs.
   865     void *_pred;
   941     void *_pred;
   866     ///Pointer to the map of distances.
   942     //Pointer to the map of distances.
   867     void *_dist;
   943     void *_dist;
   868     ///Pointer to the source node.
   944     //Pointer to the source node.
   869     Node _source;
   945     Node _source;
   870 
   946 
   871     public:
   947     public:
   872     /// Constructor.
   948     /// Constructor.
   873 
   949 
   874     /// This constructor does not require parameters, therefore it initiates
   950     /// This constructor does not require parameters, therefore it initiates
   875     /// all of the attributes to default values (0, INVALID).
   951     /// all of the attributes to default values (0, INVALID).
   876     BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
   952     BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
   877                            _dist(0), _source(INVALID) {}
   953                       _dist(0), _source(INVALID) {}
   878 
   954 
   879     /// Constructor.
   955     /// Constructor.
   880 
   956 
   881     /// This constructor requires some parameters,
   957     /// This constructor requires some parameters,
   882     /// listed in the parameters list.
   958     /// listed in the parameters list.
   883     /// Others are initiated to 0.
   959     /// Others are initiated to 0.
   884     /// \param g is the initial value of  \ref _g
   960     /// \param g The digraph the algorithm runs on.
   885     /// \param s is the initial value of  \ref _source
   961     /// \param s The source node.
   886     BfsWizardBase(const GR &g, Node s=INVALID) :
   962     BfsWizardBase(const GR &g, Node s=INVALID) :
   887       _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
   963       _g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
   888       _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
   964       _reached(0), _processed(0), _pred(0), _dist(0), _source(s) {}
   889 
   965 
   890   };
   966   };
   891 
   967 
   892   /// A class to make the usage of Bfs algorithm easier
   968   /// Auxiliary class for the function type interface of BFS algorithm.
   893 
   969 
   894   /// This class is created to make it easier to use Bfs algorithm.
   970   /// This auxiliary class is created to implement the function type
   895   /// It uses the functions and features of the plain \ref Bfs,
   971   /// interface of \ref Bfs algorithm. It uses the functions and features
   896   /// but it is much simpler to use it.
   972   /// of the plain \ref Bfs, but it is much simpler to use it.
       
   973   /// It should only be used through the \ref bfs() function, which makes
       
   974   /// it easier to use the algorithm.
   897   ///
   975   ///
   898   /// Simplicity means that the way to change the types defined
   976   /// Simplicity means that the way to change the types defined
   899   /// in the traits class is based on functions that returns the new class
   977   /// in the traits class is based on functions that returns the new class
   900   /// and not on templatable built-in classes.
   978   /// and not on templatable built-in classes.
   901   /// When using the plain \ref Bfs
   979   /// When using the plain \ref Bfs
   902   /// the new class with the modified type comes from
   980   /// the new class with the modified type comes from
   903   /// the original class by using the ::
   981   /// the original class by using the ::
   904   /// operator. In the case of \ref BfsWizard only
   982   /// operator. In the case of \ref BfsWizard only
   905   /// a function have to be called and it will
   983   /// a function have to be called, and it will
   906   /// return the needed class.
   984   /// return the needed class.
   907   ///
   985   ///
   908   /// It does not have own \ref run method. When its \ref run method is called
   986   /// It does not have own \ref run() method. When its \ref run() method
   909   /// it initiates a plain \ref Bfs class, and calls the \ref Bfs::run
   987   /// is called, it initiates a plain \ref Bfs object, and calls the
   910   /// method of it.
   988   /// \ref Bfs::run() method of it.
   911   template<class TR>
   989   template<class TR>
   912   class BfsWizard : public TR
   990   class BfsWizard : public TR
   913   {
   991   {
   914     typedef TR Base;
   992     typedef TR Base;
   915 
   993 
   916     ///The type of the underlying digraph.
   994     ///The type of the digraph the algorithm runs on.
   917     typedef typename TR::Digraph Digraph;
   995     typedef typename TR::Digraph Digraph;
   918     //\e
   996 
   919     typedef typename Digraph::Node Node;
   997     typedef typename Digraph::Node Node;
   920     //\e
       
   921     typedef typename Digraph::NodeIt NodeIt;
   998     typedef typename Digraph::NodeIt NodeIt;
   922     //\e
       
   923     typedef typename Digraph::Arc Arc;
   999     typedef typename Digraph::Arc Arc;
   924     //\e
       
   925     typedef typename Digraph::OutArcIt OutArcIt;
  1000     typedef typename Digraph::OutArcIt OutArcIt;
   926 
  1001 
   927     ///\brief The type of the map that stores
  1002     ///\brief The type of the map that stores the predecessor
   928     ///the reached nodes
       
   929     typedef typename TR::ReachedMap ReachedMap;
       
   930     ///\brief The type of the map that stores
       
   931     ///the processed nodes
       
   932     typedef typename TR::ProcessedMap ProcessedMap;
       
   933     ///\brief The type of the map that stores the last
       
   934     ///arcs of the shortest paths.
  1003     ///arcs of the shortest paths.
   935     typedef typename TR::PredMap PredMap;
  1004     typedef typename TR::PredMap PredMap;
   936     ///The type of the map that stores the dists of the nodes.
  1005     ///\brief The type of the map that stores the distances of the nodes.
   937     typedef typename TR::DistMap DistMap;
  1006     typedef typename TR::DistMap DistMap;
       
  1007     ///\brief The type of the map that indicates which nodes are reached.
       
  1008     typedef typename TR::ReachedMap ReachedMap;
       
  1009     ///\brief The type of the map that indicates which nodes are processed.
       
  1010     typedef typename TR::ProcessedMap ProcessedMap;
   938 
  1011 
   939   public:
  1012   public:
       
  1013 
   940     /// Constructor.
  1014     /// Constructor.
   941     BfsWizard() : TR() {}
  1015     BfsWizard() : TR() {}
   942 
  1016 
   943     /// Constructor that requires parameters.
  1017     /// Constructor that requires parameters.
   944 
  1018 
   950     ///Copy constructor
  1024     ///Copy constructor
   951     BfsWizard(const TR &b) : TR(b) {}
  1025     BfsWizard(const TR &b) : TR(b) {}
   952 
  1026 
   953     ~BfsWizard() {}
  1027     ~BfsWizard() {}
   954 
  1028 
   955     ///Runs Bfs algorithm from a given node.
  1029     ///Runs BFS algorithm from a source node.
   956 
  1030 
   957     ///Runs Bfs algorithm from a given node.
  1031     ///Runs BFS algorithm from a source node.
   958     ///The node can be given by the \ref source function.
  1032     ///The node can be given with the \ref source() function.
   959     void run()
  1033     void run()
   960     {
  1034     {
   961       if(Base::_source==INVALID) throw UninitializedParameter();
  1035       if(Base::_source==INVALID) throw UninitializedParameter();
   962       Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
  1036       Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
   963       if(Base::_reached)
  1037       if(Base::_reached)
   969       if(Base::_dist)
  1043       if(Base::_dist)
   970         alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
  1044         alg.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
   971       alg.run(Base::_source);
  1045       alg.run(Base::_source);
   972     }
  1046     }
   973 
  1047 
   974     ///Runs Bfs algorithm from the given node.
  1048     ///Runs BFS algorithm from the given node.
   975 
  1049 
   976     ///Runs Bfs algorithm from the given node.
  1050     ///Runs BFS algorithm from the given node.
   977     ///\param s is the given source.
  1051     ///\param s is the given source.
   978     void run(Node s)
  1052     void run(Node s)
   979     {
  1053     {
   980       Base::_source=s;
  1054       Base::_source=s;
   981       run();
  1055       run();
       
  1056     }
       
  1057 
       
  1058     /// Sets the source node, from which the Bfs algorithm runs.
       
  1059 
       
  1060     /// Sets the source node, from which the Bfs algorithm runs.
       
  1061     /// \param s is the source node.
       
  1062     BfsWizard<TR> &source(Node s)
       
  1063     {
       
  1064       Base::_source=s;
       
  1065       return *this;
   982     }
  1066     }
   983 
  1067 
   984     template<class T>
  1068     template<class T>
   985     struct DefPredMapBase : public Base {
  1069     struct DefPredMapBase : public Base {
   986       typedef T PredMap;
  1070       typedef T PredMap;
   987       static PredMap *createPredMap(const Digraph &) { return 0; };
  1071       static PredMap *createPredMap(const Digraph &) { return 0; };
   988       DefPredMapBase(const TR &b) : TR(b) {}
  1072       DefPredMapBase(const TR &b) : TR(b) {}
   989     };
  1073     };
   990 
       
   991     ///\brief \ref named-templ-param "Named parameter"
  1074     ///\brief \ref named-templ-param "Named parameter"
   992     ///function for setting PredMap
  1075     ///for setting \ref PredMap object.
   993     ///
  1076     ///
   994     /// \ref named-templ-param "Named parameter"
  1077     /// \ref named-templ-param "Named parameter"
   995     ///function for setting PredMap
  1078     ///for setting \ref PredMap object.
   996     ///
       
   997     template<class T>
  1079     template<class T>
   998     BfsWizard<DefPredMapBase<T> > predMap(const T &t)
  1080     BfsWizard<DefPredMapBase<T> > predMap(const T &t)
   999     {
  1081     {
  1000       Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
  1082       Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
  1001       return BfsWizard<DefPredMapBase<T> >(*this);
  1083       return BfsWizard<DefPredMapBase<T> >(*this);
  1002     }
  1084     }
  1003 
       
  1004 
  1085 
  1005     template<class T>
  1086     template<class T>
  1006     struct DefReachedMapBase : public Base {
  1087     struct DefReachedMapBase : public Base {
  1007       typedef T ReachedMap;
  1088       typedef T ReachedMap;
  1008       static ReachedMap *createReachedMap(const Digraph &) { return 0; };
  1089       static ReachedMap *createReachedMap(const Digraph &) { return 0; };
  1009       DefReachedMapBase(const TR &b) : TR(b) {}
  1090       DefReachedMapBase(const TR &b) : TR(b) {}
  1010     };
  1091     };
  1011 
       
  1012     ///\brief \ref named-templ-param "Named parameter"
  1092     ///\brief \ref named-templ-param "Named parameter"
  1013     ///function for setting ReachedMap
  1093     ///for setting \ref ReachedMap object.
  1014     ///
  1094     ///
  1015     /// \ref named-templ-param "Named parameter"
  1095     /// \ref named-templ-param "Named parameter"
  1016     ///function for setting ReachedMap
  1096     ///for setting \ref ReachedMap object.
  1017     ///
       
  1018     template<class T>
  1097     template<class T>
  1019     BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t)
  1098     BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t)
  1020     {
  1099     {
  1021       Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
  1100       Base::_reached=reinterpret_cast<void*>(const_cast<T*>(&t));
  1022       return BfsWizard<DefReachedMapBase<T> >(*this);
  1101       return BfsWizard<DefReachedMapBase<T> >(*this);
  1023     }
  1102     }
  1024 
       
  1025 
  1103 
  1026     template<class T>
  1104     template<class T>
  1027     struct DefProcessedMapBase : public Base {
  1105     struct DefProcessedMapBase : public Base {
  1028       typedef T ProcessedMap;
  1106       typedef T ProcessedMap;
  1029       static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
  1107       static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
  1030       DefProcessedMapBase(const TR &b) : TR(b) {}
  1108       DefProcessedMapBase(const TR &b) : TR(b) {}
  1031     };
  1109     };
  1032 
       
  1033     ///\brief \ref named-templ-param "Named parameter"
  1110     ///\brief \ref named-templ-param "Named parameter"
  1034     ///function for setting ProcessedMap
  1111     ///for setting \ref ProcessedMap object.
  1035     ///
  1112     ///
  1036     /// \ref named-templ-param "Named parameter"
  1113     /// \ref named-templ-param "Named parameter"
  1037     ///function for setting ProcessedMap
  1114     ///for setting \ref ProcessedMap object.
  1038     ///
       
  1039     template<class T>
  1115     template<class T>
  1040     BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t)
  1116     BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t)
  1041     {
  1117     {
  1042       Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
  1118       Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
  1043       return BfsWizard<DefProcessedMapBase<T> >(*this);
  1119       return BfsWizard<DefProcessedMapBase<T> >(*this);
  1044     }
  1120     }
  1045 
       
  1046 
  1121 
  1047     template<class T>
  1122     template<class T>
  1048     struct DefDistMapBase : public Base {
  1123     struct DefDistMapBase : public Base {
  1049       typedef T DistMap;
  1124       typedef T DistMap;
  1050       static DistMap *createDistMap(const Digraph &) { return 0; };
  1125       static DistMap *createDistMap(const Digraph &) { return 0; };
  1051       DefDistMapBase(const TR &b) : TR(b) {}
  1126       DefDistMapBase(const TR &b) : TR(b) {}
  1052     };
  1127     };
  1053 
       
  1054     ///\brief \ref named-templ-param "Named parameter"
  1128     ///\brief \ref named-templ-param "Named parameter"
  1055     ///function for setting DistMap type
  1129     ///for setting \ref DistMap object.
  1056     ///
  1130     ///
  1057     /// \ref named-templ-param "Named parameter"
  1131     /// \ref named-templ-param "Named parameter"
  1058     ///function for setting DistMap type
  1132     ///for setting \ref DistMap object.
  1059     ///
       
  1060     template<class T>
  1133     template<class T>
  1061     BfsWizard<DefDistMapBase<T> > distMap(const T &t)
  1134     BfsWizard<DefDistMapBase<T> > distMap(const T &t)
  1062     {
  1135     {
  1063       Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
  1136       Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
  1064       return BfsWizard<DefDistMapBase<T> >(*this);
  1137       return BfsWizard<DefDistMapBase<T> >(*this);
  1065     }
       
  1066 
       
  1067     /// Sets the source node, from which the Bfs algorithm runs.
       
  1068 
       
  1069     /// Sets the source node, from which the Bfs algorithm runs.
       
  1070     /// \param s is the source node.
       
  1071     BfsWizard<TR> &source(Node s)
       
  1072     {
       
  1073       Base::_source=s;
       
  1074       return *this;
       
  1075     }
  1138     }
  1076 
  1139 
  1077   };
  1140   };
  1078 
  1141 
  1079   ///Function type interface for Bfs algorithm.
  1142   ///Function type interface for Bfs algorithm.
  1099   {
  1162   {
  1100     return BfsWizard<BfsWizardBase<GR> >(g,s);
  1163     return BfsWizard<BfsWizardBase<GR> >(g,s);
  1101   }
  1164   }
  1102 
  1165 
  1103 #ifdef DOXYGEN
  1166 #ifdef DOXYGEN
  1104   /// \brief Visitor class for bfs.
  1167   /// \brief Visitor class for BFS.
  1105   ///
  1168   ///
  1106   /// This class defines the interface of the BfsVisit events, and
  1169   /// This class defines the interface of the BfsVisit events, and
  1107   /// it could be the base of a real Visitor class.
  1170   /// it could be the base of a real visitor class.
  1108   template <typename _Digraph>
  1171   template <typename _Digraph>
  1109   struct BfsVisitor {
  1172   struct BfsVisitor {
  1110     typedef _Digraph Digraph;
  1173     typedef _Digraph Digraph;
  1111     typedef typename Digraph::Arc Arc;
  1174     typedef typename Digraph::Arc Arc;
  1112     typedef typename Digraph::Node Node;
  1175     typedef typename Digraph::Node Node;
  1113     /// \brief Called when the arc reach a node.
  1176     /// \brief Called for the source node(s) of the BFS.
  1114     ///
  1177     ///
  1115     /// It is called when the bfs find an arc which target is not
  1178     /// This function is called for the source node(s) of the BFS.
  1116     /// reached yet.
  1179     void start(const Node& node) {}
       
  1180     /// \brief Called when a node is reached first time.
       
  1181     ///
       
  1182     /// This function is called when a node is reached first time.
       
  1183     void reach(const Node& node) {}
       
  1184     /// \brief Called when a node is processed.
       
  1185     ///
       
  1186     /// This function is called when a node is processed.
       
  1187     void process(const Node& node) {}
       
  1188     /// \brief Called when an arc reaches a new node.
       
  1189     ///
       
  1190     /// This function is called when the BFS finds an arc whose target node
       
  1191     /// is not reached yet.
  1117     void discover(const Arc& arc) {}
  1192     void discover(const Arc& arc) {}
  1118     /// \brief Called when the node reached first time.
  1193     /// \brief Called when an arc is examined but its target node is
  1119     ///
       
  1120     /// It is Called when the node reached first time.
       
  1121     void reach(const Node& node) {}
       
  1122     /// \brief Called when the arc examined but target of the arc
       
  1123     /// already discovered.
  1194     /// already discovered.
  1124     ///
  1195     ///
  1125     /// It called when the arc examined but the target of the arc
  1196     /// This function is called when an arc is examined but its target node is
  1126     /// already discovered.
  1197     /// already discovered.
  1127     void examine(const Arc& arc) {}
  1198     void examine(const Arc& arc) {}
  1128     /// \brief Called for the source node of the bfs.
       
  1129     ///
       
  1130     /// It is called for the source node of the bfs.
       
  1131     void start(const Node& node) {}
       
  1132     /// \brief Called when the node processed.
       
  1133     ///
       
  1134     /// It is Called when the node processed.
       
  1135     void process(const Node& node) {}
       
  1136   };
  1199   };
  1137 #else
  1200 #else
  1138   template <typename _Digraph>
  1201   template <typename _Digraph>
  1139   struct BfsVisitor {
  1202   struct BfsVisitor {
  1140     typedef _Digraph Digraph;
  1203     typedef _Digraph Digraph;
  1141     typedef typename Digraph::Arc Arc;
  1204     typedef typename Digraph::Arc Arc;
  1142     typedef typename Digraph::Node Node;
  1205     typedef typename Digraph::Node Node;
       
  1206     void start(const Node&) {}
       
  1207     void reach(const Node&) {}
       
  1208     void process(const Node&) {}
  1143     void discover(const Arc&) {}
  1209     void discover(const Arc&) {}
  1144     void reach(const Node&) {}
       
  1145     void examine(const Arc&) {}
  1210     void examine(const Arc&) {}
  1146     void start(const Node&) {}
       
  1147     void process(const Node&) {}
       
  1148 
  1211 
  1149     template <typename _Visitor>
  1212     template <typename _Visitor>
  1150     struct Constraints {
  1213     struct Constraints {
  1151       void constraints() {
  1214       void constraints() {
  1152         Arc arc;
  1215         Arc arc;
  1153         Node node;
  1216         Node node;
       
  1217         visitor.start(node);
       
  1218         visitor.reach(node);
       
  1219         visitor.process(node);
  1154         visitor.discover(arc);
  1220         visitor.discover(arc);
  1155         visitor.reach(node);
       
  1156         visitor.examine(arc);
  1221         visitor.examine(arc);
  1157         visitor.start(node);
       
  1158         visitor.process(node);
       
  1159       }
  1222       }
  1160       _Visitor& visitor;
  1223       _Visitor& visitor;
  1161     };
  1224     };
  1162   };
  1225   };
  1163 #endif
  1226 #endif
  1164 
  1227 
  1165   /// \brief Default traits class of BfsVisit class.
  1228   /// \brief Default traits class of BfsVisit class.
  1166   ///
  1229   ///
  1167   /// Default traits class of BfsVisit class.
  1230   /// Default traits class of BfsVisit class.
  1168   /// \tparam _Digraph Digraph type.
  1231   /// \tparam _Digraph The type of the digraph the algorithm runs on.
  1169   template<class _Digraph>
  1232   template<class _Digraph>
  1170   struct BfsVisitDefaultTraits {
  1233   struct BfsVisitDefaultTraits {
  1171 
  1234 
  1172     /// \brief The digraph type the algorithm runs on.
  1235     /// \brief The type of the digraph the algorithm runs on.
  1173     typedef _Digraph Digraph;
  1236     typedef _Digraph Digraph;
  1174 
  1237 
  1175     /// \brief The type of the map that indicates which nodes are reached.
  1238     /// \brief The type of the map that indicates which nodes are reached.
  1176     ///
  1239     ///
  1177     /// The type of the map that indicates which nodes are reached.
  1240     /// The type of the map that indicates which nodes are reached.
  1178     /// It must meet the \ref concepts::WriteMap "WriteMap" concept.
  1241     /// It must meet the \ref concepts::ReadWriteMap "ReadWriteMap" concept.
  1179     /// \todo named parameter to set this type, function to read and write.
       
  1180     typedef typename Digraph::template NodeMap<bool> ReachedMap;
  1242     typedef typename Digraph::template NodeMap<bool> ReachedMap;
  1181 
  1243 
  1182     /// \brief Instantiates a ReachedMap.
  1244     /// \brief Instantiates a \ref ReachedMap.
  1183     ///
  1245     ///
  1184     /// This function instantiates a \ref ReachedMap.
  1246     /// This function instantiates a \ref ReachedMap.
  1185     /// \param digraph is the digraph, to which
  1247     /// \param digraph is the digraph, to which
  1186     /// we would like to define the \ref ReachedMap.
  1248     /// we would like to define the \ref ReachedMap.
  1187     static ReachedMap *createReachedMap(const Digraph &digraph) {
  1249     static ReachedMap *createReachedMap(const Digraph &digraph) {
  1190 
  1252 
  1191   };
  1253   };
  1192 
  1254 
  1193   /// \ingroup search
  1255   /// \ingroup search
  1194   ///
  1256   ///
  1195   /// \brief %BFS Visit algorithm class.
  1257   /// \brief %BFS algorithm class with visitor interface.
  1196   ///
  1258   ///
  1197   /// This class provides an efficient implementation of the %BFS algorithm
  1259   /// This class provides an efficient implementation of the %BFS algorithm
  1198   /// with visitor interface.
  1260   /// with visitor interface.
  1199   ///
  1261   ///
  1200   /// The %BfsVisit class provides an alternative interface to the Bfs
  1262   /// The %BfsVisit class provides an alternative interface to the Bfs
  1201   /// class. It works with callback mechanism, the BfsVisit object calls
  1263   /// class. It works with callback mechanism, the BfsVisit object calls
  1202   /// on every bfs event the \c Visitor class member functions.
  1264   /// the member functions of the \c Visitor class on every BFS event.
  1203   ///
  1265   ///
  1204   /// \tparam _Digraph The digraph type the algorithm runs on.
  1266   /// \tparam _Digraph The type of the digraph the algorithm runs on.
  1205   /// The default value is
  1267   /// The default value is
  1206   /// \ref ListDigraph. The value of _Digraph is not used directly by Bfs, it
  1268   /// \ref ListDigraph. The value of _Digraph is not used directly by
  1207   /// is only passed to \ref BfsDefaultTraits.
  1269   /// \ref BfsVisit, it is only passed to \ref BfsVisitDefaultTraits.
  1208   /// \tparam _Visitor The Visitor object for the algorithm. The
  1270   /// \tparam _Visitor The Visitor type that is used by the algorithm.
  1209   /// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty Visitor which
  1271   /// \ref BfsVisitor "BfsVisitor<_Digraph>" is an empty visitor, which
  1210   /// does not observe the Bfs events. If you want to observe the bfs
  1272   /// does not observe the BFS events. If you want to observe the BFS
  1211   /// events you should implement your own Visitor class.
  1273   /// events, you should implement your own visitor class.
  1212   /// \tparam _Traits Traits class to set various data types used by the
  1274   /// \tparam _Traits Traits class to set various data types used by the
  1213   /// algorithm. The default traits class is
  1275   /// algorithm. The default traits class is
  1214   /// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>".
  1276   /// \ref BfsVisitDefaultTraits "BfsVisitDefaultTraits<_Digraph>".
  1215   /// See \ref BfsVisitDefaultTraits for the documentation of
  1277   /// See \ref BfsVisitDefaultTraits for the documentation of
  1216   /// a Bfs visit traits class.
  1278   /// a BFS visit traits class.
  1217 #ifdef DOXYGEN
  1279 #ifdef DOXYGEN
  1218   template <typename _Digraph, typename _Visitor, typename _Traits>
  1280   template <typename _Digraph, typename _Visitor, typename _Traits>
  1219 #else
  1281 #else
  1220   template <typename _Digraph = ListDigraph,
  1282   template <typename _Digraph = ListDigraph,
  1221             typename _Visitor = BfsVisitor<_Digraph>,
  1283             typename _Visitor = BfsVisitor<_Digraph>,
  1225   public:
  1287   public:
  1226 
  1288 
  1227     /// \brief \ref Exception for uninitialized parameters.
  1289     /// \brief \ref Exception for uninitialized parameters.
  1228     ///
  1290     ///
  1229     /// This error represents problems in the initialization
  1291     /// This error represents problems in the initialization
  1230     /// of the parameters of the algorithms.
  1292     /// of the parameters of the algorithm.
  1231     class UninitializedParameter : public lemon::UninitializedParameter {
  1293     class UninitializedParameter : public lemon::UninitializedParameter {
  1232     public:
  1294     public:
  1233       virtual const char* what() const throw()
  1295       virtual const char* what() const throw()
  1234       {
  1296       {
  1235         return "lemon::BfsVisit::UninitializedParameter";
  1297         return "lemon::BfsVisit::UninitializedParameter";
  1236       }
  1298       }
  1237     };
  1299     };
  1238 
  1300 
       
  1301     ///The traits class.
  1239     typedef _Traits Traits;
  1302     typedef _Traits Traits;
  1240 
  1303 
       
  1304     ///The type of the digraph the algorithm runs on.
  1241     typedef typename Traits::Digraph Digraph;
  1305     typedef typename Traits::Digraph Digraph;
  1242 
  1306 
       
  1307     ///The visitor type used by the algorithm.
  1243     typedef _Visitor Visitor;
  1308     typedef _Visitor Visitor;
  1244 
  1309 
  1245     ///The type of the map indicating which nodes are reached.
  1310     ///The type of the map that indicates which nodes are reached.
  1246     typedef typename Traits::ReachedMap ReachedMap;
  1311     typedef typename Traits::ReachedMap ReachedMap;
  1247 
  1312 
  1248   private:
  1313   private:
  1249 
  1314 
  1250     typedef typename Digraph::Node Node;
  1315     typedef typename Digraph::Node Node;
  1251     typedef typename Digraph::NodeIt NodeIt;
  1316     typedef typename Digraph::NodeIt NodeIt;
  1252     typedef typename Digraph::Arc Arc;
  1317     typedef typename Digraph::Arc Arc;
  1253     typedef typename Digraph::OutArcIt OutArcIt;
  1318     typedef typename Digraph::OutArcIt OutArcIt;
  1254 
  1319 
  1255     /// Pointer to the underlying digraph.
  1320     //Pointer to the underlying digraph.
  1256     const Digraph *_digraph;
  1321     const Digraph *_digraph;
  1257     /// Pointer to the visitor object.
  1322     //Pointer to the visitor object.
  1258     Visitor *_visitor;
  1323     Visitor *_visitor;
  1259     ///Pointer to the map of reached status of the nodes.
  1324     //Pointer to the map of reached status of the nodes.
  1260     ReachedMap *_reached;
  1325     ReachedMap *_reached;
  1261     ///Indicates if \ref _reached is locally allocated (\c true) or not.
  1326     //Indicates if _reached is locally allocated (true) or not.
  1262     bool local_reached;
  1327     bool local_reached;
  1263 
  1328 
  1264     std::vector<typename Digraph::Node> _list;
  1329     std::vector<typename Digraph::Node> _list;
  1265     int _list_front, _list_back;
  1330     int _list_front, _list_back;
  1266 
  1331 
  1267     /// \brief Creates the maps if necessary.
  1332     ///Creates the maps if necessary.
  1268     ///
  1333     ///\todo Better memory allocation (instead of new).
  1269     /// Creates the maps if necessary.
       
  1270     void create_maps() {
  1334     void create_maps() {
  1271       if(!_reached) {
  1335       if(!_reached) {
  1272         local_reached = true;
  1336         local_reached = true;
  1273         _reached = Traits::createReachedMap(*_digraph);
  1337         _reached = Traits::createReachedMap(*_digraph);
  1274       }
  1338       }
  1291       static ReachedMap *createReachedMap(const Digraph &digraph) {
  1355       static ReachedMap *createReachedMap(const Digraph &digraph) {
  1292         throw UninitializedParameter();
  1356         throw UninitializedParameter();
  1293       }
  1357       }
  1294     };
  1358     };
  1295     /// \brief \ref named-templ-param "Named parameter" for setting
  1359     /// \brief \ref named-templ-param "Named parameter" for setting
  1296     /// ReachedMap type
  1360     /// ReachedMap type.
  1297     ///
  1361     ///
  1298     /// \ref named-templ-param "Named parameter" for setting ReachedMap type
  1362     /// \ref named-templ-param "Named parameter" for setting ReachedMap type.
  1299     template <class T>
  1363     template <class T>
  1300     struct DefReachedMap : public BfsVisit< Digraph, Visitor,
  1364     struct DefReachedMap : public BfsVisit< Digraph, Visitor,
  1301                                             DefReachedMapTraits<T> > {
  1365                                             DefReachedMapTraits<T> > {
  1302       typedef BfsVisit< Digraph, Visitor, DefReachedMapTraits<T> > Create;
  1366       typedef BfsVisit< Digraph, Visitor, DefReachedMapTraits<T> > Create;
  1303     };
  1367     };
  1307 
  1371 
  1308     /// \brief Constructor.
  1372     /// \brief Constructor.
  1309     ///
  1373     ///
  1310     /// Constructor.
  1374     /// Constructor.
  1311     ///
  1375     ///
  1312     /// \param digraph the digraph the algorithm will run on.
  1376     /// \param digraph The digraph the algorithm runs on.
  1313     /// \param visitor The visitor of the algorithm.
  1377     /// \param visitor The visitor object of the algorithm.
  1314     ///
       
  1315     BfsVisit(const Digraph& digraph, Visitor& visitor)
  1378     BfsVisit(const Digraph& digraph, Visitor& visitor)
  1316       : _digraph(&digraph), _visitor(&visitor),
  1379       : _digraph(&digraph), _visitor(&visitor),
  1317         _reached(0), local_reached(false) {}
  1380         _reached(0), local_reached(false) {}
  1318 
  1381 
  1319     /// \brief Destructor.
  1382     /// \brief Destructor.
  1320     ///
       
  1321     /// Destructor.
       
  1322     ~BfsVisit() {
  1383     ~BfsVisit() {
  1323       if(local_reached) delete _reached;
  1384       if(local_reached) delete _reached;
  1324     }
  1385     }
  1325 
  1386 
  1326     /// \brief Sets the map indicating if a node is reached.
  1387     /// \brief Sets the map that indicates which nodes are reached.
  1327     ///
  1388     ///
  1328     /// Sets the map indicating if a node is reached.
  1389     /// Sets the map that indicates which nodes are reached.
  1329     /// If you don't use this function before calling \ref run(),
  1390     /// If you don't use this function before calling \ref run(),
  1330     /// it will allocate one. The destuctor deallocates this
  1391     /// it will allocate one. The destructor deallocates this
  1331     /// automatically allocated map, of course.
  1392     /// automatically allocated map, of course.
  1332     /// \return <tt> (*this) </tt>
  1393     /// \return <tt> (*this) </tt>
  1333     BfsVisit &reachedMap(ReachedMap &m) {
  1394     BfsVisit &reachedMap(ReachedMap &m) {
  1334       if(local_reached) {
  1395       if(local_reached) {
  1335         delete _reached;
  1396         delete _reached;
  1338       _reached = &m;
  1399       _reached = &m;
  1339       return *this;
  1400       return *this;
  1340     }
  1401     }
  1341 
  1402 
  1342   public:
  1403   public:
       
  1404 
  1343     /// \name Execution control
  1405     /// \name Execution control
  1344     /// The simplest way to execute the algorithm is to use
  1406     /// The simplest way to execute the algorithm is to use
  1345     /// one of the member functions called \c run(...).
  1407     /// one of the member functions called \ref lemon::BfsVisit::run()
       
  1408     /// "run()".
  1346     /// \n
  1409     /// \n
  1347     /// If you need more control on the execution,
  1410     /// If you need more control on the execution, first you must call
  1348     /// first you must call \ref init(), then you can adda source node
  1411     /// \ref lemon::BfsVisit::init() "init()", then you can add several
  1349     /// with \ref addSource().
  1412     /// source nodes with \ref lemon::BfsVisit::addSource() "addSource()".
  1350     /// Finally \ref start() will perform the actual path
  1413     /// Finally \ref lemon::BfsVisit::start() "start()" will perform the
  1351     /// computation.
  1414     /// actual path computation.
  1352 
  1415 
  1353     /// @{
  1416     /// @{
       
  1417 
  1354     /// \brief Initializes the internal data structures.
  1418     /// \brief Initializes the internal data structures.
  1355     ///
  1419     ///
  1356     /// Initializes the internal data structures.
  1420     /// Initializes the internal data structures.
  1357     ///
       
  1358     void init() {
  1421     void init() {
  1359       create_maps();
  1422       create_maps();
  1360       _list.resize(countNodes(*_digraph));
  1423       _list.resize(countNodes(*_digraph));
  1361       _list_front = _list_back = -1;
  1424       _list_front = _list_back = -1;
  1362       for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
  1425       for (NodeIt u(*_digraph) ; u != INVALID ; ++u) {
  1380     ///
  1443     ///
  1381     /// Processes the next node.
  1444     /// Processes the next node.
  1382     ///
  1445     ///
  1383     /// \return The processed node.
  1446     /// \return The processed node.
  1384     ///
  1447     ///
  1385     /// \pre The queue must not be empty!
  1448     /// \pre The queue must not be empty.
  1386     Node processNextNode() {
  1449     Node processNextNode() {
  1387       Node n = _list[++_list_front];
  1450       Node n = _list[++_list_front];
  1388       _visitor->process(n);
  1451       _visitor->process(n);
  1389       Arc e;
  1452       Arc e;
  1390       for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
  1453       for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
  1401       return n;
  1464       return n;
  1402     }
  1465     }
  1403 
  1466 
  1404     /// \brief Processes the next node.
  1467     /// \brief Processes the next node.
  1405     ///
  1468     ///
  1406     /// Processes the next node. And checks that the given target node
  1469     /// Processes the next node and checks if the given target node
  1407     /// is reached. If the target node is reachable from the processed
  1470     /// is reached. If the target node is reachable from the processed
  1408     /// node then the reached parameter will be set true. The reached
  1471     /// node, then the \c reach parameter will be set to \c true.
  1409     /// parameter should be initially false.
       
  1410     ///
  1472     ///
  1411     /// \param target The target node.
  1473     /// \param target The target node.
  1412     /// \retval reach Indicates that the target node is reached.
  1474     /// \retval reach Indicates if the target node is reached.
       
  1475     /// It should be initially \c false.
       
  1476     ///
  1413     /// \return The processed node.
  1477     /// \return The processed node.
  1414     ///
  1478     ///
  1415     /// \warning The queue must not be empty!
  1479     /// \pre The queue must not be empty.
  1416     Node processNextNode(Node target, bool& reach) {
  1480     Node processNextNode(Node target, bool& reach) {
  1417       Node n = _list[++_list_front];
  1481       Node n = _list[++_list_front];
  1418       _visitor->process(n);
  1482       _visitor->process(n);
  1419       Arc e;
  1483       Arc e;
  1420       for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
  1484       for (_digraph->firstOut(e, n); e != INVALID; _digraph->nextOut(e)) {
  1432       return n;
  1496       return n;
  1433     }
  1497     }
  1434 
  1498 
  1435     /// \brief Processes the next node.
  1499     /// \brief Processes the next node.
  1436     ///
  1500     ///
  1437     /// Processes the next node. And checks that at least one of
  1501     /// Processes the next node and checks if at least one of reached
  1438     /// reached node has true value in the \c nm node map. If one node
  1502     /// nodes has \c true value in the \c nm node map. If one node
  1439     /// with true value is reachable from the processed node then the
  1503     /// with \c true value is reachable from the processed node, then the
  1440     /// rnode parameter will be set to the first of such nodes.
  1504     /// \c rnode parameter will be set to the first of such nodes.
  1441     ///
  1505     ///
  1442     /// \param nm The node map of possible targets.
  1506     /// \param nm A \c bool (or convertible) node map that indicates the
       
  1507     /// possible targets.
  1443     /// \retval rnode The reached target node.
  1508     /// \retval rnode The reached target node.
       
  1509     /// It should be initially \c INVALID.
       
  1510     ///
  1444     /// \return The processed node.
  1511     /// \return The processed node.
  1445     ///
  1512     ///
  1446     /// \warning The queue must not be empty!
  1513     /// \pre The queue must not be empty.
  1447     template <typename NM>
  1514     template <typename NM>
  1448     Node processNextNode(const NM& nm, Node& rnode) {
  1515     Node processNextNode(const NM& nm, Node& rnode) {
  1449       Node n = _list[++_list_front];
  1516       Node n = _list[++_list_front];
  1450       _visitor->process(n);
  1517       _visitor->process(n);
  1451       Arc e;
  1518       Arc e;
  1462         }
  1529         }
  1463       }
  1530       }
  1464       return n;
  1531       return n;
  1465     }
  1532     }
  1466 
  1533 
  1467     /// \brief Next node to be processed.
  1534     /// \brief The next node to be processed.
  1468     ///
  1535     ///
  1469     /// Next node to be processed.
  1536     /// Returns the next node to be processed or \c INVALID if the queue
  1470     ///
  1537     /// is empty.
  1471     /// \return The next node to be processed or INVALID if the stack is
  1538     Node nextNode() const {
  1472     /// empty.
       
  1473     Node nextNode() {
       
  1474       return _list_front != _list_back ? _list[_list_front + 1] : INVALID;
  1539       return _list_front != _list_back ? _list[_list_front + 1] : INVALID;
  1475     }
  1540     }
  1476 
  1541 
  1477     /// \brief Returns \c false if there are nodes
  1542     /// \brief Returns \c false if there are nodes
  1478     /// to be processed in the queue
  1543     /// to be processed.
  1479     ///
  1544     ///
  1480     /// Returns \c false if there are nodes
  1545     /// Returns \c false if there are nodes
  1481     /// to be processed in the queue
  1546     /// to be processed in the queue.
  1482     bool emptyQueue() { return _list_front == _list_back; }
  1547     bool emptyQueue() const { return _list_front == _list_back; }
  1483 
  1548 
  1484     /// \brief Returns the number of the nodes to be processed.
  1549     /// \brief Returns the number of the nodes to be processed.
  1485     ///
  1550     ///
  1486     /// Returns the number of the nodes to be processed in the queue.
  1551     /// Returns the number of the nodes to be processed in the queue.
  1487     int queueSize() { return _list_back - _list_front; }
  1552     int queueSize() const { return _list_back - _list_front; }
  1488 
  1553 
  1489     /// \brief Executes the algorithm.
  1554     /// \brief Executes the algorithm.
  1490     ///
  1555     ///
  1491     /// Executes the algorithm.
  1556     /// Executes the algorithm.
  1492     ///
  1557     ///
  1493     /// \pre init() must be called and at least one node should be added
  1558     /// This method runs the %BFS algorithm from the root node(s)
       
  1559     /// in order to compute the shortest path to each node.
       
  1560     ///
       
  1561     /// The algorithm computes
       
  1562     /// - the shortest path tree (forest),
       
  1563     /// - the distance of each node from the root(s).
       
  1564     ///
       
  1565     /// \pre init() must be called and at least one root node should be added
  1494     /// with addSource() before using this function.
  1566     /// with addSource() before using this function.
       
  1567     ///
       
  1568     /// \note <tt>b.start()</tt> is just a shortcut of the following code.
       
  1569     /// \code
       
  1570     ///   while ( !b.emptyQueue() ) {
       
  1571     ///     b.processNextNode();
       
  1572     ///   }
       
  1573     /// \endcode
  1495     void start() {
  1574     void start() {
  1496       while ( !emptyQueue() ) processNextNode();
  1575       while ( !emptyQueue() ) processNextNode();
  1497     }
  1576     }
  1498 
  1577 
  1499     /// \brief Executes the algorithm until \c dest is reached.
  1578     /// \brief Executes the algorithm until the given target node is reached.
  1500     ///
  1579     ///
  1501     /// Executes the algorithm until \c dest is reached.
  1580     /// Executes the algorithm until the given target node is reached.
  1502     ///
  1581     ///
  1503     /// \pre init() must be called and at least one node should be added
  1582     /// This method runs the %BFS algorithm from the root node(s)
  1504     /// with addSource() before using this function.
  1583     /// in order to compute the shortest path to \c dest.
       
  1584     ///
       
  1585     /// The algorithm computes
       
  1586     /// - the shortest path to \c dest,
       
  1587     /// - the distance of \c dest from the root(s).
       
  1588     ///
       
  1589     /// \pre init() must be called and at least one root node should be
       
  1590     /// added with addSource() before using this function.
       
  1591     ///
       
  1592     /// \note <tt>b.start(t)</tt> is just a shortcut of the following code.
       
  1593     /// \code
       
  1594     ///   bool reach = false;
       
  1595     ///   while ( !b.emptyQueue() && !reach ) {
       
  1596     ///     b.processNextNode(t, reach);
       
  1597     ///   }
       
  1598     /// \endcode
  1505     void start(Node dest) {
  1599     void start(Node dest) {
  1506       bool reach = false;
  1600       bool reach = false;
  1507       while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
  1601       while ( !emptyQueue() && !reach ) processNextNode(dest, reach);
  1508     }
  1602     }
  1509 
  1603 
  1510     /// \brief Executes the algorithm until a condition is met.
  1604     /// \brief Executes the algorithm until a condition is met.
  1511     ///
  1605     ///
  1512     /// Executes the algorithm until a condition is met.
  1606     /// Executes the algorithm until a condition is met.
  1513     ///
  1607     ///
  1514     /// \pre init() must be called and at least one node should be added
  1608     /// This method runs the %BFS algorithm from the root node(s) in
  1515     /// with addSource() before using this function.
  1609     /// order to compute the shortest path to a node \c v with
  1516     ///
  1610     /// <tt>nm[v]</tt> true, if such a node can be found.
  1517     ///\param nm must be a bool (or convertible) node map. The
  1611     ///
  1518     ///algorithm will stop when it reaches a node \c v with
  1612     /// \param nm must be a bool (or convertible) node map. The
       
  1613     /// algorithm will stop when it reaches a node \c v with
  1519     /// <tt>nm[v]</tt> true.
  1614     /// <tt>nm[v]</tt> true.
  1520     ///
  1615     ///
  1521     ///\return The reached node \c v with <tt>nm[v]</tt> true or
  1616     /// \return The reached node \c v with <tt>nm[v]</tt> true or
  1522     ///\c INVALID if no such node was found.
  1617     /// \c INVALID if no such node was found.
       
  1618     ///
       
  1619     /// \pre init() must be called and at least one root node should be
       
  1620     /// added with addSource() before using this function.
       
  1621     ///
       
  1622     /// \note <tt>b.start(nm)</tt> is just a shortcut of the following code.
       
  1623     /// \code
       
  1624     ///   Node rnode = INVALID;
       
  1625     ///   while ( !b.emptyQueue() && rnode == INVALID ) {
       
  1626     ///     b.processNextNode(nm, rnode);
       
  1627     ///   }
       
  1628     ///   return rnode;
       
  1629     /// \endcode
  1523     template <typename NM>
  1630     template <typename NM>
  1524     Node start(const NM &nm) {
  1631     Node start(const NM &nm) {
  1525       Node rnode = INVALID;
  1632       Node rnode = INVALID;
  1526       while ( !emptyQueue() && rnode == INVALID ) {
  1633       while ( !emptyQueue() && rnode == INVALID ) {
  1527         processNextNode(nm, rnode);
  1634         processNextNode(nm, rnode);
  1528       }
  1635       }
  1529       return rnode;
  1636       return rnode;
  1530     }
  1637     }
  1531 
  1638 
  1532     /// \brief Runs %BFSVisit algorithm from node \c s.
  1639     /// \brief Runs the algorithm from the given node.
  1533     ///
  1640     ///
  1534     /// This method runs the %BFS algorithm from a root node \c s.
  1641     /// This method runs the %BFS algorithm from node \c s
  1535     /// \note b.run(s) is just a shortcut of the following code.
  1642     /// in order to compute the shortest path to each node.
       
  1643     ///
       
  1644     /// The algorithm computes
       
  1645     /// - the shortest path tree,
       
  1646     /// - the distance of each node from the root.
       
  1647     ///
       
  1648     /// \note <tt>b.run(s)</tt> is just a shortcut of the following code.
  1536     ///\code
  1649     ///\code
  1537     ///   b.init();
  1650     ///   b.init();
  1538     ///   b.addSource(s);
  1651     ///   b.addSource(s);
  1539     ///   b.start();
  1652     ///   b.start();
  1540     ///\endcode
  1653     ///\endcode
  1542       init();
  1655       init();
  1543       addSource(s);
  1656       addSource(s);
  1544       start();
  1657       start();
  1545     }
  1658     }
  1546 
  1659 
  1547     /// \brief Runs %BFSVisit algorithm to visit all nodes in the digraph.
  1660     /// \brief Runs the algorithm to visit all nodes in the digraph.
  1548     ///
  1661     ///
  1549     /// This method runs the %BFS algorithm in order to
  1662     /// This method runs the %BFS algorithm in order to
  1550     /// compute the %BFS path to each node. The algorithm computes
  1663     /// compute the shortest path to each node.
  1551     /// - The %BFS tree.
  1664     ///
  1552     /// - The distance of each node from the root in the %BFS tree.
  1665     /// The algorithm computes
  1553     ///
  1666     /// - the shortest path tree (forest),
  1554     ///\note b.run() is just a shortcut of the following code.
  1667     /// - the distance of each node from the root(s).
       
  1668     ///
       
  1669     /// \note <tt>b.run(s)</tt> is just a shortcut of the following code.
  1555     ///\code
  1670     ///\code
  1556     ///  b.init();
  1671     ///  b.init();
  1557     ///  for (NodeIt it(digraph); it != INVALID; ++it) {
  1672     ///  for (NodeIt n(gr); n != INVALID; ++n) {
  1558     ///    if (!b.reached(it)) {
  1673     ///    if (!b.reached(n)) {
  1559     ///      b.addSource(it);
  1674     ///      b.addSource(n);
  1560     ///      b.start();
  1675     ///      b.start();
  1561     ///    }
  1676     ///    }
  1562     ///  }
  1677     ///  }
  1563     ///\endcode
  1678     ///\endcode
  1564     void run() {
  1679     void run() {
  1568           addSource(it);
  1683           addSource(it);
  1569           start();
  1684           start();
  1570         }
  1685         }
  1571       }
  1686       }
  1572     }
  1687     }
       
  1688 
  1573     ///@}
  1689     ///@}
  1574 
  1690 
  1575     /// \name Query Functions
  1691     /// \name Query Functions
  1576     /// The result of the %BFS algorithm can be obtained using these
  1692     /// The result of the %BFS algorithm can be obtained using these
  1577     /// functions.\n
  1693     /// functions.\n
  1578     /// Before the use of these functions,
  1694     /// Either \ref lemon::BfsVisit::run() "run()" or
  1579     /// either run() or start() must be called.
  1695     /// \ref lemon::BfsVisit::start() "start()" must be called before
       
  1696     /// using them.
  1580     ///@{
  1697     ///@{
  1581 
  1698 
  1582     /// \brief Checks if a node is reachable from the root.
  1699     /// \brief Checks if a node is reachable from the root(s).
  1583     ///
  1700     ///
  1584     /// Returns \c true if \c v is reachable from the root(s).
  1701     /// Returns \c true if \c v is reachable from the root(s).
  1585     /// \warning The source nodes are inditated as unreachable.
       
  1586     /// \pre Either \ref run() or \ref start()
  1702     /// \pre Either \ref run() or \ref start()
  1587     /// must be called before using this function.
  1703     /// must be called before using this function.
  1588     ///
       
  1589     bool reached(Node v) { return (*_reached)[v]; }
  1704     bool reached(Node v) { return (*_reached)[v]; }
       
  1705 
  1590     ///@}
  1706     ///@}
       
  1707 
  1591   };
  1708   };
  1592 
  1709 
  1593 } //END OF NAMESPACE LEMON
  1710 } //END OF NAMESPACE LEMON
  1594 
  1711 
  1595 #endif
  1712 #endif
  1596