Graph Theory37 sections · 1633 units
Open in Course

Think About It

(Why not DFS)

Ask yourself:

1.1. Does DFS guarantee the shortest path in a maze?

2.2. What happens if DFS finds a path of length 100100 but a path of length 1010 exists?

3.3. Why does BFS avoid this problem? Next unit explains why BFS is necessary.

DFS visits cells in depth-first order. It might go deep into one corner of the grid before finding B. BFS visits all cells at distance 11 first, then distance 22, and so on. The first time BFS reaches B, you know no shorter path exists.