0% found this document useful (0 votes)
43 views88 pages

DS Ch3 Sets and Functions Handout

The document discusses sets and functions. It introduces basic concepts of sets such as set operations, subsets, cardinality, Venn diagrams and special sets like empty sets and singleton sets. It also discusses functions, including one-to-one and onto functions, sequences and summation, and recursion.

Uploaded by

Quân Bảo Cát
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)
43 views88 pages

DS Ch3 Sets and Functions Handout

The document discusses sets and functions. It introduces basic concepts of sets such as set operations, subsets, cardinality, Venn diagrams and special sets like empty sets and singleton sets. It also discusses functions, including one-to-one and onto functions, sequences and summation, and recursion.

Uploaded by

Quân Bảo Cát
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/ 88

Sets and Functions

Nguyen An Khuong,
Huynh Tuong Nguyen

Chapter 4
Sets and Functions Contents

Sets

Discrete Structures for Computer Science (CO1007) Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

Nguyen An Khuong, Huynh Tuong Nguyen


Faculty of Computer Science and Engineering
University of Technology, VNU-HCM
4.1
Sets and Functions
Contents
Nguyen An Khuong,
Huynh Tuong Nguyen

1 Sets
Contents

Sets

2 Set Operation Set Operation

Functions

One-to-one and Onto


Functions
3 Functions
Sequences and
Summation

Recursion
4 One-to-one and Onto Functions

5 Sequences and Summation

6 Recursion

4.2
Sets and Functions
Set Definition
Nguyen An Khuong,
Huynh Tuong Nguyen

• Set is a fundamental discrete structure on which all discrete


structures are built
• Sets are used to group objects, which often have the same Contents

properties Sets

Set Operation

Example Functions

One-to-one and Onto


Functions
• Set of all the students who are currently taking Discrete
Sequences and
Mathematics 1 course. Summation

Recursion
• Set of all the subjects that K2011 students have to take in
the first semester.
• Set of natural numbers N

Definition
A set is an unordered collection of objects.
The objects in a set are called the elements (phần tử ) of the set.
A set is said to contain (chứa) its elements.

4.3
Sets and Functions
Notations
Nguyen An Khuong,
Huynh Tuong Nguyen

Definition Contents

Sets
• a ∈ A: a is an element of the set A
Set Operation
• a∈
/ A: a is not an element of the set A Functions

One-to-one and Onto


Functions

Definition (Set Description) Sequences and


Summation

Recursion
• The set V of all vowels in English alphabet, V = {a, e, i, o, u}
• Set of all real numbers greater than 1???
{x | x ∈ R, x > 1}
{x | x > 1}
{x : x > 1}

4.4
Sets and Functions
Equal Sets
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Definition Sets

Set Operation
Two sets are equal iff they have the same elements.
Functions

One-to-one and Onto


• (A = B) ↔ ∀x(x ∈ A ↔ x ∈ B) Functions

Sequences and
Summation
Example Recursion

• {1, 3, 5} = {3, 5, 1}
• {1, 3, 5} = {1, 3, 3, 3, 5, 5, 5, 5}

4.5
Sets and Functions
Venn Diagram
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets
• John Venn in 1881 Set Operation

• Universal set (tập vũ trụ) is Functions

represented by a rectangle One-to-one and Onto


Functions

• Circles and other Sequences and


Summation
geometrical figures are used
Recursion
to represent sets
• Points are used to represent
particular elements in set

4.6
Sets and Functions
Special Sets
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

• Empty set (tập rỗng ) has no elements, denoted by ∅, or {} Functions

One-to-one and Onto


• A set with one element is called a singleton set Functions

Sequences and
• What is {∅}? Summation

Recursion
• Answer: singleton

4.7
Sets and Functions
Subset
Nguyen An Khuong,
Huynh Tuong Nguyen

Definition Contents

The set A is called a subset (tập con) of B iff every element of A Sets

is also an element of B, denoted by A ⊆ B. Set Operation

Functions
If A 6= B, we write A ⊂ B and say A is a proper subset (tập con One-to-one and Onto
Functions
thực sự) of B.
Sequences and
Summation

