liveBook · Manning

Backtracking

As explained in Object-Oriented Software Design in C++

A backtracking technique that recursively explores alternative solution paths and reverses choices when a path fails.

Backtracking recursively explores alternative solution paths. When a recursive attempt reaches a dead end, it reverses the latest choice and tries another path, continuing upward when necessary. section 15.8 section 15.11

A typical backtracking search proceeds as follows:

  • Choose an available option at the current step and record the choice. section 15.10 section 15.9
  • Recursively solve the smaller remaining problem. section 15.8
  • Return success when the base case confirms a solution; otherwise undo the choice and try the next option. section 15.10 section 15.9
  • Backtrack to the previous step after exhausting every option at the current step. section 15.11

Sudoku and eight queens

A Sudoku solver visits cells in order, tries numbers allowed by the row, column, and block, and recursively tests the next cell. Failed numbers are replaced; if every number fails, the solver returns to the previous cell. An eight-queens solver similarly places a safe queen in each column, recursively searches the next column, and removes the queen before trying another row. section 15.10 section 15.9

Figure 2.1 A design decision tree and the application development path shown as the dotted line. Each tree node denotes a juncture during development when we must decide to choose a path (i.e., branch) on which to continue. While designing and coding, if we head down a branch that leads to a dead-end (the lumps of coal), we must backtrack to the previous decision node and try another branch. After trying all the branches at a node, we must backtrack to the next higher node and try the next branch from there. We hope to find the path to a successfully completed application (the pot of gold).

A decision-tree figure makes branching, dead ends, and returning to earlier decision points visible. section 2.1

How Object-Oriented Software Design in C++ covers it

How deep it goes
Explains base cases, recursive subproblems, choice reversal, and path exhaustion, while warning that misuse can cause performance problems.
Examples
Demonstrates candidate replacement in Sudoku and queen placement, removal, and repositioning in the eight queens puzzle.
What it assumes
Assumes basic programming while teaching recursion through smaller problems, base cases, and recursive returns.
What you can do after
Apply it via recursive search to solve constraint problems with multiple candidate paths and recover from failed choices.
How it connects
Connects recursion with base cases, brute-force search, Sudoku, and the eight queens puzzle.

Also explained in

Compare how 6 books explain backtracking →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.