I am trying to write the recurrence relation for the running time of the following function:
function G(n):
if n>0 then:
x=0
for i = 1 to n:
x = x + 1
G(n-1)
end if
What I came up with was:
T(n) = 1 if n <= 0
T(n) = T(n-1) + 1 if n>0
However I was told that this was incorrect and I don't know why or what the correct solution would be. Any help is greatly appreciated!
T(n) = 1 if n <= 0
T(n) = T(n-1) + O(n) if n>0
Instead of O(1), it should be O(n), because you are looping from 1 to n
If you solve the recurrence, the overall complexity will be O(n2)
T(n) = T(n-1)+n^c
T(n) = T(n-1)+c^n
where c is a constant
If you unroll the recursion, for the first case you will get:
1^c + 2^c + ... + (n-1)^c + n^c
which is a Faulhaber's formula. It tells you that the complexity is O(n^(c+1))
The second one is:
c^1 + c^2 + ... + c^(n-1) + c^n
which is the sum of geometrics and O(c^n)
How to solve the below recurrence relation?
T(n) = 2T(root(n)) + logn/loglogn if n > 4
T(n) = 1 if n <= 4
Preferably by master theorem otherwise by any method.
I know Master Theorem fails,But is there any extension for these type of problems?
Can you guide me any stuff for solving complex relation like above?
I think this should work :
if n = 2^m and T(2^m) = s(m) then
logn = m , loglogn = logm ;
s(m) = 2*s(m/2) + m/logm ;
now solving the above equation is our problem
now you can not use the master theorem for solving this , so you have to use other methods like expanding this equation by writing s(m/2) and s(m/4) and then you might solve this problem , and after doing that you change your parameters to n again .
According to me
if n = 2^m and T(2^m) = s(m) then
logn = m , loglogn = logm ;
s(m) = 2*s(m/2) + m/logm ;
Here's the question:
Solve the recurrence by obtaining a theta bound for T(n) given that T(1) = theta(1).
T(n) = n + T(n-3)
Attempted Solution:
T(n) = T(n-6) + (n-3) + n
= T(n-9) + (n-6) + (n-3) + n
= T(n-(n-1)) + [(n-n) + (n-(n-3)) + (n-(n-6)) + ... + n]
= T(1) + [0 + 3 + 6 + ... + n]
= theta(1) = 3[1 + 2 + 3 + ... + n/3]
= theta(1) + [(n/3)(n/3 + 1)]/2
= theta(1) + (n^2+3n)/6
When I double check to see if the solution fits the recurrence, it doesn't work.
The issue was that you were getting the wrong summation.
It doesn't start at 0, since your last T function was T(n - (n-1)) , which means previous one was T(n-(n-4)). So the summation starts at 4, and goes up till n.
If you don't know how to find the summation of this, I'd suggest you look at some of the proofs from the summation formula. This is what the solution looks like.
T(n) = T(n-3) + n
= T(n-6) + (n-3) + n
= T(n-(n-1)) + [ (n-(n-4)) + (n-(n-7)) + ... + n]
= T(1) + [4 + 7 + ... + n]
= theta(1) + (4 + n) * (n - 1)/6
I am trying to find the big O bound for the following recurrence relation:
T(n) = T(n-1) + n^c, where c >= 1 is a constant
So I've decided to solve this by using iteration:
T(n) = T(n-1) + n^c
T(n-1) = T(n-2) + (n-1)^c
T(n) = T(n-2) + n^c + (n-1)^c
T(n-2) = T(n-3) + (n-2)^c
T(n) = T(n-3) + n^c + (n-1)^c + (n-2)^c
T(n) = T(n-k) + n^c + (n-1)^c + .... + (n-k+1)^c
Suppose k = n-1, then:
T(n) = T(1) + n^c + (n-1)^c + (n-n+1+1)^c
T(n) = n^c + (n-1)^c + 2^c + 1
I'm not sure if this is correct however, plus I would really appreciate some guidance as to how to derive Big O from this. Thanks a lot!
Yes, you are correct:
T(n) = nc + (n-1)c + (n-2)c + … + 3c + 2c + 1,
and this sum is
T(n) = O(nc+1). See e.g. Faulhaber's formula. In fact, you can even determine the constant in the leading term (even if it's not germane to the algorithm's asymptotics): the sum is nc+1/(c+1) + O(c), as you can determine through e.g., using, say, integration.
What you have is not correct, but you were on the right track.
The mistake you made:
T(n) = T(n-3) + n^c + (n-1)^c + (n-2)^c
T(n) = T(n-k) + n^c + (n-1)^c + (n-k+1)^c
You cannot just go from the first line to the second line.
As you increase k, the number of terms in the right hand side increases too.
To see that think of writing it this way:
T(n) - T(n-1) = n^c.
T(n-1) - T(n-2) = (n-1)^c
..
T(n-k) - T(n-k-1) = (n-k)^c.
..
T(2) - T(1) = 2^c
What happens if you add these up?
Once you do that, can you see what the answer will be for c=1 and c=2? Can you figure out a pattern for the final answer from there?
Instead of working you way down from n, how about start by working your way up from 0 (I assume the recursion terminates at 0 and you left that bit out). When you start noticing a fixed point (ie a pattern which repeats the same in all cases) you have a good candidate for an answer. Try proving the answer, e.g. through induction.
I would start by observing that n^c, whilst of course influenced by the value of n, is not going to be any more complex for n vs. n + 1 - it's c that determines the "runtime" of that particular term. Given that, you can assume (at least I would) that the term has constant runtime and determine how many times the recursion will execute for a given n and you'll get your bound.
To figure these out, fill out a couple of terms and look for the pattern:
T(1) = 0 + 1^c
T(2) = T(1) + 2^c = 0 + 1^c + 2^c
T(3) = T(2) + 3^c = 0 + 1^c + 2^c + 3^c
T(4) = ...
Now express the pattern in terms of n and you have your answer.
Here it is:
T(n) = n^c + (n-1)^c + (n-2)^c + ... + 2^c + 1^c
< n^c + n^c + n^c + ... + n^c + n^c
= n * n^c
= n^(c+1)
which is O(nc+1).
To show this is a reasonable bound, note that when c = 1,
T(n) = n + (n-1) + (n-2) + ... + 2 + 1
= n * (n-1) / 2
which is clearly Θ(n2).