import Mathlib import OAI.Combinatorics.MatchingCount.Sampling.RankOrder namespace OAI namespace MatchingFPRAS.UnitRefresh.Materialized open Finset variable {V : Type*} [Fintype V] [DecidableEq V] def plan {m : ℕ} (rank : Fin m → ℕ) : List (Fin m) := (List.ofFn (fun l : Fin m => l)).mergeSort (fun a b=>decide (rank b≤rank a)) lemma plan_mem {m : ℕ} (rank : Fin m → ℕ) (a : Fin m) : a∈plan rank := by unfold plan rw [(List.mergeSort_perm _ _).mem_iff] simp lemma plan_length {m : ℕ} (rank : Fin m → ℕ) : (plan rank).length=m := by unfold plan rw [(List.mergeSort_perm _ _).length_eq] simp lemma plan_sorted {m : ℕ} (rank : Fin m → ℕ) : (plan rank).Pairwise (fun a b=>rank b≤rank a) := by let r := fun a b : Fin m => rank b≤rank a let : Std.Total r := ⟨fun a b => le_total (rank b) (rank a)⟩ let : IsTrans (Fin m) r := ⟨fun a b c hab hbc => hbc.trans hab⟩ exact List.pairwise_mergeSort' r _ lemma plan_legal {m : ℕ} (bags : V → Finset (Fin m)) (rank : Fin m → ℕ) (parent : Fin m → Option (Fin m)) (hb : RankedBags bags rank parent) (hne : ∀ x,(bags x).Nonempty) (hp : ∀ l p,parent l=some p → rank ppairingCount) rw [←active_univ] rw [tableDP_correct bags parent _ _ (plan_legal bags rank parent hb hne hp)] unfold count apply prod_congr rfl intro a _ rw [read_run] apply finiteDP_correct intro b r hr rw [plan_length] at hr simp [initial,read_tabulate,hr] def branchCount {m : ℕ} (bags : V → Finset (Fin m)) (rank : Fin m → ℕ) (parent : Fin m → Option (Fin m)) (a b : V) : ℕ := count (residualBags bags a b) parent (plan rank) lemma branchCount_eq {m : ℕ} (bags : V → Finset (Fin m)) (rank : Fin m → ℕ) (parent : Fin m → Option (Fin m)) (hb : RankedBags bags rank parent) (hne : ∀ x,(bags x).Nonempty) (hp : ∀ l p,parent l=some p → rank pedgeCount bags rank parent a (finProdFinEquiv.symm i))] simp only [Equiv.symm_apply_apply] rw [edgeCount_sum bags rank parent hb hne hp a] exact div_self (by exact_mod_cast hz.ne') def drawEdge (bags : Fin n → Finset (Fin m)) (rank : Fin m → ℕ) (parent : Fin m → Option (Fin m)) (hb : RankedBags bags rank parent) (hne : ∀ x,(bags x).Nonempty) (hp : ∀ l p,parent l=some p → rank pbags (e x))) : ColoredPairing bags where pair := p.pair.map e color x := p.color (e.symm x) allowed x := by simpa using p.allowed (e.symm x) same_color x := by simpa [Pairing.map] using p.same_color (e.symm x) def ColoredPairing.comap (bags : V → Finset L) (e : U ≃ V) (p : ColoredPairing bags) : ColoredPairing (fun x=>bags (e x)) where pair := p.pair.map e.symm color x := p.color (e x) allowed x := p.allowed (e x) same_color x := by simpa [Pairing.map] using p.same_color (e x) def ColoredPairing.equiv (bags : V → Finset L) (e : U ≃ V) : ColoredPairing (fun x=>bags (e x)) ≃ ColoredPairing bags where toFun := ColoredPairing.map bags e invFun := ColoredPairing.comap bags e left_inv p := by apply ColoredPairing.ext · ext x; simp [ColoredPairing.map,ColoredPairing.comap,Pairing.map] · funext x; simp [ColoredPairing.map,ColoredPairing.comap] right_inv p := by apply ColoredPairing.ext · ext x; simp [ColoredPairing.map,ColoredPairing.comap,Pairing.map] · funext x; simp [ColoredPairing.map,ColoredPairing.comap] def gapEquiv {n : ℕ} (a : Fin (n+1)) : Fin n ≃ {x : Fin (n+1) // x≠a} := Equiv.optionSubtype a ⟨(finSuccEquiv' a).symm,rfl⟩ def removePairEquiv {n : ℕ} (a b : Fin (n+2)) (hb : b≠a) : Fin n ≃ withoutPair a b := by let e := gapEquiv a let b' := e.symm ⟨b,hb⟩ let f := gapEquiv b' refine { toFun := fun i=>⟨(e (f i).val).val,(e (f i).val).property,?_⟩ invFun := fun x=>f.symm ⟨e.symm ⟨x.val,x.property.1⟩,?_⟩ left_inv := ?_ right_inv := ?_ } · intro h apply (f i).property apply e.injective apply Subtype.ext simpa [b'] using h · intro h have hh := congrArg (fun i=>(e i).val) h apply x.property.2 simpa [b'] using hh · intro i apply f.injective simp · intro x apply Subtype.ext simp end MatchingFPRAS.UnitRefresh namespace MatchingFPRAS.UnitRefresh.Materialized open Finset MatchingFPRAS.FiniteChoice variable {m : ℕ} lemma singleton_count_zero (bags : Fin 1 → Finset (Fin m)) (rank : Fin m → ℕ) (parent : Fin m → Option (Fin m)) (hb : RankedBags bags rank parent) (hne : ∀ x,(bags x).Nonempty) (hp : ∀ l p,parent l=some p → rank pp.pair.no_fixed 0 (Subsingleton.elim _ _)⟩ exact Fintype.card_eq_zero def sampleUnit (k : ℕ) : (n : ℕ) → (bags : Fin n → Finset (Fin m)) → (rank : Fin m → ℕ) → (parent : Fin m → Option (Fin m)) → (hb : RankedBags bags rank parent) → (hne : ∀ x,(bags x).Nonempty) → (hp : ∀ l p,parent l=some p → rank p ⟨⟨fun x=>Fin.elim0 x,fun x=>Fin.elim0 x,fun x=>Fin.elim0 x⟩, (fun x=>Fin.elim0 x),(fun x=>Fin.elim0 x),(fun x=>Fin.elim0 x)⟩ | 1,bags,rank,parent,hb,hne,hp,hz,_ => False.elim (by rw [singleton_count_zero bags rank parent hb hne hp] at hz; omega) | n+2,bags,rank,parent,hb,hne,hp,hz,bits => by let a : Fin (n+2) := Fin.last (n+1) let c := drawEdge bags rank parent hb hne hp a hz k (bits a) let e := removePairEquiv a c.val.val.1 c.val.property.1 let b' : Fin n → Finset (Fin m) := fun x=>bags (e x).val have hb' : RankedBags b' rank parent := by intro x l hl r hr; exact hb (e x).val l hl r hr have hne' : ∀ x,(b' x).Nonempty := fun x=>hne (e x).val have hz' : 0bits i.castSucc.castSucc) exact (ColoredPairing.map (residualBags bags a c.val.val.1) e p).insert a c.val open Finset MatchingFPRAS.FiniteChoice MatchingFPRAS.Variation open scoped Classical variable {n m : ℕ} abbrev PositivePartner (bags : Fin n → Finset (Fin m)) (rank : Fin m → ℕ) (parent : Fin m → Option (Fin m)) (a : Fin n) := {c : Partner bags a // 0(branchCount bags rank parent a c.val.val.1:ℝ)/count bags parent (plan rank)) ≤ 2*n*m/(2:ℝ)^k := by let idx := partnerIndex bags rank parent a let p := edgeProbability bags rank parent a let hs := edgeProbability_sum bags rank parent hb hne hp a hz have he (c : PositivePartner bags rank parent a) : (p (idx c):ℝ)=(branchCount bags rank parent a c.val.val.1:ℝ)/count bags parent (plan rank) := by have hv : validBranch bags a c.val.val := c.val.property simp [p,idx,partnerIndex,edgeProbability,edgeCount,hv] have hg : ∑ c : PositivePartner bags rank parent a,|(tapeLaw p hs k (idx c):ℝ)-(p (idx c):ℝ)| ≤ ∑ i : Fin (n*m),|(tapeLaw p hs k i:ℝ)-(p i:ℝ)| := by change (∑ c,|(tapeLaw p hs k (partnerIndex bags rank parent a c):ℝ)-(p (partnerIndex bags rank parent a c):ℝ)|)≤_ rw [←Finset.sum_image (s:=univ) (f:=fun i=>|(tapeLaw p hs k i:ℝ)-(p i:ℝ)|) (fun c _ d _ h=>(partnerIndex_injective bags rank parent a) h)] exact Finset.sum_le_univ_sum_of_nonneg (fun _=>abs_nonneg _) have ht : (∑ i : Fin (n*m),|(tapeLaw p hs k i:ℝ)-(p i:ℝ)|)/2 ≤ 2*(n*m)/(2:ℝ)^k := by have hh := drawEdge_tv bags rank parent hb hne hp a hz k have hcast := (Rat.cast_le (K:=ℝ)).mpr hh push_cast at hcast simpa only [p,hs,Nat.cast_mul] using hcast unfold tv have hg' : (∑ c : PositivePartner bags rank parent a, |imageLaw (drawEdge bags rank parent hb hne hp a hz k) c- (branchCount bags rank parent a c.val.val.1:ℝ)/count bags parent (plan rank)|)≤ ∑ i : Fin (n*m),|(tapeLaw p hs k i:ℝ)-(p i:ℝ)| := by simpa only [drawEdge_law,he] using hg exact (div_le_div_of_nonneg_right hg' (by norm_num)).trans (by simpa only [mul_assoc] using ht) noncomputable def positiveSplitEquiv (bags : Fin n → Finset (Fin m)) (rank : Fin m → ℕ) (parent : Fin m → Option (Fin m)) (hb : RankedBags bags rank parent) (hne : ∀ x,(bags x).Nonempty) (hp : ∀ l p,parent l=some p → rank p⟨s.1.val,s.2⟩ invFun := fun s=>⟨⟨s.1,by rw [branchCount_eq bags rank parent hb hne hp] exact Fintype.card_pos_iff.mpr ⟨s.2⟩⟩,s.2⟩ left_inv := fun s=>by rcases s with ⟨⟨c,hc⟩,x⟩; rfl right_inv := fun s=>by cases s; rfl } exact e.trans (ColoredPairing.split a).symm lemma positiveSplitEquiv_apply (bags : Fin n → Finset (Fin m)) (rank : Fin m → ℕ) (parent : Fin m → Option (Fin m)) (hb : RankedBags bags rank parent) (hne : ∀ x,(bags x).Nonempty) (hp : ∀ l p,parent l=some p → rank p(branchCount bags rank parent a c.val.val.1:ℝ)/count bags parent (plan rank)) (fun c=>uniform (ColoredPairing (residualBags bags a c.val.val.1)))) (positiveSplitEquiv bags rank parent hb hne hp a) z=uniform (ColoredPairing bags) z := by let : Nonempty (ColoredPairing bags) := Fintype.card_pos_iff.mp (by rwa [←computable_count_eq bags rank parent hb hne hp]) simp_rw [branchCount_eq bags rank parent hb hne hp,computable_count_eq bags rank parent hb hne hp] exact uniform_sigma (positiveSplitEquiv bags rank parent hb hne hp a) z end MatchingFPRAS.UnitRefresh.Materialized end OAI