Randomized Rounding for Set Cover
Threshold rounding gives an -approximation for set cover, which is useless when an element can lie in many sets. Randomized rounding, introduced by Raghavan and Thompson (1987), reads the fractional solution as a probability distribution: a set with is chosen with probability . One round of sampling costs LP in expectation but leaves some elements uncovered; repeating it times covers everything with high probability. The result is an -approximation, matching greedy's up to the constant, and a method that extends to problems greedy cannot handle.
The algorithm
Weighted set cover with universe of elements, sets , weights . Let be an optimal solution of
RANDOMIZED-ROUNDING(U, sets, w, c)
1 x* = an optimal LP solution
2 C = empty
3 repeat t = ceil(c ln n) times -- independent rounds
4 for each set j: add j to C with probability x*_j
5 if some element is uncovered, add for it the cheapest set containing it -- rarely needed
6 return CAnalysis of one round
Fix an element and let . In one round, stays uncovered only if no set of is chosen:
using and the LP constraint for . The expected cost of a round is by linearity of expectation.
Theorem. With rounds (), the sets chosen in lines 3 and 4 cover every element with probability at least , and their expected cost is at most .
Proof. Rounds are independent, so . By the union bound over the elements, the probability that any element is uncovered is at most . The cost of the union is at most the sum of the costs of the rounds, whose expectation is .
To turn "expected cost" and "high probability" into one statement: by Markov's inequality the cost exceeds with probability at most , so with probability at least the result is a cover of cost at most . If it is not, run the algorithm again; the expected number of attempts is a constant. Line 5 is a cheaper safety net: each fix-up costs at most OPT (the cheapest set containing is at most the weight of the optimal set containing ), and is needed with probability at most per run, so it adds at most to the expectation. Either way, the algorithm is an -approximation.
import math
import random
from itertools import combinations
from scipy.optimize import linprog
rng = random.Random(1)
n, m = 20, 14
sets = [set(rng.sample(range(n), rng.randint(3, 7))) for _ in range(m)]
for e in range(n):
if not any(e in S for S in sets):
sets[rng.randrange(m)].add(e)
w = [rng.randint(2, 9) for _ in range(m)]
A = [[-1 if e in S else 0 for S in sets] for e in range(n)]
lp = linprog(w, A_ub=A, b_ub=[-1] * n, bounds=[(0, 1)] * m, method="highs")
x = [float(v) for v in lp.x]
opt = min(sum(w[j] for j in C) for k in range(1, m + 1) for C in combinations(range(m), k)
if set().union(*(sets[j] for j in C)) == set(range(n)))
print("LP =", round(float(lp.fun), 3), " OPT =", opt, " fractional sets:", sum(0 < v < 1 for v in x))
def rounding(x, rounds, rng):
C = set()
for _ in range(rounds):
C |= {j for j in range(m) if rng.random() < x[j]}
covered, fixes = set().union(*(sets[j] for j in C)), 0
for e in range(n): # line 5: fix-ups
if e not in covered:
j = min((j for j in range(m) if e in sets[j]), key=lambda j: w[j])
C.add(j)
covered |= sets[j]
fixes += 1
return sum(w[j] for j in C), fixes
t = math.ceil(2 * math.log(n))
for rounds in [1, 3, t]:
results = [rounding(x, rounds, rng) for _ in range(200)]
avg = sum(c for c, _ in results) / len(results)
needed = sum(f > 0 for _, f in results)
print(f"{rounds:2} rounds: average cost {avg:6.2f} ({avg / opt:.2f} x OPT), runs that needed fix-ups {needed:3} of 200")LP = 28.333 OPT = 31 fractional sets: 6
1 rounds: average cost 36.17 (1.17 x OPT), runs that needed fix-ups 143 of 200
3 rounds: average cost 41.99 (1.35 x OPT), runs that needed fix-ups 18 of 200
6 rounds: average cost 46.80 (1.51 x OPT), runs that needed fix-ups 1 of 200
With one round the sampled sets miss elements most of the time and the fix-ups do much of the work; with rounds misses are rare, at the price of paying several rounds. The bound is loose here because many sets are chosen in several rounds but paid only once.
Scaling instead of repeating
An equivalent and slightly cheaper version chooses set once, with probability for . The same computation gives , about , and expected cost at most . Careful versions of this analysis (with alterations and fix-ups) give , the same leading constant as greedy and as the hardness threshold.
import math
import random
from scipy.optimize import linprog
rng = random.Random(1)
n, m = 20, 14
sets = [set(rng.sample(range(n), rng.randint(3, 7))) for _ in range(m)]
for e in range(n):
if not any(e in S for S in sets):
sets[rng.randrange(m)].add(e)
w = [rng.randint(2, 9) for _ in range(m)]
A = [[-1 if e in S else 0 for S in sets] for e in range(n)]
x = [float(v) for v in linprog(w, A_ub=A, b_ub=[-1] * n, bounds=[(0, 1)] * m, method="highs").x]
# exact probability that each element is missed when set j is taken with probability min(1, alpha x_j)
for alpha in [1, 2, math.log(n), math.log(n) + 2]:
miss = [math.prod(1 - min(1, alpha * x[j]) for j in range(m) if e in sets[j]) for e in range(n)]
cost = sum(w[j] * min(1, alpha * x[j]) for j in range(m))
print(f"alpha = {alpha:5.2f}: expected cost {cost:6.2f}, worst miss probability {max(miss):.4f} <= e^-alpha = {math.exp(-alpha):.4f}")alpha = 1.00: expected cost 28.33, worst miss probability 0.2963 <= e^-alpha = 0.3679
alpha = 2.00: expected cost 40.67, worst miss probability 0.0370 <= e^-alpha = 0.1353
alpha = 3.00: expected cost 47.97, worst miss probability 0.0000 <= e^-alpha = 0.0500
alpha = 5.00: expected cost 48.00, worst miss probability 0.0000 <= e^-alpha = 0.0068
These numbers are exact, not sampled: the probability of missing element is a product over the sets containing it. The bound is respected element by element, and is often far from tight because many elements have LP coverage well above 1 once multiplied by .
Derandomization
The method of conditional expectations (The Method of Conditional Expectations) turns the scaled rounding into a deterministic algorithm. Define the pessimistic estimator . With its expectation is at most . Decide the sets one by one, each time choosing the option (take or skip) that does not increase the conditional expectation of ; both terms are easy to compute exactly given the decisions so far, the second as a product over the undecided sets. At the end nothing is random: , the second term is twice the number of uncovered elements, so it is 0, and the cost is below .
How good can rounding be?
The set cover LP has an integrality gap of (Integrality Gaps), so no rounding of this LP beats by more than a constant factor; Dinur and Steurer (2014) showed that no polynomial algorithm whatsoever achieves unless P = NP. The real power of randomized rounding appears where greedy has no natural analogue: problems with packing and covering constraints mixed, such as integer multicommodity flow and congestion minimization (route each demand on one path so that the most loaded edge carries as little as possible). There Raghavan and Thompson's analysis with Chernoff bounds gives congestion times the LP optimum, the bound that gave the method its name.
Test yourself
Why is the probability that one round misses element at most ?
- Because each set has probability at most
- It equals over the sets containing , which is at most by and the LP constraint
- By Markov's inequality
- Because the rounds are dependent
How many independent rounds make all elements covered with probability at least ?
- One round
- About rounds, so each element is missed with probability at most , then the union bound
- rounds
- rounds
What is the expected cost of one round of randomized rounding?
- OPT
- Exactly the LP value , by linearity of expectation
- times LP
- It cannot be computed