src/include/dijkstra.h
changeset 385 d7ebbae96025
parent 373 259ea2d741a2
child 421 54b943063901
equal deleted inserted replaced
6:e646d4053290 7:5552f6b71d27
     1 // -*- C++ -*-
     1 // -*- C++ -*-
     2 
       
     3 #ifndef HUGO_DIJKSTRA_H
     2 #ifndef HUGO_DIJKSTRA_H
     4 #define HUGO_DIJKSTRA_H
     3 #define HUGO_DIJKSTRA_H
     5 
     4 
     6 ///\file
     5 ///\file
     7 ///\brief Dijkstra algorithm.
     6 ///\brief Dijkstra algorithm.
     8 
     7 
     9 #include <fib_heap.h>
       
    10 #include <bin_heap.h>
     8 #include <bin_heap.h>
    11 #include <invalid.h>
     9 #include <invalid.h>
    12 
    10 
    13 namespace hugo {
    11 namespace hugo {
    14   
    12 
    15   ///%Dijkstra algorithm class.
    13   ///%Dijkstra algorithm class.
    16 
    14 
    17   ///This class provides an efficient implementation of %Dijkstra algorithm.
    15   ///This class provides an efficient implementation of %Dijkstra algorithm.
    18   ///The edge lengths are passed to the algorithm using a
    16   ///The edge lengths are passed to the algorithm using a
    19   ///\ref ReadMapSkeleton "readable map",
    17   ///\ref ReadMapSkeleton "readable map",
    21   ///
    19   ///
    22   ///The type of the length is determined by the \c ValueType of the length map.
    20   ///The type of the length is determined by the \c ValueType of the length map.
    23   ///
    21   ///
    24   ///It is also possible to change the underlying priority heap.
    22   ///It is also possible to change the underlying priority heap.
    25   ///
    23   ///
    26   ///\param Graph The graph type the algorithm runs on.  
    24   ///\param Graph The graph type the algorithm runs on.
    27   ///\param LengthMap This read-only EdgeMap determines the lengths of
    25   ///\param LengthMap This read-only
    28   ///the edges. It is read once for each edge, so the map may involve
    26   ///EdgeMap
    29   ///in relatively time consuming process to compute the edge length
    27   ///determines the
    30   ///if it is necessary. The default map type is \ref
    28   ///lengths of the edges. It is read once for each edge, so the map
    31   ///GraphSkeleton::EdgeMap "Graph::EdgeMap<int>"
    29   ///may involve in relatively time consuming process to compute the edge
    32   ///\param Heap The heap type used by the %Dijkstra algorithm. The
    30   ///length if it is necessary. The default map type is
    33   ///default is using \ref BinHeap "binary heap".
    31   ///\ref GraphSkeleton::EdgeMap "Graph::EdgeMap<int>"
       
    32   ///\param Heap The heap type used by the %Dijkstra
       
    33   ///algorithm. The default
       
    34   ///is using \ref BinHeap "binary heap".
    34   
    35   
    35 #ifdef DOXYGEN
    36 #ifdef DOXYGEN
    36   template <typename Graph,
    37   template <typename Graph,
    37 	    typename LengthMap,
    38 	    typename LengthMap,
    38 	    typename Heap>
    39 	    typename Heap>
    65     Dijkstra(Graph& _G, LengthMap& _length) :
    66     Dijkstra(Graph& _G, LengthMap& _length) :
    66       G(_G), length(_length), predecessor(_G), pred_node(_G), distance(_G) { }
    67       G(_G), length(_length), predecessor(_G), pred_node(_G), distance(_G) { }
    67     
    68     
    68     void run(Node s);
    69     void run(Node s);
    69     
    70     
    70     ///The distance of a node from the source.
    71     ///The distance of a node from the root.
    71 
    72 
    72     ///Returns the distance of a node from the source.
    73     ///Returns the distance of a node from the root.
    73     ///\pre \ref run() must be called before using this function.
    74     ///\pre \ref run() must be called before using this function.
    74     ///\warning If node \c v in unreachable from the source the return value
    75     ///\warning If node \c v in unreachable from the root the return value
    75     ///of this funcion is undefined.
    76     ///of this funcion is undefined.
    76     ValueType dist(Node v) const { return distance[v]; }
    77     ValueType dist(Node v) const { return distance[v]; }
    77 
    78 
    78     ///Returns the edges of the shortest path tree.
    79     ///Returns the previous edge of the shortest path tree.
    79 
    80 
    80     ///For a node \c v it returns the last edge of the shortest path
    81     ///For a node \c v it returns the previous edge of the shortest path tree,
    81     ///from the source to \c v or INVALID if \c v is unreachable
    82     ///i.e. it returns the last edge from a shortest path from the root to \c
    82     ///from the source.
    83     ///v. It is INVALID if \c v is unreachable from the root or if \c v=s. The
    83     ///\pre \ref run() must be called before using this function.
    84     ///shortest path tree used here is equal to the shortest path tree used in
       
    85     ///\ref predNode(Node v).  \pre \ref run() must be called before using
       
    86     ///this function.
    84     Edge pred(Node v) const { return predecessor[v]; }
    87     Edge pred(Node v) const { return predecessor[v]; }
    85 
    88 
    86     ///Returns the nodes of the shortest paths.
    89     ///Returns the previous node of the shortest path tree.
    87 
    90 
    88     ///For a node \c v it returns the last but one node of the shortest path
    91     ///For a node \c v it returns the previous node of the shortest path tree,
    89     ///from the source to \c v or INVALID if \c v is unreachable
    92     ///i.e. it returns the last but one node from a shortest path from the
    90     ///from the source.
    93     ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
    91     ///\pre \ref run() must be called before using this function.
    94     ///\c v=s. The shortest path tree used here is equal to the shortest path
       
    95     ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
       
    96     ///using this function.
    92     Node predNode(Node v) const { return pred_node[v]; }
    97     Node predNode(Node v) const { return pred_node[v]; }
    93     
    98     
    94     ///Returns a reference to the NodeMap of distances.
    99     ///Returns a reference to the NodeMap of distances.
    95 
   100 
    96     ///\pre \ref run() must be called before using this function.
   101     ///Returns a reference to the NodeMap of distances. \pre \ref run() must
    97     ///
   102     ///be called before using this function.
    98     const DistMap &distMap() const { return distance;}
   103     const DistMap &distMap() const { return distance;}
    99     
   104  
   100     ///Returns a reference to the shortest path tree map.
   105     ///Returns a reference to the shortest path tree map.
   101 
   106 
   102     ///Returns a reference to the NodeMap of the edges of the
   107     ///Returns a reference to the NodeMap of the edges of the
   103     ///shortest path tree.
   108     ///shortest path tree.
   104     ///\pre \ref run() must be called before using this function.
   109     ///\pre \ref run() must be called before using this function.
   105     const PredMap &predMap() const { return predecessor;}
   110     const PredMap &predMap() const { return predecessor;}
   106     
   111  
   107     ///Returns a reference to the map of nodes of  shortest paths.
   112     ///Returns a reference to the map of nodes of shortest paths.
   108 
   113 
   109     ///Returns a reference to the NodeMap of the last but one nodes of the
   114     ///Returns a reference to the NodeMap of the last but one nodes of the
   110     ///shortest paths.
   115     ///shortest path tree.
   111     ///\pre \ref run() must be called before using this function.
   116     ///\pre \ref run() must be called before using this function.
   112     const PredNodeMap &predNodeMap() const { return pred_node;}
   117     const PredNodeMap &predNodeMap() const { return pred_node;}
   113 
   118 
   114     ///Checks if a node is reachable from the source.
   119     ///Checks if a node is reachable from the root.
   115 
   120 
   116     ///Returns \c true if \c v is reachable from the source.
   121     ///Returns \c true if \c v is reachable from the root.
   117     ///\warning the source node is reported to be unreached!
   122     ///\warning the root node is reported to be unreached!
   118     ///\todo Is this what we want?
   123     ///\todo Is this what we want?
   119     ///\pre \ref run() must be called before using this function.
   124     ///\pre \ref run() must be called before using this function.
       
   125     ///
   120     bool reached(Node v) { return G.valid(predecessor[v]); }
   126     bool reached(Node v) { return G.valid(predecessor[v]); }
   121     
   127     
   122   };
   128   };
   123   
   129   
   124 
   130 
   125   // **********************************************************************
   131   // **********************************************************************
   126   //  IMPLEMENTATIONS
   132   //  IMPLEMENTATIONS
   127   // **********************************************************************
   133   // **********************************************************************
   128 
   134 
   129   ///Runs %Dijkstra algorithm from source node \c s.
   135   ///Runs %Dijkstra algorithm from node the root.
   130 
   136 
   131   ///This method runs the %Dijkstra algorithm from a source node \c s
   137   ///This method runs the %Dijkstra algorithm from a root node \c s
   132   ///in order to compute the shortest path to each node. The algorithm
   138   ///in order to
   133   ///computes - The shortest path tree.  - The distance of each node
   139   ///compute the
   134   ///from the source.
   140   ///shortest path to each node. The algorithm computes
       
   141   ///- The shortest path tree.
       
   142   ///- The distance of each node from the root.
   135   template <typename Graph, typename LengthMap,
   143   template <typename Graph, typename LengthMap,
   136 	    template<class,class,class> class Heap >
   144 	    template<class,class,class> class Heap >
   137   void Dijkstra<Graph,LengthMap,Heap>::run(Node s) {
   145   void Dijkstra<Graph,LengthMap,Heap>::run(Node s) {
   138     
   146     
   139     NodeIt u;
   147     NodeIt u;
   143     }
   151     }
   144     
   152     
   145     typename Graph::NodeMap<int> heap_map(G,-1);
   153     typename Graph::NodeMap<int> heap_map(G,-1);
   146     
   154     
   147     Heap<Node,ValueType,typename Graph::NodeMap<int> > heap(heap_map);
   155     Heap<Node,ValueType,typename Graph::NodeMap<int> > heap(heap_map);
       
   156     
   148     heap.push(s,0); 
   157     heap.push(s,0); 
   149     
   158     
   150     while ( !heap.empty() ) {
   159       while ( !heap.empty() ) {
   151       
       
   152       Node v=heap.top(); 
       
   153       ValueType oldvalue=heap[v];
       
   154       heap.pop();
       
   155       distance.set(v, oldvalue);
       
   156 	
   160 	
   157       { //FIXME this bracket is for e to be local
   161 	Node v=heap.top(); 
   158 	OutEdgeIt e;
   162 	ValueType oldvalue=heap[v];
   159 	for(G.first(e, v); G.valid(e); G.next(e)) {
   163 	heap.pop();
       
   164 	distance.set(v, oldvalue);
       
   165 	
       
   166 	{ //FIXME this bracket is for e to be local
       
   167 	  OutEdgeIt e;
       
   168 	for(G.first(e, v);
       
   169 	    G.valid(e); G.next(e)) {
   160 	  Node w=G.head(e); 
   170 	  Node w=G.head(e); 
   161 	  
   171 	  
   162 	  switch(heap.state(w)) {
   172 	  switch(heap.state(w)) {
   163 	  case heap.PRE_HEAP:
   173 	  case heap.PRE_HEAP:
   164 	    heap.push(w,oldvalue+length[e]); 
   174 	    heap.push(w,oldvalue+length[e]); 
   174 	    break;
   184 	    break;
   175 	  case heap.POST_HEAP:
   185 	  case heap.POST_HEAP:
   176 	    break;
   186 	    break;
   177 	  }
   187 	  }
   178 	}
   188 	}
   179       } //FIXME this bracket
   189       } //FIXME tis bracket
   180     }
   190       }
   181   }
   191   }
   182   
   192   
   183 } //END OF NAMESPACE HUGO
   193 } //END OF NAMESPACE HUGO
   184 
   194 
   185 #endif
   195 #endif