Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 16 | package org.onlab.graph; |
| 17 | |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.Comparator; |
| 20 | import java.util.Set; |
| 21 | |
| 22 | /** |
| 23 | * Dijkstra shortest-path graph search algorithm capable of finding not just |
| 24 | * one, but all shortest paths between the source and destinations. |
| 25 | */ |
| 26 | public class DijkstraGraphSearch<V extends Vertex, E extends Edge<V>> |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 27 | extends AbstractGraphPathSearch<V, E> { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 28 | |
| 29 | @Override |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 30 | public Result<V, E> search(Graph<V, E> graph, V src, V dst, |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 31 | EdgeWeight<V, E> weight, int maxPaths) { |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 32 | checkArguments(graph, src, dst); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 33 | |
| 34 | // Use the default result to remember cumulative costs and parent |
| 35 | // edges to each each respective vertex. |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 36 | DefaultResult result = new DefaultResult(src, dst, maxPaths); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 37 | |
| 38 | // Cost to reach the source vertex is 0 of course. |
| 39 | result.updateVertex(src, null, 0.0, false); |
| 40 | |
Thomas Vachuska | 26df2f2 | 2014-11-26 13:25:22 -0800 | [diff] [blame] | 41 | if (graph.getEdges().isEmpty()) { |
| 42 | result.buildPaths(); |
| 43 | return result; |
| 44 | } |
| 45 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 46 | // Use the min priority queue to progressively find each nearest |
| 47 | // vertex until we reach the desired destination, if one was given, |
| 48 | // or until we reach all possible destinations. |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 49 | Heap<V> minQueue = createMinQueue(graph.getVertexes(), |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 50 | new PathCostComparator(result)); |
| 51 | while (!minQueue.isEmpty()) { |
| 52 | // Get the nearest vertex |
| 53 | V nearest = minQueue.extractExtreme(); |
| 54 | if (nearest.equals(dst)) { |
| 55 | break; |
| 56 | } |
| 57 | |
| 58 | // Find its cost and use it to determine if the vertex is reachable. |
| 59 | double cost = result.cost(nearest); |
| 60 | if (cost < Double.MAX_VALUE) { |
| 61 | // If the vertex is reachable, relax all its egress edges. |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 62 | for (E e : graph.getEdgesFrom(nearest)) { |
Thomas Vachuska | 4d69087 | 2014-10-27 08:57:08 -0700 | [diff] [blame] | 63 | result.relaxEdge(e, cost, weight, true); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | // Re-prioritize the min queue. |
| 68 | minQueue.heapify(); |
| 69 | } |
| 70 | |
| 71 | // Now construct a set of paths from the results. |
| 72 | result.buildPaths(); |
| 73 | return result; |
| 74 | } |
| 75 | |
| 76 | // Compares path weights using their accrued costs; used for sorting the |
| 77 | // min priority queue. |
| 78 | private final class PathCostComparator implements Comparator<V> { |
| 79 | private final DefaultResult result; |
| 80 | |
| 81 | private PathCostComparator(DefaultResult result) { |
| 82 | this.result = result; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public int compare(V v1, V v2) { |
| 87 | double delta = result.cost(v2) - result.cost(v1); |
| 88 | return delta < 0 ? -1 : (delta > 0 ? 1 : 0); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Creates a min priority queue from the specified vertexes and comparator. |
| 93 | private Heap<V> createMinQueue(Set<V> vertexes, Comparator<V> comparator) { |
| 94 | return new Heap<>(new ArrayList<>(vertexes), comparator); |
| 95 | } |
| 96 | |
| 97 | } |