src/hugo/dijkstra.h
author marci
Tue, 11 May 2004 15:54:00 +0000
changeset 609 0566ac97809b
parent 570 eec0a62979c9
child 688 bdc429a557f2
permissions -rw-r--r--
misc
     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
    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 the type of the map that stores the edge lengths.
    64     typedef LM LengthMap;
    65     ///\brief The 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 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 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     PredMap predecessor;
    78     PredNodeMap pred_node;
    79     DistMap distance;
    80     
    81   public :
    82     
    83     Dijkstra(const Graph& _G, const LM& _length) :
    84       G(_G), length(_length), predecessor(_G), pred_node(_G), distance(_G) { }
    85     
    86     void run(Node s);
    87     
    88     ///The distance of a node from the root.
    89 
    90     ///Returns the distance of a node from the root.
    91     ///\pre \ref run() must be called before using this function.
    92     ///\warning If node \c v in unreachable from the root the return value
    93     ///of this funcion is undefined.
    94     ValueType dist(Node v) const { return distance[v]; }
    95 
    96     ///Returns the 'previous edge' of the shortest path tree.
    97 
    98     ///For a node \c v it returns the 'previous edge' of the shortest path tree,
    99     ///i.e. it returns the last edge from a shortest path from the root to \c
   100     ///v. It is INVALID if \c v is unreachable from the root or if \c v=s. The
   101     ///shortest path tree used here is equal to the shortest path tree used in
   102     ///\ref predNode(Node v).  \pre \ref run() must be called before using
   103     ///this function.
   104     Edge pred(Node v) const { return predecessor[v]; }
   105 
   106     ///Returns the 'previous node' of the shortest path tree.
   107 
   108     ///For a node \c v it returns the 'previous node' of the shortest path tree,
   109     ///i.e. it returns the last but one node from a shortest path from the
   110     ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
   111     ///\c v=s. The shortest path tree used here is equal to the shortest path
   112     ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
   113     ///using this function.
   114     Node predNode(Node v) const { return pred_node[v]; }
   115     
   116     ///Returns a reference to the NodeMap of distances.
   117 
   118     ///Returns a reference to the NodeMap of distances. \pre \ref run() must
   119     ///be called before using this function.
   120     const DistMap &distMap() const { return distance;}
   121  
   122     ///Returns a reference to the shortest path tree map.
   123 
   124     ///Returns a reference to the NodeMap of the edges of the
   125     ///shortest path tree.
   126     ///\pre \ref run() must be called before using this function.
   127     const PredMap &predMap() const { return predecessor;}
   128  
   129     ///Returns a reference to the map of nodes of shortest paths.
   130 
   131     ///Returns a reference to the NodeMap of the last but one nodes of the
   132     ///shortest path tree.
   133     ///\pre \ref run() must be called before using this function.
   134     const PredNodeMap &predNodeMap() const { return pred_node;}
   135 
   136     ///Checks if a node is reachable from the root.
   137 
   138     ///Returns \c true if \c v is reachable from the root.
   139     ///\warning the root node is reported to be unreached!
   140     ///\todo Is this what we want?
   141     ///\pre \ref run() must be called before using this function.
   142     ///
   143     bool reached(Node v) { return G.valid(predecessor[v]); }
   144     
   145   };
   146   
   147 
   148   // **********************************************************************
   149   //  IMPLEMENTATIONS
   150   // **********************************************************************
   151 
   152   ///Runs %Dijkstra algorithm from node the root.
   153 
   154   ///This method runs the %Dijkstra algorithm from a root node \c s
   155   ///in order to
   156   ///compute the
   157   ///shortest path to each node. The algorithm computes
   158   ///- The shortest path tree.
   159   ///- The distance of each node from the root.
   160   template <typename GR, typename LM,
   161 	    template<class,class,class,class> class Heap >
   162   void Dijkstra<GR,LM,Heap>::run(Node s) {
   163     
   164     NodeIt u;
   165     for ( G.first(u) ; G.valid(u) ; G.next(u) ) {
   166       predecessor.set(u,INVALID);
   167       pred_node.set(u,INVALID);
   168     }
   169     
   170     typename GR::template NodeMap<int> heap_map(G,-1);
   171     
   172     typedef Heap<Node, ValueType, typename GR::template NodeMap<int>,
   173       std::less<ValueType> > 
   174       HeapType;
   175     
   176     HeapType heap(heap_map);
   177     
   178     heap.push(s,0); 
   179     
   180       while ( !heap.empty() ) {
   181 	
   182 	Node v=heap.top(); 
   183 	ValueType oldvalue=heap[v];
   184 	heap.pop();
   185 	distance.set(v, oldvalue);
   186 	
   187 	{ //FIXME this bracket is for e to be local
   188 	  OutEdgeIt e;
   189 	for(G.first(e, v);
   190 	    G.valid(e); G.next(e)) {
   191 	  Node w=G.bNode(e); 
   192 	  
   193 	  switch(heap.state(w)) {
   194 	  case HeapType::PRE_HEAP:
   195 	    heap.push(w,oldvalue+length[e]); 
   196 	    predecessor.set(w,e);
   197 	    pred_node.set(w,v);
   198 	    break;
   199 	  case HeapType::IN_HEAP:
   200 	    if ( oldvalue+length[e] < heap[w] ) {
   201 	      heap.decrease(w, oldvalue+length[e]); 
   202 	      predecessor.set(w,e);
   203 	      pred_node.set(w,v);
   204 	    }
   205 	    break;
   206 	  case HeapType::POST_HEAP:
   207 	    break;
   208 	  }
   209 	}
   210       } //FIXME tis bracket
   211       }
   212   }
   213 
   214 /// @}
   215   
   216 } //END OF NAMESPACE HUGO
   217 
   218 #endif
   219 
   220