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 ///\name Named template parameters
245 struct DefPredMapTraits : public Traits {
247 static PredMap *createPredMap(const Graph &G)
249 throw UninitializedParameter();
252 ///\ref named-templ-param "Named parameter" for setting PredMap type
254 ///\ref named-templ-param "Named parameter" for setting PredMap type
258 : public Dijkstra< Graph, LengthMap, DefPredMapTraits<T> > {
259 typedef Dijkstra< Graph, LengthMap, DefPredMapTraits<T> > Create;
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
276 : public Dijkstra< Graph, LengthMap, DefDistMapTraits<T> > {
277 typedef Dijkstra< Graph, LengthMap, DefDistMapTraits<T> > Create;
281 struct DefProcessedMapTraits : public Traits {
282 typedef T ProcessedMap;
283 static ProcessedMap *createProcessedMap(const Graph &G)
285 throw UninitializedParameter();
288 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
290 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
293 struct DefProcessedMap
294 : public Dijkstra< Graph, LengthMap, DefProcessedMapTraits<T> > {
295 typedef Dijkstra< Graph, LengthMap, DefProcessedMapTraits<T> > Create;
298 struct DefGraphProcessedMapTraits : public Traits {
299 typedef typename Graph::template NodeMap<bool> ProcessedMap;
300 static ProcessedMap *createProcessedMap(const Graph &G)
302 return new ProcessedMap(G);
305 ///\brief \ref named-templ-param "Named parameter"
306 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
308 ///\ref named-templ-param "Named parameter"
309 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
310 ///If you don't set it explicitely, it will be automatically allocated.
312 struct DefProcessedMapToBeDefaultMap
313 : public Dijkstra< Graph, LengthMap, DefGraphProcessedMapTraits> {
314 typedef Dijkstra< Graph, LengthMap, DefGraphProcessedMapTraits> Create;
321 typename Graph::template NodeMap<int> _heap_map;
327 ///\param _G the graph the algorithm will run on.
328 ///\param _length the length map used by the algorithm.
329 Dijkstra(const Graph& _G, const LengthMap& _length) :
330 G(&_G), length(&_length),
331 _pred(NULL), local_pred(false),
332 _dist(NULL), local_dist(false),
333 _processed(NULL), local_processed(false),
334 _heap_map(*G,-1),_heap(_heap_map)
340 if(local_pred) delete _pred;
341 if(local_dist) delete _dist;
342 if(local_processed) delete _processed;
345 ///Sets the length map.
347 ///Sets the length map.
348 ///\return <tt> (*this) </tt>
349 Dijkstra &lengthMap(const LengthMap &m)
355 ///Sets the map storing the predecessor edges.
357 ///Sets the map storing the predecessor edges.
358 ///If you don't use this function before calling \ref run(),
359 ///it will allocate one. The destuctor deallocates this
360 ///automatically allocated map, of course.
361 ///\return <tt> (*this) </tt>
362 Dijkstra &predMap(PredMap &m)
372 ///Sets the map storing the distances calculated by the algorithm.
374 ///Sets the map storing the distances calculated by the algorithm.
375 ///If you don't use this function before calling \ref run(),
376 ///it will allocate one. The destuctor deallocates this
377 ///automatically allocated map, of course.
378 ///\return <tt> (*this) </tt>
379 Dijkstra &distMap(DistMap &m)
390 void finalizeNodeData(Node v,Value dst)
392 _processed->set(v,true);
397 ///\name Execution control
398 ///The simplest way to execute the algorithm is to use
399 ///one of the member functions called \c run(...).
401 ///If you need more control on the execution,
402 ///first you must call \ref init(), then you can add several source nodes
403 ///with \ref addSource().
404 ///Finally \ref start() will perform the actual path
409 ///Initializes the internal data structures.
411 ///Initializes the internal data structures.
413 ///\todo _heap_map's type could also be in the traits class.
414 ///\todo The heaps should be able to make themselves empty directly.
418 while(!_heap.empty()) _heap.pop();
419 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
420 _pred->set(u,INVALID);
421 _processed->set(u,false);
422 _heap_map.set(u,Heap::PRE_HEAP);
426 ///Adds a new source node.
428 ///Adds a new source node to the priority heap.
430 ///The optional second parameter is the initial distance of the node.
432 ///It checks if the node has already been added to the heap and
433 ///It is pushed to the heap only if either it was not in the heap
434 ///or the shortest path found till then is longer then \c dst.
435 void addSource(Node s,Value dst=0)
437 if(_heap.state(s) != Heap::IN_HEAP) _heap.push(s,dst);
438 else if(_heap[s]<dst) {
440 _pred->set(s,INVALID);
444 ///Processes the next node in the priority heap
446 ///Processes the next node in the priority heap.
448 ///\return The processed node.
450 ///\warning The priority heap must not be empty!
451 Node processNextNode()
454 Value oldvalue=_heap[v];
456 finalizeNodeData(v,oldvalue);
458 for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
460 switch(_heap.state(w)) {
462 _heap.push(w,oldvalue+(*length)[e]);
466 if ( oldvalue+(*length)[e] < _heap[w] ) {
467 _heap.decrease(w, oldvalue+(*length)[e]);
471 case Heap::POST_HEAP:
478 ///Next node to be processed.
480 ///Next node to be processed.
482 ///\return The next node to be processed or INVALID if the priority heap
486 return _heap.empty()?_heap.top():INVALID;
489 ///\brief Returns \c false if there are nodes
490 ///to be processed in the priority heap
492 ///Returns \c false if there are nodes
493 ///to be processed in the priority heap
494 bool emptyQueue() { return _heap.empty(); }
495 ///Returns the number of the nodes to be processed in the priority heap
497 ///Returns the number of the nodes to be processed in the priority heap
499 int queueSize() { return _heap.size(); }
501 ///Executes the algorithm.
503 ///Executes the algorithm.
505 ///\pre init() must be called and at least one node should be added
506 ///with addSource() before using this function.
508 ///This method runs the %Dijkstra algorithm from the root node(s)
511 ///shortest path to each node. The algorithm computes
512 ///- The shortest path tree.
513 ///- The distance of each node from the root(s).
517 while ( !_heap.empty() ) processNextNode();
520 ///Executes the algorithm until \c dest is reached.
522 ///Executes the algorithm until \c dest is reached.
524 ///\pre init() must be called and at least one node should be added
525 ///with addSource() before using this function.
527 ///This method runs the %Dijkstra algorithm from the root node(s)
530 ///shortest path to \c dest. The algorithm computes
531 ///- The shortest path to \c dest.
532 ///- The distance of \c dest from the root(s).
534 void start(Node dest)
536 while ( !_heap.empty() && _heap.top()!=dest ) processNextNode();
537 if ( !_heap.empty() ) finalizeNodeData(_heap.top(),_heap.prio());
540 ///Executes the algorithm until a condition is met.
542 ///Executes the algorithm until a condition is met.
544 ///\pre init() must be called and at least one node should be added
545 ///with addSource() before using this function.
547 ///\param nm must be a bool (or convertible) node map. The algorithm
548 ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
549 template<class NodeBoolMap>
550 void start(const NodeBoolMap &nm)
552 while ( !_heap.empty() && !nm[_heap.top()] ) processNextNode();
553 if ( !_heap.empty() ) finalizeNodeData(_heap.top(),_heap.prio());
556 ///Runs %Dijkstra algorithm from node \c s.
558 ///This method runs the %Dijkstra algorithm from a root node \c s
561 ///shortest path to each node. The algorithm computes
562 ///- The shortest path tree.
563 ///- The distance of each node from the root.
565 ///\note d.run(s) is just a shortcut of the following code.
577 ///Finds the shortest path between \c s and \c t.
579 ///Finds the shortest path between \c s and \c t.
581 ///\return The length of the shortest s---t path if there exists one,
583 ///\note Apart from the return value, d.run(s) is
584 ///just a shortcut of the following code.
590 Value run(Node s,Node t) {
594 return (*_pred)[t]==INVALID?0:(*_dist)[t];
599 ///\name Query Functions
600 ///The result of the %Dijkstra algorithm can be obtained using these
602 ///Before the use of these functions,
603 ///either run() or start() must be called.
607 ///Copies the shortest path to \c t into \c p
609 ///This function copies the shortest path to \c t into \c p.
610 ///If it \c t is a source itself or unreachable, then it does not
612 ///\todo Is it the right way to handle unreachable nodes?
613 ///\return Returns \c true if a path to \c t was actually copied to \c p,
614 ///\c false otherwise.
617 bool getPath(P &p,Node t)
621 typename P::Builder b(p);
622 for(b.setStartNode(t);pred(t)!=INVALID;t=predNode(t))
623 b.pushFront(pred(t));
630 ///The distance of a node from the root.
632 ///Returns the distance of a node from the root.
633 ///\pre \ref run() must be called before using this function.
634 ///\warning If node \c v in unreachable from the root the return value
635 ///of this funcion is undefined.
636 Value dist(Node v) const { return (*_dist)[v]; }
638 ///Returns the 'previous edge' of the shortest path tree.
640 ///For a node \c v it returns the 'previous edge' of the shortest path tree,
641 ///i.e. it returns the last edge of a shortest path from the root to \c
642 ///v. It is \ref INVALID
643 ///if \c v is unreachable from the root or if \c v=s. The
644 ///shortest path tree used here is equal to the shortest path tree used in
645 ///\ref predNode(). \pre \ref run() must be called before using
647 ///\todo predEdge could be a better name.
648 Edge pred(Node v) const { return (*_pred)[v]; }
650 ///Returns the 'previous node' of the shortest path tree.
652 ///For a node \c v it returns the 'previous node' of the shortest path tree,
653 ///i.e. it returns the last but one node from a shortest path from the
654 ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
655 ///\c v=s. The shortest path tree used here is equal to the shortest path
656 ///tree used in \ref pred(). \pre \ref run() must be called before
657 ///using this function.
658 Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
659 G->source((*_pred)[v]); }
661 ///Returns a reference to the NodeMap of distances.
663 ///Returns a reference to the NodeMap of distances. \pre \ref run() must
664 ///be called before using this function.
665 const DistMap &distMap() const { return *_dist;}
667 ///Returns a reference to the shortest path tree map.
669 ///Returns a reference to the NodeMap of the edges of the
670 ///shortest path tree.
671 ///\pre \ref run() must be called before using this function.
672 const PredMap &predMap() const { return *_pred;}
674 ///Checks if a node is reachable from the root.
676 ///Returns \c true if \c v is reachable from the root.
677 ///\warning The source nodes are inditated as unreached.
678 ///\pre \ref run() must be called before using this function.
680 bool reached(Node v) { return _heap_map[v]!=Heap::PRE_HEAP; }
689 ///Default traits class of Dijkstra function.
691 ///Default traits class of Dijkstra function.
692 ///\param GR Graph type.
693 ///\param LM Type of length map.
694 template<class GR, class LM>
695 struct DijkstraWizardDefaultTraits
697 ///The graph type the algorithm runs on.
699 ///The type of the map that stores the edge lengths.
701 ///The type of the map that stores the edge lengths.
702 ///It must meet the \ref concept::ReadMap "ReadMap" concept.
703 typedef LM LengthMap;
704 //The type of the length of the edges.
705 typedef typename LM::Value Value;
706 ///The heap type used by Dijkstra algorithm.
708 ///The heap type used by Dijkstra algorithm.
712 typedef BinHeap<typename Graph::Node,
714 typename GR::template NodeMap<int>,
715 std::less<Value> > Heap;
717 ///\brief The type of the map that stores the last
718 ///edges of the shortest paths.
720 ///The type of the map that stores the last
721 ///edges of the shortest paths.
722 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
724 typedef NullMap <typename GR::Node,typename GR::Edge> PredMap;
725 ///Instantiates a PredMap.
727 ///This function instantiates a \ref PredMap.
728 ///\param g is the graph, to which we would like to define the PredMap.
729 ///\todo The graph alone may be insufficient for the initialization
731 static PredMap *createPredMap(const GR &g)
733 static PredMap *createPredMap(const GR &)
736 return new PredMap();
738 ///The type of the map that stores whether a nodes is processed.
740 ///The type of the map that stores whether a nodes is processed.
741 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
742 ///By default it is a NullMap.
743 ///\todo If it is set to a real map,
744 ///Dijkstra::processed() should read this.
745 ///\todo named parameter to set this type, function to read and write.
746 typedef NullMap<typename Graph::Node,bool> ProcessedMap;
747 ///Instantiates a ProcessedMap.
749 ///This function instantiates a \ref ProcessedMap.
750 ///\param g is the graph, to which
751 ///we would like to define the \ref ProcessedMap
753 static ProcessedMap *createProcessedMap(const GR &g)
755 static ProcessedMap *createProcessedMap(const GR &)
758 return new ProcessedMap();
760 ///The type of the map that stores the dists of the nodes.
762 ///The type of the map that stores the dists of the nodes.
763 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
765 typedef NullMap<typename Graph::Node,typename LM::Value> DistMap;
766 ///Instantiates a DistMap.
768 ///This function instantiates a \ref DistMap.
769 ///\param g is the graph, to which we would like to define the \ref DistMap
771 static DistMap *createDistMap(const GR &g)
773 static DistMap *createDistMap(const GR &)
776 return new DistMap();
780 /// Default traits used by \ref DijkstraWizard
782 /// To make it easier to use Dijkstra algorithm
783 ///we have created a wizard class.
784 /// This \ref DijkstraWizard class needs default traits,
785 ///as well as the \ref Dijkstra class.
786 /// The \ref DijkstraWizardBase is a class to be the default traits of the
787 /// \ref DijkstraWizard class.
788 /// \todo More named parameters are required...
789 template<class GR,class LM>
790 class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
793 typedef DijkstraWizardDefaultTraits<GR,LM> Base;
795 /// Type of the nodes in the graph.
796 typedef typename Base::Graph::Node Node;
798 /// Pointer to the underlying graph.
800 /// Pointer to the length map
802 ///Pointer to the map of predecessors edges.
804 // ///Pointer to the map of predecessors nodes.
806 ///Pointer to the map of distances.
808 ///Pointer to the source node.
814 /// This constructor does not require parameters, therefore it initiates
815 /// all of the attributes to default values (0, INVALID).
816 DijkstraWizardBase() : _g(0), _length(0), _pred(0),
818 _dist(0), _source(INVALID) {}
822 /// This constructor requires some parameters,
823 /// listed in the parameters list.
824 /// Others are initiated to 0.
825 /// \param g is the initial value of \ref _g
826 /// \param l is the initial value of \ref _length
827 /// \param s is the initial value of \ref _source
828 DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
829 _g((void *)&g), _length((void *)&l), _pred(0),
831 _dist(0), _source(s) {}
835 /// A class to make the usage of Dijkstra algorithm easier
837 /// This class is created to make it easier to use Dijkstra algorithm.
838 /// It uses the functions and features of the plain \ref Dijkstra,
839 /// but it is much simpler to use it.
841 /// Simplicity means that the way to change the types defined
842 /// in the traits class is based on functions that returns the new class
843 /// and not on templatable built-in classes.
844 /// When using the plain \ref Dijkstra
845 /// the new class with the modified type comes from
846 /// the original class by using the ::
847 /// operator. In the case of \ref DijkstraWizard only
848 /// a function have to be called and it will
849 /// return the needed class.
851 /// It does not have own \ref run method. When its \ref run method is called
852 /// it initiates a plain \ref Dijkstra class, and calls the \ref Dijkstra::run
855 class DijkstraWizard : public TR
859 ///The type of the underlying graph.
860 typedef typename TR::Graph Graph;
862 typedef typename Graph::Node Node;
864 typedef typename Graph::NodeIt NodeIt;
866 typedef typename Graph::Edge Edge;
868 typedef typename Graph::OutEdgeIt OutEdgeIt;
870 ///The type of the map that stores the edge lengths.
871 typedef typename TR::LengthMap LengthMap;
872 ///The type of the length of the edges.
873 typedef typename LengthMap::Value Value;
874 ///\brief The type of the map that stores the last
875 ///edges of the shortest paths.
876 typedef typename TR::PredMap PredMap;
877 // ///\brief The type of the map that stores the last but one
878 // ///nodes of the shortest paths.
879 // typedef typename TR::PredNodeMap PredNodeMap;
880 ///The type of the map that stores the dists of the nodes.
881 typedef typename TR::DistMap DistMap;
883 ///The heap type used by the dijkstra algorithm.
884 typedef typename TR::Heap Heap;
887 DijkstraWizard() : TR() {}
889 /// Constructor that requires parameters.
891 /// Constructor that requires parameters.
892 /// These parameters will be the default values for the traits class.
893 DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
897 DijkstraWizard(const TR &b) : TR(b) {}
901 ///Runs Dijkstra algorithm from a given node.
903 ///Runs Dijkstra algorithm from a given node.
904 ///The node can be given by the \ref source function.
907 if(Base::_source==INVALID) throw UninitializedParameter();
908 Dijkstra<Graph,LengthMap,TR>
909 dij(*(Graph*)Base::_g,*(LengthMap*)Base::_length);
910 if(Base::_pred) dij.predMap(*(PredMap*)Base::_pred);
911 // if(Base::_predNode) Dij.predNodeMap(*(PredNodeMap*)Base::_predNode);
912 if(Base::_dist) dij.distMap(*(DistMap*)Base::_dist);
913 dij.run(Base::_source);
916 ///Runs Dijkstra algorithm from the given node.
918 ///Runs Dijkstra algorithm from the given node.
919 ///\param s is the given source.
927 struct DefPredMapBase : public Base {
929 static PredMap *createPredMap(const Graph &) { return 0; };
930 DefPredMapBase(const TR &b) : TR(b) {}
933 ///\brief \ref named-templ-param "Named parameter"
934 ///function for setting PredMap type
936 /// \ref named-templ-param "Named parameter"
937 ///function for setting PredMap type
940 DijkstraWizard<DefPredMapBase<T> > predMap(const T &t)
942 Base::_pred=(void *)&t;
943 return DijkstraWizard<DefPredMapBase<T> >(*this);
948 // struct DefPredNodeMapBase : public Base {
949 // typedef T PredNodeMap;
950 // static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; };
951 // DefPredNodeMapBase(const TR &b) : TR(b) {}
954 // ///\brief \ref named-templ-param "Named parameter"
955 // ///function for setting PredNodeMap type
957 // /// \ref named-templ-param "Named parameter"
958 // ///function for setting PredNodeMap type
961 // DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t)
963 // Base::_predNode=(void *)&t;
964 // return DijkstraWizard<DefPredNodeMapBase<T> >(*this);
968 struct DefDistMapBase : public Base {
970 static DistMap *createDistMap(const Graph &) { return 0; };
971 DefDistMapBase(const TR &b) : TR(b) {}
974 ///\brief \ref named-templ-param "Named parameter"
975 ///function for setting DistMap type
977 /// \ref named-templ-param "Named parameter"
978 ///function for setting DistMap type
981 DijkstraWizard<DefDistMapBase<T> > distMap(const T &t)
983 Base::_dist=(void *)&t;
984 return DijkstraWizard<DefDistMapBase<T> >(*this);
987 /// Sets the source node, from which the Dijkstra algorithm runs.
989 /// Sets the source node, from which the Dijkstra algorithm runs.
990 /// \param s is the source node.
991 DijkstraWizard<TR> &source(Node s)
999 ///Function type interface for Dijkstra algorithm.
1001 /// \ingroup flowalgs
1002 ///Function type interface for Dijkstra algorithm.
1004 ///This function also has several
1005 ///\ref named-templ-func-param "named parameters",
1006 ///they are declared as the members of class \ref DijkstraWizard.
1008 ///example shows how to use these parameters.
1010 /// dijkstra(g,length,source).predMap(preds).run();
1012 ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
1013 ///to the end of the parameter list.
1014 ///\sa DijkstraWizard
1016 template<class GR, class LM>
1017 DijkstraWizard<DijkstraWizardBase<GR,LM> >
1018 dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
1020 return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
1023 } //END OF NAMESPACE LEMON