1 | /* -*- C++ -*- |
---|
2 | * src/lemon/dijkstra.h - Part of LEMON, a generic C++ optimization library |
---|
3 | * |
---|
4 | * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
5 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
---|
6 | * |
---|
7 | * Permission to use, modify and distribute this software is granted |
---|
8 | * provided that this copyright notice appears in all copies. For |
---|
9 | * precise terms see the accompanying LICENSE file. |
---|
10 | * |
---|
11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
12 | * express or implied, and with no claim as to its suitability for any |
---|
13 | * purpose. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | #ifndef LEMON_DIJKSTRA_H |
---|
18 | #define LEMON_DIJKSTRA_H |
---|
19 | |
---|
20 | ///\ingroup flowalgs |
---|
21 | ///\file |
---|
22 | ///\brief Dijkstra algorithm. |
---|
23 | |
---|
24 | #include <lemon/list_graph.h> |
---|
25 | #include <lemon/bin_heap.h> |
---|
26 | #include <lemon/invalid.h> |
---|
27 | #include <lemon/error.h> |
---|
28 | #include <lemon/maps.h> |
---|
29 | |
---|
30 | namespace lemon { |
---|
31 | |
---|
32 | |
---|
33 | /// \addtogroup flowalgs |
---|
34 | /// @{ |
---|
35 | |
---|
36 | ///Default traits class of Dijkstra class. |
---|
37 | |
---|
38 | ///Default traits class of Dijkstra class. |
---|
39 | ///\param GR Graph type. |
---|
40 | ///\param LM Type of length map. |
---|
41 | template<class GR, class LM> |
---|
42 | struct DijkstraDefaultTraits |
---|
43 | { |
---|
44 | ///The graph type the algorithm runs on. |
---|
45 | typedef GR Graph; |
---|
46 | ///The type of the map that stores the edge lengths. |
---|
47 | |
---|
48 | ///The type of the map that stores the edge lengths. |
---|
49 | ///It must meet the \ref concept::ReadMap "ReadMap" concept. |
---|
50 | typedef LM LengthMap; |
---|
51 | //The type of the length of the edges. |
---|
52 | typedef typename LM::Value Value; |
---|
53 | ///The heap type used by Dijkstra algorithm. |
---|
54 | |
---|
55 | ///The heap type used by Dijkstra algorithm. |
---|
56 | /// |
---|
57 | ///\sa BinHeap |
---|
58 | ///\sa Dijkstra |
---|
59 | typedef BinHeap<typename Graph::Node, |
---|
60 | typename LM::Value, |
---|
61 | typename GR::template NodeMap<int>, |
---|
62 | std::less<Value> > Heap; |
---|
63 | |
---|
64 | ///\brief The type of the map that stores the last |
---|
65 | ///edges of the shortest paths. |
---|
66 | /// |
---|
67 | ///The type of the map that stores the last |
---|
68 | ///edges of the shortest paths. |
---|
69 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
70 | /// |
---|
71 | typedef typename Graph::template NodeMap<typename GR::Edge> PredMap; |
---|
72 | ///Instantiates a PredMap. |
---|
73 | |
---|
74 | ///This function instantiates a \ref PredMap. |
---|
75 | ///\param G is the graph, to which we would like to define the PredMap. |
---|
76 | ///\todo The graph alone may be insufficient for the initialization |
---|
77 | static PredMap *createPredMap(const GR &G) |
---|
78 | { |
---|
79 | return new PredMap(G); |
---|
80 | } |
---|
81 | ///\brief The type of the map that stores the last but one |
---|
82 | ///nodes of the shortest paths. |
---|
83 | /// |
---|
84 | ///The type of the map that stores the last but one |
---|
85 | ///nodes of the shortest paths. |
---|
86 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
87 | /// |
---|
88 | typedef typename Graph::template NodeMap<typename GR::Node> PredNodeMap; |
---|
89 | ///Instantiates a PredNodeMap. |
---|
90 | |
---|
91 | ///This function instantiates a \ref PredNodeMap. |
---|
92 | ///\param G is the graph, to which we would like to define the \ref PredNodeMap |
---|
93 | static PredNodeMap *createPredNodeMap(const GR &G) |
---|
94 | { |
---|
95 | return new PredNodeMap(G); |
---|
96 | } |
---|
97 | |
---|
98 | ///The type of the map that stores whether a nodes is reached. |
---|
99 | |
---|
100 | ///The type of the map that stores whether a nodes is reached. |
---|
101 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
102 | ///By default it is a NullMap. |
---|
103 | ///\todo If it is set to a real map, Dijkstra::reached() should read this. |
---|
104 | ///\todo named parameter to set this type, function to read and write. |
---|
105 | typedef NullMap<typename Graph::Node,bool> ReachedMap; |
---|
106 | ///Instantiates a ReachedMap. |
---|
107 | |
---|
108 | ///This function instantiates a \ref ReachedMap. |
---|
109 | ///\param G is the graph, to which we would like to define the \ref ReachedMap |
---|
110 | static ReachedMap *createReachedMap(const GR &G) |
---|
111 | { |
---|
112 | return new ReachedMap(); |
---|
113 | } |
---|
114 | ///The type of the map that stores the dists of the nodes. |
---|
115 | |
---|
116 | ///The type of the map that stores the dists of the nodes. |
---|
117 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
118 | /// |
---|
119 | typedef typename Graph::template NodeMap<typename LM::Value> DistMap; |
---|
120 | ///Instantiates a DistMap. |
---|
121 | |
---|
122 | ///This function instantiates a \ref DistMap. |
---|
123 | ///\param G is the graph, to which we would like to define the \ref DistMap |
---|
124 | static DistMap *createDistMap(const GR &G) |
---|
125 | { |
---|
126 | return new DistMap(G); |
---|
127 | } |
---|
128 | }; |
---|
129 | |
---|
130 | ///%Dijkstra algorithm class. |
---|
131 | |
---|
132 | ///This class provides an efficient implementation of %Dijkstra algorithm. |
---|
133 | ///The edge lengths are passed to the algorithm using a |
---|
134 | ///\ref concept::ReadMap "ReadMap", |
---|
135 | ///so it is easy to change it to any kind of length. |
---|
136 | /// |
---|
137 | ///The type of the length is determined by the |
---|
138 | ///\ref concept::ReadMap::Value "Value" of the length map. |
---|
139 | /// |
---|
140 | ///It is also possible to change the underlying priority heap. |
---|
141 | /// |
---|
142 | ///\param GR The graph type the algorithm runs on. The default value is |
---|
143 | ///\ref ListGraph. The value of GR is not used directly by Dijkstra, it |
---|
144 | ///is only passed to \ref DijkstraDefaultTraits. |
---|
145 | ///\param LM This read-only |
---|
146 | ///EdgeMap |
---|
147 | ///determines the |
---|
148 | ///lengths of the edges. It is read once for each edge, so the map |
---|
149 | ///may involve in relatively time consuming process to compute the edge |
---|
150 | ///length if it is necessary. The default map type is |
---|
151 | ///\ref concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>". |
---|
152 | ///The value of LM is not used directly by Dijkstra, it |
---|
153 | ///is only passed to \ref DijkstraDefaultTraits. |
---|
154 | ///\param TR Traits class to set various data types used by the algorithm. |
---|
155 | ///The default traits class is |
---|
156 | ///\ref DijkstraDefaultTraits "DijkstraDefaultTraits<GR,LM>". |
---|
157 | ///See \ref DijkstraDefaultTraits for the documentation of |
---|
158 | ///a Dijkstra traits class. |
---|
159 | /// |
---|
160 | ///\author Jacint Szabo and Alpar Juttner |
---|
161 | ///\todo We need a typedef-names should be standardized. (-: |
---|
162 | |
---|
163 | #ifdef DOXYGEN |
---|
164 | template <typename GR, |
---|
165 | typename LM, |
---|
166 | typename TR> |
---|
167 | #else |
---|
168 | template <typename GR=ListGraph, |
---|
169 | typename LM=typename GR::template EdgeMap<int>, |
---|
170 | typename TR=DijkstraDefaultTraits<GR,LM> > |
---|
171 | #endif |
---|
172 | class Dijkstra { |
---|
173 | public: |
---|
174 | /** |
---|
175 | * \brief \ref Exception for uninitialized parameters. |
---|
176 | * |
---|
177 | * This error represents problems in the initialization |
---|
178 | * of the parameters of the algorithms. |
---|
179 | */ |
---|
180 | class UninitializedParameter : public lemon::UninitializedParameter { |
---|
181 | public: |
---|
182 | virtual const char* exceptionName() const { |
---|
183 | return "lemon::Dijsktra::UninitializedParameter"; |
---|
184 | } |
---|
185 | }; |
---|
186 | |
---|
187 | typedef TR Traits; |
---|
188 | ///The type of the underlying graph. |
---|
189 | typedef typename TR::Graph Graph; |
---|
190 | ///\e |
---|
191 | typedef typename Graph::Node Node; |
---|
192 | ///\e |
---|
193 | typedef typename Graph::NodeIt NodeIt; |
---|
194 | ///\e |
---|
195 | typedef typename Graph::Edge Edge; |
---|
196 | ///\e |
---|
197 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
198 | |
---|
199 | ///The type of the length of the edges. |
---|
200 | typedef typename TR::LengthMap::Value Value; |
---|
201 | ///The type of the map that stores the edge lengths. |
---|
202 | typedef typename TR::LengthMap LengthMap; |
---|
203 | ///\brief The type of the map that stores the last |
---|
204 | ///edges of the shortest paths. |
---|
205 | typedef typename TR::PredMap PredMap; |
---|
206 | ///\brief The type of the map that stores the last but one |
---|
207 | ///nodes of the shortest paths. |
---|
208 | typedef typename TR::PredNodeMap PredNodeMap; |
---|
209 | ///The type of the map indicating if a node is reached. |
---|
210 | typedef typename TR::ReachedMap ReachedMap; |
---|
211 | ///The type of the map that stores the dists of the nodes. |
---|
212 | typedef typename TR::DistMap DistMap; |
---|
213 | ///The heap type used by the dijkstra algorithm. |
---|
214 | typedef typename TR::Heap Heap; |
---|
215 | private: |
---|
216 | /// Pointer to the underlying graph. |
---|
217 | const Graph *G; |
---|
218 | /// Pointer to the length map |
---|
219 | const LengthMap *length; |
---|
220 | ///Pointer to the map of predecessors edges. |
---|
221 | PredMap *_pred; |
---|
222 | ///Indicates if \ref _pred is locally allocated (\c true) or not. |
---|
223 | bool local_pred; |
---|
224 | ///Pointer to the map of predecessors nodes. |
---|
225 | PredNodeMap *pred_node; |
---|
226 | ///Indicates if \ref pred_node is locally allocated (\c true) or not. |
---|
227 | bool local_pred_node; |
---|
228 | ///Pointer to the map of distances. |
---|
229 | DistMap *distance; |
---|
230 | ///Indicates if \ref distance is locally allocated (\c true) or not. |
---|
231 | bool local_distance; |
---|
232 | ///Pointer to the map of reached status of the nodes. |
---|
233 | ReachedMap *_reached; |
---|
234 | ///Indicates if \ref _reached is locally allocated (\c true) or not. |
---|
235 | bool local_reached; |
---|
236 | |
---|
237 | ///The source node of the last execution. |
---|
238 | Node source; |
---|
239 | |
---|
240 | ///Initializes the maps. |
---|
241 | |
---|
242 | ///\todo Error if \c G or are \c NULL. What about \c length? |
---|
243 | ///\todo Better memory allocation (instead of new). |
---|
244 | void init_maps() |
---|
245 | { |
---|
246 | if(!_pred) { |
---|
247 | local_pred = true; |
---|
248 | _pred = Traits::createPredMap(*G); |
---|
249 | } |
---|
250 | if(!pred_node) { |
---|
251 | local_pred_node = true; |
---|
252 | pred_node = Traits::createPredNodeMap(*G); |
---|
253 | } |
---|
254 | if(!distance) { |
---|
255 | local_distance = true; |
---|
256 | distance = Traits::createDistMap(*G); |
---|
257 | } |
---|
258 | if(!_reached) { |
---|
259 | local_reached = true; |
---|
260 | _reached = Traits::createReachedMap(*G); |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | public : |
---|
265 | |
---|
266 | template <class T> |
---|
267 | struct DefPredMapTraits : public Traits { |
---|
268 | typedef T PredMap; |
---|
269 | ///\todo An exception should be thrown. |
---|
270 | /// |
---|
271 | static PredMap *createPredMap(const Graph &G) |
---|
272 | { |
---|
273 | throw UninitializedData(); |
---|
274 | } |
---|
275 | }; |
---|
276 | ///\ref named-templ-param "Named parameter" for setting PredMap type |
---|
277 | |
---|
278 | ///\ref named-templ-param "Named parameter" for setting PredMap type |
---|
279 | /// |
---|
280 | template <class T> |
---|
281 | class DefPredMap : public Dijkstra< Graph, |
---|
282 | LengthMap, |
---|
283 | DefPredMapTraits<T> > { }; |
---|
284 | |
---|
285 | template <class T> |
---|
286 | struct DefPredNodeMapTraits : public Traits { |
---|
287 | typedef T PredNodeMap; |
---|
288 | ///\todo An exception should be thrown. |
---|
289 | /// |
---|
290 | static PredNodeMap *createPredNodeMap(const Graph &G) |
---|
291 | { |
---|
292 | throw UninitializedData(); |
---|
293 | } |
---|
294 | }; |
---|
295 | ///\ref named-templ-param "Named parameter" for setting PredNodeMap type |
---|
296 | |
---|
297 | ///\ref named-templ-param "Named parameter" for setting PredNodeMap type |
---|
298 | /// |
---|
299 | template <class T> |
---|
300 | class DefPredNodeMap : public Dijkstra< Graph, |
---|
301 | LengthMap, |
---|
302 | DefPredNodeMapTraits<T> > { }; |
---|
303 | |
---|
304 | template <class T> |
---|
305 | struct DefDistMapTraits : public Traits { |
---|
306 | typedef T DistMap; |
---|
307 | ///\todo An exception should be thrown. |
---|
308 | /// |
---|
309 | static DistMap *createDistMap(const Graph &G) |
---|
310 | { |
---|
311 | throw UninitializedData(); |
---|
312 | } |
---|
313 | }; |
---|
314 | ///\ref named-templ-param "Named parameter" for setting DistMap type |
---|
315 | |
---|
316 | ///\ref named-templ-param "Named parameter" for setting DistMap type |
---|
317 | /// |
---|
318 | template <class T> |
---|
319 | class DefDistMap : public Dijkstra< Graph, |
---|
320 | LengthMap, |
---|
321 | DefDistMapTraits<T> > { }; |
---|
322 | |
---|
323 | ///Constructor. |
---|
324 | |
---|
325 | ///\param _G the graph the algorithm will run on. |
---|
326 | ///\param _length the length map used by the algorithm. |
---|
327 | Dijkstra(const Graph& _G, const LengthMap& _length) : |
---|
328 | G(&_G), length(&_length), |
---|
329 | _pred(NULL), local_pred(false), |
---|
330 | pred_node(NULL), local_pred_node(false), |
---|
331 | distance(NULL), local_distance(false), |
---|
332 | _reached(NULL), local_reached(false) |
---|
333 | { } |
---|
334 | |
---|
335 | ///Destructor. |
---|
336 | ~Dijkstra() |
---|
337 | { |
---|
338 | if(local_pred) delete _pred; |
---|
339 | if(local_pred_node) delete pred_node; |
---|
340 | if(local_distance) delete distance; |
---|
341 | if(local_reached) delete _reached; |
---|
342 | } |
---|
343 | |
---|
344 | ///Sets the length map. |
---|
345 | |
---|
346 | ///Sets the length map. |
---|
347 | ///\return <tt> (*this) </tt> |
---|
348 | Dijkstra &lengthMap(const LengthMap &m) |
---|
349 | { |
---|
350 | length = &m; |
---|
351 | return *this; |
---|
352 | } |
---|
353 | |
---|
354 | ///Sets the map storing the predecessor edges. |
---|
355 | |
---|
356 | ///Sets the map storing the predecessor edges. |
---|
357 | ///If you don't use this function before calling \ref run(), |
---|
358 | ///it will allocate one. The destuctor deallocates this |
---|
359 | ///automatically allocated map, of course. |
---|
360 | ///\return <tt> (*this) </tt> |
---|
361 | Dijkstra &predMap(PredMap &m) |
---|
362 | { |
---|
363 | if(local_pred) { |
---|
364 | delete _pred; |
---|
365 | local_pred=false; |
---|
366 | } |
---|
367 | _pred = &m; |
---|
368 | return *this; |
---|
369 | } |
---|
370 | |
---|
371 | ///Sets the map storing the predecessor nodes. |
---|
372 | |
---|
373 | ///Sets the map storing the predecessor nodes. |
---|
374 | ///If you don't use this function before calling \ref run(), |
---|
375 | ///it will allocate one. The destuctor deallocates this |
---|
376 | ///automatically allocated map, of course. |
---|
377 | ///\return <tt> (*this) </tt> |
---|
378 | Dijkstra &predNodeMap(PredNodeMap &m) |
---|
379 | { |
---|
380 | if(local_pred_node) { |
---|
381 | delete pred_node; |
---|
382 | local_pred_node=false; |
---|
383 | } |
---|
384 | pred_node = &m; |
---|
385 | return *this; |
---|
386 | } |
---|
387 | |
---|
388 | ///Sets the map storing the distances calculated by the algorithm. |
---|
389 | |
---|
390 | ///Sets the map storing the distances calculated by the algorithm. |
---|
391 | ///If you don't use this function before calling \ref run(), |
---|
392 | ///it will allocate one. The destuctor deallocates this |
---|
393 | ///automatically allocated map, of course. |
---|
394 | ///\return <tt> (*this) </tt> |
---|
395 | Dijkstra &distMap(DistMap &m) |
---|
396 | { |
---|
397 | if(local_distance) { |
---|
398 | delete distance; |
---|
399 | local_distance=false; |
---|
400 | } |
---|
401 | distance = &m; |
---|
402 | return *this; |
---|
403 | } |
---|
404 | |
---|
405 | ///Runs %Dijkstra algorithm from node \c s. |
---|
406 | |
---|
407 | ///This method runs the %Dijkstra algorithm from a root node \c s |
---|
408 | ///in order to |
---|
409 | ///compute the |
---|
410 | ///shortest path to each node. The algorithm computes |
---|
411 | ///- The shortest path tree. |
---|
412 | ///- The distance of each node from the root. |
---|
413 | ///\todo heap_map's type could also be in the traits class. |
---|
414 | void run(Node s) { |
---|
415 | |
---|
416 | init_maps(); |
---|
417 | |
---|
418 | source = s; |
---|
419 | |
---|
420 | for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
---|
421 | _pred->set(u,INVALID); |
---|
422 | pred_node->set(u,INVALID); |
---|
423 | ///\todo *_reached is not set to false. |
---|
424 | } |
---|
425 | |
---|
426 | typename Graph::template NodeMap<int> heap_map(*G,-1); |
---|
427 | |
---|
428 | Heap heap(heap_map); |
---|
429 | |
---|
430 | heap.push(s,0); |
---|
431 | |
---|
432 | while ( !heap.empty() ) { |
---|
433 | |
---|
434 | Node v=heap.top(); |
---|
435 | _reached->set(v,true); |
---|
436 | Value oldvalue=heap[v]; |
---|
437 | heap.pop(); |
---|
438 | distance->set(v, oldvalue); |
---|
439 | |
---|
440 | |
---|
441 | for(OutEdgeIt e(*G,v); e!=INVALID; ++e) { |
---|
442 | Node w=G->target(e); |
---|
443 | switch(heap.state(w)) { |
---|
444 | case Heap::PRE_HEAP: |
---|
445 | heap.push(w,oldvalue+(*length)[e]); |
---|
446 | _pred->set(w,e); |
---|
447 | pred_node->set(w,v); |
---|
448 | break; |
---|
449 | case Heap::IN_HEAP: |
---|
450 | if ( oldvalue+(*length)[e] < heap[w] ) { |
---|
451 | heap.decrease(w, oldvalue+(*length)[e]); |
---|
452 | _pred->set(w,e); |
---|
453 | pred_node->set(w,v); |
---|
454 | } |
---|
455 | break; |
---|
456 | case Heap::POST_HEAP: |
---|
457 | break; |
---|
458 | } |
---|
459 | } |
---|
460 | } |
---|
461 | } |
---|
462 | |
---|
463 | ///The distance of a node from the root. |
---|
464 | |
---|
465 | ///Returns the distance of a node from the root. |
---|
466 | ///\pre \ref run() must be called before using this function. |
---|
467 | ///\warning If node \c v in unreachable from the root the return value |
---|
468 | ///of this funcion is undefined. |
---|
469 | Value dist(Node v) const { return (*distance)[v]; } |
---|
470 | |
---|
471 | ///Returns the 'previous edge' of the shortest path tree. |
---|
472 | |
---|
473 | ///For a node \c v it returns the 'previous edge' of the shortest path tree, |
---|
474 | ///i.e. it returns the last edge of a shortest path from the root to \c |
---|
475 | ///v. It is \ref INVALID |
---|
476 | ///if \c v is unreachable from the root or if \c v=s. The |
---|
477 | ///shortest path tree used here is equal to the shortest path tree used in |
---|
478 | ///\ref predNode(Node v). \pre \ref run() must be called before using |
---|
479 | ///this function. |
---|
480 | ///\todo predEdge could be a better name. |
---|
481 | Edge pred(Node v) const { return (*_pred)[v]; } |
---|
482 | |
---|
483 | ///Returns the 'previous node' of the shortest path tree. |
---|
484 | |
---|
485 | ///For a node \c v it returns the 'previous node' of the shortest path tree, |
---|
486 | ///i.e. it returns the last but one node from a shortest path from the |
---|
487 | ///root to \c /v. It is INVALID if \c v is unreachable from the root or if |
---|
488 | ///\c v=s. The shortest path tree used here is equal to the shortest path |
---|
489 | ///tree used in \ref pred(Node v). \pre \ref run() must be called before |
---|
490 | ///using this function. |
---|
491 | Node predNode(Node v) const { return (*pred_node)[v]; } |
---|
492 | |
---|
493 | ///Returns a reference to the NodeMap of distances. |
---|
494 | |
---|
495 | ///Returns a reference to the NodeMap of distances. \pre \ref run() must |
---|
496 | ///be called before using this function. |
---|
497 | const DistMap &distMap() const { return *distance;} |
---|
498 | |
---|
499 | ///Returns a reference to the shortest path tree map. |
---|
500 | |
---|
501 | ///Returns a reference to the NodeMap of the edges of the |
---|
502 | ///shortest path tree. |
---|
503 | ///\pre \ref run() must be called before using this function. |
---|
504 | const PredMap &predMap() const { return *_pred;} |
---|
505 | |
---|
506 | ///Returns a reference to the map of nodes of shortest paths. |
---|
507 | |
---|
508 | ///Returns a reference to the NodeMap of the last but one nodes of the |
---|
509 | ///shortest path tree. |
---|
510 | ///\pre \ref run() must be called before using this function. |
---|
511 | const PredNodeMap &predNodeMap() const { return *pred_node;} |
---|
512 | |
---|
513 | ///Checks if a node is reachable from the root. |
---|
514 | |
---|
515 | ///Returns \c true if \c v is reachable from the root. |
---|
516 | ///\note The root node is reported to be reached! |
---|
517 | ///\pre \ref run() must be called before using this function. |
---|
518 | /// |
---|
519 | bool reached(Node v) { return v==source || (*_pred)[v]!=INVALID; } |
---|
520 | |
---|
521 | }; |
---|
522 | |
---|
523 | /// Default traits used by \ref DijkstraWizard |
---|
524 | |
---|
525 | /// To make it easier to use Dijkstra algorithm we have created a wizard class. |
---|
526 | /// This \ref DijkstraWizard class needs default traits, as well as the \ref Dijkstra class. |
---|
527 | /// The \ref DijkstraWizardBase is a class to be the default traits of the |
---|
528 | /// \ref DijkstraWizard class. |
---|
529 | template<class GR,class LM> |
---|
530 | class DijkstraWizardBase : public DijkstraDefaultTraits<GR,LM> |
---|
531 | { |
---|
532 | |
---|
533 | typedef DijkstraDefaultTraits<GR,LM> Base; |
---|
534 | protected: |
---|
535 | /// Pointer to the underlying graph. |
---|
536 | void *_g; |
---|
537 | /// Pointer to the length map |
---|
538 | void *_length; |
---|
539 | ///Pointer to the map of predecessors edges. |
---|
540 | void *_pred; |
---|
541 | ///Pointer to the map of predecessors nodes. |
---|
542 | void *_predNode; |
---|
543 | ///Pointer to the map of distances. |
---|
544 | void *_dist; |
---|
545 | ///Pointer to the source node. |
---|
546 | void *_source; |
---|
547 | |
---|
548 | /// Type of the nodes in the graph. |
---|
549 | typedef typename Base::Graph::Node Node; |
---|
550 | |
---|
551 | public: |
---|
552 | /// Constructor. |
---|
553 | |
---|
554 | /// This constructor does not require parameters, therefore it initiates |
---|
555 | /// all of the attributes to default values (0, INVALID). |
---|
556 | DijkstraWizardBase() : _g(0), _length(0), _pred(0), _predNode(0), |
---|
557 | _dist(0), _source(INVALID) {} |
---|
558 | |
---|
559 | /// Constructor. |
---|
560 | |
---|
561 | /// This constructor requires some parameters, listed in the parameters list. |
---|
562 | /// Others are initiated to 0. |
---|
563 | /// \param g is the initial value of \ref _g |
---|
564 | /// \param l is the initial value of \ref _length |
---|
565 | /// \param s is the initial value of \ref _source |
---|
566 | DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) : |
---|
567 | _g((void *)&g), _length((void *)&l), _pred(0), _predNode(0), |
---|
568 | _dist(0), _source((void *)&s) {} |
---|
569 | |
---|
570 | }; |
---|
571 | |
---|
572 | /// A class to make easier the usage of Dijkstra algorithm |
---|
573 | |
---|
574 | /// This class is created to make it easier to use Dijkstra algorithm. |
---|
575 | /// It uses the functions and features of the plain \ref Dijkstra, |
---|
576 | /// but it is much more simple to use it. |
---|
577 | /// |
---|
578 | /// Simplicity means that the way to change the types defined |
---|
579 | /// in the traits class is based on functions that returns the new class |
---|
580 | /// and not on templatable built-in classes. When using the plain \ref Dijkstra |
---|
581 | /// the new class with the modified type comes from the original class by using the :: |
---|
582 | /// operator. In the case of \ref DijkstraWizard only a function have to be called and it will |
---|
583 | /// return the needed class. |
---|
584 | /// |
---|
585 | /// It does not have own \ref run method. When its \ref run method is called |
---|
586 | /// it initiates a plain \ref Dijkstra class, and calls the \ref Dijkstra::run |
---|
587 | /// method of it. |
---|
588 | template<class TR> |
---|
589 | class DijkstraWizard : public TR |
---|
590 | { |
---|
591 | typedef TR Base; |
---|
592 | |
---|
593 | ///The type of the underlying graph. |
---|
594 | typedef typename TR::Graph Graph; |
---|
595 | //\e |
---|
596 | typedef typename Graph::Node Node; |
---|
597 | //\e |
---|
598 | typedef typename Graph::NodeIt NodeIt; |
---|
599 | //\e |
---|
600 | typedef typename Graph::Edge Edge; |
---|
601 | //\e |
---|
602 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
603 | |
---|
604 | ///The type of the map that stores the edge lengths. |
---|
605 | typedef typename TR::LengthMap LengthMap; |
---|
606 | ///The type of the length of the edges. |
---|
607 | typedef typename LengthMap::Value Value; |
---|
608 | ///\brief The type of the map that stores the last |
---|
609 | ///edges of the shortest paths. |
---|
610 | typedef typename TR::PredMap PredMap; |
---|
611 | ///\brief The type of the map that stores the last but one |
---|
612 | ///nodes of the shortest paths. |
---|
613 | typedef typename TR::PredNodeMap PredNodeMap; |
---|
614 | ///The type of the map that stores the dists of the nodes. |
---|
615 | typedef typename TR::DistMap DistMap; |
---|
616 | |
---|
617 | ///The heap type used by the dijkstra algorithm. |
---|
618 | typedef typename TR::Heap Heap; |
---|
619 | public: |
---|
620 | /// Constructor. |
---|
621 | DijkstraWizard() : TR() {} |
---|
622 | |
---|
623 | /// Constructor that requires parameters. |
---|
624 | |
---|
625 | /// Constructor that requires parameters. |
---|
626 | /// These parameters will be the default values for the traits class. |
---|
627 | DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) : |
---|
628 | TR(g,l,s) {} |
---|
629 | |
---|
630 | ///Copy constructor |
---|
631 | DijkstraWizard(const TR &b) : TR(b) {} |
---|
632 | |
---|
633 | ~DijkstraWizard() {} |
---|
634 | |
---|
635 | ///Runs Dijkstra algorithm from a given node. |
---|
636 | |
---|
637 | ///Runs Dijkstra algorithm from a given node. |
---|
638 | ///The node can be given by the \ref source function. |
---|
639 | void run() |
---|
640 | { |
---|
641 | if(_source==0) throw UninitializedData(); |
---|
642 | Dijkstra<Graph,LengthMap,TR> Dij(*(Graph*)_g,*(LengthMap*)_length); |
---|
643 | if(_pred) Dij.predMap(*(PredMap*)_pred); |
---|
644 | if(_predNode) Dij.predNodeMap(*(PredNodeMap*)_predNode); |
---|
645 | if(_dist) Dij.distMap(*(DistMap*)_dist); |
---|
646 | Dij.run(*(Node*)_source); |
---|
647 | } |
---|
648 | |
---|
649 | ///Runs Dijkstra algorithm from the given node. |
---|
650 | |
---|
651 | ///Runs Dijkstra algorithm from the given node. |
---|
652 | ///\param s is the given source. |
---|
653 | void run(Node s) |
---|
654 | { |
---|
655 | _source=(void *)&s; |
---|
656 | run(); |
---|
657 | } |
---|
658 | |
---|
659 | template<class T> |
---|
660 | struct DefPredMapBase : public Base { |
---|
661 | typedef T PredMap; |
---|
662 | static PredMap *createPredMap(const Graph &G) { return 0; }; |
---|
663 | DefPredMapBase(const Base &b) : Base(b) {} |
---|
664 | }; |
---|
665 | |
---|
666 | /// \ref named-templ-param "Named parameter" function for setting PredMap type |
---|
667 | |
---|
668 | /// \ref named-templ-param "Named parameter" function for setting PredMap type |
---|
669 | /// |
---|
670 | template<class T> |
---|
671 | DijkstraWizard<DefPredMapBase<T> > predMap(const T &t) |
---|
672 | { |
---|
673 | _pred=(void *)&t; |
---|
674 | return DijkstraWizard<DefPredMapBase<T> >(*this); |
---|
675 | } |
---|
676 | |
---|
677 | |
---|
678 | template<class T> |
---|
679 | struct DefPredNodeMapBase : public Base { |
---|
680 | typedef T PredNodeMap; |
---|
681 | static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; }; |
---|
682 | DefPredNodeMapBase(const Base &b) : Base(b) {} |
---|
683 | }; |
---|
684 | |
---|
685 | /// \ref named-templ-param "Named parameter" function for setting PredNodeMap type |
---|
686 | |
---|
687 | /// \ref named-templ-param "Named parameter" function for setting PredNodeMap type |
---|
688 | /// |
---|
689 | template<class T> |
---|
690 | DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t) |
---|
691 | { |
---|
692 | _predNode=(void *)&t; |
---|
693 | return DijkstraWizard<DefPredNodeMapBase<T> >(*this); |
---|
694 | } |
---|
695 | |
---|
696 | template<class T> |
---|
697 | struct DefDistMapBase : public Base { |
---|
698 | typedef T DistMap; |
---|
699 | static DistMap *createDistMap(const Graph &G) { return 0; }; |
---|
700 | DefDistMapBase(const Base &b) : Base(b) {} |
---|
701 | }; |
---|
702 | |
---|
703 | /// \ref named-templ-param "Named parameter" function for setting DistMap type |
---|
704 | |
---|
705 | /// \ref named-templ-param "Named parameter" function for setting DistMap type |
---|
706 | /// |
---|
707 | template<class T> |
---|
708 | DijkstraWizard<DefDistMapBase<T> > distMap(const T &t) |
---|
709 | { |
---|
710 | _dist=(void *)&t; |
---|
711 | return DijkstraWizard<DefDistMapBase<T> >(*this); |
---|
712 | } |
---|
713 | |
---|
714 | /// Sets the source node, from which the Dijkstra algorithm runs. |
---|
715 | |
---|
716 | /// Sets the source node, from which the Dijkstra algorithm runs. |
---|
717 | /// \param s is the source node. |
---|
718 | DijkstraWizard<TR> &source(Node s) |
---|
719 | { |
---|
720 | source=(void *)&s; |
---|
721 | return *this; |
---|
722 | } |
---|
723 | |
---|
724 | }; |
---|
725 | |
---|
726 | ///\e |
---|
727 | |
---|
728 | ///\todo Please document... |
---|
729 | /// |
---|
730 | template<class GR, class LM> |
---|
731 | DijkstraWizard<DijkstraWizardBase<GR,LM> > |
---|
732 | dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID) |
---|
733 | { |
---|
734 | return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s); |
---|
735 | } |
---|
736 | |
---|
737 | /// @} |
---|
738 | |
---|
739 | } //END OF NAMESPACE LEMON |
---|
740 | |
---|
741 | #endif |
---|
742 | |
---|