Skip to content

Commit 793b07a

Browse files
williamfisetclaude
andauthored
Remove BellmanFordAdjacencyMatrix, migrate tests to BellmanFordEdgeList (#1294)
The adjacency matrix variant is redundant with BellmanFordEdgeList and BellmanFordAdjacencyList. Tests that used it as an oracle now use BellmanFordEdgeList instead. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8de04c1 commit 793b07a

5 files changed

Lines changed: 40 additions & 175 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ $ java -cp classes com.williamfiset.algorithms.search.BinarySearch
204204
- [Articulation points/cut vertices (adjacency list)](src/main/java/com/williamfiset/algorithms/graphtheory/ArticulationPointsAdjacencyList.java) **- O(V+E)**
205205
- [Bellman-Ford (edge list, negative cycles, fast & optimized)](src/main/java/com/williamfiset/algorithms/graphtheory/BellmanFordEdgeList.java) **- O(VE)**
206206
- [:movie_camera:](https://www.youtube.com/watch?v=lyw4FaxrwHg) [Bellman-Ford (adjacency list, negative cycles)](src/main/java/com/williamfiset/algorithms/graphtheory/BellmanFordAdjacencyList.java) **- O(VE)**
207-
- [Bellman-Ford (adjacency matrix, negative cycles)](src/main/java/com/williamfiset/algorithms/graphtheory/BellmanFordAdjacencyMatrix.java) **- O(V<sup>3</sup>)**
208207
- [:movie_camera:](https://www.youtube.com/watch?v=oDqjPvD54Ss) [Breadth first search (adjacency list)](src/main/java/com/williamfiset/algorithms/graphtheory/BreadthFirstSearchAdjacencyListIterative.java) **- O(V+E)**
209208
- [Breadth first search (adjacency list, fast queue)](src/main/java/com/williamfiset/algorithms/graphtheory/BreadthFirstSearchAdjacencyListIterativeFastQueue.java) **- O(V+E)**
210209
- [Bridges/cut edges (adjacency list)](src/main/java/com/williamfiset/algorithms/graphtheory/BridgesAdjacencyList.java) **- O(V+E)**

src/main/java/com/williamfiset/algorithms/graphtheory/BUILD

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,6 @@ java_binary(
4141
runtime_deps = [":graphtheory"],
4242
)
4343

44-
# bazel run //src/main/java/com/williamfiset/algorithms/graphtheory:BellmanFordAdjacencyMatrix
45-
java_binary(
46-
name = "BellmanFordAdjacencyMatrix",
47-
main_class = "com.williamfiset.algorithms.graphtheory.BellmanFordAdjacencyMatrix",
48-
runtime_deps = [":graphtheory"],
49-
)
50-
5144
# bazel run //src/main/java/com/williamfiset/algorithms/graphtheory:BellmanFordEdgeList
5245
java_binary(
5346
name = "BellmanFordEdgeList",

src/main/java/com/williamfiset/algorithms/graphtheory/BellmanFordAdjacencyMatrix.java

Lines changed: 0 additions & 157 deletions
This file was deleted.

src/test/java/com/williamfiset/algorithms/graphtheory/BreadthFirstSearchAdjacencyListIterativeTest.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import java.util.List;
1414
import org.junit.jupiter.api.*;
1515

16+
import com.williamfiset.algorithms.graphtheory.BellmanFordEdgeList;
17+
1618
public class BreadthFirstSearchAdjacencyListIterativeTest {
1719

1820
BreadthFirstSearchAdjacencyListIterative solver;
@@ -82,11 +84,23 @@ public void testShortestPathAgainstBellmanFord() {
8284
int s = (int) (random() * n);
8385
int e = (int) (random() * n);
8486
solver = new BreadthFirstSearchAdjacencyListIterative(graph);
85-
BellmanFordAdjacencyMatrix bfSolver = new BellmanFordAdjacencyMatrix(s, graph2);
87+
88+
// Convert adjacency matrix to edge list for BellmanFord
89+
List<BellmanFordEdgeList.Edge> edgeList = new ArrayList<>();
90+
for (int u = 0; u < n; u++) {
91+
for (int v = 0; v < n; v++) {
92+
if (u != v && graph2[u][v] != Double.POSITIVE_INFINITY) {
93+
edgeList.add(new BellmanFordEdgeList.Edge(u, v, graph2[u][v]));
94+
}
95+
}
96+
}
97+
BellmanFordEdgeList.Edge[] edges = edgeList.toArray(new BellmanFordEdgeList.Edge[0]);
98+
double[] dist = BellmanFordEdgeList.bellmanFord(edges, n, s);
8699

87100
List<Integer> p1 = solver.reconstructPath(s, e);
88-
List<Integer> p2 = bfSolver.reconstructShortestPath(e);
89-
assertThat(p1.size()).isEqualTo(p2.size());
101+
// All edges have weight 1, so BF distance equals number of edges = path size - 1
102+
int expectedPathSize = (dist[e] == Double.POSITIVE_INFINITY) ? 0 : (int) dist[e] + 1;
103+
assertThat(p1.size()).isEqualTo(expectedPathSize);
90104
}
91105
}
92106

src/test/java/com/williamfiset/algorithms/graphtheory/FloydWarshallSolverTest.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static com.google.common.truth.Truth.assertThat;
44

5+
import com.williamfiset.algorithms.graphtheory.BellmanFordEdgeList.Edge;
56
import java.util.*;
67
import org.junit.jupiter.api.*;
78

@@ -51,6 +52,20 @@ private static double[][] createMatrix(int n) {
5152
return m;
5253
}
5354

55+
/** Converts an adjacency matrix to an array of BellmanFordEdgeList edges. */
56+
private static Edge[] matrixToEdges(double[][] matrix) {
57+
int n = matrix.length;
58+
List<Edge> edges = new ArrayList<>();
59+
for (int i = 0; i < n; i++) {
60+
for (int j = 0; j < n; j++) {
61+
if (i != j && matrix[i][j] != Double.POSITIVE_INFINITY) {
62+
edges.add(new Edge(i, j, matrix[i][j]));
63+
}
64+
}
65+
}
66+
return edges.toArray(new Edge[0]);
67+
}
68+
5469
private static void addRandomEdges(double[][] matrix, int count, boolean allowNegativeEdges) {
5570
int n = matrix.length;
5671
while (count-- > 0) {
@@ -125,7 +140,7 @@ public void testApspAgainstBellmanFord_nonNegativeEdgeWeights() {
125140
double[][] fw = new FloydWarshallSolver(m).getApspMatrix();
126141

127142
for (int s = 0; s < n; s++) {
128-
double[] bf = new BellmanFordAdjacencyMatrix(s, m).getShortestPaths();
143+
double[] bf = BellmanFordEdgeList.bellmanFord(matrixToEdges(m), n, s);
129144
assertThat(bf).isEqualTo(fw[s]);
130145
}
131146
}
@@ -144,7 +159,7 @@ public void testApspAgainstBellmanFord_withNegativeEdgeWeights() {
144159
double[][] fw = new FloydWarshallSolver(m).getApspMatrix();
145160

146161
for (int s = 0; s < n; s++) {
147-
double[] bf = new BellmanFordAdjacencyMatrix(s, m).getShortestPaths();
162+
double[] bf = BellmanFordEdgeList.bellmanFord(matrixToEdges(m), n, s);
148163
assertThat(bf).isEqualTo(fw[s]);
149164
}
150165
}
@@ -165,15 +180,16 @@ public void testPathReconstructionBellmanFord_nonNegativeEdgeWeights() {
165180
FloydWarshallSolver fwSolver = new FloydWarshallSolver(m);
166181
fwSolver.solve();
167182

183+
Edge[] edges = matrixToEdges(m);
168184
for (int s = 0; s < n; s++) {
169-
BellmanFordAdjacencyMatrix bfSolver = new BellmanFordAdjacencyMatrix(s, m);
185+
double[] bf = BellmanFordEdgeList.bellmanFord(edges, n, s);
170186
for (int e = 0; e < n; e++) {
171187

172-
// Make sure that if 'fwp' returns null that 'bfp' also returns null or
173-
// that if 'fwp' is not null that 'bfp' is also not null.
188+
// FW returns null path when a negative cycle exists on the shortest path.
189+
// BF marks such nodes with -Infinity. Verify they agree.
174190
List<Integer> fwp = fwSolver.reconstructShortestPath(s, e);
175-
List<Integer> bfp = bfSolver.reconstructShortestPath(e);
176-
if ((fwp == null) ^ (bfp == null)) {
191+
boolean bfNegCycle = (bf[e] == Double.NEGATIVE_INFINITY);
192+
if ((fwp == null) != bfNegCycle) {
177193
org.junit.Assert.fail("Mismatch.");
178194
}
179195
}

0 commit comments

Comments
 (0)