COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/include/dijkstra.h @ 391:ddf25b2fa6ab

Last change on this file since 391:ddf25b2fa6ab was 385:d7ebbae96025, checked in by jacint, 21 years ago

Some changes in the documentation.

File size: 6.1 KB
Line 
1// -*- C++ -*-
2#ifndef HUGO_DIJKSTRA_H
3#define HUGO_DIJKSTRA_H
4
5///\file
6///\brief Dijkstra algorithm.
7
8#include <bin_heap.h>
9#include <invalid.h>
10
11namespace hugo {
12
13  ///%Dijkstra algorithm class.
14
15  ///This class provides an efficient implementation of %Dijkstra algorithm.
16  ///The edge lengths are passed to the algorithm using a
17  ///\ref ReadMapSkeleton "readable map",
18  ///so it is easy to change it to any kind of length.
19  ///
20  ///The type of the length is determined by the \c ValueType of the length map.
21  ///
22  ///It is also possible to change the underlying priority heap.
23  ///
24  ///\param Graph The graph type the algorithm runs on.
25  ///\param LengthMap This read-only
26  ///EdgeMap
27  ///determines the
28  ///lengths of the edges. It is read once for each edge, so the map
29  ///may involve in relatively time consuming process to compute the edge
30  ///length if it is necessary. The default map type is
31  ///\ref GraphSkeleton::EdgeMap "Graph::EdgeMap<int>"
32  ///\param Heap The heap type used by the %Dijkstra
33  ///algorithm. The default
34  ///is using \ref BinHeap "binary heap".
35 
36#ifdef DOXYGEN
37  template <typename Graph,
38            typename LengthMap,
39            typename Heap>
40#else
41  template <typename Graph,
42            typename LengthMap=typename Graph::EdgeMap<int>,
43            template <class,class,class> class Heap = BinHeap >
44#endif
45  class Dijkstra{
46  public:
47    typedef typename Graph::Node Node;
48    typedef typename Graph::NodeIt NodeIt;
49    typedef typename Graph::Edge Edge;
50    typedef typename Graph::OutEdgeIt OutEdgeIt;
51   
52    typedef typename LengthMap::ValueType ValueType;
53    typedef typename Graph::NodeMap<Edge> PredMap;
54    typedef typename Graph::NodeMap<Node> PredNodeMap;
55    typedef typename Graph::NodeMap<ValueType> DistMap;
56
57  private:
58    const Graph& G;
59    const LengthMap& length;
60    PredMap predecessor;
61    PredNodeMap pred_node;
62    DistMap distance;
63   
64  public :
65   
66    Dijkstra(Graph& _G, LengthMap& _length) :
67      G(_G), length(_length), predecessor(_G), pred_node(_G), distance(_G) { }
68   
69    void run(Node s);
70   
71    ///The distance of a node from the root.
72
73    ///Returns the distance of a node from the root.
74    ///\pre \ref run() must be called before using this function.
75    ///\warning If node \c v in unreachable from the root the return value
76    ///of this funcion is undefined.
77    ValueType dist(Node v) const { return distance[v]; }
78
79    ///Returns the previous edge of the shortest path tree.
80
81    ///For a node \c v it returns the previous edge of the shortest path tree,
82    ///i.e. it returns the last edge from a shortest path from the root to \c
83    ///v. It is INVALID if \c v is unreachable from the root or if \c v=s. The
84    ///shortest path tree used here is equal to the shortest path tree used in
85    ///\ref predNode(Node v).  \pre \ref run() must be called before using
86    ///this function.
87    Edge pred(Node v) const { return predecessor[v]; }
88
89    ///Returns the previous node of the shortest path tree.
90
91    ///For a node \c v it returns the previous node of the shortest path tree,
92    ///i.e. it returns the last but one node from a shortest path from the
93    ///root to \c /v. It is INVALID if \c v is unreachable from the root or if
94    ///\c v=s. The shortest path tree used here is equal to the shortest path
95    ///tree used in \ref pred(Node v).  \pre \ref run() must be called before
96    ///using this function.
97    Node predNode(Node v) const { return pred_node[v]; }
98   
99    ///Returns a reference to the NodeMap of distances.
100
101    ///Returns a reference to the NodeMap of distances. \pre \ref run() must
102    ///be called before using this function.
103    const DistMap &distMap() const { return distance;}
104 
105    ///Returns a reference to the shortest path tree map.
106
107    ///Returns a reference to the NodeMap of the edges of the
108    ///shortest path tree.
109    ///\pre \ref run() must be called before using this function.
110    const PredMap &predMap() const { return predecessor;}
111 
112    ///Returns a reference to the map of nodes of shortest paths.
113
114    ///Returns a reference to the NodeMap of the last but one nodes of the
115    ///shortest path tree.
116    ///\pre \ref run() must be called before using this function.
117    const PredNodeMap &predNodeMap() const { return pred_node;}
118
119    ///Checks if a node is reachable from the root.
120
121    ///Returns \c true if \c v is reachable from the root.
122    ///\warning the root node is reported to be unreached!
123    ///\todo Is this what we want?
124    ///\pre \ref run() must be called before using this function.
125    ///
126    bool reached(Node v) { return G.valid(predecessor[v]); }
127   
128  };
129 
130
131  // **********************************************************************
132  //  IMPLEMENTATIONS
133  // **********************************************************************
134
135  ///Runs %Dijkstra algorithm from node the root.
136
137  ///This method runs the %Dijkstra algorithm from a root node \c s
138  ///in order to
139  ///compute the
140  ///shortest path to each node. The algorithm computes
141  ///- The shortest path tree.
142  ///- The distance of each node from the root.
143  template <typename Graph, typename LengthMap,
144            template<class,class,class> class Heap >
145  void Dijkstra<Graph,LengthMap,Heap>::run(Node s) {
146   
147    NodeIt u;
148    for ( G.first(u) ; G.valid(u) ; G.next(u) ) {
149      predecessor.set(u,INVALID);
150      pred_node.set(u,INVALID);
151    }
152   
153    typename Graph::NodeMap<int> heap_map(G,-1);
154   
155    Heap<Node,ValueType,typename Graph::NodeMap<int> > heap(heap_map);
156   
157    heap.push(s,0);
158   
159      while ( !heap.empty() ) {
160       
161        Node v=heap.top();
162        ValueType oldvalue=heap[v];
163        heap.pop();
164        distance.set(v, oldvalue);
165       
166        { //FIXME this bracket is for e to be local
167          OutEdgeIt e;
168        for(G.first(e, v);
169            G.valid(e); G.next(e)) {
170          Node w=G.head(e);
171         
172          switch(heap.state(w)) {
173          case heap.PRE_HEAP:
174            heap.push(w,oldvalue+length[e]);
175            predecessor.set(w,e);
176            pred_node.set(w,v);
177            break;
178          case heap.IN_HEAP:
179            if ( oldvalue+length[e] < heap[w] ) {
180              heap.decrease(w, oldvalue+length[e]);
181              predecessor.set(w,e);
182              pred_node.set(w,v);
183            }
184            break;
185          case heap.POST_HEAP:
186            break;
187          }
188        }
189      } //FIXME tis bracket
190      }
191  }
192 
193} //END OF NAMESPACE HUGO
194
195#endif
196
197
Note: See TracBrowser for help on using the repository browser.