tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 1 | package org.onlab.graph; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Comparator; |
| 5 | import java.util.Set; |
| 6 | |
| 7 | /** |
| 8 | * Dijkstra shortest-path graph search algorithm capable of finding not just |
| 9 | * one, but all shortest paths between the source and destinations. |
| 10 | */ |
| 11 | public class DijkstraGraphSearch<V extends Vertex, E extends Edge<V>> |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 12 | extends AbstractGraphPathSearch<V, E> { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 13 | |
| 14 | @Override |
| 15 | public Result<V, E> search(Graph<V, E> g, V src, V dst, EdgeWeight<V, E> ew) { |
| 16 | checkArguments(g, src, dst); |
| 17 | |
| 18 | // Use the default result to remember cumulative costs and parent |
| 19 | // edges to each each respective vertex. |
| 20 | DefaultResult result = new DefaultResult(src, dst); |
| 21 | |
| 22 | // Cost to reach the source vertex is 0 of course. |
| 23 | result.updateVertex(src, null, 0.0, false); |
| 24 | |
| 25 | // Use the min priority queue to progressively find each nearest |
| 26 | // vertex until we reach the desired destination, if one was given, |
| 27 | // or until we reach all possible destinations. |
| 28 | Heap<V> minQueue = createMinQueue(g.getVertexes(), |
| 29 | new PathCostComparator(result)); |
| 30 | while (!minQueue.isEmpty()) { |
| 31 | // Get the nearest vertex |
| 32 | V nearest = minQueue.extractExtreme(); |
| 33 | if (nearest.equals(dst)) { |
| 34 | break; |
| 35 | } |
| 36 | |
| 37 | // Find its cost and use it to determine if the vertex is reachable. |
| 38 | double cost = result.cost(nearest); |
| 39 | if (cost < Double.MAX_VALUE) { |
| 40 | // If the vertex is reachable, relax all its egress edges. |
| 41 | for (E e : g.getEdgesFrom(nearest)) { |
| 42 | result.relaxEdge(e, cost, ew); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // Re-prioritize the min queue. |
| 47 | minQueue.heapify(); |
| 48 | } |
| 49 | |
| 50 | // Now construct a set of paths from the results. |
| 51 | result.buildPaths(); |
| 52 | return result; |
| 53 | } |
| 54 | |
| 55 | // Compares path weights using their accrued costs; used for sorting the |
| 56 | // min priority queue. |
| 57 | private final class PathCostComparator implements Comparator<V> { |
| 58 | private final DefaultResult result; |
| 59 | |
| 60 | private PathCostComparator(DefaultResult result) { |
| 61 | this.result = result; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public int compare(V v1, V v2) { |
| 66 | double delta = result.cost(v2) - result.cost(v1); |
| 67 | return delta < 0 ? -1 : (delta > 0 ? 1 : 0); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Creates a min priority queue from the specified vertexes and comparator. |
| 72 | private Heap<V> createMinQueue(Set<V> vertexes, Comparator<V> comparator) { |
| 73 | return new Heap<>(new ArrayList<>(vertexes), comparator); |
| 74 | } |
| 75 | |
| 76 | } |