recursion time complexity calculation - algorithm

Everyone.
I have quick question about one recurrence: T(n) = n^2 * T(n-1).
I am using "recursion-tree method" of CLRS, and got
T(n)=n(square) + (n-1)(square)*n + (n-2)(square)n(n-1) + (n-3)(square)n(n-1)*(n-2) + ...+1(square)*n!
I don't know how to summarize this expression to a upper bound.
Could some one help here

You seem to be overcomplicating things. If T(n) = n^2 * T(n - 1) is correct, you would simply have a product of squares:
(assuming the stopping condition is n = 1).

Related

How to simplify the summation of a recurrence relation

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..

Solve the recurrence of T(n) = 3T(n/5) + T(n/2) + 2^n

I am solving the recurrence of T(n) under the assumption that T(n) is contact for n <= 2. I started to solve this T(n) with the tree-method since we cannot use the master method here, but when I do the tree I am of course calculating the time C for this T(n) but my C-s are very non-trivial and weird, so I get for
c = 2^n and then for the next c I get ' 3 * 2^(n/5) + 2^(n/3)
And I don't how to solve with these values, is there anything that I am doing wrong or what procedure should I follow in order to solve this?
You might want to reduce the number of terms down as much as you can.
3 * 2^(n/5) + 2^(n/3) = 3 * (2^(1/5) * 2^n) + (2^(1/3) * 2^n)
Then combine all the coefficients together.
(3 * 2^(1/5)) * 2^n + (2^(1/3)) * 2^n
Notice that the common factor is 2^n. So you would get:
(3 * 2^(1/5) + 2^(1/3)) * 2^n
and I'm going to name the first part of the product as constant which
will give us:
constant * 2^n which is just T(2^n) because the constant is insignificant as the size of n gets very large.
You can simplify the case. As T(n) is increasing we know that T(n/2) > T(n/5). Hence, T(n) < 4T(n/2) + 2^n. Now, you can use master theorem, and say that T(n)=O(2^n). On the other hand, without this replacement, as there exists 2^n in T(n), we can say T(n) = \Omega(2^n). Therefore, T(n) = \Theta(2^n).

Recurence related to master theorem T(n)=T(n^(1/2))+1

In masters theorem were given a "plug-in" formula to find the big O, given it satisfies some condition.
However, what if we have problems like the following below? Can anyone show me how to do a step by step formula. And what topics would help me to know more about these types of questions. Assume that the person asking this question knows nothing about induction.
T(n)=T(n^(1/2))+1
T(n)=T(n-1) + 1
T(n)=T(n-1) + n^c , c is a natural number >1
T(n)= T(n-1) C^n, c is a natural number >1
You'll need to know a little math to do some of these. You can figure out what the recursion looks like when you expand it out all the way to the base case, e.g. for T(n) = T(n-1) + n^c you get T(n) = 1^c + 2^c + ... + n^c, but then you need to know some math in order to know that this is O(n^(c+1)). (The easiest way to see this is by bounding the sum above and below in terms of integrals of x^c). Similarly for T(n) = T(n-1) + c^n you easily get T(n) = c^1 + c^2 + ... + c^n but you again need to use some calculus or something to figure out that this is T(n) = O(c^n).
For T(n) = T(n^(1/2)) + 1 you need to count how many times you apply the recurrence before you get to the base case. Again math helps here. When you take square-root, the logarithm gets cut in half. So you want to know how many times you can cut the logarithm in half until you get to the base case. This is O(log log n).
You can expand upon the formula and work on it:
For example:
T(n) = T(n-1) + 1
T(n) = [T(n-2) + 1] + 1
...
T(n) = 1 + 1 + 1 ... (n times)
So T(n) = O(n).

What's the run time of: T(n) = 2T(n-1) + 3T(n-2)+ 1

I understand that it is similar to the Fibonacci sequence that has an exponential running time. However this recurrence relation has more branches. What are the asymptotic bounds of T(n) = 2T(n-1) + 3T(n-2)+ 1?
Usually, you will need to make some assumptions on T(0) and T(1), because there will be exponentially many of them and their values may determine the functional form of T(n). However in this case it doesn't seem to matter.
Then, recurrence relations of this form can be solved by finding their characteristic polynomials. I found this article: http://www.wikihow.com/Solve-Recurrence-Relations
I obtained the characteristic polynomial with roots 3 and 1, so that guesses the form T(n) = c_1*3^n + c_2. In particular, T(n) = 1/2*3^n - 1/4 satisfies the recurrence relation, and we can verify this.
1/2*3^n - 1/4 = 2*T(n-1) + 3*T(n-2) + 1
= 2*(1/2*3^(n-1) - 1/4) + 3*(1/2*3^(n-2) - 1/4) + 1
= 3^(n-1) - 1/2 + 1/2*3^(n-1) - 3/4 + 1
= 3/2*3^(n-1) - 1/4
= 1/2*3^n - 1/4
Hence it would give that T(n) = Theta(3^n). However, this may not be the only function that satisfies the recurrence and other possibilities will also depend on what you defined the values T(0) and T(1), but they should all be O(3^n).
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:
,
which is clearly an exponential complexity: O(3^n)

Substitution method for solving recurrences

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)

Resources