Contents of Table: A. Recurrence
Contents of Table: A. Recurrence
=
O + =
O + + + + + =
=
O + <
O + =
=
x
x
k
i
=
=
1
1
0
2. Recurrence solution methods
7
Recursion-tree Solution to example
Total Cost is
( When 0<x<1 then, )
Now we guess the solution is T (n) = O(n
2
)
2. Recurrence solution methods
8
Recursion-tree Solution to example
Prove T (n) = O(n
2
) by the substitution method
Recurrence :
Guess : T (n) = O(n
2
)
Prove : T (n) dn
2
(for some d > 0 and for the same c > 0)
T (n) = 3T (n/4) + (n
2
)
= 3T(n/4) + cn
2
k means
k+1 means n
Substitution
T (n/4) d (n/4)
2
4 / n
2. Recurrence solution methods
9
Recursion-tree Solution to example
Find constant d
T(n) = 3T(n/4) + cn
2
3d(n/4)
2
+ cn
2
3d(n/4)
2
+ cn
2
= 3/16 dn
2
+ cn
2
dn
2
(where the last step holds as long as d (16/13)c )
Now we proved the solution is T (n) = O(n
2
)
2. Recurrence solution methods
Recursion-tree Example
10
The recurrence
T (n) = T (n/3)+T(2n/3)+O(n)
Use the recursion tree to making a good guess for upper
bound for the solution.
2. Recurrence solution methods
Recursion-tree Solution to example
Draw recursion tree
11
)
9
(
n
c )
9
2
(
n
c
)
3
(
n
c
)
9
2
(
n
c )
9
4
(
n
c
)
3
2
(
n
c
cn
log
3/2
n
cn
cn
cn
Total : O(n log n)
T (n) = T (n/3)+T(2n/3)+ cn
This recursion tree is not a complete binary tree.
Not all leaves contribute a cost of exactly cn
2. Recurrence solution methods
Recursion-tree Solution to example
Draw recursion tree
12
The total cost = Cost of each level x Height
= cn x log
3/2
n
= O(cnlog
3/2
n)
= O(n lg n)
The longest path from root to a leaf is
n(2/3)n (2/3)
2
n 1.
Since, (2/3)
k
n = 1 when k=log
3/2
n and it means trees height is log
3/2
n .
T (n) = T (n/3)+T(2n/3)+ cn
)
9
(
n
c )
9
2
(
n
c
)
3
(
n
c
)
9
2
(
n
c )
9
4
(
n
c
)
3
2
(
n
c
cn
log
3/2
n
cn
cn
cn
2. Recurrence solution methods
Recursion-tree Solution to example
Prove T (n) = O(n lg n) by the substitution method
13
Recurrence :
Guess : T (n) = O(n lg n)
Prove : T (n) d n lg n
(for some d > 0 and for the same c > 0)
T(n/3) + T(2n/3) + cn
Substitution
k means
k+1 means n
) 3 / lg( ) 3 / ( ) 3 / ( n n c n T s
) 3 / 2 lg( ) 3 / 2 ( ) 3 / 2 ( n n c n T s
3 / n
3 / 2n
2. Recurrence solution methods
14
Recursion-tree Solution to example
Find constant d
T(n) = T(n/3) + T(2n/3) + cn
d(n/3)lg(n/3) + d(2n/3)lg(2n/3) + cn
= (d(n/3)lgn - d(n/3)lg 3) + (d(2n/3)lgn d(2n/3)lg(3/2)) + cn
= dnlgn - d((n/3)lg3 + (2n/3)lg(3/2)) + cn
= dnlgn - d((n/3)lg3 + (2n/3)lg(3/2)) + cn
= dnlgn - d((n/3)lg3 + (2n/3)lg3 - (2n/3)lg2) + cn
= dnlgn - dn(lg3 - 2/3) + cn
dnlgn (as long as d c/(lg 3 - (2/3)) )
Now we proved the solution is T (n) = O(n log n
)