TheAlgorithms/Java · error · IllegalArgumentException
Capacities must be non-negative
Error message
Capacities must be non-negative
What it means
EdmondsKarp.maxFlow throws this IllegalArgumentException when any entry capacity[row][col] is negative. Flow capacities are physically meaningless below zero, and negative values would break the BFS augmentation invariant.
Source
Thrown at src/main/java/com/thealgorithms/graph/EdmondsKarp.java:47
* @param source the source vertex index
* @param sink the sink vertex index
* @return the value of the maximum flow between {@code source} and {@code sink}
* @throws IllegalArgumentException if the matrix is {@code null}, not square, contains negative
* capacities, or if {@code source} / {@code sink} indices are invalid
*/
public static int maxFlow(int[][] capacity, int source, int sink) {
if (capacity == null || capacity.length == 0) {
throw new IllegalArgumentException("Capacity matrix must not be null or empty");
}
final int n = capacity.length;
for (int row = 0; row < n; row++) {
if (capacity[row] == null || capacity[row].length != n) {
throw new IllegalArgumentException("Capacity matrix must be square");
}
for (int col = 0; col < n; col++) {
if (capacity[row][col] < 0) {
throw new IllegalArgumentException("Capacities must be non-negative");
}
}
}
if (source < 0 || source >= n || sink < 0 || sink >= n) {
throw new IllegalArgumentException("Source and sink must be valid vertex indices");
}
if (source == sink) {
return 0;
}
final int[][] residual = new int[n][n];
for (int i = 0; i < n; i++) {
residual[i] = Arrays.copyOf(capacity[i], n);
}
final int[] parent = new int[n];
int maxFlow = 0;View on GitHub (pinned to fdfb9a395b)
Solutions
- Use 0 (not -1) to represent absence of an edge or zero capacity.
- Sanitize input: cap[u][v] = Math.max(0, parsedValue) before calling.
- Audit any in-place mutation of the capacity matrix prior to the call.
Example fix
// before cap[u][v] = (edge == null) ? -1 : edge.capacity; // after cap[u][v] = (edge == null) ? 0 : edge.capacity;
Defensive patterns
Strategy: validation
Validate before calling
for (int i = 0; i < capacity.length; i++)
for (int j = 0; j < capacity[i].length; j++)
if (capacity[i][j] < 0) throw new IllegalArgumentException("Negative capacity at [" + i + "][" + j + "]"); Type guard
boolean allNonNegative(int[][] cap) {
for (int[] row : cap) for (int v : row) if (v < 0) return false;
return true;
} Prevention
- Use 0 for no-edge, never -1.
- Clamp parsed values with Math.max(0, val).
- Audit any code that mutates the matrix before the call.
When it happens
Trigger: Passing a matrix that contains any negative cell, including a sentinel value like -1 used to denote 'no edge'.
Common situations: Using -1 as a 'no capacity' marker instead of 0. Reading signed weights from input without clamping. Subtracting during residual updates before copying to a fresh matrix.
Related errors
- Capacities must be non-negative
- Source and sink must be valid vertex indices
- Capacity matrix must not be null or empty
- Capacity matrix must be square
- Source and sink must be valid vertex indices
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/3e34b965b9c27106.
Report an issue: GitHub.