When does breadth-first search find a shortest path, and when is it the wrong choice?
Interactive Practice Mode
Formulate your thoughts or take notes before revealing the model solution.
Saved automatically
Model Answer & Deep-Dive Explanation
Breadth-first search explores vertices by edge distance and finds shortest paths in an unweighted graph, or when every edge has the same cost. Mark vertices when enqueuing to avoid repeated work and retain parents to reconstruct a path. With adjacency lists, runtime is O(V + E). Unequal edge weights invalidate the ordinary BFS guarantee: use Dijkstra’s algorithm for nonnegative weights or an appropriate algorithm when negative weights exist. Clarify whether the graph is directed and whether unreachable destinations should retu an empty result or an explicit failure.
Related Questions in Data Structures & Algorithms
How to detect a cycle in a Linked List using Floyd's Cycle-Finding Algorithm?
Intermediate level • 513 views
Explain the LRU (Least Recently Used) Cache architecture and how to implement it in O(1) time complexity.
Advanced level • 1 views
How do you design a Distributed API Rate Limiter and what algorithms are commonly employed?
Advanced level • 1 views
What is Dynamic Programming? Compare Memoization (Top-Down) with Tabulation (Bottom-Up) using the 0/1 Knapsack Problem.
Advanced level • 1 views
How do a stack and a queue differ, and where would each be useful?
Easy level • 1 views