Number of bags of a given length with exactly d distinct elements

We develop a formula for counting the number of bags of length \(l\) containing \(d\) distinct elements drawn from \(t\) objects. The formula involves multiplication of combinations without replacement and combinations with replacement. We present alternative forms of the formula and a generator in Python. Our motivation was computing probabilities of various types of card hands, such as how many hands of size 5 contain exactly two suites.

Introduction

In games, a matching mechanic may be used to restrict a player’s choice to options that match a previous play. Trick-taking games, a popular and specialized subset of matching, often restrict a player to “follow” a previous play’s cards suites or colors. In computer science, a function may assign items to specific buckets (e.g. a hash function) and we are concerned with the spread and number of collisions with a bucket. For analyzing both types of problems we likely desire to count the total number of variations possible.

In our case, we were modeling collections of cards drawn from a deck using a variation of the Python code below.

def distinct_generator(d: int, l: int, t: int):
    assert d > 0 and l > 0 and t > 0, "all must be positive integers"
    assert l >= d and t >= d, "l and t must be greater than or equal to d"
    for bag in itertools.combinations_with_replacement(range(0, t), l):
        actual_d = len(set(bag))
        if actual_d == d:
            yield seq

A note on novelty We wrote this article to document a formula since we could not find prior art to reference. However, combinatorics is a well-studied area, so we expect textbooks may include this a student problem.

Number of bags

A bag is an unordered collection that may contain duplicates. A bag is also called a multiset. Bags differ from a set, which is also unordered but may not contain duplicates, and a sequence, which is ordered and can contain duplicates.

Let \(d\) be the number of distinct element within a bag, \(l\) be the total size or length of a bag, and \(t\) be the number of discrete elements from which we populate bags. All three variables are positive integers.

Theorem 1 The number of bags of length \(l\) with \(d\) discrete elements drawn from \(t\) possible elements, where \(d\), \(l\), \(t\) are all positive integers, \(t \geq d\), and \(l \geq d\) is:

$$ \begin{equation} N(d, l, t) = \binom{t}{d} \binom{l-1}{l-d} \end{equation} $$

Notationally, the parentheses expression is the binomial coefficient.

Aside Equation 1 can also be written as:

$$ N(d, l, t) = \binom{t}{d} \binom{l - 1}{d - 1} $$

This form may be more convenient computationally. The equivalence of the two equations is given by Lemma 1.

Lemma 1 The expression \(\binom{l-1}{l-d} = \binom{l-1}{d-1}\). We can demonstrate the equivalency through first showing the identity \(\binom{n}{k} = \binom{n}{n-k}\):

$$ \binom{n}{k} \stackrel{?}{=} \binom{n}{n-k} $$

$$ \frac{n!}{k!(n-k)!} \stackrel{?}{=} \frac{n!}{(n-k)!(n - n + k)!} $$

$$ \frac{n!}{k!(n-k)!} = \frac{n!}{k!(n-k)!} $$

Using this identity, \(\binom{l-1}{l-d} = \binom{l-1}{d-1}\) can be shown algebraically.

Strategy

Conceptually, we first generate all bags with \(d\) distinct elements from \(t\). Each bag will have length \(d\). Then, for each of these original bags, we generate pads. Each pad is a bag with size \(l-d\) and is restricted to the \(d\) elements in the original, although as a bag these elements may be duplicated. The total number of bags is equal to the number of original bags multiplied by the number of padded bags (or one, if the pads have zero length.)

For example, if there are three types of cards (\(t=3\)), a hand size of five (\(l=5\)), and we restrict to two distinct card types per hand (\(d=2\)), then as shown in the table below, there are three original bags, each with four pads of length 3 (\(l-d\)). The third column shows the original appended with each pad, rewritten into lexicographic order since the order of the hand does not matter. There are 12 total bags as a result.

Original Pads Lexicographic Result
01- -000, -001, -011, -111 00001, 00011, 00111, 01111
02- -000, -002, -022, -222 00002, 00022, 00222, 02222
12- -111, -112, -122, -222 11112, 11122, 11222, 12222

As proof of the equation, we will first discuss the first term and generation of the original bags, and then discuss the second term and generation of the pads.

Original Bags

We generate the original bags by drawing \(d\) unordered elements from \(t\) possibilities. This is the definition of a combination and is computed as \(\binom{t}{d}\). With \(d=l\), the second term \(\binom{l-1}{l-d}\) becomes \(\binom{l-1}{0}\) which simplies to 1, so if there are no pads, the formula is still valid.

Pads

Pads are generated differently than the original bags because they can contain duplicate items. The order of items still does not matter, so this is an example of combinations with replacement or multichoose. Let \(e\) be the size of the individual pads and \(e = l - d\). We are generating \(e\) objects from the \(d\) variants in the corresponding original bag, so by definition (and including translating multichoose to equivalent binomial representation):

$$ \bigg(\negthinspace\negthinspace\binom{d}{e}\negthinspace\negthinspace\bigg) = \binom{d + e -1 }{e} $$

