import OAI.Combinatorics.ContingencyTables.Machines.DirectedBFSProgram namespace OAI /-! # Polynomial size of the materialized BFS path store -/ namespace ContingencyTables.DirectedBFSBounds open MatchingFPRAS.TreeTyped DirectedBFSCode def Bounded (P : Store) (B : ℕ) : Prop := ∀ o∈P,weight o≤B lemma none_weight : weight (none : Option (List ℕ))≤5 := by decide lemma lookup_weight (P : Store) (B : ℕ) (hB : 5≤B) (hP : Bounded P B) (v : ℕ) : weight (lookup P v)≤B := by by_cases hv : v have h := lookup_weight P B hB hP v simp only [hc] at h simpa only [roundRow,hc] using h.trans (by omega) | none => cases hp : predecessor g P v with | none => simpa only [roundRow,hc,hp,Option.bind_none] using none_weight.trans (by omega) | some u => cases hu : lookup P u with | none => simpa only [roundRow,hc,hp,Option.bind_some,hu,Option.map_none] using none_weight.trans (by omega) | some xs => have h := lookup_weight P B hB hP u simp only [hu] at h simp only [roundRow,hc,hp,Option.bind_some,hu,Option.map_some,append_weight] omega lemma round_bounded (g : Graph) (P : Store) (B : ℕ) (hB : 5≤B) (hP : Bounded P B) : Bounded (round g P) (B+4*g.length+2) := by intro o ho obtain ⟨v,hv,rfl⟩ := List.mem_map.mp ho exact row_weight g P B hB hP v (List.mem_range.mp hv) lemma round_length (g : Graph) (P : Store) : (round g P).length=g.length := by simp [DirectedBFSCode.round] lemma fold_bounded (g : Graph) (P : Store) (B : ℕ) (hB : 5≤B) (hP : Bounded P B) (steps : List ℕ) : Bounded (steps.foldl (fun P _=>round g P) P) (B+steps.length*(4*g.length+2)) := by induction steps generalizing P B with | nil => simpa using hP | cons a steps ih => have h := ih (round g P) (B+4*g.length+2) (by omega) (round_bounded g P B hB hP) have he : B+(a::steps).length*(4*g.length+2)= B+4*g.length+2+steps.length*(4*g.length+2) := by simp only [List.length_cons] ring rw [List.foldl_cons,he] exact h lemma fold_length_le (g : Graph) (P : Store) (steps : List ℕ) : (steps.foldl (fun P _=>round g P) P).length≤ max P.length g.length := by induction steps generalizing P with | nil => exact le_max_left _ _ | cons a steps ih => have h := ih (round g P) simp only [List.foldl_cons] exact h.trans (by rw [round_length,max_self]; exact le_max_right _ _) lemma store_weight (P : Store) (B : ℕ) (hP : Bounded P B) : weight P≤P.length*(B+1)+1 := by have h := weight_map_bound (as:=P) (f:=id) (N:=B) hP simpa only [List.map_id] using h lemma initial_bounded (g : Graph) (s : ℕ) : Bounded (initial g s) (weight s+10) := by intro o ho obtain ⟨v,_,rfl⟩ := List.mem_map.mp ho by_cases h : v=s · simp only [h,ite_true] change weight (true,[s])≤weight s+10 have hb := weight_bool_le true simp only [weight_prod,weight_cons,weight_nil] omega · simpa only [h,ite_false] using none_weight.trans (by omega) lemma initial_length (g : Graph) (s : ℕ) : (initial g s).length=g.length := by simp [initial] end ContingencyTables.DirectedBFSBounds end OAI