{"record":{"id":"f5becd93569f4928","repo":"TheAlgorithms/Java","slug":"capacities-must-be-non-negative-f5becd","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/PushRelabel.java","lineNumber":154,"sourceCode":"            }\n        }\n        if (minHeight < Integer.MAX_VALUE) {\n            height[u] = minHeight + 1;\n        }\n    }\n\n    private static void validate(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        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    }\n}\n","sourceCodeStart":136,"sourceCodeEnd":163,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/PushRelabel.java#L136-L163","documentation":"PushRelabel.validate throws this IllegalArgumentException when any capacity cell is negative. Push-relabel's preflow and relabel operations assume non-negative capacities; negative values break the excess invariant and can produce incorrect flow values.","triggerScenarios":"Passing a matrix with any cell < 0, including -1 sentinels for 'no edge'.","commonSituations":"Using -1 for no-edge. Signed weights not clamped. Buggy in-place residual mutation before calling validate.","solutions":["Use 0 for no-edge representation.","Clamp parsed values with Math.max(0, val).","Unit-test the matrix for non-negativity before calling."],"exampleFix":"// before\ncap[u][v] = hasEdge ? w : -1;\n\n// after\ncap[u][v] = hasEdge ? w : 0;","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) capacity[i][j] = 0;","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.","Clamp parsed values with Math.max(0, val).","Avoid in-place residual mutation before validation."],"tags":["graph-algorithm","argument-validation","value-constraint","max-flow","push-relabel"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}