简体   繁体   中英

Algorithm: find largest subset from arrays

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. eg 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.
eg given the above example, how to construct {a1,a2,a4} from (a1,a2), (a1,a4), (a2,a4), (a3,a4).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM