Professional Documents
Culture Documents
Chapter 5
Chapter 5
———o0o———
Chapter 5: Counting
DISCRETE MATHEMATIS
Hanoi, 2022
1 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Objectives
2 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Content
3 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Content
4 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
5.1 The Basics of Counting
5 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
The Product rule
Example 1
A new company with just two employees, Sanchez and Patel, rents a
floor of a building with 12 offices. How many ways are there to assign
different offices to these two employees?
6 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
The Product rule
Example 2
How many different bit strings of length seven are there?
7 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
The Product rule
Example 2
How many different bit strings of length seven are there?
Example 3
How many different license plates can be made if each plate contains a
sequence of three uppercase English letters followed by three digits
(and no sequences of letters are prohibited, even if they are obscene)?
7 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
The Sum rule
8 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
The Sum rule
Example 5
A student can choose a computer project from one of three lists. The
three lists contain 23, 15, and 19 possible projects, respectively. No
project is on more than one list. How many possible projects are there
to choose from?
8 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
The Sum rule
Example 6
There are 37 faculties, 83 students. A person either a faculty or student
can be choose to attend a committee. How many ways are there to
select such person?
9 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
The Sum rule
Example 6
There are 37 faculties, 83 students. A person either a faculty or student
can be choose to attend a committee. How many ways are there to
select such person?
Example 7
In a version of the computer language BASIC, the name of a variable is
a string of one or two alphanumeric characters, where uppercase and
lowercase letters are not distinguished. (An alphanumeric character is
either one of the 26 English letters or one of the 10 digits). A
one-character variable name must be a letter. Moreover, a variable
name must begin with a letter and must be different from the five
strings of two characters that are reserved for programming use. How
many different variable names are there in this version of BASIC?
9 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
The Inclusion - Exclusion Principle
Explain
Suppose that a task can be done in n1 or n2 ways, but that some
ways in the set of n1 ways are the same as some ways in the set of
n2 ways.
ns : number of ways that are the same in two sets of ways.
Total ways: n = n1 + n2 − ns .
|A ∪ B| = |A| + |B| − |A ∩ B|.
10 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
The Inclusion - Exclusion Principle
Example 8
How many 8-bit strings either start with 1 or end with 00?
11 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
The Inclusion - Exclusion Principle
Example 8
How many 8-bit strings either start with 1 or end with 00?
Example 9
There are 350 applications, in which
A1: 220 people majored in IT.
A2: 147 people majored in business
A3: 51 people majored both in IT and business
How many applicants majored neither in IT nor business?
11 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Tree Diagrams
Counting problem can be solved using Tree Diagram.
Example 10
How many 4-bit strings do not have two consecutive 1s?
12 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Tree Diagrams
Example 10
A playoff between two teams consists of at lost 5 games. The first team
that wins 3 games wins the playoff. In how many different ways can the
playoff occur?
13 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Tree Diagrams
Example 11
T-Shirts come in 5 sizes: S, M, L, XL, XXL. Each size comes in 4
colors, W, R, G, B excepts for XL, which comes only in R, G, B, and
XXL, which comes only in G and B. How many different shirts a
souvenir shop have to stock to have at least one of each available size
and color of the T-shirt?
14 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Content
15 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
5.2 Recurrence Relations (sect. 7.1)
Definition 1
Recurrence relation on the sequence {an }: an is expressed in terms
of one or more of previous items for all n with n ≥ n0 .
{an } is called a solution of recurrence relatio.
Initial conditions: Terms that precede the first item where the
recurrence takes effect.
Example
a0 = 3; a1 = 5; an = an−1 − an−2 , n > 1
Determine whether {an } = 3n, n ≥ 0 is a solution of the recurrence
relation an = 2an−1 − an−2 , n ≥ 2?
16 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Modeling with Recurrence Relations
17 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
The Tower of Hanoi Problem
18 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
The Tower of Hanoi Problem
19 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Content
20 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Divide-and-Conquer Algorithms and Recurrence
Relations
21 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Divide-and-Conquer Recurrence Relations
22 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Recurrence Relations for Binary Search
23 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Recurrence Relations for Binary Search
23 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Recurrence Relations for Finding Maximum of a
sequence
Recurrence Relations for Finding Maximum of a sequence
Procedure max (i, j: integer, ai , a2+1 , . . . , aj : integers)
if i = j then
begin
max: = ai
end
else
begin
m = b(i + j)/2c
max1 = max (i, m, ai , ai+1 , . . . , am )
max2 = max (m + 1, j, am+1 , am+2 , . . . , aj )
if max1 > max2 then max:= max1
else max:=max2
end
24 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Recurrence Relations for Finding Maximum of a
sequence
Recurrence Relations for Finding Maximum of a sequence
Procedure max (i, j: integer, ai , a2+1 , . . . , aj : integers)
if i = j then
begin
max: = ai
end
else
begin
m = b(i + j)/2c
max1 = max (i, m, ai , ai+1 , . . . , am )
max2 = max (m + 1, j, am+1 , am+2 , . . . , aj )
if max1 > max2 then max:= max1
else max:=max2
end
Theorem 1
Let f be an increasing function that satisfies the recurrence relation
f (n) = af (n/b) + c whenever n is divisible by b, where a ≥ 1, b is an
integer greater than 1, and c is a positive real number. Then f (n) is
(
O(nlogb a ) if a > 1
O(log n) if a = 1
f (n) = C1 nlogb a + C2 ,
25 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Divide-and-Conquer Recurrence Relations
Using Theorem 1
f (n) = 5f (n/2) + 3, f (1) = 7. Find f (2k ), k is a positive integer.
Estimate f (n) if f is increasing function.
Estimate the number of comparisons used by a binary search
Estimate the number of comparisons to locate the maximum
element in a sequence
26 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Divide-and-Conquer Recurrence Relations
Theorem 2
Let f be an increasing function that satisfies the recurrence relation
f (n) = af (n/b) + cnd . Whenever n = bk , where k is a positive integer,
a ≥ 1, b is an integer greater than 1, and c and d are real numbers with
c positive and d nonnegative. Then f (n) is
d d
O(n ) if a < b
O(nd log n) if a = bd
O(nlogb a ) if a > bd
Example
The number of comparisons used by the merge sort to sort a list of n
elements is less than M (n), where M (n) = 2M (n/2) + n. Use the
master theorem (Theorem 2) to find M (n).
27 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting