TheAlgorithms/C-Sharp · error · ArgumentException
Matrix must be symmetric!
Error message
Matrix must be symmetric!
What it means
Kruskal's MST solver only accepts an adjacency matrix representing an undirected weighted graph, so it requires adj[i,j] == adj[j,i] for every pair of nodes (within a 1e-6 tolerance). ValidateGraph, called from Solve, throws this ArgumentException as soon as any asymmetric pair of cells is found. A symmetric matrix is the matrix representation of an undirected graph; an asymmetric one silently defines different edge weights depending on direction, which would make the MST result ill-defined.
Solutions
- Fix the input so every weight is mirrored: after filling adj[i,j], also set adj[j,i] to the same value (or run a symmetrization pass adj[j,i] = adj[i,j] before calling Solve).
- If your graph is genuinely directed, this algorithm is not applicable — use a different algorithm or convert the graph to undirected by taking min/max of the two directional weights consistently.
- Check for floating-point noise: if the asymmetry is only ~1e-6 or smaller, the check already tolerates it; larger drift means the values were computed differently and should be rounded or computed symmetrically.
- Add a pre-validation pass in your own code that reports which (i,j) pair is asymmetric to pinpoint the bad entry.
Example fix
// before adj[0, 1] = 4f; // after adj[0, 1] = 4f; adj[1, 0] = 4f; // mirror every edge
Defensive patterns
Strategy: validation
Validate before calling
static bool IsSymmetric(float[,] adj)
{
for (int i = 0; i < adj.GetLength(0) - 1; i++)
for (int j = i + 1; j < adj.GetLength(1); j++)
if (Math.Abs(adj[i, j] - adj[j, i]) > 1e-6)
return false;
return true;
}
if (!IsSymmetric(adj)) throw new ArgumentException("adjacency matrix is not symmetric"); Type guard
static bool IsSquareSymmetricMatrix(float[,] adj) =>
adj != null && adj.GetLength(0) == adj.GetLength(1) && IsSymmetric(adj); Try / catch
try
{
var mst = kruskal.Solve(adj);
}
catch (ArgumentException ex) when (ex.Message == "Matrix must be symmetric!")
{
// symmetrize or report the asymmetric input to the caller
} Prevention
- Always mirror an edge in both cells when building the matrix
- Add a symmetrization pass as the last build step before calling Solve
- Assert symmetry in unit tests for every fixture matrix
- Keep one source of truth per weight and assign both cells from it
When it happens
Trigger: Calling Kruskal.Solve with a float[,] adjacency matrix where adj[i,j] differs from adj[j,i] by more than 1e-6 for at least one pair i<j — e.g. setting only the upper (or lower) triangle of the matrix, or assigning direction-dependent weights.
Common situations: Building the matrix from a directed edge list and forgetting to mirror each edge; filling only half the matrix as an optimization without expanding it; a data pipeline that merges directed and undirected edges; rounding/typo when hand-authoring small test matrices.
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
- Adjacency matrix must be square!
- Adjacency matrix must be symmetric!
- Graph must be undirected!
- Graph must be connected!
- The length of key should be divisible by 16
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/fc9a70838fca911d.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Graph/MinimumSpanningTree/Kruskal.cs:140
/// <summary>
/// Ensure that the given graph is undirected.
/// </summary>
/// <param name="adj">Adjacency matrix of graph to check.</param>
private static void ValidateGraph(float[,] adj)
{
if (adj.GetLength(0) != adj.GetLength(1))
{
throw new ArgumentException("Matrix must be square!");
}
for (var i = 0; i < adj.GetLength(0) - 1; i++)
{
for (var j = i + 1; j < adj.GetLength(1); j++)
{
if (Math.Abs(adj[i, j] - adj[j, i]) > 1e-6)
{
throw new ArgumentException("Matrix must be symmetric!");
}
}
}
}
/// <summary>
/// Ensure that the given graph is undirected.
/// </summary>
/// <param name="adj">Adjacency list of graph to check.</param>
private static void ValidateGraph(Dictionary<int, float>[] adj)
{
for (var i = 0; i < adj.Length; i++)
{
foreach (var edge in adj[i])
{
if (!adj[edge.Key].ContainsKey(i) || Math.Abs(edge.Value - adj[edge.Key][i]) > 1e-6)
{
throw new ArgumentException("Graph must be undirected!");View on GitHub (pinned to 96e2905cab)