2 * src/lemon/dijkstra.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 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>
34 ///Default traits class of Dijkstra class.
36 ///Default traits class of Dijkstra class.
37 ///\param GR Graph type.
38 ///\param LM Type of length map.
39 template<class GR, class LM>
40 struct DijkstraDefaultTraits
42 ///The graph type the algorithm runs on.
44 ///The type of the map that stores the edge lengths.
46 ///The type of the map that stores the edge lengths.
47 ///It must meet the \ref concept::ReadMap "ReadMap" concept.
49 //The type of the length of the edges.
50 typedef typename LM::Value Value;
51 ///The heap type used by Dijkstra algorithm.
53 ///The heap type used by Dijkstra algorithm.
57 typedef BinHeap<typename Graph::Node,
59 typename GR::template NodeMap<int>,
60 std::less<Value> > Heap;
62 ///\brief The type of the map that stores the last
63 ///edges of the shortest paths.
65 ///The type of the map that stores the last
66 ///edges of the shortest paths.
67 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
69 typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
70 ///Instantiates a PredMap.
72 ///This function instantiates a \ref PredMap.
73 ///\param G is the graph, to which we would like to define the PredMap.
74 ///\todo The graph alone may be insufficient for the initialization
75 static PredMap *createPredMap(const GR &G)
77 return new PredMap(G);
79 // ///\brief The type of the map that stores the last but one
80 // ///nodes of the shortest paths.
82 // ///The type of the map that stores the last but one
83 // ///nodes of the shortest paths.
84 // ///It must meet the \ref concept::WriteMap "WriteMap" concept.
86 // typedef NullMap<typename Graph::Node,typename Graph::Node> PredNodeMap;
87 // ///Instantiates a PredNodeMap.
89 // ///This function instantiates a \ref PredNodeMap.
90 // ///\param G is the graph, to which
91 // ///we would like to define the \ref PredNodeMap
92 // static PredNodeMap *createPredNodeMap(const GR &G)
94 // return new PredNodeMap();
97 ///The type of the map that stores whether a nodes is processed.
99 ///The type of the map that stores whether a nodes is processed.
100 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
101 ///By default it is a NullMap.
102 ///\todo If it is set to a real map,
103 ///Dijkstra::processed() should read this.
104 ///\todo named parameter to set this type, function to read and write.
105 typedef NullMap<typename Graph::Node,bool> ProcessedMap;
106 ///Instantiates a ProcessedMap.
108 ///This function instantiates a \ref ProcessedMap.
109 ///\param G is the graph, to which
110 ///we would like to define the \ref ProcessedMap
111 static ProcessedMap *createProcessedMap(const GR &G)
113 return new ProcessedMap();
115 ///The type of the map that stores the dists of the nodes.
117 ///The type of the map that stores the dists of the nodes.
118 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
120 typedef typename Graph::template NodeMap<typename LM::Value> DistMap;
121 ///Instantiates a DistMap.
123 ///This function instantiates a \ref DistMap.
124 ///\param G is the graph, to which we would like to define the \ref DistMap
125 static DistMap *createDistMap(const GR &G)
127 return new DistMap(G);
131 ///%Dijkstra algorithm class.
133 /// \ingroup flowalgs
134 ///This class provides an efficient implementation of %Dijkstra algorithm.
135 ///The edge lengths are passed to the algorithm using a
136 ///\ref concept::ReadMap "ReadMap",
137 ///so it is easy to change it to any kind of length.
139 ///The type of the length is determined by the
140 ///\ref concept::ReadMap::Value "Value" of the length map.
142 ///It is also possible to change the underlying priority heap.
144 ///\param GR The graph type the algorithm runs on. The default value
145 ///is \ref ListGraph. The value of GR is not used directly by
146 ///Dijkstra, it is only passed to \ref DijkstraDefaultTraits.
147 ///\param LM This read-only EdgeMap determines the lengths of the
148 ///edges. It is read once for each edge, so the map may involve in
149 ///relatively time consuming process to compute the edge length if
150 ///it is necessary. The default map type is \ref
151 ///concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>". The value
152 ///of LM is not used directly by Dijkstra, it is only passed to \ref
153 ///DijkstraDefaultTraits. \param TR Traits class to set
154 ///various data types used by the algorithm. The default traits
155 ///class is \ref DijkstraDefaultTraits
156 ///"DijkstraDefaultTraits<GR,LM>". See \ref
157 ///DijkstraDefaultTraits for the documentation of a Dijkstra traits
160 ///\author Jacint Szabo and Alpar Juttner
161 ///\todo A compare object would be nice.
164 template <typename GR,
168 template <typename GR=ListGraph,
169 typename LM=typename GR::template EdgeMap<int>,
170 typename TR=DijkstraDefaultTraits<GR,LM> >
175 * \brief \ref Exception for uninitialized parameters.
177 * This error represents problems in the initialization
178 * of the parameters of the algorithms.
180 class UninitializedParameter : public lemon::UninitializedParameter {
182 virtual const char* exceptionName() const {
183 return "lemon::Dijkstra::UninitializedParameter";
188 ///The type of the underlying graph.
189 typedef typename TR::Graph Graph;
191 typedef typename Graph::Node Node;
193 typedef typename Graph::NodeIt NodeIt;
195 typedef typename Graph::Edge Edge;
197 typedef typename Graph::OutEdgeIt OutEdgeIt;
199 ///The type of the length of the edges.
200 typedef typename TR::LengthMap::Value Value;
201 ///The type of the map that stores the edge lengths.
202 typedef typename TR::LengthMap LengthMap;
203 ///\brief The type of the map that stores the last
204 ///edges of the shortest paths.
205 typedef typename TR::PredMap PredMap;
206 // ///\brief The type of the map that stores the last but one
207 // ///nodes of the shortest paths.
208 // typedef typename TR::PredNodeMap PredNodeMap;
209 ///The type of the map indicating if a node is processed.
210 typedef typename TR::ProcessedMap ProcessedMap;
211 ///The type of the map that stores the dists of the nodes.
212 typedef typename TR::DistMap DistMap;
213 ///The heap type used by the dijkstra algorithm.
214 typedef typename TR::Heap Heap;
216 /// Pointer to the underlying graph.
218 /// Pointer to the length map
219 const LengthMap *length;
220 ///Pointer to the map of predecessors edges.
222 ///Indicates if \ref _pred is locally allocated (\c true) or not.
224 // ///Pointer to the map of predecessors nodes.
225 // PredNodeMap *_predNode;
226 // ///Indicates if \ref _predNode is locally allocated (\c true) or not.
227 // bool local_predNode;
228 ///Pointer to the map of distances.
230 ///Indicates if \ref _dist is locally allocated (\c true) or not.
232 ///Pointer to the map of processed status of the nodes.
233 ProcessedMap *_processed;
234 ///Indicates if \ref _processed is locally allocated (\c true) or not.
235 bool local_processed;
237 // ///The source node of the last execution.
240 ///Creates the maps if necessary.
242 ///\todo Error if \c G or are \c NULL. What about \c length?
243 ///\todo Better memory allocation (instead of new).
248 _pred = Traits::createPredMap(*G);
251 // local_predNode = true;
252 // _predNode = Traits::createPredNodeMap(*G);
256 _dist = Traits::createDistMap(*G);
259 local_processed = true;
260 _processed = Traits::createProcessedMap(*G);
266 ///\name Named template parameters
271 struct DefPredMapTraits : public Traits {
273 static PredMap *createPredMap(const Graph &G)
275 throw UninitializedParameter();
278 ///\ref named-templ-param "Named parameter" for setting PredMap type
280 ///\ref named-templ-param "Named parameter" for setting PredMap type
283 class DefPredMap : public Dijkstra< Graph,
285 DefPredMapTraits<T> > { };
287 // template <class T>
288 // struct DefPredNodeMapTraits : public Traits {
289 // typedef T PredNodeMap;
290 // static PredNodeMap *createPredNodeMap(const Graph &G)
292 // throw UninitializedParameter();
295 // ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
297 // ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
299 // template <class T>
300 // class DefPredNodeMap : public Dijkstra< Graph,
302 // DefPredNodeMapTraits<T> > { };
305 struct DefDistMapTraits : public Traits {
307 static DistMap *createDistMap(const Graph &G)
309 throw UninitializedParameter();
312 ///\ref named-templ-param "Named parameter" for setting DistMap type
314 ///\ref named-templ-param "Named parameter" for setting DistMap type
317 class DefDistMap : public Dijkstra< Graph,
319 DefDistMapTraits<T> > { };
322 struct DefProcessedMapTraits : public Traits {
323 typedef T ProcessedMap;
324 static ProcessedMap *createProcessedMap(const Graph &G)
326 throw UninitializedParameter();
329 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
331 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
334 class DefProcessedMap : public Dijkstra< Graph,
336 DefProcessedMapTraits<T> > { };
338 struct DefGraphProcessedMapTraits : public Traits {
339 typedef typename Graph::template NodeMap<bool> ProcessedMap;
340 static ProcessedMap *createProcessedMap(const Graph &G)
342 return new ProcessedMap(G);
345 ///\brief \ref named-templ-param "Named parameter"
346 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
348 ///\ref named-templ-param "Named parameter"
349 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
350 ///If you don't set it explicitely, it will be automatically allocated.
352 class DefProcessedMapToBeDefaultMap :
353 public Dijkstra< Graph,
355 DefGraphProcessedMapTraits> { };
361 typename Graph::template NodeMap<int> _heap_map;
367 ///\param _G the graph the algorithm will run on.
368 ///\param _length the length map used by the algorithm.
369 Dijkstra(const Graph& _G, const LengthMap& _length) :
370 G(&_G), length(&_length),
371 _pred(NULL), local_pred(false),
372 // _predNode(NULL), local_predNode(false),
373 _dist(NULL), local_dist(false),
374 _processed(NULL), local_processed(false),
375 _heap_map(*G,-1),_heap(_heap_map)
381 if(local_pred) delete _pred;
382 // if(local_predNode) delete _predNode;
383 if(local_dist) delete _dist;
384 if(local_processed) delete _processed;
387 ///Sets the length map.
389 ///Sets the length map.
390 ///\return <tt> (*this) </tt>
391 Dijkstra &lengthMap(const LengthMap &m)
397 ///Sets the map storing the predecessor edges.
399 ///Sets the map storing the predecessor edges.
400 ///If you don't use this function before calling \ref run(),
401 ///it will allocate one. The destuctor deallocates this
402 ///automatically allocated map, of course.
403 ///\return <tt> (*this) </tt>
404 Dijkstra &predMap(PredMap &m)
414 // ///Sets the map storing the predecessor nodes.
416 // ///Sets the map storing the predecessor nodes.
417 // ///If you don't use this function before calling \ref run(),
418 // ///it will allocate one. The destuctor deallocates this
419 // ///automatically allocated map, of course.
420 // ///\return <tt> (*this) </tt>
421 // Dijkstra &predNodeMap(PredNodeMap &m)
423 // if(local_predNode) {
425 // local_predNode=false;
431 ///Sets the map storing the distances calculated by the algorithm.
433 ///Sets the map storing the distances calculated by the algorithm.
434 ///If you don't use this function before calling \ref run(),
435 ///it will allocate one. The destuctor deallocates this
436 ///automatically allocated map, of course.
437 ///\return <tt> (*this) </tt>
438 Dijkstra &distMap(DistMap &m)
449 void finalizeNodeData(Node v,Value dst)
451 _processed->set(v,true);
453 // if((*_pred)[v]!=INVALID)
454 // _predNode->set(v,G->source((*_pred)[v])); ///\todo What to do?
458 ///\name Execution control
459 ///The simplest way to execute the algorithm is to use
460 ///one of the member functions called \c run(...).
462 ///If you need more control on the execution,
463 ///first you must call \ref init(), then you can add several source nodes
464 ///with \ref addSource().
465 ///Finally \ref start() will perform the actual path
470 ///Initializes the internal data structures.
472 ///Initializes the internal data structures.
474 ///\todo _heap_map's type could also be in the traits class.
475 ///\todo The heaps should be able to make themselves empty directly.
479 while(!_heap.empty()) _heap.pop();
480 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
481 _pred->set(u,INVALID);
482 // _predNode->set(u,INVALID);
483 _processed->set(u,false);
484 _heap_map.set(u,Heap::PRE_HEAP);
488 ///Adds a new source node.
490 ///Adds a new source node to the priority heap.
492 ///The optional second parameter is the initial distance of the node.
494 ///It checks if the node has already been added to the heap and
495 ///It is pushed to the heap only if either it was not in the heap
496 ///or the shortest path found till then is longer then \c dst.
497 void addSource(Node s,Value dst=0)
500 if(_heap.state(s) != Heap::IN_HEAP) _heap.push(s,dst);
501 else if(_heap[s]<dst) {
503 _pred->set(s,INVALID);
507 ///Processes the next node in the priority heap
509 ///Processes the next node in the priority heap.
511 ///\warning The priority heap must not be empty!
512 void processNextNode()
515 Value oldvalue=_heap[v];
517 finalizeNodeData(v,oldvalue);
519 for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
521 switch(_heap.state(w)) {
523 _heap.push(w,oldvalue+(*length)[e]);
525 // _predNode->set(w,v);
528 if ( oldvalue+(*length)[e] < _heap[w] ) {
529 _heap.decrease(w, oldvalue+(*length)[e]);
531 // _predNode->set(w,v);
534 case Heap::POST_HEAP:
540 ///\brief Returns \c false if there are nodes
541 ///to be processed in the priority heap
543 ///Returns \c false if there are nodes
544 ///to be processed in the priority heap
545 bool emptyQueue() { return _heap.empty(); }
546 ///Returns the number of the nodes to be processed in the priority heap
548 ///Returns the number of the nodes to be processed in the priority heap
550 int queueSize() { return _heap.size(); }
552 ///Executes the algorithm.
554 ///Executes the algorithm.
556 ///\pre init() must be called and at least one node should be added
557 ///with addSource() before using this function.
559 ///This method runs the %Dijkstra algorithm from the root node(s)
562 ///shortest path to each node. The algorithm computes
563 ///- The shortest path tree.
564 ///- The distance of each node from the root(s).
568 while ( !_heap.empty() ) processNextNode();
571 ///Executes the algorithm until \c dest is reached.
573 ///Executes the algorithm until \c dest is reached.
575 ///\pre init() must be called and at least one node should be added
576 ///with addSource() before using this function.
578 ///This method runs the %Dijkstra algorithm from the root node(s)
581 ///shortest path to \c dest. The algorithm computes
582 ///- The shortest path to \c dest.
583 ///- The distance of \c dest from the root(s).
585 void start(Node dest)
587 while ( !_heap.empty() && _heap.top()!=dest ) processNextNode();
588 if ( !_heap.empty() ) finalizeNodeData(_heap.top(),_heap.prio());
591 ///Executes the algorithm until a condition is met.
593 ///Executes the algorithm until a condition is met.
595 ///\pre init() must be called and at least one node should be added
596 ///with addSource() before using this function.
598 ///\param nm must be a bool (or convertible) node map. The algorithm
599 ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
601 void start(const NM &nm)
603 while ( !_heap.empty() && !nm[_heap.top()] ) processNextNode();
604 if ( !_heap.empty() ) finalizeNodeData(_heap.top(),_heap.prio());
607 ///Runs %Dijkstra algorithm from node \c s.
609 ///This method runs the %Dijkstra algorithm from a root node \c s
612 ///shortest path to each node. The algorithm computes
613 ///- The shortest path tree.
614 ///- The distance of each node from the root.
616 ///\note d.run(s) is just a shortcut of the following code.
628 ///Finds the shortest path between \c s and \c t.
630 ///Finds the shortest path between \c s and \c t.
632 ///\return The length of the shortest s---t path if there exists one,
634 ///\note Apart from the return value, d.run(s) is
635 ///just a shortcut of the following code.
641 Value run(Node s,Node t) {
645 return (*_pred)[t]==INVALID?0:(*_dist)[t];
650 ///\name Query Functions
651 ///The result of the %Dijkstra algorithm can be obtained using these
653 ///Before the use of these functions,
654 ///either run() or start() must be called.
658 ///The distance of a node from the root.
660 ///Returns the distance of a node from the root.
661 ///\pre \ref run() must be called before using this function.
662 ///\warning If node \c v in unreachable from the root the return value
663 ///of this funcion is undefined.
664 Value dist(Node v) const { return (*_dist)[v]; }
666 ///Returns the 'previous edge' of the shortest path tree.
668 ///For a node \c v it returns the 'previous edge' of the shortest path tree,
669 ///i.e. it returns the last edge of a shortest path from the root to \c
670 ///v. It is \ref INVALID
671 ///if \c v is unreachable from the root or if \c v=s. The
672 ///shortest path tree used here is equal to the shortest path tree used in
673 ///\ref predNode(Node v). \pre \ref run() must be called before using
675 ///\todo predEdge could be a better name.
676 Edge pred(Node v) const { return (*_pred)[v]; }
678 ///Returns the 'previous node' of the shortest path tree.
680 ///For a node \c v it returns the 'previous node' of the shortest path tree,
681 ///i.e. it returns the last but one node from a shortest path from the
682 ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
683 ///\c v=s. The shortest path tree used here is equal to the shortest path
684 ///tree used in \ref pred(Node v). \pre \ref run() must be called before
685 ///using this function.
686 Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
687 G->source((*_pred)[v]); }
689 ///Returns a reference to the NodeMap of distances.
691 ///Returns a reference to the NodeMap of distances. \pre \ref run() must
692 ///be called before using this function.
693 const DistMap &distMap() const { return *_dist;}
695 ///Returns a reference to the shortest path tree map.
697 ///Returns a reference to the NodeMap of the edges of the
698 ///shortest path tree.
699 ///\pre \ref run() must be called before using this function.
700 const PredMap &predMap() const { return *_pred;}
702 // ///Returns a reference to the map of nodes of shortest paths.
704 // ///Returns a reference to the NodeMap of the last but one nodes of the
705 // ///shortest path tree.
706 // ///\pre \ref run() must be called before using this function.
707 // const PredNodeMap &predNodeMap() const { return *_predNode;}
709 ///Checks if a node is reachable from the root.
711 ///Returns \c true if \c v is reachable from the root.
712 ///\warning The source nodes are inditated as unreached.
713 ///\pre \ref run() must be called before using this function.
715 bool reached(Node v) { return _heap_map[v]!=Heap::PRE_HEAP; }
724 ///Default traits class of Dijkstra function.
726 ///Default traits class of Dijkstra function.
727 ///\param GR Graph type.
728 ///\param LM Type of length map.
729 template<class GR, class LM>
730 struct DijkstraWizardDefaultTraits
732 ///The graph type the algorithm runs on.
734 ///The type of the map that stores the edge lengths.
736 ///The type of the map that stores the edge lengths.
737 ///It must meet the \ref concept::ReadMap "ReadMap" concept.
738 typedef LM LengthMap;
739 //The type of the length of the edges.
740 typedef typename LM::Value Value;
741 ///The heap type used by Dijkstra algorithm.
743 ///The heap type used by Dijkstra algorithm.
747 typedef BinHeap<typename Graph::Node,
749 typename GR::template NodeMap<int>,
750 std::less<Value> > Heap;
752 ///\brief The type of the map that stores the last
753 ///edges of the shortest paths.
755 ///The type of the map that stores the last
756 ///edges of the shortest paths.
757 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
759 typedef NullMap <typename GR::Node,typename GR::Edge> PredMap;
760 ///Instantiates a PredMap.
762 ///This function instantiates a \ref PredMap.
763 ///\param G is the graph, to which we would like to define the PredMap.
764 ///\todo The graph alone may be insufficient for the initialization
765 static PredMap *createPredMap(const GR &G)
767 return new PredMap();
769 ///The type of the map that stores whether a nodes is processed.
771 ///The type of the map that stores whether a nodes is processed.
772 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
773 ///By default it is a NullMap.
774 ///\todo If it is set to a real map,
775 ///Dijkstra::processed() should read this.
776 ///\todo named parameter to set this type, function to read and write.
777 typedef NullMap<typename Graph::Node,bool> ProcessedMap;
778 ///Instantiates a ProcessedMap.
780 ///This function instantiates a \ref ProcessedMap.
781 ///\param G is the graph, to which
782 ///we would like to define the \ref ProcessedMap
783 static ProcessedMap *createProcessedMap(const GR &G)
785 return new ProcessedMap();
787 ///The type of the map that stores the dists of the nodes.
789 ///The type of the map that stores the dists of the nodes.
790 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
792 typedef NullMap<typename Graph::Node,typename LM::Value> DistMap;
793 ///Instantiates a DistMap.
795 ///This function instantiates a \ref DistMap.
796 ///\param G is the graph, to which we would like to define the \ref DistMap
797 static DistMap *createDistMap(const GR &G)
799 return new DistMap();
803 /// Default traits used by \ref DijkstraWizard
805 /// To make it easier to use Dijkstra algorithm
806 ///we have created a wizard class.
807 /// This \ref DijkstraWizard class needs default traits,
808 ///as well as the \ref Dijkstra class.
809 /// The \ref DijkstraWizardBase is a class to be the default traits of the
810 /// \ref DijkstraWizard class.
811 /// \todo More named parameters are required...
812 template<class GR,class LM>
813 class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
816 typedef DijkstraWizardDefaultTraits<GR,LM> Base;
818 /// Type of the nodes in the graph.
819 typedef typename Base::Graph::Node Node;
821 /// Pointer to the underlying graph.
823 /// Pointer to the length map
825 ///Pointer to the map of predecessors edges.
827 // ///Pointer to the map of predecessors nodes.
829 ///Pointer to the map of distances.
831 ///Pointer to the source node.
837 /// This constructor does not require parameters, therefore it initiates
838 /// all of the attributes to default values (0, INVALID).
839 DijkstraWizardBase() : _g(0), _length(0), _pred(0),
841 _dist(0), _source(INVALID) {}
845 /// This constructor requires some parameters,
846 /// listed in the parameters list.
847 /// Others are initiated to 0.
848 /// \param g is the initial value of \ref _g
849 /// \param l is the initial value of \ref _length
850 /// \param s is the initial value of \ref _source
851 DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
852 _g((void *)&g), _length((void *)&l), _pred(0),
854 _dist(0), _source(s) {}
858 /// A class to make the usage of Dijkstra algorithm easier
860 /// This class is created to make it easier to use Dijkstra algorithm.
861 /// It uses the functions and features of the plain \ref Dijkstra,
862 /// but it is much simpler to use it.
864 /// Simplicity means that the way to change the types defined
865 /// in the traits class is based on functions that returns the new class
866 /// and not on templatable built-in classes.
867 /// When using the plain \ref Dijkstra
868 /// the new class with the modified type comes from
869 /// the original class by using the ::
870 /// operator. In the case of \ref DijkstraWizard only
871 /// a function have to be called and it will
872 /// return the needed class.
874 /// It does not have own \ref run method. When its \ref run method is called
875 /// it initiates a plain \ref Dijkstra class, and calls the \ref Dijkstra::run
878 class DijkstraWizard : public TR
882 ///The type of the underlying graph.
883 typedef typename TR::Graph Graph;
885 typedef typename Graph::Node Node;
887 typedef typename Graph::NodeIt NodeIt;
889 typedef typename Graph::Edge Edge;
891 typedef typename Graph::OutEdgeIt OutEdgeIt;
893 ///The type of the map that stores the edge lengths.
894 typedef typename TR::LengthMap LengthMap;
895 ///The type of the length of the edges.
896 typedef typename LengthMap::Value Value;
897 ///\brief The type of the map that stores the last
898 ///edges of the shortest paths.
899 typedef typename TR::PredMap PredMap;
900 // ///\brief The type of the map that stores the last but one
901 // ///nodes of the shortest paths.
902 // typedef typename TR::PredNodeMap PredNodeMap;
903 ///The type of the map that stores the dists of the nodes.
904 typedef typename TR::DistMap DistMap;
906 ///The heap type used by the dijkstra algorithm.
907 typedef typename TR::Heap Heap;
910 DijkstraWizard() : TR() {}
912 /// Constructor that requires parameters.
914 /// Constructor that requires parameters.
915 /// These parameters will be the default values for the traits class.
916 DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
920 DijkstraWizard(const TR &b) : TR(b) {}
924 ///Runs Dijkstra algorithm from a given node.
926 ///Runs Dijkstra algorithm from a given node.
927 ///The node can be given by the \ref source function.
930 if(Base::_source==INVALID) throw UninitializedParameter();
931 Dijkstra<Graph,LengthMap,TR>
932 Dij(*(Graph*)Base::_g,*(LengthMap*)Base::_length);
933 if(Base::_pred) Dij.predMap(*(PredMap*)Base::_pred);
934 // if(Base::_predNode) Dij.predNodeMap(*(PredNodeMap*)Base::_predNode);
935 if(Base::_dist) Dij.distMap(*(DistMap*)Base::_dist);
936 Dij.run(Base::_source);
939 ///Runs Dijkstra algorithm from the given node.
941 ///Runs Dijkstra algorithm from the given node.
942 ///\param s is the given source.
950 struct DefPredMapBase : public Base {
952 static PredMap *createPredMap(const Graph &G) { return 0; };
953 DefPredMapBase(const TR &b) : TR(b) {}
956 ///\brief \ref named-templ-param "Named parameter"
957 ///function for setting PredMap type
959 /// \ref named-templ-param "Named parameter"
960 ///function for setting PredMap type
963 DijkstraWizard<DefPredMapBase<T> > predMap(const T &t)
965 Base::_pred=(void *)&t;
966 return DijkstraWizard<DefPredMapBase<T> >(*this);
971 // struct DefPredNodeMapBase : public Base {
972 // typedef T PredNodeMap;
973 // static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; };
974 // DefPredNodeMapBase(const TR &b) : TR(b) {}
977 // ///\brief \ref named-templ-param "Named parameter"
978 // ///function for setting PredNodeMap type
980 // /// \ref named-templ-param "Named parameter"
981 // ///function for setting PredNodeMap type
984 // DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t)
986 // Base::_predNode=(void *)&t;
987 // return DijkstraWizard<DefPredNodeMapBase<T> >(*this);
991 struct DefDistMapBase : public Base {
993 static DistMap *createDistMap(const Graph &G) { return 0; };
994 DefDistMapBase(const TR &b) : TR(b) {}
997 ///\brief \ref named-templ-param "Named parameter"
998 ///function for setting DistMap type
1000 /// \ref named-templ-param "Named parameter"
1001 ///function for setting DistMap type
1004 DijkstraWizard<DefDistMapBase<T> > distMap(const T &t)
1006 Base::_dist=(void *)&t;
1007 return DijkstraWizard<DefDistMapBase<T> >(*this);
1010 /// Sets the source node, from which the Dijkstra algorithm runs.
1012 /// Sets the source node, from which the Dijkstra algorithm runs.
1013 /// \param s is the source node.
1014 DijkstraWizard<TR> &source(Node s)
1022 ///Function type interface for Dijkstra algorithm.
1024 /// \ingroup flowalgs
1025 ///Function type interface for Dijkstra algorithm.
1027 ///This function also has several
1028 ///\ref named-templ-func-param "named parameters",
1029 ///they are declared as the members of class \ref DijkstraWizard.
1031 ///example shows how to use these parameters.
1033 /// dijkstra(g,length,source).predMap(preds).run();
1035 ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
1036 ///to the end of the parameter list.
1037 ///\sa DijkstraWizard
1039 template<class GR, class LM>
1040 DijkstraWizard<DijkstraWizardBase<GR,LM> >
1041 dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
1043 return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
1046 } //END OF NAMESPACE LEMON