A Bit of History
Multi-source BFS is a direct extension of Edward F. Moore's 1959 breadth-first search — the innovation is not a new traversal rule but a new way of thinking about the "start" of the search. The technique became especially popular through competitive programming communities in the 2000s and 2010s as a standard pattern for "nearest facility" style problems, and is formally equivalent to adding a single virtual super-source vertex connected to every real source with zero-weight edges — a trick with roots in classical network-flow reductions (Ford & Fulkerson, 1956) where multiple sources/sinks are similarly unified into one.
Working Principle: The Virtual Super-Source
Rather than running $k$ separate BFS traversals (one per source) and taking a pointwise minimum — costing $O(k \cdot (V + E))$ — multi-source BFS achieves the same result in a single $O(V + E)$ pass:
- Multi-Source Initialization: Initialize the BFS queue with all source vertices simultaneously, each tagged with distance 0. Mark all initial sources as visited immediately.
- Unified Queue Traversal: Proceed with standard BFS exactly as usual — pop from the front, inspect unvisited neighbors, assign
dist[neighbor] = dist[u] + 1, and push to the back. - Earliest Arrival Guarantee: Because every source starts at distance 0 and BFS explores level-by-level, the first time any vertex $v$ is reached, it is guaranteed to be reached via the closest source.
Why This Works: The Super-Source Reduction
This is mathematically identical to adding one new virtual vertex $s^*$ connected by zero-weight directed edges to every real source $s_i \in S$, then running ordinary single-source BFS from $s^*$. Since $s^*$'s neighbors all start at distance 1 from $s^*$ (which maps to distance 0 from the original sources), seeding the queue directly with all sources at distance 0 is completely equivalent — without allocating an extra vertex or modifying edge lists.
Worked Example & SVGs
Consider the classic Rotten Oranges problem on a $3 \times 3$ grid:
- Grid values:
2= Rotten (Source),1= Fresh,0= Empty. - Initial state:
[ [2, 1, 1], [1, 1, 0], [0, 1, 2] ]with sources at $(0, 0)$ and $(2, 2)$.
Mathematical Formulation
Let $G = (V, E)$ be an unweighted graph, and let $S \subseteq V$ be the non-empty set of source vertices. The distance to the nearest source $\text{dist}(v, S)$ for any vertex $v \in V$ is defined as:
where $d(s, v)$ denotes the standard unweighted shortest path distance (number of edges) from $s$ to $v$. Multi-source BFS guarantees that when a vertex $v$ is popped from the queue, its assigned distance equals $\text{dist}(v, S)$.
Complexity Analysis
Let $V$ be the number of vertices and $E$ be the number of edges:
Notice that the number of source vertices $k = |S|$ does not affect the asymptotic time complexity! Running $k$ independent single-source BFS passes would require $O(k \cdot (V + E))$ time, whereas multi-source BFS performs the exact same computation in a single linear pass.
| Approach | Queue Seeding | Time Complexity | Space Complexity |
|---|---|---|---|
| Naive Repeats | Run single-source BFS $k$ times | $O(k \cdot (V + E))$ | $O(V)$ |
| Multi-Source BFS | Seed queue with all $k$ sources at $t=0$ | $O(V + E)$ | $O(V)$ |
| Super-Source Reduction | Add $s^*$, add $k$ zero-weight edges | $O((V+1) + (E+k)) = O(V + E)$ | $O(V + k)$ |
Implementation
Below are production-ready multi-source BFS implementations in Python, C++, and Java:
Real-World Applications
1. Nearest Emergency Facility Location
City GIS mapping engines compute the shortest distance to the nearest hospital or fire station for every residential block. By seeding the queue with all facility nodes, a single multi-source BFS computes distances for the entire metropolitan area in $O(V + E)$ time.
2. Wildfire & Disease Outbreak Propagation
In environmental and epidemiological modeling, multiple initial ignition or infection points expand simultaneously across terrain grids. Multi-source BFS computes the exact arrival time of the wave front at every location.
Exercises
Practice Problems
- Solve the "Rotten Oranges" problem on a $5 \times 5$ grid with 3 initially-rotten cells scattered around the border.
- Prove formally that multi-source BFS produces the same result as running single-source BFS from each source and taking the pointwise minimum.
- Modify the algorithm to return an array
nearest_source[]recording which specific source is closest to each vertex.
nearest_source[v] = nearest_source[u] when relaxing edges.Limitations
Unweighted Graphs Only
Plain queue-based multi-source BFS is valid only on unweighted graphs or uniform-cost grid graphs. For weighted graphs with varying edge costs, replacing the queue with a Priority Queue yields multi-source Dijkstra's algorithm.