Download data/lean/OAI/Combinatorics/MatchingCount/Graphs/CompleteGraph.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 14.1 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/MatchingCount/Graphs/CompleteGraph.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Combinatorics/MatchingCount/Graphs/CompleteGraph.lean
-
curl -L -o CompleteGraph.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/MatchingCount/Graphs/CompleteGraph.lean
14.1 kB
| import Mathlib | |
| import OAI.Combinatorics.MatchingCount.Model | |
| namespace OAI | |
| namespace MatchingFPRAS | |
| @[reducible] noncomputable def completeGraph (n : β) : GraphInput where | |
| n := n | |
| edges := by | |
| classical | |
| exact Finset.univ.filter (fun e => e.1 < e.2) | |
| increasing := by | |
| classical | |
| intro e he | |
| exact (Finset.mem_filter.mp he).2 | |
| noncomputable def rawWeight (G : GraphInput) (x : β) (M : Finset (Fin G.n Γ Fin G.n)) : β := | |
| β e β M, if e β G.edges then 1 else x | |
| noncomputable def rawPartition (G : GraphInput) (x : β) : β := | |
| β M β perfectMatchings (completeGraph G.n), rawWeight G x M | |
| lemma graph_edges_subset_complete (G : GraphInput) : G.edges β (completeGraph G.n).edges := by | |
| classical | |
| intro e he | |
| exact Finset.mem_filter.mpr β¨Finset.mem_univ _, G.increasing e heβ© | |
| lemma mem_perfectMatchings (G : GraphInput) (M) : M β perfectMatchings G β Perfect G M := by | |
| classical | |
| simp only [perfectMatchings, Finset.mem_filter, Finset.mem_powerset] | |
| exact β¨fun h => h.2, fun h => β¨h.1,hβ©β© | |
| lemma pm_subset_complete (G : GraphInput) : perfectMatchings G β perfectMatchings (completeGraph G.n) := by | |
| intro M hM | |
| apply (mem_perfectMatchings _ _).mpr | |
| have h := (mem_perfectMatchings _ _).mp hM | |
| exact β¨h.1.trans (graph_edges_subset_complete G), h.2β© | |
| lemma rawWeight_nonneg (G : GraphInput) (x : β) (hx : 0 β€ x) (M) : 0 β€ rawWeight G x M := by | |
| classical | |
| apply Finset.prod_nonneg | |
| intro e he | |
| split_ifs <;> positivity | |
| lemma rawWeight_one (G : GraphInput) (x : β) (M) (hM : M β G.edges) : rawWeight G x M = 1 := by | |
| classical | |
| apply Finset.prod_eq_one | |
| intro e he | |
| simp [hM he] | |
| lemma rawWeight_bad (G : GraphInput) (x : β) (hx : 0 β€ x) (hx1 : x β€ 1) (M) | |
| (hM : Β¬ M β G.edges) : rawWeight G x M β€ x := by | |
| classical | |
| obtain β¨e, he, hbadβ© := Finset.not_subset.mp hM | |
| have hp : (β f β M.erase e, if f β G.edges then (1:β) else x) β€ 1 := by | |
| apply Finset.prod_le_oneβ (fun f _ => by split_ifs <;> positivity) | |
| intro f hf | |
| split_ifs <;> simp_all | |
| change (β f β M, if f β G.edges then (1:β) else x) β€ x | |
| rw [β Finset.mul_prod_erase M _ he, ite_eq_right hbad] | |
| simpa using mul_le_mul_of_nonneg_left hp hx | |
| theorem raw_padding_bound (G : GraphInput) (x : β) (hx : 0 β€ x) (hx1 : x β€ 1) : | |
| (Z G : β) β€ rawPartition G x β§ | |
| rawPartition G x β€ (Z G : β) + (Z (completeGraph G.n) : β)*x := by | |
| classical | |
| let full := perfectMatchings (completeGraph G.n) | |
| let good := perfectMatchings G | |
| let indicator : Finset (Fin G.n Γ Fin G.n) β β := fun M => if M β good then 1 else 0 | |
| have hind : (β M β full, indicator M) = (Z G : β) := by | |
| have heq := Finset.sum_subset (f := indicator) (pm_subset_complete G) | |
| (fun M _ hnot => ite_eq_right hnot) | |
| change (β M β good, indicator M) = β M β full, indicator M at heq | |
| rw [β heq] | |
| simp [indicator,good,Z] | |
| have hlo (M : Finset (Fin G.n Γ Fin G.n)) (hMF : M β full) : indicator M β€ rawWeight G x M := by | |
| by_cases hM : M β good | |
| Β· have hsub := ((mem_perfectMatchings G M).mp hM).1 | |
| have hi : indicator M = 1 := ite_eq_left hM | |
| rw [hi, rawWeight_one G x M hsub] | |
| Β· have hi : indicator M = 0 := ite_eq_right hM | |
| rw [hi] | |
| exact rawWeight_nonneg G x hx M | |
| have hup (M : Finset (Fin G.n Γ Fin G.n)) (hMF : M β full) : rawWeight G x M β€ indicator M+x := by | |
| by_cases hM : M β good | |
| Β· have hsub := ((mem_perfectMatchings G M).mp hM).1 | |
| have hi : indicator M = 1 := ite_eq_left hM | |
| rw [hi, rawWeight_one G x M hsub] | |
| linarith | |
| Β· have hsub : Β¬ M β G.edges := by | |
| intro hh | |
| apply hM | |
| apply (mem_perfectMatchings G M).mpr | |
| exact β¨hh, ((mem_perfectMatchings (completeGraph G.n) M).mp hMF).2β© | |
| have hi : indicator M = 0 := ite_eq_right hM | |
| rw [hi, zero_add] | |
| exact rawWeight_bad G x hx hx1 M hsub | |
| constructor | |
| Β· rw [β hind] | |
| exact Finset.sum_le_sum hlo | |
| Β· calc | |
| rawPartition G x β€ β M β full, (indicator M+x) := Finset.sum_le_sum hup | |
| _ = (Z G : β) + (Z (completeGraph G.n) : β)*x := by | |
| rw [Finset.sum_add_distrib, hind] | |
| simp [Z,full] | |
| theorem perfect_vertex_factor (G : GraphInput) (M : Finset (Fin G.n Γ Fin G.n)) | |
| (hM : Perfect G M) (s : Fin G.n β β) : | |
| (β e β M, s e.1*s e.2) = β v, s v := by | |
| classical | |
| let ends : Fin G.n Γ Fin G.n β Finset (Fin G.n) := fun e => {e.1,e.2} | |
| have hends (e : Fin G.n Γ Fin G.n) (v) : v β ends e β e.1=v β¨ e.2=v := by | |
| simp [ends, eq_comm] | |
| have hpair : (M : Set (Fin G.n Γ Fin G.n)).PairwiseDisjoint ends := by | |
| intro e he f hf hne | |
| apply Finset.disjoint_left.mpr | |
| intro v hv hw | |
| have hve : e β M β§ (e.1=v β¨ e.2=v) := β¨he,(hends e v).mp hvβ© | |
| have hvf : f β M β§ (f.1=v β¨ f.2=v) := β¨hf,(hends f v).mp hwβ© | |
| exact hne ((hM.2 v).unique hve hvf) | |
| have hall : M.biUnion ends = Finset.univ := by | |
| ext v | |
| simp only [Finset.mem_univ, iff_true] | |
| obtain β¨e,he,_β© := hM.2 v | |
| exact Finset.mem_biUnion.mpr β¨e,he.1,(hends e v).mpr he.2β© | |
| calc | |
| (β e β M, s e.1*s e.2) = β e β M, β v β ends e, s v := by | |
| apply Finset.prod_congr rfl | |
| intro e he | |
| have hne : e.1 β e.2 := (G.increasing e (hM.1 he)).ne | |
| simp [ends,hne] | |
| _ = β v β M.biUnion ends, s v := (Finset.prod_biUnion hpair).symm | |
| _ = β v, s v := by rw [hall] | |
| noncomputable def weightedPartition (G : GraphInput) (w : Fin G.n Γ Fin G.n β β) : β := | |
| β M β perfectMatchings G, β e β M, w e | |
| theorem vertex_scaled_partition (G : GraphInput) (w : Fin G.n Γ Fin G.n β β) | |
| (s : Fin G.n β β) : | |
| weightedPartition G (fun e => w e*s e.1*s e.2) = | |
| weightedPartition G w * β v, s v := by | |
| classical | |
| unfold weightedPartition | |
| rw [Finset.sum_mul] | |
| apply Finset.sum_congr rfl | |
| intro M hM | |
| have hp : Perfect G M := (Finset.mem_filter.mp hM).2 | |
| calc | |
| (β e β M, w e*s e.1*s e.2) = β e β M, w e*(s e.1*s e.2) := by | |
| apply Finset.prod_congr rfl | |
| intro e he | |
| ring | |
| _ = (β e β M, w e)*(β e β M, s e.1*s e.2) := Finset.prod_mul_distrib | |
| _ = (β e β M, w e)*(β v, s v) := by rw [perfect_vertex_factor G M hp s] | |
| def NearPerfect (G : GraphInput) (i j : Fin G.n) | |
| (M : Finset (Fin G.n Γ Fin G.n)) : Prop := | |
| M β G.edges β§ (β e β M, e.1 β i β§ e.2 β i β§ e.1 β j β§ e.2 β j) β§ | |
| β v, v β i β v β j β β! e, e β M β§ (e.1=v β¨ e.2=v) | |
| noncomputable def holePartition (G : GraphInput) (i j : Fin G.n) | |
| (w : Fin G.n Γ Fin G.n β β) : β := by | |
| classical | |
| exact β M β G.edges.powerset.filter (NearPerfect G i j), β e β M, w e | |
| noncomputable def holeRatio (G : GraphInput) (i j : Fin G.n) | |
| (w : Fin G.n Γ Fin G.n β β) : β := | |
| holePartition G i j w / weightedPartition G w | |
| theorem near_vertex_factor (G : GraphInput) (i j : Fin G.n) (hij : i β j) | |
| (M : Finset (Fin G.n Γ Fin G.n)) (hM : NearPerfect G i j M) (s : Fin G.n β β) : | |
| (β e β M, s e.1*s e.2) * (s i*s j) = β v, s v := by | |
| classical | |
| let ends : Fin G.n Γ Fin G.n β Finset (Fin G.n) := fun e => {e.1,e.2} | |
| have hends (e : Fin G.n Γ Fin G.n) (v) : v β ends e β e.1=v β¨ e.2=v := by | |
| simp [ends, eq_comm] | |
| have hpair : (M : Set (Fin G.n Γ Fin G.n)).PairwiseDisjoint ends := by | |
| intro e he f hf hne | |
| apply Finset.disjoint_left.mpr | |
| intro v hv hw | |
| have hve : e β M β§ (e.1=v β¨ e.2=v) := β¨he,(hends e v).mp hvβ© | |
| have hvf : f β M β§ (f.1=v β¨ f.2=v) := β¨hf,(hends f v).mp hwβ© | |
| have hvi : v β i := by | |
| rcases hve.2 with h | h <;> subst v | |
| Β· exact (hM.2.1 e he).1 | |
| Β· exact (hM.2.1 e he).2.1 | |
| have hvj : v β j := by | |
| rcases hve.2 with h | h <;> subst v | |
| Β· exact (hM.2.1 e he).2.2.1 | |
| Β· exact (hM.2.1 e he).2.2.2 | |
| exact hne ((hM.2.2 v hvi hvj).unique hve hvf) | |
| have hall : M.biUnion ends = (Finset.univ.erase i).erase j := by | |
| ext v | |
| simp only [Finset.mem_biUnion, Finset.mem_erase, Finset.mem_univ, and_true] | |
| constructor | |
| Β· rintro β¨e,he,hvβ© | |
| rcases (hends e v).mp hv with h | h <;> subst v | |
| Β· exact β¨(hM.2.1 e he).2.2.1,(hM.2.1 e he).1β© | |
| Β· exact β¨(hM.2.1 e he).2.2.2,(hM.2.1 e he).2.1β© | |
| Β· rintro β¨hvj,hviβ© | |
| obtain β¨e,he,_β© := hM.2.2 v hvi hvj | |
| exact β¨e,he.1,(hends e v).mpr he.2β© | |
| have hfactor : (β e β M, s e.1*s e.2) = β v β (Finset.univ.erase i).erase j, s v := by | |
| calc | |
| (β e β M, s e.1*s e.2) = β e β M, β v β ends e, s v := by | |
| apply Finset.prod_congr rfl | |
| intro e he | |
| have hne : e.1 β e.2 := (G.increasing e (hM.1 he)).ne | |
| simp [ends,hne] | |
| _ = β v β M.biUnion ends, s v := (Finset.prod_biUnion hpair).symm | |
| _ = _ := by rw [hall] | |
| rw [hfactor] | |
| calc | |
| (β v β (Finset.univ.erase i).erase j, s v)*(s i*s j) = | |
| ((β v β (Finset.univ.erase i).erase j, s v)*s j)*s i := by ring | |
| _ = (β v β Finset.univ.erase i, s v)*s i := by | |
| rw [Finset.prod_erase_mul _ _ (by simp [hij.symm])] | |
| _ = β v, s v := Finset.prod_erase_mul _ _ (Finset.mem_univ _) | |
| theorem hole_partition_vertex_scaling (G : GraphInput) (i j : Fin G.n) (hij : i β j) | |
| (w : Fin G.n Γ Fin G.n β β) (s : Fin G.n β β) : | |
| holePartition G i j (fun e => w e*s e.1*s e.2)*(s i*s j) = | |
| holePartition G i j w * β v, s v := by | |
| classical | |
| unfold holePartition | |
| rw [Finset.sum_mul,Finset.sum_mul] | |
| apply Finset.sum_congr rfl | |
| intro M hM | |
| have hp : NearPerfect G i j M := (Finset.mem_filter.mp hM).2 | |
| calc | |
| (β e β M, w e*s e.1*s e.2)*(s i*s j) = | |
| ((β e β M, w e)*(β e β M, s e.1*s e.2))*(s i*s j) := by | |
| congr 1 | |
| rw [β Finset.prod_mul_distrib] | |
| apply Finset.prod_congr rfl | |
| intro e he | |
| ring | |
| _ = (β e β M, w e)*((β e β M, s e.1*s e.2)*(s i*s j)) := by ring | |
| _ = (β e β M, w e)*(β v, s v) := by rw [near_vertex_factor G i j hij M hp s] | |
| theorem hole_ratio_vertex_scaling (G : GraphInput) (i j : Fin G.n) (hij : i β j) | |
| (w : Fin G.n Γ Fin G.n β β) (s : Fin G.n β β) (hs : β v, 0 < s v) : | |
| holeRatio G i j (fun e => w e*s e.1*s e.2) = | |
| holeRatio G i j w / (s i*s j) := by | |
| classical | |
| have hsij : s i*s j β 0 := mul_ne_zero (hs i).ne' (hs j).ne' | |
| have hsall : (β v, s v) β 0 := Finset.prod_ne_zero_iff.mpr (fun v _ => (hs v).ne') | |
| have hp := hole_partition_vertex_scaling G i j hij w s | |
| have hscaled : holePartition G i j (fun e => w e*s e.1*s e.2) = | |
| (holePartition G i j w * β v, s v)/(s i*s j) := (eq_div_iff hsij).mpr hp | |
| unfold holeRatio | |
| rw [hscaled, vertex_scaled_partition] | |
| by_cases hw : weightedPartition G w = 0 | |
| Β· simp [hw] | |
| Β· field_simp | |
| end MatchingFPRAS | |
| namespace MatchingFPRAS.Counting | |
| def scheduleAdmissible (n : β) (epsilon : β) (k : β) : Prop := | |
| 1 β€ k β§ (n:β)^(n/2)/(1+1/(n:β))^k β€ epsilon/32 | |
| theorem counting_schedule_exists (n : β) (hn : 2 β€ n) (epsilon : β) | |
| (he : 0 < epsilon) (he1 : epsilon < 1) : | |
| β K : β, scheduleAdmissible n epsilon K β§ | |
| (β k, scheduleAdmissible n epsilon k β K β€ k) β§ | |
| (K:β) β€ 1+2*n*((n/2:β)*Real.log n+Real.log (32/(epsilon:β))) := by | |
| classical | |
| have hnr : (2:β) β€ n := by exact_mod_cast hn | |
| have hn0 : (0:β) < n := by linarith | |
| have her : (0:β) < epsilon := by exact_mod_cast he | |
| have he1r : (epsilon:β) < 1 := by exact_mod_cast he1 | |
| let b : β := 1+1/(n:β) | |
| have hb : 1 < b := by | |
| dsimp [b] | |
| have ht : 0 < 1/(n:β) := by positivity | |
| linarith | |
| have hb0 : 0 < b := by linarith | |
| have hlog : 1/(2*(n:β)) β€ Real.log b := by | |
| have h := Real.one_sub_inv_le_log_of_pos hb0 | |
| have hcalc : 1/(2*(n:β)) β€ 1-bβ»ΒΉ := by | |
| dsimp [b] | |
| field_simp | |
| nlinarith | |
| exact hcalc.trans h | |
| let L : β := (n/2:β)*Real.log n+Real.log (32/(epsilon:β)) | |
| have hratio : 1 < 32/(epsilon:β) := (lt_div_iffβ her).mpr (by linarith) | |
| have hL : 0 < L := by | |
| have ha : 0 β€ (n/2:β)*Real.log (n:β) := mul_nonneg (Nat.cast_nonneg _) (Real.log_nonneg (by linarith)) | |
| have hb := Real.log_pos hratio | |
| dsimp [L] | |
| linarith | |
| let C : β := 2*n*L | |
| have hC : 0 β€ C := by dsimp [C]; positivity | |
| let k : β := βCββ+1 | |
| have hk1 : 1 β€ k := by dsimp [k]; omega | |
| have hkt : C < (k:β) := by | |
| dsimp [k] | |
| exact_mod_cast Nat.lt_floor_add_one C | |
| have hku : (k:β) β€ C+1 := by | |
| dsimp [k] | |
| push_cast | |
| linarith [Nat.floor_le hC] | |
| have hkl : L β€ (k:β)*Real.log b := by | |
| have hk0 : (0:β) β€ k := Nat.cast_nonneg _ | |
| have hm := mul_le_mul_of_nonneg_left hlog hk0 | |
| have hsmall : L β€ (k:β)*(1/(2*(n:β))) := by | |
| rw [mul_one_div] | |
| apply (le_div_iffβ (by positivity : 0 < 2*(n:β))).mpr | |
| dsimp [C] at hkt | |
| nlinarith | |
| exact hsmall.trans hm | |
| have ha0 : (0:β) < (n:β)^(n/2)/b^k := by positivity | |
| have ht0 : (0:β) < (epsilon:β)/32 := by positivity | |
| have hlogratio : Real.log ((n:β)^(n/2)/b^k) β€ Real.log ((epsilon:β)/32) := by | |
| rw [Real.log_div (by positivity) (by positivity),Real.log_pow,Real.log_pow] | |
| have hcancel : Real.log ((epsilon:β)/32) = -Real.log (32/(epsilon:β)) := by | |
| rw [Real.log_div (by positivity) (by norm_num),Real.log_div (by norm_num) (by positivity)] | |
| ring | |
| rw [hcancel] | |
| dsimp [L] at hkl | |
| linarith | |
| have hreal : (n:β)^(n/2)/(1+1/(n:β))^k β€ (epsilon:β)/32 := | |
| (Real.log_le_log_iff ha0 ht0).mp hlogratio | |
| have hrat : (n:β)^(n/2)/(1+1/(n:β))^k β€ epsilon/32 := by | |
| apply (Rat.cast_le (K := β)).mp | |
| push_cast | |
| exact hreal | |
| have hex : β k, scheduleAdmissible n epsilon k := β¨k,hk1,hratβ© | |
| refine β¨Nat.find hex, Nat.find_spec hex, ?_, ?_β© | |
| Β· intro j hj | |
| exact Nat.find_min' hex hj | |
| Β· have hleast : Nat.find hex β€ k := Nat.find_min' hex β¨hk1,hratβ© | |
| have hlr : (Nat.find hex:β) β€ k := by exact_mod_cast hleast | |
| change (Nat.find hex:β) β€ 1+C | |
| linarith | |
| end MatchingFPRAS.Counting | |
| end OAI | |