Recursion

• ∀x(x ∈ A → x ∈ B)
• For every set S,
(i) ∅ ⊆ S, (ii) S ⊆ S.

4.8
Sets and Functions
Cardinality
Nguyen An Khuong,
Huynh Tuong Nguyen

Definition
If S has exactly n distinct elements where n is non-negative
integers, S is finite set (tập hữu hạn), and n is cardinality (bản Contents
số ) of S, denoted by |S|. Sets

Set Operation

Example Functions

One-to-one and Onto


Functions
• A is the set of odd positive integers less than 10. |A| = 5.
Sequences and
• S is the letters in Vietnamese alphabet, |S| = 29. Summation

Recursion
• Null set |∅| = 0.

Definition
A set that is infinite if it is not finite.

Example

• Set of positive integers is infinite

4.9
Sets and Functions
Power Set
Nguyen An Khuong,
Huynh Tuong Nguyen

Definition
Contents
Given a set S, the power set (tập lũy thừa) of S is the set of all Sets
subsets of the set S, denoted by P (S). Set Operation

Functions

Example One-to-one and Onto


Functions

What is the power set of {0, 1, 2}? Sequences and


Summation
P ({0, 1, 2}) = {∅, {0}, {1}, {2}, {0, 1}, {0, 2}, {1, 2}, {0, 1, 2}} Recursion

Example

• What is the power set of the empty set?


• What is the power set of the set {∅}

4.10
Sets and Functions
Power Set
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions
Theorem
One-to-one and Onto
n
If a set has n elements, then its power set has 2 elements. Functions

Sequences and
Summation

Recursion
Prove using induction!

4.11
Sets and Functions
Ordered n-tuples
Nguyen An Khuong,
Huynh Tuong Nguyen

Definition Contents

The ordered n-tuple (dãy sắp thứ tự) (a1 , a2 , . . . , an ) is the Sets

ordered collection that has a1 as its first element, a2 as its second Set Operation

element, . . ., and an as its nth element. Functions

One-to-one and Onto


Functions

Definition Sequences and


Summation
Two ordered n-tuples (a1 , a2 , . . . , an ) = (b1 , b2 , . . . , bn ) iff ai = bi , Recursion
for i = 1, 2, . . . , n.

Example
2-tuples, or ordered pairs (cặp), (a, b) and (c, d) are equal iff
a = c and b = d

4.12
Sets and Functions
Cartesian Product
Nguyen An Khuong,
Huynh Tuong Nguyen

• René Descartes (1596–1650)

Contents
Definition
Sets
Let A and B be sets. The Cartesian product (tích Đề-các) of A Set Operation

and B, denoted by A × B, is the set of ordered pairs (a, b), where Functions

a ∈ A and b ∈ B. Hence, One-to-one and Onto


Functions

Sequences and
A × B = {(a, b) | a ∈ A ∧ b ∈ B} Summation

Recursion

Example
Cartesian product of A = {1, 2} and B = {a, b, c}. Then

A × B = {(1, a), (1, b), (1, c), (2, a), (2, b), (2, c)}

Show that A × B 6= B × A

4.13
Sets and Functions
Cartesian Product
Nguyen An Khuong,
Huynh Tuong Nguyen

Definition
Contents

Sets

Set Operation
A1 ×A2 ×· · ·×An = {(a1 , a2 , . . . , an ) | ai ∈ Ai for i = 1, 2, . . . , n} Functions

One-to-one and Onto


Functions

Sequences and
Example Summation

Recursion
A = {0, 1}, B = {1, 2}, C = {0, 1, 2}. What is A × B × C?

A×B×C = {(0, 1, 0), (0, 1, 1), (0, 1, 2), (0, 2, 0), (0, 2, 1),
(0, 2, 2), (1, 1, 0), (1, 1, 1), (1, 1, 2), (1, 2, 0),
(1, 2, 1), (1, 2, 2)}

4.14
Sets and Functions
Union
Nguyen An Khuong,
Huynh Tuong Nguyen

Definition
The union (hợp) of A and B Contents

Sets

