import Mathlib /-! # Linear Lower Bounds, Exact Thresholds, and Structural Upper Bounds for # Approximate Roommates Stability (Fanelli–Moscardelli Problem) We formalize a suite of results on the universal rank-improvement stability threshold `Roommates.stabilityThreshold N` (denoted $K(N)$) from A. Fanelli and L. Moscardelli, *Approximately Stable Matching* (ECAI 2025, https://doi.org/10.3233/FAIA251234), formalized as an open problem in `FormalConjectures/Paper/ApproximatelyStableMatching.lean`. ## Main Results 1. **Universal Upper Bound (`stabilityThreshold_le_half`)**: For every $N \ge 1$, $K(N) \le \lfloor (N + 1) / 2 \rfloor = \lceil N / 2 \rceil$ (formalizing Fanelli–Moscardelli Theorem 1 via maximal matchings in $G_{N-k}$). 2. **Dummy-Agent Monotonicity (`stabilityThreshold_mono`, `stabilityThreshold_monotone`)**: For all $1 \le m \le n$, $K(m) \le K(n)$. 3. **Exact Small Values (`stabilityThreshold_three`..`stabilityThreshold_six`)**: $K(1) = K(2) = 1$, $K(3) = K(4) = 2$, and $K(5) = K(6) = 3$ (via an explicit 5-agent profile with no $2$-stable matching). 4. **Master 1-Step Shift-Invariant Blocking Theorem (`not_isKStable_of_shiftInvariant`)** and its **Exact Sharpness Ceiling (`one_step_shift_invariant_ceiling`)**: Any shift-invariant bijection $r$ on $\mathbb{Z}_N$ ($N$ odd) satisfying the 1-step blocking conditions witnesses $K(N) \ge k + 1$, and necessarily satisfies $5k \le 2N - 1$. 5. **Explicit Linear Lower Bound (`stabilityThreshold_ge_of_odd_two_fifths`, `stabilityThreshold_linear_lower_bound`, `stabilityThreshold_sandwich`)**: A unified constructive family hitting the exact 1-step ceiling $5k \le 2N - 1$ for every $k \ge 2$ and odd $N \ge 2k + \lfloor k / 2 \rfloor + 1$, yielding for EVERY $N \ge 1$: $$\left\lfloor \frac{2N + 2}{5} \right\rfloor \le K(N) \le \left\lfloor \frac{N + 1}{2} \right\rfloor$$ (and for all odd $N \ge 3$, $K(N) \ge \lfloor (2N + 4) / 5 \rfloor$). This improves the Fanelli–Moscardelli ECAI 2025 lower bound $K(N) = \Omega(\sqrt{N})$ to an explicit linear lower bound with slope $2/5$. 6. **Structural Sub-$\lceil N / 2 \rceil$ Upper Bound (`exists_m_stable_of_no_odd_critical_cycle`)**: For odd $N = 2m + 1$ ($m \ge 1$), if a maximal matching $\mu_0$ of $G_m$ leaves the critical digraph $u \Rightarrow v \iff (\mathrm{rk}(u, v) = 1 \land \mathrm{rk}(v, u) = m + 1)$ on unmatched agents free of odd directed cycles, then the profile admits an $m$-stable matching (strictly beating $\lceil N / 2 \rceil = m + 1$). -/ namespace Roommates /-! ## Section 1: Verbatim Roommates Definitions -/ /-- A possibly partial matching, with fixed points representing single agents. -/ abbrev Matching (α : Type*) := {μ : Equiv.Perm α // Function.Involutive μ} /-- Matchings form a finite type when the agents do. -/ instance {α : Type*} [Fintype α] [DecidableEq α] : Fintype (Matching α) := by letI : DecidablePred (fun μ : Equiv.Perm α ↦ Function.Involutive μ) := fun μ ↦ by unfold Function.Involutive infer_instance exact Subtype.fintype _ /-- The matching in which every agent is single. -/ def emptyMatching (α : Type*) : Matching α := ⟨Equiv.refl α, fun _ ↦ rfl⟩ /-- Every matched pair must be an edge of the acceptability graph. -/ def IsAcceptable {α : Type*} (G : SimpleGraph α) (μ : Matching α) : Prop := ∀ i, μ.val i = i ∨ G.Adj i (μ.val i) /-- An acceptable pair blocks when both endpoints strictly prefer it to their assignments. -/ def IsBlockingPair {α : Type*} (G : SimpleGraph α) (rank : α → α → ℕ) (μ : Matching α) (i j : α) : Prop := G.Adj i j ∧ μ.val i ≠ j ∧ rank i j < rank i (μ.val i) ∧ rank j i < rank j (μ.val j) /-- A stable matching is acceptable and has no blocking pair. -/ def IsStable {α : Type*} (G : SimpleGraph α) (rank : α → α → ℕ) (μ : Matching α) : Prop := IsAcceptable G μ ∧ ∀ i j, ¬ IsBlockingPair G rank μ i j /-- An acceptable pair $k$-blocks if both agents gain at least $k$ rank positions. -/ def IsKBlockingPair {α : Type*} (G : SimpleGraph α) (rank : α → α → ℕ) (k : ℕ) (μ : Matching α) (i j : α) : Prop := G.Adj i j ∧ μ.val i ≠ j ∧ rank i j + k ≤ rank i (μ.val i) ∧ rank j i + k ≤ rank j (μ.val j) /-- A $k$-stable matching is acceptable and has no $k$-blocking pair. -/ def IsKStable {α : Type*} (G : SimpleGraph α) (rank : α → α → ℕ) (k : ℕ) (μ : Matching α) : Prop := IsAcceptable G μ ∧ ∀ i j, ¬ IsKBlockingPair G rank k μ i j /-- The matching with all agents single is acceptable. -/ theorem isAcceptable_emptyMatching {α : Type*} (G : SimpleGraph α) : IsAcceptable G (emptyMatching α) := fun _ ↦ Or.inl rfl /-- Every matching is acceptable on the complete graph `⊤`. -/ theorem isAcceptable_top {α : Type*} (μ : Matching α) : IsAcceptable (⊤ : SimpleGraph α) μ := by intro i by_cases h : μ.val i = i · exact Or.inl h · exact Or.inr ((SimpleGraph.top_adj _ _).2 (Ne.symm h)) /-- Improvement by one position is ordinary strict improvement. -/ @[simp] theorem isKBlockingPair_one {α : Type*} (G : SimpleGraph α) (rank : α → α → ℕ) (μ : Matching α) (i j : α) : IsKBlockingPair G rank 1 μ i j ↔ IsBlockingPair G rank μ i j := by simp [IsKBlockingPair, IsBlockingPair] /-- One-stability is ordinary stability. -/ @[simp] theorem isKStable_one {α : Type*} (G : SimpleGraph α) (rank : α → α → ℕ) (μ : Matching α) : IsKStable G rank 1 μ ↔ IsStable G rank μ := by simp [IsKStable, IsStable] /-- Complete strict preferences, with the agent's own name ranked last. -/ structure CompleteProfile (n : ℕ) where /-- Permutations map candidate labels to zero-based ranks. -/ rank : Fin n → Equiv.Perm (Fin n) /-- Being single is worse than every other assignment. -/ self_last : ∀ i, (rank i i).val + 1 = n /-- One-based ranks, assigning rank $n$ to being single. -/ def CompleteProfile.partnerRank {n : ℕ} (p : CompleteProfile n) (i j : Fin n) : ℕ := (p.rank i j).val + 1 /-- Being single has rank equal to the number of agents. -/ @[simp] theorem CompleteProfile.partnerRank_self {n : ℕ} (p : CompleteProfile n) (i : Fin n) : p.partnerRank i i = n := p.self_last i /-- All assigned ranks are at most the number of agents. -/ theorem CompleteProfile.partnerRank_le {n : ℕ} (p : CompleteProfile n) (i j : Fin n) : p.partnerRank i j ≤ n := (p.rank i j).isLt /-- All assigned ranks are positive. -/ theorem CompleteProfile.partnerRank_pos {n : ℕ} (p : CompleteProfile n) (i j : Fin n) : 0 < p.partnerRank i j := Nat.zero_lt_succ _ /-- Distinct candidates receive distinct ranks from any agent. -/ theorem CompleteProfile.partnerRank_injective {n : ℕ} (p : CompleteProfile n) (i : Fin n) : Function.Injective (p.partnerRank i) := by intro j₁ j₂ h apply (p.rank i).injective ext unfold CompleteProfile.partnerRank at h omega /-- Every complete preference profile admits a positive stability threshold. -/ theorem exists_stabilityThreshold (n : ℕ) : ∃ k : ℕ, 0 < k ∧ ∀ p : CompleteProfile n, ∃ μ : Matching (Fin n), IsKStable ⊤ p.partnerRank k μ := by refine ⟨n + 1, Nat.zero_lt_succ _, fun p ↦ ⟨emptyMatching _, ?_⟩⟩ refine ⟨isAcceptable_emptyMatching _, fun i j h ↦ ?_⟩ have hbound := p.partnerRank_le i ((emptyMatching (Fin n)).val i) have hpositive : 0 < p.partnerRank i j := Nat.zero_lt_succ _ rcases h with ⟨_, _, hi, _⟩ omega open scoped Classical in /-- The least positive $k$ for which every complete profile has a $k$-stable matching. -/ noncomputable def stabilityThreshold (n : ℕ) : ℕ := Nat.find (exists_stabilityThreshold n) /-- The threshold is positive and guarantees existence for every complete profile. -/ theorem stabilityThreshold_spec (n : ℕ) : 0 < stabilityThreshold n ∧ ∀ p : CompleteProfile n, ∃ μ : Matching (Fin n), IsKStable ⊤ p.partnerRank (stabilityThreshold n) μ := by classical exact Nat.find_spec (exists_stabilityThreshold n) /-- Any positive universal existence bound is at least the threshold. -/ theorem stabilityThreshold_le {n k : ℕ} (hk : 0 < k) (h : ∀ p : CompleteProfile n, ∃ μ : Matching (Fin n), IsKStable ⊤ p.partnerRank k μ) : stabilityThreshold n ≤ k := by classical exact Nat.find_min' (exists_stabilityThreshold n) ⟨hk, h⟩ /-- Monotonicity of $k$-stability in $k$: if $k_1 \le k_2$, then $k_1$-stability implies $k_2$-stability. -/ theorem IsKStable.mono {α : Type*} {G : SimpleGraph α} {rank : α → α → ℕ} {k₁ k₂ : ℕ} {μ : Matching α} (hk : k₁ ≤ k₂) (h : IsKStable G rank k₁ μ) : IsKStable G rank k₂ μ := by refine ⟨h.1, fun i j hb ↦ h.2 i j ?_⟩ rcases hb with ⟨hadj, hne, hi, hj⟩ exact ⟨hadj, hne, by omega, by omega⟩ /-- A profile with no $k$-stable matching yields the lower bound $k + 1 \le K(n)$. -/ theorem stabilityThreshold_gt_of_no_isKStable {n k : ℕ} (p : CompleteProfile n) (h : ∀ μ : Matching (Fin n), ¬ IsKStable ⊤ p.partnerRank k μ) : k + 1 ≤ stabilityThreshold n := by by_contra hlt have hle : stabilityThreshold n ≤ k := by omega obtain ⟨μ, hμ⟩ := (stabilityThreshold_spec n).2 p exact h μ (hμ.mono hle) /-- A single agent has no possible blocking pair. -/ theorem stabilityThreshold_one : stabilityThreshold 1 = 1 := by apply Nat.le_antisymm · apply stabilityThreshold_le (by decide) intro p refine ⟨emptyMatching _, isAcceptable_emptyMatching _, ?_⟩ intro i j h have hij : i = j := Subsingleton.elim _ _ subst j exact h.2.1 rfl · exact (stabilityThreshold_spec 1).1 /-- Two agents can always be matched to each other. -/ theorem stabilityThreshold_two : stabilityThreshold 2 = 1 := by apply Nat.le_antisymm · apply stabilityThreshold_le (by decide) intro p let μ : Matching (Fin 2) := ⟨Equiv.swap 0 1, Equiv.swap_apply_self 0 1⟩ refine ⟨μ, isAcceptable_top μ, ?_⟩ intro i j h have hswap : ∀ i j : Fin 2, i ≠ j → Equiv.swap 0 1 i = j := by decide exact h.2.1 (hswap i j ((SimpleGraph.top_adj _ _).1 h.1)) · exact (stabilityThreshold_spec 2).1 /-- Three agents whose first choices form a directed cycle. -/ noncomputable def CompleteProfile.cyclicThree : CompleteProfile 3 where rank := ![Equiv.ofBijective ![2, 0, 1] (by decide), Equiv.ofBijective ![1, 2, 0] (by decide), Equiv.ofBijective ![0, 1, 2] (by decide)] self_last := by decide /-- The cyclic three-agent profile has no stable matching, even when singles are allowed. -/ theorem CompleteProfile.not_isStable_cyclicThree : ∀ μ : Matching (Fin 3), ¬ IsStable ⊤ CompleteProfile.cyclicThree.partnerRank μ := by unfold IsStable IsAcceptable IsBlockingPair decide /-! ## Section 2: Universal Upper Bound (Fanelli–Moscardelli Theorem 1) -/ /-- Augment a matching `μ` by pairing two distinct fixed points `i` and `j`. -/ def Matching.addPair {α : Type*} [DecidableEq α] (μ : Matching α) {i j : α} (hi : μ.val i = i) (hj : μ.val j = j) : Matching α := ⟨μ.val.trans (Equiv.swap i j), by intro x simp only [Equiv.trans_apply] by_cases hxi : μ.val x = i · have hx : x = i := by have := μ.property x; rw [hxi, hi] at this; exact this.symm subst hx simp [hi, hj] · by_cases hxj : μ.val x = j · have hx : x = j := by have := μ.property x; rw [hxj, hj] at this; exact this.symm subst hx simp [hi, hj] · rw [Equiv.swap_apply_of_ne_of_ne hxi hxj, μ.property x] have hxi' : x ≠ i := fun h ↦ hxi (by rw [h, hi]) have hxj' : x ≠ j := fun h ↦ hxj (by rw [h, hj]) exact Equiv.swap_apply_of_ne_of_ne hxi' hxj'⟩ theorem Matching.addPair_apply_left {α : Type*} [DecidableEq α] (μ : Matching α) {i j : α} (hi : μ.val i = i) (hj : μ.val j = j) : (μ.addPair hi hj).val i = j := by simp [Matching.addPair, hi] theorem Matching.addPair_apply_right {α : Type*} [DecidableEq α] (μ : Matching α) {i j : α} (hi : μ.val i = i) (hj : μ.val j = j) : (μ.addPair hi hj).val j = i := by simp [Matching.addPair, hj] theorem Matching.addPair_apply_other {α : Type*} [DecidableEq α] (μ : Matching α) {i j x : α} (hi : μ.val i = i) (hj : μ.val j = j) (hxi : x ≠ i) (hxj : x ≠ j) : (μ.addPair hi hj).val x = μ.val x := by simp only [Matching.addPair, Equiv.trans_apply] have h1 : μ.val x ≠ i := fun h ↦ hxi (by have := μ.property x; rw [h, hi] at this; exact this.symm) have h2 : μ.val x ≠ j := fun h ↦ hxj (by have := μ.property x; rw [h, hj] at this; exact this.symm) exact Equiv.swap_apply_of_ne_of_ne h1 h2 /-- The set of matched agents in a matching. -/ def Matching.matchedSet {α : Type*} [Fintype α] [DecidableEq α] (μ : Matching α) : Finset α := Finset.univ.filter fun x ↦ μ.val x ≠ x theorem Matching.card_matchedSet_lt_addPair {α : Type*} [Fintype α] [DecidableEq α] (μ : Matching α) {i j : α} (hij : i ≠ j) (hi : μ.val i = i) (hj : μ.val j = j) : μ.matchedSet.card < (μ.addPair hi hj).matchedSet.card := by apply Finset.card_lt_card rw [Finset.ssubset_iff_subset_ne] constructor · intro x hx simp only [Matching.matchedSet, Finset.mem_filter, Finset.mem_univ, true_and] at hx ⊢ have hxi : x ≠ i := fun h ↦ hx (by rw [h, hi]) have hxj : x ≠ j := fun h ↦ hx (by rw [h, hj]) rwa [μ.addPair_apply_other hi hj hxi hxj] · intro heq have hi_in : i ∈ (μ.addPair hi hj).matchedSet := by simp [Matching.matchedSet, μ.addPair_apply_left hi hj, hij.symm] rw [← heq] at hi_in simp [Matching.matchedSet, hi] at hi_in /-- A matching is `t`-bounded if every matched agent has rank at most `t` for their partner. -/ def IsRankBounded {n : ℕ} (p : CompleteProfile n) (t : ℕ) (μ : Matching (Fin n)) : Prop := ∀ i, μ.val i ≠ i → p.partnerRank i (μ.val i) ≤ t /-- **Theorem 1 (Universal Upper Bound, Fanelli–Moscardelli)**: For every $N \ge 1$, every complete profile on $N$ agents admits an $\lceil N / 2 \rceil$-stable matching, so `stabilityThreshold N ≤ (N + 1) / 2`. -/ theorem stabilityThreshold_le_half (n : ℕ) (hn : 1 ≤ n) : stabilityThreshold n ≤ (n + 1) / 2 := by classical set k := (n + 1) / 2 have hk_pos : 0 < k := by omega apply stabilityThreshold_le hk_pos intro p let S : Finset (Matching (Fin n)) := Finset.univ.filter (fun μ ↦ IsRankBounded p (n - k) μ) have hempty : emptyMatching (Fin n) ∈ S := Finset.mem_filter.2 ⟨Finset.mem_univ _, fun _ hi ↦ absurd rfl hi⟩ obtain ⟨μ, hμS, hμ_max⟩ := Finset.exists_max_image S (fun ν ↦ ν.matchedSet.card) ⟨_, hempty⟩ have hμ_bnd : IsRankBounded p (n - k) μ := (Finset.mem_filter.1 hμS).2 refine ⟨μ, isAcceptable_top μ, ?_⟩ intro i j ⟨hadj, _, hi_imp, hj_imp⟩ have hij : i ≠ j := (SimpleGraph.top_adj _ _).1 hadj have hi_single : μ.val i = i := by by_contra hne have h1 := hμ_bnd i hne have h2 := p.partnerRank_pos i j omega have hj_single : μ.val j = j := by by_contra hne have h1 := hμ_bnd j hne have h2 := p.partnerRank_pos j i omega rw [hi_single, p.partnerRank_self] at hi_imp rw [hj_single, p.partnerRank_self] at hj_imp let μ' := μ.addPair hi_single hj_single have hμ'_bnd : IsRankBounded p (n - k) μ' := by intro x hx by_cases hxi : x = i · subst hxi rw [μ.addPair_apply_left hi_single hj_single] omega · by_cases hxj : x = j · subst hxj rw [μ.addPair_apply_right hi_single hj_single] omega · rw [μ.addPair_apply_other hi_single hj_single hxi hxj] at hx ⊢ exact hμ_bnd x hx have hμ'_mem : μ' ∈ S := Finset.mem_filter.2 ⟨Finset.mem_univ _, hμ'_bnd⟩ have hlt := μ.card_matchedSet_lt_addPair hij hi_single hj_single have hle : (μ.addPair hi_single hj_single).matchedSet.card ≤ μ.matchedSet.card := hμ_max μ' hμ'_mem omega /-! ## Section 3: Dummy-Agent Monotonicity Theorem -/ /-- Lift a permutation of `Fin n` to a permutation of `Fin (n + 1)` fixing `Fin.last n`. -/ def liftPermLast {n : ℕ} (e : Equiv.Perm (Fin n)) : Equiv.Perm (Fin (n + 1)) where toFun x := if h : x.val < n then (e ⟨x.val, h⟩).castSucc else Fin.last n invFun x := if h : x.val < n then (e.symm ⟨x.val, h⟩).castSucc else Fin.last n left_inv x := by by_cases h : x.val < n · simp [h, Fin.is_lt] · have hx : x = Fin.last n := by ext; simp [Fin.last]; omega subst hx simp [Fin.last] right_inv x := by by_cases h : x.val < n · simp [h, Fin.is_lt] · have hx : x = Fin.last n := by ext; simp [Fin.last]; omega subst hx simp [Fin.last] theorem liftPermLast_castSucc {n : ℕ} (e : Equiv.Perm (Fin n)) (x : Fin n) : liftPermLast e x.castSucc = (e x).castSucc := by simp [liftPermLast, x.isLt] theorem liftPermLast_last {n : ℕ} (e : Equiv.Perm (Fin n)) : liftPermLast e (Fin.last n) = Fin.last n := by simp [liftPermLast, Fin.last] /-- Extend a complete profile on `n` agents to `n + 1` agents by adding a dummy agent `Fin.last n` ranked just above being single (at 1-based rank `n`) by every original agent. -/ def CompleteProfile.addDummy {n : ℕ} (hn : 1 ≤ n) (p : CompleteProfile n) : CompleteProfile (n + 1) where rank i := if h : i.val < n then (liftPermLast (p.rank ⟨i.val, h⟩)).trans (Equiv.swap ⟨n - 1, by omega⟩ (Fin.last n)) else Equiv.refl (Fin (n + 1)) self_last i := by by_cases h : i.val < n · simp only [h, ↓reduceDIte, Equiv.trans_apply] have hlift : liftPermLast (p.rank ⟨i.val, h⟩) i = (p.rank ⟨i.val, h⟩ ⟨i.val, h⟩).castSucc := by simp [liftPermLast, h] rw [hlift] have hlast : (p.rank ⟨i.val, h⟩ ⟨i.val, h⟩).castSucc = ⟨n - 1, by omega⟩ := by ext have := p.self_last ⟨i.val, h⟩ simp only [Fin.val_castSucc] omega rw [hlast, Equiv.swap_apply_left] simp [Fin.last] · simp only [h, ↓reduceDIte, Equiv.refl_apply] omega theorem CompleteProfile.addDummy_partnerRank_castSucc {n : ℕ} (hn : 1 ≤ n) (p : CompleteProfile n) (i j : Fin n) (hij : i ≠ j) : (p.addDummy hn).partnerRank i.castSucc j.castSucc = p.partnerRank i j := by unfold CompleteProfile.partnerRank CompleteProfile.addDummy simp only [Fin.val_castSucc, i.isLt, ↓reduceDIte, Equiv.trans_apply] have hi_eq : (⟨i.val, i.isLt⟩ : Fin n) = i := by ext; rfl rw [hi_eq, liftPermLast_castSucc] have hne1 : (p.rank i j).castSucc ≠ ⟨n - 1, by omega⟩ := by intro heq have hval : (p.rank i j).val = n - 1 := congrArg Fin.val heq have hself := p.self_last i have heq' : p.rank i j = p.rank i i := by ext; omega exact hij ((p.rank i).injective heq').symm have hne2 : (p.rank i j).castSucc ≠ Fin.last n := by intro heq have hval : (p.rank i j).val = n := congrArg Fin.val heq have := (p.rank i j).isLt omega rw [Equiv.swap_apply_of_ne_of_ne hne1 hne2] rfl theorem CompleteProfile.addDummy_partnerRank_last {n : ℕ} (hn : 1 ≤ n) (p : CompleteProfile n) (i : Fin n) : (p.addDummy hn).partnerRank i.castSucc (Fin.last n) = n := by unfold CompleteProfile.partnerRank CompleteProfile.addDummy simp only [Fin.val_castSucc, i.isLt, ↓reduceDIte, Equiv.trans_apply, liftPermLast_last, Equiv.swap_apply_right] omega /-- Restrict a matching on `Fin (n + 1)` to `Fin n` by unmatching whoever was paired with the dummy agent `Fin.last n`. -/ def Matching.restrictDummy {n : ℕ} (μ : Matching (Fin (n + 1))) : Matching (Fin n) := let f : Fin n → Fin n := fun i ↦ if h : (μ.val i.castSucc).val < n then ⟨(μ.val i.castSucc).val, h⟩ else i have hf_inv : Function.Involutive f := by intro i dsimp only [f] by_cases h : (μ.val i.castSucc).val < n · simp only [h, ↓reduceDIte] have hcast : (⟨(μ.val i.castSucc).val, h⟩ : Fin n).castSucc = μ.val i.castSucc := by ext; rfl simp [hcast, μ.property i.castSucc, i.isLt] · simp [h] ⟨hf_inv.toPerm, hf_inv⟩ /-- **Theorem 2 (Dummy-Agent Monotonicity)**: For every $n \ge 1$, `stabilityThreshold n ≤ stabilityThreshold (n + 1)`. -/ theorem stabilityThreshold_mono (n : ℕ) (hn : 1 ≤ n) : stabilityThreshold n ≤ stabilityThreshold (n + 1) := by obtain ⟨hk_pos, hspec⟩ := stabilityThreshold_spec (n + 1) apply stabilityThreshold_le hk_pos intro p obtain ⟨μ', hμ'⟩ := hspec (p.addDummy hn) let μ := μ'.restrictDummy refine ⟨μ, isAcceptable_top μ, ?_⟩ intro i j ⟨hadj, hne_ij, hi_imp, hj_imp⟩ have hij : i ≠ j := (SimpleGraph.top_adj _ _).1 hadj have hrank_le : ∀ x : Fin n, p.partnerRank x (μ.val x) ≤ (p.addDummy hn).partnerRank x.castSucc (μ'.val x.castSucc) := by intro x change p.partnerRank x (if h : (μ'.val x.castSucc).val < n then ⟨(μ'.val x.castSucc).val, h⟩ else x) ≤ _ by_cases h : (μ'.val x.castSucc).val < n · simp only [h, ↓reduceDIte] let y : Fin n := ⟨(μ'.val x.castSucc).val, h⟩ have hy : y.castSucc = μ'.val x.castSucc := by ext; rfl change p.partnerRank x y ≤ (p.addDummy hn).partnerRank x.castSucc (μ'.val x.castSucc) rw [← hy] by_cases hxy : x = y · rw [hxy, p.partnerRank_self, (p.addDummy hn).partnerRank_self] omega · rw [p.addDummy_partnerRank_castSucc hn x y hxy] · simp only [h, ↓reduceDIte] have hlast : μ'.val x.castSucc = Fin.last n := by ext; simp [Fin.last]; have := (μ'.val x.castSucc).isLt; omega rw [hlast, p.partnerRank_self, p.addDummy_partnerRank_last hn x] have hne' : μ'.val i.castSucc ≠ j.castSucc := by intro heq apply hne_ij change (if h : (μ'.val i.castSucc).val < n then ⟨(μ'.val i.castSucc).val, h⟩ else i) = j rw [heq] simp [j.isLt] apply hμ'.2 i.castSucc j.castSucc refine ⟨(SimpleGraph.top_adj _ _).2 (by simpa [Fin.castSucc_inj] using hij), hne', ?_, ?_⟩ · rw [p.addDummy_partnerRank_castSucc hn i j hij] exact le_trans hi_imp (hrank_le i) · rw [p.addDummy_partnerRank_castSucc hn j i hij.symm] exact le_trans hj_imp (hrank_le j) /-- Monotonicity across any two market sizes $1 \le m \le n$. -/ theorem stabilityThreshold_monotone {m n : ℕ} (hm : 1 ≤ m) (hmn : m ≤ n) : stabilityThreshold m ≤ stabilityThreshold n := by induction hmn with | refl => exact le_rfl | @step k hmk ih => exact le_trans ih (stabilityThreshold_mono k (le_trans hm hmk)) /-! ## Section 4: Exact Stability Thresholds for $N = 3, 4, 5, 6$ -/ /-- Exact value $K(3) = 2$. -/ theorem stabilityThreshold_three : stabilityThreshold 3 = 2 := by apply Nat.le_antisymm · exact stabilityThreshold_le_half 3 (by omega) · apply stabilityThreshold_gt_of_no_isKStable CompleteProfile.cyclicThree intro μ hμ exact CompleteProfile.not_isStable_cyclicThree μ ((isKStable_one _ _ _).1 hμ) /-- Exact value $K(4) = 2$. -/ theorem stabilityThreshold_four : stabilityThreshold 4 = 2 := by apply Nat.le_antisymm · exact stabilityThreshold_le_half 4 (by omega) · rw [← stabilityThreshold_three] exact stabilityThreshold_mono 3 (by omega) /-- An explicit 5-agent complete profile with NO $2$-stable matching. One-based rank matrix: - Agent 0: `[5, 4, 2, 3, 1]` - Agent 1: `[4, 5, 1, 3, 2]` - Agent 2: `[4, 3, 5, 1, 2]` - Agent 3: `[2, 1, 3, 5, 4]` - Agent 4: `[1, 4, 3, 2, 5]` -/ noncomputable def profileFive : CompleteProfile 5 where rank := ![ Equiv.ofBijective ![4, 3, 1, 2, 0] (by decide), Equiv.ofBijective ![3, 4, 0, 2, 1] (by decide), Equiv.ofBijective ![3, 2, 4, 0, 1] (by decide), Equiv.ofBijective ![1, 0, 2, 4, 3] (by decide), Equiv.ofBijective ![0, 3, 2, 1, 4] (by decide) ] self_last := by decide /-- Helper lemma to evaluate `profileFive.partnerRank` on concrete `Fin 5` indices. -/ private lemma profileFive_rank_eval (i j : Fin 5) : profileFive.partnerRank i j = (![![5, 4, 2, 3, 1], ![4, 5, 1, 3, 2], ![4, 3, 5, 1, 2], ![2, 1, 3, 5, 4], ![1, 4, 3, 2, 5]] : Fin 5 → Fin 5 → ℕ) i j := by fin_cases i <;> fin_cases j <;> rfl /-- `profileFive` admits NO $2$-stable matching (pure case-split kernel proof). -/ theorem not_isKStable_two_profileFive : ∀ μ : Matching (Fin 5), ¬ IsKStable ⊤ profileFive.partnerRank 2 μ := by intro μ hstab have hblk : ∀ i j : Fin 5, i ≠ j → μ.val i ≠ j → profileFive.partnerRank i j + 2 ≤ profileFive.partnerRank i (μ.val i) → profileFive.partnerRank j i + 2 ≤ profileFive.partnerRank j (μ.val j) → False := by intro i j hij hne hi hj exact hstab.2 i j ⟨(SimpleGraph.top_adj _ _).2 hij, hne, hi, hj⟩ have hinv := μ.property have hinj := μ.val.injective -- Split on the partner of agent 0 generalize h0 : μ.val 0 = a0 fin_cases a0 · -- Case μ 0 = 0 generalize h4 : μ.val 4 = a4 fin_cases a4 · exact absurd (hinj (h4.trans h0.symm)) (by decide) · exact hblk 0 4 (by decide) (by rw [h0]; decide) (by rw [h0, profileFive_rank_eval, profileFive_rank_eval]; decide) (by rw [h4, profileFive_rank_eval, profileFive_rank_eval]; decide) · exact hblk 0 4 (by decide) (by rw [h0]; decide) (by rw [h0, profileFive_rank_eval, profileFive_rank_eval]; decide) (by rw [h4, profileFive_rank_eval, profileFive_rank_eval]; decide) · have h3 : μ.val 3 = 4 := by simpa [h4] using hinv 4 exact hblk 0 3 (by decide) (by rw [h0]; decide) (by rw [h0, profileFive_rank_eval, profileFive_rank_eval]; decide) (by rw [h3, profileFive_rank_eval, profileFive_rank_eval]; decide) · exact hblk 0 4 (by decide) (by rw [h0]; decide) (by rw [h0, profileFive_rank_eval, profileFive_rank_eval]; decide) (by rw [h4, profileFive_rank_eval, profileFive_rank_eval]; decide) · -- Case μ 0 = 1 have h1 : μ.val 1 = 0 := by simpa [h0] using hinv 0 generalize h4 : μ.val 4 = a4 fin_cases a4 · exact absurd (hinj (h4.trans h1.symm)) (by decide) · exact absurd (hinj (h4.trans h0.symm)) (by decide) · exact hblk 0 4 (by decide) (by rw [h0]; decide) (by rw [h0, profileFive_rank_eval, profileFive_rank_eval]; decide) (by rw [h4, profileFive_rank_eval, profileFive_rank_eval]; decide) · have h3 : μ.val 3 = 4 := by simpa [h4] using hinv 4 have h2 : μ.val 2 = 2 := by generalize h2' : μ.val 2 = a2 fin_cases a2 · exact absurd (hinj (h2'.trans h1.symm)) (by decide) · exact absurd (hinj (h2'.trans h0.symm)) (by decide) · rfl · exact absurd (hinj (h2'.trans h4.symm)) (by decide) · exact absurd (hinj (h2'.trans h3.symm)) (by decide) exact hblk 1 2 (by decide) (by rw [h1]; decide) (by rw [h1, profileFive_rank_eval, profileFive_rank_eval]; decide) (by rw [h2, profileFive_rank_eval, profileFive_rank_eval]; decide) · exact hblk 0 4 (by decide) (by rw [h0]; decide) (by rw [h0, profileFive_rank_eval, profileFive_rank_eval]; decide) (by rw [h4, profileFive_rank_eval, profileFive_rank_eval]; decide) · -- Case μ 0 = 2 have h2 : μ.val 2 = 0 := by simpa [h0] using hinv 0 generalize h1 : μ.val 1 = a1 fin_cases a1 · exact absurd (hinj (h1.trans h2.symm)) (by decide) · generalize h3 : μ.val 3 = a3 fin_cases a3 · exact absurd (hinj (h3.trans h2.symm)) (by decide) · exact absurd (hinj (h3.trans h1.symm)) (by decide) · exact absurd (hinj (h3.trans h0.symm)) (by decide) · exact hblk 1 3 (by decide) (by rw [h1]; decide) (by rw [h1, profileFive_rank_eval, profileFive_rank_eval]; decide) (by rw [h3, profileFive_rank_eval, profileFive_rank_eval]; decide) · exact hblk 1 3 (by decide) (by rw [h1]; decide) (by rw [h1, profileFive_rank_eval, profileFive_rank_eval]; decide) (by rw [h3, profileFive_rank_eval, profileFive_rank_eval]; decide) · exact absurd (hinj (h1.trans h0.symm)) (by decide) · have h3 : μ.val 3 = 1 := by simpa [h1] using hinv 1 have h4 : μ.val 4 = 4 := by generalize h4' : μ.val 4 = a4 fin_cases a4 · exact absurd (hinj (h4'.trans h2.symm)) (by decide) · exact absurd (hinj (h4'.trans h3.symm)) (by decide) · exact absurd (hinj (h4'.trans h0.symm)) (by decide) · exact absurd (hinj (h4'.trans h1.symm)) (by decide) · rfl exact hblk 2 4 (by decide) (by rw [h2]; decide) (by rw [h2, profileFive_rank_eval, profileFive_rank_eval]; decide) (by rw [h4, profileFive_rank_eval, profileFive_rank_eval]; decide) · have h4 : μ.val 4 = 1 := by simpa [h1] using hinv 1 have h3 : μ.val 3 = 3 := by generalize h3' : μ.val 3 = a3 fin_cases a3 · exact absurd (hinj (h3'.trans h2.symm)) (by decide) · exact absurd (hinj (h3'.trans h4.symm)) (by decide) · exact absurd (hinj (h3'.trans h0.symm)) (by decide) · rfl · exact absurd (hinj (h3'.trans h1.symm)) (by decide) exact hblk 2 3 (by decide) (by rw [h2]; decide) (by rw [h2, profileFive_rank_eval, profileFive_rank_eval]; decide) (by rw [h3, profileFive_rank_eval, profileFive_rank_eval]; decide) · -- Case μ 0 = 3 have h3 : μ.val 3 = 0 := by simpa [h0] using hinv 0 generalize h4 : μ.val 4 = a4 fin_cases a4 · exact absurd (hinj (h4.trans h3.symm)) (by decide) · exact hblk 0 4 (by decide) (by rw [h0]; decide) (by rw [h0, profileFive_rank_eval, profileFive_rank_eval]; decide) (by rw [h4, profileFive_rank_eval, profileFive_rank_eval]; decide) · exact hblk 0 4 (by decide) (by rw [h0]; decide) (by rw [h0, profileFive_rank_eval, profileFive_rank_eval]; decide) (by rw [h4, profileFive_rank_eval, profileFive_rank_eval]; decide) · exact absurd (hinj (h4.trans h0.symm)) (by decide) · exact hblk 0 4 (by decide) (by rw [h0]; decide) (by rw [h0, profileFive_rank_eval, profileFive_rank_eval]; decide) (by rw [h4, profileFive_rank_eval, profileFive_rank_eval]; decide) · -- Case μ 0 = 4 have h4 : μ.val 4 = 0 := by simpa [h0] using hinv 0 generalize h1 : μ.val 1 = a1 fin_cases a1 · exact absurd (hinj (h1.trans h4.symm)) (by decide) · generalize h2 : μ.val 2 = a2 fin_cases a2 · exact absurd (hinj (h2.trans h4.symm)) (by decide) · exact absurd (hinj (h2.trans h1.symm)) (by decide) · exact hblk 1 2 (by decide) (by rw [h1]; decide) (by rw [h1, profileFive_rank_eval, profileFive_rank_eval]; decide) (by rw [h2, profileFive_rank_eval, profileFive_rank_eval]; decide) · have h3 : μ.val 3 = 2 := by simpa [h2] using hinv 2 exact hblk 1 3 (by decide) (by rw [h1]; decide) (by rw [h1, profileFive_rank_eval, profileFive_rank_eval]; decide) (by rw [h3, profileFive_rank_eval, profileFive_rank_eval]; decide) · exact absurd (hinj (h2.trans h0.symm)) (by decide) · have h2 : μ.val 2 = 1 := by simpa [h1] using hinv 1 have h3 : μ.val 3 = 3 := by generalize h3' : μ.val 3 = a3 fin_cases a3 · exact absurd (hinj (h3'.trans h4.symm)) (by decide) · exact absurd (hinj (h3'.trans h2.symm)) (by decide) · exact absurd (hinj (h3'.trans h1.symm)) (by decide) · rfl · exact absurd (hinj (h3'.trans h0.symm)) (by decide) exact hblk 2 3 (by decide) (by rw [h2]; decide) (by rw [h2, profileFive_rank_eval, profileFive_rank_eval]; decide) (by rw [h3, profileFive_rank_eval, profileFive_rank_eval]; decide) · have h3 : μ.val 3 = 1 := by simpa [h1] using hinv 1 have h2 : μ.val 2 = 2 := by generalize h2' : μ.val 2 = a2 fin_cases a2 · exact absurd (hinj (h2'.trans h4.symm)) (by decide) · exact absurd (hinj (h2'.trans h3.symm)) (by decide) · rfl · exact absurd (hinj (h2'.trans h1.symm)) (by decide) · exact absurd (hinj (h2'.trans h0.symm)) (by decide) exact hblk 1 2 (by decide) (by rw [h1]; decide) (by rw [h1, profileFive_rank_eval, profileFive_rank_eval]; decide) (by rw [h2, profileFive_rank_eval, profileFive_rank_eval]; decide) · exact absurd (hinj (h1.trans h0.symm)) (by decide) /-- Exact value $K(5) = 3$. -/ theorem stabilityThreshold_five : stabilityThreshold 5 = 3 := by apply Nat.le_antisymm · exact stabilityThreshold_le_half 5 (by omega) · exact stabilityThreshold_gt_of_no_isKStable profileFive not_isKStable_two_profileFive /-- Exact value $K(6) = 3$. -/ theorem stabilityThreshold_six : stabilityThreshold 6 = 3 := by apply Nat.le_antisymm · exact stabilityThreshold_le_half 6 (by omega) · rw [← stabilityThreshold_five] exact stabilityThreshold_mono 5 (by omega) /-! ## Section 5: Master 1-Step Shift-Invariant Blocking Theorem and Exact Ceiling -/ /-- Any involution on a finite set of odd cardinality has a fixed point. -/ theorem exists_fixed_point_of_odd_involutive {α : Type*} [Fintype α] [DecidableEq α] (hodd : Odd (Fintype.card α)) (μ : Matching α) : ∃ x : α, μ.val x = x := by -- Show the non-fixed points have even cardinality by strong induction on `Finset α`. have heven_aux : ∀ (S : Finset α), (∀ x ∈ S, μ.val x ∈ S) → (∀ x ∈ S, μ.val x ≠ x) → Even S.card := by intro S induction hcard : S.card using Nat.strong_induction_on generalizing S with | h m ih => intro hclosed hnofix rcases S.eq_empty_or_nonempty with rfl | ⟨x, hx⟩ · exact ⟨0, by simp [← hcard]⟩ · have hμx : μ.val x ∈ S := hclosed x hx have hne : μ.val x ≠ x := hnofix x hx let T := (S.erase x).erase (μ.val x) have hμx_in : μ.val x ∈ S.erase x := Finset.mem_erase.2 ⟨hne, hμx⟩ have hT_card : T.card + 2 = S.card := by have h1 : T.card + 1 = (S.erase x).card := Finset.card_erase_add_one hμx_in have h2 : (S.erase x).card + 1 = S.card := Finset.card_erase_add_one hx omega have hT_closed : ∀ y ∈ T, μ.val y ∈ T := by intro y hy simp only [T, Finset.mem_erase] at hy ⊢ rcases hy with ⟨hy_ne_μx, hy_ne_x, hyS⟩ refine ⟨?_, ?_, hclosed y hyS⟩ · intro h exact hy_ne_x (μ.val.injective h) · intro h exact hy_ne_μx (by have := μ.property y; rw [h] at this; exact this.symm) have hT_nofix : ∀ y ∈ T, μ.val y ≠ y := by intro y hy exact hnofix y (Finset.mem_of_mem_erase (Finset.mem_of_mem_erase hy)) have hTeven : Even T.card := ih T.card (by omega) T rfl hT_closed hT_nofix rcases hTeven with ⟨r, hr⟩ refine ⟨r + 1, ?_⟩ omega by_contra hnone push Not at hnone have heven : Even (Finset.univ : Finset α).card := heven_aux Finset.univ (fun _ _ ↦ Finset.mem_univ _) (fun x _ ↦ hnone x) rw [Finset.card_univ] at heven rcases hodd with ⟨a, ha⟩ rcases heven with ⟨b, hb⟩ omega /-- Construct a shift-invariant complete profile on `Fin n` from a rank bijection `r : ZMod n ≃ Fin n` sending `0` to the last rank `n - 1`. -/ def CompleteProfile.ofShift {n : ℕ} [NeZero n] (r : ZMod n ≃ Fin n) (hr0 : (r 0).val + 1 = n) : CompleteProfile n where rank i := (ZMod.finEquiv n).toEquiv.trans ((Equiv.subRight (ZMod.finEquiv n i)).trans r) self_last i := by simp [hr0] theorem CompleteProfile.ofShift_partnerRank {n : ℕ} [NeZero n] (r : ZMod n ≃ Fin n) (hr0 : (r 0).val + 1 = n) (i j : Fin n) : (CompleteProfile.ofShift r hr0).partnerRank i j = (r (ZMod.finEquiv n j - ZMod.finEquiv n i)).val + 1 := rfl /-- **Master 1-Step Shift-Invariant Blocking Theorem**: Let $n \ge 3$ be odd, $1 \le k < n$, and $r : \mathbb{Z}_n \simeq \mathrm{Fin}\, n$ be a bijection with 1-based rank $R(d) = r(d) + 1$ satisfying: - $R(0) = n$, $R(1) = 1$, $R(-1) \le n - k$; - For every $t \in \mathbb{Z}_n \setminus \{0, 1\}$ with $R(t) \le k$: 1. $R(t - 1) \le n - k$, and 2. $R(1 - t) + k \le R(-t)$. Then the shift-invariant profile `ofShift r` has NO $k$-stable matching. -/ theorem not_isKStable_of_shiftInvariant {n k : ℕ} [NeZero n] (hodd : Odd n) (hn3 : 3 ≤ n) (_hk_pos : 1 ≤ k) (hkn : k < n) (r : ZMod n ≃ Fin n) (hr0 : (r 0).val + 1 = n) (hr1 : (r 1).val + 1 = 1) (hrm1 : (r (-1)).val + 1 ≤ n - k) (hstep : ∀ t : ZMod n, t ≠ 0 → t ≠ 1 → (r t).val + 1 ≤ k → (r (t - 1)).val + 1 ≤ n - k ∧ (r (1 - t)).val + 1 + k ≤ (r (-t)).val + 1) : ∀ μ : Matching (Fin n), ¬ IsKStable ⊤ (CompleteProfile.ofShift r hr0).partnerRank k μ := by intro μ hstab have hodd_card : Odd (Fintype.card (Fin n)) := by simpa using hodd obtain ⟨x0, hx0⟩ := exists_fixed_point_of_odd_involutive hodd_card μ let e : Fin n ≃ ZMod n := (ZMod.finEquiv n).toEquiv let z0 : ZMod n := e x0 let x1 : Fin n := e.symm (z0 - 1) have h1_ne_0 : (1 : ZMod n) ≠ 0 := by have : Fact (1 < n) := ⟨by omega⟩ exact one_ne_zero have hx1_ne_x0 : x1 ≠ x0 := by intro h have h' : z0 - 1 = z0 := by have := congrArg e h simpa [x1, z0] using this exact h1_ne_0 (by linear_combination -h') have hμx1_ne_x0 : μ.val x1 ≠ x0 := by intro h exact hx1_ne_x0 (μ.val.injective (h.trans hx0.symm)) -- (x1, x0) does not k-block μ, so x1 must be matched to rank ≤ k have hblk1 := hstab.2 x1 x0 have hrx0_x1 : (CompleteProfile.ofShift r hr0).partnerRank x0 x1 + k ≤ (CompleteProfile.ofShift r hr0).partnerRank x0 (μ.val x0) := by rw [hx0, CompleteProfile.partnerRank_self, CompleteProfile.ofShift_partnerRank] have hdiff : e x1 - e x0 = -1 := by simp [x1, z0] change (r (e x1 - e x0)).val + 1 + k ≤ n rw [hdiff] omega have hrx1_x0 : (CompleteProfile.ofShift r hr0).partnerRank x1 x0 = 1 := by rw [CompleteProfile.ofShift_partnerRank] have hdiff : e x0 - e x1 = 1 := by simp [x1, z0] change (r (e x0 - e x1)).val + 1 = 1 rw [hdiff] exact hr1 have hrx1_partner : (CompleteProfile.ofShift r hr0).partnerRank x1 (μ.val x1) ≤ k := by by_contra hgt apply hblk1 refine ⟨(SimpleGraph.top_adj _ _).2 hx1_ne_x0, hμx1_ne_x0, ?_, hrx0_x1⟩ omega -- Let y = μ x1 = e.symm (z0 - 1 + t), where t = e y - e x1 let y : Fin n := μ.val x1 let t : ZMod n := e y - e x1 have ht_rank : (r t).val + 1 ≤ k := by simpa [CompleteProfile.ofShift_partnerRank, t, y, e] using hrx1_partner have ht_ne_0 : t ≠ 0 := by intro ht0 rw [ht0, hr0] at ht_rank omega have ht_ne_1 : t ≠ 1 := by intro ht1 apply hμx1_ne_x0 apply e.injective have hdiff : e x0 - e x1 = 1 := by simp [x1, z0] have : e y - e x1 = e x0 - e x1 := ht1.trans hdiff.symm exact sub_left_inj.1 this obtain ⟨h3a, h3b⟩ := hstep t ht_ne_0 ht_ne_1 ht_rank -- Now (y, x0) k-blocks μ! have hy_ne_x0 : y ≠ x0 := hμx1_ne_x0 have hμy : μ.val y = x1 := μ.property x1 have hμy_ne_x0 : μ.val y ≠ x0 := by rw [hμy]; exact hx1_ne_x0 have hdiff_x0_y : e y - e x0 = t - 1 := by have : e x1 = e x0 - 1 := by simp [x1, z0] dsimp only [t] rw [this] ring have hdiff_y_x0 : e x0 - e y = 1 - t := by rw [← neg_sub, hdiff_x0_y] ring have hdiff_y_x1 : e x1 - e y = -t := by dsimp only [t] ring apply hstab.2 y x0 refine ⟨(SimpleGraph.top_adj _ _).2 hy_ne_x0, hμy_ne_x0, ?_, ?_⟩ · rw [hμy, CompleteProfile.ofShift_partnerRank, CompleteProfile.ofShift_partnerRank] change (r (e x0 - e y)).val + 1 + k ≤ (r (e x1 - e y)).val + 1 rw [hdiff_y_x0, hdiff_y_x1] exact h3b · rw [hx0, CompleteProfile.partnerRank_self, CompleteProfile.ofShift_partnerRank] change (r (e y - e x0)).val + 1 + k ≤ n rw [hdiff_x0_y] omega /-- Corollary: any valid 1-step shift-invariant bijection gives `k + 1 ≤ stabilityThreshold n`. -/ theorem stabilityThreshold_ge_of_shiftInvariant {n k : ℕ} [NeZero n] (hodd : Odd n) (hn3 : 3 ≤ n) (hk_pos : 1 ≤ k) (hkn : k < n) (r : ZMod n ≃ Fin n) (hr0 : (r 0).val + 1 = n) (hr1 : (r 1).val + 1 = 1) (hrm1 : (r (-1)).val + 1 ≤ n - k) (hstep : ∀ t : ZMod n, t ≠ 0 → t ≠ 1 → (r t).val + 1 ≤ k → (r (t - 1)).val + 1 ≤ n - k ∧ (r (1 - t)).val + 1 + k ≤ (r (-t)).val + 1) : k + 1 ≤ stabilityThreshold n := stabilityThreshold_gt_of_no_isKStable (CompleteProfile.ofShift r hr0) (not_isKStable_of_shiftInvariant hodd hn3 hk_pos hkn r hr0 hr1 hrm1 hstep) /-- **Exact Sharpness Ceiling of the 1-Step Shift-Invariant Method**: Any bijection `r : ZMod n ≃ Fin n` satisfying the 1-step shift-invariant blocking hypotheses necessarily satisfies $5k \le 2n - 1$. -/ theorem one_step_shift_invariant_ceiling {n k : ℕ} [NeZero n] (hn3 : 3 ≤ n) (hk_pos : 1 ≤ k) (hkn : k < n) (r : ZMod n ≃ Fin n) (hr0 : (r 0).val + 1 = n) (hr1 : (r 1).val + 1 = 1) (hstep : ∀ t : ZMod n, t ≠ 0 → t ≠ 1 → (r t).val + 1 ≤ k → (r (t - 1)).val + 1 ≤ n - k ∧ (r (1 - t)).val + 1 + k ≤ (r (-t)).val + 1) : 5 * k ≤ 2 * n - 1 := by classical -- Let T = {d | R(d) ≤ k} (size k) and S = {1 - t | t ∈ T \ {1}} (size k - 1). let T : Finset (ZMod n) := Finset.univ.filter fun d ↦ (r d).val + 1 ≤ k let T' : Finset (ZMod n) := T.erase 1 let S : Finset (ZMod n) := T'.image (fun t ↦ (1 : ZMod n) - t) have hT_card : T.card = k := by have hbij : T.card = (Finset.univ.filter (fun v : Fin n ↦ v.val + 1 ≤ k)).card := by apply Finset.card_bij (fun d _ ↦ r d) · intro d hd simp only [T, Finset.mem_filter, Finset.mem_univ, true_and] at hd ⊢ exact hd · intro d₁ _ d₂ _ h; exact r.injective h · intro v hv refine ⟨r.symm v, ?_, r.apply_symm_apply v⟩ simp only [T, Finset.mem_filter, Finset.mem_univ, true_and] at hv ⊢ simpa using hv rw [hbij] have hset : (Finset.univ.filter (fun v : Fin n ↦ v.val + 1 ≤ k)) = (Finset.range k).attach.image (fun x ↦ (⟨x.1, by have := Finset.mem_range.1 x.2; omega⟩ : Fin n)) := by ext v simp only [Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image, Finset.mem_attach, true_and, Subtype.exists, Finset.mem_range] constructor · intro hv exact ⟨v.val, by omega, by ext; rfl⟩ · rintro ⟨m, hm, rfl⟩ dsimp omega rw [hset, Finset.card_image_of_injective] · simp · intro x y h ext exact congrArg Fin.val h have h1_in_T : (1 : ZMod n) ∈ T := by simp only [T, Finset.mem_filter, Finset.mem_univ, true_and] omega have hT'_card : T'.card = k - 1 := by have h1 : T'.card + 1 = T.card := Finset.card_erase_add_one h1_in_T omega have hS_card : S.card = k - 1 := by rw [Finset.card_image_of_injective] · exact hT'_card · intro x y h exact sub_right_injective h -- Partition S into A = S ∩ T and B = S \ T let A := S.filter fun s ↦ (r s).val + 1 ≤ k let B := S.filter fun s ↦ ¬ ((r s).val + 1 ≤ k) have hAB : A.card + B.card = k - 1 := by rw [Finset.card_filter_add_card_filter_not, hS_card] -- Every s ∈ S corresponds to t = 1 - s ∈ T' (so t ≠ 0, t ≠ 1, R(t) ≤ k). -- Hence R(s) + k ≤ R(s - 1) and R(-s) ≤ n - k! have hS_props : ∀ s ∈ S, (r s).val + 1 + k ≤ (r (s - 1)).val + 1 ∧ (r (-s)).val + 1 ≤ n - k := by intro s hs rcases Finset.mem_image.1 hs with ⟨t, ht, rfl⟩ have ht_ne1 : t ≠ 1 := (Finset.mem_erase.1 ht).1 have htT : (r t).val + 1 ≤ k := (Finset.mem_filter.1 (Finset.mem_erase.1 ht).2).2 have ht_ne0 : t ≠ 0 := by intro h0; rw [h0, hr0] at htT; omega obtain ⟨h3a, h3b⟩ := hstep t ht_ne0 ht_ne1 htT have h1 : (1 - t) - 1 = -t := by ring have h2 : -(1 - t) = t - 1 := by ring rw [h1, h2] exact ⟨h3b, h3a⟩ -- Bound A.card: every s ∈ A has s ≠ 1, 1 - s ∈ S, so -(1 - s) = s - 1 has R(s - 1) ≤ n - k. -- Combined with R(s) + k ≤ R(s - 1), we get 2 ≤ R(s) ≤ n - 2k! have hA_bound : A.card ≤ n - 2 * k - 1 := by have hsub : A.image r ⊆ Finset.univ.filter (fun v : Fin n ↦ 1 ≤ v.val ∧ v.val + 1 + 2 * k ≤ n) := by intro v hv rcases Finset.mem_image.1 hv with ⟨s, hsA, rfl⟩ have hsS : s ∈ S := (Finset.mem_filter.1 hsA).1 have hsT : (r s).val + 1 ≤ k := (Finset.mem_filter.1 hsA).2 have hs_ne1 : s ≠ 1 := by intro h1 rcases Finset.mem_image.1 hsS with ⟨t, ht, ht1⟩ have ht0 : t = 0 := by rw [h1] at ht1; linear_combination -ht1 have htT : (r t).val + 1 ≤ k := (Finset.mem_filter.1 (Finset.mem_erase.1 ht).2).2 rw [ht0, hr0] at htT omega have hv_ge1 : 1 ≤ (r s).val := by by_contra hlt have hv0 : (r s).val = 0 := by omega have hr1' : (r 1).val = 0 := by omega exact hs_ne1 (r.injective (Fin.ext (hv0.trans hr1'.symm))) have h1ms_in_S : (1 : ZMod n) - s ∈ S := by apply Finset.mem_image.2 refine ⟨s, Finset.mem_erase.2 ⟨hs_ne1, Finset.mem_filter.2 ⟨Finset.mem_univ _, hsT⟩⟩, rfl⟩ have h1 := (hS_props s hsS).1 have h2 := (hS_props (1 - s) h1ms_in_S).2 have hneg : -((1 : ZMod n) - s) = s - 1 := by ring rw [hneg] at h2 simp only [Finset.mem_filter, Finset.mem_univ, true_and] omega have hcard1 : A.card = (A.image r).card := (Finset.card_image_of_injective A r.injective).symm have hcard2 := Finset.card_le_card hsub have hcard3 : (Finset.univ.filter (fun v : Fin n ↦ 1 ≤ v.val ∧ v.val + 1 + 2 * k ≤ n)).card ≤ n - 2 * k - 1 := by have hsub' : (Finset.univ.filter (fun v : Fin n ↦ 1 ≤ v.val ∧ v.val + 1 + 2 * k ≤ n)).image Fin.val ⊆ Finset.Ico 1 (n - 2 * k) := by intro m hm rcases Finset.mem_image.1 hm with ⟨v, hv, rfl⟩ simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hv simp only [Finset.mem_Ico] omega have hc := Finset.card_le_card hsub' rw [Finset.card_image_of_injective _ Fin.val_injective, Nat.card_Ico] at hc exact hc omega -- Bound B.card: every s ∈ B has R(s) ≥ k + 1, so R(s - 1) ≥ 2k + 1 and R(s - 1) ≤ n - 1 -- (since s - 1 ≠ 0, as s = 1 ∉ S). have hB_bound : B.card ≤ n - 2 * k - 1 := by have hsub : (B.image (fun s ↦ r (s - 1))) ⊆ Finset.univ.filter (fun v : Fin n ↦ 2 * k ≤ v.val ∧ v.val + 1 < n) := by intro v hv rcases Finset.mem_image.1 hv with ⟨s, hsB, rfl⟩ have hsS : s ∈ S := (Finset.mem_filter.1 hsB).1 have hsNT : ¬ ((r s).val + 1 ≤ k) := (Finset.mem_filter.1 hsB).2 have hstep_s := (hS_props s hsS).1 have hs_ne1 : s ≠ 1 := by intro h1 rcases Finset.mem_image.1 hsS with ⟨t, ht, ht1⟩ have ht0 : t = 0 := by rw [h1] at ht1; linear_combination -ht1 have htT : (r t).val + 1 ≤ k := (Finset.mem_filter.1 (Finset.mem_erase.1 ht).2).2 rw [ht0, hr0] at htT omega have hsm1_ne0 : s - 1 ≠ 0 := sub_ne_zero.2 hs_ne1 have hne_last : (r (s - 1)).val + 1 ≠ n := by intro heq exact hsm1_ne0 (r.injective (Fin.ext (by omega))) have hlt_n := (r (s - 1)).isLt simp only [Finset.mem_filter, Finset.mem_univ, true_and] omega have hinj : Function.Injective (fun s : ZMod n ↦ r (s - 1)) := by intro s₁ s₂ h have h' := r.injective h exact sub_left_inj.1 h' have hcard1 : B.card = (B.image (fun s ↦ r (s - 1))).card := (Finset.card_image_of_injective B hinj).symm have hcard2 := Finset.card_le_card hsub have hcard3 : (Finset.univ.filter (fun v : Fin n ↦ 2 * k ≤ v.val ∧ v.val + 1 < n)).card ≤ n - 2 * k - 1 := by have hsub' : (Finset.univ.filter (fun v : Fin n ↦ 2 * k ≤ v.val ∧ v.val + 1 < n)).image Fin.val ⊆ Finset.Ico (2 * k) (n - 1) := by intro m hm rcases Finset.mem_image.1 hm with ⟨v, hv, rfl⟩ simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hv simp only [Finset.mem_Ico] omega have hc := Finset.card_le_card hsub' rw [Finset.card_image_of_injective _ Fin.val_injective, Nat.card_Ico] at hc omega omega omega /-! ## Section 6: Unified Exact $\frac{2}{5} N$ Infinite Family & Universal Linear Lower Bound -/ /-- Extend any partial injection `dom : Fin m → α`, `cod : Fin m → β` between finite types of the same cardinality to a global bijection `α ≃ β`. -/ noncomputable def equivOfPartialInjection {α β : Type*} [Fintype α] [Fintype β] [DecidableEq α] [DecidableEq β] {m : ℕ} (hcard : Fintype.card α = Fintype.card β) (dom : Fin m → α) (cod : Fin m → β) (hdom : Function.Injective dom) (hcod : Function.Injective cod) : { e : α ≃ β // ∀ i : Fin m, e (dom i) = cod i } := by classical let D : Finset α := Finset.univ.image dom let C : Finset β := Finset.univ.image cod have hD_card : D.card = m := by rw [Finset.card_image_of_injective _ hdom, Finset.card_fin] have hC_card : C.card = m := by rw [Finset.card_image_of_injective _ hcod, Finset.card_fin] let e_dom : Fin m ≃ {x // x ∈ D} := Equiv.ofBijective (fun i ↦ ⟨dom i, Finset.mem_image_of_mem dom (Finset.mem_univ i)⟩) ⟨ fun i j h ↦ hdom (congrArg Subtype.val h), fun ⟨x, hx⟩ ↦ by rcases Finset.mem_image.1 hx with ⟨i, _, rfl⟩ exact ⟨i, rfl⟩⟩ let e_cod : Fin m ≃ {y // y ∈ C} := Equiv.ofBijective (fun i ↦ ⟨cod i, Finset.mem_image_of_mem cod (Finset.mem_univ i)⟩) ⟨ fun i j h ↦ hcod (congrArg Subtype.val h), fun ⟨y, hy⟩ ↦ by rcases Finset.mem_image.1 hy with ⟨i, _, rfl⟩ exact ⟨i, rfl⟩⟩ let e_in : {x // x ∈ (D : Set α)} ≃ {y // y ∈ (C : Set β)} := e_dom.symm.trans e_cod have hcompl_card : Fintype.card {x // x ∉ (D : Set α)} = Fintype.card {y // y ∉ (C : Set β)} := by rw [Fintype.card_subtype_compl, Fintype.card_subtype_compl, hcard] simp only [Finset.mem_coe, Fintype.card_coe, hD_card, hC_card] let e_out : {x // x ∉ (D : Set α)} ≃ {y // y ∉ (C : Set β)} := Fintype.equivOfCardEq hcompl_card let e : α ≃ β := (Equiv.Set.sumCompl (D : Set α)).symm.trans ((Equiv.sumCongr e_in e_out).trans (Equiv.Set.sumCompl (C : Set β))) refine ⟨e, fun i ↦ ?_⟩ have hiD : dom i ∈ (D : Set α) := Finset.mem_coe.2 (Finset.mem_image_of_mem dom (Finset.mem_univ i)) have h1 : (Equiv.Set.sumCompl (D : Set α)).symm (dom i) = Sum.inl ⟨dom i, hiD⟩ := by apply (Equiv.Set.sumCompl (D : Set α)).injective simp only [Equiv.apply_symm_apply] rfl have h2 : e_in ⟨dom i, hiD⟩ = ⟨cod i, Finset.mem_coe.2 (Finset.mem_image_of_mem cod (Finset.mem_univ i))⟩ := by have hdom_eq : e_dom.symm ⟨dom i, hiD⟩ = i := e_dom.symm_apply_apply i change e_cod (e_dom.symm ⟨dom i, hiD⟩) = _ rw [hdom_eq] rfl change (Equiv.Set.sumCompl (C : Set β)) ((Equiv.sumCongr e_in e_out) ((Equiv.Set.sumCompl (D : Set α)).symm (dom i))) = cod i rw [h1, Equiv.sumCongr_apply, Sum.map_inl, h2] rfl /-- Natural-number coordinates of the symmetric set $A \subset [5, n - 4]$ of size `a` in the unified $\frac{2}{5} N$ construction. -/ private def unifiedA (n a j : ℕ) : ℕ := if j < 2 * (a / 2) then if j % 2 = 0 then 2 * j + 5 else n - 2 * j - 2 else (n + 1) / 2 /-- Involution pairing `j` with `pairIdx a j` so that `unifiedA n a j + unifiedA n a (pairIdx a j) = n + 1`. -/ private def pairIdx (a j : ℕ) : ℕ := if j + 1 < a ∧ j % 2 = 0 then j + 1 else if j % 2 = 1 then j - 1 else j private lemma pairIdx_lt {a j : ℕ} (hj : j < a) : pairIdx a j < a := by unfold pairIdx split_ifs <;> omega private lemma pairIdx_involutive {a j : ℕ} (hj : j < a) : pairIdx a (pairIdx a j) = j := by unfold pairIdx split_ifs <;> omega private lemma unifiedA_bounds {n a j : ℕ} (hodd : Odd n) (hfit : 4 * a + 5 ≤ n) (hj : j < a) : 5 ≤ unifiedA n a j ∧ unifiedA n a j ≤ n - 4 := by rcases hodd with ⟨m, rfl⟩ unfold unifiedA split_ifs <;> omega private lemma unifiedA_pair_sum {n a j : ℕ} (hodd : Odd n) (hfit : 4 * a + 5 ≤ n) (hj : j < a) : unifiedA n a j + unifiedA n a (pairIdx a j) = n + 1 := by rcases hodd with ⟨m, rfl⟩ unfold unifiedA pairIdx split_ifs <;> omega private lemma unifiedA_block_sep {n a j₁ j₂ : ℕ} (hodd : Odd n) (hfit : 4 * a + 5 ≤ n) (hj₁ : j₁ < a) (hj₂ : j₂ < a) (hne : j₁ ≠ j₂) : unifiedA n a j₁ + 4 ≤ unifiedA n a j₂ ∨ unifiedA n a j₂ + 4 ≤ unifiedA n a j₁ := by rcases hodd with ⟨m, rfl⟩ unfold unifiedA split_ifs <;> omega /-- Domain coordinates of the unified $\frac{2}{5} N$ partial injection. -/ private def unifiedDomNat (n a b idx : ℕ) : ℕ := if idx = 0 then 0 else if idx = 1 then 1 else if idx = 2 then 2 else if idx = 3 then n - 1 else if idx = 4 then n - 2 else if idx < 5 + a then unifiedA n a (idx - 5) else if idx < 5 + a + (b - 1) then unifiedA n a (pairIdx a (idx - (5 + a))) + 1 else if idx < 5 + 2 * a + (b - 1) then unifiedA n a (idx - (5 + a + (b - 1))) - 1 else unifiedA n a (idx - (5 + 2 * a + (b - 1))) - 2 /-- Codomain (0-based rank) coordinates of the unified $\frac{2}{5} N$ partial injection. -/ private def unifiedCodNat (n k a b idx : ℕ) : ℕ := if idx = 0 then n - 1 else if idx = 1 then 0 else if idx = 2 then a + 1 else if idx = 3 then k else if idx = 4 then 2 * k else if idx < 5 + a then 1 + (idx - 5) else if idx < 5 + a + (b - 1) then a + 2 + (idx - (5 + a)) else if idx < 5 + 2 * a + (b - 1) then k + 1 + (idx - (5 + a + (b - 1))) else 2 * k + 1 + (idx - (5 + 2 * a + (b - 1))) /-- Injectivity of `unifiedDomNat` on `Fin (5 + 2 * a + 2 * (b - 1))`. -/ private lemma unifiedDomNat_inj {n a b : ℕ} (hodd : Odd n) (hfit : 4 * a + 5 ≤ n) (_hb1 : 1 ≤ b) (hbm1 : b - 1 ≤ a) : Function.Injective (fun i : Fin (5 + 2 * a + 2 * (b - 1)) ↦ unifiedDomNat n a b i.val) := by set m := 5 + 2 * a + 2 * (b - 1) have hblk_disj : ∀ j₁ < a, ∀ j₂ < a, ∀ d₁ ≤ 3, ∀ d₂ ≤ 3, unifiedA n a j₁ + d₁ - 2 = unifiedA n a j₂ + d₂ - 2 → j₁ = j₂ ∧ d₁ = d₂ := by intro j₁ hj₁ j₂ hj₂ d₁ hd₁ d₂ hd₂ heq have h1 := unifiedA_bounds hodd hfit hj₁ have h2 := unifiedA_bounds hodd hfit hj₂ by_cases hjj : j₁ = j₂ · subst hjj; exact ⟨rfl, by omega⟩ · rcases unifiedA_block_sep hodd hfit hj₁ hj₂ hjj with hlt | hlt <;> omega have hsmall : ∀ i : Fin m, 5 ≤ i.val → 3 ≤ unifiedDomNat n a b i.val ∧ unifiedDomNat n a b i.val ≤ n - 3 := by intro i hi5 dsimp only [unifiedDomNat] split_ifs with h0 h1 h2 h3 h4 h5 h6 h7 all_goals try omega · have := unifiedA_bounds hodd hfit (j := i.val - 5) (by omega); omega · have := unifiedA_bounds hodd hfit (j := pairIdx a (i.val - (5 + a))) (pairIdx_lt (by omega)); omega · have := unifiedA_bounds hodd hfit (j := i.val - (5 + a + (b - 1))) (by omega); omega · have := unifiedA_bounds hodd hfit (j := i.val - (5 + 2 * a + (b - 1))) (by omega); omega have hlt5_val : ∀ i : Fin m, i.val < 5 → (i.val = 0 ∧ unifiedDomNat n a b i.val = 0) ∨ (i.val = 1 ∧ unifiedDomNat n a b i.val = 1) ∨ (i.val = 2 ∧ unifiedDomNat n a b i.val = 2) ∨ (i.val = 3 ∧ unifiedDomNat n a b i.val = n - 1) ∨ (i.val = 4 ∧ unifiedDomNat n a b i.val = n - 2) := by intro i hi5 have hcases : i.val = 0 ∨ i.val = 1 ∨ i.val = 2 ∨ i.val = 3 ∨ i.val = 4 := by omega rcases hcases with h | h | h | h | h · exact Or.inl ⟨h, by dsimp only [unifiedDomNat]; rw [h]; norm_num⟩ · exact Or.inr (Or.inl ⟨h, by dsimp only [unifiedDomNat]; rw [h]; norm_num⟩) · exact Or.inr (Or.inr (Or.inl ⟨h, by dsimp only [unifiedDomNat]; rw [h]; norm_num⟩)) · exact Or.inr (Or.inr (Or.inr (Or.inl ⟨h, by dsimp only [unifiedDomNat]; rw [h]; norm_num⟩))) · exact Or.inr (Or.inr (Or.inr (Or.inr ⟨h, by dsimp only [unifiedDomNat]; rw [h]; norm_num⟩))) intro i₁ i₂ heq dsimp only at heq ext by_contra hne by_cases h1_lt : i₁.val < 5 · by_cases h2_lt : i₂.val < 5 · rcases hlt5_val i₁ h1_lt with ⟨r1, v1⟩ | ⟨r1, v1⟩ | ⟨r1, v1⟩ | ⟨r1, v1⟩ | ⟨r1, v1⟩ <;> rcases hlt5_val i₂ h2_lt with ⟨r2, v2⟩ | ⟨r2, v2⟩ | ⟨r2, v2⟩ | ⟨r2, v2⟩ | ⟨r2, v2⟩ <;> omega · have hr := hsmall i₂ (by omega) rcases hlt5_val i₁ h1_lt with ⟨_, v1⟩ | ⟨_, v1⟩ | ⟨_, v1⟩ | ⟨_, v1⟩ | ⟨_, v1⟩ <;> omega · by_cases h2_lt : i₂.val < 5 · have hr := hsmall i₁ (by omega) rcases hlt5_val i₂ h2_lt with ⟨_, v2⟩ | ⟨_, v2⟩ | ⟨_, v2⟩ | ⟨_, v2⟩ | ⟨_, v2⟩ <;> omega · have hrepr : ∀ i : Fin m, 5 ≤ i.val → ∃ j < a, ∃ d ≤ 3, unifiedDomNat n a b i.val = unifiedA n a j + d - 2 ∧ ((d = 2 ∧ i.val = 5 + j) ∨ (d = 3 ∧ ∃ t < b - 1, i.val = 5 + a + t ∧ j = pairIdx a t) ∨ (d = 1 ∧ i.val = 5 + a + (b - 1) + j) ∨ (d = 0 ∧ i.val = 5 + 2 * a + (b - 1) + j)) := by intro i hi5 have h0 : ¬ (i.val = 0) := by omega have h1 : ¬ (i.val = 1) := by omega have h2 : ¬ (i.val = 2) := by omega have h3 : ¬ (i.val = 3) := by omega have h4 : ¬ (i.val = 4) := by omega by_cases h5 : i.val < 5 + a · have hdom_eq : unifiedDomNat n a b i.val = unifiedA n a (i.val - 5) := by dsimp only [unifiedDomNat]; simp only [h0, h1, h2, h3, h4, h5, ↓reduceIte] refine ⟨i.val - 5, by omega, 2, by omega, ?_, Or.inl ⟨rfl, by omega⟩⟩ have := unifiedA_bounds hodd hfit (j := i.val - 5) (by omega); omega · by_cases h6 : i.val < 5 + a + (b - 1) · have hdom_eq : unifiedDomNat n a b i.val = unifiedA n a (pairIdx a (i.val - (5 + a))) + 1 := by dsimp only [unifiedDomNat]; simp only [h0, h1, h2, h3, h4, h5, h6, ↓reduceIte] refine ⟨pairIdx a (i.val - (5 + a)), pairIdx_lt (by omega), 3, by omega, ?_, Or.inr (Or.inl ⟨rfl, i.val - (5 + a), by omega, by omega, rfl⟩)⟩ have := unifiedA_bounds hodd hfit (j := pairIdx a (i.val - (5 + a))) (pairIdx_lt (by omega)); omega · by_cases h7 : i.val < 5 + 2 * a + (b - 1) · have hdom_eq : unifiedDomNat n a b i.val = unifiedA n a (i.val - (5 + a + (b - 1))) - 1 := by dsimp only [unifiedDomNat]; simp only [h0, h1, h2, h3, h4, h5, h6, h7, ↓reduceIte] refine ⟨i.val - (5 + a + (b - 1)), by omega, 1, by omega, ?_, Or.inr (Or.inr (Or.inl ⟨rfl, by omega⟩))⟩ have := unifiedA_bounds hodd hfit (j := i.val - (5 + a + (b - 1))) (by omega); omega · have hdom_eq : unifiedDomNat n a b i.val = unifiedA n a (i.val - (5 + 2 * a + (b - 1))) - 2 := by dsimp only [unifiedDomNat]; simp only [h0, h1, h2, h3, h4, h5, h6, h7, ↓reduceIte] refine ⟨i.val - (5 + 2 * a + (b - 1)), by omega, 0, by omega, ?_, Or.inr (Or.inr (Or.inr ⟨rfl, by omega⟩))⟩ have := unifiedA_bounds hodd hfit (j := i.val - (5 + 2 * a + (b - 1))) (by omega); omega obtain ⟨j₁, hj₁, d₁, hd₁, hval₁, hcase₁⟩ := hrepr i₁ (by omega) obtain ⟨j₂, hj₂, d₂, hd₂, hval₂, hcase₂⟩ := hrepr i₂ (by omega) rw [hval₁, hval₂] at heq obtain ⟨rfl, rfl⟩ := hblk_disj j₁ hj₁ j₂ hj₂ d₁ hd₁ d₂ hd₂ heq rcases hcase₁ with ⟨hd, hi1⟩ | ⟨hd, t₁, ht₁, hi1, hp1⟩ | ⟨hd, hi1⟩ | ⟨hd, hi1⟩ <;> rcases hcase₂ with ⟨hd', hi2⟩ | ⟨hd', t₂, ht₂, hi2, hp2⟩ | ⟨hd', hi2⟩ | ⟨hd', hi2⟩ <;> try omega have ht_eq : t₁ = t₂ := by have h1 := pairIdx_involutive (a := a) (j := t₁) (by omega) have h2 := pairIdx_involutive (a := a) (j := t₂) (by omega) rw [← h1, ← hp1, hp2, h2] omega /-- Injectivity of `unifiedCodNat` on `Fin (5 + 2 * a + 2 * (b - 1))`. -/ private lemma unifiedCodNat_inj {n k a b : ℕ} (hk : 2 ≤ k) (hab : a + b = k - 1) (hb1 : 1 ≤ b) (_hbm1 : b - 1 ≤ a) (hbound : 2 * k + b + 1 ≤ n) : Function.Injective (fun i : Fin (5 + 2 * a + 2 * (b - 1)) ↦ unifiedCodNat n k a b i.val) := by let invCod : ℕ → ℕ := fun v ↦ if v = n - 1 then 0 else if v = 0 then 1 else if v ≤ a then 5 + (v - 1) else if v = a + 1 then 2 else if v < k then 5 + a + (v - (a + 2)) else if v = k then 3 else if v ≤ k + a then 5 + a + (b - 1) + (v - (k + 1)) else if v = 2 * k then 4 else 5 + 2 * a + (b - 1) + (v - (2 * k + 1)) have hinv_eval : ∀ v, (v = n - 1 → invCod v = 0) ∧ (v = 0 → invCod v = 1) ∧ (1 ≤ v ∧ v ≤ a → invCod v = 5 + (v - 1)) ∧ (v = a + 1 → invCod v = 2) ∧ (a + 2 ≤ v ∧ v < k → invCod v = 5 + a + (v - (a + 2))) ∧ (v = k → invCod v = 3) ∧ (k + 1 ≤ v ∧ v ≤ k + a → invCod v = 5 + a + (b - 1) + (v - (k + 1))) ∧ (v = 2 * k → invCod v = 4) ∧ (2 * k + 1 ≤ v ∧ v < n - 1 → invCod v = 5 + 2 * a + (b - 1) + (v - (2 * k + 1))) := by intro v refine ⟨?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_, ?_⟩ · intro h; dsimp only [invCod]; rw [if_pos h] · intro h; dsimp only [invCod]; rw [if_neg (by omega), if_pos h] · rintro ⟨h1, h2⟩; dsimp only [invCod]; rw [if_neg (by omega), if_neg (by omega), if_pos h2] · intro h; dsimp only [invCod] rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), if_pos h] · rintro ⟨h1, h2⟩; dsimp only [invCod] rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_pos h2] · intro h; dsimp only [invCod] rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_pos h] · rintro ⟨h1, h2⟩; dsimp only [invCod] rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_pos h2] · intro h; dsimp only [invCod] rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_pos h] · rintro ⟨h1, h2⟩; dsimp only [invCod] rw [if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega), if_neg (by omega)] have hleft : ∀ i : Fin (5 + 2 * a + 2 * (b - 1)), invCod (unifiedCodNat n k a b i.val) = i.val := by intro i by_cases h0 : i.val = 0 · have hv : unifiedCodNat n k a b i.val = n - 1 := by dsimp only [unifiedCodNat]; rw [if_pos h0] rw [hv, (hinv_eval _).1 rfl, h0] · by_cases h1 : i.val = 1 · have hv : unifiedCodNat n k a b i.val = 0 := by dsimp only [unifiedCodNat]; rw [if_neg h0, if_pos h1] rw [hv, (hinv_eval _).2.1 rfl, h1] · by_cases h2 : i.val = 2 · have hv : unifiedCodNat n k a b i.val = a + 1 := by dsimp only [unifiedCodNat]; rw [if_neg h0, if_neg h1, if_pos h2] rw [hv, (hinv_eval _).2.2.2.1 rfl, h2] · by_cases h3 : i.val = 3 · have hv : unifiedCodNat n k a b i.val = k := by dsimp only [unifiedCodNat]; rw [if_neg h0, if_neg h1, if_neg h2, if_pos h3] rw [hv, (hinv_eval _).2.2.2.2.2.1 rfl, h3] · by_cases h4 : i.val = 4 · have hv : unifiedCodNat n k a b i.val = 2 * k := by dsimp only [unifiedCodNat]; rw [if_neg h0, if_neg h1, if_neg h2, if_neg h3, if_pos h4] rw [hv, (hinv_eval _).2.2.2.2.2.2.2.1 rfl, h4] · by_cases h5 : i.val < 5 + a · have hv : unifiedCodNat n k a b i.val = 1 + (i.val - 5) := by dsimp only [unifiedCodNat] rw [if_neg h0, if_neg h1, if_neg h2, if_neg h3, if_neg h4, if_pos h5] rw [hv, (hinv_eval _).2.2.1 ⟨by omega, by omega⟩]; omega · by_cases h6 : i.val < 5 + a + (b - 1) · have hv : unifiedCodNat n k a b i.val = a + 2 + (i.val - (5 + a)) := by dsimp only [unifiedCodNat] rw [if_neg h0, if_neg h1, if_neg h2, if_neg h3, if_neg h4, if_neg h5, if_pos h6] rw [hv, (hinv_eval _).2.2.2.2.1 ⟨by omega, by omega⟩]; omega · by_cases h7 : i.val < 5 + 2 * a + (b - 1) · have hv : unifiedCodNat n k a b i.val = k + 1 + (i.val - (5 + a + (b - 1))) := by dsimp only [unifiedCodNat] rw [if_neg h0, if_neg h1, if_neg h2, if_neg h3, if_neg h4, if_neg h5, if_neg h6, if_pos h7] rw [hv, (hinv_eval _).2.2.2.2.2.2.1 ⟨by omega, by omega⟩]; omega · have hv : unifiedCodNat n k a b i.val = 2 * k + 1 + (i.val - (5 + 2 * a + (b - 1))) := by dsimp only [unifiedCodNat] rw [if_neg h0, if_neg h1, if_neg h2, if_neg h3, if_neg h4, if_neg h5, if_neg h6, if_neg h7] rw [hv, (hinv_eval _).2.2.2.2.2.2.2.2 ⟨by omega, by omega⟩]; omega intro i₁ i₂ heq dsimp only at heq ext rw [← hleft i₁, ← hleft i₂, heq] /-- **Unified Exact $\frac{2}{5} N$ Infinite Family**: For every $k \ge 2$ and every odd $n \ge 2k + \lfloor k / 2 \rfloor + 1$, `k + 1 ≤ stabilityThreshold n`. -/ theorem stabilityThreshold_ge_of_odd_two_fifths {k n : ℕ} (hk : 2 ≤ k) (hodd : Odd n) (hbound : 2 * k + k / 2 + 1 ≤ n) : k + 1 ≤ stabilityThreshold n := by have hn3 : 3 ≤ n := by omega have : NeZero n := ⟨by omega⟩ set a := (k - 1) / 2 set b := k / 2 have hab : a + b = k - 1 := by omega have hb_ge1 : 1 ≤ b := by omega have hbm1_le_a : b - 1 ≤ a := by omega have hfit : 4 * a + 5 ≤ n := by rcases hodd with ⟨q, hq⟩ omega have hkn : k < n := by omega set m := 5 + 2 * a + 2 * (b - 1) let domNat : Fin m → ℕ := fun i ↦ unifiedDomNat n a b i.val let codNat : Fin m → ℕ := fun i ↦ unifiedCodNat n k a b i.val have hdomNat_lt : ∀ i : Fin m, domNat i < n := by intro i dsimp only [domNat, unifiedDomNat] split_ifs with h0 h1 h2 h3 h4 h5 h6 h7 · omega · omega · omega · omega · omega · have := unifiedA_bounds hodd hfit (j := i.val - 5) (by omega); omega · have := unifiedA_bounds hodd hfit (j := pairIdx a (i.val - (5 + a))) (pairIdx_lt (by omega)); omega · have := unifiedA_bounds hodd hfit (j := i.val - (5 + a + (b - 1))) (by omega); omega · have := unifiedA_bounds hodd hfit (j := i.val - (5 + 2 * a + (b - 1))) (by omega); omega have hcodNat_lt : ∀ i : Fin m, codNat i < n := by intro i dsimp only [codNat, unifiedCodNat] split_ifs <;> omega have hdomNat_inj : Function.Injective domNat := unifiedDomNat_inj hodd hfit hb_ge1 hbm1_le_a have hcodNat_inj : Function.Injective codNat := unifiedCodNat_inj hk hab hb_ge1 hbm1_le_a hbound -- Lift domNat, codNat to ZMod n and Fin n, and extend to a global bijection r : ZMod n ≃ Fin n! let dom : Fin m → ZMod n := fun i ↦ (domNat i : ZMod n) let cod : Fin m → Fin n := fun i ↦ ⟨codNat i, hcodNat_lt i⟩ have hdom_inj : Function.Injective dom := by intro i₁ i₂ heq apply hdomNat_inj have hv := congrArg ZMod.val heq dsimp only [dom] at hv rwa [ZMod.val_natCast_of_lt (hdomNat_lt i₁), ZMod.val_natCast_of_lt (hdomNat_lt i₂)] at hv have hcod_inj : Function.Injective cod := by intro i₁ i₂ heq exact hcodNat_inj (congrArg Fin.val heq) obtain ⟨r, hr⟩ := equivOfPartialInjection (by simp [ZMod.card]) dom cod hdom_inj hcod_inj -- Extract evaluation formulas for r at every assigned point! have hr_eval : ∀ (idx : ℕ) (hidx : idx < m), (r (domNat ⟨idx, hidx⟩ : ZMod n)).val = codNat ⟨idx, hidx⟩ := by intro idx hidx exact congrArg Fin.val (hr ⟨idx, hidx⟩) have hr0 : (r 0).val + 1 = n := by have h := hr_eval 0 (by omega) dsimp only [domNat, codNat, unifiedDomNat, unifiedCodNat] at h rw [if_pos rfl, if_pos rfl] at h simp only [Nat.cast_zero] at h omega have hr1 : (r 1).val + 1 = 1 := by have h := hr_eval 1 (by omega) dsimp only [domNat, codNat, unifiedDomNat, unifiedCodNat] at h rw [if_neg (by decide), if_pos rfl, if_neg (by decide), if_pos rfl] at h simp only [Nat.cast_one] at h omega have hr2 : (r 2).val + 1 = a + 2 := by have h := hr_eval 2 (by omega) dsimp only [domNat, codNat, unifiedDomNat, unifiedCodNat] at h rw [if_neg (by decide), if_neg (by decide), if_pos rfl, if_neg (by decide), if_neg (by decide), if_pos rfl] at h simp only [Nat.cast_ofNat] at h omega have hrm1_eq : (r (-1)).val + 1 = k + 1 := by have h := hr_eval 3 (by omega) dsimp only [domNat, codNat, unifiedDomNat, unifiedCodNat] at h simp only [OfNat.ofNat_ne_zero, ↓reduceIte, Nat.reduceEqDiff] at h have hcast : ((n - 1 : ℕ) : ZMod n) = (-1 : ZMod n) := by have : (n - 1 : ℕ) + 1 = n := by omega have hc : (((n - 1 : ℕ) + 1 : ℕ) : ZMod n) = 0 := by rw [this, ZMod.natCast_self] push_cast at hc linear_combination hc rw [hcast] at h omega have hrm2_eq : (r (-2)).val + 1 = 2 * k + 1 := by have h := hr_eval 4 (by omega) dsimp only [domNat, codNat, unifiedDomNat, unifiedCodNat] at h simp only [OfNat.ofNat_ne_zero, ↓reduceIte, Nat.reduceEqDiff] at h have hcast : ((n - 2 : ℕ) : ZMod n) = (-2 : ZMod n) := by have : (n - 2 : ℕ) + 2 = n := by omega have hc : (((n - 2 : ℕ) + 2 : ℕ) : ZMod n) = 0 := by rw [this, ZMod.natCast_self] push_cast at hc linear_combination hc rw [hcast] at h omega have hrA : ∀ j < a, (r (unifiedA n a j : ZMod n)).val + 1 = 2 + j := by intro j hj have h := hr_eval (5 + j) (by omega) dsimp only [domNat, codNat, unifiedDomNat, unifiedCodNat] at h have h1 : ¬ (5 + j = 0) := by omega have h2 : ¬ (5 + j = 1) := by omega have h3 : ¬ (5 + j = 2) := by omega have h4 : ¬ (5 + j = 3) := by omega have h5 : ¬ (5 + j = 4) := by omega have h6 : 5 + j < 5 + a := by omega rw [if_neg h1, if_neg h2, if_neg h3, if_neg h4, if_neg h5, if_pos h6, if_neg h1, if_neg h2, if_neg h3, if_neg h4, if_neg h5, if_pos h6, Nat.add_sub_cancel_left] at h omega have hrA_plus1 : ∀ j < b - 1, (r ((unifiedA n a (pairIdx a j) : ZMod n) + 1)).val + 1 = a + 3 + j := by intro j hj have h := hr_eval (5 + a + j) (by omega) dsimp only [domNat, codNat, unifiedDomNat, unifiedCodNat] at h have h1 : ¬ (5 + a + j = 0) := by omega have h2 : ¬ (5 + a + j = 1) := by omega have h3 : ¬ (5 + a + j = 2) := by omega have h4 : ¬ (5 + a + j = 3) := by omega have h5 : ¬ (5 + a + j = 4) := by omega have h6 : ¬ (5 + a + j < 5 + a) := by omega have h7 : 5 + a + j < 5 + a + (b - 1) := by omega rw [if_neg h1, if_neg h2, if_neg h3, if_neg h4, if_neg h5, if_neg h6, if_pos h7, if_neg h1, if_neg h2, if_neg h3, if_neg h4, if_neg h5, if_neg h6, if_pos h7, Nat.add_sub_cancel_left, Nat.cast_add, Nat.cast_one] at h omega have hrA_minus1 : ∀ j < a, (r ((unifiedA n a j : ZMod n) - 1)).val + 1 = k + 2 + j := by intro j hj have h := hr_eval (5 + a + (b - 1) + j) (by omega) dsimp only [domNat, codNat, unifiedDomNat, unifiedCodNat] at h have h1 : ¬ (5 + a + (b - 1) + j = 0) := by omega have h2 : ¬ (5 + a + (b - 1) + j = 1) := by omega have h3 : ¬ (5 + a + (b - 1) + j = 2) := by omega have h4 : ¬ (5 + a + (b - 1) + j = 3) := by omega have h5 : ¬ (5 + a + (b - 1) + j = 4) := by omega have h6 : ¬ (5 + a + (b - 1) + j < 5 + a) := by omega have h7 : ¬ (5 + a + (b - 1) + j < 5 + a + (b - 1)) := by omega have h8 : 5 + a + (b - 1) + j < 5 + 2 * a + (b - 1) := by omega rw [if_neg h1, if_neg h2, if_neg h3, if_neg h4, if_neg h5, if_neg h6, if_neg h7, if_pos h8, if_neg h1, if_neg h2, if_neg h3, if_neg h4, if_neg h5, if_neg h6, if_neg h7, if_pos h8, Nat.add_sub_cancel_left] at h have hge1 : 1 ≤ unifiedA n a j := by have := unifiedA_bounds hodd hfit hj; omega rw [Nat.cast_sub hge1, Nat.cast_one] at h omega have hrA_minus2 : ∀ j < b - 1, (r ((unifiedA n a j : ZMod n) - 2)).val + 1 = 2 * k + 2 + j := by intro j hj have h := hr_eval (5 + 2 * a + (b - 1) + j) (by omega) dsimp only [domNat, codNat, unifiedDomNat, unifiedCodNat] at h have h1 : ¬ (5 + 2 * a + (b - 1) + j = 0) := by omega have h2 : ¬ (5 + 2 * a + (b - 1) + j = 1) := by omega have h3 : ¬ (5 + 2 * a + (b - 1) + j = 2) := by omega have h4 : ¬ (5 + 2 * a + (b - 1) + j = 3) := by omega have h5 : ¬ (5 + 2 * a + (b - 1) + j = 4) := by omega have h6 : ¬ (5 + 2 * a + (b - 1) + j < 5 + a) := by omega have h7 : ¬ (5 + 2 * a + (b - 1) + j < 5 + a + (b - 1)) := by omega have h8 : ¬ (5 + 2 * a + (b - 1) + j < 5 + 2 * a + (b - 1)) := by omega rw [if_neg h1, if_neg h2, if_neg h3, if_neg h4, if_neg h5, if_neg h6, if_neg h7, if_neg h8, if_neg h1, if_neg h2, if_neg h3, if_neg h4, if_neg h5, if_neg h6, if_neg h7, if_neg h8, Nat.add_sub_cancel_left] at h have hge2 : 2 ≤ unifiedA n a j := by have := unifiedA_bounds hodd hfit (j := j) (by omega); omega rw [Nat.cast_sub hge2, Nat.cast_ofNat] at h omega -- Modular identity: `1 - (A j : ZMod n) = (A (pairIdx a j) : ZMod n)`. have hpair_zmod : ∀ j < a, (1 : ZMod n) - (unifiedA n a j : ZMod n) = (unifiedA n a (pairIdx a j) : ZMod n) := by intro j hj have hsum := unifiedA_pair_sum hodd hfit hj have hc : (((unifiedA n a j + unifiedA n a (pairIdx a j)) : ℕ) : ZMod n) = ((n + 1 : ℕ) : ZMod n) := by rw [hsum] push_cast at hc rw [ZMod.natCast_self, zero_add] at hc linear_combination -hc -- Apply Master 1-Step Shift-Invariant Blocking Theorem! apply stabilityThreshold_ge_of_shiftInvariant hodd hn3 (by omega) hkn r hr0 hr1 (by omega) intro t ht0 ht1 hrt -- Since (r t).val + 1 ≤ k, classify t by its rank value v = (r t).val + 1 ∈ {2 .. k}! set v := (r t).val + 1 have hv_ge2 : 2 ≤ v := by by_contra hlt have hv1 : (r t).val = 0 := by omega have h1' : (r 1).val = 0 := by omega exact ht1 (r.injective (Fin.ext (hv1.trans h1'.symm))) by_cases hv_A : v ≤ a + 1 · -- t = A j where j = v - 2 < a set j := v - 2 have hj : j < a := by omega have ht_eq : t = (unifiedA n a j : ZMod n) := by apply r.injective ext have := hrA j hj omega rw [ht_eq] have h3a : (r ((unifiedA n a j : ZMod n) - 1)).val + 1 ≤ n - k := by rw [hrA_minus1 j hj] omega have h3b : (r (1 - (unifiedA n a j : ZMod n))).val + 1 + k ≤ (r (-(unifiedA n a j : ZMod n))).val + 1 := by rw [hpair_zmod j hj, hrA (pairIdx a j) (pairIdx_lt hj)] have hneg : -(unifiedA n a j : ZMod n) = (unifiedA n a (pairIdx a j) : ZMod n) - 1 := by have := hpair_zmod j hj linear_combination this rw [hneg, hrA_minus1 (pairIdx a j) (pairIdx_lt hj)] omega exact ⟨h3a, h3b⟩ · by_cases hv_2 : v = a + 2 · -- t = 2 have ht_eq : t = 2 := by apply r.injective ext omega rw [ht_eq] constructor · have hsub : (2 : ZMod n) - 1 = 1 := by ring rw [hsub, hr1] omega · have hsub : (1 : ZMod n) - 2 = -1 := by ring rw [hsub, hrm1_eq, hrm2_eq] omega · -- t = A (pairIdx a j) + 1 where j = v - (a + 3) < b - 1 set j := v - (a + 3) have hj : j < b - 1 := by omega have ht_eq : t = (unifiedA n a (pairIdx a j) : ZMod n) + 1 := by apply r.injective ext have := hrA_plus1 j hj omega rw [ht_eq] constructor · have hsub : (unifiedA n a (pairIdx a j) : ZMod n) + 1 - 1 = (unifiedA n a (pairIdx a j) : ZMod n) := by ring have hplt : pairIdx a j < a := pairIdx_lt (by omega) rw [hsub, hrA (pairIdx a j) hplt] omega · have h1mt : (1 : ZMod n) - ((unifiedA n a (pairIdx a j) : ZMod n) + 1) = (unifiedA n a j : ZMod n) - 1 := by have hp := hpair_zmod (pairIdx a j) (pairIdx_lt (by omega)) rw [pairIdx_involutive (by omega)] at hp linear_combination hp have hnt : -((unifiedA n a (pairIdx a j) : ZMod n) + 1) = (unifiedA n a j : ZMod n) - 2 := by have hp := hpair_zmod (pairIdx a j) (pairIdx_lt (by omega)) rw [pairIdx_involutive (by omega)] at hp linear_combination hp rw [h1mt, hnt, hrA_minus1 j (by omega), hrA_minus2 j hj] omega /-- **Exact 1-Step Odd Lower Bound**: For every odd $n \ge 3$, $\lfloor (2n + 4) / 5 \rfloor \le K(n)$. -/ theorem stabilityThreshold_ge_two_fifths_odd {n : ℕ} (hodd : Odd n) (hn3 : 3 ≤ n) : (2 * n + 4) / 5 ≤ stabilityThreshold n := by by_cases hn5 : n ≤ 5 · rcases hodd with ⟨m, rfl⟩ have : m = 1 ∨ m = 2 := by omega rcases this with rfl | rfl · norm_num; rw [stabilityThreshold_three] · norm_num; rw [stabilityThreshold_five]; decide · -- For odd n ≥ 7, let k = (2 * n - 1) / 5 ≥ 2! set k := (2 * n - 1) / 5 have hk2 : 2 ≤ k := by omega have hgoal : (2 * n + 4) / 5 = k + 1 := by omega rw [hgoal] -- Check if n ≥ 2*k + k/2 + 1 directly, else n = 10q + 5 and use monotonicity from n - 2! by_cases hfit : 2 * k + k / 2 + 1 ≤ n · exact stabilityThreshold_ge_of_odd_two_fifths hk2 hodd hfit · -- Only fails when n ≡ 5 (mod 10), where n ≥ 15 and n - 2 satisfies 2*k + k/2 + 1 ≤ n - 2! have hn_sub : 2 * k + k / 2 + 1 ≤ n - 2 := by rcases hodd with ⟨m, rfl⟩ omega have hodd_sub : Odd (n - 2) := by rcases hodd with ⟨m, rfl⟩ refine ⟨m - 1, ?_⟩ omega exact le_trans (stabilityThreshold_ge_of_odd_two_fifths hk2 hodd_sub hn_sub) (stabilityThreshold_monotone (by omega) (by omega)) /-- **Main Theorem (Universal Explicit Linear Lower Bound)**: For EVERY $n \ge 1$, $$\left\lfloor \frac{2n + 2}{5} \right\rfloor \le \mathrm{stabilityThreshold}\, n.$$ This improves Fanelli–Moscardelli's ECAI 2025 bound $\Omega(\sqrt{N})$ to an explicit linear lower bound with slope $2/5$ valid for all $N \ge 1$. -/ theorem stabilityThreshold_linear_lower_bound (n : ℕ) (hn : 1 ≤ n) : (2 * n + 2) / 5 ≤ stabilityThreshold n := by by_cases hn3 : n ≤ 3 · interval_cases n · rw [stabilityThreshold_one]; decide · rw [stabilityThreshold_two] · rw [stabilityThreshold_three]; decide · by_cases hodd : Odd n · exact le_trans (by omega) (stabilityThreshold_ge_two_fifths_odd hodd (by omega)) · have hodd_prev : Odd (n - 1) := by rw [Nat.not_odd_iff_even] at hodd rcases hodd with ⟨m, rfl⟩ refine ⟨m - 1, ?_⟩ omega have h1 := stabilityThreshold_ge_two_fifths_odd hodd_prev (by omega) have h2 := stabilityThreshold_mono (n - 1) (by omega) have hnm : n - 1 + 1 = n := by omega rw [hnm] at h2 have hdiv : (2 * n + 2) / 5 ≤ (2 * (n - 1) + 4) / 5 := by omega exact le_trans hdiv (le_trans h1 h2) /-- **Two-Sided Universal Sandwich Theorem**: For EVERY $n \ge 1$, $$\left\lfloor \frac{2n + 2}{5} \right\rfloor \le \mathrm{stabilityThreshold}\, n \le \left\lfloor \frac{n + 1}{2} \right\rfloor.$$ -/ theorem stabilityThreshold_sandwich (n : ℕ) (hn : 1 ≤ n) : (2 * n + 2) / 5 ≤ stabilityThreshold n ∧ stabilityThreshold n ≤ (n + 1) / 2 := ⟨stabilityThreshold_linear_lower_bound n hn, stabilityThreshold_le_half n hn⟩ /-! ## Section 7: Structural Sub-$\lceil N / 2 \rceil$ Upper Bound Theorem (Odd $N = 2m + 1$) -/ /-- The critical digraph on `Fin (2 * m + 1)`: a directed edge `u → v` means `u` ranks `v` first (rank `1`) and `v` ranks `u` at `m + 1`. -/ def CritAdj {m : ℕ} (p : CompleteProfile (2 * m + 1)) (u v : Fin (2 * m + 1)) : Prop := p.partnerRank u v = 1 ∧ p.partnerRank v u = m + 1 /-- Every vertex has out-degree at most $1$ in `CritAdj`. -/ theorem CritAdj.out_unique {m : ℕ} {p : CompleteProfile (2 * m + 1)} {u v₁ v₂ : Fin (2 * m + 1)} (h1 : CritAdj p u v₁) (h2 : CritAdj p u v₂) : v₁ = v₂ := p.partnerRank_injective u (h1.1.trans h2.1.symm) /-- Every vertex has in-degree at most $1$ in `CritAdj`. -/ theorem CritAdj.in_unique {m : ℕ} {p : CompleteProfile (2 * m + 1)} {u₁ u₂ v : Fin (2 * m + 1)} (h1 : CritAdj p u₁ v) (h2 : CritAdj p u₂ v) : u₁ = u₂ := p.partnerRank_injective v (h1.2.trans h2.2.symm) /-- `CritAdj` is irreflexive when `m ≥ 1` (being single has rank `2m + 1 ≠ 1`). -/ theorem CritAdj.irrefl {m : ℕ} (hm : 1 ≤ m) {p : CompleteProfile (2 * m + 1)} (u : Fin (2 * m + 1)) : ¬ CritAdj p u u := by intro h have := h.1 rw [p.partnerRank_self] at this omega /-- A directed cycle of length `L` in a relation `R` restricted to `U : Finset α`. -/ def IsDirCycle {α : Type*} (R : α → α → Prop) (U : Finset α) (L : ℕ) (c : Fin L → α) : Prop := 0 < L ∧ Function.Injective c ∧ (∀ i : Fin L, c i ∈ U) ∧ ∀ i : Fin L, R (c i) (c ⟨(i.val + 1) % L, Nat.mod_lt _ (Nat.lt_of_le_of_lt (Nat.zero_le _) i.isLt)⟩) /-- **Functional-Injective Digraph Covering Lemma**: Let `R` be an irreflexive relation on a finite type `α` with out-degree ≤ 1 and in-degree ≤ 1. If `R` restricted to `U : Finset α` has NO odd directed cycle, then there exists a matching `μ : Matching α` supported on `R`-edges within `U` that matches every vertex of `U` having an `R`-predecessor in `U`. -/ theorem exists_matching_covering_inDeg_of_no_odd_cycle {α : Type*} [Fintype α] [DecidableEq α] (R : α → α → Prop) (hirr : ∀ x, ¬ R x x) (hout : ∀ x y₁ y₂, R x y₁ → R x y₂ → y₁ = y₂) (hin : ∀ x₁ x₂ y, R x₁ y → R x₂ y → x₁ = x₂) : ∀ (U : Finset α), (∀ L c, IsDirCycle R U L c → Even L) → ∃ μ : Matching α, (∀ x, μ.val x ≠ x → x ∈ U ∧ (R x (μ.val x) ∨ R (μ.val x) x)) ∧ (∀ u ∈ U, ∀ v ∈ U, R u v → μ.val v ≠ v) := by classical intro U induction hcard : U.card using Nat.strong_induction_on generalizing U with | h k ih => intro hno_odd -- Case 1: No edges in U at all -> emptyMatching works! by_cases hedge : ∃ u ∈ U, ∃ v ∈ U, R u v swap · push Not at hedge refine ⟨emptyMatching α, fun x hx ↦ absurd rfl hx, fun u hu v hv huv ↦ absurd huv (hedge u hu v hv)⟩ -- Case 2: There is a sink v ∈ U (has an in-neighbor u ∈ U, but no out-neighbor in U) by_cases hsink : ∃ u ∈ U, ∃ v ∈ U, R u v ∧ ∀ w ∈ U, ¬ R v w · rcases hsink with ⟨u, huU, v, hvU, huv, hvsink⟩ have huv_ne : u ≠ v := fun h ↦ hirr u (h ▸ huv) let U' := (U.erase u).erase v have hU'_sub : U' ⊆ U := fun x hx ↦ Finset.mem_of_mem_erase (Finset.mem_of_mem_erase hx) have hv_in : v ∈ U.erase u := Finset.mem_erase.2 ⟨huv_ne.symm, hvU⟩ have hU'_card : U'.card < U.card := by have h1 : U'.card + 1 = (U.erase u).card := Finset.card_erase_add_one hv_in have h2 : (U.erase u).card + 1 = U.card := Finset.card_erase_add_one huU omega have hno_odd' : ∀ L c, IsDirCycle R U' L c → Even L := by intro L c ⟨hL, hinj, hmem, hstep⟩ exact hno_odd L c ⟨hL, hinj, fun i ↦ hU'_sub (hmem i), hstep⟩ obtain ⟨μ', hμ'_supp, hμ'_cov⟩ := ih U'.card (by omega) U' rfl hno_odd' have hu_fix : μ'.val u = u := by by_contra hne have hu' := (hμ'_supp u hne).1 simp [U'] at hu' have hv_fix : μ'.val v = v := by by_contra hne have hv' := (hμ'_supp v hne).1 simp [U'] at hv' let μ := μ'.addPair hu_fix hv_fix refine ⟨μ, ?_, ?_⟩ · intro x hx by_cases hxu : x = u · subst hxu rw [μ'.addPair_apply_left hu_fix hv_fix] exact ⟨huU, Or.inl huv⟩ · by_cases hxv : x = v · subst hxv rw [μ'.addPair_apply_right hu_fix hv_fix] exact ⟨hvU, Or.inr huv⟩ · rw [μ'.addPair_apply_other hu_fix hv_fix hxu hxv] at hx ⊢ obtain ⟨hxU', hR⟩ := hμ'_supp x hx exact ⟨hU'_sub hxU', hR⟩ · intro a haU b hbU hab by_cases hbu : b = u · subst hbu rw [μ'.addPair_apply_left hu_fix hv_fix] exact huv_ne.symm · by_cases hbv : b = v · subst hbv rw [μ'.addPair_apply_right hu_fix hv_fix] exact huv_ne · have ha_ne_u : a ≠ u := fun h ↦ hbv (hout u b v (h ▸ hab) huv) have ha_ne_v : a ≠ v := fun h ↦ hvsink b hbU (h ▸ hab) have haU' : a ∈ U' := Finset.mem_erase.2 ⟨ha_ne_v, Finset.mem_erase.2 ⟨ha_ne_u, haU⟩⟩ have hbU' : b ∈ U' := Finset.mem_erase.2 ⟨hbv, Finset.mem_erase.2 ⟨hbu, hbU⟩⟩ rw [μ'.addPair_apply_other hu_fix hv_fix hbu hbv] exact hμ'_cov a haU' b hbU' hab · -- Case 3: Every vertex with an in-neighbor in U ALSO has an out-neighbor in U! -- Therefore every vertex in V₁ = {x ∈ U | ∃ u ∈ U, R u x} has a unique successor in V₁, -- forming a bijection on V₁; pick any x₀ ∈ V₁ and trace its directed cycle! push Not at hsink rcases hedge with ⟨u₀, hu₀U, x₀, hx₀U, hu₀x₀⟩ have hsucc : ∀ x ∈ U, (∃ u ∈ U, R u x) → ∃! y, y ∈ U ∧ R x y := by intro x hx ⟨u, hu, hux⟩ obtain ⟨y, hy, hxy⟩ := hsink u hu x hx hux exact ⟨y, ⟨hy, hxy⟩, fun z ⟨_, hxz⟩ ↦ hout x z y hxz hxy⟩ -- Define forward walk `f : ℕ → α` starting at `x₀` let nextV : α → α := fun x ↦ if h : ∃ y, y ∈ U ∧ R x y then Classical.choose h else x have hnextV : ∀ x ∈ U, (∃ u ∈ U, R u x) → nextV x ∈ U ∧ R x (nextV x) := by intro x hx hin_x have hex : ∃ y, y ∈ U ∧ R x y := by obtain ⟨y, hy, _⟩ := hsucc x hx hin_x exact ⟨y, hy⟩ simp only [nextV, hex, ↓reduceDIte] exact Classical.choose_spec hex let walk : ℕ → α := fun t ↦ nextV^[t] x₀ have hwalk_prop : ∀ t : ℕ, walk t ∈ U ∧ (∃ u ∈ U, R u (walk t)) ∧ R (walk t) (walk (t + 1)) := by intro t induction t with | zero => have h0 : walk 0 = x₀ := rfl rw [h0] have hstep := hnextV x₀ hx₀U ⟨u₀, hu₀U, hu₀x₀⟩ exact ⟨hx₀U, ⟨u₀, hu₀U, hu₀x₀⟩, hstep.2⟩ | succ t ih => rcases ih with ⟨htU, htin, htR⟩ have ht1 : walk (t + 1) = nextV (walk t) := Function.iterate_succ_apply' nextV t x₀ have hmem : walk (t + 1) ∈ U := by rw [ht1]; exact (hnextV (walk t) htU htin).1 have hin1 : ∃ u ∈ U, R u (walk (t + 1)) := ⟨walk t, htU, htR⟩ have hstep1 := hnextV (walk (t + 1)) hmem hin1 have ht2 : walk (t + 2) = nextV (walk (t + 1)) := Function.iterate_succ_apply' nextV (t + 1) x₀ rw [ht2] exact ⟨hmem, hin1, hstep1.2⟩ -- By Pigeonhole on `U`, `walk` repeats; let `L` be the least positive period! have hrep : ∃ i j : ℕ, i < j ∧ walk i = walk j := by have hcard_lt : Fintype.card {x // x ∈ U} < Fintype.card (Fin (U.card + 1)) := by simp obtain ⟨a, b, hab, heq⟩ := Fintype.exists_ne_map_eq_of_card_lt (fun t : Fin (U.card + 1) ↦ (⟨walk t.val, (hwalk_prop t.val).1⟩ : {x // x ∈ U})) hcard_lt have hval : walk a.val = walk b.val := congrArg Subtype.val heq rcases lt_or_gt_of_ne (Fin.val_ne_of_ne hab) with hlt | hgt · exact ⟨a.val, b.val, hlt, hval⟩ · exact ⟨b.val, a.val, hgt, hval.symm⟩ -- Because `R` has in-degree ≤ 1, `walk i = walk j` with `i < j` implies `walk 0 = walk (j - i)`! have hret0 : ∃ L : ℕ, 0 < L ∧ walk L = walk 0 := by rcases hrep with ⟨i, j, hij, heq⟩ refine ⟨j - i, by omega, ?_⟩ -- Prove by induction on `i` that `walk (i + d) = walk i → walk d = walk 0` have hshift : ∀ s d, walk (s + d) = walk s → walk d = walk 0 := by intro s d induction s with | zero => simp | succ s ihs => intro h apply ihs have h1 : R (walk (s + d)) (walk (s + 1 + d)) := by have : s + 1 + d = (s + d) + 1 := by omega rw [this]; exact (hwalk_prop (s + d)).2.2 have h2 : R (walk s) (walk (s + 1)) := (hwalk_prop s).2.2 rw [h] at h1 exact hin _ _ _ h1 h2 have hji : i + (j - i) = j := by omega exact hshift i (j - i) (by rw [hji]; exact heq.symm) let L := Nat.find hret0 have hL_spec : 0 < L ∧ walk L = walk 0 := Nat.find_spec hret0 have hL_min : ∀ d, 0 < d → d < L → walk d ≠ walk 0 := by intro d hd hlt exact fun h ↦ Nat.find_min hret0 hlt ⟨hd, h⟩ let cyc : Fin L → α := fun i ↦ walk i.val have hcyc_inj : Function.Injective cyc := by have hshift : ∀ s d, walk (s + d) = walk s → walk d = walk 0 := by intro s d induction s with | zero => simp | succ s ihs => intro h apply ihs have h1 : R (walk (s + d)) (walk (s + 1 + d)) := by have : s + 1 + d = (s + d) + 1 := by omega rw [this]; exact (hwalk_prop (s + d)).2.2 have h2 : R (walk s) (walk (s + 1)) := (hwalk_prop s).2.2 rw [h] at h1 exact hin _ _ _ h1 h2 intro a b hab ext by_contra hne rcases lt_or_gt_of_ne hne with hlt | hlt · have hba : a.val + (b.val - a.val) = b.val := by omega exact hL_min (b.val - a.val) (by omega) (by omega) (hshift a.val (b.val - a.val) (by rw [hba]; exact hab.symm)) · have hab' : b.val + (a.val - b.val) = a.val := by omega exact hL_min (a.val - b.val) (by omega) (by omega) (hshift b.val (a.val - b.val) (by rw [hab']; exact hab)) have hcyc_is : IsDirCycle R U L cyc := by refine ⟨hL_spec.1, hcyc_inj, fun i ↦ (hwalk_prop i.val).1, fun i ↦ ?_⟩ by_cases hlast : i.val + 1 = L · have hmod : (i.val + 1) % L = 0 := by rw [hlast, Nat.mod_self] change R (walk i.val) (walk ((i.val + 1) % L)) rw [hmod, ← hL_spec.2] have hstep := (hwalk_prop i.val).2.2 rwa [hlast] at hstep · have hmod : (i.val + 1) % L = i.val + 1 := Nat.mod_eq_of_lt (by omega) change R (walk i.val) (walk ((i.val + 1) % L)) rw [hmod] exact (hwalk_prop i.val).2.2 have hL_even : Even L := hno_odd L cyc hcyc_is rcases hL_even with ⟨r, hr⟩ have hr_pos : 0 < r := by omega -- Delete the cycle `C = image cyc` from `U` and induct! let C : Finset α := Finset.univ.image cyc have hC_sub : C ⊆ U := by intro x hx rcases Finset.mem_image.1 hx with ⟨i, _, rfl⟩ exact hcyc_is.2.2.1 i have hC_nonempty : C.Nonempty := ⟨cyc ⟨0, hL_spec.1⟩, Finset.mem_image_of_mem _ (Finset.mem_univ _)⟩ let U' := U \ C have hU'_card : U'.card < U.card := by rw [Finset.card_sdiff_of_subset hC_sub] have hpos : 0 < C.card := Finset.card_pos.2 hC_nonempty have hle : C.card ≤ U.card := Finset.card_le_card hC_sub omega have hno_odd' : ∀ M c, IsDirCycle R U' M c → Even M := by intro M c ⟨hM, hinj, hmem, hstep⟩ exact hno_odd M c ⟨hM, hinj, fun i ↦ (Finset.mem_sdiff.1 (hmem i)).1, hstep⟩ obtain ⟨μ', hμ'_supp, hμ'_cov⟩ := ih U'.card (by omega) U' rfl hno_odd' -- Build the alternating matching on the even cycle `cyc (2*s) ↔ cyc (2*s + 1)` -- and combine it with `μ'` on `U' = U \ C`! have hcyc_R : ∀ i : Fin L, R (cyc i) (cyc ⟨(i.val + 1) % L, Nat.mod_lt _ hL_spec.1⟩) := hcyc_is.2.2.2 let f_cyc : α → α := fun x ↦ if hx : x ∈ C then let i := Classical.choose (Finset.mem_image.1 hx) if he : i.val % 2 = 0 then cyc ⟨i.val + 1, by have := i.isLt; omega⟩ else cyc ⟨i.val - 1, by have := i.isLt; omega⟩ else μ'.val x have hf_cyc_on : ∀ i : Fin L, f_cyc (cyc i) = if he : i.val % 2 = 0 then cyc ⟨i.val + 1, by have := i.isLt; omega⟩ else cyc ⟨i.val - 1, by have := i.isLt; omega⟩ := by intro i have hiC : cyc i ∈ C := Finset.mem_image_of_mem cyc (Finset.mem_univ i) simp only [f_cyc, hiC, ↓reduceDIte] have hch : Classical.choose (Finset.mem_image.1 hiC) = i := hcyc_inj (Classical.choose_spec (Finset.mem_image.1 hiC)).2 simp only [hch] have hf_cyc_off : ∀ x, x ∉ C → f_cyc x = μ'.val x := by intro x hx; simp [f_cyc, hx] have hf_inv : Function.Involutive f_cyc := by intro x by_cases hx : x ∈ C · rcases Finset.mem_image.1 hx with ⟨i, _, rfl⟩ rw [hf_cyc_on i] split_ifs with he · rw [hf_cyc_on ⟨i.val + 1, by have := i.isLt; omega⟩] have ho : ¬ ((i.val + 1) % 2 = 0) := by omega simp only [ho, ↓reduceDIte] exact congrArg cyc (Fin.ext rfl) · rw [hf_cyc_on ⟨i.val - 1, by have := i.isLt; omega⟩] have he' : (i.val - 1) % 2 = 0 := by omega simp only [he', ↓reduceDIte] exact congrArg cyc (Fin.ext (by dsimp; omega)) · rw [hf_cyc_off x hx] have hμx_notC : μ'.val x ∉ C := by intro hmem by_cases hfix : μ'.val x = x · exact hx (hfix ▸ hmem) · have hxU' := (hμ'_supp x hfix).1 have hμxU' := (hμ'_supp (μ'.val x) (by rw [μ'.property x]; exact Ne.symm hfix)).1 exact (Finset.mem_sdiff.1 hμxU').2 hmem rw [hf_cyc_off (μ'.val x) hμx_notC, μ'.property x] let μ : Matching α := ⟨hf_inv.toPerm, hf_inv⟩ refine ⟨μ, ?_, ?_⟩ · intro x hx by_cases hxC : x ∈ C · rcases Finset.mem_image.1 hxC with ⟨i, _, rfl⟩ refine ⟨hC_sub hxC, ?_⟩ change R (cyc i) (f_cyc (cyc i)) ∨ R (f_cyc (cyc i)) (cyc i) rw [hf_cyc_on i] split_ifs with he · left have hstep := hcyc_R i have hmod : (i.val + 1) % L = i.val + 1 := Nat.mod_eq_of_lt (by omega) simpa [hmod] using hstep · right have hstep := hcyc_R ⟨i.val - 1, by omega⟩ have hmod : (i.val - 1 + 1) % L = i.val := by rw [Nat.sub_add_cancel (by omega)] exact Nat.mod_eq_of_lt i.isLt simpa [hmod] using hstep · change f_cyc x ≠ x at hx rw [hf_cyc_off x hxC] at hx change x ∈ U ∧ (R x (f_cyc x) ∨ R (f_cyc x) x) rw [hf_cyc_off x hxC] obtain ⟨hxU', hR⟩ := hμ'_supp x hx exact ⟨(Finset.mem_sdiff.1 hxU').1, hR⟩ · intro a haU b hbU hab by_cases hbC : b ∈ C · rcases Finset.mem_image.1 hbC with ⟨i, _, rfl⟩ change f_cyc (cyc i) ≠ cyc i rw [hf_cyc_on i] split_ifs with he · intro h have := congrArg Fin.val (hcyc_inj h) dsimp at this; omega · intro h have := congrArg Fin.val (hcyc_inj h) dsimp at this; omega · -- b ∉ C: also a ∉ C (since every vertex of C has its unique out-neighbor in C!) have ha_notC : a ∉ C := by intro haC rcases Finset.mem_image.1 haC with ⟨i, _, rfl⟩ have hstep := hcyc_R i have hbeq := hout (cyc i) b _ hab hstep exact hbC (hbeq ▸ Finset.mem_image_of_mem cyc (Finset.mem_univ _)) change f_cyc b ≠ b rw [hf_cyc_off b hbC] exact hμ'_cov a (Finset.mem_sdiff.2 ⟨haU, ha_notC⟩) b (Finset.mem_sdiff.2 ⟨hbU, hbC⟩) hab /-- **Theorem 6 (Structural Sub-$\lceil N / 2 \rceil$ Upper Bound for Odd $N = 2m + 1$)**: Let $N = 2m + 1$ ($m \ge 1$) and $p$ be a complete profile on `Fin (2 * m + 1)`. Suppose there exists a maximal matching $\mu_0$ of $G_m$ (every matched pair has both ranks $\le m$, and no two unmatched agents have both ranks $\le m$) such that the critical digraph `CritAdj p` restricted to the unmatched agents $U = \{x \mid \mu_0(x) = x\}$ has **no odd directed cycle**. Then $p$ admits an **$m$-stable matching** (strictly beating $\lceil N / 2 \rceil = m + 1$). -/ theorem exists_m_stable_of_no_odd_critical_cycle {m : ℕ} (hm : 1 ≤ m) (p : CompleteProfile (2 * m + 1)) (μ₀ : Matching (Fin (2 * m + 1))) (hμ₀_bnd : IsRankBounded p m μ₀) (hμ₀_max : ∀ i j, i ≠ j → μ₀.val i = i → μ₀.val j = j → ¬ (p.partnerRank i j ≤ m ∧ p.partnerRank j i ≤ m)) (hno_odd : ∀ L c, IsDirCycle (CritAdj p) (Finset.univ.filter fun x ↦ μ₀.val x = x) L c → Even L) : ∃ μ : Matching (Fin (2 * m + 1)), IsKStable ⊤ p.partnerRank m μ := by classical let U : Finset (Fin (2 * m + 1)) := Finset.univ.filter fun x ↦ μ₀.val x = x -- Step 1: Cover every in-degree-1 vertex of `CritAdj p` in `U` with a matching `μ_crit` on `U`. obtain ⟨μ_crit, hcrit_supp, hcrit_cov⟩ := exists_matching_covering_inDeg_of_no_odd_cycle (CritAdj p) (CritAdj.irrefl hm) (fun _ _ _ h1 h2 ↦ h1.out_unique h2) (fun _ _ _ h1 h2 ↦ h1.in_unique h2) U hno_odd -- Step 2: Among all matchings `ν` supported inside `U` that: -- (a) only use edges of G_{m+1} (both ranks ≤ m + 1), and -- (b) cover every vertex of U with a CritAdj-predecessor in U, -- pick one maximizing `ν.matchedSet.card`. let Good : Matching (Fin (2 * m + 1)) → Prop := fun ν ↦ (∀ x, ν.val x ≠ x → x ∈ U ∧ p.partnerRank x (ν.val x) ≤ m + 1) ∧ (∀ u ∈ U, ∀ v ∈ U, CritAdj p u v → ν.val v ≠ v) have hcrit_good : Good μ_crit := by refine ⟨?_, hcrit_cov⟩ intro x hx obtain ⟨hxU, hR⟩ := hcrit_supp x hx refine ⟨hxU, ?_⟩ rcases hR with h | h · have := h.1; omega · have := h.2; omega let S : Finset (Matching (Fin (2 * m + 1))) := Finset.univ.filter Good have hcrit_mem : μ_crit ∈ S := Finset.mem_filter.2 ⟨Finset.mem_univ _, hcrit_good⟩ obtain ⟨μ_H, hμH_mem, hμH_max⟩ := Finset.exists_max_image S (fun ν ↦ ν.matchedSet.card) ⟨_, hcrit_mem⟩ obtain ⟨hH_supp, hH_cov⟩ := (Finset.mem_filter.1 hμH_mem).2 -- Step 3: Combine `μ₀` (on `Uᶜ`) and `μ_H` (on `U`) into `μ`! let f : Fin (2 * m + 1) → Fin (2 * m + 1) := fun x ↦ if x ∈ U then μ_H.val x else μ₀.val x have hH_closed : ∀ x ∈ U, μ_H.val x ∈ U := by intro x hxU by_cases hfix : μ_H.val x = x · rwa [hfix] · exact (hH_supp (μ_H.val x) (by rw [μ_H.property x]; exact Ne.symm hfix)).1 have h0_closed : ∀ x, x ∉ U → μ₀.val x ∉ U := by intro x hxU hmem have hfix : μ₀.val (μ₀.val x) = μ₀.val x := (Finset.mem_filter.1 hmem).2 rw [μ₀.property x] at hfix exact hxU (Finset.mem_filter.2 ⟨Finset.mem_univ _, hfix.symm⟩) have hf_inv : Function.Involutive f := by intro x by_cases hxU : x ∈ U · simp only [f, hxU, ↓reduceIte, hH_closed x hxU, μ_H.property x] · simp only [f, hxU, ↓reduceIte, h0_closed x hxU, μ₀.property x] let μ : Matching (Fin (2 * m + 1)) := ⟨hf_inv.toPerm, hf_inv⟩ refine ⟨μ, isAcceptable_top μ, ?_⟩ intro i j ⟨hadj, hne_ij, hi_imp, hj_imp⟩ have hij : i ≠ j := (SimpleGraph.top_adj _ _).1 hadj -- Any agent in `Uᶜ` (matched in `μ₀`) has rank ≤ m, so cannot gain `m`! have hiU : i ∈ U := by by_contra hi have hμi : μ.val i = μ₀.val i := by simp [μ, f, hi] have hne0 : μ₀.val i ≠ i := by intro hfix; exact hi (Finset.mem_filter.2 ⟨Finset.mem_univ _, hfix⟩) have hbnd := hμ₀_bnd i hne0 have hpos := p.partnerRank_pos i j rw [hμi] at hi_imp omega have hjU : j ∈ U := by by_contra hj have hμj : μ.val j = μ₀.val j := by simp [μ, f, hj] have hne0 : μ₀.val j ≠ j := by intro hfix; exact hj (Finset.mem_filter.2 ⟨Finset.mem_univ _, hfix⟩) have hbnd := hμ₀_bnd j hne0 have hpos := p.partnerRank_pos j i rw [hμj] at hj_imp omega have hμi : μ.val i = μ_H.val i := by simp [μ, f, hiU] have hμj : μ.val j = μ_H.val j := by simp [μ, f, hjU] rw [hμi] at hi_imp rw [hμj] at hj_imp -- Split on whether i and j are matched in `μ_H` by_cases hi_fix : μ_H.val i = i · by_cases hj_fix : μ_H.val j = j · -- Both single in μ_H: both ranks ≤ m + 1, contradicting maximality of `μ_H`! rw [hi_fix, p.partnerRank_self] at hi_imp rw [hj_fix, p.partnerRank_self] at hj_imp let ν := μ_H.addPair hi_fix hj_fix have hν_good : Good ν := by refine ⟨?_, ?_⟩ · intro x hx by_cases hxi : x = i · subst hxi rw [μ_H.addPair_apply_left hi_fix hj_fix] exact ⟨hiU, by omega⟩ · by_cases hxj : x = j · subst hxj rw [μ_H.addPair_apply_right hi_fix hj_fix] exact ⟨hjU, by omega⟩ · rw [μ_H.addPair_apply_other hi_fix hj_fix hxi hxj] at hx ⊢ exact hH_supp x hx · intro u hu v hv huv by_cases hvi : v = i · subst hvi rw [μ_H.addPair_apply_left hi_fix hj_fix] exact hij.symm · by_cases hvj : v = j · subst hvj rw [μ_H.addPair_apply_right hi_fix hj_fix] exact hij · rw [μ_H.addPair_apply_other hi_fix hj_fix hvi hvj] exact hH_cov u hu v hv huv have hν_mem : ν ∈ S := Finset.mem_filter.2 ⟨Finset.mem_univ _, hν_good⟩ have hlt := μ_H.card_matchedSet_lt_addPair hij hi_fix hj_fix have hle : (μ_H.addPair hi_fix hj_fix).matchedSet.card ≤ μ_H.matchedSet.card := hμH_max ν hν_mem omega · -- i single, j matched in μ_H: rk(j, μ_H j) ≤ m+1 => rk(j, i) = 1! -- Since i, j ∈ U are both single in μ₀, by G_m-maximality rk(i, j) ≥ m + 1; -- and i single gives rk(i, j) + m ≤ 2m + 1 => rk(i, j) = m + 1! -- Thus CritAdj p j i holds, contradicting hH_cov (which matched i)! have hj_bnd := (hH_supp j hj_fix).2 have hj_pos := p.partnerRank_pos j i have hrk_ji : p.partnerRank j i = 1 := by omega rw [hi_fix, p.partnerRank_self] at hi_imp have hnot_both := hμ₀_max i j hij (Finset.mem_filter.1 hiU).2 (Finset.mem_filter.1 hjU).2 have hrk_ij : p.partnerRank i j = m + 1 := by omega exact (hH_cov j hjU i hiU ⟨hrk_ji, hrk_ij⟩) hi_fix · by_cases hj_fix : μ_H.val j = j · -- Symmetric: i matched, j single in μ_H => CritAdj p i j holds, contradicting hH_cov! have hi_bnd := (hH_supp i hi_fix).2 have hi_pos := p.partnerRank_pos i j have hrk_ij : p.partnerRank i j = 1 := by omega rw [hj_fix, p.partnerRank_self] at hj_imp have hnot_both := hμ₀_max i j hij (Finset.mem_filter.1 hiU).2 (Finset.mem_filter.1 hjU).2 have hrk_ji : p.partnerRank j i = m + 1 := by omega exact (hH_cov i hiU j hjU ⟨hrk_ij, hrk_ji⟩) hj_fix · -- Both i and j matched in μ_H: both have rk(i, j) = 1 and rk(j, i) = 1 ≤ m, -- contradicting G_m-maximality of μ₀! have hi_bnd := (hH_supp i hi_fix).2 have hj_bnd := (hH_supp j hj_fix).2 have hi_pos := p.partnerRank_pos i j have hj_pos := p.partnerRank_pos j i exact hμ₀_max i j hij (Finset.mem_filter.1 hiU).2 (Finset.mem_filter.1 hjU).2 ⟨by omega, by omega⟩ end Roommates /-! ## Section 8: Axiom Audit -/ #print axioms Roommates.stabilityThreshold_le_half #print axioms Roommates.stabilityThreshold_mono #print axioms Roommates.stabilityThreshold_monotone #print axioms Roommates.stabilityThreshold_three #print axioms Roommates.stabilityThreshold_four #print axioms Roommates.not_isKStable_two_profileFive #print axioms Roommates.stabilityThreshold_five #print axioms Roommates.stabilityThreshold_six #print axioms Roommates.not_isKStable_of_shiftInvariant #print axioms Roommates.one_step_shift_invariant_ceiling #print axioms Roommates.stabilityThreshold_ge_of_odd_two_fifths #print axioms Roommates.stabilityThreshold_ge_two_fifths_odd #print axioms Roommates.stabilityThreshold_linear_lower_bound #print axioms Roommates.stabilityThreshold_sandwich #print axioms Roommates.exists_m_stable_of_no_odd_critical_cycle