blob: ccc769f2b22f33e58a15868ca227f28282d3f705 [file] [log] [blame]
tome3489412014-08-29 02:30:38 -07001package org.onlab.graph;
2
tome3489412014-08-29 02:30:38 -07003import org.junit.Test;
4
5import java.util.Set;
6
tom144de692014-08-29 11:38:44 -07007import static com.google.common.collect.ImmutableSet.of;
tome3489412014-08-29 02:30:38 -07008import static org.junit.Assert.assertEquals;
9
10/**
11 * Test of the Dijkstra algorithm.
12 */
13public class DijkstraGraphSearchTest extends BreadthFirstSearchTest {
14
15 @Override
tom144de692014-08-29 11:38:44 -070016 protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() {
tome3489412014-08-29 02:30:38 -070017 return new DijkstraGraphSearch<>();
18 }
19
20 @Test
21 @Override
tom144de692014-08-29 11:38:44 -070022 public void defaultGraphTest() {
23 executeDefaultTest(7, 5, 5.0);
tome3489412014-08-29 02:30:38 -070024 }
25
26 @Test
tom144de692014-08-29 11:38:44 -070027 @Override
28 public void defaultHopCountWeight() {
tome3489412014-08-29 02:30:38 -070029 weight = null;
tom144de692014-08-29 11:38:44 -070030 executeDefaultTest(10, 3, 3.0);
tome3489412014-08-29 02:30:38 -070031 }
32
33 @Test
34 public void noPath() {
tom0633d682014-09-10 12:10:03 -070035 graph = new AdjacencyListsGraph<>(of(A, B, C, D),
36 of(new TestEdge(A, B, 1),
37 new TestEdge(B, A, 1),
38 new TestEdge(C, D, 1),
39 new TestEdge(D, C, 1)));
tome3489412014-08-29 02:30:38 -070040 GraphPathSearch<TestVertex, TestEdge> gs = graphSearch();
tom0633d682014-09-10 12:10:03 -070041 Set<Path<TestVertex, TestEdge>> paths = gs.search(graph, A, B, weight).paths();
tome3489412014-08-29 02:30:38 -070042 printPaths(paths);
43 assertEquals("incorrect paths count", 1, paths.size());
44 assertEquals("incorrect path cost", 1.0, paths.iterator().next().cost(), 0.1);
45
tom0633d682014-09-10 12:10:03 -070046 paths = gs.search(graph, A, D, weight).paths();
tome3489412014-08-29 02:30:38 -070047 printPaths(paths);
48 assertEquals("incorrect paths count", 0, paths.size());
49
tom0633d682014-09-10 12:10:03 -070050 paths = gs.search(graph, A, null, weight).paths();
tome3489412014-08-29 02:30:38 -070051 printPaths(paths);
52 assertEquals("incorrect paths count", 1, paths.size());
53 assertEquals("incorrect path cost", 1.0, paths.iterator().next().cost(), 0.1);
54 }
55
56 @Test
tom144de692014-08-29 11:38:44 -070057 public void simpleMultiplePath() {
tom0633d682014-09-10 12:10:03 -070058 graph = new AdjacencyListsGraph<>(of(A, B, C, D),
59 of(new TestEdge(A, B, 1),
60 new TestEdge(A, C, 1),
61 new TestEdge(B, D, 1),
62 new TestEdge(C, D, 1)));
63 executeSearch(graphSearch(), graph, A, D, weight, 2, 2.0);
tome3489412014-08-29 02:30:38 -070064 }
65
66 @Test
tom144de692014-08-29 11:38:44 -070067 public void denseMultiplePath() {
tom0633d682014-09-10 12:10:03 -070068 graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G),
69 of(new TestEdge(A, B, 1),
70 new TestEdge(A, C, 1),
71 new TestEdge(B, D, 1),
72 new TestEdge(C, D, 1),
73 new TestEdge(D, E, 1),
74 new TestEdge(D, F, 1),
75 new TestEdge(E, G, 1),
76 new TestEdge(F, G, 1),
77 new TestEdge(A, G, 4)));
78 executeSearch(graphSearch(), graph, A, G, weight, 5, 4.0);
tome3489412014-08-29 02:30:38 -070079 }
80
81 @Test
tom144de692014-08-29 11:38:44 -070082 public void dualEdgeMultiplePath() {
tom0633d682014-09-10 12:10:03 -070083 graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G, H),
84 of(new TestEdge(A, B, 1), new TestEdge(A, C, 3),
85 new TestEdge(B, D, 2), new TestEdge(B, C, 1),
86 new TestEdge(B, E, 4), new TestEdge(C, E, 1),
87 new TestEdge(D, H, 5), new TestEdge(D, E, 1),
88 new TestEdge(E, F, 1), new TestEdge(F, D, 1),
89 new TestEdge(F, G, 1), new TestEdge(F, H, 1),
90 new TestEdge(A, E, 3), new TestEdge(B, D, 1)));
91 executeSearch(graphSearch(), graph, A, E, weight, 3, 3.0);
tome3489412014-08-29 02:30:38 -070092 }
93
Thomas Vachuska4d690872014-10-27 08:57:08 -070094 @Test
95 public void negativeWeights() {
96 graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G),
97 of(new TestEdge(A, B, 1),
98 new TestEdge(A, C, -1),
99 new TestEdge(B, D, 1),
100 new TestEdge(D, A, -2),
101 new TestEdge(C, D, 1),
102 new TestEdge(D, E, 1),
103 new TestEdge(D, F, 1),
104 new TestEdge(E, G, 1),
105 new TestEdge(F, G, 1),
106 new TestEdge(G, A, -5),
107 new TestEdge(A, G, 4)));
108 executeSearch(graphSearch(), graph, A, G, weight, 3, 4.0);
109 }
110
tome3489412014-08-29 02:30:38 -0700111}