Graphs & Connectivity Hard
Multi-Source BFS
Breadth-first search grows one wavefront from every source, so distance is the fewest edges from the nearest source.
Trace input
Bounded JSONEdit the lesson input, then build a deterministic source-linked trace.
Source scenarios
Complexity
BestΘ(V + E)
AverageΘ(V + E)
WorstΘ(V + E)
SpaceΘ(V)
Array state
Follow the active comparisons and writes.
Current operation
Trace stateReady to explore
Source behavior & common mistakes
Return each vertex’s shortest unweighted distance, preserving Java’s Integer.MAX_VALUE for unreachable vertices.
- Running a separate search for every source.
- Replacing the source’s Integer.MAX_VALUE sentinel with -1.
Algorithm code
JavaBreadthFirstShortestPaths.javaStep linked
The highlighted line follows the current step.
Ready to begin
Press Play to see what happens.
Detailed execution
Snapshot 1 / 1