Lean / Reader
Lean reader

DoubleBroom.lean

223 lines33 declarationsno sorryimports Mathlib.Combinatorics.SimpleGraph.Metric Mathlib.Data.Nat.Dist Mathlib.Algebra.BigOperators.Fin Mathlib.Tactic
  1. import Mathlib.Combinatorics.SimpleGraph.Metric
  2. import Mathlib.Data.Nat.Dist
  3. import Mathlib.Algebra.BigOperators.Fin
  4. import Mathlib.Tactic
  5. /-! A finite double broom with A leaves on the left, B leaves on the right,
  6. and a path of k edges between their attachment vertices. -/
  7. namespace DoubleBroom
  8. abbrev Vertex (A B k : ℕ) := Fin A ⊕ (Fin (k + 1) ⊕ Fin B)
  9. variable {A B k : ℕ}
  10. abbrev left (i : Fin A) : Vertex A B k := .inl i
  11. abbrev spine (j : Fin (k+1)) : Vertex A B k := .inr (.inl j)
  12. abbrev right (i : Fin B) : Vertex A B k := .inr (.inr i)
  13. def foot : Vertex A B k → Fin (k+1)
  14. | .inl _ => 0
  15. | .inr (.inl j) => j
  16. | .inr (.inr _) => Fin.last k
  17. def depth : Vertex A B k → ℕ
  18. | .inl _ | .inr (.inr _) => 1
  19. | .inr (.inl _) => 0
  20. /-- The distance along the spine, plus the contributions of the leaf endpoints. -/
  21. def route (x y : Vertex A B k) : ℕ :=
  22. if x = y then 0 else depth x + Nat.dist (foot x).val (foot y).val + depth y
  23. @[simp] theorem route_self (x : Vertex A B k) : route x x = 0 := by simp [route]
  24. theorem route_comm (x y : Vertex A B k) : route x y = route y x := by
  25. by_cases h : x = y
  26. · subst y; rfl
  27. · simp only [route, if_neg h, if_neg (Ne.symm h), Nat.dist_comm (foot x).val (foot y).val]
  28. omega
  29. theorem route_triangle (x y z : Vertex A B k) : route x z ≤ route x y + route y z := by
  30. by_cases h₁ : x = y
  31. · subst y; simp
  32. by_cases h₂ : y = z
  33. · subst z; simp
  34. by_cases h₃ : x = z
  35. · subst z; simp
  36. simp only [route, if_neg h₁, if_neg h₂, if_neg h₃, Nat.dist]
  37. omega
  38. /-- Precisely the unit-length links of the double broom. -/
  39. def graph (A B k : ℕ) : SimpleGraph (Vertex A B k) where
  40. Adj x y := route x y = 1
  41. symm := ⟨fun x y h => (route_comm x y) ▸ h⟩
  42. loopless := ⟨by intro x; simp⟩
  43. @[simp] theorem route_spine (i j : Fin (k+1)) :
  44. route (spine i : Vertex A B k) (spine j) = Nat.dist i.val j.val := by
  45. by_cases h : i = j
  46. · subst j; simp
  47. · simp [route, spine, h, foot, depth]
  48. @[simp] theorem route_left_spine (i : Fin A) (j : Fin (k+1)) :
  49. route (left i : Vertex A B k) (spine j) = j.val + 1 := by
  50. simp [route, left, spine, foot, depth, Nat.dist, Nat.add_comm]
  51. @[simp] theorem route_right_spine (i : Fin B) (j : Fin (k+1)) :
  52. route (right i : Vertex A B k) (spine j) = (k - j.val) + 1 := by
  53. have hj := j.isLt
  54. simp [route, right, spine, foot, depth, Nat.dist]
  55. omega
  56. @[simp] theorem route_left_right (i : Fin A) (j : Fin B) :
  57. route (left i : Vertex A B k) (right j) = k + 2 := by
  58. simp [route, left, right, foot, depth, Nat.dist]
  59. omega
  60. @[simp] theorem route_left (i j : Fin A) :
  61. route (left i : Vertex A B k) (left j) = if i = j then 0 else 2 := by
  62. simp [route, left, foot, depth]
  63. @[simp] theorem route_right (i j : Fin B) :
  64. route (right i : Vertex A B k) (right j) = if i = j then 0 else 2 := by
  65. simp [route, right, foot, depth]
  66. private theorem walk_forward (i d : ℕ) (h : i+d < k+1) :
  67. ∃ w : (graph A B k).Walk (spine ⟨i, by omega⟩) (spine ⟨i+d, h⟩), w.length = d := by
  68. induction d with
  69. | zero => exact ⟨.nil, rfl⟩
  70. | succ d ih =>
  71. obtain ⟨w, hw⟩ := ih (by omega)
  72. have he : (graph A B k).Adj (spine ⟨i+d, by omega⟩) (spine ⟨i+(d+1), h⟩) := by
  73. change route _ _ = 1
  74. rw [route_spine]
  75. simp [Nat.dist]
  76. omega
  77. refine ⟨w.append he.toWalk, ?_⟩
  78. simp [hw]
  79. private theorem walk_ordered (i j : Fin (k+1)) (h : i.val ≤ j.val) :
  80. ∃ w : (graph A B k).Walk (spine i) (spine j), w.length = Nat.dist i.val j.val := by
  81. obtain ⟨d, hd⟩ := Nat.exists_eq_add_of_le h
  82. have hk : i.val + d < k+1 := hd ▸ j.isLt
  83. obtain ⟨w, hw⟩ := walk_forward (A:=A) (B:=B) i.val d hk
  84. have he : (⟨i.val+d, hk⟩ : Fin (k+1)) = j := Fin.ext hd.symm
  85. rw [← he]
  86. refine ⟨w, ?_⟩
  87. simp [hw, Nat.dist]
  88. private theorem walk_spine (i j : Fin (k+1)) :
  89. ∃ w : (graph A B k).Walk (spine i) (spine j), w.length = Nat.dist i.val j.val := by
  90. rcases le_total i.val j.val with h | h
  91. · exact walk_ordered i j h
  92. · obtain ⟨w, hw⟩ := walk_ordered (A:=A) (B:=B) j i h
  93. exact ⟨w.reverse, by rw [SimpleGraph.Walk.length_reverse, hw, Nat.dist_comm]⟩
  94. private theorem walk_foot (x : Vertex A B k) :
  95. ∃ w : (graph A B k).Walk x (spine (foot x)), w.length = depth x := by
  96. rcases x with i | (j | i)
  97. · have h : (graph A B k).Adj (left i) (spine 0) := by change route _ _ = 1; simp
  98. exact ⟨h.toWalk, rfl⟩
  99. · exact ⟨.nil, rfl⟩
  100. · have h : (graph A B k).Adj (right i) (spine (Fin.last k)) := by
  101. change route _ _ = 1; simp
  102. exact ⟨h.toWalk, rfl⟩
  103. private theorem exists_route (x y : Vertex A B k) :
  104. ∃ w : (graph A B k).Walk x y, w.length = route x y := by
  105. by_cases h : x = y
  106. · subst y; exact ⟨.nil, by simp⟩
  107. obtain ⟨p, hp⟩ := walk_foot x
  108. obtain ⟨q, hq⟩ := walk_spine (A:=A) (B:=B) (foot x) (foot y)
  109. obtain ⟨r, hr⟩ := walk_foot y
  110. exact ⟨(p.append q).append r.reverse, by simp [hp, hq, hr, route, h]⟩
  111. theorem connected (A B k : ℕ) : (graph A B k).Connected where
  112. preconnected := fun x y => (exists_route x y).choose.reachable
  113. nonempty := ⟨spine 0⟩
  114. private theorem route_le_length {x y : Vertex A B k} (p : (graph A B k).Walk x y) :
  115. route x y ≤ p.length := by
  116. induction p with
  117. | nil => simp
  118. | @cons x z y h p ih =>
  119. have he : route x z = 1 := h
  120. have := route_triangle x z y
  121. simp only [SimpleGraph.Walk.length_cons]
  122. omega
  123. theorem graph_dist (x y : Vertex A B k) : (graph A B k).dist x y = route x y := by
  124. obtain ⟨w, hw⟩ := exists_route x y
  125. obtain ⟨p, hp⟩ := ((connected A B k) x y).exists_walk_length_eq_dist
  126. exact le_antisymm (hw ▸ SimpleGraph.dist_le w) (hp ▸ route_le_length p)
  127. noncomputable def transmission (u : Vertex A B k) : ℕ := ∑ v, (graph A B k).dist u v
  128. private theorem sum_other (n : ℕ) (i : Fin n) :
  129. (∑ j : Fin n, if i = j then 0 else 2) = 2 * (n - 1) := by
  130. classical
  131. rw [Finset.sum_ite]
  132. have he : Finset.univ.filter (fun j : Fin n => ¬ i = j) = Finset.univ.erase i := by
  133. ext j
  134. simp [ne_comm]
  135. simp [he, Finset.card_erase_of_mem, Nat.mul_comm]
  136. theorem transmission_left (i : Fin A) :
  137. transmission (left i : Vertex A B k) =
  138. 2 * (A-1) + (∑ j : Fin (k+1), (j.val+1)) + B*(k+2) := by
  139. unfold transmission Vertex
  140. rw [Fintype.sum_sum_type]
  141. simp only [Fintype.sum_sum_type, graph_dist]
  142. change (∑ j : Fin A, route (left i : Vertex A B k) (left j)) +
  143. ((∑ j : Fin (k+1), route (left i : Vertex A B k) (spine j)) +
  144. (∑ j : Fin B, route (left i : Vertex A B k) (right j))) = _
  145. simp [sum_other, Nat.add_assoc]
  146. private theorem sum_reverse (k : ℕ) :
  147. (∑ j : Fin (k+1), (k-j.val+1)) = ∑ j : Fin (k+1), (j.val+1) := by
  148. simpa only [Fin.revPerm_apply, Fin.val_rev, Nat.succ_sub_succ_eq_sub] using
  149. Equiv.sum_comp (Fin.revPerm (n:=k+1)) (fun j => j.val+1)
  150. theorem transmission_right (i : Fin B) :
  151. transmission (right i : Vertex A B k) =
  152. 2 * (B-1) + (∑ j : Fin (k+1), (j.val+1)) + A*(k+2) := by
  153. unfold transmission Vertex
  154. rw [Fintype.sum_sum_type]
  155. simp only [Fintype.sum_sum_type, graph_dist]
  156. change (∑ j : Fin A, route (right i : Vertex A B k) (left j)) +
  157. ((∑ j : Fin (k+1), route (right i : Vertex A B k) (spine j)) +
  158. (∑ j : Fin B, route (right i : Vertex A B k) (right j))) = _
  159. simp [route_comm (right i) (left _), sum_other, sum_reverse, Nat.add_comm]
  160. theorem transmission_spine (j : Fin (k+1)) :
  161. transmission (spine j : Vertex A B k) =
  162. A*(j.val+1) + (∑ i : Fin (k+1), Nat.dist j.val i.val) + B*(k-j.val+1) := by
  163. unfold transmission Vertex
  164. rw [Fintype.sum_sum_type]
  165. simp only [Fintype.sum_sum_type, graph_dist]
  166. change (∑ i : Fin A, route (spine j : Vertex A B k) (left i)) +
  167. ((∑ i : Fin (k+1), route (spine j : Vertex A B k) (spine i)) +
  168. (∑ i : Fin B, route (spine j : Vertex A B k) (right i))) = _
  169. simp [route_comm (spine j) (left _), route_comm (spine j) (right _), Nat.add_assoc]
  170. theorem twice_spine_sum (k : ℕ) :
  171. 2 * (∑ j : Fin (k+1), (j.val+1)) = (k+1)*(k+2) := by
  172. rw [Finset.sum_add_distrib]
  173. simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, smul_eq_mul, mul_one]
  174. rw [Fin.sum_univ_eq_sum_range (fun i => i)]
  175. have h := Finset.sum_range_id_mul_two (k+1)
  176. simp only [Nat.add_sub_cancel] at h
  177. nlinarith
  178. theorem spine_sum_bound (j : Fin (k+1)) :
  179. (∑ i : Fin (k+1), Nat.dist j.val i.val) ≤ (k+1)*k := by
  180. calc
  181. _ ≤ ∑ _ : Fin (k+1), k := Finset.sum_le_sum fun i _ => by
  182. have hi := i.isLt
  183. have hj := j.isLt
  184. unfold Nat.dist
  185. omega
  186. _ = _ := by simp
  187. end DoubleBroom