{"record":{"id":"fb7412ccf087d70e","repo":"TheAlgorithms/Java","slug":"source-and-sink-must-be-valid-vertex-indices-fb7412","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/EdmondsKarp.java","lineNumber":53,"sourceCode":"    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\n        final int n = capacity.length;\n        for (int row = 0; row < n; row++) {\n            if (capacity[row] == null || capacity[row].length != n) {\n                throw new IllegalArgumentException(\"Capacity matrix must be square\");\n            }\n            for (int col = 0; col < n; col++) {\n                if (capacity[row][col] < 0) {\n                    throw new IllegalArgumentException(\"Capacities must be non-negative\");\n                }\n            }\n        }\n\n        if (source < 0 || source >= n || sink < 0 || 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        final 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        final int[] parent = new int[n];\n        int maxFlow = 0;\n\n        while (bfs(residual, source, sink, parent)) {\n            int pathFlow = Integer.MAX_VALUE;\n            for (int v = sink; v != source; v = parent[v]) {\n                int u = parent[v];\n                pathFlow = Math.min(pathFlow, residual[u][v]);","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/EdmondsKarp.java#L35-L71","documentation":"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.","triggerScenarios":"Calling maxFlow with source or sink negative, or >= capacity.length.","commonSituations":"1-based vertex labels passed directly. Vertex count mismatch between matrix dimension and external ids. Sink computed as numVertices instead of numVertices - 1.","solutions":["Clamp/verify source and sink to [0, n - 1] before calling.","Convert external 1-based labels to 0-based.","Ensure n (matrix dimension) reflects the true total vertex count."],"exampleFix":"// before\nint flow = EdmondsKarp.maxFlow(cap, 1, n); // 1-based, sink == n is out of range\n\n// after\nint flow = EdmondsKarp.maxFlow(cap, 0, n - 1);","handlingStrategy":"validation","validationCode":"int n = capacity.length;\nif (source < 0 || source >= n || sink < 0 || sink >= n) {\n    throw new IllegalArgumentException(\"source/sink out of bounds for n=\" + n);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Convert 1-based labels to 0-based before calling.","Derive sink as n - 1, not n.","Keep vertex count consistent with matrix dimension."],"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"}