blob: 1eea4a20a4da43bef584cfc630d384163c311531 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska24c849c2014-10-27 09:53:05 -070015 */
tome3489412014-08-29 02:30:38 -070016package org.onlab.graph;
17
Sbhat356137f032017-06-19 16:28:34 -070018import org.junit.Rule;
tome3489412014-08-29 02:30:38 -070019import org.junit.Test;
Sbhat356137f032017-06-19 16:28:34 -070020import org.junit.rules.ExpectedException;
tome3489412014-08-29 02:30:38 -070021
Thomas Vachuska26df2f22014-11-26 13:25:22 -080022import java.text.DecimalFormat;
23import java.util.HashSet;
Sbhat356137f032017-06-19 16:28:34 -070024import java.util.NoSuchElementException;
tome3489412014-08-29 02:30:38 -070025import java.util.Set;
26
tom144de692014-08-29 11:38:44 -070027import static com.google.common.collect.ImmutableSet.of;
tome3489412014-08-29 02:30:38 -070028import static org.junit.Assert.assertEquals;
29
30/**
31 * Test of the Dijkstra algorithm.
32 */
33public class DijkstraGraphSearchTest extends BreadthFirstSearchTest {
34
35 @Override
tom144de692014-08-29 11:38:44 -070036 protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() {
tome3489412014-08-29 02:30:38 -070037 return new DijkstraGraphSearch<>();
38 }
39
40 @Test
41 @Override
tom144de692014-08-29 11:38:44 -070042 public void defaultGraphTest() {
Andrey Komarov2398d962016-09-26 15:11:23 +030043 executeDefaultTest(7, 5, new TestDoubleWeight(5.0));
tome3489412014-08-29 02:30:38 -070044 }
45
46 @Test
tom144de692014-08-29 11:38:44 -070047 @Override
48 public void defaultHopCountWeight() {
Andrey Komarov2398d962016-09-26 15:11:23 +030049 weigher = null;
50 executeDefaultTest(10, 3, new ScalarWeight(3.0));
tome3489412014-08-29 02:30:38 -070051 }
52
Sbhat356137f032017-06-19 16:28:34 -070053 @Rule
54 public final ExpectedException exception = ExpectedException.none();
55
tome3489412014-08-29 02:30:38 -070056 @Test
57 public void noPath() {
tom0633d682014-09-10 12:10:03 -070058 graph = new AdjacencyListsGraph<>(of(A, B, C, D),
Sbhat356137f032017-06-19 16:28:34 -070059 of(new TestEdge(A, B, W1),
60 new TestEdge(B, A, W1),
61 new TestEdge(C, D, W1),
62 new TestEdge(D, C, W1)));
tome3489412014-08-29 02:30:38 -070063 GraphPathSearch<TestVertex, TestEdge> gs = graphSearch();
Andrey Komarov2398d962016-09-26 15:11:23 +030064 Set<Path<TestVertex, TestEdge>> paths = gs.search(graph, A, B, weigher, 1).paths();
tome3489412014-08-29 02:30:38 -070065 printPaths(paths);
66 assertEquals("incorrect paths count", 1, paths.size());
Andrey Komarov2398d962016-09-26 15:11:23 +030067 assertEquals("incorrect path cost", new TestDoubleWeight(1.0), paths.iterator().next().cost());
tome3489412014-08-29 02:30:38 -070068
Andrey Komarov2398d962016-09-26 15:11:23 +030069 paths = gs.search(graph, A, D, weigher, 1).paths();
tome3489412014-08-29 02:30:38 -070070 printPaths(paths);
71 assertEquals("incorrect paths count", 0, paths.size());
72
Andrey Komarov2398d962016-09-26 15:11:23 +030073 paths = gs.search(graph, A, null, weigher, 1).paths();
tome3489412014-08-29 02:30:38 -070074 printPaths(paths);
75 assertEquals("incorrect paths count", 1, paths.size());
Andrey Komarov2398d962016-09-26 15:11:23 +030076 assertEquals("incorrect path cost", new TestDoubleWeight(1.0), paths.iterator().next().cost());
tome3489412014-08-29 02:30:38 -070077 }
78
79 @Test
Sbhat356137f032017-06-19 16:28:34 -070080 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
tom144de692014-08-29 11:38:44 -0700107 public void simpleMultiplePath() {
tom0633d682014-09-10 12:10:03 -0700108 graph = new AdjacencyListsGraph<>(of(A, B, C, D),
Sbhat356137f032017-06-19 16:28:34 -0700109 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 Komarov2398d962016-09-26 15:11:23 +0300117 executeSearch(graphSearch(), graph, A, D, weigher, 2, W2);
118 executeSinglePathSearch(graphSearch(), graph, A, D, weigher, 1, W2);
Sbhat356137f032017-06-19 16:28:34 -0700119
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);
tome3489412014-08-29 02:30:38 -0700136 }
137
138 @Test
tom144de692014-08-29 11:38:44 -0700139 public void denseMultiplePath() {
tom0633d682014-09-10 12:10:03 -0700140 graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G),
Sbhat356137f032017-06-19 16:28:34 -0700141 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 Komarov2398d962016-09-26 15:11:23 +0300150 executeSearch(graphSearch(), graph, A, G, weigher, 5, W4);
151 executeSinglePathSearch(graphSearch(), graph, A, G, weigher, 1, W4);
tome3489412014-08-29 02:30:38 -0700152 }
153
154 @Test
tom144de692014-08-29 11:38:44 -0700155 public void dualEdgeMultiplePath() {
tom0633d682014-09-10 12:10:03 -0700156 graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G, H),
Sbhat356137f032017-06-19 16:28:34 -0700157 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 Komarov2398d962016-09-26 15:11:23 +0300171 executeSearch(graphSearch(), graph, A, E, weigher, 3, W3);
172 executeSinglePathSearch(graphSearch(), graph, A, E, weigher, 1, W3);
Sbhat356137f032017-06-19 16:28:34 -0700173
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()));
tome3489412014-08-29 02:30:38 -0700180 }
181
Thomas Vachuska4d690872014-10-27 08:57:08 -0700182 @Test
183 public void negativeWeights() {
184 graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G),
Sbhat356137f032017-06-19 16:28:34 -0700185 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 Komarov2398d962016-09-26 15:11:23 +0300196 executeSearch(graphSearch(), graph, A, G, weigher, 3, new TestDoubleWeight(4.0));
197 executeSinglePathSearch(graphSearch(), graph, A, G, weigher, 1, new TestDoubleWeight(4.0));
Thomas Vachuska4d690872014-10-27 08:57:08 -0700198 }
199
Thomas Vachuska26df2f22014-11-26 13:25:22 -0800200
Sbhat356137f032017-06-19 16:28:34 -0700201 @Test
Thomas Vachuska26df2f22014-11-26 13:25:22 -0800202 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 Komarov2398d962016-09-26 15:11:23 +0300212 executeSearch(graphSearch(), graph, src, null, null, 0, new TestDoubleWeight(0));
Thomas Vachuska26df2f22014-11-26 13:25:22 -0800213 }
214 long end = System.nanoTime();
215 DecimalFormat fmt = new DecimalFormat("#,###");
216 System.out.println("Compute cost is " + fmt.format(end - start) + " nanos");
217 }
218
Sbhat356137f032017-06-19 16:28:34 -0700219}