DS Ch3 Sets and Functions Handout
DS Ch3 Sets and Functions Handout
Nguyen An Khuong,
Huynh Tuong Nguyen
Chapter 4
Sets and Functions Contents
Sets
Functions
Sequences and
Summation
Recursion
1 Sets
Contents
Sets
Functions
Recursion
4 One-to-one and Onto Functions
6 Recursion
4.2
Sets and Functions
Set Definition
Nguyen An Khuong,
Huynh Tuong Nguyen
properties Sets
Set Operation
Example Functions
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
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
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
4.6
Sets and Functions
Special Sets
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
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
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
Recursion
• Null set |∅| = 0.
Definition
A set that is infinite if it is not finite.
Example
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
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
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
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
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
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
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
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
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
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
4.19
Sets and Functions
Set Identities
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
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
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
4.22
Sets and Functions
Example (1)
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
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
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 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
{0, 0.1, 0.2, 0.3, . . . , 9.9, 10.0} at the end of semester Functions
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
f
a b = f (a)
A B
f
4.28
Sets and Functions
Example
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Example: Sets
• c is a pre-image of z 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
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
Sequences and
Summation
Recursion
Example
Let f1 (x) = x2 and f2 (x) = x − x2 . What are the functions
f1 + f2 and f1 f2 ?
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
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
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
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
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
Set Operation
because we can define the inverse of this function. One-to-one and Onto
Functions
Sequences and
f (a) Summation
Recursion
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
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
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
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
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
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
Set Operation
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
1, 3, 5, 7, 9, . . . an = 2n − 1 Sets
Functions
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
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
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
4.47
Sets and Functions
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
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
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
Sequences and
procedure iterative fibonacci(n: nonnegative integer) Summation
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
Initially, these disks are placed on the first peg in order of size, Set Operation
Recursion
2 A disk is never placed on top of a smaller disk
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
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
Sequences and
Summation
Recursion
4.55
Sets and Functions
Tower of Hanoi – 1 Disc
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
OK
Set Operation
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
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
Sequences and
Summation
Recursion
2 1
4.58
Sets and Functions
Tower of Hanoi – 2 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
Summation
Recursion
1 2
4.59
Sets and Functions
Tower of Hanoi – 2 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
Summation
1 Recursion
4.60
Sets and Functions
Tower of Hanoi – 2 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
OK
Set Operation
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
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
Sequences and
Summation
2 Recursion
3 1
4.63
Sets and Functions
Tower of Hanoi – 3 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
Summation
Recursion
3 2 1
4.64
Sets and Functions
Tower of Hanoi – 3 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
Summation
1 Recursion
3 2
4.65
Sets and Functions
Tower of Hanoi – 3 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
Summation
1 Recursion
2 3
4.66
Sets and Functions
Tower of Hanoi – 3 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
Summation
Recursion
1 2 3
4.67
Sets and Functions
Tower of Hanoi – 3 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
Summation
2 Recursion
1 3
4.68
Sets and Functions
Tower of Hanoi – 3 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
1 Summation
2 Recursion
4.69
Sets and Functions
Tower of Hanoi – 3 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
OK
Set Operation
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
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
Sequences and
2 Summation
3 Recursion
4 1
4.72
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
Summation
3 Recursion
4 1 2
4.73
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
Summation
3 1 Recursion
4 2
4.74
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
Summation
1 Recursion
4 3 2
4.75
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
Summation
1 Recursion
4 3 2
4.76
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
Summation
1 2 Recursion
4 3
4.77
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
1 Summation
2 Recursion
4 3
4.78
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
1 Summation
2 Recursion
3 4
4.79
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
Summation
2 1 Recursion
3 4
4.80
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
Summation
1 Recursion
2 3 4
4.81
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
Summation
1 Recursion
2 3 4
4.82
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
Summation
1 3 Recursion
2 4
4.83
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
Summation
3 Recursion
2 1 4
4.84
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
2 Summation
3 Recursion
1 4
4.85
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
Set Operation
Functions
Sequences and
2 Summation
3 Recursion
4.86
Sets and Functions
Tower of Hanoi – 4 Discs
Nguyen An Khuong,
Huynh Tuong Nguyen
Contents
Sets
OK
Set Operation
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
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