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

Recurrence Relations Examples

Uploaded by

221nicole0006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Recurrence Relations Examples

Uploaded by

221nicole0006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Practice with Recurrence Relations (Solutions)

Solve the following recurrence relations using the iteration technique:

1) 𝑇(𝑛) = 𝑇(𝑛 − 1) + 2, 𝑇(1) = 1


================================================
T(n) = T(n-1) + 2 Substituting Equations
T(1) = 1 n → n-1

T(n) = T(n-1) + 2 = [T(n-2) + 2] + 2 = T(n-2) + 2 + 2 T(n-1) = T(n-2) + 2


T(n) = T(n-2) + 2*2 T(n-2) = T(n-3) + 2
T(n) = T(n-2) + 2*2 = [T(n-3) + 2] + 2*2 = T(n-3) + 2 + 2*2 T(n-3) = T(n-4) + 2
T(n) = T(n-3) + 2*3 T(n-4) = T(n-5) + 2
T(n) = T(n-3) + 2*3 = [T(n-4) + 2] + 2*3 = T(n-4) + 2 + 2*3
T(n) = T(n-4) + 2*4

Do it one more time…

T(n) = T(n-4) + 2*4

So now rewrite these five equations and look for a pattern:


T(n) = T(n-1) + 2*1 1st step of recursion
T(n) = T(n-2) + 2*2 2nd step of recursion
T(n) = T(n-3) + 2*3 3rd step of recursion
T(n) = T(n-4) + 2*4 4th step of recursion
T(n) = T(n-5) + 2*5 5th step of recursion

Generalized recurrence relation at the kth step of the recursion:


T(n) = T(n-k) + 2*k

We want T(1). So we let n-k = 1. Solving for k, we get k = n - 1. Now plug back in.

T(n) = T(n-k) + 2*k


T(n) = T(1) + 2*(n-1), and we know T(1) = 1
T(n) = 2*(n-1) = 2n-1

We are done. Right side does not have any T(…)’s. This recurrence relation is now solved in its
closed form, and it runs in O(n) time.
2) 𝑇(𝑛) = 2𝑇(𝑛/2) + 𝑛, 𝑇(1) = 1
================================================
T(n) = 2T(n/2) + n Substituting Equations
T(1) = 1 n → n/2

T(n) = 2T(n/2) + n = 2[2T(n/4) + n/2] + n = 4T(n/4) + n + n T(n/2) = 2T(n/4) + n/2


T(n) = 4T(n/4) + 2n T(n/4) = 2T(n/8) + n/4
T(n) = 4T(n/4) + 2n = 4[2T(n/8) + n/4] + 2n = 8T(n/8) + n + 2n T(n/8) = 2T(n/16) + n/8
T(n) = 8T(n/8) + 3n T(n/16) = 2T(n/32) + n/16
T(n) = 8T(n/8) + 3n = 8[2T(n/16) + n/8] + 3n = 16T(n/16) + n + 3n
T(n) = 16T(n/16) + 4n
T(n) = 16T(n/16) + 4n = 16[2T(n/32) + n/16] + 4n = 32T(n/32) + n + 4n
T(n) = 32T(n/32) + 5n

So now rewrite these five equations and look for a pattern:


T(n) = 2T(n/2) + n = 21T(n/21) + 1n 1st step of recursion
T(n) = 4T(n/4) + 2n = 22T(n/22) + 2n 2nd step of recursion
T(n) = 8T(n/8) + 3n = 23T(n/23) + 3n 3rd step of recursion
T(n) = 16T(n/16) + 4n = 24T(n/24) + 4n 4th step of recursion
T(n) = 32T(n/32) + 5n = 25T(n/25) + 5n 5th step of recursion

Generalized recurrence relation at the kth step of the recursion:


T(n) = 2kT(n/2k) + kn

We want T(1). So we let n = 2k. Solving for k, we get k = logn. Now plug back in.

T(n) = 2lognT(2k/2k) + (logn)n = n*T(1) + (logn)n = n + nlogn

T(n) = n + nlogn
𝑛
3) 𝑇(𝑛) = 2𝑇 �2� + 1, 𝑇(1) = 1
================================================
T(n) = 2T(n/2) + 1 Substituting Equations
T(1) = 1 n → n/2

T(n) = 2T(n/2) + 1 = 2[2T(n/4) + 1] + 1 = 4T(n/4) + 2 + 1 T(n/2) = 2T(n/4) + 1


