Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

number of all subsets of a set

This is what I came up with to calculate all subsets of length 0, 1, ... , n of a set of length n with doubling single elements. Difficult to describe...

def subsets(seq, *args):

    seqstart = [[seq[i] for i in args], ]

    if len(args) == 0:
        for i in range(len(seq)):
            seqstart += subsets(seq, i)
    elif len(args) < len(seq):
        for i in range(args[-1], len(seq)):
            seqstart += subsets(seq, *args + (i, ))

    return seqstart

Examples:

>>> subsets(['x', 'y'])
[[], ['x'], ['x', 'x'], ['x', 'y'], ['y'], ['y', 'y']]

>>> subsets(['x', 'y', 'z'])
[[], ['x'], ['x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'y'], ['x', 'x', 'z'], ['x', 'y'], ['x', 'y', 'y'], ['x', 'y', 'z'], ['x', 'z'], ['x', 'z', 'z'], ['y'], ['y', 'y'], ['y', 'y', 'y'], ['y', 'y', 'z'], ['y', 'z'], ['y', 'z', 'z'], ['z'], ['z', 'z'], ['z', 'z', 'z']]

What is the length of subsets(sequence) dependent on the length of the sequence? (I killed the process after 50 hours with n=14)

Thank You

Michael

edit: Thank you all. So it is the Binomial Coefficient of 2n over n.

To obtain all subsets instead of multisets (so a total length of 2^n) I needed to replace

for i in range(args[-1], len(seq)):

with

for i in range(args[-1] + 1, len(seq)):

like image 346
Michael Franzen Avatar asked Dec 06 '25 13:12

Michael Franzen


2 Answers

The number of multisets of size up to n of a set of size n is equal to the binomial coefficient

/ 2n \
|    |
\ n  /

This follows by summing up the number of combinations with repetition for k from 0 to n.

For n=14, this yields 40116600 multisets.

like image 72
Sven Marnach Avatar answered Dec 09 '25 21:12

Sven Marnach


For a given set A with N number of elements, the number of subsets is equal to 2^N

like image 44
Jean-Louis Mbaka Avatar answered Dec 09 '25 21:12

Jean-Louis Mbaka



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!