Shortest Paths in an Unweighted Graph
Coding
Graph Traversal
Easy
Hint 1
Think about which traversal order visits vertices in increasing order of distance from the source.
Hint 2
This traversal explores all vertices at distance k before any vertex at distance k+1.
Show worked solution
1. Identify the traversal order
Breadth-first search explores vertices level by level outward from the source, visiting all vertices at distance before any at distance .
2. Conclude correctness
Since every edge has the same unit weight, this level-order visiting order exactly matches shortest-path distance, so the first time a vertex is reached gives its shortest distance.
Answer: Breadth-first search.
Related roles: Browse quant jobs