← Course home

Problem-solving methods · Lesson 43 of 52

Backtracking search

Explore choice trees with reversible state, safe pruning and explicit search costs.

Full lessonAbout 10 min

You will learn to trace choose, recurse and undo and estimate search-tree size and justify pruning.

Recommended first: Recursion and recurrences.

On this page 6 sections

The idea

permutations of 1, 2, 3 returns 6 lists.

Backtracking searches a tree of choices. You choose, recurse, and undo so the next choice sees the earlier state. permutations of 1, 2, 3 returns six lists, one for every order. The first position has three choices. The second has two remaining items. The third has one. The product is 6, and the function materializes each list. The undo step puts the chosen item back into the pool before the next sibling is tried. Without the undo, the later branches would see a shrinking pool that is not restored. n_queens_count uses the same shape. A queen is placed, the search goes to the next row, and the placement is removed on the way back. Pruning skips a placement that already shares a column or a diagonal with a queen.

A worked trace

StepStateWhat changed
Leafcount = 1Leaf 1 is [1, 2, 3].
Leafcount = 2Leaf 2 is [1, 3, 2].
Leafcount = 3Leaf 3 is [2, 1, 3].
Leafcount = 4Leaf 4 is [2, 3, 1].
Leafcount = 5Leaf 5 is [3, 1, 2].
Leafcount = 6Leaf 6 is [3, 2, 1].

Why it is correct

The state invariant is that the pool, or the board. Matches the choices on the path from the root to the current call, and nothing else. Choose extends that path. Recurse searches below it. Undo returns to the parent path. A leaf is a complete choice, and it is recorded before the undo. Every permutation appears once because each item is chosen into each position from the items not yet used. The search tree is exhaustive unless a prune deletes a branch. A prune is correct only when every leaf under that branch would fail the same test.

A common mistake

Skipping the undo leaves later siblings with a pool that was not restored.

What it costs

The permutation tree has n! leaves, and building each list copies n items, so the time is proportional to n times n!. Extra memory is the current path, which is n, plus the output. Pruning does not change the worst-case shape. It changes which branches you actually enter. Estimate the tree before you code the search. If the unpruned tree is already enormous, a different state is the real fix. The permutation tree is Θ(n!) leaves, and the path uses Θ(n) extra memory.

Run it

python
from bitsandbytes.backtracking.search import permutationsprint(len(permutations([1, 2, 3])))# prints: 6 

Practice and next step

Open Greedy methods next. Greedy methods make one local choice and justify it with an exchange argument.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.backtracking.search

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Count the permutations of three items

permutations of 1, 2, 3 returns six lists. Each call chooses, recurses, and undoes.

count
1

State

  1. 01done
  2. 12done
  3. 23done

Leaf 1 is [1, 2, 3].

Practice with feedback

Check your understanding

Try each checkpoint before revealing the feedback. Ask for a hint when you need one. Answers and hints stay in this browser when local storage is available.

1A permutation search places an item and then tries the next sibling. What must happen between those siblings?
2How many leaves does the permutation tree of n distinct items have?

Progress is never sent to an account or synced to another device. If local storage is blocked, choices last only until you leave this page.