3 #ifndef HUGO_DIJKSTRA_H
4 #define HUGO_DIJKSTRA_H
7 ///\brief Dijkstra algorithm.
15 ///%Dijkstra algorithm class.
17 ///This class provides an efficient implementation of %Dijkstra algorithm.
18 ///The edge lengths are passed to the algorithm using a
19 ///\ref ReadMapSkeleton "readable map",
20 ///so it is easy to change it to any kind of length.
22 ///The type of the length is determined by the \c ValueType of the length map.
24 ///It is also possible to change the underlying priority heap.
26 ///\param Graph The graph type the algorithm runs on.
27 ///\param LengthMap This read-only EdgeMap determines the lengths of
28 ///the edges. It is read once for each edge, so the map may involve
29 ///in relatively time consuming process to compute the edge length
30 ///if it is necessary. The default map type is \ref
31 ///GraphSkeleton::EdgeMap "Graph::EdgeMap<int>"
32 ///\param Heap The heap type used by the %Dijkstra algorithm. The
33 ///default is using \ref BinHeap "binary heap".
36 template <typename Graph,
40 template <typename Graph,
41 typename LengthMap=typename Graph::EdgeMap<int>,
42 template <class,class,class> class Heap = BinHeap >
46 typedef typename Graph::Node Node;
47 typedef typename Graph::NodeIt NodeIt;
48 typedef typename Graph::Edge Edge;
49 typedef typename Graph::OutEdgeIt OutEdgeIt;
51 typedef typename LengthMap::ValueType ValueType;
52 typedef typename Graph::NodeMap<Edge> PredMap;
53 typedef typename Graph::NodeMap<Node> PredNodeMap;
54 typedef typename Graph::NodeMap<ValueType> DistMap;
58 const LengthMap& length;
60 PredNodeMap pred_node;
65 Dijkstra(Graph& _G, LengthMap& _length) :
66 G(_G), length(_length), predecessor(_G), pred_node(_G), distance(_G) { }
70 ///The distance of a node from the source.
72 ///Returns the distance of a node from the source.
73 ///\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 ///of this funcion is undefined.
76 ValueType dist(Node v) const { return distance[v]; }
78 ///Returns the edges of the shortest path tree.
80 ///For a node \c v it returns the last edge of the shortest path
81 ///from the source to \c v or INVALID if \c v is unreachable
83 ///\pre \ref run() must be called before using this function.
84 Edge pred(Node v) const { return predecessor[v]; }
86 ///Returns the nodes of the shortest paths.
88 ///For a node \c v it returns the last but one node of the shortest path
89 ///from the source to \c v or INVALID if \c v is unreachable
91 ///\pre \ref run() must be called before using this function.
92 Node predNode(Node v) const { return pred_node[v]; }
94 ///Returns a reference to the NodeMap of distances.
96 ///\pre \ref run() must be called before using this function.
98 const DistMap &distMap() const { return distance;}
100 ///Returns a reference to the shortest path tree map.
102 ///Returns a reference to the NodeMap of the edges of the
103 ///shortest path tree.
104 ///\pre \ref run() must be called before using this function.
105 const PredMap &predMap() const { return predecessor;}
107 ///Returns a reference to the map of nodes of shortest paths.
109 ///Returns a reference to the NodeMap of the last but one nodes of the
111 ///\pre \ref run() must be called before using this function.
112 const PredNodeMap &predNodeMap() const { return pred_node;}
114 ///Checks if a node is reachable from the source.
116 ///Returns \c true if \c v is reachable from the source.
117 ///\warning the source node is reported to be unreached!
118 ///\todo Is this what we want?
119 ///\pre \ref run() must be called before using this function.
120 bool reached(Node v) { return G.valid(predecessor[v]); }
125 // **********************************************************************
127 // **********************************************************************
129 ///Runs %Dijkstra algorithm from source node \c s.
131 ///This method runs the %Dijkstra algorithm from a source node \c s
132 ///in order to compute the shortest path to each node. The algorithm
133 ///computes - The shortest path tree. - The distance of each node
135 template <typename Graph, typename LengthMap,
136 template<class,class,class> class Heap >
137 void Dijkstra<Graph,LengthMap,Heap>::run(Node s) {
140 for ( G.first(u) ; G.valid(u) ; G.next(u) ) {
141 predecessor.set(u,INVALID);
142 pred_node.set(u,INVALID);
145 typename Graph::NodeMap<int> heap_map(G,-1);
147 Heap<Node,ValueType,typename Graph::NodeMap<int> > heap(heap_map);
150 while ( !heap.empty() ) {
153 ValueType oldvalue=heap[v];
155 distance.set(v, oldvalue);
157 { //FIXME this bracket is for e to be local
159 for(G.first(e, v); G.valid(e); G.next(e)) {
162 switch(heap.state(w)) {
164 heap.push(w,oldvalue+length[e]);
165 predecessor.set(w,e);
169 if ( oldvalue+length[e] < heap[w] ) {
170 heap.decrease(w, oldvalue+length[e]);
171 predecessor.set(w,e);
179 } //FIXME this bracket
183 } //END OF NAMESPACE HUGO