{"record":{"id":"775e8022e7e66ec8","repo":"TheAlgorithms/Java","slug":"capacity-matrix-must-be-square-775e80","errorCode":null,"errorMessage":"Capacity matrix must be square","messagePattern":"Capacity matrix must be square","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/graph/GomoryHuTree.java","lineNumber":61,"sourceCode":"\n            if (t != 0 && res.reachable[parent[t]]) {\n                parent[s] = parent[t];\n                parent[t] = s;\n                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","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/GomoryHuTree.java#L43-L79","documentation":"GomoryHuTree.validateCapacityMatrix throws this IllegalArgumentException when any row is null or its length differs from the matrix dimension n. The validator enforces a square n×n matrix because internal max-flow calls index both dimensions symmetrically.","triggerScenarios":"Passing a ragged array, a rectangular (non-square) matrix, or a matrix containing a null row.","commonSituations":"Allocating rows individually with wrong length. Mixing an adjacency matrix built for n vertices with rows sized for m edges. Null row after partial fill.","solutions":["Allocate with new int[n][n] and fill only [u][v] entries.","Validate every row length equals n before calling.","Replace any null row with new int[n]."],"exampleFix":"// before\nint[][] cap = new int[n][]; // rows uninitialized\n\n// after\nint[][] cap = new int[n][n];","handlingStrategy":"validation","validationCode":"for (int[] row : cap) {\n    if (row == null || row.length != cap.length) {\n        throw new IllegalArgumentException(\"Non-square capacity matrix\");\n    }\n}","typeGuard":"boolean isSquare(int[][] cap) {\n    if (cap == null) return false;\n    for (int[] row : cap) if (row == null || row.length != cap.length) return false;\n    return true;\n}","tryCatchPattern":null,"preventionTips":["Allocate new int[n][n] directly.","Fill null rows with new int[n] before calling.","Share a single matrix-validation utility across graph algorithms."],"tags":["graph-algorithm","argument-validation","matrix-shape","gomory-hu-tree"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}