Skip to content

Advanced Topics

6.1 NP-Completeness

6.1.1 P, NP, and NP-Completeness

P: The class of decision problems solvable in polynomial time by a deterministic Turing machine.

NP: The class of decision problems solvable in polynomial time by a non-deterministic Turing Machine. Equivalently, problems whose “yes” instances have polynomial-time verifiable certificates.

NP-hard: A problem AA is NP-hard if every problem in NP can be reduced to AA in polynomial Time.

NP-complete: A problem is NP-complete if it is in NP and NP-hard.

Theorem 6.1. If any NP-complete problem is in P, then P = NP.

6.1.2 Polynomial-Time Reductions

A polynomial-time reduction from problem AA to problem BB is a polynomial-time algorithm that Transforms instances of AA into instances of BBPreserving the answer.

Lemma 6.1. If ApBA \leq_p B and BPB \in PThen APA \in P.

Lemma 6.2. If ApBA \leq_p B and AA is NP-hard, then BB is NP-hard.

6.1.3 The Cook-Levin Theorem

Theorem 6.2 (Cook-Levin, 1971). SAT is NP-complete.

Proof sketch. We show that every problem in NP reduces to SAT. Let LNPL \in \mathrm{NP}. There exists a polynomial-time non-deterministic Turing machine MM that decides LLRunning in time p(n)p(n) on inputs of length nn. For an input xxWe construct a Boolean formula ϕx\phi_x that is satisfiable if and only if MM accepts xx.

The formula encodes:

  1. Tableau variables: T[i,j,σ]=1T[i, j, \sigma] = 1 iff cell (i,j)(i, j) of the computation tableau contains symbol σ\sigma. The tableau has p(n)+1p(n) + 1 rows and p(n)p(n) columns.
  2. Initial configuration: Row 0 encodes the initial state of MM on input xx.
  3. Transition constraints: Each 2×32 \times 3 window of the tableau corresponds to a valid transition of MM.
  4. Accepting configuration: Some cell in the last row contains the accept state.

Each of these constraints can be expressed as a polynomial-size CNF formula. The total formula ϕx\phi_x has size polynomial in nn and is satisfiable iff MM accepts xx. \blacksquare

6.1.4 Classic NP-Complete Problems

SAT. Given a Boolean formula in CNF, is there a satisfying assignment?

3-SAT. SAT restricted to clauses with exactly 3 literals.

Vertex Cover. Given a graph G=(V,E)G = (V, E) and integer kkIs there a vertex cover of size k\leq k?

Travelling Salesman Problem (decision version). Given a weighted graph and bound BBIs there a Tour of total weight B\leq B?

Subset Sum. Given a set of integers and a target TTIs there a subset summing to TT?

Clique. Given a graph GG and integer kkDoes GG contain a clique of size kk?

6.1.5 Proof Strategy for NP-Completeness

To prove a problem BB is NP-complete:

  1. Show BNPB \in \mathrm{NP} (polynomial-time verifiable certificate).
  2. Show a known NP-complete problem AA reduces to BB: ApBA \leq_p B.

Example. 3-SAT p\leq_p Vertex Cover: construct a graph from the 3-SAT formula where each Variable and each clause become vertices, and edges enforce the constraint that a satisfying Assignment corresponds to a vertex cover.

Worked Example: 3-SAT $\leq_p$ Vertex Cover Reduction

Reduce 3-SAT formula ϕ=(x1xˉ2x3)(xˉ1x2x3)(x1x2xˉ3)\phi = (x_1 \vee \bar{x}_2 \vee x_3) \wedge (\bar{x}_1 \vee x_2 \vee x_3) \wedge (x_1 \vee x_2 \vee \bar{x}_3) to a vertex cover instance.

For each variable xix_iCreate two vertices xix_i and xˉi\bar{x}_i connected by an edge (the “literal edge”).

For each clause CjC_jCreate a triangle of 3 vertices cj1,cj2,cj3c_{j1}, c_{j2}, c_{j3}.

For each clause vertex cjkc_{jk}Connect it to the literal vertex corresponding to the kk-th literal of clause jj.

Claim: ϕ\phi is satisfiable iff the graph has a vertex cover of size k+2mk + 2m where kk is the number of variables and mm is the number of clauses.

