{"record":{"id":"d52ecd4240f48c4c","repo":"TheAlgorithms/Python","slug":"incorrect-input-either-source-vertex-or-destin-d52ecd","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_matrix.py","lineNumber":185,"sourceCode":"        Returns True if the graph contains the vertex, False otherwise.\n        \"\"\"\n        return vertex in self.vertex_to_index\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        u = self.vertex_to_index[source_vertex]\n        v = self.vertex_to_index[destination_vertex]\n        return self.adj_matrix[u][v] == 1\n\n    def clear_graph(self) -> None:\n        \"\"\"\n        Clears all vertices and edges.\n        \"\"\"\n        self.vertex_to_index = {}\n        self.adj_matrix = []\n\n    def __repr__(self) -> str:\n        first = \"Adj Matrix:\\n\" + pformat(self.adj_matrix)\n        second = \"\\nVertex to index mapping:\\n\" + pformat(self.vertex_to_index)\n        return first + second\n\n","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/graphs/graph_adjacency_matrix.py#L167-L203","documentation":"Raised by GraphAdjacencyMatrix.contains_edge when either queried endpoint is not a registered vertex. Like the adjacency-list counterpart, this predicate validates its inputs and raises instead of returning False for unknown vertices, because it must map both endpoints to matrix indices (vertex_to_index lookups) to read the cell.","triggerScenarios":"Calling contains_edge(u, v) where u or v is not in vertex_to_index; probing candidate edges against a graph built from a subset of nodes; querying after remove_vertex or clear_graph removed an endpoint.","commonSituations":"Using contains_edge as a filter over arbitrary node pairs from external data; assuming predicates are exception-safe in validation code; checking for an edge whose endpoints were never verified to exist.","solutions":["Check both endpoints first: `graph.contains_vertex(u) and graph.contains_vertex(v) and graph.contains_edge(u, v)`.","Restrict edge queries to ids drawn from the graph's own vertex set.","Catch ValueError when scanning pairs where unknown vertices are expected."],"exampleFix":"# before\nif graph.contains_edge(u, v):  # raises on unknown vertex\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":["Treat contains_edge as raising on unknown vertices, not as a total function.","Filter candidate pairs by the graph's vertex set before probing.","Keep a helper (safe_contains_edge) and use it everywhere instead of raw calls."],"tags":["graph","adjacency-matrix","predicate-raises","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}