Apply the Substitution Method to the following equation . T(n) = n* T(n-1)
Apply the Substitution Method to the following equation . T(n) = n* T(n-1)
Related
I'm having difficulties solving this recurrence relation: T(n)=16T(n/4)-2n^2. From my understanding I can't use the master theorem because f(n) is only allowed to be positive. Because of that I tried using the recurrence method:
T(n) = 16T(n/4)-2n^2
T(n) = 16(16T(n/16)-2(n/4)^2)-2n^2
T(n) = 16(16(16T(n/64)-2(n/16)^2)-2(n/4)^2)-2n^2
So this brings me to:
T(n) = 16^i * T(n/4^i) - (i*2*n^2)
T(n) terminates when n/4^i=1 so i = log4(n)
When I insert this into the equation I get:
T(n) = 16^log4(n) - (2*n^2*log4(n))
So would this bring me to a complexity of
T(n) = θ(n^2*log4(n)) ?
Thanks so much!
After solving the recurrence relation T(n) = 3T(n/3) + nlogn
I get following equation: T(n)=3kT(n/3k)+ nlogn + nlog(n/3) + nlog(n/3^2) …nlog(n/3^k)
How can I simplify the summation and how to know the asymptotic function?
log a + log b = log(a*b), so something like
T(n) = 3kT(n/3k) + n^k*logn(\Pi_k(n/(3^k)))
product of 1/(3^k) (\Pi_k(1/(3^k))) is
3^(-1/2k(k+1))
so
it simplifies
the logs become the single term n*log{3^(-1/2k(k+1)) * n^(k+1)}
that takes care of the simplification i gues..
I am trying to find the runtime of the following recurrence using iterative substitution:
T(n) = T(n/2) + T(n/3) + n
The issue is that there are two T(n/x) terms and finding general form for this case has proven to be quite challenging.
Is there a general guideline one should follow using iterative substitution for cases like this?
This recurrence is from the class of Akra–Bazzi recurrences . Following the formula the solution is:
Alternatively, suppose that T(1) = c0 then you can prove that T(n) <= max(6,c0)*n by induction.
You can also use the substitution rule. Here's how:
T(n) = T(n/2)+T(n/3) + n =
= n+(n/2+n/3)+T(n/(2*2))+T(n/(2*3))+T(n/(3*2))+T(n/(3*3))
= n+(n/2+n/3)+(n/(2*2)+n/(2*3)+n/(3*2)+n/(3*3))
+T(n/(2*2*2))+T(n/(2*2*3))
+T(n/(2*3*2))+T(n/(2*3*3))
+T(n/(3*2*2))+T(n/(3*2*3))
+T(n/(3*3*2))+T(n/(3*3*3))=
...
= n * (1 + 5/6 + (5/6)^2 + (5/6)^3 + (5/6)^4 + ...)
= 6 * n (assuming n = 2^k3^k. you get < 6*n otherwise)
Nothing formal here, but
T(n) = 2T(n/2) + n // O(nlog(n))
So your recurrence might still be O(nlog(n))?
Also what is the base case?
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)
First of all sorry for asking such a basic question.
But I am having difficulties understanding substitution method for solving recurrences.I am following Introduction to Algo.s -CLRS. As I am not able to find enough examples and ambiguity is the main concern.Especially the induction step.In the text books we have to prove f(n) implies f(n+1) but in CLRS this step is missing or may be I am not getting the example. Please explain step by step how to prove that O(n^2) is the solution for recurrence function T(n)=T(n-1)+n
Its the general steps of substitution method that I want to understand. If you could shed some light on strong mathematical induction and provide links to material on substitution method that'll be helpful also.
In substitution method, simply replace any occurance of T(k) by T(k-1) + k, and do it iteratively.
T(n) = T(n-1) + n =
= (T(n-2) + (n-1)) + n =
= T(n-3) + (n-2) + (n-1) + n =
= ... =
= 1 + 2 + ... + n-1 + n
From sum of arithmetic progression, you can get that T(n) is in O(n^2).
Note that substitution method is usually used to get an intuition on what the complexity is, to formally prove it - you will probably need a different tool - such as mathematical induction.
The formal proof will go something like that:
Claim: T(n) <= n^2
Base: T(1) = 1 <= 1^2
Hypothesis: the claim is true for each `k < n` for some `n`.
T(n) = T(n-1) + n <= (hypothesis) (n-1)^2 + n = n^2-2n + 1 < n^2 (for n > 1)