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