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