math / data /lean /OAI /Computability /MatchingCount /DenseOracle.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
5.22 kB
import OAI.Computability.MatchingCount.BinaryAlgorithm
namespace OAI
section
namespace BinaryMatching.DenseOracle
open Finset UnitFlow CutAlgebra OddCutParity
variable {n : β„•}
abbrev Matrix (n : β„•) (Ξ± : Type) := Vector (Vector Ξ± n) n
def make {Ξ± : Type} (f : Fin n β†’ Fin n β†’ Ξ±) : Matrix n Ξ± :=
Vector.ofFn (fun i => Vector.ofFn (f i))
def view {Ξ± : Type} (a : Matrix n Ξ±) : Fin n β†’ Fin n β†’ Ξ± := fun i j => a[i][j]
@[simp] lemma view_make {Ξ± : Type} (f : Fin n β†’ Fin n β†’ Ξ±) : view (make f)=f := by
funext i j; simp [view,make]
abbrev Paths (n : β„•) := Vector (Option (List (Fin n))) n
def pathView (p : Paths n) : ResidualSearch.Table n := fun v => p[v]
def pathInitial (a : Fin n) : Paths n := Vector.ofFn (ResidualSearch.initial a)
def pathStep (c : Matrix n β„•) (b : Matrix n β„€) (p : Paths n) : Paths n :=
Vector.ofFn (ResidualSearch.step (UnitMincut.residual (view c) (view b)) (pathView p))
def search (c : Matrix n β„•) (b : Matrix n β„€) (a : Fin n) : β„• β†’ Paths n
| 0 => pathInitial a
| k+1 => pathStep c b (search c b a k)
lemma pathView_ofFn (p : ResidualSearch.Table n) : pathView (Vector.ofFn p)=p := by
funext v; simp [pathView]
lemma search_view (c : Matrix n β„•) (b : Matrix n β„€) (a : Fin n) (k : β„•) :
pathView (search c b a k)=ResidualSearch.search (UnitMincut.residual (view c) (view b)) a k := by
induction k with
| zero => exact pathView_ofFn _
| succ k ih => simp only [search,pathStep,pathView_ofFn,ih,ResidualSearch.search]
def augment (c : Matrix n β„•) (a z : Fin n) (b : Matrix n β„€) : Matrix n β„€ :=
match (search c b a n)[z] with
| none => b
| some p => make (fun i j => view b i j+pathFlow p i j)
def iterate (c : Matrix n β„•) (a z : Fin n) : β„• β†’ Matrix n β„€
| 0 => make (fun _ _ => 0)
| k+1 => augment c a z (iterate c a z k)
lemma augment_view (c : Matrix n β„•) (a z : Fin n) (b : Matrix n β„€) :
view (augment c a z b)=UnitMincut.augment (view c) a z (view b) := by
have hs := congrFun (search_view c b a n) z
change (search c b a n)[z]=_ at hs
simp only [augment,hs,UnitMincut.augment]
cases h : ResidualSearch.search (UnitMincut.residual (view c) (view b)) a n z <;>
simp only [view_make]
lemma iterate_view (c : Matrix n β„•) (a z : Fin n) (k : β„•) :
view (iterate c a z k)=UnitMincut.iterate (view c) a z k := by
induction k with
| zero => exact view_make _
| succ k ih => simp only [iterate,augment_view,ih,UnitMincut.iterate]
def mincut (c : Matrix n β„•) (a z : Fin n) : Finset (Fin n) :=
let b := iterate c a z (UnitMincut.sourceCapacity (view c) a)
ResidualSearch.reached (pathView (search c b a n))
lemma mincut_eq (c : Matrix n β„•) (a z : Fin n) : mincut c a z=UnitMincut.mincut (view c) a z := by
unfold mincut UnitMincut.mincut
dsimp only
rw [search_view]
have h := iterate_view c a z (UnitMincut.sourceCapacity (view c) a)
rw [h]
def terminalCut (c : Matrix n β„•) (U : Finset (Fin n)) : Finset (Fin n) :=
let cuts := (((List.finRange n).product (List.finRange n)).filter
(fun az => decide (az.1∈U ∧ az.2∈U ∧ az.1β‰ az.2))).map
(fun az => mincut c az.1 az.2)
((cuts.argmin (cutCapacity (view c))).getD βˆ…)
lemma terminalCut_eq (c : Matrix n β„•) (U : Finset (Fin n)) :
terminalCut c U=TerminalMincut.terminalCut (view c) U := by
simp only [terminalCut,mincut_eq,TerminalMincut.terminalCut,TerminalMincut.pairCuts]
def contract (c : Matrix n β„•) (f : Fin n β†’ Fin n) : Matrix n β„• := make (push (view c) f)
@[simp] lemma contract_view (c : Matrix n β„•) (f : Fin n β†’ Fin n) :
view (contract c f)=push (view c) f := view_make _
def recurse : β„• β†’ Matrix n β„• β†’ Finset (Fin n) β†’ Finset (Fin n)
| 0,_,_ => βˆ…
| k+1,c,U =>
let A := terminalCut c U
if OddSide U A then A else
match OddMincut.outside A, OddMincut.outside Aᢜ with
| some r,some q =>
let f := contractMap A r
let g := contractMap Aᢜ q
let X := recurse k (contract c f) (A∩U)
let Y := recurse k (contract c g) (Aᢜ∩U)
OddMincut.cheaper (view c) (lift f X) (lift g Y)
| _,_ => βˆ…
lemma recurse_eq (k : β„•) (c : Matrix n β„•) (U : Finset (Fin n)) :
recurse k c U=OddMincut.recurse k (view c) U := by
induction k generalizing c U with
| zero => rfl
| succ k ih =>
simp only [recurse,terminalCut_eq,OddMincut.recurse]
split
Β· rfl
Β· cases h₁ : OddMincut.outside (TerminalMincut.terminalCut (view c) U) <;>
cases hβ‚‚ : OddMincut.outside (TerminalMincut.terminalCut (view c) U)ᢜ <;>
simp only [ih,contract_view]
def oddCut (c : Matrix n β„•) (U : Finset (Fin n)) : Finset (Fin n) := recurse U.card c U
lemma oddCut_eq (c : Matrix n β„•) (U : Finset (Fin n)) :
oddCut c U=OddMincut.oddCut (view c) U := recurse_eq _ _ _
theorem oddCut_correct (c : Matrix n β„•) (hc : βˆ€ i j, view c i j=view c j i)
(U : Finset (Fin n)) (he : Even U.card) (hU : 2≀U.card) :
OddSide U (oddCut c U) ∧
βˆ€ S, OddSide U S β†’ cutCapacity (view c) (oddCut c U) ≀ cutCapacity (view c) S := by
rw [oddCut_eq]
exact OddMincut.oddCut_correct _ hc _ he hU
end BinaryMatching.DenseOracle
end
end OAI