Last Updated: April 5, 2026
Multi-source BFS is a variation of breadth-first search where you start from multiple sources at once instead of just one. All sources are added to the queue initially, and the search expands outward in layers, ensuring the shortest distance to each node or cell.
If you already understand single-source BFS from the previous chapter, multi-source BFS is a small but powerful extension. The algorithm is identical. The only thing that changes is the initialization.
In standard BFS, you start from a single source node, add it to a queue, and explore its neighbors level by level. The FIFO queue guarantees that all nodes at distance d are processed before any node at distance d+1. This gives you shortest paths from that single source.
Multi-source BFS is the same algorithm, but instead of putting one node into the queue at the start, you put all source nodes into the queue at level 0. From there, BFS expands outward from all sources simultaneously, level by level, just as it always does.
There is a useful mental model for understanding why this works. Imagine you create a "virtual super-source" node and connect it to every real source with a zero-weight edge. Running single-source BFS from this virtual node would first visit all real sources (at distance 0), and then expand outward from all of them. Multi-source BFS skips the virtual node and just places all sources directly at level 0. The result is exactly the same.
This means multi-source BFS finds the shortest distance from each cell to its nearest source. Not to a specific source, but to whichever source is closest. That is the key distinction from running BFS individually from each source.
These all share the same structure: multiple origins, simultaneous expansion, and the need to know the shortest distance from each point to its nearest origin.
Suppose you have a grid, and some cells are marked as "sources." You want to find the shortest distance from every cell to its nearest source.
The naive approach is to run a BFS from each source independently, computing the distance from that source to every cell, and then take the minimum across all sources. If there are k sources and the grid has mn cells, that costs O(k m * n). For a 1000x1000 grid with 500 sources, that is 500 billion operations. Far too slow.
All sources are at distance 0. If you put them all into the queue at the start, BFS will naturally expand outward from all of them at the same time. Level 1 contains all cells that are adjacent to any source. Level 2 contains all cells that are two steps from their nearest source. And so on.
Because BFS is FIFO and processes nodes in the order they were enqueued, the first time any cell is visited, it has been reached by the nearest source, via the shortest path. No need to run multiple BFS passes. One pass does everything.
This brings the time complexity down to O(m * n), same as a single BFS. You visit every cell exactly once, regardless of how many sources there are.
Think of it like dropping multiple stones into a pond at the same time. The ripples from each stone expand outward simultaneously. Where two sets of ripples meet, the boundary between them represents cells equidistant from two sources. Multi-source BFS computes the distance field for all of this in a single pass.
The diagram below shows a grid with three sources (S). The colors represent the BFS level at which each cell is first reached.
Each level expands outward from all sources simultaneously. A cell at level 2 means its nearest source is exactly 2 steps away.
The algorithm has three phases. The first is unique to multi-source BFS. The second and third are identical to standard BFS.
That is it. The only difference from single-source BFS is step 1: instead of enqueuing one node, you enqueue all sources.
Let us trace through a concrete example to see multi-source BFS in action.
We have a 4x4 grid. The value 0 means empty, 1 means fresh orange, and 2 means rotten orange.
There are two rotten oranges: at (0,0) and (3,3). There are 8 fresh oranges. We need to find the minimum number of minutes until all fresh oranges are rotten.
Notice how both rotten oranges spread simultaneously. By minute 2, the expanding "rot zones" from the two sources have almost met in the middle. By minute 3, every fresh orange has been reached. If we had run BFS from each source separately, we would have gotten a different (incorrect) result, because the spread from (3,3) and (0,0) does not happen sequentially.
Here is a generic multi-source BFS implementation for grid problems. The function takes a grid, identifies all sources, and computes the shortest distance from each cell to its nearest source.
The structure across all implementations is identical:
Common Mistakes:
Overall: O(m * n) for a grid, or O(V + E) for a general graph.
Here is why:
The number of sources does not affect the time complexity. Whether you have 1 source or 10,000 sources, the BFS still visits each cell exactly once. This is the entire point of multi-source BFS: it collapses what would be O(k m n) with separate BFS passes into O(m * n).
Overall: O(m * n)
How does multi-source BFS stack up against alternative approaches? Here is a side-by-side comparison.
| Approach | Time Complexity | Space Complexity | Correctness | When to Use |
|---|---|---|---|---|
| Multi-source BFS | O(V + E) | O(V) | Correct for unweighted graphs | Multiple sources, uniform edge weights, simultaneous expansion |
| Separate BFS from each source | O(k * (V + E)) | O(V) | Correct but slow | Very few sources (k is small), or when you need per-source distances |
| Dijkstra from virtual super-source | O((V + E) log V) | O(V) | Correct for weighted graphs | Sources at different "starting costs," weighted edges |
| BFS from single source | O(V + E) | O(V) | Wrong for multi-source problems | Only one source exists |
Multi-source BFS appears in several recognizable patterns in interview problems. Once you learn to spot these, the implementation is almost mechanical.
Problem type: Multiple entities spread to adjacent cells each time step. Find how long until everything is reached (or whether it is possible).
How multi-source BFS helps: Enqueue all spreading entities at level 0. Each BFS level represents one time step. The answer is the maximum level reached (or -1 if some cells are unreachable).
Examples: Rotting Oranges (LC 994), where rotten oranges spread rot. Shortest Bridge (LC 934), where you find all cells of one island, then BFS-expand toward the other island.
Problem type: Given a grid with some special cells, compute the shortest distance from every cell to its nearest special cell.
How multi-source BFS helps: Enqueue all special cells as sources. BFS naturally computes the shortest distance from each cell to whichever source is closest.
Examples: 01 Matrix (LC 542), where you find the distance from each cell to the nearest 0. Walls and Gates (LC 286), where you find the distance from each empty room to the nearest gate. Map of Highest Peak (LC 1765), where you assign heights based on distance from water cells.
Problem type: Start from all boundary cells and expand inward.
How multi-source BFS helps: Enqueue all boundary cells as sources. BFS expands inward, and you can use the distance values or the visited set for further processing.
Examples: Surrounded Regions (LC 130), where you mark all O cells reachable from the boundary. Pacific Atlantic Water Flow (LC 417), where you expand from ocean boundaries inward.