Breadth-first search finds a shortest path when all edges have equal cost

← Back to Blog & Articles

Breadth-first search explores a graph in increasing numbers of edges from a starting vertex. With equal edge costs, that exploration order lets the first discovery of a vertex establish a shortest path to it.

Worked example

Start with a queue containing the source. Remove one vertex, inspect its neighbors, and enqueue each undiscovered neighbor. Record the predecessor when a neighbor is first discovered. Walking those predecessor links backward from a destination reconstructs a shortest path. Marking vertices at enqueue time prevents several parents from adding the same vertex repeatedly.

What to check

The shortest-path claim depends on the edge-cost assumption. When costs differ, the fewest edges may have a larger total cost. Check disconnected destinations and cycles as well as simple paths. With adjacency lists, the traversal takes O(V + E) time for the reachable portion of the graph and uses O(V) additional storage for discovered vertices, predecessors, and the queue.

Continue with the book

Explore the full treatment, worked examples, and exercises in Algorithms Made Simple.


Companion article for Algorithms Made Simple, published by Choral Software. Article text and original examples © 2026 Choral Software. All rights reserved.

Scroll to Top