math / data /lean /OAI /Computability /MinUncut /Graphs /GraphOutput.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
10.2 kB
import Mathlib
import OAI.Computability.MinUncut.Graphs.SignedGraph
namespace OAI
noncomputable section
open scoped BigOperators
namespace MinUncut.PathRealization
attribute [local instance] Classical.propDecidable
variable {V D : Type*} [Fintype V] [Fintype D] [DecidableEq V] [DecidableEq D]
variable {N : β„•}
def output (e : D β†’ Demand V) (number : Vertex V D ≃ Fin N) (k : β„•) (hk : 1 ≀ k) : MinUncut.Output where
vertices := N
adjacent u v := decide ((graph e).Adj (number.symm u) (number.symm v))
symmetric u v := by simp only [decide_eq_decide]; exact (graph e).adj_comm _ _
loopless u := by simp
threshold := k
threshold_pos := hk
def edgePair (e : D β†’ Demand V) (number : Vertex V D ≃ Fin N) (p : EdgeIndex e) : Fin N Γ— Fin N :=
(Sym2.sortEquiv (Sym2.map number (edge e p))).val
omit [Fintype V] [Fintype D] [DecidableEq V] [DecidableEq D] in
lemma edgePair_sym (e : D β†’ Demand V) (number : Vertex V D ≃ Fin N) (p : EdgeIndex e) :
s((edgePair e number p).1,(edgePair e number p).2)=Sym2.map number (edge e p) :=
Sym2.sortEquiv.symm_apply_apply _
omit [Fintype V] [Fintype D] [DecidableEq V] [DecidableEq D] in
lemma edgePair_injective (e : D β†’ Demand V) (number : Vertex V D ≃ Fin N) :
Function.Injective (edgePair e number) := by
intro p q h
apply edge_injective e
apply Sym2.map.injective number.injective
rw [← edgePair_sym,← edgePair_sym,h]
omit [Fintype V] [Fintype D] [DecidableEq V] [DecidableEq D] in
lemma edgePair_lt (e : D β†’ Demand V) (number : Vertex V D ≃ Fin N) (p : EdgeIndex e) :
(edgePair e number p).1 < (edgePair e number p).2 := by
have hl := (Sym2.sortEquiv (Sym2.map number (edge e p))).property
apply lt_of_le_of_ne hl
intro he
have hd : (Sym2.map number (edge e p)).IsDiag := by
rw [← edgePair_sym,Sym2.mk_isDiag_iff]
exact he
exact ends_ne e p ((Sym2.isDiag_map number.injective).mp hd)
omit [Fintype V] in
lemma output_adjacent_iff (e : D β†’ Demand V) (number : Vertex V D ≃ Fin N) (k : β„•) (hk : 1 ≀ k)
(u v : Fin N) :
(output e number k hk).adjacent u v=true ↔ βˆƒ p : EdgeIndex e, s(u,v)=Sym2.map number (edge e p) := by
simp only [output,decide_eq_true_eq,graph,edgeSet,Finset.mem_image,Finset.mem_univ,true_and]
constructor
· rintro ⟨p,hp⟩
refine ⟨p,?_⟩
simpa using (congrArg (Sym2.map number) hp).symm
· rintro ⟨p,hp⟩
refine ⟨p,?_⟩
apply Sym2.map.injective number.injective
simpa using hp.symm
omit [Fintype V] in
lemma output_forward_edge_iff (e : D β†’ Demand V) (number : Vertex V D ≃ Fin N) (k : β„•) (hk : 1 ≀ k)
(u v : Fin N) :
(u<v ∧ (output e number k hk).adjacent u v=true) ↔ βˆƒ p : EdgeIndex e, edgePair e number p=(u,v) := by
rw [output_adjacent_iff]
constructor
· rintro ⟨hu,p,hp⟩
refine ⟨p,?_⟩
unfold edgePair
rw [← hp]
simp [Sym2.sortEquiv,le_of_lt hu]
· rintro ⟨p,hp⟩
refine ⟨?_,p,?_⟩
Β· simpa only [hp] using edgePair_lt e number p
Β· rw [← edgePair_sym,hp]
omit [Fintype V] [Fintype D] [DecidableEq V] [DecidableEq D] in
lemma edgePair_mono (e : D β†’ Demand V) (number : Vertex V D ≃ Fin N)
(s : Fin N β†’ Bool) (p : EdgeIndex e) :
mono (s (edgePair e number p).1) (s (edgePair e number p).2)=
mono (s (number (ends e p.val).1)) (s (number (ends e p.val).2)) := by
have he := edgePair_sym e number p
change s((edgePair e number p).1,(edgePair e number p).2)=s(number (ends e p.val).1,number (ends e p.val).2) at he
rcases Sym2.eq_iff.mp he with h|h
Β· rw [h.1,h.2]
Β· rw [h.1,h.2]; simp only [mono,eq_comm]
omit [Fintype V] in
lemma output_uncut (e : D β†’ Demand V) (number : Vertex V D ≃ Fin N) (k : β„•) (hk : 1 ≀ k)
(s : Fin N β†’ Bool) :
(output e number k hk).uncut s=uncut e (s ∘ number) := by
have he : (Finset.univ.filter fun uv : Fin NΓ—Fin N =>
uv.1<uv.2 ∧ (output e number k hk).adjacent uv.1 uv.2=true ∧ s uv.1=s uv.2)=
(Finset.univ.filter fun p : EdgeIndex e => s (edgePair e number p).1=s (edgePair e number p).2).image (edgePair e number) := by
ext uv
simp only [Finset.mem_filter,Finset.mem_univ,true_and,Finset.mem_image]
constructor
· rintro ⟨hu,ha,hs⟩
obtain ⟨p,hp⟩ := (output_forward_edge_iff e number k hk uv.1 uv.2).mp ⟨hu,ha⟩
exact ⟨p,by simpa only [hp] using hs,hp⟩
· rintro ⟨p,hs,hp⟩
have hh := (output_forward_edge_iff e number k hk uv.1 uv.2).mpr ⟨p,hp⟩
exact ⟨hh.1,hh.2,by simpa only [hp] using hs⟩
unfold MinUncut.Output.uncut
erw [he,Finset.card_image_of_injective _ (edgePair_injective e number)]
change _=βˆ‘ p : EdgeIndex e, mono (s (number (ends e p.val).1)) (s (number (ends e p.val).2))
simp_rw [← edgePair_mono]
simp only [mono,Finset.card_filter]
lemma output_opt_le (G : MinUncut.Output) (s : Fin G.vertices β†’ Bool) : G.opt ≀ G.uncut s := by
exact Finset.min'_le _ _ (Finset.mem_image.mpr ⟨s,Finset.mem_univ _,rfl⟩)
lemma le_output_opt (G : MinUncut.Output) (r : β„•) (h : βˆ€ s, r ≀ G.uncut s) : r ≀ G.opt := by
apply Finset.le_min'
intro x hx
obtain ⟨s,_,rfl⟩ := Finset.mem_image.mp hx
exact h s
omit [Fintype V] in
lemma output_yes (e : D β†’ Demand V) (number : Vertex V D ≃ Fin N) (k : β„•) (hk : 1 ≀ k)
(s : V β†’ Bool) (h : totalFailure e s ≀ k) : (output e number k hk).opt ≀ k := by
apply (output_opt_le _ ((extend e s) ∘ number.symm)).trans
erw [output_uncut]
have he : ((extend e s) ∘ number.symm) ∘ number=extend e s := by ext v; simp
erw [he,uncut_extend]
exact h
omit [Fintype V] in
lemma output_no (e : D β†’ Demand V) (number : Vertex V D ≃ Fin N) (k : β„•) (hk : 1 ≀ k)
(R : β„•) (h : βˆ€ s : V β†’ Bool, R<totalFailure e s) : R<(output e number k hk).opt := by
apply lt_of_lt_of_le (Nat.lt_succ_self R)
apply le_output_opt
intro s
erw [output_uncut]
exact (h (fun v => s (number (.inl v)))).trans_le (uncut_lower e (s ∘ number))
end MinUncut.PathRealization
open scoped BigOperators
namespace MinUncut.FiniteProof
open PathRealization
variable {E V : Type*} [Fintype E]
def multiplicity (D : β„•) (w : β„š) : β„• := w.num.toNat*(D/w.den)
lemma multiplicity_cast {D : β„•} {w : β„š} (hw : 0≀w) (hD : w.den ∣ D) :
(multiplicity D w:β„š)=(D:β„š)*w := by
have hn : 0≀w.num := Rat.num_nonneg.mpr hw
have hncast : (w.num.toNat:β„š)=(w.num:β„š) := by exact_mod_cast Int.toNat_of_nonneg hn
calc
(multiplicity D w:β„š)=(w.num:β„š)*((D:β„š)/(w.den:β„š)) := by
unfold multiplicity
rw [Nat.cast_mul,Nat.cast_div_charZero hD,hncast]
_ = (D:β„š)*((w.num:β„š)/(w.den:β„š)) := by ring
_ = (D:β„š)*w := by rw [Rat.num_div_den]
abbrev UnitDemand (D : β„•) (w : E β†’ β„š) := Ξ£ e, Fin (multiplicity D (w e))
def expanded (D : β„•) (w : E β†’ β„š) (d : E β†’ Demand V) : UnitDemand D w β†’ Demand V :=
fun p => d p.1
lemma expanded_failure (D : β„•) (w : E β†’ β„š) (d : E β†’ Demand V) (y : V β†’ Bool) :
totalFailure (expanded D w d) y = βˆ‘ e, multiplicity D (w e)*failure (d e) y := by
simp [totalFailure,expanded,Fintype.sum_sigma]
lemma expanded_weight (D : β„•) (w : E β†’ β„š) (hw : βˆ€ e, 0≀w e) (hD : βˆ€ e, (w e).den ∣ D)
(d : E β†’ Demand V) (y : V β†’ Bool) :
(totalFailure (expanded D w d) y:ℝ)=(D:ℝ)*βˆ‘ e, (w e:ℝ)*(failure (d e) y:ℝ) := by
rw [expanded_failure]
push_cast
rw [Finset.mul_sum]
apply Finset.sum_congr rfl
intro e _
have h : (multiplicity D (w e):ℝ)=(D:ℝ)*(w e:ℝ) := by
exact_mod_cast multiplicity_cast (hw e) (hD e)
rw [h,mul_assoc]
lemma expanded_card (D : β„•) (w : E β†’ β„š) :
Fintype.card (UnitDemand D w)=βˆ‘ e, multiplicity D (w e) := by
simp [Fintype.card_sigma]
lemma expanded_card_cast (D : β„•) (w : E β†’ β„š) (hw : βˆ€ e, 0≀w e) (hD : βˆ€ e, (w e).den ∣ D) :
(Fintype.card (UnitDemand D w):β„š)=(D:β„š)*βˆ‘ e, w e := by
rw [expanded_card,Nat.cast_sum,Finset.mul_sum]
exact Finset.sum_congr rfl (fun e _ => multiplicity_cast (hw e) (hD e))
open scoped BigOperators
open PathRealization
attribute [local instance] Classical.propDecidable
variable {E V : Type*} [Fintype E] [Fintype V] [DecidableEq V]
lemma threshold_positive {D : β„•} (hD : 0<D) : 1≀5*D := by omega
def rationalGraph (D : β„•) (hD : 0<D) (w : E β†’ β„š) (d : E β†’ Demand V) : MinUncut.Output :=
output (expanded D w d) (Fintype.equivFin _) (5*D) (threshold_positive hD)
lemma rationalGraph_threshold (D : β„•) (hD : 0<D) (w : E β†’ β„š) (d : E β†’ Demand V) :
(rationalGraph D hD w d).threshold=5*D := rfl
lemma rationalGraph_vertices (D : β„•) (hD : 0<D) (w : E β†’ β„š) (d : E β†’ Demand V) :
(rationalGraph D hD w d).vertices=Fintype.card V+3*βˆ‘ e, multiplicity D (w e) := by
simp [rationalGraph,output,Vertex,Fintype.card_sum,Fintype.card_prod,mul_comm]
lemma rationalGraph_yes (D : β„•) (hD : 0<D) (w : E β†’ β„š) (hw : βˆ€ e, 0≀w e)
(hden : βˆ€ e, (w e).den ∣ D) (d : E β†’ Demand V) (y : V β†’ Bool)
(hc : (βˆ‘ e, (w e:ℝ)*(failure (d e) y:ℝ))≀9/2) :
(rationalGraph D hD w d).opt≀(rationalGraph D hD w d).threshold := by
apply output_yes
have he := expanded_weight D w hw hden d y
have hD' : (0:ℝ)<D := by exact_mod_cast hD
have hh := mul_le_mul_of_nonneg_left hc hD'.le
have hz : (totalFailure (expanded D w d) y:ℝ)≀5*D := by rw [he]; nlinarith
exact_mod_cast hz
lemma rationalGraph_no {K : β„•} (hK : 2≀K) (D : β„•) (hD : 0<D) (w : E β†’ β„š)
(hw : βˆ€ e, 0≀w e) (hden : βˆ€ e, (w e).den ∣ D) (d : E β†’ Demand V)
(hc : βˆ€ y, 8*(K:ℝ)-1/2 < βˆ‘ e, (w e:ℝ)*(failure (d e) y:ℝ)) :
K*(rationalGraph D hD w d).threshold < (rationalGraph D hD w d).opt := by
apply output_no
intro y
have he := expanded_weight D w hw hden d y
have hK' : (2:ℝ)≀K := by exact_mod_cast hK
have hg : 5*(K:ℝ) < βˆ‘ e, (w e:ℝ)*(failure (d e) y:ℝ) := lt_trans (by linarith) (hc y)
have hD' : (0:ℝ)<D := by exact_mod_cast hD
have hh := mul_lt_mul_of_pos_left hg hD'
have hz : (K:ℝ)*(5*D) < (totalFailure (expanded D w d) y:ℝ) := by rw [he]; nlinarith
exact_mod_cast hz
end MinUncut.FiniteProof
end
end OAI