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.
Related
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.
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.
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?
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).
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)