Lean / Reader
Lean reader

DoubleBroomTree.lean

91 lines8 declarationsno sorryimports DoubleBroom Mathlib.Combinatorics.SimpleGraph.Acyclic

Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports DoubleBroom, which it does not have: you can read it there, but it will not check. Build the project itself to check it.

  1. import DoubleBroom
  2. import Mathlib.Combinatorics.SimpleGraph.Acyclic
  3. namespace DoubleBroom
  4. variable {A B k : ℕ}
  5. private abbrev EdgeIndex (A B k : ℕ) := Fin A ⊕ (Fin k ⊕ Fin B)
  6. private def edgeCode : EdgeIndex A B k → Sym2 (Vertex A B k)
  7. | .inl i => s(left i, spine 0)
  8. | .inr (.inl j) => s(spine j.castSucc, spine j.succ)
  9. | .inr (.inr i) => s(right i, spine (Fin.last k))
  10. private theorem edge_spine_ordered (i j : Fin (k+1)) (h : i.val+1=j.val) :
  11. ∃ e : EdgeIndex A B k, edgeCode e = s(spine i, spine j) := by
  12. have hj := j.isLt
  13. let t : Fin k := ⟨i.val, by omega⟩
  14. have h₁ : t.castSucc = i := Fin.ext rfl
  15. have h₂ : t.succ = j := Fin.ext h
  16. exact ⟨.inr (.inl t), by simp [edgeCode, h₁, h₂]⟩
  17. private theorem code_swap {x y : Vertex A B k} :
  18. (∃ e : EdgeIndex A B k, edgeCode e = s(x,y)) →
  19. (∃ e : EdgeIndex A B k, edgeCode e = s(y,x)) := by
  20. rintro ⟨e, he⟩
  21. exact ⟨e, he.trans (Sym2.eq_swap ..)⟩
  22. private theorem edge_left_spine (i : Fin A) (j : Fin (k+1))
  23. (h : route (left i : Vertex A B k) (spine j) = 1) :
  24. ∃ e : EdgeIndex A B k, edgeCode e = s(left i, spine j) := by
  25. rw [route_left_spine] at h
  26. have hj : j = 0 := Fin.ext (by change j.val = 0; omega)
  27. exact ⟨.inl i, by simp [edgeCode, hj]⟩
  28. private theorem edge_right_spine (i : Fin B) (j : Fin (k+1))
  29. (h : route (right i : Vertex A B k) (spine j) = 1) :
  30. ∃ e : EdgeIndex A B k, edgeCode e = s(right i, spine j) := by
  31. rw [route_right_spine] at h
  32. have ht := j.isLt
  33. have hj : j = Fin.last k := Fin.ext (by change j.val = k; omega)
  34. exact ⟨.inr (.inr i), by simp [edgeCode, hj]⟩
  35. private theorem edge_has_code (x y : Vertex A B k) (h : (graph A B k).Adj x y) :
  36. ∃ e : EdgeIndex A B k, edgeCode e = s(x,y) := by
  37. change route x y = 1 at h
  38. rcases x with i | (i | i) <;> rcases y with j | (j | j)
  39. · change route (left i : Vertex A B k) (left j) = 1 at h
  40. rw [route_left] at h
  41. split at h <;> omega
  42. · exact edge_left_spine i j h
  43. · change route (left i : Vertex A B k) (right j) = 1 at h
  44. rw [route_left_right] at h
  45. omega
  46. · exact code_swap (edge_left_spine j i ((route_comm _ _) ▸ h))
  47. · change route (spine i : Vertex A B k) (spine j) = 1 at h
  48. rw [route_spine, Nat.dist] at h
  49. by_cases he : i.val < j.val
  50. · exact edge_spine_ordered i j (by omega)
  51. · exact code_swap (edge_spine_ordered j i (by omega))
  52. · exact code_swap (edge_right_spine j i ((route_comm _ _) ▸ h))
  53. · change route (right i : Vertex A B k) (left j) = 1 at h
  54. rw [route_comm, route_left_right] at h
  55. omega
  56. · exact edge_right_spine i j h
  57. · change route (right i : Vertex A B k) (right j) = 1 at h
  58. rw [route_right] at h
  59. split at h <;> omega
  60. /-- Every double broom, including the degenerate zero-length spine, is a tree. -/
  61. theorem isTree (A B k : ℕ) : (graph A B k).IsTree := by
  62. classical
  63. refine SimpleGraph.isTree_iff_connected_and_card.mpr ⟨connected A B k, ?_⟩
  64. have hle := (connected A B k).card_vert_le_card_edgeSet_add_one
  65. apply Nat.le_antisymm _ hle
  66. let f : EdgeIndex A B k → Sym2 (Vertex A B k) := edgeCode
  67. have he : (graph A B k).edgeFinset ⊆ Finset.univ.image f := by
  68. intro e he
  69. induction e using Sym2.inductionOn with
  70. | hf x y =>
  71. have hxy : (graph A B k).Adj x y := (SimpleGraph.mem_edgeFinset.mp he)
  72. obtain ⟨j, hj⟩ := edge_has_code x y hxy
  73. exact Finset.mem_image.mpr ⟨j, Finset.mem_univ _, hj⟩
  74. have he' := (Finset.card_le_card he).trans Finset.card_image_le
  75. rw [Nat.card_eq_fintype_card, ← SimpleGraph.edgeFinset_card]
  76. simp [Vertex, EdgeIndex] at he' ⊢
  77. omega
  78. end DoubleBroom