{"record":{"id":"de77d93192700f55","repo":"TheAlgorithms/Java","slug":"capacity-matrix-must-be-square-de77d9","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/EdmondsKarp.java","lineNumber":43,"sourceCode":"    /**\n     * Computes the maximum flow from {@code source} to {@code sink} in the provided capacity matrix.\n     *\n     * @param capacity the capacity matrix representing the directed graph; must be square and non-null\n     * @param source the source vertex index\n     * @param sink the sink vertex index\n     * @return the value of the maximum flow between {@code source} and {@code sink}\n     * @throws IllegalArgumentException if the matrix is {@code null}, not square, contains negative\n     *         capacities, or if {@code source} / {@code sink} indices are 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\n        final int n = capacity.length;\n        for (int row = 0; row < n; row++) {\n            if (capacity[row] == null || capacity[row].length != n) {\n                throw new IllegalArgumentException(\"Capacity matrix must be square\");\n            }\n            for (int col = 0; col < n; col++) {\n                if (capacity[row][col] < 0) {\n                    throw new IllegalArgumentException(\"Capacities must be non-negative\");\n                }\n            }\n        }\n\n        if (source < 0 || source >= n || sink < 0 || 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        final int[][] residual = new int[n][n];\n        for (int i = 0; i < n; i++) {\n            residual[i] = Arrays.copyOf(capacity[i], n);","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/EdmondsKarp.java#L25-L61","documentation":"EdmondsKarp.maxFlow throws this IllegalArgumentException when any row of the capacity matrix is null or has a length different from the matrix dimension n. The algorithm requires a square n×n matrix to map every (source, sink) pair to a capacity entry.","triggerScenarios":"Passing a ragged 2D array (rows of differing lengths), a matrix with a null row, or a rectangular non-square matrix (e.g. n×m where m != n).","commonSituations":"Building the matrix from an edge list using new int[numEdges][] instead of new int[n][n]. Accidentally transposing dimensions. A row left null after partial initialization.","solutions":["Allocate the matrix as new int[n][n] where n is the vertex count.","If constructing from edges, first size n, then fill cap[u][v] for each edge.","Verify all rows are non-null and length n before calling."],"exampleFix":"// before\nint[][] cap = new int[numEdges][]; // wrong: ragged\nfor (Edge e : edges) cap[e.u] = new int[n];\n\n// after\nint[][] cap = new int[n][n];\nfor (Edge e : edges) cap[e.u][e.v] = e.weight;","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 matrices as new int[n][n], never new int[n][].","Build from an edge list after sizing n, not vice versa.","Add a shape-validation helper used by all graph algorithm entry points."],"tags":["graph-algorithm","argument-validation","matrix-shape","max-flow"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}