An executable unary-code transducer for Definition 10
DefinitionNQueensComp_NPC_CodeReduction13n-queensparsimonious-reductionpolynomial-time
The function decodes a Problem 3 bit string, re-encodes it to check the syntax, and checks the duplicate-free lists and validity conditions. On valid inputs it applies Definition 10 and encodes the new board size and the lexicographically sorted set of placed queens. On all other inputs it returns the empty bit string.
Definition code
import Definitions.Def_NQueensComp_NPC_Codes
import Definitions.Def_NQueensComp_NPC_Reductions
namespace NQueensComp.NPC
/-- Group the successive coordinates of an encoded queen list into pairs. -/
def pairQueenCoords : List ℕ → Option (List Queen)
| [] => some []
| a :: b :: rest => (pairQueenCoords rest).map ((a, b) :: ·)
| [_] => none
/-- Read the common board-size and queen-list prefix of a Problem 2 or 3 code. -/
def readQueenPrefix (x : List Bool) : Option (ℕ × List Queen × List Bool) := do
let (n, rest) := CookLevin.decodeNatFuel x.length x
let (coords, rest) := CookLevin.decodeListNatFuel rest.length rest
let queens ← pairQueenCoords coords
pure (n, queens, rest)
/-- Read the board, the listed queens and the column and row lists of a Problem 3 code. -/
def readCode3 (x : List Bool) : Option Code3 := do
let (n, queens, rest) ← readQueenPrefix x
let (columns, rest) := CookLevin.decodeListNatFuel rest.length rest
let (rows, _) := CookLevin.decodeListNatFuel rest.length rest
pure ⟨n, queens, columns, rows⟩
/-- The instance obtained by interpreting the three lists as finite sets. -/
def Code3.toInstance (L : Code3) : Inst3 :=
⟨L.n, L.P.toFinset, L.C.toFinset, L.R.toFinset⟩
instance codeReduction13ValidDecidable (M : Inst3) : Decidable M.Valid := by
unfold Inst3.Valid Inst2.Valid DistinctRowsCols FitsOn
infer_instance
instance codeReduction13SelfAttackingDecidable (P : Finset Queen) :
Decidable (SelfAttacking P) := by
unfold SelfAttacking Attacks
infer_instance
/-- Construct the indexed queens from the sorted elements of a finite set of columns or rows. -/
def indexedLineQueens (s : Finset ℕ) (place : ℕ → ℕ → Queen) : Finset Queen :=
Finset.univ.image fun i : Fin s.card =>
place i.val (s.sort[i.val]'(by rw [Finset.length_sort]; exact i.isLt))
/-- The finite construction of Definition 10, using executable tests and sorted lists. -/
def def10Computable (M : Inst3) : Inst2 :=
if SelfAttacking M.P then ⟨M.n, M.P⟩ else
let n' := def10_nprime M.n
let columns := def10_C' M
let rows := def10_R' M
let qc := indexedLineQueens columns fun i c => (i, 2 * c)
let qr := indexedLineQueens rows fun i r => (2 * r, i)
⟨19 * n' + columns.card,
def10_Q3 M ∪ shiftSet qc (n' • ((19, 8) : Queen)) ∪
shiftSet qr (n' • ((17, 19) : Queen)) ∪ M.P⟩
/-- List the placed queens once each, in lexicographic order. -/
def Inst2.toCode2 (M : Inst2) : Code2 :=
⟨M.n, ((M.P.image (toLex : Queen → ℕ ×ₗ ℕ)).sort).map ofLex⟩
/-- Decode and validate the input, apply Definition 10, then encode its output.
Invalid inputs go to the empty bit string, which is not a Problem 2 code. -/
def reduce13Bits (x : List Bool) : List Bool :=
match readCode3 x with
| none => []
| some L =>
if L.enc = x ∧ L.P.Nodup ∧ L.C.Nodup ∧ L.R.Nodup ∧ L.toInstance.Valid then
(def10Computable L.toInstance).toCode2.enc
else []
end NQueensComp.NPC
Source
Gent, Jefferson & Nightingale, Complexity of n-Queens Completion, https://doi.org/10.1613/jair.5512, pp. 819–820, Definition 10 (pp. 824–825), Theorem 13 (p. 826), and the polynomial-time observation in Theorem 37 (p. 836).