Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Algorithms
Algorithmshard

When can BFS replace Dijkstra’s algorithm?

Tags
#shortest-path#bfs#dijkstra#graph
Back to categoryPractice quiz

Answer

If all edges have equal weight (e.g., weight 1), BFS finds shortest paths. If weights are only 0 or 1, you can use 0–1 BFS with a deque; otherwise you need Dijkstra.

Advanced answer

Deep dive

Dijkstra is the general shortest-path algorithm for non-negative weights (different costs), usually with a priority queue.

BFS is the special case where every edge cost is identical (typically 1). In that world, “shortest path” means “fewest edges”, and BFS explores nodes in increasing hop count.

Useful variants

  • 0–1 BFS (weights in {0,1}): use a deque (0-weight edges to front, 1-weight to back) in O(V+E).
  • Small integer weights: Dial’s algorithm (bucket queue).

Common pitfalls

  • Using BFS on weighted graphs and expecting correct weighted distances.
  • Mixing up “shortest by hops” vs “shortest by total weight”.

Related questions

Algorithms
A* search: how does the heuristic affect optimality?
#a-star#heuristic#shortest-path
Algorithms
Bellman–Ford: when do you use it and what extra capability does it have over Dijkstra?
#bellman-ford#shortest-path#graphs
Algorithms
What does the Floyd–Warshall algorithm compute and what is its complexity?
#graphs
#shortest-path
#floyd-warshall
Algorithms
A* vs Dijkstra — what’s the difference and when is A* faster?
#a-star#dijkstra#heuristics
Algorithms
Why doesn’t Dijkstra work with negative edge weights, and what can you use instead?
#dijkstra#bellman-ford#negative-weights
Algorithms
What is a topological sort and when is it possible?
#topological-sort#dag#graph