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
- 1Graphs, before the theory9 minNodes, edges, adjacency lists, and why a grid is already a graph.
- 2BFS: search in ripples12 minA queue, a visited set, and levels that double as shortest-path distances.
- 3DFS: go deep, then back up11 minRecursion as a path-walker, backtracking, and the visited set that stops infinite loops.
- 4Grids and islands11 minFlood fill in practice: counting islands, the directions array, and marking as you go.
- 5BFS or DFS?8 minShortest path says BFS, exhaustive exploration says either. A decision guide with the traps.
Practice it after
Real interview questions that test this pattern, in an editor with execution and AI feedback.