TheAlgorithms/Python · error · ValueError

Incorrect input: The edge does NOT exist between {source_ver

Error message

Incorrect input: The edge does NOT exist between {source_vertex} and {destination_vertex}

What it means

Raised by GraphAdjacencyMatrix.remove_edge when both vertices exist but the matrix cell for (source, destination) is 0 — there is no edge to remove. On directed graphs direction matters: removing v->u when only u->v exists raises this. The matrix is left unchanged.

Source

Thrown at graphs/graph_adjacency_matrix.py:109

        """
        Removes the edge between the two vertices. If any given vertex
        doesn't exist or the edge does not 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} or "
                f"{destination_vertex} does not exist"
            )
            raise ValueError(msg)
        if not self.contains_edge(source_vertex, destination_vertex):
            msg = (
                "Incorrect input: The edge does NOT exist between "
                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

View on GitHub (pinned to f5988cc097)

Solutions

  1. Guard with `if graph.contains_edge(u, v): graph.remove_edge(u, v)`.
  2. For directed graphs, verify the direction before removing.
  3. Skip removals involving vertices you already removed.

Example fix

# before
graph.remove_edge(u, v)

# after
if graph.contains_edge(u, v):
    graph.remove_edge(u, v)
Defensive patterns

Strategy: validation

Validate before calling

if graph.contains_edge(source_vertex, destination_vertex):
    graph.remove_edge(source_vertex, destination_vertex)

Type guard

def edge_exists(graph, u, v) -> bool:
    try:
        return graph.contains_edge(u, v)
    except ValueError:
        return False

Try / catch

try:
    graph.remove_edge(u, v)
except ValueError as exc:
    if "does NOT exist" not in str(exc):
        raise

Prevention

When it happens

Trigger: Calling remove_edge on an absent edge; removing an edge twice; wrong direction on a directed graph; removing an edge that remove_vertex already implicitly deleted along with its endpoint.

Common situations: Idempotent retry logic re-running a completed removal; directed-graph code assuming symmetry; deletions sequenced after remove_vertex that already cleared the row/column for that vertex.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/b5bc1a3bcb398b8c. Report an issue: GitHub.