jacint@50
|
1 |
/*
|
jacint@50
|
2 |
*dijkstra
|
jacint@50
|
3 |
*by jacint
|
jacint@50
|
4 |
*Performs Dijkstra's algorithm from node s.
|
jacint@50
|
5 |
*
|
jacint@50
|
6 |
*Constructor:
|
jacint@50
|
7 |
*
|
jacint@50
|
8 |
*dijkstra(graph_type& G, node_iterator s, edge_property_vector& distance)
|
jacint@50
|
9 |
*
|
jacint@50
|
10 |
*
|
jacint@50
|
11 |
*
|
jacint@50
|
12 |
*Member functions:
|
jacint@50
|
13 |
*
|
jacint@50
|
14 |
*void run()
|
jacint@50
|
15 |
*
|
jacint@50
|
16 |
* The following function should be used after run() was already run.
|
jacint@50
|
17 |
*
|
jacint@50
|
18 |
*
|
jacint@50
|
19 |
*T dist(node_iterator v) : returns the distance from s to v.
|
jacint@50
|
20 |
* It is 0 if v is not reachable from s.
|
jacint@50
|
21 |
*
|
jacint@50
|
22 |
*
|
jacint@50
|
23 |
*edge_iterator pred(node_iterator v)
|
jacint@50
|
24 |
* Returns the last edge of a shortest s-v path.
|
jacint@50
|
25 |
* Returns an invalid iterator if v=s or v is not
|
jacint@50
|
26 |
* reachable from s.
|
jacint@50
|
27 |
*
|
jacint@50
|
28 |
*
|
jacint@50
|
29 |
*bool reach(node_iterator v) : true if v is reachable from s
|
jacint@50
|
30 |
*
|
jacint@50
|
31 |
*
|
jacint@50
|
32 |
*
|
jacint@50
|
33 |
*
|
jacint@50
|
34 |
*
|
jacint@50
|
35 |
*Problems:
|
jacint@50
|
36 |
*
|
jacint@50
|
37 |
*Heap implementation is needed, because the priority queue of stl
|
jacint@50
|
38 |
*does not have a mathod for key-decrease, so we had to use here a
|
jacint@50
|
39 |
*g\'any solution.
|
jacint@50
|
40 |
*
|
jacint@50
|
41 |
*The implementation of infinity would be desirable, see after line 100.
|
jacint@50
|
42 |
*/
|
jacint@50
|
43 |
|
jacint@50
|
44 |
#ifndef DIJKSTRA_HH
|
jacint@50
|
45 |
#define DIJKSTRA_HH
|
jacint@50
|
46 |
|
jacint@50
|
47 |
#include <queue>
|
jacint@50
|
48 |
#include <algorithm>
|
jacint@50
|
49 |
|
jacint@50
|
50 |
#include <marci_graph_traits.hh>
|
jacint@50
|
51 |
#include <marci_property_vector.hh>
|
jacint@50
|
52 |
|
jacint@50
|
53 |
|
jacint@50
|
54 |
namespace std {
|
jacint@50
|
55 |
namespace marci {
|
jacint@50
|
56 |
|
jacint@50
|
57 |
|
jacint@50
|
58 |
|
jacint@50
|
59 |
|
jacint@50
|
60 |
|
jacint@50
|
61 |
template <typename graph_type, typename T>
|
jacint@50
|
62 |
class dijkstra{
|
jacint@50
|
63 |
typedef typename graph_traits<graph_type>::node_iterator node_iterator;
|
jacint@50
|
64 |
typedef typename graph_traits<graph_type>::edge_iterator edge_iterator;
|
jacint@50
|
65 |
typedef typename graph_traits<graph_type>::each_node_iterator each_node_iterator;
|
jacint@50
|
66 |
typedef typename graph_traits<graph_type>::in_edge_iterator in_edge_iterator;
|
jacint@50
|
67 |
typedef typename graph_traits<graph_type>::out_edge_iterator out_edge_iterator;
|
jacint@50
|
68 |
|
jacint@50
|
69 |
|
jacint@50
|
70 |
graph_type& G;
|
jacint@50
|
71 |
node_iterator s;
|
jacint@50
|
72 |
node_property_vector<graph_type, edge_iterator> predecessor;
|
jacint@50
|
73 |
node_property_vector<graph_type, T> distance;
|
jacint@50
|
74 |
edge_property_vector<graph_type, T> length;
|
jacint@50
|
75 |
node_property_vector<graph_type, bool> reached;
|
jacint@50
|
76 |
|
jacint@50
|
77 |
public :
|
jacint@50
|
78 |
|
jacint@50
|
79 |
/*
|
jacint@50
|
80 |
The distance of all the nodes is 0.
|
jacint@50
|
81 |
*/
|
jacint@50
|
82 |
dijkstra(graph_type& _G, node_iterator _s, edge_property_vector<graph_type, T>& _length) :
|
jacint@50
|
83 |
G(_G), s(_s), predecessor(G, 0), distance(G, 0), length(_length), reached(G, false) { }
|
jacint@50
|
84 |
|
jacint@50
|
85 |
|
jacint@50
|
86 |
|
jacint@50
|
87 |
/*By Misi.*/
|
jacint@50
|
88 |
struct node_dist_comp
|
jacint@50
|
89 |
{
|
jacint@50
|
90 |
node_property_vector<graph_type, T> &d;
|
jacint@50
|
91 |
node_dist_comp(node_property_vector<graph_type, T> &_d) : d(_d) {}
|
jacint@50
|
92 |
|
jacint@50
|
93 |
bool operator()(const node_iterator& u, const node_iterator& v) const
|
jacint@50
|
94 |
{ return d.get(u) < d.get(v); }
|
jacint@50
|
95 |
};
|
jacint@50
|
96 |
|
jacint@50
|
97 |
|
jacint@50
|
98 |
|
jacint@50
|
99 |
void run() {
|
jacint@50
|
100 |
|
jacint@50
|
101 |
node_property_vector<graph_type, bool> scanned(G, false);
|
jacint@50
|
102 |
std::priority_queue<node_iterator, vector<node_iterator>, node_dist_comp>
|
jacint@50
|
103 |
heap(( node_dist_comp(distance) ));
|
jacint@50
|
104 |
|
jacint@50
|
105 |
heap.push(s);
|
jacint@50
|
106 |
reached.put(s, true);
|
jacint@50
|
107 |
|
jacint@50
|
108 |
while (!heap.empty()) {
|
jacint@50
|
109 |
|
jacint@50
|
110 |
node_iterator v=heap.top();
|
jacint@50
|
111 |
heap.pop();
|
jacint@50
|
112 |
|
jacint@50
|
113 |
|
jacint@50
|
114 |
if (!scanned.get(v)) {
|
jacint@50
|
115 |
|
jacint@50
|
116 |
for(out_edge_iterator e=G.first_out_edge(v); e.valid(); ++e) {
|
jacint@50
|
117 |
node_iterator w=G.head(e);
|
jacint@50
|
118 |
|
jacint@50
|
119 |
if (!scanned.get(w)) {
|
jacint@50
|
120 |
if (!reached.get(w)) {
|
jacint@50
|
121 |
reached.put(w,true);
|
jacint@50
|
122 |
distance.put(w, distance.get(v)-length.get(e));
|
jacint@50
|
123 |
predecessor.put(w,e);
|
jacint@50
|
124 |
} else if (distance.get(v)-length.get(e)>distance.get(w)) {
|
jacint@50
|
125 |
distance.put(w, distance.get(v)-length.get(e));
|
jacint@50
|
126 |
predecessor.put(w,e);
|
jacint@50
|
127 |
}
|
jacint@50
|
128 |
|
jacint@50
|
129 |
heap.push(w);
|
jacint@50
|
130 |
|
jacint@50
|
131 |
}
|
jacint@50
|
132 |
|
jacint@50
|
133 |
}
|
jacint@50
|
134 |
scanned.put(v,true);
|
jacint@50
|
135 |
|
jacint@50
|
136 |
} // if (!scanned.get(v))
|
jacint@50
|
137 |
|
jacint@50
|
138 |
|
jacint@50
|
139 |
|
jacint@50
|
140 |
} // while (!heap.empty())
|
jacint@50
|
141 |
|
jacint@50
|
142 |
|
jacint@50
|
143 |
} //void run()
|
jacint@50
|
144 |
|
jacint@50
|
145 |
|
jacint@50
|
146 |
|
jacint@50
|
147 |
|
jacint@50
|
148 |
|
jacint@50
|
149 |
/*
|
jacint@50
|
150 |
*Returns the distance of the node v.
|
jacint@50
|
151 |
*It is 0 for the root and for the nodes not
|
jacint@50
|
152 |
*reachable form the root.
|
jacint@50
|
153 |
*/
|
jacint@50
|
154 |
T dist(node_iterator v) {
|
jacint@50
|
155 |
return -distance.get(v);
|
jacint@50
|
156 |
}
|
jacint@50
|
157 |
|
jacint@50
|
158 |
|
jacint@50
|
159 |
|
jacint@50
|
160 |
/*
|
jacint@50
|
161 |
* Returns the last edge of a shortest s-v path.
|
jacint@50
|
162 |
* Returns an invalid iterator if v=root or v is not
|
jacint@50
|
163 |
* reachable from the root.
|
jacint@50
|
164 |
*/
|
jacint@50
|
165 |
edge_iterator pred(node_iterator v) {
|
jacint@50
|
166 |
if (v!=s) { return predecessor.get(v);}
|
jacint@50
|
167 |
else {return edge_iterator();}
|
jacint@50
|
168 |
}
|
jacint@50
|
169 |
|
jacint@50
|
170 |
|
jacint@50
|
171 |
|
jacint@50
|
172 |
bool reach(node_iterator v) {
|
jacint@50
|
173 |
return reached.get(v);
|
jacint@50
|
174 |
}
|
jacint@50
|
175 |
|
jacint@50
|
176 |
|
jacint@50
|
177 |
|
jacint@50
|
178 |
|
jacint@50
|
179 |
|
jacint@50
|
180 |
|
jacint@50
|
181 |
|
jacint@50
|
182 |
|
jacint@50
|
183 |
|
jacint@50
|
184 |
};// class dijkstra
|
jacint@50
|
185 |
|
jacint@50
|
186 |
|
jacint@50
|
187 |
|
jacint@50
|
188 |
} // namespace marci
|
jacint@50
|
189 |
}
|
jacint@50
|
190 |
#endif //DIJKSTRA_HH
|
jacint@50
|
191 |
|
jacint@50
|
192 |
|