import Mathlib.Combinatorics.SimpleGraph.Metric import Mathlib.Data.Nat.Dist import Mathlib.Algebra.BigOperators.Fin import 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 DoubleBroom abbrev 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 i abbrev 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 k def 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 := by by_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] omega theorem route_triangle (x y z : Vertex A B k) : route x z ≤ route x y + route y z := by by_cases h₁ : x = y · subst y; simp by_cases h₂ : y = z · subst z; simp by_cases h₃ : x = z · subst z; simp simp 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) where Adj x y := route x y = 1 symm := ⟨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 := by by_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 := by simp [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 := by have hj := j.isLt simp [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 := by simp [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 := by simp [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 := by simp [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 := by induction 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⟩) := by change route _ _ = 1 rw [route_spine] simp [Nat.dist] omega refine ⟨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 := by obtain ⟨d, hd⟩ := Nat.exists_eq_add_of_le h have hk : i.val + d < k+1 := hd ▸ j.isLt obtain ⟨w, hw⟩ := walk_forward (A:=A) (B:=B) i.val d hk have he : (⟨i.val+d, hk⟩ : Fin (k+1)) = j := Fin.ext hd.symm rw [← 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 := by rcases 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 h exact ⟨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 := by rcases x with i | (j | i) · have h : (graph A B k).Adj (left i) (spine 0) := by change route _ _ = 1; simp exact ⟨h.toWalk, rfl⟩ · exact ⟨.nil, rfl⟩ · have h : (graph A B k).Adj (right i) (spine (Fin.last k)) := by change route _ _ = 1; simp exact ⟨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 := by by_cases h : x = y · subst y; exact ⟨.nil, by simp⟩ obtain ⟨p, hp⟩ := walk_foot x obtain ⟨q, hq⟩ := walk_spine (A:=A) (B:=B) (foot x) (foot y) obtain ⟨r, hr⟩ := walk_foot y exact ⟨(p.append q).append r.reverse, by simp [hp, hq, hr, route, h]⟩ theorem connected (A B k : ℕ) : (graph A B k).Connected where preconnected := fun x y => (exists_route x y).choose.reachable nonempty := ⟨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 := by induction p with | nil => simp | @cons x z y h p ih => have he : route x z = 1 := h have := route_triangle x z y simp only [SimpleGraph.Walk.length_cons] omega theorem graph_dist (x y : Vertex A B k) : (graph A B k).dist x y = route x y := by obtain ⟨w, hw⟩ := exists_route x y obtain ⟨p, hp⟩ := ((connected A B k) x y).exists_walk_length_eq_dist exact 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 v private theorem sum_other (n : ℕ) (i : Fin n) : (∑ j : Fin n, if i = j then 0 else 2) = 2 * (n - 1) := by classical rw [Finset.sum_ite] have he : Finset.univ.filter (fun j : Fin n => ¬ i = j) = Finset.univ.erase i := by ext j simp [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) := by unfold transmission Vertex rw [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) := by simpa only [Fin.revPerm_apply, Fin.val_rev, Nat.succ_sub_succ_eq_sub] using Equiv.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) := by unfold transmission Vertex rw [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) := by unfold transmission Vertex rw [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) := by rw [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 h nlinarith theorem spine_sum_bound (j : Fin (k+1)) : (∑ i : Fin (k+1), Nat.dist j.val i.val) ≤ (k+1)*k := by calc _ ≤ ∑ _ : Fin (k+1), k := Finset.sum_le_sum fun i _ => by have hi := i.isLt have hj := j.isLt unfold Nat.dist omega _ = _ := by simp end DoubleBroom