Mapselector widget reached its first release, but there are still work to do on it, I know...
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 cross reference type used by heap.
55 /// The cross reference type used by heap.
56 /// Usually it is \c Graph::NodeMap<int>.
57 typedef typename Graph::template NodeMap<int> HeapCrossRef;
58 ///Instantiates a HeapCrossRef.
60 ///This function instantiates a \ref HeapCrossRef.
61 /// \param G is the graph, to which we would like to define the
63 /// \todo The graph alone may be insufficient for the initialization
64 static HeapCrossRef *createHeapCrossRef(const GR &G)
66 return new HeapCrossRef(G);
69 ///The heap type used by Dijkstra algorithm.
71 ///The heap type used by Dijkstra algorithm.
75 typedef BinHeap<typename Graph::Node, typename LM::Value,
76 typename GR::template NodeMap<int>,
77 std::less<Value> > Heap;
79 static Heap *createHeap(HeapCrossRef& R)
84 ///\brief The type of the map that stores the last
85 ///edges of the shortest paths.
87 ///The type of the map that stores the last
88 ///edges of the shortest paths.
89 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
91 typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
92 ///Instantiates a PredMap.
94 ///This function instantiates a \ref PredMap.
95 ///\param G is the graph, to which we would like to define the PredMap.
96 ///\todo The graph alone may be insufficient for the initialization
97 static PredMap *createPredMap(const GR &G)
99 return new PredMap(G);
102 ///The type of the map that stores whether a nodes is processed.
104 ///The type of the map that stores whether a nodes is processed.
105 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
106 ///By default it is a NullMap.
107 ///\todo If it is set to a real map,
108 ///Dijkstra::processed() should read this.
109 ///\todo named parameter to set this type, function to read and write.
110 typedef NullMap<typename Graph::Node,bool> ProcessedMap;
111 ///Instantiates a ProcessedMap.
113 ///This function instantiates a \ref ProcessedMap.
114 ///\param g is the graph, to which
115 ///we would like to define the \ref ProcessedMap
117 static ProcessedMap *createProcessedMap(const GR &g)
119 static ProcessedMap *createProcessedMap(const GR &)
122 return new ProcessedMap();
124 ///The type of the map that stores the dists of the nodes.
126 ///The type of the map that stores the dists of the nodes.
127 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
129 typedef typename Graph::template NodeMap<typename LM::Value> DistMap;
130 ///Instantiates a DistMap.
132 ///This function instantiates a \ref DistMap.
133 ///\param G is the graph, to which we would like to define the \ref DistMap
134 static DistMap *createDistMap(const GR &G)
136 return new DistMap(G);
140 ///%Dijkstra algorithm class.
142 /// \ingroup flowalgs
143 ///This class provides an efficient implementation of %Dijkstra algorithm.
144 ///The edge lengths are passed to the algorithm using a
145 ///\ref concept::ReadMap "ReadMap",
146 ///so it is easy to change it to any kind of length.
148 ///The type of the length is determined by the
149 ///\ref concept::ReadMap::Value "Value" of the length map.
151 ///It is also possible to change the underlying priority heap.
153 ///\param GR The graph type the algorithm runs on. The default value
154 ///is \ref ListGraph. The value of GR is not used directly by
155 ///Dijkstra, it is only passed to \ref DijkstraDefaultTraits.
156 ///\param LM This read-only EdgeMap determines the lengths of the
157 ///edges. It is read once for each edge, so the map may involve in
158 ///relatively time consuming process to compute the edge length if
159 ///it is necessary. The default map type is \ref
160 ///concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>". The value
161 ///of LM is not used directly by Dijkstra, it is only passed to \ref
162 ///DijkstraDefaultTraits. \param TR Traits class to set
163 ///various data types used by the algorithm. The default traits
164 ///class is \ref DijkstraDefaultTraits
165 ///"DijkstraDefaultTraits<GR,LM>". See \ref
166 ///DijkstraDefaultTraits for the documentation of a Dijkstra traits
169 ///\author Jacint Szabo and Alpar Juttner
170 ///\todo A compare object would be nice.
173 template <typename GR,
177 template <typename GR=ListGraph,
178 typename LM=typename GR::template EdgeMap<int>,
179 typename TR=DijkstraDefaultTraits<GR,LM> >
184 * \brief \ref Exception for uninitialized parameters.
186 * This error represents problems in the initialization
187 * of the parameters of the algorithms.
189 class UninitializedParameter : public lemon::UninitializedParameter {
191 virtual const char* exceptionName() const {
192 return "lemon::Dijkstra::UninitializedParameter";
197 ///The type of the underlying graph.
198 typedef typename TR::Graph Graph;
200 typedef typename Graph::Node Node;
202 typedef typename Graph::NodeIt NodeIt;
204 typedef typename Graph::Edge Edge;
206 typedef typename Graph::OutEdgeIt OutEdgeIt;
208 ///The type of the length of the edges.
209 typedef typename TR::LengthMap::Value Value;
210 ///The type of the map that stores the edge lengths.
211 typedef typename TR::LengthMap LengthMap;
212 ///\brief The type of the map that stores the last
213 ///edges of the shortest paths.
214 typedef typename TR::PredMap PredMap;
215 ///The type of the map indicating if a node is processed.
216 typedef typename TR::ProcessedMap ProcessedMap;
217 ///The type of the map that stores the dists of the nodes.
218 typedef typename TR::DistMap DistMap;
219 ///The cross reference type used for the current heap.
220 typedef typename TR::HeapCrossRef HeapCrossRef;
221 ///The heap type used by the dijkstra algorithm.
222 typedef typename TR::Heap Heap;
224 /// Pointer to the underlying graph.
226 /// Pointer to the length map
227 const LengthMap *length;
228 ///Pointer to the map of predecessors edges.
230 ///Indicates if \ref _pred is locally allocated (\c true) or not.
232 ///Pointer to the map of distances.
234 ///Indicates if \ref _dist is locally allocated (\c true) or not.
236 ///Pointer to the map of processed status of the nodes.
237 ProcessedMap *_processed;
238 ///Indicates if \ref _processed is locally allocated (\c true) or not.
239 bool local_processed;
240 ///Pointer to the heap cross references.
241 HeapCrossRef *_heap_cross_ref;
242 ///Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not.
243 bool local_heap_cross_ref;
244 ///Pointer to the heap.
246 ///Indicates if \ref _heap is locally allocated (\c true) or not.
249 ///Creates the maps if necessary.
251 ///\todo Error if \c G or are \c NULL. What about \c length?
252 ///\todo Better memory allocation (instead of new).
257 _pred = Traits::createPredMap(*G);
261 _dist = Traits::createDistMap(*G);
264 local_processed = true;
265 _processed = Traits::createProcessedMap(*G);
267 if (!_heap_cross_ref) {
268 local_heap_cross_ref = true;
269 _heap_cross_ref = Traits::createHeapCrossRef(*G);
273 _heap = Traits::createHeap(*_heap_cross_ref);
279 typedef Dijkstra Create;
281 ///\name Named template parameters
286 struct DefPredMapTraits : public Traits {
288 static PredMap *createPredMap(const Graph &G)
290 throw UninitializedParameter();
293 ///\ref named-templ-param "Named parameter" for setting PredMap type
295 ///\ref named-templ-param "Named parameter" for setting PredMap type
299 : public Dijkstra< Graph, LengthMap, DefPredMapTraits<T> > {
300 typedef Dijkstra< Graph, LengthMap, DefPredMapTraits<T> > Create;
304 struct DefDistMapTraits : public Traits {
306 static DistMap *createDistMap(const Graph &G)
308 throw UninitializedParameter();
311 ///\ref named-templ-param "Named parameter" for setting DistMap type
313 ///\ref named-templ-param "Named parameter" for setting DistMap type
317 : public Dijkstra< Graph, LengthMap, DefDistMapTraits<T> > {
318 typedef Dijkstra< Graph, LengthMap, DefDistMapTraits<T> > Create;
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 struct DefProcessedMap
335 : public Dijkstra< Graph, LengthMap, DefProcessedMapTraits<T> > {
336 typedef Dijkstra< Graph, LengthMap, DefProcessedMapTraits<T> > Create;
339 struct DefGraphProcessedMapTraits : public Traits {
340 typedef typename Graph::template NodeMap<bool> ProcessedMap;
341 static ProcessedMap *createProcessedMap(const Graph &G)
343 return new ProcessedMap(G);
346 ///\brief \ref named-templ-param "Named parameter"
347 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
349 ///\ref named-templ-param "Named parameter"
350 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
351 ///If you don't set it explicitely, it will be automatically allocated.
353 struct DefProcessedMapToBeDefaultMap
354 : public Dijkstra< Graph, LengthMap, DefGraphProcessedMapTraits> {
355 typedef Dijkstra< Graph, LengthMap, DefGraphProcessedMapTraits> Create;
358 template <class H, class CR>
359 struct DefHeapTraits : public Traits {
360 typedef CR HeapCrossRef;
362 static HeapCrossRef *createHeapCrossRef(const Graph &G) {
363 return new HeapCrossRef(G);
365 static Heap *createHeap(HeapCrossRef &R)
370 ///\ref named-templ-param "Named parameter" for setting heap and cross
373 ///\ref named-templ-param "Named parameter" for setting heap and cross
376 template <class H, class CR = typename Graph::template NodeMap<int> >
378 : public Dijkstra< Graph, LengthMap, DefHeapTraits<H, CR> > {
379 typedef Dijkstra< Graph, LengthMap, DefHeapTraits<H, CR> > Create;
393 ///\param _G the graph the algorithm will run on.
394 ///\param _length the length map used by the algorithm.
395 Dijkstra(const Graph& _G, const LengthMap& _length) :
396 G(&_G), length(&_length),
397 _pred(NULL), local_pred(false),
398 _dist(NULL), local_dist(false),
399 _processed(NULL), local_processed(false),
400 _heap_cross_ref(NULL), local_heap_cross_ref(false),
401 _heap(NULL), local_heap(false)
407 if(local_pred) delete _pred;
408 if(local_dist) delete _dist;
409 if(local_processed) delete _processed;
410 if(local_heap_cross_ref) delete _heap_cross_ref;
411 if(local_heap) delete _heap;
414 ///Sets the length map.
416 ///Sets the length map.
417 ///\return <tt> (*this) </tt>
418 Dijkstra &lengthMap(const LengthMap &m)
424 ///Sets the map storing the predecessor edges.
426 ///Sets the map storing the predecessor edges.
427 ///If you don't use this function before calling \ref run(),
428 ///it will allocate one. The destuctor deallocates this
429 ///automatically allocated map, of course.
430 ///\return <tt> (*this) </tt>
431 Dijkstra &predMap(PredMap &m)
441 ///Sets the map storing the distances calculated by the algorithm.
443 ///Sets the map storing the distances calculated by the algorithm.
444 ///If you don't use this function before calling \ref run(),
445 ///it will allocate one. The destuctor deallocates this
446 ///automatically allocated map, of course.
447 ///\return <tt> (*this) </tt>
448 Dijkstra &distMap(DistMap &m)
459 void finalizeNodeData(Node v,Value dst)
461 _processed->set(v,true);
466 ///\name Execution control
467 ///The simplest way to execute the algorithm is to use
468 ///one of the member functions called \c run(...).
470 ///If you need more control on the execution,
471 ///first you must call \ref init(), then you can add several source nodes
472 ///with \ref addSource().
473 ///Finally \ref start() will perform the actual path
478 ///Initializes the internal data structures.
480 ///Initializes the internal data structures.
486 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
487 _pred->set(u,INVALID);
488 _processed->set(u,false);
489 _heap_cross_ref->set(u,Heap::PRE_HEAP);
493 ///Adds a new source node.
495 ///Adds a new source node to the priority heap.
497 ///The optional second parameter is the initial distance of the node.
499 ///It checks if the node has already been added to the heap and
500 ///It is pushed to the heap only if either it was not in the heap
501 ///or the shortest path found till then is longer then \c dst.
502 void addSource(Node s,Value dst=0)
504 if(_heap->state(s) != Heap::IN_HEAP) {
506 } else if((*_heap)[s]<dst) {
508 _pred->set(s,INVALID);
512 ///Processes the next node in the priority heap
514 ///Processes the next node in the priority heap.
516 ///\return The processed node.
518 ///\warning The priority heap must not be empty!
519 Node processNextNode()
522 Value oldvalue=_heap->prio();
524 finalizeNodeData(v,oldvalue);
526 for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
528 switch(_heap->state(w)) {
530 _heap->push(w,oldvalue+(*length)[e]);
534 if ( oldvalue+(*length)[e] < (*_heap)[w] ) {
535 _heap->decrease(w, oldvalue+(*length)[e]);
539 case Heap::POST_HEAP:
546 ///Next node to be processed.
548 ///Next node to be processed.
550 ///\return The next node to be processed or INVALID if the priority heap
554 return _heap->empty()?_heap->top():INVALID;
557 ///\brief Returns \c false if there are nodes
558 ///to be processed in the priority heap
560 ///Returns \c false if there are nodes
561 ///to be processed in the priority heap
562 bool emptyQueue() { return _heap->empty(); }
563 ///Returns the number of the nodes to be processed in the priority heap
565 ///Returns the number of the nodes to be processed in the priority heap
567 int queueSize() { return _heap->size(); }
569 ///Executes the algorithm.
571 ///Executes the algorithm.
573 ///\pre init() must be called and at least one node should be added
574 ///with addSource() before using this function.
576 ///This method runs the %Dijkstra algorithm from the root node(s)
579 ///shortest path to each node. The algorithm computes
580 ///- The shortest path tree.
581 ///- The distance of each node from the root(s).
585 while ( !_heap->empty() ) processNextNode();
588 ///Executes the algorithm until \c dest is reached.
590 ///Executes the algorithm until \c dest is reached.
592 ///\pre init() must be called and at least one node should be added
593 ///with addSource() before using this function.
595 ///This method runs the %Dijkstra algorithm from the root node(s)
598 ///shortest path to \c dest. The algorithm computes
599 ///- The shortest path to \c dest.
600 ///- The distance of \c dest from the root(s).
602 void start(Node dest)
604 while ( !_heap->empty() && _heap->top()!=dest ) processNextNode();
605 if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio());
608 ///Executes the algorithm until a condition is met.
610 ///Executes the algorithm until a condition is met.
612 ///\pre init() must be called and at least one node should be added
613 ///with addSource() before using this function.
615 ///\param nm must be a bool (or convertible) node map. The algorithm
616 ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
617 template<class NodeBoolMap>
618 void start(const NodeBoolMap &nm)
620 while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode();
621 if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio());
624 ///Runs %Dijkstra algorithm from node \c s.
626 ///This method runs the %Dijkstra algorithm from a root node \c s
629 ///shortest path to each node. The algorithm computes
630 ///- The shortest path tree.
631 ///- The distance of each node from the root.
633 ///\note d.run(s) is just a shortcut of the following code.
645 ///Finds the shortest path between \c s and \c t.
647 ///Finds the shortest path between \c s and \c t.
649 ///\return The length of the shortest s---t path if there exists one,
651 ///\note Apart from the return value, d.run(s) is
652 ///just a shortcut of the following code.
658 Value run(Node s,Node t) {
662 return (*_pred)[t]==INVALID?0:(*_dist)[t];
667 ///\name Query Functions
668 ///The result of the %Dijkstra algorithm can be obtained using these
670 ///Before the use of these functions,
671 ///either run() or start() must be called.
675 ///Copies the shortest path to \c t into \c p
677 ///This function copies the shortest path to \c t into \c p.
678 ///If it \c t is a source itself or unreachable, then it does not
680 ///\todo Is it the right way to handle unreachable nodes?
681 ///\return Returns \c true if a path to \c t was actually copied to \c p,
682 ///\c false otherwise.
685 bool getPath(P &p,Node t)
689 typename P::Builder b(p);
690 for(b.setStartNode(t);pred(t)!=INVALID;t=predNode(t))
691 b.pushFront(pred(t));
698 ///The distance of a node from the root.
700 ///Returns the distance of a node from the root.
701 ///\pre \ref run() must be called before using this function.
702 ///\warning If node \c v in unreachable from the root the return value
703 ///of this funcion is undefined.
704 Value dist(Node v) const { return (*_dist)[v]; }
706 ///Returns the 'previous edge' of the shortest path tree.
708 ///For a node \c v it returns the 'previous edge' of the shortest path tree,
709 ///i.e. it returns the last edge of a shortest path from the root to \c
710 ///v. It is \ref INVALID
711 ///if \c v is unreachable from the root or if \c v=s. The
712 ///shortest path tree used here is equal to the shortest path tree used in
713 ///\ref predNode(). \pre \ref run() must be called before using
715 ///\todo predEdge could be a better name.
716 Edge pred(Node v) const { return (*_pred)[v]; }
718 ///Returns the 'previous node' of the shortest path tree.
720 ///For a node \c v it returns the 'previous node' of the shortest path tree,
721 ///i.e. it returns the last but one node from a shortest path from the
722 ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
723 ///\c v=s. The shortest path tree used here is equal to the shortest path
724 ///tree used in \ref pred(). \pre \ref run() must be called before
725 ///using this function.
726 Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
727 G->source((*_pred)[v]); }
729 ///Returns a reference to the NodeMap of distances.
731 ///Returns a reference to the NodeMap of distances. \pre \ref run() must
732 ///be called before using this function.
733 const DistMap &distMap() const { return *_dist;}
735 ///Returns a reference to the shortest path tree map.
737 ///Returns a reference to the NodeMap of the edges of the
738 ///shortest path tree.
739 ///\pre \ref run() must be called before using this function.
740 const PredMap &predMap() const { return *_pred;}
742 ///Checks if a node is reachable from the root.
744 ///Returns \c true if \c v is reachable from the root.
745 ///\warning The source nodes are inditated as unreached.
746 ///\pre \ref run() must be called before using this function.
748 bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; }
757 ///Default traits class of Dijkstra function.
759 ///Default traits class of Dijkstra function.
760 ///\param GR Graph type.
761 ///\param LM Type of length map.
762 template<class GR, class LM>
763 struct DijkstraWizardDefaultTraits
765 ///The graph type the algorithm runs on.
767 ///The type of the map that stores the edge lengths.
769 ///The type of the map that stores the edge lengths.
770 ///It must meet the \ref concept::ReadMap "ReadMap" concept.
771 typedef LM LengthMap;
772 //The type of the length of the edges.
773 typedef typename LM::Value Value;
774 ///The heap type used by Dijkstra algorithm.
776 /// The cross reference type used by heap.
778 /// The cross reference type used by heap.
779 /// Usually it is \c Graph::NodeMap<int>.
780 typedef typename Graph::template NodeMap<int> HeapCrossRef;
781 ///Instantiates a HeapCrossRef.
783 ///This function instantiates a \ref HeapCrossRef.
784 /// \param G is the graph, to which we would like to define the
786 /// \todo The graph alone may be insufficient for the initialization
787 static HeapCrossRef *createHeapCrossRef(const GR &G)
789 return new HeapCrossRef(G);
792 ///The heap type used by Dijkstra algorithm.
794 ///The heap type used by Dijkstra algorithm.
798 typedef BinHeap<typename Graph::Node, typename LM::Value,
799 typename GR::template NodeMap<int>,
800 std::less<Value> > Heap;
802 static Heap *createHeap(HeapCrossRef& R)
807 ///\brief The type of the map that stores the last
808 ///edges of the shortest paths.
810 ///The type of the map that stores the last
811 ///edges of the shortest paths.
812 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
814 typedef NullMap <typename GR::Node,typename GR::Edge> PredMap;
815 ///Instantiates a PredMap.
817 ///This function instantiates a \ref PredMap.
818 ///\param g is the graph, to which we would like to define the PredMap.
819 ///\todo The graph alone may be insufficient for the initialization
821 static PredMap *createPredMap(const GR &g)
823 static PredMap *createPredMap(const GR &)
826 return new PredMap();
828 ///The type of the map that stores whether a nodes is processed.
830 ///The type of the map that stores whether a nodes is processed.
831 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
832 ///By default it is a NullMap.
833 ///\todo If it is set to a real map,
834 ///Dijkstra::processed() should read this.
835 ///\todo named parameter to set this type, function to read and write.
836 typedef NullMap<typename Graph::Node,bool> ProcessedMap;
837 ///Instantiates a ProcessedMap.
839 ///This function instantiates a \ref ProcessedMap.
840 ///\param g is the graph, to which
841 ///we would like to define the \ref ProcessedMap
843 static ProcessedMap *createProcessedMap(const GR &g)
845 static ProcessedMap *createProcessedMap(const GR &)
848 return new ProcessedMap();
850 ///The type of the map that stores the dists of the nodes.
852 ///The type of the map that stores the dists of the nodes.
853 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
855 typedef NullMap<typename Graph::Node,typename LM::Value> DistMap;
856 ///Instantiates a DistMap.
858 ///This function instantiates a \ref DistMap.
859 ///\param g is the graph, to which we would like to define the \ref DistMap
861 static DistMap *createDistMap(const GR &g)
863 static DistMap *createDistMap(const GR &)
866 return new DistMap();
870 /// Default traits used by \ref DijkstraWizard
872 /// To make it easier to use Dijkstra algorithm
873 ///we have created a wizard class.
874 /// This \ref DijkstraWizard class needs default traits,
875 ///as well as the \ref Dijkstra class.
876 /// The \ref DijkstraWizardBase is a class to be the default traits of the
877 /// \ref DijkstraWizard class.
878 /// \todo More named parameters are required...
879 template<class GR,class LM>
880 class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
883 typedef DijkstraWizardDefaultTraits<GR,LM> Base;
885 /// Type of the nodes in the graph.
886 typedef typename Base::Graph::Node Node;
888 /// Pointer to the underlying graph.
890 /// Pointer to the length map
892 ///Pointer to the map of predecessors edges.
894 ///Pointer to the map of distances.
896 ///Pointer to the source node.
902 /// This constructor does not require parameters, therefore it initiates
903 /// all of the attributes to default values (0, INVALID).
904 DijkstraWizardBase() : _g(0), _length(0), _pred(0),
905 _dist(0), _source(INVALID) {}
909 /// This constructor requires some parameters,
910 /// listed in the parameters list.
911 /// Others are initiated to 0.
912 /// \param g is the initial value of \ref _g
913 /// \param l is the initial value of \ref _length
914 /// \param s is the initial value of \ref _source
915 DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
916 _g((void *)&g), _length((void *)&l), _pred(0),
917 _dist(0), _source(s) {}
921 /// A class to make the usage of Dijkstra algorithm easier
923 /// This class is created to make it easier to use Dijkstra algorithm.
924 /// It uses the functions and features of the plain \ref Dijkstra,
925 /// but it is much simpler to use it.
927 /// Simplicity means that the way to change the types defined
928 /// in the traits class is based on functions that returns the new class
929 /// and not on templatable built-in classes.
930 /// When using the plain \ref Dijkstra
931 /// the new class with the modified type comes from
932 /// the original class by using the ::
933 /// operator. In the case of \ref DijkstraWizard only
934 /// a function have to be called and it will
935 /// return the needed class.
937 /// It does not have own \ref run method. When its \ref run method is called
938 /// it initiates a plain \ref Dijkstra class, and calls the \ref
939 /// Dijkstra::run method of it.
941 class DijkstraWizard : public TR
945 ///The type of the underlying graph.
946 typedef typename TR::Graph Graph;
948 typedef typename Graph::Node Node;
950 typedef typename Graph::NodeIt NodeIt;
952 typedef typename Graph::Edge Edge;
954 typedef typename Graph::OutEdgeIt OutEdgeIt;
956 ///The type of the map that stores the edge lengths.
957 typedef typename TR::LengthMap LengthMap;
958 ///The type of the length of the edges.
959 typedef typename LengthMap::Value Value;
960 ///\brief The type of the map that stores the last
961 ///edges of the shortest paths.
962 typedef typename TR::PredMap PredMap;
963 ///The type of the map that stores the dists of the nodes.
964 typedef typename TR::DistMap DistMap;
965 ///The heap type used by the dijkstra algorithm.
966 typedef typename TR::Heap Heap;
969 DijkstraWizard() : TR() {}
971 /// Constructor that requires parameters.
973 /// Constructor that requires parameters.
974 /// These parameters will be the default values for the traits class.
975 DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
979 DijkstraWizard(const TR &b) : TR(b) {}
983 ///Runs Dijkstra algorithm from a given node.
985 ///Runs Dijkstra algorithm from a given node.
986 ///The node can be given by the \ref source function.
989 if(Base::_source==INVALID) throw UninitializedParameter();
990 Dijkstra<Graph,LengthMap,TR>
991 dij(*(Graph*)Base::_g,*(LengthMap*)Base::_length);
992 if(Base::_pred) dij.predMap(*(PredMap*)Base::_pred);
993 if(Base::_dist) dij.distMap(*(DistMap*)Base::_dist);
994 dij.run(Base::_source);
997 ///Runs Dijkstra algorithm from the given node.
999 ///Runs Dijkstra algorithm from the given node.
1000 ///\param s is the given source.
1008 struct DefPredMapBase : public Base {
1010 static PredMap *createPredMap(const Graph &) { return 0; };
1011 DefPredMapBase(const TR &b) : TR(b) {}
1014 ///\brief \ref named-templ-param "Named parameter"
1015 ///function for setting PredMap type
1017 /// \ref named-templ-param "Named parameter"
1018 ///function for setting PredMap type
1021 DijkstraWizard<DefPredMapBase<T> > predMap(const T &t)
1023 Base::_pred=(void *)&t;
1024 return DijkstraWizard<DefPredMapBase<T> >(*this);
1028 struct DefDistMapBase : public Base {
1030 static DistMap *createDistMap(const Graph &) { return 0; };
1031 DefDistMapBase(const TR &b) : TR(b) {}
1034 ///\brief \ref named-templ-param "Named parameter"
1035 ///function for setting DistMap type
1037 /// \ref named-templ-param "Named parameter"
1038 ///function for setting DistMap type
1041 DijkstraWizard<DefDistMapBase<T> > distMap(const T &t)
1043 Base::_dist=(void *)&t;
1044 return DijkstraWizard<DefDistMapBase<T> >(*this);
1047 /// Sets the source node, from which the Dijkstra algorithm runs.
1049 /// Sets the source node, from which the Dijkstra algorithm runs.
1050 /// \param s is the source node.
1051 DijkstraWizard<TR> &source(Node s)
1059 ///Function type interface for Dijkstra algorithm.
1061 /// \ingroup flowalgs
1062 ///Function type interface for Dijkstra algorithm.
1064 ///This function also has several
1065 ///\ref named-templ-func-param "named parameters",
1066 ///they are declared as the members of class \ref DijkstraWizard.
1068 ///example shows how to use these parameters.
1070 /// dijkstra(g,length,source).predMap(preds).run();
1072 ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
1073 ///to the end of the parameter list.
1074 ///\sa DijkstraWizard
1076 template<class GR, class LM>
1077 DijkstraWizard<DijkstraWizardBase<GR,LM> >
1078 dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
1080 return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
1083 } //END OF NAMESPACE LEMON