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