0% found this document useful (0 votes)
2 views31 pages

Unit05

Uploaded by

holyshitjk48
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views31 pages

Unit05

Uploaded by

holyshitjk48
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

1

COMP S264F Unit 5:


Basics of Counting

Dr. Keith Lee


School of Science and Technology
Hong Kong Metropolitan University
2

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?

• Given a complicated nested loop involving conditionals,


count the number of iterations.
4

Basics of Counting: Sum and Product


• A student can choose an elective course (e.g., general
education course) from two schools.
• These schools offer 3 and 5 courses, respectively.

How many possible courses are there to choose from?

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

The Sum Rule


Suppose that a task can be done either in one of x1 ways
or in one of x2 ways, where
none of the x1 ways is the same as any one of the x2 ways
(i.e., the two sets of ways are disjoint).

Then there are x1 + x2 ways to do the task.


6

The Product Rule


How many different license plates are available if each
contains two letters followed by 4 digits?

Answer: 262610101010 = 6,760,000

Suppose that a procedure can be broken in n steps


T1, T2, …, Tn, and
Ti can be done in yi ways after T1, T2, …, Ti-1 have been
done (in whatever ways).

Then there are y1 × y2 × … × yn ways to do the procedure.


7

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.

How many one-to-one functions are there?


8

An old example (Unit 3 Slide 31)


• Let A be a set of n elements.
• What is the cardinality of the set
{ (X, Y) | X  A, Y  A, X  Y =  }?
Solution:
• Let A = {a1, a2, …, an}.
• Consider the following procedure for choosing an ordered pair (X, Y):
n steps: T1, T2, …, Tn
where Ti puts ai into one of the followings:
➢X
➢Y
➢ none

• Each Ti can be done in 3 different ways.


• The procedure can be done in 3n ways.
9

Using both Sum rule & Produce rule


Example: A computer password consists of 6, 7, 8 characters,
each must be a digit or a letter. A password must also contain
at least one digit. How many passwords are there?

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.

• By the sum rule, P = P6 + P7 + P8.


10

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

• P6 = 626 - 526 = 37,029,625,920.


Similarly,
• P7 = 627 - 527 = 2,493,542,903,680.
• P8 = 628 - 528 = 164,880,377,053,440.

Therefore,
• P = P6 + P7 + P8
= 167,410,949,583,040.
12

How many bit strings of length 10 contain


5 consecutive 0’s?

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

How many bit strings of length 10 contain


5 consecutive 0’s?
• Answer = P1 + P2 + P3 + P4 + P5 + P6
where Pi is the number of strings of length 10 containing 5
consecutive 0’s with the first occurrence of 5 consecutive
0’s starting from position i.

• P1 = 25 = 32 0000011000

• P2 = ? 1000001100

• P3 = ? 0100000110

• Answer = ?
14

More counting examples


Calculate the number of length-7 strings over the alphabet {a, b}
that begin with an a, and contain at least one b.

Solution:
• The number of choices for the 1st character is 1.

• b can only occur after the 1st character.


• The number of choices for the other 6 characters
= (number of all possible length-6 strings)
- (number of all length-6 strings without any b)
= 26 - 1
= 64 - 1 = 63

• Answer = 1  63 = 63
15

More counting examples (cont’)


Calculate the number of length-6 strings over the alphabet {a, b, c}
that begin with an a or b, and contain at least one c.

Solution:
• The number of choices for the 1st character is 2.

• c can only occur after the 1st character.


• The number of choices for the other 5 characters
= (number of all possible length-5 strings)
- (number of all length-5 strings without any c)
= 3 5 - 25
= 243 - 32 = 211

• Answer = 2  211 = 422


16

Over Counting
• How many bit strings of length 8
➢ start with 1; or
➢ end with 00 ?

• Number of bit strings starting with 1 = 27 = 128.


• Number of bit strings ending with 00 = 26 = 64.
• Answer = 128 + 64?

• No. The string 11110000 is double counted!


17

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

Example: Double counted


• Number of bit strings of length 8
that start with 1 and end with 00
= 25 = 32
• Number of bit strings of length 8
that start with 1 or end with 00
= 128 + 64 - 32 = 160
18

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

Principle of Inclusion-Exclusion for 3 sets


Let A, B, C be three sets. Then,

NB. Number in a subset


= Number of times that subset is counted.
20

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

• There are at least 3 people who know each other or don’t


know each other.

• There are at least 4 people who know each other or don’t


know each other.
22

The Pigeonhole Principle


Theorem: If k+1 or more objects are placed into k boxes,
then there is at least one box containing two or more of the
objects.

Example:
• 10 pigeons
in 9 pigeonholes
23

The Pigeonhole Principle: Proof


• Consider any way of placing k+1 or more objects into k boxes.
(I.e., the boxes contain at least k+1 objects in total.)

• Suppose, for the sake of contradiction, that no box contains


two or more objects.
1 2 3 k

≤1 ≤1 ≤1 ≤1

• Let us sum up the objects in the k boxes.


The sum is at most k.

• A contradiction occurs.
24

Generalized pigeonhole principle


If n objects are placed into k boxes, then
there is at least one box containing at least objects.

Proof: Again, by contradiction. The smallest integer

• Suppose there is not any box containing


at least objects.
• I.e., the number of objects in every box is ,
or equivalently,

• Thus, the total number of objects in the k boxes is

• 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 = 72 + 1 = 15.

To check that the minimum n is 15, consider the case with


14 people. It is possible that each day of week has exactly 2
people.
26

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 = 123 + 1 = 37.

To check that the minimum n is 37, consider the case with 36


people. It is possible that each month has exactly 3 people.
27

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 = 162 + 1 = 33.
29

Friends or not: Proof


• Assumption: If a person A knows a person B, then B also
knows A. In this case, we say that A and B are friends.

• 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

Friends or not: Proof


Claim: Out of any 6 people, there are at least 3 people who know
each other or don’t know each other.
Proof.
• Let A be one of the six people.
• Of the other five people, there are
➢ either three or more who know A (are friends of A),
➢ or three or more who don’t know A (are not friends of A).

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

Friends or not: Proof (cont’)


Let us first consider the case when A has
B 3 or more friends.
A Name 3 of them as B, C, and D.
C
• If two of B, C, and D are friends, then
D
these two and A form a group who know
each other.
• If any two of B, C, and D are not friends,
then B, C, D form a group who don’t
know each other.

The other case when A does not have 3


or more friends is symmetric. Fill in the
details yourself.

You might also like