src/lemon/dijkstra.h
author deba
Mon, 07 Feb 2005 10:50:05 +0000
changeset 1135 cfccb33ecf7b
parent 1130 47ef467ccf70
child 1151 b217fc69f913
permissions -rw-r--r--
Removing graph_defines.h
     1 /* -*- C++ -*-
     2  * src/lemon/dijkstra.h - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Combinatorial Optimization Research Group, 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_DIJKSTRA_H
    18 #define LEMON_DIJKSTRA_H
    19 
    20 ///\ingroup flowalgs
    21 ///\file
    22 ///\brief Dijkstra algorithm.
    23 
    24 #include <lemon/list_graph.h>
    25 #include <lemon/bin_heap.h>
    26 #include <lemon/invalid.h>
    27 #include <lemon/error.h>
    28 #include <lemon/maps.h>
    29 
    30 namespace lemon {
    31 
    32 
    33 /// \addtogroup flowalgs
    34 /// @{
    35 
    36   ///Default traits class of Dijkstra class.
    37 
    38   ///Default traits class of Dijkstra class.
    39   ///\param GR Graph type.
    40   ///\param LM Type of length map.
    41   template<class GR, class LM>
    42   struct DijkstraDefaultTraits
    43   {
    44     ///The graph type the algorithm runs on. 
    45     typedef GR Graph;
    46     ///The type of the map that stores the edge lengths.
    47 
    48     ///The type of the map that stores the edge lengths.
    49     ///It must meet the \ref concept::ReadMap "ReadMap" concept.
    50     typedef LM LengthMap;
    51     //The type of the length of the edges.
    52     typedef typename LM::Value Value;
    53     ///The heap type used by Dijkstra algorithm.
    54 
    55     ///The heap type used by Dijkstra algorithm.
    56     ///
    57     ///\sa BinHeap
    58     ///\sa Dijkstra
    59     typedef BinHeap<typename Graph::Node,
    60 		    typename LM::Value,
    61 		    typename GR::template NodeMap<int>,
    62 		    std::less<Value> > Heap;
    63 
    64     ///\brief The type of the map that stores the last
    65     ///edges of the shortest paths.
    66     /// 
    67     ///The type of the map that stores the last
    68     ///edges of the shortest paths.
    69     ///It must meet the \ref concept::WriteMap "WriteMap" concept.
    70     ///
    71     typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
    72     ///Instantiates a PredMap.
    73  
    74     ///This function instantiates a \ref PredMap. 
    75     ///\param G is the graph, to which we would like to define the PredMap.
    76     ///\todo The graph alone may be insufficient for the initialization
    77     static PredMap *createPredMap(const GR &G) 
    78     {
    79       return new PredMap(G);
    80     }
    81     ///\brief The type of the map that stores the last but one
    82     ///nodes of the shortest paths.
    83     ///
    84     ///The type of the map that stores the last but one
    85     ///nodes of the shortest paths.
    86     ///It must meet the \ref concept::WriteMap "WriteMap" concept.
    87     ///
    88     typedef NullMap<typename Graph::Node,typename Graph::Node> PredNodeMap;
    89     ///Instantiates a PredNodeMap.
    90     
    91     ///This function instantiates a \ref PredNodeMap. 
    92     ///\param G is the graph, to which we would like to define the \ref PredNodeMap
    93     static PredNodeMap *createPredNodeMap(const GR &G)
    94     {
    95       return new PredNodeMap();
    96     }
    97 
    98     ///The type of the map that stores whether a nodes is reached.
    99  
   100     ///The type of the map that stores whether a nodes is reached.
   101     ///It must meet the \ref concept::WriteMap "WriteMap" concept.
   102     ///By default it is a NullMap.
   103     ///\todo If it is set to a real map, Dijkstra::reached() should read this.
   104     ///\todo named parameter to set this type, function to read and write.
   105     typedef NullMap<typename Graph::Node,bool> ReachedMap;
   106     ///Instantiates a ReachedMap.
   107  
   108     ///This function instantiates a \ref ReachedMap. 
   109     ///\param G is the graph, to which we would like to define the \ref ReachedMap
   110     static ReachedMap *createReachedMap(const GR &G)
   111     {
   112       return new ReachedMap();
   113     }
   114     ///The type of the map that stores the dists of the nodes.
   115  
   116     ///The type of the map that stores the dists of the nodes.
   117     ///It must meet the \ref concept::WriteMap "WriteMap" concept.
   118     ///
   119     typedef typename Graph::template NodeMap<typename LM::Value> DistMap;
   120     ///Instantiates a DistMap.
   121  
   122     ///This function instantiates a \ref DistMap. 
   123     ///\param G is the graph, to which we would like to define the \ref DistMap
   124     static DistMap *createDistMap(const GR &G)
   125     {
   126       return new DistMap(G);
   127     }
   128   };
   129   
   130   ///%Dijkstra algorithm class.
   131   
   132   ///This class provides an efficient implementation of %Dijkstra algorithm.
   133   ///The edge lengths are passed to the algorithm using a
   134   ///\ref concept::ReadMap "ReadMap",
   135   ///so it is easy to change it to any kind of length.
   136   ///
   137   ///The type of the length is determined by the
   138   ///\ref concept::ReadMap::Value "Value" of the length map.
   139   ///
   140   ///It is also possible to change the underlying priority heap.
   141   ///
   142   ///\param GR The graph type the algorithm runs on. The default value is
   143   ///\ref ListGraph. The value of GR is not used directly by Dijkstra, it
   144   ///is only passed to \ref DijkstraDefaultTraits.
   145   ///\param LM This read-only
   146   ///EdgeMap
   147   ///determines the
   148   ///lengths of the edges. It is read once for each edge, so the map
   149   ///may involve in relatively time consuming process to compute the edge
   150   ///length if it is necessary. The default map type is
   151   ///\ref concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>".
   152   ///The value of LM is not used directly by Dijkstra, it
   153   ///is only passed to \ref DijkstraDefaultTraits.
   154   ///\param TR Traits class to set various data types used by the algorithm.
   155   ///The default traits class is
   156   ///\ref DijkstraDefaultTraits "DijkstraDefaultTraits<GR,LM>".
   157   ///See \ref DijkstraDefaultTraits for the documentation of
   158   ///a Dijkstra traits class.
   159   ///
   160   ///\author Jacint Szabo and Alpar Juttner
   161   ///\todo A compare object would be nice.
   162 
   163 #ifdef DOXYGEN
   164   template <typename GR,
   165 	    typename LM,
   166 	    typename TR>
   167 #else
   168   template <typename GR=ListGraph,
   169 	    typename LM=typename GR::template EdgeMap<int>,
   170 	    typename TR=DijkstraDefaultTraits<GR,LM> >
   171 #endif
   172   class Dijkstra {
   173   public:
   174     /**
   175      * \brief \ref Exception for uninitialized parameters.
   176      *
   177      * This error represents problems in the initialization
   178      * of the parameters of the algorithms.
   179      */
   180     class UninitializedParameter : public lemon::UninitializedParameter {
   181     public:
   182       virtual const char* exceptionName() const {
   183 	return "lemon::Dijsktra::UninitializedParameter";
   184       }
   185     };
   186 
   187     typedef TR Traits;
   188     ///The type of the underlying graph.
   189     typedef typename TR::Graph Graph;
   190     ///\e
   191     typedef typename Graph::Node Node;
   192     ///\e
   193     typedef typename Graph::NodeIt NodeIt;
   194     ///\e
   195     typedef typename Graph::Edge Edge;
   196     ///\e
   197     typedef typename Graph::OutEdgeIt OutEdgeIt;
   198     
   199     ///The type of the length of the edges.
   200     typedef typename TR::LengthMap::Value Value;
   201     ///The type of the map that stores the edge lengths.
   202     typedef typename TR::LengthMap LengthMap;
   203     ///\brief The type of the map that stores the last
   204     ///edges of the shortest paths.
   205     typedef typename TR::PredMap PredMap;
   206     ///\brief The type of the map that stores the last but one
   207     ///nodes of the shortest paths.
   208     typedef typename TR::PredNodeMap PredNodeMap;
   209     ///The type of the map indicating if a node is reached.
   210     typedef typename TR::ReachedMap ReachedMap;
   211     ///The type of the map that stores the dists of the nodes.
   212     typedef typename TR::DistMap DistMap;
   213     ///The heap type used by the dijkstra algorithm.
   214     typedef typename TR::Heap Heap;
   215   private:
   216     /// Pointer to the underlying graph.
   217     const Graph *G;
   218     /// Pointer to the length map
   219     const LengthMap *length;
   220     ///Pointer to the map of predecessors edges.
   221     PredMap *_pred;
   222     ///Indicates if \ref _pred is locally allocated (\c true) or not.
   223     bool local_pred;
   224     ///Pointer to the map of predecessors nodes.
   225     PredNodeMap *_predNode;
   226     ///Indicates if \ref _predNode is locally allocated (\c true) or not.
   227     bool local_predNode;
   228     ///Pointer to the map of distances.
   229     DistMap *_dist;
   230     ///Indicates if \ref _dist is locally allocated (\c true) or not.
   231     bool local_dist;
   232     ///Pointer to the map of reached status of the nodes.
   233     ReachedMap *_reached;
   234     ///Indicates if \ref _reached is locally allocated (\c true) or not.
   235     bool local_reached;
   236 
   237     ///The source node of the last execution.
   238     Node source;
   239 
   240     ///Creates the maps if necessary.
   241     
   242     ///\todo Error if \c G or are \c NULL. What about \c length?
   243     ///\todo Better memory allocation (instead of new).
   244     void create_maps() 
   245     {
   246       if(!_pred) {
   247 	local_pred = true;
   248 	_pred = Traits::createPredMap(*G);
   249       }
   250       if(!_predNode) {
   251 	local_predNode = true;
   252 	_predNode = Traits::createPredNodeMap(*G);
   253       }
   254       if(!_dist) {
   255 	local_dist = true;
   256 	_dist = Traits::createDistMap(*G);
   257       }
   258       if(!_reached) {
   259 	local_reached = true;
   260 	_reached = Traits::createReachedMap(*G);
   261       }
   262     }
   263     
   264   public :
   265  
   266     ///\name Named template parameters
   267 
   268     ///@{
   269 
   270     template <class T>
   271     struct DefPredMapTraits : public Traits {
   272       typedef T PredMap;
   273       static PredMap *createPredMap(const Graph &G) 
   274       {
   275 	throw UninitializedParameter();
   276       }
   277     };
   278     ///\ref named-templ-param "Named parameter" for setting PredMap type
   279 
   280     ///\ref named-templ-param "Named parameter" for setting PredMap type
   281     ///
   282     template <class T>
   283     class DefPredMap : public Dijkstra< Graph,
   284 					LengthMap,
   285 					DefPredMapTraits<T> > { };
   286     
   287     template <class T>
   288     struct DefPredNodeMapTraits : public Traits {
   289       typedef T PredNodeMap;
   290       static PredNodeMap *createPredNodeMap(const Graph &G) 
   291       {
   292 	throw UninitializedParameter();
   293       }
   294     };
   295     ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
   296 
   297     ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
   298     ///
   299     template <class T>
   300     class DefPredNodeMap : public Dijkstra< Graph,
   301 					    LengthMap,
   302 					    DefPredNodeMapTraits<T> > { };
   303     
   304     template <class T>
   305     struct DefDistMapTraits : public Traits {
   306       typedef T DistMap;
   307       static DistMap *createDistMap(const Graph &G) 
   308       {
   309 	throw UninitializedParameter();
   310       }
   311     };
   312     ///\ref named-templ-param "Named parameter" for setting DistMap type
   313 
   314     ///\ref named-templ-param "Named parameter" for setting DistMap type
   315     ///
   316     template <class T>
   317     class DefDistMap : public Dijkstra< Graph,
   318 					LengthMap,
   319 					DefDistMapTraits<T> > { };
   320     
   321     template <class T>
   322     struct DefReachedMapTraits : public Traits {
   323       typedef T ReachedMap;
   324       static ReachedMap *createReachedMap(const Graph &G) 
   325       {
   326 	throw UninitializedParameter();
   327       }
   328     };
   329     ///\ref named-templ-param "Named parameter" for setting ReachedMap type
   330 
   331     ///\ref named-templ-param "Named parameter" for setting ReachedMap type
   332     ///
   333     template <class T>
   334     class DefReachedMap : public Dijkstra< Graph,
   335 					LengthMap,
   336 					DefReachedMapTraits<T> > { };
   337     
   338     struct DefGraphReachedMapTraits : public Traits {
   339       typedef typename Graph::NodeMap<bool> ReachedMap;
   340       static ReachedMap *createReachedMap(const Graph &G) 
   341       {
   342 	return new ReachedMap(G);
   343       }
   344     };
   345     ///\brief \ref named-templ-param "Named parameter"
   346     ///for setting the ReachedMap type to be Graph::NodeMap<bool>.
   347     ///
   348     ///\ref named-templ-param "Named parameter"
   349     ///for setting the ReachedMap type to be Graph::NodeMap<bool>.
   350     ///If you don't set it explicitely, it will be automatically allocated.
   351     template <class T>
   352     class DefReachedMapToBeDefaultMap :
   353       public Dijkstra< Graph,
   354 		       LengthMap,
   355 		       DefGraphReachedMapTraits> { };
   356     
   357     ///@}
   358 
   359 
   360   private:
   361     typename Graph::template NodeMap<int> _heap_map;
   362     Heap _heap;
   363   public:      
   364     
   365     ///Constructor.
   366     
   367     ///\param _G the graph the algorithm will run on.
   368     ///\param _length the length map used by the algorithm.
   369     Dijkstra(const Graph& _G, const LengthMap& _length) :
   370       G(&_G), length(&_length),
   371       _pred(NULL), local_pred(false),
   372       _predNode(NULL), local_predNode(false),
   373       _dist(NULL), local_dist(false),
   374       _reached(NULL), local_reached(false),
   375       _heap_map(*G,-1),_heap(_heap_map)
   376     { }
   377     
   378     ///Destructor.
   379     ~Dijkstra() 
   380     {
   381       if(local_pred) delete _pred;
   382       if(local_predNode) delete _predNode;
   383       if(local_dist) delete _dist;
   384       if(local_reached) delete _reached;
   385     }
   386 
   387     ///Sets the length map.
   388 
   389     ///Sets the length map.
   390     ///\return <tt> (*this) </tt>
   391     Dijkstra &lengthMap(const LengthMap &m) 
   392     {
   393       length = &m;
   394       return *this;
   395     }
   396 
   397     ///Sets the map storing the predecessor edges.
   398 
   399     ///Sets the map storing the predecessor edges.
   400     ///If you don't use this function before calling \ref run(),
   401     ///it will allocate one. The destuctor deallocates this
   402     ///automatically allocated map, of course.
   403     ///\return <tt> (*this) </tt>
   404     Dijkstra &predMap(PredMap &m) 
   405     {
   406       if(local_pred) {
   407 	delete _pred;
   408 	local_pred=false;
   409       }
   410       _pred = &m;
   411       return *this;
   412     }
   413 
   414     ///Sets the map storing the predecessor nodes.
   415 
   416     ///Sets the map storing the predecessor nodes.
   417     ///If you don't use this function before calling \ref run(),
   418     ///it will allocate one. The destuctor deallocates this
   419     ///automatically allocated map, of course.
   420     ///\return <tt> (*this) </tt>
   421     Dijkstra &predNodeMap(PredNodeMap &m) 
   422     {
   423       if(local_predNode) {
   424 	delete _predNode;
   425 	local_predNode=false;
   426       }
   427       _predNode = &m;
   428       return *this;
   429     }
   430 
   431     ///Sets the map storing the distances calculated by the algorithm.
   432 
   433     ///Sets the map storing the distances calculated by the algorithm.
   434     ///If you don't use this function before calling \ref run(),
   435     ///it will allocate one. The destuctor deallocates this
   436     ///automatically allocated map, of course.
   437     ///\return <tt> (*this) </tt>
   438     Dijkstra &distMap(DistMap &m) 
   439     {
   440       if(local_dist) {
   441 	delete _dist;
   442 	local_dist=false;
   443       }
   444       _dist = &m;
   445       return *this;
   446     }
   447 
   448   private:
   449     void finalizeNodeData(Node v,Value dst)
   450     {
   451       _reached->set(v,true);
   452       _dist->set(v, dst);
   453       _predNode->set(v,G->source((*_pred)[v]));
   454     }
   455 
   456   public:
   457     ///\name Excetution control
   458     ///The simplest way to execute the algorithm is to use
   459     ///\ref run().
   460     ///\n
   461     ///It you need more control on the execution,
   462     ///first you must call \ref init(), then you can add several source nodes
   463     ///with \ref addSource(). Finally \ref start() will perform the actual path
   464     ///computation.
   465 
   466     ///@{
   467 
   468     ///Initializes the internal data structures.
   469 
   470     ///Initializes the internal data structures.
   471     ///
   472     ///\todo _heap_map's type could also be in the traits class.
   473     void init()
   474     {
   475       create_maps();
   476       
   477       for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
   478 	_pred->set(u,INVALID);
   479 	_predNode->set(u,INVALID);
   480 	///\todo *_reached is not set to false.
   481 	_heap_map.set(u,Heap::PRE_HEAP);
   482       }
   483     }
   484     
   485     ///Adds a new source node.
   486 
   487     ///Adds a new source node the the priority heap.
   488     ///It checks if the node has already been added to the heap.
   489     ///
   490     ///The optional second parameter is the initial distance of the node.
   491     ///
   492     ///\todo Do we really want to check it?
   493     void addSource(Node s,Value dst=0)
   494     {
   495       source = s;
   496       if(_heap.state(s) != Heap::IN_HEAP) _heap.push(s,dst);
   497     }
   498     
   499     void processNode()
   500     {
   501       Node v=_heap.top(); 
   502       Value oldvalue=_heap[v];
   503       _heap.pop();
   504       finalizeNodeData(v,oldvalue);
   505       
   506       for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
   507 	Node w=G->target(e); 
   508 	switch(_heap.state(w)) {
   509 	case Heap::PRE_HEAP:
   510 	  _heap.push(w,oldvalue+(*length)[e]); 
   511 	  _pred->set(w,e);
   512 //  	  _predNode->set(w,v);
   513 	  break;
   514 	case Heap::IN_HEAP:
   515 	  if ( oldvalue+(*length)[e] < _heap[w] ) {
   516 	    _heap.decrease(w, oldvalue+(*length)[e]); 
   517 	    _pred->set(w,e);
   518 // 	    _predNode->set(w,v);
   519 	  }
   520 	  break;
   521 	case Heap::POST_HEAP:
   522 	  break;
   523 	}
   524       }
   525     }
   526 
   527     ///Executes the algorithm.
   528 
   529     ///Executes the algorithm.
   530     ///
   531     ///\pre init() must be called and at least one node should be added
   532     ///with addSource() before using this function.
   533     ///
   534     ///This method runs the %Dijkstra algorithm from the root node(s)
   535     ///in order to
   536     ///compute the
   537     ///shortest path to each node. The algorithm computes
   538     ///- The shortest path tree.
   539     ///- The distance of each node from the root(s).
   540     ///
   541     void start()
   542     {
   543       while ( !_heap.empty() ) processNode();
   544     }
   545     
   546     ///Executes the algorithm until \c dest is reached.
   547 
   548     ///Executes the algorithm until \c dest is reached.
   549     ///
   550     ///\pre init() must be called and at least one node should be added
   551     ///with addSource() before using this function.
   552     ///
   553     ///This method runs the %Dijkstra algorithm from the root node(s)
   554     ///in order to
   555     ///compute the
   556     ///shortest path to \c dest. The algorithm computes
   557     ///- The shortest path to \c  dest.
   558     ///- The distance of \c dest from the root(s).
   559     ///
   560     void start(Node dest)
   561     {
   562       while ( !_heap.empty() && _heap.top()!=dest ) processNode();
   563       if ( _heap.top()==dest ) finalizeNodeData(_heap.top());
   564     }
   565     
   566     ///Executes the algorithm until a condition is met.
   567 
   568     ///Executes the algorithm until a condition is met.
   569     ///
   570     ///\pre init() must be called and at least one node should be added
   571     ///with addSource() before using this function.
   572     ///
   573     ///\param nm must be a bool (or convertible) node map. The algorithm
   574     ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
   575     template<class NM>
   576     void start(const NM &nm)
   577     {
   578       while ( !_heap.empty() && !mn[_heap.top()] ) processNode();
   579       if ( !_heap.empty() ) finalizeNodeData(_heap.top());
   580     }
   581     
   582     ///Runs %Dijkstra algorithm from node \c s.
   583     
   584     ///This method runs the %Dijkstra algorithm from a root node \c s
   585     ///in order to
   586     ///compute the
   587     ///shortest path to each node. The algorithm computes
   588     ///- The shortest path tree.
   589     ///- The distance of each node from the root.
   590     ///
   591     ///\note d.run(s) is just a shortcut of the following code.
   592     ///\code
   593     ///  d.init();
   594     ///  d.addSource(s);
   595     ///  d.start();
   596     ///\endcode
   597     void run(Node s) {
   598       init();
   599       addSource(s);
   600       start();
   601     }
   602     
   603     ///Finds the shortest path between \c s and \c t.
   604     
   605     ///Finds the shortest path between \c s and \c t.
   606     ///
   607     ///\return The length of the shortest s---t path if there exists one,
   608     ///0 otherwise.
   609     ///\note Apart from the return value, d.run(s) is
   610     ///just a shortcut of the following code.
   611     ///\code
   612     ///  d.init();
   613     ///  d.addSource(s);
   614     ///  d.start(t);
   615     ///\endcode
   616     Value run(Node s,Node t) {
   617       init();
   618       addSource(s);
   619       start(t);
   620       return (*_pred)[t]==INVALID?0:(*_dist)[t];
   621     }
   622     
   623     ///@}
   624 
   625     ///\name Query Functions
   626     ///The result of the %Dijkstra algorithm can be obtained using these
   627     ///functions.\n
   628     ///Before the use of these functions,
   629     ///either run() or start() must be called.
   630     
   631     ///@{
   632 
   633     ///The distance of a node from the root.
   634 
   635     ///Returns the distance of a node from the root.
   636     ///\pre \ref run() must be called before using this function.
   637     ///\warning If node \c v in unreachable from the root the return value
   638     ///of this funcion is undefined.
   639     Value dist(Node v) const { return (*_dist)[v]; }
   640 
   641     ///Returns the 'previous edge' of the shortest path tree.
   642 
   643     ///For a node \c v it returns the 'previous edge' of the shortest path tree,
   644     ///i.e. it returns the last edge of a shortest path from the root to \c
   645     ///v. It is \ref INVALID
   646     ///if \c v is unreachable from the root or if \c v=s. The
   647     ///shortest path tree used here is equal to the shortest path tree used in
   648     ///\ref predNode(Node v).  \pre \ref run() must be called before using
   649     ///this function.
   650     ///\todo predEdge could be a better name.
   651     Edge pred(Node v) const { return (*_pred)[v]; }
   652 
   653     ///Returns the 'previous node' of the shortest path tree.
   654 
   655     ///For a node \c v it returns the 'previous node' of the shortest path tree,
   656     ///i.e. it returns the last but one node from a shortest path from the
   657     ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
   658     ///\c v=s. The shortest path tree used here is equal to the shortest path
   659     ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
   660     ///using this function.
   661     Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
   662 				  G->source((*_pred)[v]); }
   663     
   664     ///Returns a reference to the NodeMap of distances.
   665 
   666     ///Returns a reference to the NodeMap of distances. \pre \ref run() must
   667     ///be called before using this function.
   668     const DistMap &distMap() const { return *_dist;}
   669  
   670     ///Returns a reference to the shortest path tree map.
   671 
   672     ///Returns a reference to the NodeMap of the edges of the
   673     ///shortest path tree.
   674     ///\pre \ref run() must be called before using this function.
   675     const PredMap &predMap() const { return *_pred;}
   676  
   677     ///Returns a reference to the map of nodes of shortest paths.
   678 
   679     ///Returns a reference to the NodeMap of the last but one nodes of the
   680     ///shortest path tree.
   681     ///\pre \ref run() must be called before using this function.
   682     const PredNodeMap &predNodeMap() const { return *_predNode;}
   683 
   684     ///Checks if a node is reachable from the root.
   685 
   686     ///Returns \c true if \c v is reachable from the root.
   687     ///\warning If the algorithm is started from multiple nodes,
   688     ///this function may give false result for the source nodes.
   689     ///\pre \ref run() must be called before using this function.
   690     ///
   691     bool reached(Node v) { return v==source || (*_pred)[v]!=INVALID; }
   692     
   693     ///@}
   694   };
   695 
   696   /// Default traits used by \ref DijkstraWizard
   697 
   698   /// To make it easier to use Dijkstra algorithm we have created a wizard class.
   699   /// This \ref DijkstraWizard class needs default traits, as well as the \ref Dijkstra class.
   700   /// The \ref DijkstraWizardBase is a class to be the default traits of the
   701   /// \ref DijkstraWizard class.
   702   template<class GR,class LM>
   703   class DijkstraWizardBase : public DijkstraDefaultTraits<GR,LM>
   704   {
   705 
   706     typedef DijkstraDefaultTraits<GR,LM> Base;
   707   protected:
   708     /// Pointer to the underlying graph.
   709     void *_g;
   710     /// Pointer to the length map
   711     void *_length;
   712     ///Pointer to the map of predecessors edges.
   713     void *_pred;
   714     ///Pointer to the map of predecessors nodes.
   715     void *_predNode;
   716     ///Pointer to the map of distances.
   717     void *_dist;
   718     ///Pointer to the source node.
   719     void *_source;
   720 
   721     /// Type of the nodes in the graph.
   722     typedef typename Base::Graph::Node Node;
   723 
   724     public:
   725     /// Constructor.
   726     
   727     /// This constructor does not require parameters, therefore it initiates
   728     /// all of the attributes to default values (0, INVALID).
   729     DijkstraWizardBase() : _g(0), _length(0), _pred(0), _predNode(0),
   730 		       _dist(0), _source(INVALID) {}
   731 
   732     /// Constructor.
   733     
   734     /// This constructor requires some parameters, listed in the parameters list.
   735     /// Others are initiated to 0.
   736     /// \param g is the initial value of  \ref _g
   737     /// \param l is the initial value of  \ref _length
   738     /// \param s is the initial value of  \ref _source
   739     DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
   740       _g((void *)&g), _length((void *)&l), _pred(0), _predNode(0),
   741 		  _dist(0), _source((void *)&s) {}
   742 
   743   };
   744   
   745   /// A class to make easier the usage of Dijkstra algorithm
   746 
   747   /// This class is created to make it easier to use Dijkstra algorithm.
   748   /// It uses the functions and features of the plain \ref Dijkstra,
   749   /// but it is much more simple to use it.
   750   ///
   751   /// Simplicity means that the way to change the types defined
   752   /// in the traits class is based on functions that returns the new class
   753   /// and not on templatable built-in classes. When using the plain \ref Dijkstra
   754   /// the new class with the modified type comes from the original class by using the ::
   755   /// operator. In the case of \ref DijkstraWizard only a function have to be called and it will
   756   /// return the needed class.
   757   ///
   758   /// It does not have own \ref run method. When its \ref run method is called
   759   /// it initiates a plain \ref Dijkstra class, and calls the \ref Dijkstra::run
   760   /// method of it.
   761   template<class TR>
   762   class DijkstraWizard : public TR
   763   {
   764     typedef TR Base;
   765 
   766     ///The type of the underlying graph.
   767     typedef typename TR::Graph Graph;
   768     //\e
   769     typedef typename Graph::Node Node;
   770     //\e
   771     typedef typename Graph::NodeIt NodeIt;
   772     //\e
   773     typedef typename Graph::Edge Edge;
   774     //\e
   775     typedef typename Graph::OutEdgeIt OutEdgeIt;
   776     
   777     ///The type of the map that stores the edge lengths.
   778     typedef typename TR::LengthMap LengthMap;
   779     ///The type of the length of the edges.
   780     typedef typename LengthMap::Value Value;
   781     ///\brief The type of the map that stores the last
   782     ///edges of the shortest paths.
   783     typedef typename TR::PredMap PredMap;
   784     ///\brief The type of the map that stores the last but one
   785     ///nodes of the shortest paths.
   786     typedef typename TR::PredNodeMap PredNodeMap;
   787     ///The type of the map that stores the dists of the nodes.
   788     typedef typename TR::DistMap DistMap;
   789 
   790     ///The heap type used by the dijkstra algorithm.
   791     typedef typename TR::Heap Heap;
   792 public:
   793     /// Constructor.
   794     DijkstraWizard() : TR() {}
   795 
   796     /// Constructor that requires parameters.
   797 
   798     /// Constructor that requires parameters.
   799     /// These parameters will be the default values for the traits class.
   800     DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
   801       TR(g,l,s) {}
   802 
   803     ///Copy constructor
   804     DijkstraWizard(const TR &b) : TR(b) {}
   805 
   806     ~DijkstraWizard() {}
   807 
   808     ///Runs Dijkstra algorithm from a given node.
   809     
   810     ///Runs Dijkstra algorithm from a given node.
   811     ///The node can be given by the \ref source function.
   812     void run()
   813     {
   814       if(_source==0) throw UninitializedParameter();
   815       Dijkstra<Graph,LengthMap,TR> Dij(*(Graph*)_g,*(LengthMap*)_length);
   816       if(_pred) Dij.predMap(*(PredMap*)_pred);
   817       if(_predNode) Dij.predNodeMap(*(PredNodeMap*)_predNode);
   818       if(_dist) Dij.distMap(*(DistMap*)_dist);
   819       Dij.run(*(Node*)_source);
   820     }
   821 
   822     ///Runs Dijkstra algorithm from the given node.
   823 
   824     ///Runs Dijkstra algorithm from the given node.
   825     ///\param s is the given source.
   826     void run(Node s)
   827     {
   828       _source=(void *)&s;
   829       run();
   830     }
   831 
   832     template<class T>
   833     struct DefPredMapBase : public Base {
   834       typedef T PredMap;
   835       static PredMap *createPredMap(const Graph &G) { return 0; };
   836       DefPredMapBase(const Base &b) : Base(b) {}
   837     };
   838     
   839     /// \ref named-templ-param "Named parameter" function for setting PredMap type
   840 
   841     /// \ref named-templ-param "Named parameter" function for setting PredMap type
   842     ///
   843     template<class T>
   844     DijkstraWizard<DefPredMapBase<T> > predMap(const T &t) 
   845     {
   846       _pred=(void *)&t;
   847       return DijkstraWizard<DefPredMapBase<T> >(*this);
   848     }
   849     
   850 
   851     template<class T>
   852     struct DefPredNodeMapBase : public Base {
   853       typedef T PredNodeMap;
   854       static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; };
   855       DefPredNodeMapBase(const Base &b) : Base(b) {}
   856     };
   857     
   858     /// \ref named-templ-param "Named parameter" function for setting PredNodeMap type
   859 
   860     /// \ref named-templ-param "Named parameter" function for setting PredNodeMap type
   861     ///
   862     template<class T>
   863     DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t) 
   864     {
   865       _predNode=(void *)&t;
   866       return DijkstraWizard<DefPredNodeMapBase<T> >(*this);
   867     }
   868    
   869     template<class T>
   870     struct DefDistMapBase : public Base {
   871       typedef T DistMap;
   872       static DistMap *createDistMap(const Graph &G) { return 0; };
   873       DefDistMapBase(const Base &b) : Base(b) {}
   874     };
   875     
   876     /// \ref named-templ-param "Named parameter" function for setting DistMap type
   877 
   878     /// \ref named-templ-param "Named parameter" function for setting DistMap type
   879     ///
   880     template<class T>
   881     DijkstraWizard<DefDistMapBase<T> > distMap(const T &t) 
   882     {
   883       _dist=(void *)&t;
   884       return DijkstraWizard<DefDistMapBase<T> >(*this);
   885     }
   886     
   887     /// Sets the source node, from which the Dijkstra algorithm runs.
   888 
   889     /// Sets the source node, from which the Dijkstra algorithm runs.
   890     /// \param s is the source node.
   891     DijkstraWizard<TR> &source(Node s) 
   892     {
   893       source=(void *)&s;
   894       return *this;
   895     }
   896     
   897   };
   898   
   899   ///\e
   900 
   901   ///\todo Please document...
   902   ///
   903   template<class GR, class LM>
   904   DijkstraWizard<DijkstraWizardBase<GR,LM> >
   905   dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
   906   {
   907     return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
   908   }
   909 
   910 /// @}
   911   
   912 } //END OF NAMESPACE LEMON
   913 
   914 #endif
   915