TheAlgorithms/Java · error · IllegalArgumentException

Source and sink must be valid vertex indices

Error message

Source and sink must be valid vertex indices

What it means

EdmondsKarp.maxFlow throws this IllegalArgumentException when source or sink is outside [0, n). It runs after the matrix is validated, so by this point n is trustworthy. The guard prevents array-index-out-of-bounds during BFS and parent reconstruction.

Source

Thrown at src/main/java/com/thealgorithms/graph/EdmondsKarp.java:53

    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;

        while (bfs(residual, source, sink, parent)) {
            int pathFlow = Integer.MAX_VALUE;
            for (int v = sink; v != source; v = parent[v]) {
                int u = parent[v];
                pathFlow = Math.min(pathFlow, residual[u][v]);

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Clamp/verify source and sink to [0, n - 1] before calling.
  2. Convert external 1-based labels to 0-based.
  3. Ensure n (matrix dimension) reflects the true total vertex count.

Example fix

// before
int flow = EdmondsKarp.maxFlow(cap, 1, n); // 1-based, sink == n is out of range

// after
int flow = EdmondsKarp.maxFlow(cap, 0, n - 1);
Defensive patterns

Strategy: validation

Validate before calling

int n = capacity.length;
if (source < 0 || source >= n || sink < 0 || sink >= n) {
    throw new IllegalArgumentException("source/sink out of bounds for n=" + n);
}

Prevention

When it happens

Trigger: Calling maxFlow with source or sink negative, or >= capacity.length.

Common situations: 1-based vertex labels passed directly. Vertex count mismatch between matrix dimension and external ids. Sink computed as numVertices instead of numVertices - 1.

Related errors


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