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@953
|
24 |
#include <lemon/list_graph.h>
|
alpar@921
|
25 |
#include <lemon/bin_heap.h>
|
alpar@921
|
26 |
#include <lemon/invalid.h>
|
alpar@255
|
27 |
|
alpar@921
|
28 |
namespace lemon {
|
jacint@385
|
29 |
|
alpar@758
|
30 |
/// \addtogroup flowalgs
|
alpar@430
|
31 |
/// @{
|
alpar@430
|
32 |
|
alpar@954
|
33 |
///Default traits class of Dijkstra class.
|
alpar@954
|
34 |
|
alpar@954
|
35 |
///Default traits class of Dijkstra class.
|
alpar@954
|
36 |
///\param GR Graph type.
|
alpar@954
|
37 |
///\param LM Type of length map.
|
alpar@953
|
38 |
template<class GR, class LM>
|
alpar@953
|
39 |
struct DijkstraDefaultTraits
|
alpar@953
|
40 |
{
|
alpar@954
|
41 |
///The graph type the algorithm runs on.
|
alpar@953
|
42 |
typedef GR Graph;
|
alpar@953
|
43 |
///The type of the map that stores the edge lengths.
|
alpar@953
|
44 |
|
alpar@967
|
45 |
///It must meet the \ref concept::ReadMap "ReadMap" concept.
|
alpar@953
|
46 |
///
|
alpar@953
|
47 |
typedef LM LengthMap;
|
alpar@954
|
48 |
//The type of the length of the edges.
|
alpar@987
|
49 |
typedef typename LM::Value Value;
|
alpar@954
|
50 |
///The heap type used by Dijkstra algorithm.
|
alpar@967
|
51 |
|
alpar@967
|
52 |
///The heap type used by Dijkstra algorithm.
|
alpar@967
|
53 |
///
|
alpar@967
|
54 |
///\sa BinHeap
|
alpar@967
|
55 |
///\sa Dijkstra
|
alpar@953
|
56 |
typedef BinHeap<typename Graph::Node,
|
alpar@987
|
57 |
typename LM::Value,
|
alpar@953
|
58 |
typename GR::template NodeMap<int>,
|
alpar@987
|
59 |
std::less<Value> > Heap;
|
alpar@953
|
60 |
|
alpar@953
|
61 |
///\brief The type of the map that stores the last
|
alpar@953
|
62 |
///edges of the shortest paths.
|
alpar@953
|
63 |
///
|
alpar@967
|
64 |
///It must meet the \ref concept::WriteMap "WriteMap" concept.
|
alpar@953
|
65 |
///
|
alpar@954
|
66 |
typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
|
alpar@954
|
67 |
///Instantiates a PredMap.
|
alpar@953
|
68 |
|
alpar@953
|
69 |
///\todo Please document...
|
alpar@953
|
70 |
///
|
alpar@954
|
71 |
static PredMap *createPredMap(const GR &G)
|
alpar@953
|
72 |
{
|
alpar@953
|
73 |
return new PredMap(G);
|
alpar@953
|
74 |
}
|
alpar@953
|
75 |
///\brief The type of the map that stores the last but one
|
alpar@953
|
76 |
///nodes of the shortest paths.
|
alpar@953
|
77 |
///
|
alpar@967
|
78 |
///It must meet the \ref concept::WriteMap "WriteMap" concept.
|
alpar@953
|
79 |
///
|
alpar@954
|
80 |
typedef typename Graph::template NodeMap<typename GR::Node> PredNodeMap;
|
alpar@954
|
81 |
///Instantiates a PredNodeMap.
|
alpar@953
|
82 |
|
alpar@953
|
83 |
///\todo Please document...
|
alpar@967
|
84 |
///
|
alpar@954
|
85 |
static PredNodeMap *createPredNodeMap(const GR &G)
|
alpar@953
|
86 |
{
|
alpar@953
|
87 |
return new PredNodeMap(G);
|
alpar@953
|
88 |
}
|
alpar@953
|
89 |
///The type of the map that stores the dists of the nodes.
|
alpar@953
|
90 |
|
alpar@967
|
91 |
///It must meet the \ref concept::WriteMap "WriteMap" concept.
|
alpar@953
|
92 |
///
|
alpar@987
|
93 |
typedef typename Graph::template NodeMap<typename LM::Value> DistMap;
|
alpar@954
|
94 |
///Instantiates a DistMap.
|
alpar@953
|
95 |
|
alpar@953
|
96 |
///\todo Please document...
|
alpar@953
|
97 |
///
|
alpar@954
|
98 |
static DistMap *createDistMap(const GR &G)
|
alpar@953
|
99 |
{
|
alpar@953
|
100 |
return new DistMap(G);
|
alpar@953
|
101 |
}
|
alpar@953
|
102 |
};
|
alpar@953
|
103 |
|
alpar@255
|
104 |
///%Dijkstra algorithm class.
|
alpar@255
|
105 |
|
alpar@255
|
106 |
///This class provides an efficient implementation of %Dijkstra algorithm.
|
alpar@255
|
107 |
///The edge lengths are passed to the algorithm using a
|
klao@959
|
108 |
///\ref concept::ReadMap "ReadMap",
|
alpar@255
|
109 |
///so it is easy to change it to any kind of length.
|
alpar@255
|
110 |
///
|
alpar@880
|
111 |
///The type of the length is determined by the
|
alpar@987
|
112 |
///\ref concept::ReadMap::Value "Value" of the length map.
|
alpar@255
|
113 |
///
|
alpar@255
|
114 |
///It is also possible to change the underlying priority heap.
|
alpar@255
|
115 |
///
|
alpar@953
|
116 |
///\param GR The graph type the algorithm runs on. The default value is
|
alpar@955
|
117 |
///\ref ListGraph. The value of GR is not used directly by Dijkstra, it
|
alpar@954
|
118 |
///is only passed to \ref DijkstraDefaultTraits.
|
alpar@584
|
119 |
///\param LM This read-only
|
jacint@385
|
120 |
///EdgeMap
|
jacint@385
|
121 |
///determines the
|
jacint@385
|
122 |
///lengths of the edges. It is read once for each edge, so the map
|
jacint@385
|
123 |
///may involve in relatively time consuming process to compute the edge
|
jacint@385
|
124 |
///length if it is necessary. The default map type is
|
klao@959
|
125 |
///\ref concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>".
|
alpar@955
|
126 |
///The value of LM is not used directly by Dijkstra, it
|
alpar@954
|
127 |
///is only passed to \ref DijkstraDefaultTraits.
|
alpar@954
|
128 |
///\param TR Traits class to set various data types used by the algorithm.
|
alpar@954
|
129 |
///The default traits class is
|
alpar@955
|
130 |
///\ref DijkstraDefaultTraits "DijkstraDefaultTraits<GR,LM>".
|
alpar@954
|
131 |
///See \ref DijkstraDefaultTraits for the documentation of
|
alpar@954
|
132 |
///a Dijkstra traits class.
|
alpar@456
|
133 |
///
|
alpar@689
|
134 |
///\author Jacint Szabo and Alpar Juttner
|
alpar@693
|
135 |
///\todo We need a typedef-names should be standardized. (-:
|
alpar@584
|
136 |
|
alpar@255
|
137 |
#ifdef DOXYGEN
|
alpar@584
|
138 |
template <typename GR,
|
alpar@584
|
139 |
typename LM,
|
alpar@953
|
140 |
typename TR>
|
alpar@255
|
141 |
#else
|
alpar@953
|
142 |
template <typename GR=ListGraph,
|
alpar@584
|
143 |
typename LM=typename GR::template EdgeMap<int>,
|
alpar@953
|
144 |
typename TR=DijkstraDefaultTraits<GR,LM> >
|
alpar@255
|
145 |
#endif
|
alpar@255
|
146 |
class Dijkstra{
|
alpar@255
|
147 |
public:
|
alpar@953
|
148 |
typedef TR Traits;
|
alpar@584
|
149 |
///The type of the underlying graph.
|
alpar@954
|
150 |
typedef typename TR::Graph Graph;
|
alpar@911
|
151 |
///\e
|
alpar@255
|
152 |
typedef typename Graph::Node Node;
|
alpar@911
|
153 |
///\e
|
alpar@255
|
154 |
typedef typename Graph::NodeIt NodeIt;
|
alpar@911
|
155 |
///\e
|
alpar@255
|
156 |
typedef typename Graph::Edge Edge;
|
alpar@911
|
157 |
///\e
|
alpar@255
|
158 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
alpar@255
|
159 |
|
alpar@584
|
160 |
///The type of the length of the edges.
|
alpar@987
|
161 |
typedef typename TR::LengthMap::Value Value;
|
alpar@693
|
162 |
///The type of the map that stores the edge lengths.
|
alpar@954
|
163 |
typedef typename TR::LengthMap LengthMap;
|
alpar@693
|
164 |
///\brief The type of the map that stores the last
|
alpar@584
|
165 |
///edges of the shortest paths.
|
alpar@953
|
166 |
typedef typename TR::PredMap PredMap;
|
alpar@693
|
167 |
///\brief The type of the map that stores the last but one
|
alpar@584
|
168 |
///nodes of the shortest paths.
|
alpar@953
|
169 |
typedef typename TR::PredNodeMap PredNodeMap;
|
alpar@693
|
170 |
///The type of the map that stores the dists of the nodes.
|
alpar@953
|
171 |
typedef typename TR::DistMap DistMap;
|
alpar@953
|
172 |
///The heap type used by the dijkstra algorithm.
|
alpar@953
|
173 |
typedef typename TR::Heap Heap;
|
alpar@255
|
174 |
private:
|
alpar@802
|
175 |
/// Pointer to the underlying graph.
|
alpar@688
|
176 |
const Graph *G;
|
alpar@802
|
177 |
/// Pointer to the length map
|
alpar@954
|
178 |
const LengthMap *length;
|
alpar@802
|
179 |
///Pointer to the map of predecessors edges.
|
alpar@688
|
180 |
PredMap *predecessor;
|
alpar@802
|
181 |
///Indicates if \ref predecessor is locally allocated (\c true) or not.
|
alpar@688
|
182 |
bool local_predecessor;
|
alpar@802
|
183 |
///Pointer to the map of predecessors nodes.
|
alpar@688
|
184 |
PredNodeMap *pred_node;
|
alpar@802
|
185 |
///Indicates if \ref pred_node is locally allocated (\c true) or not.
|
alpar@688
|
186 |
bool local_pred_node;
|
alpar@802
|
187 |
///Pointer to the map of distances.
|
alpar@688
|
188 |
DistMap *distance;
|
alpar@802
|
189 |
///Indicates if \ref distance is locally allocated (\c true) or not.
|
alpar@688
|
190 |
bool local_distance;
|
alpar@688
|
191 |
|
alpar@802
|
192 |
///The source node of the last execution.
|
alpar@774
|
193 |
Node source;
|
alpar@774
|
194 |
|
alpar@785
|
195 |
///Initializes the maps.
|
alpar@688
|
196 |
|
alpar@694
|
197 |
///\todo Error if \c G or are \c NULL. What about \c length?
|
alpar@688
|
198 |
///\todo Better memory allocation (instead of new).
|
alpar@688
|
199 |
void init_maps()
|
alpar@688
|
200 |
{
|
alpar@688
|
201 |
if(!predecessor) {
|
alpar@688
|
202 |
local_predecessor = true;
|
alpar@953
|
203 |
predecessor = Traits::createPredMap(*G);
|
alpar@688
|
204 |
}
|
alpar@688
|
205 |
if(!pred_node) {
|
alpar@688
|
206 |
local_pred_node = true;
|
alpar@953
|
207 |
pred_node = Traits::createPredNodeMap(*G);
|
alpar@688
|
208 |
}
|
alpar@688
|
209 |
if(!distance) {
|
alpar@688
|
210 |
local_distance = true;
|
alpar@953
|
211 |
distance = Traits::createDistMap(*G);
|
alpar@688
|
212 |
}
|
alpar@688
|
213 |
}
|
alpar@255
|
214 |
|
alpar@255
|
215 |
public :
|
alpar@953
|
216 |
|
alpar@953
|
217 |
template <class T>
|
alpar@953
|
218 |
struct SetPredMapTraits : public Traits {
|
alpar@953
|
219 |
typedef T PredMap;
|
alpar@953
|
220 |
///\todo An exception should be thrown.
|
alpar@953
|
221 |
///
|
alpar@953
|
222 |
static PredMap *createPredMap(const Graph &G)
|
alpar@953
|
223 |
{
|
alpar@953
|
224 |
std::cerr << __FILE__ ":" << __LINE__ <<
|
alpar@953
|
225 |
": error: Special maps should be manually created" << std::endl;
|
alpar@953
|
226 |
exit(1);
|
alpar@953
|
227 |
}
|
alpar@953
|
228 |
};
|
alpar@954
|
229 |
///\ref named-templ-param "Named parameter" for setting PredMap type
|
alpar@954
|
230 |
|
alpar@967
|
231 |
///\relates Dijkstra
|
alpar@954
|
232 |
///\ingroup flowalgs
|
alpar@954
|
233 |
///\ref named-templ-param "Named parameter" for setting PredMap type
|
alpar@953
|
234 |
template <class T>
|
alpar@953
|
235 |
class SetPredMap : public Dijkstra< Graph,
|
alpar@953
|
236 |
LengthMap,
|
alpar@953
|
237 |
SetPredMapTraits<T> > { };
|
alpar@953
|
238 |
|
alpar@953
|
239 |
template <class T>
|
alpar@953
|
240 |
struct SetPredNodeMapTraits : public Traits {
|
alpar@953
|
241 |
typedef T PredNodeMap;
|
alpar@953
|
242 |
///\todo An exception should be thrown.
|
alpar@953
|
243 |
///
|
alpar@953
|
244 |
static PredNodeMap *createPredNodeMap(const Graph &G)
|
alpar@953
|
245 |
{
|
alpar@953
|
246 |
std::cerr << __FILE__ ":" << __LINE__ <<
|
alpar@953
|
247 |
": error: Special maps should be manually created" << std::endl;
|
alpar@953
|
248 |
exit(1);
|
alpar@953
|
249 |
}
|
alpar@953
|
250 |
};
|
alpar@954
|
251 |
///\ref named-templ-param "Named parameter" for setting PredNodeMap type
|
alpar@954
|
252 |
|
alpar@954
|
253 |
///\ingroup flowalgs
|
alpar@954
|
254 |
///\ref named-templ-param "Named parameter" for setting PredNodeMap type
|
alpar@953
|
255 |
template <class T>
|
alpar@953
|
256 |
class SetPredNodeMap : public Dijkstra< Graph,
|
alpar@953
|
257 |
LengthMap,
|
alpar@953
|
258 |
SetPredNodeMapTraits<T> > { };
|
alpar@953
|
259 |
|
alpar@953
|
260 |
template <class T>
|
alpar@953
|
261 |
struct SetDistMapTraits : public Traits {
|
alpar@953
|
262 |
typedef T DistMap;
|
alpar@953
|
263 |
///\todo An exception should be thrown.
|
alpar@953
|
264 |
///
|
alpar@953
|
265 |
static DistMap *createDistMap(const Graph &G)
|
alpar@953
|
266 |
{
|
alpar@953
|
267 |
std::cerr << __FILE__ ":" << __LINE__ <<
|
alpar@953
|
268 |
": error: Special maps should be manually created" << std::endl;
|
alpar@953
|
269 |
exit(1);
|
alpar@953
|
270 |
}
|
alpar@953
|
271 |
};
|
alpar@954
|
272 |
///\ref named-templ-param "Named parameter" for setting DistMap type
|
alpar@954
|
273 |
|
alpar@954
|
274 |
///\ingroup flowalgs
|
alpar@954
|
275 |
///\ref named-templ-param "Named parameter" for setting DistMap type
|
alpar@953
|
276 |
template <class T>
|
alpar@953
|
277 |
class SetDistMap : public Dijkstra< Graph,
|
alpar@953
|
278 |
LengthMap,
|
alpar@953
|
279 |
SetDistMapTraits<T> > { };
|
alpar@953
|
280 |
|
alpar@802
|
281 |
///Constructor.
|
alpar@255
|
282 |
|
alpar@802
|
283 |
///\param _G the graph the algorithm will run on.
|
alpar@802
|
284 |
///\param _length the length map used by the algorithm.
|
alpar@954
|
285 |
Dijkstra(const Graph& _G, const LengthMap& _length) :
|
alpar@688
|
286 |
G(&_G), length(&_length),
|
alpar@707
|
287 |
predecessor(NULL), local_predecessor(false),
|
alpar@707
|
288 |
pred_node(NULL), local_pred_node(false),
|
alpar@707
|
289 |
distance(NULL), local_distance(false)
|
alpar@688
|
290 |
{ }
|
alpar@688
|
291 |
|
alpar@802
|
292 |
///Destructor.
|
alpar@688
|
293 |
~Dijkstra()
|
alpar@688
|
294 |
{
|
alpar@688
|
295 |
if(local_predecessor) delete predecessor;
|
alpar@688
|
296 |
if(local_pred_node) delete pred_node;
|
alpar@688
|
297 |
if(local_distance) delete distance;
|
alpar@688
|
298 |
}
|
alpar@688
|
299 |
|
alpar@688
|
300 |
///Sets the length map.
|
alpar@688
|
301 |
|
alpar@688
|
302 |
///Sets the length map.
|
alpar@688
|
303 |
///\return <tt> (*this) </tt>
|
alpar@954
|
304 |
Dijkstra &setLengthMap(const LengthMap &m)
|
alpar@688
|
305 |
{
|
alpar@688
|
306 |
length = &m;
|
alpar@688
|
307 |
return *this;
|
alpar@688
|
308 |
}
|
alpar@688
|
309 |
|
alpar@688
|
310 |
///Sets the map storing the predecessor edges.
|
alpar@688
|
311 |
|
alpar@688
|
312 |
///Sets the map storing the predecessor edges.
|
alpar@688
|
313 |
///If you don't use this function before calling \ref run(),
|
alpar@688
|
314 |
///it will allocate one. The destuctor deallocates this
|
alpar@688
|
315 |
///automatically allocated map, of course.
|
alpar@688
|
316 |
///\return <tt> (*this) </tt>
|
alpar@688
|
317 |
Dijkstra &setPredMap(PredMap &m)
|
alpar@688
|
318 |
{
|
alpar@688
|
319 |
if(local_predecessor) {
|
alpar@688
|
320 |
delete predecessor;
|
alpar@688
|
321 |
local_predecessor=false;
|
alpar@688
|
322 |
}
|
alpar@688
|
323 |
predecessor = &m;
|
alpar@688
|
324 |
return *this;
|
alpar@688
|
325 |
}
|
alpar@688
|
326 |
|
alpar@688
|
327 |
///Sets the map storing the predecessor nodes.
|
alpar@688
|
328 |
|
alpar@688
|
329 |
///Sets the map storing the predecessor nodes.
|
alpar@688
|
330 |
///If you don't use this function before calling \ref run(),
|
alpar@688
|
331 |
///it will allocate one. The destuctor deallocates this
|
alpar@688
|
332 |
///automatically allocated map, of course.
|
alpar@688
|
333 |
///\return <tt> (*this) </tt>
|
alpar@688
|
334 |
Dijkstra &setPredNodeMap(PredNodeMap &m)
|
alpar@688
|
335 |
{
|
alpar@688
|
336 |
if(local_pred_node) {
|
alpar@688
|
337 |
delete pred_node;
|
alpar@688
|
338 |
local_pred_node=false;
|
alpar@688
|
339 |
}
|
alpar@688
|
340 |
pred_node = &m;
|
alpar@688
|
341 |
return *this;
|
alpar@688
|
342 |
}
|
alpar@688
|
343 |
|
alpar@688
|
344 |
///Sets the map storing the distances calculated by the algorithm.
|
alpar@688
|
345 |
|
alpar@688
|
346 |
///Sets the map storing the distances calculated by the algorithm.
|
alpar@688
|
347 |
///If you don't use this function before calling \ref run(),
|
alpar@688
|
348 |
///it will allocate one. The destuctor deallocates this
|
alpar@688
|
349 |
///automatically allocated map, of course.
|
alpar@688
|
350 |
///\return <tt> (*this) </tt>
|
alpar@688
|
351 |
Dijkstra &setDistMap(DistMap &m)
|
alpar@688
|
352 |
{
|
alpar@688
|
353 |
if(local_distance) {
|
alpar@688
|
354 |
delete distance;
|
alpar@688
|
355 |
local_distance=false;
|
alpar@688
|
356 |
}
|
alpar@688
|
357 |
distance = &m;
|
alpar@688
|
358 |
return *this;
|
alpar@688
|
359 |
}
|
alpar@255
|
360 |
|
alpar@694
|
361 |
///Runs %Dijkstra algorithm from node \c s.
|
alpar@694
|
362 |
|
alpar@694
|
363 |
///This method runs the %Dijkstra algorithm from a root node \c s
|
alpar@694
|
364 |
///in order to
|
alpar@694
|
365 |
///compute the
|
alpar@694
|
366 |
///shortest path to each node. The algorithm computes
|
alpar@694
|
367 |
///- The shortest path tree.
|
alpar@694
|
368 |
///- The distance of each node from the root.
|
alpar@954
|
369 |
///\todo heap_map's type could also be in the traits class.
|
alpar@694
|
370 |
void run(Node s) {
|
alpar@694
|
371 |
|
alpar@694
|
372 |
init_maps();
|
alpar@694
|
373 |
|
alpar@774
|
374 |
source = s;
|
alpar@774
|
375 |
|
alpar@774
|
376 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
|
alpar@694
|
377 |
predecessor->set(u,INVALID);
|
alpar@694
|
378 |
pred_node->set(u,INVALID);
|
alpar@694
|
379 |
}
|
alpar@694
|
380 |
|
alpar@954
|
381 |
typename Graph::template NodeMap<int> heap_map(*G,-1);
|
alpar@694
|
382 |
|
alpar@953
|
383 |
Heap heap(heap_map);
|
alpar@694
|
384 |
|
alpar@694
|
385 |
heap.push(s,0);
|
alpar@694
|
386 |
|
alpar@694
|
387 |
while ( !heap.empty() ) {
|
alpar@694
|
388 |
|
alpar@694
|
389 |
Node v=heap.top();
|
alpar@987
|
390 |
Value oldvalue=heap[v];
|
alpar@694
|
391 |
heap.pop();
|
alpar@694
|
392 |
distance->set(v, oldvalue);
|
alpar@694
|
393 |
|
alpar@694
|
394 |
|
alpar@774
|
395 |
for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
|
alpar@986
|
396 |
Node w=G->target(e);
|
alpar@694
|
397 |
switch(heap.state(w)) {
|
alpar@953
|
398 |
case Heap::PRE_HEAP:
|
alpar@694
|
399 |
heap.push(w,oldvalue+(*length)[e]);
|
alpar@694
|
400 |
predecessor->set(w,e);
|
alpar@694
|
401 |
pred_node->set(w,v);
|
alpar@694
|
402 |
break;
|
alpar@953
|
403 |
case Heap::IN_HEAP:
|
alpar@694
|
404 |
if ( oldvalue+(*length)[e] < heap[w] ) {
|
alpar@694
|
405 |
heap.decrease(w, oldvalue+(*length)[e]);
|
alpar@694
|
406 |
predecessor->set(w,e);
|
alpar@694
|
407 |
pred_node->set(w,v);
|
alpar@694
|
408 |
}
|
alpar@694
|
409 |
break;
|
alpar@953
|
410 |
case Heap::POST_HEAP:
|
alpar@694
|
411 |
break;
|
alpar@694
|
412 |
}
|
alpar@694
|
413 |
}
|
alpar@694
|
414 |
}
|
alpar@694
|
415 |
}
|
alpar@255
|
416 |
|
jacint@385
|
417 |
///The distance of a node from the root.
|
alpar@255
|
418 |
|
jacint@385
|
419 |
///Returns the distance of a node from the root.
|
alpar@255
|
420 |
///\pre \ref run() must be called before using this function.
|
jacint@385
|
421 |
///\warning If node \c v in unreachable from the root the return value
|
alpar@255
|
422 |
///of this funcion is undefined.
|
alpar@987
|
423 |
Value dist(Node v) const { return (*distance)[v]; }
|
jacint@373
|
424 |
|
alpar@584
|
425 |
///Returns the 'previous edge' of the shortest path tree.
|
alpar@255
|
426 |
|
alpar@584
|
427 |
///For a node \c v it returns the 'previous edge' of the shortest path tree,
|
alpar@785
|
428 |
///i.e. it returns the last edge of a shortest path from the root to \c
|
alpar@688
|
429 |
///v. It is \ref INVALID
|
alpar@688
|
430 |
///if \c v is unreachable from the root or if \c v=s. The
|
jacint@385
|
431 |
///shortest path tree used here is equal to the shortest path tree used in
|
jacint@385
|
432 |
///\ref predNode(Node v). \pre \ref run() must be called before using
|
jacint@385
|
433 |
///this function.
|
alpar@780
|
434 |
///\todo predEdge could be a better name.
|
alpar@688
|
435 |
Edge pred(Node v) const { return (*predecessor)[v]; }
|
jacint@373
|
436 |
|
alpar@584
|
437 |
///Returns the 'previous node' of the shortest path tree.
|
alpar@255
|
438 |
|
alpar@584
|
439 |
///For a node \c v it returns the 'previous node' of the shortest path tree,
|
jacint@385
|
440 |
///i.e. it returns the last but one node from a shortest path from the
|
jacint@385
|
441 |
///root to \c /v. It is INVALID if \c v is unreachable from the root or if
|
jacint@385
|
442 |
///\c v=s. The shortest path tree used here is equal to the shortest path
|
jacint@385
|
443 |
///tree used in \ref pred(Node v). \pre \ref run() must be called before
|
jacint@385
|
444 |
///using this function.
|
alpar@688
|
445 |
Node predNode(Node v) const { return (*pred_node)[v]; }
|
alpar@255
|
446 |
|
alpar@255
|
447 |
///Returns a reference to the NodeMap of distances.
|
alpar@255
|
448 |
|
jacint@385
|
449 |
///Returns a reference to the NodeMap of distances. \pre \ref run() must
|
jacint@385
|
450 |
///be called before using this function.
|
alpar@688
|
451 |
const DistMap &distMap() const { return *distance;}
|
jacint@385
|
452 |
|
alpar@255
|
453 |
///Returns a reference to the shortest path tree map.
|
alpar@255
|
454 |
|
alpar@255
|
455 |
///Returns a reference to the NodeMap of the edges of the
|
alpar@255
|
456 |
///shortest path tree.
|
alpar@255
|
457 |
///\pre \ref run() must be called before using this function.
|
alpar@688
|
458 |
const PredMap &predMap() const { return *predecessor;}
|
jacint@385
|
459 |
|
jacint@385
|
460 |
///Returns a reference to the map of nodes of shortest paths.
|
alpar@255
|
461 |
|
alpar@255
|
462 |
///Returns a reference to the NodeMap of the last but one nodes of the
|
jacint@385
|
463 |
///shortest path tree.
|
alpar@255
|
464 |
///\pre \ref run() must be called before using this function.
|
alpar@688
|
465 |
const PredNodeMap &predNodeMap() const { return *pred_node;}
|
alpar@255
|
466 |
|
jacint@385
|
467 |
///Checks if a node is reachable from the root.
|
alpar@255
|
468 |
|
jacint@385
|
469 |
///Returns \c true if \c v is reachable from the root.
|
alpar@802
|
470 |
///\note The root node is reported to be reached!
|
alpar@255
|
471 |
///\pre \ref run() must be called before using this function.
|
jacint@385
|
472 |
///
|
alpar@780
|
473 |
bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
|
alpar@255
|
474 |
|
alpar@255
|
475 |
};
|
alpar@953
|
476 |
|
alpar@953
|
477 |
///\e
|
alpar@953
|
478 |
|
alpar@953
|
479 |
///\e
|
alpar@953
|
480 |
///
|
alpar@953
|
481 |
template<class TR>
|
alpar@953
|
482 |
class _Dijkstra
|
alpar@953
|
483 |
{
|
alpar@953
|
484 |
typedef TR Traits;
|
alpar@953
|
485 |
|
alpar@953
|
486 |
///The type of the underlying graph.
|
alpar@953
|
487 |
typedef typename TR::Graph Graph;
|
alpar@953
|
488 |
///\e
|
alpar@953
|
489 |
typedef typename Graph::Node Node;
|
alpar@953
|
490 |
///\e
|
alpar@953
|
491 |
typedef typename Graph::NodeIt NodeIt;
|
alpar@953
|
492 |
///\e
|
alpar@953
|
493 |
typedef typename Graph::Edge Edge;
|
alpar@953
|
494 |
///\e
|
alpar@953
|
495 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
alpar@953
|
496 |
|
alpar@953
|
497 |
///The type of the map that stores the edge lengths.
|
alpar@953
|
498 |
typedef typename TR::LengthMap LengthMap;
|
alpar@953
|
499 |
///The type of the length of the edges.
|
alpar@987
|
500 |
typedef typename LengthMap::Value Value;
|
alpar@953
|
501 |
///\brief The type of the map that stores the last
|
alpar@953
|
502 |
///edges of the shortest paths.
|
alpar@953
|
503 |
typedef typename TR::PredMap PredMap;
|
alpar@953
|
504 |
///\brief The type of the map that stores the last but one
|
alpar@953
|
505 |
///nodes of the shortest paths.
|
alpar@953
|
506 |
typedef typename TR::PredNodeMap PredNodeMap;
|
alpar@953
|
507 |
///The type of the map that stores the dists of the nodes.
|
alpar@953
|
508 |
typedef typename TR::DistMap DistMap;
|
alpar@953
|
509 |
|
alpar@953
|
510 |
///The heap type used by the dijkstra algorithm.
|
alpar@953
|
511 |
typedef typename TR::Heap Heap;
|
alpar@953
|
512 |
|
alpar@953
|
513 |
/// Pointer to the underlying graph.
|
alpar@953
|
514 |
const Graph *G;
|
alpar@953
|
515 |
/// Pointer to the length map
|
alpar@953
|
516 |
const LengthMap *length;
|
alpar@953
|
517 |
///Pointer to the map of predecessors edges.
|
alpar@953
|
518 |
PredMap *predecessor;
|
alpar@953
|
519 |
///Pointer to the map of predecessors nodes.
|
alpar@953
|
520 |
PredNodeMap *pred_node;
|
alpar@953
|
521 |
///Pointer to the map of distances.
|
alpar@953
|
522 |
DistMap *distance;
|
alpar@953
|
523 |
|
alpar@953
|
524 |
Node source;
|
alpar@953
|
525 |
|
alpar@953
|
526 |
public:
|
alpar@953
|
527 |
_Dijkstra() : G(0), length(0), predecessor(0), pred_node(0),
|
alpar@953
|
528 |
distance(0), source(INVALID) {}
|
alpar@953
|
529 |
|
alpar@953
|
530 |
_Dijkstra(const Graph &g,const LengthMap &l, Node s) :
|
alpar@953
|
531 |
G(&g), length(&l), predecessor(0), pred_node(0),
|
alpar@953
|
532 |
distance(0), source(s) {}
|
alpar@953
|
533 |
|
alpar@953
|
534 |
~_Dijkstra()
|
alpar@953
|
535 |
{
|
alpar@953
|
536 |
Dijkstra<Graph,LengthMap,TR> Dij(*G,*length);
|
alpar@953
|
537 |
if(predecessor) Dij.setPredMap(*predecessor);
|
alpar@953
|
538 |
if(pred_node) Dij.setPredNodeMap(*pred_node);
|
alpar@953
|
539 |
if(distance) Dij.setDistMap(*distance);
|
alpar@953
|
540 |
Dij.run(source);
|
alpar@953
|
541 |
}
|
alpar@953
|
542 |
|
alpar@953
|
543 |
template<class T>
|
alpar@953
|
544 |
struct SetPredMapTraits : public Traits {typedef T PredMap;};
|
alpar@953
|
545 |
|
alpar@953
|
546 |
///\e
|
alpar@953
|
547 |
template<class T>
|
alpar@953
|
548 |
_Dijkstra<SetPredMapTraits<T> > setPredMap(const T &t)
|
alpar@953
|
549 |
{
|
alpar@953
|
550 |
_Dijkstra<SetPredMapTraits<T> > r;
|
alpar@953
|
551 |
r.G=G;
|
alpar@953
|
552 |
r.length=length;
|
alpar@953
|
553 |
r.predecessor=&t;
|
alpar@953
|
554 |
r.pred_node=pred_node;
|
alpar@953
|
555 |
r.distance=distance;
|
alpar@953
|
556 |
r.source=source;
|
alpar@953
|
557 |
return r;
|
alpar@953
|
558 |
}
|
alpar@953
|
559 |
|
alpar@953
|
560 |
template<class T>
|
alpar@953
|
561 |
struct SetPredNodeMapTraits :public Traits {typedef T PredNodeMap;};
|
alpar@953
|
562 |
///\e
|
alpar@953
|
563 |
template<class T>
|
alpar@953
|
564 |
_Dijkstra<SetPredNodeMapTraits<T> > setPredNodeMap(const T &t)
|
alpar@953
|
565 |
{
|
alpar@953
|
566 |
_Dijkstra<SetPredNodeMapTraits<T> > r;
|
alpar@953
|
567 |
r.G=G;
|
alpar@953
|
568 |
r.length=length;
|
alpar@953
|
569 |
r.predecessor=predecessor;
|
alpar@953
|
570 |
r.pred_node=&t;
|
alpar@953
|
571 |
r.distance=distance;
|
alpar@953
|
572 |
r.source=source;
|
alpar@953
|
573 |
return r;
|
alpar@953
|
574 |
}
|
alpar@953
|
575 |
|
alpar@953
|
576 |
template<class T>
|
alpar@953
|
577 |
struct SetDistMapTraits : public Traits {typedef T DistMap;};
|
alpar@953
|
578 |
///\e
|
alpar@953
|
579 |
template<class T>
|
alpar@953
|
580 |
_Dijkstra<SetDistMapTraits<T> > setDistMap(const T &t)
|
alpar@953
|
581 |
{
|
alpar@953
|
582 |
_Dijkstra<SetPredMapTraits<T> > r;
|
alpar@953
|
583 |
r.G=G;
|
alpar@953
|
584 |
r.length=length;
|
alpar@953
|
585 |
r.predecessor=predecessor;
|
alpar@953
|
586 |
r.pred_node=pred_node;
|
alpar@953
|
587 |
r.distance=&t;
|
alpar@953
|
588 |
r.source=source;
|
alpar@953
|
589 |
return r;
|
alpar@953
|
590 |
}
|
alpar@953
|
591 |
|
alpar@953
|
592 |
///\e
|
alpar@953
|
593 |
_Dijkstra<TR> &setSource(Node s)
|
alpar@953
|
594 |
{
|
alpar@953
|
595 |
source=s;
|
alpar@953
|
596 |
return *this;
|
alpar@953
|
597 |
}
|
alpar@953
|
598 |
|
alpar@953
|
599 |
};
|
alpar@255
|
600 |
|
alpar@953
|
601 |
///\e
|
alpar@953
|
602 |
|
alpar@954
|
603 |
///\todo Please document...
|
alpar@953
|
604 |
///
|
alpar@953
|
605 |
template<class GR, class LM>
|
alpar@953
|
606 |
_Dijkstra<DijkstraDefaultTraits<GR,LM> >
|
alpar@953
|
607 |
dijkstra(const GR &g,const LM &l,typename GR::Node s)
|
alpar@953
|
608 |
{
|
alpar@953
|
609 |
return _Dijkstra<DijkstraDefaultTraits<GR,LM> >(g,l,s);
|
alpar@953
|
610 |
}
|
alpar@953
|
611 |
|
alpar@430
|
612 |
/// @}
|
alpar@255
|
613 |
|
alpar@921
|
614 |
} //END OF NAMESPACE LEMON
|
alpar@255
|
615 |
|
alpar@255
|
616 |
#endif
|
alpar@255
|
617 |
|
alpar@255
|
618 |
|