Recurrence Relations Examples
Recurrence Relations Examples
We want T(1). So we let n-k = 1. Solving for k, we get k = n - 1. Now plug back in.
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
We want T(1). So we let n = 2k. Solving for k, we get k = logn. Now plug back in.
T(n) = n + nlogn
𝑛
3) 𝑇(𝑛) = 2𝑇 �2� + 1, 𝑇(1) = 1
================================================
T(n) = 2T(n/2) + 1 Substituting Equations
T(1) = 1 n → n/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
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) + 𝑛
𝑛(𝑛 + 1)
𝑇(𝑛) = = 𝑂(𝑛2 )
2