31 #include <lemon/error.h> |
31 #include <lemon/error.h> |
32 #include <lemon/maps.h> |
32 #include <lemon/maps.h> |
33 |
33 |
34 namespace lemon { |
34 namespace lemon { |
35 |
35 |
36 /// \brief Default OperationTraits for the Dijkstra algorithm class. |
36 /// \brief Default operation traits for the Dijkstra algorithm class. |
37 /// |
37 /// |
38 /// It defines all computational operations and constants which are |
38 /// This operation traits class defines all computational operations and |
39 /// used in the Dijkstra algorithm. |
39 /// constants which are used in the Dijkstra algorithm. |
40 template <typename Value> |
40 template <typename Value> |
41 struct DijkstraDefaultOperationTraits { |
41 struct DijkstraDefaultOperationTraits { |
42 /// \brief Gives back the zero value of the type. |
42 /// \brief Gives back the zero value of the type. |
43 static Value zero() { |
43 static Value zero() { |
44 return static_cast<Value>(0); |
44 return static_cast<Value>(0); |
45 } |
45 } |
46 /// \brief Gives back the sum of the given two elements. |
46 /// \brief Gives back the sum of the given two elements. |
47 static Value plus(const Value& left, const Value& right) { |
47 static Value plus(const Value& left, const Value& right) { |
48 return left + right; |
48 return left + right; |
49 } |
49 } |
50 /// \brief Gives back true only if the first value less than the second. |
50 /// \brief Gives back true only if the first value is less than the second. |
51 static bool less(const Value& left, const Value& right) { |
51 static bool less(const Value& left, const Value& right) { |
52 return left < right; |
52 return left < right; |
53 } |
53 } |
54 }; |
54 }; |
55 |
55 |
56 /// \brief Widest path OperationTraits for the Dijkstra algorithm class. |
56 /// \brief Widest path operation traits for the Dijkstra algorithm class. |
57 /// |
57 /// |
58 /// It defines all computational operations and constants which are |
58 /// This operation traits class defines all computational operations and |
59 /// used in the Dijkstra algorithm for widest path computation. |
59 /// constants which are used in the Dijkstra algorithm for widest path |
|
60 /// computation. |
|
61 /// |
|
62 /// \see DijkstraDefaultOperationTraits |
60 template <typename Value> |
63 template <typename Value> |
61 struct DijkstraWidestPathOperationTraits { |
64 struct DijkstraWidestPathOperationTraits { |
62 /// \brief Gives back the maximum value of the type. |
65 /// \brief Gives back the maximum value of the type. |
63 static Value zero() { |
66 static Value zero() { |
64 return std::numeric_limits<Value>::max(); |
67 return std::numeric_limits<Value>::max(); |
65 } |
68 } |
66 /// \brief Gives back the minimum of the given two elements. |
69 /// \brief Gives back the minimum of the given two elements. |
67 static Value plus(const Value& left, const Value& right) { |
70 static Value plus(const Value& left, const Value& right) { |
68 return std::min(left, right); |
71 return std::min(left, right); |
69 } |
72 } |
70 /// \brief Gives back true only if the first value less than the second. |
73 /// \brief Gives back true only if the first value is less than the second. |
71 static bool less(const Value& left, const Value& right) { |
74 static bool less(const Value& left, const Value& right) { |
72 return left < right; |
75 return left < right; |
73 } |
76 } |
74 }; |
77 }; |
75 |
78 |
76 ///Default traits class of Dijkstra class. |
79 ///Default traits class of Dijkstra class. |
77 |
80 |
78 ///Default traits class of Dijkstra class. |
81 ///Default traits class of Dijkstra class. |
79 ///\tparam GR Digraph type. |
82 ///\tparam GR The type of the digraph. |
80 ///\tparam LM Type of length map. |
83 ///\tparam LM The type of the length map. |
81 template<class GR, class LM> |
84 template<class GR, class LM> |
82 struct DijkstraDefaultTraits |
85 struct DijkstraDefaultTraits |
83 { |
86 { |
84 ///The digraph type the algorithm runs on. |
87 ///The type of the digraph the algorithm runs on. |
85 typedef GR Digraph; |
88 typedef GR Digraph; |
|
89 |
86 ///The type of the map that stores the arc lengths. |
90 ///The type of the map that stores the arc lengths. |
87 |
91 |
88 ///The type of the map that stores the arc lengths. |
92 ///The type of the map that stores the arc lengths. |
89 ///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
93 ///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
90 typedef LM LengthMap; |
94 typedef LM LengthMap; |
91 //The type of the length of the arcs. |
95 ///The type of the length of the arcs. |
92 typedef typename LM::Value Value; |
96 typedef typename LM::Value Value; |
|
97 |
93 /// Operation traits for Dijkstra algorithm. |
98 /// Operation traits for Dijkstra algorithm. |
94 |
99 |
95 /// It defines the used operation by the algorithm. |
100 /// This class defines the operations that are used in the algorithm. |
96 /// \see DijkstraDefaultOperationTraits |
101 /// \see DijkstraDefaultOperationTraits |
97 typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
102 typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
98 /// The cross reference type used by heap. |
103 |
99 |
104 /// The cross reference type used by the heap. |
100 |
105 |
101 /// The cross reference type used by heap. |
106 /// The cross reference type used by the heap. |
102 /// Usually it is \c Digraph::NodeMap<int>. |
107 /// Usually it is \c Digraph::NodeMap<int>. |
103 typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
108 typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
104 ///Instantiates a HeapCrossRef. |
109 ///Instantiates a \ref HeapCrossRef. |
105 |
110 |
106 ///This function instantiates a \c HeapCrossRef. |
111 ///This function instantiates a \ref HeapCrossRef. |
107 /// \param G is the digraph, to which we would like to define the |
112 /// \param g is the digraph, to which we would like to define the |
108 /// HeapCrossRef. |
113 /// \ref HeapCrossRef. |
109 static HeapCrossRef *createHeapCrossRef(const GR &G) |
114 static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
110 { |
115 { |
111 return new HeapCrossRef(G); |
116 return new HeapCrossRef(g); |
112 } |
117 } |
113 |
118 |
114 ///The heap type used by Dijkstra algorithm. |
119 ///The heap type used by the Dijkstra algorithm. |
115 |
120 |
116 ///The heap type used by Dijkstra algorithm. |
121 ///The heap type used by the Dijkstra algorithm. |
117 /// |
122 /// |
118 ///\sa BinHeap |
123 ///\sa BinHeap |
119 ///\sa Dijkstra |
124 ///\sa Dijkstra |
120 typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap; |
125 typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap; |
121 |
126 ///Instantiates a \ref Heap. |
122 static Heap *createHeap(HeapCrossRef& R) |
127 |
123 { |
128 ///This function instantiates a \ref Heap. |
124 return new Heap(R); |
129 static Heap *createHeap(HeapCrossRef& r) |
125 } |
130 { |
126 |
131 return new Heap(r); |
127 ///\brief The type of the map that stores the last |
132 } |
|
133 |
|
134 ///\brief The type of the map that stores the predecessor |
128 ///arcs of the shortest paths. |
135 ///arcs of the shortest paths. |
129 /// |
136 /// |
130 ///The type of the map that stores the last |
137 ///The type of the map that stores the predecessor |
131 ///arcs of the shortest paths. |
138 ///arcs of the shortest paths. |
132 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
139 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
133 /// |
140 typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
134 typedef typename Digraph::template NodeMap<typename GR::Arc> PredMap; |
141 ///Instantiates a \ref PredMap. |
135 ///Instantiates a PredMap. |
142 |
136 |
143 ///This function instantiates a \ref PredMap. |
137 ///This function instantiates a \c PredMap. |
144 ///\param g is the digraph, to which we would like to define the |
138 ///\param G is the digraph, to which we would like to define the PredMap. |
145 ///\ref PredMap. |
139 ///\todo The digraph alone may be insufficient for the initialization |
146 ///\todo The digraph alone may be insufficient for the initialization |
140 static PredMap *createPredMap(const GR &G) |
147 static PredMap *createPredMap(const Digraph &g) |
141 { |
148 { |
142 return new PredMap(G); |
149 return new PredMap(g); |
143 } |
150 } |
144 |
151 |
145 ///The type of the map that stores whether a nodes is processed. |
152 ///The type of the map that indicates which nodes are processed. |
146 |
153 |
147 ///The type of the map that stores whether a nodes is processed. |
154 ///The type of the map that indicates which nodes are processed. |
148 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
155 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
149 ///By default it is a NullMap. |
156 ///By default it is a NullMap. |
150 ///\todo If it is set to a real map, |
157 ///\todo If it is set to a real map, |
151 ///Dijkstra::processed() should read this. |
158 ///Dijkstra::processed() should read this. |
152 ///\todo named parameter to set this type, function to read and write. |
|
153 typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
159 typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
154 ///Instantiates a ProcessedMap. |
160 ///Instantiates a \ref ProcessedMap. |
155 |
161 |
156 ///This function instantiates a \c ProcessedMap. |
162 ///This function instantiates a \ref ProcessedMap. |
157 ///\param g is the digraph, to which |
163 ///\param g is the digraph, to which |
158 ///we would like to define the \c ProcessedMap |
164 ///we would like to define the \ref ProcessedMap |
159 #ifdef DOXYGEN |
165 #ifdef DOXYGEN |
160 static ProcessedMap *createProcessedMap(const GR &g) |
166 static ProcessedMap *createProcessedMap(const Digraph &g) |
161 #else |
167 #else |
162 static ProcessedMap *createProcessedMap(const GR &) |
168 static ProcessedMap *createProcessedMap(const Digraph &) |
163 #endif |
169 #endif |
164 { |
170 { |
165 return new ProcessedMap(); |
171 return new ProcessedMap(); |
166 } |
172 } |
167 ///The type of the map that stores the dists of the nodes. |
173 |
168 |
174 ///The type of the map that stores the distances of the nodes. |
169 ///The type of the map that stores the dists of the nodes. |
175 |
|
176 ///The type of the map that stores the distances of the nodes. |
170 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
177 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
171 /// |
|
172 typedef typename Digraph::template NodeMap<typename LM::Value> DistMap; |
178 typedef typename Digraph::template NodeMap<typename LM::Value> DistMap; |
173 ///Instantiates a DistMap. |
179 ///Instantiates a \ref DistMap. |
174 |
180 |
175 ///This function instantiates a \ref DistMap. |
181 ///This function instantiates a \ref DistMap. |
176 ///\param G is the digraph, to which we would like to define |
182 ///\param g is the digraph, to which we would like to define |
177 ///the \ref DistMap |
183 ///the \ref DistMap |
178 static DistMap *createDistMap(const GR &G) |
184 static DistMap *createDistMap(const Digraph &g) |
179 { |
185 { |
180 return new DistMap(G); |
186 return new DistMap(g); |
181 } |
187 } |
182 }; |
188 }; |
183 |
189 |
184 ///%Dijkstra algorithm class. |
190 ///%Dijkstra algorithm class. |
185 |
191 |
186 /// \ingroup shortest_path |
192 /// \ingroup shortest_path |
187 ///This class provides an efficient implementation of %Dijkstra algorithm. |
193 ///This class provides an efficient implementation of the %Dijkstra algorithm. |
|
194 /// |
188 ///The arc lengths are passed to the algorithm using a |
195 ///The arc lengths are passed to the algorithm using a |
189 ///\ref concepts::ReadMap "ReadMap", |
196 ///\ref concepts::ReadMap "ReadMap", |
190 ///so it is easy to change it to any kind of length. |
197 ///so it is easy to change it to any kind of length. |
191 /// |
|
192 ///The type of the length is determined by the |
198 ///The type of the length is determined by the |
193 ///\ref concepts::ReadMap::Value "Value" of the length map. |
199 ///\ref concepts::ReadMap::Value "Value" of the length map. |
194 /// |
|
195 ///It is also possible to change the underlying priority heap. |
200 ///It is also possible to change the underlying priority heap. |
196 /// |
201 /// |
197 ///\tparam GR The digraph type the algorithm runs on. The default value |
202 ///There is also a \ref dijkstra() "function type interface" for the |
198 ///is \ref ListDigraph. The value of GR is not used directly by |
203 ///%Dijkstra algorithm, which is convenient in the simplier cases and |
199 ///Dijkstra, it is only passed to \ref DijkstraDefaultTraits. |
204 ///it can be used easier. |
200 ///\tparam LM This read-only ArcMap determines the lengths of the |
205 /// |
|
206 ///\tparam GR The type of the digraph the algorithm runs on. |
|
207 ///The default value is \ref ListDigraph. |
|
208 ///The value of GR is not used directly by \ref Dijkstra, it is only |
|
209 ///passed to \ref DijkstraDefaultTraits. |
|
210 ///\tparam LM A readable arc map that determines the lengths of the |
201 ///arcs. It is read once for each arc, so the map may involve in |
211 ///arcs. It is read once for each arc, so the map may involve in |
202 ///relatively time consuming process to compute the arc length if |
212 ///relatively time consuming process to compute the arc lengths if |
203 ///it is necessary. The default map type is \ref |
213 ///it is necessary. The default map type is \ref |
204 ///concepts::Digraph::ArcMap "Digraph::ArcMap<int>". The value |
214 ///concepts::Digraph::ArcMap "Digraph::ArcMap<int>". |
205 ///of LM is not used directly by Dijkstra, it is only passed to \ref |
215 ///The value of LM is not used directly by \ref Dijkstra, it is only |
206 ///DijkstraDefaultTraits. |
216 ///passed to \ref DijkstraDefaultTraits. |
207 ///\tparam TR Traits class to set |
217 ///\tparam TR Traits class to set various data types used by the algorithm. |
208 ///various data types used by the algorithm. The default traits |
218 ///The default traits class is \ref DijkstraDefaultTraits |
209 ///class is \ref DijkstraDefaultTraits |
219 ///"DijkstraDefaultTraits<GR,LM>". See \ref DijkstraDefaultTraits |
210 ///"DijkstraDefaultTraits<GR,LM>". See \ref |
220 ///for the documentation of a Dijkstra traits class. |
211 ///DijkstraDefaultTraits for the documentation of a Dijkstra traits |
|
212 ///class. |
|
213 |
|
214 #ifdef DOXYGEN |
221 #ifdef DOXYGEN |
215 template <typename GR, typename LM, typename TR> |
222 template <typename GR, typename LM, typename TR> |
216 #else |
223 #else |
217 template <typename GR=ListDigraph, |
224 template <typename GR=ListDigraph, |
218 typename LM=typename GR::template ArcMap<int>, |
225 typename LM=typename GR::template ArcMap<int>, |
219 typename TR=DijkstraDefaultTraits<GR,LM> > |
226 typename TR=DijkstraDefaultTraits<GR,LM> > |
220 #endif |
227 #endif |
221 class Dijkstra { |
228 class Dijkstra { |
222 public: |
229 public: |
223 /** |
230 ///\ref Exception for uninitialized parameters. |
224 * \brief \ref Exception for uninitialized parameters. |
231 |
225 * |
232 ///This error represents problems in the initialization of the |
226 * This error represents problems in the initialization |
233 ///parameters of the algorithm. |
227 * of the parameters of the algorithms. |
|
228 */ |
|
229 class UninitializedParameter : public lemon::UninitializedParameter { |
234 class UninitializedParameter : public lemon::UninitializedParameter { |
230 public: |
235 public: |
231 virtual const char* what() const throw() { |
236 virtual const char* what() const throw() { |
232 return "lemon::Dijkstra::UninitializedParameter"; |
237 return "lemon::Dijkstra::UninitializedParameter"; |
233 } |
238 } |
234 }; |
239 }; |
235 |
240 |
236 typedef TR Traits; |
241 ///The type of the digraph the algorithm runs on. |
237 ///The type of the underlying digraph. |
|
238 typedef typename TR::Digraph Digraph; |
242 typedef typename TR::Digraph Digraph; |
239 ///\e |
|
240 typedef typename Digraph::Node Node; |
|
241 ///\e |
|
242 typedef typename Digraph::NodeIt NodeIt; |
|
243 ///\e |
|
244 typedef typename Digraph::Arc Arc; |
|
245 ///\e |
|
246 typedef typename Digraph::OutArcIt OutArcIt; |
|
247 |
243 |
248 ///The type of the length of the arcs. |
244 ///The type of the length of the arcs. |
249 typedef typename TR::LengthMap::Value Value; |
245 typedef typename TR::LengthMap::Value Value; |
250 ///The type of the map that stores the arc lengths. |
246 ///The type of the map that stores the arc lengths. |
251 typedef typename TR::LengthMap LengthMap; |
247 typedef typename TR::LengthMap LengthMap; |
252 ///\brief The type of the map that stores the last |
248 ///\brief The type of the map that stores the predecessor arcs of the |
253 ///arcs of the shortest paths. |
249 ///shortest paths. |
254 typedef typename TR::PredMap PredMap; |
250 typedef typename TR::PredMap PredMap; |
255 ///The type of the map indicating if a node is processed. |
251 ///The type of the map that stores the distances of the nodes. |
|
252 typedef typename TR::DistMap DistMap; |
|
253 ///The type of the map that indicates which nodes are processed. |
256 typedef typename TR::ProcessedMap ProcessedMap; |
254 typedef typename TR::ProcessedMap ProcessedMap; |
257 ///The type of the map that stores the dists of the nodes. |
255 ///The type of the paths. |
258 typedef typename TR::DistMap DistMap; |
256 typedef PredMapPath<Digraph, PredMap> Path; |
259 ///The cross reference type used for the current heap. |
257 ///The cross reference type used for the current heap. |
260 typedef typename TR::HeapCrossRef HeapCrossRef; |
258 typedef typename TR::HeapCrossRef HeapCrossRef; |
261 ///The heap type used by the dijkstra algorithm. |
259 ///The heap type used by the algorithm. |
262 typedef typename TR::Heap Heap; |
260 typedef typename TR::Heap Heap; |
263 ///The operation traits. |
261 ///The operation traits class. |
264 typedef typename TR::OperationTraits OperationTraits; |
262 typedef typename TR::OperationTraits OperationTraits; |
|
263 |
|
264 ///The traits class. |
|
265 typedef TR Traits; |
|
266 |
265 private: |
267 private: |
266 /// Pointer to the underlying digraph. |
268 |
|
269 typedef typename Digraph::Node Node; |
|
270 typedef typename Digraph::NodeIt NodeIt; |
|
271 typedef typename Digraph::Arc Arc; |
|
272 typedef typename Digraph::OutArcIt OutArcIt; |
|
273 |
|
274 //Pointer to the underlying digraph. |
267 const Digraph *G; |
275 const Digraph *G; |
268 /// Pointer to the length map |
276 //Pointer to the length map. |
269 const LengthMap *length; |
277 const LengthMap *length; |
270 ///Pointer to the map of predecessors arcs. |
278 //Pointer to the map of predecessors arcs. |
271 PredMap *_pred; |
279 PredMap *_pred; |
272 ///Indicates if \ref _pred is locally allocated (\c true) or not. |
280 //Indicates if _pred is locally allocated (true) or not. |
273 bool local_pred; |
281 bool local_pred; |
274 ///Pointer to the map of distances. |
282 //Pointer to the map of distances. |
275 DistMap *_dist; |
283 DistMap *_dist; |
276 ///Indicates if \ref _dist is locally allocated (\c true) or not. |
284 //Indicates if _dist is locally allocated (true) or not. |
277 bool local_dist; |
285 bool local_dist; |
278 ///Pointer to the map of processed status of the nodes. |
286 //Pointer to the map of processed status of the nodes. |
279 ProcessedMap *_processed; |
287 ProcessedMap *_processed; |
280 ///Indicates if \ref _processed is locally allocated (\c true) or not. |
288 //Indicates if _processed is locally allocated (true) or not. |
281 bool local_processed; |
289 bool local_processed; |
282 ///Pointer to the heap cross references. |
290 //Pointer to the heap cross references. |
283 HeapCrossRef *_heap_cross_ref; |
291 HeapCrossRef *_heap_cross_ref; |
284 ///Indicates if \ref _heap_cross_ref is locally allocated (\c true) or not. |
292 //Indicates if _heap_cross_ref is locally allocated (true) or not. |
285 bool local_heap_cross_ref; |
293 bool local_heap_cross_ref; |
286 ///Pointer to the heap. |
294 //Pointer to the heap. |
287 Heap *_heap; |
295 Heap *_heap; |
288 ///Indicates if \ref _heap is locally allocated (\c true) or not. |
296 //Indicates if _heap is locally allocated (true) or not. |
289 bool local_heap; |
297 bool local_heap; |
290 |
298 |
291 ///Creates the maps if necessary. |
299 ///Creates the maps if necessary. |
292 |
|
293 ///\todo Better memory allocation (instead of new). |
300 ///\todo Better memory allocation (instead of new). |
294 void create_maps() |
301 void create_maps() |
295 { |
302 { |
296 if(!_pred) { |
303 if(!_pred) { |
297 local_pred = true; |
304 local_pred = true; |
654 } |
679 } |
655 } |
680 } |
656 return v; |
681 return v; |
657 } |
682 } |
658 |
683 |
659 ///Next node to be processed. |
684 ///The next node to be processed. |
660 |
685 |
661 ///Next node to be processed. |
686 ///Returns the next node to be processed or \c INVALID if the |
662 /// |
687 ///priority heap is empty. |
663 ///\return The next node to be processed or INVALID if the priority heap |
688 Node nextNode() const |
664 /// is empty. |
|
665 Node nextNode() |
|
666 { |
689 { |
667 return !_heap->empty()?_heap->top():INVALID; |
690 return !_heap->empty()?_heap->top():INVALID; |
668 } |
691 } |
669 |
692 |
670 ///\brief Returns \c false if there are nodes |
693 ///\brief Returns \c false if there are nodes |
671 ///to be processed in the priority heap |
694 ///to be processed. |
672 /// |
695 /// |
673 ///Returns \c false if there are nodes |
696 ///Returns \c false if there are nodes |
674 ///to be processed in the priority heap |
697 ///to be processed in the priority heap. |
675 bool emptyQueue() { return _heap->empty(); } |
698 bool emptyQueue() const { return _heap->empty(); } |
|
699 |
676 ///Returns the number of the nodes to be processed in the priority heap |
700 ///Returns the number of the nodes to be processed in the priority heap |
677 |
701 |
678 ///Returns the number of the nodes to be processed in the priority heap |
702 ///Returns the number of the nodes to be processed in the priority heap. |
679 /// |
703 /// |
680 int queueSize() { return _heap->size(); } |
704 int queueSize() const { return _heap->size(); } |
681 |
705 |
682 ///Executes the algorithm. |
706 ///Executes the algorithm. |
683 |
707 |
684 ///Executes the algorithm. |
708 ///Executes the algorithm. |
685 /// |
709 /// |
686 ///\pre init() must be called and at least one node should be added |
|
687 ///with addSource() before using this function. |
|
688 /// |
|
689 ///This method runs the %Dijkstra algorithm from the root node(s) |
710 ///This method runs the %Dijkstra algorithm from the root node(s) |
690 ///in order to |
711 ///in order to compute the shortest path to each node. |
691 ///compute the |
712 /// |
692 ///shortest path to each node. The algorithm computes |
713 ///The algorithm computes |
693 ///- The shortest path tree. |
714 ///- the shortest path tree (forest), |
694 ///- The distance of each node from the root(s). |
715 ///- the distance of each node from the root(s). |
695 /// |
716 /// |
|
717 ///\pre init() must be called and at least one root node should be |
|
718 ///added with addSource() before using this function. |
|
719 /// |
|
720 ///\note <tt>d.start()</tt> is just a shortcut of the following code. |
|
721 ///\code |
|
722 /// while ( !d.emptyQueue() ) { |
|
723 /// d.processNextNode(); |
|
724 /// } |
|
725 ///\endcode |
696 void start() |
726 void start() |
697 { |
727 { |
698 while ( !_heap->empty() ) processNextNode(); |
728 while ( !emptyQueue() ) processNextNode(); |
699 } |
729 } |
700 |
730 |
701 ///Executes the algorithm until \c dest is reached. |
731 ///Executes the algorithm until the given target node is reached. |
702 |
732 |
703 ///Executes the algorithm until \c dest is reached. |
733 ///Executes the algorithm until the given target node is reached. |
704 /// |
|
705 ///\pre init() must be called and at least one node should be added |
|
706 ///with addSource() before using this function. |
|
707 /// |
734 /// |
708 ///This method runs the %Dijkstra algorithm from the root node(s) |
735 ///This method runs the %Dijkstra algorithm from the root node(s) |
709 ///in order to |
736 ///in order to compute the shortest path to \c dest. |
710 ///compute the |
737 /// |
711 ///shortest path to \c dest. The algorithm computes |
738 ///The algorithm computes |
712 ///- The shortest path to \c dest. |
739 ///- the shortest path to \c dest, |
713 ///- The distance of \c dest from the root(s). |
740 ///- the distance of \c dest from the root(s). |
714 /// |
741 /// |
|
742 ///\pre init() must be called and at least one root node should be |
|
743 ///added with addSource() before using this function. |
715 void start(Node dest) |
744 void start(Node dest) |
716 { |
745 { |
717 while ( !_heap->empty() && _heap->top()!=dest ) processNextNode(); |
746 while ( !_heap->empty() && _heap->top()!=dest ) processNextNode(); |
718 if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio()); |
747 if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio()); |
719 } |
748 } |
720 |
749 |
721 ///Executes the algorithm until a condition is met. |
750 ///Executes the algorithm until a condition is met. |
722 |
751 |
723 ///Executes the algorithm until a condition is met. |
752 ///Executes the algorithm until a condition is met. |
724 /// |
753 /// |
725 ///\pre init() must be called and at least one node should be added |
754 ///This method runs the %Dijkstra algorithm from the root node(s) in |
726 ///with addSource() before using this function. |
755 ///order to compute the shortest path to a node \c v with |
727 /// |
756 /// <tt>nm[v]</tt> true, if such a node can be found. |
728 ///\param nm must be a bool (or convertible) node map. The algorithm |
757 /// |
|
758 ///\param nm A \c bool (or convertible) node map. The algorithm |
729 ///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. |
759 ///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. |
730 /// |
760 /// |
731 ///\return The reached node \c v with <tt>nm[v]</tt> true or |
761 ///\return The reached node \c v with <tt>nm[v]</tt> true or |
732 ///\c INVALID if no such node was found. |
762 ///\c INVALID if no such node was found. |
|
763 /// |
|
764 ///\pre init() must be called and at least one root node should be |
|
765 ///added with addSource() before using this function. |
733 template<class NodeBoolMap> |
766 template<class NodeBoolMap> |
734 Node start(const NodeBoolMap &nm) |
767 Node start(const NodeBoolMap &nm) |
735 { |
768 { |
736 while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode(); |
769 while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode(); |
737 if ( _heap->empty() ) return INVALID; |
770 if ( _heap->empty() ) return INVALID; |
738 finalizeNodeData(_heap->top(),_heap->prio()); |
771 finalizeNodeData(_heap->top(),_heap->prio()); |
739 return _heap->top(); |
772 return _heap->top(); |
740 } |
773 } |
741 |
774 |
742 ///Runs %Dijkstra algorithm from node \c s. |
775 ///Runs the algorithm from the given node. |
743 |
776 |
744 ///This method runs the %Dijkstra algorithm from a root node \c s |
777 ///This method runs the %Dijkstra algorithm from node \c s |
745 ///in order to |
778 ///in order to compute the shortest path to each node. |
746 ///compute the |
779 /// |
747 ///shortest path to each node. The algorithm computes |
780 ///The algorithm computes |
748 ///- The shortest path tree. |
781 ///- the shortest path tree, |
749 ///- The distance of each node from the root. |
782 ///- the distance of each node from the root. |
750 /// |
783 /// |
751 ///\note d.run(s) is just a shortcut of the following code. |
784 ///\note <tt>d.run(s)</tt> is just a shortcut of the following code. |
752 ///\code |
785 ///\code |
753 /// d.init(); |
786 /// d.init(); |
754 /// d.addSource(s); |
787 /// d.addSource(s); |
755 /// d.start(); |
788 /// d.start(); |
756 ///\endcode |
789 ///\endcode |
783 ///@} |
818 ///@} |
784 |
819 |
785 ///\name Query Functions |
820 ///\name Query Functions |
786 ///The result of the %Dijkstra algorithm can be obtained using these |
821 ///The result of the %Dijkstra algorithm can be obtained using these |
787 ///functions.\n |
822 ///functions.\n |
788 ///Before the use of these functions, |
823 ///Either \ref lemon::Dijkstra::run() "run()" or |
789 ///either run() or start() must be called. |
824 ///\ref lemon::Dijkstra::start() "start()" must be called before |
|
825 ///using them. |
790 |
826 |
791 ///@{ |
827 ///@{ |
792 |
828 |
793 ///Gives back the shortest path. |
829 ///The shortest path to a node. |
794 |
830 |
795 ///Gives back the shortest path. |
831 ///Returns the shortest path to a node. |
796 ///\pre The \c t should be reachable from the source. |
832 /// |
797 Path path(Node t) |
833 ///\warning \c t should be reachable from the root(s). |
798 { |
834 /// |
799 return Path(*G, *_pred, t); |
835 ///\pre Either \ref run() or \ref start() must be called before |
800 } |
836 ///using this function. |
801 |
837 Path path(Node t) const { return Path(*G, *_pred, t); } |
802 ///The distance of a node from the root. |
838 |
803 |
839 ///The distance of a node from the root(s). |
804 ///Returns the distance of a node from the root. |
840 |
805 ///\pre \ref run() must be called before using this function. |
841 ///Returns the distance of a node from the root(s). |
806 ///\warning If node \c v in unreachable from the root the return value |
842 /// |
807 ///of this funcion is undefined. |
843 ///\warning If node \c v is not reachable from the root(s), then |
|
844 ///the return value of this function is undefined. |
|
845 /// |
|
846 ///\pre Either \ref run() or \ref start() must be called before |
|
847 ///using this function. |
808 Value dist(Node v) const { return (*_dist)[v]; } |
848 Value dist(Node v) const { return (*_dist)[v]; } |
809 |
849 |
810 ///The current distance of a node from the root. |
850 ///Returns the 'previous arc' of the shortest path tree for a node. |
811 |
851 |
812 ///Returns the current distance of a node from the root. |
852 ///This function returns the 'previous arc' of the shortest path |
813 ///It may be decreased in the following processes. |
853 ///tree for the node \c v, i.e. it returns the last arc of a |
814 ///\pre \c node should be reached but not processed |
854 ///shortest path from the root(s) to \c v. It is \c INVALID if \c v |
815 Value currentDist(Node v) const { return (*_heap)[v]; } |
855 ///is not reachable from the root(s) or if \c v is a root. |
816 |
856 /// |
817 ///Returns the 'previous arc' of the shortest path tree. |
857 ///The shortest path tree used here is equal to the shortest path |
818 |
858 ///tree used in \ref predNode(). |
819 ///For a node \c v it returns the 'previous arc' of the shortest path tree, |
859 /// |
820 ///i.e. it returns the last arc of a shortest path from the root to \c |
860 ///\pre Either \ref run() or \ref start() must be called before |
821 ///v. It is \ref INVALID |
861 ///using this function. |
822 ///if \c v is unreachable from the root or if \c v=s. The |
|
823 ///shortest path tree used here is equal to the shortest path tree used in |
|
824 ///\ref predNode(). \pre \ref run() must be called before using |
|
825 ///this function. |
|
826 Arc predArc(Node v) const { return (*_pred)[v]; } |
862 Arc predArc(Node v) const { return (*_pred)[v]; } |
827 |
863 |
828 ///Returns the 'previous node' of the shortest path tree. |
864 ///Returns the 'previous node' of the shortest path tree for a node. |
829 |
865 |
830 ///For a node \c v it returns the 'previous node' of the shortest path tree, |
866 ///This function returns the 'previous node' of the shortest path |
831 ///i.e. it returns the last but one node from a shortest path from the |
867 ///tree for the node \c v, i.e. it returns the last but one node |
832 ///root to \c /v. It is INVALID if \c v is unreachable from the root or if |
868 ///from a shortest path from the root(s) to \c v. It is \c INVALID |
833 ///\c v=s. The shortest path tree used here is equal to the shortest path |
869 ///if \c v is not reachable from the root(s) or if \c v is a root. |
834 ///tree used in \ref predArc(). \pre \ref run() must be called before |
870 /// |
|
871 ///The shortest path tree used here is equal to the shortest path |
|
872 ///tree used in \ref predArc(). |
|
873 /// |
|
874 ///\pre Either \ref run() or \ref start() must be called before |
835 ///using this function. |
875 ///using this function. |
836 Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
876 Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
837 G->source((*_pred)[v]); } |
877 G->source((*_pred)[v]); } |
838 |
878 |
839 ///Returns a reference to the NodeMap of distances. |
879 ///\brief Returns a const reference to the node map that stores the |
840 |
880 ///distances of the nodes. |
841 ///Returns a reference to the NodeMap of distances. \pre \ref run() must |
881 /// |
842 ///be called before using this function. |
882 ///Returns a const reference to the node map that stores the distances |
|
883 ///of the nodes calculated by the algorithm. |
|
884 /// |
|
885 ///\pre Either \ref run() or \ref init() |
|
886 ///must be called before using this function. |
843 const DistMap &distMap() const { return *_dist;} |
887 const DistMap &distMap() const { return *_dist;} |
844 |
888 |
845 ///Returns a reference to the shortest path tree map. |
889 ///\brief Returns a const reference to the node map that stores the |
846 |
890 ///predecessor arcs. |
847 ///Returns a reference to the NodeMap of the arcs of the |
891 /// |
848 ///shortest path tree. |
892 ///Returns a const reference to the node map that stores the predecessor |
849 ///\pre \ref run() must be called before using this function. |
893 ///arcs, which form the shortest path tree. |
|
894 /// |
|
895 ///\pre Either \ref run() or \ref init() |
|
896 ///must be called before using this function. |
850 const PredMap &predMap() const { return *_pred;} |
897 const PredMap &predMap() const { return *_pred;} |
851 |
898 |
852 ///Checks if a node is reachable from the root. |
899 ///Checks if a node is reachable from the root(s). |
853 |
900 |
854 ///Returns \c true if \c v is reachable from the root. |
901 ///Returns \c true if \c v is reachable from the root(s). |
855 ///\warning The source nodes are inditated as unreached. |
902 ///\pre Either \ref run() or \ref start() |
856 ///\pre \ref run() must be called before using this function. |
903 ///must be called before using this function. |
857 /// |
904 bool reached(Node v) const { return (*_heap_cross_ref)[v] != |
858 bool reached(Node v) { return (*_heap_cross_ref)[v] != Heap::PRE_HEAP; } |
905 Heap::PRE_HEAP; } |
859 |
906 |
860 ///Checks if a node is processed. |
907 ///Checks if a node is processed. |
861 |
908 |
862 ///Returns \c true if \c v is processed, i.e. the shortest |
909 ///Returns \c true if \c v is processed, i.e. the shortest |
863 ///path to \c v has already found. |
910 ///path to \c v has already found. |
864 ///\pre \ref run() must be called before using this function. |
911 ///\pre Either \ref run() or \ref start() |
865 /// |
912 ///must be called before using this function. |
866 bool processed(Node v) { return (*_heap_cross_ref)[v] == Heap::POST_HEAP; } |
913 bool processed(Node v) const { return (*_heap_cross_ref)[v] == |
|
914 Heap::POST_HEAP; } |
|
915 |
|
916 ///The current distance of a node from the root(s). |
|
917 |
|
918 ///Returns the current distance of a node from the root(s). |
|
919 ///It may be decreased in the following processes. |
|
920 ///\pre \c v should be reached but not processed. |
|
921 Value currentDist(Node v) const { return (*_heap)[v]; } |
867 |
922 |
868 ///@} |
923 ///@} |
869 }; |
924 }; |
870 |
925 |
871 |
926 |
872 |
927 ///Default traits class of dijkstra() function. |
873 |
928 |
874 |
929 ///Default traits class of dijkstra() function. |
875 ///Default traits class of Dijkstra function. |
930 ///\tparam GR The type of the digraph. |
876 |
931 ///\tparam LM The type of the length map. |
877 ///Default traits class of Dijkstra function. |
|
878 ///\tparam GR Digraph type. |
|
879 ///\tparam LM Type of length map. |
|
880 template<class GR, class LM> |
932 template<class GR, class LM> |
881 struct DijkstraWizardDefaultTraits |
933 struct DijkstraWizardDefaultTraits |
882 { |
934 { |
883 ///The digraph type the algorithm runs on. |
935 ///The type of the digraph the algorithm runs on. |
884 typedef GR Digraph; |
936 typedef GR Digraph; |
885 ///The type of the map that stores the arc lengths. |
937 ///The type of the map that stores the arc lengths. |
886 |
938 |
887 ///The type of the map that stores the arc lengths. |
939 ///The type of the map that stores the arc lengths. |
888 ///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
940 ///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
889 typedef LM LengthMap; |
941 typedef LM LengthMap; |
890 //The type of the length of the arcs. |
942 ///The type of the length of the arcs. |
891 typedef typename LM::Value Value; |
943 typedef typename LM::Value Value; |
|
944 |
892 /// Operation traits for Dijkstra algorithm. |
945 /// Operation traits for Dijkstra algorithm. |
893 |
946 |
894 /// It defines the used operation by the algorithm. |
947 /// This class defines the operations that are used in the algorithm. |
895 /// \see DijkstraDefaultOperationTraits |
948 /// \see DijkstraDefaultOperationTraits |
896 typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
949 typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
897 ///The heap type used by Dijkstra algorithm. |
950 |
898 |
951 /// The cross reference type used by the heap. |
899 /// The cross reference type used by heap. |
952 |
900 |
953 /// The cross reference type used by the heap. |
901 /// The cross reference type used by heap. |
|
902 /// Usually it is \c Digraph::NodeMap<int>. |
954 /// Usually it is \c Digraph::NodeMap<int>. |
903 typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
955 typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
904 ///Instantiates a HeapCrossRef. |
956 ///Instantiates a \ref HeapCrossRef. |
905 |
957 |
906 ///This function instantiates a \ref HeapCrossRef. |
958 ///This function instantiates a \ref HeapCrossRef. |
907 /// \param G is the digraph, to which we would like to define the |
959 /// \param g is the digraph, to which we would like to define the |
908 /// HeapCrossRef. |
960 /// HeapCrossRef. |
909 /// \todo The digraph alone may be insufficient for the initialization |
961 /// \todo The digraph alone may be insufficient for the initialization |
910 static HeapCrossRef *createHeapCrossRef(const GR &G) |
962 static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
911 { |
963 { |
912 return new HeapCrossRef(G); |
964 return new HeapCrossRef(g); |
913 } |
965 } |
914 |
966 |
915 ///The heap type used by Dijkstra algorithm. |
967 ///The heap type used by the Dijkstra algorithm. |
916 |
968 |
917 ///The heap type used by Dijkstra algorithm. |
969 ///The heap type used by the Dijkstra algorithm. |
918 /// |
970 /// |
919 ///\sa BinHeap |
971 ///\sa BinHeap |
920 ///\sa Dijkstra |
972 ///\sa Dijkstra |
921 typedef BinHeap<typename LM::Value, typename GR::template NodeMap<int>, |
973 typedef BinHeap<Value, typename Digraph::template NodeMap<int>, |
922 std::less<Value> > Heap; |
974 std::less<Value> > Heap; |
923 |
975 |
924 static Heap *createHeap(HeapCrossRef& R) |
976 ///Instantiates a \ref Heap. |
925 { |
977 |
926 return new Heap(R); |
978 ///This function instantiates a \ref Heap. |
927 } |
979 /// \param r is the HeapCrossRef which is used. |
928 |
980 static Heap *createHeap(HeapCrossRef& r) |
929 ///\brief The type of the map that stores the last |
981 { |
|
982 return new Heap(r); |
|
983 } |
|
984 |
|
985 ///\brief The type of the map that stores the predecessor |
930 ///arcs of the shortest paths. |
986 ///arcs of the shortest paths. |
931 /// |
987 /// |
932 ///The type of the map that stores the last |
988 ///The type of the map that stores the predecessor |
933 ///arcs of the shortest paths. |
989 ///arcs of the shortest paths. |
934 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
990 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
935 /// |
991 typedef NullMap <typename Digraph::Node,typename Digraph::Arc> PredMap; |
936 typedef NullMap <typename GR::Node,typename GR::Arc> PredMap; |
992 ///Instantiates a \ref PredMap. |
937 ///Instantiates a PredMap. |
|
938 |
993 |
939 ///This function instantiates a \ref PredMap. |
994 ///This function instantiates a \ref PredMap. |
940 ///\param g is the digraph, to which we would like to define the PredMap. |
995 ///\param g is the digraph, to which we would like to define the |
941 ///\todo The digraph alone may be insufficient for the initialization |
996 ///\ref PredMap. |
|
997 ///\todo The digraph alone may be insufficient to initialize |
942 #ifdef DOXYGEN |
998 #ifdef DOXYGEN |
943 static PredMap *createPredMap(const GR &g) |
999 static PredMap *createPredMap(const Digraph &g) |
944 #else |
1000 #else |
945 static PredMap *createPredMap(const GR &) |
1001 static PredMap *createPredMap(const Digraph &) |
946 #endif |
1002 #endif |
947 { |
1003 { |
948 return new PredMap(); |
1004 return new PredMap(); |
949 } |
1005 } |
950 ///The type of the map that stores whether a nodes is processed. |
1006 |
951 |
1007 ///The type of the map that indicates which nodes are processed. |
952 ///The type of the map that stores whether a nodes is processed. |
1008 |
|
1009 ///The type of the map that indicates which nodes are processed. |
953 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
1010 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
954 ///By default it is a NullMap. |
1011 ///By default it is a NullMap. |
955 ///\todo If it is set to a real map, |
1012 ///\todo If it is set to a real map, |
956 ///Dijkstra::processed() should read this. |
1013 ///Dijkstra::processed() should read this. |
957 ///\todo named parameter to set this type, function to read and write. |
1014 ///\todo named parameter to set this type, function to read and write. |
958 typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
1015 typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
959 ///Instantiates a ProcessedMap. |
1016 ///Instantiates a \ref ProcessedMap. |
960 |
1017 |
961 ///This function instantiates a \ref ProcessedMap. |
1018 ///This function instantiates a \ref ProcessedMap. |
962 ///\param g is the digraph, to which |
1019 ///\param g is the digraph, to which |
963 ///we would like to define the \ref ProcessedMap |
1020 ///we would like to define the \ref ProcessedMap. |
964 #ifdef DOXYGEN |
1021 #ifdef DOXYGEN |
965 static ProcessedMap *createProcessedMap(const GR &g) |
1022 static ProcessedMap *createProcessedMap(const Digraph &g) |
966 #else |
1023 #else |
967 static ProcessedMap *createProcessedMap(const GR &) |
1024 static ProcessedMap *createProcessedMap(const Digraph &) |
968 #endif |
1025 #endif |
969 { |
1026 { |
970 return new ProcessedMap(); |
1027 return new ProcessedMap(); |
971 } |
1028 } |
972 ///The type of the map that stores the dists of the nodes. |
1029 |
973 |
1030 ///The type of the map that stores the distances of the nodes. |
974 ///The type of the map that stores the dists of the nodes. |
1031 |
|
1032 ///The type of the map that stores the distances of the nodes. |
975 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
1033 ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
976 /// |
1034 typedef NullMap<typename Digraph::Node,Value> DistMap; |
977 typedef NullMap<typename Digraph::Node,typename LM::Value> DistMap; |
1035 ///Instantiates a \ref DistMap. |
978 ///Instantiates a DistMap. |
|
979 |
1036 |
980 ///This function instantiates a \ref DistMap. |
1037 ///This function instantiates a \ref DistMap. |
981 ///\param g is the digraph, to which we would like to define |
1038 ///\param g is the digraph, to which we would like to define |
982 ///the \ref DistMap |
1039 ///the \ref DistMap |
983 #ifdef DOXYGEN |
1040 #ifdef DOXYGEN |
984 static DistMap *createDistMap(const GR &g) |
1041 static DistMap *createDistMap(const Digraph &g) |
985 #else |
1042 #else |
986 static DistMap *createDistMap(const GR &) |
1043 static DistMap *createDistMap(const Digraph &) |
987 #endif |
1044 #endif |
988 { |
1045 { |
989 return new DistMap(); |
1046 return new DistMap(); |
990 } |
1047 } |
991 }; |
1048 }; |
992 |
1049 |
993 /// Default traits used by \ref DijkstraWizard |
1050 /// Default traits class used by \ref DijkstraWizard |
994 |
1051 |
995 /// To make it easier to use Dijkstra algorithm |
1052 /// To make it easier to use Dijkstra algorithm |
996 ///we have created a wizard class. |
1053 /// we have created a wizard class. |
997 /// This \ref DijkstraWizard class needs default traits, |
1054 /// This \ref DijkstraWizard class needs default traits, |
998 ///as well as the \ref Dijkstra class. |
1055 /// as well as the \ref Dijkstra class. |
999 /// The \ref DijkstraWizardBase is a class to be the default traits of the |
1056 /// The \ref DijkstraWizardBase is a class to be the default traits of the |
1000 /// \ref DijkstraWizard class. |
1057 /// \ref DijkstraWizard class. |
1001 /// \todo More named parameters are required... |
1058 /// \todo More named parameters are required... |
1002 template<class GR,class LM> |
1059 template<class GR,class LM> |
1003 class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM> |
1060 class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM> |
1004 { |
1061 { |
1005 |
|
1006 typedef DijkstraWizardDefaultTraits<GR,LM> Base; |
1062 typedef DijkstraWizardDefaultTraits<GR,LM> Base; |
1007 protected: |
1063 protected: |
1008 /// Type of the nodes in the digraph. |
1064 //The type of the nodes in the digraph. |
1009 typedef typename Base::Digraph::Node Node; |
1065 typedef typename Base::Digraph::Node Node; |
1010 |
1066 |
1011 /// Pointer to the underlying digraph. |
1067 //Pointer to the digraph the algorithm runs on. |
1012 void *_g; |
1068 void *_g; |
1013 /// Pointer to the length map |
1069 //Pointer to the length map |
1014 void *_length; |
1070 void *_length; |
1015 ///Pointer to the map of predecessors arcs. |
1071 //Pointer to the map of predecessors arcs. |
1016 void *_pred; |
1072 void *_pred; |
1017 ///Pointer to the map of distances. |
1073 //Pointer to the map of distances. |
1018 void *_dist; |
1074 void *_dist; |
1019 ///Pointer to the source node. |
1075 //Pointer to the source node. |
1020 Node _source; |
1076 Node _source; |
1021 |
1077 |
1022 public: |
1078 public: |
1023 /// Constructor. |
1079 /// Constructor. |
1024 |
1080 |
1025 /// This constructor does not require parameters, therefore it initiates |
1081 /// This constructor does not require parameters, therefore it initiates |
1026 /// all of the attributes to default values (0, INVALID). |
1082 /// all of the attributes to default values (0, INVALID). |
1027 DijkstraWizardBase() : _g(0), _length(0), _pred(0), |
1083 DijkstraWizardBase() : _g(0), _length(0), _pred(0), |
1030 /// Constructor. |
1086 /// Constructor. |
1031 |
1087 |
1032 /// This constructor requires some parameters, |
1088 /// This constructor requires some parameters, |
1033 /// listed in the parameters list. |
1089 /// listed in the parameters list. |
1034 /// Others are initiated to 0. |
1090 /// Others are initiated to 0. |
1035 /// \param g is the initial value of \ref _g |
1091 /// \param g The digraph the algorithm runs on. |
1036 /// \param l is the initial value of \ref _length |
1092 /// \param l The length map. |
1037 /// \param s is the initial value of \ref _source |
1093 /// \param s The source node. |
1038 DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) : |
1094 DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) : |
1039 _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
1095 _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
1040 _length(reinterpret_cast<void*>(const_cast<LM*>(&l))), |
1096 _length(reinterpret_cast<void*>(const_cast<LM*>(&l))), |
1041 _pred(0), _dist(0), _source(s) {} |
1097 _pred(0), _dist(0), _source(s) {} |
1042 |
1098 |
1043 }; |
1099 }; |
1044 |
1100 |
1045 /// A class to make the usage of Dijkstra algorithm easier |
1101 /// Auxiliary class for the function type interface of Dijkstra algorithm. |
1046 |
1102 |
1047 /// This class is created to make it easier to use Dijkstra algorithm. |
1103 /// This auxiliary class is created to implement the function type |
1048 /// It uses the functions and features of the plain \ref Dijkstra, |
1104 /// interface of \ref Dijkstra algorithm. It uses the functions and features |
1049 /// but it is much simpler to use it. |
1105 /// of the plain \ref Dijkstra, but it is much simpler to use it. |
|
1106 /// It should only be used through the \ref dijkstra() function, which makes |
|
1107 /// it easier to use the algorithm. |
1050 /// |
1108 /// |
1051 /// Simplicity means that the way to change the types defined |
1109 /// Simplicity means that the way to change the types defined |
1052 /// in the traits class is based on functions that returns the new class |
1110 /// in the traits class is based on functions that returns the new class |
1053 /// and not on templatable built-in classes. |
1111 /// and not on templatable built-in classes. |
1054 /// When using the plain \ref Dijkstra |
1112 /// When using the plain \ref Dijkstra |
1055 /// the new class with the modified type comes from |
1113 /// the new class with the modified type comes from |
1056 /// the original class by using the :: |
1114 /// the original class by using the :: |
1057 /// operator. In the case of \ref DijkstraWizard only |
1115 /// operator. In the case of \ref DijkstraWizard only |
1058 /// a function have to be called and it will |
1116 /// a function have to be called, and it will |
1059 /// return the needed class. |
1117 /// return the needed class. |
1060 /// |
1118 /// |
1061 /// It does not have own \ref run method. When its \ref run method is called |
1119 /// It does not have own \ref run() method. When its \ref run() method |
1062 /// it initiates a plain \ref Dijkstra class, and calls the \ref |
1120 /// is called, it initiates a plain \ref Dijkstra object, and calls the |
1063 /// Dijkstra::run method of it. |
1121 /// \ref Dijkstra::run() method of it. |
1064 template<class TR> |
1122 template<class TR> |
1065 class DijkstraWizard : public TR |
1123 class DijkstraWizard : public TR |
1066 { |
1124 { |
1067 typedef TR Base; |
1125 typedef TR Base; |
1068 |
1126 |
1069 ///The type of the underlying digraph. |
1127 ///The type of the digraph the algorithm runs on. |
1070 typedef typename TR::Digraph Digraph; |
1128 typedef typename TR::Digraph Digraph; |
1071 //\e |
1129 |
1072 typedef typename Digraph::Node Node; |
1130 typedef typename Digraph::Node Node; |
1073 //\e |
|
1074 typedef typename Digraph::NodeIt NodeIt; |
1131 typedef typename Digraph::NodeIt NodeIt; |
1075 //\e |
|
1076 typedef typename Digraph::Arc Arc; |
1132 typedef typename Digraph::Arc Arc; |
1077 //\e |
|
1078 typedef typename Digraph::OutArcIt OutArcIt; |
1133 typedef typename Digraph::OutArcIt OutArcIt; |
1079 |
1134 |
1080 ///The type of the map that stores the arc lengths. |
1135 ///The type of the map that stores the arc lengths. |
1081 typedef typename TR::LengthMap LengthMap; |
1136 typedef typename TR::LengthMap LengthMap; |
1082 ///The type of the length of the arcs. |
1137 ///The type of the length of the arcs. |
1083 typedef typename LengthMap::Value Value; |
1138 typedef typename LengthMap::Value Value; |
1084 ///\brief The type of the map that stores the last |
1139 ///\brief The type of the map that stores the predecessor |
1085 ///arcs of the shortest paths. |
1140 ///arcs of the shortest paths. |
1086 typedef typename TR::PredMap PredMap; |
1141 typedef typename TR::PredMap PredMap; |
1087 ///The type of the map that stores the dists of the nodes. |
1142 ///The type of the map that stores the distances of the nodes. |
1088 typedef typename TR::DistMap DistMap; |
1143 typedef typename TR::DistMap DistMap; |
|
1144 ///The type of the map that indicates which nodes are processed. |
|
1145 typedef typename TR::ProcessedMap ProcessedMap; |
1089 ///The heap type used by the dijkstra algorithm. |
1146 ///The heap type used by the dijkstra algorithm. |
1090 typedef typename TR::Heap Heap; |
1147 typedef typename TR::Heap Heap; |
|
1148 |
1091 public: |
1149 public: |
|
1150 |
1092 /// Constructor. |
1151 /// Constructor. |
1093 DijkstraWizard() : TR() {} |
1152 DijkstraWizard() : TR() {} |
1094 |
1153 |
1095 /// Constructor that requires parameters. |
1154 /// Constructor that requires parameters. |
1096 |
1155 |