Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Algo Assignment 3 Solution

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1.

T(n) = T(n-2) + 2n, T(0)=0


= T(n-4) + 2n-2 + 2n
= T(n-6) + 2n-4 + 2n-2 + 2n
= T(n-8) + 2n-6 + 2n-4 + 2n-2 + 2n
.
.
.
= T(n-2*k) + 2n-2(k-1) + 2n-2(k-2) +...+ 2n-2 + 2n
= T(n-2*k) +
Let,
n-2*k = 0
n = 2k
k = n/2
So,
T(n) = T(0) +
= 0 +
= 0 +
= 0 +
= 0 +
= Θ(2n)

2. T(n) = (T(n-2))2, T(0)=2


= (T(n-4)2)2
= (T(n-4))4
= (T(n-6)2)4
= (T(n-6))8
= (T(n-8)2)8
= (T(n-8))16
.
.
.
= (T(n-2*k))^
Let,
n-2*k = 0
n = 2k
k = n/2
So,
T(n) = (T(0))^
= (2)^
= (2)^
= Θ()

3. T(n) = T(√n) + Θ(log2n), T(2)=1


= T(n1/2) + clog2n
= (T(n1/4) + clog2n1/2) + clog2n [Substitution]
= T(n1/4) + (c/2)log2n + clog2n
= (T(n1/8) + clog2n1/4) + (c/2)log2n + clog2n [Substitution]
= T(n1/8) + (c/4)log2n + (c/2)log2n + clog2n
.
.
.
= T(n1/2^k) + clog2n
Let,
n1/2^k = 2
lg2n1/2^k = lg22
1/2klg2n = 1
lg2n = 2k
lg2lg2n = lg22k
lg2lg2n = k
So,
T(n) = T(2) + clog2n
= 1 + clog2n
= O(lg2n)

4. T(n) = T(n/2) + T(√n) + Θ(n), T(1)=1


T(n) ≤ 2T(n/2) + cn

≤ 2(2T(n/4) + cn/2) + cn [Substitution]

≤ 4T(n/4)+ cn + cn

≤ 4(2T(n/8) + cn/2) + cn + cn [Substitution]

≤ 8T(n/8) + cn + cn + cn

.
.
.

≤ 2kT(n/2k) + kcn
Let,
n/2k = 1
n = 2k
lg2n = lg22k
lg2n = k
So,
T(n) = 2lg2_nT(1) + lg2n*cn
= 2lg2_n*1 + lg2n*cn
= O(nlg2n)
5. T(n) = 2T(n/3) + Θ(nlg2n), T(1)=1
= 2T(n/3) + cnlg2n
= 2(2T(n/9) + cn/3lg2n/3 ) + cnlg2n [Substitution]
= 4T(n/9) + 4cn/3lg2n/3 + cnlg2n
= 4(2T(n/27) + cn/9lg2n/9) + 4cn/3lg2n/3 + cnlg2n [Substitution]

= 8T(n/27) + 8cn/9lg2n/9 + 4cn/3lg2n/3 + ccnlg2n


.
.
.
= 2kT(n/3k) +
Let,
n/3k = 1
n = 3k
lg3n = lg33k
lg3n = k
So,
T(n) = 2lg3_nT(1) +
= 2lg3_n*1 +
= 2lg3_n +
= 2lg3_n +
= nlg3_2 +
= Θ(nlg2n)

6. T(n) = T(n/5) + Θ((lg2n)2), T(1)=1


= T(n/5) + c(lg2n)2
= T(n/52) + c(lg2n/5)2 + c(lg2n)2 [Substitution]
= T(n/53) + c(lg2n/52)2 + c(lg2n/5)2 + c(lg2n)2 [Substitution]

.
.
.
= T(n/5k) + c
Let,
n/5k = 1
n = 5k
lg5n = lg55k
lg5n = k
So,
T(n) = T(1) + c
= 1 + c
= 1 + c
= 1 + c
= 1 + c
= 1 +
= Θ(lg5n*(lg2n)2)
= Θ((lg2n/lg25)*(lg2n)2) = Θ((lg2n)3)

7. T(n) = T(n/3) + T(2n/6) + Θ(n), T(1)=1


= T(n/3) + T(n/3) + Θ(n)
= 2T(n/3) + Θ(n)
= 2T(n/3) + cn
= 2(2T(n/9) + cn/3) + cn [Substitution]
= 4T(n/9) + 2cn/3 + cn
= 4(2T(n/27) + cn/9) + 2cn/3 + cn [Substitution]
= 8T(n/27) + 4cn/9 + 2cn/3 + cn
.
.
.
= 2kT(n/3k) + cn
Let,
n/3k = 1
n = 3k
lg3n = lg33k
lg3n = k
So,
T(n) = 2lg3_nT(1) + cn
= nlg3_2*1 + cn
= nlg3_2 + cn * (1-(2/3)lg3_n)/(1-2/3)
= n0.63 + cn * (1-(2/3)lg3_n)/(1-2/3)
= O(n)

You might also like