Documentation

CslibFrontier.Computability.Complexity.Problems.SubsetSum

Subset sum #

SubsetSum is the set of instances x : SubsetSumInstance of a list x.numbers of natural numbers, possibly with repetitions, and a target x.target such that some sub-multiset of x.numbers sums to x.target Sipser, Section 7.3. All numbers are encoded in binary (Turing.MultiTapeTM.encNat, digits least significant first); the NP-hardness proved here is for this encoding and is not strong NP-hardness. A certificate is a mask selecting entries of x.numbers (Cslib.Complexity.maskSelect), and the verifier adds the selected numbers in binary.

Sipser's reduction from 3SAT Sipser, Theorem 7.56 gives each of the n variables of a formula, numbered by first occurrence (Cslib.Logic.PL.CNF.distinctVars), and each of its m clauses a digit position. Each variable has two literal numbers, with digit one at the position of the variable and at the positions of the clauses containing the literal (ThreeSAT.literalDigits); each clause has two slack numbers with digit one at its position only. The target has digit one at every variable position and three at every clause position. At each position, all numbers together have at most five digits one, so no subfamily produces carries in any base above five (Cslib.Complexity.sum_sum_pow_eq_iff). We use base 8 rather than Sipser's base 10, so that digits are blocks of three bits; every number has at most n + m base-8 digits (ThreeSAT.lt_of_mem_digitNumbers, ThreeSAT.digitTarget_lt). Variable labels only enter through their order of first occurrence, so large binary labels do not enlarge the numbers. A literal repeated in a clause counts once, and a clause containing both literals of a variable is covered by both of its literal numbers. Formulas with a clause of more than three literals are not in ThreeSAT and are mapped to an instance without solutions.

Main definitions #

Main results #

References #

An instance of subset sum: a list of candidate natural numbers and a target sum.

  • numbers : List ℕ

    The candidate natural numbers, possibly with repetitions.

  • target : ℕ

    The target sum.

