2 * lemon/dijkstra.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, 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 ///\todo getPath() should be implemented! (also for BFS and DFS)
26 #include <lemon/list_graph.h>
27 #include <lemon/bin_heap.h>
28 #include <lemon/invalid.h>
29 #include <lemon/error.h>
30 #include <lemon/maps.h>
36 ///Default traits class of Dijkstra class.
38 ///Default traits class of Dijkstra class.
39 ///\param GR Graph type.
40 ///\param LM Type of length map.
41 template<class GR, class LM>
42 struct DijkstraDefaultTraits
44 ///The graph type the algorithm runs on.
46 ///The type of the map that stores the edge lengths.
48 ///The type of the map that stores the edge lengths.
49 ///It must meet the \ref concept::ReadMap "ReadMap" concept.
51 //The type of the length of the edges.
52 typedef typename LM::Value Value;
53 ///The heap type used by Dijkstra algorithm.
55 ///The heap type used by Dijkstra algorithm.
59 typedef BinHeap<typename Graph::Node, typename LM::Value,
60 typename GR::template NodeMap<int>,
61 std::less<Value> > Heap;
63 ///\brief The type of the map that stores the last
64 ///edges of the shortest paths.
66 ///The type of the map that stores the last
67 ///edges of the shortest paths.
68 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
70 typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
71 ///Instantiates a PredMap.
73 ///This function instantiates a \ref PredMap.
74 ///\param G is the graph, to which we would like to define the PredMap.
75 ///\todo The graph alone may be insufficient for the initialization
76 static PredMap *createPredMap(const GR &G)
78 return new PredMap(G);
81 ///The type of the map that stores whether a nodes is processed.
83 ///The type of the map that stores whether a nodes is processed.
84 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
85 ///By default it is a NullMap.
86 ///\todo If it is set to a real map,
87 ///Dijkstra::processed() should read this.
88 ///\todo named parameter to set this type, function to read and write.
89 typedef NullMap<typename Graph::Node,bool> ProcessedMap;
90 ///Instantiates a ProcessedMap.
92 ///This function instantiates a \ref ProcessedMap.
93 ///\param g is the graph, to which
94 ///we would like to define the \ref ProcessedMap
96 static ProcessedMap *createProcessedMap(const GR &g)
98 static ProcessedMap *createProcessedMap(const GR &)
101 return new ProcessedMap();
103 ///The type of the map that stores the dists of the nodes.
105 ///The type of the map that stores the dists of the nodes.
106 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
108 typedef typename Graph::template NodeMap<typename LM::Value> DistMap;
109 ///Instantiates a DistMap.
111 ///This function instantiates a \ref DistMap.
112 ///\param G is the graph, to which we would like to define the \ref DistMap
113 static DistMap *createDistMap(const GR &G)
115 return new DistMap(G);
119 ///%Dijkstra algorithm class.
121 /// \ingroup flowalgs
122 ///This class provides an efficient implementation of %Dijkstra algorithm.
123 ///The edge lengths are passed to the algorithm using a
124 ///\ref concept::ReadMap "ReadMap",
125 ///so it is easy to change it to any kind of length.
127 ///The type of the length is determined by the
128 ///\ref concept::ReadMap::Value "Value" of the length map.
130 ///It is also possible to change the underlying priority heap.
132 ///\param GR The graph type the algorithm runs on. The default value
133 ///is \ref ListGraph. The value of GR is not used directly by
134 ///Dijkstra, it is only passed to \ref DijkstraDefaultTraits.
135 ///\param LM This read-only EdgeMap determines the lengths of the
136 ///edges. It is read once for each edge, so the map may involve in
137 ///relatively time consuming process to compute the edge length if
138 ///it is necessary. The default map type is \ref
139 ///concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>". The value
140 ///of LM is not used directly by Dijkstra, it is only passed to \ref
141 ///DijkstraDefaultTraits. \param TR Traits class to set
142 ///various data types used by the algorithm. The default traits
143 ///class is \ref DijkstraDefaultTraits
144 ///"DijkstraDefaultTraits<GR,LM>". See \ref
145 ///DijkstraDefaultTraits for the documentation of a Dijkstra traits
148 ///\author Jacint Szabo and Alpar Juttner
149 ///\todo A compare object would be nice.
152 template <typename GR,
156 template <typename GR=ListGraph,
157 typename LM=typename GR::template EdgeMap<int>,
158 typename TR=DijkstraDefaultTraits<GR,LM> >
163 * \brief \ref Exception for uninitialized parameters.
165 * This error represents problems in the initialization
166 * of the parameters of the algorithms.
168 class UninitializedParameter : public lemon::UninitializedParameter {
170 virtual const char* exceptionName() const {
171 return "lemon::Dijkstra::UninitializedParameter";
176 ///The type of the underlying graph.
177 typedef typename TR::Graph Graph;
179 typedef typename Graph::Node Node;
181 typedef typename Graph::NodeIt NodeIt;
183 typedef typename Graph::Edge Edge;
185 typedef typename Graph::OutEdgeIt OutEdgeIt;
187 ///The type of the length of the edges.
188 typedef typename TR::LengthMap::Value Value;
189 ///The type of the map that stores the edge lengths.
190 typedef typename TR::LengthMap LengthMap;
191 ///\brief The type of the map that stores the last
192 ///edges of the shortest paths.
193 typedef typename TR::PredMap PredMap;
194 ///The type of the map indicating if a node is processed.
195 typedef typename TR::ProcessedMap ProcessedMap;
196 ///The type of the map that stores the dists of the nodes.
197 typedef typename TR::DistMap DistMap;
198 ///The heap type used by the dijkstra algorithm.
199 typedef typename TR::Heap Heap;
201 /// Pointer to the underlying graph.
203 /// Pointer to the length map
204 const LengthMap *length;
205 ///Pointer to the map of predecessors edges.
207 ///Indicates if \ref _pred is locally allocated (\c true) or not.
209 ///Pointer to the map of distances.
211 ///Indicates if \ref _dist is locally allocated (\c true) or not.
213 ///Pointer to the map of processed status of the nodes.
214 ProcessedMap *_processed;
215 ///Indicates if \ref _processed is locally allocated (\c true) or not.
216 bool local_processed;
218 ///Creates the maps if necessary.
220 ///\todo Error if \c G or are \c NULL. What about \c length?
221 ///\todo Better memory allocation (instead of new).
226 _pred = Traits::createPredMap(*G);
230 _dist = Traits::createDistMap(*G);
233 local_processed = true;
234 _processed = Traits::createProcessedMap(*G);
240 typedef Dijkstra Create;
242 ///\name Named template parameters
247 struct DefPredMapTraits : public Traits {
249 static PredMap *createPredMap(const Graph &G)
251 throw UninitializedParameter();
254 ///\ref named-templ-param "Named parameter" for setting PredMap type
256 ///\ref named-templ-param "Named parameter" for setting PredMap type
260 : public Dijkstra< Graph, LengthMap, DefPredMapTraits<T> > {
261 typedef Dijkstra< Graph, LengthMap, DefPredMapTraits<T> > Create;
265 struct DefDistMapTraits : public Traits {
267 static DistMap *createDistMap(const Graph &G)
269 throw UninitializedParameter();
272 ///\ref named-templ-param "Named parameter" for setting DistMap type
274 ///\ref named-templ-param "Named parameter" for setting DistMap type
278 : public Dijkstra< Graph, LengthMap, DefDistMapTraits<T> > {
279 typedef Dijkstra< Graph, LengthMap, DefDistMapTraits<T> > Create;
283 struct DefProcessedMapTraits : public Traits {
284 typedef T ProcessedMap;
285 static ProcessedMap *createProcessedMap(const Graph &G)
287 throw UninitializedParameter();
290 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
292 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
295 struct DefProcessedMap
296 : public Dijkstra< Graph, LengthMap, DefProcessedMapTraits<T> > {
297 typedef Dijkstra< Graph, LengthMap, DefProcessedMapTraits<T> > Create;
300 struct DefGraphProcessedMapTraits : public Traits {
301 typedef typename Graph::template NodeMap<bool> ProcessedMap;
302 static ProcessedMap *createProcessedMap(const Graph &G)
304 return new ProcessedMap(G);
307 ///\brief \ref named-templ-param "Named parameter"
308 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
310 ///\ref named-templ-param "Named parameter"
311 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
312 ///If you don't set it explicitely, it will be automatically allocated.
314 struct DefProcessedMapToBeDefaultMap
315 : public Dijkstra< Graph, LengthMap, DefGraphProcessedMapTraits> {
316 typedef Dijkstra< Graph, LengthMap, DefGraphProcessedMapTraits> Create;
323 typename Graph::template NodeMap<int> _heap_map;
333 ///\param _G the graph the algorithm will run on.
334 ///\param _length the length map used by the algorithm.
335 Dijkstra(const Graph& _G, const LengthMap& _length) :
336 G(&_G), length(&_length),
337 _pred(NULL), local_pred(false),
338 _dist(NULL), local_dist(false),
339 _processed(NULL), local_processed(false),
340 _heap_map(*G,-1),_heap(_heap_map)
346 if(local_pred) delete _pred;
347 if(local_dist) delete _dist;
348 if(local_processed) delete _processed;
351 ///Sets the length map.
353 ///Sets the length map.
354 ///\return <tt> (*this) </tt>
355 Dijkstra &lengthMap(const LengthMap &m)
361 ///Sets the map storing the predecessor edges.
363 ///Sets the map storing the predecessor edges.
364 ///If you don't use this function before calling \ref run(),
365 ///it will allocate one. The destuctor deallocates this
366 ///automatically allocated map, of course.
367 ///\return <tt> (*this) </tt>
368 Dijkstra &predMap(PredMap &m)
378 ///Sets the map storing the distances calculated by the algorithm.
380 ///Sets the map storing the distances calculated by the algorithm.
381 ///If you don't use this function before calling \ref run(),
382 ///it will allocate one. The destuctor deallocates this
383 ///automatically allocated map, of course.
384 ///\return <tt> (*this) </tt>
385 Dijkstra &distMap(DistMap &m)
396 void finalizeNodeData(Node v,Value dst)
398 _processed->set(v,true);
403 ///\name Execution control
404 ///The simplest way to execute the algorithm is to use
405 ///one of the member functions called \c run(...).
407 ///If you need more control on the execution,
408 ///first you must call \ref init(), then you can add several source nodes
409 ///with \ref addSource().
410 ///Finally \ref start() will perform the actual path
415 ///Initializes the internal data structures.
417 ///Initializes the internal data structures.
419 ///\todo _heap_map's type could also be in the traits class.
420 ///\todo The heaps should be able to make themselves empty directly.
424 while(!_heap.empty()) _heap.pop();
425 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
426 _pred->set(u,INVALID);
427 _processed->set(u,false);
428 _heap_map.set(u,Heap::PRE_HEAP);
432 ///Adds a new source node.
434 ///Adds a new source node to the priority heap.
436 ///The optional second parameter is the initial distance of the node.
438 ///It checks if the node has already been added to the heap and
439 ///It is pushed to the heap only if either it was not in the heap
440 ///or the shortest path found till then is longer then \c dst.
441 void addSource(Node s,Value dst=0)
443 if(_heap.state(s) != Heap::IN_HEAP) _heap.push(s,dst);
444 else if(_heap[s]<dst) {
446 _pred->set(s,INVALID);
450 ///Processes the next node in the priority heap
452 ///Processes the next node in the priority heap.
454 ///\return The processed node.
456 ///\warning The priority heap must not be empty!
457 Node processNextNode()
460 Value oldvalue=_heap[v];
462 finalizeNodeData(v,oldvalue);
464 for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
466 switch(_heap.state(w)) {
468 _heap.push(w,oldvalue+(*length)[e]);
472 if ( oldvalue+(*length)[e] < _heap[w] ) {
473 _heap.decrease(w, oldvalue+(*length)[e]);
477 case Heap::POST_HEAP:
484 ///Next node to be processed.
486 ///Next node to be processed.
488 ///\return The next node to be processed or INVALID if the priority heap
492 return _heap.empty()?_heap.top():INVALID;
495 ///\brief Returns \c false if there are nodes
496 ///to be processed in the priority heap
498 ///Returns \c false if there are nodes
499 ///to be processed in the priority heap
500 bool emptyQueue() { return _heap.empty(); }
501 ///Returns the number of the nodes to be processed in the priority heap
503 ///Returns the number of the nodes to be processed in the priority heap
505 int queueSize() { return _heap.size(); }
507 ///Executes the algorithm.
509 ///Executes the algorithm.
511 ///\pre init() must be called and at least one node should be added
512 ///with addSource() before using this function.
514 ///This method runs the %Dijkstra algorithm from the root node(s)
517 ///shortest path to each node. The algorithm computes
518 ///- The shortest path tree.
519 ///- The distance of each node from the root(s).
523 while ( !_heap.empty() ) processNextNode();
526 ///Executes the algorithm until \c dest is reached.
528 ///Executes the algorithm until \c dest is reached.
530 ///\pre init() must be called and at least one node should be added
531 ///with addSource() before using this function.
533 ///This method runs the %Dijkstra algorithm from the root node(s)
536 ///shortest path to \c dest. The algorithm computes
537 ///- The shortest path to \c dest.
538 ///- The distance of \c dest from the root(s).
540 void start(Node dest)
542 while ( !_heap.empty() && _heap.top()!=dest ) processNextNode();
543 if ( !_heap.empty() ) finalizeNodeData(_heap.top(),_heap.prio());
546 ///Executes the algorithm until a condition is met.
548 ///Executes the algorithm until a condition is met.
550 ///\pre init() must be called and at least one node should be added
551 ///with addSource() before using this function.
553 ///\param nm must be a bool (or convertible) node map. The algorithm
554 ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
555 template<class NodeBoolMap>
556 void start(const NodeBoolMap &nm)
558 while ( !_heap.empty() && !nm[_heap.top()] ) processNextNode();
559 if ( !_heap.empty() ) finalizeNodeData(_heap.top(),_heap.prio());
562 ///Runs %Dijkstra algorithm from node \c s.
564 ///This method runs the %Dijkstra algorithm from a root node \c s
567 ///shortest path to each node. The algorithm computes
568 ///- The shortest path tree.
569 ///- The distance of each node from the root.
571 ///\note d.run(s) is just a shortcut of the following code.
583 ///Finds the shortest path between \c s and \c t.
585 ///Finds the shortest path between \c s and \c t.
587 ///\return The length of the shortest s---t path if there exists one,
589 ///\note Apart from the return value, d.run(s) is
590 ///just a shortcut of the following code.
596 Value run(Node s,Node t) {
600 return (*_pred)[t]==INVALID?0:(*_dist)[t];
605 ///\name Query Functions
606 ///The result of the %Dijkstra algorithm can be obtained using these
608 ///Before the use of these functions,
609 ///either run() or start() must be called.
613 ///Copies the shortest path to \c t into \c p
615 ///This function copies the shortest path to \c t into \c p.
616 ///If it \c t is a source itself or unreachable, then it does not
618 ///\todo Is it the right way to handle unreachable nodes?
619 ///\return Returns \c true if a path to \c t was actually copied to \c p,
620 ///\c false otherwise.
623 bool getPath(P &p,Node t)
627 typename P::Builder b(p);
628 for(b.setStartNode(t);pred(t)!=INVALID;t=predNode(t))
629 b.pushFront(pred(t));
636 ///The distance of a node from the root.
638 ///Returns the distance of a node from the root.
639 ///\pre \ref run() must be called before using this function.
640 ///\warning If node \c v in unreachable from the root the return value
641 ///of this funcion is undefined.
642 Value dist(Node v) const { return (*_dist)[v]; }
644 ///Returns the 'previous edge' of the shortest path tree.
646 ///For a node \c v it returns the 'previous edge' of the shortest path tree,
647 ///i.e. it returns the last edge of a shortest path from the root to \c
648 ///v. It is \ref INVALID
649 ///if \c v is unreachable from the root or if \c v=s. The
650 ///shortest path tree used here is equal to the shortest path tree used in
651 ///\ref predNode(). \pre \ref run() must be called before using
653 ///\todo predEdge could be a better name.
654 Edge pred(Node v) const { return (*_pred)[v]; }
656 ///Returns the 'previous node' of the shortest path tree.
658 ///For a node \c v it returns the 'previous node' of the shortest path tree,
659 ///i.e. it returns the last but one node from a shortest path from the
660 ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
661 ///\c v=s. The shortest path tree used here is equal to the shortest path
662 ///tree used in \ref pred(). \pre \ref run() must be called before
663 ///using this function.
664 Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
665 G->source((*_pred)[v]); }
667 ///Returns a reference to the NodeMap of distances.
669 ///Returns a reference to the NodeMap of distances. \pre \ref run() must
670 ///be called before using this function.
671 const DistMap &distMap() const { return *_dist;}
673 ///Returns a reference to the shortest path tree map.
675 ///Returns a reference to the NodeMap of the edges of the
676 ///shortest path tree.
677 ///\pre \ref run() must be called before using this function.
678 const PredMap &predMap() const { return *_pred;}
680 ///Checks if a node is reachable from the root.
682 ///Returns \c true if \c v is reachable from the root.
683 ///\warning The source nodes are inditated as unreached.
684 ///\pre \ref run() must be called before using this function.
686 bool reached(Node v) { return _heap_map[v]!=Heap::PRE_HEAP; }
695 ///Default traits class of Dijkstra function.
697 ///Default traits class of Dijkstra function.
698 ///\param GR Graph type.
699 ///\param LM Type of length map.
700 template<class GR, class LM>
701 struct DijkstraWizardDefaultTraits
703 ///The graph type the algorithm runs on.
705 ///The type of the map that stores the edge lengths.
707 ///The type of the map that stores the edge lengths.
708 ///It must meet the \ref concept::ReadMap "ReadMap" concept.
709 typedef LM LengthMap;
710 //The type of the length of the edges.
711 typedef typename LM::Value Value;
712 ///The heap type used by Dijkstra algorithm.
714 ///The heap type used by Dijkstra algorithm.
718 typedef BinHeap<typename Graph::Node,
720 typename GR::template NodeMap<int>,
721 std::less<Value> > Heap;
723 ///\brief The type of the map that stores the last
724 ///edges of the shortest paths.
726 ///The type of the map that stores the last
727 ///edges of the shortest paths.
728 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
730 typedef NullMap <typename GR::Node,typename GR::Edge> PredMap;
731 ///Instantiates a PredMap.
733 ///This function instantiates a \ref PredMap.
734 ///\param g is the graph, to which we would like to define the PredMap.
735 ///\todo The graph alone may be insufficient for the initialization
737 static PredMap *createPredMap(const GR &g)
739 static PredMap *createPredMap(const GR &)
742 return new PredMap();
744 ///The type of the map that stores whether a nodes is processed.
746 ///The type of the map that stores whether a nodes is processed.
747 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
748 ///By default it is a NullMap.
749 ///\todo If it is set to a real map,
750 ///Dijkstra::processed() should read this.
751 ///\todo named parameter to set this type, function to read and write.
752 typedef NullMap<typename Graph::Node,bool> ProcessedMap;
753 ///Instantiates a ProcessedMap.
755 ///This function instantiates a \ref ProcessedMap.
756 ///\param g is the graph, to which
757 ///we would like to define the \ref ProcessedMap
759 static ProcessedMap *createProcessedMap(const GR &g)
761 static ProcessedMap *createProcessedMap(const GR &)
764 return new ProcessedMap();
766 ///The type of the map that stores the dists of the nodes.
768 ///The type of the map that stores the dists of the nodes.
769 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
771 typedef NullMap<typename Graph::Node,typename LM::Value> DistMap;
772 ///Instantiates a DistMap.
774 ///This function instantiates a \ref DistMap.
775 ///\param g is the graph, to which we would like to define the \ref DistMap
777 static DistMap *createDistMap(const GR &g)
779 static DistMap *createDistMap(const GR &)
782 return new DistMap();
786 /// Default traits used by \ref DijkstraWizard
788 /// To make it easier to use Dijkstra algorithm
789 ///we have created a wizard class.
790 /// This \ref DijkstraWizard class needs default traits,
791 ///as well as the \ref Dijkstra class.
792 /// The \ref DijkstraWizardBase is a class to be the default traits of the
793 /// \ref DijkstraWizard class.
794 /// \todo More named parameters are required...
795 template<class GR,class LM>
796 class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
799 typedef DijkstraWizardDefaultTraits<GR,LM> Base;
801 /// Type of the nodes in the graph.
802 typedef typename Base::Graph::Node Node;
804 /// Pointer to the underlying graph.
806 /// Pointer to the length map
808 ///Pointer to the map of predecessors edges.
810 // ///Pointer to the map of predecessors nodes.
812 ///Pointer to the map of distances.
814 ///Pointer to the source node.
820 /// This constructor does not require parameters, therefore it initiates
821 /// all of the attributes to default values (0, INVALID).
822 DijkstraWizardBase() : _g(0), _length(0), _pred(0),
824 _dist(0), _source(INVALID) {}
828 /// This constructor requires some parameters,
829 /// listed in the parameters list.
830 /// Others are initiated to 0.
831 /// \param g is the initial value of \ref _g
832 /// \param l is the initial value of \ref _length
833 /// \param s is the initial value of \ref _source
834 DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
835 _g((void *)&g), _length((void *)&l), _pred(0),
837 _dist(0), _source(s) {}
841 /// A class to make the usage of Dijkstra algorithm easier
843 /// This class is created to make it easier to use Dijkstra algorithm.
844 /// It uses the functions and features of the plain \ref Dijkstra,
845 /// but it is much simpler to use it.
847 /// Simplicity means that the way to change the types defined
848 /// in the traits class is based on functions that returns the new class
849 /// and not on templatable built-in classes.
850 /// When using the plain \ref Dijkstra
851 /// the new class with the modified type comes from
852 /// the original class by using the ::
853 /// operator. In the case of \ref DijkstraWizard only
854 /// a function have to be called and it will
855 /// return the needed class.
857 /// It does not have own \ref run method. When its \ref run method is called
858 /// it initiates a plain \ref Dijkstra class, and calls the \ref Dijkstra::run
861 class DijkstraWizard : public TR
865 ///The type of the underlying graph.
866 typedef typename TR::Graph Graph;
868 typedef typename Graph::Node Node;
870 typedef typename Graph::NodeIt NodeIt;
872 typedef typename Graph::Edge Edge;
874 typedef typename Graph::OutEdgeIt OutEdgeIt;
876 ///The type of the map that stores the edge lengths.
877 typedef typename TR::LengthMap LengthMap;
878 ///The type of the length of the edges.
879 typedef typename LengthMap::Value Value;
880 ///\brief The type of the map that stores the last
881 ///edges of the shortest paths.
882 typedef typename TR::PredMap PredMap;
883 // ///\brief The type of the map that stores the last but one
884 // ///nodes of the shortest paths.
885 // typedef typename TR::PredNodeMap PredNodeMap;
886 ///The type of the map that stores the dists of the nodes.
887 typedef typename TR::DistMap DistMap;
889 ///The heap type used by the dijkstra algorithm.
890 typedef typename TR::Heap Heap;
893 DijkstraWizard() : TR() {}
895 /// Constructor that requires parameters.
897 /// Constructor that requires parameters.
898 /// These parameters will be the default values for the traits class.
899 DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
903 DijkstraWizard(const TR &b) : TR(b) {}
907 ///Runs Dijkstra algorithm from a given node.
909 ///Runs Dijkstra algorithm from a given node.
910 ///The node can be given by the \ref source function.
913 if(Base::_source==INVALID) throw UninitializedParameter();
914 Dijkstra<Graph,LengthMap,TR>
915 dij(*(Graph*)Base::_g,*(LengthMap*)Base::_length);
916 if(Base::_pred) dij.predMap(*(PredMap*)Base::_pred);
917 // if(Base::_predNode) Dij.predNodeMap(*(PredNodeMap*)Base::_predNode);
918 if(Base::_dist) dij.distMap(*(DistMap*)Base::_dist);
919 dij.run(Base::_source);
922 ///Runs Dijkstra algorithm from the given node.
924 ///Runs Dijkstra algorithm from the given node.
925 ///\param s is the given source.
933 struct DefPredMapBase : public Base {
935 static PredMap *createPredMap(const Graph &) { return 0; };
936 DefPredMapBase(const TR &b) : TR(b) {}
939 ///\brief \ref named-templ-param "Named parameter"
940 ///function for setting PredMap type
942 /// \ref named-templ-param "Named parameter"
943 ///function for setting PredMap type
946 DijkstraWizard<DefPredMapBase<T> > predMap(const T &t)
948 Base::_pred=(void *)&t;
949 return DijkstraWizard<DefPredMapBase<T> >(*this);
954 // struct DefPredNodeMapBase : public Base {
955 // typedef T PredNodeMap;
956 // static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; };
957 // DefPredNodeMapBase(const TR &b) : TR(b) {}
960 // ///\brief \ref named-templ-param "Named parameter"
961 // ///function for setting PredNodeMap type
963 // /// \ref named-templ-param "Named parameter"
964 // ///function for setting PredNodeMap type
967 // DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t)
969 // Base::_predNode=(void *)&t;
970 // return DijkstraWizard<DefPredNodeMapBase<T> >(*this);
974 struct DefDistMapBase : public Base {
976 static DistMap *createDistMap(const Graph &) { return 0; };
977 DefDistMapBase(const TR &b) : TR(b) {}
980 ///\brief \ref named-templ-param "Named parameter"
981 ///function for setting DistMap type
983 /// \ref named-templ-param "Named parameter"
984 ///function for setting DistMap type
987 DijkstraWizard<DefDistMapBase<T> > distMap(const T &t)
989 Base::_dist=(void *)&t;
990 return DijkstraWizard<DefDistMapBase<T> >(*this);
993 /// Sets the source node, from which the Dijkstra algorithm runs.
995 /// Sets the source node, from which the Dijkstra algorithm runs.
996 /// \param s is the source node.
997 DijkstraWizard<TR> &source(Node s)
1005 ///Function type interface for Dijkstra algorithm.
1007 /// \ingroup flowalgs
1008 ///Function type interface for Dijkstra algorithm.
1010 ///This function also has several
1011 ///\ref named-templ-func-param "named parameters",
1012 ///they are declared as the members of class \ref DijkstraWizard.
1014 ///example shows how to use these parameters.
1016 /// dijkstra(g,length,source).predMap(preds).run();
1018 ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
1019 ///to the end of the parameter list.
1020 ///\sa DijkstraWizard
1022 template<class GR, class LM>
1023 DijkstraWizard<DijkstraWizardBase<GR,LM> >
1024 dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
1026 return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
1029 } //END OF NAMESPACE LEMON