A ∪ B = {x | x ∈ A ∨ x ∈ B} Set Operation

Functions

One-to-one and Onto


A∪B Functions

Sequences and
Summation

Recursion
A B

• Example:
• {1,2,3} ∪ {2,4} = {1,2,3,4}
• {1,2,3} ∪ ∅ = {1,2,3}

4.15
Sets and Functions
Intersection
Nguyen An Khuong,
Huynh Tuong Nguyen

Definition
The intersection (giao) of A and B Contents

Sets

A ∩ B = {x | x ∈ A ∧ x ∈ B} Set Operation

Functions

One-to-one and Onto


A∩B Functions

Sequences and
Summation

Recursion
A B

Example:
• {1,2,3} ∩ {2,4} = {2}
• {1,2,3} ∩ N = {1,2,3}

4.16
Sets and Functions
Union/Intersection
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

n Functions
[
Ai = A1 ∪ A2 ∪ ... ∪ An = {x | x ∈ A1 ∨ x ∈ A2 ∨ ... ∨ x ∈ An } One-to-one
Functions
and Onto

i=1 Sequences and


Summation
n
\ Recursion
Ai = A1 ∩ A2 ∩ ... ∩ An = {x | x ∈ A1 ∧ x ∈ A2 ∧ ... ∧ x ∈ An }
i=1

4.17
Sets and Functions
Difference
Nguyen An Khuong,
Huynh Tuong Nguyen

Definition
The difference (hiệu) of A and B Contents

Sets

A − B = {x | x ∈ A ∧ x ∈
/ B} Set Operation

Functions

One-to-one and Onto


A−B Functions

Sequences and
Summation

Recursion

A B

Example:
• {1,2,3} - {2,4} = {1,3}
• {1,2,3} - N = ∅

4.18
Sets and Functions
Complement
Nguyen An Khuong,
Huynh Tuong Nguyen

Definition
The complement (phần bù) of A Contents

Sets
A = {x | x ∈A}
/ Set Operation

Functions

One-to-one and Onto


Functions
Example: Sequences and
Summation
• A = {1,2,3} then A = ???
Recursion
• Note that A - B = A ∩ B

4.19
Sets and Functions
Set Identities
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

A∪∅ = A Identity laws Sets

A∩U = A Luật đồng nhất Set Operation

Functions
A∪U = U Domination laws One-to-one and Onto
Functions
A∩∅ = ∅ Luật nuốt Sequences and
Summation
A∪A = A Idempotent laws Recursion
A∩A = A Luật lũy đẳng
(Ā) = A Complementation law
Luật bù

4.20
Sets and Functions
Set Identities
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

A∪B = B∪A Commutative laws Sets

A∩B = B∩A Luật giao hoán Set Operation

Functions
A ∪ (B ∪ C) = (A ∪ B) ∪ C Associative laws One-to-one and Onto
Functions
A ∩ (B ∩ C) = (A ∩ B) ∩ C Luật kết hợp
Sequences and
Summation
A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C) Distributive laws Recursion
A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C) Luật phân phối
A∪B = A∩B De Morgan’s laws
A∩B = A∪B Luật De Morgan

4.21
Sets and Functions
Method of Proofs of Set Equations
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation
To prove A = B, we could use Functions
• Venn diagrams One-to-one and Onto
Functions
• Prove that A ⊆ B and B ⊆ A Sequences and
Summation
• Use membership table
Recursion

• Use set builder notation and logical equivalences

4.22
Sets and Functions
Example (1)
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

Example
Verify the distributive rule P ∪ (Q ∩ R) = (P ∪ Q) ∩ (P ∪ R)

4.23
Sets and Functions
Example (2)
Nguyen An Khuong,
Huynh Tuong Nguyen

Example Contents

Sets
Prove: A ∩ B = A ∪ B Set Operation

(1) Show that A ∩ B ⊆ A ∪ B Functions

One-to-one and Onto


Suppose that x ∈ A ∩ B Functions

Sequences and
By the definition of complement, x ∈
/ A∩B Summation

So, x ∈
/ A or x ∈/B Recursion

