![]() |
VOOZH | about |
Big O notation is used to describe the time or space complexity of algorithms. Big-O is a way to express an upper bound of an algorithm’s time or space complexity.
Given two functions f(n) and g(n), we say that f(n) is O(g(n)) if there exist constants c > 0 and n0 >= 0 such that f(n) <= c*g(n) for all n >= n0.
In simpler terms, f(n) is O(g(n)) if f(n) grows no faster than c*g(n) for all n >= n0 where c and n0 are constants.
Example 1: f(n) = 3n2 + 2n + 1000Logn + 5000
After ignoring lower order terms, we get the highest order term as 3n2
After ignoring the constant 3, we get n2
Therefore the Big O value of this expression is O(n2)Example 2 : f(n) = 3n3 + 2n2 + 5n + 1
Dominant Term: 3n3
Order of Growth: Cubic (n3)
Big O Notation: O(n3)
Below are some important Properties of Big O Notation:
For any function f(n), f(n) = O(f(n)).
Example:
f(n) = n2, then f(n) = O(n2).
If f(n) = O(g(n)) and g(n) = O(h(n)), then f(n) = O(h(n)).
Example:
If f(n) = n^2, g(n) = n^3, and h(n) = n^4, then f(n) = O(g(n)) and g(n) = O(h(n)).
Therefore, by transitivity, f(n) = O(h(n)).
For any constant c > 0 and functions f(n) and g(n), if f(n) = O(g(n)), then cf(n) = O(g(n)).
Example:
f(n) = n, g(n) = n2. Then f(n) = O(g(n)). Therefore, 2f(n) = O(g(n)).
If f(n) = O(g(n)) and h(n) = O(k(n)), then f(n) + h(n) = O(max( g(n), k(n) ) When combining complexities, only the largest term dominates.
Example:
f(n) = n2, h(n) = n3. Then , f(n) + h(n) = O(max(n2 + n3) = O ( n3)
If f(n) = O(g(n)) and h(n) = O(k(n)), then f(n) * h(n) = O(g(n) * k(n)).
Example:
f(n) = n, g(n) = n2, h(n) = n3, k(n) = n4. Then f(n) = O(g(n)) and h(n) = O(k(n)). Therefore, f(n) * h(n) = O(g(n) * k(n)) = O(n6).
If f(n) = O(g(n)), then f(h(n)) = O(g(h(n))).
Example:
If , and , then by composition rule f(h(n))=O(g(h(n))). Therefore, .
Big-O notation is a way to measure the time and space complexity of an algorithm. It describes the upper bound of the complexity in the worst-case scenario. Let’s look into the different types of time complexities:
Linear time complexity means that the running time of an algorithm grows linearly with the size of the input.
For example, consider an algorithm that traverses through an array to find a specific element:
Logarithmic time complexity means that the running time of an algorithm is proportional to the logarithm of the input size.
For example, a binary search algorithm has a logarithmic time complexity:
Quadratic time complexity means that the running time of an algorithm is proportional to the square of the input size.
For example, a simple bubble sort algorithm has a quadratic time complexity:
Cubic time complexity means that the running time of an algorithm is proportional to the cube of the input size.
For example, a naive matrix multiplication algorithm has a cubic time complexity:
Polynomial time complexity refers to the time complexity of an algorithm that can be expressed as a polynomial function of the input size n. In Big O notation, an algorithm is said to have polynomial time complexity if its time complexity is O(nk), where k is a constant and represents the degree of the polynomial.
Algorithms with polynomial time complexity are generally considered efficient, as the running time grows at a reasonable rate as the input size increases. Common examples of algorithms with polynomial time complexity include linear time complexity O(n), quadratic time complexity O(n2), and cubic time complexity O(n3).
Exponential time complexity means that the running time of an algorithm doubles with each addition to the input data set.
For example, the problem of generating all subsets of a set is of exponential time complexity:
Factorial time complexity means that the running time of an algorithm grows factorially with the size of the input. This is often seen in algorithms that generate all permutations of a set of data.
Here’s an example of a factorial time complexity algorithm, which generates all permutations of an array:
If we plot the most common Big O notation examples, we would have graph like this:
Below table illustrates the runtime analysis of different orders of algorithms as the input size (n) increases.
| n | log(n) | n | n * log(n) | n^2 | 2^n | n! |
|---|---|---|---|---|---|---|
| 10 | 1 | 10 | 10 | 100 | 1024 | 3628800 |
| 20 | 2.996 | 20 | 59.9 | 400 | 1048576 | 2.432902e+1818 |
Below table categorizes algorithms based on their runtime complexity and provides examples for each type.
| Type | Notation | Example Algorithms |
|---|---|---|
| Logarithmic | O(log n) | Binary Search |
| Linear | O(n) | Linear Search |
| Superlinear | O(n log n) | Heap Sort, Merge Sort |
| Polynomial | O(n^c) | Strassen’s Matrix Multiplication, Bubble Sort, Selection Sort, Insertion Sort, Bucket Sort |
| Exponential | O(c^n) | Tower of Hanoi |
| Factorial | O(n!) | Determinant Expansion by Minors, Brute force Search algorithm for Traveling Salesman Problem |