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 GraphAdjacencyMatrix.contains_edge when either queried endpoint is not a registered vertex. Like the adjacency-list counterpart, this predicate validates its inputs and raises instead of returning False for unknown vertices, because it must map both endpoints to matrix indices (vertex_to_index lookups) to read the cell.
Source
Thrown at graphs/graph_adjacency_matrix.py:185
Returns True if the graph contains the vertex, False otherwise.
"""
return vertex in self.vertex_to_index
def contains_edge(self, source_vertex: T, destination_vertex: T) -> bool:
"""
Returns True if the graph contains the edge from the source_vertex to the
destination_vertex, False otherwise. If any given vertex doesn't exist, 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} "
f"or {destination_vertex} does not exist."
)
raise ValueError(msg)
u = self.vertex_to_index[source_vertex]
v = self.vertex_to_index[destination_vertex]
return self.adj_matrix[u][v] == 1
def clear_graph(self) -> None:
"""
Clears all vertices and edges.
"""
self.vertex_to_index = {}
self.adj_matrix = []
def __repr__(self) -> str:
first = "Adj Matrix:\n" + pformat(self.adj_matrix)
second = "\nVertex to index mapping:\n" + pformat(self.vertex_to_index)
return first + second
View on GitHub (pinned to f5988cc097)
Solutions
- Check both endpoints first: `graph.contains_vertex(u) and graph.contains_vertex(v) and graph.contains_edge(u, v)`.
- Restrict edge queries to ids drawn from the graph's own vertex set.
- Catch ValueError when scanning pairs where unknown vertices are expected.
Example fix
# before
if graph.contains_edge(u, v): # raises on unknown vertex
...
# after
if (
graph.contains_vertex(u)
and graph.contains_vertex(v)
and graph.contains_edge(u, v)
):
... Defensive patterns
Strategy: validation
Validate before calling
def safe_contains_edge(graph, u, v) -> bool:
if not (graph.contains_vertex(u) and graph.contains_vertex(v)):
return False
return graph.contains_edge(u, v) Type guard
def safe_contains_edge(graph, u, v) -> bool:
if not (graph.contains_vertex(u) and graph.contains_vertex(v)):
return False
return graph.contains_edge(u, v) Try / catch
try:
has = graph.contains_edge(u, v)
except ValueError:
has = False Prevention
- Treat contains_edge as raising on unknown vertices, not as a total function.
- Filter candidate pairs by the graph's vertex set before probing.
- Keep a helper (safe_contains_edge) and use it everywhere instead of raw calls.
When it happens
Trigger: Calling contains_edge(u, v) where u or v is not in vertex_to_index; probing candidate edges against a graph built from a subset of nodes; querying after remove_vertex or clear_graph removed an endpoint.
Common situations: Using contains_edge as a filter over arbitrary node pairs from external data; assuming predicates are exception-safe in validation code; checking for an edge whose endpoints were never verified to exist.
Related errors
- Incorrect input: Either {source_vertex} or {destination_vert
- Incorrect input: {vertex} does not exist in this graph.
- Incorrect input: {vertex} does not exist in this graph.
- Invalid input: {edge} must have length 2.
- Incorrect input: Either {source_vertex} or {destination_vert
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/d52ecd4240f48c4c.
Report an issue: GitHub.