[209] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
[100] | 2 | * |
---|
[209] | 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
[100] | 4 | * |
---|
| 5 | * Copyright (C) 2003-2008 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #ifndef LEMON_DIJKSTRA_H |
---|
| 20 | #define LEMON_DIJKSTRA_H |
---|
| 21 | |
---|
| 22 | ///\ingroup shortest_path |
---|
| 23 | ///\file |
---|
| 24 | ///\brief Dijkstra algorithm. |
---|
| 25 | |
---|
[184] | 26 | #include <limits> |
---|
[169] | 27 | #include <lemon/list_graph.h> |
---|
[100] | 28 | #include <lemon/bin_heap.h> |
---|
| 29 | #include <lemon/bits/path_dump.h> |
---|
[220] | 30 | #include <lemon/core.h> |
---|
[100] | 31 | #include <lemon/error.h> |
---|
| 32 | #include <lemon/maps.h> |
---|
| 33 | |
---|
| 34 | namespace lemon { |
---|
| 35 | |
---|
[244] | 36 | /// \brief Default operation traits for the Dijkstra algorithm class. |
---|
[209] | 37 | /// |
---|
[244] | 38 | /// This operation traits class defines all computational operations and |
---|
| 39 | /// constants which are used in the Dijkstra algorithm. |
---|
[100] | 40 | template <typename Value> |
---|
| 41 | struct DijkstraDefaultOperationTraits { |
---|
| 42 | /// \brief Gives back the zero value of the type. |
---|
| 43 | static Value zero() { |
---|
| 44 | return static_cast<Value>(0); |
---|
| 45 | } |
---|
| 46 | /// \brief Gives back the sum of the given two elements. |
---|
| 47 | static Value plus(const Value& left, const Value& right) { |
---|
| 48 | return left + right; |
---|
| 49 | } |
---|
[244] | 50 | /// \brief Gives back true only if the first value is less than the second. |
---|
[100] | 51 | static bool less(const Value& left, const Value& right) { |
---|
| 52 | return left < right; |
---|
| 53 | } |
---|
| 54 | }; |
---|
| 55 | |
---|
[244] | 56 | /// \brief Widest path operation traits for the Dijkstra algorithm class. |
---|
[209] | 57 | /// |
---|
[244] | 58 | /// This operation traits class defines all computational operations and |
---|
| 59 | /// constants which are used in the Dijkstra algorithm for widest path |
---|
| 60 | /// computation. |
---|
| 61 | /// |
---|
| 62 | /// \see DijkstraDefaultOperationTraits |
---|
[100] | 63 | template <typename Value> |
---|
| 64 | struct DijkstraWidestPathOperationTraits { |
---|
| 65 | /// \brief Gives back the maximum value of the type. |
---|
| 66 | static Value zero() { |
---|
| 67 | return std::numeric_limits<Value>::max(); |
---|
| 68 | } |
---|
| 69 | /// \brief Gives back the minimum of the given two elements. |
---|
| 70 | static Value plus(const Value& left, const Value& right) { |
---|
| 71 | return std::min(left, right); |
---|
| 72 | } |
---|
[244] | 73 | /// \brief Gives back true only if the first value is less than the second. |
---|
[100] | 74 | static bool less(const Value& left, const Value& right) { |
---|
| 75 | return left < right; |
---|
| 76 | } |
---|
| 77 | }; |
---|
[209] | 78 | |
---|
[100] | 79 | ///Default traits class of Dijkstra class. |
---|
| 80 | |
---|
| 81 | ///Default traits class of Dijkstra class. |
---|
[244] | 82 | ///\tparam GR The type of the digraph. |
---|
| 83 | ///\tparam LM The type of the length map. |
---|
[100] | 84 | template<class GR, class LM> |
---|
| 85 | struct DijkstraDefaultTraits |
---|
| 86 | { |
---|
[244] | 87 | ///The type of the digraph the algorithm runs on. |
---|
[100] | 88 | typedef GR Digraph; |
---|
[244] | 89 | |
---|
[100] | 90 | ///The type of the map that stores the arc lengths. |
---|
| 91 | |
---|
| 92 | ///The type of the map that stores the arc lengths. |
---|
| 93 | ///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
---|
| 94 | typedef LM LengthMap; |
---|
[244] | 95 | ///The type of the length of the arcs. |
---|
[100] | 96 | typedef typename LM::Value Value; |
---|
[244] | 97 | |
---|
[100] | 98 | /// Operation traits for Dijkstra algorithm. |
---|
| 99 | |
---|
[244] | 100 | /// This class defines the operations that are used in the algorithm. |
---|
[100] | 101 | /// \see DijkstraDefaultOperationTraits |
---|
| 102 | typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
---|
| 103 | |
---|
[244] | 104 | /// The cross reference type used by the heap. |
---|
[100] | 105 | |
---|
[244] | 106 | /// The cross reference type used by the heap. |
---|
[100] | 107 | /// Usually it is \c Digraph::NodeMap<int>. |
---|
| 108 | typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
---|
[244] | 109 | ///Instantiates a \ref HeapCrossRef. |
---|
[100] | 110 | |
---|
[244] | 111 | ///This function instantiates a \ref HeapCrossRef. |
---|
| 112 | /// \param g is the digraph, to which we would like to define the |
---|
| 113 | /// \ref HeapCrossRef. |
---|
| 114 | static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
---|
[100] | 115 | { |
---|
[244] | 116 | return new HeapCrossRef(g); |
---|
[100] | 117 | } |
---|
[209] | 118 | |
---|
[244] | 119 | ///The heap type used by the Dijkstra algorithm. |
---|
[100] | 120 | |
---|
[244] | 121 | ///The heap type used by the Dijkstra algorithm. |
---|
[100] | 122 | /// |
---|
| 123 | ///\sa BinHeap |
---|
| 124 | ///\sa Dijkstra |
---|
| 125 | typedef BinHeap<typename LM::Value, HeapCrossRef, std::less<Value> > Heap; |
---|
[244] | 126 | ///Instantiates a \ref Heap. |
---|
[100] | 127 | |
---|
[244] | 128 | ///This function instantiates a \ref Heap. |
---|
| 129 | static Heap *createHeap(HeapCrossRef& r) |
---|
[100] | 130 | { |
---|
[244] | 131 | return new Heap(r); |
---|
[100] | 132 | } |
---|
| 133 | |
---|
[244] | 134 | ///\brief The type of the map that stores the predecessor |
---|
[100] | 135 | ///arcs of the shortest paths. |
---|
[209] | 136 | /// |
---|
[244] | 137 | ///The type of the map that stores the predecessor |
---|
[100] | 138 | ///arcs of the shortest paths. |
---|
| 139 | ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
---|
[244] | 140 | typedef typename Digraph::template NodeMap<typename Digraph::Arc> PredMap; |
---|
| 141 | ///Instantiates a \ref PredMap. |
---|
[209] | 142 | |
---|
[244] | 143 | ///This function instantiates a \ref PredMap. |
---|
| 144 | ///\param g is the digraph, to which we would like to define the |
---|
| 145 | ///\ref PredMap. |
---|
| 146 | static PredMap *createPredMap(const Digraph &g) |
---|
[100] | 147 | { |
---|
[244] | 148 | return new PredMap(g); |
---|
[100] | 149 | } |
---|
| 150 | |
---|
[244] | 151 | ///The type of the map that indicates which nodes are processed. |
---|
[209] | 152 | |
---|
[244] | 153 | ///The type of the map that indicates which nodes are processed. |
---|
[100] | 154 | ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
---|
| 155 | ///By default it is a NullMap. |
---|
| 156 | typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
---|
[244] | 157 | ///Instantiates a \ref ProcessedMap. |
---|
[209] | 158 | |
---|
[244] | 159 | ///This function instantiates a \ref ProcessedMap. |
---|
[100] | 160 | ///\param g is the digraph, to which |
---|
[244] | 161 | ///we would like to define the \ref ProcessedMap |
---|
[100] | 162 | #ifdef DOXYGEN |
---|
[244] | 163 | static ProcessedMap *createProcessedMap(const Digraph &g) |
---|
[100] | 164 | #else |
---|
[244] | 165 | static ProcessedMap *createProcessedMap(const Digraph &) |
---|
[100] | 166 | #endif |
---|
| 167 | { |
---|
| 168 | return new ProcessedMap(); |
---|
| 169 | } |
---|
[209] | 170 | |
---|
[244] | 171 | ///The type of the map that stores the distances of the nodes. |
---|
| 172 | |
---|
| 173 | ///The type of the map that stores the distances of the nodes. |
---|
[100] | 174 | ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
---|
| 175 | typedef typename Digraph::template NodeMap<typename LM::Value> DistMap; |
---|
[244] | 176 | ///Instantiates a \ref DistMap. |
---|
[209] | 177 | |
---|
| 178 | ///This function instantiates a \ref DistMap. |
---|
[244] | 179 | ///\param g is the digraph, to which we would like to define |
---|
[210] | 180 | ///the \ref DistMap |
---|
[244] | 181 | static DistMap *createDistMap(const Digraph &g) |
---|
[100] | 182 | { |
---|
[244] | 183 | return new DistMap(g); |
---|
[100] | 184 | } |
---|
| 185 | }; |
---|
[209] | 186 | |
---|
[100] | 187 | ///%Dijkstra algorithm class. |
---|
[209] | 188 | |
---|
[100] | 189 | /// \ingroup shortest_path |
---|
[244] | 190 | ///This class provides an efficient implementation of the %Dijkstra algorithm. |
---|
| 191 | /// |
---|
[100] | 192 | ///The arc lengths are passed to the algorithm using a |
---|
| 193 | ///\ref concepts::ReadMap "ReadMap", |
---|
| 194 | ///so it is easy to change it to any kind of length. |
---|
| 195 | ///The type of the length is determined by the |
---|
| 196 | ///\ref concepts::ReadMap::Value "Value" of the length map. |
---|
| 197 | ///It is also possible to change the underlying priority heap. |
---|
| 198 | /// |
---|
[244] | 199 | ///There is also a \ref dijkstra() "function type interface" for the |
---|
| 200 | ///%Dijkstra algorithm, which is convenient in the simplier cases and |
---|
| 201 | ///it can be used easier. |
---|
| 202 | /// |
---|
| 203 | ///\tparam GR The type of the digraph the algorithm runs on. |
---|
| 204 | ///The default value is \ref ListDigraph. |
---|
| 205 | ///The value of GR is not used directly by \ref Dijkstra, it is only |
---|
| 206 | ///passed to \ref DijkstraDefaultTraits. |
---|
| 207 | ///\tparam LM A readable arc map that determines the lengths of the |
---|
[100] | 208 | ///arcs. It is read once for each arc, so the map may involve in |
---|
[244] | 209 | ///relatively time consuming process to compute the arc lengths if |
---|
[100] | 210 | ///it is necessary. The default map type is \ref |
---|
[244] | 211 | ///concepts::Digraph::ArcMap "Digraph::ArcMap<int>". |
---|
| 212 | ///The value of LM is not used directly by \ref Dijkstra, it is only |
---|
| 213 | ///passed to \ref DijkstraDefaultTraits. |
---|
| 214 | ///\tparam TR Traits class to set various data types used by the algorithm. |
---|
| 215 | ///The default traits class is \ref DijkstraDefaultTraits |
---|
| 216 | ///"DijkstraDefaultTraits<GR,LM>". See \ref DijkstraDefaultTraits |
---|
| 217 | ///for the documentation of a Dijkstra traits class. |
---|
[100] | 218 | #ifdef DOXYGEN |
---|
| 219 | template <typename GR, typename LM, typename TR> |
---|
| 220 | #else |
---|
| 221 | template <typename GR=ListDigraph, |
---|
[209] | 222 | typename LM=typename GR::template ArcMap<int>, |
---|
| 223 | typename TR=DijkstraDefaultTraits<GR,LM> > |
---|
[100] | 224 | #endif |
---|
| 225 | class Dijkstra { |
---|
| 226 | public: |
---|
[244] | 227 | ///\ref Exception for uninitialized parameters. |
---|
| 228 | |
---|
| 229 | ///This error represents problems in the initialization of the |
---|
| 230 | ///parameters of the algorithm. |
---|
[100] | 231 | class UninitializedParameter : public lemon::UninitializedParameter { |
---|
| 232 | public: |
---|
| 233 | virtual const char* what() const throw() { |
---|
[209] | 234 | return "lemon::Dijkstra::UninitializedParameter"; |
---|
[100] | 235 | } |
---|
| 236 | }; |
---|
| 237 | |
---|
[244] | 238 | ///The type of the digraph the algorithm runs on. |
---|
[100] | 239 | typedef typename TR::Digraph Digraph; |
---|
[209] | 240 | |
---|
[100] | 241 | ///The type of the length of the arcs. |
---|
| 242 | typedef typename TR::LengthMap::Value Value; |
---|
| 243 | ///The type of the map that stores the arc lengths. |
---|
| 244 | typedef typename TR::LengthMap LengthMap; |
---|
[244] | 245 | ///\brief The type of the map that stores the predecessor arcs of the |
---|
| 246 | ///shortest paths. |
---|
[100] | 247 | typedef typename TR::PredMap PredMap; |
---|
[244] | 248 | ///The type of the map that stores the distances of the nodes. |
---|
| 249 | typedef typename TR::DistMap DistMap; |
---|
| 250 | ///The type of the map that indicates which nodes are processed. |
---|
[100] | 251 | typedef typename TR::ProcessedMap ProcessedMap; |
---|
[244] | 252 | ///The type of the paths. |
---|
| 253 | typedef PredMapPath<Digraph, PredMap> Path; |
---|
[100] | 254 | ///The cross reference type used for the current heap. |
---|
| 255 | typedef typename TR::HeapCrossRef HeapCrossRef; |
---|
[244] | 256 | ///The heap type used by the algorithm. |
---|
[100] | 257 | typedef typename TR::Heap Heap; |
---|
[244] | 258 | ///The operation traits class. |
---|
[100] | 259 | typedef typename TR::OperationTraits OperationTraits; |
---|
[244] | 260 | |
---|
| 261 | ///The traits class. |
---|
| 262 | typedef TR Traits; |
---|
| 263 | |
---|
[100] | 264 | private: |
---|
[244] | 265 | |
---|
| 266 | typedef typename Digraph::Node Node; |
---|
| 267 | typedef typename Digraph::NodeIt NodeIt; |
---|
| 268 | typedef typename Digraph::Arc Arc; |
---|
| 269 | typedef typename Digraph::OutArcIt OutArcIt; |
---|
| 270 | |
---|
| 271 | //Pointer to the underlying digraph. |
---|
[100] | 272 | const Digraph *G; |
---|
[244] | 273 | //Pointer to the length map. |
---|
[100] | 274 | const LengthMap *length; |
---|
[244] | 275 | //Pointer to the map of predecessors arcs. |
---|
[100] | 276 | PredMap *_pred; |
---|
[244] | 277 | //Indicates if _pred is locally allocated (true) or not. |
---|
[100] | 278 | bool local_pred; |
---|
[244] | 279 | //Pointer to the map of distances. |
---|
[100] | 280 | DistMap *_dist; |
---|
[244] | 281 | //Indicates if _dist is locally allocated (true) or not. |
---|
[100] | 282 | bool local_dist; |
---|
[244] | 283 | //Pointer to the map of processed status of the nodes. |
---|
[100] | 284 | ProcessedMap *_processed; |
---|
[244] | 285 | //Indicates if _processed is locally allocated (true) or not. |
---|
[100] | 286 | bool local_processed; |
---|
[244] | 287 | //Pointer to the heap cross references. |
---|
[100] | 288 | HeapCrossRef *_heap_cross_ref; |
---|
[244] | 289 | //Indicates if _heap_cross_ref is locally allocated (true) or not. |
---|
[100] | 290 | bool local_heap_cross_ref; |
---|
[244] | 291 | //Pointer to the heap. |
---|
[100] | 292 | Heap *_heap; |
---|
[244] | 293 | //Indicates if _heap is locally allocated (true) or not. |
---|
[100] | 294 | bool local_heap; |
---|
| 295 | |
---|
[280] | 296 | //Creates the maps if necessary. |
---|
[209] | 297 | void create_maps() |
---|
[100] | 298 | { |
---|
| 299 | if(!_pred) { |
---|
[209] | 300 | local_pred = true; |
---|
| 301 | _pred = Traits::createPredMap(*G); |
---|
[100] | 302 | } |
---|
| 303 | if(!_dist) { |
---|
[209] | 304 | local_dist = true; |
---|
| 305 | _dist = Traits::createDistMap(*G); |
---|
[100] | 306 | } |
---|
| 307 | if(!_processed) { |
---|
[209] | 308 | local_processed = true; |
---|
| 309 | _processed = Traits::createProcessedMap(*G); |
---|
[100] | 310 | } |
---|
| 311 | if (!_heap_cross_ref) { |
---|
[209] | 312 | local_heap_cross_ref = true; |
---|
| 313 | _heap_cross_ref = Traits::createHeapCrossRef(*G); |
---|
[100] | 314 | } |
---|
| 315 | if (!_heap) { |
---|
[209] | 316 | local_heap = true; |
---|
| 317 | _heap = Traits::createHeap(*_heap_cross_ref); |
---|
[100] | 318 | } |
---|
| 319 | } |
---|
[209] | 320 | |
---|
[244] | 321 | public: |
---|
[100] | 322 | |
---|
| 323 | typedef Dijkstra Create; |
---|
[209] | 324 | |
---|
[100] | 325 | ///\name Named template parameters |
---|
| 326 | |
---|
| 327 | ///@{ |
---|
| 328 | |
---|
| 329 | template <class T> |
---|
[257] | 330 | struct SetPredMapTraits : public Traits { |
---|
[100] | 331 | typedef T PredMap; |
---|
| 332 | static PredMap *createPredMap(const Digraph &) |
---|
| 333 | { |
---|
[209] | 334 | throw UninitializedParameter(); |
---|
[100] | 335 | } |
---|
| 336 | }; |
---|
[244] | 337 | ///\brief \ref named-templ-param "Named parameter" for setting |
---|
| 338 | ///\ref PredMap type. |
---|
[100] | 339 | /// |
---|
[244] | 340 | ///\ref named-templ-param "Named parameter" for setting |
---|
| 341 | ///\ref PredMap type. |
---|
[100] | 342 | template <class T> |
---|
[257] | 343 | struct SetPredMap |
---|
| 344 | : public Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > { |
---|
| 345 | typedef Dijkstra< Digraph, LengthMap, SetPredMapTraits<T> > Create; |
---|
[100] | 346 | }; |
---|
[209] | 347 | |
---|
[100] | 348 | template <class T> |
---|
[257] | 349 | struct SetDistMapTraits : public Traits { |
---|
[100] | 350 | typedef T DistMap; |
---|
| 351 | static DistMap *createDistMap(const Digraph &) |
---|
| 352 | { |
---|
[209] | 353 | throw UninitializedParameter(); |
---|
[100] | 354 | } |
---|
| 355 | }; |
---|
[244] | 356 | ///\brief \ref named-templ-param "Named parameter" for setting |
---|
| 357 | ///\ref DistMap type. |
---|
[100] | 358 | /// |
---|
[244] | 359 | ///\ref named-templ-param "Named parameter" for setting |
---|
| 360 | ///\ref DistMap type. |
---|
[100] | 361 | template <class T> |
---|
[257] | 362 | struct SetDistMap |
---|
| 363 | : public Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > { |
---|
| 364 | typedef Dijkstra< Digraph, LengthMap, SetDistMapTraits<T> > Create; |
---|
[100] | 365 | }; |
---|
[209] | 366 | |
---|
[100] | 367 | template <class T> |
---|
[257] | 368 | struct SetProcessedMapTraits : public Traits { |
---|
[100] | 369 | typedef T ProcessedMap; |
---|
[244] | 370 | static ProcessedMap *createProcessedMap(const Digraph &) |
---|
[100] | 371 | { |
---|
[209] | 372 | throw UninitializedParameter(); |
---|
[100] | 373 | } |
---|
| 374 | }; |
---|
[244] | 375 | ///\brief \ref named-templ-param "Named parameter" for setting |
---|
| 376 | ///\ref ProcessedMap type. |
---|
[100] | 377 | /// |
---|
[244] | 378 | ///\ref named-templ-param "Named parameter" for setting |
---|
| 379 | ///\ref ProcessedMap type. |
---|
[100] | 380 | template <class T> |
---|
[257] | 381 | struct SetProcessedMap |
---|
| 382 | : public Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > { |
---|
| 383 | typedef Dijkstra< Digraph, LengthMap, SetProcessedMapTraits<T> > Create; |
---|
[100] | 384 | }; |
---|
[209] | 385 | |
---|
[257] | 386 | struct SetStandardProcessedMapTraits : public Traits { |
---|
[100] | 387 | typedef typename Digraph::template NodeMap<bool> ProcessedMap; |
---|
[244] | 388 | static ProcessedMap *createProcessedMap(const Digraph &g) |
---|
[100] | 389 | { |
---|
[244] | 390 | return new ProcessedMap(g); |
---|
[100] | 391 | } |
---|
| 392 | }; |
---|
[244] | 393 | ///\brief \ref named-templ-param "Named parameter" for setting |
---|
| 394 | ///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
---|
[100] | 395 | /// |
---|
[244] | 396 | ///\ref named-templ-param "Named parameter" for setting |
---|
| 397 | ///\ref ProcessedMap type to be <tt>Digraph::NodeMap<bool></tt>. |
---|
| 398 | ///If you don't set it explicitly, it will be automatically allocated. |
---|
[257] | 399 | struct SetStandardProcessedMap |
---|
| 400 | : public Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > { |
---|
| 401 | typedef Dijkstra< Digraph, LengthMap, SetStandardProcessedMapTraits > |
---|
[210] | 402 | Create; |
---|
[100] | 403 | }; |
---|
| 404 | |
---|
| 405 | template <class H, class CR> |
---|
[257] | 406 | struct SetHeapTraits : public Traits { |
---|
[100] | 407 | typedef CR HeapCrossRef; |
---|
| 408 | typedef H Heap; |
---|
| 409 | static HeapCrossRef *createHeapCrossRef(const Digraph &) { |
---|
[209] | 410 | throw UninitializedParameter(); |
---|
[100] | 411 | } |
---|
[209] | 412 | static Heap *createHeap(HeapCrossRef &) |
---|
[100] | 413 | { |
---|
[209] | 414 | throw UninitializedParameter(); |
---|
[100] | 415 | } |
---|
| 416 | }; |
---|
| 417 | ///\brief \ref named-templ-param "Named parameter" for setting |
---|
| 418 | ///heap and cross reference type |
---|
| 419 | /// |
---|
[209] | 420 | ///\ref named-templ-param "Named parameter" for setting heap and cross |
---|
[244] | 421 | ///reference type. |
---|
[100] | 422 | template <class H, class CR = typename Digraph::template NodeMap<int> > |
---|
[257] | 423 | struct SetHeap |
---|
| 424 | : public Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > { |
---|
| 425 | typedef Dijkstra< Digraph, LengthMap, SetHeapTraits<H, CR> > Create; |
---|
[100] | 426 | }; |
---|
| 427 | |
---|
| 428 | template <class H, class CR> |
---|
[257] | 429 | struct SetStandardHeapTraits : public Traits { |
---|
[100] | 430 | typedef CR HeapCrossRef; |
---|
| 431 | typedef H Heap; |
---|
| 432 | static HeapCrossRef *createHeapCrossRef(const Digraph &G) { |
---|
[209] | 433 | return new HeapCrossRef(G); |
---|
[100] | 434 | } |
---|
[209] | 435 | static Heap *createHeap(HeapCrossRef &R) |
---|
[100] | 436 | { |
---|
[209] | 437 | return new Heap(R); |
---|
[100] | 438 | } |
---|
| 439 | }; |
---|
| 440 | ///\brief \ref named-templ-param "Named parameter" for setting |
---|
| 441 | ///heap and cross reference type with automatic allocation |
---|
| 442 | /// |
---|
[209] | 443 | ///\ref named-templ-param "Named parameter" for setting heap and cross |
---|
| 444 | ///reference type. It can allocate the heap and the cross reference |
---|
| 445 | ///object if the cross reference's constructor waits for the digraph as |
---|
[100] | 446 | ///parameter and the heap's constructor waits for the cross reference. |
---|
| 447 | template <class H, class CR = typename Digraph::template NodeMap<int> > |
---|
[257] | 448 | struct SetStandardHeap |
---|
| 449 | : public Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > { |
---|
| 450 | typedef Dijkstra< Digraph, LengthMap, SetStandardHeapTraits<H, CR> > |
---|
[100] | 451 | Create; |
---|
| 452 | }; |
---|
| 453 | |
---|
| 454 | template <class T> |
---|
[257] | 455 | struct SetOperationTraitsTraits : public Traits { |
---|
[100] | 456 | typedef T OperationTraits; |
---|
| 457 | }; |
---|
[209] | 458 | |
---|
| 459 | /// \brief \ref named-templ-param "Named parameter" for setting |
---|
[244] | 460 | ///\ref OperationTraits type |
---|
[100] | 461 | /// |
---|
[244] | 462 | ///\ref named-templ-param "Named parameter" for setting |
---|
| 463 | ///\ref OperationTraits type. |
---|
[100] | 464 | template <class T> |
---|
[257] | 465 | struct SetOperationTraits |
---|
| 466 | : public Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > { |
---|
| 467 | typedef Dijkstra<Digraph, LengthMap, SetOperationTraitsTraits<T> > |
---|
[100] | 468 | Create; |
---|
| 469 | }; |
---|
[209] | 470 | |
---|
[100] | 471 | ///@} |
---|
| 472 | |
---|
| 473 | protected: |
---|
| 474 | |
---|
| 475 | Dijkstra() {} |
---|
| 476 | |
---|
[209] | 477 | public: |
---|
| 478 | |
---|
[100] | 479 | ///Constructor. |
---|
[209] | 480 | |
---|
[244] | 481 | ///Constructor. |
---|
| 482 | ///\param _g The digraph the algorithm runs on. |
---|
| 483 | ///\param _length The length map used by the algorithm. |
---|
| 484 | Dijkstra(const Digraph& _g, const LengthMap& _length) : |
---|
| 485 | G(&_g), length(&_length), |
---|
[100] | 486 | _pred(NULL), local_pred(false), |
---|
| 487 | _dist(NULL), local_dist(false), |
---|
| 488 | _processed(NULL), local_processed(false), |
---|
| 489 | _heap_cross_ref(NULL), local_heap_cross_ref(false), |
---|
| 490 | _heap(NULL), local_heap(false) |
---|
| 491 | { } |
---|
[209] | 492 | |
---|
[100] | 493 | ///Destructor. |
---|
[209] | 494 | ~Dijkstra() |
---|
[100] | 495 | { |
---|
| 496 | if(local_pred) delete _pred; |
---|
| 497 | if(local_dist) delete _dist; |
---|
| 498 | if(local_processed) delete _processed; |
---|
| 499 | if(local_heap_cross_ref) delete _heap_cross_ref; |
---|
| 500 | if(local_heap) delete _heap; |
---|
| 501 | } |
---|
| 502 | |
---|
| 503 | ///Sets the length map. |
---|
| 504 | |
---|
| 505 | ///Sets the length map. |
---|
| 506 | ///\return <tt> (*this) </tt> |
---|
[209] | 507 | Dijkstra &lengthMap(const LengthMap &m) |
---|
[100] | 508 | { |
---|
| 509 | length = &m; |
---|
| 510 | return *this; |
---|
| 511 | } |
---|
| 512 | |
---|
[244] | 513 | ///Sets the map that stores the predecessor arcs. |
---|
[100] | 514 | |
---|
[244] | 515 | ///Sets the map that stores the predecessor arcs. |
---|
[100] | 516 | ///If you don't use this function before calling \ref run(), |
---|
[244] | 517 | ///it will allocate one. The destructor deallocates this |
---|
[100] | 518 | ///automatically allocated map, of course. |
---|
| 519 | ///\return <tt> (*this) </tt> |
---|
[209] | 520 | Dijkstra &predMap(PredMap &m) |
---|
[100] | 521 | { |
---|
| 522 | if(local_pred) { |
---|
[209] | 523 | delete _pred; |
---|
| 524 | local_pred=false; |
---|
[100] | 525 | } |
---|
| 526 | _pred = &m; |
---|
| 527 | return *this; |
---|
| 528 | } |
---|
| 529 | |
---|
[244] | 530 | ///Sets the map that indicates which nodes are processed. |
---|
[100] | 531 | |
---|
[244] | 532 | ///Sets the map that indicates which nodes are processed. |
---|
[100] | 533 | ///If you don't use this function before calling \ref run(), |
---|
[244] | 534 | ///it will allocate one. The destructor deallocates this |
---|
| 535 | ///automatically allocated map, of course. |
---|
| 536 | ///\return <tt> (*this) </tt> |
---|
| 537 | Dijkstra &processedMap(ProcessedMap &m) |
---|
| 538 | { |
---|
| 539 | if(local_processed) { |
---|
| 540 | delete _processed; |
---|
| 541 | local_processed=false; |
---|
| 542 | } |
---|
| 543 | _processed = &m; |
---|
| 544 | return *this; |
---|
| 545 | } |
---|
| 546 | |
---|
| 547 | ///Sets the map that stores the distances of the nodes. |
---|
| 548 | |
---|
| 549 | ///Sets the map that stores the distances of the nodes calculated by the |
---|
| 550 | ///algorithm. |
---|
| 551 | ///If you don't use this function before calling \ref run(), |
---|
| 552 | ///it will allocate one. The destructor deallocates this |
---|
[100] | 553 | ///automatically allocated map, of course. |
---|
| 554 | ///\return <tt> (*this) </tt> |
---|
[209] | 555 | Dijkstra &distMap(DistMap &m) |
---|
[100] | 556 | { |
---|
| 557 | if(local_dist) { |
---|
[209] | 558 | delete _dist; |
---|
| 559 | local_dist=false; |
---|
[100] | 560 | } |
---|
| 561 | _dist = &m; |
---|
| 562 | return *this; |
---|
| 563 | } |
---|
| 564 | |
---|
| 565 | ///Sets the heap and the cross reference used by algorithm. |
---|
| 566 | |
---|
| 567 | ///Sets the heap and the cross reference used by algorithm. |
---|
| 568 | ///If you don't use this function before calling \ref run(), |
---|
[244] | 569 | ///it will allocate one. The destructor deallocates this |
---|
[100] | 570 | ///automatically allocated heap and cross reference, of course. |
---|
| 571 | ///\return <tt> (*this) </tt> |
---|
| 572 | Dijkstra &heap(Heap& hp, HeapCrossRef &cr) |
---|
| 573 | { |
---|
| 574 | if(local_heap_cross_ref) { |
---|
[209] | 575 | delete _heap_cross_ref; |
---|
| 576 | local_heap_cross_ref=false; |
---|
[100] | 577 | } |
---|
| 578 | _heap_cross_ref = &cr; |
---|
| 579 | if(local_heap) { |
---|
[209] | 580 | delete _heap; |
---|
| 581 | local_heap=false; |
---|
[100] | 582 | } |
---|
| 583 | _heap = &hp; |
---|
| 584 | return *this; |
---|
| 585 | } |
---|
| 586 | |
---|
| 587 | private: |
---|
[244] | 588 | |
---|
[100] | 589 | void finalizeNodeData(Node v,Value dst) |
---|
| 590 | { |
---|
| 591 | _processed->set(v,true); |
---|
| 592 | _dist->set(v, dst); |
---|
| 593 | } |
---|
| 594 | |
---|
| 595 | public: |
---|
| 596 | |
---|
| 597 | ///\name Execution control |
---|
[244] | 598 | ///The simplest way to execute the algorithm is to use one of the |
---|
| 599 | ///member functions called \ref lemon::Dijkstra::run() "run()". |
---|
[100] | 600 | ///\n |
---|
[244] | 601 | ///If you need more control on the execution, first you must call |
---|
| 602 | ///\ref lemon::Dijkstra::init() "init()", then you can add several |
---|
| 603 | ///source nodes with \ref lemon::Dijkstra::addSource() "addSource()". |
---|
| 604 | ///Finally \ref lemon::Dijkstra::start() "start()" will perform the |
---|
| 605 | ///actual path computation. |
---|
[100] | 606 | |
---|
| 607 | ///@{ |
---|
| 608 | |
---|
| 609 | ///Initializes the internal data structures. |
---|
| 610 | |
---|
| 611 | ///Initializes the internal data structures. |
---|
| 612 | /// |
---|
| 613 | void init() |
---|
| 614 | { |
---|
| 615 | create_maps(); |
---|
| 616 | _heap->clear(); |
---|
| 617 | for ( NodeIt u(*G) ; u!=INVALID ; ++u ) { |
---|
[209] | 618 | _pred->set(u,INVALID); |
---|
| 619 | _processed->set(u,false); |
---|
| 620 | _heap_cross_ref->set(u,Heap::PRE_HEAP); |
---|
[100] | 621 | } |
---|
| 622 | } |
---|
[209] | 623 | |
---|
[100] | 624 | ///Adds a new source node. |
---|
| 625 | |
---|
| 626 | ///Adds a new source node to the priority heap. |
---|
| 627 | ///The optional second parameter is the initial distance of the node. |
---|
| 628 | /// |
---|
[244] | 629 | ///The function checks if the node has already been added to the heap and |
---|
[100] | 630 | ///it is pushed to the heap only if either it was not in the heap |
---|
| 631 | ///or the shortest path found till then is shorter than \c dst. |
---|
| 632 | void addSource(Node s,Value dst=OperationTraits::zero()) |
---|
| 633 | { |
---|
| 634 | if(_heap->state(s) != Heap::IN_HEAP) { |
---|
[209] | 635 | _heap->push(s,dst); |
---|
[100] | 636 | } else if(OperationTraits::less((*_heap)[s], dst)) { |
---|
[209] | 637 | _heap->set(s,dst); |
---|
| 638 | _pred->set(s,INVALID); |
---|
[100] | 639 | } |
---|
| 640 | } |
---|
[209] | 641 | |
---|
[100] | 642 | ///Processes the next node in the priority heap |
---|
| 643 | |
---|
| 644 | ///Processes the next node in the priority heap. |
---|
| 645 | /// |
---|
| 646 | ///\return The processed node. |
---|
| 647 | /// |
---|
[244] | 648 | ///\warning The priority heap must not be empty. |
---|
[100] | 649 | Node processNextNode() |
---|
| 650 | { |
---|
[209] | 651 | Node v=_heap->top(); |
---|
[100] | 652 | Value oldvalue=_heap->prio(); |
---|
| 653 | _heap->pop(); |
---|
| 654 | finalizeNodeData(v,oldvalue); |
---|
[209] | 655 | |
---|
[100] | 656 | for(OutArcIt e(*G,v); e!=INVALID; ++e) { |
---|
[209] | 657 | Node w=G->target(e); |
---|
| 658 | switch(_heap->state(w)) { |
---|
| 659 | case Heap::PRE_HEAP: |
---|
| 660 | _heap->push(w,OperationTraits::plus(oldvalue, (*length)[e])); |
---|
| 661 | _pred->set(w,e); |
---|
| 662 | break; |
---|
| 663 | case Heap::IN_HEAP: |
---|
| 664 | { |
---|
| 665 | Value newvalue = OperationTraits::plus(oldvalue, (*length)[e]); |
---|
| 666 | if ( OperationTraits::less(newvalue, (*_heap)[w]) ) { |
---|
| 667 | _heap->decrease(w, newvalue); |
---|
| 668 | _pred->set(w,e); |
---|
| 669 | } |
---|
| 670 | } |
---|
| 671 | break; |
---|
| 672 | case Heap::POST_HEAP: |
---|
| 673 | break; |
---|
| 674 | } |
---|
[100] | 675 | } |
---|
| 676 | return v; |
---|
| 677 | } |
---|
| 678 | |
---|
[244] | 679 | ///The next node to be processed. |
---|
[209] | 680 | |
---|
[244] | 681 | ///Returns the next node to be processed or \c INVALID if the |
---|
| 682 | ///priority heap is empty. |
---|
| 683 | Node nextNode() const |
---|
[209] | 684 | { |
---|
[100] | 685 | return !_heap->empty()?_heap->top():INVALID; |
---|
| 686 | } |
---|
[209] | 687 | |
---|
[100] | 688 | ///\brief Returns \c false if there are nodes |
---|
[244] | 689 | ///to be processed. |
---|
[100] | 690 | /// |
---|
| 691 | ///Returns \c false if there are nodes |
---|
[244] | 692 | ///to be processed in the priority heap. |
---|
| 693 | bool emptyQueue() const { return _heap->empty(); } |
---|
| 694 | |
---|
[100] | 695 | ///Returns the number of the nodes to be processed in the priority heap |
---|
| 696 | |
---|
[244] | 697 | ///Returns the number of the nodes to be processed in the priority heap. |
---|
[100] | 698 | /// |
---|
[244] | 699 | int queueSize() const { return _heap->size(); } |
---|
[209] | 700 | |
---|
[100] | 701 | ///Executes the algorithm. |
---|
| 702 | |
---|
| 703 | ///Executes the algorithm. |
---|
| 704 | /// |
---|
[244] | 705 | ///This method runs the %Dijkstra algorithm from the root node(s) |
---|
| 706 | ///in order to compute the shortest path to each node. |
---|
| 707 | /// |
---|
| 708 | ///The algorithm computes |
---|
| 709 | ///- the shortest path tree (forest), |
---|
| 710 | ///- the distance of each node from the root(s). |
---|
| 711 | /// |
---|
| 712 | ///\pre init() must be called and at least one root node should be |
---|
| 713 | ///added with addSource() before using this function. |
---|
| 714 | /// |
---|
| 715 | ///\note <tt>d.start()</tt> is just a shortcut of the following code. |
---|
| 716 | ///\code |
---|
| 717 | /// while ( !d.emptyQueue() ) { |
---|
| 718 | /// d.processNextNode(); |
---|
| 719 | /// } |
---|
| 720 | ///\endcode |
---|
| 721 | void start() |
---|
| 722 | { |
---|
| 723 | while ( !emptyQueue() ) processNextNode(); |
---|
| 724 | } |
---|
| 725 | |
---|
| 726 | ///Executes the algorithm until the given target node is reached. |
---|
| 727 | |
---|
| 728 | ///Executes the algorithm until the given target node is reached. |
---|
[100] | 729 | /// |
---|
| 730 | ///This method runs the %Dijkstra algorithm from the root node(s) |
---|
[244] | 731 | ///in order to compute the shortest path to \c dest. |
---|
[100] | 732 | /// |
---|
[244] | 733 | ///The algorithm computes |
---|
| 734 | ///- the shortest path to \c dest, |
---|
| 735 | ///- the distance of \c dest from the root(s). |
---|
[100] | 736 | /// |
---|
[244] | 737 | ///\pre init() must be called and at least one root node should be |
---|
| 738 | ///added with addSource() before using this function. |
---|
[100] | 739 | void start(Node dest) |
---|
| 740 | { |
---|
| 741 | while ( !_heap->empty() && _heap->top()!=dest ) processNextNode(); |
---|
| 742 | if ( !_heap->empty() ) finalizeNodeData(_heap->top(),_heap->prio()); |
---|
| 743 | } |
---|
[209] | 744 | |
---|
[100] | 745 | ///Executes the algorithm until a condition is met. |
---|
| 746 | |
---|
| 747 | ///Executes the algorithm until a condition is met. |
---|
| 748 | /// |
---|
[244] | 749 | ///This method runs the %Dijkstra algorithm from the root node(s) in |
---|
| 750 | ///order to compute the shortest path to a node \c v with |
---|
| 751 | /// <tt>nm[v]</tt> true, if such a node can be found. |
---|
[100] | 752 | /// |
---|
[244] | 753 | ///\param nm A \c bool (or convertible) node map. The algorithm |
---|
[100] | 754 | ///will stop when it reaches a node \c v with <tt>nm[v]</tt> true. |
---|
| 755 | /// |
---|
| 756 | ///\return The reached node \c v with <tt>nm[v]</tt> true or |
---|
| 757 | ///\c INVALID if no such node was found. |
---|
[244] | 758 | /// |
---|
| 759 | ///\pre init() must be called and at least one root node should be |
---|
| 760 | ///added with addSource() before using this function. |
---|
[100] | 761 | template<class NodeBoolMap> |
---|
| 762 | Node start(const NodeBoolMap &nm) |
---|
| 763 | { |
---|
| 764 | while ( !_heap->empty() && !nm[_heap->top()] ) processNextNode(); |
---|
| 765 | if ( _heap->empty() ) return INVALID; |
---|
| 766 | finalizeNodeData(_heap->top(),_heap->prio()); |
---|
| 767 | return _heap->top(); |
---|
| 768 | } |
---|
[209] | 769 | |
---|
[244] | 770 | ///Runs the algorithm from the given node. |
---|
[209] | 771 | |
---|
[244] | 772 | ///This method runs the %Dijkstra algorithm from node \c s |
---|
| 773 | ///in order to compute the shortest path to each node. |
---|
[100] | 774 | /// |
---|
[244] | 775 | ///The algorithm computes |
---|
| 776 | ///- the shortest path tree, |
---|
| 777 | ///- the distance of each node from the root. |
---|
| 778 | /// |
---|
| 779 | ///\note <tt>d.run(s)</tt> is just a shortcut of the following code. |
---|
[100] | 780 | ///\code |
---|
| 781 | /// d.init(); |
---|
| 782 | /// d.addSource(s); |
---|
| 783 | /// d.start(); |
---|
| 784 | ///\endcode |
---|
| 785 | void run(Node s) { |
---|
| 786 | init(); |
---|
| 787 | addSource(s); |
---|
| 788 | start(); |
---|
| 789 | } |
---|
[209] | 790 | |
---|
[100] | 791 | ///Finds the shortest path between \c s and \c t. |
---|
[209] | 792 | |
---|
[244] | 793 | ///This method runs the %Dijkstra algorithm from node \c s |
---|
| 794 | ///in order to compute the shortest path to \c t. |
---|
[100] | 795 | /// |
---|
[244] | 796 | ///\return The length of the shortest <tt>s</tt>--<tt>t</tt> path, |
---|
| 797 | ///if \c t is reachable form \c s, \c 0 otherwise. |
---|
| 798 | /// |
---|
| 799 | ///\note Apart from the return value, <tt>d.run(s,t)</tt> is just a |
---|
| 800 | ///shortcut of the following code. |
---|
[100] | 801 | ///\code |
---|
| 802 | /// d.init(); |
---|
| 803 | /// d.addSource(s); |
---|
| 804 | /// d.start(t); |
---|
| 805 | ///\endcode |
---|
| 806 | Value run(Node s,Node t) { |
---|
| 807 | init(); |
---|
| 808 | addSource(s); |
---|
| 809 | start(t); |
---|
| 810 | return (*_pred)[t]==INVALID?OperationTraits::zero():(*_dist)[t]; |
---|
| 811 | } |
---|
[209] | 812 | |
---|
[100] | 813 | ///@} |
---|
| 814 | |
---|
| 815 | ///\name Query Functions |
---|
| 816 | ///The result of the %Dijkstra algorithm can be obtained using these |
---|
| 817 | ///functions.\n |
---|
[244] | 818 | ///Either \ref lemon::Dijkstra::run() "run()" or |
---|
| 819 | ///\ref lemon::Dijkstra::start() "start()" must be called before |
---|
| 820 | ///using them. |
---|
[209] | 821 | |
---|
[100] | 822 | ///@{ |
---|
| 823 | |
---|
[244] | 824 | ///The shortest path to a node. |
---|
[209] | 825 | |
---|
[244] | 826 | ///Returns the shortest path to a node. |
---|
| 827 | /// |
---|
| 828 | ///\warning \c t should be reachable from the root(s). |
---|
| 829 | /// |
---|
| 830 | ///\pre Either \ref run() or \ref start() must be called before |
---|
| 831 | ///using this function. |
---|
| 832 | Path path(Node t) const { return Path(*G, *_pred, t); } |
---|
[100] | 833 | |
---|
[244] | 834 | ///The distance of a node from the root(s). |
---|
[100] | 835 | |
---|
[244] | 836 | ///Returns the distance of a node from the root(s). |
---|
| 837 | /// |
---|
| 838 | ///\warning If node \c v is not reachable from the root(s), then |
---|
| 839 | ///the return value of this function is undefined. |
---|
| 840 | /// |
---|
| 841 | ///\pre Either \ref run() or \ref start() must be called before |
---|
| 842 | ///using this function. |
---|
[100] | 843 | Value dist(Node v) const { return (*_dist)[v]; } |
---|
| 844 | |
---|
[244] | 845 | ///Returns the 'previous arc' of the shortest path tree for a node. |
---|
[100] | 846 | |
---|
[244] | 847 | ///This function returns the 'previous arc' of the shortest path |
---|
| 848 | ///tree for the node \c v, i.e. it returns the last arc of a |
---|
| 849 | ///shortest path from the root(s) to \c v. It is \c INVALID if \c v |
---|
| 850 | ///is not reachable from the root(s) or if \c v is a root. |
---|
| 851 | /// |
---|
| 852 | ///The shortest path tree used here is equal to the shortest path |
---|
| 853 | ///tree used in \ref predNode(). |
---|
| 854 | /// |
---|
| 855 | ///\pre Either \ref run() or \ref start() must be called before |
---|
| 856 | ///using this function. |
---|
[100] | 857 | Arc predArc(Node v) const { return (*_pred)[v]; } |
---|
| 858 | |
---|
[244] | 859 | ///Returns the 'previous node' of the shortest path tree for a node. |
---|
[100] | 860 | |
---|
[244] | 861 | ///This function returns the 'previous node' of the shortest path |
---|
| 862 | ///tree for the node \c v, i.e. it returns the last but one node |
---|
| 863 | ///from a shortest path from the root(s) to \c v. It is \c INVALID |
---|
| 864 | ///if \c v is not reachable from the root(s) or if \c v is a root. |
---|
| 865 | /// |
---|
| 866 | ///The shortest path tree used here is equal to the shortest path |
---|
| 867 | ///tree used in \ref predArc(). |
---|
| 868 | /// |
---|
| 869 | ///\pre Either \ref run() or \ref start() must be called before |
---|
[100] | 870 | ///using this function. |
---|
| 871 | Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID: |
---|
[209] | 872 | G->source((*_pred)[v]); } |
---|
| 873 | |
---|
[244] | 874 | ///\brief Returns a const reference to the node map that stores the |
---|
| 875 | ///distances of the nodes. |
---|
| 876 | /// |
---|
| 877 | ///Returns a const reference to the node map that stores the distances |
---|
| 878 | ///of the nodes calculated by the algorithm. |
---|
| 879 | /// |
---|
| 880 | ///\pre Either \ref run() or \ref init() |
---|
| 881 | ///must be called before using this function. |
---|
[100] | 882 | const DistMap &distMap() const { return *_dist;} |
---|
[209] | 883 | |
---|
[244] | 884 | ///\brief Returns a const reference to the node map that stores the |
---|
| 885 | ///predecessor arcs. |
---|
| 886 | /// |
---|
| 887 | ///Returns a const reference to the node map that stores the predecessor |
---|
| 888 | ///arcs, which form the shortest path tree. |
---|
| 889 | /// |
---|
| 890 | ///\pre Either \ref run() or \ref init() |
---|
| 891 | ///must be called before using this function. |
---|
[100] | 892 | const PredMap &predMap() const { return *_pred;} |
---|
[209] | 893 | |
---|
[244] | 894 | ///Checks if a node is reachable from the root(s). |
---|
[100] | 895 | |
---|
[244] | 896 | ///Returns \c true if \c v is reachable from the root(s). |
---|
| 897 | ///\pre Either \ref run() or \ref start() |
---|
| 898 | ///must be called before using this function. |
---|
| 899 | bool reached(Node v) const { return (*_heap_cross_ref)[v] != |
---|
| 900 | Heap::PRE_HEAP; } |
---|
[100] | 901 | |
---|
| 902 | ///Checks if a node is processed. |
---|
| 903 | |
---|
| 904 | ///Returns \c true if \c v is processed, i.e. the shortest |
---|
| 905 | ///path to \c v has already found. |
---|
[244] | 906 | ///\pre Either \ref run() or \ref start() |
---|
| 907 | ///must be called before using this function. |
---|
| 908 | bool processed(Node v) const { return (*_heap_cross_ref)[v] == |
---|
| 909 | Heap::POST_HEAP; } |
---|
| 910 | |
---|
| 911 | ///The current distance of a node from the root(s). |
---|
| 912 | |
---|
| 913 | ///Returns the current distance of a node from the root(s). |
---|
| 914 | ///It may be decreased in the following processes. |
---|
| 915 | ///\pre \c v should be reached but not processed. |
---|
| 916 | Value currentDist(Node v) const { return (*_heap)[v]; } |
---|
[209] | 917 | |
---|
[100] | 918 | ///@} |
---|
| 919 | }; |
---|
| 920 | |
---|
| 921 | |
---|
[244] | 922 | ///Default traits class of dijkstra() function. |
---|
[100] | 923 | |
---|
[244] | 924 | ///Default traits class of dijkstra() function. |
---|
| 925 | ///\tparam GR The type of the digraph. |
---|
| 926 | ///\tparam LM The type of the length map. |
---|
[100] | 927 | template<class GR, class LM> |
---|
| 928 | struct DijkstraWizardDefaultTraits |
---|
| 929 | { |
---|
[244] | 930 | ///The type of the digraph the algorithm runs on. |
---|
[100] | 931 | typedef GR Digraph; |
---|
| 932 | ///The type of the map that stores the arc lengths. |
---|
| 933 | |
---|
| 934 | ///The type of the map that stores the arc lengths. |
---|
| 935 | ///It must meet the \ref concepts::ReadMap "ReadMap" concept. |
---|
| 936 | typedef LM LengthMap; |
---|
[244] | 937 | ///The type of the length of the arcs. |
---|
[100] | 938 | typedef typename LM::Value Value; |
---|
[244] | 939 | |
---|
[100] | 940 | /// Operation traits for Dijkstra algorithm. |
---|
| 941 | |
---|
[244] | 942 | /// This class defines the operations that are used in the algorithm. |
---|
[100] | 943 | /// \see DijkstraDefaultOperationTraits |
---|
| 944 | typedef DijkstraDefaultOperationTraits<Value> OperationTraits; |
---|
| 945 | |
---|
[244] | 946 | /// The cross reference type used by the heap. |
---|
[100] | 947 | |
---|
[244] | 948 | /// The cross reference type used by the heap. |
---|
[100] | 949 | /// Usually it is \c Digraph::NodeMap<int>. |
---|
| 950 | typedef typename Digraph::template NodeMap<int> HeapCrossRef; |
---|
[244] | 951 | ///Instantiates a \ref HeapCrossRef. |
---|
[100] | 952 | |
---|
[209] | 953 | ///This function instantiates a \ref HeapCrossRef. |
---|
[244] | 954 | /// \param g is the digraph, to which we would like to define the |
---|
[100] | 955 | /// HeapCrossRef. |
---|
[244] | 956 | static HeapCrossRef *createHeapCrossRef(const Digraph &g) |
---|
[100] | 957 | { |
---|
[244] | 958 | return new HeapCrossRef(g); |
---|
[100] | 959 | } |
---|
[209] | 960 | |
---|
[244] | 961 | ///The heap type used by the Dijkstra algorithm. |
---|
[100] | 962 | |
---|
[244] | 963 | ///The heap type used by the Dijkstra algorithm. |
---|
[100] | 964 | /// |
---|
| 965 | ///\sa BinHeap |
---|
| 966 | ///\sa Dijkstra |
---|
[244] | 967 | typedef BinHeap<Value, typename Digraph::template NodeMap<int>, |
---|
[209] | 968 | std::less<Value> > Heap; |
---|
[100] | 969 | |
---|
[244] | 970 | ///Instantiates a \ref Heap. |
---|
| 971 | |
---|
| 972 | ///This function instantiates a \ref Heap. |
---|
| 973 | /// \param r is the HeapCrossRef which is used. |
---|
| 974 | static Heap *createHeap(HeapCrossRef& r) |
---|
[100] | 975 | { |
---|
[244] | 976 | return new Heap(r); |
---|
[100] | 977 | } |
---|
| 978 | |
---|
[244] | 979 | ///\brief The type of the map that stores the predecessor |
---|
[100] | 980 | ///arcs of the shortest paths. |
---|
[209] | 981 | /// |
---|
[244] | 982 | ///The type of the map that stores the predecessor |
---|
[100] | 983 | ///arcs of the shortest paths. |
---|
| 984 | ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
---|
[244] | 985 | typedef NullMap <typename Digraph::Node,typename Digraph::Arc> PredMap; |
---|
| 986 | ///Instantiates a \ref PredMap. |
---|
[209] | 987 | |
---|
| 988 | ///This function instantiates a \ref PredMap. |
---|
[244] | 989 | ///\param g is the digraph, to which we would like to define the |
---|
| 990 | ///\ref PredMap. |
---|
[100] | 991 | #ifdef DOXYGEN |
---|
[244] | 992 | static PredMap *createPredMap(const Digraph &g) |
---|
[100] | 993 | #else |
---|
[244] | 994 | static PredMap *createPredMap(const Digraph &) |
---|
[100] | 995 | #endif |
---|
| 996 | { |
---|
| 997 | return new PredMap(); |
---|
| 998 | } |
---|
[209] | 999 | |
---|
[244] | 1000 | ///The type of the map that indicates which nodes are processed. |
---|
| 1001 | |
---|
| 1002 | ///The type of the map that indicates which nodes are processed. |
---|
[100] | 1003 | ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
---|
| 1004 | ///By default it is a NullMap. |
---|
| 1005 | typedef NullMap<typename Digraph::Node,bool> ProcessedMap; |
---|
[244] | 1006 | ///Instantiates a \ref ProcessedMap. |
---|
[209] | 1007 | |
---|
| 1008 | ///This function instantiates a \ref ProcessedMap. |
---|
[100] | 1009 | ///\param g is the digraph, to which |
---|
[244] | 1010 | ///we would like to define the \ref ProcessedMap. |
---|
[100] | 1011 | #ifdef DOXYGEN |
---|
[244] | 1012 | static ProcessedMap *createProcessedMap(const Digraph &g) |
---|
[100] | 1013 | #else |
---|
[244] | 1014 | static ProcessedMap *createProcessedMap(const Digraph &) |
---|
[100] | 1015 | #endif |
---|
| 1016 | { |
---|
| 1017 | return new ProcessedMap(); |
---|
| 1018 | } |
---|
[209] | 1019 | |
---|
[244] | 1020 | ///The type of the map that stores the distances of the nodes. |
---|
| 1021 | |
---|
| 1022 | ///The type of the map that stores the distances of the nodes. |
---|
[100] | 1023 | ///It must meet the \ref concepts::WriteMap "WriteMap" concept. |
---|
[244] | 1024 | typedef NullMap<typename Digraph::Node,Value> DistMap; |
---|
| 1025 | ///Instantiates a \ref DistMap. |
---|
[209] | 1026 | |
---|
| 1027 | ///This function instantiates a \ref DistMap. |
---|
[210] | 1028 | ///\param g is the digraph, to which we would like to define |
---|
| 1029 | ///the \ref DistMap |
---|
[100] | 1030 | #ifdef DOXYGEN |
---|
[244] | 1031 | static DistMap *createDistMap(const Digraph &g) |
---|
[100] | 1032 | #else |
---|
[244] | 1033 | static DistMap *createDistMap(const Digraph &) |
---|
[100] | 1034 | #endif |
---|
| 1035 | { |
---|
| 1036 | return new DistMap(); |
---|
| 1037 | } |
---|
| 1038 | }; |
---|
[209] | 1039 | |
---|
[244] | 1040 | /// Default traits class used by \ref DijkstraWizard |
---|
[100] | 1041 | |
---|
| 1042 | /// To make it easier to use Dijkstra algorithm |
---|
[244] | 1043 | /// we have created a wizard class. |
---|
[100] | 1044 | /// This \ref DijkstraWizard class needs default traits, |
---|
[244] | 1045 | /// as well as the \ref Dijkstra class. |
---|
[100] | 1046 | /// The \ref DijkstraWizardBase is a class to be the default traits of the |
---|
| 1047 | /// \ref DijkstraWizard class. |
---|
| 1048 | template<class GR,class LM> |
---|
| 1049 | class DijkstraWizardBase : public DijkstraWizardDefaultTraits<GR,LM> |
---|
| 1050 | { |
---|
| 1051 | typedef DijkstraWizardDefaultTraits<GR,LM> Base; |
---|
| 1052 | protected: |
---|
[244] | 1053 | //The type of the nodes in the digraph. |
---|
[100] | 1054 | typedef typename Base::Digraph::Node Node; |
---|
| 1055 | |
---|
[244] | 1056 | //Pointer to the digraph the algorithm runs on. |
---|
[100] | 1057 | void *_g; |
---|
[244] | 1058 | //Pointer to the length map |
---|
[100] | 1059 | void *_length; |
---|
[251] | 1060 | //Pointer to the map of processed nodes. |
---|
| 1061 | void *_processed; |
---|
[244] | 1062 | //Pointer to the map of predecessors arcs. |
---|
[100] | 1063 | void *_pred; |
---|
[244] | 1064 | //Pointer to the map of distances. |
---|
[100] | 1065 | void *_dist; |
---|
[244] | 1066 | //Pointer to the source node. |
---|
[100] | 1067 | Node _source; |
---|
| 1068 | |
---|
[244] | 1069 | public: |
---|
[100] | 1070 | /// Constructor. |
---|
[209] | 1071 | |
---|
[100] | 1072 | /// This constructor does not require parameters, therefore it initiates |
---|
| 1073 | /// all of the attributes to default values (0, INVALID). |
---|
[251] | 1074 | DijkstraWizardBase() : _g(0), _length(0), _processed(0), _pred(0), |
---|
[209] | 1075 | _dist(0), _source(INVALID) {} |
---|
[100] | 1076 | |
---|
| 1077 | /// Constructor. |
---|
[209] | 1078 | |
---|
[100] | 1079 | /// This constructor requires some parameters, |
---|
| 1080 | /// listed in the parameters list. |
---|
| 1081 | /// Others are initiated to 0. |
---|
[244] | 1082 | /// \param g The digraph the algorithm runs on. |
---|
| 1083 | /// \param l The length map. |
---|
| 1084 | /// \param s The source node. |
---|
[100] | 1085 | DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) : |
---|
[209] | 1086 | _g(reinterpret_cast<void*>(const_cast<GR*>(&g))), |
---|
| 1087 | _length(reinterpret_cast<void*>(const_cast<LM*>(&l))), |
---|
[251] | 1088 | _processed(0), _pred(0), _dist(0), _source(s) {} |
---|
[100] | 1089 | |
---|
| 1090 | }; |
---|
[209] | 1091 | |
---|
[244] | 1092 | /// Auxiliary class for the function type interface of Dijkstra algorithm. |
---|
[100] | 1093 | |
---|
[244] | 1094 | /// This auxiliary class is created to implement the function type |
---|
| 1095 | /// interface of \ref Dijkstra algorithm. It uses the functions and features |
---|
| 1096 | /// of the plain \ref Dijkstra, but it is much simpler to use it. |
---|
| 1097 | /// It should only be used through the \ref dijkstra() function, which makes |
---|
| 1098 | /// it easier to use the algorithm. |
---|
[100] | 1099 | /// |
---|
| 1100 | /// Simplicity means that the way to change the types defined |
---|
| 1101 | /// in the traits class is based on functions that returns the new class |
---|
| 1102 | /// and not on templatable built-in classes. |
---|
| 1103 | /// When using the plain \ref Dijkstra |
---|
| 1104 | /// the new class with the modified type comes from |
---|
| 1105 | /// the original class by using the :: |
---|
| 1106 | /// operator. In the case of \ref DijkstraWizard only |
---|
[244] | 1107 | /// a function have to be called, and it will |
---|
[100] | 1108 | /// return the needed class. |
---|
| 1109 | /// |
---|
[244] | 1110 | /// It does not have own \ref run() method. When its \ref run() method |
---|
| 1111 | /// is called, it initiates a plain \ref Dijkstra object, and calls the |
---|
| 1112 | /// \ref Dijkstra::run() method of it. |
---|
[100] | 1113 | template<class TR> |
---|
| 1114 | class DijkstraWizard : public TR |
---|
| 1115 | { |
---|
| 1116 | typedef TR Base; |
---|
| 1117 | |
---|
[244] | 1118 | ///The type of the digraph the algorithm runs on. |
---|
[100] | 1119 | typedef typename TR::Digraph Digraph; |
---|
[244] | 1120 | |
---|
[100] | 1121 | typedef typename Digraph::Node Node; |
---|
| 1122 | typedef typename Digraph::NodeIt NodeIt; |
---|
| 1123 | typedef typename Digraph::Arc Arc; |
---|
| 1124 | typedef typename Digraph::OutArcIt OutArcIt; |
---|
[209] | 1125 | |
---|
[100] | 1126 | ///The type of the map that stores the arc lengths. |
---|
| 1127 | typedef typename TR::LengthMap LengthMap; |
---|
| 1128 | ///The type of the length of the arcs. |
---|
| 1129 | typedef typename LengthMap::Value Value; |
---|
[244] | 1130 | ///\brief The type of the map that stores the predecessor |
---|
[100] | 1131 | ///arcs of the shortest paths. |
---|
| 1132 | typedef typename TR::PredMap PredMap; |
---|
[244] | 1133 | ///The type of the map that stores the distances of the nodes. |
---|
[100] | 1134 | typedef typename TR::DistMap DistMap; |
---|
[244] | 1135 | ///The type of the map that indicates which nodes are processed. |
---|
| 1136 | typedef typename TR::ProcessedMap ProcessedMap; |
---|
[100] | 1137 | ///The heap type used by the dijkstra algorithm. |
---|
| 1138 | typedef typename TR::Heap Heap; |
---|
[244] | 1139 | |
---|
[100] | 1140 | public: |
---|
[244] | 1141 | |
---|
[100] | 1142 | /// Constructor. |
---|
| 1143 | DijkstraWizard() : TR() {} |
---|
| 1144 | |
---|
| 1145 | /// Constructor that requires parameters. |
---|
| 1146 | |
---|
| 1147 | /// Constructor that requires parameters. |
---|
| 1148 | /// These parameters will be the default values for the traits class. |
---|
| 1149 | DijkstraWizard(const Digraph &g,const LengthMap &l, Node s=INVALID) : |
---|
| 1150 | TR(g,l,s) {} |
---|
| 1151 | |
---|
| 1152 | ///Copy constructor |
---|
| 1153 | DijkstraWizard(const TR &b) : TR(b) {} |
---|
| 1154 | |
---|
| 1155 | ~DijkstraWizard() {} |
---|
| 1156 | |
---|
[244] | 1157 | ///Runs Dijkstra algorithm from a source node. |
---|
[209] | 1158 | |
---|
[244] | 1159 | ///Runs Dijkstra algorithm from a source node. |
---|
| 1160 | ///The node can be given with the \ref source() function. |
---|
[100] | 1161 | void run() |
---|
| 1162 | { |
---|
| 1163 | if(Base::_source==INVALID) throw UninitializedParameter(); |
---|
[209] | 1164 | Dijkstra<Digraph,LengthMap,TR> |
---|
| 1165 | dij(*reinterpret_cast<const Digraph*>(Base::_g), |
---|
[100] | 1166 | *reinterpret_cast<const LengthMap*>(Base::_length)); |
---|
[251] | 1167 | if(Base::_processed) |
---|
| 1168 | dij.processedMap(*reinterpret_cast<ProcessedMap*>(Base::_processed)); |
---|
| 1169 | if(Base::_pred) |
---|
| 1170 | dij.predMap(*reinterpret_cast<PredMap*>(Base::_pred)); |
---|
| 1171 | if(Base::_dist) |
---|
| 1172 | dij.distMap(*reinterpret_cast<DistMap*>(Base::_dist)); |
---|
[100] | 1173 | dij.run(Base::_source); |
---|
| 1174 | } |
---|
| 1175 | |
---|
| 1176 | ///Runs Dijkstra algorithm from the given node. |
---|
| 1177 | |
---|
| 1178 | ///Runs Dijkstra algorithm from the given node. |
---|
| 1179 | ///\param s is the given source. |
---|
| 1180 | void run(Node s) |
---|
| 1181 | { |
---|
| 1182 | Base::_source=s; |
---|
| 1183 | run(); |
---|
| 1184 | } |
---|
| 1185 | |
---|
[244] | 1186 | /// Sets the source node, from which the Dijkstra algorithm runs. |
---|
| 1187 | |
---|
| 1188 | /// Sets the source node, from which the Dijkstra algorithm runs. |
---|
| 1189 | /// \param s is the source node. |
---|
| 1190 | DijkstraWizard<TR> &source(Node s) |
---|
| 1191 | { |
---|
| 1192 | Base::_source=s; |
---|
| 1193 | return *this; |
---|
| 1194 | } |
---|
| 1195 | |
---|
[100] | 1196 | template<class T> |
---|
[257] | 1197 | struct SetPredMapBase : public Base { |
---|
[100] | 1198 | typedef T PredMap; |
---|
| 1199 | static PredMap *createPredMap(const Digraph &) { return 0; }; |
---|
[257] | 1200 | SetPredMapBase(const TR &b) : TR(b) {} |
---|
[100] | 1201 | }; |
---|
| 1202 | ///\brief \ref named-templ-param "Named parameter" |
---|
[244] | 1203 | ///for setting \ref PredMap object. |
---|
[100] | 1204 | /// |
---|
[244] | 1205 | ///\ref named-templ-param "Named parameter" |
---|
| 1206 | ///for setting \ref PredMap object. |
---|
[100] | 1207 | template<class T> |
---|
[257] | 1208 | DijkstraWizard<SetPredMapBase<T> > predMap(const T &t) |
---|
[100] | 1209 | { |
---|
| 1210 | Base::_pred=reinterpret_cast<void*>(const_cast<T*>(&t)); |
---|
[257] | 1211 | return DijkstraWizard<SetPredMapBase<T> >(*this); |
---|
[100] | 1212 | } |
---|
[209] | 1213 | |
---|
[100] | 1214 | template<class T> |
---|
[257] | 1215 | struct SetProcessedMapBase : public Base { |
---|
[244] | 1216 | typedef T ProcessedMap; |
---|
| 1217 | static ProcessedMap *createProcessedMap(const Digraph &) { return 0; }; |
---|
[257] | 1218 | SetProcessedMapBase(const TR &b) : TR(b) {} |
---|
[244] | 1219 | }; |
---|
| 1220 | ///\brief \ref named-templ-param "Named parameter" |
---|
| 1221 | ///for setting \ref ProcessedMap object. |
---|
| 1222 | /// |
---|
| 1223 | /// \ref named-templ-param "Named parameter" |
---|
| 1224 | ///for setting \ref ProcessedMap object. |
---|
| 1225 | template<class T> |
---|
[257] | 1226 | DijkstraWizard<SetProcessedMapBase<T> > processedMap(const T &t) |
---|
[244] | 1227 | { |
---|
| 1228 | Base::_processed=reinterpret_cast<void*>(const_cast<T*>(&t)); |
---|
[257] | 1229 | return DijkstraWizard<SetProcessedMapBase<T> >(*this); |
---|
[244] | 1230 | } |
---|
| 1231 | |
---|
| 1232 | template<class T> |
---|
[257] | 1233 | struct SetDistMapBase : public Base { |
---|
[100] | 1234 | typedef T DistMap; |
---|
| 1235 | static DistMap *createDistMap(const Digraph &) { return 0; }; |
---|
[257] | 1236 | SetDistMapBase(const TR &b) : TR(b) {} |
---|
[100] | 1237 | }; |
---|
| 1238 | ///\brief \ref named-templ-param "Named parameter" |
---|
[244] | 1239 | ///for setting \ref DistMap object. |
---|
[100] | 1240 | /// |
---|
[244] | 1241 | ///\ref named-templ-param "Named parameter" |
---|
| 1242 | ///for setting \ref DistMap object. |
---|
[100] | 1243 | template<class T> |
---|
[257] | 1244 | DijkstraWizard<SetDistMapBase<T> > distMap(const T &t) |
---|
[100] | 1245 | { |
---|
| 1246 | Base::_dist=reinterpret_cast<void*>(const_cast<T*>(&t)); |
---|
[257] | 1247 | return DijkstraWizard<SetDistMapBase<T> >(*this); |
---|
[100] | 1248 | } |
---|
[209] | 1249 | |
---|
[100] | 1250 | }; |
---|
[209] | 1251 | |
---|
[100] | 1252 | ///Function type interface for Dijkstra algorithm. |
---|
| 1253 | |
---|
| 1254 | /// \ingroup shortest_path |
---|
| 1255 | ///Function type interface for Dijkstra algorithm. |
---|
| 1256 | /// |
---|
| 1257 | ///This function also has several |
---|
| 1258 | ///\ref named-templ-func-param "named parameters", |
---|
| 1259 | ///they are declared as the members of class \ref DijkstraWizard. |
---|
| 1260 | ///The following |
---|
| 1261 | ///example shows how to use these parameters. |
---|
| 1262 | ///\code |
---|
| 1263 | /// dijkstra(g,length,source).predMap(preds).run(); |
---|
| 1264 | ///\endcode |
---|
| 1265 | ///\warning Don't forget to put the \ref DijkstraWizard::run() "run()" |
---|
| 1266 | ///to the end of the parameter list. |
---|
| 1267 | ///\sa DijkstraWizard |
---|
| 1268 | ///\sa Dijkstra |
---|
| 1269 | template<class GR, class LM> |
---|
| 1270 | DijkstraWizard<DijkstraWizardBase<GR,LM> > |
---|
| 1271 | dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID) |
---|
| 1272 | { |
---|
| 1273 | return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s); |
---|
| 1274 | } |
---|
| 1275 | |
---|
| 1276 | } //END OF NAMESPACE LEMON |
---|
| 1277 | |
---|
| 1278 | #endif |
---|