blob: 49907185a2daae1159254ef1695fd096a7a8ca24 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
tome3489412014-08-29 02:30:38 -070016package org.onlab.graph;
17
18import java.util.ArrayList;
19import java.util.Comparator;
20import 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 */
26public class DijkstraGraphSearch<V extends Vertex, E extends Edge<V>>
tom144de692014-08-29 11:38:44 -070027 extends AbstractGraphPathSearch<V, E> {
tome3489412014-08-29 02:30:38 -070028
29 @Override
tom2e1f0712014-08-29 13:32:00 -070030 public Result<V, E> search(Graph<V, E> graph, V src, V dst,
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080031 EdgeWeight<V, E> weight, int maxPaths) {
tom2e1f0712014-08-29 13:32:00 -070032 checkArguments(graph, src, dst);
tome3489412014-08-29 02:30:38 -070033
34 // Use the default result to remember cumulative costs and parent
35 // edges to each each respective vertex.
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080036 DefaultResult result = new DefaultResult(src, dst, maxPaths);
tome3489412014-08-29 02:30:38 -070037
38 // Cost to reach the source vertex is 0 of course.
39 result.updateVertex(src, null, 0.0, false);
40
Thomas Vachuska26df2f22014-11-26 13:25:22 -080041 if (graph.getEdges().isEmpty()) {
42 result.buildPaths();
43 return result;
44 }
45
tome3489412014-08-29 02:30:38 -070046 // 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.
tom2e1f0712014-08-29 13:32:00 -070049 Heap<V> minQueue = createMinQueue(graph.getVertexes(),
tome3489412014-08-29 02:30:38 -070050 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.
tom2e1f0712014-08-29 13:32:00 -070062 for (E e : graph.getEdgesFrom(nearest)) {
Thomas Vachuska4d690872014-10-27 08:57:08 -070063 result.relaxEdge(e, cost, weight, true);
tome3489412014-08-29 02:30:38 -070064 }
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}