Recurrences where masters method doesnt apply - recursion

Suppose you have a reoccurence defined by: T(n) = T(n/2) +1. How does one evaluate this without master's method? What I have so far:
T(n) = T(n/2) + 1
T(n/2) = T(n/4) + 1
T(n/4) = T(n/8) + 1
...
T(1) = 1
It looks like this would be O(logn). Is this the only way to do these problems where master
s theorem does not occur?

How you got this T(1) = 1?
Lets see:
T(0) = T(0/2) + 1 => 0 = 1!
So the T(x) function have an asymptote at x=0.
Please notice that we can redefine it to:
T(2^(x+1)) = T(2^x) + 1
=>
f(x+1) = f(x) + 1
=>
f(x) = x + a
=>
T(n) = Log2(n) + a(n)
where a(n) is a function with interval length 1

What makes you think the Master Method doesn't apply? We have:
T(n) = a T(n/b) + f(n)
with
a = 1, b = 2, f(n) = 1
We can see that
c = log_b a = log_2 1 = 0
f(n) = 1 = Theta(1) = Theta(n^0 log^0 n) = Theta(n^c log^k n)
So we can use the result that
T(n) = Theta(n^c log^(k+1) n) = Theta(n^0 log^(0+1) n) = Theta(log n)
Which is your result.

Related

Substitution method for solving reccurences

I have recently had trouble with understanding the substitution method for solving reccurences. I watched few on-line lectures about the problem, but sadly it did not tell me much (in one of them I heard that it is based on guessing, which made me even more confused) and I am looking for some tips. My objective is to solve three different reccurence functions using substitution method, find their time complexity and their values for T(32).
Function 1 is defined as:
T(1) = 1
T(n) = T(n-1) + n for n > 1
I started off by listing first few executions:
T(2) = T(2-1)+2 = 1+2
T(3) = T(3-1)+3 = 1+2+3
T(4) = T(4-1)+4 = 1+2+3+4
T(5) = T(5-1)+5 = 1+2+3+4+5
...
T(n) = 1+2+...+(n-1)+n = n(n+1)/2
Then I proved by induction, that T(1) = 1 using the formula for sum of the first n natural numbers, and then that it is also true for n+1. It was pretty clear to me, but I am not sure whether this is substitution method. Also knowing the formula T(n) = n(n+1)/2 I easily calculated T(32) = 528 and counted the time complexity, which is O(n^2).
In examples (2) and (3) I only need solution for n=2^k when k is a natural number, but it would be nice if you recommended me any articles showing how to get these for all n as well (but I suppose it is way harder than that).
Function 2 is defined as:
T(1) = 0
T(n) = T(n/2) + 1 for even n > 1
T(n) = T((n+1)/2) + 1 for odd n > 1
As I was allowed to prove it only for n=2^k and based on my gained knowledge I tried to do it following way:
T(n) = T(n/2) + 1
= T(n/4) + 1 + 1 = T(n/4) + 2
= T(n/8) + 1 + 2 = T(n/8) + 3
= T(n/16) + 1 + 3 = T(n/16) + 4
= T(n/2^i) + i // where i <= k, according to tutorials
And this is the moment where I get stuck and I cannot proceed further. I suppose that my calculations are correct, but I am not sure how should I look for a formula, which would satisfy this function. After I get the right formula, calculating T(32) or time complexity will not be a problem.
Function 3 is defined as:
T(1) = 1
T(n) = 2T(n/2) + 1 for even n > 1
T(n) = T((n – 1)/2) + T((n+1)/2) + 1 for odd n > 1
My calculations:
T(n) = 2T(n/2) + 1
= 2(2T(n/4)+1) + 1 = 4T(n/4) + 3
= 4(2T(n/8)+1) + 3 = 8T(n/8) + 7
= iT(n/2^i) + 2^i - 1
And again it comes to the formula, which I am not sure how should be rewritten.
Basically, does substitution method for solving reccurences means finding and iterative formula?
After restudying the topic I found what I did wrong and not to leave my question unanswered, I will try to do it.
The first function is calculated well, induction proof is also correct - nothing to add here.
When it comes to the second function where I got stuck, I did not
pay attention that I was actually using a substitution n=2^k. This
is how it should look:
T(n) = T(n/2) + 1
= T(n/4) + 1 + 1 = T(n/4) + 2
= T(n/8) + 1 + 2 = T(n/8) + 3
= T(n/16) + 1 + 3 = T(n/16) + 4
= T(n/2^k) + k
= T(1) + k
= k
Induction proof that T(2^k) = k works:
Base case: k=1, then T(2^1) = T(2) = 1. (it cannot be k=0, as 2^0 is not bigger than 1)
Inductive step: assume T(2^k) = k, we want to show T(2^(k+1)) = k+1. As 2^k=n, then 2^(k+1) = 2*2^k = 2n.
T(2n) = T(n) + 1
= T(n/2) + 1 + 1
= T(n/4) + 2 + 1
= T(n/8) + 3 + 1
= T(1) + k + 1
= k + 1.
Time complexity: O(log n)
T(32) = T(2^5) = 5
In the third function I missed that every time the function called
itself, the value has doubled.
T(n) = 2T(n/2) + 1
= 2(2T(n/4)+1) + 1 = 4T(n/4) + 3
= 4(2T(n/8)+1) + 3 = 8T(n/8) + 7
= 8(2T(n/16)+1) + 7 = 16T(n/16) + 15
= 16(2T(n/32)+1) + 15 = 32T(n/32) + 31
= 2^k*T(n/2^i) + 2^k - 1
= 2^k*T(1) + 2^k - 1
= 2^k + 2^k - 1
= 2^(k+1) - 1
Induction proof that T(2^k) = 2^(k+1)-1 works:
Base case: k=1, then T(2^1) = T(2) = 3. Original function T(2) = 2T(1)+1 = 2+1 = 3, so base case is true.
Inductive step: assume T(2^k) = 2^(k+1)-1, we want to show T(2^(k+1)) = 2^(k+2)-1. Similarly, as in the second function, 2^k=n, so 2^(k+1) = 2*2^k = 2n.
T(2n) = 2T(n) + 1
= 2(2T(n/2)+1) + 1 = 4T(n/2) + 3
= 4(2T(n/4)+1) + 1 = 8T(n/4) + 7
= 8(2T(n/8)+1) + 1 = 16T(n/8) + 15
= 2^(k+1) + 2^(k+1) - 1
= 2*2^(k+1) - 1
= 2^(k+2) - 1.
We could also take a look at first few elements for T(n), which are 1, 3, 5, 7, 9, etc., so T(n) = 2n-1
Time complexity: O(n)
T(32) = T(2^5) = 2^(5+1) - 1 = 64 - 1 = 63

