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