alpar@906
|
1 |
/* -*- C++ -*-
|
ladanyi@1435
|
2 |
* lemon/dijkstra.h - Part of LEMON, a generic C++ optimization library
|
alpar@906
|
3 |
*
|
alpar@1164
|
4 |
* Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@1359
|
5 |
* (Egervary Research Group on Combinatorial Optimization, 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@1283
|
23 |
///
|
alpar@1283
|
24 |
///\todo getPath() should be implemented! (also for BFS and DFS)
|
alpar@255
|
25 |
|
alpar@953
|
26 |
#include <lemon/list_graph.h>
|
alpar@921
|
27 |
#include <lemon/bin_heap.h>
|
alpar@921
|
28 |
#include <lemon/invalid.h>
|
alpar@1119
|
29 |
#include <lemon/error.h>
|
alpar@1119
|
30 |
#include <lemon/maps.h>
|
alpar@255
|
31 |
|
alpar@921
|
32 |
namespace lemon {
|
jacint@385
|
33 |
|
alpar@1119
|
34 |
|
alpar@1151
|
35 |
|
alpar@954
|
36 |
///Default traits class of Dijkstra class.
|
alpar@954
|
37 |
|
alpar@954
|
38 |
///Default traits class of Dijkstra class.
|
alpar@954
|
39 |
///\param GR Graph type.
|
alpar@954
|
40 |
///\param LM Type of length map.
|
alpar@953
|
41 |
template<class GR, class LM>
|
alpar@953
|
42 |
struct DijkstraDefaultTraits
|
alpar@953
|
43 |
{
|
alpar@954
|
44 |
///The graph type the algorithm runs on.
|
alpar@953
|
45 |
typedef GR Graph;
|
alpar@953
|
46 |
///The type of the map that stores the edge lengths.
|
alpar@953
|
47 |
|
hegyi@1124
|
48 |
///The type of the map that stores the edge lengths.
|
alpar@967
|
49 |
///It must meet the \ref concept::ReadMap "ReadMap" concept.
|
alpar@953
|
50 |
typedef LM LengthMap;
|
alpar@954
|
51 |
//The type of the length of the edges.
|
alpar@987
|
52 |
typedef typename LM::Value Value;
|
deba@1721
|
53 |
/// The cross reference type used by heap.
|
deba@1721
|
54 |
|
deba@1721
|
55 |
/// The cross reference type used by heap.
|
deba@1721
|
56 |
/// Usually it is \c Graph::NodeMap<int>.
|
deba@1721
|
57 |
typedef typename Graph::template NodeMap<int> HeapCrossRef;
|
deba@1721
|
58 |
///Instantiates a HeapCrossRef.
|
deba@1721
|
59 |
|
deba@1721
|
60 |
///This function instantiates a \ref HeapCrossRef.
|
deba@1721
|
61 |
/// \param G is the graph, to which we would like to define the
|
deba@1721
|
62 |
/// HeapCrossRef.
|
deba@1721
|
63 |
/// \todo The graph alone may be insufficient for the initialization
|
deba@1721
|
64 |
static HeapCrossRef *createHeapCrossRef(const GR &G)
|
deba@1721
|
65 |
{
|
deba@1721
|
66 |
return new HeapCrossRef(G);
|
deba@1721
|
67 |
}
|
deba@1721
|
68 |
|
alpar@954
|
69 |
///The heap type used by Dijkstra algorithm.
|
alpar@967
|
70 |
|
alpar@967
|
71 |
///The heap type used by Dijkstra algorithm.
|
alpar@967
|
72 |
///
|
alpar@967
|
73 |
///\sa BinHeap
|
alpar@967
|
74 |
///\sa Dijkstra
|
deba@1709
|
75 |
typedef BinHeap<typename Graph::Node, typename LM::Value,
|
alpar@953
|
76 |
typename GR::template NodeMap<int>,
|
alpar@987
|
77 |
std::less<Value> > Heap;
|
alpar@953
|
78 |
|
deba@1721
|
79 |
static Heap *createHeap(HeapCrossRef& R)
|
deba@1721
|
80 |
{
|
deba@1721
|
81 |
return new Heap(R);
|
deba@1721
|
82 |
}
|
deba@1721
|
83 |
|
alpar@953
|
84 |
///\brief The type of the map that stores the last
|
alpar@953
|
85 |
///edges of the shortest paths.
|
alpar@953
|
86 |
///
|
hegyi@1124
|
87 |
///The type of the map that stores the last
|
hegyi@1124
|
88 |
///edges of the shortest paths.
|
alpar@967
|
89 |
///It must meet the \ref concept::WriteMap "WriteMap" concept.
|
alpar@953
|
90 |
///
|
alpar@954
|
91 |
typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
|
alpar@954
|
92 |
///Instantiates a PredMap.
|
alpar@953
|
93 |
|
hegyi@1123
|
94 |
///This function instantiates a \ref PredMap.
|
hegyi@1123
|
95 |
///\param G is the graph, to which we would like to define the PredMap.
|
alpar@1119
|
96 |
///\todo The graph alone may be insufficient for the initialization
|
alpar@954
|
97 |
static PredMap *createPredMap(const GR &G)
|
alpar@953
|
98 |
{
|
alpar@953
|
99 |
return new PredMap(G);
|
alpar@953
|
100 |
}
|
alpar@1119
|
101 |
|
alpar@1218
|
102 |
///The type of the map that stores whether a nodes is processed.
|
alpar@1119
|
103 |
|
alpar@1218
|
104 |
///The type of the map that stores whether a nodes is processed.
|
alpar@1119
|
105 |
///It must meet the \ref concept::WriteMap "WriteMap" concept.
|
alpar@1119
|
106 |
///By default it is a NullMap.
|
alpar@1218
|
107 |
///\todo If it is set to a real map,
|
alpar@1218
|
108 |
///Dijkstra::processed() should read this.
|
alpar@1119
|
109 |
///\todo named parameter to set this type, function to read and write.
|
alpar@1218
|
110 |
typedef NullMap<typename Graph::Node,bool> ProcessedMap;
|
alpar@1218
|
111 |
///Instantiates a ProcessedMap.
|
alpar@1119
|
112 |
|
alpar@1218
|
113 |
///This function instantiates a \ref ProcessedMap.
|
alpar@1536
|
114 |
///\param g is the graph, to which
|
alpar@1218
|
115 |
///we would like to define the \ref ProcessedMap
|
alpar@1536
|
116 |
#ifdef DOXYGEN
|
alpar@1536
|
117 |
static ProcessedMap *createProcessedMap(const GR &g)
|
alpar@1536
|
118 |
#else
|
alpar@1366
|
119 |
static ProcessedMap *createProcessedMap(const GR &)
|
alpar@1536
|
120 |
#endif
|
alpar@1119
|
121 |
{
|
alpar@1218
|
122 |
return new ProcessedMap();
|
alpar@1119
|
123 |
}
|
alpar@953
|
124 |
///The type of the map that stores the dists of the nodes.
|
alpar@953
|
125 |
|
hegyi@1124
|
126 |
///The type of the map that stores the dists of the nodes.
|
alpar@967
|
127 |
///It must meet the \ref concept::WriteMap "WriteMap" concept.
|
alpar@953
|
128 |
///
|
alpar@987
|
129 |
typedef typename Graph::template NodeMap<typename LM::Value> DistMap;
|
alpar@954
|
130 |
///Instantiates a DistMap.
|
alpar@953
|
131 |
|
hegyi@1123
|
132 |
///This function instantiates a \ref DistMap.
|
hegyi@1123
|
133 |
///\param G is the graph, to which we would like to define the \ref DistMap
|
alpar@954
|
134 |
static DistMap *createDistMap(const GR &G)
|
alpar@953
|
135 |
{
|
alpar@953
|
136 |
return new DistMap(G);
|
alpar@953
|
137 |
}
|
alpar@953
|
138 |
};
|
alpar@953
|
139 |
|
alpar@255
|
140 |
///%Dijkstra algorithm class.
|
alpar@1125
|
141 |
|
alpar@1151
|
142 |
/// \ingroup flowalgs
|
alpar@255
|
143 |
///This class provides an efficient implementation of %Dijkstra algorithm.
|
alpar@255
|
144 |
///The edge lengths are passed to the algorithm using a
|
klao@959
|
145 |
///\ref concept::ReadMap "ReadMap",
|
alpar@255
|
146 |
///so it is easy to change it to any kind of length.
|
alpar@255
|
147 |
///
|
alpar@880
|
148 |
///The type of the length is determined by the
|
alpar@987
|
149 |
///\ref concept::ReadMap::Value "Value" of the length map.
|
alpar@255
|
150 |
///
|
alpar@255
|
151 |
///It is also possible to change the underlying priority heap.
|
alpar@255
|
152 |
///
|
alpar@1218
|
153 |
///\param GR The graph type the algorithm runs on. The default value
|
alpar@1218
|
154 |
///is \ref ListGraph. The value of GR is not used directly by
|
alpar@1218
|
155 |
///Dijkstra, it is only passed to \ref DijkstraDefaultTraits.
|
alpar@1218
|
156 |
///\param LM This read-only EdgeMap determines the lengths of the
|
alpar@1218
|
157 |
///edges. It is read once for each edge, so the map may involve in
|
alpar@1218
|
158 |
///relatively time consuming process to compute the edge length if
|
alpar@1218
|
159 |
///it is necessary. The default map type is \ref
|
alpar@1218
|
160 |
///concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>". The value
|
alpar@1218
|
161 |
///of LM is not used directly by Dijkstra, it is only passed to \ref
|
alpar@1218
|
162 |
///DijkstraDefaultTraits. \param TR Traits class to set
|
alpar@1218
|
163 |
///various data types used by the algorithm. The default traits
|
alpar@1218
|
164 |
///class is \ref DijkstraDefaultTraits
|
alpar@1218
|
165 |
///"DijkstraDefaultTraits<GR,LM>". See \ref
|
alpar@1218
|
166 |
///DijkstraDefaultTraits for the documentation of a Dijkstra traits
|
alpar@1218
|
167 |
///class.
|
alpar@456
|
168 |
///
|
alpar@689
|
169 |
///\author Jacint Szabo and Alpar Juttner
|
alpar@1128
|
170 |
///\todo A compare object would be nice.
|
alpar@584
|
171 |
|
alpar@255
|
172 |
#ifdef DOXYGEN
|
alpar@584
|
173 |
template <typename GR,
|
alpar@584
|
174 |
typename LM,
|
alpar@953
|
175 |
typename TR>
|
alpar@255
|
176 |
#else
|
alpar@953
|
177 |
template <typename GR=ListGraph,
|
alpar@584
|
178 |
typename LM=typename GR::template EdgeMap<int>,
|
alpar@953
|
179 |
typename TR=DijkstraDefaultTraits<GR,LM> >
|
alpar@255
|
180 |
#endif
|
alpar@1116
|
181 |
class Dijkstra {
|
alpar@255
|
182 |
public:
|
alpar@1125
|
183 |
/**
|
alpar@1125
|
184 |
* \brief \ref Exception for uninitialized parameters.
|
alpar@1125
|
185 |
*
|
alpar@1125
|
186 |
* This error represents problems in the initialization
|
alpar@1125
|
187 |
* of the parameters of the algorithms.
|
alpar@1125
|
188 |
*/
|
alpar@1125
|
189 |
class UninitializedParameter : public lemon::UninitializedParameter {
|
alpar@1125
|
190 |
public:
|
alpar@1125
|
191 |
virtual const char* exceptionName() const {
|
alpar@1218
|
192 |
return "lemon::Dijkstra::UninitializedParameter";
|
alpar@1125
|
193 |
}
|
alpar@1125
|
194 |
};
|
alpar@1119
|
195 |
|
alpar@953
|
196 |
typedef TR Traits;
|
alpar@584
|
197 |
///The type of the underlying graph.
|
alpar@954
|
198 |
typedef typename TR::Graph Graph;
|
alpar@911
|
199 |
///\e
|
alpar@255
|
200 |
typedef typename Graph::Node Node;
|
alpar@911
|
201 |
///\e
|
alpar@255
|
202 |
typedef typename Graph::NodeIt NodeIt;
|
alpar@911
|
203 |
///\e
|
alpar@255
|
204 |
typedef typename Graph::Edge Edge;
|
alpar@911
|
205 |
///\e
|
alpar@255
|
206 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
alpar@255
|
207 |
|
alpar@584
|
208 |
///The type of the length of the edges.
|
alpar@987
|
209 |
typedef typename TR::LengthMap::Value Value;
|
alpar@693
|
210 |
///The type of the map that stores the edge lengths.
|
alpar@954
|
211 |
typedef typename TR::LengthMap LengthMap;
|
alpar@693
|
212 |
///\brief The type of the map that stores the last
|
alpar@584
|
213 |
///edges of the shortest paths.
|
alpar@953
|
214 |
typedef typename TR::PredMap PredMap;
|
alpar@1218
|
215 |
///The type of the map indicating if a node is processed.
|
alpar@1218
|
216 |
typedef typename TR::ProcessedMap ProcessedMap;
|
alpar@693
|
217 |
///The type of the map that stores the dists of the nodes.
|
alpar@953
|
218 |
typedef typename TR::DistMap DistMap;
|
deba@1721
|
219 |
///The cross reference type used for the current heap.
|
deba@1721
|
220 |
typedef typename TR::HeapCrossRef HeapCrossRef;
|
alpar@953
|
221 |
///The heap type used by the dijkstra algorithm.
|
alpar@953
|
222 |
typedef typename TR::Heap Heap;
|
alpar@255
|
223 |
private:
|
alpar@802
|
224 |
/// Pointer to the underlying graph.
|
alpar@688
|
225 |
const Graph *G;
|
alpar@802
|
226 |
/// Pointer to the length map
|
alpar@954
|
227 |
const LengthMap *length;
|
alpar@802
|
228 |
///Pointer to the map of predecessors edges.
|
alpar@1119
|
229 |
PredMap *_pred;
|
alpar@1119
|
230 |
///Indicates if \ref _pred is locally allocated (\c true) or not.
|
alpar@1119
|
231 |
bool local_pred;
|
alpar@802
|
232 |
///Pointer to the map of distances.
|
alpar@1130
|
233 |
DistMap *_dist;
|
alpar@1130
|
234 |
///Indicates if \ref _dist is locally allocated (\c true) or not.
|
alpar@1130
|
235 |
bool local_dist;
|
alpar@1218
|
236 |
///Pointer to the map of processed status of the nodes.
|
alpar@1218
|
237 |
ProcessedMap *_processed;
|
alpar@1218
|
238 |
///Indicates if \ref _processed is locally allocated (\c true) or not.
|
alpar@1218
|
239 |
bool local_processed;
|
deba@1721
|
240 |
///Pointer to the heap cross references.
|
deba@1721
|
241 |
HeapCrossRef *_heap_cross_ref;
|
deba@1721
|
242 |
///Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not.
|
deba@1721
|
243 |
bool local_heap_cross_ref;
|
deba@1721
|
244 |
///Pointer to the heap.
|
deba@1721
|
245 |
Heap *_heap;
|
deba@1721
|
246 |
///Indicates if \ref _heap is locally allocated (\c true) or not.
|
deba@1721
|
247 |
bool local_heap;
|
alpar@688
|
248 |
|
alpar@1128
|
249 |
///Creates the maps if necessary.
|
alpar@688
|
250 |
|
alpar@694
|
251 |
///\todo Error if \c G or are \c NULL. What about \c length?
|
alpar@688
|
252 |
///\todo Better memory allocation (instead of new).
|
alpar@1128
|
253 |
void create_maps()
|
alpar@688
|
254 |
{
|
alpar@1119
|
255 |
if(!_pred) {
|
alpar@1119
|
256 |
local_pred = true;
|
alpar@1119
|
257 |
_pred = Traits::createPredMap(*G);
|
alpar@688
|
258 |
}
|
alpar@1130
|
259 |
if(!_dist) {
|
alpar@1130
|
260 |
local_dist = true;
|
alpar@1130
|
261 |
_dist = Traits::createDistMap(*G);
|
alpar@688
|
262 |
}
|
alpar@1218
|
263 |
if(!_processed) {
|
alpar@1218
|
264 |
local_processed = true;
|
alpar@1218
|
265 |
_processed = Traits::createProcessedMap(*G);
|
alpar@1119
|
266 |
}
|
deba@1721
|
267 |
if (!_heap_cross_ref) {
|
deba@1721
|
268 |
local_heap_cross_ref = true;
|
deba@1721
|
269 |
_heap_cross_ref = Traits::createHeapCrossRef(*G);
|
deba@1721
|
270 |
}
|
deba@1721
|
271 |
if (!_heap) {
|
deba@1721
|
272 |
local_heap = true;
|
deba@1721
|
273 |
_heap = Traits::createHeap(*_heap_cross_ref);
|
deba@1721
|
274 |
}
|
alpar@688
|
275 |
}
|
alpar@255
|
276 |
|
alpar@255
|
277 |
public :
|
deba@1710
|
278 |
|
deba@1710
|
279 |
typedef Dijkstra Create;
|
alpar@1116
|
280 |
|
alpar@1128
|
281 |
///\name Named template parameters
|
alpar@1128
|
282 |
|
alpar@1128
|
283 |
///@{
|
alpar@1128
|
284 |
|
alpar@953
|
285 |
template <class T>
|
alpar@1116
|
286 |
struct DefPredMapTraits : public Traits {
|
alpar@953
|
287 |
typedef T PredMap;
|
alpar@953
|
288 |
static PredMap *createPredMap(const Graph &G)
|
alpar@953
|
289 |
{
|
alpar@1126
|
290 |
throw UninitializedParameter();
|
alpar@953
|
291 |
}
|
alpar@953
|
292 |
};
|
alpar@954
|
293 |
///\ref named-templ-param "Named parameter" for setting PredMap type
|
alpar@954
|
294 |
|
alpar@954
|
295 |
///\ref named-templ-param "Named parameter" for setting PredMap type
|
alpar@1043
|
296 |
///
|
alpar@953
|
297 |
template <class T>
|
deba@1709
|
298 |
struct DefPredMap
|
deba@1709
|
299 |
: public Dijkstra< Graph, LengthMap, DefPredMapTraits<T> > {
|
deba@1709
|
300 |
typedef Dijkstra< Graph, LengthMap, DefPredMapTraits<T> > Create;
|
deba@1709
|
301 |
};
|
alpar@953
|
302 |
|
alpar@953
|
303 |
template <class T>
|
alpar@1116
|
304 |
struct DefDistMapTraits : public Traits {
|
alpar@953
|
305 |
typedef T DistMap;
|
alpar@953
|
306 |
static DistMap *createDistMap(const Graph &G)
|
alpar@953
|
307 |
{
|
alpar@1126
|
308 |
throw UninitializedParameter();
|
alpar@953
|
309 |
}
|
alpar@953
|
310 |
};
|
alpar@954
|
311 |
///\ref named-templ-param "Named parameter" for setting DistMap type
|
alpar@954
|
312 |
|
alpar@954
|
313 |
///\ref named-templ-param "Named parameter" for setting DistMap type
|
alpar@1043
|
314 |
///
|
alpar@953
|
315 |
template <class T>
|
deba@1709
|
316 |
struct DefDistMap
|
deba@1709
|
317 |
: public Dijkstra< Graph, LengthMap, DefDistMapTraits<T> > {
|
deba@1709
|
318 |
typedef Dijkstra< Graph, LengthMap, DefDistMapTraits<T> > Create;
|
deba@1709
|
319 |
};
|
alpar@953
|
320 |
|
alpar@1128
|
321 |
template <class T>
|
alpar@1218
|
322 |
struct DefProcessedMapTraits : public Traits {
|
alpar@1218
|
323 |
typedef T ProcessedMap;
|
alpar@1218
|
324 |
static ProcessedMap *createProcessedMap(const Graph &G)
|
alpar@1128
|
325 |
{
|
alpar@1128
|
326 |
throw UninitializedParameter();
|
alpar@1128
|
327 |
}
|
alpar@1128
|
328 |
};
|
alpar@1218
|
329 |
///\ref named-templ-param "Named parameter" for setting ProcessedMap type
|
alpar@1128
|
330 |
|
alpar@1218
|
331 |
///\ref named-templ-param "Named parameter" for setting ProcessedMap type
|
alpar@1128
|
332 |
///
|
alpar@1128
|
333 |
template <class T>
|
deba@1709
|
334 |
struct DefProcessedMap
|
deba@1709
|
335 |
: public Dijkstra< Graph, LengthMap, DefProcessedMapTraits<T> > {
|
deba@1709
|
336 |
typedef Dijkstra< Graph, LengthMap, DefProcessedMapTraits<T> > Create;
|
deba@1709
|
337 |
};
|
alpar@1128
|
338 |
|
alpar@1218
|
339 |
struct DefGraphProcessedMapTraits : public Traits {
|
alpar@1218
|
340 |
typedef typename Graph::template NodeMap<bool> ProcessedMap;
|
alpar@1218
|
341 |
static ProcessedMap *createProcessedMap(const Graph &G)
|
alpar@1128
|
342 |
{
|
alpar@1218
|
343 |
return new ProcessedMap(G);
|
alpar@1128
|
344 |
}
|
alpar@1128
|
345 |
};
|
alpar@1128
|
346 |
///\brief \ref named-templ-param "Named parameter"
|
alpar@1218
|
347 |
///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
|
alpar@1128
|
348 |
///
|
alpar@1128
|
349 |
///\ref named-templ-param "Named parameter"
|
alpar@1218
|
350 |
///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
|
alpar@1128
|
351 |
///If you don't set it explicitely, it will be automatically allocated.
|
alpar@1128
|
352 |
template <class T>
|
deba@1709
|
353 |
struct DefProcessedMapToBeDefaultMap
|
deba@1709
|
354 |
: public Dijkstra< Graph, LengthMap, DefGraphProcessedMapTraits> {
|
deba@1709
|
355 |
typedef Dijkstra< Graph, LengthMap, DefGraphProcessedMapTraits> Create;
|
deba@1709
|
356 |
};
|
deba@1721
|
357 |
|
deba@1721
|
358 |
template <class H, class CR>
|
deba@1721
|
359 |
struct DefHeapTraits : public Traits {
|
deba@1721
|
360 |
typedef CR HeapCrossRef;
|
deba@1721
|
361 |
typedef H Heap;
|
deba@1721
|
362 |
static HeapCrossRef *createHeapCrossRef(const Graph &G) {
|
deba@1721
|
363 |
return new HeapCrossRef(G);
|
deba@1721
|
364 |
}
|
deba@1721
|
365 |
static Heap *createHeap(HeapCrossRef &R)
|
deba@1721
|
366 |
{
|
deba@1721
|
367 |
return new Heap(R);
|
deba@1721
|
368 |
}
|
deba@1721
|
369 |
};
|
deba@1721
|
370 |
///\ref named-templ-param "Named parameter" for setting heap and cross
|
deba@1721
|
371 |
///reference type
|
deba@1721
|
372 |
|
deba@1721
|
373 |
///\ref named-templ-param "Named parameter" for setting heap and cross
|
deba@1721
|
374 |
///reference type
|
deba@1721
|
375 |
///
|
deba@1721
|
376 |
template <class H, class CR = typename Graph::template NodeMap<int> >
|
deba@1721
|
377 |
struct DefHeap
|
deba@1721
|
378 |
: public Dijkstra< Graph, LengthMap, DefHeapTraits<H, CR> > {
|
deba@1721
|
379 |
typedef Dijkstra< Graph, LengthMap, DefHeapTraits<H, CR> > Create;
|
deba@1721
|
380 |
};
|
alpar@1128
|
381 |
|
alpar@1128
|
382 |
///@}
|
alpar@1128
|
383 |
|
alpar@1128
|
384 |
|
deba@1710
|
385 |
protected:
|
deba@1710
|
386 |
|
deba@1710
|
387 |
Dijkstra() {}
|
deba@1710
|
388 |
|
alpar@1128
|
389 |
public:
|
alpar@1128
|
390 |
|
alpar@802
|
391 |
///Constructor.
|
alpar@255
|
392 |
|
alpar@802
|
393 |
///\param _G the graph the algorithm will run on.
|
alpar@802
|
394 |
///\param _length the length map used by the algorithm.
|
alpar@954
|
395 |
Dijkstra(const Graph& _G, const LengthMap& _length) :
|
alpar@688
|
396 |
G(&_G), length(&_length),
|
alpar@1119
|
397 |
_pred(NULL), local_pred(false),
|
alpar@1130
|
398 |
_dist(NULL), local_dist(false),
|
alpar@1218
|
399 |
_processed(NULL), local_processed(false),
|
deba@1721
|
400 |
_heap_cross_ref(NULL), local_heap_cross_ref(false),
|
deba@1721
|
401 |
_heap(NULL), local_heap(false)
|
alpar@688
|
402 |
{ }
|
alpar@688
|
403 |
|
alpar@802
|
404 |
///Destructor.
|
alpar@688
|
405 |
~Dijkstra()
|
alpar@688
|
406 |
{
|
alpar@1119
|
407 |
if(local_pred) delete _pred;
|
alpar@1130
|
408 |
if(local_dist) delete _dist;
|
alpar@1218
|
409 |
if(local_processed) delete _processed;
|
deba@1721
|
410 |
if(local_heap_cross_ref) delete _heap_cross_ref;
|
deba@1721
|
411 |
if(local_heap) delete _heap;
|
alpar@688
|
412 |
}
|
alpar@688
|
413 |
|
alpar@688
|
414 |
///Sets the length map.
|
alpar@688
|
415 |
|
alpar@688
|
416 |
///Sets the length map.
|
alpar@688
|
417 |
///\return <tt> (*this) </tt>
|
alpar@1116
|
418 |
Dijkstra &lengthMap(const LengthMap &m)
|
alpar@688
|
419 |
{
|
alpar@688
|
420 |
length = &m;
|
alpar@688
|
421 |
return *this;
|
alpar@688
|
422 |
}
|
alpar@688
|
423 |
|
alpar@688
|
424 |
///Sets the map storing the predecessor edges.
|
alpar@688
|
425 |
|
alpar@688
|
426 |
///Sets the map storing the predecessor edges.
|
alpar@688
|
427 |
///If you don't use this function before calling \ref run(),
|
alpar@688
|
428 |
///it will allocate one. The destuctor deallocates this
|
alpar@688
|
429 |
///automatically allocated map, of course.
|
alpar@688
|
430 |
///\return <tt> (*this) </tt>
|
alpar@1116
|
431 |
Dijkstra &predMap(PredMap &m)
|
alpar@688
|
432 |
{
|
alpar@1119
|
433 |
if(local_pred) {
|
alpar@1119
|
434 |
delete _pred;
|
alpar@1119
|
435 |
local_pred=false;
|
alpar@688
|
436 |
}
|
alpar@1119
|
437 |
_pred = &m;
|
alpar@688
|
438 |
return *this;
|
alpar@688
|
439 |
}
|
alpar@688
|
440 |
|
alpar@688
|
441 |
///Sets the map storing the distances calculated by the algorithm.
|
alpar@688
|
442 |
|
alpar@688
|
443 |
///Sets the map storing the distances calculated by the algorithm.
|
alpar@688
|
444 |
///If you don't use this function before calling \ref run(),
|
alpar@688
|
445 |
///it will allocate one. The destuctor deallocates this
|
alpar@688
|
446 |
///automatically allocated map, of course.
|
alpar@688
|
447 |
///\return <tt> (*this) </tt>
|
alpar@1116
|
448 |
Dijkstra &distMap(DistMap &m)
|
alpar@688
|
449 |
{
|
alpar@1130
|
450 |
if(local_dist) {
|
alpar@1130
|
451 |
delete _dist;
|
alpar@1130
|
452 |
local_dist=false;
|
alpar@688
|
453 |
}
|
alpar@1130
|
454 |
_dist = &m;
|
alpar@688
|
455 |
return *this;
|
alpar@688
|
456 |
}
|
alpar@694
|
457 |
|
alpar@1130
|
458 |
private:
|
alpar@1130
|
459 |
void finalizeNodeData(Node v,Value dst)
|
alpar@1130
|
460 |
{
|
alpar@1218
|
461 |
_processed->set(v,true);
|
alpar@1130
|
462 |
_dist->set(v, dst);
|
alpar@1130
|
463 |
}
|
alpar@1130
|
464 |
|
alpar@1130
|
465 |
public:
|
alpar@1218
|
466 |
///\name Execution control
|
alpar@1128
|
467 |
///The simplest way to execute the algorithm is to use
|
alpar@1156
|
468 |
///one of the member functions called \c run(...).
|
alpar@1128
|
469 |
///\n
|
alpar@1218
|
470 |
///If you need more control on the execution,
|
alpar@1128
|
471 |
///first you must call \ref init(), then you can add several source nodes
|
alpar@1218
|
472 |
///with \ref addSource().
|
alpar@1218
|
473 |
///Finally \ref start() will perform the actual path
|
alpar@1128
|
474 |
///computation.
|
alpar@1128
|
475 |
|
alpar@1128
|
476 |
///@{
|
alpar@1128
|
477 |
|
alpar@1128
|
478 |
///Initializes the internal data structures.
|
alpar@1128
|
479 |
|
alpar@1128
|
480 |
///Initializes the internal data structures.
|
alpar@1128
|
481 |
///
|
alpar@1128
|
482 |
void init()
|
alpar@1128
|
483 |
{
|
alpar@1128
|
484 |
create_maps();
|
deba@1721
|
485 |
_heap->clear();
|
alpar@774
|
486 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
|
alpar@1119
|
487 |
_pred->set(u,INVALID);
|
alpar@1218
|
488 |
_processed->set(u,false);
|
deba@1721
|
489 |
_heap_cross_ref->set(u,Heap::PRE_HEAP);
|
alpar@694
|
490 |
}
|
alpar@1128
|
491 |
}
|
alpar@1128
|
492 |
|
alpar@1128
|
493 |
///Adds a new source node.
|
alpar@1128
|
494 |
|
alpar@1155
|
495 |
///Adds a new source node to the priority heap.
|
alpar@1128
|
496 |
///
|
alpar@1128
|
497 |
///The optional second parameter is the initial distance of the node.
|
alpar@1128
|
498 |
///
|
alpar@1155
|
499 |
///It checks if the node has already been added to the heap and
|
alpar@1155
|
500 |
///It is pushed to the heap only if either it was not in the heap
|
alpar@1155
|
501 |
///or the shortest path found till then is longer then \c dst.
|
alpar@1128
|
502 |
void addSource(Node s,Value dst=0)
|
alpar@1128
|
503 |
{
|
deba@1721
|
504 |
if(_heap->state(s) != Heap::IN_HEAP) {
|
deba@1721
|
505 |
_heap->push(s,dst);
|
deba@1721
|
506 |
} else if((*_heap)[s]<dst) {
|
deba@1721
|
507 |
_heap->push(s,dst);
|
alpar@1155
|
508 |
_pred->set(s,INVALID);
|
alpar@1155
|
509 |
}
|
alpar@1128
|
510 |
}
|
alpar@1128
|
511 |
|
alpar@1155
|
512 |
///Processes the next node in the priority heap
|
alpar@1155
|
513 |
|
alpar@1155
|
514 |
///Processes the next node in the priority heap.
|
alpar@1155
|
515 |
///
|
alpar@1516
|
516 |
///\return The processed node.
|
alpar@1516
|
517 |
///
|
alpar@1155
|
518 |
///\warning The priority heap must not be empty!
|
alpar@1516
|
519 |
Node processNextNode()
|
alpar@1128
|
520 |
{
|
deba@1721
|
521 |
Node v=_heap->top();
|
deba@1721
|
522 |
Value oldvalue=_heap->prio();
|
deba@1721
|
523 |
_heap->pop();
|
alpar@1130
|
524 |
finalizeNodeData(v,oldvalue);
|
alpar@694
|
525 |
|
alpar@1128
|
526 |
for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
|
alpar@1128
|
527 |
Node w=G->target(e);
|
deba@1721
|
528 |
switch(_heap->state(w)) {
|
alpar@1128
|
529 |
case Heap::PRE_HEAP:
|
deba@1721
|
530 |
_heap->push(w,oldvalue+(*length)[e]);
|
alpar@1128
|
531 |
_pred->set(w,e);
|
alpar@1128
|
532 |
break;
|
alpar@1128
|
533 |
case Heap::IN_HEAP:
|
deba@1721
|
534 |
if ( oldvalue+(*length)[e] < (*_heap)[w] ) {
|
deba@1721
|
535 |
_heap->decrease(w, oldvalue+(*length)[e]);
|
alpar@1119
|
536 |
_pred->set(w,e);
|
alpar@694
|
537 |
}
|
alpar@1128
|
538 |
break;
|
alpar@1128
|
539 |
case Heap::POST_HEAP:
|
alpar@1128
|
540 |
break;
|
alpar@694
|
541 |
}
|
alpar@694
|
542 |
}
|
alpar@1516
|
543 |
return v;
|
alpar@694
|
544 |
}
|
alpar@1128
|
545 |
|
alpar@1665
|
546 |
///Next node to be processed.
|
alpar@1665
|
547 |
|
alpar@1665
|
548 |
///Next node to be processed.
|
alpar@1665
|
549 |
///
|
alpar@1665
|
550 |
///\return The next node to be processed or INVALID if the priority heap
|
alpar@1665
|
551 |
/// is empty.
|
deba@1694
|
552 |
Node nextNode()
|
alpar@1665
|
553 |
{
|
deba@1721
|
554 |
return _heap->empty()?_heap->top():INVALID;
|
alpar@1665
|
555 |
}
|
alpar@1665
|
556 |
|
alpar@1218
|
557 |
///\brief Returns \c false if there are nodes
|
alpar@1218
|
558 |
///to be processed in the priority heap
|
alpar@1155
|
559 |
///
|
alpar@1218
|
560 |
///Returns \c false if there are nodes
|
alpar@1218
|
561 |
///to be processed in the priority heap
|
deba@1721
|
562 |
bool emptyQueue() { return _heap->empty(); }
|
alpar@1155
|
563 |
///Returns the number of the nodes to be processed in the priority heap
|
alpar@1155
|
564 |
|
alpar@1155
|
565 |
///Returns the number of the nodes to be processed in the priority heap
|
alpar@1155
|
566 |
///
|
deba@1721
|
567 |
int queueSize() { return _heap->size(); }
|
alpar@1155
|
568 |
|
alpar@1130
|
569 |
///Executes the algorithm.
|
alpar@1128
|
570 |
|
alpar@1130
|
571 |
///Executes the algorithm.
|
alpar@1128
|
572 |
///
|
alpar@1130
|
573 |
///\pre init() must be called and at least one node should be added
|
alpar@1130
|
574 |
///with addSource() before using this function.
|
alpar@1128
|
575 |
///
|
alpar@1128
|
576 |
///This method runs the %Dijkstra algorithm from the root node(s)
|
alpar@1128
|
577 |
///in order to
|
alpar@1128
|
578 |
///compute the
|
alpar@1128
|
579 |
///shortest path to each node. The algorithm computes
|
alpar@1128
|
580 |
///- The shortest path tree.
|
alpar@1128
|
581 |
///- The distance of each node from the root(s).
|
alpar@1128
|
582 |
///
|
alpar@1128
|
583 |
void start()
|
alpar@1128
|
584 |
{
|
deba@1721
|
585 |
while ( !_heap->empty() ) processNextNode();
|
alpar@1128
|
586 |
}
|
alpar@255
|
587 |
|
alpar@1130
|
588 |
///Executes the algorithm until \c dest is reached.
|
alpar@1128
|
589 |
|
alpar@1130
|
590 |
///Executes the algorithm until \c dest is reached.
|
alpar@1128
|
591 |
///
|
alpar@1130
|
592 |
///\pre init() must be called and at least one node should be added
|
alpar@1130
|
593 |
///with addSource() before using this function.
|
alpar@1128
|
594 |
///
|
alpar@1128
|
595 |
///This method runs the %Dijkstra algorithm from the root node(s)
|
alpar@1128
|
596 |
///in order to
|
alpar@1128
|
597 |
///compute the
|
alpar@1128
|
598 |
///shortest path to \c dest. The algorithm computes
|
alpar@1128
|
599 |
///- The shortest path to \c dest.
|
alpar@1128
|
600 |
///- The distance of \c dest from the root(s).
|
alpar@1128
|
601 |
///
|
alpar@1128
|
602 |
void start(Node dest)
|
alpar@1128
|
603 |
{
|
deba@1721
|
604 |
while ( !_heap->empty() && _heap->top()!=dest ) processNextNode();
|
deba@1721
|
605 |
if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio());
|
alpar@1130
|
606 |
}
|
alpar@1130
|
607 |
|
alpar@1130
|
608 |
///Executes the algorithm until a condition is met.
|
alpar@1130
|
609 |
|
alpar@1130
|
610 |
///Executes the algorithm until a condition is met.
|
alpar@1130
|
611 |
///
|
alpar@1130
|
612 |
///\pre init() must be called and at least one node should be added
|
alpar@1130
|
613 |
///with addSource() before using this function.
|
alpar@1130
|
614 |
///
|
alpar@1130
|
615 |
///\param nm must be a bool (or convertible) node map. The algorithm
|
alpar@1130
|
616 |
///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
|
deba@1345
|
617 |
template<class NodeBoolMap>
|
deba@1345
|
618 |
void start(const NodeBoolMap &nm)
|
alpar@1130
|
619 |
{
|
deba@1721
|
620 |
while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode();
|
deba@1721
|
621 |
if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio());
|
alpar@1128
|
622 |
}
|
alpar@1128
|
623 |
|
alpar@1128
|
624 |
///Runs %Dijkstra algorithm from node \c s.
|
alpar@1128
|
625 |
|
alpar@1128
|
626 |
///This method runs the %Dijkstra algorithm from a root node \c s
|
alpar@1128
|
627 |
///in order to
|
alpar@1128
|
628 |
///compute the
|
alpar@1128
|
629 |
///shortest path to each node. The algorithm computes
|
alpar@1128
|
630 |
///- The shortest path tree.
|
alpar@1128
|
631 |
///- The distance of each node from the root.
|
alpar@1128
|
632 |
///
|
alpar@1128
|
633 |
///\note d.run(s) is just a shortcut of the following code.
|
alpar@1128
|
634 |
///\code
|
alpar@1128
|
635 |
/// d.init();
|
alpar@1128
|
636 |
/// d.addSource(s);
|
alpar@1128
|
637 |
/// d.start();
|
alpar@1128
|
638 |
///\endcode
|
alpar@1128
|
639 |
void run(Node s) {
|
alpar@1128
|
640 |
init();
|
alpar@1128
|
641 |
addSource(s);
|
alpar@1128
|
642 |
start();
|
alpar@1128
|
643 |
}
|
alpar@1128
|
644 |
|
alpar@1130
|
645 |
///Finds the shortest path between \c s and \c t.
|
alpar@1130
|
646 |
|
alpar@1130
|
647 |
///Finds the shortest path between \c s and \c t.
|
alpar@1130
|
648 |
///
|
alpar@1130
|
649 |
///\return The length of the shortest s---t path if there exists one,
|
alpar@1130
|
650 |
///0 otherwise.
|
alpar@1130
|
651 |
///\note Apart from the return value, d.run(s) is
|
alpar@1130
|
652 |
///just a shortcut of the following code.
|
alpar@1130
|
653 |
///\code
|
alpar@1130
|
654 |
/// d.init();
|
alpar@1130
|
655 |
/// d.addSource(s);
|
alpar@1130
|
656 |
/// d.start(t);
|
alpar@1130
|
657 |
///\endcode
|
alpar@1130
|
658 |
Value run(Node s,Node t) {
|
alpar@1130
|
659 |
init();
|
alpar@1130
|
660 |
addSource(s);
|
alpar@1130
|
661 |
start(t);
|
alpar@1130
|
662 |
return (*_pred)[t]==INVALID?0:(*_dist)[t];
|
alpar@1130
|
663 |
}
|
alpar@1130
|
664 |
|
alpar@1128
|
665 |
///@}
|
alpar@1128
|
666 |
|
alpar@1128
|
667 |
///\name Query Functions
|
alpar@1128
|
668 |
///The result of the %Dijkstra algorithm can be obtained using these
|
alpar@1128
|
669 |
///functions.\n
|
alpar@1128
|
670 |
///Before the use of these functions,
|
alpar@1128
|
671 |
///either run() or start() must be called.
|
alpar@1128
|
672 |
|
alpar@1128
|
673 |
///@{
|
alpar@1128
|
674 |
|
alpar@1283
|
675 |
///Copies the shortest path to \c t into \c p
|
alpar@1283
|
676 |
|
alpar@1283
|
677 |
///This function copies the shortest path to \c t into \c p.
|
alpar@1536
|
678 |
///If it \c t is a source itself or unreachable, then it does not
|
alpar@1283
|
679 |
///alter \c p.
|
alpar@1283
|
680 |
///\todo Is it the right way to handle unreachable nodes?
|
alpar@1283
|
681 |
///\return Returns \c true if a path to \c t was actually copied to \c p,
|
alpar@1283
|
682 |
///\c false otherwise.
|
alpar@1283
|
683 |
///\sa DirPath
|
alpar@1283
|
684 |
template<class P>
|
alpar@1283
|
685 |
bool getPath(P &p,Node t)
|
alpar@1283
|
686 |
{
|
alpar@1283
|
687 |
if(reached(t)) {
|
alpar@1283
|
688 |
p.clear();
|
alpar@1283
|
689 |
typename P::Builder b(p);
|
alpar@1283
|
690 |
for(b.setStartNode(t);pred(t)!=INVALID;t=predNode(t))
|
alpar@1283
|
691 |
b.pushFront(pred(t));
|
alpar@1283
|
692 |
b.commit();
|
alpar@1283
|
693 |
return true;
|
alpar@1283
|
694 |
}
|
alpar@1283
|
695 |
return false;
|
alpar@1283
|
696 |
}
|
alpar@1283
|
697 |
|
jacint@385
|
698 |
///The distance of a node from the root.
|
alpar@255
|
699 |
|
jacint@385
|
700 |
///Returns the distance of a node from the root.
|
alpar@255
|
701 |
///\pre \ref run() must be called before using this function.
|
jacint@385
|
702 |
///\warning If node \c v in unreachable from the root the return value
|
alpar@255
|
703 |
///of this funcion is undefined.
|
alpar@1130
|
704 |
Value dist(Node v) const { return (*_dist)[v]; }
|
jacint@373
|
705 |
|
alpar@584
|
706 |
///Returns the 'previous edge' of the shortest path tree.
|
alpar@255
|
707 |
|
alpar@584
|
708 |
///For a node \c v it returns the 'previous edge' of the shortest path tree,
|
alpar@785
|
709 |
///i.e. it returns the last edge of a shortest path from the root to \c
|
alpar@688
|
710 |
///v. It is \ref INVALID
|
alpar@688
|
711 |
///if \c v is unreachable from the root or if \c v=s. The
|
jacint@385
|
712 |
///shortest path tree used here is equal to the shortest path tree used in
|
alpar@1631
|
713 |
///\ref predNode(). \pre \ref run() must be called before using
|
jacint@385
|
714 |
///this function.
|
alpar@780
|
715 |
///\todo predEdge could be a better name.
|
alpar@1119
|
716 |
Edge pred(Node v) const { return (*_pred)[v]; }
|
jacint@373
|
717 |
|
alpar@584
|
718 |
///Returns the 'previous node' of the shortest path tree.
|
alpar@255
|
719 |
|
alpar@584
|
720 |
///For a node \c v it returns the 'previous node' of the shortest path tree,
|
jacint@385
|
721 |
///i.e. it returns the last but one node from a shortest path from the
|
jacint@385
|
722 |
///root to \c /v. It is INVALID if \c v is unreachable from the root or if
|
jacint@385
|
723 |
///\c v=s. The shortest path tree used here is equal to the shortest path
|
alpar@1631
|
724 |
///tree used in \ref pred(). \pre \ref run() must be called before
|
jacint@385
|
725 |
///using this function.
|
alpar@1130
|
726 |
Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
|
alpar@1130
|
727 |
G->source((*_pred)[v]); }
|
alpar@255
|
728 |
|
alpar@255
|
729 |
///Returns a reference to the NodeMap of distances.
|
alpar@255
|
730 |
|
jacint@385
|
731 |
///Returns a reference to the NodeMap of distances. \pre \ref run() must
|
jacint@385
|
732 |
///be called before using this function.
|
alpar@1130
|
733 |
const DistMap &distMap() const { return *_dist;}
|
jacint@385
|
734 |
|
alpar@255
|
735 |
///Returns a reference to the shortest path tree map.
|
alpar@255
|
736 |
|
alpar@255
|
737 |
///Returns a reference to the NodeMap of the edges of the
|
alpar@255
|
738 |
///shortest path tree.
|
alpar@255
|
739 |
///\pre \ref run() must be called before using this function.
|
alpar@1119
|
740 |
const PredMap &predMap() const { return *_pred;}
|
jacint@385
|
741 |
|
jacint@385
|
742 |
///Checks if a node is reachable from the root.
|
alpar@255
|
743 |
|
jacint@385
|
744 |
///Returns \c true if \c v is reachable from the root.
|
alpar@1218
|
745 |
///\warning The source nodes are inditated as unreached.
|
alpar@255
|
746 |
///\pre \ref run() must be called before using this function.
|
jacint@385
|
747 |
///
|
deba@1721
|
748 |
bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; }
|
alpar@255
|
749 |
|
alpar@1128
|
750 |
///@}
|
alpar@255
|
751 |
};
|
alpar@953
|
752 |
|
alpar@1218
|
753 |
|
alpar@1218
|
754 |
|
alpar@1218
|
755 |
|
alpar@1218
|
756 |
|
alpar@1218
|
757 |
///Default traits class of Dijkstra function.
|
alpar@1218
|
758 |
|
alpar@1218
|
759 |
///Default traits class of Dijkstra function.
|
alpar@1218
|
760 |
///\param GR Graph type.
|
alpar@1218
|
761 |
///\param LM Type of length map.
|
alpar@1218
|
762 |
template<class GR, class LM>
|
alpar@1218
|
763 |
struct DijkstraWizardDefaultTraits
|
alpar@1218
|
764 |
{
|
alpar@1218
|
765 |
///The graph type the algorithm runs on.
|
alpar@1218
|
766 |
typedef GR Graph;
|
alpar@1218
|
767 |
///The type of the map that stores the edge lengths.
|
alpar@1218
|
768 |
|
alpar@1218
|
769 |
///The type of the map that stores the edge lengths.
|
alpar@1218
|
770 |
///It must meet the \ref concept::ReadMap "ReadMap" concept.
|
alpar@1218
|
771 |
typedef LM LengthMap;
|
alpar@1218
|
772 |
//The type of the length of the edges.
|
alpar@1218
|
773 |
typedef typename LM::Value Value;
|
alpar@1218
|
774 |
///The heap type used by Dijkstra algorithm.
|
alpar@1218
|
775 |
|
deba@1721
|
776 |
/// The cross reference type used by heap.
|
deba@1721
|
777 |
|
deba@1721
|
778 |
/// The cross reference type used by heap.
|
deba@1721
|
779 |
/// Usually it is \c Graph::NodeMap<int>.
|
deba@1721
|
780 |
typedef typename Graph::template NodeMap<int> HeapCrossRef;
|
deba@1721
|
781 |
///Instantiates a HeapCrossRef.
|
deba@1721
|
782 |
|
deba@1721
|
783 |
///This function instantiates a \ref HeapCrossRef.
|
deba@1721
|
784 |
/// \param G is the graph, to which we would like to define the
|
deba@1721
|
785 |
/// HeapCrossRef.
|
deba@1721
|
786 |
/// \todo The graph alone may be insufficient for the initialization
|
deba@1721
|
787 |
static HeapCrossRef *createHeapCrossRef(const GR &G)
|
deba@1721
|
788 |
{
|
deba@1721
|
789 |
return new HeapCrossRef(G);
|
deba@1721
|
790 |
}
|
deba@1721
|
791 |
|
deba@1721
|
792 |
///The heap type used by Dijkstra algorithm.
|
deba@1721
|
793 |
|
alpar@1218
|
794 |
///The heap type used by Dijkstra algorithm.
|
alpar@1218
|
795 |
///
|
alpar@1218
|
796 |
///\sa BinHeap
|
alpar@1218
|
797 |
///\sa Dijkstra
|
deba@1721
|
798 |
typedef BinHeap<typename Graph::Node, typename LM::Value,
|
alpar@1218
|
799 |
typename GR::template NodeMap<int>,
|
alpar@1218
|
800 |
std::less<Value> > Heap;
|
alpar@1218
|
801 |
|
deba@1721
|
802 |
static Heap *createHeap(HeapCrossRef& R)
|
deba@1721
|
803 |
{
|
deba@1721
|
804 |
return new Heap(R);
|
deba@1721
|
805 |
}
|
deba@1721
|
806 |
|
alpar@1218
|
807 |
///\brief The type of the map that stores the last
|
alpar@1218
|
808 |
///edges of the shortest paths.
|
alpar@1218
|
809 |
///
|
alpar@1218
|
810 |
///The type of the map that stores the last
|
alpar@1218
|
811 |
///edges of the shortest paths.
|
alpar@1218
|
812 |
///It must meet the \ref concept::WriteMap "WriteMap" concept.
|
alpar@1218
|
813 |
///
|
alpar@1218
|
814 |
typedef NullMap <typename GR::Node,typename GR::Edge> PredMap;
|
alpar@1218
|
815 |
///Instantiates a PredMap.
|
alpar@1218
|
816 |
|
alpar@1218
|
817 |
///This function instantiates a \ref PredMap.
|
alpar@1536
|
818 |
///\param g is the graph, to which we would like to define the PredMap.
|
alpar@1218
|
819 |
///\todo The graph alone may be insufficient for the initialization
|
alpar@1536
|
820 |
#ifdef DOXYGEN
|
alpar@1536
|
821 |
static PredMap *createPredMap(const GR &g)
|
alpar@1536
|
822 |
#else
|
alpar@1367
|
823 |
static PredMap *createPredMap(const GR &)
|
alpar@1536
|
824 |
#endif
|
alpar@1218
|
825 |
{
|
alpar@1218
|
826 |
return new PredMap();
|
alpar@1218
|
827 |
}
|
alpar@1218
|
828 |
///The type of the map that stores whether a nodes is processed.
|
alpar@1218
|
829 |
|
alpar@1218
|
830 |
///The type of the map that stores whether a nodes is processed.
|
alpar@1218
|
831 |
///It must meet the \ref concept::WriteMap "WriteMap" concept.
|
alpar@1218
|
832 |
///By default it is a NullMap.
|
alpar@1218
|
833 |
///\todo If it is set to a real map,
|
alpar@1218
|
834 |
///Dijkstra::processed() should read this.
|
alpar@1218
|
835 |
///\todo named parameter to set this type, function to read and write.
|
alpar@1218
|
836 |
typedef NullMap<typename Graph::Node,bool> ProcessedMap;
|
alpar@1218
|
837 |
///Instantiates a ProcessedMap.
|
alpar@1218
|
838 |
|
alpar@1218
|
839 |
///This function instantiates a \ref ProcessedMap.
|
alpar@1536
|
840 |
///\param g is the graph, to which
|
alpar@1218
|
841 |
///we would like to define the \ref ProcessedMap
|
alpar@1536
|
842 |
#ifdef DOXYGEN
|
alpar@1536
|
843 |
static ProcessedMap *createProcessedMap(const GR &g)
|
alpar@1536
|
844 |
#else
|
alpar@1367
|
845 |
static ProcessedMap *createProcessedMap(const GR &)
|
alpar@1536
|
846 |
#endif
|
alpar@1218
|
847 |
{
|
alpar@1218
|
848 |
return new ProcessedMap();
|
alpar@1218
|
849 |
}
|
alpar@1218
|
850 |
///The type of the map that stores the dists of the nodes.
|
alpar@1218
|
851 |
|
alpar@1218
|
852 |
///The type of the map that stores the dists of the nodes.
|
alpar@1218
|
853 |
///It must meet the \ref concept::WriteMap "WriteMap" concept.
|
alpar@1218
|
854 |
///
|
alpar@1218
|
855 |
typedef NullMap<typename Graph::Node,typename LM::Value> DistMap;
|
alpar@1218
|
856 |
///Instantiates a DistMap.
|
alpar@1218
|
857 |
|
alpar@1218
|
858 |
///This function instantiates a \ref DistMap.
|
alpar@1536
|
859 |
///\param g is the graph, to which we would like to define the \ref DistMap
|
alpar@1536
|
860 |
#ifdef DOXYGEN
|
alpar@1536
|
861 |
static DistMap *createDistMap(const GR &g)
|
alpar@1536
|
862 |
#else
|
alpar@1367
|
863 |
static DistMap *createDistMap(const GR &)
|
alpar@1536
|
864 |
#endif
|
alpar@1218
|
865 |
{
|
alpar@1218
|
866 |
return new DistMap();
|
alpar@1218
|
867 |
}
|
alpar@1218
|
868 |
};
|
alpar@1218
|
869 |
|
hegyi@1123
|
870 |
/// Default traits used by \ref DijkstraWizard
|
hegyi@1123
|
871 |
|
alpar@1151
|
872 |
/// To make it easier to use Dijkstra algorithm
|
alpar@1151
|
873 |
///we have created a wizard class.
|
alpar@1151
|
874 |
/// This \ref DijkstraWizard class needs default traits,
|
alpar@1151
|
875 |
///as well as the \ref Dijkstra class.
|
hegyi@1123
|
876 |
/// The \ref DijkstraWizardBase is a class to be the default traits of the
|
hegyi@1123
|
877 |
/// \ref DijkstraWizard class.
|
alpar@1220
|
878 |
/// \todo More named parameters are required...
|
alpar@1116
|
879 |
template<class GR,class LM>
|
alpar@1218
|
880 |
class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
|
alpar@1116
|
881 |
{
|
alpar@1116
|
882 |
|
alpar@1218
|
883 |
typedef DijkstraWizardDefaultTraits<GR,LM> Base;
|
alpar@1116
|
884 |
protected:
|
alpar@1201
|
885 |
/// Type of the nodes in the graph.
|
alpar@1201
|
886 |
typedef typename Base::Graph::Node Node;
|
alpar@1201
|
887 |
|
alpar@1116
|
888 |
/// Pointer to the underlying graph.
|
alpar@1116
|
889 |
void *_g;
|
alpar@1116
|
890 |
/// Pointer to the length map
|
alpar@1116
|
891 |
void *_length;
|
alpar@1116
|
892 |
///Pointer to the map of predecessors edges.
|
alpar@1116
|
893 |
void *_pred;
|
alpar@1116
|
894 |
///Pointer to the map of distances.
|
alpar@1116
|
895 |
void *_dist;
|
alpar@1116
|
896 |
///Pointer to the source node.
|
alpar@1201
|
897 |
Node _source;
|
alpar@1116
|
898 |
|
alpar@1116
|
899 |
public:
|
hegyi@1123
|
900 |
/// Constructor.
|
hegyi@1123
|
901 |
|
hegyi@1123
|
902 |
/// This constructor does not require parameters, therefore it initiates
|
hegyi@1123
|
903 |
/// all of the attributes to default values (0, INVALID).
|
alpar@1218
|
904 |
DijkstraWizardBase() : _g(0), _length(0), _pred(0),
|
alpar@1218
|
905 |
_dist(0), _source(INVALID) {}
|
alpar@1116
|
906 |
|
hegyi@1123
|
907 |
/// Constructor.
|
hegyi@1123
|
908 |
|
alpar@1156
|
909 |
/// This constructor requires some parameters,
|
alpar@1156
|
910 |
/// listed in the parameters list.
|
hegyi@1123
|
911 |
/// Others are initiated to 0.
|
hegyi@1123
|
912 |
/// \param g is the initial value of \ref _g
|
hegyi@1123
|
913 |
/// \param l is the initial value of \ref _length
|
hegyi@1123
|
914 |
/// \param s is the initial value of \ref _source
|
alpar@1116
|
915 |
DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
|
alpar@1218
|
916 |
_g((void *)&g), _length((void *)&l), _pred(0),
|
alpar@1218
|
917 |
_dist(0), _source(s) {}
|
alpar@1116
|
918 |
|
alpar@1116
|
919 |
};
|
alpar@1116
|
920 |
|
alpar@1229
|
921 |
/// A class to make the usage of Dijkstra algorithm easier
|
alpar@953
|
922 |
|
hegyi@1123
|
923 |
/// This class is created to make it easier to use Dijkstra algorithm.
|
hegyi@1123
|
924 |
/// It uses the functions and features of the plain \ref Dijkstra,
|
alpar@1151
|
925 |
/// but it is much simpler to use it.
|
alpar@953
|
926 |
///
|
hegyi@1123
|
927 |
/// Simplicity means that the way to change the types defined
|
hegyi@1123
|
928 |
/// in the traits class is based on functions that returns the new class
|
alpar@1151
|
929 |
/// and not on templatable built-in classes.
|
alpar@1151
|
930 |
/// When using the plain \ref Dijkstra
|
alpar@1151
|
931 |
/// the new class with the modified type comes from
|
alpar@1151
|
932 |
/// the original class by using the ::
|
alpar@1151
|
933 |
/// operator. In the case of \ref DijkstraWizard only
|
alpar@1151
|
934 |
/// a function have to be called and it will
|
hegyi@1123
|
935 |
/// return the needed class.
|
hegyi@1123
|
936 |
///
|
hegyi@1123
|
937 |
/// It does not have own \ref run method. When its \ref run method is called
|
deba@1721
|
938 |
/// it initiates a plain \ref Dijkstra class, and calls the \ref
|
deba@1721
|
939 |
/// Dijkstra::run method of it.
|
alpar@953
|
940 |
template<class TR>
|
alpar@1116
|
941 |
class DijkstraWizard : public TR
|
alpar@953
|
942 |
{
|
alpar@1116
|
943 |
typedef TR Base;
|
alpar@953
|
944 |
|
hegyi@1123
|
945 |
///The type of the underlying graph.
|
alpar@953
|
946 |
typedef typename TR::Graph Graph;
|
alpar@1119
|
947 |
//\e
|
alpar@953
|
948 |
typedef typename Graph::Node Node;
|
alpar@1119
|
949 |
//\e
|
alpar@953
|
950 |
typedef typename Graph::NodeIt NodeIt;
|
alpar@1119
|
951 |
//\e
|
alpar@953
|
952 |
typedef typename Graph::Edge Edge;
|
alpar@1119
|
953 |
//\e
|
alpar@953
|
954 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
alpar@953
|
955 |
|
hegyi@1123
|
956 |
///The type of the map that stores the edge lengths.
|
alpar@953
|
957 |
typedef typename TR::LengthMap LengthMap;
|
hegyi@1123
|
958 |
///The type of the length of the edges.
|
alpar@987
|
959 |
typedef typename LengthMap::Value Value;
|
hegyi@1123
|
960 |
///\brief The type of the map that stores the last
|
hegyi@1123
|
961 |
///edges of the shortest paths.
|
alpar@953
|
962 |
typedef typename TR::PredMap PredMap;
|
hegyi@1123
|
963 |
///The type of the map that stores the dists of the nodes.
|
alpar@953
|
964 |
typedef typename TR::DistMap DistMap;
|
hegyi@1123
|
965 |
///The heap type used by the dijkstra algorithm.
|
alpar@953
|
966 |
typedef typename TR::Heap Heap;
|
alpar@1116
|
967 |
public:
|
hegyi@1123
|
968 |
/// Constructor.
|
alpar@1116
|
969 |
DijkstraWizard() : TR() {}
|
alpar@953
|
970 |
|
hegyi@1123
|
971 |
/// Constructor that requires parameters.
|
hegyi@1124
|
972 |
|
hegyi@1124
|
973 |
/// Constructor that requires parameters.
|
hegyi@1123
|
974 |
/// These parameters will be the default values for the traits class.
|
alpar@1116
|
975 |
DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
|
alpar@1116
|
976 |
TR(g,l,s) {}
|
alpar@953
|
977 |
|
hegyi@1123
|
978 |
///Copy constructor
|
alpar@1116
|
979 |
DijkstraWizard(const TR &b) : TR(b) {}
|
alpar@953
|
980 |
|
alpar@1116
|
981 |
~DijkstraWizard() {}
|
alpar@1116
|
982 |
|
hegyi@1123
|
983 |
///Runs Dijkstra algorithm from a given node.
|
hegyi@1123
|
984 |
|
hegyi@1123
|
985 |
///Runs Dijkstra algorithm from a given node.
|
hegyi@1123
|
986 |
///The node can be given by the \ref source function.
|
alpar@1116
|
987 |
void run()
|
alpar@953
|
988 |
{
|
alpar@1201
|
989 |
if(Base::_source==INVALID) throw UninitializedParameter();
|
deba@1193
|
990 |
Dijkstra<Graph,LengthMap,TR>
|
deba@1345
|
991 |
dij(*(Graph*)Base::_g,*(LengthMap*)Base::_length);
|
deba@1345
|
992 |
if(Base::_pred) dij.predMap(*(PredMap*)Base::_pred);
|
deba@1345
|
993 |
if(Base::_dist) dij.distMap(*(DistMap*)Base::_dist);
|
deba@1345
|
994 |
dij.run(Base::_source);
|
alpar@1116
|
995 |
}
|
alpar@1116
|
996 |
|
hegyi@1124
|
997 |
///Runs Dijkstra algorithm from the given node.
|
hegyi@1123
|
998 |
|
hegyi@1124
|
999 |
///Runs Dijkstra algorithm from the given node.
|
hegyi@1123
|
1000 |
///\param s is the given source.
|
alpar@1116
|
1001 |
void run(Node s)
|
alpar@1116
|
1002 |
{
|
alpar@1201
|
1003 |
Base::_source=s;
|
alpar@1116
|
1004 |
run();
|
alpar@953
|
1005 |
}
|
alpar@953
|
1006 |
|
alpar@953
|
1007 |
template<class T>
|
alpar@1116
|
1008 |
struct DefPredMapBase : public Base {
|
alpar@1116
|
1009 |
typedef T PredMap;
|
alpar@1367
|
1010 |
static PredMap *createPredMap(const Graph &) { return 0; };
|
alpar@1236
|
1011 |
DefPredMapBase(const TR &b) : TR(b) {}
|
alpar@1116
|
1012 |
};
|
alpar@953
|
1013 |
|
alpar@1156
|
1014 |
///\brief \ref named-templ-param "Named parameter"
|
alpar@1156
|
1015 |
///function for setting PredMap type
|
alpar@1156
|
1016 |
///
|
alpar@1156
|
1017 |
/// \ref named-templ-param "Named parameter"
|
alpar@1156
|
1018 |
///function for setting PredMap type
|
hegyi@1124
|
1019 |
///
|
alpar@953
|
1020 |
template<class T>
|
alpar@1116
|
1021 |
DijkstraWizard<DefPredMapBase<T> > predMap(const T &t)
|
alpar@953
|
1022 |
{
|
deba@1193
|
1023 |
Base::_pred=(void *)&t;
|
alpar@1116
|
1024 |
return DijkstraWizard<DefPredMapBase<T> >(*this);
|
alpar@953
|
1025 |
}
|
alpar@953
|
1026 |
|
alpar@1116
|
1027 |
template<class T>
|
alpar@1116
|
1028 |
struct DefDistMapBase : public Base {
|
alpar@1116
|
1029 |
typedef T DistMap;
|
alpar@1367
|
1030 |
static DistMap *createDistMap(const Graph &) { return 0; };
|
alpar@1236
|
1031 |
DefDistMapBase(const TR &b) : TR(b) {}
|
alpar@1116
|
1032 |
};
|
alpar@953
|
1033 |
|
alpar@1156
|
1034 |
///\brief \ref named-templ-param "Named parameter"
|
alpar@1156
|
1035 |
///function for setting DistMap type
|
alpar@1156
|
1036 |
///
|
alpar@1156
|
1037 |
/// \ref named-templ-param "Named parameter"
|
alpar@1156
|
1038 |
///function for setting DistMap type
|
hegyi@1124
|
1039 |
///
|
alpar@953
|
1040 |
template<class T>
|
alpar@1116
|
1041 |
DijkstraWizard<DefDistMapBase<T> > distMap(const T &t)
|
alpar@953
|
1042 |
{
|
deba@1193
|
1043 |
Base::_dist=(void *)&t;
|
alpar@1116
|
1044 |
return DijkstraWizard<DefDistMapBase<T> >(*this);
|
alpar@953
|
1045 |
}
|
alpar@1117
|
1046 |
|
hegyi@1123
|
1047 |
/// Sets the source node, from which the Dijkstra algorithm runs.
|
hegyi@1123
|
1048 |
|
hegyi@1123
|
1049 |
/// Sets the source node, from which the Dijkstra algorithm runs.
|
hegyi@1123
|
1050 |
/// \param s is the source node.
|
alpar@1117
|
1051 |
DijkstraWizard<TR> &source(Node s)
|
alpar@953
|
1052 |
{
|
alpar@1201
|
1053 |
Base::_source=s;
|
alpar@953
|
1054 |
return *this;
|
alpar@953
|
1055 |
}
|
alpar@953
|
1056 |
|
alpar@953
|
1057 |
};
|
alpar@255
|
1058 |
|
alpar@1218
|
1059 |
///Function type interface for Dijkstra algorithm.
|
alpar@953
|
1060 |
|
alpar@1151
|
1061 |
/// \ingroup flowalgs
|
alpar@1218
|
1062 |
///Function type interface for Dijkstra algorithm.
|
alpar@953
|
1063 |
///
|
alpar@1218
|
1064 |
///This function also has several
|
alpar@1218
|
1065 |
///\ref named-templ-func-param "named parameters",
|
alpar@1218
|
1066 |
///they are declared as the members of class \ref DijkstraWizard.
|
alpar@1218
|
1067 |
///The following
|
alpar@1218
|
1068 |
///example shows how to use these parameters.
|
alpar@1218
|
1069 |
///\code
|
alpar@1218
|
1070 |
/// dijkstra(g,length,source).predMap(preds).run();
|
alpar@1218
|
1071 |
///\endcode
|
alpar@1218
|
1072 |
///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
|
alpar@1218
|
1073 |
///to the end of the parameter list.
|
alpar@1218
|
1074 |
///\sa DijkstraWizard
|
alpar@1218
|
1075 |
///\sa Dijkstra
|
alpar@953
|
1076 |
template<class GR, class LM>
|
alpar@1116
|
1077 |
DijkstraWizard<DijkstraWizardBase<GR,LM> >
|
alpar@1116
|
1078 |
dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
|
alpar@953
|
1079 |
{
|
alpar@1116
|
1080 |
return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
|
alpar@953
|
1081 |
}
|
alpar@953
|
1082 |
|
alpar@921
|
1083 |
} //END OF NAMESPACE LEMON
|
alpar@255
|
1084 |
|
alpar@255
|
1085 |
#endif
|