Hence, x ∈ Ā or x ∈ B̄
We conclude, x ∈ A ∪ B
Or, A ∩ B ⊆ A ∪ B
(2) Show that A ∪ B ⊆ A ∩ B

4.24
Sets and Functions
Example (3)
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets
Prove: A ∩ B = A ∪ B
Set Operation

Functions
A B A∩B A∩B Ā ∪ B̄ One-to-one and Onto
Functions
1 1 1 0 0 Sequences and
1 0 0 1 1 Summation

Recursion
0 1 0 1 1
0 0 0 1 1

4.25
Sets and Functions
Example (4)
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Prove: A ∩ B = A ∪ B Sets

Set Operation
A∩B = {x|x 6∈ A ∩ B} Functions

= {x|¬(x ∈ A ∩ B)} One-to-one and Onto


Functions
= {x|¬(x ∈ A ∧ x ∈ B)}
Sequences and
= {x|¬(x ∈ A) ∨ ¬(x ∈ B)} Summation

= {x|x 6∈ A ∨ x 6∈ B} Recursion

= {x|x ∈ A ∨ x ∈ B}
= {x|x ∈ A ∪ B}

4.26
Sets and Functions
Introduction
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

• Each student is assigned a grade from set Set Operation

{0, 0.1, 0.2, 0.3, . . . , 9.9, 10.0} at the end of semester Functions

One-to-one and Onto


• Function is extremely important in mathematics and Functions

computer science Sequences and


Summation
• linear, polynomial, exponential, logarithmic,... Recursion

• Don’t worry! For discrete mathematics, we need to


understand functions at a basic set theoretic level

4.27
Sets and Functions
Function
Nguyen An Khuong,
Huynh Tuong Nguyen
Definition
Let A and B be nonempty sets. A function f from A to B is an
assignment of exactly one element of B to each element of A.
Contents

• f :A→B Sets

Set Operation
• A: domain (miền xác định) of f
Functions
• B: codomain (miền giá trị) of f One-to-one and Onto
Functions
• For each a ∈ A, if f (a) = b
Sequences and
• b is an image (ảnh) of a Summation

• a is pre-image (nghịch ảnh/tạo ảnh) of f (a) Recursion

• Range of f is the set of all images of elements of A


• f maps (ánh xạ) A to B

f
a b = f (a)

A B
f
4.28
Sets and Functions
Example
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents
Example: Sets

• y is an image of d Set Operation

• c is a pre-image of z Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

4.29
Sets and Functions
Example
Nguyen An Khuong,
Huynh Tuong Nguyen

Example
Contents
What are domain, codomain, and range of the function that Sets
assigns grades to students includes: student A: 5, B: 3.5, C: 9, D: Set Operation
5.2, E: 4.9? Functions

One-to-one and Onto


Functions
Example
Sequences and
Summation
Let f : Z → Z assign the the square of an integer to this integer.
Recursion
What is f (x)? Domain, codomain, range of f ?
• f (x) = x2
• Domain: set of all integers
• Codomain: Set of all integers
• Range of f : {0, 1, 4, 9, . . .}

4.30
Sets and Functions
Add and multiply real-valued functions
Nguyen An Khuong,
Huynh Tuong Nguyen

Definition
Let f1 and f2 be functions from A to R. Then f1 + f2 and f1 f2 Contents
are also functions from A to R defined by Sets

Set Operation
(f1 + f2 )(x) = f1 (x) + f2 (x) Functions

One-to-one and Onto


(f1 f2 )(x) = f1 (x)f2 (x) Functions

Sequences and
Summation

Recursion
Example
Let f1 (x) = x2 and f2 (x) = x − x2 . What are the functions
f1 + f2 and f1 f2 ?

(f1 + f2 )(x) = f1 (x) + f2 (x) = x2 + x − x2 = x


(f1 f2 )(x) = f1 (x)f2 (x) = x2 (x − x2 ) = x3 − x4

4.31
Sets and Functions
Image of a subset
Nguyen An Khuong,
Huynh Tuong Nguyen

Definition
Contents
Let f : A → B and S ⊆ A. The image of S: Sets

