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