cfahlgren1/math / lean /ComparatorChallenges /AlgorithmicThinTrees.lean
cfahlgren1's picture
download
raw
6.41 kB
9
structure MultiGraph (n m : ℕ) where
body · 3 lines
  left : Fin m → Fin n
  right : Fin m → Fin n
  loopless : ∀ e, left e ≠ right e
16
def cut {n m : ℕ} (G : MultiGraph n m) (T : Finset (Fin m))
    (S : Finset (Fin n)) : Finset (Fin m) :=
body · 2 lines
  T.filter fun e => (G.left e ∈ S ∧ G.right e ∉ S) ∨
    (G.right e ∈ S ∧ G.left e ∉ S)
21
def Connected {n m : ℕ} (G : MultiGraph n m) (T : Finset (Fin m)) : Prop :=
body · 1 line
  ∀ S : Finset (Fin n), S.Nonempty → S ≠ Finset.univ → (G.cut T S).Nonempty
24
def SpanningTree {n m : ℕ} (G : MultiGraph n m) (T : Finset (Fin m)) : Prop :=
body · 1 line
  G.Connected T ∧ ∀ e ∈ T, ¬ G.Connected (T.erase e)
27
def EdgeConnected {n m : ℕ} (G : MultiGraph n m) (k : ℕ) : Prop :=
body · 2 lines
  ∀ S : Finset (Fin n), S.Nonempty → S ≠ Finset.univ →
    k ≤ (G.cut Finset.univ S).card
36
def frame : List Bool → List Bool
body · 2 lines
  | [] => [true]
  | b :: bs => false :: b :: frame bs
40
def encodeNat (n : ℕ) : List Bool := frame (Computability.encodeNat n)
42
inductive StackAction where
body · 4 lines
  | stay
  | pop
  | push (bit : Bool)
  deriving DecidableEq
48
def StackAction.apply : StackAction → List Bool → List Bool
body · 3 lines
  | .stay, xs => xs
  | .pop, xs => xs.tail
  | .push b, xs => b :: xs
53
structure Instruction (k q : ℕ) where
body · 2 lines
  next : Option (Fin q)
  actions : Fin k → StackAction
57
def Instruction.halt (k q : ℕ) : Instruction k q := ⟨none, fun _ => .stay⟩
59
structure Machine where
body · 6 lines
  stackCount : ℕ
  states : ℕ
  initial : Fin states
  inputStack : Fin stackCount
  outputStack : Fin stackCount
  table : List (Instruction stackCount states)
67
structure Config (k q : ℕ) where
body · 2 lines
  state : Option (Fin q)
  «stacks» : Fin k → List Bool
71
def topCode : List Bool → ℕ
body · 3 lines
  | [] => 0
  | false :: _ => 1
  | true :: _ => 2
76
def headsCode : (k : ℕ) → (Fin k → List Bool) → ℕ
body · 2 lines
  | 0, _ => 0
  | k + 1, s => topCode (s 0) + 3 * headsCode k (fun j => s j.succ)
80
def Machine.step (M : Machine) (c : Config M.stackCount M.states) :
    Config M.stackCount M.states :=
body · 7 lines
  match c.state with
  | none => c
  | some q =>
    let instruction := M.table.getD
      (3 ^ M.stackCount * q.val + headsCode M.stackCount c.stacks)
      (Instruction.halt M.stackCount M.states)
    ⟨instruction.next, fun j => (instruction.actions j).apply (c.stacks j)⟩
90
def Machine.init (M : Machine) (input : List Bool) : Config M.stackCount M.states :=
body · 1 line
  ⟨some M.initial, fun j => if j = M.inputStack then input else []⟩
93
def Machine.run (M : Machine) (input : List Bool) (fuel : ℕ) :
    Config M.stackCount M.states := (M.step^[fuel]) (M.init input)
101
structure ExplicitInput where
body · 4 lines
  n : ℕ
  m : ℕ
  k : ℕ
  graph : MultiGraph n m
107
structure BinaryInput where
body · 8 lines
  n : ℕ
  p : ℕ
  k : ℕ
  left : Fin p → Fin n
  right : Fin p → Fin n
  ordered : ∀ i, left i < right i
  distinct : Function.Injective (fun i => (left i, right i))
  multiplicity : Fin p → ℕ
117
abbrev BinaryInput.Edge (x : BinaryInput) := Σ i : Fin x.p, Fin (x.multiplicity i)
119
noncomputable def BinaryInput.edgeEquiv (x : BinaryInput) :
    x.Edge ≃ Fin (Fintype.card x.Edge) := Fintype.equivFin x.Edge
