Algorithm
Algorithm
Algorithm
and
Recurrence Relations
Debdeep Mukhopadhyay
IIT Madras
Divide & Conquer Algorithms
T(n)=2T(n/2)+2
Fast Multiplication Example
• The ordinary grade-school algorithm takes
Θ(n2) steps to multiply two n-digit numbers.
– Can we do better?
• Let’s find an asymptotically faster algorithm!
• To find the product cd of two 2n-digit base-b
numbers, c=(c2n-1c2n-2…c0)b and
d=(d2n-1d2n-2…d0)b, first, we break c and d in
half:
c=bnC1+C0, d=bnD1+D0
Derivation of Fast Multiplication
cd = (b nC1 + C0 )(b n D1 + D0 )
(Multiply out
= b C1 D1 + b (C1 D0 + C0 D1 ) + C0 D0 polynomials)
2n n
= b 2 nC1 D1 + C0 D0 + Zero
⎪
f (n) ∈ ⎨O(n d log n) if a = b d
⎪O(nlogb a ) if a > b d
⎩
Master Theorem Example
• Recall that complexity of fast multiply was:
T(n) = 3T(n/2) + Θ(n)
• Thus, a=3, b=2, d=1. So a > bd, so case 3
of the master theorem applies, so:
T (n) = O(n logb a
) = O(n log2 3
)