Ds 5 Function
Ds 5 Function
Nguyen An Khuong,
Tran Tuan Anh, Mai
Chapter 5
Xuan Toan, Tran Hong
Tai
Functions
Discrete Structures for Computing on June 16, 2024
Contents
Functions
Sequences and
Summation
Recursion
Nguyen An Khuong, Tran Tuan Anh, Mai Xuan Toan, Tran Hong
Tai
Faculty of Computer Science and Engineering
University of Technology - VNUHCM
[email protected]
5.1
Contents Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
1 Functions
Contents
Sequences and
Summation
4 Recursion
5.2
Course outcomes Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
L.O.2 Represent and model practical problems with discrete structures Contents
L.O.2.2 Use proving methods: direct, contrapositive, induction One-to-one and Onto
Functions
L.O.2.3 Explain problem modeling using discrete structures
Sequences and
Summation
5.3
Introduction Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
5.4
Function Functions
Nguyen An Khuong,
function f
Tai
Let A and B be nonempty sets. A from A to B is an
assignment of exactly one element of B to each element of A.
• f :A→B
• A: domain (mi·n x¡c ành) of f
• B: codomain (mi·n gi¡ trà ) of f Contents
Recursion
• f maps (¡nh x¤) A to B
f
a b = f (a)
A B
f
5.5
Example Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
Summation
Recursion
y is an image of d
c is a pre-image of z
5.6
Example Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Example
What are domain, codomain, and range of the function that
assigns grades to students includes: student A: 5, B: 3.5, C: 9, D:
5.2, E: 4.9?
Contents
Functions
• f (x) = x2
• Domain: set of all integers
• Range of f : {0, 1, 4, 9, . . .}
5.7
Add and multiply real-valued functions Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Definition
Let f1 and f2 be functions from A to R. Then f1 + f2 and f1 f2
are also functions from A to R defined by
Sequences and
Example Summation
Recursion
5.8
Image of a subset Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Definition
Tai
f (S) = {f (s) | s ∈ S}
Contents
Functions
Sequences and
Summation
Recursion
5.9
One-to-one Functions
Nguyen An Khuong,
ìn ¡nh)
Tai
Contents
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?
5.10
Onto Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
f :A→B to n ¡nh)
is onto or surjective ( if and only if
∀b ∈ B, ∃a ∈ A : f (a) = b
Contents
Functions
Sequences and
Summation
Recursion
• Is f : Z → Z, f (x) = x + 1
onto?
• Is f : Z → Z, f (x) = x2
onto?
5.11
One-to-one and onto (bijection) Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Definition
f :A→B is bijective (one-to-one correspondence) ( song ¡nh) if
and only if f is injective and surjective Contents
Functions
f (3) = C , f (4) = A.
Is f a bijection?
5.12
Example Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
Summation
Recursion
5.13
Inverse function (H m ng֖c) Functions
Nguyen An Khuong,
Recursion
−1
a = f −1 (b) f (b) b = f (a)
A f B
f −1
5.14
Example Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Example
A = {a, b, c} and B = {1, 2, 3} with
Functions
Sequences and
Recursion
Example
Let f :R→R with f (x) = x2 . If f invertible?
5.15
Example Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
f :R→R
Contents
Functions
f (x) = 2x + 1
One-to-one and Onto
Functions
Sequences and
f −1 : R → R Summation
Recursion
x−1
f −1 (x) =
2
5.16
Function Composition Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Definition
Given a pair of functions g : A → B and f : B → C . Then the Contents
5.17
Example Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
Summation
Recursion
5.18
Graphs of Functions Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Example
The graph of f (x) = x2 from Z to Z.
(−3, 9) (3, 9)
Contents
Functions
Sequences and
Summation
(−1, 1) (1, 1)
Recursion
(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}.
5.19
Important Functions Functions
Nguyen An Khuong,
Floor function (
⌊ 12 ⌋ = 0, ⌊3.1⌋ = 3, ⌊7⌋ = 7
Ceiling function (h m tr¦n) of x (⌈x⌉): the smallest integer ≥ x
⌈ 12 ⌉ = 1, ⌈3.1⌉ = 4, ⌈7⌉ = 7
Contents
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Functions
Sequences and
1 1 1 1 1
1, 2 , 4 , 8 , 16 , . . . an = 2n−1 Summation
Example
{an } 5, 11, 17, 23, 29, 35, 41, 47, ... an = 6n − 1
{bn } 1, 7, 25, 79, 241, 727, 2185, ... bn = 3n − 2
5.21
Recurrence Relations Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Contents
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
5.22
Functions
Solution:
Let Pn be the amount in the account after n years. The sequence
Contents
{Pn } satisfies the recurrence relation
Functions
Pn = Pn−1 + 0.11Pn−1 = (1.11)Pn−1 . One-to-one and Onto
Sequences and
Recursion
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.
5.23
Functions
Nguyen An Khuong,
Contents
Functions
Sequences and
Summation
Recursion
5.24
Functions
Nguyen An Khuong,
Solution:
In this sequence, integer 1 appears once, the integer 2 appears
twice, the integer 3 appears three times, and so on. Therefore Contents
n
X n(n + 1) Sequences and
i = 1 + 2 + 3 + ... + n = Summation
i=1
2 Recursion
5.24
Functions
If a and r are real numbers and r ̸= 0, then Xuan Toan, Tran Hong
Tai
n ar n+1
−a
X if r≠ 1
arj = r−1
j=0
(n + 1)a if r = 1.
Chùng minh.
Contents
Functions
Pn j
Let Sn = j=0 ar .
One-to-one and Onto
Functions
Pn Sequences and
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 ̸= 1, then Sn = r−1
Pn
If r = 1, then Sn = j=0 a = (n + 1)a
5.25
Recursion Functions
Nguyen An Khuong,
Sequences and
Summation
Recursion
5.26
Recursive Algorithms Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Definition
An algorithm is called recursive if it solves a problem by reducing
it to an instance of the same problem with smaller input.
Example
Contents
Functions
Give a recursive algorithm for computing n!, where n is a One-to-one and Onto
Functions
nonnegative integer.
Sequences and
Summation
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!}
5.27
Algorithms for Fibonacci Numbers Functions
Nguyen An Khuong,
Functions
Iterative Algorithm One-to-one and Onto
else Recursion
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}
5.28
Tower of Hanoi Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Initially, these disks are placed on the first peg in order of size,
with the largest on the borrom. Contents
Functions
The myth says that the world will end when they finish the
puzzle.
5.29
Tower of Hanoi 64 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
?
1
Contents
2
Functions
3
One-to-one and Onto
Functions
4
Sequences and
5 Summation
Recursion
6
5.30
Tower of Hanoi 1 Disc Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
Summation
Recursion
1
5.31
Tower of Hanoi 1 Disc Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
Summation
Recursion
1
5.32
Tower of Hanoi 1 Disc Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
OK Contents
Functions
Sequences and
Summation
Recursion
1
5.33
Tower of Hanoi 2 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
1 Summation
Recursion
2
5.34
Tower of Hanoi 2 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
Summation
Recursion
2 1
5.35
Tower of Hanoi 2 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
Summation
Recursion
1 2
5.36
Tower of Hanoi 2 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
1 Summation
Recursion
2
5.37
Tower of Hanoi 2 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
OK Contents
Functions
Sequences and
1 Summation
Recursion
2
5.38
Tower of Hanoi 3 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
2 Summation
Recursion
3
5.39
Tower of Hanoi 3 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
2 Summation
Recursion
3 1
5.40
Tower of Hanoi 3 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
Summation
Recursion
3 2 1
5.41
Tower of Hanoi 3 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
1 Summation
Recursion
3 2
5.42
Tower of Hanoi 3 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
1 Summation
Recursion
2 3
5.43
Tower of Hanoi 3 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
Summation
Recursion
1 2 3
5.44
Tower of Hanoi 3 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
2 Summation
Recursion
1 3
5.45
Tower of Hanoi 3 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
2 Summation
Recursion
3
5.46
Tower of Hanoi 3 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
OK 1
Contents
Functions
Sequences and
2 Summation
Recursion
3
5.47
Tower of Hanoi 4 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
1
One-to-one and Onto
Functions
2
Sequences and
3 Summation
Recursion
4
5.48
Tower of Hanoi 4 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
3 Summation
Recursion
4 1
5.49
Tower of Hanoi 4 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
3 Summation
Recursion
4 1 2
5.50
Tower of Hanoi 4 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
3 1 Summation
Recursion
4 2
5.51
Tower of Hanoi 4 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
1 Summation
Recursion
4 3 2
5.52
Tower of Hanoi 4 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
1 Summation
Recursion
4 3 2
5.53
Tower of Hanoi 4 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
1 2 Summation
Recursion
4 3
5.54
Tower of Hanoi 4 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
2 Summation
Recursion
4 3
5.55
Tower of Hanoi 4 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
2 Summation
Recursion
3 4
5.56
Tower of Hanoi 4 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
2 1 Summation
Recursion
3 4
5.57
Tower of Hanoi 4 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
1 Summation
Recursion
2 3 4
5.58
Tower of Hanoi 4 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
1 Summation
Recursion
2 3 4
5.59
Tower of Hanoi 4 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
1 3 Summation
Recursion
2 4
5.60
Tower of Hanoi 4 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
Sequences and
3 Summation
Recursion
2 1 4
5.61
Tower of Hanoi 4 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
3 Summation
Recursion
1 4
5.62
Tower of Hanoi 4 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
Contents
Functions
1
One-to-one and Onto
Functions
2
Sequences and
3 Summation
Recursion
4
5.63
Tower of Hanoi 4 Discs Functions
Nguyen An Khuong,
Tran Tuan Anh, Mai
Xuan Toan, Tran Hong
Tai
OK 1
2
Contents
Functions
Sequences and
3 Summation
Recursion
4
5.64
Tower of Hanoi Functions
Nguyen An Khuong,
Functions
Sequences and
Summation
1 if n=1 Recursion
H(n) =
2H(n − 1) + 1 if n > 1.
Recurrence Solving
H(n) = 2n − 1
If one move takes 1 second, for n = 64