Sub topic 13
Analyzing the Cost of Backtracking
This chapter assumes you know Big-O notation and can read the subset and permutation generators from earlier chapters. The goal is a repeatable way to estimate the running time and memory of any backtracking algorithm before you run it.
The Core Formula
A backtracking algorithm visits nodes of its state-space tree. Its running time is roughly:
time = (number of nodes visited) × (work done per node)
Work per node includes the loop over choices, any validity check, and copying a finished result. Copying path[:] costs O(n), and it is often the term people forget.
Memory is a separate question. Without counting the output, space is the maximum recursion depth plus the size of the mutable state (path, used, sets of occupied columns). Both are usually O(n).
Counting Nodes in Practice
For subsets, every call records one subset, so the tree has exactly 2^n nodes. Each node copies a path of up to n elements, which gives O(n · 2^n).
For permutations, level k of the tree has n!/(n−k)! nodes. The sum over all levels is about e · n!, so the node count is O(n!). Each node loops over n indices, and each leaf copies n elements. The total is O(n · n!).
You can check these counts by instrumenting the recursion:
def count_subset_calls(n):
calls = 0
def backtrack(start):
nonlocal calls
calls += 1
for i in range(start, n):
backtrack(i + 1)
backtrack(0)
return calls
def count_permutation_calls(n):
calls = 0
used = [False] * n
def backtrack(depth):
nonlocal calls
calls += 1
if depth == n:
return
for i in range(n):
if not used[i]:
used[i] = True
backtrack(depth + 1)
used[i] = False
backtrack(0)
return calls
for n in range(1, 7):
print(n, count_subset_calls(n), count_permutation_calls(n))
# 1 2 2
# 2 4 5
# 3 8 16
# 4 16 65
# 5 32 326
# 6 64 1957For n = 6, 1957 is very close to e × 720 ≈ 1957.3, which confirms the estimate.
When the Tree Is Irregular
For combination sum, the tree has no fixed shape. A safe upper bound is b^d. Here b is the branching factor (the number of candidates) and d is the maximum depth (target ÷ smallest candidate). The bound is loose, but it tells you whether the search is feasible at all.
What Pruning Does and Does Not Change
Pruning removes subtrees, so it often cuts the real running time by large factors. It rarely improves the worst-case Big-O, because an adversarial input can make every check pass. N-Queens is still exponential with constant-time set checks. Pruning only shrinks the base of the exponent in practice.
Summary Table
| Problem | Nodes | Work per node | Time | Extra space |
|---|---|---|---|---|
| Subsets | 2^n | O(n) copy | O(n · 2^n) | O(n) |
| Permutations | ≈ e · n! | O(n) loop | O(n · n!) | O(n) |
| Combination sum | ≤ b^d | O(d) copy at leaves | O(d · b^d) | O(d) |
| N-Queens | ≤ n! | O(n) loop | O(n · n!) worst case | O(n) |
Exercise
A word search runs on an R × C grid for a word of length L. Each step can move to at most 3 new neighbors, because the cell it came from is marked visited. Give a time bound and a space bound.
Show answer
Each of the R·C starting cells explores a tree of depth L. The first step has up to 4 branches and later steps up to 3, so time is O(R · C · 3^L). Space is O(L) for the recursion depth, since visited marks are written into the board in place.
Exercise
Why does the permutation generator visit about e · n! nodes rather than exactly n!?
Show answer
The n! leaves are only the last level. The internal levels add n!/1! + n!/2! + ..., and the sum of 1/k! converges to e, so the total is about e · n!.