TheAlgorithms/C-Sharp · error · InvalidOperationException
Vertex already exists
Error message
Vertex already exists: {currentEdgeWeight} What it means
ThrowIfEdgeExists throws when AddEdge is called for a vertex pair that already has an edge. The (misleading) message interpolates the existing edge's weight, e.g. 'Vertex already exists: 5'. The library does not silently overwrite edges; callers must remove the edge first.
Solutions
- Check first: if (!AreAdjacent(u, v)) AddEdge(u, v, w); else RemoveEdge then AddEdge.
- Deduplicate edge input before insertion (distinct on the vertex pair).
- Wrap in try/catch for InvalidOperationException when re-adding is expected/ignorable.
Example fix
// before graph.AddEdge(a, b, 2.0); graph.AddEdge(a, b, 3.0); // throws // after if (graph.AreAdjacent(a, b)) graph.RemoveEdge(a, b); graph.AddEdge(a, b, 3.0);
Defensive patterns
Strategy: validation
Validate before calling
if (!graph.AreAdjacent(u, v)) { graph.AddEdge(u, v, w); } Try / catch
try { graph.AddEdge(u, v, w); }
catch (InvalidOperationException ex) when (ex.Message.StartsWith("Vertex already exists:")) { /* edge already present: ignore or update */ } Prevention
- Check AreAdjacent before adding.
- Deduplicate edge lists on the (from,to) pair before import.
- Use RemoveEdge + AddEdge to change an existing weight.
When it happens
Trigger: Calling AddEdge(u, v, w) twice for the same vertex pair; the second call finds the current matrix weight non-zero and throws.
Common situations: Re-running graph-building code without clearing state, importing edge lists containing duplicate edges, or trying to update an edge's weight via AddEdge instead of RemoveEdge-then-AddEdge.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- Vertex does not belong to graph
- Graph contains a negative weight cycle.
- Matrix must be symmetric!
- Graph must be undirected!
- Adjacency matrix must be square!
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/de8a5eb3d74dfc53.
Report an issue: GitHub.
Appendix: source
Thrown at DataStructures/Graph/DirectedWeightedGraph.cs:214
if (capacity < 0)
{
throw new InvalidOperationException("Graph capacity should always be a non-negative integer.");
}
}
private static void ThrowIfWeightZero(double weight)
{
if (weight.Equals(0.0d))
{
throw new InvalidOperationException("Edge weight cannot be zero.");
}
}
private static void ThrowIfEdgeExists(double currentEdgeWeight)
{
if (!currentEdgeWeight.Equals(0.0d))
{
throw new InvalidOperationException($"Vertex already exists: {currentEdgeWeight}");
}
}
private void ThrowIfOverflow()
{
if (Count == capacity)
{
throw new InvalidOperationException("Graph overflow.");
}
}
private void ThrowIfVertexNotInGraph(Vertex<T> vertex)
{
if (vertex.Graph != this)
{
throw new InvalidOperationException($"Vertex does not belong to graph: {vertex}.");
}
}View on GitHub (pinned to 96e2905cab)