Thomas Vachuska
Committed by Gerrit Code Review

Added short-circuit to Dijkstra when there are no edges.

Change-Id: I7e647ffceeae9de1736c5f36159c33d882bdb9f2
...@@ -38,6 +38,11 @@ public class DijkstraGraphSearch<V extends Vertex, E extends Edge<V>> ...@@ -38,6 +38,11 @@ public class DijkstraGraphSearch<V extends Vertex, E extends Edge<V>>
38 // Cost to reach the source vertex is 0 of course. 38 // Cost to reach the source vertex is 0 of course.
39 result.updateVertex(src, null, 0.0, false); 39 result.updateVertex(src, null, 0.0, false);
40 40
41 + if (graph.getEdges().isEmpty()) {
42 + result.buildPaths();
43 + return result;
44 + }
45 +
41 // Use the min priority queue to progressively find each nearest 46 // Use the min priority queue to progressively find each nearest
42 // vertex until we reach the desired destination, if one was given, 47 // vertex until we reach the desired destination, if one was given,
43 // or until we reach all possible destinations. 48 // or until we reach all possible destinations.
......
...@@ -17,6 +17,8 @@ package org.onlab.graph; ...@@ -17,6 +17,8 @@ package org.onlab.graph;
17 17
18 import org.junit.Test; 18 import org.junit.Test;
19 19
20 +import java.text.DecimalFormat;
21 +import java.util.HashSet;
20 import java.util.Set; 22 import java.util.Set;
21 23
22 import static com.google.common.collect.ImmutableSet.of; 24 import static com.google.common.collect.ImmutableSet.of;
...@@ -123,4 +125,37 @@ public class DijkstraGraphSearchTest extends BreadthFirstSearchTest { ...@@ -123,4 +125,37 @@ public class DijkstraGraphSearchTest extends BreadthFirstSearchTest {
123 executeSearch(graphSearch(), graph, A, G, weight, 3, 4.0); 125 executeSearch(graphSearch(), graph, A, G, weight, 3, 4.0);
124 } 126 }
125 127
128 + @Test
129 + public void disconnectedPerf() {
130 + disconnected();
131 + disconnected();
132 + disconnected();
133 + disconnected();
134 + disconnected();
135 + disconnected();
136 + disconnected();
137 + disconnected();
138 + disconnected();
139 + disconnected();
140 + }
141 +
142 +
143 + @Test
144 + public void disconnected() {
145 + Set<TestVertex> vertexes = new HashSet<>();
146 + for (int i = 0; i < 200; i++) {
147 + vertexes.add(new TestVertex("v" + i));
148 + }
149 +
150 + graph = new AdjacencyListsGraph<>(vertexes, of());
151 +
152 + long start = System.nanoTime();
153 + for (TestVertex src : vertexes) {
154 + executeSearch(graphSearch(), graph, src, null, null, 0, 0);
155 + }
156 + long end = System.nanoTime();
157 + DecimalFormat fmt = new DecimalFormat("#,###");
158 + System.out.println("Compute cost is " + fmt.format(end - start) + " nanos");
159 + }
160 +
126 } 161 }
......