known polynomial-time Algorithm. Many NP-complete problems have efficient approximation algorithms or can be solved Exactly for practical input sizes using branch-and-bound or SAT solvers.
Confusing P with NP. P is the class of problems solvable in polynomial time; NP is the class whose solutions are verifiable in polynomial time. Showing a problem is in NP does not mean it is in P. Most researchers believe P = NP, meaning NP-complete problems have no polynomial-time solution.
Incorrect polynomial-time reductions. A reduction from A to B must transform instances of A into instances of B in polynomial time, preserving the answer. A common error is constructing a transformation that is not polynomial, or that does not preserve the yes/no answer. The reduction must be correct in both directions.
Misunderstanding approximation algorithm guarantees. A 2-approximation for vertex cover means the solution is at most twice the optimal, not exactly twice. The actual ratio depends on the input. Also, some problems (like general TSP) cannot be approximated within any constant factor unless P = NP.
When exact solutions are intractable, we seek approximation algorithms with provable guarantees.
Definition. A ρ-approximation algorithm for a minimisation problem produces a solution of cost at most ρ times the optimal cost. For a maximisation problem, the solution has value at least (1/ρ) times the optimal value.
Theorem 6.3. Greedy vertex cover (repeatedly pick an edge, add both endpoints) is a 2-approximation.
Proof. The algorithm selects a set C of vertices. Each edge in the matching used by the algorithm contributes 2 vertices to C. Let M∗ be a maximum matching. Then ∣C∣=2∣M∗∣≤2⋅∣OPT∣Since OPT must contain at least one endpoint of every edge in M∗ (and M∗ is maximum, so ∣M∗∣≥ the size of any matching). Therefore the approximation ratio is at most 2. ■
Theorem 6.4 (Metric TSP). The Christofides algorithm is a 3/2-approximation for TSP with the triangle inequality.
Proof. The algorithm computes an MST (≤OPT), finds a minimum-weight perfect matching M on the odd-degree vertices of the MST (∣M∣≤OPT/2), and combines them into an Eulerian tour which is shortcut to a Hamiltonian cycle. The total weight is at most MST+∣M∣≤OPT+OPT/2=23OPT. ■
Theorem 6.5 (Inapproximability). Unless P = NP, TSP (general, without triangle inequality) has no polynomial-time approximation algorithm with any constant ratio.
Proof sketch. If a c-approximation existed for TSP, we could use it to solve the Hamiltonian cycle problem (which is NP-complete): given a graph GConstruct a TSP instance with edge weight 1 for existing edges and weight cn+1 for non-edges. If the approximation returns a tour of weight n Then G has a Hamiltonian cycle. Otherwise, the tour weight is at least n−1+cn+1>cn So the approximation ratio would exceed cContradiction. ■
Theorem 6.6 (SET COVER). The greedy algorithm for SET COVER is a (lnn+O(1))-approximation, where n is the size of the universe.
Proof sketch. At each step, the greedy algorithm picks the set covering the most uncovered elements. Let ci be the cost of the i-th set picked, and let ni be the number of newly covered elements. Then ci/ni≤OPT/(n−∑j<inj) (otherwise OPT could not cover the remaining elements at lower cost). Summing gives the lnn+O(1) bound. ■
Worked Example: Greedy Set Cover
Universe U=1,2,3,4,5,6. Sets: S1=1,2,3, S2=2,4, S3=3,5,6, S4=4,5, S5=1,4,6. All sets have equal cost 1.
Greedy:
- Pick S1 (covers 3 elements, tied with S3). Covered: 1,2,3.
- Remaining: 4,5,6. S3 covers 5,6 (2 new), S4 covers 4,5 (2 new), S5 covers 4,6 (2 new). Pick S3. Covered: 1,2,3,5,6.
- Remaining: 4. Pick S2 (or S4 or S5). Covered: 1,2,3,4,5,6.
Greedy solution: S1,S3,S2Size 3. Optimal: S1,S5,S4 or S3,S5,S2Size 3. Here greedy is optimal, but it is a lnn-approximation.
Las Vegas algorithms always produce the correct answer but have randomised running time.
Monte Carlo algorithms always run in polynomial time but may produce incorrect answers with some probability.
| Property | Las Vegas | Monte Carlo |
|---|
| Correctness | Always correct | Bounded error prob |
| Running time | Randomised | Bounded |
| Example | Randomised Quicksort | Miller-Rabin primality |
Theorem 6.6. A Monte Carlo algorithm with error probability ϵ can be amplified to error probability ϵk by running it k times and taking the majority vote (for decision problems with one-sided error) or the most frequent answer (for two-sided error).
Proof. For one-sided error: Pr[all k runs fail]=ϵk. For two-sided error with majority vote: by the Chernoff bound, the probability that the majority is wrong decreases exponentially in k. ■
Problem. Find the k-th smallest element in an unsorted array.
Algorithm (Randomised Select): Like quicksort, but recurse only into the partition containing the k-th element.
Theorem 6.7. Randomised select has expected running time O(n).
Proof. The expected number of comparisons satisfies T(n)≤n+n1∑i=1n(T(max(i−1,n−i))). This can be shown to satisfy T(n)=O(n) by induction. ■
Worked Example: Randomised Select
Find the 3rd smallest element in [7,2,1,6,8,5,3,4].
Pivot = randomly chosen. Suppose pivot = 5 (index 5).
Partition: [7,2,1,6,8,5,3,4]→[2,1,3,4,5,6,8,7].
Pivot 5 is at index 4 (0-indexed). We want rank 3 (0-indexed rank 2). 4>2 So recurse on left: [2,1,3,4].
Pivot = randomly chosen. Suppose pivot = 3.
Partition: [2,1,3,4]→[2,1,3,4].
Pivot 3 is at index 2. We want rank 2. 2=2 So return 3.
The 3rd smallest element is 3.
Worked Example: Miller-Rabin Primality Test
Test whether n=561 is prime (it is not; 561=3×11×17A Carmichael number).
Write n−1=560=24×35 So s=4, d=35.
Choose random base a=2.
Compute admodn=235mod561.
25=32, 210=1024mod561=463, 220=4632mod561=67, 235=220⋅210⋅25mod561=67⋅463⋅32mod561.
67×463=31021mod561=31021−55×561=31021−30855=166. 166×32=5312mod561=5312−9×561=5312−5049=263.
admodn=263=1 and =n−1=560.
Now square: 2632mod561=69169mod561=69169−123×561=69169−69003=166=560.
Square: 1662mod561=27556mod561=27556−49×561=27556−27489=67=560.
Square: 672mod561=4489mod561=4489−8×561=4489−4488=1.
We got 1 on the last squaring, but the previous result was 67=1,560. So a=2 is a witness that 561 is composite. Output: COMPOSITE.
The error probability of Miller-Rabin is at most 1/4 per random base, so k iterations give error ≤4−k.
Definition. A family H of hash functions from U to 0,…,m−1 is universal if for any distinct x,y∈U, Prh∈H[h(x)=h(y)]≤1/m.
Theorem 6.8. With a universal hash family and chaining, the expected number of collisions for any element is at most n/m.
Proof. For a fixed element xLet Xiy be the indicator that h(x)=h(yi) where y1,…,yn are the other n−1 elements. Then E[Xiy]=Pr[h(x)=h(yi)]≤1/m by universality. By linearity of expectation, the expected number of collisions is ∑iE[Xiy]≤(n−1)/m. ■
Example: Multi-pop Stack. A stack supports push (O(1)) and multi-pop(k) (pop k elements, cost min(k,s) where s is the stack size). Although a single multi-pop can cost O(n)A sequence of n push/multi-pop operations costs O(n) total: each element is pushed once and popped at most once.
Theorem 6.9. The amortised cost per operation for a multi-pop stack is O(1).
Proof. In a sequence of n operations, each element is pushed at most once and popped at most once. The total cost is at most 2n=O(n) So the amortised cost per operation is O(n)/n=O(1). ■
Each operation is charged an amortised cost. The difference between the amortised cost and the actual cost is stored as credit. The credit must always be non-negative.
Example: Binary Counter. A k-bit binary counter supports increment (flip bits from the right until a 0 is found).
Assign amortised cost of 2 per increment: 1 to pay for flipping a 0 to 1, 1 stored as credit. When a 1 is flipped back to 0, the credit from when it was set pays for the flip.
Theorem 6.10. The amortised cost per increment of a binary counter is O(1).
Proof. Each bit flips from 0 to 1 at most once between consecutive resets to 0. The credit stored on a 1 bit pays for the subsequent flip back to 0. The total credit is always non-negative. ■
The potential function Φ maps data structure states to non-negative real numbers. The amortised cost of the i-th operation is c^i=ci+Φ(Di)−Φ(Di−1).
Theorem 6.11. If Φ(Di)≥0 for all i Then ∑i=1nc^i≥∑i=1nci.
Worked Example: Binary Counter with Potential Method
Define Φ(Di)= number of 1-bits in the counter after i operations.
For increment i: let ti be the number of trailing 1s flipped. The actual cost is ti+1 (flipping ti ones and one zero). The number of 1-bits changes by 1−ti.
c^i=(ti+1)+Φ(Di)−Φ(Di−1)=(ti+1)+(1−ti)=2
The amortised cost per increment is exactly 2, i.e., O(1).
Worked Example: Splay Tree Amortised Analysis
A splay tree is a BST where every access is followed by a splay operation that moves the accessed node to the root using rotations. Three types of rotations are used: zig (single rotation when parent is root), zig-zig (two rotations in the same direction), and zig-zag (two rotations in alternating directions).
Access Lemma. The amortised cost of splaying a node x in a splay tree with n nodes is O(logn).
Proof sketch. Define the potential as Φ(T)=∑x∈Tlogsize(x)Where size(x) is the number of nodes in the subtree rooted at x (including x). Define the rank r(x)=logsize(x).
The amortised cost of a splay step at node x with parent p and grandparent g is c^=1+r"(x)−r(x)Where primes denote ranks after the step.
- Zig: c^=1+r′(x)−r(x)≤1+3(r′(x)−r(x)).
- Zig-zig: c^=2+r′(x)−r(x)≤3(r′(x)−r(x)).
- Zig-zag: c^=2+r′(x)−r(x)≤3(r′(x)−r(x)).
Summing over all splay steps: c^total≤1+3(rfinal(x)−rinitial(x))≤1+3logn=O(logn). ■
Corollary. A sequence of m splay tree operations takes O(mlogn) amortised time.