Search

Search papers and lessons

Multiset

Level 1 · 11 min read

A multiset is a collection where an object can appear several times and the count of each object matters, so {1, 1, 2} and {1, 2} differ.

On this page

  • Colour = topic mix
  • Discrete math
  • ReadReady to readNot yet

TL;DR

A multiset is a collection of objects in which an object can appear more than once, and what counts is how many copies of each object there are. It is the mathematical version of a bag: a bag holding two apples and one pear is different from a bag holding one apple and one pear, even though both contain apples and pears. A multiset generalises a set, which keeps only one copy of each object.

Builds on: Set Operations · Leads to: counting with repeats, graph neural networks

Where it's used

  1. Counting arrangements of letters. The word BANANA has the letters A, A, A, B, N, N. The multiset {A, A, A, B, N, N} captures exactly which letters are present and how many times. Counting the distinct words you can make from those letters is a standard problem in Counting Principles, and the answer depends on the multiplicities, not only on which letters appear.

  2. Representing text as word counts. In the bag-of-words model of text, a document is represented by how many times each word occurs, not by the order of the words. Two documents with the same words in a different order get the same representation. This is a multiset in which order is ignored and repeats are kept.

  3. Describing neighbourhoods in graph neural networks. A node in a graph can have several neighbours with identical features, and removing or doubling one of them changes the information. The GIN paper treats a node's neighbour features as a multiset for its aggregation, so repeated neighbours are counted rather than collapsed into one.

How it works

Take the fruit bag with two apples and one pear. A multiset stores exactly what a Histogram stores, a count per item, so we will count its contents, compare it with another bag, and combine the two.

  1. List each object with its count. Apple appears 2 times and pear appears 1 time. Check: 2 plus 1 is 3, the number of fruits in the bag.
  2. Find the size. The size of a multiset counts repeats, so the size is 3. A set of the same fruits would have size 2, because it would list apple and pear once each. Check: size 3 is not the same as the set size 2.
  3. Test for membership. Apple is in the bag. Kiwi is not. Membership tells you only whether an object is present at all. Check: the count for kiwi is 0, which is the same as absence.
  4. Compare with a second bag. Take a second bag with one pear and one kiwi. The two bags differ in the number of apples, so they are different multisets, even though they share a pear.
  5. Combine the bags by adding counts. Adding the two bags gives apple 2, pear 2, kiwi 1. Check: the total size is 3 plus 2, which is 5, and the counts 2 + 2 + 1 also give 5.
ObjectCount in bag 1Count in bag 2Count in the sum
apple202
pear112
kiwi011

The table is the whole idea. A multiset is a list of objects with a count attached to each, and every operation is an operation on those counts.

The math

Definition: Multiset

Let U be a collection of objects, called the universe. A multiset M over U is given by a Function m from U to the whole numbers 0, 1, 2 and so on. The value m(x) is the multiplicity of x, the number of copies of x in M. An object with multiplicity 0 is absent. The size of M is the sum of all multiplicities over the universe.

Two multisets are equal exactly when every object has the same multiplicity in both. So {1, 1, 2} is not equal to {1, 2}, because 1 has multiplicity 2 in the first and 1 in the second.

Steps 1 and 2 in symbols: counts and size

For the fruit bag, m(apple) = 2, m(pear) = 1 and m(kiwi) = 0. The size is written

∣M∣=∑x∈Um(x)=2+1+0=3,|M| = \sum_{x \in U} m(x) = 2 + 1 + 0 = 3,

where the sum runs over the universe of fruits. The size of the set of distinct fruits, written with the same symbols, would be the number of objects with multiplicity at least 1, which is 2.

Step 5 in symbols: combining counts

The sum of two multisets M and N is the multiset whose multiplicities add:

(m+n)(x)=m(x)+n(x).(m + n)(x) = m(x) + n(x).

Its size is |M| + |N|, which checks step 5: 3 + 2 = 5. Two other common combinations are the maximum and the minimum of the multiplicities, written with the symbols max and min. The maximum version of the union of the two fruit bags gives apple 2, pear 1, kiwi 1, of size 4. Which combination is meant must always be stated, because the word "union" for multisets is used in more than one way.

A counting formula

The number of multisets of size k that can be built from n kinds of object, where repeats are allowed and order is ignored, is

(n+k−1k).\binom{n + k - 1}{k}.

Check with n = 3 kinds, A, B and C, and k = 2 items: the binomial coefficient is \binom{4}{2} = 6. The six multisets are AA, BB, CC, AB, AC and BC. Listing them confirms the formula.

Worked examples

Example
Example 1: does repetition matter?

Setup: compare the multiset {1, 1, 2} with the set {1, 2}.

  1. List the multiset with counts. The number 1 has multiplicity 2, and the number 2 has multiplicity 1.
  2. List the set. The set {1, 2} keeps one copy of each element, so 1 and 2 each have multiplicity 1.
  3. Compare multiplicities. The number 1 has multiplicity 2 in the multiset and 1 in the set, so the two are not equal as multisets.
  4. Compare as sets. Ignoring multiplicity, both contain exactly the numbers 1 and 2, so they are equal as sets.
  5. Interpret. The multiset keeps the information that 1 appears twice. The set throws it away. Use a multiset when that information is needed.
Example
Example 2: two bags, three combinations

