TheAlgorithms/Python · warning · ValueError
Incorrect input: The edge already exists between {source_ver
Error message
Incorrect input: The edge already exists between {source_vertex} and {destination_vertex} What it means
Raised by GraphAdjacencyList.add_edge (graphs/graph_adjacency_list.py:99) when an edge between source_vertex and destination_vertex already exists. This graph forbids parallel/multi-edges, so adding the same pair twice is rejected. In undirected mode (directed=False) the reverse direction also counts as the same edge via contains_edge.
Source
Thrown at graphs/graph_adjacency_list.py:99
Creates an edge from source vertex to destination vertex. If any
given vertex doesn't exist or the edge already exists, a ValueError
will be thrown.
"""
if not (
self.contains_vertex(source_vertex)
and self.contains_vertex(destination_vertex)
):
msg = (
f"Incorrect input: Either {source_vertex} or "
f"{destination_vertex} does not exist"
)
raise ValueError(msg)
if self.contains_edge(source_vertex, destination_vertex):
msg = (
"Incorrect input: The edge already exists between "
f"{source_vertex} and {destination_vertex}"
)
raise ValueError(msg)
# add the destination vertex to the list associated with the source vertex
# and vice versa if not directed
self.adj_list[source_vertex].append(destination_vertex)
if not self.directed:
self.adj_list[destination_vertex].append(source_vertex)
def remove_vertex(self, vertex: T) -> None:
"""
Removes the given vertex from the graph and deletes all incoming and
outgoing edges from the given vertex as well. If the given vertex
does not exist, a ValueError will be thrown.
"""
if not self.contains_vertex(vertex):
msg = f"Incorrect input: {vertex} does not exist in this graph."
raise ValueError(msg)
if not self.directed:View on GitHub (pinned to f5988cc097)
Solutions
- Guard with contains_edge: if not g.contains_edge(src, dst): g.add_edge(src, dst)
- Deduplicate symmetric pairs when loading undirected graphs: edges = {frozenset((u, v)) for u, v in raw} (add explicit handling if self-loops are possible)
- Catch ValueError where duplicates are expected and skip them
Example fix
# before
for u, v in raw_edges:
g.add_edge(u, v) # raises on repeated/symmetric pairs
# after
for u, v in raw_edges:
if not g.contains_edge(u, v):
g.add_edge(u, v) Defensive patterns
Strategy: validation
Validate before calling
if not g.contains_edge(source_vertex, destination_vertex):
g.add_edge(source_vertex, destination_vertex) Try / catch
try:
g.add_edge(src, dst)
except ValueError as exc:
if "already exists" in str(exc):
pass # skip duplicate
else:
raise Prevention
- Remember undirected graphs count (u,v) and (v,u) as the same edge
- Deduplicate symmetric pairs with frozenset((u, v)) when loading undirected data
When it happens
Trigger: g.add_edge('A','B') twice; in undirected graphs, add_edge('A','B') then add_edge('B','A') raises because the first call appended B to A's list and A to B's list; loading an edge list that contains the same pair in both orders.
Common situations: Symmetric datasets (e.g. friendship graphs) that list each relationship twice; merging multiple edge files with overlapping pairs; loops that re-add edges on retry or re-ingestion.
Related errors
- Incorrect input: {vertex} is already in the graph.
- Duplicate symbol ({i})
- Algorithm is unable to find solution
- Negative cycle found
- No path from vertex: {self.source_vertex} to vertex: {target
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/fb40095e104e3244.
Report an issue: GitHub.