TExcelTAD
Home › Approximation Algorithms › Exercises

Exercises

Thirteen problems covering the whole course: ratios and lower bounds, combinatorial algorithms, LP rounding and primal-dual, approximation schemes and hardness. Each has a short Answer and a full Solution; several solutions include a program that checks the result by brute force. Work each one on paper first.

Tip: For any claimed ratio, name the lower bound (for minimization) or upper bound (for maximization) it is measured against, and find an instance where the algorithm meets it. For any hardness claim, name the gap and the assumption.

# Exercise Practises
1 What a dual certifies lower bounds, weak duality
2 Matchings on a path vertex cover, best and worst case
3 Greedy set cover by hand greedy, the HdH_d bound
4 Christofides on a square MST, odd vertices, matching
5 Farthest-first on a line kk-center
6 LPT on two machines makespan, tightness
7 Rounding the 5-cycle half-integrality, LP rounding
8 How many rounds? randomized rounding
9 A formula that splits the two algorithms MAX-SAT, better of two
10 Scaling knapsack profits FPTAS
11 Next Fit is tight bin packing
12 Non-metric kk-center gap-introducing reductions
13 Conditional or not? PCP, UGC, assumptions

Exercise 1: What a dual certifies

A primal-dual algorithm for weighted vertex cover returns a cover of weight 30 together with a feasible dual solution of value 18. What do you know about OPT, and what ratio does this run certify? Could the true ratio be 1?

Answer

18≤OPT≤3018 \le \text{OPT} \le 30, and the run certifies a ratio of at most 3018=53\frac{30}{18} = \frac{5}{3}. The true ratio could be 1 (if OPT = 30) or as large as 53\frac{5}{3} (if OPT = 18).

Solution

By weak duality every feasible dual value is at most the LP optimum, which is at most OPT, so 18≤OPT18 \le \text{OPT}. The cover itself is feasible, so OPT≤30\text{OPT} \le 30. Hence ALGOPT≤3018\frac{\text{ALG}}{\text{OPT}} \le \frac{30}{18}. Nothing more can be concluded without more information: the dual may be far from optimal (The Primal-Dual Method for Vertex Cover).

Exercise 2: Matchings on a path

Let P6P_6 be the path 1−2−3−4−5−61 - 2 - 3 - 4 - 5 - 6. What is the minimum vertex cover? Which maximal matchings make the matching algorithm return the largest and the smallest covers?

Answer

OPT=3\text{OPT} = 3 (for example {2,4,5}\{2, 4, 5\}). The maximal matching {12,34,56}\{12, 34, 56\} gives all 6 vertices (ratio 2); the maximal matching {23,45}\{23, 45\} gives {2,3,4,5}\{2, 3, 4, 5\} (ratio 43\frac{4}{3}).

Solution

The matching {12,34,56}\{12, 34, 56\} has 3 disjoint edges, so OPT≥3\text{OPT} \ge 3, and {2,4,5}\{2, 4, 5\} covers all five edges. A maximal matching on a path with five edges has 2 or 3 edges: {23,45}\{23, 45\} is maximal since 1212, 3434 and 5656 each touch it. The algorithm returns twice the matching size, so 4 or 6 vertices. The program enumerates all maximal matchings.

python · try it yourself
from itertools import combinations

edges = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
def is_matching(M):
    return len({v for e in M for v in e}) == 2 * len(M)
maximal = [M for k in range(1, 4) for M in combinations(edges, k)
           if is_matching(M) and all(not is_matching(M + (e,)) for e in edges if e not in M)]
for M in maximal:
    print("maximal matching", M, "-> cover of size", 2 * len(M))
maximal matching ((1, 2), (4, 5)) -> cover of size 4
maximal matching ((2, 3), (4, 5)) -> cover of size 4
maximal matching ((2, 3), (5, 6)) -> cover of size 4
maximal matching ((1, 2), (3, 4), (5, 6)) -> cover of size 6

Exercise 3: Greedy set cover by hand

U={1,…,8}U = \{1, \ldots, 8\}, unit costs, sets A={1,2,3,4,5}A = \{1, 2, 3, 4, 5\}, B={1,2,6}B = \{1, 2, 6\}, C={3,4,7}C = \{3, 4, 7\}, D={5,6,7,8}D = \{5, 6, 7, 8\}, E={1,8}E = \{1, 8\}. Run greedy (ties broken alphabetically), find OPT, and compare with the bound HdH_d, where dd is the largest set size.

Answer

Greedy picks AA, then DD: 2 sets, which is optimal. H5=13760≈2.28H_5 = \frac{137}{60} \approx 2.28.

