src/hugo/dijkstra.h
author alpar
Mon, 30 Aug 2004 12:01:47 +0000
changeset 774 4297098d9677
parent 758 49b1a30c4dc4
child 776 f2994a2b10b2
permissions -rw-r--r--
Merge back the whole branches/hugo++ to trunk.
     1 // -*- C++ -*-
     2 #ifndef HUGO_DIJKSTRA_H
     3 #define HUGO_DIJKSTRA_H
     4 
     5 ///\ingroup flowalgs
     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 flowalgs
    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     //The source node of the last execution.
    88     Node source;
    89 
    90     ///Initialize maps
    91     
    92     ///\todo Error if \c G or are \c NULL. What about \c length?
    93     ///\todo Better memory allocation (instead of new).
    94     void init_maps() 
    95     {
    96 //       if(!length) {
    97 // 	local_length = true;
    98 // 	length = new LM(G);
    99 //       }
   100       if(!predecessor) {
   101 	local_predecessor = true;
   102 	predecessor = new PredMap(*G);
   103       }
   104       if(!pred_node) {
   105 	local_pred_node = true;
   106 	pred_node = new PredNodeMap(*G);
   107       }
   108       if(!distance) {
   109 	local_distance = true;
   110 	distance = new DistMap(*G);
   111       }
   112     }
   113     
   114   public :
   115     
   116     Dijkstra(const Graph& _G, const LM& _length) :
   117       G(&_G), length(&_length),
   118       predecessor(NULL), local_predecessor(false),
   119       pred_node(NULL), local_pred_node(false),
   120       distance(NULL), local_distance(false)
   121     { }
   122     
   123     ~Dijkstra() 
   124     {
   125       //      if(local_length) delete length;
   126       if(local_predecessor) delete predecessor;
   127       if(local_pred_node) delete pred_node;
   128       if(local_distance) delete distance;
   129     }
   130 
   131     ///Sets the graph the algorithm will run on.
   132 
   133     ///Sets the graph the algorithm will run on.
   134     ///\return <tt> (*this) </tt>
   135     Dijkstra &setGraph(const Graph &_G) 
   136     {
   137       G = &_G;
   138       return *this;
   139     }
   140     ///Sets the length map.
   141 
   142     ///Sets the length map.
   143     ///\return <tt> (*this) </tt>
   144     Dijkstra &setLengthMap(const LM &m) 
   145     {
   146 //       if(local_length) {
   147 // 	delete length;
   148 // 	local_length=false;
   149 //       }
   150       length = &m;
   151       return *this;
   152     }
   153 
   154     ///Sets the map storing the predecessor edges.
   155 
   156     ///Sets the map storing the predecessor edges.
   157     ///If you don't use this function before calling \ref run(),
   158     ///it will allocate one. The destuctor deallocates this
   159     ///automatically allocated map, of course.
   160     ///\return <tt> (*this) </tt>
   161     Dijkstra &setPredMap(PredMap &m) 
   162     {
   163       if(local_predecessor) {
   164 	delete predecessor;
   165 	local_predecessor=false;
   166       }
   167       predecessor = &m;
   168       return *this;
   169     }
   170 
   171     ///Sets the map storing the predecessor nodes.
   172 
   173     ///Sets the map storing the predecessor nodes.
   174     ///If you don't use this function before calling \ref run(),
   175     ///it will allocate one. The destuctor deallocates this
   176     ///automatically allocated map, of course.
   177     ///\return <tt> (*this) </tt>
   178     Dijkstra &setPredNodeMap(PredNodeMap &m) 
   179     {
   180       if(local_pred_node) {
   181 	delete pred_node;
   182 	local_pred_node=false;
   183       }
   184       pred_node = &m;
   185       return *this;
   186     }
   187 
   188     ///Sets the map storing the distances calculated by the algorithm.
   189 
   190     ///Sets the map storing the distances calculated by the algorithm.
   191     ///If you don't use this function before calling \ref run(),
   192     ///it will allocate one. The destuctor deallocates this
   193     ///automatically allocated map, of course.
   194     ///\return <tt> (*this) </tt>
   195     Dijkstra &setDistMap(DistMap &m) 
   196     {
   197       if(local_distance) {
   198 	delete distance;
   199 	local_distance=false;
   200       }
   201       distance = &m;
   202       return *this;
   203     }
   204     
   205   ///Runs %Dijkstra algorithm from node \c s.
   206 
   207   ///This method runs the %Dijkstra algorithm from a root node \c s
   208   ///in order to
   209   ///compute the
   210   ///shortest path to each node. The algorithm computes
   211   ///- The shortest path tree.
   212   ///- The distance of each node from the root.
   213     
   214     void run(Node s) {
   215       
   216       init_maps();
   217       
   218       source = s;
   219       
   220       for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
   221 	predecessor->set(u,INVALID);
   222 	pred_node->set(u,INVALID);
   223       }
   224       
   225       typename GR::template NodeMap<int> heap_map(*G,-1);
   226       
   227       typedef Heap<Node, ValueType, typename GR::template NodeMap<int>,
   228       std::less<ValueType> > 
   229       HeapType;
   230       
   231       HeapType heap(heap_map);
   232       
   233       heap.push(s,0); 
   234       
   235       while ( !heap.empty() ) {
   236 	
   237 	Node v=heap.top(); 
   238 	ValueType oldvalue=heap[v];
   239 	heap.pop();
   240 	distance->set(v, oldvalue);
   241 	
   242 	
   243 	for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
   244 	  Node w=G->head(e); 
   245 	  
   246 	  switch(heap.state(w)) {
   247 	  case HeapType::PRE_HEAP:
   248 	    heap.push(w,oldvalue+(*length)[e]); 
   249 	    predecessor->set(w,e);
   250 	    pred_node->set(w,v);
   251 	    break;
   252 	  case HeapType::IN_HEAP:
   253 	    if ( oldvalue+(*length)[e] < heap[w] ) {
   254 	      heap.decrease(w, oldvalue+(*length)[e]); 
   255 	      predecessor->set(w,e);
   256 	      pred_node->set(w,v);
   257 	    }
   258 	    break;
   259 	  case HeapType::POST_HEAP:
   260 	    break;
   261 	  }
   262 	}
   263       }
   264     }
   265     
   266     ///The distance of a node from the root.
   267 
   268     ///Returns the distance of a node from the root.
   269     ///\pre \ref run() must be called before using this function.
   270     ///\warning If node \c v in unreachable from the root the return value
   271     ///of this funcion is undefined.
   272     ValueType dist(Node v) const { return (*distance)[v]; }
   273 
   274     ///Returns the 'previous edge' of the shortest path tree.
   275 
   276     ///For a node \c v it returns the 'previous edge' of the shortest path tree,
   277     ///i.e. it returns the last edge from a shortest path from the root to \c
   278     ///v. It is \ref INVALID
   279     ///if \c v is unreachable from the root or if \c v=s. The
   280     ///shortest path tree used here is equal to the shortest path tree used in
   281     ///\ref predNode(Node v).  \pre \ref run() must be called before using
   282     ///this function.
   283     Edge pred(Node v) const { return (*predecessor)[v]; }
   284 
   285     ///Returns the 'previous node' of the shortest path tree.
   286 
   287     ///For a node \c v it returns the 'previous node' of the shortest path tree,
   288     ///i.e. it returns the last but one node from a shortest path from the
   289     ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
   290     ///\c v=s. The shortest path tree used here is equal to the shortest path
   291     ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
   292     ///using this function.
   293     Node predNode(Node v) const { return (*pred_node)[v]; }
   294     
   295     ///Returns a reference to the NodeMap of distances.
   296 
   297     ///Returns a reference to the NodeMap of distances. \pre \ref run() must
   298     ///be called before using this function.
   299     const DistMap &distMap() const { return *distance;}
   300  
   301     ///Returns a reference to the shortest path tree map.
   302 
   303     ///Returns a reference to the NodeMap of the edges of the
   304     ///shortest path tree.
   305     ///\pre \ref run() must be called before using this function.
   306     const PredMap &predMap() const { return *predecessor;}
   307  
   308     ///Returns a reference to the map of nodes of shortest paths.
   309 
   310     ///Returns a reference to the NodeMap of the last but one nodes of the
   311     ///shortest path tree.
   312     ///\pre \ref run() must be called before using this function.
   313     const PredNodeMap &predNodeMap() const { return *pred_node;}
   314 
   315     ///Checks if a node is reachable from the root.
   316 
   317     ///Returns \c true if \c v is reachable from the root.
   318     ///\warning the root node is reported to be reached!
   319     ///\pre \ref run() must be called before using this function.
   320     ///
   321     bool reached(Node v) { return v==source || (*predecessor)[v]==INVALID; }
   322     
   323   };
   324   
   325 
   326   // **********************************************************************
   327   //  IMPLEMENTATIONS
   328   // **********************************************************************
   329 
   330 /// @}
   331   
   332 } //END OF NAMESPACE HUGO
   333 
   334 #endif
   335 
   336