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