Shortest Path in Unweighted Graph
BFS: with every edge worth 1 step, the first visit to a node IS its shortest distance
When every edge costs the same, distance from the source is just how many BFS layers deep a node sits — so plain level-by-level BFS, not Dijkstra, already finds the shortest path. Track one distance per node (unknown until first discovered) and one parent pointer to rebuild the actual route afterward. The moment the destination is pulled off the queue, its distance is final and the answer is that number; nothing later in the queue can ever produce a shorter route to it.
Details
Animated on: n = 6, edges = [[0,1],[0,2],[1,3],[2,3],[3,4],[4,5]], source = 0, destination = 5 — shortest distance?
Problem:Shortest Path in Unweighted GraphMedium
BFS: with every edge worth 1 step, the first visit to a node IS its shortest distance
step 1 / 15
Undirected graph, unit edge weights
6 nodes, every edge worth 1 step. Find node 0's shortest distance to node 5. With every edge the same length, BFS visits nodes in strict order of distance from the source, so the first time 5 comes out of the queue, that distance is final.
1FUNCTION shortestPath(n, edges, source, dest):2 BUILD adjacency list FROM edges3 FOR c FROM 0 TO n - 14 dist[c] <- -15 dist[source] <- 06 queue <- [source]7 WHILE queue NOT EMPTY8 u <- REMOVE FIRST FROM queue9 IF u = dest10 RETURN dist[u]11 FOR EACH v IN adjacency[u]12 IF dist[v] = -113 dist[v] <- dist[u] + 114 APPEND v TO queue15 RETURN dist[dest]
← / → step · space play · Home restart
Every step in words
Show all 15 steps
- 6 nodes, every edge worth 1 step. Find node 0's shortest distance to node 5. With every edge the same length, BFS visits nodes in strict order of distance from the source, so the first time 5 comes out of the queue, that distance is final.
- Every distance starts unknown except the source itself, which is 0 steps from itself. Queue holds just 0.
- Dequeue 0 (dist 0). Look at its neighbours 1, 2 for any that have never been reached.
- 1 gets dist 1, 2 gets dist 1 — first time seeing them through 0, so they're queued with parent 0.
- Dequeue 1 (dist 1). Look at its neighbours 0, 3 for any that have never been reached.
- 3 gets dist 2 — first time seeing it through 1, so it's queued with parent 1.
- Dequeue 2 (dist 1). Look at its neighbours 0, 3 for any that have never been reached.
- Every neighbour of 2 already has a distance, so nothing changes and nothing new is queued.
- Dequeue 3 (dist 2). Look at its neighbours 1, 2, 4 for any that have never been reached.
- 4 gets dist 3 — first time seeing it through 3, so it's queued with parent 3.
- Dequeue 4 (dist 3). Look at its neighbours 3, 5 for any that have never been reached.
- 5 gets dist 4 — first time seeing it through 4, so it's queued with parent 4.
- Dequeue 5 (dist 4). Look at its neighbours 4 for any that have never been reached.
- 5 IS the destination, and BFS only reaches it once, at its true shortest distance. No point scanning the rest of the queue — stop here.
- Walking parent pointers back from 5 to 0 gives 0 -> 1 -> 3 -> 4 -> 5, 4 edges long. RETURN dist[5] = 4.
Now solve it yourself
Watching is the first half. The second is writing it without the animation.