I have the following harmonic sequence:
h(n) = 1 + 1/2 + 1/3 + 1/4 +...+ 1/n
Id like to prove that there's a recurrence with
h(n) (less than or equal to) h( lowerbound( n/2)) + 1
This belongs on math.SE, but we have
h(2n) - h(n) = 1/(n/2 + 1) + 1/(n/2 + 2) + ... + 1/n
< 1/(n/2) + 1/(n/2) + ... + 1/(n/2)
= 1,
since there are n/2 terms. I'll leave the odd case as an exercise.
Related
I have the function :
int ps (int n)
{ if (n == 1) return 1;
else return (Extra(n) + Ps (n/4) + Ps (n/4)); }
Extra(n) is O(n)
I have tried to find T(n) of this function which is T(n)=T(n)+2 T(n/4) and I have calculated the complexity using the master theorem it is O(n)
but I don't know how to find the complexity of it using back substitution
First, you are wrong in terms of complexity. You didn't mention Extra(n) in writing the time complexity. So, T(n) = 2 T(n/4) + n. Now, I think the new recurrent complexity term is easy to solve by substitution:
T(n) = 2T(n/4) + n = 2 (2 T(n/8) + n/4) + n = 2^2 T(n/8) + n/2 + n =
2^2 (2 T(n/16) + n/8) + n/2 + n = 2^3 T(n/16) + n/4 + n/2 + n
Now, by mathematical induction, if we suppose n = 2^k, you can find that:
T(n) = n + n/2 + n/4 + n/8 + ... + n/2^k = n (1 + 1/2 + 1/4 + ... + 1/2^k) <= 2n
The last part of the above analysis comes from the being geometric series of the sum with factor 1/2. Hence, T(n) is in Theta(n) and O(n).
Consider this recurrence relation: x(n) = x(n/2) + n, for n > 1 and x(1) = 0.
Now here the method of back substitution will struggle for values of n not powers of 2, so it is best known here is to use the smoothness rule to solve this types of questions, and when we use the smoothness rule, where we will solve for n = 2^k (for n = values powers of 2) we will have a solution of x(n) = 2n - 1.
However, if we use the method of backward substitution, this recurrence relation will have a solution!
x(n) = x(n/2) + n = x(n/4) + n/2 + n = x(n/8) + n/4 + n/2 + n = x(n/16) + n/8 + n/4 + n/2 + n = ....
where the pattern is
x(n) = x(n/i) + n/(i/2) + n/(i/4) + n/(i/8) + n/(i/16) + ...
which will stop when n = 1 (i.e when i = n) and in this case
x(n) = x(n/n) + n/(n/2) + n/(n/4) + n/(n/8) + n/(n/16) + ... = 1 + 2 + 4 + 8 + 16 + ... = 2^(n+1) - 1
which is two different answers!
So please I am so confused here because in the textbook (Introduction to Analysis and Design of Algorithms by Anany Levitin) it is mention that we should use here the smoothness rule, but as you can see I have solved it exactly by the method of backward substitution where the method was expected here to struggle but nothing has happened!
The transition 1 + 2 + 4 + 8 + 16 + ... = 2^(n+1) - 1 is false.
That is since the number of elements in the left series is log n so the sum is 2^(log n + 1) - 1, which is exactly 2n - 1.
The reason there are log n elements is that n/(2^i) = 1 (the last element of the series is 1) when i = log n.
This is for analysis of algorithms and I cannot seem to understand where to get started, how do i approach this to find the solution?
As the sum of series S = 1 + 1/2 + 1/2^2 + ... + 1/2^n is constant,f(n) = Theta(n).
We know that S = (1 - 1/2^(n+1)) / (1 - 1/2) and n goes to infinity S goes to 2. Hence, limit of f(n) with grows of n is 3 log(n) + 2.
The sum of 1 + 1/2 + 1/4 + 1/8 + … + 1/2n is constant. Indeed, this is a gemetric progression [wiki]. The sum of:
n
---
\ 1
/ ----
--- 2n
i=0
is the same as 2-1/2n, if n goes to infinity, the sum will eventually go to 2 (not infinity).
This thus means that 3×log(n) + 1 + 1/2 + 1/4 + … + 1/2n is equivalent to 3×log(n)+2-1/2n. For an asymptotic n, this is thus equivalent to Θ(3× log n) and thus Θ( log n).
I thought it would be something like this...
T(n) = 2T(n-1) + O(n)
= 2(2T(n-2)+(n-1)) + (n)
= 2(2(2T(n-3)+(n-2))+(n-1))+(n)
= 8T(n-3) + 4(n-2) + 2(n-1) + n
Which ends up being something like the summation of 2i * (n-i), and my book says this ends up being O(2n). Could anybody explain this to me? I don't understand why it's 2n and not just O(n) as the (n-i) will continue n times.
This recurrence has already been solved on Math Stack Exchange. As I solve this recurrence, I get:
T(n) = n + 2(T(n-1))
= n + 2(n - 1 + 2T(n-2)) = 3n - 2 + 2^2(T(n-2))
= 3n - 2 + 4(n - 2 + 2(T(n-3))) = 7n - 10 + 2^3(T(n-3))
= 7n - 10 + 8(n - 3 + 2(T(n-4))) = 15n - 34 + 2^4(T(n-4))
= (2^4 - 1)n - 34 + 2^4(T(n-4))
...and so on.
Effectively the recurrence boils down to:
T(n) = (2n+1) * T(1) − n − 2
See the Math Stack Exchange link for how we arrive at this solution. Taking T(1) to be constant, the dominating factor in the above recurrence is (2(n + 1)).
Therefore, the rate of growth of given recurrence is O(2n).
How did my teacher go from T(n-1)+cn, to T(n)=O(n^2)
Am I multiplying out T(n) * n? Little confused on how he got there without doing any work in class.
You might have more luck expanding this out using the iteration method:
T(n) = T(n - 1) + cn
= T(n - 2) + c(n-1) + cn
= T(n - 3) + c(n-2) + c(n-1) + cn
...
= T(1) + 1c + 2c + 3c + ... + c(n-1) + cn
= O(1) + c(1 + 2 + 3 + 4 + ... + (n-1) + n)
That last summation is O(n2) because it's the sum of the first n integers. Therefore, the whole summation works out to O(n2).
Hope this helps!