views:

116

answers:

3

While considering O(log(N)) for time complexity, what is the base of log?

+12  A: 

All logarithms are related by some constant. (Hence the change-of-base formula). Because we generally disregard constants in complexity analysis, the base doesn't matter.

Usually, the base is considered to be 2, when deriving the algorithm. Consider a sort like merge sort. You can construct a tree out of it, and the tree has a height of log₂ n, because each node has two branches.

GMan
+7  A: 

It doesn't matter, the relative complexity is the same regardless of the base used.

Rob Walker
Hmmm. If you logically extend this statement, you would be saying that O(n^2) is the same relative complexity as O(n^3).
Andrew Shepherd
Not at all. Big diff between 1 million squared or cubed. But log2, log10, log100? Not much difference at all.
cletus
@Andrew Shepherd - That is not correct. log_a(2n) / log_a(n) = log_b(2n) / log_b(n) for any a and b
mobrule
@Andres Shepherd: To expand on the other responses to your comment, log_a(n)/log_b(n) is a constant which is the same for all values of n. The ratio of (n^2)/(n^3), on the other hand, grows as n grows. Algorithmic complexity analysis is concerned with how resource requirements increase as n increases, so constants don't matter. Values that vary with n do.
Dave Sherohman
+1  A: 

One way to think of it is that O(log2X) = O(log10X) = O(logNX)

RickNZ