how bfs gives shortest path?
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.