Specialized ConstMap for defining constant maps at compile time, by klao.
Time comparision of the generic and specialized maps.
2 #ifndef HUGO_DIJKSTRA_H
3 #define HUGO_DIJKSTRA_H
7 ///\brief Dijkstra algorithm.
9 #include <hugo/bin_heap.h>
10 #include <hugo/invalid.h>
14 /// \addtogroup flowalgs
17 ///%Dijkstra algorithm class.
19 ///This class provides an efficient implementation of %Dijkstra algorithm.
20 ///The edge lengths are passed to the algorithm using a
21 ///\ref skeleton::ReadMap "ReadMap",
22 ///so it is easy to change it to any kind of length.
24 ///The type of the length is determined by the
25 ///\ref skeleton::ReadMap::ValueType "ValueType" of the length map.
27 ///It is also possible to change the underlying priority heap.
29 ///\param GR The graph type the algorithm runs on.
30 ///\param LM This read-only
33 ///lengths of the edges. It is read once for each edge, so the map
34 ///may involve in relatively time consuming process to compute the edge
35 ///length if it is necessary. The default map type is
36 ///\ref skeleton::StaticGraph::EdgeMap "Graph::EdgeMap<int>"
37 ///\param Heap The heap type used by the %Dijkstra
38 ///algorithm. The default
39 ///is using \ref BinHeap "binary heap".
41 ///\author Jacint Szabo and Alpar Juttner
42 ///\todo We need a typedef-names should be standardized. (-:
43 ///\todo Type of \c PredMap, \c PredNodeMap and \c DistMap
44 ///should not be fixed. (Problematic to solve).
47 template <typename GR,
51 template <typename GR,
52 typename LM=typename GR::template EdgeMap<int>,
53 template <class,class,class,class> class Heap = BinHeap >
57 ///The type of the underlying graph.
60 typedef typename Graph::Node Node;
62 typedef typename Graph::NodeIt NodeIt;
64 typedef typename Graph::Edge Edge;
66 typedef typename Graph::OutEdgeIt OutEdgeIt;
68 ///The type of the length of the edges.
69 typedef typename LM::ValueType ValueType;
70 ///The type of the map that stores the edge lengths.
72 ///\brief The type of the map that stores the last
73 ///edges of the shortest paths.
74 typedef typename Graph::template NodeMap<Edge> PredMap;
75 ///\brief The type of the map that stores the last but one
76 ///nodes of the shortest paths.
77 typedef typename Graph::template NodeMap<Node> PredNodeMap;
78 ///The type of the map that stores the dists of the nodes.
79 typedef typename Graph::template NodeMap<ValueType> DistMap;
82 /// Pointer to the underlying graph.
84 /// Pointer to the length map
86 ///Pointer to the map of predecessors edges.
88 ///Indicates if \ref predecessor is locally allocated (\c true) or not.
89 bool local_predecessor;
90 ///Pointer to the map of predecessors nodes.
91 PredNodeMap *pred_node;
92 ///Indicates if \ref pred_node is locally allocated (\c true) or not.
94 ///Pointer to the map of distances.
96 ///Indicates if \ref distance is locally allocated (\c true) or not.
99 ///The source node of the last execution.
102 ///Initializes the maps.
104 ///\todo Error if \c G or are \c NULL. What about \c length?
105 ///\todo Better memory allocation (instead of new).
109 local_predecessor = true;
110 predecessor = new PredMap(*G);
113 local_pred_node = true;
114 pred_node = new PredNodeMap(*G);
117 local_distance = true;
118 distance = new DistMap(*G);
125 ///\param _G the graph the algorithm will run on.
126 ///\param _length the length map used by the algorithm.
127 Dijkstra(const Graph& _G, const LM& _length) :
128 G(&_G), length(&_length),
129 predecessor(NULL), local_predecessor(false),
130 pred_node(NULL), local_pred_node(false),
131 distance(NULL), local_distance(false)
137 if(local_predecessor) delete predecessor;
138 if(local_pred_node) delete pred_node;
139 if(local_distance) delete distance;
142 ///Sets the length map.
144 ///Sets the length map.
145 ///\return <tt> (*this) </tt>
146 Dijkstra &setLengthMap(const LM &m)
152 ///Sets the map storing the predecessor edges.
154 ///Sets the map storing the predecessor edges.
155 ///If you don't use this function before calling \ref run(),
156 ///it will allocate one. The destuctor deallocates this
157 ///automatically allocated map, of course.
158 ///\return <tt> (*this) </tt>
159 Dijkstra &setPredMap(PredMap &m)
161 if(local_predecessor) {
163 local_predecessor=false;
169 ///Sets the map storing the predecessor nodes.
171 ///Sets the map storing the predecessor nodes.
172 ///If you don't use this function before calling \ref run(),
173 ///it will allocate one. The destuctor deallocates this
174 ///automatically allocated map, of course.
175 ///\return <tt> (*this) </tt>
176 Dijkstra &setPredNodeMap(PredNodeMap &m)
178 if(local_pred_node) {
180 local_pred_node=false;
186 ///Sets the map storing the distances calculated by the algorithm.
188 ///Sets the map storing the distances calculated by the algorithm.
189 ///If you don't use this function before calling \ref run(),
190 ///it will allocate one. The destuctor deallocates this
191 ///automatically allocated map, of course.
192 ///\return <tt> (*this) </tt>
193 Dijkstra &setDistMap(DistMap &m)
197 local_distance=false;
203 ///Runs %Dijkstra algorithm from node \c s.
205 ///This method runs the %Dijkstra algorithm from a root node \c s
208 ///shortest path to each node. The algorithm computes
209 ///- The shortest path tree.
210 ///- The distance of each node from the root.
218 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
219 predecessor->set(u,INVALID);
220 pred_node->set(u,INVALID);
223 typename GR::template NodeMap<int> heap_map(*G,-1);
225 typedef Heap<Node, ValueType, typename GR::template NodeMap<int>,
226 std::less<ValueType> >
229 HeapType heap(heap_map);
233 while ( !heap.empty() ) {
236 ValueType oldvalue=heap[v];
238 distance->set(v, oldvalue);
241 for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
243 switch(heap.state(w)) {
244 case HeapType::PRE_HEAP:
245 heap.push(w,oldvalue+(*length)[e]);
246 predecessor->set(w,e);
249 case HeapType::IN_HEAP:
250 if ( oldvalue+(*length)[e] < heap[w] ) {
251 heap.decrease(w, oldvalue+(*length)[e]);
252 predecessor->set(w,e);
256 case HeapType::POST_HEAP:
263 ///The distance of a node from the root.
265 ///Returns the distance of a node from the root.
266 ///\pre \ref run() must be called before using this function.
267 ///\warning If node \c v in unreachable from the root the return value
268 ///of this funcion is undefined.
269 ValueType dist(Node v) const { return (*distance)[v]; }
271 ///Returns the 'previous edge' of the shortest path tree.
273 ///For a node \c v it returns the 'previous edge' of the shortest path tree,
274 ///i.e. it returns the last edge of a shortest path from the root to \c
275 ///v. It is \ref INVALID
276 ///if \c v is unreachable from the root or if \c v=s. The
277 ///shortest path tree used here is equal to the shortest path tree used in
278 ///\ref predNode(Node v). \pre \ref run() must be called before using
280 ///\todo predEdge could be a better name.
281 Edge pred(Node v) const { return (*predecessor)[v]; }
283 ///Returns the 'previous node' of the shortest path tree.
285 ///For a node \c v it returns the 'previous node' of the shortest path tree,
286 ///i.e. it returns the last but one node from a shortest path from the
287 ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
288 ///\c v=s. The shortest path tree used here is equal to the shortest path
289 ///tree used in \ref pred(Node v). \pre \ref run() must be called before
290 ///using this function.
291 Node predNode(Node v) const { return (*pred_node)[v]; }
293 ///Returns a reference to the NodeMap of distances.
295 ///Returns a reference to the NodeMap of distances. \pre \ref run() must
296 ///be called before using this function.
297 const DistMap &distMap() const { return *distance;}
299 ///Returns a reference to the shortest path tree map.
301 ///Returns a reference to the NodeMap of the edges of the
302 ///shortest path tree.
303 ///\pre \ref run() must be called before using this function.
304 const PredMap &predMap() const { return *predecessor;}
306 ///Returns a reference to the map of nodes of shortest paths.
308 ///Returns a reference to the NodeMap of the last but one nodes of the
309 ///shortest path tree.
310 ///\pre \ref run() must be called before using this function.
311 const PredNodeMap &predNodeMap() const { return *pred_node;}
313 ///Checks if a node is reachable from the root.
315 ///Returns \c true if \c v is reachable from the root.
316 ///\note The root node is reported to be reached!
317 ///\pre \ref run() must be called before using this function.
319 bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
325 } //END OF NAMESPACE HUGO