Setup: M has a appearing 2 times and b appearing 1 time. N has a appearing 1 time, b appearing 2 times and c appearing 1 time. The minimum in step 3 is the intersection of the two bags.

  1. Sum. Add multiplicities: a gets 2 + 1 = 3, b gets 1 + 2 = 3, c gets 0 + 1 = 1. The size is 7, which is 3 plus 4.
  2. Maximum. Take the larger multiplicity: a gets max(2, 1) = 2, b gets max(1, 2) = 2, c gets max(0, 1) = 1. The size is 5.
  3. Minimum, which is the intersection. Take the smaller multiplicity: a gets min(2, 1) = 1, b gets min(1, 2) = 1, c gets min(0, 1) = 0. The size is 2, and the result is {a, b}.
  4. Compare the three results. They differ in size: 7, 5 and 2. The sum counts everything that was held, the maximum counts each object as often as the more frequent bag, and the minimum keeps only what both bags share.
  5. Interpret. Pick the operation that answers your question. If you want the total stock of items across two warehouses, use the sum. If you want the most of each item that one warehouse ever held, use the maximum.
Example
Example 3: arrangements of letters

Setup: how many different words can be made by rearranging the letters of BANANA? The letters form the multiset {A, A, A, B, N, N}. This is a counting problem with repeats.

  1. Count the letters. There are 6 letters in total, with A three times, N twice and B once.
  2. Count all orderings as if the letters were distinct. That is 6! = 720 orderings.
  3. Correct for the repeated A's. The three A's can be swapped among themselves without changing the word, and there are 3! = 6 ways to do that. Divide by 6.
  4. Correct for the repeated N's. The two N's can be swapped in 2! = 2 ways without changing the word. Divide by 2.
  5. Correct for B, which appears once. Dividing by 1! changes nothing.
  6. Compute the result. 720 divided by (6 times 2 times 1) is 720 divided by 12, which is 60.
  7. Interpret. There are 60 distinct arrangements of the letters. The multiplicities, not just the set of letters, determine the answer.

Try it: how many distinct arrangements are there of the letters of MISSISSIPPI? The letters form a multiset with M once, I four times, S four times and P twice.

Answer: there are 11 letters in total, so start from 11!. Divide by 4! for the I's, by 4! for the S's and by 2! for the P's: 11! divided by (4! times 4! times 2!), which is 39916800 divided by 1152, equal to 34650.

Common mistakes and tradeoffs

It is tempting to think that a multiset is just a set with extra notation. In fact the count of each object is part of the identity of a multiset, so {1, 1, 2} and {1, 2} are different multisets even though they are the same set. Repeats must be kept whenever they matter for the question.

It is tempting to think that a multiset keeps the order of its items, as a list does. In fact a multiset ignores order: the bag with an apple then a pear is the same bag as a pear then an apple. If order matters, use a Sequence (a list or a tuple), which keeps order and repeats.

It is tempting to assume that union means the same thing for multisets as for sets. In fact union can mean the sum, the maximum, or the minimum of the counts, and these give different answers. State which one you use.

It is tempting to count multisets with the formula for ordered choices with repetition, n to the power k. In fact the formula n to the power k counts ordered lists, and the multiset count is \binom{n + k - 1}{k}, which ignores order.

Use a multiset when repeats matter and order does not, as in word counts, letter arrangements and neighbourhoods. Use a set when repeats are noise. Use a list when order matters. Alternatives to consider are a dictionary from each object to its count, which is the same idea written as a program (often a hash table), and a Power Set, which collects subsets rather than repeated items.

History

Set theory was founded in the 1870s by Georg Cantor, who studied collections of distinct points and numbers. Its basic operations were developed in the decades that followed, and the emphasis on distinct elements became the default in mathematics.

Problems with repeated items, such as counting arrangements of letters, were studied for a long time before the idea was given a formal name. The definition as a function from objects to counts is the modern standard, and it makes the sum, maximum and minimum precise.

Recap

  • A multiset is a collection in which each object has a multiplicity, the number of copies it has.
  • The size of a multiset counts repeats, so a set and its multiset version can have different sizes.
  • Operations act on multiplicities: the sum adds counts, the maximum takes the larger and the minimum takes the smaller.
  • The number of multisets of size k from n kinds of object is \binom{n + k - 1}{k}.
  • Next, see counting principles for arrangements with repeats, and the GIN paper for multisets in graph learning.

Check yourself

QuestionIs the multiset {a, a, b} equal to the multiset {a, b, b}? Explain in one sentence.Show answer

No. Object a has multiplicity 2 in the first and 1 in the second, so the multiplicities differ, and multisets are equal only when every multiplicity matches.

QuestionTwo multisets are M = {x, x, y} and N = {x, y, y, y}. What is the size of the sum M + N, and what is the multiplicity of y in it?Show answer

The sum adds multiplicities. Object x gets 2 + 1 = 3 and object y gets 1 + 3 = 4. The multiplicity of y is 4, and the size is 3 + 4 = 7.

QuestionWhy does the number of distinct arrangements of BANANA come out to 60 rather than 720?Show answer

Only 720 orderings would be counted if all six letters were distinct. The three A's and two N's are identical, so swapping them does not produce a new word. Dividing by 3! and 2! removes those duplicates, leaving 720 divided by 12, which is 60.

QuestionHow many multisets of size 3 can be made from 4 kinds of object?Show answer

Use the formula \binom{n + k - 1}{k} with n = 4 and k = 3. That gives \binom{6}{3} = 20.

Sources

Multiset — explained simply · ungoon.me