Backtracking
Write a recursive function that generates all subsets or all permutations by choosing, exploring and undoing, and state how many results and calls it makes.
It opens on
This function lists every subset of [1, 2, 3], adding an item to cur, calling itself, then removing the item. How many subsets does it list in all, and how many for 10 items?
Why it matters
Search problems such as filling a sudoku, finding a route that visits every town or checking every way to schedule exams are solved by trying a choice, going on, and undoing it when it fails, and the same pattern also shows when a search is too big to finish.
Good to know first
- Recursion and the call stack: each call is a frame and a list of n items gives n + 1 frames
- Recurrences and divide and conquer: the calls form a tree and the work is counted over its nodes
- Aliasing and mutability: out.append(cur) stores the same list and out.append(cur[:]) stores a copy
Step by step
Choose, explore, undo
Trace the subsets function as choose, explore and undo on one shared list, and count its subsets and calls as 2ⁿ.
The first call has cur equal to [ ] and records the empty subset. Its first choice is the item 1, so cur becomes [1], and the call that explores it records [1]. That call chooses 2, so cur is [1, 2] and the next call records [1, 2], and that one chooses 3, so the call after it records [1, 2, 3].
A common mix-up
The pop is optional, since out already has the subset.
In fact: Without the undo cur keeps the item, and every later subset contains it, so subsets of [1, 2] come out as [ ], [1], [1, 2], [1, 2, 2].
Every order of the items
Trace the permutations function with a used flag for each item, and give the number of permutations as n! and the number of calls.
So from [1] the call chooses 2, then the only item left is 3, and [1, 2, 3] is recorded. After the undos it chooses 3 and then 2, and records [1, 3, 2]. The calls that start with 2 and with 3 have the same shape as the one under 1.
the branch under 1
A common mix-up
The permutations of n items are about the same number as the subsets.
In fact: 4! = 24 is already more than 2⁴ = 16, and n! grows far faster than 2ⁿ.
Pruning on the queens
Trace the 4 queens search, in which a test before each choice skips the choices that cannot lead to a solution, and count how much it saves.
The first queen is in row 0 at column 0, so cols is [0]. In row 1, column 0 is the same column as that queen. Column 1 is a diagonal, since the columns differ by 1 and the rows differ by 1.
A common mix-up
Pruning may skip a solution, since it cuts branches without looking at them.
In fact: It only cuts partial placements that already clash, which no later queen can repair, so the same 2 solutions are found as by testing all 24 orderings of the columns.
Try it
How many subsets does a list of 5 items have, and what is the largest size of a subset the function records?Answer
32 subsets; the largest has 5 items
Hint: Each item is in or out, and the call that chose them all records the whole list
How many permutations are there of the items 1, 2, 3, 4, 5, and how many start with the item 5?Answer
120; 24
Hint: After fixing the 5, the other 4 items can be ordered in 4! ways
For 4 queens with cols = [2], which column of row 1 is safe?Answer
Only column 0
Hint: Columns 1 and 3 are one column away and one row away
The call that recorded [1, 2, 3] has no item left to choose, so it returns. What does the pop in the call that chose 3 do, and what is cur afterwards?Answer
It removes the 3, and cur is [1, 2].
With cur = [1], which items can the loop choose next, and why not the 1?Answer
The 2 and the 3, since used[0] is True while the 1 is in cur.
