22 ///\brief Dijkstra algorithm. |
22 ///\brief Dijkstra algorithm. |
23 |
23 |
24 #include <lemon/list_graph.h> |
24 #include <lemon/list_graph.h> |
25 #include <lemon/bin_heap.h> |
25 #include <lemon/bin_heap.h> |
26 #include <lemon/invalid.h> |
26 #include <lemon/invalid.h> |
|
27 #include <lemon/error.h> |
|
28 #include <lemon/maps.h> |
27 |
29 |
28 namespace lemon { |
30 namespace lemon { |
|
31 |
|
32 |
|
33 class UninitializedData : public LogicError {}; |
|
34 |
29 |
35 |
30 /// \addtogroup flowalgs |
36 /// \addtogroup flowalgs |
31 /// @{ |
37 /// @{ |
32 |
38 |
33 ///Default traits class of Dijkstra class. |
39 ///Default traits class of Dijkstra class. |
83 ///\todo Please document... |
89 ///\todo Please document... |
84 /// |
90 /// |
85 static PredNodeMap *createPredNodeMap(const GR &G) |
91 static PredNodeMap *createPredNodeMap(const GR &G) |
86 { |
92 { |
87 return new PredNodeMap(G); |
93 return new PredNodeMap(G); |
|
94 } |
|
95 |
|
96 ///The type of the map that stores whether a nodes is reached. |
|
97 |
|
98 ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
|
99 ///By default it is a NullMap. |
|
100 ///\todo If it is set to a real map, Dijkstra::reached() should read this. |
|
101 ///\todo named parameter to set this type, function to read and write. |
|
102 typedef NullMap<typename Graph::Node,bool> ReachedMap; |
|
103 ///Instantiates a ReachedMap. |
|
104 |
|
105 ///\todo Please document... |
|
106 /// |
|
107 static ReachedMap *createReachedMap(const GR &G) |
|
108 { |
|
109 return new ReachedMap(); |
88 } |
110 } |
89 ///The type of the map that stores the dists of the nodes. |
111 ///The type of the map that stores the dists of the nodes. |
90 |
112 |
91 ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
113 ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
92 /// |
114 /// |
165 ///edges of the shortest paths. |
190 ///edges of the shortest paths. |
166 typedef typename TR::PredMap PredMap; |
191 typedef typename TR::PredMap PredMap; |
167 ///\brief The type of the map that stores the last but one |
192 ///\brief The type of the map that stores the last but one |
168 ///nodes of the shortest paths. |
193 ///nodes of the shortest paths. |
169 typedef typename TR::PredNodeMap PredNodeMap; |
194 typedef typename TR::PredNodeMap PredNodeMap; |
|
195 ///The type of the map indicating if a node is reached. |
|
196 typedef typename TR::ReachedMap ReachedMap; |
170 ///The type of the map that stores the dists of the nodes. |
197 ///The type of the map that stores the dists of the nodes. |
171 typedef typename TR::DistMap DistMap; |
198 typedef typename TR::DistMap DistMap; |
172 ///The heap type used by the dijkstra algorithm. |
199 ///The heap type used by the dijkstra algorithm. |
173 typedef typename TR::Heap Heap; |
200 typedef typename TR::Heap Heap; |
174 private: |
201 private: |
175 /// Pointer to the underlying graph. |
202 /// Pointer to the underlying graph. |
176 const Graph *G; |
203 const Graph *G; |
177 /// Pointer to the length map |
204 /// Pointer to the length map |
178 const LengthMap *length; |
205 const LengthMap *length; |
179 ///Pointer to the map of predecessors edges. |
206 ///Pointer to the map of predecessors edges. |
180 PredMap *predecessor; |
207 PredMap *_pred; |
181 ///Indicates if \ref predecessor is locally allocated (\c true) or not. |
208 ///Indicates if \ref _pred is locally allocated (\c true) or not. |
182 bool local_predecessor; |
209 bool local_pred; |
183 ///Pointer to the map of predecessors nodes. |
210 ///Pointer to the map of predecessors nodes. |
184 PredNodeMap *pred_node; |
211 PredNodeMap *pred_node; |
185 ///Indicates if \ref pred_node is locally allocated (\c true) or not. |
212 ///Indicates if \ref pred_node is locally allocated (\c true) or not. |
186 bool local_pred_node; |
213 bool local_pred_node; |
187 ///Pointer to the map of distances. |
214 ///Pointer to the map of distances. |
188 DistMap *distance; |
215 DistMap *distance; |
189 ///Indicates if \ref distance is locally allocated (\c true) or not. |
216 ///Indicates if \ref distance is locally allocated (\c true) or not. |
190 bool local_distance; |
217 bool local_distance; |
|
218 ///Pointer to the map of reached status of the nodes. |
|
219 ReachedMap *_reached; |
|
220 ///Indicates if \ref _reached is locally allocated (\c true) or not. |
|
221 bool local_reached; |
191 |
222 |
192 ///The source node of the last execution. |
223 ///The source node of the last execution. |
193 Node source; |
224 Node source; |
194 |
225 |
195 ///Initializes the maps. |
226 ///Initializes the maps. |
196 |
227 |
197 ///\todo Error if \c G or are \c NULL. What about \c length? |
228 ///\todo Error if \c G or are \c NULL. What about \c length? |
198 ///\todo Better memory allocation (instead of new). |
229 ///\todo Better memory allocation (instead of new). |
199 void init_maps() |
230 void init_maps() |
200 { |
231 { |
201 if(!predecessor) { |
232 if(!_pred) { |
202 local_predecessor = true; |
233 local_pred = true; |
203 predecessor = Traits::createPredMap(*G); |
234 _pred = Traits::createPredMap(*G); |
204 } |
235 } |
205 if(!pred_node) { |
236 if(!pred_node) { |
206 local_pred_node = true; |
237 local_pred_node = true; |
207 pred_node = Traits::createPredNodeMap(*G); |
238 pred_node = Traits::createPredNodeMap(*G); |
208 } |
239 } |
209 if(!distance) { |
240 if(!distance) { |
210 local_distance = true; |
241 local_distance = true; |
211 distance = Traits::createDistMap(*G); |
242 distance = Traits::createDistMap(*G); |
|
243 } |
|
244 if(!_reached) { |
|
245 local_reached = true; |
|
246 _reached = Traits::createReachedMap(*G); |
212 } |
247 } |
213 } |
248 } |
214 |
249 |
215 public : |
250 public : |
216 |
251 |
281 |
310 |
282 ///\param _G the graph the algorithm will run on. |
311 ///\param _G the graph the algorithm will run on. |
283 ///\param _length the length map used by the algorithm. |
312 ///\param _length the length map used by the algorithm. |
284 Dijkstra(const Graph& _G, const LengthMap& _length) : |
313 Dijkstra(const Graph& _G, const LengthMap& _length) : |
285 G(&_G), length(&_length), |
314 G(&_G), length(&_length), |
286 predecessor(NULL), local_predecessor(false), |
315 _pred(NULL), local_pred(false), |
287 pred_node(NULL), local_pred_node(false), |
316 pred_node(NULL), local_pred_node(false), |
288 distance(NULL), local_distance(false) |
317 distance(NULL), local_distance(false), |
|
318 _reached(NULL), local_reached(false) |
289 { } |
319 { } |
290 |
320 |
291 ///Destructor. |
321 ///Destructor. |
292 ~Dijkstra() |
322 ~Dijkstra() |
293 { |
323 { |
294 if(local_predecessor) delete predecessor; |
324 if(local_pred) delete _pred; |
295 if(local_pred_node) delete pred_node; |
325 if(local_pred_node) delete pred_node; |
296 if(local_distance) delete distance; |
326 if(local_distance) delete distance; |
|
327 if(local_reached) delete _reached; |
297 } |
328 } |
298 |
329 |
299 ///Sets the length map. |
330 ///Sets the length map. |
300 |
331 |
301 ///Sets the length map. |
332 ///Sets the length map. |
384 heap.push(s,0); |
416 heap.push(s,0); |
385 |
417 |
386 while ( !heap.empty() ) { |
418 while ( !heap.empty() ) { |
387 |
419 |
388 Node v=heap.top(); |
420 Node v=heap.top(); |
|
421 _reached->set(v,true); |
389 Value oldvalue=heap[v]; |
422 Value oldvalue=heap[v]; |
390 heap.pop(); |
423 heap.pop(); |
391 distance->set(v, oldvalue); |
424 distance->set(v, oldvalue); |
392 |
425 |
393 |
426 |
394 for(OutEdgeIt e(*G,v); e!=INVALID; ++e) { |
427 for(OutEdgeIt e(*G,v); e!=INVALID; ++e) { |
395 Node w=G->target(e); |
428 Node w=G->target(e); |
396 switch(heap.state(w)) { |
429 switch(heap.state(w)) { |
397 case Heap::PRE_HEAP: |
430 case Heap::PRE_HEAP: |
398 heap.push(w,oldvalue+(*length)[e]); |
431 heap.push(w,oldvalue+(*length)[e]); |
399 predecessor->set(w,e); |
432 _pred->set(w,e); |
400 pred_node->set(w,v); |
433 pred_node->set(w,v); |
401 break; |
434 break; |
402 case Heap::IN_HEAP: |
435 case Heap::IN_HEAP: |
403 if ( oldvalue+(*length)[e] < heap[w] ) { |
436 if ( oldvalue+(*length)[e] < heap[w] ) { |
404 heap.decrease(w, oldvalue+(*length)[e]); |
437 heap.decrease(w, oldvalue+(*length)[e]); |
405 predecessor->set(w,e); |
438 _pred->set(w,e); |
406 pred_node->set(w,v); |
439 pred_node->set(w,v); |
407 } |
440 } |
408 break; |
441 break; |
409 case Heap::POST_HEAP: |
442 case Heap::POST_HEAP: |
410 break; |
443 break; |
429 ///if \c v is unreachable from the root or if \c v=s. The |
462 ///if \c v is unreachable from the root or if \c v=s. The |
430 ///shortest path tree used here is equal to the shortest path tree used in |
463 ///shortest path tree used here is equal to the shortest path tree used in |
431 ///\ref predNode(Node v). \pre \ref run() must be called before using |
464 ///\ref predNode(Node v). \pre \ref run() must be called before using |
432 ///this function. |
465 ///this function. |
433 ///\todo predEdge could be a better name. |
466 ///\todo predEdge could be a better name. |
434 Edge pred(Node v) const { return (*predecessor)[v]; } |
467 Edge pred(Node v) const { return (*_pred)[v]; } |
435 |
468 |
436 ///Returns the 'previous node' of the shortest path tree. |
469 ///Returns the 'previous node' of the shortest path tree. |
437 |
470 |
438 ///For a node \c v it returns the 'previous node' of the shortest path tree, |
471 ///For a node \c v it returns the 'previous node' of the shortest path tree, |
439 ///i.e. it returns the last but one node from a shortest path from the |
472 ///i.e. it returns the last but one node from a shortest path from the |
452 ///Returns a reference to the shortest path tree map. |
485 ///Returns a reference to the shortest path tree map. |
453 |
486 |
454 ///Returns a reference to the NodeMap of the edges of the |
487 ///Returns a reference to the NodeMap of the edges of the |
455 ///shortest path tree. |
488 ///shortest path tree. |
456 ///\pre \ref run() must be called before using this function. |
489 ///\pre \ref run() must be called before using this function. |
457 const PredMap &predMap() const { return *predecessor;} |
490 const PredMap &predMap() const { return *_pred;} |
458 |
491 |
459 ///Returns a reference to the map of nodes of shortest paths. |
492 ///Returns a reference to the map of nodes of shortest paths. |
460 |
493 |
461 ///Returns a reference to the NodeMap of the last but one nodes of the |
494 ///Returns a reference to the NodeMap of the last but one nodes of the |
462 ///shortest path tree. |
495 ///shortest path tree. |
467 |
500 |
468 ///Returns \c true if \c v is reachable from the root. |
501 ///Returns \c true if \c v is reachable from the root. |
469 ///\note The root node is reported to be reached! |
502 ///\note The root node is reported to be reached! |
470 ///\pre \ref run() must be called before using this function. |
503 ///\pre \ref run() must be called before using this function. |
471 /// |
504 /// |
472 bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; } |
505 bool reached(Node v) { return v==source || (*_pred)[v]!=INVALID; } |
473 |
506 |
474 }; |
507 }; |
475 |
508 |
476 template<class GR,class LM> |
509 template<class GR,class LM> |
477 class DijkstraWizardBase : public DijkstraDefaultTraits<GR,LM> |
510 class DijkstraWizardBase : public DijkstraDefaultTraits<GR,LM> |
511 template<class TR> |
544 template<class TR> |
512 class DijkstraWizard : public TR |
545 class DijkstraWizard : public TR |
513 { |
546 { |
514 typedef TR Base; |
547 typedef TR Base; |
515 |
548 |
516 ///The type of the underlying graph. |
549 //The type of the underlying graph. |
517 typedef typename TR::Graph Graph; |
550 typedef typename TR::Graph Graph; |
518 ///\e |
551 //\e |
519 typedef typename Graph::Node Node; |
552 typedef typename Graph::Node Node; |
520 ///\e |
553 //\e |
521 typedef typename Graph::NodeIt NodeIt; |
554 typedef typename Graph::NodeIt NodeIt; |
522 ///\e |
555 //\e |
523 typedef typename Graph::Edge Edge; |
556 typedef typename Graph::Edge Edge; |
524 ///\e |
557 //\e |
525 typedef typename Graph::OutEdgeIt OutEdgeIt; |
558 typedef typename Graph::OutEdgeIt OutEdgeIt; |
526 |
559 |
527 ///The type of the map that stores the edge lengths. |
560 //The type of the map that stores the edge lengths. |
528 typedef typename TR::LengthMap LengthMap; |
561 typedef typename TR::LengthMap LengthMap; |
529 ///The type of the length of the edges. |
562 //The type of the length of the edges. |
530 typedef typename LengthMap::Value Value; |
563 typedef typename LengthMap::Value Value; |
531 ///\brief The type of the map that stores the last |
564 //\brief The type of the map that stores the last |
532 ///edges of the shortest paths. |
565 //edges of the shortest paths. |
533 typedef typename TR::PredMap PredMap; |
566 typedef typename TR::PredMap PredMap; |
534 ///\brief The type of the map that stores the last but one |
567 //\brief The type of the map that stores the last but one |
535 ///nodes of the shortest paths. |
568 //nodes of the shortest paths. |
536 typedef typename TR::PredNodeMap PredNodeMap; |
569 typedef typename TR::PredNodeMap PredNodeMap; |
537 ///The type of the map that stores the dists of the nodes. |
570 //The type of the map that stores the dists of the nodes. |
538 typedef typename TR::DistMap DistMap; |
571 typedef typename TR::DistMap DistMap; |
539 |
572 |
540 ///The heap type used by the dijkstra algorithm. |
573 //The heap type used by the dijkstra algorithm. |
541 typedef typename TR::Heap Heap; |
574 typedef typename TR::Heap Heap; |
542 public: |
575 public: |
543 DijkstraWizard() : TR() {} |
576 DijkstraWizard() : TR() {} |
544 |
577 |
545 DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) : |
578 DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) : |
550 ~DijkstraWizard() {} |
583 ~DijkstraWizard() {} |
551 |
584 |
552 ///\e |
585 ///\e |
553 void run() |
586 void run() |
554 { |
587 { |
|
588 if(_source==0) throw UninitializedData(); |
555 Dijkstra<Graph,LengthMap,TR> Dij(*(Graph*)_g,*(LengthMap*)_length); |
589 Dijkstra<Graph,LengthMap,TR> Dij(*(Graph*)_g,*(LengthMap*)_length); |
556 if(_pred) Dij.predMap(*(PredMap*)_pred); |
590 if(_pred) Dij.predMap(*(PredMap*)_pred); |
557 if(_predNode) Dij.predNodeMap(*(PredNodeMap*)_predNode); |
591 if(_predNode) Dij.predNodeMap(*(PredNodeMap*)_predNode); |
558 if(_dist) Dij.distMap(*(DistMap*)_dist); |
592 if(_dist) Dij.distMap(*(DistMap*)_dist); |
559 Dij.run(*(Node*)_source); |
593 Dij.run(*(Node*)_source); |