Graph Theory37 sections · 1633 units
Open in Course

Intro

Why we need something else

DFS searches as deep as possible before backtracking. When you want the shortest path in an unweighted graph, this causes problems. DFS might dive 100100 nodes deep before finding the target, when the target was only 33 steps away from the start. There is no guarantee DFS finds the shortest route.

You need an algorithm that explores nodes in order of their distance from the source. That is where BFS comes in. BFS expands outward from the source like ripples in water, visiting all nodes at distance 11 before any node at distance 22.

Screenshot 2025-12-23 at 11.45.59 AM.png