Professional Documents
Culture Documents
(CLRS 4.1-4.2)
(1)
If n = 1
T (d n2 e) + T (b n2 c) + (n) If n > 1
but we will often cheat and just solve the simple formula (equivalent to assuming that
n = 2k for some constant k, and leaving out base case and constant in ).
Methods for solving recurrences
1. Substitution method
2. Iteration method
Recursion-tree method
(Master method)
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
Similarly it can be shown that T (n) = (n log n)
Exercise!
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.
logX
n1
2k n2 + 8log n
k=0
logX
n1
2
k
2 + (23 )log n
= n
k=0
Now
Plog n1
k=0
Plog n1
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
Example: T (n) = 8T (n/2) + n2 (T (1) = 1)
n2
2)
1)
n2
3)
T(n)
(n/2)2
T(n/2)
(n/2)2
T(n/2)
T(n/4)
log n)
T(n/4)
n2
n2
+
(n/2)2
8(n/2)2= 2n2
(n/2)2
+
(n/4)2
82(n/4)2= 22n2
(n/4)2
+
log n
8
T(1)
T(1) T(1)
T(1)
Matrix Multiplication
Let X and Y
x11
x21
X=
x31
xn1
be n n
x12
x22
x32
xn2
matrices
x1n
x1n
x1n
xnn
We want to compute Z = X Y
zij =
Pn
k=1 Xik
Ykj
A B
C D
) (
E F
G H
(A E + B G) (A F + B H)
(C E + D G) (C F + D H)
Lets assume n = 2c for some constant c and let A, B, C and D be n/2 n/2 matrices
Running time of algorithm is T (n) = 8T (n/2) + (n2 ) T (n) = (n3 )
3.1
Strassens Algorithm
where
A B
C D
) (
E F
G H
(S1 + S2 S4 + S6 )
(S4 + S5 )
(S6 + S7 )
(S2 + S3 + S5 S7 )
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
= DG + CE
This leads to a divide-and-conquer algorithm with running time T (n) = 7T (n/2) + (n2 )
We only need to perform 7 multiplications recursively.
Division/Combination can still be performed in (n2 ) time.
Lets solve the recurrence using the iteration method
T (n) = 7T (n/2) + n2
n
n
= n2 + 7(7T ( 2 ) + ( )2 )
2
2
n
7 2
2
2
= n + ( 2 )n + 7 T ( 2 )
2
2
7 2
n
n
2
= n + ( 2 )n + 72 (7T ( 3 ) + ( 2 )2 )
2
2
2
7 2 2
n
7 2
3
2
= n + ( 2 )n + ( 2 ) n + 7 T ( 3 )
2
2
2
7 2
7 2 2
7 3 2
7
2
= n + ( 2 )n + ( 2 ) n + ( 2 ) n .... + ( 2 )log n1 n2 + 7log n
2
2
2
2
=
logX
n1
i=0
7 i 2
) n + 7log n
22
7 log n1
)
) + 7log n
22
7log n
= n2 ( 2 log n ) + 7log n
(2 )
7log n
= n2 ( 2 ) + 7log n
n
log n
= (7
)
= n2 ((
7log n = 7 log7 2
= n log2 2
= nlog 7
Or in general: alogk n = nlogk a
Master Method
We have solved several recurrences using substitution and iteration.
we solved several recurrences of the form T (n) = aT (n/b) + nc
(T (1) = 1).
T (n) = aT
n
b
+ nc
a 1, b 1, c > 0
log a
(n b )
a > bc
(nc logb n) a = bc
T (n) =
(nc )
a < bc
Proof (Iteration method)
T (n) = aT nb + nc
c
= nc + a nb + aT bn2
= nc +
= nc +
=
=
=
=
=
nc
a
c
bc n
a
c
bc n
a
c
bc n
+
+
n
2
b c
n
a2 b2 + aT bn3
a 2 c
3T n
n
+
a
c
b
b3
+ a2 T
...
2
3
nc + bac nc + bac nc + bac nc +
Plog n1 a k
nc k=0b
+ alogb n
c
Plogb n1 ba k
c
+ nlogb a
n
k=0
bc
Pn
k
k=0 x
xn+1 1
x1
a 4 c
n
bc
= (xn )
+ ... +
a logb n1 c
n
bc
+ alogb n T (1)
a < bc
Plogb n1
P
a k
+
k=0
k=0
bc
a < bc logb a < logb bc = c
Plog n1 a k
+ nlogb a
T (n) = nc k=0b
bc
c
log
a
= n (1) + n b
a < bc
a
bc
<1
a k
bc
1
1( bac )
= (1)
= (nc )
a = bc
Plog
n1
k
a
=
a = bc bac = 1 k=0b
bc
c
c
a = b logb a = logb b = c
Plogb n1 a k
+ nlogb a
T (n) =
k=0
bc
= nc (logb n) + nlogb a
= (nc logb n)
a > bc
a > bc
a
bc
>1
Plogb n1
k=0
log n
a k
=
bc
nlogb a
T (n) = nc a ncb
+
log
a
log
= (n b ) + n b a
= (nlogb a )
Plogb n1
k=0
1 = (logb n)
a logb n
bc
alogb n
(bc )logb n
alogb n
nc
Note: Book states and proves the result slightly differently (dont read it).
Changing variables
Other recurrences