Big O Question - Algorithmic Analysis III - algorithm

I have the following question:
Solve the recurrence relation simplifying the answer using Big 'O' notation:
f(0) = 2
f(n) = 6f(n-1)-5, n>0
I know this is a first order inhomogenous recurrence relation and have had a go at the question but I cannot seem to get the right output for the base case (f(0) = 2).
The question MUST use the sum of geometric series forumla within the proof.
Here is my answer - Note sum(x = y, z) is a replacement for capital sigma notation, where x is the lower bound of the summation initialised to y and z is the upper bound of the summation:
1. *change forumla:*
2. f(n) = 6^n.g(n)
3. => 6^n.g(n) = 6.6^(n-1) .g(n-1) -5
4. => g(n) = g(n-1)-5/6^n
5. => g(n) = sum(i=1, n)-5/6^i
6. => f(n) = 6^n.sum(i=1, n)-5/6^i
7. => *Evaluate the sum using geometric series forumla*
8. => sum(i = 1, n)-5/6^i = [sum(i = 1, n)a^i] -------> (a = -5/6)
9. => *sub a = -5/6 into geometric forumla [a(1-a^n)/(1-a)]*
10. => [(-5/6(1 - (-5/6)^n))/(1-(-5/6))]
11. => g(n) = [(-5/6(1 + (5/6)^n))/(1+5/6)]
12. => f(n) = 6^n . g(n) = 6^n[(-5/6(1 + (5/6)^n))/(1+5/6)]
13. => *sub in n = 0 to see if f(0) = 2*
Firstly, I am sure the equation on line 11 can be simplified further and secondly subbing in n = 0 should yield 2 as the result. I cannot obtain this answer when reaching line 13...
EDIT: What I need to know is why I am not getting f(0) = 2 when subbing n = 0 into the equation in line 12. Also what I would like to know is how can I simplify the equation for f(n) in line 12?
Anyone...?

Without thinking too hard about this, I'm going to say that f(n + 1) is 6 times larger than f(n), minus a constant. f(n) is therefore certainly O(6^n). Although you may find a tighter bound, that's about as far as I'd go in practice!
For the fun of it, I'll try this:
f(1) = 6f(0) - 5
= 6^1.f(0)
f(2) = 6f(1) - 5
= 6(6f(0) - 5) - 5
= 6^2.f(0) - 6^1.5 - 5
f(3) = 6f(2) - 5
= 6^3.f(0) - 6^2.5 - 6^1.5 - 5
I'll hazard a guess that
f(n) = 6^n.f(0) - 5.(6^0 + 6^1 + ... + 6^(n-1))
and I'm pretty sure that I could prove this by induction in a few lines (exercise left as an exercise for the student).
Now,
sum (k in 0..n-1) 6^k = (1 - 6^n) / (1 - 6)
therefore
f(n) = 6^n.f(0) - 5.(1 - 6^n) / (1 - 6)
= 6^n.f(0) + (1 - 6^n)
= 6^n.(2 - 1) + 1
= 6^n + 1
confirming my earlier intuition.
Let's just do a few quick check calculations:
f(0) = 2 = 6^0 + 1
f(1) = 6.2 - 5 = 7 = 6^1 + 1
f(2) = 6.7 - 5 = 37 = 6^2 + 1
f(3) = 6.37 - 5 = 237 = 6^3 + 1
That's enough for me for homework :-)

Related

Summation of series 1 + (1+2+1) + (1+2+1+3+1+2+1)

