COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/hugo/dijkstra.h @ 788:c3187cafcabf

Last change on this file since 788:c3187cafcabf was 785:a9b0863c2265, checked in by Alpar Juttner, 20 years ago

Changes in doc. (New module name for array/vector maps added.)

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    ///Initializes the 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    ///\bug What about maps?
136    ///\todo It may be unnecessary
137    Dijkstra &setGraph(const Graph &_G)
138    {
139      G = &_G;
140      return *this;
141    }
142    ///Sets the length map.
143
144    ///Sets the length map.
145    ///\return <tt> (*this) </tt>
146    Dijkstra &setLengthMap(const LM &m)
147    {
148//       if(local_length) {
149//      delete length;
150//      local_length=false;
151//       }
152      length = &m;
153      return *this;
154    }
155
156    ///Sets the map storing the predecessor edges.
157
158    ///Sets the map storing the predecessor edges.
159    ///If you don't use this function before calling \ref run(),
160    ///it will allocate one. The destuctor deallocates this
161    ///automatically allocated map, of course.
162    ///\return <tt> (*this) </tt>
163    Dijkstra &setPredMap(PredMap &m)
164    {
165      if(local_predecessor) {
166        delete predecessor;
167        local_predecessor=false;
168      }
169      predecessor = &m;
170      return *this;
171    }
172
173    ///Sets the map storing the predecessor nodes.
174
175    ///Sets the map storing the predecessor nodes.
176    ///If you don't use this function before calling \ref run(),
177    ///it will allocate one. The destuctor deallocates this
178    ///automatically allocated map, of course.
179    ///\return <tt> (*this) </tt>
180    Dijkstra &setPredNodeMap(PredNodeMap &m)
181    {
182      if(local_pred_node) {
183        delete pred_node;
184        local_pred_node=false;
185      }
186      pred_node = &m;
187      return *this;
188    }
189
190    ///Sets the map storing the distances calculated by the algorithm.
191
192    ///Sets the map storing the distances calculated by the algorithm.
193    ///If you don't use this function before calling \ref run(),
194    ///it will allocate one. The destuctor deallocates this
195    ///automatically allocated map, of course.
196    ///\return <tt> (*this) </tt>
197    Dijkstra &setDistMap(DistMap &m)
198    {
199      if(local_distance) {
200        delete distance;
201        local_distance=false;
202      }
203      distance = &m;
204      return *this;
205    }
206   
207  ///Runs %Dijkstra algorithm from node \c s.
208
209  ///This method runs the %Dijkstra algorithm from a root node \c s
210  ///in order to
211  ///compute the
212  ///shortest path to each node. The algorithm computes
213  ///- The shortest path tree.
214  ///- The distance of each node from the root.
215   
216    void run(Node s) {
217     
218      init_maps();
219     
220      source = s;
221     
222      for ( NodeIt u(*G) ; u!=INVALID ; ++u ) {
223        predecessor->set(u,INVALID);
224        pred_node->set(u,INVALID);
225      }
226     
227      typename GR::template NodeMap<int> heap_map(*G,-1);
228     
229      typedef Heap<Node, ValueType, typename GR::template NodeMap<int>,
230      std::less<ValueType> >
231      HeapType;
232     
233      HeapType heap(heap_map);
234     
235      heap.push(s,0);
236     
237      while ( !heap.empty() ) {
238       
239        Node v=heap.top();
240        ValueType oldvalue=heap[v];
241        heap.pop();
242        distance->set(v, oldvalue);
243       
244       
245        for(OutEdgeIt e(*G,v); e!=INVALID; ++e) {
246          Node w=G->head(e);
247          switch(heap.state(w)) {
248          case HeapType::PRE_HEAP:
249            heap.push(w,oldvalue+(*length)[e]);
250            predecessor->set(w,e);
251            pred_node->set(w,v);
252            break;
253          case HeapType::IN_HEAP:
254            if ( oldvalue+(*length)[e] < heap[w] ) {
255              heap.decrease(w, oldvalue+(*length)[e]);
256              predecessor->set(w,e);
257              pred_node->set(w,v);
258            }
259            break;
260          case HeapType::POST_HEAP:
261            break;
262          }
263        }
264      }
265    }
266   
267    ///The distance of a node from the root.
268
269    ///Returns the distance of a node from the root.
270    ///\pre \ref run() must be called before using this function.
271    ///\warning If node \c v in unreachable from the root the return value
272    ///of this funcion is undefined.
273    ValueType dist(Node v) const { return (*distance)[v]; }
274
275    ///Returns the 'previous edge' of the shortest path tree.
276
277    ///For a node \c v it returns the 'previous edge' of the shortest path tree,
278    ///i.e. it returns the last edge of a shortest path from the root to \c
279    ///v. It is \ref INVALID
280    ///if \c v is unreachable from the root or if \c v=s. The
281    ///shortest path tree used here is equal to the shortest path tree used in
282    ///\ref predNode(Node v).  \pre \ref run() must be called before using
283    ///this function.
284    ///\todo predEdge could be a better name.
285    Edge pred(Node v) const { return (*predecessor)[v]; }
286
287    ///Returns the 'previous node' of the shortest path tree.
288
289    ///For a node \c v it returns the 'previous node' of the shortest path tree,
290    ///i.e. it returns the last but one node from a shortest path from the
291    ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
292    ///\c v=s. The shortest path tree used here is equal to the shortest path
293    ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
294    ///using this function.
295    Node predNode(Node v) const { return (*pred_node)[v]; }
296   
297    ///Returns a reference to the NodeMap of distances.
298
299    ///Returns a reference to the NodeMap of distances. \pre \ref run() must
300    ///be called before using this function.
301    const DistMap &distMap() const { return *distance;}
302 
303    ///Returns a reference to the shortest path tree map.
304
305    ///Returns a reference to the NodeMap of the edges of the
306    ///shortest path tree.
307    ///\pre \ref run() must be called before using this function.
308    const PredMap &predMap() const { return *predecessor;}
309 
310    ///Returns a reference to the map of nodes of shortest paths.
311
312    ///Returns a reference to the NodeMap of the last but one nodes of the
313    ///shortest path tree.
314    ///\pre \ref run() must be called before using this function.
315    const PredNodeMap &predNodeMap() const { return *pred_node;}
316
317    ///Checks if a node is reachable from the root.
318
319    ///Returns \c true if \c v is reachable from the root.
320    ///\warning the root node is reported to be reached!
321    ///\pre \ref run() must be called before using this function.
322    ///
323    bool reached(Node v) { return v==source || (*predecessor)[v]!=INVALID; }
324   
325  };
326 
327/// @}
328 
329} //END OF NAMESPACE HUGO
330
331#endif
332
333
Note: See TracBrowser for help on using the repository browser.