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

Lessons

  1. 1Graphs, before the theory9 minNodes, edges, adjacency lists, and why a grid is already a graph.
  2. 2BFS: search in ripples12 minA queue, a visited set, and levels that double as shortest-path distances.
  3. 3DFS: go deep, then back up11 minRecursion as a path-walker, backtracking, and the visited set that stops infinite loops.
  4. 4Grids and islands11 minFlood fill in practice: counting islands, the directions array, and marking as you go.
  5. 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.