Recurrence Relation Homework Struggles - recurrence

Here's the question:
Solve the recurrence by obtaining a theta bound for T(n) given that T(1) = theta(1).
T(n) = n + T(n-3)
Attempted Solution:
T(n) = T(n-6) + (n-3) + n
= T(n-9) + (n-6) + (n-3) + n
= T(n-(n-1)) + [(n-n) + (n-(n-3)) + (n-(n-6)) + ... + n]
= T(1) + [0 + 3 + 6 + ... + n]
= theta(1) = 3[1 + 2 + 3 + ... + n/3]
= theta(1) + [(n/3)(n/3 + 1)]/2
= theta(1) + (n^2+3n)/6
When I double check to see if the solution fits the recurrence, it doesn't work.

The issue was that you were getting the wrong summation.
It doesn't start at 0, since your last T function was T(n - (n-1)) , which means previous one was T(n-(n-4)). So the summation starts at 4, and goes up till n.
If you don't know how to find the summation of this, I'd suggest you look at some of the proofs from the summation formula. This is what the solution looks like.
T(n) = T(n-3) + n
= T(n-6) + (n-3) + n
= T(n-(n-1)) + [ (n-(n-4)) + (n-(n-7)) + ... + n]
= T(1) + [4 + 7 + ... + n]
= theta(1) + (4 + n) * (n - 1)/6

Related

Show full calculation in Mathematica to copy into latex

I'm new to Mathematica I was hoping that this is a functionality built in. I am calculating the conditional entropy of a table and have the following 4 variables:
a = 1/8*Log[(1/8)/(1/2)] + 1/16*Log[(1/16)/(1/2)] + 1/16*Log[(1/16)/(1/2)] + 1/4*Log[(1/4)/(1/2)]
b = 1/16*Log[(1/16)/(1/4)] + 1/8*Log[(1/8)/(1/4)] + 1/32*Log[(1/32)/(1/4)] + 0
c = 1/32*Log[(1/32)/(1/8)] + 1/32*Log[(1/32)/(1/8)] + 1/16*Log[(1/16)/(1/8)] + 0
d = 1/32*Log[(1/32)/(1/8)] + 1/32*Log[(1/32)/(1/8)] + 1/32*Log[(1/32)/(1/8)] + 0
Then the final calculation is:
a + b + c + d
I was hoping there was a way to display the expanded a + b + c + d as a output so I can right-click/copy as latex to past into a document so I don't have to type it out. I can't figure out how to do it though. I'm guessing there is a way to expand those variables in the output to show the full calculation but maybe it is not possible. Thanks for any help.
I'm not sure how expanded you wanted the mathematical expression to be. See if the following expression is in the form you want to convert to LaTeX: (1/16 Log[1/(16/4)]+1/8 Log[1/(8/4)]+1/32 Log[1/(32/4)]+0)+(1/32 Log[1/(32/8)]+1/32 Log[1/(32/8)]+1/16 Log[1/(16/8)]+0)+(1/32 Log[1/(32/8)]+1/32 Log[1/(32/8)]+1/32 Log[1/(32/8)]+0)+(1/8 Log[1/(8/2)]+1/16 Log[1/(16/2)]+1/16 Log[1/(16/2)]+1/4 Log[1/(4/2)]).
You can get that by using HoldForm on the right-hand side of your assignments. Then use TeXForm[a+b+c+d] to convert:
a = HoldForm[
1/8*Log[(1/8)/(1/2)] + 1/16*Log[(1/16)/(1/2)] +
1/16*Log[(1/16)/(1/2)] + 1/4*Log[(1/4)/(1/2)]];
b = HoldForm[
1/16*Log[(1/16)/(1/4)] + 1/8*Log[(1/8)/(1/4)] +
1/32*Log[(1/32)/(1/4)] + 0];
c = HoldForm[
1/32*Log[(1/32)/(1/8)] + 1/32*Log[(1/32)/(1/8)] +
1/16*Log[(1/16)/(1/8)] + 0];
d = HoldForm[
1/32*Log[(1/32)/(1/8)] + 1/32*Log[(1/32)/(1/8)] +
1/32*Log[(1/32)/(1/8)] + 0];
TeXForm[a + b + c + d]

Regular expression from this dfa

