While this submission is a draft, it cannot be used by other submissions.

2-SAT Is Decided in Time Linear in the Word Times the Number of Variables

Lax117284.TwoSatRunningTime · concepts/Lax117284/TwoSatRunningTime.lean · lax-117284

proven

Loading review…

Sign in with ORCID

Community review

Flags

Each flag is tied to a public ORCID identity and explains why this concept may be incorrect.

No flags have been submitted.

    Community review

    Flag this concept

    State precisely what appears incorrect. This explanation will be public under your ORCID name.

    No source line selected.

    Natural Language Statement

    Theorem

    There is one word RAM program and one constant cc such that, at every word length ww, given the bits of a binary word xx with c (∣x∣+1)≤2wc\,(|x|+1) \le 2^w, the program halts within c (∣x∣+1) (v+1)c\,(|x|+1)\,(v+1) instructions, where vv is the number of distinct variables of the formula xx encodes (and 00 if it encodes none), with output 11 if xx is in 2-SAT and 00 otherwise. Since a formula with mm clauses has at most 2m2m variables and at most ∣x∣|x|, the bound is at most c (∣x∣+1) (2m+1)c\,(|x|+1)\,(2m+1) and at most c (∣x∣+1)2c\,(|x|+1)^2.

    Concept map
    9 concepts
    100%
    Proven claimDefinitionThis conceptRelated conceptA → B: B builds on A
    Evidence

    Each proof establishes this claim relative to its assumptions.

    Lean source view on GitHub

    1import Lax117284.TwoSatCNF
    2import Lax808846.RamComputes
    3
    4/-!
    5---
    6title: 2-SAT Is Decided in Time Linear in the Word Times the Number of Variables
    7type: theorem
    8---
    9There is one word RAM program and one constant cc such that, at every word length ww, given
    10the bits of a binary word xx with c (∣x∣+1)≤2wc\,(|x|+1) \le 2^w, the program halts within
    11c (∣x∣+1) (v+1)c\,(|x|+1)\,(v+1) instructions, where vv is the number of distinct variables of the formula
    12xx encodes (and 00 if it encodes none), with output 11 if xx is in 2-SAT and 00 otherwise.
    13Since a formula with mm clauses has at most 2m2m variables and at most ∣x∣|x|, the bound is at
    14most c (∣x∣+1) (2m+1)c\,(|x|+1)\,(2m+1) and at most c (∣x∣+1)2c\,(|x|+1)^2.
    15
    16# Formalization Notes
    17
    18The word RAM of `lax-808846` reads words of natural numbers; a binary word is handed to it as
    19its list of zeros and ones, one bit an entry, so that the length of the input is the length of
    20the binary word and the machine has no advantage from packing bits into its words. The admissible
    21inputs are the lists of zeros and ones, all of them: a list that is not the encoding of a formula
    22is answered `0`, as it is not in the language. The output is `1` or `0`; the machine writes it
    23as a single entry.
    24
    25The bound is proved, not merely asserted, from the program: reading and decoding the word is
    26linear in its length; the implication graph has 2b2b nodes for bb the bound on the indices,
    27which is at most the length of the word because an index is written in unary, and at most two
    28edges per clause; each of the at most 2v2v searches costs a number of instructions linear in the
    29size of the graph, and skipping a variable that does not occur costs a constant. The strongly
    30connected components of Aspvall, Plass and Tarjan would bring the second factor down to a
    31constant; that algorithm is not formalized here.
    32
    33The fitting hypothesis c (∣x∣+1)≤2wc\,(|x|+1) \le 2^w is the "word is wide enough" condition written as an
    34inequality: every value the program manipulates is a count of nodes, edges, bits or steps, all
    35of them below c (∣x∣+1)c\,(|x|+1). The program is quantified before the word length, so it is one
    36algorithm at every word length, not a family.
    37-/
    38
    39namespace Lax117284.TwoSatRunningTime
    40
    41open Lax429075.CNF Lax429075.Encoding Lax434930.PolynomialTime Lax117284.TwoSatCNF
    42open Lax808846.Ram Lax808846.RamComputes
    43
    44/-- A binary word as a list of zeros and ones, the form in which the word RAM reads it. -/
    45def natBits (w : Word) : List ℕ := w.map fun b => if b then 1 else 0
    46
    47/-- A list of numbers as a binary word: which entries are nonzero. -/
    48def bitsOf (x : List ℕ) : Word := x.map fun n => decide (n ≠ 0)
    49
    50/-- The number of distinct variables of the formula a list of bits encodes, and `0` if it
    51encodes none. -/
    52def wordVarCount (x : List ℕ) : ℕ := ((decodeCNF (bitsOf x)).map varCount).getD 0
    53
    54open scoped Classical in
    55/-- **2-SAT is decided within `c · (|x| + 1) · (v + 1)` instructions**, `v` the number of
    56distinct variables, by one word RAM program at every word length that fits the word. -/
    57axiom decides : ∃ (prog : Program) (c : ℕ), ∀ w : ℕ,
    58 ComputesInTime w prog
    59 {x | (∀ v ∈ x, v ≤ 1) ∧ c * (x.length + 1) ≤ 2 ^ w}
    60 (fun x => if bitsOf x ∈ TwoSAT then [1] else [0])
    61 (fun x => c * (x.length + 1) * (wordVarCount x + 1))
    62
    63end Lax117284.TwoSatRunningTime
    64
    Show Proof
    Formalization Notes

    The word RAM of lax−808846lax-808846 reads words of natural numbers; a binary word is handed to it as its list of zeros and ones, one bit an entry, so that the length of the input is the length of the binary word and the machine has no advantage from packing bits into its words. The admissible inputs are the lists of zeros and ones, all of them: a list that is not the encoding of a formula is answered 00, as it is not in the language. The output is 11 or 00; the machine writes it as a single entry.

    The bound is proved, not merely asserted, from the program: reading and decoding the word is linear in its length; the implication graph has 2b2b nodes for bb the bound on the indices, which is at most the length of the word because an index is written in unary, and at most two edges per clause; each of the at most 2v2v searches costs a number of instructions linear in the size of the graph, and skipping a variable that does not occur costs a constant. The strongly connected components of Aspvall, Plass and Tarjan would bring the second factor down to a constant; that algorithm is not formalized here.

    The fitting hypothesis c (∣x∣+1)≤2wc\,(|x|+1) \le 2^w is the "word is wide enough" condition written as an inequality: every value the program manipulates is a count of nodes, edges, bits or steps, all of them below c (∣x∣+1)c\,(|x|+1). The program is quantified before the word length, so it is one algorithm at every word length, not a family.

    Builds on
    Used by

    none

    From Mathlib

    none

    Discussion

    Ask a question or add context. Endorsements and structured flags are kept in the review panel above.

    Loading discussion…