blob: e0628c1fe3abae7e72f854b665758e098aa9f9be [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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,
31 EdgeWeight<V, E> weight) {
32 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.
36 DefaultResult result = new DefaultResult(src, dst);
37
38 // Cost to reach the source vertex is 0 of course.
39 result.updateVertex(src, null, 0.0, false);
40
41 // Use the min priority queue to progressively find each nearest
42 // vertex until we reach the desired destination, if one was given,
43 // or until we reach all possible destinations.
tom2e1f0712014-08-29 13:32:00 -070044 Heap<V> minQueue = createMinQueue(graph.getVertexes(),
tome3489412014-08-29 02:30:38 -070045 new PathCostComparator(result));
46 while (!minQueue.isEmpty()) {
47 // Get the nearest vertex
48 V nearest = minQueue.extractExtreme();
49 if (nearest.equals(dst)) {
50 break;
51 }
52
53 // Find its cost and use it to determine if the vertex is reachable.
54 double cost = result.cost(nearest);
55 if (cost < Double.MAX_VALUE) {
56 // If the vertex is reachable, relax all its egress edges.
tom2e1f0712014-08-29 13:32:00 -070057 for (E e : graph.getEdgesFrom(nearest)) {
Thomas Vachuska4d690872014-10-27 08:57:08 -070058 result.relaxEdge(e, cost, weight, true);
tome3489412014-08-29 02:30:38 -070059 }
60 }
61
62 // Re-prioritize the min queue.
63 minQueue.heapify();
64 }
65
66 // Now construct a set of paths from the results.
67 result.buildPaths();
68 return result;
69 }
70
71 // Compares path weights using their accrued costs; used for sorting the
72 // min priority queue.
73 private final class PathCostComparator implements Comparator<V> {
74 private final DefaultResult result;
75
76 private PathCostComparator(DefaultResult result) {
77 this.result = result;
78 }
79
80 @Override
81 public int compare(V v1, V v2) {
82 double delta = result.cost(v2) - result.cost(v1);
83 return delta < 0 ? -1 : (delta > 0 ? 1 : 0);
84 }
85 }
86
87 // Creates a min priority queue from the specified vertexes and comparator.
88 private Heap<V> createMinQueue(Set<V> vertexes, Comparator<V> comparator) {
89 return new Heap<>(new ArrayList<>(vertexes), comparator);
90 }
91
92}