{"record":{"id":"3bd3e0aa7dba60aa","repo":"TheAlgorithms/Java","slug":"capacities-must-be-non-negative","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/Dinic.java","lineNumber":48,"sourceCode":"     * @param capacity square capacity matrix (n x n); entries must be >= 0\n     * @param source source vertex index in [0, n)\n     * @param sink sink vertex index in [0, n)\n     * @return the maximum flow value\n     * @throws IllegalArgumentException if the input matrix is null/non-square/has negatives or\n     *     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;","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/Dinic.java#L30-L66","documentation":"Thrown by Dinic.maxFlow when any capacity[i][j] < 0. Flow capacities are non-negative by definition; negative values break the residual-graph invariants. Message: 'Capacities must be non-negative'.","triggerScenarios":"A negative entry in the capacity matrix; using -1 as a 'no edge' sentinel instead of 0; signed parsing of an unsigned source.","commonSituations":"Sentinel values for 'no edge'; a subtraction that underflowed into a negative capacity; typo'd config with a minus sign.","solutions":["Use 0 to represent 'no edge' between two vertices, never a negative sentinel.","Validate every entry >= 0 at the boundary.","Sanitize parsed numeric input to reject negatives."],"exampleFix":"// before\ncap[0][1] = -1; // intended 'no edge'\n\n// after\ncap[0][1] = 0; // no edge","handlingStrategy":"validation","validationCode":"for (int[] row : capacity) {\n    for (int c : row) {\n        if (c < 0) throw new IllegalArgumentException(\"negative capacity: \" + c);\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use 0 (not -1) to mean 'no edge'.","Validate all entries >= 0 at the build boundary.","Sanitize parsed numeric input for sign."],"tags":["input-validation","negative-values","graph","matrix"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}