{"record":{"id":"2013e933524e3d8b","repo":"TheAlgorithms/C-Sharp","slug":"number-of-colors-must-be-positive","errorCode":null,"errorMessage":"Number of colors must be positive.","messagePattern":"Number of colors must be positive\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Algorithms/Problems/GraphColoring/GraphColoringSolver.cs","lineNumber":83,"sourceCode":"    /// </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\n        if (!ColorVertex(adjacencyMatrix, colors, 0, numColors))\n        {\n            throw new ArgumentException(\n                $\"Graph cannot be colored with {numColors} color(s). \" +\n                $\"A larger number of colors may be required.\");","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Problems/GraphColoring/GraphColoringSolver.cs#L65-L101","documentation":"This ArgumentException is the numColors parameter check at the top of GraphColoringSolver.ColorGraph (GraphColoringSolver.cs:83), after the null-matrix and square-matrix checks. Graph coloring needs at least one color available; a numColors of zero or negative would make backtracking search impossible for any non-empty graph. It fires whenever ColorGraph is called with numColors <= 0.","triggerScenarios":"Calling ColorGraph(matrix, 0) or ColorGraph(matrix, -1); passing an uninitialized/failed computation result as the color count.","commonSituations":"Computing k from user configuration where 0 means 'auto' but is passed through unchanged; integer parse of an empty field yielding 0; sign errors in derived limits.","solutions":["Pass at least 1 as numColors.","Clamp/validate: if (k < 1) k = 1; or reject earlier with a clearer message.","Resolve the config/default handling that produced a non-positive k."],"exampleFix":"// before\nvar k = config.MaxColors; // may be 0\nvar colors = solver.ColorGraph(matrix, k);\n// after\nvar k = Math.Max(1, config.MaxColors);\nvar colors = solver.ColorGraph(matrix, k);","handlingStrategy":"validation","validationCode":"if (numColors < 1) throw new ArgumentException(\"numColors must be >= 1.\", nameof(numColors));","typeGuard":null,"tryCatchPattern":"try { colors = solver.ColorGraph(matrix, k); }\ncatch (ArgumentException ex) { Console.Error.WriteLine(ex.Message); }","preventionTips":["Clamp color counts with Math.Max(1, k)","Treat 0 as 'auto' in config and resolve before calling","Validate config-derived integers at load time"],"tags":["argument-out-of-range","graph-coloring","validation"],"backgroundTag":"argument-out-of-range","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"}