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, 16 years ago

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

File size: 9.7 KB
Line 
1// -*- C++ -*-
2#ifndef HUGO_DIJKSTRA_H
3#define HUGO_DIJKSTRA_H
4
5///\ingroup flowalgs
6///\file
7///\brief Dijkstra algorithm.
8
9#include <hugo/bin_heap.h>
10#include <hugo/invalid.h>
11
12namespace hugo {
13
14/// \addtogroup flowalgs
15/// @{
16
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  ///
28  ///\param GR The graph type the algorithm runs on.
29  ///\param LM This read-only
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".
39  ///
40  ///\author Jacint Szabo and Alpar Juttner
41  ///\todo We need a typedef-names should be standardized. (-:
42  ///\todo Type of \c PredMap, \c PredNodeMap and \c DistMap
43  ///should not be fixed. (Problematic to solve).
44
45#ifdef DOXYGEN
46  template <typename GR,
47            typename LM,
48            typename Heap>
49#else
50  template <typename GR,
51            typename LM=typename GR::template EdgeMap<int>,
52            template <class,class,class,class> class Heap = BinHeap >
53#endif
54  class Dijkstra{
55  public:
56    ///The type of the underlying graph.
57    typedef GR Graph;
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   
63    ///The type of the length of the edges.
64    typedef typename LM::ValueType ValueType;
65    ///The type of the map that stores the edge lengths.
66    typedef LM LengthMap;
67    ///\brief The type of the map that stores the last
68    ///edges of the shortest paths.
69    typedef typename Graph::template NodeMap<Edge> PredMap;
70    ///\brief The type of the map that stores the last but one
71    ///nodes of the shortest paths.
72    typedef typename Graph::template NodeMap<Node> PredNodeMap;
73    ///The type of the map that stores the dists of the nodes.
74    typedef typename Graph::template NodeMap<ValueType> DistMap;
75
76  private:
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
87    //The source node of the last execution.
88    Node source;
89
90    ///Initialize maps
91   
92    ///\todo Error if \c G or are \c NULL. What about \c length?
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    }
113   
114  public :
115   
116    Dijkstra(const Graph& _G, const LM& _length) :
117      G(&_G), length(&_length),
118      predecessor(NULL), local_predecessor(false),
119      pred_node(NULL), local_pred_node(false),
120      distance(NULL), local_distance(false)
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    }
204   
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     
218      source = s;
219     
220      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
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       
243        for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
244          Node w=G->head(e);
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    }
264   
265    ///The distance of a node from the root.
266
267    ///Returns the distance of a node from the root.
268    ///\pre \ref run() must be called before using this function.
269    ///\warning If node \c v in unreachable from the root the return value
270    ///of this funcion is undefined.
271    ValueType dist(Node v) const { return (*distance)[v]; }
272
273    ///Returns the 'previous edge' of the shortest path tree.
274
275    ///For a node \c v it returns the 'previous edge' of the shortest path tree,
276    ///i.e. it returns the last edge from a shortest path from the root to \c
277    ///v. It is \ref INVALID
278    ///if \c v is unreachable from the root or if \c v=s. The
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.
282    Edge pred(Node v) const { return (*predecessor)[v]; }
283
284    ///Returns the 'previous node' of the shortest path tree.
285
286    ///For a node \c v it returns the 'previous node' of the shortest path tree,
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.
292    Node predNode(Node v) const { return (*pred_node)[v]; }
293   
294    ///Returns a reference to the NodeMap of distances.
295
296    ///Returns a reference to the NodeMap of distances. \pre \ref run() must
297    ///be called before using this function.
298    const DistMap &distMap() const { return *distance;}
299 
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.
305    const PredMap &predMap() const { return *predecessor;}
306 
307    ///Returns a reference to the map of nodes of shortest paths.
308
309    ///Returns a reference to the NodeMap of the last but one nodes of the
310    ///shortest path tree.
311    ///\pre \ref run() must be called before using this function.
312    const PredNodeMap &predNodeMap() const { return *pred_node;}
313
314    ///Checks if a node is reachable from the root.
315
316    ///Returns \c true if \c v is reachable from the root.
317    ///\warning the root node is reported to be reached!
318    ///\pre \ref run() must be called before using this function.
319    ///
320    bool reached(Node v) { return v==source || (*predecessor)[v]==INVALID; }
321   
322  };
323 
324
325  // **********************************************************************
326  //  IMPLEMENTATIONS
327  // **********************************************************************
328
329/// @}
330 
331} //END OF NAMESPACE HUGO
332
333#endif
334
335
Note: See TracBrowser for help on using the repository browser.