alpar@906
|
1 |
/* -*- C++ -*-
|
alpar@921
|
2 |
* src/lemon/dijkstra.h - Part of LEMON, a generic C++ optimization library
|
alpar@906
|
3 |
*
|
alpar@906
|
4 |
* Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@906
|
5 |
* (Egervary Combinatorial Optimization Research Group, EGRES).
|
alpar@906
|
6 |
*
|
alpar@906
|
7 |
* Permission to use, modify and distribute this software is granted
|
alpar@906
|
8 |
* provided that this copyright notice appears in all copies. For
|
alpar@906
|
9 |
* precise terms see the accompanying LICENSE file.
|
alpar@906
|
10 |
*
|
alpar@906
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@906
|
12 |
* express or implied, and with no claim as to its suitability for any
|
alpar@906
|
13 |
* purpose.
|
alpar@906
|
14 |
*
|
alpar@906
|
15 |
*/
|
alpar@906
|
16 |
|
alpar@921
|
17 |
#ifndef LEMON_DIJKSTRA_H
|
alpar@921
|
18 |
#define LEMON_DIJKSTRA_H
|
alpar@255
|
19 |
|
alpar@758
|
20 |
///\ingroup flowalgs
|
alpar@255
|
21 |
///\file
|
alpar@255
|
22 |
///\brief Dijkstra algorithm.
|
alpar@255
|
23 |
|
alpar@921
|
24 |
#include <lemon/bin_heap.h>
|
alpar@921
|
25 |
#include <lemon/invalid.h>
|
alpar@255
|
26 |
|
alpar@921
|
27 |
namespace lemon {
|
jacint@385
|
28 |
|
alpar@758
|
29 |
/// \addtogroup flowalgs
|
alpar@430
|
30 |
/// @{
|
alpar@430
|
31 |
|
alpar@255
|
32 |
///%Dijkstra algorithm class.
|
alpar@255
|
33 |
|
alpar@255
|
34 |
///This class provides an efficient implementation of %Dijkstra algorithm.
|
alpar@255
|
35 |
///The edge lengths are passed to the algorithm using a
|
klao@959
|
36 |
///\ref concept::ReadMap "ReadMap",
|
alpar@255
|
37 |
///so it is easy to change it to any kind of length.
|
alpar@255
|
38 |
///
|
alpar@880
|
39 |
///The type of the length is determined by the
|
klao@959
|
40 |
///\ref concept::ReadMap::ValueType "ValueType" of the length map.
|
alpar@255
|
41 |
///
|
alpar@255
|
42 |
///It is also possible to change the underlying priority heap.
|
alpar@255
|
43 |
///
|
alpar@584
|
44 |
///\param GR The graph type the algorithm runs on.
|
alpar@584
|
45 |
///\param LM This read-only
|
jacint@385
|
46 |
///EdgeMap
|
jacint@385
|
47 |
///determines the
|
jacint@385
|
48 |
///lengths of the edges. It is read once for each edge, so the map
|
jacint@385
|
49 |
///may involve in relatively time consuming process to compute the edge
|
jacint@385
|
50 |
///length if it is necessary. The default map type is
|
klao@959
|
51 |
///\ref concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>"
|
jacint@385
|
52 |
///\param Heap The heap type used by the %Dijkstra
|
jacint@385
|
53 |
///algorithm. The default
|
jacint@385
|
54 |
///is using \ref BinHeap "binary heap".
|
alpar@456
|
55 |
///
|
alpar@689
|
56 |
///\author Jacint Szabo and Alpar Juttner
|
alpar@693
|
57 |
///\todo We need a typedef-names should be standardized. (-:
|
alpar@734
|
58 |
///\todo Type of \c PredMap, \c PredNodeMap and \c DistMap
|
alpar@734
|
59 |
///should not be fixed. (Problematic to solve).
|
alpar@584
|
60 |
|
alpar@255
|
61 |
#ifdef DOXYGEN
|
alpar@584
|
62 |
template <typename GR,
|
alpar@584
|
63 |
typename LM,
|
alpar@255
|
64 |
typename Heap>
|
alpar@255
|
65 |
#else
|
alpar@584
|
66 |
template <typename GR,
|
alpar@584
|
67 |
typename LM=typename GR::template EdgeMap<int>,
|
alpar@532
|
68 |
template <class,class,class,class> class Heap = BinHeap >
|
alpar@255
|
69 |
#endif
|
alpar@255
|
70 |
class Dijkstra{
|
alpar@255
|
71 |
public:
|
alpar@584
|
72 |
///The type of the underlying graph.
|
alpar@584
|
73 |
typedef GR Graph;
|
alpar@911
|
74 |
///\e
|
alpar@255
|
75 |
typedef typename Graph::Node Node;
|
alpar@911
|
76 |
///\e
|
alpar@255
|
77 |
typedef typename Graph::NodeIt NodeIt;
|
alpar@911
|
78 |
///\e
|
alpar@255
|
79 |
typedef typename Graph::Edge Edge;
|
alpar@911
|
80 |
///\e
|
alpar@255
|
81 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
alpar@255
|
82 |
|
alpar@584
|
83 |
///The type of the length of the edges.
|
alpar@584
|
84 |
typedef typename LM::ValueType ValueType;
|
alpar@693
|
85 |
///The type of the map that stores the edge lengths.
|
alpar@584
|
86 |
typedef LM LengthMap;
|
alpar@693
|
87 |
///\brief The type of the map that stores the last
|
alpar@584
|
88 |
///edges of the shortest paths.
|
marci@433
|
89 |
typedef typename Graph::template NodeMap<Edge> PredMap;
|
alpar@693
|
90 |
///\brief The type of the map that stores the last but one
|
alpar@584
|
91 |
///nodes of the shortest paths.
|
marci@433
|
92 |
typedef typename Graph::template NodeMap<Node> PredNodeMap;
|
alpar@693
|
93 |
///The type of the map that stores the dists of the nodes.
|
marci@433
|
94 |
typedef typename Graph::template NodeMap<ValueType> DistMap;
|
alpar@255
|
95 |
|
alpar@255
|
96 |
private:
|
alpar@802
|
97 |
/// Pointer to the underlying graph.
|
alpar@688
|
98 |
const Graph *G;
|
alpar@802
|
99 |
/// Pointer to the length map
|
alpar@688
|
100 |
const LM *length;
|
alpar@802
|
101 |
///Pointer to the map of predecessors edges.
|
alpar@688
|
102 |
PredMap *predecessor;
|
alpar@802
|
103 |
///Indicates if \ref predecessor is locally allocated (\c true) or not.
|
alpar@688
|
104 |
bool local_predecessor;
|
alpar@802
|
105 |
///Pointer to the map of predecessors nodes.
|
alpar@688
|
106 |
PredNodeMap *pred_node;
|
alpar@802
|
107 |
///Indicates if \ref pred_node is locally allocated (\c true) or not.
|
alpar@688
|
108 |
bool local_pred_node;
|
alpar@802
|
109 |
///Pointer to the map of distances.
|
alpar@688
|
110 |
DistMap *distance;
|
alpar@802
|
111 |
///Indicates if \ref distance is locally allocated (\c true) or not.
|
alpar@688
|
112 |
bool local_distance;
|
alpar@688
|
113 |
|
alpar@802
|
114 |
///The source node of the last execution.
|
alpar@774
|
115 |
Node source;
|
alpar@774
|
116 |
|
alpar@785
|
117 |
///Initializes the maps.
|
alpar@688
|
118 |
|
alpar@694
|
119 |
///\todo Error if \c G or are \c NULL. What about \c length?
|
alpar@688
|
120 |
///\todo Better memory allocation (instead of new).
|
alpar@688
|
121 |
void init_maps()
|
alpar@688
|
122 |
{
|
alpar@688
|
123 |
if(!predecessor) {
|
alpar@688
|
124 |
local_predecessor = true;
|
alpar@688
|
125 |
predecessor = new PredMap(*G);
|
alpar@688
|
126 |
}
|
alpar@688
|
127 |
if(!pred_node) {
|
alpar@688
|
128 |
local_pred_node = true;
|
alpar@688
|
129 |
pred_node = new PredNodeMap(*G);
|
alpar@688
|
130 |
}
|
alpar@688
|
131 |
if(!distance) {
|
alpar@688
|
132 |
local_distance = true;
|
alpar@688
|
133 |
distance = new DistMap(*G);
|
alpar@688
|
134 |
}
|
alpar@688
|
135 |
}
|
alpar@255
|
136 |
|
alpar@255
|
137 |
public :
|
alpar@802
|
138 |
///Constructor.
|
alpar@255
|
139 |
|
alpar@802
|
140 |
///\param _G the graph the algorithm will run on.
|
alpar@802
|
141 |
///\param _length the length map used by the algorithm.
|
alpar@584
|
142 |
Dijkstra(const Graph& _G, const LM& _length) :
|
alpar@688
|
143 |
G(&_G), length(&_length),
|
alpar@707
|
144 |
predecessor(NULL), local_predecessor(false),
|
alpar@707
|
145 |
pred_node(NULL), local_pred_node(false),
|
alpar@707
|
146 |
distance(NULL), local_distance(false)
|
alpar@688
|
147 |
{ }
|
alpar@688
|
148 |
|
alpar@802
|
149 |
///Destructor.
|
alpar@688
|
150 |
~Dijkstra()
|
alpar@688
|
151 |
{
|
alpar@688
|
152 |
if(local_predecessor) delete predecessor;
|
alpar@688
|
153 |
if(local_pred_node) delete pred_node;
|
alpar@688
|
154 |
if(local_distance) delete distance;
|
alpar@688
|
155 |
}
|
alpar@688
|
156 |
|
alpar@688
|
157 |
///Sets the length map.
|
alpar@688
|
158 |
|
alpar@688
|
159 |
///Sets the length map.
|
alpar@688
|
160 |
///\return <tt> (*this) </tt>
|
alpar@688
|
161 |
Dijkstra &setLengthMap(const LM &m)
|
alpar@688
|
162 |
{
|
alpar@688
|
163 |
length = &m;
|
alpar@688
|
164 |
return *this;
|
alpar@688
|
165 |
}
|
alpar@688
|
166 |
|
alpar@688
|
167 |
///Sets the map storing the predecessor edges.
|
alpar@688
|
168 |
|
alpar@688
|
169 |
///Sets the map storing the predecessor edges.
|
alpar@688
|
170 |
///If you don't use this function before calling \ref run(),
|
alpar@688
|
171 |
///it will allocate one. The destuctor deallocates this
|
alpar@688
|
172 |
///automatically allocated map, of course.
|
alpar@688
|
173 |
///\return <tt> (*this) </tt>
|
alpar@688
|
174 |
Dijkstra &setPredMap(PredMap &m)
|
alpar@688
|
175 |
{
|
alpar@688
|
176 |
if(local_predecessor) {
|
alpar@688
|
177 |
delete predecessor;
|
alpar@688
|
178 |
local_predecessor=false;
|
alpar@688
|
179 |
}
|
alpar@688
|
180 |
predecessor = &m;
|
alpar@688
|
181 |
return *this;
|
alpar@688
|
182 |
}
|
alpar@688
|
183 |
|
alpar@688
|
184 |
///Sets the map storing the predecessor nodes.
|
alpar@688
|
185 |
|
alpar@688
|
186 |
///Sets the map storing the predecessor nodes.
|
alpar@688
|
187 |
///If you don't use this function before calling \ref run(),
|
alpar@688
|
188 |
///it will allocate one. The destuctor deallocates this
|
alpar@688
|
189 |
///automatically allocated map, of course.
|
alpar@688
|
190 |
///\return <tt> (*this) </tt>
|
alpar@688
|
191 |
Dijkstra &setPredNodeMap(PredNodeMap &m)
|
alpar@688
|
192 |
{
|
alpar@688
|
193 |
if(local_pred_node) {
|
alpar@688
|
194 |
delete pred_node;
|
alpar@688
|
195 |
local_pred_node=false;
|
alpar@688
|
196 |
}
|
alpar@688
|
197 |
pred_node = &m;
|
alpar@688
|
198 |
return *this;
|
alpar@688
|
199 |
}
|
alpar@688
|
200 |
|
alpar@688
|
201 |
///Sets the map storing the distances calculated by the algorithm.
|
alpar@688
|
202 |
|
alpar@688
|
203 |
///Sets the map storing the distances calculated by the algorithm.
|
alpar@688
|
204 |
///If you don't use this function before calling \ref run(),
|
alpar@688
|
205 |
///it will allocate one. The destuctor deallocates this
|
alpar@688
|
206 |
///automatically allocated map, of course.
|
alpar@688
|
207 |
///\return <tt> (*this) </tt>
|
alpar@688
|
208 |
Dijkstra &setDistMap(DistMap &m)
|
alpar@688
|
209 |
{
|
alpar@688
|
210 |
if(local_distance) {
|
alpar@688
|
211 |
delete distance;
|
alpar@688
|
212 |
local_distance=false;
|
alpar@688
|
213 |
}
|
alpar@688
|
214 |
distance = &m;
|
alpar@688
|
215 |
return *this;
|
alpar@688
|
216 |
}
|
alpar@255
|
217 |
|
alpar@694
|
218 |
///Runs %Dijkstra algorithm from node \c s.
|
alpar@694
|
219 |
|
alpar@694
|
220 |
///This method runs the %Dijkstra algorithm from a root node \c s
|
alpar@694
|
221 |
///in order to
|
alpar@694
|
222 |
///compute the
|
alpar@694
|
223 |
///shortest path to each node. The algorithm computes
|
alpar@694
|
224 |
///- The shortest path tree.
|
alpar@694
|
225 |
///- The distance of each node from the root.
|
alpar@694
|
226 |
|
alpar@694
|
227 |
void run(Node s) {
|
alpar@694
|
228 |
|
alpar@694
|
229 |
init_maps();
|
alpar@694
|
230 |
|
alpar@774
|
231 |
source = s;
|
alpar@774
|
232 |
|
alpar@774
|
233 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
|
alpar@694
|
234 |
predecessor->set(u,INVALID);
|
alpar@694
|
235 |
pred_node->set(u,INVALID);
|
alpar@694
|
236 |
}
|
alpar@694
|
237 |
|
alpar@694
|
238 |
typename GR::template NodeMap<int> heap_map(*G,-1);
|
alpar@694
|
239 |
|
alpar@694
|
240 |
typedef Heap<Node, ValueType, typename GR::template NodeMap<int>,
|
alpar@694
|
241 |
std::less<ValueType> >
|
alpar@694
|
242 |
HeapType;
|
alpar@694
|
243 |
|
alpar@694
|
244 |
HeapType heap(heap_map);
|
alpar@694
|
245 |
|
alpar@694
|
246 |
heap.push(s,0);
|
alpar@694
|
247 |
|
alpar@694
|
248 |
while ( !heap.empty() ) {
|
alpar@694
|
249 |
|
alpar@694
|
250 |
Node v=heap.top();
|
alpar@694
|
251 |
ValueType oldvalue=heap[v];
|
alpar@694
|
252 |
heap.pop();
|
alpar@694
|
253 |
distance->set(v, oldvalue);
|
alpar@694
|
254 |
|
alpar@694
|
255 |
|
alpar@774
|
256 |
for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
|
alpar@986
|
257 |
Node w=G->target(e);
|
alpar@694
|
258 |
switch(heap.state(w)) {
|
alpar@694
|
259 |
case HeapType::PRE_HEAP:
|
alpar@694
|
260 |
heap.push(w,oldvalue+(*length)[e]);
|
alpar@694
|
261 |
predecessor->set(w,e);
|
alpar@694
|
262 |
pred_node->set(w,v);
|
alpar@694
|
263 |
break;
|
alpar@694
|
264 |
case HeapType::IN_HEAP:
|
alpar@694
|
265 |
if ( oldvalue+(*length)[e] < heap[w] ) {
|
alpar@694
|
266 |
heap.decrease(w, oldvalue+(*length)[e]);
|
alpar@694
|
267 |
predecessor->set(w,e);
|
alpar@694
|
268 |
pred_node->set(w,v);
|
alpar@694
|
269 |
}
|
alpar@694
|
270 |
break;
|
alpar@694
|
271 |
case HeapType::POST_HEAP:
|
alpar@694
|
272 |
break;
|
alpar@694
|
273 |
}
|
alpar@694
|
274 |
}
|
alpar@694
|
275 |
}
|
alpar@694
|
276 |
}
|
alpar@255
|
277 |
|
jacint@385
|
278 |
///The distance of a node from the root.
|
alpar@255
|
279 |
|
jacint@385
|
280 |
///Returns the distance of a node from the root.
|
alpar@255
|
281 |
///\pre \ref run() must be called before using this function.
|
jacint@385
|
282 |
///\warning If node \c v in unreachable from the root the return value
|
alpar@255
|
283 |
///of this funcion is undefined.
|
alpar@688
|
284 |
ValueType dist(Node v) const { return (*distance)[v]; }
|
jacint@373
|
285 |
|
alpar@584
|
286 |
///Returns the 'previous edge' of the shortest path tree.
|
alpar@255
|
287 |
|
alpar@584
|
288 |
///For a node \c v it returns the 'previous edge' of the shortest path tree,
|
alpar@785
|
289 |
///i.e. it returns the last edge of a shortest path from the root to \c
|
alpar@688
|
290 |
///v. It is \ref INVALID
|
alpar@688
|
291 |
///if \c v is unreachable from the root or if \c v=s. The
|
jacint@385
|
292 |
///shortest path tree used here is equal to the shortest path tree used in
|
jacint@385
|
293 |
///\ref predNode(Node v). \pre \ref run() must be called before using
|
jacint@385
|
294 |
///this function.
|
alpar@780
|
295 |
///\todo predEdge could be a better name.
|
alpar@688
|
296 |
Edge pred(Node v) const { return (*predecessor)[v]; }
|
jacint@373
|
297 |
|
alpar@584
|
298 |
///Returns the 'previous node' of the shortest path tree.
|
alpar@255
|
299 |
|
alpar@584
|
300 |
///For a node \c v it returns the 'previous node' of the shortest path tree,
|
jacint@385
|
301 |
///i.e. it returns the last but one node from a shortest path from the
|
jacint@385
|
302 |
///root to \c /v. It is INVALID if \c v is unreachable from the root or if
|
jacint@385
|
303 |
///\c v=s. The shortest path tree used here is equal to the shortest path
|
jacint@385
|
304 |
///tree used in \ref pred(Node v). \pre \ref run() must be called before
|
jacint@385
|
305 |
///using this function.
|
alpar@688
|
306 |
Node predNode(Node v) const { return (*pred_node)[v]; }
|
alpar@255
|
307 |
|
alpar@255
|
308 |
///Returns a reference to the NodeMap of distances.
|
alpar@255
|
309 |
|
jacint@385
|
310 |
///Returns a reference to the NodeMap of distances. \pre \ref run() must
|
jacint@385
|
311 |
///be called before using this function.
|
alpar@688
|
312 |
const DistMap &distMap() const { return *distance;}
|
jacint@385
|
313 |
|
alpar@255
|
314 |
///Returns a reference to the shortest path tree map.
|
alpar@255
|
315 |
|
alpar@255
|
316 |
///Returns a reference to the NodeMap of the edges of the
|
alpar@255
|
317 |
///shortest path tree.
|
alpar@255
|
318 |
///\pre \ref run() must be called before using this function.
|
alpar@688
|
319 |
const PredMap &predMap() const { return *predecessor;}
|
jacint@385
|
320 |
|
jacint@385
|
321 |
///Returns a reference to the map of nodes of shortest paths.
|
alpar@255
|
322 |
|
alpar@255
|
323 |
///Returns a reference to the NodeMap of the last but one nodes of the
|
jacint@385
|
324 |
///shortest path tree.
|
alpar@255
|
325 |
///\pre \ref run() must be called before using this function.
|
alpar@688
|
326 |
const PredNodeMap &predNodeMap() const { return *pred_node;}
|
alpar@255
|
327 |
|
jacint@385
|
328 |
///Checks if a node is reachable from the root.
|
alpar@255
|
329 |
|
jacint@385
|
330 |
///Returns \c true if \c v is reachable from the root.
|
alpar@802
|
331 |
///\note The root node is reported to be reached!
|
alpar@255
|
332 |
///\pre \ref run() must be called before using this function.
|
jacint@385
|
333 |
///
|
alpar@780
|
334 |
bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
|
alpar@255
|
335 |
|
alpar@255
|
336 |
};
|
alpar@255
|
337 |
|
alpar@430
|
338 |
/// @}
|
alpar@255
|
339 |
|
alpar@921
|
340 |
} //END OF NAMESPACE LEMON
|
alpar@255
|
341 |
|
alpar@255
|
342 |
#endif
|
alpar@255
|
343 |
|
alpar@255
|
344 |
|