TheAlgorithms/C-Sharp · error · ArgumentNullException
ArgumentNullException: vertices
Error message
ArgumentNullException: vertices
What it means
Bridges.Find computes all bridges in an undirected graph from a vertex collection and a neighbor function. The library throws ArgumentNullException for 'vertices' when the caller passes a null vertex enumerable, because the algorithm cannot enumerate a null sequence. It is an eager argument guard at the start of the public Find method (Algorithms/Graph/Bridges.cs:26).
Solutions
- Pass a non-empty or empty collection instead of null (e.g. ?? Enumerable.Empty<T>()).
- Fix the upstream source that produced a null vertex set (check TryGetValue / failed lookups).
- Wrap the call in a null check or throw a more descriptive exception in your own code before calling Find.
Example fix
// before var bridges = Bridges.Find(adjacencyMap?[nodeKey], n => adjacencyMap[n]); // after var vertices = adjacencyMap?[nodeKey] ?? Enumerable.Empty<string>(); var bridges = Bridges.Find(vertices, n => adjacencyMap[n]);
Defensive patterns
Strategy: validation
Validate before calling
if (vertices is null)
throw new ArgumentException("Vertex collection must not be null", nameof(vertices));
var bridges = Bridges.Find(vertices, getNeighbors); Type guard
static bool HasVertices<T>(IEnumerable<T>? source) => source is not null;
Try / catch
try
{
var bridges = Bridges.Find(vertices, getNeighbors);
}
catch (ArgumentNullException ex)
{
// ex.ParamName == "vertices": fix the null vertex source before retrying
logger.LogError(ex, "Vertex collection was null when computing bridges");
} Prevention
- Never let graph vertex sources come from nullable lookups without coalescing to Enumerable.Empty<T>().
- Enable C# nullable reference types so null vertex arguments are caught at compile time.
- Validate graph inputs at the boundary where the graph is loaded, not deep in algorithm calls.
When it happens
Trigger: Calling Algorithms.Graph.Bridges.Find<T>(vertices, getNeighbors) with vertices == null, e.g. Find<int>(null, v => graph[v]).
Common situations: Passing a dictionary lookup result that is null (TryGetValue failed), a factory method returning null instead of an empty collection, or wiring up a graph variable that was never initialized before computing bridges.
Related errors
- ArgumentNullException: getNeighbors
- ArgumentNullException: graph
- ArgumentNullException: features
- Input data cannot be null.
- The parameters 'listOfAs' and 'listOfNs' must not be null…
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/ae3f7a812785aa1b.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Graph/Bridges.cs:26
/// Finds bridges (cut edges) in an undirected graph.
/// A bridge is an edge whose removal increases the number of connected components.
/// </summary>
public static class Bridges
{
/// <summary>
/// Finds all bridges in an undirected graph.
/// </summary>
/// <typeparam name="T">Type of vertex.</typeparam>
/// <param name="vertices">All vertices in the graph.</param>
/// <param name="getNeighbors">Function to get neighbors of a vertex.</param>
/// <returns>Set of bridges as tuples of vertices.</returns>
public static HashSet<(T From, T To)> Find<T>(
IEnumerable<T> vertices,
Func<T, IEnumerable<T>> getNeighbors) where T : notnull
{
if (vertices == null)
{
throw new ArgumentNullException(nameof(vertices));
}
if (getNeighbors == null)
{
throw new ArgumentNullException(nameof(getNeighbors));
}
var vertexList = vertices.ToList();
if (vertexList.Count == 0)
{
return new HashSet<(T, T)>();
}
var bridges = new HashSet<(T, T)>();
var visited = new HashSet<T>();
var discoveryTime = new Dictionary<T, int>();
var low = new Dictionary<T, int>();
var parent = new Dictionary<T, T?>();View on GitHub (pinned to 96e2905cab)