1 | // -*- C++ -*- |
---|
2 | #ifndef HUGO_DIJKSTRA_H |
---|
3 | #define HUGO_DIJKSTRA_H |
---|
4 | |
---|
5 | ///\ingroup galgs |
---|
6 | ///\file |
---|
7 | ///\brief Dijkstra algorithm. |
---|
8 | |
---|
9 | #include <bin_heap.h> |
---|
10 | #include <invalid.h> |
---|
11 | |
---|
12 | namespace hugo { |
---|
13 | |
---|
14 | /// \addtogroup galgs |
---|
15 | /// @{ |
---|
16 | |
---|
17 | ///%Dijkstra algorithm class. |
---|
18 | |
---|
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. |
---|
23 | /// |
---|
24 | ///The type of the length is determined by the \c ValueType of the length map. |
---|
25 | /// |
---|
26 | ///It is also possible to change the underlying priority heap. |
---|
27 | /// |
---|
28 | ///\param Graph The graph type the algorithm runs on. |
---|
29 | ///\param LengthMap This read-only |
---|
30 | ///EdgeMap |
---|
31 | ///determines the |
---|
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". |
---|
39 | /// |
---|
40 | ///\author Jacint Szabo |
---|
41 | #ifdef DOXYGEN |
---|
42 | template <typename Graph, |
---|
43 | typename LengthMap, |
---|
44 | typename Heap> |
---|
45 | #else |
---|
46 | template <typename Graph, |
---|
47 | typename LengthMap=typename Graph::template EdgeMap<int>, |
---|
48 | template <class,class,class,class> class Heap = BinHeap > |
---|
49 | #endif |
---|
50 | class Dijkstra{ |
---|
51 | public: |
---|
52 | typedef typename Graph::Node Node; |
---|
53 | typedef typename Graph::NodeIt NodeIt; |
---|
54 | typedef typename Graph::Edge Edge; |
---|
55 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
56 | |
---|
57 | typedef typename LengthMap::ValueType ValueType; |
---|
58 | typedef typename Graph::template NodeMap<Edge> PredMap; |
---|
59 | typedef typename Graph::template NodeMap<Node> PredNodeMap; |
---|
60 | typedef typename Graph::template NodeMap<ValueType> DistMap; |
---|
61 | |
---|
62 | private: |
---|
63 | const Graph& G; |
---|
64 | const LengthMap& length; |
---|
65 | PredMap predecessor; |
---|
66 | PredNodeMap pred_node; |
---|
67 | DistMap distance; |
---|
68 | |
---|
69 | public : |
---|
70 | |
---|
71 | Dijkstra(const Graph& _G, const LengthMap& _length) : |
---|
72 | G(_G), length(_length), predecessor(_G), pred_node(_G), distance(_G) { } |
---|
73 | |
---|
74 | void run(Node s); |
---|
75 | |
---|
76 | ///The distance of a node from the root. |
---|
77 | |
---|
78 | ///Returns the distance of a node from the root. |
---|
79 | ///\pre \ref run() must be called before using this function. |
---|
80 | ///\warning If node \c v in unreachable from the root the return value |
---|
81 | ///of this funcion is undefined. |
---|
82 | ValueType dist(Node v) const { return distance[v]; } |
---|
83 | |
---|
84 | ///Returns the previous edge of the shortest path tree. |
---|
85 | |
---|
86 | ///For a node \c v it returns the previous edge of the shortest path tree, |
---|
87 | ///i.e. it returns the last edge from a shortest path from the root to \c |
---|
88 | ///v. It is INVALID if \c v is unreachable from the root or if \c v=s. The |
---|
89 | ///shortest path tree used here is equal to the shortest path tree used in |
---|
90 | ///\ref predNode(Node v). \pre \ref run() must be called before using |
---|
91 | ///this function. |
---|
92 | Edge pred(Node v) const { return predecessor[v]; } |
---|
93 | |
---|
94 | ///Returns the previous node of the shortest path tree. |
---|
95 | |
---|
96 | ///For a node \c v it returns the previous node of the shortest path tree, |
---|
97 | ///i.e. it returns the last but one node from a shortest path from the |
---|
98 | ///root to \c /v. It is INVALID if \c v is unreachable from the root or if |
---|
99 | ///\c v=s. The shortest path tree used here is equal to the shortest path |
---|
100 | ///tree used in \ref pred(Node v). \pre \ref run() must be called before |
---|
101 | ///using this function. |
---|
102 | Node predNode(Node v) const { return pred_node[v]; } |
---|
103 | |
---|
104 | ///Returns a reference to the NodeMap of distances. |
---|
105 | |
---|
106 | ///Returns a reference to the NodeMap of distances. \pre \ref run() must |
---|
107 | ///be called before using this function. |
---|
108 | const DistMap &distMap() const { return distance;} |
---|
109 | |
---|
110 | ///Returns a reference to the shortest path tree map. |
---|
111 | |
---|
112 | ///Returns a reference to the NodeMap of the edges of the |
---|
113 | ///shortest path tree. |
---|
114 | ///\pre \ref run() must be called before using this function. |
---|
115 | const PredMap &predMap() const { return predecessor;} |
---|
116 | |
---|
117 | ///Returns a reference to the map of nodes of shortest paths. |
---|
118 | |
---|
119 | ///Returns a reference to the NodeMap of the last but one nodes of the |
---|
120 | ///shortest path tree. |
---|
121 | ///\pre \ref run() must be called before using this function. |
---|
122 | const PredNodeMap &predNodeMap() const { return pred_node;} |
---|
123 | |
---|
124 | ///Checks if a node is reachable from the root. |
---|
125 | |
---|
126 | ///Returns \c true if \c v is reachable from the root. |
---|
127 | ///\warning the root node is reported to be unreached! |
---|
128 | ///\todo Is this what we want? |
---|
129 | ///\pre \ref run() must be called before using this function. |
---|
130 | /// |
---|
131 | bool reached(Node v) { return G.valid(predecessor[v]); } |
---|
132 | |
---|
133 | }; |
---|
134 | |
---|
135 | |
---|
136 | // ********************************************************************** |
---|
137 | // IMPLEMENTATIONS |
---|
138 | // ********************************************************************** |
---|
139 | |
---|
140 | ///Runs %Dijkstra algorithm from node the root. |
---|
141 | |
---|
142 | ///This method runs the %Dijkstra algorithm from a root node \c s |
---|
143 | ///in order to |
---|
144 | ///compute the |
---|
145 | ///shortest path to each node. The algorithm computes |
---|
146 | ///- The shortest path tree. |
---|
147 | ///- The distance of each node from the root. |
---|
148 | template <typename Graph, typename LengthMap, |
---|
149 | template<class,class,class,class> class Heap > |
---|
150 | void Dijkstra<Graph,LengthMap,Heap>::run(Node s) { |
---|
151 | |
---|
152 | NodeIt u; |
---|
153 | for ( G.first(u) ; G.valid(u) ; G.next(u) ) { |
---|
154 | predecessor.set(u,INVALID); |
---|
155 | pred_node.set(u,INVALID); |
---|
156 | } |
---|
157 | |
---|
158 | typename Graph::template NodeMap<int> heap_map(G,-1); |
---|
159 | |
---|
160 | typedef Heap<Node, ValueType, typename Graph::template NodeMap<int>, |
---|
161 | std::less<ValueType> > |
---|
162 | HeapType; |
---|
163 | |
---|
164 | HeapType heap(heap_map); |
---|
165 | |
---|
166 | heap.push(s,0); |
---|
167 | |
---|
168 | while ( !heap.empty() ) { |
---|
169 | |
---|
170 | Node v=heap.top(); |
---|
171 | ValueType oldvalue=heap[v]; |
---|
172 | heap.pop(); |
---|
173 | distance.set(v, oldvalue); |
---|
174 | |
---|
175 | { //FIXME this bracket is for e to be local |
---|
176 | OutEdgeIt e; |
---|
177 | for(G.first(e, v); |
---|
178 | G.valid(e); G.next(e)) { |
---|
179 | Node w=G.bNode(e); |
---|
180 | |
---|
181 | switch(heap.state(w)) { |
---|
182 | case HeapType::PRE_HEAP: |
---|
183 | heap.push(w,oldvalue+length[e]); |
---|
184 | predecessor.set(w,e); |
---|
185 | pred_node.set(w,v); |
---|
186 | break; |
---|
187 | case HeapType::IN_HEAP: |
---|
188 | if ( oldvalue+length[e] < heap[w] ) { |
---|
189 | heap.decrease(w, oldvalue+length[e]); |
---|
190 | predecessor.set(w,e); |
---|
191 | pred_node.set(w,v); |
---|
192 | } |
---|
193 | break; |
---|
194 | case HeapType::POST_HEAP: |
---|
195 | break; |
---|
196 | } |
---|
197 | } |
---|
198 | } //FIXME tis bracket |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | /// @} |
---|
203 | |
---|
204 | } //END OF NAMESPACE HUGO |
---|
205 | |
---|
206 | #endif |
---|
207 | |
---|
208 | |
---|