Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
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 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 18 | import org.junit.Test; |
| 19 | |
Thomas Vachuska | 26df2f2 | 2014-11-26 13:25:22 -0800 | [diff] [blame] | 20 | import java.text.DecimalFormat; |
| 21 | import java.util.HashSet; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 22 | import java.util.Set; |
| 23 | |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 24 | import static com.google.common.collect.ImmutableSet.of; |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 25 | import static org.junit.Assert.assertEquals; |
| 26 | |
| 27 | /** |
| 28 | * Test of the Dijkstra algorithm. |
| 29 | */ |
| 30 | public class DijkstraGraphSearchTest extends BreadthFirstSearchTest { |
| 31 | |
| 32 | @Override |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 33 | protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() { |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 34 | return new DijkstraGraphSearch<>(); |
| 35 | } |
| 36 | |
| 37 | @Test |
| 38 | @Override |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 39 | public void defaultGraphTest() { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 40 | executeDefaultTest(7, 5, new TestDoubleWeight(5.0)); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 44 | @Override |
| 45 | public void defaultHopCountWeight() { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 46 | weigher = null; |
| 47 | executeDefaultTest(10, 3, new ScalarWeight(3.0)); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Test |
| 51 | public void noPath() { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 52 | graph = new AdjacencyListsGraph<>(of(A, B, C, D), |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 53 | of(new TestEdge(A, B, W1), |
| 54 | new TestEdge(B, A, W1), |
| 55 | new TestEdge(C, D, W1), |
| 56 | new TestEdge(D, C, W1))); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 57 | GraphPathSearch<TestVertex, TestEdge> gs = graphSearch(); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 58 | Set<Path<TestVertex, TestEdge>> paths = gs.search(graph, A, B, weigher, 1).paths(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 59 | printPaths(paths); |
| 60 | assertEquals("incorrect paths count", 1, paths.size()); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 61 | assertEquals("incorrect path cost", new TestDoubleWeight(1.0), paths.iterator().next().cost()); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 62 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 63 | paths = gs.search(graph, A, D, weigher, 1).paths(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 64 | printPaths(paths); |
| 65 | assertEquals("incorrect paths count", 0, paths.size()); |
| 66 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 67 | paths = gs.search(graph, A, null, weigher, 1).paths(); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 68 | printPaths(paths); |
| 69 | assertEquals("incorrect paths count", 1, paths.size()); |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 70 | assertEquals("incorrect path cost", new TestDoubleWeight(1.0), paths.iterator().next().cost()); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 74 | public void simpleMultiplePath() { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 75 | graph = new AdjacencyListsGraph<>(of(A, B, C, D), |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 76 | of(new TestEdge(A, B, W1), |
| 77 | new TestEdge(A, C, W1), |
| 78 | new TestEdge(B, D, W1), |
| 79 | new TestEdge(C, D, W1))); |
| 80 | executeSearch(graphSearch(), graph, A, D, weigher, 2, W2); |
| 81 | executeSinglePathSearch(graphSearch(), graph, A, D, weigher, 1, W2); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 85 | public void denseMultiplePath() { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 86 | graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G), |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 87 | of(new TestEdge(A, B, W1), |
| 88 | new TestEdge(A, C, W1), |
| 89 | new TestEdge(B, D, W1), |
| 90 | new TestEdge(C, D, W1), |
| 91 | new TestEdge(D, E, W1), |
| 92 | new TestEdge(D, F, W1), |
| 93 | new TestEdge(E, G, W1), |
| 94 | new TestEdge(F, G, W1), |
| 95 | new TestEdge(A, G, W4))); |
| 96 | executeSearch(graphSearch(), graph, A, G, weigher, 5, W4); |
| 97 | executeSinglePathSearch(graphSearch(), graph, A, G, weigher, 1, W4); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Test |
tom | 144de69 | 2014-08-29 11:38:44 -0700 | [diff] [blame] | 101 | public void dualEdgeMultiplePath() { |
tom | 0633d68 | 2014-09-10 12:10:03 -0700 | [diff] [blame] | 102 | graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G, H), |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 103 | of(new TestEdge(A, B, W1), |
| 104 | new TestEdge(A, C, W3), |
| 105 | new TestEdge(B, D, W2), |
| 106 | new TestEdge(B, C, W1), |
| 107 | new TestEdge(B, E, W4), |
| 108 | new TestEdge(C, E, W1), |
| 109 | new TestEdge(D, H, W5), |
| 110 | new TestEdge(D, E, W1), |
| 111 | new TestEdge(E, F, W1), |
| 112 | new TestEdge(F, D, W1), |
| 113 | new TestEdge(F, G, W1), |
| 114 | new TestEdge(F, H, W1), |
| 115 | new TestEdge(A, E, W3), |
| 116 | new TestEdge(B, D, W1))); |
| 117 | executeSearch(graphSearch(), graph, A, E, weigher, 3, W3); |
| 118 | executeSinglePathSearch(graphSearch(), graph, A, E, weigher, 1, W3); |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Thomas Vachuska | 4d69087 | 2014-10-27 08:57:08 -0700 | [diff] [blame] | 121 | @Test |
| 122 | public void negativeWeights() { |
| 123 | graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G), |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 124 | of(new TestEdge(A, B, W1), |
| 125 | new TestEdge(A, C, NW1), |
| 126 | new TestEdge(B, D, W1), |
| 127 | new TestEdge(D, A, NW2), |
| 128 | new TestEdge(C, D, W1), |
| 129 | new TestEdge(D, E, W1), |
| 130 | new TestEdge(D, F, W1), |
| 131 | new TestEdge(E, G, W1), |
| 132 | new TestEdge(F, G, W1), |
| 133 | new TestEdge(G, A, NW5), |
| 134 | new TestEdge(A, G, W4))); |
| 135 | executeSearch(graphSearch(), graph, A, G, weigher, 3, new TestDoubleWeight(4.0)); |
| 136 | executeSinglePathSearch(graphSearch(), graph, A, G, weigher, 1, new TestDoubleWeight(4.0)); |
Thomas Vachuska | 4d69087 | 2014-10-27 08:57:08 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Thomas Vachuska | 26df2f2 | 2014-11-26 13:25:22 -0800 | [diff] [blame] | 139 | public void disconnectedPerf() { |
| 140 | disconnected(); |
| 141 | disconnected(); |
| 142 | disconnected(); |
| 143 | disconnected(); |
| 144 | disconnected(); |
| 145 | disconnected(); |
| 146 | disconnected(); |
| 147 | disconnected(); |
| 148 | disconnected(); |
| 149 | disconnected(); |
| 150 | } |
| 151 | |
Thomas Vachuska | 26df2f2 | 2014-11-26 13:25:22 -0800 | [diff] [blame] | 152 | public void disconnected() { |
| 153 | Set<TestVertex> vertexes = new HashSet<>(); |
| 154 | for (int i = 0; i < 200; i++) { |
| 155 | vertexes.add(new TestVertex("v" + i)); |
| 156 | } |
| 157 | |
| 158 | graph = new AdjacencyListsGraph<>(vertexes, of()); |
| 159 | |
| 160 | long start = System.nanoTime(); |
| 161 | for (TestVertex src : vertexes) { |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 162 | executeSearch(graphSearch(), graph, src, null, null, 0, new TestDoubleWeight(0)); |
Thomas Vachuska | 26df2f2 | 2014-11-26 13:25:22 -0800 | [diff] [blame] | 163 | } |
| 164 | long end = System.nanoTime(); |
| 165 | DecimalFormat fmt = new DecimalFormat("#,###"); |
| 166 | System.out.println("Compute cost is " + fmt.format(end - start) + " nanos"); |
| 167 | } |
| 168 | |
tom | e348941 | 2014-08-29 02:30:38 -0700 | [diff] [blame] | 169 | } |