(\Rightarrow) If ϕ\phi is satisfiable, include in the cover: for each variable, the literal vertex matching the truth assignment (e.g., x1x_1 if x1=truex_1 = \mathrm{true}, xˉ1\bar{x}_1 if x1=falsex_1 = \mathrm{false}). This covers all literal edges (kk vertices). For each clause triangle, at least one literal in the clause is true, so the corresponding literal vertex covers one of the three edges from the triangle. Include the other two vertices of the triangle (2m2m vertices total).

(\Leftarrow) A vertex cover of size k+2mk + 2m must include exactly one endpoint of each literal edge (otherwise the triangle requires 3 vertices). Set each variable according to which literal vertex is in the cover. Each clause triangle has exactly one uncovered vertex, whose edge to a literal vertex is covered by that literal vertex, meaning the clause is satisfied.

The reduction takes polynomial time (number of vertices and edges is polynomial in the formula size).

:::caution Common Pitfall NP-hardness does not mean the problem is unsolvable. It means there is no 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.

6.2 Approximation Algorithms

When exact solutions are intractable, we seek approximation algorithms with provable guarantees.

Definition. A ρ\rho-approximation algorithm for a minimisation problem produces a solution of cost at most ρ\rho times the optimal cost. For a maximisation problem, the solution has value at least (1/ρ)(1/\rho) 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 CC of vertices. Each edge in the matching used by the algorithm contributes 2 vertices to CC. Let MM^* be a maximum matching. Then C=2M2OPT|C| = 2|M^*| \leq 2 \cdot |\mathrm{OPT}|Since OPT must contain at least one endpoint of every edge in MM^* (and MM^* is maximum, so M|M^*| \geq the size of any matching). Therefore the approximation ratio is at most 2. \blacksquare

Theorem 6.4 (Metric TSP). The Christofides algorithm is a 3/23/2-approximation for TSP with the triangle inequality.

Proof. The algorithm computes an MST (OPT\leq \mathrm{OPT}), finds a minimum-weight perfect matching MM on the odd-degree vertices of the MST (MOPT/2\lvert M \rvert \leq \mathrm{OPT}/2), and combines them into an Eulerian tour which is shortcut to a Hamiltonian cycle. The total weight is at most MST+MOPT+OPT/2=32OPT\mathrm{MST} + \lvert M \rvert \leq \mathrm{OPT} + \mathrm{OPT}/2 = \frac{3}{2}\mathrm{OPT}. \blacksquare

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 cc-approximation existed for TSP, we could use it to solve the Hamiltonian cycle problem (which is NP-complete): given a graph GGConstruct a TSP instance with edge weight 1 for existing edges and weight cn+1cn + 1 for non-edges. If the approximation returns a tour of weight nnThen GG has a Hamiltonian cycle. Otherwise, the tour weight is at least n1+cn+1>cnn - 1 + cn + 1 \gt cnSo the approximation ratio would exceed ccContradiction. \blacksquare

Theorem 6.6 (SET COVER). The greedy algorithm for SET COVER is a (lnn+O(1))(\ln n + O(1))-approximation, where nn is the size of the universe.

Proof sketch. At each step, the greedy algorithm picks the set covering the most uncovered elements. Let cic_i be the cost of the ii-th set picked, and let nin_i be the number of newly covered elements. Then ci/niOPT/(nj<inj)c_i / n_i \leq \mathrm{OPT} / (n - \sum_{j \lt i} n_j) (otherwise OPT could not cover the remaining elements at lower cost). Summing gives the lnn+O(1)\ln n + O(1) bound. \blacksquare

Worked Example: Greedy Set Cover

Universe U=1,2,3,4,5,6U = \\{1, 2, 3, 4, 5, 6\\}. Sets: S1=1,2,3S_1 = \\{1, 2, 3\\}, S2=2,4S_2 = \\{2, 4\\}, S3=3,5,6S_3 = \\{3, 5, 6\\}, S4=4,5S_4 = \\{4, 5\\}, S5=1,4,6S_5 = \\{1, 4, 6\\}. All sets have equal cost 1.

Greedy:

  1. Pick S1S_1 (covers 3 elements, tied with S3S_3). Covered: 1,2,3\\{1, 2, 3\\}.
  2. Remaining: 4,5,6\\{4, 5, 6\\}. S3S_3 covers 5,6\\{5, 6\\} (2 new), S4S_4 covers 4,5\\{4, 5\\} (2 new), S5S_5 covers 4,6\\{4, 6\\} (2 new). Pick S3S_3. Covered: 1,2,3,5,6\\{1, 2, 3, 5, 6\\}.
  3. Remaining: 4\\{4\\}. Pick S2S_2 (or S4S_4 or S5S_5). Covered: 1,2,3,4,5,6\\{1, 2, 3, 4, 5, 6\\}.

