Showing
6 changed files
with
17 additions
and
27 deletions
... | @@ -12,7 +12,7 @@ public class BreadthFirstSearch<V extends Vertex, E extends Edge<V>> | ... | @@ -12,7 +12,7 @@ public class BreadthFirstSearch<V extends Vertex, E extends Edge<V>> |
12 | @Override | 12 | @Override |
13 | public Result<V, E> search(Graph<V, E> graph, V src, V dst, EdgeWeight<V, E> ew) { | 13 | public Result<V, E> search(Graph<V, E> graph, V src, V dst, EdgeWeight<V, E> ew) { |
14 | checkArguments(graph, src, dst); | 14 | checkArguments(graph, src, dst); |
15 | - | 15 | + |
16 | // Prepare the graph result. | 16 | // Prepare the graph result. |
17 | DefaultResult result = new DefaultResult(src, dst); | 17 | DefaultResult result = new DefaultResult(src, dst); |
18 | 18 | ||
... | @@ -20,15 +20,16 @@ public class BreadthFirstSearch<V extends Vertex, E extends Edge<V>> | ... | @@ -20,15 +20,16 @@ public class BreadthFirstSearch<V extends Vertex, E extends Edge<V>> |
20 | Set<V> frontier = new HashSet<>(); | 20 | Set<V> frontier = new HashSet<>(); |
21 | result.costs.put(src, 0.0); | 21 | result.costs.put(src, 0.0); |
22 | frontier.add(src); | 22 | frontier.add(src); |
23 | - | 23 | + |
24 | - search: while (!frontier.isEmpty()) { | 24 | + search: |
25 | + while (!frontier.isEmpty()) { | ||
25 | // Prepare the next frontier. | 26 | // Prepare the next frontier. |
26 | Set<V> next = new HashSet<>(); | 27 | Set<V> next = new HashSet<>(); |
27 | 28 | ||
28 | // Visit all vertexes in the current frontier. | 29 | // Visit all vertexes in the current frontier. |
29 | for (V vertex : frontier) { | 30 | for (V vertex : frontier) { |
30 | double cost = result.cost(vertex); | 31 | double cost = result.cost(vertex); |
31 | - | 32 | + |
32 | // Visit all egress edges of the current frontier vertex. | 33 | // Visit all egress edges of the current frontier vertex. |
33 | for (E edge : graph.getEdgesFrom(vertex)) { | 34 | for (E edge : graph.getEdgesFrom(vertex)) { |
34 | V nextVertex = edge.dst(); | 35 | V nextVertex = edge.dst(); |
... | @@ -38,20 +39,21 @@ public class BreadthFirstSearch<V extends Vertex, E extends Edge<V>> | ... | @@ -38,20 +39,21 @@ public class BreadthFirstSearch<V extends Vertex, E extends Edge<V>> |
38 | cost + (ew == null ? 1.0 : ew.weight(edge)), | 39 | cost + (ew == null ? 1.0 : ew.weight(edge)), |
39 | true); | 40 | true); |
40 | // If we have reached our intended destination, bail. | 41 | // If we have reached our intended destination, bail. |
41 | - if (nextVertex.equals(dst)) | 42 | + if (nextVertex.equals(dst)) { |
42 | break search; | 43 | break search; |
44 | + } | ||
43 | next.add(nextVertex); | 45 | next.add(nextVertex); |
44 | } | 46 | } |
45 | } | 47 | } |
46 | } | 48 | } |
47 | - | 49 | + |
48 | // Promote the next frontier. | 50 | // Promote the next frontier. |
49 | frontier = next; | 51 | frontier = next; |
50 | } | 52 | } |
51 | - | 53 | + |
52 | // Finally, but the paths on the search result and return. | 54 | // Finally, but the paths on the search result and return. |
53 | result.buildPaths(); | 55 | result.buildPaths(); |
54 | return result; | 56 | return result; |
55 | } | 57 | } |
56 | - | 58 | + |
57 | } | 59 | } | ... | ... |
1 | package org.onlab.graph; | 1 | package org.onlab.graph; |
2 | 2 | ||
3 | -import com.google.common.collect.ImmutableList; | ||
4 | import com.google.common.testing.EqualsTester; | 3 | import com.google.common.testing.EqualsTester; |
5 | import org.junit.Test; | 4 | import org.junit.Test; |
6 | 5 | ||
7 | -import java.util.Iterator; | ||
8 | -import java.util.List; | ||
9 | - | ||
10 | import static com.google.common.collect.ImmutableList.of; | 6 | import static com.google.common.collect.ImmutableList.of; |
11 | import static org.junit.Assert.assertEquals; | 7 | import static org.junit.Assert.assertEquals; |
12 | import static org.junit.Assert.assertNull; | 8 | import static org.junit.Assert.assertNull; | ... | ... |
1 | package org.onlab.graph; | 1 | package org.onlab.graph; |
2 | 2 | ||
3 | -import com.google.common.collect.ImmutableList; | ||
4 | import com.google.common.testing.EqualsTester; | 3 | import com.google.common.testing.EqualsTester; |
5 | import org.junit.Test; | 4 | import org.junit.Test; |
6 | 5 | ||
... | @@ -8,7 +7,6 @@ import java.util.List; | ... | @@ -8,7 +7,6 @@ import java.util.List; |
8 | 7 | ||
9 | import static com.google.common.collect.ImmutableList.of; | 8 | import static com.google.common.collect.ImmutableList.of; |
10 | import static org.junit.Assert.assertEquals; | 9 | import static org.junit.Assert.assertEquals; |
11 | -import static org.junit.Assert.assertNull; | ||
12 | 10 | ||
13 | /** | 11 | /** |
14 | * Test of the default path. | 12 | * Test of the default path. |
... | @@ -33,8 +31,8 @@ public class DefaultPathTest extends GraphTest { | ... | @@ -33,8 +31,8 @@ public class DefaultPathTest extends GraphTest { |
33 | 31 | ||
34 | // Validates the path against expected attributes | 32 | // Validates the path against expected attributes |
35 | protected void validatePath(Path<TestVertex, TestEdge> p, | 33 | protected void validatePath(Path<TestVertex, TestEdge> p, |
36 | - TestVertex src, TestVertex dst, | 34 | + TestVertex src, TestVertex dst, |
37 | - int length, double cost) { | 35 | + int length, double cost) { |
38 | assertEquals("incorrect path length", length, p.edges().size()); | 36 | assertEquals("incorrect path length", length, p.edges().size()); |
39 | assertEquals("incorrect source", src, p.src()); | 37 | assertEquals("incorrect source", src, p.src()); |
40 | assertEquals("incorrect destination", dst, p.dst()); | 38 | assertEquals("incorrect destination", dst, p.dst()); | ... | ... |
1 | package org.onlab.graph; | 1 | package org.onlab.graph; |
2 | 2 | ||
3 | -import com.google.common.collect.ImmutableSet; | ||
4 | - | ||
5 | import java.util.Set; | 3 | import java.util.Set; |
6 | 4 | ||
7 | import static com.google.common.collect.ImmutableSet.of; | 5 | import static com.google.common.collect.ImmutableSet.of; |
... | @@ -24,11 +22,11 @@ public class GraphTest { | ... | @@ -24,11 +22,11 @@ public class GraphTest { |
24 | 22 | ||
25 | protected EdgeWeight<TestVertex, TestEdge> weight = | 23 | protected EdgeWeight<TestVertex, TestEdge> weight = |
26 | new EdgeWeight<TestVertex, TestEdge>() { | 24 | new EdgeWeight<TestVertex, TestEdge>() { |
27 | - @Override | 25 | + @Override |
28 | - public double weight(TestEdge edge) { | 26 | + public double weight(TestEdge edge) { |
29 | - return edge.weight(); | 27 | + return edge.weight(); |
30 | - } | 28 | + } |
31 | - }; | 29 | + }; |
32 | 30 | ||
33 | protected void printPaths(Set<Path<TestVertex, TestEdge>> paths) { | 31 | protected void printPaths(Set<Path<TestVertex, TestEdge>> paths) { |
34 | for (Path p : paths) { | 32 | for (Path p : paths) { | ... | ... |
1 | package org.onlab.graph; | 1 | package org.onlab.graph; |
2 | 2 | ||
3 | -import com.google.common.collect.ImmutableList; | ||
4 | import com.google.common.collect.Ordering; | 3 | import com.google.common.collect.Ordering; |
5 | import com.google.common.testing.EqualsTester; | 4 | import com.google.common.testing.EqualsTester; |
6 | import org.junit.Test; | 5 | import org.junit.Test; |
7 | 6 | ||
8 | import java.util.ArrayList; | 7 | import java.util.ArrayList; |
9 | import java.util.Comparator; | 8 | import java.util.Comparator; |
10 | -import java.util.Iterator; | ||
11 | 9 | ||
12 | import static com.google.common.collect.ImmutableList.of; | 10 | import static com.google.common.collect.ImmutableList.of; |
13 | import static org.junit.Assert.*; | 11 | import static org.junit.Assert.*; | ... | ... |
-
Please register or login to post a comment