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,
61 typename GR::template NodeMap<int>,
62 std::less<Value> > Heap;
64 ///\brief The type of the map that stores the last
65 ///edges of the shortest paths.
67 ///The type of the map that stores the last
68 ///edges of the shortest paths.
69 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
71 typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
72 ///Instantiates a PredMap.
74 ///This function instantiates a \ref PredMap.
75 ///\param G is the graph, to which we would like to define the PredMap.
76 ///\todo The graph alone may be insufficient for the initialization
77 static PredMap *createPredMap(const GR &G)
79 return new PredMap(G);
82 ///The type of the map that stores whether a nodes is processed.
84 ///The type of the map that stores whether a nodes is processed.
85 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
86 ///By default it is a NullMap.
87 ///\todo If it is set to a real map,
88 ///Dijkstra::processed() should read this.
89 ///\todo named parameter to set this type, function to read and write.
90 typedef NullMap<typename Graph::Node,bool> ProcessedMap;
91 ///Instantiates a ProcessedMap.
93 ///This function instantiates a \ref ProcessedMap.
94 ///\param g is the graph, to which
95 ///we would like to define the \ref ProcessedMap
97 static ProcessedMap *createProcessedMap(const GR &g)
99 static ProcessedMap *createProcessedMap(const GR &)
102 return new ProcessedMap();
104 ///The type of the map that stores the dists of the nodes.
106 ///The type of the map that stores the dists of the nodes.
107 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
109 typedef typename Graph::template NodeMap<typename LM::Value> DistMap;
110 ///Instantiates a DistMap.
112 ///This function instantiates a \ref DistMap.
113 ///\param G is the graph, to which we would like to define the \ref DistMap
114 static DistMap *createDistMap(const GR &G)
116 return new DistMap(G);
120 ///%Dijkstra algorithm class.
122 /// \ingroup flowalgs
123 ///This class provides an efficient implementation of %Dijkstra algorithm.
124 ///The edge lengths are passed to the algorithm using a
125 ///\ref concept::ReadMap "ReadMap",
126 ///so it is easy to change it to any kind of length.
128 ///The type of the length is determined by the
129 ///\ref concept::ReadMap::Value "Value" of the length map.
131 ///It is also possible to change the underlying priority heap.
133 ///\param GR The graph type the algorithm runs on. The default value
134 ///is \ref ListGraph. The value of GR is not used directly by
135 ///Dijkstra, it is only passed to \ref DijkstraDefaultTraits.
136 ///\param LM This read-only EdgeMap determines the lengths of the
137 ///edges. It is read once for each edge, so the map may involve in
138 ///relatively time consuming process to compute the edge length if
139 ///it is necessary. The default map type is \ref
140 ///concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>". The value
141 ///of LM is not used directly by Dijkstra, it is only passed to \ref
142 ///DijkstraDefaultTraits. \param TR Traits class to set
143 ///various data types used by the algorithm. The default traits
144 ///class is \ref DijkstraDefaultTraits
145 ///"DijkstraDefaultTraits<GR,LM>". See \ref
146 ///DijkstraDefaultTraits for the documentation of a Dijkstra traits
149 ///\author Jacint Szabo and Alpar Juttner
150 ///\todo A compare object would be nice.
153 template <typename GR,
157 template <typename GR=ListGraph,
158 typename LM=typename GR::template EdgeMap<int>,
159 typename TR=DijkstraDefaultTraits<GR,LM> >
164 * \brief \ref Exception for uninitialized parameters.
166 * This error represents problems in the initialization
167 * of the parameters of the algorithms.
169 class UninitializedParameter : public lemon::UninitializedParameter {
171 virtual const char* exceptionName() const {
172 return "lemon::Dijkstra::UninitializedParameter";
177 ///The type of the underlying graph.
178 typedef typename TR::Graph Graph;
180 typedef typename Graph::Node Node;
182 typedef typename Graph::NodeIt NodeIt;
184 typedef typename Graph::Edge Edge;
186 typedef typename Graph::OutEdgeIt OutEdgeIt;
188 ///The type of the length of the edges.
189 typedef typename TR::LengthMap::Value Value;
190 ///The type of the map that stores the edge lengths.
191 typedef typename TR::LengthMap LengthMap;
192 ///\brief The type of the map that stores the last
193 ///edges of the shortest paths.
194 typedef typename TR::PredMap PredMap;
195 ///The type of the map indicating if a node is processed.
196 typedef typename TR::ProcessedMap ProcessedMap;
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 distances.
212 ///Indicates if \ref _dist is locally allocated (\c true) or not.
214 ///Pointer to the map of processed status of the nodes.
215 ProcessedMap *_processed;
216 ///Indicates if \ref _processed is locally allocated (\c true) or not.
217 bool local_processed;
219 ///Creates the maps if necessary.
221 ///\todo Error if \c G or are \c NULL. What about \c length?
222 ///\todo Better memory allocation (instead of new).
227 _pred = Traits::createPredMap(*G);
231 _dist = Traits::createDistMap(*G);
234 local_processed = true;
235 _processed = Traits::createProcessedMap(*G);
241 ///\name Named template parameters
246 struct DefPredMapTraits : public Traits {
248 static PredMap *createPredMap(const Graph &G)
250 throw UninitializedParameter();
253 ///\ref named-templ-param "Named parameter" for setting PredMap type
255 ///\ref named-templ-param "Named parameter" for setting PredMap type
258 class DefPredMap : public Dijkstra< Graph,
260 DefPredMapTraits<T> > { };
263 struct DefDistMapTraits : public Traits {
265 static DistMap *createDistMap(const Graph &G)
267 throw UninitializedParameter();
270 ///\ref named-templ-param "Named parameter" for setting DistMap type
272 ///\ref named-templ-param "Named parameter" for setting DistMap type
275 class DefDistMap : public Dijkstra< Graph,
277 DefDistMapTraits<T> > { };
280 struct DefProcessedMapTraits : public Traits {
281 typedef T ProcessedMap;
282 static ProcessedMap *createProcessedMap(const Graph &G)
284 throw UninitializedParameter();
287 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
289 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
292 class DefProcessedMap : public Dijkstra< Graph,
294 DefProcessedMapTraits<T> > { };
296 struct DefGraphProcessedMapTraits : public Traits {
297 typedef typename Graph::template NodeMap<bool> ProcessedMap;
298 static ProcessedMap *createProcessedMap(const Graph &G)
300 return new ProcessedMap(G);
303 ///\brief \ref named-templ-param "Named parameter"
304 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
306 ///\ref named-templ-param "Named parameter"
307 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
308 ///If you don't set it explicitely, it will be automatically allocated.
310 class DefProcessedMapToBeDefaultMap :
311 public Dijkstra< Graph,
313 DefGraphProcessedMapTraits> { };
319 typename Graph::template NodeMap<int> _heap_map;
325 ///\param _G the graph the algorithm will run on.
326 ///\param _length the length map used by the algorithm.
327 Dijkstra(const Graph& _G, const LengthMap& _length) :
328 G(&_G), length(&_length),
329 _pred(NULL), local_pred(false),
330 _dist(NULL), local_dist(false),
331 _processed(NULL), local_processed(false),
332 _heap_map(*G,-1),_heap(_heap_map)
338 if(local_pred) delete _pred;
339 if(local_dist) delete _dist;
340 if(local_processed) delete _processed;
343 ///Sets the length map.
345 ///Sets the length map.
346 ///\return <tt> (*this) </tt>
347 Dijkstra &lengthMap(const LengthMap &m)
353 ///Sets the map storing the predecessor edges.
355 ///Sets the map storing the predecessor edges.
356 ///If you don't use this function before calling \ref run(),
357 ///it will allocate one. The destuctor deallocates this
358 ///automatically allocated map, of course.
359 ///\return <tt> (*this) </tt>
360 Dijkstra &predMap(PredMap &m)
370 ///Sets the map storing the distances calculated by the algorithm.
372 ///Sets the map storing the distances calculated by the algorithm.
373 ///If you don't use this function before calling \ref run(),
374 ///it will allocate one. The destuctor deallocates this
375 ///automatically allocated map, of course.
376 ///\return <tt> (*this) </tt>
377 Dijkstra &distMap(DistMap &m)
388 void finalizeNodeData(Node v,Value dst)
390 _processed->set(v,true);
395 ///\name Execution control
396 ///The simplest way to execute the algorithm is to use
397 ///one of the member functions called \c run(...).
399 ///If you need more control on the execution,
400 ///first you must call \ref init(), then you can add several source nodes
401 ///with \ref addSource().
402 ///Finally \ref start() will perform the actual path
407 ///Initializes the internal data structures.
409 ///Initializes the internal data structures.
411 ///\todo _heap_map's type could also be in the traits class.
412 ///\todo The heaps should be able to make themselves empty directly.
416 while(!_heap.empty()) _heap.pop();
417 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
418 _pred->set(u,INVALID);
419 _processed->set(u,false);
420 _heap_map.set(u,Heap::PRE_HEAP);
424 ///Adds a new source node.
426 ///Adds a new source node to the priority heap.
428 ///The optional second parameter is the initial distance of the node.
430 ///It checks if the node has already been added to the heap and
431 ///It is pushed to the heap only if either it was not in the heap
432 ///or the shortest path found till then is longer then \c dst.
433 void addSource(Node s,Value dst=0)
435 if(_heap.state(s) != Heap::IN_HEAP) _heap.push(s,dst);
436 else if(_heap[s]<dst) {
438 _pred->set(s,INVALID);
442 ///Processes the next node in the priority heap
444 ///Processes the next node in the priority heap.
446 ///\return The processed node.
448 ///\warning The priority heap must not be empty!
449 Node processNextNode()
452 Value oldvalue=_heap[v];
454 finalizeNodeData(v,oldvalue);
456 for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
458 switch(_heap.state(w)) {
460 _heap.push(w,oldvalue+(*length)[e]);
464 if ( oldvalue+(*length)[e] < _heap[w] ) {
465 _heap.decrease(w, oldvalue+(*length)[e]);
469 case Heap::POST_HEAP:
476 ///Next node to be processed.
478 ///Next node to be processed.
480 ///\return The next node to be processed or INVALID if the priority heap
484 return _heap.empty()?_heap.top():INVALID;
487 ///\brief Returns \c false if there are nodes
488 ///to be processed in the priority heap
490 ///Returns \c false if there are nodes
491 ///to be processed in the priority heap
492 bool emptyQueue() { return _heap.empty(); }
493 ///Returns the number of the nodes to be processed in the priority heap
495 ///Returns the number of the nodes to be processed in the priority heap
497 int queueSize() { return _heap.size(); }
499 ///Executes the algorithm.
501 ///Executes the algorithm.
503 ///\pre init() must be called and at least one node should be added
504 ///with addSource() before using this function.
506 ///This method runs the %Dijkstra algorithm from the root node(s)
509 ///shortest path to each node. The algorithm computes
510 ///- The shortest path tree.
511 ///- The distance of each node from the root(s).
515 while ( !_heap.empty() ) processNextNode();
518 ///Executes the algorithm until \c dest is reached.
520 ///Executes the algorithm until \c dest is reached.
522 ///\pre init() must be called and at least one node should be added
523 ///with addSource() before using this function.
525 ///This method runs the %Dijkstra algorithm from the root node(s)
528 ///shortest path to \c dest. The algorithm computes
529 ///- The shortest path to \c dest.
530 ///- The distance of \c dest from the root(s).
532 void start(Node dest)
534 while ( !_heap.empty() && _heap.top()!=dest ) processNextNode();
535 if ( !_heap.empty() ) finalizeNodeData(_heap.top(),_heap.prio());
538 ///Executes the algorithm until a condition is met.
540 ///Executes the algorithm until a condition is met.
542 ///\pre init() must be called and at least one node should be added
543 ///with addSource() before using this function.
545 ///\param nm must be a bool (or convertible) node map. The algorithm
546 ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
547 template<class NodeBoolMap>
548 void start(const NodeBoolMap &nm)
550 while ( !_heap.empty() && !nm[_heap.top()] ) processNextNode();
551 if ( !_heap.empty() ) finalizeNodeData(_heap.top(),_heap.prio());
554 ///Runs %Dijkstra algorithm from node \c s.
556 ///This method runs the %Dijkstra algorithm from a root node \c s
559 ///shortest path to each node. The algorithm computes
560 ///- The shortest path tree.
561 ///- The distance of each node from the root.
563 ///\note d.run(s) is just a shortcut of the following code.
575 ///Finds the shortest path between \c s and \c t.
577 ///Finds the shortest path between \c s and \c t.
579 ///\return The length of the shortest s---t path if there exists one,
581 ///\note Apart from the return value, d.run(s) is
582 ///just a shortcut of the following code.
588 Value run(Node s,Node t) {
592 return (*_pred)[t]==INVALID?0:(*_dist)[t];
597 ///\name Query Functions
598 ///The result of the %Dijkstra algorithm can be obtained using these
600 ///Before the use of these functions,
601 ///either run() or start() must be called.
605 ///Copies the shortest path to \c t into \c p
607 ///This function copies the shortest path to \c t into \c p.
608 ///If it \c t is a source itself or unreachable, then it does not
610 ///\todo Is it the right way to handle unreachable nodes?
611 ///\return Returns \c true if a path to \c t was actually copied to \c p,
612 ///\c false otherwise.
615 bool getPath(P &p,Node t)
619 typename P::Builder b(p);
620 for(b.setStartNode(t);pred(t)!=INVALID;t=predNode(t))
621 b.pushFront(pred(t));
628 ///The distance of a node from the root.
630 ///Returns the distance of a node from the root.
631 ///\pre \ref run() must be called before using this function.
632 ///\warning If node \c v in unreachable from the root the return value
633 ///of this funcion is undefined.
634 Value dist(Node v) const { return (*_dist)[v]; }
636 ///Returns the 'previous edge' of the shortest path tree.
638 ///For a node \c v it returns the 'previous edge' of the shortest path tree,
639 ///i.e. it returns the last edge of a shortest path from the root to \c
640 ///v. It is \ref INVALID
641 ///if \c v is unreachable from the root or if \c v=s. The
642 ///shortest path tree used here is equal to the shortest path tree used in
643 ///\ref predNode(). \pre \ref run() must be called before using
645 ///\todo predEdge could be a better name.
646 Edge pred(Node v) const { return (*_pred)[v]; }
648 ///Returns the 'previous node' of the shortest path tree.
650 ///For a node \c v it returns the 'previous node' of the shortest path tree,
651 ///i.e. it returns the last but one node from a shortest path from the
652 ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
653 ///\c v=s. The shortest path tree used here is equal to the shortest path
654 ///tree used in \ref pred(). \pre \ref run() must be called before
655 ///using this function.
656 Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
657 G->source((*_pred)[v]); }
659 ///Returns a reference to the NodeMap of distances.
661 ///Returns a reference to the NodeMap of distances. \pre \ref run() must
662 ///be called before using this function.
663 const DistMap &distMap() const { return *_dist;}
665 ///Returns a reference to the shortest path tree map.
667 ///Returns a reference to the NodeMap of the edges of the
668 ///shortest path tree.
669 ///\pre \ref run() must be called before using this function.
670 const PredMap &predMap() const { return *_pred;}
672 ///Checks if a node is reachable from the root.
674 ///Returns \c true if \c v is reachable from the root.
675 ///\warning The source nodes are inditated as unreached.
676 ///\pre \ref run() must be called before using this function.
678 bool reached(Node v) { return _heap_map[v]!=Heap::PRE_HEAP; }
687 ///Default traits class of Dijkstra function.
689 ///Default traits class of Dijkstra function.
690 ///\param GR Graph type.
691 ///\param LM Type of length map.
692 template<class GR, class LM>
693 struct DijkstraWizardDefaultTraits
695 ///The graph type the algorithm runs on.
697 ///The type of the map that stores the edge lengths.
699 ///The type of the map that stores the edge lengths.
700 ///It must meet the \ref concept::ReadMap "ReadMap" concept.
701 typedef LM LengthMap;
702 //The type of the length of the edges.
703 typedef typename LM::Value Value;
704 ///The heap type used by Dijkstra algorithm.
706 ///The heap type used by Dijkstra algorithm.
710 typedef BinHeap<typename Graph::Node,
712 typename GR::template NodeMap<int>,
713 std::less<Value> > Heap;
715 ///\brief The type of the map that stores the last
716 ///edges of the shortest paths.
718 ///The type of the map that stores the last
719 ///edges of the shortest paths.
720 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
722 typedef NullMap <typename GR::Node,typename GR::Edge> PredMap;
723 ///Instantiates a PredMap.
725 ///This function instantiates a \ref PredMap.
726 ///\param g is the graph, to which we would like to define the PredMap.
727 ///\todo The graph alone may be insufficient for the initialization
729 static PredMap *createPredMap(const GR &g)
731 static PredMap *createPredMap(const GR &)
734 return new PredMap();
736 ///The type of the map that stores whether a nodes is processed.
738 ///The type of the map that stores whether a nodes is processed.
739 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
740 ///By default it is a NullMap.
741 ///\todo If it is set to a real map,
742 ///Dijkstra::processed() should read this.
743 ///\todo named parameter to set this type, function to read and write.
744 typedef NullMap<typename Graph::Node,bool> ProcessedMap;
745 ///Instantiates a ProcessedMap.
747 ///This function instantiates a \ref ProcessedMap.
748 ///\param g is the graph, to which
749 ///we would like to define the \ref ProcessedMap
751 static ProcessedMap *createProcessedMap(const GR &g)
753 static ProcessedMap *createProcessedMap(const GR &)
756 return new ProcessedMap();
758 ///The type of the map that stores the dists of the nodes.
760 ///The type of the map that stores the dists of the nodes.
761 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
763 typedef NullMap<typename Graph::Node,typename LM::Value> DistMap;
764 ///Instantiates a DistMap.
766 ///This function instantiates a \ref DistMap.
767 ///\param g is the graph, to which we would like to define the \ref DistMap
769 static DistMap *createDistMap(const GR &g)
771 static DistMap *createDistMap(const GR &)
774 return new DistMap();
778 /// Default traits used by \ref DijkstraWizard
780 /// To make it easier to use Dijkstra algorithm
781 ///we have created a wizard class.
782 /// This \ref DijkstraWizard class needs default traits,
783 ///as well as the \ref Dijkstra class.
784 /// The \ref DijkstraWizardBase is a class to be the default traits of the
785 /// \ref DijkstraWizard class.
786 /// \todo More named parameters are required...
787 template<class GR,class LM>
788 class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
791 typedef DijkstraWizardDefaultTraits<GR,LM> Base;
793 /// Type of the nodes in the graph.
794 typedef typename Base::Graph::Node Node;
796 /// Pointer to the underlying graph.
798 /// Pointer to the length map
800 ///Pointer to the map of predecessors edges.
802 // ///Pointer to the map of predecessors nodes.
804 ///Pointer to the map of distances.
806 ///Pointer to the source node.
812 /// This constructor does not require parameters, therefore it initiates
813 /// all of the attributes to default values (0, INVALID).
814 DijkstraWizardBase() : _g(0), _length(0), _pred(0),
816 _dist(0), _source(INVALID) {}
820 /// This constructor requires some parameters,
821 /// listed in the parameters list.
822 /// Others are initiated to 0.
823 /// \param g is the initial value of \ref _g
824 /// \param l is the initial value of \ref _length
825 /// \param s is the initial value of \ref _source
826 DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
827 _g((void *)&g), _length((void *)&l), _pred(0),
829 _dist(0), _source(s) {}
833 /// A class to make the usage of Dijkstra algorithm easier
835 /// This class is created to make it easier to use Dijkstra algorithm.
836 /// It uses the functions and features of the plain \ref Dijkstra,
837 /// but it is much simpler to use it.
839 /// Simplicity means that the way to change the types defined
840 /// in the traits class is based on functions that returns the new class
841 /// and not on templatable built-in classes.
842 /// When using the plain \ref Dijkstra
843 /// the new class with the modified type comes from
844 /// the original class by using the ::
845 /// operator. In the case of \ref DijkstraWizard only
846 /// a function have to be called and it will
847 /// return the needed class.
849 /// It does not have own \ref run method. When its \ref run method is called
850 /// it initiates a plain \ref Dijkstra class, and calls the \ref Dijkstra::run
853 class DijkstraWizard : public TR
857 ///The type of the underlying graph.
858 typedef typename TR::Graph Graph;
860 typedef typename Graph::Node Node;
862 typedef typename Graph::NodeIt NodeIt;
864 typedef typename Graph::Edge Edge;
866 typedef typename Graph::OutEdgeIt OutEdgeIt;
868 ///The type of the map that stores the edge lengths.
869 typedef typename TR::LengthMap LengthMap;
870 ///The type of the length of the edges.
871 typedef typename LengthMap::Value Value;
872 ///\brief The type of the map that stores the last
873 ///edges of the shortest paths.
874 typedef typename TR::PredMap PredMap;
875 // ///\brief The type of the map that stores the last but one
876 // ///nodes of the shortest paths.
877 // typedef typename TR::PredNodeMap PredNodeMap;
878 ///The type of the map that stores the dists of the nodes.
879 typedef typename TR::DistMap DistMap;
881 ///The heap type used by the dijkstra algorithm.
882 typedef typename TR::Heap Heap;
885 DijkstraWizard() : TR() {}
887 /// Constructor that requires parameters.
889 /// Constructor that requires parameters.
890 /// These parameters will be the default values for the traits class.
891 DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
895 DijkstraWizard(const TR &b) : TR(b) {}
899 ///Runs Dijkstra algorithm from a given node.
901 ///Runs Dijkstra algorithm from a given node.
902 ///The node can be given by the \ref source function.
905 if(Base::_source==INVALID) throw UninitializedParameter();
906 Dijkstra<Graph,LengthMap,TR>
907 dij(*(Graph*)Base::_g,*(LengthMap*)Base::_length);
908 if(Base::_pred) dij.predMap(*(PredMap*)Base::_pred);
909 // if(Base::_predNode) Dij.predNodeMap(*(PredNodeMap*)Base::_predNode);
910 if(Base::_dist) dij.distMap(*(DistMap*)Base::_dist);
911 dij.run(Base::_source);
914 ///Runs Dijkstra algorithm from the given node.
916 ///Runs Dijkstra algorithm from the given node.
917 ///\param s is the given source.
925 struct DefPredMapBase : public Base {
927 static PredMap *createPredMap(const Graph &) { return 0; };
928 DefPredMapBase(const TR &b) : TR(b) {}
931 ///\brief \ref named-templ-param "Named parameter"
932 ///function for setting PredMap type
934 /// \ref named-templ-param "Named parameter"
935 ///function for setting PredMap type
938 DijkstraWizard<DefPredMapBase<T> > predMap(const T &t)
940 Base::_pred=(void *)&t;
941 return DijkstraWizard<DefPredMapBase<T> >(*this);
946 // struct DefPredNodeMapBase : public Base {
947 // typedef T PredNodeMap;
948 // static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; };
949 // DefPredNodeMapBase(const TR &b) : TR(b) {}
952 // ///\brief \ref named-templ-param "Named parameter"
953 // ///function for setting PredNodeMap type
955 // /// \ref named-templ-param "Named parameter"
956 // ///function for setting PredNodeMap type
959 // DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t)
961 // Base::_predNode=(void *)&t;
962 // return DijkstraWizard<DefPredNodeMapBase<T> >(*this);
966 struct DefDistMapBase : public Base {
968 static DistMap *createDistMap(const Graph &) { return 0; };
969 DefDistMapBase(const TR &b) : TR(b) {}
972 ///\brief \ref named-templ-param "Named parameter"
973 ///function for setting DistMap type
975 /// \ref named-templ-param "Named parameter"
976 ///function for setting DistMap type
979 DijkstraWizard<DefDistMapBase<T> > distMap(const T &t)
981 Base::_dist=(void *)&t;
982 return DijkstraWizard<DefDistMapBase<T> >(*this);
985 /// Sets the source node, from which the Dijkstra algorithm runs.
987 /// Sets the source node, from which the Dijkstra algorithm runs.
988 /// \param s is the source node.
989 DijkstraWizard<TR> &source(Node s)
997 ///Function type interface for Dijkstra algorithm.
999 /// \ingroup flowalgs
1000 ///Function type interface for Dijkstra algorithm.
1002 ///This function also has several
1003 ///\ref named-templ-func-param "named parameters",
1004 ///they are declared as the members of class \ref DijkstraWizard.
1006 ///example shows how to use these parameters.
1008 /// dijkstra(g,length,source).predMap(preds).run();
1010 ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
1011 ///to the end of the parameter list.
1012 ///\sa DijkstraWizard
1014 template<class GR, class LM>
1015 DijkstraWizard<DijkstraWizardBase<GR,LM> >
1016 dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
1018 return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
1021 } //END OF NAMESPACE LEMON