Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 16 | package org.onlab.graph; |
| 17 | |
Sbhat35 | 6137f03 | 2017-06-19 16:28:34 -0700 | [diff] [blame] | 18 | import org.junit.Rule; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 19 | import org.junit.Test; |
Sbhat35 | 6137f03 | 2017-06-19 16:28:34 -0700 | [diff] [blame] | 20 | import org.junit.rules.ExpectedException; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 21 | |
Thomas Vachuska | 26df2f2 | 2014-11-26 13:25:22 -0800 | [diff] [blame] | 22 | import java.text.DecimalFormat; |
| 23 | import java.util.HashSet; |
Sbhat35 | 6137f03 | 2017-06-19 16:28:34 -0700 | [diff] [blame] | 24 | import java.util.NoSuchElementException; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 25 | import java.util.Set; |
| 26 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 27 | import static com.google.common.collect.ImmutableSet.of; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 28 | import static org.junit.Assert.assertEquals; |
| 29 | |
| 30 | /** |
| 31 | * Test of the Dijkstra algorithm. |
| 32 | */ |
| 33 | public class DijkstraGraphSearchTest extends BreadthFirstSearchTest { |
| 34 | |
| 35 | @Override |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 36 | protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 37 | return new DijkstraGraphSearch<>(); |
| 38 | } |
| 39 | |
| 40 | @Test |
| 41 | @Override |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 42 | public void defaultGraphTest() { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 43 | executeDefaultTest(7, 5, new TestDoubleWeight(5.0)); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 47 | @Override |
| 48 | public void defaultHopCountWeight() { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 49 | weigher = null; |
| 50 | executeDefaultTest(10, 3, new ScalarWeight(3.0)); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Sbhat35 | 6137f03 | 2017-06-19 16:28:34 -0700 | [diff] [blame] | 53 | @Rule |
| 54 | public final ExpectedException exception = ExpectedException.none(); |
| 55 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 56 | @Test |
| 57 | public void noPath() { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 58 | graph = new AdjacencyListsGraph<>(of(A, B, C, D), |
Sbhat35 | 6137f03 | 2017-06-19 16:28:34 -0700 | [diff] [blame] | 59 | of(new TestEdge(A, B, W1), |
| 60 | new TestEdge(B, A, W1), |
| 61 | new TestEdge(C, D, W1), |
| 62 | new TestEdge(D, C, W1))); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 63 | GraphPathSearch<TestVertex, TestEdge> gs = graphSearch(); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 64 | Set<Path<TestVertex, TestEdge>> paths = gs.search(graph, A, B, weigher, 1).paths(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 65 | printPaths(paths); |
| 66 | assertEquals("incorrect paths count", 1, paths.size()); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 67 | assertEquals("incorrect path cost", new TestDoubleWeight(1.0), paths.iterator().next().cost()); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 68 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 69 | paths = gs.search(graph, A, D, weigher, 1).paths(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 70 | printPaths(paths); |
| 71 | assertEquals("incorrect paths count", 0, paths.size()); |
| 72 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 73 | paths = gs.search(graph, A, null, weigher, 1).paths(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 74 | printPaths(paths); |
| 75 | assertEquals("incorrect paths count", 1, paths.size()); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 76 | assertEquals("incorrect path cost", new TestDoubleWeight(1.0), paths.iterator().next().cost()); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Test |
Sbhat35 | 6137f03 | 2017-06-19 16:28:34 -0700 | [diff] [blame] | 80 | public void exceptions() { |
| 81 | graph = new AdjacencyListsGraph<>(of(A, B, C, D), |
| 82 | of(new TestEdge(A, B, W2), |
| 83 | new TestEdge(B, A, W1), |
| 84 | new TestEdge(A, A, W3), |
| 85 | new TestEdge(A, C, NW1), |
| 86 | new TestEdge(C, D, W3))); |
| 87 | GraphPathSearch<TestVertex, TestEdge> gs = graphSearch(); |
| 88 | Set<Path<TestVertex, TestEdge>> paths = gs.search(graph, A, D, weigher, GraphPathSearch.ALL_PATHS).paths(); |
| 89 | printPaths(paths); |
| 90 | assertEquals("Incorrect path count", 0, paths.size()); |
| 91 | |
| 92 | paths = gs.search(graph, A, A, weigher, 5).paths(); |
| 93 | exception.expect(NoSuchElementException.class); |
| 94 | paths.iterator().next().cost(); |
| 95 | } |
| 96 | |
| 97 | @Test |
| 98 | public void noEdges() { |
| 99 | graph = new AdjacencyListsGraph<>(vertexes(), of()); |
| 100 | for (TestVertex v: vertexes()) { |
| 101 | executeSearch(graphSearch(), graph, v, null, weigher, 0, new TestDoubleWeight(0)); |
| 102 | } |
| 103 | |
| 104 | } |
| 105 | |
| 106 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 107 | public void simpleMultiplePath() { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 108 | graph = new AdjacencyListsGraph<>(of(A, B, C, D), |
Sbhat35 | 6137f03 | 2017-06-19 16:28:34 -0700 | [diff] [blame] | 109 | of(new TestEdge(A, B, W1), |
| 110 | new TestEdge(A, C, W1), |
| 111 | new TestEdge(B, D, W1), |
| 112 | new TestEdge(C, D, W1), |
| 113 | new TestEdge(D, E, W1), |
| 114 | new TestEdge(A, E, ZW), |
| 115 | new TestEdge(E, F, NW1), |
| 116 | new TestEdge(F, B, ZW))); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 117 | executeSearch(graphSearch(), graph, A, D, weigher, 2, W2); |
| 118 | executeSinglePathSearch(graphSearch(), graph, A, D, weigher, 1, W2); |
Sbhat35 | 6137f03 | 2017-06-19 16:28:34 -0700 | [diff] [blame] | 119 | |
| 120 | executeSearch(graphSearch(), graph, A, B, weigher, 1, W1); |
| 121 | executeSearch(graphSearch(), graph, D, A, weigher, 0, null); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | @Test |
| 126 | public void manualDoubleWeights() { |
| 127 | graph = new AdjacencyListsGraph<>(of(A, B, C, D, E), |
| 128 | of(new TestEdge(A, B, new TestDoubleWeight(1.5)), |
| 129 | new TestEdge(B, D, new TestDoubleWeight(3.5)), |
| 130 | new TestEdge(A, C, new TestDoubleWeight(2.2)), |
| 131 | new TestEdge(C, E, new TestDoubleWeight(1.1)), |
| 132 | new TestEdge(E, D, new TestDoubleWeight(1.7)), |
| 133 | new TestEdge(A, D, new TestDoubleWeight(5.0)))); |
| 134 | executeSearch(graphSearch(), graph, A, D, weigher, 3, new TestDoubleWeight(5.0)); |
| 135 | executeSinglePathSearch(graphSearch(), graph, A, D, weigher, 1, W5); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 139 | public void denseMultiplePath() { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 140 | graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G), |
Sbhat35 | 6137f03 | 2017-06-19 16:28:34 -0700 | [diff] [blame] | 141 | of(new TestEdge(A, B, W1), |
| 142 | new TestEdge(A, C, W1), |
| 143 | new TestEdge(B, D, W1), |
| 144 | new TestEdge(C, D, W1), |
| 145 | new TestEdge(D, E, W1), |
| 146 | new TestEdge(D, F, W1), |
| 147 | new TestEdge(E, G, W1), |
| 148 | new TestEdge(F, G, W1), |
| 149 | new TestEdge(A, G, W4))); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 150 | executeSearch(graphSearch(), graph, A, G, weigher, 5, W4); |
| 151 | executeSinglePathSearch(graphSearch(), graph, A, G, weigher, 1, W4); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 155 | public void dualEdgeMultiplePath() { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 156 | graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G, H), |
Sbhat35 | 6137f03 | 2017-06-19 16:28:34 -0700 | [diff] [blame] | 157 | of(new TestEdge(A, B, W1), |
| 158 | new TestEdge(A, C, W3), |
| 159 | new TestEdge(B, D, W2), |
| 160 | new TestEdge(B, C, W1), |
| 161 | new TestEdge(B, E, W4), |
| 162 | new TestEdge(C, E, W1), |
| 163 | new TestEdge(D, H, W5), |
| 164 | new TestEdge(D, E, W1), |
| 165 | new TestEdge(E, F, W1), |
| 166 | new TestEdge(F, D, W1), |
| 167 | new TestEdge(F, G, W1), |
| 168 | new TestEdge(F, H, W1), |
| 169 | new TestEdge(A, E, W3), |
| 170 | new TestEdge(B, D, W1))); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 171 | executeSearch(graphSearch(), graph, A, E, weigher, 3, W3); |
| 172 | executeSinglePathSearch(graphSearch(), graph, A, E, weigher, 1, W3); |
Sbhat35 | 6137f03 | 2017-06-19 16:28:34 -0700 | [diff] [blame] | 173 | |
| 174 | GraphPathSearch<TestVertex, TestEdge> gs = graphSearch(); |
| 175 | Set<Path<TestVertex, TestEdge>> pathF = gs.search(graph, A, F, weigher, GraphPathSearch.ALL_PATHS).paths(); |
| 176 | Set<Path<TestVertex, TestEdge>> pathE = gs.search(graph, A, E, weigher, GraphPathSearch.ALL_PATHS).paths(); |
| 177 | assertEquals(0, pathF.size() - pathE.size()); |
| 178 | assertEquals(new TestDoubleWeight(1.0), |
| 179 | pathF.iterator().next().cost().subtract(pathE.iterator().next().cost())); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Thomas Vachuska | 4d69087 | 2014-10-27 08:57:08 -0700 | [diff] [blame] | 182 | @Test |
| 183 | public void negativeWeights() { |
| 184 | graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G), |
Sbhat35 | 6137f03 | 2017-06-19 16:28:34 -0700 | [diff] [blame] | 185 | of(new TestEdge(A, B, W1), |
| 186 | new TestEdge(A, C, NW1), |
| 187 | new TestEdge(B, D, W1), |
| 188 | new TestEdge(D, A, NW2), |
| 189 | new TestEdge(C, D, W1), |
| 190 | new TestEdge(D, E, W1), |
| 191 | new TestEdge(D, F, W1), |
| 192 | new TestEdge(E, G, W1), |
| 193 | new TestEdge(F, G, W1), |
| 194 | new TestEdge(G, A, NW5), |
| 195 | new TestEdge(A, G, W4))); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 196 | executeSearch(graphSearch(), graph, A, G, weigher, 3, new TestDoubleWeight(4.0)); |
| 197 | executeSinglePathSearch(graphSearch(), graph, A, G, weigher, 1, new TestDoubleWeight(4.0)); |
Thomas Vachuska | 4d69087 | 2014-10-27 08:57:08 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Thomas Vachuska | 26df2f2 | 2014-11-26 13:25:22 -0800 | [diff] [blame] | 200 | |
Sbhat35 | 6137f03 | 2017-06-19 16:28:34 -0700 | [diff] [blame] | 201 | @Test |
Thomas Vachuska | 26df2f2 | 2014-11-26 13:25:22 -0800 | [diff] [blame] | 202 | public void disconnected() { |
| 203 | Set<TestVertex> vertexes = new HashSet<>(); |
| 204 | for (int i = 0; i < 200; i++) { |
| 205 | vertexes.add(new TestVertex("v" + i)); |
| 206 | } |
| 207 | |
| 208 | graph = new AdjacencyListsGraph<>(vertexes, of()); |
| 209 | |
| 210 | long start = System.nanoTime(); |
| 211 | for (TestVertex src : vertexes) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 212 | executeSearch(graphSearch(), graph, src, null, null, 0, new TestDoubleWeight(0)); |
Thomas Vachuska | 26df2f2 | 2014-11-26 13:25:22 -0800 | [diff] [blame] | 213 | } |
| 214 | long end = System.nanoTime(); |
| 215 | DecimalFormat fmt = new DecimalFormat("#,###"); |
| 216 | System.out.println("Compute cost is " + fmt.format(end - start) + " nanos"); |
| 217 | } |
| 218 | |
Sbhat35 | 6137f03 | 2017-06-19 16:28:34 -0700 | [diff] [blame] | 219 | } |