Professional Documents
Culture Documents
Reccurences PDF
Reccurences PDF
(CLRS 4.1-4.2)
To Solve P:
1. Divide P into smaller problems P1 , P2 , P3 .....Pk .
2. Conquer by solving the (smaller) subproblems recursively.
3. Combine solutions to P1 , P2 , ...Pk into solution for P.
1. Substitution method
2. Iteration method
Recursion-tree method
(Master method)
1 Solving Recurrences with the Substitution Method
Idea: Make a guess for the form of the solution and prove by induction.
Can be used to prove both upper bounds O() and lower bounds ().
Guess T (n) cn log n for some constant c (that is, T (n) = O(n log n))
Proof:
Base case: we need to show that our guess holds for some base case (not necessarily
n = 1, some small n is ok). Ok, since function constant for small constant n.
Assume holds for n/2: T (n/2) c n2 log n2 (Question: Why not n 1?)
Prove that holds for n: T (n) cn log n
T (n) = 2T (n/2) + n
n n
2(c log ) + n
2 2
n
= cn log + n
2
= cn log n cn log 2 + n
= cn log n cn + n
So ok if c 1
The hard part of the substitution method is often to make a good guess. How do we make
a good (i.e. tight) guess??? Unfortunately, theres no recipe for this one. Try iteratively
O(n3 ), (n3 ), O(n2 ), (n2 ) and so on. Try solving by iteration to get a feeling of the growth.
2 Solving Recurrences with the Iteration/Recursion-tree Method
In the iteration method we iteratively unfold the recurrence until we see the pattern.
The iteration method does not require making a good guess like the substitution method (but
it is often more involved than using induction).
T (n) = n2 + 8T (n/2)
n n
= n2 + 8(8T ( 2 ) + ( )2 )
2 2
n n 2
= n2 + 82 T ( 2 ) + 8( ))
2 4
2 2 2 n
= n + 2n + 8 T ( 2 )
2
n n
2 2 2
= n + 2n + 8 (8T ( 3 ) + ( 2 )2 )
2 2
n n2
= n2 + 2n2 + 83 T ( 3 ) + 82 ( 2 ))
2 4
2 2 2 2 3 n
= n + 2n + 2 n + 8 T ( 3 )
2
= ...
= n2 + 2n2 + 22 n2 + 23 n2 + 24 n2 + . . .
Recursion depth: How long (how many iterations) it takes until the subproblem has
constant size? i times where 2ni = 1 i = log n
What is the last term? 8i T (1) = 8log n
k=0
Plog n1 Plog n1
Now k=0 2k is a geometric sum so we have k=0 2k = (2log n1 ) = (n)
T (n) = n2 (n) + n3
= (n3 )
2.1 Recursion tree
A different way to look at the iteration method: is the recursion-tree, discussed in the book (4.2).
we draw out the recursion tree with cost of single call in each noderunning time is sum of
costs in all nodes
if you are careful drawing the recursion tree and summing up the costs, the recursion tree is
a direct proof for the solution of the recurrence, just like iteration and substitution
1) 2) n2 3) n2
T(n)
(n/2)2 (n/2)2
T(n/2) T(n/2)
T(n/4) T(n/4)
log n) n2 n2
+
log n
8 T(1)
T(1) T(1) T(1)
We want to compute Z = X Y
Pn
zij = k=1 Xik Ykj
Divide-and-conquer solution:
( ) ( ) ( )
A B E F (A E + B G) (A F + B H)
Z= =
C D G H (C E + D G) (C F + D H)
S1 = (B D) (G + H)
S2 = (A + D) (E + H)
S3 = (A C) (E + F )
S4 = (A + B) H
S5 = A (F H)
S6 = D (G E)
S7 = (C + D) E
Lets test that S6 + S7 is really C E + D G
S6 + S7 = D (G E) + (C + D) E
= DG DE + CE + DE
= DG + CE
This leads to a divide-and-conquer algorithm with running time T (n) = 7T (n/2) + (n2 )
T (n) = 7T (n/2) + n2
n n
= n2 + 7(7T ( 2 ) + ( )2 )
2 2
7 n
= n2 + ( 2 )n2 + 72 T ( 2 )
2 2
7 2 n n
= n + ( 2 )n + 7 (7T ( 3 ) + ( 2 )2 )
2 2
2 2 2
2 7 2 7 2 2 3 n
= n + ( 2 )n + ( 2 ) n + 7 T ( 3 )
2 2 2
7 7 7 7
= n2 + ( 2 )n2 + ( 2 )2 n2 + ( 2 )3 n2 .... + ( 2 )log n1 n2 + 7log n
2 2 2 2
logX
n1
7 i 2
= ( ) n + 7log n
i=0
22
7 log n1
= n2 (( ) ) + 7log n
22
7log n
= n2 ( 2 log n ) + 7log n
(2 )
7log n
= n2 ( 2 ) + 7log n
n
= (7log n )
Note:
4 Master Method
We have solved several recurrences using substitution and iteration.
We do!
n
+ nc
T (n) = aT b a 1, b 1, c > 0
log a a > bc
(n b )
T (n) = (nc log b n) a = b
c
(nc )
a < bc
T (n) = aT nb + nc
c
= nc + a nb + aT bn2
a n
= nc + c + a2 T
bc n 2
b c
a n n
= nc + c 2
bc n + a b2
+ aT
b3
a a 2 c
nc + c n + a3 T bn3
= bc n+ bc
= ...
2 3 a 4 c a logb n1 c
nc + bac nc + bac nc + bac nc + + alogb n T (1)
= bc n + ... + bc n
Plog n1 a k
= nc k=0b bc + a
logb n
log n1 a k
= nc k=0b + nlogb a
P
bc
Pn k xn+1 1
Recall geometric sum k=0 x = x1 = (xn )
a < bc
Plogb n1
a k a k
a < bc a
<1 + = 1
= (1)
P
bc k=0 bc k=0 bc 1( bac )
c c
a < b logb a < logb b = c
Plog n1 a k
T (n) = nc k=0b bc + nlogb a
= nc (1) + nlogb a
= (nc )
a = bc
Plog n1 k Plogb n1
a = bc bac = 1 k=0b a
bc = k=0 1 = (logb n)
a = bc logb a = logb bc = c
Plogb n1 a k
T (n) = k=0 bc + nlogb a
= n (logb n) + nlogb a
c
= (nc logb n)
a > bc
a Plogb n1 a k
a logb n
alogb n
alogb n
a > bc bc >1 k=0 bc = bc = (bc )logb n
= nc
log n
T (n) = nc a ncb + nlogb a
= (n logb a )+n balog
= (nlogb a )
Note: Book states and proves the result slightly differently (dont read it).
5 Changing variables
Sometimes reucurrences can be reduced to simpler ones by changing variables
Example: Solve T (n) = 2T ( n) + log n
Let m = log n 2m = n n = 2m/2
T (n) = 2T ( n) + log n T (2m ) = 2T (2m/2 ) + m
Logarithmic: (log n)
Linear: (N )
Recurrence: T (n) = 1 + T (n 1)
Typical example: Single loop
Variations: T (n) = 1 + 2T (n/2), T (n) = n + T (n/2), T (n) = T (n/5) + T (7n/10 + 6) + n
Quadratic: (n2 )
Recurrence: T (n) = n + T (n 1)
Typical example: Nested loops
Exponential: (2n )
Recurrence: T (n) = 2T (n 1)