Lean / Reader
Lean reader

Erdos824Greedy.lean

95 lines6 declarationsno sorryimports Mathlib.Algebra.BigOperators.Group.Finset.Basic Mathlib.Data.List.Count Mathlib.Tactic
  1. import Mathlib.Algebra.BigOperators.Group.Finset.Basic
  2. import Mathlib.Data.List.Count
  3. import Mathlib.Tactic
  4. /-! Counting assignments subject to a per-label capacity. -/
  5. namespace Erdos824Greedy
  6. open Finset
  7. open scoped BigOperators
  8. private theorem sum_counts_le {α : Type*} [DecidableEq α] (S : Finset α) (xs : List α) :
  9. ∑ x ∈ S, xs.count x ≤ xs.length := by
  10. induction xs with
  11. | nil => simp
  12. | cons a xs ih =>
  13. simp only [List.count_cons, List.length_cons, Finset.sum_add_distrib]
  14. by_cases ha : a ∈ S <;> simp [beq_iff_eq, ha] at * <;> omega
  15. /-- At least r choices remain whenever the alphabet has at least r+n/b
  16. members and n positions have been assigned. -/
  17. theorem room {α : Type*} [DecidableEq α] (S : Finset α) (xs : List α)
  18. (b r : ℕ) (hb : 0 < b) (hS : r + xs.length / b ≤ S.card) :
  19. r ≤ (S.filter fun x => xs.count x < b).card := by
  20. let full := S.filter fun x => b ≤ xs.count x
  21. have hh : full.card * b ≤ xs.length := calc
  22. _ = ∑ _x ∈ full, b := by simp
  23. _ ≤ ∑ x ∈ full, xs.count x := Finset.sum_le_sum fun x hx => (Finset.mem_filter.mp hx).2
  24. _ ≤ xs.length := sum_counts_le full xs
  25. have hfull : full.card ≤ xs.length / b := (Nat.le_div_iff_mul_le hb).mpr hh
  26. have heq : (S.filter fun x => xs.count x < b).card + full.card = S.card := by
  27. simpa [full, not_lt] using S.card_filter_add_card_filter_not (fun x => xs.count x < b)
  28. omega
  29. /-- The assignments in reverse order, with coordinate i drawn from S i. -/
  30. def assignments {α : Type*} [DecidableEq α] (S : ℕ → Finset α) (b : ℕ) : ℕ → Finset (List α)
  31. | 0 => {[]}
  32. | i+1 => (assignments S b i).biUnion fun xs =>
  33. ((S i).filter fun x => xs.count x < b).image (fun x => x::xs)
  34. theorem assignment_length {α : Type*} [DecidableEq α] {S : ℕ → Finset α} {b n : ℕ}
  35. {xs} (h : xs ∈ assignments S b n) : xs.length = n := by
  36. induction n generalizing xs with
  37. | zero =>
  38. have : xs = [] := by simpa [assignments] using h
  39. simp [this]
  40. | succ n ih =>
  41. obtain ⟨ys,hy,hx⟩ := Finset.mem_biUnion.mp h
  42. obtain ⟨a,_,rfl⟩ := Finset.mem_image.mp hx
  43. simp [ih hy]
  44. theorem assignment_counts {α : Type*} [DecidableEq α] {S : ℕ → Finset α} {b n : ℕ}
  45. {xs} (h : xs ∈ assignments S b n) : ∀ x, xs.count x ≤ b := by
  46. induction n generalizing xs with
  47. | zero =>
  48. have : xs = [] := by simpa [assignments] using h
  49. simp [this]
  50. | succ n ih =>
  51. obtain ⟨ys,hy,hx⟩ := Finset.mem_biUnion.mp h
  52. obtain ⟨a,ha,rfl⟩ := Finset.mem_image.mp hx
  53. have hc := (Finset.mem_filter.mp ha).2
  54. intro x
  55. by_cases hxa : x = a
  56. · subst x
  57. simp
  58. omega
  59. · simpa [List.count_cons, hxa, Ne.symm hxa] using ih hy x
  60. theorem many_assignments {α : Type*} [DecidableEq α] (S : ℕ → Finset α) (b r n : ℕ)
  61. (hb : 0 < b) (hS : ∀ i<n, r + i/b ≤ (S i).card) :
  62. r^n ≤ (assignments S b n).card := by
  63. induction n with
  64. | zero => simp [assignments]
  65. | succ n ih =>
  66. let F (xs : List α) := ((S n).filter fun x => xs.count x < b).image fun x => x::xs
  67. have hdis : ((assignments S b n : Finset (List α)) : Set (List α)).PairwiseDisjoint F := by
  68. intro xs _ ys _ hne
  69. refine Finset.disjoint_left.mpr ?_
  70. intro ls hl hr
  71. obtain ⟨_,_,rfl⟩ := Finset.mem_image.mp hl
  72. obtain ⟨_,_,he⟩ := Finset.mem_image.mp hr
  73. exact hne (List.cons.inj he).2.symm
  74. have hc (xs : List α) (hx : xs ∈ assignments S b n) : r ≤ (F xs).card := by
  75. rw [show (F xs).card = _ from Finset.card_image_of_injective _ (fun _ _ h => (List.cons.inj h).1)]
  76. apply room _ _ _ _ hb
  77. rw [assignment_length hx]
  78. exact hS n (by omega)
  79. rw [pow_succ, assignments, Finset.card_biUnion hdis]
  80. calc
  81. _ ≤ (assignments S b n).card * r :=
  82. Nat.mul_le_mul_right r (ih fun i hi => hS i (by omega))
  83. _ = ∑ _xs ∈ assignments S b n, r := by simp
  84. _ ≤ _ := Finset.sum_le_sum hc
  85. #print axioms many_assignments
  86. end Erdos824Greedy