Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

FPT UNIVERSITY

———o0o———

Chapter 5: Counting

DISCRETE MATHEMATIS

Lecture: Dr. Nguyen Kieu Linh

Hanoi, 2022

1 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Objectives

Some questions we can face


How can we determine the complexity of an algorithm?
How many cases possible are there to arrange n objects?
How can we generate input data from given initial data?
A password consists of six characters. How many such password
with some criteria?
How many bit strings of length n that do not have two consecutive
0s?

2 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Content

1 5.1 The Basics of Counting

2 5.2 Recurrence Relations (sect. 7.1)

3 5.3 Divide-and-Conquer Algorithms and Recurrence Relations

3 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Content

1 5.1 The Basics of Counting

2 5.2 Recurrence Relations (sect. 7.1)

3 5.3 Divide-and-Conquer Algorithms and Recurrence Relations

4 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
5.1 The Basics of Counting

Basic Counting Principles


Product rule
Sum rule
The Inclusion-Exclusion Principle
Tree Diagrams

5 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
The Product rule

THE PRODUCT RULE


Suppose that a procedure can be broken down into a sequence of k
tasks. If there are
n1 ways to do the first task
n2 ways to do the second task
......
nk ways to do the k-th task
then there are n1 n2 . . . nk ways to do the procedure.

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

THE SUM RULE


If a procedure can be done either in one of k tasks:
n1 ways to do the first task
n2 ways to do the second task
......
nk ways to do the k-th task
then there are n1 + n2 + . . . + nk ways to do the procedure.

8 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
The Sum rule

THE SUM RULE


If a procedure can be done either in one of k tasks:
n1 ways to do the first task
n2 ways to do the second task
......
nk ways to do the k-th task
then there are n1 + n2 + . . . + nk ways to do the procedure.

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

The Inclusion - Exclusion Principle (The subtraction rule)


If a task can be done in either n1 ways or n2 ways, then the number of
ways to do the task is n1 + n2 minus the number of ways to do the task
that are common to the two different ways.

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

1 5.1 The Basics of Counting

2 5.2 Recurrence Relations (sect. 7.1)

3 5.3 Divide-and-Conquer Algorithms and Recurrence Relations

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

Compound interest Problem


P0 : Initial deposite
r: interest rate per year
Pn : amount at the nth year: Pn = Pn−1 + rPn−1
=⇒ Pn = (1 + r)n P0 .

17 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
The Tower of Hanoi Problem

The Tower of Hanoi Problem


Three are three pegs mounted on a board together with disks of
different sizes. Initially these disks are placed on the first peg in order
of size, with the largest on the bottom (as shown in Figure 2). The
rules of the puzzle allow disks to be moved one at a time from one peg
to another as long as a disk is never placed on top of a smaller disk.
The goal of the puzzle is to have all the disks on the second peg in
order of size, with the largest on the bottom.
Let Hn denote the number of moves needed to solve the Tower of
Hanoi problem with n disks. Set up a recurrence relation for the
sequence {Hn }.

18 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
The Tower of Hanoi Problem

19 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Content

1 5.1 The Basics of Counting

2 5.2 Recurrence Relations (sect. 7.1)

3 5.3 Divide-and-Conquer Algorithms and Recurrence Relations

20 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Divide-and-Conquer Algorithms and Recurrence
Relations

Divide-and-Conquer Algorithms and Recurrence


Relations
Divide: Dividing a problem into one or more instances of the
same problem of smaller size
Conquer: Using the solutions of the smaller problems to find a
solution of the original problem, perhaps with some additional
work.

21 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Divide-and-Conquer Recurrence Relations

Divide-and-Conquer Recurrence Relations


n: size of the original problem
n/b : size of the sub-problem
f (n) : number of operation required to solve the original problem.
f (n/b) : number of operation required to solve a sub-problem.
g(n): overhead for additional work of the step conquer.
Recurrence relation: f (n) = af (n/b) + g(n). This is called a
divide-and-conquer recurrence relation.

22 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Recurrence Relations for Binary Search

Recurrence Relations for Binary Search


Procedure binary-search(x, i, j)
if i > j then location= 0
m = b(i + j)/2c
if x = am then location= m
else if x < am then location= binary-search(x, i, m − 1)
else location= binary-search(x, m + 1, j)

23 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Recurrence Relations for Binary Search

Recurrence Relations for Binary Search


Procedure binary-search(x, i, j)
if i > j then location= 0
m = b(i + j)/2c
if x = am then location= m
else if x < am then location= binary-search(x, i, m − 1)
else location= binary-search(x, m + 1, j)

⇒ f (n) = f (n/2) + 2 when n is even.

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

f (n) = 2f (n/2) + 1 when n is even.


24 / 27
Dr. Nguyen Kieu Linh Chapter 5: Counting
Divide-and-Conquer Recurrence Relations

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

Furthermore, when n = bk and a 6= 1, where k is a positive integer,

f (n) = C1 nlogb a + C2 ,

where C1 = f (1) + c/(a − 1) and C2 = −c/(a − 1).

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

You might also like