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