{"record":{"id":"b1ca58f7fc0785bd","repo":"TheAlgorithms/Java","slug":"capacity-matrix-must-be-square","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/Dinic.java","lineNumber":44,"sourceCode":"\n    /**\n     * Computes the maximum flow from source to sink using Dinic's algorithm.\n     *\n     * @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);","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/Dinic.java#L26-L62","documentation":"Thrown by Dinic.maxFlow when any row capacity[i] is null or has length != n (n = capacity.length). The algorithm assumes a square n x n matrix; a ragged matrix breaks symmetry assumptions and indexing. Message: 'Capacity matrix must be square'.","triggerScenarios":"A row of different length; a null row in the matrix; building the matrix row-by-row where one row was sized incorrectly.","commonSituations":"Reading a jagged CSV/array; off-by-one in row allocation; mixing matrix sources of different dimensions.","solutions":["Allocate capacity as new int[n][n] so all rows are uniformly sized.","Validate every row length equals capacity.length before calling.","Reject ragged input at the parse/load boundary."],"exampleFix":"// before\nint[][] cap = { {0,1}, {2} }; // ragged\n\n// after\nint n = 2;\nint[][] cap = new int[n][n];\ncap[0][1] = 1; cap[1][0] = 2;","handlingStrategy":"validation","validationCode":"int n = capacity.length;\nfor (int i = 0; i < n; i++) {\n    if (capacity[i] == null || capacity[i].length != n) {\n        throw new IllegalArgumentException(\"non-square row \" + i);\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Allocate capacity as new int[n][n] so rows are uniform.","Reject ragged input at parse time.","Add a matrix-shape assertion in tests."],"tags":["input-validation","matrix","graph","square-matrix"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}