My first instinct was to run BFS from each source separately and merge results, which would've been way slower.
Recognize this as a multi-source shortest path problem on an unweighted graph and solve it with a multi-source BFS. Initialize a queue with all source nodes at distance 0, then perform BFS to propagate distances to all reachable nodes. This runs in O(V + E) time, which is optimal for unweighted graphs.
Pro tip: Mention that multi-source BFS is equivalent to adding a super-source connected to all sources with zero-weight edges, which elegantly handles the problem. Also note that if the graph is disconnected, unreachable nodes should be marked as infinity or -1.
Confirm that the graph is undirected and unweighted, and ask about the number of nodes, edges, and sources. Clarify how to represent unreachable nodes (e.g., -1 or infinity).
Select multi-source BFS because it efficiently computes shortest distances from multiple sources in O(V+E) time. Explain why Dijkstra or repeated BFS from each source would be less efficient.
Create a distance array initialized to infinity (or -1) and a queue. Set distance of each source to 0 and enqueue all sources.
While the queue is not empty, dequeue a node, and for each unvisited neighbor, set its distance to current distance + 1 and enqueue it.
Return the distance array. Analyze time complexity O(V+E) and space complexity O(V), and discuss handling of disconnected components.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.