Counting
Contents
6.1. Counting#
How do we count things?
Using only letters and numbers, there are
6.1.1. The Principles of Counting#
There are many algorithmic ways of counting. Here we look at 3.
Product Rule#
The product rule is the easiest method to understand.
Definition (product rule)
Given two choices,
Here’s a very simple example. Consider the set
That’s a lot. Or, we could apply the product rule.
For the first term of the sequence we have three possible choices:
So, the total possible combination of choices is
The Product Rule for Sets#
The product rule can be made formal by considering sets and their Cartesian product.
Given two sets
This follows naturally from the product rule. The Cartesian product
Let’s generalize this to any number of sets.
Let
Counting License Plates
Consider a province where the license plate format is
We could proceed intuitively using the product rule. There are
We could also formalize this procedure.
Let
A license plate of the form
From the product rule applied to sets:
Sum Rule#
The sum rule applies when you must make a single choice from multiple groups.
Definition (sum rule)
Given a choice, where one can either choose from a group of
This rule is actually quite simple and obvious. Consider picking your outfit of the day. In your closet you have 10 pairs of jeans and 5 pairs of chinos. How many different choices of pants do you have? 15, of course. You can wear one of your 10 pairs of jeans or one of your 5 pairs of chinos (but not both).
Counting Pets
You are adopting one pet from the local SPCA. Good job!
The shelter currently houses
You can either choose a cat or choose a dog. Therefore, there are
The Sum Rule for Sets#
Given two sets
This follows naturally from the union operation of disjoint sets. If
Let’s generalize this to any number of sets.
Let
Sum and Product, together#
Of course, one may use the sum and product rule together.
Say you have to make one choice from group
For the first choice, there are
Here’s an example.
Counting Programming Language Variables
In most programming languages, the first character of a variable’s name must be a letter. From then on, characters may be numbers of letters. Assuming variables are case-sensitive, how many possible 4-character variable names are there?
There are 26 lower-case letters and 26 upper-case letters. There are also 10 individual decimal digit characters.
For a 4-character variable name we have 4 choices.
The first choice must be a letter and thus there are
In total, we have:
possible 4-character variable names.
Counting rules
In some computer system, accounts must have a case-insensitive password of 6 to 8 characters in length. Those characters must only be letters, numbers, or one of
How many possible passwords are there to choose from?
Counting rules
For each character we can choose a letter, a number, or a special character. This is
Passwords can be either 6, 7, or 8 characters long.
All length-6 passwords count:
All length-7 passwords count:
All length-8 passwords count:
Therefore, the total number of possible passwords is:
The Principle of Inclusion-Exclusion#
The sum rule assumes that the sets from which you are choosing are disjoint. This is a strong assumption. When the sets to choose from are not disjoint, special care is needed so that choices are not double counted.
We may recall the following proposition from the section on Union and Intersection.
Proposition
For two sets
When
More generally, the inclusion-exclusion principle applies to exclude items which are double counted, but then include items which have been doubly excluded.
This is easiest to see with the union of three sets.
Fig. 6.1 A Venn diagram of three sets.#
If we want to count the number of elements in the union of
This formula says the following about
If
, , and are all disjoint, then , as expected by the sum rule.If they are not disjoint, then
double counts elements in the overlap of the sets. Thus, we remove one count for each element of , , and .However, in this removal process, we have removed too many counts for the elements of
. Notice these elements were triple counted by . However, we have removed a total of three counts for each of those elements as they are all individually elements of , , and . Thus, we must add back in .
Generalizing the inclusion exclusion principle to many more sets is certainly possible, but the formula is unwieldy. Rather, consider a recursive construction.
Let
Putting all of these formulas together, we have:
Science Fair Inclusion-Exclusion
Your high school offers Biology, Chemistry, and Physics courses.
There are
You are looking for a partner for your school’s science fair. You are enrolled in Biology and Chemistry, and so you want your partner to also be enrolled in Biology and Chemistry. How many suitable partners are there to choose from?
Science Fair Inclusion-Exclusion
Let
We also know:
From inclusion-exclusion:
Therefore, we have:
Finally, since we know ourselves belong to
6.1.2. The Pigeon Hole Principle#
Fig. 6.2 A pigeon in a cubby-hole.#
If there are 9 cubbys and 10 pigeons, there must be a cubby with more than one pigeon.
This is a simple idea that applies to more than just pigeons. If you have 9 lockers and 10 book bags, a locker must have more than one book bag.
This idea, when generalized, is the pigeon hole principle.
Theorem (pigeon hole principle)
Let
Proof:
Proceed by contrapositive.
We look to prove that if every box contains fewer than 2 objects,
then there are
The pigeon hole principle is perhaps the most simple proof you will see in this course. But, it is very powerful with many applications.
Birthdays and pigeons
Proposition
In any group of 367 people, at least two people have the same birthday
Proof: At most, there are 366 days in a year (because of leap years). Each of these 367 people are born on a particular day of those 366. Thus, by the pigeon hole principle, at least two people share the same birthday.
Here’s a more interesting and math-y example.
Divisors and pigeons
Proposition
For every integer
Proof:
Consider the list of
where the last number has
By Euclidean division with
We divide each of the numbers in our list by
The difference of these two numbers in our list is certainly divisible by
Pigeon holes as functions#
Recall from Injective, Surjective, Bijective an important fact about bijective functions.
If a bijection exists between sets
Recall that functions are always total.
For
Let
No matter how you define
Such a function from
6.1.3. Generalized Pigeon Hole#
We can extend the pigeon hole principle to get an even stronger result. Rather than having “at least two objects are in the same box”, we can actually count the maximum number of objects in any box.
Theorem (generalized pigeon hole principle)
If
Proof:
Proceed by contrapositive. Suppose that none of the
However, notice that:
Thus, there are strictly less than
Birthdays and generalized pigeons
In a group of
Why? Generalized pigeon hole principle. There are
Try one yourself now.
Dice and pigeons
How many times must you roll a pair of
standard 6-sided dice to guarantee
that you roll a
Dice and pigeons
When you roll a pair of dice, there are
To guarantee you roll a
We can only guarantee that some outcome from the list of possible outcomes appears at least three times. For that,
if we roll the dice 23 times (
6.1.4. Exercises#
Exercise 6.1
Recall that a byte consists of 8 binary digits.
How many different bytes are there
that begin with
Solution to Exercise 6.1
In a byte there are 8 bits, and each bit belongs to
When a byte begins with
When a byte ends with
Now, how many bytes both start with
In total we have
Exercise 6.2
On the internet, every computer must have an IP address.
In Version 4 of the IP standard, there are 3 possible “classes” of IP addresses for computers connected to the internet. Class A is a 31-bit binary number. Class B is a 30-bit binary number. Class C is a 29-bit binary number.
If no IP address can have every bit assigned to 1, nor every bit assigned to 0, how many possible IP addresses are there for computers connected to the internet?
Solution to Exercise 6.2
Class A counts
Class B counts
Class C counts
We subtract 2 for the addresses containing only 1s and only 0s.
Therefore, the total number of addresses is:
Exercise 6.3
How many cards must you draw from a standard 52-card deck
to guarantee that you will have
Solution to Exercise 6.3
There are 4 suits in a standard deck of cards. If we want
to guarantee that we get 4 cards of the same suit,
we must draw
Exercise 6.4
9 sports teams participate in a round robin, where every teams plays every other team, and ties are not allowed. Prove that if no team loses all of its games, then there must be at least two teams that end up with the same number of wins.
Solution to Exercise 6.4
Each teams plays 8 games. Every team has at least 1 win.
So 9 teams are pigeons, the holes are the number of wins:
Exercise 6.5
Let
Solution to Exercise 6.5
There are
Exercise 6.6
The generalized pigeon hole principle can be stated using sets, functions, and the number of pre-images of a particular image.
Let
What is the contrapositive of this definition? Note that the contrapositive is equivalent.
Solution to Exercise 6.6
Let
Exercise 6.7
A class of 42 students is required to break into 12 groups of size no more than 6. Show that there are at least 5 groups of 3 or more students. (Hint: consider a proof by contradiction on the contrapositive from Exercise 6.6).
Solution to Exercise 6.7
Assume for contradiction that there are at most 4 groups of 3 or more students. Thus, there are
This is actually the “contrapositive form” of pigeon-hole principle.