blob: bdd7a84cd93b3d84734eeb878a4644f3e3f6afd0 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
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 */
tome3489412014-08-29 02:30:38 -070019package org.onlab.graph;
20
21import java.util.ArrayList;
22import java.util.Comparator;
23import 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 */
29public class DijkstraGraphSearch<V extends Vertex, E extends Edge<V>>
tom144de692014-08-29 11:38:44 -070030 extends AbstractGraphPathSearch<V, E> {
tome3489412014-08-29 02:30:38 -070031
32 @Override
tom2e1f0712014-08-29 13:32:00 -070033 public Result<V, E> search(Graph<V, E> graph, V src, V dst,
34 EdgeWeight<V, E> weight) {
35 checkArguments(graph, src, dst);
tome3489412014-08-29 02:30:38 -070036
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.
tom2e1f0712014-08-29 13:32:00 -070047 Heap<V> minQueue = createMinQueue(graph.getVertexes(),
tome3489412014-08-29 02:30:38 -070048 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.
tom2e1f0712014-08-29 13:32:00 -070060 for (E e : graph.getEdgesFrom(nearest)) {
Thomas Vachuska4d690872014-10-27 08:57:08 -070061 result.relaxEdge(e, cost, weight, true);
tome3489412014-08-29 02:30:38 -070062 }
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}