Set Operation
f (S) = {f (s) | s ∈ S} Functions

One-to-one and Onto


Functions

Sequences and
Summation
f ({a, b, c, d}) = {x, y, z}
Recursion

4.32
Sets and Functions
One-to-one
Nguyen An Khuong,
Huynh Tuong Nguyen

Definition Contents

Sets
A function f is one-to-one or injective (đơn ánh) if and only if
Set Operation

Functions
∀a∀b (f (a) = f (b) → a = b)
One-to-one and Onto
Functions

Sequences and
Summation

Recursion
• Is f : Z → Z, f (x) = x + 1
one-to-one?
• Is f : Z → Z, f (x) = x2
one-to-one?

4.33
Sets and Functions
Onto
Nguyen An Khuong,
Huynh Tuong Nguyen

Definition Contents

f : A → B is onto or surjective (toàn ánh) if and only if Sets

Set Operation

Functions
∀b ∈ B, ∃a ∈ A : f (a) = b
One-to-one and Onto
Functions

Sequences and
Summation

Recursion
• Is f : Z → Z, f (x) = x + 1
onto?
• Is f : Z → Z, f (x) = x2
onto?

4.34
Sets and Functions
One-to-one and onto (bijection)
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents
Definition Sets

f : A → B is bijective (one-to-one correspondence) (song ánh) if Set Operation

and only if f is injective and surjective Functions

One-to-one and Onto


Functions

Sequences and
• Let f be the function from Summation

Recursion
{a, b, c, d} to {1, 2, 3, 4}
with f (a) = 4, f (b) = 2,
f (c) = 1, f (d) = 3. Is f a
bijection?

4.35
Sets and Functions
Example
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

4.36
Sets and Functions
Inverse function (Hàm ngược)
Nguyen An Khuong,
Huynh Tuong Nguyen
Definition
Let f : A → B be a bijection then the inverse of f is the function
f − : B → A defined by
Contents

if f (a) = b then f − (b) = a Sets

Set Operation

A one-to-one correspondence is call invertible (khả nghịch) Functions

because we can define the inverse of this function. One-to-one and Onto
Functions

Sequences and
f (a) Summation

Recursion

a = f −1 (b) f −1 (b) b = f (a)

A f B

f −1
4.37
Sets and Functions
Example
Nguyen An Khuong,
Huynh Tuong Nguyen

Example Contents

Sets
A = {a, b, c} and B = {1, 2, 3} with
Set Operation

Functions
f (a) = 2 f (b) = 3 f (c) = 1
One-to-one and Onto
Functions

f is invertible and its inverse is Sequences and


Summation

f −1 (1) = c f −1 (2) = a f −1 (3) = b Recursion

Example
Let f : R → R with f (x) = x2 . If f invertible?

4.38
Sets and Functions
Example
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets
f :R→R Set Operation

Functions

One-to-one and Onto


f (x) = 2x + 1 Functions

Sequences and
Summation
−1
f :R→R Recursion

x−1
f −1 (x) =
2

4.39
Sets and Functions
Function Composition
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets
Definition Set Operation

Given a pair of functions g : A → B and f : B → C. Then the Functions

composition (hợp thành) of f and g, denoted f ◦ g is defined by One-to-one and Onto


Functions

Sequences and
f ◦g :A→C Summation

Recursion

f ◦ g(a) = f (g(a))

4.40
Sets and Functions
Example
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

4.41
Sets and Functions
Graphs of Functions
Nguyen An Khuong,
Huynh Tuong Nguyen

Example
The graph of f (x) = x2 from Z to Z.
Contents

Sets
(−3, 9) (3, 9)
Set Operation

Functions

One-to-one and Onto


Functions
(−2, 4) (2, 4) Sequences and
Summation

Recursion
(−1, 1) (1, 1)
(0, 0)

Definition
Let f be a function from the set A to the set B. The graph of the
function f is the set of ordered pairs {(a, b) | a ∈ A and f (a) = b}.

