The greedy algorithm for weighted set cover #
This module formalizes the greedy algorithm for weighted set cover and Chvátal's harmonic bound on its cost ([WS11], Section 1.6, Algorithm 1.2, Theorems 1.11 and 1.12; [Chv79]).
Model #
An instance consists of a ground set U : Finset α, sets S : ι → Finset α, weights w : ι → 𝕜
in a linearly ordered field (ℚ, ℝ, …), and a list J : List ι of the candidate sets. The
weights of the candidates are assumed nonnegative where the bounds need it, as in
[WS11], Section 1.2. A cover is a finite set C of candidates whose sets contain
every element of U; its cost is ∑ j ∈ C, w j. A fractional cover gives each candidate j ∈ C
a weight x j ≥ 0 such that the sets containing each element of U have total weight at least
1; a cover is the fractional cover with x = 1.
Algorithm #
greedy S w J U repeatedly chooses, among the candidates meeting the set R of elements not yet
covered, one minimizing the cost per newly covered element w j / |S j ∩ R|
(costPerUncovered), breaking ties toward the earliest candidate in J (List.argmin), and stops
when no candidate meets R. Each round covers at least one new element, so U.card rounds suffice
(greedyLoop is given that many rounds of fuel). The output is the list of chosen candidates, in
order. Ratios are compared exactly in 𝕜.
Source mapping #
[WS11], Algorithm 1.2, chooses argmin_{j : Ŝ_j ≠ ∅} w_j / |Ŝ_j| with Ŝ_j the
uncovered elements of S_j, and stops when the chosen sets cover the ground set; here the loop
also stops when no candidate meets the remaining elements, which only happens on infeasible inputs
(greedy_covers). The analysis follows Chvátal's: for every fractional cover x,
∑_{i ∈ greedy} w i ≤ ∑_{j ∈ C} x j w j H(|S j ∩ U|) (sum_greedy_le_sum_harmonic), proved by
induction on the rounds: the chosen set's weight ρ |A|, with A its newly covered elements and
ρ the minimal ratio, is at most the decrease ∑ x j w j (H(|S j ∩ R|) - H(|S j ∩ R \ A|)), since
w j / |S j ∩ R| ≥ ρ and (a - b) / a ≤ H a - H b. This is the dual-fitting argument of
[WS11], Theorem 1.12, with the dual feasibility step and weak duality composed into
one inequality against an arbitrary fractional cover, so no linear-programming API is needed. With
d bounding the sizes |S j ∩ U| this gives cost ≤ H d · ∑ x j w j, Chvátal's H(d) bound
against the fractional optimum (sum_greedy_le_harmonic_mul_fractional), hence against every
cover (sum_greedy_le_harmonic_mul, [WS11], Theorem 1.12 with g = d); taking
d = |U| gives the H_n guarantee of [WS11], Theorem 1.11
(sum_greedy_le_harmonic_card_mul). Over ℝ, Mathlib's harmonic_le_one_add_log gives
cost ≤ (1 + log d) · w(C) (sum_greedy_le_one_add_log_mul).
Earlier greedy analyses for unweighted set cover are due to Johnson and Lovász; see [WS11], Chapter 1, Chapter Notes.
Limitations #
The algorithm is a pure Lean function; no machine-level running time is certified here.
Main definitions #
Cslib.Algorithms.Lean.SetCover.costPerUncovered: the ratiow j / |S j ∩ R|.Cslib.Algorithms.Lean.SetCover.bestSet: the candidate chosen in a round.Cslib.Algorithms.Lean.SetCover.greedyLoop,Cslib.Algorithms.Lean.SetCover.greedy: the greedy algorithm with explicit fuel, and withU.cardrounds.
Main results #
bestSet_eq_none_iff,bestSet_spec: the choice rule.greedy_covers,greedy_subset,nodup_greedy,length_greedy_le: the output is a cover by distinct candidates whenever the candidates coverU, with at mostU.cardsets.sum_greedy_le_sum_harmonic: Chvátal's charging bound against every fractional cover.sum_greedy_le_harmonic_mul_fractional,sum_greedy_le_harmonic_mul,sum_greedy_le_harmonic_card_mul: theH dandH |U|approximation guarantees.sum_greedy_le_one_add_log_mul: the1 + log dguarantee overℝ.
References #
The algorithm #
The cost per newly covered element of candidate j when the elements of R are uncovered:
w j / |S j ∩ R|.
The candidate chosen by the greedy algorithm when the elements of R are uncovered: among the
candidates of J whose sets meet R, the first one minimizing costPerUncovered, or none if no
candidate meets R.
At most k rounds of the greedy algorithm on the uncovered elements R: choose
bestSet S w J R, remove its elements from R, and continue; stop when no candidate meets R.
Returns the chosen candidates in order.
The greedy algorithm for weighted set cover ([WS11], Algorithm 1.2): the
candidates of J chosen to cover U, with U.card rounds, each covering at least one new
element.
No candidate is chosen exactly when no candidate meets the uncovered elements.
The chosen candidate meets the uncovered elements and minimizes the cost per newly covered element among the candidates that do.
With at least R.card rounds, the greedy loop covers R whenever the candidates do.
The greedy loop chooses only candidates.
Every set chosen by the greedy loop meets the elements uncovered at the start.
The greedy loop never chooses a candidate twice.
The greedy loop runs at most its fuel many rounds.
Feasibility. The greedy algorithm covers U whenever the candidates do.
The greedy algorithm chooses only candidates.
The greedy algorithm never chooses a candidate twice.
The greedy algorithm chooses at most U.card sets.
Chvátal's bound #
The charging bound for the greedy loop on uncovered elements R, against every fractional
cover x of R by candidates.
Chvátal's bound ([Chv79]; cf. the proof of [WS11], Theorem 1.12):
against every fractional cover x of U by candidates, the greedy cover costs at most
∑ j ∈ C, x j * w j * H(|S j ∩ U|).
The H d guarantee against fractional covers ([Chv79]; [WS11],
Theorem 1.12): if every set of the fractional cover meets U in at most d elements, the greedy
cover costs at most H d times the cost ∑ j ∈ C, x j * w j of the fractional cover.
The H d guarantee ([Chv79]; [WS11], Theorem 1.12): if the
candidates C cover U and each of their sets meets U in at most d elements, the greedy cover
costs at most H d times the cost of C. Taking C optimal, the greedy algorithm is an
H d-approximation.
The H n guarantee ([WS11], Theorem 1.11): if the candidates C cover
U, the greedy cover costs at most H |U| times the cost of C.
The logarithmic guarantee: over ℝ, if the candidates C cover U and each of their
sets meets U in at most d elements, the greedy cover costs at most (1 + log d) times the cost
of C, by harmonic_le_one_add_log.