|
| 1 | +/- |
| 2 | +Copyright (c) 2020 Bhavik Mehta. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Bhavik Mehta |
| 5 | +
|
| 6 | +! This file was ported from Lean 3 source module wiedijk_100_theorems.ascending_descending_sequences |
| 7 | +! leanprover-community/mathlib commit 5563b1b49e86e135e8c7b556da5ad2f5ff881cad |
| 8 | +! Please do not edit these lines, except to modify the commit id |
| 9 | +! if you have ported upstream changes. |
| 10 | +-/ |
| 11 | +import Mathlib.Data.Fintype.Powerset |
| 12 | + |
| 13 | +/-! |
| 14 | +# Erdős–Szekeres theorem |
| 15 | +
|
| 16 | +This file proves Theorem 73 from the [100 Theorems List](https://www.cs.ru.nl/~freek/100/), also |
| 17 | +known as the Erdős–Szekeres theorem: given a sequence of more than `r * s` distinct |
| 18 | +values, there is an increasing sequence of length longer than `r` or a decreasing sequence of length |
| 19 | +longer than `s`. |
| 20 | +
|
| 21 | +We use the proof outlined at |
| 22 | +https://en.wikipedia.org/wiki/Erdos-Szekeres_theorem#Pigeonhole_principle. |
| 23 | +
|
| 24 | +## Tags |
| 25 | +
|
| 26 | +sequences, increasing, decreasing, Ramsey, Erdos-Szekeres, Erdős–Szekeres, Erdős-Szekeres |
| 27 | +-/ |
| 28 | + |
| 29 | + |
| 30 | +variable {α : Type _} [LinearOrder α] {β : Type _} |
| 31 | + |
| 32 | +open Function Finset |
| 33 | + |
| 34 | +open scoped Classical |
| 35 | + |
| 36 | +namespace Theorems100 |
| 37 | + |
| 38 | +/-- **Erdős–Szekeres Theorem**: Given a sequence of more than `r * s` distinct values, there is an |
| 39 | +increasing sequence of length longer than `r` or a decreasing sequence of length longer than `s`. |
| 40 | +
|
| 41 | +Proof idea: |
| 42 | +We label each value in the sequence with two numbers specifying the longest increasing |
| 43 | +subsequence ending there, and the longest decreasing subsequence ending there. |
| 44 | +We then show the pair of labels must be unique. Now if there is no increasing sequence longer than |
| 45 | +`r` and no decreasing sequence longer than `s`, then there are at most `r * s` possible labels, |
| 46 | +which is a contradiction if there are more than `r * s` elements. |
| 47 | +-/ |
| 48 | +theorem erdos_szekeres {r s n : ℕ} {f : Fin n → α} (hn : r * s < n) (hf : Injective f) : |
| 49 | + (∃ t : Finset (Fin n), r < t.card ∧ StrictMonoOn f ↑t) ∨ |
| 50 | + ∃ t : Finset (Fin n), s < t.card ∧ StrictAntiOn f ↑t := by |
| 51 | + -- Given an index `i`, produce the set of increasing (resp., decreasing) subsequences which ends |
| 52 | + -- at `i`. |
| 53 | + let inc_sequences_ending_in : Fin n → Finset (Finset (Fin n)) := fun i => |
| 54 | + univ.powerset.filter fun t => Finset.max t = i ∧ StrictMonoOn f ↑t |
| 55 | + let dec_sequences_ending_in : Fin n → Finset (Finset (Fin n)) := fun i => |
| 56 | + univ.powerset.filter fun t => Finset.max t = i ∧ StrictAntiOn f ↑t |
| 57 | + -- The singleton sequence is in both of the above collections. |
| 58 | + -- (This is useful to show that the maximum length subsequence is at least 1, and that the set |
| 59 | + -- of subsequences is nonempty.) |
| 60 | + have inc_i : ∀ i, {i} ∈ inc_sequences_ending_in i := fun i => by simp [StrictMonoOn] |
| 61 | + have dec_i : ∀ i, {i} ∈ dec_sequences_ending_in i := fun i => by simp [StrictAntiOn] |
| 62 | + -- Define the pair of labels: at index `i`, the pair is the maximum length of an increasing |
| 63 | + -- subsequence ending at `i`, paired with the maximum length of a decreasing subsequence ending |
| 64 | + -- at `i`. |
| 65 | + -- We call these labels `(a_i, b_i)`. |
| 66 | + let ab' : Fin n → ℕ × ℕ := by |
| 67 | + intro i |
| 68 | + apply |
| 69 | + (max' ((inc_sequences_ending_in i).image card) (Nonempty.image ⟨{i}, inc_i i⟩ _), |
| 70 | + max' ((dec_sequences_ending_in i).image card) (Nonempty.image ⟨{i}, dec_i i⟩ _)) |
| 71 | + -- Porting note: it costs many resources to unfold `ab'` so we obscure the definition: |
| 72 | + generalize hab : ab' = ab |
| 73 | + -- It now suffices to show that one of the labels is 'big' somewhere. In particular, if the |
| 74 | + -- first in the pair is more than `r` somewhere, then we have an increasing subsequence in our |
| 75 | + -- set, and if the second is more than `s` somewhere, then we have a decreasing subsequence. |
| 76 | + rsuffices ⟨i, hi⟩ : ∃ i, r < (ab i).1 ∨ s < (ab i).2 |
| 77 | + · refine Or.imp ?_ ?_ hi |
| 78 | + on_goal 1 => have : (ab i).1 ∈ _ := by simp only [← hab]; exact max'_mem _ _ |
| 79 | + on_goal 2 => have : (ab i).2 ∈ _ := by simp only [← hab]; exact max'_mem _ _ |
| 80 | + all_goals |
| 81 | + intro hi |
| 82 | + rw [mem_image] at this |
| 83 | + obtain ⟨t, ht₁, ht₂⟩ := this |
| 84 | + refine' ⟨t, by rwa [ht₂], _⟩ |
| 85 | + rw [mem_filter] at ht₁ |
| 86 | + apply ht₁.2.2 |
| 87 | + -- Show first that the pair of labels is unique. |
| 88 | + have : Injective ab := by |
| 89 | + simp only [← hab] |
| 90 | + apply injective_of_lt_imp_ne |
| 91 | + intro i j k q |
| 92 | + injection q with q₁ q₂ |
| 93 | + -- We have two cases: `f i < f j` or `f j < f i`. |
| 94 | + -- In the former we'll show `a_i < a_j`, and in the latter we'll show `b_i < b_j`. |
| 95 | + cases lt_or_gt_of_ne fun _ => ne_of_lt ‹i < j› (hf ‹f i = f j›) |
| 96 | + on_goal 1 => apply ne_of_lt _ q₁; have : (ab' i).1 ∈ _ := by dsimp only; exact max'_mem _ _ |
| 97 | + on_goal 2 => apply ne_of_lt _ q₂; have : (ab' i).2 ∈ _ := by dsimp only; exact max'_mem _ _ |
| 98 | + all_goals |
| 99 | + -- Reduce to showing there is a subsequence of length `a_i + 1` which ends at `j`. |
| 100 | + rw [Nat.lt_iff_add_one_le] |
| 101 | + apply le_max' |
| 102 | + rw [mem_image] at this ⊢ |
| 103 | + -- In particular we take the subsequence `t` of length `a_i` which ends at `i`, by definition |
| 104 | + -- of `a_i` |
| 105 | + rcases this with ⟨t, ht₁, ht₂⟩ |
| 106 | + rw [mem_filter] at ht₁ |
| 107 | + -- Ensure `t` ends at `i`. |
| 108 | + have : t.max = i |
| 109 | + simp [ht₁.2.1] |
| 110 | + -- Now our new subsequence is given by adding `j` at the end of `t`. |
| 111 | + refine' ⟨insert j t, _, _⟩ |
| 112 | + -- First make sure it's valid, i.e., that this subsequence ends at `j` and is increasing |
| 113 | + · rw [mem_filter] |
| 114 | + refine' ⟨_, _, _⟩ |
| 115 | + · rw [mem_powerset]; apply subset_univ |
| 116 | + -- It ends at `j` since `i < j`. |
| 117 | + · convert max_insert (a := j) (s := t) |
| 118 | + rw [ht₁.2.1, max_eq_left] |
| 119 | + apply WithBot.coe_le_coe.mpr (le_of_lt ‹i < j›) |
| 120 | + -- To show it's increasing (i.e., `f` is monotone increasing on `t.insert j`), we do cases |
| 121 | + -- on what the possibilities could be - either in `t` or equals `j`. |
| 122 | + simp only [StrictMonoOn, StrictAntiOn, coe_insert, Set.mem_insert_iff, mem_coe] |
| 123 | + -- Most of the cases are just bashes. |
| 124 | + rintro x ⟨rfl | _⟩ y ⟨rfl | _⟩ _ |
| 125 | + · apply (irrefl _ ‹j < j›).elim |
| 126 | + · exfalso |
| 127 | + apply not_le_of_lt (_root_.trans ‹i < j› ‹j < y›) (le_max_of_eq ‹y ∈ t› ‹t.max = i›) |
| 128 | + · first |
| 129 | + | apply lt_of_le_of_lt _ ‹f i < f j› |
| 130 | + | apply lt_of_lt_of_le ‹f j < f i› _ |
| 131 | + rcases lt_or_eq_of_le (le_max_of_eq ‹x ∈ t› ‹t.max = i›) with (_ | rfl) |
| 132 | + · apply le_of_lt (ht₁.2.2 ‹x ∈ t› (mem_of_max ‹t.max = i›) ‹x < i›) |
| 133 | + · rfl |
| 134 | + · apply ht₁.2.2 ‹x ∈ t› ‹y ∈ t› ‹x < y› |
| 135 | + -- Finally show that this new subsequence is one longer than the old one. |
| 136 | + · rw [card_insert_of_not_mem, ht₂] |
| 137 | + intro |
| 138 | + apply not_le_of_lt ‹i < j› (le_max_of_eq ‹j ∈ t› ‹t.max = i›) |
| 139 | + -- Finished both goals! |
| 140 | + -- Now that we have uniqueness of each label, it remains to do some counting to finish off. |
| 141 | + -- Suppose all the labels are small. |
| 142 | + by_contra q |
| 143 | + push_neg at q |
| 144 | + -- Then the labels `(a_i, b_i)` all fit in the following set: `{ (x,y) | 1 ≤ x ≤ r, 1 ≤ y ≤ s }` |
| 145 | + let ran : Finset (ℕ × ℕ) := (range r).image Nat.succ ×ˢ (range s).image Nat.succ |
| 146 | + -- which we prove here. |
| 147 | + have : image ab univ ⊆ ran := by |
| 148 | + -- First some logical shuffling |
| 149 | + rintro ⟨x₁, x₂⟩ |
| 150 | + simp only [mem_image, exists_prop, mem_range, mem_univ, mem_product, true_and_iff, |
| 151 | + Prod.ext_iff] |
| 152 | + rintro ⟨i, rfl, rfl⟩ |
| 153 | + specialize q i |
| 154 | + -- Show `1 ≤ a_i` and `1 ≤ b_i`, which is easy from the fact that `{i}` is a increasing and |
| 155 | + -- decreasing subsequence which we did right near the top. |
| 156 | + have z : 1 ≤ (ab i).1 ∧ 1 ≤ (ab i).2 := by |
| 157 | + simp only [← hab] |
| 158 | + constructor <;> |
| 159 | + · apply le_max' |
| 160 | + rw [mem_image] |
| 161 | + refine' ⟨{i}, by solve_by_elim, card_singleton i⟩ |
| 162 | + refine' ⟨_, _⟩ |
| 163 | + -- Need to get `a_i ≤ r`, here phrased as: there is some `a < r` with `a+1 = a_i`. |
| 164 | + · refine' ⟨(ab i).1 - 1, _, Nat.succ_pred_eq_of_pos z.1⟩ |
| 165 | + rw [tsub_lt_iff_right z.1] |
| 166 | + apply Nat.lt_succ_of_le q.1 |
| 167 | + · refine' ⟨(ab i).2 - 1, _, Nat.succ_pred_eq_of_pos z.2⟩ |
| 168 | + rw [tsub_lt_iff_right z.2] |
| 169 | + apply Nat.lt_succ_of_le q.2 |
| 170 | + -- To get our contradiction, it suffices to prove `n ≤ r * s` |
| 171 | + apply not_le_of_lt hn |
| 172 | + -- Which follows from considering the cardinalities of the subset above, since `ab` is injective. |
| 173 | + simpa [Nat.succ_injective, card_image_of_injective, ‹Injective ab›] using card_le_of_subset this |
| 174 | +#align theorems_100.erdos_szekeres Theorems100.erdos_szekeres |
| 175 | + |
| 176 | +end Theorems100 |
0 commit comments