Solution

AA covers 5 new elements, the most. Then BB covers {6}\{6\}, CC covers {7}\{7\}, DD covers {6,7,8}\{6, 7, 8\}, EE covers {8}\{8\}: greedy takes DD and everything is covered. One set cannot cover 8 elements, so OPT=2\text{OPT} = 2. Greedy is often optimal on small instances; the HdH_d bound is for the worst case (Greedy Set Cover).

Exercise 4: Christofides on a square

Four cities at the corners of a unit square, with Euclidean distances. Run Christofides' algorithm. What do the MST, the matching and the tour cost, and how does the tour compare with OPT?

Answer

MST 3 (three sides), two odd vertices (the ends of the path) at distance 1, matching 1, tour 4 = OPT.

Solution

All sides have length 1 and the diagonals 2\sqrt{2}, so any MST is a path of three sides, of cost 3. Its two endpoints are adjacent corners (a path through all four corners along three sides starts and ends at corners joined by the fourth side), so the minimum matching on them costs 1. Tree plus matching is the square itself, an Euler circuit of cost 4 that needs no shortcut, and no tour is shorter than the perimeter. The guarantee would allow 32⋅4=6\frac{3}{2} \cdot 4 = 6 (Christofides' Algorithm).

Exercise 5: Farthest-first on a line

Points 0,1,2,10,11,200, 1, 2, 10, 11, 20 on a line, k=2k = 2, centres chosen among the points. Run farthest-first traversal starting at 0 and compare with the optimum.

Answer

Greedy picks 0, then 20: radius 10 (the point 10). The optimum is 9 (for example centres 1 and 11, or 2 and 11). Ratio 109\frac{10}{9}.

Solution

From 0 the farthest point is 20. With centres 0 and 20, point 10 is at distance 10 from both, the worst. For the optimum, some point is always at distance at least 9: if 20 is not a centre, its nearest centre is at most 11, at distance at least 9; if 20 is a centre and the other centre is at most 2, point 11 is at distance at least 9 from both; and if the other centre is 10 or 11, point 0 is at distance at least 10. So OPT≥9\text{OPT} \ge 9, and centres 2 and 11 attain it. The program checks all 15 pairs.

python · try it yourself
from itertools import combinations

pts = [0, 1, 2, 10, 11, 20]
radius = lambda C: max(min(abs(p - c) for c in C) for p in pts)
best = min(radius(C) for C in combinations(pts, 2))
print("greedy {0, 20}: radius", radius((0, 20)))
print("optimum:", best, "attained by", [C for C in combinations(pts, 2) if radius(C) == best])
greedy {0, 20}: radius 10
optimum: 9 attained by [(0, 11), (1, 11), (1, 20), (2, 11), (2, 20)]

The ratio is well within the guarantee of 2 (k-Center and Farthest-First Traversal).

Exercise 6: LPT on two machines

Jobs 3,3,2,2,23, 3, 2, 2, 2 on m=2m = 2 machines. Compute the LPT schedule and OPT, and compare with Graham's bound.

Answer

LPT: {3,2,2}\{3, 2, 2\} and {3,2}\{3, 2\}, makespan 7. OPT = 6 ({3,3}\{3, 3\} and {2,2,2}\{2, 2, 2\}). Ratio 76=43−16\frac{7}{6} = \frac{4}{3} - \frac{1}{6}: the bound is attained.

Solution

The two 3s go to different machines (loads 3, 3); the first 2 goes to machine 1 (5, 3); the second to machine 2 (5, 5); the third to either (7, 5). The total is 12, so OPT≥6\text{OPT} \ge 6, and 3+3=2+2+2=63 + 3 = 2 + 2 + 2 = 6 attains it. Graham's bound for m=2m = 2 is 43−16=76\frac{4}{3} - \frac{1}{6} = \frac{7}{6}: this is the tight family of Longest Processing Time First for m=2m = 2.

Exercise 7: Rounding the 5-cycle

Unit weights on the cycle C5C_5. Find the LP optimum, the result of threshold rounding, and OPT.

Answer

LP =52= \frac{5}{2} with xv=12x_v = \frac{1}{2} everywhere; rounding takes all 5 vertices; OPT = 3. Ratio 53\frac{5}{3}, integrality gap 65\frac{6}{5}.

Solution

Summing the 5 edge constraints gives 2∑vxv≥52\sum_v x_v \ge 5, so LP≥52\text{LP} \ge \frac{5}{2}, attained by all halves (the unique optimum, which is also a vertex, as half-integrality predicts). Rounding at 12\frac{1}{2} takes everything. An odd cycle of 5 needs 3 cover vertices (2 vertices cover at most 4 edges). See Rounding the Vertex Cover LP.

python · try it yourself
from scipy.optimize import linprog

edges = [(i, (i + 1) % 5) for i in range(5)]
res = linprog([1] * 5, A_ub=[[-1 if v in e else 0 for v in range(5)] for e in edges], b_ub=[-1] * 5,
              bounds=[(0, 1)] * 5, method="highs-ds")
print("LP value", round(float(res.fun), 6), " x =", [round(float(x), 6) for x in res.x])
print("rounded cover size", sum(float(x) >= 0.5 for x in res.x))
LP value 2.5  x = [0.5, 0.5, 0.5, 0.5, 0.5]
rounded cover size 5

Exercise 8: How many rounds?

In randomized rounding for set cover, suppose an element lies in exactly kk sets, each with xj∗=1kx^*_j = \frac{1}{k}. What is the probability that one round misses it, for k=1,2,10k = 1, 2, 10? With n=1000n = 1000 elements, how many rounds guarantee that all elements are covered with probability at least 1−1n1 - \frac{1}{n}, using the bound 1e\frac{1}{e} per round?

Answer

(1−1k)k(1 - \frac{1}{k})^k: 0, 14\frac{1}{4}, about 0.3490.349. Rounds: tt with ne−t≤1nn e^{-t} \le \frac{1}{n}, i.e. t≥2ln⁡n≈13.8t \ge 2 \ln n \approx 13.8, so 14 rounds.

Solution

The element is missed only if none of its kk sets is chosen, probability (1−1k)k(1 - \frac{1}{k})^k, which increases towards 1e≈0.368\frac{1}{e} \approx 0.368. After tt rounds the miss probability is at most e−te^{-t}; the union bound over nn elements needs ne−t≤1nn e^{-t} \le \frac{1}{n}, so t≥2ln⁡1000=13.82t \ge 2\ln 1000 = 13.82 (Randomized Rounding for Set Cover).

Exercise 9: A formula that splits the two algorithms

Consider the clauses x1x_1, x2x_2, xˉ1∨xˉ2∨x3\bar x_1 \vee \bar x_2 \vee x_3, xˉ3∨x1\bar x_3 \vee x_1 (unit weights). Compute OPT, the expected value of the uniformly random assignment, an LP optimum, and the expected value of LP rounding for it.

Answer

OPT = 4 (x1=x2=x3=1x_1 = x_2 = x_3 = 1). Random: 12+12+78+34=218=2.625\frac{1}{2} + \frac{1}{2} + \frac{7}{8} + \frac{3}{4} = \frac{21}{8} = 2.625. The LP has the integral optimum y=(1,1,1)y = (1, 1, 1), value 4, and rounding it gives 4.

Solution

The all-true assignment satisfies all four clauses. The random assignment satisfies a clause of length kk with probability 1−2−k1 - 2^{-k}. The LP cannot exceed 4 and the integral point attains it; rounding an integral LP solution reproduces it. So on this formula LP rounding wins, and the better-of-two algorithm returns the optimum (MAX-SAT: Random Assignments, LP Rounding and the Better of Two). The unit clauses are what defeat the random assignment.

Exercise 10: Scaling knapsack profits

Four items with profits 100,60,55,30100, 60, 55, 30 and ε=12\varepsilon = \frac{1}{2}. Compute KK, the rounded profits, and the maximum possible absolute loss.

Answer

K=εPn=0.5⋅1004=12.5K = \frac{\varepsilon P}{n} = \frac{0.5 \cdot 100}{4} = 12.5; rounded profits 8,4,4,28, 4, 4, 2; loss at most nK=50=εPnK = 50 = \varepsilon P.

Solution

⌊100/12.5⌋=8\lfloor 100 / 12.5 \rfloor = 8, ⌊60/12.5⌋=4\lfloor 60 / 12.5 \rfloor = 4, ⌊55/12.5⌋=4\lfloor 55 / 12.5 \rfloor = 4, ⌊30/12.5⌋=2\lfloor 30 / 12.5 \rfloor = 2. Each item loses less than KK in the rounding, so any set loses less than 4K=504K = 50, and OPT≥P=100\text{OPT} \ge P = 100 gives the relative guarantee 1−ε1 - \varepsilon. Note that 60 and 55 become equal: the scaled instance can no longer tell them apart, which is where the loss comes from (A Knapsack FPTAS by Profit Scaling).

Exercise 11: Next Fit is tight

Construct, for every nn, an instance where Next Fit uses 2n2n bins while n+1n + 1 suffice.

Answer

4n4n items alternating 12\frac{1}{2} and δ\delta (with δ=12n\delta = \frac{1}{2n}): 12,δ,12,δ,…\frac{1}{2}, \delta, \frac{1}{2}, \delta, \ldots

Solution

Next Fit puts 12,δ\frac{1}{2}, \delta in a bin; the next 12\frac{1}{2} does not fit (12+δ+12>1\frac{1}{2} + \delta + \frac{1}{2} > 1), so every bin gets one pair: 2n2n bins. The optimum pairs the 2n2n halves into nn bins and puts the 2n2n small items, of total size 2nδ=12n \delta = 1, into one more bin: n+1n + 1. The ratio 2nn+1\frac{2n}{n + 1} tends to 2.

python · try it yourself
def next_fit(items):
    bins, room = 0, 0.0
    for s in items:
        if s > room + 1e-12:
            bins, room = bins + 1, 1.0
        room -= s
    return bins

for n in [2, 5, 20]:
    d = 1 / (2 * n)
    items = [0.5, d] * (2 * n)
    print(f"n = {n:2}: Next Fit {next_fit(items):2} bins, optimum {n + 1:2}, ratio {next_fit(items) / (n + 1):.3f}")
n =  2: Next Fit  4 bins, optimum  3, ratio 1.333
n =  5: Next Fit 10 bins, optimum  6, ratio 1.667
n = 20: Next Fit 40 bins, optimum 21, ratio 1.905

Exercise 12: Non-metric k-center

Show that without the triangle inequality, kk-center has no α\alpha-approximation for any constant α\alpha unless P = NP.

Answer

Reduce from dominating set with distances 1 (edges) and α+1\alpha + 1 (non-edges): OPT is 1 or α+1\alpha + 1.

Solution

Given GG and kk, set d(u,v)=1d(u, v) = 1 for edges and α+1\alpha + 1 otherwise (d(v,v)=0d(v, v) = 0). If GG has a dominating set of size kk, the optimal radius is 1; otherwise some vertex is not adjacent to any chosen centre, and the radius is α+1\alpha + 1. An α\alpha-approximation returns radius at most α<α+1\alpha < \alpha + 1 exactly in the first case, deciding dominating set. With α+1>2\alpha + 1 > 2 these distances violate the triangle inequality, which is why the metric case escapes with ratio 2 (Gap-Introducing Reductions).

Exercise 13: Conditional or not?

For each statement, give the assumption under which it is known: (a) MAX-E3SAT has no (78+ε)(\frac{7}{8} + \varepsilon)-approximation; (b) vertex cover has no (2−ε)(2 - \varepsilon)-approximation; (c) set cover has no (1−ε)ln⁡n(1 - \varepsilon)\ln n-approximation; (d) max-cut has no (0.879)(0.879)-approximation; (e) metric TSP has no PTAS.

Answer

(a) P ≠\ne NP (Håstad). (b) the UGC (Khot and Regev); under P ≠\ne NP only 2−ε\sqrt{2} - \varepsilon. (c) P ≠\ne NP (Dinur and Steurer). (d) the UGC (Khot, Kindler, Mossel and O'Donnell); under P ≠\ne NP only 1617+ε\frac{16}{17} + \varepsilon. (e) P ≠\ne NP (123122\frac{123}{122}, Karpinski, Lampis and Schmied).

Solution

See the table in Tight Thresholds: MAX-3SAT, Clique and Set Cover and the list of consequences in The Unique Games Conjecture. A common mistake is to quote the UGC-based thresholds for vertex cover and max-cut as if they were known under P ≠\ne NP.

Test yourself

A minimization algorithm comes with a feasible dual of value DD and returns cost CC. Which ratio is certified?

  1. DC\frac{D}{C}
  2. CD\frac{C}{D}, since D≤OPTD \le \text{OPT}
  3. C−DC - D
  4. None without OPT

On jobs 3, 3, 2, 2, 2 and two machines, what are LPT's makespan and OPT?

  1. 6 and 6
  2. 7 and 6
  3. 7 and 7
  4. 8 and 6

Why does Next Fit use 2n2n bins on the items 12,δ,12,δ,…\frac{1}{2}, \delta, \frac{1}{2}, \delta, \ldots?

  1. Because the items are sorted
  2. Each bin receives 12\frac{1}{2} and δ\delta, and the next 12\frac{1}{2} no longer fits, so the bin is closed
  3. Because δ\delta items need their own bins
  4. Because OPT is 2n2n