DSAVisualiseData Structures & Algorithms Studio
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 JSON

Edit the lesson input, then build a deterministic source-linked trace.

This lesson validates its own input and operation limits.

Source scenarios

Complexity

BestΘ(V + E)
AverageΘ(V + E)
WorstΘ(V + E)
SpaceΘ(V)

Array state

Follow the active comparisons and writes.

Live snapshot

Current operation

Trace state
Ready 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

Java
BreadthFirstShortestPaths.javaStep linked
The highlighted line follows the current step.

Ready to begin

Press Play to see what happens.

Detailed execution
Snapshot 1 / 1