Skip to content
DSA Visualizerpreview

Breadth-first search

Explore in rings of distance with a queue: the first time you reach a node is by a shortest route.

O(V + E) time · O(V) space

EdgesA-B, A-C, B-D, C-D, C-E, D-F, E-Ffrom A

Start at A (distance 0). The queue holds the nodes waiting to be explored.

Graph with nodes A, B, C, D, E, F and edges A–B, A–C, B–D, C–D, C–E, D–F, E–F.
  • compare
  • swap or write
  • settled

1 / 22

Speed

Space play · ← → step · Home End jump · drag the strip to scrub