Depth First Search

Depth First Search (DFS). We go as deep as we can to look for a value, and when there is nothing new to discover, we retrace our steps to find something new.
Backtracking and DFS are similar concepts and essentially the same thing since in DFS you always "backtrack" after exploring a deeper node.

When to use DFS

Tree

DFS is essentially Pre-order Traversal.

Combinatorial problems

DFS/backtracking and combinatorial problems are a match made in heaven (or a silver bullet and a werewolf 😅). As we will see in the Combinatorial Search module, combinatorial search problems boil down to searching in trees.

Graph

Trees are special graphs that have no cycle. We can still use DFS in graphs with cycles. We just have to record the nodes we have visited and avoid re-visiting them and going into an infinite loop.