122
noncomputable def BinaryInput.graph (x : BinaryInput) :
    MultiGraph x.n (Fintype.card x.Edge) where
body · 3 lines
  left e := x.left (x.edgeEquiv.symm e).1
  right e := x.right (x.edgeEquiv.symm e).1
  loopless e := ne_of_lt (x.ordered (x.edgeEquiv.symm e).1)
128
inductive Input where
body · 2 lines
  | explicit (x : ExplicitInput)
  | binary (x : BinaryInput)
132
def ExplicitInput.encode (x : ExplicitInput) : List Bool :=
body · 4 lines
  CurrentKS.encodeNat x.n ++ CurrentKS.encodeNat x.m ++ CurrentKS.encodeNat x.k ++
    (List.ofFn fun i : Fin x.m =>
      CurrentKS.encodeNat (x.graph.left i).val ++
      CurrentKS.encodeNat (x.graph.right i).val).flatten
138
def BinaryInput.encode (x : BinaryInput) : List Bool :=
body · 4 lines
  CurrentKS.encodeNat x.n ++ CurrentKS.encodeNat x.p ++ CurrentKS.encodeNat x.k ++
    (List.ofFn fun i : Fin x.p =>
      CurrentKS.encodeNat (x.left i).val ++ CurrentKS.encodeNat (x.right i).val ++
      CurrentKS.encodeNat (x.multiplicity i)).flatten
144
def Input.encode : Input → List Bool
body · 2 lines
  | .explicit x => false :: x.encode
  | .binary x => true :: x.encode
148
def Input.length (x : Input) : ℕ := x.encode.length
150
def Input.n : Input → ℕ
body · 2 lines
  | .explicit x => x.n
  | .binary x => x.n
154
def Valid : Input → Prop
body · 2 lines
  | .explicit x => 1 ≤ x.n ∧ 1 ≤ x.k ∧ x.graph.EdgeConnected x.k
  | .binary x => 1 ≤ x.n ∧ 1 ≤ x.k ∧ x.graph.EdgeConnected x.k
158
def ThinTree {n m : ℕ} (C : ℝ) (k : ℕ) (G : MultiGraph n m)
    (T : Finset (Fin m)) : Prop :=
body · 2 lines
  G.SpanningTree T ∧ ∀ S : Finset (Fin n), S.Nonempty → S ≠ Finset.univ →
    ((G.cut T S).card : ℝ) ≤ C / (k : ℝ) * (G.cut Finset.univ S).card
163
def ExplicitInput.encodeEdges (x : ExplicitInput) (edges : List (Fin x.m)) : List Bool :=
body · 1 line
  edges.flatMap (fun e => CurrentKS.encodeNat e.val)
166
def BinaryInput.encodeEdges (x : BinaryInput) (edges : List x.Edge) : List Bool :=
body · 1 line
  edges.flatMap (fun e => CurrentKS.encodeNat e.1.val ++ CurrentKS.encodeNat e.2.val)
169
noncomputable def BinaryInput.selected (x : BinaryInput) (edges : List x.Edge) :
    Finset (Fin (Fintype.card x.Edge)) := edges.toFinset.map x.edgeEquiv.toEmbedding
172
def TreeOutput (C : ℝ) : Input → List Bool → Prop
body · 4 lines
  | .explicit x, bits => ∃ edges : List (Fin x.m), edges.Nodup ∧
      bits = x.encodeEdges edges ∧ ThinTree C x.k x.graph edges.toFinset
  | .binary x, bits => ∃ edges : List x.Edge, edges.Nodup ∧
      bits = x.encodeEdges edges ∧ ThinTree C x.k x.graph (x.selected edges)
178
def GoodOutput (C : ℝ) (x : Input) (bits : List Bool) : Prop :=
body · 1 line
  TreeOutput C x bits ∧ (x.n = 1 → bits = [])
181
def AlgorithmicStrongThinTrees : Prop :=
body · 4 lines
  ∃ C : ℝ, 0 < C ∧ ∃ M : CurrentKS.Machine, ∃ a degree : ℕ, 0 < a ∧
    ∀ x : Input, Valid x →
      let result := M.run x.encode (a * (x.length + 1) ^ degree)
      result.state = none ∧ GoodOutput C x (result.stacks M.outputStack)
191
theorem algorithmic_strong_thin_trees : AlgorithmicStrongThinTrees := by
proof · 1 line
  sorry

Xet Storage Details

Size:
6.41 kB
·
Xet hash:
2ee29f7654fb532ad3c54bf8dc42bf0d48d0fb06e256085cbe30409ff946a57a

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.