How do you calculate the total number of all possible unique subsets from a set with repeats?

Nixuz picture Nixuz · Apr 10, 2009 · Viewed 7.5k times · Source

Given a set** S containing duplicate elements, how can one determine the total number all the possible subsets of S, where each subset is unique.

For example, say S = {A, B, B} and let K be the set of all subsets, then K = {{}, {A}, {B}, {A, B}, {B, B}, {A, B, B}} and therefore |K| = 6.

Another example would be if S = {A, A, B, B}, then K = {{}, {A}, {B}, {A, B}, {A, A}, {B, B}, {A, B, B}, {A, A, B}, {A, A, B, B}} and therefor |K| = 9

It is easy to see that if S is a real set, having only unique elements, then |K| = 2^|S|.

What is a formula to calculate this value |K| given a "set" S (with duplicates), without generating all the subsets?

** Not technically a set.

Answer

v3. picture v3. · Apr 10, 2009

Take the product of all the (frequencies + 1).

For example, in {A,B,B}, the answer is (1+1) [the number of As] * (2+1) [the number of Bs] = 6.

In the second example, count(A) = 2 and count(B) = 2. Thus the answer is (2+1) * (2+1) = 9.

The reason this works is that you can define any subset as a vector of counts - for {A,B,B}, the subsets can be described as {A=0,B=0}, {A=0,B=1}, {0,2}, {1,0}, {1,1}, {1,2}.

For each number in counts[] there are (frequencies of that object + 1) possible values. (0..frequencies)

Therefore, the total number of possiblities is the product of all (frequencies+1).

The "all unique" case can also be explained this way - there is one occurence of each object, so the answer is (1+1)^|S| = 2^|S|.