pp. 367–368 — the reduction map of THEOREM 2: formula code graph code of with threshold
DefinitionEdgeDomSets_NPC_ReductionMapThe polynomial-time reduction of THEOREM 2 (Yannakakis–Gavril, pp. 367–368), written as an explicit map on binary strings.
A formula is handled in its numeric form: a list of clauses, each a list of literal numbers, where the literal is and is (the encoding CookLevin.formulaN). Let be the number of literal occurrences, the number of clauses, and call a clause replaced when it has three literals, all of the same sign; let be the number of replaced clauses. The graph of the proof is numbered on vertices:
- occurrence (in reading order) gives the literal vertex , its pendant copy and its variable-side vertex ;
- clause gives and ;
- clause gives a copy of the 14-vertex gadget of Fig. 3 on .
The edges are: – and – for every occurrence; – (positive literal) or – (negated literal) when clause of the occurrence is not replaced, and otherwise –(corner , or of the -th copy of , by the position of the literal in its clause); – for every clause that is not replaced, and the 18 edges of in the -th copy for every replaced clause; and – whenever occurrence is and occurrence is .
The map decodes the input string as a formula. If the string is exactly the code of a formula whose clauses all have between one and three literals and every occurring variable occurs twice positively and once negated, the output is the graph code (vertex count, threshold, row-major adjacency matrix) of the numbered with threshold
every other string is sent to the empty string, which is not an instance code.
Formalization Note edges lists the edges in the order above; edgeBit marks the adjacency-matrix entries of both orientations; gcode is the published NPGraph.Graph code. For a SAT-3-restricted formula , so , the threshold of the corrected reading of p. 368. The vertices of unused gadget copies and the removed , of replaced clauses are isolated, exactly as in the platform's Gstar.
import Mathlib
import Definitions.Def_EdgeDomSets_NPC_Construction
namespace EdgeDomSets.NPC.Red
/-! Numeric form of the graph `G*` of the proof of THEOREM 2 (pp. 367–368), on a formula
given as lists of literal numbers (`Neg v ↦ 2v`, `Pos v ↦ 2v+1`, as in `CookLevin.formulaN`). -/
/-- Literal occurrences `(clause index, position in clause, literal number)`, in order. -/
def occs (nss : List (List ℕ)) : List (ℕ × ℕ × ℕ) :=
nss.zipIdx.flatMap fun p => p.1.zipIdx.map fun q => (p.2, q.2, q.1)
/-- A clause is replaced by a copy of `H` iff it has three literals, all of one sign. -/
def rep (ns : List ℕ) : Bool :=
ns.length == 3 && ((ns.countP fun l => l % 2 == 1) == 0 || (ns.countP fun l => l % 2 == 1) == 3)
/-- Number of occurrences `L`, clauses `P`, replaced clauses `Q`. -/
def numL (nss : List (List ℕ)) : ℕ := (occs nss).length
def numP (nss : List (List ℕ)) : ℕ := nss.length
def numQ (nss : List (List ℕ)) : ℕ := nss.countP rep
/-- Number of vertices: `3L` occurrence vertices, `2P` clause vertices, `14P` gadget vertices. -/
def order (nss : List (List ℕ)) : ℕ := 3 * numL nss + 16 * numP nss
/-- First vertex of the `j`-th copy of `H`. -/
def hbase (nss : List (List ℕ)) (j : ℕ) : ℕ := 3 * numL nss + 2 * numP nss + 14 * j
/-- The vertex the literal vertex of occurrence `o` is attached to. -/
def attach (nss : List (List ℕ)) (o : ℕ × ℕ × ℕ) : ℕ :=
if rep (nss.getD o.1 []) then hbase nss o.1 + o.2.1
else 3 * numL nss + 2 * o.1 + (if o.2.2 % 2 = 1 then 0 else 1)
/-- Occurrence edges: literal–pendant, literal–variable side, literal–clause side. -/
def edges1 (nss : List (List ℕ)) : List (ℕ × ℕ) :=
(List.range (numL nss)).flatMap fun t =>
[(3 * t, 3 * t + 1), (3 * t, 3 * t + 2), (3 * t, attach nss ((occs nss).getD t (0, 0, 0)))]
/-- Clause edges: `d_j – d'_j`, or the 18 edges of the `j`-th copy of `H`. -/
def edges2 (nss : List (List ℕ)) : List (ℕ × ℕ) :=
(List.range (numP nss)).flatMap fun j =>
if rep (nss.getD j []) then HEdges.map fun e => (hbase nss j + e.1.val, hbase nss j + e.2.val)
else [(3 * numL nss + 2 * j, 3 * numL nss + 2 * j + 1)]
/-- Variable edges: positive occurrence of `x_v` – negated occurrence of `x_v`. -/
def edges3 (nss : List (List ℕ)) : List (ℕ × ℕ) :=
(List.range (numL nss)).flatMap fun t => (List.range (numL nss)).flatMap fun t' =>
if ((occs nss).getD t (0, 0, 0)).2.2 % 2 = 1 ∧
((occs nss).getD t' (0, 0, 0)).2.2 + 1 = ((occs nss).getD t (0, 0, 0)).2.2
then [(3 * t + 2, 3 * t' + 2)] else []
def edges (nss : List (List ℕ)) : List (ℕ × ℕ) := edges1 nss ++ edges2 nss ++ edges3 nss
/-- Adjacency bit at row-major index `i`. -/
def edgeBit (nss : List (List ℕ)) (i : ℕ) : Bool :=
(edges nss).any fun e => i == e.1 * order nss + e.2 || i == e.2 * order nss + e.1
/-- The graph code of `G*`. -/
def gcode (nss : List (List ℕ)) : NPGraph.Graph :=
⟨order nss, (List.range (order nss * order nss)).map (edgeBit nss)⟩
/-- The threshold `3n + 4q` (`= L + 4Q` for restricted formulas), at least `1`. -/
def kval (nss : List (List ℕ)) : ℕ := max 1 (numL nss + 4 * numQ nss)
/-- Positive / negated occurrence counts of variable `v`. -/
def cntPos (nss : List (List ℕ)) (v : ℕ) : ℕ := nss.flatten.countP fun l => l / 2 == v && l % 2 == 1
def cntNeg (nss : List (List ℕ)) (v : ℕ) : ℕ := nss.flatten.countP fun l => l / 2 == v && l % 2 == 0
/-- Accepted instances: nonempty clauses of at most three literals, every occurring variable
twice positive and once negated. -/
def validN (nss : List (List ℕ)) : Bool :=
nss.all (fun ns => 1 ≤ ns.length && ns.length ≤ 3) &&
nss.flatten.all fun l => cntPos nss (l / 2) == 2 && cntNeg nss (l / 2) == 1
/-- The reduction map: decode, check, and output the code of `G*` with threshold `kval`;
everything else goes to the empty string (a no-instance). -/
def redMap (x : List Bool) : List Bool :=
let nss := (CookLevin.decodeFormulaNFuel (x.length + 1) x).1
if CookLevin.encodeFormulaN nss = x ∧ validN nss = true then
NPGraph.encodeInstance (gcode nss) (kval nss)
else []
end EdgeDomSets.NPC.Red