Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

base of logarithms in time-complexity algorithms

Tags:

algorithm

What is the base of logarithm on all time-complexity algorithms? Is it base 10 or base e?

When we say that the average sorting complexity is O(n log n). Is the base of log n 10 or e?

like image 478
Mike Avatar asked Jul 15 '11 01:07

Mike


People also ask

How do you calculate time complexity of a logarithm?

A common algorithm with O(log n) time complexity is Binary Search whose recursive relation is T(n/2) + O(1) i.e. at every subsequent level of the tree you divide problem into half and do constant amount of additional work.

Why log is used in time complexity?

Logarithmic time complexity log(n): Represented in Big O notation as O(log n), when an algorithm has O(log n) running time, it means that as the input size grows, the number of operations grows very slowly. Example: binary search.

Which algorithm has a logarithmic time complexity?

Specifically, we'll use the Binary Search algorithm and its logarithmic time complexity – O(log n). Binary Search is an algorithm that is used to search for an element in an ordered set.

Does base matter in exponential time complexity?

It depends where the logarithm is. If it is just a factor, then it doesn't make a difference, because big-O or θ allows you to multiply by any constant. If you take O(2logn) then the base is important.


2 Answers

In Computer Science, it's often base 2. This is because many divide and conquer algorithms that exhibit this kind of complexity are dividing the problem in two at each step.

Binary search is a classic example. At each step, we divide the array into two and only recursively search in one of the halves, until you reach a base case of a subarray of one element (or zero elements). When dividing an array of length n in two, the total number of divisions before reaching a one element array is log2(n).

This is often simplified because the logarithms of different bases are effectively equivalent when discussing algorithm analysis.

like image 82
michiakig Avatar answered Oct 03 '22 07:10

michiakig


In Big-O complexity analysis, it doesn't actually matter what the logarithm base is. (they are asymptotically the same, i.e. they differ by only a constant factor):

O(log2 N) = O(log10 N) = O(loge N) 

Most of the time when mathematicians talk about logs, they implicitly mean to base e. Computer Scientists would tend to favour base 2, but it doesn't actually matter.

like image 39
Mitch Wheat Avatar answered Oct 03 '22 06:10

Mitch Wheat