Problem
Given an undirected, unweighted graph G(V, E) with N vertices and M edges, find the minimum number of edges (i.e., the shortest path in terms of edge count) between two given vertices u and v.
Examples
Example 1
|
|
Example 2
|
|
Solution
Method 1 – Breadth-First Search (BFS)
Intuition
Breadth-first search (BFS) explores the graph in layers: first all vertices at distance 0 (the source), then all vertices at distance 1, then distance 2, and so on. Because each edge contributes exactly 1 to path length in an unweighted graph, the first time BFS reaches a vertex it has found a shortest path (in number of edges) to that vertex.
One convenient way to see this is by induction on the BFS level. After processing k layers BFS has discovered all vertices at distance <= k and assigned them the correct distance. When BFS expands all vertices at level k it discovers previously-unseen neighbors and labels them distance k+1; by construction these cannot have a shorter path than k+1 because any path to them must come through some vertex at level <= k.
Equivalently, BFS implicitly builds a BFS tree rooted at the source; the tree path to any vertex is a shortest (edge-count) path in the original graph.
This is how above BFS tree may look:
graph TB A("s") A --- B1("1") A --- B2("1") A --- B3("1") A --- B4("1") A --- B5("1") A --- B6("1") A --- B7("1") A --- B8("1") B1 --- C1("2") B2 --- C2("2") B3 --- C3("2") B4 --- C4("2") B5 --- C5("2") B6 --- C6("2") B7 --- C7("2") B8 --- C8("2")
Approach
- Initialize a queue and a visited array for all vertices.
- Start BFS from the source vertex u, marking it visited and setting its distance to 0.
- For each vertex dequeued, enqueue all unvisited neighbors, updating their distance as distance of current + 1.
- Stop when the target vertex v is reached; its distance is the answer.
- If v is unreachable, return -1.
Pseudocode
|
|
Code
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(N + M)
, where N is the number of vertices and M is the number of edges, since each vertex and edge is processed at most once. - 🧺 Space complexity:
O(N)
, for the visited and distance arrays and the queue.
Application
Finding the shortest path (by edge count) is a fundamental primitive used in many domains: routing and networking, map/navigation, social networks, puzzle solving, and game AI. Running BFS from a source gives you not only the distance to a single target but the shortest distances and predecessors for all reachable vertices in the same O(N + M) time. That extra information is useful for reconstructing paths, computing reachability or eccentricity measures, performing multi-source searches, and as a subroutine in larger algorithms.
When edges have unequal weights, use Dijkstra or Bellman–Ford instead; the BFS approach is the right tool specifically for unweighted or equal-weight graphs.
When not to use BFS
Use BFS only when all edges have equal weight (or when you care about edge-count distance). If edges have non-equal non-negative weights, use Dijkstra’s algorithm (O((N+M) log N) with a heap). If negative edge weights are allowed, use Bellman–Ford (or detect/handle negative cycles).