lemon/bfs.h
author ladanyi
Mon, 20 Nov 2006 10:14:21 +0000
changeset 2304 108d6db4f32a
parent 2260 4274224f8a7d
child 2306 42cce226b87b
permissions -rw-r--r--
Doc fix.
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2006
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #ifndef LEMON_BFS_H
    20 #define LEMON_BFS_H
    21 
    22 ///\ingroup flowalgs
    23 ///\file
    24 ///\brief Bfs algorithm.
    25 
    26 #include <lemon/list_graph.h>
    27 #include <lemon/graph_utils.h>
    28 #include <lemon/bits/invalid.h>
    29 #include <lemon/error.h>
    30 #include <lemon/maps.h>
    31 
    32 namespace lemon {
    33 
    34 
    35   
    36   ///Default traits class of Bfs class.
    37 
    38   ///Default traits class of Bfs class.
    39   ///\param GR Graph type.
    40   template<class GR>
    41   struct BfsDefaultTraits
    42   {
    43     ///The graph type the algorithm runs on. 
    44     typedef GR Graph;
    45     ///\brief The type of the map that stores the last
    46     ///edges of the shortest paths.
    47     /// 
    48     ///The type of the map that stores the last
    49     ///edges of the shortest paths.
    50     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    51     ///
    52     typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
    53     ///Instantiates a PredMap.
    54  
    55     ///This function instantiates a \ref PredMap. 
    56     ///\param G is the graph, to which we would like to define the PredMap.
    57     ///\todo The graph alone may be insufficient to initialize
    58     static PredMap *createPredMap(const GR &G) 
    59     {
    60       return new PredMap(G);
    61     }
    62     ///The type of the map that indicates which nodes are processed.
    63  
    64     ///The type of the map that indicates which nodes are processed.
    65     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    66     ///\todo named parameter to set this type, function to read and write.
    67     typedef NullMap<typename Graph::Node,bool> ProcessedMap;
    68     ///Instantiates a ProcessedMap.
    69  
    70     ///This function instantiates a \ref ProcessedMap. 
    71     ///\param g is the graph, to which
    72     ///we would like to define the \ref ProcessedMap
    73 #ifdef DOXYGEN
    74     static ProcessedMap *createProcessedMap(const GR &g)
    75 #else
    76     static ProcessedMap *createProcessedMap(const GR &)
    77 #endif
    78     {
    79       return new ProcessedMap();
    80     }
    81     ///The type of the map that indicates which nodes are reached.
    82  
    83     ///The type of the map that indicates which nodes are reached.
    84     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
    85     ///\todo named parameter to set this type, function to read and write.
    86     typedef typename Graph::template NodeMap<bool> ReachedMap;
    87     ///Instantiates a ReachedMap.
    88  
    89     ///This function instantiates a \ref ReachedMap. 
    90     ///\param G is the graph, to which
    91     ///we would like to define the \ref ReachedMap.
    92     static ReachedMap *createReachedMap(const GR &G)
    93     {
    94       return new ReachedMap(G);
    95     }
    96     ///The type of the map that stores the dists of the nodes.
    97  
    98     ///The type of the map that stores the dists of the nodes.
    99     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   100     ///
   101     typedef typename Graph::template NodeMap<int> DistMap;
   102     ///Instantiates a DistMap.
   103  
   104     ///This function instantiates a \ref DistMap. 
   105     ///\param G is the graph, to which we would like to define the \ref DistMap
   106     static DistMap *createDistMap(const GR &G)
   107     {
   108       return new DistMap(G);
   109     }
   110   };
   111   
   112   ///%BFS algorithm class.
   113   
   114   ///\ingroup flowalgs
   115   ///This class provides an efficient implementation of the %BFS algorithm.
   116   ///
   117   ///\param GR The graph type the algorithm runs on. The default value is
   118   ///\ref ListGraph. The value of GR is not used directly by Bfs, it
   119   ///is only passed to \ref BfsDefaultTraits.
   120   ///\param TR Traits class to set various data types used by the algorithm.
   121   ///The default traits class is
   122   ///\ref BfsDefaultTraits "BfsDefaultTraits<GR>".
   123   ///See \ref BfsDefaultTraits for the documentation of
   124   ///a Bfs traits class.
   125   ///
   126   ///\author Alpar Juttner
   127 
   128 #ifdef DOXYGEN
   129   template <typename GR,
   130 	    typename TR>
   131 #else
   132   template <typename GR=ListGraph,
   133 	    typename TR=BfsDefaultTraits<GR> >
   134 #endif
   135   class Bfs {
   136   public:
   137     /**
   138      * \brief \ref Exception for uninitialized parameters.
   139      *
   140      * This error represents problems in the initialization
   141      * of the parameters of the algorithms.
   142      */
   143     class UninitializedParameter : public lemon::UninitializedParameter {
   144     public:
   145       virtual const char* what() const throw() {
   146 	return "lemon::Bfs::UninitializedParameter";
   147       }
   148     };
   149 
   150     typedef TR Traits;
   151     ///The type of the underlying graph.
   152     typedef typename TR::Graph Graph;
   153     ///\e
   154     typedef typename Graph::Node Node;
   155     ///\e
   156     typedef typename Graph::NodeIt NodeIt;
   157     ///\e
   158     typedef typename Graph::Edge Edge;
   159     ///\e
   160     typedef typename Graph::OutEdgeIt OutEdgeIt;
   161     
   162     ///\brief The type of the map that stores the last
   163     ///edges of the shortest paths.
   164     typedef typename TR::PredMap PredMap;
   165     ///The type of the map indicating which nodes are reached.
   166     typedef typename TR::ReachedMap ReachedMap;
   167     ///The type of the map indicating which nodes are processed.
   168     typedef typename TR::ProcessedMap ProcessedMap;
   169     ///The type of the map that stores the dists of the nodes.
   170     typedef typename TR::DistMap DistMap;
   171   private:
   172     /// Pointer to the underlying graph.
   173     const Graph *G;
   174     ///Pointer to the map of predecessors edges.
   175     PredMap *_pred;
   176     ///Indicates if \ref _pred is locally allocated (\c true) or not.
   177     bool local_pred;
   178     ///Pointer to the map of distances.
   179     DistMap *_dist;
   180     ///Indicates if \ref _dist is locally allocated (\c true) or not.
   181     bool local_dist;
   182     ///Pointer to the map of reached status of the nodes.
   183     ReachedMap *_reached;
   184     ///Indicates if \ref _reached is locally allocated (\c true) or not.
   185     bool local_reached;
   186     ///Pointer to the map of processed status of the nodes.
   187     ProcessedMap *_processed;
   188     ///Indicates if \ref _processed is locally allocated (\c true) or not.
   189     bool local_processed;
   190 
   191     std::vector<typename Graph::Node> _queue;
   192     int _queue_head,_queue_tail,_queue_next_dist;
   193     int _curr_dist;
   194 
   195     ///Creates the maps if necessary.
   196     
   197     ///\todo Better memory allocation (instead of new).
   198     void create_maps() 
   199     {
   200       if(!_pred) {
   201 	local_pred = true;
   202 	_pred = Traits::createPredMap(*G);
   203       }
   204       if(!_dist) {
   205 	local_dist = true;
   206 	_dist = Traits::createDistMap(*G);
   207       }
   208       if(!_reached) {
   209 	local_reached = true;
   210 	_reached = Traits::createReachedMap(*G);
   211       }
   212       if(!_processed) {
   213 	local_processed = true;
   214 	_processed = Traits::createProcessedMap(*G);
   215       }
   216     }
   217 
   218   protected:
   219     
   220     Bfs() {}
   221     
   222   public:
   223  
   224     typedef Bfs Create;
   225 
   226     ///\name Named template parameters
   227 
   228     ///@{
   229 
   230     template <class T>
   231     struct DefPredMapTraits : public Traits {
   232       typedef T PredMap;
   233       static PredMap *createPredMap(const Graph &) 
   234       {
   235 	throw UninitializedParameter();
   236       }
   237     };
   238     ///\ref named-templ-param "Named parameter" for setting PredMap type
   239 
   240     ///\ref named-templ-param "Named parameter" for setting PredMap type
   241     ///
   242     template <class T>
   243     struct DefPredMap : public Bfs< Graph, DefPredMapTraits<T> > { 
   244       typedef Bfs< Graph, DefPredMapTraits<T> > Create;
   245     };
   246     
   247     template <class T>
   248     struct DefDistMapTraits : public Traits {
   249       typedef T DistMap;
   250       static DistMap *createDistMap(const Graph &) 
   251       {
   252 	throw UninitializedParameter();
   253       }
   254     };
   255     ///\ref named-templ-param "Named parameter" for setting DistMap type
   256 
   257     ///\ref named-templ-param "Named parameter" for setting DistMap type
   258     ///
   259     template <class T>
   260     struct DefDistMap : public Bfs< Graph, DefDistMapTraits<T> > { 
   261       typedef Bfs< Graph, DefDistMapTraits<T> > Create;
   262     };
   263     
   264     template <class T>
   265     struct DefReachedMapTraits : public Traits {
   266       typedef T ReachedMap;
   267       static ReachedMap *createReachedMap(const Graph &) 
   268       {
   269 	throw UninitializedParameter();
   270       }
   271     };
   272     ///\ref named-templ-param "Named parameter" for setting ReachedMap type
   273 
   274     ///\ref named-templ-param "Named parameter" for setting ReachedMap type
   275     ///
   276     template <class T>
   277     struct DefReachedMap : public Bfs< Graph, DefReachedMapTraits<T> > { 
   278       typedef Bfs< Graph, DefReachedMapTraits<T> > Create;
   279     };
   280     
   281     template <class T>
   282     struct DefProcessedMapTraits : public Traits {
   283       typedef T ProcessedMap;
   284       static ProcessedMap *createProcessedMap(const Graph &) 
   285       {
   286 	throw UninitializedParameter();
   287       }
   288     };
   289     ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
   290 
   291     ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
   292     ///
   293     template <class T>
   294     struct DefProcessedMap : public Bfs< Graph, DefProcessedMapTraits<T> > {
   295       typedef Bfs< Graph, DefProcessedMapTraits<T> > Create;
   296     };
   297     
   298     struct DefGraphProcessedMapTraits : public Traits {
   299       typedef typename Graph::template NodeMap<bool> ProcessedMap;
   300       static ProcessedMap *createProcessedMap(const Graph &G) 
   301       {
   302 	return new ProcessedMap(G);
   303       }
   304     };
   305     ///\brief \ref named-templ-param "Named parameter"
   306     ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
   307     ///
   308     ///\ref named-templ-param "Named parameter"
   309     ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
   310     ///If you don't set it explicitly, it will be automatically allocated.
   311     template <class T>
   312     struct DefProcessedMapToBeDefaultMap :
   313       public Bfs< Graph, DefGraphProcessedMapTraits> { 
   314       typedef Bfs< Graph, DefGraphProcessedMapTraits> Create;
   315     };
   316     
   317     ///@}
   318 
   319   public:      
   320     
   321     ///Constructor.
   322     
   323     ///\param _G the graph the algorithm will run on.
   324     ///
   325     Bfs(const Graph& _G) :
   326       G(&_G),
   327       _pred(NULL), local_pred(false),
   328       _dist(NULL), local_dist(false),
   329       _reached(NULL), local_reached(false),
   330       _processed(NULL), local_processed(false)
   331     { }
   332     
   333     ///Destructor.
   334     ~Bfs() 
   335     {
   336       if(local_pred) delete _pred;
   337       if(local_dist) delete _dist;
   338       if(local_reached) delete _reached;
   339       if(local_processed) delete _processed;
   340     }
   341 
   342     ///Sets the map storing the predecessor edges.
   343 
   344     ///Sets the map storing the predecessor edges.
   345     ///If you don't use this function before calling \ref run(),
   346     ///it will allocate one. The destructor deallocates this
   347     ///automatically allocated map, of course.
   348     ///\return <tt> (*this) </tt>
   349     Bfs &predMap(PredMap &m) 
   350     {
   351       if(local_pred) {
   352 	delete _pred;
   353 	local_pred=false;
   354       }
   355       _pred = &m;
   356       return *this;
   357     }
   358 
   359     ///Sets the map indicating the reached nodes.
   360 
   361     ///Sets the map indicating the reached nodes.
   362     ///If you don't use this function before calling \ref run(),
   363     ///it will allocate one. The destructor deallocates this
   364     ///automatically allocated map, of course.
   365     ///\return <tt> (*this) </tt>
   366     Bfs &reachedMap(ReachedMap &m) 
   367     {
   368       if(local_reached) {
   369 	delete _reached;
   370 	local_reached=false;
   371       }
   372       _reached = &m;
   373       return *this;
   374     }
   375 
   376     ///Sets the map indicating the processed nodes.
   377 
   378     ///Sets the map indicating the processed nodes.
   379     ///If you don't use this function before calling \ref run(),
   380     ///it will allocate one. The destructor deallocates this
   381     ///automatically allocated map, of course.
   382     ///\return <tt> (*this) </tt>
   383     Bfs &processedMap(ProcessedMap &m) 
   384     {
   385       if(local_processed) {
   386 	delete _processed;
   387 	local_processed=false;
   388       }
   389       _processed = &m;
   390       return *this;
   391     }
   392 
   393     ///Sets the map storing the distances calculated by the algorithm.
   394 
   395     ///Sets the map storing the distances calculated by the algorithm.
   396     ///If you don't use this function before calling \ref run(),
   397     ///it will allocate one. The destructor deallocates this
   398     ///automatically allocated map, of course.
   399     ///\return <tt> (*this) </tt>
   400     Bfs &distMap(DistMap &m) 
   401     {
   402       if(local_dist) {
   403 	delete _dist;
   404 	local_dist=false;
   405       }
   406       _dist = &m;
   407       return *this;
   408     }
   409 
   410   public:
   411     ///\name Execution control
   412     ///The simplest way to execute the algorithm is to use
   413     ///one of the member functions called \c run(...).
   414     ///\n
   415     ///If you need more control on the execution,
   416     ///first you must call \ref init(), then you can add several source nodes
   417     ///with \ref addSource().
   418     ///Finally \ref start() will perform the actual path
   419     ///computation.
   420 
   421     ///@{
   422 
   423     ///Initializes the internal data structures.
   424 
   425     ///Initializes the internal data structures.
   426     ///
   427     void init()
   428     {
   429       create_maps();
   430       _queue.resize(countNodes(*G));
   431       _queue_head=_queue_tail=0;
   432       _curr_dist=1;
   433       for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
   434 	_pred->set(u,INVALID);
   435 	_reached->set(u,false);
   436 	_processed->set(u,false);
   437       }
   438     }
   439     
   440     ///Adds a new source node.
   441 
   442     ///Adds a new source node to the set of nodes to be processed.
   443     ///
   444     void addSource(Node s)
   445     {
   446       if(!(*_reached)[s])
   447 	{
   448 	  _reached->set(s,true);
   449 	  _pred->set(s,INVALID);
   450 	  _dist->set(s,0);
   451 	  _queue[_queue_head++]=s;
   452 	  _queue_next_dist=_queue_head;
   453 	}
   454     }
   455     
   456     ///Processes the next node.
   457 
   458     ///Processes the next node.
   459     ///
   460     ///\return The processed node.
   461     ///
   462     ///\warning The queue must not be empty!
   463     Node processNextNode()
   464     {
   465       if(_queue_tail==_queue_next_dist) {
   466 	_curr_dist++;
   467 	_queue_next_dist=_queue_head;
   468       }
   469       Node n=_queue[_queue_tail++];
   470       _processed->set(n,true);
   471       Node m;
   472       for(OutEdgeIt e(*G,n);e!=INVALID;++e)
   473 	if(!(*_reached)[m=G->target(e)]) {
   474 	  _queue[_queue_head++]=m;
   475 	  _reached->set(m,true);
   476 	  _pred->set(m,e);
   477 	  _dist->set(m,_curr_dist);
   478 	}
   479       return n;
   480     }
   481 
   482     ///Processes the next node.
   483 
   484     ///Processes the next node. And checks that the given target node
   485     ///is reached. If the target node is reachable from the processed
   486     ///node then the reached parameter will be set true. The reached
   487     ///parameter should be initially false.
   488     ///
   489     ///\param target The target node.
   490     ///\retval reached Indicates that the target node is reached.
   491     ///\return The processed node.
   492     ///
   493     ///\warning The queue must not be empty!
   494     Node processNextNode(Node target, bool& reached)
   495     {
   496       if(_queue_tail==_queue_next_dist) {
   497 	_curr_dist++;
   498 	_queue_next_dist=_queue_head;
   499       }
   500       Node n=_queue[_queue_tail++];
   501       _processed->set(n,true);
   502       Node m;
   503       for(OutEdgeIt e(*G,n);e!=INVALID;++e)
   504 	if(!(*_reached)[m=G->target(e)]) {
   505 	  _queue[_queue_head++]=m;
   506 	  _reached->set(m,true);
   507 	  _pred->set(m,e);
   508 	  _dist->set(m,_curr_dist);
   509           reached = reached || (target == m);
   510 	}
   511       return n;
   512     }
   513 
   514     ///Processes the next node.
   515 
   516     ///Processes the next node. And checks that at least one of
   517     ///reached node has true value in the \c nm nodemap. If one node
   518     ///with true value is reachable from the processed node then the
   519     ///reached parameter will be set true. The reached parameter
   520     ///should be initially false.
   521     ///
   522     ///\param target The nodemaps of possible targets.
   523     ///\retval reached Indicates that one of the target nodes is reached.
   524     ///\return The processed node.
   525     ///
   526     ///\warning The queue must not be empty!
   527     template<class NM>
   528     Node processNextNode(const NM& nm, bool& reached)
   529     {
   530       if(_queue_tail==_queue_next_dist) {
   531 	_curr_dist++;
   532 	_queue_next_dist=_queue_head;
   533       }
   534       Node n=_queue[_queue_tail++];
   535       _processed->set(n,true);
   536       Node m;
   537       for(OutEdgeIt e(*G,n);e!=INVALID;++e)
   538 	if(!(*_reached)[m=G->target(e)]) {
   539 	  _queue[_queue_head++]=m;
   540 	  _reached->set(m,true);
   541 	  _pred->set(m,e);
   542 	  _dist->set(m,_curr_dist);
   543           reached = reached || nm[m];
   544 	}
   545       return n;
   546     }
   547       
   548     ///Next node to be processed.
   549 
   550     ///Next node to be processed.
   551     ///
   552     ///\return The next node to be processed or INVALID if the queue is
   553     /// empty.
   554     Node nextNode()
   555     { 
   556       return _queue_tail<_queue_head?_queue[_queue_tail]:INVALID;
   557     }
   558  
   559     ///\brief Returns \c false if there are nodes
   560     ///to be processed in the queue
   561     ///
   562     ///Returns \c false if there are nodes
   563     ///to be processed in the queue
   564     bool emptyQueue() { return _queue_tail==_queue_head; }
   565     ///Returns the number of the nodes to be processed.
   566     
   567     ///Returns the number of the nodes to be processed in the queue.
   568     ///
   569     int queueSize() { return _queue_head-_queue_tail; }
   570     
   571     ///Executes the algorithm.
   572 
   573     ///Executes the algorithm.
   574     ///
   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)
   579     ///in order to
   580     ///compute the
   581     ///shortest path to each node. The algorithm computes
   582     ///- The shortest path tree.
   583     ///- The distance of each node from the root(s).
   584     ///
   585     void start()
   586     {
   587       while ( !emptyQueue() ) processNextNode();
   588     }
   589     
   590     ///Executes the algorithm until \c dest is reached.
   591 
   592     ///Executes the algorithm until \c dest is reached.
   593     ///
   594     ///\pre init() must be called and at least one node should be added
   595     ///with addSource() before using this function.
   596     ///
   597     ///This method runs the %BFS algorithm from the root node(s)
   598     ///in order to
   599     ///compute the
   600     ///shortest path to \c dest. The algorithm computes
   601     ///- The shortest path to \c  dest.
   602     ///- The distance of \c dest from the root(s).
   603     ///
   604     void start(Node dest)
   605     {
   606       bool reached = false;
   607       while ( !emptyQueue() && !reached) processNextNode(dest, reached);
   608     }
   609     
   610     ///Executes the algorithm until a condition is met.
   611 
   612     ///Executes the algorithm until a condition is met.
   613     ///
   614     ///\pre init() must be called and at least one node should be added
   615     ///with addSource() before using this function.
   616     ///
   617     ///\param nm must be a bool (or convertible) node map. The algorithm
   618     ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
   619     ///\todo query the reached target
   620     template<class NM>
   621     void start(const NM &nm)
   622     {
   623       bool reached = false;
   624       while ( !emptyQueue() && !reached) processNextNode(nm, reached);
   625     }
   626     
   627     ///Runs %BFS algorithm from node \c s.
   628     
   629     ///This method runs the %BFS algorithm from a root node \c s
   630     ///in order to
   631     ///compute the
   632     ///shortest path to each node. The algorithm computes
   633     ///- The shortest path tree.
   634     ///- The distance of each node from the root.
   635     ///
   636     ///\note d.run(s) is just a shortcut of the following code.
   637     ///\code
   638     ///  d.init();
   639     ///  d.addSource(s);
   640     ///  d.start();
   641     ///\endcode
   642     void run(Node s) {
   643       init();
   644       addSource(s);
   645       start();
   646     }
   647     
   648     ///Finds the shortest path between \c s and \c t.
   649     
   650     ///Finds the shortest path between \c s and \c t.
   651     ///
   652     ///\return The length of the shortest s---t path if there exists one,
   653     ///0 otherwise.
   654     ///\note Apart from the return value, d.run(s) is
   655     ///just a shortcut of the following code.
   656     ///\code
   657     ///  d.init();
   658     ///  d.addSource(s);
   659     ///  d.start(t);
   660     ///\endcode
   661     int run(Node s,Node t) {
   662       init();
   663       addSource(s);
   664       start(t);
   665       return reached(t)? _curr_dist : 0;
   666     }
   667     
   668     ///@}
   669 
   670     ///\name Query Functions
   671     ///The result of the %BFS algorithm can be obtained using these
   672     ///functions.\n
   673     ///Before the use of these functions,
   674     ///either run() or start() must be called.
   675     
   676     ///@{
   677 
   678     ///Copies the shortest path to \c t into \c p
   679     
   680     ///This function copies the shortest path to \c t into \c p.
   681     ///If \c t is a source itself or unreachable, then it does not
   682     ///alter \c p.
   683     ///\return Returns \c true if a path to \c t was actually copied to \c p,
   684     ///\c false otherwise.
   685     ///\sa DirPath
   686     template<class P>
   687     bool getPath(P &p,Node t) 
   688     {
   689       if(reached(t)) {
   690 	p.clear();
   691 	typename P::Builder b(p);
   692 	for(b.setStartNode(t);predEdge(t)!=INVALID;t=predNode(t))
   693 	  b.pushFront(predEdge(t));
   694 	b.commit();
   695 	return true;
   696       }
   697       return false;
   698     }
   699 
   700     ///The distance of a node from the root(s).
   701 
   702     ///Returns the distance of a node from the root(s).
   703     ///\pre \ref run() must be called before using this function.
   704     ///\warning If node \c v in unreachable from the root(s) the return value
   705     ///of this function is undefined.
   706     int dist(Node v) const { return (*_dist)[v]; }
   707 
   708     ///Returns the 'previous edge' of the shortest path tree.
   709 
   710     ///For a node \c v it returns the 'previous edge'
   711     ///of the shortest path tree,
   712     ///i.e. it returns the last edge of a shortest path from the root(s) to \c
   713     ///v. It is \ref INVALID
   714     ///if \c v is unreachable from the root(s) or \c v is a root. The
   715     ///shortest path tree used here is equal to the shortest path tree used in
   716     ///\ref predNode().
   717     ///\pre Either \ref run() or \ref start() must be called before using
   718     ///this function.
   719     Edge predEdge(Node v) const { return (*_pred)[v];}
   720 
   721     ///Returns the 'previous node' of the shortest path tree.
   722 
   723     ///For a node \c v it returns the 'previous node'
   724     ///of the shortest path tree,
   725     ///i.e. it returns the last but one node from a shortest path from the
   726     ///root(a) to \c /v.
   727     ///It is INVALID if \c v is unreachable from the root(s) or
   728     ///if \c v itself a root.
   729     ///The shortest path tree used here is equal to the shortest path
   730     ///tree used in \ref predEdge().
   731     ///\pre Either \ref run() or \ref start() must be called before
   732     ///using this function.
   733     Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
   734 				  G->source((*_pred)[v]); }
   735     
   736     ///Returns a reference to the NodeMap of distances.
   737 
   738     ///Returns a reference to the NodeMap of distances.
   739     ///\pre Either \ref run() or \ref init() must
   740     ///be called before using this function.
   741     const DistMap &distMap() const { return *_dist;}
   742  
   743     ///Returns a reference to the shortest path tree map.
   744 
   745     ///Returns a reference to the NodeMap of the edges of the
   746     ///shortest path tree.
   747     ///\pre Either \ref run() or \ref init()
   748     ///must be called before using this function.
   749     const PredMap &predMap() const { return *_pred;}
   750  
   751     ///Checks if a node is reachable from the root.
   752 
   753     ///Returns \c true if \c v is reachable from the root.
   754     ///\warning The source nodes are indicated as unreached.
   755     ///\pre Either \ref run() or \ref start()
   756     ///must be called before using this function.
   757     ///
   758     bool reached(Node v) { return (*_reached)[v]; }
   759     
   760     ///@}
   761   };
   762 
   763   ///Default traits class of Bfs function.
   764 
   765   ///Default traits class of Bfs function.
   766   ///\param GR Graph type.
   767   template<class GR>
   768   struct BfsWizardDefaultTraits
   769   {
   770     ///The graph type the algorithm runs on. 
   771     typedef GR Graph;
   772     ///\brief The type of the map that stores the last
   773     ///edges of the shortest paths.
   774     /// 
   775     ///The type of the map that stores the last
   776     ///edges of the shortest paths.
   777     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   778     ///
   779     typedef NullMap<typename Graph::Node,typename GR::Edge> PredMap;
   780     ///Instantiates a PredMap.
   781  
   782     ///This function instantiates a \ref PredMap. 
   783     ///\param g is the graph, to which we would like to define the PredMap.
   784     ///\todo The graph alone may be insufficient to initialize
   785 #ifdef DOXYGEN
   786     static PredMap *createPredMap(const GR &g) 
   787 #else
   788     static PredMap *createPredMap(const GR &) 
   789 #endif
   790     {
   791       return new PredMap();
   792     }
   793 
   794     ///The type of the map that indicates which nodes are processed.
   795  
   796     ///The type of the map that indicates which nodes are processed.
   797     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   798     ///\todo named parameter to set this type, function to read and write.
   799     typedef NullMap<typename Graph::Node,bool> ProcessedMap;
   800     ///Instantiates a ProcessedMap.
   801  
   802     ///This function instantiates a \ref ProcessedMap. 
   803     ///\param g is the graph, to which
   804     ///we would like to define the \ref ProcessedMap
   805 #ifdef DOXYGEN
   806     static ProcessedMap *createProcessedMap(const GR &g)
   807 #else
   808     static ProcessedMap *createProcessedMap(const GR &)
   809 #endif
   810     {
   811       return new ProcessedMap();
   812     }
   813     ///The type of the map that indicates which nodes are reached.
   814  
   815     ///The type of the map that indicates which nodes are reached.
   816     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   817     ///\todo named parameter to set this type, function to read and write.
   818     typedef typename Graph::template NodeMap<bool> ReachedMap;
   819     ///Instantiates a ReachedMap.
   820  
   821     ///This function instantiates a \ref ReachedMap. 
   822     ///\param G is the graph, to which
   823     ///we would like to define the \ref ReachedMap.
   824     static ReachedMap *createReachedMap(const GR &G)
   825     {
   826       return new ReachedMap(G);
   827     }
   828     ///The type of the map that stores the dists of the nodes.
   829  
   830     ///The type of the map that stores the dists of the nodes.
   831     ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
   832     ///
   833     typedef NullMap<typename Graph::Node,int> DistMap;
   834     ///Instantiates a DistMap.
   835  
   836     ///This function instantiates a \ref DistMap. 
   837     ///\param g is the graph, to which we would like to define the \ref DistMap
   838 #ifdef DOXYGEN
   839     static DistMap *createDistMap(const GR &g)
   840 #else
   841     static DistMap *createDistMap(const GR &)
   842 #endif
   843     {
   844       return new DistMap();
   845     }
   846   };
   847   
   848   /// Default traits used by \ref BfsWizard
   849 
   850   /// To make it easier to use Bfs algorithm
   851   ///we have created a wizard class.
   852   /// This \ref BfsWizard class needs default traits,
   853   ///as well as the \ref Bfs class.
   854   /// The \ref BfsWizardBase is a class to be the default traits of the
   855   /// \ref BfsWizard class.
   856   template<class GR>
   857   class BfsWizardBase : public BfsWizardDefaultTraits<GR>
   858   {
   859 
   860     typedef BfsWizardDefaultTraits<GR> Base;
   861   protected:
   862     /// Type of the nodes in the graph.
   863     typedef typename Base::Graph::Node Node;
   864 
   865     /// Pointer to the underlying graph.
   866     void *_g;
   867     ///Pointer to the map of reached nodes.
   868     void *_reached;
   869     ///Pointer to the map of processed nodes.
   870     void *_processed;
   871     ///Pointer to the map of predecessors edges.
   872     void *_pred;
   873     ///Pointer to the map of distances.
   874     void *_dist;
   875     ///Pointer to the source node.
   876     Node _source;
   877     
   878     public:
   879     /// Constructor.
   880     
   881     /// This constructor does not require parameters, therefore it initiates
   882     /// all of the attributes to default values (0, INVALID).
   883     BfsWizardBase() : _g(0), _reached(0), _processed(0), _pred(0),
   884 			   _dist(0), _source(INVALID) {}
   885 
   886     /// Constructor.
   887     
   888     /// This constructor requires some parameters,
   889     /// listed in the parameters list.
   890     /// Others are initiated to 0.
   891     /// \param g is the initial value of  \ref _g
   892     /// \param s is the initial value of  \ref _source
   893     BfsWizardBase(const GR &g, Node s=INVALID) :
   894       _g((void *)&g), _reached(0), _processed(0), _pred(0),
   895       _dist(0), _source(s) {}
   896 
   897   };
   898   
   899   /// A class to make the usage of Bfs algorithm easier
   900 
   901   /// This class is created to make it easier to use Bfs algorithm.
   902   /// It uses the functions and features of the plain \ref Bfs,
   903   /// but it is much simpler to use it.
   904   ///
   905   /// Simplicity means that the way to change the types defined
   906   /// in the traits class is based on functions that returns the new class
   907   /// and not on templatable built-in classes.
   908   /// When using the plain \ref Bfs
   909   /// the new class with the modified type comes from
   910   /// the original class by using the ::
   911   /// operator. In the case of \ref BfsWizard only
   912   /// a function have to be called and it will
   913   /// return the needed class.
   914   ///
   915   /// It does not have own \ref run method. When its \ref run method is called
   916   /// it initiates a plain \ref Bfs class, and calls the \ref Bfs::run
   917   /// method of it.
   918   template<class TR>
   919   class BfsWizard : public TR
   920   {
   921     typedef TR Base;
   922 
   923     ///The type of the underlying graph.
   924     typedef typename TR::Graph Graph;
   925     //\e
   926     typedef typename Graph::Node Node;
   927     //\e
   928     typedef typename Graph::NodeIt NodeIt;
   929     //\e
   930     typedef typename Graph::Edge Edge;
   931     //\e
   932     typedef typename Graph::OutEdgeIt OutEdgeIt;
   933     
   934     ///\brief The type of the map that stores
   935     ///the reached nodes
   936     typedef typename TR::ReachedMap ReachedMap;
   937     ///\brief The type of the map that stores
   938     ///the processed nodes
   939     typedef typename TR::ProcessedMap ProcessedMap;
   940     ///\brief The type of the map that stores the last
   941     ///edges of the shortest paths.
   942     typedef typename TR::PredMap PredMap;
   943     ///The type of the map that stores the dists of the nodes.
   944     typedef typename TR::DistMap DistMap;
   945 
   946 public:
   947     /// Constructor.
   948     BfsWizard() : TR() {}
   949 
   950     /// Constructor that requires parameters.
   951 
   952     /// Constructor that requires parameters.
   953     /// These parameters will be the default values for the traits class.
   954     BfsWizard(const Graph &g, Node s=INVALID) :
   955       TR(g,s) {}
   956 
   957     ///Copy constructor
   958     BfsWizard(const TR &b) : TR(b) {}
   959 
   960     ~BfsWizard() {}
   961 
   962     ///Runs Bfs algorithm from a given node.
   963     
   964     ///Runs Bfs algorithm from a given node.
   965     ///The node can be given by the \ref source function.
   966     void run()
   967     {
   968       if(Base::_source==INVALID) throw UninitializedParameter();
   969       Bfs<Graph,TR> alg(*(Graph*)Base::_g);
   970       if(Base::_reached)
   971 	alg.reachedMap(*(ReachedMap*)Base::_reached);
   972       if(Base::_processed) alg.processedMap(*(ProcessedMap*)Base::_processed);
   973       if(Base::_pred) alg.predMap(*(PredMap*)Base::_pred);
   974       if(Base::_dist) alg.distMap(*(DistMap*)Base::_dist);
   975       alg.run(Base::_source);
   976     }
   977 
   978     ///Runs Bfs algorithm from the given node.
   979 
   980     ///Runs Bfs algorithm from the given node.
   981     ///\param s is the given source.
   982     void run(Node s)
   983     {
   984       Base::_source=s;
   985       run();
   986     }
   987 
   988     template<class T>
   989     struct DefPredMapBase : public Base {
   990       typedef T PredMap;
   991       static PredMap *createPredMap(const Graph &) { return 0; };
   992       DefPredMapBase(const TR &b) : TR(b) {}
   993     };
   994     
   995     ///\brief \ref named-templ-param "Named parameter"
   996     ///function for setting PredMap
   997     ///
   998     /// \ref named-templ-param "Named parameter"
   999     ///function for setting PredMap
  1000     ///
  1001     template<class T>
  1002     BfsWizard<DefPredMapBase<T> > predMap(const T &t) 
  1003     {
  1004       Base::_pred=(void *)&t;
  1005       return BfsWizard<DefPredMapBase<T> >(*this);
  1006     }
  1007     
  1008  
  1009     template<class T>
  1010     struct DefReachedMapBase : public Base {
  1011       typedef T ReachedMap;
  1012       static ReachedMap *createReachedMap(const Graph &) { return 0; };
  1013       DefReachedMapBase(const TR &b) : TR(b) {}
  1014     };
  1015     
  1016     ///\brief \ref named-templ-param "Named parameter"
  1017     ///function for setting ReachedMap
  1018     ///
  1019     /// \ref named-templ-param "Named parameter"
  1020     ///function for setting ReachedMap
  1021     ///
  1022     template<class T>
  1023     BfsWizard<DefReachedMapBase<T> > reachedMap(const T &t) 
  1024     {
  1025       Base::_pred=(void *)&t;
  1026       return BfsWizard<DefReachedMapBase<T> >(*this);
  1027     }
  1028     
  1029 
  1030     template<class T>
  1031     struct DefProcessedMapBase : public Base {
  1032       typedef T ProcessedMap;
  1033       static ProcessedMap *createProcessedMap(const Graph &) { return 0; };
  1034       DefProcessedMapBase(const TR &b) : TR(b) {}
  1035     };
  1036     
  1037     ///\brief \ref named-templ-param "Named parameter"
  1038     ///function for setting ProcessedMap
  1039     ///
  1040     /// \ref named-templ-param "Named parameter"
  1041     ///function for setting ProcessedMap
  1042     ///
  1043     template<class T>
  1044     BfsWizard<DefProcessedMapBase<T> > processedMap(const T &t) 
  1045     {
  1046       Base::_pred=(void *)&t;
  1047       return BfsWizard<DefProcessedMapBase<T> >(*this);
  1048     }
  1049     
  1050    
  1051     template<class T>
  1052     struct DefDistMapBase : public Base {
  1053       typedef T DistMap;
  1054       static DistMap *createDistMap(const Graph &) { return 0; };
  1055       DefDistMapBase(const TR &b) : TR(b) {}
  1056     };
  1057     
  1058     ///\brief \ref named-templ-param "Named parameter"
  1059     ///function for setting DistMap type
  1060     ///
  1061     /// \ref named-templ-param "Named parameter"
  1062     ///function for setting DistMap type
  1063     ///
  1064     template<class T>
  1065     BfsWizard<DefDistMapBase<T> > distMap(const T &t) 
  1066     {
  1067       Base::_dist=(void *)&t;
  1068       return BfsWizard<DefDistMapBase<T> >(*this);
  1069     }
  1070     
  1071     /// Sets the source node, from which the Bfs algorithm runs.
  1072 
  1073     /// Sets the source node, from which the Bfs algorithm runs.
  1074     /// \param s is the source node.
  1075     BfsWizard<TR> &source(Node s) 
  1076     {
  1077       Base::_source=s;
  1078       return *this;
  1079     }
  1080     
  1081   };
  1082   
  1083   ///Function type interface for Bfs algorithm.
  1084 
  1085   /// \ingroup flowalgs
  1086   ///Function type interface for Bfs algorithm.
  1087   ///
  1088   ///This function also has several
  1089   ///\ref named-templ-func-param "named parameters",
  1090   ///they are declared as the members of class \ref BfsWizard.
  1091   ///The following
  1092   ///example shows how to use these parameters.
  1093   ///\code
  1094   ///  bfs(g,source).predMap(preds).run();
  1095   ///\endcode
  1096   ///\warning Don't forget to put the \ref BfsWizard::run() "run()"
  1097   ///to the end of the parameter list.
  1098   ///\sa BfsWizard
  1099   ///\sa Bfs
  1100   template<class GR>
  1101   BfsWizard<BfsWizardBase<GR> >
  1102   bfs(const GR &g,typename GR::Node s=INVALID)
  1103   {
  1104     return BfsWizard<BfsWizardBase<GR> >(g,s);
  1105   }
  1106 
  1107 } //END OF NAMESPACE LEMON
  1108 
  1109 #endif
  1110