Sub topic 3
Why Brute Force Does Not Scale
This chapter assumes you can read Python functions and loops, and that you know Big-O notation. It also assumes you know the difference between a subsequence and a substring, and how the LCS problem is stated. Both were covered in the earlier chapters.
The Obvious Approach
The most direct way to find a longest common subsequence of a (length m) and b (length n) works in two steps:
- List every subsequence of
a. - Keep the longest one that is also a subsequence of
b.
This method is correct. Every common subsequence is a subsequence of a, so it appears in the list, and the longest one that passes the check is an LCS.
A Working Implementation
The version below tries candidates from longest to shortest, so the first one that passes the check is an answer. The subsequence test uses a single iterator over b. Each ch in it call consumes b up to and including the match, which keeps the characters in order.
from itertools import combinations
def is_subsequence(s, t):
it = iter(t)
return all(ch in it for ch in s)
def lcs_brute(a, b):
checks = 0
for k in range(min(len(a), len(b)), -1, -1):
for idx in combinations(range(len(a)), k):
checks += 1
cand = "".join(a[i] for i in idx)
if is_subsequence(cand, b):
return cand, checks
return "", checks
result, checks = lcs_brute("ABCBDAB", "BDCABA")
print(result, len(result), checks) # one LCS of length 4, after testing several candidatesOn these 7- and 6-character strings the function returns almost instantly. The trouble shows up as the inputs grow.
Counting the Cost
Each character of a is either kept or dropped, so a has 2^m subsequences once you count the empty one. Each check costs O(n). In the worst case the total is O(n · 2^m). You can pick the shorter string as a, but the cost is still exponential in min(m, n).
| m (length of a) | Candidates (2^m) | Time at 10^9 checks per second |
|---|---|---|
| 10 | 1,024 | about 1 microsecond |
| 20 | about 10^6 | about 1 millisecond |
| 30 | about 10^9 | about 1 second |
| 50 | about 10^15 | about 13 days |
| 100 | about 10^30 | far longer than the age of the universe |
Where the Work Is Wasted
Brute force treats each candidate as an independent question. Candidates overlap heavily, though. Testing "ABCB" and then "ABCA" against b repeats the same work for the shared prefix "ABC". Across 2^m candidates, the same questions about the same prefixes of a and b get answered over and over.
A good algorithm should answer each of those smaller questions once. To make that possible, we need to describe an LCS in terms of the LCS of shorter prefixes, which is the subject of the next chapter.
Exercise
Two strings each have 40 characters. Assuming 10^9 basic steps per second and about n steps per subsequence check, roughly how long does brute force take in the worst case?
Show answer
There are 2^40 ≈ 1.1 × 10^12 candidates, and each costs about 40 steps, so the total is about 4.4 × 10^13 steps. At 10^9 steps per second that is about 44,000 seconds, or roughly 12 hours.
Exercise
Why should the brute-force method enumerate subsequences of the shorter string rather than the longer one?
Show answer
The number of candidates is 2^(length of the enumerated string), while the check costs only time linear in the other string's length. Enumerating the shorter string gives O(max(m, n) · 2^min(m, n)), which is exponentially smaller than enumerating the longer one.