COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/hugo/dijkstra.h @ 776:f2994a2b10b2

Last change on this file since 776:f2994a2b10b2 was 776:f2994a2b10b2, checked in by Hegyi Péter, 20 years ago

minlengthpaths_test.cc is already hugo++ comform and is compilable

File size: 9.7 KB
RevLine 
[255]1// -*- C++ -*-
2#ifndef HUGO_DIJKSTRA_H
3#define HUGO_DIJKSTRA_H
4
[758]5///\ingroup flowalgs
[255]6///\file
7///\brief Dijkstra algorithm.
8
[542]9#include <hugo/bin_heap.h>
10#include <hugo/invalid.h>
[255]11
12namespace hugo {
[385]13
[758]14/// \addtogroup flowalgs
[430]15/// @{
16
[255]17  ///%Dijkstra algorithm class.
18
19  ///This class provides an efficient implementation of %Dijkstra algorithm.
20  ///The edge lengths are passed to the algorithm using a
21  ///\ref ReadMapSkeleton "readable map",
22  ///so it is easy to change it to any kind of length.
23  ///
24  ///The type of the length is determined by the \c ValueType of the length map.
25  ///
26  ///It is also possible to change the underlying priority heap.
27  ///
[584]28  ///\param GR The graph type the algorithm runs on.
29  ///\param LM This read-only
[385]30  ///EdgeMap
31  ///determines the
32  ///lengths of the edges. It is read once for each edge, so the map
33  ///may involve in relatively time consuming process to compute the edge
34  ///length if it is necessary. The default map type is
35  ///\ref GraphSkeleton::EdgeMap "Graph::EdgeMap<int>"
36  ///\param Heap The heap type used by the %Dijkstra
37  ///algorithm. The default
38  ///is using \ref BinHeap "binary heap".
[456]39  ///
[689]40  ///\author Jacint Szabo and Alpar Juttner
[693]41  ///\todo We need a typedef-names should be standardized. (-:
[734]42  ///\todo Type of \c PredMap, \c PredNodeMap and \c DistMap
43  ///should not be fixed. (Problematic to solve).
[584]44
[255]45#ifdef DOXYGEN
[584]46  template <typename GR,
47            typename LM,
[255]48            typename Heap>
49#else
[584]50  template <typename GR,
51            typename LM=typename GR::template EdgeMap<int>,
[532]52            template <class,class,class,class> class Heap = BinHeap >
[255]53#endif
54  class Dijkstra{
55  public:
[584]56    ///The type of the underlying graph.
57    typedef GR Graph;
[255]58    typedef typename Graph::Node Node;
59    typedef typename Graph::NodeIt NodeIt;
60    typedef typename Graph::Edge Edge;
61    typedef typename Graph::OutEdgeIt OutEdgeIt;
62   
[584]63    ///The type of the length of the edges.
64    typedef typename LM::ValueType ValueType;
[693]65    ///The type of the map that stores the edge lengths.
[584]66    typedef LM LengthMap;
[693]67    ///\brief The type of the map that stores the last
[584]68    ///edges of the shortest paths.
[433]69    typedef typename Graph::template NodeMap<Edge> PredMap;
[693]70    ///\brief The type of the map that stores the last but one
[584]71    ///nodes of the shortest paths.
[433]72    typedef typename Graph::template NodeMap<Node> PredNodeMap;
[693]73    ///The type of the map that stores the dists of the nodes.
[433]74    typedef typename Graph::template NodeMap<ValueType> DistMap;
[255]75
76  private:
[688]77    const Graph *G;
78    const LM *length;
79    //    bool local_length;
80    PredMap *predecessor;
81    bool local_predecessor;
82    PredNodeMap *pred_node;
83    bool local_pred_node;
84    DistMap *distance;
85    bool local_distance;
86
[774]87    //The source node of the last execution.
88    Node source;
89
[688]90    ///Initialize maps
91   
[694]92    ///\todo Error if \c G or are \c NULL. What about \c length?
[688]93    ///\todo Better memory allocation (instead of new).
94    void init_maps()
95    {
96//       if(!length) {
97//      local_length = true;
98//      length = new LM(G);
99//       }
100      if(!predecessor) {
101        local_predecessor = true;
102        predecessor = new PredMap(*G);
103      }
104      if(!pred_node) {
105        local_pred_node = true;
106        pred_node = new PredNodeMap(*G);
107      }
108      if(!distance) {
109        local_distance = true;
110        distance = new DistMap(*G);
111      }
112    }
[255]113   
114  public :
115   
[584]116    Dijkstra(const Graph& _G, const LM& _length) :
[688]117      G(&_G), length(&_length),
[707]118      predecessor(NULL), local_predecessor(false),
119      pred_node(NULL), local_pred_node(false),
120      distance(NULL), local_distance(false)
[688]121    { }
122   
123    ~Dijkstra()
124    {
125      //      if(local_length) delete length;
126      if(local_predecessor) delete predecessor;
127      if(local_pred_node) delete pred_node;
128      if(local_distance) delete distance;
129    }
130
131    ///Sets the graph the algorithm will run on.
132
133    ///Sets the graph the algorithm will run on.
134    ///\return <tt> (*this) </tt>
135    Dijkstra &setGraph(const Graph &_G)
136    {
137      G = &_G;
138      return *this;
139    }
140    ///Sets the length map.
141
142    ///Sets the length map.
143    ///\return <tt> (*this) </tt>
144    Dijkstra &setLengthMap(const LM &m)
145    {
146//       if(local_length) {
147//      delete length;
148//      local_length=false;
149//       }
150      length = &m;
151      return *this;
152    }
153
154    ///Sets the map storing the predecessor edges.
155
156    ///Sets the map storing the predecessor edges.
157    ///If you don't use this function before calling \ref run(),
158    ///it will allocate one. The destuctor deallocates this
159    ///automatically allocated map, of course.
160    ///\return <tt> (*this) </tt>
161    Dijkstra &setPredMap(PredMap &m)
162    {
163      if(local_predecessor) {
164        delete predecessor;
165        local_predecessor=false;
166      }
167      predecessor = &m;
168      return *this;
169    }
170
171    ///Sets the map storing the predecessor nodes.
172
173    ///Sets the map storing the predecessor nodes.
174    ///If you don't use this function before calling \ref run(),
175    ///it will allocate one. The destuctor deallocates this
176    ///automatically allocated map, of course.
177    ///\return <tt> (*this) </tt>
178    Dijkstra &setPredNodeMap(PredNodeMap &m)
179    {
180      if(local_pred_node) {
181        delete pred_node;
182        local_pred_node=false;
183      }
184      pred_node = &m;
185      return *this;
186    }
187
188    ///Sets the map storing the distances calculated by the algorithm.
189
190    ///Sets the map storing the distances calculated by the algorithm.
191    ///If you don't use this function before calling \ref run(),
192    ///it will allocate one. The destuctor deallocates this
193    ///automatically allocated map, of course.
194    ///\return <tt> (*this) </tt>
195    Dijkstra &setDistMap(DistMap &m)
196    {
197      if(local_distance) {
198        delete distance;
199        local_distance=false;
200      }
201      distance = &m;
202      return *this;
203    }
[255]204   
[694]205  ///Runs %Dijkstra algorithm from node \c s.
206
207  ///This method runs the %Dijkstra algorithm from a root node \c s
208  ///in order to
209  ///compute the
210  ///shortest path to each node. The algorithm computes
211  ///- The shortest path tree.
212  ///- The distance of each node from the root.
213   
214    void run(Node s) {
215     
216      init_maps();
217     
[774]218      source = s;
219     
220      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
[694]221        predecessor->set(u,INVALID);
222        pred_node->set(u,INVALID);
223      }
224     
225      typename GR::template NodeMap<int> heap_map(*G,-1);
226     
227      typedef Heap<Node, ValueType, typename GR::template NodeMap<int>,
228      std::less<ValueType> >
229      HeapType;
230     
231      HeapType heap(heap_map);
232     
233      heap.push(s,0);
234     
235      while ( !heap.empty() ) {
236       
237        Node v=heap.top();
238        ValueType oldvalue=heap[v];
239        heap.pop();
240        distance->set(v, oldvalue);
241       
242       
[774]243        for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
244          Node w=G->head(e);
[694]245          switch(heap.state(w)) {
246          case HeapType::PRE_HEAP:
247            heap.push(w,oldvalue+(*length)[e]);
248            predecessor->set(w,e);
249            pred_node->set(w,v);
250            break;
251          case HeapType::IN_HEAP:
252            if ( oldvalue+(*length)[e] < heap[w] ) {
253              heap.decrease(w, oldvalue+(*length)[e]);
254              predecessor->set(w,e);
255              pred_node->set(w,v);
256            }
257            break;
258          case HeapType::POST_HEAP:
259            break;
260          }
261        }
262      }
263    }
[255]264   
[385]265    ///The distance of a node from the root.
[255]266
[385]267    ///Returns the distance of a node from the root.
[255]268    ///\pre \ref run() must be called before using this function.
[385]269    ///\warning If node \c v in unreachable from the root the return value
[255]270    ///of this funcion is undefined.
[688]271    ValueType dist(Node v) const { return (*distance)[v]; }
[373]272
[584]273    ///Returns the 'previous edge' of the shortest path tree.
[255]274
[584]275    ///For a node \c v it returns the 'previous edge' of the shortest path tree,
[385]276    ///i.e. it returns the last edge from a shortest path from the root to \c
[688]277    ///v. It is \ref INVALID
278    ///if \c v is unreachable from the root or if \c v=s. The
[385]279    ///shortest path tree used here is equal to the shortest path tree used in
280    ///\ref predNode(Node v).  \pre \ref run() must be called before using
281    ///this function.
[688]282    Edge pred(Node v) const { return (*predecessor)[v]; }
[373]283
[584]284    ///Returns the 'previous node' of the shortest path tree.
[255]285
[584]286    ///For a node \c v it returns the 'previous node' of the shortest path tree,
[385]287    ///i.e. it returns the last but one node from a shortest path from the
288    ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
289    ///\c v=s. The shortest path tree used here is equal to the shortest path
290    ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
291    ///using this function.
[688]292    Node predNode(Node v) const { return (*pred_node)[v]; }
[255]293   
294    ///Returns a reference to the NodeMap of distances.
295
[385]296    ///Returns a reference to the NodeMap of distances. \pre \ref run() must
297    ///be called before using this function.
[688]298    const DistMap &distMap() const { return *distance;}
[385]299 
[255]300    ///Returns a reference to the shortest path tree map.
301
302    ///Returns a reference to the NodeMap of the edges of the
303    ///shortest path tree.
304    ///\pre \ref run() must be called before using this function.
[688]305    const PredMap &predMap() const { return *predecessor;}
[385]306 
307    ///Returns a reference to the map of nodes of shortest paths.
[255]308
309    ///Returns a reference to the NodeMap of the last but one nodes of the
[385]310    ///shortest path tree.
[255]311    ///\pre \ref run() must be called before using this function.
[688]312    const PredNodeMap &predNodeMap() const { return *pred_node;}
[255]313
[385]314    ///Checks if a node is reachable from the root.
[255]315
[385]316    ///Returns \c true if \c v is reachable from the root.
[774]317    ///\warning the root node is reported to be reached!
[255]318    ///\pre \ref run() must be called before using this function.
[385]319    ///
[774]320    bool reached(Node v) { return v==source || (*predecessor)[v]==INVALID; }
[255]321   
322  };
323 
324
325  // **********************************************************************
326  //  IMPLEMENTATIONS
327  // **********************************************************************
328
[430]329/// @}
[255]330 
331} //END OF NAMESPACE HUGO
332
333#endif
334
335
Note: See TracBrowser for help on using the repository browser.