How can I find the actual path found by BFS?

To do so, you need to store a map:V->V (from vertices to vertices), which will map from each node v, the vertex u that “discovered” v.

You will populate this map during the iterations of BFS.

Later – you can reconstruct the path by simply going from the target node (in the map) up until you get back to the source, which will be your path (reversed, of course).

Note that this map can be implemented as an array if you enumerate the vertices.

Leave a Comment