TheAlgorithms/Python · error · ValueError
Incorrect input: Either {source_vertex} or {destination_vert
Error message
Incorrect input: Either {source_vertex} or {destination_vertex} does not exist What it means
Raised by GraphAdjacencyList.add_edge (graphs/graph_adjacency_list.py:93) when either the source or destination vertex has not been added to the graph. add_edge cannot create implicit vertices, so both endpoints must already exist via add_vertex (or the vertices constructor parameter).
Source
Thrown at graphs/graph_adjacency_list.py:93
msg = f"Incorrect input: {vertex} is already in the graph."
raise ValueError(msg)
self.adj_list[vertex] = []
def add_edge(self, source_vertex: T, destination_vertex: T) -> None:
"""
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.View on GitHub (pinned to f5988cc097)
Solutions
- Add both endpoints before the edge: ensure contains_vertex for both, calling add_vertex as needed
- Or supply all vertices up front: GraphAdjacencyList(vertices=vs, edges=es) — but note this still requires edge endpoints to be in vs
- Validate edges against the vertex set at load time and report unknown endpoints with row numbers
Example fix
# before
g = GraphAdjacencyList(vertices=["A"], edges=[])
g.add_edge("A", "B") # raises: B does not exist
# after
g = GraphAdjacencyList(vertices=["A"], edges=[])
for v in ("A", "B"):
if not g.contains_vertex(v):
g.add_vertex(v)
g.add_edge("A", "B") Defensive patterns
Strategy: validation
Validate before calling
for v in (source_vertex, destination_vertex):
if not g.contains_vertex(v):
g.add_vertex(v)
g.add_edge(source_vertex, destination_vertex) Try / catch
try:
g.add_edge(src, dst)
except ValueError as exc:
if "does not exist" in str(exc):
g.add_vertex(dst)
g.add_edge(src, dst)
else:
raise Prevention
- Register all vertices before any edges (constructor vertices= parameter or add_vertex loop)
- Cross-check edge endpoint names against the vertex list at load time
When it happens
Trigger: g = GraphAdjacencyList(vertices=['A']); g.add_edge('A', 'Z') — 'Z' missing; building edges first and vertices later; typos or case mismatches between the vertex list and edge endpoints ('alice' vs 'Alice').
Common situations: Loading vertices and edges from separate files where the edge file references vertices absent from the vertex file; incremental construction where edges are added before their endpoints; inconsistent naming from data normalization bugs.
Related errors
- You should execute algorithm before using its result!
- Incorrect input: {vertex} is already in the graph.
- 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/efc034daa8e26bc1.
Report an issue: GitHub.