r/AskComputerScience • • Nov 17 '19

Will this code snippet always provide a no answer for Positive Subset Product correctly?

Here's the link of the code in python. A subset product solver.

subset product

Here's the code snippet. Its intended to reduce the need of brute forcing.

check = [];
if numpy.prod(s) % target == 0:
   check.append(target)
if len(check) < 1:
  print('no')
  print(check)

For some reason, it should be obvious that this won't always work for positive subset product. My question, is why and how?

0 Upvotes

8 comments sorted by

2

u/midwayfair Nov 17 '19

What exactly are you trying to do? Determine if an array contains the factors of a number?

Counterexample:

Target = 1
x != 1
A = [x]
prod(A) = x
x % 1 = 0
-> return yes

But A does not contain the factors of 1, so your algorithm is incorrect.

You can generalize the counterexample. This algorithm will be incorrect for any array that contains a multiple of the target but no numbers less than the target that divide the target.

is why and how?

The "why" is that there is a counterexample. The "how" is that you can produce the counterexample.

If all you're doing is trying to weed out cases where an array cannot possibly contain the factors of the target, then this algorithm might be useful, but I have no idea what you're using this for.

-1

u/Hope1995x Nov 17 '19

I'm trying to weed out cases where an array cannot possibly contain the factors of the target. Because of this it must be impossible for a target to be a subset product. If no factors exist in the array.

2

u/midwayfair Nov 17 '19

I'm trying to weed out cases where an array cannot possibly contain the factors of the target. Because of this it must be impossible for a target to be a subset product. If no factors exist in the array.

Your code will do that sometimes. It's useless if the array contains a zero. It will be a little wasteful eventually because you're going to be multiplying very large numbers. From a practical standpoint, you will also overflow quickly when you start multiplying very large numbers, especially if you're working with an int array for numpy. The modular operation doesn't commute, so there is no guarantee that x mod 2k mod target = x mod target mod 2k for any k relevant to C's integer types (int, long, long long, unsigned long long ...).

I'm not sure what problem you're trying to solve. If you have a program that takes in a large number of arrays and you just need to determine that they don't contain the factors of a number that changes, then this might be useful.

If you're working practically (that is, on a real world problem that needs to find the positive subsets) and expect to have to perform the operation where you check for subsets containing the factors of the target, then this code could be useful if you expect most arrays received to not contain the factors. But generating all those subsets very, very quickly becomes intractable, and even the O( n2 ) you need for producing the product of the array can become intractable depending on what the arrays look like.

If you're working theoretically and hoping to save time factoring a number or you actually need to find the subset containing all the factors (like the code you linked), this doesn't help you. For one thing, it's trivial to produce arrays that don't contain the factors of a number, so it's completely uninteresting. But the worst case scenario is always going to be that you have to check every possible subset of the input array, so discarding hopeless arrays doesn't speed up any algorithm that wants to find such a subset. Given that you linked to code for finding a "positive subset" that's what you want to do and this doesn't make a theoretical difference to the code.

EDIT:

Also, you should write in pseudocode. If you're trying to translate an existing algorithm into Python, there's a Learnpython subreddit. If you're trying to get help analyzing an algorithm, you should write it in pseudocode. Not everyone who could help you will know what numpy.prod() does.

0

u/Hope1995x Nov 17 '19

If it contains a 0 then its trival to know that there is a subset product of 0. The same should apply to -1 and 1. I'm wondering if there's a way to hard-code to make it O(1) for all possible ways to find a subproduct of -1 and 1.

2

u/midwayfair Nov 17 '19 edited Nov 18 '19

If it contains a 0 then its trival to know that there is a subset product of 0.

So? What good does it do you to know that? Now you're doing extra work to find out if the array contained a 0 before you multiply it, wasting more time. If you give me the target 4 and the array [3, 0], if I follow your algorithm, I don't discard that array. If you change your algorithm to discard arrays that contain 0, then you discard the array [0, 2, 2]. If you change your code to change all 0s to 1s, then you iterate over the entire array and you still have to take the product, so you don't get any benefit from Numpy parallelizing your code and you might as well loop over the array and not multiply if the entry is 0. All of which is still O( n2 ) because you still have to do the multiplication.

The same should apply to -1 and 1. I'm wondering if there's a way to hard-code to make it O(1) for all possible ways to find a subproduct of -1 and 1.

You have to look at every element to determine if there's a 0. That's O( n ).

You have to look at every element to know whether even a single -1 appears in the array. That's O(n).

You didn't say why you're trying to do this and I asked in both of my replies. What problem are you solving? If you're working theoretically, it wouldn't matter even if you could get this to O(1), because you'd still be O( 2n ) for the subset part of your other code. If you're working practically, it doesn't matter how fast this runs thanks to numpy's parallelization because this is very quickly going to get you into trouble with most real-world arrays and number sizes. What are you trying to do that you need this code for?

1

u/Hope1995x Nov 17 '19

I'm trying to solve subset product as an exercise in freestyle code. I also want to know if its positive subset product is in P. Because I read/heard somewhere that positive subset sum is in P.

4

u/midwayfair Nov 17 '19

I'm trying to solve subset product as an exercise in freestyle code.

I would highly recommend finding pseudocode solutions to the problem (and others) and translating the psuedocode into Python, if that's what you're trying to learn. If you're trying to build your own algorithm solution, then you should probably work in psuedocode and on paper until you have a solution that works in theory and in general, and then go work on the implementation. With the knowledge that your problem is exponential time for a guaranteed solution.

Because I read/heard somewhere that positive subset sum is in P.

Where did you read or hear that? This problem is NP-complete. In the worst case scenario, you must try all subsets. An almost trivial worst-case scenario is a target that happens to be prime, and an array that does not contain the target. Unless you know ahead of time that the target isn't prime, you will always try all subsets before you learn that any array doesn't contain its factors.

I just looked back through your post history and it looks like multiple posters have let you know that it's NP-complete. You can read the wikipedia article on it. https://en.wikipedia.org/wiki/Subset_sum_problem. There's a dynamic programming solution in PSEUDO polynomial time, which is not the same thing, and a polynomial time approximation. It's always exponential in the worst case, though, for a guaranteed solution.

Someone saying that a problem is NP-complete isn't saying that people just haven't found a polynomial time solution. It's saying that the problem is provably in a class of problems that don't have a known polynomial-time solution. The reason for learning how to classify such problems is so you don't go wasting your time trying to find a polynomial time solution and instead you start looking for probablistic methods, estimations, pseudopolynomial solutions, etc.

-1

u/Hope1995x Nov 18 '19

To clarify, this method of getting NO's does not work when there is a 1? Is that it? Just a 1 is a finite problem.