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