vmap.h: Enables one to create maps with a virtual base class.
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/invalid.h>
31 #include <lemon/error.h>
32 #include <lemon/maps.h>
36 template<class T> T dijkstraZero() {return 0;}
38 ///Default traits class of Dijkstra class.
40 ///Default traits class of Dijkstra class.
41 ///\param GR Graph type.
42 ///\param LM Type of length map.
43 template<class GR, class LM>
44 struct DijkstraDefaultTraits
46 ///The graph type the algorithm runs on.
48 ///The type of the map that stores the edge lengths.
50 ///The type of the map that stores the edge lengths.
51 ///It must meet the \ref concept::ReadMap "ReadMap" concept.
53 //The type of the length of the edges.
54 typedef typename LM::Value Value;
55 /// The cross reference type used by heap.
57 /// The cross reference type used by heap.
58 /// Usually it is \c Graph::NodeMap<int>.
59 typedef typename Graph::template NodeMap<int> HeapCrossRef;
60 ///Instantiates a HeapCrossRef.
62 ///This function instantiates a \ref HeapCrossRef.
63 /// \param G is the graph, to which we would like to define the
65 static HeapCrossRef *createHeapCrossRef(const GR &G)
67 return new HeapCrossRef(G);
70 ///The heap type used by Dijkstra algorithm.
72 ///The heap type used by Dijkstra algorithm.
76 typedef BinHeap<typename Graph::Node, typename LM::Value,
77 HeapCrossRef, 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
172 template <typename GR,
176 template <typename GR=ListGraph,
177 typename LM=typename GR::template EdgeMap<int>,
178 typename TR=DijkstraDefaultTraits<GR,LM> >
183 * \brief \ref Exception for uninitialized parameters.
185 * This error represents problems in the initialization
186 * of the parameters of the algorithms.
188 class UninitializedParameter : public lemon::UninitializedParameter {
190 virtual const char* exceptionName() const {
191 return "lemon::Dijkstra::UninitializedParameter";
196 ///The type of the underlying graph.
197 typedef typename TR::Graph Graph;
199 typedef typename Graph::Node Node;
201 typedef typename Graph::NodeIt NodeIt;
203 typedef typename Graph::Edge Edge;
205 typedef typename Graph::OutEdgeIt OutEdgeIt;
207 ///The type of the length of the edges.
208 typedef typename TR::LengthMap::Value Value;
209 ///The type of the map that stores the edge lengths.
210 typedef typename TR::LengthMap LengthMap;
211 ///\brief The type of the map that stores the last
212 ///edges of the shortest paths.
213 typedef typename TR::PredMap PredMap;
214 ///The type of the map indicating if a node is processed.
215 typedef typename TR::ProcessedMap ProcessedMap;
216 ///The type of the map that stores the dists of the nodes.
217 typedef typename TR::DistMap DistMap;
218 ///The cross reference type used for the current heap.
219 typedef typename TR::HeapCrossRef HeapCrossRef;
220 ///The heap type used by the dijkstra algorithm.
221 typedef typename TR::Heap Heap;
223 /// Pointer to the underlying graph.
225 /// Pointer to the length map
226 const LengthMap *length;
227 ///Pointer to the map of predecessors edges.
229 ///Indicates if \ref _pred is locally allocated (\c true) or not.
231 ///Pointer to the map of distances.
233 ///Indicates if \ref _dist is locally allocated (\c true) or not.
235 ///Pointer to the map of processed status of the nodes.
236 ProcessedMap *_processed;
237 ///Indicates if \ref _processed is locally allocated (\c true) or not.
238 bool local_processed;
239 ///Pointer to the heap cross references.
240 HeapCrossRef *_heap_cross_ref;
241 ///Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not.
242 bool local_heap_cross_ref;
243 ///Pointer to the heap.
245 ///Indicates if \ref _heap is locally allocated (\c true) or not.
248 ///Creates the maps if necessary.
250 ///\todo Better memory allocation (instead of new).
255 _pred = Traits::createPredMap(*G);
259 _dist = Traits::createDistMap(*G);
262 local_processed = true;
263 _processed = Traits::createProcessedMap(*G);
265 if (!_heap_cross_ref) {
266 local_heap_cross_ref = true;
267 _heap_cross_ref = Traits::createHeapCrossRef(*G);
271 _heap = Traits::createHeap(*_heap_cross_ref);
277 typedef Dijkstra Create;
279 ///\name Named template parameters
284 struct DefPredMapTraits : public Traits {
286 static PredMap *createPredMap(const Graph &G)
288 throw UninitializedParameter();
291 ///\ref named-templ-param "Named parameter" for setting PredMap type
293 ///\ref named-templ-param "Named parameter" for setting PredMap type
297 : public Dijkstra< Graph, LengthMap, DefPredMapTraits<T> > {
298 typedef Dijkstra< Graph, LengthMap, DefPredMapTraits<T> > Create;
302 struct DefDistMapTraits : public Traits {
304 static DistMap *createDistMap(const Graph &G)
306 throw UninitializedParameter();
309 ///\ref named-templ-param "Named parameter" for setting DistMap type
311 ///\ref named-templ-param "Named parameter" for setting DistMap type
315 : public Dijkstra< Graph, LengthMap, DefDistMapTraits<T> > {
316 typedef Dijkstra< Graph, LengthMap, DefDistMapTraits<T> > Create;
320 struct DefProcessedMapTraits : public Traits {
321 typedef T ProcessedMap;
322 static ProcessedMap *createProcessedMap(const Graph &G)
324 throw UninitializedParameter();
327 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
329 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type
332 struct DefProcessedMap
333 : public Dijkstra< Graph, LengthMap, DefProcessedMapTraits<T> > {
334 typedef Dijkstra< Graph, LengthMap, DefProcessedMapTraits<T> > Create;
337 struct DefGraphProcessedMapTraits : public Traits {
338 typedef typename Graph::template NodeMap<bool> ProcessedMap;
339 static ProcessedMap *createProcessedMap(const Graph &G)
341 return new ProcessedMap(G);
344 ///\brief \ref named-templ-param "Named parameter"
345 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
347 ///\ref named-templ-param "Named parameter"
348 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>.
349 ///If you don't set it explicitely, it will be automatically allocated.
351 struct DefProcessedMapToBeDefaultMap
352 : public Dijkstra< Graph, LengthMap, DefGraphProcessedMapTraits> {
353 typedef Dijkstra< Graph, LengthMap, DefGraphProcessedMapTraits> Create;
356 template <class H, class CR>
357 struct DefHeapTraits : public Traits {
358 typedef CR HeapCrossRef;
360 static HeapCrossRef *createHeapCrossRef(const Graph &) {
361 throw UninitializedParameter();
363 static Heap *createHeap(HeapCrossRef &)
365 throw UninitializedParameter();
368 ///\ref named-templ-param "Named parameter" for setting heap and cross
371 ///\ref named-templ-param "Named parameter" for setting heap and cross
374 template <class H, class CR = typename Graph::template NodeMap<int> >
376 : public Dijkstra< Graph, LengthMap, DefHeapTraits<H, CR> > {
377 typedef Dijkstra< Graph, LengthMap, DefHeapTraits<H, CR> > Create;
380 template <class H, class CR>
381 struct DefStandardHeapTraits : public Traits {
382 typedef CR HeapCrossRef;
384 static HeapCrossRef *createHeapCrossRef(const Graph &G) {
385 return new HeapCrossRef(G);
387 static Heap *createHeap(HeapCrossRef &R)
392 ///\ref named-templ-param "Named parameter" for setting heap and cross
393 ///reference type with automatic allocation
395 ///\ref named-templ-param "Named parameter" for setting heap and cross
396 ///reference type. It can allocate the heap and the cross reference
397 ///object if the cross reference's constructor waits for the graph as
398 ///parameter and the heap's constructor waits for the cross reference.
399 template <class H, class CR = typename Graph::template NodeMap<int> >
400 struct DefStandardHeap
401 : public Dijkstra< Graph, LengthMap, DefStandardHeapTraits<H, CR> > {
402 typedef Dijkstra< Graph, LengthMap, DefStandardHeapTraits<H, CR> >
417 ///\param _G the graph the algorithm will run on.
418 ///\param _length the length map used by the algorithm.
419 Dijkstra(const Graph& _G, const LengthMap& _length) :
420 G(&_G), length(&_length),
421 _pred(NULL), local_pred(false),
422 _dist(NULL), local_dist(false),
423 _processed(NULL), local_processed(false),
424 _heap_cross_ref(NULL), local_heap_cross_ref(false),
425 _heap(NULL), local_heap(false)
431 if(local_pred) delete _pred;
432 if(local_dist) delete _dist;
433 if(local_processed) delete _processed;
434 if(local_heap_cross_ref) delete _heap_cross_ref;
435 if(local_heap) delete _heap;
438 ///Sets the length map.
440 ///Sets the length map.
441 ///\return <tt> (*this) </tt>
442 Dijkstra &lengthMap(const LengthMap &m)
448 ///Sets the map storing the predecessor edges.
450 ///Sets the map storing the predecessor edges.
451 ///If you don't use this function before calling \ref run(),
452 ///it will allocate one. The destuctor deallocates this
453 ///automatically allocated map, of course.
454 ///\return <tt> (*this) </tt>
455 Dijkstra &predMap(PredMap &m)
465 ///Sets the map storing the distances calculated by the algorithm.
467 ///Sets the map storing the distances calculated by the algorithm.
468 ///If you don't use this function before calling \ref run(),
469 ///it will allocate one. The destuctor deallocates this
470 ///automatically allocated map, of course.
471 ///\return <tt> (*this) </tt>
472 Dijkstra &distMap(DistMap &m)
482 ///Sets the heap and the cross reference used by algorithm.
484 ///Sets the heap and the cross reference used by algorithm.
485 ///If you don't use this function before calling \ref run(),
486 ///it will allocate one. The destuctor deallocates this
487 ///automatically allocated map, of course.
488 ///\return <tt> (*this) </tt>
489 Dijkstra &heap(Heap& heap, HeapCrossRef &crossRef)
491 if(local_heap_cross_ref) {
492 delete _heap_cross_ref;
493 local_heap_cross_ref=false;
495 _heap_cross_ref = &crossRef;
505 void finalizeNodeData(Node v,Value dst)
507 _processed->set(v,true);
512 ///\name Execution control
513 ///The simplest way to execute the algorithm is to use
514 ///one of the member functions called \c run(...).
516 ///If you need more control on the execution,
517 ///first you must call \ref init(), then you can add several source nodes
518 ///with \ref addSource().
519 ///Finally \ref start() will perform the actual path
524 ///Initializes the internal data structures.
526 ///Initializes the internal data structures.
532 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
533 _pred->set(u,INVALID);
534 _processed->set(u,false);
535 _heap_cross_ref->set(u,Heap::PRE_HEAP);
539 ///Adds a new source node.
541 ///Adds a new source node to the priority heap.
543 ///The optional second parameter is the initial distance of the node.
545 ///It checks if the node has already been added to the heap and
546 ///It is pushed to the heap only if either it was not in the heap
547 ///or the shortest path found till then is longer then \c dst.
548 void addSource(Node s,Value dst=dijkstraZero<Value>())
550 if(_heap->state(s) != Heap::IN_HEAP) {
552 } else if((*_heap)[s]<dst) {
554 _pred->set(s,INVALID);
558 ///Processes the next node in the priority heap
560 ///Processes the next node in the priority heap.
562 ///\return The processed node.
564 ///\warning The priority heap must not be empty!
565 Node processNextNode()
568 Value oldvalue=_heap->prio();
570 finalizeNodeData(v,oldvalue);
572 for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
574 switch(_heap->state(w)) {
576 _heap->push(w,oldvalue+(*length)[e]);
580 if ( oldvalue+(*length)[e] < (*_heap)[w] ) {
581 _heap->decrease(w, oldvalue+(*length)[e]);
585 case Heap::POST_HEAP:
592 ///Next node to be processed.
594 ///Next node to be processed.
596 ///\return The next node to be processed or INVALID if the priority heap
600 return _heap->empty()?_heap->top():INVALID;
603 ///\brief Returns \c false if there are nodes
604 ///to be processed in the priority heap
606 ///Returns \c false if there are nodes
607 ///to be processed in the priority heap
608 bool emptyQueue() { return _heap->empty(); }
609 ///Returns the number of the nodes to be processed in the priority heap
611 ///Returns the number of the nodes to be processed in the priority heap
613 int queueSize() { return _heap->size(); }
615 ///Executes the algorithm.
617 ///Executes the algorithm.
619 ///\pre init() must be called and at least one node should be added
620 ///with addSource() before using this function.
622 ///This method runs the %Dijkstra algorithm from the root node(s)
625 ///shortest path to each node. The algorithm computes
626 ///- The shortest path tree.
627 ///- The distance of each node from the root(s).
631 while ( !_heap->empty() ) processNextNode();
634 ///Executes the algorithm until \c dest is reached.
636 ///Executes the algorithm until \c dest is reached.
638 ///\pre init() must be called and at least one node should be added
639 ///with addSource() before using this function.
641 ///This method runs the %Dijkstra algorithm from the root node(s)
644 ///shortest path to \c dest. The algorithm computes
645 ///- The shortest path to \c dest.
646 ///- The distance of \c dest from the root(s).
648 void start(Node dest)
650 while ( !_heap->empty() && _heap->top()!=dest ) processNextNode();
651 if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio());
654 ///Executes the algorithm until a condition is met.
656 ///Executes the algorithm until a condition is met.
658 ///\pre init() must be called and at least one node should be added
659 ///with addSource() before using this function.
661 ///\param nm must be a bool (or convertible) node map. The algorithm
662 ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
663 template<class NodeBoolMap>
664 void start(const NodeBoolMap &nm)
666 while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode();
667 if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio());
670 ///Runs %Dijkstra algorithm from node \c s.
672 ///This method runs the %Dijkstra algorithm from a root node \c s
675 ///shortest path to each node. The algorithm computes
676 ///- The shortest path tree.
677 ///- The distance of each node from the root.
679 ///\note d.run(s) is just a shortcut of the following code.
691 ///Finds the shortest path between \c s and \c t.
693 ///Finds the shortest path between \c s and \c t.
695 ///\return The length of the shortest s---t path if there exists one,
697 ///\note Apart from the return value, d.run(s) is
698 ///just a shortcut of the following code.
704 Value run(Node s,Node t) {
708 return (*_pred)[t]==INVALID?dijkstraZero<Value>():(*_dist)[t];
713 ///\name Query Functions
714 ///The result of the %Dijkstra algorithm can be obtained using these
716 ///Before the use of these functions,
717 ///either run() or start() must be called.
721 ///Copies the shortest path to \c t into \c p
723 ///This function copies the shortest path to \c t into \c p.
724 ///If it \c t is a source itself or unreachable, then it does not
726 ///\return Returns \c true if a path to \c t was actually copied to \c p,
727 ///\c false otherwise.
730 bool getPath(P &p,Node t)
734 typename P::Builder b(p);
735 for(b.setStartNode(t);predEdge(t)!=INVALID;t=predNode(t))
736 b.pushFront(predEdge(t));
743 ///The distance of a node from the root.
745 ///Returns the distance of a node from the root.
746 ///\pre \ref run() must be called before using this function.
747 ///\warning If node \c v in unreachable from the root the return value
748 ///of this funcion is undefined.
749 Value dist(Node v) const { return (*_dist)[v]; }
751 ///Returns the 'previous edge' of the shortest path tree.
753 ///For a node \c v it returns the 'previous edge' of the shortest path tree,
754 ///i.e. it returns the last edge of a shortest path from the root to \c
755 ///v. It is \ref INVALID
756 ///if \c v is unreachable from the root or if \c v=s. The
757 ///shortest path tree used here is equal to the shortest path tree used in
758 ///\ref predNode(). \pre \ref run() must be called before using
760 Edge predEdge(Node v) const { return (*_pred)[v]; }
762 ///Returns the 'previous node' of the shortest path tree.
764 ///For a node \c v it returns the 'previous node' of the shortest path tree,
765 ///i.e. it returns the last but one node from a shortest path from the
766 ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
767 ///\c v=s. The shortest path tree used here is equal to the shortest path
768 ///tree used in \ref predEdge(). \pre \ref run() must be called before
769 ///using this function.
770 Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
771 G->source((*_pred)[v]); }
773 ///Returns a reference to the NodeMap of distances.
775 ///Returns a reference to the NodeMap of distances. \pre \ref run() must
776 ///be called before using this function.
777 const DistMap &distMap() const { return *_dist;}
779 ///Returns a reference to the shortest path tree map.
781 ///Returns a reference to the NodeMap of the edges of the
782 ///shortest path tree.
783 ///\pre \ref run() must be called before using this function.
784 const PredMap &predMap() const { return *_pred;}
786 ///Checks if a node is reachable from the root.
788 ///Returns \c true if \c v is reachable from the root.
789 ///\warning The source nodes are inditated as unreached.
790 ///\pre \ref run() must be called before using this function.
792 bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; }
794 ///Checks if a node is processed.
796 ///Returns \c true if \c v is processed, i.e. the shortest
797 ///path to \c v has already found.
798 ///\pre \ref run() must be called before using this function.
800 bool processed(Node v) { return (*_heap_cross_ref)[v] == Heap::POST_HEAP; }
809 ///Default traits class of Dijkstra function.
811 ///Default traits class of Dijkstra function.
812 ///\param GR Graph type.
813 ///\param LM Type of length map.
814 template<class GR, class LM>
815 struct DijkstraWizardDefaultTraits
817 ///The graph type the algorithm runs on.
819 ///The type of the map that stores the edge lengths.
821 ///The type of the map that stores the edge lengths.
822 ///It must meet the \ref concept::ReadMap "ReadMap" concept.
823 typedef LM LengthMap;
824 //The type of the length of the edges.
825 typedef typename LM::Value Value;
826 ///The heap type used by Dijkstra algorithm.
828 /// The cross reference type used by heap.
830 /// The cross reference type used by heap.
831 /// Usually it is \c Graph::NodeMap<int>.
832 typedef typename Graph::template NodeMap<int> HeapCrossRef;
833 ///Instantiates a HeapCrossRef.
835 ///This function instantiates a \ref HeapCrossRef.
836 /// \param G is the graph, to which we would like to define the
838 /// \todo The graph alone may be insufficient for the initialization
839 static HeapCrossRef *createHeapCrossRef(const GR &G)
841 return new HeapCrossRef(G);
844 ///The heap type used by Dijkstra algorithm.
846 ///The heap type used by Dijkstra algorithm.
850 typedef BinHeap<typename Graph::Node, typename LM::Value,
851 typename GR::template NodeMap<int>,
852 std::less<Value> > Heap;
854 static Heap *createHeap(HeapCrossRef& R)
859 ///\brief The type of the map that stores the last
860 ///edges of the shortest paths.
862 ///The type of the map that stores the last
863 ///edges of the shortest paths.
864 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
866 typedef NullMap <typename GR::Node,typename GR::Edge> PredMap;
867 ///Instantiates a PredMap.
869 ///This function instantiates a \ref PredMap.
870 ///\param g is the graph, to which we would like to define the PredMap.
871 ///\todo The graph alone may be insufficient for the initialization
873 static PredMap *createPredMap(const GR &g)
875 static PredMap *createPredMap(const GR &)
878 return new PredMap();
880 ///The type of the map that stores whether a nodes is processed.
882 ///The type of the map that stores whether a nodes is processed.
883 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
884 ///By default it is a NullMap.
885 ///\todo If it is set to a real map,
886 ///Dijkstra::processed() should read this.
887 ///\todo named parameter to set this type, function to read and write.
888 typedef NullMap<typename Graph::Node,bool> ProcessedMap;
889 ///Instantiates a ProcessedMap.
891 ///This function instantiates a \ref ProcessedMap.
892 ///\param g is the graph, to which
893 ///we would like to define the \ref ProcessedMap
895 static ProcessedMap *createProcessedMap(const GR &g)
897 static ProcessedMap *createProcessedMap(const GR &)
900 return new ProcessedMap();
902 ///The type of the map that stores the dists of the nodes.
904 ///The type of the map that stores the dists of the nodes.
905 ///It must meet the \ref concept::WriteMap "WriteMap" concept.
907 typedef NullMap<typename Graph::Node,typename LM::Value> DistMap;
908 ///Instantiates a DistMap.
910 ///This function instantiates a \ref DistMap.
911 ///\param g is the graph, to which we would like to define the \ref DistMap
913 static DistMap *createDistMap(const GR &g)
915 static DistMap *createDistMap(const GR &)
918 return new DistMap();
922 /// Default traits used by \ref DijkstraWizard
924 /// To make it easier to use Dijkstra algorithm
925 ///we have created a wizard class.
926 /// This \ref DijkstraWizard class needs default traits,
927 ///as well as the \ref Dijkstra class.
928 /// The \ref DijkstraWizardBase is a class to be the default traits of the
929 /// \ref DijkstraWizard class.
930 /// \todo More named parameters are required...
931 template<class GR,class LM>
932 class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM>
935 typedef DijkstraWizardDefaultTraits<GR,LM> Base;
937 /// Type of the nodes in the graph.
938 typedef typename Base::Graph::Node Node;
940 /// Pointer to the underlying graph.
942 /// Pointer to the length map
944 ///Pointer to the map of predecessors edges.
946 ///Pointer to the map of distances.
948 ///Pointer to the source node.
954 /// This constructor does not require parameters, therefore it initiates
955 /// all of the attributes to default values (0, INVALID).
956 DijkstraWizardBase() : _g(0), _length(0), _pred(0),
957 _dist(0), _source(INVALID) {}
961 /// This constructor requires some parameters,
962 /// listed in the parameters list.
963 /// Others are initiated to 0.
964 /// \param g is the initial value of \ref _g
965 /// \param l is the initial value of \ref _length
966 /// \param s is the initial value of \ref _source
967 DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
968 _g((void *)&g), _length((void *)&l), _pred(0),
969 _dist(0), _source(s) {}
973 /// A class to make the usage of Dijkstra algorithm easier
975 /// This class is created to make it easier to use Dijkstra algorithm.
976 /// It uses the functions and features of the plain \ref Dijkstra,
977 /// but it is much simpler to use it.
979 /// Simplicity means that the way to change the types defined
980 /// in the traits class is based on functions that returns the new class
981 /// and not on templatable built-in classes.
982 /// When using the plain \ref Dijkstra
983 /// the new class with the modified type comes from
984 /// the original class by using the ::
985 /// operator. In the case of \ref DijkstraWizard only
986 /// a function have to be called and it will
987 /// return the needed class.
989 /// It does not have own \ref run method. When its \ref run method is called
990 /// it initiates a plain \ref Dijkstra class, and calls the \ref
991 /// Dijkstra::run method of it.
993 class DijkstraWizard : public TR
997 ///The type of the underlying graph.
998 typedef typename TR::Graph Graph;
1000 typedef typename Graph::Node Node;
1002 typedef typename Graph::NodeIt NodeIt;
1004 typedef typename Graph::Edge Edge;
1006 typedef typename Graph::OutEdgeIt OutEdgeIt;
1008 ///The type of the map that stores the edge lengths.
1009 typedef typename TR::LengthMap LengthMap;
1010 ///The type of the length of the edges.
1011 typedef typename LengthMap::Value Value;
1012 ///\brief The type of the map that stores the last
1013 ///edges of the shortest paths.
1014 typedef typename TR::PredMap PredMap;
1015 ///The type of the map that stores the dists of the nodes.
1016 typedef typename TR::DistMap DistMap;
1017 ///The heap type used by the dijkstra algorithm.
1018 typedef typename TR::Heap Heap;
1021 DijkstraWizard() : TR() {}
1023 /// Constructor that requires parameters.
1025 /// Constructor that requires parameters.
1026 /// These parameters will be the default values for the traits class.
1027 DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
1031 DijkstraWizard(const TR &b) : TR(b) {}
1033 ~DijkstraWizard() {}
1035 ///Runs Dijkstra algorithm from a given node.
1037 ///Runs Dijkstra algorithm from a given node.
1038 ///The node can be given by the \ref source function.
1041 if(Base::_source==INVALID) throw UninitializedParameter();
1042 Dijkstra<Graph,LengthMap,TR>
1043 dij(*(Graph*)Base::_g,*(LengthMap*)Base::_length);
1044 if(Base::_pred) dij.predMap(*(PredMap*)Base::_pred);
1045 if(Base::_dist) dij.distMap(*(DistMap*)Base::_dist);
1046 dij.run(Base::_source);
1049 ///Runs Dijkstra algorithm from the given node.
1051 ///Runs Dijkstra algorithm from the given node.
1052 ///\param s is the given source.
1060 struct DefPredMapBase : public Base {
1062 static PredMap *createPredMap(const Graph &) { return 0; };
1063 DefPredMapBase(const TR &b) : TR(b) {}
1066 ///\brief \ref named-templ-param "Named parameter"
1067 ///function for setting PredMap type
1069 /// \ref named-templ-param "Named parameter"
1070 ///function for setting PredMap type
1073 DijkstraWizard<DefPredMapBase<T> > predMap(const T &t)
1075 Base::_pred=(void *)&t;
1076 return DijkstraWizard<DefPredMapBase<T> >(*this);
1080 struct DefDistMapBase : public Base {
1082 static DistMap *createDistMap(const Graph &) { return 0; };
1083 DefDistMapBase(const TR &b) : TR(b) {}
1086 ///\brief \ref named-templ-param "Named parameter"
1087 ///function for setting DistMap type
1089 /// \ref named-templ-param "Named parameter"
1090 ///function for setting DistMap type
1093 DijkstraWizard<DefDistMapBase<T> > distMap(const T &t)
1095 Base::_dist=(void *)&t;
1096 return DijkstraWizard<DefDistMapBase<T> >(*this);
1099 /// Sets the source node, from which the Dijkstra algorithm runs.
1101 /// Sets the source node, from which the Dijkstra algorithm runs.
1102 /// \param s is the source node.
1103 DijkstraWizard<TR> &source(Node s)
1111 ///Function type interface for Dijkstra algorithm.
1113 /// \ingroup flowalgs
1114 ///Function type interface for Dijkstra algorithm.
1116 ///This function also has several
1117 ///\ref named-templ-func-param "named parameters",
1118 ///they are declared as the members of class \ref DijkstraWizard.
1120 ///example shows how to use these parameters.
1122 /// dijkstra(g,length,source).predMap(preds).run();
1124 ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()"
1125 ///to the end of the parameter list.
1126 ///\sa DijkstraWizard
1128 template<class GR, class LM>
1129 DijkstraWizard<DijkstraWizardBase<GR,LM> >
1130 dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
1132 return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
1135 } //END OF NAMESPACE LEMON