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@758
|
33 |
/// \addtogroup flowalgs
|
alpar@430
|
34 |
/// @{
|
alpar@430
|
35 |
|
alpar@954
|
36 |
///Default traits class of Dijkstra class.
|
alpar@954
|
37 |
|
alpar@954
|
38 |
///Default traits class of Dijkstra class.
|
alpar@954
|
39 |
///\param GR Graph type.
|
alpar@954
|
40 |
///\param LM Type of length map.
|
alpar@953
|
41 |
template<class GR, class LM>
|
alpar@953
|
42 |
struct DijkstraDefaultTraits
|
alpar@953
|
43 |
{
|
alpar@954
|
44 |
///The graph type the algorithm runs on.
|
alpar@953
|
45 |
typedef GR Graph;
|
alpar@953
|
46 |
///The type of the map that stores the edge lengths.
|
alpar@953
|
47 |
|
hegyi@1124
|
48 |
///The type of the map that stores the edge lengths.
|
alpar@967
|
49 |
///It must meet the \ref concept::ReadMap "ReadMap" concept.
|
alpar@953
|
50 |
typedef LM LengthMap;
|
alpar@954
|
51 |
//The type of the length of the edges.
|
alpar@987
|
52 |
typedef typename LM::Value Value;
|
alpar@954
|
53 |
///The heap type used by Dijkstra algorithm.
|
alpar@967
|
54 |
|
alpar@967
|
55 |
///The heap type used by Dijkstra algorithm.
|
alpar@967
|
56 |
///
|
alpar@967
|
57 |
///\sa BinHeap
|
alpar@967
|
58 |
///\sa Dijkstra
|
alpar@953
|
59 |
typedef BinHeap<typename Graph::Node,
|
alpar@987
|
60 |
typename LM::Value,
|
alpar@953
|
61 |
typename GR::template NodeMap<int>,
|
alpar@987
|
62 |
std::less<Value> > Heap;
|
alpar@953
|
63 |
|
alpar@953
|
64 |
///\brief The type of the map that stores the last
|
alpar@953
|
65 |
///edges of the shortest paths.
|
alpar@953
|
66 |
///
|
hegyi@1124
|
67 |
///The type of the map that stores the last
|
hegyi@1124
|
68 |
///edges of the shortest paths.
|
alpar@967
|
69 |
///It must meet the \ref concept::WriteMap "WriteMap" concept.
|
alpar@953
|
70 |
///
|
alpar@954
|
71 |
typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
|
alpar@954
|
72 |
///Instantiates a PredMap.
|
alpar@953
|
73 |
|
hegyi@1123
|
74 |
///This function instantiates a \ref PredMap.
|
hegyi@1123
|
75 |
///\param G is the graph, to which we would like to define the PredMap.
|
alpar@1119
|
76 |
///\todo The graph alone may be insufficient for the initialization
|
alpar@954
|
77 |
static PredMap *createPredMap(const GR &G)
|
alpar@953
|
78 |
{
|
alpar@953
|
79 |
return new PredMap(G);
|
alpar@953
|
80 |
}
|
alpar@953
|
81 |
///\brief The type of the map that stores the last but one
|
alpar@953
|
82 |
///nodes of the shortest paths.
|
alpar@953
|
83 |
///
|
hegyi@1124
|
84 |
///The type of the map that stores the last but one
|
hegyi@1124
|
85 |
///nodes of the shortest paths.
|
alpar@967
|
86 |
///It must meet the \ref concept::WriteMap "WriteMap" concept.
|
alpar@953
|
87 |
///
|
alpar@954
|
88 |
typedef typename Graph::template NodeMap<typename GR::Node> PredNodeMap;
|
alpar@954
|
89 |
///Instantiates a PredNodeMap.
|
alpar@1125
|
90 |
|
hegyi@1123
|
91 |
///This function instantiates a \ref PredNodeMap.
|
hegyi@1123
|
92 |
///\param G is the graph, to which we would like to define the \ref PredNodeMap
|
alpar@954
|
93 |
static PredNodeMap *createPredNodeMap(const GR &G)
|
alpar@953
|
94 |
{
|
alpar@953
|
95 |
return new PredNodeMap(G);
|
alpar@953
|
96 |
}
|
alpar@1119
|
97 |
|
alpar@1119
|
98 |
///The type of the map that stores whether a nodes is reached.
|
alpar@1119
|
99 |
|
hegyi@1124
|
100 |
///The type of the map that stores whether a nodes is reached.
|
alpar@1119
|
101 |
///It must meet the \ref concept::WriteMap "WriteMap" concept.
|
alpar@1119
|
102 |
///By default it is a NullMap.
|
alpar@1119
|
103 |
///\todo If it is set to a real map, Dijkstra::reached() should read this.
|
alpar@1119
|
104 |
///\todo named parameter to set this type, function to read and write.
|
alpar@1119
|
105 |
typedef NullMap<typename Graph::Node,bool> ReachedMap;
|
alpar@1119
|
106 |
///Instantiates a ReachedMap.
|
alpar@1119
|
107 |
|
hegyi@1123
|
108 |
///This function instantiates a \ref ReachedMap.
|
hegyi@1123
|
109 |
///\param G is the graph, to which 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@255
|
132 |
///This class provides an efficient implementation of %Dijkstra algorithm.
|
alpar@255
|
133 |
///The edge lengths are passed to the algorithm using a
|
klao@959
|
134 |
///\ref concept::ReadMap "ReadMap",
|
alpar@255
|
135 |
///so it is easy to change it to any kind of length.
|
alpar@255
|
136 |
///
|
alpar@880
|
137 |
///The type of the length is determined by the
|
alpar@987
|
138 |
///\ref concept::ReadMap::Value "Value" of the length map.
|
alpar@255
|
139 |
///
|
alpar@255
|
140 |
///It is also possible to change the underlying priority heap.
|
alpar@255
|
141 |
///
|
alpar@953
|
142 |
///\param GR The graph type the algorithm runs on. The default value is
|
alpar@955
|
143 |
///\ref ListGraph. The value of GR is not used directly by Dijkstra, it
|
alpar@954
|
144 |
///is only passed to \ref DijkstraDefaultTraits.
|
alpar@584
|
145 |
///\param LM This read-only
|
jacint@385
|
146 |
///EdgeMap
|
jacint@385
|
147 |
///determines the
|
jacint@385
|
148 |
///lengths of the edges. It is read once for each edge, so the map
|
jacint@385
|
149 |
///may involve in relatively time consuming process to compute the edge
|
jacint@385
|
150 |
///length if it is necessary. The default map type is
|
klao@959
|
151 |
///\ref concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>".
|
alpar@955
|
152 |
///The value of LM is not used directly by Dijkstra, it
|
alpar@954
|
153 |
///is only passed to \ref DijkstraDefaultTraits.
|
alpar@954
|
154 |
///\param TR Traits class to set various data types used by the algorithm.
|
alpar@954
|
155 |
///The default traits class is
|
alpar@955
|
156 |
///\ref DijkstraDefaultTraits "DijkstraDefaultTraits<GR,LM>".
|
alpar@954
|
157 |
///See \ref DijkstraDefaultTraits for the documentation of
|
alpar@954
|
158 |
///a Dijkstra traits class.
|
alpar@456
|
159 |
///
|
alpar@689
|
160 |
///\author Jacint Szabo and Alpar Juttner
|
alpar@693
|
161 |
///\todo We need a typedef-names should be standardized. (-:
|
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@1125
|
183 |
return "lemon::Dijsktra::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@693
|
206 |
///\brief The type of the map that stores the last but one
|
alpar@584
|
207 |
///nodes of the shortest paths.
|
alpar@953
|
208 |
typedef typename TR::PredNodeMap PredNodeMap;
|
alpar@1119
|
209 |
///The type of the map indicating if a node is reached.
|
alpar@1119
|
210 |
typedef typename TR::ReachedMap ReachedMap;
|
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@802
|
224 |
///Pointer to the map of predecessors nodes.
|
alpar@688
|
225 |
PredNodeMap *pred_node;
|
alpar@802
|
226 |
///Indicates if \ref pred_node is locally allocated (\c true) or not.
|
alpar@688
|
227 |
bool local_pred_node;
|
alpar@802
|
228 |
///Pointer to the map of distances.
|
alpar@688
|
229 |
DistMap *distance;
|
alpar@802
|
230 |
///Indicates if \ref distance is locally allocated (\c true) or not.
|
alpar@688
|
231 |
bool local_distance;
|
alpar@1119
|
232 |
///Pointer to the map of reached status of the nodes.
|
alpar@1119
|
233 |
ReachedMap *_reached;
|
alpar@1119
|
234 |
///Indicates if \ref _reached is locally allocated (\c true) or not.
|
alpar@1119
|
235 |
bool local_reached;
|
alpar@688
|
236 |
|
alpar@802
|
237 |
///The source node of the last execution.
|
alpar@774
|
238 |
Node source;
|
alpar@774
|
239 |
|
alpar@785
|
240 |
///Initializes the maps.
|
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@688
|
244 |
void init_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@688
|
250 |
if(!pred_node) {
|
alpar@688
|
251 |
local_pred_node = true;
|
alpar@953
|
252 |
pred_node = Traits::createPredNodeMap(*G);
|
alpar@688
|
253 |
}
|
alpar@688
|
254 |
if(!distance) {
|
alpar@688
|
255 |
local_distance = true;
|
alpar@953
|
256 |
distance = Traits::createDistMap(*G);
|
alpar@688
|
257 |
}
|
alpar@1119
|
258 |
if(!_reached) {
|
alpar@1119
|
259 |
local_reached = true;
|
alpar@1119
|
260 |
_reached = Traits::createReachedMap(*G);
|
alpar@1119
|
261 |
}
|
alpar@688
|
262 |
}
|
alpar@255
|
263 |
|
alpar@255
|
264 |
public :
|
alpar@1116
|
265 |
|
alpar@953
|
266 |
template <class T>
|
alpar@1116
|
267 |
struct DefPredMapTraits : public Traits {
|
alpar@953
|
268 |
typedef T PredMap;
|
alpar@953
|
269 |
///\todo An exception should be thrown.
|
alpar@953
|
270 |
///
|
alpar@953
|
271 |
static PredMap *createPredMap(const Graph &G)
|
alpar@953
|
272 |
{
|
alpar@1119
|
273 |
throw UninitializedData();
|
alpar@953
|
274 |
}
|
alpar@953
|
275 |
};
|
alpar@954
|
276 |
///\ref named-templ-param "Named parameter" for setting PredMap type
|
alpar@954
|
277 |
|
alpar@954
|
278 |
///\ref named-templ-param "Named parameter" for setting PredMap type
|
alpar@1043
|
279 |
///
|
alpar@953
|
280 |
template <class T>
|
alpar@1116
|
281 |
class DefPredMap : public Dijkstra< Graph,
|
alpar@953
|
282 |
LengthMap,
|
alpar@1116
|
283 |
DefPredMapTraits<T> > { };
|
alpar@953
|
284 |
|
alpar@953
|
285 |
template <class T>
|
alpar@1116
|
286 |
struct DefPredNodeMapTraits : public Traits {
|
alpar@953
|
287 |
typedef T PredNodeMap;
|
alpar@953
|
288 |
///\todo An exception should be thrown.
|
alpar@953
|
289 |
///
|
alpar@953
|
290 |
static PredNodeMap *createPredNodeMap(const Graph &G)
|
alpar@953
|
291 |
{
|
alpar@1119
|
292 |
throw UninitializedData();
|
alpar@953
|
293 |
}
|
alpar@953
|
294 |
};
|
alpar@954
|
295 |
///\ref named-templ-param "Named parameter" for setting PredNodeMap type
|
alpar@954
|
296 |
|
alpar@954
|
297 |
///\ref named-templ-param "Named parameter" for setting PredNodeMap type
|
alpar@1043
|
298 |
///
|
alpar@953
|
299 |
template <class T>
|
alpar@1116
|
300 |
class DefPredNodeMap : public Dijkstra< Graph,
|
alpar@953
|
301 |
LengthMap,
|
alpar@1116
|
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 |
///\todo An exception should be thrown.
|
alpar@953
|
308 |
///
|
alpar@953
|
309 |
static DistMap *createDistMap(const Graph &G)
|
alpar@953
|
310 |
{
|
alpar@1119
|
311 |
throw UninitializedData();
|
alpar@953
|
312 |
}
|
alpar@953
|
313 |
};
|
alpar@954
|
314 |
///\ref named-templ-param "Named parameter" for setting DistMap type
|
alpar@954
|
315 |
|
alpar@954
|
316 |
///\ref named-templ-param "Named parameter" for setting DistMap type
|
alpar@1043
|
317 |
///
|
alpar@953
|
318 |
template <class T>
|
alpar@1116
|
319 |
class DefDistMap : public Dijkstra< Graph,
|
alpar@953
|
320 |
LengthMap,
|
alpar@1116
|
321 |
DefDistMapTraits<T> > { };
|
alpar@953
|
322 |
|
alpar@802
|
323 |
///Constructor.
|
alpar@255
|
324 |
|
alpar@802
|
325 |
///\param _G the graph the algorithm will run on.
|
alpar@802
|
326 |
///\param _length the length map used by the algorithm.
|
alpar@954
|
327 |
Dijkstra(const Graph& _G, const LengthMap& _length) :
|
alpar@688
|
328 |
G(&_G), length(&_length),
|
alpar@1119
|
329 |
_pred(NULL), local_pred(false),
|
alpar@707
|
330 |
pred_node(NULL), local_pred_node(false),
|
alpar@1119
|
331 |
distance(NULL), local_distance(false),
|
alpar@1119
|
332 |
_reached(NULL), local_reached(false)
|
alpar@688
|
333 |
{ }
|
alpar@688
|
334 |
|
alpar@802
|
335 |
///Destructor.
|
alpar@688
|
336 |
~Dijkstra()
|
alpar@688
|
337 |
{
|
alpar@1119
|
338 |
if(local_pred) delete _pred;
|
alpar@688
|
339 |
if(local_pred_node) delete pred_node;
|
alpar@688
|
340 |
if(local_distance) delete distance;
|
alpar@1119
|
341 |
if(local_reached) delete _reached;
|
alpar@688
|
342 |
}
|
alpar@688
|
343 |
|
alpar@688
|
344 |
///Sets the length map.
|
alpar@688
|
345 |
|
alpar@688
|
346 |
///Sets the length map.
|
alpar@688
|
347 |
///\return <tt> (*this) </tt>
|
alpar@1116
|
348 |
Dijkstra &lengthMap(const LengthMap &m)
|
alpar@688
|
349 |
{
|
alpar@688
|
350 |
length = &m;
|
alpar@688
|
351 |
return *this;
|
alpar@688
|
352 |
}
|
alpar@688
|
353 |
|
alpar@688
|
354 |
///Sets the map storing the predecessor edges.
|
alpar@688
|
355 |
|
alpar@688
|
356 |
///Sets the map storing the predecessor edges.
|
alpar@688
|
357 |
///If you don't use this function before calling \ref run(),
|
alpar@688
|
358 |
///it will allocate one. The destuctor deallocates this
|
alpar@688
|
359 |
///automatically allocated map, of course.
|
alpar@688
|
360 |
///\return <tt> (*this) </tt>
|
alpar@1116
|
361 |
Dijkstra &predMap(PredMap &m)
|
alpar@688
|
362 |
{
|
alpar@1119
|
363 |
if(local_pred) {
|
alpar@1119
|
364 |
delete _pred;
|
alpar@1119
|
365 |
local_pred=false;
|
alpar@688
|
366 |
}
|
alpar@1119
|
367 |
_pred = &m;
|
alpar@688
|
368 |
return *this;
|
alpar@688
|
369 |
}
|
alpar@688
|
370 |
|
alpar@688
|
371 |
///Sets the map storing the predecessor nodes.
|
alpar@688
|
372 |
|
alpar@688
|
373 |
///Sets the map storing the predecessor nodes.
|
alpar@688
|
374 |
///If you don't use this function before calling \ref run(),
|
alpar@688
|
375 |
///it will allocate one. The destuctor deallocates this
|
alpar@688
|
376 |
///automatically allocated map, of course.
|
alpar@688
|
377 |
///\return <tt> (*this) </tt>
|
alpar@1116
|
378 |
Dijkstra &predNodeMap(PredNodeMap &m)
|
alpar@688
|
379 |
{
|
alpar@688
|
380 |
if(local_pred_node) {
|
alpar@688
|
381 |
delete pred_node;
|
alpar@688
|
382 |
local_pred_node=false;
|
alpar@688
|
383 |
}
|
alpar@688
|
384 |
pred_node = &m;
|
alpar@688
|
385 |
return *this;
|
alpar@688
|
386 |
}
|
alpar@688
|
387 |
|
alpar@688
|
388 |
///Sets the map storing the distances calculated by the algorithm.
|
alpar@688
|
389 |
|
alpar@688
|
390 |
///Sets the map storing the distances calculated by the algorithm.
|
alpar@688
|
391 |
///If you don't use this function before calling \ref run(),
|
alpar@688
|
392 |
///it will allocate one. The destuctor deallocates this
|
alpar@688
|
393 |
///automatically allocated map, of course.
|
alpar@688
|
394 |
///\return <tt> (*this) </tt>
|
alpar@1116
|
395 |
Dijkstra &distMap(DistMap &m)
|
alpar@688
|
396 |
{
|
alpar@688
|
397 |
if(local_distance) {
|
alpar@688
|
398 |
delete distance;
|
alpar@688
|
399 |
local_distance=false;
|
alpar@688
|
400 |
}
|
alpar@688
|
401 |
distance = &m;
|
alpar@688
|
402 |
return *this;
|
alpar@688
|
403 |
}
|
alpar@255
|
404 |
|
alpar@694
|
405 |
///Runs %Dijkstra algorithm from node \c s.
|
alpar@694
|
406 |
|
alpar@694
|
407 |
///This method runs the %Dijkstra algorithm from a root node \c s
|
alpar@694
|
408 |
///in order to
|
alpar@694
|
409 |
///compute the
|
alpar@694
|
410 |
///shortest path to each node. The algorithm computes
|
alpar@694
|
411 |
///- The shortest path tree.
|
alpar@694
|
412 |
///- The distance of each node from the root.
|
alpar@954
|
413 |
///\todo heap_map's type could also be in the traits class.
|
alpar@694
|
414 |
void run(Node s) {
|
alpar@694
|
415 |
|
alpar@694
|
416 |
init_maps();
|
alpar@694
|
417 |
|
alpar@774
|
418 |
source = s;
|
alpar@774
|
419 |
|
alpar@774
|
420 |
for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
|
alpar@1119
|
421 |
_pred->set(u,INVALID);
|
alpar@694
|
422 |
pred_node->set(u,INVALID);
|
alpar@1119
|
423 |
///\todo *_reached is not set to false.
|
alpar@694
|
424 |
}
|
alpar@694
|
425 |
|
alpar@954
|
426 |
typename Graph::template NodeMap<int> heap_map(*G,-1);
|
alpar@694
|
427 |
|
alpar@953
|
428 |
Heap heap(heap_map);
|
alpar@694
|
429 |
|
alpar@694
|
430 |
heap.push(s,0);
|
alpar@694
|
431 |
|
alpar@694
|
432 |
while ( !heap.empty() ) {
|
alpar@694
|
433 |
|
alpar@694
|
434 |
Node v=heap.top();
|
alpar@1119
|
435 |
_reached->set(v,true);
|
alpar@987
|
436 |
Value oldvalue=heap[v];
|
alpar@694
|
437 |
heap.pop();
|
alpar@694
|
438 |
distance->set(v, oldvalue);
|
alpar@694
|
439 |
|
alpar@694
|
440 |
|
alpar@774
|
441 |
for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
|
alpar@986
|
442 |
Node w=G->target(e);
|
alpar@694
|
443 |
switch(heap.state(w)) {
|
alpar@953
|
444 |
case Heap::PRE_HEAP:
|
alpar@694
|
445 |
heap.push(w,oldvalue+(*length)[e]);
|
alpar@1119
|
446 |
_pred->set(w,e);
|
alpar@694
|
447 |
pred_node->set(w,v);
|
alpar@694
|
448 |
break;
|
alpar@953
|
449 |
case Heap::IN_HEAP:
|
alpar@694
|
450 |
if ( oldvalue+(*length)[e] < heap[w] ) {
|
alpar@694
|
451 |
heap.decrease(w, oldvalue+(*length)[e]);
|
alpar@1119
|
452 |
_pred->set(w,e);
|
alpar@694
|
453 |
pred_node->set(w,v);
|
alpar@694
|
454 |
}
|
alpar@694
|
455 |
break;
|
alpar@953
|
456 |
case Heap::POST_HEAP:
|
alpar@694
|
457 |
break;
|
alpar@694
|
458 |
}
|
alpar@694
|
459 |
}
|
alpar@694
|
460 |
}
|
alpar@694
|
461 |
}
|
alpar@255
|
462 |
|
jacint@385
|
463 |
///The distance of a node from the root.
|
alpar@255
|
464 |
|
jacint@385
|
465 |
///Returns the distance of a node from the root.
|
alpar@255
|
466 |
///\pre \ref run() must be called before using this function.
|
jacint@385
|
467 |
///\warning If node \c v in unreachable from the root the return value
|
alpar@255
|
468 |
///of this funcion is undefined.
|
alpar@987
|
469 |
Value dist(Node v) const { return (*distance)[v]; }
|
jacint@373
|
470 |
|
alpar@584
|
471 |
///Returns the 'previous edge' of the shortest path tree.
|
alpar@255
|
472 |
|
alpar@584
|
473 |
///For a node \c v it returns the 'previous edge' of the shortest path tree,
|
alpar@785
|
474 |
///i.e. it returns the last edge of a shortest path from the root to \c
|
alpar@688
|
475 |
///v. It is \ref INVALID
|
alpar@688
|
476 |
///if \c v is unreachable from the root or if \c v=s. The
|
jacint@385
|
477 |
///shortest path tree used here is equal to the shortest path tree used in
|
jacint@385
|
478 |
///\ref predNode(Node v). \pre \ref run() must be called before using
|
jacint@385
|
479 |
///this function.
|
alpar@780
|
480 |
///\todo predEdge could be a better name.
|
alpar@1119
|
481 |
Edge pred(Node v) const { return (*_pred)[v]; }
|
jacint@373
|
482 |
|
alpar@584
|
483 |
///Returns the 'previous node' of the shortest path tree.
|
alpar@255
|
484 |
|
alpar@584
|
485 |
///For a node \c v it returns the 'previous node' of the shortest path tree,
|
jacint@385
|
486 |
///i.e. it returns the last but one node from a shortest path from the
|
jacint@385
|
487 |
///root to \c /v. It is INVALID if \c v is unreachable from the root or if
|
jacint@385
|
488 |
///\c v=s. The shortest path tree used here is equal to the shortest path
|
jacint@385
|
489 |
///tree used in \ref pred(Node v). \pre \ref run() must be called before
|
jacint@385
|
490 |
///using this function.
|
alpar@688
|
491 |
Node predNode(Node v) const { return (*pred_node)[v]; }
|
alpar@255
|
492 |
|
alpar@255
|
493 |
///Returns a reference to the NodeMap of distances.
|
alpar@255
|
494 |
|
jacint@385
|
495 |
///Returns a reference to the NodeMap of distances. \pre \ref run() must
|
jacint@385
|
496 |
///be called before using this function.
|
alpar@688
|
497 |
const DistMap &distMap() const { return *distance;}
|
jacint@385
|
498 |
|
alpar@255
|
499 |
///Returns a reference to the shortest path tree map.
|
alpar@255
|
500 |
|
alpar@255
|
501 |
///Returns a reference to the NodeMap of the edges of the
|
alpar@255
|
502 |
///shortest path tree.
|
alpar@255
|
503 |
///\pre \ref run() must be called before using this function.
|
alpar@1119
|
504 |
const PredMap &predMap() const { return *_pred;}
|
jacint@385
|
505 |
|
jacint@385
|
506 |
///Returns a reference to the map of nodes of shortest paths.
|
alpar@255
|
507 |
|
alpar@255
|
508 |
///Returns a reference to the NodeMap of the last but one nodes of the
|
jacint@385
|
509 |
///shortest path tree.
|
alpar@255
|
510 |
///\pre \ref run() must be called before using this function.
|
alpar@688
|
511 |
const PredNodeMap &predNodeMap() const { return *pred_node;}
|
alpar@255
|
512 |
|
jacint@385
|
513 |
///Checks if a node is reachable from the root.
|
alpar@255
|
514 |
|
jacint@385
|
515 |
///Returns \c true if \c v is reachable from the root.
|
alpar@802
|
516 |
///\note The root node is reported to be reached!
|
alpar@255
|
517 |
///\pre \ref run() must be called before using this function.
|
jacint@385
|
518 |
///
|
alpar@1119
|
519 |
bool reached(Node v) { return v==source || (*_pred)[v]!=INVALID; }
|
alpar@255
|
520 |
|
alpar@255
|
521 |
};
|
alpar@953
|
522 |
|
hegyi@1123
|
523 |
/// Default traits used by \ref DijkstraWizard
|
hegyi@1123
|
524 |
|
hegyi@1124
|
525 |
/// To make it easier to use Dijkstra algorithm we have created a wizard class.
|
hegyi@1124
|
526 |
/// This \ref DijkstraWizard class needs default traits, as well as the \ref Dijkstra class.
|
hegyi@1123
|
527 |
/// The \ref DijkstraWizardBase is a class to be the default traits of the
|
hegyi@1123
|
528 |
/// \ref DijkstraWizard class.
|
alpar@1116
|
529 |
template<class GR,class LM>
|
alpar@1116
|
530 |
class DijkstraWizardBase : public DijkstraDefaultTraits<GR,LM>
|
alpar@1116
|
531 |
{
|
alpar@1116
|
532 |
|
alpar@1116
|
533 |
typedef DijkstraDefaultTraits<GR,LM> Base;
|
alpar@1116
|
534 |
protected:
|
alpar@1116
|
535 |
/// Pointer to the underlying graph.
|
alpar@1116
|
536 |
void *_g;
|
alpar@1116
|
537 |
/// Pointer to the length map
|
alpar@1116
|
538 |
void *_length;
|
alpar@1116
|
539 |
///Pointer to the map of predecessors edges.
|
alpar@1116
|
540 |
void *_pred;
|
alpar@1116
|
541 |
///Pointer to the map of predecessors nodes.
|
alpar@1116
|
542 |
void *_predNode;
|
alpar@1116
|
543 |
///Pointer to the map of distances.
|
alpar@1116
|
544 |
void *_dist;
|
alpar@1116
|
545 |
///Pointer to the source node.
|
alpar@1116
|
546 |
void *_source;
|
alpar@1116
|
547 |
|
hegyi@1123
|
548 |
/// Type of the nodes in the graph.
|
alpar@1116
|
549 |
typedef typename Base::Graph::Node Node;
|
alpar@1116
|
550 |
|
alpar@1116
|
551 |
public:
|
hegyi@1123
|
552 |
/// Constructor.
|
hegyi@1123
|
553 |
|
hegyi@1123
|
554 |
/// This constructor does not require parameters, therefore it initiates
|
hegyi@1123
|
555 |
/// all of the attributes to default values (0, INVALID).
|
alpar@1116
|
556 |
DijkstraWizardBase() : _g(0), _length(0), _pred(0), _predNode(0),
|
alpar@1116
|
557 |
_dist(0), _source(INVALID) {}
|
alpar@1116
|
558 |
|
hegyi@1123
|
559 |
/// Constructor.
|
hegyi@1123
|
560 |
|
hegyi@1123
|
561 |
/// This constructor requires some parameters, listed in the parameters list.
|
hegyi@1123
|
562 |
/// Others are initiated to 0.
|
hegyi@1123
|
563 |
/// \param g is the initial value of \ref _g
|
hegyi@1123
|
564 |
/// \param l is the initial value of \ref _length
|
hegyi@1123
|
565 |
/// \param s is the initial value of \ref _source
|
alpar@1116
|
566 |
DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
|
alpar@1116
|
567 |
_g((void *)&g), _length((void *)&l), _pred(0), _predNode(0),
|
alpar@1116
|
568 |
_dist(0), _source((void *)&s) {}
|
alpar@1116
|
569 |
|
alpar@1116
|
570 |
};
|
alpar@1116
|
571 |
|
hegyi@1123
|
572 |
/// A class to make easier the usage of Dijkstra algorithm
|
alpar@953
|
573 |
|
hegyi@1123
|
574 |
/// This class is created to make it easier to use Dijkstra algorithm.
|
hegyi@1123
|
575 |
/// It uses the functions and features of the plain \ref Dijkstra,
|
hegyi@1123
|
576 |
/// but it is much more simple to use it.
|
alpar@953
|
577 |
///
|
hegyi@1123
|
578 |
/// Simplicity means that the way to change the types defined
|
hegyi@1123
|
579 |
/// in the traits class is based on functions that returns the new class
|
hegyi@1124
|
580 |
/// and not on templatable built-in classes. When using the plain \ref Dijkstra
|
hegyi@1124
|
581 |
/// the new class with the modified type comes from the original class by using the ::
|
hegyi@1124
|
582 |
/// operator. In the case of \ref DijkstraWizard only a function have to be called and it will
|
hegyi@1123
|
583 |
/// return the needed class.
|
hegyi@1123
|
584 |
///
|
hegyi@1123
|
585 |
/// It does not have own \ref run method. When its \ref run method is called
|
hegyi@1123
|
586 |
/// it initiates a plain \ref Dijkstra class, and calls the \ref Dijkstra::run
|
hegyi@1123
|
587 |
/// method of it.
|
alpar@953
|
588 |
template<class TR>
|
alpar@1116
|
589 |
class DijkstraWizard : public TR
|
alpar@953
|
590 |
{
|
alpar@1116
|
591 |
typedef TR Base;
|
alpar@953
|
592 |
|
hegyi@1123
|
593 |
///The type of the underlying graph.
|
alpar@953
|
594 |
typedef typename TR::Graph Graph;
|
alpar@1119
|
595 |
//\e
|
alpar@953
|
596 |
typedef typename Graph::Node Node;
|
alpar@1119
|
597 |
//\e
|
alpar@953
|
598 |
typedef typename Graph::NodeIt NodeIt;
|
alpar@1119
|
599 |
//\e
|
alpar@953
|
600 |
typedef typename Graph::Edge Edge;
|
alpar@1119
|
601 |
//\e
|
alpar@953
|
602 |
typedef typename Graph::OutEdgeIt OutEdgeIt;
|
alpar@953
|
603 |
|
hegyi@1123
|
604 |
///The type of the map that stores the edge lengths.
|
alpar@953
|
605 |
typedef typename TR::LengthMap LengthMap;
|
hegyi@1123
|
606 |
///The type of the length of the edges.
|
alpar@987
|
607 |
typedef typename LengthMap::Value Value;
|
hegyi@1123
|
608 |
///\brief The type of the map that stores the last
|
hegyi@1123
|
609 |
///edges of the shortest paths.
|
alpar@953
|
610 |
typedef typename TR::PredMap PredMap;
|
hegyi@1123
|
611 |
///\brief The type of the map that stores the last but one
|
hegyi@1123
|
612 |
///nodes of the shortest paths.
|
alpar@953
|
613 |
typedef typename TR::PredNodeMap PredNodeMap;
|
hegyi@1123
|
614 |
///The type of the map that stores the dists of the nodes.
|
alpar@953
|
615 |
typedef typename TR::DistMap DistMap;
|
alpar@953
|
616 |
|
hegyi@1123
|
617 |
///The heap type used by the dijkstra algorithm.
|
alpar@953
|
618 |
typedef typename TR::Heap Heap;
|
alpar@1116
|
619 |
public:
|
hegyi@1123
|
620 |
/// Constructor.
|
alpar@1116
|
621 |
DijkstraWizard() : TR() {}
|
alpar@953
|
622 |
|
hegyi@1123
|
623 |
/// Constructor that requires parameters.
|
hegyi@1124
|
624 |
|
hegyi@1124
|
625 |
/// Constructor that requires parameters.
|
hegyi@1123
|
626 |
/// These parameters will be the default values for the traits class.
|
alpar@1116
|
627 |
DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
|
alpar@1116
|
628 |
TR(g,l,s) {}
|
alpar@953
|
629 |
|
hegyi@1123
|
630 |
///Copy constructor
|
alpar@1116
|
631 |
DijkstraWizard(const TR &b) : TR(b) {}
|
alpar@953
|
632 |
|
alpar@1116
|
633 |
~DijkstraWizard() {}
|
alpar@1116
|
634 |
|
hegyi@1123
|
635 |
///Runs Dijkstra algorithm from a given node.
|
hegyi@1123
|
636 |
|
hegyi@1123
|
637 |
///Runs Dijkstra algorithm from a given node.
|
hegyi@1123
|
638 |
///The node can be given by the \ref source function.
|
alpar@1116
|
639 |
void run()
|
alpar@953
|
640 |
{
|
alpar@1119
|
641 |
if(_source==0) throw UninitializedData();
|
alpar@1116
|
642 |
Dijkstra<Graph,LengthMap,TR> Dij(*(Graph*)_g,*(LengthMap*)_length);
|
alpar@1116
|
643 |
if(_pred) Dij.predMap(*(PredMap*)_pred);
|
alpar@1116
|
644 |
if(_predNode) Dij.predNodeMap(*(PredNodeMap*)_predNode);
|
alpar@1116
|
645 |
if(_dist) Dij.distMap(*(DistMap*)_dist);
|
alpar@1116
|
646 |
Dij.run(*(Node*)_source);
|
alpar@1116
|
647 |
}
|
alpar@1116
|
648 |
|
hegyi@1124
|
649 |
///Runs Dijkstra algorithm from the given node.
|
hegyi@1123
|
650 |
|
hegyi@1124
|
651 |
///Runs Dijkstra algorithm from the given node.
|
hegyi@1123
|
652 |
///\param s is the given source.
|
alpar@1116
|
653 |
void run(Node s)
|
alpar@1116
|
654 |
{
|
alpar@1116
|
655 |
_source=(void *)&s;
|
alpar@1116
|
656 |
run();
|
alpar@953
|
657 |
}
|
alpar@953
|
658 |
|
alpar@953
|
659 |
template<class T>
|
alpar@1116
|
660 |
struct DefPredMapBase : public Base {
|
alpar@1116
|
661 |
typedef T PredMap;
|
alpar@1117
|
662 |
static PredMap *createPredMap(const Graph &G) { return 0; };
|
alpar@1117
|
663 |
DefPredMapBase(const Base &b) : Base(b) {}
|
alpar@1116
|
664 |
};
|
alpar@953
|
665 |
|
hegyi@1123
|
666 |
/// \ref named-templ-param "Named parameter" function for setting PredMap type
|
hegyi@1123
|
667 |
|
hegyi@1123
|
668 |
/// \ref named-templ-param "Named parameter" function for setting PredMap type
|
hegyi@1124
|
669 |
///
|
alpar@953
|
670 |
template<class T>
|
alpar@1116
|
671 |
DijkstraWizard<DefPredMapBase<T> > predMap(const T &t)
|
alpar@953
|
672 |
{
|
alpar@1116
|
673 |
_pred=(void *)&t;
|
alpar@1116
|
674 |
return DijkstraWizard<DefPredMapBase<T> >(*this);
|
alpar@953
|
675 |
}
|
alpar@953
|
676 |
|
alpar@1116
|
677 |
|
alpar@953
|
678 |
template<class T>
|
alpar@1116
|
679 |
struct DefPredNodeMapBase : public Base {
|
alpar@1116
|
680 |
typedef T PredNodeMap;
|
alpar@1117
|
681 |
static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; };
|
alpar@1117
|
682 |
DefPredNodeMapBase(const Base &b) : Base(b) {}
|
alpar@1116
|
683 |
};
|
alpar@1116
|
684 |
|
hegyi@1123
|
685 |
/// \ref named-templ-param "Named parameter" function for setting PredNodeMap type
|
hegyi@1123
|
686 |
|
hegyi@1123
|
687 |
/// \ref named-templ-param "Named parameter" function for setting PredNodeMap type
|
hegyi@1124
|
688 |
///
|
alpar@953
|
689 |
template<class T>
|
alpar@1116
|
690 |
DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t)
|
alpar@953
|
691 |
{
|
alpar@1116
|
692 |
_predNode=(void *)&t;
|
alpar@1116
|
693 |
return DijkstraWizard<DefPredNodeMapBase<T> >(*this);
|
alpar@953
|
694 |
}
|
alpar@1116
|
695 |
|
alpar@1116
|
696 |
template<class T>
|
alpar@1116
|
697 |
struct DefDistMapBase : public Base {
|
alpar@1116
|
698 |
typedef T DistMap;
|
alpar@1117
|
699 |
static DistMap *createDistMap(const Graph &G) { return 0; };
|
alpar@1117
|
700 |
DefDistMapBase(const Base &b) : Base(b) {}
|
alpar@1116
|
701 |
};
|
alpar@953
|
702 |
|
hegyi@1123
|
703 |
/// \ref named-templ-param "Named parameter" function for setting DistMap type
|
hegyi@1123
|
704 |
|
hegyi@1123
|
705 |
/// \ref named-templ-param "Named parameter" function for setting DistMap type
|
hegyi@1124
|
706 |
///
|
alpar@953
|
707 |
template<class T>
|
alpar@1116
|
708 |
DijkstraWizard<DefDistMapBase<T> > distMap(const T &t)
|
alpar@953
|
709 |
{
|
alpar@1116
|
710 |
_dist=(void *)&t;
|
alpar@1116
|
711 |
return DijkstraWizard<DefDistMapBase<T> >(*this);
|
alpar@953
|
712 |
}
|
alpar@1117
|
713 |
|
hegyi@1123
|
714 |
/// Sets the source node, from which the Dijkstra algorithm runs.
|
hegyi@1123
|
715 |
|
hegyi@1123
|
716 |
/// Sets the source node, from which the Dijkstra algorithm runs.
|
hegyi@1123
|
717 |
/// \param s is the source node.
|
alpar@1117
|
718 |
DijkstraWizard<TR> &source(Node s)
|
alpar@953
|
719 |
{
|
alpar@1116
|
720 |
source=(void *)&s;
|
alpar@953
|
721 |
return *this;
|
alpar@953
|
722 |
}
|
alpar@953
|
723 |
|
alpar@953
|
724 |
};
|
alpar@255
|
725 |
|
alpar@953
|
726 |
///\e
|
alpar@953
|
727 |
|
alpar@954
|
728 |
///\todo Please document...
|
alpar@953
|
729 |
///
|
alpar@953
|
730 |
template<class GR, class LM>
|
alpar@1116
|
731 |
DijkstraWizard<DijkstraWizardBase<GR,LM> >
|
alpar@1116
|
732 |
dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
|
alpar@953
|
733 |
{
|
alpar@1116
|
734 |
return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
|
alpar@953
|
735 |
}
|
alpar@953
|
736 |
|
alpar@430
|
737 |
/// @}
|
alpar@255
|
738 |
|
alpar@921
|
739 |
} //END OF NAMESPACE LEMON
|
alpar@255
|
740 |
|
alpar@255
|
741 |
#endif
|
alpar@255
|
742 |
|