import Mathlib import OAI.Probability.CriticalPercolation.Clusters.ComponentSelf import OAI.Probability.CriticalPercolation.Clusters.ClusterObject import OAI.Probability.CriticalPercolation.Exploration.SearchTree namespace OAI section section noncomputable section open Set open scoped Classical namespace CriticalPercolation.IncidenceSearch structure State (E : Type*) where opened : Finset E read : Finset E variable {V E : Type*} [DecidableEq E] def State.closed (st : State E) (e : E) : State E := ⟨st.opened,insert e st.read⟩ def State.open (st : State E) (e : E) : State E := ⟨insert e st.opened,insert e st.read⟩ def Available (G : BondGraph V E) (v : V) (st : State E) (e : E) : Prop := e ∉ st.read ∧ e ∈ G.incidentEdges (G.cluster (st.opened : Set E) v) def compile (G : BondGraph V E) (v : V) : ℕ → State E → SearchTree E (State E) | 0, st => .done st | n+1, st => if h : ∃ e, Available G v st e then let e := h.choose .ask e (compile G v n (st.closed e)) (compile G v n (st.open e)) else .done st lemma compile_valid (G : BondGraph V E) (v : V) (n : ℕ) (st : State E) : (compile G v n st).Valid st.read := by induction n generalizing st with | zero => trivial | succ n ih => rw [compile] split_ifs with h · exact ⟨h.choose_spec.1,ih _,ih _⟩ · trivial def Good (G : BondGraph V E) (v : V) (ω : Set E) (st : State E) : Prop := st.opened ⊆ st.read ∧ (∀ e ∈ st.read, e ∈ st.opened ↔ e ∈ ω) ∧ ∀ e ∈ st.read, e ∈ G.incidentEdges (G.cluster (st.opened : Set E) v) lemma good_closed (G : BondGraph V E) (v : V) (ω : Set E) (st : State E) (hs : Good G v ω st) (e : E) (he : Available G v st e) (hω : e ∉ ω) : Good G v ω (st.closed e) := by refine ⟨fun a ha => Finset.mem_insert_of_mem (hs.1 ha),?_,?_⟩ · intro a ha rcases Finset.mem_insert.mp ha with rfl|ha · exact iff_of_false (fun h => he.1 (hs.1 h)) hω · exact hs.2.1 a ha · intro a ha rcases Finset.mem_insert.mp ha with rfl|ha · exact he.2 · exact hs.2.2 a ha lemma good_open (G : BondGraph V E) (v : V) (ω : Set E) (st : State E) (hs : Good G v ω st) (e : E) (he : Available G v st e) (hω : e ∈ ω) : Good G v ω (st.open e) := by refine ⟨Finset.insert_subset_insert e hs.1,?_,?_⟩ · intro a ha rcases Finset.mem_insert.mp ha with rfl|ha · exact iff_of_true (Finset.mem_insert_self _ _) hω · have hae : a≠e := fun hh => he.1 (hh ▸ ha) simpa only [State.open,Finset.mem_insert,hae,false_or] using hs.2.1 a ha · intro a ha change a ∈ G.incidentEdges (G.cluster ((insert e st.opened : Finset E) : Set E) v) rw [Finset.coe_insert] apply G.incidentEdges_mono (G.cluster_mono (Set.subset_insert e (st.opened : Set E)) v) rcases Finset.mem_insert.mp ha with rfl|ha · exact he.2 · exact hs.2.2 a ha lemma terminal_cluster (G : BondGraph V E) (v : V) (ω : Set E) (st : State E) (hs : Good G v ω st) (ht : ¬∃ e, Available G v st e) : G.cluster ω v = G.cluster (st.opened : Set E) v ∧ (st.read : Set E) = G.incidentEdges (G.cluster ω v) := by have hread : ∀ e ∈ G.incidentEdges (G.cluster (st.opened : Set E) v), e ∈ st.read := by intro e he by_contra hh exact ht ⟨e,hh,he⟩ have hc : G.cluster ω v = G.cluster (st.opened : Set E) v := by have hh := G.roots_eq_of_incident (S := {v}) (U := G.cluster (st.opened : Set E) v) (ω := (st.opened : Set E)) (ω' := ω) (by simp only [ClusterDeletion.roots_singleton,ClusterDeletion.component,BondGraph.cluster]) (fun e he => hs.2.1 e (hread e he)) simpa only [ClusterDeletion.roots_singleton,ClusterDeletion.component,BondGraph.cluster] using hh refine ⟨hc,?_⟩ rw [hc] exact Set.Subset.antisymm hs.2.2 hread variable [Fintype E] lemma compile_terminal (G : BondGraph V E) (v : V) (ω : Set E) (n : ℕ) (st : State E) (hs : Good G v ω st) (hn : Fintype.card E - st.read.card < n) : let out := (compile G v n st).run (fun e => decide (e∈ω)) Good G v ω out ∧ ¬∃ e, Available G v out e := by induction n generalizing st with | zero => omega | succ n ih => rw [compile] split_ifs with h · let e := h.choose have he : Available G v st e := h.choose_spec have hc : (insert e st.read).card = st.read.card+1 := Finset.card_insert_of_notMem he.1 have hle : st.read.card < Fintype.card E := by have hh := Finset.card_le_univ (s := insert e st.read) rw [hc] at hh omega have hn' : Fintype.card E - (insert e st.read).card < n := by rw [hc]; omega simp only [SearchTree.run] change (let out := if decide (e∈ω) then (compile G v n (st.open e)).run (fun a => decide (a∈ω)) else (compile G v n (st.closed e)).run (fun a => decide (a∈ω)); Good G v ω out ∧ ¬∃ a, Available G v out a) by_cases hω : e∈ω · simp only [hω,decide_true,ite_true] exact ih (st.open e) (good_open G v ω st hs e he hω) hn' · simp only [hω,decide_false,Bool.false_eq_true,ite_false] exact ih (st.closed e) (good_closed G v ω st hs e he hω) hn' · exact ⟨hs,h⟩ end CriticalPercolation.IncidenceSearch end end end end OAI