How can I find the actual path found by BFS?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Breadth-first search can tell you whether a target is reachable, but it can also recover the exact shortest path in an unweighted graph. The missing ingredient is a parent map that remembers how each discovered node was reached.
That is the core idea: BFS explores level by level, and each time it discovers a new node, it stores the predecessor that led to it. Once the target is found, you walk backward through those predecessors and reverse the result.
Why BFS Finds the Shortest Path
In an unweighted graph, BFS visits nodes in order of increasing distance from the start. That means the first time BFS reaches a node, it has already found the shortest path to that node in terms of number of edges.
So if you store the parent at discovery time, the recorded parent chain for the goal corresponds to a shortest path.
Store Parents While Exploring
Here is a complete Python example:
The result is one shortest path, such as ['A', 'C', 'F'].
How the Parent Map Works
Suppose BFS starts at A:
- it discovers
Band storesparent['B'] = 'A' - it discovers
Cand storesparent['C'] = 'A' - later it discovers
FfromC, so it storesparent['F'] = 'C'
Once the goal is found, reconstruction is just repeated parent lookup:
- '
F' - parent of
FisC - parent of
CisA - parent of
AisNone
Reverse that sequence and you have the path from start to goal.
Why You Should Mark Visited Early
Notice that the code marks a node as visited when it is enqueued, not when it is dequeued. That matters because it prevents the same node from being added to the queue multiple times from different parents.
If you delay the visited mark, you may overwrite the clean shortest-path parent information or waste work exploring duplicates.
What Happens If There Is No Path
If the goal is unreachable, it never appears in the parent map. That is why the code checks:
This is a simple and reliable way to distinguish "found a path" from "searched the connected component and never reached the target."
Common Pitfalls
- Running BFS without a parent map and then wondering why only the distance is known.
- Marking nodes visited too late and allowing duplicates into the queue.
- Overwriting parent pointers after a node has already been discovered.
- Forgetting to handle the no-path case explicitly.
- Assuming BFS gives weighted shortest paths. It only guarantees shortest paths in unweighted graphs or graphs with equal edge cost.
Summary
- To recover the actual BFS path, store each node's parent when the node is first discovered.
- BFS guarantees a shortest path in an unweighted graph because it explores by distance layers.
- Reconstruct the path by walking backward from the goal through the parent map.
- Mark nodes visited when they are enqueued, not later.
- If the goal never enters the parent map, no path exists from the chosen start node.
Related reading
- How can I find the best fit subsequences of a large string?
- How can I find the maximum sum of a sub-sequence using dynamic programming?
- How can I find the minimal circle include some given points?
- How can I find the number of Hamiltonian cycles in a complete undirected graph?
- How can I find the index for a given item in a list?
- How can I find the method that called the current method?
- How can I find the shortest path between 100 moving targets? Live demo included.
- How can I find the time complexity of an algorithm?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.