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 GraphAdjacencyList.remove_edge when both vertices exist but there is no edge between them (contains_edge returns False, i.e. destination_vertex not in adj_list[source_vertex]). This is the second check in remove_edge, after vertex existence. The error message names both endpoints so you can inspect the adjacency lists directly.

Source

Thrown at graphs/graph_adjacency_list.py:151

        """
        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)

        # remove the destination vertex from the list associated with the source
        # vertex and vice versa if not directed
        self.adj_list[source_vertex].remove(destination_vertex)
        if not self.directed:
            self.adj_list[destination_vertex].remove(source_vertex)

    def contains_vertex(self, vertex: T) -> bool:
        """
        Returns True if the graph contains the vertex, False otherwise.
        """
        return vertex in self.adj_list

    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.

View on GitHub (pinned to f5988cc097)

Solutions

  1. Check `graph.contains_edge(u, v)` before remove_edge.
  2. For directed graphs, verify direction: contains_edge(u, v) is not the same as contains_edge(v, u).
  3. Make removal idempotent with a contains_edge guard or try/except ValueError.

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  # re-raise vertex-missing errors, swallow edge-missing only

Prevention

When it happens

Trigger: Calling remove_edge(u, v) on vertices that exist but were never connected; removing an edge twice; removing an edge in the wrong direction on a directed graph (u->v does not imply v->u).

Common situations: Directed-graph code that assumes symmetry (removing v->u after u->v); idempotent retry logic that re-runs a successful removal; toggling operations that pair add/remove calls unevenly.

Related errors


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