COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/include/dijkstra.h @ 440:f92099d27236

Last change on this file since 440:f92099d27236 was 433:d9fac1497298, checked in by marci, 20 years ago

g++-3.4.0, misc

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