Documentation

CslibFrontier.Algorithms.Lean.SetCover.Greedy

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 #

Main results #

References #

The algorithm #

def Cslib.Algorithms.Lean.SetCover.costPerUncovered {ι : Type u_1} {α : Type u_2} {𝕜 : Type u_3} [DecidableEq α] [Field 𝕜] (S : ι → Finset α) (w : ι → 𝕜) (R : Finset α) (j : ι) :
𝕜

The cost per newly covered element of candidate j when the elements of R are uncovered: w j / |S j ∩ R|.

def Cslib.Algorithms.Lean.SetCover.bestSet {ι : Type u_1} {α : Type u_2} {𝕜 : Type u_3} [DecidableEq α] [Field 𝕜] [LinearOrder 𝕜] (S : ι → Finset α) (w : ι → 𝕜) (J : List ι) (R : Finset α) :

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.

def Cslib.Algorithms.Lean.SetCover.greedyLoop {ι : Type u_1} {α : Type u_2} {𝕜 : Type u_3} [DecidableEq α] [Field 𝕜] [LinearOrder 𝕜] (S : ι → Finset α) (w : ι → 𝕜) (J : List ι) :
ℕ → Finset α → List ι

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.

def Cslib.Algorithms.Lean.SetCover.greedy {ι : Type u_1} {α : Type u_2} {𝕜 : Type u_3} [DecidableEq α] [Field 𝕜] [LinearOrder 𝕜] (S : ι → Finset α) (w : ι → 𝕜) (J : List ι) (U : Finset α) :
List ι

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.

theorem Cslib.Algorithms.Lean.SetCover.bestSet_eq_none_iff {ι : Type u_1} {α : Type u_2} {𝕜 : Type u_3} [DecidableEq α] [Field 𝕜] [LinearOrder 𝕜] {S : ι → Finset α} {w : ι → 𝕜} {J : List ι} {R : Finset α} :
bestSet S w J R = none ↔ ∀ j ∈ J, S j ∩ R = ∅

No candidate is chosen exactly when no candidate meets the uncovered elements.

theorem Cslib.Algorithms.Lean.SetCover.bestSet_spec {ι : Type u_1} {α : Type u_2} {𝕜 : Type u_3} [DecidableEq α] [Field 𝕜] [LinearOrder 𝕜] {S : ι → Finset α} {w : ι → 𝕜} {J : List ι} {R : Finset α} {i : ι} (h : bestSet S w J R = some i) :
i ∈ J ∧ (S i ∩ R).Nonempty ∧ ∀ j ∈ J, (S j ∩ R).Nonempty → costPerUncovered S w R i ≤ costPerUncovered S w R j

The chosen candidate meets the uncovered elements and minimizes the cost per newly covered element among the candidates that do.

theorem Cslib.Algorithms.Lean.SetCover.greedyLoop_covers {ι : Type u_1} {α : Type u_2} {𝕜 : Type u_3} [DecidableEq α] [Field 𝕜] [LinearOrder 𝕜] {S : ι → Finset α} {w : ι → 𝕜} {J : List ι} {k : ℕ} {R : Finset α} (hR : R.card ≤ k) (hcov : ∀ a ∈ R, ∃ j ∈ J, a ∈ S j) (a : α) :
a ∈ R → ∃ i ∈ greedyLoop S w J k R, a ∈ S i

With at least R.card rounds, the greedy loop covers R whenever the candidates do.

theorem Cslib.Algorithms.Lean.SetCover.greedyLoop_subset {ι : Type u_1} {α : Type u_2} {𝕜 : Type u_3} [DecidableEq α] [Field 𝕜] [LinearOrder 𝕜] {S : ι → Finset α} {w : ι → 𝕜} {J : List ι} {k : ℕ} {R : Finset α} (i : ι) :
i ∈ greedyLoop S w J k R → i ∈ J

The greedy loop chooses only candidates.

theorem Cslib.Algorithms.Lean.SetCover.greedyLoop_inter_nonempty {ι : Type u_1} {α : Type u_2} {𝕜 : Type u_3} [DecidableEq α] [Field 𝕜] [LinearOrder 𝕜] {S : ι → Finset α} {w : ι → 𝕜} {J : List ι} {k : ℕ} {R : Finset α} (i : ι) :
i ∈ greedyLoop S w J k R → (S i ∩ R).Nonempty

