A comprehensive guide to the depth-first search (DFS) pattern for traversing trees and graphs. Covers the core recursive DFS pattern, finding paths in trees, handling cycles in graphs using a visited set, finding all paths with backtracking, and treating matrices as implicit graphs. Includes Python code examples for each variant and guidance on when to apply DFS versus BFS. Concludes a six-part series on recursion patterns.
Table of contents
The Core PatternFinding PathsHandling CyclesFinding All PathsMatrices as Implicit GraphsWhen DFS AppliesConclusion22.3K Impressions