Search⌘ K

Big-O Notation

Explore the concept of Big-O notation to understand how running times of algorithms are compared based on their growth rates. Learn to interpret the definition, inequalities, and examples that highlight how functions scale and what it means for an algorithm's efficiency in terms of input size.

Given any two functions f(n)f(n) and g(n)g(n), when we say a function f(n)f(n) is O(g(n))O(g(n)), we are expressing a certain relationship between the two functions.

Let’s look at the definition, but don’t worry if you don’t grasp it immediately. We’ll expand on its meaning and usage bit by bit.

Definition

We say f(n)f(n) is O(g(n))O(g(n)) if for some positive constants cc' and nn',

0f(n)c g(n)      for all nn0 \leq f(n) \leq c' \ g(n) \ \ \ \ \ \text{ for all } n \geq n'

There are two inequalities above. Let’s take the one on the right first and see what it means.

The inequality on the right

First, let’s focus on the following part of the definition

f(n)c g(n)      for all nnf(n) \leq c' \ g(n) \ \ \ \ \ \text{ for all } n \geq n'

for some positive constants cc' and nn'.

On the right-hand side of the inequality, the constant cc' is multiplied by g(n)g(n). Multiplication of a function by a constant is known as scaling. Scaling has the effect of changing the shape of that function.

Try choosing different values for the constant cc' to see how the example function g(n)g(n) scales.

Coming back to our inequality, the ...