4.42
Sets and Functions
Important Functions
Nguyen An Khuong,
Huynh Tuong Nguyen
Definition
Floor function (hàm sàn) of x (bxc): the largest integer ≤ x
b 12 c = 0, b3.1c = 3, b7c = 7
Ceiling function (hàm trần) of x (dxe): the smallest integer ≥ x Contents

d 21 e = 1, d3.1e = 4, d7e = 7 Sets

Set Operation

Functions

One-to-one and Onto


Bảng: Properties (n is an integer, x is a real number) Functions

Sequences and
Summation
(1a) bxc = n iff n≤x<n+1
Recursion
(1b) dxe = n iff n−1<x≤n
(1c) bxc = n iff x−1<n≤x
(1d) dxe = n iff x≤n<x+1
(2) x − 1 < bxc ≤ dxe < x + 1
(3a) b−xc = −dxe
(3b) d−xe = −bxc
(4a) bx + nc = bxc + n
(4b) dx + ne = dxe + n
4.43
Sets and Functions
Sequences
Nguyen An Khuong,
Huynh Tuong Nguyen

What are the rule of these sequences (dãy )?


Example Contents

1, 3, 5, 7, 9, . . . an = 2n − 1 Sets

Arithmetic sequence (cấp số cộng ) Set Operation

Functions

One-to-one and Onto


Functions

Example Sequences and


Summation
1 1 1 1 1
1,2 , 4 , 8 , 16 , . . . an = 2n−1 Recursion

Geometric sequence (cấp số nhân)

Example
{an } 5, 11, 17, 23, 29, 35, 41, 47, . . . an = 6n − 1
{bn } 1, 7, 25, 79, 241, 727, 2185, . . . bn = 3n − 2

4.44
Sets and Functions
Recurrence Relations
Nguyen An Khuong,
Huynh Tuong Nguyen
Example
{an } 5, 11, 17, 23, 29, 35, 41, 47, . . .
an = an−1 + 6 for n = 2, 3, 4, . . . and a1 = 5 Contents
Recurrence relations: công thức truy hồi Sets

Set Operation

Functions

Definition (Fibonacci Sequence) One-to-one and Onto


Functions

Initial condition: f0 = 0 and f1 = 1 Sequences and


Summation
fn = fn−1 + fn−2 for n = 2, 3, 4, . . . Recursion

Example
Find the Fibonacci numbers f2 , f3 , f4 , f5 and f6
f2 = f1 + f0 =1+0=1
f3 = f2 + f1 =1+1=2
f4 = f3 + f2 =2+1=3
f5 = f4 + f3 =3+2=5
f6 = f5 + f4 =5+3=8
4.45
Sets and Functions

Exercise (1) Nguyen An Khuong,


Huynh Tuong Nguyen
Initial deposit: $10,000
Interest: 11%/year, compounded annually (lãi suất kép)
After 30 years, how much do you have in your account? Contents

Sets
Solution:
Set Operation
Let Pn be the amount in the account after n years. The sequence Functions
{Pn } satisfies the recurrence relation One-to-one and Onto
Pn = Pn−1 + 0.11Pn−1 = (1.11)Pn−1 . Functions

Sequences and
The initial condition is P0 = 10, 000 Summation

Recursion
Step 1. Solve the recurrence relation (iteration technique)
P1 = (1.11)P0
P2 = (1.11)P1 = (1.11)2 P0
P3 = (1.11)P2 = (1.11)3 P0
..
.
Pn = (1.11)Pn−1 = (1.11)n P0 .
Step 2. Calculate
P30 = (1.11)30 10, 000 = $228, 922.97.
4.46
Sets and Functions

Nguyen An Khuong,
Huynh Tuong Nguyen
Exercise (2)
What is the 2012th number in the sequence {xn }: 1, 2, 2, 3, 3, 3,
4, 4, 4, 4, 5, 5, 5, 5, 5, 6,. . .
Contents
Solution: Sets
In this sequence, integer 1 appears once, the integer 2 appears Set Operation

twice, the integer 3 appears three times, and so on. Therefore Functions

integer n appears n times in the sequence. One-to-one and Onto


Functions

We can prove that (try it!) Sequences and


