Skip to content
DSA Visualizerpreview

Shortest path on a grid (BFS)

BFS on a grid: distances spread out in waves from S, and the first wave to touch E gives the shortest path.

O(rows × cols) time · O(rows × cols) space

GridS..#...., .#.#.##., .#...#.., .####.#., ......#E

Start at S with distance 0. Walls (█) block the way; moves go up, down, left and right.

Grid 8 by 5; 1 cells reached so far.
  • compare
  • swap or write
  • settled

1 / 26

Speed

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