File size: 5,328 Bytes
97c3405
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import OAI.Combinatorics.ContingencyTables.Transport.DirectedBFSBounds
import OAI.Combinatorics.MatchingCount.Complexity.TreePolynomialGrid

namespace OAI

/-! # Polynomial binary execution of the entire materialized BFS -/

namespace ContingencyTables.DirectedBFSRuntime

open MatchingFPRAS.TreeTyped Polynomial DirectedBFSCode DirectedBFSBounds DirectedBFSProgram

def advance (z : ℕ×Context) : Context := (z.2.1,round z.2.1 z.2.2)
def advanceRealizer : Realizer advance :=
  pair (composition second first) (composition second roundRealizer)

lemma polynomial_advance : PolynomialTime advanceRealizer :=
  polynomial_pair (polynomial_composition polynomial_second polynomial_first)
    (polynomial_composition polynomial_second polynomial_round)

lemma fold_eq (steps : List ℕ) (g : Graph) (P : Store) :
    steps.foldl (fun s i=>advance (i,s)) (g,P)=
      (g,steps.foldl (fun P _=>round g P) P) := by
  induction steps generalizing P with
  | nil => rfl
  | cons i steps ih => exact ih (round g P)

theorem polynomial_fold : PolynomialTime (MatchingFPRAS.TreeTyped.fold advanceRealizer) := by
  apply MatchingFPRAS.TreeTyped.polynomial_fold polynomial_advance (20*(X+1)^3)
  intro steps s pre post he
  let S := weight (steps,s)
  have hsw : weight s≤ S := weight_snd (steps,s)
  have hgw : weight s.1≤ S := (weight_fst s).trans hsw
  have hpw : weight s.2≤ S := (weight_snd s).trans hsw
  have hn : s.1.length≤ S := (list_length_le_weight s.1).trans hgw
  have hpn : s.2.length≤ S := (list_length_le_weight s.2).trans hpw
  have hpre : pre.length≤ S := by
    have hl : pre.length≤ steps.length := by rw [he,List.length_append]; omega
    exact hl.trans ((list_length_le_weight steps).trans (weight_fst (steps,s)))
  have hb : Bounded s.2 (S+5) := by
    intro o ho
    exact (weight_mem ho).trans (hpw.trans (by omega))
  let out := pre.foldl (fun P _=>round s.1 P) s.2
  have ho : Bounded out (S+5+pre.length*(4*s.1.length+2)) :=
    fold_bounded s.1 s.2 (S+5) (by omega) hb pre
  have hol : out.length≤ S := (fold_length_le s.1 s.2 pre).trans
    (max_le hpn hn)
  have hob : S+5+pre.length*(4*s.1.length+2)≤4*S^2+3*S+5 := by
    have hm := Nat.mul_le_mul hpre (show 4*s.1.length+2≤4*S+2 by omega)
    nlinarith
  have hwo := store_weight out _ ho
  have hwm := Nat.mul_le_mul hol (Nat.add_le_add_right hob 1)
  have hwout : weight out≤4*S^3+3*S^2+6*S+1 := by nlinarith
  change weight (pre.foldl (fun s i=>advance (i,s)) s)≤_
  have hf := fold_eq pre s.1 s.2
  rw [Prod.eta] at hf
  rw [hf,weight_prod]
  simp only [eval_mul,eval_ofNat,eval_pow,eval_add,eval_X,eval_one]
  change 1+weight s.1+weight out≤20*(S+1)^3
  nlinarith

def initialRow (z : ℕ×ℕ) : Option (List ℕ) :=
  if z.2=z.1 then Option.some [z.1] else none

def initialRowRealizer : Realizer initialRow :=
  (choose (composition (pair second first) natEqual)
    (composition (composition (pair first (constant [])) listCons) some)
    (constant none)).congr (by
      intro z
      simp only [Function.comp_apply,initialRow,decide_eq_true_eq])

lemma polynomial_initialRow : PolynomialTime initialRowRealizer :=
  polynomial_congr _ (polynomial_choose
    (polynomial_composition (polynomial_pair polynomial_second polynomial_first) polynomial_natEqual)
    (polynomial_composition (polynomial_composition
      (polynomial_pair polynomial_first (polynomial_constant [])) polynomial_listCons) polynomial_some)
    (polynomial_constant none))

def initialInput (z : Graph×ℕ) : Store := initial z.1 z.2
def initialRealizer : Realizer initialInput :=
  composition (pair second (composition first indicesRealizer)) (mapWith initialRowRealizer)

lemma polynomial_initial : PolynomialTime initialRealizer :=
  polynomial_composition (polynomial_pair polynomial_second
    (polynomial_composition polynomial_first polynomial_indices))
    (polynomial_mapWith polynomial_initialRow)

def prepare (z : Graph×ℕ) : List ℕ×Context :=
  (indices z.1,(z.1,initial z.1 z.2))
def prepareRealizer : Realizer prepare :=
  pair (composition (f := fun z : Graph×ℕ=>z.1) (g := indices) first indicesRealizer)
    (pair first initialRealizer)

lemma polynomial_prepare : PolynomialTime prepareRealizer :=
  polynomial_pair (polynomial_composition polynomial_first polynomial_indices)
    (polynomial_pair polynomial_first polynomial_initial)

def runInput (z : Graph×ℕ) : Store := run z.1 z.2
def runRealizer : Realizer runInput :=
  (composition (composition prepareRealizer (MatchingFPRAS.TreeTyped.fold advanceRealizer))
    second).congr (by
      intro z
      change ((indices z.1).foldl (fun s i=>advance (i,s)) (z.1,initial z.1 z.2)).2 = _
      rw [fold_eq]
      rfl)

theorem polynomial_run : PolynomialTime runRealizer :=
  polynomial_congr _ (polynomial_composition
    (polynomial_composition polynomial_prepare polynomial_fold) polynomial_second)

def searchInput (z : (Graph×ℕ)×ℕ) : Option (List ℕ) := search z.1.1 z.1.2 z.2
def searchRealizer : Realizer searchInput :=
  composition (pair (composition first runRealizer) second) lookupRealizer

theorem polynomial_search : PolynomialTime searchRealizer :=
  polynomial_composition (polynomial_pair
    (polynomial_composition polynomial_first polynomial_run) polynomial_second) polynomial_lookup

end ContingencyTables.DirectedBFSRuntime

end OAI