Search code examples
algorithmgraphbreadth-first-search

Apply BFS, with goal node what is the path?


I is the starting node and E is the goal node. What will be the path that would be return by BFS? I know this is easy the path that I found is I-H-E but my teacher is saying that the correct path is I-F-H-C-E. So I really don't know how did she found that path. Any idea?

https://i.sstatic.net/pfDrx.png


Solution

  • The correct path would be E-H-I.

    Initially visited node: I

    After the first iteration visited nodes [ I, F, H ]

    Second Iteration visited nodes [ I, F, H, E, G, C ]

    Also, the path leading to E, in this case, is though H. So I -> H -> E.