{"record":{"id":"efc034daa8e26bc1","repo":"TheAlgorithms/Python","slug":"incorrect-input-either-source-vertex-or-destin","errorCode":null,"errorMessage":"Incorrect input: Either {source_vertex} or {destination_vertex} does not exist","messagePattern":"Incorrect input: Either (.+?) or (.+?) does not exist","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"graphs/graph_adjacency_list.py","lineNumber":93,"sourceCode":"            msg = f\"Incorrect input: {vertex} is already in the graph.\"\n            raise ValueError(msg)\n        self.adj_list[vertex] = []\n\n    def add_edge(self, source_vertex: T, destination_vertex: T) -> None:\n        \"\"\"\n        Creates an edge from source vertex to destination vertex. If any\n        given vertex doesn't exist or the edge already exists, a ValueError\n        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 self.contains_edge(source_vertex, destination_vertex):\n            msg = (\n                \"Incorrect input: The edge already exists between \"\n                f\"{source_vertex} and {destination_vertex}\"\n            )\n            raise ValueError(msg)\n\n        # add the destination vertex to the list associated with the source vertex\n        # and vice versa if not directed\n        self.adj_list[source_vertex].append(destination_vertex)\n        if not self.directed:\n            self.adj_list[destination_vertex].append(source_vertex)\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.","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/graphs/graph_adjacency_list.py#L75-L111","documentation":"Raised by GraphAdjacencyList.add_edge (graphs/graph_adjacency_list.py:93) when either the source or destination vertex has not been added to the graph. add_edge cannot create implicit vertices, so both endpoints must already exist via add_vertex (or the vertices constructor parameter).","triggerScenarios":"g = GraphAdjacencyList(vertices=['A']); g.add_edge('A', 'Z') — 'Z' missing; building edges first and vertices later; typos or case mismatches between the vertex list and edge endpoints ('alice' vs 'Alice').","commonSituations":"Loading vertices and edges from separate files where the edge file references vertices absent from the vertex file; incremental construction where edges are added before their endpoints; inconsistent naming from data normalization bugs.","solutions":["Add both endpoints before the edge: ensure contains_vertex for both, calling add_vertex as needed","Or supply all vertices up front: GraphAdjacencyList(vertices=vs, edges=es) — but note this still requires edge endpoints to be in vs","Validate edges against the vertex set at load time and report unknown endpoints with row numbers"],"exampleFix":"# before\ng = GraphAdjacencyList(vertices=[\"A\"], edges=[])\ng.add_edge(\"A\", \"B\")  # raises: B does not exist\n\n# after\ng = GraphAdjacencyList(vertices=[\"A\"], edges=[])\nfor v in (\"A\", \"B\"):\n    if not g.contains_vertex(v):\n        g.add_vertex(v)\ng.add_edge(\"A\", \"B\")","handlingStrategy":"validation","validationCode":"for v in (source_vertex, destination_vertex):\n    if not g.contains_vertex(v):\n        g.add_vertex(v)\ng.add_edge(source_vertex, destination_vertex)","typeGuard":null,"tryCatchPattern":"try:\n    g.add_edge(src, dst)\nexcept ValueError as exc:\n    if \"does not exist\" in str(exc):\n        g.add_vertex(dst)\n        g.add_edge(src, dst)\n    else:\n        raise","preventionTips":["Register all vertices before any edges (constructor vertices= parameter or add_vertex loop)","Cross-check edge endpoint names against the vertex list at load time"],"tags":["graphs","vertex","missing-vertex","ordering"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}