{"record":{"id":"a24c873f5cc9cbe8","repo":"TheAlgorithms/Java","slug":"capacities-must-be-non-negative-a24c87","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/GomoryHuTree.java","lineNumber":65,"sourceCode":"                weight[s] = weight[t];\n                weight[t] = f;\n            }\n        }\n        return new int[][] {parent, weight};\n    }\n\n    private static void validateCapacityMatrix(int[][] cap) {\n        if (cap == null || cap.length == 0) {\n            throw new IllegalArgumentException(\"Capacity matrix must not be null or empty\");\n        }\n        final int n = cap.length;\n        for (int i = 0; i < n; i++) {\n            if (cap[i] == null || cap[i].length != n) {\n                throw new IllegalArgumentException(\"Capacity matrix must be square\");\n            }\n            for (int j = 0; j < n; j++) {\n                if (cap[i][j] < 0) {\n                    throw new IllegalArgumentException(\"Capacities must be non-negative\");\n                }\n            }\n        }\n    }\n\n    private static final class MaxFlowResult {\n        final int flow;\n        final boolean[] reachable;\n        MaxFlowResult(int flow, boolean[] reachable) {\n            this.flow = flow;\n            this.reachable = reachable;\n        }\n    }\n\n    private static MaxFlowResult edmondsKarpWithMinCut(int[][] capacity, int source, int sink) {\n        final int n = capacity.length;\n        int[][] residual = new int[n][n];\n        for (int i = 0; i < n; i++) {","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/GomoryHuTree.java#L47-L83","documentation":"GomoryHuTree.validateCapacityMatrix throws this IllegalArgumentException when any capacity cell is negative. The Gomory-Hu construction repeatedly solves max-flow sub-problems, each of which requires non-negative capacities.","triggerScenarios":"Passing a matrix with any cell < 0, including sentinels like -1 for 'no edge'.","commonSituations":"Using -1 as a no-edge sentinel. Signed weights from a file not clamped. Negative values introduced by buggy residual arithmetic before calling the validator.","solutions":["Represent 'no edge' as 0, never -1.","Clamp parsed values: cap[u][v] = Math.max(0, val).","Add a unit test that asserts no negatives after matrix construction."],"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 < cap.length; i++)\n    for (int j = 0; j < cap[i].length; j++)\n        if (cap[i][j] < 0) cap[i][j] = 0; // clamp","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":["Replace -1 sentinels with 0.","Clamp during matrix construction.","Add a non-negativity assertion in tests."],"tags":["graph-algorithm","argument-validation","value-constraint","gomory-hu-tree"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}