Efficient approximation of nth term without losing accuracy - algorithm

Probelem Given a recurrence relation for gn as
g0 = c where is a contant double.
gn = f( gn-1 ) , where f is a linear function
then find the value of another recurrence given by
hn = gn/exp(n)
constraints: 1 <= n <= 10^9
Mathematically, the value of g(n) can be calculated in log(n) time and then h(n) can be calculated very easily but the problem is the overflow of doubles data types. So the above strategy works only for n around 1000 but not for bigger n. Note that value of h(n) can be well within the range of doubles
The actual problem is that we are trying to calculate h(n) from g(n). My question is that is there any good method to caculate h(n) directly without overflowing doubles.

G0=c
G1=ac+b
G2=a²c+ab+b
G3=a³c+a²b+ab+b
...
Gn=a^nc+b(a^n-1)/(a-1)
Then
Hn = (a/e)^nc+b((a/e)^n-1/e^n)/(a-1) ~ (a/e)^n (c + b/(a-1))

Related

Solving the following recurrence: T(n) = T(n/3) + T(n/2) + sqrt(n)

I am trying to solve the following recurrence:
T(n) = T(n/3) + T(n/2) + sqrt(n)
I currently have done the following but am not sure if I am on the right track:
T(n) <= 2T(n/2) + sqrt(n)
T(n) <= 4T(n/4) + sqrt(n/2) + sqrt(n)
T(n) <= 8T(n/8) + sqrt(n/4) + sqrt(n/2) + sqrt(n)
so, n/(2^k) = 1, and the sqrt portion simplifies to: (a(1-r^n))/(1-r)
K = log2(n) and the height is 2^k, so 2^(log2(n)) but:
I am not sure how to combine the result of 2^(log2(n)) with the sqrt(n) portion.
A good initial attempt would be to identify the upper and lower bounds of the time complexity function. These are given by:
These two functions are much easier to solve for than T(n) itself. Consider the slightly more general function:
When do we stop recursing? We need a stopping condition. Since it is not given, we can assume it is n = 1 without loss of generality (you'll hopefully see how). Therefore the number of terms, m, is given by:
Therefore we can obtain the lower and upper bounds for T(n):
Can we do better than this? i.e. obtain the exact relationship between n and T(n)?
From my previous answer here, we can derive a binomial summation formula for T(n):
Where
C is such that n = C is the stopping condition for T(n). If not given, we can assume C = 1 without loss of generality.
In your example, f(n) = sqrt(n), c1 = c2 = 1, a = 3, b = 2. Therefore:
How do we evaluate the inner sum? Consider the standard formula for a binomial expansion, with positive exponent m:
Thus we replace x, y with the corresponding values in the formula, and get:
Where we arrived at the last two steps with the standard geometric series formula and logarithm rules. Note that the exponent is consistent with the bounds we found before.
Some numerical tests to confirm the relationship:
N T(N)
--------------------
500000 118537.6226
550000 121572.4712
600000 135160.4025
650000 141671.5369
700000 149696.4756
750000 165645.2079
800000 168368.1888
850000 181528.6266
900000 185899.2682
950000 191220.0292
1000000 204493.2952
Plot of log T(N) against log N:
The gradient of such a plot m is such that T(N) ∝ N^m, and we see that m = 0.863, which is quite close to the theoretical value of 0.861.

How dominant term helps determine time complexity using Big-O? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I don't quite understand the concept of dominant terms and how to determine the time complexity using big o. Like, for example, the dominant term of N(100N + 200N^3) + N^3. If anyone could explain it, that would be very helpful.
The dominant term is the term the one that gets biggest (i.e. dominates) as N gets bigger.
For example:
N(100N + 200N^3) + N^3
can be rewritten as
(100 * N^2) + (200 * N^4) + N^3
and as N gets very large, the N^4 is going to get biggest (irrespective of the 200 that you multiply it by).
So that would be O(N^4).
Let f(N) = 100N^2 + 200 N^4 + N^3. As N increases, the value of f(N) is dominated by the 200 N^4 term. This would be the case even if coefficient of the N^4 term was smaller than that of the lower order terms.
Consider the simpler example for g(N) = N^2 + 4N. When N = 1000, we get g(1000) =10^6 + 4000 which is approximately 10^6 since a million plus a (few) thousand is still about a million. So the N^2$ term dominates the lower order terms. This would be the case even if the coefficients of the lower order terms are large - in g(N) = N^2 + 10^{100} N, N^2 term dominates the linear term, and its just that the value of N required for the quadratic term to exceed the linear term is larger in this case. But as N goes to infinity, we can approximate a function by just its leading term.
As for big-oh notation, you can prove using its definition that a polynomial f(N) can be expressed as O(N^k), where k is the exponent in the leading term. In your example, we can say f(N) = O(N^4). This notation discards the lower order terms and the coefficient of the leading term as they are often irrelevant when comparing different algorithms for their running time.
"Dominant" simply means "the one that grows faster in the long run". That's the best way I can put it.
Let's devise your polynomial function into parts ,
F(N) = 100N²+ 200N^4 + N^3 ;
g,h,k are respectively 3 polynomial functions
g(N) = 200N^4 , h(N) = N^3 , k(N) = 100N² .
h is dominated by g , and k is dominated by h , so using Transitive relation k is dominated by g ; so both h and k are dominated by g.
I mean by domination in mathematics , the limit of the fraction (h(n) / g(n) ) or ( k(n) / g(n) ) if n goes to infinity : is zero.
so to know which function is dominated , you need to study the asymptotic behavior and limits.
This is an example illustrated from this website

Asymptotic Notion: What is n₀ in formula, how do we find constant

I was doing study on Asymptotic Notations Topic, i recon that its formula is so simple yet it tells nothing and there are couple of things i don't understand.
When we say
f(n) <= c.g(n) where n >= n₀
And we don't know the value of c =? and n=? at first but by doing division of f(n) or g(n) we get the value of c. (here is where confusion lies)
First Question: How do we decide which side's 'n' has to get divided in equation f(n) or g(n)?
Suppose we have to prove:
2n(square) = O(n(cube))
here f(n) = 2(n(square)) and g(n)=n(cube)
which will form as:
2(n(square)) = c . n(cube)
Now in the notes i have read they are dividing 2(n(square)) to get the value of c by doing that we get c = 1;
But if we do it dividing n(cube) [which i don't know whether we can do it or not] we get c = 2;
How do we know what value we have to divide ?
Second Problem: Where does n₀ come from what's its task ?
Well by formula we know n >= n(0) which means what ever we take n we should take the value of n(0) or should be greater what n is.
But i am confuse that where do we use n₀ ? Why it is needed ?
By just finding C and N can't we get to conclusion if
n(square) = O(n(cube)) or not.
Would any one like to address this? Many thanks in advance.
Please don't snub me if i ask anything stupid or give -1. Address it please any useful link which covers all this would be enough as well:3
I have gone through the following links before posting this question this is what i understand and here are those links:
http://openclassroom.stanford.edu/MainFolder/VideoPage.php?course=IntroToAlgorithms&video=CS161L2P8&speed=
http://faculty.cse.tamu.edu/djimenez/ut/utsa/cs3343/lecture3.html
https://sites.google.com/sites/algorithmss15
From the second url in your question:
Let's define big-Oh more formally:
O(g(n)) = { the set of all f such that there exist positive constants c and n0 satisfying 0 <= f(n) <= cg(n) for all n >= n0 }.
This means, that for f(n) = 4*n*n + 135*n*log(n) + 1e8*n the big-O is O(n*n).
Because for large enough c and n0 this is true:
4*n*n + 135*n*log(n) + 1e8*n = f(n) <= O(n*n) = c*n*n
In this particular case the [c,n0] can be for example [6, 1e8], because (this is of course not valid mathematical proof, but I hope it's "obvious" from it):
f(1e8) = 4*1e16 + 135*8*1e8 + 1e16 = 5*1e16 + 1080*1e8 <= 6*1e16 = 6*1e8*1e8 =~= O(n*n). There are of course many more possible [c,n0] for which the f(n) <= c*n*n holds true, but you need to find only one such pair to prove the f(n) has O(f(n)) of O(n*n).
As you can see, for n=1 you need quite a huge c (like 1e9), so at first look the f(n) may look much bigger than n*n, but in the asymptotic notion you don't care about the first few initial values, as long as the behaviour since some boundary is as desired. That boundary is some [c,n0]. If you can find such boundary ([6, 1e8]), then QED: "f(n) has big-O of n*n".
The n >= n₀ means that whatever you say in the lemma can be false for some first k (countable) parameters n' : n' < n₀, but since some n₀ the lemma is true for all the rest of (bigger) integers.
It says that you don't care about first few integers ("first few" can be as "little" as 1e400, or 1e400000, ...etc... from the theory point of view), and you only care about the bigger (big enough, bigger than n₀) n values.
Ultimately it means, that in the big-O notation you usually write the simplest and lowest function having the same asymptotic notion as the examined f(n).
For example for any f(n) of polynomial type like f(n) = ∑aini, i=0..k the O(f(n)) = O(nk).
So I did throw away all the lower 0..(k-1) powers of n, as they stand no chance against nk in the long run (for large n). And the ak does lose to some bigger c.
In case you are lost in that i,k,...:
f(n) = 34n4 + 23920392n2 has O(n4).
As for large enough n that n4 will "eclipse" any value created from n2. And 34n4 is only 34 times bigger than n4 => 34 is constant (relates to c) and can be omitted from big-O notation too.

Intro to Algorithms (chapter 1-1)

Just reading this book for fun, this isn't homework.
However I am already confused on the first main assignment:
1-1 Comparison of running times
For each function f(n) and time t in the following table, determine the largest size n of a problem that can be solved in time t, assuming that the algorithm to solve the problem takes f(n) microseconds.
What does this even mean?
The next table shows a bunch of times along one axis (1 second, 1 minute, one hour, etc), and the other axis shows different f(n) such as lg n, sqrt(n), n, etc.
I am not sure how to fill in the matrix because I can't understand the question. So if f(n) = lg n, it's asking the largest n that can be solved in, for example, 1 second, but the problem takes f(n) = lg n microseconds to solve? What does that last part even mean? I don't even know how to set up the equations / ratios to solve this problem because I literally can't even put together the meaning of the question.
My hangup is over the sentence "assuming that the algorithm to solve the problem takes f(n) microseconds" because I don't know what this refers to. The time for what algorithm to solve what problem takes f(n) microseconds? So if I call f(100) it'll take lg 100 microseconds? So I need to find some n where f(n) = lg n microseconds = 1 second?
Does this mean lg n microseconds = 1 second when lg n microseconds = 10^6 microseconds, so n = 2^(10^6)?
For each time T, and each function f(n), you are required to find the maximal integer n such that f(n) <= T
For example, f(n) = n^2, T=1Sec = 1000 ms:
n^2 <= 1000
n <= sqrt(1000)
n <= ~31.63 <- not an integer
n <= 31
Given any function f(n), and some time T, you are required to similarly find the maximal value of n, and fill in the table.
I will do the first two as an example to help you do the rest. Since a second is 10^6 microseconds. By solving an equation which relates f(n) to the time we are plotting for f(n) to run we can solve for the largest input n that f can run on within the time limit.
1 second:
log(n2)=1,000,000⟹n2=e1,000,000⟹n=e500,000
1 minute:
log(n2)=60,000,000⟹n2=e60,000,000⟹n=e30,000,000
the rest can be similarly done.
P.S. make sure to floor the values of n you get from these equations because n is an integer length input.

Negative Coefficients in Polynomial time Complexity

Assuming some algorithm has a polynomial time complexity T(n), is it possible for any of the terms to have a negative coefficient? Intuitively, the answer seems like an obvious "No" since there is no part of any algorithm that reduces the existing amount of time taken by previous steps but I want to be certain.
When talking about polynomial complexity, only the coefficient with the highest degree counts.
But I think you can have T(n) = n*n - n = n*(n-1). The n-1 would represent something you don't do on the first or last iteration.
Anyway, the complexity would still be n*n.
It is possible for an algorithm to have a negative coefficient in its time complexity, but overall the algorithm will have some positive time complexity. As an example from Wikipedia, take the function f(x)=6x^4-2x^3+5. They solve for the complexity of O(x^4) as follows:
for some suitable choice of x0 and M and for all x > x0. To prove this, let x0 = 1 and M = 13. Then, for all x > x0:
So,
That is, even if there are negative coefficients in the original equation, there is still some positive overall time complexity based on the term with the highest order of power.
What about for lower bounds? By definition, we can find the lower bound of any function by using the following definition: As n goes to infinity, then for some constant k and some n0 we have that the following holds for all n>n0:
Let's guess that the above function f(x) is also Omega(x^4). This means that:
6x^4 - 2x^3 + 5 >= kx^4
Solving for k:
k <= (6x^4 - 2x^3 + 5)/(x^4)
k <= 6 - 2x^-1 + 5x^-4
The term (2/x) approaches 0, as does (5/x^4) so we can choose k=2 for some large x0=30. To show that this holds, we show that:
6x^4 - 2x^3 + 5 >= 2x^4 where x > 30
4x^4 - 2x^3 + 5 >= 0
Which holds. So f(x) is Omega(x^4), and we can also conclude that we have found a tight bound such that f(x) is Theta(x^4).
Why does this work, even though the coefficient was negative? For both Big O and Big Omega notation, we are looking for a bound such that after some point one function dominates another. That is, as these graphs illustrate:
(source: Alistair.Rendell at cs.anu.edu.au)
-- Big O
(source: Alistair.Rendell at cs.anu.edu.au)
-- Big Omega
Thinking about our original f(x), 6x^4grows faster than 2x^4 (our kg(x) function). After some point, the 6x^4 term will outstrip the growth of 2x^4 in such a way that it is always greater than 2x^4. Graphically, the two functions look like this:
Despite the negative coefficient, clearly kg(x) is a lower bound of f(x).
Now, is this always true for any polynomial function with any negative coefficient--that a function f(x) with any coefficients will be bound by its highest degree polynomials? No. If the term with the highest degree has the negative coefficient, then the bounds aren't quite the same. Take f(x) = -2x^2. We can show that f(x) = O(x^2):
-2x^2 <= cx^2
-2 <= c
Which can be satisfied by any c>0 (as c is by definition a positive constant). However, if we try to do the same for lower bound:
-2x^2 >= cx^2
-2 <= c
Then we can't find the right c because again c must be non-negative.

Resources