Replacing \(e\) with \(l - d\), we obtain \(\binom{l-1}{l-d}\).

Appendix: Computed Results

This table contains computed results for \(d\) values between 2 and 5, \(l\) values between d and 10, and \(t\) values between d and 10. All ranges are inclusive.

d l t Count
2 2 2 1
2 2 3 3
2 2 4 6
2 2 5 10
2 2 6 15
2 2 7 21
2 2 8 28
2 2 9 36
2 2 10 45
2 3 2 2
2 3 3 6
2 3 4 12
2 3 5 20
2 3 6 30
2 3 7 42
2 3 8 56
2 3 9 72
2 3 10 90
2 4 2 3
2 4 3 9
2 4 4 18
2 4 5 30
2 4 6 45
2 4 7 63
2 4 8 84
2 4 9 108
2 4 10 135
2 5 2 4
2 5 3 12
2 5 4 24
2 5 5 40
2 5 6 60
2 5 7 84
2 5 8 112
2 5 9 144
2 5 10 180
2 6 2 5
2 6 3 15
2 6 4 30
2 6 5 50
2 6 6 75
2 6 7 105
2 6 8 140
2 6 9 180
2 6 10 225
2 7 2 6
2 7 3 18
2 7 4 36
2 7 5 60
2 7 6 90
2 7 7 126
2 7 8 168
2 7 9 216
2 7 10 270
2 8 2 7
2 8 3 21
2 8 4 42
2 8 5 70
2 8 6 105
2 8 7 147
2 8 8 196
2 8 9 252
2 8 10 315
2 9 2 8
2 9 3 24
2 9 4 48
2 9 5 80
2 9 6 120
2 9 7 168
2 9 8 224
2 9 9 288
2 9 10 360
2 10 2 9
2 10 3 27
2 10 4 54
2 10 5 90
2 10 6 135
2 10 7 189
2 10 8 252
2 10 9 324
2 10 10 405
3 3 3 1
3 3 4 4
3 3 5 10
3 3 6 20
3 3 7 35
3 3 8 56
3 3 9 84
3 3 10 120
3 4 3 3
3 4 4 12
3 4 5 30
3 4 6 60
3 4 7 105
3 4 8 168
3 4 9 252
3 4 10 360
3 5 3 6
3 5 4 24
3 5 5 60
3 5 6 120
3 5 7 210
3 5 8 336
3 5 9 504
3 5 10 720
3 6 3 10
3 6 4 40
3 6 5 100
3 6 6 200
3 6 7 350
3 6 8 560
3 6 9 840
3 6 10 1200
3 7 3 15
3 7 4 60
3 7 5 150
3 7 6 300
3 7 7 525
3 7 8 840
3 7 9 1260
3 7 10 1800
3 8 3 21
3 8 4 84
3 8 5 210
3 8 6 420
3 8 7 735
3 8 8 1176
3 8 9 1764
3 8 10 2520
3 9 3 28
3 9 4 112
3 9 5 280
3 9 6 560
3 9 7 980
3 9 8 1568
3 9 9 2352
3 9 10 3360
3 10 3 36
3 10 4 144
3 10 5 360
3 10 6 720
3 10 7 1260
3 10 8 2016
3 10 9 3024
3 10 10 4320
4 4 4 1
4 4 5 5
4 4 6 15
4 4 7 35
4 4 8 70
4 4 9 126
4 4 10 210
4 5 4 4
4 5 5 20
4 5 6 60
4 5 7 140
4 5 8 280
4 5 9 504
4 5 10 840
4 6 4 10
4 6 5 50
4 6 6 150
4 6 7 350
4 6 8 700
4 6 9 1260
4 6 10 2100
4 7 4 20
4 7 5 100
4 7 6 300
4 7 7 700
4 7 8 1400
4 7 9 2520
4 7 10 4200
4 8 4 35
4 8 5 175
4 8 6 525
4 8 7 1225
4 8 8 2450
4 8 9 4410
4 8 10 7350
4 9 4 56
4 9 5 280
4 9 6 840
4 9 7 1960
4 9 8 3920
4 9 9 7056
4 9 10 11760
4 10 4 84
4 10 5 420
4 10 6 1260
4 10 7 2940
4 10 8 5880
4 10 9 10584
4 10 10 17640
5 5 5 1
5 5 6 6
5 5 7 21
5 5 8 56
5 5 9 126
5 5 10 252
5 6 5 5
5 6 6 30
5 6 7 105
5 6 8 280
5 6 9 630
5 6 10 1260
5 7 5 15
5 7 6 90
5 7 7 315
5 7 8 840
5 7 9 1890
5 7 10 3780
5 8 5 35
5 8 6 210
5 8 7 735
5 8 8 1960
5 8 9 4410
5 8 10 8820
5 9 5 70
5 9 6 420
5 9 7 1470
5 9 8 3920
5 9 9 8820
5 9 10 17640
5 10 5 126
5 10 6 756
5 10 7 2646
5 10 8 7056
5 10 9 15876
5 10 10 31752