This repository was archived by the owner on Jun 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestPathFinderTest.java
More file actions
308 lines (256 loc) · 11.5 KB
/
Copy pathShortestPathFinderTest.java
File metadata and controls
308 lines (256 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package gvisual;
import edu.uci.ics.jung.graph.UndirectedSparseGraph;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.Graph;
import org.junit.Test;
import java.util.*;
import static org.junit.Assert.*;
/**
* Tests for {@link ShortestPathFinder} — BFS shortest path, Dijkstra
* weighted shortest path, reachability, and connectivity.
*/
public class ShortestPathFinderTest {
// ── Helpers ─────────────────────────────────────────────────────
private static edge addEdge(Graph<String, edge> g, String v1, String v2) {
g.addVertex(v1);
g.addVertex(v2);
edge e = new edge("f", v1, v2);
g.addEdge(e, v1, v2);
return e;
}
private static edge addWeightedEdge(Graph<String, edge> g, String v1, String v2, float w) {
g.addVertex(v1);
g.addVertex(v2);
edge e = new edge("f", v1, v2);
e.setWeight(w);
g.addEdge(e, v1, v2);
return e;
}
// ── Constructor ─────────────────────────────────────────────────
@Test(expected = IllegalArgumentException.class)
public void constructor_nullGraph_throws() {
new ShortestPathFinder(null);
}
// ── findShortestByHops ──────────────────────────────────────────
@Test
public void byHops_sameVertex_returnsZeroHopPath() {
Graph<String, edge> g = new UndirectedSparseGraph<>();
g.addVertex("A");
ShortestPathFinder spf = new ShortestPathFinder(g);
ShortestPathFinder.PathResult result = spf.findShortestByHops("A", "A");
assertNotNull(result);
assertEquals(1, result.getVertices().size());
assertEquals("A", result.getVertices().get(0));
assertEquals(0, result.getHopCount());
assertEquals(0.0, result.getTotalWeight(), 1e-9);
}
@Test
public void byHops_directNeighbor_returns1Hop() {
Graph<String, edge> g = new UndirectedSparseGraph<>();
addEdge(g, "A", "B");
ShortestPathFinder spf = new ShortestPathFinder(g);
ShortestPathFinder.PathResult result = spf.findShortestByHops("A", "B");
assertNotNull(result);
assertEquals(Arrays.asList("A", "B"), result.getVertices());
assertEquals(1, result.getHopCount());
}
@Test
public void byHops_choosesFewestHops_notLowestWeight() {
// A --1-- B --1-- C (2 hops)
// A --10-- C (1 hop, heavier)
Graph<String, edge> g = new UndirectedSparseGraph<>();
addWeightedEdge(g, "A", "B", 1f);
addWeightedEdge(g, "B", "C", 1f);
addWeightedEdge(g, "A", "C", 10f);
ShortestPathFinder spf = new ShortestPathFinder(g);
ShortestPathFinder.PathResult result = spf.findShortestByHops("A", "C");
assertNotNull(result);
assertEquals(1, result.getHopCount()); // direct edge = 1 hop
assertEquals(Arrays.asList("A", "C"), result.getVertices());
}
@Test
public void byHops_disconnectedVertices_returnsNull() {
Graph<String, edge> g = new UndirectedSparseGraph<>();
g.addVertex("A");
g.addVertex("B");
ShortestPathFinder spf = new ShortestPathFinder(g);
assertNull(spf.findShortestByHops("A", "B"));
}
@Test
public void byHops_linearChain_returnsCorrectPath() {
Graph<String, edge> g = new UndirectedSparseGraph<>();
addEdge(g, "A", "B");
addEdge(g, "B", "C");
addEdge(g, "C", "D");
ShortestPathFinder spf = new ShortestPathFinder(g);
ShortestPathFinder.PathResult result = spf.findShortestByHops("A", "D");
assertNotNull(result);
assertEquals(3, result.getHopCount());
assertEquals(Arrays.asList("A", "B", "C", "D"), result.getVertices());
assertEquals(3, result.getEdges().size());
}
@Test(expected = IllegalArgumentException.class)
public void byHops_nullSource_throws() {
Graph<String, edge> g = new UndirectedSparseGraph<>();
g.addVertex("A");
new ShortestPathFinder(g).findShortestByHops(null, "A");
}
@Test(expected = IllegalArgumentException.class)
public void byHops_vertexNotInGraph_throws() {
Graph<String, edge> g = new UndirectedSparseGraph<>();
g.addVertex("A");
new ShortestPathFinder(g).findShortestByHops("A", "Z");
}
// ── findShortestByWeight (Dijkstra) ─────────────────────────────
@Test
public void byWeight_sameVertex_returnsZeroWeight() {
Graph<String, edge> g = new UndirectedSparseGraph<>();
g.addVertex("X");
ShortestPathFinder spf = new ShortestPathFinder(g);
ShortestPathFinder.PathResult result = spf.findShortestByWeight("X", "X");
assertNotNull(result);
assertEquals(0, result.getHopCount());
assertEquals(0.0, result.getTotalWeight(), 1e-9);
}
@Test
public void byWeight_prefersLighterPath() {
// A --1-- B --1-- C (weight 2, 2 hops)
// A --10-- C (weight 10, 1 hop)
Graph<String, edge> g = new UndirectedSparseGraph<>();
addWeightedEdge(g, "A", "B", 1f);
addWeightedEdge(g, "B", "C", 1f);
addWeightedEdge(g, "A", "C", 10f);
ShortestPathFinder spf = new ShortestPathFinder(g);
ShortestPathFinder.PathResult result = spf.findShortestByWeight("A", "C");
assertNotNull(result);
assertEquals(2.0, result.getTotalWeight(), 1e-9);
assertEquals(Arrays.asList("A", "B", "C"), result.getVertices());
}
@Test
public void byWeight_zeroWeightEdges_treatedAsWeight1() {
// Zero-weight edges should be normalized to 1.0
Graph<String, edge> g = new UndirectedSparseGraph<>();
addEdge(g, "A", "B"); // weight 0 (default) -> treated as 1.0
addEdge(g, "B", "C"); // weight 0 -> treated as 1.0
ShortestPathFinder spf = new ShortestPathFinder(g);
ShortestPathFinder.PathResult result = spf.findShortestByWeight("A", "C");
assertNotNull(result);
assertEquals(2.0, result.getTotalWeight(), 1e-9);
}
@Test
public void byWeight_disconnected_returnsNull() {
Graph<String, edge> g = new UndirectedSparseGraph<>();
g.addVertex("A");
g.addVertex("B");
ShortestPathFinder spf = new ShortestPathFinder(g);
assertNull(spf.findShortestByWeight("A", "B"));
}
@Test(expected = IllegalArgumentException.class)
public void byWeight_negativeEdge_throws() {
Graph<String, edge> g = new UndirectedSparseGraph<>();
addWeightedEdge(g, "A", "B", -5f);
new ShortestPathFinder(g).findShortestByWeight("A", "B");
}
// ── getReachableVertices ────────────────────────────────────────
@Test
public void reachable_isolatedVertex_returnsSelf() {
Graph<String, edge> g = new UndirectedSparseGraph<>();
g.addVertex("A");
ShortestPathFinder spf = new ShortestPathFinder(g);
Set<String> reachable = spf.getReachableVertices("A");
assertEquals(Collections.singleton("A"), reachable);
}
@Test
public void reachable_connectedComponent_returnsAll() {
Graph<String, edge> g = new UndirectedSparseGraph<>();
addEdge(g, "A", "B");
addEdge(g, "B", "C");
g.addVertex("D"); // isolated
ShortestPathFinder spf = new ShortestPathFinder(g);
Set<String> reachable = spf.getReachableVertices("A");
assertEquals(new HashSet<>(Arrays.asList("A", "B", "C")), reachable);
assertFalse(reachable.contains("D"));
}
// ── areConnected ────────────────────────────────────────────────
@Test
public void areConnected_sameVertex_returnsTrue() {
Graph<String, edge> g = new UndirectedSparseGraph<>();
g.addVertex("A");
assertTrue(new ShortestPathFinder(g).areConnected("A", "A"));
}
@Test
public void areConnected_directNeighbors_returnsTrue() {
Graph<String, edge> g = new UndirectedSparseGraph<>();
addEdge(g, "A", "B");
assertTrue(new ShortestPathFinder(g).areConnected("A", "B"));
}
@Test
public void areConnected_disconnected_returnsFalse() {
Graph<String, edge> g = new UndirectedSparseGraph<>();
g.addVertex("A");
g.addVertex("B");
assertFalse(new ShortestPathFinder(g).areConnected("A", "B"));
}
@Test
public void areConnected_transitivelyConnected_returnsTrue() {
Graph<String, edge> g = new UndirectedSparseGraph<>();
addEdge(g, "A", "B");
addEdge(g, "B", "C");
addEdge(g, "C", "D");
assertTrue(new ShortestPathFinder(g).areConnected("A", "D"));
}
// ── PathResult.toString ─────────────────────────────────────────
@Test
public void pathResult_toString_formatsCorrectly() {
Graph<String, edge> g = new UndirectedSparseGraph<>();
addWeightedEdge(g, "A", "B", 3f);
addWeightedEdge(g, "B", "C", 4f);
ShortestPathFinder spf = new ShortestPathFinder(g);
ShortestPathFinder.PathResult result = spf.findShortestByHops("A", "C");
assertNotNull(result);
String str = result.toString();
assertTrue(str.contains("A"));
assertTrue(str.contains("→"));
assertTrue(str.contains("C"));
assertTrue(str.contains("2 hops"));
}
// ── Directed graph support ──────────────────────────────────────
@Test
public void byHops_directedGraph_respectsEdgeDirection() {
Graph<String, edge> g = new DirectedSparseGraph<>();
addEdge(g, "A", "B"); // A -> B only
addEdge(g, "B", "C"); // B -> C only
ShortestPathFinder spf = new ShortestPathFinder(g);
// Forward direction should work
ShortestPathFinder.PathResult forward = spf.findShortestByHops("A", "C");
assertNotNull(forward);
assertEquals(2, forward.getHopCount());
// Reverse direction should fail (no path C -> A in directed graph)
ShortestPathFinder.PathResult reverse = spf.findShortestByHops("C", "A");
assertNull(reverse);
}
// ── Larger graph: diamond/grid ──────────────────────────────────
@Test
public void byWeight_diamondGraph_findsOptimalPath() {
// B
// / \
// 1 5
// / \
// A D
// \ /
// 2 1
// \ /
// C
Graph<String, edge> g = new UndirectedSparseGraph<>();
addWeightedEdge(g, "A", "B", 1f);
addWeightedEdge(g, "A", "C", 2f);
addWeightedEdge(g, "B", "D", 5f);
addWeightedEdge(g, "C", "D", 1f);
ShortestPathFinder spf = new ShortestPathFinder(g);
ShortestPathFinder.PathResult result = spf.findShortestByWeight("A", "D");
assertNotNull(result);
// A->C->D = 3.0 is cheaper than A->B->D = 6.0
assertEquals(3.0, result.getTotalWeight(), 1e-9);
assertEquals(Arrays.asList("A", "C", "D"), result.getVertices());
}
}