{"record":{"id":"9d25b39d61183928","repo":"TheAlgorithms/Python","slug":"no-path-from-vertex-self-source-vertex-to-verte","errorCode":null,"errorMessage":"No path from vertex: {self.source_vertex} to vertex: {target_vertex}","messagePattern":"No path from vertex: (.+?) to vertex: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"graphs/breadth_first_search_shortest_path.py","lineNumber":80,"sourceCode":"        Traceback (most recent call last):\n            ...\n        ValueError: No path from vertex: G to vertex: Foo\n\n        Case 2 - The path is found.\n        >>> g.shortest_path(\"D\")\n        'G->C->A->B->D'\n        >>> g.shortest_path(\"G\")\n        'G'\n        \"\"\"\n        if target_vertex == self.source_vertex:\n            return self.source_vertex\n\n        target_vertex_parent = self.parent.get(target_vertex)\n        if target_vertex_parent is None:\n            msg = (\n                f\"No path from vertex: {self.source_vertex} to vertex: {target_vertex}\"\n            )\n            raise ValueError(msg)\n\n        return self.shortest_path(target_vertex_parent) + f\"->{target_vertex}\"\n\n\nif __name__ == \"__main__\":\n    g = Graph(graph, \"G\")\n    g.breath_first_search()\n    print(g.shortest_path(\"D\"))\n    print(g.shortest_path(\"G\"))\n    print(g.shortest_path(\"Foo\"))\n","sourceCodeStart":62,"sourceCodeEnd":91,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/graphs/breadth_first_search_shortest_path.py#L62-L91","documentation":"Raised by Graph.shortest_path (graphs/breadth_first_search_shortest_path.py:80) when the BFS parent map has no entry for target_vertex — meaning the target was never reached during breadth-first search from source_vertex. Typical causes: the target is not in the graph at all, or it lies in a different connected component (or is unreachable in a directed graph).","triggerScenarios":"g.shortest_path(\"Foo\") where \"Foo\" was never a vertex; g.shortest_path(\"D\") where D belongs to a component disconnected from the BFS source; calling shortest_path before breath_first_search() was run so parent is empty.","commonSituations":"Looking up a path to a vertex that was never added; directed graphs where the target is only reachable in the reverse direction; forgetting to call breath_first_search() after constructing Graph(graph, source).","solutions":["Call breath_first_search() once after constructing the Graph, before any shortest_path call","Verify the target exists and was reached: check target_vertex in g.parent (or in the graph) before calling shortest_path","For directed graphs, confirm the target is downstream of the source vertex"],"exampleFix":"# before\nprint(g.shortest_path(\"D\"))  # may raise if D unreachable\n\n# after\ng.breath_first_search()\nif \"D\" in g.parent or \"D\" == g.source_vertex:\n    print(g.shortest_path(\"D\"))\nelse:\n    print(\"no path\")","handlingStrategy":"validation","validationCode":"g.breath_first_search()  # must run first to populate g.parent\nif target != g.source_vertex and target not in g.parent:\n    raise LookupError(f\"{target!r} is not reachable from {g.source_vertex!r}\")\npath = g.shortest_path(target)","typeGuard":null,"tryCatchPattern":"try:\n    print(g.shortest_path(target))\nexcept ValueError:\n    print(f\"no path to {target}\")","preventionTips":["Always call breath_first_search() before shortest_path","Check membership in g.parent (or your vertex set) before path queries"],"tags":["graphs","bfs","shortest-path","unreachable"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}