File size: 8,347 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import OAI.Combinatorics.ContingencyTables.Transport.FiniteGraphRoots
import OAI.Combinatorics.ContingencyTables.Sampling.ProfileExchangeProgram
import OAI.Combinatorics.MatchingCount.Complexity.TreePolynomialCollections
import OAI.Combinatorics.MatchingCount.Complexity.TreeChoiceCosts
import OAI.Combinatorics.MatchingCount.Machines.TreeColex
import OAI.Combinatorics.MatchingCount.Sampling.TreeDPCounts

namespace OAI

/-! # A binary list program computing a component root

The graph is supplied by adjacency lists. The program makes one growth
round per stored vertex, then minimizes the reached indices. No loop
depends on the numeric size of an index or of an edge capacity.
-/

namespace ContingencyTables.FiniteGraphRootProgram

open MatchingFPRAS.TreeTyped Polynomial

abbrev GraphCode := List (List ℕ)
abbrev SearchState := GraphCode×List ℕ
abbrev Input := GraphCode×ℕ

def indices (rows : GraphCode) : List ℕ := List.range rows.length

def indicesRealizer : Realizer indices := composition listLength range

lemma polynomial_indices : PolynomialTime indicesRealizer := by
  apply polynomial_boundedRange polynomial_listLength X
  intro rows
  simpa only [Function.comp_apply,eval_X] using list_length_le_weight rows

def neighborHit (z : List ℕ×List ℕ) : Bool :=
  z.2.any (fun a => decide (a∈z.1))

def neighborHitRealizer : Realizer neighborHit :=
  (composition (mapWith membership) any).congr (by
    intro z
    simp only [Function.comp_apply,List.any_map,Function.comp_def,id_eq,neighborHit])

lemma polynomial_neighborHit : PolynomialTime neighborHitRealizer :=
  polynomial_congr _ (polynomial_composition
    (polynomial_mapWith polynomial_membership) polynomial_any)

def growTest (z : SearchState×ℕ) : Bool :=
  decide (z.2∈z.1.2) || neighborHit (z.1.2,z.1.1.getD z.2 [])

def growTestRealizer : Realizer growTest :=
  composition (f := fun z : SearchState×ℕ =>
      (decide (z.2∈z.1.2),neighborHit (z.1.2,z.1.1.getD z.2 [])))
    (g := fun z : Bool×Bool => z.1 || z.2)
    (pair (composition (pair (composition first second) second) membership)
      (composition (pair (composition first second)
        (composition (pair (composition first first) second) (getDFast [])))
        neighborHitRealizer)) boolOr

lemma polynomial_growTest : PolynomialTime growTestRealizer :=
  polynomial_composition (polynomial_pair
    (polynomial_composition (polynomial_pair
      (polynomial_composition polynomial_first polynomial_second) polynomial_second)
      polynomial_membership)
    (polynomial_composition (polynomial_pair
      (polynomial_composition polynomial_first polynomial_second)
      (polynomial_composition (polynomial_pair
        (polynomial_composition polynomial_first polynomial_first) polynomial_second)
        (polynomial_getDFast []))) polynomial_neighborHit)) polynomial_boolOr

def grow (z : SearchState) : List ℕ :=
  (indices z.1).filter (fun a => growTest (z,a))

def growRealizer : Realizer grow :=
  composition (pair identity (composition first indicesRealizer))
    (filterWith growTestRealizer)

lemma polynomial_grow : PolynomialTime growRealizer :=
  polynomial_composition (polynomial_pair polynomial_identity
    (polynomial_composition polynomial_first polynomial_indices))
    (polynomial_filterWith polynomial_growTest)

def advance (z : List ℕ×SearchState) : SearchState :=
  (z.2.1,grow z.2)

def advanceRealizer : Realizer advance :=
  pair (composition second first)
    (composition (f := fun z : List ℕ×SearchState => z.2) (g := grow) second growRealizer)

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

lemma fold_first (steps : List (List ℕ)) (s : SearchState) :
    (steps.foldl (fun s row => advance (row,s)) s).1=s.1 := by
  induction steps generalizing s with
  | nil => rfl
  | cons row steps ih => exact ih (advance (row,s))

lemma grow_weight (s : SearchState) : weight (grow s)≤weight (indices s.1) :=
  weight_sublist (List.filter_sublist (p := fun a => growTest (s,a)) (l := indices s.1))

lemma fold_weight (steps : List (List ℕ)) (s : SearchState) :
    weight (steps.foldl (fun s row => advance (row,s)) s).2≤
      max (weight s.2) (weight (indices s.1)) := by
  induction steps generalizing s with
  | nil => exact le_max_left _ _
  | cons row steps ih =>
    have ht := ih (advance (row,s))
    have hs := grow_weight s
    exact ht.trans (max_le (hs.trans (le_max_right _ _)) (le_max_right _ _))