computational complexity of T(n)=T(n-1)+n

I have to calculate the computational complexity and computational complexity class of T(n) = T(n-1) + n.
My problem is that I don't know any method to do so and the only one I'm familiar with is universal recursion which doesn't apply to this task.
T(0) = a
T(n) = T(n-1) + n
n T(n)
---------
0 a
1 T(1-1) + n = a + 1
2 T(2-1) + n = a + 1 + 2
3 T(3-1) + n = a + 1 + 2 + 3
...
k T(k-1) + n = a + 1 + 2 + ... + k
= a + k(k+1)/2
Guess T(n) = O(n^2) based on the above. We can prove it by induction.
Base case: T(1) = T(0) + 1 = a + 1 <= c*1^2 provided that c >= a + 1.
Induction hypothesis: assume T(n) <= c*n^2 for all n up to and including k.
Induction step: show that T(k+1) <= c*(k+1)^2. We have
T(k+1) = T(k) + k + 1 <= c*k^2 + k + 1
<= c*k^2 + 2k + 1 // provided k >= 0
<= c*(k^2 + 2k + 1) // provided c >= 1
= c*(k+1)^2
We know k >= 0 and we can choose c to be the greater of a+1 and 1, which must reasonably be a+1 since T(0) is nonnegative.

Asymptotic time complexity of recursive function (Theta)

