File size: 6,730 Bytes
e5a97c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Mathlib
import OAI.Combinatorics.MatchingCount.Sampling.TreeOptionsMaps
import OAI.Combinatorics.MatchingCount.Machines.TreeCertify
import OAI.Combinatorics.MatchingCount.Machines.TreeColorRaw
import OAI.Combinatorics.MatchingCount.Graphs.TreeSwitchRaw

namespace OAI

namespace MatchingFPRAS.TreeTyped.SparseProgram
open TreeTyped ExecutableOptions

variable {E V C : Type} [Codec E] [Codec V] [Codec C] [LinearOrder E] [LinearOrder V]
attribute [local instance] Transport.Sparse.finsetOrder
abbrev TableInput (C E V : Type) := (C×(Finset V×Finset E))×Finset E

def colorTable (p : C×(E×V)→Bool) (q : C×(E×E)→Bool) (x : TableInput C E V) : Options (Finset E) :=
  neighbors ((x.2,(certify p ((x.1.1,x.1.2.1),colorRaw q ((x.1.1,x.1.2.2),x.2))).erase x.2),x.1.2.1.card)
def colorTableRealizer {p : C×(E×V)→Bool} {q : C×(E×E)→Bool}
    (P : Realizer p) (Q : Realizer q)
    (Eq : Realizer (fun x : E×E=>decide (x.1=x.2)))
    (Le : Realizer (fun x : E×E=>decide (x.1≤x.2))) : Realizer (colorTable p q) :=
  (composition (pair (pair second (composition (pair second (composition
    (pair (pair first.fst first.snd.fst) (composition (pair (pair first.fst first.snd.snd) second)
      (colorRawRealizer Q Eq Le))) (certifyRealizer P))) (finsetErase (orderedSetEqual Eq))))
    (composition first.snd.fst finsetCard)) neighborsRealizer).congr (by
      intro x
      apply congrArg neighbors
      dsimp only [Function.comp_apply]
      congr 2
      ext b
      simp only [Finset.mem_erase])
lemma polynomial_colorTable {p : C×(E×V)→Bool} {q : C×(E×E)→Bool}
    {P : Realizer p} {Q : Realizer q}
    {Eq : Realizer (fun x : E×E=>decide (x.1=x.2))}
    {Le : Realizer (fun x : E×E=>decide (x.1≤x.2))}
    (hP : PolynomialTime P) (hQ : PolynomialTime Q) (hE : PolynomialTime Eq) (hL : PolynomialTime Le) :
    PolynomialTime (colorTableRealizer P Q Eq Le) := by
  unfold colorTableRealizer Realizer.fst Realizer.snd
  apply polynomial_congr
  have := polynomial_colorRaw hQ hE hL
  have := polynomial_certify hP
  have := polynomial_orderedSetEqual hE
  have := ExecutableOptions.polynomial_neighbors (A:=Finset E)
  tree_cost

def degree (n : ℕ) : ℕ := 8*(n/2).choose 2
def degreeRealizer : Realizer degree := by
  let half : Realizer (fun n : ℕ=>n/2) := composition (pair identity (constant 2)) quotient
  let chooseTwo : Realizer (fun n : ℕ=>n.choose 2) :=
    (composition (pair (composition (pair identity (composition (pair identity (constant 1)) subtract)) multiply)
      (constant 2)) quotient).congr (fun n=>Nat.choose_two_right n |>.symm)
  exact (composition (pair (constant 8) (composition half chooseTwo)) multiply).congr (fun _=>rfl)
lemma polynomial_degree : PolynomialTime degreeRealizer := by
  unfold degreeRealizer
  dsimp only
  apply polynomial_congr
  have := polynomial_quotient
  tree_cost

def switchTable (p : C×(E×V)→Bool) (q : C×(E×(V×V))→Bool) (x : TableInput C E V) : Options (Finset E) :=
  neighbors ((x.2,(certify p ((x.1.1,x.1.2.1),switchRaw q x)).erase x.2),degree x.1.2.1.card)
