
This is part 8 of the Formal Methods 101 series. Complexity theory deals with how complex a problem is to solve: that is, how many steps it takes to solve it. It is one of the most profound and consequential topics in all of computer science and mathematics.
Big-O notation
Big-O notation is what computer scientists use to describe the complexity of algorithms. It describes how many steps an algorithm will take to complete a task at run time, and it tries to capture the algorithm's scale (its growth rate) as the problem size grows. The common types are:
| Notation | Name |
|---|---|
| O(1) | Constant |
| O(log n) | Logarithmic |
| O(n) | Linear |
| O(n²) | Quadratic |
| O(n³) | Cubic |
| O(nᶜ) | Polynomial, c is a constant |
| O(cⁿ) | Exponential, c is a constant |
| O(n!) | Factorial |
For a sense of scale, here are the step counts at three input sizes:
| n | log₂ n | n² | n³ | 2ⁿ | n! |
|---|---|---|---|---|---|
| 10 | 3.3 | 100 | 1,000 | 1,024 | 3,628,800 |
| 20 | 4.3 | 400 | 8,000 | 1,048,576 | about 2.4 × 10¹⁸ |
| 50 | 5.6 | 2,500 | 125,000 | about 1.1 × 10¹⁵ | about 3.0 × 10⁶⁴ |
Examples
O(1): constant
function foo(int x):
x = x + 1 (step 1)
return x
The function foo has a Big-O of O(1) runtime, since no matter how big the input x is, it always takes the same number of steps (or amount of time) to compute.
function foo(int x):
x = x + 1 (step 1)
y = x + 1 (step 2)
z = x + 1 (step 3)
return x
This function also has an O(1) runtime. Even though it is technically O(3), O(1) and O(3) have the same growth rate, which is constant, so we usually just say the function has an O(1) runtime. That is, O(3) = O(1) = O(c), where c is a constant integer. Notice that the runtime would still be constant if we added more arbitrary steps, because the number of steps does not increase with the input size.
O(n): linear
function foo(int x):
y = 1 (step 1)
for 1 to size_of(x):
y = y + 1 (steps 2 to n + 1)
return y
This function has an O(n) runtime, because the number of steps (or amount of time) scales linearly with the size of the input x. Even though it is technically O(n + 1), O(n + 1) and O(n) have the same growth rate, which is linear, so we usually just say O(n). That is, O(n + 1) = O(n) = O(n + c), where c is a constant integer.
function foo(int x):
y = 1 (step 1)
z = 1 (step 2)
for 1 to size_of(x):
y = y + 1 (steps 3 to n + 2)
for 1 to size_of(x):
z = z + 1 (steps n + 3 to 2n + 2)
return y, z
This function is O(n) as well. It is technically O(2n + 2), but O(2n + 2) and O(n) have the same growth rate, which is linear. That is, O(2n + 2) = O(n) = O(bn + c), where b and c are constant integers. Notice that the runtime would still be linear if we added more non-nested loops, because the number of steps per loop does not change with the input size.
O(n²): quadratic
function foo(int x):
y = 1 (step 1)
for 1 to size_of(x):
for 1 to size_of(x):
y = y + 1 (steps 2 to n² + 1)
return y
This function has an O(n²) runtime, because the number of steps scales quadratically with the size of the input x.
O(n³): cubic
function foo(int x):
y = 1 (step 1)
for 1 to size_of(x):
for 1 to size_of(x):
for 1 to size_of(x):
y = y + 1 (steps 2 to n³ + 1)
return y
This function has an O(n³) runtime, because the number of steps scales cubically with the size of the input x.
O(nᶜ): polynomial
function foo(int x):
y = 1 (step 1)
for 1 to size_of(x):
for 1 to size_of(x):
... c nested loops ...
y = y + 1 (steps 2 to nᶜ + 1)
return y
This function has an O(nᶜ) runtime, because the number of steps scales with the size of the input x to the power c.
Complexity classes
P
In general, algorithms with a polynomial runtime or lower are considered computationally friendly: a computer can solve them in a reasonable amount of time because they scale polynomially. The class of problems that can be solved by deterministic polynomial-time algorithms is called P. In other words, these are the polynomial-time problems that belong to the polynomial-time complexity class.
NP and NP-hard
In contrast, algorithms with an exponential runtime are very challenging for computers as they scale. Computers often cannot solve them in a reasonable amount of time, because the work grows exponentially. The problems whose best known algorithms need exponential runtimes or worse are where the NP-hard and NP-complete classes live. Here NP stands for nondeterministic polynomial time. This essentially means that if we could search all paths of the problem at one time (simultaneously), we could find a solution in polynomial time.
NP-complete
NP-complete problems are a subclass of the NP-hard problems and also a subclass of NP (note that the NP class is not the same as the NP-hard class). It is difficult to find a solution to them, because the best known algorithms take exponential time, but if a solution is given it can be verified in polynomial time by a polynomial-time algorithm. The standard NP-complete problem is SAT: it is easy to verify a solution once it is given, but hard to find one. NP-complete problems are the hardest problems in the NP class. In fact, if a polynomial-time algorithm is found for any one NP-complete problem (none has been found so far), then all NP problems can be solved using it.
NP, between P and NP-complete
The NP class sits between P and the NP-complete class. Its problems can be reduced to NP-complete problems (for example, converted to a SAT problem) by a polynomial-time algorithm.
coNP
The coNP class is the mirror of NP: rather than being easy to verify a solution, it is easy to exclude non-solutions. In other words, you can exclude non-solutions in polynomial time. The standard coNP-complete problem is UNSAT (proving that a Boolean formula is unsatisfiable).
The coNP-complete problems are the hardest problems in coNP.
P = NP?
Every once in a while we find an efficient polynomial-time algorithm for a problem in NP and realize that it is actually a P problem; we just never realized it. This leads to one of the biggest unsolved questions in all of computer science and mathematics:
P = NP, or P ≠ NP?
If P = NP, then all of the NP-complete and coNP-complete problems (including SAT and UNSAT) are really P problems, and many of the computational challenges we have today would disappear. The general suspicion is that P ≠ NP, but no one has been able to prove it.