Problem. A grid holds 0 (empty), 1 (fresh orange) and 2 (rotten orange). Every minute, each fresh orange that is
4-directionally adjacent to a rotten one becomes rotten. Return the minimum number of minutes until no fresh orange
remains, or −1 if some fresh orange can never rot.
2 1 1 minute 1: 2 2 1 minute 2: 2 2 2 minute 3: 2 2 2 minute 4: 2 2 2
1 1 0 2 1 0 2 2 0 2 2 0 2 2 0
0 1 1 0 1 1 0 1 1 0 2 1 0 2 2 → 4
The rot spreads one step per minute from every rotten orange simultaneously. That's BFS with many sources: put all of them in the queue at time 0, and each BFS level is one minute.
ML context. Spreading processes on graphs: how many hops until a message (or a label, in label propagation) reaches every node, how fast a model update rolls out through a fleet where each upgraded server upgrades its neighbours, the distance from each pixel to the nearest edge or annotated point (a distance transform, used for weighting losses near boundaries). All are multi-source BFS (Changes B and D).