{"record":{"id":"a5cfc732318279f1","repo":"TheAlgorithms/Python","slug":"incorrect-input-vertex-does-not-exist-in-this-g-a5cfc7","errorCode":null,"errorMessage":"Incorrect input: {vertex} does not exist in this graph.","messagePattern":"Incorrect input: (.+?) does not exist in this graph\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"graphs/graph_adjacency_matrix.py","lineNumber":143,"sourceCode":"            raise ValueError(msg)\n\n        # build column for vertex\n        for row in self.adj_matrix:\n            row.append(0)\n\n        # build row for vertex and update other data structures\n        self.adj_matrix.append([0] * (len(self.adj_matrix) + 1))\n        self.vertex_to_index[vertex] = len(self.adj_matrix) - 1\n\n    def remove_vertex(self, vertex: T) -> None:\n        \"\"\"\n        Removes the given vertex from the graph and deletes all incoming and\n        outgoing edges from the given vertex as well. If the given vertex\n        does not exist, a ValueError will be thrown.\n        \"\"\"\n        if not self.contains_vertex(vertex):\n            msg = f\"Incorrect input: {vertex} does not exist in this graph.\"\n            raise ValueError(msg)\n\n        # first slide up the rows by deleting the row corresponding to\n        # the vertex being deleted.\n        start_index = self.vertex_to_index[vertex]\n        self.adj_matrix.pop(start_index)\n\n        # next, slide the columns to the left by deleting the values in\n        # the column corresponding to the vertex being deleted\n        for lst in self.adj_matrix:\n            lst.pop(start_index)\n\n        # final clean up\n        self.vertex_to_index.pop(vertex)\n\n        # decrement indices for vertices shifted by the deleted vertex in the adj matrix\n        for inner_vertex in self.vertex_to_index:\n            if self.vertex_to_index[inner_vertex] >= start_index:\n                self.vertex_to_index[inner_vertex] = (","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/graphs/graph_adjacency_matrix.py#L125-L161","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Check `graph.contains_vertex(vertex)` before remove_vertex.","De-duplicate deletion requests before applying them.","Catch ValueError for best-effort deletion endpoints."],"exampleFix":"# before\ngraph.remove_vertex(node_id)\n\n// after\nif graph.contains_vertex(node_id):\n    graph.remove_vertex(node_id)","handlingStrategy":"validation","validationCode":"if graph.contains_vertex(vertex):\n    graph.remove_vertex(vertex)","typeGuard":"def can_remove_vertex(graph, vertex) -> bool:\n    return graph.contains_vertex(vertex)","tryCatchPattern":"try:\n    graph.remove_vertex(vertex)\nexcept ValueError:\n    logger.debug(\"vertex %r already absent\", vertex)","preventionTips":["Expose deletion endpoints that check existence first.","De-duplicate deletion request lists.","Match vertex types exactly between construction and deletion."],"tags":["graph","adjacency-matrix","validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}