introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
1.94 kB
import Mathlib
namespace OAI
universe u_1 u_2
namespace MatchingEntropy
structure LooplessGraph (V : Type u_1) (E : Type u_2) where
left : E β†’ V
right : E β†’ V
loopless : βˆ€ e, left e β‰  right e
namespace LooplessGraph
variable {V : Type u_1} {E : Type u_2} [Fintype V] [Fintype E] [DecidableEq V] [DecidableEq E]
def Incident (G : LooplessGraph V E) (v : V) (e : E) : Prop :=
G.left e=v ∨ G.right e=v
def IsPerfectMatching (G : LooplessGraph V E) (M : Finset E) : Prop :=
βˆ€ v, βˆƒ! e, e∈M ∧ G.Incident v e
abbrev Matching (G : LooplessGraph V E) := {M : Finset E // G.IsPerfectMatching M}
noncomputable instance matchingFintype (G : LooplessGraph V E) : Fintype G.Matching :=
Fintype.ofFinite _
end LooplessGraph
end MatchingEntropy
namespace BinaryMatching
abbrev Pair (n : β„•) := {ij : Fin n Γ— Fin n // ij.1 < ij.2}
def completeGraph (n : β„•) : MatchingEntropy.LooplessGraph (Fin n) (Pair n) where
left e := e.val.1
right e := e.val.2
loopless e := ne_of_lt e.property
structure Record where
left : β„•
right : β„•
multiplicity : β„•
deriving DecidableEq
structure Input where
n : β„•
records : List Record
valid : βˆ€ e∈records, e.left < e.right ∧ e.right < n
unique : (records.map (fun e => (e.left,e.right))).Nodup
def multiplicity (G : Input) (e : Pair G.n) : β„• :=
match G.records.find? (fun r => r.left=e.val.1.val && r.right=e.val.2.val) with
| none => 0
| some r => r.multiplicity
noncomputable def count (G : Input) : β„• :=
βˆ‘ M : (completeGraph G.n).Matching, ∏ e∈M.val, multiplicity G e
def encodeNat (n : β„•) : List Bool :=
List.replicate n.bits.length false ++ true :: n.bits
def encodeRecord (r : Record) : List Bool :=
encodeNat r.left ++ encodeNat r.right ++ encodeNat r.multiplicity
def encodeInput (G : Input) : List Bool :=
encodeNat G.n ++ encodeNat G.records.length ++ G.records.flatMap encodeRecord
end BinaryMatching
end OAI