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