Having issues dealing with T(n) runtime problems - math

I was going to meet with my TA today but just didn't have the time. I am in an algorithms analysis class and we started doing recurrence relations and I'm not 100% sure if I am doing this problem correct. I get to a point where I am just stuck and don't know what to do. Maybe I'm doing this wrong, who knows. The question doesn't care about upper or lower bounds, it just wants a theta.
The problem is this:
T(n) = T(n-1) + cn^(2)
This is what I have so far....
=T(n-2) + (n-1)^(2) + cn^(2)
=T(n-3) + (n-2)^(2) + 2cn^(2)
=T(n-4) + (n-3)^(2) + 3cn^(2)
So, at this point I was going to generalize and substitute K into the equation.
T(n-k) + (n-k+1)^(2) + c(K-1)^(2)
Now, I start to bring the base case of 1 into the picture. On a couple of previous, more simple problems, I was able to set my generalized k equation equal to 1 and then solve for K. Then put K back into the equation to get my ultimate answer.
But I am totally stuck on the (n-k+1)^(2) part. I mean, should I actually foil all this out? I did it and got k^(2)-2kn-2k+n^(2) +2n +1 = 1. At this point I'm thinking I totally must have done something wrong since I've never see this in previous problems.
Could anyone offer me some help with how to solve this one? I would greatly appreciate it.

What you have isn't fully correct even at the first line of "what I have so far".
Go ahead and do the full substitutions, to see that:
T(n-1) = T(n-2) + c(n-1)^2
so
T(n) = T(n-2) + c(n-1)^2 + c(n)^2
T(n) = T(n-3) + c(n-2)^2 + c(n-1)^2 + c(n)^2
Overall running time looks like adding "c(n-i)^2" for each value of i from 0 to your base case. Hopefully that puts you on the right track.

Related

Determine runtime complexity given a recurrence relation

I'm trying to understand how to determine the runtime complexity given such a recurrence relation: T(n) = 2T(n-1) - T(n-2) + 8n + 7
I'm clueless about the minuses in the T's and all I know that it has nothing to do with master's theorem. I didn't find any explanation so far so any help would be appriciated.

What is the answer for: n! = Θ( )?

How do I find the answer for: n! = Θ( )?
Even Big O is enough. All clues I found are complex math ideas.
What would be the correct approach to tackle this problem? recursion tree seems too much of a work
the goal is to compare between n! and n^logn
Θ(n!) is a perfectly fine, valid complexity, so n! = Θ(n!).
As Niklas pointed out, this is actually true for every function, although, for something like
6x² + 15x + 2, you could write Θ(6x² + 15x + 2), but it would generally be preferred to simply write Θ(x²) instead.
If you want to compare two functions, simply plotting it on WolframAlpha might be considered sufficient to see that Θ(n!) functions grow faster.
To mathematically determine the result, we can take the log of both, giving us log (n!) and log nlog n = log n . log n = (log n)2.
Then, since log(n!) = Θ(n log n), and n log n > (log n)2 for any large n, we could derive that Θ(n!) grows faster.
The derivation is perhaps non-trivial, and I'm slightly unsure whether it's actually possible, but we're a bit beyond the scope of Stack Overflow already (try the Mathematics site if you want more details).
If you want some sort of "closed form" expressions, you can get n! = Ω((sqrt(n/2))^n) and n! = O(n^n). Note sure those are more useful.
To derive them, see that (n/2)^(n/2) < n! < n^n.
To compare against n^(log n), use limit rules; you may also want to use n = e^(log n).

Recursive Runtime of T(n-k)

I am trying to find the runtime of the equation;
T(n) = T(n-2) + n³.
When I am solving it I arrive at the summation T(n) = T(n-k) + Σk = 0,...,n/2(n-2k)³.
Solving that sum I get 1/8(n²)(n + 2)². Solving this I would get the runtime to be Θ(n⁴).
However, I think I did something wrong, does anyone have any ideas?
Why do you think that it is wrong? This equation is clearly Theta(n^4)
The more detailed solution can be obtained from WolframALpha (did you know it solves recurrence equations?)
https://www.wolframalpha.com/input/?i=T%28n%29%3DT%28n-2%29%2Bn%5E3
You can also add some border cases, like T(0)=T(1)=1
https://www.wolframalpha.com/input/?i=T%28n%29%3DT%28n-2%29%2Bn%5E3%2C+T%281%29%3D1%2C+T%282%29%3D1
and finally: asymptotic plot, showing that it truly behaves like n^4 function
Here is an attempt to show your recursive recursive recurrence with steps:
With WolframAlpha engine solving the summation.

