DFS explores as far as possible along each branch before backtracking. It dives deep into one path before trying others.
Analogy: Imagine exploring a maze by always taking the first unexplored path you see, going deep until you hit a dead end, then backtracking to try other paths.
for (const neighbor of neighbors) { if (!visited.has(neighbor)) { dfsHelper(neighbor); } }
recursively visit each unvisited neighbor to explore deeply
OutputSuccess
2 -> 0 -> 1 -> 3
Complexity Analysis
Time: O(V + E) because DFS visits every vertex and edge once
Space: O(V) for the visited set and recursion stack in worst case
vs Alternative: Compared to BFS which uses a queue and explores level by level, DFS uses recursion and explores depth first; both have similar time but different traversal orders
Edge Cases
Graph with no edges (isolated nodes)
DFS visits only the start node since no neighbors exist
When a problem requires exploring all connected parts of a graph deeply before backtracking, use DFS to traverse paths fully before trying alternatives.
Common Mistakes
Mistake: Not marking nodes as visited before recursive calls, causing infinite loops on cycles Fix: Mark nodes as visited immediately when first visiting them to prevent revisiting
Summary
DFS visits nodes by going deep along each path before backtracking.
Use DFS when you want to explore all nodes reachable from a start point deeply.
The key is to mark nodes visited as soon as you see them to avoid cycles.