I am refreshing on Master Theorem a bit and I am trying to figure out the running time of an algorithm that solves a problem of size n
by recursively solving 2 subproblems of size n-1
and combine solutions in constant time.
So the formula is:
T(N) = 2T(N - 1) + O(1)
But I am not sure how can I formulate the condition of master theorem.
I mean we don't have T(N/b)
so is b
of the Master Theorem formula in this case b=N/(N-1)
?
If yes since obviously a > b^k
since k=0
and is O(N^z)
where z=log2
with base of (N/N-1)
how can I make sense out of this? Assuming I am right so far?

- 52,998
- 69
- 209
- 339
-
first, this is ill defined because T(1) needs to be defined, so let's define it as T1. second, this is actually 1st order linear recurrence relation. I am going to give you a hint. look at it this way: x[n] = a x[n-1] + c – thang Jan 20 '13 at 11:50
4 Answers
ah, enough with the hints. the solution is actually quite simple. z-transform both sides, group the terms, and then inverse z transform to get the solution.
first, look at the problem as
x[n] = a x[n-1] + c
apply z transform to both sides (there are some technicalities with respect to the ROC, but let's ignore that for now)
X(z) = (a X(z) / z) + (c z / (z-1))
solve for X(z) to get
X(z) = c z^2 / [(z - 1) * (z-a)]
now observe that this formula can be re-written as:
X(z) = r z / (z-1) + s z / (z-a)
where r = c/(1-a) and s = - a c / (1-a)
Furthermore, observe that
X(z) = P(z) + Q(z)
where P(z) = r z / (z-1) = r / (1 - (1/z)), and Q(z) = s z / (z-a) = s / (1 - a (1/z))
apply inverse z-transform to get that:
p[n] = r u[n]
and
q[n] = s exp(log(a)n) u[n]
where log denotes the natural log and u[n] is the unit (Heaviside) step function (i.e. u[n]=1 for n>=0 and u[n]=0 for n<0).
Finally, by linearity of z-transform:
x[n] = (r + s exp(log(a) n))u[n]
where r and s are as defined above.
so relabeling back to your original problem,
T(n) = a T(n-1) + c
then
T(n) = (c/(a-1))(-1+a exp(log(a) n))u[n]
where exp(x) = e^x, log(x) is the natural log of x, and u[n] is the unit step function.
What does this tell you?
Unless I made a mistake, T grows exponentially with n. This is effectively an exponentially increasing function under the reasonable assumption that a > 1. The exponent is govern by a (more specifically, the natural log of a).
One more simplification, note that exp(log(a) n) = exp(log(a))^n = a^n:
T(n) = (c/(a-1))(-1+a^(n+1))u[n]
so O(a^n) in big O notation.
And now here is the easy way:
put T(0) = 1
T(n) = a T(n-1) + c
T(1) = a * T(0) + c = a + c
T(2) = a * T(1) + c = a*a + a * c + c
T(3) = a * T(2) + c = a*a*a + a * a * c + a * c + c
....
note that this creates a pattern. specifically:
T(n) = sum(a^j c^(n-j), j=0,...,n)
put c = 1 gives
T(n) = sum(a^j, j=0,...,n)
this is geometric series, which evaluates to:
T(n) = (1-a^(n+1))/(1-a)
= (1/(1-a)) - (1/(1-a)) a^n
= (1/(a-1))(-1 + a^(n+1))
for n>=0.
Note that this formula is the same as given above for c=1 using the z-transform method. Again, O(a^n).

- 3,466
- 1
- 19
- 31
-
I apologize for the lamen question but what is ROC?Also I don't even remember what is a `z-transform`.But your solution is the same result as the textbook solution.So +1 – Cratylus Jan 20 '13 at 12:22
-
@Cratylus, I added the simple method, which is probably more in line with what you're learning. Seems you can just expand the terms and notice that it's a geometric series. That is by luck for this equation. It is not always so clean. The z-transform method would work for higher orders than 1 and with funky combos. For example, what if T(n) = a T(n-1) + b T(n-2) + c T(n-3) + d. Simple expansion doesn't always work out nicely. Regardless, good refresher for me. Haven't done this stuff in over 15 years. – thang Jan 20 '13 at 13:17
-
Seems to be the correct answer but I will have to study it to understand it.Too advanced for me – Cratylus Jan 20 '13 at 14:37
-
Don't even think about Master's Theorem. You can only use Masther's Theorem when you're given master's theorem when b > 1 from the general form T(n) = aT(n/b) + f(n).
Instead, think of it this way. You have a recursive call that decrements the size of input, n, by 1 at each recursive call. And at each recursive call, the cost is constant O(1). The input size will decrement until it reaches 1. Then you add up all the costs that you used to make the recursive calls. How many are they? n. So this would take O(2^n).

- 1,205
- 5
- 18
- 32
-
Corrected a typo in OP.Wrote `a` instead of `2` in the formula.Does this change your answer? – Cratylus Jan 20 '13 at 12:08
-
My OP is about how to apply the Master Theorem on this formula having `a` `b` and `k` the recurrence's values – Cratylus Jan 20 '13 at 12:15
-
As I explained above, you cannot directly apply the Master's Theorem's formula for this one. But you can draw a recursive tree from which Master's Theorem is based on and figure out the total time. – kchoi Jan 20 '13 at 12:16
-
+1 for pointing out that `b>1` in master theorem!I completely forgot that! – Cratylus Jan 20 '13 at 12:23
-
Sorry about my 2nd comment, it's actually O(2^n). It's because you make two recursive calls at each step. Then, the number of recursive calls will increase exponentially by the time n becomes 1. – kchoi Jan 20 '13 at 12:26
Looks like you can't formulate this problem in terms of the Master Theorem.
A good start is to draw the recursion tree to understand the pattern, then prove it with the substitution method. You can also expand the formula a couple of times and see where it leads.
See also this question which solves 2 subproblems instead of a
:
Time bound for recursive algorithm with constant combination time

- 1
- 1

- 6,699
- 2
- 25
- 31
-
Corrected a typo in OP.Wrote `a` instead of `2` in the formula.Does this change your answer? – Cratylus Jan 20 '13 at 12:08
May be you could think of it this way
when
n = 1, T(1) = 1
n = 2, T(2) = 2
n = 3, T(3) = 4
n = 4, T(4) = 8
n = 5, T(5) = 16
It is easy to see that this is a geometric series 1 + 2+ 4+ 8 + 16...
, the sum of which is
first term (ratio^n - 1)/(ratio - 1)
. For this series it is
1 * (2^n - 1)/(2 - 1) = 2^n - 1.
The dominating term here is 2^n
, therefore the function belongs to Theta(2^n)
. You could verify it by doing a lim(n->inf) [2^n / (2^n - 1)] = +ve constant.
Therefore the function belongs to Big Theta (2^n)

- 5,735
- 10
- 41
- 66

- 324
- 2
- 5