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