The function is : F(n-1) n F(n-1)
Its a type of palindrome function called Zimmer Series.
The values would be : 1, 121, 1213121, ...
I want to figure the summation of the individual digits.
1 + (1+2+1) + (1+2+1+3+1+2+1) + ...
Any help is welcome.
Breaking this down into steps, we first find out a formula for the summation of a single value of the series and then we can find out the summation of said formula.
Expanding the definition you gave and manipulating it:
F(n) = n + 2F(n-1)
F(n) = n + 2(n-1) + 22(n-2) + 23(n-3) + ... + 2n-1
2F(n) = 2n + 22(n-1) + 23(n-2) + ... + 2n-1(2) + 2n
F(n) - 2F(n) = -F(n) = n - 2 - 22 - 23 - ... - 2n
From this and using the formula for Geometric Progression we can then get an expression for a single term of the series.
F(n) = (2n + 2n-1 + ... + 2) - n
= (2n+1 - 2) - n
Now we just have to work out the summation of this expression.
G(n) = Σ F(n) = Σ (2n+1 - 2 - n)
G(n) = (2n+2 - 22) - (2n) - (n(n+1)/2)
Simplifying this should hopefully give you the answer you seek!
G(n) = (2n+2 - (n(n+5)/2) - 22)
Trying this out on a few of the terms just to double check.
G(1) = (21+2 - (1(1+5)/2) - 22)
G(1) = 1
G(2) = (22+2 - (2(2+5)/2) - 22)
G(2) = 5 = 1 + (1 + 2 + 1)
G(3) = (23+2 - (3(3+5)/2) - 22)
G(3) = 16 = 1 + (1 + 2 + 1) + (1 + 2 + 1 + 3 + 1 + 2 + 1)
EDIT: Mark Dickinson is right, I misinterpreted the question, this solution is incorrect.
I think after the second term the sequence is of the form difference in Arithmetic Progression.
Let me show you how
Second Term = 1+2+1
Third Term = 1+2+1+3 + 1+2+1
Difference = 1+2+1+3 = 7
Third Term = 1+2+1+3+1+2+1
Fourth Term = 1+2+1+3+ 1+4+1+3 +1+2+1
Difference = 1+4+1+3 = 9
Fourth Term = 1+2+1+3+1+4+1+3+1+2+1
Fifth Term = 1+2+1+3+1+4+ 1+5+1+4 +1+3+1+2+1
Difference = 1+5+1+4 = 11
So as you can see the difference is in the arithmetic progression and you find the sum of the terms using the formula for the sum of numbers whose different are in Arithmetic Progression

solving recurrence relation with multiple T(n)s

T(n) = 1/2(T(n − 1) + T(n − 2)) + cn, with c > 0
I am having trouble understanding how to solve recurrences with multiple T(n)s. I did a lot of practices by solving recurrence with just one T(n) and following the definition I can do it well. But this is not a recurrence directly solvable with the Master theorem. Anyway I can start a good approach to this question?
solve the homogeneous recurrence:
T_H(n) = 1/2(T_H(n − 1) + T_H(n − 2))
r^2 - r/2 - 1/2 = 0
r = 1 or r = -1/2
T_H(n) = alpha * 1^n + beta * (-1/2)^n (alpha and beta to be determined by initial conditions)
solve the special solution
(1) we want to find a s(n) such that s(n) = 1/2(s(n-1)+s(n-2)) + cn
we know cn is a polynome (in n) so special solution can be found as a polynome too.
Trying with s(n) = an leads to:
an = 1/2(an-1 + an-2) + cn and all terms in an simplify themselves so try the next degree: s(n)=an^2 + bn
an^2 + bn = 1/2 (a(n-1)^2 + b(n-1) + a(n-2)^2 + b(n-2) ) + cn
developping everybody then identifying we get
a = c/3
b = 5c/9
A quick check if we don't trust our ability to make valid calculus:
since s(n) must be valid for all n, let's put arbitrarily n=2, c=7 and check whether s(2) still verifies (1) idem
n = 2, c=7
s(n)-1/2(s(n-1)+s(n-2))-cn ?= 0
below octave shows that indeed s(2) = 0
octave:1> n=2
n = 2
octave:2> c=7
c = 7
octave:3> c/3*n^2 + 5*c/9*n - 1/2*(c/3*(n-1)^2 + 5*c/9*(n-1) +c/3*(n-2)^2 + 5*c/9*(n-2))-c*n
ans = 0
Complexity
T(n) = T_H(n) + sp(n) = alpha + beta (-1/2)^n + c/3n^2 + 5c/9n
so T(n) is in O(n^2)

Solving recurrence relation effiency class?

I am calculating the effiency class of this
R(n) = 2R(n−1)+2.
with the base case of R(1) = 1
using backwards substitution.
My equations came out to
4R(n-2) + 6
8R(n-3) + 14
16R(n-4) +30
I don't see how i can get an equation from this... Is it incorrect?
Not a complete answer because this looks like homework, but let’s say that each step of the relation doubled in complexity, so f(1) = 1, f(2) = 2, f(3) = 4, f(4) = 8, and so on. What kind of growth is this? What is the equation for f(n)?
Now, suppose it was g(n) = 2g(n-1) + 1, with g(1) = 1. We see that the sequence is 1, 3, 7, 15, etc, which looks a lot like 2^n-1, so let’s check our intuition. For g(n) = a2^n + b, we get 2g(n-1) + 1 = 2[a2^(n-1) + b] + 1 = a2^n + 2b + 1. We solve g(n) = a2^n + b = a2^n + 2b + 1 for b and get b = -1. Now we need to solve for a using the boundary condition. g(1) = 1 = a2^1 - 1. In this case, a = 1 and therefore g = 2^n - 1, as we guessed.
Does this example show the method to use to solve your problem?

