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>>
// Cost to reach the source vertex is 0 of course.
result.updateVertex(src, null, 0.0, false);
if (graph.getEdges().isEmpty()) {
result.buildPaths();
return result;
}
// Use the min priority queue to progressively find each nearest
// vertex until we reach the desired destination, if one was given,
// or until we reach all possible destinations.
......
......@@ -17,6 +17,8 @@ package org.onlab.graph;
import org.junit.Test;
import java.text.DecimalFormat;
import java.util.HashSet;
import java.util.Set;
import static com.google.common.collect.ImmutableSet.of;
......@@ -123,4 +125,37 @@ public class DijkstraGraphSearchTest extends BreadthFirstSearchTest {
executeSearch(graphSearch(), graph, A, G, weight, 3, 4.0);
}
@Test
public void disconnectedPerf() {
disconnected();
disconnected();
disconnected();
disconnected();
disconnected();
disconnected();
disconnected();
disconnected();
disconnected();
disconnected();
}
@Test
public void disconnected() {
Set<TestVertex> vertexes = new HashSet<>();
for (int i = 0; i < 200; i++) {
vertexes.add(new TestVertex("v" + i));
}
graph = new AdjacencyListsGraph<>(vertexes, of());
long start = System.nanoTime();
for (TestVertex src : vertexes) {
executeSearch(graphSearch(), graph, src, null, null, 0, 0);
}
long end = System.nanoTime();
DecimalFormat fmt = new DecimalFormat("#,###");
System.out.println("Compute cost is " + fmt.format(end - start) + " nanos");
}
}
......