def switchTableRealizer {p : C×(E×V)→Bool} {q : C×(E×(V×V))→Bool}
    (P : Realizer p) (Q : Realizer q)
    (Eq : Realizer (fun x : E×E=>decide (x.1=x.2)))
    (Le : Realizer (fun x : E×E=>decide (x.1≤x.2)))
    (EqV : Realizer (fun x : V×V=>decide (x.1=x.2)))
    (LeV : Realizer (fun x : V×V=>decide (x.1≤x.2))) : Realizer (switchTable p q) :=
  (composition (pair (pair second (composition (pair second (composition
    (pair (pair first.fst first.snd.fst) (switchRawRealizer Q Eq Le EqV LeV)) (certifyRealizer P)))
    (finsetErase (orderedSetEqual Eq))))
    (composition (composition first.snd.fst finsetCard) degreeRealizer)) neighborsRealizer).congr (by
      intro x
      apply congrArg neighbors
      dsimp only [Function.comp_apply]
      congr 2
      ext b
      simp only [Finset.mem_erase])
lemma polynomial_switchTable {p : C×(E×V)→Bool} {q : C×(E×(V×V))→Bool}
    {P : Realizer p} {Q : Realizer q}
    {Eq : Realizer (fun x : E×E=>decide (x.1=x.2))}
    {Le : Realizer (fun x : E×E=>decide (x.1≤x.2))}
    {EqV : Realizer (fun x : V×V=>decide (x.1=x.2))}
    {LeV : Realizer (fun x : V×V=>decide (x.1≤x.2))}
    (hP : PolynomialTime P) (hQ : PolynomialTime Q) (hE : PolynomialTime Eq) (hL : PolynomialTime Le)
    (hV : PolynomialTime EqV) (hlV : PolynomialTime LeV) :
    PolynomialTime (switchTableRealizer P Q Eq Le EqV LeV) := by
  unfold switchTableRealizer Realizer.fst Realizer.snd
  apply polynomial_congr
  have := polynomial_switchRaw hQ hE hL hV hlV
  have := polynomial_certify hP
  have := polynomial_orderedSetEqual hE
  have := polynomial_degree
  have := ExecutableOptions.polynomial_neighbors (A:=Finset E)
  tree_cost

variable [Fintype E] [Fintype V] (ends : E→Sym2 V)
attribute [local instance] Transport.Sparse.pmOrder
variable (hloop : ∀ e,¬(ends e).IsDiag) (hpar : ∀ e,(Finset.univ.filter (fun f=>ends f=ends e)).card≤2)
omit [Codec E] [Codec V] [Codec C] in
lemma colorTable_eq (p : C×(E×V)→Bool) (q : C×(E×E)→Bool) (c : C)
    (hp : ∀ e v,p (c,(e,v))=decide (v∈ends e)) (hq : ∀ e f,q (c,(e,f))=decide (ends e=ends f))
    (a : Transport.PM (fun e v=>v∈ends e)) :
    colorTable p q ((c,(Finset.univ,Finset.univ)),a.val)=repr Subtype.val (Transport.Sparse.colorTable ends hloop hpar a) := by
  unfold colorTable
  have hraw : colorRaw q ((c,Finset.univ),a.val)=Transport.Sparse.colorRaw ends a.val := by
    simp only [colorRaw,Transport.Sparse.colorRaw,hq,decide_eq_true_eq]
  rw [hraw,certify_eq ends p c hp,Finset.card_univ]
  have he:=Finset.map_erase (pmEmbedding ends).toEmbedding (Transport.Sparse.colors ends a) a
  have hn:=neighbors_repr_map (pmEmbedding ends) a ((Transport.Sparse.colors ends a).erase a)
    (Fintype.card V) (Transport.Sparse.color_degree ends hloop hpar a)
  rw [he] at hn
  exact hn
omit [Codec E] [Codec V] [Codec C] in
lemma switchTable_eq (p : C×(E×V)→Bool) (q : C×(E×(V×V))→Bool) (c : C)
    (hp : ∀ e v,p (c,(e,v))=decide (v∈ends e)) (hq : ∀ e v w,q (c,(e,(v,w)))=decide (ends e=s(v,w)))
    (a : Transport.PM (fun e v=>v∈ends e)) :
    switchTable p q ((c,(Finset.univ,Finset.univ)),a.val)=repr Subtype.val (Transport.Sparse.switchTable ends hloop hpar a) := by
  unfold switchTable
  rw [switchRaw_eq ends q c hq,certify_eq ends p c hp,Finset.card_univ]
  have he:=Finset.map_erase (pmEmbedding ends).toEmbedding (Transport.Sparse.switches ends a) a
  have hn:=neighbors_repr_map (pmEmbedding ends) a ((Transport.Sparse.switches ends a).erase a)
    (degree (Fintype.card V)) (Transport.Sparse.switch_degree ends hloop hpar a)
  rw [he] at hn
  exact hn
end MatchingFPRAS.TreeTyped.SparseProgram

end OAI