Lecture 7
Lecture 7
Functions
A function f : X → Y is defined to be one-one (or injective), if the
images of distinct elements of X under f are distinct, i.e., for every
x1 , x2 ∈ X, f(x1 ) = f(x2 ) implies x1 = x2 . Otherwise, f is called
many-one.
One-to-One Illustration
• Graph representations of functions that are
(or not) one-to-one:
• • • •
• • •
• • • •
• • •
• • •
• • • • •
• •
• •
•
Not one-to-one Not even a
One-to-one function!
9
A function f : X → Y is said to be onto (or surjective), if every
element of Y is the image of some element of X under f, i.e., for
every y ∈ Y, there exists an element x in X such that f(x) = y
Illustration of Onto
• Some functions that are or are not onto
their codomains:
•
• • • • • • • •
• • • • • • • •
• • • • • •
• •
• • • • • •
• • • •
Onto Not Onto Both 1-1 1-1 but
(but not 1-1) (or 1-1) and onto not onto
11
Bijections
• A function f is a one-to-one
correspondence, or a bijection, or
reversible, or invertible, if it is both one-
to-one and onto.
12
Function Addition/Multiplication
13
Function Composition
• For functions g:AB and f:BC, there is a
special operator called compose (“○”).
– It composes (i.e., creates) a new function out
of f,g by applying f to the result of g.
(f○g):AC, where (f○g)(a) = f(g(a)).
– Note g(a)B, so f(g(a)) is defined and C.
– The range of g must be a subset of f’s
domain!!
– In general, f○g g○f.
14
Function Composition
Example:
Let f : {2, 3, 4, 5} → {3, 4, 5, 9} and g : {3, 4, 5, 9} → {7, 11, 15} be functions
defined as f(2) = 3, f(3) = 4, f(4) = f(5) = 5 and g (3) = g (4) = 7 and g (5) = g (9) =
11. Find gof.
Solution:
15
Recursion and
Recurrence Relations
Recursive
Examples
Recursion
• Recursive definitions: define an entity in terms of itself.
• There are two parts:
– Basic case (basis): the most primitive case(s) of the entity are
defined without reference to the entity.
– Recursive (inductive) case: new cases of the entity are defined in
terms of simpler cases of the entity.
• Recursive sequences
– A sequence is an ordered list of objects, which is potentially
infinite. s(k) denotes the kth object in the sequence.
– A sequence is defined recursively by explicitly naming the first
value (or the first few values) in the sequence and then define
later values in the sequence in terms of earlier values.
Examples of Recursive
Sequence
• A recursively defined sequence:
– S(1) = 2 …… basis case
– S(n) = 2 * S(n-1), for n >= 2 …… recursive
case
Calculate S(n):
if n = 1 then output 2 and return Calculate S(n):
j=2 if n = 1 then
S=2 return 2
while j <= n else
S = S*2 return 2*S(n-1)
j=j+1 endif
end while output S
Solving Recurrence
Relations
• Closed-form solution
– S(1) = 2
– S(n) = 2*S(n-1) for n >= 2
– Since S(1) = 2 = 21, S(2) = 4 = 22, S(3) = 8 = 23, …
– We can see S(n) = 2n, and it is called a closed-form solution to
the recurrence relation
– Finding a closed-form solution is called solving recurrence
relation
• General method: expand, guess and verify
Example of Expand,
Guess, and Verify
• The factorial example:
– S(1) = 2
– S(n) = 2S(n-1) for n >= 2
• Expand:
S(n) = 2S(n-1) = 2[2S(n-2)] = 22S(n-2)
= 22[2S(n-3)] = 23S(n-3)
• Guess: after k such expansions, the equation
has the form: S(n) = 2kS(n-k)
– it stops when n-k = 1, that is when k = n-1, replace k
with (n-1)
– S(n) = 2n-1S(n-(n-1)) = 2n-1S(1) = 2n
• Verify: proof by induction
Exercise