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