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.
Multi-source BFS is a small extension of standard single-source BFS. The algorithm is identical. The only change 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. The result is the shortest distance from each cell to its nearest source, not to a specific 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.
A common setup is a grid where some cells are marked as "sources" and the goal is 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 million operations.
All sources are at distance 0. If you put them all into the queue at the start, BFS expands 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. A single pass produces the answer for every cell.
This brings the time complexity down to O(m * n), the same as a single BFS. Every cell is visited exactly once, regardless of how many sources there are.
A useful visual is multiple stones dropped into a pond at the same time. The ripples from each stone expand outward simultaneously. Where two sets of ripples meet, the boundary represents cells equidistant from two sources. Multi-source BFS computes that entire distance field 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.
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.
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.
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. Running BFS from each source separately and taking the per-cell minimum would produce the same final answer, but it would cost O(k m n) instead of O(m * n). Multi-source BFS speeds up the computation without changing the result.
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.
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.
Overall: O(m * n)
The table below compares multi-source BFS against alternative approaches.
| 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 can spot the pattern, the implementation follows the same template each time.
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 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.
10 quizzes