Formal Methods 101: Complexity Theory

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:

NotationName
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
Growth curves for input sizes 1 to 10 on a chart whose steps axis stops at 100. log base 2 of n stays below 4 and n reaches 10. n squared reaches 100 at n of 10. n cubed passes 100 before n of 5, n factorial passes 100 at n of 5, and 2 to the n passes 100 at n of 7, so those curves leave the chart while the slower ones are still near the bottom.
The classes drawn as curves. The polynomial curves stay on the chart while n³, n! and 2ⁿ shoot off the top by n = 7 or sooner. The curves are the functions themselves, not measurements of any program.

For a sense of scale, here are the step counts at three input sizes:

nlog₂ nn²n³2ⁿn!
103.31001,0001,0243,628,800
204.34008,0001,048,576about 2.4 × 10¹⁸
505.62,500125,000about 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.

P is a small region inside the larger region NP. The NP-Hard region is drawn as a dashed ellipse across the top of the picture, overlapping the top of NP; the overlap is NP-Complete. Callouts on the right say what each region means: P can be solved in polynomial time, NP problems can be turned into an NP-Complete problem in polynomial time, NP-Complete problems have no known polynomial-time algorithm but solutions can be verified in polynomial time, and NP-Hard is NP-Complete or harder.
P sits inside NP. NP-Hard overlaps NP at the top, and the overlap is exactly the NP-complete problems, the hardest ones in NP.

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.

A dashed ellipse labelled NP-Hard contains two overlapping ellipses, NP on the left and coNP on the right. Their overlap is NP intersect coNP, and P is a small circle inside that overlap. The leftmost slice of NP is NP-Complete and the rightmost slice of coNP is coNP-Complete.
NP and coNP overlap, and P lies inside the overlap. NP-complete problems are at the far end of NP, and coNP-complete problems at the far end of 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.

Two diagrams. In the first, if P equals NP then P, NP and NP-Complete are one region inside the dashed NP-Hard region. In the second, with coNP included, P, NP, coNP, NP-Complete and coNP-Complete are all one region inside NP-Hard.
What would happen if P = NP: the separate regions merge into one, leaving only the NP-hard problems outside it.