alpar@209
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
alpar@100
|
2 |
*
|
alpar@209
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
alpar@100
|
4 |
*
|
alpar@100
|
5 |
* Copyright (C) 2003-2008
|
alpar@100
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@100
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@100
|
8 |
*
|
alpar@100
|
9 |
* Permission to use, modify and distribute this software is granted
|
alpar@100
|
10 |
* provided that this copyright notice appears in all copies. For
|
alpar@100
|
11 |
* precise terms see the accompanying LICENSE file.
|
alpar@100
|
12 |
*
|
alpar@100
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@100
|
14 |
* express or implied, and with no claim as to its suitability for any
|
alpar@100
|
15 |
* purpose.
|
alpar@100
|
16 |
*
|
alpar@100
|
17 |
*/
|
alpar@100
|
18 |
|
alpar@100
|
19 |
#ifndef LEMON_DIJKSTRA_H
|
alpar@100
|
20 |
#define LEMON_DIJKSTRA_H
|
alpar@100
|
21 |
|
alpar@100
|
22 |
///\ingroup shortest_path
|
alpar@100
|
23 |
///\file
|
alpar@100
|
24 |
///\brief Dijkstra algorithm.
|
alpar@100
|
25 |
|
alpar@184
|
26 |
#include <limits>
|
kpeter@169
|
27 |
#include <lemon/list_graph.h>
|
alpar@100
|
28 |
#include <lemon/bin_heap.h>
|
alpar@100
|
29 |
#include <lemon/bits/path_dump.h>
|
deba@220
|
30 |
#include <lemon/core.h>
|
alpar@100
|
31 |
#include <lemon/error.h>
|
alpar@100
|
32 |
#include <lemon/maps.h>
|
alpar@100
|
33 |
|
alpar@100
|
34 |
namespace lemon {
|
alpar@100
|
35 |
|
kpeter@244
|
36 |
/// \brief Default operation traits for the Dijkstra algorithm class.
|
alpar@209
|
37 |
///
|
kpeter@244
|
38 |
/// This operation traits class defines all computational operations and
|
kpeter@244
|
39 |
/// constants which are used in the Dijkstra algorithm.
|
alpar@100
|
40 |
template <typename Value>
|
alpar@100
|
41 |
struct DijkstraDefaultOperationTraits {
|
alpar@100
|
42 |
/// \brief Gives back the zero value of the type.
|
alpar@100
|
43 |
static Value zero() {
|
alpar@100
|
44 |
return static_cast<Value>(0);
|
alpar@100
|
45 |
}
|
alpar@100
|
46 |
/// \brief Gives back the sum of the given two elements.
|
alpar@100
|
47 |
static Value plus(const Value& left, const Value& right) {
|
alpar@100
|
48 |
return left + right;
|
alpar@100
|
49 |
}
|
kpeter@244
|
50 |
/// \brief Gives back true only if the first value is less than the second.
|
alpar@100
|
51 |
static bool less(const Value& left, const Value& right) {
|
alpar@100
|
52 |
return left < right;
|
alpar@100
|
53 |
}
|
alpar@100
|
54 |
};
|
alpar@100
|
55 |
|
kpeter@244
|
56 |
/// \brief Widest path operation traits for the Dijkstra algorithm class.
|
alpar@209
|
57 |
///
|
kpeter@244
|
58 |
/// This operation traits class defines all computational operations and
|
kpeter@244
|
59 |
/// constants which are used in the Dijkstra algorithm for widest path
|
kpeter@244
|
60 |
/// computation.
|
kpeter@244
|
61 |
///
|
kpeter@244
|
62 |
/// \see DijkstraDefaultOperationTraits
|
alpar@100
|
63 |
template <typename Value>
|
alpar@100
|
64 |
struct DijkstraWidestPathOperationTraits {
|
alpar@100
|
65 |
/// \brief Gives back the maximum value of the type.
|
alpar@100
|
66 |
static Value zero() {
|
alpar@100
|
67 |
return std::numeric_limits<Value>::max();
|
alpar@100
|
68 |
}
|
alpar@100
|
69 |
/// \brief Gives back the minimum of the given two elements.
|
alpar@100
|
70 |
static Value plus(const Value& left, const Value& right) {
|
alpar@100
|
71 |
return std::min(left, right);
|
alpar@100
|
72 |
}
|
kpeter@244
|
73 |
/// \brief Gives back true only if the first value is less than the second.
|
alpar@100
|
74 |
static bool less(const Value& left, const Value& right) {
|
alpar@100
|
75 |
return left < right;
|
alpar@100
|
76 |
}
|
alpar@100
|
77 |
};
|
alpar@209
|
78 |
|
alpar@100
|
79 |
///Default traits class of Dijkstra class.
|
alpar@100
|
80 |
|
alpar@100
|
81 |
///Default traits class of Dijkstra class.
|
kpeter@244
|
82 |
///\tparam GR The type of the digraph.
|
kpeter@244
|
83 |
///\tparam LM The type of the length map.
|
alpar@100
|
84 |
template<class GR, class LM>
|
alpar@100
|
85 |
struct DijkstraDefaultTraits
|
alpar@100
|
86 |
{
|
kpeter@244
|
87 |
///The type of the digraph the algorithm runs on.
|
alpar@100
|
88 |
typedef GR Digraph;
|
kpeter@244
|
89 |
|
alpar@100
|
90 |
///The type of the map that stores the arc lengths.
|
alpar@100
|
91 |
|
alpar@100
|
92 |
///The type of the map that stores the arc lengths.
|
alpar@100
|
93 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept.
|
alpar@100
|
94 |
typedef LM LengthMap;
|
kpeter@244
|
95 |
///The type of the length of the arcs.
|
alpar@100
|
96 |
typedef typename LM::Value Value;
|
kpeter@244
|
97 |
|
alpar@100
|
98 |
/// Operation traits for Dijkstra algorithm.
|
alpar@100
|
99 |
|
kpeter@244
|
100 |
/// This class defines the operations that are used in the algorithm.
|
alpar@100
|
101 |
/// \see DijkstraDefaultOperationTraits
|
alpar@100
|
102 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits;
|
alpar@100
|
103 |
|
kpeter@244
|
104 |
/// The cross reference type used by the heap.
|
alpar@100
|
105 |
|
kpeter@244
|
106 |
/// The cross reference type used by the heap.
|
alpar@100
|
107 |
/// Usually it is \c Digraph::NodeMap<int>.
|
alpar@100
|
108 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef;
|
kpeter@244
|
109 |
///Instantiates a \ref HeapCrossRef.
|
alpar@100
|
110 |
|
kpeter@244
|
111 |
///This function instantiates a \ref HeapCrossRef.
|
kpeter@244
|
112 |
/// \param g is the digraph, to which we would like to define the
|
kpeter@244
|
113 |
/// \ref HeapCrossRef.
|
kpeter@244
|
114 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g)
|
alpar@100
|
115 |
{
|
kpeter@244
|
116 |
return new HeapCrossRef(g);
|
alpar@100
|
117 |
}
|
alpar@209
|
118 |
|
kpeter@244
|
119 |
///The heap type used by the Dijkstra algorithm.
|
alpar@100
|
120 |
|
kpeter@244
|
121 |
///The heap type used by the Dijkstra algorithm.
|
alpar@100
|
122 |
///
|
alpar@100
|
123 |
///\sa BinHeap
|
alpar@100
|
124 |
///\sa Dijkstra
|
alpar@100
|
125 |
typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap;
|
kpeter@244
|
126 |
///Instantiates a \ref Heap.
|
alpar@100
|
127 |
|
kpeter@244
|
128 |
///This function instantiates a \ref Heap.
|
kpeter@244
|
129 |
static Heap *createHeap(HeapCrossRef& r)
|
alpar@100
|
130 |
{
|
kpeter@244
|
131 |
return new Heap(r);
|
alpar@100
|
132 |
}
|
alpar@100
|
133 |
|
kpeter@244
|
134 |
///\brief The type of the map that stores the predecessor
|
alpar@100
|
135 |
///arcs of the shortest paths.
|
alpar@209
|
136 |
///
|
kpeter@244
|
137 |
///The type of the map that stores the predecessor
|
alpar@100
|
138 |
///arcs of the shortest paths.
|
alpar@100
|
139 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept.
|
kpeter@244
|
140 |
typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap;
|
kpeter@244
|
141 |
///Instantiates a \ref PredMap.
|
alpar@209
|
142 |
|
kpeter@244
|
143 |
///This function instantiates a \ref PredMap.
|
kpeter@244
|
144 |
///\param g is the digraph, to which we would like to define the
|
kpeter@244
|
145 |
///\ref PredMap.
|
alpar@100
|
146 |
///\todo The digraph alone may be insufficient for the initialization
|
kpeter@244
|
147 |
static PredMap *createPredMap(const Digraph &g)
|
alpar@100
|
148 |
{
|
kpeter@244
|
149 |
return new PredMap(g);
|
alpar@100
|
150 |
}
|
alpar@100
|
151 |
|
kpeter@244
|
152 |
///The type of the map that indicates which nodes are processed.
|
alpar@209
|
153 |
|
kpeter@244
|
154 |
///The type of the map that indicates which nodes are processed.
|
alpar@100
|
155 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept.
|
alpar@100
|
156 |
///By default it is a NullMap.
|
alpar@100
|
157 |
///\todo If it is set to a real map,
|
alpar@100
|
158 |
///Dijkstra::processed() should read this.
|
alpar@100
|
159 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
|
kpeter@244
|
160 |
///Instantiates a \ref ProcessedMap.
|
alpar@209
|
161 |
|
kpeter@244
|
162 |
///This function instantiates a \ref ProcessedMap.
|
alpar@100
|
163 |
///\param g is the digraph, to which
|
kpeter@244
|
164 |
///we would like to define the \ref ProcessedMap
|
alpar@100
|
165 |
#ifdef DOXYGEN
|
kpeter@244
|
166 |
static ProcessedMap *createProcessedMap(const Digraph &g)
|
alpar@100
|
167 |
#else
|
kpeter@244
|
168 |
static ProcessedMap *createProcessedMap(const Digraph &)
|
alpar@100
|
169 |
#endif
|
alpar@100
|
170 |
{
|
alpar@100
|
171 |
return new ProcessedMap();
|
alpar@100
|
172 |
}
|
alpar@209
|
173 |
|
kpeter@244
|
174 |
///The type of the map that stores the distances of the nodes.
|
kpeter@244
|
175 |
|
kpeter@244
|
176 |
///The type of the map that stores the distances of the nodes.
|
alpar@100
|
177 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept.
|
alpar@100
|
178 |
typedef typename Digraph::template NodeMap<typename LM::Value> DistMap;
|
kpeter@244
|
179 |
///Instantiates a \ref DistMap.
|
alpar@209
|
180 |
|
alpar@209
|
181 |
///This function instantiates a \ref DistMap.
|
kpeter@244
|
182 |
///\param g is the digraph, to which we would like to define
|
alpar@210
|
183 |
///the \ref DistMap
|
kpeter@244
|
184 |
static DistMap *createDistMap(const Digraph &g)
|
alpar@100
|
185 |
{
|
kpeter@244
|
186 |
return new DistMap(g);
|
alpar@100
|
187 |
}
|
alpar@100
|
188 |
};
|
alpar@209
|
189 |
|
alpar@100
|
190 |
///%Dijkstra algorithm class.
|
alpar@209
|
191 |
|
alpar@100
|
192 |
/// \ingroup shortest_path
|
kpeter@244
|
193 |
///This class provides an efficient implementation of the %Dijkstra algorithm.
|
kpeter@244
|
194 |
///
|
alpar@100
|
195 |
///The arc lengths are passed to the algorithm using a
|
alpar@100
|
196 |
///\ref concepts::ReadMap "ReadMap",
|
alpar@100
|
197 |
///so it is easy to change it to any kind of length.
|
alpar@100
|
198 |
///The type of the length is determined by the
|
alpar@100
|
199 |
///\ref concepts::ReadMap::Value "Value" of the length map.
|
alpar@100
|
200 |
///It is also possible to change the underlying priority heap.
|
alpar@100
|
201 |
///
|
kpeter@244
|
202 |
///There is also a \ref dijkstra() "function type interface" for the
|
kpeter@244
|
203 |
///%Dijkstra algorithm, which is convenient in the simplier cases and
|
kpeter@244
|
204 |
///it can be used easier.
|
kpeter@244
|
205 |
///
|
kpeter@244
|
206 |
///\tparam GR The type of the digraph the algorithm runs on.
|
kpeter@244
|
207 |
///The default value is \ref ListDigraph.
|
kpeter@244
|
208 |
///The value of GR is not used directly by \ref Dijkstra, it is only
|
kpeter@244
|
209 |
///passed to \ref DijkstraDefaultTraits.
|
kpeter@244
|
210 |
///\tparam LM A readable arc map that determines the lengths of the
|
alpar@100
|
211 |
///arcs. It is read once for each arc, so the map may involve in
|
kpeter@244
|
212 |
///relatively time consuming process to compute the arc lengths if
|
alpar@100
|
213 |
///it is necessary. The default map type is \ref
|
kpeter@244
|
214 |
///concepts::Digraph::ArcMap "Digraph::ArcMap<int>".
|
kpeter@244
|
215 |
///The value of LM is not used directly by \ref Dijkstra, it is only
|
kpeter@244
|
216 |
///passed to \ref DijkstraDefaultTraits.
|
kpeter@244
|
217 |
///\tparam TR Traits class to set various data types used by the algorithm.
|
kpeter@244
|
218 |
///The default traits class is \ref DijkstraDefaultTraits
|
kpeter@244
|
219 |
///"DijkstraDefaultTraits<GR,LM>". See \ref DijkstraDefaultTraits
|
kpeter@244
|
220 |
///for the documentation of a Dijkstra traits class.
|
alpar@100
|
221 |
#ifdef DOXYGEN
|
alpar@100
|
222 |
template <typename GR, typename LM, typename TR>
|
alpar@100
|
223 |
#else
|
alpar@100
|
224 |
template <typename GR=ListDigraph,
|
alpar@209
|
225 |
typename LM=typename GR::template ArcMap<int>,
|
alpar@209
|
226 |
typename TR=DijkstraDefaultTraits<GR,LM> >
|
alpar@100
|
227 |
#endif
|
alpar@100
|
228 |
class Dijkstra {
|
alpar@100
|
229 |
public:
|
kpeter@244
|
230 |
///\ref Exception for uninitialized parameters.
|
kpeter@244
|
231 |
|
kpeter@244
|
232 |
///This error represents problems in the initialization of the
|
kpeter@244
|
233 |
///parameters of the algorithm.
|
alpar@100
|
234 |
class UninitializedParameter : public lemon::UninitializedParameter {
|
alpar@100
|
235 |
public:
|
alpar@100
|
236 |
virtual const char* what() const throw() {
|
alpar@209
|
237 |
return "lemon::Dijkstra::UninitializedParameter";
|
alpar@100
|
238 |
}
|
alpar@100
|
239 |
};
|
alpar@100
|
240 |
|
kpeter@244
|
241 |
///The type of the digraph the algorithm runs on.
|
alpar@100
|
242 |
typedef typename TR::Digraph Digraph;
|
alpar@209
|
243 |
|
alpar@100
|
244 |
///The type of the length of the arcs.
|
alpar@100
|
245 |
typedef typename TR::LengthMap::Value Value;
|
alpar@100
|
246 |
///The type of the map that stores the arc lengths.
|
alpar@100
|
247 |
typedef typename TR::LengthMap LengthMap;
|
kpeter@244
|
248 |
///\brief The type of the map that stores the predecessor arcs of the
|
kpeter@244
|
249 |
///shortest paths.
|
alpar@100
|
250 |
typedef typename TR::PredMap PredMap;
|
kpeter@244
|
251 |
///The type of the map that stores the distances of the nodes.
|
kpeter@244
|
252 |
typedef typename TR::DistMap DistMap;
|
kpeter@244
|
253 |
///The type of the map that indicates which nodes are processed.
|
alpar@100
|
254 |
typedef typename TR::ProcessedMap ProcessedMap;
|
kpeter@244
|
255 |
///The type of the paths.
|
kpeter@244
|
256 |
typedef PredMapPath<Digraph, PredMap> Path;
|
alpar@100
|
257 |
///The cross reference type used for the current heap.
|
alpar@100
|
258 |
typedef typename TR::HeapCrossRef HeapCrossRef;
|
kpeter@244
|
259 |
///The heap type used by the algorithm.
|
alpar@100
|
260 |
typedef typename TR::Heap Heap;
|
kpeter@244
|
261 |
///The operation traits class.
|
alpar@100
|
262 |
typedef typename TR::OperationTraits OperationTraits;
|
kpeter@244
|
263 |
|
kpeter@244
|
264 |
///The traits class.
|
kpeter@244
|
265 |
typedef TR Traits;
|
kpeter@244
|
266 |
|
alpar@100
|
267 |
private:
|
kpeter@244
|
268 |
|
kpeter@244
|
269 |
typedef typename Digraph::Node Node;
|
kpeter@244
|
270 |
typedef typename Digraph::NodeIt NodeIt;
|
kpeter@244
|
271 |
typedef typename Digraph::Arc Arc;
|
kpeter@244
|
272 |
typedef typename Digraph::OutArcIt OutArcIt;
|
kpeter@244
|
273 |
|
kpeter@244
|
274 |
//Pointer to the underlying digraph.
|
alpar@100
|
275 |
const Digraph *G;
|
kpeter@244
|
276 |
//Pointer to the length map.
|
alpar@100
|
277 |
const LengthMap *length;
|
kpeter@244
|
278 |
//Pointer to the map of predecessors arcs.
|
alpar@100
|
279 |
PredMap *_pred;
|
kpeter@244
|
280 |
//Indicates if _pred is locally allocated (true) or not.
|
alpar@100
|
281 |
bool local_pred;
|
kpeter@244
|
282 |
//Pointer to the map of distances.
|
alpar@100
|
283 |
DistMap *_dist;
|
kpeter@244
|
284 |
//Indicates if _dist is locally allocated (true) or not.
|
alpar@100
|
285 |
bool local_dist;
|
kpeter@244
|
286 |
//Pointer to the map of processed status of the nodes.
|
alpar@100
|
287 |
ProcessedMap *_processed;
|
kpeter@244
|
288 |
//Indicates if _processed is locally allocated (true) or not.
|
alpar@100
|
289 |
bool local_processed;
|
kpeter@244
|
290 |
//Pointer to the heap cross references.
|
alpar@100
|
291 |
HeapCrossRef *_heap_cross_ref;
|
kpeter@244
|
292 |
//Indicates if _heap_cross_ref is locally allocated (true) or not.
|
alpar@100
|
293 |
bool local_heap_cross_ref;
|
kpeter@244
|
294 |
//Pointer to the heap.
|
alpar@100
|
295 |
Heap *_heap;
|
kpeter@244
|
296 |
//Indicates if _heap is locally allocated (true) or not.
|
alpar@100
|
297 |
bool local_heap;
|
alpar@100
|
298 |
|
alpar@100
|
299 |
///Creates the maps if necessary.
|
alpar@100
|
300 |
///\todo Better memory allocation (instead of new).
|
alpar@209
|
301 |
void create_maps()
|
alpar@100
|
302 |
{
|
alpar@100
|
303 |
if(!_pred) {
|
alpar@209
|
304 |
local_pred = true;
|
alpar@209
|
305 |
_pred = Traits::createPredMap(*G);
|
alpar@100
|
306 |
}
|
alpar@100
|
307 |
if(!_dist) {
|
alpar@209
|
308 |
local_dist = true;
|
alpar@209
|
309 |
_dist = Traits::createDistMap(*G);
|
alpar@100
|
310 |
}
|
alpar@100
|
311 |
if(!_processed) {
|
alpar@209
|
312 |
local_processed = true;
|
alpar@209
|
313 |
_processed = Traits::createProcessedMap(*G);
|
alpar@100
|
314 |
}
|
alpar@100
|
315 |
if (!_heap_cross_ref) {
|
alpar@209
|
316 |
local_heap_cross_ref = true;
|
alpar@209
|
317 |
_heap_cross_ref = Traits::createHeapCrossRef(*G);
|
alpar@100
|
318 |
}
|
alpar@100
|
319 |
if (!_heap) {
|
alpar@209
|
320 |
local_heap = true;
|
alpar@209
|
321 |
_heap = Traits::createHeap(*_heap_cross_ref);
|
alpar@100
|
322 |
}
|
alpar@100
|
323 |
}
|
alpar@209
|
324 |
|
kpeter@244
|
325 |
public:
|
alpar@100
|
326 |
|
alpar@100
|
327 |
typedef Dijkstra Create;
|
alpar@209
|
328 |
|
alpar@100
|
329 |
///\name Named template parameters
|
alpar@100
|
330 |
|
alpar@100
|
331 |
///@{
|
alpar@100
|
332 |
|
alpar@100
|
333 |
template <class T>
|
kpeter@257
|
334 |
struct SetPredMapTraits : public Traits {
|
alpar@100
|
335 |
typedef T PredMap;
|
alpar@100
|
336 |
static PredMap *createPredMap(const Digraph &)
|
alpar@100
|
337 |
{
|
alpar@209
|
338 |
throw UninitializedParameter();
|
alpar@100
|
339 |
}
|
alpar@100
|
340 |
};
|
kpeter@244
|
341 |
///\brief \ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
342 |
///\ref PredMap type.
|
alpar@100
|
343 |
///
|
kpeter@244
|
344 |
///\ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
345 |
///\ref PredMap type.
|
alpar@100
|
346 |
template <class T>
|
kpeter@257
|
347 |
struct SetPredMap
|
kpeter@257
|
348 |
: public Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > {
|
kpeter@257
|
349 |
typedef Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > Create;
|
alpar@100
|
350 |
};
|
alpar@209
|
351 |
|
alpar@100
|
352 |
template <class T>
|
kpeter@257
|
353 |
struct SetDistMapTraits : public Traits {
|
alpar@100
|
354 |
typedef T DistMap;
|
alpar@100
|
355 |
static DistMap *createDistMap(const Digraph &)
|
alpar@100
|
356 |
{
|
alpar@209
|
357 |
throw UninitializedParameter();
|
alpar@100
|
358 |
}
|
alpar@100
|
359 |
};
|
kpeter@244
|
360 |
///\brief \ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
361 |
///\ref DistMap type.
|
alpar@100
|
362 |
///
|
kpeter@244
|
363 |
///\ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
364 |
///\ref DistMap type.
|
alpar@100
|
365 |
template <class T>
|
kpeter@257
|
366 |
struct SetDistMap
|
kpeter@257
|
367 |
: public Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > {
|
kpeter@257
|
368 |
typedef Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > Create;
|
alpar@100
|
369 |
};
|
alpar@209
|
370 |
|
alpar@100
|
371 |
template <class T>
|
kpeter@257
|
372 |
struct SetProcessedMapTraits : public Traits {
|
alpar@100
|
373 |
typedef T ProcessedMap;
|
kpeter@244
|
374 |
static ProcessedMap *createProcessedMap(const Digraph &)
|
alpar@100
|
375 |
{
|
alpar@209
|
376 |
throw UninitializedParameter();
|
alpar@100
|
377 |
}
|
alpar@100
|
378 |
};
|
kpeter@244
|
379 |
///\brief \ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
380 |
///\ref ProcessedMap type.
|
alpar@100
|
381 |
///
|
kpeter@244
|
382 |
///\ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
383 |
///\ref ProcessedMap type.
|
alpar@100
|
384 |
template <class T>
|
kpeter@257
|
385 |
struct SetProcessedMap
|
kpeter@257
|
386 |
: public Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > {
|
kpeter@257
|
387 |
typedef Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > Create;
|
alpar@100
|
388 |
};
|
alpar@209
|
389 |
|
kpeter@257
|
390 |
struct SetStandardProcessedMapTraits : public Traits {
|
alpar@100
|
391 |
typedef typename Digraph::template NodeMap<bool> ProcessedMap;
|
kpeter@244
|
392 |
static ProcessedMap *createProcessedMap(const Digraph &g)
|
alpar@100
|
393 |
{
|
kpeter@244
|
394 |
return new ProcessedMap(g);
|
alpar@100
|
395 |
}
|
alpar@100
|
396 |
};
|
kpeter@244
|
397 |
///\brief \ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
398 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
|
alpar@100
|
399 |
///
|
kpeter@244
|
400 |
///\ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
401 |
///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>.
|
kpeter@244
|
402 |
///If you don't set it explicitly, it will be automatically allocated.
|
kpeter@257
|
403 |
struct SetStandardProcessedMap
|
kpeter@257
|
404 |
: public Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > {
|
kpeter@257
|
405 |
typedef Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits >
|
alpar@210
|
406 |
Create;
|
alpar@100
|
407 |
};
|
alpar@100
|
408 |
|
alpar@100
|
409 |
template <class H, class CR>
|
kpeter@257
|
410 |
struct SetHeapTraits : public Traits {
|
alpar@100
|
411 |
typedef CR HeapCrossRef;
|
alpar@100
|
412 |
typedef H Heap;
|
alpar@100
|
413 |
static HeapCrossRef *createHeapCrossRef(const Digraph &) {
|
alpar@209
|
414 |
throw UninitializedParameter();
|
alpar@100
|
415 |
}
|
alpar@209
|
416 |
static Heap *createHeap(HeapCrossRef &)
|
alpar@100
|
417 |
{
|
alpar@209
|
418 |
throw UninitializedParameter();
|
alpar@100
|
419 |
}
|
alpar@100
|
420 |
};
|
alpar@100
|
421 |
///\brief \ref named-templ-param "Named parameter" for setting
|
alpar@100
|
422 |
///heap and cross reference type
|
alpar@100
|
423 |
///
|
alpar@209
|
424 |
///\ref named-templ-param "Named parameter" for setting heap and cross
|
kpeter@244
|
425 |
///reference type.
|
alpar@100
|
426 |
template <class H, class CR = typename Digraph::template NodeMap<int> >
|
kpeter@257
|
427 |
struct SetHeap
|
kpeter@257
|
428 |
: public Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > {
|
kpeter@257
|
429 |
typedef Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > Create;
|
alpar@100
|
430 |
};
|
alpar@100
|
431 |
|
alpar@100
|
432 |
template <class H, class CR>
|
kpeter@257
|
433 |
struct SetStandardHeapTraits : public Traits {
|
alpar@100
|
434 |
typedef CR HeapCrossRef;
|
alpar@100
|
435 |
typedef H Heap;
|
alpar@100
|
436 |
static HeapCrossRef *createHeapCrossRef(const Digraph &G) {
|
alpar@209
|
437 |
return new HeapCrossRef(G);
|
alpar@100
|
438 |
}
|
alpar@209
|
439 |
static Heap *createHeap(HeapCrossRef &R)
|
alpar@100
|
440 |
{
|
alpar@209
|
441 |
return new Heap(R);
|
alpar@100
|
442 |
}
|
alpar@100
|
443 |
};
|
alpar@100
|
444 |
///\brief \ref named-templ-param "Named parameter" for setting
|
alpar@100
|
445 |
///heap and cross reference type with automatic allocation
|
alpar@100
|
446 |
///
|
alpar@209
|
447 |
///\ref named-templ-param "Named parameter" for setting heap and cross
|
alpar@209
|
448 |
///reference type. It can allocate the heap and the cross reference
|
alpar@209
|
449 |
///object if the cross reference's constructor waits for the digraph as
|
alpar@100
|
450 |
///parameter and the heap's constructor waits for the cross reference.
|
alpar@100
|
451 |
template <class H, class CR = typename Digraph::template NodeMap<int> >
|
kpeter@257
|
452 |
struct SetStandardHeap
|
kpeter@257
|
453 |
: public Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > {
|
kpeter@257
|
454 |
typedef Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> >
|
alpar@100
|
455 |
Create;
|
alpar@100
|
456 |
};
|
alpar@100
|
457 |
|
alpar@100
|
458 |
template <class T>
|
kpeter@257
|
459 |
struct SetOperationTraitsTraits : public Traits {
|
alpar@100
|
460 |
typedef T OperationTraits;
|
alpar@100
|
461 |
};
|
alpar@209
|
462 |
|
alpar@209
|
463 |
/// \brief \ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
464 |
///\ref OperationTraits type
|
alpar@100
|
465 |
///
|
kpeter@244
|
466 |
///\ref named-templ-param "Named parameter" for setting
|
kpeter@244
|
467 |
///\ref OperationTraits type.
|
alpar@100
|
468 |
template <class T>
|
kpeter@257
|
469 |
struct SetOperationTraits
|
kpeter@257
|
470 |
: public Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > {
|
kpeter@257
|
471 |
typedef Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> >
|
alpar@100
|
472 |
Create;
|
alpar@100
|
473 |
};
|
alpar@209
|
474 |
|
alpar@100
|
475 |
///@}
|
alpar@100
|
476 |
|
alpar@100
|
477 |
protected:
|
alpar@100
|
478 |
|
alpar@100
|
479 |
Dijkstra() {}
|
alpar@100
|
480 |
|
alpar@209
|
481 |
public:
|
alpar@209
|
482 |
|
alpar@100
|
483 |
///Constructor.
|
alpar@209
|
484 |
|
kpeter@244
|
485 |
///Constructor.
|
kpeter@244
|
486 |
///\param _g The digraph the algorithm runs on.
|
kpeter@244
|
487 |
///\param _length The length map used by the algorithm.
|
kpeter@244
|
488 |
Dijkstra(const Digraph& _g, const LengthMap& _length) :
|
kpeter@244
|
489 |
G(&_g), length(&_length),
|
alpar@100
|
490 |
_pred(NULL), local_pred(false),
|
alpar@100
|
491 |
_dist(NULL), local_dist(false),
|
alpar@100
|
492 |
_processed(NULL), local_processed(false),
|
alpar@100
|
493 |
_heap_cross_ref(NULL), local_heap_cross_ref(false),
|
alpar@100
|
494 |
_heap(NULL), local_heap(false)
|
alpar@100
|
495 |
{ }
|
alpar@209
|
496 |
|
alpar@100
|
497 |
///Destructor.
|
alpar@209
|
498 |
~Dijkstra()
|
alpar@100
|
499 |
{
|
alpar@100
|
500 |
if(local_pred) delete _pred;
|
alpar@100
|
501 |
if(local_dist) delete _dist;
|
alpar@100
|
502 |
if(local_processed) delete _processed;
|
alpar@100
|
503 |
if(local_heap_cross_ref) delete _heap_cross_ref;
|
alpar@100
|
504 |
if(local_heap) delete _heap;
|
alpar@100
|
505 |
}
|
alpar@100
|
506 |
|
alpar@100
|
507 |
///Sets the length map.
|
alpar@100
|
508 |
|
alpar@100
|
509 |
///Sets the length map.
|
alpar@100
|
510 |
///\return <tt> (*this) </tt>
|
alpar@209
|
511 |
Dijkstra &lengthMap(const LengthMap &m)
|
alpar@100
|
512 |
{
|
alpar@100
|
513 |
length = &m;
|
alpar@100
|
514 |
return *this;
|
alpar@100
|
515 |
}
|
alpar@100
|
516 |
|
kpeter@244
|
517 |
///Sets the map that stores the predecessor arcs.
|
alpar@100
|
518 |
|
kpeter@244
|
519 |
///Sets the map that stores the predecessor arcs.
|
alpar@100
|
520 |
///If you don't use this function before calling \ref run(),
|
kpeter@244
|
521 |
///it will allocate one. The destructor deallocates this
|
alpar@100
|
522 |
///automatically allocated map, of course.
|
alpar@100
|
523 |
///\return <tt> (*this) </tt>
|
alpar@209
|
524 |
Dijkstra &predMap(PredMap &m)
|
alpar@100
|
525 |
{
|
alpar@100
|
526 |
if(local_pred) {
|
alpar@209
|
527 |
delete _pred;
|
alpar@209
|
528 |
local_pred=false;
|
alpar@100
|
529 |
}
|
alpar@100
|
530 |
_pred = &m;
|
alpar@100
|
531 |
return *this;
|
alpar@100
|
532 |
}
|
alpar@100
|
533 |
|
kpeter@244
|
534 |
///Sets the map that indicates which nodes are processed.
|
alpar@100
|
535 |
|
kpeter@244
|
536 |
///Sets the map that indicates which nodes are processed.
|
alpar@100
|
537 |
///If you don't use this function before calling \ref run(),
|
kpeter@244
|
538 |
///it will allocate one. The destructor deallocates this
|
kpeter@244
|
539 |
///automatically allocated map, of course.
|
kpeter@244
|
540 |
///\return <tt> (*this) </tt>
|
kpeter@244
|
541 |
Dijkstra &processedMap(ProcessedMap &m)
|
kpeter@244
|
542 |
{
|
kpeter@244
|
543 |
if(local_processed) {
|
kpeter@244
|
544 |
delete _processed;
|
kpeter@244
|
545 |
local_processed=false;
|
kpeter@244
|
546 |
}
|
kpeter@244
|
547 |
_processed = &m;
|
kpeter@244
|
548 |
return *this;
|
kpeter@244
|
549 |
}
|
kpeter@244
|
550 |
|
kpeter@244
|
551 |
///Sets the map that stores the distances of the nodes.
|
kpeter@244
|
552 |
|
kpeter@244
|
553 |
///Sets the map that stores the distances of the nodes calculated by the
|
kpeter@244
|
554 |
///algorithm.
|
kpeter@244
|
555 |
///If you don't use this function before calling \ref run(),
|
kpeter@244
|
556 |
///it will allocate one. The destructor deallocates this
|
alpar@100
|
557 |
///automatically allocated map, of course.
|
alpar@100
|
558 |
///\return <tt> (*this) </tt>
|
alpar@209
|
559 |
Dijkstra &distMap(DistMap &m)
|
alpar@100
|
560 |
{
|
alpar@100
|
561 |
if(local_dist) {
|
alpar@209
|
562 |
delete _dist;
|
alpar@209
|
563 |
local_dist=false;
|
alpar@100
|
564 |
}
|
alpar@100
|
565 |
_dist = &m;
|
alpar@100
|
566 |
return *this;
|
alpar@100
|
567 |
}
|
alpar@100
|
568 |
|
alpar@100
|
569 |
///Sets the heap and the cross reference used by algorithm.
|
alpar@100
|
570 |
|
alpar@100
|
571 |
///Sets the heap and the cross reference used by algorithm.
|
alpar@100
|
572 |
///If you don't use this function before calling \ref run(),
|
kpeter@244
|
573 |
///it will allocate one. The destructor deallocates this
|
alpar@100
|
574 |
///automatically allocated heap and cross reference, of course.
|
alpar@100
|
575 |
///\return <tt> (*this) </tt>
|
alpar@100
|
576 |
Dijkstra &heap(Heap& hp, HeapCrossRef &cr)
|
alpar@100
|
577 |
{
|
alpar@100
|
578 |
if(local_heap_cross_ref) {
|
alpar@209
|
579 |
delete _heap_cross_ref;
|
alpar@209
|
580 |
local_heap_cross_ref=false;
|
alpar@100
|
581 |
}
|
alpar@100
|
582 |
_heap_cross_ref = &cr;
|
alpar@100
|
583 |
if(local_heap) {
|
alpar@209
|
584 |
delete _heap;
|
alpar@209
|
585 |
local_heap=false;
|
alpar@100
|
586 |
}
|
alpar@100
|
587 |
_heap = &hp;
|
alpar@100
|
588 |
return *this;
|
alpar@100
|
589 |
}
|
alpar@100
|
590 |
|
alpar@100
|
591 |
private:
|
kpeter@244
|
592 |
|
alpar@100
|
593 |
void finalizeNodeData(Node v,Value dst)
|
alpar@100
|
594 |
{
|
alpar@100
|
595 |
_processed->set(v,true);
|
alpar@100
|
596 |
_dist->set(v, dst);
|
alpar@100
|
597 |
}
|
alpar@100
|
598 |
|
alpar@100
|
599 |
public:
|
alpar@100
|
600 |
|
alpar@100
|
601 |
///\name Execution control
|
kpeter@244
|
602 |
///The simplest way to execute the algorithm is to use one of the
|
kpeter@244
|
603 |
///member functions called \ref lemon::Dijkstra::run() "run()".
|
alpar@100
|
604 |
///\n
|
kpeter@244
|
605 |
///If you need more control on the execution, first you must call
|
kpeter@244
|
606 |
///\ref lemon::Dijkstra::init() "init()", then you can add several
|
kpeter@244
|
607 |
///source nodes with \ref lemon::Dijkstra::addSource() "addSource()".
|
kpeter@244
|
608 |
///Finally \ref lemon::Dijkstra::start() "start()" will perform the
|
kpeter@244
|
609 |
///actual path computation.
|
alpar@100
|
610 |
|
alpar@100
|
611 |
///@{
|
alpar@100
|
612 |
|
alpar@100
|
613 |
///Initializes the internal data structures.
|
alpar@100
|
614 |
|
alpar@100
|
615 |
///Initializes the internal data structures.
|
alpar@100
|
616 |
///
|
alpar@100
|
617 |
void init()
|
alpar@100
|
618 |
{
|
alpar@100
|
619 |
create_maps();
|
alpar@100
|
620 |
_heap->clear();
|
alpar@100
|
621 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
|
alpar@209
|
622 |
_pred->set(u,INVALID);
|
alpar@209
|
623 |
_processed->set(u,false);
|
alpar@209
|
624 |
_heap_cross_ref->set(u,Heap::PRE_HEAP);
|
alpar@100
|
625 |
}
|
alpar@100
|
626 |
}
|
alpar@209
|
627 |
|
alpar@100
|
628 |
///Adds a new source node.
|
alpar@100
|
629 |
|
alpar@100
|
630 |
///Adds a new source node to the priority heap.
|
alpar@100
|
631 |
///The optional second parameter is the initial distance of the node.
|
alpar@100
|
632 |
///
|
kpeter@244
|
633 |
///The function checks if the node has already been added to the heap and
|
alpar@100
|
634 |
///it is pushed to the heap only if either it was not in the heap
|
alpar@100
|
635 |
///or the shortest path found till then is shorter than \c dst.
|
alpar@100
|
636 |
void addSource(Node s,Value dst=OperationTraits::zero())
|
alpar@100
|
637 |
{
|
alpar@100
|
638 |
if(_heap->state(s) != Heap::IN_HEAP) {
|
alpar@209
|
639 |
_heap->push(s,dst);
|
alpar@100
|
640 |
} else if(OperationTraits::less((*_heap)[s], dst)) {
|
alpar@209
|
641 |
_heap->set(s,dst);
|
alpar@209
|
642 |
_pred->set(s,INVALID);
|
alpar@100
|
643 |
}
|
alpar@100
|
644 |
}
|
alpar@209
|
645 |
|
alpar@100
|
646 |
///Processes the next node in the priority heap
|
alpar@100
|
647 |
|
alpar@100
|
648 |
///Processes the next node in the priority heap.
|
alpar@100
|
649 |
///
|
alpar@100
|
650 |
///\return The processed node.
|
alpar@100
|
651 |
///
|
kpeter@244
|
652 |
///\warning The priority heap must not be empty.
|
alpar@100
|
653 |
Node processNextNode()
|
alpar@100
|
654 |
{
|
alpar@209
|
655 |
Node v=_heap->top();
|
alpar@100
|
656 |
Value oldvalue=_heap->prio();
|
alpar@100
|
657 |
_heap->pop();
|
alpar@100
|
658 |
finalizeNodeData(v,oldvalue);
|
alpar@209
|
659 |
|
alpar@100
|
660 |
for(OutArcIt e(*G,v); e!=INVALID; ++e) {
|
alpar@209
|
661 |
Node w=G->target(e);
|
alpar@209
|
662 |
switch(_heap->state(w)) {
|
alpar@209
|
663 |
case Heap::PRE_HEAP:
|
alpar@209
|
664 |
_heap->push(w,OperationTraits::plus(oldvalue, (*length)[e]));
|
alpar@209
|
665 |
_pred->set(w,e);
|
alpar@209
|
666 |
break;
|
alpar@209
|
667 |
case Heap::IN_HEAP:
|
alpar@209
|
668 |
{
|
alpar@209
|
669 |
Value newvalue = OperationTraits::plus(oldvalue, (*length)[e]);
|
alpar@209
|
670 |
if ( OperationTraits::less(newvalue, (*_heap)[w]) ) {
|
alpar@209
|
671 |
_heap->decrease(w, newvalue);
|
alpar@209
|
672 |
_pred->set(w,e);
|
alpar@209
|
673 |
}
|
alpar@209
|
674 |
}
|
alpar@209
|
675 |
break;
|
alpar@209
|
676 |
case Heap::POST_HEAP:
|
alpar@209
|
677 |
break;
|
alpar@209
|
678 |
}
|
alpar@100
|
679 |
}
|
alpar@100
|
680 |
return v;
|
alpar@100
|
681 |
}
|
alpar@100
|
682 |
|
kpeter@244
|
683 |
///The next node to be processed.
|
alpar@209
|
684 |
|
kpeter@244
|
685 |
///Returns the next node to be processed or \c INVALID if the
|
kpeter@244
|
686 |
///priority heap is empty.
|
kpeter@244
|
687 |
Node nextNode() const
|
alpar@209
|
688 |
{
|
alpar@100
|
689 |
return !_heap->empty()?_heap->top():INVALID;
|
alpar@100
|
690 |
}
|
alpar@209
|
691 |
|
alpar@100
|
692 |
///\brief Returns \c false if there are nodes
|
kpeter@244
|
693 |
///to be processed.
|
alpar@100
|
694 |
///
|
alpar@100
|
695 |
///Returns \c false if there are nodes
|
kpeter@244
|
696 |
///to be processed in the priority heap.
|
kpeter@244
|
697 |
bool emptyQueue() const { return _heap->empty(); }
|
kpeter@244
|
698 |
|
alpar@100
|
699 |
///Returns the number of the nodes to be processed in the priority heap
|
alpar@100
|
700 |
|
kpeter@244
|
701 |
///Returns the number of the nodes to be processed in the priority heap.
|
alpar@100
|
702 |
///
|
kpeter@244
|
703 |
int queueSize() const { return _heap->size(); }
|
alpar@209
|
704 |
|
alpar@100
|
705 |
///Executes the algorithm.
|
alpar@100
|
706 |
|
alpar@100
|
707 |
///Executes the algorithm.
|
alpar@100
|
708 |
///
|
kpeter@244
|
709 |
///This method runs the %Dijkstra algorithm from the root node(s)
|
kpeter@244
|
710 |
///in order to compute the shortest path to each node.
|
kpeter@244
|
711 |
///
|
kpeter@244
|
712 |
///The algorithm computes
|
kpeter@244
|
713 |
///- the shortest path tree (forest),
|
kpeter@244
|
714 |
///- the distance of each node from the root(s).
|
kpeter@244
|
715 |
///
|
kpeter@244
|
716 |
///\pre init() must be called and at least one root node should be
|
kpeter@244
|
717 |
///added with addSource() before using this function.
|
kpeter@244
|
718 |
///
|
kpeter@244
|
719 |
///\note <tt>d.start()</tt> is just a shortcut of the following code.
|
kpeter@244
|
720 |
///\code
|
kpeter@244
|
721 |
/// while ( !d.emptyQueue() ) {
|
kpeter@244
|
722 |
/// d.processNextNode();
|
kpeter@244
|
723 |
/// }
|
kpeter@244
|
724 |
///\endcode
|
kpeter@244
|
725 |
void start()
|
kpeter@244
|
726 |
{
|
kpeter@244
|
727 |
while ( !emptyQueue() ) processNextNode();
|
kpeter@244
|
728 |
}
|
kpeter@244
|
729 |
|
kpeter@244
|
730 |
///Executes the algorithm until the given target node is reached.
|
kpeter@244
|
731 |
|
kpeter@244
|
732 |
///Executes the algorithm until the given target node is reached.
|
alpar@100
|
733 |
///
|
alpar@100
|
734 |
///This method runs the %Dijkstra algorithm from the root node(s)
|
kpeter@244
|
735 |
///in order to compute the shortest path to \c dest.
|
alpar@100
|
736 |
///
|
kpeter@244
|
737 |
///The algorithm computes
|
kpeter@244
|
738 |
///- the shortest path to \c dest,
|
kpeter@244
|
739 |
///- the distance of \c dest from the root(s).
|
alpar@100
|
740 |
///
|
kpeter@244
|
741 |
///\pre init() must be called and at least one root node should be
|
kpeter@244
|
742 |
///added with addSource() before using this function.
|
alpar@100
|
743 |
void start(Node dest)
|
alpar@100
|
744 |
{
|
alpar@100
|
745 |
while ( !_heap->empty() && _heap->top()!=dest ) processNextNode();
|
alpar@100
|
746 |
if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio());
|
alpar@100
|
747 |
}
|
alpar@209
|
748 |
|
alpar@100
|
749 |
///Executes the algorithm until a condition is met.
|
alpar@100
|
750 |
|
alpar@100
|
751 |
///Executes the algorithm until a condition is met.
|
alpar@100
|
752 |
///
|
kpeter@244
|
753 |
///This method runs the %Dijkstra algorithm from the root node(s) in
|
kpeter@244
|
754 |
///order to compute the shortest path to a node \c v with
|
kpeter@244
|
755 |
/// <tt>nm[v]</tt> true, if such a node can be found.
|
alpar@100
|
756 |
///
|
kpeter@244
|
757 |
///\param nm A \c bool (or convertible) node map. The algorithm
|
alpar@100
|
758 |
///will stop when it reaches a node \c v with <tt>nm[v]</tt> true.
|
alpar@100
|
759 |
///
|
alpar@100
|
760 |
///\return The reached node \c v with <tt>nm[v]</tt> true or
|
alpar@100
|
761 |
///\c INVALID if no such node was found.
|
kpeter@244
|
762 |
///
|
kpeter@244
|
763 |
///\pre init() must be called and at least one root node should be
|
kpeter@244
|
764 |
///added with addSource() before using this function.
|
alpar@100
|
765 |
template<class NodeBoolMap>
|
alpar@100
|
766 |
Node start(const NodeBoolMap &nm)
|
alpar@100
|
767 |
{
|
alpar@100
|
768 |
while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode();
|
alpar@100
|
769 |
if ( _heap->empty() ) return INVALID;
|
alpar@100
|
770 |
finalizeNodeData(_heap->top(),_heap->prio());
|
alpar@100
|
771 |
return _heap->top();
|
alpar@100
|
772 |
}
|
alpar@209
|
773 |
|
kpeter@244
|
774 |
///Runs the algorithm from the given node.
|
alpar@209
|
775 |
|
kpeter@244
|
776 |
///This method runs the %Dijkstra algorithm from node \c s
|
kpeter@244
|
777 |
///in order to compute the shortest path to each node.
|
alpar@100
|
778 |
///
|
kpeter@244
|
779 |
///The algorithm computes
|
kpeter@244
|
780 |
///- the shortest path tree,
|
kpeter@244
|
781 |
///- the distance of each node from the root.
|
kpeter@244
|
782 |
///
|
kpeter@244
|
783 |
///\note <tt>d.run(s)</tt> is just a shortcut of the following code.
|
alpar@100
|
784 |
///\code
|
alpar@100
|
785 |
/// d.init();
|
alpar@100
|
786 |
/// d.addSource(s);
|
alpar@100
|
787 |
/// d.start();
|
alpar@100
|
788 |
///\endcode
|
alpar@100
|
789 |
void run(Node s) {
|
alpar@100
|
790 |
init();
|
alpar@100
|
791 |
addSource(s);
|
alpar@100
|
792 |
start();
|
alpar@100
|
793 |
}
|
alpar@209
|
794 |
|
alpar@100
|
795 |
///Finds the shortest path between \c s and \c t.
|
alpar@209
|
796 |
|
kpeter@244
|
797 |
///This method runs the %Dijkstra algorithm from node \c s
|
kpeter@244
|
798 |
///in order to compute the shortest path to \c t.
|
alpar@100
|
799 |
///
|
kpeter@244
|
800 |
///\return The length of the shortest <tt>s</tt>--<tt>t</tt> path,
|
kpeter@244
|
801 |
///if \c t is reachable form \c s, \c 0 otherwise.
|
kpeter@244
|
802 |
///
|
kpeter@244
|
803 |
///\note Apart from the return value, <tt>d.run(s,t)</tt> is just a
|
kpeter@244
|
804 |
///shortcut of the following code.
|
alpar@100
|
805 |
///\code
|
alpar@100
|
806 |
/// d.init();
|
alpar@100
|
807 |
/// d.addSource(s);
|
alpar@100
|
808 |
/// d.start(t);
|
alpar@100
|
809 |
///\endcode
|
alpar@100
|
810 |
Value run(Node s,Node t) {
|
alpar@100
|
811 |
init();
|
alpar@100
|
812 |
addSource(s);
|
alpar@100
|
813 |
start(t);
|
alpar@100
|
814 |
return (*_pred)[t]==INVALID?OperationTraits::zero():(*_dist)[t];
|
alpar@100
|
815 |
}
|
alpar@209
|
816 |
|
alpar@100
|
817 |
///@}
|
alpar@100
|
818 |
|
alpar@100
|
819 |
///\name Query Functions
|
alpar@100
|
820 |
///The result of the %Dijkstra algorithm can be obtained using these
|
alpar@100
|
821 |
///functions.\n
|
kpeter@244
|
822 |
///Either \ref lemon::Dijkstra::run() "run()" or
|
kpeter@244
|
823 |
///\ref lemon::Dijkstra::start() "start()" must be called before
|
kpeter@244
|
824 |
///using them.
|
alpar@209
|
825 |
|
alpar@100
|
826 |
///@{
|
alpar@100
|
827 |
|
kpeter@244
|
828 |
///The shortest path to a node.
|
alpar@209
|
829 |
|
kpeter@244
|
830 |
///Returns the shortest path to a node.
|
kpeter@244
|
831 |
///
|
kpeter@244
|
832 |
///\warning \c t should be reachable from the root(s).
|
kpeter@244
|
833 |
///
|
kpeter@244
|
834 |
///\pre Either \ref run() or \ref start() must be called before
|
kpeter@244
|
835 |
///using this function.
|
kpeter@244
|
836 |
Path path(Node t) const { return Path(*G, *_pred, t); }
|
alpar@100
|
837 |
|
kpeter@244
|
838 |
///The distance of a node from the root(s).
|
alpar@100
|
839 |
|
kpeter@244
|
840 |
///Returns the distance of a node from the root(s).
|
kpeter@244
|
841 |
///
|
kpeter@244
|
842 |
///\warning If node \c v is not reachable from the root(s), then
|
kpeter@244
|
843 |
///the return value of this function is undefined.
|
kpeter@244
|
844 |
///
|
kpeter@244
|
845 |
///\pre Either \ref run() or \ref start() must be called before
|
kpeter@244
|
846 |
///using this function.
|
alpar@100
|
847 |
Value dist(Node v) const { return (*_dist)[v]; }
|
alpar@100
|
848 |
|
kpeter@244
|
849 |
///Returns the 'previous arc' of the shortest path tree for a node.
|
alpar@100
|
850 |
|
kpeter@244
|
851 |
///This function returns the 'previous arc' of the shortest path
|
kpeter@244
|
852 |
///tree for the node \c v, i.e. it returns the last arc of a
|
kpeter@244
|
853 |
///shortest path from the root(s) to \c v. It is \c INVALID if \c v
|
kpeter@244
|
854 |
///is not reachable from the root(s) or if \c v is a root.
|
kpeter@244
|
855 |
///
|
kpeter@244
|
856 |
///The shortest path tree used here is equal to the shortest path
|
kpeter@244
|
857 |
///tree used in \ref predNode().
|
kpeter@244
|
858 |
///
|
kpeter@244
|
859 |
///\pre Either \ref run() or \ref start() must be called before
|
kpeter@244
|
860 |
///using this function.
|
alpar@100
|
861 |
Arc predArc(Node v) const { return (*_pred)[v]; }
|
alpar@100
|
862 |
|
kpeter@244
|
863 |
///Returns the 'previous node' of the shortest path tree for a node.
|
alpar@100
|
864 |
|
kpeter@244
|
865 |
///This function returns the 'previous node' of the shortest path
|
kpeter@244
|
866 |
///tree for the node \c v, i.e. it returns the last but one node
|
kpeter@244
|
867 |
///from a shortest path from the root(s) to \c v. It is \c INVALID
|
kpeter@244
|
868 |
///if \c v is not reachable from the root(s) or if \c v is a root.
|
kpeter@244
|
869 |
///
|
kpeter@244
|
870 |
///The shortest path tree used here is equal to the shortest path
|
kpeter@244
|
871 |
///tree used in \ref predArc().
|
kpeter@244
|
872 |
///
|
kpeter@244
|
873 |
///\pre Either \ref run() or \ref start() must be called before
|
alpar@100
|
874 |
///using this function.
|
alpar@100
|
875 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
|
alpar@209
|
876 |
G->source((*_pred)[v]); }
|
alpar@209
|
877 |
|
kpeter@244
|
878 |
///\brief Returns a const reference to the node map that stores the
|
kpeter@244
|
879 |
///distances of the nodes.
|
kpeter@244
|
880 |
///
|
kpeter@244
|
881 |
///Returns a const reference to the node map that stores the distances
|
kpeter@244
|
882 |
///of the nodes calculated by the algorithm.
|
kpeter@244
|
883 |
///
|
kpeter@244
|
884 |
///\pre Either \ref run() or \ref init()
|
kpeter@244
|
885 |
///must be called before using this function.
|
alpar@100
|
886 |
const DistMap &distMap() const { return *_dist;}
|
alpar@209
|
887 |
|
kpeter@244
|
888 |
///\brief Returns a const reference to the node map that stores the
|
kpeter@244
|
889 |
///predecessor arcs.
|
kpeter@244
|
890 |
///
|
kpeter@244
|
891 |
///Returns a const reference to the node map that stores the predecessor
|
kpeter@244
|
892 |
///arcs, which form the shortest path tree.
|
kpeter@244
|
893 |
///
|
kpeter@244
|
894 |
///\pre Either \ref run() or \ref init()
|
kpeter@244
|
895 |
///must be called before using this function.
|
alpar@100
|
896 |
const PredMap &predMap() const { return *_pred;}
|
alpar@209
|
897 |
|
kpeter@244
|
898 |
///Checks if a node is reachable from the root(s).
|
alpar@100
|
899 |
|
kpeter@244
|
900 |
///Returns \c true if \c v is reachable from the root(s).
|
kpeter@244
|
901 |
///\pre Either \ref run() or \ref start()
|
kpeter@244
|
902 |
///must be called before using this function.
|
kpeter@244
|
903 |
bool reached(Node v) const { return (*_heap_cross_ref)[v] !=
|
kpeter@244
|
904 |
Heap::PRE_HEAP; }
|
alpar@100
|
905 |
|
alpar@100
|
906 |
///Checks if a node is processed.
|
alpar@100
|
907 |
|
alpar@100
|
908 |
///Returns \c true if \c v is processed, i.e. the shortest
|
alpar@100
|
909 |
///path to \c v has already found.
|
kpeter@244
|
910 |
///\pre Either \ref run() or \ref start()
|
kpeter@244
|
911 |
///must be called before using this function.
|
kpeter@244
|
912 |
bool processed(Node v) const { return (*_heap_cross_ref)[v] ==
|
kpeter@244
|
913 |
Heap::POST_HEAP; }
|
kpeter@244
|
914 |
|
kpeter@244
|
915 |
///The current distance of a node from the root(s).
|
kpeter@244
|
916 |
|
kpeter@244
|
917 |
///Returns the current distance of a node from the root(s).
|
kpeter@244
|
918 |
///It may be decreased in the following processes.
|
kpeter@244
|
919 |
///\pre \c v should be reached but not processed.
|
kpeter@244
|
920 |
Value currentDist(Node v) const { return (*_heap)[v]; }
|
alpar@209
|
921 |
|
alpar@100
|
922 |
///@}
|
alpar@100
|
923 |
};
|
alpar@100
|
924 |
|
alpar@100
|
925 |
|
kpeter@244
|
926 |
///Default traits class of dijkstra() function.
|
alpar@100
|
927 |
|
kpeter@244
|
928 |
///Default traits class of dijkstra() function.
|
kpeter@244
|
929 |
///\tparam GR The type of the digraph.
|
kpeter@244
|
930 |
///\tparam LM The type of the length map.
|
alpar@100
|
931 |
template<class GR, class LM>
|
alpar@100
|
932 |
struct DijkstraWizardDefaultTraits
|
alpar@100
|
933 |
{
|
kpeter@244
|
934 |
///The type of the digraph the algorithm runs on.
|
alpar@100
|
935 |
typedef GR Digraph;
|
alpar@100
|
936 |
///The type of the map that stores the arc lengths.
|
alpar@100
|
937 |
|
alpar@100
|
938 |
///The type of the map that stores the arc lengths.
|
alpar@100
|
939 |
///It must meet the \ref concepts::ReadMap "ReadMap" concept.
|
alpar@100
|
940 |
typedef LM LengthMap;
|
kpeter@244
|
941 |
///The type of the length of the arcs.
|
alpar@100
|
942 |
typedef typename LM::Value Value;
|
kpeter@244
|
943 |
|
alpar@100
|
944 |
/// Operation traits for Dijkstra algorithm.
|
alpar@100
|
945 |
|
kpeter@244
|
946 |
/// This class defines the operations that are used in the algorithm.
|
alpar@100
|
947 |
/// \see DijkstraDefaultOperationTraits
|
alpar@100
|
948 |
typedef DijkstraDefaultOperationTraits<Value> OperationTraits;
|
alpar@100
|
949 |
|
kpeter@244
|
950 |
/// The cross reference type used by the heap.
|
alpar@100
|
951 |
|
kpeter@244
|
952 |
/// The cross reference type used by the heap.
|
alpar@100
|
953 |
/// Usually it is \c Digraph::NodeMap<int>.
|
alpar@100
|
954 |
typedef typename Digraph::template NodeMap<int> HeapCrossRef;
|
kpeter@244
|
955 |
///Instantiates a \ref HeapCrossRef.
|
alpar@100
|
956 |
|
alpar@209
|
957 |
///This function instantiates a \ref HeapCrossRef.
|
kpeter@244
|
958 |
/// \param g is the digraph, to which we would like to define the
|
alpar@100
|
959 |
/// HeapCrossRef.
|
alpar@100
|
960 |
/// \todo The digraph alone may be insufficient for the initialization
|
kpeter@244
|
961 |
static HeapCrossRef *createHeapCrossRef(const Digraph &g)
|
alpar@100
|
962 |
{
|
kpeter@244
|
963 |
return new HeapCrossRef(g);
|
alpar@100
|
964 |
}
|
alpar@209
|
965 |
|
kpeter@244
|
966 |
///The heap type used by the Dijkstra algorithm.
|
alpar@100
|
967 |
|
kpeter@244
|
968 |
///The heap type used by the Dijkstra algorithm.
|
alpar@100
|
969 |
///
|
alpar@100
|
970 |
///\sa BinHeap
|
alpar@100
|
971 |
///\sa Dijkstra
|
kpeter@244
|
972 |
typedef BinHeap<Value, typename Digraph::template NodeMap<int>,
|
alpar@209
|
973 |
std::less<Value> > Heap;
|
alpar@100
|
974 |
|
kpeter@244
|
975 |
///Instantiates a \ref Heap.
|
kpeter@244
|
976 |
|
kpeter@244
|
977 |
///This function instantiates a \ref Heap.
|
kpeter@244
|
978 |
/// \param r is the HeapCrossRef which is used.
|
kpeter@244
|
979 |
static Heap *createHeap(HeapCrossRef& r)
|
alpar@100
|
980 |
{
|
kpeter@244
|
981 |
return new Heap(r);
|
alpar@100
|
982 |
}
|
alpar@100
|
983 |
|
kpeter@244
|
984 |
///\brief The type of the map that stores the predecessor
|
alpar@100
|
985 |
///arcs of the shortest paths.
|
alpar@209
|
986 |
///
|
kpeter@244
|
987 |
///The type of the map that stores the predecessor
|
alpar@100
|
988 |
///arcs of the shortest paths.
|
alpar@100
|
989 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept.
|
kpeter@244
|
990 |
typedef NullMap <typename Digraph::Node,typename Digraph::Arc> PredMap;
|
kpeter@244
|
991 |
///Instantiates a \ref PredMap.
|
alpar@209
|
992 |
|
alpar@209
|
993 |
///This function instantiates a \ref PredMap.
|
kpeter@244
|
994 |
///\param g is the digraph, to which we would like to define the
|
kpeter@244
|
995 |
///\ref PredMap.
|
kpeter@244
|
996 |
///\todo The digraph alone may be insufficient to initialize
|
alpar@100
|
997 |
#ifdef DOXYGEN
|
kpeter@244
|
998 |
static PredMap *createPredMap(const Digraph &g)
|
alpar@100
|
999 |
#else
|
kpeter@244
|
1000 |
static PredMap *createPredMap(const Digraph &)
|
alpar@100
|
1001 |
#endif
|
alpar@100
|
1002 |
{
|
alpar@100
|
1003 |
return new PredMap();
|
alpar@100
|
1004 |
}
|
alpar@209
|
1005 |
|
kpeter@244
|
1006 |
///The type of the map that indicates which nodes are processed.
|
kpeter@244
|
1007 |
|
kpeter@244
|
1008 |
///The type of the map that indicates which nodes are processed.
|
alpar@100
|
1009 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept.
|
alpar@100
|
1010 |
///By default it is a NullMap.
|
alpar@100
|
1011 |
///\todo If it is set to a real map,
|
alpar@100
|
1012 |
///Dijkstra::processed() should read this.
|
alpar@100
|
1013 |
///\todo named parameter to set this type, function to read and write.
|
alpar@100
|
1014 |
typedef NullMap<typename Digraph::Node,bool> ProcessedMap;
|
kpeter@244
|
1015 |
///Instantiates a \ref ProcessedMap.
|
alpar@209
|
1016 |
|
alpar@209
|
1017 |
///This function instantiates a \ref ProcessedMap.
|
alpar@100
|
1018 |
///\param g is the digraph, to which
|
kpeter@244
|
1019 |
///we would like to define the \ref ProcessedMap.
|
alpar@100
|
1020 |
#ifdef DOXYGEN
|
kpeter@244
|
1021 |
static ProcessedMap *createProcessedMap(const Digraph &g)
|
alpar@100
|
1022 |
#else
|
kpeter@244
|
1023 |
static ProcessedMap *createProcessedMap(const Digraph &)
|
alpar@100
|
1024 |
#endif
|
alpar@100
|
1025 |
{
|
alpar@100
|
1026 |
return new ProcessedMap();
|
alpar@100
|
1027 |
}
|
alpar@209
|
1028 |
|
kpeter@244
|
1029 |
///The type of the map that stores the distances of the nodes.
|
kpeter@244
|
1030 |
|
kpeter@244
|
1031 |
///The type of the map that stores the distances of the nodes.
|
alpar@100
|
1032 |
///It must meet the \ref concepts::WriteMap "WriteMap" concept.
|
kpeter@244
|
1033 |
typedef NullMap<typename Digraph::Node,Value> DistMap;
|
kpeter@244
|
1034 |
///Instantiates a \ref DistMap.
|
alpar@209
|
1035 |
|
alpar@209
|
1036 |
///This function instantiates a \ref DistMap.
|
alpar@210
|
1037 |
///\param g is the digraph, to which we would like to define
|
alpar@210
|
1038 |
///the \ref DistMap
|
alpar@100
|
1039 |
#ifdef DOXYGEN
|
kpeter@244
|
1040 |
static DistMap *createDistMap(const Digraph &g)
|
alpar@100
|
1041 |
#else
|
kpeter@244
|
1042 |
static DistMap *createDistMap(const Digraph &)
|
alpar@100
|
1043 |
#endif
|
alpar@100
|
1044 |
{
|
alpar@100
|
1045 |
return new DistMap();
|
alpar@100
|
1046 |
}
|
alpar@100
|
1047 |
};
|
alpar@209
|
1048 |
|
kpeter@244
|
1049 |
/// Default traits class used by \ref DijkstraWizard
|
alpar@100
|
1050 |
|
alpar@100
|
1051 |
/// To make it easier to use Dijkstra algorithm
|
kpeter@244
|
1052 |
/// we have created a wizard class.
|
alpar@100
|
1053 |
/// This \ref DijkstraWizard class needs default traits,
|
kpeter@244
|
1054 |
/// as well as the \ref Dijkstra class.
|
alpar@100
|
1055 |
/// The \ref DijkstraWizardBase is a class to be the default traits of the
|
alpar@100
|
1056 |
/// \ref DijkstraWizard class.
|
alpar@100
|
1057 |
/// \todo More named parameters are required...
|
alpar@100
|
1058 |
template<class GR,class LM>
|
alpar@100
|
1059 |
class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
|
alpar@100
|
1060 |
{
|
alpar@100
|
1061 |
typedef DijkstraWizardDefaultTraits<GR,LM> Base;
|
alpar@100
|
1062 |
protected:
|
kpeter@244
|
1063 |
//The type of the nodes in the digraph.
|
alpar@100
|
1064 |
typedef typename Base::Digraph::Node Node;
|
alpar@100
|
1065 |
|
kpeter@244
|
1066 |
//Pointer to the digraph the algorithm runs on.
|
alpar@100
|
1067 |
void *_g;
|
kpeter@244
|
1068 |
//Pointer to the length map
|
alpar@100
|
1069 |
void *_length;
|
kpeter@251
|
1070 |
//Pointer to the map of processed nodes.
|
kpeter@251
|
1071 |
void *_processed;
|
kpeter@244
|
1072 |
//Pointer to the map of predecessors arcs.
|
alpar@100
|
1073 |
void *_pred;
|
kpeter@244
|
1074 |
//Pointer to the map of distances.
|
alpar@100
|
1075 |
void *_dist;
|
kpeter@244
|
1076 |
//Pointer to the source node.
|
alpar@100
|
1077 |
Node _source;
|
alpar@100
|
1078 |
|
kpeter@244
|
1079 |
public:
|
alpar@100
|
1080 |
/// Constructor.
|
alpar@209
|
1081 |
|
alpar@100
|
1082 |
/// This constructor does not require parameters, therefore it initiates
|
alpar@100
|
1083 |
/// all of the attributes to default values (0, INVALID).
|
kpeter@251
|
1084 |
DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0),
|
alpar@209
|
1085 |
_dist(0), _source(INVALID) {}
|
alpar@100
|
1086 |
|
alpar@100
|
1087 |
/// Constructor.
|
alpar@209
|
1088 |
|
alpar@100
|
1089 |
/// This constructor requires some parameters,
|
alpar@100
|
1090 |
/// listed in the parameters list.
|
alpar@100
|
1091 |
/// Others are initiated to 0.
|
kpeter@244
|
1092 |
/// \param g The digraph the algorithm runs on.
|
kpeter@244
|
1093 |
/// \param l The length map.
|
kpeter@244
|
1094 |
/// \param s The source node.
|
alpar@100
|
1095 |
DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
|
alpar@209
|
1096 |
_g(reinterpret_cast<void*>(const_cast<GR*>(&g))),
|
alpar@209
|
1097 |
_length(reinterpret_cast<void*>(const_cast<LM*>(&l))),
|
kpeter@251
|
1098 |
_processed(0), _pred(0), _dist(0), _source(s) {}
|
alpar@100
|
1099 |
|
alpar@100
|
1100 |
};
|
alpar@209
|
1101 |
|
kpeter@244
|
1102 |
/// Auxiliary class for the function type interface of Dijkstra algorithm.
|
alpar@100
|
1103 |
|
kpeter@244
|
1104 |
/// This auxiliary class is created to implement the function type
|
kpeter@244
|
1105 |
/// interface of \ref Dijkstra algorithm. It uses the functions and features
|
kpeter@244
|
1106 |
/// of the plain \ref Dijkstra, but it is much simpler to use it.
|
kpeter@244
|
1107 |
/// It should only be used through the \ref dijkstra() function, which makes
|
kpeter@244
|
1108 |
/// it easier to use the algorithm.
|
alpar@100
|
1109 |
///
|
alpar@100
|
1110 |
/// Simplicity means that the way to change the types defined
|
alpar@100
|
1111 |
/// in the traits class is based on functions that returns the new class
|
alpar@100
|
1112 |
/// and not on templatable built-in classes.
|
alpar@100
|
1113 |
/// When using the plain \ref Dijkstra
|
alpar@100
|
1114 |
/// the new class with the modified type comes from
|
alpar@100
|
1115 |
/// the original class by using the ::
|
alpar@100
|
1116 |
/// operator. In the case of \ref DijkstraWizard only
|
kpeter@244
|
1117 |
/// a function have to be called, and it will
|
alpar@100
|
1118 |
/// return the needed class.
|
alpar@100
|
1119 |
///
|
kpeter@244
|
1120 |
/// It does not have own \ref run() method. When its \ref run() method
|
kpeter@244
|
1121 |
/// is called, it initiates a plain \ref Dijkstra object, and calls the
|
kpeter@244
|
1122 |
/// \ref Dijkstra::run() method of it.
|
alpar@100
|
1123 |
template<class TR>
|
alpar@100
|
1124 |
class DijkstraWizard : public TR
|
alpar@100
|
1125 |
{
|
alpar@100
|
1126 |
typedef TR Base;
|
alpar@100
|
1127 |
|
kpeter@244
|
1128 |
///The type of the digraph the algorithm runs on.
|
alpar@100
|
1129 |
typedef typename TR::Digraph Digraph;
|
kpeter@244
|
1130 |
|
alpar@100
|
1131 |
typedef typename Digraph::Node Node;
|
alpar@100
|
1132 |
typedef typename Digraph::NodeIt NodeIt;
|
alpar@100
|
1133 |
typedef typename Digraph::Arc Arc;
|
alpar@100
|
1134 |
typedef typename Digraph::OutArcIt OutArcIt;
|
alpar@209
|
1135 |
|
alpar@100
|
1136 |
///The type of the map that stores the arc lengths.
|
alpar@100
|
1137 |
typedef typename TR::LengthMap LengthMap;
|
alpar@100
|
1138 |
///The type of the length of the arcs.
|
alpar@100
|
1139 |
typedef typename LengthMap::Value Value;
|
kpeter@244
|
1140 |
///\brief The type of the map that stores the predecessor
|
alpar@100
|
1141 |
///arcs of the shortest paths.
|
alpar@100
|
1142 |
typedef typename TR::PredMap PredMap;
|
kpeter@244
|
1143 |
///The type of the map that stores the distances of the nodes.
|
alpar@100
|
1144 |
typedef typename TR::DistMap DistMap;
|
kpeter@244
|
1145 |
///The type of the map that indicates which nodes are processed.
|
kpeter@244
|
1146 |
typedef typename TR::ProcessedMap ProcessedMap;
|
alpar@100
|
1147 |
///The heap type used by the dijkstra algorithm.
|
alpar@100
|
1148 |
typedef typename TR::Heap Heap;
|
kpeter@244
|
1149 |
|
alpar@100
|
1150 |
public:
|
kpeter@244
|
1151 |
|
alpar@100
|
1152 |
/// Constructor.
|
alpar@100
|
1153 |
DijkstraWizard() : TR() {}
|
alpar@100
|
1154 |
|
alpar@100
|
1155 |
/// Constructor that requires parameters.
|
alpar@100
|
1156 |
|
alpar@100
|
1157 |
/// Constructor that requires parameters.
|
alpar@100
|
1158 |
/// These parameters will be the default values for the traits class.
|
alpar@100
|
1159 |
DijkstraWizard(const Digraph &g,const LengthMap &l, Node s=INVALID) :
|
alpar@100
|
1160 |
TR(g,l,s) {}
|
alpar@100
|
1161 |
|
alpar@100
|
1162 |
///Copy constructor
|
alpar@100
|
1163 |
DijkstraWizard(const TR &b) : TR(b) {}
|
alpar@100
|
1164 |
|
alpar@100
|
1165 |
~DijkstraWizard() {}
|
alpar@100
|
1166 |
|
kpeter@244
|
1167 |
///Runs Dijkstra algorithm from a source node.
|
alpar@209
|
1168 |
|
kpeter@244
|
1169 |
///Runs Dijkstra algorithm from a source node.
|
kpeter@244
|
1170 |
///The node can be given with the \ref source() function.
|
alpar@100
|
1171 |
void run()
|
alpar@100
|
1172 |
{
|
alpar@100
|
1173 |
if(Base::_source==INVALID) throw UninitializedParameter();
|
alpar@209
|
1174 |
Dijkstra<Digraph,LengthMap,TR>
|
alpar@209
|
1175 |
dij(*reinterpret_cast<const Digraph*>(Base::_g),
|
alpar@100
|
1176 |
*reinterpret_cast<const LengthMap*>(Base::_length));
|
kpeter@251
|
1177 |
if(Base::_processed)
|
kpeter@251
|
1178 |
dij.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed));
|
kpeter@251
|
1179 |
if(Base::_pred)
|
kpeter@251
|
1180 |
dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
|
kpeter@251
|
1181 |
if(Base::_dist)
|
kpeter@251
|
1182 |
dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist));
|
alpar@100
|
1183 |
dij.run(Base::_source);
|
alpar@100
|
1184 |
}
|
alpar@100
|
1185 |
|
alpar@100
|
1186 |
///Runs Dijkstra algorithm from the given node.
|
alpar@100
|
1187 |
|
alpar@100
|
1188 |
///Runs Dijkstra algorithm from the given node.
|
alpar@100
|
1189 |
///\param s is the given source.
|
alpar@100
|
1190 |
void run(Node s)
|
alpar@100
|
1191 |
{
|
alpar@100
|
1192 |
Base::_source=s;
|
alpar@100
|
1193 |
run();
|
alpar@100
|
1194 |
}
|
alpar@100
|
1195 |
|
kpeter@244
|
1196 |
/// Sets the source node, from which the Dijkstra algorithm runs.
|
kpeter@244
|
1197 |
|
kpeter@244
|
1198 |
/// Sets the source node, from which the Dijkstra algorithm runs.
|
kpeter@244
|
1199 |
/// \param s is the source node.
|
kpeter@244
|
1200 |
DijkstraWizard<TR> &source(Node s)
|
kpeter@244
|
1201 |
{
|
kpeter@244
|
1202 |
Base::_source=s;
|
kpeter@244
|
1203 |
return *this;
|
kpeter@244
|
1204 |
}
|
kpeter@244
|
1205 |
|
alpar@100
|
1206 |
template<class T>
|
kpeter@257
|
1207 |
struct SetPredMapBase : public Base {
|
alpar@100
|
1208 |
typedef T PredMap;
|
alpar@100
|
1209 |
static PredMap *createPredMap(const Digraph &) { return 0; };
|
kpeter@257
|
1210 |
SetPredMapBase(const TR &b) : TR(b) {}
|
alpar@100
|
1211 |
};
|
alpar@100
|
1212 |
///\brief \ref named-templ-param "Named parameter"
|
kpeter@244
|
1213 |
///for setting \ref PredMap object.
|
alpar@100
|
1214 |
///
|
kpeter@244
|
1215 |
///\ref named-templ-param "Named parameter"
|
kpeter@244
|
1216 |
///for setting \ref PredMap object.
|
alpar@100
|
1217 |
template<class T>
|
kpeter@257
|
1218 |
DijkstraWizard<SetPredMapBase<T> > predMap(const T &t)
|
alpar@100
|
1219 |
{
|
alpar@100
|
1220 |
Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t));
|
kpeter@257
|
1221 |
return DijkstraWizard<SetPredMapBase<T> >(*this);
|
alpar@100
|
1222 |
}
|
alpar@209
|
1223 |
|
alpar@100
|
1224 |
template<class T>
|
kpeter@257
|
1225 |
struct SetProcessedMapBase : public Base {
|
kpeter@244
|
1226 |
typedef T ProcessedMap;
|
kpeter@244
|
1227 |
static ProcessedMap *createProcessedMap(const Digraph &) { return 0; };
|
kpeter@257
|
1228 |
SetProcessedMapBase(const TR &b) : TR(b) {}
|
kpeter@244
|
1229 |
};
|
kpeter@244
|
1230 |
///\brief \ref named-templ-param "Named parameter"
|
kpeter@244
|
1231 |
///for setting \ref ProcessedMap object.
|
kpeter@244
|
1232 |
///
|
kpeter@244
|
1233 |
/// \ref named-templ-param "Named parameter"
|
kpeter@244
|
1234 |
///for setting \ref ProcessedMap object.
|
kpeter@244
|
1235 |
template<class T>
|
kpeter@257
|
1236 |
DijkstraWizard<SetProcessedMapBase<T> > processedMap(const T &t)
|
kpeter@244
|
1237 |
{
|
kpeter@244
|
1238 |
Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t));
|
kpeter@257
|
1239 |
return DijkstraWizard<SetProcessedMapBase<T> >(*this);
|
kpeter@244
|
1240 |
}
|
kpeter@244
|
1241 |
|
kpeter@244
|
1242 |
template<class T>
|
kpeter@257
|
1243 |
struct SetDistMapBase : public Base {
|
alpar@100
|
1244 |
typedef T DistMap;
|
alpar@100
|
1245 |
static DistMap *createDistMap(const Digraph &) { return 0; };
|
kpeter@257
|
1246 |
SetDistMapBase(const TR &b) : TR(b) {}
|
alpar@100
|
1247 |
};
|
alpar@100
|
1248 |
///\brief \ref named-templ-param "Named parameter"
|
kpeter@244
|
1249 |
///for setting \ref DistMap object.
|
alpar@100
|
1250 |
///
|
kpeter@244
|
1251 |
///\ref named-templ-param "Named parameter"
|
kpeter@244
|
1252 |
///for setting \ref DistMap object.
|
alpar@100
|
1253 |
template<class T>
|
kpeter@257
|
1254 |
DijkstraWizard<SetDistMapBase<T> > distMap(const T &t)
|
alpar@100
|
1255 |
{
|
alpar@100
|
1256 |
Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t));
|
kpeter@257
|
1257 |
return DijkstraWizard<SetDistMapBase<T> >(*this);
|
alpar@100
|
1258 |
}
|
alpar@209
|
1259 |
|
alpar@100
|
1260 |
};
|
alpar@209
|
1261 |
|
alpar@100
|
1262 |
///Function type interface for Dijkstra algorithm.
|
alpar@100
|
1263 |
|
alpar@100
|
1264 |
/// \ingroup shortest_path
|
alpar@100
|
1265 |
///Function type interface for Dijkstra algorithm.
|
alpar@100
|
1266 |
///
|
alpar@100
|
1267 |
///This function also has several
|
alpar@100
|
1268 |
///\ref named-templ-func-param "named parameters",
|
alpar@100
|
1269 |
///they are declared as the members of class \ref DijkstraWizard.
|
alpar@100
|
1270 |
///The following
|
alpar@100
|
1271 |
///example shows how to use these parameters.
|
alpar@100
|
1272 |
///\code
|
alpar@100
|
1273 |
/// dijkstra(g,length,source).predMap(preds).run();
|
alpar@100
|
1274 |
///\endcode
|
alpar@100
|
1275 |
///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
|
alpar@100
|
1276 |
///to the end of the parameter list.
|
alpar@100
|
1277 |
///\sa DijkstraWizard
|
alpar@100
|
1278 |
///\sa Dijkstra
|
alpar@100
|
1279 |
template<class GR, class LM>
|
alpar@100
|
1280 |
DijkstraWizard<DijkstraWizardBase<GR,LM> >
|
alpar@100
|
1281 |
dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
|
alpar@100
|
1282 |
{
|
alpar@100
|
1283 |
return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
|
alpar@100
|
1284 |
}
|
alpar@100
|
1285 |
|
alpar@100
|
1286 |
} //END OF NAMESPACE LEMON
|
alpar@100
|
1287 |
|
alpar@100
|
1288 |
#endif
|