TheAlgorithms/Python · error · ValueError
Incorrect input: {vertex} already exists in this graph.
Error message
Incorrect input: {vertex} already exists in this graph. What it means
Raised by GraphAdjacencyMatrix.add_vertex when contains_vertex(vertex) is True, i.e. the vertex already has an index in vertex_to_index. Vertices act as unique keys; re-adding one would corrupt the index mapping and matrix dimensions, so the request is rejected. It commonly fires from the constructor's loop over the `vertices` argument when that list contains duplicates.
Source
Thrown at graphs/graph_adjacency_matrix.py:125
f"{source_vertex} and {destination_vertex}"
)
raise ValueError(msg)
# Get the indices of the corresponding vertices and set their edge value to 0.
u: int = self.vertex_to_index[source_vertex]
v: int = self.vertex_to_index[destination_vertex]
self.adj_matrix[u][v] = 0
if not self.directed:
self.adj_matrix[v][u] = 0
def add_vertex(self, vertex: T) -> None:
"""
Adds a vertex to the graph. If the given vertex already exists,
a ValueError will be thrown.
"""
if self.contains_vertex(vertex):
msg = f"Incorrect input: {vertex} already exists in this graph."
raise ValueError(msg)
# build column for vertex
for row in self.adj_matrix:
row.append(0)
# build row for vertex and update other data structures
self.adj_matrix.append([0] * (len(self.adj_matrix) + 1))
self.vertex_to_index[vertex] = len(self.adj_matrix) - 1
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)View on GitHub (pinned to f5988cc097)
Solutions
- Guard: `if not graph.contains_vertex(v): graph.add_vertex(v)`.
- De-duplicate the vertices list before passing it to the constructor: `vertices = list(dict.fromkeys(vertices))`.
- Use a set to track added vertices during ingestion.
Example fix
# before GraphAdjacencyMatrix(vertices=[1, 2, 2, 3]) # duplicate 2 # after GraphAdjacencyMatrix(vertices=list(dict.fromkeys([1, 2, 2, 3])))
Defensive patterns
Strategy: validation
Validate before calling
vertices = list(dict.fromkeys(vertices)) # order-preserving de-dup g = GraphAdjacencyMatrix(vertices=vertices)
Type guard
def no_duplicates(vertices) -> bool:
return len(vertices) == len(set(vertices)) Prevention
- De-duplicate vertex lists before constructor or add_vertex loops.
- Track added vertices in a set during ingestion.
- Never re-run initialization over an already-populated graph object.
When it happens
Trigger: Calling add_vertex on an existing vertex; passing a `vertices` list with duplicate entries to __init__; adding a vertex twice in a data-ingestion loop without de-duplication.
Common situations: Ingesting node lists from data with repeated ids; combining vertex sets from multiple sources without set operations; re-running initialization code over an existing graph object.
Related errors
- Invalid input: {edge} must have length 2.
- Incorrect input: Either {source_vertex} or {destination_vert
- Incorrect input: The edge already exists between {source_ver
- Incorrect input: The edge does NOT exist between {source_ver
- Incorrect input: {vertex} does not exist in this graph.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/042c440e7c8d035d.
Report an issue: GitHub.