Greedy solution: S1,S3,S2\\{S_1, S_3, S_2\\}Size 3. Optimal: S1,S5,S4\\{S_1, S_5, S_4\\} or S3,S5,S2\\{S_3, S_5, S_2\\}Size 3. Here greedy is optimal, but it is a lnn\ln n-approximation.

6.3 Randomised Algorithms

6.3.1 Las Vegas vs. Monte Carlo

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.

PropertyLas VegasMonte Carlo
CorrectnessAlways correctBounded error prob
Running timeRandomisedBounded
ExampleRandomised QuicksortMiller-Rabin primality

Theorem 6.6. A Monte Carlo algorithm with error probability ϵ\epsilon can be amplified to error probability ϵk\epsilon^k by running it kk 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\Pr[\mathrm{all}~ k \mathrm{~runs~fail}] = \epsilon^k. For two-sided error with majority vote: by the Chernoff bound, the probability that the majority is wrong decreases exponentially in kk. \blacksquare

6.3.2 Randomised Select (Quickselect)

Problem. Find the kk-th smallest element in an unsorted array.

Algorithm (Randomised Select): Like quicksort, but recurse only into the partition containing the kk-th element.

Theorem 6.7. Randomised select has expected running time O(n)O(n).

Proof. The expected number of comparisons satisfies T(n)n+1ni=1n(T(max(i1,ni)))T(n) \leq n + \frac{1}{n}\sum_{i=1}^{n}(T(\max(i-1, n-i))). This can be shown to satisfy T(n)=O(n)T(n) = O(n) by induction. \blacksquare

Worked Example: Randomised Select

Find the 3rd smallest element in [7,2,1,6,8,5,3,4][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][7, 2, 1, 6, 8, 5, 3, 4] \to [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>24 > 2So recurse on left: [2,1,3,4][2, 1, 3, 4].

Pivot = randomly chosen. Suppose pivot = 3.

Partition: [2,1,3,4][2,1,3,4][2, 1, 3, 4] \to [2, 1, 3, 4].

Pivot 3 is at index 2. We want rank 2. 2=22 = 2So return 3.

The 3rd smallest element is 3.

Worked Example: Miller-Rabin Primality Test

Test whether n=561n = 561 is prime (it is not; 561=3×11×17561 = 3 \times 11 \times 17A Carmichael number).

Write n1=560=24×35n - 1 = 560 = 2^4 \times 35So s=4s = 4, d=35d = 35.

Choose random base a=2a = 2.

Compute admodn=235mod561a^d \bmod n = 2^{35} \bmod 561.

25=322^5 = 32, 210=1024mod561=4632^{10} = 1024 \bmod 561 = 463, 220=4632mod561=672^{20} = 463^2 \bmod 561 = 67, 235=22021025mod561=6746332mod5612^{35} = 2^{20} \cdot 2^{10} \cdot 2^5 \bmod 561 = 67 \cdot 463 \cdot 32 \bmod 561.

67×463=31021mod561=3102155×561=3102130855=16667 \times 463 = 31021 \bmod 561 = 31021 - 55 \times 561 = 31021 - 30855 = 166. 166×32=5312mod561=53129×561=53125049=263166 \times 32 = 5312 \bmod 561 = 5312 - 9 \times 561 = 5312 - 5049 = 263.

admodn=2631a^d \bmod n = 263 \neq 1 and n1=560\neq n - 1 = 560.

Now square: 2632mod561=69169mod561=69169123×561=6916969003=166560263^2 \bmod 561 = 69169 \bmod 561 = 69169 - 123 \times 561 = 69169 - 69003 = 166 \neq 560.

Square: 1662mod561=27556mod561=2755649×561=2755627489=67560166^2 \bmod 561 = 27556 \bmod 561 = 27556 - 49 \times 561 = 27556 - 27489 = 67 \neq 560.

Square: 672mod561=4489mod561=44898×561=44894488=167^2 \bmod 561 = 4489 \bmod 561 = 4489 - 8 \times 561 = 4489 - 4488 = 1.

We got 1 on the last squaring, but the previous result was 671,56067 \neq 1, 560. So a=2a = 2 is a witness that 561 is composite. Output: COMPOSITE.

The error probability of Miller-Rabin is at most 1/41/4 per random base, so kk iterations give error 4k\leq 4^{-k}.

6.3.3 Hashing with Universal Hash Functions

Definition. A family H\mathcal{H} of hash functions from UU to 0,,m1\\{0, \ldots, m - 1\\} is universal if for any distinct x,yUx, y \in U, PrhH[h(x)=h(y)]1/m\Pr_{h \in \mathcal{H}}[h(x) = h(y)] \leq 1/m.

Theorem 6.8. With a universal hash family and chaining, the expected number of collisions for any element is at most n/mn/m.

Proof. For a fixed element xxLet XiyX_{iy} be the indicator that h(x)=h(yi)h(x) = h(y_i) where y1,,yny_1, \ldots, y_n are the other n1n-1 elements. Then E[Xiy]=Pr[h(x)=h(yi)]1/m\mathrm{E}[X_{iy}] = \Pr[h(x) = h(y_i)] \leq 1/m by universality. By linearity of expectation, the expected number of collisions is iE[Xiy](n1)/m\sum_i \mathrm{E}[X_{iy}] \leq (n-1)/m. \blacksquare

6.4 Amortised Analysis (Detailed)

6.4.1 Aggregate Analysis

Example: Multi-pop Stack. A stack supports push (O(1)O(1)) and multi-pop(kk) (pop kk elements, cost min(k,s)\min(k, s) where ss is the stack size). Although a single multi-pop can cost O(n)O(n)A sequence of nn push/multi-pop operations costs O(n)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)O(1).

