Sub topic 2
Why Brute Force Fails: 2^n Subsequences
This chapter assumes you can read basic Python, know Big-O notation, and know what a subsequence is from the previous chapter. The goal is to write the obvious exhaustive solution, measure how fast it grows, and find the wasted work that the dynamic program in the next chapter removes.
The Obvious Algorithm
A subsequence is fixed by choosing, for each index, whether to keep that element or skip it. An array of length n therefore has exactly 2^n subsequences, counting the empty one. The brute-force approach checks each choice:
- Generate every subset of indices.
- Walk the chosen elements left to right and check that each is strictly greater than the one before it.
- Keep the longest subsequence that passes the check.
A bitmask is a convenient way to list the subsets. Bit i of an integer mask says whether index i is kept.
def lis_brute_force(a):
n = len(a)
best = 0
for mask in range(1 << n): # every subset of indices
prev = None
length = 0
ok = True
for i in range(n):
if (mask >> i) & 1: # index i is kept
if prev is not None and a[i] <= prev:
ok = False
break
prev = a[i]
length += 1
if ok and length > best:
best = length
return best
print(lis_brute_force([10, 9, 2, 5, 3, 7, 101, 18])) # 4The function is correct, and it is short enough to trust. The outer loop runs 2^n times and the inner check costs up to n, so the total cost is O(n · 2^n) time with O(1) extra space.
How Fast 2^n Grows
The table below assumes a generous rate of 10^8 subsets checked per second. Compiled code might reach that rate. Pure Python is much slower.
| n | Subsets (2^n) | Approximate time |
|---|---|---|
| 10 | 1,024 | instant |
| 20 | about 1.05 × 10^6 | 0.01 s |
| 30 | about 1.07 × 10^9 | 11 s |
| 40 | about 1.10 × 10^12 | 3 hours |
| 60 | about 1.15 × 10^18 | 365 years |
Each extra element doubles the work. An array of 60 numbers is tiny as input, and brute force still cannot finish on it.
Pruning Does Not Save It
A natural improvement is recursive search that stops extending a branch once the increasing condition fails. You decide "take" or "skip" for each element and only take an element if it is larger than the last one taken.
Pruning helps on some inputs, but the worst case remains. On a sorted array every subset is increasing, so no branch is ever cut and the search visits all 2^n leaves.
Where the Work Is Wasted
Look at the tree again. The branches [1, 2] and [2] both reach the decision about element 3 with the same last value, 2. To decide what can come next, the search only needs two facts: which element was taken last, and how long the subsequence is. The path that led there does not matter.
Brute force solves the question "what is the best continuation after taking index j?" over and over, once for every path that ends at j. There are only n such questions. If each answer is computed once and reused, the exponential tree collapses to roughly n^2 comparisons. The next chapter builds exactly that dynamic program.
Exercises
Exercise
An array contains n distinct values in ascending order. How many of its subsequences, including the empty one, are strictly increasing, and what does this imply for pruned backtracking?
Show answer
All 2^n subsequences are strictly increasing, because any subset of a sorted array of distinct values stays sorted. Pruned backtracking never cuts a branch on this input, so it still visits 2^n leaves and runs in exponential time.
Exercise
The brute-force check runs in O(n · 2^n). If n = 25 takes 1 second, roughly how long does n = 30 take?
Show answer
About 2^5 × (30/25) ≈ 32 × 1.2 ≈ 38 seconds. The 2^n factor dominates: five more elements multiply the work by 32.