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