{"record":{"id":"83b04f8f73c3fa09","repo":"TheAlgorithms/Java","slug":"cost-matrix-must-be-rectangular-with-equal-row-len","errorCode":null,"errorMessage":"Cost matrix must be rectangular with equal row lengths","messagePattern":"Cost matrix must be rectangular with equal row lengths","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/graph/HungarianAlgorithm.java","lineNumber":141,"sourceCode":"            if (j >= 0 && j < cols) {\n                assignment[i] = j;\n                total += cost[i][j];\n            }\n        }\n        return new Result(assignment, total);\n    }\n\n    private static void validate(int[][] cost) {\n        if (cost == null || cost.length == 0) {\n            throw new IllegalArgumentException(\"Cost matrix must not be null or empty\");\n        }\n        int c = cost[0].length;\n        if (c == 0) {\n            throw new IllegalArgumentException(\"Cost matrix must have at least 1 column\");\n        }\n        for (int i = 0; i < cost.length; i++) {\n            if (cost[i] == null || cost[i].length != c) {\n                throw new IllegalArgumentException(\"Cost matrix must be rectangular with equal row lengths\");\n            }\n            for (int j = 0; j < c; j++) {\n                if (cost[i][j] < 0) {\n                    throw new IllegalArgumentException(\"Costs must be non-negative\");\n                }\n            }\n        }\n    }\n}\n","sourceCodeStart":123,"sourceCodeEnd":151,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/graph/HungarianAlgorithm.java#L123-L151","documentation":"HungarianAlgorithm.validate throws this IllegalArgumentException when any row is null or its length differs from the first row's length c. Unlike the flow algorithms, the Hungarian algorithm accepts rectangular (non-square) matrices but requires consistent column counts across all rows.","triggerScenarios":"Passing a ragged array where rows have differing lengths, or a matrix with a null row.","commonSituations":"Appending tasks per worker without aligning column counts. A null row from incomplete initialization. Mixing data sources with different task counts.","solutions":["Pad all rows to the same column count (max across workers) before calling.","Ensure every row is non-null and of equal length.","Validate row lengths against the first row during construction."],"exampleFix":"// before\nint[][] cost = { {1,2}, {3} }; // ragged\n\n// after\nint[][] cost = { {1,2}, {3,0} }; // pad missing with large/zero cost","handlingStrategy":"validation","validationCode":"int cols = cost[0].length;\nfor (int[] row : cost) {\n    if (row == null || row.length != cols) {\n        throw new IllegalArgumentException(\"Ragged cost matrix\");\n    }\n}","typeGuard":"boolean isRectangular(int[][] cost) {\n    if (cost == null || cost.length == 0) return false;\n    int c = cost[0].length;\n    for (int[] row : cost) if (row == null || row.length != c) return false;\n    return true;\n}","tryCatchPattern":null,"preventionTips":["Pad all rows to the same column count.","Validate row lengths against the first row during construction.","Use a builder that enforces consistent dimensions."],"tags":["graph-algorithm","argument-validation","matrix-shape","assignment-problem"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}