Complexity Theory
1. Asymptotic Notation
1.1 Big-O (Upper Bound)
if there exist constants and such that:
1.2 Big-Omega (Lower Bound)
if there exist constants and such that:
1.3 Big-Theta (Tight Bound)
if both and :
1.4 Little-o and Little-omega
if for every constant , there exists such that for all .
Equivalently: .
: .
1.5 Common Complexity Classes
| Class | Name | Example |
|---|---|---|
| Constant | Hash table access | |
| Logarithmic | Binary search | |
| Square root | Trial division factorization | |
| Linear | Array scan | |
| Linearithmic | Merge sort, heap sort | |
| Quadratic | Bubble sort, insertion sort | |
| Cubic | Matrix multiplication | |
| Exponential | Subset enumeration | |
| Factorial | Permutation generation | |
| Exponential tower | Some brute force |
1.6 Properties
Transitivity: If and , then .
Reflexivity: .
Sum rule: .
Product rule: .
2. Complexity Classes
2.1 Decision Problems
A decision problem has a yes/no answer. Optimization problems can be cast as decision problems (e.g., “is there a TSP tour of length ?“).
2.2 P (Polynomial Time)
Examples: Sorting, shortest paths, MST, 2-SAT, primality testing.
2.3 NP (Nondeterministic Polynomial Time)
Equivalently, NP is the class of problems for which a yes-instance can be verified in polynomial time given a certificate (witness).
Examples: SAT, traveling salesman (decision version), graph coloring, clique, vertex cover.
Key insight: . Whether is the most important open question in CS.
2.4 NP-Hard
A problem is NP-hard if every problem in NP can be reduced to in polynomial time:
NP-hard problems are at least as hard as every problem in NP. They may or may not be in NP themselves.
2.5 NP-Complete
A problem is NP-complete if it is both in NP and NP-hard:
If any NP-complete problem is in P, then P = NP.
Relationship: NP-Hard / \ / NP-Complete / / All problems P ⊆ NP2.6 co-NP
The class of problems whose complement can be verified in polynomial time.
Open question: Is NP = co-NP? (Believed no, but not proven.)
2.7 Other Classes
| Class | Definition |
|---|---|
| PSPACE | Solvable in polynomial space |
| EXP | Solvable in exponential time |
| BPP | Solvable by randomized algorithm with error (polynomial) |
3. Polynomial-Time Reductions
3.1 Definition
A polynomial-time reduction transforms instances of into instances of in polynomial time, preserving yes/no answers.
If and , then .
If and is NP-hard, then is NP-hard.
3.2 Karp Reductions
A Karp reduction (many-one reduction) maps instance of to instance of :
where is computable in polynomial time.
3.3 Proving NP-Completeness
To prove is NP-complete:
- Show : Given a certificate, verify in polynomial time.
- Show is NP-hard: Reduce a known NP-complete problem to .
Standard NP-complete problems for reduction:
- SAT (Cook’s theorem)
- 3-SAT
- Independent Set
- Vertex Cover
- Clique
- Subset Sum
- Hamiltonian Cycle
- TSP (decision version)
4. Classic NP-Complete Problems
4.1 SAT (Boolean Satisfiability)
Instance: A Boolean formula in conjunctive normal form (CNF): , where each clause is a disjunction of literals.
Question: Is there a truth assignment that satisfies ?
4.2 3-SAT
SAT where each clause has exactly 3 literals.
4.3 Vertex Cover
Instance: Graph , integer .
Question: Is there a set with such that every edge has at least one endpoint in ?
4.4 Clique
Instance: Graph , integer .
Question: Is there a clique of size (a set of pairwise adjacent vertices)?
Reduction from Vertex Cover: has a vertex cover of size iff has a clique of size .
4.5 Independent Set
Instance: Graph , integer .
Question: Is there an independent set of size (a set of vertices, no two adjacent)?
Reduction from Vertex Cover: has a vertex cover of size iff has an independent set of size .
4.6 Subset Sum
Instance: Set of integers, target .
Question: Is there a subset such that ?
4.7 Hamiltonian Cycle
Instance: Graph .
Question: Does contain a cycle that visits each vertex exactly once?
4.8 Traveling Salesman (Decision Version)
Instance: Complete graph with edge weights, integer .
Question: Is there a Hamiltonian cycle of total weight ?
4.9 Graph Coloring
Instance: Graph , integer .
Question: Can be colored with colors such that no adjacent vertices share a color?
- : Solvable in polynomial time (bipartiteness test).
- : NP-complete.
4.10 Reduction Chain
SAT └── 3-SAT └── Independent Set │ ├── Vertex Cover │ └── Clique └── Hamiltonian Cycle └── TSP5. Cook’s Theorem
Theorem (Cook, 1971 / Levin, 1973): SAT is NP-complete.
Proof sketch:
SAT NP: Given a truth assignment, verify each clause is satisfied in time.
SAT is NP-hard: Every NP problem can be decided by a nondeterministic TM in polynomial time. Given any instance of , construct a Boolean formula that is satisfiable iff accepts .
Construction of :
- Encode the computation of on as a tableau (2D grid of cell states).
- Each cell’s state depends on its neighbors (TM transition function).
- Assert: initial configuration is correct, transitions are valid, accepting state is reached.
- All constraints are expressible as CNF clauses.
- Size is polynomial in .
Consequence: Any NP-complete problem can be proven NP-hard by reducing from SAT (or from any other known NP-complete problem).
6. Approximation Algorithms
6.1 Motivation
For NP-hard optimization problems, we seek polynomial-time algorithms that produce near-optimal solutions.
6.2 Approximation Ratio
An algorithm is an -approximation for a minimization problem if:
For maximization: .
6.3 Vertex Cover: 2-Approximation
APPROX_VERTEX_COVER(G): C = {} E' = G.E (copy) while E' is not empty: pick arbitrary (u, v) in E' C = C ∪ {u, v} remove all edges incident to u or v from E' return CRatio: (since each selected edge has at least one endpoint in any vertex cover).
Time: .
6.4 TSP: Christofides’ Algorithm
For metric TSP (triangle inequality):
- Compute MST.
- Find minimum-weight perfect matching on odd-degree vertices.
- Combine MST + matching into Eulerian circuit.
- Shortcut repeated vertices.
Approximation ratio: .
6.5 Set Cover: Greedy -Approximation
APPROX_SET_COVER(U, S): C = {} while C != U: pick S_i in S that maximizes |S_i \ (C ∩ S_i)| C = C ∪ S_i return selected setsApproximation ratio: , where is the -th harmonic number.
6.6 PTAS and FPTAS
PTAS (Polynomial-Time Approximation Scheme): For any , a -approximation running in polynomial time (possibly exponential in ).
FPTAS (Fully PTAS): -approximation running in polynomial time in both and .
Example: Knapsack has an FPTAS.
7. Hardness of Approximation
7.1 Inapproximability Results
Some problems cannot be approximated within any constant factor unless P = NP.
| Problem | Best known ratio | Inapproximability |
|---|---|---|
| Max-Clique | Cannot approximate within unless ZPP = NP | |
| TSP (general) | None (metric: 3/2) | No constant factor unless P = NP |
| Set Cover | (greedy) | Cannot do better than unless P = NP |
7.2 PCP Theorem
PCP Theorem (1992): Every problem in NP has a probabilistically checkable proof that can be verified by reading only a constant number of bits.
Consequence: Max-3SAT cannot be approximated beyond unless P = NP.
7.3 APX, APX-Hard
- APX: Problems with constant-factor polynomial approximation algorithms.
- APX-hard: As hard as any problem in APX under PTAS reductions.
Examples: Vertex cover (APX, 2-approximable), Max-3SAT (APX, 7/8-approximable).
8. Common Pitfalls
Confusing NP with “not polynomial.” NP means verifiable in polynomial time, not “hard.” P NP, so every problem in P is also in NP.
Confusing NP-hard with NP-complete. NP-hard includes problems not in NP (e.g., the halting problem). NP-complete requires the problem to be both NP-hard and in NP.
Incorrect reduction direction. To prove is NP-hard, reduce a known NP-hard problem to (not the other way). The reduction goes from hard to unknown.
Forgetting to prove membership in NP. A reduction alone only shows NP-hardness. You must also show to claim NP-completeness.
Confusing worst-case with average-case. NP-completeness is a worst-case notion. An NP-complete problem might be efficiently solvable on typical inputs.
Assuming approximation exists. Not all NP-hard problems have good approximations. Some are inapproximable within any polynomial factor (unless P = NP).
Using wrong reduction type. Karp (many-one) reductions map yes-instances to yes-instances and no-instances to no-instances. Turing reductions (which use as a subroutine) are more general but give weaker results for NP-hardness.
Worked Examples
Example 1: Proving a Language is in P
Problem: Prove that the language PATH = {(G, u, v) : G is a directed graph with a path from u to v} is in P. Solution: BFS or DFS from u in G takes O(V + E) time, which is polynomial in the input size. If v is reached, accept; otherwise reject. Since there exists a polynomial-time algorithm, PATH is in P.
Example 2: NP-Completeness Reduction
Problem: Prove that 3-SAT is NP-hard by reducing from SAT. Solution: Given a CNF formula phi, transform each clause into a set of exactly 3 literals. For clauses with 1 literal (l), replace with (l OR x OR NOT x) where x is a new variable. For clauses with 2 literals (l1 OR l2), replace with (l1 OR l2 OR x) AND (l1 OR l2 OR NOT x). This produces an equisatisfiable 3-CNF formula in polynomial time. Therefore, 3-SAT is NP-hard. Since 3-SAT is in NP, it is NP-complete.
Summary
- Asymptotic notation (, , , , ) describes growth rates of functions.
- P = solvable in polynomial time; NP = verifiable in polynomial time.
- NP-complete = NP NP-hard; the “hardest” problems in NP.
- Cook’s theorem establishes SAT as the first NP-complete problem.
- Polynomial reductions propagate hardness: if and is NP-hard, then is NP-hard.
- Approximation algorithms provide near-optimal solutions in polynomial time with provable guarantees.
- Hardness of approximation shows limits on how well NP-hard problems can be approximated.
Cross-References
| Topic | Link |
|---|---|
| Algorithm Design | View |
| Data Structures | View |
| Graph Algorithms | View |