1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/lemon/dijkstra.h Wed Sep 29 15:30:04 2004 +0000
1.3 @@ -0,0 +1,344 @@
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/bin_heap.h>
1.28 +#include <lemon/invalid.h>
1.29 +
1.30 +namespace lemon {
1.31 +
1.32 +/// \addtogroup flowalgs
1.33 +/// @{
1.34 +
1.35 + ///%Dijkstra algorithm class.
1.36 +
1.37 + ///This class provides an efficient implementation of %Dijkstra algorithm.
1.38 + ///The edge lengths are passed to the algorithm using a
1.39 + ///\ref skeleton::ReadMap "ReadMap",
1.40 + ///so it is easy to change it to any kind of length.
1.41 + ///
1.42 + ///The type of the length is determined by the
1.43 + ///\ref skeleton::ReadMap::ValueType "ValueType" of the length map.
1.44 + ///
1.45 + ///It is also possible to change the underlying priority heap.
1.46 + ///
1.47 + ///\param GR The graph type the algorithm runs on.
1.48 + ///\param LM This read-only
1.49 + ///EdgeMap
1.50 + ///determines the
1.51 + ///lengths of the edges. It is read once for each edge, so the map
1.52 + ///may involve in relatively time consuming process to compute the edge
1.53 + ///length if it is necessary. The default map type is
1.54 + ///\ref skeleton::StaticGraph::EdgeMap "Graph::EdgeMap<int>"
1.55 + ///\param Heap The heap type used by the %Dijkstra
1.56 + ///algorithm. The default
1.57 + ///is using \ref BinHeap "binary heap".
1.58 + ///
1.59 + ///\author Jacint Szabo and Alpar Juttner
1.60 + ///\todo We need a typedef-names should be standardized. (-:
1.61 + ///\todo Type of \c PredMap, \c PredNodeMap and \c DistMap
1.62 + ///should not be fixed. (Problematic to solve).
1.63 +
1.64 +#ifdef DOXYGEN
1.65 + template <typename GR,
1.66 + typename LM,
1.67 + typename Heap>
1.68 +#else
1.69 + template <typename GR,
1.70 + typename LM=typename GR::template EdgeMap<int>,
1.71 + template <class,class,class,class> class Heap = BinHeap >
1.72 +#endif
1.73 + class Dijkstra{
1.74 + public:
1.75 + ///The type of the underlying graph.
1.76 + typedef GR Graph;
1.77 + ///\e
1.78 + typedef typename Graph::Node Node;
1.79 + ///\e
1.80 + typedef typename Graph::NodeIt NodeIt;
1.81 + ///\e
1.82 + typedef typename Graph::Edge Edge;
1.83 + ///\e
1.84 + typedef typename Graph::OutEdgeIt OutEdgeIt;
1.85 +
1.86 + ///The type of the length of the edges.
1.87 + typedef typename LM::ValueType ValueType;
1.88 + ///The type of the map that stores the edge lengths.
1.89 + typedef LM LengthMap;
1.90 + ///\brief The type of the map that stores the last
1.91 + ///edges of the shortest paths.
1.92 + typedef typename Graph::template NodeMap<Edge> PredMap;
1.93 + ///\brief The type of the map that stores the last but one
1.94 + ///nodes of the shortest paths.
1.95 + typedef typename Graph::template NodeMap<Node> PredNodeMap;
1.96 + ///The type of the map that stores the dists of the nodes.
1.97 + typedef typename Graph::template NodeMap<ValueType> DistMap;
1.98 +
1.99 + private:
1.100 + /// Pointer to the underlying graph.
1.101 + const Graph *G;
1.102 + /// Pointer to the length map
1.103 + const LM *length;
1.104 + ///Pointer to the map of predecessors edges.
1.105 + PredMap *predecessor;
1.106 + ///Indicates if \ref predecessor is locally allocated (\c true) or not.
1.107 + bool local_predecessor;
1.108 + ///Pointer to the map of predecessors nodes.
1.109 + PredNodeMap *pred_node;
1.110 + ///Indicates if \ref pred_node is locally allocated (\c true) or not.
1.111 + bool local_pred_node;
1.112 + ///Pointer to the map of distances.
1.113 + DistMap *distance;
1.114 + ///Indicates if \ref distance is locally allocated (\c true) or not.
1.115 + bool local_distance;
1.116 +
1.117 + ///The source node of the last execution.
1.118 + Node source;
1.119 +
1.120 + ///Initializes the maps.
1.121 +
1.122 + ///\todo Error if \c G or are \c NULL. What about \c length?
1.123 + ///\todo Better memory allocation (instead of new).
1.124 + void init_maps()
1.125 + {
1.126 + if(!predecessor) {
1.127 + local_predecessor = true;
1.128 + predecessor = new PredMap(*G);
1.129 + }
1.130 + if(!pred_node) {
1.131 + local_pred_node = true;
1.132 + pred_node = new PredNodeMap(*G);
1.133 + }
1.134 + if(!distance) {
1.135 + local_distance = true;
1.136 + distance = new DistMap(*G);
1.137 + }
1.138 + }
1.139 +
1.140 + public :
1.141 + ///Constructor.
1.142 +
1.143 + ///\param _G the graph the algorithm will run on.
1.144 + ///\param _length the length map used by the algorithm.
1.145 + Dijkstra(const Graph& _G, const LM& _length) :
1.146 + G(&_G), length(&_length),
1.147 + predecessor(NULL), local_predecessor(false),
1.148 + pred_node(NULL), local_pred_node(false),
1.149 + distance(NULL), local_distance(false)
1.150 + { }
1.151 +
1.152 + ///Destructor.
1.153 + ~Dijkstra()
1.154 + {
1.155 + if(local_predecessor) delete predecessor;
1.156 + if(local_pred_node) delete pred_node;
1.157 + if(local_distance) delete distance;
1.158 + }
1.159 +
1.160 + ///Sets the length map.
1.161 +
1.162 + ///Sets the length map.
1.163 + ///\return <tt> (*this) </tt>
1.164 + Dijkstra &setLengthMap(const LM &m)
1.165 + {
1.166 + length = &m;
1.167 + return *this;
1.168 + }
1.169 +
1.170 + ///Sets the map storing the predecessor edges.
1.171 +
1.172 + ///Sets the map storing the predecessor edges.
1.173 + ///If you don't use this function before calling \ref run(),
1.174 + ///it will allocate one. The destuctor deallocates this
1.175 + ///automatically allocated map, of course.
1.176 + ///\return <tt> (*this) </tt>
1.177 + Dijkstra &setPredMap(PredMap &m)
1.178 + {
1.179 + if(local_predecessor) {
1.180 + delete predecessor;
1.181 + local_predecessor=false;
1.182 + }
1.183 + predecessor = &m;
1.184 + return *this;
1.185 + }
1.186 +
1.187 + ///Sets the map storing the predecessor nodes.
1.188 +
1.189 + ///Sets the map storing the predecessor nodes.
1.190 + ///If you don't use this function before calling \ref run(),
1.191 + ///it will allocate one. The destuctor deallocates this
1.192 + ///automatically allocated map, of course.
1.193 + ///\return <tt> (*this) </tt>
1.194 + Dijkstra &setPredNodeMap(PredNodeMap &m)
1.195 + {
1.196 + if(local_pred_node) {
1.197 + delete pred_node;
1.198 + local_pred_node=false;
1.199 + }
1.200 + pred_node = &m;
1.201 + return *this;
1.202 + }
1.203 +
1.204 + ///Sets the map storing the distances calculated by the algorithm.
1.205 +
1.206 + ///Sets the map storing the distances calculated by the algorithm.
1.207 + ///If you don't use this function before calling \ref run(),
1.208 + ///it will allocate one. The destuctor deallocates this
1.209 + ///automatically allocated map, of course.
1.210 + ///\return <tt> (*this) </tt>
1.211 + Dijkstra &setDistMap(DistMap &m)
1.212 + {
1.213 + if(local_distance) {
1.214 + delete distance;
1.215 + local_distance=false;
1.216 + }
1.217 + distance = &m;
1.218 + return *this;
1.219 + }
1.220 +
1.221 + ///Runs %Dijkstra algorithm from node \c s.
1.222 +
1.223 + ///This method runs the %Dijkstra algorithm from a root node \c s
1.224 + ///in order to
1.225 + ///compute the
1.226 + ///shortest path to each node. The algorithm computes
1.227 + ///- The shortest path tree.
1.228 + ///- The distance of each node from the root.
1.229 +
1.230 + void run(Node s) {
1.231 +
1.232 + init_maps();
1.233 +
1.234 + source = s;
1.235 +
1.236 + for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
1.237 + predecessor->set(u,INVALID);
1.238 + pred_node->set(u,INVALID);
1.239 + }
1.240 +
1.241 + typename GR::template NodeMap<int> heap_map(*G,-1);
1.242 +
1.243 + typedef Heap<Node, ValueType, typename GR::template NodeMap<int>,
1.244 + std::less<ValueType> >
1.245 + HeapType;
1.246 +
1.247 + HeapType heap(heap_map);
1.248 +
1.249 + heap.push(s,0);
1.250 +
1.251 + while ( !heap.empty() ) {
1.252 +
1.253 + Node v=heap.top();
1.254 + ValueType oldvalue=heap[v];
1.255 + heap.pop();
1.256 + distance->set(v, oldvalue);
1.257 +
1.258 +
1.259 + for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
1.260 + Node w=G->head(e);
1.261 + switch(heap.state(w)) {
1.262 + case HeapType::PRE_HEAP:
1.263 + heap.push(w,oldvalue+(*length)[e]);
1.264 + predecessor->set(w,e);
1.265 + pred_node->set(w,v);
1.266 + break;
1.267 + case HeapType::IN_HEAP:
1.268 + if ( oldvalue+(*length)[e] < heap[w] ) {
1.269 + heap.decrease(w, oldvalue+(*length)[e]);
1.270 + predecessor->set(w,e);
1.271 + pred_node->set(w,v);
1.272 + }
1.273 + break;
1.274 + case HeapType::POST_HEAP:
1.275 + break;
1.276 + }
1.277 + }
1.278 + }
1.279 + }
1.280 +
1.281 + ///The distance of a node from the root.
1.282 +
1.283 + ///Returns the distance of a node from the root.
1.284 + ///\pre \ref run() must be called before using this function.
1.285 + ///\warning If node \c v in unreachable from the root the return value
1.286 + ///of this funcion is undefined.
1.287 + ValueType dist(Node v) const { return (*distance)[v]; }
1.288 +
1.289 + ///Returns the 'previous edge' of the shortest path tree.
1.290 +
1.291 + ///For a node \c v it returns the 'previous edge' of the shortest path tree,
1.292 + ///i.e. it returns the last edge of a shortest path from the root to \c
1.293 + ///v. It is \ref INVALID
1.294 + ///if \c v is unreachable from the root or if \c v=s. The
1.295 + ///shortest path tree used here is equal to the shortest path tree used in
1.296 + ///\ref predNode(Node v). \pre \ref run() must be called before using
1.297 + ///this function.
1.298 + ///\todo predEdge could be a better name.
1.299 + Edge pred(Node v) const { return (*predecessor)[v]; }
1.300 +
1.301 + ///Returns the 'previous node' of the shortest path tree.
1.302 +
1.303 + ///For a node \c v it returns the 'previous node' of the shortest path tree,
1.304 + ///i.e. it returns the last but one node from a shortest path from the
1.305 + ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
1.306 + ///\c v=s. The shortest path tree used here is equal to the shortest path
1.307 + ///tree used in \ref pred(Node v). \pre \ref run() must be called before
1.308 + ///using this function.
1.309 + Node predNode(Node v) const { return (*pred_node)[v]; }
1.310 +
1.311 + ///Returns a reference to the NodeMap of distances.
1.312 +
1.313 + ///Returns a reference to the NodeMap of distances. \pre \ref run() must
1.314 + ///be called before using this function.
1.315 + const DistMap &distMap() const { return *distance;}
1.316 +
1.317 + ///Returns a reference to the shortest path tree map.
1.318 +
1.319 + ///Returns a reference to the NodeMap of the edges of the
1.320 + ///shortest path tree.
1.321 + ///\pre \ref run() must be called before using this function.
1.322 + const PredMap &predMap() const { return *predecessor;}
1.323 +
1.324 + ///Returns a reference to the map of nodes of shortest paths.
1.325 +
1.326 + ///Returns a reference to the NodeMap of the last but one nodes of the
1.327 + ///shortest path tree.
1.328 + ///\pre \ref run() must be called before using this function.
1.329 + const PredNodeMap &predNodeMap() const { return *pred_node;}
1.330 +
1.331 + ///Checks if a node is reachable from the root.
1.332 +
1.333 + ///Returns \c true if \c v is reachable from the root.
1.334 + ///\note The root node is reported to be reached!
1.335 + ///\pre \ref run() must be called before using this function.
1.336 + ///
1.337 + bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
1.338 +
1.339 + };
1.340 +
1.341 +/// @}
1.342 +
1.343 +} //END OF NAMESPACE LEMON
1.344 +
1.345 +#endif
1.346 +
1.347 +