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 ReadMapSkeleton "readable map",
22 ///so it is easy to change it to any kind of length.
24 ///The type of the length is determined by the \c ValueType of the length map.
26 ///It is also possible to change the underlying priority heap.
28 ///\param GR The graph type the algorithm runs on.
29 ///\param LM This read-only
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".
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).
46 template <typename GR,
50 template <typename GR,
51 typename LM=typename GR::template EdgeMap<int>,
52 template <class,class,class,class> class Heap = BinHeap >
56 ///The type of the underlying 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;
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.
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;
81 bool local_predecessor;
82 PredNodeMap *pred_node;
87 //The source node of the last execution.
92 ///\todo Error if \c G or are \c NULL. What about \c length?
93 ///\todo Better memory allocation (instead of new).
97 // local_length = true;
98 // length = new LM(G);
101 local_predecessor = true;
102 predecessor = new PredMap(*G);
105 local_pred_node = true;
106 pred_node = new PredNodeMap(*G);
109 local_distance = true;
110 distance = new DistMap(*G);
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)
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;
131 ///Sets the graph the algorithm will run on.
133 ///Sets the graph the algorithm will run on.
134 ///\return <tt> (*this) </tt>
135 Dijkstra &setGraph(const Graph &_G)
140 ///Sets the length map.
142 ///Sets the length map.
143 ///\return <tt> (*this) </tt>
144 Dijkstra &setLengthMap(const LM &m)
146 // if(local_length) {
148 // local_length=false;
154 ///Sets the map storing the predecessor edges.
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)
163 if(local_predecessor) {
165 local_predecessor=false;
171 ///Sets the map storing the predecessor nodes.
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)
180 if(local_pred_node) {
182 local_pred_node=false;
188 ///Sets the map storing the distances calculated by the algorithm.
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)
199 local_distance=false;
205 ///Runs %Dijkstra algorithm from node \c s.
207 ///This method runs the %Dijkstra algorithm from a root node \c s
210 ///shortest path to each node. The algorithm computes
211 ///- The shortest path tree.
212 ///- The distance of each node from the root.
220 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
221 predecessor->set(u,INVALID);
222 pred_node->set(u,INVALID);
225 typename GR::template NodeMap<int> heap_map(*G,-1);
227 typedef Heap<Node, ValueType, typename GR::template NodeMap<int>,
228 std::less<ValueType> >
231 HeapType heap(heap_map);
235 while ( !heap.empty() ) {
238 ValueType oldvalue=heap[v];
240 distance->set(v, oldvalue);
243 for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
245 switch(heap.state(w)) {
246 case HeapType::PRE_HEAP:
247 heap.push(w,oldvalue+(*length)[e]);
248 predecessor->set(w,e);
251 case HeapType::IN_HEAP:
252 if ( oldvalue+(*length)[e] < heap[w] ) {
253 heap.decrease(w, oldvalue+(*length)[e]);
254 predecessor->set(w,e);
258 case HeapType::POST_HEAP:
265 ///The distance of a node from the root.
267 ///Returns the distance of a node from the root.
268 ///\pre \ref run() must be called before using this function.
269 ///\warning If node \c v in unreachable from the root the return value
270 ///of this funcion is undefined.
271 ValueType dist(Node v) const { return (*distance)[v]; }
273 ///Returns the 'previous edge' of the shortest path tree.
275 ///For a node \c v it returns the 'previous edge' of the shortest path tree,
276 ///i.e. it returns the last edge from a shortest path from the root to \c
277 ///v. It is \ref INVALID
278 ///if \c v is unreachable from the root or if \c v=s. The
279 ///shortest path tree used here is equal to the shortest path tree used in
280 ///\ref predNode(Node v). \pre \ref run() must be called before using
282 ///\todo predEdge could be a better name.
283 Edge pred(Node v) const { return (*predecessor)[v]; }
285 ///Returns the 'previous node' of the shortest path tree.
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]; }
295 ///Returns a reference to the NodeMap of distances.
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;}
301 ///Returns a reference to the shortest path tree map.
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;}
308 ///Returns a reference to the map of nodes of shortest paths.
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;}
315 ///Checks if a node is reachable from the root.
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.
321 bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
326 // **********************************************************************
328 // **********************************************************************
332 } //END OF NAMESPACE HUGO