Skip to main content

DFS

Learn DFS (Graphs, Advanced DSA) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

Depth-First Search - Go Deep Before Backtracking DFS explores as far as possible down one path before backtracking, using a STACK (or recursion, which uses the call stack implicitly) - the opposite exploration order from BFS's queue-based level-by-level approach. A visited set is essential to avoid infinite loops in graphs with cycles.

More lessons in Graphs