Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way of finding subsets and supersets in a collection of sets

Is there any efficient algorithm or data structure for looking up supersets and subsets on a collection of sets?

Example:

# collection of sets
>>> col = [{1,2,3},{2,3,4},{4,5,6,7}]

>>> subsets_of({1,2,3,4}, col)
[{1,2,3},{2,3,4}]

>>> supersets_of({4}, col)
[{2,3,4},{4,5,6,7}]
like image 304
aperez Avatar asked Nov 23 '25 18:11

aperez


1 Answers

In the general case, no, other than brute force search methods.

However, there are various techniques for doing linear time searches depending on the type of data you have. For example, if the element values were always less than 32 (or 64) then you could create an integer with the bits set for the values present and OR them to find out if the set was a subset. If the values are less than a 100 million or so you can create a giant boolean array where each array element is 1 if the set contains the value, or 0 if it does not. Then the subset lookup is linear time. A similar method is possible for solving the superset problem in linear time.

like image 198
Tyler Durden Avatar answered Nov 25 '25 10:11

Tyler Durden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!