import Mathlib /-! # Exact Elder Rule for H₀ Merge Trees, Kruskal MST Correspondence & Sharp p-Wasserstein Distances This standalone module formalizes: 1. **Filtered Finite Graphs, Exact H₀ Persistent Homology & the Elder Rule**: For any connected finite filtered graph `G` with vertex birth times `birth : V → ℝ` and nondecreasing edge weights `wt : Fin m → ℝ` (`max(birth u, birth v) ≤ wt e`): - Kruskal's incremental component-merge process selects exactly `|V| - 1` tree edges; - Every Kruskal merge of two components with oldest vertices `r₁ < r₂` (in birth order) kills the younger component `r₂` at `wt e` (producing exact bar `[birth r₂, wt e)`) while the older component `r₁` survives; - Exact persistence-module isomorphism `H_0(G; K) ≅ 𝕀[birth r_min, ∞) ⊕ ⨁_{e ∈ Kruskal(G)} 𝕀[birth(youngerRoot e), wt e)` verified at every filtration level `t ∈ ℝ` via triangular Elder-Rule basis vectors; - Exact **Kruskal MST Optimality** (for arbitrary real weights via prefix-component counting and Abel summation by parts) and exact **Persistence–MST Weight Identity**: `∑_{e ∈ Kruskal} (wt e - birth(youngerRoot e)) = wt(MST) - ∑_{v ≠ r_min} birth v`. 2. **Sharp `p`-Wasserstein Persistence Distances & Stability Separations**: - Exact `p`-Wasserstein persistence distance `W_p(D₁, D₂)` (`p ≥ 1`) and `W_∞ = d_B(D₁, D₂)`, attained by finite optimal partial matchings; - Full metric axioms: non-negativity, identity of indiscernibles (up to bar multiset equality), symmetry, and exact **triangle inequality** via matching composition + Minkowski's inequality; - Sharp monotonicity hierarchy `d_B ≤ W_q ≤ W_p` (`1 ≤ p ≤ q ≤ ∞`), reverse cardinality bound `W_p(D₁, D₂) ≤ (|D₁| + |D₂|)^{1/p} d_B(D₁, D₂)`, and exact `N`-bar separation witnesses attaining `W_p = N^{1/p} d_B` and `W_p = N^{1/p - 1/q} W_q`; - Exact **`W_1`-vs.-`d_B` Stability Separation on Filtered Complexes**: on the alternating valley–peak path `P_{2N+1}` with `‖f - 0‖_∞ = ε`, the Elder Rule produces exactly `N` positive bars `[-ε, +ε)`, giving `d_B = ε` versus `W_1 = N · ε` and `W_p = N^{1/p} · ε`. -/ open scoped BigOperators namespace OpenTDAPersistence.ElderRuleWasserstein universe u /-! ### Part 1: Filtered Finite Graphs, Connected Components & Elder Order -/ /-- A finite filtered graph (simplicial 1-complex) on a finite linearly ordered vertex set `V` with vertex birth times `birth` and `m` undirected edges ordered by nondecreasing filtration time. -/ structure FilteredGraph (V : Type*) [Fintype V] [LinearOrder V] where birth : V → ℝ m : ℕ src : Fin m → V dst : Fin m → V wt : Fin m → ℝ hbirth_src : ∀ e : Fin m, birth (src e) ≤ wt e hbirth_dst : ∀ e : Fin m, birth (dst e) ≤ wt e hsorted : Monotone wt namespace FilteredGraph variable {V : Type*} [Fintype V] [LinearOrder V] /-- Strict Elder order on vertices: `u` is strictly older than `v` iff `birth u < birth v`, breaking ties deterministically by `u < v`. -/ def older (G : FilteredGraph V) (u v : V) : Prop := G.birth u < G.birth v ∨ (G.birth u = G.birth v ∧ u < v) noncomputable instance decidableOlder (G : FilteredGraph V) : DecidableRel G.older := by classical infer_instance lemma older_irrefl (G : FilteredGraph V) (u : V) : ¬ G.older u u := by rintro (h | ⟨_, h⟩) <;> exact lt_irrefl _ h lemma older_trans (G : FilteredGraph V) {u v w : V} (h1 : G.older u v) (h2 : G.older v w) : G.older u w := by rcases h1 with h1 | ⟨e1, l1⟩ <;> rcases h2 with h2 | ⟨e2, l2⟩ · exact Or.inl (h1.trans h2) · exact Or.inl (by linarith) · exact Or.inl (by linarith) · exact Or.inr ⟨e1.trans e2, l1.trans l2⟩ lemma older_asymm (G : FilteredGraph V) {u v : V} (h : G.older u v) : ¬ G.older v u := fun h' => G.older_irrefl u (G.older_trans h h') lemma older_trichotomy (G : FilteredGraph V) (u v : V) : G.older u v ∨ u = v ∨ G.older v u := by rcases lt_trichotomy (G.birth u) (G.birth v) with hlt | heq | hgt · exact Or.inl (Or.inl hlt) · rcases lt_trichotomy u v with hlt' | heq' | hgt' · exact Or.inl (Or.inr ⟨heq, hlt'⟩) · exact Or.inr (Or.inl heq') · exact Or.inr (Or.inr (Or.inr ⟨heq.symm, hgt'⟩)) · exact Or.inr (Or.inr (Or.inl hgt)) lemma birth_le_of_older (G : FilteredGraph V) {u v : V} (h : G.older u v) : G.birth u ≤ G.birth v := by rcases h with hlt | ⟨heq, _⟩ · exact hlt.le · exact heq.le /-- Any nonempty finite set of vertices has a unique oldest vertex under `G.older`. -/ lemma exists_oldest (G : FilteredGraph V) (S : Finset V) (hS : S.Nonempty) : ∃ r ∈ S, ∀ v ∈ S, v = r ∨ G.older r v := by induction S using Finset.induction_on with | empty => exact absurd hS Finset.not_nonempty_empty | @insert a s ha ih => by_cases hs : s.Nonempty · obtain ⟨r, hr_mem, hr_min⟩ := ih hs rcases G.older_trichotomy a r with har | rfl | hra · refine ⟨a, Finset.mem_insert_self a s, fun v hv => ?_⟩ rcases Finset.mem_insert.mp hv with rfl | hvs · exact Or.inl rfl · rcases hr_min v hvs with rfl | hrv · exact Or.inr har · exact Or.inr (G.older_trans har hrv) · exact absurd hr_mem ha · refine ⟨r, Finset.mem_insert_of_mem hr_mem, fun v hv => ?_⟩ rcases Finset.mem_insert.mp hv with rfl | hvs · exact Or.inr hra · exact hr_min v hvs · rw [Finset.not_nonempty_iff_eq_empty] at hs subst hs exact ⟨a, Finset.mem_singleton_self a, fun v hv => Or.inl (Finset.mem_singleton.mp hv)⟩ /-- The unique oldest vertex of a nonempty vertex subset `S`. -/ noncomputable def oldestOf (G : FilteredGraph V) (S : Finset V) (hS : S.Nonempty) : V := Classical.choose (G.exists_oldest S hS) lemma oldestOf_mem (G : FilteredGraph V) (S : Finset V) (hS : S.Nonempty) : G.oldestOf S hS ∈ S := (Classical.choose_spec (G.exists_oldest S hS)).1 lemma oldestOf_le (G : FilteredGraph V) (S : Finset V) (hS : S.Nonempty) {v : V} (hv : v ∈ S) : v = G.oldestOf S hS ∨ G.older (G.oldestOf S hS) v := (Classical.choose_spec (G.exists_oldest S hS)).2 v hv lemma oldestOf_unique (G : FilteredGraph V) {S : Finset V} (hS : S.Nonempty) {r : V} (hr : r ∈ S) (hmin : ∀ v ∈ S, v = r ∨ G.older r v) : r = G.oldestOf S hS := by rcases hmin (G.oldestOf S hS) (G.oldestOf_mem S hS) with h1 | h1 · exact h1.symm · rcases G.oldestOf_le S hS hr with h2 | h2 · exact h2 · exact absurd h2 (G.older_asymm h1) /-- The global oldest vertex of `G` (`[Nonempty V]`). -/ noncomputable def globalElder (G : FilteredGraph V) [Nonempty V] : V := G.oldestOf Finset.univ Finset.univ_nonempty lemma globalElder_le (G : FilteredGraph V) [Nonempty V] (v : V) : v = G.globalElder ∨ G.older G.globalElder v := G.oldestOf_le Finset.univ Finset.univ_nonempty (Finset.mem_univ v) /-! ### Reachability via Edge Subsets and Prefix Steps -/ /-- One-step undirected adjacency using an edge subset `T ⊆ Fin G.m`. -/ def EdgeSubsetAdj (G : FilteredGraph V) (T : Finset (Fin G.m)) (u v : V) : Prop := ∃ e ∈ T, (G.src e = u ∧ G.dst e = v) ∨ (G.src e = v ∧ G.dst e = u) /-- Connectedness relation on `V` using edges in `T`. -/ def ReachIn (G : FilteredGraph V) (T : Finset (Fin G.m)) (u v : V) : Prop := Relation.ReflTransGen (G.EdgeSubsetAdj T) u v lemma reachIn_refl (G : FilteredGraph V) (T : Finset (Fin G.m)) (u : V) : G.ReachIn T u u := Relation.ReflTransGen.refl lemma reachIn_symm (G : FilteredGraph V) {T : Finset (Fin G.m)} {u v : V} (h : G.ReachIn T u v) : G.ReachIn T v u := by induction h with | refl => exact Relation.ReflTransGen.refl | tail _ hstep ih => rcases hstep with ⟨e, he, ⟨h1, h2⟩ | ⟨h1, h2⟩⟩ · exact Relation.ReflTransGen.head ⟨e, he, Or.inr ⟨h1, h2⟩⟩ ih · exact Relation.ReflTransGen.head ⟨e, he, Or.inl ⟨h1, h2⟩⟩ ih lemma reachIn_trans (G : FilteredGraph V) {T : Finset (Fin G.m)} {u v w : V} (h1 : G.ReachIn T u v) (h2 : G.ReachIn T v w) : G.ReachIn T u w := Relation.ReflTransGen.trans h1 h2 lemma reachIn_mono (G : FilteredGraph V) {T₁ T₂ : Finset (Fin G.m)} (hT : T₁ ⊆ T₂) {u v : V} (h : G.ReachIn T₁ u v) : G.ReachIn T₂ u v := by induction h with | refl => exact Relation.ReflTransGen.refl | tail _ hstep ih => rcases hstep with ⟨e, he, hdir⟩ exact Relation.ReflTransGen.tail ih ⟨e, hT he, hdir⟩ /-- The prefix edge set `{e : Fin G.m | e.val < k}` (for `k : ℕ`). -/ def prefixEdges (G : FilteredGraph V) (k : ℕ) : Finset (Fin G.m) := Finset.univ.filter fun e : Fin G.m => e.val < k /-- Connectedness of `G` using all `m` edges. -/ def IsConnected (G : FilteredGraph V) : Prop := ∀ u v : V, G.ReachIn Finset.univ u v /-- Connected component of `v` using edges in `T`. -/ noncomputable def compIn (G : FilteredGraph V) (T : Finset (Fin G.m)) (v : V) : Finset V := by classical exact Finset.univ.filter fun x : V => G.ReachIn T v x lemma mem_compIn_self (G : FilteredGraph V) (T : Finset (Fin G.m)) (v : V) : v ∈ G.compIn T v := by classical simp [compIn, reachIn_refl] lemma compIn_nonempty (G : FilteredGraph V) (T : Finset (Fin G.m)) (v : V) : (G.compIn T v).Nonempty := ⟨v, G.mem_compIn_self T v⟩ lemma compIn_eq_of_reach (G : FilteredGraph V) {T : Finset (Fin G.m)} {u v : V} (h : G.ReachIn T u v) : G.compIn T u = G.compIn T v := by classical ext x simp only [compIn, Finset.mem_filter, Finset.mem_univ, true_and] exact ⟨fun hu => G.reachIn_trans (G.reachIn_symm h) hu, fun hv => G.reachIn_trans h hv⟩ /-- Oldest vertex (Elder representative) of `v`'s connected component in edge subset `T`. -/ noncomputable def rootIn (G : FilteredGraph V) (T : Finset (Fin G.m)) (v : V) : V := G.oldestOf (G.compIn T v) (G.compIn_nonempty T v) lemma rootIn_reach (G : FilteredGraph V) (T : Finset (Fin G.m)) (v : V) : G.ReachIn T v (G.rootIn T v) := by classical have hm := G.oldestOf_mem (G.compIn T v) (G.compIn_nonempty T v) simpa [rootIn, compIn] using hm lemma rootIn_le (G : FilteredGraph V) (T : Finset (Fin G.m)) {v x : V} (h : G.ReachIn T v x) : x = G.rootIn T v ∨ G.older (G.rootIn T v) x := by classical apply G.oldestOf_le (G.compIn T v) (G.compIn_nonempty T v) simpa [compIn] using h lemma rootIn_eq_of_reach (G : FilteredGraph V) {T : Finset (Fin G.m)} {u v : V} (h : G.ReachIn T u v) : G.rootIn T u = G.rootIn T v := by unfold rootIn have heq := G.compIn_eq_of_reach h apply G.oldestOf_unique (G.compIn_nonempty T v) · rw [← heq]; exact G.oldestOf_mem _ _ · intro x hx; rw [← heq] at hx; exact G.oldestOf_le _ _ hx lemma rootIn_eq_iff (G : FilteredGraph V) (T : Finset (Fin G.m)) (u v : V) : G.rootIn T u = G.rootIn T v ↔ G.ReachIn T u v := by constructor · intro heq have h1 := G.rootIn_reach T u have h2 := G.rootIn_reach T v rw [heq] at h1 exact G.reachIn_trans h1 (G.reachIn_symm h2) · exact G.rootIn_eq_of_reach /-! ### Kruskal's Algorithm & Exact Elder Rule -/ /-- Edge `e : Fin G.m` is a **Kruskal MST edge** iff its endpoints are in distinct connected components before `e` is processed. -/ def isKruskalEdge (G : FilteredGraph V) (e : Fin G.m) : Prop := ¬ G.ReachIn (G.prefixEdges e.val) (G.src e) (G.dst e) /-- The set of Kruskal MST edges of `G`. -/ noncomputable def kruskalSet (G : FilteredGraph V) : Finset (Fin G.m) := by classical exact Finset.univ.filter fun e => G.isKruskalEdge e /-- The **Elder (surviving)** component root when edge `e` merges two components. -/ noncomputable def elderRoot (G : FilteredGraph V) (e : Fin G.m) : V := if G.older (G.rootIn (G.prefixEdges e.val) (G.src e)) (G.rootIn (G.prefixEdges e.val) (G.dst e)) then G.rootIn (G.prefixEdges e.val) (G.src e) else G.rootIn (G.prefixEdges e.val) (G.dst e) /-- The **Younger (dying)** component root when edge `e` merges two components. -/ noncomputable def youngerRoot (G : FilteredGraph V) (e : Fin G.m) : V := if G.older (G.rootIn (G.prefixEdges e.val) (G.src e)) (G.rootIn (G.prefixEdges e.val) (G.dst e)) then G.rootIn (G.prefixEdges e.val) (G.dst e) else G.rootIn (G.prefixEdges e.val) (G.src e) lemma elder_older_younger (G : FilteredGraph V) {e : Fin G.m} (he : G.isKruskalEdge e) : G.older (G.elderRoot e) (G.youngerRoot e) := by unfold elderRoot youngerRoot set r1 := G.rootIn (G.prefixEdges e.val) (G.src e) set r2 := G.rootIn (G.prefixEdges e.val) (G.dst e) have hne : r1 ≠ r2 := fun h => he ((G.rootIn_eq_iff _ _ _).mp h) rcases G.older_trichotomy r1 r2 with h12 | heq | h21 · simp [h12] · exact absurd heq hne · simp [G.older_asymm h21, h21] lemma birth_elder_le_younger (G : FilteredGraph V) {e : Fin G.m} (he : G.isKruskalEdge e) : G.birth (G.elderRoot e) ≤ G.birth (G.youngerRoot e) := G.birth_le_of_older (G.elder_older_younger he) lemma birth_younger_le_wt (G : FilteredGraph V) (e : Fin G.m) : G.birth (G.youngerRoot e) ≤ G.wt e := by unfold youngerRoot split_ifs · have hle := G.rootIn_le (G.prefixEdges e.val) (G.reachIn_refl _ (G.dst e)) rcases hle with heq | hold · rw [← heq]; exact G.hbirth_dst e · exact (G.birth_le_of_older hold).trans (G.hbirth_dst e) · have hle := G.rootIn_le (G.prefixEdges e.val) (G.reachIn_refl _ (G.src e)) rcases hle with heq | hold · rw [← heq]; exact G.hbirth_src e · exact (G.birth_le_of_older hold).trans (G.hbirth_src e) lemma reach_after_kruskalEdge (G : FilteredGraph V) (e : Fin G.m) : G.ReachIn (G.prefixEdges (e.val + 1)) (G.elderRoot e) (G.youngerRoot e) := by have hsub : G.prefixEdges e.val ⊆ G.prefixEdges (e.val + 1) := by intro x hx; simp only [prefixEdges, Finset.mem_filter, Finset.mem_univ, true_and] at hx ⊢; omega have h1 : G.ReachIn (G.prefixEdges (e.val + 1)) (G.src e) (G.rootIn (G.prefixEdges e.val) (G.src e)) := G.reachIn_mono hsub (G.rootIn_reach _ _) have h2 : G.ReachIn (G.prefixEdges (e.val + 1)) (G.dst e) (G.rootIn (G.prefixEdges e.val) (G.dst e)) := G.reachIn_mono hsub (G.rootIn_reach _ _) have hedge : G.ReachIn (G.prefixEdges (e.val + 1)) (G.src e) (G.dst e) := Relation.ReflTransGen.single ⟨e, by simp [prefixEdges], Or.inl ⟨rfl, rfl⟩⟩ have hr12 : G.ReachIn (G.prefixEdges (e.val + 1)) (G.rootIn (G.prefixEdges e.val) (G.src e)) (G.rootIn (G.prefixEdges e.val) (G.dst e)) := G.reachIn_trans (G.reachIn_symm h1) (G.reachIn_trans hedge h2) unfold elderRoot youngerRoot split_ifs · exact hr12 · exact G.reachIn_symm hr12 /-- Key characterization: before edge `e`, `youngerRoot e` is its own component root. -/ lemma rootIn_youngerRoot_before (G : FilteredGraph V) (e : Fin G.m) : G.rootIn (G.prefixEdges e.val) (G.youngerRoot e) = G.youngerRoot e := by unfold youngerRoot split_ifs · exact (G.rootIn_eq_of_reach (G.rootIn_reach (G.prefixEdges e.val) (G.dst e))).symm · exact (G.rootIn_eq_of_reach (G.rootIn_reach (G.prefixEdges e.val) (G.src e))).symm /-- **Exact Elder-Rule Merge-Tree Bijection**: On any connected filtered graph `G`, `youngerRoot` is a bijection from Kruskal MST edges to non-global vertices `{v : V // v ≠ G.globalElder}`. -/ theorem kruskal_youngerRoot_bijection (G : FilteredGraph V) [Nonempty V] (hconn : G.IsConnected) : ∃ (f : {e : Fin G.m // G.isKruskalEdge e} ≃ {v : V // v ≠ G.globalElder}), ∀ e, (f e).1 = G.youngerRoot e.1 := by classical have hne_glob : ∀ e : Fin G.m, G.isKruskalEdge e → G.youngerRoot e ≠ G.globalElder := by intro e he hEq have hold := G.elder_older_younger he rw [hEq] at hold rcases G.globalElder_le (G.elderRoot e) with h1 | h1 · rw [h1] at hold; exact G.older_irrefl _ hold · exact G.older_asymm h1 hold let toF : {e : Fin G.m // G.isKruskalEdge e} → {v : V // v ≠ G.globalElder} := fun e => ⟨G.youngerRoot e.1, hne_glob e.1 e.2⟩ have hinj : Function.Injective toF := by intro ⟨e₁, he₁⟩ ⟨e₂, he₂⟩ heq have hv : G.youngerRoot e₁ = G.youngerRoot e₂ := congrArg Subtype.val heq apply Subtype.ext rcases lt_trichotomy e₁.val e₂.val with hlt | heqv | hgt · exfalso have hsub : G.prefixEdges (e₁.val + 1) ⊆ G.prefixEdges e₂.val := by intro x hx; simp only [prefixEdges, Finset.mem_filter, Finset.mem_univ, true_and] at hx ⊢; omega have hreach := G.reachIn_mono hsub (G.reach_after_kruskalEdge e₁) rw [hv] at hreach have hle := G.rootIn_le (G.prefixEdges e₂.val) (G.reachIn_symm hreach) rw [G.rootIn_youngerRoot_before e₂, ← hv] at hle have hold := G.elder_older_younger he₁ rcases hle with h1 | h1 · rw [h1] at hold; exact G.older_irrefl _ hold · exact G.older_asymm hold h1 · exact Fin.ext heqv · exfalso have hsub : G.prefixEdges (e₂.val + 1) ⊆ G.prefixEdges e₁.val := by intro x hx; simp only [prefixEdges, Finset.mem_filter, Finset.mem_univ, true_and] at hx ⊢; omega have hreach := G.reachIn_mono hsub (G.reach_after_kruskalEdge e₂) rw [← hv] at hreach have hle := G.rootIn_le (G.prefixEdges e₁.val) (G.reachIn_symm hreach) rw [G.rootIn_youngerRoot_before e₁, hv] at hle have hold := G.elder_older_younger he₂ rcases hle with h1 | h1 · rw [h1] at hold; exact G.older_irrefl _ hold · exact G.older_asymm hold h1 have hsurj : Function.Surjective toF := by rintro ⟨v, hv⟩ have hv_gt : G.older G.globalElder v := by rcases G.globalElder_le v with h1 | h1 · exact absurd h1 hv · exact h1 -- Find first k ≤ m where v's component in prefixEdges k contains a vertex older than v let P : ℕ → Prop := fun k => ∃ u : V, G.older u v ∧ G.ReachIn (G.prefixEdges k) v u have hPm : P G.m := by refine ⟨G.globalElder, hv_gt, ?_⟩ have hall : G.prefixEdges G.m = Finset.univ := by ext e; simp [prefixEdges] rw [hall]; exact hconn v G.globalElder let k := Nat.find (⟨G.m, hPm⟩ : ∃ n, P n) have hkP : P k := Nat.find_spec (⟨G.m, hPm⟩ : ∃ n, P n) have hk_le : k ≤ G.m := Nat.find_min' _ hPm have hk_pos : 0 < k := by by_contra h0 have hk0 : k = 0 := by omega rcases hkP with ⟨u, hu_old, hu_reach⟩ rw [hk0] at hu_reach have hemp : G.prefixEdges 0 = ∅ := by ext e; simp [prefixEdges] rw [hemp] at hu_reach have hvu : v = u := by induction hu_reach with | refl => rfl | tail _ hstep _ => rcases hstep with ⟨e, he, _⟩; exact absurd he (Finset.notMem_empty e) subst hvu; exact G.older_irrefl v hu_old have hk_prev : ¬ P (k - 1) := Nat.find_min (⟨G.m, hPm⟩ : ∃ n, P n) (by omega) let e : Fin G.m := ⟨k - 1, by omega⟩ -- Helper: reaching in prefixEdges k without hitting older vertex before k means e connects v's -- component to a strictly older component! have hroot_v : G.rootIn (G.prefixEdges e.val) v = v := by apply (G.oldestOf_unique (G.compIn_nonempty _ v) (G.mem_compIn_self _ v) _).symm intro w hw have hreach : G.ReachIn (G.prefixEdges e.val) v w := by simpa [compIn] using hw rcases G.older_trichotomy w v with hwv | rfl | hvw · exfalso; exact hk_prev ⟨w, hwv, hreach⟩ · exact Or.inl rfl · exact Or.inr hvw -- Check that edge e connects v's component to a component with an older vertex have hreach_split : ∀ {a b : V}, G.ReachIn (G.prefixEdges k) a b → G.ReachIn (G.prefixEdges e.val) a b ∨ (G.ReachIn (G.prefixEdges e.val) a (G.src e) ∧ G.ReachIn (G.prefixEdges e.val) (G.dst e) b) ∨ (G.ReachIn (G.prefixEdges e.val) a (G.dst e) ∧ G.ReachIn (G.prefixEdges e.val) (G.src e) b) := by intro a b hab induction hab with | refl => exact Or.inl (G.reachIn_refl _ _) | @tail x y _ hstep ih => rcases hstep with ⟨e', he', hdir⟩ have he'lt : e'.val < k := by simpa [prefixEdges] using he' by_cases heq : e' = e · subst heq rcases hdir with ⟨hs, hd⟩ | ⟨hs, hd⟩ · subst hs; subst hd rcases ih with h | ⟨h1, _⟩ | ⟨h1, _⟩ · exact Or.inr (Or.inl ⟨h, G.reachIn_refl _ _⟩) · exact Or.inr (Or.inl ⟨h1, G.reachIn_refl _ _⟩) · exact Or.inl h1 · subst hs; subst hd rcases ih with h | ⟨h1, _⟩ | ⟨h1, _⟩ · exact Or.inr (Or.inr ⟨h, G.reachIn_refl _ _⟩) · exact Or.inl h1 · exact Or.inr (Or.inr ⟨h1, G.reachIn_refl _ _⟩) · have he'prev : e' ∈ G.prefixEdges e.val := by simp only [prefixEdges, Finset.mem_filter, Finset.mem_univ, true_and] have : e'.val ≠ e.val := fun h => heq (Fin.ext h) have hev : e.val = k - 1 := rfl omega have hxy : G.ReachIn (G.prefixEdges e.val) x y := Relation.ReflTransGen.single ⟨e', he'prev, hdir⟩ rcases ih with h | ⟨h1, h2⟩ | ⟨h1, h2⟩ · exact Or.inl (G.reachIn_trans h hxy) · exact Or.inr (Or.inl ⟨h1, G.reachIn_trans h2 hxy⟩) · exact Or.inr (Or.inr ⟨h1, G.reachIn_trans h2 hxy⟩) rcases hkP with ⟨u, hu_old, hu_reach⟩ rcases hreach_split hu_reach with hprev | ⟨h1, h2⟩ | ⟨h1, h2⟩ · exact absurd ⟨u, hu_old, hprev⟩ hk_prev · -- v ~ src e and dst e ~ u (with older u v) have hr1 : G.rootIn (G.prefixEdges e.val) (G.src e) = v := by rw [← G.rootIn_eq_of_reach h1, hroot_v] have hr2_le : u = G.rootIn (G.prefixEdges e.val) (G.dst e) ∨ G.older (G.rootIn (G.prefixEdges e.val) (G.dst e)) u := G.rootIn_le _ h2 have hr2_old : G.older (G.rootIn (G.prefixEdges e.val) (G.dst e)) v := by rcases hr2_le with rfl | h' · exact hu_old · exact G.older_trans h' hu_old have heK : G.isKruskalEdge e := by intro hsd have hEq := G.rootIn_eq_of_reach hsd rw [hr1] at hEq rw [← hEq] at hr2_old exact G.older_irrefl v hr2_old refine ⟨⟨e, heK⟩, Subtype.ext ?_⟩ change G.youngerRoot e = v unfold youngerRoot rw [hr1, if_neg (G.older_asymm hr2_old)] · -- v ~ dst e and src e ~ u (with older u v) have hr2 : G.rootIn (G.prefixEdges e.val) (G.dst e) = v := by rw [← G.rootIn_eq_of_reach h1, hroot_v] have hr1_le : u = G.rootIn (G.prefixEdges e.val) (G.src e) ∨ G.older (G.rootIn (G.prefixEdges e.val) (G.src e)) u := G.rootIn_le _ h2 have hr1_old : G.older (G.rootIn (G.prefixEdges e.val) (G.src e)) v := by rcases hr1_le with rfl | h' · exact hu_old · exact G.older_trans h' hu_old have heK : G.isKruskalEdge e := by intro hsd have hEq := G.rootIn_eq_of_reach hsd rw [hr2] at hEq rw [hEq] at hr1_old exact G.older_irrefl v hr1_old refine ⟨⟨e, heK⟩, Subtype.ext ?_⟩ change G.youngerRoot e = v unfold youngerRoot rw [hr2, if_pos hr1_old] exact ⟨Equiv.ofBijective toF ⟨hinj, hsurj⟩, fun _ => rfl⟩ /-- Exact cardinality of the Kruskal MST edge set: `|Kruskal(G)| = |V| - 1`. -/ theorem kruskalSet_card (G : FilteredGraph V) [Nonempty V] (hconn : G.IsConnected) : G.kruskalSet.card = Fintype.card V - 1 := by classical obtain ⟨f, _⟩ := G.kruskal_youngerRoot_bijection hconn have h1 : G.kruskalSet.card = Fintype.card {e : Fin G.m // G.isKruskalEdge e} := by rw [Fintype.card_subtype]; rfl rw [h1, Fintype.card_congr f] have h2 : Fintype.card {v : V // v ≠ G.globalElder} = (Finset.univ.erase G.globalElder).card := by rw [← Fintype.card_coe] apply Fintype.card_congr exact { toFun := fun ⟨v, hv⟩ => ⟨v, Finset.mem_erase.mpr ⟨hv, Finset.mem_univ v⟩⟩ invFun := fun ⟨v, hv⟩ => ⟨v, (Finset.mem_erase.mp hv).1⟩ left_inv := fun _ => rfl right_inv := fun _ => rfl } rw [h2, Finset.card_erase_of_mem (Finset.mem_univ _), Finset.card_univ] /-! ### Exact Persistence–MST Weight Identity & Kruskal Optimality -/ /-- Total edge weight of an edge subset `T ⊆ Fin G.m`. -/ def subsetWeight (G : FilteredGraph V) (T : Finset (Fin G.m)) : ℝ := ∑ e ∈ T, G.wt e /-- **Exact Persistence–MST Weight Correspondence**: On any connected filtered graph `G`, the sum of all finite `H_0` Elder-Rule bar lengths `∑_{e ∈ Kruskal} (wt e - birth(youngerRoot e))` equals the Kruskal MST weight minus the sum of non-global vertex birth times `wt(MST) - ∑_{v ≠ r_min} birth v`. -/ theorem elder_persistence_sum_eq_mst_sub_births (G : FilteredGraph V) [Nonempty V] (hconn : G.IsConnected) : (∑ e ∈ G.kruskalSet, (G.wt e - G.birth (G.youngerRoot e))) = G.subsetWeight G.kruskalSet - ∑ v ∈ Finset.univ.erase G.globalElder, G.birth v := by classical rw [Finset.sum_sub_distrib] unfold subsetWeight congr 1 obtain ⟨f, hf⟩ := G.kruskal_youngerRoot_bijection hconn apply Finset.sum_bij (fun e he => (f ⟨e, (Finset.mem_filter.mp he).2⟩).1) · intro e he exact Finset.mem_erase.mpr ⟨(f ⟨e, (Finset.mem_filter.mp he).2⟩).2, Finset.mem_univ _⟩ · intro e₁ he₁ e₂ he₂ heq have hsub : f ⟨e₁, (Finset.mem_filter.mp he₁).2⟩ = f ⟨e₂, (Finset.mem_filter.mp he₂).2⟩ := Subtype.ext heq exact congrArg Subtype.val (f.injective hsub) · intro v hv have hvne : v ≠ G.globalElder := (Finset.mem_erase.mp hv).1 let e := f.symm ⟨v, hvne⟩ refine ⟨e.1, Finset.mem_filter.mpr ⟨Finset.mem_univ _, e.2⟩, ?_⟩ have := f.apply_symm_apply ⟨v, hvne⟩ exact congrArg Subtype.val this · intro e he rw [hf ⟨e, (Finset.mem_filter.mp he).2⟩] /-- Corollary: when all vertices are born at `t = 0`, the total finite `H_0` persistence equals exactly the Kruskal MST weight. -/ theorem elder_persistence_sum_eq_mst_of_zero_birth (G : FilteredGraph V) [Nonempty V] (hconn : G.IsConnected) (h0 : ∀ v : V, G.birth v = 0) : (∑ e ∈ G.kruskalSet, (G.wt e - G.birth (G.youngerRoot e))) = G.subsetWeight G.kruskalSet := by rw [G.elder_persistence_sum_eq_mst_sub_births hconn] simp [h0] /-- Number of connected components of `(V, T)` (counted via canonical component roots). -/ noncomputable def numComps (G : FilteredGraph V) (T : Finset (Fin G.m)) : ℕ := by classical exact (Finset.univ.image (G.rootIn T)).card lemma numComps_empty (G : FilteredGraph V) : G.numComps ∅ = Fintype.card V := by classical unfold numComps have hid : G.rootIn ∅ = id := by funext v have hv : ∀ r, G.ReachIn ∅ v r → r = v := by intro r hr induction hr with | refl => rfl | tail _ hstep _ => rcases hstep with ⟨e, he, _⟩; exact absurd he (Finset.notMem_empty e) exact hv _ (G.rootIn_reach ∅ v) rw [hid, Finset.image_id, Finset.card_univ] lemma numComps_insert_ge (G : FilteredGraph V) (T : Finset (Fin G.m)) (e : Fin G.m) : G.numComps T ≤ G.numComps (insert e T) + 1 := by classical -- Map each root of T to its root in `insert e T`: at most two roots of T merge! let R := Finset.univ.image (G.rootIn T) let R' := Finset.univ.image (G.rootIn (insert e T)) let f : V → V := G.rootIn (insert e T) have hf_maps : ∀ r ∈ R, f r ∈ R' := fun r _ => Finset.mem_image_of_mem _ (Finset.mem_univ r) -- Any two distinct roots r1, r2 ∈ R that map to the same root in insert e T must be -- {rootIn T (src e), rootIn T (dst e)}! let rs := G.rootIn T (G.src e) let rd := G.rootIn T (G.dst e) have hinj_erase : Set.InjOn f (R.erase rd : Set V) := by intro r₁ hr₁ r₂ hr₂ heq have hr1_mem : r₁ ∈ R := Finset.mem_of_mem_erase hr₁ have hr2_mem : r₂ ∈ R := Finset.mem_of_mem_erase hr₂ have hr1_ne : r₁ ≠ rd := (Finset.mem_erase.mp hr₁).1 have hr2_ne : r₂ ≠ rd := (Finset.mem_erase.mp hr₂).1 -- Express r₁, r₂ as their own T-roots have hself : ∀ r ∈ R, G.rootIn T r = r := by intro r hr; rcases Finset.mem_image.mp hr with ⟨x, _, rfl⟩ exact (G.rootIn_eq_of_reach (G.rootIn_reach T x)).symm have hreach_ins : G.ReachIn (insert e T) r₁ r₂ := (G.rootIn_eq_iff _ _ _).mp heq -- Either r₁ ~_T r₂ (done), or the path uses edge e, so one of r₁, r₂ is in dst's component! have hsplit : ∀ {a b : V}, G.ReachIn (insert e T) a b → G.ReachIn T a b ∨ (G.ReachIn T a (G.src e) ∧ G.ReachIn T (G.dst e) b) ∨ (G.ReachIn T a (G.dst e) ∧ G.ReachIn T (G.src e) b) := by intro a b hab induction hab with | refl => exact Or.inl (G.reachIn_refl _ _) | @tail x y _ hstep ih => rcases hstep with ⟨e', he', hdir⟩ rcases Finset.mem_insert.mp he' with rfl | he'T · rcases hdir with ⟨hs, hd⟩ | ⟨hs, hd⟩ · subst hs; subst hd rcases ih with h | ⟨h1, _⟩ | ⟨h1, _⟩ · exact Or.inr (Or.inl ⟨h, G.reachIn_refl _ _⟩) · exact Or.inr (Or.inl ⟨h1, G.reachIn_refl _ _⟩) · exact Or.inl h1 · subst hs; subst hd rcases ih with h | ⟨h1, _⟩ | ⟨h1, _⟩ · exact Or.inr (Or.inr ⟨h, G.reachIn_refl _ _⟩) · exact Or.inl h1 · exact Or.inr (Or.inr ⟨h1, G.reachIn_refl _ _⟩) · have hxy : G.ReachIn T x y := Relation.ReflTransGen.single ⟨e', he'T, hdir⟩ rcases ih with h | ⟨h1, h2⟩ | ⟨h1, h2⟩ · exact Or.inl (G.reachIn_trans h hxy) · exact Or.inr (Or.inl ⟨h1, G.reachIn_trans h2 hxy⟩) · exact Or.inr (Or.inr ⟨h1, G.reachIn_trans h2 hxy⟩) rcases hsplit hreach_ins with hT | ⟨_, h2⟩ | ⟨h1, _⟩ · rw [← hself r₁ hr1_mem, ← hself r₂ hr2_mem] exact G.rootIn_eq_of_reach hT · exfalso; apply hr2_ne; rw [← hself r₂ hr2_mem]; exact (G.rootIn_eq_of_reach h2).symm · exfalso; apply hr1_ne; rw [← hself r₁ hr1_mem]; exact G.rootIn_eq_of_reach h1 have hcard_le : (R.erase rd).card ≤ R'.card := by calc (R.erase rd).card = ((R.erase rd).image f).card := (Finset.card_image_of_injOn hinj_erase).symm _ ≤ R'.card := Finset.card_le_card (Finset.image_subset_iff.mpr fun r hr => hf_maps r (Finset.mem_of_mem_erase hr)) have hrd_mem : rd ∈ R := Finset.mem_image_of_mem _ (Finset.mem_univ (G.dst e)) have herase := Finset.card_erase_add_one hrd_mem change R.card ≤ R'.card + 1 omega /-- Adding `|T|` edges reduces connected components by at most `|T|`: `|V| ≤ numComps T + |T|`. -/ lemma card_le_numComps_add_card (G : FilteredGraph V) (T : Finset (Fin G.m)) : Fintype.card V ≤ G.numComps T + T.card := by induction T using Finset.induction_on with | empty => simp [G.numComps_empty] | @insert e s he ih => rw [Finset.card_insert_of_notMem he] have hstep := G.numComps_insert_ge s e omega /-- Prefix Kruskal edges preserve the EXACT connected components of `prefixEdges k`. -/ lemma reachIn_kruskal_prefix_iff (G : FilteredGraph V) (k : ℕ) (u v : V) : G.ReachIn (G.kruskalSet ∩ G.prefixEdges k) u v ↔ G.ReachIn (G.prefixEdges k) u v := by classical constructor · exact G.reachIn_mono Finset.inter_subset_right · intro h induction k generalizing u v with | zero => have hemp : G.prefixEdges 0 = ∅ := by ext e; simp [prefixEdges] rw [hemp] at h ⊢; rwa [Finset.inter_empty] | succ k ih => by_cases hk : k < G.m · let ek : Fin G.m := ⟨k, hk⟩ have hpre : G.prefixEdges (k + 1) = insert ek (G.prefixEdges k) := by ext e; simp only [prefixEdges, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert, Fin.ext_iff, ek]; omega -- Every single step in prefixEdges (k+1) is reachable in kruskalSet ∩ prefixEdges (k+1) have hstep_reach : ∀ {a b : V}, G.EdgeSubsetAdj (G.prefixEdges (k + 1)) a b → G.ReachIn (G.kruskalSet ∩ G.prefixEdges (k + 1)) a b := by intro a b ⟨e', he', hdir⟩ rw [hpre] at he' have hsub_step : G.kruskalSet ∩ G.prefixEdges k ⊆ G.kruskalSet ∩ G.prefixEdges (k + 1) := Finset.inter_subset_inter_left (by rw [hpre]; exact Finset.subset_insert _ _) rcases Finset.mem_insert.mp he' with rfl | he'k · by_cases heK : G.isKruskalEdge ek · refine Relation.ReflTransGen.single ⟨ek, ?_, hdir⟩ exact Finset.mem_inter.mpr ⟨Finset.mem_filter.mpr ⟨Finset.mem_univ _, heK⟩, by rw [hpre]; exact Finset.mem_insert_self _ _⟩ · have hreach_sd : G.ReachIn (G.prefixEdges k) (G.src ek) (G.dst ek) := not_not.mp heK have hreach_k := ih (G.src ek) (G.dst ek) hreach_sd rcases hdir with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ · exact G.reachIn_mono hsub_step hreach_k · exact G.reachIn_mono hsub_step (G.reachIn_symm hreach_k) · exact G.reachIn_mono hsub_step (ih a b (Relation.ReflTransGen.single ⟨e', he'k, hdir⟩)) clear ih induction h with | refl => exact G.reachIn_refl _ _ | tail _ hst ih' => exact G.reachIn_trans ih' (hstep_reach hst) · have heq : G.prefixEdges (k + 1) = G.prefixEdges k := by ext e; simp only [prefixEdges, Finset.mem_filter, Finset.mem_univ, true_and] have := e.isLt; omega rw [heq] at h ⊢; exact ih u v h lemma numComps_mono (G : FilteredGraph V) {T₁ T₂ : Finset (Fin G.m)} (hT : T₁ ⊆ T₂) : G.numComps T₂ ≤ G.numComps T₁ := by classical unfold numComps have himg : Finset.univ.image (G.rootIn T₂) = (Finset.univ.image (G.rootIn T₁)).image (G.rootIn T₂) := by ext r; simp only [Finset.mem_image, Finset.mem_univ, true_and] constructor · rintro ⟨v, rfl⟩ refine ⟨G.rootIn T₁ v, ⟨v, rfl⟩, ?_⟩ exact (G.rootIn_eq_of_reach (G.reachIn_mono hT (G.rootIn_reach T₁ v))).symm · rintro ⟨_, ⟨v, rfl⟩, rfl⟩ exact ⟨v, G.rootIn_eq_of_reach (G.reachIn_mono hT (G.rootIn_reach T₁ v))⟩ rw [himg] exact Finset.card_image_le /-- Exact component count for prefix Kruskal edges: `numComps (prefixEdges k) + |Kruskal ∩ prefixEdges k| = |V|`. -/ lemma numComps_add_kruskal_prefix (G : FilteredGraph V) (k : ℕ) : G.numComps (G.prefixEdges k) + (G.kruskalSet ∩ G.prefixEdges k).card = Fintype.card V := by classical induction k with | zero => have hemp : G.prefixEdges 0 = ∅ := by ext e; simp [prefixEdges] rw [hemp, Finset.inter_empty, Finset.card_empty, add_zero, G.numComps_empty] | succ k ih => by_cases hk : k < G.m · let ek : Fin G.m := ⟨k, hk⟩ have hpre : G.prefixEdges (k + 1) = insert ek (G.prefixEdges k) := by ext e; simp only [prefixEdges, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert, Fin.ext_iff, ek]; omega by_cases heK : G.isKruskalEdge ek · -- Kruskal edge merges 2 distinct components, so numComps drops by EXACTLY 1 have hnotmem : ek ∉ G.kruskalSet ∩ G.prefixEdges k := by simp [prefixEdges, ek] have hinter : G.kruskalSet ∩ G.prefixEdges (k + 1) = insert ek (G.kruskalSet ∩ G.prefixEdges k) := by ext e; simp only [Finset.mem_inter, hpre, Finset.mem_insert, kruskalSet, Finset.mem_filter, Finset.mem_univ, true_and] constructor · rintro ⟨he, rfl | h2⟩ · exact Or.inl rfl · exact Or.inr ⟨he, h2⟩ · rintro (rfl | ⟨h1, h2⟩) · exact ⟨heK, Or.inl rfl⟩ · exact ⟨h1, Or.inr h2⟩ rw [hinter, Finset.card_insert_of_notMem hnotmem] have hdrop : G.numComps (G.prefixEdges (k + 1)) + 1 = G.numComps (G.prefixEdges k) := by have hge := G.numComps_insert_ge (G.prefixEdges k) ek rw [← hpre] at hge -- Strict drop: two distinct roots rs ≠ rd map to the same root in prefixEdges (k+1) let R := Finset.univ.image (G.rootIn (G.prefixEdges k)) let R' := Finset.univ.image (G.rootIn (G.prefixEdges (k + 1))) let rs := G.rootIn (G.prefixEdges k) (G.src ek) let rd := G.rootIn (G.prefixEdges k) (G.dst ek) have hne : rs ≠ rd := fun h => heK ((G.rootIn_eq_iff _ _ _).mp h) have hsub_k : G.prefixEdges k ⊆ G.prefixEdges (k + 1) := by rw [hpre]; exact Finset.subset_insert _ _ have hmerge : G.rootIn (G.prefixEdges (k + 1)) rs = G.rootIn (G.prefixEdges (k + 1)) rd := by have h1 := G.reachIn_mono hsub_k (G.rootIn_reach (G.prefixEdges k) (G.src ek)) have h2 := G.reachIn_mono hsub_k (G.rootIn_reach (G.prefixEdges k) (G.dst ek)) have h3 : G.ReachIn (G.prefixEdges (k + 1)) (G.src ek) (G.dst ek) := Relation.ReflTransGen.single ⟨ek, by rw [hpre]; exact Finset.mem_insert_self _ _, Or.inl ⟨rfl, rfl⟩⟩ exact G.rootIn_eq_of_reach (G.reachIn_trans (G.reachIn_symm h1) (G.reachIn_trans h3 h2)) have himg : R' = (R.erase rd).image (G.rootIn (G.prefixEdges (k + 1))) := by ext x; simp only [R', R, Finset.mem_image, Finset.mem_univ, true_and, Finset.mem_erase] constructor · rintro ⟨v, rfl⟩ by_cases hv : G.rootIn (G.prefixEdges k) v = rd · refine ⟨rs, ⟨hne, ⟨G.src ek, rfl⟩⟩, ?_⟩ rw [hmerge, ← hv] exact (G.rootIn_eq_of_reach (G.reachIn_mono hsub_k (G.rootIn_reach _ v))).symm · refine ⟨G.rootIn (G.prefixEdges k) v, ⟨hv, ⟨v, rfl⟩⟩, ?_⟩ exact (G.rootIn_eq_of_reach (G.reachIn_mono hsub_k (G.rootIn_reach _ v))).symm · rintro ⟨_, ⟨_, ⟨v, rfl⟩⟩, rfl⟩ exact ⟨v, G.rootIn_eq_of_reach (G.reachIn_mono hsub_k (G.rootIn_reach _ v))⟩ have hlt : R'.card ≤ (R.erase rd).card := by rw [himg]; exact Finset.card_image_le have hrd_mem : rd ∈ R := Finset.mem_image_of_mem _ (Finset.mem_univ _) have herase := Finset.card_erase_add_one hrd_mem change R'.card + 1 = R.card change R.card ≤ R'.card + 1 at hge omega omega · -- Non-Kruskal edge changes neither kruskalSet nor numComps have hinter : G.kruskalSet ∩ G.prefixEdges (k + 1) = G.kruskalSet ∩ G.prefixEdges k := by ext e; simp only [Finset.mem_inter, hpre, Finset.mem_insert, kruskalSet, Finset.mem_filter, Finset.mem_univ, true_and] constructor · rintro ⟨he, rfl | h2⟩ · exact absurd he heK · exact ⟨he, h2⟩ · rintro ⟨h1, h2⟩; exact ⟨h1, Or.inr h2⟩ have hcomp_eq : G.numComps (G.prefixEdges (k + 1)) = G.numComps (G.prefixEdges k) := by have hreach_eq : ∀ u v, G.ReachIn (G.prefixEdges (k + 1)) u v ↔ G.ReachIn (G.prefixEdges k) u v := by intro u v rw [← G.reachIn_kruskal_prefix_iff (k + 1), ← G.reachIn_kruskal_prefix_iff k, hinter] have hroot_eq : G.rootIn (G.prefixEdges (k + 1)) = G.rootIn (G.prefixEdges k) := by funext v have hceq : G.compIn (G.prefixEdges (k + 1)) v = G.compIn (G.prefixEdges k) v := by ext x; simp [compIn, hreach_eq] unfold rootIn apply G.oldestOf_unique (G.compIn_nonempty _ v) · rw [← hceq]; exact G.oldestOf_mem _ _ · intro x hx; rw [← hceq] at hx; exact G.oldestOf_le _ _ hx unfold numComps; rw [hroot_eq] rw [hinter, hcomp_eq]; exact ih · have heq : G.prefixEdges (k + 1) = G.prefixEdges k := by ext e; simp only [prefixEdges, Finset.mem_filter, Finset.mem_univ, true_and] have := e.isLt; omega rw [heq]; exact ih /-- **Kruskal MST Optimality Theorem (Arbitrary Real Edge Weights)**: For any connected filtered graph `G` and any spanning tree `T ⊆ Fin G.m` (connected subset of `|V| - 1` edges), `wt(Kruskal(G)) ≤ wt(T)`. -/ theorem kruskal_mst_optimal (G : FilteredGraph V) [Nonempty V] (hconn : G.IsConnected) (T : Finset (Fin G.m)) (hT_card : T.card = Fintype.card V - 1) (hT_conn : ∀ u v : V, G.ReachIn T u v) : G.subsetWeight G.kruskalSet ≤ G.subsetWeight T := by classical -- Step 1: Prefix cardinality dominance: |T ∩ prefixEdges k| ≤ |Kruskal ∩ prefixEdges k| for all k ≤ m! have hprefix_le : ∀ k : ℕ, (T ∩ G.prefixEdges k).card ≤ (G.kruskalSet ∩ G.prefixEdges k).card := by intro k -- For T: remaining edges T \ prefixEdges k have size |T| - |T ∩ prefixEdges k|; -- Wait: T = (T ∩ prefixEdges k) ∪ (T \ prefixEdges k), and adding (T \ prefixEdges k) to -- (T ∩ prefixEdges k) reduces numComps from numComps(T ∩ prefixEdges k) to 1! -- Therefore numComps(T ∩ prefixEdges k) - 1 ≤ |T \ prefixEdges k| = (|V| - 1) - |T ∩ prefixEdges k|? -- Wait: that gives an upper bound on numComps, not lower! For lower bound on |Kruskal ∩ prefix k|: -- numComps(prefix k) ≤ numComps(T ∩ prefix k), AND by adding T \ prefix k to T ∩ prefix k -- to reach 1 component: numComps(T ∩ prefix k) ≤ 1 + |T \ prefix k|! have h1 : G.numComps (G.prefixEdges k) ≤ G.numComps (T ∩ G.prefixEdges k) := G.numComps_mono Finset.inter_subset_right -- Adding s edges to T ∩ prefixEdges k lowers numComps by at most s have hadd_drop : ∀ (A B : Finset (Fin G.m)), G.numComps A ≤ G.numComps (A ∪ B) + B.card := by intro A B induction B using Finset.induction_on with | empty => simp | @insert e s he ih => rw [Finset.card_insert_of_notMem he, Finset.union_insert] have hstep := G.numComps_insert_ge (A ∪ s) e omega have hsplit : (T ∩ G.prefixEdges k) ∪ (T \ G.prefixEdges k) = T := by ext e; by_cases h : e ∈ G.prefixEdges k <;> simp [h] have hT_one : G.numComps T = 1 := by unfold numComps obtain ⟨v0⟩ := ‹Nonempty V› have himg : Finset.univ.image (G.rootIn T) = {G.rootIn T v0} := by ext r; simp only [Finset.mem_image, Finset.mem_univ, true_and, Finset.mem_singleton] constructor · rintro ⟨v, rfl⟩; exact G.rootIn_eq_of_reach (hT_conn v v0) · rintro rfl; exact ⟨v0, rfl⟩ rw [himg, Finset.card_singleton] have h2 : G.numComps (T ∩ G.prefixEdges k) ≤ 1 + (T \ G.prefixEdges k).card := by have := hadd_drop (T ∩ G.prefixEdges k) (T \ G.prefixEdges k) rw [hsplit, hT_one] at this; exact this have hcard_split : (T ∩ G.prefixEdges k).card + (T \ G.prefixEdges k).card = T.card := Finset.card_inter_add_card_sdiff T (G.prefixEdges k) have hK := G.numComps_add_kruskal_prefix k have hV_pos : 1 ≤ Fintype.card V := Fintype.card_pos omega -- Step 2: Abel summation by parts on `Fin G.m` using `G.hsorted` and `|Kruskal| = |T|`! have hcard_eq : G.kruskalSet.card = T.card := by rw [G.kruskalSet_card hconn, hT_card] unfold subsetWeight -- Prove for all `k ≤ G.m`: if `k = G.m`, the boundary term vanishes; in general: -- For any two subsets `A, B ⊆ Fin m` with `|A ∩ prefix k| ≤ |B ∩ prefix k|` for all `k` -- and `|A| = |B|`, `∑_{e ∈ B} wt e ≤ ∑_{e ∈ A} wt e` (Abel summation)! have habel : ∀ (n : ℕ) (hn : n ≤ G.m), (∑ e ∈ G.kruskalSet ∩ G.prefixEdges n, G.wt e) - (∑ e ∈ T ∩ G.prefixEdges n, G.wt e) ≤ if h : 0 < n then ((G.kruskalSet ∩ G.prefixEdges n).card - (T ∩ G.prefixEdges n).card : ℝ) * G.wt ⟨n - 1, by omega⟩ else 0 := by intro n induction n with | zero => intro _ have hemp : G.prefixEdges 0 = ∅ := by ext e; simp [prefixEdges] simp [hemp] | succ k ih => intro hk let ek : Fin G.m := ⟨k, by omega⟩ have hpre : G.prefixEdges (k + 1) = insert ek (G.prefixEdges k) := by ext e; simp only [prefixEdges, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert, Fin.ext_iff, ek]; omega have hnot_pre : ek ∉ G.prefixEdges k := by simp [prefixEdges, ek] have hsum_step : ∀ (S : Finset (Fin G.m)), (∑ e ∈ S ∩ G.prefixEdges (k + 1), G.wt e) = (∑ e ∈ S ∩ G.prefixEdges k, G.wt e) + if ek ∈ S then G.wt ek else 0 := by intro S by_cases hS : ek ∈ S · have hinter : S ∩ G.prefixEdges (k + 1) = insert ek (S ∩ G.prefixEdges k) := by ext e; simp only [Finset.mem_inter, hpre, Finset.mem_insert] constructor · rintro ⟨h1, rfl | h2⟩ · exact Or.inl rfl · exact Or.inr ⟨h1, h2⟩ · rintro (rfl | ⟨h1, h2⟩) · exact ⟨hS, Or.inl rfl⟩ · exact ⟨h1, Or.inr h2⟩ rw [hinter, Finset.sum_insert (fun h => hnot_pre (Finset.mem_inter.mp h).2), if_pos hS] ring · have hinter : S ∩ G.prefixEdges (k + 1) = S ∩ G.prefixEdges k := by ext e; simp only [Finset.mem_inter, hpre, Finset.mem_insert] constructor · rintro ⟨h1, rfl | h2⟩ · exact absurd h1 hS · exact ⟨h1, h2⟩ · rintro ⟨h1, h2⟩; exact ⟨h1, Or.inr h2⟩ rw [hinter, if_neg hS, add_zero] have hcard_step : ∀ (S : Finset (Fin G.m)), ((S ∩ G.prefixEdges (k + 1)).card : ℝ) = ((S ∩ G.prefixEdges k).card : ℝ) + if ek ∈ S then 1 else 0 := by intro S by_cases hS : ek ∈ S · have hinter : S ∩ G.prefixEdges (k + 1) = insert ek (S ∩ G.prefixEdges k) := by ext e; simp only [Finset.mem_inter, hpre, Finset.mem_insert] constructor · rintro ⟨h1, rfl | h2⟩ · exact Or.inl rfl · exact Or.inr ⟨h1, h2⟩ · rintro (rfl | ⟨h1, h2⟩) · exact ⟨hS, Or.inl rfl⟩ · exact ⟨h1, Or.inr h2⟩ rw [hinter, Finset.card_insert_of_notMem (fun h => hnot_pre (Finset.mem_inter.mp h).2), if_pos hS] push_cast; rfl · have hinter : S ∩ G.prefixEdges (k + 1) = S ∩ G.prefixEdges k := by ext e; simp only [Finset.mem_inter, hpre, Finset.mem_insert] constructor · rintro ⟨h1, rfl | h2⟩ · exact absurd h1 hS · exact ⟨h1, h2⟩ · rintro ⟨h1, h2⟩; exact ⟨h1, Or.inr h2⟩ rw [hinter, if_neg hS, add_zero] rw [hsum_step G.kruskalSet, hsum_step T, dif_pos (Nat.succ_pos k)] have hcardK := hcard_step G.kruskalSet have hcardT := hcard_step T have hih := ih (by omega) by_cases hk0 : 0 < k · rw [dif_pos hk0] at hih have hdiff_nonneg : (0 : ℝ) ≤ ((G.kruskalSet ∩ G.prefixEdges k).card : ℝ) - ((T ∩ G.prefixEdges k).card : ℝ) := by have := hprefix_le k have : ((T ∩ G.prefixEdges k).card : ℝ) ≤ ((G.kruskalSet ∩ G.prefixEdges k).card : ℝ) := Nat.cast_le.mpr this linarith have hwt_le : G.wt ⟨k - 1, by omega⟩ ≤ G.wt ek := G.hsorted (show (⟨k - 1, by omega⟩ : Fin G.m) ≤ ek by simp [Fin.le_def, ek]) have hmul : (((G.kruskalSet ∩ G.prefixEdges k).card : ℝ) - ((T ∩ G.prefixEdges k).card : ℝ)) * G.wt ⟨k - 1, by omega⟩ ≤ (((G.kruskalSet ∩ G.prefixEdges k).card : ℝ) - ((T ∩ G.prefixEdges k).card : ℝ)) * G.wt ek := mul_le_mul_of_nonneg_left hwt_le hdiff_nonneg have hstep_eq : (⟨k + 1 - 1, by omega⟩ : Fin G.m) = ek := Fin.ext (by simp [ek]) rw [hstep_eq, hcardK, hcardT] split_ifs <;> linarith · have hk_eq : k = 0 := by omega subst hk_eq have hemp : G.prefixEdges 0 = ∅ := by ext e; simp [prefixEdges] have hstep_eq : (⟨0 + 1 - 1, by omega⟩ : Fin G.m) = ek := Fin.ext (by simp [ek]) rw [hstep_eq, hcardK, hcardT, hemp] simp only [Finset.inter_empty, Finset.sum_empty, Finset.card_empty, Nat.cast_zero, zero_add] split_ifs <;> linarith have hfinal := habel G.m le_rfl have hall : G.prefixEdges G.m = Finset.univ := by ext e; simp [prefixEdges] rw [hall, Finset.inter_univ, Finset.inter_univ, hcard_eq] at hfinal split_ifs at hfinal <;> linarith /-! ### Exact `H_0` Persistent Homology Module & Elder-Rule Isomorphism -/ /-- Active vertices at filtration time `t`: `{v : V // G.birth v ≤ t}`. -/ abbrev ActiveVert (G : FilteredGraph V) (t : ℝ) := {v : V // G.birth v ≤ t} /-- Active edges at filtration time `t`: `{e : Fin G.m | G.wt e ≤ t}`. -/ noncomputable def activeEdges (G : FilteredGraph V) (t : ℝ) : Finset (Fin G.m) := by classical exact Finset.univ.filter fun e : Fin G.m => G.wt e ≤ t /-- Adjacency on active vertices at time `t`. -/ def ActiveAdj (G : FilteredGraph V) (t : ℝ) (u v : G.ActiveVert t) : Prop := G.EdgeSubsetAdj (G.activeEdges t) u.1 v.1 /-- Connected components `π_0(G_t)` of the filtered graph at time `t`. -/ abbrev CompAt (G : FilteredGraph V) (t : ℝ) : Type _ := Quot (Relation.ReflTransGen (G.ActiveAdj t)) /-- Connectedness in `ActiveAdj t` agrees with `ReachIn (activeEdges t)` for active vertices. -/ lemma active_reach_iff (G : FilteredGraph V) (t : ℝ) (u v : G.ActiveVert t) : Relation.ReflTransGen (G.ActiveAdj t) u v ↔ G.ReachIn (G.activeEdges t) u.1 v.1 := by classical constructor · intro h induction h with | refl => exact G.reachIn_refl _ _ | tail _ hstep ih => exact Relation.ReflTransGen.tail ih hstep · intro h have hgen : ∀ {x y : V} (hx : G.birth x ≤ t) (hy : G.birth y ≤ t), G.ReachIn (G.activeEdges t) x y → Relation.ReflTransGen (G.ActiveAdj t) ⟨x, hx⟩ ⟨y, hy⟩ := by intro x y hx hy hxy induction hxy with | refl => exact Relation.ReflTransGen.refl | @tail a b hab hstep ih => rcases hstep with ⟨e, he, hdir⟩ have hwt : G.wt e ≤ t := (Finset.mem_filter.mp he).2 have ha : G.birth a ≤ t := by rcases hdir with ⟨rfl, _⟩ | ⟨_, rfl⟩ · exact (G.hbirth_src e).trans hwt · exact (G.hbirth_dst e).trans hwt exact Relation.ReflTransGen.tail (ih ha) ⟨e, he, hdir⟩ exact hgen u.2 v.2 h /-- Every `activeEdges t` is a prefix `prefixEdges (k_t)` (since `G.wt` is monotone!). -/ lemma activeEdges_eq_prefixEdges (G : FilteredGraph V) (t : ℝ) : ∃ k : ℕ, k ≤ G.m ∧ G.activeEdges t = G.prefixEdges k ∧ (∀ e : Fin G.m, e.val < k ↔ G.wt e ≤ t) := by classical let k := (G.activeEdges t).card have hiff : ∀ e : Fin G.m, e.val < k ↔ G.wt e ≤ t := by intro e constructor · intro hlt by_contra hgt; push Not at hgt have hsub : G.activeEdges t ⊆ G.prefixEdges e.val := by intro e' he' have hw' : G.wt e' ≤ t := (Finset.mem_filter.mp he').2 simp only [prefixEdges, Finset.mem_filter, Finset.mem_univ, true_and] by_contra hge; push Not at hge have := G.hsorted (show e ≤ e' from hge); linarith have hcard_pre : (G.prefixEdges e.val).card = e.val := by have himg : G.prefixEdges e.val = (Finset.range e.val).attach.image (fun x => (⟨x.1, by have := Finset.mem_range.mp x.2; omega⟩ : Fin G.m)) := by ext x; simp only [prefixEdges, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image, Finset.mem_attach, Subtype.exists, Finset.mem_range] constructor · intro hx; exact ⟨x.val, hx, rfl⟩ · rintro ⟨a, ha, hae⟩; have := congrArg Fin.val hae; simp at this; omega rw [himg, Finset.card_image_of_injective] · simp · intro a b hab; exact Subtype.ext (congrArg Fin.val hab) have := Finset.card_le_card hsub rw [hcard_pre] at this; omega · intro hle have hsub : G.prefixEdges (e.val + 1) ⊆ G.activeEdges t := by intro e' he' simp only [prefixEdges, Finset.mem_filter, Finset.mem_univ, true_and] at he' refine Finset.mem_filter.mpr ⟨Finset.mem_univ _, ?_⟩ exact (G.hsorted (show e' ≤ e from Nat.le_of_lt_succ he')).trans hle have hcard_pre : (G.prefixEdges (e.val + 1)).card = e.val + 1 := by have himg : G.prefixEdges (e.val + 1) = (Finset.range (e.val + 1)).attach.image (fun x => (⟨x.1, by have := Finset.mem_range.mp x.2; omega⟩ : Fin G.m)) := by ext x; simp only [prefixEdges, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image, Finset.mem_attach, Subtype.exists, Finset.mem_range] constructor · intro hx; exact ⟨x.val, hx, rfl⟩ · rintro ⟨a, ha, hae⟩; have := congrArg Fin.val hae; simp at this; omega rw [himg, Finset.card_image_of_injective] · simp · intro a b hab; exact Subtype.ext (congrArg Fin.val hab) have := Finset.card_le_card hsub rw [hcard_pre] at this; omega refine ⟨k, ?_, ?_, hiff⟩ · calc k = (G.activeEdges t).card := rfl _ ≤ (Finset.univ : Finset (Fin G.m)).card := Finset.card_filter_le _ _ _ = G.m := by simp · ext e; simp only [activeEdges, prefixEdges, Finset.mem_filter, Finset.mem_univ, true_and] exact (hiff e).symm /-- Free `K`-vector space `H_0(G_t; K)` on connected components at time `t`. -/ abbrev H0Space (K : Type u) [Field K] (G : FilteredGraph V) (t : ℝ) := G.CompAt t →₀ K /-- Canonical map on connected components from time `s` to time `t` (`s ≤ t`). -/ def compMap (G : FilteredGraph V) {s t : ℝ} (hst : s ≤ t) : G.CompAt s → G.CompAt t := Quot.lift (fun u : G.ActiveVert s => Quot.mk _ ⟨u.1, u.2.trans hst⟩) (fun a b hab => by apply Quot.sound induction hab with | refl => exact Relation.ReflTransGen.refl | tail _ hstep ih => rcases hstep with ⟨e, he, hdir⟩ have he' : e ∈ G.activeEdges t := by classical exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, (Finset.mem_filter.mp he).2.trans hst⟩ exact Relation.ReflTransGen.tail ih ⟨e, he', hdir⟩) /-- Persistence transition map `H_0(G_s; K) →ₗ[K] H_0(G_t; K)` for `s ≤ t`. -/ noncomputable def h0Map (K : Type u) [Field K] (G : FilteredGraph V) {s t : ℝ} (hst : s ≤ t) : G.H0Space K s →ₗ[K] G.H0Space K t := Finsupp.lmapDomain K K (G.compMap hst) /-- Elder-Rule barcode direct-sum space at time `t`: `𝕀[birth r_min, ∞)(t) ⊕ ⨁_{e ∈ Kruskal(G)} 𝕀[birth(youngerRoot e), wt e)(t)`. -/ abbrev ElderBarcodeSpace (K : Type u) [Field K] (G : FilteredGraph V) [Nonempty V] (t : ℝ) := (PLift (G.birth G.globalElder ≤ t) → K) × (∀ e : {e : Fin G.m // G.isKruskalEdge e}, PLift (G.birth (G.youngerRoot e.1) ≤ t ∧ t < G.wt e.1) → K) /-- Transition map on the Elder-Rule barcode direct sum for `s ≤ t`. -/ noncomputable def elderBarcodeMap (K : Type u) [Field K] (G : FilteredGraph V) [Nonempty V] {s t : ℝ} (_hst : s ≤ t) : G.ElderBarcodeSpace K s →ₗ[K] G.ElderBarcodeSpace K t where toFun x := (fun _ => if hs : G.birth G.globalElder ≤ s then x.1 ⟨hs⟩ else 0, fun e _ => if hs : G.birth (G.youngerRoot e.1) ≤ s ∧ s < G.wt e.1 then x.2 e ⟨hs⟩ else 0) map_add' x y := by apply Prod.ext · funext _; by_cases h : G.birth G.globalElder ≤ s <;> simp [h] · funext e _; by_cases h : G.birth (G.youngerRoot e.1) ≤ s ∧ s < G.wt e.1 <;> simp [h] map_smul' c x := by apply Prod.ext · funext _; by_cases h : G.birth G.globalElder ≤ s <;> simp [h] · funext e _; by_cases h : G.birth (G.youngerRoot e.1) ≤ s ∧ s < G.wt e.1 <;> simp [h] /-- **Exact Elder Rule Persistence Isomorphism Theorem**: For any connected finite filtered graph `G` and any field `K`, there exists an explicit family of `K`-linear isomorphisms `e_t : ElderBarcodeSpace K G t ≃ₗ[K] H_0(G_t; K)` commuting with all filtration transition maps `s ≤ t`. That is: `H_0(G; K) ≅ 𝕀[birth(r_min), ∞) ⊕ ⨁_{e ∈ Kruskal(G)} 𝕀[birth(youngerRoot e), wt e)`. -/ theorem elder_rule_h0_iso (K : Type u) [Field K] (G : FilteredGraph V) [Nonempty V] (hconn : G.IsConnected) : ∃ (e : ∀ t : ℝ, G.ElderBarcodeSpace K t ≃ₗ[K] G.H0Space K t), ∀ {s t : ℝ} (hst : s ≤ t), (e t : G.ElderBarcodeSpace K t →ₗ[K] G.H0Space K t).comp (G.elderBarcodeMap K hst) = (G.h0Map K hst).comp (e s : G.ElderBarcodeSpace K s →ₗ[K] G.H0Space K s) := by classical obtain ⟨bij, hbij⟩ := G.kruskal_youngerRoot_bijection hconn -- Define component class `[v]_t ∈ H_0(G_t; K)` for active `v` let cls (t : ℝ) (v : V) (hv : G.birth v ≤ t) : G.H0Space K t := Finsupp.single (Quot.mk _ ⟨v, hv⟩) (1 : K) -- Explicit linear map Φ_t : ElderBarcodeSpace K G t →ₗ[K] H_0(G_t; K) let Φ (t : ℝ) : G.ElderBarcodeSpace K t →ₗ[K] G.H0Space K t := { toFun := fun x => (if hg : G.birth G.globalElder ≤ t then x.1 ⟨hg⟩ • cls t G.globalElder hg else 0) + ∑ e : {e : Fin G.m // G.isKruskalEdge e}, if he : G.birth (G.youngerRoot e.1) ≤ t ∧ t < G.wt e.1 then x.2 e ⟨he⟩ • (cls t (G.youngerRoot e.1) he.1 - cls t (G.elderRoot e.1) ((G.birth_elder_le_younger e.2).trans he.1)) else 0 map_add' := fun x y => by have h1 : (if hg : G.birth G.globalElder ≤ t then (x.1 + y.1) ⟨hg⟩ • cls t G.globalElder hg else 0) = (if hg : G.birth G.globalElder ≤ t then x.1 ⟨hg⟩ • cls t G.globalElder hg else 0) + (if hg : G.birth G.globalElder ≤ t then y.1 ⟨hg⟩ • cls t G.globalElder hg else 0) := by split_ifs <;> simp [add_smul] have h2 : (∑ e : {e : Fin G.m // G.isKruskalEdge e}, if he : G.birth (G.youngerRoot e.1) ≤ t ∧ t < G.wt e.1 then (x.2 + y.2) e ⟨he⟩ • (cls t (G.youngerRoot e.1) he.1 - cls t (G.elderRoot e.1) ((G.birth_elder_le_younger e.2).trans he.1)) else 0) = (∑ e : {e : Fin G.m // G.isKruskalEdge e}, if he : G.birth (G.youngerRoot e.1) ≤ t ∧ t < G.wt e.1 then x.2 e ⟨he⟩ • (cls t (G.youngerRoot e.1) he.1 - cls t (G.elderRoot e.1) ((G.birth_elder_le_younger e.2).trans he.1)) else 0) + (∑ e : {e : Fin G.m // G.isKruskalEdge e}, if he : G.birth (G.youngerRoot e.1) ≤ t ∧ t < G.wt e.1 then y.2 e ⟨he⟩ • (cls t (G.youngerRoot e.1) he.1 - cls t (G.elderRoot e.1) ((G.birth_elder_le_younger e.2).trans he.1)) else 0) := by rw [← Finset.sum_add_distrib] apply Finset.sum_congr rfl; intro e _; split_ifs <;> simp [add_smul] simp only [Prod.fst_add, Prod.snd_add] rw [h1, h2]; abel map_smul' := fun c x => by simp only [RingHom.id_apply, smul_add, Finset.smul_sum] congr 1 · split_ifs <;> simp [smul_smul] · apply Finset.sum_congr rfl; intro e _; split_ifs <;> simp [smul_smul] } -- Helper: key root criterion at time t: a vertex v with birth v ≤ t is its own root in activeEdges t -- iff either v = globalElder, or its unique Kruskal edge e has t < wt e! have hroot_iff : ∀ (t : ℝ) (v : V), G.rootIn (G.activeEdges t) v = v ↔ (v = G.globalElder ∨ ∃ e : {e : Fin G.m // G.isKruskalEdge e}, G.youngerRoot e.1 = v ∧ t < G.wt e.1) := by intro t v obtain ⟨kt, hkt_le, hkt_eq, hkt_iff⟩ := G.activeEdges_eq_prefixEdges t rw [hkt_eq] by_cases hvg : v = G.globalElder · subst hvg simp only [true_or, iff_true] apply (G.oldestOf_unique (G.compIn_nonempty _ _) (G.mem_compIn_self _ _) _).symm intro w _; exact G.globalElder_le w · simp only [hvg, false_or] let e := bij.symm ⟨v, hvg⟩ have he_v : G.youngerRoot e.1 = v := by have := congrArg Subtype.val (bij.apply_symm_apply ⟨v, hvg⟩) rwa [hbij e] at this constructor · intro hroot refine ⟨e, he_v, ?_⟩ by_contra hle; push Not at hle have he_lt : e.1.val < kt := (hkt_iff e.1).mpr hle have hsub : G.prefixEdges (e.1.val + 1) ⊆ G.prefixEdges kt := by intro x hx; simp only [prefixEdges, Finset.mem_filter, Finset.mem_univ, true_and] at hx ⊢ omega have hreach := G.reachIn_mono hsub (G.reach_after_kruskalEdge e.1) rw [he_v] at hreach have hmin := G.rootIn_le (G.prefixEdges kt) (G.reachIn_symm hreach) rw [hroot] at hmin have hold := G.elder_older_younger e.2 rw [he_v] at hold rcases hmin with h1 | h1 · rw [h1] at hold; exact G.older_irrefl _ hold · exact G.older_asymm hold h1 · rintro ⟨e', he'_v, hwt_gt⟩ have he'_eq : e' = e := by apply bij.injective apply Subtype.ext rw [hbij e', hbij e, he'_v, he_v] subst he'_eq have hkt_le_e : kt ≤ e.1.val := by by_contra hgt; push Not at hgt have : G.wt e.1 ≤ t := (hkt_iff e.1).mp (by omega) linarith have hsub : G.prefixEdges kt ⊆ G.prefixEdges e.1.val := by intro x hx; simp only [prefixEdges, Finset.mem_filter, Finset.mem_univ, true_and] at hx ⊢ omega apply (G.oldestOf_unique (G.compIn_nonempty _ v) (G.mem_compIn_self _ v) _).symm intro w hw have hreach : G.ReachIn (G.prefixEdges e.1.val) v w := G.reachIn_mono hsub (by simpa [compIn] using hw) have hmin := G.rootIn_le (G.prefixEdges e.1.val) hreach rw [← he_v, G.rootIn_youngerRoot_before e.1, he_v] at hmin exact hmin -- Helper: two active vertices have the same CompAt t iff they have the same rootIn (activeEdges t) have hcls_eq_iff : ∀ (t : ℝ) (u v : V) (hu : G.birth u ≤ t) (hv : G.birth v ≤ t), (Quot.mk (Relation.ReflTransGen (G.ActiveAdj t)) ⟨u, hu⟩ : G.CompAt t) = Quot.mk _ ⟨v, hv⟩ ↔ G.rootIn (G.activeEdges t) u = G.rootIn (G.activeEdges t) v := by intro t u v hu hv rw [G.rootIn_eq_iff, ← G.active_reach_iff t ⟨u, hu⟩ ⟨v, hv⟩] constructor · intro hq -- Lift component equivalence to Quot via rootIn! let inv : G.CompAt t → V := Quot.lift (fun a => G.rootIn (G.activeEdges t) a.1) fun a b hab => G.rootIn_eq_of_reach ((G.active_reach_iff t a b).mp hab) have := congrArg inv hq exact (G.active_reach_iff t ⟨u, hu⟩ ⟨v, hv⟩).mpr ((G.rootIn_eq_iff _ _ _).mp this) · exact Quot.sound -- Helper: Φ t is bijective at every t : ℝ have hbij_Φ : ∀ t : ℝ, Function.Bijective (Φ t) := by intro t -- Map each component q : CompAt t to its oldest vertex r(q) : V let compRoot : G.CompAt t → V := Quot.lift (fun a => G.rootIn (G.activeEdges t) a.1) fun a b hab => G.rootIn_eq_of_reach ((G.active_reach_iff t a b).mp hab) have hcompRoot_inj : Function.Injective compRoot := by intro q₁ q₂ heq refine Quot.inductionOn q₁ (fun a => Quot.inductionOn q₂ (fun b h => ?_)) heq exact (hcls_eq_iff t a.1 b.1 a.2 b.2).mpr h -- Proof of injectivity of Φ t by choosing the YOUNGEST active bar with nonzero coefficient! have hinj : Function.Injective (Φ t) := by rw [injective_iff_map_eq_zero] intro x hx by_contra hne -- Collect all vertices whose corresponding basis element has nonzero coefficient in x let S : Finset V := Finset.univ.filter fun v => (∃ hg : G.birth G.globalElder ≤ t, v = G.globalElder ∧ x.1 ⟨hg⟩ ≠ 0) ∨ (∃ e : {e : Fin G.m // G.isKruskalEdge e}, ∃ he : G.birth (G.youngerRoot e.1) ≤ t ∧ t < G.wt e.1, v = G.youngerRoot e.1 ∧ x.2 e ⟨he⟩ ≠ 0) have hSne : S.Nonempty := by by_contra hemp; rw [Finset.not_nonempty_iff_eq_empty] at hemp apply hne apply Prod.ext · funext ⟨hg⟩ have hnot : G.globalElder ∉ S := by rw [hemp]; exact Finset.notMem_empty _ simp only [S, Finset.mem_filter, Finset.mem_univ, true_and, not_or, not_exists, not_and] at hnot by_contra h0; exact hnot.1 hg h0 · funext e ⟨he⟩ have hnot : G.youngerRoot e.1 ∉ S := by rw [hemp]; exact Finset.notMem_empty _ simp only [S, Finset.mem_filter, Finset.mem_univ, true_and, not_or, not_exists, not_and] at hnot by_contra h0; exact hnot.2 e he rfl h0 -- Pick the YOUNGEST vertex vstar ∈ S (maximal in G.older)! have hex_max_gen : ∀ (T : Finset V), T.Nonempty → ∃ vstar ∈ T, ∀ v ∈ T, v = vstar ∨ G.older v vstar := by intro T hT induction T using Finset.induction_on with | empty => exact absurd hT Finset.not_nonempty_empty | @insert a s ha ih => by_cases hs : s.Nonempty · obtain ⟨r, hrm, hrmax⟩ := ih hs rcases G.older_trichotomy a r with har | rfl | hra · refine ⟨r, Finset.mem_insert_of_mem hrm, fun v hv => ?_⟩ rcases Finset.mem_insert.mp hv with rfl | hvs · exact Or.inr har · exact hrmax v hvs · exact absurd hrm ha · refine ⟨a, Finset.mem_insert_self a s, fun v hv => ?_⟩ rcases Finset.mem_insert.mp hv with rfl | hvs · exact Or.inl rfl · rcases hrmax v hvs with rfl | hvr · exact Or.inr hra · exact Or.inr (G.older_trans hvr hra) · rw [Finset.not_nonempty_iff_eq_empty] at hs; subst hs exact ⟨a, Finset.mem_singleton_self a, fun v hv => Or.inl (Finset.mem_singleton.mp hv)⟩ obtain ⟨vstar, hvstar_mem, hvstar_max⟩ := hex_max_gen S hSne -- Evaluate (Φ t x) at the component qstar = [vstar]_t! have hvstar_birth : G.birth vstar ≤ t := by simp only [S, Finset.mem_filter, Finset.mem_univ, true_and] at hvstar_mem rcases hvstar_mem with ⟨hg, rfl, _⟩ | ⟨e, he, rfl, _⟩ · exact hg · exact he.1 have hvstar_root : G.rootIn (G.activeEdges t) vstar = vstar := by rw [hroot_iff] simp only [S, Finset.mem_filter, Finset.mem_univ, true_and] at hvstar_mem rcases hvstar_mem with ⟨_, rfl, _⟩ | ⟨e, he, rfl, _⟩ · exact Or.inl rfl · exact Or.inr ⟨e, rfl, he.2⟩ let qstar : G.CompAt t := Quot.mk _ ⟨vstar, hvstar_birth⟩ have hzero : (Φ t x) qstar = 0 := by rw [hx]; rfl -- Helper: for any active vertex w strictly older than vstar, [w]_t ≠ qstar! have hne_older : ∀ (w : V) (hw : G.birth w ≤ t), G.older w vstar → (Quot.mk _ ⟨w, hw⟩ : G.CompAt t) ≠ qstar := by intro w hw hold hq have hreq := (hcls_eq_iff t w vstar hw hvstar_birth).mp hq rw [hvstar_root] at hreq have hmin := G.rootIn_le (G.activeEdges t) (G.reachIn_refl _ w) rw [hreq] at hmin rcases hmin with rfl | h' · exact G.older_irrefl w hold · exact G.older_asymm hold h' -- Evaluate Φ t x at qstar simp only [Φ, LinearMap.coe_mk, AddHom.coe_mk, Finsupp.add_apply, Finsupp.finsetSum_apply] at hzero simp only [S, Finset.mem_filter, Finset.mem_univ, true_and] at hvstar_mem rcases hvstar_mem with ⟨hg, rfl, hx1_ne⟩ | ⟨estar, hestar, rfl, hx2_ne⟩ · -- vstar = globalElder: every Kruskal term has youngerRoot ≠ globalElder and cannot be in S -- (since globalElder is oldest, so no other vertex is in S, hence x.2 = 0!) have hsum0 : ∑ e : {e : Fin G.m // G.isKruskalEdge e}, (if he : G.birth (G.youngerRoot e.1) ≤ t ∧ t < G.wt e.1 then x.2 e ⟨he⟩ • (cls t (G.youngerRoot e.1) he.1 - cls t (G.elderRoot e.1) ((G.birth_elder_le_younger e.2).trans he.1)) else 0) qstar = 0 := by apply Finset.sum_eq_zero; intro e _ split_ifs with he · by_cases hxe : x.2 e ⟨he⟩ = 0 · simp [hxe] · exfalso have hmem : G.youngerRoot e.1 ∈ S := by simp only [S, Finset.mem_filter, Finset.mem_univ, true_and] exact Or.inr ⟨e, he, rfl, hxe⟩ have hle := hvstar_max _ hmem have hne_g : G.youngerRoot e.1 ≠ G.globalElder := by rw [← hbij e]; exact (bij e).2 rcases hle with h1 | h1 · exact hne_g h1 · rcases G.globalElder_le (G.youngerRoot e.1) with h2 | h2 · exact hne_g h2 · exact G.older_asymm h2 h1 · rfl rw [hsum0, add_zero, dif_pos hg] at hzero simp only [cls, qstar, Finsupp.smul_apply, Finsupp.single_eq_same, smul_eq_mul, mul_one] at hzero exact hx1_ne hzero · -- vstar = youngerRoot estar: globalElder term and all other Kruskal terms vanish at qstar! have hg_term : (if hg : G.birth G.globalElder ≤ t then x.1 ⟨hg⟩ • cls t G.globalElder hg else 0) qstar = 0 := by split_ifs with hg · have hold_g : G.older G.globalElder (G.youngerRoot estar.1) := by rcases G.globalElder_le (G.youngerRoot estar.1) with h1 | h1 · exfalso; rw [← hbij estar] at h1; exact (bij estar).2 h1 · exact h1 have hne_q := hne_older G.globalElder hg hold_g simp only [cls, Finsupp.smul_apply, Finsupp.single_eq_of_ne hne_q.symm, smul_eq_mul, mul_zero] · rfl have hsum_single : (∑ e : {e : Fin G.m // G.isKruskalEdge e}, (if he : G.birth (G.youngerRoot e.1) ≤ t ∧ t < G.wt e.1 then x.2 e ⟨he⟩ • (cls t (G.youngerRoot e.1) he.1 - cls t (G.elderRoot e.1) ((G.birth_elder_le_younger e.2).trans he.1)) else 0) qstar) = x.2 estar ⟨hestar⟩ := by rw [Finset.sum_eq_single estar] · rw [dif_pos hestar] have hne_el := hne_older (G.elderRoot estar.1) ((G.birth_elder_le_younger estar.2).trans hestar.1) (G.elder_older_younger estar.2) simp only [cls, Finsupp.smul_apply, Finsupp.sub_apply, Finsupp.single_eq_of_ne hne_el.symm, sub_zero, smul_eq_mul] change x.2 estar ⟨hestar⟩ * (Finsupp.single qstar (1 : K)) qstar = x.2 estar ⟨hestar⟩ rw [Finsupp.single_eq_same, mul_one] · intro e _ hene split_ifs with he · by_cases hxe : x.2 e ⟨he⟩ = 0 · simp [hxe] · have hmem : G.youngerRoot e.1 ∈ S := by simp only [S, Finset.mem_filter, Finset.mem_univ, true_and] exact Or.inr ⟨e, he, rfl, hxe⟩ have hne_y : G.youngerRoot e.1 ≠ G.youngerRoot estar.1 := by intro hEq; apply hene; apply bij.injective; apply Subtype.ext rw [hbij e, hbij estar, hEq] have hy_old : G.older (G.youngerRoot e.1) (G.youngerRoot estar.1) := by rcases hvstar_max _ hmem with h1 | h1 · exact absurd h1 hne_y · exact h1 have hel_old : G.older (G.elderRoot e.1) (G.youngerRoot estar.1) := G.older_trans (G.elder_older_younger e.2) hy_old have hq1 := hne_older (G.youngerRoot e.1) he.1 hy_old have hq2 := hne_older (G.elderRoot e.1) ((G.birth_elder_le_younger e.2).trans he.1) hel_old simp only [cls, Finsupp.smul_apply, Finsupp.sub_apply, Finsupp.single_eq_of_ne hq1.symm, Finsupp.single_eq_of_ne hq2.symm, sub_self, smul_eq_mul, mul_zero] · rfl · intro h; exact absurd (Finset.mem_univ estar) h rw [hg_term, zero_add, hsum_single] at hzero exact hx2_ne hzero -- Proof of surjectivity of Φ t by strong induction on vertex rank in G.older! have hsurj : Function.Surjective (Φ t) := by -- It suffices to show every generator `cls t v hv` lies in `LinearMap.range (Φ t)` have hgen : ∀ (n : ℕ) (v : V) (hv : G.birth v ≤ t), (Finset.univ.filter fun u : V => G.older u v).card ≤ n → cls t v hv ∈ LinearMap.range (Φ t) := by intro n induction n with | zero => intro v hv hcnt -- No vertex is older than v, so v = globalElder! have hvg : v = G.globalElder := by rcases G.globalElder_le v with rfl | hold · rfl · exfalso have hmem : G.globalElder ∈ Finset.univ.filter fun u : V => G.older u v := by simp [hold] have hpos : 0 < (Finset.univ.filter fun u : V => G.older u v).card := Finset.card_pos.mpr ⟨G.globalElder, hmem⟩ omega subst hvg refine ⟨(fun _ => 1, fun _ _ => 0), ?_⟩ simp [Φ, hv] | succ n ih => intro v hv hcnt -- Replace v by its component root r = rootIn (activeEdges t) v set r := G.rootIn (G.activeEdges t) v have hr_le : v = r ∨ G.older r v := G.rootIn_le (G.activeEdges t) (G.reachIn_refl _ v) have hr_birth : G.birth r ≤ t := by rcases hr_le with heq | hold · rw [← heq]; exact hv · exact (G.birth_le_of_older hold).trans hv have hcls_eq : cls t v hv = cls t r hr_birth := by unfold cls; congr 1 exact (hcls_eq_iff t v r hv hr_birth).mpr (G.rootIn_eq_of_reach (G.rootIn_reach (G.activeEdges t) v)) rw [hcls_eq] have hr_root : G.rootIn (G.activeEdges t) r = r := (G.rootIn_eq_of_reach (G.rootIn_reach (G.activeEdges t) v)).symm rcases (hroot_iff t r).mp hr_root with hr_g | ⟨e, he_r, hwt_gt⟩ · refine ⟨(fun _ => 1, fun _ _ => 0), ?_⟩ have hg_b : G.birth G.globalElder ≤ t := by rw [← hr_g]; exact hr_birth simp only [Φ, LinearMap.coe_mk, AddHom.coe_mk, dif_pos hg_b, one_smul, zero_smul, dite_eq_ite, ite_self, Finset.sum_const_zero, add_zero] congr 1; exact hr_g.symm · -- `cls t r = (cls t r - cls t (elderRoot e)) + cls t (elderRoot e)` have hel_old_r : G.older (G.elderRoot e.1) r := by rw [← he_r]; exact G.elder_older_younger e.2 have hel_old_v : G.older (G.elderRoot e.1) v := by rcases hr_le with heq | hrv · rw [heq]; exact hel_old_r · exact G.older_trans hel_old_r hrv have hel_birth : G.birth (G.elderRoot e.1) ≤ t := (G.birth_le_of_older hel_old_r).trans hr_birth have hsub_cnt : (Finset.univ.filter fun u : V => G.older u (G.elderRoot e.1)).card ≤ n := by have hssub : (Finset.univ.filter fun u : V => G.older u (G.elderRoot e.1)) ⊂ (Finset.univ.filter fun u : V => G.older u v) := by rw [Finset.ssubset_iff_subset_ne] refine ⟨fun u hu => ?_, fun heq => ?_⟩ · simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hu ⊢ exact G.older_trans hu hel_old_v · have hmem : G.elderRoot e.1 ∈ Finset.univ.filter fun u : V => G.older u v := by simp [hel_old_v] rw [← heq] at hmem simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hmem exact G.older_irrefl _ hmem have := Finset.card_lt_card hssub omega have h1 : cls t (G.elderRoot e.1) hel_birth ∈ LinearMap.range (Φ t) := ih (G.elderRoot e.1) hel_birth hsub_cnt have he_active : G.birth (G.youngerRoot e.1) ≤ t ∧ t < G.wt e.1 := ⟨by rw [he_r]; exact hr_birth, hwt_gt⟩ have h2 : cls t r hr_birth - cls t (G.elderRoot e.1) hel_birth ∈ LinearMap.range (Φ t) := by refine ⟨(0, fun e' _ => if e' = e then 1 else 0), ?_⟩ simp only [Φ, LinearMap.coe_mk, AddHom.coe_mk, Pi.zero_apply, zero_smul, dite_eq_ite, ite_self, zero_add] rw [Finset.sum_eq_single e] · simp [he_active, he_r, hr_birth] · intro e' _ hene; split_ifs <;> simp · intro h; exact absurd (Finset.mem_univ e) h have hsum : cls t r hr_birth = (cls t r hr_birth - cls t (G.elderRoot e.1) hel_birth) + cls t (G.elderRoot e.1) hel_birth := by abel rw [hsum] exact Submodule.add_mem _ h2 h1 intro y refine Finsupp.induction_linear y ?_ ?_ ?_ · exact ⟨0, map_zero _⟩ · rintro f₁ f₂ ⟨x₁, rfl⟩ ⟨x₂, rfl⟩; exact ⟨x₁ + x₂, map_add _ _ _⟩ · intro q c refine Quot.inductionOn q fun ⟨v, hv⟩ => ?_ obtain ⟨x, hx⟩ := hgen (Fintype.card V) v hv (by calc _ ≤ (Finset.univ : Finset V).card := Finset.card_filter_le _ _ _ = Fintype.card V := Finset.card_univ) refine ⟨c • x, ?_⟩ rw [map_smul, hx] dsimp only [cls] rw [Finsupp.smul_single, smul_eq_mul, mul_one] exact ⟨hinj, hsurj⟩ -- Assemble the family of LinearEquivs and prove naturality for all s ≤ t! refine ⟨fun t => LinearEquiv.ofBijective (Φ t) (hbij_Φ t), ?_⟩ intro s t hst apply LinearMap.ext; intro x change Φ t (G.elderBarcodeMap K hst x) = G.h0Map K hst (Φ s x) simp only [Φ, elderBarcodeMap, h0Map, LinearMap.coe_mk, AddHom.coe_mk, map_add, map_sum] have hcls_map : ∀ (v : V) (hv : G.birth v ≤ s), Finsupp.lmapDomain K K (G.compMap hst) (cls s v hv) = cls t v (hv.trans hst) := by intro v hv unfold cls; rw [Finsupp.lmapDomain_apply, Finsupp.mapDomain_single]; rfl congr 1 · by_cases hgs : G.birth G.globalElder ≤ s · have hgt : G.birth G.globalElder ≤ t := hgs.trans hst rw [dif_pos hgt, dif_pos hgs, dif_pos hgs, map_smul, hcls_map] · by_cases hgt : G.birth G.globalElder ≤ t <;> simp [hgs, hgt] · apply Finset.sum_congr rfl; intro e _ by_cases hs : G.birth (G.youngerRoot e.1) ≤ s ∧ s < G.wt e.1 · by_cases ht : G.birth (G.youngerRoot e.1) ≤ t ∧ t < G.wt e.1 · rw [dif_pos ht, dif_pos hs, dif_pos hs, map_smul, map_sub, hcls_map, hcls_map] · -- Bar died between s and t: wt e ≤ t, so youngerRoot and elderRoot are in the SAME component at t! rw [dif_neg ht, dif_pos hs, map_smul, map_sub, hcls_map, hcls_map] have hwt_le : G.wt e.1 ≤ t := by by_contra hgt; push Not at hgt exact ht ⟨hs.1.trans hst, hgt⟩ have heq_cls : cls t (G.youngerRoot e.1) (hs.1.trans hst) = cls t (G.elderRoot e.1) (((G.birth_elder_le_younger e.2).trans hs.1).trans hst) := by unfold cls; congr 1 apply (hcls_eq_iff t _ _ _ _).mpr rw [G.rootIn_eq_iff] obtain ⟨kt, _, hkt_eq, hkt_iff⟩ := G.activeEdges_eq_prefixEdges t rw [hkt_eq] have hsub : G.prefixEdges (e.1.val + 1) ⊆ G.prefixEdges kt := by intro z hz; simp only [prefixEdges, Finset.mem_filter, Finset.mem_univ, true_and] at hz ⊢ have : e.1.val < kt := (hkt_iff e.1).mpr hwt_le omega exact G.reachIn_symm (G.reachIn_mono hsub (G.reach_after_kruskalEdge e.1)) rw [heq_cls, sub_self, smul_zero] · by_cases ht : G.birth (G.youngerRoot e.1) ≤ t ∧ t < G.wt e.1 <;> simp [hs, ht] end FilteredGraph /-! ### Part 2: Sharp `p`-Wasserstein Persistence Distances & Metric Axioms -/ /-- A finite half-open persistence bar `[b, d)` with `b < d`. -/ structure Bar where b : ℝ d : ℝ hlt : b < d /-- A finite persistence diagram. -/ structure Diagram where n : ℕ bar : Fin n → Bar def Diagram.empty : Diagram where n := 0 bar := Fin.elim0 def barDist (p q : Bar) : ℝ := max |p.b - q.b| |p.d - q.d| noncomputable def diagDist (p : Bar) : ℝ := (p.d - p.b) / 2 lemma barDist_nonneg (p q : Bar) : 0 ≤ barDist p q := le_max_of_le_left (abs_nonneg _) lemma barDist_self (p : Bar) : barDist p p = 0 := by simp [barDist] lemma barDist_symm (p q : Bar) : barDist p q = barDist q p := by simp [barDist, abs_sub_comm] lemma barDist_eq_zero_iff (p q : Bar) : barDist p q = 0 ↔ p = q := by constructor · intro h have h1 : |p.b - q.b| ≤ 0 := by rw [← h]; exact le_max_left _ _ have h2 : |p.d - q.d| ≤ 0 := by rw [← h]; exact le_max_right _ _ have hb : p.b = q.b := sub_eq_zero.mp (abs_nonpos_iff.mp h1) have hd : p.d = q.d := sub_eq_zero.mp (abs_nonpos_iff.mp h2) cases p; cases q; simp only at hb hd; subst hb; subst hd; rfl · rintro rfl; exact barDist_self p lemma barDist_triangle (p q r : Bar) : barDist p r ≤ barDist p q + barDist q r := by unfold barDist apply max_le · calc |p.b - r.b| ≤ |p.b - q.b| + |q.b - r.b| := abs_sub_le _ _ _ _ ≤ max |p.b - q.b| |p.d - q.d| + max |q.b - r.b| |q.d - r.d| := add_le_add (le_max_left _ _) (le_max_left _ _) · calc |p.d - r.d| ≤ |p.d - q.d| + |q.d - r.d| := abs_sub_le _ _ _ _ ≤ max |p.b - q.b| |p.d - q.d| + max |q.b - r.b| |q.d - r.d| := add_le_add (le_max_right _ _) (le_max_right _ _) lemma diagDist_pos (p : Bar) : 0 < diagDist p := by unfold diagDist; linarith [p.hlt] lemma diagDist_nonneg (p : Bar) : 0 ≤ diagDist p := (diagDist_pos p).le lemma diagDist_le_barDist_add_diagDist (p q : Bar) : diagDist p ≤ barDist p q + diagDist q := by unfold diagDist barDist have h1 : q.b - p.b ≤ max |p.b - q.b| |p.d - q.d| := le_trans (by rw [abs_sub_comm]; exact le_abs_self (q.b - p.b)) (le_max_left _ _) have h2 : p.d - q.d ≤ max |p.b - q.b| |p.d - q.d| := le_trans (le_abs_self _) (le_max_right _ _) linarith /-- A partial matching between finite persistence diagrams `D₁` and `D₂`. -/ structure PartialMatching (D₁ D₂ : Diagram) where toFun : Fin D₁.n → Option (Fin D₂.n) inj : ∀ {i₁ i₂ : Fin D₁.n} {j : Fin D₂.n}, toFun i₁ = some j → toFun i₂ = some j → i₁ = i₂ namespace PartialMatching def empty (D₁ D₂ : Diagram) : PartialMatching D₁ D₂ where toFun _ := none inj h := by cases h def id (D : Diagram) : PartialMatching D D where toFun i := some i inj h1 h2 := Option.some.inj (h1.trans h2.symm) /-- Inverse partial matching `M⁻¹ : D₂ ⇀ D₁`. -/ noncomputable def symm {D₁ D₂ : Diagram} (M : PartialMatching D₁ D₂) : PartialMatching D₂ D₁ := by classical exact { toFun := fun j => if h : ∃ i : Fin D₁.n, M.toFun i = some j then some (Classical.choose h) else none inj := fun {j₁ j₂ i} h1 h2 => by by_cases hex1 : ∃ i' : Fin D₁.n, M.toFun i' = some j₁ · rw [dif_pos hex1] at h1 by_cases hex2 : ∃ i' : Fin D₁.n, M.toFun i' = some j₂ · rw [dif_pos hex2] at h2 have hi1 : Classical.choose hex1 = i := Option.some.inj h1 have hi2 : Classical.choose hex2 = i := Option.some.inj h2 have h1' := Classical.choose_spec hex1 have h2' := Classical.choose_spec hex2 rw [hi1] at h1'; rw [hi2] at h2' exact Option.some.inj (h1'.symm.trans h2') · rw [dif_neg hex2] at h2; cases h2 · rw [dif_neg hex1] at h1; cases h1 } /-- Composition of partial matchings `M₂₃ ∘ M₁₂ : D₁ ⇀ D₃`. -/ def comp {D₁ D₂ D₃ : Diagram} (M₁₂ : PartialMatching D₁ D₂) (M₂₃ : PartialMatching D₂ D₃) : PartialMatching D₁ D₃ where toFun i := match M₁₂.toFun i with | some j => M₂₃.toFun j | none => none inj {i₁ i₂ k} h1 h2 := by cases hm1 : M₁₂.toFun i₁ with | none => rw [hm1] at h1; cases h1 | some j₁ => cases hm2 : M₁₂.toFun i₂ with | none => rw [hm2] at h2; cases h2 | some j₂ => rw [hm1] at h1; rw [hm2] at h2 have hj : j₁ = j₂ := M₂₃.inj h1 h2 subst hj exact M₁₂.inj hm1 hm2 instance instFinite (D₁ D₂ : Diagram) : Finite (PartialMatching D₁ D₂) := Finite.of_injective (fun M => M.toFun) fun M₁ M₂ h => by cases M₁; cases M₂; simp at h; subst h; rfl /-- Exact `p`-th power of the `p`-Wasserstein cost of a partial matching `M`. -/ noncomputable def pCostPow {D₁ D₂ : Diagram} (M : PartialMatching D₁ D₂) (p : ℝ) : ℝ := by classical exact (∑ i : Fin D₁.n, (match M.toFun i with | some j => barDist (D₁.bar i) (D₂.bar j) | none => diagDist (D₁.bar i)) ^ p) + (∑ j : Fin D₂.n, if ∀ i : Fin D₁.n, M.toFun i ≠ some j then (diagDist (D₂.bar j)) ^ p else 0) /-- Exact `p`-Wasserstein cost `cost_p(M) = (pCostPow M p)^(1/p)`. -/ noncomputable def pCost {D₁ D₂ : Diagram} (M : PartialMatching D₁ D₂) (p : ℝ) : ℝ := (M.pCostPow p) ^ (1 / p) /-- Exact `∞`-cost (bottleneck cost) of a partial matching `M`. -/ noncomputable def infCost {D₁ D₂ : Diagram} (M : PartialMatching D₁ D₂) : ℝ := by classical let c1 : ℝ := Finset.univ.fold max 0 fun i : Fin D₁.n => match M.toFun i with | some j => barDist (D₁.bar i) (D₂.bar j) | none => diagDist (D₁.bar i) let c2 : ℝ := Finset.univ.fold max 0 fun j : Fin D₂.n => if ∀ i : Fin D₁.n, M.toFun i ≠ some j then diagDist (D₂.bar j) else 0 exact max c1 c2 lemma pCostPow_nonneg {D₁ D₂ : Diagram} (M : PartialMatching D₁ D₂) (p : ℝ) : 0 ≤ M.pCostPow p := by classical unfold pCostPow apply add_nonneg · apply Finset.sum_nonneg; intro i _ cases M.toFun i with | none => exact Real.rpow_nonneg (diagDist_nonneg _) p | some j => exact Real.rpow_nonneg (barDist_nonneg _ _) p · apply Finset.sum_nonneg; intro j _ split_ifs · exact Real.rpow_nonneg (diagDist_nonneg _) p · exact le_rfl lemma pCost_nonneg {D₁ D₂ : Diagram} (M : PartialMatching D₁ D₂) (p : ℝ) : 0 ≤ M.pCost p := Real.rpow_nonneg (M.pCostPow_nonneg p) _ lemma fold_max_le {α : Type*} [DecidableEq α] (s : Finset α) (f : α → ℝ) (z ε : ℝ) (hz : z ≤ ε) (hf : ∀ a ∈ s, f a ≤ ε) : s.fold max z f ≤ ε := by induction s using Finset.induction_on with | empty => simpa using hz | @insert a s ha ih => rw [Finset.fold_insert ha] exact max_le (hf a (Finset.mem_insert_self a s)) (ih fun b hb => hf b (Finset.mem_insert_of_mem hb)) lemma le_fold_max_init {α : Type*} [DecidableEq α] (s : Finset α) (f : α → ℝ) (z : ℝ) : z ≤ s.fold max z f := by induction s using Finset.induction_on with | empty => simp | @insert a s ha ih => rw [Finset.fold_insert ha]; exact le_trans ih (le_max_right _ _) lemma le_fold_max_of_mem {α : Type*} [DecidableEq α] (s : Finset α) (f : α → ℝ) (z : ℝ) {a : α} (ha : a ∈ s) : f a ≤ s.fold max z f := by induction s using Finset.induction_on with | empty => exact absurd ha (Finset.notMem_empty a) | @insert b s hb ih => rw [Finset.fold_insert hb] rcases Finset.mem_insert.mp ha with rfl | hmem · exact le_max_left _ _ · exact le_trans (ih hmem) (le_max_right _ _) lemma infCost_nonneg {D₁ D₂ : Diagram} (M : PartialMatching D₁ D₂) : 0 ≤ M.infCost := by classical unfold infCost; exact le_trans (le_fold_max_init Finset.univ _ 0) (le_max_left _ _) lemma pCostPow_symm {D₁ D₂ : Diagram} (M : PartialMatching D₁ D₂) (p : ℝ) : M.symm.pCostPow p = M.pCostPow p := by classical unfold pCostPow -- Split LHS sum over j into matched (∃ i, M i = some j) and unmatched, and similarly on RHS have h1 : (∑ j : Fin D₂.n, (match M.symm.toFun j with | some i => barDist (D₂.bar j) (D₁.bar i) | none => diagDist (D₂.bar j)) ^ p) = (∑ j ∈ Finset.univ.filter (fun j : Fin D₂.n => ∃ i, M.toFun i = some j), (match M.symm.toFun j with | some i => barDist (D₂.bar j) (D₁.bar i) | none => diagDist (D₂.bar j)) ^ p) + (∑ j : Fin D₂.n, if ∀ i : Fin D₁.n, M.toFun i ≠ some j then (diagDist (D₂.bar j)) ^ p else 0) := by rw [← Finset.sum_filter_add_sum_filter_not Finset.univ (fun j : Fin D₂.n => ∃ i, M.toFun i = some j)] congr 1 rw [Finset.sum_filter] apply Finset.sum_congr rfl; intro j _ by_cases hex : ∃ i : Fin D₁.n, M.toFun i = some j · have hnot : ¬ (∀ i : Fin D₁.n, M.toFun i ≠ some j) := by rcases hex with ⟨i, hi⟩; intro h; exact h i hi rw [if_neg (not_not.mpr hex), if_neg hnot] · have hall : ∀ i : Fin D₁.n, M.toFun i ≠ some j := fun i hi => hex ⟨i, hi⟩ rw [if_pos hex, if_pos hall] have hnone : M.symm.toFun j = none := by simp [symm, hex] rw [hnone] have h2 : (∑ i : Fin D₁.n, (match M.toFun i with | some j => barDist (D₁.bar i) (D₂.bar j) | none => diagDist (D₁.bar i)) ^ p) = (∑ i ∈ Finset.univ.filter (fun i : Fin D₁.n => ∃ j, M.toFun i = some j), (match M.toFun i with | some j => barDist (D₁.bar i) (D₂.bar j) | none => diagDist (D₁.bar i)) ^ p) + (∑ i : Fin D₁.n, if ∀ j : Fin D₂.n, M.symm.toFun j ≠ some i then (diagDist (D₁.bar i)) ^ p else 0) := by rw [← Finset.sum_filter_add_sum_filter_not Finset.univ (fun i : Fin D₁.n => ∃ j, M.toFun i = some j)] congr 1 rw [Finset.sum_filter] apply Finset.sum_congr rfl; intro i _ cases hmi : M.toFun i with | none => have hex : ¬ ∃ j, M.toFun i = some j := by simp [hmi] have hall : ∀ j : Fin D₂.n, M.symm.toFun j ≠ some i := by intro j hsym simp only [symm] at hsym by_cases hj : ∃ i', M.toFun i' = some j · rw [dif_pos hj] at hsym have hi := Option.some.inj hsym have hsp := Classical.choose_spec hj rw [hi, hmi] at hsp; cases hsp · rw [dif_neg hj] at hsym; cases hsym simp [hall] | some j => have hnot : ¬ (∀ j' : Fin D₂.n, M.symm.toFun j' ≠ some i) := by intro hall; apply hall j have hj : ∃ i', M.toFun i' = some j := ⟨i, hmi⟩ show (if h : ∃ i', M.toFun i' = some j then some (Classical.choose h) else none) = some i rw [dif_pos hj] exact congrArg some (M.inj (Classical.choose_spec hj) hmi) simp [hnot] have hmatch_eq : (∑ j ∈ Finset.univ.filter (fun j : Fin D₂.n => ∃ i, M.toFun i = some j), (match M.symm.toFun j with | some i => barDist (D₂.bar j) (D₁.bar i) | none => diagDist (D₂.bar j)) ^ p) = (∑ i ∈ Finset.univ.filter (fun i : Fin D₁.n => ∃ j, M.toFun i = some j), (match M.toFun i with | some j => barDist (D₁.bar i) (D₂.bar j) | none => diagDist (D₁.bar i)) ^ p) := by apply Finset.sum_bij (fun j hj => Classical.choose (Finset.mem_filter.mp hj).2) · intro j hj exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, j, Classical.choose_spec (Finset.mem_filter.mp hj).2⟩ · intro j₁ hj₁ j₂ hj₂ heq have h1 := Classical.choose_spec (Finset.mem_filter.mp hj₁).2 have h2 := Classical.choose_spec (Finset.mem_filter.mp hj₂).2 rw [heq] at h1 exact Option.some.inj (h1.symm.trans h2) · intro i hi rcases (Finset.mem_filter.mp hi).2 with ⟨j, hmi⟩ have hj : j ∈ Finset.univ.filter (fun j : Fin D₂.n => ∃ i, M.toFun i = some j) := Finset.mem_filter.mpr ⟨Finset.mem_univ _, i, hmi⟩ refine ⟨j, hj, M.inj (Classical.choose_spec (Finset.mem_filter.mp hj).2) hmi⟩ · intro j hj have hex := (Finset.mem_filter.mp hj).2 have hsym : M.symm.toFun j = some (Classical.choose hex) := by simp [symm, hex] have hmi : M.toFun (Classical.choose hex) = some j := Classical.choose_spec hex rw [hsym, hmi]; dsimp only; rw [barDist_symm] rw [h1, h2, hmatch_eq]; ring /-- Key `ℓ_p` triangle inequality for partial matching composition via Minkowski's inequality. -/ lemma pCost_comp_le {D₁ D₂ D₃ : Diagram} (M₁₂ : PartialMatching D₁ D₂) (M₂₃ : PartialMatching D₂ D₃) {p : ℝ} (hp : 1 ≤ p) : (M₁₂.comp M₂₃).pCost p ≤ M₁₂.pCost p + M₂₃.pCost p := by classical -- Index all terms on the common finite type `Fin D₁.n ⊕ Fin D₃.n`: -- - On `inl i`: cost in `M₁₂.comp M₂₃` ≤ `a (inl i) + b (inl i)` -- - On `inr k`: cost in `M₁₂.comp M₂₃` ≤ `a (inr k) + b (inr k)` let c : Fin D₁.n ⊕ Fin D₃.n → ℝ := fun s => match s with | Sum.inl i => match (M₁₂.comp M₂₃).toFun i with | some k => barDist (D₁.bar i) (D₃.bar k) | none => diagDist (D₁.bar i) | Sum.inr k => if ∀ i : Fin D₁.n, (M₁₂.comp M₂₃).toFun i ≠ some k then diagDist (D₃.bar k) else 0 let a : Fin D₁.n ⊕ Fin D₃.n → ℝ := fun s => match s with | Sum.inl i => match M₁₂.toFun i with | some j => barDist (D₁.bar i) (D₂.bar j) | none => diagDist (D₁.bar i) | Sum.inr k => if ∀ i : Fin D₁.n, (M₁₂.comp M₂₃).toFun i ≠ some k then match M₂₃.symm.toFun k with | some j => diagDist (D₂.bar j) | none => 0 else 0 let b : Fin D₁.n ⊕ Fin D₃.n → ℝ := fun s => match s with | Sum.inl i => match M₁₂.toFun i with | some j => match M₂₃.toFun j with | some k => barDist (D₂.bar j) (D₃.bar k) | none => diagDist (D₂.bar j) | none => 0 | Sum.inr k => if ∀ i : Fin D₁.n, (M₁₂.comp M₂₃).toFun i ≠ some k then match M₂₃.symm.toFun k with | some j => barDist (D₂.bar j) (D₃.bar k) | none => diagDist (D₃.bar k) else 0 have ha_nn : ∀ s, 0 ≤ a s := by rintro (i | k) · dsimp [a]; cases M₁₂.toFun i with | none => exact diagDist_nonneg _ | some j => exact barDist_nonneg _ _ · dsimp [a]; split_ifs · cases M₂₃.symm.toFun k with | none => exact le_rfl | some j => exact diagDist_nonneg _ · exact le_rfl have hb_nn : ∀ s, 0 ≤ b s := by rintro (i | k) · dsimp [b]; cases M₁₂.toFun i with | none => exact le_rfl | some j => dsimp only; cases M₂₃.toFun j with | none => exact diagDist_nonneg _ | some k => exact barDist_nonneg _ _ · dsimp [b]; split_ifs · cases M₂₃.symm.toFun k with | none => exact diagDist_nonneg _ | some j => exact barDist_nonneg _ _ · exact le_rfl have hc_nn : ∀ s, 0 ≤ c s := by rintro (i | k) · dsimp [c]; cases (M₁₂.comp M₂₃).toFun i with | none => exact diagDist_nonneg _ | some k => exact barDist_nonneg _ _ · dsimp [c]; split_ifs · exact diagDist_nonneg _ · exact le_rfl have hc_le : ∀ s, c s ≤ a s + b s := by rintro (i | k) · dsimp only [c, a, b, comp] cases h1 : M₁₂.toFun i with | none => simp | some j => dsimp only cases h2 : M₂₃.toFun j with | none => exact diagDist_le_barDist_add_diagDist _ _ | some k => exact barDist_triangle _ _ _ · dsimp only [c, a, b] split_ifs with hk · cases hsym : M₂₃.symm.toFun k with | none => simp | some j => dsimp only rw [add_comm, barDist_symm] exact diagDist_le_barDist_add_diagDist _ _ · simp have hp_pos : 0 < p := by linarith -- Sum of c^p equals (M₁₂.comp M₂₃).pCostPow p have hc_sum : (∑ s : Fin D₁.n ⊕ Fin D₃.n, (c s) ^ p) = (M₁₂.comp M₂₃).pCostPow p := by rw [Fintype.sum_sum_type]; unfold pCostPow; congr 1 apply Finset.sum_congr rfl; intro k _ dsimp only [c]; split_ifs · rfl · exact Real.zero_rpow hp_pos.ne' -- Sum of a^p ≤ M₁₂.pCostPow p have ha_sum : (∑ s : Fin D₁.n ⊕ Fin D₃.n, (a s) ^ p) ≤ M₁₂.pCostPow p := by rw [Fintype.sum_sum_type]; unfold pCostPow apply add_le_add le_rfl -- Map each k with a (inr k) > 0 injectively via M₂₃.symm to an unmatched j in M₁₂! let S_k : Finset (Fin D₃.n) := Finset.univ.filter fun k => (∀ i : Fin D₁.n, (M₁₂.comp M₂₃).toFun i ≠ some k) ∧ (∃ j : Fin D₂.n, M₂₃.toFun j = some k) -- Clean direct injection from S_k into unmatched j of M₁₂ have hle_filter : (∑ k : Fin D₃.n, (a (Sum.inr k)) ^ p) = ∑ k ∈ S_k, (match M₂₃.symm.toFun k with | some j => diagDist (D₂.bar j) | none => 0) ^ p := by rw [← Finset.sum_filter_add_sum_filter_not Finset.univ (fun k => k ∈ S_k)] have hrest : (∑ k ∈ Finset.univ.filter (fun k => k ∉ S_k), (a (Sum.inr k)) ^ p) = 0 := by apply Finset.sum_eq_zero; intro k hk have hnot : k ∉ S_k := (Finset.mem_filter.mp hk).2 dsimp only [a] by_cases h1 : ∀ i : Fin D₁.n, (M₁₂.comp M₂₃).toFun i ≠ some k · rw [if_pos h1] have h2 : ¬ ∃ j : Fin D₂.n, M₂₃.toFun j = some k := fun hex => hnot (Finset.mem_filter.mpr ⟨Finset.mem_univ _, h1, hex⟩) have hnone : M₂₃.symm.toFun k = none := by simp [symm, h2] rw [hnone, Real.zero_rpow hp_pos.ne'] · rw [if_neg h1, Real.zero_rpow hp_pos.ne'] rw [hrest, add_zero] have hfeq : Finset.univ.filter (fun k => k ∈ S_k) = S_k := by ext k; simp rw [hfeq] apply Finset.sum_congr rfl; intro k hk dsimp only [a]; rw [if_pos (Finset.mem_filter.mp hk).2.1] rw [hle_filter] have hsum_inj : (∑ k ∈ S_k, (match M₂₃.symm.toFun k with | some j => diagDist (D₂.bar j) | none => 0) ^ p) ≤ ∑ j : Fin D₂.n, if ∀ i : Fin D₁.n, M₁₂.toFun i ≠ some j then (diagDist (D₂.bar j)) ^ p else 0 := by let g : ∀ k ∈ S_k, Fin D₂.n := fun k hk => Classical.choose (Finset.mem_filter.mp hk).2.2 have hg_unm : ∀ k (hk : k ∈ S_k), ∀ i : Fin D₁.n, M₁₂.toFun i ≠ some (g k hk) := by intro k hk i hmi have hj : M₂₃.toFun (g k hk) = some k := Classical.choose_spec (Finset.mem_filter.mp hk).2.2 have hcomp : (M₁₂.comp M₂₃).toFun i = some k := by simp [comp, hmi, hj] exact (Finset.mem_filter.mp hk).2.1 i hcomp have hg_inj : ∀ k₁ (hk₁ : k₁ ∈ S_k) k₂ (hk₂ : k₂ ∈ S_k), g k₁ hk₁ = g k₂ hk₂ → k₁ = k₂ := by intro k₁ hk₁ k₂ hk₂ heq have h1 : M₂₃.toFun (g k₁ hk₁) = some k₁ := Classical.choose_spec (Finset.mem_filter.mp hk₁).2.2 have h2 : M₂₃.toFun (g k₂ hk₂) = some k₂ := Classical.choose_spec (Finset.mem_filter.mp hk₂).2.2 rw [heq] at h1; exact Option.some.inj (h1.symm.trans h2) -- Rewrite LHS as sum over the image finset `S_k.attach.image (fun x => g x.1 x.2) ⊆ Finset.univ` let imgJ : Finset (Fin D₂.n) := S_k.attach.image fun x => g x.1 x.2 calc (∑ k ∈ S_k, (match M₂₃.symm.toFun k with | some j => diagDist (D₂.bar j) | none => 0) ^ p) = ∑ x ∈ S_k.attach, (diagDist (D₂.bar (g x.1 x.2))) ^ p := by rw [← Finset.sum_attach S_k] apply Finset.sum_congr rfl; intro ⟨k, hk⟩ _ have hex := (Finset.mem_filter.mp hk).2.2 have hsym : M₂₃.symm.toFun k = some (g k hk) := by simp [symm, hex, g] rw [hsym] _ = ∑ j ∈ imgJ, (if ∀ i : Fin D₁.n, M₁₂.toFun i ≠ some j then (diagDist (D₂.bar j)) ^ p else 0) := by rw [Finset.sum_image] · apply Finset.sum_congr rfl; intro ⟨k, hk⟩ _ rw [if_pos (hg_unm k hk)] · intro ⟨k₁, hk₁⟩ _ ⟨k₂, hk₂⟩ _ heq exact Subtype.ext (hg_inj k₁ hk₁ k₂ hk₂ heq) _ ≤ ∑ j : Fin D₂.n, if ∀ i : Fin D₁.n, M₁₂.toFun i ≠ some j then (diagDist (D₂.bar j)) ^ p else 0 := by apply Finset.sum_le_univ_sum_of_nonneg intro j; split_ifs · exact Real.rpow_nonneg (diagDist_nonneg _) p · exact le_rfl exact hsum_inj -- Sum of b^p ≤ M₂₃.pCostPow p have hb_sum : (∑ s : Fin D₁.n ⊕ Fin D₃.n, (b s) ^ p) ≤ M₂₃.pCostPow p := by rw [Fintype.sum_sum_type]; unfold pCostPow -- Split `∑ k, (b (inr k))^p` into those matched in M₂₃ (mapped to j = M₂₃.symm k, disjoint from M₁₂'s range!) -- and those unmatched in M₂₃ (bounded by the unmatched D₃ sum of M₂₃)! let T_match : Finset (Fin D₃.n) := Finset.univ.filter fun k => (∀ i : Fin D₁.n, (M₁₂.comp M₂₃).toFun i ≠ some k) ∧ (∃ j : Fin D₂.n, M₂₃.toFun j = some k) let T_unm : Finset (Fin D₃.n) := Finset.univ.filter fun k => ∀ j : Fin D₂.n, M₂₃.toFun j ≠ some k have hsplit_b3 : (∑ k : Fin D₃.n, (b (Sum.inr k)) ^ p) ≤ (∑ k ∈ T_match, (b (Sum.inr k)) ^ p) + (∑ k : Fin D₃.n, if ∀ j : Fin D₂.n, M₂₃.toFun j ≠ some k then (diagDist (D₃.bar k)) ^ p else 0) := by rw [← Finset.sum_filter_add_sum_filter_not Finset.univ (fun k => k ∈ T_match)] have hfeq : Finset.univ.filter (fun k => k ∈ T_match) = T_match := by ext k; simp rw [hfeq] apply add_le_add le_rfl rw [Finset.sum_filter] apply Finset.sum_le_sum; intro k _ by_cases htm : k ∈ T_match · rw [if_neg (not_not.mpr htm)] split_ifs · exact Real.rpow_nonneg (diagDist_nonneg _) p · exact le_rfl · rw [if_pos htm] dsimp only [b] by_cases h1 : ∀ i : Fin D₁.n, (M₁₂.comp M₂₃).toFun i ≠ some k · rw [if_pos h1] have h2 : ∀ j : Fin D₂.n, M₂₃.toFun j ≠ some k := by intro j hj; exact htm (Finset.mem_filter.mpr ⟨Finset.mem_univ _, h1, j, hj⟩) have hnone : M₂₃.symm.toFun k = none := by have hnex : ¬ ∃ j, M₂₃.toFun j = some k := fun ⟨j, hj⟩ => h2 j hj simp [symm, hnex] rw [hnone, if_pos h2] · rw [if_neg h1, Real.zero_rpow hp_pos.ne'] split_ifs · exact Real.rpow_nonneg (diagDist_nonneg _) p · exact le_rfl -- Now combine `∑ i, (b (inl i))^p + ∑ k ∈ T_match, (b (inr k))^p ≤ ∑ j : Fin D₂.n, cost_j^p`! let S_i : Finset (Fin D₁.n) := Finset.univ.filter fun i => ∃ j, M₁₂.toFun i = some j let g1 : ∀ i ∈ S_i, Fin D₂.n := fun i hi => Classical.choose (Finset.mem_filter.mp hi).2 let g2 : ∀ k ∈ T_match, Fin D₂.n := fun k hk => Classical.choose (Finset.mem_filter.mp hk).2.2 let img1 : Finset (Fin D₂.n) := S_i.attach.image fun x => g1 x.1 x.2 let img2 : Finset (Fin D₂.n) := T_match.attach.image fun x => g2 x.1 x.2 have hdisj : Disjoint img1 img2 := by rw [Finset.disjoint_left] intro j hj1 hj2 rcases Finset.mem_image.mp hj1 with ⟨⟨i, hi⟩, _, rfl⟩ rcases Finset.mem_image.mp hj2 with ⟨⟨k, hk⟩, _, hjeq⟩ have hmi : M₁₂.toFun i = some (g1 i hi) := Classical.choose_spec (Finset.mem_filter.mp hi).2 have hmk : M₂₃.toFun (g2 k hk) = some k := Classical.choose_spec (Finset.mem_filter.mp hk).2.2 dsimp only at hjeq rw [hjeq] at hmk have hcomp : (M₁₂.comp M₂₃).toFun i = some k := by simp [comp, hmi, hmk] exact (Finset.mem_filter.mp hk).2.1 i hcomp have hsum1 : (∑ i : Fin D₁.n, (b (Sum.inl i)) ^ p) = ∑ j ∈ img1, (match M₂₃.toFun j with | some k => barDist (D₂.bar j) (D₃.bar k) | none => diagDist (D₂.bar j)) ^ p := by have hfilt : (∑ i : Fin D₁.n, (b (Sum.inl i)) ^ p) = ∑ i ∈ S_i, (b (Sum.inl i)) ^ p := by rw [← Finset.sum_filter_add_sum_filter_not Finset.univ (fun i => i ∈ S_i)] have hfeq : Finset.univ.filter (fun i => i ∈ S_i) = S_i := by ext i; simp rw [hfeq] have hz : (∑ i ∈ Finset.univ.filter (fun i => i ∉ S_i), (b (Sum.inl i)) ^ p) = 0 := by apply Finset.sum_eq_zero; intro i hi have hnot : i ∉ S_i := (Finset.mem_filter.mp hi).2 cases hmi : M₁₂.toFun i with | none => dsimp only [b]; rw [hmi, Real.zero_rpow hp_pos.ne'] | some j => exact absurd (Finset.mem_filter.mpr ⟨Finset.mem_univ _, j, hmi⟩) hnot rw [hz, add_zero] rw [hfilt, ← Finset.sum_attach S_i, Finset.sum_image] · apply Finset.sum_congr rfl; intro ⟨i, hi⟩ _ have hmi : M₁₂.toFun i = some (g1 i hi) := Classical.choose_spec (Finset.mem_filter.mp hi).2 dsimp only [b]; rw [hmi] · intro ⟨i₁, hi₁⟩ _ ⟨i₂, hi₂⟩ _ heq have h1 : M₁₂.toFun i₁ = some (g1 i₁ hi₁) := Classical.choose_spec (Finset.mem_filter.mp hi₁).2 have h2 : M₁₂.toFun i₂ = some (g1 i₂ hi₂) := Classical.choose_spec (Finset.mem_filter.mp hi₂).2 dsimp only at heq rw [heq] at h1; exact Subtype.ext (M₁₂.inj h1 h2) have hsum2 : (∑ k ∈ T_match, (b (Sum.inr k)) ^ p) = ∑ j ∈ img2, (match M₂₃.toFun j with | some k => barDist (D₂.bar j) (D₃.bar k) | none => diagDist (D₂.bar j)) ^ p := by rw [← Finset.sum_attach T_match, Finset.sum_image] · apply Finset.sum_congr rfl; intro ⟨k, hk⟩ _ have h1 := (Finset.mem_filter.mp hk).2.1 have hex := (Finset.mem_filter.mp hk).2.2 have hmk : M₂₃.toFun (g2 k hk) = some k := Classical.choose_spec hex have hsym : M₂₃.symm.toFun k = some (g2 k hk) := by simp [symm, hex, g2] dsimp only [b]; rw [if_pos h1, hsym, hmk] · intro ⟨k₁, hk₁⟩ _ ⟨k₂, hk₂⟩ _ heq have h1 : M₂₃.toFun (g2 k₁ hk₁) = some k₁ := Classical.choose_spec (Finset.mem_filter.mp hk₁).2.2 have h2 : M₂₃.toFun (g2 k₂ hk₂) = some k₂ := Classical.choose_spec (Finset.mem_filter.mp hk₂).2.2 dsimp only at heq rw [heq] at h1; exact Subtype.ext (Option.some.inj (h1.symm.trans h2)) have hcomb : (∑ i : Fin D₁.n, (b (Sum.inl i)) ^ p) + (∑ k ∈ T_match, (b (Sum.inr k)) ^ p) ≤ ∑ j : Fin D₂.n, (match M₂₃.toFun j with | some k => barDist (D₂.bar j) (D₃.bar k) | none => diagDist (D₂.bar j)) ^ p := by rw [hsum1, hsum2, ← Finset.sum_union hdisj] apply Finset.sum_le_univ_sum_of_nonneg intro j; cases M₂₃.toFun j with | none => exact Real.rpow_nonneg (diagDist_nonneg _) p | some k => exact Real.rpow_nonneg (barDist_nonneg _ _) p linarith -- Apply Minkowski's inequality (`Real.Lp_add_le`)! have hmink : (∑ s : Fin D₁.n ⊕ Fin D₃.n, (a s + b s) ^ p) ^ (1 / p) ≤ (∑ s : Fin D₁.n ⊕ Fin D₃.n, (a s) ^ p) ^ (1 / p) + (∑ s : Fin D₁.n ⊕ Fin D₃.n, (b s) ^ p) ^ (1 / p) := by have h := Real.Lp_add_le (s := Finset.univ) (f := a) (g := b) hp simpa [abs_of_nonneg (ha_nn _), abs_of_nonneg (hb_nn _), abs_of_nonneg (add_nonneg (ha_nn _) (hb_nn _))] using h have hc_pow_le : (∑ s : Fin D₁.n ⊕ Fin D₃.n, (c s) ^ p) ≤ ∑ s : Fin D₁.n ⊕ Fin D₃.n, (a s + b s) ^ p := Finset.sum_le_sum fun s _ => Real.rpow_le_rpow (hc_nn s) (hc_le s) hp_pos.le have hinv_nn : (0 : ℝ) ≤ 1 / p := by positivity calc (M₁₂.comp M₂₃).pCost p = (∑ s : Fin D₁.n ⊕ Fin D₃.n, (c s) ^ p) ^ (1 / p) := by unfold pCost; rw [hc_sum] _ ≤ (∑ s : Fin D₁.n ⊕ Fin D₃.n, (a s + b s) ^ p) ^ (1 / p) := Real.rpow_le_rpow (Finset.sum_nonneg fun s _ => Real.rpow_nonneg (hc_nn s) p) hc_pow_le hinv_nn _ ≤ (∑ s : Fin D₁.n ⊕ Fin D₃.n, (a s) ^ p) ^ (1 / p) + (∑ s : Fin D₁.n ⊕ Fin D₃.n, (b s) ^ p) ^ (1 / p) := hmink _ ≤ M₁₂.pCost p + M₂₃.pCost p := add_le_add (Real.rpow_le_rpow (Finset.sum_nonneg fun s _ => Real.rpow_nonneg (ha_nn s) p) ha_sum hinv_nn) (Real.rpow_le_rpow (Finset.sum_nonneg fun s _ => Real.rpow_nonneg (hb_nn s) p) hb_sum hinv_nn) end PartialMatching /-- Exact `p`-Wasserstein persistence distance `W_p(D₁, D₂)` (`p ≥ 1`). -/ noncomputable def wassersteinDist (p : ℝ) (D₁ D₂ : Diagram) : ℝ := sInf {c : ℝ | ∃ M : PartialMatching D₁ D₂, M.pCost p = c} /-- Exact bottleneck persistence distance `d_B(D₁, D₂) = W_∞(D₁, D₂)`. -/ noncomputable def bottleneckDist (D₁ D₂ : Diagram) : ℝ := sInf {c : ℝ | ∃ M : PartialMatching D₁ D₂, M.infCost = c} theorem exists_optimal_wasserstein_matching (p : ℝ) (D₁ D₂ : Diagram) : ∃ M : PartialMatching D₁ D₂, M.pCost p = wassersteinDist p D₁ D₂ := by classical let _ : Fintype (PartialMatching D₁ D₂) := Fintype.ofFinite _ have hne : (Finset.univ : Finset (PartialMatching D₁ D₂)).Nonempty := ⟨PartialMatching.empty D₁ D₂, Finset.mem_univ _⟩ obtain ⟨Mstar, _, hmin⟩ := Finset.exists_min_image Finset.univ (fun M => M.pCost p) hne refine ⟨Mstar, le_antisymm ?_ ?_⟩ · apply le_csInf · exact ⟨Mstar.pCost p, Mstar, rfl⟩ · rintro _ ⟨M, rfl⟩; exact hmin M (Finset.mem_univ M) · apply csInf_le · refine ⟨0, ?_⟩; rintro _ ⟨M, rfl⟩; exact M.pCost_nonneg p · exact ⟨Mstar, rfl⟩ theorem exists_optimal_bottleneck_matching (D₁ D₂ : Diagram) : ∃ M : PartialMatching D₁ D₂, M.infCost = bottleneckDist D₁ D₂ := by classical let _ : Fintype (PartialMatching D₁ D₂) := Fintype.ofFinite _ have hne : (Finset.univ : Finset (PartialMatching D₁ D₂)).Nonempty := ⟨PartialMatching.empty D₁ D₂, Finset.mem_univ _⟩ obtain ⟨Mstar, _, hmin⟩ := Finset.exists_min_image Finset.univ PartialMatching.infCost hne refine ⟨Mstar, le_antisymm ?_ ?_⟩ · apply le_csInf · exact ⟨Mstar.infCost, Mstar, rfl⟩ · rintro _ ⟨M, rfl⟩; exact hmin M (Finset.mem_univ M) · apply csInf_le · refine ⟨0, ?_⟩; rintro _ ⟨M, rfl⟩; exact M.infCost_nonneg · exact ⟨Mstar, rfl⟩ lemma wassersteinDist_le_pCost (p : ℝ) {D₁ D₂ : Diagram} (M : PartialMatching D₁ D₂) : wassersteinDist p D₁ D₂ ≤ M.pCost p := by apply csInf_le · refine ⟨0, ?_⟩; rintro _ ⟨M', rfl⟩; exact M'.pCost_nonneg p · exact ⟨M, rfl⟩ lemma bottleneckDist_le_infCost {D₁ D₂ : Diagram} (M : PartialMatching D₁ D₂) : bottleneckDist D₁ D₂ ≤ M.infCost := by apply csInf_le · refine ⟨0, ?_⟩; rintro _ ⟨M', rfl⟩; exact M'.infCost_nonneg · exact ⟨M, rfl⟩ /-! ### Metric Axioms for `W_p` (`p ≥ 1`) -/ theorem wassersteinDist_nonneg (p : ℝ) (D₁ D₂ : Diagram) : 0 ≤ wassersteinDist p D₁ D₂ := by obtain ⟨M, hM⟩ := exists_optimal_wasserstein_matching p D₁ D₂ rw [← hM]; exact M.pCost_nonneg p theorem wassersteinDist_self {p : ℝ} (hp : 1 ≤ p) (D : Diagram) : wassersteinDist p D D = 0 := by classical apply le_antisymm _ (wassersteinDist_nonneg p D D) have h0 : (PartialMatching.id D).pCost p = 0 := by have hp_pos : 0 < p := by linarith unfold PartialMatching.pCost PartialMatching.pCostPow have hpow0 : (∑ i : Fin D.n, (match (PartialMatching.id D).toFun i with | some j => barDist (D.bar i) (D.bar j) | none => diagDist (D.bar i)) ^ p) + (∑ j : Fin D.n, if ∀ i : Fin D.n, (PartialMatching.id D).toFun i ≠ some j then (diagDist (D.bar j)) ^ p else 0) = 0 := by have h1 : (∑ i : Fin D.n, (barDist (D.bar i) (D.bar i)) ^ p) = 0 := by apply Finset.sum_eq_zero; intro i _ rw [barDist_self, Real.zero_rpow hp_pos.ne'] have h2 : (∑ j : Fin D.n, if ∀ i : Fin D.n, (some i : Option (Fin D.n)) ≠ some j then (diagDist (D.bar j)) ^ p else 0) = 0 := by apply Finset.sum_eq_zero; intro j _ rw [if_neg (fun h => h j rfl)] simpa [PartialMatching.id] using congrArg₂ (· + ·) h1 h2 rw [hpow0, Real.zero_rpow (by positivity)] rw [← h0]; exact wassersteinDist_le_pCost p (PartialMatching.id D) theorem wassersteinDist_eq_zero_iff {p : ℝ} (hp : 1 ≤ p) (D₁ D₂ : Diagram) : wassersteinDist p D₁ D₂ = 0 ↔ ∃ e : Fin D₁.n ≃ Fin D₂.n, ∀ i : Fin D₁.n, D₁.bar i = D₂.bar (e i) := by classical have hp_pos : 0 < p := by linarith constructor · intro hzero obtain ⟨M, hM⟩ := exists_optimal_wasserstein_matching p D₁ D₂ rw [hzero] at hM have hpow0 : M.pCostPow p = 0 := by by_contra hne have hpos : 0 < M.pCostPow p := lt_of_le_of_ne (M.pCostPow_nonneg p) (Ne.symm hne) have := Real.rpow_pos_of_pos hpos (1 / p) change M.pCost p = 0 at hM unfold PartialMatching.pCost at hM; linarith unfold PartialMatching.pCostPow at hpow0 have hsum1_nn : 0 ≤ ∑ i : Fin D₁.n, (match M.toFun i with | some j => barDist (D₁.bar i) (D₂.bar j) | none => diagDist (D₁.bar i)) ^ p := Finset.sum_nonneg fun i _ => by cases M.toFun i <;> [exact Real.rpow_nonneg (diagDist_nonneg _) p; exact Real.rpow_nonneg (barDist_nonneg _ _) p] have hsum2_nn : 0 ≤ ∑ j : Fin D₂.n, if ∀ i : Fin D₁.n, M.toFun i ≠ some j then (diagDist (D₂.bar j)) ^ p else 0 := Finset.sum_nonneg fun j _ => by split_ifs · exact Real.rpow_nonneg (diagDist_nonneg _) p · exact le_rfl have h1 : ∀ i : Fin D₁.n, (match M.toFun i with | some j => barDist (D₁.bar i) (D₂.bar j) | none => diagDist (D₁.bar i)) ^ p = 0 := by have hz : ∑ i : Fin D₁.n, (match M.toFun i with | some j => barDist (D₁.bar i) (D₂.bar j) | none => diagDist (D₁.bar i)) ^ p = 0 := by linarith exact fun i => (Finset.sum_eq_zero_iff_of_nonneg (fun k _ => by cases M.toFun k <;> [exact Real.rpow_nonneg (diagDist_nonneg _) p; exact Real.rpow_nonneg (barDist_nonneg _ _) p])).mp hz i (Finset.mem_univ i) have h2 : ∀ j : Fin D₂.n, (if ∀ i : Fin D₁.n, M.toFun i ≠ some j then (diagDist (D₂.bar j)) ^ p else 0) = 0 := by have hz : ∑ j : Fin D₂.n, (if ∀ i : Fin D₁.n, M.toFun i ≠ some j then (diagDist (D₂.bar j)) ^ p else 0) = 0 := by linarith exact fun j => (Finset.sum_eq_zero_iff_of_nonneg (fun k _ => by split_ifs · exact Real.rpow_nonneg (diagDist_nonneg _) p · exact le_rfl)).mp hz j (Finset.mem_univ j) have hall1 : ∀ i : Fin D₁.n, ∃ j : Fin D₂.n, M.toFun i = some j ∧ D₁.bar i = D₂.bar j := by intro i have hi := h1 i cases hmi : M.toFun i with | none => rw [hmi] at hi have := Real.rpow_pos_of_pos (diagDist_pos (D₁.bar i)) p linarith | some j => rw [hmi] at hi have hbd0 : barDist (D₁.bar i) (D₂.bar j) = 0 := by by_contra hne have hpos : 0 < barDist (D₁.bar i) (D₂.bar j) := lt_of_le_of_ne (barDist_nonneg _ _) (Ne.symm hne) have := Real.rpow_pos_of_pos hpos p; linarith exact ⟨j, rfl, (barDist_eq_zero_iff _ _).mp hbd0⟩ have hall2 : ∀ j : Fin D₂.n, ∃ i : Fin D₁.n, M.toFun i = some j := by intro j by_contra hnex push Not at hnex have hj := h2 j rw [if_pos hnex] at hj have := Real.rpow_pos_of_pos (diagDist_pos (D₂.bar j)) p linarith let f : Fin D₁.n → Fin D₂.n := fun i => Classical.choose (hall1 i) have hf_spec : ∀ i, M.toFun i = some (f i) ∧ D₁.bar i = D₂.bar (f i) := fun i => Classical.choose_spec (hall1 i) have hf_inj : Function.Injective f := fun i₁ i₂ heq => M.inj (hf_spec i₁).1 (by rw [heq]; exact (hf_spec i₂).1) have hf_surj : Function.Surjective f := fun j => by obtain ⟨i, hi⟩ := hall2 j refine ⟨i, Option.some.inj ((hf_spec i).1.symm.trans hi)⟩ exact ⟨Equiv.ofBijective f ⟨hf_inj, hf_surj⟩, fun i => (hf_spec i).2⟩ · rintro ⟨e, he⟩ apply le_antisymm _ (wassersteinDist_nonneg p D₁ D₂) let M : PartialMatching D₁ D₂ := { toFun := fun i => some (e i) inj := fun h1 h2 => e.injective (Option.some.inj (h1.trans h2.symm)) } have h0 : M.pCost p = 0 := by unfold PartialMatching.pCost PartialMatching.pCostPow have hpow0 : (∑ i : Fin D₁.n, (barDist (D₁.bar i) (D₂.bar (e i))) ^ p) + (∑ j : Fin D₂.n, if ∀ i : Fin D₁.n, (some (e i) : Option (Fin D₂.n)) ≠ some j then (diagDist (D₂.bar j)) ^ p else 0) = 0 := by have h1 : (∑ i : Fin D₁.n, (barDist (D₁.bar i) (D₂.bar (e i))) ^ p) = 0 := by apply Finset.sum_eq_zero; intro i _ rw [he i, barDist_self, Real.zero_rpow hp_pos.ne'] have h2 : (∑ j : Fin D₂.n, if ∀ i : Fin D₁.n, (some (e i) : Option (Fin D₂.n)) ≠ some j then (diagDist (D₂.bar j)) ^ p else 0) = 0 := by apply Finset.sum_eq_zero; intro j _ rw [if_neg (fun h => h (e.symm j) (by simp))] linarith change ((∑ i : Fin D₁.n, (barDist (D₁.bar i) (D₂.bar (e i))) ^ p) + (∑ j : Fin D₂.n, if ∀ i : Fin D₁.n, (some (e i) : Option (Fin D₂.n)) ≠ some j then (diagDist (D₂.bar j)) ^ p else 0)) ^ (1 / p) = 0 rw [hpow0, Real.zero_rpow (by positivity)] rw [← h0]; exact wassersteinDist_le_pCost p M theorem wassersteinDist_symm (p : ℝ) (D₁ D₂ : Diagram) : wassersteinDist p D₁ D₂ = wassersteinDist p D₂ D₁ := by have hle : ∀ (A B : Diagram), wassersteinDist p A B ≤ wassersteinDist p B A := by intro A B obtain ⟨M, hM⟩ := exists_optimal_wasserstein_matching p B A rw [← hM] calc wassersteinDist p A B ≤ M.symm.pCost p := wassersteinDist_le_pCost p M.symm _ = M.pCost p := by unfold PartialMatching.pCost; rw [M.pCostPow_symm p] exact le_antisymm (hle D₁ D₂) (hle D₂ D₁) /-- **Exact Triangle Inequality for `p`-Wasserstein Persistence Distance (`p ≥ 1`)**: `W_p(D₁, D₃) ≤ W_p(D₁, D₂) + W_p(D₂, D₃)` via matching composition and Minkowski's inequality. -/ theorem wassersteinDist_triangle {p : ℝ} (hp : 1 ≤ p) (D₁ D₂ D₃ : Diagram) : wassersteinDist p D₁ D₃ ≤ wassersteinDist p D₁ D₂ + wassersteinDist p D₂ D₃ := by obtain ⟨M₁₂, h12⟩ := exists_optimal_wasserstein_matching p D₁ D₂ obtain ⟨M₂₃, h23⟩ := exists_optimal_wasserstein_matching p D₂ D₃ rw [← h12, ← h23] exact le_trans (wassersteinDist_le_pCost p (M₁₂.comp M₂₃)) (M₁₂.pCost_comp_le M₂₃ hp) /-! ### Part 3: Sharp Monotonicity Hierarchy (`d_B ≤ W_q ≤ W_p`) & Exact `N`-Bar Separations -/ /-- Elementary `ℓ_q ≤ ℓ_p` inequality for finite sums of nonnegative reals (`1 ≤ p ≤ q`). -/ lemma rpow_sum_le_rpow_sum {ι : Type*} (s : Finset ι) (f : ι → ℝ) (hf : ∀ i ∈ s, 0 ≤ f i) {p q : ℝ} (hp : 1 ≤ p) (hpq : p ≤ q) : (∑ i ∈ s, (f i) ^ q) ^ (1 / q) ≤ (∑ i ∈ s, (f i) ^ p) ^ (1 / p) := by have hp_pos : 0 < p := by linarith have hq_pos : 0 < q := by linarith set S := (∑ i ∈ s, (f i) ^ p) ^ (1 / p) have hsum_nn : 0 ≤ ∑ i ∈ s, (f i) ^ p := Finset.sum_nonneg fun i hi => Real.rpow_nonneg (hf i hi) p by_cases hS0 : S = 0 · have hsum0 : ∑ i ∈ s, (f i) ^ p = 0 := by by_contra hne have hpos : 0 < ∑ i ∈ s, (f i) ^ p := lt_of_le_of_ne hsum_nn (Ne.symm hne) have := Real.rpow_pos_of_pos hpos (1 / p); linarith have hf0 : ∀ i ∈ s, f i = 0 := by intro i hi have hi0 := (Finset.sum_eq_zero_iff_of_nonneg (fun k hk => Real.rpow_nonneg (hf k hk) p)).mp hsum0 i hi by_contra hne have hpos : 0 < f i := lt_of_le_of_ne (hf i hi) (Ne.symm hne) have := Real.rpow_pos_of_pos hpos p; linarith have hq0 : ∑ i ∈ s, (f i) ^ q = 0 := Finset.sum_eq_zero fun i hi => by rw [hf0 i hi, Real.zero_rpow hq_pos.ne'] rw [hq0, Real.zero_rpow (by positivity), hS0] · have hS_pos : 0 < S := lt_of_le_of_ne (Real.rpow_nonneg hsum_nn _) (Ne.symm hS0) have hS_pow : S ^ p = ∑ i ∈ s, (f i) ^ p := by dsimp only [S]; rw [← Real.rpow_mul hsum_nn, one_div_mul_cancel hp_pos.ne', Real.rpow_one] have hle1 : ∑ i ∈ s, (f i / S) ^ q ≤ 1 := by calc ∑ i ∈ s, (f i / S) ^ q ≤ ∑ i ∈ s, (f i / S) ^ p := by apply Finset.sum_le_sum; intro i hi have hdiv_nn : 0 ≤ f i / S := div_nonneg (hf i hi) hS_pos.le have hdiv_le1 : f i / S ≤ 1 := by rw [div_le_one hS_pos] have hterm : (f i) ^ p ≤ ∑ k ∈ s, (f k) ^ p := Finset.single_le_sum (fun k hk => Real.rpow_nonneg (hf k hk) p) hi rw [← hS_pow] at hterm exact (Real.rpow_le_rpow_iff (hf i hi) hS_pos.le hp_pos).mp hterm exact Real.rpow_le_rpow_of_exponent_ge' hdiv_nn hdiv_le1 hp_pos.le hpq _ = (∑ i ∈ s, (f i) ^ p) / S ^ p := by rw [Finset.sum_div]; apply Finset.sum_congr rfl; intro i hi exact Real.div_rpow (hf i hi) hS_pos.le p _ = 1 := by rw [← hS_pow, div_self (Real.rpow_pos_of_pos hS_pos p).ne'] have hsum_q : ∑ i ∈ s, (f i) ^ q ≤ S ^ q := by have hmul := mul_le_mul_of_nonneg_right hle1 (Real.rpow_pos_of_pos hS_pos q).le rw [one_mul, Finset.sum_mul] at hmul have heq : ∑ i ∈ s, (f i / S) ^ q * S ^ q = ∑ i ∈ s, (f i) ^ q := by apply Finset.sum_congr rfl; intro i hi rw [Real.div_rpow (hf i hi) hS_pos.le q, div_mul_cancel₀ _ (Real.rpow_pos_of_pos hS_pos q).ne'] rwa [heq] at hmul calc (∑ i ∈ s, (f i) ^ q) ^ (1 / q) ≤ (S ^ q) ^ (1 / q) := Real.rpow_le_rpow (Finset.sum_nonneg fun i hi => Real.rpow_nonneg (hf i hi) q) hsum_q (by positivity) _ = S := by rw [← Real.rpow_mul hS_pos.le, mul_one_div_cancel hq_pos.ne', Real.rpow_one] /-- **Sharp Monotonicity (`d_B ≤ W_p`)**: For every `p ≥ 1` and all finite diagrams `D₁, D₂`, `d_B(D₁, D₂) ≤ W_p(D₁, D₂)`. -/ theorem bottleneckDist_le_wassersteinDist {p : ℝ} (hp : 1 ≤ p) (D₁ D₂ : Diagram) : bottleneckDist D₁ D₂ ≤ wassersteinDist p D₁ D₂ := by classical have hp_pos : 0 < p := by linarith obtain ⟨M, hM⟩ := exists_optimal_wasserstein_matching p D₁ D₂ rw [← hM] refine le_trans (bottleneckDist_le_infCost M) ?_ have hle_p : M.infCost ≤ M.pCost p := by unfold PartialMatching.infCost PartialMatching.pCost PartialMatching.pCostPow apply max_le · apply PartialMatching.fold_max_le _ _ _ _ (M.pCost_nonneg p) intro i _ set x := match M.toFun i with | some j => barDist (D₁.bar i) (D₂.bar j) | none => diagDist (D₁.bar i) have hx_nn : 0 ≤ x := by dsimp only [x]; cases M.toFun i <;> [exact diagDist_nonneg _; exact barDist_nonneg _ _] have hterm : x ^ p ≤ (∑ k : Fin D₁.n, (match M.toFun k with | some j => barDist (D₁.bar k) (D₂.bar j) | none => diagDist (D₁.bar k)) ^ p) + (∑ j : Fin D₂.n, if ∀ k : Fin D₁.n, M.toFun k ≠ some j then (diagDist (D₂.bar j)) ^ p else 0) := by have h1 : x ^ p ≤ ∑ k : Fin D₁.n, (match M.toFun k with | some j => barDist (D₁.bar k) (D₂.bar j) | none => diagDist (D₁.bar k)) ^ p := Finset.single_le_sum (f := fun k : Fin D₁.n => (match M.toFun k with | some j => barDist (D₁.bar k) (D₂.bar j) | none => diagDist (D₁.bar k)) ^ p) (fun k _ => by cases M.toFun k <;> [exact Real.rpow_nonneg (diagDist_nonneg _) p; exact Real.rpow_nonneg (barDist_nonneg _ _) p]) (Finset.mem_univ i) have h2 : 0 ≤ ∑ j : Fin D₂.n, if ∀ k : Fin D₁.n, M.toFun k ≠ some j then (diagDist (D₂.bar j)) ^ p else 0 := Finset.sum_nonneg fun j _ => by split_ifs; exact Real.rpow_nonneg (diagDist_nonneg _) p; exact le_rfl linarith calc x = (x ^ p) ^ (1 / p) := by rw [← Real.rpow_mul hx_nn, mul_one_div_cancel hp_pos.ne', Real.rpow_one] _ ≤ _ := Real.rpow_le_rpow (Real.rpow_nonneg hx_nn p) hterm (by positivity) · apply PartialMatching.fold_max_le _ _ _ _ (M.pCost_nonneg p) intro j _ by_cases hj : ∀ i : Fin D₁.n, M.toFun i ≠ some j · rw [if_pos hj] have hx_nn : 0 ≤ diagDist (D₂.bar j) := diagDist_nonneg _ have hterm : (diagDist (D₂.bar j)) ^ p ≤ (∑ k : Fin D₁.n, (match M.toFun k with | some j' => barDist (D₁.bar k) (D₂.bar j') | none => diagDist (D₁.bar k)) ^ p) + (∑ j' : Fin D₂.n, if ∀ k : Fin D₁.n, M.toFun k ≠ some j' then (diagDist (D₂.bar j')) ^ p else 0) := by have h1 : 0 ≤ ∑ k : Fin D₁.n, (match M.toFun k with | some j' => barDist (D₁.bar k) (D₂.bar j') | none => diagDist (D₁.bar k)) ^ p := Finset.sum_nonneg fun k _ => by cases M.toFun k <;> [exact Real.rpow_nonneg (diagDist_nonneg _) p; exact Real.rpow_nonneg (barDist_nonneg _ _) p] have h2 : (diagDist (D₂.bar j)) ^ p ≤ ∑ j' : Fin D₂.n, if ∀ k : Fin D₁.n, M.toFun k ≠ some j' then (diagDist (D₂.bar j')) ^ p else 0 := by have := Finset.single_le_sum (f := fun j' : Fin D₂.n => if ∀ k : Fin D₁.n, M.toFun k ≠ some j' then (diagDist (D₂.bar j')) ^ p else 0) (fun j' _ => by split_ifs; exact Real.rpow_nonneg (diagDist_nonneg _) p; exact le_rfl) (Finset.mem_univ j) rwa [if_pos hj] at this linarith calc diagDist (D₂.bar j) = ((diagDist (D₂.bar j)) ^ p) ^ (1 / p) := by rw [← Real.rpow_mul hx_nn, mul_one_div_cancel hp_pos.ne', Real.rpow_one] _ ≤ _ := Real.rpow_le_rpow (Real.rpow_nonneg hx_nn p) hterm (by positivity) · rw [if_neg hj]; exact M.pCost_nonneg p exact hle_p /-- **Sharp Monotonicity (`W_q ≤ W_p` for `1 ≤ p ≤ q`)**: For all `1 ≤ p ≤ q` and all finite diagrams `D₁, D₂`, `W_q(D₁, D₂) ≤ W_p(D₁, D₂)`. -/ theorem wassersteinDist_anti {p q : ℝ} (hp : 1 ≤ p) (hpq : p ≤ q) (D₁ D₂ : Diagram) : wassersteinDist q D₁ D₂ ≤ wassersteinDist p D₁ D₂ := by classical have hp_pos : 0 < p := by linarith have hq_pos : 0 < q := by linarith obtain ⟨M, hM⟩ := exists_optimal_wasserstein_matching p D₁ D₂ rw [← hM] refine le_trans (wassersteinDist_le_pCost q M) ?_ let f : Fin D₁.n ⊕ Fin D₂.n → ℝ := fun s => match s with | Sum.inl i => match M.toFun i with | some j => barDist (D₁.bar i) (D₂.bar j) | none => diagDist (D₁.bar i) | Sum.inr j => if ∀ i : Fin D₁.n, M.toFun i ≠ some j then diagDist (D₂.bar j) else 0 have hf_nn : ∀ s ∈ (Finset.univ : Finset (Fin D₁.n ⊕ Fin D₂.n)), 0 ≤ f s := by rintro (i | j) _ · dsimp [f]; cases M.toFun i <;> [exact diagDist_nonneg _; exact barDist_nonneg _ _] · dsimp [f]; split_ifs; exact diagDist_nonneg _; exact le_rfl have hsum_eq : ∀ {r : ℝ}, 0 < r → M.pCostPow r = ∑ s : Fin D₁.n ⊕ Fin D₂.n, (f s) ^ r := by intro r hr rw [Fintype.sum_sum_type]; unfold PartialMatching.pCostPow; congr 1 apply Finset.sum_congr rfl; intro j _ dsimp only [f]; split_ifs · rfl · exact (Real.zero_rpow hr.ne').symm unfold PartialMatching.pCost rw [hsum_eq hq_pos, hsum_eq hp_pos] exact rpow_sum_le_rpow_sum Finset.univ f hf_nn hp hpq /-- **Reverse Cardinality Comparison (`W_p ≤ (|D₁| + |D₂|)^{1/p} d_B`)**: For every `p ≥ 1` and finite diagrams `D₁, D₂`, `W_p(D₁, D₂) ≤ ((D₁.n + D₂.n : ℕ) : ℝ)^(1/p) * d_B(D₁, D₂)`. -/ theorem wassersteinDist_le_card_rpow_mul_bottleneckDist {p : ℝ} (hp : 1 ≤ p) (D₁ D₂ : Diagram) : wassersteinDist p D₁ D₂ ≤ ((D₁.n + D₂.n : ℕ) : ℝ) ^ (1 / p) * bottleneckDist D₁ D₂ := by classical have hp_pos : 0 < p := by linarith obtain ⟨M, hM⟩ := exists_optimal_bottleneck_matching D₁ D₂ rw [← hM] refine le_trans (wassersteinDist_le_pCost p M) ?_ have hc_nn : 0 ≤ M.infCost := M.infCost_nonneg have hpow : M.pCostPow p ≤ ((D₁.n + D₂.n : ℕ) : ℝ) * (M.infCost) ^ p := by unfold PartialMatching.pCostPow have h1 : (∑ i : Fin D₁.n, (match M.toFun i with | some j => barDist (D₁.bar i) (D₂.bar j) | none => diagDist (D₁.bar i)) ^ p) ≤ (D₁.n : ℝ) * (M.infCost) ^ p := by calc _ ≤ ∑ _i : Fin D₁.n, (M.infCost) ^ p := by apply Finset.sum_le_sum; intro i _ apply Real.rpow_le_rpow · cases M.toFun i <;> [exact diagDist_nonneg _; exact barDist_nonneg _ _] · unfold PartialMatching.infCost exact le_trans (PartialMatching.le_fold_max_of_mem Finset.univ (fun k : Fin D₁.n => match M.toFun k with | some j => barDist (D₁.bar k) (D₂.bar j) | none => diagDist (D₁.bar k)) 0 (Finset.mem_univ i)) (le_max_left _ _) · exact hp_pos.le _ = (D₁.n : ℝ) * (M.infCost) ^ p := by simp have h2 : (∑ j : Fin D₂.n, if ∀ i : Fin D₁.n, M.toFun i ≠ some j then (diagDist (D₂.bar j)) ^ p else 0) ≤ (D₂.n : ℝ) * (M.infCost) ^ p := by calc _ ≤ ∑ _j : Fin D₂.n, (M.infCost) ^ p := by apply Finset.sum_le_sum; intro j _ split_ifs with hj · apply Real.rpow_le_rpow (diagDist_nonneg _) _ hp_pos.le unfold PartialMatching.infCost have hle := PartialMatching.le_fold_max_of_mem Finset.univ (fun j' : Fin D₂.n => if ∀ i : Fin D₁.n, M.toFun i ≠ some j' then diagDist (D₂.bar j') else 0) 0 (Finset.mem_univ j) rw [if_pos hj] at hle exact le_trans hle (le_max_right _ _) · exact Real.rpow_nonneg hc_nn p _ = (D₂.n : ℝ) * (M.infCost) ^ p := by simp push_cast; linarith calc M.pCost p = (M.pCostPow p) ^ (1 / p) := rfl _ ≤ (((D₁.n + D₂.n : ℕ) : ℝ) * (M.infCost) ^ p) ^ (1 / p) := Real.rpow_le_rpow (M.pCostPow_nonneg p) hpow (by positivity) _ = ((D₁.n + D₂.n : ℕ) : ℝ) ^ (1 / p) * ((M.infCost) ^ p) ^ (1 / p) := Real.mul_rpow (Nat.cast_nonneg _) (Real.rpow_nonneg hc_nn p) _ = ((D₁.n + D₂.n : ℕ) : ℝ) ^ (1 / p) * M.infCost := by rw [← Real.rpow_mul hc_nn, mul_one_div_cancel hp_pos.ne', Real.rpow_one] /-- Diagram with `N` identical copies of a bar `b`. -/ def constDiagram (N : ℕ) (b : Bar) : Diagram where n := N bar _ := b /-- **Exact `N`-Bar Separation Witness (`W_p = N^{1/p} d_B` and `W_p = N^{1/p - 1/q} W_q`)**: For every `N ≥ 1`, `r > 0`, and `1 ≤ p ≤ q`, the diagram `D_N = {[0, 2r)}^{⊕ N}` against `∅` satisfies exact equalities `d_B = r`, `W_p = N^{1/p} r = N^{1/p} d_B`, and `W_p = N^{1/p - 1/q} W_q`. -/ theorem wasserstein_exact_N_bar_separation {N : ℕ} (hN : 1 ≤ N) {r : ℝ} (hr : 0 < r) {p q : ℝ} (hp : 1 ≤ p) (hpq : p ≤ q) : let D := constDiagram N ⟨0, 2 * r, by linarith⟩ bottleneckDist D Diagram.empty = r ∧ wassersteinDist p D Diagram.empty = (N : ℝ) ^ (1 / p) * r ∧ wassersteinDist p D Diagram.empty = (N : ℝ) ^ (1 / p) * bottleneckDist D Diagram.empty ∧ wassersteinDist p D Diagram.empty = (N : ℝ) ^ (1 / p - 1 / q) * wassersteinDist q D Diagram.empty := by classical dsimp only set D := constDiagram N ⟨0, 2 * r, by linarith⟩ have huniq : ∀ M : PartialMatching D Diagram.empty, M.toFun = fun _ => none := by intro M; funext i cases h : M.toFun i with | none => rfl | some j => exact Fin.elim0 j have hdb : bottleneckDist D Diagram.empty = r := by obtain ⟨M, hM⟩ := exists_optimal_bottleneck_matching D Diagram.empty rw [← hM] have hMn := huniq M unfold PartialMatching.infCost have h2 : (Finset.univ.fold max 0 fun j : Fin Diagram.empty.n => if ∀ i : Fin D.n, M.toFun i ≠ some j then diagDist (Diagram.empty.bar j) else 0) = 0 := by have : (Finset.univ : Finset (Fin Diagram.empty.n)) = ∅ := by ext j; exact Fin.elim0 j rw [this]; rfl rw [h2, max_eq_left (PartialMatching.le_fold_max_init _ _ _), hMn] dsimp only apply le_antisymm · apply PartialMatching.fold_max_le _ _ _ _ hr.le intro i _; unfold diagDist D constDiagram; dsimp; linarith · have h0 := PartialMatching.le_fold_max_of_mem Finset.univ (fun i : Fin D.n => diagDist (D.bar i)) 0 (Finset.mem_univ (⟨0, by dsimp [D, constDiagram]; omega⟩ : Fin D.n)) have h0' : diagDist (D.bar ⟨0, by dsimp [D, constDiagram]; omega⟩) = r := by unfold diagDist D constDiagram; dsimp; ring rwa [h0'] at h0 have hwp : ∀ {s : ℝ}, 1 ≤ s → wassersteinDist s D Diagram.empty = (N : ℝ) ^ (1 / s) * r := by intro s hs have hs_pos : 0 < s := by linarith obtain ⟨M, hM⟩ := exists_optimal_wasserstein_matching s D Diagram.empty rw [← hM] have hMn := huniq M have hpow : M.pCostPow s = (N : ℝ) * r ^ s := by unfold PartialMatching.pCostPow have h2 : (∑ j : Fin Diagram.empty.n, if ∀ i : Fin D.n, M.toFun i ≠ some j then (diagDist (Diagram.empty.bar j)) ^ s else 0) = 0 := by have : (Finset.univ : Finset (Fin Diagram.empty.n)) = ∅ := by ext j; exact Fin.elim0 j rw [this, Finset.sum_empty] rw [h2, add_zero, hMn] dsimp only have h1 : ∀ i : Fin D.n, (diagDist (D.bar i)) ^ s = r ^ s := by intro i; unfold diagDist D constDiagram; dsimp; congr 1; ring simp only [h1, Finset.sum_const, Finset.card_univ, Fintype.card_fin, nsmul_eq_mul] rfl unfold PartialMatching.pCost rw [hpow, Real.mul_rpow (Nat.cast_nonneg N) (Real.rpow_nonneg hr.le s), ← Real.rpow_mul hr.le, mul_one_div_cancel hs_pos.ne', Real.rpow_one] have hwp_val := hwp hp have hwq_val := hwp (hp.trans hpq) have hN_pos : (0 : ℝ) < N := Nat.cast_pos.mpr (by omega) refine ⟨hdb, hwp_val, ?_, ?_⟩ · rw [hwp_val, hdb] · rw [hwp_val, hwq_val, ← mul_assoc, ← Real.rpow_add hN_pos, sub_add_cancel] /-! ### Part 4: Sharp `W_1`-vs.-`d_B` Stability Separation on Filtered Complexes (`P_{2N+1}`) -/ /-- The alternating valley–peak filtered path graph `P_{2N+1}` on `Fin (2*N + 1)`: - Even vertices `2k` (valleys) born at `-ε`; - Odd vertices `2k+1` (peaks) born at `+ε`; - Path edges `(i, i+1)` (`i < 2N`) with sublevel weight `max(f i, f (i+1)) = +ε`. -/ def altPathGraph (N : ℕ) {ε : ℝ} (hε : 0 < ε) : FilteredGraph (Fin (2 * N + 1)) where birth v := if v.val % 2 = 0 then -ε else ε m := 2 * N src e := ⟨e.val, by omega⟩ dst e := ⟨e.val + 1, by omega⟩ wt _ := ε hbirth_src e := by split_ifs <;> linarith hbirth_dst e := by split_ifs <;> linarith hsorted _ _ _ := le_rfl lemma altPathGraph_connected (N : ℕ) {ε : ℝ} (hε : 0 < ε) : (altPathGraph N hε).IsConnected := by intro u v -- Show every vertex reaches vertex `0` have h0 : ∀ (k : ℕ) (hk : k < 2 * N + 1), (altPathGraph N hε).ReachIn Finset.univ ⟨0, by omega⟩ ⟨k, hk⟩ := by intro k induction k with | zero => intro _; exact FilteredGraph.reachIn_refl _ _ _ | succ k ih => intro hk let ek : Fin (altPathGraph N hε).m := ⟨k, by dsimp [altPathGraph]; omega⟩ have hstep : (altPathGraph N hε).EdgeSubsetAdj Finset.univ ⟨k, by omega⟩ ⟨k + 1, hk⟩ := ⟨ek, Finset.mem_univ ek, Or.inl ⟨rfl, rfl⟩⟩ exact Relation.ReflTransGen.tail (ih (by omega)) hstep exact FilteredGraph.reachIn_trans _ (FilteredGraph.reachIn_symm _ (h0 u.val u.isLt)) (h0 v.val v.isLt) /-- Every edge of the path `P_{2N+1}` is a Kruskal MST edge, and the positive-length Elder-Rule bars of `altPathGraph N hε` consist of **exact `N` bars `[-ε, +ε)`** (while the `N` peak-birth merges produce degenerate zero-length bars `[+ε, +ε)`). -/ theorem altPathGraph_elder_bars_exact {N : ℕ} (hN : 1 ≤ N) {ε : ℝ} (hε : 0 < ε) : let G := altPathGraph N hε (∀ e : Fin G.m, G.isKruskalEdge e) ∧ (Finset.univ.filter fun e : Fin G.m => G.birth (G.youngerRoot e) < G.wt e).card = N ∧ (∀ e : Fin G.m, G.birth (G.youngerRoot e) < G.wt e → G.birth (G.youngerRoot e) = -ε ∧ G.wt e = ε) := by classical dsimp only set G := altPathGraph N hε -- In a path with edges 0, 1, ..., 2N-1, before edge e, `prefixEdges e.val` only connects -- vertices `{0, ..., e.val}` while `dst e = e.val + 1` is isolated! have hisol : ∀ (e : Fin G.m) (x : Fin (2 * N + 1)), G.ReachIn (G.prefixEdges e.val) (G.dst e) x → x = G.dst e := by intro e x h induction h with | refl => rfl | @tail a b _ hstep ih => subst ih rcases hstep with ⟨e', he', ⟨h1, _⟩ | ⟨_, h2⟩⟩ · have hlt : e'.val < e.val := by simpa [FilteredGraph.prefixEdges] using he' have hval : e'.val = e.val + 1 := congrArg Fin.val h1 omega · have hlt : e'.val < e.val := by simpa [FilteredGraph.prefixEdges] using he' have hval : e'.val + 1 = e.val + 1 := congrArg Fin.val h2 omega have hall_kruskal : ∀ e : Fin G.m, G.isKruskalEdge e := by intro e hreach have heq := hisol e (G.src e) (G.reachIn_symm hreach) have hval := congrArg Fin.val heq dsimp [G, altPathGraph] at hval; omega -- Before edge e, vertex 0 is connected to src e = e.val, and 0 is the global elder (birth -ε, index 0)! have h0_reach : ∀ (k : ℕ) (hk : k < 2 * N + 1), G.ReachIn (G.prefixEdges k) ⟨0, by omega⟩ ⟨k, hk⟩ := by intro k induction k with | zero => intro _; exact G.reachIn_refl _ _ | succ k ih => intro hk have hsub : G.prefixEdges k ⊆ G.prefixEdges (k + 1) := by intro x hx; simp only [FilteredGraph.prefixEdges, Finset.mem_filter, Finset.mem_univ, true_and] at hx ⊢ omega let ek : Fin G.m := ⟨k, by dsimp [G, altPathGraph]; omega⟩ have hstep : G.EdgeSubsetAdj (G.prefixEdges (k + 1)) ⟨k, by omega⟩ ⟨k + 1, hk⟩ := ⟨ek, by simp [FilteredGraph.prefixEdges, ek], Or.inl ⟨rfl, rfl⟩⟩ exact Relation.ReflTransGen.tail (G.reachIn_mono hsub (ih (by omega))) hstep have hyounger_eq_dst : ∀ e : Fin G.m, G.youngerRoot e = G.dst e := by intro e -- rootIn (prefixEdges e.val) (dst e) = dst e (isolated!) have hr2 : G.rootIn (G.prefixEdges e.val) (G.dst e) = G.dst e := by exact (hisol e _ (G.rootIn_reach _ _)) -- rootIn (prefixEdges e.val) (src e) = 0 (oldest vertex globally!) have hr1 : G.rootIn (G.prefixEdges e.val) (G.src e) = ⟨0, by omega⟩ := by have hreach0 : G.ReachIn (G.prefixEdges e.val) (G.src e) ⟨0, by omega⟩ := G.reachIn_symm (h0_reach e.val (by have := e.isLt; dsimp [G, altPathGraph] at this; omega)) rw [G.rootIn_eq_of_reach hreach0] apply (G.oldestOf_unique (G.compIn_nonempty _ _) (G.mem_compIn_self _ _) _).symm intro v _ by_cases hv0 : v = ⟨0, by omega⟩ · exact Or.inl hv0 · right dsimp only [FilteredGraph.older, G, altPathGraph] by_cases hev : v.val % 2 = 0 · right; simp [hev]; exact Fin.pos_iff_ne_zero.mpr hv0 · left; simp [hev]; linarith have hold0 : G.older ⟨0, by omega⟩ (G.dst e) := by dsimp only [FilteredGraph.older, G, altPathGraph] by_cases hev : (e.val + 1) % 2 = 0 · right; simp [hev]; exact Nat.succ_pos e.val · left; simp [hev]; linarith unfold FilteredGraph.youngerRoot rw [hr1, hr2, if_pos hold0] have hpos_iff : ∀ e : Fin G.m, G.birth (G.youngerRoot e) < G.wt e ↔ e.val % 2 = 1 := by intro e rw [hyounger_eq_dst e] dsimp only [G, altPathGraph] by_cases hodd : e.val % 2 = 1 · have hev : (e.val + 1) % 2 = 0 := by omega simp [hev, hodd]; linarith · have hnev : (e.val + 1) % 2 ≠ 0 := by omega simp [hnev, hodd] refine ⟨hall_kruskal, ?_, ?_⟩ · -- Count odd indices in Fin (2 * N) = N! have hbij : (Finset.univ.filter fun e : Fin G.m => G.birth (G.youngerRoot e) < G.wt e).card = N := by trans (Finset.univ : Finset (Fin N)).card · apply Finset.card_bij (fun e _ => (⟨e.val / 2, by have := e.isLt; dsimp [G, altPathGraph] at this; omega⟩ : Fin N)) · intro _ _; exact Finset.mem_univ _ · intro e₁ he₁ e₂ he₂ heq have h1 := (hpos_iff e₁).mp (Finset.mem_filter.mp he₁).2 have h2 := (hpos_iff e₂).mp (Finset.mem_filter.mp he₂).2 have hv : e₁.val / 2 = e₂.val / 2 := congrArg Fin.val heq exact Fin.ext (by omega) · intro k _ refine ⟨⟨2 * k.val + 1, by dsimp [G, altPathGraph]; omega⟩, ?_, ?_⟩ · simp only [Finset.mem_filter, Finset.mem_univ, true_and] rw [hpos_iff]; simp · apply Fin.ext; simp; omega · simp exact hbij · intro e he have hodd := (hpos_iff e).mp he rw [hyounger_eq_dst e] dsimp only [G, altPathGraph] have hev : (e.val + 1) % 2 = 0 := by omega simp [hev] /-- **Sharp `W_1`-vs.-`d_B` Stability Separation on the Filtered Alternating Path `P_{2N+1}`**: Comparing `f` (`altPathGraph N hε`, where `∀ v, |f v - 0| = ε`) against constant `0` (empty diagram), the Elder-Rule persistence diagram `D_f` has exact `N` positive bars `[-ε, +ε)`, giving: - Uniform perturbation `‖f - 0‖_∞ = ε`, - Bottleneck distance `d_B(D_f, ∅) = ε = ‖f - 0‖_∞` (universal `1`-Lipschitz stability), - `1`-Wasserstein distance `W_1(D_f, ∅) = N · ε = N · ‖f - 0‖_∞` (linear `N`-factor blow-up!), - General `p`-Wasserstein distance `W_p(D_f, ∅) = N^{1/p} · ε` (`p ≥ 1`). -/ theorem alternating_path_W1_vs_dB_separation {N : ℕ} (hN : 1 ≤ N) {ε : ℝ} (hε : 0 < ε) {p : ℝ} (hp : 1 ≤ p) : let G := altPathGraph N hε let D_f := constDiagram N ⟨-ε, ε, by linarith⟩ (∀ v : Fin (2 * N + 1), |G.birth v - 0| = ε) ∧ (Finset.univ.filter fun e : Fin G.m => G.birth (G.youngerRoot e) < G.wt e).card = N ∧ bottleneckDist D_f Diagram.empty = ε ∧ wassersteinDist 1 D_f Diagram.empty = (N : ℝ) * ε ∧ wassersteinDist p D_f Diagram.empty = (N : ℝ) ^ (1 / p) * ε := by dsimp only set G := altPathGraph N hε set D_f := constDiagram N ⟨-ε, ε, by linarith⟩ have hnorm : ∀ v : Fin (2 * N + 1), |G.birth v - 0| = ε := by intro v; dsimp [G, altPathGraph]; split_ifs <;> simp [abs_of_pos hε] have helder := (altPathGraph_elder_bars_exact hN hε).2.1 -- Shift invariance: `diagDist ⟨-ε, ε⟩ = ε = diagDist ⟨0, 2ε⟩` have hshift : ∀ s : ℝ, 1 ≤ s → wassersteinDist s D_f Diagram.empty = wassersteinDist s (constDiagram N ⟨0, 2 * ε, by linarith⟩) Diagram.empty := by intro s _ have huniq1 : ∀ M : PartialMatching D_f Diagram.empty, M.toFun = fun _ => none := by intro M; funext i; cases h : M.toFun i with | none => rfl | some j => exact Fin.elim0 j have huniq2 : ∀ M : PartialMatching (constDiagram N ⟨0, 2 * ε, by linarith⟩) Diagram.empty, M.toFun = fun _ => none := by intro M; funext i; cases h : M.toFun i with | none => rfl | some j => exact Fin.elim0 j obtain ⟨M1, hM1⟩ := exists_optimal_wasserstein_matching s D_f Diagram.empty obtain ⟨M2, hM2⟩ := exists_optimal_wasserstein_matching s (constDiagram N ⟨0, 2 * ε, by linarith⟩) Diagram.empty rw [← hM1, ← hM2] unfold PartialMatching.pCost PartialMatching.pCostPow congr 1 have he1 : (Finset.univ : Finset (Fin Diagram.empty.n)) = ∅ := by ext j; exact Fin.elim0 j rw [huniq1 M1, huniq2 M2, he1, Finset.sum_empty, add_zero, Finset.sum_empty, add_zero] apply Finset.sum_congr rfl; intro i _ unfold diagDist D_f constDiagram; dsimp; congr 1; ring have hdb_eq : bottleneckDist D_f Diagram.empty = ε := by have huniq1 : ∀ M : PartialMatching D_f Diagram.empty, M.toFun = fun _ => none := by intro M; funext i; cases h : M.toFun i with | none => rfl | some j => exact Fin.elim0 j obtain ⟨M1, hM1⟩ := exists_optimal_bottleneck_matching D_f Diagram.empty rw [← hM1] have hMn := huniq1 M1 unfold PartialMatching.infCost have he1 : (Finset.univ : Finset (Fin Diagram.empty.n)) = ∅ := by ext j; exact Fin.elim0 j rw [he1] simp only [Finset.fold_empty, max_eq_left (PartialMatching.le_fold_max_init _ _ _)] rw [hMn]; dsimp only apply le_antisymm · apply PartialMatching.fold_max_le _ _ _ _ hε.le intro i _; unfold diagDist D_f constDiagram; dsimp; linarith · have h0 := PartialMatching.le_fold_max_of_mem Finset.univ (fun i : Fin D_f.n => diagDist (D_f.bar i)) 0 (Finset.mem_univ (⟨0, by dsimp [D_f, constDiagram]; omega⟩ : Fin D_f.n)) have h0' : diagDist (D_f.bar ⟨0, by dsimp [D_f, constDiagram]; omega⟩) = ε := by unfold diagDist D_f constDiagram; dsimp; ring rwa [h0'] at h0 have hsep1 := wasserstein_exact_N_bar_separation hN hε (p := 1) (q := 1) le_rfl le_rfl have hsepp := wasserstein_exact_N_bar_separation hN hε (p := p) (q := p) hp le_rfl refine ⟨hnorm, helder, hdb_eq, ?_, ?_⟩ · rw [hshift 1 le_rfl, hsep1.2.1]; simp · rw [hshift p hp]; exact hsepp.2.1 end OpenTDAPersistence.ElderRuleWasserstein #print axioms OpenTDAPersistence.ElderRuleWasserstein.FilteredGraph.kruskal_youngerRoot_bijection #print axioms OpenTDAPersistence.ElderRuleWasserstein.FilteredGraph.kruskalSet_card #print axioms OpenTDAPersistence.ElderRuleWasserstein.FilteredGraph.elder_persistence_sum_eq_mst_sub_births #print axioms OpenTDAPersistence.ElderRuleWasserstein.FilteredGraph.elder_persistence_sum_eq_mst_of_zero_birth #print axioms OpenTDAPersistence.ElderRuleWasserstein.FilteredGraph.kruskal_mst_optimal #print axioms OpenTDAPersistence.ElderRuleWasserstein.FilteredGraph.elder_rule_h0_iso #print axioms OpenTDAPersistence.ElderRuleWasserstein.exists_optimal_wasserstein_matching #print axioms OpenTDAPersistence.ElderRuleWasserstein.exists_optimal_bottleneck_matching #print axioms OpenTDAPersistence.ElderRuleWasserstein.wassersteinDist_nonneg #print axioms OpenTDAPersistence.ElderRuleWasserstein.wassersteinDist_self #print axioms OpenTDAPersistence.ElderRuleWasserstein.wassersteinDist_eq_zero_iff #print axioms OpenTDAPersistence.ElderRuleWasserstein.wassersteinDist_symm #print axioms OpenTDAPersistence.ElderRuleWasserstein.wassersteinDist_triangle #print axioms OpenTDAPersistence.ElderRuleWasserstein.bottleneckDist_le_wassersteinDist #print axioms OpenTDAPersistence.ElderRuleWasserstein.wassersteinDist_anti #print axioms OpenTDAPersistence.ElderRuleWasserstein.wassersteinDist_le_card_rpow_mul_bottleneckDist #print axioms OpenTDAPersistence.ElderRuleWasserstein.wasserstein_exact_N_bar_separation #print axioms OpenTDAPersistence.ElderRuleWasserstein.altPathGraph_elder_bars_exact #print axioms OpenTDAPersistence.ElderRuleWasserstein.alternating_path_W1_vs_dB_separation