[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 | |
---|
[758] | 33 | /// \addtogroup flowalgs |
---|
[430] | 34 | /// @{ |
---|
| 35 | |
---|
[954] | 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. |
---|
[953] | 41 | template<class GR, class LM> |
---|
| 42 | struct DijkstraDefaultTraits |
---|
| 43 | { |
---|
[954] | 44 | ///The graph type the algorithm runs on. |
---|
[953] | 45 | typedef GR Graph; |
---|
| 46 | ///The type of the map that stores the edge lengths. |
---|
| 47 | |
---|
[1124] | 48 | ///The type of the map that stores the edge lengths. |
---|
[967] | 49 | ///It must meet the \ref concept::ReadMap "ReadMap" concept. |
---|
[953] | 50 | typedef LM LengthMap; |
---|
[954] | 51 | //The type of the length of the edges. |
---|
[987] | 52 | typedef typename LM::Value Value; |
---|
[954] | 53 | ///The heap type used by Dijkstra algorithm. |
---|
[967] | 54 | |
---|
| 55 | ///The heap type used by Dijkstra algorithm. |
---|
| 56 | /// |
---|
| 57 | ///\sa BinHeap |
---|
| 58 | ///\sa Dijkstra |
---|
[953] | 59 | typedef BinHeap<typename Graph::Node, |
---|
[987] | 60 | typename LM::Value, |
---|
[953] | 61 | typename GR::template NodeMap<int>, |
---|
[987] | 62 | std::less<Value> > Heap; |
---|
[953] | 63 | |
---|
| 64 | ///\brief The type of the map that stores the last |
---|
| 65 | ///edges of the shortest paths. |
---|
| 66 | /// |
---|
[1124] | 67 | ///The type of the map that stores the last |
---|
| 68 | ///edges of the shortest paths. |
---|
[967] | 69 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
[953] | 70 | /// |
---|
[954] | 71 | typedef typename Graph::template NodeMap<typename GR::Edge> PredMap; |
---|
| 72 | ///Instantiates a PredMap. |
---|
[953] | 73 | |
---|
[1123] | 74 | ///This function instantiates a \ref PredMap. |
---|
| 75 | ///\param G is the graph, to which we would like to define the PredMap. |
---|
[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 | /// |
---|
[1124] | 84 | ///The type of the map that stores the last but one |
---|
| 85 | ///nodes of the shortest paths. |
---|
[967] | 86 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
[953] | 87 | /// |
---|
[954] | 88 | typedef typename Graph::template NodeMap<typename GR::Node> PredNodeMap; |
---|
| 89 | ///Instantiates a PredNodeMap. |
---|
[1125] | 90 | |
---|
[1123] | 91 | ///This function instantiates a \ref PredNodeMap. |
---|
| 92 | ///\param G is the graph, to which we would like to define the \ref PredNodeMap |
---|
[954] | 93 | static PredNodeMap *createPredNodeMap(const GR &G) |
---|
[953] | 94 | { |
---|
| 95 | return new PredNodeMap(G); |
---|
| 96 | } |
---|
[1119] | 97 | |
---|
| 98 | ///The type of the map that stores whether a nodes is reached. |
---|
| 99 | |
---|
[1124] | 100 | ///The type of the map that stores whether a nodes is reached. |
---|
[1119] | 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 | |
---|
[1123] | 108 | ///This function instantiates a \ref ReachedMap. |
---|
| 109 | ///\param G is the graph, to which we would like to define the \ref ReachedMap |
---|
[1119] | 110 | static ReachedMap *createReachedMap(const GR &G) |
---|
| 111 | { |
---|
| 112 | return new ReachedMap(); |
---|
| 113 | } |
---|
[953] | 114 | ///The type of the map that stores the dists of the nodes. |
---|
| 115 | |
---|
[1124] | 116 | ///The type of the map that stores the dists of the nodes. |
---|
[967] | 117 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
[953] | 118 | /// |
---|
[987] | 119 | typedef typename Graph::template NodeMap<typename LM::Value> DistMap; |
---|
[954] | 120 | ///Instantiates a DistMap. |
---|
[953] | 121 | |
---|
[1123] | 122 | ///This function instantiates a \ref DistMap. |
---|
| 123 | ///\param G is the graph, to which we would like to define the \ref DistMap |
---|
[954] | 124 | static DistMap *createDistMap(const GR &G) |
---|
[953] | 125 | { |
---|
| 126 | return new DistMap(G); |
---|
| 127 | } |
---|
| 128 | }; |
---|
| 129 | |
---|
[255] | 130 | ///%Dijkstra algorithm class. |
---|
[1125] | 131 | |
---|
[255] | 132 | ///This class provides an efficient implementation of %Dijkstra algorithm. |
---|
| 133 | ///The edge lengths are passed to the algorithm using a |
---|
[959] | 134 | ///\ref concept::ReadMap "ReadMap", |
---|
[255] | 135 | ///so it is easy to change it to any kind of length. |
---|
| 136 | /// |
---|
[880] | 137 | ///The type of the length is determined by the |
---|
[987] | 138 | ///\ref concept::ReadMap::Value "Value" of the length map. |
---|
[255] | 139 | /// |
---|
| 140 | ///It is also possible to change the underlying priority heap. |
---|
| 141 | /// |
---|
[953] | 142 | ///\param GR The graph type the algorithm runs on. The default value is |
---|
[955] | 143 | ///\ref ListGraph. The value of GR is not used directly by Dijkstra, it |
---|
[954] | 144 | ///is only passed to \ref DijkstraDefaultTraits. |
---|
[584] | 145 | ///\param LM This read-only |
---|
[385] | 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 |
---|
[959] | 151 | ///\ref concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>". |
---|
[955] | 152 | ///The value of LM is not used directly by Dijkstra, it |
---|
[954] | 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 |
---|
[955] | 156 | ///\ref DijkstraDefaultTraits "DijkstraDefaultTraits<GR,LM>". |
---|
[954] | 157 | ///See \ref DijkstraDefaultTraits for the documentation of |
---|
| 158 | ///a Dijkstra traits class. |
---|
[456] | 159 | /// |
---|
[689] | 160 | ///\author Jacint Szabo and Alpar Juttner |
---|
[693] | 161 | ///\todo We need a typedef-names should be standardized. (-: |
---|
[584] | 162 | |
---|
[255] | 163 | #ifdef DOXYGEN |
---|
[584] | 164 | template <typename GR, |
---|
| 165 | typename LM, |
---|
[953] | 166 | typename TR> |
---|
[255] | 167 | #else |
---|
[953] | 168 | template <typename GR=ListGraph, |
---|
[584] | 169 | typename LM=typename GR::template EdgeMap<int>, |
---|
[953] | 170 | typename TR=DijkstraDefaultTraits<GR,LM> > |
---|
[255] | 171 | #endif |
---|
[1116] | 172 | class Dijkstra { |
---|
[255] | 173 | public: |
---|
[1125] | 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 | }; |
---|
[1119] | 186 | |
---|
[953] | 187 | typedef TR Traits; |
---|
[584] | 188 | ///The type of the underlying graph. |
---|
[954] | 189 | typedef typename TR::Graph Graph; |
---|
[911] | 190 | ///\e |
---|
[255] | 191 | typedef typename Graph::Node Node; |
---|
[911] | 192 | ///\e |
---|
[255] | 193 | typedef typename Graph::NodeIt NodeIt; |
---|
[911] | 194 | ///\e |
---|
[255] | 195 | typedef typename Graph::Edge Edge; |
---|
[911] | 196 | ///\e |
---|
[255] | 197 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
| 198 | |
---|
[584] | 199 | ///The type of the length of the edges. |
---|
[987] | 200 | typedef typename TR::LengthMap::Value Value; |
---|
[693] | 201 | ///The type of the map that stores the edge lengths. |
---|
[954] | 202 | typedef typename TR::LengthMap LengthMap; |
---|
[693] | 203 | ///\brief The type of the map that stores the last |
---|
[584] | 204 | ///edges of the shortest paths. |
---|
[953] | 205 | typedef typename TR::PredMap PredMap; |
---|
[693] | 206 | ///\brief The type of the map that stores the last but one |
---|
[584] | 207 | ///nodes of the shortest paths. |
---|
[953] | 208 | typedef typename TR::PredNodeMap PredNodeMap; |
---|
[1119] | 209 | ///The type of the map indicating if a node is reached. |
---|
| 210 | typedef typename TR::ReachedMap ReachedMap; |
---|
[693] | 211 | ///The type of the map that stores the dists of the nodes. |
---|
[953] | 212 | typedef typename TR::DistMap DistMap; |
---|
| 213 | ///The heap type used by the dijkstra algorithm. |
---|
| 214 | typedef typename TR::Heap Heap; |
---|
[255] | 215 | private: |
---|
[802] | 216 | /// Pointer to the underlying graph. |
---|
[688] | 217 | const Graph *G; |
---|
[802] | 218 | /// Pointer to the length map |
---|
[954] | 219 | const LengthMap *length; |
---|
[802] | 220 | ///Pointer to the map of predecessors edges. |
---|
[1119] | 221 | PredMap *_pred; |
---|
| 222 | ///Indicates if \ref _pred is locally allocated (\c true) or not. |
---|
| 223 | bool local_pred; |
---|
[802] | 224 | ///Pointer to the map of predecessors nodes. |
---|
[688] | 225 | PredNodeMap *pred_node; |
---|
[802] | 226 | ///Indicates if \ref pred_node is locally allocated (\c true) or not. |
---|
[688] | 227 | bool local_pred_node; |
---|
[802] | 228 | ///Pointer to the map of distances. |
---|
[688] | 229 | DistMap *distance; |
---|
[802] | 230 | ///Indicates if \ref distance is locally allocated (\c true) or not. |
---|
[688] | 231 | bool local_distance; |
---|
[1119] | 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; |
---|
[688] | 236 | |
---|
[802] | 237 | ///The source node of the last execution. |
---|
[774] | 238 | Node source; |
---|
| 239 | |
---|
[785] | 240 | ///Initializes the maps. |
---|
[688] | 241 | |
---|
[694] | 242 | ///\todo Error if \c G or are \c NULL. What about \c length? |
---|
[688] | 243 | ///\todo Better memory allocation (instead of new). |
---|
| 244 | void init_maps() |
---|
| 245 | { |
---|
[1119] | 246 | if(!_pred) { |
---|
| 247 | local_pred = true; |
---|
| 248 | _pred = Traits::createPredMap(*G); |
---|
[688] | 249 | } |
---|
| 250 | if(!pred_node) { |
---|
| 251 | local_pred_node = true; |
---|
[953] | 252 | pred_node = Traits::createPredNodeMap(*G); |
---|
[688] | 253 | } |
---|
| 254 | if(!distance) { |
---|
| 255 | local_distance = true; |
---|
[953] | 256 | distance = Traits::createDistMap(*G); |
---|
[688] | 257 | } |
---|
[1119] | 258 | if(!_reached) { |
---|
| 259 | local_reached = true; |
---|
| 260 | _reached = Traits::createReachedMap(*G); |
---|
| 261 | } |
---|
[688] | 262 | } |
---|
[255] | 263 | |
---|
| 264 | public : |
---|
[1116] | 265 | |
---|
[953] | 266 | template <class T> |
---|
[1116] | 267 | struct DefPredMapTraits : public Traits { |
---|
[953] | 268 | typedef T PredMap; |
---|
| 269 | ///\todo An exception should be thrown. |
---|
| 270 | /// |
---|
| 271 | static PredMap *createPredMap(const Graph &G) |
---|
| 272 | { |
---|
[1126] | 273 | throw UninitializedParameter(); |
---|
[953] | 274 | } |
---|
| 275 | }; |
---|
[954] | 276 | ///\ref named-templ-param "Named parameter" for setting PredMap type |
---|
| 277 | |
---|
| 278 | ///\ref named-templ-param "Named parameter" for setting PredMap type |
---|
[1043] | 279 | /// |
---|
[953] | 280 | template <class T> |
---|
[1116] | 281 | class DefPredMap : public Dijkstra< Graph, |
---|
[953] | 282 | LengthMap, |
---|
[1116] | 283 | DefPredMapTraits<T> > { }; |
---|
[953] | 284 | |
---|
| 285 | template <class T> |
---|
[1116] | 286 | struct DefPredNodeMapTraits : public Traits { |
---|
[953] | 287 | typedef T PredNodeMap; |
---|
| 288 | ///\todo An exception should be thrown. |
---|
| 289 | /// |
---|
| 290 | static PredNodeMap *createPredNodeMap(const Graph &G) |
---|
| 291 | { |
---|
[1126] | 292 | throw UninitializedParameter(); |
---|
[953] | 293 | } |
---|
| 294 | }; |
---|
[954] | 295 | ///\ref named-templ-param "Named parameter" for setting PredNodeMap type |
---|
| 296 | |
---|
| 297 | ///\ref named-templ-param "Named parameter" for setting PredNodeMap type |
---|
[1043] | 298 | /// |
---|
[953] | 299 | template <class T> |
---|
[1116] | 300 | class DefPredNodeMap : public Dijkstra< Graph, |
---|
[953] | 301 | LengthMap, |
---|
[1116] | 302 | DefPredNodeMapTraits<T> > { }; |
---|
[953] | 303 | |
---|
| 304 | template <class T> |
---|
[1116] | 305 | struct DefDistMapTraits : public Traits { |
---|
[953] | 306 | typedef T DistMap; |
---|
| 307 | ///\todo An exception should be thrown. |
---|
| 308 | /// |
---|
| 309 | static DistMap *createDistMap(const Graph &G) |
---|
| 310 | { |
---|
[1126] | 311 | throw UninitializedParameter(); |
---|
[953] | 312 | } |
---|
| 313 | }; |
---|
[954] | 314 | ///\ref named-templ-param "Named parameter" for setting DistMap type |
---|
| 315 | |
---|
| 316 | ///\ref named-templ-param "Named parameter" for setting DistMap type |
---|
[1043] | 317 | /// |
---|
[953] | 318 | template <class T> |
---|
[1116] | 319 | class DefDistMap : public Dijkstra< Graph, |
---|
[953] | 320 | LengthMap, |
---|
[1116] | 321 | DefDistMapTraits<T> > { }; |
---|
[953] | 322 | |
---|
[802] | 323 | ///Constructor. |
---|
[255] | 324 | |
---|
[802] | 325 | ///\param _G the graph the algorithm will run on. |
---|
| 326 | ///\param _length the length map used by the algorithm. |
---|
[954] | 327 | Dijkstra(const Graph& _G, const LengthMap& _length) : |
---|
[688] | 328 | G(&_G), length(&_length), |
---|
[1119] | 329 | _pred(NULL), local_pred(false), |
---|
[707] | 330 | pred_node(NULL), local_pred_node(false), |
---|
[1119] | 331 | distance(NULL), local_distance(false), |
---|
| 332 | _reached(NULL), local_reached(false) |
---|
[688] | 333 | { } |
---|
| 334 | |
---|
[802] | 335 | ///Destructor. |
---|
[688] | 336 | ~Dijkstra() |
---|
| 337 | { |
---|
[1119] | 338 | if(local_pred) delete _pred; |
---|
[688] | 339 | if(local_pred_node) delete pred_node; |
---|
| 340 | if(local_distance) delete distance; |
---|
[1119] | 341 | if(local_reached) delete _reached; |
---|
[688] | 342 | } |
---|
| 343 | |
---|
| 344 | ///Sets the length map. |
---|
| 345 | |
---|
| 346 | ///Sets the length map. |
---|
| 347 | ///\return <tt> (*this) </tt> |
---|
[1116] | 348 | Dijkstra &lengthMap(const LengthMap &m) |
---|
[688] | 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> |
---|
[1116] | 361 | Dijkstra &predMap(PredMap &m) |
---|
[688] | 362 | { |
---|
[1119] | 363 | if(local_pred) { |
---|
| 364 | delete _pred; |
---|
| 365 | local_pred=false; |
---|
[688] | 366 | } |
---|
[1119] | 367 | _pred = &m; |
---|
[688] | 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> |
---|
[1116] | 378 | Dijkstra &predNodeMap(PredNodeMap &m) |
---|
[688] | 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> |
---|
[1116] | 395 | Dijkstra &distMap(DistMap &m) |
---|
[688] | 396 | { |
---|
| 397 | if(local_distance) { |
---|
| 398 | delete distance; |
---|
| 399 | local_distance=false; |
---|
| 400 | } |
---|
| 401 | distance = &m; |
---|
| 402 | return *this; |
---|
| 403 | } |
---|
[255] | 404 | |
---|
[694] | 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. |
---|
[954] | 413 | ///\todo heap_map's type could also be in the traits class. |
---|
[694] | 414 | void run(Node s) { |
---|
| 415 | |
---|
| 416 | init_maps(); |
---|
| 417 | |
---|
[774] | 418 | source = s; |
---|
| 419 | |
---|
| 420 | for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
---|
[1119] | 421 | _pred->set(u,INVALID); |
---|
[694] | 422 | pred_node->set(u,INVALID); |
---|
[1119] | 423 | ///\todo *_reached is not set to false. |
---|
[694] | 424 | } |
---|
| 425 | |
---|
[954] | 426 | typename Graph::template NodeMap<int> heap_map(*G,-1); |
---|
[694] | 427 | |
---|
[953] | 428 | Heap heap(heap_map); |
---|
[694] | 429 | |
---|
| 430 | heap.push(s,0); |
---|
| 431 | |
---|
| 432 | while ( !heap.empty() ) { |
---|
| 433 | |
---|
| 434 | Node v=heap.top(); |
---|
[1119] | 435 | _reached->set(v,true); |
---|
[987] | 436 | Value oldvalue=heap[v]; |
---|
[694] | 437 | heap.pop(); |
---|
| 438 | distance->set(v, oldvalue); |
---|
| 439 | |
---|
| 440 | |
---|
[774] | 441 | for(OutEdgeIt e(*G,v); e!=INVALID; ++e) { |
---|
[986] | 442 | Node w=G->target(e); |
---|
[694] | 443 | switch(heap.state(w)) { |
---|
[953] | 444 | case Heap::PRE_HEAP: |
---|
[694] | 445 | heap.push(w,oldvalue+(*length)[e]); |
---|
[1119] | 446 | _pred->set(w,e); |
---|
[694] | 447 | pred_node->set(w,v); |
---|
| 448 | break; |
---|
[953] | 449 | case Heap::IN_HEAP: |
---|
[694] | 450 | if ( oldvalue+(*length)[e] < heap[w] ) { |
---|
| 451 | heap.decrease(w, oldvalue+(*length)[e]); |
---|
[1119] | 452 | _pred->set(w,e); |
---|
[694] | 453 | pred_node->set(w,v); |
---|
| 454 | } |
---|
| 455 | break; |
---|
[953] | 456 | case Heap::POST_HEAP: |
---|
[694] | 457 | break; |
---|
| 458 | } |
---|
| 459 | } |
---|
| 460 | } |
---|
| 461 | } |
---|
[255] | 462 | |
---|
[385] | 463 | ///The distance of a node from the root. |
---|
[255] | 464 | |
---|
[385] | 465 | ///Returns the distance of a node from the root. |
---|
[255] | 466 | ///\pre \ref run() must be called before using this function. |
---|
[385] | 467 | ///\warning If node \c v in unreachable from the root the return value |
---|
[255] | 468 | ///of this funcion is undefined. |
---|
[987] | 469 | Value dist(Node v) const { return (*distance)[v]; } |
---|
[373] | 470 | |
---|
[584] | 471 | ///Returns the 'previous edge' of the shortest path tree. |
---|
[255] | 472 | |
---|
[584] | 473 | ///For a node \c v it returns the 'previous edge' of the shortest path tree, |
---|
[785] | 474 | ///i.e. it returns the last edge of a shortest path from the root to \c |
---|
[688] | 475 | ///v. It is \ref INVALID |
---|
| 476 | ///if \c v is unreachable from the root or if \c v=s. The |
---|
[385] | 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. |
---|
[780] | 480 | ///\todo predEdge could be a better name. |
---|
[1119] | 481 | Edge pred(Node v) const { return (*_pred)[v]; } |
---|
[373] | 482 | |
---|
[584] | 483 | ///Returns the 'previous node' of the shortest path tree. |
---|
[255] | 484 | |
---|
[584] | 485 | ///For a node \c v it returns the 'previous node' of the shortest path tree, |
---|
[385] | 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. |
---|
[688] | 491 | Node predNode(Node v) const { return (*pred_node)[v]; } |
---|
[255] | 492 | |
---|
| 493 | ///Returns a reference to the NodeMap of distances. |
---|
| 494 | |
---|
[385] | 495 | ///Returns a reference to the NodeMap of distances. \pre \ref run() must |
---|
| 496 | ///be called before using this function. |
---|
[688] | 497 | const DistMap &distMap() const { return *distance;} |
---|
[385] | 498 | |
---|
[255] | 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. |
---|
[1119] | 504 | const PredMap &predMap() const { return *_pred;} |
---|
[385] | 505 | |
---|
| 506 | ///Returns a reference to the map of nodes of shortest paths. |
---|
[255] | 507 | |
---|
| 508 | ///Returns a reference to the NodeMap of the last but one nodes of the |
---|
[385] | 509 | ///shortest path tree. |
---|
[255] | 510 | ///\pre \ref run() must be called before using this function. |
---|
[688] | 511 | const PredNodeMap &predNodeMap() const { return *pred_node;} |
---|
[255] | 512 | |
---|
[385] | 513 | ///Checks if a node is reachable from the root. |
---|
[255] | 514 | |
---|
[385] | 515 | ///Returns \c true if \c v is reachable from the root. |
---|
[802] | 516 | ///\note The root node is reported to be reached! |
---|
[255] | 517 | ///\pre \ref run() must be called before using this function. |
---|
[385] | 518 | /// |
---|
[1119] | 519 | bool reached(Node v) { return v==source || (*_pred)[v]!=INVALID; } |
---|
[255] | 520 | |
---|
| 521 | }; |
---|
[953] | 522 | |
---|
[1123] | 523 | /// Default traits used by \ref DijkstraWizard |
---|
| 524 | |
---|
[1124] | 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. |
---|
[1123] | 527 | /// The \ref DijkstraWizardBase is a class to be the default traits of the |
---|
| 528 | /// \ref DijkstraWizard class. |
---|
[1116] | 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 | |
---|
[1123] | 548 | /// Type of the nodes in the graph. |
---|
[1116] | 549 | typedef typename Base::Graph::Node Node; |
---|
| 550 | |
---|
| 551 | public: |
---|
[1123] | 552 | /// Constructor. |
---|
| 553 | |
---|
| 554 | /// This constructor does not require parameters, therefore it initiates |
---|
| 555 | /// all of the attributes to default values (0, INVALID). |
---|
[1116] | 556 | DijkstraWizardBase() : _g(0), _length(0), _pred(0), _predNode(0), |
---|
| 557 | _dist(0), _source(INVALID) {} |
---|
| 558 | |
---|
[1123] | 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 |
---|
[1116] | 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 | |
---|
[1123] | 572 | /// A class to make easier the usage of Dijkstra algorithm |
---|
[953] | 573 | |
---|
[1123] | 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. |
---|
[953] | 577 | /// |
---|
[1123] | 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 |
---|
[1124] | 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 |
---|
[1123] | 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. |
---|
[953] | 588 | template<class TR> |
---|
[1116] | 589 | class DijkstraWizard : public TR |
---|
[953] | 590 | { |
---|
[1116] | 591 | typedef TR Base; |
---|
[953] | 592 | |
---|
[1123] | 593 | ///The type of the underlying graph. |
---|
[953] | 594 | typedef typename TR::Graph Graph; |
---|
[1119] | 595 | //\e |
---|
[953] | 596 | typedef typename Graph::Node Node; |
---|
[1119] | 597 | //\e |
---|
[953] | 598 | typedef typename Graph::NodeIt NodeIt; |
---|
[1119] | 599 | //\e |
---|
[953] | 600 | typedef typename Graph::Edge Edge; |
---|
[1119] | 601 | //\e |
---|
[953] | 602 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
| 603 | |
---|
[1123] | 604 | ///The type of the map that stores the edge lengths. |
---|
[953] | 605 | typedef typename TR::LengthMap LengthMap; |
---|
[1123] | 606 | ///The type of the length of the edges. |
---|
[987] | 607 | typedef typename LengthMap::Value Value; |
---|
[1123] | 608 | ///\brief The type of the map that stores the last |
---|
| 609 | ///edges of the shortest paths. |
---|
[953] | 610 | typedef typename TR::PredMap PredMap; |
---|
[1123] | 611 | ///\brief The type of the map that stores the last but one |
---|
| 612 | ///nodes of the shortest paths. |
---|
[953] | 613 | typedef typename TR::PredNodeMap PredNodeMap; |
---|
[1123] | 614 | ///The type of the map that stores the dists of the nodes. |
---|
[953] | 615 | typedef typename TR::DistMap DistMap; |
---|
| 616 | |
---|
[1123] | 617 | ///The heap type used by the dijkstra algorithm. |
---|
[953] | 618 | typedef typename TR::Heap Heap; |
---|
[1116] | 619 | public: |
---|
[1123] | 620 | /// Constructor. |
---|
[1116] | 621 | DijkstraWizard() : TR() {} |
---|
[953] | 622 | |
---|
[1123] | 623 | /// Constructor that requires parameters. |
---|
[1124] | 624 | |
---|
| 625 | /// Constructor that requires parameters. |
---|
[1123] | 626 | /// These parameters will be the default values for the traits class. |
---|
[1116] | 627 | DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) : |
---|
| 628 | TR(g,l,s) {} |
---|
[953] | 629 | |
---|
[1123] | 630 | ///Copy constructor |
---|
[1116] | 631 | DijkstraWizard(const TR &b) : TR(b) {} |
---|
[953] | 632 | |
---|
[1116] | 633 | ~DijkstraWizard() {} |
---|
| 634 | |
---|
[1123] | 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. |
---|
[1116] | 639 | void run() |
---|
[953] | 640 | { |
---|
[1126] | 641 | if(_source==0) throw UninitializedParameter(); |
---|
[1116] | 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 | |
---|
[1124] | 649 | ///Runs Dijkstra algorithm from the given node. |
---|
[1123] | 650 | |
---|
[1124] | 651 | ///Runs Dijkstra algorithm from the given node. |
---|
[1123] | 652 | ///\param s is the given source. |
---|
[1116] | 653 | void run(Node s) |
---|
| 654 | { |
---|
| 655 | _source=(void *)&s; |
---|
| 656 | run(); |
---|
[953] | 657 | } |
---|
| 658 | |
---|
| 659 | template<class T> |
---|
[1116] | 660 | struct DefPredMapBase : public Base { |
---|
| 661 | typedef T PredMap; |
---|
[1117] | 662 | static PredMap *createPredMap(const Graph &G) { return 0; }; |
---|
| 663 | DefPredMapBase(const Base &b) : Base(b) {} |
---|
[1116] | 664 | }; |
---|
[953] | 665 | |
---|
[1123] | 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 |
---|
[1124] | 669 | /// |
---|
[953] | 670 | template<class T> |
---|
[1116] | 671 | DijkstraWizard<DefPredMapBase<T> > predMap(const T &t) |
---|
[953] | 672 | { |
---|
[1116] | 673 | _pred=(void *)&t; |
---|
| 674 | return DijkstraWizard<DefPredMapBase<T> >(*this); |
---|
[953] | 675 | } |
---|
| 676 | |
---|
[1116] | 677 | |
---|
[953] | 678 | template<class T> |
---|
[1116] | 679 | struct DefPredNodeMapBase : public Base { |
---|
| 680 | typedef T PredNodeMap; |
---|
[1117] | 681 | static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; }; |
---|
| 682 | DefPredNodeMapBase(const Base &b) : Base(b) {} |
---|
[1116] | 683 | }; |
---|
| 684 | |
---|
[1123] | 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 |
---|
[1124] | 688 | /// |
---|
[953] | 689 | template<class T> |
---|
[1116] | 690 | DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t) |
---|
[953] | 691 | { |
---|
[1116] | 692 | _predNode=(void *)&t; |
---|
| 693 | return DijkstraWizard<DefPredNodeMapBase<T> >(*this); |
---|
[953] | 694 | } |
---|
[1116] | 695 | |
---|
| 696 | template<class T> |
---|
| 697 | struct DefDistMapBase : public Base { |
---|
| 698 | typedef T DistMap; |
---|
[1117] | 699 | static DistMap *createDistMap(const Graph &G) { return 0; }; |
---|
| 700 | DefDistMapBase(const Base &b) : Base(b) {} |
---|
[1116] | 701 | }; |
---|
[953] | 702 | |
---|
[1123] | 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 |
---|
[1124] | 706 | /// |
---|
[953] | 707 | template<class T> |
---|
[1116] | 708 | DijkstraWizard<DefDistMapBase<T> > distMap(const T &t) |
---|
[953] | 709 | { |
---|
[1116] | 710 | _dist=(void *)&t; |
---|
| 711 | return DijkstraWizard<DefDistMapBase<T> >(*this); |
---|
[953] | 712 | } |
---|
[1117] | 713 | |
---|
[1123] | 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. |
---|
[1117] | 718 | DijkstraWizard<TR> &source(Node s) |
---|
[953] | 719 | { |
---|
[1116] | 720 | source=(void *)&s; |
---|
[953] | 721 | return *this; |
---|
| 722 | } |
---|
| 723 | |
---|
| 724 | }; |
---|
[255] | 725 | |
---|
[953] | 726 | ///\e |
---|
| 727 | |
---|
[954] | 728 | ///\todo Please document... |
---|
[953] | 729 | /// |
---|
| 730 | template<class GR, class LM> |
---|
[1116] | 731 | DijkstraWizard<DijkstraWizardBase<GR,LM> > |
---|
| 732 | dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID) |
---|
[953] | 733 | { |
---|
[1116] | 734 | return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s); |
---|
[953] | 735 | } |
---|
| 736 | |
---|
[430] | 737 | /// @} |
---|
[255] | 738 | |
---|
[921] | 739 | } //END OF NAMESPACE LEMON |
---|
[255] | 740 | |
---|
| 741 | #endif |
---|
| 742 | |
---|