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 • 516 views
How do a stack and a queue differ, and where would each be useful?
Easy level • 8 views
When would you choose a hash map instead of an array for lookups?
Easy level • 7 views
How can you find two indices whose values sum to a target in one pass?
Medium level • 7 views
What is Dynamic Programming? Compare Memoization (Top-Down) with Tabulation (Bottom-Up) using the 0/1 Knapsack Problem.
Advanced level • 5 views