|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.rblasch.convert.dijkstra; |
|
17 |
| |
|
18 |
| import org.rblasch.convert.graph.Connection; |
|
19 |
| import org.rblasch.convert.graph.Edge; |
|
20 |
| import org.rblasch.convert.graph.ListPath; |
|
21 |
| import org.rblasch.convert.graph.Path; |
|
22 |
| import org.rblasch.convert.graph.Vertex; |
|
23 |
| import org.rblasch.convert.graph.WeightedDirectedGraph; |
|
24 |
| import org.rblasch.convert.graph.WeightedEdge; |
|
25 |
| |
|
26 |
| import java.util.Collections; |
|
27 |
| import java.util.Comparator; |
|
28 |
| import java.util.HashMap; |
|
29 |
| import java.util.HashSet; |
|
30 |
| import java.util.Iterator; |
|
31 |
| import java.util.LinkedList; |
|
32 |
| import java.util.List; |
|
33 |
| import java.util.Map; |
|
34 |
| import java.util.Set; |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| public class ShortestPathEngine { |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| public static final int INFINITE_DISTANCE = Integer.MAX_VALUE; |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| private final Comparator shortestDistanceComparator = new Comparator() { |
|
79 |
13701
| public int compare(final Object left, final Object right) {
|
|
80 |
| |
|
81 |
13701
| return getShortestDistance((Vertex) left) - getShortestDistance((Vertex) right);
|
|
82 |
| } |
|
83 |
| }; |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| private final WeightedDirectedGraph graph; |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| private final Set unsettledNodes = new HashSet(); |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| private final Set settledNodes = new HashSet(); |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| private final Map shortestDistances = new HashMap(); |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| private final Map predecessors = new HashMap(); |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
655
| public ShortestPathEngine(final WeightedDirectedGraph map) {
|
|
116 |
655
| this.graph = map;
|
|
117 |
| } |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
655
| private void init(final Vertex start) {
|
|
125 |
655
| settledNodes.clear();
|
|
126 |
655
| unsettledNodes.clear();
|
|
127 |
| |
|
128 |
655
| shortestDistances.clear();
|
|
129 |
655
| predecessors.clear();
|
|
130 |
| |
|
131 |
| |
|
132 |
655
| setShortestDistance(start, 0);
|
|
133 |
655
| unsettledNodes.add(start);
|
|
134 |
| } |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
655
| public void execute(final Vertex start, final Vertex destination) {
|
|
149 |
655
| init(start);
|
|
150 |
| |
|
151 |
| |
|
152 |
655
| Vertex u;
|
|
153 |
| |
|
154 |
| |
|
155 |
?
| while ((u = extractMin()) != null) {
|
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
0
| if (u == destination) break;
|
|
160 |
| |
|
161 |
5770
| markSettled(u);
|
|
162 |
| |
|
163 |
5770
| relaxNeighbors(u);
|
|
164 |
| } |
|
165 |
| } |
|
166 |
| |
|
167 |
655
| public Path getShortestPath(final Vertex start, final Vertex end) {
|
|
168 |
655
| execute(start, end);
|
|
169 |
| |
|
170 |
655
| final List path = new LinkedList();
|
|
171 |
655
| if (getShortestDistance(end) == ShortestPathEngine.INFINITE_DISTANCE) {
|
|
172 |
55
| return null;
|
|
173 |
| } |
|
174 |
600
| Vertex nextVertex = end;
|
|
175 |
600
| path.add(nextVertex);
|
|
176 |
600
| while (nextVertex != null) {
|
|
177 |
1690
| final Vertex vertex = getPredecessor(nextVertex);
|
|
178 |
1690
| if (vertex != null) {
|
|
179 |
1090
| path.add(getMinWeightEdge(vertex, nextVertex));
|
|
180 |
1090
| path.add(vertex);
|
|
181 |
| } |
|
182 |
1690
| nextVertex = vertex;
|
|
183 |
| } |
|
184 |
| |
|
185 |
600
| Collections.reverse(path);
|
|
186 |
| |
|
187 |
600
| return new ListPath(path);
|
|
188 |
| } |
|
189 |
| |
|
190 |
12294
| private Edge getMinWeightEdge(final Vertex start, final Vertex end) {
|
|
191 |
12294
| return ((WeightedEdge) ((Connection) Collections.min(graph.getConnections(start, end), new Comparator() {
|
|
192 |
0
| public int compare(final Object o1, final Object o2) {
|
|
193 |
0
| final Connection c1 = (Connection) o1;
|
|
194 |
0
| final Connection c2 = (Connection) o2;
|
|
195 |
| |
|
196 |
0
| return new Integer(((WeightedEdge) c1.getEdge()).getWeight()).compareTo(new Integer(((WeightedEdge) c2.getEdge()).getWeight()));
|
|
197 |
| } |
|
198 |
| })).getEdge()); |
|
199 |
| } |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
11204
| public int getMinWeight(final Vertex start, final Vertex end) {
|
|
205 |
11204
| return ((WeightedEdge) getMinWeightEdge(start, end)).getWeight();
|
|
206 |
| } |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
6425
| private Vertex extractMin() {
|
|
215 |
655
| if (unsettledNodes.isEmpty()) return null;
|
|
216 |
| |
|
217 |
5770
| final Object min = Collections.min(unsettledNodes, shortestDistanceComparator);
|
|
218 |
5770
| unsettledNodes.remove(min);
|
|
219 |
| |
|
220 |
5770
| return (Vertex) min;
|
|
221 |
| } |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
5770
| private void relaxNeighbors(final Vertex u) {
|
|
230 |
5770
| for (Iterator i = graph.getOutbound(u).iterator(); i.hasNext();) {
|
|
231 |
12590
| final Vertex v = (Vertex) i.next();
|
|
232 |
| |
|
233 |
| |
|
234 |
6501
| if (isSettled(v)) continue;
|
|
235 |
| |
|
236 |
6089
| if (getShortestDistance(v) > getShortestDistance(u) + getMinWeight(u, v)) {
|
|
237 |
| |
|
238 |
5115
| setShortestDistance(v, getShortestDistance(u) + getMinWeight(u, v));
|
|
239 |
| |
|
240 |
| |
|
241 |
5115
| setPredecessor(v, u);
|
|
242 |
| } |
|
243 |
| } |
|
244 |
| } |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
5770
| private void markSettled(final Vertex u) {
|
|
252 |
5770
| settledNodes.add(u);
|
|
253 |
| } |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
12590
| private boolean isSettled(final Vertex v) {
|
|
263 |
12590
| return settledNodes.contains(v);
|
|
264 |
| } |
|
265 |
| |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
45350
| public int getShortestDistance(final Vertex vertex) {
|
|
271 |
45350
| final Number d = (Number) shortestDistances.get(vertex);
|
|
272 |
45350
| return (d == null) ? INFINITE_DISTANCE : d.intValue();
|
|
273 |
| } |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
5770
| private void setShortestDistance(final Vertex vertex, final int distance) {
|
|
283 |
| |
|
284 |
| |
|
285 |
5770
| unsettledNodes.remove(vertex);
|
|
286 |
| |
|
287 |
5770
| shortestDistances.put(vertex, new Integer(distance));
|
|
288 |
| |
|
289 |
| |
|
290 |
| |
|
291 |
5770
| unsettledNodes.add(vertex);
|
|
292 |
| } |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
1690
| public Vertex getPredecessor(final Vertex vertex) {
|
|
299 |
1690
| return (Vertex) predecessors.get(vertex);
|
|
300 |
| } |
|
301 |
| |
|
302 |
5115
| private void setPredecessor(final Vertex a, final Vertex b) {
|
|
303 |
5115
| predecessors.put(a, b);
|
|
304 |
| } |
|
305 |
| } |