Small bug corrected.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #ifndef LEMON_DIJKSTRA_H
20 #define LEMON_DIJKSTRA_H
24 ///\brief Dijkstra algorithm.
26 ///\todo dijkstraZero() solution should be revised.
28 #include <lemon/list_graph.h>
29 #include <lemon/bin_heap.h>
30 #include <lemon/bits/path_dump.h>
31 #include <lemon/bits/invalid.h>
32 #include <lemon/error.h>
33 #include <lemon/maps.h>
38 template<class T> T dijkstraZero() {return 0;}
40 ///Default traits class of Dijkstra class.
42 ///Default traits class of Dijkstra class.
43 ///\param GR Graph type.
44 ///\param LM Type of length map.
45 template<class GR, class LM>
46 struct DijkstraDefaultTraits
48 ///The graph type the algorithm runs on.
50 ///The type of the map that stores the edge lengths.
52 ///The type of the map that stores the edge lengths.
53 ///It must meet the \ref concepts::ReadMap "ReadMap" concept.
55 //The type of the length of the edges.
56 typedef typename LM::Value Value;
57 /// The cross reference type used by heap.
59 /// The cross reference type used by heap.
60 /// Usually it is \c Graph::NodeMap<int>.
61 typedef typename Graph::template NodeMap<int> HeapCrossRef;
62 ///Instantiates a HeapCrossRef.
64 ///This function instantiates a \ref HeapCrossRef.
65 /// \param G is the graph, to which we would like to define the
67 static HeapCrossRef *createHeapCrossRef(const GR &G)
69 return new HeapCrossRef(G);
72 ///The heap type used by Dijkstra algorithm.
74 ///The heap type used by Dijkstra algorithm.
78 typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap;
80 static Heap *createHeap(HeapCrossRef& R)
85 ///\brief The type of the map that stores the last
86 ///edges of the shortest paths.
88 ///The type of the map that stores the last
89 ///edges of the shortest paths.
90 ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
92 typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
93 ///Instantiates a PredMap.
95 ///This function instantiates a \ref PredMap.
96 ///\param G is the graph, to which we would like to define the PredMap.
97 ///\todo The graph alone may be insufficient for the initialization
98 static PredMap *createPredMap(const GR &G)
100 return new PredMap(G);
103 ///The type of the map that stores whether a nodes is processed.
105 ///The type of the map that stores whether a nodes is processed.
106 ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
107 ///By default it is a NullMap.
108 ///\todo If it is set to a real map,
109 ///Dijkstra::processed() should read this.
110 ///\todo named parameter to set this type, function to read and write.
111 typedef NullMap<typename Graph::Node,bool> ProcessedMap;
112 ///Instantiates a ProcessedMap.
114 ///This function instantiates a \ref ProcessedMap.
115 ///\param g is the graph, to which
116 ///we would like to define the \ref ProcessedMap
118 static ProcessedMap *createProcessedMap(const GR &g)
120 static ProcessedMap *createProcessedMap(const GR &)
123 return new ProcessedMap();
125 ///The type of the map that stores the dists of the nodes.
127 ///The type of the map that stores the dists of the nodes.
128 ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
130 typedef typename Graph::template NodeMap<typename LM::Value> DistMap;
131 ///Instantiates a DistMap.
133 ///This function instantiates a \ref DistMap.
134 ///\param G is the graph, to which we would like to define the \ref DistMap
135 static DistMap *createDistMap(const GR &G)
137 return new DistMap(G);
141 ///%Dijkstra algorithm class.
143 /// \ingroup flowalgs
144 ///This class provides an efficient implementation of %Dijkstra algorithm.
145 ///The edge lengths are passed to the algorithm using a
146 ///\ref concepts::ReadMap "ReadMap",
147 ///so it is easy to change it to any kind of length.
149 ///The type of the length is determined by the
150 ///\ref concepts::ReadMap::Value "Value" of the length map.
152 ///It is also possible to change the underlying priority heap.
154 ///\param GR The graph type the algorithm runs on. The default value
155 ///is \ref ListGraph. The value of GR is not used directly by
156 ///Dijkstra, it is only passed to \ref DijkstraDefaultTraits.
157 ///\param LM This read-only EdgeMap determines the lengths of the
158 ///edges. It is read once for each edge, so the map may involve in
159 ///relatively time consuming process to compute the edge length if
160 ///it is necessary. The default map type is \ref
161 ///concepts::Graph::EdgeMap "Graph::EdgeMap<int>". The value
162 ///of LM is not used directly by Dijkstra, it is only passed to \ref
163 ///DijkstraDefaultTraits. \param TR Traits class to set
164 ///various data types used by the algorithm. The default traits
165 ///class is \ref DijkstraDefaultTraits
166 ///"DijkstraDefaultTraits<GR,LM>". See \ref
167 ///DijkstraDefaultTraits for the documentation of a Dijkstra traits
170 ///\author Jacint Szabo and Alpar Juttner
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* what() const throw() {
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 Better memory allocation (instead of new).
256 _pred = Traits::createPredMap(*G);
260 _dist = Traits::createDistMap(*G);
263 local_processed = true;
264 _processed = Traits::createProcessedMap(*G);
266 if (!_heap_cross_ref) {
267 local_heap_cross_ref = true;
268 _heap_cross_ref = Traits::createHeapCrossRef(*G);
272 _heap = Traits::createHeap(*_heap_cross_ref);
278 typedef Dijkstra Create;
280 ///\name Named template parameters
285 struct DefPredMapTraits : public Traits {
287 static PredMap *createPredMap(const Graph &)
289 throw UninitializedParameter();
292 ///\ref named-templ-param "Named parameter" for setting PredMap type
294 ///\ref named-templ-param "Named parameter" for setting PredMap type
298 : public Dijkstra< Graph, LengthMap, DefPredMapTraits<T> > {
299 typedef Dijkstra< Graph, LengthMap, DefPredMapTraits<T> > Create;
303 struct DefDistMapTraits : public Traits {
305 static DistMap *createDistMap(const Graph &)
307 throw UninitializedParameter();
310 ///\ref named-templ-param "Named parameter" for setting DistMap type
312 ///\ref named-templ-param "Named parameter" for setting DistMap type
316 : public Dijkstra< Graph, LengthMap, DefDistMapTraits<T> > {
317 typedef Dijkstra< Graph, LengthMap, DefDistMapTraits<T> > Create;
321 struct DefProcessedMapTraits : public Traits {
322 typedef T ProcessedMap;
323 static ProcessedMap *createProcessedMap(const Graph &G)
325 throw UninitializedParameter();
328 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
330 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
333 struct DefProcessedMap
334 : public Dijkstra< Graph, LengthMap, DefProcessedMapTraits<T> > {
335 typedef Dijkstra< Graph, LengthMap, DefProcessedMapTraits<T> > Create;
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 struct DefProcessedMapToBeDefaultMap
353 : public Dijkstra< Graph, LengthMap, DefGraphProcessedMapTraits> {
354 typedef Dijkstra< Graph, LengthMap, DefGraphProcessedMapTraits> Create;
357 template <class H, class CR>
358 struct DefHeapTraits : public Traits {
359 typedef CR HeapCrossRef;
361 static HeapCrossRef *createHeapCrossRef(const Graph &) {
362 throw UninitializedParameter();
364 static Heap *createHeap(HeapCrossRef &)
366 throw UninitializedParameter();
369 ///\brief \ref named-templ-param "Named parameter" for setting
370 ///heap and cross reference type
372 ///\ref named-templ-param "Named parameter" for setting heap and cross
375 template <class H, class CR = typename Graph::template NodeMap<int> >
377 : public Dijkstra< Graph, LengthMap, DefHeapTraits<H, CR> > {
378 typedef Dijkstra< Graph, LengthMap, DefHeapTraits<H, CR> > Create;
381 template <class H, class CR>
382 struct DefStandardHeapTraits : public Traits {
383 typedef CR HeapCrossRef;
385 static HeapCrossRef *createHeapCrossRef(const Graph &G) {
386 return new HeapCrossRef(G);
388 static Heap *createHeap(HeapCrossRef &R)
393 ///\brief \ref named-templ-param "Named parameter" for setting
394 ///heap and cross reference type with automatic allocation
396 ///\ref named-templ-param "Named parameter" for setting heap and cross
397 ///reference type. It can allocate the heap and the cross reference
398 ///object if the cross reference's constructor waits for the graph as
399 ///parameter and the heap's constructor waits for the cross reference.
400 template <class H, class CR = typename Graph::template NodeMap<int> >
401 struct DefStandardHeap
402 : public Dijkstra< Graph, LengthMap, DefStandardHeapTraits<H, CR> > {
403 typedef Dijkstra< Graph, LengthMap, DefStandardHeapTraits<H, CR> >
418 ///\param _G the graph the algorithm will run on.
419 ///\param _length the length map used by the algorithm.
420 Dijkstra(const Graph& _G, const LengthMap& _length) :
421 G(&_G), length(&_length),
422 _pred(NULL), local_pred(false),
423 _dist(NULL), local_dist(false),
424 _processed(NULL), local_processed(false),
425 _heap_cross_ref(NULL), local_heap_cross_ref(false),
426 _heap(NULL), local_heap(false)
432 if(local_pred) delete _pred;
433 if(local_dist) delete _dist;
434 if(local_processed) delete _processed;
435 if(local_heap_cross_ref) delete _heap_cross_ref;
436 if(local_heap) delete _heap;
439 ///Sets the length map.
441 ///Sets the length map.
442 ///\return <tt> (*this) </tt>
443 Dijkstra &lengthMap(const LengthMap &m)
449 ///Sets the map storing the predecessor edges.
451 ///Sets the map storing the predecessor edges.
452 ///If you don't use this function before calling \ref run(),
453 ///it will allocate one. The destuctor deallocates this
454 ///automatically allocated map, of course.
455 ///\return <tt> (*this) </tt>
456 Dijkstra &predMap(PredMap &m)
466 ///Sets the map storing the distances calculated by the algorithm.
468 ///Sets the map storing the distances calculated by the algorithm.
469 ///If you don't use this function before calling \ref run(),
470 ///it will allocate one. The destuctor deallocates this
471 ///automatically allocated map, of course.
472 ///\return <tt> (*this) </tt>
473 Dijkstra &distMap(DistMap &m)
483 ///Sets the heap and the cross reference used by algorithm.
485 ///Sets the heap and the cross reference used by algorithm.
486 ///If you don't use this function before calling \ref run(),
487 ///it will allocate one. The destuctor deallocates this
488 ///automatically allocated heap and cross reference, of course.
489 ///\return <tt> (*this) </tt>
490 Dijkstra &heap(Heap& heap, HeapCrossRef &crossRef)
492 if(local_heap_cross_ref) {
493 delete _heap_cross_ref;
494 local_heap_cross_ref=false;
496 _heap_cross_ref = &crossRef;
506 void finalizeNodeData(Node v,Value dst)
508 _processed->set(v,true);
513 ///\name Execution control
514 ///The simplest way to execute the algorithm is to use
515 ///one of the member functions called \c run(...).
517 ///If you need more control on the execution,
518 ///first you must call \ref init(), then you can add several source nodes
519 ///with \ref addSource().
520 ///Finally \ref start() will perform the actual path
525 ///Initializes the internal data structures.
527 ///Initializes the internal data structures.
533 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
534 _pred->set(u,INVALID);
535 _processed->set(u,false);
536 _heap_cross_ref->set(u,Heap::PRE_HEAP);
540 ///Adds a new source node.
542 ///Adds a new source node to the priority heap.
544 ///The optional second parameter is the initial distance of the node.
546 ///It checks if the node has already been added to the heap and
547 ///it is pushed to the heap only if either it was not in the heap
548 ///or the shortest path found till then is shorter than \c dst.
549 void addSource(Node s,Value dst=dijkstraZero<Value>())
551 if(_heap->state(s) != Heap::IN_HEAP) {
553 } else if((*_heap)[s]<dst) {
555 _pred->set(s,INVALID);
559 ///Processes the next node in the priority heap
561 ///Processes the next node in the priority heap.
563 ///\return The processed node.
565 ///\warning The priority heap must not be empty!
566 Node processNextNode()
569 Value oldvalue=_heap->prio();
571 finalizeNodeData(v,oldvalue);
573 for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
575 switch(_heap->state(w)) {
577 _heap->push(w,oldvalue+(*length)[e]);
581 if ( oldvalue+(*length)[e] < (*_heap)[w] ) {
582 _heap->decrease(w, oldvalue+(*length)[e]);
586 case Heap::POST_HEAP:
593 ///Next node to be processed.
595 ///Next node to be processed.
597 ///\return The next node to be processed or INVALID if the priority heap
601 return _heap->empty()?_heap->top():INVALID;
604 ///\brief Returns \c false if there are nodes
605 ///to be processed in the priority heap
607 ///Returns \c false if there are nodes
608 ///to be processed in the priority heap
609 bool emptyQueue() { return _heap->empty(); }
610 ///Returns the number of the nodes to be processed in the priority heap
612 ///Returns the number of the nodes to be processed in the priority heap
614 int queueSize() { return _heap->size(); }
616 ///Executes the algorithm.
618 ///Executes the algorithm.
620 ///\pre init() must be called and at least one node should be added
621 ///with addSource() before using this function.
623 ///This method runs the %Dijkstra algorithm from the root node(s)
626 ///shortest path to each node. The algorithm computes
627 ///- The shortest path tree.
628 ///- The distance of each node from the root(s).
632 while ( !_heap->empty() ) processNextNode();
635 ///Executes the algorithm until \c dest is reached.
637 ///Executes the algorithm until \c dest is reached.
639 ///\pre init() must be called and at least one node should be added
640 ///with addSource() before using this function.
642 ///This method runs the %Dijkstra algorithm from the root node(s)
645 ///shortest path to \c dest. The algorithm computes
646 ///- The shortest path to \c dest.
647 ///- The distance of \c dest from the root(s).
649 void start(Node dest)
651 while ( !_heap->empty() && _heap->top()!=dest ) processNextNode();
652 if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio());
655 ///Executes the algorithm until a condition is met.
657 ///Executes the algorithm until a condition is met.
659 ///\pre init() must be called and at least one node should be added
660 ///with addSource() before using this function.
662 ///\param nm must be a bool (or convertible) node map. The algorithm
663 ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
664 template<class NodeBoolMap>
665 void start(const NodeBoolMap &nm)
667 while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode();
668 if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio());
671 ///Runs %Dijkstra algorithm from node \c s.
673 ///This method runs the %Dijkstra algorithm from a root node \c s
676 ///shortest path to each node. The algorithm computes
677 ///- The shortest path tree.
678 ///- The distance of each node from the root.
680 ///\note d.run(s) is just a shortcut of the following code.
692 ///Finds the shortest path between \c s and \c t.
694 ///Finds the shortest path between \c s and \c t.
696 ///\return The length of the shortest s---t path if there exists one,
698 ///\note Apart from the return value, d.run(s) is
699 ///just a shortcut of the following code.
705 Value run(Node s,Node t) {
709 return (*_pred)[t]==INVALID?dijkstraZero<Value>():(*_dist)[t];
714 ///\name Query Functions
715 ///The result of the %Dijkstra algorithm can be obtained using these
717 ///Before the use of these functions,
718 ///either run() or start() must be called.
722 typedef PredMapPath<Graph, PredMap> Path;
724 ///Gives back the shortest path.
726 ///Gives back the shortest path.
727 ///\pre The \c t should be reachable from the source.
730 return Path(*G, *_pred, t);
733 ///The distance of a node from the root.
735 ///Returns the distance of a node from the root.
736 ///\pre \ref run() must be called before using this function.
737 ///\warning If node \c v in unreachable from the root the return value
738 ///of this funcion is undefined.
739 Value dist(Node v) const { return (*_dist)[v]; }
741 ///Returns the 'previous edge' of the shortest path tree.
743 ///For a node \c v it returns the 'previous edge' of the shortest path tree,
744 ///i.e. it returns the last edge of a shortest path from the root to \c
745 ///v. It is \ref INVALID
746 ///if \c v is unreachable from the root or if \c v=s. The
747 ///shortest path tree used here is equal to the shortest path tree used in
748 ///\ref predNode(). \pre \ref run() must be called before using
750 Edge predEdge(Node v) const { return (*_pred)[v]; }
752 ///Returns the 'previous node' of the shortest path tree.
754 ///For a node \c v it returns the 'previous node' of the shortest path tree,
755 ///i.e. it returns the last but one node from a shortest path from the
756 ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
757 ///\c v=s. The shortest path tree used here is equal to the shortest path
758 ///tree used in \ref predEdge(). \pre \ref run() must be called before
759 ///using this function.
760 Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
761 G->source((*_pred)[v]); }
763 ///Returns a reference to the NodeMap of distances.
765 ///Returns a reference to the NodeMap of distances. \pre \ref run() must
766 ///be called before using this function.
767 const DistMap &distMap() const { return *_dist;}
769 ///Returns a reference to the shortest path tree map.
771 ///Returns a reference to the NodeMap of the edges of the
772 ///shortest path tree.
773 ///\pre \ref run() must be called before using this function.
774 const PredMap &predMap() const { return *_pred;}
776 ///Checks if a node is reachable from the root.
778 ///Returns \c true if \c v is reachable from the root.
779 ///\warning The source nodes are inditated as unreached.
780 ///\pre \ref run() must be called before using this function.
782 bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; }
784 ///Checks if a node is processed.
786 ///Returns \c true if \c v is processed, i.e. the shortest
787 ///path to \c v has already found.
788 ///\pre \ref run() must be called before using this function.
790 bool processed(Node v) { return (*_heap_cross_ref)[v] == Heap::POST_HEAP; }
799 ///Default traits class of Dijkstra function.
801 ///Default traits class of Dijkstra function.
802 ///\param GR Graph type.
803 ///\param LM Type of length map.
804 template<class GR, class LM>
805 struct DijkstraWizardDefaultTraits
807 ///The graph type the algorithm runs on.
809 ///The type of the map that stores the edge lengths.
811 ///The type of the map that stores the edge lengths.
812 ///It must meet the \ref concepts::ReadMap "ReadMap" concept.
813 typedef LM LengthMap;
814 //The type of the length of the edges.
815 typedef typename LM::Value Value;
816 ///The heap type used by Dijkstra algorithm.
818 /// The cross reference type used by heap.
820 /// The cross reference type used by heap.
821 /// Usually it is \c Graph::NodeMap<int>.
822 typedef typename Graph::template NodeMap<int> HeapCrossRef;
823 ///Instantiates a HeapCrossRef.
825 ///This function instantiates a \ref HeapCrossRef.
826 /// \param G is the graph, to which we would like to define the
828 /// \todo The graph alone may be insufficient for the initialization
829 static HeapCrossRef *createHeapCrossRef(const GR &G)
831 return new HeapCrossRef(G);
834 ///The heap type used by Dijkstra algorithm.
836 ///The heap type used by Dijkstra algorithm.
840 typedef BinHeap<typename LM::Value, typename GR::template NodeMap<int>,
841 std::less<Value> > Heap;
843 static Heap *createHeap(HeapCrossRef& R)
848 ///\brief The type of the map that stores the last
849 ///edges of the shortest paths.
851 ///The type of the map that stores the last
852 ///edges of the shortest paths.
853 ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
855 typedef NullMap <typename GR::Node,typename GR::Edge> PredMap;
856 ///Instantiates a PredMap.
858 ///This function instantiates a \ref PredMap.
859 ///\param g is the graph, to which we would like to define the PredMap.
860 ///\todo The graph alone may be insufficient for the initialization
862 static PredMap *createPredMap(const GR &g)
864 static PredMap *createPredMap(const GR &)
867 return new PredMap();
869 ///The type of the map that stores whether a nodes is processed.
871 ///The type of the map that stores whether a nodes is processed.
872 ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
873 ///By default it is a NullMap.
874 ///\todo If it is set to a real map,
875 ///Dijkstra::processed() should read this.
876 ///\todo named parameter to set this type, function to read and write.
877 typedef NullMap<typename Graph::Node,bool> ProcessedMap;
878 ///Instantiates a ProcessedMap.
880 ///This function instantiates a \ref ProcessedMap.
881 ///\param g is the graph, to which
882 ///we would like to define the \ref ProcessedMap
884 static ProcessedMap *createProcessedMap(const GR &g)
886 static ProcessedMap *createProcessedMap(const GR &)
889 return new ProcessedMap();
891 ///The type of the map that stores the dists of the nodes.
893 ///The type of the map that stores the dists of the nodes.
894 ///It must meet the \ref concepts::WriteMap "WriteMap" concept.
896 typedef NullMap<typename Graph::Node,typename LM::Value> DistMap;
897 ///Instantiates a DistMap.
899 ///This function instantiates a \ref DistMap.
900 ///\param g is the graph, to which we would like to define the \ref DistMap
902 static DistMap *createDistMap(const GR &g)
904 static DistMap *createDistMap(const GR &)
907 return new DistMap();
911 /// Default traits used by \ref DijkstraWizard
913 /// To make it easier to use Dijkstra algorithm
914 ///we have created a wizard class.
915 /// This \ref DijkstraWizard class needs default traits,
916 ///as well as the \ref Dijkstra class.
917 /// The \ref DijkstraWizardBase is a class to be the default traits of the
918 /// \ref DijkstraWizard class.
919 /// \todo More named parameters are required...
920 template<class GR,class LM>
921 class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
924 typedef DijkstraWizardDefaultTraits<GR,LM> Base;
926 /// Type of the nodes in the graph.
927 typedef typename Base::Graph::Node Node;
929 /// Pointer to the underlying graph.
931 /// Pointer to the length map
933 ///Pointer to the map of predecessors edges.
935 ///Pointer to the map of distances.
937 ///Pointer to the source node.
943 /// This constructor does not require parameters, therefore it initiates
944 /// all of the attributes to default values (0, INVALID).
945 DijkstraWizardBase() : _g(0), _length(0), _pred(0),
946 _dist(0), _source(INVALID) {}
950 /// This constructor requires some parameters,
951 /// listed in the parameters list.
952 /// Others are initiated to 0.
953 /// \param g is the initial value of \ref _g
954 /// \param l is the initial value of \ref _length
955 /// \param s is the initial value of \ref _source
956 DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
957 _g((void *)&g), _length((void *)&l), _pred(0),
958 _dist(0), _source(s) {}
962 /// A class to make the usage of Dijkstra algorithm easier
964 /// This class is created to make it easier to use Dijkstra algorithm.
965 /// It uses the functions and features of the plain \ref Dijkstra,
966 /// but it is much simpler to use it.
968 /// Simplicity means that the way to change the types defined
969 /// in the traits class is based on functions that returns the new class
970 /// and not on templatable built-in classes.
971 /// When using the plain \ref Dijkstra
972 /// the new class with the modified type comes from
973 /// the original class by using the ::
974 /// operator. In the case of \ref DijkstraWizard only
975 /// a function have to be called and it will
976 /// return the needed class.
978 /// It does not have own \ref run method. When its \ref run method is called
979 /// it initiates a plain \ref Dijkstra class, and calls the \ref
980 /// Dijkstra::run method of it.
982 class DijkstraWizard : public TR
986 ///The type of the underlying graph.
987 typedef typename TR::Graph Graph;
989 typedef typename Graph::Node Node;
991 typedef typename Graph::NodeIt NodeIt;
993 typedef typename Graph::Edge Edge;
995 typedef typename Graph::OutEdgeIt OutEdgeIt;
997 ///The type of the map that stores the edge lengths.
998 typedef typename TR::LengthMap LengthMap;
999 ///The type of the length of the edges.
1000 typedef typename LengthMap::Value Value;
1001 ///\brief The type of the map that stores the last
1002 ///edges of the shortest paths.
1003 typedef typename TR::PredMap PredMap;
1004 ///The type of the map that stores the dists of the nodes.
1005 typedef typename TR::DistMap DistMap;
1006 ///The heap type used by the dijkstra algorithm.
1007 typedef typename TR::Heap Heap;
1010 DijkstraWizard() : TR() {}
1012 /// Constructor that requires parameters.
1014 /// Constructor that requires parameters.
1015 /// These parameters will be the default values for the traits class.
1016 DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
1020 DijkstraWizard(const TR &b) : TR(b) {}
1022 ~DijkstraWizard() {}
1024 ///Runs Dijkstra algorithm from a given node.
1026 ///Runs Dijkstra algorithm from a given node.
1027 ///The node can be given by the \ref source function.
1030 if(Base::_source==INVALID) throw UninitializedParameter();
1031 Dijkstra<Graph,LengthMap,TR>
1032 dij(*(Graph*)Base::_g,*(LengthMap*)Base::_length);
1033 if(Base::_pred) dij.predMap(*(PredMap*)Base::_pred);
1034 if(Base::_dist) dij.distMap(*(DistMap*)Base::_dist);
1035 dij.run(Base::_source);
1038 ///Runs Dijkstra algorithm from the given node.
1040 ///Runs Dijkstra algorithm from the given node.
1041 ///\param s is the given source.
1049 struct DefPredMapBase : public Base {
1051 static PredMap *createPredMap(const Graph &) { return 0; };
1052 DefPredMapBase(const TR &b) : TR(b) {}
1055 ///\brief \ref named-templ-param "Named parameter"
1056 ///function for setting PredMap type
1058 /// \ref named-templ-param "Named parameter"
1059 ///function for setting PredMap type
1062 DijkstraWizard<DefPredMapBase<T> > predMap(const T &t)
1064 Base::_pred=(void *)&t;
1065 return DijkstraWizard<DefPredMapBase<T> >(*this);
1069 struct DefDistMapBase : public Base {
1071 static DistMap *createDistMap(const Graph &) { return 0; };
1072 DefDistMapBase(const TR &b) : TR(b) {}
1075 ///\brief \ref named-templ-param "Named parameter"
1076 ///function for setting DistMap type
1078 /// \ref named-templ-param "Named parameter"
1079 ///function for setting DistMap type
1082 DijkstraWizard<DefDistMapBase<T> > distMap(const T &t)
1084 Base::_dist=(void *)&t;
1085 return DijkstraWizard<DefDistMapBase<T> >(*this);
1088 /// Sets the source node, from which the Dijkstra algorithm runs.
1090 /// Sets the source node, from which the Dijkstra algorithm runs.
1091 /// \param s is the source node.
1092 DijkstraWizard<TR> &source(Node s)
1100 ///Function type interface for Dijkstra algorithm.
1102 /// \ingroup flowalgs
1103 ///Function type interface for Dijkstra algorithm.
1105 ///This function also has several
1106 ///\ref named-templ-func-param "named parameters",
1107 ///they are declared as the members of class \ref DijkstraWizard.
1109 ///example shows how to use these parameters.
1111 /// dijkstra(g,length,source).predMap(preds).run();
1113 ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
1114 ///to the end of the parameter list.
1115 ///\sa DijkstraWizard
1117 template<class GR, class LM>
1118 DijkstraWizard<DijkstraWizardBase<GR,LM> >
1119 dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
1121 return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
1124 } //END OF NAMESPACE LEMON