changeset 1219 | ce885274b754 |
parent 1201 | cb26a6250401 |
child 1220 | 20b26ee5812b |
11:0b6f35ead62a | 12:d9d984fea157 |
---|---|
74 ///\todo The graph alone may be insufficient for the initialization |
74 ///\todo The graph alone may be insufficient for the initialization |
75 static PredMap *createPredMap(const GR &G) |
75 static PredMap *createPredMap(const GR &G) |
76 { |
76 { |
77 return new PredMap(G); |
77 return new PredMap(G); |
78 } |
78 } |
79 ///\brief The type of the map that stores the last but one |
79 // ///\brief The type of the map that stores the last but one |
80 ///nodes of the shortest paths. |
80 // ///nodes of the shortest paths. |
81 /// |
81 // /// |
82 ///The type of the map that stores the last but one |
82 // ///The type of the map that stores the last but one |
83 ///nodes of the shortest paths. |
83 // ///nodes of the shortest paths. |
84 ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
84 // ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
85 /// |
85 // /// |
86 typedef NullMap<typename Graph::Node,typename Graph::Node> PredNodeMap; |
86 // typedef NullMap<typename Graph::Node,typename Graph::Node> PredNodeMap; |
87 ///Instantiates a PredNodeMap. |
87 // ///Instantiates a PredNodeMap. |
88 |
88 |
89 ///This function instantiates a \ref PredNodeMap. |
89 // ///This function instantiates a \ref PredNodeMap. |
90 ///\param G is the graph, to which |
90 // ///\param G is the graph, to which |
91 ///we would like to define the \ref PredNodeMap |
91 // ///we would like to define the \ref PredNodeMap |
92 static PredNodeMap *createPredNodeMap(const GR &G) |
92 // static PredNodeMap *createPredNodeMap(const GR &G) |
93 { |
93 // { |
94 return new PredNodeMap(); |
94 // return new PredNodeMap(); |
95 } |
95 // } |
96 |
96 |
97 ///The type of the map that stores whether a nodes is reached. |
97 ///The type of the map that stores whether a nodes is processed. |
98 |
98 |
99 ///The type of the map that stores whether a nodes is reached. |
99 ///The type of the map that stores whether a nodes is processed. |
100 ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
100 ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
101 ///By default it is a NullMap. |
101 ///By default it is a NullMap. |
102 ///\todo If it is set to a real map, Dijkstra::reached() should read this. |
102 ///\todo If it is set to a real map, |
103 ///Dijkstra::processed() should read this. |
|
103 ///\todo named parameter to set this type, function to read and write. |
104 ///\todo named parameter to set this type, function to read and write. |
104 typedef NullMap<typename Graph::Node,bool> ReachedMap; |
105 typedef NullMap<typename Graph::Node,bool> ProcessedMap; |
105 ///Instantiates a ReachedMap. |
106 ///Instantiates a ProcessedMap. |
106 |
107 |
107 ///This function instantiates a \ref ReachedMap. |
108 ///This function instantiates a \ref ProcessedMap. |
108 ///\param G is the graph, to which |
109 ///\param G is the graph, to which |
109 ///we would like to define the \ref ReachedMap |
110 ///we would like to define the \ref ProcessedMap |
110 static ReachedMap *createReachedMap(const GR &G) |
111 static ProcessedMap *createProcessedMap(const GR &G) |
111 { |
112 { |
112 return new ReachedMap(); |
113 return new ProcessedMap(); |
113 } |
114 } |
114 ///The type of the map that stores the dists of the nodes. |
115 ///The type of the map that stores the dists of the nodes. |
115 |
116 |
116 ///The type of the map that stores the dists of the nodes. |
117 ///The type of the map that stores the dists of the nodes. |
117 ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
118 ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
138 ///The type of the length is determined by the |
139 ///The type of the length is determined by the |
139 ///\ref concept::ReadMap::Value "Value" of the length map. |
140 ///\ref concept::ReadMap::Value "Value" of the length map. |
140 /// |
141 /// |
141 ///It is also possible to change the underlying priority heap. |
142 ///It is also possible to change the underlying priority heap. |
142 /// |
143 /// |
143 ///\param GR The graph type the algorithm runs on. The default value is |
144 ///\param GR The graph type the algorithm runs on. The default value |
144 ///\ref ListGraph. The value of GR is not used directly by Dijkstra, it |
145 ///is \ref ListGraph. The value of GR is not used directly by |
145 ///is only passed to \ref DijkstraDefaultTraits. |
146 ///Dijkstra, it is only passed to \ref DijkstraDefaultTraits. |
146 ///\param LM This read-only |
147 ///\param LM This read-only EdgeMap determines the lengths of the |
147 ///EdgeMap |
148 ///edges. It is read once for each edge, so the map may involve in |
148 ///determines the |
149 ///relatively time consuming process to compute the edge length if |
149 ///lengths of the edges. It is read once for each edge, so the map |
150 ///it is necessary. The default map type is \ref |
150 ///may involve in relatively time consuming process to compute the edge |
151 ///concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>". The value |
151 ///length if it is necessary. The default map type is |
152 ///of LM is not used directly by Dijkstra, it is only passed to \ref |
152 ///\ref concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>". |
153 ///DijkstraDefaultTraits. \param TR Traits class to set |
153 ///The value of LM is not used directly by Dijkstra, it |
154 ///various data types used by the algorithm. The default traits |
154 ///is only passed to \ref DijkstraDefaultTraits. |
155 ///class is \ref DijkstraDefaultTraits |
155 ///\param TR Traits class to set various data types used by the algorithm. |
156 ///"DijkstraDefaultTraits<GR,LM>". See \ref |
156 ///The default traits class is |
157 ///DijkstraDefaultTraits for the documentation of a Dijkstra traits |
157 ///\ref DijkstraDefaultTraits "DijkstraDefaultTraits<GR,LM>". |
158 ///class. |
158 ///See \ref DijkstraDefaultTraits for the documentation of |
|
159 ///a Dijkstra traits class. |
|
160 /// |
159 /// |
161 ///\author Jacint Szabo and Alpar Juttner |
160 ///\author Jacint Szabo and Alpar Juttner |
162 ///\todo A compare object would be nice. |
161 ///\todo A compare object would be nice. |
163 |
162 |
164 #ifdef DOXYGEN |
163 #ifdef DOXYGEN |
179 * of the parameters of the algorithms. |
178 * of the parameters of the algorithms. |
180 */ |
179 */ |
181 class UninitializedParameter : public lemon::UninitializedParameter { |
180 class UninitializedParameter : public lemon::UninitializedParameter { |
182 public: |
181 public: |
183 virtual const char* exceptionName() const { |
182 virtual const char* exceptionName() const { |
184 return "lemon::Dijsktra::UninitializedParameter"; |
183 return "lemon::Dijkstra::UninitializedParameter"; |
185 } |
184 } |
186 }; |
185 }; |
187 |
186 |
188 typedef TR Traits; |
187 typedef TR Traits; |
189 ///The type of the underlying graph. |
188 ///The type of the underlying graph. |
202 ///The type of the map that stores the edge lengths. |
201 ///The type of the map that stores the edge lengths. |
203 typedef typename TR::LengthMap LengthMap; |
202 typedef typename TR::LengthMap LengthMap; |
204 ///\brief The type of the map that stores the last |
203 ///\brief The type of the map that stores the last |
205 ///edges of the shortest paths. |
204 ///edges of the shortest paths. |
206 typedef typename TR::PredMap PredMap; |
205 typedef typename TR::PredMap PredMap; |
207 ///\brief The type of the map that stores the last but one |
206 // ///\brief The type of the map that stores the last but one |
208 ///nodes of the shortest paths. |
207 // ///nodes of the shortest paths. |
209 typedef typename TR::PredNodeMap PredNodeMap; |
208 // typedef typename TR::PredNodeMap PredNodeMap; |
210 ///The type of the map indicating if a node is reached. |
209 ///The type of the map indicating if a node is processed. |
211 typedef typename TR::ReachedMap ReachedMap; |
210 typedef typename TR::ProcessedMap ProcessedMap; |
212 ///The type of the map that stores the dists of the nodes. |
211 ///The type of the map that stores the dists of the nodes. |
213 typedef typename TR::DistMap DistMap; |
212 typedef typename TR::DistMap DistMap; |
214 ///The heap type used by the dijkstra algorithm. |
213 ///The heap type used by the dijkstra algorithm. |
215 typedef typename TR::Heap Heap; |
214 typedef typename TR::Heap Heap; |
216 private: |
215 private: |
220 const LengthMap *length; |
219 const LengthMap *length; |
221 ///Pointer to the map of predecessors edges. |
220 ///Pointer to the map of predecessors edges. |
222 PredMap *_pred; |
221 PredMap *_pred; |
223 ///Indicates if \ref _pred is locally allocated (\c true) or not. |
222 ///Indicates if \ref _pred is locally allocated (\c true) or not. |
224 bool local_pred; |
223 bool local_pred; |
225 ///Pointer to the map of predecessors nodes. |
224 // ///Pointer to the map of predecessors nodes. |
226 PredNodeMap *_predNode; |
225 // PredNodeMap *_predNode; |
227 ///Indicates if \ref _predNode is locally allocated (\c true) or not. |
226 // ///Indicates if \ref _predNode is locally allocated (\c true) or not. |
228 bool local_predNode; |
227 // bool local_predNode; |
229 ///Pointer to the map of distances. |
228 ///Pointer to the map of distances. |
230 DistMap *_dist; |
229 DistMap *_dist; |
231 ///Indicates if \ref _dist is locally allocated (\c true) or not. |
230 ///Indicates if \ref _dist is locally allocated (\c true) or not. |
232 bool local_dist; |
231 bool local_dist; |
233 ///Pointer to the map of reached status of the nodes. |
232 ///Pointer to the map of processed status of the nodes. |
234 ReachedMap *_reached; |
233 ProcessedMap *_processed; |
235 ///Indicates if \ref _reached is locally allocated (\c true) or not. |
234 ///Indicates if \ref _processed is locally allocated (\c true) or not. |
236 bool local_reached; |
235 bool local_processed; |
237 |
236 |
238 ///The source node of the last execution. |
237 // ///The source node of the last execution. |
239 Node source; |
238 // Node source; |
240 |
239 |
241 ///Creates the maps if necessary. |
240 ///Creates the maps if necessary. |
242 |
241 |
243 ///\todo Error if \c G or are \c NULL. What about \c length? |
242 ///\todo Error if \c G or are \c NULL. What about \c length? |
244 ///\todo Better memory allocation (instead of new). |
243 ///\todo Better memory allocation (instead of new). |
246 { |
245 { |
247 if(!_pred) { |
246 if(!_pred) { |
248 local_pred = true; |
247 local_pred = true; |
249 _pred = Traits::createPredMap(*G); |
248 _pred = Traits::createPredMap(*G); |
250 } |
249 } |
251 if(!_predNode) { |
250 // if(!_predNode) { |
252 local_predNode = true; |
251 // local_predNode = true; |
253 _predNode = Traits::createPredNodeMap(*G); |
252 // _predNode = Traits::createPredNodeMap(*G); |
254 } |
253 // } |
255 if(!_dist) { |
254 if(!_dist) { |
256 local_dist = true; |
255 local_dist = true; |
257 _dist = Traits::createDistMap(*G); |
256 _dist = Traits::createDistMap(*G); |
258 } |
257 } |
259 if(!_reached) { |
258 if(!_processed) { |
260 local_reached = true; |
259 local_processed = true; |
261 _reached = Traits::createReachedMap(*G); |
260 _processed = Traits::createProcessedMap(*G); |
262 } |
261 } |
263 } |
262 } |
264 |
263 |
265 public : |
264 public : |
266 |
265 |
283 template <class T> |
282 template <class T> |
284 class DefPredMap : public Dijkstra< Graph, |
283 class DefPredMap : public Dijkstra< Graph, |
285 LengthMap, |
284 LengthMap, |
286 DefPredMapTraits<T> > { }; |
285 DefPredMapTraits<T> > { }; |
287 |
286 |
288 template <class T> |
287 // template <class T> |
289 struct DefPredNodeMapTraits : public Traits { |
288 // struct DefPredNodeMapTraits : public Traits { |
290 typedef T PredNodeMap; |
289 // typedef T PredNodeMap; |
291 static PredNodeMap *createPredNodeMap(const Graph &G) |
290 // static PredNodeMap *createPredNodeMap(const Graph &G) |
292 { |
291 // { |
293 throw UninitializedParameter(); |
292 // throw UninitializedParameter(); |
294 } |
293 // } |
295 }; |
294 // }; |
296 ///\ref named-templ-param "Named parameter" for setting PredNodeMap type |
295 // ///\ref named-templ-param "Named parameter" for setting PredNodeMap type |
297 |
296 |
298 ///\ref named-templ-param "Named parameter" for setting PredNodeMap type |
297 // ///\ref named-templ-param "Named parameter" for setting PredNodeMap type |
299 /// |
298 // /// |
300 template <class T> |
299 // template <class T> |
301 class DefPredNodeMap : public Dijkstra< Graph, |
300 // class DefPredNodeMap : public Dijkstra< Graph, |
302 LengthMap, |
301 // LengthMap, |
303 DefPredNodeMapTraits<T> > { }; |
302 // DefPredNodeMapTraits<T> > { }; |
304 |
303 |
305 template <class T> |
304 template <class T> |
306 struct DefDistMapTraits : public Traits { |
305 struct DefDistMapTraits : public Traits { |
307 typedef T DistMap; |
306 typedef T DistMap; |
308 static DistMap *createDistMap(const Graph &G) |
307 static DistMap *createDistMap(const Graph &G) |
318 class DefDistMap : public Dijkstra< Graph, |
317 class DefDistMap : public Dijkstra< Graph, |
319 LengthMap, |
318 LengthMap, |
320 DefDistMapTraits<T> > { }; |
319 DefDistMapTraits<T> > { }; |
321 |
320 |
322 template <class T> |
321 template <class T> |
323 struct DefReachedMapTraits : public Traits { |
322 struct DefProcessedMapTraits : public Traits { |
324 typedef T ReachedMap; |
323 typedef T ProcessedMap; |
325 static ReachedMap *createReachedMap(const Graph &G) |
324 static ProcessedMap *createProcessedMap(const Graph &G) |
326 { |
325 { |
327 throw UninitializedParameter(); |
326 throw UninitializedParameter(); |
328 } |
327 } |
329 }; |
328 }; |
330 ///\ref named-templ-param "Named parameter" for setting ReachedMap type |
329 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type |
331 |
330 |
332 ///\ref named-templ-param "Named parameter" for setting ReachedMap type |
331 ///\ref named-templ-param "Named parameter" for setting ProcessedMap type |
333 /// |
332 /// |
334 template <class T> |
333 template <class T> |
335 class DefReachedMap : public Dijkstra< Graph, |
334 class DefProcessedMap : public Dijkstra< Graph, |
336 LengthMap, |
335 LengthMap, |
337 DefReachedMapTraits<T> > { }; |
336 DefProcessedMapTraits<T> > { }; |
338 |
337 |
339 struct DefGraphReachedMapTraits : public Traits { |
338 struct DefGraphProcessedMapTraits : public Traits { |
340 typedef typename Graph::template NodeMap<bool> ReachedMap; |
339 typedef typename Graph::template NodeMap<bool> ProcessedMap; |
341 static ReachedMap *createReachedMap(const Graph &G) |
340 static ProcessedMap *createProcessedMap(const Graph &G) |
342 { |
341 { |
343 return new ReachedMap(G); |
342 return new ProcessedMap(G); |
344 } |
343 } |
345 }; |
344 }; |
346 ///\brief \ref named-templ-param "Named parameter" |
345 ///\brief \ref named-templ-param "Named parameter" |
347 ///for setting the ReachedMap type to be Graph::NodeMap<bool>. |
346 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>. |
348 /// |
347 /// |
349 ///\ref named-templ-param "Named parameter" |
348 ///\ref named-templ-param "Named parameter" |
350 ///for setting the ReachedMap type to be Graph::NodeMap<bool>. |
349 ///for setting the ProcessedMap type to be Graph::NodeMap<bool>. |
351 ///If you don't set it explicitely, it will be automatically allocated. |
350 ///If you don't set it explicitely, it will be automatically allocated. |
352 template <class T> |
351 template <class T> |
353 class DefReachedMapToBeDefaultMap : |
352 class DefProcessedMapToBeDefaultMap : |
354 public Dijkstra< Graph, |
353 public Dijkstra< Graph, |
355 LengthMap, |
354 LengthMap, |
356 DefGraphReachedMapTraits> { }; |
355 DefGraphProcessedMapTraits> { }; |
357 |
356 |
358 ///@} |
357 ///@} |
359 |
358 |
360 |
359 |
361 private: |
360 private: |
368 ///\param _G the graph the algorithm will run on. |
367 ///\param _G the graph the algorithm will run on. |
369 ///\param _length the length map used by the algorithm. |
368 ///\param _length the length map used by the algorithm. |
370 Dijkstra(const Graph& _G, const LengthMap& _length) : |
369 Dijkstra(const Graph& _G, const LengthMap& _length) : |
371 G(&_G), length(&_length), |
370 G(&_G), length(&_length), |
372 _pred(NULL), local_pred(false), |
371 _pred(NULL), local_pred(false), |
373 _predNode(NULL), local_predNode(false), |
372 // _predNode(NULL), local_predNode(false), |
374 _dist(NULL), local_dist(false), |
373 _dist(NULL), local_dist(false), |
375 _reached(NULL), local_reached(false), |
374 _processed(NULL), local_processed(false), |
376 _heap_map(*G,-1),_heap(_heap_map) |
375 _heap_map(*G,-1),_heap(_heap_map) |
377 { } |
376 { } |
378 |
377 |
379 ///Destructor. |
378 ///Destructor. |
380 ~Dijkstra() |
379 ~Dijkstra() |
381 { |
380 { |
382 if(local_pred) delete _pred; |
381 if(local_pred) delete _pred; |
383 if(local_predNode) delete _predNode; |
382 // if(local_predNode) delete _predNode; |
384 if(local_dist) delete _dist; |
383 if(local_dist) delete _dist; |
385 if(local_reached) delete _reached; |
384 if(local_processed) delete _processed; |
386 } |
385 } |
387 |
386 |
388 ///Sets the length map. |
387 ///Sets the length map. |
389 |
388 |
390 ///Sets the length map. |
389 ///Sets the length map. |
410 } |
409 } |
411 _pred = &m; |
410 _pred = &m; |
412 return *this; |
411 return *this; |
413 } |
412 } |
414 |
413 |
415 ///Sets the map storing the predecessor nodes. |
414 // ///Sets the map storing the predecessor nodes. |
416 |
415 |
417 ///Sets the map storing the predecessor nodes. |
416 // ///Sets the map storing the predecessor nodes. |
418 ///If you don't use this function before calling \ref run(), |
417 // ///If you don't use this function before calling \ref run(), |
419 ///it will allocate one. The destuctor deallocates this |
418 // ///it will allocate one. The destuctor deallocates this |
420 ///automatically allocated map, of course. |
419 // ///automatically allocated map, of course. |
421 ///\return <tt> (*this) </tt> |
420 // ///\return <tt> (*this) </tt> |
422 Dijkstra &predNodeMap(PredNodeMap &m) |
421 // Dijkstra &predNodeMap(PredNodeMap &m) |
423 { |
422 // { |
424 if(local_predNode) { |
423 // if(local_predNode) { |
425 delete _predNode; |
424 // delete _predNode; |
426 local_predNode=false; |
425 // local_predNode=false; |
427 } |
426 // } |
428 _predNode = &m; |
427 // _predNode = &m; |
429 return *this; |
428 // return *this; |
430 } |
429 // } |
431 |
430 |
432 ///Sets the map storing the distances calculated by the algorithm. |
431 ///Sets the map storing the distances calculated by the algorithm. |
433 |
432 |
434 ///Sets the map storing the distances calculated by the algorithm. |
433 ///Sets the map storing the distances calculated by the algorithm. |
435 ///If you don't use this function before calling \ref run(), |
434 ///If you don't use this function before calling \ref run(), |
447 } |
446 } |
448 |
447 |
449 private: |
448 private: |
450 void finalizeNodeData(Node v,Value dst) |
449 void finalizeNodeData(Node v,Value dst) |
451 { |
450 { |
452 _reached->set(v,true); |
451 _processed->set(v,true); |
453 _dist->set(v, dst); |
452 _dist->set(v, dst); |
454 if((*_pred)[v]!=INVALID) _predNode->set(v,G->source((*_pred)[v])); ///\todo What to do? |
453 // if((*_pred)[v]!=INVALID) |
454 // _predNode->set(v,G->source((*_pred)[v])); ///\todo What to do? |
|
455 } |
455 } |
456 |
456 |
457 public: |
457 public: |
458 ///\name Excetution control |
458 ///\name Execution control |
459 ///The simplest way to execute the algorithm is to use |
459 ///The simplest way to execute the algorithm is to use |
460 ///one of the member functions called \c run(...). |
460 ///one of the member functions called \c run(...). |
461 ///\n |
461 ///\n |
462 ///It you need more control on the execution, |
462 ///If you need more control on the execution, |
463 ///first you must call \ref init(), then you can add several source nodes |
463 ///first you must call \ref init(), then you can add several source nodes |
464 ///with \ref addSource(). Finally \ref start() will perform the actual path |
464 ///with \ref addSource(). |
465 ///Finally \ref start() will perform the actual path |
|
465 ///computation. |
466 ///computation. |
466 |
467 |
467 ///@{ |
468 ///@{ |
468 |
469 |
469 ///Initializes the internal data structures. |
470 ///Initializes the internal data structures. |
475 { |
476 { |
476 create_maps(); |
477 create_maps(); |
477 |
478 |
478 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
479 for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
479 _pred->set(u,INVALID); |
480 _pred->set(u,INVALID); |
480 _predNode->set(u,INVALID); |
481 // _predNode->set(u,INVALID); |
481 ///\todo *_reached is not set to false. |
482 _processed->set(u,false); |
482 _heap_map.set(u,Heap::PRE_HEAP); |
483 _heap_map.set(u,Heap::PRE_HEAP); |
483 } |
484 } |
484 } |
485 } |
485 |
486 |
486 ///Adds a new source node. |
487 ///Adds a new source node. |
492 ///It checks if the node has already been added to the heap and |
493 ///It checks if the node has already been added to the heap and |
493 ///It is pushed to the heap only if either it was not in the heap |
494 ///It is pushed to the heap only if either it was not in the heap |
494 ///or the shortest path found till then is longer then \c dst. |
495 ///or the shortest path found till then is longer then \c dst. |
495 void addSource(Node s,Value dst=0) |
496 void addSource(Node s,Value dst=0) |
496 { |
497 { |
497 source = s; |
498 // source = s; |
498 if(_heap.state(s) != Heap::IN_HEAP) _heap.push(s,dst); |
499 if(_heap.state(s) != Heap::IN_HEAP) _heap.push(s,dst); |
499 else if(_heap[s]<dst) { |
500 else if(_heap[s]<dst) { |
500 _heap.push(s,dst); |
501 _heap.push(s,dst); |
501 _pred->set(s,INVALID); |
502 _pred->set(s,INVALID); |
502 } |
503 } |
533 break; |
534 break; |
534 } |
535 } |
535 } |
536 } |
536 } |
537 } |
537 |
538 |
538 ///Returns \c false if there are nodes to be processed in the priority heap |
539 ///\brief Returns \c false if there are nodes |
539 |
540 ///to be processed in the priority heap |
540 ///Returns \c false if there are nodes to be processed in the priority heap |
541 /// |
541 /// |
542 ///Returns \c false if there are nodes |
542 bool emptyHeap() { return _heap.empty(); } |
543 ///to be processed in the priority heap |
544 bool emptyQueue() { return _heap.empty(); } |
|
543 ///Returns the number of the nodes to be processed in the priority heap |
545 ///Returns the number of the nodes to be processed in the priority heap |
544 |
546 |
545 ///Returns the number of the nodes to be processed in the priority heap |
547 ///Returns the number of the nodes to be processed in the priority heap |
546 /// |
548 /// |
547 int heapSize() { return _heap.size(); } |
549 int queueSize() { return _heap.size(); } |
548 |
550 |
549 ///Executes the algorithm. |
551 ///Executes the algorithm. |
550 |
552 |
551 ///Executes the algorithm. |
553 ///Executes the algorithm. |
552 /// |
554 /// |
694 ///Returns a reference to the NodeMap of the edges of the |
696 ///Returns a reference to the NodeMap of the edges of the |
695 ///shortest path tree. |
697 ///shortest path tree. |
696 ///\pre \ref run() must be called before using this function. |
698 ///\pre \ref run() must be called before using this function. |
697 const PredMap &predMap() const { return *_pred;} |
699 const PredMap &predMap() const { return *_pred;} |
698 |
700 |
699 ///Returns a reference to the map of nodes of shortest paths. |
701 // ///Returns a reference to the map of nodes of shortest paths. |
700 |
702 |
701 ///Returns a reference to the NodeMap of the last but one nodes of the |
703 // ///Returns a reference to the NodeMap of the last but one nodes of the |
702 ///shortest path tree. |
704 // ///shortest path tree. |
705 // ///\pre \ref run() must be called before using this function. |
|
706 // const PredNodeMap &predNodeMap() const { return *_predNode;} |
|
707 |
|
708 ///Checks if a node is reachable from the root. |
|
709 |
|
710 ///Returns \c true if \c v is reachable from the root. |
|
711 ///\warning The source nodes are inditated as unreached. |
|
703 ///\pre \ref run() must be called before using this function. |
712 ///\pre \ref run() must be called before using this function. |
704 const PredNodeMap &predNodeMap() const { return *_predNode;} |
713 /// |
705 |
714 bool reached(Node v) { return _heap_map[v]!=Heap::PRE_HEAP; } |
706 ///Checks if a node is reachable from the root. |
|
707 |
|
708 ///Returns \c true if \c v is reachable from the root. |
|
709 ///\warning If the algorithm is started from multiple nodes, |
|
710 ///this function may give false result for the source nodes. |
|
711 ///\pre \ref run() must be called before using this function. |
|
712 /// |
|
713 bool reached(Node v) { return v==source || (*_pred)[v]!=INVALID; } |
|
714 |
715 |
715 ///@} |
716 ///@} |
716 }; |
717 }; |
717 |
718 |
719 |
|
720 |
|
721 |
|
722 |
|
723 ///Default traits class of Dijkstra function. |
|
724 |
|
725 ///Default traits class of Dijkstra function. |
|
726 ///\param GR Graph type. |
|
727 ///\param LM Type of length map. |
|
728 template<class GR, class LM> |
|
729 struct DijkstraWizardDefaultTraits |
|
730 { |
|
731 ///The graph type the algorithm runs on. |
|
732 typedef GR Graph; |
|
733 ///The type of the map that stores the edge lengths. |
|
734 |
|
735 ///The type of the map that stores the edge lengths. |
|
736 ///It must meet the \ref concept::ReadMap "ReadMap" concept. |
|
737 typedef LM LengthMap; |
|
738 //The type of the length of the edges. |
|
739 typedef typename LM::Value Value; |
|
740 ///The heap type used by Dijkstra algorithm. |
|
741 |
|
742 ///The heap type used by Dijkstra algorithm. |
|
743 /// |
|
744 ///\sa BinHeap |
|
745 ///\sa Dijkstra |
|
746 typedef BinHeap<typename Graph::Node, |
|
747 typename LM::Value, |
|
748 typename GR::template NodeMap<int>, |
|
749 std::less<Value> > Heap; |
|
750 |
|
751 ///\brief The type of the map that stores the last |
|
752 ///edges of the shortest paths. |
|
753 /// |
|
754 ///The type of the map that stores the last |
|
755 ///edges of the shortest paths. |
|
756 ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
|
757 /// |
|
758 typedef NullMap <typename GR::Node,typename GR::Edge> PredMap; |
|
759 ///Instantiates a PredMap. |
|
760 |
|
761 ///This function instantiates a \ref PredMap. |
|
762 ///\param G is the graph, to which we would like to define the PredMap. |
|
763 ///\todo The graph alone may be insufficient for the initialization |
|
764 static PredMap *createPredMap(const GR &G) |
|
765 { |
|
766 return new PredMap(); |
|
767 } |
|
768 ///The type of the map that stores whether a nodes is processed. |
|
769 |
|
770 ///The type of the map that stores whether a nodes is processed. |
|
771 ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
|
772 ///By default it is a NullMap. |
|
773 ///\todo If it is set to a real map, |
|
774 ///Dijkstra::processed() should read this. |
|
775 ///\todo named parameter to set this type, function to read and write. |
|
776 typedef NullMap<typename Graph::Node,bool> ProcessedMap; |
|
777 ///Instantiates a ProcessedMap. |
|
778 |
|
779 ///This function instantiates a \ref ProcessedMap. |
|
780 ///\param G is the graph, to which |
|
781 ///we would like to define the \ref ProcessedMap |
|
782 static ProcessedMap *createProcessedMap(const GR &G) |
|
783 { |
|
784 return new ProcessedMap(); |
|
785 } |
|
786 ///The type of the map that stores the dists of the nodes. |
|
787 |
|
788 ///The type of the map that stores the dists of the nodes. |
|
789 ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
|
790 /// |
|
791 typedef NullMap<typename Graph::Node,typename LM::Value> DistMap; |
|
792 ///Instantiates a DistMap. |
|
793 |
|
794 ///This function instantiates a \ref DistMap. |
|
795 ///\param G is the graph, to which we would like to define the \ref DistMap |
|
796 static DistMap *createDistMap(const GR &G) |
|
797 { |
|
798 return new DistMap(); |
|
799 } |
|
800 }; |
|
801 |
|
718 /// Default traits used by \ref DijkstraWizard |
802 /// Default traits used by \ref DijkstraWizard |
719 |
803 |
720 /// To make it easier to use Dijkstra algorithm |
804 /// To make it easier to use Dijkstra algorithm |
721 ///we have created a wizard class. |
805 ///we have created a wizard class. |
722 /// This \ref DijkstraWizard class needs default traits, |
806 /// This \ref DijkstraWizard class needs default traits, |
723 ///as well as the \ref Dijkstra class. |
807 ///as well as the \ref Dijkstra class. |
724 /// The \ref DijkstraWizardBase is a class to be the default traits of the |
808 /// The \ref DijkstraWizardBase is a class to be the default traits of the |
725 /// \ref DijkstraWizard class. |
809 /// \ref DijkstraWizard class. |
726 template<class GR,class LM> |
810 template<class GR,class LM> |
727 class DijkstraWizardBase : public DijkstraDefaultTraits<GR,LM> |
811 class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM> |
728 { |
812 { |
729 |
813 |
730 typedef DijkstraDefaultTraits<GR,LM> Base; |
814 typedef DijkstraWizardDefaultTraits<GR,LM> Base; |
731 protected: |
815 protected: |
732 /// Type of the nodes in the graph. |
816 /// Type of the nodes in the graph. |
733 typedef typename Base::Graph::Node Node; |
817 typedef typename Base::Graph::Node Node; |
734 |
818 |
735 /// Pointer to the underlying graph. |
819 /// Pointer to the underlying graph. |
736 void *_g; |
820 void *_g; |
737 /// Pointer to the length map |
821 /// Pointer to the length map |
738 void *_length; |
822 void *_length; |
739 ///Pointer to the map of predecessors edges. |
823 ///Pointer to the map of predecessors edges. |
740 void *_pred; |
824 void *_pred; |
741 ///Pointer to the map of predecessors nodes. |
825 // ///Pointer to the map of predecessors nodes. |
742 void *_predNode; |
826 // void *_predNode; |
743 ///Pointer to the map of distances. |
827 ///Pointer to the map of distances. |
744 void *_dist; |
828 void *_dist; |
745 ///Pointer to the source node. |
829 ///Pointer to the source node. |
746 Node _source; |
830 Node _source; |
747 |
831 |
748 public: |
832 public: |
749 /// Constructor. |
833 /// Constructor. |
750 |
834 |
751 /// This constructor does not require parameters, therefore it initiates |
835 /// This constructor does not require parameters, therefore it initiates |
752 /// all of the attributes to default values (0, INVALID). |
836 /// all of the attributes to default values (0, INVALID). |
753 DijkstraWizardBase() : _g(0), _length(0), _pred(0), _predNode(0), |
837 DijkstraWizardBase() : _g(0), _length(0), _pred(0), |
754 _dist(0), _source(INVALID) {} |
838 // _predNode(0), |
839 _dist(0), _source(INVALID) {} |
|
755 |
840 |
756 /// Constructor. |
841 /// Constructor. |
757 |
842 |
758 /// This constructor requires some parameters, |
843 /// This constructor requires some parameters, |
759 /// listed in the parameters list. |
844 /// listed in the parameters list. |
760 /// Others are initiated to 0. |
845 /// Others are initiated to 0. |
761 /// \param g is the initial value of \ref _g |
846 /// \param g is the initial value of \ref _g |
762 /// \param l is the initial value of \ref _length |
847 /// \param l is the initial value of \ref _length |
763 /// \param s is the initial value of \ref _source |
848 /// \param s is the initial value of \ref _source |
764 DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) : |
849 DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) : |
765 _g((void *)&g), _length((void *)&l), _pred(0), _predNode(0), |
850 _g((void *)&g), _length((void *)&l), _pred(0), |
766 _dist(0), _source(s) {} |
851 // _predNode(0), |
852 _dist(0), _source(s) {} |
|
767 |
853 |
768 }; |
854 }; |
769 |
855 |
770 /// A class to make easier the usage of Dijkstra algorithm |
856 /// A class to make easier the usage of Dijkstra algorithm |
771 |
857 |
772 /// \ingroup flowalgs |
|
773 /// This class is created to make it easier to use Dijkstra algorithm. |
858 /// This class is created to make it easier to use Dijkstra algorithm. |
774 /// It uses the functions and features of the plain \ref Dijkstra, |
859 /// It uses the functions and features of the plain \ref Dijkstra, |
775 /// but it is much simpler to use it. |
860 /// but it is much simpler to use it. |
776 /// |
861 /// |
777 /// Simplicity means that the way to change the types defined |
862 /// Simplicity means that the way to change the types defined |
808 ///The type of the length of the edges. |
893 ///The type of the length of the edges. |
809 typedef typename LengthMap::Value Value; |
894 typedef typename LengthMap::Value Value; |
810 ///\brief The type of the map that stores the last |
895 ///\brief The type of the map that stores the last |
811 ///edges of the shortest paths. |
896 ///edges of the shortest paths. |
812 typedef typename TR::PredMap PredMap; |
897 typedef typename TR::PredMap PredMap; |
813 ///\brief The type of the map that stores the last but one |
898 // ///\brief The type of the map that stores the last but one |
814 ///nodes of the shortest paths. |
899 // ///nodes of the shortest paths. |
815 typedef typename TR::PredNodeMap PredNodeMap; |
900 // typedef typename TR::PredNodeMap PredNodeMap; |
816 ///The type of the map that stores the dists of the nodes. |
901 ///The type of the map that stores the dists of the nodes. |
817 typedef typename TR::DistMap DistMap; |
902 typedef typename TR::DistMap DistMap; |
818 |
903 |
819 ///The heap type used by the dijkstra algorithm. |
904 ///The heap type used by the dijkstra algorithm. |
820 typedef typename TR::Heap Heap; |
905 typedef typename TR::Heap Heap; |
842 { |
927 { |
843 if(Base::_source==INVALID) throw UninitializedParameter(); |
928 if(Base::_source==INVALID) throw UninitializedParameter(); |
844 Dijkstra<Graph,LengthMap,TR> |
929 Dijkstra<Graph,LengthMap,TR> |
845 Dij(*(Graph*)Base::_g,*(LengthMap*)Base::_length); |
930 Dij(*(Graph*)Base::_g,*(LengthMap*)Base::_length); |
846 if(Base::_pred) Dij.predMap(*(PredMap*)Base::_pred); |
931 if(Base::_pred) Dij.predMap(*(PredMap*)Base::_pred); |
847 if(Base::_predNode) Dij.predNodeMap(*(PredNodeMap*)Base::_predNode); |
932 // if(Base::_predNode) Dij.predNodeMap(*(PredNodeMap*)Base::_predNode); |
848 if(Base::_dist) Dij.distMap(*(DistMap*)Base::_dist); |
933 if(Base::_dist) Dij.distMap(*(DistMap*)Base::_dist); |
849 Dij.run(Base::_source); |
934 Dij.run(Base::_source); |
850 } |
935 } |
851 |
936 |
852 ///Runs Dijkstra algorithm from the given node. |
937 ///Runs Dijkstra algorithm from the given node. |
878 Base::_pred=(void *)&t; |
963 Base::_pred=(void *)&t; |
879 return DijkstraWizard<DefPredMapBase<T> >(*this); |
964 return DijkstraWizard<DefPredMapBase<T> >(*this); |
880 } |
965 } |
881 |
966 |
882 |
967 |
883 template<class T> |
968 // template<class T> |
884 struct DefPredNodeMapBase : public Base { |
969 // struct DefPredNodeMapBase : public Base { |
885 typedef T PredNodeMap; |
970 // typedef T PredNodeMap; |
886 static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; }; |
971 // static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; }; |
887 DefPredNodeMapBase(const Base &b) : Base(b) {} |
972 // DefPredNodeMapBase(const Base &b) : Base(b) {} |
888 }; |
973 // }; |
889 |
974 |
890 ///\brief \ref named-templ-param "Named parameter" |
975 // ///\brief \ref named-templ-param "Named parameter" |
891 ///function for setting PredNodeMap type |
976 // ///function for setting PredNodeMap type |
892 /// |
977 // /// |
893 /// \ref named-templ-param "Named parameter" |
978 // /// \ref named-templ-param "Named parameter" |
894 ///function for setting PredNodeMap type |
979 // ///function for setting PredNodeMap type |
895 /// |
980 // /// |
896 template<class T> |
981 // template<class T> |
897 DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t) |
982 // DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t) |
898 { |
983 // { |
899 Base::_predNode=(void *)&t; |
984 // Base::_predNode=(void *)&t; |
900 return DijkstraWizard<DefPredNodeMapBase<T> >(*this); |
985 // return DijkstraWizard<DefPredNodeMapBase<T> >(*this); |
901 } |
986 // } |
902 |
987 |
903 template<class T> |
988 template<class T> |
904 struct DefDistMapBase : public Base { |
989 struct DefDistMapBase : public Base { |
905 typedef T DistMap; |
990 typedef T DistMap; |
906 static DistMap *createDistMap(const Graph &G) { return 0; }; |
991 static DistMap *createDistMap(const Graph &G) { return 0; }; |
930 return *this; |
1015 return *this; |
931 } |
1016 } |
932 |
1017 |
933 }; |
1018 }; |
934 |
1019 |
935 ///\e |
1020 ///Function type interface for Dijkstra algorithm. |
936 |
1021 |
937 /// \ingroup flowalgs |
1022 /// \ingroup flowalgs |
938 ///\todo Please document... |
1023 ///Function type interface for Dijkstra algorithm. |
939 /// |
1024 /// |
1025 ///This function also has several |
|
1026 ///\ref named-templ-func-param "named parameters", |
|
1027 ///they are declared as the members of class \ref DijkstraWizard. |
|
1028 ///The following |
|
1029 ///example shows how to use these parameters. |
|
1030 ///\code |
|
1031 /// dijkstra(g,length,source).predMap(preds).run(); |
|
1032 ///\endcode |
|
1033 ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()" |
|
1034 ///to the end of the parameter list. |
|
1035 ///\sa DijkstraWizard |
|
1036 ///\sa Dijkstra |
|
940 template<class GR, class LM> |
1037 template<class GR, class LM> |
941 DijkstraWizard<DijkstraWizardBase<GR,LM> > |
1038 DijkstraWizard<DijkstraWizardBase<GR,LM> > |
942 dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID) |
1039 dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID) |
943 { |
1040 { |
944 return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s); |
1041 return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s); |
945 } |
1042 } |
946 |
1043 |
947 /// @} |
|
948 |
|
949 } //END OF NAMESPACE LEMON |
1044 } //END OF NAMESPACE LEMON |
950 |
1045 |
951 #endif |
1046 #endif |
952 |
1047 |