1 // -*- C++ -*- |
1 // -*- C++ -*- |
2 |
|
3 /* |
|
4 *template <Graph, T, Heap=FibHeap, LengthMap=Graph::EdgeMap<T> > |
|
5 * |
|
6 *Constructor: |
|
7 * |
|
8 *Dijkstra(Graph G, LengthMap length) |
|
9 * |
|
10 * |
|
11 *Methods: |
|
12 * |
|
13 *void run(Node s) |
|
14 * |
|
15 *T dist(Node v) : After run(s) was run, it returns the distance from s to v. |
|
16 * Returns T() if v is not reachable from s. |
|
17 * |
|
18 *Edge pred(Node v) : After run(s) was run, it returns the last |
|
19 * edge of a shortest s-v path. It is INVALID for s and for |
|
20 * the nodes not reachable from s. |
|
21 * |
|
22 *bool reached(Node v) : After run(s) was run, it is true iff v is |
|
23 * reachable from s |
|
24 * |
|
25 */ |
|
26 |
2 |
27 #ifndef HUGO_DIJKSTRA_H |
3 #ifndef HUGO_DIJKSTRA_H |
28 #define HUGO_DIJKSTRA_H |
4 #define HUGO_DIJKSTRA_H |
29 |
5 |
30 ///\file |
6 ///\file |
47 /// |
21 /// |
48 ///The type of the length is determined by the \c ValueType of the length map. |
22 ///The type of the length is determined by the \c ValueType of the length map. |
49 /// |
23 /// |
50 ///It is also possible to change the underlying priority heap. |
24 ///It is also possible to change the underlying priority heap. |
51 /// |
25 /// |
52 ///\param Graph The graph type the algorithm runs on. |
26 ///\param Graph The graph type the algorithm runs on. |
53 ///\param LengthMap This read-only |
27 ///\param LengthMap This read-only EdgeMap determines the lengths of |
54 ///EdgeMap |
28 ///the edges. It is read once for each edge, so the map may involve |
55 ///determines the |
29 ///in relatively time consuming process to compute the edge length |
56 ///lengths of the edges. It is read once for each edge, so the map |
30 ///if it is necessary. The default map type is \ref |
57 ///may involve in relatively time consuming process to compute the edge |
31 ///GraphSkeleton::EdgeMap "Graph::EdgeMap<int>" |
58 ///length if it is necessary. The default map type is |
32 ///\param Heap The heap type used by the %Dijkstra algorithm. The |
59 ///\ref GraphSkeleton::EdgeMap "Graph::EdgeMap<int>" |
33 ///default is using \ref BinHeap "binary heap". |
60 ///\param Heap The heap type used by the %Dijkstra |
|
61 ///algorithm. The default |
|
62 ///is using \ref BinHeap "binary heap". |
|
63 |
34 |
64 #ifdef DOXYGEN |
35 #ifdef DOXYGEN |
65 template <typename Graph, |
36 template <typename Graph, |
66 typename LengthMap, |
37 typename LengthMap, |
67 typename Heap> |
38 typename Heap> |
101 ///Returns the distance of a node from the source. |
72 ///Returns the distance of a node from the source. |
102 ///\pre \ref run() must be called before using this function. |
73 ///\pre \ref run() must be called before using this function. |
103 ///\warning If node \c v in unreachable from the source the return value |
74 ///\warning If node \c v in unreachable from the source the return value |
104 ///of this funcion is undefined. |
75 ///of this funcion is undefined. |
105 ValueType dist(Node v) const { return distance[v]; } |
76 ValueType dist(Node v) const { return distance[v]; } |
|
77 |
106 ///Returns the edges of the shortest path tree. |
78 ///Returns the edges of the shortest path tree. |
107 |
79 |
108 ///For a node \c v it returns the last edge of the shortest path |
80 ///For a node \c v it returns the last edge of the shortest path |
109 ///from the source to \c v or INVALID if \c v is unreachable |
81 ///from the source to \c v or INVALID if \c v is unreachable |
110 ///from the source. |
82 ///from the source. |
111 ///\pre \ref run() must be called before using this function. |
83 ///\pre \ref run() must be called before using this function. |
112 Edge pred(Node v) const { return predecessor[v]; } |
84 Edge pred(Node v) const { return predecessor[v]; } |
|
85 |
113 ///Returns the nodes of the shortest paths. |
86 ///Returns the nodes of the shortest paths. |
114 |
87 |
115 ///For a node \c v it returns the last but one node of the shortest path |
88 ///For a node \c v it returns the last but one node of the shortest path |
116 ///from the source to \c v or INVALID if \c v is unreachable |
89 ///from the source to \c v or INVALID if \c v is unreachable |
117 ///from the source. |
90 ///from the source. |
121 ///Returns a reference to the NodeMap of distances. |
94 ///Returns a reference to the NodeMap of distances. |
122 |
95 |
123 ///\pre \ref run() must be called before using this function. |
96 ///\pre \ref run() must be called before using this function. |
124 /// |
97 /// |
125 const DistMap &distMap() const { return distance;} |
98 const DistMap &distMap() const { return distance;} |
|
99 |
126 ///Returns a reference to the shortest path tree map. |
100 ///Returns a reference to the shortest path tree map. |
127 |
101 |
128 ///Returns a reference to the NodeMap of the edges of the |
102 ///Returns a reference to the NodeMap of the edges of the |
129 ///shortest path tree. |
103 ///shortest path tree. |
130 ///\pre \ref run() must be called before using this function. |
104 ///\pre \ref run() must be called before using this function. |
131 const PredMap &predMap() const { return predecessor;} |
105 const PredMap &predMap() const { return predecessor;} |
|
106 |
132 ///Returns a reference to the map of nodes of shortest paths. |
107 ///Returns a reference to the map of nodes of shortest paths. |
133 |
108 |
134 ///Returns a reference to the NodeMap of the last but one nodes of the |
109 ///Returns a reference to the NodeMap of the last but one nodes of the |
135 ///shortest paths. |
110 ///shortest paths. |
136 ///\pre \ref run() must be called before using this function. |
111 ///\pre \ref run() must be called before using this function. |
140 |
115 |
141 ///Returns \c true if \c v is reachable from the source. |
116 ///Returns \c true if \c v is reachable from the source. |
142 ///\warning the source node is reported to be unreached! |
117 ///\warning the source node is reported to be unreached! |
143 ///\todo Is this what we want? |
118 ///\todo Is this what we want? |
144 ///\pre \ref run() must be called before using this function. |
119 ///\pre \ref run() must be called before using this function. |
145 /// |
|
146 bool reached(Node v) { return G.valid(predecessor[v]); } |
120 bool reached(Node v) { return G.valid(predecessor[v]); } |
147 |
121 |
148 }; |
122 }; |
149 |
123 |
150 |
124 |
151 // ********************************************************************** |
125 // ********************************************************************** |
152 // IMPLEMENTATIONS |
126 // IMPLEMENTATIONS |
153 // ********************************************************************** |
127 // ********************************************************************** |
154 |
128 |
155 ///Runs %Dijkstra algorithm from node the source. |
129 ///Runs %Dijkstra algorithm from source node \c s. |
156 |
130 |
157 ///This method runs the %Dijkstra algorithm from a source node \c s |
131 ///This method runs the %Dijkstra algorithm from a source node \c s |
158 ///in order to |
132 ///in order to compute the shortest path to each node. The algorithm |
159 ///compute the |
133 ///computes - The shortest path tree. - The distance of each node |
160 ///shortest path to each node. The algorithm computes |
134 ///from the source. |
161 ///- The shortest path tree. |
|
162 ///- The distance of each node from the source. |
|
163 template <typename Graph, typename LengthMap, |
135 template <typename Graph, typename LengthMap, |
164 template<class,class,class> class Heap > |
136 template<class,class,class> class Heap > |
165 void Dijkstra<Graph,LengthMap,Heap>::run(Node s) { |
137 void Dijkstra<Graph,LengthMap,Heap>::run(Node s) { |
166 |
138 |
167 NodeIt u; |
139 NodeIt u; |
168 for ( G.first(u) ; G.valid(u) ; G.next(u) ) { |
140 for ( G.first(u) ; G.valid(u) ; G.next(u) ) { |
169 predecessor.set(u,INVALID); |
141 predecessor.set(u,INVALID); |
170 pred_node.set(u,INVALID); |
142 pred_node.set(u,INVALID); |
171 // If a node is unreacheable, then why should be the dist=0? |
|
172 // distance.set(u,0); |
|
173 // reach.set(u,false); |
|
174 } |
143 } |
175 |
144 |
176 typename Graph::NodeMap<int> heap_map(G,-1); |
145 typename Graph::NodeMap<int> heap_map(G,-1); |
177 |
146 |
178 Heap<Node,ValueType,typename Graph::NodeMap<int> > heap(heap_map); |
147 Heap<Node,ValueType,typename Graph::NodeMap<int> > heap(heap_map); |
179 |
|
180 heap.push(s,0); |
148 heap.push(s,0); |
181 |
149 |
182 while ( !heap.empty() ) { |
150 while ( !heap.empty() ) { |
|
151 |
|
152 Node v=heap.top(); |
|
153 ValueType oldvalue=heap[v]; |
|
154 heap.pop(); |
|
155 distance.set(v, oldvalue); |
183 |
156 |
184 Node v=heap.top(); |
157 { //FIXME this bracket is for e to be local |
185 ValueType oldvalue=heap[v]; |
158 OutEdgeIt e; |
186 heap.pop(); |
159 for(G.first(e, v); G.valid(e); G.next(e)) { |
187 distance.set(v, oldvalue); |
|
188 |
|
189 { //FIXME this bracket is for e to be local |
|
190 OutEdgeIt e; |
|
191 for(G.first(e, v); |
|
192 G.valid(e); G.next(e)) { |
|
193 Node w=G.head(e); |
160 Node w=G.head(e); |
194 |
161 |
195 switch(heap.state(w)) { |
162 switch(heap.state(w)) { |
196 case heap.PRE_HEAP: |
163 case heap.PRE_HEAP: |
197 heap.push(w,oldvalue+length[e]); |
164 heap.push(w,oldvalue+length[e]); |