time complexity of following recurrence?

Find out the time complexity (Big Oh Bound) of the recurrence T(n) = T(⌊n⌋) + T(⌈n⌉) + 1.
How the time complexity of this comes out to be O(n)??
You probably ment T(n)=T(⌊n/2⌋)+ T(⌈n/2⌉) + 1.
Lets calculate first few values of T(n).
T(1) = 1
T(2) = 3
T(3) = 5
T(4) = 7
We can guess that T(n) = 2 * n - 1.
Lets prove that by mathematical induction
Basis
T(1) = 1
T(2) = 3
T(3) = 5
T(4) = 7
Inductive step
T(2*n) = T(⌊2*n/2⌋)+ T(⌈2*n/2⌉) + 1
= T(⌊n⌋)+ T(⌈n⌉) + 1
= (2*n - 1) + (2*n - 1) + 1
= 4*n - 1
= 2 * (2*n) - 1
T(2*n+1) = T(⌊(2*n+1)/2⌋)+ T(⌈(2*n+1)/2⌉) + 1
= T(n)+ T(n+1) + 1
= (2*n - 1) + (2*(n+1) - 1) + 1 =
= 4*n + 1 =
= (2*n+1)*2 - 1
Since both the basis and the inductive step have been proved, it has now been proved by mathematical induction that T(n) holds for all natural 2*n - 1.
T(n) = 2*n - 1 = O(n)
What you have currently does not make sense. Since n is usually taken to be a natural number, then n=⌊n⌋=⌈n⌉. The recurrence then reads: break down a problem of size n into two problems of size n and spend time 1 doing that. The two new problems you just created will be split in turn, and so on- all you are doing is creating more work for yourself.

Solving a recurrence relation

I'm not sure if this is the right place to post this, but the problem actually belongs to a programming assignment. This recursion is something I probably should know how to solve but Im having a bit of trouble with it.
Solve the recursion:
T(0) = 2;
T(n) = T(n-1) + 2;
Solution:
T(n) = 2(n+1)
Could someone please show me how they got to that solution?
Please not that its not the main part of the assignment to solve this particular problem.
You have to figure out what is solution and then you can use induction, to prove it.
To figure solution is simple.
Value is previous value + 2.
2, 2+2, 2+2+2, 2+2+2+2, 2+2+2+2+2, ...
Use induction to prove:
T(0) = 2
T(n) = T(n-1) + 2;
Solution
T(n) = 2(n+1)
Proof:
T(n) = T(n-1) + 2 => 2((n-1)+1) + 2 = 2(n+1)
Check for n=0
2(0+1)=2
End of proof
Try writing out the first few values - it should then be obvious.
Take T(5):
T(5)
|
+-> T(4) + 2
|
+-> T(3) + 2
|
+-> T(2) + 2
|
+-> T(1) + 2
|
+-> T(0) + 2
|
+-> 2
Now count the number of 2's that are added together for T(5).
Then try to figure out how many 2's would be added for T(n).
It's an arithmetic progression with ratio common difference 2.
The first term is T[0] = 2 and the ratio common difference is r = 2 so the n + 1th term (n + 1th because there are n + 1 numbers in 0, 1, 2, ..., n) is T[0] + r*(n + 1 - 1) = 2 + 2*n = 2*(n + 1).
No guessing required, just recognize it as an arithmetic progression.
Each time n decreases by one, 2 is added. This gives a variable term of 2n. Since T(0) is fixed at 2, this gives a constant term of 2. Adding them together gives 2n + 2, or 2(n + 1).
I'd solve it as follows:
Assume that T(n) = a*n + b for some a and b.
T(0) = 2. So a * 0 + b = 2, thus b = 2.
T(n) = T(n-1) + 2, so
a * n + b = (a * (n-1) + b) + 2 consequently
a * n + b = a * n - a + b + 2 and
0 = - a + 2, thus a = 2.
So we have T(n) = 2 * n + 2 = 2 (n+1).
This one is pretty straightforward to solve by hand as the other answers point out, but in case it's ever useful, Mathematica is pretty good solving recurrence relations like this.
Evaluating
RSolve[{T[0] == 2, T[n] == T[n-1] + 2}, T[n], n]
returns
{{T[n] -> 2 (1 + n)}}
It can, for example, find the closed form of the nth Fibonacci number as well:
RSolve[{F[1] == 1, F[2] == 1, F[n] == F[n-1] + F[n-2]}, F[n], n] //FunctionExpand
returns
{{F[n] -> (((1 + Sqrt[5])/2)^n - (2/(1 + Sqrt[5]))^n*Cos[n*Pi])/Sqrt[5]}}

Resources