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).
a1 = {1, 2}; a2 = {2, 4}; a4 = {1, 4}. We havea1 ∩ a2 != ∅ && a1 ∩ a4 != ∅ && a2 ∩ a4 != ∅but stilla1 ∩ a2 ∩ a4 = ∅.