Skip to content

BFS Shortest Path in a Grid
Step by Step

Spread out from the start one ring at a time with a queue, and the first time you reach a cell is by a shortest route.

Step through every ring at your own pace, then share a link to the step where you got lost.

What You Watch While Rings Spread

The queue, the rings and every cell's distance change on every step, and the visualiser tracks them for you.

Holding the queue in your head works for a small grid. By step nine, which cells wait in the queue and which are done blur together.

CodeStory's visualiser spreads the rings across your grid, cell by cell. Code lines stay highlighted beside the grid. Step forward, step back or let it play.

Try a grid where the start sits in a corner, then one where it sits in the middle. Compare how the rings grow and where the shortest path ends.

How BFS Finds the Shortest Path

Steps below match the picture, so you can follow them beside the grid.

  1. 1Put the start cell in the queue and mark it seen.
  2. 2Take the cell at the front of the queue.
  3. 3Add every unseen neighbour to the back of the queue, one step farther from the start.
  4. 4Never revisit a cell, so every cell is examined once.
  5. 5The first time you reach a cell is a shortest route to it.

BFS Grid Cost in Time and Space

Interviewers ask why grid BFS costs rows times cols, and the visualiser counts real operations.

Time
O(rows × cols)counted from the real steps on your grid
Space
O(rows × cols)extra memory for the queue and the seen cells
Pattern
GraphsQueues spread in layers, so first arrival means shortest distance.

Everything in the App in One Place

Practice, interviews and tools all start free. Swipe to see the rest.

Sign up for 100 AI credits now and get 100 more every month. Start free in the app

BFS Grid Visualiser FAQs

Open the visualiser and watch the answers play out.

Open the visualiser
What does the BFS grid visualiser show?

CodeStory's visualiser spreads from your start cell one ring at a time and plays every step on a grid you choose. It highlights the line of code being executed, and you can step forward, step back or let it play.

What is the time complexity of BFS on a grid?

BFS on a grid takes O(rows × cols) time and O(rows × cols) space. CodeStory's visualiser counts real operations as it plays, so you watch the numbers instead of trusting the formula.

Why does BFS find the shortest path on a grid?

BFS reaches cells in rings of equal distance, so the first time you reach a cell is by a shortest route.

Can I use my own input?

Yes. Type your own grid and the visualiser plays BFS on it, edge cases included.

Is the visualiser free?

Yes. You start free with 100 AI credits when you sign up and 100 every month, and watching an algorithm costs no credits.

Stuck on Grid Problems? Get a Free Callback

Tell CodeStory which grid problems stump you, and a mentor plans your next steps.

  1. 1CodeStory finds your starting point, from your first queue to interview ready.
  2. 2CodeStory explains online and campus batches, with timings for your mornings or evenings.
  3. 3You decide. No pressure, no payment on the call.

Get a free callback

I am a
I want to learn

Your number is only used for a CodeStory callback.