2 * src/lemon/dijkstra.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
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.
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
17 #ifndef LEMON_DIJKSTRA_H
18 #define LEMON_DIJKSTRA_H
22 ///\brief Dijkstra algorithm.
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>
33 class UninitializedData : public LogicError {};
36 /// \addtogroup flowalgs
39 ///Default traits class of Dijkstra class.
41 ///Default traits class of Dijkstra class.
42 ///\param GR Graph type.
43 ///\param LM Type of length map.
44 template<class GR, class LM>
45 struct DijkstraDefaultTraits
47 ///The graph type the algorithm runs on.
49 ///The type of the map that stores the edge lengths.
51 ///It must meet the \ref concept::ReadMap "ReadMap" concept.
54 //The type of the length of the edges.
55 typedef typename LM::Value Value;
56 ///The heap type used by Dijkstra algorithm.
58 ///The heap type used by Dijkstra algorithm.
62 typedef BinHeap<typename Graph::Node,
64 typename GR::template NodeMap<int>,
65 std::less<Value> > Heap;
67 ///\brief The type of the map that stores the last
68 ///edges of the shortest paths.
70 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
72 typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
73 ///Instantiates a PredMap.
75 ///\todo Please document...
76 ///\todo The graph alone may be insufficient for the initialization
77 static PredMap *createPredMap(const GR &G)
79 return new PredMap(G);
81 ///\brief The type of the map that stores the last but one
82 ///nodes of the shortest paths.
84 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
86 typedef typename Graph::template NodeMap<typename GR::Node> PredNodeMap;
87 ///Instantiates a PredNodeMap.
89 ///\todo Please document...
91 static PredNodeMap *createPredNodeMap(const GR &G)
93 return new PredNodeMap(G);
96 ///The type of the map that stores whether a nodes is reached.
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.
105 ///\todo Please document...
107 static ReachedMap *createReachedMap(const GR &G)
109 return new ReachedMap();
111 ///The type of the map that stores the dists of the nodes.
113 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
115 typedef typename Graph::template NodeMap<typename LM::Value> DistMap;
116 ///Instantiates a DistMap.
118 ///\todo Please document...
120 static DistMap *createDistMap(const GR &G)
122 return new DistMap(G);
126 ///%Dijkstra algorithm class.
128 ///This class provides an efficient implementation of %Dijkstra algorithm.
129 ///The edge lengths are passed to the algorithm using a
130 ///\ref concept::ReadMap "ReadMap",
131 ///so it is easy to change it to any kind of length.
133 ///The type of the length is determined by the
134 ///\ref concept::ReadMap::Value "Value" of the length map.
136 ///It is also possible to change the underlying priority heap.
138 ///\param GR The graph type the algorithm runs on. The default value is
139 ///\ref ListGraph. The value of GR is not used directly by Dijkstra, it
140 ///is only passed to \ref DijkstraDefaultTraits.
141 ///\param LM This read-only
144 ///lengths of the edges. It is read once for each edge, so the map
145 ///may involve in relatively time consuming process to compute the edge
146 ///length if it is necessary. The default map type is
147 ///\ref concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>".
148 ///The value of LM is not used directly by Dijkstra, it
149 ///is only passed to \ref DijkstraDefaultTraits.
150 ///\param TR Traits class to set various data types used by the algorithm.
151 ///The default traits class is
152 ///\ref DijkstraDefaultTraits "DijkstraDefaultTraits<GR,LM>".
153 ///See \ref DijkstraDefaultTraits for the documentation of
154 ///a Dijkstra traits class.
156 ///\author Jacint Szabo and Alpar Juttner
157 ///\todo We need a typedef-names should be standardized. (-:
160 template <typename GR,
164 template <typename GR=ListGraph,
165 typename LM=typename GR::template EdgeMap<int>,
166 typename TR=DijkstraDefaultTraits<GR,LM> >
170 ///Exception thrown by dijkstra.
171 class UninitializedData : public lemon::UninitializedData {};
174 ///The type of the underlying graph.
175 typedef typename TR::Graph Graph;
177 typedef typename Graph::Node Node;
179 typedef typename Graph::NodeIt NodeIt;
181 typedef typename Graph::Edge Edge;
183 typedef typename Graph::OutEdgeIt OutEdgeIt;
185 ///The type of the length of the edges.
186 typedef typename TR::LengthMap::Value Value;
187 ///The type of the map that stores the edge lengths.
188 typedef typename TR::LengthMap LengthMap;
189 ///\brief The type of the map that stores the last
190 ///edges of the shortest paths.
191 typedef typename TR::PredMap PredMap;
192 ///\brief The type of the map that stores the last but one
193 ///nodes of the shortest paths.
194 typedef typename TR::PredNodeMap PredNodeMap;
195 ///The type of the map indicating if a node is reached.
196 typedef typename TR::ReachedMap ReachedMap;
197 ///The type of the map that stores the dists of the nodes.
198 typedef typename TR::DistMap DistMap;
199 ///The heap type used by the dijkstra algorithm.
200 typedef typename TR::Heap Heap;
202 /// Pointer to the underlying graph.
204 /// Pointer to the length map
205 const LengthMap *length;
206 ///Pointer to the map of predecessors edges.
208 ///Indicates if \ref _pred is locally allocated (\c true) or not.
210 ///Pointer to the map of predecessors nodes.
211 PredNodeMap *pred_node;
212 ///Indicates if \ref pred_node is locally allocated (\c true) or not.
213 bool local_pred_node;
214 ///Pointer to the map of distances.
216 ///Indicates if \ref distance is locally allocated (\c true) or not.
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.
223 ///The source node of the last execution.
226 ///Initializes the maps.
228 ///\todo Error if \c G or are \c NULL. What about \c length?
229 ///\todo Better memory allocation (instead of new).
234 _pred = Traits::createPredMap(*G);
237 local_pred_node = true;
238 pred_node = Traits::createPredNodeMap(*G);
241 local_distance = true;
242 distance = Traits::createDistMap(*G);
245 local_reached = true;
246 _reached = Traits::createReachedMap(*G);
253 struct DefPredMapTraits : public Traits {
255 ///\todo An exception should be thrown.
257 static PredMap *createPredMap(const Graph &G)
259 throw UninitializedData();
262 ///\ref named-templ-param "Named parameter" for setting PredMap type
264 ///\ref named-templ-param "Named parameter" for setting PredMap type
267 class DefPredMap : public Dijkstra< Graph,
269 DefPredMapTraits<T> > { };
272 struct DefPredNodeMapTraits : public Traits {
273 typedef T PredNodeMap;
274 ///\todo An exception should be thrown.
276 static PredNodeMap *createPredNodeMap(const Graph &G)
278 throw UninitializedData();
281 ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
283 ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
286 class DefPredNodeMap : public Dijkstra< Graph,
288 DefPredNodeMapTraits<T> > { };
291 struct DefDistMapTraits : public Traits {
293 ///\todo An exception should be thrown.
295 static DistMap *createDistMap(const Graph &G)
297 throw UninitializedData();
300 ///\ref named-templ-param "Named parameter" for setting DistMap type
302 ///\ref named-templ-param "Named parameter" for setting DistMap type
305 class DefDistMap : public Dijkstra< Graph,
307 DefDistMapTraits<T> > { };
311 ///\param _G the graph the algorithm will run on.
312 ///\param _length the length map used by the algorithm.
313 Dijkstra(const Graph& _G, const LengthMap& _length) :
314 G(&_G), length(&_length),
315 _pred(NULL), local_pred(false),
316 pred_node(NULL), local_pred_node(false),
317 distance(NULL), local_distance(false),
318 _reached(NULL), local_reached(false)
324 if(local_pred) delete _pred;
325 if(local_pred_node) delete pred_node;
326 if(local_distance) delete distance;
327 if(local_reached) delete _reached;
330 ///Sets the length map.
332 ///Sets the length map.
333 ///\return <tt> (*this) </tt>
334 Dijkstra &lengthMap(const LengthMap &m)
340 ///Sets the map storing the predecessor edges.
342 ///Sets the map storing the predecessor edges.
343 ///If you don't use this function before calling \ref run(),
344 ///it will allocate one. The destuctor deallocates this
345 ///automatically allocated map, of course.
346 ///\return <tt> (*this) </tt>
347 Dijkstra &predMap(PredMap &m)
357 ///Sets the map storing the predecessor nodes.
359 ///Sets the map storing the predecessor nodes.
360 ///If you don't use this function before calling \ref run(),
361 ///it will allocate one. The destuctor deallocates this
362 ///automatically allocated map, of course.
363 ///\return <tt> (*this) </tt>
364 Dijkstra &predNodeMap(PredNodeMap &m)
366 if(local_pred_node) {
368 local_pred_node=false;
374 ///Sets the map storing the distances calculated by the algorithm.
376 ///Sets the map storing the distances calculated by the algorithm.
377 ///If you don't use this function before calling \ref run(),
378 ///it will allocate one. The destuctor deallocates this
379 ///automatically allocated map, of course.
380 ///\return <tt> (*this) </tt>
381 Dijkstra &distMap(DistMap &m)
385 local_distance=false;
391 ///Runs %Dijkstra algorithm from node \c s.
393 ///This method runs the %Dijkstra algorithm from a root node \c s
396 ///shortest path to each node. The algorithm computes
397 ///- The shortest path tree.
398 ///- The distance of each node from the root.
399 ///\todo heap_map's type could also be in the traits class.
406 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
407 _pred->set(u,INVALID);
408 pred_node->set(u,INVALID);
409 ///\todo *_reached is not set to false.
412 typename Graph::template NodeMap<int> heap_map(*G,-1);
418 while ( !heap.empty() ) {
421 _reached->set(v,true);
422 Value oldvalue=heap[v];
424 distance->set(v, oldvalue);
427 for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
429 switch(heap.state(w)) {
431 heap.push(w,oldvalue+(*length)[e]);
436 if ( oldvalue+(*length)[e] < heap[w] ) {
437 heap.decrease(w, oldvalue+(*length)[e]);
442 case Heap::POST_HEAP:
449 ///The distance of a node from the root.
451 ///Returns the distance of a node from the root.
452 ///\pre \ref run() must be called before using this function.
453 ///\warning If node \c v in unreachable from the root the return value
454 ///of this funcion is undefined.
455 Value dist(Node v) const { return (*distance)[v]; }
457 ///Returns the 'previous edge' of the shortest path tree.
459 ///For a node \c v it returns the 'previous edge' of the shortest path tree,
460 ///i.e. it returns the last edge of a shortest path from the root to \c
461 ///v. It is \ref INVALID
462 ///if \c v is unreachable from the root or if \c v=s. The
463 ///shortest path tree used here is equal to the shortest path tree used in
464 ///\ref predNode(Node v). \pre \ref run() must be called before using
466 ///\todo predEdge could be a better name.
467 Edge pred(Node v) const { return (*_pred)[v]; }
469 ///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,
472 ///i.e. it returns the last but one node from a shortest path from the
473 ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
474 ///\c v=s. The shortest path tree used here is equal to the shortest path
475 ///tree used in \ref pred(Node v). \pre \ref run() must be called before
476 ///using this function.
477 Node predNode(Node v) const { return (*pred_node)[v]; }
479 ///Returns a reference to the NodeMap of distances.
481 ///Returns a reference to the NodeMap of distances. \pre \ref run() must
482 ///be called before using this function.
483 const DistMap &distMap() const { return *distance;}
485 ///Returns a reference to the shortest path tree map.
487 ///Returns a reference to the NodeMap of the edges of the
488 ///shortest path tree.
489 ///\pre \ref run() must be called before using this function.
490 const PredMap &predMap() const { return *_pred;}
492 ///Returns a reference to the map of nodes of shortest paths.
494 ///Returns a reference to the NodeMap of the last but one nodes of the
495 ///shortest path tree.
496 ///\pre \ref run() must be called before using this function.
497 const PredNodeMap &predNodeMap() const { return *pred_node;}
499 ///Checks if a node is reachable from the root.
501 ///Returns \c true if \c v is reachable from the root.
502 ///\note The root node is reported to be reached!
503 ///\pre \ref run() must be called before using this function.
505 bool reached(Node v) { return v==source || (*_pred)[v]!=INVALID; }
509 template<class GR,class LM>
510 class DijkstraWizardBase : public DijkstraDefaultTraits<GR,LM>
513 typedef DijkstraDefaultTraits<GR,LM> Base;
515 /// Pointer to the underlying graph.
517 /// Pointer to the length map
519 ///Pointer to the map of predecessors edges.
521 ///Pointer to the map of predecessors nodes.
523 ///Pointer to the map of distances.
525 ///Pointer to the source node.
528 typedef typename Base::Graph::Node Node;
531 DijkstraWizardBase() : _g(0), _length(0), _pred(0), _predNode(0),
532 _dist(0), _source(INVALID) {}
534 DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
535 _g((void *)&g), _length((void *)&l), _pred(0), _predNode(0),
536 _dist(0), _source((void *)&s) {}
545 class DijkstraWizard : public TR
549 //The type of the underlying graph.
550 typedef typename TR::Graph Graph;
552 typedef typename Graph::Node Node;
554 typedef typename Graph::NodeIt NodeIt;
556 typedef typename Graph::Edge Edge;
558 typedef typename Graph::OutEdgeIt OutEdgeIt;
560 //The type of the map that stores the edge lengths.
561 typedef typename TR::LengthMap LengthMap;
562 //The type of the length of the edges.
563 typedef typename LengthMap::Value Value;
564 //\brief The type of the map that stores the last
565 //edges of the shortest paths.
566 typedef typename TR::PredMap PredMap;
567 //\brief The type of the map that stores the last but one
568 //nodes of the shortest paths.
569 typedef typename TR::PredNodeMap PredNodeMap;
570 //The type of the map that stores the dists of the nodes.
571 typedef typename TR::DistMap DistMap;
573 //The heap type used by the dijkstra algorithm.
574 typedef typename TR::Heap Heap;
576 DijkstraWizard() : TR() {}
578 DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
581 DijkstraWizard(const TR &b) : TR(b) {}
588 if(_source==0) throw UninitializedData();
589 Dijkstra<Graph,LengthMap,TR> Dij(*(Graph*)_g,*(LengthMap*)_length);
590 if(_pred) Dij.predMap(*(PredMap*)_pred);
591 if(_predNode) Dij.predNodeMap(*(PredNodeMap*)_predNode);
592 if(_dist) Dij.distMap(*(DistMap*)_dist);
593 Dij.run(*(Node*)_source);
604 struct DefPredMapBase : public Base {
606 static PredMap *createPredMap(const Graph &G) { return 0; };
607 DefPredMapBase(const Base &b) : Base(b) {}
612 DijkstraWizard<DefPredMapBase<T> > predMap(const T &t)
615 return DijkstraWizard<DefPredMapBase<T> >(*this);
620 struct DefPredNodeMapBase : public Base {
621 typedef T PredNodeMap;
622 static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; };
623 DefPredNodeMapBase(const Base &b) : Base(b) {}
628 DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t)
630 _predNode=(void *)&t;
631 return DijkstraWizard<DefPredNodeMapBase<T> >(*this);
635 struct DefDistMapBase : public Base {
637 static DistMap *createDistMap(const Graph &G) { return 0; };
638 DefDistMapBase(const Base &b) : Base(b) {}
643 DijkstraWizard<DefDistMapBase<T> > distMap(const T &t)
646 return DijkstraWizard<DefDistMapBase<T> >(*this);
650 DijkstraWizard<TR> &source(Node s)
660 ///\todo Please document...
662 template<class GR, class LM>
663 DijkstraWizard<DijkstraWizardBase<GR,LM> >
664 dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
666 return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
671 } //END OF NAMESPACE LEMON