/- Copyright 2026 The Formal Conjectures Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -/ import Mathlib namespace ResearchErdos561 open SimpleGraph /-- The size Ramsey number `r̂(G, H)` is the minimum number of edges in a graph `F` such that any 2-coloring of `F`'s edges contains a copy of `G` in one color or `H` in the other. A 2-coloring is represented by a subgraph `R ≤ F` (the "red" edges); the "blue" edges are `F \ R`. -/ noncomputable def sizeRamsey {α β : Type*} [Fintype α] [Fintype β] (G : SimpleGraph α) (H : SimpleGraph β) : ℕ := sInf { m | ∃ (n : ℕ) (F : SimpleGraph (Fin n)), F.edgeSet.ncard = m ∧ ∀ (R : SimpleGraph (Fin n)), R ≤ F → G.IsContained R ∨ H.IsContained (F \ R) } local notation "F(" s ", " n ")" => fromRel (fun (u v : Σ i : Fin s, Fin (n i + 1)) ↦ Sigma.fst u = Sigma.fst v ∧ (Fin.val (Sigma.snd u) = 0 ∨ Fin.val (Sigma.snd v) = 0)) theorem forest_adj_iff_center_leaf {s : ℕ} (n : Fin s → ℕ) (u v : Σ i : Fin s, Fin (n i + 1)) : (F(s, n)).Adj u v ↔ ∃ (i : Fin s) (j : Fin (n i)), (u = ⟨i, 0⟩ ∧ v = ⟨i, j.succ⟩) ∨ (v = ⟨i, 0⟩ ∧ u = ⟨i, j.succ⟩) := by constructor · rcases u with ⟨i, u⟩ rcases v with ⟨k, v⟩ rw [fromRel_adj] simp only [eq_comm (a := k), or_comm (a := v.val = 0), or_self] rintro ⟨hne, rfl, hu | hv⟩ · have hu' : u = 0 := Fin.ext hu subst u obtain ⟨j, rfl⟩ := Fin.exists_succ_eq_of_ne_zero (show v ≠ 0 from fun h ↦ hne (congrArg (Sigma.mk i) h.symm)) exact ⟨i, j, Or.inl ⟨rfl, rfl⟩⟩ · have hv' : v = 0 := Fin.ext hv subst v obtain ⟨j, rfl⟩ := Fin.exists_succ_eq_of_ne_zero (show u ≠ 0 from fun h ↦ hne (congrArg (Sigma.mk i) h)) exact ⟨i, j, Or.inr ⟨rfl, rfl⟩⟩ · rintro ⟨i, j, (⟨rfl, rfl⟩ | ⟨rfl, rfl⟩)⟩ · simp [fromRel_adj, Ne.symm (Fin.succ_ne_zero j)] · simp [fromRel_adj] theorem forest_edge_ncard {s : ℕ} (n : Fin s → ℕ) : (F(s, n)).edgeSet.ncard = ∑ i : Fin s, n i := by classical let f : (Σ i : Fin s, Fin (n i)) → Sym2 (Σ i : Fin s, Fin (n i + 1)) := fun x ↦ s(⟨x.1, 0⟩, ⟨x.1, x.2.succ⟩) have hf : Function.Injective f := by rintro ⟨i, j⟩ ⟨k, l⟩ h change s((⟨i, 0⟩ : Σ i : Fin s, Fin (n i + 1)), ⟨i, j.succ⟩) = s(⟨k, 0⟩, ⟨k, l.succ⟩) at h rcases Sym2.eq_iff.mp h with ⟨h0, h1⟩ | ⟨h0, _⟩ · have hik := congrArg Sigma.fst h0 change i = k at hik subst k apply congrArg (Sigma.mk i) apply Fin.ext have hv := congrArg (fun x : Σ i : Fin s, Fin (n i + 1) ↦ x.2.val) h1 change j.val + 1 = l.val + 1 at hv omega · have hv := congrArg (fun x : Σ i : Fin s, Fin (n i + 1) ↦ x.2.val) h0 simp at hv have h_image : (F(s, n)).edgeSet = f '' Set.univ := by ext e induction e using Sym2.inductionOn with | _ u v => rw [mem_edgeSet, forest_adj_iff_center_leaf] constructor · rintro ⟨i, j, (⟨rfl, rfl⟩ | ⟨rfl, rfl⟩)⟩ · exact ⟨⟨i, j⟩, Set.mem_univ _, rfl⟩ · exact ⟨⟨i, j⟩, Set.mem_univ _, Sym2.eq_swap⟩ · rintro ⟨⟨i, j⟩, -, h⟩ rcases Sym2.eq_iff.mp h with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ · exact ⟨i, j, Or.inl ⟨rfl, rfl⟩⟩ · exact ⟨i, j, Or.inr ⟨rfl, rfl⟩⟩ rw [h_image, Set.ncard_image_of_injective _ hf, Set.ncard_univ, Nat.card_eq_fintype_card, Fintype.card_sigma] simp theorem forest_copy_of_index_le {s t : ℕ} (n : Fin s → ℕ) (m : Fin t → ℕ) (G : SimpleGraph (Σ i : Fin t, Fin (m i + 1))) [DecidableRel G.Adj] (e : Fin s ↪ Fin t) (he : ∀ i, n i ≤ ((Finset.univ : Finset (Fin (m (e i)))).filter (fun j ↦ G.Adj ⟨e i, 0⟩ ⟨e i, j.succ⟩)).card) : (F(s, n)).IsContained G := by classical have chooseLeaf (i : Fin s) : ∃ f : Fin (n i) ↪ Fin (m (e i)), ∀ j, G.Adj ⟨e i, 0⟩ ⟨e i, (f j).succ⟩ := by obtain ⟨f, hf⟩ := Function.Embedding.exists_of_card_le_finset (α := Fin (n i)) (by simpa only [Fintype.card_fin] using he i) refine ⟨f, fun j ↦ ?_⟩ exact (Finset.mem_filter.mp (hf ⟨j, rfl⟩)).2 choose f hf using chooseLeaf let g (i : Fin s) : Fin (n i + 1) ↪ Fin (m (e i) + 1) := Fin.Embedding.cons ((f i).trans ⟨Fin.succ, Fin.succ_injective _⟩) (by rintro ⟨j, h⟩; exact Fin.succ_ne_zero _ h) have hg0 (i : Fin s) : g i 0 = 0 := rfl have hg1 (i : Fin s) (j : Fin (n i)) : g i j.succ = (f i j).succ := by simp [g, Function.Embedding.trans_apply] apply isContained_iff_exists_le_comap.mpr refine ⟨e.sigmaMap g, ?_⟩ intro u v h rcases (forest_adj_iff_center_leaf n u v).mp h with ⟨i, j, ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩⟩ · change G.Adj ⟨e i, g i 0⟩ ⟨e i, g i j.succ⟩ rw [hg0, hg1] exact hf i j · change G.Adj ⟨e i, g i j.succ⟩ ⟨e i, g i 0⟩ rw [hg0, hg1] exact (hf i j).symm theorem canonical_profile_case (a k l : ℕ) (hk : k ≤ 3) (hkl : k + l ≤ 5) (r b : Fin 6 → ℕ) (hr : a ≤ r 0) (hsmall : ∀ i : Fin 6, i ≠ 0 → 3 ≤ r i + b i) (hmiddle : ∀ i ∈ ({1, 2, 3} : Finset (Fin 6)), a + 1 ≤ r i + b i) : (∃ e : Fin (k + 1) ↪ Fin 6, ∀ i, Fin.cons (α := fun _ ↦ ℕ) a (fun _ ↦ 2) i ≤ r (e i)) ∨ (∃ e : Fin (l + 1) ↪ Fin 6, ∀ i, Fin.cons (α := fun _ ↦ ℕ) a (fun _ ↦ 2) i ≤ b (e i)) := by classical have choose (c : Fin 6 → ℕ) (u : Fin 6) (hu : a ≤ c u) (p : ℕ) (T : Finset (Fin 6)) (hT : p ≤ T.card) (hnot : u ∉ T) (hb : ∀ i ∈ T, 2 ≤ c i) : ∃ e : Fin (p + 1) ↪ Fin 6, ∀ i, Fin.cons (α := fun _ ↦ ℕ) a (fun _ ↦ 2) i ≤ c (e i) := by obtain ⟨f, hf⟩ := Function.Embedding.exists_of_card_le_finset (α := Fin p) (by simpa only [Fintype.card_fin] using hT) refine ⟨Fin.Embedding.cons f (fun h ↦ hnot (hf h)), ?_⟩ intro i refine Fin.cases ?_ (fun j ↦ ?_) i · simpa using hu · simpa using hb (f j) (hf ⟨j, rfl⟩) let S : Finset (Fin 6) := Finset.univ.erase 0 let P := S.filter (fun i ↦ 2 ≤ r i) let Q := S.filter (fun i ↦ ¬2 ≤ r i) by_cases hp : k ≤ P.card · left apply choose r 0 hr k P hp · simp [P, S] · exact fun i hi ↦ (Finset.mem_filter.mp hi).2 · right have hsum : P.card + Q.card = 5 := by calc _ = S.card := Finset.card_filter_add_card_filter_not (s := S) _ _ = 5 := by simp [S, Finset.card_erase_of_mem] let M : Finset (Fin 6) := {1, 2, 3} have hmS : M ⊆ S := by decide have hm3 : M.card = 3 := by decide have hmiddleQ : ∃ i ∈ M, i ∈ Q := by by_contra! h have hMP : M ⊆ P := by intro i hi have hS := hmS hi apply Finset.mem_filter.mpr refine ⟨hS, ?_⟩ by_contra hnot exact h i hi (Finset.mem_filter.mpr ⟨hS, hnot⟩) have := Finset.card_le_card hMP omega obtain ⟨i, hiM, hiQ⟩ := hmiddleQ have hiBig : a ≤ b i := by have := hmiddle i hiM have := (Finset.mem_filter.mp hiQ).2 omega apply choose b i hiBig l (Q.erase i) · rw [Finset.card_erase_of_mem hiQ] omega · exact Finset.notMem_erase _ _ · intro j hj have hjQ := Finset.mem_filter.mp (Finset.mem_of_mem_erase hj) have := (Finset.mem_erase.mp hjQ.1).1 have := hsmall j this omega theorem upper_three_four (a : ℕ) (ha : 3 ≤ a) : ∃ (n : ℕ) (H : SimpleGraph (Fin n)), H.edgeSet.ncard = 5 * a + 8 ∧ ∀ (R : SimpleGraph (Fin n)), R ≤ H → (F(3, ![a, 2, 2])).IsContained R ∨ (F(4, ![a, 2, 2, 2])).IsContained (H \ R) := by classical let m : Fin 6 → ℕ := ![2 * a - 1, a + 1, a + 1, a + 1, 3, 3] let V := Σ i : Fin 6, Fin (m i + 1) let H₀ : SimpleGraph V := F(6, m) let e := Fintype.equivFin V let H := H₀.map e.toEmbedding refine ⟨Fintype.card V, H, ?_, ?_⟩ · rw [edgeSet_map, Set.ncard_image_of_injective _ e.toEmbedding.sym2Map.injective] change (F(6, m)).edgeSet.ncard = _ rw [forest_edge_ncard] simp [m, Fin.sum_univ_succ] omega · intro R _ let R₀ := R.comap e.toEmbedding let B₀ := (H \ R).comap e.toEmbedding let r (i : Fin 6) := ((Finset.univ : Finset (Fin (m i))).filter (fun j ↦ R₀.Adj ⟨i, 0⟩ ⟨i, j.succ⟩)).card let b (i : Fin 6) := ((Finset.univ : Finset (Fin (m i))).filter (fun j ↦ B₀.Adj ⟨i, 0⟩ ⟨i, j.succ⟩)).card have hAdj (i : Fin 6) (j : Fin (m i)) : B₀.Adj ⟨i, 0⟩ ⟨i, j.succ⟩ ↔ ¬ R₀.Adj ⟨i, 0⟩ ⟨i, j.succ⟩ := by change (H \ R).Adj (e (Sigma.mk i 0)) (e (Sigma.mk i j.succ)) ↔ _ rw [sdiff_adj] have h : H.Adj (e (Sigma.mk i 0)) (e (Sigma.mk i j.succ)) := by apply (map_adj_apply (G := H₀) (f := e.toEmbedding)).mpr exact (forest_adj_iff_center_leaf m _ _).mpr ⟨i, j, Or.inl ⟨rfl, rfl⟩⟩ exact and_iff_right h have hsum (i : Fin 6) : r i + b i = m i := by have hfilter : ((Finset.univ : Finset (Fin (m i))).filter (fun j ↦ B₀.Adj ⟨i, 0⟩ ⟨i, j.succ⟩)) = (Finset.univ.filter (fun j ↦ ¬ R₀.Adj ⟨i, 0⟩ ⟨i, j.succ⟩)) := by ext j simp only [Finset.mem_filter, Finset.mem_univ, true_and, hAdj] dsimp only [r, b] rw [hfilter, Finset.card_filter_add_card_filter_not, Finset.card_univ, Fintype.card_fin] have hm0 : m 0 = 2 * a - 1 := rfl have h0 := hsum 0 rw [hm0] at h0 have hsmall (i : Fin 6) (hi : i ≠ 0) : 3 ≤ r i + b i := by rw [hsum] fin_cases i <;> simp_all [m] <;> omega have hmiddle (i : Fin 6) (hi : i ∈ ({1, 2, 3} : Finset (Fin 6))) : a + 1 ≤ r i + b i := by rw [hsum] fin_cases i <;> simp_all [m] have redOf (h : ∃ f : Fin 3 ↪ Fin 6, ∀ i, Fin.cons (α := fun _ ↦ ℕ) a (fun _ ↦ 2) i ≤ r (f i)) : (F(3, ![a, 2, 2])).IsContained R := by obtain ⟨f, hf⟩ := h have hf' (i : Fin 3) : (![a, 2, 2] : Fin 3 → ℕ) i ≤ r (f i) := by specialize hf i fin_cases i <;> exact hf have hc := forest_copy_of_index_le ![a, 2, 2] m R₀ f hf' exact hc.trans ⟨(SimpleGraph.Embedding.comap e.toEmbedding R).toCopy⟩ have blueOf (h : ∃ f : Fin 4 ↪ Fin 6, ∀ i, Fin.cons (α := fun _ ↦ ℕ) a (fun _ ↦ 2) i ≤ b (f i)) : (F(4, ![a, 2, 2, 2])).IsContained (H \ R) := by obtain ⟨f, hf⟩ := h have hf' (i : Fin 4) : (![a, 2, 2, 2] : Fin 4 → ℕ) i ≤ b (f i) := by specialize hf i fin_cases i <;> exact hf have hc := forest_copy_of_index_le ![a, 2, 2, 2] m B₀ f hf' exact hc.trans ⟨(SimpleGraph.Embedding.comap e.toEmbedding (H \ R)).toCopy⟩ by_cases hr0 : a ≤ r 0 · exact (canonical_profile_case a 2 3 (by decide) (by decide) r b hr0 hsmall hmiddle).imp redOf blueOf · have hb0 : a ≤ b 0 := by omega have hsmall' (i : Fin 6) (hi : i ≠ 0) : 3 ≤ b i + r i := by rw [Nat.add_comm (b i)] exact hsmall i hi have hmiddle' (i : Fin 6) (hi : i ∈ ({1, 2, 3} : Finset (Fin 6))) : a + 1 ≤ b i + r i := by rw [Nat.add_comm (b i)] exact hmiddle i hi exact ((canonical_profile_case a 3 2 (by decide) (by decide) b r hb0 hsmall' hmiddle').imp blueOf redOf).symm theorem starForest_adj {s : ℕ} {d : Fin s → ℕ} (x y : Σ i : Fin s, Fin (d i + 1)) : F(s, d).Adj x y ↔ x ≠ y ∧ x.1 = y.1 ∧ (x.2.val = 0 ∨ y.2.val = 0) := by rw [SimpleGraph.fromRel_adj] tauto theorem starForest_le_degree {V : Type*} [Finite V] {s : ℕ} {d : Fin s → ℕ} {G : SimpleGraph V} (h : F(s, d).IsContained G) (i : Fin s) : ∃ v, d i ≤ (G.neighborSet v).ncard := by classical obtain ⟨f⟩ := h let v := f ⟨i, 0⟩ refine ⟨v, ?_⟩ let g : Fin (d i) → G.neighborSet v := fun j => ⟨f ⟨i, j.succ⟩, f.toHom.map_rel' ((starForest_adj _ _).2 ⟨fun he => Nat.succ_ne_zero j.val (congrArg (fun u => u.2.val) he).symm, rfl, Or.inl rfl⟩)⟩ have hg : Function.Injective g := by intro x y hxy have he := f.injective (congrArg Subtype.val hxy) exact Fin.ext (by have := congrArg (fun u => u.2.val) he; simpa using this) let := Fintype.ofFinite (G.neighborSet v) rw [← Nat.card_coe_set_eq, Nat.card_eq_fintype_card] simpa only [Fintype.card_fin] using Fintype.card_le_of_injective g hg theorem starForest_select {V : Type*} {s t : ℕ} {d : Fin s → ℕ} {e : Fin t → ℕ} {G H : SimpleGraph V} (f : F(s, d).Copy G) (j : Fin t ↪ Fin s) (hj : ∀ i, e i ≤ d (j i)) (hkeep : ∀ x y, F(s, d).Adj x y → x.1 ∈ Set.range j → H.Adj (f x) (f y)) : F(t, e).IsContained H := by let g : (Σ i : Fin t, Fin (e i + 1)) ↪ (Σ i : Fin s, Fin (d i + 1)) := j.sigmaMap fun i => Fin.castLEEmb (Nat.succ_le_succ (hj i)) refine ⟨⟨⟨f ∘ g, ?_⟩, f.injective.comp g.injective⟩⟩ intro x y hxy obtain ⟨hn, he, hz⟩ := (starForest_adj _ _).1 hxy exact hkeep (g x) (g y) ((starForest_adj _ _).2 ⟨g.injective.ne hn, congrArg j he, hz⟩) ⟨x.1, rfl⟩ theorem smallForest_after_delete {V : Type*} {k : ℕ} {d : Fin (k+1) → ℕ} (hd : ∀ i, 2 ≤ d i) {G : SimpleGraph V} (v : V) (h : F(k+1, d).IsContained G) : F(k, fun _ => 2).IsContained (G.deleteIncidenceSet v) := by classical obtain ⟨f⟩ := h let p : Fin (k+1) := if hh : ∃ x, f x = v then (Classical.choose hh).1 else 0 have hp : ∀ x, f x = v → x.1 = p := by intro x hx have hh : ∃ x, f x = v := ⟨x, hx⟩ simpa [p, dif_pos hh] using congrArg Sigma.fst (f.injective (hx.trans (Classical.choose_spec hh).symm)) refine starForest_select f ⟨p.succAbove, Fin.succAbove_right_injective⟩ (fun i => hd (p.succAbove i)) ?_ intro x y hxy hx obtain ⟨i, hi⟩ := hx change p.succAbove i = x.1 at hi have hxp : x.1 ≠ p := hi ▸ p.succAbove_ne i have hyp : y.1 ≠ p := ((starForest_adj _ _).1 hxy).2.1 ▸ hxp exact deleteIncidenceSet_adj.mpr ⟨f.toHom.map_rel' hxy, hxp ∘ hp x, hyp ∘ hp y⟩ theorem edgeCount_delete_vertex {V : Type*} [Finite V] (G : SimpleGraph V) (v : V) : (G.deleteIncidenceSet v).edgeSet.ncard = G.edgeSet.ncard - (G.neighborSet v).ncard := by classical let := Fintype.ofFinite V simpa [Set.ncard_eq_toFinset_card', edgeFinset, degree, neighborFinset] using G.card_edgeFinset_deleteIncidenceSet v theorem blue_delete_color {V : Type*} (G : SimpleGraph V) (v : V) {R : SimpleGraph V} (hR : R ≤ G.deleteIncidenceSet v) : R ≤ G ∧ (G \ R).deleteIncidenceSet v = G.deleteIncidenceSet v \ R := by refine ⟨hR.trans (G.deleteIncidenceSet_le v), ?_⟩ ext x y simp only [deleteIncidenceSet_adj, sdiff_adj] tauto theorem starForest_edges_lower {V : Type*} [Finite V] {s : ℕ} {d : Fin s → ℕ} {G : SimpleGraph V} (h : F(s, d).IsContained G) : (∑ i, d i) ≤ G.edgeSet.ncard := by rw [← forest_edge_ncard d, ← Nat.card_coe_set_eq, ← Nat.card_coe_set_eq] exact Nat.card_le_card_of_injective (Classical.choice h).mapEdgeSet (Classical.choice h).mapEdgeSet.injective theorem oneForest_unique_neighbor {s : ℕ} {x y z : Σ _ : Fin s, Fin 2} (hxy : F(s, fun _ => 1).Adj x y) (hxz : F(s, fun _ => 1).Adj x z) : y = z := by obtain ⟨hn, he, hz⟩ := (starForest_adj _ _).1 hxy obtain ⟨hn', he', hz'⟩ := (starForest_adj _ _).1 hxz obtain ⟨i, x⟩ := x obtain ⟨j, y⟩ := y obtain ⟨k, z⟩ := z change i = j at he change i = k at he' subst j subst k have hy : x ≠ y := fun he => hn (by cases he; rfl) have hz : x ≠ z := fun he => hn' (by cases he; rfl) have hyz : y = z := by have hyy := fun h : x.val = y.val => hy (Fin.ext h) have hzz := fun h : x.val = z.val => hz (Fin.ext h) apply Fin.ext have := x.isLt have := y.isLt have := z.isLt omega cases hyz rfl /-- A maximum matching works as the blue class when the blue target is one two-edge star. -/ theorem mixed_pure_one_avoid (a : ℕ) (ha : 3 ≤ a) {n : ℕ} (G : SimpleGraph (Fin n)) (hm : G.edgeSet.ncard < a + 7) : ∃ R : SimpleGraph (Fin n), R ≤ G ∧ ¬ F(3, ![a,2,2]).IsContained R ∧ ¬ F(1, fun _ => 2).IsContained (G \ R) := by classical let S : Finset (SimpleGraph (Fin n)) := Finset.univ.filter fun M => M ≤ G ∧ ∀ v, (M.neighborSet v).ncard ≤ 1 have hS : S.Nonempty := by refine ⟨⊥, Finset.mem_filter.mpr ⟨Finset.mem_univ _, bot_le, ?_⟩⟩ intro v simp obtain ⟨M, hMS, hbest⟩ := Finset.exists_max_image S (fun M => M.edgeSet.ncard) hS have hM := (Finset.mem_filter.mp hMS).2 refine ⟨G \ M, sdiff_le, ?_, ?_⟩ · intro h have hsmall : F(3, fun _ => 1).IsContained G := starForest_select (Classical.choice h) (Function.Embedding.refl _) (by intro i fin_cases i <;> simp <;> omega) (fun x y hxy _ => (sdiff_le : G \ M ≤ G) ((Classical.choice h).toHom.map_rel' hxy)) obtain ⟨g⟩ := hsmall let N := F(3, fun _ => 1).map g.toEmbedding have hNG : N ≤ G := by intro v w hvw obtain ⟨x, y, hxy, rfl, rfl⟩ := (map_adj g.toEmbedding _ _ _).mp hvw exact g.toHom.map_rel' hxy have hNd (v : Fin n) : (N.neighborSet v).ncard ≤ 1 := by apply (Set.ncard_le_one_iff).mpr intro w w' hw hw' obtain ⟨x, y, hxy, h₁, rfl⟩ := (map_adj g.toEmbedding _ _ _).mp hw obtain ⟨x', y', hx'y', h₂, rfl⟩ := (map_adj g.toEmbedding _ _ _).mp hw' have hh := g.toEmbedding.injective (h₁.trans h₂.symm) subst x' exact congrArg g.toEmbedding (oneForest_unique_neighbor hxy hx'y') have hNcopy : F(3, fun _ => 1).IsContained N := by refine ⟨⟨⟨g.toEmbedding, fun hxy => ?_⟩, g.toEmbedding.injective⟩⟩ exact map_adj_apply.mpr hxy have hn : 3 ≤ N.edgeSet.ncard := by simpa using starForest_edges_lower hNcopy have hml : 3 ≤ M.edgeSet.ncard := hn.trans (hbest N (Finset.mem_filter.mpr ⟨Finset.mem_univ _, hNG, hNd⟩)) have hmixed : a + 4 ≤ (G \ M).edgeSet.ncard := by simpa [Fin.sum_univ_succ, Nat.add_assoc] using starForest_edges_lower h have hcount := Set.ncard_sdiff_add_ncard_of_subset (edgeSet_mono hM.1) rw [← edgeSet_sdiff] at hcount omega · intro h obtain ⟨v, hv⟩ := starForest_le_degree h 0 have hb : G \ (G \ M) = M := sdiff_sdiff_eq_self hM.1 rw [hb] at hv have := hM.2 v omega /-- Remove a vertex of degree at least a + 1 and color all its edges blue. -/ theorem mixed_pure_avoid_of_bounded (a : ℕ) (ha : 3 ≤ a) {n : ℕ} (hbounded : ∀ (k : ℕ), 2 ≤ k → k ≤ 3 → ∀ (G : SimpleGraph (Fin n)), G.edgeSet.ncard < k*(a+1)+6 → (∀ v, (G.neighborSet v).ncard ≤ a) → ∃ R : SimpleGraph (Fin n), R ≤ G ∧ ¬ F(3, ![a,2,2]).IsContained R ∧ ¬ F(k, fun _ => 2).IsContained (G \ R)) (k : ℕ) (hk₁ : 1 ≤ k) (hk₃ : k ≤ 3) (G : SimpleGraph (Fin n)) (hm : G.edgeSet.ncard < k*(a+1)+6) : ∃ R : SimpleGraph (Fin n), R ≤ G ∧ ¬ F(3, ![a,2,2]).IsContained R ∧ ¬ F(k, fun _ => 2).IsContained (G \ R) := by induction k generalizing G with | zero => omega | succ k ih => by_cases hk : k = 0 · subst k apply mixed_pure_one_avoid a ha G simp at hm omega · by_cases hdeg : ∀ v, (G.neighborSet v).ncard ≤ a · exact hbounded (k+1) (by omega) hk₃ G hm hdeg · push Not at hdeg obtain ⟨v, hv⟩ := hdeg have hmdel : (G.deleteIncidenceSet v).edgeSet.ncard < k*(a+1)+6 := by rw [edgeCount_delete_vertex] rw [Nat.succ_mul] at hm omega obtain ⟨R, hR, hr, hb⟩ := ih (by omega) (by omega) _ hmdel obtain ⟨hRG, heq⟩ := blue_delete_color G v hR refine ⟨R, hRG, hr, fun h => hb ?_⟩ rw [← heq] exact smallForest_after_delete (fun _ => le_rfl) v h /-- At degree at least 2a − 1, one blue deletion leaves the mixed versus pure case. -/ theorem three_four_avoid_of_cases (a : ℕ) (ha : 3 ≤ a) {n : ℕ} (hbounded : ∀ (k : ℕ), 2 ≤ k → k ≤ 3 → ∀ (G : SimpleGraph (Fin n)), G.edgeSet.ncard < k*(a+1)+6 → (∀ v, (G.neighborSet v).ncard ≤ a) → ∃ R : SimpleGraph (Fin n), R ≤ G ∧ ¬ F(3, ![a,2,2]).IsContained R ∧ ¬ F(k, fun _ => 2).IsContained (G \ R)) (hbalanced : ∀ (G : SimpleGraph (Fin n)), G.edgeSet.ncard < 5*a+8 → (∀ v, (G.neighborSet v).ncard ≤ 2*a-2) → ∃ R : SimpleGraph (Fin n), R ≤ G ∧ ¬ F(3, ![a,2,2]).IsContained R ∧ ¬ F(4, ![a,2,2,2]).IsContained (G \ R)) (G : SimpleGraph (Fin n)) (hm : G.edgeSet.ncard < 5*a+8) : ∃ R : SimpleGraph (Fin n), R ≤ G ∧ ¬ F(3, ![a,2,2]).IsContained R ∧ ¬ F(4, ![a,2,2,2]).IsContained (G \ R) := by classical by_cases hd : ∀ v, (G.neighborSet v).ncard ≤ 2*a-2 · exact hbalanced G hm hd · push Not at hd obtain ⟨v, hv⟩ := hd have hmdel : (G.deleteIncidenceSet v).edgeSet.ncard < 3*(a+1)+6 := by rw [edgeCount_delete_vertex] omega obtain ⟨R, hR, hr, hb⟩ := mixed_pure_avoid_of_bounded a ha hbounded 3 (by omega) (by omega) _ hmdel obtain ⟨hRG, heq⟩ := blue_delete_color G v hR refine ⟨R, hRG, hr, fun h => hb ?_⟩ rw [← heq] apply smallForest_after_delete _ v h intro i fin_cases i <;> simp <;> omega theorem sizeRamsey_three_four_of_bounds (a : ℕ) (hu : ∃ (n : ℕ) (G : SimpleGraph (Fin n)), G.edgeSet.ncard = 5*a+8 ∧ ∀ R : SimpleGraph (Fin n), R ≤ G → F(3, ![a,2,2]).IsContained R ∨ F(4, ![a,2,2,2]).IsContained (G \ R)) (hl : ∀ (n : ℕ) (G : SimpleGraph (Fin n)), G.edgeSet.ncard < 5*a+8 → ∃ R : SimpleGraph (Fin n), R ≤ G ∧ ¬ F(3, ![a,2,2]).IsContained R ∧ ¬ F(4, ![a,2,2,2]).IsContained (G \ R)) : sizeRamsey F(3, ![a,2,2]) F(4, ![a,2,2,2]) = 5*a+8 := by unfold sizeRamsey classical apply le_antisymm · exact csInf_le (OrderBot.bddBelow _) hu · refine le_csInf ?_ ?_ · exact ⟨5*a+8, hu⟩ intro m hm rcases hm with ⟨n, G, hGn, hRamsey⟩ by_contra! hm obtain ⟨R, hR, hr, hb⟩ := hl n G (hGn ▸ hm) exact (hRamsey R hR).elim hr hb theorem starForest_support_lower {V : Type*} [Finite V] {s : ℕ} {d : Fin s → ℕ} {G : SimpleGraph V} (hd : ∀ i, 0 < d i) (h : F(s, d).IsContained G) : (∑ i, (d i + 1)) ≤ G.support.ncard := by classical obtain ⟨f⟩ := h have hsupport (x : Σ i : Fin s, Fin (d i + 1)) : f x ∈ G.support := by obtain ⟨i, j⟩ := x by_cases hj : j = 0 · subst j have hAdj : F(s, d).Adj ⟨i, 0⟩ ⟨i, (⟨0, hd i⟩ : Fin (d i)).succ⟩ := (forest_adj_iff_center_leaf d _ _).mpr ⟨i, ⟨0, hd i⟩, Or.inl ⟨rfl, rfl⟩⟩ exact (f.toHom.map_rel' hAdj).mem_support_left · obtain ⟨k, rfl⟩ := Fin.exists_succ_eq_of_ne_zero hj have hAdj : F(s, d).Adj ⟨i, 0⟩ ⟨i, k.succ⟩ := (forest_adj_iff_center_leaf d _ _).mpr ⟨i, k, Or.inl ⟨rfl, rfl⟩⟩ exact (f.toHom.map_rel' hAdj).mem_support_right let g : (Σ i : Fin s, Fin (d i + 1)) → G.support := fun x => ⟨f x, hsupport x⟩ have hg : Function.Injective g := fun _ _ he => f.injective (congrArg Subtype.val he) rw [← Nat.card_coe_set_eq] simpa only [Nat.card_eq_fintype_card, Fintype.card_sigma, Fintype.card_fin] using Nat.card_le_card_of_injective g hg /-- The graph on the exceptional regular part is the entire blue class. -/ theorem avoid_three_four_of_regular_part (a : ℕ) (ha : 4 ≤ a) {n : ℕ} (G : SimpleGraph (Fin n)) (hm : G.edgeSet.ncard < 5*a+8) (S : Finset (Fin n)) (hS : S.Nonempty) (hclosed : ∀ ⦃v w⦄, v ∈ S → G.Adj v w → w ∈ S) (hdegrees : ∀ v ∈ S, (G.neighborSet v).ncard = 2*a-2) : ∃ R : SimpleGraph (Fin n), R ≤ G ∧ ¬ F(3, ![a,2,2]).IsContained R ∧ ¬ F(4, ![a,2,2,2]).IsContained (G \ R) := by classical let B := G ⊓ fromRel fun v w => v ∈ S ∧ w ∈ S have hBG : B ≤ G := inf_le_left have hBAdj {v w : Fin n} : B.Adj v w ↔ G.Adj v w ∧ v ∈ S ∧ w ∈ S := by change (G.Adj v w ∧ (v ≠ w ∧ (v ∈ S ∧ w ∈ S ∨ w ∈ S ∧ v ∈ S))) ↔ _ have h := fun h : G.Adj v w => h.ne tauto have hBsupport : B.support ⊆ (S : Set (Fin n)) := by intro v hv obtain ⟨w, hw⟩ := B.mem_support.mp hv exact (hBAdj.mp hw).2.1 have hBdegree (v : Fin n) : (B.neighborSet v).ncard = if v ∈ S then 2*a-2 else 0 := by by_cases hv : v ∈ S · rw [if_pos hv, ← hdegrees v hv] congr 1 ext w exact hBAdj.trans ⟨And.left, fun h => ⟨h, hv, hclosed hv h⟩⟩ · have hzero : B.neighborSet v = ∅ := by ext w exact ⟨fun hw => (hv (hBAdj.mp hw).2.1).elim, fun hw => hw.elim⟩ simp [hv, hzero] have hsum : 2 * B.edgeSet.ncard = S.card * (2*a-2) := by calc 2 * B.edgeSet.ncard = 2 * (@edgeFinset _ B B.fintypeEdgeSet).card := congrArg (2 * ·) (@Set.ncard_eq_toFinset_card' _ _ B.fintypeEdgeSet) _ = ∑ v, B.degree v := B.sum_degrees_eq_twice_card_edges.symm _ = ∑ v, if v ∈ S then 2*a-2 else 0 := by apply Finset.sum_congr rfl intro v _ exact (card_neighborSet_eq_degree B v).symm.trans ((Nat.card_eq_fintype_card (α := B.neighborSet v)).symm.trans (hBdegree v)) _ = S.card * (2*a-2) := by rw [← Finset.sum_filter] simp obtain ⟨v, hv⟩ := hS have hsub : G.neighborSet v ⊆ (S.erase v : Set (Fin n)) := by intro w hw exact Finset.mem_erase.mpr ⟨hw.ne', hclosed hv hw⟩ have hsize : 2*a-1 ≤ S.card := by have h := Set.ncard_le_ncard hsub rw [hdegrees v hv, Set.ncard_coe_finset, Finset.card_erase_of_mem hv] at h omega have hcounts := Set.ncard_sdiff_add_ncard_of_subset (edgeSet_mono hBG) rw [← edgeSet_sdiff] at hcounts have ha4 : a = 4 := by by_contra hne have h9 : 9 ≤ S.card := by omega have h := Nat.mul_le_mul_right (2*a-2) h9 omega subst a have hBm : (G \ B).edgeSet.ncard < 8 := by omega have hBs : B.support.ncard < 14 := by have hh := Set.ncard_le_ncard hBsupport rw [Set.ncard_coe_finset] at hh omega refine ⟨G \ B, sdiff_le, ?_, ?_⟩ · intro hc have h : 8 ≤ (G \ B).edgeSet.ncard := by simpa [Fin.sum_univ_succ] using starForest_edges_lower hc omega · rw [sdiff_sdiff_eq_self hBG] intro hc have hpos (i : Fin 4) : 0 < (![4,2,2,2] : Fin 4 → ℕ) i := by fin_cases i <;> decide have h : 14 ≤ B.support.ncard := by simpa [Fin.sum_univ_succ] using starForest_support_lower hpos hc omega theorem three_four_avoid_bounded_of_balance (a : ℕ) {n : ℕ} (G : SimpleGraph (Fin n)) (hm : G.edgeSet.ncard < 5*a+8) (h : (∃ R : SimpleGraph (Fin n), R ≤ G ∧ ∀ v, (R.neighborSet v).ncard < a ∧ ((G \ R).neighborSet v).ncard < a) ∨ (4 ≤ a ∧ ∃ S : Finset (Fin n), S.Nonempty ∧ (∀ ⦃v w⦄, v ∈ S → G.Adj v w → w ∈ S) ∧ (∀ v ∈ S, (G.neighborSet v).ncard = 2*a-2))) : ∃ R : SimpleGraph (Fin n), R ≤ G ∧ ¬ F(3, ![a,2,2]).IsContained R ∧ ¬ F(4, ![a,2,2,2]).IsContained (G \ R) := by rcases h with ⟨R, hRG, hdeg⟩ | ⟨ha', S, hS, hclosed, hdegrees⟩ · refine ⟨R, hRG, ?_, ?_⟩ · intro h obtain ⟨v, hv⟩ := starForest_le_degree h 0 exact (not_lt_of_ge hv) (hdeg v).1 · intro h obtain ⟨v, hv⟩ := starForest_le_degree h 0 exact (not_lt_of_ge hv) (hdeg v).2 · exact avoid_three_four_of_regular_part a ha' G hm S hS hclosed hdegrees theorem bd_degree_count {V : Type*} [Fintype V] [DecidableEq V] {G : SimpleGraph V} [DecidableRel G.Adj] {u v : V} {p : G.Walk u v} (h : p.IsTrail) [DecidableRel (G.deleteEdges p.edgeSet).Adj] (x : V) : (G.degree x = (p.edges.countP (fun e => x ∈ e)) + (G.deleteEdges p.edgeSet).degree x) := by classical let L := h.edgesFinset let H := G.deleteEdges p.edgeSet have hL : L ⊆ G.edgeFinset := by intro e he exact G.mem_edgeFinset.mpr (p.edges_subset_edgeSet (by change e ∈ p.edges at he; exact he)) have he : H.edgeFinset = G.edgeFinset \ L := by ext e rw [Finset.mem_sdiff, mem_edgeFinset, mem_edgeFinset, edgeSet_deleteEdges] rfl have hfilter : H.incidenceFinset x = G.incidenceFinset x \ L.filter (x ∈ ·) := by rw [H.incidenceFinset_eq_filter, G.incidenceFinset_eq_filter, he] ext e simp only [Finset.mem_filter, Finset.mem_sdiff] tauto have hsub : L.filter (x ∈ ·) ⊆ G.incidenceFinset x := by rw [G.incidenceFinset_eq_filter] exact Finset.filter_subset_filter _ hL have hc : (L.filter (x ∈ ·)).card = p.edges.countP (fun e => x ∈ e) := by change ((p.edges : Multiset (Sym2 V)).filter (x ∈ ·)).card = _ rw [← Multiset.countP_eq_card_filter, Multiset.coe_countP] have hd := Finset.card_sdiff_of_subset hsub rw [← hfilter, H.card_incidenceFinset_eq_degree, G.card_incidenceFinset_eq_degree, hc] at hd have hle := Finset.card_le_card hsub rw [G.card_incidenceFinset_eq_degree, hc] at hle change G.degree x = _ + H.degree x exact (Nat.sub_eq_iff_eq_add hle |>.mp hd.symm).trans (Nat.add_comm _ _) theorem bd_euler {V : Type*} [Fintype V] [DecidableEq V] (G : SimpleGraph V) [DecidableRel G.Adj] (hconn : G.Preconnected) (heven : ∀ v, Even (G.degree v)) (x : V) : ∃ p : G.Walk x x, p.IsEulerian := by classical let P : ℕ → Prop := fun k => ∃ u v, ∃ p : G.Walk u v, p.IsTrail ∧ p.length = k let M := G.edgeFinset.card let k := Nat.findGreatest P M have hx : P 0 := ⟨x, x, .nil, by simp, rfl⟩ have hk : P k := Nat.findGreatest_spec (Nat.zero_le M) hx have hmax : ∀ {u v} (p : G.Walk u v), p.IsTrail → p.length ≤ k := by intro u v p h have hle : p.length ≤ M := by have hs : h.edgesFinset ⊆ G.edgeFinset := by intro e he exact G.mem_edgeFinset.mpr (p.edges_subset_edgeSet (by change e ∈ p.edges at he; exact he)) have hh := Finset.card_le_card hs change p.edges.length ≤ M at hh simpa using hh exact Nat.le_findGreatest hle ⟨u, v, p, h, rfl⟩ obtain ⟨u, v, p, hp, hpk⟩ := hk have hextend {s t z : V} (q : G.Walk s t) (hq : q.IsTrail) (hql : q.length = k) (h : G.Adj t z) (hfresh : Sym2.mk t z ∉ q.edges) : False := by have htrail : (q.concat h).IsTrail := by rw [Walk.isTrail_def, Walk.edges_concat, List.nodup_concat] exact ⟨hfresh, hq.edges_nodup⟩ have hn := hmax _ htrail rw [Walk.length_concat, hql] at hn omega have huv : u = v := by by_contra hne have hodd : ¬ Even (p.edges.countP (fun e => v ∈ e)) := by rw [hp.even_countP_edges_iff] simp [hne] have hhpos : 0 < (G.deleteEdges p.edgeSet).degree v := by by_contra! hh have hc := bd_degree_count hp v have h0 := heven v have hd : (G.deleteEdges p.edgeSet).degree v = 0 := by omega rw [hd, Nat.add_zero] at hc exact hodd (hc ▸ h0) have hn : (G.deleteEdges p.edgeSet).neighborFinset v |>.Nonempty := Finset.card_pos.mp hhpos obtain ⟨z, hz⟩ := hn have hadj := (G.deleteEdges p.edgeSet).mem_neighborFinset v z |>.mp hz have hem := (G.deleteEdges p.edgeSet).mem_edgeSet.mpr hadj rw [edgeSet_deleteEdges] at hem exact hextend p hp hpk (G.mem_edgeSet.mp hem.1) hem.2 subst v have hrotate (y : V) (hy : y ∈ p.support) : ∃ q : G.Walk y y, q.IsTrail ∧ q.length = k ∧ q.edgeSet = p.edgeSet := by obtain ⟨p₁, p₂, rfl⟩ := p.mem_support_iff_exists_append.mp hy refine ⟨p₂.append p₁, ?_, ?_, ?_⟩ · have hnodup := hp.edges_nodup rw [Walk.edges_append, List.nodup_append] at hnodup rw [Walk.isTrail_def, Walk.edges_append, List.nodup_append] exact ⟨hnodup.2.1, hnodup.1, fun a ha b hb hab => hnodup.2.2 b hb a ha hab.symm⟩ · simpa [Walk.length_append, Nat.add_comm] using hpk · simp [Walk.edgeSet_append, Set.union_comm] have hpall : ∀ e ∈ G.edgeSet, e ∈ p.edges := by intro e he by_contra hfresh obtain ⟨y, z⟩ := e have hex : ∃ w z, w ∈ p.support ∧ G.Adj w z ∧ Sym2.mk w z ∉ p.edges := by by_cases hy : y ∈ p.support · exact ⟨y, z, hy, G.mem_edgeSet.mp he, hfresh⟩ · obtain ⟨q⟩ := hconn u y obtain ⟨d, _, hd1, hd2⟩ := q.exists_boundary_dart {w | w ∈ p.support} p.start_mem_support hy refine ⟨d.fst, d.snd, hd1, d.adj, ?_⟩ intro hdmem apply hd2 exact p.mem_support_iff_exists_mem_edges.mpr (Or.inr ⟨d.edge, hdmem, Sym2.mem_mk_right _ _⟩) obtain ⟨w, z, hw, h, hf⟩ := hex obtain ⟨q, hq, hql, hqr⟩ := hrotate w hw apply hextend q hq hql h change Sym2.mk w z ∉ q.edgeSet rw [hqr] exact hf have hs : x ∈ p.support := by by_contra hx obtain ⟨q⟩ := hconn u x obtain ⟨d, _, _, hd⟩ := q.exists_boundary_dart {v | v ∈ p.support} p.start_mem_support hx have he := hpall d.edge d.edge_mem exact hd (p.mem_support_iff_exists_mem_edges.mpr (Or.inr ⟨_, he, Sym2.mem_mk_right _ _⟩)) obtain ⟨q, hq, _, hqr⟩ := hrotate x hs refine ⟨q, hq.isEulerian_of_forall_mem ?_⟩ intro e he change e ∈ q.edgeSet rw [hqr] exact hpall e he def bdCut {E : Type*} [DecidableEq E] (b : Bool) : List E → Finset E | [] => ∅ | e :: l => if b then insert e (bdCut false l) else bdCut true l theorem bdCut_mem {E : Type*} [DecidableEq E] (b : Bool) {l : List E} {e : E} (h : e ∈ bdCut b l) : e ∈ l := by induction l generalizing b with | nil => simp [bdCut] at h | cons a l ih => cases b · exact List.mem_cons_of_mem _ (ih true (by simpa [bdCut] using h)) · simp only [bdCut, ↓reduceIte, Finset.mem_insert] at h exact h.elim (fun h => h ▸ List.mem_cons_self) (List.mem_cons_of_mem _ ∘ ih false) theorem bdCut_compl {E : Type*} [DecidableEq E] {l : List E} (hn : l.Nodup) : l.toFinset \ bdCut true l = bdCut false l ∧ l.toFinset \ bdCut false l = bdCut true l := by induction l with | nil => simp [bdCut] | cons e l ih => have hn' := List.nodup_cons.mp hn obtain ⟨ht, hf⟩ := ih hn'.2 have het : e ∉ bdCut true l := fun h => hn'.1 (bdCut_mem true h) have hef : e ∉ bdCut false l := fun h => hn'.1 (bdCut_mem false h) constructor <;> ext a <;> by_cases ha : a = e · subst a simp [bdCut, het] · simp [bdCut, ha, ← hf] · subst a simp [bdCut, het] · simp [bdCut, ha, ← ht] theorem bdCut_walk_counts {V : Type*} [DecidableEq V] (G : SimpleGraph V) {u v : V} (p : G.Walk u v) (ht : p.IsTrail) (x : V) : ((bdCut true p.edges).filter (x ∈ ·)).card - (((bdCut false p.edges).filter (x ∈ ·)).card : ℤ) = (if x = u then 1 else 0) + (if x = v then if Even p.length then -1 else 1 else 0) := by induction p with | @nil a => by_cases h : x = a <;> simp [bdCut, h] | @cons a b c hab q ih => obtain ⟨hq, hfresh⟩ := (Walk.isTrail_cons hab q).mp ht specialize ih hq have hcut : Sym2.mk a b ∉ bdCut false q.edges := fun h => hfresh (bdCut_mem false h) have hfilter : Sym2.mk a b ∉ (bdCut false q.edges).filter (x ∈ ·) := fun h => hcut (Finset.mem_filter.mp h).1 have hinc : (if x ∈ Sym2.mk a b then (1 : ℤ) else 0) = (if x = a then 1 else 0) + (if x = b then 1 else 0) := by have hne := hab.ne simp only [Sym2.mem_iff] split_ifs <;> simp_all simp only [Walk.edges_cons, bdCut, ↓reduceIte, Bool.false_eq_true, Walk.length_cons, Finset.filter_insert, Nat.even_add_one] have hinsert := Finset.card_insert_of_notMem hfilter have hadd : (((if x ∈ Sym2.mk a b then insert (Sym2.mk a b) ((bdCut false q.edges).filter (x ∈ ·)) else (bdCut false q.edges).filter (x ∈ ·)).card) : ℤ) = ((bdCut false q.edges).filter (x ∈ ·)).card + (if x ∈ Sym2.mk a b then (1 : ℤ) else 0) := by by_cases h : x ∈ Sym2.mk a b <;> simp only [h, ↓reduceIte, hinsert, Nat.cast_add, Nat.cast_one, add_zero] rw [hadd, hinc] by_cases hc : x = c <;> by_cases hp : Even q.length <;> simp [hc, hp] at ih ⊢ <;> omega theorem bd_ncard_degree {V : Type*} [Fintype V] {G : SimpleGraph V} [DecidableRel G.Adj] (v : V) : (G.neighborSet v).ncard = G.degree v := Set.ncard_eq_toFinset_card' _ theorem bd_ncard_edges {V : Type*} [Fintype V] [DecidableEq V] {G : SimpleGraph V} [DecidableRel G.Adj] : G.edgeSet.ncard = G.edgeFinset.card := Set.ncard_eq_toFinset_card' _ theorem bd_degree_partition {V : Type*} [Finite V] {G R : SimpleGraph V} (hR : R ≤ G) (v : V) : (R.neighborSet v).ncard + ((G \ R).neighborSet v).ncard = (G.neighborSet v).ncard := by have hsub : R.neighborSet v ⊆ G.neighborSet v := fun _ hw => hR hw have hb : (G \ R).neighborSet v = G.neighborSet v \ R.neighborSet v := rfl rw [hb, Nat.add_comm, Set.ncard_sdiff_add_ncard_of_subset hsub] theorem bd_balance_euler {V : Type*} [Fintype V] [DecidableEq V] (G : SimpleGraph V) (hconn : G.Preconnected) (heven : ∀ v, Even (G.neighborSet v).ncard) (x : V) : ∃ R : SimpleGraph V, R ≤ G ∧ ∀ v, ((G \ R).neighborSet v).ncard - ((R.neighborSet v).ncard : ℤ) = if v = x ∧ Odd G.edgeSet.ncard then 2 else 0 := by classical obtain ⟨p, hp⟩ := bd_euler G hconn (fun v => by simpa [bd_ncard_degree] using heven v) x let X := bdCut true p.edges let Y := bdCut false p.edges let R := fromEdgeSet (Y : Set (Sym2 V)) have hY : (Y : Set (Sym2 V)) ⊆ G.edgeSet := fun _ he => p.edges_subset_edgeSet (bdCut_mem false he) have hR : R ≤ G := (fromEdgeSet_le (G := G)).mpr (sdiff_le.trans hY) refine ⟨R, hR, fun v => ?_⟩ have heR : R.edgeFinset = Y := by ext e rw [mem_edgeFinset, edgeSet_fromEdgeSet] exact ⟨fun h => h.1, fun h => ⟨h, G.not_isDiag_of_mem_edgeSet (hY h)⟩⟩ have heG : G.edgeFinset = p.edges.toFinset := by ext e exact G.mem_edgeFinset.trans (hp.mem_edges_iff.symm.trans List.mem_toFinset.symm) have heB : (G \ R).edgeFinset = X := by rw [edgeFinset_sdiff, heR, heG] exact (bdCut_compl hp.isTrail.edges_nodup).2 have hp_len : p.length = G.edgeSet.ncard := by rw [bd_ncard_edges, ← hp.edgesFinset_eq] change _ = p.edges.length exact p.length_edges.symm rw [bd_ncard_degree, bd_ncard_degree, ← card_incidenceFinset_eq_degree, ← card_incidenceFinset_eq_degree, incidenceFinset_eq_filter, incidenceFinset_eq_filter, heR, heB] have hh := bdCut_walk_counts G p hp.isTrail v rw [hp_len] at hh by_cases hv : v = x <;> by_cases he : Even G.edgeSet.ncard <;> simpa [X, Y, hv, he, ← Nat.not_even_iff_odd] using hh def bdAug {V : Type*} (G : SimpleGraph V) : SimpleGraph (Option V) where Adj | some v, some w => G.Adj v w | some v, none => Odd (G.neighborSet v).ncard | none, some v => Odd (G.neighborSet v).ncard | none, none => False symm := ⟨by rintro (v | v) (w | w) h <;> first | exact h | exact G.symm.symm _ _ h⟩ loopless := ⟨by intro v h; cases v <;> first | exact h | exact G.loopless.irrefl _ h⟩ theorem bdAug_ncard_some {V : Type*} (G : SimpleGraph V) [Finite V] (v : V) : ((bdAug G).neighborSet (some v)).ncard = (G.neighborSet v).ncard + if Odd (G.neighborSet v).ncard then 1 else 0 := by classical let S : Set (Option V) := Option.some '' G.neighborSet v have hS : S.ncard = (G.neighborSet v).ncard := Set.ncard_image_of_injective _ (Option.some_injective V) have h0 : none ∉ S := by simp [S] by_cases hv : Odd (G.neighborSet v).ncard · have he : (bdAug G).neighborSet (some v) = insert none S := by ext (w | w) · simp [S, bdAug, hv] · simp [S, bdAug, mem_neighborSet] rw [he, Set.ncard_insert_of_notMem h0, hS, if_pos hv] · have he : (bdAug G).neighborSet (some v) = S := by ext (w | w) · simpa [S, bdAug, mem_neighborSet] using hv · simp [S, bdAug, mem_neighborSet] rw [he, hS, if_neg hv, Nat.add_zero] theorem bdAug_even {V : Type*} [Fintype V] (G : SimpleGraph V) (v : Option V) : Even ((bdAug G).neighborSet v).ncard := by classical cases v with | none => have he : (bdAug G).neighborSet none = Option.some '' {w : V | Odd (G.degree w)} := by ext (w | w) <;> simp [bdAug, mem_neighborSet, bd_ncard_degree] rw [he, Set.ncard_image_of_injective _ (Option.some_injective V), Set.ncard_eq_toFinset_card'] simpa using G.even_card_odd_degree_vertices | some w => rw [bdAug_ncard_some] by_cases h : Odd (G.neighborSet w).ncard · simp [h] · rw [if_neg h, Nat.add_zero] exact Nat.not_odd_iff_even.mp h theorem bdAug_connected {V : Type*} (G : SimpleGraph V) (hc : G.Preconnected) {v : V} (hv : Odd (G.neighborSet v).ncard) : (bdAug G).Preconnected := by let f : G →g bdAug G := ⟨Option.some, fun h => h⟩ have ha : (bdAug G).Adj none (some v) := hv have hw (w : Option V) : (bdAug G).Reachable none w := by cases w with | none => rfl | some w => exact ha.reachable.trans ((hc v w).map f) intro x y exact (hw x).symm.trans (hw y) theorem bdAug_pull_degree {V : Type*} [Finite V] (H : SimpleGraph (Option V)) (v : V) : ((H.comap Option.some).neighborSet v).ncard ≤ (H.neighborSet (some v)).ncard := by let S := (H.comap Option.some).neighborSet v have hS : Option.some '' S ⊆ H.neighborSet (some v) := by rintro _ ⟨w, hw, rfl⟩ exact hw simpa [S, Set.ncard_image_of_injective _ (Option.some_injective V)] using Set.ncard_le_ncard hS theorem bd_balance_odd {V : Type*} [Fintype V] [DecidableEq V] (G : SimpleGraph V) (hc : G.Preconnected) (ho : ∃ v, Odd (G.neighborSet v).ncard) (a : ℕ) (ha : 0 < a) (hD : ∀ v, (G.neighborSet v).ncard ≤ 2*a-2) : ∃ R : SimpleGraph V, R ≤ G ∧ ∀ v, (R.neighborSet v).ncard < a ∧ ((G \ R).neighborSet v).ncard < a := by classical obtain ⟨v, hv⟩ := ho obtain ⟨R, hR, hbal⟩ := bd_balance_euler (bdAug G) (bdAug_connected G hc hv) (bdAug_even G) none have hsub : R.comap Option.some ≤ G := fun _ _ h => hR h refine ⟨R.comap Option.some, hsub, fun w => ?_⟩ have hdiff := hbal (some w) simp only [Option.some_ne_none, false_and, ↓reduceIte, sub_eq_zero] at hdiff have hsum := bd_degree_partition hR (some w) rw [bdAug_ncard_some] at hsum have hr := bdAug_pull_degree R w have hb := bdAug_pull_degree (bdAug G \ R) w change ((G \ R.comap Option.some).neighborSet w).ncard ≤ _ at hb have hD' := hD w by_cases hw : Odd (G.neighborSet w).ncard · rw [if_pos hw] at hsum omega · rw [if_neg hw] at hsum omega theorem bd_component_degree {V : Type*} (G : SimpleGraph V) (C : G.ConnectedComponent) (v : C) : (C.toSimpleGraph.neighborSet v).ncard = (G.neighborSet v.val).ncard := by classical have h : Subtype.val '' C.toSimpleGraph.neighborSet v = G.neighborSet v.val := by ext w constructor · rintro ⟨u, h, rfl⟩ exact h · intro hw exact ⟨⟨w, C.mem_supp_of_adj_mem_supp v.property hw⟩, hw, rfl⟩ rw [← h, Set.ncard_image_of_injective _ Subtype.val_injective] theorem bd_glue_colorings {V : Type*} [Fintype V] (G : SimpleGraph V) (a : ℕ) (h : ∀ C : G.ConnectedComponent, ∃ R : SimpleGraph C, R ≤ C.toSimpleGraph ∧ ∀ v, (R.neighborSet v).ncard < a ∧ ((C.toSimpleGraph \ R).neighborSet v).ncard < a) : ∃ R : SimpleGraph V, R ≤ G ∧ ∀ v, (R.neighborSet v).ncard < a ∧ ((G \ R).neighborSet v).ncard < a := by classical choose rc hrc hbound using h let R : SimpleGraph V := ⨆ C, (rc C).spanningCoe have hc_le (C : G.ConnectedComponent) : (rc C).spanningCoe ≤ G := by change (rc C).map _ ≤ G exact (map_monotone _ (hrc C)).trans (G.spanningCoe_induce_le C.supp) have hR : R ≤ G := iSup_le hc_le refine ⟨R, hR, fun v => ?_⟩ let C := G.connectedComponentMk v let vc : C := ⟨v, rfl⟩ have hrc_adj {w : V} (hw : w ∈ C.supp) : R.Adj v w ↔ (rc C).Adj vc ⟨w, hw⟩ := by change (⨆ D, (rc D).spanningCoe).Adj v w ↔ _ rw [iSup_adj] constructor · rintro ⟨D, hd⟩ have hD : v ∈ D.supp := by have hh := hd.mem_support_left rw [support_spanningCoe] at hh obtain ⟨z, _, hze⟩ := hh exact hze ▸ z.property have he : C = D := D.mem_supp_iff v |>.mp hD subst D exact map_adj_apply.mp hd · intro hadj exact ⟨C, map_adj_apply.mpr hadj⟩ have hr : Subtype.val '' (rc C).neighborSet vc = R.neighborSet v := by ext w constructor · rintro ⟨z, hz, rfl⟩ exact (hrc_adj z.property).mpr hz · intro hr have hw : w ∈ C.supp := C.mem_supp_of_adj_mem_supp vc.property (hR hr) exact ⟨⟨w, hw⟩, (hrc_adj hw).mp hr, rfl⟩ have hb : Subtype.val '' (C.toSimpleGraph \ rc C).neighborSet vc = (G \ R).neighborSet v := by ext w constructor · rintro ⟨z, hz, rfl⟩ exact ⟨hz.1, fun h => hz.2 ((hrc_adj z.property).mp h)⟩ · intro hb have hw : w ∈ C.supp := C.mem_supp_of_adj_mem_supp vc.property hb.1 exact ⟨⟨w, hw⟩, ⟨hb.1, fun h => hb.2 ((hrc_adj hw).mpr h)⟩, rfl⟩ rw [← hr, ← hb, Set.ncard_image_of_injective _ Subtype.val_injective, Set.ncard_image_of_injective _ Subtype.val_injective] exact hbound C vc theorem bd_component_balance {V : Type*} [Fintype V] [DecidableEq V] (G : SimpleGraph V) (hc : G.Connected) (a : ℕ) (ha : 2 ≤ a) (hD : ∀ v, (G.neighborSet v).ncard ≤ 2*a-2) (hbad : ¬ ((∀ v, (G.neighborSet v).ncard = 2*a-2) ∧ Odd G.edgeSet.ncard)) : ∃ R : SimpleGraph V, R ≤ G ∧ ∀ v, (R.neighborSet v).ncard < a ∧ ((G \ R).neighborSet v).ncard < a := by classical by_cases ho : ∃ v, Odd (G.neighborSet v).ncard · exact bd_balance_odd G hc.preconnected ho a (by omega) hD push Not at ho have heven (v : V) : Even (G.neighborSet v).ncard := Nat.not_odd_iff_even.mp (ho v) have hchoose : ∃ x : V, Odd G.edgeSet.ncard → (G.neighborSet x).ncard < 2*a-2 := by by_cases hm : Odd G.edgeSet.ncard · have h : ¬ ∀ x, (G.neighborSet x).ncard = 2*a-2 := fun hall => hbad ⟨hall, hm⟩ push Not at h obtain ⟨x, hx⟩ := h exact ⟨x, fun _ => lt_of_le_of_ne (hD x) hx⟩ · obtain ⟨x⟩ := hc.nonempty exact ⟨x, fun h => (hm h).elim⟩ obtain ⟨x, hx⟩ := hchoose obtain ⟨R, hR, hbal⟩ := bd_balance_euler G hc.preconnected heven x refine ⟨R, hR, fun v => ?_⟩ have hP := bd_degree_partition hR v have hd := hD v have hb := hbal v by_cases hve : v = x ∧ Odd G.edgeSet.ncard · rw [if_pos hve] at hb have hsmall := hve.1 ▸ hx hve.2 obtain ⟨k, hk⟩ := heven v omega · rw [if_neg hve] at hb omega theorem bd_balance_or_regular {V : Type*} [Fintype V] (G : SimpleGraph V) (a : ℕ) (ha : 3 ≤ a) (hD : ∀ v, (G.neighborSet v).ncard ≤ 2*a-2) : (∃ R : SimpleGraph V, R ≤ G ∧ ∀ v, (R.neighborSet v).ncard < a ∧ ((G \ R).neighborSet v).ncard < a) ∨ (4 ≤ a ∧ ∃ S : Finset V, S.Nonempty ∧ (∀ ⦃v w⦄, v ∈ S → G.Adj v w → w ∈ S) ∧ (∀ v ∈ S, (G.neighborSet v).ncard = 2*a-2)) := by classical by_cases hbad : ∃ C : G.ConnectedComponent, (∀ v : C, (C.toSimpleGraph.neighborSet v).ncard = 2*a-2) ∧ Odd C.toSimpleGraph.edgeSet.ncard · obtain ⟨C, hreg, ho⟩ := hbad have ha4 : 4 ≤ a := by by_contra! hk have he : a = 3 := by omega subst a have hsum := C.toSimpleGraph.sum_degrees_eq_twice_card_edges have h4 : ∀ v : C, C.toSimpleGraph.degree v = 4 := by intro v rw [← bd_ncard_degree] exact hreg v simp only [h4, Finset.sum_const, Finset.card_univ, smul_eq_mul, ← bd_ncard_edges] at hsum exact Nat.not_even_iff_odd.mpr ho ⟨Fintype.card C, by omega⟩ refine Or.inr ⟨ha4, C.supp.toFinset, ?_, ?_, ?_⟩ · obtain ⟨v, hv⟩ := C.nonempty_supp exact ⟨v, Set.mem_toFinset.mpr hv⟩ · intro v w hv hadj exact Set.mem_toFinset.mpr (C.mem_supp_of_adj_mem_supp (Set.mem_toFinset.mp hv) hadj) · intro v hv have hvc : v ∈ C.supp := by simpa only [Set.mem_toFinset] using hv let vc : C := ⟨v, hvc⟩ exact (bd_component_degree G C vc).symm.trans (hreg vc) · refine Or.inl (bd_glue_colorings G a fun C => ?_) let : Fintype C := Fintype.ofFinite _ refine bd_component_balance C.toSimpleGraph C.connected_toSimpleGraph a (by omega) (fun v => (bd_component_degree G C v).trans_le (hD v.val)) ?_ exact fun h => hbad ⟨C, h⟩ /-- The bounded degree branch uses balanced colors or colors a regular part blue. -/ theorem three_four_avoid_bounded (a : ℕ) (ha : 3 ≤ a) {n : ℕ} (G : SimpleGraph (Fin n)) (hm : G.edgeSet.ncard < 5*a+8) (hd : ∀ v, (G.neighborSet v).ncard ≤ 2*a-2) : ∃ R : SimpleGraph (Fin n), R ≤ G ∧ ¬ F(3, ![a,2,2]).IsContained R ∧ ¬ F(4, ![a,2,2,2]).IsContained (G \ R) := by exact three_four_avoid_bounded_of_balance a G hm (bd_balance_or_regular G a ha hd) def DegreeBound.Admissible {V : Type*} (G : SimpleGraph V) (p : V → V) : Prop := Function.Involutive p ∧ ∀ x, p x = x ∨ G.Adj x (p x) def DegreeBound.weightTerm {V : Type*} [DecidableEq V] (w : V → ℕ) (p : V → V) (x : V) : ℕ := if p x = x then 0 else w x def DegreeBound.weight {V : Type*} [Fintype V] [DecidableEq V] (w : V → ℕ) (p : V → V) : ℕ := ∑ x, weightTerm w p x theorem DegreeBound.no_improvement {V : Type*} [Fintype V] [DecidableEq V] {G : SimpleGraph V} {w : V → ℕ} {p q : V → V} (maximal : ∀ q, Admissible G q → weight w q ≤ weight w p) (hq : Admissible G q) (s : Finset V) (hout : ∀ x ∉ s, q x = p x) (hin : (∑ x ∈ s, weightTerm w p x) < ∑ x ∈ s, weightTerm w q x) : False := by classical have hpo := Finset.sum_sdiff (f := weightTerm w p) (Finset.subset_univ s) have hqo := Finset.sum_sdiff (f := weightTerm w q) (Finset.subset_univ s) have he : (∑ x ∈ Finset.univ \ s, weightTerm w p x) = ∑ x ∈ Finset.univ \ s, weightTerm w q x := by refine Finset.sum_congr rfl ?_ intro x hx rw [weightTerm, weightTerm, hout x (Finset.mem_sdiff.mp hx).2] have hz := maximal q hq unfold weight at hz omega theorem DegreeBound.matching_facts {V : Type*} [Fintype V] [DecidableEq V] (G : SimpleGraph V) (w : V → ℕ) (hw : ∀ x, 0 < w x) : ∃ p : V → V, Admissible G p ∧ (∀ ⦃u v⦄, p u = u → G.Adj u v → p v ≠ v) ∧ (∀ ⦃u x⦄, p u = u → G.Adj u x → w u ≤ w (p x)) ∧ (∀ ⦃u x y v⦄, p u = u → p v = v → u ≠ v → G.Adj u x → G.Adj y v → p x = y → x ≠ y → False) := by classical let S := Finset.univ.filter (Admissible G) have hS : S.Nonempty := ⟨id, Finset.mem_filter.mpr ⟨Finset.mem_univ _, by exact ⟨fun _ => rfl, fun _ => Or.inl rfl⟩⟩⟩ obtain ⟨p, hpS, hm⟩ := Finset.exists_max_image S (weight w) hS have hp : Admissible G p := (Finset.mem_filter.mp hpS).2 have hmax : ∀ q, Admissible G q → weight w q ≤ weight w p := fun q hq => hm q (Finset.mem_filter.mpr ⟨Finset.mem_univ _, hq⟩) have hinvol : ∀ x, p (p x) = x := hp.1 have hgood : ∀ x, p x = x ∨ G.Adj x (p x) := hp.2 have hinj : Function.Injective p := hp.1.injective have htwo : ∀ ⦃u v⦄, p u = u → G.Adj u v → p v ≠ v := by intro u v hu huv hv have hne := huv.ne let q : V → V := fun x => if x = u then v else if x = v then u else p x have hqu : q u = v := by simp [q] have hqv : q v = u := by simp [q, hne.symm] have hqo : ∀ x, x ≠ u → x ≠ v → q x = p x := by intro x hxu hxv simp [q, hxu, hxv] have hq : Admissible G q := by refine ⟨?_, ?_⟩ · intro x by_cases hxu : x = u · subst x rw [hqu, hqv] by_cases hxv : x = v · subst x rw [hqv, hqu] rw [hqo x hxu hxv, hqo _ (by grind) (by grind), hinvol x] · intro x by_cases hxu : x = u · subst x exact Or.inr (hqu ▸ huv) by_cases hxv : x = v · subst x exact Or.inr (hqv ▸ huv.symm) rw [hqo x hxu hxv] exact hgood x apply no_improvement hmax hq {u, v} · intro x hx simp at hx grind · have hwu := hw u have hwv := hw v simpa [weightTerm, Finset.sum_insert, hne, hne.symm, hu, hv, hqu, hqv] using (show 0 < w u + w v by omega) refine ⟨p, hp, @htwo, ?_, ?_⟩ · intro u x hu hux by_contra! hlt let y := p x have hxp : p x = y := rfl have hyp : p y = x := hinvol x have hxy : x ≠ y := Ne.symm (htwo hu hux) have hux' : u ≠ x := hux.ne have huy : u ≠ y := by grind let q : V → V := fun z => if z = u then x else if z = x then u else if z = y then y else p z have hqu : q u = x := by simp [q] have hqx : q x = u := by simp [q, hux'.symm] have hqy : q y = y := by simp [q, huy.symm, hxy.symm] have hqo : ∀ z, z ≠ u → z ≠ x → z ≠ y → q z = p z := by intro z hzu hzx hzy simp [q, hzu, hzx, hzy] have hq : Admissible G q := by refine ⟨?_, ?_⟩ · intro z by_cases hzu : z = u · subst z rw [hqu, hqx] by_cases hzx : z = x · subst z rw [hqx, hqu] by_cases hzy : z = y · subst z rw [hqy, hqy] rw [hqo z hzu hzx hzy, hqo _ (by grind) (by grind) (by grind), hinvol z] · intro z by_cases hzu : z = u · subst z exact Or.inr (hqu ▸ hux) by_cases hzx : z = x · subst z exact Or.inr (hqx ▸ hux.symm) by_cases hzy : z = y · subst z exact Or.inl hqy rw [hqo z hzu hzx hzy] exact hgood z apply no_improvement hmax hq {u, x, y} · intro z hz simp at hz grind · have hlt' : w y < w u := hlt simp [weightTerm, Finset.sum_insert, hux', hux'.symm, hxy, hxy.symm, huy, hu, hxp, hyp, hqu, hqx, hqy] omega · intro u x y v hu hv huv hux hyv hxy hne have hyx : p y = x := by rw [← hxy, hinvol] have hux' : u ≠ x := hux.ne have hyv' : y ≠ v := hyv.ne have huy : u ≠ y := by grind have hxv : x ≠ v := by grind let q : V → V := fun z => if z = u then x else if z = x then u else if z = y then v else if z = v then y else p z have hqu : q u = x := by simp [q] have hqx : q x = u := by simp [q, hux'.symm] have hqy : q y = v := by simp [q, huy.symm, hne.symm] have hqv : q v = y := by simp [q, huv.symm, hxv.symm, hyv'.symm] have hqo : ∀ z, z ≠ u → z ≠ x → z ≠ y → z ≠ v → q z = p z := by intro z hzu hzx hzy hzv simp [q, hzu, hzx, hzy, hzv] have hq : Admissible G q := by refine ⟨?_, ?_⟩ · intro z by_cases hzu : z = u · subst z rw [hqu, hqx] by_cases hzx : z = x · subst z rw [hqx, hqu] by_cases hzy : z = y · subst z rw [hqy, hqv] by_cases hzv : z = v · subst z rw [hqv, hqy] rw [hqo z hzu hzx hzy hzv, hqo _ (by grind) (by grind) (by grind) (by grind), hinvol z] · intro z by_cases hzu : z = u · subst z exact Or.inr (hqu ▸ hux) by_cases hzx : z = x · subst z exact Or.inr (hqx ▸ hux.symm) by_cases hzy : z = y · subst z exact Or.inr (hqy ▸ hyv) by_cases hzv : z = v · subst z exact Or.inr (hqv ▸ hyv.symm) rw [hqo z hzu hzx hzy hzv] exact hgood z apply no_improvement hmax hq {u, x, y, v} · intro z hz simp at hz grind · have hwu := hw u have hwv := hw v simp [weightTerm, Finset.sum_insert, hux', hux'.symm, hne, hne.symm, huy, hxv, hyv', hyv'.symm, huv, hu, hv, hxy, hyx, hqu, hqx, hqy, hqv] omega theorem DegreeBound.card_cover_choices {V : Type*} [DecidableEq V] {G : SimpleGraph V} {U : Finset V} {k : ℕ} (hn : ∀ u ∈ U, ∃ w, G.Adj u w) (hsize : U.card ≤ k) (h : U.card < k ∨ ∃ u ∈ U, ∃ v ∈ U, u ≠ v ∧ ∃ w, G.Adj u w ∧ G.Adj v w) : ∃ D : Finset V, D.card < k ∧ ∀ u ∈ U, ∃ w ∈ D, G.Adj u w := by classical let f (u : V) := if hu : u ∈ U then Classical.choose (hn u hu) else u have hf (u : V) (hu : u ∈ U) : G.Adj u (f u) := by simp only [f, dif_pos hu] exact Classical.choose_spec (hn u hu) rcases h with hlt | ⟨u, hu, v, hv, huv, w, huw, hvw⟩ · exact ⟨U.image f, Finset.card_image_le.trans_lt hlt, fun x hx => ⟨f x, Finset.mem_image_of_mem _ hx, hf x hx⟩⟩ · let E := (U.erase u).erase v have hve : v ∈ U.erase u := Finset.mem_erase.mpr ⟨huv.symm, hv⟩ have hcard : E.card + 2 = U.card := by have h₁ := Finset.card_erase_add_one hu have h₂ := Finset.card_erase_add_one hve dsimp [E] omega refine ⟨insert w (E.image f), ?_, ?_⟩ · have h₁ := Finset.card_insert_le (s := E.image f) (a := w) have h₂ : (E.image f).card ≤ E.card := Finset.card_image_le omega · intro x hx by_cases hxu : x = u · subst x exact ⟨w, Finset.mem_insert_self _ _, huw⟩ by_cases hxv : x = v · subst x exact ⟨w, Finset.mem_insert_self _ _, hvw⟩ have hxE : x ∈ E := Finset.mem_erase.mpr ⟨hxv, Finset.mem_erase.mpr ⟨hxu, hx⟩⟩ exact ⟨f x, Finset.mem_insert_of_mem (Finset.mem_image_of_mem _ hxE), hf x hx⟩ theorem DegreeBound.three_escape {V : Type*} [DecidableEq V] {s : Finset V} {p : V → V} (hs : s.card = 3) (hp : Function.Involutive p) (hfree : ∀ x ∈ s, p x ≠ x) : ∃ x ∈ s, p x ∉ s := by classical by_contra! hclosed obtain ⟨x, y, z, hxy, hxz, hyz, rfl⟩ := Finset.card_eq_three.mp hs simp only [Finset.mem_insert, Finset.mem_singleton] at hclosed hfree have hx := hclosed x (by simp) have hy := hclosed y (by simp) have hz := hclosed z (by simp) have hpx := hfree x (by simp) have hpy := hfree y (by simp) have hpz := hfree z (by simp) have hxx := hp x have hyy := hp y have hzz := hp z grind only theorem DegreeBound.cover {V : Type*} [Fintype V] [DecidableEq V] (a k : ℕ) (ha : 3 ≤ a) (hk₂ : 2 ≤ k) (hk₃ : k ≤ 3) (G : SimpleGraph V) [DecidableRel G.Adj] (hm : G.edgeFinset.card < k * (a + 1) + 6) (hd : ∀ v, G.degree v ≤ a) : ∃ (p : V → V) (D : Finset V), Admissible G p ∧ D.card < k ∧ ∀ u, p u = u → G.degree u = a → ∃ w ∈ D, G.Adj u w := by classical obtain ⟨p, hp, htwo, hswap, hfour⟩ := DegreeBound.matching_facts G (fun v => G.degree v + 1) (by simp) have hpinv (x : V) : p (p x) = x := hp.1 x let U := Finset.univ.filter (fun u => p u = u ∧ G.degree u = a) let W := U.biUnion (fun v => G.neighborFinset v) let P := W.image p let c (w : V) := (U.filter (G.Adj w)).card have mU (u : V) : u ∈ U ↔ p u = u ∧ G.degree u = a := by simp [U] have mW (w : V) : w ∈ W ↔ ∃ u ∈ U, G.Adj u w := by simp [W] have mP (w : V) : w ∈ P ↔ p w ∈ W := by simp only [P, Finset.mem_image] exact ⟨fun ⟨y, hy, h⟩ => by rwa [← h, hpinv], fun hw => ⟨p w, hw, hpinv w⟩⟩ have hcS (w : V) : U.filter (G.Adj w) ⊆ G.neighborFinset w := by intro v hv exact (G.mem_neighborFinset _ _).mpr (Finset.mem_filter.mp hv).2 have wmatch (w : V) (hw : w ∈ W) : p w ≠ w := by obtain ⟨u, hu, huw⟩ := (mW w).mp hw exact htwo ((mU u).mp hu).1 huw have hmatedge (w : V) (hw : w ∈ W) : G.Adj w (p w) := (hp.2 w).resolve_left (wmatch w hw) have hmfixed (w : V) (hw : w ∈ W) : p (p w) ≠ p w := by rw [hpinv] exact (wmatch w hw).symm have hUw : Disjoint U W := Finset.disjoint_left.mpr fun w hU hW => wmatch w hW ((mU w).mp hU).1 have hUp : Disjoint U P := by refine Finset.disjoint_left.mpr fun w hU hP => ?_ have hfix := ((mU w).mp hU).1 have hW := (mP w).mp hP exact wmatch w (hfix ▸ hW) hfix have hcardP : P.card = W.card := Finset.card_image_of_injective _ hp.1.injective have hdegreeP (w : V) (hw : w ∈ P) : G.degree w = a := by obtain ⟨v, hv, hvp⟩ := Finset.mem_image.mp hw obtain ⟨u, hu, huv⟩ := (mW v).mp hv have hs := hswap ((mU u).mp hu).1 huv have hdu := ((mU u).mp hu).2 rw [hvp] at hs have hdw := hd w omega have sum_high (E : Finset V) (hE : Disjoint E (U ∪ P)) : a * (U.card + W.card) + ∑ x ∈ E, G.degree x ≤ 2 * G.edgeFinset.card := by rw [← G.sum_degrees_eq_twice_card_edges] have ht := Finset.sum_le_sum_of_subset (f := fun w => G.degree w) (Finset.subset_univ ((U ∪ P) ∪ E)) rw [Finset.sum_union hE.symm, Finset.sum_union hUp] at ht have hsumU : (∑ x ∈ U, G.degree x) = U.card * a := by calc _ = ∑ _x ∈ U, a := Finset.sum_congr rfl fun x hx => ((mU x).mp hx).2 _ = _ := by simp have hsumP : (∑ x ∈ P, G.degree x) = P.card * a := by calc _ = ∑ _x ∈ P, a := Finset.sum_congr rfl hdegreeP _ = _ := by simp rw [hsumU, hsumP, hcardP] at ht rwa [Nat.mul_add, Nat.mul_comm a U.card, Nat.mul_comm a W.card] have hdegree2 (w : V) (hw : w ∈ W) : c w + 1 ≤ G.degree w := by unfold c have hsub : insert (p w) (U.filter (G.Adj w)) ⊆ G.neighborFinset w := Finset.insert_subset_iff.mpr ⟨(G.mem_neighborFinset _ _).mpr (hmatedge w hw), hcS w⟩ have hnot : p w ∉ U.filter (G.Adj w) := by intro hh exact hmfixed w hw ((mU (p w)).mp (Finset.mem_filter.mp hh).1).1 have he := Finset.card_le_card hsub rwa [Finset.card_insert_of_notMem hnot] at he have hfan (w : V) (hw : w ∈ W) : 1 ≤ c w := by obtain ⟨u, hu, huw⟩ := (mW w).mp hw exact Finset.card_pos.mpr ⟨u, Finset.mem_filter.mpr ⟨hu, huw.symm⟩⟩ have hfan_notin (w : V) (hw : w ∈ W) (hc : 2 ≤ c w) : w ∉ P := by intro hwp obtain ⟨v, hv, hvp⟩ := (mW (p w)).mp ((mP w).mp hwp) obtain ⟨u, hu, hne⟩ := Finset.exists_mem_ne (show 1 < c w by omega) v obtain ⟨hU, hwu⟩ := Finset.mem_filter.mp hu exact hfour ((mU u).mp hU).1 ((mU v).mp hv).1 hne hwu.symm hvp.symm rfl (wmatch w hw).symm have hinc : (∑ w ∈ W, c w) = a * U.card := by calc (∑ w ∈ W, c w) = ∑ w ∈ W, ∑ u ∈ U, if G.Adj w u then 1 else 0 := by simp [c, Finset.sum_boole] _ = ∑ u ∈ U, ∑ w ∈ W, if G.Adj w u then 1 else 0 := Finset.sum_comm _ = ∑ u ∈ U, a := by refine Finset.sum_congr rfl fun u hu => ?_ rw [Finset.sum_boole] have he : W.filter (fun w => G.Adj w u) = G.neighborFinset u := by ext w simp only [Finset.mem_filter, G.mem_neighborFinset] exact ⟨fun h => h.2.symm, fun h => ⟨(mW w).mpr ⟨u, hu, h⟩, h.symm⟩⟩ rw [he] exact ((mU u).mp hu).2 _ = _ := by simp [Nat.mul_comm] have hi₁ : a * U.card ≤ (a - 1) * W.card := by rw [← hinc] calc _ ≤ ∑ _w ∈ W, (a - 1) := Finset.sum_le_sum fun w hw => by have h₁ := hdegree2 w hw have h₂ := hd w omega _ = _ := by simp [Nat.mul_comm] -- Repeated neighbors contribute degrees outside the two sets already counted. have hi₂ : 3 * (a * U.card) ≤ 2 * G.edgeFinset.card := by let E := W.filter (fun w => 2 ≤ c w) have hdis : Disjoint E (U ∪ P) := by refine Finset.disjoint_left.mpr fun w hw hUP => ?_ have ⟨hwW, hcw⟩ := Finset.mem_filter.mp hw rcases Finset.mem_union.mp hUP with h | h · exact Finset.disjoint_left.mp hUw h hwW · exact hfan_notin w hwW hcw h have hs : (∑ w ∈ W, c w) ≤ W.card + ∑ w ∈ E, G.degree w := by calc (∑ w ∈ W, c w) ≤ ∑ w ∈ W, (1 + if 2 ≤ c w then G.degree w else 0) := by refine Finset.sum_le_sum fun w hw => ?_ split_ifs with h · have := hdegree2 w hw omega · omega _ = _ := by rw [Finset.sum_add_distrib, ← Finset.sum_filter] simp [E] have ht := sum_high E hdis rw [hinc] at hs rw [Nat.mul_add] at ht have hh : (a - 1) * W.card + W.card = a * W.card := by have ha₁ : a - 1 + 1 = a := by omega conv_rhs => rw [← ha₁, Nat.add_mul, Nat.one_mul] omega have hU₀ : U.card ≤ k := by rcases (show k = 2 ∨ k = 3 by omega) with rfl | rfl · by_contra! hU₀ have h₁ := Nat.mul_le_mul_left a (show 3 ≤ U.card by omega) simp only [Nat.mul_add] at hm omega · by_contra! hU₀ have h₁ := Nat.mul_le_mul_left a (show 4 ≤ U.card by omega) simp only [Nat.mul_add] at hm omega have hn : ∀ u ∈ U, ∃ w, G.Adj u w := by intro u hu apply (G.degree_pos_iff_exists_adj u).mp rw [((mU u).mp hu).2] omega have hsmall : U.card < k ∨ ∃ u ∈ U, ∃ v ∈ U, u ≠ v ∧ ∃ w, G.Adj u w ∧ G.Adj v w := by by_contra! hfalse have ht : U.card = k := by omega have hsep (u v w : V) (hu : u ∈ U) (hv : v ∈ U) (huw : G.Adj u w) (hvw : G.Adj v w) : u = v := by by_contra hh exact hfalse.2 u hu v hv hh w huw hvw have hq : W.card = a * k := by calc W.card = ∑ w ∈ W, c w := by have hh (w : V) (hw : w ∈ W) : c w = 1 := by apply le_antisymm ?_ (hfan w hw) apply Finset.card_le_one.mpr intro u hu v hv obtain ⟨hUu, hwu⟩ := Finset.mem_filter.mp hu obtain ⟨hUv, hwv⟩ := Finset.mem_filter.mp hv exact hsep u v w hUu hUv hwu.symm hwv.symm rw [Finset.sum_congr rfl hh] simp _ = _ := by rw [hinc, ht] have hlower := sum_high ∅ (by simp) simp only [Finset.sum_empty, add_zero] at hlower rw [ht, hq, Nat.mul_add, ← Nat.mul_assoc] at hlower have hsmall : a = 3 ∧ k = 2 := by rcases (show k = 2 ∨ k = 3 by omega) with rfl | rfl · by_contra! h₁ have ha₄ : 4 ≤ a := by omega have hsq : a * 4 ≤ a * a := Nat.mul_le_mul_left _ ha₄ simp only [Nat.mul_comm _ 2, Nat.mul_add] at hm hlower hsq omega · have hsq : a * 3 ≤ a * a := Nat.mul_le_mul_left _ ha simp only [Nat.mul_comm _ 3, Nat.mul_add] at hm hlower hsq omega obtain ⟨ha₃, hk₂'⟩ := hsmall have hcardU : U.card = 2 := ht.trans hk₂' obtain ⟨u, v, huv, hUV⟩ := Finset.card_eq_two.mp hcardU have hu : u ∈ U := by simp [hUV] have hv : v ∈ U := by simp [hUV] -- In the remaining cubic case, each of the two triples has a mate outside it. have escape (s : V) (hs : s ∈ U) : ∃ w ∈ W, w ∉ P ∧ G.Adj s w := by have hdeg : (G.neighborFinset s).card = 3 := (((mU s).mp hs).2).trans ha₃ obtain ⟨w, hw, hpw⟩ := DegreeBound.three_escape hdeg hp.1 (fun w hw => htwo ((mU s).mp hs).1 ((G.mem_neighborFinset _ _).mp hw)) have hsw := (G.mem_neighborFinset _ _).mp hw refine ⟨w, (mW w).mpr ⟨s, hs, hsw⟩, ?_, hsw⟩ intro hP obtain ⟨z, hz, hpwz⟩ := (mW (p w)).mp ((mP w).mp hP) have hne : s ≠ z := by intro hsz subst z exact hpw ((G.mem_neighborFinset _ _).mpr hpwz) exact hfour ((mU s).mp hs).1 ((mU z).mp hz).1 hne hsw hpwz.symm rfl (htwo ((mU s).mp hs).1 hsw).symm obtain ⟨x, hxW, hxP, hux⟩ := escape u hu obtain ⟨y, hyW, hyP, hvy⟩ := escape v hv have hxy : x ≠ y := by intro h subst y exact huv (hsep u v x hu hv hux hvy) have hdis : Disjoint ({x, y} : Finset V) (U ∪ P) := by refine Finset.disjoint_left.mpr fun z hz hz' => ?_ simp only [Finset.mem_insert, Finset.mem_singleton] at hz rcases hz with rfl | rfl · exact (Finset.mem_union.mp hz').elim (fun h => Finset.disjoint_left.mp hUw h hxW) hxP · exact (Finset.mem_union.mp hz').elim (fun h => Finset.disjoint_left.mp hUw h hyW) hyP have hdegx := hdegree2 x hxW have hdegy := hdegree2 y hyW have hfanx := hfan x hxW have hfany := hfan y hyW have hz := sum_high {x, y} hdis simp [Finset.sum_insert, hxy, hcardU, hq, ha₃, hk₂'] at hz rw [ha₃, hk₂'] at hm omega obtain ⟨D, hD, hcover⟩ := DegreeBound.card_cover_choices hn hU₀ hsmall exact ⟨p, D, hp, hD, fun u hpu hdu => hcover u ((mU u).mpr ⟨hpu, hdu⟩)⟩ theorem DegreeBound.center_adj {s : ℕ} {b : Fin s → ℕ} (i : Fin s) (j : Fin (b i + 1)) (hj : j ≠ 0) : F(s, b).Adj ⟨i, 0⟩ ⟨i, j⟩ := by refine ⟨?_, Or.inl ⟨rfl, Or.inl rfl⟩⟩ intro he exact hj (Fin.ext (congrArg (fun p : Σ i : Fin s, Fin (b i + 1) => p.2.val) he).symm) theorem avoid_mixed_pure_at_most_a (a k : ℕ) (ha : 3 ≤ a) (hk₂ : 2 ≤ k) (hk₃ : k ≤ 3) {n : ℕ} (G : SimpleGraph (Fin n)) (hm : G.edgeSet.ncard < k * (a + 1) + 6) (hd : ∀ v, (G.neighborSet v).ncard ≤ a) : ∃ R : SimpleGraph (Fin n), R ≤ G ∧ ¬ F(3, ![a, 2, 2]).IsContained R ∧ ¬ F(k, fun _ => 2).IsContained (G \ R) := by classical rw [← G.coe_edgeFinset, Set.ncard_coe_finset] at hm have hd' (v : Fin n) : G.degree v ≤ a := by have h := hd v rw [← G.coe_neighborFinset v, Set.ncard_coe_finset] at h exact h obtain ⟨p, D, hp, hD, hcover⟩ := DegreeBound.cover a k ha hk₂ hk₃ G hm hd' let R : SimpleGraph (Fin n) := ⟨fun u v => G.Adj u v ∧ p u ≠ v ∧ u ∉ D ∧ v ∉ D, ⟨by intro u v h refine ⟨h.1.symm, ?_, h.2.2.2, h.2.2.1⟩ intro he exact h.2.1 (by rw [← he, hp.1])⟩, ⟨fun _ h => G.irrefl h.1⟩⟩ have hRG : R ≤ G := fun _ _ h => h.1 have hblue (u v : Fin n) (h : (G \ R).Adj u v) (hu : u ∉ D) (hv : v ∉ D) : p u = v := by by_contra! hh exact h.2 ⟨h.1, hh, hu, hv⟩ have hred (v : Fin n) : R.degree v < a := by have hdv := hd' v have hdegree := degree_le_of_le (v := v) hRG by_cases hlt : G.degree v < a · omega have heq : G.degree v = a := by omega have hw : ∃ w, G.Adj v w ∧ ¬ R.Adj v w := by by_cases he : p v = v · obtain ⟨w, hwD, hvw⟩ := hcover v he heq exact ⟨w, hvw, fun hr => hr.2.2.2 hwD⟩ · exact ⟨p v, (hp.2 v).resolve_left he, fun hr => hr.2.1 rfl⟩ obtain ⟨w, hwG, hwR⟩ := hw have hsub : R.neighborFinset v ⊆ G.neighborFinset v := by intro w hw exact (G.mem_neighborFinset _ _).mpr (hRG ((R.mem_neighborFinset _ _).mp hw)) have hproper : R.neighborFinset v ⊂ G.neighborFinset v := by refine Finset.ssubset_iff_subset_ne.mpr ⟨hsub, ?_⟩ intro h have hmem := (G.mem_neighborFinset _ _).mpr hwG rw [← h] at hmem exact hwR ((R.mem_neighborFinset _ _).mp hmem) have hc := Finset.card_lt_card hproper change R.degree v < G.degree v at hc omega refine ⟨R, hRG, ?_, ?_⟩ · rintro ⟨f⟩ let c := f ⟨0, 0⟩ let l (j : Fin a) : Σ i : Fin 3, Fin ((![a, 2, 2] : Fin 3 → ℕ) i + 1) := ⟨0, ⟨j.val + 1, by simp⟩⟩ have hL : Function.Injective (fun j : Fin a => f (l j)) := by intro i j h have hh := congrArg (fun p : Σ i : Fin 3, Fin ((![a, 2, 2] : Fin 3 → ℕ) i + 1) => p.2.val) (f.injective h) change i.val + 1 = j.val + 1 at hh exact Fin.ext (Nat.add_right_cancel hh) have hsub : Finset.univ.image (fun j : Fin a => f (l j)) ⊆ R.neighborFinset c := by intro w hw obtain ⟨j, -, rfl⟩ := Finset.mem_image.mp hw refine (R.mem_neighborFinset _ _).mpr (f.toHom.map_adj ?_) apply DegreeBound.center_adj intro he have hval := congrArg Fin.val he change j.val + 1 = 0 at hval omega have hc := Finset.card_le_card hsub rw [Finset.card_image_of_injective _ hL, Finset.card_univ, Fintype.card_fin] at hc exact (Nat.not_lt_of_ge hc) (hred c) · rintro ⟨f⟩ have hcomponent (i : Fin k) : ∃ j : Fin 3, f ⟨i, j⟩ ∈ D := by by_contra! hnone have h₁ : (G \ R).Adj (f ⟨i, 0⟩) (f ⟨i, 1⟩) := f.toHom.map_adj (DegreeBound.center_adj i 1 (by decide)) have h₂ : (G \ R).Adj (f ⟨i, 0⟩) (f ⟨i, 2⟩) := f.toHom.map_adj (DegreeBound.center_adj i 2 (by decide)) have he := (hblue _ _ h₁ (hnone 0) (hnone 1)).symm.trans (hblue _ _ h₂ (hnone 0) (hnone 2)) have hf := f.injective he have hv := congrArg (fun p : Σ _ : Fin k, Fin 3 => p.2.val) hf change (1 : ℕ) = 2 at hv omega choose j hj using hcomponent have hinj : Function.Injective (fun i : Fin k => f ⟨i, j i⟩) := by intro u v h exact congrArg Sigma.fst (f.injective h) have hle := Finset.card_le_card (show Finset.univ.image (fun i : Fin k => f ⟨i, j i⟩) ⊆ D by intro x hx obtain ⟨i, -, rfl⟩ := Finset.mem_image.mp hx exact hj i) rw [Finset.card_image_of_injective _ hinj, Finset.card_univ, Fintype.card_fin] at hle omega /-- Every smaller host admits an avoiding red-blue coloring. -/ theorem three_four_avoid (a : ℕ) (ha : 3 ≤ a) {n : ℕ} (G : SimpleGraph (Fin n)) (hm : G.edgeSet.ncard < 5 * a + 8) : ∃ R : SimpleGraph (Fin n), R ≤ G ∧ ¬ F(3, ![a, 2, 2]).IsContained R ∧ ¬ F(4, ![a, 2, 2, 2]).IsContained (G \ R) := by exact three_four_avoid_of_cases a ha (fun k hk₂ hk₃ => avoid_mixed_pure_at_most_a a k ha hk₂ hk₃) (three_four_avoid_bounded a ha) G hm theorem sizeRamsey_three_four (a : ℕ) (ha : 3 ≤ a) : sizeRamsey F(3, ![a, 2, 2]) F(4, ![a, 2, 2, 2]) = 5 * a + 8 := by exact sizeRamsey_three_four_of_bounds a (upper_three_four a ha) (fun _ G hm => three_four_avoid a ha G hm) end ResearchErdos561