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