liveBook · Manning

Depth-first search

As explained in Optimization Algorithms: AI techniques for design, planning, and control problems

Depth-first search explores a path as deeply as possible before backtracking to an alternative path.

Depth-first search (DFS) explores one branch as deeply as possible, then backtracks to the latest decision point and tries another branch. It is a blind graph-search method: it uses no information about the search space and stops when it finds a destination or exhausts the paths. section 3.2 section 3.6

A stack drives the search:

  • Initialize a last-in, first-out stack with the source node, an empty explored collection, and an unsuccessful-found state. section 3.3.2
  • Pop the newest node, mark it explored, and expand its successors. section 3.3.2
  • Prefer DFS when the graph is wide, adjacent nodes are numerous, and no path is excessively deep; avoid it when a shallow solution is likely in a very deep graph. section 3.3.2

Solving an 8-puzzle

DFS can search an 8-puzzle by treating blank-tile moves as successors. With a depth bound of five, dead ends become terminal nodes and the search backtracks to earlier alternatives. A Python run reports process time, memory, explored states, and solution steps. section 3.3.2

How Optimization Algorithms: AI techniques for design, planning, and control problems covers it

How deep it goes
Explains recursive backtracking, LIFO implementation, depth bounds, trade-offs, and a complete 8-puzzle procedure.
Examples
Demonstrates DFS on an 8-puzzle and contrasts its behavior with BFS through complexity and preference conditions.
What it assumes
Assumes graph nodes, successors, routes, and stacks, while defining DFS through a small 8-puzzle search.
What you can do after
Apply it via a stack-based implementation to search graphs and solve state-space problems while tracking routes and explored states.
How it connects
Connects DFS with graph traversal, backtracking, breadth-first search, depth-limited search, and iterative deepening search.

Also explained in

Compare how 7 books explain depth-first search →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Optimization Algorithms: AI techniques for design, planning, and control problems. The section links point to the parts of the book it draws on.