# Subset sum through balancing

This is a note for Pisinger’s balancing algorithm for subset sum [1]. Let $S$ be the set of all subset sums of $S$. The subset sum problem, the input is $S⊂N$, and we are interested in checking if $t∈S$.

We define a variation of the subset sum problem. The *balanced
subset sum problem*. In this problem, we are given a vector $v$ of integers(does not have to be positive).
We let $M=∥v∥_{∞}$. We are
interested in find a subset that sums to $t∈[M]$.

Each subset sum problem on $n$ elements can be reduced to a balanced subset sum problem in $n$ elements in $O(n)$ time.

Consider the input to the subset sum problem $S$ and $t$. Greedily find a subset of elements $S_{′}$, such that adding any other element will exceed $t$. Let $∥S_{′}∥_{1}=t_{′}$. Now, we negate all the elements in $S_{′}$, and ask for balanced subset sum with input set $−S_{′}∪(S∖S_{′})$ and target number $t−t_{′}$.

We partition $S$ into $A=[−M..0]∩S$ and $B=S∖A$. We also define $A_{i}={a_{1},…,a_{i}}$ and $B_{i}={b_{1},…,b_{i}}$.

A set is balanced by the following recursive definition. Let $S$ be a set.

- $S=∅$ is balanced.
- $∥S∥_{1}>t$, then $S∪{a}$ is balanced, where $a∈A$.
- $∥S∥_{1}≤t$, then $S∪{b}$ is balanced, where $b∈B$.

Consider a set $T$, such that $(i,j,k)∈T$ if and only if $k$ is a subset sum of $A_{i}∪B_{j}$. Certainly, we are interested if $(∣A∣,∣B∣,t)$ is in $T$. However, the state space is already $O(n_{2}M)$, which is no better than the standard dynamic programming algorithm.

There is a nice dominance relation. If $(i,j,k)∈T$, then for $(i_{′},j_{′})≥(i,j)$, we have $(i_{′},j_{′},k)∈T$. We can ask for each $k$, what are all the minimal $(i,j)$ pairs where $(i,j,k)∈T$. Such value will be $g(j,k)$. Formally, $g(j,k)=min{i∣(i,j,k)∈T}$, one can see that $g(j,k)≥g(j+1,k)$. Also, we know the solution corresponding to $g(j,k)$ must contain $a_{g(j,k)}$ as an element.

One can get a recurrence relation for $g$ as below.

$g(j,k)=min⎩⎨⎧ g(j−1,k)g(j−1,k−b_{j})i ifk−b_{j}≤tifk−a_{i}>tandi>g(j,k−a_{i}) $

Fix a $k$ and $j$, let $i$ to be as small as possible, such that there is $A_{i}⊂A_{i}$ and $B_{j}⊂B_{j}$ such that $A_{i}∪B_{j}$ is balanced and sums to $k$. Note that $a_{i}∈A_{i}$.

We obtained $A_{i}∪B_{j}$ by inserting an element in $B$ or $A$ to another balanced set. If the inserted element is in $B$, but not $b_{j}$, then we know $i=g(j−1,k)$. If it is $b_{j}$, then $i=g(j−1,k−b_{j})$. If the last inserted is $a_{i}$, then $g(j,k)=i$. Note we observe in this case, $g(j,k−a_{i})<i$. A direct dynamic programming implementation seems to imply a $O(n_{2}M)$ time algorithm, since there does not seem to be a quick way to obtain $i$.

On the other hand, if we think bottom up instead of top down, we can obtain a better result. Below is the algorithm.

The value $D[j,k]$ eventually equals $g(j,k)$. Note we can argue the running time is $O(nM)$, since for each fixed $k$, the final for loop can ran at most $n$ times. It is frustrating that the DP algorithm cannot be inferred directly from the recurrence relation. Indeed, we mainly obtained this through the fact that we can prune the search space if we start bottom up, which is unclear from the recurrence relation.

# References

**Linear time algorithms for knapsack problems with bounded weights**, Journal of Algorithms. 33 (1999) 1–14 10.1006/jagm.1999.1034.