My professor gave me some practice problems to do. But they aren't homework or graded, it's meant to be practice for an upcoming quiz. I would ask my professor, but she's notorious for not being very helpful. But im struggling with this problem, and was hoping to get some help.
The problem is as follows:
T(n) = 1 if n = 1
T(n-1) + n(n-1) if n>= 2
She gives a hint to use a summation, and to assume n = 2^k
So far this is what I have:
T(n) = T(n-1) + n(n-1)
T(n-1) = T(n-2) + n-1(n-2)
T(n-2) = T(n-3) + n-2(n-3)
Therefore...
T(n) = T(n-3) + n(n-1) + n-1(n-2) + n-2(n-3)
this is about when I get stumped, but i tried to push forward and guess which summation to use.
T(n) = T(n-k) + (n(n+1)(2n+1))/6
Well, by telescoping the series, T(n) = 1 + sum(i(i-1) for i = 2..n). That's n(n-1)(n+1)/3 + 1 according to Wolfram Alpha.
Or if you want to do it by hand,
sum(i(i-1) for i = 2..n)
= sum(i(i-1) for i = 1..n)
= sum(i^2 for i = 1..n) - sum(i for i = 1..n)
And then use the well known equations for sums of consecutive squares, and sums of consecutive integers.
Related
I've got a problem where I forgot how to do recurrence relations and I'm not really sure what the guess and check method is. Does anyone know how to solve them using that method and if so a nice explanation would be appreciated.
2. Solve the following recurrence relation from guess and check method.
a. T(n) = T(n/2) + T(n/3) + T(n/10) + c n where c is a constant.
T(small number) = another small number.
b. T(n) = T(n/2) + T(n/3) + T(n/4) + T(n/5) + d n where d is a constant
T(small number) = another small number.
The guess method works by figuring out a possible answer and testing it. For a., if the function T does not grow too slowly, the T on the left could follow the last, linear term. So we try T(n) = an.
an = an/2 + an/3 + an/10 + cn
which indeed gives the solution
a = 15c.
For b., this fails as we get a negative coefficient, and we are a little stuck. We can try a larger power of n to increase the growth, and try an^α:
an^α = an^α/2^α + an^α/3^α + an^α/4^α + an^α/5^α + cn
After simplification,
1 = 1/2^α + 1/3^α + 1/4^α + 1/5^α + c/a n^(1-α)
Now by numerical resolution, we observe that with α = 1.2348, we have
0 ~ c/a n^(1-α)
which gives us an asymptotic solution.
How can I solve T(n) = T(n-3)+n^2 using iteration?By master theorem answer is O(n^3) but I am having trouble solving it by iteration.
By direct resolution of the recurrence:
This is a linear recurrence of the first order. We first solve the homogeneous part,
T(n) = T(n - 3)
which is solved by a constant (more precisely three constants as three intertwined sequences form the solution).
Now for the non-homogeneous part, we use the Ansatz T(n) = an³ + bn² + cn + d, because we know that the difference of two cubic polynomials is a quadratic one.
Then
a(n³ - (n-3)³) + b(n² - (n-3)²) + c(n - (n-3)) = 9an² + 3(-9a + 2b)n + 3(9a - 3b + c) = n²
gives
a = 1/9, b = 1/2, c = 1/2.
Finally
T(n) = (2n³ + 9n² + 9n)/18 + T(0)
and similarly for the two other sequences.
Just try to expand the equation:
T(n) = n^2 + (n-3)^2 + (n-6)^2 + ... + 1 = \Theta(n^3)
T(3) = T(0) + 3²
T(6) = T(3) + 6² = T(0) + 3² + 6²
T(9) = T(6) + 9² = T(0) + 3² + 6² + 9²
...
More generally, T(3N) is the sum of T(0) and nine times the sum of the squared naturals up to N. The well-known Faulhaber formula justifies O(N³).
Similar results hold for T(3N+1) and T(3N+2).
I tried recursion tree method since the master method is not applicable for this recurrence but it seems that it is not the right method also, any help would be appreciated !
Either I have an error somewhere in my derivation or there is an error in your statement.
You do this by unrolling the recursion:
T(n) = T(n/2) + T(n/4) = 2T(n/4) + T(n/8)
T(n) = 3T(n/8) + 2T(n/16)
T(n) = 5T(n/16) + 3T(n/32)
....
T(n) = F(i + 1)T(n/2^(i-1)) + F(i)T(n/2^i)
where F(i) if a Fibonacci number.
Using boundary condition T(n/2^i) = T(1) have n = 2^i -> i = log2(n).
T(n) = F(log2(n) + 1) T(2) + F(log2(n)) T(1) which is equal F(log2(n) + 1)
Now using this formula:
and stripping it to only phi^n (square root of 5 has nothing to do with complexity and the second thi^n -> 0 if n->inf) you will get:
T(n) = phi^(log2(n)+1) = phi * phi^log2(n) which is equal to O(n^log2(phi)), where log2(phi) = 0.694.
P.S. Look at it as a hint or a suggestion. Now you do not need college or a professor to learn something. Determination and perseverance is more important. Do not be afraid to try doing something. You already asked this question and claimed to try master method where you failed. People suggested you a completely different approach and here you claim that you tried completely the sam and have not tried the method that worked in a previous case.
How do I determine the running time (in terms of Big-Theta) for the algorithm of input size n that satisfies recurrence relation T(n) = T(n-1)+n where n >= 1 and with initial condition T(1) = 1?
Edit: I was practicing a past exam paper. Got stuck on this question. Need guidance
Look at it this way: T(n) = T(n-1) + n = T(n-2) + (n-1) + n = T(n-3) + (n-2) + (n-1) + n. Which means if n >= 1 then you will get something like T(n) = 1 + 2 + 3 + ... + n. If you work out the pattern of this series you will see that (n+1)n/2. Therefore, Ө(n^2)
I want to understand how to arrive at the complexity of the below recurrence relation.
T(n) = T(n-1) + T(n-2) + C
Given T(1) = C and T(2) = 2C;
Generally for equations like T(n) = 2T(n/2) + C (Given T(1) = C), I use the following method.
T(n) = 2T(n/2) + C
=> T(n) = 4T(n/4) + 3C
=> T(n) = 8T(n/8) + 7C
=> ...
=> T(n) = 2^k T (n/2^k) + (2^k - 1) c
Now when n/2^k = 1 => K = log (n) (to the base 2)
T(n) = n T(1) + (n-1)C
= (2n -1) C
= O(n)
But, I'm not able to come up with similar approach for the problem I have in question. Please correct me if my approach is incorrect.
The complexity is related to input-size, where each call produce a binary-tree of calls
Where T(n) make 2n calls in total ..
T(n) = T(n-1) + T(n-2) + C
T(n) = O(2n-1) + O(2n-2) + O(1)
O(2n)
In the same fashion, you can generalize your recursive function, as a Fibonacci number
T(n) = F(n) + ( C * 2n)
Next you can use a direct formula instead of recursive way
Using a complex method known as Binet's Formula
You can use this general approach described here.Please ask if you have more questions.
If you were also interested in finding an explicit formula for T(n) this may help.
We know that T(1) = c and T(2) = 2c and T(n) = T(n-1) + T(n-2) + c.
So just write T(n) and start expanding.
T(n) = T(n-1) + T(n-2) + c
T(n) = 2*T(n-2) + T(n-3) + 2c
T(n) = 3*T(n-3) + 2*T(n-4) + 4c
T(n) = 5*T(n-4) + 3*T(n-5) + 7c
and so on.
You see the coefficients are Fibonacci numbers themselves!
Call F(n) the nth Fibonacci number. F(n) = (phi^n + psi^n)/sqrt(5) where phi = (1+sqrt(5))/2 and psi = -1/phi, then we have:
T(n) = F(n)*2c + F(n-1)*c + (F(n+1)-1)*c
Here is some quick code to demonstrate:
def fib_gen(n):
"""generates fib numbers to avoid rounding errors"""
fibs=[1,1]
for i in xrange(n-2):
fibs.append(fibs[i]+fibs[i+1])
return fibs
F = fib_gen(50) #just an example.
c=1
def T(n):
"""the recursive definiton"""
if n == 1:
return c
if n == 2:
return 2*c
return T(n-1) + T(n-2) + c
def our_T(n):
n=n-2 #just because your intials were T(1) and T(2), sorry this is ugly!
"""our found relation"""
return F[n]*2*c + F[n-1]*c + (F[n+1]-1)*c
and
>>> T(24)
121392
>>> our_T(24)
121392
Is "worse than exponential" accurate enough for your purposes? The special case C=0 defines http://en.wikipedia.org/wiki/Fibonacci_number, which you can see from the article is exponential. Assuming C is positive, your series will be growing faster than this. In fact, your series will lie between the Fibonacci series and a variant of the Fibonacci series in which the golden ratio is replaced by something very slightly larger.
This type of recurrences are called: non-homogeneous recurrence relations and you have to solve in the beginning homogeneous recurrence (the one without a constant at the end). If you are interested, read the math behind it.
I will show you an easy way. Just type your equation in wolfram-alpha and you will get:
So the complexity grows in the same way as either Lucas or Fibonacci number (the bigger of them).
But both of them have the same growth rate:
and therefore your growth rate is an exponential of the golden ratio: O(phi^n)