Unit05
Unit05
Overview
• Sum rule
• Product rule
• Principle of Inclusion-Exclusion
• Pigeonhole principle
• Generalized pigeonhole principle
3
Counting Problems
• Counting problems arise on many occasions in Computer
Science.
Examples:
• A computer password consists of 6, 7, or 8 characters,
each must be a digit or a letter.
A password must also contain at least one digit.
How many passwords are there?
Answer: 3+5 = 8.
NB. This is true only if the two schools do not offer a joint
course. Otherwise, 3+5 is over-counted!
5
Examples
How many functions are there from a set A with m elements
to a set B with n elements?
Solution:
• Let A = {a1, a2, …, am}.
• The following procedure generates a function f from A to B:
m steps: T1, T2, …, Tm
where Ti assigns an element of B to ai.
• Each step can be done in n ways.
• There are nm ways to generate f.
Solution:
• Let P be the total number of such passwords.
• Let P6, P7, and P8 denote the number of passwords of length
6, 7, and 8, respectively.
x
What is P6 ? P6
No 1 6
• 62 possible characters: y …
digit digit digits
➢26 uppercase letters
➢26 lowercase letters
➢10 digits
• The number of all possible 6-character strings is 626,
which is denoted by x.
• The number of 6-character strings containing no digit is
526, which is denoted by y.
• P6 = the number of 6-character strings containing at least
one digit.
• By the sum rule, x = P6 + y.
• Therefore, P6 = 626 - 526 = 37,029,625,920.
11
Therefore,
• P = P6 + P7 + P8
= 167,410,949,583,040.
12
0100000110
• For any integer i ≤ 10, let Pi be the number of length-10
strings containing 5 consecutive 0’s starting from the
position i.
• Answer = P1 + P2 + P3 + P4 + P5 + P6
13
• P1 = 25 = 32 0000011000
• P2 = ? 1000001100
• P3 = ? 0100000110
• Answer = ?
14
Solution:
• The number of choices for the 1st character is 1.
• Answer = 1 63 = 63
15
Solution:
• The number of choices for the 1st character is 2.
Over Counting
• How many bit strings of length 8
➢ start with 1; or
➢ end with 00 ?
Principle of Inclusion-Exclusion
• Let A1 be the set of ways a task T1 can be done, and
let A2 be the set of ways a task T2 can be done.
• Note that A1 may overlap A2, i.e., there are some ways that
satisfy both T1 and T2.
• Then, the number of ways to do T1 or T2 is
Another example
• How many bit strings of length 10 contain 5 consecutive 0’s
or 5 consecutive 1’s?
Solution:
• 5 consecutive 0’s: 112
• 5 consecutive 1’s: 112
• 5 consecutive 0’s and 5 consecutive 1’s: 2
• Answer = 112 + 112 – 2 = 222
19
Example
• In a class, 15 students study C programming;
20 students study Java; 13 students study Python;
5 students study both C and Java;
7 students study both C and Python;
4 students study both Java and Python; and
no students study all the three programming languages.
• What is the number of students in the class?
Solution:
• Let C, J, P be the set of students on C, Java, Python, respectively.
• The number of students in the class is
21
Friends or not
• There are 6 people inside the elevator. Suppose that if a
person A knows a person B, then B also knows A.
True or false?
• There are at least 2 people who know each other or don’t
know each other. True
Example:
• 10 pigeons
in 9 pigeonholes
23
• A contradiction occurs.
24
• A contradiction occurs.
25
Example 1
Find the minimum number of people needed such that three
of them were born on the same day of the week?
Solution:
• There are 7 days of week, namely, Monday, …, Sunday.
• We need to find the minimum number n of people such that
by generalized pigeonhole principle,
one of these days of week has at least people.
• Therefore, n = 72 + 1 = 15.
Example 2
Find the minimum number of people needed such that four of
them were born in the same month?
Solution:
• There are 12 months.
• We need to find the minimum number n of people such that
by generalized pigeonhole principle,
one of these months has at least people.
• Therefore, n = 123 + 1 = 37.
Example 3
Why does any set of 10 non-empty strings over the alphabet
{a, b, c} contain two different strings with the same starting
character and the same ending character?
Solution:
• Given any non-empty string, let x and y be its starting and
ending characters.
• (x, y) has 3 3 = 9 distinct values.
• By pigeonhole principle, in any set of 10 non-empty strings,
there must be two different strings with the same
(x, y) values, i.e., they have the same starting characters
and the same ending characters.
28
Example 4
Find the minimum number of non-empty strings over {a, b, c, d}
such that three of them have the same starting character and
the same ending character.
Solution:
• Given any non-empty string, let x and y be its starting and
ending characters.
• (x, y) has 4 4 = 16 distinct values.
• We need to find the minimum number n of non-empty strings
such that by generalized pigeonhole principle,
one of these (x, y) pair values has at least strings.
• Therefore, n = 162 + 1 = 33.
29
• Trivial fact: For any two persons, either they know each
other or they don’t know each other.
True or false?
• Out of any 6 people, there are at least 4 people who know
each other or don’t know each other.
30
Why?
• The five people can be classified into 2 groups, i.e., friends of A
and not friends of A.
• By the generalized pigeonhole principle,
one group has a size
31