import Mathlib /-! # Core Definitions and Key Nonempty-Opposite Lemma for Vietoris–Rips Loop Restoration on C_n This file defines clockwise distance on `ZMod n`, the loop-restoring conditions `NoLargeGap`, `IsWrappingTriangle`, `NoWrappingTriangle`, `ValidS`, and `NonemptyOpposite`, proves the equivalence of the gap characterizations, and proves the **Key Nonempty-Opposite Lemma**: for any valid survivor set `S` with `2 * k < n` and any `u ∈ S`, `S` intersects the open opposite arc `(u + k, u - k)`. -/ namespace RipsCircleErasures /-- Clockwise distance from `x` to `y` on the cycle `C_n = ZMod n`, taking values in `{0, 1, ..., n - 1}`. -/ def cwDist {n : ℕ} (x y : ZMod n) : ℕ := (y - x).val /-- Every vertex has a survivor within clockwise steps `1, ..., k`. Equivalently, the erased set `D = univ \ S` contains no `k` consecutive vertices, and every consecutive clockwise gap of `S` is at most `k`. -/ def NoLargeGap (n k : ℕ) (S : Finset (ZMod n)) : Prop := ∀ x : ZMod n, ∃ d : ℕ, 1 ≤ d ∧ d ≤ k ∧ x + (d : ZMod n) ∈ S /-- A triple `(a, b, c)` in `ZMod n` is a *wrapping triangle* at threshold `k` if all three clockwise gaps `a → b`, `b → c`, `c → a` lie in `[1, k]`. When `2 * k < n`, these three gaps automatically sum to `n`. -/ def IsWrappingTriangle (n k : ℕ) (a b c : ZMod n) : Prop := 1 ≤ cwDist a b ∧ cwDist a b ≤ k ∧ 1 ≤ cwDist b c ∧ cwDist b c ≤ k ∧ 1 ≤ cwDist c a ∧ cwDist c a ≤ k /-- `S` contains no wrapping triangle at threshold `k`. -/ def NoWrappingTriangle (n k : ℕ) (S : Finset (ZMod n)) : Prop := ∀ a ∈ S, ∀ b ∈ S, ∀ c ∈ S, ¬ IsWrappingTriangle n k a b c /-- A survivor subset `S ⊆ ZMod n` is *loop-restoring* (`ValidS n k S`) iff `|S| ≥ 3`, every consecutive clockwise gap is `≤ k`, and `S` contains no wrapping triangle. -/ def ValidS (n k : ℕ) (S : Finset (ZMod n)) : Prop := 3 ≤ S.card ∧ NoLargeGap n k S ∧ NoWrappingTriangle n k S /-- Every `u ∈ S` has an opposite vertex `v ∈ S` strictly inside the open opposite arc `(u + k, u - k)`, i.e., `k < cwDist u v < n - k`. -/ def NonemptyOpposite (n k : ℕ) (S : Finset (ZMod n)) : Prop := ∀ u ∈ S, ∃ v ∈ S, k < cwDist u v ∧ cwDist u v < n - k section BasicCwDist variable {n : ℕ} lemma cwDist_lt [NeZero n] (x y : ZMod n) : cwDist x y < n := ZMod.val_lt (y - x) lemma cwDist_self [NeZero n] (x : ZMod n) : cwDist x x = 0 := by simp [cwDist] lemma cwDist_eq_zero [NeZero n] (x y : ZMod n) : cwDist x y = 0 ↔ x = y := by simp only [cwDist, ZMod.val_eq_zero, sub_eq_zero] exact eq_comm lemma cwDist_pos_iff [NeZero n] (x y : ZMod n) : 1 ≤ cwDist x y ↔ x ≠ y := by rw [← not_iff_not, not_le, Nat.lt_one_iff, not_not] exact cwDist_eq_zero x y lemma cwDist_add_right [NeZero n] (x : ZMod n) {d : ℕ} (hd : d < n) : cwDist x (x + (d : ZMod n)) = d := by simp only [cwDist, add_sub_cancel_left] exact ZMod.val_natCast_of_lt hd lemma cwDist_add_shift (x y c : ZMod n) : cwDist (x + c) (y + c) = cwDist x y := by simp [cwDist] lemma add_cwDist [NeZero n] (x y : ZMod n) : x + (cwDist x y : ZMod n) = y := by simp [cwDist] lemma cwDist_val_of_le [NeZero n] {x y : ZMod n} (h : x.val ≤ y.val) : cwDist x y = y.val - x.val := by have hx : x.val < n := ZMod.val_lt x have hy : y.val < n := ZMod.val_lt y have hsub : y - x = ((y.val - x.val : ℕ) : ZMod n) := by rw [Nat.cast_sub h, ZMod.natCast_zmod_val, ZMod.natCast_zmod_val] rw [cwDist, hsub, ZMod.val_natCast_of_lt (by omega)] lemma cwDist_val_of_gt [NeZero n] {x y : ZMod n} (h : y.val < x.val) : cwDist x y = n + y.val - x.val := by have hx : x.val < n := ZMod.val_lt x have hy : y.val < n := ZMod.val_lt y have hsub : y - x = ((n + y.val - x.val : ℕ) : ZMod n) := by rw [Nat.cast_sub (by omega), Nat.cast_add, ZMod.natCast_self, zero_add, ZMod.natCast_zmod_val, ZMod.natCast_zmod_val] rw [cwDist, hsub, ZMod.val_natCast_of_lt (by omega)] lemma cwDist_symm_add [NeZero n] {x y : ZMod n} (hxy : x ≠ y) : cwDist x y + cwDist y x = n := by have h1 : cwDist x y < n := cwDist_lt x y have h2 : cwDist y x < n := cwDist_lt y x have hpos1 : 1 ≤ cwDist x y := (cwDist_pos_iff x y).mpr hxy have hpos2 : 1 ≤ cwDist y x := (cwDist_pos_iff y x).mpr (Ne.symm hxy) have hsum : ((cwDist x y + cwDist y x : ℕ) : ZMod n) = 0 := by rw [Nat.cast_add] simp only [cwDist, ZMod.natCast_zmod_val] ring have hdvd : n ∣ (cwDist x y + cwDist y x) := (ZMod.natCast_eq_zero_iff _ _).mp hsum obtain ⟨q, hq⟩ := hdvd have hq1 : q = 1 := by by_contra hne rcases Nat.lt_or_gt_of_ne hne with hlt | hgt · have : q = 0 := by omega subst this; omega · have : 2 ≤ q := by omega nlinarith subst hq1 simpa using hq lemma cwDist_add_eq_of_lt [NeZero n] {x y z : ZMod n} (h : cwDist x y + cwDist y z < n) : cwDist x z = cwDist x y + cwDist y z := by have hsub : z - x = ((cwDist x y + cwDist y z : ℕ) : ZMod n) := by rw [Nat.cast_add] simp only [cwDist, ZMod.natCast_zmod_val] ring rw [cwDist, hsub, ZMod.val_natCast_of_lt h] lemma cwDist_sub_eq_of_le [NeZero n] {x y z : ZMod n} (h : cwDist x y ≤ cwDist x z) : cwDist y z = cwDist x z - cwDist x y := by have hlt : cwDist x z < n := cwDist_lt x z have hsub : z - y = ((cwDist x z - cwDist x y : ℕ) : ZMod n) := by rw [Nat.cast_sub h] simp only [cwDist, ZMod.natCast_zmod_val] ring rw [cwDist, hsub, ZMod.val_natCast_of_lt (by omega)] lemma isWrappingTriangle_cyclic {k : ℕ} {a b c : ZMod n} (h : IsWrappingTriangle n k a b c) : IsWrappingTriangle n k b c a := by rcases h with ⟨h1, h2, h3, h4, h5, h6⟩ exact ⟨h3, h4, h5, h6, h1, h2⟩ lemma wrapping_triangle_sum {k : ℕ} (hn : 2 * k < n) {a b c : ZMod n} (h : IsWrappingTriangle n k a b c) : cwDist a b + cwDist b c + cwDist c a = n := by have : NeZero n := ⟨by omega⟩ rcases h with ⟨h1, h2, h3, h4, h5, h6⟩ have hsum : ((cwDist a b + cwDist b c + cwDist c a : ℕ) : ZMod n) = 0 := by rw [Nat.cast_add, Nat.cast_add] simp only [cwDist, ZMod.natCast_zmod_val] ring obtain ⟨q, hq⟩ := (ZMod.natCast_eq_zero_iff _ _).mp hsum have hq1 : q = 1 := by by_contra hne rcases Nat.lt_or_gt_of_ne hne with hlt | hgt · have : q = 0 := by omega subst this; omega · have : 2 ≤ q := by omega nlinarith subst hq1 simpa using hq end BasicCwDist section GapEquivalences /-- `NoLargeGap n k S` is equivalent to `D = Finset.univ \ S` containing no `k` consecutive vertices. -/ theorem noLargeGap_iff_no_consec_erased (n k : ℕ) (S : Finset (ZMod n)) [NeZero n] : NoLargeGap n k S ↔ ¬ ∃ x : ZMod n, ∀ i : ℕ, i < k → x + (i : ZMod n) ∈ (Finset.univ \ S) := by constructor · intro h ⟨x, hx⟩ obtain ⟨d, hd1, hdk, hmem⟩ := h (x - 1) have hi : d - 1 < k := by omega have hD := hx (d - 1) hi rw [Finset.mem_sdiff] at hD have heq : x + ((d - 1 : ℕ) : ZMod n) = (x - 1) + (d : ZMod n) := by rw [Nat.cast_sub hd1, Nat.cast_one] ring rw [heq] at hD exact hD.2 hmem · intro h x by_contra hno push Not at hno apply h refine ⟨x + 1, fun i hi => ?_⟩ rw [Finset.mem_sdiff] refine ⟨Finset.mem_univ _, ?_⟩ have heq : (x + 1) + (i : ZMod n) = x + ((i + 1 : ℕ) : ZMod n) := by push_cast; ring rw [heq] exact hno (i + 1) (by omega) (by omega) /-- For `1 ≤ k < n`, `NoLargeGap n k S` is equivalent to `S` being nonempty and every `u ∈ S` having a clockwise neighbor in `S` at distance in `[1, k]`. -/ theorem noLargeGap_iff_step_le_k {n k : ℕ} (hk : 1 ≤ k) (hkn : k < n) (S : Finset (ZMod n)) : NoLargeGap n k S ↔ S.Nonempty ∧ ∀ u ∈ S, ∃ v ∈ S, 1 ≤ cwDist u v ∧ cwDist u v ≤ k := by have : NeZero n := ⟨by omega⟩ constructor · intro h refine ⟨?_, ?_⟩ · obtain ⟨d, _, _, hmem⟩ := h 0 exact ⟨0 + (d : ZMod n), hmem⟩ · intro u _ obtain ⟨d, hd1, hdk, hmem⟩ := h u refine ⟨u + (d : ZMod n), hmem, ?_, ?_⟩ · rw [cwDist_add_right u (by omega)]; exact hd1 · rw [cwDist_add_right u (by omega)]; exact hdk · rintro ⟨hne, hstep⟩ x obtain ⟨u, huS, humin⟩ := Finset.exists_min_image S (fun u => cwDist u x) hne obtain ⟨v, hvS, hv1, hvk⟩ := hstep u huS by_cases hux : u = x · subst hux exact ⟨cwDist u v, hv1, hvk, by rwa [add_cwDist]⟩ · have hux_pos : 1 ≤ cwDist u x := (cwDist_pos_iff u x).mpr hux have hlt : cwDist u x < cwDist u v := by by_contra hge push Not at hge have hvx : cwDist v x = cwDist u x - cwDist u v := cwDist_sub_eq_of_le hge have hle := humin v hvS omega have hxv : cwDist x v = cwDist u v - cwDist u x := cwDist_sub_eq_of_le (le_of_lt hlt) refine ⟨cwDist x v, by omega, by omega, ?_⟩ rw [add_cwDist] exact hvS /-- Any set with `NoLargeGap n k S` when `1 ≤ k` and `2 * k < n` automatically has at least 3 elements. -/ theorem card_ge_three_of_noLargeGap {n k : ℕ} (hk : 1 ≤ k) (hn : 2 * k < n) {S : Finset (ZMod n)} (hgap : NoLargeGap n k S) : 3 ≤ S.card := by have : NeZero n := ⟨by omega⟩ have hstep := (noLargeGap_iff_step_le_k hk (by omega) S).mp hgap obtain ⟨⟨a, ha⟩, hnext⟩ := hstep obtain ⟨b, hb, hab1, habk⟩ := hnext a ha obtain ⟨c, hc, hbc1, hbck⟩ := hnext b hb have hab : a ≠ b := (cwDist_pos_iff a b).mp hab1 have hbc : b ≠ c := (cwDist_pos_iff b c).mp hbc1 have hac : a ≠ c := by intro h subst h have := cwDist_symm_add hab omega have hsub : ({a, b, c} : Finset (ZMod n)) ⊆ S := by intro x hx simp only [Finset.mem_insert, Finset.mem_singleton] at hx rcases hx with rfl | rfl | rfl <;> assumption have hcard : ({a, b, c} : Finset (ZMod n)).card = 3 := by rw [Finset.card_insert_of_notMem, Finset.card_insert_of_notMem, Finset.card_singleton] · simp [hbc] · simp [hab, hac] calc 3 = ({a, b, c} : Finset (ZMod n)).card := hcard.symm _ ≤ S.card := Finset.card_le_card hsub end GapEquivalences section KeyNonemptyOpposite /-- **Key Nonempty-Opposite Lemma**: For `1 ≤ k`, `2 * k < n`, and any `S` satisfying `NoLargeGap n k S` and `NoWrappingTriangle n k S`, every `u ∈ S` has an opposite vertex `z ∈ S` strictly inside the open opposite arc `(u + k, u - k)` (i.e., `k < cwDist u z < n - k`). -/ theorem nonempty_opposite_of_gap_and_noWrap {n k : ℕ} (hk : 1 ≤ k) (hn : 2 * k < n) {S : Finset (ZMod n)} (hgap : NoLargeGap n k S) (hnowarp : NoWrappingTriangle n k S) : NonemptyOpposite n k S := by have : NeZero n := ⟨by omega⟩ intro u hu let A := S.filter (fun v => 1 ≤ cwDist u v ∧ cwDist u v ≤ k) have hA_ne : A.Nonempty := by obtain ⟨d, hd1, hdk, hmem⟩ := hgap u refine ⟨u + (d : ZMod n), Finset.mem_filter.mpr ⟨hmem, ?_, ?_⟩⟩ · rw [cwDist_add_right u (by omega)]; exact hd1 · rw [cwDist_add_right u (by omega)]; exact hdk obtain ⟨v, hvA, hvmax⟩ := Finset.exists_max_image A (fun v => cwDist u v) hA_ne obtain ⟨hvS, huv1, huvk⟩ := Finset.mem_filter.mp hvA obtain ⟨d, hd1, hdk, hzS⟩ := hgap v set z : ZMod n := v + (d : ZMod n) have hvz : cwDist v z = d := cwDist_add_right v (by omega) have huz : cwDist u z = cwDist u v + d := by have hlt : cwDist u v + cwDist v z < n := by omega rw [cwDist_add_eq_of_lt hlt, hvz] have huz_gt_k : k < cwDist u z := by by_contra hle push Not at hle have hzA : z ∈ A := Finset.mem_filter.mpr ⟨hzS, by omega, hle⟩ have := hvmax z hzA omega have huz_lt : cwDist u z < n - k := by by_contra hge push Not at hge have huz_ne : u ≠ z := (cwDist_pos_iff u z).mp (by omega) have hsym := cwDist_symm_add huz_ne have hzu1 : 1 ≤ cwDist z u := (cwDist_pos_iff z u).mpr (Ne.symm huz_ne) have hzuk : cwDist z u ≤ k := by omega exact hnowarp u hu v hvS z hzS ⟨huv1, huvk, by omega, by omega, hzu1, hzuk⟩ exact ⟨z, hzS, huz_gt_k, huz_lt⟩ /-- **Key Nonempty-Opposite Lemma** stated directly for `ValidS n k S`. -/ theorem nonempty_opposite_of_valid {n k : ℕ} (hk : 1 ≤ k) (hn : 2 * k < n) {S : Finset (ZMod n)} (hS : ValidS n k S) : NonemptyOpposite n k S := nonempty_opposite_of_gap_and_noWrap hk hn hS.2.1 hS.2.2 end KeyNonemptyOpposite #print axioms noLargeGap_iff_no_consec_erased #print axioms noLargeGap_iff_step_le_k #print axioms card_ge_three_of_noLargeGap #print axioms nonempty_opposite_of_valid end RipsCircleErasures