solving a recurrence: T(n) = 4T(n/2) + n(logn)^2

I'm trying to solve this recursive function but i reached a dead end:
T(n) = 4T(n/2) + n(logn)^2
I tried the master rule (with the 3 cases) and non of the cases applied. I also tried iteration method and got a complex equation...
Can anybody give me a final solution or a method how to solve it?
Thank you

What does the term "BODMAS" mean?

What is BODMAS and why is it useful in programming?
http://www.easymaths.com/What_on_earth_is_Bodmas.htm:
What do you think the answer to 2 + 3 x 5 is?
Is it (2 + 3) x 5 = 5 x 5 = 25 ?
or 2 + (3 x 5) = 2 + 15 = 17 ?
BODMAS can come to the rescue and give us rules to follow so that we always get the right answer:
(B)rackets (O)rder (D)ivision (M)ultiplication (A)ddition (S)ubtraction
According to BODMAS, multiplication should always be done before addition, therefore 17 is actually the correct answer according to BODMAS and will also be the answer which your calculator will give if you type in 2 + 3 x 5 .
Why it is useful in programming? No idea, but i assume it's because you can get rid of some brackets? I am a quite defensive programmer, so my lines can look like this:
result = (((i + 4) - (a + b)) * MAGIC_NUMBER) - ANOTHER_MAGIC_NUMBER;
with BODMAS you can make this a bit clearer:
result = (i + 4 - (a + b)) * MAGIC_NUMBER - ANOTHER_MAGIC_NUMBER;
I think i'd still use the first variant - more brackets, but that way i do not have to learn yet another rule and i run into less risk of forgetting it and causing those weird hard to debug errors?
Just guessing at that part though.
Mike Stone EDIT: Fixed math as Gaius points out
Another version of this (in middle school) was "Please Excuse My Dear Aunt Sally".
Parentheses
Exponents
Multiplication
Division
Addition
Subtraction
The mnemonic device was helpful in school, and still useful in programming today.
Order of operations in an expression, such as:
foo * (bar + baz^2 / foo)
Brackets first
Orders (ie Powers and Square Roots, etc.)
Division and Multiplication (left-to-right)
Addition and Subtraction (left-to-right)
source: http://www.mathsisfun.com/operation-order-bodmas.html
I don't have the power to edit #Michael Stum's answer, but it's not quite correct. He reduces
(i + 4) - (a + b)
to
(i + 4 - a + b)
They are not equivalent. The best reduction I can get for the whole expression is
((i + 4) - (a + b)) * MAGIC_NUMBER - ANOTHER_MAGIC_NUMBER;
or
(i + 4 - a - b) * MAGIC_NUMBER - ANOTHER_MAGIC_NUMBER;
When I learned this in grade school (in Canada) it was referred to as BEDMAS:
Brackets
Exponents
Division
Multiplication
Addition
Subtraction
Just for those from this part of the world...
I'm not really sure how applicable to programming the old BODMAS mnemonic is anyways. There is no guarantee on order of operations between languages, and while many keep the standard operations in that order, not all do. And then there are some languages where order of operations isn't really all that meaningful (Lisp dialects, for example). In a way, you're probably better off for programming if you forget the standard order and either use parentheses for everything(eg (a*b) + c) or specifically learn the order for each language you work in.
I read somewhere that especially in C/C++ splitting your expressions into small statements was better for optimisation; so instead of writing hugely complex expressions in one line, you cache the parts into variables and do each one in steps, then build them up as you go along.
The optimisation routines will use registers in places where you had variables so it shouldn't impact space but it can help the compiler a little.

Resources