what will be regular express of this transition graph anyone?can make and explain this please i will be very thankful to him.
We can write some equations for this:
(q0) = e + (q1)a + (q3)b
(q1) = (q0)a + (q2)b
(q2) = (q1)b + (q3)a
(q3) = (q0)b + (q2)a
You can read these equations like this: "the set of strings that leads me to state X is the set of strings that lead me to state Y followed by symbol c, or the set of strings that lead me to state Z followed by symbol d, or..."
We can now solve these equations using substitution and a rule to eliminate self-references, namely:
if (q) = (q)x + y, then (q) = y(x*)
We can start solving the system by eliminating (q3):
(q0) = e + (q1)a + [(q0)b + (q2)a]b
(q1) = (q0)a + (q2)b
(q2) = (q1)b + [(q0)b + (q2)a]a
Distributing:
(q0) = e + (q1)a + (q0)bb + (q2)ab
(q1) = (q0)a + (q2)b
(q2) = (q1)b + (q0)ba + (q2)aa
We can get rid of (q1) now:
(q0) = e + [(q0)a + (q2)b]a + (q0)bb + (q2)ab
(q2) = [(q0)a + (q2)b]b + (q0)ba + (q2)aa
Distributing:
(q0) = e + (q0)aa + (q2)ba + (q0)bb + (q2)ab
(q2) = (q0)ab + (q2)bb + (q0)ba + (q2)aa
Grouping like terms:
(q0) = e + (q0)(aa + bb) + (q2)(ab + ba)
(q2) = (q0)(ab + ba) + (q2)(aa + bb)
Using the rule to remove self-reference:
(q0) = (aa + bb)* + (q2)(ab + ba)(aa + bb)*
(q2) = (q0)(ab + ba)(aa + bb)*
Substituting to get rid of (q2):
(q0) = (aa + bb)* + [(q0)(ab + ba)(aa + bb)*](ab + ba)(aa + bb)*
Distributing:
(q0) = (aa + bb)* + (q0)(ab + ba)(aa + bb)*(ab + ba)(aa + bb)*
Using the rule for self-reference:
(q0) = (aa + bb)*[(ab + ba)(aa + bb)*(ab + ba)(aa + bb)*]*
from the given transition diagram or DFA, equations for each state can be written as:
q0 = ε+q1.a+q3.b
q1 = q0.a+q2.b
q2 = q1.b+q3.a
q3 = q0.b+q2.a
substitute q3 in q2,
q2=q1.b+(q0.b+q2.a)a
q2=q2.aa+q0.ba+q1.b
substitute q1 in q2
q2=q2.aa+q0.ba+(q0.a+q2.b)b
q2=q2.aa+q0.ba+q0.ab+q2.bb
finally,
q2=q2(aa+bb)+q0(ba+ab)
It is in the form of t=tr+s
According to Arden's theorem,
the solution is t=sr*
So,q2=q0(ba+ab)(aa+bb)
Now substitute q3 in the equation q0,
q0=ε+q1.a+(q0.b+q2.a)b
q0=ε+q1.a+q0.bb+q2.ab
q0=q0.bb+(q1.a+q2.ab+ε)
substitute q1 in q0,
q0=q0.bb+(q0.a+q2.b)a+q2.ab+ε
q0=q0.bb+q0.aa+q2.ba+q2.ab+ε
q0=q0(bb+aa)+q2(ba+ab)+ε
Now substitute the q2 in the above equation,
q0=q0(bb+aa)+q0(ba+ab)(aa+bb)*(ba+ab)+ε
q0=q0((bb+aa)+(ba+ab)(aa+bb)*(ba+ab))+ε
Now q0 is in the form of t=tr+s so the solution is t=sr*
q0=((bb+aa)+(ba+ab)(aa+bb)(ab+ba))
The Regular Expression for the given DFA is:
q0=((bb+aa)+(ba+ab)(aa+bb)*(ab+ba))

How to solve the following recurrences and find a Theta bound

T(n) = T(n-1)+n^c
T(n) = T(n-1)+c^n
where c is a constant
If you unroll the recursion, for the first case you will get:
1^c + 2^c + ... + (n-1)^c + n^c
which is a Faulhaber's formula. It tells you that the complexity is O(n^(c+1))
The second one is:
c^1 + c^2 + ... + c^(n-1) + c^n
which is the sum of geometrics and O(c^n)

Recurrence relation: T(n) = T(n - 1) + n - 1

