{"record":{"id":"ccb0d25e854eb77b","repo":"TheAlgorithms/Java","slug":"source-and-sink-must-be-valid-vertex-indices","errorCode":null,"errorMessage":"Source and sink must be valid vertex indices","messagePattern":"Source and sink must be valid vertex indices","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/graph/Dinic.java","lineNumber":53,"sourceCode":"     *     indices invalid\n     */\n    public static int maxFlow(int[][] capacity, int source, int sink) {\n        if (capacity == null || capacity.length == 0) {\n            throw new IllegalArgumentException(\"Capacity matrix must not be null or empty\");\n        }\n        final int n = capacity.length;\n        for (int i = 0; i < n; i++) {\n            if (capacity[i] == null || capacity[i].length != n) {\n                throw new IllegalArgumentException(\"Capacity matrix must be square\");\n            }\n            for (int j = 0; j < n; j++) {\n                if (capacity[i][j] < 0) {\n                    throw new IllegalArgumentException(\"Capacities must be non-negative\");\n                }\n            }\n        }\n        if (source < 0 || sink < 0 || source >= n || sink >= n) {\n            throw new IllegalArgumentException(\"Source and sink must be valid vertex indices\");\n        }\n        if (source == sink) {\n            return 0;\n        }\n\n        // residual capacities\n        int[][] residual = new int[n][n];\n        for (int i = 0; i < n; i++) {\n            residual[i] = Arrays.copyOf(capacity[i], n);\n        }\n\n        int[] level = new int[n];\n        int flow = 0;\n        while (bfsBuildLevelGraph(residual, source, sink, level)) {\n            int[] next = new int[n]; // current-edge optimization\n            int pushed;\n            do {\n                pushed = dfsBlocking(residual, level, next, source, sink, Integer.MAX_VALUE);","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/Dinic.java#L35-L71","documentation":"Dinic.maxFlow throws this IllegalArgumentException when the source or sink vertex index falls outside the valid range [0, n) for a capacity matrix of size n×n. The check runs after the matrix is validated for squareness and non-negative capacities, so it guards only index bounds. It ensures the algorithm never indexes residual/level arrays out of range.","triggerScenarios":"Calling Dinic.maxFlow(capacity, source, sink) where source or sink is negative, or >= capacity.length. Also fires when the matrix was built for a different vertex count than the indices imply.","commonSituations":"Zero-indexed vs one-indexed confusion (passing 1-based vertex labels). Building the capacity matrix from an edge list but forgetting to include isolated vertices, making n smaller than the largest vertex id. Off-by-one when computing sink as the last node index.","solutions":["Verify source and sink are in [0, capacity.length - 1] before calling.","Ensure the capacity matrix dimension equals the total vertex count, including isolated vertices.","If your graph uses 1-based labels externally, convert to 0-based indices before calling.","Check that source != sink (though that returns 0, not an error) and that both are legitimate vertices."],"exampleFix":"// before\nint flow = Dinic.maxFlow(cap, startNode, endNode); // startNode/endNode are 1-based\n\n// after\nint flow = Dinic.maxFlow(cap, startNode - 1, endNode - 1); // convert to 0-based","handlingStrategy":"validation","validationCode":"if (source < 0 || sink < 0 || source >= capacity.length || sink >= capacity.length) {\n    throw new IllegalArgumentException(\"Invalid source/sink for matrix of size \" + capacity.length);\n}","typeGuard":"boolean validIndices(int[][] cap, int s, int t) {\n    return cap != null && s >= 0 && t >= 0 && s < cap.length && t < cap.length;\n}","tryCatchPattern":null,"preventionTips":["Normalize all external vertex ids to 0-based before calling graph algorithms.","Keep a single source of truth for vertex count and derive matrix dimension from it.","Add a unit test that asserts index bounds before invoking maxFlow."],"tags":["graph-algorithm","argument-validation","index-bounds","max-flow"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}