Posts

Table of common time complexities

The following table summarizes some classes of commonly encountered time complexities. In the table, poly( x ) =  x O (1), i.e., polynomial in x . Name Complexity class Running time ( T ( n )) Examples of running times Example algorithms constant time O (1) 10 Finding the median value in a sorted array of numbers Calculating (−1) n inverse Ackermann time O ( α ( n )) Amortized time per operation using a disjoint set iterated logarithmic time O (log *   n ) Distributed coloring of cycles log-logarithmic O (log log n ) Amortized time per operation using a bounded priority queue logarithmic time DLOGTIME O (log  n ) log  n , log( n 2) Binary search polylogarithmic time poly(log  n ) (log  n )2 fractional power O ( n c) where 0 < c < 1 n 1/2, n 2/3 Searching in a kd-tree linear time O ( n ) n , 2n + 5 Finding the smallest or largest item in an unsorted array, Kadane's algorithm, linear search "n log-star n" ti...

Constant time

An algorithm is said to be constant time (also written as O(1) time) if the value of T ( n ) is bounded by a value that does not depend on the size of the input. For example, accessing any single element in an array takes constant time as only one operation has to be performed to locate it. In a similar manner, finding the minimal value in an array sorted in ascending order; it is the first element. However, finding the minimal value in an unordered array is not a constant time operation as scanning over each element in the array is needed in order to determine the minimal value. Hence it is a linear time operation, taking O(n) time. If the number of elements is known in advance and does not change, however, such an algorithm can still be said to run in constant time. Despite the name "constant time", the running time does not have to be independent of the problem size, but an upper bound for the running time has to be bounded independently of the problem size. For example,...

Logarithmic time

Image
An algorithm is said to take logarithmic time when T ( n ) = O(log n ) . Since log a n and log b n are related by a constant multiplier, and such a multiplier is irrelevant to big-O classification, the standard usage for logarithmic-time algorithms is O(log n ) regardless of the base of the logarithm appearing in the expression of T . Algorithms taking logarithmic time are commonly found in operations on binary trees or when using binary search. An O(log n) algorithm is considered highly efficient, as the ratio of the number of operations to the size of the input decreases and tends to zero when n increases. An algorithm that must access all elements of its input cannot take logarithmic time, as the time taken for reading an input of size n is of the order of n . An example of logarithmic time is given by dictionary search. Consider a dictionary D which contains n entries, sorted by alphabetical order. We suppose that, for 1 ≤ k ≤ n , one may access the k th entry of the dic...

Polylogarithmic time

An algorithm is said to run in polylogarithmic time if its time T ( n ) is O ((log n ) k ) for some constant k . Another way to write this is O (log k n ) . For example, matrix chain ordering can be solved in polylogarithmic time on a parallel random-access machine, and a graph can be determined to be planar in a fully dynamic way in O(log3 n) time per insert/delete operation.

Sub-linear time

An algorithm is said to run in sub-linear time (often spelled sublinear time ) if T ( n ) = o( n ). In particular this includes algorithms with the time complexities defined above. Typical algorithms that are exact and yet run in sub-linear time use parallel processing (as the NC 1 matrix determinant calculation does), or alternatively have guaranteed assumptions on the input structure (as the logarithmic time binary search and many tree maintenance algorithms do). However, formal languages such as the set of all strings that have a 1-bit in the position indicated by the first log(n) bits of the string may depend on every bit of the input and yet be computable in sub-linear time. The specific term sublinear time algorithm is usually reserved to algorithms that are unlike the above in that they are run over classical serial machine models and are not allowed prior assumptions on the input. They are however allowed to be randomized, and indeed must be randomized for all but the most t...

Linear time

An algorithm is said to take linear time , or O ( n ) time, if its time complexity is O ( n ) . Informally, this means that the running time increases at most linearly with the size of the input. More precisely, this means that there is a constant c such that the running time is at most cn for every input of size n . For example, a procedure that adds up all elements of a list requires time proportional to the length of the list, if the adding time is constant, or, at least, bounded by a constant. Linear time is the best possible time complexity in situations where the algorithm has to sequentially read its entire input. Therefore, much research has been invested into discovering algorithms exhibiting linear time or, at least, nearly linear time. This research includes both software and hardware methods. There are several hardware technologies which exploit parallelism to provide this. An example is content-addressable memory. This concept of linear time is used in string matching a...

Quasilinear time

An algorithm is said to run in quasilinear time (also referred to as log-linear time ) if T ( n ) = O( n log k n ) for some positive constant k ; linearithmic time is the case k  = 1. Using soft O notation these algorithms are Õ( n ). Quasilinear time algorithms are also O( n 1+ε) for every constant ε > 0, and thus run faster than any polynomial time algorithm whose time bound includes a term n c for any c  > 1. Algorithms which run in quasilinear time include: In-place merge sort, O( n log 2 n ) Quicksort, O( n log n ), in its randomized version, has a running time that is O( n log n ) in expectation on the worst-case input. Its non-randomized version has an O( n log n ) running time only when considering average case complexity. Heapsort, O( n log n ), merge sort, introsort, binary tree sort, smoothsort, patience sorting, etc. in the worst case Fast Fourier transforms, O( n log n ) Monge array calculation, O( n log n ) In many cases, the n · log n running ti...