Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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 |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 30 | protected Result<V, E> internalSearch(Graph<V, E> graph, V src, V dst, |
| 31 | EdgeWeigher<V, E> weigher, int maxPaths) { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 32 | |
| 33 | // Use the default result to remember cumulative costs and parent |
| 34 | // edges to each each respective vertex. |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 35 | DefaultResult result = new DefaultResult(src, dst, maxPaths); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 36 | |
| 37 | // Cost to reach the source vertex is 0 of course. |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 38 | result.updateVertex(src, null, weigher.getInitialWeight(), false); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 39 | |
Thomas Vachuska | 26df2f2 | 2014-11-26 13:25:22 -0800 | [diff] [blame] | 40 | if (graph.getEdges().isEmpty()) { |
| 41 | result.buildPaths(); |
| 42 | return result; |
| 43 | } |
| 44 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 45 | // Use the min priority queue to progressively find each nearest |
| 46 | // vertex until we reach the desired destination, if one was given, |
| 47 | // or until we reach all possible destinations. |
tom | 2e1f071 | 2014-08-29 13:32:00 -0700 | [diff] [blame] | 48 | Heap<V> minQueue = createMinQueue(graph.getVertexes(), |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 49 | new PathCostComparator(result)); |
| 50 | while (!minQueue.isEmpty()) { |
| 51 | // Get the nearest vertex |
| 52 | V nearest = minQueue.extractExtreme(); |
| 53 | if (nearest.equals(dst)) { |
| 54 | break; |
| 55 | } |
| 56 | |
| 57 | // Find its cost and use it to determine if the vertex is reachable. |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 58 | if (result.hasCost(nearest)) { |
| 59 | Weight cost = result.cost(nearest); |
| 60 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 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)) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 63 | result.relaxEdge(e, cost, weigher, 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) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 87 | //not accessed vertices should be pushed to the back of the queue |
| 88 | if (!result.hasCost(v1) && !result.hasCost(v2)) { |
| 89 | return 0; |
| 90 | } else if (!result.hasCost(v1)) { |
| 91 | return -1; |
| 92 | } else if (!result.hasCost(v2)) { |
| 93 | return 1; |
| 94 | } |
| 95 | |
| 96 | return result.cost(v2).compareTo(result.cost(v1)); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | // Creates a min priority queue from the specified vertexes and comparator. |
| 101 | private Heap<V> createMinQueue(Set<V> vertexes, Comparator<V> comparator) { |
| 102 | return new Heap<>(new ArrayList<>(vertexes), comparator); |
| 103 | } |
| 104 | |
| 105 | } |