DFS
Follow one path as far as it goes, then back up and try the next: the simplest way to visit, count and classify everything reachable.
After this lesson you will be able to
- Explain how DFS differs from BFS and why recursion (the call stack) gives depth-first order.
- Write DFS recursively and iteratively with an explicit stack, and know when to prefer each.
- Count connected components and flood-fill a grid.
- Detect a cycle in an undirected graph using the parent trick.
- Explain why DFS does not find shortest paths, and what it is good for instead.
Before you start. Recursion and BFS will help: this lesson reuses the “seen” idea from BFS and relies on how recursive calls stack up.