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