Instances For
    theorem Cslib.Complexity.SubsetSumInstance.ext {x y : SubsetSumInstance} (numbers : x.numbers = y.numbers) (target : x.target = y.target) :
    x = y
    theorem Cslib.Complexity.encSubsetSum_mk_budget {γ : Type} {encIn : γ ↪ List Bool} {budget : γ → ℕ} {x0 : γ → List ℕ} {x1 : γ → ℕ} (h0 : Turing.MultiTapeTM.ComputableInSpace x0 encIn (Turing.MultiTapeTM.encList Turing.MultiTapeTM.encNat) budget) (h1 : Turing.MultiTapeTM.ComputableInSpace x1 encIn Turing.MultiTapeTM.encNat budget) :
    Turing.MultiTapeTM.ComputableInSpace (fun (x : γ) => { numbers := x0 x, target := x1 x }) encIn encSubsetSum budget
    theorem Cslib.Complexity.encSubsetSum_numbers_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → SubsetSumInstance} :

    Instances ⟨a, t⟩ of SubsetSum: the list a, framed, each of its numbers framed, then the target t; all numbers in binary.

    Instances For
      theorem Cslib.Complexity.encSubsetSum_mk_inO {γ : Type} {encIn : γ ↪ List Bool} {x0 : γ → List ℕ} {T0 S0 x1 T1 S1 : γ → ℕ} (h0 : Turing.MultiTapeTM.ComputableInO x0 encIn (Turing.MultiTapeTM.encList Turing.MultiTapeTM.encNat) T0 S0) (h1 : Turing.MultiTapeTM.ComputableInO x1 encIn Turing.MultiTapeTM.encNat T1 S1) :
      Turing.MultiTapeTM.ComputableInO (fun (x : γ) => { numbers := x0 x, target := x1 x }) encIn encSubsetSum (fun (x : γ) => T0 x + T1 x) fun (x : γ) => S0 x + S1 x
      theorem Cslib.Complexity.encSubsetSum_target_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → SubsetSumInstance} :
      @[reducible, inline]

      The tuple of the fields of encSubsetSum.

      Instances For

        The instances of subset sum: some sub-multiset of x.numbers sums to x.target [Kar72], Sipser, Chapter 7.

        Instances For

          Subset sum asks for a subsequence of the numbers with the target sum.

          theorem Cslib.Complexity.map_mem_SubsetSum_iff {ι : Type u_1} [DecidableEq ι] {xs : List ι} (hxs : xs.Nodup) (f : ι → ℕ) (t : ℕ) :
          { numbers := List.map f xs, target := t } ∈ SubsetSum ↔ ∃ F ⊆ xs.toFinset, ∑ x ∈ F, f x = t

          A sub-multiset of the values of a duplicate-free list sums to t exactly when a subset of the list does.

          The verifier for SubsetSum, a mask verifier (Cslib.Complexity.maskVerifier): the numbers selected by the certificate mask sum to the target.

          Instances For

            The SubsetSum verifier is polynomial-time: it selects the masked numbers, adds them in binary and compares the sum with the target.

            The digit construction #

            Whether clause j of φ contains the literal of sign b of variable number i of φ.distinctVars.

            Instances For

              The digit positions of the number of a literal: the position i of its variable, and the position φ.distinctVars.length + j of every clause j containing it.

              Instances For

                The number whose base-8 digits are one at the positions ps and zero elsewhere, for distinct positions.

                Instances For

                  The numbers of the digit construction: two literal numbers for each variable, and two slack numbers with the single digit of each clause.

                  Instances For

                    The target of the digit construction: digit one at each variable position and digit three at each clause position.

                    Instances For

                      Sipser's reduction from ThreeSAT to SubsetSum: the digit construction for formulas whose clauses have at most three literals, and an instance without solutions otherwise.

                      Instances For

                        Correctness #

                        theorem Cslib.Complexity.ThreeSAT.covers_eq_true_iff {φ : Logic.PL.CNF ℕ} {i : ℕ} {b : Bool} {j : ℕ} :
                        covers φ i b j = true ↔ ∃ l ∈ List.getD φ j [], List.idxOf l.1 φ.distinctVars = i ∧ l.2 = b

                        Clause j covers the literal number (i, b) when it contains a literal of sign b whose variable is number i of φ.distinctVars.

                        @[reducible, inline]

                        The items of the digit construction: literal numbers (i, b) and slack numbers (j, c).

                        Instances For

                          Size of the digit construction. Every number has at most φ.distinctVars.length + φ.length base-8 digits, that is, three binary digits per variable and per clause.

                          The target has at most φ.distinctVars.length + φ.length base-8 digits.

                          theorem Cslib.Complexity.ThreeSAT.digit_mem_SubsetSum_iff_counts {φ : Logic.PL.CNF ℕ} (hφ : ∀ c ∈ φ, List.length c ≤ 3) :
                          { numbers := digitNumbers φ, target := digitTarget φ } ∈ SubsetSum ↔ ∃ A ⊆ Finset.range φ.distinctVars.length ×ˢ Finset.univ, ∃ B ⊆ Finset.range (List.length φ) ×ˢ Finset.univ, (∀ i < φ.distinctVars.length, {a ∈ A | a.1 = i}.card = 1) ∧ ∀ j < List.length φ, {a ∈ A | covers φ a.1 a.2 j = true}.card + {c ∈ B | c.1 = j}.card = 3

                          The digit construction in terms of column counts: a solution chooses literal numbers A and slack numbers B so that each variable position holds one chosen literal number and each clause position three chosen numbers.

                          theorem Cslib.Complexity.ThreeSAT.counts_iff_satisfiable {φ : Logic.PL.CNF ℕ} (hφ : ∀ c ∈ φ, List.length c ≤ 3) :
                          (∃ A ⊆ Finset.range φ.distinctVars.length ×ˢ Finset.univ, ∃ B ⊆ Finset.range (List.length φ) ×ˢ Finset.univ, (∀ i < φ.distinctVars.length, {a ∈ A | a.1 = i}.card = 1) ∧ ∀ j < List.length φ, {a ∈ A | covers φ a.1 a.2 j = true}.card + {c ∈ B | c.1 = j}.card = 3) ↔ φ.Satisfiable

                          Column counts as in digit_mem_SubsetSum_iff_counts exist exactly when the formula is satisfiable: the chosen literal numbers are the true literals, and slack numbers fill each clause position up to three.

                          Correctness of Sipser's reduction. A formula belongs to ThreeSAT exactly when its digit construction has a sub-multiset summing to the target.

                          Polynomial time #

                          ThreeSAT reduces to SubsetSum in polynomial time by Sipser's digit construction.

                          Subset sum is NP-complete, with the numbers and the target in binary [Kar72], Sipser, Theorem 7.56.