src/hugo/dijkstra.h
author alpar
Wed, 21 Jul 2004 06:59:55 +0000
changeset 716 e7f13f60fcfd
parent 694 2d87cefb35b2
child 734 329832ac02b7
permissions -rw-r--r--
Turn on '-Wall' by default.
     1 // -*- C++ -*-
     2 #ifndef HUGO_DIJKSTRA_H
     3 #define HUGO_DIJKSTRA_H
     4 
     5 ///\ingroup galgs
     6 ///\file
     7 ///\brief Dijkstra algorithm.
     8 
     9 #include <hugo/bin_heap.h>
    10 #include <hugo/invalid.h>
    11 
    12 namespace hugo {
    13 
    14 /// \addtogroup galgs
    15 /// @{
    16 
    17   ///%Dijkstra algorithm class.
    18 
    19   ///This class provides an efficient implementation of %Dijkstra algorithm.
    20   ///The edge lengths are passed to the algorithm using a
    21   ///\ref ReadMapSkeleton "readable map",
    22   ///so it is easy to change it to any kind of length.
    23   ///
    24   ///The type of the length is determined by the \c ValueType of the length map.
    25   ///
    26   ///It is also possible to change the underlying priority heap.
    27   ///
    28   ///\param GR The graph type the algorithm runs on.
    29   ///\param LM This read-only
    30   ///EdgeMap
    31   ///determines the
    32   ///lengths of the edges. It is read once for each edge, so the map
    33   ///may involve in relatively time consuming process to compute the edge
    34   ///length if it is necessary. The default map type is
    35   ///\ref GraphSkeleton::EdgeMap "Graph::EdgeMap<int>"
    36   ///\param Heap The heap type used by the %Dijkstra
    37   ///algorithm. The default
    38   ///is using \ref BinHeap "binary heap".
    39   ///
    40   ///\author Jacint Szabo and Alpar Juttner
    41   ///\todo We need a typedef-names should be standardized. (-:
    42 
    43 #ifdef DOXYGEN
    44   template <typename GR,
    45 	    typename LM,
    46 	    typename Heap>
    47 #else
    48   template <typename GR,
    49 	    typename LM=typename GR::template EdgeMap<int>,
    50 	    template <class,class,class,class> class Heap = BinHeap >
    51 #endif
    52   class Dijkstra{
    53   public:
    54     ///The type of the underlying graph.
    55     typedef GR Graph;
    56     typedef typename Graph::Node Node;
    57     typedef typename Graph::NodeIt NodeIt;
    58     typedef typename Graph::Edge Edge;
    59     typedef typename Graph::OutEdgeIt OutEdgeIt;
    60     
    61     ///The type of the length of the edges.
    62     typedef typename LM::ValueType ValueType;
    63     ///The type of the map that stores the edge lengths.
    64     typedef LM LengthMap;
    65     ///\brief The type of the map that stores the last
    66     ///edges of the shortest paths.
    67     typedef typename Graph::template NodeMap<Edge> PredMap;
    68     ///\brief The type of the map that stores the last but one
    69     ///nodes of the shortest paths.
    70     typedef typename Graph::template NodeMap<Node> PredNodeMap;
    71     ///The type of the map that stores the dists of the nodes.
    72     typedef typename Graph::template NodeMap<ValueType> DistMap;
    73 
    74   private:
    75     const Graph *G;
    76     const LM *length;
    77     //    bool local_length;
    78     PredMap *predecessor;
    79     bool local_predecessor;
    80     PredNodeMap *pred_node;
    81     bool local_pred_node;
    82     DistMap *distance;
    83     bool local_distance;
    84 
    85     ///Initialize maps
    86     
    87     ///\todo Error if \c G or are \c NULL. What about \c length?
    88     ///\todo Better memory allocation (instead of new).
    89     void init_maps() 
    90     {
    91 //       if(!length) {
    92 // 	local_length = true;
    93 // 	length = new LM(G);
    94 //       }
    95       if(!predecessor) {
    96 	local_predecessor = true;
    97 	predecessor = new PredMap(*G);
    98       }
    99       if(!pred_node) {
   100 	local_pred_node = true;
   101 	pred_node = new PredNodeMap(*G);
   102       }
   103       if(!distance) {
   104 	local_distance = true;
   105 	distance = new DistMap(*G);
   106       }
   107     }
   108     
   109   public :
   110     
   111     Dijkstra(const Graph& _G, const LM& _length) :
   112       G(&_G), length(&_length),
   113       predecessor(NULL), local_predecessor(false),
   114       pred_node(NULL), local_pred_node(false),
   115       distance(NULL), local_distance(false)
   116     { }
   117     
   118     ~Dijkstra() 
   119     {
   120       //      if(local_length) delete length;
   121       if(local_predecessor) delete predecessor;
   122       if(local_pred_node) delete pred_node;
   123       if(local_distance) delete distance;
   124     }
   125 
   126     ///Sets the graph the algorithm will run on.
   127 
   128     ///Sets the graph the algorithm will run on.
   129     ///\return <tt> (*this) </tt>
   130     Dijkstra &setGraph(const Graph &_G) 
   131     {
   132       G = &_G;
   133       return *this;
   134     }
   135     ///Sets the length map.
   136 
   137     ///Sets the length map.
   138     ///\return <tt> (*this) </tt>
   139     Dijkstra &setLengthMap(const LM &m) 
   140     {
   141 //       if(local_length) {
   142 // 	delete length;
   143 // 	local_length=false;
   144 //       }
   145       length = &m;
   146       return *this;
   147     }
   148 
   149     ///Sets the map storing the predecessor edges.
   150 
   151     ///Sets the map storing the predecessor edges.
   152     ///If you don't use this function before calling \ref run(),
   153     ///it will allocate one. The destuctor deallocates this
   154     ///automatically allocated map, of course.
   155     ///\return <tt> (*this) </tt>
   156     Dijkstra &setPredMap(PredMap &m) 
   157     {
   158       if(local_predecessor) {
   159 	delete predecessor;
   160 	local_predecessor=false;
   161       }
   162       predecessor = &m;
   163       return *this;
   164     }
   165 
   166     ///Sets the map storing the predecessor nodes.
   167 
   168     ///Sets the map storing the predecessor nodes.
   169     ///If you don't use this function before calling \ref run(),
   170     ///it will allocate one. The destuctor deallocates this
   171     ///automatically allocated map, of course.
   172     ///\return <tt> (*this) </tt>
   173     Dijkstra &setPredNodeMap(PredNodeMap &m) 
   174     {
   175       if(local_pred_node) {
   176 	delete pred_node;
   177 	local_pred_node=false;
   178       }
   179       pred_node = &m;
   180       return *this;
   181     }
   182 
   183     ///Sets the map storing the distances calculated by the algorithm.
   184 
   185     ///Sets the map storing the distances calculated by the algorithm.
   186     ///If you don't use this function before calling \ref run(),
   187     ///it will allocate one. The destuctor deallocates this
   188     ///automatically allocated map, of course.
   189     ///\return <tt> (*this) </tt>
   190     Dijkstra &setDistMap(DistMap &m) 
   191     {
   192       if(local_distance) {
   193 	delete distance;
   194 	local_distance=false;
   195       }
   196       distance = &m;
   197       return *this;
   198     }
   199     
   200   ///Runs %Dijkstra algorithm from node \c s.
   201 
   202   ///This method runs the %Dijkstra algorithm from a root node \c s
   203   ///in order to
   204   ///compute the
   205   ///shortest path to each node. The algorithm computes
   206   ///- The shortest path tree.
   207   ///- The distance of each node from the root.
   208     
   209     void run(Node s) {
   210       
   211       init_maps();
   212       
   213       for ( NodeIt u(*G) ; G->valid(u) ; G->next(u) ) {
   214 	predecessor->set(u,INVALID);
   215 	pred_node->set(u,INVALID);
   216       }
   217       
   218       typename GR::template NodeMap<int> heap_map(*G,-1);
   219       
   220       typedef Heap<Node, ValueType, typename GR::template NodeMap<int>,
   221       std::less<ValueType> > 
   222       HeapType;
   223       
   224       HeapType heap(heap_map);
   225       
   226       heap.push(s,0); 
   227       
   228       while ( !heap.empty() ) {
   229 	
   230 	Node v=heap.top(); 
   231 	ValueType oldvalue=heap[v];
   232 	heap.pop();
   233 	distance->set(v, oldvalue);
   234 	
   235 	
   236 	for(OutEdgeIt e(*G,v); G->valid(e); G->next(e)) {
   237 	  Node w=G->bNode(e); 
   238 	  
   239 	  switch(heap.state(w)) {
   240 	  case HeapType::PRE_HEAP:
   241 	    heap.push(w,oldvalue+(*length)[e]); 
   242 	    predecessor->set(w,e);
   243 	    pred_node->set(w,v);
   244 	    break;
   245 	  case HeapType::IN_HEAP:
   246 	    if ( oldvalue+(*length)[e] < heap[w] ) {
   247 	      heap.decrease(w, oldvalue+(*length)[e]); 
   248 	      predecessor->set(w,e);
   249 	      pred_node->set(w,v);
   250 	    }
   251 	    break;
   252 	  case HeapType::POST_HEAP:
   253 	    break;
   254 	  }
   255 	}
   256       }
   257     }
   258     
   259     ///The distance of a node from the root.
   260 
   261     ///Returns the distance of a node from the root.
   262     ///\pre \ref run() must be called before using this function.
   263     ///\warning If node \c v in unreachable from the root the return value
   264     ///of this funcion is undefined.
   265     ValueType dist(Node v) const { return (*distance)[v]; }
   266 
   267     ///Returns the 'previous edge' of the shortest path tree.
   268 
   269     ///For a node \c v it returns the 'previous edge' of the shortest path tree,
   270     ///i.e. it returns the last edge from a shortest path from the root to \c
   271     ///v. It is \ref INVALID
   272     ///if \c v is unreachable from the root or if \c v=s. The
   273     ///shortest path tree used here is equal to the shortest path tree used in
   274     ///\ref predNode(Node v).  \pre \ref run() must be called before using
   275     ///this function.
   276     Edge pred(Node v) const { return (*predecessor)[v]; }
   277 
   278     ///Returns the 'previous node' of the shortest path tree.
   279 
   280     ///For a node \c v it returns the 'previous node' of the shortest path tree,
   281     ///i.e. it returns the last but one node from a shortest path from the
   282     ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
   283     ///\c v=s. The shortest path tree used here is equal to the shortest path
   284     ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
   285     ///using this function.
   286     Node predNode(Node v) const { return (*pred_node)[v]; }
   287     
   288     ///Returns a reference to the NodeMap of distances.
   289 
   290     ///Returns a reference to the NodeMap of distances. \pre \ref run() must
   291     ///be called before using this function.
   292     const DistMap &distMap() const { return *distance;}
   293  
   294     ///Returns a reference to the shortest path tree map.
   295 
   296     ///Returns a reference to the NodeMap of the edges of the
   297     ///shortest path tree.
   298     ///\pre \ref run() must be called before using this function.
   299     const PredMap &predMap() const { return *predecessor;}
   300  
   301     ///Returns a reference to the map of nodes of shortest paths.
   302 
   303     ///Returns a reference to the NodeMap of the last but one nodes of the
   304     ///shortest path tree.
   305     ///\pre \ref run() must be called before using this function.
   306     const PredNodeMap &predNodeMap() const { return *pred_node;}
   307 
   308     ///Checks if a node is reachable from the root.
   309 
   310     ///Returns \c true if \c v is reachable from the root.
   311     ///\warning the root node is reported to be unreached!
   312     ///\todo Is this what we want?
   313     ///\pre \ref run() must be called before using this function.
   314     ///
   315     bool reached(Node v) { return G->valid((*predecessor)[v]); }
   316     
   317   };
   318   
   319 
   320   // **********************************************************************
   321   //  IMPLEMENTATIONS
   322   // **********************************************************************
   323 
   324 /// @}
   325   
   326 } //END OF NAMESPACE HUGO
   327 
   328 #endif
   329 
   330