{"record":{"id":"f481e8c9e1d776b3","repo":"TheAlgorithms/Python","slug":"incorrect-input-the-edge-does-not-exist-between","errorCode":null,"errorMessage":"Incorrect input: The edge does NOT exist between {source_vertex} and {destination_vertex}","messagePattern":"Incorrect input: The edge does NOT exist between (.+?) and (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"graphs/graph_adjacency_list.py","lineNumber":151,"sourceCode":"        \"\"\"\n        Removes the edge between the two vertices. If any given vertex\n        doesn't exist or the edge does not exist, a ValueError will be thrown.\n        \"\"\"\n        if not (\n            self.contains_vertex(source_vertex)\n            and self.contains_vertex(destination_vertex)\n        ):\n            msg = (\n                f\"Incorrect input: Either {source_vertex} or \"\n                f\"{destination_vertex} does not exist\"\n            )\n            raise ValueError(msg)\n        if not self.contains_edge(source_vertex, destination_vertex):\n            msg = (\n                \"Incorrect input: The edge does NOT exist between \"\n                f\"{source_vertex} and {destination_vertex}\"\n            )\n            raise ValueError(msg)\n\n        # remove the destination vertex from the list associated with the source\n        # vertex and vice versa if not directed\n        self.adj_list[source_vertex].remove(destination_vertex)\n        if not self.directed:\n            self.adj_list[destination_vertex].remove(source_vertex)\n\n    def contains_vertex(self, vertex: T) -> bool:\n        \"\"\"\n        Returns True if the graph contains the vertex, False otherwise.\n        \"\"\"\n        return vertex in self.adj_list\n\n    def contains_edge(self, source_vertex: T, destination_vertex: T) -> bool:\n        \"\"\"\n        Returns True if the graph contains the edge from the source_vertex to the\n        destination_vertex, False otherwise. If any given vertex doesn't exist, a\n        ValueError will be thrown.","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/graphs/graph_adjacency_list.py#L133-L169","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Check `graph.contains_edge(u, v)` before remove_edge.","For directed graphs, verify direction: contains_edge(u, v) is not the same as contains_edge(v, u).","Make removal idempotent with a contains_edge guard or try/except ValueError."],"exampleFix":"// before\ngraph.remove_edge(u, v)\n\n// after\nif graph.contains_edge(u, v):\n    graph.remove_edge(u, v)","handlingStrategy":"validation","validationCode":"if graph.contains_edge(source_vertex, destination_vertex):\n    graph.remove_edge(source_vertex, destination_vertex)","typeGuard":"def edge_exists(graph, u, v) -> bool:\n    try:\n        return graph.contains_edge(u, v)\n    except ValueError:\n        return False","tryCatchPattern":"try:\n    graph.remove_edge(u, v)\nexcept ValueError as exc:\n    if \"does NOT exist\" not in str(exc):\n        raise  # re-raise vertex-missing errors, swallow edge-missing only","preventionTips":["For directed graphs, always confirm direction before removal.","De-duplicate removal requests before applying.","Pair add/remove calls symmetrically so counts stay balanced."],"tags":["graph","adjacency-list","edges","directed-graph"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}