blob: e11ccb258b3e9a11fc2df24471f42067ac112e23 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
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
tome3489412014-08-29 02:30:38 -070018import org.junit.Test;
19
Thomas Vachuska26df2f22014-11-26 13:25:22 -080020import java.text.DecimalFormat;
21import java.util.HashSet;
tome3489412014-08-29 02:30:38 -070022import java.util.Set;
23
tom144de692014-08-29 11:38:44 -070024import static com.google.common.collect.ImmutableSet.of;
tome3489412014-08-29 02:30:38 -070025import static org.junit.Assert.assertEquals;
26
27/**
28 * Test of the Dijkstra algorithm.
29 */
30public class DijkstraGraphSearchTest extends BreadthFirstSearchTest {
31
32 @Override
tom144de692014-08-29 11:38:44 -070033 protected AbstractGraphPathSearch<TestVertex, TestEdge> graphSearch() {
tome3489412014-08-29 02:30:38 -070034 return new DijkstraGraphSearch<>();
35 }
36
37 @Test
38 @Override
tom144de692014-08-29 11:38:44 -070039 public void defaultGraphTest() {
40 executeDefaultTest(7, 5, 5.0);
tome3489412014-08-29 02:30:38 -070041 }
42
43 @Test
tom144de692014-08-29 11:38:44 -070044 @Override
45 public void defaultHopCountWeight() {
tome3489412014-08-29 02:30:38 -070046 weight = null;
tom144de692014-08-29 11:38:44 -070047 executeDefaultTest(10, 3, 3.0);
tome3489412014-08-29 02:30:38 -070048 }
49
50 @Test
51 public void noPath() {
tom0633d682014-09-10 12:10:03 -070052 graph = new AdjacencyListsGraph<>(of(A, B, C, D),
53 of(new TestEdge(A, B, 1),
54 new TestEdge(B, A, 1),
55 new TestEdge(C, D, 1),
56 new TestEdge(D, C, 1)));
tome3489412014-08-29 02:30:38 -070057 GraphPathSearch<TestVertex, TestEdge> gs = graphSearch();
tom0633d682014-09-10 12:10:03 -070058 Set<Path<TestVertex, TestEdge>> paths = gs.search(graph, A, B, weight).paths();
tome3489412014-08-29 02:30:38 -070059 printPaths(paths);
60 assertEquals("incorrect paths count", 1, paths.size());
61 assertEquals("incorrect path cost", 1.0, paths.iterator().next().cost(), 0.1);
62
tom0633d682014-09-10 12:10:03 -070063 paths = gs.search(graph, A, D, weight).paths();
tome3489412014-08-29 02:30:38 -070064 printPaths(paths);
65 assertEquals("incorrect paths count", 0, paths.size());
66
tom0633d682014-09-10 12:10:03 -070067 paths = gs.search(graph, A, null, weight).paths();
tome3489412014-08-29 02:30:38 -070068 printPaths(paths);
69 assertEquals("incorrect paths count", 1, paths.size());
70 assertEquals("incorrect path cost", 1.0, paths.iterator().next().cost(), 0.1);
71 }
72
73 @Test
tom144de692014-08-29 11:38:44 -070074 public void simpleMultiplePath() {
tom0633d682014-09-10 12:10:03 -070075 graph = new AdjacencyListsGraph<>(of(A, B, C, D),
76 of(new TestEdge(A, B, 1),
77 new TestEdge(A, C, 1),
78 new TestEdge(B, D, 1),
79 new TestEdge(C, D, 1)));
80 executeSearch(graphSearch(), graph, A, D, weight, 2, 2.0);
tome3489412014-08-29 02:30:38 -070081 }
82
83 @Test
tom144de692014-08-29 11:38:44 -070084 public void denseMultiplePath() {
tom0633d682014-09-10 12:10:03 -070085 graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G),
86 of(new TestEdge(A, B, 1),
87 new TestEdge(A, C, 1),
88 new TestEdge(B, D, 1),
89 new TestEdge(C, D, 1),
90 new TestEdge(D, E, 1),
91 new TestEdge(D, F, 1),
92 new TestEdge(E, G, 1),
93 new TestEdge(F, G, 1),
94 new TestEdge(A, G, 4)));
95 executeSearch(graphSearch(), graph, A, G, weight, 5, 4.0);
tome3489412014-08-29 02:30:38 -070096 }
97
98 @Test
tom144de692014-08-29 11:38:44 -070099 public void dualEdgeMultiplePath() {
tom0633d682014-09-10 12:10:03 -0700100 graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G, H),
101 of(new TestEdge(A, B, 1), new TestEdge(A, C, 3),
102 new TestEdge(B, D, 2), new TestEdge(B, C, 1),
103 new TestEdge(B, E, 4), new TestEdge(C, E, 1),
104 new TestEdge(D, H, 5), new TestEdge(D, E, 1),
105 new TestEdge(E, F, 1), new TestEdge(F, D, 1),
106 new TestEdge(F, G, 1), new TestEdge(F, H, 1),
107 new TestEdge(A, E, 3), new TestEdge(B, D, 1)));
108 executeSearch(graphSearch(), graph, A, E, weight, 3, 3.0);
tome3489412014-08-29 02:30:38 -0700109 }
110
Thomas Vachuska4d690872014-10-27 08:57:08 -0700111 @Test
112 public void negativeWeights() {
113 graph = new AdjacencyListsGraph<>(of(A, B, C, D, E, F, G),
114 of(new TestEdge(A, B, 1),
115 new TestEdge(A, C, -1),
116 new TestEdge(B, D, 1),
117 new TestEdge(D, A, -2),
118 new TestEdge(C, D, 1),
119 new TestEdge(D, E, 1),
120 new TestEdge(D, F, 1),
121 new TestEdge(E, G, 1),
122 new TestEdge(F, G, 1),
123 new TestEdge(G, A, -5),
124 new TestEdge(A, G, 4)));
125 executeSearch(graphSearch(), graph, A, G, weight, 3, 4.0);
126 }
127
Thomas Vachuska26df2f22014-11-26 13:25:22 -0800128 @Test
129 public void disconnectedPerf() {
130 disconnected();
131 disconnected();
132 disconnected();
133 disconnected();
134 disconnected();
135 disconnected();
136 disconnected();
137 disconnected();
138 disconnected();
139 disconnected();
140 }
141
142
143 @Test
144 public void disconnected() {
145 Set<TestVertex> vertexes = new HashSet<>();
146 for (int i = 0; i < 200; i++) {
147 vertexes.add(new TestVertex("v" + i));
148 }
149
150 graph = new AdjacencyListsGraph<>(vertexes, of());
151
152 long start = System.nanoTime();
153 for (TestVertex src : vertexes) {
154 executeSearch(graphSearch(), graph, src, null, null, 0, 0);
155 }
156 long end = System.nanoTime();
157 DecimalFormat fmt = new DecimalFormat("#,###");
158 System.out.println("Compute cost is " + fmt.format(end - start) + " nanos");
159 }
160
tome3489412014-08-29 02:30:38 -0700161}