{"record":{"id":"53e5a756ad470a2c","repo":"TheAlgorithms/Java","slug":"capacity-matrix-must-be-square-53e5a7","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/PushRelabel.java","lineNumber":150,"sourceCode":"        int minHeight = Integer.MAX_VALUE;\n        for (int v = 0; v < n; v++) {\n            if (residual[u][v] > 0) {\n                minHeight = Math.min(minHeight, height[v]);\n            }\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":132,"sourceCodeEnd":163,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/PushRelabel.java#L132-L163","documentation":"PushRelabel.validate throws this IllegalArgumentException when any row is null or its length differs from n (the matrix dimension). The push-relabel algorithm indexes capacity[u][v] symmetrically, requiring a square n×n matrix.","triggerScenarios":"Passing a ragged array, a non-square rectangular matrix, or a matrix with a null row.","commonSituations":"Allocating new int[n][] and forgetting to initialize rows. Mixing edge-list-derived sizing with vertex-count sizing. Null row after partial fill.","solutions":["Allocate new int[n][n] and fill capacity[u][v] per edge.","Verify all rows are non-null and length n.","Replace null rows with new int[n] before calling."],"exampleFix":"// before\nint[][] cap = new int[n][]; // uninitialized rows\n\n// after\nint[][] cap = new int[n][n];","handlingStrategy":"validation","validationCode":"for (int[] row : capacity) {\n    if (row == null || row.length != capacity.length) {\n        throw new IllegalArgumentException(\"Non-square 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.","Never leave rows uninitialized.","Reuse a shared square-matrix validator."],"tags":["graph-algorithm","argument-validation","matrix-shape","max-flow","push-relabel"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}