
Lecture 5
Divide-and-Conquer Algorithms
Integer Multiplication and Selection Problems
Description of basic technique
1) Decompose input instance into a number of smaller sub-instances
2) Solve each of the subinstances
3) Combine the subsolutions to obtain a solution to the original input instance
Key issue
- How do we solve sub-problems (direct solution or recursive call)?
- Tradeoff depending on problem size
Multiplication of two n-digit integers
- Assume n is a perfect power of 2
- Operations we will measure:
- How many 1 digit additions/shifts are required?
Direct Solution
- (n2) 1 digit additions/shifts required; lets say cn2 work for some constant c
One-step Divide-and-Conquer
Two integers can be represented as wx and yz
Their multiplication can be broken down into the addition of
1. w times y shift n places
2. w times z plus x times y shift n/2 places
3. x times z
Also observe that (w+x)(y+z) = wy + (wz + xy) + yz
Thus we can compute all of the above three parts by doing essentially 3 n/2 digit multiplications
- We do need some extra additions/shifts, (n) total
- The last multiplication may be of n/2 + 1 digit numbers rather n/2 digit numbers
Analysis: T(n) = 3 c(n/2)2 + dn
= ¾ c n2 + dn
The n2 term dominates for large n and we have reduced the time by ¼
Full Divide-and-Conquer
To solve subproblem, recursively call the same routine
Analysis: T(n) = 3 T(n/2) + dn
T(1) = A where A is some constant
Complexity by Master Theorem analysis?
Working out using substitution and recurrence relation techniques?
When to do direct versus recursive solution?
Theoretically, doing the recursive solution is fine
However, there is typically a trade-off point at which the direct solution is preferable
- We then use the direct solution once the problem size dips below that level
- Calculating this trade-off point precisely requires having a good sense of all
the constants in play including lower order terms
Example:
n = 1024
Direct solution time complexity: n2
Direct solution cost: (1024)2
Recursive solution time complexity: T(n) = 3 T(n/2) + 16 n ---> 33nlg 3 – 32 n
Recursive solution cost: 33 (1024)lg 3 – 32 (1024)
Direct solution is roughly half the cost of the recursive solution