4

Say I have three arrays - ['a', 'b'], ['b', 'c'] and ['d']. If I were to create a fourth array that intersects with these three arrays with a minimal number of elements the array I'd get would be ['b', 'd']. My question is... how would I go about finding this array?

Like ['a', 'b', 'c', 'd'] certainly intersects with all the arrays but it's not the minimal intersection - ['b', 'd'] is.

Any ideas?

5
  • 1
    Isn't that just taking the intersection of your original 3 sets? Commented Jul 23, 2015 at 21:28
  • @bhspencer define intersection (what does 'd' intersect with?) Commented Jul 23, 2015 at 21:29
  • 1
    @bhspencer i think he wants a array the intersects with any of the arrays. Very interesting Commented Jul 23, 2015 at 21:31
  • Oh I see what you mean Commented Jul 23, 2015 at 21:31
  • 1
    I'm almost certain this is NP-Complete and will require a brute force solution. This almost fits perfectly to Clique problem Commented Jul 23, 2015 at 22:02

2 Answers 2

3

I don't have a good answer for an algorithm, but it is true that, like commenter Amit wrote, this is an NP-complete problem. This problem is called the hitting set problem, which is equivalent to the set cover problem.

If you're fine with approximation, then according to the wiki article, greedily picking the elements that hit the most arrays as about as good as it gets.

Sign up to request clarification or add additional context in comments.

1 Comment

I just couldn't find the correct problem type... and there it was, so classic :-)
1

I think what you might want to try is going through each array to grab values that match more than one array. Then once you have those values, determine which values you can remove from the array.

Example:

[1,2] [2,3] [2,4] [1,5] [3,7] [4,8]

After looping through, we find that [1,2,3,4] are all values which match in more than one array.

Now we must determine if there are any values we can eliminate from this list.

If we eliminate 1, will everything still match?

No, we need 1.

If we eliminate 2, will everything still match?

Yes, we can eliminate 2 from the array. Now we have [1,3,4].

If we eliminate 3, will everything still match?

No, we need 3.

If we eliminate 4, will everything still match?

No, we need 4.

Our final array is [1,3,4].

This will not work if you have a completely unique array. To account for this, you could create a boolean array of all false values and set values to true as you match arrays. Any value that is still false in the end, you would have to pick a value from that array.

4 Comments

Sorry to put you down again, but really, this is not solvable. You can only come up with a method (I'm intentionally not calling this an algorithm) that feels like it's working, but with the correct input it will fail (like I showed you in your previous attempt). Again, sorry...
So you're saying there isn't a correct answer? Could you show me an example where this wouldn't work?
Nevermind, this wouldn't work if you had a completely unique array.
What I'm saying (and to be more accurate, what the world of set theory says) is that this is defined as NP-Complete. You can't guarantee a correct answer and at the same time guarantee efficiency better than brute force (test all possible answers). Whether I can (or have the time to) come up with an input that breaks your current method is irrelevant

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.