Search code examples
mathpascals-triangle

Pascal's Theorem for non-unique sets?


Pascal's rule on counting the subset's of a set works great, when the set contains unique entities.

Is there a modification to this rule for when the set contains duplicate items?

For instance, when I try to find the count of the combinations of the letters A,B,C,D, it's easy to see that it's 1 + 4 + 6 + 4 + 1 (from Pascal's Triangle) = 16, or 15 if I remove the "use none of the letters" entry.

Now, what if the set of letters is A,B,B,B,C,C,D? Computing by hand, I can determine that the sum of subsets is: 1 + 4 + 8 + 11 + 11 + 8 + 4 + 1 = 48, but this doesn't conform to the Triangle I know.

Question: How do you modify Pascal's Triangle to take into account duplicate entities in the set?


Solution

  • It looks like you want to know how many sub-multi-sets have, say, 3 elements. The math for this gets very tricky, very quickly. The idea is that you want to add together all of the combinations of ways to get there. So you have C(3,4) = 4 ways of doing it with no duplicated elements. B can be repeated twice in C(1,3) = 3 ways. B can be repeated 3 times in 1 way. And C can be repeated twice in C(1,3) = 3 ways. For 11 total. (Your 10 you got by hand was wrong. Sorry.)

    In general trying to do that logic is too hard. The simpler way to keep track of it is to write out a polynomial whose coefficients have the terms you want which you multiply out. For Pascal's triangle this is easy, the polynomial is (1+x)^n. (You can use repeated squaring to calculate this more efficiently.) In your case if an element is repeated twice you would have a (1+x+x^2) factor. 3 times would be (1+x+x^2+x^3). So your specific problem would be solved as follows:

    (1 + x) (1 + x + x^2 + x^3) (1 + x + x^2) (1 + x)
      = (1 + 2x + 2x^2 + 2x^3 + x^4)(1 + 2x + 2x^2 + x^3)
      = 1    + 2x   + 2x^2 +  x^3 +
        2x   + 4x^2 + 4x^3 + 2x^4 +
        2x^2 + 4x^3 + 4x^4 + 2x^5 +
        2x^3 + 4x^4 + 4x^5 + 2x^6 +
        x^4  + 2x^5 + 2x^6 +  x^7
      = 1 + 4x + 8x^2 + 11x^3 + 11x^4 + 8x^5 + 4x^6 + x^7
    

    If you want to produce those numbers in code, I would use the polynomial trick to organize your thinking and code. (You'd be working with arrays of coefficients.)