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