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