[906] | 1 | /* -*- C++ -*- |
---|
[1435] | 2 | * lemon/dijkstra.h - Part of LEMON, a generic C++ optimization library |
---|
[906] | 3 | * |
---|
[1164] | 4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[906] | 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. |
---|
[1283] | 23 | /// |
---|
| 24 | ///\todo getPath() should be implemented! (also for BFS and DFS) |
---|
[255] | 25 | |
---|
[953] | 26 | #include <lemon/list_graph.h> |
---|
[921] | 27 | #include <lemon/bin_heap.h> |
---|
| 28 | #include <lemon/invalid.h> |
---|
[1119] | 29 | #include <lemon/error.h> |
---|
| 30 | #include <lemon/maps.h> |
---|
[255] | 31 | |
---|
[921] | 32 | namespace lemon { |
---|
[385] | 33 | |
---|
[1119] | 34 | |
---|
[1151] | 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 | } |
---|
[1218] | 81 | // ///\brief The type of the map that stores the last but one |
---|
| 82 | // ///nodes of the shortest paths. |
---|
| 83 | // /// |
---|
| 84 | // ///The type of the map that stores the last but one |
---|
| 85 | // ///nodes of the shortest paths. |
---|
| 86 | // ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
| 87 | // /// |
---|
| 88 | // typedef NullMap<typename Graph::Node,typename Graph::Node> PredNodeMap; |
---|
| 89 | // ///Instantiates a PredNodeMap. |
---|
[1125] | 90 | |
---|
[1218] | 91 | // ///This function instantiates a \ref PredNodeMap. |
---|
| 92 | // ///\param G is the graph, to which |
---|
| 93 | // ///we would like to define the \ref PredNodeMap |
---|
| 94 | // static PredNodeMap *createPredNodeMap(const GR &G) |
---|
| 95 | // { |
---|
| 96 | // return new PredNodeMap(); |
---|
| 97 | // } |
---|
[1119] | 98 | |
---|
[1218] | 99 | ///The type of the map that stores whether a nodes is processed. |
---|
[1119] | 100 | |
---|
[1218] | 101 | ///The type of the map that stores whether a nodes is processed. |
---|
[1119] | 102 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
| 103 | ///By default it is a NullMap. |
---|
[1218] | 104 | ///\todo If it is set to a real map, |
---|
| 105 | ///Dijkstra::processed() should read this. |
---|
[1119] | 106 | ///\todo named parameter to set this type, function to read and write. |
---|
[1218] | 107 | typedef NullMap<typename Graph::Node,bool> ProcessedMap; |
---|
| 108 | ///Instantiates a ProcessedMap. |
---|
[1119] | 109 | |
---|
[1218] | 110 | ///This function instantiates a \ref ProcessedMap. |
---|
[1536] | 111 | ///\param g is the graph, to which |
---|
[1218] | 112 | ///we would like to define the \ref ProcessedMap |
---|
[1536] | 113 | #ifdef DOXYGEN |
---|
| 114 | static ProcessedMap *createProcessedMap(const GR &g) |
---|
| 115 | #else |
---|
[1366] | 116 | static ProcessedMap *createProcessedMap(const GR &) |
---|
[1536] | 117 | #endif |
---|
[1119] | 118 | { |
---|
[1218] | 119 | return new ProcessedMap(); |
---|
[1119] | 120 | } |
---|
[953] | 121 | ///The type of the map that stores the dists of the nodes. |
---|
| 122 | |
---|
[1124] | 123 | ///The type of the map that stores the dists of the nodes. |
---|
[967] | 124 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
[953] | 125 | /// |
---|
[987] | 126 | typedef typename Graph::template NodeMap<typename LM::Value> DistMap; |
---|
[954] | 127 | ///Instantiates a DistMap. |
---|
[953] | 128 | |
---|
[1123] | 129 | ///This function instantiates a \ref DistMap. |
---|
| 130 | ///\param G is the graph, to which we would like to define the \ref DistMap |
---|
[954] | 131 | static DistMap *createDistMap(const GR &G) |
---|
[953] | 132 | { |
---|
| 133 | return new DistMap(G); |
---|
| 134 | } |
---|
| 135 | }; |
---|
| 136 | |
---|
[255] | 137 | ///%Dijkstra algorithm class. |
---|
[1125] | 138 | |
---|
[1151] | 139 | /// \ingroup flowalgs |
---|
[255] | 140 | ///This class provides an efficient implementation of %Dijkstra algorithm. |
---|
| 141 | ///The edge lengths are passed to the algorithm using a |
---|
[959] | 142 | ///\ref concept::ReadMap "ReadMap", |
---|
[255] | 143 | ///so it is easy to change it to any kind of length. |
---|
| 144 | /// |
---|
[880] | 145 | ///The type of the length is determined by the |
---|
[987] | 146 | ///\ref concept::ReadMap::Value "Value" of the length map. |
---|
[255] | 147 | /// |
---|
| 148 | ///It is also possible to change the underlying priority heap. |
---|
| 149 | /// |
---|
[1218] | 150 | ///\param GR The graph type the algorithm runs on. The default value |
---|
| 151 | ///is \ref ListGraph. The value of GR is not used directly by |
---|
| 152 | ///Dijkstra, it is only passed to \ref DijkstraDefaultTraits. |
---|
| 153 | ///\param LM This read-only EdgeMap determines the lengths of the |
---|
| 154 | ///edges. It is read once for each edge, so the map may involve in |
---|
| 155 | ///relatively time consuming process to compute the edge length if |
---|
| 156 | ///it is necessary. The default map type is \ref |
---|
| 157 | ///concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>". The value |
---|
| 158 | ///of LM is not used directly by Dijkstra, it is only passed to \ref |
---|
| 159 | ///DijkstraDefaultTraits. \param TR Traits class to set |
---|
| 160 | ///various data types used by the algorithm. The default traits |
---|
| 161 | ///class is \ref DijkstraDefaultTraits |
---|
| 162 | ///"DijkstraDefaultTraits<GR,LM>". See \ref |
---|
| 163 | ///DijkstraDefaultTraits for the documentation of a Dijkstra traits |
---|
| 164 | ///class. |
---|
[456] | 165 | /// |
---|
[689] | 166 | ///\author Jacint Szabo and Alpar Juttner |
---|
[1128] | 167 | ///\todo A compare object would be nice. |
---|
[584] | 168 | |
---|
[255] | 169 | #ifdef DOXYGEN |
---|
[584] | 170 | template <typename GR, |
---|
| 171 | typename LM, |
---|
[953] | 172 | typename TR> |
---|
[255] | 173 | #else |
---|
[953] | 174 | template <typename GR=ListGraph, |
---|
[584] | 175 | typename LM=typename GR::template EdgeMap<int>, |
---|
[953] | 176 | typename TR=DijkstraDefaultTraits<GR,LM> > |
---|
[255] | 177 | #endif |
---|
[1116] | 178 | class Dijkstra { |
---|
[255] | 179 | public: |
---|
[1125] | 180 | /** |
---|
| 181 | * \brief \ref Exception for uninitialized parameters. |
---|
| 182 | * |
---|
| 183 | * This error represents problems in the initialization |
---|
| 184 | * of the parameters of the algorithms. |
---|
| 185 | */ |
---|
| 186 | class UninitializedParameter : public lemon::UninitializedParameter { |
---|
| 187 | public: |
---|
| 188 | virtual const char* exceptionName() const { |
---|
[1218] | 189 | return "lemon::Dijkstra::UninitializedParameter"; |
---|
[1125] | 190 | } |
---|
| 191 | }; |
---|
[1119] | 192 | |
---|
[953] | 193 | typedef TR Traits; |
---|
[584] | 194 | ///The type of the underlying graph. |
---|
[954] | 195 | typedef typename TR::Graph Graph; |
---|
[911] | 196 | ///\e |
---|
[255] | 197 | typedef typename Graph::Node Node; |
---|
[911] | 198 | ///\e |
---|
[255] | 199 | typedef typename Graph::NodeIt NodeIt; |
---|
[911] | 200 | ///\e |
---|
[255] | 201 | typedef typename Graph::Edge Edge; |
---|
[911] | 202 | ///\e |
---|
[255] | 203 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
| 204 | |
---|
[584] | 205 | ///The type of the length of the edges. |
---|
[987] | 206 | typedef typename TR::LengthMap::Value Value; |
---|
[693] | 207 | ///The type of the map that stores the edge lengths. |
---|
[954] | 208 | typedef typename TR::LengthMap LengthMap; |
---|
[693] | 209 | ///\brief The type of the map that stores the last |
---|
[584] | 210 | ///edges of the shortest paths. |
---|
[953] | 211 | typedef typename TR::PredMap PredMap; |
---|
[1218] | 212 | // ///\brief The type of the map that stores the last but one |
---|
| 213 | // ///nodes of the shortest paths. |
---|
| 214 | // typedef typename TR::PredNodeMap PredNodeMap; |
---|
| 215 | ///The type of the map indicating if a node is processed. |
---|
| 216 | typedef typename TR::ProcessedMap ProcessedMap; |
---|
[693] | 217 | ///The type of the map that stores the dists of the nodes. |
---|
[953] | 218 | typedef typename TR::DistMap DistMap; |
---|
| 219 | ///The heap type used by the dijkstra algorithm. |
---|
| 220 | typedef typename TR::Heap Heap; |
---|
[255] | 221 | private: |
---|
[802] | 222 | /// Pointer to the underlying graph. |
---|
[688] | 223 | const Graph *G; |
---|
[802] | 224 | /// Pointer to the length map |
---|
[954] | 225 | const LengthMap *length; |
---|
[802] | 226 | ///Pointer to the map of predecessors edges. |
---|
[1119] | 227 | PredMap *_pred; |
---|
| 228 | ///Indicates if \ref _pred is locally allocated (\c true) or not. |
---|
| 229 | bool local_pred; |
---|
[1218] | 230 | // ///Pointer to the map of predecessors nodes. |
---|
| 231 | // PredNodeMap *_predNode; |
---|
| 232 | // ///Indicates if \ref _predNode is locally allocated (\c true) or not. |
---|
| 233 | // bool local_predNode; |
---|
[802] | 234 | ///Pointer to the map of distances. |
---|
[1130] | 235 | DistMap *_dist; |
---|
| 236 | ///Indicates if \ref _dist is locally allocated (\c true) or not. |
---|
| 237 | bool local_dist; |
---|
[1218] | 238 | ///Pointer to the map of processed status of the nodes. |
---|
| 239 | ProcessedMap *_processed; |
---|
| 240 | ///Indicates if \ref _processed is locally allocated (\c true) or not. |
---|
| 241 | bool local_processed; |
---|
[688] | 242 | |
---|
[1218] | 243 | // ///The source node of the last execution. |
---|
| 244 | // Node source; |
---|
[774] | 245 | |
---|
[1128] | 246 | ///Creates the maps if necessary. |
---|
[688] | 247 | |
---|
[694] | 248 | ///\todo Error if \c G or are \c NULL. What about \c length? |
---|
[688] | 249 | ///\todo Better memory allocation (instead of new). |
---|
[1128] | 250 | void create_maps() |
---|
[688] | 251 | { |
---|
[1119] | 252 | if(!_pred) { |
---|
| 253 | local_pred = true; |
---|
| 254 | _pred = Traits::createPredMap(*G); |
---|
[688] | 255 | } |
---|
[1218] | 256 | // if(!_predNode) { |
---|
| 257 | // local_predNode = true; |
---|
| 258 | // _predNode = Traits::createPredNodeMap(*G); |
---|
| 259 | // } |
---|
[1130] | 260 | if(!_dist) { |
---|
| 261 | local_dist = true; |
---|
| 262 | _dist = Traits::createDistMap(*G); |
---|
[688] | 263 | } |
---|
[1218] | 264 | if(!_processed) { |
---|
| 265 | local_processed = true; |
---|
| 266 | _processed = Traits::createProcessedMap(*G); |
---|
[1119] | 267 | } |
---|
[688] | 268 | } |
---|
[255] | 269 | |
---|
| 270 | public : |
---|
[1116] | 271 | |
---|
[1128] | 272 | ///\name Named template parameters |
---|
| 273 | |
---|
| 274 | ///@{ |
---|
| 275 | |
---|
[953] | 276 | template <class T> |
---|
[1116] | 277 | struct DefPredMapTraits : public Traits { |
---|
[953] | 278 | typedef T PredMap; |
---|
| 279 | static PredMap *createPredMap(const Graph &G) |
---|
| 280 | { |
---|
[1126] | 281 | throw UninitializedParameter(); |
---|
[953] | 282 | } |
---|
| 283 | }; |
---|
[954] | 284 | ///\ref named-templ-param "Named parameter" for setting PredMap type |
---|
| 285 | |
---|
| 286 | ///\ref named-templ-param "Named parameter" for setting PredMap type |
---|
[1043] | 287 | /// |
---|
[953] | 288 | template <class T> |
---|
[1116] | 289 | class DefPredMap : public Dijkstra< Graph, |
---|
[953] | 290 | LengthMap, |
---|
[1116] | 291 | DefPredMapTraits<T> > { }; |
---|
[953] | 292 | |
---|
[1218] | 293 | // template <class T> |
---|
| 294 | // struct DefPredNodeMapTraits : public Traits { |
---|
| 295 | // typedef T PredNodeMap; |
---|
| 296 | // static PredNodeMap *createPredNodeMap(const Graph &G) |
---|
| 297 | // { |
---|
| 298 | // throw UninitializedParameter(); |
---|
| 299 | // } |
---|
| 300 | // }; |
---|
| 301 | // ///\ref named-templ-param "Named parameter" for setting PredNodeMap type |
---|
[954] | 302 | |
---|
[1218] | 303 | // ///\ref named-templ-param "Named parameter" for setting PredNodeMap type |
---|
| 304 | // /// |
---|
| 305 | // template <class T> |
---|
| 306 | // class DefPredNodeMap : public Dijkstra< Graph, |
---|
| 307 | // LengthMap, |
---|
| 308 | // DefPredNodeMapTraits<T> > { }; |
---|
[953] | 309 | |
---|
| 310 | template <class T> |
---|
[1116] | 311 | struct DefDistMapTraits : public Traits { |
---|
[953] | 312 | typedef T DistMap; |
---|
| 313 | static DistMap *createDistMap(const Graph &G) |
---|
| 314 | { |
---|
[1126] | 315 | throw UninitializedParameter(); |
---|
[953] | 316 | } |
---|
| 317 | }; |
---|
[954] | 318 | ///\ref named-templ-param "Named parameter" for setting DistMap type |
---|
| 319 | |
---|
| 320 | ///\ref named-templ-param "Named parameter" for setting DistMap type |
---|
[1043] | 321 | /// |
---|
[953] | 322 | template <class T> |
---|
[1116] | 323 | class DefDistMap : public Dijkstra< Graph, |
---|
[953] | 324 | LengthMap, |
---|
[1116] | 325 | DefDistMapTraits<T> > { }; |
---|
[953] | 326 | |
---|
[1128] | 327 | template <class T> |
---|
[1218] | 328 | struct DefProcessedMapTraits : public Traits { |
---|
| 329 | typedef T ProcessedMap; |
---|
| 330 | static ProcessedMap *createProcessedMap(const Graph &G) |
---|
[1128] | 331 | { |
---|
| 332 | throw UninitializedParameter(); |
---|
| 333 | } |
---|
| 334 | }; |
---|
[1218] | 335 | ///\ref named-templ-param "Named parameter" for setting ProcessedMap type |
---|
[1128] | 336 | |
---|
[1218] | 337 | ///\ref named-templ-param "Named parameter" for setting ProcessedMap type |
---|
[1128] | 338 | /// |
---|
| 339 | template <class T> |
---|
[1218] | 340 | class DefProcessedMap : public Dijkstra< Graph, |
---|
[1128] | 341 | LengthMap, |
---|
[1218] | 342 | DefProcessedMapTraits<T> > { }; |
---|
[1128] | 343 | |
---|
[1218] | 344 | struct DefGraphProcessedMapTraits : public Traits { |
---|
| 345 | typedef typename Graph::template NodeMap<bool> ProcessedMap; |
---|
| 346 | static ProcessedMap *createProcessedMap(const Graph &G) |
---|
[1128] | 347 | { |
---|
[1218] | 348 | return new ProcessedMap(G); |
---|
[1128] | 349 | } |
---|
| 350 | }; |
---|
| 351 | ///\brief \ref named-templ-param "Named parameter" |
---|
[1218] | 352 | ///for setting the ProcessedMap type to be Graph::NodeMap<bool>. |
---|
[1128] | 353 | /// |
---|
| 354 | ///\ref named-templ-param "Named parameter" |
---|
[1218] | 355 | ///for setting the ProcessedMap type to be Graph::NodeMap<bool>. |
---|
[1128] | 356 | ///If you don't set it explicitely, it will be automatically allocated. |
---|
| 357 | template <class T> |
---|
[1218] | 358 | class DefProcessedMapToBeDefaultMap : |
---|
[1128] | 359 | public Dijkstra< Graph, |
---|
| 360 | LengthMap, |
---|
[1218] | 361 | DefGraphProcessedMapTraits> { }; |
---|
[1128] | 362 | |
---|
| 363 | ///@} |
---|
| 364 | |
---|
| 365 | |
---|
| 366 | private: |
---|
| 367 | typename Graph::template NodeMap<int> _heap_map; |
---|
| 368 | Heap _heap; |
---|
| 369 | public: |
---|
| 370 | |
---|
[802] | 371 | ///Constructor. |
---|
[255] | 372 | |
---|
[802] | 373 | ///\param _G the graph the algorithm will run on. |
---|
| 374 | ///\param _length the length map used by the algorithm. |
---|
[954] | 375 | Dijkstra(const Graph& _G, const LengthMap& _length) : |
---|
[688] | 376 | G(&_G), length(&_length), |
---|
[1119] | 377 | _pred(NULL), local_pred(false), |
---|
[1218] | 378 | // _predNode(NULL), local_predNode(false), |
---|
[1130] | 379 | _dist(NULL), local_dist(false), |
---|
[1218] | 380 | _processed(NULL), local_processed(false), |
---|
[1128] | 381 | _heap_map(*G,-1),_heap(_heap_map) |
---|
[688] | 382 | { } |
---|
| 383 | |
---|
[802] | 384 | ///Destructor. |
---|
[688] | 385 | ~Dijkstra() |
---|
| 386 | { |
---|
[1119] | 387 | if(local_pred) delete _pred; |
---|
[1218] | 388 | // if(local_predNode) delete _predNode; |
---|
[1130] | 389 | if(local_dist) delete _dist; |
---|
[1218] | 390 | if(local_processed) delete _processed; |
---|
[688] | 391 | } |
---|
| 392 | |
---|
| 393 | ///Sets the length map. |
---|
| 394 | |
---|
| 395 | ///Sets the length map. |
---|
| 396 | ///\return <tt> (*this) </tt> |
---|
[1116] | 397 | Dijkstra &lengthMap(const LengthMap &m) |
---|
[688] | 398 | { |
---|
| 399 | length = &m; |
---|
| 400 | return *this; |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | ///Sets the map storing the predecessor edges. |
---|
| 404 | |
---|
| 405 | ///Sets the map storing the predecessor edges. |
---|
| 406 | ///If you don't use this function before calling \ref run(), |
---|
| 407 | ///it will allocate one. The destuctor deallocates this |
---|
| 408 | ///automatically allocated map, of course. |
---|
| 409 | ///\return <tt> (*this) </tt> |
---|
[1116] | 410 | Dijkstra &predMap(PredMap &m) |
---|
[688] | 411 | { |
---|
[1119] | 412 | if(local_pred) { |
---|
| 413 | delete _pred; |
---|
| 414 | local_pred=false; |
---|
[688] | 415 | } |
---|
[1119] | 416 | _pred = &m; |
---|
[688] | 417 | return *this; |
---|
| 418 | } |
---|
| 419 | |
---|
[1218] | 420 | // ///Sets the map storing the predecessor nodes. |
---|
[688] | 421 | |
---|
[1218] | 422 | // ///Sets the map storing the predecessor nodes. |
---|
| 423 | // ///If you don't use this function before calling \ref run(), |
---|
| 424 | // ///it will allocate one. The destuctor deallocates this |
---|
| 425 | // ///automatically allocated map, of course. |
---|
| 426 | // ///\return <tt> (*this) </tt> |
---|
| 427 | // Dijkstra &predNodeMap(PredNodeMap &m) |
---|
| 428 | // { |
---|
| 429 | // if(local_predNode) { |
---|
| 430 | // delete _predNode; |
---|
| 431 | // local_predNode=false; |
---|
| 432 | // } |
---|
| 433 | // _predNode = &m; |
---|
| 434 | // return *this; |
---|
| 435 | // } |
---|
[688] | 436 | |
---|
| 437 | ///Sets the map storing the distances calculated by the algorithm. |
---|
| 438 | |
---|
| 439 | ///Sets the map storing the distances calculated by the algorithm. |
---|
| 440 | ///If you don't use this function before calling \ref run(), |
---|
| 441 | ///it will allocate one. The destuctor deallocates this |
---|
| 442 | ///automatically allocated map, of course. |
---|
| 443 | ///\return <tt> (*this) </tt> |
---|
[1116] | 444 | Dijkstra &distMap(DistMap &m) |
---|
[688] | 445 | { |
---|
[1130] | 446 | if(local_dist) { |
---|
| 447 | delete _dist; |
---|
| 448 | local_dist=false; |
---|
[688] | 449 | } |
---|
[1130] | 450 | _dist = &m; |
---|
[688] | 451 | return *this; |
---|
| 452 | } |
---|
[694] | 453 | |
---|
[1130] | 454 | private: |
---|
| 455 | void finalizeNodeData(Node v,Value dst) |
---|
| 456 | { |
---|
[1218] | 457 | _processed->set(v,true); |
---|
[1130] | 458 | _dist->set(v, dst); |
---|
[1218] | 459 | // if((*_pred)[v]!=INVALID) |
---|
| 460 | // _predNode->set(v,G->source((*_pred)[v])); ///\todo What to do? |
---|
[1130] | 461 | } |
---|
| 462 | |
---|
| 463 | public: |
---|
[1218] | 464 | ///\name Execution control |
---|
[1128] | 465 | ///The simplest way to execute the algorithm is to use |
---|
[1156] | 466 | ///one of the member functions called \c run(...). |
---|
[1128] | 467 | ///\n |
---|
[1218] | 468 | ///If you need more control on the execution, |
---|
[1128] | 469 | ///first you must call \ref init(), then you can add several source nodes |
---|
[1218] | 470 | ///with \ref addSource(). |
---|
| 471 | ///Finally \ref start() will perform the actual path |
---|
[1128] | 472 | ///computation. |
---|
| 473 | |
---|
| 474 | ///@{ |
---|
| 475 | |
---|
| 476 | ///Initializes the internal data structures. |
---|
| 477 | |
---|
| 478 | ///Initializes the internal data structures. |
---|
| 479 | /// |
---|
| 480 | ///\todo _heap_map's type could also be in the traits class. |
---|
[1229] | 481 | ///\todo The heaps should be able to make themselves empty directly. |
---|
[1128] | 482 | void init() |
---|
| 483 | { |
---|
| 484 | create_maps(); |
---|
[1229] | 485 | while(!_heap.empty()) _heap.pop(); |
---|
[774] | 486 | for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
---|
[1119] | 487 | _pred->set(u,INVALID); |
---|
[1218] | 488 | // _predNode->set(u,INVALID); |
---|
| 489 | _processed->set(u,false); |
---|
[1128] | 490 | _heap_map.set(u,Heap::PRE_HEAP); |
---|
[694] | 491 | } |
---|
[1128] | 492 | } |
---|
| 493 | |
---|
| 494 | ///Adds a new source node. |
---|
| 495 | |
---|
[1155] | 496 | ///Adds a new source node to the priority heap. |
---|
[1128] | 497 | /// |
---|
| 498 | ///The optional second parameter is the initial distance of the node. |
---|
| 499 | /// |
---|
[1155] | 500 | ///It checks if the node has already been added to the heap and |
---|
| 501 | ///It is pushed to the heap only if either it was not in the heap |
---|
| 502 | ///or the shortest path found till then is longer then \c dst. |
---|
[1128] | 503 | void addSource(Node s,Value dst=0) |
---|
| 504 | { |
---|
[1218] | 505 | // source = s; |
---|
[1128] | 506 | if(_heap.state(s) != Heap::IN_HEAP) _heap.push(s,dst); |
---|
[1155] | 507 | else if(_heap[s]<dst) { |
---|
| 508 | _heap.push(s,dst); |
---|
| 509 | _pred->set(s,INVALID); |
---|
| 510 | } |
---|
[1128] | 511 | } |
---|
| 512 | |
---|
[1155] | 513 | ///Processes the next node in the priority heap |
---|
| 514 | |
---|
| 515 | ///Processes the next node in the priority heap. |
---|
| 516 | /// |
---|
[1516] | 517 | ///\return The processed node. |
---|
| 518 | /// |
---|
[1155] | 519 | ///\warning The priority heap must not be empty! |
---|
[1516] | 520 | Node processNextNode() |
---|
[1128] | 521 | { |
---|
| 522 | Node v=_heap.top(); |
---|
| 523 | Value oldvalue=_heap[v]; |
---|
| 524 | _heap.pop(); |
---|
[1130] | 525 | finalizeNodeData(v,oldvalue); |
---|
[694] | 526 | |
---|
[1128] | 527 | for(OutEdgeIt e(*G,v); e!=INVALID; ++e) { |
---|
| 528 | Node w=G->target(e); |
---|
| 529 | switch(_heap.state(w)) { |
---|
| 530 | case Heap::PRE_HEAP: |
---|
| 531 | _heap.push(w,oldvalue+(*length)[e]); |
---|
| 532 | _pred->set(w,e); |
---|
[1130] | 533 | // _predNode->set(w,v); |
---|
[1128] | 534 | break; |
---|
| 535 | case Heap::IN_HEAP: |
---|
| 536 | if ( oldvalue+(*length)[e] < _heap[w] ) { |
---|
| 537 | _heap.decrease(w, oldvalue+(*length)[e]); |
---|
[1119] | 538 | _pred->set(w,e); |
---|
[1130] | 539 | // _predNode->set(w,v); |
---|
[694] | 540 | } |
---|
[1128] | 541 | break; |
---|
| 542 | case Heap::POST_HEAP: |
---|
| 543 | break; |
---|
[694] | 544 | } |
---|
| 545 | } |
---|
[1516] | 546 | return v; |
---|
[694] | 547 | } |
---|
[1128] | 548 | |
---|
[1218] | 549 | ///\brief Returns \c false if there are nodes |
---|
| 550 | ///to be processed in the priority heap |
---|
[1155] | 551 | /// |
---|
[1218] | 552 | ///Returns \c false if there are nodes |
---|
| 553 | ///to be processed in the priority heap |
---|
| 554 | bool emptyQueue() { return _heap.empty(); } |
---|
[1155] | 555 | ///Returns the number of the nodes to be processed in the priority heap |
---|
| 556 | |
---|
| 557 | ///Returns the number of the nodes to be processed in the priority heap |
---|
| 558 | /// |
---|
[1218] | 559 | int queueSize() { return _heap.size(); } |
---|
[1155] | 560 | |
---|
[1130] | 561 | ///Executes the algorithm. |
---|
[1128] | 562 | |
---|
[1130] | 563 | ///Executes the algorithm. |
---|
[1128] | 564 | /// |
---|
[1130] | 565 | ///\pre init() must be called and at least one node should be added |
---|
| 566 | ///with addSource() before using this function. |
---|
[1128] | 567 | /// |
---|
| 568 | ///This method runs the %Dijkstra algorithm from the root node(s) |
---|
| 569 | ///in order to |
---|
| 570 | ///compute the |
---|
| 571 | ///shortest path to each node. The algorithm computes |
---|
| 572 | ///- The shortest path tree. |
---|
| 573 | ///- The distance of each node from the root(s). |
---|
| 574 | /// |
---|
| 575 | void start() |
---|
| 576 | { |
---|
[1151] | 577 | while ( !_heap.empty() ) processNextNode(); |
---|
[1128] | 578 | } |
---|
[255] | 579 | |
---|
[1130] | 580 | ///Executes the algorithm until \c dest is reached. |
---|
[1128] | 581 | |
---|
[1130] | 582 | ///Executes the algorithm until \c dest is reached. |
---|
[1128] | 583 | /// |
---|
[1130] | 584 | ///\pre init() must be called and at least one node should be added |
---|
| 585 | ///with addSource() before using this function. |
---|
[1128] | 586 | /// |
---|
| 587 | ///This method runs the %Dijkstra algorithm from the root node(s) |
---|
| 588 | ///in order to |
---|
| 589 | ///compute the |
---|
| 590 | ///shortest path to \c dest. The algorithm computes |
---|
| 591 | ///- The shortest path to \c dest. |
---|
| 592 | ///- The distance of \c dest from the root(s). |
---|
| 593 | /// |
---|
| 594 | void start(Node dest) |
---|
| 595 | { |
---|
[1151] | 596 | while ( !_heap.empty() && _heap.top()!=dest ) processNextNode(); |
---|
[1229] | 597 | if ( !_heap.empty() ) finalizeNodeData(_heap.top(),_heap.prio()); |
---|
[1130] | 598 | } |
---|
| 599 | |
---|
| 600 | ///Executes the algorithm until a condition is met. |
---|
| 601 | |
---|
| 602 | ///Executes the algorithm until a condition is met. |
---|
| 603 | /// |
---|
| 604 | ///\pre init() must be called and at least one node should be added |
---|
| 605 | ///with addSource() before using this function. |
---|
| 606 | /// |
---|
| 607 | ///\param nm must be a bool (or convertible) node map. The algorithm |
---|
| 608 | ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>. |
---|
[1345] | 609 | template<class NodeBoolMap> |
---|
| 610 | void start(const NodeBoolMap &nm) |
---|
[1130] | 611 | { |
---|
[1193] | 612 | while ( !_heap.empty() && !nm[_heap.top()] ) processNextNode(); |
---|
[1229] | 613 | if ( !_heap.empty() ) finalizeNodeData(_heap.top(),_heap.prio()); |
---|
[1128] | 614 | } |
---|
| 615 | |
---|
| 616 | ///Runs %Dijkstra algorithm from node \c s. |
---|
| 617 | |
---|
| 618 | ///This method runs the %Dijkstra algorithm from a root node \c s |
---|
| 619 | ///in order to |
---|
| 620 | ///compute the |
---|
| 621 | ///shortest path to each node. The algorithm computes |
---|
| 622 | ///- The shortest path tree. |
---|
| 623 | ///- The distance of each node from the root. |
---|
| 624 | /// |
---|
| 625 | ///\note d.run(s) is just a shortcut of the following code. |
---|
| 626 | ///\code |
---|
| 627 | /// d.init(); |
---|
| 628 | /// d.addSource(s); |
---|
| 629 | /// d.start(); |
---|
| 630 | ///\endcode |
---|
| 631 | void run(Node s) { |
---|
| 632 | init(); |
---|
| 633 | addSource(s); |
---|
| 634 | start(); |
---|
| 635 | } |
---|
| 636 | |
---|
[1130] | 637 | ///Finds the shortest path between \c s and \c t. |
---|
| 638 | |
---|
| 639 | ///Finds the shortest path between \c s and \c t. |
---|
| 640 | /// |
---|
| 641 | ///\return The length of the shortest s---t path if there exists one, |
---|
| 642 | ///0 otherwise. |
---|
| 643 | ///\note Apart from the return value, d.run(s) is |
---|
| 644 | ///just a shortcut of the following code. |
---|
| 645 | ///\code |
---|
| 646 | /// d.init(); |
---|
| 647 | /// d.addSource(s); |
---|
| 648 | /// d.start(t); |
---|
| 649 | ///\endcode |
---|
| 650 | Value run(Node s,Node t) { |
---|
| 651 | init(); |
---|
| 652 | addSource(s); |
---|
| 653 | start(t); |
---|
| 654 | return (*_pred)[t]==INVALID?0:(*_dist)[t]; |
---|
| 655 | } |
---|
| 656 | |
---|
[1128] | 657 | ///@} |
---|
| 658 | |
---|
| 659 | ///\name Query Functions |
---|
| 660 | ///The result of the %Dijkstra algorithm can be obtained using these |
---|
| 661 | ///functions.\n |
---|
| 662 | ///Before the use of these functions, |
---|
| 663 | ///either run() or start() must be called. |
---|
| 664 | |
---|
| 665 | ///@{ |
---|
| 666 | |
---|
[1283] | 667 | ///Copies the shortest path to \c t into \c p |
---|
| 668 | |
---|
| 669 | ///This function copies the shortest path to \c t into \c p. |
---|
[1536] | 670 | ///If it \c t is a source itself or unreachable, then it does not |
---|
[1283] | 671 | ///alter \c p. |
---|
| 672 | ///\todo Is it the right way to handle unreachable nodes? |
---|
| 673 | ///\return Returns \c true if a path to \c t was actually copied to \c p, |
---|
| 674 | ///\c false otherwise. |
---|
| 675 | ///\sa DirPath |
---|
| 676 | template<class P> |
---|
| 677 | bool getPath(P &p,Node t) |
---|
| 678 | { |
---|
| 679 | if(reached(t)) { |
---|
| 680 | p.clear(); |
---|
| 681 | typename P::Builder b(p); |
---|
| 682 | for(b.setStartNode(t);pred(t)!=INVALID;t=predNode(t)) |
---|
| 683 | b.pushFront(pred(t)); |
---|
| 684 | b.commit(); |
---|
| 685 | return true; |
---|
| 686 | } |
---|
| 687 | return false; |
---|
| 688 | } |
---|
| 689 | |
---|
[385] | 690 | ///The distance of a node from the root. |
---|
[255] | 691 | |
---|
[385] | 692 | ///Returns the distance of a node from the root. |
---|
[255] | 693 | ///\pre \ref run() must be called before using this function. |
---|
[385] | 694 | ///\warning If node \c v in unreachable from the root the return value |
---|
[255] | 695 | ///of this funcion is undefined. |
---|
[1130] | 696 | Value dist(Node v) const { return (*_dist)[v]; } |
---|
[373] | 697 | |
---|
[584] | 698 | ///Returns the 'previous edge' of the shortest path tree. |
---|
[255] | 699 | |
---|
[584] | 700 | ///For a node \c v it returns the 'previous edge' of the shortest path tree, |
---|
[785] | 701 | ///i.e. it returns the last edge of a shortest path from the root to \c |
---|
[688] | 702 | ///v. It is \ref INVALID |
---|
| 703 | ///if \c v is unreachable from the root or if \c v=s. The |
---|
[385] | 704 | ///shortest path tree used here is equal to the shortest path tree used in |
---|
| 705 | ///\ref predNode(Node v). \pre \ref run() must be called before using |
---|
| 706 | ///this function. |
---|
[780] | 707 | ///\todo predEdge could be a better name. |
---|
[1119] | 708 | Edge pred(Node v) const { return (*_pred)[v]; } |
---|
[373] | 709 | |
---|
[584] | 710 | ///Returns the 'previous node' of the shortest path tree. |
---|
[255] | 711 | |
---|
[584] | 712 | ///For a node \c v it returns the 'previous node' of the shortest path tree, |
---|
[385] | 713 | ///i.e. it returns the last but one node from a shortest path from the |
---|
| 714 | ///root to \c /v. It is INVALID if \c v is unreachable from the root or if |
---|
| 715 | ///\c v=s. The shortest path tree used here is equal to the shortest path |
---|
| 716 | ///tree used in \ref pred(Node v). \pre \ref run() must be called before |
---|
| 717 | ///using this function. |
---|
[1130] | 718 | Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
---|
| 719 | G->source((*_pred)[v]); } |
---|
[255] | 720 | |
---|
| 721 | ///Returns a reference to the NodeMap of distances. |
---|
| 722 | |
---|
[385] | 723 | ///Returns a reference to the NodeMap of distances. \pre \ref run() must |
---|
| 724 | ///be called before using this function. |
---|
[1130] | 725 | const DistMap &distMap() const { return *_dist;} |
---|
[385] | 726 | |
---|
[255] | 727 | ///Returns a reference to the shortest path tree map. |
---|
| 728 | |
---|
| 729 | ///Returns a reference to the NodeMap of the edges of the |
---|
| 730 | ///shortest path tree. |
---|
| 731 | ///\pre \ref run() must be called before using this function. |
---|
[1119] | 732 | const PredMap &predMap() const { return *_pred;} |
---|
[385] | 733 | |
---|
[1218] | 734 | // ///Returns a reference to the map of nodes of shortest paths. |
---|
[255] | 735 | |
---|
[1218] | 736 | // ///Returns a reference to the NodeMap of the last but one nodes of the |
---|
| 737 | // ///shortest path tree. |
---|
| 738 | // ///\pre \ref run() must be called before using this function. |
---|
| 739 | // const PredNodeMap &predNodeMap() const { return *_predNode;} |
---|
[255] | 740 | |
---|
[385] | 741 | ///Checks if a node is reachable from the root. |
---|
[255] | 742 | |
---|
[385] | 743 | ///Returns \c true if \c v is reachable from the root. |
---|
[1218] | 744 | ///\warning The source nodes are inditated as unreached. |
---|
[255] | 745 | ///\pre \ref run() must be called before using this function. |
---|
[385] | 746 | /// |
---|
[1218] | 747 | bool reached(Node v) { return _heap_map[v]!=Heap::PRE_HEAP; } |
---|
[255] | 748 | |
---|
[1128] | 749 | ///@} |
---|
[255] | 750 | }; |
---|
[953] | 751 | |
---|
[1218] | 752 | |
---|
| 753 | |
---|
| 754 | |
---|
| 755 | |
---|
| 756 | ///Default traits class of Dijkstra function. |
---|
| 757 | |
---|
| 758 | ///Default traits class of Dijkstra function. |
---|
| 759 | ///\param GR Graph type. |
---|
| 760 | ///\param LM Type of length map. |
---|
| 761 | template<class GR, class LM> |
---|
| 762 | struct DijkstraWizardDefaultTraits |
---|
| 763 | { |
---|
| 764 | ///The graph type the algorithm runs on. |
---|
| 765 | typedef GR Graph; |
---|
| 766 | ///The type of the map that stores the edge lengths. |
---|
| 767 | |
---|
| 768 | ///The type of the map that stores the edge lengths. |
---|
| 769 | ///It must meet the \ref concept::ReadMap "ReadMap" concept. |
---|
| 770 | typedef LM LengthMap; |
---|
| 771 | //The type of the length of the edges. |
---|
| 772 | typedef typename LM::Value Value; |
---|
| 773 | ///The heap type used by Dijkstra algorithm. |
---|
| 774 | |
---|
| 775 | ///The heap type used by Dijkstra algorithm. |
---|
| 776 | /// |
---|
| 777 | ///\sa BinHeap |
---|
| 778 | ///\sa Dijkstra |
---|
| 779 | typedef BinHeap<typename Graph::Node, |
---|
| 780 | typename LM::Value, |
---|
| 781 | typename GR::template NodeMap<int>, |
---|
| 782 | std::less<Value> > Heap; |
---|
| 783 | |
---|
| 784 | ///\brief The type of the map that stores the last |
---|
| 785 | ///edges of the shortest paths. |
---|
| 786 | /// |
---|
| 787 | ///The type of the map that stores the last |
---|
| 788 | ///edges of the shortest paths. |
---|
| 789 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
| 790 | /// |
---|
| 791 | typedef NullMap <typename GR::Node,typename GR::Edge> PredMap; |
---|
| 792 | ///Instantiates a PredMap. |
---|
| 793 | |
---|
| 794 | ///This function instantiates a \ref PredMap. |
---|
[1536] | 795 | ///\param g is the graph, to which we would like to define the PredMap. |
---|
[1218] | 796 | ///\todo The graph alone may be insufficient for the initialization |
---|
[1536] | 797 | #ifdef DOXYGEN |
---|
| 798 | static PredMap *createPredMap(const GR &g) |
---|
| 799 | #else |
---|
[1367] | 800 | static PredMap *createPredMap(const GR &) |
---|
[1536] | 801 | #endif |
---|
[1218] | 802 | { |
---|
| 803 | return new PredMap(); |
---|
| 804 | } |
---|
| 805 | ///The type of the map that stores whether a nodes is processed. |
---|
| 806 | |
---|
| 807 | ///The type of the map that stores whether a nodes is processed. |
---|
| 808 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
| 809 | ///By default it is a NullMap. |
---|
| 810 | ///\todo If it is set to a real map, |
---|
| 811 | ///Dijkstra::processed() should read this. |
---|
| 812 | ///\todo named parameter to set this type, function to read and write. |
---|
| 813 | typedef NullMap<typename Graph::Node,bool> ProcessedMap; |
---|
| 814 | ///Instantiates a ProcessedMap. |
---|
| 815 | |
---|
| 816 | ///This function instantiates a \ref ProcessedMap. |
---|
[1536] | 817 | ///\param g is the graph, to which |
---|
[1218] | 818 | ///we would like to define the \ref ProcessedMap |
---|
[1536] | 819 | #ifdef DOXYGEN |
---|
| 820 | static ProcessedMap *createProcessedMap(const GR &g) |
---|
| 821 | #else |
---|
[1367] | 822 | static ProcessedMap *createProcessedMap(const GR &) |
---|
[1536] | 823 | #endif |
---|
[1218] | 824 | { |
---|
| 825 | return new ProcessedMap(); |
---|
| 826 | } |
---|
| 827 | ///The type of the map that stores the dists of the nodes. |
---|
| 828 | |
---|
| 829 | ///The type of the map that stores the dists of the nodes. |
---|
| 830 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
| 831 | /// |
---|
| 832 | typedef NullMap<typename Graph::Node,typename LM::Value> DistMap; |
---|
| 833 | ///Instantiates a DistMap. |
---|
| 834 | |
---|
| 835 | ///This function instantiates a \ref DistMap. |
---|
[1536] | 836 | ///\param g is the graph, to which we would like to define the \ref DistMap |
---|
| 837 | #ifdef DOXYGEN |
---|
| 838 | static DistMap *createDistMap(const GR &g) |
---|
| 839 | #else |
---|
[1367] | 840 | static DistMap *createDistMap(const GR &) |
---|
[1536] | 841 | #endif |
---|
[1218] | 842 | { |
---|
| 843 | return new DistMap(); |
---|
| 844 | } |
---|
| 845 | }; |
---|
| 846 | |
---|
[1123] | 847 | /// Default traits used by \ref DijkstraWizard |
---|
| 848 | |
---|
[1151] | 849 | /// To make it easier to use Dijkstra algorithm |
---|
| 850 | ///we have created a wizard class. |
---|
| 851 | /// This \ref DijkstraWizard class needs default traits, |
---|
| 852 | ///as well as the \ref Dijkstra class. |
---|
[1123] | 853 | /// The \ref DijkstraWizardBase is a class to be the default traits of the |
---|
| 854 | /// \ref DijkstraWizard class. |
---|
[1220] | 855 | /// \todo More named parameters are required... |
---|
[1116] | 856 | template<class GR,class LM> |
---|
[1218] | 857 | class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM> |
---|
[1116] | 858 | { |
---|
| 859 | |
---|
[1218] | 860 | typedef DijkstraWizardDefaultTraits<GR,LM> Base; |
---|
[1116] | 861 | protected: |
---|
[1201] | 862 | /// Type of the nodes in the graph. |
---|
| 863 | typedef typename Base::Graph::Node Node; |
---|
| 864 | |
---|
[1116] | 865 | /// Pointer to the underlying graph. |
---|
| 866 | void *_g; |
---|
| 867 | /// Pointer to the length map |
---|
| 868 | void *_length; |
---|
| 869 | ///Pointer to the map of predecessors edges. |
---|
| 870 | void *_pred; |
---|
[1218] | 871 | // ///Pointer to the map of predecessors nodes. |
---|
| 872 | // void *_predNode; |
---|
[1116] | 873 | ///Pointer to the map of distances. |
---|
| 874 | void *_dist; |
---|
| 875 | ///Pointer to the source node. |
---|
[1201] | 876 | Node _source; |
---|
[1116] | 877 | |
---|
| 878 | public: |
---|
[1123] | 879 | /// Constructor. |
---|
| 880 | |
---|
| 881 | /// This constructor does not require parameters, therefore it initiates |
---|
| 882 | /// all of the attributes to default values (0, INVALID). |
---|
[1218] | 883 | DijkstraWizardBase() : _g(0), _length(0), _pred(0), |
---|
| 884 | // _predNode(0), |
---|
| 885 | _dist(0), _source(INVALID) {} |
---|
[1116] | 886 | |
---|
[1123] | 887 | /// Constructor. |
---|
| 888 | |
---|
[1156] | 889 | /// This constructor requires some parameters, |
---|
| 890 | /// listed in the parameters list. |
---|
[1123] | 891 | /// Others are initiated to 0. |
---|
| 892 | /// \param g is the initial value of \ref _g |
---|
| 893 | /// \param l is the initial value of \ref _length |
---|
| 894 | /// \param s is the initial value of \ref _source |
---|
[1116] | 895 | DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) : |
---|
[1218] | 896 | _g((void *)&g), _length((void *)&l), _pred(0), |
---|
| 897 | // _predNode(0), |
---|
| 898 | _dist(0), _source(s) {} |
---|
[1116] | 899 | |
---|
| 900 | }; |
---|
| 901 | |
---|
[1229] | 902 | /// A class to make the usage of Dijkstra algorithm easier |
---|
[953] | 903 | |
---|
[1123] | 904 | /// This class is created to make it easier to use Dijkstra algorithm. |
---|
| 905 | /// It uses the functions and features of the plain \ref Dijkstra, |
---|
[1151] | 906 | /// but it is much simpler to use it. |
---|
[953] | 907 | /// |
---|
[1123] | 908 | /// Simplicity means that the way to change the types defined |
---|
| 909 | /// in the traits class is based on functions that returns the new class |
---|
[1151] | 910 | /// and not on templatable built-in classes. |
---|
| 911 | /// When using the plain \ref Dijkstra |
---|
| 912 | /// the new class with the modified type comes from |
---|
| 913 | /// the original class by using the :: |
---|
| 914 | /// operator. In the case of \ref DijkstraWizard only |
---|
| 915 | /// a function have to be called and it will |
---|
[1123] | 916 | /// return the needed class. |
---|
| 917 | /// |
---|
| 918 | /// It does not have own \ref run method. When its \ref run method is called |
---|
| 919 | /// it initiates a plain \ref Dijkstra class, and calls the \ref Dijkstra::run |
---|
| 920 | /// method of it. |
---|
[953] | 921 | template<class TR> |
---|
[1116] | 922 | class DijkstraWizard : public TR |
---|
[953] | 923 | { |
---|
[1116] | 924 | typedef TR Base; |
---|
[953] | 925 | |
---|
[1123] | 926 | ///The type of the underlying graph. |
---|
[953] | 927 | typedef typename TR::Graph Graph; |
---|
[1119] | 928 | //\e |
---|
[953] | 929 | typedef typename Graph::Node Node; |
---|
[1119] | 930 | //\e |
---|
[953] | 931 | typedef typename Graph::NodeIt NodeIt; |
---|
[1119] | 932 | //\e |
---|
[953] | 933 | typedef typename Graph::Edge Edge; |
---|
[1119] | 934 | //\e |
---|
[953] | 935 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
| 936 | |
---|
[1123] | 937 | ///The type of the map that stores the edge lengths. |
---|
[953] | 938 | typedef typename TR::LengthMap LengthMap; |
---|
[1123] | 939 | ///The type of the length of the edges. |
---|
[987] | 940 | typedef typename LengthMap::Value Value; |
---|
[1123] | 941 | ///\brief The type of the map that stores the last |
---|
| 942 | ///edges of the shortest paths. |
---|
[953] | 943 | typedef typename TR::PredMap PredMap; |
---|
[1218] | 944 | // ///\brief The type of the map that stores the last but one |
---|
| 945 | // ///nodes of the shortest paths. |
---|
| 946 | // typedef typename TR::PredNodeMap PredNodeMap; |
---|
[1123] | 947 | ///The type of the map that stores the dists of the nodes. |
---|
[953] | 948 | typedef typename TR::DistMap DistMap; |
---|
| 949 | |
---|
[1123] | 950 | ///The heap type used by the dijkstra algorithm. |
---|
[953] | 951 | typedef typename TR::Heap Heap; |
---|
[1116] | 952 | public: |
---|
[1123] | 953 | /// Constructor. |
---|
[1116] | 954 | DijkstraWizard() : TR() {} |
---|
[953] | 955 | |
---|
[1123] | 956 | /// Constructor that requires parameters. |
---|
[1124] | 957 | |
---|
| 958 | /// Constructor that requires parameters. |
---|
[1123] | 959 | /// These parameters will be the default values for the traits class. |
---|
[1116] | 960 | DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) : |
---|
| 961 | TR(g,l,s) {} |
---|
[953] | 962 | |
---|
[1123] | 963 | ///Copy constructor |
---|
[1116] | 964 | DijkstraWizard(const TR &b) : TR(b) {} |
---|
[953] | 965 | |
---|
[1116] | 966 | ~DijkstraWizard() {} |
---|
| 967 | |
---|
[1123] | 968 | ///Runs Dijkstra algorithm from a given node. |
---|
| 969 | |
---|
| 970 | ///Runs Dijkstra algorithm from a given node. |
---|
| 971 | ///The node can be given by the \ref source function. |
---|
[1116] | 972 | void run() |
---|
[953] | 973 | { |
---|
[1201] | 974 | if(Base::_source==INVALID) throw UninitializedParameter(); |
---|
[1193] | 975 | Dijkstra<Graph,LengthMap,TR> |
---|
[1345] | 976 | dij(*(Graph*)Base::_g,*(LengthMap*)Base::_length); |
---|
| 977 | if(Base::_pred) dij.predMap(*(PredMap*)Base::_pred); |
---|
[1218] | 978 | // if(Base::_predNode) Dij.predNodeMap(*(PredNodeMap*)Base::_predNode); |
---|
[1345] | 979 | if(Base::_dist) dij.distMap(*(DistMap*)Base::_dist); |
---|
| 980 | dij.run(Base::_source); |
---|
[1116] | 981 | } |
---|
| 982 | |
---|
[1124] | 983 | ///Runs Dijkstra algorithm from the given node. |
---|
[1123] | 984 | |
---|
[1124] | 985 | ///Runs Dijkstra algorithm from the given node. |
---|
[1123] | 986 | ///\param s is the given source. |
---|
[1116] | 987 | void run(Node s) |
---|
| 988 | { |
---|
[1201] | 989 | Base::_source=s; |
---|
[1116] | 990 | run(); |
---|
[953] | 991 | } |
---|
| 992 | |
---|
| 993 | template<class T> |
---|
[1116] | 994 | struct DefPredMapBase : public Base { |
---|
| 995 | typedef T PredMap; |
---|
[1367] | 996 | static PredMap *createPredMap(const Graph &) { return 0; }; |
---|
[1236] | 997 | DefPredMapBase(const TR &b) : TR(b) {} |
---|
[1116] | 998 | }; |
---|
[953] | 999 | |
---|
[1156] | 1000 | ///\brief \ref named-templ-param "Named parameter" |
---|
| 1001 | ///function for setting PredMap type |
---|
| 1002 | /// |
---|
| 1003 | /// \ref named-templ-param "Named parameter" |
---|
| 1004 | ///function for setting PredMap type |
---|
[1124] | 1005 | /// |
---|
[953] | 1006 | template<class T> |
---|
[1116] | 1007 | DijkstraWizard<DefPredMapBase<T> > predMap(const T &t) |
---|
[953] | 1008 | { |
---|
[1193] | 1009 | Base::_pred=(void *)&t; |
---|
[1116] | 1010 | return DijkstraWizard<DefPredMapBase<T> >(*this); |
---|
[953] | 1011 | } |
---|
| 1012 | |
---|
[1116] | 1013 | |
---|
[1218] | 1014 | // template<class T> |
---|
| 1015 | // struct DefPredNodeMapBase : public Base { |
---|
| 1016 | // typedef T PredNodeMap; |
---|
| 1017 | // static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; }; |
---|
[1236] | 1018 | // DefPredNodeMapBase(const TR &b) : TR(b) {} |
---|
[1218] | 1019 | // }; |
---|
[1116] | 1020 | |
---|
[1218] | 1021 | // ///\brief \ref named-templ-param "Named parameter" |
---|
| 1022 | // ///function for setting PredNodeMap type |
---|
| 1023 | // /// |
---|
| 1024 | // /// \ref named-templ-param "Named parameter" |
---|
| 1025 | // ///function for setting PredNodeMap type |
---|
| 1026 | // /// |
---|
| 1027 | // template<class T> |
---|
| 1028 | // DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t) |
---|
| 1029 | // { |
---|
| 1030 | // Base::_predNode=(void *)&t; |
---|
| 1031 | // return DijkstraWizard<DefPredNodeMapBase<T> >(*this); |
---|
| 1032 | // } |
---|
[1116] | 1033 | |
---|
| 1034 | template<class T> |
---|
| 1035 | struct DefDistMapBase : public Base { |
---|
| 1036 | typedef T DistMap; |
---|
[1367] | 1037 | static DistMap *createDistMap(const Graph &) { return 0; }; |
---|
[1236] | 1038 | DefDistMapBase(const TR &b) : TR(b) {} |
---|
[1116] | 1039 | }; |
---|
[953] | 1040 | |
---|
[1156] | 1041 | ///\brief \ref named-templ-param "Named parameter" |
---|
| 1042 | ///function for setting DistMap type |
---|
| 1043 | /// |
---|
| 1044 | /// \ref named-templ-param "Named parameter" |
---|
| 1045 | ///function for setting DistMap type |
---|
[1124] | 1046 | /// |
---|
[953] | 1047 | template<class T> |
---|
[1116] | 1048 | DijkstraWizard<DefDistMapBase<T> > distMap(const T &t) |
---|
[953] | 1049 | { |
---|
[1193] | 1050 | Base::_dist=(void *)&t; |
---|
[1116] | 1051 | return DijkstraWizard<DefDistMapBase<T> >(*this); |
---|
[953] | 1052 | } |
---|
[1117] | 1053 | |
---|
[1123] | 1054 | /// Sets the source node, from which the Dijkstra algorithm runs. |
---|
| 1055 | |
---|
| 1056 | /// Sets the source node, from which the Dijkstra algorithm runs. |
---|
| 1057 | /// \param s is the source node. |
---|
[1117] | 1058 | DijkstraWizard<TR> &source(Node s) |
---|
[953] | 1059 | { |
---|
[1201] | 1060 | Base::_source=s; |
---|
[953] | 1061 | return *this; |
---|
| 1062 | } |
---|
| 1063 | |
---|
| 1064 | }; |
---|
[255] | 1065 | |
---|
[1218] | 1066 | ///Function type interface for Dijkstra algorithm. |
---|
[953] | 1067 | |
---|
[1151] | 1068 | /// \ingroup flowalgs |
---|
[1218] | 1069 | ///Function type interface for Dijkstra algorithm. |
---|
[953] | 1070 | /// |
---|
[1218] | 1071 | ///This function also has several |
---|
| 1072 | ///\ref named-templ-func-param "named parameters", |
---|
| 1073 | ///they are declared as the members of class \ref DijkstraWizard. |
---|
| 1074 | ///The following |
---|
| 1075 | ///example shows how to use these parameters. |
---|
| 1076 | ///\code |
---|
| 1077 | /// dijkstra(g,length,source).predMap(preds).run(); |
---|
| 1078 | ///\endcode |
---|
| 1079 | ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()" |
---|
| 1080 | ///to the end of the parameter list. |
---|
| 1081 | ///\sa DijkstraWizard |
---|
| 1082 | ///\sa Dijkstra |
---|
[953] | 1083 | template<class GR, class LM> |
---|
[1116] | 1084 | DijkstraWizard<DijkstraWizardBase<GR,LM> > |
---|
| 1085 | dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID) |
---|
[953] | 1086 | { |
---|
[1116] | 1087 | return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s); |
---|
[953] | 1088 | } |
---|
| 1089 | |
---|
[921] | 1090 | } //END OF NAMESPACE LEMON |
---|
[255] | 1091 | |
---|
| 1092 | #endif |
---|
| 1093 | |
---|