lemma indices_weight (rows : GraphCode) :
    weight (indices rows)≤rows.length*(4*rows.length+2)+1 := by
  have h := weight_map_bound (as := List.range rows.length) (f := id)
    (N := 4*rows.length+1) (by
      intro a ha
      exact (weight_nat_numeric a).trans (by
        have := List.mem_range.mp ha
        omega))
  simpa only [List.map_id,List.length_range,indices] using h

lemma polynomial_searchFold : PolynomialTime (fold advanceRealizer) := by
  apply polynomial_fold polynomial_advance (10*(X+1)^2)
  intro steps s pre _post _he
  let S := weight (steps,s)
  let out := pre.foldl (fun s row => advance (row,s)) s
  have hs : weight s≤S := weight_snd (steps,s)
  have hr : weight s.1≤S := (weight_fst s).trans hs
  have hv : weight s.2≤S := (weight_snd s).trans hs
  have hn : s.1.length≤S := (list_length_le_weight s.1).trans hr
  have hi := indices_weight s.1
  have hib : weight (indices s.1)≤4*S^2+2*S+1 := by
    have hm := Nat.mul_le_mul hn (show 4*s.1.length+2≤4*S+2 by omega)
    nlinarith
  have ho : weight out.2 ≤ max (weight s.2) (weight (indices s.1)) := fold_weight pre s
  have hot : weight out.2≤4*S^2+3*S+1 := ho.trans (max_le (by omega) (by omega))
  have hf : out.1=s.1 := fold_first pre s
  have hw : weight out=weight out.1+weight out.2+1 := by
    change weight (out.1,out.2)=_
    rw [weight_prod]
    omega
  simp only [eval_mul,eval_ofNat,eval_pow,eval_add,eval_X,eval_one]
  change weight out≤10*(S+1)^2
  rw [hw,hf]
  nlinarith

def prepare (z : Input) : GraphCode×SearchState := (z.1,(z.1,[z.2]))

def prepareRealizer : Realizer prepare :=
  pair first (pair first (composition (pair second (constant [])) listCons))

lemma polynomial_prepare : PolynomialTime prepareRealizer :=
  polynomial_pair polynomial_first (polynomial_pair polynomial_first
    (polynomial_composition (polynomial_pair polynomial_second (polynomial_constant []))
      polynomial_listCons))

def run (z : Input) : SearchState :=
  z.1.foldl (fun s row => advance (row,s)) (z.1,[z.2])

def runRealizer : Realizer run := composition prepareRealizer (fold advanceRealizer)

lemma polynomial_run : PolynomialTime runRealizer :=
  polynomial_composition polynomial_prepare polynomial_searchFold

def reached (z : Input) : List ℕ := (run z).2

def reachedRealizer : Realizer reached :=
  composition (f := run) (g := fun s : SearchState => s.2) runRealizer second

theorem polynomial_reached : PolynomialTime reachedRealizer :=
  polynomial_composition polynomial_run polynomial_second

lemma minimum_fold_le (xs : List ℕ) (b : ℕ) :
    xs.foldl (fun b a => min a b) b≤b := by
  induction xs generalizing b with
  | nil => exact le_rfl
  | cons a xs ih => exact (ih (min a b)).trans (min_le_right _ _)

lemma polynomial_minimumFold : PolynomialTime (fold minimum) := by
  apply polynomial_fold polynomial_minimum (4*X+1)
  intro xs b pre _post _he
  have hb : b.size≤weight (xs,b) := (size_le_weight b).trans (weight_snd (xs,b))
  have hs := Nat.size_le_size (minimum_fold_le pre b)
  have hw := weight_nat_le (pre.foldl (fun b a => min a b) b)
  simp only [eval_add,eval_mul,eval_ofNat,eval_X,eval_one]
  omega

def rootCode (z : Input) : ℕ :=
  (reached z).foldl (fun b a => min a b) z.1.length

def rootCodeRealizer : Realizer rootCode :=
  composition (f := fun z : Input => (reached z,z.1.length))
    (g := fun z : List ℕ×ℕ => z.1.foldl (fun b a => min a b) z.2)
    (pair reachedRealizer (composition first listLength)) (fold minimum)

theorem polynomial_rootCode : PolynomialTime rootCodeRealizer :=
  polynomial_composition (polynomial_pair polynomial_reached
    (polynomial_composition polynomial_first polynomial_listLength)) polynomial_minimumFold

end ContingencyTables.FiniteGraphRootProgram

end OAI