Every set chosen by the greedy loop meets the elements uncovered at the start.

theorem Cslib.Algorithms.Lean.SetCover.nodup_greedyLoop {ι : Type u_1} {α : Type u_2} {𝕜 : Type u_3} [DecidableEq α] [Field 𝕜] [LinearOrder 𝕜] {S : ι → Finset α} {w : ι → 𝕜} {J : List ι} {k : ℕ} {R : Finset α} :
(greedyLoop S w J k R).Nodup

The greedy loop never chooses a candidate twice.

theorem Cslib.Algorithms.Lean.SetCover.length_greedyLoop_le {ι : Type u_1} {α : Type u_2} {𝕜 : Type u_3} [DecidableEq α] [Field 𝕜] [LinearOrder 𝕜] {S : ι → Finset α} {w : ι → 𝕜} {J : List ι} {k : ℕ} {R : Finset α} :
(greedyLoop S w J k R).length ≤ k

The greedy loop runs at most its fuel many rounds.

theorem Cslib.Algorithms.Lean.SetCover.greedy_covers {ι : Type u_1} {α : Type u_2} {𝕜 : Type u_3} [DecidableEq α] [Field 𝕜] [LinearOrder 𝕜] {S : ι → Finset α} {w : ι → 𝕜} {J : List ι} {U : Finset α} (hcov : ∀ a ∈ U, ∃ j ∈ J, a ∈ S j) (a : α) :
a ∈ U → ∃ i ∈ greedy S w J U, a ∈ S i

Feasibility. The greedy algorithm covers U whenever the candidates do.

theorem Cslib.Algorithms.Lean.SetCover.greedy_subset {ι : Type u_1} {α : Type u_2} {𝕜 : Type u_3} [DecidableEq α] [Field 𝕜] [LinearOrder 𝕜] {S : ι → Finset α} {w : ι → 𝕜} {J : List ι} {U : Finset α} :
greedy S w J U ⊆ J

The greedy algorithm chooses only candidates.

theorem Cslib.Algorithms.Lean.SetCover.nodup_greedy {ι : Type u_1} {α : Type u_2} {𝕜 : Type u_3} [DecidableEq α] [Field 𝕜] [LinearOrder 𝕜] {S : ι → Finset α} {w : ι → 𝕜} {J : List ι} {U : Finset α} :
(greedy S w J U).Nodup

The greedy algorithm never chooses a candidate twice.

theorem Cslib.Algorithms.Lean.SetCover.length_greedy_le {ι : Type u_1} {α : Type u_2} {𝕜 : Type u_3} [DecidableEq α] [Field 𝕜] [LinearOrder 𝕜] {S : ι → Finset α} {w : ι → 𝕜} {J : List ι} {U : Finset α} :
(greedy S w J U).length ≤ U.card

The greedy algorithm chooses at most U.card sets.

Chvátal's bound #

theorem Cslib.Algorithms.Lean.SetCover.sum_greedyLoop_le_sum_harmonic {ι : Type u_1} {α : Type u_2} {𝕜 : Type u_3} [DecidableEq α] [Field 𝕜] [LinearOrder 𝕜] [IsStrictOrderedRing 𝕜] {S : ι → Finset α} {w : ι → 𝕜} {J : List ι} {k : ℕ} {R : Finset α} {C : Finset ι} {x : ι → 𝕜} (hw : ∀ j ∈ J, 0 ≤ w j) (hCJ : ∀ j ∈ C, j ∈ J) (hx : ∀ j ∈ C, 0 ≤ x j) (hcov : ∀ a ∈ R, 1 ≤ ∑ j ∈ C with a ∈ S j, x j) :
(List.map w (greedyLoop S w J k R)).sum ≤ ∑ j ∈ C, x j * w j * ↑(harmonic (S j ∩ R).card)

The charging bound for the greedy loop on uncovered elements R, against every fractional cover x of R by candidates.

