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