{"record":{"id":"3e34b965b9c27106","repo":"TheAlgorithms/Java","slug":"capacities-must-be-non-negative-3e34b9","errorCode":null,"errorMessage":"Capacities must be non-negative","messagePattern":"Capacities must be non-negative","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/graph/EdmondsKarp.java","lineNumber":47,"sourceCode":"     * @param source the source vertex index\n     * @param sink the sink vertex index\n     * @return the value of the maximum flow between {@code source} and {@code sink}\n     * @throws IllegalArgumentException if the matrix is {@code null}, not square, contains negative\n     *         capacities, or if {@code source} / {@code sink} indices are 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\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;","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/EdmondsKarp.java#L29-L65","documentation":"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.","triggerScenarios":"Passing a matrix that contains any negative cell, including a sentinel value like -1 used to denote 'no edge'.","commonSituations":"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.","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."],"exampleFix":"// before\ncap[u][v] = (edge == null) ? -1 : edge.capacity;\n\n// after\ncap[u][v] = (edge == null) ? 0 : edge.capacity;","handlingStrategy":"validation","validationCode":"for (int i = 0; i < capacity.length; i++)\n    for (int j = 0; j < capacity[i].length; j++)\n        if (capacity[i][j] < 0) throw new IllegalArgumentException(\"Negative capacity at [\" + i + \"][\" + j + \"]\");","typeGuard":"boolean allNonNegative(int[][] cap) {\n    for (int[] row : cap) for (int v : row) if (v < 0) return false;\n    return true;\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["graph-algorithm","argument-validation","value-constraint","max-flow"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}