r/MathHelp • • Mar 17 '21

Create all subsets from a given set

Hello,

I'm trying to figure out a way to generate all the possible subsets of a given size from a given set of n elements. I've figured out that I can map all of the possible sets to the integers from 0 to 2^n, and use the binary representation to say whether a particular element is included or excluded. So for instance element1 corresponds to the least significant bit, ..., elementN corresponds to the most significant bit, and the element is either included or excluded based on whether that element corresponds to a bit value of 1 or 0.

For example, if the starting set has 6 elements, subset 5 = 000101 corresponds to a subset of size 2 with element1 and element3 included in the subset.

Can anyone come up with a systematic way to generate all of the integers that would correspond to every subset of a given size? This is trivial for a subset of size 0, and for a subset of size n. It's pretty easy for a subset of size 1 and size n-1. But sizes 2, 3,..., n-2, n-3, ..., etc., are harder.

For size 1, I can do a loop,

integer = 1;
for(i = 0; i < n; i++)
{
    integer = integer << 1;
}

this would give me 1, 2, 4, 8, 16, ..., 2^(n-1). Size n-1 can be found by inverting the bits of these integers.

For size 2, it's a bit harder, but one I've found that works is

for(i = 0; i < n; i++)
{
    start = 1;
    start = start + (1 << i);
    integer = start;
    while(integer < (1 << n))
    {
        integer = integer << 1;
    }
}

This is not the only way I've found for size 2, but it's the cleanest.

Can someone please help me figure out a way to generate sets of size 3, 4, etc.?

9 Upvotes

2 comments sorted by

2

u/yearofplenty Mar 17 '21 edited Mar 17 '21

You're on the right track. I'm assuming by this syntax that you're doing this in something like R or Java. I'm more familiar with Python but it should be easily adapted. I've included comments in the code which should explain.

EDIT: meant to mention -- what you're generating is called the "power set". It contains all subsets of all sizes, including the empty set and the original set itself.

s = [str(i) for i in range(5)]

def powerset(iterable):
    out = []
    n = len(iterable)
    bitmasks = [1 << i for i in range(n)] # get powers of 2
    for i in range(1 << n): # 0-2^n 
        subset = [element for mask, element in zip(bitmasks, iterable) if i & mask]
        # zip() returns a tuple iterator with elements (l1_1, l2_1, .., ln_1) 
        # where lp_1 is the element at index 1 of list `p`
        # `if i & mask` checks if bitwise AND results in 0 
        out.append(subset)
    return out


p = sorted(powerset(s), key=lambda x: len(x)) # get powerset sorted by subset length 
print(f"set: {s}\npowerset:\n{p}")

output:

set: ['0', '1', '2', '3', '4']
powerset:
[[], ['0'], ['1'], ['2'], ['3'], ['4'], ['0', '1'], ['0', '2'], ['1', '2'], ['0', '3'], ['1', '3'], ['2', '3'], ['0', '4'], ['1', '4'], ['2', '4'], ['3', '4'], ['0', '1', '2'], ['0', '1', '3'], ['0', '2', '3'], ['1', '2', '3'], ['0', '1', '4'], ['0', '2', '4'], ['1', '2', '4'], ['0', '3', '4'], ['1', '3', '4'], ['2', '3', '4'], ['0', '1', '2', '3'], ['0', '1', '2', '4'], ['0', '1', '3', '4'], ['0', '2', '3', '4'], ['1', '2', '3', '4'], ['0', '1', '2', '3', '4']]

1

u/AutoModerator Mar 17 '21

This is an automated reminder:

  • What have you tried so far? (See Rule #2; to add an image, you may upload it to an external image-sharing site like Imgur and include the link in your post.)

  • Please don't delete your post. (See Rule #7)

We, the moderators of /r/MathHelp, appreciate that your question contributes to the MathHelp archived questions that will help others searching for similar answers in the future. Thank you for obeying these instructions.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.