{"record":{"id":"83443c065909f985","repo":"TheAlgorithms/Python","slug":"incorrect-input-either-source-vertex-or-destin-83443c","errorCode":null,"errorMessage":"Incorrect input: Either {source_vertex} or {destination_vertex} does not exist.","messagePattern":"Incorrect input: Either (.+?) or (.+?) does not exist\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"graphs/graph_adjacency_list.py","lineNumber":179,"sourceCode":"        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.\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} \"\n                f\"or {destination_vertex} does not exist.\"\n            )\n            raise ValueError(msg)\n\n        return destination_vertex in self.adj_list[source_vertex]\n\n    def clear_graph(self) -> None:\n        \"\"\"\n        Clears all vertices and edges.\n        \"\"\"\n        self.adj_list = {}\n\n    def __repr__(self) -> str:\n        return pformat(self.adj_list)\n\n\nclass TestGraphAdjacencyList(unittest.TestCase):\n    def __assert_graph_edge_exists_check(\n        self,\n        undirected_graph: GraphAdjacencyList,\n        directed_graph: GraphAdjacencyList,","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/graphs/graph_adjacency_list.py#L161-L197","documentation":"Raised by GraphAdjacencyList.contains_edge when either endpoint is not a vertex of the graph. Unlike a pure predicate, this implementation validates inputs and throws ValueError instead of returning False for missing vertices. So a seemingly read-only query can raise if the graph does not contain the queried nodes.","triggerScenarios":"Calling contains_edge(u, v) where u or v was never added or was removed; probing many candidate edges against a graph built from a subset of nodes; calling contains_edge after clear_graph().","commonSituations":"Developers assuming contains_edge is total (never raises) and using it as a filter over arbitrary node pairs; validation code that checks edges before checking vertices; graph rebuilt from filtered data while edge candidates come from the unfiltered set.","solutions":["Guard the query: `if graph.contains_vertex(u) and graph.contains_vertex(v) and graph.contains_edge(u, v)`.","Filter candidate endpoints to the graph's vertex set before probing edges.","Catch ValueError when scanning arbitrary pairs where missing vertices are expected."],"exampleFix":"// before\nif graph.contains_edge(u, v):  # raises if u or v absent\n    ...\n\n// after\nif (\n    graph.contains_vertex(u)\n    and graph.contains_vertex(v)\n    and graph.contains_edge(u, v)\n):\n    ...","handlingStrategy":"validation","validationCode":"def safe_contains_edge(graph, u, v) -> bool:\n    if not (graph.contains_vertex(u) and graph.contains_vertex(v)):\n        return False\n    return graph.contains_edge(u, v)","typeGuard":"def safe_contains_edge(graph, u, v) -> bool:\n    if not (graph.contains_vertex(u) and graph.contains_vertex(v)):\n        return False\n    return graph.contains_edge(u, v)","tryCatchPattern":"try:\n    has = graph.contains_edge(u, v)\nexcept ValueError:\n    has = False","preventionTips":["Remember this predicate raises on unknown vertices — do not use it as an unguarded filter.","Always pair contains_edge with contains_vertex checks on both endpoints.","Restrict queried ids to ids drawn from the graph itself."],"tags":["graph","adjacency-list","predicate-raises","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}