Skip to content

BFS and DFS Graph Traversal
Step by Step

Same loop, different container. Queues visit the graph in layers and stacks dive down one path before backing up.

Step through every visit at your own pace, then share a link to the step where the order surprised you.

What You Watch While the Frontier Moves

The container, the frontier and the seen nodes change on every step, and the visualiser tracks them for you.

Two traversals share one loop, which is why they blur together on step nine, when the container, the seen set and the current node all change.

CodeStory's visualiser plays BFS and DFS on a graph you choose and highlights the code line being executed. Step forward, step back or let it play.

Try the With a cycle preset first. Then switch on the mark when taken out bug and watch a node get queued twice.

How BFS and DFS Visit Every Node

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

  1. 1Start from a source and mark it seen.
  2. 2Take the next node from the frontier, where a queue spreads in layers and a stack goes deep.
  3. 3Look at its neighbours and add the ones you have not seen.
  4. 4Skip seen nodes, so every edge is examined once.
  5. 5Stop when the frontier is empty.

Where Graph Traversal Goes Wrong

One switch lives in the visualiser. Flip it on and watch a node get visited twice.

  • Bug: mark when taken outMarks a node only when it leaves the queue, so two neighbours can both queue it. In DFS the same idea without the "already seen?" check visits a node twice. Try "With a cycle".

Graph Traversal Cost in Time and Space

Interviewers ask why BFS and DFS cost V plus E, and the visualiser counts real operations.

Time
O(V + E)counted from the real visits on your graph
Space
O(V + E)extra memory for the seen set and the frontier
Pattern
GraphsOne loop with two containers covers every traversal.

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 and DFS Visualiser FAQs

Open the visualiser and watch the answers play out.

Open the visualiser
What does the BFS and DFS visualiser show?

CodeStory's visualiser plays BFS and DFS on a graph you choose and highlights the line of code being executed. You can step forward, step back or let it play.

What is the time complexity of BFS and DFS?

BFS and DFS both take O(V + E) time and O(V + E) space. CodeStory's visualiser counts real operations as it plays, so you watch the numbers instead of trusting the formula.

What is the difference between BFS and DFS?

BFS uses a queue and visits a graph in layers. DFS uses a stack and dives down one path before backing up.

Why does my BFS or DFS code visit a node twice?

Marking a node only when it leaves the queue lets two neighbours queue it twice, and DFS without the already seen check visits a node twice. CodeStory's visualiser ships this as the mark when taken out switch, so you flip it on and watch it happen.

Can I use my own input?

Yes. Type your own graph and the visualiser plays BFS or DFS 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 Graph Traversal? Get a Free Callback

Tell CodeStory where graph traversal loses you, and a mentor plans your next steps.

  1. 1CodeStory finds your starting point, from your first visit 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.