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.
- 1Start from a source and mark it seen.
- 2Take the next node from the frontier, where a queue spreads in layers and a stack goes deep.
- 3Look at its neighbours and add the ones you have not seen.
- 4Skip seen nodes, so every edge is examined once.
- 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.
Take BFS and DFS Into Problems
Watching shows the visit order and solving makes it automatic.
Everything in the App in One Place
Practice, interviews and tools all start free. Swipe to see the rest.
DSA practice
Problems filed under the pattern that solves them, not a sheet.
352 problemsOpenCompany sets
Practise what one company asks in the week before your round.
463 companiesOpenCoding interview
Choose a role, write in a real editor and answer while you type.
scored reportOpenMock interviews, spoken
You answer out loud and CodeStory follows up on what you said.
32 topicsOpenDatabase round
Answer DBMS the way a panel asks it, with indexes, joins and normal forms.
in Core CSOpenOOP round
Inheritance, polymorphism and the follow-up you never rehearse.
in Core CSOpenSystem design
How real systems are built, from the primitives to the AI stack.
27 chaptersOpenSQL practice
Real schemas, your query, the rows it returns and a clear verdict.
39 questionsOpenAptitude
Quant, logical, verbal, DI and technical, timed like the real test.
280 questionsOpenOnline assessment
Get a clock, marks per question and hidden tests like a company's judge.
37 papersOpenAlgorithm visualiser
Watch it play on your own input, one step at a time.
26 algorithmsOpenAI that helps, not solves
Hints one at a time and AI review before a mentor sees your work.
100 free creditsOpenResume builder
Build it inside the app, next to the work you practised.
in the appOpenProgress and readiness
One score and the axis where you are weakest.
tracked dailyOpen
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 visualiserWhat 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.
- 1CodeStory finds your starting point, from your first visit to interview ready.
- 2CodeStory explains online and campus batches, with timings for your mornings or evenings.
- 3You decide. No pressure, no payment on the call.