0xDE ([info]11011110) wrote,
@ 2008-03-22 09:00:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Entry tags:algorithms

The missing subset sum
Here's a cute algorithmic puzzler that occurred to me yesterday, though perhaps it was long-known to others.

Let S be a set (or multiset) of positive integers. Describe an efficient algorithm for finding the smallest positive integer x such that x is not the sum of some subset of S.

Using dynamic programming to test each possible subset sum isn't good enough; I want something strongly polynomial. Or, to be really concrete about it, let's suppose that you have a computer in which W-bit integers can be added, subtracted, and compared in constant time, that additionally you have a constant time operation to compute floor(log2(k)) for any W-bit value k, and that all the members of S are W-bit numbers and that W ≥ log2|S|; I want an algorithm that runs in time O(|S|+W) in this model.

If you get stuck, the characterization of practical numbers might give you a hint.



(Post a new comment)

Nice puzzle
[info]rgrig
2008-03-23 02:35 pm UTC (link)
I think this works. (I don't know python so the code is probably ugly.)

(Reply to this)(Thread)

Re: Nice puzzle
[info]11011110
2008-03-23 04:01 pm UTC (link)
Yes, that's the solution I had in mind.

(Reply to this)(Parent)


(Anonymous)
2008-08-05 02:48 pm UTC (link)
Wouldn't min(S) be a solution... I guess I'm not getting it!

(Reply to this)(Thread)


[info]11011110
2008-08-05 03:44 pm UTC (link)
min(S) is a number that ''can''' be represented as the sum of a set of elements of S, namely the set consisting of the single element min(S). We're looking for a number that ''cannot'' be represented.

I suppose I should have included an example. If S = {1,2,3,8}, then
1 = sum {1}
2 = sum {2}
3 = sum {1,2}
4 = sum {1,3}
5 = sum {2,3}
6 = sum {1,2,3}
but there is no way to form 7 as a sum of a subset of S, so 7 is the number I'm looking for.

(Reply to this)(Parent)


Create an Account
Forgot your login?
Login w/ OpenID
English • Español • Deutsch • Русский…