0% found this document useful (0 votes)
31 views

Contents of Table: A. Recurrence

The document discusses different methods for solving recurrence relations, including recursion trees. It provides an example of using a recursion tree to solve the recurrence T(n) = 3T(n/4) + Θ(n^2). The recursion tree has height log_4(n) and 3^log_4(n) nodes, leading to the solution T(n) = O(n^2). It also solves another example recurrence T(n) = T(n/3) + T(2n/3) + O(n) using a recursion tree, finding the solution T(n) = O(n log n).

Uploaded by

Byungwoo Kim
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Contents of Table: A. Recurrence

The document discusses different methods for solving recurrence relations, including recursion trees. It provides an example of using a recursion tree to solve the recurrence T(n) = 3T(n/4) + Θ(n^2). The recursion tree has height log_4(n) and 3^log_4(n) nodes, leading to the solution T(n) = O(n^2). It also solves another example recurrence T(n) = T(n/3) + T(2n/3) + O(n) using a recursion tree, finding the solution T(n) = O(n log n).

Uploaded by

Byungwoo Kim
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

Contents of Table

Recurrence solution methods


The substitution method
The recursion-tree method
The master theorem method
1
A. Recurrence
2. Recurrence solution methods
Recursion-tree
- Definition
For come up with good guess,
convert the recurrence into a tree
that each node represents the cost of a single subproblem
somewhere in the set of recursive function invocations.


We sum all the per-level costs to determine the total cost of all
levels of the recursion
2
2. Recurrence solution methods
Recursion-tree Example








3
The recurrence is
T (n) = 3T (n/4) + (n
2
)
= 3T (n/4) + cn
2
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





4
T(n)
cn
2
)
4
(
n
T )
4
(
n
T )
4
(
n
T
)
16
(
n
T )
16
(
n
T )
16
(
n
T
2
)
4
(
n
c
)
16
(
n
T )
16
(
n
T )
16
(
n
T
2
)
4
(
n
c
)
16
(
n
T )
16
(
n
T )
16
(
n
T
2
)
4
(
n
c
cn
2
3T (n/4) + cn
2

2. Recurrence solution methods
Recursion-tree Solution to example


5
cn
2
c(n/4)
2
c(n/4)
2
c(n/4)
2
2
)
16
(
n
c
2
)
16
(
n
c
2
)
16
(
n
c
2
)
16
(
n
c
2
)
16
(
n
c
2
)
16
(
n
c
2
)
16
(
n
c
2
)
16
(
n
c
2
)
16
(
n
c
T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1) T(1)

3 log
4
n
log
4
n
When depth i then subprogram size is n/4
i
= 1 so, i = log
4
n
and it means trees height and tree has log
4
n + 1 levels.

The number of nodes at depth i is 3
i
and finally
3 log log
4 4
3 n
n
=
2. Recurrence solution methods
6
Recursion-tree Solution to example
Total Cost


) (
1 ) 16 / 3 (
1 ) 16 / 3 (
) ( )
16
3
(
) ( )
16
3
( ... )
16
3
(
16
3
) (
3 log 2
log
3 log 2
1 log
0
3 log 2 1 log 2 2 2 2
4
4
4
4
4 4
n cn
n cn
n cn cn cn cn n T
n
i
n
i
n
O +


=
O + =
O + + + + + =

Sum of cost for root to i-1 depth


Sum of cost for i depth
) (
) (
13
16
) (
) 16 / 3 ( 1
1
) ( )
16
3
(
) ( )
16
3
( ) (
2
3 log 2
3 log 2
3 log 2
0
3 log 2
1 log
0
4
4
4
4
4
n O
n cn
n cn
n cn
n cn n T
i
i
i
n
i
=
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

)

You might also like