101 Logo
onenoughtone

The Four Core Patterns of DFS

Understanding DFS Patterns

Depth-First Search can be applied to a wide variety of problems, but most of them follow one of these four core patterns.

Each pattern represents a different way of thinking about and applying DFS to solve problems.

Backtracking Pattern

Solve problems by trying different possibilities and backtracking when a solution is not feasible.

Cycle Detection Pattern

Detect cycles in a graph by keeping track of vertices in the current recursion stack.

Topological Sort Pattern

Order vertices in a directed acyclic graph such that for every directed edge (u, v), vertex u comes before v.

Graph Traversal Pattern

Explore all vertices and edges in a graph, useful for finding paths, connected components, and more.

IntroVisualizePatternsPractice
101 Logo
onenoughtone