[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 |
---|
[1128] | 161 | ///\todo A compare object would be nice. |
---|
[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 | |
---|
[1128] | 240 | ///Creates the maps if necessary. |
---|
[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). |
---|
[1128] | 244 | void create_maps() |
---|
[688] | 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 | |
---|
[1128] | 266 | ///\name Named template parameters |
---|
| 267 | |
---|
| 268 | ///@{ |
---|
| 269 | |
---|
[953] | 270 | template <class T> |
---|
[1116] | 271 | struct DefPredMapTraits : public Traits { |
---|
[953] | 272 | typedef T PredMap; |
---|
| 273 | static PredMap *createPredMap(const Graph &G) |
---|
| 274 | { |
---|
[1126] | 275 | throw UninitializedParameter(); |
---|
[953] | 276 | } |
---|
| 277 | }; |
---|
[954] | 278 | ///\ref named-templ-param "Named parameter" for setting PredMap type |
---|
| 279 | |
---|
| 280 | ///\ref named-templ-param "Named parameter" for setting PredMap type |
---|
[1043] | 281 | /// |
---|
[953] | 282 | template <class T> |
---|
[1116] | 283 | class DefPredMap : public Dijkstra< Graph, |
---|
[953] | 284 | LengthMap, |
---|
[1116] | 285 | DefPredMapTraits<T> > { }; |
---|
[953] | 286 | |
---|
| 287 | template <class T> |
---|
[1116] | 288 | struct DefPredNodeMapTraits : public Traits { |
---|
[953] | 289 | typedef T PredNodeMap; |
---|
| 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 | static DistMap *createDistMap(const Graph &G) |
---|
| 308 | { |
---|
[1126] | 309 | throw UninitializedParameter(); |
---|
[953] | 310 | } |
---|
| 311 | }; |
---|
[954] | 312 | ///\ref named-templ-param "Named parameter" for setting DistMap type |
---|
| 313 | |
---|
| 314 | ///\ref named-templ-param "Named parameter" for setting DistMap type |
---|
[1043] | 315 | /// |
---|
[953] | 316 | template <class T> |
---|
[1116] | 317 | class DefDistMap : public Dijkstra< Graph, |
---|
[953] | 318 | LengthMap, |
---|
[1116] | 319 | DefDistMapTraits<T> > { }; |
---|
[953] | 320 | |
---|
[1128] | 321 | template <class T> |
---|
| 322 | struct DefReachedMapTraits : public Traits { |
---|
| 323 | typedef T ReachedMap; |
---|
| 324 | static ReachedMap *createReachedMap(const Graph &G) |
---|
| 325 | { |
---|
| 326 | throw UninitializedParameter(); |
---|
| 327 | } |
---|
| 328 | }; |
---|
| 329 | ///\ref named-templ-param "Named parameter" for setting ReachedMap type |
---|
| 330 | |
---|
| 331 | ///\ref named-templ-param "Named parameter" for setting ReachedMap type |
---|
| 332 | /// |
---|
| 333 | template <class T> |
---|
| 334 | class DefReachedMap : public Dijkstra< Graph, |
---|
| 335 | LengthMap, |
---|
| 336 | DefReachedMapTraits<T> > { }; |
---|
| 337 | |
---|
| 338 | struct DefGraphReachedMapTraits : public Traits { |
---|
| 339 | typedef typename Graph::NodeMap<bool> ReachedMap; |
---|
| 340 | static ReachedMap *createReachedMap(const Graph &G) |
---|
| 341 | { |
---|
| 342 | return new ReachedMap(G); |
---|
| 343 | } |
---|
| 344 | }; |
---|
| 345 | ///\brief \ref named-templ-param "Named parameter" |
---|
| 346 | ///for setting the ReachedMap type to be Graph::NodeMap<bool>. |
---|
| 347 | /// |
---|
| 348 | ///\ref named-templ-param "Named parameter" |
---|
| 349 | ///for setting the ReachedMap type to be Graph::NodeMap<bool>. |
---|
| 350 | ///If you don't set it explicitely, it will be automatically allocated. |
---|
| 351 | template <class T> |
---|
| 352 | class DefReachedMapToBeDefaultMap : |
---|
| 353 | public Dijkstra< Graph, |
---|
| 354 | LengthMap, |
---|
| 355 | DefGraphReachedMapTraits> { }; |
---|
| 356 | |
---|
| 357 | ///@} |
---|
| 358 | |
---|
| 359 | |
---|
| 360 | private: |
---|
| 361 | typename Graph::template NodeMap<int> _heap_map; |
---|
| 362 | Heap _heap; |
---|
| 363 | public: |
---|
| 364 | |
---|
[802] | 365 | ///Constructor. |
---|
[255] | 366 | |
---|
[802] | 367 | ///\param _G the graph the algorithm will run on. |
---|
| 368 | ///\param _length the length map used by the algorithm. |
---|
[954] | 369 | Dijkstra(const Graph& _G, const LengthMap& _length) : |
---|
[688] | 370 | G(&_G), length(&_length), |
---|
[1119] | 371 | _pred(NULL), local_pred(false), |
---|
[707] | 372 | pred_node(NULL), local_pred_node(false), |
---|
[1119] | 373 | distance(NULL), local_distance(false), |
---|
[1128] | 374 | _reached(NULL), local_reached(false), |
---|
| 375 | _heap_map(*G,-1),_heap(_heap_map) |
---|
[688] | 376 | { } |
---|
| 377 | |
---|
[802] | 378 | ///Destructor. |
---|
[688] | 379 | ~Dijkstra() |
---|
| 380 | { |
---|
[1119] | 381 | if(local_pred) delete _pred; |
---|
[688] | 382 | if(local_pred_node) delete pred_node; |
---|
| 383 | if(local_distance) delete distance; |
---|
[1119] | 384 | if(local_reached) delete _reached; |
---|
[688] | 385 | } |
---|
| 386 | |
---|
| 387 | ///Sets the length map. |
---|
| 388 | |
---|
| 389 | ///Sets the length map. |
---|
| 390 | ///\return <tt> (*this) </tt> |
---|
[1116] | 391 | Dijkstra &lengthMap(const LengthMap &m) |
---|
[688] | 392 | { |
---|
| 393 | length = &m; |
---|
| 394 | return *this; |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | ///Sets the map storing the predecessor edges. |
---|
| 398 | |
---|
| 399 | ///Sets the map storing the predecessor edges. |
---|
| 400 | ///If you don't use this function before calling \ref run(), |
---|
| 401 | ///it will allocate one. The destuctor deallocates this |
---|
| 402 | ///automatically allocated map, of course. |
---|
| 403 | ///\return <tt> (*this) </tt> |
---|
[1116] | 404 | Dijkstra &predMap(PredMap &m) |
---|
[688] | 405 | { |
---|
[1119] | 406 | if(local_pred) { |
---|
| 407 | delete _pred; |
---|
| 408 | local_pred=false; |
---|
[688] | 409 | } |
---|
[1119] | 410 | _pred = &m; |
---|
[688] | 411 | return *this; |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | ///Sets the map storing the predecessor nodes. |
---|
| 415 | |
---|
| 416 | ///Sets the map storing the predecessor nodes. |
---|
| 417 | ///If you don't use this function before calling \ref run(), |
---|
| 418 | ///it will allocate one. The destuctor deallocates this |
---|
| 419 | ///automatically allocated map, of course. |
---|
| 420 | ///\return <tt> (*this) </tt> |
---|
[1116] | 421 | Dijkstra &predNodeMap(PredNodeMap &m) |
---|
[688] | 422 | { |
---|
| 423 | if(local_pred_node) { |
---|
| 424 | delete pred_node; |
---|
| 425 | local_pred_node=false; |
---|
| 426 | } |
---|
| 427 | pred_node = &m; |
---|
| 428 | return *this; |
---|
| 429 | } |
---|
| 430 | |
---|
| 431 | ///Sets the map storing the distances calculated by the algorithm. |
---|
| 432 | |
---|
| 433 | ///Sets the map storing the distances calculated by the algorithm. |
---|
| 434 | ///If you don't use this function before calling \ref run(), |
---|
| 435 | ///it will allocate one. The destuctor deallocates this |
---|
| 436 | ///automatically allocated map, of course. |
---|
| 437 | ///\return <tt> (*this) </tt> |
---|
[1116] | 438 | Dijkstra &distMap(DistMap &m) |
---|
[688] | 439 | { |
---|
| 440 | if(local_distance) { |
---|
| 441 | delete distance; |
---|
| 442 | local_distance=false; |
---|
| 443 | } |
---|
| 444 | distance = &m; |
---|
| 445 | return *this; |
---|
| 446 | } |
---|
[694] | 447 | |
---|
[1128] | 448 | ///\name Excetution control |
---|
| 449 | ///The simplest way to execute the algorithm is to use |
---|
| 450 | ///\ref run(). |
---|
| 451 | ///\n |
---|
| 452 | ///It you need more control on the execution, |
---|
| 453 | ///first you must call \ref init(), then you can add several source nodes |
---|
| 454 | ///with \ref addSource(). Finally \ref start() will perform the actual path |
---|
| 455 | ///computation. |
---|
| 456 | |
---|
| 457 | ///@{ |
---|
| 458 | |
---|
| 459 | ///Initializes the internal data structures. |
---|
| 460 | |
---|
| 461 | ///Initializes the internal data structures. |
---|
| 462 | /// |
---|
| 463 | ///\todo _heap_map's type could also be in the traits class. |
---|
| 464 | void init() |
---|
| 465 | { |
---|
| 466 | create_maps(); |
---|
[774] | 467 | |
---|
| 468 | for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
---|
[1119] | 469 | _pred->set(u,INVALID); |
---|
[694] | 470 | pred_node->set(u,INVALID); |
---|
[1119] | 471 | ///\todo *_reached is not set to false. |
---|
[1128] | 472 | _heap_map.set(u,Heap::PRE_HEAP); |
---|
[694] | 473 | } |
---|
[1128] | 474 | } |
---|
| 475 | |
---|
| 476 | ///Adds a new source node. |
---|
| 477 | |
---|
| 478 | ///Adds a new source node the the priority heap. |
---|
| 479 | ///It checks if the node has already been added to the heap. |
---|
| 480 | /// |
---|
| 481 | ///The optional second parameter is the initial distance of the node. |
---|
| 482 | /// |
---|
| 483 | ///\todo Do we really want to check it? |
---|
| 484 | void addSource(Node s,Value dst=0) |
---|
| 485 | { |
---|
| 486 | source = s; |
---|
| 487 | if(_heap.state(s) != Heap::IN_HEAP) _heap.push(s,dst); |
---|
| 488 | } |
---|
| 489 | |
---|
| 490 | void processNode() |
---|
| 491 | { |
---|
| 492 | Node v=_heap.top(); |
---|
| 493 | _reached->set(v,true); |
---|
| 494 | Value oldvalue=_heap[v]; |
---|
| 495 | _heap.pop(); |
---|
| 496 | distance->set(v, oldvalue); |
---|
[694] | 497 | |
---|
[1128] | 498 | for(OutEdgeIt e(*G,v); e!=INVALID; ++e) { |
---|
| 499 | Node w=G->target(e); |
---|
| 500 | switch(_heap.state(w)) { |
---|
| 501 | case Heap::PRE_HEAP: |
---|
| 502 | _heap.push(w,oldvalue+(*length)[e]); |
---|
| 503 | _pred->set(w,e); |
---|
| 504 | pred_node->set(w,v); |
---|
| 505 | break; |
---|
| 506 | case Heap::IN_HEAP: |
---|
| 507 | if ( oldvalue+(*length)[e] < _heap[w] ) { |
---|
| 508 | _heap.decrease(w, oldvalue+(*length)[e]); |
---|
[1119] | 509 | _pred->set(w,e); |
---|
[694] | 510 | pred_node->set(w,v); |
---|
| 511 | } |
---|
[1128] | 512 | break; |
---|
| 513 | case Heap::POST_HEAP: |
---|
| 514 | break; |
---|
[694] | 515 | } |
---|
| 516 | } |
---|
| 517 | } |
---|
[1128] | 518 | |
---|
| 519 | ///Starts the execution of the algorithm. |
---|
| 520 | |
---|
| 521 | ///Starts the execution of the algorithm. |
---|
| 522 | /// |
---|
| 523 | ///\pre init() must be called before and at least one node should be added |
---|
| 524 | ///with addSource(). |
---|
| 525 | /// |
---|
| 526 | ///This method runs the %Dijkstra algorithm from the root node(s) |
---|
| 527 | ///in order to |
---|
| 528 | ///compute the |
---|
| 529 | ///shortest path to each node. The algorithm computes |
---|
| 530 | ///- The shortest path tree. |
---|
| 531 | ///- The distance of each node from the root(s). |
---|
| 532 | /// |
---|
| 533 | void start() |
---|
| 534 | { |
---|
| 535 | while ( !_heap.empty() ) processNode(); |
---|
| 536 | } |
---|
[255] | 537 | |
---|
[1128] | 538 | ///Starts the execution of the algorithm until \c dest is reached. |
---|
| 539 | |
---|
| 540 | ///Starts the execution of the algorithm until \c dest is reached. |
---|
| 541 | /// |
---|
| 542 | ///\pre init() must be called before and at least one node should be added |
---|
| 543 | ///with addSource(). |
---|
| 544 | /// |
---|
| 545 | ///This method runs the %Dijkstra algorithm from the root node(s) |
---|
| 546 | ///in order to |
---|
| 547 | ///compute the |
---|
| 548 | ///shortest path to \c dest. The algorithm computes |
---|
| 549 | ///- The shortest path to \c dest. |
---|
| 550 | ///- The distance of \c dest from the root(s). |
---|
| 551 | /// |
---|
| 552 | void start(Node dest) |
---|
| 553 | { |
---|
| 554 | while ( !_heap.empty() && _heap.top()!=dest) processNode(); |
---|
| 555 | } |
---|
| 556 | |
---|
| 557 | ///Runs %Dijkstra algorithm from node \c s. |
---|
| 558 | |
---|
| 559 | ///This method runs the %Dijkstra algorithm from a root node \c s |
---|
| 560 | ///in order to |
---|
| 561 | ///compute the |
---|
| 562 | ///shortest path to each node. The algorithm computes |
---|
| 563 | ///- The shortest path tree. |
---|
| 564 | ///- The distance of each node from the root. |
---|
| 565 | /// |
---|
| 566 | ///\note d.run(s) is just a shortcut of the following code. |
---|
| 567 | ///\code |
---|
| 568 | /// d.init(); |
---|
| 569 | /// d.addSource(s); |
---|
| 570 | /// d.start(); |
---|
| 571 | ///\endcode |
---|
| 572 | void run(Node s) { |
---|
| 573 | init(); |
---|
| 574 | addSource(s); |
---|
| 575 | start(); |
---|
| 576 | } |
---|
| 577 | |
---|
| 578 | ///@} |
---|
| 579 | |
---|
| 580 | ///\name Query Functions |
---|
| 581 | ///The result of the %Dijkstra algorithm can be obtained using these |
---|
| 582 | ///functions.\n |
---|
| 583 | ///Before the use of these functions, |
---|
| 584 | ///either run() or start() must be called. |
---|
| 585 | |
---|
| 586 | ///@{ |
---|
| 587 | |
---|
[385] | 588 | ///The distance of a node from the root. |
---|
[255] | 589 | |
---|
[385] | 590 | ///Returns the distance of a node from the root. |
---|
[255] | 591 | ///\pre \ref run() must be called before using this function. |
---|
[385] | 592 | ///\warning If node \c v in unreachable from the root the return value |
---|
[255] | 593 | ///of this funcion is undefined. |
---|
[987] | 594 | Value dist(Node v) const { return (*distance)[v]; } |
---|
[373] | 595 | |
---|
[584] | 596 | ///Returns the 'previous edge' of the shortest path tree. |
---|
[255] | 597 | |
---|
[584] | 598 | ///For a node \c v it returns the 'previous edge' of the shortest path tree, |
---|
[785] | 599 | ///i.e. it returns the last edge of a shortest path from the root to \c |
---|
[688] | 600 | ///v. It is \ref INVALID |
---|
| 601 | ///if \c v is unreachable from the root or if \c v=s. The |
---|
[385] | 602 | ///shortest path tree used here is equal to the shortest path tree used in |
---|
| 603 | ///\ref predNode(Node v). \pre \ref run() must be called before using |
---|
| 604 | ///this function. |
---|
[780] | 605 | ///\todo predEdge could be a better name. |
---|
[1119] | 606 | Edge pred(Node v) const { return (*_pred)[v]; } |
---|
[373] | 607 | |
---|
[584] | 608 | ///Returns the 'previous node' of the shortest path tree. |
---|
[255] | 609 | |
---|
[584] | 610 | ///For a node \c v it returns the 'previous node' of the shortest path tree, |
---|
[385] | 611 | ///i.e. it returns the last but one node from a shortest path from the |
---|
| 612 | ///root to \c /v. It is INVALID if \c v is unreachable from the root or if |
---|
| 613 | ///\c v=s. The shortest path tree used here is equal to the shortest path |
---|
| 614 | ///tree used in \ref pred(Node v). \pre \ref run() must be called before |
---|
| 615 | ///using this function. |
---|
[688] | 616 | Node predNode(Node v) const { return (*pred_node)[v]; } |
---|
[255] | 617 | |
---|
| 618 | ///Returns a reference to the NodeMap of distances. |
---|
| 619 | |
---|
[385] | 620 | ///Returns a reference to the NodeMap of distances. \pre \ref run() must |
---|
| 621 | ///be called before using this function. |
---|
[688] | 622 | const DistMap &distMap() const { return *distance;} |
---|
[385] | 623 | |
---|
[255] | 624 | ///Returns a reference to the shortest path tree map. |
---|
| 625 | |
---|
| 626 | ///Returns a reference to the NodeMap of the edges of the |
---|
| 627 | ///shortest path tree. |
---|
| 628 | ///\pre \ref run() must be called before using this function. |
---|
[1119] | 629 | const PredMap &predMap() const { return *_pred;} |
---|
[385] | 630 | |
---|
| 631 | ///Returns a reference to the map of nodes of shortest paths. |
---|
[255] | 632 | |
---|
| 633 | ///Returns a reference to the NodeMap of the last but one nodes of the |
---|
[385] | 634 | ///shortest path tree. |
---|
[255] | 635 | ///\pre \ref run() must be called before using this function. |
---|
[688] | 636 | const PredNodeMap &predNodeMap() const { return *pred_node;} |
---|
[255] | 637 | |
---|
[385] | 638 | ///Checks if a node is reachable from the root. |
---|
[255] | 639 | |
---|
[385] | 640 | ///Returns \c true if \c v is reachable from the root. |
---|
[1128] | 641 | ///\warning If the algorithm is started from multiple nodes, |
---|
| 642 | ///this function may give false result for the source nodes. |
---|
[255] | 643 | ///\pre \ref run() must be called before using this function. |
---|
[385] | 644 | /// |
---|
[1119] | 645 | bool reached(Node v) { return v==source || (*_pred)[v]!=INVALID; } |
---|
[255] | 646 | |
---|
[1128] | 647 | ///@} |
---|
[255] | 648 | }; |
---|
[953] | 649 | |
---|
[1123] | 650 | /// Default traits used by \ref DijkstraWizard |
---|
| 651 | |
---|
[1124] | 652 | /// To make it easier to use Dijkstra algorithm we have created a wizard class. |
---|
| 653 | /// This \ref DijkstraWizard class needs default traits, as well as the \ref Dijkstra class. |
---|
[1123] | 654 | /// The \ref DijkstraWizardBase is a class to be the default traits of the |
---|
| 655 | /// \ref DijkstraWizard class. |
---|
[1116] | 656 | template<class GR,class LM> |
---|
| 657 | class DijkstraWizardBase : public DijkstraDefaultTraits<GR,LM> |
---|
| 658 | { |
---|
| 659 | |
---|
| 660 | typedef DijkstraDefaultTraits<GR,LM> Base; |
---|
| 661 | protected: |
---|
| 662 | /// Pointer to the underlying graph. |
---|
| 663 | void *_g; |
---|
| 664 | /// Pointer to the length map |
---|
| 665 | void *_length; |
---|
| 666 | ///Pointer to the map of predecessors edges. |
---|
| 667 | void *_pred; |
---|
| 668 | ///Pointer to the map of predecessors nodes. |
---|
| 669 | void *_predNode; |
---|
| 670 | ///Pointer to the map of distances. |
---|
| 671 | void *_dist; |
---|
| 672 | ///Pointer to the source node. |
---|
| 673 | void *_source; |
---|
| 674 | |
---|
[1123] | 675 | /// Type of the nodes in the graph. |
---|
[1116] | 676 | typedef typename Base::Graph::Node Node; |
---|
| 677 | |
---|
| 678 | public: |
---|
[1123] | 679 | /// Constructor. |
---|
| 680 | |
---|
| 681 | /// This constructor does not require parameters, therefore it initiates |
---|
| 682 | /// all of the attributes to default values (0, INVALID). |
---|
[1116] | 683 | DijkstraWizardBase() : _g(0), _length(0), _pred(0), _predNode(0), |
---|
| 684 | _dist(0), _source(INVALID) {} |
---|
| 685 | |
---|
[1123] | 686 | /// Constructor. |
---|
| 687 | |
---|
| 688 | /// This constructor requires some parameters, listed in the parameters list. |
---|
| 689 | /// Others are initiated to 0. |
---|
| 690 | /// \param g is the initial value of \ref _g |
---|
| 691 | /// \param l is the initial value of \ref _length |
---|
| 692 | /// \param s is the initial value of \ref _source |
---|
[1116] | 693 | DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) : |
---|
| 694 | _g((void *)&g), _length((void *)&l), _pred(0), _predNode(0), |
---|
| 695 | _dist(0), _source((void *)&s) {} |
---|
| 696 | |
---|
| 697 | }; |
---|
| 698 | |
---|
[1123] | 699 | /// A class to make easier the usage of Dijkstra algorithm |
---|
[953] | 700 | |
---|
[1123] | 701 | /// This class is created to make it easier to use Dijkstra algorithm. |
---|
| 702 | /// It uses the functions and features of the plain \ref Dijkstra, |
---|
| 703 | /// but it is much more simple to use it. |
---|
[953] | 704 | /// |
---|
[1123] | 705 | /// Simplicity means that the way to change the types defined |
---|
| 706 | /// in the traits class is based on functions that returns the new class |
---|
[1124] | 707 | /// and not on templatable built-in classes. When using the plain \ref Dijkstra |
---|
| 708 | /// the new class with the modified type comes from the original class by using the :: |
---|
| 709 | /// operator. In the case of \ref DijkstraWizard only a function have to be called and it will |
---|
[1123] | 710 | /// return the needed class. |
---|
| 711 | /// |
---|
| 712 | /// It does not have own \ref run method. When its \ref run method is called |
---|
| 713 | /// it initiates a plain \ref Dijkstra class, and calls the \ref Dijkstra::run |
---|
| 714 | /// method of it. |
---|
[953] | 715 | template<class TR> |
---|
[1116] | 716 | class DijkstraWizard : public TR |
---|
[953] | 717 | { |
---|
[1116] | 718 | typedef TR Base; |
---|
[953] | 719 | |
---|
[1123] | 720 | ///The type of the underlying graph. |
---|
[953] | 721 | typedef typename TR::Graph Graph; |
---|
[1119] | 722 | //\e |
---|
[953] | 723 | typedef typename Graph::Node Node; |
---|
[1119] | 724 | //\e |
---|
[953] | 725 | typedef typename Graph::NodeIt NodeIt; |
---|
[1119] | 726 | //\e |
---|
[953] | 727 | typedef typename Graph::Edge Edge; |
---|
[1119] | 728 | //\e |
---|
[953] | 729 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
| 730 | |
---|
[1123] | 731 | ///The type of the map that stores the edge lengths. |
---|
[953] | 732 | typedef typename TR::LengthMap LengthMap; |
---|
[1123] | 733 | ///The type of the length of the edges. |
---|
[987] | 734 | typedef typename LengthMap::Value Value; |
---|
[1123] | 735 | ///\brief The type of the map that stores the last |
---|
| 736 | ///edges of the shortest paths. |
---|
[953] | 737 | typedef typename TR::PredMap PredMap; |
---|
[1123] | 738 | ///\brief The type of the map that stores the last but one |
---|
| 739 | ///nodes of the shortest paths. |
---|
[953] | 740 | typedef typename TR::PredNodeMap PredNodeMap; |
---|
[1123] | 741 | ///The type of the map that stores the dists of the nodes. |
---|
[953] | 742 | typedef typename TR::DistMap DistMap; |
---|
| 743 | |
---|
[1123] | 744 | ///The heap type used by the dijkstra algorithm. |
---|
[953] | 745 | typedef typename TR::Heap Heap; |
---|
[1116] | 746 | public: |
---|
[1123] | 747 | /// Constructor. |
---|
[1116] | 748 | DijkstraWizard() : TR() {} |
---|
[953] | 749 | |
---|
[1123] | 750 | /// Constructor that requires parameters. |
---|
[1124] | 751 | |
---|
| 752 | /// Constructor that requires parameters. |
---|
[1123] | 753 | /// These parameters will be the default values for the traits class. |
---|
[1116] | 754 | DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) : |
---|
| 755 | TR(g,l,s) {} |
---|
[953] | 756 | |
---|
[1123] | 757 | ///Copy constructor |
---|
[1116] | 758 | DijkstraWizard(const TR &b) : TR(b) {} |
---|
[953] | 759 | |
---|
[1116] | 760 | ~DijkstraWizard() {} |
---|
| 761 | |
---|
[1123] | 762 | ///Runs Dijkstra algorithm from a given node. |
---|
| 763 | |
---|
| 764 | ///Runs Dijkstra algorithm from a given node. |
---|
| 765 | ///The node can be given by the \ref source function. |
---|
[1116] | 766 | void run() |
---|
[953] | 767 | { |
---|
[1126] | 768 | if(_source==0) throw UninitializedParameter(); |
---|
[1116] | 769 | Dijkstra<Graph,LengthMap,TR> Dij(*(Graph*)_g,*(LengthMap*)_length); |
---|
| 770 | if(_pred) Dij.predMap(*(PredMap*)_pred); |
---|
| 771 | if(_predNode) Dij.predNodeMap(*(PredNodeMap*)_predNode); |
---|
| 772 | if(_dist) Dij.distMap(*(DistMap*)_dist); |
---|
| 773 | Dij.run(*(Node*)_source); |
---|
| 774 | } |
---|
| 775 | |
---|
[1124] | 776 | ///Runs Dijkstra algorithm from the given node. |
---|
[1123] | 777 | |
---|
[1124] | 778 | ///Runs Dijkstra algorithm from the given node. |
---|
[1123] | 779 | ///\param s is the given source. |
---|
[1116] | 780 | void run(Node s) |
---|
| 781 | { |
---|
| 782 | _source=(void *)&s; |
---|
| 783 | run(); |
---|
[953] | 784 | } |
---|
| 785 | |
---|
| 786 | template<class T> |
---|
[1116] | 787 | struct DefPredMapBase : public Base { |
---|
| 788 | typedef T PredMap; |
---|
[1117] | 789 | static PredMap *createPredMap(const Graph &G) { return 0; }; |
---|
| 790 | DefPredMapBase(const Base &b) : Base(b) {} |
---|
[1116] | 791 | }; |
---|
[953] | 792 | |
---|
[1123] | 793 | /// \ref named-templ-param "Named parameter" function for setting PredMap type |
---|
| 794 | |
---|
| 795 | /// \ref named-templ-param "Named parameter" function for setting PredMap type |
---|
[1124] | 796 | /// |
---|
[953] | 797 | template<class T> |
---|
[1116] | 798 | DijkstraWizard<DefPredMapBase<T> > predMap(const T &t) |
---|
[953] | 799 | { |
---|
[1116] | 800 | _pred=(void *)&t; |
---|
| 801 | return DijkstraWizard<DefPredMapBase<T> >(*this); |
---|
[953] | 802 | } |
---|
| 803 | |
---|
[1116] | 804 | |
---|
[953] | 805 | template<class T> |
---|
[1116] | 806 | struct DefPredNodeMapBase : public Base { |
---|
| 807 | typedef T PredNodeMap; |
---|
[1117] | 808 | static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; }; |
---|
| 809 | DefPredNodeMapBase(const Base &b) : Base(b) {} |
---|
[1116] | 810 | }; |
---|
| 811 | |
---|
[1123] | 812 | /// \ref named-templ-param "Named parameter" function for setting PredNodeMap type |
---|
| 813 | |
---|
| 814 | /// \ref named-templ-param "Named parameter" function for setting PredNodeMap type |
---|
[1124] | 815 | /// |
---|
[953] | 816 | template<class T> |
---|
[1116] | 817 | DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t) |
---|
[953] | 818 | { |
---|
[1116] | 819 | _predNode=(void *)&t; |
---|
| 820 | return DijkstraWizard<DefPredNodeMapBase<T> >(*this); |
---|
[953] | 821 | } |
---|
[1116] | 822 | |
---|
| 823 | template<class T> |
---|
| 824 | struct DefDistMapBase : public Base { |
---|
| 825 | typedef T DistMap; |
---|
[1117] | 826 | static DistMap *createDistMap(const Graph &G) { return 0; }; |
---|
| 827 | DefDistMapBase(const Base &b) : Base(b) {} |
---|
[1116] | 828 | }; |
---|
[953] | 829 | |
---|
[1123] | 830 | /// \ref named-templ-param "Named parameter" function for setting DistMap type |
---|
| 831 | |
---|
| 832 | /// \ref named-templ-param "Named parameter" function for setting DistMap type |
---|
[1124] | 833 | /// |
---|
[953] | 834 | template<class T> |
---|
[1116] | 835 | DijkstraWizard<DefDistMapBase<T> > distMap(const T &t) |
---|
[953] | 836 | { |
---|
[1116] | 837 | _dist=(void *)&t; |
---|
| 838 | return DijkstraWizard<DefDistMapBase<T> >(*this); |
---|
[953] | 839 | } |
---|
[1117] | 840 | |
---|
[1123] | 841 | /// Sets the source node, from which the Dijkstra algorithm runs. |
---|
| 842 | |
---|
| 843 | /// Sets the source node, from which the Dijkstra algorithm runs. |
---|
| 844 | /// \param s is the source node. |
---|
[1117] | 845 | DijkstraWizard<TR> &source(Node s) |
---|
[953] | 846 | { |
---|
[1116] | 847 | source=(void *)&s; |
---|
[953] | 848 | return *this; |
---|
| 849 | } |
---|
| 850 | |
---|
| 851 | }; |
---|
[255] | 852 | |
---|
[953] | 853 | ///\e |
---|
| 854 | |
---|
[954] | 855 | ///\todo Please document... |
---|
[953] | 856 | /// |
---|
| 857 | template<class GR, class LM> |
---|
[1116] | 858 | DijkstraWizard<DijkstraWizardBase<GR,LM> > |
---|
| 859 | dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID) |
---|
[953] | 860 | { |
---|
[1116] | 861 | return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s); |
---|
[953] | 862 | } |
---|
| 863 | |
---|
[430] | 864 | /// @} |
---|
[255] | 865 | |
---|
[921] | 866 | } //END OF NAMESPACE LEMON |
---|
[255] | 867 | |
---|
| 868 | #endif |
---|
| 869 | |
---|