I am trying to understand how to solve recurrence relations. I understand it to the point where we have to simplify.
T(N) = T(N-1) + N-1 Initial condition: T(1)=O(1)=1
T(N) = T(N-1) + N-1
T(N-1) = T(N-2) + N-2
T(N-2) = T(N-3) + N-3
……
T(2) = T(1) + 1
**Summing up right and left sides**
T(N) + T(N-1) + T(N-2) + T(N-3) + …. T(3) + T(2) =
= T(N-1) + T(N-2) + T(N-3) + …. T(3) + T(2) + T(1) +
(N-1) + (N-2) + (N-3) + …. +3 + 2 + 1
** Canceling like terms and simplifying **
T(N) = T(1) + N*(N-1)/2 1 + N*(N - 1)/2
T(N) = 1 + N*(N - 1)/2
I really don't understand the last part. I understand canceling like terms but don't understand how the simplification below works:
T(N) = T(1) + (N-1) + (N-2) + (N-3) + …. +3 + 2 + 1
T(N) = T(1) + N*(N-1)/2 1 + N*(N - 1)/2
How is the second line derived from the first? Doesn't make any sense to me.
Would be a great help if someone can help me understand this. Thanks =)
In your second-to-last-line:
S = (N-1) + (N-2) + (N-3) + ... + 3 + 2 + 1
You can say:
2S = S + S
= (N-1) + (N-2) + (N-3) + ... + 3 + 2 + 1
1 + 2 + 3 + ... + (N-3) + (N-2) + (N-1)
= N + N + N + ... + N + N + N
|__________________ N-1 times ________________|
You're counting from N - 1 to 1, so there are N - 1 terms in the sequence. But the whole sequence is just N so you can say:
2S = N * (N - 1)
S = (N * (N - 1)) / 2
So in your last chunk:
T(N) = T(1) + (N-1) + (N-2) + (N-3) + ... + 3 + 2 + 1
= T(1) + (N * (N - 1)) / 2
T(N) = T(1) + (N-1) + (N-2) + (N-3) + …. +3 + 2 + 1
= T(1) + (N-1) + (N-2) + (N-3) + ..... + ( N-(N-3)) + (N-(N-2)) + (N-(N-1))
= T(1) + [N+N+N+..... n-1 times] - [1+2+3+......+(N-3)+(N-2)+(N-1)]
= T(1) + N*(N-1) - (N*(N-1))/2
= T(1) + N*(N-1)/2

Recurrence Relation: Finding Big O

I am trying to find the big O bound for the following recurrence relation:
T(n) = T(n-1) + n^c, where c >= 1 is a constant
So I've decided to solve this by using iteration:
T(n) = T(n-1) + n^c
T(n-1) = T(n-2) + (n-1)^c
T(n) = T(n-2) + n^c + (n-1)^c
T(n-2) = T(n-3) + (n-2)^c
T(n) = T(n-3) + n^c + (n-1)^c + (n-2)^c
T(n) = T(n-k) + n^c + (n-1)^c + .... + (n-k+1)^c
Suppose k = n-1, then:
T(n) = T(1) + n^c + (n-1)^c + (n-n+1+1)^c
T(n) = n^c + (n-1)^c + 2^c + 1
I'm not sure if this is correct however, plus I would really appreciate some guidance as to how to derive Big O from this. Thanks a lot!
Yes, you are correct:
T(n) = nc + (n-1)c + (n-2)c + … + 3c + 2c + 1,
and this sum is
T(n) = O(nc+1). See e.g. Faulhaber's formula. In fact, you can even determine the constant in the leading term (even if it's not germane to the algorithm's asymptotics): the sum is nc+1/(c+1) + O(c), as you can determine through e.g., using, say, integration.
What you have is not correct, but you were on the right track.
The mistake you made:
T(n) = T(n-3) + n^c + (n-1)^c + (n-2)^c
T(n) = T(n-k) + n^c + (n-1)^c + (n-k+1)^c
You cannot just go from the first line to the second line.
As you increase k, the number of terms in the right hand side increases too.
To see that think of writing it this way:
T(n) - T(n-1) = n^c.
T(n-1) - T(n-2) = (n-1)^c
..
T(n-k) - T(n-k-1) = (n-k)^c.
..
T(2) - T(1) = 2^c
What happens if you add these up?
Once you do that, can you see what the answer will be for c=1 and c=2? Can you figure out a pattern for the final answer from there?
Instead of working you way down from n, how about start by working your way up from 0 (I assume the recursion terminates at 0 and you left that bit out). When you start noticing a fixed point (ie a pattern which repeats the same in all cases) you have a good candidate for an answer. Try proving the answer, e.g. through induction.
I would start by observing that n^c, whilst of course influenced by the value of n, is not going to be any more complex for n vs. n + 1 - it's c that determines the "runtime" of that particular term. Given that, you can assume (at least I would) that the term has constant runtime and determine how many times the recursion will execute for a given n and you'll get your bound.
To figure these out, fill out a couple of terms and look for the pattern:
T(1) = 0 + 1^c
T(2) = T(1) + 2^c = 0 + 1^c + 2^c
T(3) = T(2) + 3^c = 0 + 1^c + 2^c + 3^c
T(4) = ...
Now express the pattern in terms of n and you have your answer.
Here it is:
T(n) = n^c + (n-1)^c + (n-2)^c + ... + 2^c + 1^c
< n^c + n^c + n^c + ... + n^c + n^c
= n * n^c
= n^(c+1)
which is O(nc+1).
To show this is a reasonable bound, note that when c = 1,
T(n) = n + (n-1) + (n-2) + ... + 2 + 1
= n * (n-1) / 2
which is clearly Θ(n2).

Resources