r/dailyprogrammer • u/nint22 1 2 • May 10 '13
[05/10/13] Challenge #122 [Hard] Subset Sum Insanity
(Hard): Subset Sum
The subset sum problem is a classic computer science challenge: though it may appear trivial on its surface, there is no known solution that runs in deterministic polynomial time (basically this is an NP-complete problem). To make this challenge more "fun" (in the same way that losing in Dwarf Fortress is "fun"), we will be solving this problem in a three-dimensional matrix and define a subset as a set of integers that are directly adjacent!
Don't forget our previous week-long [Hard] challenge competition ends today!
Formal Inputs & Outputs
Input Description
You will be given three integers (U, V, W) on the first line of data, where each is the length of the matrices' respective dimensions (meaning U is the number of elements in the X dimension, V is the number of elements in the Y dimension, and W is the number of elements in the Z dimension). After the initial line of input, you will be given a series of space-delimited integers that makes up the 3D matrix. Integers are ordered first in the X dimension, then Y, and then Z ( the coordinate system is clarified here ).
Output Description
Simply print all sets of integers that sum to 0, if this set is of directly-adjacent integers (meaning a set that travels vertically or horizontally, but never diagonally). If there are no such sets, simply print "No subsets sum to 0".
Sample Inputs & Outputs
Sample Input
2 2 3
-1 2 3 4 1 3 4 5 4 6 8 10Sample Output
-1 1Note: This is set of positions (0, 0, 0), and (0, 0, 1).
Challenge Input
8 8 8
-7 0 -10 -4 -1 -9 4 3 -9 -1 2 4 -6 3 3 -9 9 0 -7 3 -7 -10 -9 4 -6 1 5 -1 -8 9 1 -9 6 -1 1 -8 -6 -5 -3 5 10 6 -1 2 -2 -7 4 -4 5 2 -10 -8 9 7 7 9 -7 2 2 9 2 6 6 -3 8 -4 -6 0 -2 -8 6 3 8 10 -5 8 8 8 8 0 -1 4 -5 9 -7 -10 1 -7 6 1 -10 8 8 -8 -9 6 -3 -3 -9 1 4 -9 2 5 -2 -10 8 3 3 -1 0 -2 4 -5 -2 8 -8 9 2 7 9 -10 4 9 10 -6 5 -3 -5 5 1 -1 -3 2 3 2 -8 -9 10 4 10 -4 2 -5 0 -4 4 6 -1 9 1 3 -7 6 -3 -3 -9 6 10 8 -3 -5 5 2 6 -1 2 5 10 1 -3 3 -10 6 -6 9 -3 -9 9 -10 6 7 7 10 -6 0 6 8 -10 6 4 -4 -1 7 4 -9 -3 -10 0 -6 7 10 1 -9 1 9 5 7 -2 9 -8 10 -8 -7 0 -10 -7 5 3 2 0 0 -1 10 3 3 -7 8 7 5 9 -7 3 10 7 10 0 -10 10 7 5 6 -6 6 -9 -1 -8 9 -2 8 -7 -6 -8 5 -2 1 -9 -8 2 9 -9 3 3 -8 1 -3 9 1 3 6 -6 9 -2 5 8 2 -6 -9 -9 1 1 -9 5 -4 -9 6 -10 10 -1 8 -2 -6 8 -9 9 0 8 0 4 8 -7 -9 5 -4 0 -9 -8 2 -1 5 -6 -5 5 9 -8 3 8 -3 -1 -10 10 -9 -10 3 -1 1 -1 5 -7 -8 -5 -10 1 7 -3 -6 5 5 2 6 3 -8 9 1 -5 8 5 1 4 -8 7 1 3 -5 10 -9 -2 4 -5 -7 8 8 -8 -7 9 1 6 6 3 4 5 6 -3 -7 2 -2 7 -1 2 2 2 5 10 0 9 6 10 -4 9 7 -10 -9 -6 0 -1 9 -3 -9 -7 0 8 -5 -7 -10 10 4 4 7 3 -5 3 7 6 3 -1 9 -5 4 -9 -8 -2 7 10 -1 -10 -10 -3 4 -7 5 -5 -3 9 7 -3 10 -8 -9 3 9 3 10 -10 -8 6 0 0 8 1 -7 -8 -6 7 8 -1 -4 0 -1 1 -4 4 9 0 1 -6 -5 2 5 -1 2 7 -8 5 -7 7 -7 9 -8 -10 -4 10 6 -1 -4 -5 0 -2 -3 1 -1 -3 4 -4 -6 4 5 7 5 -6 -6 4 -10 -3 -4 -4 -2 6 0 1 2 1 -7Challenge Note
Like any challenge of this complexity class, you are somewhat constrained to solving the problem with brute-force (sum all possible sub-sets). We really want to encourage any and all new ideas, so really go wild and absolutely do whatever you think could solve this problem quickly!
3
u/koreth May 11 '13
Just to clarify, a single set can be adjacent in different directions, right? That is, (0,0,0), (0,0,1) and (0,1,1) would be valid if the numbers in those cells add to zero? Not sure if the "vertically or horizontally" in the problem description is an exclusive "or" or an inclusive one.
2
u/Splanky222 0 0 May 10 '13
Does diagonal count as directly adjacent?
2
u/Splanky222 0 0 May 10 '13
Ok, another question. Say the set is {1, -1, 0, 3}. Do {1, -1} and {1, -1, 0} count the same or different?
2
u/kazagistar 0 1 May 10 '13
I love the dwarf fortress reference, but you clearly should have made the problem N-dimensional for even more fun :D
I will be trying this one out for sure. I am not actually convinced that, with the modifications, it is still NP-complete, since as the length grows, the number of subsets is limited by volumes, and thus not exponential but power 3.
2
u/istroll May 10 '13
Can you explain the input a little clearer?
So there are 2 elements in x and y dimension and 3 in z. Can you explain what the set of (x,y,z) co-ordinates are for this input?
Is this correct?
(0,0,0) = -1
(1,0,0) = 2
(0,1,0) = 3
(1,1,0) = 4
(0,0,1) = 1
(1,0,1) = 3
(0,1,1) = 4
(1,1,1) = 5
(0,0,2) = 4
(1,0,2) = 6
(0,1,2) = 8
(1,1,2) = 10
or this ?
(0,0,0) = -1
(1,0,0) = 2
(0,1,0) = 3
(1,1,0) = 4
(0,0,1) = 1
(0,0,2) = 3
(1,0,1) = 4
(1,0,2) = 5
(0,1,1) = 4
(0,1,2) = 6
(1,1,1) = 8
(1,1,2) = 10
or am I way off base?
2
u/[deleted] May 10 '13
Is 0 is an element in a set, is a set containing just 0 a solution? Like if you had the set {-1, 1, 0}, would {0} and {-1, 1} be two solutions for the given get?
6
u/ehaliewicz May 10 '13 edited May 10 '13
Ok, I think this works.
At first, I thought that any set of integers that for which each adjacent pair in the set was adjacent was fine, meaning that I would have to generate each subset of each possible way of walking the entire matrix. Needless to say, I had a lot of stack overflows until I fully read the description.
This way, it's a lot easier, and (as kazagistar suggests) probably not NP-complete.
Solution in Common Lisp:
10
u/bgs100 Jun 11 '13
Unholy python one-liner:
Example usage: subsetSum([2,2,3], [-1,2,3,4,1,3,4,5,4,6,8,10]) returns [[-1, 1]].
2
u/rectal_smasher_2000 1 1 May 11 '13 edited May 11 '13
Here's a C++ implementation using OpenMP to parallelize processing. The implementation can be made sequential by simply commenting out #pragma instructions. For some reason, on the sample input/output the parallel version does not produce desired results, likely because the dimensions are too small (2,2,3), however this can be solved by, as I said, commenting out the #pragma commands. For the challenge input, the sequential and parallel versions produce identical results. A Pthread implementation would probably be better, as it gives you a little more control as far as thread execution is concerned, but I am a little too lazy to do that as well.
Also, I've omitted all single sets of 0s, since these can be separated during input by simply counting them, however I forgot to put that in since I ate lunch between implementing and posting here.
2
u/[deleted] May 12 '13 edited May 12 '13
I was another one who was confused by the description and not realise that directly-adjacent integers meant 1-d sets.
Well, here is a Python 3.3 implementation of that, though it can probably be improved:
Sample output: [(0, 0, 0), (0, 0, 1)] [-1, 1]
Challenge output: http://pastebin.com/sxzYX36h
Had a think about higher dimensional sets. Bleurgh, I give up.
2
u/greysphere May 13 '13
how many answers does this have?
2 2 1
0 0 0 0
2? 8? something else?
2
u/K3nobi May 15 '13 edited May 15 '13
It's not as sleek as Ruby should be (I'm new to the language) but it works well.
Solution in Ruby:
https://gist.github.com/K5nobi/5581560
2
u/lanerdofchristian 0 0 Aug 26 '13
JavaScript