blob: a4bcdc30a2461017e0320b531eac38630f2337ad [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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
Andrey Komarov2398d962016-09-26 15:11:23 +030030 protected Result<V, E> internalSearch(Graph<V, E> graph, V src, V dst,
31 EdgeWeigher<V, E> weigher, int maxPaths) {
tome3489412014-08-29 02:30:38 -070032
33 // Use the default result to remember cumulative costs and parent
34 // edges to each each respective vertex.
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080035 DefaultResult result = new DefaultResult(src, dst, maxPaths);
tome3489412014-08-29 02:30:38 -070036
37 // Cost to reach the source vertex is 0 of course.
Andrey Komarov2398d962016-09-26 15:11:23 +030038 result.updateVertex(src, null, weigher.getInitialWeight(), false);
tome3489412014-08-29 02:30:38 -070039
Thomas Vachuska26df2f22014-11-26 13:25:22 -080040 if (graph.getEdges().isEmpty()) {
41 result.buildPaths();
42 return result;
43 }
44
tome3489412014-08-29 02:30:38 -070045 // 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.
tom2e1f0712014-08-29 13:32:00 -070048 Heap<V> minQueue = createMinQueue(graph.getVertexes(),
tome3489412014-08-29 02:30:38 -070049 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 Komarov2398d962016-09-26 15:11:23 +030058 if (result.hasCost(nearest)) {
59 Weight cost = result.cost(nearest);
60
tome3489412014-08-29 02:30:38 -070061 // If the vertex is reachable, relax all its egress edges.
tom2e1f0712014-08-29 13:32:00 -070062 for (E e : graph.getEdgesFrom(nearest)) {
Andrey Komarov2398d962016-09-26 15:11:23 +030063 result.relaxEdge(e, cost, weigher, 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) {
Andrey Komarov2398d962016-09-26 15:11:23 +030087 //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));
tome3489412014-08-29 02:30:38 -070097 }
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}