Summation
n
X n(n + 1) Recursion
i = 1 + 2 + 3 + ... + n =
i=1
2
and can easily calculate that
X62
i = 1953
i=1
so the next 63 numbers (until 2016) is 63.
Therefore, 2012th number in the sequence is 63.

4.47
Sets and Functions

Theorem Nguyen An Khuong,


Huynh Tuong Nguyen
If a and r are real numbers and r 6= 0, then
n  ar n+1 −a
X if r 6= 1
arj = r−1
j=0
(n + 1)a if r = 1. Contents

Sets

Set Operation

Functions
Chứng minh. One-to-one and Onto
Pn Functions
j
Let Sn = j=0 ar . Sequences and
Summation
Pn
rSn = r j=0 arj Recursion
Pn
= j=0 arj+1
Pn+1
=  k=1 ark 
Pn k
= k=0 ar + (arn+1 − a)
= Sn + (arn+1 − a)

ar n+1 −a
Solving for Sn shows that if r 6= 1, then Sn = r−1
Pn
If r = 1, then Sn = j=0 a = (n + 1)a

4.48
Sets and Functions
Recursion
Nguyen An Khuong,
Huynh Tuong Nguyen
Definition (Recurrence Relation)
An equation that recursively defines a sequence.
Contents

Definition (Recursion (đệ quy)) Sets

Set Operation
The act of defining an object (usually a function) in terms of that Functions
object itself. One-to-one and Onto
Functions

Sequences and
Summation

Recursion

4.49
Sets and Functions
Recursive Algorithms
Nguyen An Khuong,
Huynh Tuong Nguyen

Definition
An algorithm is called recursive if it solves a problem by reducing Contents
it to an instance of the same problem with smaller input. Sets

Set Operation

Functions
Example One-to-one and Onto
Functions
Give a recursive algorithm for computing n!, where n is a Sequences and
nonnegative integer. Summation

Recursion

Solution. We base on the recursive definition of n!:


n! = n · (n − 1)! and 0! = 1.
procedure factorial (n: nonnegative integer)
if n = 0 then return 1
else return n· factorial (n - 1)
{output is n!}

4.50
Sets and Functions
Algorithms for Fibonacci Numbers
Nguyen An Khuong,
Huynh Tuong Nguyen
Recursive Algorithm
procedure fibonacci(n: nonnegative integer)
if n = 0 then return 0
else if n = 1 then return 1 Contents

Sets
else return fibonacci(n-1) + fibonacci(n-2)
Set Operation
{output is fibonacci(n)}
Functions

One-to-one and Onto


Iterative Algorithm Functions

Sequences and
procedure iterative fibonacci(n: nonnegative integer) Summation

if n = 0 then return 0 Recursion

else
x := 0
y := 1
for i := 1 to n − 1
z := x + y
x := y
y := z
return y
{output is the nth Fibonacci number}
4.51
Sets and Functions
Tower of Hanoi
Nguyen An Khuong,
Huynh Tuong Nguyen

There is a tower in Hanoi that has three pegs mounted on a board


Contents
together with 64 gold disks of different sizes. Sets

Initially, these disks are placed on the first peg in order of size, Set Operation

with the largest on the borrom. Functions

One-to-one and Onto


Functions
The rules:
Sequences and
1 Move one at a time from one peg to another Summation

Recursion
2 A disk is never placed on top of a smaller disk

Goals: all the disks on the third peg in order of size.

The myth says that the world will end when they finish the
puzzle.

4.52
Sets and Functions
Tower of Hanoi – 64 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

?
Set Operation

Functions
2
One-to-one and Onto
3 Functions

Sequences and
4 Summation

5 Recursion

4.53
Sets and Functions
Tower of Hanoi – 1 Disc
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

4.54
Sets and Functions
Tower of Hanoi – 1 Disc
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

Moved disc from peg 1 to peg 3.

4.55
Sets and Functions
Tower of Hanoi – 1 Disc
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

OK
Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

4.56
Sets and Functions
Tower of Hanoi – 2 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

1 Recursion

4.57
Sets and Functions
Tower of Hanoi – 2 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

2 1

Moved disc from peg 1 to peg 2.