theorem Cslib.Algorithms.Lean.SetCover.sum_greedy_le_sum_harmonic {ι : Type u_1} {α : Type u_2} {𝕜 : Type u_3} [DecidableEq α] [Field 𝕜] [LinearOrder 𝕜] [IsStrictOrderedRing 𝕜] {S : ι → Finset α} {w : ι → 𝕜} {J : List ι} {U : Finset α} {C : Finset ι} {x : ι → 𝕜} (hw : ∀ j ∈ J, 0 ≤ w j) (hCJ : ∀ j ∈ C, j ∈ J) (hx : ∀ j ∈ C, 0 ≤ x j) (hcov : ∀ a ∈ U, 1 ≤ ∑ j ∈ C with a ∈ S j, x j) :
(List.map w (greedy S w J U)).sum ≤ ∑ j ∈ C, x j * w j * ↑(harmonic (S j ∩ U).card)

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|).

theorem Cslib.Algorithms.Lean.SetCover.sum_greedy_le_harmonic_mul_fractional {ι : Type u_1} {α : Type u_2} {𝕜 : Type u_3} [DecidableEq α] [Field 𝕜] [LinearOrder 𝕜] [IsStrictOrderedRing 𝕜] {S : ι → Finset α} {w : ι → 𝕜} {J : List ι} {U : Finset α} {C : Finset ι} {x : ι → 𝕜} {d : ℕ} (hw : ∀ j ∈ J, 0 ≤ w j) (hCJ : ∀ j ∈ C, j ∈ J) (hx : ∀ j ∈ C, 0 ≤ x j) (hcov : ∀ a ∈ U, 1 ≤ ∑ j ∈ C with a ∈ S j, x j) (hd : ∀ j ∈ C, (S j ∩ U).card ≤ d) :
(List.map w (greedy S w J U)).sum ≤ ↑(harmonic d) * ∑ j ∈ C, x j * w j

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.

theorem Cslib.Algorithms.Lean.SetCover.sum_greedy_le_harmonic_mul {ι : Type u_1} {α : Type u_2} {𝕜 : Type u_3} [DecidableEq α] [Field 𝕜] [LinearOrder 𝕜] [IsStrictOrderedRing 𝕜] {S : ι → Finset α} {w : ι → 𝕜} {J : List ι} {U : Finset α} {C : Finset ι} {d : ℕ} (hw : ∀ j ∈ J, 0 ≤ w j) (hCJ : ∀ j ∈ C, j ∈ J) (hcov : ∀ a ∈ U, ∃ j ∈ C, a ∈ S j) (hd : ∀ j ∈ C, (S j ∩ U).card ≤ d) :
(List.map w (greedy S w J U)).sum ≤ ↑(harmonic d) * ∑ j ∈ C, w j

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.

theorem Cslib.Algorithms.Lean.SetCover.sum_greedy_le_harmonic_card_mul {ι : Type u_1} {α : Type u_2} {𝕜 : Type u_3} [DecidableEq α] [Field 𝕜] [LinearOrder 𝕜] [IsStrictOrderedRing 𝕜] {S : ι → Finset α} {w : ι → 𝕜} {J : List ι} {U : Finset α} {C : Finset ι} (hw : ∀ j ∈ J, 0 ≤ w j) (hCJ : ∀ j ∈ C, j ∈ J) (hcov : ∀ a ∈ U, ∃ j ∈ C, a ∈ S j) :
(List.map w (greedy S w J U)).sum ≤ ↑(harmonic U.card) * ∑ j ∈ C, w j

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.

theorem Cslib.Algorithms.Lean.SetCover.sum_greedy_le_one_add_log_mul {ι : Type u_1} {α : Type u_2} [DecidableEq α] {S : ι → Finset α} {w : ι → ℝ} {J : List ι} {U : Finset α} {C : Finset ι} {d : ℕ} (hw : ∀ j ∈ J, 0 ≤ w j) (hCJ : ∀ j ∈ C, j ∈ J) (hcov : ∀ a ∈ U, ∃ j ∈ C, a ∈ S j) (hd : ∀ j ∈ C, (S j ∩ U).card ≤ d) :
(List.map w (greedy S w J U)).sum ≤ (1 + Real.log ↑d) * ∑ j ∈ C, w j

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.