Discrete Structures
§2 Counting Principles
7 min read
Counting problems become easier when we describe the objects as sets and then find their cardinalities. The main question is always the same: what choices build one valid object, and can any object be counted more than once?
Cardinality
The cardinality of a finite set , written , is the number of elements in . For example,
The empty set has no elements, while a set containing the empty set has one element:
The vertical bars in mean cardinality here, not absolute value.
The product principle
The product principle, also called the multiplication principle, applies when an object is built through a sequence of choices. If the first choice has options and the second has options, then the pair of choices has
possible outcomes. The same idea extends to any finite number of steps: multiply the number of options available at each step.
For finite sets and , this gives the cardinality of their Cartesian product:
If and , then
Example: Canadian postal codes
Let be the set of 26 uppercase letters and the set of 10 digits:
A Canadian postal code has the pattern
There are three letter positions and three digit positions. Since the six choices are independent and repetition is allowed, the number of possible codes is
For example, Santa's postal code H0H 0H0 follows this pattern. Each round character is the digit zero, not the letter O.
Why a power set has size
The power set contains every subset of . Suppose
To construct a subset , make one choice for each element :
- include in ; or
- leave out of .
There are two choices for each of the elements, so the product principle gives
This also works for the empty set:
because .
The sum principle
Two sets are disjoint when they have no elements in common:
For disjoint finite sets, the sum principle, also called the addition principle, says
Nothing is counted twice because the sets do not overlap. More generally, the cardinalities of any finite collection of pairwise-disjoint sets can be added.
Example: usernames of length 6, 7, or 8
Suppose a username may contain uppercase letters, lowercase letters, and digits. Let
where , , and are the three character sets. They are disjoint, so there are
allowed characters.
Let be the set of valid strings of length . Repetition is allowed, so the product principle gives
A string cannot have two different lengths, so , , and are disjoint. The sum principle therefore gives
The complement principle
Suppose . The complement principle counts the elements of that are not in :
This follows from the disjoint decomposition
The sets and do not overlap, so the sum principle gives
Rearranging produces the complement principle.
Example: usernames containing at least one digit
Let be the set of all valid usernames of length 6, 7, or 8. Let be the set of usernames containing no digits.
There are 62 allowed alphanumeric characters in total, but a username with no digits may use only the 52 uppercase and lowercase letters. Therefore,
and
The set contains exactly the usernames with at least one digit. By the complement principle,
Inclusion–exclusion for two sets
The sum principle cannot be applied directly when and overlap. In , every element of is counted once as part of and again as part of .
The inclusion–exclusion principle corrects this double count:
We can also derive the formula by splitting the union into three disjoint regions:
Their cardinalities add:
Example: bit strings
A bit is either or . How many bit strings of length 8 start with or end with ?
Let
and
For , the first bit is fixed and the remaining seven bits are free, so
For , the last two bits are fixed and the remaining six are free, so
A string in starts with and ends with . Five positions remain free, so
By inclusion–exclusion,
Inclusion–exclusion for three or more sets
For three finite sets, inclusion–exclusion becomes
First add the three individual cardinalities. This overcounts every pairwise overlap, so subtract all three pairwise intersections. An element in all three sets was then added three times and subtracted three times, leaving it uncounted; add the triple intersection once.
The same alternating pattern continues for more sets:
- add all one-set cardinalities;
- subtract all pairwise intersections;
- add all triple intersections;
- subtract all four-set intersections;
- continue alternating signs.
For four sets , , , and , this means adding the four individual sizes, subtracting all six pairwise intersections, adding all four triple intersections, and subtracting .
Example: integers with none of three properties
How many integers from 1 through 1000 are neither perfect squares, nor perfect cubes, nor multiples of 7?
Let
and define
Thus, is the set of perfect squares, the set of perfect cubes, and the set of multiples of 7.
The floor function is the greatest integer less than or equal to . It lets us count the possible integer bases:
and
Now count the intersections:
- A number that is both a square and a cube is a sixth power, so
- A square divisible by 7 has a square root divisible by 7. Among , there are four multiples of 7, so
- A cube divisible by 7 has a cube root divisible by 7. Among , only qualifies, so
- A number in all three sets would be a sixth power whose base is divisible by 7. The smallest is , so
Inclusion–exclusion gives
We want the integers in none of the three sets, which form the complement of this union in :
0 reads