{"record":{"id":"cd83518009d3884e","repo":"TheAlgorithms/C-Sharp","slug":"adjacency-matrix-must-be-square-graphcoloringsolver","errorCode":null,"errorMessage":"Adjacency matrix must be square.","messagePattern":"Adjacency matrix must be square\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Algorithms/Problems/GraphColoring/GraphColoringSolver.cs","lineNumber":78,"sourceCode":"    /// </para>\n    /// <para>\n    /// <b>Example:</b> For a triangle graph (3 vertices, all connected), at least 3 colors\n    /// are required. Calling this method with <c>numColors = 2</c> will throw an exception,\n    /// while <c>numColors = 3</c> will return a valid coloring such as <c>[0, 1, 2]</c>.\n    /// </para>\n    /// </remarks>\n    public int[] ColorGraph(bool[,] adjacencyMatrix, int numColors)\n    {\n        if (adjacencyMatrix is null)\n        {\n            throw new ArgumentNullException(nameof(adjacencyMatrix));\n        }\n\n        var numVertices = adjacencyMatrix.GetLength(0);\n\n        if (numVertices != adjacencyMatrix.GetLength(1))\n        {\n            throw new ArgumentException(\"Adjacency matrix must be square.\", nameof(adjacencyMatrix));\n        }\n\n        if (numColors <= 0)\n        {\n            throw new ArgumentException(\"Number of colors must be positive.\", nameof(numColors));\n        }\n\n        // Handle empty graph\n        if (numVertices == 0)\n        {\n            return Array.Empty<int>();\n        }\n\n        var colors = new int[numVertices];\n\n        // Initialize all vertices as uncolored (-1)\n        Array.Fill(colors, -1);\n","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Problems/GraphColoring/GraphColoringSolver.cs#L60-L96","documentation":"ColorGraph requires a square 2D boolean matrix (n x n) where rows and columns both index vertices. If GetLength(0) != GetLength(1), the input cannot represent an adjacency matrix, so ArgumentException is thrown with parameter name adjacencyMatrix.","triggerScenarios":"Passing a rectangular matrix such as new bool[3, 4], or a matrix built with mismatched vertex/edge counts.","commonSituations":"Loading a matrix from CSV where rows have unequal column counts; building the matrix with (vertices, edges) dimensions by mistake; padding errors after deserialization.","solutions":["Rebuild the matrix as square with dimension equal to the number of vertices.","Validate GetLength(0) == GetLength(1) at the call site before invoking.","Fix the export/import code that produced a non-square matrix."],"exampleFix":"// before\nvar matrix = new bool[vertices, edges]; // wrong\nvar colors = solver.ColorGraph(matrix, 3);\n// after\nvar matrix = new bool[vertices, vertices];\nvar colors = solver.ColorGraph(matrix, 3);","handlingStrategy":"validation","validationCode":"if (matrix is null || matrix.GetLength(0) != matrix.GetLength(1)) throw new ArgumentException(\"Adjacency matrix must be square.\");","typeGuard":"static bool IsSquare(bool[,] m) => m.GetLength(0) == m.GetLength(1);","tryCatchPattern":"try { colors = solver.ColorGraph(matrix, k); }\ncatch (ArgumentException ex) { Console.Error.WriteLine(ex.Message); }","preventionTips":["Build matrices as [v, v] always","Validate CSV imports have equal rows/columns","Assert squareness in graph-construction tests"],"tags":["invalid-argument-value","graph-coloring","shape-mismatch"],"backgroundTag":"invalid-argument-value","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}