The Art Gallery Guardian

Subset sum through balancing


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

We define a variation of the subset sum problem. The balanced subset sum problem. In this problem, we are given a vector of integers(does not have to be positive). We let . We are interested in find a subset that sums to .

Theorem1

Each subset sum problem on elements can be reduced to a balanced subset sum problem in elements in time.

Proof

Consider the input to the subset sum problem and . Greedily find a subset of elements , such that adding any other element will exceed . Let . Now, we negate all the elements in , and ask for balanced subset sum with input set and target number .

We partition into and . We also define and .

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

  • is balanced.
  • , then is balanced, where .
  • , then is balanced, where .

Consider a set , such that if and only if is a subset sum of . Certainly, we are interested if is in . However, the state space is already , which is no better than the standard dynamic programming algorithm.

There is a nice dominance relation. If , then for , we have . We can ask for each , what are all the minimal pairs where . Such value will be . Formally, , one can see that . Also, we know the solution corresponding to must contain as an element.

One can get a recurrence relation for as below.

Fix a and , let to be as small as possible, such that there is and such that is balanced and sums to . Note that .

We obtained by inserting an element in or to another balanced set. If the inserted element is in , but not , then we know . If it is , then . If the last inserted is , then . Note we observe in this case, . A direct dynamic programming implementation seems to imply a time algorithm, since there does not seem to be a quick way to obtain .

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

The algorithm

The value eventually equals . Note we can argue the running time is , since for each fixed , the final for loop can ran at most 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

[1]
D. Pisinger, Linear time algorithms for knapsack problems with bounded weights, Journal of Algorithms. 33 (1999) 1–14 10.1006/jagm.1999.1034.
Posted by Chao Xu on .
Tags: algorithms, subset sum.