BFS & DFS
Two ways to walk a graph: ripple outward level by level, or dive deep and backtrack.
5 lessons · ~51 min · free
By the end you can
- Represent graphs with adjacency lists and treat grids as graphs for free
- Run BFS with a queue and read shortest paths straight off the levels
- Run DFS with recursion, and know what the call stack is doing for you
- Count islands and flood-fill regions on grids
- Choose BFS or DFS from the question, not from habit
Lessons
Start the course- Graphs, before the theory9 minNodes, edges, adjacency lists, and why a grid is already a graph.
- BFS: search in ripples12 minA queue, a visited set, and levels that double as shortest-path distances.
- DFS: go deep, then back up11 minRecursion as a path-walker, backtracking, and the visited set that stops infinite loops.
- Grids and islands11 minFlood fill in practice: counting islands, the directions array, and marking as you go.
- BFS or DFS?8 minShortest path says BFS, exhaustive exploration says either. A decision guide with the traps.
The account is free and so is every lesson. It unlocks the courses and saves your progress.
Practice it after
The final lesson links here too: real interview questions that test this pattern, in an editor with execution and AI feedback.