{"record":{"id":"b5bc1a3bcb398b8c","repo":"TheAlgorithms/Python","slug":"incorrect-input-the-edge-does-not-exist-between-b5bc1a","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_matrix.py","lineNumber":109,"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        # Get the indices of the corresponding vertices and set their edge value to 0.\n        u: int = self.vertex_to_index[source_vertex]\n        v: int = self.vertex_to_index[destination_vertex]\n        self.adj_matrix[u][v] = 0\n        if not self.directed:\n            self.adj_matrix[v][u] = 0\n\n    def add_vertex(self, vertex: T) -> None:\n        \"\"\"\n        Adds a vertex to the graph. If the given vertex already exists,\n        a ValueError will be thrown.\n        \"\"\"\n        if self.contains_vertex(vertex):\n            msg = f\"Incorrect input: {vertex} already exists in this graph.\"\n            raise ValueError(msg)\n\n        # build column for vertex","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/graphs/graph_adjacency_matrix.py#L91-L127","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Guard with `if graph.contains_edge(u, v): graph.remove_edge(u, v)`.","For directed graphs, verify the direction before removing.","Skip removals involving vertices you already removed."],"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","preventionTips":["Make removals idempotent with a contains_edge guard.","On directed graphs check the exact direction.","Do not re-remove edges whose endpoint vertex was already removed."],"tags":["graph","adjacency-matrix","edges","directed-graph"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}