Kemeny, Young and Dodgson winners, electoral control problems, graph membership problems, and their unary codes
DefinitionVHC_Electionscomputational-social-choicepolynomial-hierarchy
Definition code
import Mathlib
import Definitions.Def_CookPvsNP_defs
import Definitions.Def_BudgetCoreExchange_Sigma2P
import Definitions.Def_BudgetCoreExchange_QSAT2
/-!
Elections, graph problems and electoral control problems, with unary codes.
Candidates and vertices are natural numbers. A vote is a list of candidates,
most preferred first.
-/
namespace VHC
/-! ### Preferences and elections -/
/-- Voter `v` ranks `a` strictly above `b` (both must be listed). -/
def Prefers (v : List ℕ) (a b : ℕ) : Prop :=
a ∈ v ∧ b ∈ v ∧ v.idxOf a < v.idxOf b
instance (v : List ℕ) (a b : ℕ) : Decidable (Prefers v a b) := by
unfold Prefers; infer_instance
/-- Number of voters ranking `a` above `b`. -/
def margin (V : List (List ℕ)) (a b : ℕ) : ℕ :=
(V.filter (fun v => decide (Prefers v a b))).length
/-- A vote restricted to the candidate set `C`. -/
def restrict (C : Finset ℕ) (v : List ℕ) : List ℕ := v.filter (· ∈ C)
/-- `r` is a strict linear order (ranking) of exactly the candidates in `C`. -/
def IsRanking (C : Finset ℕ) (r : List ℕ) : Prop := r.Nodup ∧ r.toFinset = C
/-- Kemeny score of ranking `r`: the total number of (voter, pair) disagreements. -/
def kemenyScore (V : List (List ℕ)) : List ℕ → ℕ
| [] => 0
| a :: t => (t.map (fun b => margin V b a)).sum + kemenyScore V t
/-- `c` is a Kemeny winner of `(C, V)`: `c` tops some ranking of `C` of minimum Kemeny
score. Votes may list candidates outside `C` (ignored) or omit candidates of `C`
(unlisted pairs contribute nothing, as in the Kemeny′ variant). -/
def KemenyWinner (C : Finset ℕ) (V : List (List ℕ)) (c : ℕ) : Prop :=
∃ r, IsRanking C r ∧ r.head? = some c ∧
∀ r', IsRanking C r' → kemenyScore V r ≤ kemenyScore V r'
/-- `c` is a weak Condorcet winner of `(C, V)`. -/
def WeakCondorcet (C : Finset ℕ) (V : List (List ℕ)) (c : ℕ) : Prop :=
c ∈ C ∧ ∀ d ∈ C, d ≠ c → margin V d c ≤ margin V c d
/-- `c` is a (strict) Condorcet winner of `(C, V)`. -/
def Condorcet (C : Finset ℕ) (V : List (List ℕ)) (c : ℕ) : Prop :=
c ∈ C ∧ ∀ d ∈ C, d ≠ c → margin V d c < margin V c d
/-- `s` is a Young score of `c`: the largest number of voters (a sub-multiset of `V`)
among whom `c` is a weak Condorcet winner. -/
def IsYoungScore (C : Finset ℕ) (V : List (List ℕ)) (c s : ℕ) : Prop :=
(∃ V', V'.Sublist V ∧ V'.length = s ∧ WeakCondorcet C V' c) ∧
∀ V', V'.Sublist V → WeakCondorcet C V' c → V'.length ≤ s
/-- `c` is a Young winner of `(C, V)`: `c ∈ C` and its Young score is maximum. -/
def YoungWinner (C : Finset ℕ) (V : List (List ℕ)) (c : ℕ) : Prop :=
c ∈ C ∧ ∃ s, IsYoungScore C V c s ∧ ∀ d ∈ C, ∀ t, IsYoungScore C V d t → t ≤ s
/-- Swap distance between two rankings of the same candidates (number of pairs on
which they disagree, = minimum number of adjacent swaps). -/
def swapDist : List ℕ → List ℕ → ℕ
| [], _ => 0
| a :: t, w => (t.filter (fun b => decide (Prefers w b a))).length + swapDist t w
/-- `V'` is obtained from `V` by reordering each vote, at total swap cost `s`. -/
def SwapsTo (V V' : List (List ℕ)) (s : ℕ) : Prop :=
V'.length = V.length ∧
(∀ i (h : i < V.length) (h' : i < V'.length), (V'.get ⟨i, h'⟩).Perm (V.get ⟨i, h⟩)) ∧
(List.zipWith swapDist V V').sum = s
/-- `c` is a Dodgson winner of `(C, V)` (votes restricted to `C`): `c` can be made a
Condorcet winner with `s` adjacent swaps, and no candidate can with fewer. -/
def DodgsonWinner (C : Finset ℕ) (V : List (List ℕ)) (c : ℕ) : Prop :=
let W := V.map (restrict C)
∃ s, (∃ W', SwapsTo W W' s ∧ Condorcet C W' c) ∧
∀ d ∈ C, ∀ W' t, SwapsTo W W' t → Condorcet C W' d → s ≤ t
/-! ### Graphs: vertex set `S`, edge list `E` (edges with an endpoint outside `S` are absent) -/
def IsVertexCover (S : Finset ℕ) (E : List (ℕ × ℕ)) (X : Finset ℕ) : Prop :=
X ⊆ S ∧ ∀ e ∈ E, e.1 ∈ S → e.2 ∈ S → e.1 ∈ X ∨ e.2 ∈ X
/-- `v` belongs to some minimum vertex cover of `(S, E)`. -/
def InMinVertexCover (S : Finset ℕ) (E : List (ℕ × ℕ)) (v : ℕ) : Prop :=
∃ X, IsVertexCover S E X ∧ v ∈ X ∧ ∀ Y, IsVertexCover S E Y → X.card ≤ Y.card
def IsIndependent (S : Finset ℕ) (E : List (ℕ × ℕ)) (I : Finset ℕ) : Prop :=
I ⊆ S ∧ ∀ e ∈ E, ¬ (e.1 ∈ I ∧ e.2 ∈ I)
/-- `v` belongs to some maximum independent set of `(S, E)`. -/
def InMaxIndependent (S : Finset ℕ) (E : List (ℕ × ℕ)) (v : ℕ) : Prop :=
∃ I, IsIndependent S E I ∧ v ∈ I ∧ ∀ J, IsIndependent S E J → J.card ≤ I.card
def Loopless (E : List (ℕ × ℕ)) : Prop := ∀ e ∈ E, e.1 ≠ e.2
/-- Vertex-Cover-Member-Add. Instance `(V, V', E, k, v̂)`: graph on `V ∪ V'`,
addable vertices `V'`. -/
def VCMemberAdd (inst : List ℕ × List ℕ × List (ℕ × ℕ) × ℕ × ℕ) : Prop :=
let (V, V', E, k, v) := inst
Loopless E ∧ v ∈ V ∧
∃ W : Finset ℕ, W ⊆ V'.toFinset ∧ W.card ≤ k ∧ InMinVertexCover (V.toFinset ∪ W) E v
/-- Independent-Set-Member-Delete. Instance `(V, E, k, v̂)`. -/
def ISMemberDelete (inst : List ℕ × List (ℕ × ℕ) × ℕ × ℕ) : Prop :=
let (V, E, k, v) := inst
Loopless E ∧ v ∈ V ∧
∃ W : Finset ℕ, W ⊆ V.toFinset ∧ W.card ≤ k ∧ InMaxIndependent (V.toFinset \ W) E v
/-- Generalized-Node-Deletion. Instance `(V, E, k, ℓ)`: can deleting at most `k` vertices
leave no clique on `ℓ + 1` vertices? -/
def GNodeDeletion (inst : List ℕ × List (ℕ × ℕ) × ℕ × ℕ) : Prop :=
let (V, E, k, l) := inst
Loopless E ∧
∃ W : Finset ℕ, W ⊆ V.toFinset ∧ W.card ≤ k ∧
∀ K ⊆ V.toFinset \ W, (∀ a ∈ K, ∀ b ∈ K, a ≠ b → (a, b) ∈ E ∨ (b, a) ∈ E) →
K.card ≤ l
/-! ### Control problems. A vote must rank all candidates (except for Kemeny′). -/
/-- Every vote is a ranking of exactly `C`. -/
def CompleteVotes (C : Finset ℕ) (V : List (List ℕ)) : Prop := ∀ v ∈ V, IsRanking C v
/-- `E`-CCAC. Instance `(C, D, V, k, p)`: registered `C`, unregistered `D`. -/
def CCAC (W : Finset ℕ → List (List ℕ) → ℕ → Prop)
(inst : List ℕ × List ℕ × List (List ℕ) × ℕ × ℕ) : Prop :=
let (C, D, V, k, p) := inst
Disjoint C.toFinset D.toFinset ∧ p ∈ C ∧ CompleteVotes (C.toFinset ∪ D.toFinset) V ∧
∃ D' : Finset ℕ, D' ⊆ D.toFinset ∧ D'.card ≤ k ∧ W (C.toFinset ∪ D') V p
/-- `E`-CCDC∗. Instance `(C, D, V, k, p)`: deletable candidates `D ⊆ C`. -/
def CCDCStar (W : Finset ℕ → List (List ℕ) → ℕ → Prop)
(inst : List ℕ × List ℕ × List (List ℕ) × ℕ × ℕ) : Prop :=
let (C, D, V, k, p) := inst
D ⊆ C ∧ p ∈ C ∧ CompleteVotes C.toFinset V ∧
∃ D' : Finset ℕ, D' ⊆ D.toFinset ∧ D'.card ≤ k ∧ W (C.toFinset \ D') V p
/-- `E`-CCDC. Instance `(C, V, k, p)`: any candidates may be deleted. -/
def CCDC (W : Finset ℕ → List (List ℕ) → ℕ → Prop)
(inst : List ℕ × List (List ℕ) × ℕ × ℕ) : Prop :=
let (C, V, k, p) := inst
p ∈ C ∧ CompleteVotes C.toFinset V ∧
∃ D' : Finset ℕ, D' ⊆ C.toFinset ∧ D'.card ≤ k ∧ W (C.toFinset \ D') V p
/-- `E`-CCAV. Instance `(C, V, U, k, p)`: registered voters `V`, unregistered `U`.
`complete` says whether votes must rank all candidates. -/
def CCAV (complete : Bool) (W : Finset ℕ → List (List ℕ) → ℕ → Prop)
(inst : List ℕ × List (List ℕ) × List (List ℕ) × ℕ × ℕ) : Prop :=
let (C, V, U, k, p) := inst
p ∈ C ∧
(if complete then CompleteVotes C.toFinset (V ++ U)
else ∀ v ∈ V ++ U, v.Nodup ∧ v.toFinset ⊆ C.toFinset) ∧
∃ U', U'.Sublist U ∧ U'.length ≤ k ∧ W C.toFinset (V ++ U') p
/-- `E`-CCDV. Instance `(C, V, k, p)`. -/
def CCDV (W : Finset ℕ → List (List ℕ) → ℕ → Prop)
(inst : List ℕ × List (List ℕ) × ℕ × ℕ) : Prop :=
let (C, V, k, p) := inst
p ∈ C ∧ CompleteVotes C.toFinset V ∧
∃ V', V'.Sublist V ∧ V.length ≤ V'.length + k ∧ W C.toFinset V' p
/-! ### Codes and languages -/
/-- Alphabet for instance codes: unary digits, a number terminator, list cells. -/
inductive Sym where
| one
| stop
| cons
| nil
deriving DecidableEq
instance : Fintype Sym where
elems := {Sym.one, Sym.stop, Sym.cons, Sym.nil}
complete := by intro x; cases x <;> simp
instance : Nonempty Sym := ⟨Sym.one⟩
/-- A coding of values as strings over `Sym` (the instances below are prefix-free). -/
class Code (α : Type) where
code : α → List Sym
/-- A number `n` is coded in unary as `oneⁿ stop`. -/
instance : Code ℕ := ⟨fun n => List.replicate n Sym.one ++ [Sym.stop]⟩
/-- A list is coded as `cons a₁ ⋯ cons aₖ nil`. -/
instance {α : Type} [Code α] : Code (List α) :=
⟨fun l => l.flatMap (fun a => Sym.cons :: Code.code a) ++ [Sym.nil]⟩
/-- A pair is coded by concatenating the codes of its components. -/
instance {α β : Type} [Code α] [Code β] : Code (α × β) :=
⟨fun x => Code.code x.1 ++ Code.code x.2⟩
/-- The language of codes of instances satisfying `P`. -/
def langOf {α : Type} [Code α] (P : α → Prop) : CookPvsNP.Lang Sym :=
{ s | ∃ x, P x ∧ s = Code.code x }
/-- `L` is `Σᵖ₂`-hard via QSAT₂: QSAT₂ reduces to `L` in polynomial time (Cook's model). -/
def QSAT2Hard {S : Type} (L : CookPvsNP.Lang S) : Prop :=
CookPvsNP.PolyReducible BudgetCoreExchange.QSAT2 L
/-- The problem `P` is in `Σᵖ₂` and is `Σᵖ₂`-hard via QSAT₂ (so, given that QSAT₂ is
`Σᵖ₂`-complete, `P` is `Σᵖ₂`-complete). -/
def Sigma2PQ {α : Type} [Code α] (P : α → Prop) : Prop :=
langOf P ∈ BudgetCoreExchange.Sigma2P Sym ∧ QSAT2Hard (langOf P)
end VHC