For binary search tree type of data structures, I see the Big O notation is typically noted as O(logn). With a lowercase 'l' in log, does this imply log base e (n) as described by the natural logarithm? Sorry for the simple question but I've always had trouble distinguishing between the different implied logarithms.
The number e frequently occurs in mathematics (especially calculus) and is an irrational constant (like π). Its value is e = 2.718 281 828 ... Apart from logarithms to base 10 which we saw in the last section, we can also have logarithms to base e. These are called natural logarithms.
The common log can be represented as log10 (x). The natural log can be represented as loge (x). The interrogative statement for the common logarithm is written as “At which number should we raise 10 to get y?”
e is used because many computations are easy if you use base e. Think in the derivative of e^x vs a^x. However a^x = e^(c x) for some c. The exponential functions are useful for modeling many systems that occur in our natural world, as well as for modeling economics.
Big O notation is not affected by logarithmic base, because all logarithms in different bases are related by a constant factor, O(ln n)
is equivalent to O(log n)
.
Once expressed in big-O() notation, both are correct. However, during the derivation of the O() polynomial, in the case of binary search, only log2 is correct. I assume this distinction was the intuitive inspiration for your question to begin with.
Also, as a matter of my opinion, writing O(log2 N) is better for your example, because it better communicates the derivation of the algorithm's run-time.
In big-O() notation, constant factors are removed. Converting from one logarithm base to another involves multiplying by a constant factor.
So O(log N) is equivalent to O(log2 N) due to a constant factor.
However, if you can easily typeset log2 N in your answer, doing so is more pedagogical. In the case of binary tree searching, you are correct that log2 N is introduced during the derivation of the big-O() runtime.
Before expressing the result as big-O() notation, the difference is very important. When deriving the polynomial to be communicated via big-O notation, it would be incorrect for this example to use a logarithm other than log2 N, prior to applying the O()-notation. As soon as the polynomial is used to communicate a worst-case runtime via big-O() notation, it doesn't matter what logarithm is used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With