T(n) = 4T(n/4) + 3 T(n/4) = 2T(n/8) + 1
T(n/8) = 2T(n/16) + 1
T(n) = 4T(n/4) + 3 = 4[2T(n/8) + 1] + 3 = 8T(n/8) + 4 + 3 T(n/16) = 2T(n/32) + 1
T(n) = 8T(n/8) + 7

T(n) = 8T(n/8) + 7 = 8[2T(n/16) + 1] + 7 = 16T(n/16) + 8 + 7


T(n) = 16T(n/16) + 15

T(n) = 16T(n/16) + 15 = 16[2T(n/32) + 1] + 15 = 32T(n/32) + 16 + 15


T(n) = 32T(n/32) + 31

So now rewrite these five equations and look for a pattern:


T(n) = 2T(n/2) + 1 = 21T(n/21) + 21-1 1st step of recursion
T(n) = 4T(n/4) + 3 = 22T(n/22) + 22-1 2nd step of recursion
T(n) = 8T(n/8) + 7 = 23T(n/23) + 23-1 3rd step of recursion
T(n) = 16T(n/16) + 15 = 24T(n/24) + 24-1 4th step of recursion
T(n) = 32T(n/32) + 31 = 25T(n/25) + 25-1 5th step of recursion

In general, after k iterations, we have:


𝑛
𝑇(𝑛) = 2𝑘 𝑇 � 𝑘 � + 2𝑘 − 1
2

We’re not done since we still have T(…)’s on the right side of the equation. We need to get
down to T(1). How?

We have T(n/2k), and we want T(1). So let n = 2k. We will then have T(2k/2k), which equals
T(1). So use that substitution (n = 2k) throughout the entire generalized, kth recurrence relation.

𝑘
𝑛 𝑘
2𝑘
𝑇(𝑛) = 2 𝑇 � 𝑘 � + 2 − 1 = 𝑛 ∗ 𝑇 � 𝑘 � + 𝑛 − 1 = 𝑛 ∗ 𝑇(1) + 𝑛 − 1
2 2
𝑇(𝑛) = 𝑛 ∗ 1 + 𝑛 − 1 = 2𝑛 − 1

So, T(n) = 2n – 1 and runs in O(n) time.


4) 𝑇(𝑛) = 𝑇(𝑛 − 1) + 𝑛, 𝑇(1) = 1
================================================
T(n) = T(n - 1) + n Substituting Equations
T(1) = 1 n → n-1

T(n) = T(n-2) + (n-1) + n T(n-1) = T(n-2) + n-1


T(n) = T(n-3) + (n-2) + (n-1) + n T(n-2) = T(n-3) + n-2
T(n) = T(n-4) + (n-3) + (n-2) + (n-1) + n T(n-3) = T(n-4) + n-3
T(n) = T(n-5) + (n-4) + (n-3) + (n-2) + (n-1) + n T(n-4) = T(n-5) + n-4

So now rewrite these five equations and look for a pattern:


T(n) = T(n - 1) + n 1st step of recursion
T(n) = T(n-2) + (n-1) + n 2nd step of recursion
T(n) = T(n-3) + (n-2) + (n-1) + n 3rd step of recursion
T(n) = T(n-4) + (n-3) + (n-2) + (n-1) + n 4th step of recursion
T(n) = T(n-5) + (n-4) + (n-3) + (n-2) + (n-1) + n 5th step of recursion

Generalized recurrence relation at the kth step of the recursion:


𝑇(𝑛) = 𝑇(𝑛 − 𝑘) + (𝑛 − 𝑘 + 1) + (𝑛 − 𝑘 + 2) + ⋯ + (𝑛 − 1) + 𝑛

Yes, this looks really ugly, but watch how quickly it cleans up when we try to solve it…

We’re not done since we still have T(…)’s on the right side of the equation. We need to get
down to T(1). How?

We have T(n-k) and we want T(1). So, we let n – k = 1. Also, solve for k, k = n – 1. Now, plug
this in all across the board:

𝑇(𝑛) = 𝑇(1) + 2 + 3 + ⋯ + (𝑛 − 1) + 𝑛
𝑇(𝑛) = 1 + 2 + ⋯ + (𝑛 − 1) + 𝑛

You should hopefully recognize this sequence, as it was shown in class.

𝑛(𝑛 + 1)
𝑇(𝑛) = = 𝑂(𝑛2 )
2

You might also like