Lean / Reader
Lean reader

DecayEdgeRefinement.lean

72 lines3 declarationsno sorryimports Mathlib.Tactic
  1. import Mathlib.Tactic
  2. namespace DecayEdgeRefinement
  3. /-- Slots assigned to a provisional color. The second coordinate numbers the
  4. allowed edges of an actual color starting at zero. -/
  5. def slots (pool : Finset ℕ) (cap : ℕ → ℕ) : Finset (Σ _ : ℕ, ℕ) :=
  6. pool.sigma fun i => Finset.range (cap i)
  7. theorem assign_one {α : Type*} [DecidableEq α] (s : Finset α) (pool : Finset ℕ)
  8. (cap : ℕ → ℕ) (enough : s.card ≤ ∑ i ∈ pool, cap i) :
  9. ∃ f : α → ℕ × ℕ, Set.InjOn f (s : Set α) ∧
  10. ∀ x ∈ s, (f x).1 ∈ pool ∧ (f x).2 < cap (f x).1 := by
  11. classical
  12. have count : Fintype.card (↥s) ≤ Fintype.card (↥(slots pool cap)) := by
  13. simpa [slots, Finset.card_sigma] using enough
  14. let emb := (Function.Embedding.nonempty_of_card_le count).some
  15. let f (x : α) : ℕ × ℕ :=
  16. if h : x ∈ s then ((emb ⟨x,h⟩).val.1, (emb ⟨x,h⟩).val.2) else (0,0)
  17. refine ⟨f, ?_, ?_⟩
  18. · intro x hx y hy hxy
  19. change x ∈ s at hx
  20. change y ∈ s at hy
  21. have eqv : (emb ⟨x,hx⟩).val = (emb ⟨y,hy⟩).val := by
  22. have hp := congrArg (fun v : ℕ × ℕ => (⟨v.1,v.2⟩ : Σ _ : ℕ, ℕ)) hxy
  23. simpa [f, hx, hy] using hp
  24. exact congrArg Subtype.val (emb.injective (Subtype.ext eqv))
  25. · intro x hx
  26. have hm := Finset.mem_sigma.mp (emb ⟨x,hx⟩).property
  27. simpa [f, hx] using hm
  28. /-- Split a coloring of any finite edge set across disjoint palettes. This
  29. produces actual color labels and proves every finite integer cap. -/
  30. theorem recolor {α : Type*} [DecidableEq α] (E : Finset α) (c : α → ℕ)
  31. (A : ℕ → Finset ℕ) (cap : ℕ → ℕ)
  32. (dis : ∀ j h, j ≠ h → Disjoint (A j) (A h))
  33. (enough : ∀ j, (E.filter (fun e => c e = j)).card ≤ ∑ i ∈ A j, cap i) :
  34. ∃ c' : α → ℕ, (∀ e ∈ E, c' e ∈ A (c e)) ∧
  35. (∀ i, (E.filter (fun e => c' e = i)).card ≤ cap i) ∧
  36. ∀ x ∈ E, ∀ y ∈ E, c' x = c' y → c x = c y := by
  37. classical
  38. choose F hinj hmem using
  39. fun j => assign_one (E.filter fun e => c e = j) (A j) cap (enough j)
  40. let pos (e : α) := F (c e) e
  41. have hm : ∀ e ∈ E, (pos e).1 ∈ A (c e) ∧ (pos e).2 < cap (pos e).1 :=
  42. fun e he => hmem (c e) e (Finset.mem_filter.mpr ⟨he,rfl⟩)
  43. have parent : ∀ x ∈ E, ∀ y ∈ E, (pos x).1 = (pos y).1 → c x = c y := by
  44. intro x hx y hy hxy
  45. by_contra ne
  46. exact Finset.disjoint_left.mp (dis _ _ ne) (hm x hx).1 (hxy ▸ (hm y hy).1)
  47. refine ⟨fun e => (pos e).1, fun e he => (hm e he).1, ?_, parent⟩
  48. intro i
  49. calc
  50. (E.filter (fun e => (pos e).1 = i)).card ≤ (Finset.range (cap i)).card := by
  51. apply Finset.card_le_card_of_injOn (fun e => (pos e).2)
  52. · intro e he
  53. obtain ⟨he, hi⟩ := Finset.mem_filter.mp he
  54. exact Finset.mem_range.mpr (hi ▸ (hm e he).2)
  55. · intro x hx y hy hxy
  56. obtain ⟨hx, hi⟩ := Finset.mem_filter.mp hx
  57. obtain ⟨hy, hj⟩ := Finset.mem_filter.mp hy
  58. have heq := hi.trans hj.symm
  59. have hc := parent x hx y hy heq
  60. have both := Prod.ext heq hxy
  61. change F (c x) x = F (c y) y at both
  62. rw [← hc] at both
  63. exact hinj (c x) (Finset.mem_filter.mpr ⟨hx,rfl⟩)
  64. (Finset.mem_filter.mpr ⟨hy,hc.symm⟩) both
  65. _ = cap i := Finset.card_range _
  66. end DecayEdgeRefinement