forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZeroOneBfsTest.java
More file actions
68 lines (58 loc) · 2.14 KB
/
ZeroOneBfsTest.java
File metadata and controls
68 lines (58 loc) · 2.14 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
package com.thealgorithms.graph;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
class ZeroOneBfsTest {
// Helper to build adjacency list with capacity n
private static List<List<int[]>> makeAdj(int n) {
List<List<int[]>> adj = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
adj.add(new ArrayList<>());
}
return adj;
}
@Test
void simpleLineGraph() {
int n = 4;
List<List<int[]>> adj = makeAdj(n);
// 0 --0--> 1 --1--> 2 --0--> 3
adj.get(0).add(new int[] {1, 0});
adj.get(1).add(new int[] {2, 1});
adj.get(2).add(new int[] {3, 0});
int[] dist = ZeroOneBfs.shortestPaths(n, adj, 0);
assertArrayEquals(new int[] {0, 0, 1, 1}, dist);
}
@Test
void parallelEdgesPreferZero() {
int n = 3;
List<List<int[]>> adj = makeAdj(n);
// Two edges 0->1: weight 1 and weight 0. Algorithm should choose 0.
adj.get(0).add(new int[] {1, 1});
adj.get(0).add(new int[] {1, 0});
adj.get(1).add(new int[] {2, 1});
int[] dist = ZeroOneBfs.shortestPaths(n, adj, 0);
assertArrayEquals(new int[] {0, 0, 1}, dist);
}
@Test
void unreachableNodes() {
int n = 3;
List<List<int[]>> adj = makeAdj(n);
adj.get(0).add(new int[] {1, 0});
int[] dist = ZeroOneBfs.shortestPaths(n, adj, 0);
// node 2 unreachable -> Integer.MAX_VALUE
assertArrayEquals(new int[] {0, 0, Integer.MAX_VALUE}, dist);
}
@Test
void invalidArgs() {
int n = 2;
List<List<int[]>> adj = makeAdj(n);
// invalid weight
adj.get(0).add(new int[] {1, 2});
assertThrows(IllegalArgumentException.class, () -> ZeroOneBfs.shortestPaths(n, adj, 0));
// invalid src
assertThrows(IllegalArgumentException.class, () -> ZeroOneBfs.shortestPaths(n, adj, -1));
assertThrows(IllegalArgumentException.class, () -> ZeroOneBfs.shortestPaths(n, adj, 2));
}
}