Proof. In a sequence of nn operations, each element is pushed at most once and popped at most once. The total cost is at most 2n=O(n)2n = O(n)So the amortised cost per operation is O(n)/n=O(1)O(n)/n = O(1). \blacksquare

6.4.2 Accounting Method

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 kk-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)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. \blacksquare

6.4.3 Potential Method

The potential function Φ\Phi maps data structure states to non-negative real numbers. The amortised cost of the ii-th operation is c^i=ci+Φ(Di)Φ(Di1)\hat{c}_i = c_i + \Phi(D_i) - \Phi(D_{i-1}).

Theorem 6.11. If Φ(Di)0\Phi(D_i) \geq 0 for all iiThen i=1nc^ii=1nci\sum_{i=1}^{n} \hat{c}_i \geq \sum_{i=1}^{n} c_i.

Worked Example: Binary Counter with Potential Method

Define Φ(Di)=\Phi(D_i) = number of 1-bits in the counter after ii operations.

For increment ii: let tit_i be the number of trailing 1s flipped. The actual cost is ti+1t_i + 1 (flipping tit_i ones and one zero). The number of 1-bits changes by 1ti1 - t_i.

c^i=(ti+1)+Φ(Di)Φ(Di1)=(ti+1)+(1ti)=2\hat{c}_i = (t_i + 1) + \Phi(D_i) - \Phi(D_{i-1}) = (t_i + 1) + (1 - t_i) = 2

The amortised cost per increment is exactly 2, i.e., O(1)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 xx in a splay tree with nn nodes is O(logn)O(\log n).

Proof sketch. Define the potential as Φ(T)=xTlogsize(x)\Phi(T) = \sum_{x \in T} \log \mathrm{size}(x)Where size(x)\mathrm{size}(x) is the number of nodes in the subtree rooted at xx (including xx). Define the rank r(x)=logsize(x)r(x) = \log \mathrm{size}(x).

The amortised cost of a splay step at node xx with parent pp and grandparent gg is c^=1+r"(x)r(x)\hat{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))\hat{c} = 1 + r'(x) - r(x) \leq 1 + 3(r'(x) - r(x)).
  • Zig-zig: c^=2+r(x)r(x)3(r(x)r(x))\hat{c} = 2 + r'(x) - r(x) \leq 3(r'(x) - r(x)).
  • Zig-zag: c^=2+r(x)r(x)3(r(x)r(x))\hat{c} = 2 + r'(x) - r(x) \leq 3(r'(x) - r(x)).

Summing over all splay steps: c^total1+3(rfinal(x)rinitial(x))1+3logn=O(logn)\hat{c}_{\mathrm{total} \leq 1 + 3(r_{\mathrm{final}(x) - r_{\mathrm{initial}(x)) \leq 1 + 3 \log n = O(\log n)}}}. \blacksquare

Corollary. A sequence of mm splay tree operations takes O(mlogn)O(m \log n) amortised time.

:::