Skip to Content

When does BFS beat Dijkstra?

Category: DSA Patterns

Answer

BFS finds shortest path when every edge has equal weight. Dijkstra is required when edges have positive weights that differ. Reach for BFS first; only graduate to Dijkstra if weights matter.

Concrete examples from the fca project context

Example 1

Level-order tree traversal: queue root, pop, push children.

Example 2

Shortest path in unweighted graph: BFS from source; first time you hit target is the answer.

Example 3

Word ladder (Bacon number): BFS over word mutations.

Data flow / flow chart

queue <- source
  pop, push neighbors, mark distance
  first time target popped -> shortest distance found

Takeaway

Equal-weight edges? BFS. Different weights? Dijkstra. Negative weights? Bellman-Ford.