Question: how bfs gives shortest path?
Answer: Dijkstra's algorithm adapts BFS to let you find single-source shortest paths. ... When the BFS queue is empty, pick a node (in your example, it's E) and traverse its predecessors back to the origin. This would give you the shortest path.