1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/lemon/dijkstra.h Sun Feb 06 20:14:30 2005 +0000
1.3 @@ -0,0 +1,915 @@
1.4 +/* -*- C++ -*-
1.5 + * src/lemon/dijkstra.h - Part of LEMON, a generic C++ optimization library
1.6 + *
1.7 + * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.8 + * (Egervary Combinatorial Optimization Research Group, EGRES).
1.9 + *
1.10 + * Permission to use, modify and distribute this software is granted
1.11 + * provided that this copyright notice appears in all copies. For
1.12 + * precise terms see the accompanying LICENSE file.
1.13 + *
1.14 + * This software is provided "AS IS" with no warranty of any kind,
1.15 + * express or implied, and with no claim as to its suitability for any
1.16 + * purpose.
1.17 + *
1.18 + */
1.19 +
1.20 +#ifndef LEMON_DIJKSTRA_H
1.21 +#define LEMON_DIJKSTRA_H
1.22 +
1.23 +///\ingroup flowalgs
1.24 +///\file
1.25 +///\brief Dijkstra algorithm.
1.26 +
1.27 +#include <lemon/list_graph.h>
1.28 +#include <lemon/bin_heap.h>
1.29 +#include <lemon/invalid.h>
1.30 +#include <lemon/error.h>
1.31 +#include <lemon/maps.h>
1.32 +
1.33 +namespace lemon {
1.34 +
1.35 +
1.36 +/// \addtogroup flowalgs
1.37 +/// @{
1.38 +
1.39 + ///Default traits class of Dijkstra class.
1.40 +
1.41 + ///Default traits class of Dijkstra class.
1.42 + ///\param GR Graph type.
1.43 + ///\param LM Type of length map.
1.44 + template<class GR, class LM>
1.45 + struct DijkstraDefaultTraits
1.46 + {
1.47 + ///The graph type the algorithm runs on.
1.48 + typedef GR Graph;
1.49 + ///The type of the map that stores the edge lengths.
1.50 +
1.51 + ///The type of the map that stores the edge lengths.
1.52 + ///It must meet the \ref concept::ReadMap "ReadMap" concept.
1.53 + typedef LM LengthMap;
1.54 + //The type of the length of the edges.
1.55 + typedef typename LM::Value Value;
1.56 + ///The heap type used by Dijkstra algorithm.
1.57 +
1.58 + ///The heap type used by Dijkstra algorithm.
1.59 + ///
1.60 + ///\sa BinHeap
1.61 + ///\sa Dijkstra
1.62 + typedef BinHeap<typename Graph::Node,
1.63 + typename LM::Value,
1.64 + typename GR::template NodeMap<int>,
1.65 + std::less<Value> > Heap;
1.66 +
1.67 + ///\brief The type of the map that stores the last
1.68 + ///edges of the shortest paths.
1.69 + ///
1.70 + ///The type of the map that stores the last
1.71 + ///edges of the shortest paths.
1.72 + ///It must meet the \ref concept::WriteMap "WriteMap" concept.
1.73 + ///
1.74 + typedef typename Graph::template NodeMap<typename GR::Edge> PredMap;
1.75 + ///Instantiates a PredMap.
1.76 +
1.77 + ///This function instantiates a \ref PredMap.
1.78 + ///\param G is the graph, to which we would like to define the PredMap.
1.79 + ///\todo The graph alone may be insufficient for the initialization
1.80 + static PredMap *createPredMap(const GR &G)
1.81 + {
1.82 + return new PredMap(G);
1.83 + }
1.84 + ///\brief The type of the map that stores the last but one
1.85 + ///nodes of the shortest paths.
1.86 + ///
1.87 + ///The type of the map that stores the last but one
1.88 + ///nodes of the shortest paths.
1.89 + ///It must meet the \ref concept::WriteMap "WriteMap" concept.
1.90 + ///
1.91 + typedef NullMap<typename Graph::Node,typename Graph::Node> PredNodeMap;
1.92 + ///Instantiates a PredNodeMap.
1.93 +
1.94 + ///This function instantiates a \ref PredNodeMap.
1.95 + ///\param G is the graph, to which we would like to define the \ref PredNodeMap
1.96 + static PredNodeMap *createPredNodeMap(const GR &G)
1.97 + {
1.98 + return new PredNodeMap();
1.99 + }
1.100 +
1.101 + ///The type of the map that stores whether a nodes is reached.
1.102 +
1.103 + ///The type of the map that stores whether a nodes is reached.
1.104 + ///It must meet the \ref concept::WriteMap "WriteMap" concept.
1.105 + ///By default it is a NullMap.
1.106 + ///\todo If it is set to a real map, Dijkstra::reached() should read this.
1.107 + ///\todo named parameter to set this type, function to read and write.
1.108 + typedef NullMap<typename Graph::Node,bool> ReachedMap;
1.109 + ///Instantiates a ReachedMap.
1.110 +
1.111 + ///This function instantiates a \ref ReachedMap.
1.112 + ///\param G is the graph, to which we would like to define the \ref ReachedMap
1.113 + static ReachedMap *createReachedMap(const GR &G)
1.114 + {
1.115 + return new ReachedMap();
1.116 + }
1.117 + ///The type of the map that stores the dists of the nodes.
1.118 +
1.119 + ///The type of the map that stores the dists of the nodes.
1.120 + ///It must meet the \ref concept::WriteMap "WriteMap" concept.
1.121 + ///
1.122 + typedef typename Graph::template NodeMap<typename LM::Value> DistMap;
1.123 + ///Instantiates a DistMap.
1.124 +
1.125 + ///This function instantiates a \ref DistMap.
1.126 + ///\param G is the graph, to which we would like to define the \ref DistMap
1.127 + static DistMap *createDistMap(const GR &G)
1.128 + {
1.129 + return new DistMap(G);
1.130 + }
1.131 + };
1.132 +
1.133 + ///%Dijkstra algorithm class.
1.134 +
1.135 + ///This class provides an efficient implementation of %Dijkstra algorithm.
1.136 + ///The edge lengths are passed to the algorithm using a
1.137 + ///\ref concept::ReadMap "ReadMap",
1.138 + ///so it is easy to change it to any kind of length.
1.139 + ///
1.140 + ///The type of the length is determined by the
1.141 + ///\ref concept::ReadMap::Value "Value" of the length map.
1.142 + ///
1.143 + ///It is also possible to change the underlying priority heap.
1.144 + ///
1.145 + ///\param GR The graph type the algorithm runs on. The default value is
1.146 + ///\ref ListGraph. The value of GR is not used directly by Dijkstra, it
1.147 + ///is only passed to \ref DijkstraDefaultTraits.
1.148 + ///\param LM This read-only
1.149 + ///EdgeMap
1.150 + ///determines the
1.151 + ///lengths of the edges. It is read once for each edge, so the map
1.152 + ///may involve in relatively time consuming process to compute the edge
1.153 + ///length if it is necessary. The default map type is
1.154 + ///\ref concept::StaticGraph::EdgeMap "Graph::EdgeMap<int>".
1.155 + ///The value of LM is not used directly by Dijkstra, it
1.156 + ///is only passed to \ref DijkstraDefaultTraits.
1.157 + ///\param TR Traits class to set various data types used by the algorithm.
1.158 + ///The default traits class is
1.159 + ///\ref DijkstraDefaultTraits "DijkstraDefaultTraits<GR,LM>".
1.160 + ///See \ref DijkstraDefaultTraits for the documentation of
1.161 + ///a Dijkstra traits class.
1.162 + ///
1.163 + ///\author Jacint Szabo and Alpar Juttner
1.164 + ///\todo A compare object would be nice.
1.165 +
1.166 +#ifdef DOXYGEN
1.167 + template <typename GR,
1.168 + typename LM,
1.169 + typename TR>
1.170 +#else
1.171 + template <typename GR=ListGraph,
1.172 + typename LM=typename GR::template EdgeMap<int>,
1.173 + typename TR=DijkstraDefaultTraits<GR,LM> >
1.174 +#endif
1.175 + class Dijkstra {
1.176 + public:
1.177 + /**
1.178 + * \brief \ref Exception for uninitialized parameters.
1.179 + *
1.180 + * This error represents problems in the initialization
1.181 + * of the parameters of the algorithms.
1.182 + */
1.183 + class UninitializedParameter : public lemon::UninitializedParameter {
1.184 + public:
1.185 + virtual const char* exceptionName() const {
1.186 + return "lemon::Dijsktra::UninitializedParameter";
1.187 + }
1.188 + };
1.189 +
1.190 + typedef TR Traits;
1.191 + ///The type of the underlying graph.
1.192 + typedef typename TR::Graph Graph;
1.193 + ///\e
1.194 + typedef typename Graph::Node Node;
1.195 + ///\e
1.196 + typedef typename Graph::NodeIt NodeIt;
1.197 + ///\e
1.198 + typedef typename Graph::Edge Edge;
1.199 + ///\e
1.200 + typedef typename Graph::OutEdgeIt OutEdgeIt;
1.201 +
1.202 + ///The type of the length of the edges.
1.203 + typedef typename TR::LengthMap::Value Value;
1.204 + ///The type of the map that stores the edge lengths.
1.205 + typedef typename TR::LengthMap LengthMap;
1.206 + ///\brief The type of the map that stores the last
1.207 + ///edges of the shortest paths.
1.208 + typedef typename TR::PredMap PredMap;
1.209 + ///\brief The type of the map that stores the last but one
1.210 + ///nodes of the shortest paths.
1.211 + typedef typename TR::PredNodeMap PredNodeMap;
1.212 + ///The type of the map indicating if a node is reached.
1.213 + typedef typename TR::ReachedMap ReachedMap;
1.214 + ///The type of the map that stores the dists of the nodes.
1.215 + typedef typename TR::DistMap DistMap;
1.216 + ///The heap type used by the dijkstra algorithm.
1.217 + typedef typename TR::Heap Heap;
1.218 + private:
1.219 + /// Pointer to the underlying graph.
1.220 + const Graph *G;
1.221 + /// Pointer to the length map
1.222 + const LengthMap *length;
1.223 + ///Pointer to the map of predecessors edges.
1.224 + PredMap *_pred;
1.225 + ///Indicates if \ref _pred is locally allocated (\c true) or not.
1.226 + bool local_pred;
1.227 + ///Pointer to the map of predecessors nodes.
1.228 + PredNodeMap *_predNode;
1.229 + ///Indicates if \ref _predNode is locally allocated (\c true) or not.
1.230 + bool local_predNode;
1.231 + ///Pointer to the map of distances.
1.232 + DistMap *_dist;
1.233 + ///Indicates if \ref _dist is locally allocated (\c true) or not.
1.234 + bool local_dist;
1.235 + ///Pointer to the map of reached status of the nodes.
1.236 + ReachedMap *_reached;
1.237 + ///Indicates if \ref _reached is locally allocated (\c true) or not.
1.238 + bool local_reached;
1.239 +
1.240 + ///The source node of the last execution.
1.241 + Node source;
1.242 +
1.243 + ///Creates the maps if necessary.
1.244 +
1.245 + ///\todo Error if \c G or are \c NULL. What about \c length?
1.246 + ///\todo Better memory allocation (instead of new).
1.247 + void create_maps()
1.248 + {
1.249 + if(!_pred) {
1.250 + local_pred = true;
1.251 + _pred = Traits::createPredMap(*G);
1.252 + }
1.253 + if(!_predNode) {
1.254 + local_predNode = true;
1.255 + _predNode = Traits::createPredNodeMap(*G);
1.256 + }
1.257 + if(!_dist) {
1.258 + local_dist = true;
1.259 + _dist = Traits::createDistMap(*G);
1.260 + }
1.261 + if(!_reached) {
1.262 + local_reached = true;
1.263 + _reached = Traits::createReachedMap(*G);
1.264 + }
1.265 + }
1.266 +
1.267 + public :
1.268 +
1.269 + ///\name Named template parameters
1.270 +
1.271 + ///@{
1.272 +
1.273 + template <class T>
1.274 + struct DefPredMapTraits : public Traits {
1.275 + typedef T PredMap;
1.276 + static PredMap *createPredMap(const Graph &G)
1.277 + {
1.278 + throw UninitializedParameter();
1.279 + }
1.280 + };
1.281 + ///\ref named-templ-param "Named parameter" for setting PredMap type
1.282 +
1.283 + ///\ref named-templ-param "Named parameter" for setting PredMap type
1.284 + ///
1.285 + template <class T>
1.286 + class DefPredMap : public Dijkstra< Graph,
1.287 + LengthMap,
1.288 + DefPredMapTraits<T> > { };
1.289 +
1.290 + template <class T>
1.291 + struct DefPredNodeMapTraits : public Traits {
1.292 + typedef T PredNodeMap;
1.293 + static PredNodeMap *createPredNodeMap(const Graph &G)
1.294 + {
1.295 + throw UninitializedParameter();
1.296 + }
1.297 + };
1.298 + ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
1.299 +
1.300 + ///\ref named-templ-param "Named parameter" for setting PredNodeMap type
1.301 + ///
1.302 + template <class T>
1.303 + class DefPredNodeMap : public Dijkstra< Graph,
1.304 + LengthMap,
1.305 + DefPredNodeMapTraits<T> > { };
1.306 +
1.307 + template <class T>
1.308 + struct DefDistMapTraits : public Traits {
1.309 + typedef T DistMap;
1.310 + static DistMap *createDistMap(const Graph &G)
1.311 + {
1.312 + throw UninitializedParameter();
1.313 + }
1.314 + };
1.315 + ///\ref named-templ-param "Named parameter" for setting DistMap type
1.316 +
1.317 + ///\ref named-templ-param "Named parameter" for setting DistMap type
1.318 + ///
1.319 + template <class T>
1.320 + class DefDistMap : public Dijkstra< Graph,
1.321 + LengthMap,
1.322 + DefDistMapTraits<T> > { };
1.323 +
1.324 + template <class T>
1.325 + struct DefReachedMapTraits : public Traits {
1.326 + typedef T ReachedMap;
1.327 + static ReachedMap *createReachedMap(const Graph &G)
1.328 + {
1.329 + throw UninitializedParameter();
1.330 + }
1.331 + };
1.332 + ///\ref named-templ-param "Named parameter" for setting ReachedMap type
1.333 +
1.334 + ///\ref named-templ-param "Named parameter" for setting ReachedMap type
1.335 + ///
1.336 + template <class T>
1.337 + class DefReachedMap : public Dijkstra< Graph,
1.338 + LengthMap,
1.339 + DefReachedMapTraits<T> > { };
1.340 +
1.341 + struct DefGraphReachedMapTraits : public Traits {
1.342 + typedef typename Graph::NodeMap<bool> ReachedMap;
1.343 + static ReachedMap *createReachedMap(const Graph &G)
1.344 + {
1.345 + return new ReachedMap(G);
1.346 + }
1.347 + };
1.348 + ///\brief \ref named-templ-param "Named parameter"
1.349 + ///for setting the ReachedMap type to be Graph::NodeMap<bool>.
1.350 + ///
1.351 + ///\ref named-templ-param "Named parameter"
1.352 + ///for setting the ReachedMap type to be Graph::NodeMap<bool>.
1.353 + ///If you don't set it explicitely, it will be automatically allocated.
1.354 + template <class T>
1.355 + class DefReachedMapToBeDefaultMap :
1.356 + public Dijkstra< Graph,
1.357 + LengthMap,
1.358 + DefGraphReachedMapTraits> { };
1.359 +
1.360 + ///@}
1.361 +
1.362 +
1.363 + private:
1.364 + typename Graph::template NodeMap<int> _heap_map;
1.365 + Heap _heap;
1.366 + public:
1.367 +
1.368 + ///Constructor.
1.369 +
1.370 + ///\param _G the graph the algorithm will run on.
1.371 + ///\param _length the length map used by the algorithm.
1.372 + Dijkstra(const Graph& _G, const LengthMap& _length) :
1.373 + G(&_G), length(&_length),
1.374 + _pred(NULL), local_pred(false),
1.375 + _predNode(NULL), local_predNode(false),
1.376 + _dist(NULL), local_dist(false),
1.377 + _reached(NULL), local_reached(false),
1.378 + _heap_map(*G,-1),_heap(_heap_map)
1.379 + { }
1.380 +
1.381 + ///Destructor.
1.382 + ~Dijkstra()
1.383 + {
1.384 + if(local_pred) delete _pred;
1.385 + if(local_predNode) delete _predNode;
1.386 + if(local_dist) delete _dist;
1.387 + if(local_reached) delete _reached;
1.388 + }
1.389 +
1.390 + ///Sets the length map.
1.391 +
1.392 + ///Sets the length map.
1.393 + ///\return <tt> (*this) </tt>
1.394 + Dijkstra &lengthMap(const LengthMap &m)
1.395 + {
1.396 + length = &m;
1.397 + return *this;
1.398 + }
1.399 +
1.400 + ///Sets the map storing the predecessor edges.
1.401 +
1.402 + ///Sets the map storing the predecessor edges.
1.403 + ///If you don't use this function before calling \ref run(),
1.404 + ///it will allocate one. The destuctor deallocates this
1.405 + ///automatically allocated map, of course.
1.406 + ///\return <tt> (*this) </tt>
1.407 + Dijkstra &predMap(PredMap &m)
1.408 + {
1.409 + if(local_pred) {
1.410 + delete _pred;
1.411 + local_pred=false;
1.412 + }
1.413 + _pred = &m;
1.414 + return *this;
1.415 + }
1.416 +
1.417 + ///Sets the map storing the predecessor nodes.
1.418 +
1.419 + ///Sets the map storing the predecessor nodes.
1.420 + ///If you don't use this function before calling \ref run(),
1.421 + ///it will allocate one. The destuctor deallocates this
1.422 + ///automatically allocated map, of course.
1.423 + ///\return <tt> (*this) </tt>
1.424 + Dijkstra &predNodeMap(PredNodeMap &m)
1.425 + {
1.426 + if(local_predNode) {
1.427 + delete _predNode;
1.428 + local_predNode=false;
1.429 + }
1.430 + _predNode = &m;
1.431 + return *this;
1.432 + }
1.433 +
1.434 + ///Sets the map storing the distances calculated by the algorithm.
1.435 +
1.436 + ///Sets the map storing the distances calculated by the algorithm.
1.437 + ///If you don't use this function before calling \ref run(),
1.438 + ///it will allocate one. The destuctor deallocates this
1.439 + ///automatically allocated map, of course.
1.440 + ///\return <tt> (*this) </tt>
1.441 + Dijkstra &distMap(DistMap &m)
1.442 + {
1.443 + if(local_dist) {
1.444 + delete _dist;
1.445 + local_dist=false;
1.446 + }
1.447 + _dist = &m;
1.448 + return *this;
1.449 + }
1.450 +
1.451 + private:
1.452 + void finalizeNodeData(Node v,Value dst)
1.453 + {
1.454 + _reached->set(v,true);
1.455 + _dist->set(v, dst);
1.456 + _predNode->set(v,G->source((*_pred)[v]));
1.457 + }
1.458 +
1.459 + public:
1.460 + ///\name Excetution control
1.461 + ///The simplest way to execute the algorithm is to use
1.462 + ///\ref run().
1.463 + ///\n
1.464 + ///It you need more control on the execution,
1.465 + ///first you must call \ref init(), then you can add several source nodes
1.466 + ///with \ref addSource(). Finally \ref start() will perform the actual path
1.467 + ///computation.
1.468 +
1.469 + ///@{
1.470 +
1.471 + ///Initializes the internal data structures.
1.472 +
1.473 + ///Initializes the internal data structures.
1.474 + ///
1.475 + ///\todo _heap_map's type could also be in the traits class.
1.476 + void init()
1.477 + {
1.478 + create_maps();
1.479 +
1.480 + for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
1.481 + _pred->set(u,INVALID);
1.482 + _predNode->set(u,INVALID);
1.483 + ///\todo *_reached is not set to false.
1.484 + _heap_map.set(u,Heap::PRE_HEAP);
1.485 + }
1.486 + }
1.487 +
1.488 + ///Adds a new source node.
1.489 +
1.490 + ///Adds a new source node the the priority heap.
1.491 + ///It checks if the node has already been added to the heap.
1.492 + ///
1.493 + ///The optional second parameter is the initial distance of the node.
1.494 + ///
1.495 + ///\todo Do we really want to check it?
1.496 + void addSource(Node s,Value dst=0)
1.497 + {
1.498 + source = s;
1.499 + if(_heap.state(s) != Heap::IN_HEAP) _heap.push(s,dst);
1.500 + }
1.501 +
1.502 + void processNode()
1.503 + {
1.504 + Node v=_heap.top();
1.505 + Value oldvalue=_heap[v];
1.506 + _heap.pop();
1.507 + finalizeNodeData(v,oldvalue);
1.508 +
1.509 + for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
1.510 + Node w=G->target(e);
1.511 + switch(_heap.state(w)) {
1.512 + case Heap::PRE_HEAP:
1.513 + _heap.push(w,oldvalue+(*length)[e]);
1.514 + _pred->set(w,e);
1.515 +// _predNode->set(w,v);
1.516 + break;
1.517 + case Heap::IN_HEAP:
1.518 + if ( oldvalue+(*length)[e] < _heap[w] ) {
1.519 + _heap.decrease(w, oldvalue+(*length)[e]);
1.520 + _pred->set(w,e);
1.521 +// _predNode->set(w,v);
1.522 + }
1.523 + break;
1.524 + case Heap::POST_HEAP:
1.525 + break;
1.526 + }
1.527 + }
1.528 + }
1.529 +
1.530 + ///Executes the algorithm.
1.531 +
1.532 + ///Executes the algorithm.
1.533 + ///
1.534 + ///\pre init() must be called and at least one node should be added
1.535 + ///with addSource() before using this function.
1.536 + ///
1.537 + ///This method runs the %Dijkstra algorithm from the root node(s)
1.538 + ///in order to
1.539 + ///compute the
1.540 + ///shortest path to each node. The algorithm computes
1.541 + ///- The shortest path tree.
1.542 + ///- The distance of each node from the root(s).
1.543 + ///
1.544 + void start()
1.545 + {
1.546 + while ( !_heap.empty() ) processNode();
1.547 + }
1.548 +
1.549 + ///Executes the algorithm until \c dest is reached.
1.550 +
1.551 + ///Executes the algorithm until \c dest is reached.
1.552 + ///
1.553 + ///\pre init() must be called and at least one node should be added
1.554 + ///with addSource() before using this function.
1.555 + ///
1.556 + ///This method runs the %Dijkstra algorithm from the root node(s)
1.557 + ///in order to
1.558 + ///compute the
1.559 + ///shortest path to \c dest. The algorithm computes
1.560 + ///- The shortest path to \c dest.
1.561 + ///- The distance of \c dest from the root(s).
1.562 + ///
1.563 + void start(Node dest)
1.564 + {
1.565 + while ( !_heap.empty() && _heap.top()!=dest ) processNode();
1.566 + if ( _heap.top()==dest ) finalizeNodeData(_heap.top());
1.567 + }
1.568 +
1.569 + ///Executes the algorithm until a condition is met.
1.570 +
1.571 + ///Executes the algorithm until a condition is met.
1.572 + ///
1.573 + ///\pre init() must be called and at least one node should be added
1.574 + ///with addSource() before using this function.
1.575 + ///
1.576 + ///\param nm must be a bool (or convertible) node map. The algorithm
1.577 + ///will stop when it reaches a node \c v with <tt>nm[v]==true</tt>.
1.578 + template<class NM>
1.579 + void start(const NM &nm)
1.580 + {
1.581 + while ( !_heap.empty() && !mn[_heap.top()] ) processNode();
1.582 + if ( !_heap.empty() ) finalizeNodeData(_heap.top());
1.583 + }
1.584 +
1.585 + ///Runs %Dijkstra algorithm from node \c s.
1.586 +
1.587 + ///This method runs the %Dijkstra algorithm from a root node \c s
1.588 + ///in order to
1.589 + ///compute the
1.590 + ///shortest path to each node. The algorithm computes
1.591 + ///- The shortest path tree.
1.592 + ///- The distance of each node from the root.
1.593 + ///
1.594 + ///\note d.run(s) is just a shortcut of the following code.
1.595 + ///\code
1.596 + /// d.init();
1.597 + /// d.addSource(s);
1.598 + /// d.start();
1.599 + ///\endcode
1.600 + void run(Node s) {
1.601 + init();
1.602 + addSource(s);
1.603 + start();
1.604 + }
1.605 +
1.606 + ///Finds the shortest path between \c s and \c t.
1.607 +
1.608 + ///Finds the shortest path between \c s and \c t.
1.609 + ///
1.610 + ///\return The length of the shortest s---t path if there exists one,
1.611 + ///0 otherwise.
1.612 + ///\note Apart from the return value, d.run(s) is
1.613 + ///just a shortcut of the following code.
1.614 + ///\code
1.615 + /// d.init();
1.616 + /// d.addSource(s);
1.617 + /// d.start(t);
1.618 + ///\endcode
1.619 + Value run(Node s,Node t) {
1.620 + init();
1.621 + addSource(s);
1.622 + start(t);
1.623 + return (*_pred)[t]==INVALID?0:(*_dist)[t];
1.624 + }
1.625 +
1.626 + ///@}
1.627 +
1.628 + ///\name Query Functions
1.629 + ///The result of the %Dijkstra algorithm can be obtained using these
1.630 + ///functions.\n
1.631 + ///Before the use of these functions,
1.632 + ///either run() or start() must be called.
1.633 +
1.634 + ///@{
1.635 +
1.636 + ///The distance of a node from the root.
1.637 +
1.638 + ///Returns the distance of a node from the root.
1.639 + ///\pre \ref run() must be called before using this function.
1.640 + ///\warning If node \c v in unreachable from the root the return value
1.641 + ///of this funcion is undefined.
1.642 + Value dist(Node v) const { return (*_dist)[v]; }
1.643 +
1.644 + ///Returns the 'previous edge' of the shortest path tree.
1.645 +
1.646 + ///For a node \c v it returns the 'previous edge' of the shortest path tree,
1.647 + ///i.e. it returns the last edge of a shortest path from the root to \c
1.648 + ///v. It is \ref INVALID
1.649 + ///if \c v is unreachable from the root or if \c v=s. The
1.650 + ///shortest path tree used here is equal to the shortest path tree used in
1.651 + ///\ref predNode(Node v). \pre \ref run() must be called before using
1.652 + ///this function.
1.653 + ///\todo predEdge could be a better name.
1.654 + Edge pred(Node v) const { return (*_pred)[v]; }
1.655 +
1.656 + ///Returns the 'previous node' of the shortest path tree.
1.657 +
1.658 + ///For a node \c v it returns the 'previous node' of the shortest path tree,
1.659 + ///i.e. it returns the last but one node from a shortest path from the
1.660 + ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
1.661 + ///\c v=s. The shortest path tree used here is equal to the shortest path
1.662 + ///tree used in \ref pred(Node v). \pre \ref run() must be called before
1.663 + ///using this function.
1.664 + Node predNode(Node v) const { return (*_pred)[v]==INVALID ? INVALID:
1.665 + G->source((*_pred)[v]); }
1.666 +
1.667 + ///Returns a reference to the NodeMap of distances.
1.668 +
1.669 + ///Returns a reference to the NodeMap of distances. \pre \ref run() must
1.670 + ///be called before using this function.
1.671 + const DistMap &distMap() const { return *_dist;}
1.672 +
1.673 + ///Returns a reference to the shortest path tree map.
1.674 +
1.675 + ///Returns a reference to the NodeMap of the edges of the
1.676 + ///shortest path tree.
1.677 + ///\pre \ref run() must be called before using this function.
1.678 + const PredMap &predMap() const { return *_pred;}
1.679 +
1.680 + ///Returns a reference to the map of nodes of shortest paths.
1.681 +
1.682 + ///Returns a reference to the NodeMap of the last but one nodes of the
1.683 + ///shortest path tree.
1.684 + ///\pre \ref run() must be called before using this function.
1.685 + const PredNodeMap &predNodeMap() const { return *_predNode;}
1.686 +
1.687 + ///Checks if a node is reachable from the root.
1.688 +
1.689 + ///Returns \c true if \c v is reachable from the root.
1.690 + ///\warning If the algorithm is started from multiple nodes,
1.691 + ///this function may give false result for the source nodes.
1.692 + ///\pre \ref run() must be called before using this function.
1.693 + ///
1.694 + bool reached(Node v) { return v==source || (*_pred)[v]!=INVALID; }
1.695 +
1.696 + ///@}
1.697 + };
1.698 +
1.699 + /// Default traits used by \ref DijkstraWizard
1.700 +
1.701 + /// To make it easier to use Dijkstra algorithm we have created a wizard class.
1.702 + /// This \ref DijkstraWizard class needs default traits, as well as the \ref Dijkstra class.
1.703 + /// The \ref DijkstraWizardBase is a class to be the default traits of the
1.704 + /// \ref DijkstraWizard class.
1.705 + template<class GR,class LM>
1.706 + class DijkstraWizardBase : public DijkstraDefaultTraits<GR,LM>
1.707 + {
1.708 +
1.709 + typedef DijkstraDefaultTraits<GR,LM> Base;
1.710 + protected:
1.711 + /// Pointer to the underlying graph.
1.712 + void *_g;
1.713 + /// Pointer to the length map
1.714 + void *_length;
1.715 + ///Pointer to the map of predecessors edges.
1.716 + void *_pred;
1.717 + ///Pointer to the map of predecessors nodes.
1.718 + void *_predNode;
1.719 + ///Pointer to the map of distances.
1.720 + void *_dist;
1.721 + ///Pointer to the source node.
1.722 + void *_source;
1.723 +
1.724 + /// Type of the nodes in the graph.
1.725 + typedef typename Base::Graph::Node Node;
1.726 +
1.727 + public:
1.728 + /// Constructor.
1.729 +
1.730 + /// This constructor does not require parameters, therefore it initiates
1.731 + /// all of the attributes to default values (0, INVALID).
1.732 + DijkstraWizardBase() : _g(0), _length(0), _pred(0), _predNode(0),
1.733 + _dist(0), _source(INVALID) {}
1.734 +
1.735 + /// Constructor.
1.736 +
1.737 + /// This constructor requires some parameters, listed in the parameters list.
1.738 + /// Others are initiated to 0.
1.739 + /// \param g is the initial value of \ref _g
1.740 + /// \param l is the initial value of \ref _length
1.741 + /// \param s is the initial value of \ref _source
1.742 + DijkstraWizardBase(const GR &g,const LM &l, Node s=INVALID) :
1.743 + _g((void *)&g), _length((void *)&l), _pred(0), _predNode(0),
1.744 + _dist(0), _source((void *)&s) {}
1.745 +
1.746 + };
1.747 +
1.748 + /// A class to make easier the usage of Dijkstra algorithm
1.749 +
1.750 + /// This class is created to make it easier to use Dijkstra algorithm.
1.751 + /// It uses the functions and features of the plain \ref Dijkstra,
1.752 + /// but it is much more simple to use it.
1.753 + ///
1.754 + /// Simplicity means that the way to change the types defined
1.755 + /// in the traits class is based on functions that returns the new class
1.756 + /// and not on templatable built-in classes. When using the plain \ref Dijkstra
1.757 + /// the new class with the modified type comes from the original class by using the ::
1.758 + /// operator. In the case of \ref DijkstraWizard only a function have to be called and it will
1.759 + /// return the needed class.
1.760 + ///
1.761 + /// It does not have own \ref run method. When its \ref run method is called
1.762 + /// it initiates a plain \ref Dijkstra class, and calls the \ref Dijkstra::run
1.763 + /// method of it.
1.764 + template<class TR>
1.765 + class DijkstraWizard : public TR
1.766 + {
1.767 + typedef TR Base;
1.768 +
1.769 + ///The type of the underlying graph.
1.770 + typedef typename TR::Graph Graph;
1.771 + //\e
1.772 + typedef typename Graph::Node Node;
1.773 + //\e
1.774 + typedef typename Graph::NodeIt NodeIt;
1.775 + //\e
1.776 + typedef typename Graph::Edge Edge;
1.777 + //\e
1.778 + typedef typename Graph::OutEdgeIt OutEdgeIt;
1.779 +
1.780 + ///The type of the map that stores the edge lengths.
1.781 + typedef typename TR::LengthMap LengthMap;
1.782 + ///The type of the length of the edges.
1.783 + typedef typename LengthMap::Value Value;
1.784 + ///\brief The type of the map that stores the last
1.785 + ///edges of the shortest paths.
1.786 + typedef typename TR::PredMap PredMap;
1.787 + ///\brief The type of the map that stores the last but one
1.788 + ///nodes of the shortest paths.
1.789 + typedef typename TR::PredNodeMap PredNodeMap;
1.790 + ///The type of the map that stores the dists of the nodes.
1.791 + typedef typename TR::DistMap DistMap;
1.792 +
1.793 + ///The heap type used by the dijkstra algorithm.
1.794 + typedef typename TR::Heap Heap;
1.795 +public:
1.796 + /// Constructor.
1.797 + DijkstraWizard() : TR() {}
1.798 +
1.799 + /// Constructor that requires parameters.
1.800 +
1.801 + /// Constructor that requires parameters.
1.802 + /// These parameters will be the default values for the traits class.
1.803 + DijkstraWizard(const Graph &g,const LengthMap &l, Node s=INVALID) :
1.804 + TR(g,l,s) {}
1.805 +
1.806 + ///Copy constructor
1.807 + DijkstraWizard(const TR &b) : TR(b) {}
1.808 +
1.809 + ~DijkstraWizard() {}
1.810 +
1.811 + ///Runs Dijkstra algorithm from a given node.
1.812 +
1.813 + ///Runs Dijkstra algorithm from a given node.
1.814 + ///The node can be given by the \ref source function.
1.815 + void run()
1.816 + {
1.817 + if(_source==0) throw UninitializedParameter();
1.818 + Dijkstra<Graph,LengthMap,TR> Dij(*(Graph*)_g,*(LengthMap*)_length);
1.819 + if(_pred) Dij.predMap(*(PredMap*)_pred);
1.820 + if(_predNode) Dij.predNodeMap(*(PredNodeMap*)_predNode);
1.821 + if(_dist) Dij.distMap(*(DistMap*)_dist);
1.822 + Dij.run(*(Node*)_source);
1.823 + }
1.824 +
1.825 + ///Runs Dijkstra algorithm from the given node.
1.826 +
1.827 + ///Runs Dijkstra algorithm from the given node.
1.828 + ///\param s is the given source.
1.829 + void run(Node s)
1.830 + {
1.831 + _source=(void *)&s;
1.832 + run();
1.833 + }
1.834 +
1.835 + template<class T>
1.836 + struct DefPredMapBase : public Base {
1.837 + typedef T PredMap;
1.838 + static PredMap *createPredMap(const Graph &G) { return 0; };
1.839 + DefPredMapBase(const Base &b) : Base(b) {}
1.840 + };
1.841 +
1.842 + /// \ref named-templ-param "Named parameter" function for setting PredMap type
1.843 +
1.844 + /// \ref named-templ-param "Named parameter" function for setting PredMap type
1.845 + ///
1.846 + template<class T>
1.847 + DijkstraWizard<DefPredMapBase<T> > predMap(const T &t)
1.848 + {
1.849 + _pred=(void *)&t;
1.850 + return DijkstraWizard<DefPredMapBase<T> >(*this);
1.851 + }
1.852 +
1.853 +
1.854 + template<class T>
1.855 + struct DefPredNodeMapBase : public Base {
1.856 + typedef T PredNodeMap;
1.857 + static PredNodeMap *createPredNodeMap(const Graph &G) { return 0; };
1.858 + DefPredNodeMapBase(const Base &b) : Base(b) {}
1.859 + };
1.860 +
1.861 + /// \ref named-templ-param "Named parameter" function for setting PredNodeMap type
1.862 +
1.863 + /// \ref named-templ-param "Named parameter" function for setting PredNodeMap type
1.864 + ///
1.865 + template<class T>
1.866 + DijkstraWizard<DefPredNodeMapBase<T> > predNodeMap(const T &t)
1.867 + {
1.868 + _predNode=(void *)&t;
1.869 + return DijkstraWizard<DefPredNodeMapBase<T> >(*this);
1.870 + }
1.871 +
1.872 + template<class T>
1.873 + struct DefDistMapBase : public Base {
1.874 + typedef T DistMap;
1.875 + static DistMap *createDistMap(const Graph &G) { return 0; };
1.876 + DefDistMapBase(const Base &b) : Base(b) {}
1.877 + };
1.878 +
1.879 + /// \ref named-templ-param "Named parameter" function for setting DistMap type
1.880 +
1.881 + /// \ref named-templ-param "Named parameter" function for setting DistMap type
1.882 + ///
1.883 + template<class T>
1.884 + DijkstraWizard<DefDistMapBase<T> > distMap(const T &t)
1.885 + {
1.886 + _dist=(void *)&t;
1.887 + return DijkstraWizard<DefDistMapBase<T> >(*this);
1.888 + }
1.889 +
1.890 + /// Sets the source node, from which the Dijkstra algorithm runs.
1.891 +
1.892 + /// Sets the source node, from which the Dijkstra algorithm runs.
1.893 + /// \param s is the source node.
1.894 + DijkstraWizard<TR> &source(Node s)
1.895 + {
1.896 + source=(void *)&s;
1.897 + return *this;
1.898 + }
1.899 +
1.900 + };
1.901 +
1.902 + ///\e
1.903 +
1.904 + ///\todo Please document...
1.905 + ///
1.906 + template<class GR, class LM>
1.907 + DijkstraWizard<DijkstraWizardBase<GR,LM> >
1.908 + dijkstra(const GR &g,const LM &l,typename GR::Node s=INVALID)
1.909 + {
1.910 + return DijkstraWizard<DijkstraWizardBase<GR,LM> >(g,l,s);
1.911 + }
1.912 +
1.913 +/// @}
1.914 +
1.915 +} //END OF NAMESPACE LEMON
1.916 +
1.917 +#endif
1.918 +