Reaves.dev

v0.1.0

built using

Phoenix v1.7.20

Subset-Sum NP Complete

Stephen M. Reaves

::

2025-03-19

Proving Subset-Sum problem is NP-Complete

Summary

We prove that the Subset-Sum problem is NP-Complete by reducing from 3SAT to SS.

Contents

Subset Sum

Input: positive integers a1,,an&t a_1, \ldots, a_n & t

Output: Subet of numbers S that add up to t

If there is an O(nt) algorithm, this problem is NOT in P since its not polynomial in the SIZE of the input. It would need to be O(n log(t)).

SS is in NP

Given inputs a1,,an&t a_1, \ldots, a_n & t and S, check that iSai=t \displaystyle\sum_{i \in S} a_i = t , takes time O(n log(t))

Reduce 3SAT to Subset-Sum

Input to subset-sum problem will be 2n + 2m + 1 numbers

x1v1,x1ˉv1,xnvn,xnˉvn,s1,s1,sm,sm,t \begin{gathered} x_1 &\rightarrow v_1, \ \bar{x_1} &\rightarrow v_1', \ \vdots \ x_n &\rightarrow v_n, \ \bar{x_n} &\rightarrow v_n', \ s_1, \ s_1', \ \vdots \ s_m, \ s_m', \ t \end{gathered}

All are n+m \le n + m digits long

t10n+m t \approx 10^{n+m}

Variables

vi=^xi:viS    xi=T v_i \hat{=} x_i : v_i \in S \iff x_i = T

vi=^xiˉ:viS    xi=F v_i' \hat{=} \bar{x_i} : v_i' \in S \iff x_i = F

Need to ensure exactly one of viviS v_i \lor v_i' \in S

In ith digit of vi,vi,&t v_i, v_i', & t put a 1, all other numbers put a 0

Example

f=(x1ˉx2ˉx3ˉ)(x1ˉx2ˉx3)(x1x2ˉx3)(x1x2) f = \left( \bar{x_1} \lor \bar{x_2} \lor \bar{x_3} \right) \land \left( \bar{x_1} \lor \bar{x_2} \lor x_3 \right) \land \left( x_1 \lor \bar{x_2} \lor x_3 \right) \land \left( x_1 \lor x_2 \right)

x1 x_1 x2 x_2 x3 x_3 c1 c_1 c2 c_2 c3 c_3 c4 c_4
v1 v_1 1
v1 v_1' 1
v2 v_2 0
v2 v_2' 0
v3 v_3 0
v3 v_3' 0
s1 s_1 0
s1 s_1' 0
s2 s_2 0
s2 s_2' 0
s3 s_3 0
s3 s_3 0
s4 s_4' 0
s4 s_4' 0
t t 1

This corresponds to only including v1v1 v_1 \lor v_1' since the sum has to equal the target of 1. Then digit n+j corresponds to clause cj c_j

if xicj x_i \in c_j put a 1 in digit n+j for vi v_i

x1 x_1 x2 x_2 x3 x_3 c1 c_1 c2 c_2 c3 c_3 c4 c_4
v1 v_1 1000
v1 v_1' 1001
v2 v_2 0100
v2 v_2' 0101
v3 v_3 0010
v3 v_3' 0011
s1 s_1 0001
s1 s_1' 0001
s2 s_2 0000
s2 s_2' 0000
s3 s_3 0000
s3 s_3 0000
s4 s_4' 0000
s4 s_4' 0000
t t 1113

This corresponds to only letting us pick 3 in the column of c1 c_1 . If all three are satisfied, we pick one of the vi v_i variables. If one is not, we skip it and include one of the s variables (buffer). Since we only have 2 buffers but need a sum of 3, we have to have at least on literal be satisfied.

x1 x_1 x2 x_2 x3 x_3 c1 c_1 c2 c_2 c3 c_3 c4 c_4
v1 v_1 1000011
v1 v_1' 1001100
v2 v_2 0100001
v2 v_2' 0101110
v3 v_3 0010110
v3 v_3' 0011000
s1 s_1 0001000
s1 s_1' 0001000
s2 s_2 0000100
s2 s_2' 0000100
s3 s_3 0000010
s3 s_3 0000010
s4 s_4' 0000001
s4 s_4' 0000001
t t 1113333

Correctness

SS has a solution      \iff 3SAT is satisfiable

Forward Claim

Take solution S to subset-sum

for digit i where 1in 1 \le i \le n :

  • to get a 1 in digit i, include vivi v_i \lor v_i' but not both
    • viS    xi=T v_i \in S \implies x_i = T
    • viS    xi=F v_i' \in S \implies x_i = F

This gives us an assignment.

For digits n+j where 1jm 1 \le j \le m :

  • to get a sum of 3 in digit n+j, need to include 1 \ge 1 literal of cj c_j

This proves our assignment is satisfying. \blacksquare

Reverse Claim

Take a satisfying assignment for f:

  • xi=T    viS x_i = T \implies v_i \in S
  • xi=F    viS x_i = F \implies v_i' \in S
    • ith digit of t is correct
  • for clauses cj:1 c_j : \ge 1 literals is satisfied
    • gets up to a sum of 3 in digit n+j, when using s buffers \blacksquare