{"record":{"id":"6d615373bc695abf","repo":"TheAlgorithms/Python","slug":"incorrect-input-either-source-vertex-or-destin-6d6153","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":75,"sourceCode":"                msg = f\"Invalid input: {edge} must have length 2.\"\n                raise ValueError(msg)\n            self.add_edge(edge[0], edge[1])\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        # Get the indices of the corresponding vertices and set their edge value to 1.\n        u: int = self.vertex_to_index[source_vertex]\n        v: int = self.vertex_to_index[destination_vertex]\n        self.adj_matrix[u][v] = 1\n        if not self.directed:\n            self.adj_matrix[v][u] = 1\n\n    def remove_edge(self, source_vertex: T, destination_vertex: T) -> None:\n        \"\"\"\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.","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/graphs/graph_adjacency_matrix.py#L57-L93","documentation":"Raised by GraphAdjacencyMatrix.add_edge when source_vertex or destination_vertex is not in vertex_to_index (contains_vertex fails). The matrix implementation indexes cells by vertex position, so both endpoints must already be registered via add_vertex or the vertices constructor argument before any edge can reference them.","triggerScenarios":"Calling add_edge(u, v) before add_vertex(u) or add_vertex(v); building edges from data referencing nodes not in the vertices list passed to __init__; adding an edge after remove_vertex removed an endpoint.","commonSituations":"Edge lists from files/databases whose node ids are a superset of the declared vertex set; ordering bugs where edge insertion runs before vertex insertion; assuming add_edge auto-creates missing endpoints (it does not).","solutions":["Add both vertices first: `for v in (u, v): if not g.contains_vertex(v): g.add_vertex(v)` then add_edge.","Ensure the vertices argument to the constructor covers every endpoint in edges.","Validate edge endpoints against the vertex set during data loading."],"exampleFix":"# before\ngraph.add_edge(new_node, existing_node)\n\n# after\nif not graph.contains_vertex(new_node):\n    graph.add_vertex(new_node)\ngraph.add_edge(new_node, existing_node)","handlingStrategy":"validation","validationCode":"for v in (source_vertex, destination_vertex):\n    if not graph.contains_vertex(v):\n        graph.add_vertex(v)\ngraph.add_edge(source_vertex, destination_vertex)","typeGuard":"def endpoints_present(graph, u, v) -> bool:\n    return graph.contains_vertex(u) and graph.contains_vertex(v)","tryCatchPattern":null,"preventionTips":["Register all vertices before inserting any edges.","Pass the complete vertex set to the constructor and validate edge endpoints against it.","add_edge never auto-creates vertices — do it explicitly."],"tags":["graph","adjacency-matrix","edges","vertex-registration"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}