Computational Complexity
Measuring what can be computed, and at what cost.
Big-O Notation
Definition
\(f(n) = O(g(n))\) if there exist constants c and \(n_0\) such that:
Common Classes
| Complexity | Name | Example |
|---|---|---|
\(O(1)\) |
Constant |
Array index access |
\(O(\log n)\) |
Logarithmic |
Binary search |
\(O(n)\) |
Linear |
Linear search |
\(O(n \log n)\) |
Linearithmic |
Merge sort, FFT |
\(O(n^2)\) |
Quadratic |
Bubble sort, naive matrix multiply |
\(O(n^3)\) |
Cubic |
Standard matrix multiply |
\(O(2^n)\) |
Exponential |
Subset enumeration |
\(O(n!)\) |
Factorial |
Permutation enumeration |
Complexity Classes
P (Polynomial Time)
Problems solvable in polynomial time by a deterministic Turing machine.
Examples: Sorting, shortest path, linear programming.
NP (Nondeterministic Polynomial)
Problems whose solutions can be verified in polynomial time.
Examples: SAT, graph coloring, traveling salesman (decision version).
The P vs NP Problem
-
If P = NP: Efficient algorithms exist for all verifiable problems
-
If P ≠ NP: Some problems are fundamentally hard to solve
Millennium Prize Problem ($1,000,000).
Cryptographic security assumes P ≠ NP.
Master Theorem
For recurrences of the form:
Let \(c = \log_b a\):
| Condition | Result |
|---|---|
\(f(n) = O(n^{c-\epsilon})\) |
\(T(n) = \Theta(n^c)\) |
\(f(n) = \Theta(n^c)\) |
\(T(n) = \Theta(n^c \log n)\) |
\(f(n) = \Omega(n^{c+\epsilon})\) |
\(T(n) = \Theta(f(n))\) |
Space Complexity
Memory usage follows same notation.
| Complexity | Example | Implication |
|---|---|---|
\(O(1)\) |
In-place sort (heapsort) |
Fixed memory |
\(O(\log n)\) |
Recursive binary search stack |
Small overhead |
\(O(n)\) |
Merge sort auxiliary array |
Scales with input |
\(O(n^2)\) |
Naive DP table |
May be prohibitive |
Related
-
Applied Mathematics — Overview
-
Automation — Algorithm implementation
-
Cryptography — Security assumptions