TheAlgorithms/Java · error · IllegalArgumentException

Capacity matrix must be square

Error message

Capacity matrix must be square

What it means

Thrown by Dinic.maxFlow when any row capacity[i] is null or has length != n (n = capacity.length). The algorithm assumes a square n x n matrix; a ragged matrix breaks symmetry assumptions and indexing. Message: 'Capacity matrix must be square'.

Source

Thrown at src/main/java/com/thealgorithms/graph/Dinic.java:44

    /**
     * Computes the maximum flow from source to sink using Dinic's algorithm.
     *
     * @param capacity square capacity matrix (n x n); entries must be >= 0
     * @param source source vertex index in [0, n)
     * @param sink sink vertex index in [0, n)
     * @return the maximum flow value
     * @throws IllegalArgumentException if the input matrix is null/non-square/has negatives or
     *     indices 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 i = 0; i < n; i++) {
            if (capacity[i] == null || capacity[i].length != n) {
                throw new IllegalArgumentException("Capacity matrix must be square");
            }
            for (int j = 0; j < n; j++) {
                if (capacity[i][j] < 0) {
                    throw new IllegalArgumentException("Capacities must be non-negative");
                }
            }
        }
        if (source < 0 || sink < 0 || source >= n || sink >= n) {
            throw new IllegalArgumentException("Source and sink must be valid vertex indices");
        }
        if (source == sink) {
            return 0;
        }

        // residual capacities
        int[][] residual = new int[n][n];
        for (int i = 0; i < n; i++) {
            residual[i] = Arrays.copyOf(capacity[i], n);

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Allocate capacity as new int[n][n] so all rows are uniformly sized.
  2. Validate every row length equals capacity.length before calling.
  3. Reject ragged input at the parse/load boundary.

Example fix

// before
int[][] cap = { {0,1}, {2} }; // ragged

// after
int n = 2;
int[][] cap = new int[n][n];
cap[0][1] = 1; cap[1][0] = 2;
Defensive patterns

Strategy: validation

Validate before calling

int n = capacity.length;
for (int i = 0; i < n; i++) {
    if (capacity[i] == null || capacity[i].length != n) {
        throw new IllegalArgumentException("non-square row " + i);
    }
}

Prevention

When it happens

Trigger: A row of different length; a null row in the matrix; building the matrix row-by-row where one row was sized incorrectly.

Common situations: Reading a jagged CSV/array; off-by-one in row allocation; mixing matrix sources of different dimensions.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/b1ca58f7fc0785bd. Report an issue: GitHub.