TheAlgorithms/C-Sharp · error
Adjacency matrix must be square.
Error message
Adjacency matrix must be square.
What it means
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.
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.
Example fix
// before var matrix = new bool[vertices, edges]; // wrong var colors = solver.ColorGraph(matrix, 3); // after var matrix = new bool[vertices, vertices]; var colors = solver.ColorGraph(matrix, 3);
Defensive patterns
Strategy: validation
Validate before calling
if (matrix is null || matrix.GetLength(0) != matrix.GetLength(1)) throw new ArgumentException("Adjacency matrix must be square."); Type guard
static bool IsSquare(bool[,] m) => m.GetLength(0) == m.GetLength(1);
Try / catch
try { colors = solver.ColorGraph(matrix, k); }
catch (ArgumentException ex) { Console.Error.WriteLine(ex.Message); } Prevention
- Build matrices as [v, v] always
- Validate CSV imports have equal rows/columns
- Assert squareness in graph-construction tests
When it happens
Trigger: Passing a rectangular matrix such as new bool[3, 4], or a matrix built with mismatched vertex/edge counts.
Common situations: Loading a matrix from CSV where rows have unequal column counts; building the matrix with (vertices, edges) dimensions by mistake; padding errors after deserialization.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Number of samples and labels must match.
- Feature count mismatch.
- The value for some n_i is smaller than or equal to 1.
- The GCD of n_ = and n_ = equals and thus these values…
- Coins array must contain coin 1
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/cd83518009d3884e.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Problems/GraphColoring/GraphColoringSolver.cs:78
/// </para>
/// <para>
/// <b>Example:</b> For a triangle graph (3 vertices, all connected), at least 3 colors
/// are required. Calling this method with <c>numColors = 2</c> will throw an exception,
/// while <c>numColors = 3</c> will return a valid coloring such as <c>[0, 1, 2]</c>.
/// </para>
/// </remarks>
public int[] ColorGraph(bool[,] adjacencyMatrix, int numColors)
{
if (adjacencyMatrix is null)
{
throw new ArgumentNullException(nameof(adjacencyMatrix));
}
var numVertices = adjacencyMatrix.GetLength(0);
if (numVertices != adjacencyMatrix.GetLength(1))
{
throw new ArgumentException("Adjacency matrix must be square.", nameof(adjacencyMatrix));
}
if (numColors <= 0)
{
throw new ArgumentException("Number of colors must be positive.", nameof(numColors));
}
// Handle empty graph
if (numVertices == 0)
{
return Array.Empty<int>();
}
var colors = new int[numVertices];
// Initialize all vertices as uncolored (-1)
Array.Fill(colors, -1);
View on GitHub (pinned to 96e2905cab)