Which of the following can be recurrence relation(s) corresponding to an algorithm with time complexity Θ(n) ?
📖 Explanation
We need to find which recurrence relations result in a time complexity of Θ(n). Let's analyze each option:
Option A: T(n)=T(n−1)+1, T(1)=1
This recurrence describes an algorithm where the problem size reduces by 1 at each step, and a constant amount of work (+1) is done.
Expanding the recurrence:
T(n)=T(n−1)+1
T(n)=(T(n−2)+1)+1=T(n−2)+2
T(n)=(T(n−3)+1)+2=T(n−3)+3
...
T(n)=T(1)+(n−1)
Given T(1)=1, we substitute this:
T(n)=1+(n−1)=n.
Therefore, the time complexity is Θ(n).
Option B: T(n)=2T(n/2)+1, T(1)=1
This recurrence represents dividing the problem into 2 subproblems of half the size, with a constant amount of work (+1) performed at each step. We use the Master Theorem, which compares f(n) with nlogba.
Here, a=2, b=2, and f(n)=1.
First, calculate nlogba: nlog22=n1=n.
Now, compare f(n)=1 with n. We see that f(n)=O(n1−ϵ) for ϵ=1. This matches Case 1 of the Master Theorem.
According to Case 1, T(n)=Θ(nlogba)=Θ(n).
Thus, the time complexity is Θ(n).
Option C: T(n)=2T(n/2)+n, T(1)=1
This recurrence also involves dividing the problem into 2 subproblems of half the size, but the work done at each step is proportional to n.
Using the Master Theorem: a=2, b=2, and f(n)=n.
Calculate nlogba: nlog22=n1=n.
Comparing f(n)=n with nlogba=n, we find that f(n)=Θ(nlogba). This matches Case 2 of the Master Theorem.
According to Case 2, T(n)=Θ(nlogbalogn)=Θ(nlogn).
Therefore, the time complexity is not Θ(n).
Option D: T(n)=T(n−1)+n, T(1)=1
This recurrence describes an algorithm where the problem size decreases by 1 and n amount of work is done at each step.
Expanding the recurrence:
T(n)=T(n−1)+n
T(n)=(T(n−2)+(n−1))+n=T(n−2)+(n−1)+n
...
T(n)=T(1)+2+3+...+n
Given T(1)=1, we substitute this:
T(n)=1+∑i=2ni=∑i=1ni=2n(n+1).
Thus, the time complexity is Θ(n2), which is not Θ(n).