I have been asked to analyze the asymptotic time complexity of the following recursion function:
for-all k ≥ 1:
T(n) = n + T(n/2) + T(n/4) + T(n/8) + .... + T(n/2^k)
I was able to prove that:
T(n) = O(n⋅log n) and T(n) = Ω(n),
but I am looking for a tighter bound (Big Theta).
First of all:
I understand "for-all k >= 1" this way: for k = 1 to k = m where 2m-1 ≤ n ≤ 2m.
So basicly m = log₂(n) holds.
Have a look at my calculation:
T(n) = n + Σk=1,...,m T(n/2k)
= n + T(n/2) + Σk=2,...,m T(n/2k)
= n + n/2 + 2⋅Σk=2,...,m T(n/2k)
= ...
= n + Σk=1,...,m k⋅n/2k
= n + n⋅Σk=1,...,m k/2k
= n + n⋅(2 - 2-mm - 21-m)
≤ n + 2⋅n
= 3n
So T(n) is in Θ(n).
Notice:
You can also approximate Σk=1,...,m k/2k by the integral s(m) = ∫1m k/2k dk.
And here limm → ∞s(m) = 2 also holds.

What is the time complexity of the recurrence T(n) = 2T(n-1) + 4

What is the time complexity of the recurrence T(n) = 2T(n-1) + 4 ?
I'm having serious problems with this.
I tried:
T(n) = 2T(n-1)+4 = 2(2T(n-2)+4)+4 = 4T(n-2)+12= 4(2T(n-3)+4)+4 = 8T(n-3)+20 = 8(2T(n-4)+4)+4 =
16T(n-4)+36 =…
T(n) = 2^kT(n-k) + (4+2^(k+1))
so it looks like T(n) = 2^n + (4+2^(n+1)) but it doesn't seem right... please help :(
Your computation are wrong. I'm assuming here that T(0)=0
T(n) = 2T(n-1)+ 4
= 2(2T(n-2)+4)+ 4 = 4T(n-2)+ 12
= 4(2T(n-3)+4)+ 12 = 8T(n-3)+ 28
= 8(2T(n-4)+4)+ 28 = 16T(n-4)+ 60
= 16(2T(n-5)+4)+ 60 = 32T(n-5)+124
= 32(2T(n-6)+4)+124 = 64T(n-6)+252
Then now: look at the sequence
0,4,12,28,60,124,252,508,1020,2044,...
It is very tempting to add 4 to all these numbers:
4,8,16,32,64,128,256,512,1024,2048,...
Do you recognize it ? So the guess is clearly
T(n) = 2^(n+2) - 4
Now, you can easily prove it by induction.
By the way if T(0) is not equal to 0 the formula is
T(n) = 2^(n+2) - 4 + T(0)*2^n
Solving the recurrence relation, I found this:
T(n) = 2T(n-1) + 1
Let n = log m then, we get T(log m)=2T(log m - log 2) + 1
Assume S(a) = 2S(a/2) + 1
Applying Master's theorem we get:
n^logb^a = n^log2^2 = n.
n^log2^2-1 = 1, here € = 1
By first case we get: S(a) = O(a)
therefore T(n) = O(2^n)

How can I calculate the exact worst-case running time of a function given by a recurrence?

I am trying to calculate the value of the running time at the worst case for a function whose worst-case runtime is given by this recurrence:
T(0) = 0
T(n) = n + T(n - 1) (if n > 0)
Does anyone have any advice how to do this? I don't see how to solve this.
It might help to try expanding out the recurrence to see if you spot a pattern:
T(0) = 0
T(1) = 1 + T(0) = 1 + 0
T(2) = 2 + T(1) = 2 + 1 + 0
T(3) = 3 + T(2) = 3 + 2 + 1 + 0
Based on this pattern, it looks like T(n) = 0 + 1 + 2 + ... + n. This is a famous summation worked out by Gauss, and it solves to n(n+1)/2. Therefore, we could conjecture that T(n) = n(n+1)/2.
You can formalize this by proving it by induction. As a base case, T(0) = 0 = 0(0+1)/2, so everything checks out. For the inductive step, assume T(n) = n(n+1)/2. Then T(n+1) = (n+1) + T(n) = (n+1) + n(n+1)/2 = (n+1) (1 + n / 2) = (n+1)(n+2)/2 = ((n+1))((n+1) + 1) / 2, which checks out as well.
Therefore, your exact value is T(n) = n(n+1)/2.
Hope this helps!

Resources