4.58
Sets and Functions
Tower of Hanoi – 2 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

1 2

Moved disc from peg 1 to peg 3.

4.59
Sets and Functions
Tower of Hanoi – 2 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

1 Recursion

Moved disc from peg 2 to peg 3.

4.60
Sets and Functions
Tower of Hanoi – 2 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

OK
Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

1 Recursion

4.61
Sets and Functions
Tower of Hanoi – 3 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
1 Summation

2 Recursion

4.62
Sets and Functions
Tower of Hanoi – 3 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

2 Recursion

3 1

Moved disc from peg 1 to peg 3.

4.63
Sets and Functions
Tower of Hanoi – 3 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

3 2 1

Moved disc from peg 1 to peg 2.

4.64
Sets and Functions
Tower of Hanoi – 3 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

1 Recursion

3 2

Moved disc from peg 3 to peg 2.

4.65
Sets and Functions
Tower of Hanoi – 3 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

1 Recursion

2 3

Moved disc from peg 1 to peg 3.

4.66
Sets and Functions
Tower of Hanoi – 3 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

Recursion

1 2 3

Moved disc from peg 2 to peg 1.

4.67
Sets and Functions
Tower of Hanoi – 3 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

2 Recursion

1 3

Moved disc from peg 2 to peg 3.

4.68
Sets and Functions
Tower of Hanoi – 3 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
1 Summation

2 Recursion

Moved disc from peg 1 to peg 3.

4.69
Sets and Functions
Tower of Hanoi – 3 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

OK
Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
1 Summation

2 Recursion

4.70
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


1 Functions

Sequences and
2 Summation

3 Recursion

4.71
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
2 Summation

3 Recursion

4 1

Moved disc from peg 1 to peg 2.

4.72
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

3 Recursion

4 1 2

Moved disc from peg 1 to peg 3.

4.73
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

3 1 Recursion

4 2

Moved disc from peg 2 to peg 3.

4.74
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

1 Recursion

4 3 2

Moved disc from peg 1 to peg 2.

4.75
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

1 Recursion

4 3 2

Moved disc from peg 3 to peg 1.

4.76
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

1 2 Recursion

4 3

Moved disc from peg 3 to peg 2.

4.77
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
1 Summation

2 Recursion

4 3

Moved disc from peg 1 to peg 2.

4.78
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
1 Summation

2 Recursion

3 4

Moved disc from peg 1 to peg 3.

4.79
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

2 1 Recursion

3 4

Moved disc from peg 2 to peg 3.

4.80
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

1 Recursion

2 3 4

Moved disc from peg 2 to peg 1.

4.81
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

1 Recursion

2 3 4

Moved disc from peg 3 to peg 1.

4.82
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

1 3 Recursion

2 4

Moved disc from peg 2 to peg 3.

4.83
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
Summation

3 Recursion

2 1 4

Moved disc from peg 1 to peg 2.

4.84
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


Functions

Sequences and
2 Summation

3 Recursion

1 4

Moved disc from peg 1 to peg 3.

4.85
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

Set Operation

Functions

One-to-one and Onto


1 Functions

Sequences and
2 Summation

3 Recursion

Moved disc from peg 2 to peg 3.

4.86
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen

Contents

Sets

OK
Set Operation

Functions

One-to-one and Onto


1 Functions

Sequences and
2 Summation

3 Recursion

4.87
Sets and Functions
Tower of Hanoi
Nguyen An Khuong,
Algorithm Huynh Tuong Nguyen

procedure hanoi(n, A, B, C)
if n = 1 then move the disk from A to C
else Contents

call hanoi(n − 1, A, C, B) Sets

Set Operation
move disk n from A to C
Functions
call hanoi(n − 1, B, A, C)
One-to-one and Onto
Functions

Sequences and
Recurrence Relation Summation

Recursion

1 if n = 1
H(n) =
2H(n − 1) + 1 if n > 1.

Recurrence Solving
H(n) = 2n − 1
If one move takes 1 second, for n = 64

264 − 1 ≈ 2 × 1019 sec


≈ 500 billion years!.
4.88

You might also like