[906] | 1 | /* -*- C++ -*- |
---|
[921] | 2 | * src/lemon/dijkstra.h - Part of LEMON, a generic C++ optimization library |
---|
[906] | 3 | * |
---|
[1164] | 4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[906] | 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 | |
---|
[1151] | 33 | |
---|
[954] | 34 | ///Default traits class of Dijkstra class. |
---|
| 35 | |
---|
| 36 | ///Default traits class of Dijkstra class. |
---|
| 37 | ///\param GR Graph type. |
---|
| 38 | ///\param LM Type of length map. |
---|
[953] | 39 | template<class GR, class LM> |
---|
| 40 | struct DijkstraDefaultTraits |
---|
| 41 | { |
---|
[954] | 42 | ///The graph type the algorithm runs on. |
---|
[953] | 43 | typedef GR Graph; |
---|
| 44 | ///The type of the map that stores the edge lengths. |
---|
| 45 | |
---|
[1124] | 46 | ///The type of the map that stores the edge lengths. |
---|
[967] | 47 | ///It must meet the \ref concept::ReadMap "ReadMap" concept. |
---|
[953] | 48 | typedef LM LengthMap; |
---|
[954] | 49 | //The type of the length of the edges. |
---|
[987] | 50 | typedef typename LM::Value Value; |
---|
[954] | 51 | ///The heap type used by Dijkstra algorithm. |
---|
[967] | 52 | |
---|
| 53 | ///The heap type used by Dijkstra algorithm. |
---|
| 54 | /// |
---|
| 55 | ///\sa BinHeap |
---|
| 56 | ///\sa Dijkstra |
---|
[953] | 57 | typedef BinHeap<typename Graph::Node, |
---|
[987] | 58 | typename LM::Value, |
---|
[953] | 59 | typename GR::template NodeMap<int>, |
---|
[987] | 60 | std::less<Value> > Heap; |
---|
[953] | 61 | |
---|
| 62 | ///\brief The type of the map that stores the last |
---|
| 63 | ///edges of the shortest paths. |
---|
| 64 | /// |
---|
[1124] | 65 | ///The type of the map that stores the last |
---|
| 66 | ///edges of the shortest paths. |
---|
[967] | 67 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
[953] | 68 | /// |
---|
[954] | 69 | typedef typename Graph::template NodeMap<typename GR::Edge> PredMap; |
---|
| 70 | ///Instantiates a PredMap. |
---|
[953] | 71 | |
---|
[1123] | 72 | ///This function instantiates a \ref PredMap. |
---|
| 73 | ///\param G is the graph, to which we would like to define the PredMap. |
---|
[1119] | 74 | ///\todo The graph alone may be insufficient for the initialization |
---|
[954] | 75 | static PredMap *createPredMap(const GR &G) |
---|
[953] | 76 | { |
---|
| 77 | return new PredMap(G); |
---|
| 78 | } |
---|
[1218] | 79 | // ///\brief The type of the map that stores the last but one |
---|
| 80 | // ///nodes of the shortest paths. |
---|
| 81 | // /// |
---|
| 82 | // ///The type of the map that stores the last but one |
---|
| 83 | // ///nodes of the shortest paths. |
---|
| 84 | // ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
| 85 | // /// |
---|
| 86 | // typedef NullMap<typename Graph::Node,typename Graph::Node> PredNodeMap; |
---|
| 87 | // ///Instantiates a PredNodeMap. |
---|
[1125] | 88 | |
---|
[1218] | 89 | // ///This function instantiates a \ref PredNodeMap. |
---|
| 90 | // ///\param G is the graph, to which |
---|
| 91 | // ///we would like to define the \ref PredNodeMap |
---|
| 92 | // static PredNodeMap *createPredNodeMap(const GR &G) |
---|
| 93 | // { |
---|
| 94 | // return new PredNodeMap(); |
---|
| 95 | // } |
---|
[1119] | 96 | |
---|
[1218] | 97 | ///The type of the map that stores whether a nodes is processed. |
---|
[1119] | 98 | |
---|
[1218] | 99 | ///The type of the map that stores whether a nodes is processed. |
---|
[1119] | 100 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
| 101 | ///By default it is a NullMap. |
---|
[1218] | 102 | ///\todo If it is set to a real map, |
---|
| 103 | ///Dijkstra::processed() should read this. |
---|
[1119] | 104 | ///\todo named parameter to set this type, function to read and write. |
---|
[1218] | 105 | typedef NullMap<typename Graph::Node,bool> ProcessedMap; |
---|
| 106 | ///Instantiates a ProcessedMap. |
---|
[1119] | 107 | |
---|
[1218] | 108 | ///This function instantiates a \ref ProcessedMap. |
---|
[1156] | 109 | ///\param G is the graph, to which |
---|
[1218] | 110 | ///we would like to define the \ref ProcessedMap |
---|
| 111 | static ProcessedMap *createProcessedMap(const GR &G) |
---|
[1119] | 112 | { |
---|
[1218] | 113 | return new ProcessedMap(); |
---|
[1119] | 114 | } |
---|
[953] | 115 | ///The type of the map that stores the dists of the nodes. |
---|
| 116 | |
---|
[1124] | 117 | ///The type of the map that stores the dists of the nodes. |
---|
[967] | 118 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
[953] | 119 | /// |
---|
[987] | 120 | typedef typename Graph::template NodeMap<typename LM::Value> DistMap; |
---|
[954] | 121 | ///Instantiates a DistMap. |
---|
[953] | 122 | |
---|
[1123] | 123 | ///This function instantiates a \ref DistMap. |
---|
| 124 | ///\param G is the graph, to which we would like to define the \ref DistMap |
---|
[954] | 125 | static DistMap *createDistMap(const GR &G) |
---|
[953] | 126 | { |
---|
| 127 | return new DistMap(G); |
---|
| 128 | } |
---|
| 129 | }; |
---|
| 130 | |
---|
[255] | 131 | ///%Dijkstra algorithm class. |
---|
[1125] | 132 | |
---|
[1151] | 133 | /// \ingroup flowalgs |
---|
[255] | 134 | ///This class provides an efficient implementation of %Dijkstra algorithm. |
---|
| 135 | ///The edge lengths are passed to the algorithm using a |
---|
[959] | 136 | ///\ref concept::ReadMap "ReadMap", |
---|
[255] | 137 | ///so it is easy to change it to any kind of length. |
---|
| 138 | /// |
---|
[880] | 139 | ///The type of the length is determined by the |
---|
[987] | 140 | ///\ref concept::ReadMap::Value "Value" of the length map. |
---|
[255] | 141 | /// |
---|
| 142 | ///It is also possible to change the underlying priority heap. |
---|
| 143 | /// |
---|
[1218] | 144 | ///\param GR The graph type the algorithm runs on. The default value |
---|
| 145 | ///is \ref ListGraph. The value of GR is not used directly by |
---|
| 146 | ///Dijkstra, it is only passed to \ref DijkstraDefaultTraits. |
---|
| 147 | ///\param LM This read-only EdgeMap determines the lengths of the |
---|
| 148 | ///edges. It is read once for each edge, so the map may involve in |
---|
| 149 | ///relatively time consuming process to compute the edge length if |
---|
| 150 | ///it is necessary. The default map type is \ref |
---|
| 151 | ///concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>". The value |
---|
| 152 | ///of LM is not used directly by Dijkstra, it is only passed to \ref |
---|
| 153 | ///DijkstraDefaultTraits. \param TR Traits class to set |
---|
| 154 | ///various data types used by the algorithm. The default traits |
---|
| 155 | ///class is \ref DijkstraDefaultTraits |
---|
| 156 | ///"DijkstraDefaultTraits<GR,LM>". See \ref |
---|
| 157 | ///DijkstraDefaultTraits for the documentation of a Dijkstra traits |
---|
| 158 | ///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 { |
---|
[1218] | 183 | return "lemon::Dijkstra::UninitializedParameter"; |
---|
[1125] | 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; |
---|
[1218] | 206 | // ///\brief The type of the map that stores the last but one |
---|
| 207 | // ///nodes of the shortest paths. |
---|
| 208 | // typedef typename TR::PredNodeMap PredNodeMap; |
---|
| 209 | ///The type of the map indicating if a node is processed. |
---|
| 210 | typedef typename TR::ProcessedMap ProcessedMap; |
---|
[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; |
---|
[1218] | 224 | // ///Pointer to the map of predecessors nodes. |
---|
| 225 | // PredNodeMap *_predNode; |
---|
| 226 | // ///Indicates if \ref _predNode is locally allocated (\c true) or not. |
---|
| 227 | // bool local_predNode; |
---|
[802] | 228 | ///Pointer to the map of distances. |
---|
[1130] | 229 | DistMap *_dist; |
---|
| 230 | ///Indicates if \ref _dist is locally allocated (\c true) or not. |
---|
| 231 | bool local_dist; |
---|
[1218] | 232 | ///Pointer to the map of processed status of the nodes. |
---|
| 233 | ProcessedMap *_processed; |
---|
| 234 | ///Indicates if \ref _processed is locally allocated (\c true) or not. |
---|
| 235 | bool local_processed; |
---|
[688] | 236 | |
---|
[1218] | 237 | // ///The source node of the last execution. |
---|
| 238 | // Node source; |
---|
[774] | 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 | } |
---|
[1218] | 250 | // if(!_predNode) { |
---|
| 251 | // local_predNode = true; |
---|
| 252 | // _predNode = Traits::createPredNodeMap(*G); |
---|
| 253 | // } |
---|
[1130] | 254 | if(!_dist) { |
---|
| 255 | local_dist = true; |
---|
| 256 | _dist = Traits::createDistMap(*G); |
---|
[688] | 257 | } |
---|
[1218] | 258 | if(!_processed) { |
---|
| 259 | local_processed = true; |
---|
| 260 | _processed = Traits::createProcessedMap(*G); |
---|
[1119] | 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 | |
---|
[1218] | 287 | // template <class T> |
---|
| 288 | // struct DefPredNodeMapTraits : public Traits { |
---|
| 289 | // typedef T PredNodeMap; |
---|
| 290 | // static PredNodeMap *createPredNodeMap(const Graph &G) |
---|
| 291 | // { |
---|
| 292 | // throw UninitializedParameter(); |
---|
| 293 | // } |
---|
| 294 | // }; |
---|
| 295 | // ///\ref named-templ-param "Named parameter" for setting PredNodeMap type |
---|
[954] | 296 | |
---|
[1218] | 297 | // ///\ref named-templ-param "Named parameter" for setting PredNodeMap type |
---|
| 298 | // /// |
---|
| 299 | // template <class T> |
---|
| 300 | // class DefPredNodeMap : public Dijkstra< Graph, |
---|
| 301 | // LengthMap, |
---|
| 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> |
---|
[1218] | 322 | struct DefProcessedMapTraits : public Traits { |
---|
| 323 | typedef T ProcessedMap; |
---|
| 324 | static ProcessedMap *createProcessedMap(const Graph &G) |
---|
[1128] | 325 | { |
---|
| 326 | throw UninitializedParameter(); |
---|
| 327 | } |
---|
| 328 | }; |
---|
[1218] | 329 | ///\ref named-templ-param "Named parameter" for setting ProcessedMap type |
---|
[1128] | 330 | |
---|
[1218] | 331 | ///\ref named-templ-param "Named parameter" for setting ProcessedMap type |
---|
[1128] | 332 | /// |
---|
| 333 | template <class T> |
---|
[1218] | 334 | class DefProcessedMap : public Dijkstra< Graph, |
---|
[1128] | 335 | LengthMap, |
---|
[1218] | 336 | DefProcessedMapTraits<T> > { }; |
---|
[1128] | 337 | |
---|
[1218] | 338 | struct DefGraphProcessedMapTraits : public Traits { |
---|
| 339 | typedef typename Graph::template NodeMap<bool> ProcessedMap; |
---|
| 340 | static ProcessedMap *createProcessedMap(const Graph &G) |
---|
[1128] | 341 | { |
---|
[1218] | 342 | return new ProcessedMap(G); |
---|
[1128] | 343 | } |
---|
| 344 | }; |
---|
| 345 | ///\brief \ref named-templ-param "Named parameter" |
---|
[1218] | 346 | ///for setting the ProcessedMap type to be Graph::NodeMap<bool>. |
---|
[1128] | 347 | /// |
---|
| 348 | ///\ref named-templ-param "Named parameter" |
---|
[1218] | 349 | ///for setting the ProcessedMap type to be Graph::NodeMap<bool>. |
---|
[1128] | 350 | ///If you don't set it explicitely, it will be automatically allocated. |
---|
| 351 | template <class T> |
---|
[1218] | 352 | class DefProcessedMapToBeDefaultMap : |
---|
[1128] | 353 | public Dijkstra< Graph, |
---|
| 354 | LengthMap, |
---|
[1218] | 355 | DefGraphProcessedMapTraits> { }; |
---|
[1128] | 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), |
---|
[1218] | 372 | // _predNode(NULL), local_predNode(false), |
---|
[1130] | 373 | _dist(NULL), local_dist(false), |
---|
[1218] | 374 | _processed(NULL), local_processed(false), |
---|
[1128] | 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; |
---|
[1218] | 382 | // if(local_predNode) delete _predNode; |
---|
[1130] | 383 | if(local_dist) delete _dist; |
---|
[1218] | 384 | if(local_processed) delete _processed; |
---|
[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 | |
---|
[1218] | 414 | // ///Sets the map storing the predecessor nodes. |
---|
[688] | 415 | |
---|
[1218] | 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> |
---|
| 421 | // Dijkstra &predNodeMap(PredNodeMap &m) |
---|
| 422 | // { |
---|
| 423 | // if(local_predNode) { |
---|
| 424 | // delete _predNode; |
---|
| 425 | // local_predNode=false; |
---|
| 426 | // } |
---|
| 427 | // _predNode = &m; |
---|
| 428 | // return *this; |
---|
| 429 | // } |
---|
[688] | 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 | { |
---|
[1130] | 440 | if(local_dist) { |
---|
| 441 | delete _dist; |
---|
| 442 | local_dist=false; |
---|
[688] | 443 | } |
---|
[1130] | 444 | _dist = &m; |
---|
[688] | 445 | return *this; |
---|
| 446 | } |
---|
[694] | 447 | |
---|
[1130] | 448 | private: |
---|
| 449 | void finalizeNodeData(Node v,Value dst) |
---|
| 450 | { |
---|
[1218] | 451 | _processed->set(v,true); |
---|
[1130] | 452 | _dist->set(v, dst); |
---|
[1218] | 453 | // if((*_pred)[v]!=INVALID) |
---|
| 454 | // _predNode->set(v,G->source((*_pred)[v])); ///\todo What to do? |
---|
[1130] | 455 | } |
---|
| 456 | |
---|
| 457 | public: |
---|
[1218] | 458 | ///\name Execution control |
---|
[1128] | 459 | ///The simplest way to execute the algorithm is to use |
---|
[1156] | 460 | ///one of the member functions called \c run(...). |
---|
[1128] | 461 | ///\n |
---|
[1218] | 462 | ///If you need more control on the execution, |
---|
[1128] | 463 | ///first you must call \ref init(), then you can add several source nodes |
---|
[1218] | 464 | ///with \ref addSource(). |
---|
| 465 | ///Finally \ref start() will perform the actual path |
---|
[1128] | 466 | ///computation. |
---|
| 467 | |
---|
| 468 | ///@{ |
---|
| 469 | |
---|
| 470 | ///Initializes the internal data structures. |
---|
| 471 | |
---|
| 472 | ///Initializes the internal data structures. |
---|
| 473 | /// |
---|
| 474 | ///\todo _heap_map's type could also be in the traits class. |
---|
[1229] | 475 | ///\todo The heaps should be able to make themselves empty directly. |
---|
[1128] | 476 | void init() |
---|
| 477 | { |
---|
| 478 | create_maps(); |
---|
[1229] | 479 | while(!_heap.empty()) _heap.pop(); |
---|
[774] | 480 | for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
---|
[1119] | 481 | _pred->set(u,INVALID); |
---|
[1218] | 482 | // _predNode->set(u,INVALID); |
---|
| 483 | _processed->set(u,false); |
---|
[1128] | 484 | _heap_map.set(u,Heap::PRE_HEAP); |
---|
[694] | 485 | } |
---|
[1128] | 486 | } |
---|
| 487 | |
---|
| 488 | ///Adds a new source node. |
---|
| 489 | |
---|
[1155] | 490 | ///Adds a new source node to the priority heap. |
---|
[1128] | 491 | /// |
---|
| 492 | ///The optional second parameter is the initial distance of the node. |
---|
| 493 | /// |
---|
[1155] | 494 | ///It checks if the node has already been added to the heap and |
---|
| 495 | ///It is pushed to the heap only if either it was not in the heap |
---|
| 496 | ///or the shortest path found till then is longer then \c dst. |
---|
[1128] | 497 | void addSource(Node s,Value dst=0) |
---|
| 498 | { |
---|
[1218] | 499 | // source = s; |
---|
[1128] | 500 | if(_heap.state(s) != Heap::IN_HEAP) _heap.push(s,dst); |
---|
[1155] | 501 | else if(_heap[s]<dst) { |
---|
| 502 | _heap.push(s,dst); |
---|
| 503 | _pred->set(s,INVALID); |
---|
| 504 | } |
---|
[1128] | 505 | } |
---|
| 506 | |
---|
[1155] | 507 | ///Processes the next node in the priority heap |
---|
| 508 | |
---|
| 509 | ///Processes the next node in the priority heap. |
---|
| 510 | /// |
---|
| 511 | ///\warning The priority heap must not be empty! |
---|
[1151] | 512 | void processNextNode() |
---|
[1128] | 513 | { |
---|
| 514 | Node v=_heap.top(); |
---|
| 515 | Value oldvalue=_heap[v]; |
---|
| 516 | _heap.pop(); |
---|
[1130] | 517 | finalizeNodeData(v,oldvalue); |
---|
[694] | 518 | |
---|
[1128] | 519 | for(OutEdgeIt e(*G,v); e!=INVALID; ++e) { |
---|
| 520 | Node w=G->target(e); |
---|
| 521 | switch(_heap.state(w)) { |
---|
| 522 | case Heap::PRE_HEAP: |
---|
| 523 | _heap.push(w,oldvalue+(*length)[e]); |
---|
| 524 | _pred->set(w,e); |
---|
[1130] | 525 | // _predNode->set(w,v); |
---|
[1128] | 526 | break; |
---|
| 527 | case Heap::IN_HEAP: |
---|
| 528 | if ( oldvalue+(*length)[e] < _heap[w] ) { |
---|
| 529 | _heap.decrease(w, oldvalue+(*length)[e]); |
---|
[1119] | 530 | _pred->set(w,e); |
---|
[1130] | 531 | // _predNode->set(w,v); |
---|
[694] | 532 | } |
---|
[1128] | 533 | break; |
---|
| 534 | case Heap::POST_HEAP: |
---|
| 535 | break; |
---|
[694] | 536 | } |
---|
| 537 | } |
---|
| 538 | } |
---|
[1128] | 539 | |
---|
[1218] | 540 | ///\brief Returns \c false if there are nodes |
---|
| 541 | ///to be processed in the priority heap |
---|
[1155] | 542 | /// |
---|
[1218] | 543 | ///Returns \c false if there are nodes |
---|
| 544 | ///to be processed in the priority heap |
---|
| 545 | bool emptyQueue() { return _heap.empty(); } |
---|
[1155] | 546 | ///Returns the number of the nodes to be processed in the priority heap |
---|
| 547 | |
---|
| 548 | ///Returns the number of the nodes to be processed in the priority heap |
---|
| 549 | /// |
---|
[1218] | 550 | int queueSize() { return _heap.size(); } |
---|
[1155] | 551 | |
---|
[1130] | 552 | ///Executes the algorithm. |
---|
[1128] | 553 | |
---|
[1130] | 554 | ///Executes the algorithm. |
---|
[1128] | 555 | /// |
---|
[1130] | 556 | ///\pre init() must be called and at least one node should be added |
---|
| 557 | ///with addSource() before using this function. |
---|
[1128] | 558 | /// |
---|
| 559 | ///This method runs the %Dijkstra algorithm from the root node(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(s). |
---|
| 565 | /// |
---|
| 566 | void start() |
---|
| 567 | { |
---|
[1151] | 568 | while ( !_heap.empty() ) processNextNode(); |
---|
[1128] | 569 | } |
---|
[255] | 570 | |
---|
[1130] | 571 | ///Executes the algorithm until \c dest is reached. |
---|
[1128] | 572 | |
---|
[1130] | 573 | ///Executes the algorithm until \c dest is reached. |
---|
[1128] | 574 | /// |
---|
[1130] | 575 | ///\pre init() must be called and at least one node should be added |
---|
| 576 | ///with addSource() before using this function. |
---|
[1128] | 577 | /// |
---|
| 578 | ///This method runs the %Dijkstra algorithm from the root node(s) |
---|
| 579 | ///in order to |
---|
| 580 | ///compute the |
---|
| 581 | ///shortest path to \c dest. The algorithm computes |
---|
| 582 | ///- The shortest path to \c dest. |
---|
| 583 | ///- The distance of \c dest from the root(s). |
---|
| 584 | /// |
---|
| 585 | void start(Node dest) |
---|
| 586 | { |
---|
[1151] | 587 | while ( !_heap.empty() && _heap.top()!=dest ) processNextNode(); |
---|
[1229] | 588 | if ( !_heap.empty() ) finalizeNodeData(_heap.top(),_heap.prio()); |
---|
[1130] | 589 | } |
---|
| 590 | |
---|
| 591 | ///Executes the algorithm until a condition is met. |
---|
| 592 | |
---|
| 593 | ///Executes the algorithm until a condition is met. |
---|
| 594 | /// |
---|
| 595 | ///\pre init() must be called and at least one node should be added |
---|
| 596 | ///with addSource() before using this function. |
---|
| 597 | /// |
---|
| 598 | ///\param nm must be a bool (or convertible) node map. The algorithm |
---|
| 599 | ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>. |
---|
| 600 | template<class NM> |
---|
| 601 | void start(const NM &nm) |
---|
| 602 | { |
---|
[1193] | 603 | while ( !_heap.empty() && !nm[_heap.top()] ) processNextNode(); |
---|
[1229] | 604 | if ( !_heap.empty() ) finalizeNodeData(_heap.top(),_heap.prio()); |
---|
[1128] | 605 | } |
---|
| 606 | |
---|
| 607 | ///Runs %Dijkstra algorithm from node \c s. |
---|
| 608 | |
---|
| 609 | ///This method runs the %Dijkstra algorithm from a root node \c s |
---|
| 610 | ///in order to |
---|
| 611 | ///compute the |
---|
| 612 | ///shortest path to each node. The algorithm computes |
---|
| 613 | ///- The shortest path tree. |
---|
| 614 | ///- The distance of each node from the root. |
---|
| 615 | /// |
---|
| 616 | ///\note d.run(s) is just a shortcut of the following code. |
---|
| 617 | ///\code |
---|
| 618 | /// d.init(); |
---|
| 619 | /// d.addSource(s); |
---|
| 620 | /// d.start(); |
---|
| 621 | ///\endcode |
---|
| 622 | void run(Node s) { |
---|
| 623 | init(); |
---|
| 624 | addSource(s); |
---|
| 625 | start(); |
---|
| 626 | } |
---|
| 627 | |
---|
[1130] | 628 | ///Finds the shortest path between \c s and \c t. |
---|
| 629 | |
---|
| 630 | ///Finds the shortest path between \c s and \c t. |
---|
| 631 | /// |
---|
| 632 | ///\return The length of the shortest s---t path if there exists one, |
---|
| 633 | ///0 otherwise. |
---|
| 634 | ///\note Apart from the return value, d.run(s) is |
---|
| 635 | ///just a shortcut of the following code. |
---|
| 636 | ///\code |
---|
| 637 | /// d.init(); |
---|
| 638 | /// d.addSource(s); |
---|
| 639 | /// d.start(t); |
---|
| 640 | ///\endcode |
---|
| 641 | Value run(Node s,Node t) { |
---|
| 642 | init(); |
---|
| 643 | addSource(s); |
---|
| 644 | start(t); |
---|
| 645 | return (*_pred)[t]==INVALID?0:(*_dist)[t]; |
---|
| 646 | } |
---|
| 647 | |
---|
[1128] | 648 | ///@} |
---|
| 649 | |
---|
| 650 | ///\name Query Functions |
---|
| 651 | ///The result of the %Dijkstra algorithm can be obtained using these |
---|
| 652 | ///functions.\n |
---|
| 653 | ///Before the use of these functions, |
---|
| 654 | ///either run() or start() must be called. |
---|
| 655 | |
---|
| 656 | ///@{ |
---|
| 657 | |
---|
[385] | 658 | ///The distance of a node from the root. |
---|
[255] | 659 | |
---|
[385] | 660 | ///Returns the distance of a node from the root. |
---|
[255] | 661 | ///\pre \ref run() must be called before using this function. |
---|
[385] | 662 | ///\warning If node \c v in unreachable from the root the return value |
---|
[255] | 663 | ///of this funcion is undefined. |
---|
[1130] | 664 | Value dist(Node v) const { return (*_dist)[v]; } |
---|
[373] | 665 | |
---|
[584] | 666 | ///Returns the 'previous edge' of the shortest path tree. |
---|
[255] | 667 | |
---|
[584] | 668 | ///For a node \c v it returns the 'previous edge' of the shortest path tree, |
---|
[785] | 669 | ///i.e. it returns the last edge of a shortest path from the root to \c |
---|
[688] | 670 | ///v. It is \ref INVALID |
---|
| 671 | ///if \c v is unreachable from the root or if \c v=s. The |
---|
[385] | 672 | ///shortest path tree used here is equal to the shortest path tree used in |
---|
| 673 | ///\ref predNode(Node v). \pre \ref run() must be called before using |
---|
| 674 | ///this function. |
---|
[780] | 675 | ///\todo predEdge could be a better name. |
---|
[1119] | 676 | Edge pred(Node v) const { return (*_pred)[v]; } |
---|
[373] | 677 | |
---|
[584] | 678 | ///Returns the 'previous node' of the shortest path tree. |
---|
[255] | 679 | |
---|
[584] | 680 | ///For a node \c v it returns the 'previous node' of the shortest path tree, |
---|
[385] | 681 | ///i.e. it returns the last but one node from a shortest path from the |
---|
| 682 | ///root to \c /v. It is INVALID if \c v is unreachable from the root or if |
---|
| 683 | ///\c v=s. The shortest path tree used here is equal to the shortest path |
---|
| 684 | ///tree used in \ref pred(Node v). \pre \ref run() must be called before |
---|
| 685 | ///using this function. |
---|
[1130] | 686 | Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
---|
| 687 | G->source((*_pred)[v]); } |
---|
[255] | 688 | |
---|
| 689 | ///Returns a reference to the NodeMap of distances. |
---|
| 690 | |
---|
[385] | 691 | ///Returns a reference to the NodeMap of distances. \pre \ref run() must |
---|
| 692 | ///be called before using this function. |
---|
[1130] | 693 | const DistMap &distMap() const { return *_dist;} |
---|
[385] | 694 | |
---|
[255] | 695 | ///Returns a reference to the shortest path tree map. |
---|
| 696 | |
---|
| 697 | ///Returns a reference to the NodeMap of the edges of the |
---|
| 698 | ///shortest path tree. |
---|
| 699 | ///\pre \ref run() must be called before using this function. |
---|
[1119] | 700 | const PredMap &predMap() const { return *_pred;} |
---|
[385] | 701 | |
---|
[1218] | 702 | // ///Returns a reference to the map of nodes of shortest paths. |
---|
[255] | 703 | |
---|
[1218] | 704 | // ///Returns a reference to the NodeMap of the last but one nodes of the |
---|
| 705 | // ///shortest path tree. |
---|
| 706 | // ///\pre \ref run() must be called before using this function. |
---|
| 707 | // const PredNodeMap &predNodeMap() const { return *_predNode;} |
---|
[255] | 708 | |
---|
[385] | 709 | ///Checks if a node is reachable from the root. |
---|
[255] | 710 | |
---|
[385] | 711 | ///Returns \c true if \c v is reachable from the root. |
---|
[1218] | 712 | ///\warning The source nodes are inditated as unreached. |
---|
[255] | 713 | ///\pre \ref run() must be called before using this function. |
---|
[385] | 714 | /// |
---|
[1218] | 715 | bool reached(Node v) { return _heap_map[v]!=Heap::PRE_HEAP; } |
---|
[255] | 716 | |
---|
[1128] | 717 | ///@} |
---|
[255] | 718 | }; |
---|
[953] | 719 | |
---|
[1218] | 720 | |
---|
| 721 | |
---|
| 722 | |
---|
| 723 | |
---|
| 724 | ///Default traits class of Dijkstra function. |
---|
| 725 | |
---|
| 726 | ///Default traits class of Dijkstra function. |
---|
| 727 | ///\param GR Graph type. |
---|
| 728 | ///\param LM Type of length map. |
---|
| 729 | template<class GR, class LM> |
---|
| 730 | struct DijkstraWizardDefaultTraits |
---|
| 731 | { |
---|
| 732 | ///The graph type the algorithm runs on. |
---|
| 733 | typedef GR Graph; |
---|
| 734 | ///The type of the map that stores the edge lengths. |
---|
| 735 | |
---|
| 736 | ///The type of the map that stores the edge lengths. |
---|
| 737 | ///It must meet the \ref concept::ReadMap "ReadMap" concept. |
---|
| 738 | typedef LM LengthMap; |
---|
| 739 | //The type of the length of the edges. |
---|
| 740 | typedef typename LM::Value Value; |
---|
| 741 | ///The heap type used by Dijkstra algorithm. |
---|
| 742 | |
---|
| 743 | ///The heap type used by Dijkstra algorithm. |
---|
| 744 | /// |
---|
| 745 | ///\sa BinHeap |
---|
| 746 | ///\sa Dijkstra |
---|
| 747 | typedef BinHeap<typename Graph::Node, |
---|
| 748 | typename LM::Value, |
---|
| 749 | typename GR::template NodeMap<int>, |
---|
| 750 | std::less<Value> > Heap; |
---|
| 751 | |
---|
| 752 | ///\brief The type of the map that stores the last |
---|
| 753 | ///edges of the shortest paths. |
---|
| 754 | /// |
---|
| 755 | ///The type of the map that stores the last |
---|
| 756 | ///edges of the shortest paths. |
---|
| 757 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
| 758 | /// |
---|
| 759 | typedef NullMap <typename GR::Node,typename GR::Edge> PredMap; |
---|
| 760 | ///Instantiates a PredMap. |
---|
| 761 | |
---|
| 762 | ///This function instantiates a \ref PredMap. |
---|
| 763 | ///\param G is the graph, to which we would like to define the PredMap. |
---|
| 764 | ///\todo The graph alone may be insufficient for the initialization |
---|
| 765 | static PredMap *createPredMap(const GR &G) |
---|
| 766 | { |
---|
| 767 | return new PredMap(); |
---|
| 768 | } |
---|
| 769 | ///The type of the map that stores whether a nodes is processed. |
---|
| 770 | |
---|
| 771 | ///The type of the map that stores whether a nodes is processed. |
---|
| 772 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
| 773 | ///By default it is a NullMap. |
---|
| 774 | ///\todo If it is set to a real map, |
---|
| 775 | ///Dijkstra::processed() should read this. |
---|
| 776 | ///\todo named parameter to set this type, function to read and write. |
---|
| 777 | typedef NullMap<typename Graph::Node,bool> ProcessedMap; |
---|
| 778 | ///Instantiates a ProcessedMap. |
---|
| 779 | |
---|
| 780 | ///This function instantiates a \ref ProcessedMap. |
---|
| 781 | ///\param G is the graph, to which |
---|
| 782 | ///we would like to define the \ref ProcessedMap |
---|
| 783 | static ProcessedMap *createProcessedMap(const GR &G) |
---|
| 784 | { |
---|
| 785 | return new ProcessedMap(); |
---|
| 786 | } |
---|
| 787 | ///The type of the map that stores the dists of the nodes. |
---|
| 788 | |
---|
| 789 | ///The type of the map that stores the dists of the nodes. |
---|
| 790 | ///It must meet the \ref concept::WriteMap "WriteMap" concept. |
---|
| 791 | /// |
---|
| 792 | typedef NullMap<typename Graph::Node,typename LM::Value> DistMap; |
---|
| 793 | ///Instantiates a DistMap. |
---|
| 794 | |
---|
| 795 | ///This function instantiates a \ref DistMap. |
---|
| 796 | ///\param G is the graph, to which we would like to define the \ref DistMap |
---|
| 797 | static DistMap *createDistMap(const GR &G) |
---|
| 798 | { |
---|
| 799 | return new DistMap(); |
---|
| 800 | } |
---|
| 801 | }; |
---|
| 802 | |
---|
[1123] | 803 | /// Default traits used by \ref DijkstraWizard |
---|
| 804 | |
---|
[1151] | 805 | /// To make it easier to use Dijkstra algorithm |
---|
| 806 | ///we have created a wizard class. |
---|
| 807 | /// This \ref DijkstraWizard class needs default traits, |
---|
| 808 | ///as well as the \ref Dijkstra class. |
---|
[1123] | 809 | /// The \ref DijkstraWizardBase is a class to be the default traits of the |
---|
| 810 | /// \ref DijkstraWizard class. |
---|
[1220] | 811 | /// \todo More named parameters are required... |
---|
[1116] | 812 | template<class GR,class LM> |
---|
[1218] | 813 | class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM> |
---|
[1116] | 814 | { |
---|
| 815 | |
---|
[1218] | 816 | typedef DijkstraWizardDefaultTraits<GR,LM> Base; |
---|
[1116] | 817 | protected: |
---|
[1201] | 818 | /// Type of the nodes in the graph. |
---|
| 819 | typedef typename Base::Graph::Node Node; |
---|
| 820 | |
---|
[1116] | 821 | /// Pointer to the underlying graph. |
---|
| 822 | void *_g; |
---|
| 823 | /// Pointer to the length map |
---|
| 824 | void *_length; |
---|
| 825 | ///Pointer to the map of predecessors edges. |
---|
| 826 | void *_pred; |
---|
[1218] | 827 | // ///Pointer to the map of predecessors nodes. |
---|
| 828 | // void *_predNode; |
---|
[1116] | 829 | ///Pointer to the map of distances. |
---|
| 830 | void *_dist; |
---|
| 831 | ///Pointer to the source node. |
---|
[1201] | 832 | Node _source; |
---|
[1116] | 833 | |
---|
| 834 | public: |
---|
[1123] | 835 | /// Constructor. |
---|
| 836 | |
---|
| 837 | /// This constructor does not require parameters, therefore it initiates |
---|
| 838 | /// all of the attributes to default values (0, INVALID). |
---|
[1218] | 839 | DijkstraWizardBase() : _g(0), _length(0), _pred(0), |
---|
| 840 | // _predNode(0), |
---|
| 841 | _dist(0), _source(INVALID) {} |
---|
[1116] | 842 | |
---|
[1123] | 843 | /// Constructor. |
---|
| 844 | |
---|
[1156] | 845 | /// This constructor requires some parameters, |
---|
| 846 | /// listed in the parameters list. |
---|
[1123] | 847 | /// Others are initiated to 0. |
---|
| 848 | /// \param g is the initial value of \ref _g |
---|
| 849 | /// \param l is the initial value of \ref _length |
---|
| 850 | /// \param s is the initial value of \ref _source |
---|
[1116] | 851 | DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) : |
---|
[1218] | 852 | _g((void *)&g), _length((void *)&l), _pred(0), |
---|
| 853 | // _predNode(0), |
---|
| 854 | _dist(0), _source(s) {} |
---|
[1116] | 855 | |
---|
| 856 | }; |
---|
| 857 | |
---|
[1229] | 858 | /// A class to make the usage of Dijkstra algorithm easier |
---|
[953] | 859 | |
---|
[1123] | 860 | /// This class is created to make it easier to use Dijkstra algorithm. |
---|
| 861 | /// It uses the functions and features of the plain \ref Dijkstra, |
---|
[1151] | 862 | /// but it is much simpler to use it. |
---|
[953] | 863 | /// |
---|
[1123] | 864 | /// Simplicity means that the way to change the types defined |
---|
| 865 | /// in the traits class is based on functions that returns the new class |
---|
[1151] | 866 | /// and not on templatable built-in classes. |
---|
| 867 | /// When using the plain \ref Dijkstra |
---|
| 868 | /// the new class with the modified type comes from |
---|
| 869 | /// the original class by using the :: |
---|
| 870 | /// operator. In the case of \ref DijkstraWizard only |
---|
| 871 | /// a function have to be called and it will |
---|
[1123] | 872 | /// return the needed class. |
---|
| 873 | /// |
---|
| 874 | /// It does not have own \ref run method. When its \ref run method is called |
---|
| 875 | /// it initiates a plain \ref Dijkstra class, and calls the \ref Dijkstra::run |
---|
| 876 | /// method of it. |
---|
[953] | 877 | template<class TR> |
---|
[1116] | 878 | class DijkstraWizard : public TR |
---|
[953] | 879 | { |
---|
[1116] | 880 | typedef TR Base; |
---|
[953] | 881 | |
---|
[1123] | 882 | ///The type of the underlying graph. |
---|
[953] | 883 | typedef typename TR::Graph Graph; |
---|
[1119] | 884 | //\e |
---|
[953] | 885 | typedef typename Graph::Node Node; |
---|
[1119] | 886 | //\e |
---|
[953] | 887 | typedef typename Graph::NodeIt NodeIt; |
---|
[1119] | 888 | //\e |
---|
[953] | 889 | typedef typename Graph::Edge Edge; |
---|
[1119] | 890 | //\e |
---|
[953] | 891 | typedef typename Graph::OutEdgeIt OutEdgeIt; |
---|
| 892 | |
---|
[1123] | 893 | ///The type of the map that stores the edge lengths. |
---|
[953] | 894 | typedef typename TR::LengthMap LengthMap; |
---|
[1123] | 895 | ///The type of the length of the edges. |
---|
[987] | 896 | typedef typename LengthMap::Value Value; |
---|
[1123] | 897 | ///\brief The type of the map that stores the last |
---|
| 898 | ///edges of the shortest paths. |
---|
[953] | 899 | typedef typename TR::PredMap PredMap; |
---|
[1218] | 900 | // ///\brief The type of the map that stores the last but one |
---|
| 901 | // ///nodes of the shortest paths. |
---|
| 902 | // typedef typename TR::PredNodeMap PredNodeMap; |
---|
[1123] | 903 | ///The type of the map that stores the dists of the nodes. |
---|
[953] | 904 | typedef typename TR::DistMap DistMap; |
---|
| 905 | |
---|
[1123] | 906 | ///The heap type used by the dijkstra algorithm. |
---|
[953] | 907 | typedef typename TR::Heap Heap; |
---|
[1116] | 908 | public: |
---|
[1123] | 909 | /// Constructor. |
---|
[1116] | 910 | DijkstraWizard() : TR() {} |
---|
[953] | 911 | |
---|
[1123] | 912 | /// Constructor that requires parameters. |
---|
[1124] | 913 | |
---|
| 914 | /// Constructor that requires parameters. |
---|
[1123] | 915 | /// These parameters will be the default values for the traits class. |
---|
[1116] | 916 | DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) : |
---|
| 917 | TR(g,l,s) {} |
---|
[953] | 918 | |
---|
[1123] | 919 | ///Copy constructor |
---|
[1116] | 920 | DijkstraWizard(const TR &b) : TR(b) {} |
---|
[953] | 921 | |
---|
[1116] | 922 | ~DijkstraWizard() {} |
---|
| 923 | |
---|
[1123] | 924 | ///Runs Dijkstra algorithm from a given node. |
---|
| 925 | |
---|
| 926 | ///Runs Dijkstra algorithm from a given node. |
---|
| 927 | ///The node can be given by the \ref source function. |
---|
[1116] | 928 | void run() |
---|
[953] | 929 | { |
---|
[1201] | 930 | if(Base::_source==INVALID) throw UninitializedParameter(); |
---|
[1193] | 931 | Dijkstra<Graph,LengthMap,TR> |
---|
| 932 | Dij(*(Graph*)Base::_g,*(LengthMap*)Base::_length); |
---|
| 933 | if(Base::_pred) Dij.predMap(*(PredMap*)Base::_pred); |
---|
[1218] | 934 | // if(Base::_predNode) Dij.predNodeMap(*(PredNodeMap*)Base::_predNode); |
---|
[1193] | 935 | if(Base::_dist) Dij.distMap(*(DistMap*)Base::_dist); |
---|
[1201] | 936 | Dij.run(Base::_source); |
---|
[1116] | 937 | } |
---|
| 938 | |
---|
[1124] | 939 | ///Runs Dijkstra algorithm from the given node. |
---|
[1123] | 940 | |
---|
[1124] | 941 | ///Runs Dijkstra algorithm from the given node. |
---|
[1123] | 942 | ///\param s is the given source. |
---|
[1116] | 943 | void run(Node s) |
---|
| 944 | { |
---|
[1201] | 945 | Base::_source=s; |
---|
[1116] | 946 | run(); |
---|
[953] | 947 | } |
---|
| 948 | |
---|
| 949 | template<class T> |
---|
[1116] | 950 | struct DefPredMapBase : public Base { |
---|
| 951 | typedef T PredMap; |
---|
[1117] | 952 | static PredMap *createPredMap(const Graph &G) { return 0; }; |
---|
| 953 | DefPredMapBase(const Base &b) : Base(b) {} |
---|
[1116] | 954 | }; |
---|
[953] | 955 | |
---|
[1156] | 956 | ///\brief \ref named-templ-param "Named parameter" |
---|
| 957 | ///function for setting PredMap type |
---|
| 958 | /// |
---|
| 959 | /// \ref named-templ-param "Named parameter" |
---|
| 960 | ///function for setting PredMap type |
---|
[1124] | 961 | /// |
---|
[953] | 962 | template<class T> |
---|
[1116] | 963 | DijkstraWizard<DefPredMapBase<T> > predMap(const T &t) |
---|
[953] | 964 | { |
---|
[1193] | 965 | Base::_pred=(void *)&t; |
---|
[1116] | 966 | return DijkstraWizard<DefPredMapBase<T> >(*this); |
---|
[953] | 967 | } |
---|
| 968 | |
---|
[1116] | 969 | |
---|
[1218] | 970 | // template<class T> |
---|
| 971 | // struct DefPredNodeMapBase : public Base { |
---|
| 972 | // typedef T PredNodeMap; |
---|
| 973 | // static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; }; |
---|
| 974 | // DefPredNodeMapBase(const Base &b) : Base(b) {} |
---|
| 975 | // }; |
---|
[1116] | 976 | |
---|
[1218] | 977 | // ///\brief \ref named-templ-param "Named parameter" |
---|
| 978 | // ///function for setting PredNodeMap type |
---|
| 979 | // /// |
---|
| 980 | // /// \ref named-templ-param "Named parameter" |
---|
| 981 | // ///function for setting PredNodeMap type |
---|
| 982 | // /// |
---|
| 983 | // template<class T> |
---|
| 984 | // DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t) |
---|
| 985 | // { |
---|
| 986 | // Base::_predNode=(void *)&t; |
---|
| 987 | // return DijkstraWizard<DefPredNodeMapBase<T> >(*this); |
---|
| 988 | // } |
---|
[1116] | 989 | |
---|
| 990 | template<class T> |
---|
| 991 | struct DefDistMapBase : public Base { |
---|
| 992 | typedef T DistMap; |
---|
[1117] | 993 | static DistMap *createDistMap(const Graph &G) { return 0; }; |
---|
| 994 | DefDistMapBase(const Base &b) : Base(b) {} |
---|
[1116] | 995 | }; |
---|
[953] | 996 | |
---|
[1156] | 997 | ///\brief \ref named-templ-param "Named parameter" |
---|
| 998 | ///function for setting DistMap type |
---|
| 999 | /// |
---|
| 1000 | /// \ref named-templ-param "Named parameter" |
---|
| 1001 | ///function for setting DistMap type |
---|
[1124] | 1002 | /// |
---|
[953] | 1003 | template<class T> |
---|
[1116] | 1004 | DijkstraWizard<DefDistMapBase<T> > distMap(const T &t) |
---|
[953] | 1005 | { |
---|
[1193] | 1006 | Base::_dist=(void *)&t; |
---|
[1116] | 1007 | return DijkstraWizard<DefDistMapBase<T> >(*this); |
---|
[953] | 1008 | } |
---|
[1117] | 1009 | |
---|
[1123] | 1010 | /// Sets the source node, from which the Dijkstra algorithm runs. |
---|
| 1011 | |
---|
| 1012 | /// Sets the source node, from which the Dijkstra algorithm runs. |
---|
| 1013 | /// \param s is the source node. |
---|
[1117] | 1014 | DijkstraWizard<TR> &source(Node s) |
---|
[953] | 1015 | { |
---|
[1201] | 1016 | Base::_source=s; |
---|
[953] | 1017 | return *this; |
---|
| 1018 | } |
---|
| 1019 | |
---|
| 1020 | }; |
---|
[255] | 1021 | |
---|
[1218] | 1022 | ///Function type interface for Dijkstra algorithm. |
---|
[953] | 1023 | |
---|
[1151] | 1024 | /// \ingroup flowalgs |
---|
[1218] | 1025 | ///Function type interface for Dijkstra algorithm. |
---|
[953] | 1026 | /// |
---|
[1218] | 1027 | ///This function also has several |
---|
| 1028 | ///\ref named-templ-func-param "named parameters", |
---|
| 1029 | ///they are declared as the members of class \ref DijkstraWizard. |
---|
| 1030 | ///The following |
---|
| 1031 | ///example shows how to use these parameters. |
---|
| 1032 | ///\code |
---|
| 1033 | /// dijkstra(g,length,source).predMap(preds).run(); |
---|
| 1034 | ///\endcode |
---|
| 1035 | ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()" |
---|
| 1036 | ///to the end of the parameter list. |
---|
| 1037 | ///\sa DijkstraWizard |
---|
| 1038 | ///\sa Dijkstra |
---|
[953] | 1039 | template<class GR, class LM> |
---|
[1116] | 1040 | DijkstraWizard<DijkstraWizardBase<GR,LM> > |
---|
| 1041 | dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID) |
---|
[953] | 1042 | { |
---|
[1116] | 1043 | return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s); |
---|
[953] | 1044 | } |
---|
| 1045 | |
---|
[921] | 1046 | } //END OF NAMESPACE LEMON |
---|
[255] | 1047 | |
---|
| 1048 | #endif |
---|
| 1049 | |
---|