Repaso
Español
Algorithms & DSIntermediateALG-08

Shortest path (fewest edges) in an unweighted graph?

Options

  1. DFS
  2. BFS (Correct option)
  3. Dijkstra with a max-heap
  4. Topological sort

Answer

BFS

Why

BFS (breadth-first search) explores in rings: first all neighbors 1 step away, then all at 2 steps, and so on, using a queue.

So the first time BFS reaches a node, it got there with the fewest possible edges.

DFS goes deep along one path first, so the first path it finds may be long. Cost of BFS: O(V + E).

Remember

Fewest steps in an unweighted graph → BFS.

Key terms

Practice this question in Repaso