1

I have multiple arrays. I need to find the largest subset of arrays, such that, all arrays in that subset have atleast an element in common with each other. By largest I mean, the subset should have most number of arrays. I am not interested in finding which particular arrays are in the subset, but rather the size of the subset. e.g. if:

a1 = [1,3,7]
a2 = [3,5,7]
a3 = [2,8,9]
a4 = [7,8,9]

then I should get largest subset size as 3, because largest subset of given arrays would be a1,a2 and a4, because:
a1 ∩ a2 != ∅ && a1 ∩ a4 != ∅ && a2 ∩ a4 != ∅

I have a function common(array1,array2) which returns true if array1 ∩ array2 != ∅ and false otherwise.
One way of solving it would be to make all possible pairs of arrays, and check them for commonality. But the issue here is, given a list of pairs that have common element(s) between them, how to construct the largest subset.
e.g. given the above example, how to construct {a1,a2,a4} from (a1,a2), (a1,a4), (a2,a4), (a3,a4).

5
  • Constructing the largest subset from the list of pairs of common sets wouldn't work, as you don't know what is common between them. Commented Nov 9, 2015 at 20:42
  • @John if a1 ∩ a2 != ∅ && a1 ∩ a4 != ∅ && a2 ∩ a4 != ∅, then we can have a subset having {a1,a2,a4} because all possible array pairs in this subset have an element in common with each other. Commented Nov 9, 2015 at 20:47
  • Consider the case a1 = {1, 2}; a2 = {2, 4}; a4 = {1, 4}. We have a1 ∩ a2 != ∅ && a1 ∩ a4 != ∅ && a2 ∩ a4 != ∅ but still a1 ∩ a2 ∩ a4 = ∅. Commented Nov 9, 2015 at 20:52
  • True! didn't think about this example. Commented Nov 9, 2015 at 21:06
  • @John in scenarios where this intersection hold, can you suggest as how to proceed ? Commented Nov 9, 2015 at 23:59

1 Answer 1

2

Since you are not interested in finding which particular arrays are in the subset, but rather only the size of the subset, one way would be to create a map of all the possible values to the number of arrays containing that value.

For the example in the question, the map would look something like:

count[1] = 1 // contained by a1
count[2] = 1 // contained by a3
count[3] = 2 // contained by a1, a2
count[7] = 3 // contained by a1, a2, a4
count[8] = 2 // contained by a3, a4
count[9] = 2 // contained by a3, a4

The highest value in the count map (in this case, 3) is the desired result.

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

4 Comments

Can you suggest any other alternative, that should work with object datatypes as well?
You can use this method, if you can represent an object uniquely with a string/int. Implementation of a map of such objects depends on the language you're using.
But the structure of object would be a complex/hierarchical one. I don't really want to represent each unique object, but want to simplify the computation by making use of intersection as explained in the question above.
How do you check whether two objects are equal or not? If you can check that, you can build a map, and an efficient one if you can even compare the objects.

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.