What is the complexity of sum of log functions

Viewed 2811

I have an algorithm that have the following complexity in big O:

log(N) + log(N+1) + log(N+2) + ... + log(N+M)

Is it the same as log(N+M) since it is the largest element?

OR is it M*log(N+M) because it is a sum of M elements?

2 Answers

The important rules to know in order to solve it are:

  • Log a + Log b = Log ab, and
  • Log a - Log b = Log a/b

Add and subtract Log 2, Log 3, ... Log N-1 to the given value.

This will give you Log 2 + Log 3 + ... + Log (N+M) - (Log 2 + Log 3 + ... + Log (N-1))

The first part will compute to Log ((N+M)!) and the part after the subtraction sign will compute to Log ((N-1)!)

Hence, this complexity comes to Log ( (N+M)! / (N-1)! ).


UPDATE after OP asked another good question in the comment:

If we have N + N^2 + N^3, it will reduce to just N^3 (the largest element), right? Why we can't apply the same logic here - log(N+M) - largest element?

If we have just two terms that look like Log(N) + Log(M+N), then we can combine them both and say that they will definitely be less than 2 * Log(M+N) and will therefore be O(Log (M+N)).

However, if there is a relation between the number of items being summed and highest value of the item, then the presence of such a relation makes the calculation slightly not so straightforward.

For example, the big O of addition of 2 (Log N)'s, is O(Log N), while the big O of summation of N Log N's is not O(Log N) but is O(N * Log N).

In the given summation, the value and the number of total values is dependent on M and N, therefore we cannot put this complexity as Log(M+N), however, we can definitely write it as M * (Log (M+N)).

How? Each of the values in the given summation is less than or equal to Log(M + N), and there are total M such values. Hence the summation of these values will be less than M * (Log (M+N)) and therefore will be O(M * (Log (M+N))).


Thus, both answers are correct but O(Log ( (N+M)! / (N-1)! )) is a tighter bound.

If M does not depend on N and does not vary then the complexity is O(log(N))

For k such as 0 <= k <= M and N>=M and N>=2,

log(N+k)=log(N(1+k/N)) = log(N) + log(1+k/N) <= log(N) + log(2) 
<= log(N) + log(N) <= 2 log(N)

So

log(N) + log(N+1) + log(N+2) + ... + log(N+M) <= (M+1)2 log(N)

So the complexity in big O is: log(N)

To answer your questions:

1) yes because there is a fixed number of elements all less or equal than log(N+M)

2) In fact there are M + 1 elements (from 0 to M)

I specify that O((M+1)log(N+M)) is a O(log(N))

Related