import Mathlib.Combinatorics.SimpleGraph.Metricimport Mathlib.Data.Nat.Distimport Mathlib.Algebra.BigOperators.Finimport Mathlib.Tactic/-! A finite double broom with A leaves on the left, B leaves on the right,and a path of k edges between their attachment vertices. -/namespace DoubleBroomabbrev Vertex (A B k : ℕ) := Fin A ⊕ (Fin (k + 1) ⊕ Fin B)variable {A B k : ℕ}abbrev left (i : Fin A) : Vertex A B k := .inl iabbrev spine (j : Fin (k+1)) : Vertex A B k := .inr (.inl j)abbrev right (i : Fin B) : Vertex A B k := .inr (.inr i)def foot : Vertex A B k → Fin (k+1)| .inl _ => 0| .inr (.inl j) => j| .inr (.inr _) => Fin.last kdef depth : Vertex A B k → ℕ| .inl _ | .inr (.inr _) => 1| .inr (.inl _) => 0/-- The distance along the spine, plus the contributions of the leaf endpoints. -/def route (x y : Vertex A B k) : ℕ :=if x = y then 0 else depth x + Nat.dist (foot x).val (foot y).val + depth y@[simp] theorem route_self (x : Vertex A B k) : route x x = 0 := by simp [route]theorem route_comm (x y : Vertex A B k) : route x y = route y x := byby_cases h : x = y· subst y; rfl· simp only [route, if_neg h, if_neg (Ne.symm h), Nat.dist_comm (foot x).val (foot y).val]omegatheorem route_triangle (x y z : Vertex A B k) : route x z ≤ route x y + route y z := byby_cases h₁ : x = y· subst y; simpby_cases h₂ : y = z· subst z; simpby_cases h₃ : x = z· subst z; simpsimp only [route, if_neg h₁, if_neg h₂, if_neg h₃, Nat.dist]omega/-- Precisely the unit-length links of the double broom. -/def graph (A B k : ℕ) : SimpleGraph (Vertex A B k) whereAdj x y := route x y = 1symm := ⟨fun x y h => (route_comm x y) ▸ h⟩loopless := ⟨by intro x; simp⟩@[simp] theorem route_spine (i j : Fin (k+1)) :route (spine i : Vertex A B k) (spine j) = Nat.dist i.val j.val := byby_cases h : i = j· subst j; simp· simp [route, spine, h, foot, depth]@[simp] theorem route_left_spine (i : Fin A) (j : Fin (k+1)) :route (left i : Vertex A B k) (spine j) = j.val + 1 := bysimp [route, left, spine, foot, depth, Nat.dist, Nat.add_comm]@[simp] theorem route_right_spine (i : Fin B) (j : Fin (k+1)) :route (right i : Vertex A B k) (spine j) = (k - j.val) + 1 := byhave hj := j.isLtsimp [route, right, spine, foot, depth, Nat.dist]omega@[simp] theorem route_left_right (i : Fin A) (j : Fin B) :route (left i : Vertex A B k) (right j) = k + 2 := bysimp [route, left, right, foot, depth, Nat.dist]omega@[simp] theorem route_left (i j : Fin A) :route (left i : Vertex A B k) (left j) = if i = j then 0 else 2 := bysimp [route, left, foot, depth]@[simp] theorem route_right (i j : Fin B) :route (right i : Vertex A B k) (right j) = if i = j then 0 else 2 := bysimp [route, right, foot, depth]private theorem walk_forward (i d : ℕ) (h : i+d < k+1) :∃ w : (graph A B k).Walk (spine ⟨i, by omega⟩) (spine ⟨i+d, h⟩), w.length = d := byinduction d with| zero => exact ⟨.nil, rfl⟩| succ d ih =>obtain ⟨w, hw⟩ := ih (by omega)have he : (graph A B k).Adj (spine ⟨i+d, by omega⟩) (spine ⟨i+(d+1), h⟩) := bychange route _ _ = 1rw [route_spine]simp [Nat.dist]omegarefine ⟨w.append he.toWalk, ?_⟩simp [hw]private theorem walk_ordered (i j : Fin (k+1)) (h : i.val ≤ j.val) :∃ w : (graph A B k).Walk (spine i) (spine j), w.length = Nat.dist i.val j.val := byobtain ⟨d, hd⟩ := Nat.exists_eq_add_of_le hhave hk : i.val + d < k+1 := hd ▸ j.isLtobtain ⟨w, hw⟩ := walk_forward (A:=A) (B:=B) i.val d hkhave he : (⟨i.val+d, hk⟩ : Fin (k+1)) = j := Fin.ext hd.symmrw [← he]refine ⟨w, ?_⟩simp [hw, Nat.dist]private theorem walk_spine (i j : Fin (k+1)) :∃ w : (graph A B k).Walk (spine i) (spine j), w.length = Nat.dist i.val j.val := byrcases le_total i.val j.val with h | h· exact walk_ordered i j h· obtain ⟨w, hw⟩ := walk_ordered (A:=A) (B:=B) j i hexact ⟨w.reverse, by rw [SimpleGraph.Walk.length_reverse, hw, Nat.dist_comm]⟩private theorem walk_foot (x : Vertex A B k) :∃ w : (graph A B k).Walk x (spine (foot x)), w.length = depth x := byrcases x with i | (j | i)· have h : (graph A B k).Adj (left i) (spine 0) := by change route _ _ = 1; simpexact ⟨h.toWalk, rfl⟩· exact ⟨.nil, rfl⟩· have h : (graph A B k).Adj (right i) (spine (Fin.last k)) := bychange route _ _ = 1; simpexact ⟨h.toWalk, rfl⟩private theorem exists_route (x y : Vertex A B k) :∃ w : (graph A B k).Walk x y, w.length = route x y := byby_cases h : x = y· subst y; exact ⟨.nil, by simp⟩obtain ⟨p, hp⟩ := walk_foot xobtain ⟨q, hq⟩ := walk_spine (A:=A) (B:=B) (foot x) (foot y)obtain ⟨r, hr⟩ := walk_foot yexact ⟨(p.append q).append r.reverse, by simp [hp, hq, hr, route, h]⟩theorem connected (A B k : ℕ) : (graph A B k).Connected wherepreconnected := fun x y => (exists_route x y).choose.reachablenonempty := ⟨spine 0⟩private theorem route_le_length {x y : Vertex A B k} (p : (graph A B k).Walk x y) :route x y ≤ p.length := byinduction p with| nil => simp| @cons x z y h p ih =>have he : route x z = 1 := hhave := route_triangle x z ysimp only [SimpleGraph.Walk.length_cons]omegatheorem graph_dist (x y : Vertex A B k) : (graph A B k).dist x y = route x y := byobtain ⟨w, hw⟩ := exists_route x yobtain ⟨p, hp⟩ := ((connected A B k) x y).exists_walk_length_eq_distexact le_antisymm (hw ▸ SimpleGraph.dist_le w) (hp ▸ route_le_length p)noncomputable def transmission (u : Vertex A B k) : ℕ := ∑ v, (graph A B k).dist u vprivate theorem sum_other (n : ℕ) (i : Fin n) :(∑ j : Fin n, if i = j then 0 else 2) = 2 * (n - 1) := byclassicalrw [Finset.sum_ite]have he : Finset.univ.filter (fun j : Fin n => ¬ i = j) = Finset.univ.erase i := byext jsimp [ne_comm]simp [he, Finset.card_erase_of_mem, Nat.mul_comm]theorem transmission_left (i : Fin A) :transmission (left i : Vertex A B k) =2 * (A-1) + (∑ j : Fin (k+1), (j.val+1)) + B*(k+2) := byunfold transmission Vertexrw [Fintype.sum_sum_type]simp only [Fintype.sum_sum_type, graph_dist]change (∑ j : Fin A, route (left i : Vertex A B k) (left j)) +((∑ j : Fin (k+1), route (left i : Vertex A B k) (spine j)) +(∑ j : Fin B, route (left i : Vertex A B k) (right j))) = _simp [sum_other, Nat.add_assoc]private theorem sum_reverse (k : ℕ) :(∑ j : Fin (k+1), (k-j.val+1)) = ∑ j : Fin (k+1), (j.val+1) := bysimpa only [Fin.revPerm_apply, Fin.val_rev, Nat.succ_sub_succ_eq_sub] usingEquiv.sum_comp (Fin.revPerm (n:=k+1)) (fun j => j.val+1)theorem transmission_right (i : Fin B) :transmission (right i : Vertex A B k) =2 * (B-1) + (∑ j : Fin (k+1), (j.val+1)) + A*(k+2) := byunfold transmission Vertexrw [Fintype.sum_sum_type]simp only [Fintype.sum_sum_type, graph_dist]change (∑ j : Fin A, route (right i : Vertex A B k) (left j)) +((∑ j : Fin (k+1), route (right i : Vertex A B k) (spine j)) +(∑ j : Fin B, route (right i : Vertex A B k) (right j))) = _simp [route_comm (right i) (left _), sum_other, sum_reverse, Nat.add_comm]theorem transmission_spine (j : Fin (k+1)) :transmission (spine j : Vertex A B k) =A*(j.val+1) + (∑ i : Fin (k+1), Nat.dist j.val i.val) + B*(k-j.val+1) := byunfold transmission Vertexrw [Fintype.sum_sum_type]simp only [Fintype.sum_sum_type, graph_dist]change (∑ i : Fin A, route (spine j : Vertex A B k) (left i)) +((∑ i : Fin (k+1), route (spine j : Vertex A B k) (spine i)) +(∑ i : Fin B, route (spine j : Vertex A B k) (right i))) = _simp [route_comm (spine j) (left _), route_comm (spine j) (right _), Nat.add_assoc]theorem twice_spine_sum (k : ℕ) :2 * (∑ j : Fin (k+1), (j.val+1)) = (k+1)*(k+2) := byrw [Finset.sum_add_distrib]simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, smul_eq_mul, mul_one]rw [Fin.sum_univ_eq_sum_range (fun i => i)]have h := Finset.sum_range_id_mul_two (k+1)simp only [Nat.add_sub_cancel] at hnlinariththeorem spine_sum_bound (j : Fin (k+1)) :(∑ i : Fin (k+1), Nat.dist j.val i.val) ≤ (k+1)*k := bycalc_ ≤ ∑ _ : Fin (k+1), k := Finset.sum_le_sum fun i _ => byhave hi := i.isLthave hj := j.isLtunfold Nat.distomega_ = _ := by simpend DoubleBroom