DijkstraGraphSearch.java
3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.onlab.graph;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Set;
/**
* Dijkstra shortest-path graph search algorithm capable of finding not just
* one, but all shortest paths between the source and destinations.
*/
public class DijkstraGraphSearch<V extends Vertex, E extends Edge<V>>
extends AbstractGraphPathSearch<V, E> {
@Override
public Result<V, E> search(Graph<V, E> graph, V src, V dst,
EdgeWeight<V, E> weight) {
checkArguments(graph, src, dst);
// Use the default result to remember cumulative costs and parent
// edges to each each respective vertex.
DefaultResult result = new DefaultResult(src, dst);
// Cost to reach the source vertex is 0 of course.
result.updateVertex(src, null, 0.0, false);
// Use the min priority queue to progressively find each nearest
// vertex until we reach the desired destination, if one was given,
// or until we reach all possible destinations.
Heap<V> minQueue = createMinQueue(graph.getVertexes(),
new PathCostComparator(result));
while (!minQueue.isEmpty()) {
// Get the nearest vertex
V nearest = minQueue.extractExtreme();
if (nearest.equals(dst)) {
break;
}
// Find its cost and use it to determine if the vertex is reachable.
double cost = result.cost(nearest);
if (cost < Double.MAX_VALUE) {
// If the vertex is reachable, relax all its egress edges.
for (E e : graph.getEdgesFrom(nearest)) {
result.relaxEdge(e, cost, weight, true);
}
}
// Re-prioritize the min queue.
minQueue.heapify();
}
// Now construct a set of paths from the results.
result.buildPaths();
return result;
}
// Compares path weights using their accrued costs; used for sorting the
// min priority queue.
private final class PathCostComparator implements Comparator<V> {
private final DefaultResult result;
private PathCostComparator(DefaultResult result) {
this.result = result;
}
@Override
public int compare(V v1, V v2) {
double delta = result.cost(v2) - result.cost(v1);
return delta < 0 ? -1 : (delta > 0 ? 1 : 0);
}
}
// Creates a min priority queue from the specified vertexes and comparator.
private Heap<V> createMinQueue(Set<V> vertexes, Comparator<V> comparator) {
return new Heap<>(new ArrayList<>(vertexes), comparator);
}
}