TheAlgorithms/Python · error · ValueError

Incorrect input: {vertex} does not exist in this graph.

Error message

Incorrect input: {vertex} does not exist in this graph.

What it means

Raised by GraphAdjacencyMatrix.remove_vertex when the vertex has no entry in vertex_to_index. Removal requires the vertex's index to pop the correct matrix row and column, so an unknown vertex cannot be processed. Nothing is mutated when this raises.

Source

Thrown at graphs/graph_adjacency_matrix.py:143

            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)

        # first slide up the rows by deleting the row corresponding to
        # the vertex being deleted.
        start_index = self.vertex_to_index[vertex]
        self.adj_matrix.pop(start_index)

        # next, slide the columns to the left by deleting the values in
        # the column corresponding to the vertex being deleted
        for lst in self.adj_matrix:
            lst.pop(start_index)

        # final clean up
        self.vertex_to_index.pop(vertex)

        # decrement indices for vertices shifted by the deleted vertex in the adj matrix
        for inner_vertex in self.vertex_to_index:
            if self.vertex_to_index[inner_vertex] >= start_index:
                self.vertex_to_index[inner_vertex] = (

View on GitHub (pinned to f5988cc097)

Solutions

  1. Check `graph.contains_vertex(vertex)` before remove_vertex.
  2. De-duplicate deletion requests before applying them.
  3. Catch ValueError for best-effort deletion endpoints.

Example fix

# before
graph.remove_vertex(node_id)

// after
if graph.contains_vertex(node_id):
    graph.remove_vertex(node_id)
Defensive patterns

Strategy: validation

Validate before calling

if graph.contains_vertex(vertex):
    graph.remove_vertex(vertex)

Type guard

def can_remove_vertex(graph, vertex) -> bool:
    return graph.contains_vertex(vertex)

Try / catch

try:
    graph.remove_vertex(vertex)
except ValueError:
    logger.debug("vertex %r already absent", vertex)

Prevention

When it happens

Trigger: Calling remove_vertex on a vertex never added, already removed, or removed implicitly by clear_graph(); passing a vertex of a different type than stored ('1' vs 1).

Common situations: Interactive or API-driven graph editing where clients request deletion of arbitrary ids; double-deletes; graphs rebuilt from filtered data while deletion requests reference the original ids.

Related errors


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