- 'minlengpaths_test.cc' added.
- Path tests in 'minlengpaths_test.cc' are swithced out.
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.
90 ///Initializes the maps.
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 ///\bug What about maps?
136 ///\todo It may be unnecessary
137 Dijkstra &setGraph(const Graph &_G)
142 ///Sets the length map.
144 ///Sets the length map.
145 ///\return <tt> (*this) </tt>
146 Dijkstra &setLengthMap(const LM &m)
148 // if(local_length) {
150 // local_length=false;
156 ///Sets the map storing the predecessor edges.
158 ///Sets the map storing the predecessor edges.
159 ///If you don't use this function before calling \ref run(),
160 ///it will allocate one. The destuctor deallocates this
161 ///automatically allocated map, of course.
162 ///\return <tt> (*this) </tt>
163 Dijkstra &setPredMap(PredMap &m)
165 if(local_predecessor) {
167 local_predecessor=false;
173 ///Sets the map storing the predecessor nodes.
175 ///Sets the map storing the predecessor nodes.
176 ///If you don't use this function before calling \ref run(),
177 ///it will allocate one. The destuctor deallocates this
178 ///automatically allocated map, of course.
179 ///\return <tt> (*this) </tt>
180 Dijkstra &setPredNodeMap(PredNodeMap &m)
182 if(local_pred_node) {
184 local_pred_node=false;
190 ///Sets the map storing the distances calculated by the algorithm.
192 ///Sets the map storing the distances calculated by the algorithm.
193 ///If you don't use this function before calling \ref run(),
194 ///it will allocate one. The destuctor deallocates this
195 ///automatically allocated map, of course.
196 ///\return <tt> (*this) </tt>
197 Dijkstra &setDistMap(DistMap &m)
201 local_distance=false;
207 ///Runs %Dijkstra algorithm from node \c s.
209 ///This method runs the %Dijkstra algorithm from a root node \c s
212 ///shortest path to each node. The algorithm computes
213 ///- The shortest path tree.
214 ///- The distance of each node from the root.
222 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
223 predecessor->set(u,INVALID);
224 pred_node->set(u,INVALID);
227 typename GR::template NodeMap<int> heap_map(*G,-1);
229 typedef Heap<Node, ValueType, typename GR::template NodeMap<int>,
230 std::less<ValueType> >
233 HeapType heap(heap_map);
237 while ( !heap.empty() ) {
240 ValueType oldvalue=heap[v];
242 distance->set(v, oldvalue);
245 for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
247 switch(heap.state(w)) {
248 case HeapType::PRE_HEAP:
249 heap.push(w,oldvalue+(*length)[e]);
250 predecessor->set(w,e);
253 case HeapType::IN_HEAP:
254 if ( oldvalue+(*length)[e] < heap[w] ) {
255 heap.decrease(w, oldvalue+(*length)[e]);
256 predecessor->set(w,e);
260 case HeapType::POST_HEAP:
267 ///The distance of a node from the root.
269 ///Returns the distance of a node from the root.
270 ///\pre \ref run() must be called before using this function.
271 ///\warning If node \c v in unreachable from the root the return value
272 ///of this funcion is undefined.
273 ValueType dist(Node v) const { return (*distance)[v]; }
275 ///Returns the 'previous edge' of the shortest path tree.
277 ///For a node \c v it returns the 'previous edge' of the shortest path tree,
278 ///i.e. it returns the last edge of a shortest path from the root to \c
279 ///v. It is \ref INVALID
280 ///if \c v is unreachable from the root or if \c v=s. The
281 ///shortest path tree used here is equal to the shortest path tree used in
282 ///\ref predNode(Node v). \pre \ref run() must be called before using
284 ///\todo predEdge could be a better name.
285 Edge pred(Node v) const { return (*predecessor)[v]; }
287 ///Returns the 'previous node' of the shortest path tree.
289 ///For a node \c v it returns the 'previous node' of the shortest path tree,
290 ///i.e. it returns the last but one node from a shortest path from the
291 ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
292 ///\c v=s. The shortest path tree used here is equal to the shortest path
293 ///tree used in \ref pred(Node v). \pre \ref run() must be called before
294 ///using this function.
295 Node predNode(Node v) const { return (*pred_node)[v]; }
297 ///Returns a reference to the NodeMap of distances.
299 ///Returns a reference to the NodeMap of distances. \pre \ref run() must
300 ///be called before using this function.
301 const DistMap &distMap() const { return *distance;}
303 ///Returns a reference to the shortest path tree map.
305 ///Returns a reference to the NodeMap of the edges of the
306 ///shortest path tree.
307 ///\pre \ref run() must be called before using this function.
308 const PredMap &predMap() const { return *predecessor;}
310 ///Returns a reference to the map of nodes of shortest paths.
312 ///Returns a reference to the NodeMap of the last but one nodes of the
313 ///shortest path tree.
314 ///\pre \ref run() must be called before using this function.
315 const PredNodeMap &predNodeMap() const { return *pred_node;}
317 ///Checks if a node is reachable from the root.
319 ///Returns \c true if \c v is reachable from the root.
320 ///\warning the root node is reported to be reached!
321 ///\pre \ref run() must be called before using this function.
323 bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
329 } //END OF NAMESPACE HUGO